Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/anholt...
[safe/jmp/linux-2.6] / drivers / md / raid5.c
index 52914d5..a5ba080 100644 (file)
  * miss any bits.
  */
 
-#include <linux/module.h>
-#include <linux/slab.h>
-#include <linux/highmem.h>
-#include <linux/bitops.h>
 #include <linux/kthread.h>
-#include <asm/atomic.h>
 #include "raid6.h"
 
 #include <linux/raid/bitmap.h>
+#include <linux/async_tx.h>
 
 /*
  * Stripe cache
@@ -62,6 +58,7 @@
 #define STRIPE_SHIFT           (PAGE_SHIFT - 9)
 #define STRIPE_SECTORS         (STRIPE_SIZE>>9)
 #define        IO_THRESHOLD            1
+#define BYPASS_THRESHOLD       1
 #define NR_HASH                        (PAGE_SIZE / sizeof(struct hlist_head))
 #define HASH_MASK              (NR_HASH - 1)
 
@@ -80,7 +77,6 @@
 /*
  * The following can be used to debug the driver
  */
-#define RAID5_DEBUG    0
 #define RAID5_PARANOIA 1
 #if RAID5_PARANOIA && defined(CONFIG_SMP)
 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
 # define CHECK_DEVLOCK()
 #endif
 
-#define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
-#if RAID5_DEBUG
+#ifdef DEBUG
 #define inline
 #define __inline__
 #endif
 
+#define printk_rl(args...) ((void) (printk_ratelimit() && printk(args)))
+
 #if !RAID6_USE_EMPTY_ZERO_PAGE
 /* In .bss so it's zeroed */
 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
 #endif
 
+/*
+ * We maintain a biased count of active stripes in the bottom 16 bits of
+ * bi_phys_segments, and a count of processed stripes in the upper 16 bits
+ */
+static inline int raid5_bi_phys_segments(struct bio *bio)
+{
+       return bio->bi_phys_segments & 0xffff;
+}
+
+static inline int raid5_bi_hw_segments(struct bio *bio)
+{
+       return (bio->bi_phys_segments >> 16) & 0xffff;
+}
+
+static inline int raid5_dec_bi_phys_segments(struct bio *bio)
+{
+       --bio->bi_phys_segments;
+       return raid5_bi_phys_segments(bio);
+}
+
+static inline int raid5_dec_bi_hw_segments(struct bio *bio)
+{
+       unsigned short val = raid5_bi_hw_segments(bio);
+
+       --val;
+       bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
+       return val;
+}
+
+static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
+{
+       bio->bi_phys_segments = raid5_bi_phys_segments(bio) || (cnt << 16);
+}
+
 static inline int raid6_next_disk(int disk, int raid_disks)
 {
        disk++;
        return (disk < raid_disks) ? disk : 0;
 }
+
+static void return_io(struct bio *return_bi)
+{
+       struct bio *bi = return_bi;
+       while (bi) {
+
+               return_bi = bi->bi_next;
+               bi->bi_next = NULL;
+               bi->bi_size = 0;
+               bio_endio(bi, 0);
+               bi = return_bi;
+       }
+}
+
 static void print_raid5_conf (raid5_conf_t *conf);
 
+static int stripe_operations_active(struct stripe_head *sh)
+{
+       return sh->check_state || sh->reconstruct_state ||
+              test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
+              test_bit(STRIPE_COMPUTE_RUN, &sh->state);
+}
+
 static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
 {
        if (atomic_dec_and_test(&sh->count)) {
@@ -125,6 +177,7 @@ static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
                        }
                        md_wakeup_thread(conf->mddev->thread);
                } else {
+                       BUG_ON(stripe_operations_active(sh));
                        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)
@@ -134,6 +187,8 @@ static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
                        if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
                                list_add_tail(&sh->lru, &conf->inactive_list);
                                wake_up(&conf->wait_for_stripe);
+                               if (conf->retry_read_aligned)
+                                       md_wakeup_thread(conf->mddev->thread);
                        }
                }
        }
@@ -150,7 +205,8 @@ static void release_stripe(struct stripe_head *sh)
 
 static inline void remove_hash(struct stripe_head *sh)
 {
-       PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
+       pr_debug("remove_hash(), stripe %llu\n",
+               (unsigned long long)sh->sector);
 
        hlist_del_init(&sh->hash);
 }
@@ -159,7 +215,8 @@ static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
 {
        struct hlist_head *hp = stripe_hash(conf, sh->sector);
 
-       PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
+       pr_debug("insert_hash(), stripe %llu\n",
+               (unsigned long long)sh->sector);
 
        CHECK_DEVLOCK();
        hlist_add_head(&sh->hash, hp);
@@ -213,7 +270,7 @@ static int grow_buffers(struct stripe_head *sh, int num)
        return 0;
 }
 
-static void raid5_build_block (struct stripe_head *sh, int i);
+static void raid5_build_block(struct stripe_head *sh, int i);
 
 static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
 {
@@ -222,9 +279,10 @@ static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int
 
        BUG_ON(atomic_read(&sh->count) != 0);
        BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
-       
+       BUG_ON(stripe_operations_active(sh));
+
        CHECK_DEVLOCK();
-       PRINTK("init_stripe called, stripe %llu\n", 
+       pr_debug("init_stripe called, stripe %llu\n",
                (unsigned long long)sh->sector);
 
        remove_hash(sh);
@@ -238,11 +296,11 @@ static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int
        for (i = sh->disks; i--; ) {
                struct r5dev *dev = &sh->dev[i];
 
-               if (dev->toread || dev->towrite || dev->written ||
+               if (dev->toread || dev->read || dev->towrite || dev->written ||
                    test_bit(R5_LOCKED, &dev->flags)) {
-                       printk("sector=%llx i=%d %p %p %p %d\n",
+                       printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
                               (unsigned long long)sh->sector, i, dev->toread,
-                              dev->towrite, dev->written,
+                              dev->read, dev->towrite, dev->written,
                               test_bit(R5_LOCKED, &dev->flags));
                        BUG();
                }
@@ -258,23 +316,23 @@ static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, in
        struct hlist_node *hn;
 
        CHECK_DEVLOCK();
-       PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
+       pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
        hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
                if (sh->sector == sector && sh->disks == disks)
                        return sh;
-       PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
+       pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
        return NULL;
 }
 
 static void unplug_slaves(mddev_t *mddev);
-static void raid5_unplug_device(request_queue_t *q);
+static void raid5_unplug_device(struct request_queue *q);
 
 static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
                                             int pd_idx, int noblock)
 {
        struct stripe_head *sh;
 
-       PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
+       pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
 
        spin_lock_irq(&conf->device_lock);
 
@@ -322,177 +380,667 @@ static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector
        return sh;
 }
 
-static int grow_one_stripe(raid5_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;
-       }
-       sh->disks = conf->raid_disks;
-       /* 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 void
+raid5_end_read_request(struct bio *bi, int error);
+static void
+raid5_end_write_request(struct bio *bi, int error);
 
-static int grow_stripes(raid5_conf_t *conf, int num)
+static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
 {
-       struct kmem_cache *sc;
-       int devs = conf->raid_disks;
+       raid5_conf_t *conf = sh->raid_conf;
+       int i, disks = sh->disks;
 
-       sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
-       sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
-       conf->active_name = 0;
-       sc = kmem_cache_create(conf->cache_name[conf->active_name],
-                              sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
-                              0, 0, NULL, NULL);
-       if (!sc)
-               return 1;
-       conf->slab_cache = sc;
-       conf->pool_size = devs;
-       while (num--)
-               if (!grow_one_stripe(conf))
-                       return 1;
-       return 0;
-}
+       might_sleep();
 
-#ifdef CONFIG_MD_RAID5_RESHAPE
-static int resize_stripes(raid5_conf_t *conf, int newsize)
-{
-       /* Make all the stripes able to hold 'newsize' devices.
-        * New slots in each stripe get 'page' set to a new page.
-        *
-        * This happens in stages:
-        * 1/ create a new kmem_cache and allocate the required number of
-        *    stripe_heads.
-        * 2/ gather all the old stripe_heads and tranfer the pages across
-        *    to the new stripe_heads.  This will have the side effect of
-        *    freezing the array as once all stripe_heads have been collected,
-        *    no IO will be possible.  Old stripe heads are freed once their
-        *    pages have been transferred over, and the old kmem_cache is
-        *    freed when all stripes are done.
-        * 3/ reallocate conf->disks to be suitable bigger.  If this fails,
-        *    we simple return a failre status - no need to clean anything up.
-        * 4/ allocate new pages for the new slots in the new stripe_heads.
-        *    If this fails, we don't bother trying the shrink the
-        *    stripe_heads down again, we just leave them as they are.
-        *    As each stripe_head is processed the new one is released into
-        *    active service.
-        *
-        * Once step2 is started, we cannot afford to wait for a write,
-        * so we use GFP_NOIO allocations.
-        */
-       struct stripe_head *osh, *nsh;
-       LIST_HEAD(newstripes);
-       struct disk_info *ndisks;
-       int err = 0;
-       struct kmem_cache *sc;
-       int i;
+       for (i = disks; i--; ) {
+               int rw;
+               struct bio *bi;
+               mdk_rdev_t *rdev;
+               if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
+                       rw = WRITE;
+               else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
+                       rw = READ;
+               else
+                       continue;
 
-       if (newsize <= conf->pool_size)
-               return 0; /* never bother to shrink */
+               bi = &sh->dev[i].req;
 
-       /* Step 1 */
-       sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
-                              sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
-                              0, 0, NULL, NULL);
-       if (!sc)
-               return -ENOMEM;
+               bi->bi_rw = rw;
+               if (rw == WRITE)
+                       bi->bi_end_io = raid5_end_write_request;
+               else
+                       bi->bi_end_io = raid5_end_read_request;
 
-       for (i = conf->max_nr_stripes; i; i--) {
-               nsh = kmem_cache_alloc(sc, GFP_KERNEL);
-               if (!nsh)
-                       break;
+               rcu_read_lock();
+               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();
 
-               memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
+               if (rdev) {
+                       if (s->syncing || s->expanding || s->expanded)
+                               md_sync_acct(rdev->bdev, STRIPE_SECTORS);
 
-               nsh->raid_conf = conf;
-               spin_lock_init(&nsh->lock);
+                       set_bit(STRIPE_IO_STARTED, &sh->state);
 
-               list_add(&nsh->lru, &newstripes);
-       }
-       if (i) {
-               /* didn't get enough, give up */
-               while (!list_empty(&newstripes)) {
-                       nsh = list_entry(newstripes.next, struct stripe_head, lru);
-                       list_del(&nsh->lru);
-                       kmem_cache_free(sc, nsh);
+                       bi->bi_bdev = rdev->bdev;
+                       pr_debug("%s: for %llu schedule op %ld on disc %d\n",
+                               __func__, (unsigned long long)sh->sector,
+                               bi->bi_rw, i);
+                       atomic_inc(&sh->count);
+                       bi->bi_sector = sh->sector + rdev->data_offset;
+                       bi->bi_flags = 1 << BIO_UPTODATE;
+                       bi->bi_vcnt = 1;
+                       bi->bi_max_vecs = 1;
+                       bi->bi_idx = 0;
+                       bi->bi_io_vec = &sh->dev[i].vec;
+                       bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
+                       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 == WRITE)
+                               set_bit(STRIPE_DEGRADED, &sh->state);
+                       pr_debug("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);
+                       set_bit(STRIPE_HANDLE, &sh->state);
                }
-               kmem_cache_destroy(sc);
-               return -ENOMEM;
-       }
-       /* Step 2 - Must use GFP_NOIO now.
-        * OK, we have enough stripes, start collecting inactive
-        * stripes and copying them over
-        */
-       list_for_each_entry(nsh, &newstripes, lru) {
-               spin_lock_irq(&conf->device_lock);
-               wait_event_lock_irq(conf->wait_for_stripe,
-                                   !list_empty(&conf->inactive_list),
-                                   conf->device_lock,
-                                   unplug_slaves(conf->mddev)
-                       );
-               osh = get_free_stripe(conf);
-               spin_unlock_irq(&conf->device_lock);
-               atomic_set(&nsh->count, 1);
-               for(i=0; i<conf->pool_size; i++)
-                       nsh->dev[i].page = osh->dev[i].page;
-               for( ; i<newsize; i++)
-                       nsh->dev[i].page = NULL;
-               kmem_cache_free(conf->slab_cache, osh);
        }
-       kmem_cache_destroy(conf->slab_cache);
+}
 
-       /* Step 3.
-        * At this point, we are holding all the stripes so the array
-        * is completely stalled, so now is a good time to resize
-        * conf->disks.
-        */
-       ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
-       if (ndisks) {
-               for (i=0; i<conf->raid_disks; i++)
-                       ndisks[i] = conf->disks[i];
-               kfree(conf->disks);
-               conf->disks = ndisks;
-       } else
-               err = -ENOMEM;
+static struct dma_async_tx_descriptor *
+async_copy_data(int frombio, struct bio *bio, struct page *page,
+       sector_t sector, struct dma_async_tx_descriptor *tx)
+{
+       struct bio_vec *bvl;
+       struct page *bio_page;
+       int i;
+       int page_offset;
 
-       /* Step 4, return new stripes to service */
-       while(!list_empty(&newstripes)) {
-               nsh = list_entry(newstripes.next, struct stripe_head, lru);
-               list_del_init(&nsh->lru);
-               for (i=conf->raid_disks; i < newsize; i++)
-                       if (nsh->dev[i].page == NULL) {
-                               struct page *p = alloc_page(GFP_NOIO);
-                               nsh->dev[i].page = p;
-                               if (!p)
-                                       err = -ENOMEM;
-                       }
-               release_stripe(nsh);
+       if (bio->bi_sector >= sector)
+               page_offset = (signed)(bio->bi_sector - sector) * 512;
+       else
+               page_offset = (signed)(sector - bio->bi_sector) * -512;
+       bio_for_each_segment(bvl, bio, i) {
+               int len = bio_iovec_idx(bio, i)->bv_len;
+               int clen;
+               int b_offset = 0;
+
+               if (page_offset < 0) {
+                       b_offset = -page_offset;
+                       page_offset += b_offset;
+                       len -= b_offset;
+               }
+
+               if (len > 0 && page_offset + len > STRIPE_SIZE)
+                       clen = STRIPE_SIZE - page_offset;
+               else
+                       clen = len;
+
+               if (clen > 0) {
+                       b_offset += bio_iovec_idx(bio, i)->bv_offset;
+                       bio_page = bio_iovec_idx(bio, i)->bv_page;
+                       if (frombio)
+                               tx = async_memcpy(page, bio_page, page_offset,
+                                       b_offset, clen,
+                                       ASYNC_TX_DEP_ACK,
+                                       tx, NULL, NULL);
+                       else
+                               tx = async_memcpy(bio_page, page, b_offset,
+                                       page_offset, clen,
+                                       ASYNC_TX_DEP_ACK,
+                                       tx, NULL, NULL);
+               }
+               if (clen < len) /* hit end of page */
+                       break;
+               page_offset +=  len;
        }
-       /* critical section pass, GFP_NOIO no longer needed */
 
-       conf->slab_cache = sc;
-       conf->active_name = 1-conf->active_name;
-       conf->pool_size = newsize;
-       return err;
+       return tx;
 }
-#endif
 
-static int drop_one_stripe(raid5_conf_t *conf)
+static void ops_complete_biofill(void *stripe_head_ref)
 {
-       struct stripe_head *sh;
+       struct stripe_head *sh = stripe_head_ref;
+       struct bio *return_bi = NULL;
+       raid5_conf_t *conf = sh->raid_conf;
+       int i;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       /* clear completed biofills */
+       spin_lock_irq(&conf->device_lock);
+       for (i = sh->disks; i--; ) {
+               struct r5dev *dev = &sh->dev[i];
+
+               /* acknowledge completion of a biofill operation */
+               /* and check if we need to reply to a read request,
+                * new R5_Wantfill requests are held off until
+                * !STRIPE_BIOFILL_RUN
+                */
+               if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
+                       struct bio *rbi, *rbi2;
+
+                       BUG_ON(!dev->read);
+                       rbi = dev->read;
+                       dev->read = NULL;
+                       while (rbi && rbi->bi_sector <
+                               dev->sector + STRIPE_SECTORS) {
+                               rbi2 = r5_next_bio(rbi, dev->sector);
+                               if (!raid5_dec_bi_phys_segments(rbi)) {
+                                       rbi->bi_next = return_bi;
+                                       return_bi = rbi;
+                               }
+                               rbi = rbi2;
+                       }
+               }
+       }
+       spin_unlock_irq(&conf->device_lock);
+       clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
+
+       return_io(return_bi);
+
+       set_bit(STRIPE_HANDLE, &sh->state);
+       release_stripe(sh);
+}
+
+static void ops_run_biofill(struct stripe_head *sh)
+{
+       struct dma_async_tx_descriptor *tx = NULL;
+       raid5_conf_t *conf = sh->raid_conf;
+       int i;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       for (i = sh->disks; i--; ) {
+               struct r5dev *dev = &sh->dev[i];
+               if (test_bit(R5_Wantfill, &dev->flags)) {
+                       struct bio *rbi;
+                       spin_lock_irq(&conf->device_lock);
+                       dev->read = rbi = dev->toread;
+                       dev->toread = NULL;
+                       spin_unlock_irq(&conf->device_lock);
+                       while (rbi && rbi->bi_sector <
+                               dev->sector + STRIPE_SECTORS) {
+                               tx = async_copy_data(0, rbi, dev->page,
+                                       dev->sector, tx);
+                               rbi = r5_next_bio(rbi, dev->sector);
+                       }
+               }
+       }
+
+       atomic_inc(&sh->count);
+       async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
+               ops_complete_biofill, sh);
+}
+
+static void ops_complete_compute5(void *stripe_head_ref)
+{
+       struct stripe_head *sh = stripe_head_ref;
+       int target = sh->ops.target;
+       struct r5dev *tgt = &sh->dev[target];
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       set_bit(R5_UPTODATE, &tgt->flags);
+       BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
+       clear_bit(R5_Wantcompute, &tgt->flags);
+       clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
+       if (sh->check_state == check_state_compute_run)
+               sh->check_state = check_state_compute_result;
+       set_bit(STRIPE_HANDLE, &sh->state);
+       release_stripe(sh);
+}
+
+static struct dma_async_tx_descriptor *ops_run_compute5(struct stripe_head *sh)
+{
+       /* kernel stack size limits the total number of disks */
+       int disks = sh->disks;
+       struct page *xor_srcs[disks];
+       int target = sh->ops.target;
+       struct r5dev *tgt = &sh->dev[target];
+       struct page *xor_dest = tgt->page;
+       int count = 0;
+       struct dma_async_tx_descriptor *tx;
+       int i;
+
+       pr_debug("%s: stripe %llu block: %d\n",
+               __func__, (unsigned long long)sh->sector, target);
+       BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
+
+       for (i = disks; i--; )
+               if (i != target)
+                       xor_srcs[count++] = sh->dev[i].page;
+
+       atomic_inc(&sh->count);
+
+       if (unlikely(count == 1))
+               tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
+                       0, NULL, ops_complete_compute5, sh);
+       else
+               tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
+                       ASYNC_TX_XOR_ZERO_DST, NULL,
+                       ops_complete_compute5, sh);
+
+       return tx;
+}
+
+static void ops_complete_prexor(void *stripe_head_ref)
+{
+       struct stripe_head *sh = stripe_head_ref;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+}
+
+static struct dma_async_tx_descriptor *
+ops_run_prexor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
+{
+       /* kernel stack size limits the total number of disks */
+       int disks = sh->disks;
+       struct page *xor_srcs[disks];
+       int count = 0, pd_idx = sh->pd_idx, i;
+
+       /* existing parity data subtracted */
+       struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       for (i = disks; i--; ) {
+               struct r5dev *dev = &sh->dev[i];
+               /* Only process blocks that are known to be uptodate */
+               if (test_bit(R5_Wantdrain, &dev->flags))
+                       xor_srcs[count++] = dev->page;
+       }
+
+       tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
+               ASYNC_TX_DEP_ACK | ASYNC_TX_XOR_DROP_DST, tx,
+               ops_complete_prexor, sh);
+
+       return tx;
+}
+
+static struct dma_async_tx_descriptor *
+ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
+{
+       int disks = sh->disks;
+       int i;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       for (i = disks; i--; ) {
+               struct r5dev *dev = &sh->dev[i];
+               struct bio *chosen;
+
+               if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
+                       struct bio *wbi;
+
+                       spin_lock(&sh->lock);
+                       chosen = dev->towrite;
+                       dev->towrite = NULL;
+                       BUG_ON(dev->written);
+                       wbi = dev->written = chosen;
+                       spin_unlock(&sh->lock);
+
+                       while (wbi && wbi->bi_sector <
+                               dev->sector + STRIPE_SECTORS) {
+                               tx = async_copy_data(1, wbi, dev->page,
+                                       dev->sector, tx);
+                               wbi = r5_next_bio(wbi, dev->sector);
+                       }
+               }
+       }
+
+       return tx;
+}
+
+static void ops_complete_postxor(void *stripe_head_ref)
+{
+       struct stripe_head *sh = stripe_head_ref;
+       int disks = sh->disks, i, pd_idx = sh->pd_idx;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       for (i = disks; i--; ) {
+               struct r5dev *dev = &sh->dev[i];
+               if (dev->written || i == pd_idx)
+                       set_bit(R5_UPTODATE, &dev->flags);
+       }
+
+       if (sh->reconstruct_state == reconstruct_state_drain_run)
+               sh->reconstruct_state = reconstruct_state_drain_result;
+       else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
+               sh->reconstruct_state = reconstruct_state_prexor_drain_result;
+       else {
+               BUG_ON(sh->reconstruct_state != reconstruct_state_run);
+               sh->reconstruct_state = reconstruct_state_result;
+       }
+
+       set_bit(STRIPE_HANDLE, &sh->state);
+       release_stripe(sh);
+}
+
+static void
+ops_run_postxor(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
+{
+       /* kernel stack size limits the total number of disks */
+       int disks = sh->disks;
+       struct page *xor_srcs[disks];
+
+       int count = 0, pd_idx = sh->pd_idx, i;
+       struct page *xor_dest;
+       int prexor = 0;
+       unsigned long flags;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       /* check if prexor is active which means only process blocks
+        * that are part of a read-modify-write (written)
+        */
+       if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
+               prexor = 1;
+               xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
+               for (i = disks; i--; ) {
+                       struct r5dev *dev = &sh->dev[i];
+                       if (dev->written)
+                               xor_srcs[count++] = dev->page;
+               }
+       } else {
+               xor_dest = sh->dev[pd_idx].page;
+               for (i = disks; i--; ) {
+                       struct r5dev *dev = &sh->dev[i];
+                       if (i != pd_idx)
+                               xor_srcs[count++] = dev->page;
+               }
+       }
+
+       /* 1/ if we prexor'd then the dest is reused as a source
+        * 2/ if we did not prexor then we are redoing the parity
+        * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
+        * for the synchronous xor case
+        */
+       flags = ASYNC_TX_DEP_ACK | ASYNC_TX_ACK |
+               (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
+
+       atomic_inc(&sh->count);
+
+       if (unlikely(count == 1)) {
+               flags &= ~(ASYNC_TX_XOR_DROP_DST | ASYNC_TX_XOR_ZERO_DST);
+               tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE,
+                       flags, tx, ops_complete_postxor, sh);
+       } else
+               tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
+                       flags, tx, ops_complete_postxor, sh);
+}
+
+static void ops_complete_check(void *stripe_head_ref)
+{
+       struct stripe_head *sh = stripe_head_ref;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       sh->check_state = check_state_check_result;
+       set_bit(STRIPE_HANDLE, &sh->state);
+       release_stripe(sh);
+}
+
+static void ops_run_check(struct stripe_head *sh)
+{
+       /* kernel stack size limits the total number of disks */
+       int disks = sh->disks;
+       struct page *xor_srcs[disks];
+       struct dma_async_tx_descriptor *tx;
+
+       int count = 0, pd_idx = sh->pd_idx, i;
+       struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
+
+       pr_debug("%s: stripe %llu\n", __func__,
+               (unsigned long long)sh->sector);
+
+       for (i = disks; i--; ) {
+               struct r5dev *dev = &sh->dev[i];
+               if (i != pd_idx)
+                       xor_srcs[count++] = dev->page;
+       }
+
+       tx = async_xor_zero_sum(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
+               &sh->ops.zero_sum_result, 0, NULL, NULL, NULL);
+
+       atomic_inc(&sh->count);
+       tx = async_trigger_callback(ASYNC_TX_DEP_ACK | ASYNC_TX_ACK, tx,
+               ops_complete_check, sh);
+}
+
+static void raid5_run_ops(struct stripe_head *sh, unsigned long ops_request)
+{
+       int overlap_clear = 0, i, disks = sh->disks;
+       struct dma_async_tx_descriptor *tx = NULL;
+
+       if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
+               ops_run_biofill(sh);
+               overlap_clear++;
+       }
+
+       if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
+               tx = ops_run_compute5(sh);
+               /* terminate the chain if postxor is not set to be run */
+               if (tx && !test_bit(STRIPE_OP_POSTXOR, &ops_request))
+                       async_tx_ack(tx);
+       }
+
+       if (test_bit(STRIPE_OP_PREXOR, &ops_request))
+               tx = ops_run_prexor(sh, tx);
+
+       if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
+               tx = ops_run_biodrain(sh, tx);
+               overlap_clear++;
+       }
+
+       if (test_bit(STRIPE_OP_POSTXOR, &ops_request))
+               ops_run_postxor(sh, tx);
+
+       if (test_bit(STRIPE_OP_CHECK, &ops_request))
+               ops_run_check(sh);
+
+       if (overlap_clear)
+               for (i = disks; i--; ) {
+                       struct r5dev *dev = &sh->dev[i];
+                       if (test_and_clear_bit(R5_Overlap, &dev->flags))
+                               wake_up(&sh->raid_conf->wait_for_overlap);
+               }
+}
+
+static int grow_one_stripe(raid5_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;
+       }
+       sh->disks = conf->raid_disks;
+       /* 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(raid5_conf_t *conf, int num)
+{
+       struct kmem_cache *sc;
+       int devs = conf->raid_disks;
+
+       sprintf(conf->cache_name[0], "raid5-%s", mdname(conf->mddev));
+       sprintf(conf->cache_name[1], "raid5-%s-alt", mdname(conf->mddev));
+       conf->active_name = 0;
+       sc = kmem_cache_create(conf->cache_name[conf->active_name],
+                              sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
+                              0, 0, NULL);
+       if (!sc)
+               return 1;
+       conf->slab_cache = sc;
+       conf->pool_size = devs;
+       while (num--)
+               if (!grow_one_stripe(conf))
+                       return 1;
+       return 0;
+}
+
+#ifdef CONFIG_MD_RAID5_RESHAPE
+static int resize_stripes(raid5_conf_t *conf, int newsize)
+{
+       /* Make all the stripes able to hold 'newsize' devices.
+        * New slots in each stripe get 'page' set to a new page.
+        *
+        * This happens in stages:
+        * 1/ create a new kmem_cache and allocate the required number of
+        *    stripe_heads.
+        * 2/ gather all the old stripe_heads and tranfer the pages across
+        *    to the new stripe_heads.  This will have the side effect of
+        *    freezing the array as once all stripe_heads have been collected,
+        *    no IO will be possible.  Old stripe heads are freed once their
+        *    pages have been transferred over, and the old kmem_cache is
+        *    freed when all stripes are done.
+        * 3/ reallocate conf->disks to be suitable bigger.  If this fails,
+        *    we simple return a failre status - no need to clean anything up.
+        * 4/ allocate new pages for the new slots in the new stripe_heads.
+        *    If this fails, we don't bother trying the shrink the
+        *    stripe_heads down again, we just leave them as they are.
+        *    As each stripe_head is processed the new one is released into
+        *    active service.
+        *
+        * Once step2 is started, we cannot afford to wait for a write,
+        * so we use GFP_NOIO allocations.
+        */
+       struct stripe_head *osh, *nsh;
+       LIST_HEAD(newstripes);
+       struct disk_info *ndisks;
+       int err;
+       struct kmem_cache *sc;
+       int i;
+
+       if (newsize <= conf->pool_size)
+               return 0; /* never bother to shrink */
+
+       err = md_allow_write(conf->mddev);
+       if (err)
+               return err;
+
+       /* Step 1 */
+       sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
+                              sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
+                              0, 0, NULL);
+       if (!sc)
+               return -ENOMEM;
+
+       for (i = conf->max_nr_stripes; i; i--) {
+               nsh = kmem_cache_alloc(sc, GFP_KERNEL);
+               if (!nsh)
+                       break;
+
+               memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
+
+               nsh->raid_conf = conf;
+               spin_lock_init(&nsh->lock);
+
+               list_add(&nsh->lru, &newstripes);
+       }
+       if (i) {
+               /* didn't get enough, give up */
+               while (!list_empty(&newstripes)) {
+                       nsh = list_entry(newstripes.next, struct stripe_head, lru);
+                       list_del(&nsh->lru);
+                       kmem_cache_free(sc, nsh);
+               }
+               kmem_cache_destroy(sc);
+               return -ENOMEM;
+       }
+       /* Step 2 - Must use GFP_NOIO now.
+        * OK, we have enough stripes, start collecting inactive
+        * stripes and copying them over
+        */
+       list_for_each_entry(nsh, &newstripes, lru) {
+               spin_lock_irq(&conf->device_lock);
+               wait_event_lock_irq(conf->wait_for_stripe,
+                                   !list_empty(&conf->inactive_list),
+                                   conf->device_lock,
+                                   unplug_slaves(conf->mddev)
+                       );
+               osh = get_free_stripe(conf);
+               spin_unlock_irq(&conf->device_lock);
+               atomic_set(&nsh->count, 1);
+               for(i=0; i<conf->pool_size; i++)
+                       nsh->dev[i].page = osh->dev[i].page;
+               for( ; i<newsize; i++)
+                       nsh->dev[i].page = NULL;
+               kmem_cache_free(conf->slab_cache, osh);
+       }
+       kmem_cache_destroy(conf->slab_cache);
+
+       /* Step 3.
+        * At this point, we are holding all the stripes so the array
+        * is completely stalled, so now is a good time to resize
+        * conf->disks.
+        */
+       ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
+       if (ndisks) {
+               for (i=0; i<conf->raid_disks; i++)
+                       ndisks[i] = conf->disks[i];
+               kfree(conf->disks);
+               conf->disks = ndisks;
+       } else
+               err = -ENOMEM;
+
+       /* Step 4, return new stripes to service */
+       while(!list_empty(&newstripes)) {
+               nsh = list_entry(newstripes.next, struct stripe_head, lru);
+               list_del_init(&nsh->lru);
+               for (i=conf->raid_disks; i < newsize; i++)
+                       if (nsh->dev[i].page == NULL) {
+                               struct page *p = alloc_page(GFP_NOIO);
+                               nsh->dev[i].page = p;
+                               if (!p)
+                                       err = -ENOMEM;
+                       }
+               release_stripe(nsh);
+       }
+       /* critical section pass, GFP_NOIO no longer needed */
+
+       conf->slab_cache = sc;
+       conf->active_name = 1-conf->active_name;
+       conf->pool_size = newsize;
+       return err;
+}
+#endif
+
+static int drop_one_stripe(raid5_conf_t *conf)
+{
+       struct stripe_head *sh;
 
        spin_lock_irq(&conf->device_lock);
        sh = get_free_stripe(conf);
@@ -516,8 +1064,7 @@ static void shrink_stripes(raid5_conf_t *conf)
        conf->slab_cache = NULL;
 }
 
