fid.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * V9FS FID Management
  4. *
  5. * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
  6. * Copyright (C) 2005, 2006 by Eric Van Hensbergen <ericvh@gmail.com>
  7. */
  8. #include <linux/module.h>
  9. #include <linux/errno.h>
  10. #include <linux/fs.h>
  11. #include <linux/slab.h>
  12. #include <linux/sched.h>
  13. #include <linux/idr.h>
  14. #include <net/9p/9p.h>
  15. #include <net/9p/client.h>
  16. #include "v9fs.h"
  17. #include "v9fs_vfs.h"
  18. #include "fid.h"
  19. /**
  20. * v9fs_fid_add - add a fid to a dentry
  21. * @dentry: dentry that the fid is being added to
  22. * @fid: fid to add
  23. *
  24. */
  25. static inline void __add_fid(struct dentry *dentry, struct p9_fid *fid)
  26. {
  27. hlist_add_head(&fid->dlist, (struct hlist_head *)&dentry->d_fsdata);
  28. }
  29. void v9fs_fid_add(struct dentry *dentry, struct p9_fid *fid)
  30. {
  31. spin_lock(&dentry->d_lock);
  32. __add_fid(dentry, fid);
  33. spin_unlock(&dentry->d_lock);
  34. }
  35. /**
  36. * v9fs_fid_find - retrieve a fid that belongs to the specified uid
  37. * @dentry: dentry to look for fid in
  38. * @uid: return fid that belongs to the specified user
  39. * @any: if non-zero, return any fid associated with the dentry
  40. *
  41. */
  42. static struct p9_fid *v9fs_fid_find(struct dentry *dentry, kuid_t uid, int any)
  43. {
  44. struct p9_fid *fid, *ret;
  45. p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p) uid %d any %d\n",
  46. dentry, dentry, from_kuid(&init_user_ns, uid),
  47. any);
  48. ret = NULL;
  49. /* we'll recheck under lock if there's anything to look in */
  50. if (dentry->d_fsdata) {
  51. struct hlist_head *h = (struct hlist_head *)&dentry->d_fsdata;
  52. spin_lock(&dentry->d_lock);
  53. hlist_for_each_entry(fid, h, dlist) {
  54. if (any || uid_eq(fid->uid, uid)) {
  55. ret = fid;
  56. break;
  57. }
  58. }
  59. spin_unlock(&dentry->d_lock);
  60. }
  61. return ret;
  62. }
  63. /*
  64. * We need to hold v9ses->rename_sem as long as we hold references
  65. * to returned path array. Array element contain pointers to
  66. * dentry names.
  67. */
  68. static int build_path_from_dentry(struct v9fs_session_info *v9ses,
  69. struct dentry *dentry, const unsigned char ***names)
  70. {
  71. int n = 0, i;
  72. const unsigned char **wnames;
  73. struct dentry *ds;
  74. for (ds = dentry; !IS_ROOT(ds); ds = ds->d_parent)
  75. n++;
  76. wnames = kmalloc_array(n, sizeof(char *), GFP_KERNEL);
  77. if (!wnames)
  78. goto err_out;
  79. for (ds = dentry, i = (n-1); i >= 0; i--, ds = ds->d_parent)
  80. wnames[i] = ds->d_name.name;
  81. *names = wnames;
  82. return n;
  83. err_out:
  84. return -ENOMEM;
  85. }
  86. static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
  87. kuid_t uid, int any)
  88. {
  89. struct dentry *ds;
  90. const unsigned char **wnames, *uname;
  91. int i, n, l, clone, access;
  92. struct v9fs_session_info *v9ses;
  93. struct p9_fid *fid, *old_fid = NULL;
  94. v9ses = v9fs_dentry2v9ses(dentry);
  95. access = v9ses->flags & V9FS_ACCESS_MASK;
  96. fid = v9fs_fid_find(dentry, uid, any);
  97. if (fid)
  98. return fid;
  99. /*
  100. * we don't have a matching fid. To do a TWALK we need
  101. * parent fid. We need to prevent rename when we want to
  102. * look at the parent.
  103. */
  104. down_read(&v9ses->rename_sem);
  105. ds = dentry->d_parent;
  106. fid = v9fs_fid_find(ds, uid, any);
  107. if (fid) {
  108. /* Found the parent fid do a lookup with that */
  109. fid = p9_client_walk(fid, 1, &dentry->d_name.name, 1);
  110. goto fid_out;
  111. }
  112. up_read(&v9ses->rename_sem);
  113. /* start from the root and try to do a lookup */
  114. fid = v9fs_fid_find(dentry->d_sb->s_root, uid, any);
  115. if (!fid) {
  116. /* the user is not attached to the fs yet */
  117. if (access == V9FS_ACCESS_SINGLE)
  118. return ERR_PTR(-EPERM);
  119. if (v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses))
  120. uname = NULL;
  121. else
  122. uname = v9ses->uname;
  123. fid = p9_client_attach(v9ses->clnt, NULL, uname, uid,
  124. v9ses->aname);
  125. if (IS_ERR(fid))
  126. return fid;
  127. v9fs_fid_add(dentry->d_sb->s_root, fid);
  128. }
  129. /* If we are root ourself just return that */
  130. if (dentry->d_sb->s_root == dentry)
  131. return fid;
  132. /*
  133. * Do a multipath walk with attached root.
  134. * When walking parent we need to make sure we
  135. * don't have a parallel rename happening
  136. */
  137. down_read(&v9ses->rename_sem);
  138. n = build_path_from_dentry(v9ses, dentry, &wnames);
  139. if (n < 0) {
  140. fid = ERR_PTR(n);
  141. goto err_out;
  142. }
  143. clone = 1;
  144. i = 0;
  145. while (i < n) {
  146. l = min(n - i, P9_MAXWELEM);
  147. /*
  148. * We need to hold rename lock when doing a multipath
  149. * walk to ensure none of the patch component change
  150. */
  151. fid = p9_client_walk(fid, l, &wnames[i], clone);
  152. if (IS_ERR(fid)) {
  153. if (old_fid) {
  154. /*
  155. * If we fail, clunk fid which are mapping
  156. * to path component and not the last component
  157. * of the path.
  158. */
  159. p9_client_clunk(old_fid);
  160. }
  161. kfree(wnames);
  162. goto err_out;
  163. }
  164. old_fid = fid;
  165. i += l;
  166. clone = 0;
  167. }
  168. kfree(wnames);
  169. fid_out:
  170. if (!IS_ERR(fid)) {
  171. spin_lock(&dentry->d_lock);
  172. if (d_unhashed(dentry)) {
  173. spin_unlock(&dentry->d_lock);
  174. p9_client_clunk(fid);
  175. fid = ERR_PTR(-ENOENT);
  176. } else {
  177. __add_fid(dentry, fid);
  178. spin_unlock(&dentry->d_lock);
  179. }
  180. }
  181. err_out:
  182. up_read(&v9ses->rename_sem);
  183. return fid;
  184. }
  185. /**
  186. * v9fs_fid_lookup - lookup for a fid, try to walk if not found
  187. * @dentry: dentry to look for fid in
  188. *
  189. * Look for a fid in the specified dentry for the current user.
  190. * If no fid is found, try to create one walking from a fid from the parent
  191. * dentry (if it has one), or the root dentry. If the user haven't accessed
  192. * the fs yet, attach now and walk from the root.
  193. */
  194. struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
  195. {
  196. kuid_t uid;
  197. int any, access;
  198. struct v9fs_session_info *v9ses;
  199. v9ses = v9fs_dentry2v9ses(dentry);
  200. access = v9ses->flags & V9FS_ACCESS_MASK;
  201. switch (access) {
  202. case V9FS_ACCESS_SINGLE:
  203. case V9FS_ACCESS_USER:
  204. case V9FS_ACCESS_CLIENT:
  205. uid = current_fsuid();
  206. any = 0;
  207. break;
  208. case V9FS_ACCESS_ANY:
  209. uid = v9ses->uid;
  210. any = 1;
  211. break;
  212. default:
  213. uid = INVALID_UID;
  214. any = 0;
  215. break;
  216. }
  217. return v9fs_fid_lookup_with_uid(dentry, uid, any);
  218. }
  219. struct p9_fid *v9fs_writeback_fid(struct dentry *dentry)
  220. {
  221. int err;
  222. struct p9_fid *fid;
  223. fid = clone_fid(v9fs_fid_lookup_with_uid(dentry, GLOBAL_ROOT_UID, 0));
  224. if (IS_ERR(fid))
  225. goto error_out;
  226. /*
  227. * writeback fid will only be used to write back the
  228. * dirty pages. We always request for the open fid in read-write
  229. * mode so that a partial page write which result in page
  230. * read can work.
  231. */
  232. err = p9_client_open(fid, O_RDWR);
  233. if (err < 0) {
  234. p9_client_clunk(fid);
  235. fid = ERR_PTR(err);
  236. goto error_out;
  237. }
  238. error_out:
  239. return fid;
  240. }