## Physical File Organization

OS-9's file system implements a universal logical organization for all I/O devices that effectively eliminates most hardware-related considerations for most applications. This section gives basic information about the physical file structure used by OS-9. For more information, see the OS-9 System Programmer's Manual.

Each OS-9 file is comprised of one or more sectors which are the physical storage units of the disk systems. Each sector holds exactly 256 data bytes, and disk is numbered sequentially starting with sector zero, track zero. This number is called a “logical sector number”, or LSN. The mapping of logical sector numbers to physical track/sector numbers is done by the disk driver module.

Sectors are the smallest allocatable physical unit on a disk system, however, to increase efficiency on some larger-capacity disk. systems, OS-9 uses uniform-sized groups of sectors, called clusters, as the smallest allocatable unit. Cluster sizes are always an integral power of two (2, 4, 8, etc.). One sector of each disk is used as a bitmap (usually LSN 1), in which each data bit corresponds to one cluster on the disk. The bits are set and cleared to indicate which clusters are in use (or defective), and which are free for allocation to files.

A 5.25 inch floppy disk system uses the following format:

- double density recording on one side

- 40 tracks per disk

- 18 sectors per track

- one sector per cluster

Each file has a directory entry (see the section called “Directory Files”) which includes the file name and the logical sector number of the file's “file descriptor sector”, which contains a complete description of the file including:

- attributes

- owner

- date and time created

- size

- segment list (description of data sector blocks)

Unless the file size is zero, the file will have one or more sectors/clusters used to store data. The data sectors are grouped into one or more contiguous blocks called “segments”. The segment list portion of the header sector consists one record for each segment. The record contains the starting logical sector number and size (number of sectors/clusters) of each segment. Therefore, the segment list has all the information OS-9 needs to determine the sector number of any of the file's sectors (and therfore, data bytes).

If the various segments that comprise a file are physically scattered on the disk, the disk head must travel more often and greater distances, especially when reading records from random access files (see the section called “Random Access Data Files”), thus degrading file access time. Therefore, OS-9's disk allocation strategy is to minimize the number of segments if possible. It can do so more effectively if programs cause larger amounts of storage to be allocated at a time. If a file is to have a fixed size which is known beforehand, a program can cause all storage to be allocated by performing a “seek” to the last byte to be used by the file, then writing dummy data to the last byte. This causes all storage to be allocated in one operation which allows OS-9 to find the most optimum segment sizes and locations.

If files are expanded at many different times, or the disk storage becomes badly fragmented, access time increases. When this happens, files should be copied (using dsave or copy but not backup) to a freshly formatted disk.