Linux文件系统(七)---系统调用之open操作(三) 之 open_namei函数

来源:互联网 发布:cab格式软件下载 编辑:程序博客网 时间:2024/06/05 20:29

Open系统调用


下面看看open_namei函数:

这个函数的基本的功能是:

首先我们知道filename,也就是知道路径了,那么我们可以根据上级目录项对象,查询下一级的目录项对象,如果在目录项缓存找到下一级的目录项对象,则直接返回,并填充nd的挂载点对象和目录项对象。否则,构建一个子目录项对象,并分配一个新的inode结构,将子目录项对象和inode结构相关联。这样,一直循环到最后一个路径分量。最后返回的是最后一个路径分量的目录项对象和挂载点对象。

分成两点:

第一:如果单纯是打开一个已有的文件,那么直接跟了lookup函数(细节:注意如果这个名字分量打开的是“符号链接,那么需要二次查找实际的位置,下面会说到”)找到打开即可

第二:如果是需要创建一个新的文件,那么需要得到父目录的目录项对象和挂载点,然后再执行创建过程

细节:对于我们的输入路径,可以是绝对路径(从'/'开始),也可以是相对路径,那么下面的处理也会根据是绝对路径还是相对路径进行不同的处理。另外一些查找的细节后面再说。

总之,下面的函数执行完成之后,就会得到

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 1006 /* 
  2. 1007  *      open_namei() 
  3. 1008  * 
  4. 1009  * namei for open - this is in fact almost the whole open-routine. 
  5. 1010  * 
  6. 1011  * Note that the low bits of "flag" aren't the same as in the open 
  7. 1012  * system call - they are 00 - no permissions needed 
  8. 1013  *                        01 - read permission needed 
  9. 1014  *                        10 - write permission needed 
  10. 1015  *                        11 - read/write permissions needed 
  11. 1016  * which is a lot more logical, and also allows the "no perm" needed 
  12. 1017  * for symlinks (where the permissions are checked later). 
  13. 1018  * SMP-safe 
  14. 1019  */  
  15. 1020 int open_namei(const char * pathname, int flag, int mode, struct nameidata *nd)  
  16. 1021 {  
  17. 1022         int acc_mode, error = 0;  
  18. 1023         struct inode *inode;  
  19. 1024         struct dentry *dentry;  
  20. 1025         struct vfsmount *mnt;  
  21. 1026         struct dentry *dir;  
  22. 1027         int count = 0;  
  23. 1028   
  24. 1029         acc_mode = ACC_MODE(flag);  
  25. 1030   
  26. 1031         /* 
  27. 1032          * The simplest case - just a plain lookup. 
  28. 1033          */  
  29. 1034         if (!(flag & O_CREAT)) {   /* 如果不是创建新的文件,那么仅仅根据提供的路径去寻找文件即可 */  
  30. 1035                 error = path_lookup(pathname, lookup_flags(flag), nd);  /* 寻找文件函数 */  
  31. 1036                 if (error)  
  32. 1037                         return error;  
  33. 1038                 dentry = nd->dentry;  /* 目录项:这个目录项就是之前path_lookup找到的路径最后一个分量也就是具体文件的对应的目录项对象 */  
  34. 1039                 goto ok;    
  35. 1040         }  
  36. 1041   
  37. 1042         /* 
  38. 1043          * Create - we need to know the parent.(需要创建文件时候,需要知道parent目录,所以要先找parent) 
  39. 1044          */  
  40. 1045         error = path_lookup(pathname, LOOKUP_PARENT, nd);   /* nd中的dentry是父目录的目录项对象,last_type是最后一个分量也就是需要创建的文件的类型 */  
  41. 1046         if (error)  
  42. 1047                 return error;  
  43. 1048   
  44. 1049         /* 
  45. 1050          * We have the parent and last component. First of all, check 
  46. 1051          * that we are not asked to creat(2) an obvious directory - that 
  47. 1052          * will not do. 
  48. 1053          */  
  49. 1054         error = -EISDIR; /* 错误的类型(不是正常文件)不给创建文件 */  
  50. 1055         if (nd->last_type != LAST_NORM || nd->last.name[nd->last.len])  
  51. 1056                 goto exit;  
  52. 1057   
  53. 1058         dir = nd->dentry; /* 获得父目录的目录项 */  
  54. 1059         down(&dir->d_inode->i_sem);  
  55. 1060         dentry = lookup_hash(&nd->last, nd->dentry); /* 在父目录下找这个last-name名字的dentry是不是存在,如果不存在需要创建一个新的目录项 */  
  56. 1061   
  57. 1062 do_last:  
  58. 1063         error = PTR_ERR(dentry);  
  59. 1064         if (IS_ERR(dentry)) {  
  60. 1065                 up(&dir->d_inode->i_sem);  
  61. 1066                 goto exit;  
  62. 1067         }  
  63. 1068   
  64. 1069         /* Negative dentry, just create the file */  
  65. 1070         if (!dentry->d_inode) { /* 目录项是之前创建的,如果d_inode不存在,那么说明文件时不存在的,那么直接需要创建一个新的文件 */  
  66. 1071                 error = vfs_create(dir->d_inode, dentry,    /* 创建这个文件对应的inode */  
  67. 1072                                    mode & ~current->fs->umask);  
  68. 1073                 up(&dir->d_inode->i_sem);  
  69. 1074                 dput(nd->dentry);  
  70. 1075                 nd->dentry = dentry;  
  71. 1076                 if (error)  
  72. 1077                         goto exit;  
  73. 1078                 /* Don't check for write permission, don't truncate */  
  74. 1079                 acc_mode = 0;  
  75. 1080                 flag &= ~O_TRUNC;  
  76. 1081                 goto ok;   /* 文件创建完成 */  
  77. 1082         }  
  78. 1083   
  79. 1084         /* 
  80. 1085          * It already exists.:这个说明上面找到的对应的文件,也就是说存在同名文件 
  81. 1086          */  
  82. 1087         up(&dir->d_inode->i_sem);  
  83. 1088   
  84. 1089         error = -EEXIST;  
  85. 1090         if (flag & O_EXCL)  
  86. 1091                 goto exit_dput;  
  87. 1092         /* 找到最底层的文件系统,没有挂在其他文件系统 */  
  88. 1093         if (d_mountpoint(dentry)) {  
  89. 1094                 error = -ELOOP;  
  90. 1095                 if (flag & O_NOFOLLOW)  
  91. 1096                         goto exit_dput;  
  92. 1097                 while (__follow_down(&nd->mnt,&dentry) && d_mountpoint(dentry));  
  93. 1098         }  
  94. 1099         error = -ENOENT;  
  95. 1100         if (!dentry->d_inode)  
  96. 1101                 goto exit_dput;  
  97. 1102         if (dentry->d_inode->i_op && dentry->d_inode->i_op->follow_link)  
  98. 1103                 goto do_link; /* 做符号链接 */  
  99. 1104   
  100. 1105         dput(nd->dentry);  
  101. 1106         nd->dentry = dentry;  
  102. 1107         error = -EISDIR;  
  103. 1108         if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))  
  104. 1109                 goto exit;  
  105. 1110 ok:  
  106. 1111         error = -ENOENT;  
  107. 1112         inode = dentry->d_inode; /* 文件inode */  
  108. 1113         if (!inode)  
  109. 1114                 goto exit;  
  110. 1115   
  111. 1116         error = -ELOOP;  
  112. 1117         if (S_ISLNK(inode->i_mode))  
  113. 1118                 goto exit;  
  114. 1119           
  115. 1120         error = -EISDIR;  
  116. 1121         if (S_ISDIR(inode->i_mode) && (flag & FMODE_WRITE))  
  117. 1122                 goto exit;  
  118. 1123   
  119. 1124         error = permission(inode,acc_mode);  
  120. 1125         if (error)  
  121. 1126                 goto exit;  
  122. 1127   
  123. 1128         /* 下面处理一些特殊的文件FIFO,socket文件,设备文件 
  124. 1129          * FIFO's, sockets and device files are special: they don't 
  125. 1130          * actually live on the filesystem itself, and as such you 
  126. 1131          * can write to them even if the filesystem is read-only. 
  127. 1132          */  
  128. 1133         if (S_ISFIFO(inode->i_mode) || S_ISSOCK(inode->i_mode)) {  
  129. 1134                 flag &= ~O_TRUNC;  
  130. 1135         } else if (S_ISBLK(inode->i_mode) || S_ISCHR(inode->i_mode)) {  
  131. 1136                 error = -EACCES;  
  132. 1137                 if (nd->mnt->mnt_flags & MNT_NODEV)  
  133. 1138                         goto exit;  
  134. 1139   
  135. 1140                 flag &= ~O_TRUNC;  
  136. 1141         } else {  
  137. 1142                 error = -EROFS;  
  138. 1143                 if (IS_RDONLY(inode) && (flag & 2))  
  139. 1144                         goto exit;  
  140. 1145         }  
  141. 1146         /* 
  142. 1147          * An append-only file must be opened in append mode for writing. 
  143. 1148          */  
  144. 1149         error = -EPERM; /* 追加模式 */  
  145. 1150         if (IS_APPEND(inode)) {  
  146. 1151                 if  ((flag & FMODE_WRITE) && !(flag & O_APPEND))  
  147. 1152                         goto exit;  
  148. 1153                 if (flag & O_TRUNC)  
  149. 1154                         goto exit;  
  150. 1155         }  
  151. 1156   
  152. 1157         /* 
  153. 1158          * Ensure there are no outstanding leases on the file. 
  154. 1159          */  
  155. 1160         error = get_lease(inode, flag);  
  156. 1161         if (error)  
  157. 1162                 goto exit;  
  158. 1163   
  159. 1164         if (flag & O_TRUNC) {  
  160. 1165                 error = get_write_access(inode);  /* 写权限 */  
  161. 1166                 if (error)  
  162. 1167                         goto exit;  
  163. 1168   
  164. 1169                 /* 
  165. 1170                  * Refuse to truncate files with mandatory locks held on them. 
  166. 1171                  */  
  167. 1172                 error = locks_verify_locked(inode);  
  168. 1173                 if (!error) {  
  169. 1174                         DQUOT_INIT(inode);  
  170. 1175                           
  171. 1176                         error = do_truncate(dentry, 0); /* 执行截断操作 */  
  172. 1177                 }  
  173. 1178                 put_write_access(inode); /* 释放写权限 */  
  174. 1179                 if (error)  
  175. 1180                         goto exit;  
  176. 1181         } else  
  177. 1182                 if (flag & FMODE_WRITE)  
  178. 1183                         DQUOT_INIT(inode);  
  179. 1184   
  180. 1185         return 0;  
  181. 1186   
  182. 1187 exit_dput:  
  183. 1188         dput(dentry);  
  184. 1189 exit:  
  185. 1190         path_release(nd);  
  186. 1191         return error;  
  187. 1192   
  188. 1193 do_link:  
  189. 1194         error = -ELOOP;  
  190. 1195         if (flag & O_NOFOLLOW)  
  191. 1196                 goto exit_dput;  
  192. 1197         /* 
  193. 1198          * This is subtle. Instead of calling do_follow_link() we do the 
  194. 1199          * thing by hands. The reason is that this way we have zero link_count 
  195. 1200          * and path_walk() (called from ->follow_link) honoring LOOKUP_PARENT. 
  196. 1201          * After that we have the parent and last component, i.e. 
  197. 1202          * we are in the same situation as after the first path_walk(). 
  198. 1203          * Well, almost - if the last component is normal we get its copy 
  199. 1204          * stored in nd->last.name and we will have to putname() it when we 
  200. 1205          * are done. Procfs-like symlinks just set LAST_BIND. 
  201. 1206          */  
  202. 1207         UPDATE_ATIME(dentry->d_inode);  
  203. 1208         mnt = mntget(nd->mnt);  
  204. 1209         error = dentry->d_inode->i_op->follow_link(dentry, nd);  
  205. 1210         dput(dentry);  
  206. 1211         mntput(mnt);  
  207. 1212         if (error)  
  208. 1213                 return error;  
  209. 1214         if (nd->last_type == LAST_BIND) {  
  210. 1215                 dentry = nd->dentry;  
  211. 1216                 goto ok;  
  212. 1217         }  
  213. 1218         error = -EISDIR;  
  214. 1219         if (nd->last_type != LAST_NORM)  
  215. 1220                 goto exit;  
  216. 1221         if (nd->last.name[nd->last.len]) {  
  217. 1222                 putname(nd->last.name);  
  218. 1223                 goto exit;  
  219. 1224         }  
  220. 1225         error = -ELOOP;  
  221. 1226         if (count++==32) {  
  222. 1227                 putname(nd->last.name);  
  223. 1228                 goto exit;  
  224. 1229         }  
  225. 1230         dir = nd->dentry;  
  226. 1231         down(&dir->d_inode->i_sem);  
  227. 1232         dentry = lookup_hash(&nd->last, nd->dentry);  
  228. 1233         putname(nd->last.name);  
  229. 1234         goto do_last;  
  230. 1235 }  