-static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
-                                  int error)
+static void raid5_end_read_request(struct bio * bi, int error)
 {
        struct stripe_head *sh = bi->bi_private;
        raid5_conf_t *conf = sh->raid_conf;
@@ -526,57 +1073,29 @@ static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
        char b[BDEVNAME_SIZE];
        mdk_rdev_t *rdev;
 
-       if (bi->bi_size)
-               return 1;
 
        for (i=0 ; i<disks; i++)
                if (bi == &sh->dev[i].req)
                        break;
 
-       PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n", 
-               (unsigned long long)sh->sector, i, atomic_read(&sh->count), 
+       pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
+               (unsigned long long)sh->sector, i, atomic_read(&sh->count),
                uptodate);
        if (i == disks) {
                BUG();
-               return 0;
+               return;
        }
 
        if (uptodate) {
-#if 0
-               struct bio *bio;
-               unsigned long flags;
-               spin_lock_irqsave(&conf->device_lock, flags);
-               /* we can return a buffer if we bypassed the cache or
-                * if the top buffer is not in highmem.  If there are
-                * multiple buffers, leave the extra work to
-                * handle_stripe
-                */
-               buffer = sh->bh_read[i];
-               if (buffer &&
-                   (!PageHighMem(buffer->b_page)
-                    || buffer->b_page == bh->b_page )
-                       ) {
-                       sh->bh_read[i] = buffer->b_reqnext;
-                       buffer->b_reqnext = NULL;
-               } else
-                       buffer = NULL;
-               spin_unlock_irqrestore(&conf->device_lock, flags);
-               if (sh->bh_page[i]==bh->b_page)
-                       set_buffer_uptodate(bh);
-               if (buffer) {
-                       if (buffer->b_page != bh->b_page)
-                               memcpy(buffer->b_data, bh->b_data, bh->b_size);
-                       buffer->b_end_io(buffer, 1);
-               }
-#else
                set_bit(R5_UPTODATE, &sh->dev[i].flags);
-#endif
                if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
                        rdev = conf->disks[i].rdev;
-                       printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
-                              mdname(conf->mddev), STRIPE_SECTORS,
-                              (unsigned long long)sh->sector + rdev->data_offset,
-                              bdevname(rdev->bdev, b));
+                       printk_rl(KERN_INFO "raid5:%s: read error corrected"
+                                 " (%lu sectors at %llu on %s)\n",
+                                 mdname(conf->mddev), STRIPE_SECTORS,
+                                 (unsigned long long)(sh->sector
+                                                      + rdev->data_offset),
+                                 bdevname(rdev->bdev, b));
                        clear_bit(R5_ReadError, &sh->dev[i].flags);
                        clear_bit(R5_ReWrite, &sh->dev[i].flags);
                }
@@ -590,16 +1109,22 @@ static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
                clear_bit(R5_UPTODATE, &sh->dev[i].flags);
                atomic_inc(&rdev->read_errors);
                if (conf->mddev->degraded)
-                       printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
-                              mdname(conf->mddev),
-                              (unsigned long long)sh->sector + rdev->data_offset,
-                              bdn);
+                       printk_rl(KERN_WARNING
+                                 "raid5:%s: read error not correctable "
+                                 "(sector %llu on %s).\n",
+                                 mdname(conf->mddev),
+                                 (unsigned long long)(sh->sector
+                                                      + rdev->data_offset),
+                                 bdn);
                else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
                        /* Oh, no!!! */
-                       printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
-                              mdname(conf->mddev),
-                              (unsigned long long)sh->sector + rdev->data_offset,
-                              bdn);
+                       printk_rl(KERN_WARNING
+                                 "raid5:%s: read error NOT corrected!! "
+                                 "(sector %llu on %s).\n",
+                                 mdname(conf->mddev),
+                                 (unsigned long long)(sh->sector
+                                                      + rdev->data_offset),
+                                 bdn);
                else if (atomic_read(&rdev->read_errors)
                         > conf->max_nr_stripes)
                        printk(KERN_WARNING
@@ -616,41 +1141,28 @@ static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
                }
        }
        rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
-#if 0
-       /* must restore b_page before unlocking buffer... */
-       if (sh->bh_page[i] != bh->b_page) {
-               bh->b_page = sh->bh_page[i];
-               bh->b_data = page_address(bh->b_page);
-               clear_buffer_uptodate(bh);
-       }
-#endif
        clear_bit(R5_LOCKED, &sh->dev[i].flags);
        set_bit(STRIPE_HANDLE, &sh->state);
        release_stripe(sh);
-       return 0;
 }
 
-static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
-                                   int error)
+static void raid5_end_write_request(struct bio *bi, int error)
 {
        struct stripe_head *sh = bi->bi_private;
        raid5_conf_t *conf = sh->raid_conf;
        int disks = sh->disks, i;
        int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
 
-       if (bi->bi_size)
-               return 1;
-
        for (i=0 ; i<disks; i++)
                if (bi == &sh->dev[i].req)
                        break;
 
-       PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n", 
+       pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
                (unsigned long long)sh->sector, i, atomic_read(&sh->count),
                uptodate);
        if (i == disks) {
                BUG();
-               return 0;
+               return;
        }
 
        if (!uptodate)
@@ -661,13 +1173,12 @@ static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
        clear_bit(R5_LOCKED, &sh->dev[i].flags);
        set_bit(STRIPE_HANDLE, &sh->state);
        release_stripe(sh);
-       return 0;
 }
 
 
 static sector_t compute_blocknr(struct stripe_head *sh, int i);
        
-static void raid5_build_block (struct stripe_head *sh, int i)
+static void raid5_build_block(struct stripe_head *sh, int i)
 {
        struct r5dev *dev = &sh->dev[i];
 
@@ -690,7 +1201,7 @@ static void error(mddev_t *mddev, mdk_rdev_t *rdev)
 {
        char b[BDEVNAME_SIZE];
        raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
-       PRINTK("raid5: error called\n");
+       pr_debug("raid5: error called\n");
 
        if (!test_bit(Faulty, &rdev->flags)) {
                set_bit(MD_CHANGE_DEVS, &mddev->flags);
@@ -702,13 +1213,13 @@ static void error(mddev_t *mddev, mdk_rdev_t *rdev)
                        /*
                         * if recovery was running, make sure it aborts.
                         */
-                       set_bit(MD_RECOVERY_ERR, &mddev->recovery);
+                       set_bit(MD_RECOVERY_INTR, &mddev->recovery);
                }
                set_bit(Faulty, &rdev->flags);
-               printk (KERN_ALERT
-                       "raid5: Disk failure on %s, disabling device."
-                       " Operation continuing on %d devices\n",
-                       bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
+               printk(KERN_ALERT
+                      "raid5: Disk failure on %s, disabling device.\n"
+                      "raid5: Operation continuing on %d devices.\n",
+                      bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
        }
 }
 
@@ -804,8 +1315,8 @@ static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
                        *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
                        break;
                default:
-                       printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
-                               conf->algorithm);
+                       printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
+                              conf->algorithm);
                }
                break;
        }
@@ -821,7 +1332,8 @@ static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
 static sector_t compute_blocknr(struct stripe_head *sh, int i)
 {
        raid5_conf_t *conf = sh->raid_conf;
-       int raid_disks = sh->disks, data_disks = raid_disks - 1;
+       int raid_disks = sh->disks;
+       int data_disks = raid_disks - conf->max_degraded;
        sector_t new_sector = sh->sector, check;
        int sectors_per_chunk = conf->chunk_size >> 9;
        sector_t stripe;
@@ -857,7 +1369,6 @@ static sector_t compute_blocknr(struct stripe_head *sh, int i)
                }
                break;
        case 6:
-               data_disks = raid_disks - 2;
                if (i == raid6_next_disk(sh->pd_idx, raid_disks))
                        return 0; /* It is the Q disk */
                switch (conf->algorithm) {
@@ -880,8 +1391,8 @@ static sector_t compute_blocknr(struct stripe_head *sh, int i)
                        }
                        break;
                default:
-                       printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
-                               conf->algorithm);
+                       printk(KERN_CRIT "raid6: unsupported algorithm %d\n",
+                              conf->algorithm);
                }
                break;
        }
@@ -889,7 +1400,7 @@ static sector_t compute_blocknr(struct stripe_head *sh, int i)
        chunk_number = stripe * data_disks + i;
        r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
 
