File System Implementation
Implementing File Systems on Mass Storage
A file system is the set of data structures and rules that an operating system uses to store, organize, locate, and retrieve files on a storage device. It provides a bridge between the way users think about data—files with names, sizes, and creation dates—and the way a device actually stores data—as numbered sectors and blocks.
The central challenge is that users want a simple, logical view of storage, while each physical device may work differently. A hard disk, USB drive, DVD, network drive, and memory card can all have different hardware and access methods. The file system hides these differences so that applications can work with files in a consistent way.
A file system translates logical file operations into physical storage operations.
The Layers Between a File and the Device
A file request passes through several layers before data is physically read from or written to storage.
At the highest level, an application might request that a file named report.txt be opened. The operating system’s file-system layer determines which file is meant, locates its metadata, and determines where its contents are stored. Lower layers translate that logical location into physical device operations. Finally, a device driver communicates with the hardware.
A simplified path looks like this:
-
Application or user request
An application requests an operation such as opening, reading, writing, or closing a file. -
Logical file system
This layer works with file names, directories, permissions, sizes, creation dates, and other metadata. -
File-control structures
The system uses structures such as a file control block to record information about an individual file, including where its data is stored. -
Basic file system
This layer works with logical blocks. It determines which blocks must be read or written. -
Device driver
The driver translates the request into commands appropriate for a particular device. -
Physical device
The storage hardware performs the actual read or write operation.
This layered design is important because the upper layers do not need to understand the physical details of every device. A USB drive may require a different driver from a hard disk, but the logical file-system interface can remain the same. The same general file operations can therefore work with a hard disk, USB drive, DVD-ROM, or network storage.
Sectors and Blocks
Storage devices normally divide their space into small physical units called sectors. The file system generally groups one or more sectors into a logical block.
For example, if a logical block contains four sectors, obtaining one logical block may require four lower-level device operations. The exact sizes are selected so that the units fit together efficiently. Storage sizes are commonly chosen as powers of two rather than arbitrary values, because powers of two can be represented and divided efficiently by computer hardware.
The logical block is useful because the file system does not need to manage every individual byte separately. Instead, it allocates and tracks storage in blocks. This reduces management overhead, although it can cause some unused space inside the final block of a file.
Device Controllers and Buffers
A storage device commonly includes a controller and a small memory area called a buffer. The controller carries out the physical operation, while the buffer temporarily holds data being transferred.
Without a buffer, the entire computer might need to coordinate its timing with the storage device. The processor and other components would have to wait until the disk head moved, the correct sector was found, and the data became available. A buffer allows the device to read data first and hold it until the operating system is ready to receive it.
This separation improves efficiency. The device can perform its slower mechanical or electronic work, while the rest of the system continues other operations.
Different File Systems for Different Purposes
There is no single file system that is ideal for every situation. Different file systems may be designed for:
- A particular kind of device, such as optical media or network storage.
- Fast sequential access.
- Efficient handling of many small files.
- Handling very large files.
- Compatibility with multiple operating systems.
- Greater reliability or improved recovery after failure.
- Special kinds of metadata or organizational features.
For example, one file system may support labels or color information associated with files, while another may not provide space for that metadata. A file name is broadly universal, but less common features may depend on the particular file system.
Persistent Storage and In-Memory Structures
The file system must store its important information on the storage device because the computer will eventually be turned off. When the system starts again, it must be able to reconstruct the organization of the device.
However, accessing this information directly from the device every time would be slow. Consequently, operating systems often copy frequently used file-system structures into memory.
Important Information Stored on the Device
A storage device may contain several important structures.
A boot control block contains information needed to start an operating system from that device. It allows the system to locate and load the appropriate startup code.
A volume control block contains information about a volume or partition, such as:
- The number of blocks in the volume.
- The beginning and end of the volume.
- Which blocks are currently used.
- Which blocks are free.
- Other information needed to manage the volume.
A directory structure records the organization of files and directories. It allows the system to associate a human-readable file name with the file’s metadata and storage location.
A file control block stores information about an individual file. Depending on the file system, it may include the file’s size, timestamps, permissions, type, and pointers to the blocks containing its data.
Why Structures Are Copied into Memory
Suppose the system receives a request to open a file. If the directory exists only on the disk, the system must first access the directory to discover where the file is located. It then needs another device access to retrieve the file’s contents.
Keeping the directory in memory avoids the first access in many cases. The system can look up the file name immediately, find the appropriate file-control information, and go directly to the data blocks.
The file control block can also be copied into memory. Once the system knows the file’s location and other properties, subsequent operations can use the in-memory copy instead of repeatedly retrieving the same metadata from the device.
This improves performance, but it consumes memory. A machine with many storage devices, large directory structures, or many active files may need considerable memory for cached file-system information.
Partitions, Volumes, and Mounting
A physical storage device can be divided into several logical sections called partitions. Each partition can be managed as a separate volume and may even use a different file system.
Partitioning is useful for several reasons. The operating system might be placed in one partition and user data in another. Different partitions can also use different file systems chosen for different workloads.
For instance, an operating-system partition may contain files that change relatively little. A simple and fast allocation strategy may work well there. A user-data partition may contain files that frequently grow and shrink, requiring a more flexible strategy.
Mounting a File System
Before the operating system can use a partition or storage device, it must connect that device to its overall directory structure. This process is called mounting.
The operating system records where the mounted file system appears in the larger directory hierarchy. The exact method depends on the operating system. Some systems assign a drive letter, while others attach the device to a directory path.
For example, a Unix-like system may mount different storage areas at paths such as:
/for the root file system./binfor binary program files./usrfor user-related programs or data.
Other operating systems may use drive letters or different naming conventions. The underlying idea remains the same: mounting makes a separate storage area accessible through the operating system’s unified file-space.
Safe Removal and Consistency
File-system structures are frequently changed in memory before those changes are written back to the device. This can happen when a file is created, extended, renamed, or deleted.
If a USB drive is removed without first informing the operating system, some changes may still exist only in memory. The directory might say that a file exists while the file’s data blocks have not yet been recorded correctly. Alternatively, the data may have been written but the pointers connecting the directory, file-control block, and data may not have been updated.
The result can be lost data or an inconsistent file system. Safe removal gives the operating system an opportunity to write pending changes and finish outstanding operations.
File-System Checking
When a computer starts, it may check whether its file systems are consistent. This process can verify relationships such as:
- Whether directory entries point to valid file-control information.
- Whether file-control structures point to valid data blocks.
- Whether blocks are incorrectly marked as both free and in use.
- Whether the recorded organization agrees with the actual contents of the device.
If the check finds a problem, the operating system may perform a more detailed repair or consistency check. This is particularly important after an unexpected shutdown or unsafe device removal.
Think About It
Why can a storage device contain the correct file data but still have a damaged file system? The answer lies in the connections among directories, file-control structures, allocation information, and data blocks. Losing or failing to update any of those connections can make otherwise present data difficult to locate.
Organizing Files in Directories
A directory must associate file names with information about those files. The simplest implementation is a linear list.
In a linear-list directory, entries are stored one after another. To find a particular file, the operating system begins at the first entry and compares names until it finds a match.
This method is easy to implement, but searching becomes slower as the directory grows. If the desired entry is equally likely to appear anywhere in the list, the average search examines approximately half of the entries. In asymptotic notation, the search takes O(n) time for a directory containing n entries.
Hash-Table Directories
A hash table uses a function to convert a file name into an index or number. The system then uses that index to go directly to a likely location for the directory entry instead of examining every name in sequence.
A hash function has the important property that the same input produces the same result. Thus, the same file name can consistently lead to the same table location.
Different names can sometimes produce the same result. This event is called a collision. A hash-table implementation must have a way to handle collisions, such as storing multiple entries at the same location or finding another available location.
Hash tables can make directory lookup much faster than a linear search, especially when directories contain many entries. Their effectiveness depends on the quality of the hash function and on how collisions are handled.
File Allocation Methods
After organizing file names, the file system must decide how to store each file’s contents in blocks. The main allocation strategies differ in how they balance speed, flexibility, fragmentation, and reliability.
Contiguous Allocation
With contiguous allocation, all blocks belonging to a file are placed next to one another on the storage device.
A directory entry can record:
- The starting block of the file.
- The number of blocks occupied by the file.
To locate a particular part of the file, the system begins at the starting block and moves forward by the required offset. If each block contains 512 bytes, the system can calculate the physical location of data based on the starting position, the requested offset, and the block size.
For example, a file might begin at block 0 and occupy two blocks. Another might begin at block 14 and occupy three blocks. The file system can find every block in either file simply by counting forward from its starting position.
Advantages of Contiguous Allocation
Contiguous allocation is fast because the blocks can be read sequentially. Once the storage device reaches the first block, it can often continue reading the following blocks with little additional movement.
It is also simple to implement. The file system needs only the starting location and length.
Problems with Contiguous Allocation
The main difficulty is file growth. If a file occupies the blocks immediately before another file, it cannot grow unless there is unused space directly after it. The system may need to move the file to a larger contiguous region.
This can create external fragmentation. Free space may exist in many separate gaps, but none of the gaps may be large enough for a new file. The total free space can therefore be substantial while still being unusable for a particular allocation request.
A system can perform compaction, which moves allocated regions together to create larger free areas. Compaction can reduce fragmentation, but moving large amounts of data takes time and temporarily increases system activity.
When Contiguous Allocation Works Well
Contiguous allocation is particularly suitable for files whose sizes do not change often. An operating-system partition may be a good candidate if its files are mostly stable. A separate data partition can then use a more flexible allocation method for files that frequently grow and shrink.
Extent-Based Allocation
An extent is a contiguous sequence of blocks allocated as a unit.
Instead of allocating one large contiguous region when a file is created, the file system initially gives it an extent of a chosen size. The file can use the blocks in that extent. If the file grows beyond the extent, the system allocates another extent elsewhere.
A file may therefore consist of several contiguous regions:
- The first extent contains one sequence of blocks.
- The second extent contains another sequence.
- Additional extents can be added as the file grows.
The file system must record the relationship among these extents, perhaps by storing a list of them or linking one extent to the next.
Benefits and Costs of Extents
Extents preserve much of the performance advantage of contiguous allocation because each extent can be read sequentially. They also allow files to grow without immediately moving the entire file.
They are especially useful in large systems where administrators may know approximately how much space different data sets require. In systems where allocated space has a cost, requesting an extent encourages careful estimation. Allocating much more space than necessary wastes resources.
The disadvantage is that a file composed of many extents may require the storage device to move between separate regions. It is therefore less efficient than one completely contiguous allocation, although usually more flexible.
Linked Allocation
With linked allocation, a file’s blocks do not need to be adjacent. Each block contains, or is associated with, a pointer to the next block belonging to the same file.
A directory entry can store the first block. The system reads that block, follows its pointer to the next block, and continues until it reaches the end of the file.
For example, a file might begin at block 9. Block 9 could point to block 16, block 16 could point to block 1, block 1 could point to block 10, and block 10 could point to block 25. Block 25 would indicate that the file ends there.
Advantages of Linked Allocation
Linked allocation makes file growth easy. A new block can be placed wherever free space is available and linked to the existing file. The file does not need to be moved to create a larger contiguous region.
It also reduces external fragmentation because individual free blocks can be reused. A deleted file leaves separate free blocks that can be allocated to other files.
Disadvantages of Linked Allocation
The major cost is performance. To reach a later block, the system may need to read every earlier pointer. If the next block is physically far away, the storage device may have to move repeatedly.
This produces file fragmentation. A fragmented file is spread across many locations, possibly requiring frequent device movement during access.
Linked allocation can also create a reliability problem. If a pointer is damaged, the system may not know where the next block is. The remainder of the file may become inaccessible even though the data still exists on the device.
File Allocation Tables
A common variation is the file allocation table, or FAT. Instead of storing the next-block pointer inside each data block, the file system keeps the links in a separate table.
The directory entry stores the first block of a file. The allocation table then records the next block in the chain. The system follows the table entries until it reaches an end marker.
A table-based approach makes the allocation information more centralized. The operating system may also load much or all of the table into memory, reducing the need to repeatedly read pointer information from the device.
However, if the table is incomplete or damaged, the system may not be able to determine whether the entire file has been recovered. Some designs store additional information, such as the final block, to provide an extra consistency check.
Indexed Allocation
Linked allocation requires the system to follow pointers one at a time. Indexed allocation improves access by placing the block addresses for a file into an index block.
The directory or file-control structure points to the index block. The index block lists the actual data blocks belonging to the file. If a file occupies blocks 9, 16, 1, and 25, the index block can list all four locations directly.
The system can then obtain the block addresses together and request the data blocks without reading each data block merely to discover the location of the next one.
Advantages and Costs
Indexed allocation can greatly improve access speed, particularly for files whose blocks are scattered throughout the device.
The cost is the extra index block. The index itself occupies storage, and the system must access it before retrieving the file’s data. If the index is already in memory, this cost becomes much smaller.
An index block also has limited capacity. If a file requires more block addresses than one index block can contain, the file system needs additional levels of indexing.
Multilevel Indexing
A file system can extend indexed allocation by allowing index blocks to point to other index blocks.
Direct Blocks
A direct block pointer points straight to a data block. This is efficient for small files because no additional index lookup is needed.
Single Indirection
A single indirect pointer points to an index block, and that index block points to data blocks.
The path is:
file control structure→index block→data blockDouble Indirection
A double indirect pointer points to an index block that points to additional index blocks. Those lower-level index blocks then point to the data blocks.
file control structure→index block→index block→data blockTriple Indirection
A triple indirect pointer adds one more index level. This allows the file system to address very large files.
A file system can combine these techniques. Small files may use direct pointers, medium-sized files may use single indirection, and very large files may use double or triple indirection. This combination avoids wasting large index structures on small files while still supporting very large files.
The best arrangement depends on the expected file sizes, access patterns, available memory, and performance requirements. More levels allow larger files, but they also introduce more lookups. Since a disk access is far slower than an additional processor instruction, systems often try to keep index information in memory or use direct pointers where possible.
Managing Free Space
The file system must track not only where files are stored, but also where new data can be placed. This is the problem of free-space management.
The system must answer questions such as:
- Which blocks are unused?
- Is there a sufficiently large region for a new file?
- Where should an expanding file receive its next block?
- Which blocks become free when a file is deleted?
Bitmap or Bit Vector
A bitmap assigns one bit to every block. The bit indicates whether the corresponding block is available or occupied.
For example:
- A bit value of 1 may represent a free block.
- A bit value of 0 may represent a block that is already in use.
The exact meaning of the bit values can be reversed; what matters is that the convention is consistent.
A bitmap makes it possible to inspect many blocks compactly. Some processors provide instructions that can locate the first set bit in a word. If the first free bit appears at a particular offset, the operating system can quickly identify a free block without performing another storage-device access.
Bitmap Memory Cost
The disadvantage is that the bitmap itself requires memory or storage. A very large device may need a large bitmap.
For example, a one-terabyte device may require approximately 64 megabytes for its bitmap under the block-size assumptions in the material. This can be a significant amount of working memory, especially when several storage devices are managed at once.
Linked List of Free Blocks
Instead of recording one bit per block, the system can link free blocks together. The free-space structure stores the first free block. That block identifies the next free block, and the chain continues through all available blocks.
When the system allocates a block, it removes the first block from the free list and updates the pointer to the next free block.
This approach can use less memory than a complete bitmap, but it may require repeated accesses. To find several free blocks, the system may need to follow several links.
Grouping Free Blocks
A more efficient method records groups of free blocks. Each entry can specify:
- The beginning of a group of free blocks.
- The number of consecutive free blocks in that group.
- The location of the next group.
For example, one entry might say that four consecutive blocks are free beginning at block 2, and that the next group begins at block 8. Another entry might record six free blocks beginning at block 8.
This representation captures not only where free space exists, but also how much of it is contiguous. That information helps the file system choose a suitable region for a new file and can reduce fragmentation.
Free-Space Runs and Extents
The same grouping idea can be applied to file allocation. If a file needs five blocks and five consecutive free blocks are available, allocating that entire run keeps the file contiguous and improves sequential access.
The free-space manager therefore influences performance. It does not merely identify unused blocks; it helps determine whether files can be placed in efficient layouts.
Very Large Storage Systems
For storage systems containing hundreds of terabytes, keeping a complete free-space map in memory may be impractical. The system may divide the storage into regions and maintain a separate free-space map for each region.
These regions are sometimes called metaslabs. Each metaslab manages a portion of the overall device, allowing the system to work with smaller maps rather than one enormous structure.
The system can then select an appropriate region, inspect its free-space information, and allocate blocks without loading the entire device’s management data into memory.
Performance Techniques
File-system performance depends on more than the allocation method. The operating system and device can reduce delays by predicting access patterns and avoiding unnecessary device operations.
Buffering and Caching
A buffer temporarily holds data during a transfer. A cache stores recently used data so that it can be reused without another device access.
If a program requests one block, the system may read several neighboring blocks as well. Sequential access is common, so the additional blocks may be needed soon. If those blocks are placed in a cache, later requests can be satisfied from memory.
For example, after reading block 10, the system may also read blocks 11 through 18. If the program soon requests block 14, the data is already available in the cache.
This technique uses extra memory, but it can greatly improve performance because memory access is much faster than storage-device access.
Read-Ahead
Read-ahead is the practice of retrieving data before the program explicitly requests it. The operating system predicts that sequentially adjacent blocks will be needed and loads them in advance.
Read-ahead is especially effective for contiguous files or files whose blocks are located near one another. It is less effective when a file is highly fragmented across the device.
Delayed and Grouped Writes
The system may temporarily keep modified file data or metadata in memory instead of writing it immediately. These delayed writes can later be grouped into larger, more efficient operations.
Grouping writes reduces the number of separate device accesses and may allow the storage device to perform them more efficiently. However, delayed writing creates a risk: if power is lost or a removable device is disconnected before the changes are written, recent data or file-system structures may be lost.
Safe shutdown and safe device removal are therefore essential when buffered or delayed writes are being used.
General Design Trade-Off
File-system design involves balancing competing goals:
- Contiguous allocation improves sequential speed but makes growth difficult.
- Linked allocation makes growth easy but can fragment files.
- Indexed allocation supports scattered blocks while requiring index structures.
- More index levels support larger files but add lookup overhead.
- Bitmaps provide compact, direct free-space information but consume memory.
- Linked free lists use a different space-time trade-off but may require more traversal.
- Caching and read-ahead improve speed but require memory.
- Delayed writes improve efficiency but increase the importance of safe shutdown.
There is no universally best method. The appropriate design depends on the device, file sizes, access patterns, reliability requirements, and available memory.
Comprehensive Review
A file system provides a logical organization for data stored on a physical device. It hides device-specific details behind layers that include the logical file system, file-control structures, the basic block-oriented file system, and device drivers.
Storage is organized into sectors and logical blocks. Boot information, volume information, directories, file-control blocks, and allocation structures must be preserved on the device so that the file system can be reconstructed after shutdown. Frequently used structures may also be copied into memory to avoid repeated slow device accesses.
Directories can be implemented as linear lists or hash tables. Linear lists are simple but become slower as they grow. Hash tables provide faster lookup by converting names into table locations, although collisions must be handled.
Files can be allocated contiguously, through extents, by linked blocks, or through index blocks. Contiguous allocation is fast but vulnerable to external fragmentation and difficult file growth. Extents provide contiguous runs while allowing additional runs to be added. Linked allocation makes growth flexible but can cause fragmented access and pointer-related reliability problems. Indexed allocation improves access to scattered blocks, and multilevel indexing supports very large files.
Free-space management may use bitmaps, linked lists, grouped free-space runs, or region-based structures such as metaslabs. These methods determine how efficiently the system can locate unused storage and how well it can preserve contiguous regions.
Finally, buffers, caches, read-ahead, and delayed writes improve performance by reducing the number and cost of storage-device operations. The fundamental principle is that disk and other mass-storage accesses are much slower than memory operations, so effective file systems organize data and metadata to minimize unnecessary physical accesses while maintaining correctness and reliability.

Ask anything
We've read this whole study set, so questions about it get the best answers.