vfs_file.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  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. /**
  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. return 0;
  74. Clunk_Fid:
  75. v9fs_fid_clunk(v9ses, vfid);
  76. kfree(fcall);
  77. return err;
  78. }
  79. /**
  80. * v9fs_file_lock - lock a file (or directory)
  81. * @inode: inode to be opened
  82. * @file: file being opened
  83. *
  84. * XXX - this looks like a local only lock, we should extend into 9P
  85. * by using open exclusive
  86. */
  87. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  88. {
  89. int res = 0;
  90. struct inode *inode = filp->f_path.dentry->d_inode;
  91. dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  92. /* No mandatory locks */
  93. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  94. return -ENOLCK;
  95. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  96. filemap_write_and_wait(inode->i_mapping);
  97. invalidate_inode_pages(&inode->i_data);
  98. }
  99. return res;
  100. }
  101. /**
  102. * v9fs_file_read - read from a file
  103. * @filep: file pointer to read
  104. * @data: data buffer to read data into
  105. * @count: size of buffer
  106. * @offset: offset at which to read data
  107. *
  108. */
  109. static ssize_t
  110. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  111. loff_t * offset)
  112. {
  113. struct inode *inode = filp->f_path.dentry->d_inode;
  114. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  115. struct v9fs_fid *v9f = filp->private_data;
  116. struct v9fs_fcall *fcall = NULL;
  117. int fid = v9f->fid;
  118. int rsize = 0;
  119. int result = 0;
  120. int total = 0;
  121. int n;
  122. dprintk(DEBUG_VFS, "\n");
  123. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  124. if (v9f->iounit != 0 && rsize > v9f->iounit)
  125. rsize = v9f->iounit;
  126. do {
  127. if (count < rsize)
  128. rsize = count;
  129. result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
  130. if (result < 0) {
  131. printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
  132. result);
  133. kfree(fcall);
  134. return total;
  135. } else
  136. *offset += result;
  137. n = copy_to_user(data, fcall->params.rread.data, result);
  138. if (n) {
  139. dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
  140. kfree(fcall);
  141. return -EFAULT;
  142. }
  143. count -= result;
  144. data += result;
  145. total += result;
  146. kfree(fcall);
  147. if (result < rsize)
  148. break;
  149. } while (count);
  150. return total;
  151. }
  152. /**
  153. * v9fs_file_write - write to a file
  154. * @filep: file pointer to write
  155. * @data: data buffer to write data from
  156. * @count: size of buffer
  157. * @offset: offset at which to write data
  158. *
  159. */
  160. static ssize_t
  161. v9fs_file_write(struct file *filp, const char __user * data,
  162. size_t count, loff_t * offset)
  163. {
  164. struct inode *inode = filp->f_path.dentry->d_inode;
  165. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  166. struct v9fs_fid *v9fid = filp->private_data;
  167. struct v9fs_fcall *fcall;
  168. int fid = v9fid->fid;
  169. int result = -EIO;
  170. int rsize = 0;
  171. int total = 0;
  172. dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
  173. (int)*offset);
  174. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  175. if (v9fid->iounit != 0 && rsize > v9fid->iounit)
  176. rsize = v9fid->iounit;
  177. do {
  178. if (count < rsize)
  179. rsize = count;
  180. result = v9fs_t_write(v9ses, fid, *offset, rsize, data, &fcall);
  181. if (result < 0) {
  182. PRINT_FCALL_ERROR("error while writing", fcall);
  183. kfree(fcall);
  184. return result;
  185. } else
  186. *offset += result;
  187. kfree(fcall);
  188. fcall = NULL;
  189. if (result != rsize) {
  190. eprintk(KERN_ERR,
  191. "short write: v9fs_t_write returned %d\n",
  192. result);
  193. break;
  194. }
  195. count -= result;
  196. data += result;
  197. total += result;
  198. } while (count);
  199. invalidate_inode_pages2(inode->i_mapping);
  200. return total;
  201. }
  202. const struct file_operations v9fs_file_operations = {
  203. .llseek = generic_file_llseek,
  204. .read = v9fs_file_read,
  205. .write = v9fs_file_write,
  206. .open = v9fs_file_open,
  207. .release = v9fs_dir_release,
  208. .lock = v9fs_file_lock,
  209. .mmap = generic_file_mmap,
  210. };