【 learn to understand 】 the interpretation of the file system

Time:2019-10-23

Speaking slowly according to my personal understanding, the order may not be very smooth, but I try to speak smoothly.
Comb oneself train of thought first, arrange format slowly again. First think about how to write the context of the article
How slow it is to write an article!!!
Write about the structure of the context, and then organize the text. Finally, organize your own language.
Original intention: is comb oneself understanding, make oneself explain better, but write to write method, or other people’s train of thought this is really bad, this can only show that understand or not deep, strengthen understanding. Some redundant explanations can be removed

Writing ideas, solving problems with problems

1. Source of the file system

The storage medium with large capacity in the computer is:disk. (the introduction to disks is not extended here.)

The so-called file system, according to our relatively easy to understand the concept of file (seems to see), its corresponding structure and data stored on disk.

As we did when we installed the file system, there are a few simple steps to save the contents of the file system to disk.

  1. Initialization, partition
  2. Format with the file system
  3. mount

Steps 1, 2, and 3 alone are always confusing, but look at the work done at each step, and the resulting system format. So the inevitability of each step is obvious.

The first step: you can add a removable hard disk to the virtual machine and then simulate the partition

【 learn to understand 】 the interpretation of the file system

After the partition

【 learn to understand 】 the interpretation of the file system

The second stepFile systems have many different file formats

Preview can be done using the command MKFS

【 learn to understand 】 the interpretation of the file system

Select the more commonly used ext4 for formatting partitions

【 learn to understand 】 the interpretation of the file system

Note: it is especially important to see the above prompt after the ext file system formats partition sdb1

Important remarks are as follows:

  1. 261888 4k blocks
  2. 65536 inodes
  3. superblock
  4. inode table

Because it’s the basic structure of the file system that determines how files are read and written and used, and we’ll talk about the concepts here as we go along.

Step 3:: mount to a different directory

【 learn to understand 】 the interpretation of the file system

Then the operation on MNT in the mount folder is the same as the operation on disk.

If the current inode has mount information, its own file functionality is ignored. This is also true when looking up files, as explained later

Error tips

  1. Mount directly if not formatted

【 learn to understand 】 the interpretation of the file system

If according to the order of the structure to introduce, will feel a sudden to the people confused, make confused. To illustrate briefly why we do this, let’s first take a look at what we do with the file system, and then loop through incrementally introducing different terms and concepts

Terminology is really the key to communication between different programmers and can reduce a lot of communication deviation costs.

2. What is a document?

View: files and folders that we see on our computers or clients (as mentioned earlier, they are stored on disk in some form)

Development use: the user through the system call, to carry outread, write

We know that disks are saved through sectors00011These bytes hold data, and the smallest unit of disk is the sector.

Essentially, we read or write data by selecting the Noxxx sector of the fixed track

2.1 information content of the file

How does the disk save files? What is the format of the file? How does he read? With these questions in mind, let’s look at the details of the document.

First according to our daily habits, enter a directory, view a file details

ls -ahl -i

【 learn to understand 】 the interpretation of the file system

First column: for example262547, is the file’s inode.
We use inodes to find file information and file contents.

2.2 create a new file

How the file system records files when it creates a new file, such as hello.txt.

【 learn to understand 】 the interpretation of the file system

The figure above is divided into two parts. The first part is inodetable and the second part is data.
We select a free address through the inode table, generate an inode node, then store the data in the data layer, and then write the address back to the structure of the inode.

For example, the table will save the sector address number corresponding to the file inode node
(the above figure is not correct for better illustration and understanding.)

When querying a file from the above image, we can locate the fixed sector and read the contents through the disk trace

2.3 block

Different file systems have different sector sizes, some 512b, some 1024b, and so on. In order to unify, a middle layer block is introduced
For example, block size is defined as 4k, and different file systems and sectors of different sizes can be filled with different Numbers.

For example, use 4 sectors of 1024B and 8 sectors of 512B to achieve. This ensures that each block is the same size.

【 learn to understand 】 the interpretation of the file system

The smallest unit of a disk is a sector
The smallest unit of a file is: block

【 learn to understand 】 the interpretation of the file system

So that’s how many blocks are there after the partition.

2.4 storage mapping

【 learn to understand 】 the interpretation of the file system

As shown in the figure, when we look at the file contents, we follow certain rules to find the file’s inode, and then retrieve the file’s data through the inode.
Large files, possibly in n blocks. (for example, a 4G mv may require many blocks)
Small files, with a block to save.

The sectors associated with the block are the contents of the record file.
(if locality is better utilized, and certain elevator algorithms are followed) start rotating tracks through the recorded sectors in order to read the data into memory.

2.5 what information is recorded in the inode

【 learn to understand 】 the interpretation of the file system

