vfs_file.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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 version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to:
  20. * Free Software Foundation
  21. * 51 Franklin Street, Fifth Floor
  22. * Boston, MA 02111-1301 USA
  23. *
  24. */
  25. #include <linux/module.h>
  26. #include <linux/errno.h>
  27. #include <linux/fs.h>
  28. #include <linux/sched.h>
  29. #include <linux/file.h>
  30. #include <linux/stat.h>
  31. #include <linux/string.h>
  32. #include <linux/inet.h>
  33. #include <linux/list.h>
  34. #include <linux/pagemap.h>
  35. #include <asm/uaccess.h>
  36. #include <linux/idr.h>
  37. #include <net/9p/9p.h>
  38. #include <net/9p/client.h>
  39. #include "v9fs.h"
  40. #include "v9fs_vfs.h"
  41. #include "fid.h"
  42. #include "cache.h"
  43. static const struct file_operations v9fs_cached_file_operations;
  44. static const struct file_operations v9fs_cached_file_operations_dotl;
  45. /**
  46. * v9fs_file_open - open a file (or directory)
  47. * @inode: inode to be opened
  48. * @file: file being opened
  49. *
  50. */
  51. int v9fs_file_open(struct inode *inode, struct file *file)
  52. {
  53. int err;
  54. struct v9fs_session_info *v9ses;
  55. struct p9_fid *fid;
  56. int omode;
  57. P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
  58. v9ses = v9fs_inode2v9ses(inode);
  59. if (v9fs_proto_dotl(v9ses))
  60. omode = file->f_flags;
  61. else
  62. omode = v9fs_uflags2omode(file->f_flags,
  63. v9fs_proto_dotu(v9ses));
  64. fid = file->private_data;
  65. if (!fid) {
  66. fid = v9fs_fid_clone(file->f_path.dentry);
  67. if (IS_ERR(fid))
  68. return PTR_ERR(fid);
  69. err = p9_client_open(fid, omode);
  70. if (err < 0) {
  71. p9_client_clunk(fid);
  72. return err;
  73. }
  74. if (file->f_flags & O_TRUNC) {
  75. i_size_write(inode, 0);
  76. inode->i_blocks = 0;
  77. }
  78. if ((file->f_flags & O_APPEND) &&
  79. (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
  80. generic_file_llseek(file, 0, SEEK_END);
  81. }
  82. file->private_data = fid;
  83. if ((fid->qid.version) && (v9ses->cache)) {
  84. P9_DPRINTK(P9_DEBUG_VFS, "cached");
  85. /* enable cached file options */
  86. if(file->f_op == &v9fs_file_operations)
  87. file->f_op = &v9fs_cached_file_operations;
  88. else if (file->f_op == &v9fs_file_operations_dotl)
  89. file->f_op = &v9fs_cached_file_operations_dotl;
  90. #ifdef CONFIG_9P_FSCACHE
  91. v9fs_cache_inode_set_cookie(inode, file);
  92. #endif
  93. }
  94. return 0;
  95. }
  96. /**
  97. * v9fs_file_lock - lock a file (or directory)
  98. * @filp: file to be locked
  99. * @cmd: lock command
  100. * @fl: file lock structure
  101. *
  102. * Bugs: this looks like a local only lock, we should extend into 9P
  103. * by using open exclusive
  104. */
  105. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  106. {
  107. int res = 0;
  108. struct inode *inode = filp->f_path.dentry->d_inode;
  109. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  110. /* No mandatory locks */
  111. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  112. return -ENOLCK;
  113. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  114. filemap_write_and_wait(inode->i_mapping);
  115. invalidate_mapping_pages(&inode->i_data, 0, -1);
  116. }
  117. return res;
  118. }
  119. /**
  120. * v9fs_file_readn - read from a file
  121. * @filp: file pointer to read
  122. * @data: data buffer to read data into
  123. * @udata: user data buffer to read data into
  124. * @count: size of buffer
  125. * @offset: offset at which to read data
  126. *
  127. */
  128. ssize_t
  129. v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
  130. u64 offset)
  131. {
  132. int n, total, size;
  133. struct p9_fid *fid = filp->private_data;
  134. P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
  135. (long long unsigned) offset, count);
  136. n = 0;
  137. total = 0;
  138. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  139. do {
  140. n = p9_client_read(fid, data, udata, offset, count);
  141. if (n <= 0)
  142. break;
  143. if (data)
  144. data += n;
  145. if (udata)
  146. udata += n;
  147. offset += n;
  148. count -= n;
  149. total += n;
  150. } while (count > 0 && n == size);
  151. if (n < 0)
  152. total = n;
  153. return total;
  154. }
  155. /**
  156. * v9fs_file_read - read from a file
  157. * @filp: file pointer to read
  158. * @udata: user data buffer to read data into
  159. * @count: size of buffer
  160. * @offset: offset at which to read data
  161. *
  162. */
  163. static ssize_t
  164. v9fs_file_read(struct file *filp, char __user *udata, size_t count,
  165. loff_t * offset)
  166. {
  167. int ret;
  168. struct p9_fid *fid;
  169. size_t size;
  170. P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
  171. fid = filp->private_data;
  172. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  173. if (count > size)
  174. ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
  175. else
  176. ret = p9_client_read(fid, NULL, udata, *offset, count);
  177. if (ret > 0)
  178. *offset += ret;
  179. return ret;
  180. }
  181. /**
  182. * v9fs_file_write - write to a file
  183. * @filp: file pointer to write
  184. * @data: data buffer to write data from
  185. * @count: size of buffer
  186. * @offset: offset at which to write data
  187. *
  188. */
  189. static ssize_t
  190. v9fs_file_write(struct file *filp, const char __user * data,
  191. size_t count, loff_t * offset)
  192. {
  193. ssize_t retval;
  194. size_t total = 0;
  195. int n;
  196. struct p9_fid *fid;
  197. struct p9_client *clnt;
  198. struct inode *inode = filp->f_path.dentry->d_inode;
  199. loff_t origin = *offset;
  200. unsigned long pg_start, pg_end;
  201. P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
  202. (int)count, (int)*offset);
  203. fid = filp->private_data;
  204. clnt = fid->clnt;
  205. retval = generic_write_checks(filp, &origin, &count, 0);
  206. if (retval)
  207. goto out;
  208. retval = -EINVAL;
  209. if ((ssize_t) count < 0)
  210. goto out;
  211. retval = 0;
  212. if (!count)
  213. goto out;
  214. do {
  215. n = p9_client_write(fid, NULL, data+total, origin+total, count);
  216. if (n <= 0)
  217. break;
  218. count -= n;
  219. total += n;
  220. } while (count > 0);
  221. if (total > 0) {
  222. pg_start = origin >> PAGE_CACHE_SHIFT;
  223. pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
  224. if (inode->i_mapping && inode->i_mapping->nrpages)
  225. invalidate_inode_pages2_range(inode->i_mapping,
  226. pg_start, pg_end);
  227. *offset += total;
  228. i_size_write(inode, i_size_read(inode) + total);
  229. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  230. }
  231. if (n < 0)
  232. retval = n;
  233. else
  234. retval = total;
  235. out:
  236. return retval;
  237. }
  238. static int v9fs_file_fsync(struct file *filp, int datasync)
  239. {
  240. struct p9_fid *fid;
  241. struct p9_wstat wstat;
  242. int retval;
  243. P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
  244. fid = filp->private_data;
  245. v9fs_blank_wstat(&wstat);
  246. retval = p9_client_wstat(fid, &wstat);
  247. return retval;
  248. }
  249. static int v9fs_file_fsync_dotl(struct file *filp, int datasync)
  250. {
  251. struct p9_fid *fid;
  252. int retval;
  253. P9_DPRINTK(P9_DEBUG_VFS, "v9fs_file_fsync_dotl: filp %p datasync %x\n",
  254. filp, datasync);
  255. fid = filp->private_data;
  256. retval = p9_client_fsync(fid);
  257. return retval;
  258. }
  259. static const struct file_operations v9fs_cached_file_operations = {
  260. .llseek = generic_file_llseek,
  261. .read = do_sync_read,
  262. .aio_read = generic_file_aio_read,
  263. .write = v9fs_file_write,
  264. .open = v9fs_file_open,
  265. .release = v9fs_dir_release,
  266. .lock = v9fs_file_lock,
  267. .mmap = generic_file_readonly_mmap,
  268. .fsync = v9fs_file_fsync,
  269. };
  270. static const struct file_operations v9fs_cached_file_operations_dotl = {
  271. .llseek = generic_file_llseek,
  272. .read = do_sync_read,
  273. .aio_read = generic_file_aio_read,
  274. .write = v9fs_file_write,
  275. .open = v9fs_file_open,
  276. .release = v9fs_dir_release,
  277. .lock = v9fs_file_lock,
  278. .mmap = generic_file_readonly_mmap,
  279. .fsync = v9fs_file_fsync_dotl,
  280. };
  281. const struct file_operations v9fs_file_operations = {
  282. .llseek = generic_file_llseek,
  283. .read = v9fs_file_read,
  284. .write = v9fs_file_write,
  285. .open = v9fs_file_open,
  286. .release = v9fs_dir_release,
  287. .lock = v9fs_file_lock,
  288. .mmap = generic_file_readonly_mmap,
  289. .fsync = v9fs_file_fsync,
  290. };
  291. const struct file_operations v9fs_file_operations_dotl = {
  292. .llseek = generic_file_llseek,
  293. .read = v9fs_file_read,
  294. .write = v9fs_file_write,
  295. .open = v9fs_file_open,
  296. .release = v9fs_dir_release,
  297. .lock = v9fs_file_lock,
  298. .mmap = generic_file_readonly_mmap,
  299. .fsync = v9fs_file_fsync_dotl,
  300. };