File System Implementation


File System Implementation

Maria
Last time we introduced file systems. Today, let’s look more closely at how an operating system organizes files, places them on storage, and gets them back efficiently. The theme is trade-offs: a design that saves space may take longer to use, while a faster design may need more metadata or more complicated hardware.

Simon
And the first important distinction is between the logical view and the physical view. A person or application asks for a named file, such as “Fred.txt.” The device, though, works with numbered blocks in particular places. The operating system connects those two views.

Maria
That connection is built in layers. An application uses the logical file system, which handles things like names and directories. Lower layers organize files into blocks and coordinate input and output; device drivers then provide the interface to the actual hardware.

Simon
So the application doesn’t need to know whether a file is on an internal hard drive, a USB drive, or another supported device. The operating system presents a consistent interface, even though different devices may require different drivers and different physical operations.

Maria
Layering makes it easier to support different devices and file systems without changing every application. It also adds overhead: each request may pass through several layers before reaching the device. That extra work can affect performance, so layering is useful but not free.

Simon
Let’s connect a file name to the actual storage. The file system needs metadata—information about the file, such as its name, size, and modification details—and a way to find the file’s blocks. A file control block, or a comparable structure, holds that information and may include links or indexes to where the data is stored.

Maria
The directory structure matters too, because it maps names and paths to files. And none of this can be used directly by a running program while it remains only on the disk: the needed structures and file data have to be brought into memory.

Simon
The operating system also needs to know what storage is available. When a file system is mounted, the system associates it with a place where users can access it and records what type of file system it is. Some devices, such as an internal disk, are commonly mounted at startup; removable drives may be mounted when they’re connected.

Maria
That’s why plugging in a USB drive can trigger a check. Another computer or operating system may have left different file-system metadata on it, or this computer may not understand its format. And if important metadata is damaged, the system may have trouble locating otherwise intact files.

Simon
Before we talk about allocation strategies, let’s picture a magnetic disk. It has rotating platters, with read/write heads that move across them. Traditionally, each platter surface is described using tracks and sectors; the operating system and device coordinate access to storage blocks.

Maria
That physical arrangement is hidden behind the logical block interface. A request for a named file is translated into requests for particular blocks. The operating system can therefore treat the file as a sequence of data even though the device has its own physical layout.

Simon
Block size is one of the basic trade-offs. If blocks are large, a small file can leave a lot of unused space in its final block. If blocks are small, that waste can be reduced, but there are more blocks to track and more metadata to manage.

Maria
It’s similar to choosing the size of memory chunks: small units make it easier to avoid wasted space, while large units can make management simpler. The file system has to balance unused space against the cost of keeping track of all those units.

Simon
Directories have a similar choice between simplicity and speed. A directory could be stored as a plain list of names and their locations. That’s straightforward, but finding a particular name can mean searching through the list one entry at a time.

Maria
For larger directories, structures such as linked lists or B-trees can make searches faster. Hash tables are another option: a hash function uses a name to calculate a value that helps locate the entry quickly. But hash tables need careful design, and two names can sometimes lead to the same value—a collision—which the system must handle.

Simon
Now let’s look at how a file’s blocks can be placed. With contiguous allocation, the file occupies one run of adjacent blocks. That makes it easy to find: the system can record where it starts and how long it is.

Maria
The weakness is finding a sufficiently large, uninterrupted space. The disk might have plenty of free blocks overall, but they may be scattered. Files can also grow after they’re created, and there may not be room beside them. That can lead to moving files and to fragmentation.

Simon
An extent-based approach keeps the simple idea of runs but lets a file occupy more than one run, or extent. The file might begin with one group of blocks and receive another group as it grows. The system must then keep track of those separate extents.

Maria
A linked allocation approach takes a different route. Each block points to the next block in the file, and the last block marks the end. The file can use blocks wherever they’re available, so it doesn’t need one large contiguous region.

Simon
That flexibility has costs. To reach a later block, the system may have to follow the chain from earlier blocks, and scattered blocks can mean a lot of movement on a hard disk. Also, if a pointer is corrupted, the system may lose the path to the rest of the file.

Maria
A file allocation table, or FAT-style approach, moves those links into a separate table. Each table entry records where to go next, rather than storing the link inside the data block itself. Keeping the table together can make it easier to access and cache in memory.

Simon
But that table becomes important metadata, so damage to it can affect access to many files. Keeping a backup is one possible safeguard. Block size matters here too: smaller blocks mean more entries to track, while larger blocks reduce the number of entries but can waste more space.

Maria
Another option is indexed allocation. Each file has an index block that lists the data blocks belonging to that file. The system can consult the index to find a particular part of the file instead of following a chain through every earlier data block.

Simon
If a file needs more entries than one index block can hold, indexes can be layered. A higher-level index can point to other index blocks, which then point to the file’s data. That supports larger files, but adds another level of structure to manage and sometimes access.

Maria
Unix-style file systems are an example of combining approaches. They can use direct pointers for data blocks, plus single- and double-indirect pointers that lead through one or more index blocks. Frequently needed data can be reached directly, while the indirect levels help represent larger files.

Simon
The particular design should suit its workload. A frequently used executable might benefit from direct access, while an infrequently used large file may rely more on indirect structures. Different devices—even ones used by the same computer—can have different file systems because their purposes differ.

Maria
The system must also keep track of free space. One method is a bitmap with one bit per block: one value means free and the other means in use. The exact convention can vary, but the system must use it consistently.

