fid.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. 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 v9fs_session_info *v9ses, int fid)
  65. {
  66. struct v9fs_fid *new;
  67. dprintk(DEBUG_9P, "fid create fid %d\n", fid);
  68. new = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
  69. if (new == NULL) {
  70. dprintk(DEBUG_ERROR, "Out of Memory\n");
  71. return ERR_PTR(-ENOMEM);
  72. }
  73. new->fid = fid;
  74. new->v9ses = v9ses;
  75. new->fidopen = 0;
  76. new->fidclunked = 0;
  77. new->iounit = 0;
  78. new->rdir_pos = 0;
  79. new->rdir_fcall = NULL;
  80. INIT_LIST_HEAD(&new->list);
  81. return new;
  82. }
  83. /**
  84. * v9fs_fid_destroy - deallocate a FID structure
  85. * @fid: fid to destroy
  86. *
  87. */
  88. void v9fs_fid_destroy(struct v9fs_fid *fid)
  89. {
  90. list_del(&fid->list);
  91. kfree(fid);
  92. }
  93. /**
  94. * v9fs_fid_walk_up - walks from the process current directory
  95. * up to the specified dentry.
  96. */
  97. static struct v9fs_fid *v9fs_fid_walk_up(struct dentry *dentry)
  98. {
  99. int fidnum, cfidnum, err;
  100. struct v9fs_fid *cfid, *fid;
  101. struct dentry *cde;
  102. struct v9fs_session_info *v9ses;
  103. v9ses = v9fs_inode2v9ses(current->fs->pwd->d_inode);
  104. cfid = v9fs_fid_lookup(current->fs->pwd);
  105. if (cfid == NULL) {
  106. dprintk(DEBUG_ERROR, "process cwd doesn't have a fid\n");
  107. return ERR_PTR(-ENOENT);
  108. }
  109. cfidnum = cfid->fid;
  110. cde = current->fs->pwd;
  111. /* TODO: take advantage of multiwalk */
  112. fidnum = v9fs_get_idpool(&v9ses->fidpool);
  113. if (fidnum < 0) {
  114. dprintk(DEBUG_ERROR, "could not get a new fid num\n");
  115. err = -ENOENT;
  116. goto clunk_fid;
  117. }
  118. while (cde != dentry) {
  119. if (cde == cde->d_parent) {
  120. dprintk(DEBUG_ERROR, "can't find dentry\n");
  121. err = -ENOENT;
  122. goto clunk_fid;
  123. }
  124. err = v9fs_t_walk(v9ses, cfidnum, fidnum, "..", NULL);
  125. if (err < 0) {
  126. dprintk(DEBUG_ERROR, "problem walking to parent\n");
  127. goto clunk_fid;
  128. }
  129. cfidnum = fidnum;
  130. cde = cde->d_parent;
  131. }
  132. fid = v9fs_fid_create(v9ses, fidnum);
  133. if (fid) {
  134. err = v9fs_fid_insert(fid, dentry);
  135. if (err < 0) {
  136. kfree(fid);
  137. goto clunk_fid;
  138. }
  139. }
  140. return fid;
  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 *return_fid = NULL;
  159. dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry);
  160. if (fid_list)
  161. return_fid = list_entry(fid_list->next, struct v9fs_fid, list);
  162. if (!return_fid) {
  163. struct dentry *par = current->fs->pwd->d_parent;
  164. int count = 1;
  165. while (par != NULL) {
  166. if (par == dentry)
  167. break;
  168. count++;
  169. if (par == par->d_parent) {
  170. dprintk(DEBUG_ERROR,
  171. "got to root without finding dentry\n");
  172. break;
  173. }
  174. par = par->d_parent;
  175. }
  176. /* XXX - there may be some duplication we can get rid of */
  177. if (par == dentry) {
  178. return_fid = v9fs_fid_walk_up(dentry);
  179. if (IS_ERR(return_fid))
  180. return_fid = NULL;
  181. }
  182. }
  183. return return_fid;
  184. }