/*static char *SCCSID = "@(#)cacheinv.c	6.2 92/01/31";*/
#define _BTXT _BTXT
#include <os2def.h>
#define INCL_DOSERRORS
#include <bse.h>
#include <fsd.h>
#include <fsh.h>

#include "const.h"
#include "chain.h"
#include "misc.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "pbmacros.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"

#include "pr_CACHE.h"
#include "pr_DEBUG.h"
#include "pr_MISC.h"

int
CacheInvalidate (POFT	 poft,
		 PCHAR	 pcParm,
		 USHORT	 usLenParm,
		 PUSHORT pusLenParmOut,
		 PFRAME	 pfrm)

{
    LSN lsnFirstSectorInBlock;
    PBUFNODE pbufCache;
    USHORT usRet;

    if ( ! (poft->bSFlag & OFS_DASD) ) {
	return ERROR_INVALID_FUNCTION;
    }

    if (usLenParm < sizeof(ULONG)) {
	*pusLenParmOut = sizeof (ULONG);
	return ERROR_BAD_LENGTH;
    }

    /* Probe the address of where the sector number is stored. */
    MAYBLOCK (
	usRet = FSH_PROBEBUF (PB_OPREAD, pcParm, sizeof (ULONG));
    );
    if (usRet != NO_ERROR) {
	return usRet;
    }

    /* Compute LSN for first sector in block. */
    lsnFirstSectorInBlock =
	( (*(PULONG)pcParm) & -SPB) |		/* Sector number, */
	poft->pVolume->ulVolBits;		/* Volbits. */

    /* Look and cache, and handle what we find there. */
//    if ((pbufCache = HashFindFar(lsnFirstSectorInBlock)) != 0) {
    if ((pbufCache = HashFind(lsnFirstSectorInBlock)) != 0) {

	/* It's in cache, so process it. */
	if (pbufCache->cLocked != 0) {
	    Block(&pbufCache->pWaitChain,pfrm);	     /* Wait for buffer. */
	}
	if (pbufCache->cusHolds != 0) {
	    return ERROR_BUSY;
	}
	if (pbufCache->dcbpDirt.pbufFwd != 0) {
	    return ERROR_SHARING_VIOLATION;
	}
	pbufCache->lsnSec = LSN_BOGUS;		/* Mark as invalid. */
    } /* if sector in cache */

    return usRet;
}
