vfs_dir.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * linux/fs/9p/vfs_dir.c
  3. *
  4. * This file contains vfs directory ops for the 9P2000 protocol.
  5. *
  6. * Copyright (C) 2004 by Eric Van Hensbergen <ericvh@gmail.com>
  7. * Copyright (C) 2002 by Ron Minnich <rminnich@lanl.gov>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to:
  21. * Free Software Foundation
  22. * 51 Franklin Street, Fifth Floor
  23. * Boston, MA 02111-1301 USA
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.h>
  29. #include <linux/file.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/smp_lock.h>
  33. #include <linux/inet.h>
  34. #include <linux/idr.h>
  35. #include "debug.h"
  36. #include "v9fs.h"
  37. #include "9p.h"
  38. #include "v9fs_vfs.h"
  39. #include "conv.h"
  40. #include "fid.h"
  41. /**
  42. * dt_type - return file type
  43. * @mistat: mistat structure
  44. *
  45. */
  46. static inline int dt_type(struct v9fs_stat *mistat)
  47. {
  48. unsigned long perm = mistat->mode;
  49. int rettype = DT_REG;
  50. if (perm & V9FS_DMDIR)
  51. rettype = DT_DIR;
  52. if (perm & V9FS_DMSYMLINK)
  53. rettype = DT_LNK;
  54. return rettype;
  55. }
  56. /**
  57. * v9fs_dir_readdir - read a directory
  58. * @filep: opened file structure
  59. * @dirent: directory structure ???
  60. * @filldir: function to populate directory structure ???
  61. *
  62. */
  63. static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
  64. {
  65. struct v9fs_fcall *fcall = NULL;
  66. struct inode *inode = filp->f_dentry->d_inode;
  67. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  68. struct v9fs_fid *file = filp->private_data;
  69. unsigned int i, n;
  70. int fid = -1;
  71. int ret = 0;
  72. struct v9fs_stat *mi = NULL;
  73. int over = 0;
  74. dprintk(DEBUG_VFS, "name %s\n", filp->f_dentry->d_name.name);
  75. fid = file->fid;
  76. mi = kmalloc(v9ses->maxdata, GFP_KERNEL);
  77. if (!mi)
  78. return -ENOMEM;
  79. if (file->rdir_fcall && (filp->f_pos != file->rdir_pos)) {
  80. kfree(file->rdir_fcall);
  81. file->rdir_fcall = NULL;
  82. }
  83. if (file->rdir_fcall) {
  84. n = file->rdir_fcall->params.rread.count;
  85. i = file->rdir_fpos;
  86. while (i < n) {
  87. int s = v9fs_deserialize_stat(v9ses,
  88. file->rdir_fcall->params.rread.data + i,
  89. n - i, mi, v9ses->maxdata);
  90. if (s == 0) {
  91. dprintk(DEBUG_ERROR,
  92. "error while deserializing mistat\n");
  93. ret = -EIO;
  94. goto FreeStructs;
  95. }
  96. over = filldir(dirent, mi->name, strlen(mi->name),
  97. filp->f_pos, v9fs_qid2ino(&mi->qid),
  98. dt_type(mi));
  99. if (over) {
  100. file->rdir_fpos = i;
  101. file->rdir_pos = filp->f_pos;
  102. break;
  103. }
  104. i += s;
  105. filp->f_pos += s;
  106. }
  107. if (!over) {
  108. kfree(file->rdir_fcall);
  109. file->rdir_fcall = NULL;
  110. }
  111. }
  112. while (!over) {
  113. ret = v9fs_t_read(v9ses, fid, filp->f_pos,
  114. v9ses->maxdata-V9FS_IOHDRSZ, &fcall);
  115. if (ret < 0) {
  116. dprintk(DEBUG_ERROR, "error while reading: %d: %p\n",
  117. ret, fcall);
  118. goto FreeStructs;
  119. } else if (ret == 0)
  120. break;
  121. n = ret;
  122. i = 0;
  123. while (i < n) {
  124. int s = v9fs_deserialize_stat(v9ses,
  125. fcall->params.rread.data + i, n - i, mi,
  126. v9ses->maxdata);
  127. if (s == 0) {
  128. dprintk(DEBUG_ERROR,
  129. "error while deserializing mistat\n");
  130. return -EIO;
  131. }
  132. over = filldir(dirent, mi->name, strlen(mi->name),
  133. filp->f_pos, v9fs_qid2ino(&mi->qid),
  134. dt_type(mi));
  135. if (over) {
  136. file->rdir_fcall = fcall;
  137. file->rdir_fpos = i;
  138. file->rdir_pos = filp->f_pos;
  139. fcall = NULL;
  140. break;
  141. }
  142. i += s;
  143. filp->f_pos += s;
  144. }
  145. kfree(fcall);
  146. }
  147. FreeStructs:
  148. kfree(fcall);
  149. kfree(mi);
  150. return ret;
  151. }
  152. /**
  153. * v9fs_dir_release - close a directory
  154. * @inode: inode of the directory
  155. * @filp: file pointer to a directory
  156. *
  157. */
  158. int v9fs_dir_release(struct inode *inode, struct file *filp)
  159. {
  160. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  161. struct v9fs_fid *fid = filp->private_data;
  162. int fidnum = -1;
  163. dprintk(DEBUG_VFS, "inode: %p filp: %p fid: %d\n", inode, filp,
  164. fid->fid);
  165. fidnum = fid->fid;
  166. filemap_fdatawrite(inode->i_mapping);
  167. filemap_fdatawait(inode->i_mapping);
  168. if (fidnum >= 0) {
  169. fid->fidopen--;
  170. dprintk(DEBUG_VFS, "fidopen: %d v9f->fid: %d\n", fid->fidopen,
  171. fid->fid);
  172. if (fid->fidopen == 0) {
  173. if (v9fs_t_clunk(v9ses, fidnum, NULL))
  174. dprintk(DEBUG_ERROR, "clunk failed\n");
  175. v9fs_put_idpool(fid->fid, &v9ses->fidpool);
  176. }
  177. kfree(fid->rdir_fcall);
  178. filp->private_data = NULL;
  179. v9fs_fid_destroy(fid);
  180. }
  181. d_drop(filp->f_dentry);
  182. return 0;
  183. }
  184. struct file_operations v9fs_dir_operations = {
  185. .read = generic_read_dir,
  186. .readdir = v9fs_dir_readdir,
  187. .open = v9fs_file_open,
  188. .release = v9fs_dir_release,
  189. };