vfs_file.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  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. /**
  45. * v9fs_file_open - open a file (or directory)
  46. * @inode: inode to be opened
  47. * @file: file being opened
  48. *
  49. */
  50. int v9fs_file_open(struct inode *inode, struct file *file)
  51. {
  52. int err;
  53. struct v9fs_session_info *v9ses;
  54. struct p9_fid *fid;
  55. int omode;
  56. P9_DPRINTK(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, file);
  57. v9ses = v9fs_inode2v9ses(inode);
  58. if (v9fs_proto_dotl(v9ses))
  59. omode = file->f_flags;
  60. else
  61. omode = v9fs_uflags2omode(file->f_flags,
  62. v9fs_proto_dotu(v9ses));
  63. fid = file->private_data;
  64. if (!fid) {
  65. fid = v9fs_fid_clone(file->f_path.dentry);
  66. if (IS_ERR(fid))
  67. return PTR_ERR(fid);
  68. err = p9_client_open(fid, omode);
  69. if (err < 0) {
  70. p9_client_clunk(fid);
  71. return err;
  72. }
  73. if (file->f_flags & O_TRUNC) {
  74. i_size_write(inode, 0);
  75. inode->i_blocks = 0;
  76. }
  77. if ((file->f_flags & O_APPEND) &&
  78. (!v9fs_proto_dotu(v9ses) && !v9fs_proto_dotl(v9ses)))
  79. generic_file_llseek(file, 0, SEEK_END);
  80. }
  81. file->private_data = fid;
  82. if ((fid->qid.version) && (v9ses->cache)) {
  83. P9_DPRINTK(P9_DEBUG_VFS, "cached");
  84. /* enable cached file options */
  85. if(file->f_op == &v9fs_file_operations)
  86. file->f_op = &v9fs_cached_file_operations;
  87. #ifdef CONFIG_9P_FSCACHE
  88. v9fs_cache_inode_set_cookie(inode, file);
  89. #endif
  90. }
  91. return 0;
  92. }
  93. /**
  94. * v9fs_file_lock - lock a file (or directory)
  95. * @filp: file to be locked
  96. * @cmd: lock command
  97. * @fl: file lock structure
  98. *
  99. * Bugs: this looks like a local only lock, we should extend into 9P
  100. * by using open exclusive
  101. */
  102. static int v9fs_file_lock(struct file *filp, int cmd, struct file_lock *fl)
  103. {
  104. int res = 0;
  105. struct inode *inode = filp->f_path.dentry->d_inode;
  106. P9_DPRINTK(P9_DEBUG_VFS, "filp: %p lock: %p\n", filp, fl);
  107. /* No mandatory locks */
  108. if (__mandatory_lock(inode) && fl->fl_type != F_UNLCK)
  109. return -ENOLCK;
  110. if ((IS_SETLK(cmd) || IS_SETLKW(cmd)) && fl->fl_type != F_UNLCK) {
  111. filemap_write_and_wait(inode->i_mapping);
  112. invalidate_mapping_pages(&inode->i_data, 0, -1);
  113. }
  114. return res;
  115. }
  116. /**
  117. * v9fs_file_readn - read from a file
  118. * @filp: file pointer to read
  119. * @data: data buffer to read data into
  120. * @udata: user data buffer to read data into
  121. * @count: size of buffer
  122. * @offset: offset at which to read data
  123. *
  124. */
  125. ssize_t
  126. v9fs_file_readn(struct file *filp, char *data, char __user *udata, u32 count,
  127. u64 offset)
  128. {
  129. int n, total, size;
  130. struct p9_fid *fid = filp->private_data;
  131. P9_DPRINTK(P9_DEBUG_VFS, "fid %d offset %llu count %d\n", fid->fid,
  132. (long long unsigned) offset, count);
  133. n = 0;
  134. total = 0;
  135. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  136. do {
  137. n = p9_client_read(fid, data, udata, offset, count);
  138. if (n <= 0)
  139. break;
  140. if (data)
  141. data += n;
  142. if (udata)
  143. udata += n;
  144. offset += n;
  145. count -= n;
  146. total += n;
  147. } while (count > 0 && n == size);
  148. if (n < 0)
  149. total = n;
  150. return total;
  151. }
  152. /**
  153. * v9fs_file_read - read from a file
  154. * @filp: file pointer to read
  155. * @udata: user data buffer to read data into
  156. * @count: size of buffer
  157. * @offset: offset at which to read data
  158. *
  159. */
  160. static ssize_t
  161. v9fs_file_read(struct file *filp, char __user *udata, size_t count,
  162. loff_t * offset)
  163. {
  164. int ret;
  165. struct p9_fid *fid;
  166. size_t size;
  167. P9_DPRINTK(P9_DEBUG_VFS, "count %zu offset %lld\n", count, *offset);
  168. fid = filp->private_data;
  169. size = fid->iounit ? fid->iounit : fid->clnt->msize - P9_IOHDRSZ;
  170. if (count > size)
  171. ret = v9fs_file_readn(filp, NULL, udata, count, *offset);
  172. else
  173. ret = p9_client_read(fid, NULL, udata, *offset, count);
  174. if (ret > 0)
  175. *offset += ret;
  176. return ret;
  177. }
  178. /**
  179. * v9fs_file_write - write to a file
  180. * @filp: file pointer to write
  181. * @data: data buffer to write data from
  182. * @count: size of buffer
  183. * @offset: offset at which to write data
  184. *
  185. */
  186. static ssize_t
  187. v9fs_file_write(struct file *filp, const char __user * data,
  188. size_t count, loff_t * offset)
  189. {
  190. int n, rsize, total = 0;
  191. struct p9_fid *fid;
  192. struct p9_client *clnt;
  193. struct inode *inode = filp->f_path.dentry->d_inode;
  194. loff_t origin = *offset;
  195. unsigned long pg_start, pg_end;
  196. P9_DPRINTK(P9_DEBUG_VFS, "data %p count %d offset %x\n", data,
  197. (int)count, (int)*offset);
  198. fid = filp->private_data;
  199. clnt = fid->clnt;
  200. rsize = fid->iounit ? fid->iounit : clnt->msize - P9_IOHDRSZ;
  201. do {
  202. if (count < rsize)
  203. rsize = count;
  204. n = p9_client_write(fid, NULL, data+total, origin+total,
  205. rsize);
  206. if (n <= 0)
  207. break;
  208. count -= n;
  209. total += n;
  210. } while (count > 0);
  211. if (total > 0) {
  212. pg_start = origin >> PAGE_CACHE_SHIFT;
  213. pg_end = (origin + total - 1) >> PAGE_CACHE_SHIFT;
  214. if (inode->i_mapping && inode->i_mapping->nrpages)
  215. invalidate_inode_pages2_range(inode->i_mapping,
  216. pg_start, pg_end);
  217. *offset += total;
  218. i_size_write(inode, i_size_read(inode) + total);
  219. inode->i_blocks = (i_size_read(inode) + 512 - 1) >> 9;
  220. }
  221. if (n < 0)
  222. return n;
  223. return total;
  224. }
  225. static int v9fs_file_fsync(struct file *filp, int datasync)
  226. {
  227. struct p9_fid *fid;
  228. struct p9_wstat wstat;
  229. int retval;
  230. P9_DPRINTK(P9_DEBUG_VFS, "filp %p datasync %x\n", filp, datasync);
  231. fid = filp->private_data;
  232. v9fs_blank_wstat(&wstat);
  233. retval = p9_client_wstat(fid, &wstat);
  234. return retval;
  235. }
  236. static const struct file_operations v9fs_cached_file_operations = {
  237. .llseek = generic_file_llseek,
  238. .read = do_sync_read,
  239. .aio_read = generic_file_aio_read,
  240. .write = v9fs_file_write,
  241. .open = v9fs_file_open,
  242. .release = v9fs_dir_release,
  243. .lock = v9fs_file_lock,
  244. .mmap = generic_file_readonly_mmap,
  245. .fsync = v9fs_file_fsync,
  246. };
  247. const struct file_operations v9fs_file_operations = {
  248. .llseek = generic_file_llseek,
  249. .read = v9fs_file_read,
  250. .write = v9fs_file_write,
  251. .open = v9fs_file_open,
  252. .release = v9fs_dir_release,
  253. .lock = v9fs_file_lock,
  254. .mmap = generic_file_readonly_mmap,
  255. .fsync = v9fs_file_fsync,
  256. };
  257. const struct file_operations v9fs_file_operations_dotl = {
  258. .llseek = generic_file_llseek,
  259. .read = v9fs_file_read,
  260. .write = v9fs_file_write,
  261. .open = v9fs_file_open,
  262. .release = v9fs_dir_release,
  263. .lock = v9fs_file_lock,
  264. .mmap = generic_file_readonly_mmap,
  265. .fsync = v9fs_file_fsync,
  266. };