← All writing

Operating Systems: Three Easy Pieces (3)

Storage and File Systems

Parts: 1 · 2 · 3

36 IO Devices

36.1 System Architecture

The picture shows a single CPU attached to the main memory of the system via some kind of memory bus or interconnect. Some devices are connected to the system via a general I/O bus, which in many modern systems would be PCI (or one of its many derivatives); graphics and some other higher-performance I/O devices might be found here. Finally, even lower down are one or more of what we call a peripheral bus, such as SCSI, SATA, or USB. These connect slow devices to the system, including disks, mice, and keyboards.

See origin for more details

36.2 A Canonical Device

a device has two important components. The first is the hardware interface it presents to the rest of the system. Just like a piece of software, hardware must also present some kind of interface that allows the system software to control its operation.

The second part of any device is its internal structure. This part of the device is implementation specific and is responsible for implementing the abstraction the device presents to the system.

36.3 The Canonical Protocol

The (simplified) device interface is comprised of three registers (a memory space in device): a status register, which can be read to see the current status of the device; a command register, to tell the device to perform a certain task; and a data register to pass data to the device, or get data from the device.

Using an interrupt in this case will actually slow down the system: switching to another process, handling the interrupt, and switching back to the issuing process is expensive.

Thus, if a device is fast, it may be best to poll; if it is slow, interrupts, which allow overlap, are best.

36.5 More Efficient Data Movement With DMA

A DMA (Direct Memory Access) engine is essentially a very specific device within a system that can orchestrate transfers between devices and main memory without much CPU intervention.

DMA works as follows. To transfer data to the device, for example, the OS would program the DMA engine by telling it where the data lives in memory, how much data to copy, and which device to send it to. At that point, the OS is done with the transfer and can proceed with other work. When the DMA is complete, the DMA controller raises an interrupt, and the OS thus knows the transfer is complete.

36.6 Methods Of Device Interaction

The first, oldest method (used by IBM mainframes for many years) is to have explicit I/O instructions.

They are part of the instruction set architecture (ISA), and they are used to communicate directly with I/O device registers. Use special instructions like in/out to talk to I/O ports (available in x86).

For example, to send data to a device, the caller specifies a register with the data in it, and a specific port which names the device.

The second method to interact with devices is known as memory mapped I/O.

With this approach, the hardware makes device registers available as if they were memory locations. To access a particular register, the OS issues a load (to read) or store (to write) the address; the hardware then routes the load/store to the device instead of main memory

36.7 Fitting Into The OS: The Device Driver (Amazing!!)

One final problem we will discuss: how to fit devices, each of which have very specific interfaces, into the OS, which we would like to keep as general as possible.

The problem is solved through the age-old technique of abstraction. At the lowest level, a piece of software in the OS must know in detail how a device works. We call this piece of software a device driver, and any specifics of device interaction are encapsulated within.

Interestingly, because device drivers are needed for any device you might plug into your system, over time they have come to represent a huge percentage of kernel code.

Studies of the Linux kernel reveal that over 70% of OS code is found in device drivers [C01]; for Windows-based systems, it is likely quite high as well. (Amazing!!!!)

37 Hard Disk Drives (Learned many times)

The drive consists of a large number of sectors (512-byte blocks), each of which can be read or written. The sectors are numbered from 0 to n − 1 on a disk with n sectors. Thus, we can view the disk as an array of sectors; 0 to n − 1 is thus the address space of the drive.

Multi-sector operations are possible; indeed, many file systems will read or write 4KB at a time (or more). However, when updating the disk, the only guarantee drive manufacturers make is that a single 512-byte write is atomic (i.e., it will either complete in its entirety or it won’t complete at all); thus, if an untimely power loss occurs, only a portion of a larger write may complete (sometimes called a torn write).

One can also usually assume that accessing blocks in a contiguous chunk (i.e., a sequential read or write) is the fastest access mode, and usually much faster than any more random access pattern.

Finally, an important part of any modern disk drive is its cache, for historical reasons sometimes called a track buffer.

For example, when reading a sector from the disk, the drive might decide to read in all of the sectors on that track and cache them in its memory; doing so allows the drive to quickly respond to any subsequent requests to the same track.

TIP: USE DISKS SEQUENTIALLY

When at all possible, transfer data to and from disks in a sequential manner.

Other learned details are omitted.

38 Redundant Arrays of Inexpensive Disks (RAIDs)

To be learned.

39 Interlude: Files and Directories

39.1 Files And Directories

A file is simply a linear array of bytes, each of which you can read or write.

Each file has some kind of low-level name, usually a number of some kind; often, the user is not aware of this name (as we will see). For historical reasons, the low-level name of a file is often referred to as its inode number (i-number).

