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 spin_lock_irq(&conf->resync_lock);
534 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
536 spin_unlock_irq(&conf->resync_lock);
538 if (bio_data_dir(bio)==WRITE) {
539 disk_stat_inc(mddev->gendisk, writes);
540 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
542 disk_stat_inc(mddev->gendisk, reads);
543 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
547 * make_request() can abort the operation when READA is being
548 * used and no empty request is available.
551 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
553 r1_bio->master_bio = bio;
554 r1_bio->sectors = bio->bi_size >> 9;
556 r1_bio->mddev = mddev;
557 r1_bio->sector = bio->bi_sector;
561 if (bio_data_dir(bio) == READ) {
563 * read balancing logic:
565 int rdisk = read_balance(conf, r1_bio);
568 /* couldn't find anywhere to read from */
569 raid_end_bio_io(r1_bio);
572 mirror = conf->mirrors + rdisk;
574 r1_bio->read_disk = rdisk;
576 read_bio = bio_clone(bio, GFP_NOIO);
578 r1_bio->bios[rdisk] = read_bio;
580 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
581 read_bio->bi_bdev = mirror->rdev->bdev;
582 read_bio->bi_end_io = raid1_end_read_request;
583 read_bio->bi_rw = READ;
584 read_bio->bi_private = r1_bio;
586 generic_make_request(read_bio);
593 /* first select target devices under spinlock and
594 * inc refcount on their rdev. Record them by setting
597 disks = conf->raid_disks;
599 for (i = 0; i < disks; i++) {
600 if ((rdev=conf->mirrors[i].rdev) != NULL &&
602 atomic_inc(&rdev->nr_pending);
604 atomic_dec(&rdev->nr_pending);
605 r1_bio->bios[i] = NULL;
607 r1_bio->bios[i] = bio;
609 r1_bio->bios[i] = NULL;
613 atomic_set(&r1_bio->remaining, 1);
614 md_write_start(mddev);
615 for (i = 0; i < disks; i++) {
617 if (!r1_bio->bios[i])
620 mbio = bio_clone(bio, GFP_NOIO);
621 r1_bio->bios[i] = mbio;
623 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
624 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
625 mbio->bi_end_io = raid1_end_write_request;
627 mbio->bi_private = r1_bio;
629 atomic_inc(&r1_bio->remaining);
630 generic_make_request(mbio);
633 if (atomic_dec_and_test(&r1_bio->remaining)) {
635 raid_end_bio_io(r1_bio);
641 static void status(struct seq_file *seq, mddev_t *mddev)
643 conf_t *conf = mddev_to_conf(mddev);
646 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
647 conf->working_disks);
648 for (i = 0; i < conf->raid_disks; i++)
649 seq_printf(seq, "%s",
650 conf->mirrors[i].rdev &&
651 conf->mirrors[i].rdev->in_sync ? "U" : "_");
652 seq_printf(seq, "]");
656 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
658 char b[BDEVNAME_SIZE];
659 conf_t *conf = mddev_to_conf(mddev);
662 * If it is not operational, then we have already marked it as dead
663 * else if it is the last working disks, ignore the error, let the
664 * next level up know.
665 * else mark the drive as failed
668 && conf->working_disks == 1)
670 * Don't fail the drive, act as though we were just a
671 * normal single drive
676 conf->working_disks--;
678 * if recovery is running, make sure it aborts.
680 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
685 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
686 " Operation continuing on %d devices\n",
687 bdevname(rdev->bdev,b), conf->working_disks);
690 static void print_conf(conf_t *conf)
695 printk("RAID1 conf printout:\n");
700 printk(" --- wd:%d rd:%d\n", conf->working_disks,
703 for (i = 0; i < conf->raid_disks; i++) {
704 char b[BDEVNAME_SIZE];
705 tmp = conf->mirrors + i;
707 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
708 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
709 bdevname(tmp->rdev->bdev,b));
713 static void close_sync(conf_t *conf)
715 spin_lock_irq(&conf->resync_lock);
716 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
717 conf->resync_lock, unplug_slaves(conf->mddev));
718 spin_unlock_irq(&conf->resync_lock);
720 if (conf->barrier) BUG();
721 if (waitqueue_active(&conf->wait_idle)) BUG();
723 mempool_destroy(conf->r1buf_pool);
724 conf->r1buf_pool = NULL;
727 static int raid1_spare_active(mddev_t *mddev)
730 conf_t *conf = mddev->private;
734 * Find all failed disks within the RAID1 configuration
735 * and mark them readable
737 for (i = 0; i < conf->raid_disks; i++) {
738 tmp = conf->mirrors + i;
740 && !tmp->rdev->faulty
741 && !tmp->rdev->in_sync) {
742 conf->working_disks++;
744 tmp->rdev->in_sync = 1;
753 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
755 conf_t *conf = mddev->private;
760 for (mirror=0; mirror < mddev->raid_disks; mirror++)
761 if ( !(p=conf->mirrors+mirror)->rdev) {
763 blk_queue_stack_limits(mddev->queue,
764 rdev->bdev->bd_disk->queue);
765 /* as we don't honour merge_bvec_fn, we must never risk
766 * violating it, so limit ->max_sector to one PAGE, as
767 * a one page request is never in violation.
769 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
770 mddev->queue->max_sectors > (PAGE_SIZE>>9))
771 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
773 p->head_position = 0;
774 rdev->raid_disk = mirror;
784 static int raid1_remove_disk(mddev_t *mddev, int number)
786 conf_t *conf = mddev->private;
789 mirror_info_t *p = conf->mirrors+ number;
795 atomic_read(&rdev->nr_pending)) {
801 if (atomic_read(&rdev->nr_pending)) {
802 /* lost the race, try later */
814 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
816 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
817 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
818 conf_t *conf = mddev_to_conf(r1_bio->mddev);
823 if (r1_bio->bios[r1_bio->read_disk] != bio)
825 update_head_pos(r1_bio->read_disk, r1_bio);
827 * we have read a block, now it needs to be re-written,
828 * or re-read if the read failed.
829 * We don't do much here, just schedule handling by raid1d
832 md_error(r1_bio->mddev,
833 conf->mirrors[r1_bio->read_disk].rdev);
835 set_bit(R1BIO_Uptodate, &r1_bio->state);
836 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
837 reschedule_retry(r1_bio);
841 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
843 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
844 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
845 mddev_t *mddev = r1_bio->mddev;
846 conf_t *conf = mddev_to_conf(mddev);
853 for (i = 0; i < conf->raid_disks; i++)
854 if (r1_bio->bios[i] == bio) {
859 md_error(mddev, conf->mirrors[mirror].rdev);
860 update_head_pos(mirror, r1_bio);
862 if (atomic_dec_and_test(&r1_bio->remaining)) {
863 md_done_sync(mddev, r1_bio->sectors, uptodate);
866 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
870 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
872 conf_t *conf = mddev_to_conf(mddev);
874 int disks = conf->raid_disks;
875 struct bio *bio, *wbio;
877 bio = r1_bio->bios[r1_bio->read_disk];
882 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
884 * There is no point trying a read-for-reconstruct as
885 * reconstruct is about to be aborted
887 char b[BDEVNAME_SIZE];
888 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
890 bdevname(bio->bi_bdev,b),
891 (unsigned long long)r1_bio->sector);
892 md_done_sync(mddev, r1_bio->sectors, 0);
897 atomic_set(&r1_bio->remaining, 1);
898 for (i = 0; i < disks ; i++) {
899 wbio = r1_bio->bios[i];
900 if (wbio->bi_end_io != end_sync_write)
903 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
904 atomic_inc(&r1_bio->remaining);
905 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
906 generic_make_request(wbio);
909 if (atomic_dec_and_test(&r1_bio->remaining)) {
910 md_done_sync(mddev, r1_bio->sectors, 1);
916 * This is a kernel thread which:
918 * 1. Retries failed read operations on working mirrors.
919 * 2. Updates the raid superblock when problems encounter.
920 * 3. Performs writes following reads for array syncronising.
923 static void raid1d(mddev_t *mddev)
928 conf_t *conf = mddev_to_conf(mddev);
929 struct list_head *head = &conf->retry_list;
933 md_check_recovery(mddev);
934 md_handle_safemode(mddev);
937 char b[BDEVNAME_SIZE];
938 spin_lock_irqsave(&conf->device_lock, flags);
939 if (list_empty(head))
941 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
942 list_del(head->prev);
943 spin_unlock_irqrestore(&conf->device_lock, flags);
945 mddev = r1_bio->mddev;
946 conf = mddev_to_conf(mddev);
947 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
948 sync_request_write(mddev, r1_bio);
952 bio = r1_bio->bios[r1_bio->read_disk];
953 if ((disk=read_balance(conf, r1_bio)) == -1) {
954 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
955 " read error for block %llu\n",
956 bdevname(bio->bi_bdev,b),
957 (unsigned long long)r1_bio->sector);
958 raid_end_bio_io(r1_bio);
960 r1_bio->bios[r1_bio->read_disk] = NULL;
961 r1_bio->read_disk = disk;
963 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
964 r1_bio->bios[r1_bio->read_disk] = bio;
965 rdev = conf->mirrors[disk].rdev;
966 if (printk_ratelimit())
967 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
969 bdevname(rdev->bdev,b),
970 (unsigned long long)r1_bio->sector);
971 bio->bi_sector = r1_bio->sector + rdev->data_offset;
972 bio->bi_bdev = rdev->bdev;
973 bio->bi_end_io = raid1_end_read_request;
975 bio->bi_private = r1_bio;
977 generic_make_request(bio);
981 spin_unlock_irqrestore(&conf->device_lock, flags);
983 unplug_slaves(mddev);
987 static int init_resync(conf_t *conf)
991 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
992 if (conf->r1buf_pool)
994 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
996 if (!conf->r1buf_pool)
998 conf->next_resync = 0;
1003 * perform a "sync" on one "block"
1005 * We need to make sure that no normal I/O request - particularly write
1006 * requests - conflict with active sync requests.
1008 * This is achieved by tracking pending requests and a 'barrier' concept
1009 * that can be installed to exclude normal IO requests.
1012 static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster)
1014 conf_t *conf = mddev_to_conf(mddev);
1015 mirror_info_t *mirror;
1018 sector_t max_sector, nr_sectors;
1021 int write_targets = 0;
1023 if (!conf->r1buf_pool)
1024 if (init_resync(conf))
1027 max_sector = mddev->size << 1;
1028 if (sector_nr >= max_sector) {
1034 * If there is non-resync activity waiting for us then
1035 * put in a delay to throttle resync.
1037 if (!go_faster && waitqueue_active(&conf->wait_resume))
1038 msleep_interruptible(1000);
1039 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1042 * If reconstructing, and >1 working disc,
1043 * could dedicate one to rebuild and others to
1044 * service read requests ..
1046 disk = conf->last_used;
1047 /* make sure disk is operational */
1049 while (conf->mirrors[disk].rdev == NULL ||
1050 !conf->mirrors[disk].rdev->in_sync) {
1052 disk = conf->raid_disks;
1054 if (disk == conf->last_used)
1057 conf->last_used = disk;
1058 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1061 mirror = conf->mirrors + disk;
1063 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1065 spin_lock_irq(&conf->resync_lock);
1067 spin_unlock_irq(&conf->resync_lock);
1069 r1_bio->mddev = mddev;
1070 r1_bio->sector = sector_nr;
1071 set_bit(R1BIO_IsSync, &r1_bio->state);
1072 r1_bio->read_disk = disk;
1074 for (i=0; i < conf->raid_disks; i++) {
1075 bio = r1_bio->bios[i];
1077 /* take from bio_init */
1078 bio->bi_next = NULL;
1079 bio->bi_flags |= 1 << BIO_UPTODATE;
1083 bio->bi_phys_segments = 0;
1084 bio->bi_hw_segments = 0;
1086 bio->bi_end_io = NULL;
1087 bio->bi_private = NULL;
1091 bio->bi_end_io = end_sync_read;
1092 } else if (conf->mirrors[i].rdev &&
1093 !conf->mirrors[i].rdev->faulty &&
1094 (!conf->mirrors[i].rdev->in_sync ||
1095 sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1097 bio->bi_end_io = end_sync_write;
1101 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1102 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1103 bio->bi_private = r1_bio;
1105 if (write_targets == 0) {
1106 /* There is nowhere to write, so all non-sync
1107 * drives must be failed - so we are finished
1109 int rv = max_sector - sector_nr;
1110 md_done_sync(mddev, rv, 1);
1112 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1119 int len = PAGE_SIZE;
1120 if (sector_nr + (len>>9) > max_sector)
1121 len = (max_sector - sector_nr) << 9;
1124 for (i=0 ; i < conf->raid_disks; i++) {
1125 bio = r1_bio->bios[i];
1126 if (bio->bi_end_io) {
1127 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1128 if (bio_add_page(bio, page, len, 0) == 0) {
1130 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1133 bio = r1_bio->bios[i];
1134 if (bio->bi_end_io==NULL) continue;
1135 /* remove last page from this bio */
1137 bio->bi_size -= len;
1138 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1144 nr_sectors += len>>9;
1145 sector_nr += len>>9;
1146 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1148 bio = r1_bio->bios[disk];
1149 r1_bio->sectors = nr_sectors;
1151 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1153 generic_make_request(bio);
1158 static int run(mddev_t *mddev)
1162 mirror_info_t *disk;
1164 struct list_head *tmp;
1166 if (mddev->level != 1) {
1167 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1168 mdname(mddev), mddev->level);
1172 * copy the already verified devices into our private RAID1
1173 * bookkeeping area. [whatever we allocate in run(),
1174 * should be freed in stop()]
1176 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1177 mddev->private = conf;
1181 memset(conf, 0, sizeof(*conf));
1182 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1187 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1189 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1190 if (!conf->poolinfo)
1192 conf->poolinfo->mddev = mddev;
1193 conf->poolinfo->raid_disks = mddev->raid_disks;
1194 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1197 if (!conf->r1bio_pool)
1200 ITERATE_RDEV(mddev, rdev, tmp) {
1201 disk_idx = rdev->raid_disk;
1202 if (disk_idx >= mddev->raid_disks
1205 disk = conf->mirrors + disk_idx;
1209 blk_queue_stack_limits(mddev->queue,
1210 rdev->bdev->bd_disk->queue);
1211 /* as we don't honour merge_bvec_fn, we must never risk
1212 * violating it, so limit ->max_sector to one PAGE, as
1213 * a one page request is never in violation.
1215 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1216 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1217 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1219 disk->head_position = 0;
1220 if (!rdev->faulty && rdev->in_sync)
1221 conf->working_disks++;
1223 conf->raid_disks = mddev->raid_disks;
1224 conf->mddev = mddev;
1225 spin_lock_init(&conf->device_lock);
1226 INIT_LIST_HEAD(&conf->retry_list);
1227 if (conf->working_disks == 1)
1228 mddev->recovery_cp = MaxSector;
1230 spin_lock_init(&conf->resync_lock);
1231 init_waitqueue_head(&conf->wait_idle);
1232 init_waitqueue_head(&conf->wait_resume);
1234 if (!conf->working_disks) {
1235 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1240 mddev->degraded = 0;
1241 for (i = 0; i < conf->raid_disks; i++) {
1243 disk = conf->mirrors + i;
1246 disk->head_position = 0;
1252 * find the first working one and use it as a starting point
1253 * to read balancing.
1255 for (j = 0; j < conf->raid_disks &&
1256 (!conf->mirrors[j].rdev ||
1257 !conf->mirrors[j].rdev->in_sync) ; j++)
1259 conf->last_used = j;
1264 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1265 if (!mddev->thread) {
1267 "raid1: couldn't allocate thread for %s\n",
1273 "raid1: raid set %s active with %d out of %d mirrors\n",
1274 mdname(mddev), mddev->raid_disks - mddev->degraded,
1277 * Ok, everything is just fine now
1279 mddev->array_size = mddev->size;
1281 mddev->queue->unplug_fn = raid1_unplug;
1282 mddev->queue->issue_flush_fn = raid1_issue_flush;
1287 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1292 if (conf->r1bio_pool)
1293 mempool_destroy(conf->r1bio_pool);
1295 kfree(conf->mirrors);
1297 kfree(conf->poolinfo);
1299 mddev->private = NULL;
1305 static int stop(mddev_t *mddev)
1307 conf_t *conf = mddev_to_conf(mddev);
1309 md_unregister_thread(mddev->thread);
1310 mddev->thread = NULL;
1311 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1312 if (conf->r1bio_pool)
1313 mempool_destroy(conf->r1bio_pool);
1315 kfree(conf->mirrors);
1317 kfree(conf->poolinfo);
1319 mddev->private = NULL;
1323 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1325 /* no resync is happening, and there is enough space
1326 * on all devices, so we can resize.
1327 * We need to make sure resync covers any new space.
1328 * If the array is shrinking we should possibly wait until
1329 * any io in the removed space completes, but it hardly seems
1332 mddev->array_size = sectors>>1;
1333 set_capacity(mddev->gendisk, mddev->array_size << 1);
1335 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1336 mddev->recovery_cp = mddev->size << 1;
1337 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1339 mddev->size = mddev->array_size;
1343 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1346 * 1/ resize the r1bio_pool
1347 * 2/ resize conf->mirrors
1349 * We allocate a new r1bio_pool if we can.
1350 * Then raise a device barrier and wait until all IO stops.
1351 * Then resize conf->mirrors and swap in the new r1bio pool.
1353 * At the same time, we "pack" the devices so that all the missing
1354 * devices have the higher raid_disk numbers.
1356 mempool_t *newpool, *oldpool;
1357 struct pool_info *newpoolinfo;
1358 mirror_info_t *newmirrors;
1359 conf_t *conf = mddev_to_conf(mddev);
1364 if (raid_disks < conf->raid_disks) {
1366 for (d= 0; d < conf->raid_disks; d++)
1367 if (conf->mirrors[d].rdev)
1369 if (cnt > raid_disks)
1373 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1376 newpoolinfo->mddev = mddev;
1377 newpoolinfo->raid_disks = raid_disks;
1379 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1380 r1bio_pool_free, newpoolinfo);
1385 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1388 mempool_destroy(newpool);
1391 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1393 spin_lock_irq(&conf->resync_lock);
1395 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
1396 conf->resync_lock, unplug_slaves(mddev));
1397 spin_unlock_irq(&conf->resync_lock);
1399 /* ok, everything is stopped */
1400 oldpool = conf->r1bio_pool;
1401 conf->r1bio_pool = newpool;
1403 for (d=d2=0; d < conf->raid_disks; d++)
1404 if (conf->mirrors[d].rdev) {
1405 conf->mirrors[d].rdev->raid_disk = d2;
1406 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1408 kfree(conf->mirrors);
1409 conf->mirrors = newmirrors;
1410 kfree(conf->poolinfo);
1411 conf->poolinfo = newpoolinfo;
1413 mddev->degraded += (raid_disks - conf->raid_disks);
1414 conf->raid_disks = mddev->raid_disks = raid_disks;
1416 conf->last_used = 0; /* just make sure it is in-range */
1417 spin_lock_irq(&conf->resync_lock);
1419 spin_unlock_irq(&conf->resync_lock);
1420 wake_up(&conf->wait_resume);
1421 wake_up(&conf->wait_idle);
1424 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1425 md_wakeup_thread(mddev->thread);
1427 mempool_destroy(oldpool);
1432 static mdk_personality_t raid1_personality =
1435 .owner = THIS_MODULE,
1436 .make_request = make_request,
1440 .error_handler = error,
1441 .hot_add_disk = raid1_add_disk,
1442 .hot_remove_disk= raid1_remove_disk,
1443 .spare_active = raid1_spare_active,
1444 .sync_request = sync_request,
1445 .resize = raid1_resize,
1446 .reshape = raid1_reshape,
1449 static int __init raid_init(void)
1451 return register_md_personality(RAID1, &raid1_personality);
1454 static void raid_exit(void)
1456 unregister_md_personality(RAID1);
1459 module_init(raid_init);
1460 module_exit(raid_exit);
1461 MODULE_LICENSE("GPL");
1462 MODULE_ALIAS("md-personality-3"); /* RAID1 */