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