Directory lookups in the file system

Source: Internet
Author: User

A process in Linux identifies a file and passes the filename to the VFS layer, which locates the inode of the file's index node based on its file name for subsequent operations on the file. The process of locating a file index node by file name is called Path lookup. This article is the notes that are recorded when you read the catalogue to find the code, and will continue to be supplemented by continuous learning.

At the beginning of the path lookup, the kernel starts with a particular dentry, and if the pathname starts with a '/', then the starting dentry is current->fs->root; otherwise, the starting dentry is current->fs- >CWD. Root and CWD are a <dentry in struct fs_struct, vfsmount> pairs, Dentry represents a directory object, and Vfsmount represents a file system object.

After the path lookup begins, the kernel starts with the first dentry, finds its corresponding index node, and reads the directory file containing that index node from the disk to get the index node ... , do this repeatedly until the final file is found. In the above lookup process, you must consider the following points:

1, Audit and verify the permissions of the acquired Inode

2, because the directory entry object for the lookup directory is a very frequent and consuming IO operation, the kernel maintains a directory entry cache in memory Directory-entry cache, also called Dcache, to speed up the process of finding catalog entries. It should be noted that from the 2.6 version, dcache through the RCU to reduce the cost of lock competition, specifically see here http://www.linuxjournal.com/article/7124

3, when the path is encountered in a non-file system mount point, you need to extend to the mount point corresponding to the file system to do directory lookup

4, when you encounter a symbolic link in the path, you should also do the corresponding processing

5, if the system calls the operation of a create file, while looking at the same time need to gradually create levels of the directory

Here are some summary of specific implementations

Path lookup Large code most implementations in FS/DCACHE.C FS/NAMEI.C, the Catalog item object struct Dentry is defined in include/linux/dcache.h, and some other structures related to directory lookup are defined in include/ The linux/namei.h.

The pathname lookup is performed by the Path_lookup function, which is invoked as follows: int path_lookup (const char *name, unsigned int flags, struct nameidata *nd); Name is the file path name to parse, flags is a number of flags, ND finds a successful return result of struct nameidata {        struct path      path;  //structure path is the address of the directory item object and the address of the file system object         struct qstr      Last;    //The last component of the pathname         struct path     root;         struct file     *file;         struct inode    *inode; /* Path.dentry.d_inode */        unsigned int    flags;         unsigned        seq;         int             Last_type;         unsigned        Depth;    //The current level of nesting of symbolic links         Char *saved_names[max_nested_links + 1];  //array of paths associated with symbolic links         /* Intent data */         Union {                 struct open_intent open;       } intent;};  

Reference documents:

1,linux2.6 Source Code

2,linux document:./document/filesystem/path_lookup.txt

Directory lookups in the 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.