read.c 18 KB

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