vfs_dir.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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_DPRINTK(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(rdir->buf + rdir->head,
  149. rdir->tail - rdir->head, &st,
  150. fid->clnt->proto_version);
  151. if (err) {
  152. P9_DPRINTK(P9_DEBUG_VFS, "returned %d\n", err);
  153. err = -EIO;
  154. p9stat_free(&st);
  155. goto unlock_and_exit;
  156. }
  157. reclen = st.size+2;
  158. over = filldir(dirent, st.name, strlen(st.name),
  159. filp->f_pos, v9fs_qid2ino(&st.qid), dt_type(&st));
  160. p9stat_free(&st);
  161. if (over) {
  162. err = 0;
  163. goto unlock_and_exit;
  164. }
  165. rdir->head += reclen;
  166. filp->f_pos += reclen;
  167. }
  168. }
  169. unlock_and_exit:
  170. mutex_unlock(&rdir->mutex);
  171. exit:
  172. return err;
  173. }
  174. /**
  175. * v9fs_dir_readdir_dotl - read a directory
  176. * @filp: opened file structure
  177. * @dirent: buffer to fill dirent structures
  178. * @filldir: function to populate dirent structures
  179. *
  180. */
  181. static int v9fs_dir_readdir_dotl(struct file *filp, void *dirent,
  182. filldir_t filldir)
  183. {
  184. int over;
  185. int err = 0;
  186. struct p9_fid *fid;
  187. int buflen;
  188. struct p9_rdir *rdir;
  189. struct p9_dirent curdirent;
  190. u64 oldoffset = 0;
  191. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  192. fid = filp->private_data;
  193. buflen = fid->clnt->msize - P9_READDIRHDRSZ;
  194. err = v9fs_alloc_rdir_buf(filp, buflen);
  195. if (err)
  196. goto exit;
  197. rdir = (struct p9_rdir *) fid->rdir;
  198. err = mutex_lock_interruptible(&rdir->mutex);
  199. if (err)
  200. return err;
  201. while (err == 0) {
  202. if (rdir->tail == rdir->head) {
  203. err = p9_client_readdir(fid, rdir->buf, buflen,
  204. filp->f_pos);
  205. if (err <= 0)
  206. goto unlock_and_exit;
  207. rdir->head = 0;
  208. rdir->tail = err;
  209. }
  210. while (rdir->head < rdir->tail) {
  211. err = p9dirent_read(rdir->buf + rdir->head,
  212. rdir->tail - rdir->head,
  213. &curdirent,
  214. fid->clnt->proto_version);
  215. if (err < 0) {
  216. P9_DPRINTK(P9_DEBUG_VFS, "returned %d\n", err);
  217. err = -EIO;
  218. goto unlock_and_exit;
  219. }
  220. /* d_off in dirent structure tracks the offset into
  221. * the next dirent in the dir. However, filldir()
  222. * expects offset into the current dirent. Hence
  223. * while calling filldir send the offset from the
  224. * previous dirent structure.
  225. */
  226. over = filldir(dirent, curdirent.d_name,
  227. strlen(curdirent.d_name),
  228. oldoffset, v9fs_qid2ino(&curdirent.qid),
  229. curdirent.d_type);
  230. oldoffset = curdirent.d_off;
  231. if (over) {
  232. err = 0;
  233. goto unlock_and_exit;
  234. }
  235. filp->f_pos = curdirent.d_off;
  236. rdir->head += err;
  237. }
  238. }
  239. unlock_and_exit:
  240. mutex_unlock(&rdir->mutex);
  241. exit:
  242. return err;
  243. }
  244. /**
  245. * v9fs_dir_release - close a directory
  246. * @inode: inode of the directory
  247. * @filp: file pointer to a directory
  248. *
  249. */
  250. int v9fs_dir_release(struct inode *inode, struct file *filp)
  251. {
  252. struct p9_fid *fid;
  253. fid = filp->private_data;
  254. P9_DPRINTK(P9_DEBUG_VFS,
  255. "v9fs_dir_release: inode: %p filp: %p fid: %d\n",
  256. inode, filp, fid ? fid->fid : -1);
  257. if (fid)
  258. p9_client_clunk(fid);
  259. return 0;
  260. }
  261. const struct file_operations v9fs_dir_operations = {
  262. .read = generic_read_dir,
  263. .llseek = generic_file_llseek,
  264. .readdir = v9fs_dir_readdir,
  265. .open = v9fs_file_open,
  266. .release = v9fs_dir_release,
  267. };
  268. const struct file_operations v9fs_dir_operations_dotl = {
  269. .read = generic_read_dir,
  270. .llseek = generic_file_llseek,
  271. .readdir = v9fs_dir_readdir_dotl,
  272. .open = v9fs_file_open,
  273. .release = v9fs_dir_release,
  274. .fsync = v9fs_file_fsync_dotl,
  275. };