vfs_file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269
  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/smp_lock.h>
  33. #include <linux/inet.h>
  34. #include <linux/list.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/idr.h>
  37. #include "debug.h"
  38. #include "v9fs.h"
  39. #include "9p.h"
  40. #include "v9fs_vfs.h"
  41. #include "fid.h"
  42. static const struct file_operations v9fs_cached_file_operations;
  43. /**
  44. * v9fs_file_open - open a file (or directory)
  45. * @inode: inode to be opened
  46. * @file: file being opened
  47. *
  48. */
  49. int v9fs_file_open(struct inode *inode, struct file *file)
  50. {
  51. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  52. struct v9fs_fid *vfid;
  53. struct v9fs_fcall *fcall = NULL;
  54. int omode;
  55. int err;
  56. dprintk(DEBUG_VFS, "inode: %p file: %p \n", inode, file);
  57. vfid = v9fs_fid_clone(file->f_path.dentry);
  58. if (IS_ERR(vfid))
  59. return PTR_ERR(vfid);
  60. omode = v9fs_uflags2omode(file->f_flags);
  61. err = v9fs_t_open(v9ses, vfid->fid, omode, &fcall);
  62. if (err < 0) {
  63. PRINT_FCALL_ERROR("open failed", fcall);
  64. goto Clunk_Fid;
  65. }
  66. file->private_data = vfid;
  67. vfid->fidopen = 1;
  68. vfid->fidclunked = 0;
  69. vfid->iounit = fcall->params.ropen.iounit;
  70. vfid->rdir_pos = 0;
  71. vfid->rdir_fcall = NULL;
  72. vfid->filp = file;
  73. kfree(fcall);
  74. if((vfid->qid.version) && (v9ses->cache)) {
  75. dprintk(DEBUG_VFS, "cached");
  76. /* enable cached file options */
  77. if(file->f_op == &v9fs_file_operations)
  78. file->f_op = &v9fs_cached_file_operations;
  79. }
  80. return 0;
  81. Clunk_Fid:
  82. v9fs_fid_clunk(v9ses, vfid);
  83. kfree(fcall);
  84. return err;
  85. }
  86. /**
  87. * v9fs_file_lock - lock a file (or directory)
  88. * @inode: inode to be opened
  89. * @file: file being opened
  90. *
  91. * XXX - this looks like a local only lock, we should extend into 9P
  92. * by using open exclusive
  93. */
  94. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  95. {
  96. int res = 0;
  97. struct inode *inode = filp->f_path.dentry->d_inode;
  98. dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  99. /* No mandatory locks */
  100. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  101. return -ENOLCK;
  102. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  103. filemap_write_and_wait(inode->i_mapping);
  104. invalidate_mapping_pages(&inode->i_data, 0, -1);
  105. }
  106. return res;
  107. }
  108. /**
  109. * v9fs_file_read - read from a file
  110. * @filep: file pointer to read
  111. * @data: data buffer to read data into
  112. * @count: size of buffer
  113. * @offset: offset at which to read data
  114. *
  115. */
  116. static ssize_t
  117. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  118. loff_t * offset)
  119. {
  120. struct inode *inode = filp->f_path.dentry->d_inode;
  121. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  122. struct v9fs_fid *v9f = filp->private_data;
  123. struct v9fs_fcall *fcall = NULL;
  124. int fid = v9f->fid;
  125. int rsize = 0;
  126. int result = 0;
  127. int total = 0;
  128. int n;
  129. dprintk(DEBUG_VFS, "\n");
  130. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  131. if (v9f->iounit != 0 && rsize > v9f->iounit)
  132. rsize = v9f->iounit;
  133. do {
  134. if (count < rsize)
  135. rsize = count;
  136. result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
  137. if (result < 0) {
  138. printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
  139. result);
  140. kfree(fcall);
  141. return total;
  142. } else
  143. *offset += result;
  144. n = copy_to_user(data, fcall->params.rread.data, result);
  145. if (n) {
  146. dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
  147. kfree(fcall);
  148. return -EFAULT;
  149. }
  150. count -= result;
  151. data += result;
  152. total += result;
  153. kfree(fcall);
  154. if (result < rsize)
  155. break;
  156. } while (count);
  157. return total;
  158. }
  159. /**
  160. * v9fs_file_write - write to a file
  161. * @filep: file pointer to write
  162. * @data: data buffer to write data from
  163. * @count: size of buffer
  164. * @offset: offset at which to write data
  165. *
  166. */
  167. static ssize_t
  168. v9fs_file_write(struct file *filp, const char __user * data,
  169. size_t count, loff_t * offset)
  170. {
  171. struct inode *inode = filp->f_path.dentry->d_inode;
  172. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  173. struct v9fs_fid *v9fid = filp->private_data;
  174. struct v9fs_fcall *fcall;
  175. int fid = v9fid->fid;
  176. int result = -EIO;
  177. int rsize = 0;
  178. int total = 0;
  179. dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
  180. (int)*offset);
  181. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  182. if (v9fid->iounit != 0 && rsize > v9fid->iounit)
  183. rsize = v9fid->iounit;
  184. do {
  185. if (count < rsize)
  186. rsize = count;
  187. result = v9fs_t_write(v9ses, fid, *offset, rsize, data, &fcall);
  188. if (result < 0) {
  189. PRINT_FCALL_ERROR("error while writing", fcall);
  190. kfree(fcall);
  191. return result;
  192. } else
  193. *offset += result;
  194. kfree(fcall);
  195. fcall = NULL;
  196. if (result != rsize) {
  197. eprintk(KERN_ERR,
  198. "short write: v9fs_t_write returned %d\n",
  199. result);
  200. break;
  201. }
  202. count -= result;
  203. data += result;
  204. total += result;
  205. } while (count);
  206. invalidate_inode_pages2(inode->i_mapping);
  207. return total;
  208. }
  209. static const struct file_operations v9fs_cached_file_operations = {
  210. .llseek = generic_file_llseek,
  211. .read = do_sync_read,
  212. .aio_read = generic_file_aio_read,
  213. .write = v9fs_file_write,
  214. .open = v9fs_file_open,
  215. .release = v9fs_dir_release,
  216. .lock = v9fs_file_lock,
  217. .mmap = generic_file_mmap,
  218. };
  219. const struct file_operations v9fs_file_operations = {
  220. .llseek = generic_file_llseek,
  221. .read = v9fs_file_read,
  222. .write = v9fs_file_write,
  223. .open = v9fs_file_open,
  224. .release = v9fs_dir_release,
  225. .lock = v9fs_file_lock,
  226. .mmap = generic_file_mmap,
  227. };