read.c 17 KB

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