/*static char *SCCSID = "@(#)cch.c      6.4 92/01/31";*/
#define  INCL_DOSINFOSEG
#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>
#include <fsh.h>
#include <pvwxport.h>             /*25118*/
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "volume.h"
#include "storage.h"
#include "tables.h"
#include "pb_ifs.h"
#include "pbmacros.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "pr_CACHE.h"
#include "pr_debug.h"
#include "mmph.h"

#include "pvw.h"                   /*25118*/

/*
 ***    CCH - Check Cache Hit
 *
 *      CCH checks to see if a given physical sector is
 *      in the cache.  CCH doesn't worry about whether or
 *      not the cache block is locked or not, and it assumes
 *      that the supplied sector number is already rounded down
 *      to a multiple of SPB
 *
 *      ENTRY   lsn = physical sector number (must be multiple of SPB)
 *
 *      RETURNS the cache header address
 *              or NULL if not in cache
 *
 */

PBUFNODE
CCH (lsn,pfrm)
LSN lsn;                        /* Physical Sector Number */
PFRAME pfrm;
{
    PBUFNODE pbuf;              /*  buffer to find */

    MMPHPreCCH();             /* Performance Hook (3302) */

    lsn |= pfrm->pvol->ulVolBits;

    while (TRUE) {

#ifdef DEBUG2
        /* check that all the buffers are in the right bucket */
        ChkHashChain ();
#endif

        pbuf = HashFind (lsn);

        if (pbuf != NULL) {
#ifndef VERSION2X
            FSH_EXTENDTIMESLICE();     //#97886
#endif

            /* cache hit increment perfview counter */ /*25118*/
            PVWINC(Ca, Hit);                           /*25118*/
           /*
            * Position cache buffer as MRU since any time CCH is called
            * it's because we want the data to be in the cache.
            * If the data has cache presence it will be referenced
            * immediately; therefore it makes sense to place the cache
            * buffer as MRU.
            */

           /*
            * If it's on the LRU list already remove it and place in MRU
            * position.
            */

          if (pbuf->cLocked == NULL) {

//	        if ((pbuf->dcbpLru.pbufFwd != pbuf) && (pbuf->cLocked == 0)) {
	        if (pbuf->dcbpLru.pbufFwd != pbuf) {
	          DCREM (pbuf, dcbpLru);
           }

//	          if (pbuf->bDirtMask == NULL) {
	           /*  Put in MRU position
	            */
   	         DCADDF (pbuf, dcbpLru, bufLRUHead);
//             }
//              else {
//               pbuf->dcbpLru.pbufFwd = pbuf;
//               pbuf->dcbpLru.pbufBak = pbuf;
//             }

//	        }
          }

           break;
        }
         /* cache miss increment perfview counter */ /*25118*/

          PVWINC(Ca, Miss);                           /*25118*/
        /*      We didn't find the lsn in the cache--if fGFBActive is
         *      set, we'll wait for the process using GFB to finish,
         *      since it might be getting our sector.
         */
        if (!fGFBActive) {
            break;
        }

        GFBWait(pfrm);
    }

    MMPHPostCCH();             /* Performance Hook (3382) */

    return pbuf;
}
