fid.c 7.2 KB

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