看一下path_lookup函数:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 755 /* SMP-safe */  
  2. 756 int fastcall path_lookup(const char *path, unsigned flags, struct nameidata *nd)  
  3. 757 {  
  4. 758         int error = 0;  
  5. 759         if (path_init(path, flags, nd))       /* 这个函数获得当前的目录项对象和安装点,同时在绝对路径情况下回探测能不能到达最后一个分量,如果可以,那么下面的path_walk就可以不需要执行了,如果失败了,那么需要执行下面的path_walk进行循环找到最后一个路径分量的目录项好安装点 */  
  6. 760                 error = path_walk(path, nd);  /* 找到最后一个名字路径分量的目录项对象和挂载点 */  
  7. 761         return error;  
  8. 762 }  

这个函数涉及到path_init和path_walk函数,下面分别看一下:

path_init函数主要是获取最初的开目录项(路径开始点)

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 765 /* SMP-safe */  
  2. 766 int fastcall path_init(const char *name, unsigned int flags, struct nameidata *nd)  
  3. 767 {  
  4. 768         nd->last_type = LAST_ROOT; /* if there are only slashes... */  
  5. 769         nd->flags = flags;  
  6. 770         if (*name=='/')    /* 如果路径第一个字符是'/',那么说明是绝对路径,从根目录开始搜索的路径 */  
  7. 771                 return walk_init_root(name,nd); /* 执行这个函数,下面会分析 */  
  8. 772         read_lock(&current->fs->lock);         /* 如果不是绝对路径,那么就是相对路径了,那么直接获得当前的进程执行目录项即可 */  
  9. 773         nd->mnt = mntget(current->fs->pwdmnt); /* 如果需要打开的不是根目录,那么找到当前进程执行目录安装点 */  
  10. 774         nd->dentry = dget(current->fs->pwd);   /* 找到进程当前执行目录 */  
  11. 775         read_unlock(&current->fs->lock);   
  12. 776         return 1;    /* 这个地方仅仅是得到了当前的目录项对象,我们需要得到的是最后一个路径分量的对象,所以返回之后还需要walk_path,return 1 */  
  13. 777 }  


