vfs_dir.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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_stat *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. * @filep: 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_fid *fid;
  65. struct v9fs_session_info *v9ses;
  66. struct inode *inode;
  67. struct p9_stat *st;
  68. P9_DPRINTK(P9_DEBUG_VFS, "name %s\n", filp->f_path.dentry->d_name.name);
  69. inode = filp->f_path.dentry->d_inode;
  70. v9ses = v9fs_inode2v9ses(inode);
  71. fid = filp->private_data;
  72. while ((st = p9_client_dirread(fid, filp->f_pos)) != NULL) {
  73. if (IS_ERR(st))
  74. return PTR_ERR(st);
  75. over = filldir(dirent, st->name.str, st->name.len, filp->f_pos,
  76. v9fs_qid2ino(&st->qid), dt_type(st));
  77. if (over)
  78. break;
  79. filp->f_pos += st->size;
  80. kfree(st);
  81. st = NULL;
  82. }
  83. kfree(st);
  84. return 0;
  85. }
  86. /**
  87. * v9fs_dir_release - close a directory
  88. * @inode: inode of the directory
  89. * @filp: file pointer to a directory
  90. *
  91. */
  92. int v9fs_dir_release(struct inode *inode, struct file *filp)
  93. {
  94. struct p9_fid *fid;
  95. fid = filp->private_data;
  96. P9_DPRINTK(P9_DEBUG_VFS,
  97. "inode: %p filp: %p fid: %d\n", inode, filp, fid->fid);
  98. filemap_write_and_wait(inode->i_mapping);
  99. p9_client_clunk(fid);
  100. return 0;
  101. }
  102. const struct file_operations v9fs_dir_operations = {
  103. .read = generic_read_dir,
  104. .readdir = v9fs_dir_readdir,
  105. .open = v9fs_file_open,
  106. .release = v9fs_dir_release,
  107. };