Fedora kernel-2.6.17-1.2142_FC4 patched with stable patch-2.6.17.4-vs2.0.2-rc26.diff
[linux-2.6.git] / drivers / md / raid6main.c
index 908edd7..bc69355 100644 (file)
@@ -29,6 +29,8 @@
 #include <asm/atomic.h>
 #include "raid6.h"
 
+#include <linux/raid/bitmap.h>
+
 /*
  * Stripe cache
  */
 #define STRIPE_SHIFT           (PAGE_SHIFT - 9)
 #define STRIPE_SECTORS         (STRIPE_SIZE>>9)
 #define        IO_THRESHOLD            1
-#define HASH_PAGES             1
-#define HASH_PAGES_ORDER       0
-#define NR_HASH                        (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
+#define NR_HASH                        (PAGE_SIZE / sizeof(struct hlist_head))
 #define HASH_MASK              (NR_HASH - 1)
 
-#define stripe_hash(conf, sect)        ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
+#define stripe_hash(conf, sect)        (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
 
 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
  * order without overlap.  There may be several bio's per stripe+device, and
@@ -88,18 +88,21 @@ static inline int raid6_next_disk(int disk, int raid_disks)
 
 static void print_raid6_conf (raid6_conf_t *conf);
 
-static inline void __release_stripe(raid6_conf_t *conf, struct stripe_head *sh)
+static void __release_stripe(raid6_conf_t *conf, struct stripe_head *sh)
 {
        if (atomic_dec_and_test(&sh->count)) {
-               if (!list_empty(&sh->lru))
-                       BUG();
-               if (atomic_read(&conf->active_stripes)==0)
-                       BUG();
+               BUG_ON(!list_empty(&sh->lru));
+               BUG_ON(atomic_read(&conf->active_stripes)==0);
                if (test_bit(STRIPE_HANDLE, &sh->state)) {
                        if (test_bit(STRIPE_DELAYED, &sh->state))
                                list_add_tail(&sh->lru, &conf->delayed_list);
-                       else
+                       else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
+                                conf->seq_write == sh->bm_seq)
+                               list_add_tail(&sh->lru, &conf->bitmap_list);
+                       else {
+                               clear_bit(STRIPE_BIT_DELAY, &sh->state);
                                list_add_tail(&sh->lru, &conf->handle_list);
+                       }
                        md_wakeup_thread(conf->mddev->thread);
                } else {
                        if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
@@ -110,7 +113,7 @@ static inline void __release_stripe(raid6_conf_t *conf, struct stripe_head *sh)
                        list_add_tail(&sh->lru, &conf->inactive_list);
                        atomic_dec(&conf->active_stripes);
                        if (!conf->inactive_blocked ||
-                           atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
+                           atomic_read(&conf->active_stripes) < (conf->max_nr_stripes*3/4))
                                wake_up(&conf->wait_for_stripe);
                }
        }
@@ -125,29 +128,21 @@ static void release_stripe(struct stripe_head *sh)
        spin_unlock_irqrestore(&conf->device_lock, flags);
 }
 
-static void remove_hash(struct stripe_head *sh)
+static inline void remove_hash(struct stripe_head *sh)
 {
        PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
 
-       if (sh->hash_pprev) {
-               if (sh->hash_next)
-                       sh->hash_next->hash_pprev = sh->hash_pprev;
-               *sh->hash_pprev = sh->hash_next;
-               sh->hash_pprev = NULL;
-       }
+       hlist_del_init(&sh->hash);
 }
 
-static __inline__ void insert_hash(raid6_conf_t *conf, struct stripe_head *sh)
+static inline void insert_hash(raid6_conf_t *conf, struct stripe_head *sh)
 {
-       struct stripe_head **shp = &stripe_hash(conf, sh->sector);
+       struct hlist_head *hp = stripe_hash(conf, sh->sector);
 
        PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
 
        CHECK_DEVLOCK();
-       if ((sh->hash_next = *shp) != NULL)
-               (*shp)->hash_pprev = &sh->hash_next;
-       *shp = sh;
-       sh->hash_pprev = shp;
+       hlist_add_head(&sh->hash, hp);
 }
 
 
@@ -179,7 +174,7 @@ static void shrink_buffers(struct stripe_head *sh, int num)
                if (!p)
                        continue;
                sh->dev[i].page = NULL;
-               page_cache_release(p);
+               put_page(p);
        }
 }
 
@@ -200,15 +195,13 @@ static int grow_buffers(struct stripe_head *sh, int num)
 
 static void raid6_build_block (struct stripe_head *sh, int i);
 
