手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

Linux虚拟文件系统(节点路径搜索)

[日期:2012-02-08] 来源:Linux社区  作者:bullbat [字体:大 中 小]

2.2.1 处理double dot

所谓double dot为访问上层目录

[cpp]
  1. static __always_inline void follow_dotdot(struct nameidata *nd)  
  2. {  
  3.     set_root(nd);  
  4.   
  5.     while(1) {  
  6.         struct vfsmount *parent;  
  7.         struct dentry *old = nd->path.dentry;  
  8.         /*如果已经达到本进程的根节点,这时不能再往上跑了 
  9.         所以保持不变*/  
  10.         if (nd->path.dentry == nd->root.dentry &&  
  11.             nd->path.mnt == nd->root.mnt) {  
  12.             break;  
  13.         }  
  14.         spin_lock(&dcache_lock);  
  15.             /*已经到达节点与其父节点在同一个设备上。在这种情况下 
  16.             既然已经到达的这个节点的dentry结构已经建立,则其父节点的 
  17.             dentry结构也必然已经建立在内存中,而且dentry结构中的指针 
  18.             d_parent就指向其父节点,所以往上跑一层是很简单的事情*/  
  19.         if (nd->path.dentry != nd->path.mnt->mnt_root) {  
  20.             nd->path.dentry = dget(nd->path.dentry->d_parent);/*往上走一层,并且对应用计数加一*/  
  21.             spin_unlock(&dcache_lock);  
  22.             dput(old);/*释放就得目录的引用*/  
  23.             break;  
  24.         }  
  25.         spin_unlock(&dcache_lock);  
  26.         spin_lock(&vfsmount_lock);  
  27.             /*运行到这里,表示已经到达节点就是其所在设备上的根节点 
  28.             往上跑一层就要跑到另一个设备上去了,当将一个存储设备安装到 
  29.             另一个设备上的某个节点时,内核会分配和设置一个vfsmount 
  30.             结构,通过这个结构将两个设备以及两个节点连接起来。 
  31.             所以,每个已经安装的存储设备都有一个vfsmount结构,结构 
  32.             中有个指针mnt_parent指向其父设备,另一个指针mnt_mountpoint 
  33.             指向代表这安装点的dentry结构*/  
  34.         /*保存nd指定的mnt的父mnt*/  
  35.         parent = nd->path.mnt->mnt_parent;  
  36.              /*当前的vfsmount结构代表这跟设备*/  
  37.         if (parent == nd->path.mnt) {  
  38.             spin_unlock(&vfsmount_lock);  
  39.             break;  
  40.         }  
  41.           /*当前设备不是跟设备*/  
  42.           
  43.         mntget(parent);  
  44.         /*指向该设备上的安装点的上一层目录*/          
  45.         nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);  
  46.         spin_unlock(&vfsmount_lock);  
  47.         dput(old);  
  48.         mntput(nd->path.mnt);  
  49.         nd->path.mnt = parent;/*指向上层设备上的vfsmount 结构*/  
  50.     }  
  51.     follow_mount(&nd->path);/*mnt向子节点移动一个*/  
  52. }  
[cpp]
  1. static void follow_mount(struct path *path)  
  2. {  
  3.     while (d_mountpoint(path->dentry)) {/*如果该文件系统已经安装*/  
  4.         /*找到指定的孩子mnt*/   
  5.         struct vfsmount *mounted = lookup_mnt(path);  
  6.         if (!mounted)/*如果没有孩子mnt了*/  
  7.             break;  
  8.         /*递减引用计数*/  
  9.         dput(path->dentry);  
  10.         mntput(path->mnt);  
  11.         path->mnt = mounted;/*找到的mnt作为path的mnt*/  
  12.         path->dentry = dget(mounted->mnt_root);/*递增引用计数,将找到的mnt文件系统的根目录赋给path*/  
  13.     }  
  14. }  

2.2.2 实际的路径搜索工作

