]> Pileus Git - ~andy/linux/blobdiff - drivers/md/raid10.c
md/raid10: writes should get directed to replacement as well as original.
[~andy/linux] / drivers / md / raid10.c
index f5088dda4dca3527dc13893c8189779dfd53c671..813f52464f8aeeb0675c1c2edb0bdbe5beb51388 100644 (file)
@@ -73,7 +73,8 @@ static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
        struct r10conf *conf = data;
        int size = offsetof(struct r10bio, devs[conf->copies]);
 
-       /* allocate a r10bio with room for raid_disks entries in the bios array */
+       /* allocate a r10bio with room for raid_disks entries in the
+        * bios array */
        return kzalloc(size, gfp_flags);
 }
 
@@ -123,12 +124,19 @@ static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
                if (!bio)
                        goto out_free_bio;
                r10_bio->devs[j].bio = bio;
+               if (!conf->have_replacement)
+                       continue;
+               bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
+               if (!bio)
+                       goto out_free_bio;
+               r10_bio->devs[j].repl_bio = bio;
        }
        /*
         * Allocate RESYNC_PAGES data pages and attach them
         * where needed.
         */
        for (j = 0 ; j < nalloc; j++) {
+               struct bio *rbio = r10_bio->devs[j].repl_bio;
                bio = r10_bio->devs[j].bio;
                for (i = 0; i < RESYNC_PAGES; i++) {
                        if (j == 1 && !test_bit(MD_RECOVERY_SYNC,
@@ -143,6 +151,8 @@ static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
                                goto out_free_pages;
 
                        bio->bi_io_vec[i].bv_page = page;
+                       if (rbio)
+                               rbio->bi_io_vec[i].bv_page = page;
                }
        }
 
@@ -156,8 +166,11 @@ out_free_pages:
                        safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page);
        j = -1;
 out_free_bio:
-       while ( ++j < nalloc )
+       while (++j < nalloc) {
                bio_put(r10_bio->devs[j].bio);
+               if (r10_bio->devs[j].repl_bio)
+                       bio_put(r10_bio->devs[j].repl_bio);
+       }
        r10bio_pool_free(r10_bio, conf);
        return NULL;
 }
@@ -178,6 +191,9 @@ static void r10buf_pool_free(void *__r10_bio, void *data)
                        }
                        bio_put(bio);
                }
+               bio = r10bio->devs[j].repl_bio;
+               if (bio)
+                       bio_put(bio);
        }
        r10bio_pool_free(r10bio, conf);
 }
@@ -191,6 +207,10 @@ static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
                if (!BIO_SPECIAL(*bio))
                        bio_put(*bio);
                *bio = NULL;
+               bio = &r10_bio->devs[i].repl_bio;
+               if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
+                       bio_put(*bio);
+               *bio = NULL;
        }
 }
 
@@ -275,19 +295,27 @@ static inline void update_head_pos(int slot, struct r10bio *r10_bio)
  * Find the disk number which triggered given bio
  */
 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
-                        struct bio *bio, int *slotp)
+                        struct bio *bio, int *slotp, int *replp)
 {
        int slot;
+       int repl = 0;
 
-       for (slot = 0; slot < conf->copies; slot++)
+       for (slot = 0; slot < conf->copies; slot++) {
                if (r10_bio->devs[slot].bio == bio)
                        break;
+               if (r10_bio->devs[slot].repl_bio == bio) {
+                       repl = 1;
+                       break;
+               }
+       }
 
        BUG_ON(slot == conf->copies);
        update_head_pos(slot, r10_bio);
 
        if (slotp)
                *slotp = slot;
+       if (replp)
+               *replp = repl;
        return r10_bio->devs[slot].devnum;
 }
 
@@ -296,11 +324,13 @@ static void raid10_end_read_request(struct bio *bio, int error)
        int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
        struct r10bio *r10_bio = bio->bi_private;
        int slot, dev;
+       struct md_rdev *rdev;
        struct r10conf *conf = r10_bio->mddev->private;
 
 
        slot = r10_bio->read_slot;
        dev = r10_bio->devs[slot].devnum;
+       rdev = r10_bio->devs[slot].rdev;
        /*
         * this branch is our 'one mirror IO has finished' event handler:
         */
@@ -318,7 +348,7 @@ static void raid10_end_read_request(struct bio *bio, int error)
                 */
                set_bit(R10BIO_Uptodate, &r10_bio->state);
                raid_end_bio_io(r10_bio);
