// BFSClose.bcpl -- procedures to clean up and close a disk
// Copyright Xerox Corporation 1979
// Last modified November 17, 1978  3:09 PM by Boggs

get "Altofilesys.d"
get "Disks.d"
get "BFS.d"

external
[
// outgoing procedures
BFSClose; BFSWriteDiskDescriptor

// incoming procedures
CloseDD; DestroyDDMgr; ReadDDPage
LockDD; UnlockDD; FlushDD
Free; MoveBlock
]


//----------------------------------------------------------------------------
let BFSClose(disk, dontFree; numargs na) = valof
//----------------------------------------------------------------------------
[
if disk>>BFSDSK.initmode ne 0 then
   [
   BFSWriteDiskDescriptor(disk)
   CloseDD(disk>>BFSDSK.ddMgr, disk)
   unless na gr 1 & dontFree do DestroyDDMgr(disk>>BFSDSK.ddMgr)
   ]
Free(disk>>BFSDSK.zone, disk)
resultis 0
]

//----------------------------------------------------------------------------
and BFSWriteDiskDescriptor(disk) be
//----------------------------------------------------------------------------
[
if disk>>BFSDSK.initmode ne 0 then
   [
   let ddMgr = disk>>BFSDSK.ddMgr
   LockDD(ddMgr, disk)
   let buf = ReadDDPage(ddMgr, disk, 1)  // Read page containing KDH
   MoveBlock(buf, disk>>DSK.diskKd, lKDHeader)
   UnlockDD(ddMgr, disk, true)
   FlushDD(ddMgr, disk)
   ]
]