vfs_dentry.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * This file contians vfs dentry ops for the 9P2000 protocol.
  4. *
  5. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  6. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/file.h>
  12. #include <linux/pagemap.h>
  13. #include <linux/stat.h>
  14. #include <linux/string.h>
  15. #include <linux/namei.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <net/9p/9p.h>
  19. #include <net/9p/client.h>
  20. #include "v9fs.h"
  21. #include "v9fs_vfs.h"
  22. #include "fid.h"
  23. /**
  24. * v9fs_cached_dentry_delete - called when dentry refcount equals 0
  25. * @dentry: dentry in question
  26. *
  27. */
  28. static int v9fs_cached_dentry_delete(const struct dentry *dentry)
  29. {
  30. p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p)\n",
  31. dentry, dentry);
  32. /* Don't cache negative dentries */
  33. if (d_really_is_negative(dentry))
  34. return 1;
  35. return 0;
  36. }
  37. /**
  38. * v9fs_dentry_release - called when dentry is going to be freed
  39. * @dentry: dentry that is being release
  40. *
  41. */
  42. static void v9fs_dentry_release(struct dentry *dentry)
  43. {
  44. struct hlist_node *p, *n;
  45. struct hlist_head head;
  46. p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p)\n",
  47. dentry, dentry);
  48. spin_lock(&dentry->d_lock);
  49. hlist_move_list((struct hlist_head *)&dentry->d_fsdata, &head);
  50. spin_unlock(&dentry->d_lock);
  51. hlist_for_each_safe(p, n, &head)
  52. p9_fid_put(hlist_entry(p, struct p9_fid, dlist));
  53. }
  54. static int __v9fs_lookup_revalidate(struct dentry *dentry, unsigned int flags)
  55. {
  56. struct p9_fid *fid;
  57. struct inode *inode;
  58. struct v9fs_inode *v9inode;
  59. if (flags & LOOKUP_RCU)
  60. return -ECHILD;
  61. inode = d_inode(dentry);
  62. if (!inode)
  63. goto out_valid;
  64. v9inode = V9FS_I(inode);
  65. if (v9inode->cache_validity & V9FS_INO_INVALID_ATTR) {
  66. int retval;
  67. struct v9fs_session_info *v9ses;
  68. fid = v9fs_fid_lookup(dentry);
  69. if (IS_ERR(fid)) {
  70. p9_debug(
  71. P9_DEBUG_VFS,
  72. "v9fs_fid_lookup: dentry = %pd (%p), got error %pe\n",
  73. dentry, dentry, fid);
  74. return PTR_ERR(fid);
  75. }
  76. v9ses = v9fs_inode2v9ses(inode);
  77. if (v9fs_proto_dotl(v9ses))
  78. retval = v9fs_refresh_inode_dotl(fid, inode);
  79. else
  80. retval = v9fs_refresh_inode(fid, inode);
  81. p9_fid_put(fid);
  82. if (retval == -ENOENT) {
  83. p9_debug(P9_DEBUG_VFS, "dentry: %pd (%p) invalidated due to ENOENT\n",
  84. dentry, dentry);
  85. return 0;
  86. }
  87. if (v9inode->cache_validity & V9FS_INO_INVALID_ATTR) {
  88. p9_debug(P9_DEBUG_VFS, "dentry: %pd (%p) invalidated due to type change\n",
  89. dentry, dentry);
  90. return 0;
  91. }
  92. if (retval < 0) {
  93. p9_debug(P9_DEBUG_VFS,
  94. "refresh inode: dentry = %pd (%p), got error %pe\n",
  95. dentry, dentry, ERR_PTR(retval));
  96. return retval;
  97. }
  98. }
  99. out_valid:
  100. p9_debug(P9_DEBUG_VFS, "dentry: %pd (%p) is valid\n", dentry, dentry);
  101. return 1;
  102. }
  103. static int v9fs_lookup_revalidate(struct inode *dir, const struct qstr *name,
  104. struct dentry *dentry, unsigned int flags)
  105. {
  106. return __v9fs_lookup_revalidate(dentry, flags);
  107. }
  108. static bool v9fs_dentry_unalias_trylock(const struct dentry *dentry)
  109. {
  110. struct v9fs_session_info *v9ses = v9fs_dentry2v9ses(dentry);
  111. return down_write_trylock(&v9ses->rename_sem);
  112. }
  113. static void v9fs_dentry_unalias_unlock(const struct dentry *dentry)
  114. {
  115. struct v9fs_session_info *v9ses = v9fs_dentry2v9ses(dentry);
  116. up_write(&v9ses->rename_sem);
  117. }
  118. const struct dentry_operations v9fs_cached_dentry_operations = {
  119. .d_revalidate = v9fs_lookup_revalidate,
  120. .d_weak_revalidate = __v9fs_lookup_revalidate,
  121. .d_delete = v9fs_cached_dentry_delete,
  122. .d_release = v9fs_dentry_release,
  123. .d_unalias_trylock = v9fs_dentry_unalias_trylock,
  124. .d_unalias_unlock = v9fs_dentry_unalias_unlock,
  125. };
  126. const struct dentry_operations v9fs_dentry_operations = {
  127. .d_release = v9fs_dentry_release,
  128. .d_unalias_trylock = v9fs_dentry_unalias_trylock,
  129. .d_unalias_unlock = v9fs_dentry_unalias_unlock,
  130. };