vfs_dir.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. /**
  71. * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir
  72. * @filp: opened file structure
  73. * @buflen: Length in bytes of buffer to allocate
  74. *
  75. */
  76. static struct p9_rdir *v9fs_alloc_rdir_buf(struct file *filp, int buflen)
  77. {
  78. struct p9_fid *fid = filp->private_data;
  79. if (!fid->rdir)
  80. fid->rdir = kzalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
  81. return fid->rdir;
  82. }
  83. /**
  84. * v9fs_dir_readdir - iterate through a directory
  85. * @file: opened file structure
  86. * @ctx: actor we feed the entries to
  87. *
  88. */
  89. static int v9fs_dir_readdir(struct file *file, struct dir_context *ctx)
  90. {
  91. bool over;
  92. struct p9_wstat st;
  93. int err = 0;
  94. struct p9_fid *fid;
  95. int buflen;
  96. struct p9_rdir *rdir;
  97. struct kvec kvec;
  98. p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
  99. fid = file->private_data;
  100. buflen = fid->clnt->msize - P9_IOHDRSZ;
  101. rdir = v9fs_alloc_rdir_buf(file, buflen);
  102. if (!rdir)
  103. return -ENOMEM;
  104. kvec.iov_base = rdir->buf;
  105. kvec.iov_len = buflen;
  106. while (1) {
  107. if (rdir->tail == rdir->head) {
  108. struct iov_iter to;
  109. int n;
  110. iov_iter_kvec(&to, READ | ITER_KVEC, &kvec, 1, buflen);
  111. n = p9_client_read(file->private_data, ctx->pos, &to,
  112. &err);
  113. if (err)
  114. return err;
  115. if (n == 0)
  116. return 0;
  117. rdir->head = 0;
  118. rdir->tail = n;
  119. }
  120. while (rdir->head < rdir->tail) {
  121. err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
  122. rdir->tail - rdir->head, &st);
  123. if (err <= 0) {
  124. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  125. return -EIO;
  126. }
  127. over = !dir_emit(ctx, st.name, strlen(st.name),
  128. v9fs_qid2ino(&st.qid), dt_type(&st));
  129. p9stat_free(&st);
  130. if (over)
  131. return 0;
  132. rdir->head += err;
  133. ctx->pos += err;
  134. }
  135. }
  136. }
  137. /**
  138. * v9fs_dir_readdir_dotl - iterate through a directory
  139. * @file: opened file structure
  140. * @ctx: actor we feed the entries to
  141. *
  142. */
  143. static int v9fs_dir_readdir_dotl(struct file *file, struct dir_context *ctx)
  144. {
  145. int err = 0;
  146. struct p9_fid *fid;
  147. int buflen;
  148. struct p9_rdir *rdir;
  149. struct p9_dirent curdirent;
  150. p9_debug(P9_DEBUG_VFS, "name %pD\n", file);
  151. fid = file->private_data;
  152. buflen = fid->clnt->msize - P9_READDIRHDRSZ;
  153. rdir = v9fs_alloc_rdir_buf(file, buflen);
  154. if (!rdir)
  155. return -ENOMEM;
  156. while (1) {
  157. if (rdir->tail == rdir->head) {
  158. err = p9_client_readdir(fid, rdir->buf, buflen,
  159. ctx->pos);
  160. if (err <= 0)
  161. return err;
  162. rdir->head = 0;
  163. rdir->tail = err;
  164. }
  165. while (rdir->head < rdir->tail) {
  166. err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
  167. rdir->tail - rdir->head,
  168. &curdirent);
  169. if (err < 0) {
  170. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  171. return -EIO;
  172. }
  173. if (!dir_emit(ctx, curdirent.d_name,
  174. strlen(curdirent.d_name),
  175. v9fs_qid2ino(&curdirent.qid),
  176. curdirent.d_type))
  177. return 0;
  178. ctx->pos = curdirent.d_off;
  179. rdir->head += err;
  180. }
  181. }
  182. }
  183. /**
  184. * v9fs_dir_release - close a directory
  185. * @inode: inode of the directory
  186. * @filp: file pointer to a directory
  187. *
  188. */
  189. int v9fs_dir_release(struct inode *inode, struct file *filp)
  190. {
  191. struct p9_fid *fid;
  192. fid = filp->private_data;
  193. p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
  194. inode, filp, fid ? fid->fid : -1);
  195. if (fid)
  196. p9_client_clunk(fid);
  197. return 0;
  198. }
  199. const struct file_operations v9fs_dir_operations = {
  200. .read = generic_read_dir,
  201. .llseek = generic_file_llseek,
  202. .iterate = v9fs_dir_readdir,
  203. .open = v9fs_file_open,
  204. .release = v9fs_dir_release,
  205. };
  206. const struct file_operations v9fs_dir_operations_dotl = {
  207. .read = generic_read_dir,
  208. .llseek = generic_file_llseek,
  209. .iterate = v9fs_dir_readdir_dotl,
  210. .open = v9fs_file_open,
  211. .release = v9fs_dir_release,
  212. .fsync = v9fs_file_fsync_dotl,
  213. };