fid.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /*
  2. * V9FS FID Management
  3. *
  4. * Copyright (C) 2005 by Eric Van Hensbergen <ericvh@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  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/config.h>
  24. #include <linux/module.h>
  25. #include <linux/errno.h>
  26. #include <linux/fs.h>
  27. #include <linux/idr.h>
  28. #include "debug.h"
  29. #include "v9fs.h"
  30. #include "9p.h"
  31. #include "v9fs_vfs.h"
  32. #include "transport.h"
  33. #include "mux.h"
  34. #include "conv.h"
  35. #include "fid.h"
  36. /**
  37. * v9fs_fid_insert - add a fid to a dentry
  38. * @fid: fid to add
  39. * @dentry: dentry that it is being added to
  40. *
  41. */
  42. static int v9fs_fid_insert(struct v9fs_fid *fid, struct dentry *dentry)
  43. {
  44. struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
  45. dprintk(DEBUG_9P, "fid %d (%p) dentry %s (%p)\n", fid->fid, fid,
  46. dentry->d_iname, dentry);
  47. if (dentry->d_fsdata == NULL) {
  48. dentry->d_fsdata =
  49. kmalloc(sizeof(struct list_head), GFP_KERNEL);
  50. if (dentry->d_fsdata == NULL) {
  51. dprintk(DEBUG_ERROR, "Out of memory\n");
  52. return -ENOMEM;
  53. }
  54. fid_list = (struct list_head *)dentry->d_fsdata;
  55. INIT_LIST_HEAD(fid_list); /* Initialize list head */
  56. }
  57. fid->uid = current->uid;
  58. fid->pid = current->pid;
  59. list_add(&fid->list, fid_list);
  60. return 0;
  61. }
  62. /**
  63. * v9fs_fid_create - allocate a FID structure
  64. * @dentry - dentry to link newly created fid to
  65. *
  66. */
  67. struct v9fs_fid *v9fs_fid_create(struct dentry *dentry,
  68. struct v9fs_session_info *v9ses, int fid, int create)
  69. {
  70. struct v9fs_fid *new;
  71. dprintk(DEBUG_9P, "fid create dentry %p, fid %d, create %d\n",
  72. dentry, fid, create);
  73. new = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
  74. if (new == NULL) {
  75. dprintk(DEBUG_ERROR, "Out of Memory\n");
  76. return ERR_PTR(-ENOMEM);
  77. }
  78. new->fid = fid;
  79. new->v9ses = v9ses;
  80. new->fidopen = 0;
  81. new->fidcreate = create;
  82. new->fidclunked = 0;
  83. new->iounit = 0;
  84. new->rdir_pos = 0;
  85. new->rdir_fcall = NULL;
  86. if (v9fs_fid_insert(new, dentry) == 0)
  87. return new;
  88. else {
  89. dprintk(DEBUG_ERROR, "Problems inserting to dentry\n");
  90. kfree(new);
  91. return NULL;
  92. }
  93. }
  94. /**
  95. * v9fs_fid_destroy - deallocate a FID structure
  96. * @fid: fid to destroy
  97. *
  98. */
  99. void v9fs_fid_destroy(struct v9fs_fid *fid)
  100. {
  101. list_del(&fid->list);
  102. kfree(fid);
  103. }
  104. /**
  105. * v9fs_fid_walk_up - walks from the process current directory
  106. * up to the specified dentry.
  107. */
  108. static struct v9fs_fid *v9fs_fid_walk_up(struct dentry *dentry)
  109. {
  110. int fidnum, cfidnum, err;
  111. struct v9fs_fid *cfid;
  112. struct dentry *cde;
  113. struct v9fs_session_info *v9ses;
  114. v9ses = v9fs_inode2v9ses(current->fs->pwd->d_inode);
  115. cfid = v9fs_fid_lookup(current->fs->pwd);
  116. if (cfid == NULL) {
  117. dprintk(DEBUG_ERROR, "process cwd doesn't have a fid\n");
  118. return ERR_PTR(-ENOENT);
  119. }
  120. cfidnum = cfid->fid;
  121. cde = current->fs->pwd;
  122. /* TODO: take advantage of multiwalk */
  123. fidnum = v9fs_get_idpool(&v9ses->fidpool);
  124. if (fidnum < 0) {
  125. dprintk(DEBUG_ERROR, "could not get a new fid num\n");
  126. err = -ENOENT;
  127. goto clunk_fid;
  128. }
  129. while (cde != dentry) {
  130. if (cde == cde->d_parent) {
  131. dprintk(DEBUG_ERROR, "can't find dentry\n");
  132. err = -ENOENT;
  133. goto clunk_fid;
  134. }
  135. err = v9fs_t_walk(v9ses, cfidnum, fidnum, "..", NULL);
  136. if (err < 0) {
  137. dprintk(DEBUG_ERROR, "problem walking to parent\n");
  138. goto clunk_fid;
  139. }
  140. cfidnum = fidnum;
  141. cde = cde->d_parent;
  142. }
  143. return v9fs_fid_create(dentry, v9ses, fidnum, 0);
  144. clunk_fid:
  145. v9fs_t_clunk(v9ses, fidnum, NULL);
  146. return ERR_PTR(err);
  147. }
  148. /**
  149. * v9fs_fid_lookup - retrieve the right fid from a particular dentry
  150. * @dentry: dentry to look for fid in
  151. * @type: intent of lookup (operation or traversal)
  152. *
  153. * search list of fids associated with a dentry for a fid with a matching
  154. * thread id or uid. If that fails, look up the dentry's parents to see if you
  155. * can find a matching fid.
  156. *
  157. */
  158. struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry)
  159. {
  160. struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
  161. struct v9fs_fid *current_fid = NULL;
  162. struct v9fs_fid *temp = NULL;
  163. struct v9fs_fid *return_fid = NULL;
  164. dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry);
  165. if (fid_list) {
  166. list_for_each_entry_safe(current_fid, temp, fid_list, list) {
  167. if (!current_fid->fidcreate) {
  168. return_fid = current_fid;
  169. break;
  170. }
  171. }
  172. if (!return_fid)
  173. return_fid = current_fid;
  174. }
  175. /* we are at the root but didn't match */
  176. if ((!return_fid) && (dentry->d_parent == dentry)) {
  177. /* TODO: clone attach with new uid */
  178. return_fid = current_fid;
  179. }
  180. if (!return_fid) {
  181. struct dentry *par = current->fs->pwd->d_parent;
  182. int count = 1;
  183. while (par != NULL) {
  184. if (par == dentry)
  185. break;
  186. count++;
  187. if (par == par->d_parent) {
  188. dprintk(DEBUG_ERROR,
  189. "got to root without finding dentry\n");
  190. break;
  191. }
  192. par = par->d_parent;
  193. }
  194. /* XXX - there may be some duplication we can get rid of */
  195. if (par == dentry) {
  196. return_fid = v9fs_fid_walk_up(dentry);
  197. if (IS_ERR(return_fid))
  198. return_fid = NULL;
  199. }
  200. }
  201. return return_fid;
  202. }
  203. struct v9fs_fid *v9fs_fid_get_created(struct dentry *dentry)
  204. {
  205. struct list_head *fid_list;
  206. struct v9fs_fid *fid, *ftmp, *ret;
  207. dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry);
  208. fid_list = (struct list_head *)dentry->d_fsdata;
  209. ret = NULL;
  210. if (fid_list) {
  211. list_for_each_entry_safe(fid, ftmp, fid_list, list) {
  212. if (fid->fidcreate && fid->pid == current->pid) {
  213. list_del(&fid->list);
  214. ret = fid;
  215. break;
  216. }
  217. }
  218. }
  219. dprintk(DEBUG_9P, "return %p\n", ret);
  220. return ret;
  221. }