vfs_dir.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  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/inet.h>
  33. #include <linux/idr.h>
  34. #include "debug.h"
  35. #include "v9fs.h"
  36. #include "9p.h"
  37. #include "conv.h"
  38. #include "v9fs_vfs.h"
  39. #include "fid.h"
  40. /**
  41. * dt_type - return file type
  42. * @mistat: mistat structure
  43. *
  44. */
  45. static inline int dt_type(struct v9fs_stat *mistat)
  46. {
  47. unsigned long perm = mistat->mode;
  48. int rettype = DT_REG;
  49. if (perm & V9FS_DMDIR)
  50. rettype = DT_DIR;
  51. if (perm & V9FS_DMSYMLINK)
  52. rettype = DT_LNK;
  53. return rettype;
  54. }
  55. /**
  56. * v9fs_dir_readdir - read a directory
  57. * @filep: opened file structure
  58. * @dirent: directory structure ???
  59. * @filldir: function to populate directory structure ???
  60. *
  61. */
  62. static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
  63. {
  64. struct v9fs_fcall *fcall = NULL;
  65. struct inode *inode = filp->f_dentry->d_inode;
  66. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  67. struct v9fs_fid *file = filp->private_data;
  68. unsigned int i, n, s;
  69. int fid = -1;
  70. int ret = 0;
  71. struct v9fs_stat stat;
  72. int over = 0;
  73. dprintk(DEBUG_VFS, "name %s\n", filp->f_dentry->d_name.name);
  74. fid = file->fid;
  75. if (file->rdir_fcall && (filp->f_pos != file->rdir_pos)) {
  76. kfree(file->rdir_fcall);
  77. file->rdir_fcall = NULL;
  78. }
  79. if (file->rdir_fcall) {
  80. n = file->rdir_fcall->params.rread.count;
  81. i = file->rdir_fpos;
  82. while (i < n) {
  83. s = v9fs_deserialize_stat(
  84. file->rdir_fcall->params.rread.data + i,
  85. n - i, &stat, v9ses->extended);
  86. if (s == 0) {
  87. dprintk(DEBUG_ERROR,
  88. "error while deserializing stat\n");
  89. ret = -EIO;
  90. goto FreeStructs;
  91. }
  92. over = filldir(dirent, stat.name.str, stat.name.len,
  93. filp->f_pos, v9fs_qid2ino(&stat.qid),
  94. dt_type(&stat));
  95. if (over) {
  96. file->rdir_fpos = i;
  97. file->rdir_pos = filp->f_pos;
  98. break;
  99. }
  100. i += s;
  101. filp->f_pos += s;
  102. }
  103. if (!over) {
  104. kfree(file->rdir_fcall);
  105. file->rdir_fcall = NULL;
  106. }
  107. }
  108. while (!over) {
  109. ret = v9fs_t_read(v9ses, fid, filp->f_pos,
  110. v9ses->maxdata-V9FS_IOHDRSZ, &fcall);
  111. if (ret < 0) {
  112. dprintk(DEBUG_ERROR, "error while reading: %d: %p\n",
  113. ret, fcall);
  114. goto FreeStructs;
  115. } else if (ret == 0)
  116. break;
  117. n = ret;
  118. i = 0;
  119. while (i < n) {
  120. s = v9fs_deserialize_stat(fcall->params.rread.data + i,
  121. n - i, &stat, v9ses->extended);
  122. if (s == 0) {
  123. dprintk(DEBUG_ERROR,
  124. "error while deserializing stat\n");
  125. return -EIO;
  126. }
  127. over = filldir(dirent, stat.name.str, stat.name.len,
  128. filp->f_pos, v9fs_qid2ino(&stat.qid),
  129. dt_type(&stat));
  130. if (over) {
  131. file->rdir_fcall = fcall;
  132. file->rdir_fpos = i;
  133. file->rdir_pos = filp->f_pos;
  134. fcall = NULL;
  135. break;
  136. }
  137. i += s;
  138. filp->f_pos += s;
  139. }
  140. kfree(fcall);
  141. }
  142. FreeStructs:
  143. kfree(fcall);
  144. return ret;
  145. }
  146. /**
  147. * v9fs_dir_release - close a directory
  148. * @inode: inode of the directory
  149. * @filp: file pointer to a directory
  150. *
  151. */
  152. int v9fs_dir_release(struct inode *inode, struct file *filp)
  153. {
  154. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  155. struct v9fs_fid *fid = filp->private_data;
  156. int fidnum = -1;
  157. dprintk(DEBUG_VFS, "inode: %p filp: %p fid: %d\n", inode, filp,
  158. fid->fid);
  159. fidnum = fid->fid;
  160. filemap_write_and_wait(inode->i_mapping);
  161. if (fidnum >= 0) {
  162. dprintk(DEBUG_VFS, "fidopen: %d v9f->fid: %d\n", fid->fidopen,
  163. fid->fid);
  164. if (v9fs_t_clunk(v9ses, fidnum))
  165. dprintk(DEBUG_ERROR, "clunk failed\n");
  166. kfree(fid->rdir_fcall);
  167. kfree(fid);
  168. filp->private_data = NULL;
  169. }
  170. return 0;
  171. }
  172. const struct file_operations v9fs_dir_operations = {
  173. .read = generic_read_dir,
  174. .readdir = v9fs_dir_readdir,
  175. .open = v9fs_file_open,
  176. .release = v9fs_dir_release,
  177. };