vfs_dir.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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 <linux/uio.h>
  36. #include <net/9p/9p.h>
  37. #include <net/9p/client.h>
  38. #include "v9fs.h"
  39. #include "v9fs_vfs.h"
  40. #include "fid.h"
  41. /**
  42. * struct p9_rdir - readdir accounting
  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. struct kvec kvec;
  107. p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
  108. fid = file->private_data;
  109. buflen = fid->clnt->msize - P9_IOHDRSZ;
  110. rdir = v9fs_alloc_rdir_buf(file, buflen);
  111. if (!rdir)
  112. return -ENOMEM;
  113. kvec.iov_base = rdir->buf;
  114. kvec.iov_len = buflen;
  115. while (1) {
  116. if (rdir->tail == rdir->head) {
  117. struct iov_iter to;
  118. int n;
  119. iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buflen);
  120. n = p9_client_read(file->private_data, ctx->pos, &to,
  121. &err);
  122. if (err)
  123. return err;
  124. if (n == 0)
  125. return 0;
  126. rdir->head = 0;
  127. rdir->tail = n;
  128. }
  129. while (rdir->head < rdir->tail) {
  130. p9stat_init(&st);
  131. err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
  132. rdir->tail - rdir->head, &st);
  133. if (err) {
  134. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  135. p9stat_free(&st);
  136. return -EIO;
  137. }
  138. reclen = st.size+2;
  139. over = !dir_emit(ctx, st.name, strlen(st.name),
  140. v9fs_qid2ino(&st.qid), dt_type(&st));
  141. p9stat_free(&st);
  142. if (over)
  143. return 0;
  144. rdir->head += reclen;
  145. ctx->pos += reclen;
  146. }
  147. }
  148. }
  149. /**
  150. * v9fs_dir_readdir_dotl - iterate through a directory
  151. * @file: opened file structure
  152. * @ctx: actor we feed the entries to
  153. *
  154. */
  155. static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
  156. {
  157. int err = 0;
  158. struct p9_fid *fid;
  159. int buflen;
  160. struct p9_rdir *rdir;
  161. struct p9_dirent curdirent;
  162. p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
  163. fid = file->private_data;
  164. buflen = fid->clnt->msize - P9_READDIRHDRSZ;
  165. rdir = v9fs_alloc_rdir_buf(file, buflen);
  166. if (!rdir)
  167. return -ENOMEM;
  168. while (1) {
  169. if (rdir->tail == rdir->head) {
  170. err = p9_client_readdir(fid, rdir->buf, buflen,
  171. ctx->pos);
  172. if (err <= 0)
  173. return err;
  174. rdir->head = 0;
  175. rdir->tail = err;
  176. }
  177. while (rdir->head < rdir->tail) {
  178. err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
  179. rdir->tail - rdir->head,
  180. &curdirent);
  181. if (err < 0) {
  182. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  183. return -EIO;
  184. }
  185. if (!dir_emit(ctx, curdirent.d_name,
  186. strlen(curdirent.d_name),
  187. v9fs_qid2ino(&curdirent.qid),
  188. curdirent.d_type))
  189. return 0;
  190. ctx->pos = curdirent.d_off;
  191. rdir->head += err;
  192. }
  193. }
  194. }
  195. /**
  196. * v9fs_dir_release - close a directory
  197. * @inode: inode of the directory
  198. * @filp: file pointer to a directory
  199. *
  200. */
  201. int v9fs_dir_release(struct inode *inode, struct file *filp)
  202. {
  203. struct p9_fid *fid;
  204. fid = filp->private_data;
  205. p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
  206. inode, filp, fid ? fid->fid : -1);
  207. if (fid)
  208. p9_client_clunk(fid);
  209. return 0;
  210. }
  211. const struct file_operations v9fs_dir_operations = {
  212. .read = generic_read_dir,
  213. .llseek = generic_file_llseek,
  214. .iterate_shared = v9fs_dir_readdir,
  215. .open = v9fs_file_open,
  216. .release = v9fs_dir_release,
  217. };
  218. const struct file_operations v9fs_dir_operations_dotl = {
  219. .read = generic_read_dir,
  220. .llseek = generic_file_llseek,
  221. .iterate_shared = v9fs_dir_readdir_dotl,
  222. .open = v9fs_file_open,
  223. .release = v9fs_dir_release,
  224. .fsync = v9fs_file_fsync_dotl,
  225. };