File System Implementation


File System Implementation

Anna
Today we're looking at Chapter 11: implementing file systems. The central question is how a mass-storage device can organize data so that users can access files easily while the device stores them efficiently.

Simon
And although we often talk about hard disks, the ideas apply more broadly. A file system can be used with different storage devices, including internal storage, removable media, USB drives, optical discs, and network storage.

Anna
A useful starting point is the relationship between the device and the file system. Storage devices work with physical units such as sectors, while file systems commonly group sectors into larger logical blocks. These sizes are usually chosen so they fit together neatly, often as powers of two, rather than having awkward mismatched sizes.

Simon
So a logical block might be made from several physical sectors. Reading one logical block could therefore require several lower-level operations by the device driver, because the driver physically locates and reads the individual sectors.

Anna
Exactly. The device driver knows how to perform the physical access: for example, locating a particular cylinder, track, or sector and transferring its contents into memory. The basic file system operates at a higher level and asks for logical blocks rather than dealing directly with every detail of the hardware.

Simon
There can be several layers between an application and the storage medium. Users and application programs think in terms of named files and directories. The file system translates those logical ideas into physical locations, and device drivers handle the hardware-specific access.

Anna
That layering is what lets us use different devices without having to understand their internals. A USB drive can have a different driver from a hard disk, but the higher layers can still provide the same general file interface. From the user's point of view, accessing a file on a USB drive, a network location, or another storage device can follow the same model.

Simon
The layers also explain why file systems differ. Different devices may need different systems, and different uses may require different features or performance characteristics. A file system designed for one purpose may not be ideal for every kind of data or every storage medium.

Anna
At the logical level, a file can have information such as a name, creation date, size, and other metadata. This information is stored in a file control block, or FCB. Some metadata is fairly universal, such as a file name, while other features may be specific to a particular file system, such as labels or colors attached to files.

Simon
A storage device also needs information about itself. It may contain a boot control block, which holds information needed to start an operating system from that device, along with details about volumes, their sizes, and their boundaries.

Anna
The device must also record which blocks are used and which are free. And because users normally do not want one enormous undifferentiated collection of files, the directory structure has to be stored as well. These structures are kept on the device so they survive when the computer is turned off.

Simon
But keeping everything only on the device would make access slow. To open a file, the system would first have to read the directory structure to find the file's location, and then access the file itself. That could require at least two storage accesses instead of one.

Anna
To improve performance, the operating system can bring directory information and file control blocks into memory. Then, when a user opens a file such as "thread.txt," the system can look up its location in memory and go directly to the relevant data on secondary storage.

Simon
The trade-off is memory usage. A computer with many disks, storage devices, directories, or active files may need to keep many structures in memory. Caching improves speed, but those cached structures consume working memory.

Anna
Storage is also divided into partitions or volumes: multiple logical sections of a physical device. Connecting a volume to the operating system's file-system structure is called mounting.

Simon
Mounting can be almost invisible to users. For example, inserting a USB drive may cause the system to assign it a drive letter. In Unix-like systems, different devices can instead be mounted at different locations in one directory tree, such as a location for program files or another for user data.

Anna
Most systems have a root partition, which is the main partition containing the operating system. During startup, the system can check the file system for consistency: whether directory entries point to valid files, whether the structures agree with one another, and whether the stored data is organized correctly.

Simon
That is why a computer may display a file-system check during startup. If the system was interrupted or a removable device was taken out without properly notifying the operating system, directory structures, file control blocks, pointers, or even file data may not have been written back from memory.

Anna
Now let's consider how directories themselves can be implemented. The simplest approach is a linear list of directory entries. It is easy to build, but finding a file can be slow because the system may have to search through the entries one by one.

Simon
If the desired entry is equally likely to appear anywhere in the list, the average search examines about half the list. That is acceptable for a small directory, but it becomes increasingly inefficient as the directory grows.

Anna
Another approach is a hash table. A hash function takes data, such as a file name, and produces a number that identifies where to look. The same input produces the same result, so the system can usually find the corresponding directory entry much more directly than by scanning the whole list.

Simon
Hashing is not completely problem-free. Different inputs can sometimes produce the same number; that situation is called a collision and needs to be handled. Even so, hash tables can speed up access to directory entries.

Anna
Once the system can find files, it still needs to decide where their blocks go on the device. With contiguous allocation, all the blocks belonging to a file are placed next to one another.

Simon
That is excellent for performance. If a file occupies ten consecutive blocks, the device can read those blocks in sequence. A simple directory entry can record the file's starting block and its length. For example, if each block is 512 bytes, the system can calculate the location of a particular part of the file from its starting position and an offset.

Anna
The weakness is growth and fragmentation. If a file becomes larger and the blocks immediately after it are already occupied, the file may have to be moved to another sufficiently large region. Over time, free space can be split into many smaller gaps, so there may be enough total space but no single gap large enough for a new file.

Simon
One way to reduce that problem while retaining contiguous regions is to use extents. Instead of reserving one enormous region for the entire file, the system allocates an initial contiguous group of blocks, called an extent. If the file grows, it can request another extent.

Anna
Extents are useful because they let a file grow without reserving large amounts of unused space from the start. They can also preserve good sequential performance because each extent is internally contiguous. However, the system has to record how the extents belong to the same file, and a file spread across several extents is no longer one single contiguous region.

