/*static char *SCCSID = "@(#)groext.c	6.2 92/01/31";*/
#include <os2.h>
#include "chain.h"
#include "const.h"
#include "misc.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "superb.h"
#include "pb_ifs.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"

#include "assert.h"

#include "pr_ALLOC.h"
#include "pr_BITMP.h"
#include "pr_CACHE.h"


/*
 ***	GroExt - Grow the file's last extent
 *
 *	GroExt is called to grow the file's last extent.
 *	GroExt is a captive of EFA and is called when the
 *	sectors have been located.  They're contiguous with the
 *	last sector allocated to the file.  GroExt will claim them
 *	and add them to the last extent.
 *
 *	We know that there already ARE some sectors allocated and
 *	that the POFT->LastRun field is valid.
 *
 *	ENTRY	poft = pointer to Open File Table
 *		fWriteThru = 1 iff Write Through
 *		culSectors = sectors that we will grow
 *		lsnFirst = first sector # that we'll allocate
 *		NOTE: poft and fbWriteThru were accessed in the
 *			 assembly code by inspecting the stack;
 *			 this pair of parameters replaces EFA Stack
 *			  passed in ebp.
 *
 *	RETURNS TRUE if ok
 *		FALSE if hard error
 *		Yields CPU
 *
 *	NOTE
 *	    GroExt expects to have the sectors we are going to grow
 *	    already subtracted out of pfrm->pvol->csecSpace.
 *
 */

BOOL
GroExt (poft, fWriteThru, culSectors, lsnFirst, pfrm)
POFT poft;		/* pointer to Open File Table */
BOOL fWriteThru;	/* 1 iff Write Through */
ULONG culSectors;	/* number of Sectors to grow */
LSN   lsnFirst;		/* first sector we'll allocate */
PFRAME pfrm;
{
PALSEC	 pals;
PBUFNODE pbufALS;

    /* add sectors in so ClrBit can sub them out */
    poft->pVolume->csecSpace += culSectors;

    pfrm->pvol->csecSpace =
     poft->pVolume->csecSpace;

    /* Claim the bits */
                                                   //#85759
    if (!ClrBit (lsnFirst,culSectors,TRUE,pfrm))
      return FALSE;

    poft -> lsnLast += culSectors;
    poft -> ulFAllocLen += (culSectors * SECSIZE);

    /* BUGBUG - who decs csecVolSpace?? */


    /* check if we have a tree */
   if ((poft->fst.alb.bFlag & ABF_NODE) == 0) {

	/*
	 * All of our extents are in the root ALBLK structure, so we just
	 * grow the last one
	 *
	 * culSectors	(esi) = sectors that we will grow
	 * poft		(edi) = OFT address
	 */
	 poft->LastRun.pallLastRun->csecRun += culSectors;

	 return TRUE;
	}

    /*
     *	We have a contiguous allocation to our last extent which is in a
     *	leaf block of an ALBLK tree.  Get that guy in and increase the
     *	extent size.
     *
     *	We know that the OFT_LRP field is up to date.
     *
     *	culSectors  (esi) = sectors that we'll grow
     *	poft	    (edi) = OFT pointer (length fields already updated)
     */
/* groe10: */
    if ((pals = LSD(&poft->LastRun.scpLastRun,BP_KEEP,&pbufALS,pfrm)) == NULL)
	return FALSE;

    ASSERT_ALS(pals);

    /*
     * BUGBUG - I use cUsed instead of playing with the byte offsets
     *		in oFree because it is much easer to do in 'C'.
     */
    pals->a.aall[ pals->alb.cUsed - 1 ].csecRun += culSectors;

    SetDirt((PBYTE)pals, pbufALS, fWriteThru, pfrm);
    return TRUE;

}
