/********************************************************************
 ** 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
 **
 ** "WRITE" file support
 **
 ** TABS to 4
 ********************************************************************/

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

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


#define IFH_WRITEMSG_SIZE (sizeof(struct IFHWriteMsg))
struct IFHWriteMsg {
	struct IFHDosMsg	dm;
	struct NFSStdReturn writeret;
};

/********************************************************************/
	static void free_retret_write(struct PktMsgRecv *pmr)
/********************************************************************/
{
#ifdef DEBUG
	fpf(out, "nfs_write: Freeing memory\n");
#endif

	FreeMem((void *)pmr->user.user2, IFH_WRITEMSG_SIZE);
	FREE_PKTMSGRECV(pmr);			/* 	everything the server sent */
}


/********************************************************************/
	static void handle_write_dos(struct IFHWriteMsg *ifhwm)
/********************************************************************/
{
struct PktMsgRecv *pmr = ifhwm->dm.pktmsg;
#ifdef DEBUG
	fpf(out, "nfs_write: result going home\n");
#endif


	ifhwm->writeret.remote_res1 = IFHMSG_RES1(&ifhwm->dm);
	ifhwm->writeret.remote_res2 = IFHMSG_RES2(&ifhwm->dm);
	PUT_STDMSG(&pmr->pm.smsg, pmr->dmaport, TYPE_INTERNAL_SEND);
}


/********************************************************************/
	void *srv_write(struct PktMsgRecv *pmr)
/********************************************************************/
{
struct NFSWrite		*req = (void *)pmr->req_body;
struct InternalFH	*ifh = GET_INTERNALFH(req->remote_ifh, req->remote_majic);
struct IFHWriteMsg	*ifhwm;

	if(!ifh) return(&nfs_res_notfound);		/* filehandle doesn't exist! */

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


	if(!pmr->pm.send_body || pmr->pm.send_len <= 0) return(&nfs_res_ok);
											/* nothing to write */
	ACT_ALLOCMSG_THINGY(ifhwm, IFH_WRITEMSG_SIZE);
	ACT_INIT_RETURN_PMR(pmr, free_retret_write, ifhwm, &ifhwm->writeret, req);

	INIT_ARGS_IFHDOSMSG(&ifhwm->dm,
		ACTION_WRITE, ifh->realfh.fh_Arg1, pmr->pm.send_body, pmr->pm.send_len);

	ACT_PUT_IFHDOSMSG(ifhwm, dosport, handle_write_dos, ifh, pmr);
	return(NULL);
}
