read.c 18 KB

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