-- FilePageMgrMainImpl.mesa
-- Last edited by
-- Kolling on February 16, 1984 12:02:55 pm PST
-- MBrown on January 31, 1984 9:01:07 pm PST

DIRECTORY

AlpineEnvironment
USING[FileID, PageCount, PageNumber, PageRun, VolumeID, VolOrVolGroupID],
AlpineInternal
USING[FileHandle],
AlpineZones
USING[static],
Basics
USING[LongDivMod],
File
USING[Create, Delete, Error, FindVolumeFromID, FP, GetProperties, Handle, Info, Open,
PropertyStorage, RC, SetSize, Volume, VolumeID],
FileMap
USING[GetNext, GetVolumeIDAndFileID, Handle, VerifyFilePageMgrHandle],
FilePageMgr
USING[DirtyNoWaitReleaseState, DirtyWaitReleaseState, ReleaseState,
VMPageSet],
FilePageMgrIO
USING[DoIO, GetNext, IORequest, IOType, LogError, RegisterRequest],
FilePageMgrLru
USING[CheckCacheInCleanState, GetOtherChunkFromLruList,
GetOurChunkFromLruList, InitializeLruLists,
LruListPlace, PutMappedChunkOnLruList, PutUnmappedChunkOnLruList,
RelinkChunkAsLruOnLruList, SweepItem, UsingTooMuchOfCache, WaitToSweep],
FilePageMgrPrivateChunk
USING[Chunk, ChunkFilePageCount, ChunkVMPageCount, ChunkType,
ClientChunkType, NLeaderFilePages, RefChunk],
FilePageMgrPrivateFile
USING[FPMFileObject, LeaderFilePageNumber, VolumeState],
OrderedSymbolTable
USING[CreateTable, Delete, Initialize, Insert, Lookup, LookupLargest,
LookupNextLarger, LookupNextSmaller, LookupProc, LookupSmallest,
Table],
Process
USING[Detach, GetCurrent],
VM
USING[AddressForPageNumber, Allocate, Interval, MakeUnchanged, PageCount,
PageNumber, PageNumberForAddress, Pin, State];


FilePageMgrMainImpl: CEDAR MONITOR
LOCKS fpmFileHandle USING fpmFileHandle: FPMFileHandle
IMPORTS AZ: AlpineZones, Basics, File, FileMap, FpmIO: FilePageMgrIO, FpmL:
FilePageMgrLru, OST: OrderedSymbolTable, Process, VM
EXPORTS AlpineInternal, FilePageMgr, FilePageMgrPrivateChunk
SHARES FilePageMgr =


BEGIN OPEN AI: AlpineInternal, AE: AlpineEnvironment, Fpm: FilePageMgr, FpmPC:
FilePageMgrPrivateChunk, FpmPF: FilePageMgrPrivateFile;

VolOrVolGroupID: TYPE = AE.VolOrVolGroupID;
FileID: TYPE = AE.FileID;


FPMFileHandle: TYPE = REF FPMFileObject;
FPMFileObject: PUBLIC TYPE = FpmPF.FPMFileObject;

RefChunk: TYPE = REF Chunk;
Chunk: PUBLIC TYPE = FpmPC.Chunk;


-- Monitors can only be nested in this order:

-- FPMFileObject monitor
-- RedBlackTreeImpl
-- FilePageMgrLruImpl
-- FileMap's FileObject monitor

-- No process ever holds more than one FPMFileObject monitor.


InsufficientSpaceOnVolume: PUBLIC -- ABSTRACTION -- ERROR = CODE;
NoSuchFile: PUBLIC -- CALLING -- ERROR = CODE;
NoSuchVolume: PUBLIC -- CALLING -- ERROR = CODE;
PageRunArgIllegal: PUBLIC -- CALLING -- ERROR = CODE;
PageRunExtendsPastEof: PUBLIC -- CALLING -- ERROR = CODE;
SizeArgIllegal: PUBLIC -- CALLING -- ERROR = CODE;
VolumeTooFragmented: PUBLIC -- ABSTRACTION -- ERROR = CODE;
VolumeWentOffline: PUBLIC -- ABSTRACTION -- ERROR = CODE;


VolumeError: ARRAY FpmPF.VolumeState OF ERROR ← [online: InternalFilePageMgrLogicError,
wentOffline: VolumeWentOffline, nonExist: NoSuchVolume];
InternalFilePageMgrLogicError
: PUBLIC -- PROGRAMMING -- ERROR = CODE;


Okay: ERROR = CODE; -- for our own use.

ReadDone: CONDITION;
WriteDone: CONDITION;


ReadPages: PUBLIC PROCEDURE[fileHandle: AI.FileHandle, pageRun: AE.PageRun]
RETURNS [vMPageSet: Fpm.VMPageSet] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, PageRunArgIllegal, PageRunExtendsPastEof, VolumeWentOffline.
vMPageSet ← BasicGetPages[fileHandle, pageRun, read, normal].vMPageSet;
END;

ReadLogPages: PUBLIC PROCEDURE[fileHandle: AI.FileHandle, pageRun: AE.PageRun]
RETURNS [vMPageSet: Fpm.VMPageSet] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, PageRunArgIllegal, PageRunExtendsPastEof, VolumeWentOffline.
vMPageSet ← BasicGetPages[fileHandle, pageRun, read, log].vMPageSet;
END;

ReadLeaderPages: PUBLIC SAFE PROCEDURE[fileHandle: AI.FileHandle] RETURNS [vMPageSet:
Fpm.VMPageSet] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, VolumeWentOffline.
vMPageSet ← BasicGetPages[fileHandle, [0, 0], read, leader].vMPageSet;
END;

UsePages: PUBLIC PROCEDURE[fileHandle: AI.FileHandle, pageRun: AE.PageRun]
RETURNS [vMPageSet: Fpm.VMPageSet] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, PageRunArgIllegal, PageRunExtendsPastEof, VolumeWentOffline.
vMPageSet ← BasicGetPages[fileHandle, pageRun, use, normal].vMPageSet;
END;


UseLogPages: PUBLIC PROCEDURE[fileHandle: AI.FileHandle, pageRun: AE.PageRun]
RETURNS [vMPageSet: Fpm.VMPageSet] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, PageRunArgIllegal, PageRunExtendsPastEof, VolumeWentOffline.
vMPageSet ← BasicGetPages[fileHandle, pageRun, use, log].vMPageSet;
END;


UseLeaderPages
: PUBLIC PROCEDURE[fileHandle: AI.FileHandle] RETURNS [vMPageSet:
Fpm.VMPageSet] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, VolumeWentOffline.
vMPageSet ← BasicGetPages[fileHandle, [0, 0], use, leader].vMPageSet;
END;


MyPageRun: TYPE = RECORD[firstPage: AE.PageNumber, count: NAT, chunkStartFilePage: AE.PageNumber];
ReadReadAheadOrUse: TYPE = {read, readAhead, use};

-- the parameters ReadReadAheadOrUse and ChunkType are used by BasicGetPages as follows:

-- ChunkType:
-- (a) each chunk type may have a different size, which affects the
-- conversion of the client's page number, etc. to the chunk
-- page number, etc.
-- (b) tells which lru list to go to to get a new chunk.
-- (c) special handling for leader (due to its being handled differently by File).

-- ReadReadAheadOrUse (except leader):
-- (a) read may do a read, use doesn't have to read unless partial chunk, readAhead's
-- caller will do the reads in a bunch.


