read.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700
  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 <asm/system.h>
  22. #include "pnfs.h"
  23. #include "nfs4_fs.h"
  24. #include "internal.h"
  25. #include "iostat.h"
  26. #include "fscache.h"
  27. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  28. static const struct nfs_pageio_ops nfs_pageio_read_ops;
  29. static const struct rpc_call_ops nfs_read_partial_ops;
  30. static const struct rpc_call_ops nfs_read_full_ops;
  31. static struct kmem_cache *nfs_rdata_cachep;
  32. struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
  33. {
  34. struct nfs_read_data *p;
  35. p = kmem_cache_zalloc(nfs_rdata_cachep, GFP_KERNEL);
  36. if (p) {
  37. INIT_LIST_HEAD(&p->pages);
  38. p->npages = pagecount;
  39. if (pagecount <= ARRAY_SIZE(p->page_array))
  40. p->pagevec = p->page_array;
  41. else {
  42. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_KERNEL);
  43. if (!p->pagevec) {
  44. kmem_cache_free(nfs_rdata_cachep, p);
  45. p = NULL;
  46. }
  47. }
  48. }
  49. return p;
  50. }
  51. void nfs_readdata_free(struct nfs_read_data *p)
  52. {
  53. if (p && (p->pagevec != &p->page_array[0]))
  54. kfree(p->pagevec);
  55. kmem_cache_free(nfs_rdata_cachep, p);
  56. }
  57. void nfs_readdata_release(struct nfs_read_data *rdata)
  58. {
  59. put_nfs_open_context(rdata->args.context);
  60. nfs_readdata_free(rdata);
  61. }
  62. static
  63. int nfs_return_empty_page(struct page *page)
  64. {
  65. zero_user(page, 0, PAGE_CACHE_SIZE);
  66. SetPageUptodate(page);
  67. unlock_page(page);
  68. return 0;
  69. }
  70. static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
  71. {
  72. unsigned int remainder = data->args.count - data->res.count;
  73. unsigned int base = data->args.pgbase + data->res.count;
  74. unsigned int pglen;
  75. struct page **pages;
  76. if (data->res.eof == 0 || remainder == 0)
  77. return;
  78. /*
  79. * Note: "remainder" can never be negative, since we check for
  80. * this in the XDR code.
  81. */
  82. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  83. base &= ~PAGE_CACHE_MASK;
  84. pglen = PAGE_CACHE_SIZE - base;
  85. for (;;) {
  86. if (remainder <= pglen) {
  87. zero_user(*pages, base, remainder);
  88. break;
  89. }
  90. zero_user(*pages, base, pglen);
  91. pages++;
  92. remainder -= pglen;
  93. pglen = PAGE_CACHE_SIZE;
  94. base = 0;
  95. }
  96. }
  97. void nfs_pageio_init_read_mds(struct nfs_pageio_descriptor *pgio,
  98. struct inode *inode)
  99. {
  100. nfs_pageio_init(pgio, inode, &nfs_pageio_read_ops,
  101. NFS_SERVER(inode)->rsize, 0);
  102. }
  103. void nfs_pageio_reset_read_mds(struct nfs_pageio_descriptor *pgio)
  104. {
  105. pgio->pg_ops = &nfs_pageio_read_ops;
  106. pgio->pg_bsize = NFS_SERVER(pgio->pg_inode)->rsize;
  107. }
  108. EXPORT_SYMBOL_GPL(nfs_pageio_reset_read_mds);
  109. static void nfs_pageio_init_read(struct nfs_pageio_descriptor *pgio,
  110. struct inode *inode)
  111. {
  112. if (!pnfs_pageio_init_read(pgio, inode))
  113. nfs_pageio_init_read_mds(pgio, inode);
  114. }
  115. int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
  116. struct page *page)
  117. {
  118. struct nfs_page *new;
  119. unsigned int len;
  120. struct nfs_pageio_descriptor pgio;
  121. len = nfs_page_length(page);
  122. if (len == 0)
  123. return nfs_return_empty_page(page);
  124. new = nfs_create_request(ctx, inode, page, 0, len);
  125. if (IS_ERR(new)) {
  126. unlock_page(page);
  127. return PTR_ERR(new);
  128. }
  129. if (len < PAGE_CACHE_SIZE)
  130. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  131. nfs_pageio_init_read(&pgio, inode);
  132. nfs_pageio_add_request(&pgio, new);
  133. nfs_pageio_complete(&pgio);
  134. return 0;
  135. }
  136. static void nfs_readpage_release(struct nfs_page *req)
  137. {
  138. struct inode *d_inode = req->wb_context->dentry->d_inode;
  139. if (PageUptodate(req->wb_page))
  140. nfs_readpage_to_fscache(d_inode, req->wb_page, 0);
  141. unlock_page(req->wb_page);
  142. dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
  143. req->wb_context->dentry->d_inode->i_sb->s_id,
  144. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  145. req->wb_bytes,
  146. (long long)req_offset(req));
  147. nfs_release_request(req);
  148. }
  149. int nfs_initiate_read(struct nfs_read_data *data, struct rpc_clnt *clnt,
  150. const struct rpc_call_ops *call_ops)
  151. {
  152. struct inode *inode = data->inode;
  153. int swap_flags = IS_SWAPFILE(inode) ? NFS_RPC_SWAPFLAGS : 0;
  154. struct rpc_task *task;
  155. struct rpc_message msg = {
  156. .rpc_argp = &data->args,
  157. .rpc_resp = &data->res,
  158. .rpc_cred = data->cred,
  159. };
  160. struct rpc_task_setup task_setup_data = {
  161. .task = &data->task,
  162. .rpc_client = clnt,
  163. .rpc_message = &msg,
  164. .callback_ops = call_ops,
  165. .callback_data = data,
  166. .workqueue = nfsiod_workqueue,
  167. .flags = RPC_TASK_ASYNC | swap_flags,
  168. };
  169. /* Set up the initial task struct. */
  170. NFS_PROTO(inode)->read_setup(data, &msg);
  171. dprintk("NFS: %5u initiated read call (req %s/%lld, %u bytes @ "
  172. "offset %llu)\n",
  173. data->task.tk_pid,
  174. inode->i_sb->s_id,
  175. (long long)NFS_FILEID(inode),
  176. data->args.count,
  177. (unsigned long long)data->args.offset);
  178. task = rpc_run_task(&task_setup_data);
  179. if (IS_ERR(task))
  180. return PTR_ERR(task);
  181. rpc_put_task(task);
  182. return 0;
  183. }
  184. EXPORT_SYMBOL_GPL(nfs_initiate_read);
  185. /*
  186. * Set up the NFS read request struct
  187. */
  188. static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
  189. unsigned int count, unsigned int offset)
  190. {
  191. struct inode *inode = req->wb_context->dentry->d_inode;
  192. data->req = req;
  193. data->inode = inode;
  194. data->cred = req->wb_context->cred;
  195. data->args.fh = NFS_FH(inode);
  196. data->args.offset = req_offset(req) + offset;
  197. data->args.pgbase = req->wb_pgbase + offset;
  198. data->args.pages = data->pagevec;
  199. data->args.count = count;
  200. data->args.context = get_nfs_open_context(req->wb_context);
  201. data->args.lock_context = req->wb_lock_context;
  202. data->res.fattr = &data->fattr;
  203. data->res.count = count;
  204. data->res.eof = 0;
  205. nfs_fattr_init(&data->fattr);
  206. }
  207. static int nfs_do_read(struct nfs_read_data *data,
  208. const struct rpc_call_ops *call_ops)
  209. {
  210. struct inode *inode = data->args.context->dentry->d_inode;
  211. return nfs_initiate_read(data, NFS_CLIENT(inode), call_ops);
  212. }
  213. static int
  214. nfs_do_multiple_reads(struct list_head *head,
  215. const struct rpc_call_ops *call_ops)
  216. {
  217. struct nfs_read_data *data;
  218. int ret = 0;
  219. while (!list_empty(head)) {
  220. int ret2;
  221. data = list_entry(head->next, struct nfs_read_data, list);
  222. list_del_init(&data->list);
  223. ret2 = nfs_do_read(data, call_ops);
  224. if (ret == 0)
  225. ret = ret2;
  226. }
  227. return ret;
  228. }
  229. static void
  230. nfs_async_read_error(struct list_head *head)
  231. {
  232. struct nfs_page *req;
  233. while (!list_empty(head)) {
  234. req = nfs_list_entry(head->next);
  235. nfs_list_remove_request(req);
  236. nfs_readpage_release(req);
  237. }
  238. }
  239. /*
  240. * Generate multiple requests to fill a single page.
  241. *
  242. * We optimize to reduce the number of read operations on the wire. If we
  243. * detect that we're reading a page, or an area of a page, that is past the
  244. * end of file, we do not generate NFS read operations but just clear the
  245. * parts of the page that would have come back zero from the server anyway.
  246. *
  247. * We rely on the cached value of i_size to make this determination; another
  248. * client can fill pages on the server past our cached end-of-file, but we
  249. * won't see the new data until our attribute cache is updated. This is more
  250. * or less conventional NFS client behavior.
  251. */
  252. static int nfs_pagein_multi(struct nfs_pageio_descriptor *desc, struct list_head *res)
  253. {
  254. struct nfs_page *req = nfs_list_entry(desc->pg_list.next);
  255. struct page *page = req->wb_page;
  256. struct nfs_read_data *data;
  257. size_t rsize = desc->pg_bsize, nbytes;
  258. unsigned int offset;
  259. int requests = 0;
  260. int ret = 0;
  261. nfs_list_remove_request(req);
  262. offset = 0;
  263. nbytes = desc->pg_count;
  264. do {
  265. size_t len = min(nbytes,rsize);
  266. data = nfs_readdata_alloc(1);
  267. if (!data)
  268. goto out_bad;
  269. data->pagevec[0] = page;
  270. nfs_read_rpcsetup(req, data, len, offset);
  271. list_add(&data->list, res);
  272. requests++;
  273. nbytes -= len;
  274. offset += len;
  275. } while(nbytes != 0);
  276. atomic_set(&req->wb_complete, requests);
  277. desc->pg_rpc_callops = &nfs_read_partial_ops;
  278. return ret;
  279. out_bad:
  280. while (!list_empty(res)) {
  281. data = list_entry(res->next, struct nfs_read_data, list);
  282. list_del(&data->list);
  283. nfs_readdata_free(data);
  284. }
  285. nfs_readpage_release(req);
  286. return -ENOMEM;
  287. }
  288. static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
  289. {
  290. struct nfs_page *req;
  291. struct page **pages;
  292. struct nfs_read_data *data;
  293. struct list_head *head = &desc->pg_list;
  294. int ret = 0;
  295. data = nfs_readdata_alloc(nfs_page_array_len(desc->pg_base,
  296. desc->pg_count));
  297. if (!data) {
  298. nfs_async_read_error(head);
  299. ret = -ENOMEM;
  300. goto out;
  301. }
  302. pages = data->pagevec;
  303. while (!list_empty(head)) {
  304. req = nfs_list_entry(head->next);
  305. nfs_list_remove_request(req);
  306. nfs_list_add_request(req, &data->pages);
  307. *pages++ = req->wb_page;
  308. }
  309. req = nfs_list_entry(data->pages.next);
  310. nfs_read_rpcsetup(req, data, desc->pg_count, 0);
  311. list_add(&data->list, res);
  312. desc->pg_rpc_callops = &nfs_read_full_ops;
  313. out:
  314. return ret;
  315. }
  316. int nfs_generic_pagein(struct nfs_pageio_descriptor *desc, struct list_head *head)
  317. {
  318. if (desc->pg_bsize < PAGE_CACHE_SIZE)
  319. return nfs_pagein_multi(desc, head);
  320. return nfs_pagein_one(desc, head);
  321. }
  322. static int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  323. {
  324. LIST_HEAD(head);
  325. int ret;
  326. ret = nfs_generic_pagein(desc, &head);
  327. if (ret == 0)
  328. ret = nfs_do_multiple_reads(&head, desc->pg_rpc_callops);
  329. return ret;
  330. }
  331. static const struct nfs_pageio_ops nfs_pageio_read_ops = {
  332. .pg_test = nfs_generic_pg_test,
  333. .pg_doio = nfs_generic_pg_readpages,
  334. };
  335. /*
  336. * This is the callback from RPC telling us whether a reply was
  337. * received or some error occurred (timeout or socket shutdown).
  338. */
  339. int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
  340. {
  341. int status;
  342. dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
  343. task->tk_status);
  344. status = NFS_PROTO(data->inode)->read_done(task, data);
  345. if (status != 0)
  346. return status;
  347. nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
  348. if (task->tk_status == -ESTALE) {
  349. set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
  350. nfs_mark_for_revalidate(data->inode);
  351. }
  352. return 0;
  353. }
  354. static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
  355. {
  356. struct nfs_readargs *argp = &data->args;
  357. struct nfs_readres *resp = &data->res;
  358. if (resp->eof || resp->count == argp->count)
  359. return;
  360. /* This is a short read! */
  361. nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
  362. /* Has the server at least made some progress? */
  363. if (resp->count == 0)
  364. return;
  365. /* Yes, so retry the read at the end of the data */
  366. data->mds_offset += resp->count;
  367. argp->offset += resp->count;
  368. argp->pgbase += resp->count;
  369. argp->count -= resp->count;
  370. rpc_restart_call_prepare(task);
  371. }
  372. /*
  373. * Handle a read reply that fills part of a page.
  374. */
  375. static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
  376. {
  377. struct nfs_read_data *data = calldata;
  378. if (nfs_readpage_result(task, data) != 0)
  379. return;
  380. if (task->tk_status < 0)
  381. return;
  382. nfs_readpage_truncate_uninitialised_page(data);
  383. nfs_readpage_retry(task, data);
  384. }
  385. static void nfs_readpage_release_partial(void *calldata)
  386. {
  387. struct nfs_read_data *data = calldata;
  388. struct nfs_page *req = data->req;
  389. struct page *page = req->wb_page;
  390. int status = data->task.tk_status;
  391. if (status < 0)
  392. set_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags);
  393. if (atomic_dec_and_test(&req->wb_complete)) {
  394. if (!test_bit(PG_PARTIAL_READ_FAILED, &req->wb_flags))
  395. SetPageUptodate(page);
  396. nfs_readpage_release(req);
  397. }
  398. nfs_readdata_release(calldata);
  399. }
  400. void nfs_read_prepare(struct rpc_task *task, void *calldata)
  401. {
  402. struct nfs_read_data *data = calldata;
  403. NFS_PROTO(data->inode)->read_rpc_prepare(task, data);
  404. }
  405. static const struct rpc_call_ops nfs_read_partial_ops = {
  406. .rpc_call_prepare = nfs_read_prepare,
  407. .rpc_call_done = nfs_readpage_result_partial,
  408. .rpc_release = nfs_readpage_release_partial,
  409. };
  410. static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
  411. {
  412. unsigned int count = data->res.count;
  413. unsigned int base = data->args.pgbase;
  414. struct page **pages;
  415. if (data->res.eof)
  416. count = data->args.count;
  417. if (unlikely(count == 0))
  418. return;
  419. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  420. base &= ~PAGE_CACHE_MASK;
  421. count += base;
  422. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  423. SetPageUptodate(*pages);
  424. if (count == 0)
  425. return;
  426. /* Was this a short read? */
  427. if (data->res.eof || data->res.count == data->args.count)
  428. SetPageUptodate(*pages);
  429. }
  430. /*
  431. * This is the callback from RPC telling us whether a reply was
  432. * received or some error occurred (timeout or socket shutdown).
  433. */
  434. static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
  435. {
  436. struct nfs_read_data *data = calldata;
  437. if (nfs_readpage_result(task, data) != 0)
  438. return;
  439. if (task->tk_status < 0)
  440. return;
  441. /*
  442. * Note: nfs_readpage_retry may change the values of
  443. * data->args. In the multi-page case, we therefore need
  444. * to ensure that we call nfs_readpage_set_pages_uptodate()
  445. * first.
  446. */
  447. nfs_readpage_truncate_uninitialised_page(data);
  448. nfs_readpage_set_pages_uptodate(data);
  449. nfs_readpage_retry(task, data);
  450. }
  451. static void nfs_readpage_release_full(void *calldata)
  452. {
  453. struct nfs_read_data *data = calldata;
  454. while (!list_empty(&data->pages)) {
  455. struct nfs_page *req = nfs_list_entry(data->pages.next);
  456. nfs_list_remove_request(req);
  457. nfs_readpage_release(req);
  458. }
  459. nfs_readdata_release(calldata);
  460. }
  461. static const struct rpc_call_ops nfs_read_full_ops = {
  462. .rpc_call_prepare = nfs_read_prepare,
  463. .rpc_call_done = nfs_readpage_result_full,
  464. .rpc_release = nfs_readpage_release_full,
  465. };
  466. /*
  467. * Read a page over NFS.
  468. * We read the page synchronously in the following case:
  469. * - The error flag is set for this page. This happens only when a
  470. * previous async read operation failed.
  471. */
  472. int nfs_readpage(struct file *file, struct page *page)
  473. {
  474. struct nfs_open_context *ctx;
  475. struct inode *inode = page->mapping->host;
  476. int error;
  477. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  478. page, PAGE_CACHE_SIZE, page->index);
  479. nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
  480. nfs_add_stats(inode, NFSIOS_READPAGES, 1);
  481. /*
  482. * Try to flush any pending writes to the file..
  483. *
  484. * NOTE! Because we own the page lock, there cannot
  485. * be any new pending writes generated at this point
  486. * for this page (other pages can be written to).
  487. */
  488. error = nfs_wb_page(inode, page);
  489. if (error)
  490. goto out_unlock;
  491. if (PageUptodate(page))
  492. goto out_unlock;
  493. error = -ESTALE;
  494. if (NFS_STALE(inode))
  495. goto out_unlock;
  496. if (file == NULL) {
  497. error = -EBADF;
  498. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  499. if (ctx == NULL)
  500. goto out_unlock;
  501. } else
  502. ctx = get_nfs_open_context(nfs_file_open_context(file));
  503. if (!IS_SYNC(inode)) {
  504. error = nfs_readpage_from_fscache(ctx, inode, page);
  505. if (error == 0)
  506. goto out;
  507. }
  508. error = nfs_readpage_async(ctx, inode, page);
  509. out:
  510. put_nfs_open_context(ctx);
  511. return error;
  512. out_unlock:
  513. unlock_page(page);
  514. return error;
  515. }
  516. struct nfs_readdesc {
  517. struct nfs_pageio_descriptor *pgio;
  518. struct nfs_open_context *ctx;
  519. };
  520. static int
  521. readpage_async_filler(void *data, struct page *page)
  522. {
  523. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  524. struct inode *inode = page->mapping->host;
  525. struct nfs_page *new;
  526. unsigned int len;
  527. int error;
  528. len = nfs_page_length(page);
  529. if (len == 0)
  530. return nfs_return_empty_page(page);
  531. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  532. if (IS_ERR(new))
  533. goto out_error;
  534. if (len < PAGE_CACHE_SIZE)
  535. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  536. if (!nfs_pageio_add_request(desc->pgio, new)) {
  537. error = desc->pgio->pg_error;
  538. goto out_unlock;
  539. }
  540. return 0;
  541. out_error:
  542. error = PTR_ERR(new);
  543. out_unlock:
  544. unlock_page(page);
  545. return error;
  546. }
  547. int nfs_readpages(struct file *filp, struct address_space *mapping,
  548. struct list_head *pages, unsigned nr_pages)
  549. {
  550. struct nfs_pageio_descriptor pgio;
  551. struct nfs_readdesc desc = {
  552. .pgio = &pgio,
  553. };
  554. struct inode *inode = mapping->host;
  555. unsigned long npages;
  556. int ret = -ESTALE;
  557. dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
  558. inode->i_sb->s_id,
  559. (long long)NFS_FILEID(inode),
  560. nr_pages);
  561. nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
  562. if (NFS_STALE(inode))
  563. goto out;
  564. if (filp == NULL) {
  565. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  566. if (desc.ctx == NULL)
  567. return -EBADF;
  568. } else
  569. desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
  570. /* attempt to read as many of the pages as possible from the cache
  571. * - this returns -ENOBUFS immediately if the cookie is negative
  572. */
  573. ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
  574. pages, &nr_pages);
  575. if (ret == 0)
  576. goto read_complete; /* all pages were read */
  577. nfs_pageio_init_read(&pgio, inode);
  578. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  579. nfs_pageio_complete(&pgio);
  580. npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  581. nfs_add_stats(inode, NFSIOS_READPAGES, npages);
  582. read_complete:
  583. put_nfs_open_context(desc.ctx);
  584. out:
  585. return ret;
  586. }
  587. int __init nfs_init_readpagecache(void)
  588. {
  589. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  590. sizeof(struct nfs_read_data),
  591. 0, SLAB_HWCACHE_ALIGN,
  592. NULL);
  593. if (nfs_rdata_cachep == NULL)
  594. return -ENOMEM;
  595. return 0;
  596. }
  597. void nfs_destroy_readpagecache(void)
  598. {
  599. kmem_cache_destroy(nfs_rdata_cachep);
  600. }