vfs_addr.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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 <[email protected]>
  7. * Copyright (C) 2002 by Ron Minnich <[email protected]>
  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 file *data, struct page *page)
  50. {
  51. struct p9_fid *fid = (struct p9_fid *)data;
  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, ITER_BVEC | 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, v9fs_fid_readpage,
  108. filp->private_data);
  109. p9_debug(P9_DEBUG_VFS, " = %d\n", ret);
  110. return ret;
  111. }
  112. /**
  113. * v9fs_release_page - release the private state associated with a page
  114. *
  115. * Returns 1 if the page can be released, false otherwise.
  116. */
  117. static int v9fs_release_page(struct page *page, gfp_t gfp)
  118. {
  119. if (PagePrivate(page))
  120. return 0;
  121. return v9fs_fscache_release_page(page, gfp);
  122. }
  123. /**
  124. * v9fs_invalidate_page - Invalidate a page completely or partially
  125. *
  126. * @page: structure to page
  127. * @offset: offset in the page
  128. */
  129. static void v9fs_invalidate_page(struct page *page, unsigned int offset,
  130. unsigned int length)
  131. {
  132. /*
  133. * If called with zero offset, we should release
  134. * the private state assocated with the page
  135. */
  136. if (offset == 0 && length == PAGE_SIZE)
  137. v9fs_fscache_invalidate_page(page);
  138. }
  139. static int v9fs_vfs_writepage_locked(struct page *page)
  140. {
  141. struct inode *inode = page->mapping->host;
  142. struct v9fs_inode *v9inode = V9FS_I(inode);
  143. loff_t size = i_size_read(inode);
  144. struct iov_iter from;
  145. struct bio_vec bvec;
  146. int err, len;
  147. if (page->index == size >> PAGE_SHIFT)
  148. len = size & ~PAGE_MASK;
  149. else
  150. len = PAGE_SIZE;
  151. bvec.bv_page = page;
  152. bvec.bv_offset = 0;
  153. bvec.bv_len = len;
  154. iov_iter_bvec(&from, ITER_BVEC | WRITE, &bvec, 1, len);
  155. /* We should have writeback_fid always set */
  156. BUG_ON(!v9inode->writeback_fid);
  157. set_page_writeback(page);
  158. p9_client_write(v9inode->writeback_fid, page_offset(page), &from, &err);
  159. end_page_writeback(page);
  160. return err;
  161. }
  162. static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
  163. {
  164. int retval;
  165. p9_debug(P9_DEBUG_VFS, "page %p\n", page);
  166. retval = v9fs_vfs_writepage_locked(page);
  167. if (retval < 0) {
  168. if (retval == -EAGAIN) {
  169. redirty_page_for_writepage(wbc, page);
  170. retval = 0;
  171. } else {
  172. SetPageError(page);
  173. mapping_set_error(page->mapping, retval);
  174. }
  175. } else
  176. retval = 0;
  177. unlock_page(page);
  178. return retval;
  179. }
  180. /**
  181. * v9fs_launder_page - Writeback a dirty page
  182. * Returns 0 on success.
  183. */
  184. static int v9fs_launder_page(struct page *page)
  185. {
  186. int retval;
  187. struct inode *inode = page->mapping->host;
  188. v9fs_fscache_wait_on_page_write(inode, page);
  189. if (clear_page_dirty_for_io(page)) {
  190. retval = v9fs_vfs_writepage_locked(page);
  191. if (retval)
  192. return retval;
  193. }
  194. return 0;
  195. }
  196. /**
  197. * v9fs_direct_IO - 9P address space operation for direct I/O
  198. * @iocb: target I/O control block
  199. *
  200. * The presence of v9fs_direct_IO() in the address space ops vector
  201. * allowes open() O_DIRECT flags which would have failed otherwise.
  202. *
  203. * In the non-cached mode, we shunt off direct read and write requests before
  204. * the VFS gets them, so this method should never be called.
  205. *
  206. * Direct IO is not 'yet' supported in the cached mode. Hence when
  207. * this routine is called through generic_file_aio_read(), the read/write fails
  208. * with an error.
  209. *
  210. */
  211. static ssize_t
  212. v9fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
  213. {
  214. struct file *file = iocb->ki_filp;
  215. loff_t pos = iocb->ki_pos;
  216. ssize_t n;
  217. int err = 0;
  218. if (iov_iter_rw(iter) == WRITE) {
  219. n = p9_client_write(file->private_data, pos, iter, &err);
  220. if (n) {
  221. struct inode *inode = file_inode(file);
  222. loff_t i_size = i_size_read(inode);
  223. if (pos + n > i_size)
  224. inode_add_bytes(inode, pos + n - i_size);
  225. }
  226. } else {
  227. n = p9_client_read(file->private_data, pos, iter, &err);
  228. }
  229. return n ? n : err;
  230. }
  231. static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
  232. loff_t pos, unsigned len, unsigned flags,
  233. struct page **pagep, void **fsdata)
  234. {
  235. int retval = 0;
  236. struct page *page;
  237. struct v9fs_inode *v9inode;
  238. pgoff_t index = pos >> PAGE_SHIFT;
  239. struct inode *inode = mapping->host;
  240. p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
  241. v9inode = V9FS_I(inode);
  242. start:
  243. page = grab_cache_page_write_begin(mapping, index, flags);
  244. if (!page) {
  245. retval = -ENOMEM;
  246. goto out;
  247. }
  248. BUG_ON(!v9inode->writeback_fid);
  249. if (PageUptodate(page))
  250. goto out;
  251. if (len == PAGE_SIZE)
  252. goto out;
  253. retval = v9fs_fid_readpage((struct file *)v9inode->writeback_fid, page);
  254. put_page(page);
  255. if (!retval)
  256. goto start;
  257. out:
  258. *pagep = page;
  259. return retval;
  260. }
  261. static int v9fs_write_end(struct file *filp, struct address_space *mapping,
  262. loff_t pos, unsigned len, unsigned copied,
  263. struct page *page, void *fsdata)
  264. {
  265. loff_t last_pos = pos + copied;
  266. struct inode *inode = page->mapping->host;
  267. p9_debug(P9_DEBUG_VFS, "filp %p, mapping %p\n", filp, mapping);
  268. if (unlikely(copied < len)) {
  269. /*
  270. * zero out the rest of the area
  271. */
  272. unsigned from = pos & (PAGE_SIZE - 1);
  273. zero_user(page, from + copied, len - copied);
  274. flush_dcache_page(page);
  275. }
  276. if (!PageUptodate(page))
  277. SetPageUptodate(page);
  278. /*
  279. * No need to use i_size_read() here, the i_size
  280. * cannot change under us because we hold the i_mutex.
  281. */
  282. if (last_pos > inode->i_size) {
  283. inode_add_bytes(inode, last_pos - inode->i_size);
  284. i_size_write(inode, last_pos);
  285. }
  286. set_page_dirty(page);
  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. };