BasicGetPages: PROCEDURE[fileHandle: AI.FileHandle, pageRun: AE.PageRun,
readReadAheadOrUse: ReadReadAheadOrUse, chunkType: FpmPC.ClientChunkType]
RETURNS [vMPageSet: Fpm.VMPageSet, nFilePagesToRead: AE.PageCount] = -- the peculiar structure is to avoid monitor conflicts.
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, PageRunArgIllegal, PageRunExtendsPastEof, VolumeWentOffline.
fpmFileHandle: FPMFileHandle ← GetFilePageMgrHandle[fileHandle, FALSE];
myPageRun: MyPageRun;
otherRefChunk: RefChunk ← NIL;
errors: ERROR;
done: BOOLEAN;
IF ((chunkType # leader) AND ((pageRun.firstPage < 0) OR (pageRun.count = 0)))
THEN ERROR PageRunArgIllegal;
myPageRun ← [firstPage: pageRun.firstPage, count: pageRun.count,
chunkStartFilePage: ChunkStartFilePage[pageRun.firstPage, chunkType]];
DO
[errors, done, nFilePagesToRead, vMPageSet] ←
MonitoredFindOldOrGiveNewChunk[fpmFileHandle, fileHandle, otherRefChunk,
myPageRun, readReadAheadOrUse, chunkType];
IF errors # Okay THEN ERROR errors;
IF done
THEN BEGIN
IF
((nFilePagesToRead # 0) AND (readReadAheadOrUse # readAhead))
THEN
BEGIN
refChunk: RefChunk ← vMPageSet.refChunk;
FpmIO.DoIO[read, fpmFileHandle.lowerHandle,
[[refChunk.startFilePageNumber], nFilePagesToRead,
VMAddressForPageNumber[refChunk.startVMPageNumber]]];
MakeAllPagesInChunkClean[refChunk];
MonitoredSetChunkValidAfterIO[fpmFileHandle, vMPageSet.refChunk,
readCompleted];
END;
RETURN
;
END
;
IF otherRefChunk = NIL
THEN otherRefChunk ← MonitoredOtherGetNewChunk[chunkType]
ELSE ERROR InternalFilePageMgrLogicError;
ENDLOOP;
END;


-- try to find the requested chunk. If not found, maybe we have been given one we can use. Inc the useCount.

MonitoredFindOldOrGiveNewChunk: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle,
fileHandle: AI.FileHandle, otherRefChunk: RefChunk, myPageRun: MyPageRun,
readReadAheadOrUse: ReadReadAheadOrUse, chunkType: FpmPC.ClientChunkType] RETURNS
[errors: ERROR, done: BOOLEAN, nFilePagesToRead: AE.PageCount, vMPageSet:
Fpm.VMPageSet] = -- values of errors are {NoSuchFile, NoSuchVolume, PageRunExtendsPastEof, VolumeWentOffline}
BEGIN -- non system fatal errors: none.
refChunk: RefChunk;
new, wholeChunk: BOOLEAN;
chunkTrueSize: AE.PageCount;
[errors, refChunk] ← GetMappedChunk[fpmFileHandle, myPageRun, readReadAheadOrUse];
IF errors # Okay THEN BEGIN IF otherRefChunk # NIL THEN
FpmL.PutUnmappedChunkOnLruList[otherRefChunk]; RETURN; END;
new ← (refChunk = NIL);
SELECT TRUE FROM
new => IF otherRefChunk = NIL
THEN RETURN[Okay, FALSE, 0, [NIL, [0, 0], NIL]]
ELSE BEGIN refChunk ← otherRefChunk;
MapChunk[fpmFileHandle, fileHandle,
myPageRun.chunkStartFilePage, refChunk];
IF
refChunk.chunkType = leader
THEN
BEGIN
propStorage: File.PropertyStorage ← File.GetProperties[
fpmFileHandle.lowerHandle];
propVMPageNumber: VM.PageNumber ←
VM.PageNumberForAddress[LOOPHOLE[propStorage,
LONG POINTER
]];
IF
VMAddressForPageNumber[propVMPageNumber] #
LOOPHOLE
[propStorage, LONG POINTER]
THEN
ERROR; -- File, you turkey! You promised!!
refChunk.startVMPageNumberpropVMPageNumber;
END
;
END
;
NOT new => IF otherRefChunk # NIL
THEN FpmL.PutUnmappedChunkOnLruList[otherRefChunk];
ENDCASE;
done ← TRUE;
refChunk.useCount ← refChunk.useCount + 1;
nFilePagesToRead ← 0;
[vMPageSet, wholeChunk, chunkTrueSize] ← SetUpVmPageSet[refChunk,
myPageRun.firstPage, myPageRun.count, fpmFileHandle.fileDataSize];
SELECT TRUE FROM
new => IF (refChunk.chunkType = leader) OR
((readReadAheadOrUse = use) AND (wholeChunk))
THEN
BEGIN MakeAllPagesInChunkClean[refChunk];
refChunk.state ← valid;
END
ELSE
BEGIN refChunk.state ← readInProgress;
nFilePagesToRead ← chunkTrueSize;
END;
NOT new => SELECT readReadAheadOrUse FROM
read, use => DO
IF
refChunk.state = readInProgress
THEN WAIT ReadDone
ELSE EXIT;
ENDLOOP;
ENDCASE;
ENDCASE;
END;



-- returns NIL if the requested chunk doesn't exist, otherwise, if necessary, takes it off the lru list.

GetMappedChunk: INTERNAL PROCEDURE[fpmFileHandle: FPMFileHandle, myPageRun:
MyPageRun, readReadAheadOrUse: ReadReadAheadOrUse] RETURNS [errors: ERROR,
refChunk: RefChunk] = -- values of errors are {NoSuchFile, NoSuchVolume, PageRunExtendsPastEof, VolumeWentOffline}.
BEGIN -- non system fatal errors: none.
IF fpmFileHandle.volumeState # online
THEN
RETURN[VolumeError[fpmFileHandle.volumeState], NIL];
IF
(NOT fpmFileHandle.exists) THEN RETURN[NoSuchFile, NIL];
IF myPageRun.firstPage + myPageRun.count > fpmFileHandle.fileDataSize
THEN RETURN[PageRunExtendsPastEof, NIL];
IF ((refChunk ← OST.Lookup[fpmFileHandle.chunkTable, myPageRun.chunkStartFilePage])
# NIL) AND (refChunk.useCount = 0)
THEN FpmL.GetOurChunkFromLruList[refChunk, FALSE];
errors ← Okay;
END;



-- on entry state and pages are already set to undefined; inserts into chunkTable, sets fileHandle and startFilePageNumber.

MapChunk: INTERNAL PROCEDURE[fpmFileHandle: FPMFileHandle, fileHandle:
AI.FileHandle, startChunkFilePage: AE.PageNumber, refChunk: RefChunk] =
BEGIN -- non system fatal errors: none.
OST.Insert[fpmFileHandle.chunkTable, refChunk, startChunkFilePage];
refChunk.fileHandle ← fileHandle;
refChunk.startFilePageNumber ← startChunkFilePage;
fpmFileHandle.nMappedChunks ← fpmFileHandle.nMappedChunks + 1;
END;


-- io is not in progress on entry; removes from chunkTable, clears fileHandle, handles defWritePending and nDefWriteChunks.

UnmapChunk: INTERNAL PROCEDURE[fpmFileHandle: FPMFileHandle, refChunk:
RefChunk] =
BEGIN -- non system fatal errors: none.
IF OST.Delete[fpmFileHandle.chunkTable, refChunk.startFilePageNumber] = NIL
THEN ERROR InternalFilePageMgrLogicError;
IF refChunk.defWritePending
THEN BEGIN refChunk.defWritePending ← FALSE;
fpmFileHandle.nDefWriteChunks ← fpmFileHandle.nDefWriteChunks - 1;
END;
refChunk.fileHandle ← NIL;
fpmFileHandle.nMappedChunks ← fpmFileHandle.nMappedChunks - 1;
refChunk.state ← undefined;
END
;


SetUpVmPageSet: INTERNAL PROCEDURE[refChunk: RefChunk, clientStartFilePage:
AE.PageNumber, clientFilePageCount: NAT, fileDataSize: AE.PageCount] RETURNS [vMPageSet:
Fpm.VMPageSet, wholeChunk: BOOLEAN, chunkTrueSize: AE.PageCount] =
BEGIN -- non system fatal errors: none.
chunkEndFilePagePlus1: AE.PageNumber;
clientEndFilePagePlus1: AE.PageNumber;
IF refChunk.chunkType = leader
THEN RETURN[
vMPageSet: [pages: VMAddressForPageNumber[refChunk.startVMPageNumber],
pageRun: [0, FpmPC.NLeaderFilePages],
refChunk: refChunk],
wholeChunk: TRUE,
chunkTrueSize: FpmPC.NLeaderFilePages
];
chunkEndFilePagePlus1 ← MIN[refChunk.startFilePageNumber
+ FpmPC.ChunkFilePageCount[refChunk.chunkType], fileDataSize];
clientEndFilePagePlus1 ← MIN[clientStartFilePage + clientFilePageCount,
chunkEndFilePagePlus1];
vMPageSet.pages ← VMAddressForPageNumber[GetVMIntervalFromFileInterval[
refChunk.startFilePageNumber, clientStartFilePage, 0,
refChunk.startVMPageNumber].vmPageNumber];
vMPageSet.pageRun.firstPage ← clientStartFilePage;
vMPageSet.pageRun.count ← clientEndFilePagePlus1 - clientStartFilePage;
vMPageSet.refChunk ← refChunk;
chunkTrueSize ← chunkEndFilePagePlus1 - refChunk.startFilePageNumber;
wholeChunk ← ((clientStartFilePage = refChunk.startFilePageNumber) AND
(vMPageSet.pageRun.count = chunkTrueSize));
END;



-- this procedure isn't monitored, but it calls one that is.
-- gets a chunk from the lru list.

MonitoredOtherGetNewChunk: PROCEDURE[chunkType: FpmPC.ClientChunkType]
RETURNS [otherRefChunk: RefChunk] =
BEGIN -- non system fatal errors: none.
DO
otherFileHandle: FileMap.Handle;
otherStartFilePageNumber: AE.PageNumber;
mapped: BOOLEAN;
[mapped, otherRefChunk, otherFileHandle, otherStartFilePageNumber] ←
FpmL.GetOtherChunkFromLruList[chunkType];
IF mapped -- we didn't get it yet, try to free it.
THEN BEGIN
ErrorProc: PROCEDURE RETURNS[fpmFileHandle: FPMFileHandle] =
BEGIN ERROR ConsistencyError; END; -- non system fatal errors: none.
IF (NOT MonitoredOtherFreeChunkFromFile[
FileMap.VerifyFilePageMgrHandle[otherFileHandle, ErrorProc], otherFileHandle,
otherRefChunk, otherStartFilePageNumber]) THEN LOOP;
END;
EXIT;
ENDLOOP;
END;


MonitoredOtherFreeChunkFromFile: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle,
fileHandle: AI.FileHandle, refChunk: RefChunk, startFilePageNumber: AE.PageNumber]
RETURNS [okay: BOOLEAN] =
BEGIN -- non system fatal errors: none.
hurryUp: BOOLEAN FALSE;
dirty: BOOLEAN FALSE;
DO

okay ← ((OST.Lookup[fpmFileHandle.chunkTable, startFilePageNumber] = refChunk) AND
(refChunk.useCount = 0));
IF NOT okay THEN RETURN;
IF refChunk.state = readInProgress
THEN
BEGIN WAIT ReadDone; LOOP; END;
IF refChunk.state = writeInProgress
THEN
BEGIN hurryUp TRUE; WAIT WriteDone; LOOP; END;
EXIT;
ENDLOOP
;
dirty ← ChunkIsDirty[refChunk];
FpmL.GetOurChunkFromLruList[refChunk, (hurryUp OR dirty)];
IF dirty
THEN BEGIN
refChunk.state ← writeInProgress;
CleanAndWriteChunk[fpmFileHandle, refChunk];
END;
UnmapChunk[fpmFileHandle, refChunk];
END;


-- Notifies the file page manager that the indicated pages are likely to be read soon.

ReadAheadPages: PUBLIC PROCEDURE[fileHandle: AI.FileHandle, pageRun: AE.PageRun] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, PageRunArgIllegal, VolumeWentOffline.
BasicReadAhead[fileHandle, pageRun, normal];
END;


ReadAheadLogPages: PUBLIC PROCEDURE[fileHandle: AI.FileHandle, pageRun:
AE.PageRun] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, PageRunArgIllegal, VolumeWentOffline.
BasicReadAhead[fileHandle, pageRun, log];
END;


BasicReadAhead: PROCEDURE[fileHandle: AI.FileHandle, pageRun: AE.PageRun,
chunkType: FpmPC.ClientChunkType[normal..log]] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, PageRunArgIllegal, VolumeWentOffline.
vMPageSet: Fpm.VMPageSet;
lastValidNFilePagesToRead: AE.PageCount ← 0;
lastIndex: NAT ← 0;
listOfRefChunk: LIST OF RefChunk ← NIL;
listOfVMPageSet: LIST OF Fpm.VMPageSet ← NIL;
DO

nFilePagesToRead: AE.PageCount;
[vMPageSet, nFilePagesToRead] ← BasicGetPages[fileHandle, pageRun, readAhead, chunkType
! PageRunExtendsPastEof => GOTO done];
IF pageRun.count < vMPageSet.pageRun.count THEN ERROR InternalFilePageMgrLogicError;
pageRun.firstPage ← pageRun.firstPage + vMPageSet.pageRun.count;
pageRun.count ← pageRun.count - vMPageSet.pageRun.count;
IF nFilePagesToRead # 0
THEN BEGIN lastIndex ← lastIndex + 1;
lastValidNFilePagesToReadnFilePagesToRead;
listOfRefChunk ← CONS[vMPageSet.refChunk, listOfRefChunk];
listOfVMPageSet ← CONS[vMPageSet, listOfVMPageSet];
END

ELSE ReleaseVMPageSet[vMPageSet, clean, TRUE];
IF ((pageRun.count = 0) OR (lastIndex = MaxReadAheadSets)) THEN EXIT;
REPEAT done => NULL;
ENDLOOP;
IF listOfRefChunk # NIL
THEN TRUSTED
BEGIN
Process.Detach[FORK ForkedBasicReader[fileHandle, listOfRefChunk, listOfVMPageSet,
lastValidNFilePagesToRead]];
END
;
END;


ForkedBasicReader: PROCEDURE[fileHandle: AI.FileHandle, listOfRefChunk: LIST OF RefChunk,
listOfVMPageSet: LIST OF Fpm.VMPageSet, nFilePagesToRead: AE.PageCount] =
BEGIN -- non system fatal errors: none.
fpmFileHandle: FPMFileHandle ← GetFilePageMgrHandle[fileHandle, TRUE];
DoSequentialIO[fpmFileHandle, read, listOfRefChunk, nFilePagesToRead];
FOR listOfVMPageSet ← listOfVMPageSet, listOfVMPageSet.rest
UNTIL listOfVMPageSet = NIL
DO
ReleaseVMPageSet[listOfVMPageSet.first, clean, TRUE];
ENDLOOP;
MonitoredSetListOfChunksValidAfterIO[fpmFileHandle, listOfRefChunk, readCompleted];
END;


-- Bumps the share count of the Chunk in the VMPageSet.

ShareVMPageSet: PUBLIC PROCEDURE[vMPageSet: Fpm.VMPageSet] =
BEGIN -- non system fatal errors: none.
refChunk: RefChunk ← vMPageSet.refChunk; -- keep the compiler happy.
-- chunk can't change mapping since useCount >= 1.
MonitoredShareVMPageSet[GetFilePageMgrHandle[refChunk.fileHandle, TRUE], refChunk];
END;


MonitoredShareVMPageSet: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle, refChunk:
RefChunk] =
BEGIN -- non system fatal errors: none.
IF (refChunk.useCount ← refChunk.useCount + 1) = 1 THEN ERROR ConsistencyError;
END;


ReleaseVMPageSet: PUBLIC PROCEDURE[vMPageSet: Fpm.VMPageSet, releaseState:
Fpm.ReleaseState, keep: BOOLEAN] =
BEGIN -- non system fatal errors: none.
refChunk: RefChunk ← vMPageSet.refChunk;
fpmFileHandle: FPMFileHandle ← GetFilePageMgrHandle[refChunk.fileHandle, TRUE];
writeNeeded: BOOLEANFALSE;
IF
releaseState IN Fpm.DirtyWaitReleaseState
THEN BEGIN
IF (writeNeeded ← MonitoredWaitForWriteToCompleteThenMaybeSetWIP[
fpmFileHandle, refChunk]) THEN CleanAndWriteChunk[fpmFileHandle,
refChunk];
END;
MonitoredMainReleaseVMPageSet[fpmFileHandle, refChunk, releaseState, keep, writeNeeded];
END;


MonitoredMainReleaseVMPageSet: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle,
refChunk: RefChunk, releaseState: Fpm.ReleaseState, keep: BOOLEAN, setValid: BOOLEAN] =
BEGIN -- non system fatal errors: none.
IF setValid THEN BEGIN refChunk.state ← valid; BROADCAST WriteDone; END;
IF
refChunk.chunkType = normal AND FpmL.UsingTooMuchOfCache[fpmFileHandle] AND refChunk.state # readInProgress
THEN BEGIN -- we catch after the fact.
keep ← FALSE;
IF releaseState = writeIndividualNoWait THEN releaseState ← writeBatchedNoWait;
END;
IF (refChunk.useCount ← refChunk.useCount - 1) = 0
THEN BEGIN FpmL.PutMappedChunkOnLruList[refChunk, IF ((releaseState IN
Fpm.DirtyNoWaitReleaseState) OR (keep)) THEN mru ELSE lru];
IF ((releaseState = writeBatchedNoWait) AND (NOT refChunk.defWritePending))
THEN BEGIN refChunk.defWritePending ← TRUE;
IF (fpmFileHandle.nDefWriteChunks ← fpmFileHandle.nDefWriteChunks
+ 1) >= LimitDefWriteChunks
THEN StartSomeDeferredWrites[fpmFileHandle, refChunk, keep];
END;
END;
END;



-- Spins off lists of chunks, in ascending or descending order.

ChunkAndPage: TYPE = RECORD[refChunk: RefChunk, startFilePageNumber: AE.PageNumber];

StartSomeDeferredWrites: INTERNAL PROCEDURE[fpmFileHandle: FPMFileHandle,
startChunk: RefChunk, keep: BOOLEAN] =
BEGIN -- non system fatal errors: none.
nextChunk: RefChunk ← startChunk;
searchProc: OST.LookupProc ← OST.LookupNextSmaller;
listOfChunksAndPages: LIST OF ChunkAndPage ← NIL;
AddChunk: PROCEDURE =
BEGIN -- non system fatal errors: none.
listOfChunksAndPages ← CONS[[nextChunk, nextChunk.startFilePageNumber],
listOfChunksAndPages];
END
;
ForkDemon
: PROCEDURE =
BEGIN -- non system fatal errors: none.
TRUSTED BEGIN Process.Detach[FORK DeferredWriteDemon[fpmFileHandle,
listOfChunksAndPages, keep]]; END;
listOfChunksAndPages ← NIL;
END;
DO
IF nextChunk.defWritePending
THEN BEGIN nextChunk.defWritePending ← FALSE;
fpmFileHandle.nDefWriteChunks ← fpmFileHandle.nDefWriteChunks - 1;
AddChunk[];
IF fpmFileHandle.nDefWriteChunks = 0 THEN EXIT;
END;
IF (nextChunk ← searchProc[fpmFileHandle.chunkTable,
nextChunk.startFilePageNumber]) = NIL
THEN BEGIN
IF searchProc = OST.LookupNextLarger
THEN ERROR InternalFilePageMgrLogicError;
ForkDemon[]; -- keep all the chunks going one way, for performance.
searchProc ← OST.LookupNextLarger;
IF (nextChunk ← searchProc[fpmFileHandle.chunkTable,
startChunk.startFilePageNumber]) = NIL
THEN ERROR InternalFilePageMgrLogicError;
END;
ENDLOOP;
ForkDemon[];
END;


-- forces the chunks out and then, if requested, moves them to the lru end of the lru list.

DeferredWriteDemon: PROCEDURE[fpmFileHandle: FPMFileHandle, listOfChunksAndPages:
LIST OF ChunkAndPage, keep: BOOLEAN] =
BEGIN -- non system fatal errors: none.
listOfValidAndDirty: LIST OF RefChunk;
listOfWIPAndClean: LIST OF ChunkAndPage;
[listOfValidAndDirty, listOfWIPAndClean] ←
MonitoredDefWriteSortChunks[fpmFileHandle, listOfChunksAndPages, keep];
DoSequentialIO[fpmFileHandle, write, listOfValidAndDirty, 0];
MonitoredDefWriteSetValidAndWait[fpmFileHandle, listOfValidAndDirty, listOfWIPAndClean];
END;


-- This routine is not interested in chunks that have undergone various state transitions such as being remapped. Essentially, it has three functions:

-- It wants to return a list of the valid and dirty chunks so that the caller can write them out outside of the monitor and then possibly reorder them.
-- If keep is TRUE:
-- it wants to return a list of the writeInProgress and clean chunks so that the caller can possibly reorder them after it's waited for them to complete their writes.
-- it reorders any valid and clean chunks that are still on the lru list.

MonitoredDefWriteSortChunks: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle,
listOfChunksAndPages: LIST OF ChunkAndPage, keep: BOOLEAN] RETURNS[listOfValidAndDirty:
LIST OF RefChunk, listOfWIPAndClean: LIST OF ChunkAndPage] =
BEGIN -- non system fatal errors: none.
refChunk: RefChunk;
listOfValidAndDirty ← NIL;
listOfWIPAndClean ← NIL;
FOR listOfChunksAndPageslistOfChunksAndPages, listOfChunksAndPages.rest
UNTIL
listOfChunksAndPages = NIL
DO

