/********************************************************************
 ** NETWORK FILE SYSTEM SERVICE
 **
 ** (c) Spak, Darrell Tam, c9107253@ee.newcastle.edu.au (1994)
 ** phone (Australia) 049-829-710
 **                    49-829-710
 ** (c) SST Jan-Feb-Mar 1994
 **
 ** "CLOSE" file support
 **
 ** TABS to 4
 ********************************************************************/

#include "/snd/everything.h"
#include <st/st_proto.h>

#include "nfsinternal.h"
#include "nfsglobs_protos.h"


struct IFHCloseMsg {
	struct IFHDosMsg	dm;
	struct NFSStdReturn closeret;				/* net-send this back */
};
#define IFH_CLOSEMSG_SIZE sizeof(struct IFHCloseMsg)


/********************************************************************/
	static void free_retret_close(struct PktMsgRecv *pmr)
/********************************************************************/
{
struct IFHCloseMsg	*ifhcm = (void *)pmr->user.user2;
struct InternalFH	*ifh = ifhcm->dm.ifh;

#ifdef DEBUG
	fpf(out, "nfs_close: Freeing memory, %ld, %ld\n", IFH_SIZE, IFH_CLOSEMSG_SIZE);
#endif

	REMOVE_FROM_LIST(ifh, ifh_first);
	FreeRemember(&ifh->mem, TRUE);				/* any appendage on the file handle */
	FreeMem(ifh, IFH_SIZE);						/* file handle */
	FreeMem(ifhcm, IFH_CLOSEMSG_SIZE);			/* dos msg */
	FREE_PKTMSGRECV(pmr);						/* server msg */

}


/********************************************************************/
	static void handle_closefile(struct IFHCloseMsg *ifhcm)
/********************************************************************/
{
struct PktMsgRecv *pmr = ifhcm->dm.pktmsg;

	ifhcm->closeret.remote_res1 = IFHMSG_RES1(&ifhcm->dm);
	ifhcm->closeret.remote_res2 = IFHMSG_RES2(&ifhcm->dm);
	PUT_STDMSG(&pmr->pm.smsg, pmr->dmaport, TYPE_INTERNAL_SEND);
#ifdef DEBUG
	fpf(out, "nfs_close: result going home\n");
#endif

}


/********************************************************************/
	void *srv_closefile(struct PktMsgRecv *pmr)
/********************************************************************/
{
struct NFSCloseFile *cf_req = (void *)pmr->req_body;
struct InternalFH	*ifh = GET_INTERNALFH(cf_req->remote_ifh, cf_req->remote_majic);
struct IFHCloseMsg	*ifhcm;

	if(!ifh) return(&nfs_res_ok);					/* filehandle doesn't exist! */
	ACT_ALLOCMSG_THINGY(ifhcm, IFH_CLOSEMSG_SIZE);	/* get dosmsgpkt+return memory */

#ifdef DEBUG
	fpf(out, "nfs_close: FH=%lx, port=%lx\n", ifh, ifh->realfh.fh_Type);
#endif

	ACT_INIT_RETURN_PMR(pmr, free_retret_close, ifhcm, &ifhcm->closeret, cf_req);
	ifhcm->dm.dosmsg.sp.sp_Pkt.dp_Type = ACTION_END;
	ifhcm->dm.dosmsg.sp.sp_Pkt.dp_Arg1 = ifh->realfh.fh_Arg1;
	ACT_PUT_IFHDOSMSG(ifhcm, dosport, handle_closefile, ifh, pmr);
	return(NULL);
}
