fid.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * V9FS FID Management
  3. *
  4. * Copyright (C) 2005, 2006 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 version 2
  8. * as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to:
  17. * Free Software Foundation
  18. * 51 Franklin Street, Fifth Floor
  19. * Boston, MA 02111-1301 USA
  20. *
  21. */
  22. #include <linux/module.h>
  23. #include <linux/errno.h>
  24. #include <linux/fs.h>
  25. #include <linux/sched.h>
  26. #include <linux/idr.h>
  27. #include <asm/semaphore.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. list_add(&fid->list, fid_list);
  56. return 0;
  57. }
  58. /**
  59. * v9fs_fid_create - allocate a FID structure
  60. * @dentry - dentry to link newly created fid to
  61. *
  62. */
  63. struct v9fs_fid *v9fs_fid_create(struct v9fs_session_info *v9ses, int fid)
  64. {
  65. struct v9fs_fid *new;
  66. dprintk(DEBUG_9P, "fid create fid %d\n", fid);
  67. new = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
  68. if (new == NULL) {
  69. dprintk(DEBUG_ERROR, "Out of Memory\n");
  70. return ERR_PTR(-ENOMEM);
  71. }
  72. new->fid = fid;
  73. new->v9ses = v9ses;
  74. new->fidopen = 0;
  75. new->fidclunked = 0;
  76. new->iounit = 0;
  77. new->rdir_pos = 0;
  78. new->rdir_fcall = NULL;
  79. init_MUTEX(&new->lock);
  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_lookup - return a locked fid from a dentry
  95. * @dentry: dentry to look for fid in
  96. *
  97. * find a fid in the dentry, obtain its semaphore and return a reference to it.
  98. * code calling lookup is responsible for releasing lock
  99. *
  100. * TODO: only match fids that have the same uid as current user
  101. *
  102. */
  103. struct v9fs_fid *v9fs_fid_lookup(struct dentry *dentry)
  104. {
  105. struct list_head *fid_list = (struct list_head *)dentry->d_fsdata;
  106. struct v9fs_fid *return_fid = NULL;
  107. dprintk(DEBUG_9P, " dentry: %s (%p)\n", dentry->d_iname, dentry);
  108. if (fid_list)
  109. return_fid = list_entry(fid_list->next, struct v9fs_fid, list);
  110. if (!return_fid) {
  111. dprintk(DEBUG_ERROR, "Couldn't find a fid in dentry\n");
  112. return_fid = ERR_PTR(-EBADF);
  113. }
  114. if(down_interruptible(&return_fid->lock))
  115. return ERR_PTR(-EINTR);
  116. return return_fid;
  117. }
  118. /**
  119. * v9fs_fid_clone - lookup the fid for a dentry, clone a private copy and
  120. * release it
  121. * @dentry: dentry to look for fid in
  122. *
  123. * find a fid in the dentry and then clone to a new private fid
  124. *
  125. * TODO: only match fids that have the same uid as current user
  126. *
  127. */
  128. struct v9fs_fid *v9fs_fid_clone(struct dentry *dentry)
  129. {
  130. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(dentry->d_inode);
  131. struct v9fs_fid *base_fid, *new_fid = ERR_PTR(-EBADF);
  132. struct v9fs_fcall *fcall = NULL;
  133. int fid, err;
  134. base_fid = v9fs_fid_lookup(dentry);
  135. if(IS_ERR(base_fid))
  136. return base_fid;
  137. if(base_fid) { /* clone fid */
  138. fid = v9fs_get_idpool(&v9ses->fidpool);
  139. if (fid < 0) {
  140. eprintk(KERN_WARNING, "newfid fails!\n");
  141. new_fid = ERR_PTR(-ENOSPC);
  142. goto Release_Fid;
  143. }
  144. err = v9fs_t_walk(v9ses, base_fid->fid, fid, NULL, &fcall);
  145. if (err < 0) {
  146. dprintk(DEBUG_ERROR, "clone walk didn't work\n");
  147. v9fs_put_idpool(fid, &v9ses->fidpool);
  148. new_fid = ERR_PTR(err);
  149. goto Free_Fcall;
  150. }
  151. new_fid = v9fs_fid_create(v9ses, fid);
  152. if (new_fid == NULL) {
  153. dprintk(DEBUG_ERROR, "out of memory\n");
  154. new_fid = ERR_PTR(-ENOMEM);
  155. }
  156. Free_Fcall:
  157. kfree(fcall);
  158. }
  159. Release_Fid:
  160. up(&base_fid->lock);
  161. return new_fid;
  162. }
  163. void v9fs_fid_clunk(struct v9fs_session_info *v9ses, struct v9fs_fid *fid)
  164. {
  165. v9fs_t_clunk(v9ses, fid->fid);
  166. v9fs_fid_destroy(fid);
  167. }