Linux Disk free space scheduling management __linux

Source: Internet
Author: User

Disk Management for the operating system in conjunction with memory management, is also the disk partition for the smallest unit of the unified scheduling, and memory of the page frame, Disk Management module with disk block as the smallest unit management disk (common disk block is 1KB, corresponding to 2 512B sectors, disk block is the OS concept, Disk-driven reading is a sector as the smallest unit.

1. Array table and list management

Inherited memory page Frame of the paragraph-page management idea, natural can think of disk Management of free space table method and free block chain method. The former refers to creating an idle table for all the free areas, each of which represents the number of the free area, the first disk block number, and the number of free disk blocks. This table manages the disk free area and the dynamic allocation of memory is similar, taking the space size as the criterion, the first applicable matching method, the first applicable algorithm (average disk block appearance opportunity) can be used, and the combined measures may be used to increase an adjacent two free areas to provide a larger free area. It can be expected that this form will be very large (usually the idle table has external memory, multiple, inefficient I/O reads are read into memory over and over again, and variables introduced due to inconsistencies in the size of the idle areas cause the need to increase traversal of idle table entries to match size requirements during the allocation process. There is no doubt that there is no small design burden for disk Management.


Fig.1 Free Space Array table

The idea of Space block list management is to set aside some fixed bits in each free block to point to the next free disk block. The idea is of course to add additional auxiliary structures without having to operate however, because if you need to allocate more than one block of free space, you need multiple I/O operations to extract the next block of free, so the efficiency is very low, and is an implicit link, once the disk of an empty block of nextfree_block pointer location failure, It is very likely that the disk is unusable and the robustness is poor.

Fig.2 free space management linked list

2.Bitmap Bitmap Management

In fact, from the above analysis, we can find that the disk block management needs to speed up the index of disk blocks as much as possible, while minimizing the auxiliary space required for the free block management. Considering these requirements, it is obvious that bitmap is a method that has to be proposed (bitmap algorithms are also the basis of Prum filters). Using each bit to indicate the use of a disk block (0 is empty, 1 is occupied, for 1KB disk blocks, 1G disks, a total of 2^20 blocks, you need 128KB of space to describe all disk blocks). But in this case, the disk interacts directly with the OS kernel with disk blocks, need CPU according to bit position information in bitmap to convert to disk block position, increase the calculation burden, at the same time the small pieces to make disk block difficult to appear, which will lead to a file distribution in the disk of various spanned large disk block, increased i/ o operating time (frequent replacement of magnetic number one, replacement track location).

Fig.3 Free space Bitmap

3. Free Block Group link method

In fact, the above methods are too suitable for large file systems, especially the free table method and free list method, will cause the index structure is too long. The Linux kernel is a fusion of two algorithms, the use of "free block first group, groups and groups linked link" Group link method. such as 100 free disk blocks constitute an idle group, and then between groups and groups through the pointer to form a linked list, in each free group, take out the first free disk block to store the next idle group (the total number of free disk blocks, the free group leader disk block number, the current idle group each free disk block number), the leader disk block constitutes a "pointer." As shown in the following figure, for the first block of a free disk chain, the first disk block is actually the details of the next group, and the remaining 99 disk numbers point to the true free disk block, which is the yellow block in the figure.

Fig.4 free space block Group link diagram
Another concept that needs to be mentioned here is the "free disk block dedicated stack" in memory, which is a critical section (critical_section, exclusive, guaranteed to have only one kernel thread at any given time to call the OS's idle management module for free disk block operations) resident memory, Used to manage disk free space scheduling with the Linux Disk Management module. The stack points to the allocation of the first group of the current space block chain at any point in time, and when Linux calls the idle disk scheduling process, the stack loads the topmost free disk block information of the list according to the first pointer of the chain.

fig.5 free disk block special stack usage principle
disk block allocation process
Kernel thread calls the free disk block scheduling process, first to the disk block dedicated stack lock, and then according to the disk block dedicated stack of information and disk capacity allocation disk block, every time a free disk block, then the top of the stack freeblocks_sum minus 1, if assigned to the last, Freeblocks_ Sum=1, that is, the current free disk block group only left the team leader disk block can be allocated, the first group of the team to indicate the free disk block group of information read into the disk block dedicated stack, and then assign the team leader disk block.

Disk Block Recycling process
The disk recycling process is clearly the inverse of the allocation process, the only thing to be explained is the handover between the group and the group. Assuming that the current freeblocks_sum=100, representing the disk block is actually full, if this is the kernel also need to reclaim a new disk block, then the memory of the disk block dedicated stack content into the new disk block, and then empty the disk block dedicated stack, The new disk block is added to the disk block dedicated stack as the leader of the new group, and the Freeblocks_sum is set to 1.

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.