vfs_file.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. dprintk(DEBUG_ERROR,
  105. "open failed, open_mode 0x%x: %s\n", open_mode,
  106. FCALL_ERROR(fcall));
  107. kfree(fcall);
  108. return result;
  109. }
  110. iounit = fcall->params.ropen.iounit;
  111. kfree(fcall);
  112. } else {
  113. /* create case */
  114. newfid = v9fid->fid;
  115. iounit = v9fid->iounit;
  116. v9fid->fidcreate = 0;
  117. }
  118. file->private_data = v9fid;
  119. v9fid->rdir_pos = 0;
  120. v9fid->rdir_fcall = NULL;
  121. v9fid->fidopen = 1;
  122. v9fid->filp = file;
  123. v9fid->iounit = iounit;
  124. return 0;
  125. }
  126. /**
  127. * v9fs_file_lock - lock a file (or directory)
  128. * @inode: inode to be opened
  129. * @file: file being opened
  130. *
  131. * XXX - this looks like a local only lock, we should extend into 9P
  132. * by using open exclusive
  133. */
  134. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  135. {
  136. int res = 0;
  137. struct inode *inode = filp->f_dentry->d_inode;
  138. dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  139. /* No mandatory locks */
  140. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  141. return -ENOLCK;
  142. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  143. filemap_fdatawrite(inode->i_mapping);
  144. filemap_fdatawait(inode->i_mapping);
  145. invalidate_inode_pages(&inode->i_data);
  146. }
  147. return res;
  148. }
  149. /**
  150. * v9fs_file_read - read from a file
  151. * @filep: file pointer to read
  152. * @data: data buffer to read data into
  153. * @count: size of buffer
  154. * @offset: offset at which to read data
  155. *
  156. */
  157. static ssize_t
  158. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  159. loff_t * offset)
  160. {
  161. struct inode *inode = filp->f_dentry->d_inode;
  162. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  163. struct v9fs_fid *v9f = filp->private_data;
  164. struct v9fs_fcall *fcall = NULL;
  165. int fid = v9f->fid;
  166. int rsize = 0;
  167. int result = 0;
  168. int total = 0;
  169. int n;
  170. dprintk(DEBUG_VFS, "\n");
  171. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  172. if (v9f->iounit != 0 && rsize > v9f->iounit)
  173. rsize = v9f->iounit;
  174. do {
  175. if (count < rsize)
  176. rsize = count;
  177. result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
  178. if (result < 0) {
  179. printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
  180. result);
  181. kfree(fcall);
  182. return total;
  183. } else
  184. *offset += result;
  185. n = copy_to_user(data, fcall->params.rread.data, result);
  186. if (n) {
  187. dprintk(DEBUG_ERROR, "Problem copying to user %d\n", n);
  188. kfree(fcall);
  189. return -EFAULT;
  190. }
  191. count -= result;
  192. data += result;
  193. total += result;
  194. kfree(fcall);
  195. if (result < rsize)
  196. break;
  197. } while (count);
  198. return total;
  199. }
  200. /**
  201. * v9fs_file_write - write to a file
  202. * @filep: file pointer to write
  203. * @data: data buffer to write data from
  204. * @count: size of buffer
  205. * @offset: offset at which to write data
  206. *
  207. */
  208. static ssize_t
  209. v9fs_file_write(struct file *filp, const char __user * data,
  210. size_t count, loff_t * offset)
  211. {
  212. struct inode *inode = filp->f_dentry->d_inode;
  213. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  214. struct v9fs_fid *v9fid = filp->private_data;
  215. struct v9fs_fcall *fcall;
  216. int fid = v9fid->fid;
  217. int result = -EIO;
  218. int rsize = 0;
  219. int total = 0;
  220. char *buf;
  221. dprintk(DEBUG_VFS, "data %p count %d offset %x\n", data, (int)count,
  222. (int)*offset);
  223. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  224. if (v9fid->iounit != 0 && rsize > v9fid->iounit)
  225. rsize = v9fid->iounit;
  226. buf = kmalloc(v9ses->maxdata - V9FS_IOHDRSZ, GFP_KERNEL);
  227. if (!buf)
  228. return -ENOMEM;
  229. do {
  230. if (count < rsize)
  231. rsize = count;
  232. result = copy_from_user(buf, data, rsize);
  233. if (result) {
  234. dprintk(DEBUG_ERROR, "Problem copying from user\n");
  235. kfree(buf);
  236. return -EFAULT;
  237. }
  238. dump_data(buf, rsize);
  239. result = v9fs_t_write(v9ses, fid, *offset, rsize, buf, &fcall);
  240. if (result < 0) {
  241. eprintk(KERN_ERR, "error while writing: %s(%d)\n",
  242. FCALL_ERROR(fcall), result);
  243. kfree(fcall);
  244. kfree(buf);
  245. return result;
  246. } else
  247. *offset += result;
  248. kfree(fcall);
  249. fcall = NULL;
  250. if (result != rsize) {
  251. eprintk(KERN_ERR,
  252. "short write: v9fs_t_write returned %d\n",
  253. result);
  254. break;
  255. }
  256. count -= result;
  257. data += result;
  258. total += result;
  259. } while (count);
  260. kfree(buf);
  261. return total;
  262. }
  263. struct file_operations v9fs_file_operations = {
  264. .llseek = generic_file_llseek,
  265. .read = v9fs_file_read,
  266. .write = v9fs_file_write,
  267. .open = v9fs_file_open,
  268. .release = v9fs_dir_release,
  269. .lock = v9fs_file_lock,
  270. };