vfs_file.c 6.5 KB

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