-       check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
+       check = raid5_compute_sector(r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
        if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
                printk(KERN_ERR "compute_blocknr: map not correct\n");
                return 0;
@@ -943,146 +1454,22 @@ static void copy_data(int frombio, struct bio *bio,
                        __bio_kunmap_atomic(ba, KM_USER0);
                }
                if (clen < len) /* hit end of page */
-                       break;
-               page_offset +=  len;
-       }
-}
-
-#define check_xor()    do {                                            \
-                          if (count == MAX_XOR_BLOCKS) {               \
-                               xor_block(count, STRIPE_SIZE, ptr);     \
-                               count = 1;                              \
-                          }                                            \
-                       } while(0)
-
-
-static void compute_block(struct stripe_head *sh, int dd_idx)
-{
-       int i, count, disks = sh->disks;
-       void *ptr[MAX_XOR_BLOCKS], *p;
-
-       PRINTK("compute_block, stripe %llu, idx %d\n", 
-               (unsigned long long)sh->sector, dd_idx);
-
-       ptr[0] = page_address(sh->dev[dd_idx].page);
-       memset(ptr[0], 0, STRIPE_SIZE);
-       count = 1;
-       for (i = disks ; i--; ) {
-               if (i == dd_idx)
-                       continue;
-               p = page_address(sh->dev[i].page);
-               if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
-                       ptr[count++] = p;
-               else
-                       printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
-                               " not present\n", dd_idx,
-                               (unsigned long long)sh->sector, i);
-
-               check_xor();
-       }
-       if (count != 1)
-               xor_block(count, STRIPE_SIZE, ptr);
-       set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
-}
-
-static void compute_parity5(struct stripe_head *sh, int method)
-{
-       raid5_conf_t *conf = sh->raid_conf;
-       int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
-       void *ptr[MAX_XOR_BLOCKS];
-       struct bio *chosen;
-
-       PRINTK("compute_parity5, stripe %llu, method %d\n",
-               (unsigned long long)sh->sector, method);
-
-       count = 1;
-       ptr[0] = page_address(sh->dev[pd_idx].page);
-       switch(method) {
-       case READ_MODIFY_WRITE:
-               BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
-               for (i=disks ; i-- ;) {
-                       if (i==pd_idx)
-                               continue;
-                       if (sh->dev[i].towrite &&
-                           test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
-                               ptr[count++] = page_address(sh->dev[i].page);
-                               chosen = sh->dev[i].towrite;
-                               sh->dev[i].towrite = NULL;
-
-                               if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
-                                       wake_up(&conf->wait_for_overlap);
-
-                               BUG_ON(sh->dev[i].written);
-                               sh->dev[i].written = chosen;
-                               check_xor();
-                       }
-               }
-               break;
-       case RECONSTRUCT_WRITE:
-               memset(ptr[0], 0, STRIPE_SIZE);
-               for (i= disks; i-- ;)
-                       if (i!=pd_idx && sh->dev[i].towrite) {
-                               chosen = sh->dev[i].towrite;
-                               sh->dev[i].towrite = NULL;
-
-                               if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
-                                       wake_up(&conf->wait_for_overlap);
-
-                               BUG_ON(sh->dev[i].written);
-                               sh->dev[i].written = chosen;
-                       }
-               break;
-       case CHECK_PARITY:
-               break;
-       }
-       if (count>1) {
-               xor_block(count, STRIPE_SIZE, ptr);
-               count = 1;
-       }
-       
-       for (i = disks; i--;)
-               if (sh->dev[i].written) {
-                       sector_t sector = sh->dev[i].sector;
-                       struct bio *wbi = sh->dev[i].written;
-                       while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
-                               copy_data(1, wbi, sh->dev[i].page, sector);
-                               wbi = r5_next_bio(wbi, sector);
-                       }
-
-                       set_bit(R5_LOCKED, &sh->dev[i].flags);
-                       set_bit(R5_UPTODATE, &sh->dev[i].flags);
-               }
-
-       switch(method) {
-       case RECONSTRUCT_WRITE:
-       case CHECK_PARITY:
-               for (i=disks; i--;)
-                       if (i != pd_idx) {
-                               ptr[count++] = page_address(sh->dev[i].page);
-                               check_xor();
-                       }
-               break;
-       case READ_MODIFY_WRITE:
-               for (i = disks; i--;)
-                       if (sh->dev[i].written) {
-                               ptr[count++] = page_address(sh->dev[i].page);
-                               check_xor();
-                       }
-       }
-       if (count != 1)
-               xor_block(count, STRIPE_SIZE, ptr);
-       
-       if (method != CHECK_PARITY) {
-               set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
-               set_bit(R5_LOCKED,   &sh->dev[pd_idx].flags);
-       } else
-               clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
+                       break;
+               page_offset +=  len;
+       }
 }
 
+#define check_xor()    do {                                              \
+                               if (count == MAX_XOR_BLOCKS) {            \
+                               xor_blocks(count, STRIPE_SIZE, dest, ptr);\
+                               count = 0;                                \
+                          }                                              \
+                       } while(0)
+
 static void compute_parity6(struct stripe_head *sh, int method)
 {
        raid6_conf_t *conf = sh->raid_conf;
-       int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
+       int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = sh->disks, count;
        struct bio *chosen;
        /**** FIX THIS: This could be very bad if disks is close to 256 ****/
        void *ptrs[disks];
@@ -1090,7 +1477,7 @@ static void compute_parity6(struct stripe_head *sh, int method)
        qd_idx = raid6_next_disk(pd_idx, disks);
        d0_idx = raid6_next_disk(qd_idx, disks);
 
-       PRINTK("compute_parity, stripe %llu, method %d\n",
+       pr_debug("compute_parity, stripe %llu, method %d\n",
                (unsigned long long)sh->sector, method);
 
        switch(method) {
@@ -1163,22 +1550,21 @@ static void compute_parity6(struct stripe_head *sh, int method)
 /* Compute one missing block */
 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;
-       void *ptr[MAX_XOR_BLOCKS], *p;
+       int i, count, disks = sh->disks;
+       void *ptr[MAX_XOR_BLOCKS], *dest, *p;
        int pd_idx = sh->pd_idx;
        int qd_idx = raid6_next_disk(pd_idx, disks);
 
-       PRINTK("compute_block_1, stripe %llu, idx %d\n",
+       pr_debug("compute_block_1, stripe %llu, idx %d\n",
                (unsigned long long)sh->sector, dd_idx);
 
        if ( dd_idx == qd_idx ) {
                /* We're actually computing the Q drive */
                compute_parity6(sh, UPDATE_PARITY);
        } else {
-               ptr[0] = page_address(sh->dev[dd_idx].page);
-               if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
-               count = 1;
+               dest = page_address(sh->dev[dd_idx].page);
+               if (!nozero) memset(dest, 0, STRIPE_SIZE);
+               count = 0;
                for (i = disks ; i--; ) {
                        if (i == dd_idx || i == qd_idx)
                                continue;
@@ -1192,8 +1578,8 @@ static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
 
                        check_xor();
                }
-               if (count != 1)
-                       xor_block(count, STRIPE_SIZE, ptr);
+               if (count)
+                       xor_blocks(count, STRIPE_SIZE, dest, ptr);
                if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
                else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
        }
@@ -1202,8 +1588,7 @@ static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
 /* Compute two missing blocks */
 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
 {
-       raid6_conf_t *conf = sh->raid_conf;
-       int i, count, disks = conf->raid_disks;
+       int i, count, disks = sh->disks;
        int pd_idx = sh->pd_idx;
        int qd_idx = raid6_next_disk(pd_idx, disks);
        int d0_idx = raid6_next_disk(qd_idx, disks);
@@ -1217,7 +1602,7 @@ static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
        BUG_ON(faila == failb);
        if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
 
-       PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
+       pr_debug("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
               (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
 
        if ( failb == disks-1 ) {
@@ -1257,105 +1642,880 @@ static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
                        raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
                }
 
-               /* Both the above update both missing blocks */
-               set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
-               set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
+               /* Both the above update both missing blocks */
+               set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
+               set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
+       }
+}
+
+static void
+schedule_reconstruction5(struct stripe_head *sh, struct stripe_head_state *s,
+                        int rcw, int expand)
+{
+       int i, pd_idx = sh->pd_idx, disks = sh->disks;
+
+       if (rcw) {
+               /* if we are not expanding this is a proper write request, and
+                * there will be bios with new data to be drained into the
+                * stripe cache
+                */
+               if (!expand) {
+                       sh->reconstruct_state = reconstruct_state_drain_run;
+                       set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
+               } else
+                       sh->reconstruct_state = reconstruct_state_run;
+
+               set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
+
+               for (i = disks; i--; ) {
+                       struct r5dev *dev = &sh->dev[i];
+
+                       if (dev->towrite) {
+                               set_bit(R5_LOCKED, &dev->flags);
+                               set_bit(R5_Wantdrain, &dev->flags);
+                               if (!expand)
+                                       clear_bit(R5_UPTODATE, &dev->flags);
+                               s->locked++;
+                       }
+               }
+               if (s->locked + 1 == disks)
+                       if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
+                               atomic_inc(&sh->raid_conf->pending_full_writes);
+       } else {
+               BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
+                       test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
+
+               sh->reconstruct_state = reconstruct_state_prexor_drain_run;
+               set_bit(STRIPE_OP_PREXOR, &s->ops_request);
+               set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
+               set_bit(STRIPE_OP_POSTXOR, &s->ops_request);
+
+               for (i = disks; i--; ) {
+                       struct r5dev *dev = &sh->dev[i];
+                       if (i == pd_idx)
+                               continue;
+
+                       if (dev->towrite &&
+                           (test_bit(R5_UPTODATE, &dev->flags) ||
+                            test_bit(R5_Wantcompute, &dev->flags))) {
+                               set_bit(R5_Wantdrain, &dev->flags);
+                               set_bit(R5_LOCKED, &dev->flags);
+                               clear_bit(R5_UPTODATE, &dev->flags);
+                               s->locked++;
+                       }
+               }
+       }
+
+       /* keep the parity disk locked while asynchronous operations
+        * are in flight
+        */
+       set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
+       clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
+       s->locked++;
+
+       pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
+               __func__, (unsigned long long)sh->sector,
+               s->locked, s->ops_request);
+}
+
+/*
+ * Each stripe/dev can have one or more bion attached.
+ * toread/towrite point to the first in a chain.
+ * The bi_next chain must be in order.
+ */
+static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
+{
+       struct bio **bip;
+       raid5_conf_t *conf = sh->raid_conf;
+       int firstwrite=0;
+
+       pr_debug("adding bh b#%llu to stripe s#%llu\n",
+               (unsigned long long)bi->bi_sector,
+               (unsigned long long)sh->sector);
+
+
+       spin_lock(&sh->lock);
+       spin_lock_irq(&conf->device_lock);
+       if (forwrite) {
+               bip = &sh->dev[dd_idx].towrite;
+               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)
+                       goto overlap;
+               bip = & (*bip)->bi_next;
+       }
+       if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
+               goto overlap;
+
+       BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
+       if (*bip)
+               bi->bi_next = *bip;
+       *bip = bi;
+       bi->bi_phys_segments++;
+       spin_unlock_irq(&conf->device_lock);
+       spin_unlock(&sh->lock);
+
+       pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
+               (unsigned long long)bi->bi_sector,
+               (unsigned long long)sh->sector, dd_idx);
+
+       if (conf->mddev->bitmap && firstwrite) {
+               bitmap_startwrite(conf->mddev->bitmap, sh->sector,
+                                 STRIPE_SECTORS, 0);
+               sh->bm_seq = conf->seq_flush+1;
+               set_bit(STRIPE_BIT_DELAY, &sh->state);
+       }
+
+       if (forwrite) {
+               /* check if page is covered */
+               sector_t sector = sh->dev[dd_idx].sector;
+               for (bi=sh->dev[dd_idx].towrite;
+                    sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
+                            bi && bi->bi_sector <= sector;
+                    bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
+                       if (bi->bi_sector + (bi->bi_size>>9) >= sector)
+                               sector = bi->bi_sector + (bi->bi_size>>9);
+               }
+               if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
+                       set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
+       }
+       return 1;
+
+ overlap:
+       set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
+       spin_unlock_irq(&conf->device_lock);
+       spin_unlock(&sh->lock);
+       return 0;
+}
+
+static void end_reshape(raid5_conf_t *conf);
+
+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);
+}
+
+static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
+{
+       int sectors_per_chunk = conf->chunk_size >> 9;
+       int pd_idx, dd_idx;
+       int chunk_offset = sector_div(stripe, sectors_per_chunk);
+
+       raid5_compute_sector(stripe * (disks - conf->max_degraded)
+                            *sectors_per_chunk + chunk_offset,
+                            disks, disks - conf->max_degraded,
+                            &dd_idx, &pd_idx, conf);
+       return pd_idx;
+}
+
+static void
+handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
+                               struct stripe_head_state *s, int disks,
+                               struct bio **return_bi)
+{
+       int i;
+       for (i = disks; i--; ) {
+               struct bio *bi;
+               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) {
+                       s->to_write--;
+                       bitmap_end = 1;
+               }
+
+               if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
+                       wake_up(&conf->wait_for_overlap);
+
+               while (bi && bi->bi_sector <
+                       sh->dev[i].sector + STRIPE_SECTORS) {
+                       struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
+                       clear_bit(BIO_UPTODATE, &bi->bi_flags);
+                       if (!raid5_dec_bi_phys_segments(bi)) {
+                               md_write_end(conf->mddev);
+                               bi->bi_next = *return_bi;
+                               *return_bi = bi;
+                       }
+                       bi = nextbi;
+               }
+               /* 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);
+                       if (!raid5_dec_bi_phys_segments(bi)) {
+                               md_write_end(conf->mddev);
+                               bi->bi_next = *return_bi;
+                               *return_bi = bi;
+                       }
+                       bi = bi2;
+               }
+
+               /* fail any reads if this device is non-operational and
+                * the data has not reached the cache yet.
+                */
+               if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
+                   (!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))
+                               wake_up(&conf->wait_for_overlap);
+                       if (bi) s->to_read--;
+                       while (bi && bi->bi_sector <
+                              sh->dev[i].sector + STRIPE_SECTORS) {
+                               struct bio *nextbi =
+                                       r5_next_bio(bi, sh->dev[i].sector);
+                               clear_bit(BIO_UPTODATE, &bi->bi_flags);
+                               if (!raid5_dec_bi_phys_segments(bi)) {
+                                       bi->bi_next = *return_bi;
+                                       *return_bi = bi;
+                               }
+                               bi = nextbi;
+                       }
+               }
+               spin_unlock_irq(&conf->device_lock);
+               if (bitmap_end)
+                       bitmap_endwrite(conf->mddev->bitmap, sh->sector,
+                                       STRIPE_SECTORS, 0, 0);
+       }
+
+       if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
+               if (atomic_dec_and_test(&conf->pending_full_writes))
+                       md_wakeup_thread(conf->mddev->thread);
+}
+
+/* fetch_block5 - checks the given member device to see if its data needs
+ * to be read or computed to satisfy a request.
+ *
+ * Returns 1 when no more member devices need to be checked, otherwise returns
+ * 0 to tell the loop in handle_stripe_fill5 to continue
+ */
+static int fetch_block5(struct stripe_head *sh, struct stripe_head_state *s,
+                       int disk_idx, int disks)
+{
+       struct r5dev *dev = &sh->dev[disk_idx];
+       struct r5dev *failed_dev = &sh->dev[s->failed_num];
+
+       /* is the data in this block needed, and can we get it? */
+       if (!test_bit(R5_LOCKED, &dev->flags) &&
+           !test_bit(R5_UPTODATE, &dev->flags) &&
+           (dev->toread ||
+            (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
+            s->syncing || s->expanding ||
+            (s->failed &&
+             (failed_dev->toread ||
+              (failed_dev->towrite &&
+               !test_bit(R5_OVERWRITE, &failed_dev->flags)))))) {
+               /* We would like to get this block, possibly by computing it,
+                * otherwise read it if the backing disk is insync
+                */
+               if ((s->uptodate == disks - 1) &&
+                   (s->failed && disk_idx == s->failed_num)) {
+                       set_bit(STRIPE_COMPUTE_RUN, &sh->state);
+                       set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
+                       set_bit(R5_Wantcompute, &dev->flags);
+                       sh->ops.target = disk_idx;
+                       s->req_compute = 1;
+                       /* Careful: from this point on 'uptodate' is in the eye
+                        * of raid5_run_ops which services 'compute' operations
+                        * before writes. R5_Wantcompute flags a block that will
+                        * be R5_UPTODATE by the time it is needed for a
+                        * subsequent operation.
+                        */
+                       s->uptodate++;
+                       return 1; /* uptodate + compute == disks */
+               } else if (test_bit(R5_Insync, &dev->flags)) {
+                       set_bit(R5_LOCKED, &dev->flags);
+                       set_bit(R5_Wantread, &dev->flags);
+                       s->locked++;
+                       pr_debug("Reading block %d (sync=%d)\n", disk_idx,
+                               s->syncing);
+               }
+       }
+
+       return 0;
+}
+
+/**
+ * handle_stripe_fill5 - read or compute data to satisfy pending requests.
+ */
+static void handle_stripe_fill5(struct stripe_head *sh,
+                       struct stripe_head_state *s, int disks)
+{
+       int i;
+
+       /* look for blocks to read/compute, skip this if a compute
+        * is already in flight, or if the stripe contents are in the
+        * midst of changing due to a write
+        */
+       if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
+           !sh->reconstruct_state)
+               for (i = disks; i--; )
+                       if (fetch_block5(sh, s, i, disks))
+                               break;
+       set_bit(STRIPE_HANDLE, &sh->state);
+}
+
+static void handle_stripe_fill6(struct stripe_head *sh,
+                       struct stripe_head_state *s, struct r6_state *r6s,
+                       int disks)
+{
+       int i;
+       for (i = disks; i--; ) {
+               struct r5dev *dev = &sh->dev[i];
+               if (!test_bit(R5_LOCKED, &dev->flags) &&
+                   !test_bit(R5_UPTODATE, &dev->flags) &&
+                   (dev->toread || (dev->towrite &&
+                    !test_bit(R5_OVERWRITE, &dev->flags)) ||
+                    s->syncing || s->expanding ||
+                    (s->failed >= 1 &&
+                     (sh->dev[r6s->failed_num[0]].toread ||
+                      s->to_write)) ||
+                    (s->failed >= 2 &&
+                     (sh->dev[r6s->failed_num[1]].toread ||
+                      s->to_write)))) {
+                       /* we would like to get this block, possibly
+                        * by computing it, but we might not be able to
+                        */
+                       if ((s->uptodate == disks - 1) &&
+                           (s->failed && (i == r6s->failed_num[0] ||
+                                          i == r6s->failed_num[1]))) {
+                               pr_debug("Computing stripe %llu block %d\n",
+                                      (unsigned long long)sh->sector, i);
+                               compute_block_1(sh, i, 0);
+                               s->uptodate++;
+                       } else if ( s->uptodate == disks-2 && s->failed >= 2 ) {
+                               /* Computing 2-failure is *very* expensive; only
+                                * do it if failed >= 2
+                                */
+                               int other;
+                               for (other = disks; other--; ) {
+                                       if (other == i)
+                                               continue;
+                                       if (!test_bit(R5_UPTODATE,
+                                             &sh->dev[other].flags))
+                                               break;
+                               }
+                               BUG_ON(other < 0);
+                               pr_debug("Computing stripe %llu blocks %d,%d\n",
+                                      (unsigned long long)sh->sector,
+                                      i, other);
+                               compute_block_2(sh, i, other);
+                               s->uptodate += 2;
+                       } else if (test_bit(R5_Insync, &dev->flags)) {
+                               set_bit(R5_LOCKED, &dev->flags);
+                               set_bit(R5_Wantread, &dev->flags);
+                               s->locked++;
+                               pr_debug("Reading block %d (sync=%d)\n",
+                                       i, s->syncing);
+                       }
+               }
+       }
+       set_bit(STRIPE_HANDLE, &sh->state);
+}
+
+
+/* handle_stripe_clean_event
+ * any written block on an uptodate or failed drive can be returned.
+ * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
+ * never LOCKED, so we don't need to test 'failed' directly.
+ */
+static void handle_stripe_clean_event(raid5_conf_t *conf,
+       struct stripe_head *sh, int disks, struct bio **return_bi)
+{
+       int i;
+       struct r5dev *dev;
+
+       for (i = disks; i--; )
+               if (sh->dev[i].written) {
+                       dev = &sh->dev[i];
+                       if (!test_bit(R5_LOCKED, &dev->flags) &&
+                               test_bit(R5_UPTODATE, &dev->flags)) {
+                               /* We can return any write requests */
+                               struct bio *wbi, *wbi2;
+                               int bitmap_end = 0;
+                               pr_debug("Return write for disc %d\n", i);
+                               spin_lock_irq(&conf->device_lock);
+                               wbi = dev->written;
+                               dev->written = NULL;
+                               while (wbi && wbi->bi_sector <
+                                       dev->sector + STRIPE_SECTORS) {
+                                       wbi2 = r5_next_bio(wbi, dev->sector);
+                                       if (!raid5_dec_bi_phys_segments(wbi)) {
+                                               md_write_end(conf->mddev);
+                                               wbi->bi_next = *return_bi;
+                                               *return_bi = wbi;
+                                       }
+                                       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);
+                       }
+               }
+
+       if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
+               if (atomic_dec_and_test(&conf->pending_full_writes))
+                       md_wakeup_thread(conf->mddev->thread);
+}
+
+static void handle_stripe_dirtying5(raid5_conf_t *conf,
+               struct stripe_head *sh, struct stripe_head_state *s, int disks)
+{
+       int rmw = 0, rcw = 0, i;
+       for (i = disks; i--; ) {
+               /* would I have to read this buffer for read_modify_write */
+               struct r5dev *dev = &sh->dev[i];
+               if ((dev->towrite || i == sh->pd_idx) &&
+                   !test_bit(R5_LOCKED, &dev->flags) &&
+                   !(test_bit(R5_UPTODATE, &dev->flags) ||
+                     test_bit(R5_Wantcompute, &dev->flags))) {
+                       if (test_bit(R5_Insync, &dev->flags))
+                               rmw++;
+                       else
+                               rmw += 2*disks;  /* cannot read it */
+               }
+               /* Would I have to read this buffer for reconstruct_write */
+               if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
+                   !test_bit(R5_LOCKED, &dev->flags) &&
+                   !(test_bit(R5_UPTODATE, &dev->flags) ||
+                   test_bit(R5_Wantcompute, &dev->flags))) {
+                       if (test_bit(R5_Insync, &dev->flags)) rcw++;
+                       else
+                               rcw += 2*disks;
+               }
+       }
+       pr_debug("for sector %llu, rmw=%d rcw=%d\n",
+               (unsigned long long)sh->sector, rmw, rcw);
+       set_bit(STRIPE_HANDLE, &sh->state);
+       if (rmw < rcw && rmw > 0)
+               /* prefer read-modify-write, but need to get some data */
+               for (i = disks; i--; ) {
+                       struct r5dev *dev = &sh->dev[i];
+                       if ((dev->towrite || i == sh->pd_idx) &&
+                           !test_bit(R5_LOCKED, &dev->flags) &&
+                           !(test_bit(R5_UPTODATE, &dev->flags) ||
+                           test_bit(R5_Wantcompute, &dev->flags)) &&
+                           test_bit(R5_Insync, &dev->flags)) {
+                               if (
+                                 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
+                                       pr_debug("Read_old block "
+                                               "%d for r-m-w\n", i);
+                                       set_bit(R5_LOCKED, &dev->flags);
+                                       set_bit(R5_Wantread, &dev->flags);
+                                       s->locked++;
+                               } else {
+                                       set_bit(STRIPE_DELAYED, &sh->state);
+                                       set_bit(STRIPE_HANDLE, &sh->state);
+                               }
+                       }
+               }
+       if (rcw <= rmw && rcw > 0)
+               /* want reconstruct write, but need to get some data */
+               for (i = disks; i--; ) {
+                       struct r5dev *dev = &sh->dev[i];
+                       if (!test_bit(R5_OVERWRITE, &dev->flags) &&
+                           i != sh->pd_idx &&
+                           !test_bit(R5_LOCKED, &dev->flags) &&
+                           !(test_bit(R5_UPTODATE, &dev->flags) ||
+                           test_bit(R5_Wantcompute, &dev->flags)) &&
+                           test_bit(R5_Insync, &dev->flags)) {
+                               if (
+                                 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
+                                       pr_debug("Read_old block "
+                                               "%d for Reconstruct\n", i);
+                                       set_bit(R5_LOCKED, &dev->flags);
+                                       set_bit(R5_Wantread, &dev->flags);
+                                       s->locked++;
+                               } else {
+                                       set_bit(STRIPE_DELAYED, &sh->state);
+                                       set_bit(STRIPE_HANDLE, &sh->state);
+                               }
+                       }
+               }
+       /* now if nothing is locked, and if we have enough data,
+        * we can start a write request
+        */
+       /* since handle_stripe can be called at any time we need to handle the
+        * case where a compute block operation has been submitted and then a
+        * subsequent call wants to start a write request.  raid5_run_ops only
+        * handles the case where compute block and postxor are requested
+        * simultaneously.  If this is not the case then new writes need to be
+        * held off until the compute completes.
+        */
+       if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
+           (s->locked == 0 && (rcw == 0 || rmw == 0) &&
+           !test_bit(STRIPE_BIT_DELAY, &sh->state)))
+               schedule_reconstruction5(sh, s, rcw == 0, 0);
+}
+
+static void handle_stripe_dirtying6(raid5_conf_t *conf,
+               struct stripe_head *sh, struct stripe_head_state *s,
+               struct r6_state *r6s, int disks)
+{
+       int rcw = 0, must_compute = 0, pd_idx = sh->pd_idx, i;
+       int qd_idx = r6s->qd_idx;
+       for (i = disks; i--; ) {
+               struct r5dev *dev = &sh->dev[i];
+               /* Would I have to read this buffer for reconstruct_write */
+               if (!test_bit(R5_OVERWRITE, &dev->flags)
+                   && i != pd_idx && i != qd_idx
+                   && (!test_bit(R5_LOCKED, &dev->flags)
+                           ) &&
+                   !test_bit(R5_UPTODATE, &dev->flags)) {
+                       if (test_bit(R5_Insync, &dev->flags)) rcw++;
+                       else {
+                               pr_debug("raid6: must_compute: "
+                                       "disk %d flags=%#lx\n", i, dev->flags);
+                               must_compute++;
+                       }
+               }
+       }
+       pr_debug("for sector %llu, rcw=%d, must_compute=%d\n",
+              (unsigned long long)sh->sector, rcw, must_compute);
+       set_bit(STRIPE_HANDLE, &sh->state);
+
+       if (rcw > 0)
+               /* want reconstruct write, but need to get some data */
+               for (i = disks; i--; ) {
+                       struct r5dev *dev = &sh->dev[i];
+                       if (!test_bit(R5_OVERWRITE, &dev->flags)
+                           && !(s->failed == 0 && (i == pd_idx || i == qd_idx))
+                           && !test_bit(R5_LOCKED, &dev->flags) &&
+                           !test_bit(R5_UPTODATE, &dev->flags) &&
+                           test_bit(R5_Insync, &dev->flags)) {
+                               if (
+                                 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
+                                       pr_debug("Read_old stripe %llu "
+                                               "block %d for Reconstruct\n",
+                                            (unsigned long long)sh->sector, i);
+                                       set_bit(R5_LOCKED, &dev->flags);
+                                       set_bit(R5_Wantread, &dev->flags);
+                                       s->locked++;
+                               } else {
+                                       pr_debug("Request delayed stripe %llu "
+                                               "block %d for Reconstruct\n",
+                                            (unsigned long long)sh->sector, i);
+                                       set_bit(STRIPE_DELAYED, &sh->state);
+                                       set_bit(STRIPE_HANDLE, &sh->state);
+                               }
+                       }
+               }
+       /* now if nothing is locked, and if we have enough data, we can start a
+        * write request
+        */
+       if (s->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 (s->failed) {
+                       case 0:
+                               BUG();
+                       case 1:
+                               compute_block_1(sh, r6s->failed_num[0], 0);
+                               break;
+                       case 2:
+                               compute_block_2(sh, r6s->failed_num[0],
+                                               r6s->failed_num[1]);
+                               break;
+                       default: /* This request should have been failed? */
+                               BUG();
+                       }
+               }
+
+               pr_debug("Computing parity for stripe %llu\n",
+                       (unsigned long long)sh->sector);
+               compute_parity6(sh, RECONSTRUCT_WRITE);
+               /* now every locked buffer is ready to be written */
+               for (i = disks; i--; )
+                       if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
+                               pr_debug("Writing stripe %llu block %d\n",
+                                      (unsigned long long)sh->sector, i);
+                               s->locked++;
+                               set_bit(R5_Wantwrite, &sh->dev[i].flags);
+                       }
+               if (s->locked == disks)
+                       if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
+                               atomic_inc(&conf->pending_full_writes);
+               /* 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)
+                               md_wakeup_thread(conf->mddev->thread);
+               }
        }
 }
 
-
-
-/*
- * Each stripe/dev can have one or more bion attached.
- * toread/towrite point to the first in a chain.
- * The bi_next chain must be in order.
- */
-static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
+static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
+                               struct stripe_head_state *s, int disks)
 {
-       struct bio **bip;
-       raid5_conf_t *conf = sh->raid_conf;
-       int firstwrite=0;
+       struct r5dev *dev = NULL;
 
-       PRINTK("adding bh b#%llu to stripe s#%llu\n",
-               (unsigned long long)bi->bi_sector,
-               (unsigned long long)sh->sector);
+       set_bit(STRIPE_HANDLE, &sh->state);
 
+       switch (sh->check_state) {
+       case check_state_idle:
+               /* start a new check operation if there are no failures */
+               if (s->failed == 0) {
+                       BUG_ON(s->uptodate != disks);
+                       sh->check_state = check_state_run;
+                       set_bit(STRIPE_OP_CHECK, &s->ops_request);
+                       clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
+                       s->uptodate--;
+                       break;
+               }
+               dev = &sh->dev[s->failed_num];
+               /* fall through */
+       case check_state_compute_result:
+               sh->check_state = check_state_idle;
+               if (!dev)
+                       dev = &sh->dev[sh->pd_idx];
+
+               /* check that a write has not made the stripe insync */
+               if (test_bit(STRIPE_INSYNC, &sh->state))
+                       break;
 
-       spin_lock(&sh->lock);
-       spin_lock_irq(&conf->device_lock);
-       if (forwrite) {
-               bip = &sh->dev[dd_idx].towrite;
-               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)
-                       goto overlap;
-               bip = & (*bip)->bi_next;
-       }
-       if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
-               goto overlap;
+               /* either failed parity check, or recovery is happening */
+               BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
+               BUG_ON(s->uptodate != disks);
 
-       BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
-       if (*bip)
-               bi->bi_next = *bip;
-       *bip = bi;
-       bi->bi_phys_segments ++;
-       spin_unlock_irq(&conf->device_lock);
-       spin_unlock(&sh->lock);
+               set_bit(R5_LOCKED, &dev->flags);
+               s->locked++;
+               set_bit(R5_Wantwrite, &dev->flags);
 
-       PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
-               (unsigned long long)bi->bi_sector,
-               (unsigned long long)sh->sector, dd_idx);
+               clear_bit(STRIPE_DEGRADED, &sh->state);
+               set_bit(STRIPE_INSYNC, &sh->state);
+               break;
+       case check_state_run:
+               break; /* we will be called again upon completion */
+       case check_state_check_result:
+               sh->check_state = check_state_idle;
 
