vfs_addr.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * linux/fs/9p/vfs_addr.c
  3. *
  4. * This file contians vfs address (mmap) ops for 9P2000.
  5. *
  6. * Copyright (C) 2005 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/file.h>
  29. #include <linux/stat.h>
  30. #include <linux/string.h>
  31. #include <linux/inet.h>
  32. #include <linux/pagemap.h>
  33. #include <linux/idr.h>
  34. #include <linux/sched.h>
  35. #include <linux/uio.h>
  36. #include <linux/bvec.h>
  37. #include <net/9p/9p.h>
  38. #include <net/9p/client.h>
  39. #include "v9fs.h"
  40. #include "v9fs_vfs.h"
  41. #include "cache.h"
  42. #include "fid.h"
  43. /**
  44. * v9fs_fid_readpage - read an entire page in from 9P
  45. *
  46. * @fid: fid being read
  47. * @page: structure to page
  48. *
  49. */
  50. static int v9fs_fid_readpage(void *data, struct page *page)
  51. {
  52. struct p9_fid *fid = data;
  53. struct inode *inode = page->mapping->host;
  54. struct bio_vec bvec = {.bv_page = page, .bv_len = PAGE_SIZE};
  55. struct iov_iter to;
  56. int retval, err;
  57. p9_debug(P9_DEBUG_VFS, "\n");
  58. BUG_ON(!PageLocked(page));
  59. retval = v9fs_readpage_from_fscache(inode, page);
  60. if (retval == 0)
  61. return retval;
  62. iov_iter_bvec(&to, ITER_BVEC | READ, &bvec, 1, PAGE_SIZE);
  63. retval = p9_client_read(fid, page_offset(page), &to, &err);
  64. if (err) {
  65. v9fs_uncache_page(inode, page);
  66. retval = err;
  67. goto done;
  68. }
  69. zero_user(page, retval, PAGE_SIZE - retval);
  70. flush_dcache_page(page);
  71. SetPageUptodate(page);
  72. v9fs_readpage_to_fscache(inode, page);
  73. retval = 0;
  74. done:
  75. unlock_page(page);
  76. return retval;
  77. }
  78. /**
  79. * v9fs_vfs_readpage - read an entire page in from 9P
  80. *
  81. * @filp: file being read
  82. * @page: structure to page
  83. *
  84. */
  85. static int v9fs_vfs_readpage(struct file *filp, struct page *page)
  86. {
  87. return v9fs_fid_readpage(filp->private_data, page);
  88. }
  89. /**
  90. * v9fs_vfs_readpages - read a set of pages from 9P
  91. *
  92. * @filp: file being read
  93. * @mapping: the address space
  94. * @pages: list of pages to read
  95. * @nr_pages: count of pages to read
  96. *
  97. */
  98. static int v9fs_vfs_readpages(struct file *filp, struct address_space *mapping,
  99. struct list_head *pages, unsigned nr_pages)
  100. {
  101. int ret = 0;
  102. struct inode *inode;
  103. inode = mapping->host;
  104. p9_debug(P9_DEBUG_VFS, "inode: %p file: %p\n", inode, filp);
  105. ret = v9fs_readpages_from_fscache(inode, mapping, pages, &nr_pages);
  106. if (ret == 0)
  107. return ret;
  108. ret = read_cache_pages(mapping, pages, v9fs_fid_readpage,
  109. filp->private_data);
  110. p9_debug(P9_DEBUG_VFS, " = %d\n", ret);
  111. return ret;
  112. }
  113. /**
  114. * v9fs_release_page - release the private state associated with a page
  115. *
  116. * Returns 1 if the page can be released, false otherwise.
  117. */
  118. static int v9fs_release_page(struct page *page, gfp_t gfp)
  119. {
  120. if (PagePrivate(page))
  121. return 0;
  122. return v9fs_fscache_release_page(page, gfp);
  123. }
  124. /**
  125. * v9fs_invalidate_page - Invalidate a page completely or partially
  126. *
  127. * @page: structure to page
  128. * @offset: offset in the page
  129. */
  130. static void v9fs_invalidate_page(struct page *page, unsigned int offset,
  131. unsigned int length)
  132. {
  133. /*
  134. * If called with zero offset, we should release
  135. * the private state assocated with the page
  136. */
  137. if (offset == 0 && length == PAGE_SIZE)
  138. v9fs_fscache_invalidate_page(page);
  139. }
  140. static int v9fs_vfs_writepage_locked(struct page *page)
  141. {
  142. struct inode *inode = page->mapping->host;
  143. struct v9fs_inode *v9inode = V9FS_I(inode);
  144. loff_t size = i_size_read(inode);
  145. struct iov_iter from;
  146. struct bio_vec bvec;
  147. int err, len;
  148. if (page->index == size >> PAGE_SHIFT)
  149. len = size & ~PAGE_MASK;
  150. else
  151. len = PAGE_SIZE;
  152. bvec.bv_page = page;
  153. bvec.bv_offset = 0;
  154. bvec.bv_len = len;
  155. iov_iter_bvec(&from, ITER_BVEC | WRITE, &bvec, 1, len);
  156. /* We should have writeback_fid always set */
  157. BUG_ON(!v9inode->writeback_fid);
  158. set_page_writeback(page);
  159. p9_client_write(v9inode->writeback_fid, page_offset(page), &from, &err);
  160. end_page_writeback(page);
  161. return err;
  162. }
  163. static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
  164. {
  165. int retval;
  166. p9_debug(P9_DEBUG_VFS, "page %p\n", page);
  167. retval = v9fs_vfs_writepage_locked(page);
  168. if (retval < 0) {
  169. if (retval == -EAGAIN) {
  170. redirty_page_for_writepage(wbc, page);
  171. retval = 0;
  172. } else {
  173. SetPageError(page);
  174. mapping_set_error(page->mapping, retval);
  175. }
  176. } else
  177. retval = 0;
  178. unlock_page(page);
  179. return retval;
  180. }
  181. /**
  182. * v9fs_launder_page - Writeback a dirty page
  183. * Returns 0 on success.
  184. */
  185. static int v9fs_launder_page(struct page *page)
  186. {
  187. int retval;
  188. struct inode *inode = page->mapping->host;
  189. v9fs_fscache_wait_on_page_write(inode, page);
  190. if (clear_page_dirty_for_io(page)) {
  191. retval = v9fs_vfs_writepage_locked(page);
  192. if (retval)
  193. return retval;
  194. }
  195. return 0;
  196. }
  197. /**
  198. * v9fs_direct_IO - 9P address space operation for direct I/O
  199. * @iocb: target I/O control block
  200. *
  201. * The presence of v9fs_direct_IO() in the address space ops vector
  202. * allowes open() O_DIRECT flags which would have failed otherwise.
  203. *
  204. * In the non-cached mode, we shunt off direct read and write requests before
  205. * the VFS gets them, so this method should never be called.
  206. *
  207. * Direct IO is not 'yet' supported in the cached mode. Hence when
  208. * this routine is called through generic_file_aio_read(), the read/write fails
  209. * with an error.
  210. *
  211. */
  212. static ssize_t
  213. v9fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  214. {
  215. struct file *file = iocb->ki_filp;
  216. loff_t pos = iocb->ki_pos;
  217. ssize_t n;
  218. int err = 0;
  219. if (iov_iter_rw(iter) == WRITE) {
  220. n = p9_client_write(file->private_data, pos, iter, &err);
  221. if (n) {
  222. struct inode *inode = file_inode(file);
  223. loff_t i_size = i_size_read(inode);
  224. if (pos + n > i_size)
  225. inode_add_bytes(inode, pos + n - i_size);
  226. }
  227. } else {
  228. n = p9_client_read(file->private_data, pos, iter, &err);
  229. }
  230. return n ? n : err;
  231. }
  232. static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
  233. loff_t pos, unsigned len, unsigned flags,
  234. struct page **pagep, void **fsdata)
  235. {
  236. int retval = 0;
  237. struct page *page;
  238. struct v9fs_inode *v9inode;
  239. pgoff_t index = pos >> PAGE_SHIFT;
  240. struct inode *inode = mapping->host;
  241. p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
  242. v9inode = V9FS_I(inode);
  243. start:
  244. page = grab_cache_page_write_begin(mapping, index, flags);
  245. if (!page) {
  246. retval = -ENOMEM;
  247. goto out;
  248. }
  249. BUG_ON(!v9inode->writeback_fid);
  250. if (PageUptodate(page))
  251. goto out;
  252. if (len == PAGE_SIZE)
  253. goto out;
  254. retval = v9fs_fid_readpage(v9inode->writeback_fid, page);
  255. put_page(page);
  256. if (!retval)
  257. goto start;
  258. out:
  259. *pagep = page;
  260. return retval;
  261. }
  262. static int v9fs_write_end(struct file *filp, struct address_space *mapping,
  263. loff_t pos, unsigned len, unsigned copied,
  264. struct page *page, void *fsdata)
  265. {
  266. loff_t last_pos = pos + copied;
  267. struct inode *inode = page->mapping->host;
  268. p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
  269. if (!PageUptodate(page)) {
  270. if (unlikely(copied < len)) {
  271. copied = 0;
  272. goto out;
  273. } else if (len == PAGE_SIZE) {
  274. SetPageUptodate(page);
  275. }
  276. }
  277. /*
  278. * No need to use i_size_read() here, the i_size
  279. * cannot change under us because we hold the i_mutex.
  280. */
  281. if (last_pos > inode->i_size) {
  282. inode_add_bytes(inode, last_pos - inode->i_size);
  283. i_size_write(inode, last_pos);
  284. }
  285. set_page_dirty(page);
  286. out:
  287. unlock_page(page);
  288. put_page(page);
  289. return copied;
  290. }
  291. const struct address_space_operations v9fs_addr_operations = {
  292. .readpage = v9fs_vfs_readpage,
  293. .readpages = v9fs_vfs_readpages,
  294. .set_page_dirty = __set_page_dirty_nobuffers,
  295. .writepage = v9fs_vfs_writepage,
  296. .write_begin = v9fs_write_begin,
  297. .write_end = v9fs_write_end,
  298. .releasepage = v9fs_release_page,
  299. .invalidatepage = v9fs_invalidate_page,
  300. .launder_page = v9fs_launder_page,
  301. .direct_IO = v9fs_direct_IO,
  302. };