Linux file name resolution processing source Analysis

Source: Internet
Author: User
Tags goto key string

Objective

When working on a file in Linux, it's important to parse the file name, find the Inode object for the file, and then create a file object that represents the files. Here, the file name parsing process, and how to find the corresponding inode process for source code analysis. The parsing code is based on the Linux-3.2 version.

Critical function Analysis

Whether it is through the application Layer API functions or open a file in the kernel, the final call to the Filp_open function, the function's primary responsibility is to parse the file name, find the file corresponding to the Inode object, and then allocate memory to create a file object, and finally execute the corresponding file- >open function.

The core processing function of Filp_open is Path_openat, which is analyzed as follows:

static struct file *path_openat (int dfd, const char *pathname, struct nameidata *nd, const struct OPEN_FLAGS  
    p, int flags) {struct file *base = NULL;  
    struct file *filp;  
    struct path path;  
    int error;  
    /* Create a File Object */Filp = GET_EMPTY_FILP ();  
     
    if (!FILP) return err_ptr (-enfile);  
    Filp->f_flags = op->open_flag;  
    Nd->intent.open.file = FILP;  
    Nd->intent.open.flags = Open_to_namei_flags (Op->open_flag);  
    Nd->intent.open.create_mode = op->mode; /* Initializes the starting directory of the search, determining whether the starting directory is the root or current directory, and initializing the Nd->inode object to prepare for the parsing of the Link_path_walk function. * * error = Path_init (DFD, pathname, Flags |  
    Lookup_parent, ND, &base);  
     
    if (unlikely (error)) goto OUT_FILP;  
    Current->total_link_count = 0; /* Key string resolution processing function, the core idea is a hierarchical parsing string, through the corresponding directory entries to find the next level of the Inode node. The specific analysis of the function is as follows.  
    * * error = Link_path_walk (pathname, ND);  
    if (unlikely (error)) goto OUT_FILP; /* Do_laThe ST function creates or obtains the corresponding Inode object for the file, and initializes the file object, which represents the memory object that opens the file Filp birth/Filp = Do_last (nd, &path, op, pathname);  
        while (unlikely (!FILP)) {/* trailing symlink/struct path link = path;  
        void *cookie; if (!) (  
            Nd->flags & Lookup_follow)) {path_put_conditional (&path, ND);  
            Path_put (&nd->path);  
            Filp = Err_ptr (-eloop);  
        Break  
        } nd->flags |= lookup_parent; Nd->flags &= ~ (lookup_open| lookup_create|  
        LOOKUP_EXCL);  
        Error = Follow_link (&link, ND, &cookie);  
        if (unlikely (error)) Filp = err_ptr (Error);  
        else Filp = Do_last (nd, &path, op, pathname);  
    Put_link (ND, &link, cookies); } out:if (Nd->root.mnt &&!) (  
    Nd->flags & Lookup_root)) Path_put (&nd->root);  
    if (base) fput (base);  
    Release_open_intent (ND); ReturnFILP;  
    OUT_FILP:FILP = err_ptr (Error);  
Goto out; }

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.