vfs_dir.c 5.5 KB

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