vfs_dir.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 <net/9p/9p.h>
  35. #include <net/9p/client.h>
  36. #include "v9fs.h"
  37. #include "v9fs_vfs.h"
  38. #include "fid.h"
  39. /**
  40. * struct p9_rdir - readdir accounting
  41. * @mutex: mutex protecting readdir
  42. * @head: start offset of current dirread buffer
  43. * @tail: end offset of current dirread buffer
  44. * @buf: dirread buffer
  45. *
  46. * private structure for keeping track of readdir
  47. * allocated on demand
  48. */
  49. struct p9_rdir {
  50. struct mutex mutex;
  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_dir_readdir - read a directory
  72. * @filp: opened file structure
  73. * @dirent: directory structure ???
  74. * @filldir: function to populate directory structure ???
  75. *
  76. */
  77. static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
  78. {
  79. int over;
  80. struct p9_wstat st;
  81. int err = 0;
  82. struct p9_fid *fid;
  83. int buflen;
  84. int reclen = 0;
  85. struct p9_rdir *rdir;
  86. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  87. fid = filp->private_data;
  88. buflen = fid->clnt->msize - P9_IOHDRSZ;
  89. /* allocate rdir on demand */
  90. if (!fid->rdir) {
  91. rdir = kmalloc(sizeof(struct p9_rdir) + buflen, GFP_KERNEL);
  92. if (rdir == NULL) {
  93. err = -ENOMEM;
  94. goto exit;
  95. }
  96. spin_lock(&filp->f_dentry->d_lock);
  97. if (!fid->rdir) {
  98. rdir->buf = (uint8_t *)rdir + sizeof(struct p9_rdir);
  99. mutex_init(&rdir->mutex);
  100. rdir->head = rdir->tail = 0;
  101. fid->rdir = (void *) rdir;
  102. rdir = NULL;
  103. }
  104. spin_unlock(&filp->f_dentry->d_lock);
  105. kfree(rdir);
  106. }
  107. rdir = (struct p9_rdir *) fid->rdir;
  108. err = mutex_lock_interruptible(&rdir->mutex);
  109. while (err == 0) {
  110. if (rdir->tail == rdir->head) {
  111. err = v9fs_file_readn(filp, rdir->buf, NULL,
  112. buflen, filp->f_pos);
  113. if (err <= 0)
  114. goto unlock_and_exit;
  115. rdir->head = 0;
  116. rdir->tail = err;
  117. }
  118. while (rdir->head < rdir->tail) {
  119. err = p9stat_read(rdir->buf + rdir->head,
  120. buflen - rdir->head, &st,
  121. fid->clnt->dotu);
  122. if (err) {
  123. P9_DPRINTK(P9_DEBUG_VFS, "returned %d\n", err);
  124. err = -EIO;
  125. p9stat_free(&st);
  126. goto unlock_and_exit;
  127. }
  128. reclen = st.size+2;
  129. over = filldir(dirent, st.name, strlen(st.name),
  130. filp->f_pos, v9fs_qid2ino(&st.qid), dt_type(&st));
  131. p9stat_free(&st);
  132. if (over) {
  133. err = 0;
  134. goto unlock_and_exit;
  135. }
  136. rdir->head += reclen;
  137. filp->f_pos += reclen;
  138. }
  139. }
  140. unlock_and_exit:
  141. mutex_unlock(&rdir->mutex);
  142. exit:
  143. return err;
  144. }
  145. /**
  146. * v9fs_dir_release - close a directory
  147. * @inode: inode of the directory
  148. * @filp: file pointer to a directory
  149. *
  150. */
  151. int v9fs_dir_release(struct inode *inode, struct file *filp)
  152. {
  153. struct p9_fid *fid;
  154. fid = filp->private_data;
  155. P9_DPRINTK(P9_DEBUG_VFS,
  156. "inode: %p filp: %p fid: %d\n", inode, filp, fid->fid);
  157. filemap_write_and_wait(inode->i_mapping);
  158. p9_client_clunk(fid);
  159. return 0;
  160. }
  161. const struct file_operations v9fs_dir_operations = {
  162. .read = generic_read_dir,
  163. .llseek = generic_file_llseek,
  164. .readdir = v9fs_dir_readdir,
  165. .open = v9fs_file_open,
  166. .release = v9fs_dir_release,
  167. };