-       if (conf->mddev->bitmap && firstwrite) {
-               bitmap_startwrite(conf->mddev->bitmap, sh->sector,
-                                 STRIPE_SECTORS, 0);
-               sh->bm_seq = conf->seq_flush+1;
-               set_bit(STRIPE_BIT_DELAY, &sh->state);
-       }
+               /* if a failure occurred during the check operation, leave
+                * STRIPE_INSYNC not set and let the stripe be handled again
+                */
+               if (s->failed)
+                       break;
 
-       if (forwrite) {
-               /* check if page is covered */
-               sector_t sector = sh->dev[dd_idx].sector;
-               for (bi=sh->dev[dd_idx].towrite;
-                    sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
-                            bi && bi->bi_sector <= sector;
-                    bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
-                       if (bi->bi_sector + (bi->bi_size>>9) >= sector)
-                               sector = bi->bi_sector + (bi->bi_size>>9);
+               /* handle a successful check operation, if parity is correct
+                * we are done.  Otherwise update the mismatch count and repair
+                * parity if !MD_RECOVERY_CHECK
+                */
+               if (sh->ops.zero_sum_result == 0)
+                       /* parity is correct (on disc,
+                        * not in buffer any more)
+                        */
+                       set_bit(STRIPE_INSYNC, &sh->state);
+               else {
+                       conf->mddev->resync_mismatches += STRIPE_SECTORS;
+                       if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
+                               /* don't try to repair!! */
+                               set_bit(STRIPE_INSYNC, &sh->state);
+                       else {
+                               sh->check_state = check_state_compute_run;
+                               set_bit(STRIPE_COMPUTE_RUN, &sh->state);
+                               set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
+                               set_bit(R5_Wantcompute,
+                                       &sh->dev[sh->pd_idx].flags);
+                               sh->ops.target = sh->pd_idx;
+                               s->uptodate++;
+                       }
                }
-               if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
-                       set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
+               break;
+       case check_state_compute_run:
+               break;
+       default:
+               printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
+                      __func__, sh->check_state,
+                      (unsigned long long) sh->sector);
+               BUG();
        }
-       return 1;
-
- overlap:
-       set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
-       spin_unlock_irq(&conf->device_lock);
-       spin_unlock(&sh->lock);
-       return 0;
 }
 
-static void end_reshape(raid5_conf_t *conf);
 
-static int page_is_zero(struct page *p)
+static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
+                               struct stripe_head_state *s,
+                               struct r6_state *r6s, struct page *tmp_page,
+                               int disks)
 {
-       char *a = page_address(p);
-       return ((*(u32*)a) == 0 &&
-               memcmp(a, a+4, STRIPE_SIZE-4)==0);
+       int update_p = 0, update_q = 0;
+       struct r5dev *dev;
+       int pd_idx = sh->pd_idx;
+       int qd_idx = r6s->qd_idx;
+
+       set_bit(STRIPE_HANDLE, &sh->state);
+
+       BUG_ON(s->failed > 2);
+       BUG_ON(s->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 (s->failed == r6s->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 (!r6s->q_failed && s->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_parity6(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;
+               }
+
+               /* now write out any block on a failed drive,
+                * or P or Q if they need it
+                */
+
+               if (s->failed == 2) {
+                       dev = &sh->dev[r6s->failed_num[1]];
+                       s->locked++;
+                       set_bit(R5_LOCKED, &dev->flags);
+                       set_bit(R5_Wantwrite, &dev->flags);
+               }
+               if (s->failed >= 1) {
+                       dev = &sh->dev[r6s->failed_num[0]];
+                       s->locked++;
+                       set_bit(R5_LOCKED, &dev->flags);
+                       set_bit(R5_Wantwrite, &dev->flags);
+               }
+
+               if (update_p) {
+                       dev = &sh->dev[pd_idx];
+                       s->locked++;
+                       set_bit(R5_LOCKED, &dev->flags);
+                       set_bit(R5_Wantwrite, &dev->flags);
+               }
+               if (update_q) {
+                       dev = &sh->dev[qd_idx];
+                       s->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);
+       }
 }
 
-static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
+static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh,
+                               struct r6_state *r6s)
 {
-       int sectors_per_chunk = conf->chunk_size >> 9;
-       int pd_idx, dd_idx;
-       int chunk_offset = sector_div(stripe, sectors_per_chunk);
+       int i;
 
-       raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
-                            + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
-       return pd_idx;
+       /* We have read all the blocks in this stripe and now we need to
+        * copy some of them into a target stripe for expand.
+        */
+       struct dma_async_tx_descriptor *tx = NULL;
+       clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
+       for (i = 0; i < sh->disks; i++)
+               if (i != sh->pd_idx && (!r6s || i != r6s->qd_idx)) {
+                       int dd_idx, pd_idx, j;
+                       struct stripe_head *sh2;
+
+                       sector_t bn = compute_blocknr(sh, i);
+                       sector_t s = raid5_compute_sector(bn, conf->raid_disks,
+                                               conf->raid_disks -
+                                               conf->max_degraded, &dd_idx,
+                                               &pd_idx, conf);
+                       sh2 = get_active_stripe(conf, s, conf->raid_disks,
+                                               pd_idx, 1);
+                       if (sh2 == NULL)
+                               /* so far only the early blocks of this stripe
+                                * have been requested.  When later blocks
+                                * get requested, we will try again
+                                */
+                               continue;
+                       if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
+                          test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
+                               /* must have already done this block */
+                               release_stripe(sh2);
+                               continue;
+                       }
+
+                       /* place all the copies on one channel */
+                       tx = async_memcpy(sh2->dev[dd_idx].page,
+                               sh->dev[i].page, 0, 0, STRIPE_SIZE,
+                               ASYNC_TX_DEP_ACK, tx, NULL, NULL);
+
+                       set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
+                       set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
+                       for (j = 0; j < conf->raid_disks; j++)
+                               if (j != sh2->pd_idx &&
+                                   (!r6s || j != raid6_next_disk(sh2->pd_idx,
+                                                                sh2->disks)) &&
+                                   !test_bit(R5_Expanded, &sh2->dev[j].flags))
+                                       break;
+                       if (j == conf->raid_disks) {
+                               set_bit(STRIPE_EXPAND_READY, &sh2->state);
+                               set_bit(STRIPE_HANDLE, &sh2->state);
+                       }
+                       release_stripe(sh2);
+
+               }
+       /* done submitting copies, wait for them to complete */
+       if (tx) {
+               async_tx_ack(tx);
+               dma_wait_for_async_tx(tx);
+       }
 }
 
 
@@ -1371,82 +2531,77 @@ static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
  *    schedule a write of some buffers
  *    return confirmation of parity correctness
  *
- * Parity calculations are done inside the stripe lock
  * buffers are taken off read_list or write_list, and bh_cache buffers
  * get BH_Lock set before the stripe lock is released.
  *
  */
-static void handle_stripe5(struct stripe_head *sh)
+
+static bool handle_stripe5(struct stripe_head *sh)
 {
        raid5_conf_t *conf = sh->raid_conf;
-       int disks = sh->disks;
-       struct bio *return_bi= NULL;
-       struct bio *bi;
-       int i;
-       int syncing, expanding, expanded;
-       int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
-       int non_overwrite = 0;
-       int failed_num=0;
+       int disks = sh->disks, i;
+       struct bio *return_bi = NULL;
+       struct stripe_head_state s;
        struct r5dev *dev;
+       mdk_rdev_t *blocked_rdev = NULL;
+       int prexor;
 
-       PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
-               (unsigned long long)sh->sector, atomic_read(&sh->count),
-               sh->pd_idx);
+       memset(&s, 0, sizeof(s));
+       pr_debug("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d check:%d "
+                "reconstruct:%d\n", (unsigned long long)sh->sector, sh->state,
+                atomic_read(&sh->count), sh->pd_idx, sh->check_state,
+                sh->reconstruct_state);
 
        spin_lock(&sh->lock);
        clear_bit(STRIPE_HANDLE, &sh->state);
        clear_bit(STRIPE_DELAYED, &sh->state);
 
-       syncing = test_bit(STRIPE_SYNCING, &sh->state);
-       expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
-       expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
-       /* Now to look around and see what can be done */
+       s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
+       s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
+       s.expanded = test_bit(STRIPE_EXPAND_READY, &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];
+               struct r5dev *dev = &sh->dev[i];
                clear_bit(R5_Insync, &dev->flags);
 
