read.c 11 KB

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