dirty: BOOLEAN;
refChunk ← OST.Lookup[fpmFileHandle.chunkTable,
listOfChunksAndPages.first.startFilePageNumber];
IF ((refChunk # listOfChunksAndPages.first.refChunk) OR (refChunk.defWritePending))
THEN
LOOP;
dirty ← ChunkIsDirty[refChunk];
SELECT TRUE FROM
((refChunk.state = valid) AND (dirty)) =>
BEGIN
listOfValidAndDirty ← CONS[listOfChunksAndPages.first.refChunk,
listOfValidAndDirty];
refChunk.state ← writeInProgress;
END
;
(keep) => SELECT TRUE FROM
((refChunk.state = valid) AND (refChunk.useCount = 0)) =>
FpmL.RelinkChunkAsLruOnLruList[refChunk];
((refChunk.state = writeInProgress) AND (NOT dirty)) => listOfWIPAndClean
CONS
[listOfChunksAndPages.first, listOfWIPAndClean];
ENDCASE;
ENDCASE => NULL;
ENDLOOP
;
END
;


-- This routine is not interested in chunks that have undergone various state transitions such as being remapped. Essentially, it has two functions:

-- It has a list of chunks that were written to be set to valid and possibly reordered.
-- It has a list of chunks that were wIP and clean. It wants to wait for them to become valid and clean, at which point it will possibly reorder them.

MonitoredDefWriteSetValidAndWait: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle,
listOfValidAndDirty: LIST OF RefChunk, listOfWIPAndClean: LIST OF ChunkAndPage] =
BEGIN -- non system fatal errors: none.
refChunk: RefChunk;
InternalSetListOfChunksValidAfterIOThenMaybeReorder[fpmFileHandle, listOfValidAndDirty,
writeCompleted, TRUE];
DO
IF listOfWIPAndClean = NIL THEN RETURN;
refChunk ← OST.Lookup[fpmFileHandle.chunkTable,
listOfWIPAndClean.first.startFilePageNumber];
IF ((refChunk = listOfWIPAndClean.first.refChunk) AND
(NOT refChunk.defWritePending) AND
(NOT ChunkIsDirty[refChunk]))
THEN
SELECT TRUE FROM
(refChunk.state = valid) => IF refChunk.useCount = 0
THEN FpmL.RelinkChunkAsLruOnLruList[refChunk];
(refChunk.state = writeInProgress) => BEGIN WAIT WriteDone; LOOP; END;
ENDCASE
=> NULL;
listOfWIPAndCleanlistOfWIPAndClean.rest;
ENDLOOP
;
END
;




ForceOutVMPageSet: PUBLIC PROCEDURE[vMPageSet: Fpm.VMPageSet] =
BEGIN -- non system fatal errors: none.
refChunk: RefChunk ← vMPageSet.refChunk; -- keep compiler happy.
fpmFileHandle: FPMFileHandle ← GetFilePageMgrHandle[refChunk.fileHandle, TRUE];
IF MonitoredWaitForWriteToCompleteThenMaybeSetWIP[fpmFileHandle, refChunk]
THEN BEGIN
CleanAndWriteChunk[fpmFileHandle, refChunk];
MonitoredSetChunkValidAfterIO[fpmFileHandle, refChunk, writeCompleted];
END
;
END;




ForceOutFile: PUBLIC PROCEDURE[fileHandle: AI.FileHandle] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, VolumeWentOffline.
fpmFileHandle: FPMFileHandle ← GetFilePageMgrHandle[fileHandle, FALSE];
errors: ERROR ← Okay;
listOfValidAndDirty: LIST OF RefChunk;
listOfWIPAndClean: LIST OF ChunkAndPage;
[errors, listOfValidAndDirty, listOfWIPAndClean] ←
MonitoredForceOutFileSortChunks[fpmFileHandle];
IF errors # Okay THEN ERROR errors;
DoSequentialIO[fpmFileHandle, write, listOfValidAndDirty, 0];
MonitoredForceOutFileSetValidAndWait[fpmFileHandle, listOfValidAndDirty,
listOfWIPAndClean];
END
;



MonitoredForceOutFileSortChunks: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle]
RETURNS [errors: ERROR, listOfValidAndDirty: LIST OF RefChunk, listOfWIPAndClean: LIST
OF ChunkAndPage] = -- values of errors are {NoSuchFile, NoSuchVolume, VolumeWentOffline}.
BEGIN -- non system fatal errors: none.
refChunk: RefChunk;
dirty: BOOLEAN;
currentFilePageNumber: AE.PageNumber;
listOfValidAndDirty ← NIL;
listOfWIPAndClean NIL;
IF fpmFileHandle.volumeState # online
THEN BEGIN
errors ← VolumeError[fpmFileHandle.volumeState]; RETURN; END;
IF (NOT fpmFileHandle.exists)
THEN BEGIN errors ← NoSuchFile; RETURN; END;
errors ← Okay;
refChunk ← OST.LookupSmallest[fpmFileHandle.chunkTable];
DO
IF refChunk = NIL THEN RETURN;
currentFilePageNumber ← refChunk.startFilePageNumber;
BEGIN
DO
dirtyChunkIsDirty[refChunk];
IF
((refChunk.state # writeInProgress) OR (NOT dirty)) THEN EXIT;
WAIT
WriteDone;
IF (OST.Lookup[fpmFileHandle.chunkTable, currentFilePageNumber] #
refChunk) THEN GOTO doneWithThisChunk;
ENDLOOP;
SELECT TRUE FROM

((refChunk.state = valid) AND (dirty)) => BEGIN listOfValidAndDirty ←
CONS[refChunk, listOfValidAndDirty];
refChunk.state ← writeInProgress;
END;
(refChunk.state = writeInProgress) => listOfWIPAndClean ← CONS[[refChunk,
refChunk.startFilePageNumber], listOfWIPAndClean];
ENDCASE;
EXITS doneWithThisChunk => NULL;
END
;
refChunk ← OST.LookupNextLarger[fpmFileHandle.chunkTable, currentFilePageNumber];
ENDLOOP;
END;


MonitoredForceOutFileSetValidAndWait
: ENTRY PROCEDURE[fpmFileHandle:
FPMFileHandle, listOfValidAndDirty: LIST OF RefChunk, listOfWIPAndClean: LIST OF
ChunkAndPage] =
BEGIN -- non system fatal errors: none.
refChunk: RefChunk;
InternalSetListOfChunksValidAfterIOThenMaybeReorder[fpmFileHandle, listOfValidAndDirty,
writeCompleted, FALSE];
DO
IF listOfWIPAndClean = NIL THEN RETURN;
refChunk ← OST.Lookup[fpmFileHandle.chunkTable,
listOfWIPAndClean.first.startFilePageNumber];
IF
((refChunk = listOfWIPAndClean.first.refChunk) AND
(listOfWIPAndClean.first.refChunk.state = writeInProgress))
THEN WAIT WriteDone
ELSE listOfWIPAndCleanlistOfWIPAndClean.rest;
ENDLOOP
;
END;



Sweeper: PUBLIC PROCEDURE[chunkType: FpmPC.ChunkType] =
BEGIN -- non system fatal errors: none.
needToHurry: BOOLEANFALSE;
sweepList: LIST OF FpmL.SweepItem;
DO
[needToHurry, sweepList] ← FpmL.WaitToSweep[needToHurry, chunkType];
UNTIL sweepList = NIL
DO
fpmFileHandle: FPMFileHandle ← GetFilePageMgrHandle[sweepList.first.fileHandle,
FALSE];
listOfValidAndDirty: LIST OF RefChunk;
[listOfValidAndDirty, sweepList] ← MonitoredSweeperSortChunks[fpmFileHandle,
sweepList.first.fileHandle, sweepList];
DoSequentialIO[fpmFileHandle, write, listOfValidAndDirty, 0];
MonitoredSetListOfChunksValidAfterIO[fpmFileHandle, listOfValidAndDirty,
writeCompleted];
ENDLOOP;
ENDLOOP;
END;


MonitoredSweeperSortChunks
: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle,
fileHandle: FileMap.Handle, sweepList: LIST OF FpmL.SweepItem] RETURNS[listOfValidAndDirty:
LIST OF RefChunk, newSweepList: LIST OF FpmL.SweepItem] =
BEGIN -- non system fatal errors: none.
refChunk: RefChunk;
listOfValidAndDirty ← NIL;
DO
refChunk ← OST.Lookup[fpmFileHandle.chunkTable, sweepList.first.startFilePageNumber];
IF
((refChunk # NIL) AND (refChunk.useCount = 0) AND (refChunk.state = valid) AND
(ChunkIsDirty[refChunk]))
THEN BEGIN listOfValidAndDirty ← CONS[refChunk, listOfValidAndDirty];
refChunk.state ← writeInProgress;
END
;
sweepList ← sweepList.rest;
IF ((sweepList = NIL) OR (sweepList.first.fileHandle # fileHandle)) THEN EXIT;
ENDLOOP;
newSweepList ← sweepList;
END;




ForceOutEverything: PUBLIC PROCEDURE =
BEGIN -- non system fatal errors: NoSuchVolume, VolumeWentOffline.
FOR fileHandle: AI.FileHandle ← FileMap.GetNext[NIL], FileMap.GetNext[fileHandle]
UNTIL fileHandle = NIL
DO ForceOutFile[fileHandle ! NoSuchFile => CONTINUE]; ENDLOOP;
END;




-- this is a routine used for debugging. On its return, it only guarantees that the cache was clean at some point in its processing. The caller must not be doing things like concurrent maps, etc. Call ForceOutEverything first so no chunks have writeinProgress up.

RestoreCacheToCleanState: PUBLIC PROCEDURE =
BEGIN -- non system fatal errors: none.
FOR fileHandle: AI.FileHandle ← FileMap.GetNext[NIL], FileMap.GetNext[fileHandle]
UNTIL fileHandle = NIL
DO
errors: ERROR ← MonitoredUnmapFile[GetFilePageMgrHandle[fileHandle, FALSE]];
IF ((errors # Okay) AND (errors # NoSuchFile)) THEN ERROR errors;
ENDLOOP;
IF (NOT FpmL.CheckCacheInCleanState[])
THEN ERROR InternalFilePageMgrLogicError; -- maybe not, maybe the client did a number on us.
END;


MonitoredUnmapFile: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle]
RETURNS[errors: ERROR] = -- values of errors are {NoSuchFile, NoSuchVolume, VolumeWentOffline}.
BEGIN -- non system fatal errors: none.
IF
fpmFileHandle.volumeState # online
THEN
RETURN[VolumeError[fpmFileHandle.volumeState]];
IF
(NOT fpmFileHandle.exists) THEN RETURN[NoSuchFile];
DumpInconvenientlyMappedChunks[fpmFileHandle, TRUE, 0];
RETURN[Okay];
END;


Create: PUBLIC PROCEDURE[volumeID: AE.VolumeID, initialSize: AE.PageCount, proc:
PROCEDURE[AE.FileID]] =
BEGIN -- non system fatal errors: InsufficientSpaceOnVolume, NoSuchVolume, SizeArgIllegal, VolumeTooFragmented, VolumeWentOffline plus any raised by proc.
LocalProc: PROCEDURE[fp: File.FP, propertyStorage: File.PropertyStorage] =
BEGIN proc[fp]; END;-- non system fatal errors: any raised by proc.
volume: File.Volume;
volOrVolGroupID: VolOrVolGroupID ← volumeID;
IF initialSize < 0 THEN ERROR SizeArgIllegal;
IF (volume ← File.FindVolumeFromID[volOrVolGroupID]) =
NIL THEN ERROR NoSuchVolume;
[] ← File.Create[volume: volume, size: initialSize, report: LocalProc
! File.Error => SELECT why FROM
wentOffline => GOTO wentOffline;
volumeFull => GOTO volumeFull;
fragmented => GOTO fragmented;
ENDCASE;];
EXITS
fragmented => ERROR VolumeTooFragmented;
volumeFull => ERROR InsufficientSpaceOnVolume;
wentOffline => ERROR VolumeWentOffline;
END
;


Delete: PUBLIC PROCEDURE[fileHandle: AI.FileHandle] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, VolumeWentOffline.
errors: ERROR;
IF (errors ← MonitoredDelete[GetFilePageMgrHandle[fileHandle, FALSE]]) # Okay
THEN ERROR errors;
END;


MonitoredDelete: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle] RETURNS[errors:
ERROR] = -- values of errors are {NoSuchFile, NoSuchVolume, VolumeWentOffline}.
BEGIN -- non system fatal errors: none.
IF fpmFileHandle.volumeState # online
THEN
RETURN[VolumeError[fpmFileHandle.volumeState]];
IF
(NOT fpmFileHandle.exists) THEN RETURN[NoSuchFile];
DumpInconvenientlyMappedChunks[fpmFileHandle, TRUE, 0];
File.Delete[fpmFileHandle.lowerHandle
! File.Error => SELECT why FROM
wentOffline => GOTO wentOffline;
unknownFile => GOTO horrible;
ENDCASE;];
fpmFileHandle.fileDataSize ← 0;
fpmFileHandle.exists ← FALSE;
RETURN[Okay];
EXITS
horrible => ERROR InternalFilePageMgrLogicError;
wentOffline => RETURN[VolumeWentOffline];
END;


SetSize: PUBLIC PROCEDURE[fileHandle: AI.FileHandle, size: AE.PageCount] =
BEGIN -- non system fatal errors: InsufficientSpaceOnVolume, NoSuchFile, NoSuchVolume, SizeArgIllegal, VolumeTooFragmented, VolumeWentOffline.
errors: ERROR;
IF size < 0 THEN ERROR SizeArgIllegal;
IF (errors ← MonitoredSetSize[GetFilePageMgrHandle[fileHandle, FALSE], size]) # Okay
THEN ERROR errors;
END;


MonitoredSetSize: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle, newSize:
AE.PageCount] RETURNS[errors: ERROR] = -- values of errors are {InsufficientSpaceOnVolume, NoSuchFile, NoSuchVolume, VolumeTooFragmented, VolumeWentOffline}.
BEGIN -- non system fatal errors: none.
IF fpmFileHandle.volumeState # online
THEN BEGIN
errors ← VolumeError[fpmFileHandle.volumeState]; RETURN; END;
IF
(NOT fpmFileHandle.exists) THEN RETURN[NoSuchFile];
IF fpmFileHandle.fileDataSize = newSize THEN RETURN[Okay];
IF newSize < fpmFileHandle.fileDataSize THEN -- truncating.
DumpInconvenientlyMappedChunks[fpmFileHandle, FALSE, newSize];
File.SetSize[fpmFileHandle.lowerHandle, newSize
! File.Error => SELECT why FROM
fragmented => GOTO fragmented;
unknownFile => GOTO horrible;
volumeFull => GOTO volumeFull;
wentOffline => GOTO wentOffline;
ENDCASE;];
fpmFileHandle.fileDataSize ← newSize;
RETURN[Okay];
EXITS
fragmented => RETURN[VolumeTooFragmented];
horrible => ERROR InternalFilePageMgrLogicError;
volumeFull => RETURN[InsufficientSpaceOnVolume];
wentOffline => RETURN[VolumeWentOffline];
END;


-- Called from unmap file, delete file, and setsize truncate. Wants to unmap chunks mapped to areas of the file that will no longer exist. None of those chunks should have useCounts > 0 except ones left around in ReadAhead, which we wait for. Some consistency checking is done.

DumpInconvenientlyMappedChunks: INTERNAL PROCEDURE[fpmFileHandle:
FPMFileHandle, dumpingWholeFile: BOOLEAN, size: AE.PageCount] =
BEGIN -- non system fatal errors: none.
IF fpmFileHandle.nMappedChunks # 0
THEN
BEGIN refChunk: RefChunk;
firstChunkIsPartial: BOOLEAN;
[refChunk, firstChunkIsPartial] ← FirstChunkToCheck[fpmFileHandle,
dumpingWholeFile, size];
IF refChunk # NIL
THEN CheckTheChunks[fpmFileHandle, refChunk, firstChunkIsPartial];
-- bug catchers:
[refChunk, firstChunkIsPartial] ← FirstChunkToCheck[fpmFileHandle,
dumpingWholeFile, size];
IF refChunk # NIL
THEN BEGIN
IF
((dumpingWholeFile) OR
(NOT firstChunkIsPartial) OR
(OST.LookupNextLarger[fpmFileHandle.chunkTable,
refChunk.startFilePageNumber] # NIL)) THEN ERROR;
END;
END;
END;


-- If unmap file or delete file, start at lowest chunk in file, including leader. If setsize truncate, start at chunk containing new eof.

FirstChunkToCheck: INTERNAL SAFE PROCEDURE[fpmFileHandle: FPMFileHandle,
dumpingWholeFile: BOOLEAN, newSize: AE.PageCount] RETURNS [refChunk: RefChunk,
firstChunkIsPartial: BOOLEAN] = CHECKED
BEGIN -- non system fatal errors: none.
firstPageToClip: AE.PageNumber;
fileChunkType: FpmPC.ClientChunkType;
IF dumpingWholeFile THEN RETURN [OST.LookupSmallest[fpmFileHandle.chunkTable],
FALSE];
IF OST.LookupLargest[fpmFileHandle.chunkTable] = NIL THEN RETURN[NIL, FALSE];
IF
(fileChunkType ← OST.LookupLargest[fpmFileHandle.chunkTable].chunkType)
= leader THEN RETURN[NIL, FALSE]; --setlen can't ask to trunc leader.
firstPageToClip ← ChunkStartFilePage[newSize, fileChunkType];
IF
(refChunk ← OST.Lookup[fpmFileHandle.chunkTable, firstPageToClip]) = NIL
THEN refChunk ← OST.LookupNextLarger[fpmFileHandle.chunkTable, firstPageToClip];
firstChunkIsPartial ← ((refChunk # NIL) AND (refChunk.startFilePageNumber # newSize));
END;


CheckTheChunks: INTERNAL SAFE PROCEDURE[fpmFileHandle: FPMFileHandle, refChunk:
RefChunk, firstChunkIsPartial: BOOLEAN] = CHECKED
BEGIN -- non system fatal errors: none.
startPageOfChunk: AE.PageNumber;
DO
startPageOfChunkrefChunk.startFilePageNumber;
BEGIN
DO
IF (refChunkOST.Lookup[fpmFileHandle.chunkTable, startPageOfChunk]) = NIL
THEN GOTO doneWithThisChunk;
IF refChunk.state = readInProgress THEN BEGIN WAIT ReadDone; LOOP; END;
IF
refChunk.useCount # 0 THEN ERROR;
IF refChunk.state = writeInProgress THEN BEGIN WAIT WriteDone; LOOP; END;
EXIT;
ENDLOOP
;
IF ((refChunk.state = valid) AND (ChunkIsDirty[refChunk]))
THEN BEGIN
refChunk.state ← writeInProgress;
CleanAndWriteChunk[fpmFileHandle, refChunk];
END
;
IF NOT firstChunkIsPartial
THEN BEGIN FpmL.GetOurChunkFromLruList[refChunk, FALSE];
UnmapChunk[fpmFileHandle, refChunk];
FpmL.PutUnmappedChunkOnLruList[refChunk];
END
ELSE refChunk.state ← valid;
EXITS doneWithThisChunk => NULL;
END;
firstChunkIsPartial ← FALSE;
IF
(refChunk ← OST.LookupNextLarger[fpmFileHandle.chunkTable, startPageOfChunk]) = NIL
THEN EXIT;
ENDLOOP;
END;


FileExists: PUBLIC PROCEDURE[fileHandle: AI.FileHandle] RETURNS [fileExists: BOOLEAN] =
BEGIN -- non system fatal errors: NoSuchVolume, VolumeWentOffline.
errors: ERROR;
[errors, fileExists] ← MonitoredFileExists[GetFilePageMgrHandle[fileHandle, FALSE]];
IF errors # Okay THEN ERROR errors;
END;


MonitoredFileExists: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle] RETURNS
[errors: ERROR, fileExists: BOOLEAN] = -- values of errors are {NoSuchVolume, VolumeWentOffline}.
BEGIN -- non system fatal errors: none.
IF fpmFileHandle.volumeState # online
THEN BEGIN
errors ← VolumeError[fpmFileHandle.volumeState]; RETURN; END;
RETURN
[Okay, fpmFileHandle.exists];
END;


GetSize: PUBLIC PROCEDURE[fileHandle: AI.FileHandle] RETURNS [size:
AE.PageCount] =
BEGIN -- non system fatal errors: NoSuchFile, NoSuchVolume, VolumeWentOffline.
errors: ERROR;
[errors, size] ← MonitoredGetDataSize[GetFilePageMgrHandle[fileHandle, FALSE]];
IF errors # Okay THEN ERROR errors;
END;


MonitoredGetDataSize: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle] RETURNS
[errors: ERROR, size: AE.PageCount] = -- values of errors are {NoSuchFile, NoSuchVolume, VolumeWentOffline}.
BEGIN -- non system fatal errors: none.
IF fpmFileHandle.volumeState # online
THEN BEGIN
errors ← VolumeError[fpmFileHandle.volumeState]; RETURN; END;
IF
(NOT fpmFileHandle.exists) THEN RETURN[NoSuchFile, 0];
RETURN[Okay, fpmFileHandle.fileDataSize];
END;


-- utility routines:

GetFilePageMgrHandle: PROCEDURE[fileHandle: FileMap.Handle, handleMustExist:
BOOLEAN] RETURNS [fpmFileHandle: FPMFileHandle] =
BEGIN -- non system fatal errors: none.
InitFPMFileHandle: PROCEDURE RETURNS[fpmFileHandle: FPMFileHandle] =
BEGIN -- non system fatal errors: none.
volumeID: AE.VolumeID;
volOrVolGroupID: VolOrVolGroupID;
fileID: AE.FileID;
volume: File.Volume;
volumeState: FpmPF.VolumeState ← online;
fileExists: BOOLEANTRUE;
lowerHandle: File.Handle ← NIL;
fileDataSize: AE.PageCount;
IF handleMustExist THEN ERROR ConsistencyError;
[volumeID, fileID] ← FileMap.GetVolumeIDAndFileID[fileHandle];
volOrVolGroupID ← volumeID;
IF
(volume ← File.FindVolumeFromID[volOrVolGroupID]) = NIL
THEN BEGIN
volumeState ← nonExist;
fileExists ← FALSE;
END
ELSE BEGIN
ENABLE File.Error => SELECT why FROM
unknownFile, wentOffline => BEGIN fileExists ← FALSE;
IF why = wentOffline THEN volumeState ← wentOffline;
CONTINUE;
END;
ENDCASE;
lowerHandle ← File.Open[volume: volume, fp: fileID];
fileDataSize ← File.Info[lowerHandle].size;
END;
fpmFileHandle ← NEW[FPMFileObject ← [
chunkTable: OST.CreateTable[header: ChunkAllocator[treeHeader, FALSE]],
nMappedChunks: 0,
fileDataSize: fileDataSize,
exists: fileExists,
volumeState: volumeState,
nDefWriteChunks: 0,
nLruListChunks: 0,
lowerHandle: lowerHandle]];
END;
fpmFileHandle ← FileMap.VerifyFilePageMgrHandle[fileHandle, InitFPMFileHandle];
END;



ChunkAllocator: PUBLIC PROCEDURE[chunkType: FpmPC.ChunkType, permanent: BOOLEAN]
RETURNS [refChunk: RefChunk] =
BEGIN -- non system fatal errors: running out of vm is system fatal.
refChunk ← IF permanent THEN AZ.static.NEW[Chunk] ELSE NEW[Chunk];
refChunk^ ← [chunkType: chunkType, defWritePending: FALSE, state: undefined,
useCount: 0, fileHandle: NIL, startFilePageNumber: 0, startVMPageNumber: 0,
prev: NIL, next: NIL, rbColor: , rbLLink: NIL, rbRLink: NIL];
IF ((chunkType IN FpmPC.ClientChunkType) AND (chunkType # leader))
THEN BEGIN
refChunk.startVMPageNumber ← VM.Allocate[count:
FpmPC.ChunkVMPageCount[chunkType], partition: normalVM,
subRange: [0, 0],start: 0, alignment: 0, in64K: FALSE].page;
VM.Pin[[refChunk.startVMPageNumber, FpmPC.ChunkVMPageCount[chunkType]]];
END;
END;


-- xx needs work when sizes differ.
GetVMIntervalFromFileInterval: PROCEDURE[startFilePageNumber, filePageNumber:
AE.PageNumber, filePageCount: AE.PageCount, startVMPageNumber: VM.PageNumber]
RETURNS[vmPageNumber: VM.PageNumber, vmPageCount: VM.PageCount] =
BEGIN -- non system fatal errors: none.
vmPageNumber ← startVMPageNumber + (filePageNumber - startFilePageNumber);
vmPageCount ← filePageCount;
END;


-- xx needs work when sizes differ.
GetFileIntervalFromVMInterval: PROCEDURE[startVMPageNumber, vmPageNumber:
VM.PageNumber, vmPageCount: VM.PageCount, startFilePageNumber: AE.PageNumber]
RETURNS[filePageNumber: AE.PageNumber, filePageCount: AE.PageCount] =
BEGIN -- non system fatal errors: none.
filePageNumber ← startFilePageNumber + (vmPageNumber - startVMPageNumber);
filePageCount ← vmPageCount;
END;


ChunkStartFilePage: PROCEDURE[clientFilePage: AE.PageNumber, chunkType:
FpmPC.ClientChunkType] RETURNS[chunkStartFilePage: AE.PageNumber] = INLINE
BEGIN -- non system fatal errors: none.
RETURN[IF chunkType = leader
THEN FpmPF.LeaderFilePageNumber
ELSE (clientFilePage - Basics.LongDivMod[clientFilePage,
FpmPC.ChunkFilePageCount[chunkType]].remainder)];
END;


VMAddressForPageNumber: PROCEDURE[page: VM.PageNumber] RETURNS [address: LONG
POINTER] = -- non system fatal errors: none.
TRUSTED BEGIN RETURN[VM.AddressForPageNumber[page]];
END;

MakePagesClean: PROCEDURE[interval: VM.Interval] = INLINE
BEGIN -- non system fatal errors: none.
VM.MakeUnchanged[interval];
END;


MakeAllPagesInChunkClean
: PROCEDURE[refChunk: RefChunk] = INLINE
BEGIN -- non system fatal errors: none.
VM.MakeUnchanged[[page: refChunk.startVMPageNumber, count:
FpmPC.ChunkVMPageCount[refChunk.chunkType]]];
END;


ChunkIsDirty: PUBLIC PROCEDURE[refChunk: RefChunk] RETURNS [dirty: BOOLEAN] =
BEGIN -- non system fatal errors: none.
FOR vMPage: VM.PageNumber IN
[refChunk.startVMPageNumber..refChunk.startVMPageNumber +
FpmPC.ChunkVMPageCount[refChunk.chunkType])
DO IF VM.State[vMPage].dataState = changed THEN RETURN[TRUE];
ENDLOOP;
RETURN[FALSE];
END;


-- chunks are presented in ascending or descending order. Sets pages clean before the writes and after the reads.

DoSequentialIO: PROCEDURE[fpmFileHandle: FPMFileHandle, io: FpmIO.IOType,
listOfRefChunk: LIST OF RefChunk, nFilePagesHighestChunkForRead: AE.PageCount] =
BEGIN -- non system fatal errors: none.
controllingProcess: PROCESSLOOPHOLE[Process.GetCurrent[]];
iORequest: FpmIO.IORequest;
workToDo: BOOLEAN;
listOfIOReq: LIST OF FpmIO.IORequest ← NIL;
error: File.RC;
errorIORequest: FpmIO.IORequest;
first: BOOLEANTRUE;
IF
listOfRefChunk = NIL THEN RETURN;
IF
((listOfRefChunk.rest # NIL) AND
(listOfRefChunk.first.startFilePageNumber < listOfRefChunk.rest.first.startFilePageNumber))
THEN BEGIN
tempList: LIST OF RefChunk ← NIL;
FOR
listOfRefChunk ← listOfRefChunk, listOfRefChunk.rest
UNTIL listOfRefChunk = NIL
DO
tempList ← CONS[listOfRefChunk.first, tempList];
ENDLOOP;
listOfRefChunktempList;
END;
FOR listOfRefChunk ← listOfRefChunk, listOfRefChunk.rest
UNTIL listOfRefChunk = NIL
DO
SELECT io FROM
read => BEGIN
listOfIOReq ← CONS[[filePageNumber: [listOfRefChunk.first.startFilePageNumber],
nPages: (IF first THEN nFilePagesHighestChunkForRead
ELSE FpmPC.ChunkFilePageCount[listOfRefChunk.first.chunkType]), vM:
VMAddressForPageNumber[listOfRefChunk.first.startVMPageNumber]],
listOfIOReq];
first ← FALSE;
END
;
write => BEGIN
AddToCmdList: PROCEDURE[filePageNumber: AE.PageNumber, filePageCount:
AE.PageCount, vmPageNumber: VM.PageNumber] RETURNS[stop: BOOLEAN] =
BEGIN-- non system fatal errors: none.
listOfIOReq ← CONS[[filePageNumber: [filePageNumber], nPages:
filePageCount, vM: VMAddressForPageNumber[vmPageNumber]],
listOfIOReq];
stop ← FALSE;
END
;
GetAndCleanNextSeqForWrite[listOfRefChunk.first, descending, AddToCmdList];
END
;
ENDCASE;
ENDLOOP;
iORequest ← FpmIO.RegisterRequest[controllingProcess: controllingProcess, io: io, file:
fpmFileHandle.lowerHandle, list: listOfIOReq];
DO
FpmIO.DoIO[io, fpmFileHandle.lowerHandle, iORequest !
File.Error => BEGIN error ← why;
errorIORequest ← iORequest;
FpmIO.LogError[controllingProcess, controller, why, iORequest];
GOTO errorSeen;
END];
[error, errorIORequest, workToDo, iORequest] ← FpmIO.GetNext[controllingProcess:
controllingProcess, who: controller];
IF error # ok THEN GOTO errorSeen;
IF workToDo = FALSE THEN EXIT;
REPEAT errorSeen => ERROR; -- someday we'll do the right thing.
ENDLOOP;
IF io = read
THEN FOR tempList: LIST OF RefChunk ← listOfRefChunk, listOfRefChunk.rest
UNTIL listOfRefChunk = NIL
DO MakeAllPagesInChunkClean[tempList.first];
ENDLOOP;
END;


-- sets the dirty pages to clean and does the write.

CleanAndWriteChunk: PROCEDURE[fpmFileHandle: FPMFileHandle, refChunk: RefChunk] =
BEGIN -- non system fatal errors: none.
WriterProc: PROCEDURE[filePageNumber: AE.PageNumber, filePageCount: AE.PageCount,
vmPageNumber: VM.PageNumber] RETURNS[stop: BOOLEAN] =
BEGIN -- non system fatal errors: none.
FpmIO.DoIO[write, fpmFileHandle.lowerHandle, [[filePageNumber], filePageCount,
VMAddressForPageNumber[vmPageNumber]]];
stop ← FALSE;
END
;
GetAndCleanNextSeqForWrite[refChunk, ascending, WriterProc];
END;


GetAndCleanNextSeqForWrite: PROCEDURE[refChunk: RefChunk, whichWay: {ascending,
descending}, proc: PROCEDURE[filePageNumber: AE.PageNumber, filePageCount: AE.PageCount,
vmPageNumber: VM.PageNumber] RETURNS[stop: BOOLEAN]] =
BEGIN -- non system fatal errors: any non system fatal errors returned by "proc".
filePageNumber: AE.PageNumber;
filePageCount: AE.PageCount;
vmPageNumber: VM.PageNumber;
vmPageCount: VM.PageCount;
startVMPage: VM.PageNumberrefChunk.startVMPageNumber;
endVMPage: VM.PageNumber refChunk.startVMPageNumber +
FpmPC.ChunkVMPageCount[refChunk.chunkType] - 1;
IF refChunk.chunkType = leader
THEN BEGIN
MakeAllPagesInChunkClean[refChunk];
[] ← proc[FpmPF.LeaderFilePageNumber, FpmPC.NLeaderFilePages,
refChunk.startVMPageNumber];
RETURN;
END;
DO

BEGIN
IF whichWay = ascending
THEN BEGIN
IF
VM.State[startVMPage].dataState = changed
THEN BEGIN
vmPageNumberstartVMPage;
DO startVMPage ← startVMPage + 1;
IF ((startVMPage > endVMPage) OR
(VM.State[startVMPage].dataState # changed)) THEN EXIT;
ENDLOOP;
vmPageCount ← startVMPage - vmPageNumber;
GOTO doInterval;
END
;
END
ELSE BEGIN
IF VM.State[endVMPage].dataState = changed
THEN BEGIN
saveEndVMPage: VM.PageNumber endVMPage;
DO endVMPage ← endVMPage - 1;
IF ((startVMPage > endVMPage) OR
(VM.State[endVMPage].dataState # changed)) THEN EXIT;
ENDLOOP;
vmPageNumberendVMPage + 1;
vmPageCount ← saveEndVMPage - endVMPage;
GOTO doInterval;
END
;
END;
EXITS

doInterval => BEGIN
[filePageNumber, filePageCount] ←
GetFileIntervalFromVMInterval[refChunk.startVMPageNumber,
vmPageNumber, vmPageCount, refChunk.startFilePageNumber];
MakePagesClean[[vmPageNumber, vmPageCount]];
IF
proc[filePageNumber, filePageCount, vmPageNumber] THEN RETURN;
END
;
END
;
IF whichWay = ascending
THEN startVMPage ← startVMPage + 1
ELSE
endVMPage ← endVMPage - 1;
IF
startVMPage > endVMPage THEN EXIT;
ENDLOOP;
END;


MonitoredWaitForWriteToCompleteThenMaybeSetWIP: ENTRY PROCEDURE[fpmFileHandle:
FPMFileHandle, refChunk: RefChunk] RETURNS[writeNeeded: BOOLEAN]=
BEGIN -- non system fatal errors: none.
DO IF refChunk.state # writeInProgress THEN EXIT;
WAIT WriteDone;
ENDLOOP
;
IF (writeNeeded ChunkIsDirty[refChunk]) THEN refChunk.state ← writeInProgress;
END;


MonitoredSetChunkValidAfterIO: ENTRY PROCEDURE[fpmFileHandle: FPMFileHandle,
refChunk: RefChunk, what: {readCompleted, writeCompleted}] =
BEGIN -- non system fatal errors: none.
refChunk.state ← valid;
IF
what = readCompleted -- weird form to keep compiler happy.
THEN BROADCAST ReadDone
ELSE BROADCAST WriteDone;
END;


MonitoredSetListOfChunksValidAfterIO: ENTRY PROCEDURE[fpmFileHandle:
FPMFileHandle, listOfRefChunk: LIST OF RefChunk, what: {readCompleted, writeCompleted}] =
BEGIN -- non system fatal errors: none.
FOR list: LIST OF RefChunklistOfRefChunk, list.rest
UNTIL list = NIL
DO
list.first.state ← valid;
ENDLOOP
;
IF what = readCompleted -- weird form to keep compiler happy.
THEN BROADCAST ReadDone
ELSE BROADCAST WriteDone;
END;


InternalSetListOfChunksValidAfterIOThenMaybeReorder: INTERNAL
PROCEDURE
[fpmFileHandle: FPMFileHandle, listOfRefChunk: LIST OF RefChunk, what:
{readCompleted, writeCompleted}, reorder: BOOLEAN] =
BEGIN -- non system fatal errors: none.
FOR list: LIST OF RefChunklistOfRefChunk, list.rest
UNTIL list = NIL
DO
list.first.state ← valid;
IF
((reorder) AND (NOT ChunkIsDirty[list.first]) AND (list.first.useCount = 0))
THEN FpmL.RelinkChunkAsLruOnLruList[list.first];
ENDLOOP
;
IF what = readCompleted -- weird form to keep compiler happy.
THEN BROADCAST ReadDone
ELSE BROADCAST WriteDone;
END;





InitializeFilePageMgr: PUBLIC PROCEDURE[nNormalChunksInCache: NAT,
nLogChunksInCache: NAT, nLeaderChunksInCache: NAT] =
BEGIN -- non system fatal errors: none.
IF moduleInitialized THEN ERROR;
OST.Initialize[ChunkAllocator[ostGeneral, TRUE], ChunkAllocator[ostGeneral, TRUE]];
FpmL.InitializeLruLists[[normal: nNormalChunksInCache, leader: nLeaderChunksInCache,
log: nLogChunksInCache]]; -- operates inside the lru list monitor.
moduleInitialized ← TRUE;
END;



-- fatal errors:

ConsistencyError: -- CALLING or FPM -- ERROR = CODE; -- caller has asserted that a FPMFileHandle exists or a vmpageset has its usecount positive, but it doesn't.



-- main line code:



MaxReadAheadSets: NAT = 10; -- to avoid swamping the cache, we won't read ahead more
-- sets than these on one request.
-- The remainder of the request is ignored.
LimitDefWriteChunks: NAT = 10; -- once this many chunks from a given file are
-- waiting for a demon, we incarnate one or more.


moduleInitialized: BOOLEANFALSE;



END.

Edit Log

Initial: Kolling: 23-Feb-82 14:49:56: main impl module for FilePageManager.