Every modern Operating System (OS) has a component called a File System. That component is part of the OS kernel and it implements things like "files" and "file directories".
There are many different File Systems, and they use various methods and algorithms, but the same basic functions are present in most File Systems:
- The File System maintains some sort of FAT (File Allocation Table) - information that associates logical files with storage block numbers.
- For example, the FAT can specify that the "File1" file is stored in 5 disk blocks with numbers 123400,123405,123401,177777,123456 and the "File2" file is stored in 6 disk blocks with numbers 323400,323405,323401,377777,323456, 893456.
- The File System maintains a list of all unused storage blocks and it automatically allocates new blocks when the file grows in size, and returns blocks into the list of unused blocks when a file decreases in size or when a file is deleted.
- The File System processes application requests that need to read from or write to logical files. The File System converts these requests into one or several storage block read and write operations, using the information in the File Allocation Table.
- The File System maintains special files called "file directories" and stores the information about other files in these directories.
- The File System maintains the "file cache." When new information is written to a file, it stores it in the Storage System (on disks) and it also copies this information into the File System "cache buffers".
- When file information is read from storage, it passes it to the application program and also copies it into the "cache buffers"
- When the same (or some other) application needs to read the same portion of the cached file, the File System simply retrieves that information from its cache buffers instead of re-reading it from the Storage System.
- The following figure illustrates how a File System works:
In this example, the File System serves requests from two applications
Application 1 asks the File System to read block number 5 from File1.
- The File System finds the information for File1 in the File Allocation Table, and detects that this file has 5 blocks allocated, and file block number 5 is stored in the block number 123456 on the disk.
- The File System uses the disk interface (IDE, SCSI, or any other one) to send the READBLOCK(123456) command to the disk.
- The disk device sends the information from the specified block to the computer.
- The File System places the read information into its cache buffers, and sends it to the application.
Application 2 asks the File System to write block number 7 into File2.
- The File System finds the information for File2 in the File Allocation Table, and detects that this file has 6 blocks allocated. It checks the list of the unused disk blocks, and finds the unused block number 13477.
- It removes the block number from the list of unused blocks and adds it as the 7th block to the File2 information in the File Allocation Table, so now File2 is 7 blocks in size.