vfs_dir.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. * dt_type - return file type
  41. * @mistat: mistat structure
  42. *
  43. */
  44. static inline int dt_type(struct p9_wstat *mistat)
  45. {
  46. unsigned long perm = mistat->mode;
  47. int rettype = DT_REG;
  48. if (perm & P9_DMDIR)
  49. rettype = DT_DIR;
  50. if (perm & P9_DMSYMLINK)
  51. rettype = DT_LNK;
  52. return rettype;
  53. }
  54. /**
  55. * v9fs_dir_readdir - read a directory
  56. * @filp: opened file structure
  57. * @dirent: directory structure ???
  58. * @filldir: function to populate directory structure ???
  59. *
  60. */
  61. static int v9fs_dir_readdir(struct file *filp, void *dirent, filldir_t filldir)
  62. {
  63. int over;
  64. struct p9_wstat st;
  65. int err;
  66. struct p9_fid *fid;
  67. int buflen;
  68. char *statbuf;
  69. int n, i = 0;
  70. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  71. fid = filp->private_data;
  72. buflen = fid->clnt->msize - P9_IOHDRSZ;
  73. statbuf = kmalloc(buflen, GFP_KERNEL);
  74. if (!statbuf)
  75. return -ENOMEM;
  76. while (1) {
  77. err = v9fs_file_readn(filp, statbuf, NULL, buflen,
  78. fid->rdir_fpos);
  79. if (err <= 0)
  80. break;
  81. n = err;
  82. while (i < n) {
  83. err = p9stat_read(statbuf + i, buflen-i, &st,
  84. fid->clnt->dotu);
  85. if (err) {
  86. P9_DPRINTK(P9_DEBUG_VFS, "returned %d\n", err);
  87. err = -EIO;
  88. p9stat_free(&st);
  89. goto free_and_exit;
  90. }
  91. i += st.size+2;
  92. fid->rdir_fpos += st.size+2;
  93. over = filldir(dirent, st.name, strlen(st.name),
  94. filp->f_pos, v9fs_qid2ino(&st.qid), dt_type(&st));
  95. filp->f_pos += st.size+2;
  96. p9stat_free(&st);
  97. if (over) {
  98. err = 0;
  99. goto free_and_exit;
  100. }
  101. }
  102. }
  103. free_and_exit:
  104. kfree(statbuf);
  105. return err;
  106. }
  107. /**
  108. * v9fs_dir_release - close a directory
  109. * @inode: inode of the directory
  110. * @filp: file pointer to a directory
  111. *
  112. */
  113. int v9fs_dir_release(struct inode *inode, struct file *filp)
  114. {
  115. struct p9_fid *fid;
  116. fid = filp->private_data;
  117. P9_DPRINTK(P9_DEBUG_VFS,
  118. "inode: %p filp: %p fid: %d\n", inode, filp, fid->fid);
  119. filemap_write_and_wait(inode->i_mapping);
  120. p9_client_clunk(fid);
  121. return 0;
  122. }
  123. const struct file_operations v9fs_dir_operations = {
  124. .read = generic_read_dir,
  125. .llseek = generic_file_llseek,
  126. .readdir = v9fs_dir_readdir,
  127. .open = v9fs_file_open,
  128. .release = v9fs_dir_release,
  129. };