read.c 18 KB

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