The buffer mechanism in Linux File Management-general Linux technology-Linux programming and kernel information. The following is a detailed description. 1.1.VFS index node Cache
When searching and installing a file system, its VFS index nodes are constantly read or written. The Virtual File System maintains an index node cache to accelerate access to all installed file systems. Each VFS index node can be read from the index node cache to accelerate access to physical devices. The cache of the VFS index node is a hash table with the VFS inodes linked list pointer entry. Each hash table contains a pointer to the VFS index node, which is at the beginning of the chain. The index node of the same linked list has the same hash value, which is obtained based on the index node and the identifier of the physical device where the file is located.
When VFS wants to access a node, first view the VFS index node cache and find the corresponding hash_table by calculating the hash value.
In the index node linked list, find the index node that matches the specified device number and index node number. If the accessed node is in the index node cache, the access count I _count is added with 1; otherwise, an idle VFS index node needs to be found so that the file system can read the node from the memory.
VFS has many options for obtaining idle index nodes. If the system can allocate multiple VFS index nodes, perform the following steps: first assign a physical page, divide it by the length of the index node, and put it in the index node linked list. If the system already has all the index nodes, you must find the nodes that are easy to reuse, that is, the node count I _count is 0, which is not used. The count field of an important VFS index node, such as the root node of the file system, is always greater than 0, so the index node it uses cannot be reused.
After obtaining an idle VFS node, VFS will call the proprietary routine of the file system to read information from the logical file system and fill in the index node, so that the node count is 1 and the node is locked at the same time, to prevent other processes from accessing the node.
1.2.VFS directory Cache
To accelerate access to common directories, VFS maintains a Directory Entry buffer.
When the actual file system looks for a directory, the details about this directory will be stored in the directory cache. When searching for this directory again, such as listing file names or opening files under this directory, this information can be found in the directory cache. Because directories with short directory names are the most frequently used, in actual implementation, only short directories (up to 15 characters) are cached. The directory cache is managed by a hash table, and the hash index value is calculated by the device number and directory name of the file.
To ensure the validity and timely update of the Directory cache, VFS stores a directory cache linked list that is least recently used (LRU. When this directory item is searched for the first time, its directory item is put into the cache for the first time and added to the end of the first level LRU linked list. If the buffer zone is fully occupied, it replaces the header of the LRU linked list. When this directory item is used again, it will be placed at the end of the second-level LRU linked list. At this time, you need to replace the header of the second-level LRU linked list. The directory items on the front end of the linked list indicate that they have not been accessed. If they are accessed, they are located near the end of the linked list. The items in the second-level LRU linked list are safer than those in the first-level LRU linked list.
The directory cache operation function is defined in fs/dcache. c.
. Buffer cache
Most file systems supported by Linux organize files in blocks. To reduce access to physical block devices, after files are transferred to the memory in blocks, you can use block buffers to manage them. Each block buffer contains a buffer header (represented by the data structure buffer_head) and a buffer space for storing data. The buffer header is not connected to the data area, and the data area is stored independently. The two are linked by a pointer * B _data.
Read/write requests of all data blocks are transmitted to the device driver in the form of a buffer_head data structure.
The buffer cache consists of two parts: first, the idle block buffer linked list, which provides a linked list for each supported block size, in addition, the free block buffer in the system is linked to this linked list when it is created or discarded; the second is the cache itself, which is a hash table. Each entry is a pointer to a buffer linked list linked by pointers. The hash index value is generated by the device identifier and block number of the data block.
The block buffer is in the idle linked list or in the hash buffer cache. If they are in the buffer cache, they are arranged by the LRU linked list. There is an LRU linked list for each buffer type.
When the file system needs to read a buffer block from its underlying physical device, it first looks for it in the buffer cache. If no buffer is available in the buffer cache, a clean node is obtained from the idle linked list of the proper size, and the new buffer is added to the buffer cache. If the required buffer is in the buffer cache, it may already contain the latest data. If it is not updated or is a new block, the file system must request the corresponding device driver to read the data block from the disk.
To make the buffer cache run more effectively and allocate cache entries reasonably among Block devices using the buffer, Linux uses the bdflush monitoring program to perform cache monitoring. Bdflush is a simple core background process that provides dynamic responses to too many dirty buffer systems. These buffer blocks contain data that must be written to the hard disk. Generally, this process remains dormant until the number of dirty buffers in the system increases to a certain number. When a buffer is allocated and discarded, the system checks the number of dirty buffers. If a percentage is exceeded (60% by default), The bdflush process is awakened. However, if the system urgently needs a buffer, bdflush may be awakened at any time. This field value can be modified using the update command. When the data is written to the buffer to make it dirty, all dirty buffers are linked to a BUF_DIRTY LRU linked list. bdflush writes the appropriate number of buffer blocks (default value: 500) to the disk.
As for update, it is not just a command, but also a background process. When running as a Super User (during system initialization), it periodically calls the system service routine and clears the old dirty buffer content to the disk. This job is similar to bdflush. After a dirty buffer completes this operation, it will mark the time that should have been written to the respective disk in the buffer_head structure. During each update operation, all the dirty buffers in the system will find the buffers that have been washed for more than a certain period of time. These expired buffers will be written to the disk.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service