vfs_dir.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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_dir_readdir - read a directory
  81. * @filp: opened file structure
  82. * @dirent: directory structure ???
  83. * @filldir: function to populate directory structure ???
  84. *
  85. */
  86. static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
  87. {
  88. int over;
  89. struct p9_wstat st;
  90. int err = 0;
  91. struct p9_fid *fid;
  92. int buflen;
  93. int reclen = 0;
  94. struct p9_rdir *rdir;
  95. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  96. fid = filp->private_data;
  97. buflen = fid->clnt->msize - P9_IOHDRSZ;
  98. /* allocate rdir on demand */
  99. if (!fid->rdir) {
  100. rdir = kmalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
  101. if (rdir == NULL) {
  102. err = -ENOMEM;
  103. goto exit;
  104. }
  105. spin_lock(&filp->f_dentry->d_lock);
  106. if (!fid->rdir) {
  107. rdir->buf = (uint8_t *)rdir + sizeof(struct p9_rdir);
  108. mutex_init(&rdir->mutex);
  109. rdir->head = rdir->tail = 0;
  110. fid->rdir = (void *) rdir;
  111. rdir = NULL;
  112. }
  113. spin_unlock(&filp->f_dentry->d_lock);
  114. kfree(rdir);
  115. }
  116. rdir = (struct p9_rdir *) fid->rdir;
  117. err = mutex_lock_interruptible(&rdir->mutex);
  118. if (err)
  119. return err;
  120. while (err == 0) {
  121. if (rdir->tail == rdir->head) {
  122. err = v9fs_file_readn(filp, rdir->buf, NULL,
  123. buflen, filp->f_pos);
  124. if (err <= 0)
  125. goto unlock_and_exit;
  126. rdir->head = 0;
  127. rdir->tail = err;
  128. }
  129. while (rdir->head < rdir->tail) {
  130. p9stat_init(&st);
  131. err = p9stat_read(rdir->buf + rdir->head,
  132. buflen - rdir->head, &st,
  133. fid->clnt->proto_version);
  134. if (err) {
  135. P9_DPRINTK(P9_DEBUG_VFS, "returned %d\n", err);
  136. err = -EIO;
  137. p9stat_free(&st);
  138. goto unlock_and_exit;
  139. }
  140. reclen = st.size+2;
  141. over = filldir(dirent, st.name, strlen(st.name),
  142. filp->f_pos, v9fs_qid2ino(&st.qid), dt_type(&st));
  143. p9stat_free(&st);
  144. if (over) {
  145. err = 0;
  146. goto unlock_and_exit;
  147. }
  148. rdir->head += reclen;
  149. filp->f_pos += reclen;
  150. }
  151. }
  152. unlock_and_exit:
  153. mutex_unlock(&rdir->mutex);
  154. exit:
  155. return err;
  156. }
  157. /**
  158. * v9fs_dir_release - close a directory
  159. * @inode: inode of the directory
  160. * @filp: file pointer to a directory
  161. *
  162. */
  163. int v9fs_dir_release(struct inode *inode, struct file *filp)
  164. {
  165. struct p9_fid *fid;
  166. fid = filp->private_data;
  167. P9_DPRINTK(P9_DEBUG_VFS,
  168. "inode: %p filp: %p fid: %d\n", inode, filp, fid->fid);
  169. filemap_write_and_wait(inode->i_mapping);
  170. p9_client_clunk(fid);
  171. return 0;
  172. }
  173. const struct file_operations v9fs_dir_operations = {
  174. .read = generic_read_dir,
  175. .llseek = generic_file_llseek,
  176. .readdir = v9fs_dir_readdir,
  177. .open = v9fs_file_open,
  178. .release = v9fs_dir_release,
  179. };