An article to understand the block device Linux operating system

Block device is stored in "blocks" of data stored in units of the device, typically a magnetic disk device, optical disk, or USB. This paper focused on the analysis of the content of the disk device, very similar to other types of devices, not being introduced.

In the Windows operating system disk device seems to be a real device, we can manage the disk devices through a graphical interface. Figure 1 is a disk management interface under Windows, you can clearly see the disk devices through this interface, and can format and other operations.

An article to understand the block device Linux operating system

Figure 1 Windows disk device

Linux operating system disk devices are not intuitive, the LInux system under the "everything is a file" concept, disk device is actually a file , but is a special file. 2 is a file path and some disk partitions, wherein the yellow font path is a disk portion (similar to a file path), and b is in front of the red block indicates the file is a disk file apparatus, instead of an ordinary file.

An article to understand the block device Linux operating system

The following diagram disk device 2 Linux System

Disk device is located in the VFS file (virtual file system) Next, the file system and other similar Ext4 (refer to FIG. 3) . Users can access the disk with a level interface (API) to access ordinary files. The following code is to write a program string to disk implemented in Python. Code is very simple, is to open the path to the disk where the (path), then call the write function to write data.

An article to understand the block device Linux operating system

 

The nature of Linux system disk

Through the above description we know that for the Linux operating system, the disk is a file . The disk itself is a linear memory space (can be understood as a large array), and files this way is very similar. In view of the similarity of the above, Linux disk device abstraction of a document and there is no improper.

In essence, the Linux operating system based on a disk device is called pseudo file system bdev of managed, bdev file system is a pseudo file system in memory (memory file system, no persistent data), location Ext4 file system is the same and so on. As shown in FIG. 3, the file system location bdev red region of FIG.

 

An article to understand the block device Linux operating system

图3 bdev文件系统位置

理解了块设备的管理方式,再结合我们之前对文件系统的相关介绍,这样就很容易理解后续的内容了。在文件系统相关文章介绍中我们知道,不同文件系统数据处理的关键是其提供的函数集,而这个函数集是在打开文件的时候确定的。磁盘设备也是如此,当我们打开磁盘设备时,操作系统根据磁盘设备的特性,会初始化inode中的函数集。而后续对该磁盘设备的读写操作就能通过该函数集完成。如下代码所示 ,块设备连同字符设备和管道都作为特殊的文件进行处理,并初始化对应的函数集。

An article to understand the block device Linux operating system

 

完成函数集的初始化后,当用户调用VFS层的接口时,VFS层就可以找到具体的处理函数,进而完成用户的操作。这里的函数集与本地文件系统的函数集别无二致,差异在于普通文件系统需要管理目录和文件,而bdev伪文件系统是将磁盘看作一个大文件,更简单一些

磁盘的缓存

既然磁盘伪文件系统bdev本身也是一个文件系统,因此自然也可以有缓存。这个缓存就是用于提升磁盘性能的缓存系统。磁盘的缓存系统与文件系统的缓存系统类似,也是通过页缓存来实现的。当然,Linux磁盘的缓存是可以关闭的,此时将调用另外一套函数集。

这样说起来可能比较抽象,下面我们以一个具体的例子来看一下磁盘缓存的具体实现。如下是磁盘伪文件系统的函数集,我们以写数据为例进行介绍。

 

An article to understand the block device Linux operating system

图4 磁盘函数集

写数据的函数为blkdev_write_iter,该函数会调用generic_perform_write函数。如果大家阅读过本号关于文件系统的文件的话,很清楚后者就是VFS中向页缓存写数据的函数。也就是说块设备伪文件系统的逻辑与本地文件系统完全一致。缓存部分点到为止,具体代码请大家自行阅读。

Well, today we process principle of Linux and its cache of disk devices were introduced . We then follow step by step details of the various properties.

Guess you like

Origin blog.csdn.net/shuningzhang/article/details/91953146