如果是根目录的话,看一下这个函数walk_init_root:

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 736 /* SMP-safe */  
  2. 737 static inline int  
  3. 738 walk_init_root(const char *name, struct nameidata *nd)  
  4. 739 {  
  5. 740         read_lock(&current->fs->lock);  
  6. 741         if (current->fs->altroot && !(nd->flags & LOOKUP_NOALT)) {  /* 如果用户有自己设置替换目录,那么使用这个 & 允许使用替换目录 */  
  7. 742                 nd->mnt = mntget(current->fs->altrootmnt);  
  8. 743                 nd->dentry = dget(current->fs->altroot);  
  9. 744                 read_unlock(&current->fs->lock);  
  10. 745                 if (__emul_lookup_dentry(name,nd)) /* 这个函数里面其实就是封装了walk_path进行路径的寻找,如果返回1,那么说明找到了最后一个了 */  
  11. 746                         return 0;                  /* 既然找到了,说明nd被填充好了,那么后期返回的时候无需再寻找了~~~~~~~ */  
  12. 747                 read_lock(&current->fs->lock);  
  13. 748         }/* 这下面仅仅是保存了当前的目录,还没有循环到最后一个目录分量,所以,返回后需要继续walk_path,所以return 1 */  
  14. 749         nd->mnt = mntget(current->fs->rootmnt); /* 如果没有设置替换目录,那么使用进程根目录即可 */  
  15. 750         nd->dentry = dget(current->fs->root);     
  16. 751         read_unlock(&current->fs->lock);  
  17. 752         return 1;  
  18. 753 }  


