read.c 11 KB

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