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>
27 #define MAJOR_NR MD_MAJOR
29 #define MD_PERSONALITY
32 * Number of guaranteed r1bios in case of extreme VM load:
34 #define NR_RAID1_BIOS 256
36 static mdk_personality_t raid1_personality;
37 static spinlock_t retry_list_lock = SPIN_LOCK_UNLOCKED;
38 static LIST_HEAD(retry_list_head);
40 static void unplug_slaves(mddev_t *mddev);
43 static void * r1bio_pool_alloc(int gfp_flags, void *data)
45 mddev_t *mddev = data;
48 /* allocate a r1bio with room for raid_disks entries in the bios array */
49 r1_bio = kmalloc(sizeof(r1bio_t) + sizeof(struct bio*)*mddev->raid_disks,
52 memset(r1_bio, 0, sizeof(*r1_bio) + sizeof(struct bio*)*mddev->raid_disks);
59 static void r1bio_pool_free(void *r1_bio, void *data)
64 #define RESYNC_BLOCK_SIZE (64*1024)
65 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
66 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
67 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
68 #define RESYNC_WINDOW (2048*1024)
70 static void * r1buf_pool_alloc(int gfp_flags, void *data)
78 r1_bio = r1bio_pool_alloc(gfp_flags, conf->mddev);
80 unplug_slaves(conf->mddev);
85 * Allocate bios : 1 for reading, n-1 for writing
87 for (j = conf->raid_disks ; j-- ; ) {
88 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
91 r1_bio->bios[j] = bio;
94 * Allocate RESYNC_PAGES data pages and attach them to
97 bio = r1_bio->bios[0];
98 for (i = 0; i < RESYNC_PAGES; i++) {
99 page = alloc_page(gfp_flags);
103 bio->bi_io_vec[i].bv_page = page;
106 r1_bio->master_bio = bio;
112 __free_page(bio->bi_io_vec[i-1].bv_page);
114 while ( ++j < conf->raid_disks )
115 bio_put(r1_bio->bios[j]);
116 r1bio_pool_free(r1_bio, conf->mddev);
120 static void r1buf_pool_free(void *__r1_bio, void *data)
124 r1bio_t *r1bio = __r1_bio;
125 struct bio *bio = r1bio->bios[0];
127 for (i = 0; i < RESYNC_PAGES; i++) {
128 __free_page(bio->bi_io_vec[i].bv_page);
129 bio->bi_io_vec[i].bv_page = NULL;
131 for (i=0 ; i < conf->raid_disks; i++)
132 bio_put(r1bio->bios[i]);
134 r1bio_pool_free(r1bio, conf->mddev);
137 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
141 for (i = 0; i < conf->raid_disks; i++) {
142 struct bio **bio = r1_bio->bios + i;
149 static inline void free_r1bio(r1bio_t *r1_bio)
153 conf_t *conf = mddev_to_conf(r1_bio->mddev);
156 * Wake up any possible resync thread that waits for the device
159 spin_lock_irqsave(&conf->resync_lock, flags);
160 if (!--conf->nr_pending) {
161 wake_up(&conf->wait_idle);
162 wake_up(&conf->wait_resume);
164 spin_unlock_irqrestore(&conf->resync_lock, flags);
166 put_all_bios(conf, r1_bio);
167 mempool_free(r1_bio, conf->r1bio_pool);
170 static inline void put_buf(r1bio_t *r1_bio)
172 conf_t *conf = mddev_to_conf(r1_bio->mddev);
175 mempool_free(r1_bio, conf->r1buf_pool);
177 spin_lock_irqsave(&conf->resync_lock, flags);
181 wake_up(&conf->wait_resume);
182 wake_up(&conf->wait_idle);
184 if (!--conf->nr_pending) {
185 wake_up(&conf->wait_idle);
186 wake_up(&conf->wait_resume);
188 spin_unlock_irqrestore(&conf->resync_lock, flags);
191 static int map(mddev_t *mddev, mdk_rdev_t **rdevp)
193 conf_t *conf = mddev_to_conf(mddev);
194 int i, disks = conf->raid_disks;
197 * Later we do read balancing on the read side
198 * now we use the first available disk.
201 spin_lock_irq(&conf->device_lock);
202 for (i = 0; i < disks; i++) {
203 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
204 if (rdev && rdev->in_sync) {
206 atomic_inc(&rdev->nr_pending);
207 spin_unlock_irq(&conf->device_lock);
211 spin_unlock_irq(&conf->device_lock);
213 printk(KERN_ERR "raid1_map(): huh, no more operational devices?\n");
217 static void reschedule_retry(r1bio_t *r1_bio)
220 mddev_t *mddev = r1_bio->mddev;
222 spin_lock_irqsave(&retry_list_lock, flags);
223 list_add(&r1_bio->retry_list, &retry_list_head);
224 spin_unlock_irqrestore(&retry_list_lock, flags);
226 md_wakeup_thread(mddev->thread);
230 * raid_end_bio_io() is called when we have finished servicing a mirrored
231 * operation and are ready to return a success/failure code to the buffer
234 static void raid_end_bio_io(r1bio_t *r1_bio)
236 struct bio *bio = r1_bio->master_bio;
238 bio_endio(bio, bio->bi_size,
239 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
244 * Update disk head position estimator based on IRQ completion info.
246 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
248 conf_t *conf = mddev_to_conf(r1_bio->mddev);
250 conf->mirrors[disk].head_position =
251 r1_bio->sector + (r1_bio->sectors);
254 static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
256 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
257 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
259 conf_t *conf = mddev_to_conf(r1_bio->mddev);
264 mirror = r1_bio->read_disk;
266 * this branch is our 'one mirror IO has finished' event handler:
269 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
272 * Set R1BIO_Uptodate in our master bio, so that
273 * we will return a good error code for to the higher
274 * levels even if IO on some other mirrored buffer fails.
276 * The 'master' represents the composite IO operation to
277 * user-side. So if something waits for IO, then it will
278 * wait for the 'master' bio.
280 set_bit(R1BIO_Uptodate, &r1_bio->state);
282 update_head_pos(mirror, r1_bio);
285 * we have only one bio on the read side
288 raid_end_bio_io(r1_bio);
293 char b[BDEVNAME_SIZE];
294 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
295 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
296 reschedule_retry(r1_bio);
299 atomic_dec(&conf->mirrors[mirror].rdev->nr_pending);
303 static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
305 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
306 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
308 conf_t *conf = mddev_to_conf(r1_bio->mddev);
313 for (mirror = 0; mirror < conf->raid_disks; mirror++)
314 if (r1_bio->bios[mirror] == bio)
318 * this branch is our 'one mirror IO has finished' event handler:
321 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
324 * Set R1BIO_Uptodate in our master bio, so that
325 * we will return a good error code for to the higher
326 * levels even if IO on some other mirrored buffer fails.
328 * The 'master' represents the composite IO operation to
329 * user-side. So if something waits for IO, then it will
330 * wait for the 'master' bio.
332 set_bit(R1BIO_Uptodate, &r1_bio->state);
334 update_head_pos(mirror, r1_bio);
338 * Let's see if all mirrored write operations have finished
341 if (atomic_dec_and_test(&r1_bio->remaining)) {
342 md_write_end(r1_bio->mddev);
343 raid_end_bio_io(r1_bio);
346 atomic_dec(&conf->mirrors[mirror].rdev->nr_pending);
352 * This routine returns the disk from which the requested read should
353 * be done. There is a per-array 'next expected sequential IO' sector
354 * number - if this matches on the next IO then we use the last disk.
355 * There is also a per-disk 'last know head position' sector that is
356 * maintained from IRQ contexts, both the normal and the resync IO
357 * completion handlers update this position correctly. If there is no
358 * perfect sequential match then we pick the disk whose head is closest.
360 * If there are 2 mirrors in the same 2 devices, performance degrades
361 * because position is mirror, not device based.
363 * The rdev for the device selected will have nr_pending incremented.
365 static int read_balance(conf_t *conf, struct bio *bio, r1bio_t *r1_bio)
367 const unsigned long this_sector = r1_bio->sector;
368 int new_disk = conf->last_used, disk = new_disk;
369 const int sectors = bio->bi_size >> 9;
370 sector_t new_distance, current_distance;
372 spin_lock_irq(&conf->device_lock);
374 * Check if it if we can balance. We can balance on the whole
375 * device if no resync is going on, or below the resync window.
376 * We take the first readable disk when above the resync window.
378 if (!conf->mddev->in_sync && (this_sector + sectors >= conf->next_resync)) {
379 /* make sure that disk is operational */
382 while (!conf->mirrors[new_disk].rdev ||
383 !conf->mirrors[new_disk].rdev->in_sync) {
385 if (new_disk == conf->raid_disks) {
394 /* make sure the disk is operational */
395 while (!conf->mirrors[new_disk].rdev ||
396 !conf->mirrors[new_disk].rdev->in_sync) {
398 new_disk = conf->raid_disks;
400 if (new_disk == disk) {
401 new_disk = conf->last_used;
406 /* now disk == new_disk == starting point for search */
409 * Don't change to another disk for sequential reads:
411 if (conf->next_seq_sect == this_sector)
413 if (this_sector == conf->mirrors[new_disk].head_position)
416 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
418 /* Find the disk whose head is closest */
422 disk = conf->raid_disks;
425 if (!conf->mirrors[disk].rdev ||
426 !conf->mirrors[disk].rdev->in_sync)
429 if (!atomic_read(&conf->mirrors[disk].rdev->nr_pending)) {
433 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
434 if (new_distance < current_distance) {
435 current_distance = new_distance;
438 } while (disk != conf->last_used);
441 r1_bio->read_disk = new_disk;
442 conf->next_seq_sect = this_sector + sectors;
444 conf->last_used = new_disk;
446 if (conf->mirrors[new_disk].rdev)
447 atomic_inc(&conf->mirrors[new_disk].rdev->nr_pending);
448 spin_unlock_irq(&conf->device_lock);
453 static void unplug_slaves(mddev_t *mddev)
455 conf_t *conf = mddev_to_conf(mddev);
459 spin_lock_irqsave(&conf->device_lock, flags);
460 for (i=0; i<mddev->raid_disks; i++) {
461 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
462 if (rdev && atomic_read(&rdev->nr_pending)) {
463 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
465 atomic_inc(&rdev->nr_pending);
466 spin_unlock_irqrestore(&conf->device_lock, flags);
468 if (r_queue->unplug_fn)
469 r_queue->unplug_fn(r_queue);
471 spin_lock_irqsave(&conf->device_lock, flags);
472 atomic_dec(&rdev->nr_pending);
475 spin_unlock_irqrestore(&conf->device_lock, flags);
477 static void raid1_unplug(request_queue_t *q)
479 unplug_slaves(q->queuedata);
483 * Throttle resync depth, so that we can both get proper overlapping of
484 * requests, but are still able to handle normal requests quickly.
486 #define RESYNC_DEPTH 32
488 static void device_barrier(conf_t *conf, sector_t sect)
490 spin_lock_irq(&conf->resync_lock);
491 wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
492 conf->resync_lock, unplug_slaves(conf->mddev));
494 if (!conf->barrier++) {
495 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
496 conf->resync_lock, unplug_slaves(conf->mddev));
497 if (conf->nr_pending)
500 wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
501 conf->resync_lock, unplug_slaves(conf->mddev));
502 conf->next_resync = sect;
503 spin_unlock_irq(&conf->resync_lock);
506 static int make_request(request_queue_t *q, struct bio * bio)
508 mddev_t *mddev = q->queuedata;
509 conf_t *conf = mddev_to_conf(mddev);
510 mirror_info_t *mirror;
512 struct bio *read_bio;
513 int i, disks = conf->raid_disks;
516 * Register the new request and wait if the reconstruction
517 * thread has put up a bar for new requests.
518 * Continue immediately if no resync is active currently.
520 spin_lock_irq(&conf->resync_lock);
521 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
523 spin_unlock_irq(&conf->resync_lock);
525 if (bio_data_dir(bio)==WRITE) {
526 disk_stat_inc(mddev->gendisk, writes);
527 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
529 disk_stat_inc(mddev->gendisk, reads);
530 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
534 * make_request() can abort the operation when READA is being
535 * used and no empty request is available.
538 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
540 r1_bio->master_bio = bio;
541 r1_bio->sectors = bio->bi_size >> 9;
543 r1_bio->mddev = mddev;
544 r1_bio->sector = bio->bi_sector;
546 if (bio_data_dir(bio) == READ) {
548 * read balancing logic:
550 mirror = conf->mirrors + read_balance(conf, bio, r1_bio);
552 read_bio = bio_clone(bio, GFP_NOIO);
554 r1_bio->bios[r1_bio->read_disk] = read_bio;
556 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
557 read_bio->bi_bdev = mirror->rdev->bdev;
558 read_bio->bi_end_io = raid1_end_read_request;
559 read_bio->bi_rw = READ;
560 read_bio->bi_private = r1_bio;
562 generic_make_request(read_bio);
569 /* first select target devices under spinlock and
570 * inc refcount on their rdev. Record them by setting
573 spin_lock_irq(&conf->device_lock);
574 for (i = 0; i < disks; i++) {
575 if (conf->mirrors[i].rdev &&
576 !conf->mirrors[i].rdev->faulty) {
577 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
578 r1_bio->bios[i] = bio;
580 r1_bio->bios[i] = NULL;
582 spin_unlock_irq(&conf->device_lock);
584 atomic_set(&r1_bio->remaining, 1);
585 md_write_start(mddev);
586 for (i = 0; i < disks; i++) {
588 if (!r1_bio->bios[i])
591 mbio = bio_clone(bio, GFP_NOIO);
592 r1_bio->bios[i] = mbio;
594 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
595 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
596 mbio->bi_end_io = raid1_end_write_request;
598 mbio->bi_private = r1_bio;
600 atomic_inc(&r1_bio->remaining);
601 generic_make_request(mbio);
604 if (atomic_dec_and_test(&r1_bio->remaining)) {
606 raid_end_bio_io(r1_bio);
612 static void status(struct seq_file *seq, mddev_t *mddev)
614 conf_t *conf = mddev_to_conf(mddev);
617 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
618 conf->working_disks);
619 for (i = 0; i < conf->raid_disks; i++)
620 seq_printf(seq, "%s",
621 conf->mirrors[i].rdev &&
622 conf->mirrors[i].rdev->in_sync ? "U" : "_");
623 seq_printf(seq, "]");
627 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
629 char b[BDEVNAME_SIZE];
630 conf_t *conf = mddev_to_conf(mddev);
633 * If it is not operational, then we have already marked it as dead
634 * else if it is the last working disks, ignore the error, let the
635 * next level up know.
636 * else mark the drive as failed
639 && conf->working_disks == 1)
641 * Don't fail the drive, act as though we were just a
642 * normal single drive
647 conf->working_disks--;
649 * if recovery is running, make sure it aborts.
651 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
656 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
657 " Operation continuing on %d devices\n",
658 bdevname(rdev->bdev,b), conf->working_disks);
661 static void print_conf(conf_t *conf)
666 printk("RAID1 conf printout:\n");
671 printk(" --- wd:%d rd:%d\n", conf->working_disks,
674 for (i = 0; i < conf->raid_disks; i++) {
675 char b[BDEVNAME_SIZE];
676 tmp = conf->mirrors + i;
678 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
679 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
680 bdevname(tmp->rdev->bdev,b));
684 static void close_sync(conf_t *conf)
686 spin_lock_irq(&conf->resync_lock);
687 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
688 conf->resync_lock, unplug_slaves(conf->mddev));
689 spin_unlock_irq(&conf->resync_lock);
691 if (conf->barrier) BUG();
692 if (waitqueue_active(&conf->wait_idle)) BUG();
694 mempool_destroy(conf->r1buf_pool);
695 conf->r1buf_pool = NULL;
698 static int raid1_spare_active(mddev_t *mddev)
701 conf_t *conf = mddev->private;
704 spin_lock_irq(&conf->device_lock);
706 * Find all failed disks within the RAID1 configuration
707 * and mark them readable
709 for (i = 0; i < conf->raid_disks; i++) {
710 tmp = conf->mirrors + i;
712 && !tmp->rdev->faulty
713 && !tmp->rdev->in_sync) {
714 conf->working_disks++;
716 tmp->rdev->in_sync = 1;
719 spin_unlock_irq(&conf->device_lock);
726 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
728 conf_t *conf = mddev->private;
733 spin_lock_irq(&conf->device_lock);
734 for (mirror=0; mirror < mddev->raid_disks; mirror++)
735 if ( !(p=conf->mirrors+mirror)->rdev) {
738 blk_queue_stack_limits(mddev->queue,
739 rdev->bdev->bd_disk->queue);
740 /* as we don't honour merge_bvec_fn, we must never risk
741 * violating it, so limit ->max_sector to one PAGE, as
742 * a one page request is never in violation.
744 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
745 mddev->queue->max_sectors > (PAGE_SIZE>>9))
746 mddev->queue->max_sectors = (PAGE_SIZE>>9);
748 p->head_position = 0;
749 rdev->raid_disk = mirror;
753 spin_unlock_irq(&conf->device_lock);
759 static int raid1_remove_disk(mddev_t *mddev, int number)
761 conf_t *conf = mddev->private;
763 mirror_info_t *p = conf->mirrors+ number;
766 spin_lock_irq(&conf->device_lock);
768 if (p->rdev->in_sync ||
769 atomic_read(&p->rdev->nr_pending)) {
779 spin_unlock_irq(&conf->device_lock);
786 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
788 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
789 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
790 conf_t *conf = mddev_to_conf(r1_bio->mddev);
795 if (r1_bio->bios[r1_bio->read_disk] != bio)
797 update_head_pos(r1_bio->read_disk, r1_bio);
799 * we have read a block, now it needs to be re-written,
800 * or re-read if the read failed.
801 * We don't do much here, just schedule handling by raid1d
804 md_error(r1_bio->mddev,
805 conf->mirrors[r1_bio->read_disk].rdev);
807 set_bit(R1BIO_Uptodate, &r1_bio->state);
808 atomic_dec(&conf->mirrors[r1_bio->read_disk].rdev->nr_pending);
809 reschedule_retry(r1_bio);
813 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
815 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
816 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
817 mddev_t *mddev = r1_bio->mddev;
818 conf_t *conf = mddev_to_conf(mddev);
825 for (i = 0; i < conf->raid_disks; i++)
826 if (r1_bio->bios[i] == bio) {
831 md_error(mddev, conf->mirrors[mirror].rdev);
832 update_head_pos(mirror, r1_bio);
834 if (atomic_dec_and_test(&r1_bio->remaining)) {
835 md_done_sync(mddev, r1_bio->sectors, uptodate);
838 atomic_dec(&conf->mirrors[mirror].rdev->nr_pending);
842 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
844 conf_t *conf = mddev_to_conf(mddev);
846 int disks = conf->raid_disks;
847 struct bio *bio, *wbio;
849 bio = r1_bio->bios[r1_bio->read_disk];
854 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
856 * There is no point trying a read-for-reconstruct as
857 * reconstruct is about to be aborted
859 char b[BDEVNAME_SIZE];
860 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
862 bdevname(bio->bi_bdev,b),
863 (unsigned long long)r1_bio->sector);
864 md_done_sync(mddev, r1_bio->sectors, 0);
869 atomic_set(&r1_bio->remaining, 1);
870 for (i = 0; i < disks ; i++) {
871 wbio = r1_bio->bios[i];
872 if (wbio->bi_end_io != end_sync_write)
875 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
876 atomic_inc(&r1_bio->remaining);
877 md_sync_acct(conf->mirrors[i].rdev, wbio->bi_size >> 9);
878 generic_make_request(wbio);
881 if (atomic_dec_and_test(&r1_bio->remaining)) {
882 md_done_sync(mddev, r1_bio->sectors, 1);
888 * This is a kernel thread which:
890 * 1. Retries failed read operations on working mirrors.
891 * 2. Updates the raid superblock when problems encounter.
892 * 3. Performs writes following reads for array syncronising.
895 static void raid1d(mddev_t *mddev)
897 struct list_head *head = &retry_list_head;
901 conf_t *conf = mddev_to_conf(mddev);
905 md_check_recovery(mddev);
906 md_handle_safemode(mddev);
909 char b[BDEVNAME_SIZE];
910 spin_lock_irqsave(&retry_list_lock, flags);
911 if (list_empty(head))
913 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
914 list_del(head->prev);
915 spin_unlock_irqrestore(&retry_list_lock, flags);
917 mddev = r1_bio->mddev;
918 conf = mddev_to_conf(mddev);
919 bio = r1_bio->master_bio;
920 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
921 sync_request_write(mddev, r1_bio);
924 if (map(mddev, &rdev) == -1) {
925 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
926 " read error for block %llu\n",
927 bdevname(bio->bi_bdev,b),
928 (unsigned long long)r1_bio->sector);
929 raid_end_bio_io(r1_bio);
931 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
933 bdevname(rdev->bdev,b),
934 (unsigned long long)r1_bio->sector);
935 bio->bi_bdev = rdev->bdev;
936 bio->bi_sector = r1_bio->sector + rdev->data_offset;
939 generic_make_request(bio);
943 spin_unlock_irqrestore(&retry_list_lock, flags);
945 unplug_slaves(mddev);
949 static int init_resync(conf_t *conf)
953 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
954 if (conf->r1buf_pool)
956 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free, conf);
957 if (!conf->r1buf_pool)
959 conf->next_resync = 0;
964 * perform a "sync" on one "block"
966 * We need to make sure that no normal I/O request - particularly write
967 * requests - conflict with active sync requests.
969 * This is achieved by tracking pending requests and a 'barrier' concept
970 * that can be installed to exclude normal IO requests.
973 static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster)
975 conf_t *conf = mddev_to_conf(mddev);
976 mirror_info_t *mirror;
979 sector_t max_sector, nr_sectors;
983 if (!conf->r1buf_pool)
984 if (init_resync(conf))
987 max_sector = mddev->size << 1;
988 if (sector_nr >= max_sector) {
994 * If there is non-resync activity waiting for us then
995 * put in a delay to throttle resync.
997 if (!go_faster && waitqueue_active(&conf->wait_resume))
998 schedule_timeout(HZ);
999 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1002 * If reconstructing, and >1 working disc,
1003 * could dedicate one to rebuild and others to
1004 * service read requests ..
1006 disk = conf->last_used;
1007 /* make sure disk is operational */
1008 spin_lock_irq(&conf->device_lock);
1009 while (conf->mirrors[disk].rdev == NULL ||
1010 !conf->mirrors[disk].rdev->in_sync) {
1012 disk = conf->raid_disks;
1014 if (disk == conf->last_used)
1017 conf->last_used = disk;
1018 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1019 spin_unlock_irq(&conf->device_lock);
1021 mirror = conf->mirrors + disk;
1023 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1025 spin_lock_irq(&conf->resync_lock);
1027 spin_unlock_irq(&conf->resync_lock);
1029 r1_bio->mddev = mddev;
1030 r1_bio->sector = sector_nr;
1031 set_bit(R1BIO_IsSync, &r1_bio->state);
1032 r1_bio->read_disk = disk;
1034 for (i=0; i < conf->raid_disks; i++) {
1035 bio = r1_bio->bios[i];
1037 /* take from bio_init */
1038 bio->bi_next = NULL;
1039 bio->bi_flags |= 1 << BIO_UPTODATE;
1043 bio->bi_phys_segments = 0;
1044 bio->bi_hw_segments = 0;
1046 bio->bi_end_io = NULL;
1047 bio->bi_private = NULL;
1051 bio->bi_end_io = end_sync_read;
1052 } else if (conf->mirrors[i].rdev &&
1053 !conf->mirrors[i].rdev->faulty &&
1054 (!conf->mirrors[i].rdev->in_sync ||
1055 sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1057 bio->bi_end_io = end_sync_write;
1060 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1061 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1062 bio->bi_private = r1_bio;
1067 int len = PAGE_SIZE;
1068 if (sector_nr + (len>>9) > max_sector)
1069 len = (max_sector - sector_nr) << 9;
1072 for (i=0 ; i < conf->raid_disks; i++) {
1073 bio = r1_bio->bios[i];
1074 if (bio->bi_end_io) {
1075 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1076 if (bio_add_page(bio, page, len, 0) == 0) {
1078 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1081 bio = r1_bio->bios[i];
1082 if (bio->bi_end_io==NULL) continue;
1083 /* remove last page from this bio */
1085 bio->bi_size -= len;
1086 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1092 nr_sectors += len>>9;
1093 sector_nr += len>>9;
1094 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1096 bio = r1_bio->bios[disk];
1097 r1_bio->sectors = nr_sectors;
1099 md_sync_acct(mirror->rdev, nr_sectors);
1101 generic_make_request(bio);
1106 static int run(mddev_t *mddev)
1110 mirror_info_t *disk;
1112 struct list_head *tmp;
1114 if (mddev->level != 1) {
1115 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1116 mdname(mddev), mddev->level);
1120 * copy the already verified devices into our private RAID1
1121 * bookkeeping area. [whatever we allocate in run(),
1122 * should be freed in stop()]
1124 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1125 mddev->private = conf;
1127 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1131 memset(conf, 0, sizeof(*conf));
1132 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1134 if (!conf->mirrors) {
1135 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1139 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1141 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1142 r1bio_pool_free, mddev);
1143 if (!conf->r1bio_pool) {
1144 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1148 mddev->queue->unplug_fn = raid1_unplug;
1151 ITERATE_RDEV(mddev, rdev, tmp) {
1152 disk_idx = rdev->raid_disk;
1153 if (disk_idx >= mddev->raid_disks
1156 disk = conf->mirrors + disk_idx;
1160 blk_queue_stack_limits(mddev->queue,
1161 rdev->bdev->bd_disk->queue);
1162 /* as we don't honour merge_bvec_fn, we must never risk
1163 * violating it, so limit ->max_sector to one PAGE, as
1164 * a one page request is never in violation.
1166 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1167 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1168 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1170 disk->head_position = 0;
1171 if (!rdev->faulty && rdev->in_sync)
1172 conf->working_disks++;
1174 conf->raid_disks = mddev->raid_disks;
1175 conf->mddev = mddev;
1176 conf->device_lock = SPIN_LOCK_UNLOCKED;
1177 if (conf->working_disks == 1)
1178 mddev->recovery_cp = MaxSector;
1180 conf->resync_lock = SPIN_LOCK_UNLOCKED;
1181 init_waitqueue_head(&conf->wait_idle);
1182 init_waitqueue_head(&conf->wait_resume);
1184 if (!conf->working_disks) {
1185 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1190 mddev->degraded = 0;
1191 for (i = 0; i < conf->raid_disks; i++) {
1193 disk = conf->mirrors + i;
1196 disk->head_position = 0;
1202 * find the first working one and use it as a starting point
1203 * to read balancing.
1205 for (j = 0; j < conf->raid_disks &&
1206 (!conf->mirrors[j].rdev ||
1207 !conf->mirrors[j].rdev->in_sync) ; j++)
1209 conf->last_used = j;
1214 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1215 if (!mddev->thread) {
1217 "raid1: couldn't allocate thread for %s\n",
1223 "raid1: raid set %s active with %d out of %d mirrors\n",
1224 mdname(mddev), mddev->raid_disks - mddev->degraded,
1227 * Ok, everything is just fine now
1229 mddev->array_size = mddev->size;
1234 if (conf->r1bio_pool)
1235 mempool_destroy(conf->r1bio_pool);
1237 kfree(conf->mirrors);
1239 mddev->private = NULL;
1244 static int stop(mddev_t *mddev)
1246 conf_t *conf = mddev_to_conf(mddev);
1248 md_unregister_thread(mddev->thread);
1249 mddev->thread = NULL;
1250 if (conf->r1bio_pool)
1251 mempool_destroy(conf->r1bio_pool);
1253 kfree(conf->mirrors);
1255 mddev->private = NULL;
1259 static mdk_personality_t raid1_personality =
1262 .owner = THIS_MODULE,
1263 .make_request = make_request,
1267 .error_handler = error,
1268 .hot_add_disk = raid1_add_disk,
1269 .hot_remove_disk= raid1_remove_disk,
1270 .spare_active = raid1_spare_active,
1271 .sync_request = sync_request,
1274 static int __init raid_init(void)
1276 return register_md_personality(RAID1, &raid1_personality);
1279 static void raid_exit(void)
1281 unregister_md_personality(RAID1);
1284 module_init(raid_init);
1285 module_exit(raid_exit);
1286 MODULE_LICENSE("GPL");
1287 MODULE_ALIAS("md-personality-3"); /* RAID1 */