read.c 17 KB

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