read.c 18 KB

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