Linux虚拟文件系统--文件路径名的解析(4)--符号链接


符号链接也是一种文件,只不过其内容是另一个文件的路径名。对于符号链接的处理,要注意避免死循环的产生,如一个符号链接指向其本身。符号链接可能包含了符号链接,因此内核采用递归的方式来处理这个问题,为了避免符号链接的死循环而导致无穷递归,内核采用link_count和total_link_count来跟踪符号链接的处理,其中前者表示连续的符号链接数,后者表示总共的符号链接数,两者都不能超过各自的限制,否则内核会放弃解析。

相关阅读: http://www.bkjia.com/search.aspx?where=nkey&keyword=3305

  1. staticinlineint do_follow_link(struct path *path, struct nameidata *nd)
  2. {
  3. int err = -ELOOP;
  4. /*如果处理的链接数超过下面的限制则会放弃搜索(因为有可能陷入死循环)*/
  5. /*link_count表示连续的符号链接数,其值不能超过8,total_link_count表示总共的
  6. 链接数,气值不能超过40*/
  7. if (current->link_count >= MAX_NESTED_LINKS)
  8. goto loop;
  9. if (current->total_link_count >= 40)
  10. goto loop;
  11. BUG_ON(nd->depth >= MAX_NESTED_LINKS);
  12. cond_resched();
  13. err = security_inode_follow_link(path->dentry, nd);
  14. if (err)
  15. goto loop;
  16. /*每处理一次符号链接,link_count,total_link_count和depth都要加1*/
  17. current->link_count++;
  18. current->total_link_count++;//
  19. nd->depth++;
  20. /*主要是调用特定于文件系统的follow_link函数进行符号链接的跟踪*/
  21. err = __do_follow_link(path, nd);
  22. /*每执行完一次符号链接,link_count和depth都要减1*/
  23. current->link_count--;
  24. nd->depth--;
  25. return err;
  26. loop:
  27. path_put_conditional(path, nd);
  28. path_put(&nd->path);
  29. return err;
  30. }
  1. static __always_inline int __do_follow_link(struct path *path, struct nameidata *nd)
  2. {
  3. int error;
  4. void *cookie;
  5. struct dentry *dentry = path->dentry;
  6. touch_atime(path->mnt, dentry);
  7. nd_set_link(nd, NULL);//在saved_names中清除当前解析的符号链接路径
  8. if (path->mnt != nd->path.mnt) {
  9. path_to_nameidata(path, nd);
  10. dget(dentry);
  11. }
  12. mntget(path->mnt);
  13. /*调用特定于文件系统的follow_link方法来跟踪符号链接,并将符号链接中保存的路径名提取出来
  14. 存放在nd的saved_names中*/
  15. cookie = dentry->d_inode->i_op->follow_link(dentry, nd);
  16. error = PTR_ERR(cookie);
  17. if (!IS_ERR(cookie)) {
  18. char *s = nd_get_link(nd);//提取saved_names中的路径
  19. error = 0;
  20. if (s)//路径存在,则继续解析,__vfs_follow_link将会调用link_path_walk,因此对于
  21. //路径中存在多个符号链接的情况,这是一个递归的过程
  22. error = __vfs_follow_link(nd, s);
  23. if (dentry->d_inode->i_op->put_link)
  24. dentry->d_inode->i_op->put_link(dentry, nd, cookie);
  25. }
  26. path_put(path);
  27. return error;
  28. }
  1. static __always_inline int __vfs_follow_link(struct nameidata *nd, constchar *link)
  2. {
  3. int res = 0;
  4. char *name;
  5. if (IS_ERR(link))
  6. goto fail;
  7. if (*link == '/') {
  8. set_root(nd);
  9. path_put(&nd->path);
  10. nd->path = nd->root;
  11. path_get(&nd->root);
  12. }
  13. res = link_path_walk(link, nd);
  14. if (nd->depth || res || nd->last_type!=LAST_NORM)
  15. return res;
  16. /*
  17. * If it is an iterative symlinks resolution in open_namei() we
  18. * have to copy the last component. And all that crap because of
  19. * bloody create() on broken symlinks. Furrfu...
  20. */
  21. name = __getname();
  22. if (unlikely(!name)) {
  23. path_put(&nd->path);
  24. return -ENOMEM;
  25. }
  26. strcpy(name, nd->last.name);
  27. nd->last.name = name;
  28. return 0;
  29. fail:
  30. path_put(&nd->path);
  31. return PTR_ERR(link);

相关内容