vfs_file.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /*
  2. * linux/fs/9p/vfs_file.c
  3. *
  4. * This file contians vfs file ops for 9P2000.
  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/sched.h>
  29. #include <linux/file.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/inet.h>
  33. #include <linux/list.h>
  34. #include <asm/uaccess.h>
  35. #include <linux/idr.h>
  36. #include <net/9p/9p.h>
  37. #include <net/9p/client.h>
  38. #include "v9fs.h"
  39. #include "v9fs_vfs.h"
  40. #include "fid.h"
  41. static const struct file_operations v9fs_cached_file_operations;
  42. /**
  43. * v9fs_file_open - open a file (or directory)
  44. * @inode: inode to be opened
  45. * @file: file being opened
  46. *
  47. */
  48. int v9fs_file_open(struct inode *inode, struct file *file)
  49. {
  50. int err;
  51. struct v9fs_session_info *v9ses;
  52. struct p9_fid *fid;
  53. int omode;
  54. P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p \n", inode, file);
  55. v9ses = v9fs_inode2v9ses(inode);
  56. omode = v9fs_uflags2omode(file->f_flags);
  57. fid = file->private_data;
  58. if (!fid) {
  59. fid = v9fs_fid_clone(file->f_path.dentry);
  60. if (IS_ERR(fid))
  61. return PTR_ERR(fid);
  62. err = p9_client_open(fid, omode);
  63. if (err < 0) {
  64. p9_client_clunk(fid);
  65. return err;
  66. }
  67. if (omode & P9_OTRUNC) {
  68. inode->i_size = 0;
  69. inode->i_blocks = 0;
  70. }
  71. }
  72. file->private_data = fid;
  73. if ((fid->qid.version) && (v9ses->cache)) {
  74. P9_DPRINTK(P9_DEBUG_VFS, "cached");
  75. /* enable cached file options */
  76. if(file->f_op == &v9fs_file_operations)
  77. file->f_op = &v9fs_cached_file_operations;
  78. }
  79. return 0;
  80. }
  81. /**
  82. * v9fs_file_lock - lock a file (or directory)
  83. * @inode: inode to be opened
  84. * @file: file being opened
  85. *
  86. * XXX - this looks like a local only lock, we should extend into 9P
  87. * by using open exclusive
  88. */
  89. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  90. {
  91. int res = 0;
  92. struct inode *inode = filp->f_path.dentry->d_inode;
  93. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  94. /* No mandatory locks */
  95. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  96. return -ENOLCK;
  97. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  98. filemap_write_and_wait(inode->i_mapping);
  99. invalidate_mapping_pages(&inode->i_data, 0, -1);
  100. }
  101. return res;
  102. }
  103. /**
  104. * v9fs_file_read - read from a file
  105. * @filep: file pointer to read
  106. * @data: data buffer to read data into
  107. * @count: size of buffer
  108. * @offset: offset at which to read data
  109. *
  110. */
  111. static ssize_t
  112. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  113. loff_t * offset)
  114. {
  115. int ret;
  116. struct p9_fid *fid;
  117. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  118. fid = filp->private_data;
  119. ret = p9_client_uread(fid, data, *offset, count);
  120. if (ret > 0)
  121. *offset += ret;
  122. return ret;
  123. }
  124. /**
  125. * v9fs_file_write - write to a file
  126. * @filep: file pointer to write
  127. * @data: data buffer to write data from
  128. * @count: size of buffer
  129. * @offset: offset at which to write data
  130. *
  131. */
  132. static ssize_t
  133. v9fs_file_write(struct file *filp, const char __user * data,
  134. size_t count, loff_t * offset)
  135. {
  136. int ret;
  137. struct p9_fid *fid;
  138. struct inode *inode = filp->f_path.dentry->d_inode;
  139. P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
  140. (int)count, (int)*offset);
  141. fid = filp->private_data;
  142. ret = p9_client_uwrite(fid, data, *offset, count);
  143. if (ret > 0)
  144. *offset += ret;
  145. if (*offset > inode->i_size) {
  146. inode->i_size = *offset;
  147. inode->i_blocks = (inode->i_size + 512 - 1) >> 9;
  148. }
  149. invalidate_inode_pages2(inode->i_mapping);
  150. return ret;
  151. }
  152. static const struct file_operations v9fs_cached_file_operations = {
  153. .llseek = generic_file_llseek,
  154. .read = do_sync_read,
  155. .aio_read = generic_file_aio_read,
  156. .write = v9fs_file_write,
  157. .open = v9fs_file_open,
  158. .release = v9fs_dir_release,
  159. .lock = v9fs_file_lock,
  160. .mmap = generic_file_mmap,
  161. };
  162. const struct file_operations v9fs_file_operations = {
  163. .llseek = generic_file_llseek,
  164. .read = v9fs_file_read,
  165. .write = v9fs_file_write,
  166. .open = v9fs_file_open,
  167. .release = v9fs_dir_release,
  168. .lock = v9fs_file_lock,
  169. .mmap = generic_file_mmap,
  170. };