In most systems, the OS does not know much about the structure of the file (e.g., whether it is a picture, or a text file, or C code); rather, the responsibility of the file system is simply to store such data persistently on disk and make sure that when you request the data again, you get what you put there in the first place. Doing so is not as simple as it seems!

The second abstraction is that of a directory.

A directory, like a file, also has a low-level name (i.e., an inode number), but its contents are quite specific: it contains a list of (user-readable name, low-level name) pairs.

39.3 Creating Files

One important aspect of open() is what it returns: a file descriptor.

A file descriptor is just an integer, private per process, and is used in UNIX systems to access files; thus, once a file is opened, you use the file descriptor to read or write the file, assuming you have permission to do so.

As stated above, file descriptors are managed by the operating system on a per-process basis. This means some kind of simple structure (e.g., an array) is kept in the proc structure on UNIX systems.

39.4 Reading And Writing Files

These file structures represent all of the currently opened files in the system; together, they are sometimes referred to as the open file table.

39.7 Writing Immediately With fsync()

Most times when a program calls write(), it is just telling the file system: please write this data to persistent storage, at some point in the future.

The file system, for performance reasons, will buffer such writes in memory for some time (say 5 seconds, or 30); at that later point in time, the write(s) will actually be issued to the storage device.

When a process calls fsync() for a particular file descriptor, the file system responds by forcing all dirty (i.e., not yet written) data to disk, for the file referred to by the specified file descriptor.

39.9 Getting Information About Files

Beyond file access, we expect the file system to keep a fair amount of information about each file it is storing. We generally call such data about files metadata.

39.12 Reading Directories

Specifically, an empty directory has two entries: one entry that refers to itself, and one entry that refers to its parent. The former is referred to as the “.” (dot) directory, and the latter as “..” (dot-dot).

sh
prompt> ls -a
./ ../

Now that we’ve created a directory, we might wish to read one too. Indeed, that is exactly what the program ls does.

39.14 Hard Links

Learned before.

39.15 Symbolic Links

The first difference is that a symbolic link is actually a file itself, of a different type. Symbolic links are a third type the file system knows about.

As you can see in this example, quite unlike hard links, removing the original file named file causes the link to point to a pathname that no longer exists.

39.17 Making And Mounting A File System

Read the original text.

40 File System Implementation (Awesome!)

It is worthy refering to original text!!

40.1 The Way To Think

To think about file systems, we usually suggest thinking about two different aspects of them.

The first is the data structures of the file system. In other words, what ypes of on-disk structures are utilized by the file system to organize its data and metadata?

The second aspect of a file system is its access methods. How does it map the calls made by a process, such as open(), read(), write(), etc., onto its structures?

40.6 Access Paths: Reading and Writing

Reading A File From Disk

Also note that the amount of I/O generated by the open is proportional to the length of the pathname. For each additional directory in the path, we have to read its inode as well as its data.

If the OS has already accessed these directories before, the metadata is cached in RAM (e.g., in the inode or directory cache), and performance won't suffer much.

Writing A File To Disk

Thus, each write to a file logically generates five I/Os: one to read the data bitmap (which is then updated to mark the newly-allocated block as used), one to write the bitmap (to reflect its new state to disk), two more to read and then write the inode (which is updated with the new block’s location), and finally one to write the actual block itself.

41 Locality and The Fast File System

42 Crash Consistency: FSCK and Journaling

42.2 Solution #1: The File System Checker

A classic example of this lazy approach is found in a tool that does this: fsck. fsck is a UNIX tool for finding such inconsistencies and repairing them [MK96]

Note that such an approach can’t fix all problems; consider, for example, the case above where the file system looks consistent but the inode points to garbage data.

The only real goal is to make sure the file system metadata is internally consistent.

However, fsck (and similar approaches) have a bigger and perhaps more fundamental problem: they are too slow.

It is incredibly expensive to scan the entire disk to fix problems that occurred during an update of just three blocks.

42.3 Solution #2: Journaling (or Write-Ahead Logging)

Probably the most popular solution to the consistent update problem is to steal an idea from the world of database management systems.

The basic idea is as follows. When updating the disk, before overwriting the structures in place, first write down a little note (somewhere else on the disk, in a well-known location) describing what you are about to do.

42.4 Solution #3: Other Approaches

However, these are not the only two approaches. One such approach, known as Soft Updates [GP94], was introduced by Ganger and Patt.

Another approach is known as copy-on-write (yes, COW), and is used in a number of popular file systems, including Sun’s ZFS [B07].

Another approach is one we just developed here at Wisconsin. In this technique, entitled backpointer-based consistency (or BBC), no orderingis enforced between writes.


← Previous

← Back to ArchiveBack to top ↑

Comments

No approved comments yet.

Your display name and plain-text comment become public only after owner approval. No account or email is required.