vfs_file.c 6.1 KB

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