How are filesystem organized with multiple drives?

Whenever you insert a floppy disk, or a CD or DVD, or even a USB drive, the operating system will detect this event and make that drive letter available for use. So it’s not really the drive itself that makes a drive letter. But instead it’s the presence of a filesystem that makes a drive letter. If you have a floppy drive that’s empty, we might think of that as drive A. But you won’t have a drive A available for use until you insert a floppy disk that contains a filesystem and the operating system recognizes the filesystem.

Make sure to listen to the full episode for more details about the shell and how this interacts with the filesystem. You’ll also learn about how you can control when information actually gets written to a filesystem through your application code by flushing buffers. You can also read the full transcript below.

Transcript

The solution that was included with DOS and later became part of Windows was to give each drive a letter. The first floppy drive was A and the second floppy drive was B. The first hard drive was C and other drives would follow.

It’s possible to specify that a CD or DVD drive should be something other than the next available letter. And you can access network filesystems through drive letters too. This system gives you 26 possible drives. Although drives A, B, and C should really be reserved for floppies and the first hard drive.

Whenever you insert a floppy disk, or a CD or DVD, or even a USB drive, the operating system will detect this event and make that drive letter available for use. So it’s not really the drive itself that makes a drive letter. But instead it’s the presence of a filesystem that makes a drive letter.

If you have a floppy drive that’s empty, we might think of that as drive A. But you won’t have a drive A available for use until you insert a floppy disk that contains a filesystem and the operating system recognizes the filesystem.

Let me be a bit more specific though. I’ve been referring to the operating system up till now. It’s probably time that I describe something called the shell. The shell is part of the operating system in the sense that you get a shell with the operating system. But it’s not part of the deep internals of the operating system. It’s like a shell that sits around the operating system. For most of us, the shell is what we see and what we think of as the operating system. It would be like how a company spokesperson can become famous and come to represent the company as a whole. But the spokesperson is not the company just like the shell is not the operating system.

Some operating systems could even have different shells that you can either choose from or maybe install later. It’s like choosing a new theme. The entire look and feel of your computer can change with a new shell.

With Windows, the shell makes up the desktop and the task bar. It also forms the file explorer you use to browse for files. And the file open dialogs. With a Mac and Linux, it forms the desktops and the launch bar and probably a lot more. In older text based operating systems such as DOS, there was a shell too that was used to manage the command prompts. With Linux and Mac terminal windows, you’ll still hear about the Bash shell and the Korn shell which are two popular command line shells.

When you insert a floppy disk, or an optical disk, or a USB storage device, the operating system will be made aware of the new device. But it’s most likely the shell that figures out what to do with it. Many operating systems require that the filesystem that was just inserted must be mounted before it can be used.

I remember when Linux was new, this mounting had to be done manually. There was a file that could be updated with instructions to do this automatically. It was still very much a manual process. Mounting the removable filesystem just made it part of the bigger filesystem that the operating system managed. To mount a storage device or floppy drive, you had to specify which device to mount, what type of filesystem to expect, and where it should be mounted to.

Windows took a simpler approach and did the mounting for you to a drive letter. The drive letter forms the root of that filesystem. Any files and folders in the filesystem can be accessed by their path. And the path starts from where the drive was mounted. Each drive letter forms a separate filesystem because each drive letter is the root of all the paths.

Linux and other Unix based computers such as a Mac have a single root for the whole filesystem. When you mount a disk or other device in this system, it becomes part of the overall filesystem instead of a separate filesystem.

Now, the shell can still get involved with this process and make it seem like the removable storage is separate. And the shell can even make things appear to be part of the filesystem that aren’t files at all.

You can do a lot with software. You could write a device driver which does become part of the inner workings of the operating system and this device driver can mimic the filesystem. The lines can be blurred between what is and is not a filesystem and where it lives.

But in general, the operating system will be responsible for keeping track of the various filesystems and how you open and close files. And in general, the shell will be responsible for how you see and interact with the filesystem.

I’ve mentioned that filesystems need to be mounted before they can be used. This can either be done automatically or manually. But they also need to be unmounted. This can be done in a well defined manner or accidentally.

Unmounting is actually a lot more important than it might seem. You see, any filesystem is really slow compared to a computer’s main memory. And the main memory is really slow compared to the various levels of caches in the processors. In order to make computers responsive, there’s no way that every time your application wants to change something on a filesystem that the change can happen immediately.

If you have a document or a image file that your application is editing, that file might be several million bytes long or even more. The only way to make this work if for the application to write the data to a filesystem driver which will keep the changes in memory until it has enough queued up to make it worthwhile to send to the filesystem.

There is a type of filesystem that can exist in a computer’s main memory. This is called a virtual filesystem and can be much faster.

Or maybe the filesystem exists on an optical drive and to write to this type of media, a laser must burn away a portion of the disk. This is even slower than a magnetic floppy or hard drive.

The point is, that there can be different kinds of filesystems on different types of drives and media. The operating system and the various device drivers make all this look the same. But you still have the problem of speed. Your application will usually be able to write data much faster than the filesystem can handle. The only reason filesystems can keep up is either for the application to slow down and wait or if the application writes some information, does some other work, writes some more information, then does something else, etc.

Normally, applications will write information in bursts and let the filesystem drivers manage when that information actually makes it all the way onto the filesystem. The information might need to be queued up for a short while in main memory.

Now imagine what happens if you forcibly remove a device with a filesystem on it before the device drivers have a chance to get everything onto the filesystem. You can end up with things written only halfway and generally in a mess.

The next time you insert this filesystem, the computer might not be able to make any sense of it. It’s been corrupted.

When you eject a storage device with a filesystem, the operating system will make sure than anything still waiting to be written will complete. Once everything has finished, the disk or device can be removed.

This process is called flushing the buffers or just flushing for short. And you can also choose when you want to flush changes in your application. Maybe you let the user save their work and you want to make sure that everything gets written all the way to the filesystem. You can flush the files that you have open. That way, if the user decides to remove the disk, you can make sure everything gets written. Even if you’re working with a filesystem on non-removable media such as an internal hard drive, there’s always the possibility that something could crash or the computer could lose power. Flushing may not prevent all filesystem corruption, but it can really help.