vfs_dir.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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 - read a directory
  93. * @filp: opened file structure
  94. * @dirent: directory structure ???
  95. * @filldir: function to populate directory structure ???
  96. *
  97. */
  98. static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
  99. {
  100. int over;
  101. struct p9_wstat st;
  102. int err = 0;
  103. struct p9_fid *fid;
  104. int buflen;
  105. int reclen = 0;
  106. struct p9_rdir *rdir;
  107. p9_debug(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  108. fid = filp->private_data;
  109. buflen = fid->clnt->msize - P9_IOHDRSZ;
  110. rdir = v9fs_alloc_rdir_buf(filp, buflen);
  111. if (!rdir)
  112. return -ENOMEM;
  113. while (1) {
  114. if (rdir->tail == rdir->head) {
  115. err = v9fs_file_readn(filp, rdir->buf, NULL,
  116. buflen, filp->f_pos);
  117. if (err <= 0)
  118. return err;
  119. rdir->head = 0;
  120. rdir->tail = err;
  121. }
  122. while (rdir->head < rdir->tail) {
  123. p9stat_init(&st);
  124. err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
  125. rdir->tail - rdir->head, &st);
  126. if (err) {
  127. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  128. p9stat_free(&st);
  129. return -EIO;
  130. }
  131. reclen = st.size+2;
  132. over = filldir(dirent, st.name, strlen(st.name),
  133. filp->f_pos, v9fs_qid2ino(&st.qid), dt_type(&st));
  134. p9stat_free(&st);
  135. if (over)
  136. return 0;
  137. rdir->head += reclen;
  138. filp->f_pos += reclen;
  139. }
  140. }
  141. }
  142. /**
  143. * v9fs_dir_readdir_dotl - read a directory
  144. * @filp: opened file structure
  145. * @dirent: buffer to fill dirent structures
  146. * @filldir: function to populate dirent structures
  147. *
  148. */
  149. static int v9fs_dir_readdir_dotl(struct file *filp, void *dirent,
  150. filldir_t filldir)
  151. {
  152. int over;
  153. int err = 0;
  154. struct p9_fid *fid;
  155. int buflen;
  156. struct p9_rdir *rdir;
  157. struct p9_dirent curdirent;
  158. u64 oldoffset = 0;
  159. p9_debug(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  160. fid = filp->private_data;
  161. buflen = fid->clnt->msize - P9_READDIRHDRSZ;
  162. rdir = v9fs_alloc_rdir_buf(filp, buflen);
  163. if (!rdir)
  164. return -ENOMEM;
  165. while (1) {
  166. if (rdir->tail == rdir->head) {
  167. err = p9_client_readdir(fid, rdir->buf, buflen,
  168. filp->f_pos);
  169. if (err <= 0)
  170. return err;
  171. rdir->head = 0;
  172. rdir->tail = err;
  173. }
  174. while (rdir->head < rdir->tail) {
  175. err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
  176. rdir->tail - rdir->head,
  177. &curdirent);
  178. if (err < 0) {
  179. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  180. return -EIO;
  181. }
  182. /* d_off in dirent structure tracks the offset into
  183. * the next dirent in the dir. However, filldir()
  184. * expects offset into the current dirent. Hence
  185. * while calling filldir send the offset from the
  186. * previous dirent structure.
  187. */
  188. over = filldir(dirent, curdirent.d_name,
  189. strlen(curdirent.d_name),
  190. oldoffset, v9fs_qid2ino(&curdirent.qid),
  191. curdirent.d_type);
  192. oldoffset = curdirent.d_off;
  193. if (over)
  194. return 0;
  195. filp->f_pos = curdirent.d_off;
  196. rdir->head += err;
  197. }
  198. }
  199. }
  200. /**
  201. * v9fs_dir_release - close a directory
  202. * @inode: inode of the directory
  203. * @filp: file pointer to a directory
  204. *
  205. */
  206. int v9fs_dir_release(struct inode *inode, struct file *filp)
  207. {
  208. struct p9_fid *fid;
  209. fid = filp->private_data;
  210. p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
  211. inode, filp, fid ? fid->fid : -1);
  212. if (fid)
  213. p9_client_clunk(fid);
  214. return 0;
  215. }
  216. const struct file_operations v9fs_dir_operations = {
  217. .read = generic_read_dir,
  218. .llseek = generic_file_llseek,
  219. .readdir = v9fs_dir_readdir,
  220. .open = v9fs_file_open,
  221. .release = v9fs_dir_release,
  222. };
  223. const struct file_operations v9fs_dir_operations_dotl = {
  224. .read = generic_read_dir,
  225. .llseek = generic_file_llseek,
  226. .readdir = v9fs_dir_readdir_dotl,
  227. .open = v9fs_file_open,
  228. .release = v9fs_dir_release,
  229. .fsync = v9fs_file_fsync_dotl,
  230. };