/*static char *SCCSID = "@(#)wrtspare.c	6.3 92/01/31";*/
#define _BTXT _BTXT

#define INCL_DOSERRORS
#include <os2def.h>
#include <bse.h>			/* NO_ERROR. */
#include <fsh.h>			/* DVIO_OPREAD, etc. */

#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "heap.h"			/* Required by "pr_heap.h". */
#include "volume.h"
#include "pbmsg.h"
#include "pbmacros.h"
#include "storage.h"
#include "tables.h"
#include "superb.h"
#include "pb_ifs.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "strat2.h"
#include "asyncio.h"

#include "pr_CACHE.h"
#include "pr_DSKIO.h"
#include "pr_HEAP.h"			/* GHSFar(), RetHeap(). */
#include "pr_MISC.h"			/* INTERR(), My prototype. */
#include "pr_DEBUG.h"

/*
 ***	WriteSparesFlag - Writes the flag byte in the spares block
 *
 *	ENTRY	the flag byte to or into the exiting flag in the spares block.
 *		pfrm->pvol must refer to the volume.
 *	EXIT	none
 *
 *	Yields CPU (IO)
 *
 *	HISTORY
 *	    21-Jul-1989 by JR (John Rogers, JohnRo@Microsoft)
 *		- PTR B704867: Prevent deadlock when writing dirty bit to disk.
 *		- Changed PANICs to INTERRs, per JonPa.
 *		- Documented pfrm->pvol dependency.
 *		- Fixed file name in SCCSID.
 *	    10-Jul-1989 by JR (John Rogers, JohnRo@Microsoft)
 *		- PTR B784106: Update copy of spare block flags in VOLTAB.
 */

void far
WriteSparesFlag (BYTE bFlag, PFRAME pfrm)
{
    LSN lsnSpare = pfrm->pvol->ulVolBits | SEC_SPAREB;
    PSPAREB psb;

    /*
     *	Begin critical section:	 establish sole right to
     *	the hotfix buffer
     */
    SemRequestFar(&semHotFix,pfrm);

    psb = RdHF(lsnSpare, pfrm);

    if ( psb != NULL ) {

//#114986
   	if ((psb->sig1 != SPSIG1) || (psb->sig2 != SPSIG2))
	     INTERR (PBMSG_WRTSPARE_READERROR);    /* not a valid Spare block */

	/*
	 *  OR in the flags the caller wants set and update the
	 *  volume table copy of the spares flag.  We also set
	 *  the dirty bit, since we are writing to the disk.
	 *  (Of course, it may already be set, but that's OK.)
	 *  Note that we have to update the volume table flags
	 *  before the actual write, in order to avoid infinite
	 *  recursion between DoVolIO and WriteSparesFlag.
	 */
	   psb->bFlag |= (bFlag | SPF_DIRT) ;

	   pfrm->pvol->bSpareFlags = psb->bFlag;

	/* Write modified flags to disk.  */
	   WtHF(lsnSpare,pfrm);
    }

   /* end of critical section */
    SemClear(&semHotFix);

}
