fid.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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/sched.h>
  27. #include <linux/idr.h>
  28. #include <asm/semaphore.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_DPRINTK(P9_DEBUG_VFS, "fid %d dentry %s\n",
  44. fid->fid, dentry->d_iname);
  45. dent = dentry->d_fsdata;
  46. if (!dent) {
  47. dent = kmalloc(sizeof(struct v9fs_dentry), GFP_KERNEL);
  48. if (!dent)
  49. return -ENOMEM;
  50. spin_lock_init(&dent->lock);
  51. INIT_LIST_HEAD(&dent->fidlist);
  52. dentry->d_fsdata = dent;
  53. }
  54. spin_lock(&dent->lock);
  55. list_add(&fid->dlist, &dent->fidlist);
  56. spin_unlock(&dent->lock);
  57. return 0;
  58. }
  59. /**
  60. * v9fs_fid_find - retrieve a fid that belongs to the specified uid
  61. * @dentry: dentry to look for fid in
  62. * @uid: return fid that belongs to the specified user
  63. * @any: if non-zero, return any fid associated with the dentry
  64. *
  65. */
  66. static struct p9_fid *v9fs_fid_find(struct dentry *dentry, u32 uid, int any)
  67. {
  68. struct v9fs_dentry *dent;
  69. struct p9_fid *fid, *ret;
  70. P9_DPRINTK(P9_DEBUG_VFS, " dentry: %s (%p) uid %d any %d\n",
  71. dentry->d_iname, dentry, uid, any);
  72. dent = (struct v9fs_dentry *) dentry->d_fsdata;
  73. ret = NULL;
  74. if (dent) {
  75. spin_lock(&dent->lock);
  76. list_for_each_entry(fid, &dent->fidlist, dlist) {
  77. if (any || fid->uid == uid) {
  78. ret = fid;
  79. break;
  80. }
  81. }
  82. spin_unlock(&dent->lock);
  83. }
  84. return ret;
  85. }
  86. /**
  87. * v9fs_fid_lookup - lookup for a fid, try to walk if not found
  88. * @dentry: dentry to look for fid in
  89. *
  90. * Look for a fid in the specified dentry for the current user.
  91. * If no fid is found, try to create one walking from a fid from the parent
  92. * dentry (if it has one), or the root dentry. If the user haven't accessed
  93. * the fs yet, attach now and walk from the root.
  94. */
  95. struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
  96. {
  97. int i, n, l, clone, any, access;
  98. u32 uid;
  99. struct p9_fid *fid;
  100. struct dentry *d, *ds;
  101. struct v9fs_session_info *v9ses;
  102. char **wnames, *uname;
  103. v9ses = v9fs_inode2v9ses(dentry->d_inode);
  104. access = v9ses->flags & V9FS_ACCESS_MASK;
  105. switch (access) {
  106. case V9FS_ACCESS_SINGLE:
  107. case V9FS_ACCESS_USER:
  108. uid = current->fsuid;
  109. any = 0;
  110. break;
  111. case V9FS_ACCESS_ANY:
  112. uid = v9ses->uid;
  113. any = 1;
  114. break;
  115. default:
  116. uid = ~0;
  117. any = 0;
  118. break;
  119. }
  120. fid = v9fs_fid_find(dentry, uid, any);
  121. if (fid)
  122. return fid;
  123. ds = dentry->d_parent;
  124. fid = v9fs_fid_find(ds, uid, any);
  125. if (!fid) { /* walk from the root */
  126. n = 0;
  127. for (ds = dentry; !IS_ROOT(ds); ds = ds->d_parent)
  128. n++;
  129. fid = v9fs_fid_find(ds, uid, any);
  130. if (!fid) { /* the user is not attached to the fs yet */
  131. if (access == V9FS_ACCESS_SINGLE)
  132. return ERR_PTR(-EPERM);
  133. if (v9fs_extended(v9ses))
  134. uname = NULL;
  135. else
  136. uname = v9ses->uname;
  137. fid = p9_client_attach(v9ses->clnt, NULL, uname, uid,
  138. v9ses->aname);
  139. if (IS_ERR(fid))
  140. return fid;
  141. v9fs_fid_add(ds, fid);
  142. }
  143. } else /* walk from the parent */
  144. n = 1;
  145. if (ds == dentry)
  146. return fid;
  147. wnames = kmalloc(sizeof(char *) * n, GFP_KERNEL);
  148. if (!wnames)
  149. return ERR_PTR(-ENOMEM);
  150. for (d = dentry, i = n; i >= 0; i--, d = d->d_parent)
  151. wnames[i] = (char *) d->d_name.name;
  152. clone = 1;
  153. i = 0;
  154. while (i < n) {
  155. l = min(n - i, P9_MAXWELEM);
  156. fid = p9_client_walk(fid, l, &wnames[i], clone);
  157. if (!fid) {
  158. kfree(wnames);
  159. return fid;
  160. }
  161. i += l;
  162. clone = 0;
  163. }
  164. kfree(wnames);
  165. v9fs_fid_add(dentry, fid);
  166. return fid;
  167. }
  168. struct p9_fid *v9fs_fid_clone(struct dentry *dentry)
  169. {
  170. struct p9_fid *fid, *ret;
  171. fid = v9fs_fid_lookup(dentry);
  172. if (IS_ERR(fid))
  173. return fid;
  174. ret = p9_client_walk(fid, 0, NULL, 1);
  175. return ret;
  176. }