How to count the usage of metadata in a Linux File System

Source: Internet
Author: User
Article Title: Counting mechanism for metadata in Linux File Systems. Linux is a technology channel of the IT lab in China. Includes basic categories such as desktop applications, Linux system management, kernel research, embedded systems, and open source.

In Linux File Systems, the reference count of metadata is mainly used to manage the creation, use, and release of metadata (such as inode and dentry structures) in memory. Understanding this mechanism helps you gain a deep understanding of the operating mechanism of the file system and how Linux manages metadata in memory. This part of the content is also necessary to build a distributed file system, so as to ensure the correct use of metadata in the distributed file system.

Overview

Metadata is an important part of a file system. Many books and articles have introduced the functions and mechanisms of dentry and inode in Linux, but few articles have mentioned their usage counter ). The mechanism of counting seems simple: when a metadata is used, it increments and when it is used up, it decreases. But after a simple description, how is the specific process implemented? This actually runs through the entire metadata operation and metadata management in the memory. Understanding this part of the mechanism is a very interesting process. You can see the rigorous and meticulous thinking of Linux and have a deep understanding of the operating mechanism of the Linux File System. This part of content is also necessary to build a distributed file system.

This article also introduces the use of Count: increase and decrease. Finally, let's look at the changes in the distributed environment.

The Code referenced here is based on the Linux kernel 2.6.20.

Increase in use count

Create operation

Metadata creation can be divided into File Creation and Directory Creation. Both files and directories correspond to the same metadata structure, and inode and dentry are available in the memory.

Next, let's take a look at the two main creation operations: creating a file and creating a directory.

(1) create a file

Create a file by calling sys_open () and setting the O_CREATE flag. The calling process is as follows:

 
sys_open() > do_sys_open() > do_filp_open() > open_namei()

In open_namei (), the dentry and inode structures are created. First look at the dentry path:

 
open_namei() > lookup_hash() > __lookup_hash()
There are three cases:

● Query in dcache: _ lookup_hash ()> cached_lookup ()> d_lookup ()> _ d_lookup ()
● Allocate a new dentry: _ lookup_hash ()> d_alloc ()> atomic_set (& dentry-> d_count, 1 );
● Search for _ lookup_hash ()> I _op-> lookup () in a specific File System ()
We will introduce the search-related content later. Here we only look at the creation, that is, d_alloc (). It will allocate a new dentry structure. During the allocation process, the usage count of dentry is initialized to 1. In d_alloc (), the usage count of the parent directory is increased by using the dget () function to prevent the parent directory from being deleted before the dentry is deleted. (Except "/", it does not have a parent directory ):

 
d_alloc() > dget(parent) > atomic_inc(&dentry->d_count);

Let's look at the inode path again:

 
open_namei() > open_namei_create() > vfs_create() > i_op->create()

The create function of the specific file system will be called. Taking Ext2 as an example, the call process is as follows:

 
ext2_create() > ext2_new_inode() > new_inode() > alloc_inode() > atomic_set(&inode->i_count, 1);

When an inode structure is allocated to a specific file system, the inode's I _count field is set to 1 through initialization. At the same time, the I _nlink field of inode is also set to 1, which indicates the number of hard links of inode, and its value will be written to the disk of a specific file system.

To sum up, the creation operation will establish the dentry and inode structures in the memory, and initialize their usage count to 1.

[1] [2] [3] [4] Next page

Related Article

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.