-               PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
-                       i, dev->flags, dev->toread, dev->towrite, dev->written);
-               /* maybe we can reply to a read */
-               if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
-                       struct bio *rbi, *rbi2;
-                       PRINTK("Return read for disc %d\n", i);
-                       spin_lock_irq(&conf->device_lock);
-                       rbi = dev->toread;
-                       dev->toread = NULL;
-                       if (test_and_clear_bit(R5_Overlap, &dev->flags))
-                               wake_up(&conf->wait_for_overlap);
-                       spin_unlock_irq(&conf->device_lock);
-                       while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
-                               copy_data(0, rbi, dev->page, dev->sector);
-                               rbi2 = r5_next_bio(rbi, dev->sector);
-                               spin_lock_irq(&conf->device_lock);
-                               if (--rbi->bi_phys_segments == 0) {
-                                       rbi->bi_next = return_bi;
-                                       return_bi = rbi;
-                               }
-                               spin_unlock_irq(&conf->device_lock);
-                               rbi = rbi2;
-                       }
-               }
+               pr_debug("check %d: state 0x%lx toread %p read %p write %p "
+                       "written %p\n", i, dev->flags, dev->toread, dev->read,
+                       dev->towrite, dev->written);
 
-               /* now count some things */
-               if (test_bit(R5_LOCKED, &dev->flags)) locked++;
-               if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
+               /* maybe we can request a biofill operation
+                *
+                * new wantfill requests are only permitted while
+                * ops_complete_biofill is guaranteed to be inactive
+                */
+               if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
+                   !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
+                       set_bit(R5_Wantfill, &dev->flags);
 
-               
-               if (dev->toread) to_read++;
+               /* now count some things */
+               if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
+               if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
+               if (test_bit(R5_Wantcompute, &dev->flags)) s.compute++;
+
+               if (test_bit(R5_Wantfill, &dev->flags))
+                       s.to_fill++;
+               else if (dev->toread)
+                       s.to_read++;
                if (dev->towrite) {
-                       to_write++;
+                       s.to_write++;
                        if (!test_bit(R5_OVERWRITE, &dev->flags))
-                               non_overwrite++;
+                               s.non_overwrite++;
                }
-               if (dev->written) written++;
+               if (dev->written)
+                       s.written++;
                rdev = rcu_dereference(conf->disks[i].rdev);
+               if (blocked_rdev == NULL &&
+                   rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
+                       blocked_rdev = rdev;
+                       atomic_inc(&rdev->nr_pending);
+               }
                if (!rdev || !test_bit(In_sync, &rdev->flags)) {
                        /* The ReadError flag will just be confusing now */
                        clear_bit(R5_ReadError, &dev->flags);
@@ -1454,321 +2609,119 @@ static void handle_stripe5(struct stripe_head *sh)
                }
                if (!rdev || !test_bit(In_sync, &rdev->flags)
                    || test_bit(R5_ReadError, &dev->flags)) {
-                       failed++;
-                       failed_num = i;
+                       s.failed++;
+                       s.failed_num = i;
                } else
                        set_bit(R5_Insync, &dev->flags);
        }
        rcu_read_unlock();
-       PRINTK("locked=%d uptodate=%d to_read=%d"
+
+       if (unlikely(blocked_rdev)) {
+               if (s.syncing || s.expanding || s.expanded ||
+                   s.to_write || s.written) {
+                       set_bit(STRIPE_HANDLE, &sh->state);
+                       goto unlock;
+               }
+               /* There is nothing for the blocked_rdev to block */
+               rdev_dec_pending(blocked_rdev, conf->mddev);
+               blocked_rdev = NULL;
+       }
+
+       if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
+               set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
+               set_bit(STRIPE_BIOFILL_RUN, &sh->state);
+       }
+
+       pr_debug("locked=%d uptodate=%d to_read=%d"
                " to_write=%d failed=%d failed_num=%d\n",
-               locked, uptodate, to_read, to_write, failed, failed_num);
+               s.locked, s.uptodate, s.to_read, s.to_write,
+               s.failed, s.failed_num);
        /* check if the array has lost two devices and, if so, some requests might
         * need to be failed
         */
-       if (failed > 1 && to_read+to_write+written) {
-               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--; bitmap_end = 1; }
-
-                       if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
-                               wake_up(&conf->wait_for_overlap);
-
-                       while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
-                               struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
-                               clear_bit(BIO_UPTODATE, &bi->bi_flags);
-                               if (--bi->bi_phys_segments == 0) {
-                                       md_write_end(conf->mddev);
-                                       bi->bi_next = return_bi;
-                                       return_bi = bi;
-                               }
-                               bi = nextbi;
-                       }
-                       /* 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);
-                               if (--bi->bi_phys_segments == 0) {
-                                       md_write_end(conf->mddev);
-                                       bi->bi_next = return_bi;
-                                       return_bi = bi;
-                               }
-                               bi = bi2;
-                       }
-
-                       /* fail any reads if this device is non-operational */
-                       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))
-                                       wake_up(&conf->wait_for_overlap);
-                               if (bi) to_read--;
-                               while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
-                                       struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
-                                       clear_bit(BIO_UPTODATE, &bi->bi_flags);
-                                       if (--bi->bi_phys_segments == 0) {
-                                               bi->bi_next = return_bi;
-                                               return_bi = bi;
-                                       }
-                                       bi = nextbi;
-                               }
-                       }
-                       spin_unlock_irq(&conf->device_lock);
-                       if (bitmap_end)
-                               bitmap_endwrite(conf->mddev->bitmap, sh->sector,
-                                               STRIPE_SECTORS, 0, 0);
-               }
-       }
-       if (failed > 1 && syncing) {
+       if (s.failed > 1 && s.to_read+s.to_write+s.written)
+               handle_failed_stripe(conf, sh, &s, disks, &return_bi);
+       if (s.failed > 1 && s.syncing) {
                md_done_sync(conf->mddev, STRIPE_SECTORS,0);
                clear_bit(STRIPE_SYNCING, &sh->state);
-               syncing = 0;
+               s.syncing = 0;
        }
 
        /* might be able to return some write requests if the parity block
         * is safe, or on a failed drive
         */
-       dev = &sh->dev[sh->pd_idx];
-       if ( written &&
-            ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
-               test_bit(R5_UPTODATE, &dev->flags))
-              || (failed == 1 && failed_num == sh->pd_idx))
-           ) {
-           /* any written block on an uptodate or failed drive can be returned.
-            * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but 
-            * never LOCKED, so we don't need to test 'failed' directly.
-            */
-           for (i=disks; i--; )
-               if (sh->dev[i].written) {
-                   dev = &sh->dev[i];
-                   if (!test_bit(R5_LOCKED, &dev->flags) &&
-                        test_bit(R5_UPTODATE, &dev->flags) ) {
-                       /* We can return any write requests */
-                           struct bio *wbi, *wbi2;
-                           int bitmap_end = 0;
-                           PRINTK("Return write for disc %d\n", i);
-                           spin_lock_irq(&conf->device_lock);
-                           wbi = dev->written;
-                           dev->written = NULL;
-                           while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
-                                   wbi2 = r5_next_bio(wbi, dev->sector);
-                                   if (--wbi->bi_phys_segments == 0) {
-                                           md_write_end(conf->mddev);
-                                           wbi->bi_next = return_bi;
-                                           return_bi = wbi;
-                                   }
-                                   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);
-                   }
-               }
-       }
-
-       /* Now we might consider reading some blocks, either to check/generate
-        * parity, or to satisfy requests
-        * or to load a block that is being partially written.
-        */
-       if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
-               for (i=disks; i--;) {
-                       dev = &sh->dev[i];
-                       if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
-                           (dev->toread ||
-                            (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
-                            syncing ||
-                            expanding ||
-                            (failed && (sh->dev[failed_num].toread ||
-                                        (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
-                                   )
-                               ) {
-                               /* we would like to get this block, possibly
-                                * by computing it, but we might not be able to
-                                */
-                               if (uptodate == disks-1) {
-                                       PRINTK("Computing block %d\n", i);
-                                       compute_block(sh, i);
-                                       uptodate++;
-                               } else if (test_bit(R5_Insync, &dev->flags)) {
-                                       set_bit(R5_LOCKED, &dev->flags);
-                                       set_bit(R5_Wantread, &dev->flags);
-#if 0
-                                       /* if I am just reading this block and we don't have
-                                          a failed drive, or any pending writes then sidestep the cache */
-                                       if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
-                                           ! syncing && !failed && !to_write) {
-                                               sh->bh_cache[i]->b_page =  sh->bh_read[i]->b_page;
-                                               sh->bh_cache[i]->b_data =  sh->bh_read[i]->b_data;
-                                       }
-#endif
-                                       locked++;
-                                       PRINTK("Reading block %d (sync=%d)\n", 
-                                               i, syncing);
-                               }
-                       }
-               }
-               set_bit(STRIPE_HANDLE, &sh->state);
-       }
-
-       /* now to consider writing and what else, if anything should be read */
-       if (to_write) {
-               int rmw=0, rcw=0;
-               for (i=disks ; i--;) {
-                       /* would I have to read this buffer for read_modify_write */
-                       dev = &sh->dev[i];
-                       if ((dev->towrite || i == sh->pd_idx) &&
-                           (!test_bit(R5_LOCKED, &dev->flags) 
-#if 0
-|| sh->bh_page[i]!=bh->b_page
-#endif
-                                   ) &&
-                           !test_bit(R5_UPTODATE, &dev->flags)) {
-                               if (test_bit(R5_Insync, &dev->flags)
-/*                                 && !(!mddev->insync && i == sh->pd_idx) */
-                                       )
-                                       rmw++;
-                               else rmw += 2*disks;  /* cannot read it */
-                       }
-                       /* Would I have to read this buffer for reconstruct_write */
-                       if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
-                           (!test_bit(R5_LOCKED, &dev->flags) 
-#if 0
-|| sh->bh_page[i] != bh->b_page
-#endif
-                                   ) &&
-                           !test_bit(R5_UPTODATE, &dev->flags)) {
-                               if (test_bit(R5_Insync, &dev->flags)) rcw++;
-                               else rcw += 2*disks;
-                       }
-               }
-               PRINTK("for sector %llu, rmw=%d rcw=%d\n", 
-                       (unsigned long long)sh->sector, rmw, rcw);
-               set_bit(STRIPE_HANDLE, &sh->state);
-               if (rmw < rcw && rmw > 0)
-                       /* prefer read-modify-write, but need to get some data */
-                       for (i=disks; i--;) {
-                               dev = &sh->dev[i];
-                               if ((dev->towrite || i == sh->pd_idx) &&
-                                   !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
-                                   test_bit(R5_Insync, &dev->flags)) {
-                                       if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
-                                       {
-                                               PRINTK("Read_old block %d for r-m-w\n", i);
-                                               set_bit(R5_LOCKED, &dev->flags);
-                                               set_bit(R5_Wantread, &dev->flags);
-                                               locked++;
-                                       } else {
-                                               set_bit(STRIPE_DELAYED, &sh->state);
-                                               set_bit(STRIPE_HANDLE, &sh->state);
-                                       }
-                               }
-                       }
-               if (rcw <= rmw && rcw > 0)
-                       /* want reconstruct write, but need to get some data */
-                       for (i=disks; i--;) {
-                               dev = &sh->dev[i];
-                               if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
-                                   !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
-                                   test_bit(R5_Insync, &dev->flags)) {
-                                       if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
-                                       {
-                                               PRINTK("Read_old block %d for Reconstruct\n", i);
-                                               set_bit(R5_LOCKED, &dev->flags);
-                                               set_bit(R5_Wantread, &dev->flags);
-                                               locked++;
-                                       } else {
-                                               set_bit(STRIPE_DELAYED, &sh->state);
-                                               set_bit(STRIPE_HANDLE, &sh->state);
-                                       }
-                               }
-                       }
-               /* now if nothing is locked, and if we have enough data, we can start a write request */
-               if (locked == 0 && (rcw == 0 ||rmw == 0) &&
-                   !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
-                       PRINTK("Computing parity...\n");
-                       compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
-                       /* now every locked buffer is ready to be written */
-                       for (i=disks; i--;)
-                               if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
-                                       PRINTK("Writing block %d\n", i);
-                                       locked++;
-                                       set_bit(R5_Wantwrite, &sh->dev[i].flags);
-                                       if (!test_bit(R5_Insync, &sh->dev[i].flags)
-                                           || (i==sh->pd_idx && failed == 0))
-                                               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)
-                                       md_wakeup_thread(conf->mddev->thread);
-                       }
-               }
-       }
+       dev = &sh->dev[sh->pd_idx];
+       if ( s.written &&
+            ((test_bit(R5_Insync, &dev->flags) &&
+              !test_bit(R5_LOCKED, &dev->flags) &&
+              test_bit(R5_UPTODATE, &dev->flags)) ||
+              (s.failed == 1 && s.failed_num == sh->pd_idx)))
+               handle_stripe_clean_event(conf, sh, disks, &return_bi);
 
-       /* maybe we need to check and possibly fix the parity for this stripe
-        * Any reads will already have been scheduled, so we just see if enough data
-        * is available
+       /* Now we might consider reading some blocks, either to check/generate
+        * parity, or to satisfy requests
+        * or to load a block that is being partially written.
         */
-       if (syncing && locked == 0 &&
-           !test_bit(STRIPE_INSYNC, &sh->state)) {
-               set_bit(STRIPE_HANDLE, &sh->state);
-               if (failed == 0) {
-                       BUG_ON(uptodate != disks);
-                       compute_parity5(sh, CHECK_PARITY);
-                       uptodate--;
-                       if (page_is_zero(sh->dev[sh->pd_idx].page)) {
-                               /* parity is correct (on disc, not in buffer any more) */
-                               set_bit(STRIPE_INSYNC, &sh->state);
-                       } else {
-                               conf->mddev->resync_mismatches += STRIPE_SECTORS;
-                               if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
-                                       /* don't try to repair!! */
+       if (s.to_read || s.non_overwrite ||
+           (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
+               handle_stripe_fill5(sh, &s, disks);
+
+       /* Now we check to see if any write operations have recently
+        * completed
+        */
+       prexor = 0;
+       if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
+               prexor = 1;
+       if (sh->reconstruct_state == reconstruct_state_drain_result ||
+           sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
+               sh->reconstruct_state = reconstruct_state_idle;
+
+               /* All the 'written' buffers and the parity block are ready to
+                * be written back to disk
+                */
+               BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
+               for (i = disks; i--; ) {
+                       dev = &sh->dev[i];
+                       if (test_bit(R5_LOCKED, &dev->flags) &&
+                               (i == sh->pd_idx || dev->written)) {
+                               pr_debug("Writing block %d\n", i);
+                               set_bit(R5_Wantwrite, &dev->flags);
+                               if (prexor)
+                                       continue;
+                               if (!test_bit(R5_Insync, &dev->flags) ||
+                                   (i == sh->pd_idx && s.failed == 0))
                                        set_bit(STRIPE_INSYNC, &sh->state);
-                               else {
-                                       compute_block(sh, sh->pd_idx);
-                                       uptodate++;
-                               }
                        }
                }
-               if (!test_bit(STRIPE_INSYNC, &sh->state)) {
-                       /* either failed parity check, or recovery is happening */
-                       if (failed==0)
-                               failed_num = sh->pd_idx;
-                       dev = &sh->dev[failed_num];
-                       BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
-                       BUG_ON(uptodate != disks);
-
-                       set_bit(R5_LOCKED, &dev->flags);
-                       set_bit(R5_Wantwrite, &dev->flags);
-                       clear_bit(STRIPE_DEGRADED, &sh->state);
-                       locked++;
-                       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)
+                               md_wakeup_thread(conf->mddev->thread);
                }
        }
-       if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
+
+       /* Now to consider new write requests and what else, if anything
+        * should be read.  We do not handle new writes when:
+        * 1/ A 'write' operation (copy+xor) is already in flight.
+        * 2/ A 'check' operation is in flight, as it may clobber the parity
+        *    block.
+        */
+       if (s.to_write && !sh->reconstruct_state && !sh->check_state)
+               handle_stripe_dirtying5(conf, sh, &s, disks);
+
+       /* maybe we need to check and possibly fix the parity for this stripe
+        * Any reads will already have been scheduled, so we just see if enough
+        * data is available.  The parity check is held off while parity
+        * dependent operations are in flight.
+        */
+       if (sh->check_state ||
+           (s.syncing && s.locked == 0 &&
+            !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
+            !test_bit(STRIPE_INSYNC, &sh->state)))
+               handle_parity_checks5(conf, sh, &s, disks);
+
+       if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
                md_done_sync(conf->mddev, STRIPE_SECTORS,1);
                clear_bit(STRIPE_SYNCING, &sh->state);
        }
@@ -1776,182 +2729,96 @@ static void handle_stripe5(struct stripe_head *sh)
        /* If the failed drive is just a ReadError, then we might need to progress
         * the repair/check process
         */
-       if (failed == 1 && ! conf->mddev->ro &&
-           test_bit(R5_ReadError, &sh->dev[failed_num].flags)
-           && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
-           && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
+       if (s.failed == 1 && !conf->mddev->ro &&
+           test_bit(R5_ReadError, &sh->dev[s.failed_num].flags)
+           && !test_bit(R5_LOCKED, &sh->dev[s.failed_num].flags)
+           && test_bit(R5_UPTODATE, &sh->dev[s.failed_num].flags)
                ) {
-               dev = &sh->dev[failed_num];
+               dev = &sh->dev[s.failed_num];
                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);
-                       locked++;
+                       s.locked++;
                } else {
                        /* let's read it back */
                        set_bit(R5_Wantread, &dev->flags);
                        set_bit(R5_LOCKED, &dev->flags);
-                       locked++;
+                       s.locked++;
                }
        }
 
-       if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
-               /* Need to write out all blocks after computing parity */
-               sh->disks = conf->raid_disks;
-               sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
-               compute_parity5(sh, RECONSTRUCT_WRITE);
-               for (i= conf->raid_disks; i--;) {
-                       set_bit(R5_LOCKED, &sh->dev[i].flags);
-                       locked++;
+       /* Finish reconstruct operations initiated by the expansion process */
+       if (sh->reconstruct_state == reconstruct_state_result) {
+               sh->reconstruct_state = reconstruct_state_idle;
+               clear_bit(STRIPE_EXPANDING, &sh->state);
+               for (i = conf->raid_disks; i--; ) {
                        set_bit(R5_Wantwrite, &sh->dev[i].flags);
+                       set_bit(R5_LOCKED, &sh->dev[i].flags);
+                       s.locked++;
                }
-               clear_bit(STRIPE_EXPANDING, &sh->state);
-       } else if (expanded) {
+       }
+
+       if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
+           !sh->reconstruct_state) {
+               /* Need to write out all blocks after computing parity */
+               sh->disks = conf->raid_disks;
+               sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
+                       conf->raid_disks);
+               schedule_reconstruction5(sh, &s, 1, 1);
+       } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
                clear_bit(STRIPE_EXPAND_READY, &sh->state);
                atomic_dec(&conf->reshape_stripes);
                wake_up(&conf->wait_for_overlap);
                md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
        }
 