-static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
+static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
 {
        raid6_conf_t *conf = sh->raid_conf;
        int disks = conf->raid_disks, i;
 
-       if (atomic_read(&sh->count) != 0)
-               BUG();
-       if (test_bit(STRIPE_HANDLE, &sh->state))
-               BUG();
+       BUG_ON(atomic_read(&sh->count) != 0);
+       BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
 
        CHECK_DEVLOCK();
        PRINTK("init_stripe called, stripe %llu\n",
@@ -240,10 +233,11 @@ static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_i
 static struct stripe_head *__find_stripe(raid6_conf_t *conf, sector_t sector)
 {
        struct stripe_head *sh;
+       struct hlist_node *hn;
 
        CHECK_DEVLOCK();
        PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
-       for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
+       hlist_for_each_entry (sh, hn,  stripe_hash(conf, sector), hash)
                if (sh->sector == sector)
                        return sh;
        PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
@@ -262,6 +256,9 @@ static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector
        spin_lock_irq(&conf->device_lock);
 
        do {
+               wait_event_lock_irq(conf->wait_for_stripe,
+                                   conf->quiesce == 0,
+                                   conf->device_lock, /* nothing */);
                sh = __find_stripe(conf, sector);
                if (!sh) {
                        if (!conf->inactive_blocked)
@@ -272,7 +269,8 @@ static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector
                                conf->inactive_blocked = 1;
                                wait_event_lock_irq(conf->wait_for_stripe,
                                                    !list_empty(&conf->inactive_list) &&
-                                                   (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
+                                                   (atomic_read(&conf->active_stripes)
+                                                    < (conf->max_nr_stripes *3/4)
                                                     || !conf->inactive_blocked),
                                                    conf->device_lock,
                                                    unplug_slaves(conf->mddev);
@@ -282,13 +280,11 @@ static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector
                                init_stripe(sh, sector, pd_idx);
                } else {
                        if (atomic_read(&sh->count)) {
-                               if (!list_empty(&sh->lru))
-                                       BUG();
+                               BUG_ON(!list_empty(&sh->lru));
                        } else {
                                if (!test_bit(STRIPE_HANDLE, &sh->state))
                                        atomic_inc(&conf->active_stripes);
-                               if (list_empty(&sh->lru))
-                                       BUG();
+                               BUG_ON(list_empty(&sh->lru));
                                list_del_init(&sh->lru);
                        }
                }
@@ -301,64 +297,75 @@ static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector
        return sh;
 }
 
-static int grow_stripes(raid6_conf_t *conf, int num)
+static int grow_one_stripe(raid6_conf_t *conf)
 {
        struct stripe_head *sh;
+       sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
+       if (!sh)
+               return 0;
+       memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
+       sh->raid_conf = conf;
+       spin_lock_init(&sh->lock);
+
+       if (grow_buffers(sh, conf->raid_disks)) {
+               shrink_buffers(sh, conf->raid_disks);
+               kmem_cache_free(conf->slab_cache, sh);
+               return 0;
+       }
+       /* we just created an active stripe so... */
+       atomic_set(&sh->count, 1);
+       atomic_inc(&conf->active_stripes);
+       INIT_LIST_HEAD(&sh->lru);
+       release_stripe(sh);
+       return 1;
+}
+
+static int grow_stripes(raid6_conf_t *conf, int num)
+{
        kmem_cache_t *sc;
        int devs = conf->raid_disks;
 
-       sprintf(conf->cache_name, "raid6/%s", mdname(conf->mddev));
+       sprintf(conf->cache_name[0], "raid6/%s", mdname(conf->mddev));
 
-       sc = kmem_cache_create(conf->cache_name,
+       sc = kmem_cache_create(conf->cache_name[0],
                               sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
                               0, 0, NULL, NULL);
        if (!sc)
                return 1;
        conf->slab_cache = sc;
-       while (num--) {
-               sh = kmem_cache_alloc(sc, GFP_KERNEL);
-               if (!sh)
-                       return 1;
-               memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
-               sh->raid_conf = conf;
-               spin_lock_init(&sh->lock);
-
-               if (grow_buffers(sh, conf->raid_disks)) {
-                       shrink_buffers(sh, conf->raid_disks);
-                       kmem_cache_free(sc, sh);
+       while (num--)
+               if (!grow_one_stripe(conf))
                        return 1;
-               }
-               /* we just created an active stripe so... */
-               atomic_set(&sh->count, 1);
-               atomic_inc(&conf->active_stripes);
-               INIT_LIST_HEAD(&sh->lru);
-               release_stripe(sh);
-       }
        return 0;
 }
 
-static void shrink_stripes(raid6_conf_t *conf)
+static int drop_one_stripe(raid6_conf_t *conf)
 {
        struct stripe_head *sh;
+       spin_lock_irq(&conf->device_lock);
+       sh = get_free_stripe(conf);
+       spin_unlock_irq(&conf->device_lock);
+       if (!sh)
+               return 0;
+       BUG_ON(atomic_read(&sh->count));
+       shrink_buffers(sh, conf->raid_disks);
+       kmem_cache_free(conf->slab_cache, sh);
+       atomic_dec(&conf->active_stripes);
+       return 1;
+}
 
-       while (1) {
-               spin_lock_irq(&conf->device_lock);
-               sh = get_free_stripe(conf);
-               spin_unlock_irq(&conf->device_lock);
-               if (!sh)
-                       break;
-               if (atomic_read(&sh->count))
-                       BUG();
-               shrink_buffers(sh, conf->raid_disks);
-               kmem_cache_free(conf->slab_cache, sh);
-               atomic_dec(&conf->active_stripes);
-       }
-       kmem_cache_destroy(conf->slab_cache);
+static void shrink_stripes(raid6_conf_t *conf)
+{
+       while (drop_one_stripe(conf))
+               ;
+
+       if (conf->slab_cache)
+               kmem_cache_destroy(conf->slab_cache);
        conf->slab_cache = NULL;
 }
 
-static int raid6_end_read_request (struct bio * bi, unsigned int bytes_done,
-                                  int error)
+static int raid6_end_read_request(struct bio * bi, unsigned int bytes_done,
+                                 int error)
 {
        struct stripe_head *sh = bi->bi_private;
        raid6_conf_t *conf = sh->raid_conf;
@@ -410,9 +417,35 @@ static int raid6_end_read_request (struct bio * bi, unsigned int bytes_done,
 #else
                set_bit(R5_UPTODATE, &sh->dev[i].flags);
 #endif
+               if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
+                       printk(KERN_INFO "raid6: read error corrected!!\n");
+                       clear_bit(R5_ReadError, &sh->dev[i].flags);
+                       clear_bit(R5_ReWrite, &sh->dev[i].flags);
+               }
+               if (atomic_read(&conf->disks[i].rdev->read_errors))
+                       atomic_set(&conf->disks[i].rdev->read_errors, 0);
        } else {
-               md_error(conf->mddev, conf->disks[i].rdev);
+               int retry = 0;
                clear_bit(R5_UPTODATE, &sh->dev[i].flags);
+               atomic_inc(&conf->disks[i].rdev->read_errors);
+               if (conf->mddev->degraded)
+                       printk(KERN_WARNING "raid6: read error not correctable.\n");
+               else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
+                       /* Oh, no!!! */
+                       printk(KERN_WARNING "raid6: read error NOT corrected!!\n");
+               else if (atomic_read(&conf->disks[i].rdev->read_errors)
+                        > conf->max_nr_stripes)
+                       printk(KERN_WARNING
+                              "raid6: Too many read errors, failing device.\n");
+               else
+                       retry = 1;
+               if (retry)
+                       set_bit(R5_ReadError, &sh->dev[i].flags);
+               else {
+                       clear_bit(R5_ReadError, &sh->dev[i].flags);
+                       clear_bit(R5_ReWrite, &sh->dev[i].flags);
+                       md_error(conf->mddev, conf->disks[i].rdev);
+               }
        }
        rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
 #if 0
@@ -497,19 +530,19 @@ static void error(mddev_t *mddev, mdk_rdev_t *rdev)
        raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
        PRINTK("raid6: error called\n");
 
-       if (!rdev->faulty) {
+       if (!test_bit(Faulty, &rdev->flags)) {
                mddev->sb_dirty = 1;
-               if (rdev->in_sync) {
+               if (test_bit(In_sync, &rdev->flags)) {
                        conf->working_disks--;
                        mddev->degraded++;
                        conf->failed_disks++;
-                       rdev->in_sync = 0;
+                       clear_bit(In_sync, &rdev->flags);
                        /*
                         * if recovery was running, make sure it aborts.
                         */
                        set_bit(MD_RECOVERY_ERR, &mddev->recovery);
                }
-               rdev->faulty = 1;
+               set_bit(Faulty, &rdev->flags);
                printk (KERN_ALERT
                        "raid6: Disk failure on %s, disabling device."
                        " Operation continuing on %d devices\n",
@@ -740,7 +773,7 @@ static void compute_parity(struct stripe_head *sh, int method)
                                if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
                                        wake_up(&conf->wait_for_overlap);
 
-                               if (sh->dev[i].written) BUG();
+                               BUG_ON(sh->dev[i].written);
                                sh->dev[i].written = chosen;
                        }
                break;
@@ -795,7 +828,7 @@ static void compute_parity(struct stripe_head *sh, int method)
 }
 
 /* Compute one missing block */
-static void compute_block_1(struct stripe_head *sh, int dd_idx)
+static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
 {
        raid6_conf_t *conf = sh->raid_conf;
        int i, count, disks = conf->raid_disks;
@@ -811,7 +844,7 @@ static void compute_block_1(struct stripe_head *sh, int dd_idx)
                compute_parity(sh, UPDATE_PARITY);
        } else {
                ptr[0] = page_address(sh->dev[dd_idx].page);
-               memset(ptr[0], 0, STRIPE_SIZE);
+               if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
                count = 1;
                for (i = disks ; i--; ) {
                        if (i == dd_idx || i == qd_idx)
@@ -828,7 +861,8 @@ static void compute_block_1(struct stripe_head *sh, int dd_idx)
                }
                if (count != 1)
                        xor_block(count, STRIPE_SIZE, ptr);
-               set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
+               if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
+               else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
        }
 }
 
@@ -861,7 +895,7 @@ static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
                        return;
                } else {
                        /* We're missing D+Q; recompute D from P */
-                       compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1);
+                       compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
                        compute_parity(sh, UPDATE_PARITY); /* Is this necessary? */
                        return;
                }
@@ -906,6 +940,7 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
 {
        struct bio **bip;
        raid6_conf_t *conf = sh->raid_conf;
+       int firstwrite=0;
 
        PRINTK("adding bh b#%llu to stripe s#%llu\n",
                (unsigned long long)bi->bi_sector,
@@ -914,9 +949,11 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
 
        spin_lock(&sh->lock);
        spin_lock_irq(&conf->device_lock);
-       if (forwrite)
+       if (forwrite) {
                bip = &sh->dev[dd_idx].towrite;
-       else
+               if (*bip == NULL && sh->dev[dd_idx].written == NULL)
+                       firstwrite = 1;
+       } else
                bip = &sh->dev[dd_idx].toread;
        while (*bip && (*bip)->bi_sector < bi->bi_sector) {
                if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
@@ -926,8 +963,7 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
        if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
                goto overlap;
 
-       if (*bip && bi->bi_next && (*bip) != bi->bi_next)
-               BUG();
+       BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
        if (*bip)
                bi->bi_next = *bip;
        *bip = bi;
@@ -939,6 +975,13 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
                (unsigned long long)bi->bi_sector,
                (unsigned long long)sh->sector, dd_idx);
 
+       if (conf->mddev->bitmap && firstwrite) {
+               sh->bm_seq = conf->seq_write;
+               bitmap_startwrite(conf->mddev->bitmap, sh->sector,
+                                 STRIPE_SECTORS, 0);
+               set_bit(STRIPE_BIT_DELAY, &sh->state);
+       }
+
        if (forwrite) {
                /* check if page is covered */
                sector_t sector = sh->dev[dd_idx].sector;
@@ -962,6 +1005,12 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
 }
 
 
+static int page_is_zero(struct page *p)
+{
+       char *a = page_address(p);
+       return ((*(u32*)a) == 0 &&
+               memcmp(a, a+4, STRIPE_SIZE-4)==0);
+}
 /*
  * handle_stripe - do things to a stripe.
  *
@@ -980,7 +1029,7 @@ static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, in
  *
  */
 
-static void handle_stripe(struct stripe_head *sh)
+static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
 {
        raid6_conf_t *conf = sh->raid_conf;
        int disks = conf->raid_disks;
@@ -1007,11 +1056,11 @@ static void handle_stripe(struct stripe_head *sh)
        syncing = test_bit(STRIPE_SYNCING, &sh->state);
        /* Now to look around and see what can be done */
 
+       rcu_read_lock();
        for (i=disks; i--; ) {
                mdk_rdev_t *rdev;
                dev = &sh->dev[i];
                clear_bit(R5_Insync, &dev->flags);
-               clear_bit(R5_Syncio, &dev->flags);
 
                PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
                        i, dev->flags, dev->toread, dev->towrite, dev->written);
@@ -1050,14 +1099,21 @@ static void handle_stripe(struct stripe_head *sh)
                                non_overwrite++;
                }
                if (dev->written) written++;
-               rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
-               if (!rdev || !rdev->in_sync) {
+               rdev = rcu_dereference(conf->disks[i].rdev);
+               if (!rdev || !test_bit(In_sync, &rdev->flags)) {
+                       /* The ReadError flag will just be confusing now */
+                       clear_bit(R5_ReadError, &dev->flags);
+                       clear_bit(R5_ReWrite, &dev->flags);
+               }
+               if (!rdev || !test_bit(In_sync, &rdev->flags)
+                   || test_bit(R5_ReadError, &dev->flags)) {
                        if ( failed < 2 )
                                failed_num[failed] = i;
                        failed++;
                } else
                        set_bit(R5_Insync, &dev->flags);
        }
+       rcu_read_unlock();
        PRINTK("locked=%d uptodate=%d to_read=%d"
               " to_write=%d failed=%d failed_num=%d,%d\n",
               locked, uptodate, to_read, to_write, failed,
@@ -1066,12 +1122,24 @@ static void handle_stripe(struct stripe_head *sh)
         * need to be failed
         */
        if (failed > 2 && to_read+to_write+written) {
-               spin_lock_irq(&conf->device_lock);
                for (i=disks; i--; ) {
+                       int bitmap_end = 0;
+
+                       if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
+                               mdk_rdev_t *rdev;
+                               rcu_read_lock();
+                               rdev = rcu_dereference(conf->disks[i].rdev);
+                               if (rdev && test_bit(In_sync, &rdev->flags))
+                                       /* multiple read failures in one stripe */
+                                       md_error(conf->mddev, rdev);
+                               rcu_read_unlock();
+                       }
+
+                       spin_lock_irq(&conf->device_lock);
                        /* fail all writes first */
                        bi = sh->dev[i].towrite;
                        sh->dev[i].towrite = NULL;
-                       if (bi) to_write--;
+                       if (bi) { to_write--; bitmap_end = 1; }
 
                        if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
                                wake_up(&conf->wait_for_overlap);
@@ -1089,6 +1157,7 @@ static void handle_stripe(struct stripe_head *sh)
                        /* and fail all 'written' */
                        bi = sh->dev[i].written;
                        sh->dev[i].written = NULL;
+                       if (bi) bitmap_end = 1;
                        while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
                                struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
                                clear_bit(BIO_UPTODATE, &bi->bi_flags);
@@ -1101,7 +1170,8 @@ static void handle_stripe(struct stripe_head *sh)
                        }
 
                        /* fail any reads if this device is non-operational */
-                       if (!test_bit(R5_Insync, &sh->dev[i].flags)) {
+                       if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
+                           test_bit(R5_ReadError, &sh->dev[i].flags)) {
                                bi = sh->dev[i].toread;
                                sh->dev[i].toread = NULL;
                                if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
@@ -1117,8 +1187,11 @@ static void handle_stripe(struct stripe_head *sh)
                                        bi = nextbi;
                                }
                        }
+                       spin_unlock_irq(&conf->device_lock);
+                       if (bitmap_end)
+                               bitmap_endwrite(conf->mddev->bitmap, sh->sector,
+                                               STRIPE_SECTORS, 0, 0);
                }
-               spin_unlock_irq(&conf->device_lock);
        }
        if (failed > 2 && syncing) {
                md_done_sync(conf->mddev, STRIPE_SECTORS,0);
@@ -1155,6 +1228,7 @@ static void handle_stripe(struct stripe_head *sh)
                                if (!test_bit(R5_LOCKED, &dev->flags) &&
                                    test_bit(R5_UPTODATE, &dev->flags) ) {
                                        /* We can return any write requests */
+                                       int bitmap_end = 0;
                                        struct bio *wbi, *wbi2;
                                        PRINTK("Return write for stripe %llu disc %d\n",
                                               (unsigned long long)sh->sector, i);
@@ -1170,7 +1244,13 @@ static void handle_stripe(struct stripe_head *sh)
                                                }
                                                wbi = wbi2;
                                        }
+                                       if (dev->towrite == NULL)
+                                               bitmap_end = 1;
                                        spin_unlock_irq(&conf->device_lock);
+                                       if (bitmap_end)
+                                               bitmap_endwrite(conf->mddev->bitmap, sh->sector,
+                                                               STRIPE_SECTORS,
+                                                               !test_bit(STRIPE_DEGRADED, &sh->state), 0);
                                }
                        }
        }
@@ -1196,7 +1276,7 @@ static void handle_stripe(struct stripe_head *sh)
                                if (uptodate == disks-1) {
                                        PRINTK("Computing stripe %llu block %d\n",
                                               (unsigned long long)sh->sector, i);
-                                       compute_block_1(sh, i);
+                                       compute_block_1(sh, i, 0);
                                        uptodate++;
                                } else if ( uptodate == disks-2 && failed >= 2 ) {
                                        /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
@@ -1227,9 +1307,6 @@ static void handle_stripe(struct stripe_head *sh)
                                        locked++;
                                        PRINTK("Reading block %d (sync=%d)\n",
                                                i, syncing);
-                                       if (syncing)
-                                               md_sync_acct(conf->disks[i].rdev->bdev,
-                                                            STRIPE_SECTORS);
                                }
                        }
                }
