vfs_file.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  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_fdatawrite(inode->i_mapping);
  143. filemap_fdatawait(inode->i_mapping);
  144. invalidate_inode_pages(&inode->i_data);
  145. }
  146. return res;
  147. }
  148. /**
  149. * v9fs_file_read - read from a file
  150. * @filep: file pointer to read
  151. * @data: data buffer to read data into
  152. * @count: size of buffer
  153. * @offset: offset at which to read data
  154. *
  155. */
  156. static ssize_t
  157. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  158. loff_t * offset)
  159. {
  160. struct inode *inode = filp->f_dentry->d_inode;
  161. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  162. struct v9fs_fid *v9f = filp->private_data;
  163. struct v9fs_fcall *fcall = NULL;
  164. int fid = v9f->fid;
  165. int rsize = 0;
  166. int result = 0;
  167. int total = 0;
  168. int n;
  169. dprintk(DEBUG_VFS, "\n");
  170. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  171. if (v9f->iounit != 0 && rsize > v9f->iounit)
  172. rsize = v9f->iounit;
  173. do {
  174. if (count < rsize)
  175. rsize = count;
  176. result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
  177. if (result < 0) {
  178. printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
  179. result);
  180. kfree(fcall);
  181. return total;
  182. } else
  183. *offset += result;
  184. n = copy_to_user(data, fcall->params.rread.data, result);
  185. if (n) {
  186. dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
  187. kfree(fcall);
  188. return -EFAULT;
  189. }
  190. count -= result;
  191. data += result;
  192. total += result;
  193. kfree(fcall);
  194. if (result < rsize)
  195. break;
  196. } while (count);
  197. return total;
  198. }
  199. /**
  200. * v9fs_file_write - write to a file
  201. * @filep: file pointer to write
  202. * @data: data buffer to write data from
  203. * @count: size of buffer
  204. * @offset: offset at which to write data
  205. *
  206. */
  207. static ssize_t
  208. v9fs_file_write(struct file *filp, const char __user * data,
  209. size_t count, loff_t * offset)
  210. {
  211. struct inode *inode = filp->f_dentry->d_inode;
  212. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  213. struct v9fs_fid *v9fid = filp->private_data;
  214. struct v9fs_fcall *fcall;
  215. int fid = v9fid->fid;
  216. int result = -EIO;
  217. int rsize = 0;
  218. int total = 0;
  219. char *buf;
  220. dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
  221. (int)*offset);
  222. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  223. if (v9fid->iounit != 0 && rsize > v9fid->iounit)
  224. rsize = v9fid->iounit;
  225. buf = kmalloc(v9ses->maxdata - V9FS_IOHDRSZ, GFP_KERNEL);
  226. if (!buf)
  227. return -ENOMEM;
  228. do {
  229. if (count < rsize)
  230. rsize = count;
  231. result = copy_from_user(buf, data, rsize);
  232. if (result) {
  233. dprintk(DEBUG_ERROR, "Problem copying from user\n");
  234. kfree(buf);
  235. return -EFAULT;
  236. }
  237. dump_data(buf, rsize);
  238. result = v9fs_t_write(v9ses, fid, *offset, rsize, buf, &fcall);
  239. if (result < 0) {
  240. eprintk(KERN_ERR, "error while writing: %s(%d)\n",
  241. FCALL_ERROR(fcall), result);
  242. kfree(fcall);
  243. kfree(buf);
  244. return result;
  245. } else
  246. *offset += result;
  247. kfree(fcall);
  248. fcall = NULL;
  249. if (result != rsize) {
  250. eprintk(KERN_ERR,
  251. "short write: v9fs_t_write returned %d\n",
  252. result);
  253. break;
  254. }
  255. count -= result;
  256. data += result;
  257. total += result;
  258. } while (count);
  259. kfree(buf);
  260. return total;
  261. }
  262. struct file_operations v9fs_file_operations = {
  263. .llseek = generic_file_llseek,
  264. .read = v9fs_file_read,
  265. .write = v9fs_file_write,
  266. .open = v9fs_file_open,
  267. .release = v9fs_dir_release,
  268. .lock = v9fs_file_lock,
  269. };