Linux file system analysis

  
 

In terms of file system, Linux® can be regarded as the "Swiss Army Knife" in the operating system
. Linux supports a wide variety of file systems, from journaled file systems to clustered file systems and encrypted file systems. Linux is an excellent platform for using standard and exotic file systems and developing file systems. This article discusses the virtual file system (VFS, sometimes called the virtual file system switch) in the Linux kernel, and then introduces the main structure that connects the file

systems together.


Basic File System Architecture

The Linux file system architecture is an interesting example of abstracting complex systems. By using a common set of API functions, Linux can support many file systems on a wide variety of storage devices. For example, a read function call can read a certain number of bytes from a specified file descriptor. The read function does not know the type of file system, such as ext3 or NFS. It also does not know the storage medium on which the file system resides, such as an AT Attachment Packet Interface (ATAPI) disk, a Serial-Attached SCSI (SAS) disk, or a Serial Advanced Technology Attachment (SATA) disk. However, when a file is read by calling the read function, the data will return normally. This article explains the implementation of this mechanism and introduces the main structure of the Linux file system layer.


What is a file system?

First answer the most common questions, “What is the file system”. A file system is a mechanism for organizing data and metadata on a storage device. Since the definition is so broad, the code that supports it will be very interesting. As mentioned earlier, there are many kinds of file systems and media. Because of these many types, it is expected that the Linux file system interface will be implemented as a layered architecture that separates the user interface layer, the file system implementation, and the drivers that operate the storage device.


File System as a Protocol

Another way of looking at a file system is to think of it as a protocol. Network protocols (such as IP) dictate the meaning of the data stream transmitted over the Internet. Similarly, the file system gives the meaning of the data on a particular storage medium.


Filling

The process of associating a file system with a storage device in Linux is called mount. Attach a file system to the current file system hierarchy (root) using the mount command. When performing a mount, provide the file system type, file system, and a mount point. To illustrate the functionality of the Linux file system layer (and how to mount it), we create a file system in a file in the current file system. The way to do this is to first create a file of the specified size with the dd command (copy files with /dev/zero as the source) —— In other words, a file initialized with zeros, as shown in Listing 1. Listing 1. Create an initialized file $ dd if=/dev/zero of=file.img bs=1k count=1000010000+0 records in10000+0 records out$ Now you have a 10MB file.img file. Use the losetup command to associate a loop device with this file, making it look like a block device, rather than a regular file in the file system: $ losetup /dev/loop0 file.img$ This file now appears as a block device ( Represented by /dev/loop0). Then use mke2fs to create a file system on this device. This command creates a new ext2 filesystem of the specified size, as shown in Listing 2. Listing 2. Creating an ext2 filesystem with a looping device $ mke2fs -c /dev/loop0 10000mke2fs 1.35 (28-Feb-2004)max_blocks 1024000, rsv_groups = 1250, rsv_gdb = 39Filesystem label=OS type: LinuxBlock size=1024 (log=0 )Fragment size=1024 (log=0)2512 inodes, 10000 blocks500 blocks (5.00%) reserved for the super user...$mount the file.img file represented by the loop device (/dev/loop0) using the mount command To the mount point /mnt/point1. Note that the file system type is specified as ext2. Once mounted, you can use this mount point as a new file system, such as using the ls command, as shown in Listing 3. Listing 3. Creating a mount point and mounting the filesystem through a loop device $ mkdir /mnt/point1$ mount -t ext2 /dev/loop0 /mnt/point1$ ls /mnt/point1lost+found$ As shown in Listing 4, Continue this process: create a new file in the file system you just mounted, associate it with a looping device, and create another filesystem on it. Listing 4. Creating a new loop file system in the loop file system $ dd if=/dev/zero of=/mnt/point1/file.img bs=1k count=10001000+0 records in1000+0 records out$ losetup /Dev/loop1 /mnt/point1/file.img$ mke2fs -c /dev/loop1 1000mke2fs 1.35 (28-Feb-2004)max_blocks 1024000, rsv_groups = 125, rsv_gdb = 3Filesystem label=...$ mkdir /mnt/point2$ Mount -t ext2 /dev/loop1 /mnt/point2$ ls /mnt/point2lost+found$ ls /mnt/point1file.img lost+found$ This simple demo makes it easy to understand that the Linux file system (and looping device) is How powerful. You can create an encrypted file system with a looping device on a file in the same way. You can use a looping device to temporarily mount files when needed, which helps protect your data.


File System Architecture

Now that you have seen the file system constructor, let's look at the Linux file system layer architecture. This article examines the Linux file system from two perspectives. First adopt the perspective of a high-level architecture. Then a deeper discussion will be presented to introduce the main structure of the file system layer.


High-level architecture

Although most file system code is in the kernel (except for the user space file system discussed later), the architecture shown in Figure 1 shows The relationship between user space and the main components of the kernel that are related to the file system.


Figure 1. Architecture of Linux file system components

User space contains some applications (for example, users of file systems) and GNU C libraries (glibc) They provide user interfaces for file system calls (open, read, write, and close). The system call interface acts like a switch, sending system calls from user space to the appropriate endpoints in kernel space. VFS is the primary interface to the underlying file system. This component exports a set of interfaces and then abstracts them into individual file systems, and the behavior of individual file systems can vary widely. There are two caches (inode and dentry) for file system objects. They cache recently used file system objects. Each file system implementation (such as ext2, JFS, and so on) exports a common set of interfaces for use by VFS. The buffer cache caches requests between the file system and related block devices. For example, read and write requests to the underlying device driver are passed through the buffer cache. This allows caching of requests, reducing the number of accesses to physical devices and speeding up access. The buffer cache is managed in the form of a recently used (LRU) list. Note that you can use the sync command to send requests from the buffer cache to the storage medium (forcing all unwritten data to be sent to the device driver and sent to the storage device).


What is a block device?

Block devices are devices that send and receive data in blocks (such as disk sectors). They support buffering and random access (you don't have to read blocks sequentially, but you can access any block at any time). Block devices include hard disks, CD-ROMs, and RAM disks. Opposite to block devices are character devices, which have no media that can be physically addressed. Character devices include serial ports and tape devices that can only read data from these devices character by character. This is the high-level case of VFS and file system components. Now, discuss the main structure that implements this subsystem.


Copyright © Windows knowledge All Rights Reserved