-       if (expanding && locked == 0) {
-               /* We have read all the blocks in this stripe and now we need to
-                * copy some of them into a target stripe for expand.
-                */
-               clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
-               for (i=0; i< sh->disks; i++)
-                       if (i != sh->pd_idx) {
-                               int dd_idx, pd_idx, j;
-                               struct stripe_head *sh2;
-
-                               sector_t bn = compute_blocknr(sh, i);
-                               sector_t s = raid5_compute_sector(bn, conf->raid_disks,
-                                                                 conf->raid_disks-1,
-                                                                 &dd_idx, &pd_idx, conf);
-                               sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
-                               if (sh2 == NULL)
-                                       /* so far only the early blocks of this stripe
-                                        * have been requested.  When later blocks
-                                        * get requested, we will try again
-                                        */
-                                       continue;
-                               if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
-                                  test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
-                                       /* must have already done this block */
-                                       release_stripe(sh2);
-                                       continue;
-                               }
-                               memcpy(page_address(sh2->dev[dd_idx].page),
-                                      page_address(sh->dev[i].page),
-                                      STRIPE_SIZE);
-                               set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
-                               set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
-                               for (j=0; j<conf->raid_disks; j++)
-                                       if (j != sh2->pd_idx &&
-                                           !test_bit(R5_Expanded, &sh2->dev[j].flags))
-                                               break;
-                               if (j == conf->raid_disks) {
-                                       set_bit(STRIPE_EXPAND_READY, &sh2->state);
-                                       set_bit(STRIPE_HANDLE, &sh2->state);
-                               }
-                               release_stripe(sh2);
-                       }
-       }
+       if (s.expanding && s.locked == 0 &&
+           !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
+               handle_stripe_expansion(conf, sh, NULL);
 
+ unlock:
        spin_unlock(&sh->lock);
 
-       while ((bi=return_bi)) {
-               int bytes = bi->bi_size;
+       /* wait for this device to become unblocked */
+       if (unlikely(blocked_rdev))
+               md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
 
-               return_bi = bi->bi_next;
-               bi->bi_next = NULL;
-               bi->bi_size = 0;
-               bi->bi_end_io(bi, bytes, 0);
-       }
-       for (i=disks; i-- ;) {
-               int rw;
-               struct bio *bi;
-               mdk_rdev_t *rdev;
-               if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
-                       rw = 1;
-               else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
-                       rw = 0;
-               else
-                       continue;
-               bi = &sh->dev[i].req;
-               bi->bi_rw = rw;
-               if (rw)
-                       bi->bi_end_io = raid5_end_write_request;
-               else
-                       bi->bi_end_io = raid5_end_read_request;
-               rcu_read_lock();
-               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 (syncing || expanding || expanded)
-                               md_sync_acct(rdev->bdev, STRIPE_SECTORS);
+       if (s.ops_request)
+               raid5_run_ops(sh, s.ops_request);
 
-                       bi->bi_bdev = rdev->bdev;
-                       PRINTK("for %llu schedule op %ld on disc %d\n",
-                               (unsigned long long)sh->sector, bi->bi_rw, i);
-                       atomic_inc(&sh->count);
-                       bi->bi_sector = sh->sector + rdev->data_offset;
-                       bi->bi_flags = 1 << BIO_UPTODATE;
-                       bi->bi_vcnt = 1;        
-                       bi->bi_max_vecs = 1;
-                       bi->bi_idx = 0;
-                       bi->bi_io_vec = &sh->dev[i].vec;
-                       bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
-                       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);
-                       set_bit(STRIPE_HANDLE, &sh->state);
-               }
-       }
+       ops_run_io(sh, &s);
+
+       return_io(return_bi);
+
+       return blocked_rdev == NULL;
 }
 
-static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
+static bool handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
 {
        raid6_conf_t *conf = sh->raid_conf;
-       int disks = conf->raid_disks;
-       struct bio *return_bi= NULL;
-       struct bio *bi;
-       int i;
-       int syncing;
-       int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
-       int non_overwrite = 0;
-       int failed_num[2] = {0, 0};
+       int disks = sh->disks;
+       struct bio *return_bi = NULL;
+       int i, pd_idx = sh->pd_idx;
+       struct stripe_head_state s;
+       struct r6_state r6s;
        struct r5dev *dev, *pdev, *qdev;
-       int pd_idx = sh->pd_idx;
-       int qd_idx = raid6_next_disk(pd_idx, disks);
-       int p_failed, q_failed;
+       mdk_rdev_t *blocked_rdev = NULL;
 
-       PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
-              (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
-              pd_idx, qd_idx);
+       r6s.qd_idx = raid6_next_disk(pd_idx, disks);
+       pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
+               "pd_idx=%d, qd_idx=%d\n",
+              (unsigned long long)sh->sector, sh->state,
+              atomic_read(&sh->count), pd_idx, r6s.qd_idx);
+       memset(&s, 0, sizeof(s));
 
        spin_lock(&sh->lock);
        clear_bit(STRIPE_HANDLE, &sh->state);
        clear_bit(STRIPE_DELAYED, &sh->state);
 
-       syncing = test_bit(STRIPE_SYNCING, &sh->state);
+       s.syncing = test_bit(STRIPE_SYNCING, &sh->state);
+       s.expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
+       s.expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
        /* Now to look around and see what can be done */
 
        rcu_read_lock();
@@ -1960,12 +2827,12 @@ static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
                dev = &sh->dev[i];
                clear_bit(R5_Insync, &dev->flags);
 
-               PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
+               pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
                        i, dev->flags, dev->toread, dev->towrite, dev->written);
                /* maybe we can reply to a read */
                if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
                        struct bio *rbi, *rbi2;
-                       PRINTK("Return read for disc %d\n", i);
+                       pr_debug("Return read for disc %d\n", i);
                        spin_lock_irq(&conf->device_lock);
                        rbi = dev->toread;
                        dev->toread = NULL;
@@ -1976,7 +2843,7 @@ static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
                                copy_data(0, rbi, dev->page, dev->sector);
                                rbi2 = r5_next_bio(rbi, dev->sector);
                                spin_lock_irq(&conf->device_lock);
-                               if (--rbi->bi_phys_segments == 0) {
+                               if (!raid5_dec_bi_phys_segments(rbi)) {
                                        rbi->bi_next = return_bi;
                                        return_bi = rbi;
                                }
@@ -1986,18 +2853,25 @@ static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
                }
 
                /* now count some things */
-               if (test_bit(R5_LOCKED, &dev->flags)) locked++;
-               if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
+               if (test_bit(R5_LOCKED, &dev->flags)) s.locked++;
+               if (test_bit(R5_UPTODATE, &dev->flags)) s.uptodate++;
 
 
-               if (dev->toread) to_read++;
+               if (dev->toread)
+                       s.to_read++;
                if (dev->towrite) {
-                       to_write++;
+                       s.to_write++;
                        if (!test_bit(R5_OVERWRITE, &dev->flags))
-                               non_overwrite++;
+                               s.non_overwrite++;
                }
-               if (dev->written) written++;
+               if (dev->written)
+                       s.written++;
                rdev = rcu_dereference(conf->disks[i].rdev);
+               if (blocked_rdev == NULL &&
+                   rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
+                       blocked_rdev = rdev;
+                       atomic_inc(&rdev->nr_pending);
+               }
                if (!rdev || !test_bit(In_sync, &rdev->flags)) {
                        /* The ReadError flag will just be confusing now */
                        clear_bit(R5_ReadError, &dev->flags);
@@ -2005,96 +2879,38 @@ static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
                }
                if (!rdev || !test_bit(In_sync, &rdev->flags)
                    || test_bit(R5_ReadError, &dev->flags)) {
-                       if ( failed < 2 )
-                               failed_num[failed] = i;
-                       failed++;
+                       if (s.failed < 2)
+                               r6s.failed_num[s.failed] = i;
+                       s.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,
-              failed_num[0], failed_num[1]);
-       /* check if the array has lost >2 devices and, if so, some requests might
-        * need to be failed
-        */
-       if (failed > 2 && to_read+to_write+written) {
-               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--; bitmap_end = 1; }
-
-                       if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
-                               wake_up(&conf->wait_for_overlap);
-
-                       while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
-                               struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
-                               clear_bit(BIO_UPTODATE, &bi->bi_flags);
-                               if (--bi->bi_phys_segments == 0) {
-                                       md_write_end(conf->mddev);
-                                       bi->bi_next = return_bi;
-                                       return_bi = bi;
-                               }
-                               bi = nextbi;
-                       }
-                       /* 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);
-                               if (--bi->bi_phys_segments == 0) {
-                                       md_write_end(conf->mddev);
-                                       bi->bi_next = return_bi;
-                                       return_bi = bi;
-                               }
-                               bi = bi2;
-                       }
 
-                       /* fail any reads if this device is non-operational */
-                       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))
-                                       wake_up(&conf->wait_for_overlap);
-                               if (bi) to_read--;
-                               while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
-                                       struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
-                                       clear_bit(BIO_UPTODATE, &bi->bi_flags);
-                                       if (--bi->bi_phys_segments == 0) {
-                                               bi->bi_next = return_bi;
-                                               return_bi = bi;
-                                       }
-                                       bi = nextbi;
-                               }
-                       }
-                       spin_unlock_irq(&conf->device_lock);
-                       if (bitmap_end)
-                               bitmap_endwrite(conf->mddev->bitmap, sh->sector,
-                                               STRIPE_SECTORS, 0, 0);
+       if (unlikely(blocked_rdev)) {
+               if (s.syncing || s.expanding || s.expanded ||
+                   s.to_write || s.written) {
+                       set_bit(STRIPE_HANDLE, &sh->state);
+                       goto unlock;
                }
+               /* There is nothing for the blocked_rdev to block */
+               rdev_dec_pending(blocked_rdev, conf->mddev);
+               blocked_rdev = NULL;
        }
-       if (failed > 2 && syncing) {
+
+       pr_debug("locked=%d uptodate=%d to_read=%d"
+              " to_write=%d failed=%d failed_num=%d,%d\n",
+              s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
+              r6s.failed_num[0], r6s.failed_num[1]);
+       /* check if the array has lost >2 devices and, if so, some requests
+        * might need to be failed
+        */
+       if (s.failed > 2 && s.to_read+s.to_write+s.written)
+               handle_failed_stripe(conf, sh, &s, disks, &return_bi);
+       if (s.failed > 2 && s.syncing) {
                md_done_sync(conf->mddev, STRIPE_SECTORS,0);
                clear_bit(STRIPE_SYNCING, &sh->state);
-               syncing = 0;
+               s.syncing = 0;
        }
 
        /*
@@ -2102,289 +2918,41 @@ static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
         * are safe, or on a failed drive
         */
        pdev = &sh->dev[pd_idx];
-       p_failed = (failed >= 1 && failed_num[0] == pd_idx)
-               || (failed >= 2 && failed_num[1] == pd_idx);
-       qdev = &sh->dev[qd_idx];
-       q_failed = (failed >= 1 && failed_num[0] == qd_idx)
-               || (failed >= 2 && failed_num[1] == qd_idx);
-
-       if ( written &&
-            ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
+       r6s.p_failed = (s.failed >= 1 && r6s.failed_num[0] == pd_idx)
+               || (s.failed >= 2 && r6s.failed_num[1] == pd_idx);
+       qdev = &sh->dev[r6s.qd_idx];
+       r6s.q_failed = (s.failed >= 1 && r6s.failed_num[0] == r6s.qd_idx)
+               || (s.failed >= 2 && r6s.failed_num[1] == r6s.qd_idx);
+
+       if ( s.written &&
+            ( r6s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
                             && !test_bit(R5_LOCKED, &pdev->flags)
-                            && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
-            ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
+                            && test_bit(R5_UPTODATE, &pdev->flags)))) &&
+            ( r6s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
                             && !test_bit(R5_LOCKED, &qdev->flags)
-                            && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
-               /* any written block on an uptodate or failed drive can be
-                * returned.  Note that if we 'wrote' to a failed drive,
-                * it will be UPTODATE, but never LOCKED, so we don't need
-                * to test 'failed' directly.
-                */
-               for (i=disks; i--; )
-                       if (sh->dev[i].written) {
-                               dev = &sh->dev[i];
-                               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);
-                                       spin_lock_irq(&conf->device_lock);
-                                       wbi = dev->written;
-                                       dev->written = NULL;
-                                       while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
-                                               wbi2 = r5_next_bio(wbi, dev->sector);
-                                               if (--wbi->bi_phys_segments == 0) {
-                                                       md_write_end(conf->mddev);
-                                                       wbi->bi_next = return_bi;
-                                                       return_bi = wbi;
-                                               }
-                                               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);
-                               }
-                       }
-       }
+                            && test_bit(R5_UPTODATE, &qdev->flags)))))
+               handle_stripe_clean_event(conf, sh, disks, &return_bi);
 
        /* Now we might consider reading some blocks, either to check/generate
         * parity, or to satisfy requests
         * or to load a block that is being partially written.
         */
-       if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
-               for (i=disks; i--;) {
-                       dev = &sh->dev[i];
-                       if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
-                           (dev->toread ||
-                            (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
-                            syncing ||
-                            (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
-                            (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
-                                   )
-                               ) {
-                               /* we would like to get this block, possibly
-                                * by computing it, but we might not be able to
-                                */
-                               if (uptodate == disks-1) {
-                                       PRINTK("Computing stripe %llu block %d\n",
-                                              (unsigned long long)sh->sector, 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 */
-                                       int other;
-                                       for (other=disks; other--;) {
-                                               if ( other == i )
-                                                       continue;
-                                               if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
-                                                       break;
-                                       }
-                                       BUG_ON(other < 0);
-                                       PRINTK("Computing stripe %llu blocks %d,%d\n",
-                                              (unsigned long long)sh->sector, i, other);
-                                       compute_block_2(sh, i, other);
-                                       uptodate += 2;
-                               } else if (test_bit(R5_Insync, &dev->flags)) {
-                                       set_bit(R5_LOCKED, &dev->flags);
-                                       set_bit(R5_Wantread, &dev->flags);
-#if 0
-                                       /* if I am just reading this block and we don't have
-                                          a failed drive, or any pending writes then sidestep the cache */
-                                       if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
-                                           ! syncing && !failed && !to_write) {
-                                               sh->bh_cache[i]->b_page =  sh->bh_read[i]->b_page;
-                                               sh->bh_cache[i]->b_data =  sh->bh_read[i]->b_data;
-                                       }
-#endif
-                                       locked++;
-                                       PRINTK("Reading block %d (sync=%d)\n",
-                                               i, syncing);
-                               }
-                       }
-               }
-               set_bit(STRIPE_HANDLE, &sh->state);
-       }
+       if (s.to_read || s.non_overwrite || (s.to_write && s.failed) ||
+           (s.syncing && (s.uptodate < disks)) || s.expanding)
+               handle_stripe_fill6(sh, &s, &r6s, disks);
 
        /* now to consider writing and what else, if anything should be read */
-       if (to_write) {
-               int rcw=0, must_compute=0;
-               for (i=disks ; i--;) {
-                       dev = &sh->dev[i];
-                       /* Would I have to read this buffer for reconstruct_write */
-                       if (!test_bit(R5_OVERWRITE, &dev->flags)
-                           && i != pd_idx && i != qd_idx
-                           && (!test_bit(R5_LOCKED, &dev->flags)
-#if 0
-                               || sh->bh_page[i] != bh->b_page
-#endif
-                                   ) &&
-                           !test_bit(R5_UPTODATE, &dev->flags)) {
-                               if (test_bit(R5_Insync, &dev->flags)) rcw++;
-                               else {
-                                       PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
-                                       must_compute++;
-                               }
-                       }
-               }
-               PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
-                      (unsigned long long)sh->sector, rcw, must_compute);
-               set_bit(STRIPE_HANDLE, &sh->state);
-
-               if (rcw > 0)
-                       /* want reconstruct write, but need to get some data */
-                       for (i=disks; i--;) {
-                               dev = &sh->dev[i];
-                               if (!test_bit(R5_OVERWRITE, &dev->flags)
-                                   && !(failed == 0 && (i == pd_idx || i == qd_idx))
-                                   && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
-                                   test_bit(R5_Insync, &dev->flags)) {
-                                       if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
-                                       {
-                                               PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
-                                                      (unsigned long long)sh->sector, i);
-                                               set_bit(R5_LOCKED, &dev->flags);
-                                               set_bit(R5_Wantread, &dev->flags);
-                                               locked++;
-                                       } else {
-                                               PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
-                                                      (unsigned long long)sh->sector, i);
-                                               set_bit(STRIPE_DELAYED, &sh->state);
-                                               set_bit(STRIPE_HANDLE, &sh->state);
-                                       }
-                               }
-                       }
-               /* now if nothing is locked, and if we have enough data, we can start a write request */
-               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], 0); break;
-                               case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
-                               default: BUG(); /* This request should have been failed? */
-                               }
-                       }
-
-                       PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
-                       compute_parity6(sh, RECONSTRUCT_WRITE);
-                       /* now every locked buffer is ready to be written */
-                       for (i=disks; i--;)
-                               if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
-                                       PRINTK("Writing stripe %llu block %d\n",
-                                              (unsigned long long)sh->sector, i);
-                                       locked++;
-                                       set_bit(R5_Wantwrite, &sh->dev[i].flags);
-                               }
-                       /* 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)
-                                       md_wakeup_thread(conf->mddev->thread);
-                       }
-               }
-       }
+       if (s.to_write)
+               handle_stripe_dirtying6(conf, sh, &s, &r6s, disks);
 
        /* maybe we need to check and possibly fix the parity for this stripe
-        * Any reads will already have been scheduled, so we just see if enough data
-        * is available
+        * 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)) {
-               int update_p = 0, update_q = 0;
-               struct r5dev *dev;
-
-               set_bit(STRIPE_HANDLE, &sh->state);
-
-               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_parity6(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;
-                       }
-
-                       /* now write out any block on a failed drive,
-                        * or P or Q if they need it
-                        */
-
-                       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);
-                       }
-
-                       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);
-               }
-       }
+       if (s.syncing && s.locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state))
+               handle_parity_checks6(conf, sh, &s, &r6s, tmp_page, disks);
 
-       if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
+       if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
                md_done_sync(conf->mddev, STRIPE_SECTORS,1);
                clear_bit(STRIPE_SYNCING, &sh->state);
        }
@@ -2392,9 +2960,9 @@ static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
        /* 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 (s.failed <= 2 && !conf->mddev->ro)
+               for (i = 0; i < s.failed; i++) {
+                       dev = &sh->dev[r6s.failed_num[i]];
                        if (test_bit(R5_ReadError, &dev->flags)
                            && !test_bit(R5_LOCKED, &dev->flags)
                            && test_bit(R5_UPTODATE, &dev->flags)
@@ -2410,82 +2978,51 @@ static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
                                }
                        }
                }
-       spin_unlock(&sh->lock);
-
-       while ((bi=return_bi)) {
-               int bytes = bi->bi_size;
-
-               return_bi = bi->bi_next;
-               bi->bi_next = NULL;
-               bi->bi_size = 0;
-               bi->bi_end_io(bi, bytes, 0);
-       }
-       for (i=disks; i-- ;) {
-               int rw;
-               struct bio *bi;
-               mdk_rdev_t *rdev;
-               if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
-                       rw = 1;
-               else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
-                       rw = 0;
-               else
-                       continue;
-
-               bi = &sh->dev[i].req;
-
-               bi->bi_rw = rw;
-               if (rw)
-                       bi->bi_end_io = raid5_end_write_request;
-               else
-                       bi->bi_end_io = raid5_end_read_request;
-
-               rcu_read_lock();
-               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 (syncing)
-                               md_sync_acct(rdev->bdev, STRIPE_SECTORS);
-
-                       bi->bi_bdev = rdev->bdev;
-                       PRINTK("for %llu schedule op %ld on disc %d\n",
-                               (unsigned long long)sh->sector, bi->bi_rw, i);
-                       atomic_inc(&sh->count);
-                       bi->bi_sector = sh->sector + rdev->data_offset;
-                       bi->bi_flags = 1 << BIO_UPTODATE;
-                       bi->bi_vcnt = 1;
-                       bi->bi_max_vecs = 1;
-                       bi->bi_idx = 0;
-                       bi->bi_io_vec = &sh->dev[i].vec;
-                       bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
-                       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);
-                       set_bit(STRIPE_HANDLE, &sh->state);
-               }
+
+       if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
+               /* Need to write out all blocks after computing P&Q */
+               sh->disks = conf->raid_disks;
+               sh->pd_idx = stripe_to_pdidx(sh->sector, conf,
+                                            conf->raid_disks);
+               compute_parity6(sh, RECONSTRUCT_WRITE);
+               for (i = conf->raid_disks ; i-- ;  ) {
+                       set_bit(R5_LOCKED, &sh->dev[i].flags);
+                       s.locked++;
+                       set_bit(R5_Wantwrite, &sh->dev[i].flags);
+               }
+               clear_bit(STRIPE_EXPANDING, &sh->state);
+       } else if (s.expanded) {
+               clear_bit(STRIPE_EXPAND_READY, &sh->state);
+               atomic_dec(&conf->reshape_stripes);
+               wake_up(&conf->wait_for_overlap);
+               md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
        }
+
+       if (s.expanding && s.locked == 0 &&
+           !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
+               handle_stripe_expansion(conf, sh, &r6s);
+
+ unlock:
+       spin_unlock(&sh->lock);
+
+       /* wait for this device to become unblocked */
+       if (unlikely(blocked_rdev))
+               md_wait_for_blocked_rdev(blocked_rdev, conf->mddev);
+
+       ops_run_io(sh, &s);
+
+       return_io(return_bi);
+
+       return blocked_rdev == NULL;
 }
 
