Linux File System

Linux file system does things more differently than the Windows file system. Linux file systems implement a basic set of common concepts derivated from the UNIX OS. Files are represented by inodes, and directories are simply files containing a list of entries and devices can be accessed by requesting I/O on special files.

Inodes:

An Inode is a structure representing each file under Linux. Inode contains information like description of the file, file type, access rights, size pointers to data blocks and timestamps. An inode plays a vital role in the read/write operation of a file. When a user requests an I/O operation on the file, the kernel code converts the current offset to a block number, uses this number as an index in the block addresses table and reads or writes the physical block.

Directories

Directory is a collection of files and subdirectories. They are implemented as special types of files. Directories are structured in a hierarchical tree containing a list of entries. When a process uses a pathname, the kernel code searches in the directories to find the corresponding inode number. After the name has been converted to an inode number, the inode is loaded into memory and is used by subsequent requests.

Links

The inode contains a field containing the number associated with the file. Adding a link simply consists of creating a directory entry, where the inode number points to the inode, and in incrementing the links count in the inode. When a link is deleted, the kernel decrement the links count and de-allocates the inode if this count becomes zero. This type of link is called a hard link and can only be used within a single file system: it is impossible to create cross-file system hard links. Moreover, hard links can only point on files: a directory hard link cannot be created to prevent the apparition of a cycle in the directory tree.

In simple terms, we can say that links are the total number of inodes. Another kind of link exists in most UNIX file systems. Symbolic links are simply files which contain a filename. When the kernel encounters a symbolic link during a pathname to inode conversion, it replaces the name of the link by its contents, i.e. the name of the target file, and restarts the pathname interpretation. Since a symbolic link does not point to an inode, it is possible to create cross-file systems symbolic links. Symbolic links can point to any type of file, even on nonexistent files. Symbolic links are very useful because they don't have the limitations associated to hard links. However, they use some disk space, allocated for their inode and their data blocks, and cause an overhead in the pathname to inode conversion because the kernel has to restart the name interpretation when it encounters a symbolic link.

Device Special Files

In Linux OS there exists two special files, namely character and block special files. The former allows I/O operations in character mode while the later requires data to be written in block mode via the buffer cache functions. When an I/O request is made on a special file, it is forwarded to a (pseudo) device driver. A special file is referenced by a major number, which identifies the device type, and a minor number, which identifies the unit.

These special files do not use any space on the file system. It is only an access point to the device driver.

The Virtual File System

The Linux kernel contains a virtual file system layer which is used during system calls acting on files. The VFS is an indirection layer which handles the file oriented system calls and calls the necessary functions in the physical file system code to do the I/O.

This indirection mechanism is frequently used in UNIX-like OSs to ease the integration and the use of several file system types.

When a process issues a file oriented system call, the kernel calls a function contained in the VFS. This function handles the structure independent manipulations and redirects the call to a function contained in the physical file system code, which is responsible for handling the structure dependent operations. File system code uses the buffer cache functions to request I/O on devices.

The VFS Structure

VFS is an interface made up of a set operations to perform a set of functions that every file system has to implement. The VFS structure operations are associated to three kinds of objects file systems, inodes and open files.

VFS structure uses a table defined during the kernel configuration. Each entry in this table describes a file system type: it contains the name of the file system type and a pointer on a function called during the mount operation. When a file system is to be mounted, the appropriate mount function is called. After the file system is mounted, the VFS functions can use this descriptor to access the physical file system routines.

Two other types of descriptors are used by the VFS: an inode descriptor and an open file descriptor. Each descriptor contains information related to files in use and a set of operations provided by the physical file system code. While the inode descriptor contains pointers to functions that can be used to act on any file (e.g., create, unlink), the file descriptors contains pointer to functions which can only act on open files (e.g., read, write).