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