/*static char *SCCSID = "@(#)rahreqc.c	6.2 91/10/20";*/
/* BUGBUG: Do I need to wakeup GFB at end?  PIA does. */

/*static char *SCCSID = "@(#)rahreqc.c	6.2 91/10/20";*/

#include "os2def.h"			/* Needed by all. */
#include "const.h"			/* SPB, SECSIZE, needed by all. */
#include "misc.h"			/* Needed by "dir.h". */
#include "buf.h"			/* PBUFNODE, BFL_LWLOCK. */
#include "chain.h"			/* Needed by "dir.h". */
#include "dir.h"			/* Needed by "fsdata.h". */
#include "storage.h"			/* Needed by "tables.h". */
#include "volume.h"			/* PVOLTAB. */
#include "tables.h"			/* Needed by "pr_cache.h". */
#include "frame.h"			/* Needed by "fsdata.h". */
#include "lw.h"			/* Needed by "fsdata.h". */
#include "fsdata.h"			/* Needed by "asyncio.h". */
#include "log.h"			/* WakeupMisc(), LOB_BUFLW. */
#include "pbmacros.h"			/* DECR_HOLDS. */
#include "strat2.h"			/* Needed by "asyncio.h". */
#include "asyncio.h"			/* BEGIN_NOTIFY, PIOH, PLISTENT, etc. */
#include "pr_cache.h"			/* HashFind(). */
#include "pr_misc.h"			/* Used by "log.h"/WakeupMisc(). */
#include "pr_read.h"			/*                                */
#include "pr_ra.h"			/*                                */
#ifdef DEBUG
    #include "pr_debug.h"		/* dprintf(), Int3(). */
#endif


/*
 *	RAHRequestCleanup will unlock each buffer for the request passed to it,
 *	and wakeup anybody waiting for the buffers.  Notes: This routine must
 *	not block, because it is called by the RAHRequestDone interrupt handler
 *	routine.
 *
 *	Note: This routine assumes that ReadAhead set up the request
 *	as a series of full blocks (SPB sectors each).
 *
 *	ENTRY	preq	the pointer to the request.  The device driver has
 *			already set the error and/or status flags in the
 *			request.
 *
 *	EXIT	nothing
 *
 *	HISTORY
 *
 */

VOID
RAHRequestCleanup( PLISTENT preq )				/* must not block */
{

    ULONG ulBlockCount;
    LSN vlsn;
	 PBUFNODE pbuf;

    {
	PVOLTAB pvol = IOHFromListHead(HeadFromIOReq(preq)) -> pvol;

	/* Find first LSN for this request. */
	vlsn = pvol->ulVolBits | preq->RWReq.Start_Block;
    }

    /* Process each cache block for this request. */
    ulBlockCount = preq->RWReq.Block_Count / SPB;
    do {

	    pbuf = HashFind(vlsn);		/* Held, so must be there. */
       ClrLockBUF (pbuf);  // @114986

	    /*  If it's on the LRU list already
	     *  remove it
	     */
//	    if (pbuf->dcbpLru.pbufFwd != pbuf) {
//	      DCREM (pbuf, dcbpLru);
//	    }
	    /*  Put in MRU position
	     */
//	     DCADDF (pbuf, dcbpLru, bufLRUHead);

	    if (pbuf->pWaitChain != 0) {

/* ********
#ifdef DEBUG_LW
	    dprintf(__FILE__": waking up waiter for readahead pbuf=%x, vlsn=%lx\n",
		    pbuf, vlsn);
#endif
  ****** */

	      WakeupMisc2 ( &(pbuf->pWaitChain), LOB_BUFWAIT );
	    }

	vlsn += SPB;
	--ulBlockCount;

    } while (ulBlockCount > 0);


		/*  if necessary, Wakeup on pNoBufWaitChain */
		     if (pNoBufWaitChain != 0) {
		        WakeupMisc2 (&(pNoBufWaitChain), LOB_NOBUF);
		     }


}
