read.c 18 KB

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