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 struct pool_info *pi = 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*)*pi->raid_disks,
52 memset(r1_bio, 0, sizeof(*r1_bio) +
53 sizeof(struct bio*) * pi->raid_disks);
55 unplug_slaves(pi->mddev);
60 static void r1bio_pool_free(void *r1_bio, void *data)
65 #define RESYNC_BLOCK_SIZE (64*1024)
66 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
67 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
68 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
69 #define RESYNC_WINDOW (2048*1024)
71 static void * r1buf_pool_alloc(int gfp_flags, void *data)
73 struct pool_info *pi = data;
79 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
81 unplug_slaves(pi->mddev);
86 * Allocate bios : 1 for reading, n-1 for writing
88 for (j = pi->raid_disks ; j-- ; ) {
89 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
92 r1_bio->bios[j] = bio;
95 * Allocate RESYNC_PAGES data pages and attach them to
98 bio = r1_bio->bios[0];
99 for (i = 0; i < RESYNC_PAGES; i++) {
100 page = alloc_page(gfp_flags);
104 bio->bi_io_vec[i].bv_page = page;
107 r1_bio->master_bio = bio;
113 __free_page(bio->bi_io_vec[i-1].bv_page);
115 while ( ++j < pi->raid_disks )
116 bio_put(r1_bio->bios[j]);
117 r1bio_pool_free(r1_bio, data);
121 static void r1buf_pool_free(void *__r1_bio, void *data)
123 struct pool_info *pi = data;
125 r1bio_t *r1bio = __r1_bio;
126 struct bio *bio = r1bio->bios[0];
128 for (i = 0; i < RESYNC_PAGES; i++) {
129 __free_page(bio->bi_io_vec[i].bv_page);
130 bio->bi_io_vec[i].bv_page = NULL;
132 for (i=0 ; i < pi->raid_disks; i++)
133 bio_put(r1bio->bios[i]);
135 r1bio_pool_free(r1bio, data);
138 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
142 for (i = 0; i < conf->raid_disks; i++) {
143 struct bio **bio = r1_bio->bios + i;
150 static inline void free_r1bio(r1bio_t *r1_bio)
154 conf_t *conf = mddev_to_conf(r1_bio->mddev);
157 * Wake up any possible resync thread that waits for the device
160 spin_lock_irqsave(&conf->resync_lock, flags);
161 if (!--conf->nr_pending) {
162 wake_up(&conf->wait_idle);
163 wake_up(&conf->wait_resume);
165 spin_unlock_irqrestore(&conf->resync_lock, flags);
167 put_all_bios(conf, r1_bio);
168 mempool_free(r1_bio, conf->r1bio_pool);
171 static inline void put_buf(r1bio_t *r1_bio)
173 conf_t *conf = mddev_to_conf(r1_bio->mddev);
176 mempool_free(r1_bio, conf->r1buf_pool);
178 spin_lock_irqsave(&conf->resync_lock, flags);
182 wake_up(&conf->wait_resume);
183 wake_up(&conf->wait_idle);
185 if (!--conf->nr_pending) {
186 wake_up(&conf->wait_idle);
187 wake_up(&conf->wait_resume);
189 spin_unlock_irqrestore(&conf->resync_lock, flags);
192 static int map(mddev_t *mddev, mdk_rdev_t **rdevp)
194 conf_t *conf = mddev_to_conf(mddev);
195 int i, disks = conf->raid_disks;
198 * Later we do read balancing on the read side
199 * now we use the first available disk.
202 spin_lock_irq(&conf->device_lock);
203 for (i = 0; i < disks; i++) {
204 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
205 if (rdev && rdev->in_sync) {
207 atomic_inc(&rdev->nr_pending);
208 spin_unlock_irq(&conf->device_lock);
212 spin_unlock_irq(&conf->device_lock);
214 printk(KERN_ERR "raid1_map(): huh, no more operational devices?\n");
218 static void reschedule_retry(r1bio_t *r1_bio)
221 mddev_t *mddev = r1_bio->mddev;
223 spin_lock_irqsave(&retry_list_lock, flags);
224 list_add(&r1_bio->retry_list, &retry_list_head);
225 spin_unlock_irqrestore(&retry_list_lock, flags);
227 md_wakeup_thread(mddev->thread);
231 * raid_end_bio_io() is called when we have finished servicing a mirrored
232 * operation and are ready to return a success/failure code to the buffer
235 static void raid_end_bio_io(r1bio_t *r1_bio)
237 struct bio *bio = r1_bio->master_bio;
239 bio_endio(bio, bio->bi_size,
240 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
245 * Update disk head position estimator based on IRQ completion info.
247 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
249 conf_t *conf = mddev_to_conf(r1_bio->mddev);
251 conf->mirrors[disk].head_position =
252 r1_bio->sector + (r1_bio->sectors);
255 static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
257 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
258 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
260 conf_t *conf = mddev_to_conf(r1_bio->mddev);
265 mirror = r1_bio->read_disk;
267 * this branch is our 'one mirror IO has finished' event handler:
270 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
273 * Set R1BIO_Uptodate in our master bio, so that
274 * we will return a good error code for to the higher
275 * levels even if IO on some other mirrored buffer fails.
277 * The 'master' represents the composite IO operation to
278 * user-side. So if something waits for IO, then it will
279 * wait for the 'master' bio.
281 set_bit(R1BIO_Uptodate, &r1_bio->state);
283 update_head_pos(mirror, r1_bio);
286 * we have only one bio on the read side
289 raid_end_bio_io(r1_bio);
294 char b[BDEVNAME_SIZE];
295 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
296 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
297 reschedule_retry(r1_bio);
300 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
304 static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
306 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
307 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
309 conf_t *conf = mddev_to_conf(r1_bio->mddev);
314 for (mirror = 0; mirror < conf->raid_disks; mirror++)
315 if (r1_bio->bios[mirror] == bio)
319 * this branch is our 'one mirror IO has finished' event handler:
322 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
325 * Set R1BIO_Uptodate in our master bio, so that
326 * we will return a good error code for to the higher
327 * levels even if IO on some other mirrored buffer fails.
329 * The 'master' represents the composite IO operation to
330 * user-side. So if something waits for IO, then it will
331 * wait for the 'master' bio.
333 set_bit(R1BIO_Uptodate, &r1_bio->state);
335 update_head_pos(mirror, r1_bio);
339 * Let's see if all mirrored write operations have finished
342 if (atomic_dec_and_test(&r1_bio->remaining)) {
343 md_write_end(r1_bio->mddev);
344 raid_end_bio_io(r1_bio);
347 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
353 * This routine returns the disk from which the requested read should
354 * be done. There is a per-array 'next expected sequential IO' sector
355 * number - if this matches on the next IO then we use the last disk.
356 * There is also a per-disk 'last know head position' sector that is
357 * maintained from IRQ contexts, both the normal and the resync IO
358 * completion handlers update this position correctly. If there is no
359 * perfect sequential match then we pick the disk whose head is closest.
361 * If there are 2 mirrors in the same 2 devices, performance degrades
362 * because position is mirror, not device based.
364 * The rdev for the device selected will have nr_pending incremented.
366 static int read_balance(conf_t *conf, struct bio *bio, r1bio_t *r1_bio)
368 const unsigned long this_sector = r1_bio->sector;
369 int new_disk = conf->last_used, disk = new_disk;
370 const int sectors = bio->bi_size >> 9;
371 sector_t new_distance, current_distance;
373 spin_lock_irq(&conf->device_lock);
375 * Check if it if we can balance. We can balance on the whole
376 * device if no resync is going on, or below the resync window.
377 * We take the first readable disk when above the resync window.
379 if (conf->mddev->recovery_cp < MaxSector &&
380 (this_sector + sectors >= conf->next_resync)) {
381 /* make sure that disk is operational */
384 while (!conf->mirrors[new_disk].rdev ||
385 !conf->mirrors[new_disk].rdev->in_sync) {
387 if (new_disk == conf->raid_disks) {
396 /* make sure the disk is operational */
397 while (!conf->mirrors[new_disk].rdev ||
398 !conf->mirrors[new_disk].rdev->in_sync) {
400 new_disk = conf->raid_disks;
402 if (new_disk == disk) {
403 new_disk = conf->last_used;
408 /* now disk == new_disk == starting point for search */
411 * Don't change to another disk for sequential reads:
413 if (conf->next_seq_sect == this_sector)
415 if (this_sector == conf->mirrors[new_disk].head_position)
418 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
420 /* Find the disk whose head is closest */
424 disk = conf->raid_disks;
427 if (!conf->mirrors[disk].rdev ||
428 !conf->mirrors[disk].rdev->in_sync)
431 if (!atomic_read(&conf->mirrors[disk].rdev->nr_pending)) {
435 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
436 if (new_distance < current_distance) {
437 current_distance = new_distance;
440 } while (disk != conf->last_used);
443 r1_bio->read_disk = new_disk;
444 conf->next_seq_sect = this_sector + sectors;
446 conf->last_used = new_disk;
448 if (conf->mirrors[new_disk].rdev)
449 atomic_inc(&conf->mirrors[new_disk].rdev->nr_pending);
450 spin_unlock_irq(&conf->device_lock);
455 static void unplug_slaves(mddev_t *mddev)
457 conf_t *conf = mddev_to_conf(mddev);
461 spin_lock_irqsave(&conf->device_lock, flags);
462 for (i=0; i<mddev->raid_disks; i++) {
463 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
464 if (rdev && atomic_read(&rdev->nr_pending)) {
465 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
467 atomic_inc(&rdev->nr_pending);
468 spin_unlock_irqrestore(&conf->device_lock, flags);
470 if (r_queue->unplug_fn)
471 r_queue->unplug_fn(r_queue);
473 spin_lock_irqsave(&conf->device_lock, flags);
474 atomic_dec(&rdev->nr_pending);
477 spin_unlock_irqrestore(&conf->device_lock, flags);
479 static void raid1_unplug(request_queue_t *q)
481 unplug_slaves(q->queuedata);
485 * Throttle resync depth, so that we can both get proper overlapping of
486 * requests, but are still able to handle normal requests quickly.
488 #define RESYNC_DEPTH 32
490 static void device_barrier(conf_t *conf, sector_t sect)
492 spin_lock_irq(&conf->resync_lock);
493 wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
494 conf->resync_lock, unplug_slaves(conf->mddev));
496 if (!conf->barrier++) {
497 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
498 conf->resync_lock, unplug_slaves(conf->mddev));
499 if (conf->nr_pending)
502 wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
503 conf->resync_lock, unplug_slaves(conf->mddev));
504 conf->next_resync = sect;
505 spin_unlock_irq(&conf->resync_lock);
508 static int make_request(request_queue_t *q, struct bio * bio)
510 mddev_t *mddev = q->queuedata;
511 conf_t *conf = mddev_to_conf(mddev);
512 mirror_info_t *mirror;
514 struct bio *read_bio;
518 * Register the new request and wait if the reconstruction
519 * thread has put up a bar for new requests.
520 * Continue immediately if no resync is active currently.
522 spin_lock_irq(&conf->resync_lock);
523 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
525 spin_unlock_irq(&conf->resync_lock);
527 if (bio_data_dir(bio)==WRITE) {
528 disk_stat_inc(mddev->gendisk, writes);
529 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
531 disk_stat_inc(mddev->gendisk, reads);
532 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
536 * make_request() can abort the operation when READA is being
537 * used and no empty request is available.
540 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
542 r1_bio->master_bio = bio;
543 r1_bio->sectors = bio->bi_size >> 9;
545 r1_bio->mddev = mddev;
546 r1_bio->sector = bio->bi_sector;
548 if (bio_data_dir(bio) == READ) {
550 * read balancing logic:
552 mirror = conf->mirrors + read_balance(conf, bio, r1_bio);
554 read_bio = bio_clone(bio, GFP_NOIO);
556 r1_bio->bios[r1_bio->read_disk] = read_bio;
558 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
559 read_bio->bi_bdev = mirror->rdev->bdev;
560 read_bio->bi_end_io = raid1_end_read_request;
561 read_bio->bi_rw = READ;
562 read_bio->bi_private = r1_bio;
564 generic_make_request(read_bio);
571 /* first select target devices under spinlock and
572 * inc refcount on their rdev. Record them by setting
575 disks = conf->raid_disks;
576 spin_lock_irq(&conf->device_lock);
577 for (i = 0; i < disks; i++) {
578 if (conf->mirrors[i].rdev &&
579 !conf->mirrors[i].rdev->faulty) {
580 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
581 r1_bio->bios[i] = bio;
583 r1_bio->bios[i] = NULL;
585 spin_unlock_irq(&conf->device_lock);
587 atomic_set(&r1_bio->remaining, 1);
588 md_write_start(mddev);
589 for (i = 0; i < disks; i++) {
591 if (!r1_bio->bios[i])
594 mbio = bio_clone(bio, GFP_NOIO);
595 r1_bio->bios[i] = mbio;
597 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
598 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
599 mbio->bi_end_io = raid1_end_write_request;
601 mbio->bi_private = r1_bio;
603 atomic_inc(&r1_bio->remaining);
604 generic_make_request(mbio);
607 if (atomic_dec_and_test(&r1_bio->remaining)) {
609 raid_end_bio_io(r1_bio);
615 static void status(struct seq_file *seq, mddev_t *mddev)
617 conf_t *conf = mddev_to_conf(mddev);
620 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
621 conf->working_disks);
622 for (i = 0; i < conf->raid_disks; i++)
623 seq_printf(seq, "%s",
624 conf->mirrors[i].rdev &&
625 conf->mirrors[i].rdev->in_sync ? "U" : "_");
626 seq_printf(seq, "]");
630 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
632 char b[BDEVNAME_SIZE];
633 conf_t *conf = mddev_to_conf(mddev);
636 * If it is not operational, then we have already marked it as dead
637 * else if it is the last working disks, ignore the error, let the
638 * next level up know.
639 * else mark the drive as failed
642 && conf->working_disks == 1)
644 * Don't fail the drive, act as though we were just a
645 * normal single drive
650 conf->working_disks--;
652 * if recovery is running, make sure it aborts.
654 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
659 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
660 " Operation continuing on %d devices\n",
661 bdevname(rdev->bdev,b), conf->working_disks);
664 static void print_conf(conf_t *conf)
669 printk("RAID1 conf printout:\n");
674 printk(" --- wd:%d rd:%d\n", conf->working_disks,
677 for (i = 0; i < conf->raid_disks; i++) {
678 char b[BDEVNAME_SIZE];
679 tmp = conf->mirrors + i;
681 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
682 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
683 bdevname(tmp->rdev->bdev,b));
687 static void close_sync(conf_t *conf)
689 spin_lock_irq(&conf->resync_lock);
690 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
691 conf->resync_lock, unplug_slaves(conf->mddev));
692 spin_unlock_irq(&conf->resync_lock);
694 if (conf->barrier) BUG();
695 if (waitqueue_active(&conf->wait_idle)) BUG();
697 mempool_destroy(conf->r1buf_pool);
698 conf->r1buf_pool = NULL;
701 static int raid1_spare_active(mddev_t *mddev)
704 conf_t *conf = mddev->private;
707 spin_lock_irq(&conf->device_lock);
709 * Find all failed disks within the RAID1 configuration
710 * and mark them readable
712 for (i = 0; i < conf->raid_disks; i++) {
713 tmp = conf->mirrors + i;
715 && !tmp->rdev->faulty
716 && !tmp->rdev->in_sync) {
717 conf->working_disks++;
719 tmp->rdev->in_sync = 1;
722 spin_unlock_irq(&conf->device_lock);
729 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
731 conf_t *conf = mddev->private;
736 spin_lock_irq(&conf->device_lock);
737 for (mirror=0; mirror < mddev->raid_disks; mirror++)
738 if ( !(p=conf->mirrors+mirror)->rdev) {
741 blk_queue_stack_limits(mddev->queue,
742 rdev->bdev->bd_disk->queue);
743 /* as we don't honour merge_bvec_fn, we must never risk
744 * violating it, so limit ->max_sector to one PAGE, as
745 * a one page request is never in violation.
747 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
748 mddev->queue->max_sectors > (PAGE_SIZE>>9))
749 mddev->queue->max_sectors = (PAGE_SIZE>>9);
751 p->head_position = 0;
752 rdev->raid_disk = mirror;
756 spin_unlock_irq(&conf->device_lock);
762 static int raid1_remove_disk(mddev_t *mddev, int number)
764 conf_t *conf = mddev->private;
766 mirror_info_t *p = conf->mirrors+ number;
769 spin_lock_irq(&conf->device_lock);
771 if (p->rdev->in_sync ||
772 atomic_read(&p->rdev->nr_pending)) {
782 spin_unlock_irq(&conf->device_lock);
789 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
791 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
792 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
793 conf_t *conf = mddev_to_conf(r1_bio->mddev);
798 if (r1_bio->bios[r1_bio->read_disk] != bio)
800 update_head_pos(r1_bio->read_disk, r1_bio);
802 * we have read a block, now it needs to be re-written,
803 * or re-read if the read failed.
804 * We don't do much here, just schedule handling by raid1d
807 md_error(r1_bio->mddev,
808 conf->mirrors[r1_bio->read_disk].rdev);
810 set_bit(R1BIO_Uptodate, &r1_bio->state);
811 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
812 reschedule_retry(r1_bio);
816 static int end_sync_write(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 mddev_t *mddev = r1_bio->mddev;
821 conf_t *conf = mddev_to_conf(mddev);
828 for (i = 0; i < conf->raid_disks; i++)
829 if (r1_bio->bios[i] == bio) {
834 md_error(mddev, conf->mirrors[mirror].rdev);
835 update_head_pos(mirror, r1_bio);
837 if (atomic_dec_and_test(&r1_bio->remaining)) {
838 md_done_sync(mddev, r1_bio->sectors, uptodate);
841 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
845 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
847 conf_t *conf = mddev_to_conf(mddev);
849 int disks = conf->raid_disks;
850 struct bio *bio, *wbio;
852 bio = r1_bio->bios[r1_bio->read_disk];
857 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
859 * There is no point trying a read-for-reconstruct as
860 * reconstruct is about to be aborted
862 char b[BDEVNAME_SIZE];
863 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
865 bdevname(bio->bi_bdev,b),
866 (unsigned long long)r1_bio->sector);
867 md_done_sync(mddev, r1_bio->sectors, 0);
872 atomic_set(&r1_bio->remaining, 1);
873 for (i = 0; i < disks ; i++) {
874 wbio = r1_bio->bios[i];
875 if (wbio->bi_end_io != end_sync_write)
878 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
879 atomic_inc(&r1_bio->remaining);
880 md_sync_acct(conf->mirrors[i].rdev, wbio->bi_size >> 9);
881 generic_make_request(wbio);
884 if (atomic_dec_and_test(&r1_bio->remaining)) {
885 md_done_sync(mddev, r1_bio->sectors, 1);
891 * This is a kernel thread which:
893 * 1. Retries failed read operations on working mirrors.
894 * 2. Updates the raid superblock when problems encounter.
895 * 3. Performs writes following reads for array syncronising.
898 static void raid1d(mddev_t *mddev)
900 struct list_head *head = &retry_list_head;
904 conf_t *conf = mddev_to_conf(mddev);
908 md_check_recovery(mddev);
909 md_handle_safemode(mddev);
912 char b[BDEVNAME_SIZE];
913 spin_lock_irqsave(&retry_list_lock, flags);
914 if (list_empty(head))
916 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
917 list_del(head->prev);
918 spin_unlock_irqrestore(&retry_list_lock, flags);
920 mddev = r1_bio->mddev;
921 conf = mddev_to_conf(mddev);
922 bio = r1_bio->master_bio;
923 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
924 sync_request_write(mddev, r1_bio);
927 if (map(mddev, &rdev) == -1) {
928 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
929 " read error for block %llu\n",
930 bdevname(bio->bi_bdev,b),
931 (unsigned long long)r1_bio->sector);
932 raid_end_bio_io(r1_bio);
934 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
936 bdevname(rdev->bdev,b),
937 (unsigned long long)r1_bio->sector);
938 bio->bi_bdev = rdev->bdev;
939 bio->bi_sector = r1_bio->sector + rdev->data_offset;
942 generic_make_request(bio);
946 spin_unlock_irqrestore(&retry_list_lock, flags);
948 unplug_slaves(mddev);
952 static int init_resync(conf_t *conf)
956 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
957 if (conf->r1buf_pool)
959 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
961 if (!conf->r1buf_pool)
963 conf->next_resync = 0;
968 * perform a "sync" on one "block"
970 * We need to make sure that no normal I/O request - particularly write
971 * requests - conflict with active sync requests.
973 * This is achieved by tracking pending requests and a 'barrier' concept
974 * that can be installed to exclude normal IO requests.
977 static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster)
979 conf_t *conf = mddev_to_conf(mddev);
980 mirror_info_t *mirror;
983 sector_t max_sector, nr_sectors;
986 int write_targets = 0;
988 if (!conf->r1buf_pool)
989 if (init_resync(conf))
992 max_sector = mddev->size << 1;
993 if (sector_nr >= max_sector) {
999 * If there is non-resync activity waiting for us then
1000 * put in a delay to throttle resync.
1002 if (!go_faster && waitqueue_active(&conf->wait_resume))
1003 schedule_timeout(HZ);
1004 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1007 * If reconstructing, and >1 working disc,
1008 * could dedicate one to rebuild and others to
1009 * service read requests ..
1011 disk = conf->last_used;
1012 /* make sure disk is operational */
1013 spin_lock_irq(&conf->device_lock);
1014 while (conf->mirrors[disk].rdev == NULL ||
1015 !conf->mirrors[disk].rdev->in_sync) {
1017 disk = conf->raid_disks;
1019 if (disk == conf->last_used)
1022 conf->last_used = disk;
1023 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1024 spin_unlock_irq(&conf->device_lock);
1026 mirror = conf->mirrors + disk;
1028 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1030 spin_lock_irq(&conf->resync_lock);
1032 spin_unlock_irq(&conf->resync_lock);
1034 r1_bio->mddev = mddev;
1035 r1_bio->sector = sector_nr;
1036 set_bit(R1BIO_IsSync, &r1_bio->state);
1037 r1_bio->read_disk = disk;
1039 for (i=0; i < conf->raid_disks; i++) {
1040 bio = r1_bio->bios[i];
1042 /* take from bio_init */
1043 bio->bi_next = NULL;
1044 bio->bi_flags |= 1 << BIO_UPTODATE;
1048 bio->bi_phys_segments = 0;
1049 bio->bi_hw_segments = 0;
1051 bio->bi_end_io = NULL;
1052 bio->bi_private = NULL;
1056 bio->bi_end_io = end_sync_read;
1057 } else if (conf->mirrors[i].rdev &&
1058 !conf->mirrors[i].rdev->faulty &&
1059 (!conf->mirrors[i].rdev->in_sync ||
1060 sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1062 bio->bi_end_io = end_sync_write;
1066 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1067 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1068 bio->bi_private = r1_bio;
1070 if (write_targets == 0) {
1071 /* There is nowhere to write, so all non-sync
1072 * drives must be failed - so we are finished
1074 int rv = max_sector - sector_nr;
1075 md_done_sync(mddev, rv, 1);
1077 atomic_dec(&conf->mirrors[disk].rdev->nr_pending);
1084 int len = PAGE_SIZE;
1085 if (sector_nr + (len>>9) > max_sector)
1086 len = (max_sector - sector_nr) << 9;
1089 for (i=0 ; i < conf->raid_disks; i++) {
1090 bio = r1_bio->bios[i];
1091 if (bio->bi_end_io) {
1092 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1093 if (bio_add_page(bio, page, len, 0) == 0) {
1095 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1098 bio = r1_bio->bios[i];
1099 if (bio->bi_end_io==NULL) continue;
1100 /* remove last page from this bio */
1102 bio->bi_size -= len;
1103 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1109 nr_sectors += len>>9;
1110 sector_nr += len>>9;
1111 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1113 bio = r1_bio->bios[disk];
1114 r1_bio->sectors = nr_sectors;
1116 md_sync_acct(mirror->rdev, nr_sectors);
1118 generic_make_request(bio);
1123 static int run(mddev_t *mddev)
1127 mirror_info_t *disk;
1129 struct list_head *tmp;
1131 if (mddev->level != 1) {
1132 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1133 mdname(mddev), mddev->level);
1137 * copy the already verified devices into our private RAID1
1138 * bookkeeping area. [whatever we allocate in run(),
1139 * should be freed in stop()]
1141 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1142 mddev->private = conf;
1146 memset(conf, 0, sizeof(*conf));
1147 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1152 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1154 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1155 if (!conf->poolinfo)
1157 conf->poolinfo->mddev = mddev;
1158 conf->poolinfo->raid_disks = mddev->raid_disks;
1159 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1162 if (!conf->r1bio_pool)
1165 mddev->queue->unplug_fn = raid1_unplug;
1168 ITERATE_RDEV(mddev, rdev, tmp) {
1169 disk_idx = rdev->raid_disk;
1170 if (disk_idx >= mddev->raid_disks
1173 disk = conf->mirrors + disk_idx;
1177 blk_queue_stack_limits(mddev->queue,
1178 rdev->bdev->bd_disk->queue);
1179 /* as we don't honour merge_bvec_fn, we must never risk
1180 * violating it, so limit ->max_sector to one PAGE, as
1181 * a one page request is never in violation.
1183 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1184 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1185 mddev->queue->max_sectors = (PAGE_SIZE>>9);
1187 disk->head_position = 0;
1188 if (!rdev->faulty && rdev->in_sync)
1189 conf->working_disks++;
1191 conf->raid_disks = mddev->raid_disks;
1192 conf->mddev = mddev;
1193 conf->device_lock = SPIN_LOCK_UNLOCKED;
1194 if (conf->working_disks == 1)
1195 mddev->recovery_cp = MaxSector;
1197 conf->resync_lock = SPIN_LOCK_UNLOCKED;
1198 init_waitqueue_head(&conf->wait_idle);
1199 init_waitqueue_head(&conf->wait_resume);
1201 if (!conf->working_disks) {
1202 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1207 mddev->degraded = 0;
1208 for (i = 0; i < conf->raid_disks; i++) {
1210 disk = conf->mirrors + i;
1213 disk->head_position = 0;
1219 * find the first working one and use it as a starting point
1220 * to read balancing.
1222 for (j = 0; j < conf->raid_disks &&
1223 (!conf->mirrors[j].rdev ||
1224 !conf->mirrors[j].rdev->in_sync) ; j++)
1226 conf->last_used = j;
1231 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1232 if (!mddev->thread) {
1234 "raid1: couldn't allocate thread for %s\n",
1240 "raid1: raid set %s active with %d out of %d mirrors\n",
1241 mdname(mddev), mddev->raid_disks - mddev->degraded,
1244 * Ok, everything is just fine now
1246 mddev->array_size = mddev->size;
1251 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1256 if (conf->r1bio_pool)
1257 mempool_destroy(conf->r1bio_pool);
1259 kfree(conf->mirrors);
1261 kfree(conf->poolinfo);
1263 mddev->private = NULL;
1269 static int stop(mddev_t *mddev)
1271 conf_t *conf = mddev_to_conf(mddev);
1273 md_unregister_thread(mddev->thread);
1274 mddev->thread = NULL;
1275 if (conf->r1bio_pool)
1276 mempool_destroy(conf->r1bio_pool);
1278 kfree(conf->mirrors);
1280 kfree(conf->poolinfo);
1282 mddev->private = NULL;
1286 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1288 /* no resync is happening, and there is enough space
1289 * on all devices, so we can resize.
1290 * We need to make sure resync covers any new space.
1291 * If the array is shrinking we should possibly wait until
1292 * any io in the removed space completes, but it hardly seems
1295 mddev->array_size = sectors>>1;
1296 set_capacity(mddev->gendisk, mddev->array_size << 1);
1298 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1299 mddev->recovery_cp = mddev->size << 1;
1300 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1302 mddev->size = mddev->array_size;
1306 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1309 * 1/ resize the r1bio_pool
1310 * 2/ resize conf->mirrors
1312 * We allocate a new r1bio_pool if we can.
1313 * Then raise a device barrier and wait until all IO stops.
1314 * Then resize conf->mirrors and swap in the new r1bio pool.
1316 mempool_t *newpool, *oldpool;
1317 struct pool_info *newpoolinfo;
1318 mirror_info_t *newmirrors;
1319 conf_t *conf = mddev_to_conf(mddev);
1323 for (d= raid_disks; d < conf->raid_disks; d++)
1324 if (conf->mirrors[d].rdev)
1327 newpoolinfo = kmalloc(sizeof(newpoolinfo), GFP_KERNEL);
1330 newpoolinfo->mddev = mddev;
1331 newpoolinfo->raid_disks = raid_disks;
1333 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1334 r1bio_pool_free, newpoolinfo);
1339 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1342 mempool_destroy(newpool);
1345 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1347 spin_lock_irq(&conf->resync_lock);
1349 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
1350 conf->resync_lock, unplug_slaves(mddev));
1351 spin_unlock_irq(&conf->resync_lock);
1353 /* ok, everything is stopped */
1354 oldpool = conf->r1bio_pool;
1355 conf->r1bio_pool = newpool;
1356 for (d=0; d < raid_disks && d < conf->raid_disks; d++)
1357 newmirrors[d] = conf->mirrors[d];
1358 kfree(conf->mirrors);
1359 conf->mirrors = newmirrors;
1360 kfree(conf->poolinfo);
1361 conf->poolinfo = newpoolinfo;
1363 mddev->degraded += (raid_disks - conf->raid_disks);
1364 conf->raid_disks = mddev->raid_disks = raid_disks;
1366 spin_lock_irq(&conf->resync_lock);
1368 spin_unlock_irq(&conf->resync_lock);
1369 wake_up(&conf->wait_resume);
1370 wake_up(&conf->wait_idle);
1373 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1374 md_wakeup_thread(mddev->thread);
1376 mempool_destroy(oldpool);
1381 static mdk_personality_t raid1_personality =
1384 .owner = THIS_MODULE,
1385 .make_request = make_request,
1389 .error_handler = error,
1390 .hot_add_disk = raid1_add_disk,
1391 .hot_remove_disk= raid1_remove_disk,
1392 .spare_active = raid1_spare_active,
1393 .sync_request = sync_request,
1394 .resize = raid1_resize,
1395 .reshape = raid1_reshape,
1398 static int __init raid_init(void)
1400 return register_md_personality(RAID1, &raid1_personality);
1403 static void raid_exit(void)
1405 unregister_md_personality(RAID1);
1408 module_init(raid_init);
1409 module_exit(raid_exit);
1410 MODULE_LICENSE("GPL");
1411 MODULE_ALIAS("md-personality-3"); /* RAID1 */