Simon
Another strategy is linked allocation. The file can use individual blocks wherever free space is available, and each block contains a pointer to the next block belonging to that file. This makes it easier to grow a file and avoids the need to find one large contiguous region.

Anna
The disadvantage is access speed. To reach a later block, the system may have to read the current block first, follow its pointer, read the next block, and continue. If those blocks are scattered across the device, the storage head may have to move repeatedly, making a fragmented file slow to read.

Simon
Linked allocation also has a reliability concern. If a pointer in the chain is damaged or lost, the system may not know where to find the rest of the file. A familiar version of this idea is a file allocation table, or FAT, which records the first block of a file and the link to the next block, continuing until the end of the chain.

Anna
In one linked example, a directory entry might say that a file begins at block 9. Block 9 points to block 16, block 16 points to block 1, block 1 points to block 10, and block 10 points to block 25. A special end marker at block 25 indicates that the chain is complete.

Simon
Some linked approaches store the expected ending block in the directory as well. Comparing the reached end with that expected value provides some confidence that the entire file has been located. A basic file allocation table may record the starting block and the chain, but it does not necessarily provide the same separate confirmation of the final block.

Anna
Indexed allocation addresses the repeated-following problem differently. Instead of putting the pointer to the next block inside every data block, the system stores an index block containing the locations of all the file's data blocks.

Simon
Then the file's directory entry can point to that index block. The system reads the index and can request the listed data blocks, rather than reading one data block merely to discover the location of the next one. The index consumes some storage, but the improvement in access speed can make that overhead worthwhile.

Anna
A single index block has a capacity limit. If the file needs more entries than fit in that block, the index can point to another index block. This produces multiple levels: direct blocks point straight to data, single indirect blocks point to an index of data blocks, and double or triple indirect structures add further levels.

Simon
A file system can combine these methods. Unix file systems, for example, can use direct pointers for small or frequently accessed portions and indirect levels for larger files. The appropriate design depends on file sizes, access patterns, device characteristics, and the performance trade-offs the system is intended to make.

Anna
There is always a cost to extra levels. A contiguous file can be accessed very quickly, while an indexed file may require an additional index lookup. But the system can reduce that cost by keeping index blocks in memory, and in general an extra CPU instruction is much cheaper than an additional disk access.

Simon
The final major issue is free-space management: remembering not only where files are, but where new files can be placed. One method is a bitmap with one bit per logical block. A bit indicates whether that block is available or already in use.

Anna
A bitmap can be searched efficiently, especially when the CPU has an instruction that finds the position of the first one bit in a word. But the bitmap itself requires memory or storage. For example, with the arrangement described in the lecture, a one-terabyte disk could require about 64 megabytes for the bitmap.

Simon
Instead of one bit for every block, the system can keep a linked list of free blocks. It records the first free block, and each free block points to the next one. Allocating space means taking a block from the list and updating the list to identify the next available block.

Anna
A grouped free-space list can reduce the amount of pointer-following. One entry can record the start of a contiguous run of free blocks, how many blocks are in that run, and where the next run begins. For example, an entry might say that four free blocks begin at one location and the next group begins at another.

Simon
Grouping also helps the system choose a sufficiently large contiguous region when it wants one. For very large storage systems, however, keeping a complete free-space map in memory may be impractical. The storage can be divided into regions, sometimes called metaslabs, with separate space maps rather than one enormous map for the whole device.

Anna
All these choices affect performance: directory lookup, file allocation, free-space tracking, and physical access. The system may use buffering and caching in the storage controller or in memory, so data can wait in a buffer until the requesting component is ready.

Simon
Read-ahead is another optimization. If the system reads one block, it may also bring nearby blocks into a cache because sequential access makes it likely they will be requested next. Then a later request can be served from memory instead of requiring another device access.

Anna
The system can also delay writes. Rather than immediately writing every small change to the device, it may collect writes and perform them together, while continuing other work. This can improve efficiency, but it also explains why removing a USB device without properly ejecting or unmounting it can risk losing updates that are still waiting in memory.

Simon
So the big picture is a series of connected decisions. The file system provides a user-friendly logical view, metadata and directories describe the files, allocation methods map files to blocks, free-space structures identify where new data can go, and drivers translate the requests into physical device operations.

Anna
And there is no single perfect method. Contiguous allocation is simple and fast but struggles with growth and fragmentation. Extents preserve contiguous runs while allowing growth, linked allocation uses space flexibly but can be slow and vulnerable to broken links, and indexed allocation improves access to scattered blocks at the cost of extra index storage and lookups.

Simon
The same principle applies to directories and free space: simple lists are easy but may be slow, hashes speed up lookup but can have collisions, bitmaps make block status easy to represent but can require substantial space, and linked or grouped structures trade memory use against access time.

Anna
Understanding the diagrams for these systems is important because they show the translation from a logical file name to the physical blocks containing its data. The key is to trace each step: directory entry, file control information, index or pointer structure if one exists, and finally the device blocks themselves.

Simon
Once those layers and trade-offs make sense, file-system implementation becomes much less mysterious. Every design is balancing accessibility, storage efficiency, reliability, memory use, and the time required to reach the data.

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