Simon
For example, if a disk has one tebibyte of space and uses four-kibibyte blocks, it has two to the twenty-eighth blocks. A one-bit-per-block bitmap therefore needs two to the twenty-eighth bits—about thirty-two mebibytes. That is the bit count converted to bytes, and it shows that tracking free space itself uses storage.

Maria
A free list is another approach. The system keeps a reference to a free block, which points to the next free block, and so on. When it allocates the first one, it follows the pointer and updates the list’s starting point.

Simon
Free-space management can be refined in several ways—for example, tracking groups of blocks or using a space map. A system with very large storage needs may use techniques suited to managing that scale. Again, the best choice depends on the size and workload of the system.

Maria
Performance can also improve through buffering and caching. If the system expects information to be used again, it can keep it in memory rather than fetch it from a slower device each time. It can also read ahead: when it fetches one block, it may fetch the next one in case it’s needed soon.

Simon
Let’s compare the storage devices themselves. A magnetic hard disk stores data using magnetized regions on rotating platters. Its access time depends partly on moving the head to the right track and waiting for the desired part of the platter to rotate underneath it.

Maria
Those delays are called seek time and rotational latency. The total time also includes transferring the data and any controller overhead. So a useful estimate of access time adds up the time to position the head, wait for rotation, transfer the data, and account for the controller.

Simon
A faster-spinning disk can reduce the rotational wait. Seek time depends on how quickly the head can move, while transfer rate depends on the device and the connection. These factors help explain why disk input and output can take far longer than operations on data already in memory.

Maria
Solid-state drives have no spinning platters or moving read/write heads, so they avoid seek time and rotational latency. They can offer faster access, but the source material contrasts them with hard disks as typically more expensive for an equivalent capacity, and notes that their memory cells have limited write cycles.

Simon
Wear-leveling software helps distribute writes across the drive so that the same cells aren’t used repeatedly while others are left idle. Magnetic tape is another option: it can store large amounts of data at low cost, but access is slow. That gives us a familiar trade-off among speed, cost, and capacity.

Maria
Devices also connect to computers through different interfaces and buses, such as USB and other storage connections. A disk controller sits between the computer and the device, coordinating how requests reach the storage hardware.

Simon
Storage doesn’t have to be attached directly to one computer. A storage area network connects storage across a network, and network-attached storage provides storage over a network connection. Cloud storage is another example: the files appear in a familiar interface, even though the storage is on computers somewhere else.

Maria
The operating system’s layers help hide those differences. But network storage brings its own performance considerations: many users may request data at once, and the network connection can become important. Systems can use faster connections or distribute data across devices so that requests can be handled in parallel.

Simon
Now suppose several requests are waiting for a hard disk. First come, first served handles them in the order they arrived, which is fair in that simple sense, but the head may have to travel back and forth across the disk many times.

Maria
Shortest seek time first chooses the pending request nearest the head’s current position. That can reduce movement, but it risks postponing requests that are far away if closer requests keep arriving. It resembles shortest-job-first scheduling, where a request may wait too long unless the system deals with that risk.

Simon
The scan algorithm moves the head in one direction, serving requests along the way, then reverses and serves requests on the return trip. It’s often pictured as an elevator moving up and down: requests get handled as the elevator passes their floors.

Maria
Circular scan, or C-SCAN, serves requests in one direction and then returns quickly to the beginning without serving requests on that return. It treats the disk as a circular path and can give requests a more uniform wait.

Simon
LOOK and C-LOOK avoid travelling all the way to the physical end of the disk when there are no requests there. LOOK reverses at the last request in the current direction; C-LOOK returns to the first request at the other end. That can save unnecessary movement.

Maria
There isn’t one best scheduling algorithm for every system. The choice depends on the workload and desired performance. A desktop and a heavily loaded server may make different choices, and specialized systems may use algorithms tailored to their needs.

Simon
Disk management begins by formatting the device so it has a usable organization of sectors and blocks. The disk can then be divided into partitions. A partition is a division of a physical disk; each partition can be set up as a logical storage area and may use its own file system.

Maria
A volume is a logical way of presenting storage. It might correspond to a partition, or it might span more than one physical disk. So a partition describes a division of physical storage, while a volume describes a logical storage unit the system can work with.

Simon
A file system often groups blocks into clusters for allocation. Some applications may instead request raw access to a partition, because they want to manage the storage in their own way. Those are different levels of control, and the operating system has to support the arrangements it provides.

Maria
To start a computer, the system needs boot information. A master boot record is one example of disk metadata that can contain boot code and a partition table. The boot process uses that information to select a partition from which to start an operating system.

Simon
A disk may also have swap space used in virtual memory. Some systems place it in a separate partition; others manage it as a file. Keeping it contiguous was described as a way to avoid extra movement on a hard disk, although the exact arrangement depends on the operating system.

Maria
Finally, RAID combines multiple disks into an array. Depending on the RAID level, it can spread data across disks to support parallel access, keep duplicate copies through mirroring, or use parity information to help reconstruct data if a disk fails.

Simon
Those choices change both capacity and reliability. Mirroring uses extra disks to keep copies, so usable capacity is lower than the total installed capacity. Other arrangements use their own balance of performance, redundancy, and space. A RAID system can also support hot swapping, letting someone replace a failed disk while the system continues running.

Maria
RAID isn’t a substitute for thinking about the whole system: its hardware and software both matter, and the right arrangement depends on what the system needs. The big picture is consistent throughout this topic: file systems, storage media, allocation methods, and scheduling algorithms are all ways of balancing speed, space, reliability, and complexity.

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