【 learn to understand 】 the interpretation of the file system

【 learn to understand 】 the interpretation of the file system

Linux looks up the file’s storage location and file information based on the inode

【 learn to understand 】 the interpretation of the file system

2.6 inode table

【 learn to understand 】 the interpretation of the file system

Find an idle inode, store it in the block, and write the address to the inode

Each file needs an inode node to describe it

3. super block

Using stat, we can see how files are being used.
However, when we create a new file, we need to allocate blocks corresponding to inode and data addr

How do they know that there are free inodes and blocks in their system?

So The Times require a field, or structure, to record the inode and block usage of the current file.

Let’s first trace the topic back to disk formatting and see what they did.

【 learn to understand 】 the interpretation of the file system

Structure of disk:

MBR: records some information about the startup

Structure of disk partition:

  1. boot sector
  2. Ext file system

Different partitions can use different file systems

【 learn to understand 】 the interpretation of the file system

In the file system, super block is used to record the usage information of the whole file system.

In each block group, the block bitmap and inode bitmap are used to record the usage of inode and data within the group.
Easy to distribute and manage.

First find a free storage file from the data block, and then find a free node from innode table to describe the file’s address and properties
, the address to store the data block

So when you create a new file, these two locations will change.

Super block: I mentioned it specifically when I was partitioning disks.

【 learn to understand 】 the interpretation of the file system

At this point, let’s look at the storage of files on disk as a whole

【 learn to understand 】 the interpretation of the file system

How do we find a file, and we know the directory before we find the file path

4. Directories and directory entries

4.1 introduction of problems

The inode information holds all kinds of information of the file. For details, we can take a look at the fields in his struct.

We use inodes to find files.

However, the inode does not save the file name, and it is customary for users to use the file name to find the contents of the file.

4.2 directory

【 learn to understand 】 the interpretation of the file system

【 learn to understand 】 the interpretation of the file system

The datablock file holds data and the directory holds tables

If you view a file, follow path
First, look for the root directory, the inode, which contains the table.

The /bin address is 6029313. The inode number
The inode table finds the corresponding inode node including the name of the file.

The inode has one constraint 2: it must be the root inode
1: generally means that the file is reserved for initialization

5. The file path

Relative path of file:

【 learn to understand 】 the interpretation of the file system

Folder of. And..

Corresponds to the hard link of the current directory and the hard link of the parent directory, respectively.

It then works its way back through the block table in the parent directory

6. A mount

【 learn to understand 】 the interpretation of the file system

The parent file system is a system on a computer disk

Subfile system: can be a U disk mobile hard disk file

6.2 how to access a usb drive file

【 learn to understand 】 the interpretation of the file system

Address path from the root directory to the fixed inode, and access the file if the edit is unmounted.
If marked as mounted, go to the hash table for the mounted vfsmout information to find the root directory of the mount point
Then, in turn, find the files under the path

This also explains why, after a folder directory has been mounted, previous files cannot be accessed and read or written to

7. VFS

【 learn to understand 】 the interpretation of the file system

【 learn to understand 】 the interpretation of the file system

【 learn to understand 】 the interpretation of the file system

【 learn to understand 】 the interpretation of the file system

8. File descriptor

How does a file dynamically manipulate a file

【 learn to understand 】 the interpretation of the file system

📚 can refer to the operating system, the concept of structure

Fd is the handle that kernel space returns to user space

【 learn to understand 】 the interpretation of the file system

【 learn to understand 】 the interpretation of the file system

8.2 a process open file

【 learn to understand 】 the interpretation of the file system

Fd is more of an array offset

9. Hard links and soft links

In the process

【 learn to understand 】 the interpretation of the file system

You already know how files are stored on disk

  1. Hard links

Same inode and data block

【 learn to understand 】 the interpretation of the file system

  1. Soft links – symbolic links – shortcuts

【 learn to understand 】 the interpretation of the file system

Soft link itself is a file, the file content to save a file address path

【 learn to understand 】 the interpretation of the file system

A hard link is equivalent to an alias

【 learn to understand 】 the interpretation of the file system

Soft link content: is a path name

9.2 differences between the two

【 learn to understand 】 the interpretation of the file system

Hard links cannot span partitions

Different inodes exist for different partitions

Soft links can span because they store the contents of a file, which is a path name

Hard links cannot log directories
Directory. Itself is a hard link
The execution file is messy when creating a hard link again. So hard links to directories are prohibited

10. Review file usage stat

【 learn to understand 】 the interpretation of the file system

Du is to go into the crowd and see the file size of each directory
Du is used to calculate the size of each directory

Salute 100.

Teacher wang litao’s explanation is really very good

If you have seen this article, I feel that what I said is not clear, you can go to watch teacher wang’s wonderful explanation

Session 01: demystify the file system video