Linux File System Analysis

Source: Internet
Author: User

LinuxFile System Analysis

 

Author: TP Writing Date ~ 2011-09-25

 

I 've been struggling since I was asked in my last interview about designing a file system. What kind of Data Structure does a file have to express them? How does our operating system manage them? What kind of thinking is there during the design of the file system by the elders? How is it gradually evolved into today's stable and scalable architecture.

 

After checking the documents for a period of time, I have a little bit of knowledge about the file system. I would like to share it with you.

 

Linux supports many file systems, such as ext2, ext3, and NTFS. However, this article discusses the Virtual File System (VFS, sometimes called the Virtual File System switch) in the Linux kernel and introduces the main structure of connecting the file system.

 

1.What is a file system?

 

A file system is a logical method for an operating system to store and retrieve data on a computer's hard disk. It is essentially a special hierarchical data storage structure that contains files, directories, and related control information.

Figure 1.

The architecture shown in Figure 1 shows the relationship between the user space and the main components related to the file system in the kernel. VFS is the main interface of the underlying file system. This component exports a set of interfaces and abstracts them to various file systems. The behavior of each file system may vary greatly. There are two caches for File System Objects (inode and dentry ). They cache recently used file system objects. The interaction between the buffer cache and the device driver, and the system interfaces provided by VFS are not discussed at the moment. Let's look at the main structure of implementing this VFS subsystem.

 

2.Main Structure of the file system

VFS has four main object types: superblock, inode, dentry, and file. A super block represents the installed file system, and an index node represents a file in the file system. Another group of structures is called dentry, which is used to map names and inode. A directory cache is used to save the recently used dentry. Dentry also maintains the relationship between directories and files to support moving in the file system. Finally, the VFS file represents an open file (Save the status of the opened file, such as the write offset ). The relationships between the four of them are shown in Figure 2:

 

Figure 2.

As shown in figure 2, the super block describes and maintains the state of the file system on the root of each file system. Each object (file or directory) managed in the file system is represented as an inode in Linux. Dentry is the path that points to inode. It represents the inoing between the file name and inode. At the same time, different file names can be mapped to the same inode. The following is a simple explanation of these objects:

 

A. Directory item objectDentry:The Chinese name of dentry is a directory, which is a link to an index node (inode) in the Linux File System. This index node can be a file or a directory. To facilitate search operations, the Virtual File System introduces the concept of directory items. Each dentry represents a specific part of the path. For path/bin/VI, both bin and VI belong to directory objects. The first two are directories, and the last is a common file. Because parsing a path and querying its components is a string comparison process and a time-consuming process, the file system will introduce the directory Cache Technology to load some directories to the memory in advance. The structure of dentry is as follows:

Struct dentry {
Atomic_t d_count; // counter used by the Directory item object
Unsigned int d_flags; // directory entry flag
Struct inode * d_inode; // The index node associated with the file name
Struct dentry * d_parent; // directory item object of the parent directory
Struct list_head d_hash; // pointer of the hash table item
Struct list_head d_lru; // a pointer that does not use a linked list
Struct list_head d_child; // pointer to the linked list of the Directory item object in the parent directory
Struct list_head d_subdirs; // For a directory, it indicates the linked list of the sub-directory item object.
Struct list_head d_alias; // linked list of the related index node (alias)
Int d_mounted; // indicates the root entry of the installed file system.
Struct qstr d_name; // file name
Unsigned long d_time;/* used by d_revalidate */
Struct dentry_operations * d_op; // directory item method
Struct super_block * d_sb; // super block object of the file
Vunsigned long d_vfs_flags;
Void * d_fsdata; // file system-related data
Unsigned char d_iname [dname_inline_len]; // Save the short file name
};

In this struct, describe the following Members because they constitute the framework of directory items.

D_inode: it directly records the pointer of the index node related to this name.

D_parent: pointing to Father dentry

D_hash: Hash list, which can quickly resolve a given path to a related directory item object.

D_child: The linked list of directory item objects in the parent directory

D _ subdirs: subdirectory

 

B. Index Node objectInode:An index node represents a file in the system. It contains all the information required by the kernel in the operating file security or directory. As the data structure of the index node is relatively large, several major members are explained here:

Struct inode {
Struct list_head I _hash; // hash list
Struct list_head I _list; // index node linked list
Struct list_head I _dentry; // list of directory items
... ...
Struct inode_operations * I _op; // index node operation table
... ...
};

 

I _hash: Linux retains the cache of active index nodes and recently used index nodes. You can use two methods to access these nodes. The first approach is through dcache. The second approach is to index the node hash. Each index node creates an 8-digit hash Based on the address of the file system superblock and the index node number. Then, information nodes with the same hash value are linked in a dual-link list. Here ihash refers to this hash.

 

I _list: The I _lish link list links nodes in different States. Including inode_in_use list and inode_unused.

 

I _dentry: The identry list is a list of all the dentry structures that reference this information node. That is, different file names or paths are mapped to the same file.

 

C. Super block objectSuper_bolck:A super block structure represents a file system. It contains the information required to manage the file system, including the file system name (such as ext2), file system size and status, block device reference and metadata (such as idle list and so on ). A super block is usually stored in the storage media. If the super block does not exist, you can create it in real time. The data structure of the super block is in linux/fs. h. Code for creating, managing, and destroying super block objects is in file fs/super. c. The super block object is created and initialized through the alloc_super () function. During file system installation, the kernel will call this function to read the file system super block from the disk and fill in the information to the super block object in the memory.

 

In any case, although understanding the linux File System does not have a direct relationship with the existing work. But I think it is an absolute classic instance of scalable and scalable architecture. If you compare the Linux kernel to a programmer's classic source code masterpiece, the file system structure is definitely a must-have chapter.

 

References: linux kernel design and implementation and linux File System

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.