read.c 18 KB

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