read.c 19 KB

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