read.c 18 KB

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