read.c 12 KB

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