vfs_dir.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. struct mutex mutex;
  52. int head;
  53. int tail;
  54. uint8_t *buf;
  55. };
  56. /**
  57. * dt_type - return file type
  58. * @mistat: mistat structure
  59. *
  60. */
  61. static inline int dt_type(struct p9_wstat *mistat)
  62. {
  63. unsigned long perm = mistat->mode;
  64. int rettype = DT_REG;
  65. if (perm & P9_DMDIR)
  66. rettype = DT_DIR;
  67. if (perm & P9_DMSYMLINK)
  68. rettype = DT_LNK;
  69. return rettype;
  70. }
  71. static void p9stat_init(struct p9_wstat *stbuf)
  72. {
  73. stbuf->name = NULL;
  74. stbuf->uid = NULL;
  75. stbuf->gid = NULL;
  76. stbuf->muid = NULL;
  77. stbuf->extension = NULL;
  78. }
  79. /**
  80. * v9fs_alloc_rdir_buf - Allocate buffer used for read and readdir
  81. * @filp: opened file structure
  82. * @buflen: Length in bytes of buffer to allocate
  83. *
  84. */
  85. static int v9fs_alloc_rdir_buf(struct file *filp, int buflen)
  86. {
  87. struct p9_rdir *rdir;
  88. struct p9_fid *fid;
  89. int err = 0;
  90. fid = filp->private_data;
  91. if (!fid->rdir) {
  92. rdir = kmalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
  93. if (rdir == NULL) {
  94. err = -ENOMEM;
  95. goto exit;
  96. }
  97. spin_lock(&filp->f_dentry->d_lock);
  98. if (!fid->rdir) {
  99. rdir->buf = (uint8_t *)rdir + sizeof(struct p9_rdir);
  100. mutex_init(&rdir->mutex);
  101. rdir->head = rdir->tail = 0;
  102. fid->rdir = (void *) rdir;
  103. rdir = NULL;
  104. }
  105. spin_unlock(&filp->f_dentry->d_lock);
  106. kfree(rdir);
  107. }
  108. exit:
  109. return err;
  110. }
  111. /**
  112. * v9fs_dir_readdir - read a directory
  113. * @filp: opened file structure
  114. * @dirent: directory structure ???
  115. * @filldir: function to populate directory structure ???
  116. *
  117. */
  118. static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
  119. {
  120. int over;
  121. struct p9_wstat st;
  122. int err = 0;
  123. struct p9_fid *fid;
  124. int buflen;
  125. int reclen = 0;
  126. struct p9_rdir *rdir;
  127. p9_debug(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  128. fid = filp->private_data;
  129. buflen = fid->clnt->msize - P9_IOHDRSZ;
  130. err = v9fs_alloc_rdir_buf(filp, buflen);
  131. if (err)
  132. goto exit;
  133. rdir = (struct p9_rdir *) fid->rdir;
  134. err = mutex_lock_interruptible(&rdir->mutex);
  135. if (err)
  136. return err;
  137. while (err == 0) {
  138. if (rdir->tail == rdir->head) {
  139. err = v9fs_file_readn(filp, rdir->buf, NULL,
  140. buflen, filp->f_pos);
  141. if (err <= 0)
  142. goto unlock_and_exit;
  143. rdir->head = 0;
  144. rdir->tail = err;
  145. }
  146. while (rdir->head < rdir->tail) {
  147. p9stat_init(&st);
  148. err = p9stat_read(fid->clnt, rdir->buf + rdir->head,
  149. rdir->tail - rdir->head, &st);
  150. if (err) {
  151. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  152. err = -EIO;
  153. p9stat_free(&st);
  154. goto unlock_and_exit;
  155. }
  156. reclen = st.size+2;
  157. over = filldir(dirent, st.name, strlen(st.name),
  158. filp->f_pos, v9fs_qid2ino(&st.qid), dt_type(&st));
  159. p9stat_free(&st);
  160. if (over) {
  161. err = 0;
  162. goto unlock_and_exit;
  163. }
  164. rdir->head += reclen;
  165. filp->f_pos += reclen;
  166. }
  167. }
  168. unlock_and_exit:
  169. mutex_unlock(&rdir->mutex);
  170. exit:
  171. return err;
  172. }
  173. /**
  174. * v9fs_dir_readdir_dotl - read a directory
  175. * @filp: opened file structure
  176. * @dirent: buffer to fill dirent structures
  177. * @filldir: function to populate dirent structures
  178. *
  179. */
  180. static int v9fs_dir_readdir_dotl(struct file *filp, void *dirent,
  181. filldir_t filldir)
  182. {
  183. int over;
  184. int err = 0;
  185. struct p9_fid *fid;
  186. int buflen;
  187. struct p9_rdir *rdir;
  188. struct p9_dirent curdirent;
  189. u64 oldoffset = 0;
  190. p9_debug(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  191. fid = filp->private_data;
  192. buflen = fid->clnt->msize - P9_READDIRHDRSZ;
  193. err = v9fs_alloc_rdir_buf(filp, buflen);
  194. if (err)
  195. goto exit;
  196. rdir = (struct p9_rdir *) fid->rdir;
  197. err = mutex_lock_interruptible(&rdir->mutex);
  198. if (err)
  199. return err;
  200. while (err == 0) {
  201. if (rdir->tail == rdir->head) {
  202. err = p9_client_readdir(fid, rdir->buf, buflen,
  203. filp->f_pos);
  204. if (err <= 0)
  205. goto unlock_and_exit;
  206. rdir->head = 0;
  207. rdir->tail = err;
  208. }
  209. while (rdir->head < rdir->tail) {
  210. err = p9dirent_read(fid->clnt, rdir->buf + rdir->head,
  211. rdir->tail - rdir->head,
  212. &curdirent);
  213. if (err < 0) {
  214. p9_debug(P9_DEBUG_VFS, "returned %d\n", err);
  215. err = -EIO;
  216. goto unlock_and_exit;
  217. }
  218. /* d_off in dirent structure tracks the offset into
  219. * the next dirent in the dir. However, filldir()
  220. * expects offset into the current dirent. Hence
  221. * while calling filldir send the offset from the
  222. * previous dirent structure.
  223. */
  224. over = filldir(dirent, curdirent.d_name,
  225. strlen(curdirent.d_name),
  226. oldoffset, v9fs_qid2ino(&curdirent.qid),
  227. curdirent.d_type);
  228. oldoffset = curdirent.d_off;
  229. if (over) {
  230. err = 0;
  231. goto unlock_and_exit;
  232. }
  233. filp->f_pos = curdirent.d_off;
  234. rdir->head += err;
  235. }
  236. }
  237. unlock_and_exit:
  238. mutex_unlock(&rdir->mutex);
  239. exit:
  240. return err;
  241. }
  242. /**
  243. * v9fs_dir_release - close a directory
  244. * @inode: inode of the directory
  245. * @filp: file pointer to a directory
  246. *
  247. */
  248. int v9fs_dir_release(struct inode *inode, struct file *filp)
  249. {
  250. struct p9_fid *fid;
  251. fid = filp->private_data;
  252. p9_debug(P9_DEBUG_VFS, "inode: %p filp: %p fid: %d\n",
  253. inode, filp, fid ? fid->fid : -1);
  254. if (fid)
  255. p9_client_clunk(fid);
  256. return 0;
  257. }
  258. const struct file_operations v9fs_dir_operations = {
  259. .read = generic_read_dir,
  260. .llseek = generic_file_llseek,
  261. .readdir = v9fs_dir_readdir,
  262. .open = v9fs_file_open,
  263. .release = v9fs_dir_release,
  264. };
  265. const struct file_operations v9fs_dir_operations_dotl = {
  266. .read = generic_read_dir,
  267. .llseek = generic_file_llseek,
  268. .readdir = v9fs_dir_readdir_dotl,
  269. .open = v9fs_file_open,
  270. .release = v9fs_dir_release,
  271. .fsync = v9fs_file_fsync_dotl,
  272. };