vfs_dir.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to:
  20. * Free Software Foundation
  21. * 51 Franklin Street, Fifth Floor
  22. * Boston, MA 02111-1301 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/errno.h>
  27. #include <linux/fs.h>
  28. #include <linux/file.h>
  29. #include <linux/stat.h>
  30. #include <linux/string.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/sched.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 "conv.h"
  39. #include "v9fs_vfs.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_path.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, s;
  70. int fid = -1;
  71. int ret = 0;
  72. struct v9fs_stat stat;
  73. int over = 0;
  74. dprintk(DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  75. fid = file->fid;
  76. if (file->rdir_fcall && (filp->f_pos != file->rdir_pos)) {
  77. kfree(file->rdir_fcall);
  78. file->rdir_fcall = NULL;
  79. }
  80. if (file->rdir_fcall) {
  81. n = file->rdir_fcall->params.rread.count;
  82. i = file->rdir_fpos;
  83. while (i < n) {
  84. s = v9fs_deserialize_stat(
  85. file->rdir_fcall->params.rread.data + i,
  86. n - i, &stat, v9ses->extended);
  87. if (s == 0) {
  88. dprintk(DEBUG_ERROR,
  89. "error while deserializing stat\n");
  90. ret = -EIO;
  91. goto FreeStructs;
  92. }
  93. over = filldir(dirent, stat.name.str, stat.name.len,
  94. filp->f_pos, v9fs_qid2ino(&stat.qid),
  95. dt_type(&stat));
  96. if (over) {
  97. file->rdir_fpos = i;
  98. file->rdir_pos = filp->f_pos;
  99. break;
  100. }
  101. i += s;
  102. filp->f_pos += s;
  103. }
  104. if (!over) {
  105. kfree(file->rdir_fcall);
  106. file->rdir_fcall = NULL;
  107. }
  108. }
  109. while (!over) {
  110. ret = v9fs_t_read(v9ses, fid, filp->f_pos,
  111. v9ses->maxdata-V9FS_IOHDRSZ, &fcall);
  112. if (ret < 0) {
  113. dprintk(DEBUG_ERROR, "error while reading: %d: %p\n",
  114. ret, fcall);
  115. goto FreeStructs;
  116. } else if (ret == 0)
  117. break;
  118. n = ret;
  119. i = 0;
  120. while (i < n) {
  121. s = v9fs_deserialize_stat(fcall->params.rread.data + i,
  122. n - i, &stat, v9ses->extended);
  123. if (s == 0) {
  124. dprintk(DEBUG_ERROR,
  125. "error while deserializing stat\n");
  126. return -EIO;
  127. }
  128. over = filldir(dirent, stat.name.str, stat.name.len,
  129. filp->f_pos, v9fs_qid2ino(&stat.qid),
  130. dt_type(&stat));
  131. if (over) {
  132. file->rdir_fcall = fcall;
  133. file->rdir_fpos = i;
  134. file->rdir_pos = filp->f_pos;
  135. fcall = NULL;
  136. break;
  137. }
  138. i += s;
  139. filp->f_pos += s;
  140. }
  141. kfree(fcall);
  142. }
  143. FreeStructs:
  144. kfree(fcall);
  145. return ret;
  146. }
  147. /**
  148. * v9fs_dir_release - close a directory
  149. * @inode: inode of the directory
  150. * @filp: file pointer to a directory
  151. *
  152. */
  153. int v9fs_dir_release(struct inode *inode, struct file *filp)
  154. {
  155. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  156. struct v9fs_fid *fid = filp->private_data;
  157. int fidnum = -1;
  158. dprintk(DEBUG_VFS, "inode: %p filp: %p fid: %d\n", inode, filp,
  159. fid->fid);
  160. fidnum = fid->fid;
  161. filemap_write_and_wait(inode->i_mapping);
  162. if (fidnum >= 0) {
  163. dprintk(DEBUG_VFS, "fidopen: %d v9f->fid: %d\n", fid->fidopen,
  164. fid->fid);
  165. if (v9fs_t_clunk(v9ses, fidnum))
  166. dprintk(DEBUG_ERROR, "clunk failed\n");
  167. kfree(fid->rdir_fcall);
  168. kfree(fid);
  169. filp->private_data = NULL;
  170. }
  171. return 0;
  172. }
  173. const struct file_operations v9fs_dir_operations = {
  174. .read = generic_read_dir,
  175. .readdir = v9fs_dir_readdir,
  176. .open = v9fs_file_open,
  177. .release = v9fs_dir_release,
  178. };