read.c 18 KB

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