-static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
+/* returns true if the stripe was handled */
+static bool handle_stripe(struct stripe_head *sh, struct page *tmp_page)
 {
        if (sh->raid_conf->level == 6)
-               handle_stripe6(sh, tmp_page);
+               return handle_stripe6(sh, tmp_page);
        else
-               handle_stripe5(sh);
+               return handle_stripe5(sh);
 }
 
 
@@ -2501,9 +3038,10 @@ static void raid5_activate_delayed(raid5_conf_t *conf)
                        clear_bit(STRIPE_DELAYED, &sh->state);
                        if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
                                atomic_inc(&conf->preread_active_stripes);
-                       list_add_tail(&sh->lru, &conf->handle_list);
+                       list_add_tail(&sh->lru, &conf->hold_list);
                }
-       }
+       } else
+               blk_plug_device(conf->mddev->queue);
 }
 
 static void activate_bit_delay(raid5_conf_t *conf)
@@ -2529,13 +3067,12 @@ static void unplug_slaves(mddev_t *mddev)
        for (i=0; i<mddev->raid_disks; i++) {
                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);
+                       struct request_queue *r_queue = bdev_get_queue(rdev->bdev);
 
                        atomic_inc(&rdev->nr_pending);
                        rcu_read_unlock();
 
-                       if (r_queue->unplug_fn)
-                               r_queue->unplug_fn(r_queue);
+                       blk_unplug(r_queue);
 
                        rdev_dec_pending(rdev, mddev);
                        rcu_read_lock();
@@ -2544,7 +3081,7 @@ static void unplug_slaves(mddev_t *mddev)
        rcu_read_unlock();
 }
 
-static void raid5_unplug_device(request_queue_t *q)
+static void raid5_unplug_device(struct request_queue *q)
 {
        mddev_t *mddev = q->queuedata;
        raid5_conf_t *conf = mddev_to_conf(mddev);
@@ -2563,36 +3100,6 @@ static void raid5_unplug_device(request_queue_t *q)
        unplug_slaves(mddev);
 }
 
-static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
-                            sector_t *error_sector)
-{
-       mddev_t *mddev = q->queuedata;
-       raid5_conf_t *conf = mddev_to_conf(mddev);
-       int i, ret = 0;
-
-       rcu_read_lock();
-       for (i=0; i<mddev->raid_disks && ret == 0; i++) {
-               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);
-
-                       if (!r_queue->issue_flush_fn)
-                               ret = -EOPNOTSUPP;
-                       else {
-                               atomic_inc(&rdev->nr_pending);
-                               rcu_read_unlock();
-                               ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
-                                                             error_sector);
-                               rdev_dec_pending(rdev, mddev);
-                               rcu_read_lock();
-                       }
-               }
-       }
-       rcu_read_unlock();
-       return ret;
-}
-
 static int raid5_congested(void *data, int bits)
 {
        mddev_t *mddev = data;
@@ -2611,7 +3118,262 @@ static int raid5_congested(void *data, int bits)
        return 0;
 }
 
-static int make_request(request_queue_t *q, struct bio * bi)
+/* We want read requests to align with chunks where possible,
+ * but write requests don't need to.
+ */
+static int raid5_mergeable_bvec(struct request_queue *q,
+                               struct bvec_merge_data *bvm,
+                               struct bio_vec *biovec)
+{
+       mddev_t *mddev = q->queuedata;
+       sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
+       int max;
+       unsigned int chunk_sectors = mddev->chunk_size >> 9;
+       unsigned int bio_sectors = bvm->bi_size >> 9;
+
+       if ((bvm->bi_rw & 1) == WRITE)
+               return biovec->bv_len; /* always allow writes to be mergeable */
+
+       max =  (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
+       if (max < 0) max = 0;
+       if (max <= biovec->bv_len && bio_sectors == 0)
+               return biovec->bv_len;
+       else
+               return max;
+}
+
+
+static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
+{
+       sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
+       unsigned int chunk_sectors = mddev->chunk_size >> 9;
+       unsigned int bio_sectors = bio->bi_size >> 9;
+
+       return  chunk_sectors >=
+               ((sector & (chunk_sectors - 1)) + bio_sectors);
+}
+
+/*
+ *  add bio to the retry LIFO  ( in O(1) ... we are in interrupt )
+ *  later sampled by raid5d.
+ */
+static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
+{
+       unsigned long flags;
+
+       spin_lock_irqsave(&conf->device_lock, flags);
+
+       bi->bi_next = conf->retry_read_aligned_list;
+       conf->retry_read_aligned_list = bi;
+
+       spin_unlock_irqrestore(&conf->device_lock, flags);
+       md_wakeup_thread(conf->mddev->thread);
+}
+
+
+static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
+{
+       struct bio *bi;
+
+       bi = conf->retry_read_aligned;
+       if (bi) {
+               conf->retry_read_aligned = NULL;
+               return bi;
+       }
+       bi = conf->retry_read_aligned_list;
+       if(bi) {
+               conf->retry_read_aligned_list = bi->bi_next;
+               bi->bi_next = NULL;
+               /*
+                * this sets the active strip count to 1 and the processed
+                * strip count to zero (upper 8 bits)
+                */
+               bi->bi_phys_segments = 1; /* biased count of active stripes */
+       }
+
+       return bi;
+}
+
+
+/*
+ *  The "raid5_align_endio" should check if the read succeeded and if it
+ *  did, call bio_endio on the original bio (having bio_put the new bio
+ *  first).
+ *  If the read failed..
+ */
+static void raid5_align_endio(struct bio *bi, int error)
+{
+       struct bio* raid_bi  = bi->bi_private;
+       mddev_t *mddev;
+       raid5_conf_t *conf;
+       int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
+       mdk_rdev_t *rdev;
+
+       bio_put(bi);
+
+       mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
+       conf = mddev_to_conf(mddev);
+       rdev = (void*)raid_bi->bi_next;
+       raid_bi->bi_next = NULL;
+
+       rdev_dec_pending(rdev, conf->mddev);
+
+       if (!error && uptodate) {
+               bio_endio(raid_bi, 0);
+               if (atomic_dec_and_test(&conf->active_aligned_reads))
+                       wake_up(&conf->wait_for_stripe);
+               return;
+       }
+
+
+       pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
+
+       add_bio_to_retry(raid_bi, conf);
+}
+
+static int bio_fits_rdev(struct bio *bi)
+{
+       struct request_queue *q = bdev_get_queue(bi->bi_bdev);
+
+       if ((bi->bi_size>>9) > q->max_sectors)
+               return 0;
+       blk_recount_segments(q, bi);
+       if (bi->bi_phys_segments > q->max_phys_segments)
+               return 0;
+
+       if (q->merge_bvec_fn)
+               /* it's too hard to apply the merge_bvec_fn at this stage,
+                * just just give up
+                */
+               return 0;
+
+       return 1;
+}
+
+
+static int chunk_aligned_read(struct request_queue *q, struct bio * raid_bio)
+{
+       mddev_t *mddev = q->queuedata;
+       raid5_conf_t *conf = mddev_to_conf(mddev);
+       const unsigned int raid_disks = conf->raid_disks;
+       const unsigned int data_disks = raid_disks - conf->max_degraded;
+       unsigned int dd_idx, pd_idx;
+       struct bio* align_bi;
+       mdk_rdev_t *rdev;
+
+       if (!in_chunk_boundary(mddev, raid_bio)) {
+               pr_debug("chunk_aligned_read : non aligned\n");
+               return 0;
+       }
+       /*
+        * use bio_clone to make a copy of the bio
+        */
+       align_bi = bio_clone(raid_bio, GFP_NOIO);
+       if (!align_bi)
+               return 0;
+       /*
+        *   set bi_end_io to a new function, and set bi_private to the
+        *     original bio.
+        */
+       align_bi->bi_end_io  = raid5_align_endio;
+       align_bi->bi_private = raid_bio;
+       /*
+        *      compute position
+        */
+       align_bi->bi_sector =  raid5_compute_sector(raid_bio->bi_sector,
+                                       raid_disks,
+                                       data_disks,
+                                       &dd_idx,
+                                       &pd_idx,
+                                       conf);
+
+       rcu_read_lock();
+       rdev = rcu_dereference(conf->disks[dd_idx].rdev);
+       if (rdev && test_bit(In_sync, &rdev->flags)) {
+               atomic_inc(&rdev->nr_pending);
+               rcu_read_unlock();
+               raid_bio->bi_next = (void*)rdev;
+               align_bi->bi_bdev =  rdev->bdev;
+               align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
+               align_bi->bi_sector += rdev->data_offset;
+
+               if (!bio_fits_rdev(align_bi)) {
+                       /* too big in some way */
+                       bio_put(align_bi);
+                       rdev_dec_pending(rdev, mddev);
+                       return 0;
+               }
+
+               spin_lock_irq(&conf->device_lock);
+               wait_event_lock_irq(conf->wait_for_stripe,
+                                   conf->quiesce == 0,
+                                   conf->device_lock, /* nothing */);
+               atomic_inc(&conf->active_aligned_reads);
+               spin_unlock_irq(&conf->device_lock);
+
+               generic_make_request(align_bi);
+               return 1;
+       } else {
+               rcu_read_unlock();
+               bio_put(align_bi);
+               return 0;
+       }
+}
+
+/* __get_priority_stripe - get the next stripe to process
+ *
+ * Full stripe writes are allowed to pass preread active stripes up until
+ * the bypass_threshold is exceeded.  In general the bypass_count
+ * increments when the handle_list is handled before the hold_list; however, it
+ * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
+ * stripe with in flight i/o.  The bypass_count will be reset when the
+ * head of the hold_list has changed, i.e. the head was promoted to the
+ * handle_list.
+ */
+static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
+{
+       struct stripe_head *sh;
+
+       pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
+                 __func__,
+                 list_empty(&conf->handle_list) ? "empty" : "busy",
+                 list_empty(&conf->hold_list) ? "empty" : "busy",
+                 atomic_read(&conf->pending_full_writes), conf->bypass_count);
+
+       if (!list_empty(&conf->handle_list)) {
+               sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
+
+               if (list_empty(&conf->hold_list))
+                       conf->bypass_count = 0;
+               else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
+                       if (conf->hold_list.next == conf->last_hold)
+                               conf->bypass_count++;
+                       else {
+                               conf->last_hold = conf->hold_list.next;
+                               conf->bypass_count -= conf->bypass_threshold;
+                               if (conf->bypass_count < 0)
+                                       conf->bypass_count = 0;
+                       }
+               }
+       } else if (!list_empty(&conf->hold_list) &&
+                  ((conf->bypass_threshold &&
+                    conf->bypass_count > conf->bypass_threshold) ||
+                   atomic_read(&conf->pending_full_writes) == 0)) {
+               sh = list_entry(conf->hold_list.next,
+                               typeof(*sh), lru);
+               conf->bypass_count -= conf->bypass_threshold;
+               if (conf->bypass_count < 0)
+                       conf->bypass_count = 0;
+       } else
+               return NULL;
+
+       list_del_init(&sh->lru);
+       atomic_inc(&sh->count);
+       BUG_ON(atomic_read(&sh->count) != 1);
+       return sh;
+}
+
+static int make_request(struct request_queue *q, struct bio * bi)
 {
        mddev_t *mddev = q->queuedata;
        raid5_conf_t *conf = mddev_to_conf(mddev);
@@ -2620,17 +3382,25 @@ static int make_request(request_queue_t *q, struct bio * bi)
        sector_t logical_sector, last_sector;
        struct stripe_head *sh;
        const int rw = bio_data_dir(bi);
-       int remaining;
+       int cpu, remaining;
 
        if (unlikely(bio_barrier(bi))) {
-               bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
+               bio_endio(bi, -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));
+       cpu = part_stat_lock();
+       part_stat_inc(cpu, &mddev->gendisk->part0, ios[rw]);
+       part_stat_add(cpu, &mddev->gendisk->part0, sectors[rw],
+                     bio_sectors(bi));
+       part_stat_unlock();
+
+       if (rw == READ &&
+            mddev->reshape_position == MaxSector &&
+            chunk_aligned_read(q,bi))
+               return 0;
 
        logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
        last_sector = bi->bi_sector + (bi->bi_size>>9);
@@ -2671,7 +3441,7 @@ static int make_request(request_queue_t *q, struct bio * bi)
 
                new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
                                                  &dd_idx, &pd_idx, conf);
-               PRINTK("raid5: make_request, sector %llu logical %llu\n",
+               pr_debug("raid5: make_request, sector %llu logical %llu\n",
                        (unsigned long long)new_sector, 
                        (unsigned long long)logical_sector);
 
@@ -2720,7 +3490,8 @@ static int make_request(request_queue_t *q, struct bio * bi)
                                goto retry;
                        }
                        finish_wait(&conf->wait_for_overlap, &w);
-                       handle_stripe(sh, NULL);
+                       set_bit(STRIPE_HANDLE, &sh->state);
+                       clear_bit(STRIPE_DELAYED, &sh->state);
                        release_stripe(sh);
                } else {
                        /* cannot get stripe for read-ahead, just give-up */
@@ -2731,15 +3502,14 @@ static int make_request(request_queue_t *q, struct bio * bi)
                        
        }
        spin_lock_irq(&conf->device_lock);
-       remaining = --bi->bi_phys_segments;
+       remaining = raid5_dec_bi_phys_segments(bi);
        spin_unlock_irq(&conf->device_lock);
        if (remaining == 0) {
-               int bytes = bi->bi_size;
 
                if ( rw == WRITE )
                        md_write_end(mddev);
-               bi->bi_size = 0;
-               bi->bi_end_io(bi, bytes, 0);
+
+               bio_endio(bi, 0);
        }
        return 0;
 }
@@ -2759,8 +3529,9 @@ static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped
        struct stripe_head *sh;
        int pd_idx;
        sector_t first_sector, last_sector;
-       int raid_disks;
-       int data_disks;
+       int raid_disks = conf->previous_raid_disks;
+       int data_disks = raid_disks - conf->max_degraded;
+       int new_data_disks = conf->raid_disks - conf->max_degraded;
        int i;
        int dd_idx;
        sector_t writepos, safepos, gap;
@@ -2769,7 +3540,7 @@ static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped
            conf->expand_progress != 0) {
                /* restarting in the middle, skip the initial sectors */
                sector_nr = conf->expand_progress;
-               sector_div(sector_nr, conf->raid_disks-1);
+               sector_div(sector_nr, new_data_disks);
                *skipped = 1;
                return sector_nr;
        }
@@ -2783,14 +3554,14 @@ static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped
         * to after where expand_lo old_maps to
         */
        writepos = conf->expand_progress +
-               conf->chunk_size/512*(conf->raid_disks-1);
-       sector_div(writepos, conf->raid_disks-1);
+               conf->chunk_size/512*(new_data_disks);
+       sector_div(writepos, new_data_disks);
        safepos = conf->expand_lo;
-       sector_div(safepos, conf->previous_raid_disks-1);
+       sector_div(safepos, data_disks);
        gap = conf->expand_progress - conf->expand_lo;
 
        if (writepos >= safepos ||
-           gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
+           gap > (new_data_disks)*3000*2 /*3Meg*/) {
                /* Cannot proceed until we've updated the superblock... */
                wait_event(conf->wait_for_overlap,
                           atomic_read(&conf->reshape_stripes)==0);
@@ -2820,8 +3591,11 @@ static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped
                        sector_t s;
                        if (j == sh->pd_idx)
                                continue;
+                       if (conf->level == 6 &&
+                           j == raid6_next_disk(sh->pd_idx, sh->disks))
+                               continue;
                        s = compute_blocknr(sh, j);
-                       if (s < (mddev->array_size<<1)) {
+                       if (s < mddev->array_sectors) {
                                skipped = 1;
                                continue;
                        }
@@ -2836,28 +3610,27 @@ static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped
                release_stripe(sh);
        }
        spin_lock_irq(&conf->device_lock);
-       conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
+       conf->expand_progress = (sector_nr + i) * new_data_disks;
        spin_unlock_irq(&conf->device_lock);
        /* Ok, those stripe are ready. We can start scheduling
         * reads on the source stripes.
         * The source stripes are determined by mapping the first and last
         * block on the destination stripes.
         */
-       raid_disks = conf->previous_raid_disks;
-       data_disks = raid_disks - 1;
        first_sector =
-               raid5_compute_sector(sector_nr*(conf->raid_disks-1),
+               raid5_compute_sector(sector_nr*(new_data_disks),
                                     raid_disks, data_disks,
                                     &dd_idx, &pd_idx, conf);
        last_sector =
                raid5_compute_sector((sector_nr+conf->chunk_size/512)
-                                    *(conf->raid_disks-1) -1,
+                                    *(new_data_disks) -1,
                                     raid_disks, data_disks,
                                     &dd_idx, &pd_idx, conf);
        if (last_sector >= (mddev->size<<1))
                last_sector = (mddev->size<<1)-1;
        while (first_sector <= last_sector) {
-               pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
+               pd_idx = stripe_to_pdidx(first_sector, conf,
+                                        conf->previous_raid_disks);
                sh = get_active_stripe(conf, first_sector,
                                       conf->previous_raid_disks, pd_idx, 0);
                set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
@@ -2865,6 +3638,25 @@ static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped
                release_stripe(sh);
                first_sector += STRIPE_SECTORS;
        }
+       /* If this takes us to the resync_max point where we have to pause,
+        * then we need to write out the superblock.
+        */
+       sector_nr += conf->chunk_size>>9;
+       if (sector_nr >= mddev->resync_max) {
+               /* Cannot proceed until we've updated the superblock... */
+               wait_event(conf->wait_for_overlap,
+                          atomic_read(&conf->reshape_stripes) == 0);
+               mddev->reshape_position = conf->expand_progress;
+               set_bit(MD_CHANGE_DEVS, &mddev->flags);
+               md_wakeup_thread(mddev->thread);
+               wait_event(mddev->sb_wait,
+                          !test_bit(MD_CHANGE_DEVS, &mddev->flags)
+                          || kthread_should_stop());
+               spin_lock_irq(&conf->device_lock);
+               conf->expand_lo = mddev->reshape_position;
+               spin_unlock_irq(&conf->device_lock);
+               wake_up(&conf->wait_for_overlap);
+       }
        return conf->chunk_size>>9;
 }
 
@@ -2901,6 +3693,12 @@ static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *ski
        if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
                return reshape_request(mddev, sector_nr, skipped);
 
+       /* No need to check resync_max as we never do more than one
+        * stripe, and as resync_max will always be on a chunk boundary,
+        * if the check in md_do_sync didn't fire, there is no chance
+        * of overstepping resync_max here
+        */
+
        /* if there is too many failed drives and we are trying
         * to resync, then assert that we are finished, because there is
         * nothing we can do.
@@ -2920,6 +3718,9 @@ static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *ski
                return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
        }
 
+
+       bitmap_cond_end_sync(mddev->bitmap, sector_nr);
+
        pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
        sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
        if (sh == NULL) {
@@ -2944,12 +3745,84 @@ static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *ski
        clear_bit(STRIPE_INSYNC, &sh->state);
        spin_unlock(&sh->lock);
 
-       handle_stripe(sh, NULL);
+       /* wait for any blocked device to be handled */
+       while(unlikely(!handle_stripe(sh, NULL)))
+               ;
        release_stripe(sh);
 
        return STRIPE_SECTORS;
 }
 
