read.c 18 KB

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