vfs_file.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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, v9fs_extended(v9ses));
  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. if ((file->f_flags & O_APPEND) && (!v9fs_extended(v9ses)))
  72. generic_file_llseek(file, 0, SEEK_END);
  73. }
  74. file->private_data = fid;
  75. if ((fid->qid.version) && (v9ses->cache)) {
  76. P9_DPRINTK(P9_DEBUG_VFS, "cached");
  77. /* enable cached file options */
  78. if(file->f_op == &v9fs_file_operations)
  79. file->f_op = &v9fs_cached_file_operations;
  80. }
  81. return 0;
  82. }
  83. /**
  84. * v9fs_file_lock - lock a file (or directory)
  85. * @filp: file to be locked
  86. * @cmd: lock command
  87. * @fl: file lock structure
  88. *
  89. * Bugs: this looks like a local only lock, we should extend into 9P
  90. * by using open exclusive
  91. */
  92. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  93. {
  94. int res = 0;
  95. struct inode *inode = filp->f_path.dentry->d_inode;
  96. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  97. /* No mandatory locks */
  98. if (__mandatory_lock(inode))
  99. return -ENOLCK;
  100. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  101. filemap_write_and_wait(inode->i_mapping);
  102. invalidate_mapping_pages(&inode->i_data, 0, -1);
  103. }
  104. return res;
  105. }
  106. /**
  107. * v9fs_file_read - read from a file
  108. * @filp: file pointer to read
  109. * @data: data buffer to read data into
  110. * @count: size of buffer
  111. * @offset: offset at which to read data
  112. *
  113. */
  114. static ssize_t
  115. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  116. loff_t * offset)
  117. {
  118. int ret;
  119. struct p9_fid *fid;
  120. P9_DPRINTK(P9_DEBUG_VFS, "\n");
  121. fid = filp->private_data;
  122. ret = p9_client_uread(fid, data, *offset, count);
  123. if (ret > 0)
  124. *offset += ret;
  125. return ret;
  126. }
  127. /**
  128. * v9fs_file_write - write to a file
  129. * @filp: file pointer to write
  130. * @data: data buffer to write data from
  131. * @count: size of buffer
  132. * @offset: offset at which to write data
  133. *
  134. */
  135. static ssize_t
  136. v9fs_file_write(struct file *filp, const char __user * data,
  137. size_t count, loff_t * offset)
  138. {
  139. int ret;
  140. struct p9_fid *fid;
  141. struct inode *inode = filp->f_path.dentry->d_inode;
  142. P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
  143. (int)count, (int)*offset);
  144. fid = filp->private_data;
  145. ret = p9_client_uwrite(fid, data, *offset, count);
  146. if (ret > 0) {
  147. invalidate_inode_pages2_range(inode->i_mapping, *offset,
  148. *offset+ret);
  149. *offset += ret;
  150. }
  151. if (*offset > inode->i_size) {
  152. inode->i_size = *offset;
  153. inode->i_blocks = (inode->i_size + 512 - 1) >> 9;
  154. }
  155. return ret;
  156. }
  157. static const struct file_operations v9fs_cached_file_operations = {
  158. .llseek = generic_file_llseek,
  159. .read = do_sync_read,
  160. .aio_read = generic_file_aio_read,
  161. .write = v9fs_file_write,
  162. .open = v9fs_file_open,
  163. .release = v9fs_dir_release,
  164. .lock = v9fs_file_lock,
  165. .mmap = generic_file_readonly_mmap,
  166. };
  167. const struct file_operations v9fs_file_operations = {
  168. .llseek = generic_file_llseek,
  169. .read = v9fs_file_read,
  170. .write = v9fs_file_write,
  171. .open = v9fs_file_open,
  172. .release = v9fs_dir_release,
  173. .lock = v9fs_file_lock,
  174. .mmap = generic_file_readonly_mmap,
  175. };