2.2.1 处理double dot
所谓double dot为访问上层目录
- static __always_inline void follow_dotdot(struct nameidata *nd)
- {
- set_root(nd);
- while(1) {
- struct vfsmount *parent;
- struct dentry *old = nd->path.dentry;
- /*如果已经达到本进程的根节点,这时不能再往上跑了
- 所以保持不变*/
- if (nd->path.dentry == nd->root.dentry &&
- nd->path.mnt == nd->root.mnt) {
- break;
- }
- spin_lock(&dcache_lock);
- /*已经到达节点与其父节点在同一个设备上。在这种情况下
- 既然已经到达的这个节点的dentry结构已经建立,则其父节点的
- dentry结构也必然已经建立在内存中,而且dentry结构中的指针
- d_parent就指向其父节点,所以往上跑一层是很简单的事情*/
- if (nd->path.dentry != nd->path.mnt->mnt_root) {
- nd->path.dentry = dget(nd->path.dentry->d_parent);/*往上走一层,并且对应用计数加一*/
- spin_unlock(&dcache_lock);
- dput(old);/*释放就得目录的引用*/
- break;
- }
- spin_unlock(&dcache_lock);
- spin_lock(&vfsmount_lock);
- /*运行到这里,表示已经到达节点就是其所在设备上的根节点
- 往上跑一层就要跑到另一个设备上去了,当将一个存储设备安装到
- 另一个设备上的某个节点时,内核会分配和设置一个vfsmount
- 结构,通过这个结构将两个设备以及两个节点连接起来。
- 所以,每个已经安装的存储设备都有一个vfsmount结构,结构
- 中有个指针mnt_parent指向其父设备,另一个指针mnt_mountpoint
- 指向代表这安装点的dentry结构*/
- /*保存nd指定的mnt的父mnt*/
- parent = nd->path.mnt->mnt_parent;
- /*当前的vfsmount结构代表这跟设备*/
- if (parent == nd->path.mnt) {
- spin_unlock(&vfsmount_lock);
- break;
- }
- /*当前设备不是跟设备*/
- mntget(parent);
- /*指向该设备上的安装点的上一层目录*/
- nd->path.dentry = dget(nd->path.mnt->mnt_mountpoint);
- spin_unlock(&vfsmount_lock);
- dput(old);
- mntput(nd->path.mnt);
- nd->path.mnt = parent;/*指向上层设备上的vfsmount 结构*/
- }
- follow_mount(&nd->path);/*mnt向子节点移动一个*/
- }
- static void follow_mount(struct path *path)
- {
- while (d_mountpoint(path->dentry)) {/*如果该文件系统已经安装*/
- /*找到指定的孩子mnt*/
- struct vfsmount *mounted = lookup_mnt(path);
- if (!mounted)/*如果没有孩子mnt了*/
- break;
- /*递减引用计数*/
- dput(path->dentry);
- mntput(path->mnt);
- path->mnt = mounted;/*找到的mnt作为path的mnt*/
- path->dentry = dget(mounted->mnt_root);/*递增引用计数,将找到的mnt文件系统的根目录赋给path*/
- }
- }
2.2.2 实际的路径搜索工作
- /*
- * It's more convoluted than I'd like it to be, but... it's still fairly
- * small and for now I'd prefer to have fast path as straight as possible.
- * It _is_ time-critical.
- */
- static int do_lookup(struct nameidata *nd, struct qstr *name,
- struct path *path)
- {
- struct vfsmount *mnt = nd->path.mnt;
- /*在内存中寻找该节点已经建立的dentry结构。内核中有个hash表dentry_hashtable
- 是一个list_head指针数组,一旦在内存中建立起一个目录节点的dentry结构
- 就根据其节点名的hash值挂入hash表中的某个队列,需要寻找时则还是根据hash值从
- hash表着手*/
- struct dentry *dentry = __d_lookup(nd->path.dentry, name);
- if (!dentry)/*如果没有找到,转向下面*/
- goto need_lookup;
- if (dentry->d_op && dentry->d_op->d_revalidate)
- goto need_revalidate;
- done:/*内存中找到了dentry*/
- path->mnt = mnt;
- path->dentry = dentry;
- /*访问下一个mnt,其实就是现在mnt的子mnt*/
- __follow_mount(path);
- return 0;
- need_lookup:/*到这里是在内存中没有找到dentry结构*/
- /*到磁盘上通过其所在的目录寻找,找到后在内存中为其建立起
- dentry结构并将之挂入hash表中某个队列中*/
- dentry = real_lookup(nd->path.dentry, name, nd);
- if (IS_ERR(dentry))
- goto fail;
- goto done;
- need_revalidate:
- dentry = do_revalidate(dentry, nd);
- if (!dentry)
- goto need_lookup;
- if (IS_ERR(dentry))
- goto fail;
- goto done;
- fail:
- return PTR_ERR(dentry);
- }
- /*
- * This is called when everything else fails, and we actually have
- * to go to the low-level filesystem to find out what we should do..
- *
- * We get the directory semaphore, and after getting that we also
- * make sure that nobody added the entry to the dcache in the meantime..
- * SMP-safe
- */
- static struct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
- {
- struct dentry * result;
- struct inode *dir = parent->d_inode;
- mutex_lock(&dir->i_mutex);
- /*
- * First re-do the cached lookup just in case it was created
- * while we waited for the directory semaphore..
- *
- * FIXME! This could use version numbering or similar to
- * avoid unnecessary cache lookups.
- *
- * The "dcache_lock" is purely to protect the RCU list walker
- * from concurrent renames at this point (we mustn't get false
- * negatives from the RCU list walk here, unlike the optimistic
- * fast walk).
- *
- * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
- */
- result = d_lookup(parent, name);
- if (!result) {
- struct dentry *dentry;
- /* Don't create child dentry for a dead directory. */
- result = ERR_PTR(-ENOENT);
- if (IS_DEADDIR(dir))
- goto out_unlock;
- /*从slab中分配dentry并且初始化*/
- dentry = d_alloc(parent, name);
- result = ERR_PTR(-ENOMEM);
- if (dentry) {
- /*调用具体文件系统的loopup函数*/
- result = dir->i_op->lookup(dir, dentry, nd);
- if (result)
- dput(dentry);
- else
- result = dentry;
- }
- out_unlock:
- mutex_unlock(&dir->i_mutex);
- return result;
- }
- /*
- * Uhhuh! Nasty case: the cache was re-populated while
- * we waited on the semaphore. Need to revalidate.
- */
- mutex_unlock(&dir->i_mutex);
- if (result->d_op && result->d_op->d_revalidate) {
- result = do_revalidate(result, nd);
- if (!result)
- result = ERR_PTR(-ENOENT);
- }
- return result;
- }
2.2.2.1 分配dentry并且初始化
- struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
- {
- struct dentry *dentry;
- char *dname;
- /*从slab中非配dentry*/
- dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
- if (!dentry)
- return NULL;
- if (name->len > DNAME_INLINE_LEN-1) {
- dname = kmalloc(name->len + 1, GFP_KERNEL);
- if (!dname) {
- kmem_cache_free(dentry_cache, dentry);
- return NULL;
- }
- } else {
- dname = dentry->d_iname;
- }
- /*初始化非配的dentry结构*/
- dentry->d_name.name = dname;
- dentry->d_name.len = name->len;
- dentry->d_name.hash = name->hash;
- memcpy(dname, name->name, name->len);
- dname[name->len] = 0;
- atomic_set(&dentry->d_count, 1);
- dentry->d_flags = DCACHE_UNHASHED;
- spin_lock_init(&dentry->d_lock);
- dentry->d_inode = NULL;
- dentry->d_parent = NULL;
- dentry->d_sb = NULL;
- dentry->d_op = NULL;
- dentry->d_fsdata = NULL;
- dentry->d_mounted = 0;
- INIT_HLIST_NODE(&dentry->d_hash);
- INIT_LIST_HEAD(&dentry->d_lru);
- INIT_LIST_HEAD(&dentry->d_subdirs);
- INIT_LIST_HEAD(&dentry->d_alias);
- if (parent) {
- dentry->d_parent = dget(parent);
- dentry->d_sb = parent->d_sb;
- } else {
- INIT_LIST_HEAD(&dentry->d_u.d_child);
- }
- spin_lock(&dcache_lock);
- if (parent)
- list_add(&dentry->d_u.d_child, &parent->d_subdirs);
- dentry_stat.nr_dentry++;
- spin_unlock(&dcache_lock);
- return dentry;
- }
从上面的代码中可以看到,linux内核中的路径搜索大体工作如下:
1,初始化查询,设置nd结构指向查询开始处的文件;
2,从起点开始路径的搜索,其中nd用来返回搜索结果,在搜索过程中需要根据路径名称一步一步的访问,包括字符‘/‘的处理、访问上层目录的处理(需要考虑超出本文件系统)以及访问的dentry在内存中不存在需要从新分配的情况等;
程序返回后,参数中的nd结构保存了当前的搜索结果信息,包括目标文件或目录的dentry结构和inode结构。
