read.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. /*
  2. * linux/fs/nfs/read.c
  3. *
  4. * Block I/O for NFS
  5. *
  6. * Partial copy of Linus' read cache modifications to fs/nfs/file.c
  7. * modified for async RPC by okir@monad.swb.de
  8. */
  9. #include <linux/time.h>
  10. #include <linux/kernel.h>
  11. #include <linux/errno.h>
  12. #include <linux/fcntl.h>
  13. #include <linux/stat.h>
  14. #include <linux/mm.h>
  15. #include <linux/slab.h>
  16. #include <linux/pagemap.h>
  17. #include <linux/sunrpc/clnt.h>
  18. #include <linux/nfs_fs.h>
  19. #include <linux/nfs_page.h>
  20. #include <linux/module.h>
  21. #include "nfs4_fs.h"
  22. #include "internal.h"
  23. #include "iostat.h"
  24. #include "fscache.h"
  25. #include "pnfs.h"
  26. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  27. static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops;
  28. static const struct nfs_rw_ops nfs_rw_read_ops;
  29. static struct kmem_cache *nfs_rdata_cachep;
  30. static struct nfs_pgio_header *nfs_readhdr_alloc(void)
  31. {
  32. return kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
  33. }
  34. static void nfs_readhdr_free(struct nfs_pgio_header *rhdr)
  35. {
  36. kmem_cache_free(nfs_rdata_cachep, rhdr);
  37. }
  38. static
  39. int nfs_return_empty_page(struct page *page)
  40. {
  41. zero_user(page, 0, PAGE_CACHE_SIZE);
  42. SetPageUptodate(page);
  43. unlock_page(page);
  44. return 0;
  45. }
  46. void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
  47. struct inode *inode, bool force_mds,
  48. const struct nfs_pgio_completion_ops *compl_ops)
  49. {
  50. struct nfs_server *server = NFS_SERVER(inode);
  51. const struct nfs_pageio_ops *pg_ops = &nfs_pgio_rw_ops;
  52. #ifdef CONFIG_NFS_V4_1
  53. if (server->pnfs_curr_ld && !force_mds)
  54. pg_ops = server->pnfs_curr_ld->pg_read_ops;
  55. #endif
  56. nfs_pageio_init(pgio, inode, pg_ops, compl_ops, &nfs_rw_read_ops,
  57. server->rsize, 0);
  58. }
  59. EXPORT_SYMBOL_GPL(nfs_pageio_init_read);
  60. void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
  61. {
  62. struct nfs_pgio_mirror *mirror;
  63. pgio->pg_ops = &nfs_pgio_rw_ops;
  64. /* read path should never have more than one mirror */
  65. WARN_ON_ONCE(pgio->pg_mirror_count != 1);
  66. mirror = &pgio->pg_mirrors[0];
  67. mirror->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
  68. }
  69. EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
  70. int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
  71. struct page *page)
  72. {
  73. struct nfs_page *new;
  74. unsigned int len;
  75. struct nfs_pageio_descriptor pgio;
  76. struct nfs_pgio_mirror *pgm;
  77. len = nfs_page_length(page);
  78. if (len == 0)
  79. return nfs_return_empty_page(page);
  80. new = nfs_create_request(ctx, page, NULL, 0, len);
  81. if (IS_ERR(new)) {
  82. unlock_page(page);
  83. return PTR_ERR(new);
  84. }
  85. if (len < PAGE_CACHE_SIZE)
  86. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  87. nfs_pageio_init_read(&pgio, inode, false,
  88. &nfs_async_read_completion_ops);
  89. nfs_pageio_add_request(&pgio, new);
  90. nfs_pageio_complete(&pgio);
  91. /* It doesn't make sense to do mirrored reads! */
  92. WARN_ON_ONCE(pgio.pg_mirror_count != 1);
  93. pgm = &pgio.pg_mirrors[0];
  94. NFS_I(inode)->read_io += pgm->pg_bytes_written;
  95. return 0;
  96. }
  97. static void nfs_readpage_release(struct nfs_page *req)
  98. {
  99. struct inode *d_inode = req->wb_context->dentry->d_inode;
  100. dprintk("NFS: read done (%s/%llu %d@%lld)\n", d_inode->i_sb->s_id,
  101. (unsigned long long)NFS_FILEID(d_inode), req->wb_bytes,
  102. (long long)req_offset(req));
  103. if (nfs_page_group_sync_on_bit(req, PG_UNLOCKPAGE)) {
  104. if (PageUptodate(req->wb_page))
  105. nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
  106. unlock_page(req->wb_page);
  107. }
  108. nfs_release_request(req);
  109. }
  110. static void nfs_page_group_set_uptodate(struct nfs_page *req)
  111. {
  112. if (nfs_page_group_sync_on_bit(req, PG_UPTODATE))
  113. SetPageUptodate(req->wb_page);
  114. }
  115. static void nfs_read_completion(struct nfs_pgio_header *hdr)
  116. {
  117. unsigned long bytes = 0;
  118. if (test_bit(NFS_IOHDR_REDO, &hdr->flags))
  119. goto out;
  120. while (!list_empty(&hdr->pages)) {
  121. struct nfs_page *req = nfs_list_entry(hdr->pages.next);
  122. struct page *page = req->wb_page;
  123. unsigned long start = req->wb_pgbase;
  124. unsigned long end = req->wb_pgbase + req->wb_bytes;
  125. if (test_bit(NFS_IOHDR_EOF, &hdr->flags)) {
  126. /* note: regions of the page not covered by a
  127. * request are zeroed in nfs_readpage_async /
  128. * readpage_async_filler */
  129. if (bytes > hdr->good_bytes) {
  130. /* nothing in this request was good, so zero
  131. * the full extent of the request */
  132. zero_user_segment(page, start, end);
  133. } else if (hdr->good_bytes - bytes < req->wb_bytes) {
  134. /* part of this request has good bytes, but
  135. * not all. zero the bad bytes */
  136. start += hdr->good_bytes - bytes;
  137. WARN_ON(start < req->wb_pgbase);
  138. zero_user_segment(page, start, end);
  139. }
  140. }
  141. bytes += req->wb_bytes;
  142. if (test_bit(NFS_IOHDR_ERROR, &hdr->flags)) {
  143. if (bytes <= hdr->good_bytes)
  144. nfs_page_group_set_uptodate(req);
  145. } else
  146. nfs_page_group_set_uptodate(req);
  147. nfs_list_remove_request(req);
  148. nfs_readpage_release(req);
  149. }
  150. out:
  151. hdr->release(hdr);
  152. }
  153. static void nfs_initiate_read(struct nfs_pgio_header *hdr,
  154. struct rpc_message *msg,
  155. const struct nfs_rpc_ops *rpc_ops,
  156. struct rpc_task_setup *task_setup_data, int how)
  157. {
  158. struct inode *inode = hdr->inode;
  159. int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
  160. task_setup_data->flags |= swap_flags;
  161. rpc_ops->read_setup(hdr, msg);
  162. }
  163. static void
  164. nfs_async_read_error(struct list_head *head)
  165. {
  166. struct nfs_page *req;
  167. while (!list_empty(head)) {
  168. req = nfs_list_entry(head->next);
  169. nfs_list_remove_request(req);
  170. nfs_readpage_release(req);
  171. }
  172. }
  173. static const struct nfs_pgio_completion_ops nfs_async_read_completion_ops = {
  174. .error_cleanup = nfs_async_read_error,
  175. .completion = nfs_read_completion,
  176. };
  177. /*
  178. * This is the callback from RPC telling us whether a reply was
  179. * received or some error occurred (timeout or socket shutdown).
  180. */
  181. static int nfs_readpage_done(struct rpc_task *task,
  182. struct nfs_pgio_header *hdr,
  183. struct inode *inode)
  184. {
  185. int status = NFS_PROTO(inode)->read_done(task, hdr);
  186. if (status != 0)
  187. return status;
  188. nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, hdr->res.count);
  189. if (task->tk_status == -ESTALE) {
  190. set_bit(NFS_INO_STALE, &NFS_I(inode)->flags);
  191. nfs_mark_for_revalidate(inode);
  192. }
  193. return 0;
  194. }
  195. static void nfs_readpage_retry(struct rpc_task *task,
  196. struct nfs_pgio_header *hdr)
  197. {
  198. struct nfs_pgio_args *argp = &hdr->args;
  199. struct nfs_pgio_res *resp = &hdr->res;
  200. /* This is a short read! */
  201. nfs_inc_stats(hdr->inode, NFSIOS_SHORTREAD);
  202. /* Has the server at least made some progress? */
  203. if (resp->count == 0) {
  204. nfs_set_pgio_error(hdr, -EIO, argp->offset);
  205. return;
  206. }
  207. /* Yes, so retry the read at the end of the hdr */
  208. hdr->mds_offset += resp->count;
  209. argp->offset += resp->count;
  210. argp->pgbase += resp->count;
  211. argp->count -= resp->count;
  212. rpc_restart_call_prepare(task);
  213. }
  214. static void nfs_readpage_result(struct rpc_task *task,
  215. struct nfs_pgio_header *hdr)
  216. {
  217. if (hdr->res.eof) {
  218. loff_t bound;
  219. bound = hdr->args.offset + hdr->res.count;
  220. spin_lock(&hdr->lock);
  221. if (bound < hdr->io_start + hdr->good_bytes) {
  222. set_bit(NFS_IOHDR_EOF, &hdr->flags);
  223. clear_bit(NFS_IOHDR_ERROR, &hdr->flags);
  224. hdr->good_bytes = bound - hdr->io_start;
  225. }
  226. spin_unlock(&hdr->lock);
  227. } else if (hdr->res.count != hdr->args.count)
  228. nfs_readpage_retry(task, hdr);
  229. }
  230. /*
  231. * Read a page over NFS.
  232. * We read the page synchronously in the following case:
  233. * - The error flag is set for this page. This happens only when a
  234. * previous async read operation failed.
  235. */
  236. int nfs_readpage(struct file *file, struct page *page)
  237. {
  238. struct nfs_open_context *ctx;
  239. struct inode *inode = page_file_mapping(page)->host;
  240. int error;
  241. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  242. page, PAGE_CACHE_SIZE, page_file_index(page));
  243. nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
  244. nfs_inc_stats(inode, NFSIOS_READPAGES);
  245. /*
  246. * Try to flush any pending writes to the file..
  247. *
  248. * NOTE! Because we own the page lock, there cannot
  249. * be any new pending writes generated at this point
  250. * for this page (other pages can be written to).
  251. */
  252. error = nfs_wb_page(inode, page);
  253. if (error)
  254. goto out_unlock;
  255. if (PageUptodate(page))
  256. goto out_unlock;
  257. error = -ESTALE;
  258. if (NFS_STALE(inode))
  259. goto out_unlock;
  260. if (file == NULL) {
  261. error = -EBADF;
  262. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  263. if (ctx == NULL)
  264. goto out_unlock;
  265. } else
  266. ctx = get_nfs_open_context(nfs_file_open_context(file));
  267. if (!IS_SYNC(inode)) {
  268. error = nfs_readpage_from_fscache(ctx, inode, page);
  269. if (error == 0)
  270. goto out;
  271. }
  272. error = nfs_readpage_async(ctx, inode, page);
  273. out:
  274. put_nfs_open_context(ctx);
  275. return error;
  276. out_unlock:
  277. unlock_page(page);
  278. return error;
  279. }
  280. struct nfs_readdesc {
  281. struct nfs_pageio_descriptor *pgio;
  282. struct nfs_open_context *ctx;
  283. };
  284. static int
  285. readpage_async_filler(void *data, struct page *page)
  286. {
  287. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  288. struct nfs_page *new;
  289. unsigned int len;
  290. int error;
  291. len = nfs_page_length(page);
  292. if (len == 0)
  293. return nfs_return_empty_page(page);
  294. new = nfs_create_request(desc->ctx, page, NULL, 0, len);
  295. if (IS_ERR(new))
  296. goto out_error;
  297. if (len < PAGE_CACHE_SIZE)
  298. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  299. if (!nfs_pageio_add_request(desc->pgio, new)) {
  300. error = desc->pgio->pg_error;
  301. goto out_unlock;
  302. }
  303. return 0;
  304. out_error:
  305. error = PTR_ERR(new);
  306. out_unlock:
  307. unlock_page(page);
  308. return error;
  309. }
  310. int nfs_readpages(struct file *filp, struct address_space *mapping,
  311. struct list_head *pages, unsigned nr_pages)
  312. {
  313. struct nfs_pageio_descriptor pgio;
  314. struct nfs_pgio_mirror *pgm;
  315. struct nfs_readdesc desc = {
  316. .pgio = &pgio,
  317. };
  318. struct inode *inode = mapping->host;
  319. unsigned long npages;
  320. int ret = -ESTALE;
  321. dprintk("NFS: nfs_readpages (%s/%Lu %d)\n",
  322. inode->i_sb->s_id,
  323. (unsigned long long)NFS_FILEID(inode),
  324. nr_pages);
  325. nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
  326. if (NFS_STALE(inode))
  327. goto out;
  328. if (filp == NULL) {
  329. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  330. if (desc.ctx == NULL)
  331. return -EBADF;
  332. } else
  333. desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
  334. /* attempt to read as many of the pages as possible from the cache
  335. * - this returns -ENOBUFS immediately if the cookie is negative
  336. */
  337. ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
  338. pages, &nr_pages);
  339. if (ret == 0)
  340. goto read_complete; /* all pages were read */
  341. nfs_pageio_init_read(&pgio, inode, false,
  342. &nfs_async_read_completion_ops);
  343. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  344. nfs_pageio_complete(&pgio);
  345. /* It doesn't make sense to do mirrored reads! */
  346. WARN_ON_ONCE(pgio.pg_mirror_count != 1);
  347. pgm = &pgio.pg_mirrors[0];
  348. NFS_I(inode)->read_io += pgm->pg_bytes_written;
  349. npages = (pgm->pg_bytes_written + PAGE_CACHE_SIZE - 1) >>
  350. PAGE_CACHE_SHIFT;
  351. nfs_add_stats(inode, NFSIOS_READPAGES, npages);
  352. read_complete:
  353. put_nfs_open_context(desc.ctx);
  354. out:
  355. return ret;
  356. }
  357. int __init nfs_init_readpagecache(void)
  358. {
  359. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  360. sizeof(struct nfs_pgio_header),
  361. 0, SLAB_HWCACHE_ALIGN,
  362. NULL);
  363. if (nfs_rdata_cachep == NULL)
  364. return -ENOMEM;
  365. return 0;
  366. }
  367. void nfs_destroy_readpagecache(void)
  368. {
  369. kmem_cache_destroy(nfs_rdata_cachep);
  370. }
  371. static const struct nfs_rw_ops nfs_rw_read_ops = {
  372. .rw_mode = FMODE_READ,
  373. .rw_alloc_header = nfs_readhdr_alloc,
  374. .rw_free_header = nfs_readhdr_free,
  375. .rw_done = nfs_readpage_done,
  376. .rw_result = nfs_readpage_result,
  377. .rw_initiate = nfs_initiate_read,
  378. };