Chinaunix首页 | 论坛 | 博客
  • 博客访问: 418529
  • 博文数量: 99
  • 博客积分: 65
  • 博客等级: 民兵
  • 技术积分: 1012
  • 用 户 组: 普通用户
  • 注册时间: 2012-04-20 16:30
个人简介

linux kernel 工程师

文章分类

全部博文(99)

文章存档

2018年(5)

2017年(12)

2016年(27)

2015年(10)

2014年(43)

2012年(2)

我的朋友

分类: LINUX

2016-12-23 16:24:55


根据路径名查找目标节点的第一步,就是确定起始路径。这个工作就是path_init来完成的。
path_init的主要目的就是为了确定path lookup的起始位置。当然是要确定起始位置的dentry和vfsmount。
分三种情形:
1. 从"/"开始
2. 从当前路径开始
3. 从进程某个已经打开的目录路径开始。


点击(此处)折叠或打开

  1. static int path_init(int dfd, const char *name, unsigned int flags, struct nameidata *nd)
  2. {
  3.     int retval = 0;
  4.     int fput_needed;
  5.     struct file *file;

  6.     nd->last_type = LAST_ROOT; /* if there are only slashes... */
  7.     nd->flags = flags;
  8.     nd->depth = 0;
  9.     nd->root.mnt = NULL;

  10.     if (*name=='/') {
  11.         /* 从"/"开始 */
  12.         set_root(nd);
  13.         nd->path = nd->root;/* 直接使用进程的根路径 */
  14.         path_get(&nd->root);
  15.     } else if (dfd == AT_FDCWD) {
  16.         /* 从当前路径开始 */
  17.         struct fs_struct *fs = current->fs;
  18.         read_lock(&fs->lock);
  19.         nd->path = fs->pwd;/* 使用进程的当前路径 */
  20.         path_get(&fs->pwd);
  21.         read_unlock(&fs->lock);
  22.     } else {
  23.         /* 如果某目录已经被当前进程打开,则根据文件描述符fd,
  24.             找到file=current->files->fdt[fd]
  25.             file->f_path就是要找的起始路径 */
  26.         struct dentry *dentry;

  27.         file = fget_light(dfd, &fput_needed);
  28.         retval = -EBADF;
  29.         if (!file)
  30.             goto out_fail;

  31.         dentry = file->f_path.dentry;

  32.         retval = -ENOTDIR;
  33.         if (!S_ISDIR(dentry->d_inode->i_mode))
  34.             goto fput_fail;

  35.         retval = file_permission(file, MAY_EXEC);
  36.         if (retval)
  37.             goto fput_fail;

  38.         nd->path = file->f_path;/* 根据fd找到file,使用file的path */
  39.         path_get(&file->f_path);

  40.         fput_light(file, fput_needed);
  41.     }
  42.     return 0;

  43. fput_fail:
  44.     fput_light(file, fput_needed);
  45. out_fail:
  46.     return retval;
  47. }

阅读(2311) | 评论(0) | 转发(0) |
0

上一篇:do_path_lookup

下一篇:__d_lookup

给主人留下些什么吧!~~