read.c 11 KB

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