read.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  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. * Handle a read reply that fills part of a page.
  402. */
  403. static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
  404. {
  405. struct nfs_read_data *data = calldata;
  406. struct nfs_page *req = data->req;
  407. struct page *page = req->wb_page;
  408. if (likely(task->tk_status >= 0))
  409. nfs_readpage_truncate_uninitialised_page(data);
  410. else
  411. SetPageError(page);
  412. if (nfs_readpage_result(task, data) != 0)
  413. return;
  414. if (atomic_dec_and_test(&req->wb_complete)) {
  415. if (!PageError(page))
  416. SetPageUptodate(page);
  417. nfs_readpage_release(req);
  418. }
  419. }
  420. static const struct rpc_call_ops nfs_read_partial_ops = {
  421. .rpc_call_done = nfs_readpage_result_partial,
  422. .rpc_release = nfs_readdata_release,
  423. };
  424. static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
  425. {
  426. unsigned int count = data->res.count;
  427. unsigned int base = data->args.pgbase;
  428. struct page **pages;
  429. if (data->res.eof)
  430. count = data->args.count;
  431. if (unlikely(count == 0))
  432. return;
  433. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  434. base &= ~PAGE_CACHE_MASK;
  435. count += base;
  436. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  437. SetPageUptodate(*pages);
  438. if (count != 0)
  439. SetPageUptodate(*pages);
  440. }
  441. static void nfs_readpage_set_pages_error(struct nfs_read_data *data)
  442. {
  443. unsigned int count = data->args.count;
  444. unsigned int base = data->args.pgbase;
  445. struct page **pages;
  446. pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
  447. base &= ~PAGE_CACHE_MASK;
  448. count += base;
  449. for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
  450. SetPageError(*pages);
  451. if (count != 0)
  452. SetPageError(*pages);
  453. }
  454. /*
  455. * This is the callback from RPC telling us whether a reply was
  456. * received or some error occurred (timeout or socket shutdown).
  457. */
  458. static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
  459. {
  460. struct nfs_read_data *data = calldata;
  461. /*
  462. * Note: nfs_readpage_result may change the values of
  463. * data->args. In the multi-page case, we therefore need
  464. * to ensure that we call the next nfs_readpage_set_page_uptodate()
  465. * first in the multi-page case.
  466. */
  467. if (likely(task->tk_status >= 0)) {
  468. nfs_readpage_truncate_uninitialised_page(data);
  469. nfs_readpage_set_pages_uptodate(data);
  470. } else
  471. nfs_readpage_set_pages_error(data);
  472. if (nfs_readpage_result(task, data) != 0)
  473. return;
  474. while (!list_empty(&data->pages)) {
  475. struct nfs_page *req = nfs_list_entry(data->pages.next);
  476. nfs_list_remove_request(req);
  477. nfs_readpage_release(req);
  478. }
  479. }
  480. static const struct rpc_call_ops nfs_read_full_ops = {
  481. .rpc_call_done = nfs_readpage_result_full,
  482. .rpc_release = nfs_readdata_release,
  483. };
  484. /*
  485. * This is the callback from RPC telling us whether a reply was
  486. * received or some error occurred (timeout or socket shutdown).
  487. */
  488. int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
  489. {
  490. struct nfs_readargs *argp = &data->args;
  491. struct nfs_readres *resp = &data->res;
  492. int status;
  493. dprintk("NFS: %4d nfs_readpage_result, (status %d)\n",
  494. task->tk_pid, task->tk_status);
  495. status = NFS_PROTO(data->inode)->read_done(task, data);
  496. if (status != 0)
  497. return status;
  498. nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count);
  499. if (task->tk_status < 0) {
  500. if (task->tk_status == -ESTALE) {
  501. set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
  502. nfs_mark_for_revalidate(data->inode);
  503. }
  504. } else if (resp->count < argp->count && !resp->eof) {
  505. /* This is a short read! */
  506. nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
  507. /* Has the server at least made some progress? */
  508. if (resp->count != 0) {
  509. /* Yes, so retry the read at the end of the data */
  510. argp->offset += resp->count;
  511. argp->pgbase += resp->count;
  512. argp->count -= resp->count;
  513. rpc_restart_call(task);
  514. return -EAGAIN;
  515. }
  516. task->tk_status = -EIO;
  517. }
  518. spin_lock(&data->inode->i_lock);
  519. NFS_I(data->inode)->cache_validity |= NFS_INO_INVALID_ATIME;
  520. spin_unlock(&data->inode->i_lock);
  521. return 0;
  522. }
  523. /*
  524. * Read a page over NFS.
  525. * We read the page synchronously in the following case:
  526. * - The error flag is set for this page. This happens only when a
  527. * previous async read operation failed.
  528. */
  529. int nfs_readpage(struct file *file, struct page *page)
  530. {
  531. struct nfs_open_context *ctx;
  532. struct inode *inode = page->mapping->host;
  533. int error;
  534. dprintk("NFS: nfs_readpage (%p %ld@%lu)\n",
  535. page, PAGE_CACHE_SIZE, page->index);
  536. nfs_inc_stats(inode, NFSIOS_VFSREADPAGE);
  537. nfs_add_stats(inode, NFSIOS_READPAGES, 1);
  538. /*
  539. * Try to flush any pending writes to the file..
  540. *
  541. * NOTE! Because we own the page lock, there cannot
  542. * be any new pending writes generated at this point
  543. * for this page (other pages can be written to).
  544. */
  545. error = nfs_wb_page(inode, page);
  546. if (error)
  547. goto out_error;
  548. error = -ESTALE;
  549. if (NFS_STALE(inode))
  550. goto out_error;
  551. if (file == NULL) {
  552. ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  553. if (ctx == NULL)
  554. return -EBADF;
  555. } else
  556. ctx = get_nfs_open_context((struct nfs_open_context *)
  557. file->private_data);
  558. if (!IS_SYNC(inode)) {
  559. error = nfs_readpage_async(ctx, inode, page);
  560. goto out;
  561. }
  562. error = nfs_readpage_sync(ctx, inode, page);
  563. if (error < 0 && IS_SWAPFILE(inode))
  564. printk("Aiee.. nfs swap-in of page failed!\n");
  565. out:
  566. put_nfs_open_context(ctx);
  567. return error;
  568. out_error:
  569. unlock_page(page);
  570. return error;
  571. }
  572. struct nfs_readdesc {
  573. struct list_head *head;
  574. struct nfs_open_context *ctx;
  575. };
  576. static int
  577. readpage_async_filler(void *data, struct page *page)
  578. {
  579. struct nfs_readdesc *desc = (struct nfs_readdesc *)data;
  580. struct inode *inode = page->mapping->host;
  581. struct nfs_page *new;
  582. unsigned int len;
  583. nfs_wb_page(inode, page);
  584. len = nfs_page_length(inode, page);
  585. if (len == 0)
  586. return nfs_return_empty_page(page);
  587. new = nfs_create_request(desc->ctx, inode, page, 0, len);
  588. if (IS_ERR(new)) {
  589. SetPageError(page);
  590. unlock_page(page);
  591. return PTR_ERR(new);
  592. }
  593. if (len < PAGE_CACHE_SIZE)
  594. memclear_highpage_flush(page, len, PAGE_CACHE_SIZE - len);
  595. nfs_list_add_request(new, desc->head);
  596. return 0;
  597. }
  598. int nfs_readpages(struct file *filp, struct address_space *mapping,
  599. struct list_head *pages, unsigned nr_pages)
  600. {
  601. LIST_HEAD(head);
  602. struct nfs_readdesc desc = {
  603. .head = &head,
  604. };
  605. struct inode *inode = mapping->host;
  606. struct nfs_server *server = NFS_SERVER(inode);
  607. int ret = -ESTALE;
  608. dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
  609. inode->i_sb->s_id,
  610. (long long)NFS_FILEID(inode),
  611. nr_pages);
  612. nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
  613. if (NFS_STALE(inode))
  614. goto out;
  615. if (filp == NULL) {
  616. desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
  617. if (desc.ctx == NULL)
  618. return -EBADF;
  619. } else
  620. desc.ctx = get_nfs_open_context((struct nfs_open_context *)
  621. filp->private_data);
  622. ret = read_cache_pages(mapping, pages, readpage_async_filler, &desc);
  623. if (!list_empty(&head)) {
  624. int err = nfs_pagein_list(&head, server->rpages);
  625. if (!ret)
  626. nfs_add_stats(inode, NFSIOS_READPAGES, err);
  627. ret = err;
  628. }
  629. put_nfs_open_context(desc.ctx);
  630. out:
  631. return ret;
  632. }
  633. int __init nfs_init_readpagecache(void)
  634. {
  635. nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
  636. sizeof(struct nfs_read_data),
  637. 0, SLAB_HWCACHE_ALIGN,
  638. NULL, NULL);
  639. if (nfs_rdata_cachep == NULL)
  640. return -ENOMEM;
  641. nfs_rdata_mempool = mempool_create_slab_pool(MIN_POOL_READ,
  642. nfs_rdata_cachep);
  643. if (nfs_rdata_mempool == NULL)
  644. return -ENOMEM;
  645. return 0;
  646. }
  647. void nfs_destroy_readpagecache(void)
  648. {
  649. mempool_destroy(nfs_rdata_mempool);
  650. kmem_cache_destroy(nfs_rdata_cachep);
  651. }