fid.c 5.9 KB

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