vfs_addr.c 8.2 KB

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