File System and wardrobe theory-an initial understanding of indexed file systems. Beginner version. Please confirm it!

Source: Internet
Author: User

After reading the Linux File System in laruence's private dish, write down what I understand. A lot of questions are still unknown. We hope to have a deeper understanding in the future.

I personally think that the operating system stores and manages data in a file unit when managing data. (This is just what I think. please correct me)

File systems, of course, are used to manage files. In addition to the actual content, operating system files also contain many attributes. For example, in a Linux operating system, file permissions (rwx) and file attributes (u, g, o, and time parameters ).

If you want to design how to manage files. Maybe you have the same idea as you initially did. Directly store the data of a logical file on the disk. Like a dirty boy, his clothes are packed in a cupboard. As long as there is space in the cupboard, clothes can be stuffed. In this way, the actual content of the file is put down. What about the attributes attached to the file? Maybe you will think the same way as I did. Write all the attributes of a file in the beginning of the actual content of the file in the agreed format and characters. There is another problem. How do we know where the file starts and ends? The data is not really like a dress. You can pull the whole dress by holding a part of it. Maybe you think about it again. We can show it in the file. Well, let's summarize the initial and original file system features.

A file contains the following content:

A special file flag. (How do you identify whether it is a file or how many files are on the disk ?)

The start and end of the file.

A bunch of attribute flags.

The actual content of the file.


The above is our file system. Let's try again. If the clothes in your cupboard are like this, they are put together. Do you have to flip other clothes you don't need. (Because your clothes are put together, maybe the clothes you are looking for are put at the bottom .) It would be too slow for a computer to access the data in the above file system to find the desired file without going through some files that do not need to be accessed. The total number of files on the disk is more than the number of clothes in the wardrobe! This is intolerable.


A wise hostess may manage his wardrobe like this. The general wardrobe is divided into several areas (we divide the disk into several areas ). Every time I put a dress in, I wrote down the place where the dress was put, the space in the area in the cupboard, and the clothes I could put. At this time, the hostess had forgotten what clothes were placed in her mind and how much space she had to remember. Of course, human memory is not omnipotent. Next time you put a dress in, the hostess needs to observe that there is enough space in the cupboard to put the dress in her hand. Put down your clothes and remember the situation in the cupboard. If you are in a mood, you can take notes about clothes and places. After all, human memory is not always reliable.

In this way, the efficiency of the hostess's clothes is greatly improved. Because the hostess remembers where to put the clothes, just take them. What clothes are stored in the head of the hostess can be called an index.


Indexed File SystemThe same is true. The file system divides the disk space managed by itself into two parts:InodeBlock (with many inode ).BlockBlock (there are manyBlock).


Block: Record the actual content of a file. If a file is too large, it will occupy multiple blocks.

Inode: Records various attributes of a file, and records the block number of the actual content of the file. A file corresponds to an inode.


In this way, you only need to find the corresponding inode to access the file, and then find the corresponding block based on the block number recorded by the inode to find the actual content of the file. Similar to the way the hostess looks for a dress, she first thinks in her mind where it is placed in the cupboard, and then takes the dress directly.

But there is another problem. When the hostess puts the clothes in the cupboard, she only needs to use her eyes to see if there is not enough room in the cupboard to put down the clothes in her hands. But how does the file system know that there is no space for new files? In this case, the third block is called the super block to record whether there is any location.

Super Block: Record the overall information of the file system, including the total inode/block, usage, remaining amount, and file format and related information.

In this way, you can go to the super block to check whether there is any spare inode or block for every reassuring file.

In this way, the file system divides the disk space managed by itself into three parts:Super block, inode block, block Block.

Note that there are many inode blocks and many blocks in the blocks. Each inode and block is numbered. In the system, only one inode is used for not placing a new file or creating a new file. The data recorded in inode is the block number of the actual content of the file. If the file is too large to fit a block, it will be placed in multiple blocks. In this way, the inode file records multiple block numbers. When accessing a file, you only need to find the inode corresponding to the file to know the block number of the actual content of the file. Of course, you can read the actual data of the file. Because you only need to find the inode in the inode block when searching for files, the efficiency is greatly improved.


Another problem is that files are stored in the system at the beginning, but they are all stored in continuous blocks. However, some files are deleted after a while, leaving some blocks in the middle with no content, the adjacent block contains the content. In this way, there will be some gaps. Just like a few blank clothes are often taken out of the closet but not sorted out. If I want to put another dress in the closet, if the dress is smaller than a blank one, I will put it in the empty one. The same is true for the system. If a new file is put, the system first checks which gaps in the block are large enough to put the new file. If it is found, put it. If there is no gap larger than the file, but there are some small gaps, the actual content of the file will be divided into several blocks in different intervals. (In this way, the files become discrete, which will affect the access efficiency. I will explain how to solve this problem later. If the gap is small, the files that you put later cannot be used will also cause a waste of space .) Of course, when we put our clothes in the closet, we won't tear them into a few pieces and put them in a different gap (maybe the idiot will do this ).

If you have a large cotton jacket that must be placed in the Cabinet, and there is no gap in the cabinet, you can only put a pair of underpants. Maybe these gaps can be put down. What do you do at this time? As mentioned above, we cannot tear the cotton jacket into several small pieces. What we often do is to re-set all the clothes in the cupboard. Maybe all you have to do is push all the clothes from the left to the right, and the gap is gone, you can leave a large gap on the left to put it in a cotton jacket. As mentioned above, files that are too discrete will affect access efficiency. The wardrobe can be organized, and the file system is the same. Move the block corresponding to the file and try to eliminate the gap. Updates the records in the inode block at the same time. In this way, you can leave a larger space to put more files and improve file access efficiency. This is actually calledDisk fragmentation.


The above is just my temporary understanding. In fact, I have explained what inode and block are. In my mind, this is related to disk partitioning and formatting. This also involves the minimum disk space units that can be used by the system.


I will modify it later.


It is a great honor to make a brick!

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.