vfs_file.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  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 as published by
  11. * the Free Software Foundation; either version 2 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to:
  21. * Free Software Foundation
  22. * 51 Franklin Street, Fifth Floor
  23. * Boston, MA 02111-1301 USA
  24. *
  25. */
  26. #include <linux/module.h>
  27. #include <linux/errno.h>
  28. #include <linux/fs.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/version.h>
  35. #include <linux/list.h>
  36. #include <asm/uaccess.h>
  37. #include <linux/idr.h>
  38. #include "debug.h"
  39. #include "v9fs.h"
  40. #include "9p.h"
  41. #include "v9fs_vfs.h"
  42. #include "fid.h"
  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 *v9fid, *fid;
  53. struct v9fs_fcall *fcall = NULL;
  54. int open_mode = 0;
  55. unsigned int iounit = 0;
  56. int newfid = -1;
  57. long result = -1;
  58. dprintk(DEBUG_VFS, "inode: %p file: %p \n", inode, file);
  59. v9fid = v9fs_fid_get_created(file->f_dentry);
  60. if (!v9fid)
  61. v9fid = v9fs_fid_lookup(file->f_dentry);
  62. if (!v9fid) {
  63. dprintk(DEBUG_ERROR, "Couldn't resolve fid from dentry\n");
  64. return -EBADF;
  65. }
  66. if (!v9fid->fidcreate) {
  67. fid = kmalloc(sizeof(struct v9fs_fid), GFP_KERNEL);
  68. if (fid == NULL) {
  69. dprintk(DEBUG_ERROR, "Out of Memory\n");
  70. return -ENOMEM;
  71. }
  72. fid->fidopen = 0;
  73. fid->fidcreate = 0;
  74. fid->fidclunked = 0;
  75. fid->iounit = 0;
  76. fid->v9ses = v9ses;
  77. newfid = v9fs_get_idpool(&v9ses->fidpool);
  78. if (newfid < 0) {
  79. eprintk(KERN_WARNING, "newfid fails!\n");
  80. return -ENOSPC;
  81. }
  82. result =
  83. v9fs_t_walk(v9ses, v9fid->fid, newfid, NULL, NULL);
  84. if (result < 0) {
  85. v9fs_put_idpool(newfid, &v9ses->fidpool);
  86. dprintk(DEBUG_ERROR, "rewalk didn't work\n");
  87. return -EBADF;
  88. }
  89. fid->fid = newfid;
  90. v9fid = fid;
  91. /* TODO: do special things for O_EXCL, O_NOFOLLOW, O_SYNC */
  92. /* translate open mode appropriately */
  93. open_mode = file->f_flags & 0x3;
  94. if (file->f_flags & O_EXCL)
  95. open_mode |= V9FS_OEXCL;
  96. if (v9ses->extended) {
  97. if (file->f_flags & O_TRUNC)
  98. open_mode |= V9FS_OTRUNC;
  99. if (file->f_flags & O_APPEND)
  100. open_mode |= V9FS_OAPPEND;
  101. }
  102. result = v9fs_t_open(v9ses, newfid, open_mode, &fcall);
  103. if (result < 0) {
  104. PRINT_FCALL_ERROR("open failed", fcall);
  105. kfree(fcall);
  106. return result;
  107. }
  108. iounit = fcall->params.ropen.iounit;
  109. kfree(fcall);
  110. } else {
  111. /* create case */
  112. newfid = v9fid->fid;
  113. iounit = v9fid->iounit;
  114. v9fid->fidcreate = 0;
  115. }
  116. file->private_data = v9fid;
  117. v9fid->rdir_pos = 0;
  118. v9fid->rdir_fcall = NULL;
  119. v9fid->fidopen = 1;
  120. v9fid->filp = file;
  121. v9fid->iounit = iounit;
  122. return 0;
  123. }
  124. /**
  125. * v9fs_file_lock - lock a file (or directory)
  126. * @inode: inode to be opened
  127. * @file: file being opened
  128. *
  129. * XXX - this looks like a local only lock, we should extend into 9P
  130. * by using open exclusive
  131. */
  132. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  133. {
  134. int res = 0;
  135. struct inode *inode = filp->f_dentry->d_inode;
  136. dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  137. /* No mandatory locks */
  138. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  139. return -ENOLCK;
  140. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  141. filemap_write_and_wait(inode->i_mapping);
  142. invalidate_inode_pages(&inode->i_data);
  143. }
  144. return res;
  145. }
  146. /**
  147. * v9fs_file_read - read from a file
  148. * @filep: file pointer to read
  149. * @data: data buffer to read data into
  150. * @count: size of buffer
  151. * @offset: offset at which to read data
  152. *
  153. */
  154. static ssize_t
  155. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  156. loff_t * offset)
  157. {
  158. struct inode *inode = filp->f_dentry->d_inode;
  159. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  160. struct v9fs_fid *v9f = filp->private_data;
  161. struct v9fs_fcall *fcall = NULL;
  162. int fid = v9f->fid;
  163. int rsize = 0;
  164. int result = 0;
  165. int total = 0;
  166. int n;
  167. dprintk(DEBUG_VFS, "\n");
  168. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  169. if (v9f->iounit != 0 && rsize > v9f->iounit)
  170. rsize = v9f->iounit;
  171. do {
  172. if (count < rsize)
  173. rsize = count;
  174. result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
  175. if (result < 0) {
  176. printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
  177. result);
  178. kfree(fcall);
  179. return total;
  180. } else
  181. *offset += result;
  182. n = copy_to_user(data, fcall->params.rread.data, result);
  183. if (n) {
  184. dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
  185. kfree(fcall);
  186. return -EFAULT;
  187. }
  188. count -= result;
  189. data += result;
  190. total += result;
  191. kfree(fcall);
  192. if (result < rsize)
  193. break;
  194. } while (count);
  195. return total;
  196. }
  197. /**
  198. * v9fs_file_write - write to a file
  199. * @filep: file pointer to write
  200. * @data: data buffer to write data from
  201. * @count: size of buffer
  202. * @offset: offset at which to write data
  203. *
  204. */
  205. static ssize_t
  206. v9fs_file_write(struct file *filp, const char __user * data,
  207. size_t count, loff_t * offset)
  208. {
  209. struct inode *inode = filp->f_dentry->d_inode;
  210. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  211. struct v9fs_fid *v9fid = filp->private_data;
  212. struct v9fs_fcall *fcall;
  213. int fid = v9fid->fid;
  214. int result = -EIO;
  215. int rsize = 0;
  216. int total = 0;
  217. dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
  218. (int)*offset);
  219. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  220. if (v9fid->iounit != 0 && rsize > v9fid->iounit)
  221. rsize = v9fid->iounit;
  222. do {
  223. if (count < rsize)
  224. rsize = count;
  225. result = v9fs_t_write(v9ses, fid, *offset, rsize, data, &fcall);
  226. if (result < 0) {
  227. PRINT_FCALL_ERROR("error while writing", fcall);
  228. kfree(fcall);
  229. return result;
  230. } else
  231. *offset += result;
  232. kfree(fcall);
  233. fcall = NULL;
  234. if (result != rsize) {
  235. eprintk(KERN_ERR,
  236. "short write: v9fs_t_write returned %d\n",
  237. result);
  238. break;
  239. }
  240. count -= result;
  241. data += result;
  242. total += result;
  243. } while (count);
  244. if(inode->i_mapping->nrpages)
  245. invalidate_inode_pages2(inode->i_mapping);
  246. return total;
  247. }
  248. struct file_operations v9fs_file_operations = {
  249. .llseek = generic_file_llseek,
  250. .read = v9fs_file_read,
  251. .write = v9fs_file_write,
  252. .open = v9fs_file_open,
  253. .release = v9fs_dir_release,
  254. .lock = v9fs_file_lock,
  255. .mmap = generic_file_mmap,
  256. };