vfs_file.c 6.2 KB

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