vfs_file.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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/file.h>
  29. #include <linux/stat.h>
  30. #include <linux/string.h>
  31. #include <linux/smp_lock.h>
  32. #include <linux/inet.h>
  33. #include <linux/version.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 fid = V9FS_NOFID;
  55. int err;
  56. dprintk(DEBUG_VFS, "inode: %p file: %p \n", inode, file);
  57. vfid = v9fs_fid_lookup(file->f_dentry);
  58. if (!vfid) {
  59. dprintk(DEBUG_ERROR, "Couldn't resolve fid from dentry\n");
  60. return -EBADF;
  61. }
  62. fid = v9fs_get_idpool(&v9ses->fidpool);
  63. if (fid < 0) {
  64. eprintk(KERN_WARNING, "newfid fails!\n");
  65. return -ENOSPC;
  66. }
  67. err = v9fs_t_walk(v9ses, vfid->fid, fid, NULL, NULL);
  68. if (err < 0) {
  69. dprintk(DEBUG_ERROR, "rewalk didn't work\n");
  70. goto put_fid;
  71. }
  72. /* TODO: do special things for O_EXCL, O_NOFOLLOW, O_SYNC */
  73. /* translate open mode appropriately */
  74. omode = v9fs_uflags2omode(file->f_flags);
  75. err = v9fs_t_open(v9ses, fid, omode, &fcall);
  76. if (err < 0) {
  77. PRINT_FCALL_ERROR("open failed", fcall);
  78. goto clunk_fid;
  79. }
  80. vfid = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
  81. if (vfid == NULL) {
  82. dprintk(DEBUG_ERROR, "out of memory\n");
  83. err = -ENOMEM;
  84. goto clunk_fid;
  85. }
  86. file->private_data = vfid;
  87. vfid->fid = fid;
  88. vfid->fidopen = 1;
  89. vfid->fidclunked = 0;
  90. vfid->iounit = fcall->params.ropen.iounit;
  91. vfid->rdir_pos = 0;
  92. vfid->rdir_fcall = NULL;
  93. vfid->filp = file;
  94. kfree(fcall);
  95. return 0;
  96. clunk_fid:
  97. v9fs_t_clunk(v9ses, fid);
  98. put_fid:
  99. v9fs_put_idpool(fid, &v9ses->fidpool);
  100. kfree(fcall);
  101. return err;
  102. }
  103. /**
  104. * v9fs_file_lock - lock a file (or directory)
  105. * @inode: inode to be opened
  106. * @file: file being opened
  107. *
  108. * XXX - this looks like a local only lock, we should extend into 9P
  109. * by using open exclusive
  110. */
  111. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  112. {
  113. int res = 0;
  114. struct inode *inode = filp->f_dentry->d_inode;
  115. dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  116. /* No mandatory locks */
  117. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  118. return -ENOLCK;
  119. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  120. filemap_write_and_wait(inode->i_mapping);
  121. invalidate_inode_pages(&inode->i_data);
  122. }
  123. return res;
  124. }
  125. /**
  126. * v9fs_file_read - read from a file
  127. * @filep: file pointer to read
  128. * @data: data buffer to read data into
  129. * @count: size of buffer
  130. * @offset: offset at which to read data
  131. *
  132. */
  133. static ssize_t
  134. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  135. loff_t * offset)
  136. {
  137. struct inode *inode = filp->f_dentry->d_inode;
  138. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  139. struct v9fs_fid *v9f = filp->private_data;
  140. struct v9fs_fcall *fcall = NULL;
  141. int fid = v9f->fid;
  142. int rsize = 0;
  143. int result = 0;
  144. int total = 0;
  145. int n;
  146. dprintk(DEBUG_VFS, "\n");
  147. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  148. if (v9f->iounit != 0 && rsize > v9f->iounit)
  149. rsize = v9f->iounit;
  150. do {
  151. if (count < rsize)
  152. rsize = count;
  153. result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
  154. if (result < 0) {
  155. printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
  156. result);
  157. kfree(fcall);
  158. return total;
  159. } else
  160. *offset += result;
  161. n = copy_to_user(data, fcall->params.rread.data, result);
  162. if (n) {
  163. dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
  164. kfree(fcall);
  165. return -EFAULT;
  166. }
  167. count -= result;
  168. data += result;
  169. total += result;
  170. kfree(fcall);
  171. if (result < rsize)
  172. break;
  173. } while (count);
  174. return total;
  175. }
  176. /**
  177. * v9fs_file_write - write to a file
  178. * @filep: file pointer to write
  179. * @data: data buffer to write data from
  180. * @count: size of buffer
  181. * @offset: offset at which to write data
  182. *
  183. */
  184. static ssize_t
  185. v9fs_file_write(struct file *filp, const char __user * data,
  186. size_t count, loff_t * offset)
  187. {
  188. struct inode *inode = filp->f_dentry->d_inode;
  189. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  190. struct v9fs_fid *v9fid = filp->private_data;
  191. struct v9fs_fcall *fcall;
  192. int fid = v9fid->fid;
  193. int result = -EIO;
  194. int rsize = 0;
  195. int total = 0;
  196. dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
  197. (int)*offset);
  198. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  199. if (v9fid->iounit != 0 && rsize > v9fid->iounit)
  200. rsize = v9fid->iounit;
  201. do {
  202. if (count < rsize)
  203. rsize = count;
  204. result = v9fs_t_write(v9ses, fid, *offset, rsize, data, &fcall);
  205. if (result < 0) {
  206. PRINT_FCALL_ERROR("error while writing", fcall);
  207. kfree(fcall);
  208. return result;
  209. } else
  210. *offset += result;
  211. kfree(fcall);
  212. fcall = NULL;
  213. if (result != rsize) {
  214. eprintk(KERN_ERR,
  215. "short write: v9fs_t_write returned %d\n",
  216. result);
  217. break;
  218. }
  219. count -= result;
  220. data += result;
  221. total += result;
  222. } while (count);
  223. invalidate_inode_pages2(inode->i_mapping);
  224. return total;
  225. }
  226. const struct file_operations v9fs_file_operations = {
  227. .llseek = generic_file_llseek,
  228. .read = v9fs_file_read,
  229. .write = v9fs_file_write,
  230. .open = v9fs_file_open,
  231. .release = v9fs_dir_release,
  232. .lock = v9fs_file_lock,
  233. .mmap = generic_file_mmap,
  234. };