@@ -1285,12 +1362,13 @@ static void handle_stripe(struct stripe_head *sh)
                                }
                        }
                /* now if nothing is locked, and if we have enough data, we can start a write request */
-               if (locked == 0 && rcw == 0) {
+               if (locked == 0 && rcw == 0 &&
+                   !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
                        if ( must_compute > 0 ) {
                                /* We have failed blocks and need to compute them */
                                switch ( failed ) {
                                case 0: BUG();
-                               case 1: compute_block_1(sh, failed_num[0]); break;
+                               case 1: compute_block_1(sh, failed_num[0], 0); break;
                                case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
                                default: BUG(); /* This request should have been failed? */
                                }
@@ -1305,12 +1383,10 @@ static void handle_stripe(struct stripe_head *sh)
                                               (unsigned long long)sh->sector, i);
                                        locked++;
                                        set_bit(R5_Wantwrite, &sh->dev[i].flags);
-#if 0 /**** FIX: I don't understand the logic here... ****/
-                                       if (!test_bit(R5_Insync, &sh->dev[i].flags)
-                                           || ((i==pd_idx || i==qd_idx) && failed == 0)) /* FIX? */
-                                               set_bit(STRIPE_INSYNC, &sh->state);
-#endif
                                }
+                       /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
+                       set_bit(STRIPE_INSYNC, &sh->state);
+
                        if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
                                atomic_dec(&conf->preread_active_stripes);
                                if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
@@ -1323,83 +1399,119 @@ static void handle_stripe(struct stripe_head *sh)
         * Any reads will already have been scheduled, so we just see if enough data
         * is available
         */
-       if (syncing && locked == 0 &&
-           !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 2) {
+       if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
+               int update_p = 0, update_q = 0;
+               struct r5dev *dev;
+
                set_bit(STRIPE_HANDLE, &sh->state);
-#if 0 /* RAID-6: Don't support CHECK PARITY yet */
-               if (failed == 0) {
-                       char *pagea;
-                       if (uptodate != disks)
-                               BUG();
-                       compute_parity(sh, CHECK_PARITY);
-                       uptodate--;
-                       pagea = page_address(sh->dev[pd_idx].page);
-                       if ((*(u32*)pagea) == 0 &&
-                           !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
-                               /* parity is correct (on disc, not in buffer any more) */
-                               set_bit(STRIPE_INSYNC, &sh->state);
-                       }
-               }
-#endif
-               if (!test_bit(STRIPE_INSYNC, &sh->state)) {
-                       int failed_needupdate[2];
-                       struct r5dev *adev, *bdev;
 
-                       if ( failed < 1 )
-                               failed_num[0] = pd_idx;
-                       if ( failed < 2 )
-                               failed_num[1] = (failed_num[0] == qd_idx) ? pd_idx : qd_idx;
-
-                       failed_needupdate[0] = !test_bit(R5_UPTODATE, &sh->dev[failed_num[0]].flags);
-                       failed_needupdate[1] = !test_bit(R5_UPTODATE, &sh->dev[failed_num[1]].flags);
-
-                       PRINTK("sync: failed=%d num=%d,%d fnu=%u%u\n",
-                              failed, failed_num[0], failed_num[1], failed_needupdate[0], failed_needupdate[1]);
-
-#if 0  /* RAID-6: This code seems to require that CHECK_PARITY destroys the uptodateness of the parity */
-                       /* should be able to compute the missing block(s) and write to spare */
-                       if ( failed_needupdate[0] ^ failed_needupdate[1] ) {
-                               if (uptodate+1 != disks)
-                                       BUG();
-                               compute_block_1(sh, failed_needupdate[0] ? failed_num[0] : failed_num[1]);
-                               uptodate++;
-                       } else if ( failed_needupdate[0] & failed_needupdate[1] ) {
-                               if (uptodate+2 != disks)
-                                       BUG();
-                               compute_block_2(sh, failed_num[0], failed_num[1]);
-                               uptodate += 2;
+               BUG_ON(failed>2);
+               BUG_ON(uptodate < disks);
+               /* Want to check and possibly repair P and Q.
+                * However there could be one 'failed' device, in which
+                * case we can only check one of them, possibly using the
+                * other to generate missing data
+                */
+
+               /* If !tmp_page, we cannot do the calculations,
+                * but as we have set STRIPE_HANDLE, we will soon be called
+                * by stripe_handle with a tmp_page - just wait until then.
+                */
+               if (tmp_page) {
+                       if (failed == q_failed) {
+                               /* The only possible failed device holds 'Q', so it makes
+                                * sense to check P (If anything else were failed, we would
+                                * have used P to recreate it).
+                                */
+                               compute_block_1(sh, pd_idx, 1);
+                               if (!page_is_zero(sh->dev[pd_idx].page)) {
+                                       compute_block_1(sh,pd_idx,0);
+                                       update_p = 1;
+                               }
+                       }
+                       if (!q_failed && failed < 2) {
+                               /* q is not failed, and we didn't use it to generate
+                                * anything, so it makes sense to check it
+                                */
+                               memcpy(page_address(tmp_page),
+                                      page_address(sh->dev[qd_idx].page),
+                                      STRIPE_SIZE);
+                               compute_parity(sh, UPDATE_PARITY);
+                               if (memcmp(page_address(tmp_page),
+                                          page_address(sh->dev[qd_idx].page),
+                                          STRIPE_SIZE)!= 0) {
+                                       clear_bit(STRIPE_INSYNC, &sh->state);
+                                       update_q = 1;
+                               }
+                       }
+                       if (update_p || update_q) {
+                               conf->mddev->resync_mismatches += STRIPE_SECTORS;
+                               if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
+                                       /* don't try to repair!! */
+                                       update_p = update_q = 0;
                        }
-#else
-                       compute_block_2(sh, failed_num[0], failed_num[1]);
-                       uptodate += failed_needupdate[0] + failed_needupdate[1];
-#endif
 
-                       if (uptodate != disks)
-                               BUG();
+                       /* now write out any block on a failed drive,
+                        * or P or Q if they need it
+                        */
 
-                       PRINTK("Marking for sync stripe %llu blocks %d,%d\n",
-                              (unsigned long long)sh->sector, failed_num[0], failed_num[1]);
+                       if (failed == 2) {
+                               dev = &sh->dev[failed_num[1]];
+                               locked++;
+                               set_bit(R5_LOCKED, &dev->flags);
+                               set_bit(R5_Wantwrite, &dev->flags);
+                       }
+                       if (failed >= 1) {
+                               dev = &sh->dev[failed_num[0]];
+                               locked++;
+                               set_bit(R5_LOCKED, &dev->flags);
+                               set_bit(R5_Wantwrite, &dev->flags);
+                       }
 
-                       /**** FIX: Should we really do both of these unconditionally? ****/
-                       adev = &sh->dev[failed_num[0]];
-                       locked += !test_bit(R5_LOCKED, &adev->flags);
-                       set_bit(R5_LOCKED, &adev->flags);
-                       set_bit(R5_Wantwrite, &adev->flags);
-                       bdev = &sh->dev[failed_num[1]];
-                       locked += !test_bit(R5_LOCKED, &bdev->flags);
-                       set_bit(R5_LOCKED, &bdev->flags);
-                       set_bit(R5_Wantwrite, &bdev->flags);
+                       if (update_p) {
+                               dev = &sh->dev[pd_idx];
+                               locked ++;
+                               set_bit(R5_LOCKED, &dev->flags);
+                               set_bit(R5_Wantwrite, &dev->flags);
+                       }
+                       if (update_q) {
+                               dev = &sh->dev[qd_idx];
+                               locked++;
+                               set_bit(R5_LOCKED, &dev->flags);
+                               set_bit(R5_Wantwrite, &dev->flags);
+                       }
+                       clear_bit(STRIPE_DEGRADED, &sh->state);
 
                        set_bit(STRIPE_INSYNC, &sh->state);
-                       set_bit(R5_Syncio, &adev->flags);
-                       set_bit(R5_Syncio, &bdev->flags);
                }
        }
+
        if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
                md_done_sync(conf->mddev, STRIPE_SECTORS,1);
                clear_bit(STRIPE_SYNCING, &sh->state);
        }
 
+       /* If the failed drives are just a ReadError, then we might need
+        * to progress the repair/check process
+        */
+       if (failed <= 2 && ! conf->mddev->ro)
+               for (i=0; i<failed;i++) {
+                       dev = &sh->dev[failed_num[i]];
+                       if (test_bit(R5_ReadError, &dev->flags)
+                           && !test_bit(R5_LOCKED, &dev->flags)
+                           && test_bit(R5_UPTODATE, &dev->flags)
+                               ) {
+                               if (!test_bit(R5_ReWrite, &dev->flags)) {
+                                       set_bit(R5_Wantwrite, &dev->flags);
+                                       set_bit(R5_ReWrite, &dev->flags);
+                                       set_bit(R5_LOCKED, &dev->flags);
+                               } else {
+                                       /* let's read it back */
+                                       set_bit(R5_Wantread, &dev->flags);
+                                       set_bit(R5_LOCKED, &dev->flags);
+                               }
+                       }
+               }
        spin_unlock(&sh->lock);
 
        while ((bi=return_bi)) {
@@ -1430,15 +1542,15 @@ static void handle_stripe(struct stripe_head *sh)
                        bi->bi_end_io = raid6_end_read_request;
 
                rcu_read_lock();
-               rdev = conf->disks[i].rdev;
-               if (rdev && rdev->faulty)
+               rdev = rcu_dereference(conf->disks[i].rdev);
+               if (rdev && test_bit(Faulty, &rdev->flags))
                        rdev = NULL;
                if (rdev)
                        atomic_inc(&rdev->nr_pending);
                rcu_read_unlock();
 
                if (rdev) {
-                       if (test_bit(R5_Syncio, &sh->dev[i].flags))
+                       if (syncing)
                                md_sync_acct(rdev->bdev, STRIPE_SECTORS);
 
                        bi->bi_bdev = rdev->bdev;
@@ -1455,8 +1567,13 @@ static void handle_stripe(struct stripe_head *sh)
                        bi->bi_io_vec[0].bv_offset = 0;
                        bi->bi_size = STRIPE_SIZE;
                        bi->bi_next = NULL;
+                       if (rw == WRITE &&
+                           test_bit(R5_ReWrite, &sh->dev[i].flags))
+                               atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
                        generic_make_request(bi);
                } else {
+                       if (rw == 1)
+                               set_bit(STRIPE_DEGRADED, &sh->state);
                        PRINTK("skip op %ld on disc %d for sector %llu\n",
                                bi->bi_rw, i, (unsigned long long)sh->sector);
                        clear_bit(R5_LOCKED, &sh->dev[i].flags);
@@ -1465,7 +1582,7 @@ static void handle_stripe(struct stripe_head *sh)
        }
 }
 
-static inline void raid6_activate_delayed(raid6_conf_t *conf)
+static void raid6_activate_delayed(raid6_conf_t *conf)
 {
        if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
                while (!list_empty(&conf->delayed_list)) {
@@ -1481,6 +1598,20 @@ static inline void raid6_activate_delayed(raid6_conf_t *conf)
        }
 }
 
+static void activate_bit_delay(raid6_conf_t *conf)
+{
+       /* device_lock is held */
+       struct list_head head;
+       list_add(&head, &conf->bitmap_list);
+       list_del_init(&conf->bitmap_list);
+       while (!list_empty(&head)) {
+               struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
+               list_del_init(&sh->lru);
+               atomic_inc(&sh->count);
+               __release_stripe(conf, sh);
+       }
+}
+
 static void unplug_slaves(mddev_t *mddev)
 {
        raid6_conf_t *conf = mddev_to_conf(mddev);
@@ -1488,8 +1619,8 @@ static void unplug_slaves(mddev_t *mddev)
 
        rcu_read_lock();
        for (i=0; i<mddev->raid_disks; i++) {
-               mdk_rdev_t *rdev = conf->disks[i].rdev;
-               if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
+               mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
+               if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
                        request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
 
                        atomic_inc(&rdev->nr_pending);
@@ -1513,8 +1644,10 @@ static void raid6_unplug_device(request_queue_t *q)
 
        spin_lock_irqsave(&conf->device_lock, flags);
 
-       if (blk_remove_plug(q))
+       if (blk_remove_plug(q)) {
+               conf->seq_flush++;
                raid6_activate_delayed(conf);
+       }
        md_wakeup_thread(mddev->thread);
 
        spin_unlock_irqrestore(&conf->device_lock, flags);
@@ -1531,8 +1664,8 @@ static int raid6_issue_flush(request_queue_t *q, struct gendisk *disk,
 
        rcu_read_lock();
        for (i=0; i<mddev->raid_disks && ret == 0; i++) {
-               mdk_rdev_t *rdev = conf->disks[i].rdev;
-               if (rdev && !rdev->faulty) {
+               mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
+               if (rdev && !test_bit(Faulty, &rdev->flags)) {
                        struct block_device *bdev = rdev->bdev;
                        request_queue_t *r_queue = bdev_get_queue(bdev);
 
@@ -1569,22 +1702,24 @@ static int make_request (request_queue_t *q, struct bio * bi)
        sector_t new_sector;
        sector_t logical_sector, last_sector;
        struct stripe_head *sh;
+       const int rw = bio_data_dir(bi);
 
-       if (bio_data_dir(bi)==WRITE) {
-               disk_stat_inc(mddev->gendisk, writes);
-               disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi));
-       } else {
-               disk_stat_inc(mddev->gendisk, reads);
-               disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi));
+       if (unlikely(bio_barrier(bi))) {
+               bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
+               return 0;
        }
 
+       md_write_start(mddev, bi);
+
+       disk_stat_inc(mddev->gendisk, ios[rw]);
+       disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
+
        logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
        last_sector = bi->bi_sector + (bi->bi_size>>9);
 
        bi->bi_next = NULL;
        bi->bi_phys_segments = 1;       /* over-loaded to count active stripes */
-       if ( bio_data_dir(bi) == WRITE )
-               md_write_start(mddev);
+
        for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
                DEFINE_WAIT(w);
 
@@ -1610,7 +1745,7 @@ static int make_request (request_queue_t *q, struct bio * bi)
                        }
                        finish_wait(&conf->wait_for_overlap, &w);
                        raid6_plug_device(conf);
-                       handle_stripe(sh);
+                       handle_stripe(sh, NULL);
                        release_stripe(sh);
                } else {
                        /* cannot get stripe for read-ahead, just give-up */
@@ -1624,7 +1759,7 @@ static int make_request (request_queue_t *q, struct bio * bi)
        if (--bi->bi_phys_segments == 0) {
                int bytes = bi->bi_size;
 
-               if ( bio_data_dir(bi) == WRITE )
+               if (rw == WRITE )
                        md_write_end(mddev);
                bi->bi_size = 0;
                bi->bi_end_io(bi, bytes, 0);
@@ -1634,7 +1769,7 @@ static int make_request (request_queue_t *q, struct bio * bi)
 }
 
 /* FIXME go_faster isn't used */
-static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
+static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
 {
        raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
        struct stripe_head *sh;
@@ -1646,10 +1781,22 @@ static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
        sector_t first_sector;
        int raid_disks = conf->raid_disks;
        int data_disks = raid_disks - 2;
+       sector_t max_sector = mddev->size << 1;
+       int sync_blocks;
+       int still_degraded = 0;
+       int i;
 
-       if (sector_nr >= mddev->size <<1) {
+       if (sector_nr >= max_sector) {
                /* just being told to finish up .. nothing much to do */
                unplug_slaves(mddev);
+
+               if (mddev->curr_resync < max_sector) /* aborted */
+                       bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
+                                       &sync_blocks, 1);
+               else /* completed sync */
+                       conf->fullsync = 0;
+               bitmap_close_sync(mddev->bitmap);
+
                return 0;
        }
        /* if there are 2 or more failed drives and we are trying
@@ -1657,10 +1804,18 @@ static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
         * nothing we can do.
         */
        if (mddev->degraded >= 2 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
-               int rv = (mddev->size << 1) - sector_nr;
-               md_done_sync(mddev, rv, 1);
+               sector_t rv = (mddev->size << 1) - sector_nr;
+               *skipped = 1;
                return rv;
        }
+       if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
+           !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
+           !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
+               /* we can skip this block, and probably more */
+               sync_blocks /= STRIPE_SECTORS;
+               *skipped = 1;
+               return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
+       }
 
        x = sector_nr;
        chunk_offset = sector_div(x, sectors_per_chunk);
@@ -1675,15 +1830,24 @@ static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
                /* make sure we don't swamp the stripe cache if someone else
                 * is trying to get access
                 */
-               set_current_state(TASK_UNINTERRUPTIBLE);
-               schedule_timeout(1);
+               schedule_timeout_uninterruptible(1);
        }
+       /* Need to check if array will still be degraded after recovery/resync
+        * We don't need to check the 'failed' flag as when that gets set,
+        * recovery aborts.
+        */
+       for (i=0; i<mddev->raid_disks; i++)
+               if (conf->disks[i].rdev == NULL)
+                       still_degraded = 1;
+
+       bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
+
        spin_lock(&sh->lock);
        set_bit(STRIPE_SYNCING, &sh->state);
        clear_bit(STRIPE_INSYNC, &sh->state);
        spin_unlock(&sh->lock);
 
-       handle_stripe(sh);
+       handle_stripe(sh, NULL);
        release_stripe(sh);
 
        return STRIPE_SECTORS;
@@ -1705,13 +1869,21 @@ static void raid6d (mddev_t *mddev)
        PRINTK("+++ raid6d active\n");
 
        md_check_recovery(mddev);
-       md_handle_safemode(mddev);
 
        handled = 0;
        spin_lock_irq(&conf->device_lock);
        while (1) {
                struct list_head *first;
 
+               if (conf->seq_flush - conf->seq_write > 0) {
+                       int seq = conf->seq_flush;
+                       spin_unlock_irq(&conf->device_lock);
+                       bitmap_unplug(mddev->bitmap);
+                       spin_lock_irq(&conf->device_lock);
+                       conf->seq_write = seq;
+                       activate_bit_delay(conf);
+               }
+
                if (list_empty(&conf->handle_list) &&
                    atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
                    !blk_queue_plugged(mddev->queue) &&
@@ -1726,12 +1898,11 @@ static void raid6d (mddev_t *mddev)
 
                list_del_init(first);
                atomic_inc(&sh->count);
-               if (atomic_read(&sh->count)!= 1)
-                       BUG();
+               BUG_ON(atomic_read(&sh->count)!= 1);
                spin_unlock_irq(&conf->device_lock);
 
                handled++;
-               handle_stripe(sh);
+               handle_stripe(sh, conf->spare_page);
                release_stripe(sh);
 
                spin_lock_irq(&conf->device_lock);
@@ -1745,7 +1916,75 @@ static void raid6d (mddev_t *mddev)
        PRINTK("--- raid6d inactive\n");
 }
 
-static int run (mddev_t *mddev)
+static ssize_t
+raid6_show_stripe_cache_size(mddev_t *mddev, char *page)
+{
+       raid6_conf_t *conf = mddev_to_conf(mddev);
+       if (conf)
+               return sprintf(page, "%d\n", conf->max_nr_stripes);
+       else
+               return 0;
+}
+
+static ssize_t
+raid6_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
+{
+       raid6_conf_t *conf = mddev_to_conf(mddev);
+       char *end;
+       int new;
+       if (len >= PAGE_SIZE)
+               return -EINVAL;
+       if (!conf)
+               return -ENODEV;
+
+       new = simple_strtoul(page, &end, 10);
+       if (!*page || (*end && *end != '\n') )
+               return -EINVAL;
+       if (new <= 16 || new > 32768)
+               return -EINVAL;
+       while (new < conf->max_nr_stripes) {
+               if (drop_one_stripe(conf))
+                       conf->max_nr_stripes--;
+               else
+                       break;
+       }
+       while (new > conf->max_nr_stripes) {
+               if (grow_one_stripe(conf))
+                       conf->max_nr_stripes++;
+               else break;
+       }
+       return len;
+}
+
+static struct md_sysfs_entry
+raid6_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
+                               raid6_show_stripe_cache_size,
+                               raid6_store_stripe_cache_size);
+
+static ssize_t
+stripe_cache_active_show(mddev_t *mddev, char *page)
+{
+       raid6_conf_t *conf = mddev_to_conf(mddev);
+       if (conf)
+               return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
+       else
+               return 0;
+}
+
+static struct md_sysfs_entry
+raid6_stripecache_active = __ATTR_RO(stripe_cache_active);
+
+static struct attribute *raid6_attrs[] =  {
+       &raid6_stripecache_size.attr,
+       &raid6_stripecache_active.attr,
+       NULL,
+};
+static struct attribute_group raid6_attrs_group = {
+       .name = NULL,
+       .attrs = raid6_attrs,
+};
+
+static int run(mddev_t *mddev)
 {
        raid6_conf_t *conf;
        int raid_disk, memory;
@@ -1758,23 +1997,29 @@ static int run (mddev_t *mddev)
                return -EIO;
        }
 
-       mddev->private = kmalloc (sizeof (raid6_conf_t)
-                                 + mddev->raid_disks * sizeof(struct disk_info),
-                                 GFP_KERNEL);
+       mddev->private = kzalloc(sizeof (raid6_conf_t), GFP_KERNEL);
        if ((conf = mddev->private) == NULL)
                goto abort;
-       memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
+       conf->disks = kzalloc(mddev->raid_disks * sizeof(struct disk_info),
+                                GFP_KERNEL);
+       if (!conf->disks)
+               goto abort;
+
        conf->mddev = mddev;
 
-       if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
+       if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
+               goto abort;
+
+       conf->spare_page = alloc_page(GFP_KERNEL);
+       if (!conf->spare_page)
                goto abort;
-       memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
 
        spin_lock_init(&conf->device_lock);
        init_waitqueue_head(&conf->wait_for_stripe);
        init_waitqueue_head(&conf->wait_for_overlap);
        INIT_LIST_HEAD(&conf->handle_list);
        INIT_LIST_HEAD(&conf->delayed_list);
+       INIT_LIST_HEAD(&conf->bitmap_list);
        INIT_LIST_HEAD(&conf->inactive_list);
        atomic_set(&conf->active_stripes, 0);
        atomic_set(&conf->preread_active_stripes, 0);
@@ -1790,7 +2035,7 @@ static int run (mddev_t *mddev)
 
                disk->rdev = rdev;
 
-               if (rdev->in_sync) {
+               if (test_bit(In_sync, &rdev->flags)) {
                        char b[BDEVNAME_SIZE];
                        printk(KERN_INFO "raid6: device %s operational as raid"
                               " disk %d\n", bdevname(rdev->bdev,b),
@@ -1813,6 +2058,7 @@ static int run (mddev_t *mddev)
 
        /* device size must be a multiple of chunk size */
        mddev->size &= ~(mddev->chunk_size/1024 -1);
+       mddev->resync_max_sectors = mddev->size << 1;
 
        if (conf->raid_disks < 4) {
                printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
@@ -1837,13 +2083,18 @@ static int run (mddev_t *mddev)
                goto abort;
        }
 
-#if 0                          /* FIX: For now */
        if (mddev->degraded > 0 &&
            mddev->recovery_cp != MaxSector) {
-               printk(KERN_ERR "raid6: cannot start dirty degraded array for %s\n", mdname(mddev));
-               goto abort;
+               if (mddev->ok_start_degraded)
+                       printk(KERN_WARNING "raid6: starting dirty degraded array:%s"
+                              "- data corruption possible.\n",
+                              mdname(mddev));
+               else {
+                       printk(KERN_ERR "raid6: cannot start dirty degraded array"
+                              " for %s\n", mdname(mddev));
+                       goto abort;
+               }
        }
-#endif
 
        {
                mddev->thread = md_register_thread(raid6d, mddev, "%s_raid6");
@@ -1885,12 +2136,14 @@ static int run (mddev_t *mddev)
         */
        {
                int stripe = (mddev->raid_disks-2) * mddev->chunk_size
-                       / PAGE_CACHE_SIZE;
+                       / PAGE_SIZE;
                if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
                        mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
        }
 
        /* Ok, everything is just fine now */
+       sysfs_create_group(&mddev->kobj, &raid6_attrs_group);
+
        mddev->array_size =  mddev->size * (mddev->raid_disks - 2);
 
        mddev->queue->unplug_fn = raid6_unplug_device;
@@ -1899,9 +2152,9 @@ static int run (mddev_t *mddev)
 abort:
        if (conf) {
                print_raid6_conf(conf);
-               if (conf->stripe_hashtbl)
-                       free_pages((unsigned long) conf->stripe_hashtbl,
-                                                       HASH_PAGES_ORDER);
+               safe_put_page(conf->spare_page);
+               kfree(conf->stripe_hashtbl);
+               kfree(conf->disks);
                kfree(conf);
        }
        mddev->private = NULL;
@@ -1918,8 +2171,9 @@ static int stop (mddev_t *mddev)
        md_unregister_thread(mddev->thread);
        mddev->thread = NULL;
        shrink_stripes(conf);
-       free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
+       kfree(conf->stripe_hashtbl);
        blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
+       sysfs_remove_group(&mddev->kobj, &raid6_attrs_group);
        kfree(conf);
        mddev->private = NULL;
        return 0;
@@ -1945,12 +2199,13 @@ static void print_sh (struct seq_file *seq, struct stripe_head *sh)
 static void printall (struct seq_file *seq, raid6_conf_t *conf)
 {
        struct stripe_head *sh;
+       struct hlist_node *hn;
        int i;
 
        spin_lock_irq(&conf->device_lock);
        for (i = 0; i < NR_HASH; i++) {
                sh = conf->stripe_hashtbl[i];
-               for (; sh; sh = sh->hash_next) {
+               hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
                        if (sh->raid_conf != conf)
                                continue;
                        print_sh(seq, sh);
@@ -1970,7 +2225,7 @@ static void status (struct seq_file *seq, mddev_t *mddev)
        for (i = 0; i < conf->raid_disks; i++)
                seq_printf (seq, "%s",
                            conf->disks[i].rdev &&
-                           conf->disks[i].rdev->in_sync ? "U" : "_");
+                           test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
        seq_printf (seq, "]");
 #if RAID6_DUMPSTATE
        seq_printf (seq, "\n");
@@ -1996,7 +2251,7 @@ static void print_raid6_conf (raid6_conf_t *conf)
                tmp = conf->disks + i;
                if (tmp->rdev)
                printk(" disk %d, o:%d, dev:%s\n",
-                       i, !tmp->rdev->faulty,
+                       i, !test_bit(Faulty, &tmp->rdev->flags),
                        bdevname(tmp->rdev->bdev,b));
        }
 }
@@ -2010,12 +2265,12 @@ static int raid6_spare_active(mddev_t *mddev)
        for (i = 0; i < conf->raid_disks; i++) {
                tmp = conf->disks + i;
                if (tmp->rdev
-                   && !tmp->rdev->faulty
-                   && !tmp->rdev->in_sync) {
+                   && !test_bit(Faulty, &tmp->rdev->flags)
+                   && !test_bit(In_sync, &tmp->rdev->flags)) {
                        mddev->degraded--;
                        conf->failed_disks--;
                        conf->working_disks++;
-                       tmp->rdev->in_sync = 1;
+                       set_bit(In_sync, &tmp->rdev->flags);
                }
        }
        print_raid6_conf(conf);
@@ -2032,7 +2287,7 @@ static int raid6_remove_disk(mddev_t *mddev, int number)
        print_raid6_conf(conf);
        rdev = p->rdev;
        if (rdev) {
-               if (rdev->in_sync ||
+               if (test_bit(In_sync, &rdev->flags) ||
                    atomic_read(&rdev->nr_pending)) {
                        err = -EBUSY;
                        goto abort;
@@ -2063,14 +2318,22 @@ static int raid6_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
                /* no point adding a device */
                return 0;
        /*
-        * find the disk ...
+        * find the disk ... but prefer rdev->saved_raid_disk
+        * if possible.
         */
-       for (disk=0; disk < mddev->raid_disks; disk++)
+       if (rdev->saved_raid_disk >= 0 &&
+           conf->disks[rdev->saved_raid_disk].rdev == NULL)
+               disk = rdev->saved_raid_disk;
+       else
+               disk = 0;
+       for ( ; disk < mddev->raid_disks; disk++)
                if ((p=conf->disks + disk)->rdev == NULL) {
-                       rdev->in_sync = 0;
+                       clear_bit(In_sync, &rdev->flags);
                        rdev->raid_disk = disk;
                        found = 1;
-                       p->rdev = rdev;
+                       if (rdev->saved_raid_disk != disk)
+                               conf->fullsync = 1;
+                       rcu_assign_pointer(p->rdev, rdev);
                        break;
                }
        print_raid6_conf(conf);
@@ -2095,12 +2358,37 @@ static int raid6_resize(mddev_t *mddev, sector_t sectors)
                set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
        }
        mddev->size = sectors /2;
+       mddev->resync_max_sectors = sectors;
        return 0;
 }
 
-static mdk_personality_t raid6_personality=
+static void raid6_quiesce(mddev_t *mddev, int state)
+{
+       raid6_conf_t *conf = mddev_to_conf(mddev);
+
+       switch(state) {
+       case 1: /* stop all writes */
+               spin_lock_irq(&conf->device_lock);
+               conf->quiesce = 1;
+               wait_event_lock_irq(conf->wait_for_stripe,
+                                   atomic_read(&conf->active_stripes) == 0,
+                                   conf->device_lock, /* nothing */);
+               spin_unlock_irq(&conf->device_lock);
+               break;
+
+       case 0: /* re-enable writes */
+               spin_lock_irq(&conf->device_lock);
+               conf->quiesce = 0;
+               wake_up(&conf->wait_for_stripe);
+               spin_unlock_irq(&conf->device_lock);
+               break;
+       }
+}
+
+static struct mdk_personality raid6_personality =
 {
        .name           = "raid6",
+       .level          = 6,
        .owner          = THIS_MODULE,
        .make_request   = make_request,
        .run            = run,
@@ -2112,9 +2400,10 @@ static mdk_personality_t raid6_personality=
        .spare_active   = raid6_spare_active,
        .sync_request   = sync_request,
        .resize         = raid6_resize,
+       .quiesce        = raid6_quiesce,
 };
 
-static int __init raid6_init (void)
+static int __init raid6_init(void)
 {
        int e;
 
@@ -2122,15 +2411,17 @@ static int __init raid6_init (void)
        if ( e )
                return e;
 
-       return register_md_personality (RAID6, &raid6_personality);
+       return register_md_personality(&raid6_personality);
 }
 
 static void raid6_exit (void)
 {
-       unregister_md_personality (RAID6);
+       unregister_md_personality(&raid6_personality);
 }
 
 module_init(raid6_init);
 module_exit(raid6_exit);
 MODULE_LICENSE("GPL");
 MODULE_ALIAS("md-personality-8"); /* RAID6 */
+MODULE_ALIAS("md-raid6");
+MODULE_ALIAS("md-level-6");