vfs_file.c 7.1 KB

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