read.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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 = NFS_SERVER(desc->pg_inode)->rsize, 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. return ret;
  287. out_bad:
  288. while (!list_empty(res)) {
  289. data = list_entry(res->next, struct nfs_read_data, list);
  290. list_del(&data->list);
  291. nfs_readdata_free(data);
  292. }
  293. SetPageError(page);
  294. nfs_readpage_release(req);
  295. return -ENOMEM;
  296. }
  297. static int nfs_pagein_one(struct nfs_pageio_descriptor *desc, struct list_head *res)
  298. {
  299. struct nfs_page *req;
  300. struct page **pages;
  301. struct nfs_read_data *data;
  302. struct list_head *head = &desc->pg_list;
  303. int ret = 0;
  304. data = nfs_readdata_alloc(nfs_page_array_len(desc->pg_base,
  305. desc->pg_count));
  306. if (!data) {
  307. nfs_async_read_error(head);
  308. ret = -ENOMEM;
  309. goto out;
  310. }
  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, &data->pages);
  316. ClearPageError(req->wb_page);
  317. *pages++ = req->wb_page;
  318. }
  319. req = nfs_list_entry(data->pages.next);
  320. nfs_read_rpcsetup(req, data, desc->pg_count, 0);
  321. list_add(&data->list, res);
  322. out:
  323. return ret;
  324. }
  325. int nfs_generic_pg_readpages(struct nfs_pageio_descriptor *desc)
  326. {
  327. LIST_HEAD(head);
  328. int ret;
  329. if (desc->pg_bsize < PAGE_CACHE_SIZE) {
  330. ret = nfs_pagein_multi(desc, &head);
  331. if (ret == 0)
  332. ret = nfs_do_multiple_reads(&head,
  333. &nfs_read_partial_ops,
  334. desc->pg_lseg);
  335. } else {
  336. ret = nfs_pagein_one(desc, &head);
  337. if (ret == 0)
  338. ret = nfs_do_multiple_reads(&head,
  339. &nfs_read_full_ops,
  340. desc->pg_lseg);
  341. }
  342. put_lseg(desc->pg_lseg);
  343. desc->pg_lseg = NULL;
  344. return ret;
  345. }
  346. EXPORT_SYMBOL_GPL(nfs_generic_pg_readpages);
  347. static const struct nfs_pageio_ops nfs_pageio_read_ops = {
  348. .pg_test = nfs_generic_pg_test,
  349. .pg_doio = nfs_generic_pg_readpages,
  350. };
  351. /*
  352. * This is the callback from RPC telling us whether a reply was
  353. * received or some error occurred (timeout or socket shutdown).
  354. */
  355. int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
  356. {
  357. int status;
  358. dprintk("NFS: %s: %5u, (status %d)\n", __func__, task->tk_pid,
  359. task->tk_status);
  360. status = NFS_PROTO(data->inode)->read_done(task, data);
  361. if (status != 0)
  362. return status;
  363. nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, data->res.count);
  364. if (task->tk_status == -ESTALE) {
  365. set_bit(NFS_INO_STALE, &NFS_I(data->inode)->flags);
  366. nfs_mark_for_revalidate(data->inode);
  367. }
  368. return 0;
  369. }
  370. static void nfs_readpage_retry(struct rpc_task *task, struct nfs_read_data *data)
  371. {
  372. struct nfs_readargs *argp = &data->args;
  373. struct nfs_readres *resp = &data->res;
  374. if (resp->eof || resp->count == argp->count)
  375. return;
  376. /* This is a short read! */
  377. nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
  378. /* Has the server at least made some progress? */
  379. if (resp->count == 0)
  380. return;
  381. /* Yes, so retry the read at the end of the data */
  382. data->mds_offset += resp->count;
  383. argp->offset += resp->count;
  384. argp->pgbase += resp->count;
  385. argp->count -= resp->count;
  386. nfs_restart_rpc(task, NFS_SERVER(data->inode)->nfs_client);
  387. }
  388. /*
  389. * Handle a read reply that fills part of a page.
  390. */
  391. static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
  392. {
  393. struct nfs_read_data *data = calldata;
  394. if (nfs_readpage_result(task, data) != 0)
  395. return;
  396. if (task->tk_status < 0)
  397. return;
  398. nfs_readpage_truncate_uninitialised_page(data);
  399. nfs_readpage_retry(task, data);
  400. }
  401. static void nfs_readpage_release_partial(void *calldata)
  402. {
  403. struct nfs_read_data *data = calldata;
  404. struct nfs_page *req = data->req;
  405. struct page *page = req->wb_page;
  406. int status = data->task.tk_status;
  407. if (status < 0)
  408. SetPageError(page);
  409. if (atomic_dec_and_test(&req->wb_complete)) {
  410. if (!PageError(page))
  411. SetPageUptodate(page);
  412. nfs_readpage_release(req);
  413. }
  414. nfs_readdata_release(calldata);
  415. }
  416. #if defined(CONFIG_NFS_V4_1)
  417. void nfs_read_prepare(struct rpc_task *task, void *calldata)
  418. {
  419. struct nfs_read_data *data = calldata;
  420. if (nfs4_setup_sequence(NFS_SERVER(data->inode),
  421. &data->args.seq_args, &data->res.seq_res,
  422. 0, task))
  423. return;
  424. rpc_call_start(task);
  425. }
  426. #endif /* CONFIG_NFS_V4_1 */
  427. static const struct rpc_call_ops nfs_read_partial_ops = {
  428. #if defined(CONFIG_NFS_V4_1)
  429. .rpc_call_prepare = nfs_read_prepare,
  430. #endif /* CONFIG_NFS_V4_1 */
  431. .rpc_call_done = nfs_readpage_result_partial,
  432. .rpc_release = nfs_readpage_release_partial,
  433. };
  434. static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
  435. {
  436. unsigned int count = data->res.count;
  437. unsigned int base = data->args.pgbase;
  438. struct page **pages;
  439. if (data->res.eof)
  440. count = data->args.count;
  441. if (unlikely(count == 0))
  442. return;
  443. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  444. base &= ~PAGE_CACHE_MASK;
  445. count += base;
  446. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  447. SetPageUptodate(*pages);
  448. if (count == 0)
  449. return;
  450. /* Was this a short read? */
  451. if (data->res.eof || data->res.count == data->args.count)
  452. SetPageUptodate(*pages);
  453. }
  454. /*
  455. * This is the callback from RPC telling us whether a reply was
  456. * received or some error occurred (timeout or socket shutdown).
  457. */
  458. static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
  459. {
  460. struct nfs_read_data *data = calldata;
  461. if (nfs_readpage_result(task, data) != 0)
  462. return;
  463. if (task->tk_status < 0)
  464. return;
  465. /*
  466. * Note: nfs_readpage_retry may change the values of
  467. * data->args. In the multi-page case, we therefore need
  468. * to ensure that we call nfs_readpage_set_pages_uptodate()
  469. * first.
  470. */
  471. nfs_readpage_truncate_uninitialised_page(data);
  472. nfs_readpage_set_pages_uptodate(data);
  473. nfs_readpage_retry(task, data);
  474. }
  475. static void nfs_readpage_release_full(void *calldata)
  476. {
  477. struct nfs_read_data *data = calldata;
  478. while (!list_empty(&data->pages)) {
  479. struct nfs_page *req = nfs_list_entry(data->pages.next);
  480. nfs_list_remove_request(req);
  481. nfs_readpage_release(req);
  482. }
  483. nfs_readdata_release(calldata);
  484. }
  485. static const struct rpc_call_ops nfs_read_full_ops = {
  486. #if defined(CONFIG_NFS_V4_1)
  487. .rpc_call_prepare = nfs_read_prepare,
  488. #endif /* CONFIG_NFS_V4_1 */
  489. .rpc_call_done = nfs_readpage_result_full,
  490. .rpc_release = nfs_readpage_release_full,
  491. };
  492. /*
  493. * Read a page over NFS.
  494. * We read the page synchronously in the following case:
  495. * - The error flag is set for this page. This happens only when a
  496. * previous async read operation failed.
  497. */
  498. int nfs_readpage(struct file *file, struct page *page)
  499. {
  500. struct nfs_open_context *ctx;
  501. struct inode *inode = page->mapping->host;
  502. int error;
  503. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  504. page, PAGE_CACHE_SIZE, page->index);
  505. nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
  506. nfs_add_stats(inode, NFSIOS_READPAGES, 1);
  507. /*
  508. * Try to flush any pending writes to the file..
  509. *
  510. * NOTE! Because we own the page lock, there cannot
  511. * be any new pending writes generated at this point
  512. * for this page (other pages can be written to).
  513. */
  514. error = nfs_wb_page(inode, page);
  515. if (error)
  516. goto out_unlock;
  517. if (PageUptodate(page))
  518. goto out_unlock;
  519. error = -ESTALE;
  520. if (NFS_STALE(inode))
  521. goto out_unlock;
  522. if (file == NULL) {
  523. error = -EBADF;
  524. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  525. if (ctx == NULL)
  526. goto out_unlock;
  527. } else
  528. ctx = get_nfs_open_context(nfs_file_open_context(file));
  529. if (!IS_SYNC(inode)) {
  530. error = nfs_readpage_from_fscache(ctx, inode, page);
  531. if (error == 0)
  532. goto out;
  533. }
  534. error = nfs_readpage_async(ctx, inode, page);
  535. out:
  536. put_nfs_open_context(ctx);
  537. return error;
  538. out_unlock:
  539. unlock_page(page);
  540. return error;
  541. }
  542. struct nfs_readdesc {
  543. struct nfs_pageio_descriptor *pgio;
  544. struct nfs_open_context *ctx;
  545. };
  546. static int
  547. readpage_async_filler(void *data, struct page *page)
  548. {
  549. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  550. struct inode *inode = page->mapping->host;
  551. struct nfs_page *new;
  552. unsigned int len;
  553. int error;
  554. len = nfs_page_length(page);
  555. if (len == 0)
  556. return nfs_return_empty_page(page);
  557. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  558. if (IS_ERR(new))
  559. goto out_error;
  560. if (len < PAGE_CACHE_SIZE)
  561. zero_user_segment(page, len, PAGE_CACHE_SIZE);
  562. if (!nfs_pageio_add_request(desc->pgio, new)) {
  563. error = desc->pgio->pg_error;
  564. goto out_unlock;
  565. }
  566. return 0;
  567. out_error:
  568. error = PTR_ERR(new);
  569. SetPageError(page);
  570. out_unlock:
  571. unlock_page(page);
  572. return error;
  573. }
  574. int nfs_readpages(struct file *filp, struct address_space *mapping,
  575. struct list_head *pages, unsigned nr_pages)
  576. {
  577. struct nfs_pageio_descriptor pgio;
  578. struct nfs_readdesc desc = {
  579. .pgio = &pgio,
  580. };
  581. struct inode *inode = mapping->host;
  582. unsigned long npages;
  583. int ret = -ESTALE;
  584. dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
  585. inode->i_sb->s_id,
  586. (long long)NFS_FILEID(inode),
  587. nr_pages);
  588. nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
  589. if (NFS_STALE(inode))
  590. goto out;
  591. if (filp == NULL) {
  592. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  593. if (desc.ctx == NULL)
  594. return -EBADF;
  595. } else
  596. desc.ctx = get_nfs_open_context(nfs_file_open_context(filp));
  597. /* attempt to read as many of the pages as possible from the cache
  598. * - this returns -ENOBUFS immediately if the cookie is negative
  599. */
  600. ret = nfs_readpages_from_fscache(desc.ctx, inode, mapping,
  601. pages, &nr_pages);
  602. if (ret == 0)
  603. goto read_complete; /* all pages were read */
  604. nfs_pageio_init_read(&pgio, inode);
  605. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  606. nfs_pageio_complete(&pgio);
  607. npages = (pgio.pg_bytes_written + PAGE_CACHE_SIZE - 1) >> PAGE_CACHE_SHIFT;
  608. nfs_add_stats(inode, NFSIOS_READPAGES, npages);
  609. read_complete:
  610. put_nfs_open_context(desc.ctx);
  611. out:
  612. return ret;
  613. }
  614. int __init nfs_init_readpagecache(void)
  615. {
  616. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  617. sizeof(struct nfs_read_data),
  618. 0, SLAB_HWCACHE_ALIGN,
  619. NULL);
  620. if (nfs_rdata_cachep == NULL)
  621. return -ENOMEM;
  622. nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
  623. nfs_rdata_cachep);
  624. if (nfs_rdata_mempool == NULL)
  625. return -ENOMEM;
  626. return 0;
  627. }
  628. void nfs_destroy_readpagecache(void)
  629. {
  630. mempool_destroy(nfs_rdata_mempool);
  631. kmem_cache_destroy(nfs_rdata_cachep);
  632. }