vfs_addr.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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/aio.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. int retval;
  52. loff_t offset;
  53. char *buffer;
  54. struct inode *inode;
  55. inode = page->mapping->host;
  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. buffer = kmap(page);
  62. offset = page_offset(page);
  63. retval = v9fs_fid_readn(fid, buffer, NULL, PAGE_CACHE_SIZE, offset);
  64. if (retval < 0) {
  65. v9fs_uncache_page(inode, page);
  66. goto done;
  67. }
  68. memset(buffer + retval, 0, PAGE_CACHE_SIZE - retval);
  69. flush_dcache_page(page);
  70. SetPageUptodate(page);
  71. v9fs_readpage_to_fscache(inode, page);
  72. retval = 0;
  73. done:
  74. kunmap(page);
  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, (void *)v9fs_vfs_readpage, filp);
  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_CACHE_SIZE)
  137. v9fs_fscache_invalidate_page(page);
  138. }
  139. static int v9fs_vfs_writepage_locked(struct page *page)
  140. {
  141. char *buffer;
  142. int retval, len;
  143. loff_t offset, size;
  144. mm_segment_t old_fs;
  145. struct v9fs_inode *v9inode;
  146. struct inode *inode = page->mapping->host;
  147. v9inode = V9FS_I(inode);
  148. size = i_size_read(inode);
  149. if (page->index == size >> PAGE_CACHE_SHIFT)
  150. len = size & ~PAGE_CACHE_MASK;
  151. else
  152. len = PAGE_CACHE_SIZE;
  153. set_page_writeback(page);
  154. buffer = kmap(page);
  155. offset = page_offset(page);
  156. old_fs = get_fs();
  157. set_fs(get_ds());
  158. /* We should have writeback_fid always set */
  159. BUG_ON(!v9inode->writeback_fid);
  160. retval = v9fs_file_write_internal(inode,
  161. v9inode->writeback_fid,
  162. (__force const char __user *)buffer,
  163. len, &offset, 0);
  164. if (retval > 0)
  165. retval = 0;
  166. set_fs(old_fs);
  167. kunmap(page);
  168. end_page_writeback(page);
  169. return retval;
  170. }
  171. static int v9fs_vfs_writepage(struct page *page, struct writeback_control *wbc)
  172. {
  173. int retval;
  174. retval = v9fs_vfs_writepage_locked(page);
  175. if (retval < 0) {
  176. if (retval == -EAGAIN) {
  177. redirty_page_for_writepage(wbc, page);
  178. retval = 0;
  179. } else {
  180. SetPageError(page);
  181. mapping_set_error(page->mapping, retval);
  182. }
  183. } else
  184. retval = 0;
  185. unlock_page(page);
  186. return retval;
  187. }
  188. /**
  189. * v9fs_launder_page - Writeback a dirty page
  190. * Returns 0 on success.
  191. */
  192. static int v9fs_launder_page(struct page *page)
  193. {
  194. int retval;
  195. struct inode *inode = page->mapping->host;
  196. v9fs_fscache_wait_on_page_write(inode, page);
  197. if (clear_page_dirty_for_io(page)) {
  198. retval = v9fs_vfs_writepage_locked(page);
  199. if (retval)
  200. return retval;
  201. }
  202. return 0;
  203. }
  204. /**
  205. * v9fs_direct_IO - 9P address space operation for direct I/O
  206. * @rw: direction (read or write)
  207. * @iocb: target I/O control block
  208. * @iov: array of vectors that define I/O buffer
  209. * @pos: offset in file to begin the operation
  210. * @nr_segs: size of iovec array
  211. *
  212. * The presence of v9fs_direct_IO() in the address space ops vector
  213. * allowes open() O_DIRECT flags which would have failed otherwise.
  214. *
  215. * In the non-cached mode, we shunt off direct read and write requests before
  216. * the VFS gets them, so this method should never be called.
  217. *
  218. * Direct IO is not 'yet' supported in the cached mode. Hence when
  219. * this routine is called through generic_file_aio_read(), the read/write fails
  220. * with an error.
  221. *
  222. */
  223. static ssize_t
  224. v9fs_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
  225. loff_t pos, unsigned long nr_segs)
  226. {
  227. /*
  228. * FIXME
  229. * Now that we do caching with cache mode enabled, We need
  230. * to support direct IO
  231. */
  232. p9_debug(P9_DEBUG_VFS, "v9fs_direct_IO: v9fs_direct_IO (%s) off/no(%lld/%lu) EINVAL\n",
  233. iocb->ki_filp->f_path.dentry->d_name.name,
  234. (long long)pos, nr_segs);
  235. return -EINVAL;
  236. }
  237. static int v9fs_write_begin(struct file *filp, struct address_space *mapping,
  238. loff_t pos, unsigned len, unsigned flags,
  239. struct page **pagep, void **fsdata)
  240. {
  241. int retval = 0;
  242. struct page *page;
  243. struct v9fs_inode *v9inode;
  244. pgoff_t index = pos >> PAGE_CACHE_SHIFT;
  245. struct inode *inode = mapping->host;
  246. v9inode = V9FS_I(inode);
  247. start:
  248. page = grab_cache_page_write_begin(mapping, index, flags);
  249. if (!page) {
  250. retval = -ENOMEM;
  251. goto out;
  252. }
  253. BUG_ON(!v9inode->writeback_fid);
  254. if (PageUptodate(page))
  255. goto out;
  256. if (len == PAGE_CACHE_SIZE)
  257. goto out;
  258. retval = v9fs_fid_readpage(v9inode->writeback_fid, page);
  259. page_cache_release(page);
  260. if (!retval)
  261. goto start;
  262. out:
  263. *pagep = page;
  264. return retval;
  265. }
  266. static int v9fs_write_end(struct file *filp, struct address_space *mapping,
  267. loff_t pos, unsigned len, unsigned copied,
  268. struct page *page, void *fsdata)
  269. {
  270. loff_t last_pos = pos + copied;
  271. struct inode *inode = page->mapping->host;
  272. if (unlikely(copied < len)) {
  273. /*
  274. * zero out the rest of the area
  275. */
  276. unsigned from = pos & (PAGE_CACHE_SIZE - 1);
  277. zero_user(page, from + copied, len - copied);
  278. flush_dcache_page(page);
  279. }
  280. if (!PageUptodate(page))
  281. SetPageUptodate(page);
  282. /*
  283. * No need to use i_size_read() here, the i_size
  284. * cannot change under us because we hold the i_mutex.
  285. */
  286. if (last_pos > inode->i_size) {
  287. inode_add_bytes(inode, last_pos - inode->i_size);
  288. i_size_write(inode, last_pos);
  289. }
  290. set_page_dirty(page);
  291. unlock_page(page);
  292. page_cache_release(page);
  293. return copied;
  294. }
  295. const struct address_space_operations v9fs_addr_operations = {
  296. .readpage = v9fs_vfs_readpage,
  297. .readpages = v9fs_vfs_readpages,
  298. .set_page_dirty = __set_page_dirty_nobuffers,
  299. .writepage = v9fs_vfs_writepage,
  300. .write_begin = v9fs_write_begin,
  301. .write_end = v9fs_write_end,
  302. .releasepage = v9fs_release_page,
  303. .invalidatepage = v9fs_invalidate_page,
  304. .launder_page = v9fs_launder_page,
  305. .direct_IO = v9fs_direct_IO,
  306. };