// TfsClose.bcpl -- procedures to clean up and close a disk
// Copyright Xerox Corporation 1979

//	Last modified October 24, 1978  11:36 AM

get "Altofilesys.d"
get "Disks.d"
get "Tfs.d"

external
[
//outgoing procedures
TFSClose
TFSWriteDiskDescriptor

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


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

//----------------------------------------------------------------------------
and TFSWriteDiskDescriptor(disk) be
//----------------------------------------------------------------------------
[
if disk>>TFSDSK.initmode ne 0 then
   [
   let ddMgr = disk>>TFSDSK.ddMgr
   LockDD(ddMgr, disk)
   let buf = ReadDDPage(ddMgr, disk, 1)  // Read page containing TFSKD
   MoveBlock(buf, lv disk>>TFSDSK.kd, lTFSKDHeader)
   UnlockDD(ddMgr, disk, true)
   FlushDD(ddMgr, disk)
   ]
]