read.c 18 KB

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