When a filesystem is mounted it performs a startup scan to determine
the format of the disk and fix up any problems caused by any
unexpected failures. The scan goes through the following steps:
Scan the freelist looking for the head and tail offsets. Each block
seen in the freelist is also recorded as having been seen and as being
free.
Scan the directory. For each entry, check that its checksum is
correct. If not, mark the entry empty and correct the checksum. For
each file, if it is in CREATING state, complete the operation by
ensuring that each block in the BAT is not also in the freelist and
changing its state to CREATED. If it is in DELETING state, complete
the operation by returning all the blocks in its BAT to the freelist
and deleting the directory entry.
Scan the BATs of all files, recording that they have been seen and
checking that they are not also in the freelist. Any block that is
both in the freelist and a BAT is removed from the freelist.
If any blocks have not yet been seen, then these orphaned blocks are
inserted into the freelist.
If any of the previous steps have updated the freelist, then the
on-disk data structure is rebuilt. This has the side effect of sorting
the freelist into block order, improving performance in future.
If the scan finds that the disk is corrupt or unformatted, or the
filesystem has been mounted using the "mmfs.format" filesystem, then
the disk is reformatted. Formatting consists of zeroing the directory
and all the BATs, and building the freelist with all the blocks in the
data area. Finally a volume label is written to the first entry in the
directory.