-               rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
+               rdev_dec_pending(rdev, conf->mddev);
        } else {
                /*
                 * oops, read error - keep the refcount on the rdev
@@ -327,7 +357,7 @@ static void raid10_end_read_request(struct bio *bio, int error)
                printk_ratelimited(KERN_ERR
                                   "md/raid10:%s: %s: rescheduling sector %llu\n",
                                   mdname(conf->mddev),
-                                  bdevname(conf->mirrors[dev].rdev->bdev, b),
+                                  bdevname(rdev->bdev, b),
                                   (unsigned long long)r10_bio->sector);
                set_bit(R10BIO_ReadError, &r10_bio->state);
                reschedule_retry(r10_bio);
@@ -366,17 +396,29 @@ static void raid10_end_write_request(struct bio *bio, int error)
        int dev;
        int dec_rdev = 1;
        struct r10conf *conf = r10_bio->mddev->private;
-       int slot;
+       int slot, repl;
+       struct md_rdev *rdev;
 
-       dev = find_bio_disk(conf, r10_bio, bio, &slot);
+       dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
 
+       if (repl)
+               rdev = conf->mirrors[dev].replacement;
+       else
+               rdev = conf->mirrors[dev].rdev;
        /*
         * this branch is our 'one mirror IO has finished' event handler:
         */
        if (!uptodate) {
-               set_bit(WriteErrorSeen, &conf->mirrors[dev].rdev->flags);
-               set_bit(R10BIO_WriteError, &r10_bio->state);
-               dec_rdev = 0;
+               if (repl)
+                       /* Never record new bad blocks to replacement,
+                        * just fail it.
+                        */
+                       md_error(rdev->mddev, rdev);
+               else {
+                       set_bit(WriteErrorSeen, &rdev->flags);
+                       set_bit(R10BIO_WriteError, &r10_bio->state);
+                       dec_rdev = 0;
+               }
        } else {
                /*
                 * Set R10BIO_Uptodate in our master bio, so that
@@ -393,12 +435,15 @@ static void raid10_end_write_request(struct bio *bio, int error)
                set_bit(R10BIO_Uptodate, &r10_bio->state);
 
                /* Maybe we can clear some bad blocks. */
-               if (is_badblock(conf->mirrors[dev].rdev,
+               if (is_badblock(rdev,
                                r10_bio->devs[slot].addr,
                                r10_bio->sectors,
                                &first_bad, &bad_sectors)) {
                        bio_put(bio);
-                       r10_bio->devs[slot].bio = IO_MADE_GOOD;
+                       if (repl)
+                               r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
+                       else
+                               r10_bio->devs[slot].bio = IO_MADE_GOOD;
                        dec_rdev = 0;
                        set_bit(R10BIO_MadeGood, &r10_bio->state);
                }
@@ -414,7 +459,6 @@ static void raid10_end_write_request(struct bio *bio, int error)
                rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev);
 }
 
-
 /*
  * RAID10 layout manager
  * As well as the chunksize and raid_disks count, there are two
@@ -562,14 +606,16 @@ static int raid10_mergeable_bvec(struct request_queue *q,
  * FIXME: possibly should rethink readbalancing and do it differently
  * depending on near_copies / far_copies geometry.
  */
-static int read_balance(struct r10conf *conf, struct r10bio *r10_bio, int *max_sectors)
+static struct md_rdev *read_balance(struct r10conf *conf,
+                                   struct r10bio *r10_bio,
+                                   int *max_sectors)
 {
        const sector_t this_sector = r10_bio->sector;
        int disk, slot;
        int sectors = r10_bio->sectors;
        int best_good_sectors;
        sector_t new_distance, best_dist;
-       struct md_rdev *rdev;
+       struct md_rdev *rdev, *best_rdev;
        int do_balance;
        int best_slot;
 
@@ -578,6 +624,7 @@ static int read_balance(struct r10conf *conf, struct r10bio *r10_bio, int *max_s
 retry:
        sectors = r10_bio->sectors;
        best_slot = -1;
+       best_rdev = NULL;
        best_dist = MaxSector;
        best_good_sectors = 0;
        do_balance = 1;
@@ -599,10 +646,16 @@ retry:
                if (r10_bio->devs[slot].bio == IO_BLOCKED)
                        continue;
                disk = r10_bio->devs[slot].devnum;
-               rdev = rcu_dereference(conf->mirrors[disk].rdev);
+               rdev = rcu_dereference(conf->mirrors[disk].replacement);
+               if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
+                   r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
+                       rdev = rcu_dereference(conf->mirrors[disk].rdev);
                if (rdev == NULL)
                        continue;
-               if (!test_bit(In_sync, &rdev->flags))
+               if (test_bit(Faulty, &rdev->flags))
+                       continue;
+               if (!test_bit(In_sync, &rdev->flags) &&
+                   r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
                        continue;
 
                dev_sector = r10_bio->devs[slot].addr;
@@ -627,6 +680,7 @@ retry:
                                if (good_sectors > best_good_sectors) {
                                        best_good_sectors = good_sectors;
                                        best_slot = slot;
+                                       best_rdev = rdev;
                                }
                                if (!do_balance)
                                        /* Must read from here */
@@ -655,16 +709,15 @@ retry:
                if (new_distance < best_dist) {
                        best_dist = new_distance;
                        best_slot = slot;
+                       best_rdev = rdev;
                }
        }
-       if (slot == conf->copies)
+       if (slot >= conf->copies) {
                slot = best_slot;
+               rdev = best_rdev;
+       }
 
        if (slot >= 0) {
-               disk = r10_bio->devs[slot].devnum;
-               rdev = rcu_dereference(conf->mirrors[disk].rdev);
-               if (!rdev)
-                       goto retry;
                atomic_inc(&rdev->nr_pending);
                if (test_bit(Faulty, &rdev->flags)) {
                        /* Cannot risk returning a device that failed
@@ -675,11 +728,11 @@ retry:
                }
                r10_bio->read_slot = slot;
        } else
-               disk = -1;
+               rdev = NULL;
        rcu_read_unlock();
        *max_sectors = best_good_sectors;
 
-       return disk;
+       return rdev;
 }
 
 static int raid10_congested(void *data, int bits)
@@ -846,7 +899,6 @@ static void unfreeze_array(struct r10conf *conf)
 static void make_request(struct mddev *mddev, struct bio * bio)
 {
        struct r10conf *conf = mddev->private;
-       struct mirror_info *mirror;
        struct r10bio *r10_bio;
        struct bio *read_bio;
        int i;
@@ -945,27 +997,27 @@ static void make_request(struct mddev *mddev, struct bio * bio)
                /*
                 * read balancing logic:
                 */
-               int disk;
+               struct md_rdev *rdev;
                int slot;
 
 read_again:
-               disk = read_balance(conf, r10_bio, &max_sectors);
-               slot = r10_bio->read_slot;
-               if (disk < 0) {
+               rdev = read_balance(conf, r10_bio, &max_sectors);
+               if (!rdev) {
                        raid_end_bio_io(r10_bio);
                        return;
                }
-               mirror = conf->mirrors + disk;
+               slot = r10_bio->read_slot;
 
                read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
                md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector,
                            max_sectors);
 
                r10_bio->devs[slot].bio = read_bio;
+               r10_bio->devs[slot].rdev = rdev;
 
                read_bio->bi_sector = r10_bio->devs[slot].addr +
-                       mirror->rdev->data_offset;
-               read_bio->bi_bdev = mirror->rdev->bdev;
+                       rdev->data_offset;
+               read_bio->bi_bdev = rdev->bdev;
                read_bio->bi_end_io = raid10_end_read_request;
                read_bio->bi_rw = READ | do_sync;
                read_bio->bi_private = r10_bio;
@@ -1025,6 +1077,7 @@ read_again:
         */
        plugged = mddev_check_plugged(mddev);
 
+       r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
        raid10_find_phys(conf, r10_bio);
 retry_write:
        blocked_rdev = NULL;
@@ -1034,12 +1087,23 @@ retry_write:
        for (i = 0;  i < conf->copies; i++) {
                int d = r10_bio->devs[i].devnum;
                struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
+               struct md_rdev *rrdev = rcu_dereference(
+                       conf->mirrors[d].replacement);
                if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
                        atomic_inc(&rdev->nr_pending);
                        blocked_rdev = rdev;
                        break;
                }
+               if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
+                       atomic_inc(&rrdev->nr_pending);
+                       blocked_rdev = rrdev;
+                       break;
+               }
+               if (rrdev && test_bit(Faulty, &rrdev->flags))
+                       rrdev = NULL;
+
                r10_bio->devs[i].bio = NULL;
+               r10_bio->devs[i].repl_bio = NULL;
                if (!rdev || test_bit(Faulty, &rdev->flags)) {
                        set_bit(R10BIO_Degraded, &r10_bio->state);
                        continue;
@@ -1088,6 +1152,10 @@ retry_write:
                }
                r10_bio->devs[i].bio = bio;
                atomic_inc(&rdev->nr_pending);
+               if (rrdev) {
+                       r10_bio->devs[i].repl_bio = bio;
+                       atomic_inc(&rrdev->nr_pending);
+               }
        }
        rcu_read_unlock();
 
@@ -1096,11 +1164,17 @@ retry_write:
                int j;
                int d;
 
-               for (j = 0; j < i; j++)
+               for (j = 0; j < i; j++) {
                        if (r10_bio->devs[j].bio) {
                                d = r10_bio->devs[j].devnum;
                                rdev_dec_pending(conf->mirrors[d].rdev, mddev);
                        }
+                       if (r10_bio->devs[j].repl_bio) {
+                               d = r10_bio->devs[j].devnum;
+                               rdev_dec_pending(
+                                       conf->mirrors[d].replacement, mddev);
+                       }
+               }
                allow_barrier(conf);
                md_wait_for_blocked_rdev(blocked_rdev, mddev);
                wait_barrier(conf);
@@ -1147,6 +1221,27 @@ retry_write:
                bio_list_add(&conf->pending_bio_list, mbio);
                conf->pending_count++;
                spin_unlock_irqrestore(&conf->device_lock, flags);
+
+               if (!r10_bio->devs[i].repl_bio)
+                       continue;
+
+               mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
+               md_trim_bio(mbio, r10_bio->sector - bio->bi_sector,
+                           max_sectors);
+               r10_bio->devs[i].repl_bio = mbio;
+
+               mbio->bi_sector = (r10_bio->devs[i].addr+
+                                  conf->mirrors[d].replacement->data_offset);
+               mbio->bi_bdev = conf->mirrors[d].replacement->bdev;
+               mbio->bi_end_io = raid10_end_write_request;
+               mbio->bi_rw = WRITE | do_sync | do_fua;
+               mbio->bi_private = r10_bio;
+
+               atomic_inc(&r10_bio->remaining);
+               spin_lock_irqsave(&conf->device_lock, flags);
+               bio_list_add(&conf->pending_bio_list, mbio);
+               conf->pending_count++;
+               spin_unlock_irqrestore(&conf->device_lock, flags);
        }
 
        /* Don't remove the bias on 'remaining' (one_write_done) until
@@ -1390,34 +1485,41 @@ static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
        struct r10conf *conf = mddev->private;
        int err = 0;
        int number = rdev->raid_disk;
-       struct mirror_info *p = conf->mirrors+ number;
+       struct md_rdev **rdevp;
+       struct mirror_info *p = conf->mirrors + number;
 
        print_conf(conf);
-       if (rdev == p->rdev) {
-               if (test_bit(In_sync, &rdev->flags) ||
-                   atomic_read(&rdev->nr_pending)) {
-                       err = -EBUSY;
-                       goto abort;
-               }
-               /* Only remove faulty devices in recovery
-                * is not possible.
-                */
-               if (!test_bit(Faulty, &rdev->flags) &&
-                   mddev->recovery_disabled != p->recovery_disabled &&
-                   enough(conf, -1)) {
-                       err = -EBUSY;
-                       goto abort;
-               }
-               p->rdev = NULL;
-               synchronize_rcu();
-               if (atomic_read(&rdev->nr_pending)) {
-                       /* lost the race, try later */
-                       err = -EBUSY;
-                       p->rdev = rdev;
-                       goto abort;
-               }
-               err = md_integrity_register(mddev);
+       if (rdev == p->rdev)
+               rdevp = &p->rdev;
+       else if (rdev == p->replacement)
+               rdevp = &p->replacement;
+       else
+               return 0;
+
+       if (test_bit(In_sync, &rdev->flags) ||
+           atomic_read(&rdev->nr_pending)) {
+               err = -EBUSY;
+               goto abort;
        }
+       /* Only remove faulty devices if recovery
+        * is not possible.
+        */
+       if (!test_bit(Faulty, &rdev->flags) &&
+           mddev->recovery_disabled != p->recovery_disabled &&
+           enough(conf, -1)) {
+               err = -EBUSY;
+               goto abort;
+       }
+       *rdevp = NULL;
+       synchronize_rcu();
+       if (atomic_read(&rdev->nr_pending)) {
+               /* lost the race, try later */
+               err = -EBUSY;
+               *rdevp = rdev;
+               goto abort;
+       }
+       err = md_integrity_register(mddev);
+
 abort:
 
        print_conf(conf);
@@ -1431,7 +1533,7 @@ static void end_sync_read(struct bio *bio, int error)
        struct r10conf *conf = r10_bio->mddev->private;
        int d;
 
-       d = find_bio_disk(conf, r10_bio, bio, NULL);
+       d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
 
        if (test_bit(BIO_UPTODATE, &bio->bi_flags))
                set_bit(R10BIO_Uptodate, &r10_bio->state);
@@ -1493,7 +1595,7 @@ static void end_sync_write(struct bio *bio, int error)
        int bad_sectors;
        int slot;
 
-       d = find_bio_disk(conf, r10_bio, bio, &slot);
+       d = find_bio_disk(conf, r10_bio, bio, &slot, NULL);
 
        if (!uptodate) {
                set_bit(WriteErrorSeen, &conf->mirrors[d].rdev->flags);
@@ -2059,10 +2161,9 @@ static int narrow_write_error(struct r10bio *r10_bio, int i)
 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
 {
        int slot = r10_bio->read_slot;
-       int mirror = r10_bio->devs[slot].devnum;
        struct bio *bio;
        struct r10conf *conf = mddev->private;
-       struct md_rdev *rdev;
+       struct md_rdev *rdev = r10_bio->devs[slot].rdev;
        char b[BDEVNAME_SIZE];
        unsigned long do_sync;
        int max_sectors;
@@ -2080,15 +2181,15 @@ static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
                fix_read_error(conf, mddev, r10_bio);
                unfreeze_array(conf);
        }
-       rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
+       rdev_dec_pending(rdev, mddev);
 
        bio = r10_bio->devs[slot].bio;
        bdevname(bio->bi_bdev, b);
        r10_bio->devs[slot].bio =
                mddev->ro ? IO_BLOCKED : NULL;
 read_more:
-       mirror = read_balance(conf, r10_bio, &max_sectors);
-       if (mirror == -1) {
+       rdev = read_balance(conf, r10_bio, &max_sectors);
+       if (rdev == NULL) {
                printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O"
                       " read error for block %llu\n",
                       mdname(mddev), b,
@@ -2102,7 +2203,6 @@ read_more:
        if (bio)
                bio_put(bio);
        slot = r10_bio->read_slot;
-       rdev = conf->mirrors[mirror].rdev;
        printk_ratelimited(
                KERN_ERR
                "md/raid10:%s: %s: redirecting"
@@ -2116,6 +2216,7 @@ read_more:
                    r10_bio->sector - bio->bi_sector,
                    max_sectors);
        r10_bio->devs[slot].bio = bio;
+       r10_bio->devs[slot].rdev = rdev;
        bio->bi_sector = r10_bio->devs[slot].addr
                + rdev->data_offset;
        bio->bi_bdev = rdev->bdev;
@@ -2208,6 +2309,15 @@ static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
                                }
                                rdev_dec_pending(rdev, conf->mddev);
                        }
+                       bio = r10_bio->devs[m].repl_bio;
+                       rdev = conf->mirrors[dev].replacement;
+                       if (bio == IO_MADE_GOOD) {
+                               rdev_clear_badblocks(
+                                       rdev,
+                                       r10_bio->devs[m].addr,
+                                       r10_bio->sectors);
+                               rdev_dec_pending(rdev, conf->mddev);
+                       }
                }
                if (test_bit(R10BIO_WriteError,
                             &r10_bio->state))
@@ -2271,9 +2381,14 @@ static void raid10d(struct mddev *mddev)
 static int init_resync(struct r10conf *conf)
 {
        int buffs;
+       int i;
 
        buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
        BUG_ON(conf->r10buf_pool);
+       conf->have_replacement = 0;
+       for (i = 0; i < conf->raid_disks; i++)
+               if (conf->mirrors[i].replacement)
+                       conf->have_replacement = 1;
        conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
        if (!conf->r10buf_pool)
                return -ENOMEM;