+static int  retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
+{
+       /* We may not be able to submit a whole bio at once as there
+        * may not be enough stripe_heads available.
+        * We cannot pre-allocate enough stripe_heads as we may need
+        * more than exist in the cache (if we allow ever large chunks).
+        * So we do one stripe head at a time and record in
+        * ->bi_hw_segments how many have been done.
+        *
+        * We *know* that this entire raid_bio is in one chunk, so
+        * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
+        */
+       struct stripe_head *sh;
+       int dd_idx, pd_idx;
+       sector_t sector, logical_sector, last_sector;
+       int scnt = 0;
+       int remaining;
+       int handled = 0;
+
+       logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
+       sector = raid5_compute_sector(  logical_sector,
+                                       conf->raid_disks,
+                                       conf->raid_disks - conf->max_degraded,
+                                       &dd_idx,
+                                       &pd_idx,
+                                       conf);
+       last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
+
+       for (; logical_sector < last_sector;
+            logical_sector += STRIPE_SECTORS,
+                    sector += STRIPE_SECTORS,
+                    scnt++) {
+
+               if (scnt < raid5_bi_hw_segments(raid_bio))
+                       /* already done this stripe */
+                       continue;
+
+               sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
+
+               if (!sh) {
+                       /* failed to get a stripe - must wait */
+                       raid5_set_bi_hw_segments(raid_bio, scnt);
+                       conf->retry_read_aligned = raid_bio;
+                       return handled;
+               }
+
+               set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
+               if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
+                       release_stripe(sh);
+                       raid5_set_bi_hw_segments(raid_bio, scnt);
+                       conf->retry_read_aligned = raid_bio;
+                       return handled;
+               }
+
+               handle_stripe(sh, NULL);
+               release_stripe(sh);
+               handled++;
+       }
+       spin_lock_irq(&conf->device_lock);
+       remaining = raid5_dec_bi_phys_segments(raid_bio);
+       spin_unlock_irq(&conf->device_lock);
+       if (remaining == 0)
+               bio_endio(raid_bio, 0);
+       if (atomic_dec_and_test(&conf->active_aligned_reads))
+               wake_up(&conf->wait_for_stripe);
+       return handled;
+}
+
+
+
 /*
  * This is our raid5 kernel thread.
  *
@@ -2957,20 +3830,20 @@ static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *ski
  * During the scan, completed stripes are saved for us by the interrupt
  * handler, so that they will not have to wait for our next wakeup.
  */
-static void raid5d (mddev_t *mddev)
+static void raid5d(mddev_t *mddev)
 {
        struct stripe_head *sh;
        raid5_conf_t *conf = mddev_to_conf(mddev);
        int handled;
 
-       PRINTK("+++ raid5d active\n");
+       pr_debug("+++ raid5d active\n");
 
        md_check_recovery(mddev);
 
        handled = 0;
        spin_lock_irq(&conf->device_lock);
        while (1) {
-               struct list_head *first;
+               struct bio *bio;
 
                if (conf->seq_flush != conf->seq_write) {
                        int seq = conf->seq_flush;
@@ -2981,21 +3854,20 @@ static void raid5d (mddev_t *mddev)
                        activate_bit_delay(conf);
                }
 
-               if (list_empty(&conf->handle_list) &&
-                   atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
-                   !blk_queue_plugged(mddev->queue) &&
-                   !list_empty(&conf->delayed_list))
-                       raid5_activate_delayed(conf);
-
-               if (list_empty(&conf->handle_list))
-                       break;
+               while ((bio = remove_bio_from_retry(conf))) {
+                       int ok;
+                       spin_unlock_irq(&conf->device_lock);
+                       ok = retry_aligned_read(conf, bio);
+                       spin_lock_irq(&conf->device_lock);
+                       if (!ok)
+                               break;
+                       handled++;
+               }
 
-               first = conf->handle_list.next;
-               sh = list_entry(first, struct stripe_head, lru);
+               sh = __get_priority_stripe(conf);
 
-               list_del_init(first);
-               atomic_inc(&sh->count);
-               BUG_ON(atomic_read(&sh->count)!= 1);
+               if (!sh)
+                       break;
                spin_unlock_irq(&conf->device_lock);
                
                handled++;
@@ -3004,13 +3876,14 @@ static void raid5d (mddev_t *mddev)
 
                spin_lock_irq(&conf->device_lock);
        }
-       PRINTK("%d stripes handled\n", handled);
+       pr_debug("%d stripes handled\n", handled);
 
        spin_unlock_irq(&conf->device_lock);
 
+       async_tx_issue_pending_all();
        unplug_slaves(mddev);
 
-       PRINTK("--- raid5d inactive\n");
+       pr_debug("--- raid5d inactive\n");
 }
 
 static ssize_t
@@ -3027,15 +3900,15 @@ static ssize_t
 raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
 {
        raid5_conf_t *conf = mddev_to_conf(mddev);
-       char *end;
-       int new;
+       unsigned long new;
+       int err;
+
        if (len >= PAGE_SIZE)
                return -EINVAL;
        if (!conf)
                return -ENODEV;
 
-       new = simple_strtoul(page, &end, 10);
-       if (!*page || (*end && *end != '\n') )
+       if (strict_strtoul(page, 10, &new))
                return -EINVAL;
        if (new <= 16 || new > 32768)
                return -EINVAL;
@@ -3045,6 +3918,9 @@ raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
                else
                        break;
        }
+       err = md_allow_write(mddev);
+       if (err)
+               return err;
        while (new > conf->max_nr_stripes) {
                if (grow_one_stripe(conf))
                        conf->max_nr_stripes++;
@@ -3059,6 +3935,40 @@ raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
                                raid5_store_stripe_cache_size);
 
 static ssize_t
+raid5_show_preread_threshold(mddev_t *mddev, char *page)
+{
+       raid5_conf_t *conf = mddev_to_conf(mddev);
+       if (conf)
+               return sprintf(page, "%d\n", conf->bypass_threshold);
+       else
+               return 0;
+}
+
+static ssize_t
+raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
+{
+       raid5_conf_t *conf = mddev_to_conf(mddev);
+       unsigned long new;
+       if (len >= PAGE_SIZE)
+               return -EINVAL;
+       if (!conf)
+               return -ENODEV;
+
+       if (strict_strtoul(page, 10, &new))
+               return -EINVAL;
+       if (new > conf->max_nr_stripes)
+               return -EINVAL;
+       conf->bypass_threshold = new;
+       return len;
+}
+
+static struct md_sysfs_entry
+raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
+                                       S_IRUGO | S_IWUSR,
+                                       raid5_show_preread_threshold,
+                                       raid5_store_preread_threshold);
+
+static ssize_t
 stripe_cache_active_show(mddev_t *mddev, char *page)
 {
        raid5_conf_t *conf = mddev_to_conf(mddev);
@@ -3074,6 +3984,7 @@ raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
 static struct attribute *raid5_attrs[] =  {
        &raid5_stripecache_size.attr,
        &raid5_stripecache_active.attr,
+       &raid5_preread_bypass_threshold.attr,
        NULL,
 };
 static struct attribute_group raid5_attrs_group = {
@@ -3087,7 +3998,6 @@ static int run(mddev_t *mddev)
        int raid_disk, memory;
        mdk_rdev_t *rdev;
        struct disk_info *disk;
-       struct list_head *tmp;
        int working_disks = 0;
 
        if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
@@ -3096,6 +4006,13 @@ static int run(mddev_t *mddev)
                return -EIO;
        }
 
+       if (mddev->chunk_size < PAGE_SIZE) {
+               printk(KERN_ERR "md/raid5: chunk_size must be at least "
+                      "PAGE_SIZE but %d < %ld\n",
+                      mddev->chunk_size, PAGE_SIZE);
+               return -EINVAL;
+       }
+
        if (mddev->reshape_position != MaxSector) {
                /* Check that we can continue the reshape.
                 * Currently only disks can change, it must
@@ -3104,35 +4021,44 @@ static int run(mddev_t *mddev)
                 */
                sector_t here_new, here_old;
                int old_disks;
+               int max_degraded = (mddev->level == 5 ? 1 : 2);
 
                if (mddev->new_level != mddev->level ||
                    mddev->new_layout != mddev->layout ||
                    mddev->new_chunk != mddev->chunk_size) {
-                       printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
+                       printk(KERN_ERR "raid5: %s: unsupported reshape "
+                              "required - aborting.\n",
                               mdname(mddev));
                        return -EINVAL;
                }
                if (mddev->delta_disks <= 0) {
-                       printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
+                       printk(KERN_ERR "raid5: %s: unsupported reshape "
+                              "(reduce disks) required - aborting.\n",
                               mdname(mddev));
                        return -EINVAL;
                }
                old_disks = mddev->raid_disks - mddev->delta_disks;
                /* reshape_position must be on a new-stripe boundary, and one
-                * further up in new geometry must map after here in old geometry.
+                * further up in new geometry must map after here in old
+                * geometry.
                 */
                here_new = mddev->reshape_position;
-               if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
-                       printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
+               if (sector_div(here_new, (mddev->chunk_size>>9)*
+                              (mddev->raid_disks - max_degraded))) {
+                       printk(KERN_ERR "raid5: reshape_position not "
+                              "on a stripe boundary\n");
                        return -EINVAL;
                }
                /* here_new is the stripe we will write to */
                here_old = mddev->reshape_position;
-               sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
-               /* here_old is the first stripe that we might need to read from */
+               sector_div(here_old, (mddev->chunk_size>>9)*
+                          (old_disks-max_degraded));
+               /* here_old is the first stripe that we might need to read
+                * from */
                if (here_new >= here_old) {
                        /* Reading from the same stripe as writing to - bad */
-                       printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
+                       printk(KERN_ERR "raid5: reshape_position too early for "
+                              "auto-recovery - aborting.\n");
                        return -EINVAL;
                }
                printk(KERN_INFO "raid5: reshape will continue\n");
@@ -3166,18 +4092,22 @@ static int run(mddev_t *mddev)
                        goto abort;
        }
        spin_lock_init(&conf->device_lock);
+       mddev->queue->queue_lock = &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->hold_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);
+       atomic_set(&conf->active_aligned_reads, 0);
+       conf->bypass_threshold = BYPASS_THRESHOLD;
 
-       PRINTK("raid5: run(%s) called.\n", mdname(mddev));
+       pr_debug("raid5: run(%s) called.\n", mdname(mddev));
 
-       ITERATE_RDEV(mddev,rdev,tmp) {
+       list_for_each_entry(rdev, &mddev->disks, same_set) {
                raid_disk = rdev->raid_disk;
                if (raid_disk >= conf->raid_disks
                    || raid_disk < 0)
@@ -3192,7 +4122,9 @@ static int run(mddev_t *mddev)
                                " disk %d\n", bdevname(rdev->bdev,b),
                                raid_disk);
                        working_disks++;
-               }
+               } else
+                       /* Cannot rely on bitmap to complete recovery */
+                       conf->fullsync = 1;
        }
 
        /*
@@ -3310,16 +4242,20 @@ static int run(mddev_t *mddev)
        }
 
        /* Ok, everything is just fine now */
-       sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
+       if (sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
+               printk(KERN_WARNING
+                      "raid5: failed to create sysfs attributes for %s\n",
+                      mdname(mddev));
 
        mddev->queue->unplug_fn = raid5_unplug_device;
-       mddev->queue->issue_flush_fn = raid5_issue_flush;
-       mddev->queue->backing_dev_info.congested_fn = raid5_congested;
        mddev->queue->backing_dev_info.congested_data = mddev;
+       mddev->queue->backing_dev_info.congested_fn = raid5_congested;
 
-       mddev->array_size =  mddev->size * (conf->previous_raid_disks -
+       mddev->array_sectors = 2 * mddev->size * (conf->previous_raid_disks -
                                            conf->max_degraded);
 
+       blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
+
        return 0;
 abort:
        if (conf) {
@@ -3344,6 +4280,7 @@ static int stop(mddev_t *mddev)
        mddev->thread = NULL;
        shrink_stripes(conf);
        kfree(conf->stripe_hashtbl);
+       mddev->queue->backing_dev_info.congested_fn = NULL;
        blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
        sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
        kfree(conf->disks);
@@ -3352,8 +4289,8 @@ static int stop(mddev_t *mddev)
        return 0;
 }
 
-#if RAID5_DEBUG
-static void print_sh (struct seq_file *seq, struct stripe_head *sh)
+#ifdef DEBUG
+static void print_sh(struct seq_file *seq, struct stripe_head *sh)
 {
        int i;
 
@@ -3369,7 +4306,7 @@ static void print_sh (struct seq_file *seq, struct stripe_head *sh)
        seq_printf(seq, "\n");
 }
 
-static void printall (struct seq_file *seq, raid5_conf_t *conf)
+static void printall(struct seq_file *seq, raid5_conf_t *conf)
 {
        struct stripe_head *sh;
        struct hlist_node *hn;
@@ -3387,7 +4324,7 @@ static void printall (struct seq_file *seq, raid5_conf_t *conf)
 }
 #endif
 
-static void status (struct seq_file *seq, mddev_t *mddev)
+static void status(struct seq_file *seq, mddev_t *mddev)
 {
        raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
        int i;
@@ -3399,7 +4336,7 @@ static void status (struct seq_file *seq, mddev_t *mddev)
                               conf->disks[i].rdev &&
                               test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
        seq_printf (seq, "]");
-#if RAID5_DEBUG
+#ifdef DEBUG
        seq_printf (seq, "\n");
        printall(seq, conf);
 #endif
@@ -3464,6 +4401,14 @@ static int raid5_remove_disk(mddev_t *mddev, int number)
                        err = -EBUSY;
                        goto abort;
                }
+               /* Only remove non-faulty devices if recovery
+                * isn't possible.
+                */
+               if (!test_bit(Faulty, &rdev->flags) &&
+                   mddev->degraded <= conf->max_degraded) {
+                       err = -EBUSY;
+                       goto abort;
+               }
                p->rdev = NULL;
                synchronize_rcu();
                if (atomic_read(&rdev->nr_pending)) {
@@ -3481,35 +4426,41 @@ abort:
 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
 {
        raid5_conf_t *conf = mddev->private;
-       int found = 0;
+       int err = -EEXIST;
        int disk;
        struct disk_info *p;
+       int first = 0;
+       int last = conf->raid_disks - 1;
 
        if (mddev->degraded > conf->max_degraded)
                /* no point adding a device */
-               return 0;
+               return -EINVAL;
+
+       if (rdev->raid_disk >= 0)
+               first = last = rdev->raid_disk;
 
        /*
         * find the disk ... but prefer rdev->saved_raid_disk
         * if possible.
         */
        if (rdev->saved_raid_disk >= 0 &&
+           rdev->saved_raid_disk >= first &&
            conf->disks[rdev->saved_raid_disk].rdev == NULL)
                disk = rdev->saved_raid_disk;
        else
-               disk = 0;
-       for ( ; disk < conf->raid_disks; disk++)
+               disk = first;
+       for ( ; disk <= last ; disk++)
                if ((p=conf->disks + disk)->rdev == NULL) {
                        clear_bit(In_sync, &rdev->flags);
                        rdev->raid_disk = disk;
-                       found = 1;
+                       err = 0;
                        if (rdev->saved_raid_disk != disk)
                                conf->fullsync = 1;
                        rcu_assign_pointer(p->rdev, rdev);
                        break;
                }
        print_raid5_conf(conf);
-       return found;
+       return err;
 }
 
 static int raid5_resize(mddev_t *mddev, sector_t sectors)
@@ -3524,8 +4475,9 @@ static int raid5_resize(mddev_t *mddev, sector_t sectors)
        raid5_conf_t *conf = mddev_to_conf(mddev);
 
        sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
-       mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
-       set_capacity(mddev->gendisk, mddev->array_size << 1);
+       mddev->array_sectors = sectors * (mddev->raid_disks
+                                         - conf->max_degraded);
+       set_capacity(mddev->gendisk, mddev->array_sectors);
        mddev->changed = 1;
        if (sectors/2  > mddev->size && mddev->recovery_cp == MaxSector) {
                mddev->recovery_cp = mddev->size << 1;
@@ -3547,6 +4499,9 @@ static int raid5_check_reshape(mddev_t *mddev)
                return -EINVAL; /* Cannot shrink array or change level yet */
        if (mddev->delta_disks == 0)
                return 0; /* nothing to do */
+       if (mddev->bitmap)
+               /* Cannot grow a bitmap yet */
+               return -EBUSY;
 
        /* Can only proceed if there are plenty of stripe_heads.
         * We need a minimum of one full stripe,, and for sensible progress
@@ -3567,6 +4522,8 @@ static int raid5_check_reshape(mddev_t *mddev)
        if (err)
                return err;
 
+       if (mddev->degraded > conf->max_degraded)
+               return -EINVAL;
        /* looks like we might be able to manage this */
        return 0;
 }
@@ -3575,21 +4532,19 @@ static int raid5_start_reshape(mddev_t *mddev)
 {
        raid5_conf_t *conf = mddev_to_conf(mddev);
        mdk_rdev_t *rdev;
-       struct list_head *rtmp;
        int spares = 0;
        int added_devices = 0;
        unsigned long flags;
 
-       if (mddev->degraded ||
-           test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
+       if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
                return -EBUSY;
 
-       ITERATE_RDEV(mddev, rdev, rtmp)
+       list_for_each_entry(rdev, &mddev->disks, same_set)
                if (rdev->raid_disk < 0 &&
                    !test_bit(Faulty, &rdev->flags))
                        spares++;
 
-       if (spares < mddev->delta_disks-1)
+       if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
                /* Not enough devices even to make a degraded array
                 * of that size
                 */
@@ -3606,16 +4561,21 @@ static int raid5_start_reshape(mddev_t *mddev)
        /* Add some new drives, as many as will fit.
         * We know there are enough to make the newly sized array work.
         */
-       ITERATE_RDEV(mddev, rdev, rtmp)
+       list_for_each_entry(rdev, &mddev->disks, same_set)
                if (rdev->raid_disk < 0 &&
                    !test_bit(Faulty, &rdev->flags)) {
-                       if (raid5_add_disk(mddev, rdev)) {
+                       if (raid5_add_disk(mddev, rdev) == 0) {
                                char nm[20];
                                set_bit(In_sync, &rdev->flags);
                                added_devices++;
                                rdev->recovery_offset = 0;
                                sprintf(nm, "rd%d", rdev->raid_disk);
-                               sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
+                               if (sysfs_create_link(&mddev->kobj,
+                                                     &rdev->kobj, nm))
+                                       printk(KERN_WARNING
+                                              "raid5: failed to create "
+                                              " link %s for %s\n",
+                                              nm, mdname(mddev));
                        } else
                                break;
                }
@@ -3652,14 +4612,16 @@ static void end_reshape(raid5_conf_t *conf)
        struct block_device *bdev;
 
        if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
-               conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
-               set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
+               conf->mddev->array_sectors = 2 * conf->mddev->size *
+                       (conf->raid_disks - conf->max_degraded);
+               set_capacity(conf->mddev->gendisk, conf->mddev->array_sectors);
                conf->mddev->changed = 1;
 
                bdev = bdget_disk(conf->mddev->gendisk, 0);
                if (bdev) {
                        mutex_lock(&bdev->bd_inode->i_mutex);
-                       i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
+                       i_size_write(bdev->bd_inode,
+                                    (loff_t)conf->mddev->array_sectors << 9);
                        mutex_unlock(&bdev->bd_inode->i_mutex);
                        bdput(bdev);
                }
@@ -3694,7 +4656,8 @@ static void raid5_quiesce(mddev_t *mddev, int state)
                spin_lock_irq(&conf->device_lock);
                conf->quiesce = 1;
                wait_event_lock_irq(conf->wait_for_stripe,
-                                   atomic_read(&conf->active_stripes) == 0,
+                                   atomic_read(&conf->active_stripes) == 0 &&
+                                   atomic_read(&conf->active_aligned_reads) == 0,
                                    conf->device_lock, /* nothing */);
                spin_unlock_irq(&conf->device_lock);
                break;
@@ -3724,6 +4687,10 @@ static struct mdk_personality raid6_personality =
        .spare_active   = raid5_spare_active,
        .sync_request   = sync_request,
        .resize         = raid5_resize,
+#ifdef CONFIG_MD_RAID5_RESHAPE
+       .check_reshape  = raid5_check_reshape,
+       .start_reshape  = raid5_start_reshape,
+#endif
        .quiesce        = raid5_quiesce,
 };
 static struct mdk_personality raid5_personality =
@@ -3763,6 +4730,10 @@ static struct mdk_personality raid4_personality =
        .spare_active   = raid5_spare_active,
        .sync_request   = sync_request,
        .resize         = raid5_resize,
+#ifdef CONFIG_MD_RAID5_RESHAPE
+       .check_reshape  = raid5_check_reshape,
+       .start_reshape  = raid5_start_reshape,
+#endif
        .quiesce        = raid5_quiesce,
 };