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