[cpp]
  1. /* 
  2.  *  It's more convoluted than I'd like it to be, but... it's still fairly 
  3.  *  small and for now I'd prefer to have fast path as straight as possible. 
  4.  *  It _is_ time-critical. 
  5.  */  
  6. static int do_lookup(struct nameidata *nd, struct qstr *name,  
  7.              struct path *path)  
  8. {  
  9.     struct vfsmount *mnt = nd->path.mnt;  
  10.     /*在内存中寻找该节点已经建立的dentry结构。内核中有个hash表dentry_hashtable 
  11.     是一个list_head指针数组,一旦在内存中建立起一个目录节点的dentry结构 
  12.     就根据其节点名的hash值挂入hash表中的某个队列,需要寻找时则还是根据hash值从 
  13.     hash表着手*/  
  14.     struct dentry *dentry = __d_lookup(nd->path.dentry, name);  
  15.   
  16.     if (!dentry)/*如果没有找到,转向下面*/  
  17.         goto need_lookup;  
  18.     if (dentry->d_op && dentry->d_op->d_revalidate)  
  19.         goto need_revalidate;  
  20. done:/*内存中找到了dentry*/  
  21.     path->mnt = mnt;  
  22.     path->dentry = dentry;  
  23.     /*访问下一个mnt,其实就是现在mnt的子mnt*/  
  24.     __follow_mount(path);  
  25.     return 0;  
  26.   
  27. need_lookup:/*到这里是在内存中没有找到dentry结构*/  
  28.      /*到磁盘上通过其所在的目录寻找,找到后在内存中为其建立起 
  29.      dentry结构并将之挂入hash表中某个队列中*/  
  30.     dentry = real_lookup(nd->path.dentry, name, nd);  
  31.     if (IS_ERR(dentry))  
  32.         goto fail;  
  33.     goto done;  
  34.   
  35. need_revalidate:  
  36.     dentry = do_revalidate(dentry, nd);  
  37.     if (!dentry)  
  38.         goto need_lookup;  
  39.     if (IS_ERR(dentry))  
  40.         goto fail;  
  41.     goto done;  
  42.   
  43. fail:  
  44.     return PTR_ERR(dentry);  
  45. }  
[cpp]
  1. /* 
  2.  * This is called when everything else fails, and we actually have 
  3.  * to go to the low-level filesystem to find out what we should do.. 
  4.  * 
  5.  * We get the directory semaphore, and after getting that we also 
  6.  * make sure that nobody added the entry to the dcache in the meantime.. 
  7.  * SMP-safe 
  8.  */  
  9. static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)  
  10. {  
  11.     struct dentry * result;  
  12.     struct inode *dir = parent->d_inode;  
  13.   
  14.     mutex_lock(&dir->i_mutex);  
  15.     /* 
  16.      * First re-do the cached lookup just in case it was created 
  17.      * while we waited for the directory semaphore.. 
  18.      * 
  19.      * FIXME! This could use version numbering or similar to 
  20.      * avoid unnecessary cache lookups. 
  21.      * 
  22.      * The "dcache_lock" is purely to protect the RCU list walker 
  23.      * from concurrent renames at this point (we mustn't get false 
  24.      * negatives from the RCU list walk here, unlike the optimistic 
  25.      * fast walk). 
  26.      * 
  27.      * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup 
  28.      */  
  29.     result = d_lookup(parent, name);  
  30.     if (!result) {  
  31.         struct dentry *dentry;  
  32.   
  33.         /* Don't create child dentry for a dead directory. */  
  34.         result = ERR_PTR(-ENOENT);  
  35.         if (IS_DEADDIR(dir))  
  36.             goto out_unlock;  
  37.         /*从slab中分配dentry并且初始化*/  
  38.         dentry = d_alloc(parent, name);  
  39.         result = ERR_PTR(-ENOMEM);  
  40.         if (dentry) {  
  41.                     /*调用具体文件系统的loopup函数*/  
  42.             result = dir->i_op->lookup(dir, dentry, nd);  
  43.             if (result)  
  44.                 dput(dentry);  
  45.             else  
  46.                 result = dentry;  
  47.         }  
  48. out_unlock:  
  49.         mutex_unlock(&dir->i_mutex);  
  50.         return result;  
  51.     }  
  52.   
  53.     /* 
  54.      * Uhhuh! Nasty case: the cache was re-populated while 
  55.      * we waited on the semaphore. Need to revalidate. 
  56.      */  
  57.     mutex_unlock(&dir->i_mutex);  
  58.     if (result->d_op && result->d_op->d_revalidate) {  
  59.         result = do_revalidate(result, nd);  
  60.         if (!result)  
  61.             result = ERR_PTR(-ENOENT);  
  62.     }  
  63.     return result;  
  64. }  

