vfs_dir.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/sched.h>
  32. #include <linux/inet.h>
  33. #include <linux/idr.h>
  34. #include <linux/slab.h>
  35. #include <net/9p/9p.h>
  36. #include <net/9p/client.h>
  37. #include "v9fs.h"
  38. #include "v9fs_vfs.h"
  39. #include "fid.h"
  40. /**
  41. * struct p9_rdir - readdir accounting
  42. * @mutex: mutex protecting readdir
  43. * @head: start offset of current dirread buffer
  44. * @tail: end offset of current dirread buffer
  45. * @buf: dirread buffer
  46. *
  47. * private structure for keeping track of readdir
  48. * allocated on demand
  49. */
  50. struct p9_rdir {
  51. int head;
  52. int tail;
  53. uint8_t buf[];
  54. };
  55. /**
  56. * dt_type - return file type
  57. * @mistat: mistat structure
  58. *
  59. */
  60. static inline int dt_type(struct p9_wstat *mistat)
  61. {
  62. unsigned long perm = mistat->mode;
  63. int rettype = DT_REG;
  64. if (perm & P9_DMDIR)
  65. rettype = DT_DIR;
  66. if (perm & P9_DMSYMLINK)
  67. rettype = DT_LNK;
  68. return rettype;
  69. }
  70. static void p9stat_init(struct p9_wstat *stbuf)
  71. {
  72. stbuf->name = NULL;
  73. stbuf->uid = NULL;
  74. stbuf->gid = NULL;
  75. stbuf->muid = NULL;
  76. stbuf->extension = NULL;
  77. }
  78. /**
  79. * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir
  80. * @filp: opened file structure
  81. * @buflen: Length in bytes of buffer to allocate
  82. *
  83. */
  84. static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)
  85. {
  86. struct p9_fid *fid = filp->private_data;
  87. if (!fid->rdir)
  88. fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
  89. return fid->rdir;
  90. }
  91. /**
  92. * v9fs_dir_readdir - iterate through a directory
  93. * @file: opened file structure
  94. * @ctx: actor we feed the entries to
  95. *
  96. */
  97. static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
  98. {
  99. bool over;
  100. struct p9_wstat st;
  101. int err = 0;
  102. struct p9_fid *fid;
  103. int buflen;
  104. int reclen = 0;
  105. struct p9_rdir *rdir;
  106. p9_debug(P9_DEBUG_VFS, "name %s\n", file->f_path.dentry->d_name.name);
  107. fid = file->private_data;
  108. buflen = fid->clnt->msize - P9_IOHDRSZ;
  109. rdir = v9fs_alloc_rdir_buf(file, buflen);
  110. if (!rdir)
  111. return -ENOMEM;
  112. while (1) {
  113. if (rdir->tail == rdir->head) {
  114. err = v9fs_file_readn(file, rdir->buf, NULL,
  115. buflen, ctx->pos);
  116. if (err <= 0)
  117. return err;
  118. rdir->head = 0;
  119. rdir->tail = err;
  120. }
  121. while (rdir->head < rdir->tail) {
  122. p9stat_init(&st);
  123. err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
  124. rdir->tail - rdir->head, &st);
  125. if (err) {
  126. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  127. p9stat_free(&st);
  128. return -EIO;
  129. }
  130. reclen = st.size+2;
  131. over = !dir_emit(ctx, st.name, strlen(st.name),
  132. v9fs_qid2ino(&st.qid), dt_type(&st));
  133. p9stat_free(&st);
  134. if (over)
  135. return 0;
  136. rdir->head += reclen;
  137. ctx->pos += reclen;
  138. }
  139. }
  140. }
  141. /**
  142. * v9fs_dir_readdir_dotl - iterate through a directory
  143. * @file: opened file structure
  144. * @ctx: actor we feed the entries to
  145. *
  146. */
  147. static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
  148. {
  149. int err = 0;
  150. struct p9_fid *fid;
  151. int buflen;
  152. struct p9_rdir *rdir;
  153. struct p9_dirent curdirent;
  154. p9_debug(P9_DEBUG_VFS, "name %s\n", file->f_path.dentry->d_name.name);
  155. fid = file->private_data;
  156. buflen = fid->clnt->msize - P9_READDIRHDRSZ;
  157. rdir = v9fs_alloc_rdir_buf(file, buflen);
  158. if (!rdir)
  159. return -ENOMEM;
  160. while (1) {
  161. if (rdir->tail == rdir->head) {
  162. err = p9_client_readdir(fid, rdir->buf, buflen,
  163. ctx->pos);
  164. if (err <= 0)
  165. return err;
  166. rdir->head = 0;
  167. rdir->tail = err;
  168. }
  169. while (rdir->head < rdir->tail) {
  170. err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
  171. rdir->tail - rdir->head,
  172. &curdirent);
  173. if (err < 0) {
  174. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  175. return -EIO;
  176. }
  177. if (!dir_emit(ctx, curdirent.d_name,
  178. strlen(curdirent.d_name),
  179. v9fs_qid2ino(&curdirent.qid),
  180. curdirent.d_type))
  181. return 0;
  182. ctx->pos = curdirent.d_off;
  183. rdir->head += err;
  184. }
  185. }
  186. }
  187. /**
  188. * v9fs_dir_release - close a directory
  189. * @inode: inode of the directory
  190. * @filp: file pointer to a directory
  191. *
  192. */
  193. int v9fs_dir_release(struct inode *inode, struct file *filp)
  194. {
  195. struct p9_fid *fid;
  196. fid = filp->private_data;
  197. p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
  198. inode, filp, fid ? fid->fid : -1);
  199. if (fid)
  200. p9_client_clunk(fid);
  201. return 0;
  202. }
  203. const struct file_operations v9fs_dir_operations = {
  204. .read = generic_read_dir,
  205. .llseek = generic_file_llseek,
  206. .iterate = v9fs_dir_readdir,
  207. .open = v9fs_file_open,
  208. .release = v9fs_dir_release,
  209. };
  210. const struct file_operations v9fs_dir_operations_dotl = {
  211. .read = generic_read_dir,
  212. .llseek = generic_file_llseek,
  213. .iterate = v9fs_dir_readdir_dotl,
  214. .open = v9fs_file_open,
  215. .release = v9fs_dir_release,
  216. .fsync = v9fs_file_fsync_dotl,
  217. };