vfs_file.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  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 = v9fs_fid_lookup(file->f_dentry, FID_WALK);
  53. struct v9fs_fid *v9newfid = NULL;
  54. struct v9fs_fcall *fcall = NULL;
  55. int open_mode = 0;
  56. unsigned int iounit = 0;
  57. int newfid = -1;
  58. long result = -1;
  59. dprintk(DEBUG_VFS, "inode: %p file: %p v9fid= %p\n", inode, file,
  60. v9fid);
  61. if (!v9fid) {
  62. struct dentry *dentry = file->f_dentry;
  63. dprintk(DEBUG_ERROR, "Couldn't resolve fid from dentry\n");
  64. /* XXX - some duplication from lookup, generalize later */
  65. /* basically vfs_lookup is too heavy weight */
  66. v9fid = v9fs_fid_lookup(file->f_dentry, FID_OP);
  67. if (!v9fid)
  68. return -EBADF;
  69. v9fid = v9fs_fid_lookup(dentry->d_parent, FID_WALK);
  70. if (!v9fid)
  71. return -EBADF;
  72. newfid = v9fs_get_idpool(&v9ses->fidpool);
  73. if (newfid < 0) {
  74. eprintk(KERN_WARNING, "newfid fails!\n");
  75. return -ENOSPC;
  76. }
  77. result =
  78. v9fs_t_walk(v9ses, v9fid->fid, newfid,
  79. (char *)file->f_dentry->d_name.name, NULL);
  80. if (result < 0) {
  81. v9fs_put_idpool(newfid, &v9ses->fidpool);
  82. dprintk(DEBUG_ERROR, "rewalk didn't work\n");
  83. return -EBADF;
  84. }
  85. v9fid = v9fs_fid_create(dentry);
  86. if (v9fid == NULL) {
  87. dprintk(DEBUG_ERROR, "couldn't insert\n");
  88. return -ENOMEM;
  89. }
  90. v9fid->fid = newfid;
  91. }
  92. if (v9fid->fidcreate) {
  93. /* create case */
  94. newfid = v9fid->fid;
  95. iounit = v9fid->iounit;
  96. v9fid->fidcreate = 0;
  97. } else {
  98. if (!S_ISDIR(inode->i_mode))
  99. newfid = v9fid->fid;
  100. else {
  101. newfid = v9fs_get_idpool(&v9ses->fidpool);
  102. if (newfid < 0) {
  103. eprintk(KERN_WARNING, "allocation failed\n");
  104. return -ENOSPC;
  105. }
  106. /* This would be a somewhat critical clone */
  107. result =
  108. v9fs_t_walk(v9ses, v9fid->fid, newfid, NULL,
  109. &fcall);
  110. if (result < 0) {
  111. dprintk(DEBUG_ERROR, "clone error: %s\n",
  112. FCALL_ERROR(fcall));
  113. kfree(fcall);
  114. return result;
  115. }
  116. v9newfid = v9fs_fid_create(file->f_dentry);
  117. v9newfid->fid = newfid;
  118. v9newfid->qid = v9fid->qid;
  119. v9newfid->iounit = v9fid->iounit;
  120. v9newfid->fidopen = 0;
  121. v9newfid->fidclunked = 0;
  122. v9newfid->v9ses = v9ses;
  123. v9fid = v9newfid;
  124. kfree(fcall);
  125. }
  126. /* TODO: do special things for O_EXCL, O_NOFOLLOW, O_SYNC */
  127. /* translate open mode appropriately */
  128. open_mode = file->f_flags & 0x3;
  129. if (file->f_flags & O_EXCL)
  130. open_mode |= V9FS_OEXCL;
  131. if (v9ses->extended) {
  132. if (file->f_flags & O_TRUNC)
  133. open_mode |= V9FS_OTRUNC;
  134. if (file->f_flags & O_APPEND)
  135. open_mode |= V9FS_OAPPEND;
  136. }
  137. result = v9fs_t_open(v9ses, newfid, open_mode, &fcall);
  138. if (result < 0) {
  139. dprintk(DEBUG_ERROR,
  140. "open failed, open_mode 0x%x: %s\n", open_mode,
  141. FCALL_ERROR(fcall));
  142. kfree(fcall);
  143. return result;
  144. }
  145. iounit = fcall->params.ropen.iounit;
  146. kfree(fcall);
  147. }
  148. file->private_data = v9fid;
  149. v9fid->rdir_pos = 0;
  150. v9fid->rdir_fcall = NULL;
  151. v9fid->fidopen = 1;
  152. v9fid->filp = file;
  153. v9fid->iounit = iounit;
  154. return 0;
  155. }
  156. /**
  157. * v9fs_file_lock - lock a file (or directory)
  158. * @inode: inode to be opened
  159. * @file: file being opened
  160. *
  161. * XXX - this looks like a local only lock, we should extend into 9P
  162. * by using open exclusive
  163. */
  164. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  165. {
  166. int res = 0;
  167. struct inode *inode = filp->f_dentry->d_inode;
  168. dprintk(DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  169. /* No mandatory locks */
  170. if ((inode->i_mode & (S_ISGID | S_IXGRP)) == S_ISGID)
  171. return -ENOLCK;
  172. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  173. filemap_fdatawrite(inode->i_mapping);
  174. filemap_fdatawait(inode->i_mapping);
  175. invalidate_inode_pages(&inode->i_data);
  176. }
  177. return res;
  178. }
  179. /**
  180. * v9fs_read - read from a file (internal)
  181. * @filep: file pointer to read
  182. * @data: data buffer to read data into
  183. * @count: size of buffer
  184. * @offset: offset at which to read data
  185. *
  186. */
  187. static ssize_t
  188. v9fs_read(struct file *filp, char *buffer, size_t count, loff_t * offset)
  189. {
  190. struct inode *inode = filp->f_dentry->d_inode;
  191. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  192. struct v9fs_fid *v9f = filp->private_data;
  193. struct v9fs_fcall *fcall = NULL;
  194. int fid = v9f->fid;
  195. int rsize = 0;
  196. int result = 0;
  197. int total = 0;
  198. dprintk(DEBUG_VFS, "\n");
  199. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  200. if (v9f->iounit != 0 && rsize > v9f->iounit)
  201. rsize = v9f->iounit;
  202. do {
  203. if (count < rsize)
  204. rsize = count;
  205. result = v9fs_t_read(v9ses, fid, *offset, rsize, &fcall);
  206. if (result < 0) {
  207. printk(KERN_ERR "9P2000: v9fs_t_read returned %d\n",
  208. result);
  209. kfree(fcall);
  210. return total;
  211. } else
  212. *offset += result;
  213. /* XXX - extra copy */
  214. memcpy(buffer, fcall->params.rread.data, result);
  215. count -= result;
  216. buffer += result;
  217. total += result;
  218. kfree(fcall);
  219. if (result < rsize)
  220. break;
  221. } while (count);
  222. return total;
  223. }
  224. /**
  225. * v9fs_file_read - read from a file
  226. * @filep: file pointer to read
  227. * @data: data buffer to read data into
  228. * @count: size of buffer
  229. * @offset: offset at which to read data
  230. *
  231. */
  232. static ssize_t
  233. v9fs_file_read(struct file *filp, char __user * data, size_t count,
  234. loff_t * offset)
  235. {
  236. int retval = -1;
  237. int ret = 0;
  238. char *buffer;
  239. buffer = kmalloc(count, GFP_KERNEL);
  240. if (!buffer)
  241. return -ENOMEM;
  242. retval = v9fs_read(filp, buffer, count, offset);
  243. if (retval > 0) {
  244. if ((ret = copy_to_user(data, buffer, retval)) != 0) {
  245. dprintk(DEBUG_ERROR, "Problem copying to user %d\n",
  246. ret);
  247. retval = ret;
  248. }
  249. }
  250. kfree(buffer);
  251. return retval;
  252. }
  253. /**
  254. * v9fs_write - write to a file
  255. * @filep: file pointer to write
  256. * @data: data buffer to write data from
  257. * @count: size of buffer
  258. * @offset: offset at which to write data
  259. *
  260. */
  261. static ssize_t
  262. v9fs_write(struct file *filp, char *buffer, size_t count, loff_t * offset)
  263. {
  264. struct inode *inode = filp->f_dentry->d_inode;
  265. struct v9fs_session_info *v9ses = v9fs_inode2v9ses(inode);
  266. struct v9fs_fid *v9fid = filp->private_data;
  267. struct v9fs_fcall *fcall;
  268. int fid = v9fid->fid;
  269. int result = -EIO;
  270. int rsize = 0;
  271. int total = 0;
  272. dprintk(DEBUG_VFS, "data %p count %d offset %x\n", buffer, (int)count,
  273. (int)*offset);
  274. rsize = v9ses->maxdata - V9FS_IOHDRSZ;
  275. if (v9fid->iounit != 0 && rsize > v9fid->iounit)
  276. rsize = v9fid->iounit;
  277. dump_data(buffer, count);
  278. do {
  279. if (count < rsize)
  280. rsize = count;
  281. result =
  282. v9fs_t_write(v9ses, fid, *offset, rsize, buffer, &fcall);
  283. if (result < 0) {
  284. eprintk(KERN_ERR, "error while writing: %s(%d)\n",
  285. FCALL_ERROR(fcall), result);
  286. kfree(fcall);
  287. return result;
  288. } else
  289. *offset += result;
  290. kfree(fcall);
  291. if (result != rsize) {
  292. eprintk(KERN_ERR,
  293. "short write: v9fs_t_write returned %d\n",
  294. result);
  295. break;
  296. }
  297. count -= result;
  298. buffer += result;
  299. total += result;
  300. } while (count);
  301. return total;
  302. }
  303. /**
  304. * v9fs_file_write - write to a file
  305. * @filep: file pointer to write
  306. * @data: data buffer to write data from
  307. * @count: size of buffer
  308. * @offset: offset at which to write data
  309. *
  310. */
  311. static ssize_t
  312. v9fs_file_write(struct file *filp, const char __user * data,
  313. size_t count, loff_t * offset)
  314. {
  315. int ret = -1;
  316. char *buffer;
  317. buffer = kmalloc(count, GFP_KERNEL);
  318. if (buffer == NULL)
  319. return -ENOMEM;
  320. ret = copy_from_user(buffer, data, count);
  321. if (ret) {
  322. dprintk(DEBUG_ERROR, "Problem copying from user\n");
  323. ret = -EFAULT;
  324. } else {
  325. ret = v9fs_write(filp, buffer, count, offset);
  326. }
  327. kfree(buffer);
  328. return ret;
  329. }
  330. struct file_operations v9fs_file_operations = {
  331. .llseek = generic_file_llseek,
  332. .read = v9fs_file_read,
  333. .write = v9fs_file_write,
  334. .open = v9fs_file_open,
  335. .release = v9fs_dir_release,
  336. .lock = v9fs_file_lock,
  337. };