/*static char *SCCSID = "@(#)reopen.c	6.2 92/01/31";*/
#define _BTXT _BTXT
#define INCL_DOSERRORS
#include <os2.h>
#include <fsd.h>
#include <fsh.h>
#include "api.h"
#include "const.h"
#include "misc.h"
#include "chain.h"
#include "buf.h"
#include "dir.h"
#include "storage.h"
#include "volume.h"
#include "tables.h"
#include "lw.h"
#include "frame.h"
#include "fsdata.h"
#include "heap.h"
#include "pbmacros.h"
#include "filemode.h"
#include "pb_ifs.h"
#include "pr_fcn.h"
#include "pr_heap.h"
#include "pr_wrsub.h"
#include "pr_fsini.h"
#include "pr_misc.h"
#include "pr_debug.h"

#ifndef MINIFSD

extern BOOL fInit2Free;

/*
 ***	ReopenFiles - reopen files opened by the mini-FSD
 *
 *	This routine mounts all volumes that the mini-FSD has mounted
 *	and opens all files that the mini-FSD has opened.  It is not
 *	actually called by the real FSD, but is called in the context
 *	of the mini-FSD when its MFS_TERM entrypoint is called.  This
 *	allows us to perform all our operations at ring 0.
 *
 *	ENTRY	psffsd = chain of sffsd structures
 *		pvrd  = array of volumes mounted by mini
 *
 *	EXIT	Returns NO_ERROR
 *
 *	HISTORY
 *	    28-Feb-1990 by JR (John Rogers, JohnRo@Microsoft)
 *		- PTR B788315: init swap lock area sooner.
 *	    14-Dec-1988 16:24:30 by Greg Jones [gregj] DCR 139
 */

extern BOOL fCacheInit;			/* Cache already init?  see init2.c */

INT far pascal
ReopenFiles (struct sffsd far *psffsd,
	     VOL_REOPEN_DATA far *pvrd )
{
    USHORT usRet;
    FRAME  frm;

    frm.usHardErr = NO_ERROR;
    frm.fRecheckAddr = TRUE;

#ifdef DEBUG
    dprintf("ReopenFiles: got here\n");
#endif

    /* Initialize heap, so (among other things) we can create frame. */
    HeapInit(&frm);
    fInit = TRUE;

    /* Initialize codepage stuff, lock swap data areas, etc. */
    if (!fCacheInit) {
	if ((usRet = Init2(&frm)) != NO_ERROR) {
	    return (usRet);
	}

	fCacheInit = TRUE;
    }

    usRet = ReopenFilesGuts(psffsd, pvrd, &frm);

    if (usRet != NO_ERROR) {
	return (usRet);
    }

    /* if INIT_SEG1 is not free then free it */
    if (!fInit1Free) {
	usRet = FreeInit1 ();
    }

    /* if INIT_SEG2 is not free then free it */
    if (!fInit2Free) {
	usRet = FreeInit2 ();
    }

#ifdef DEBUG
    dprintf("ReopenFiles: leaving\n");
#endif

    return usRet;
}
#endif
