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

Linux虚拟文件系统--文件路径名的解析(3)--普通文件名

[日期:2012-11-22] 来源:Linux社区  作者:vanbreaker [字体:大 中 小]

d_hash()函数将父目录dentry的地址和所要查找的文件名的哈希值组合起来,重新构建一个哈希值,并根据其定位到

dentry_hashtable哈希表中,dentry_hashtable是dentry缓存的一部分,所有的dentry都会保存在dentry_hashtable中,这样一来,就得到了一个哈希表的溢出链表的表头,即代码中的head变量。下面的工作就是扫描这个链表,并从中查找真正的目标。

如果在dentry_hashtable中没能找到目标dentry,则通过real_lookup()函数从磁盘中查找

  1. staticstruct dentry * real_lookup(struct dentry * parent, struct qstr * name, struct nameidata *nd)
  2. {
  3. struct dentry * result;
  4. struct inode *dir = parent->d_inode;
  5. mutex_lock(&dir->i_mutex);
  6. /*
  7. * First re-do the cached lookup just in case it was created
  8. * while we waited for the directory semaphore..
  9. *
  10. * FIXME! This could use version numbering or similar to
  11. * avoid unnecessary cache lookups.
  12. *
  13. * The "dcache_lock" is purely to protect the RCU list walker
  14. * from concurrent renames at this point (we mustn't get false
  15. * negatives from the RCU list walk here, unlike the optimistic
  16. * fast walk).
  17. *
  18. * so doing d_lookup() (with seqlock), instead of lockfree __d_lookup
  19. */
  20. //再从磁盘读取前再进行一次d_lookup尝试,因为之前等待互斥锁时有可能已经创建了相应的dentry
  21. result = d_lookup(parent, name);
  22. /*下面进行dentry的创建*/
  23. if (!result) {
  24. struct dentry *dentry;
  25. /* Don't create child dentry for a dead directory. */
  26. result = ERR_PTR(-ENOENT);
  27. if (IS_DEADDIR(dir))
  28. goto out_unlock;
  29. /*分配一个dentry并进行相应的初始化*/
  30. dentry = d_alloc(parent, name);
  31. result = ERR_PTR(-ENOMEM);
  32. if (dentry) {
  33. /*调用特定于文件系统的lookup函数从磁盘中读取数据并将dentry添入散列表*/
  34. result = dir->i_op->lookup(dir, dentry, nd);
  35. if (result)
  36. dput(dentry);
  37. else
  38. result = dentry;
  39. }
  40. out_unlock:
  41. mutex_unlock(&dir->i_mutex);
  42. return result;
  43. }
  44. /*
  45. * Uhhuh! Nasty case: the cache was re-populated while
  46. * we waited on the semaphore. Need to revalidate.
  47. */
  48. mutex_unlock(&dir->i_mutex);
  49. if (result->d_op && result->d_op->d_revalidate) {
  50. result = do_revalidate(result, nd);
  51. if (!result)
  52. result = ERR_PTR(-ENOENT);
  53. }
  54. return result;
  55. }
linux
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

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