aboutsummaryrefslogtreecommitdiffstats
path: root/fs/nfs/read.c
diff options
context:
space:
mode:
Diffstat (limited to 'fs/nfs/read.c')
-rw-r--r--fs/nfs/read.c180
1 files changed, 121 insertions, 59 deletions
diff --git a/fs/nfs/read.c b/fs/nfs/read.c
index 624ca7146b6b..c2e49c397a27 100644
--- a/fs/nfs/read.c
+++ b/fs/nfs/read.c
@@ -15,7 +15,6 @@
15 * within the RPC code when root squashing is suspected. 15 * within the RPC code when root squashing is suspected.
16 */ 16 */
17 17
18#include <linux/config.h>
19#include <linux/time.h> 18#include <linux/time.h>
20#include <linux/kernel.h> 19#include <linux/kernel.h>
21#include <linux/errno.h> 20#include <linux/errno.h>
@@ -44,21 +43,20 @@ static mempool_t *nfs_rdata_mempool;
44 43
45#define MIN_POOL_READ (32) 44#define MIN_POOL_READ (32)
46 45
47struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount) 46struct nfs_read_data *nfs_readdata_alloc(size_t len)
48{ 47{
48 unsigned int pagecount = (len + PAGE_SIZE - 1) >> PAGE_SHIFT;
49 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS); 49 struct nfs_read_data *p = mempool_alloc(nfs_rdata_mempool, SLAB_NOFS);
50 50
51 if (p) { 51 if (p) {
52 memset(p, 0, sizeof(*p)); 52 memset(p, 0, sizeof(*p));
53 INIT_LIST_HEAD(&p->pages); 53 INIT_LIST_HEAD(&p->pages);
54 if (pagecount < NFS_PAGEVEC_SIZE) 54 p->npages = pagecount;
55 p->pagevec = &p->page_array[0]; 55 if (pagecount <= ARRAY_SIZE(p->page_array))
56 p->pagevec = p->page_array;
56 else { 57 else {
57 size_t size = ++pagecount * sizeof(struct page *); 58 p->pagevec = kcalloc(pagecount, sizeof(struct page *), GFP_NOFS);
58 p->pagevec = kmalloc(size, GFP_NOFS); 59 if (!p->pagevec) {
59 if (p->pagevec) {
60 memset(p->pagevec, 0, size);
61 } else {
62 mempool_free(p, nfs_rdata_mempool); 60 mempool_free(p, nfs_rdata_mempool);
63 p = NULL; 61 p = NULL;
64 } 62 }
@@ -67,7 +65,7 @@ struct nfs_read_data *nfs_readdata_alloc(unsigned int pagecount)
67 return p; 65 return p;
68} 66}
69 67
70void nfs_readdata_free(struct nfs_read_data *p) 68static void nfs_readdata_free(struct nfs_read_data *p)
71{ 69{
72 if (p && (p->pagevec != &p->page_array[0])) 70 if (p && (p->pagevec != &p->page_array[0]))
73 kfree(p->pagevec); 71 kfree(p->pagevec);
@@ -104,6 +102,35 @@ int nfs_return_empty_page(struct page *page)
104 return 0; 102 return 0;
105} 103}
106 104
105static void nfs_readpage_truncate_uninitialised_page(struct nfs_read_data *data)
106{
107 unsigned int remainder = data->args.count - data->res.count;
108 unsigned int base = data->args.pgbase + data->res.count;
109 unsigned int pglen;
110 struct page **pages;
111
112 if (data->res.eof == 0 || remainder == 0)
113 return;
114 /*
115 * Note: "remainder" can never be negative, since we check for
116 * this in the XDR code.
117 */
118 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
119 base &= ~PAGE_CACHE_MASK;
120 pglen = PAGE_CACHE_SIZE - base;
121 for (;;) {
122 if (remainder <= pglen) {
123 memclear_highpage_flush(*pages, base, remainder);
124 break;
125 }
126 memclear_highpage_flush(*pages, base, pglen);
127 pages++;
128 remainder -= pglen;
129 pglen = PAGE_CACHE_SIZE;
130 base = 0;
131 }
132}
133
107/* 134/*
108 * Read a page synchronously. 135 * Read a page synchronously.
109 */ 136 */
@@ -115,7 +142,7 @@ static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
115 int result; 142 int result;
116 struct nfs_read_data *rdata; 143 struct nfs_read_data *rdata;
117 144
118 rdata = nfs_readdata_alloc(1); 145 rdata = nfs_readdata_alloc(count);
119 if (!rdata) 146 if (!rdata)
120 return -ENOMEM; 147 return -ENOMEM;
121 148
@@ -144,7 +171,7 @@ static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
144 rdata->args.offset = page_offset(page) + rdata->args.pgbase; 171 rdata->args.offset = page_offset(page) + rdata->args.pgbase;
145 172
146 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n", 173 dprintk("NFS: nfs_proc_read(%s, (%s/%Ld), %Lu, %u)\n",
147 NFS_SERVER(inode)->hostname, 174 NFS_SERVER(inode)->nfs_client->cl_hostname,
148 inode->i_sb->s_id, 175 inode->i_sb->s_id,
149 (long long)NFS_FILEID(inode), 176 (long long)NFS_FILEID(inode),
150 (unsigned long long)rdata->args.pgbase, 177 (unsigned long long)rdata->args.pgbase,
@@ -177,11 +204,11 @@ static int nfs_readpage_sync(struct nfs_open_context *ctx, struct inode *inode,
177 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME; 204 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATIME;
178 spin_unlock(&inode->i_lock); 205 spin_unlock(&inode->i_lock);
179 206
180 if (count) 207 if (rdata->res.eof || rdata->res.count == rdata->args.count) {
181 memclear_highpage_flush(page, rdata->args.pgbase, count); 208 SetPageUptodate(page);
182 SetPageUptodate(page); 209 if (rdata->res.eof && count != 0)
183 if (PageError(page)) 210 memclear_highpage_flush(page, rdata->args.pgbase, count);
184 ClearPageError(page); 211 }
185 result = 0; 212 result = 0;
186 213
187io_error: 214io_error:
@@ -313,25 +340,25 @@ static int nfs_pagein_multi(struct list_head *head, struct inode *inode)
313 struct nfs_page *req = nfs_list_entry(head->next); 340 struct nfs_page *req = nfs_list_entry(head->next);
314 struct page *page = req->wb_page; 341 struct page *page = req->wb_page;
315 struct nfs_read_data *data; 342 struct nfs_read_data *data;
316 unsigned int rsize = NFS_SERVER(inode)->rsize; 343 size_t rsize = NFS_SERVER(inode)->rsize, nbytes;
317 unsigned int nbytes, offset; 344 unsigned int offset;
318 int requests = 0; 345 int requests = 0;
319 LIST_HEAD(list); 346 LIST_HEAD(list);
320 347
321 nfs_list_remove_request(req); 348 nfs_list_remove_request(req);
322 349
323 nbytes = req->wb_bytes; 350 nbytes = req->wb_bytes;
324 for(;;) { 351 do {
325 data = nfs_readdata_alloc(1); 352 size_t len = min(nbytes,rsize);
353
354 data = nfs_readdata_alloc(len);
326 if (!data) 355 if (!data)
327 goto out_bad; 356 goto out_bad;
328 INIT_LIST_HEAD(&data->pages); 357 INIT_LIST_HEAD(&data->pages);
329 list_add(&data->pages, &list); 358 list_add(&data->pages, &list);
330 requests++; 359 requests++;
331 if (nbytes <= rsize) 360 nbytes -= len;
332 break; 361 } while(nbytes != 0);
333 nbytes -= rsize;
334 }
335 atomic_set(&req->wb_complete, requests); 362 atomic_set(&req->wb_complete, requests);
336 363
337 ClearPageError(page); 364 ClearPageError(page);
@@ -379,7 +406,7 @@ static int nfs_pagein_one(struct list_head *head, struct inode *inode)
379 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE) 406 if (NFS_SERVER(inode)->rsize < PAGE_CACHE_SIZE)
380 return nfs_pagein_multi(head, inode); 407 return nfs_pagein_multi(head, inode);
381 408
382 data = nfs_readdata_alloc(NFS_SERVER(inode)->rpages); 409 data = nfs_readdata_alloc(NFS_SERVER(inode)->rsize);
383 if (!data) 410 if (!data)
384 goto out_bad; 411 goto out_bad;
385 412
@@ -436,20 +463,12 @@ static void nfs_readpage_result_partial(struct rpc_task *task, void *calldata)
436 struct nfs_page *req = data->req; 463 struct nfs_page *req = data->req;
437 struct page *page = req->wb_page; 464 struct page *page = req->wb_page;
438 465
466 if (likely(task->tk_status >= 0))
467 nfs_readpage_truncate_uninitialised_page(data);
468 else
469 SetPageError(page);
439 if (nfs_readpage_result(task, data) != 0) 470 if (nfs_readpage_result(task, data) != 0)
440 return; 471 return;
441 if (task->tk_status >= 0) {
442 unsigned int request = data->args.count;
443 unsigned int result = data->res.count;
444
445 if (result < request) {
446 memclear_highpage_flush(page,
447 data->args.pgbase + result,
448 request - result);
449 }
450 } else
451 SetPageError(page);
452
453 if (atomic_dec_and_test(&req->wb_complete)) { 472 if (atomic_dec_and_test(&req->wb_complete)) {
454 if (!PageError(page)) 473 if (!PageError(page))
455 SetPageUptodate(page); 474 SetPageUptodate(page);
@@ -462,6 +481,40 @@ static const struct rpc_call_ops nfs_read_partial_ops = {
462 .rpc_release = nfs_readdata_release, 481 .rpc_release = nfs_readdata_release,
463}; 482};
464 483
484static void nfs_readpage_set_pages_uptodate(struct nfs_read_data *data)
485{
486 unsigned int count = data->res.count;
487 unsigned int base = data->args.pgbase;
488 struct page **pages;
489
490 if (data->res.eof)
491 count = data->args.count;
492 if (unlikely(count == 0))
493 return;
494 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
495 base &= ~PAGE_CACHE_MASK;
496 count += base;
497 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
498 SetPageUptodate(*pages);
499 if (count != 0)
500 SetPageUptodate(*pages);
501}
502
503static void nfs_readpage_set_pages_error(struct nfs_read_data *data)
504{
505 unsigned int count = data->args.count;
506 unsigned int base = data->args.pgbase;
507 struct page **pages;
508
509 pages = &data->args.pages[base >> PAGE_CACHE_SHIFT];
510 base &= ~PAGE_CACHE_MASK;
511 count += base;
512 for (;count >= PAGE_CACHE_SIZE; count -= PAGE_CACHE_SIZE, pages++)
513 SetPageError(*pages);
514 if (count != 0)
515 SetPageError(*pages);
516}
517
465/* 518/*
466 * This is the callback from RPC telling us whether a reply was 519 * This is the callback from RPC telling us whether a reply was
467 * received or some error occurred (timeout or socket shutdown). 520 * received or some error occurred (timeout or socket shutdown).
@@ -469,27 +522,24 @@ static const struct rpc_call_ops nfs_read_partial_ops = {
469static void nfs_readpage_result_full(struct rpc_task *task, void *calldata) 522static void nfs_readpage_result_full(struct rpc_task *task, void *calldata)
470{ 523{
471 struct nfs_read_data *data = calldata; 524 struct nfs_read_data *data = calldata;
472 unsigned int count = data->res.count;
473 525
526 /*
527 * Note: nfs_readpage_result may change the values of
528 * data->args. In the multi-page case, we therefore need
529 * to ensure that we call the next nfs_readpage_set_page_uptodate()
530 * first in the multi-page case.
531 */
532 if (likely(task->tk_status >= 0)) {
533 nfs_readpage_truncate_uninitialised_page(data);
534 nfs_readpage_set_pages_uptodate(data);
535 } else
536 nfs_readpage_set_pages_error(data);
474 if (nfs_readpage_result(task, data) != 0) 537 if (nfs_readpage_result(task, data) != 0)
475 return; 538 return;
476 while (!list_empty(&data->pages)) { 539 while (!list_empty(&data->pages)) {
477 struct nfs_page *req = nfs_list_entry(data->pages.next); 540 struct nfs_page *req = nfs_list_entry(data->pages.next);
478 struct page *page = req->wb_page;
479 nfs_list_remove_request(req);
480 541
481 if (task->tk_status >= 0) { 542 nfs_list_remove_request(req);
482 if (count < PAGE_CACHE_SIZE) {
483 if (count < req->wb_bytes)
484 memclear_highpage_flush(page,
485 req->wb_pgbase + count,
486 req->wb_bytes - count);
487 count = 0;
488 } else
489 count -= PAGE_CACHE_SIZE;
490 SetPageUptodate(page);
491 } else
492 SetPageError(page);
493 nfs_readpage_release(req); 543 nfs_readpage_release(req);
494 } 544 }
495} 545}
@@ -518,8 +568,13 @@ int nfs_readpage_result(struct rpc_task *task, struct nfs_read_data *data)
518 568
519 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count); 569 nfs_add_stats(data->inode, NFSIOS_SERVERREADBYTES, resp->count);
520 570
521 /* Is this a short read? */ 571 if (task->tk_status < 0) {
522 if (task->tk_status >= 0 && resp->count < argp->count && !resp->eof) { 572 if (task->tk_status == -ESTALE) {
573 set_bit(NFS_INO_STALE, &NFS_FLAGS(data->inode));
574 nfs_mark_for_revalidate(data->inode);
575 }
576 } else if (resp->count < argp->count && !resp->eof) {
577 /* This is a short read! */
523 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD); 578 nfs_inc_stats(data->inode, NFSIOS_SHORTREAD);
524 /* Has the server at least made some progress? */ 579 /* Has the server at least made some progress? */
525 if (resp->count != 0) { 580 if (resp->count != 0) {
@@ -566,6 +621,10 @@ int nfs_readpage(struct file *file, struct page *page)
566 if (error) 621 if (error)
567 goto out_error; 622 goto out_error;
568 623
624 error = -ESTALE;
625 if (NFS_STALE(inode))
626 goto out_error;
627
569 if (file == NULL) { 628 if (file == NULL) {
570 ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 629 ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
571 if (ctx == NULL) 630 if (ctx == NULL)
@@ -628,7 +687,7 @@ int nfs_readpages(struct file *filp, struct address_space *mapping,
628 }; 687 };
629 struct inode *inode = mapping->host; 688 struct inode *inode = mapping->host;
630 struct nfs_server *server = NFS_SERVER(inode); 689 struct nfs_server *server = NFS_SERVER(inode);
631 int ret; 690 int ret = -ESTALE;
632 691
633 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n", 692 dprintk("NFS: nfs_readpages (%s/%Ld %d)\n",
634 inode->i_sb->s_id, 693 inode->i_sb->s_id,
@@ -636,6 +695,9 @@ int nfs_readpages(struct file *filp, struct address_space *mapping,
636 nr_pages); 695 nr_pages);
637 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES); 696 nfs_inc_stats(inode, NFSIOS_VFSREADPAGES);
638 697
698 if (NFS_STALE(inode))
699 goto out;
700
639 if (filp == NULL) { 701 if (filp == NULL) {
640 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ); 702 desc.ctx = nfs_find_open_context(inode, NULL, FMODE_READ);
641 if (desc.ctx == NULL) 703 if (desc.ctx == NULL)
@@ -651,10 +713,11 @@ int nfs_readpages(struct file *filp, struct address_space *mapping,
651 ret = err; 713 ret = err;
652 } 714 }
653 put_nfs_open_context(desc.ctx); 715 put_nfs_open_context(desc.ctx);
716out:
654 return ret; 717 return ret;
655} 718}
656 719
657int nfs_init_readpagecache(void) 720int __init nfs_init_readpagecache(void)
658{ 721{
659 nfs_rdata_cachep = kmem_cache_create("nfs_read_data", 722 nfs_rdata_cachep = kmem_cache_create("nfs_read_data",
660 sizeof(struct nfs_read_data), 723 sizeof(struct nfs_read_data),
@@ -674,6 +737,5 @@ int nfs_init_readpagecache(void)
674void nfs_destroy_readpagecache(void) 737void nfs_destroy_readpagecache(void)
675{ 738{
676 mempool_destroy(nfs_rdata_mempool); 739 mempool_destroy(nfs_rdata_mempool);
677 if (kmem_cache_destroy(nfs_rdata_cachep)) 740 kmem_cache_destroy(nfs_rdata_cachep);
678 printk(KERN_INFO "nfs_read_data: not all structures were freed\n");
679} 741}