2 * raid1.c : Multiple Devices driver for Linux
4 * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat
6 * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman
8 * RAID-1 management functions.
10 * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000
12 * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk>
13 * Various fixes by Neil Brown <neilb@cse.unsw.edu.au>
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2, or (at your option)
20 * You should have received a copy of the GNU General Public License
21 * (for example /usr/src/linux/COPYING); if not, write to the Free
22 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 #include <linux/raid/raid1.h>
28 * Number of guaranteed r1bios in case of extreme VM load:
30 #define NR_RAID1_BIOS 256
32 static mdk_personality_t raid1_personality;
34 static void unplug_slaves(mddev_t *mddev);
37 static void * r1bio_pool_alloc(unsigned int __nocast gfp_flags, void *data)
39 struct pool_info *pi = data;
41 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
43 /* allocate a r1bio with room for raid_disks entries in the bios array */
44 r1_bio = kmalloc(size, gfp_flags);
46 memset(r1_bio, 0, size);
48 unplug_slaves(pi->mddev);
53 static void r1bio_pool_free(void *r1_bio, void *data)
58 #define RESYNC_BLOCK_SIZE (64*1024)
59 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
60 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
61 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
62 #define RESYNC_WINDOW (2048*1024)
64 static void * r1buf_pool_alloc(unsigned int __nocast gfp_flags, void *data)
66 struct pool_info *pi = data;
72 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
74 unplug_slaves(pi->mddev);
79 * Allocate bios : 1 for reading, n-1 for writing
81 for (j = pi->raid_disks ; j-- ; ) {
82 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
85 r1_bio->bios[j] = bio;
88 * Allocate RESYNC_PAGES data pages and attach them to
91 bio = r1_bio->bios[0];
92 for (i = 0; i < RESYNC_PAGES; i++) {
93 page = alloc_page(gfp_flags);
97 bio->bi_io_vec[i].bv_page = page;
100 r1_bio->master_bio = NULL;
106 __free_page(bio->bi_io_vec[i-1].bv_page);
108 while ( ++j < pi->raid_disks )
109 bio_put(r1_bio->bios[j]);
110 r1bio_pool_free(r1_bio, data);
114 static void r1buf_pool_free(void *__r1_bio, void *data)
116 struct pool_info *pi = data;
118 r1bio_t *r1bio = __r1_bio;
119 struct bio *bio = r1bio->bios[0];
121 for (i = 0; i < RESYNC_PAGES; i++) {
122 __free_page(bio->bi_io_vec[i].bv_page);
123 bio->bi_io_vec[i].bv_page = NULL;
125 for (i=0 ; i < pi->raid_disks; i++)
126 bio_put(r1bio->bios[i]);
128 r1bio_pool_free(r1bio, data);
131 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
135 for (i = 0; i < conf->raid_disks; i++) {
136 struct bio **bio = r1_bio->bios + i;
143 static inline void free_r1bio(r1bio_t *r1_bio)
147 conf_t *conf = mddev_to_conf(r1_bio->mddev);
150 * Wake up any possible resync thread that waits for the device
153 spin_lock_irqsave(&conf->resync_lock, flags);
154 if (!--conf->nr_pending) {
155 wake_up(&conf->wait_idle);
156 wake_up(&conf->wait_resume);
158 spin_unlock_irqrestore(&conf->resync_lock, flags);
160 put_all_bios(conf, r1_bio);
161 mempool_free(r1_bio, conf->r1bio_pool);
164 static inline void put_buf(r1bio_t *r1_bio)
166 conf_t *conf = mddev_to_conf(r1_bio->mddev);
169 mempool_free(r1_bio, conf->r1buf_pool);
171 spin_lock_irqsave(&conf->resync_lock, flags);
175 wake_up(&conf->wait_resume);
176 wake_up(&conf->wait_idle);
178 if (!--conf->nr_pending) {
179 wake_up(&conf->wait_idle);
180 wake_up(&conf->wait_resume);
182 spin_unlock_irqrestore(&conf->resync_lock, flags);
185 static void reschedule_retry(r1bio_t *r1_bio)
188 mddev_t *mddev = r1_bio->mddev;
189 conf_t *conf = mddev_to_conf(mddev);
191 spin_lock_irqsave(&conf->device_lock, flags);
192 list_add(&r1_bio->retry_list, &conf->retry_list);
193 spin_unlock_irqrestore(&conf->device_lock, flags);
195 md_wakeup_thread(mddev->thread);
199 * raid_end_bio_io() is called when we have finished servicing a mirrored
200 * operation and are ready to return a success/failure code to the buffer
203 static void raid_end_bio_io(r1bio_t *r1_bio)
205 struct bio *bio = r1_bio->master_bio;
207 bio_endio(bio, bio->bi_size,
208 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
213 * Update disk head position estimator based on IRQ completion info.
215 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
217 conf_t *conf = mddev_to_conf(r1_bio->mddev);
219 conf->mirrors[disk].head_position =
220 r1_bio->sector + (r1_bio->sectors);
223 static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
225 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
226 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
228 conf_t *conf = mddev_to_conf(r1_bio->mddev);
233 mirror = r1_bio->read_disk;
235 * this branch is our 'one mirror IO has finished' event handler:
238 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
241 * Set R1BIO_Uptodate in our master bio, so that
242 * we will return a good error code for to the higher
243 * levels even if IO on some other mirrored buffer fails.
245 * The 'master' represents the composite IO operation to
246 * user-side. So if something waits for IO, then it will
247 * wait for the 'master' bio.
249 set_bit(R1BIO_Uptodate, &r1_bio->state);
251 update_head_pos(mirror, r1_bio);
254 * we have only one bio on the read side
257 raid_end_bio_io(r1_bio);
262 char b[BDEVNAME_SIZE];
263 if (printk_ratelimit())
264 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
265 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
266 reschedule_retry(r1_bio);
269 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
273 static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
275 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
276 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
278 conf_t *conf = mddev_to_conf(r1_bio->mddev);
283 for (mirror = 0; mirror < conf->raid_disks; mirror++)
284 if (r1_bio->bios[mirror] == bio)
288 * this branch is our 'one mirror IO has finished' event handler:
291 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
294 * Set R1BIO_Uptodate in our master bio, so that
295 * we will return a good error code for to the higher
296 * levels even if IO on some other mirrored buffer fails.
298 * The 'master' represents the composite IO operation to
299 * user-side. So if something waits for IO, then it will
300 * wait for the 'master' bio.
302 set_bit(R1BIO_Uptodate, &r1_bio->state);
304 update_head_pos(mirror, r1_bio);
308 * Let's see if all mirrored write operations have finished
311 if (atomic_dec_and_test(&r1_bio->remaining)) {
312 md_write_end(r1_bio->mddev);
313 raid_end_bio_io(r1_bio);
316 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
322 * This routine returns the disk from which the requested read should
323 * be done. There is a per-array 'next expected sequential IO' sector
324 * number - if this matches on the next IO then we use the last disk.
325 * There is also a per-disk 'last know head position' sector that is
326 * maintained from IRQ contexts, both the normal and the resync IO
327 * completion handlers update this position correctly. If there is no
328 * perfect sequential match then we pick the disk whose head is closest.
330 * If there are 2 mirrors in the same 2 devices, performance degrades
331 * because position is mirror, not device based.
333 * The rdev for the device selected will have nr_pending incremented.
335 static int read_balance(conf_t *conf, r1bio_t *r1_bio)
337 const unsigned long this_sector = r1_bio->sector;
338 int new_disk = conf->last_used, disk = new_disk;
339 const int sectors = r1_bio->sectors;
340 sector_t new_distance, current_distance;
341 mdk_rdev_t *new_rdev, *rdev;
345 * Check if it if we can balance. We can balance on the whole
346 * device if no resync is going on, or below the resync window.
347 * We take the first readable disk when above the resync window.
350 if (conf->mddev->recovery_cp < MaxSector &&
351 (this_sector + sectors >= conf->next_resync)) {
352 /* Choose the first operation device, for consistancy */
355 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
356 !new_rdev->in_sync) {
358 if (new_disk == conf->raid_disks) {
367 /* make sure the disk is operational */
368 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
369 !new_rdev->in_sync) {
371 new_disk = conf->raid_disks;
373 if (new_disk == disk) {
379 /* now disk == new_disk == starting point for search */
382 * Don't change to another disk for sequential reads:
384 if (conf->next_seq_sect == this_sector)
386 if (this_sector == conf->mirrors[new_disk].head_position)
389 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
391 /* Find the disk whose head is closest */
395 disk = conf->raid_disks;
398 if ((rdev=conf->mirrors[disk].rdev) == NULL ||
402 if (!atomic_read(&rdev->nr_pending)) {
407 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
408 if (new_distance < current_distance) {
409 current_distance = new_distance;
413 } while (disk != conf->last_used);
419 conf->next_seq_sect = this_sector + sectors;
420 conf->last_used = new_disk;
421 atomic_inc(&new_rdev->nr_pending);
422 if (!new_rdev->in_sync) {
423 /* cannot risk returning a device that failed
424 * before we inc'ed nr_pending
426 atomic_dec(&new_rdev->nr_pending);
435 static void unplug_slaves(mddev_t *mddev)
437 conf_t *conf = mddev_to_conf(mddev);
441 for (i=0; i<mddev->raid_disks; i++) {
442 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
443 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
444 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
446 atomic_inc(&rdev->nr_pending);
449 if (r_queue->unplug_fn)
450 r_queue->unplug_fn(r_queue);
452 rdev_dec_pending(rdev, mddev);
459 static void raid1_unplug(request_queue_t *q)
461 unplug_slaves(q->queuedata);
464 static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
465 sector_t *error_sector)
467 mddev_t *mddev = q->queuedata;
468 conf_t *conf = mddev_to_conf(mddev);
472 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
473 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
474 if (rdev && !rdev->faulty) {
475 struct block_device *bdev = rdev->bdev;
476 request_queue_t *r_queue = bdev_get_queue(bdev);
478 if (!r_queue->issue_flush_fn)
481 atomic_inc(&rdev->nr_pending);
483 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
485 rdev_dec_pending(rdev, mddev);
495 * Throttle resync depth, so that we can both get proper overlapping of
496 * requests, but are still able to handle normal requests quickly.
498 #define RESYNC_DEPTH 32
500 static void device_barrier(conf_t *conf, sector_t sect)
502 spin_lock_irq(&conf->resync_lock);
503 wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
504 conf->resync_lock, unplug_slaves(conf->mddev));
506 if (!conf->barrier++) {
507 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
508 conf->resync_lock, unplug_slaves(conf->mddev));
509 if (conf->nr_pending)
512 wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
513 conf->resync_lock, unplug_slaves(conf->mddev));
514 conf->next_resync = sect;
515 spin_unlock_irq(&conf->resync_lock);
518 static int make_request(request_queue_t *q, struct bio * bio)
520 mddev_t *mddev = q->queuedata;
521 conf_t *conf = mddev_to_conf(mddev);
522 mirror_info_t *mirror;
524 struct bio *read_bio;
529 * Register the new request and wait if the reconstruction
530 * thread has put up a bar for new requests.
531 * Continue immediately if no resync is active currently.
533 if (md_write_start(mddev, bio)==0)
535 spin_lock_irq(&conf->resync_lock);
536 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
538 spin_unlock_irq(&conf->resync_lock);
540 if (bio_data_dir(bio)==WRITE) {
541 disk_stat_inc(mddev->gendisk, writes);
542 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
544 disk_stat_inc(mddev->gendisk, reads);
545 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
549 * make_request() can abort the operation when READA is being
550 * used and no empty request is available.
553 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
555 r1_bio->master_bio = bio;
556 r1_bio->sectors = bio->bi_size >> 9;
558 r1_bio->mddev = mddev;
559 r1_bio->sector = bio->bi_sector;
563 if (bio_data_dir(bio) == READ) {
565 * read balancing logic:
567 int rdisk = read_balance(conf, r1_bio);
570 /* couldn't find anywhere to read from */
571 raid_end_bio_io(r1_bio);
574 mirror = conf->mirrors + rdisk;
576 r1_bio->read_disk = rdisk;
578 read_bio = bio_clone(bio, GFP_NOIO);
580 r1_bio->bios[rdisk] = read_bio;
582 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
583 read_bio->bi_bdev = mirror->rdev->bdev;
584 read_bio->bi_end_io = raid1_end_read_request;
585 read_bio->bi_rw = READ;
586 read_bio->bi_private = r1_bio;
588 generic_make_request(read_bio);
595 /* first select target devices under spinlock and
596 * inc refcount on their rdev. Record them by setting
599 disks = conf->raid_disks;
601 for (i = 0; i < disks; i++) {
602 if ((rdev=conf->mirrors[i].rdev) != NULL &&
604 atomic_inc(&rdev->nr_pending);
606 atomic_dec(&rdev->nr_pending);
607 r1_bio->bios[i] = NULL;
609 r1_bio->bios[i] = bio;
611 r1_bio->bios[i] = NULL;
615 atomic_set(&r1_bio->remaining, 1);
617 for (i = 0; i < disks; i++) {
619 if (!r1_bio->bios[i])
622 mbio = bio_clone(bio, GFP_NOIO);
623 r1_bio->bios[i] = mbio;
625 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
626 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
627 mbio->bi_end_io = raid1_end_write_request;
629 mbio->bi_private = r1_bio;
631 atomic_inc(&r1_bio->remaining);
632 generic_make_request(mbio);
635 if (atomic_dec_and_test(&r1_bio->remaining)) {
637 raid_end_bio_io(r1_bio);
643 static void status(struct seq_file *seq, mddev_t *mddev)
645 conf_t *conf = mddev_to_conf(mddev);
648 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
649 conf->working_disks);
650 for (i = 0; i < conf->raid_disks; i++)
651 seq_printf(seq, "%s",
652 conf->mirrors[i].rdev &&
653 conf->mirrors[i].rdev->in_sync ? "U" : "_");
654 seq_printf(seq, "]");
658 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
660 char b[BDEVNAME_SIZE];
661 conf_t *conf = mddev_to_conf(mddev);
664 * If it is not operational, then we have already marked it as dead
665 * else if it is the last working disks, ignore the error, let the
666 * next level up know.
667 * else mark the drive as failed
670 && conf->working_disks == 1)
672 * Don't fail the drive, act as though we were just a
673 * normal single drive
678 conf->working_disks--;
680 * if recovery is running, make sure it aborts.
682 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
687 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
688 " Operation continuing on %d devices\n",
689 bdevname(rdev->bdev,b), conf->working_disks);
692 static void print_conf(conf_t *conf)
697 printk("RAID1 conf printout:\n");
702 printk(" --- wd:%d rd:%d\n", conf->working_disks,
705 for (i = 0; i < conf->raid_disks; i++) {
706 char b[BDEVNAME_SIZE];
707 tmp = conf->mirrors + i;
709 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
710 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
711 bdevname(tmp->rdev->bdev,b));
715 static void close_sync(conf_t *conf)
717 spin_lock_irq(&conf->resync_lock);
718 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
719 conf->resync_lock, unplug_slaves(conf->mddev));
720 spin_unlock_irq(&conf->resync_lock);
722 if (conf->barrier) BUG();
723 if (waitqueue_active(&conf->wait_idle)) BUG();
725 mempool_destroy(conf->r1buf_pool);
726 conf->r1buf_pool = NULL;
729 static int raid1_spare_active(mddev_t *mddev)
732 conf_t *conf = mddev->private;
736 * Find all failed disks within the RAID1 configuration
737 * and mark them readable
739 for (i = 0; i < conf->raid_disks; i++) {
740 tmp = conf->mirrors + i;
742 && !tmp->rdev->faulty
743 && !tmp->rdev->in_sync) {
744 conf->working_disks++;
746 tmp->rdev->in_sync = 1;
755 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
757 conf_t *conf = mddev->private;
762 for (mirror=0; mirror < mddev->raid_disks; mirror++)
763 if ( !(p=conf->mirrors+mirror)->rdev) {
765 blk_queue_stack_limits(mddev->queue,
766 rdev->bdev->bd_disk->queue);
767 /* as we don't honour merge_bvec_fn, we must never risk
768 * violating it, so limit ->max_sector to one PAGE, as
769 * a one page request is never in violation.
771 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
772 mddev->queue->max_sectors > (PAGE_SIZE>>9))
773 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
775 p->head_position = 0;
776 rdev->raid_disk = mirror;
786 static int raid1_remove_disk(mddev_t *mddev, int number)
788 conf_t *conf = mddev->private;
791 mirror_info_t *p = conf->mirrors+ number;
797 atomic_read(&rdev->nr_pending)) {
803 if (atomic_read(&rdev->nr_pending)) {
804 /* lost the race, try later */
816 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
818 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
819 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
820 conf_t *conf = mddev_to_conf(r1_bio->mddev);
825 if (r1_bio->bios[r1_bio->read_disk] != bio)
827 update_head_pos(r1_bio->read_disk, r1_bio);
829 * we have read a block, now it needs to be re-written,
830 * or re-read if the read failed.
831 * We don't do much here, just schedule handling by raid1d
834 md_error(r1_bio->mddev,
835 conf->mirrors[r1_bio->read_disk].rdev);
837 set_bit(R1BIO_Uptodate, &r1_bio->state);
838 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
839 reschedule_retry(r1_bio);
843 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
845 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
846 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
847 mddev_t *mddev = r1_bio->mddev;
848 conf_t *conf = mddev_to_conf(mddev);
855 for (i = 0; i < conf->raid_disks; i++)
856 if (r1_bio->bios[i] == bio) {
861 md_error(mddev, conf->mirrors[mirror].rdev);
862 update_head_pos(mirror, r1_bio);
864 if (atomic_dec_and_test(&r1_bio->remaining)) {
865 md_done_sync(mddev, r1_bio->sectors, uptodate);
868 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
872 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
874 conf_t *conf = mddev_to_conf(mddev);
876 int disks = conf->raid_disks;
877 struct bio *bio, *wbio;
879 bio = r1_bio->bios[r1_bio->read_disk];
884 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
886 * There is no point trying a read-for-reconstruct as
887 * reconstruct is about to be aborted
889 char b[BDEVNAME_SIZE];
890 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
892 bdevname(bio->bi_bdev,b),
893 (unsigned long long)r1_bio->sector);
894 md_done_sync(mddev, r1_bio->sectors, 0);
899 atomic_set(&r1_bio->remaining, 1);
900 for (i = 0; i < disks ; i++) {
901 wbio = r1_bio->bios[i];
902 if (wbio->bi_end_io != end_sync_write)
905 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
906 atomic_inc(&r1_bio->remaining);
907 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
908 generic_make_request(wbio);
911 if (atomic_dec_and_test(&r1_bio->remaining)) {
912 md_done_sync(mddev, r1_bio->sectors, 1);
918 * This is a kernel thread which:
920 * 1. Retries failed read operations on working mirrors.
921 * 2. Updates the raid superblock when problems encounter.
922 * 3. Performs writes following reads for array syncronising.
925 static void raid1d(mddev_t *mddev)
930 conf_t *conf = mddev_to_conf(mddev);
931 struct list_head *head = &conf->retry_list;
935 md_check_recovery(mddev);
938 char b[BDEVNAME_SIZE];
939 spin_lock_irqsave(&conf->device_lock, flags);
940 if (list_empty(head))
942 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
943 list_del(head->prev);
944 spin_unlock_irqrestore(&conf->device_lock, flags);
946 mddev = r1_bio->mddev;
947 conf = mddev_to_conf(mddev);
948 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
949 sync_request_write(mddev, r1_bio);
953 bio = r1_bio->bios[r1_bio->read_disk];
954 if ((disk=read_balance(conf, r1_bio)) == -1) {
955 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
956 " read error for block %llu\n",
957 bdevname(bio->bi_bdev,b),
958 (unsigned long long)r1_bio->sector);
959 raid_end_bio_io(r1_bio);
961 r1_bio->bios[r1_bio->read_disk] = NULL;
962 r1_bio->read_disk = disk;
964 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
965 r1_bio->bios[r1_bio->read_disk] = bio;
966 rdev = conf->mirrors[disk].rdev;
967 if (printk_ratelimit())
968 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
970 bdevname(rdev->bdev,b),
971 (unsigned long long)r1_bio->sector);
972 bio->bi_sector = r1_bio->sector + rdev->data_offset;
973 bio->bi_bdev = rdev->bdev;
974 bio->bi_end_io = raid1_end_read_request;
976 bio->bi_private = r1_bio;
978 generic_make_request(bio);
982 spin_unlock_irqrestore(&conf->device_lock, flags);
984 unplug_slaves(mddev);
988 static int init_resync(conf_t *conf)
992 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
993 if (conf->r1buf_pool)
995 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
997 if (!conf->r1buf_pool)
999 conf->next_resync = 0;
1004 * perform a "sync" on one "block"
1006 * We need to make sure that no normal I/O request - particularly write
1007 * requests - conflict with active sync requests.
1009 * This is achieved by tracking pending requests and a 'barrier' concept
1010 * that can be installed to exclude normal IO requests.
1013 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1015 conf_t *conf = mddev_to_conf(mddev);
1016 mirror_info_t *mirror;
1019 sector_t max_sector, nr_sectors;
1022 int write_targets = 0;
1024 if (!conf->r1buf_pool)
1025 if (init_resync(conf))
1028 max_sector = mddev->size << 1;
1029 if (sector_nr >= max_sector) {
1035 * If there is non-resync activity waiting for us then
1036 * put in a delay to throttle resync.
1038 if (!go_faster && waitqueue_active(&conf->wait_resume))
1039 msleep_interruptible(1000);
1040 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1043 * If reconstructing, and >1 working disc,
1044 * could dedicate one to rebuild and others to
1045 * service read requests ..
1047 disk = conf->last_used;
1048 /* make sure disk is operational */
1050 while (conf->mirrors[disk].rdev == NULL ||
1051 !conf->mirrors[disk].rdev->in_sync) {
1053 disk = conf->raid_disks;
1055 if (disk == conf->last_used)
1058 conf->last_used = disk;
1059 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1062 mirror = conf->mirrors + disk;
1064 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1066 spin_lock_irq(&conf->resync_lock);
1068 spin_unlock_irq(&conf->resync_lock);
1070 r1_bio->mddev = mddev;
1071 r1_bio->sector = sector_nr;
1072 set_bit(R1BIO_IsSync, &r1_bio->state);
1073 r1_bio->read_disk = disk;
1075 for (i=0; i < conf->raid_disks; i++) {
1076 bio = r1_bio->bios[i];
1078 /* take from bio_init */
1079 bio->bi_next = NULL;
1080 bio->bi_flags |= 1 << BIO_UPTODATE;
1084 bio->bi_phys_segments = 0;
1085 bio->bi_hw_segments = 0;
1087 bio->bi_end_io = NULL;
1088 bio->bi_private = NULL;
1092 bio->bi_end_io = end_sync_read;
1093 } else if (conf->mirrors[i].rdev &&
1094 !conf->mirrors[i].rdev->faulty &&
1095 (!conf->mirrors[i].rdev->in_sync ||
1096 sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1098 bio->bi_end_io = end_sync_write;
1102 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1103 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1104 bio->bi_private = r1_bio;
1106 if (write_targets == 0) {
1107 /* There is nowhere to write, so all non-sync
1108 * drives must be failed - so we are finished
1110 sector_t rv = max_sector - sector_nr;
1113 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1120 int len = PAGE_SIZE;
1121 if (sector_nr + (len>>9) > max_sector)
1122 len = (max_sector - sector_nr) << 9;
1125 for (i=0 ; i < conf->raid_disks; i++) {
1126 bio = r1_bio->bios[i];
1127 if (bio->bi_end_io) {
1128 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1129 if (bio_add_page(bio, page, len, 0) == 0) {
1131 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1134 bio = r1_bio->bios[i];
1135 if (bio->bi_end_io==NULL) continue;
1136 /* remove last page from this bio */
1138 bio->bi_size -= len;
1139 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1145 nr_sectors += len>>9;
1146 sector_nr += len>>9;
1147 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1149 bio = r1_bio->bios[disk];
1150 r1_bio->sectors = nr_sectors;
1152 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1154 generic_make_request(bio);
1159 static int run(mddev_t *mddev)
1163 mirror_info_t *disk;
1165 struct list_head *tmp;
1167 if (mddev->level != 1) {
1168 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1169 mdname(mddev), mddev->level);
1173 * copy the already verified devices into our private RAID1
1174 * bookkeeping area. [whatever we allocate in run(),
1175 * should be freed in stop()]
1177 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1178 mddev->private = conf;
1182 memset(conf, 0, sizeof(*conf));
1183 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1188 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1190 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1191 if (!conf->poolinfo)
1193 conf->poolinfo->mddev = mddev;
1194 conf->poolinfo->raid_disks = mddev->raid_disks;
1195 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1198 if (!conf->r1bio_pool)
1201 ITERATE_RDEV(mddev, rdev, tmp) {
1202 disk_idx = rdev->raid_disk;
1203 if (disk_idx >= mddev->raid_disks
1206 disk = conf->mirrors + disk_idx;
1210 blk_queue_stack_limits(mddev->queue,
1211 rdev->bdev->bd_disk->queue);
1212 /* as we don't honour merge_bvec_fn, we must never risk
1213 * violating it, so limit ->max_sector to one PAGE, as
1214 * a one page request is never in violation.
1216 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1217 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1218 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1220 disk->head_position = 0;
1221 if (!rdev->faulty && rdev->in_sync)
1222 conf->working_disks++;
1224 conf->raid_disks = mddev->raid_disks;
1225 conf->mddev = mddev;
1226 spin_lock_init(&conf->device_lock);
1227 INIT_LIST_HEAD(&conf->retry_list);
1228 if (conf->working_disks == 1)
1229 mddev->recovery_cp = MaxSector;
1231 spin_lock_init(&conf->resync_lock);
1232 init_waitqueue_head(&conf->wait_idle);
1233 init_waitqueue_head(&conf->wait_resume);
1235 if (!conf->working_disks) {
1236 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1241 mddev->degraded = 0;
1242 for (i = 0; i < conf->raid_disks; i++) {
1244 disk = conf->mirrors + i;
1247 disk->head_position = 0;
1253 * find the first working one and use it as a starting point
1254 * to read balancing.
1256 for (j = 0; j < conf->raid_disks &&
1257 (!conf->mirrors[j].rdev ||
1258 !conf->mirrors[j].rdev->in_sync) ; j++)
1260 conf->last_used = j;
1265 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1266 if (!mddev->thread) {
1268 "raid1: couldn't allocate thread for %s\n",
1274 "raid1: raid set %s active with %d out of %d mirrors\n",
1275 mdname(mddev), mddev->raid_disks - mddev->degraded,
1278 * Ok, everything is just fine now
1280 mddev->array_size = mddev->size;
1282 mddev->queue->unplug_fn = raid1_unplug;
1283 mddev->queue->issue_flush_fn = raid1_issue_flush;
1288 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1293 if (conf->r1bio_pool)
1294 mempool_destroy(conf->r1bio_pool);
1296 kfree(conf->mirrors);
1298 kfree(conf->poolinfo);
1300 mddev->private = NULL;
1306 static int stop(mddev_t *mddev)
1308 conf_t *conf = mddev_to_conf(mddev);
1310 md_unregister_thread(mddev->thread);
1311 mddev->thread = NULL;
1312 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1313 if (conf->r1bio_pool)
1314 mempool_destroy(conf->r1bio_pool);
1316 kfree(conf->mirrors);
1318 kfree(conf->poolinfo);
1320 mddev->private = NULL;
1324 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1326 /* no resync is happening, and there is enough space
1327 * on all devices, so we can resize.
1328 * We need to make sure resync covers any new space.
1329 * If the array is shrinking we should possibly wait until
1330 * any io in the removed space completes, but it hardly seems
1333 mddev->array_size = sectors>>1;
1334 set_capacity(mddev->gendisk, mddev->array_size << 1);
1336 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1337 mddev->recovery_cp = mddev->size << 1;
1338 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1340 mddev->size = mddev->array_size;
1344 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1347 * 1/ resize the r1bio_pool
1348 * 2/ resize conf->mirrors
1350 * We allocate a new r1bio_pool if we can.
1351 * Then raise a device barrier and wait until all IO stops.
1352 * Then resize conf->mirrors and swap in the new r1bio pool.
1354 * At the same time, we "pack" the devices so that all the missing
1355 * devices have the higher raid_disk numbers.
1357 mempool_t *newpool, *oldpool;
1358 struct pool_info *newpoolinfo;
1359 mirror_info_t *newmirrors;
1360 conf_t *conf = mddev_to_conf(mddev);
1365 if (raid_disks < conf->raid_disks) {
1367 for (d= 0; d < conf->raid_disks; d++)
1368 if (conf->mirrors[d].rdev)
1370 if (cnt > raid_disks)
1374 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1377 newpoolinfo->mddev = mddev;
1378 newpoolinfo->raid_disks = raid_disks;
1380 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1381 r1bio_pool_free, newpoolinfo);
1386 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1389 mempool_destroy(newpool);
1392 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1394 spin_lock_irq(&conf->resync_lock);
1396 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
1397 conf->resync_lock, unplug_slaves(mddev));
1398 spin_unlock_irq(&conf->resync_lock);
1400 /* ok, everything is stopped */
1401 oldpool = conf->r1bio_pool;
1402 conf->r1bio_pool = newpool;
1404 for (d=d2=0; d < conf->raid_disks; d++)
1405 if (conf->mirrors[d].rdev) {
1406 conf->mirrors[d].rdev->raid_disk = d2;
1407 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1409 kfree(conf->mirrors);
1410 conf->mirrors = newmirrors;
1411 kfree(conf->poolinfo);
1412 conf->poolinfo = newpoolinfo;
1414 mddev->degraded += (raid_disks - conf->raid_disks);
1415 conf->raid_disks = mddev->raid_disks = raid_disks;
1417 conf->last_used = 0; /* just make sure it is in-range */
1418 spin_lock_irq(&conf->resync_lock);
1420 spin_unlock_irq(&conf->resync_lock);
1421 wake_up(&conf->wait_resume);
1422 wake_up(&conf->wait_idle);
1425 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1426 md_wakeup_thread(mddev->thread);
1428 mempool_destroy(oldpool);
1433 static mdk_personality_t raid1_personality =
1436 .owner = THIS_MODULE,
1437 .make_request = make_request,
1441 .error_handler = error,
1442 .hot_add_disk = raid1_add_disk,
1443 .hot_remove_disk= raid1_remove_disk,
1444 .spare_active = raid1_spare_active,
1445 .sync_request = sync_request,
1446 .resize = raid1_resize,
1447 .reshape = raid1_reshape,
1450 static int __init raid_init(void)
1452 return register_md_personality(RAID1, &raid1_personality);
1455 static void raid_exit(void)
1457 unregister_md_personality(RAID1);
1460 module_init(raid_init);
1461 module_exit(raid_exit);
1462 MODULE_LICENSE("GPL");
1463 MODULE_ALIAS("md-personality-3"); /* RAID1 */