2.2.2.1 分配dentry并且初始化

[cpp]
  1. struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)  
  2. {  
  3.     struct dentry *dentry;  
  4.     char *dname;  
  5.     /*从slab中非配dentry*/  
  6.     dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);  
  7.     if (!dentry)  
  8.         return NULL;  
  9.   
  10.     if (name->len > DNAME_INLINE_LEN-1) {  
  11.         dname = kmalloc(name->len + 1, GFP_KERNEL);  
  12.         if (!dname) {  
  13.             kmem_cache_free(dentry_cache, dentry);   
  14.             return NULL;  
  15.         }  
  16.     } else  {  
  17.         dname = dentry->d_iname;  
  18.     }  
  19.         /*初始化非配的dentry结构*/  
  20.     dentry->d_name.name = dname;  
  21.   
  22.     dentry->d_name.len = name->len;  
  23.     dentry->d_name.hash = name->hash;  
  24.     memcpy(dname, name->name, name->len);  
  25.     dname[name->len] = 0;  
  26.   
  27.     atomic_set(&dentry->d_count, 1);  
  28.     dentry->d_flags = DCACHE_UNHASHED;  
  29.     spin_lock_init(&dentry->d_lock);  
  30.     dentry->d_inode = NULL;  
  31.     dentry->d_parent = NULL;  
  32.     dentry->d_sb = NULL;  
  33.     dentry->d_op = NULL;  
  34.     dentry->d_fsdata = NULL;  
  35.     dentry->d_mounted = 0;  
  36.     INIT_HLIST_NODE(&dentry->d_hash);  
  37.     INIT_LIST_HEAD(&dentry->d_lru);  
  38.     INIT_LIST_HEAD(&dentry->d_subdirs);  
  39.     INIT_LIST_HEAD(&dentry->d_alias);  
  40.   
  41.     if (parent) {  
  42.         dentry->d_parent = dget(parent);  
  43.         dentry->d_sb = parent->d_sb;  
  44.     } else {  
  45.         INIT_LIST_HEAD(&dentry->d_u.d_child);  
  46.     }  
  47.   
  48.     spin_lock(&dcache_lock);  
  49.     if (parent)  
  50.         list_add(&dentry->d_u.d_child, &parent->d_subdirs);  
  51.     dentry_stat.nr_dentry++;  
  52.     spin_unlock(&dcache_lock);  
  53.   
  54.     return dentry;  
  55. }  

从上面的代码中可以看到,linux内核中的路径搜索大体工作如下:

1,初始化查询,设置nd结构指向查询开始处的文件;

2,从起点开始路径的搜索,其中nd用来返回搜索结果,在搜索过程中需要根据路径名称一步一步的访问,包括字符‘/‘的处理、访问上层目录的处理(需要考虑超出本文件系统)以及访问的dentry在内存中不存在需要从新分配的情况等;

程序返回后,参数中的nd结构保存了当前的搜索结果信息,包括目标文件或目录的dentry结构和inode结构。

linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款