vfs_file.c 6.3 KB

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