OK,回到上面,继续看path_walk函数:
[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 668 int fastcall path_walk(const char * name, struct nameidata *nd)  
  2. 669 {  
  3. 670         current->total_link_count = 0;   /* 初始化符号链接总数 */  
  4. 671         return link_path_walk(name, nd); /* 调用实际文件系统的路径分解函数 */  
  5. 672 }  

注意这个link_path_walk函数主要是根据给定的路径,找到最后一个路径分量的目录项对象和安装点~

这个函数主要思路:打开一个文件,如果是仅仅打开,那么沿着路径分量不断往下找,最后返回最后一个名字分量的目录项对象就OK,如果是创建文件,那么返回的是倒数第二个分量也就是需要创建的文件的父目录的目录项对象。

细节:

第一:如果是打开的路径不是符号链接路径,那么按照路径(每个路径分量)层层去找到,直到找到最后一个路径分量代表的目录项对象,也就说最终返回的就是最后一个分量的目录项对象和挂载点。

第二:存在有些目录项对象对应的是符号链接,遇到这种情况,需要二次寻找这个真实的的路径分量,然后再继续往下找

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 450 /* 
  2. 451  * Name resolution. 
  3. 452  * 
  4. 453  * This is the basic name resolution function, turning a pathname 
  5. 454  * into the final dentry. 
  6. 455  * 
  7. 456  * We expect 'base' to be positive and a directory. 
  8. 457  */  
  9. 458 int fastcall link_path_walk(const char * name, struct nameidata *nd)  
  10. 459 {  
  11. 460         struct dentry *dentry;  
  12. 461         struct inode *inode;  
  13. 462         int err;  
  14. 463         unsigned int lookup_flags = nd->flags;  
  15. 464         /* 从根目录之后的目录项开始 */  
  16. 465         while (*name=='/')  
  17. 466                 name++;  
  18. 467         if (!*name)  
  19. 468                 goto return_reval;  
  20. 469         /* 获得之前保存的开始位置的目录项中对象的inode节点 */  
  21. 470         inode = nd->dentry->d_inode;  
  22. 471         if (current->link_count)  
  23. 472                 lookup_flags = LOOKUP_FOLLOW;  
  24. 473   
  25. 474         /* At this point we know we have a real path component. */  
  26. 475         for(;;) {   /* 注意下面是一个词一个词的处理,例如home/tt/desktoop/xxx.txt,那么先处理home,在处理tt,在处理。。。 */  
  27. 476                 unsigned long hash;  
  28. 477                 struct qstr this;  
  29. 478                 unsigned int c;  
  30. 479                 /* 检测权限问题 */  
  31. 480                 err = permission(inode, MAY_EXEC);  
  32. 481                 dentry = ERR_PTR(err);  
  33. 482                 if (err)  
  34. 483                         break;  
  35. 484   
  36. 485                 this.name = name;  
  37. 486                 c = *(const unsigned char *)name;  
  38. 487                   
  39. 488                 hash = init_name_hash();   /* 默认=0 */  
  40. 489                 do {  
  41. 490                         name++;   /* 指向下一个字符,做准备 */  
  42. 491                         hash = partial_name_hash(c, hash);   /* 计算部分hash值,例如上面home的hash值 */  
  43. 492                         c = *(const unsigned char *)name;  
  44. 493                 } while (c && (c != '/'));  
  45. 494                 this.len = name - (const char *) this.name;   /* 中间一个名字分量长度 */  
  46. 495                 this.hash = end_name_hash(hash);              /* 这个分量的hash值 */  
  47. 496 <span>        </span>  
  48. 497                 /* remove trailing slashes? */  
  49. 498                 if (!c)   /* 如果已经是最后一个名字分量 */  
  50. 499                         goto last_component;   
  51. 500                 while (*++name == '/');  
  52. 501                 if (!*name)  
  53. 502                         goto last_with_slashes;  
  54. 503   
  55. 504                 /* 
  56. 505                  * "." and ".." are special - ".." especially so because it has 
  57. 506                  * to be able to know about the current root directory and 
  58. 507                  * parent relationships. 
  59. 508                  *//* 注意下面是处理当前目录'.'和父目录'..' */  
  60. 509                 if (this.name[0] == '.'switch (this.len) {  
  61. 510                         default:  
  62. 511                                 break;  
  63. 512                         case 2: /* 注意这个地方存在两种情况,第一种是父目录,第二种是隐藏文件,前面也是'.'开始 */  
  64. 513                                 if (this.name[1] != '.')   /* 这种情况就是隐藏文件 */  
  65. 514                                         break;  
  66. 515                                 follow_dotdot(nd);   /* 否则,这样情况就是父目录情况,需要回溯到父目录 '..' */  
  67. 516                                 inode = nd->dentry->d_inode;  
  68. 517                                 /* fallthrough */  
  69. 518                         case 1: /* 当前目录开始 */  
  70. 519                                 continue;  
  71. 520                 }  
  72. 521                 /* 
  73. 522                  * See if the low-level filesystem might want 
  74. 523                  * to use its own hash.. 
  75. 524                  */  /* 使用底层文件系统自己的计算hash值函数 */  
  76. 525                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {  
  77. 526                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);  
  78. 527                         if (err < 0)  
  79. 528                                 break;  
  80. 529                 }  
  81. 530                 /* This does the actual lookups.. */  
  82. 531                 dentry = cached_lookup(nd->dentry, &this, LOOKUP_CONTINUE);  /* 注意这个函数是在dentry的缓冲区去寻找是不是存在这个dentry缓存 */  
  83. 532                 if (!dentry) {                                               /* 这个在前面的dentry缓冲具体说过 */  
  84. 533                         dentry = real_lookup(nd->dentry, &this, LOOKUP_CONTINUE); /* 如果在目录项缓存中不存在,那么,需要分配一个新的dentry缓冲区加入到里面,这个在前面也说过,同样的链接:http://blog.csdn.net/shanshanpt/article/details/39829281 */  
  85. 534                         err = PTR_ERR(dentry);  
  86. 535                         if (IS_ERR(dentry))  
  87. 536                                 break;  
  88. 537                 }  
  89. 538                 /* Check mountpoints.. */ /* 检测挂载点然后继续向下寻找 */  
  90. 539                 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry))  
  91. 540                         ;  
  92. 541 <span>            </span>  
  93. 542                 err = -ENOENT;  
  94. 543                 inode = dentry->d_inode;  /* 获得inode */  
  95. 544                 if (!inode)  
  96. 545                         goto out_dput;  
  97. 546                 err = -ENOTDIR;   
  98. 547                 if (!inode->i_op)  
  99. 548                         goto out_dput;  
  100. 549                 /* 如果当前解析的分量指向的是一个符号链接,那么转到处理符号链接 */  
  101. 550                 if (inode->i_op->follow_link) {  
  102. 551                         struct vfsmount *mnt = mntget(nd->mnt);  
  103. 552                         err = do_follow_link(dentry, nd);    /* 处理符号链接函数 */  
  104. 553                         dput(dentry);  
  105. 554                         mntput(mnt);  
  106. 555                         if (err)  
  107. 556                                 goto return_err;  
  108. 557                         err = -ENOENT;  
  109. 558                         inode = nd->dentry->d_inode;   /* 获得真实inode */  
  110. 559                         if (!inode)  
  111. 560                                 break;  
  112. 561                         err = -ENOTDIR;   
  113. 562                         if (!inode->i_op)  
  114. 563                                 break;  
  115. 564                 } else {  
  116. 565                         dput(nd->dentry);  
  117. 566                         nd->dentry = dentry;    /* 新的dentry赋值给nd的dentry,然后回到上面继续下一个分量处理 */  
  118. 567                 }  
  119. 568                 err = -ENOTDIR;   
  120. 569                 if (!inode->i_op->lookup)  
  121. 570                         break;  
  122. 571                 continue;  
  123. 572                 /* here ends the main loop */  
  124. 573   
  125. 574 last_with_slashes:  
  126. 575                 lookup_flags |= LOOKUP_FOLLOW | LOOKUP_DIRECTORY;  
  127. 576 last_component: /* 对于路径最后一个名字分量的处理 */  
  128. 577                 if (lookup_flags & LOOKUP_PARENT)  /* 如果是创建文件,那么是存在这个标识 */  
  129. 578                         goto lookup_parent;  
  130. 579                 if (this.name[0] == '.'switch (this.len) {   /* 关于'.'和'..'情况和上面说的一样 */  
  131. 580                         default:  
  132. 581                                 break;  
  133. 582                         case 2:   
  134. 583                                 if (this.name[1] != '.')  
  135. 584                                         break;  
  136. 585                                 follow_dotdot(nd);  
  137. 586                                 inode = nd->dentry->d_inode;  
  138. 587                                 /* fallthrough */  
  139. 588                         case 1:  
  140. 589                                 goto return_reval;  
  141. 590                 }/* 底层自己的hash函数 */  
  142. 591                 if (nd->dentry->d_op && nd->dentry->d_op->d_hash) {  
  143. 592                         err = nd->dentry->d_op->d_hash(nd->dentry, &this);  
  144. 593                         if (err < 0)  
  145. 594                                 break;  
  146. 595                 }  
  147. 596                 dentry = cached_lookup(nd->dentry, &this, nd->flags); /* 同样是在dentry缓存中中寻找dentry */  
  148. 597                 if (!dentry) {  
  149. 598                         dentry = real_lookup(nd->dentry, &this, nd->flags); /* 没有dentry换成就会创建一个新的dentry缓存 */  
  150. 599                         err = PTR_ERR(dentry);  
  151. 600                         if (IS_ERR(dentry))  
  152. 601                                 break;  
  153. 602                 }  
  154. 603                 while (d_mountpoint(dentry) && __follow_down(&nd->mnt, &dentry)) /* 检测挂载点,继续寻找 */  
  155. 604                         ;  
  156. 605                 inode = dentry->d_inode;  
  157. 606                 if ((lookup_flags & LOOKUP_FOLLOW)   /* 处理符号链接 */  
  158. 607                     && inode && inode->i_op && inode->i_op->follow_link) {  
  159. 608                         struct vfsmount *mnt = mntget(nd->mnt);  
  160. 609                         err = do_follow_link(dentry, nd);  /* 二次寻找~~~ */  
  161. 610                         dput(dentry);  
  162. 611                         mntput(mnt);  
  163. 612                         if (err)  
  164. 613                                 goto return_err;  
  165. 614                         inode = nd->dentry->d_inode;  
  166. 615                 } else {  
  167. 616                         dput(nd->dentry);  
  168. 617                         nd->dentry = dentry;  
  169. 618                 }  
  170. 619                 err = -ENOENT;  
  171. 620                 if (!inode)   /* 没有存在的inode,那么另外处理 */  
  172. 621                         goto no_inode;  
  173. 622                 if (lookup_flags & LOOKUP_DIRECTORY) {   /* 如果打开的文件时文件夹,那么error */  
  174. 623                         err = -ENOTDIR;   
  175. 624                         if (!inode->i_op || !inode->i_op->lookup)  
  176. 625                                 break;  
  177. 626                 }  
  178. 627                 goto return_base;  
  179. 628 no_inode:  
  180. 629                 err = -ENOENT;  
  181. 630                 if (lookup_flags & (LOOKUP_POSITIVE|LOOKUP_DIRECTORY))  
  182. 631                         break;  
  183. 632                 goto return_base;  
  184. 633 lookup_parent:  /* 如果是创建文件,那么需要先找到父目录,在处理!需要注意的是,父目录就是之前访问的那个目录,所以在这个地方,没有必要将nd中的dentry换成现在孩子的dentry,想换也没有啊,后来需要创建的,所以这个时候nd中保存的就已经是父目录的dentry了~ */  
  185. 634                 nd->last = this;  /* 最后一个分量名 */  
  186. 635                 nd->last_type = LAST_NORM; /* 最后一个分量类型 */  
  187. 636                 if (this.name[0] != '.')  /* 不是'.'说明是正常的文件 */  
  188. 637                         goto return_base;  
  189. 638                 if (this.len == 1)  /* 否则是'.'情况 */  
  190. 639                         nd->last_type = LAST_DOT;  
  191. 640                 else if (this.len == 2 && this.name[1] == '.'/* '..'情况 */  
  192. 641                         nd->last_type = LAST_DOTDOT;  
  193. 642                 else  
  194. 643                         goto return_base;  
  195. 644 return_reval:  
  196. 645                 /* 
  197. 646                  * We bypassed the ordinary revalidation routines. 
  198. 647                  * Check the cached dentry for staleness. 
  199. 648                  */  
  200. 649                 dentry = nd->dentry;  
  201. 650                 if (dentry && dentry->d_op && dentry->d_op->d_revalidate) {  
  202. 651                         err = -ESTALE;  
  203. 652                         if (!dentry->d_op->d_revalidate(dentry, 0)) {  
  204. 653                                 d_invalidate(dentry);  
  205. 654                                 break;  
  206. 655                         }  
  207. 656                 }  
  208. 657 return_base:  
  209. 658                 return 0;  
  210. 659 out_dput:  
  211. 660                 dput(dentry);  
  212. 661                 break;  
  213. 662         }  
  214. 663         path_release(nd);  
  215. 664 return_err:  
  216. 665         return err;  
  217. 666 }  

注意这里面有一个函数叫follow_dotdot,这个函数是回溯到父目录的函数

[cpp] view plaincopyprint?在CODE上查看代码片派生到我的代码片
  1. 412    
  2. 413 static inline void follow_dotdot(struct nameidata *nd)  
  3. 414 {  
  4. 415         while(1) {  
  5. 416                 struct vfsmount *parent;  
  6. 417                 struct dentry *dentry;  
  7. 418                 read_lock(&current->fs->lock);  
  8. 419                 if (nd->dentry == current->fs->root &&  /* 如果回溯到的是进程的根目录。不允许 */  
  9. 420                     nd->mnt == current->fs->rootmnt)  {  
  10. 421                         read_unlock(&current->fs->lock);  
  11. 422                         break;  
  12. 423                 }  
  13. 424                 read_unlock(&current->fs->lock);  
  14. 425                 spin_lock(&dcache_lock);  
  15. 426                 if (nd->dentry != nd->mnt->mnt_root) {  /*如果目录项对象不是根目录,则返回上一级目录项对象*/    
  16. 427                         dentry = dget(nd->dentry->d_parent);  /* 得到parent目录项 */   
  17. 428                         spin_unlock(&dcache_lock);  
  18. 429                         dput(nd->dentry);  
  19. 430                         nd->dentry = dentry;  
  20. 431                         break;  
  21. 432                 }  
  22. 433                 parent=nd->mnt->mnt_parent;   /* 否则得到挂载点parent */  
  23. 434                 if (parent == nd->mnt) {  
  24. 435                         spin_unlock(&dcache_lock);  
  25. 436                         break;  
  26. 437                 }  
  27. 438                 mntget(parent);  
  28. 439                 dentry=dget(nd->mnt->mnt_mountpoint);  
  29. 440                 spin_unlock(&dcache_lock);  
  30. 441                 dput(nd->dentry);  
  31. 442                 nd->dentry = dentry;  
  32. 443                 mntput(nd->mnt);  
  33. 444                 nd->mnt = parent;  
  34. 445         }/* 下面是一直回溯到没有挂载其它文件系统的挂载点,mnt指向这个最底层的挂载点 */  
  35. 446         while (d_mountpoint(nd->dentry) && __follow_down(&nd->mnt, &nd->dentry))  
  36. 447                 ;  
  37. 448 }  

/************至此,open_namei函数完成***************************************************************************************/
0 0
原创粉丝点击