fid.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. {
  69. struct v9fs_fid *new;
  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 = -1;
  76. new->fidopen = 0;
  77. new->fidcreate = 0;
  78. new->fidclunked = 0;
  79. new->iounit = 0;
  80. if (v9fs_fid_insert(new, dentry) == 0)
  81. return new;
  82. else {
  83. dprintk(DEBUG_ERROR, "Problems inserting to dentry\n");
  84. kfree(new);
  85. return NULL;
  86. }
  87. }
  88. /**
  89. * v9fs_fid_destroy - deallocate a FID structure
  90. * @fid: fid to destroy
  91. *
  92. */
  93. void v9fs_fid_destroy(struct v9fs_fid *fid)
  94. {
  95. list_del(&fid->list);
  96. kfree(fid);
  97. }
  98. /**
  99. * v9fs_fid_lookup - retrieve the right fid from a particular dentry
  100. * @dentry: dentry to look for fid in
  101. * @type: intent of lookup (operation or traversal)
  102. *
  103. * search list of fids associated with a dentry for a fid with a matching
  104. * thread id or uid. If that fails, look up the dentry's parents to see if you
  105. * can find a matching fid.
  106. *
  107. */
  108. struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry, int type)
  109. {
  110. struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
  111. struct v9fs_fid *current_fid = NULL;
  112. struct v9fs_fid *temp = NULL;
  113. struct v9fs_fid *return_fid = NULL;
  114. int found_parent = 0;
  115. int found_user = 0;
  116. dprintk(DEBUG_9P, " dentry: %s (%p) type %d\n", dentry->d_iname, dentry,
  117. type);
  118. if (fid_list && !list_empty(fid_list)) {
  119. list_for_each_entry_safe(current_fid, temp, fid_list, list) {
  120. if (current_fid->uid == current->uid) {
  121. if (return_fid == NULL) {
  122. if ((type == FID_OP)
  123. || (!current_fid->fidopen)) {
  124. return_fid = current_fid;
  125. found_user = 1;
  126. }
  127. }
  128. }
  129. if (current_fid->pid == current->real_parent->pid) {
  130. if ((return_fid == NULL) || (found_parent)
  131. || (found_user)) {
  132. if ((type == FID_OP)
  133. || (!current_fid->fidopen)) {
  134. return_fid = current_fid;
  135. found_parent = 1;
  136. found_user = 0;
  137. }
  138. }
  139. }
  140. if (current_fid->pid == current->pid) {
  141. if ((type == FID_OP) ||
  142. (!current_fid->fidopen)) {
  143. return_fid = current_fid;
  144. found_parent = 0;
  145. found_user = 0;
  146. }
  147. }
  148. }
  149. }
  150. /* we are at the root but didn't match */
  151. if ((!return_fid) && (dentry->d_parent == dentry)) {
  152. /* TODO: clone attach with new uid */
  153. return_fid = current_fid;
  154. }
  155. if (!return_fid) {
  156. struct dentry *par = current->fs->pwd->d_parent;
  157. int count = 1;
  158. while (par != NULL) {
  159. if (par == dentry)
  160. break;
  161. count++;
  162. if (par == par->d_parent) {
  163. dprintk(DEBUG_ERROR,
  164. "got to root without finding dentry\n");
  165. break;
  166. }
  167. par = par->d_parent;
  168. }
  169. /* XXX - there may be some duplication we can get rid of */
  170. if (par == dentry) {
  171. /* we need to fid_lookup the starting point */
  172. int fidnum = -1;
  173. int oldfid = -1;
  174. int result = -1;
  175. struct v9fs_session_info *v9ses =
  176. v9fs_inode2v9ses(current->fs->pwd->d_inode);
  177. current_fid =
  178. v9fs_fid_lookup(current->fs->pwd, FID_WALK);
  179. if (current_fid == NULL) {
  180. dprintk(DEBUG_ERROR,
  181. "process cwd doesn't have a fid\n");
  182. return return_fid;
  183. }
  184. oldfid = current_fid->fid;
  185. par = current->fs->pwd;
  186. /* TODO: take advantage of multiwalk */
  187. fidnum = v9fs_get_idpool(&v9ses->fidpool);
  188. if (fidnum < 0) {
  189. dprintk(DEBUG_ERROR,
  190. "could not get a new fid num\n");
  191. return return_fid;
  192. }
  193. while (par != dentry) {
  194. result =
  195. v9fs_t_walk(v9ses, oldfid, fidnum, "..",
  196. NULL);
  197. if (result < 0) {
  198. dprintk(DEBUG_ERROR,
  199. "problem walking to parent\n");
  200. break;
  201. }
  202. oldfid = fidnum;
  203. if (par == par->d_parent) {
  204. dprintk(DEBUG_ERROR,
  205. "can't find dentry\n");
  206. break;
  207. }
  208. par = par->d_parent;
  209. }
  210. if (par == dentry) {
  211. return_fid = v9fs_fid_create(dentry);
  212. return_fid->fid = fidnum;
  213. }
  214. }
  215. }
  216. return return_fid;
  217. }