fid.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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 <net/9p/9p.h>
  14. #include <net/9p/client.h>
  15. #include "v9fs.h"
  16. #include "v9fs_vfs.h"
  17. #include "fid.h"
  18. static inline void __add_fid(struct dentry *dentry, struct p9_fid *fid)
  19. {
  20. hlist_add_head(&fid->dlist, (struct hlist_head *)&dentry->d_fsdata);
  21. }
  22. /**
  23. * v9fs_fid_add - add a fid to a dentry
  24. * @dentry: dentry that the fid is being added to
  25. * @pfid: fid to add, NULLed out
  26. *
  27. */
  28. void v9fs_fid_add(struct dentry *dentry, struct p9_fid **pfid)
  29. {
  30. struct p9_fid *fid = *pfid;
  31. spin_lock(&dentry->d_lock);
  32. __add_fid(dentry, fid);
  33. spin_unlock(&dentry->d_lock);
  34. *pfid = NULL;
  35. }
  36. static bool v9fs_is_writeable(int mode)
  37. {
  38. if (mode & (P9_OWRITE|P9_ORDWR))
  39. return true;
  40. else
  41. return false;
  42. }
  43. /**
  44. * v9fs_fid_find_inode - search for an open fid off of the inode list
  45. * @inode: return a fid pointing to a specific inode
  46. * @want_writeable: only consider fids which are writeable
  47. * @uid: return a fid belonging to the specified user
  48. * @any: ignore uid as a selection criteria
  49. *
  50. */
  51. struct p9_fid *v9fs_fid_find_inode(struct inode *inode, bool want_writeable,
  52. kuid_t uid, bool any)
  53. {
  54. struct hlist_head *h;
  55. struct p9_fid *fid, *ret = NULL;
  56. p9_debug(P9_DEBUG_VFS, " inode: %p\n", inode);
  57. spin_lock(&inode->i_lock);
  58. h = (struct hlist_head *)&inode->i_private;
  59. hlist_for_each_entry(fid, h, ilist) {
  60. if (any || uid_eq(fid->uid, uid)) {
  61. if (want_writeable && !v9fs_is_writeable(fid->mode)) {
  62. p9_debug(P9_DEBUG_VFS, " mode: %x not writeable?\n",
  63. fid->mode);
  64. continue;
  65. }
  66. p9_fid_get(fid);
  67. ret = fid;
  68. break;
  69. }
  70. }
  71. spin_unlock(&inode->i_lock);
  72. return ret;
  73. }
  74. /**
  75. * v9fs_open_fid_add - add an open fid to an inode
  76. * @inode: inode that the fid is being added to
  77. * @pfid: fid to add, NULLed out
  78. *
  79. */
  80. void v9fs_open_fid_add(struct inode *inode, struct p9_fid **pfid)
  81. {
  82. struct p9_fid *fid = *pfid;
  83. spin_lock(&inode->i_lock);
  84. hlist_add_head(&fid->ilist, (struct hlist_head *)&inode->i_private);
  85. spin_unlock(&inode->i_lock);
  86. *pfid = NULL;
  87. }
  88. /**
  89. * v9fs_fid_find - retrieve a fid that belongs to the specified uid
  90. * @dentry: dentry to look for fid in
  91. * @uid: return fid that belongs to the specified user
  92. * @any: if non-zero, return any fid associated with the dentry
  93. *
  94. */
  95. static struct p9_fid *v9fs_fid_find(struct dentry *dentry, kuid_t uid, int any)
  96. {
  97. struct p9_fid *fid, *ret;
  98. p9_debug(P9_DEBUG_VFS, " dentry: %pd (%p) uid %d any %d\n",
  99. dentry, dentry, from_kuid(&init_user_ns, uid),
  100. any);
  101. ret = NULL;
  102. /* we'll recheck under lock if there's anything to look in */
  103. if (dentry->d_fsdata) {
  104. struct hlist_head *h = (struct hlist_head *)&dentry->d_fsdata;
  105. spin_lock(&dentry->d_lock);
  106. hlist_for_each_entry(fid, h, dlist) {
  107. if (any || uid_eq(fid->uid, uid)) {
  108. ret = fid;
  109. p9_fid_get(ret);
  110. break;
  111. }
  112. }
  113. spin_unlock(&dentry->d_lock);
  114. }
  115. if (!ret && dentry->d_inode)
  116. ret = v9fs_fid_find_inode(dentry->d_inode, false, uid, any);
  117. return ret;
  118. }
  119. /*
  120. * We need to hold v9ses->rename_sem as long as we hold references
  121. * to returned path array. Array element contain pointers to
  122. * dentry names.
  123. */
  124. static int build_path_from_dentry(struct v9fs_session_info *v9ses,
  125. struct dentry *dentry, const unsigned char ***names)
  126. {
  127. int n = 0, i;
  128. const unsigned char **wnames;
  129. struct dentry *ds;
  130. for (ds = dentry; !IS_ROOT(ds); ds = ds->d_parent)
  131. n++;
  132. wnames = kmalloc_array(n, sizeof(char *), GFP_KERNEL);
  133. if (!wnames)
  134. goto err_out;
  135. for (ds = dentry, i = (n-1); i >= 0; i--, ds = ds->d_parent)
  136. wnames[i] = ds->d_name.name;
  137. *names = wnames;
  138. return n;
  139. err_out:
  140. return -ENOMEM;
  141. }
  142. static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
  143. kuid_t uid, int any)
  144. {
  145. struct dentry *ds;
  146. const unsigned char **wnames, *uname;
  147. int i, n, l, access;
  148. struct v9fs_session_info *v9ses;
  149. struct p9_fid *fid, *root_fid, *old_fid;
  150. v9ses = v9fs_dentry2v9ses(dentry);
  151. access = v9ses->flags & V9FS_ACCESS_MASK;
  152. fid = v9fs_fid_find(dentry, uid, any);
  153. if (fid)
  154. return fid;
  155. /*
  156. * we don't have a matching fid. To do a TWALK we need
  157. * parent fid. We need to prevent rename when we want to
  158. * look at the parent.
  159. */
  160. down_read(&v9ses->rename_sem);
  161. ds = dentry->d_parent;
  162. fid = v9fs_fid_find(ds, uid, any);
  163. if (fid) {
  164. /* Found the parent fid do a lookup with that */
  165. old_fid = fid;
  166. fid = p9_client_walk(old_fid, 1, &dentry->d_name.name, 1);
  167. p9_fid_put(old_fid);
  168. goto fid_out;
  169. }
  170. up_read(&v9ses->rename_sem);
  171. /* start from the root and try to do a lookup */
  172. root_fid = v9fs_fid_find(dentry->d_sb->s_root, uid, any);
  173. if (!root_fid) {
  174. /* the user is not attached to the fs yet */
  175. if (access == V9FS_ACCESS_SINGLE)
  176. return ERR_PTR(-EPERM);
  177. if (v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses))
  178. uname = NULL;
  179. else
  180. uname = v9ses->uname;
  181. fid = p9_client_attach(v9ses->clnt, NULL, uname, uid,
  182. v9ses->aname);
  183. if (IS_ERR(fid))
  184. return fid;
  185. root_fid = p9_fid_get(fid);
  186. v9fs_fid_add(dentry->d_sb->s_root, &fid);
  187. }
  188. /* If we are root ourself just return that */
  189. if (dentry->d_sb->s_root == dentry)
  190. return root_fid;
  191. /*
  192. * Do a multipath walk with attached root.
  193. * When walking parent we need to make sure we
  194. * don't have a parallel rename happening
  195. */
  196. down_read(&v9ses->rename_sem);
  197. n = build_path_from_dentry(v9ses, dentry, &wnames);
  198. if (n < 0) {
  199. fid = ERR_PTR(n);
  200. goto err_out;
  201. }
  202. fid = root_fid;
  203. old_fid = root_fid;
  204. i = 0;
  205. while (i < n) {
  206. l = min(n - i, P9_MAXWELEM);
  207. /*
  208. * We need to hold rename lock when doing a multipath
  209. * walk to ensure none of the path components change
  210. */
  211. fid = p9_client_walk(old_fid, l, &wnames[i],
  212. old_fid == root_fid /* clone */);
  213. /* non-cloning walk will return the same fid */
  214. if (fid != old_fid) {
  215. p9_fid_put(old_fid);
  216. old_fid = fid;
  217. }
  218. if (IS_ERR(fid)) {
  219. kfree(wnames);
  220. goto err_out;
  221. }
  222. i += l;
  223. }
  224. kfree(wnames);
  225. fid_out:
  226. if (!IS_ERR(fid)) {
  227. spin_lock(&dentry->d_lock);
  228. if (d_unhashed(dentry)) {
  229. spin_unlock(&dentry->d_lock);
  230. p9_fid_put(fid);
  231. fid = ERR_PTR(-ENOENT);
  232. } else {
  233. __add_fid(dentry, fid);
  234. p9_fid_get(fid);
  235. spin_unlock(&dentry->d_lock);
  236. }
  237. }
  238. err_out:
  239. up_read(&v9ses->rename_sem);
  240. return fid;
  241. }
  242. /**
  243. * v9fs_fid_lookup - lookup for a fid, try to walk if not found
  244. * @dentry: dentry to look for fid in
  245. *
  246. * Look for a fid in the specified dentry for the current user.
  247. * If no fid is found, try to create one walking from a fid from the parent
  248. * dentry (if it has one), or the root dentry. If the user haven't accessed
  249. * the fs yet, attach now and walk from the root.
  250. */
  251. struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
  252. {
  253. kuid_t uid;
  254. int any, access;
  255. struct v9fs_session_info *v9ses;
  256. v9ses = v9fs_dentry2v9ses(dentry);
  257. access = v9ses->flags & V9FS_ACCESS_MASK;
  258. switch (access) {
  259. case V9FS_ACCESS_SINGLE:
  260. case V9FS_ACCESS_USER:
  261. case V9FS_ACCESS_CLIENT:
  262. uid = current_fsuid();
  263. any = 0;
  264. break;
  265. case V9FS_ACCESS_ANY:
  266. uid = v9ses->uid;
  267. any = 1;
  268. break;
  269. default:
  270. uid = INVALID_UID;
  271. any = 0;
  272. break;
  273. }
  274. return v9fs_fid_lookup_with_uid(dentry, uid, any);
  275. }