read.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. * We do an ugly hack here in order to return proper error codes to the
  10. * user program when a read request failed: since generic_file_read
  11. * only checks the return value of inode->i_op->readpage() which is always 0
  12. * for async RPC, we set the error bit of the page to 1 when an error occurs,
  13. * and make nfs_readpage transmit requests synchronously when encountering this.
  14. * This is only a small problem, though, since we now retry all operations
  15. * within the RPC code when root squashing is suspected.
  16. */
  17. #include <linux/time.h>
  18. #include <linux/kernel.h>
  19. #include <linux/errno.h>
  20. #include <linux/fcntl.h>
  21. #include <linux/stat.h>
  22. #include <linux/mm.h>
  23. #include <linux/slab.h>
  24. #include <linux/pagemap.h>
  25. #include <linux/sunrpc/clnt.h>
  26. #include <linux/nfs_fs.h>
  27. #include <linux/nfs_page.h>
  28. #include <linux/smp_lock.h>
  29. #include <asm/system.h>
  30. #include "iostat.h"
  31. #define NFSDBG_FACILITY NFSDBG_PAGECACHE
  32. static int nfs_pagein_one(struct list_head *, struct inode *);
  33. static const struct rpc_call_ops nfs_read_partial_ops;
  34. static const struct rpc_call_ops nfs_read_full_ops;
  35. static kmem_cache_t *nfs_rdata_cachep;
  36. static mempool_t *nfs_rdata_mempool;
  37. #define MIN_POOL_READ (32)
  38. struct nfs_read_data *nfs_readdata_alloc(size_t len)
  39. {
  40. unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
  41. struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
  42. if (p) {
  43. memset(p, 0, sizeof(*p));
  44. INIT_LIST_HEAD(&p->pages);
  45. p->npages = pagecount;
  46. if (pagecount <= ARRAY_SIZE(p->page_array))
  47. p->pagevec = p->page_array;
  48. else {
  49. p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
  50. if (!p->pagevec) {
  51. mempool_free(p, nfs_rdata_mempool);
  52. p = NULL;
  53. }
  54. }
  55. }
  56. return p;
  57. }
  58. static void nfs_readdata_free(struct nfs_read_data *p)
  59. {
  60. if (p && (p->pagevec != &p->page_array[0]))
  61. kfree(p->pagevec);
  62. mempool_free(p, nfs_rdata_mempool);
  63. }
  64. void nfs_readdata_release(void *data)
  65. {
  66. nfs_readdata_free(data);
  67. }
  68. static
  69. unsigned int nfs_page_length(struct inode *inode, struct page *page)
  70. {
  71. loff_t i_size = i_size_read(inode);
  72. unsigned long idx;
  73. if (i_size <= 0)
  74. return 0;
  75. idx = (i_size - 1) >> PAGE_CACHE_SHIFT;
  76. if (page->index > idx)
  77. return 0;
  78. if (page->index != idx)
  79. return PAGE_CACHE_SIZE;
  80. return 1 + ((i_size - 1) & (PAGE_CACHE_SIZE - 1));
  81. }
  82. static
  83. int nfs_return_empty_page(struct page *page)
  84. {
  85. memclear_highpage_flush(page, 0, PAGE_CACHE_SIZE);
  86. SetPageUptodate(page);
  87. unlock_page(page);
  88. return 0;
  89. }
  90. static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
  91. {
  92. unsigned int remainder = data->args.count - data->res.count;
  93. unsigned int base = data->args.pgbase + data->res.count;
  94. unsigned int pglen;
  95. struct page **pages;
  96. if (data->res.eof == 0 || remainder == 0)
  97. return;
  98. /*
  99. * Note: "remainder" can never be negative, since we check for
  100. * this in the XDR code.
  101. */
  102. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  103. base &= ~PAGE_CACHE_MASK;
  104. pglen = PAGE_CACHE_SIZE - base;
  105. for (;;) {
  106. if (remainder <= pglen) {
  107. memclear_highpage_flush(*pages, base, remainder);
  108. break;
  109. }
  110. memclear_highpage_flush(*pages, base, pglen);
  111. pages++;
  112. remainder -= pglen;
  113. pglen = PAGE_CACHE_SIZE;
  114. base = 0;
  115. }
  116. }
  117. /*
  118. * Read a page synchronously.
  119. */
  120. static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
  121. struct page *page)
  122. {
  123. unsigned int rsize = NFS_SERVER(inode)->rsize;
  124. unsigned int count = PAGE_CACHE_SIZE;
  125. int result;
  126. struct nfs_read_data *rdata;
  127. rdata = nfs_readdata_alloc(count);
  128. if (!rdata)
  129. return -ENOMEM;
  130. memset(rdata, 0, sizeof(*rdata));
  131. rdata->flags = (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
  132. rdata->cred = ctx->cred;
  133. rdata->inode = inode;
  134. INIT_LIST_HEAD(&rdata->pages);
  135. rdata->args.fh = NFS_FH(inode);
  136. rdata->args.context = ctx;
  137. rdata->args.pages = &page;
  138. rdata->args.pgbase = 0UL;
  139. rdata->args.count = rsize;
  140. rdata->res.fattr = &rdata->fattr;
  141. dprintk("NFS: nfs_readpage_sync(%p)\n", page);
  142. /*
  143. * This works now because the socket layer never tries to DMA
  144. * into this buffer directly.
  145. */
  146. do {
  147. if (count < rsize)
  148. rdata->args.count = count;
  149. rdata->res.count = rdata->args.count;
  150. rdata->args.offset = page_offset(page) + rdata->args.pgbase;
  151. dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
  152. NFS_SERVER(inode)->nfs_client->cl_hostname,
  153. inode->i_sb->s_id,
  154. (long long)NFS_FILEID(inode),
  155. (unsigned long long)rdata->args.pgbase,
  156. rdata->args.count);
  157. lock_kernel();
  158. result = NFS_PROTO(inode)->read(rdata);
  159. unlock_kernel();
  160. /*
  161. * Even if we had a partial success we can't mark the page
  162. * cache valid.
  163. */
  164. if (result < 0) {
  165. if (result == -EISDIR)
  166. result = -EINVAL;
  167. goto io_error;
  168. }
  169. count -= result;
  170. rdata->args.pgbase += result;
  171. nfs_add_stats(inode, NFSIOS_SERVERREADBYTES, result);
  172. /* Note: result == 0 should only happen if we're caching
  173. * a write that extends the file and punches a hole.
  174. */
  175. if (rdata->res.eof != 0 || result == 0)
  176. break;
  177. } while (count);
  178. spin_lock(&inode->i_lock);
  179. NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  180. spin_unlock(&inode->i_lock);
  181. if (rdata->res.eof || rdata->res.count == rdata->args.count) {
  182. SetPageUptodate(page);
  183. if (rdata->res.eof && count != 0)
  184. memclear_highpage_flush(page, rdata->args.pgbase, count);
  185. }
  186. result = 0;
  187. io_error:
  188. unlock_page(page);
  189. nfs_readdata_free(rdata);
  190. return result;
  191. }
  192. static int nfs_readpage_async(struct nfs_open_context *ctx, struct inode *inode,
  193. struct page *page)
  194. {
  195. LIST_HEAD(one_request);
  196. struct nfs_page *new;
  197. unsigned int len;
  198. len = nfs_page_length(inode, page);
  199. if (len == 0)
  200. return nfs_return_empty_page(page);
  201. new = nfs_create_request(ctx, inode, page, 0, len);
  202. if (IS_ERR(new)) {
  203. unlock_page(page);
  204. return PTR_ERR(new);
  205. }
  206. if (len < PAGE_CACHE_SIZE)
  207. memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
  208. nfs_list_add_request(new, &one_request);
  209. nfs_pagein_one(&one_request, inode);
  210. return 0;
  211. }
  212. static void nfs_readpage_release(struct nfs_page *req)
  213. {
  214. unlock_page(req->wb_page);
  215. dprintk("NFS: read done (%s/%Ld %d@%Ld)\n",
  216. req->wb_context->dentry->d_inode->i_sb->s_id,
  217. (long long)NFS_FILEID(req->wb_context->dentry->d_inode),
  218. req->wb_bytes,
  219. (long long)req_offset(req));
  220. nfs_clear_request(req);
  221. nfs_release_request(req);
  222. }
  223. /*
  224. * Set up the NFS read request struct
  225. */
  226. static void nfs_read_rpcsetup(struct nfs_page *req, struct nfs_read_data *data,
  227. const struct rpc_call_ops *call_ops,
  228. unsigned int count, unsigned int offset)
  229. {
  230. struct inode *inode;
  231. int flags;
  232. data->req = req;
  233. data->inode = inode = req->wb_context->dentry->d_inode;
  234. data->cred = req->wb_context->cred;
  235. data->args.fh = NFS_FH(inode);
  236. data->args.offset = req_offset(req) + offset;
  237. data->args.pgbase = req->wb_pgbase + offset;
  238. data->args.pages = data->pagevec;
  239. data->args.count = count;
  240. data->args.context = req->wb_context;
  241. data->res.fattr = &data->fattr;
  242. data->res.count = count;
  243. data->res.eof = 0;
  244. nfs_fattr_init(&data->fattr);
  245. /* Set up the initial task struct. */
  246. flags = RPC_TASK_ASYNC | (IS_SWAPFILE(inode)? NFS_RPC_SWAPFLAGS : 0);
  247. rpc_init_task(&data->task, NFS_CLIENT(inode), flags, call_ops, data);
  248. NFS_PROTO(inode)->read_setup(data);
  249. data->task.tk_cookie = (unsigned long)inode;
  250. dprintk("NFS: %4d initiated read call (req %s/%Ld, %u bytes @ offset %Lu)\n",
  251. data->task.tk_pid,
  252. inode->i_sb->s_id,
  253. (long long)NFS_FILEID(inode),
  254. count,
  255. (unsigned long long)data->args.offset);
  256. }
  257. static void
  258. nfs_async_read_error(struct list_head *head)
  259. {
  260. struct nfs_page *req;
  261. while (!list_empty(head)) {
  262. req = nfs_list_entry(head->next);
  263. nfs_list_remove_request(req);
  264. SetPageError(req->wb_page);
  265. nfs_readpage_release(req);
  266. }
  267. }
  268. /*
  269. * Start an async read operation
  270. */
  271. static void nfs_execute_read(struct nfs_read_data *data)
  272. {
  273. struct rpc_clnt *clnt = NFS_CLIENT(data->inode);
  274. sigset_t oldset;
  275. rpc_clnt_sigmask(clnt, &oldset);
  276. lock_kernel();
  277. rpc_execute(&data->task);
  278. unlock_kernel();
  279. rpc_clnt_sigunmask(clnt, &oldset);
  280. }
  281. /*
  282. * Generate multiple requests to fill a single page.
  283. *
  284. * We optimize to reduce the number of read operations on the wire. If we
  285. * detect that we're reading a page, or an area of a page, that is past the
  286. * end of file, we do not generate NFS read operations but just clear the
  287. * parts of the page that would have come back zero from the server anyway.
  288. *
  289. * We rely on the cached value of i_size to make this determination; another
  290. * client can fill pages on the server past our cached end-of-file, but we
  291. * won't see the new data until our attribute cache is updated. This is more
  292. * or less conventional NFS client behavior.
  293. */
  294. static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
  295. {
  296. struct nfs_page *req = nfs_list_entry(head->next);
  297. struct page *page = req->wb_page;
  298. struct nfs_read_data *data;
  299. size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
  300. unsigned int offset;
  301. int requests = 0;
  302. LIST_HEAD(list);
  303. nfs_list_remove_request(req);
  304. nbytes = req->wb_bytes;
  305. do {
  306. size_t len = min(nbytes,rsize);
  307. data = nfs_readdata_alloc(len);
  308. if (!data)
  309. goto out_bad;
  310. INIT_LIST_HEAD(&data->pages);
  311. list_add(&data->pages, &list);
  312. requests++;
  313. nbytes -= len;
  314. } while(nbytes != 0);
  315. atomic_set(&req->wb_complete, requests);
  316. ClearPageError(page);
  317. offset = 0;
  318. nbytes = req->wb_bytes;
  319. do {
  320. data = list_entry(list.next, struct nfs_read_data, pages);
  321. list_del_init(&data->pages);
  322. data->pagevec[0] = page;
  323. if (nbytes > rsize) {
  324. nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
  325. rsize, offset);
  326. offset += rsize;
  327. nbytes -= rsize;
  328. } else {
  329. nfs_read_rpcsetup(req, data, &nfs_read_partial_ops,
  330. nbytes, offset);
  331. nbytes = 0;
  332. }
  333. nfs_execute_read(data);
  334. } while (nbytes != 0);
  335. return 0;
  336. out_bad:
  337. while (!list_empty(&list)) {
  338. data = list_entry(list.next, struct nfs_read_data, pages);
  339. list_del(&data->pages);
  340. nfs_readdata_free(data);
  341. }
  342. SetPageError(page);
  343. nfs_readpage_release(req);
  344. return -ENOMEM;
  345. }
  346. static int nfs_pagein_one(struct list_head *head, struct inode *inode)
  347. {
  348. struct nfs_page *req;
  349. struct page **pages;
  350. struct nfs_read_data *data;
  351. unsigned int count;
  352. if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
  353. return nfs_pagein_multi(head, inode);
  354. data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize);
  355. if (!data)
  356. goto out_bad;
  357. INIT_LIST_HEAD(&data->pages);
  358. pages = data->pagevec;
  359. count = 0;
  360. while (!list_empty(head)) {
  361. req = nfs_list_entry(head->next);
  362. nfs_list_remove_request(req);
  363. nfs_list_add_request(req, &data->pages);
  364. ClearPageError(req->wb_page);
  365. *pages++ = req->wb_page;
  366. count += req->wb_bytes;
  367. }
  368. req = nfs_list_entry(data->pages.next);
  369. nfs_read_rpcsetup(req, data, &nfs_read_full_ops, count, 0);
  370. nfs_execute_read(data);
  371. return 0;
  372. out_bad:
  373. nfs_async_read_error(head);
  374. return -ENOMEM;
  375. }
  376. static int
  377. nfs_pagein_list(struct list_head *head, int rpages)
  378. {
  379. LIST_HEAD(one_request);
  380. struct nfs_page *req;
  381. int error = 0;
  382. unsigned int pages = 0;
  383. while (!list_empty(head)) {
  384. pages += nfs_coalesce_requests(head, &one_request, rpages);
  385. req = nfs_list_entry(one_request.next);
  386. error = nfs_pagein_one(&one_request, req->wb_context->dentry->d_inode);
  387. if (error < 0)
  388. break;
  389. }
  390. if (error >= 0)
  391. return pages;
  392. nfs_async_read_error(head);
  393. return error;
  394. }
  395. /*
  396. * Handle a read reply that fills part of a page.
  397. */
  398. static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
  399. {
  400. struct nfs_read_data *data = calldata;
  401. struct nfs_page *req = data->req;
  402. struct page *page = req->wb_page;
  403. if (likely(task->tk_status >= 0))
  404. nfs_readpage_truncate_uninitialised_page(data);
  405. else
  406. SetPageError(page);
  407. if (nfs_readpage_result(task, data) != 0)
  408. return;
  409. if (atomic_dec_and_test(&req->wb_complete)) {
  410. if (!PageError(page))
  411. SetPageUptodate(page);
  412. nfs_readpage_release(req);
  413. }
  414. }
  415. static const struct rpc_call_ops nfs_read_partial_ops = {
  416. .rpc_call_done = nfs_readpage_result_partial,
  417. .rpc_release = nfs_readdata_release,
  418. };
  419. static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
  420. {
  421. unsigned int count = data->res.count;
  422. unsigned int base = data->args.pgbase;
  423. struct page **pages;
  424. if (data->res.eof)
  425. count = data->args.count;
  426. if (unlikely(count == 0))
  427. return;
  428. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  429. base &= ~PAGE_CACHE_MASK;
  430. count += base;
  431. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  432. SetPageUptodate(*pages);
  433. if (count != 0)
  434. SetPageUptodate(*pages);
  435. }
  436. static void nfs_readpage_set_pages_error(struct nfs_read_data *data)
  437. {
  438. unsigned int count = data->args.count;
  439. unsigned int base = data->args.pgbase;
  440. struct page **pages;
  441. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  442. base &= ~PAGE_CACHE_MASK;
  443. count += base;
  444. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  445. SetPageError(*pages);
  446. if (count != 0)
  447. SetPageError(*pages);
  448. }
  449. /*
  450. * This is the callback from RPC telling us whether a reply was
  451. * received or some error occurred (timeout or socket shutdown).
  452. */
  453. static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
  454. {
  455. struct nfs_read_data *data = calldata;
  456. /*
  457. * Note: nfs_readpage_result may change the values of
  458. * data->args. In the multi-page case, we therefore need
  459. * to ensure that we call the next nfs_readpage_set_page_uptodate()
  460. * first in the multi-page case.
  461. */
  462. if (likely(task->tk_status >= 0)) {
  463. nfs_readpage_truncate_uninitialised_page(data);
  464. nfs_readpage_set_pages_uptodate(data);
  465. } else
  466. nfs_readpage_set_pages_error(data);
  467. if (nfs_readpage_result(task, data) != 0)
  468. return;
  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. }
  475. static const struct rpc_call_ops nfs_read_full_ops = {
  476. .rpc_call_done = nfs_readpage_result_full,
  477. .rpc_release = nfs_readdata_release,
  478. };
  479. /*
  480. * This is the callback from RPC telling us whether a reply was
  481. * received or some error occurred (timeout or socket shutdown).
  482. */
  483. int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
  484. {
  485. struct nfs_readargs *argp = &data->args;
  486. struct nfs_readres *resp = &data->res;
  487. int status;
  488. dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
  489. task->tk_pid, task->tk_status);
  490. status = NFS_PROTO(data->inode)->read_done(task, data);
  491. if (status != 0)
  492. return status;
  493. nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count);
  494. if (task->tk_status < 0) {
  495. if (task->tk_status == -ESTALE) {
  496. set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
  497. nfs_mark_for_revalidate(data->inode);
  498. }
  499. } else if (resp->count < argp->count && !resp->eof) {
  500. /* This is a short read! */
  501. nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
  502. /* Has the server at least made some progress? */
  503. if (resp->count != 0) {
  504. /* Yes, so retry the read at the end of the data */
  505. argp->offset += resp->count;
  506. argp->pgbase += resp->count;
  507. argp->count -= resp->count;
  508. rpc_restart_call(task);
  509. return -EAGAIN;
  510. }
  511. task->tk_status = -EIO;
  512. }
  513. spin_lock(&data->inode->i_lock);
  514. NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  515. spin_unlock(&data->inode->i_lock);
  516. return 0;
  517. }
  518. /*
  519. * Read a page over NFS.
  520. * We read the page synchronously in the following case:
  521. * - The error flag is set for this page. This happens only when a
  522. * previous async read operation failed.
  523. */
  524. int nfs_readpage(struct file *file, struct page *page)
  525. {
  526. struct nfs_open_context *ctx;
  527. struct inode *inode = page->mapping->host;
  528. int error;
  529. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  530. page, PAGE_CACHE_SIZE, page->index);
  531. nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
  532. nfs_add_stats(inode, NFSIOS_READPAGES, 1);
  533. /*
  534. * Try to flush any pending writes to the file..
  535. *
  536. * NOTE! Because we own the page lock, there cannot
  537. * be any new pending writes generated at this point
  538. * for this page (other pages can be written to).
  539. */
  540. error = nfs_wb_page(inode, page);
  541. if (error)
  542. goto out_error;
  543. error = -ESTALE;
  544. if (NFS_STALE(inode))
  545. goto out_error;
  546. if (file == NULL) {
  547. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  548. if (ctx == NULL)
  549. return -EBADF;
  550. } else
  551. ctx = get_nfs_open_context((struct nfs_open_context *)
  552. file->private_data);
  553. if (!IS_SYNC(inode)) {
  554. error = nfs_readpage_async(ctx, inode, page);
  555. goto out;
  556. }
  557. error = nfs_readpage_sync(ctx, inode, page);
  558. if (error < 0 && IS_SWAPFILE(inode))
  559. printk("Aiee.. nfs swap-in of page failed!\n");
  560. out:
  561. put_nfs_open_context(ctx);
  562. return error;
  563. out_error:
  564. unlock_page(page);
  565. return error;
  566. }
  567. struct nfs_readdesc {
  568. struct list_head *head;
  569. struct nfs_open_context *ctx;
  570. };
  571. static int
  572. readpage_async_filler(void *data, struct page *page)
  573. {
  574. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  575. struct inode *inode = page->mapping->host;
  576. struct nfs_page *new;
  577. unsigned int len;
  578. nfs_wb_page(inode, page);
  579. len = nfs_page_length(inode, page);
  580. if (len == 0)
  581. return nfs_return_empty_page(page);
  582. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  583. if (IS_ERR(new)) {
  584. SetPageError(page);
  585. unlock_page(page);
  586. return PTR_ERR(new);
  587. }
  588. if (len < PAGE_CACHE_SIZE)
  589. memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
  590. nfs_list_add_request(new, desc->head);
  591. return 0;
  592. }
  593. int nfs_readpages(struct file *filp, struct address_space *mapping,
  594. struct list_head *pages, unsigned nr_pages)
  595. {
  596. LIST_HEAD(head);
  597. struct nfs_readdesc desc = {
  598. .head = &head,
  599. };
  600. struct inode *inode = mapping->host;
  601. struct nfs_server *server = NFS_SERVER(inode);
  602. int ret = -ESTALE;
  603. dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
  604. inode->i_sb->s_id,
  605. (long long)NFS_FILEID(inode),
  606. nr_pages);
  607. nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
  608. if (NFS_STALE(inode))
  609. goto out;
  610. if (filp == NULL) {
  611. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  612. if (desc.ctx == NULL)
  613. return -EBADF;
  614. } else
  615. desc.ctx = get_nfs_open_context((struct nfs_open_context *)
  616. filp->private_data);
  617. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  618. if (!list_empty(&head)) {
  619. int err = nfs_pagein_list(&head, server->rpages);
  620. if (!ret)
  621. nfs_add_stats(inode, NFSIOS_READPAGES, err);
  622. ret = err;
  623. }
  624. put_nfs_open_context(desc.ctx);
  625. out:
  626. return ret;
  627. }
  628. int __init nfs_init_readpagecache(void)
  629. {
  630. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  631. sizeof(struct nfs_read_data),
  632. 0, SLAB_HWCACHE_ALIGN,
  633. NULL, NULL);
  634. if (nfs_rdata_cachep == NULL)
  635. return -ENOMEM;
  636. nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
  637. nfs_rdata_cachep);
  638. if (nfs_rdata_mempool == NULL)
  639. return -ENOMEM;
  640. return 0;
  641. }
  642. void nfs_destroy_readpagecache(void)
  643. {
  644. mempool_destroy(nfs_rdata_mempool);
  645. kmem_cache_destroy(nfs_rdata_cachep);
  646. }