[PATCH] kexec: x86: rename APIC_MODE_EXINT
[linux-2.6/libata-dev.git] / drivers / md / raid1.c
blobff1dbec864af51d780e1135ae392c1f6962ae37c
1 /*
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 * Changes by Peter T. Breuer <ptb@it.uc3m.es> 31/1/2003 to support
16 * bitmapped intelligence in resync:
18 * - bitmap marked during normal i/o
19 * - bitmap used to skip nondirty blocks during sync
21 * Additions to bitmap code, (C) 2003-2004 Paul Clements, SteelEye Technology:
22 * - persistent bitmap code
24 * This program is free software; you can redistribute it and/or modify
25 * it under the terms of the GNU General Public License as published by
26 * the Free Software Foundation; either version 2, or (at your option)
27 * any later version.
29 * You should have received a copy of the GNU General Public License
30 * (for example /usr/src/linux/COPYING); if not, write to the Free
31 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 #include "dm-bio-list.h"
35 #include <linux/raid/raid1.h>
36 #include <linux/raid/bitmap.h>
38 #define DEBUG 0
39 #if DEBUG
40 #define PRINTK(x...) printk(x)
41 #else
42 #define PRINTK(x...)
43 #endif
46 * Number of guaranteed r1bios in case of extreme VM load:
48 #define NR_RAID1_BIOS 256
50 static mdk_personality_t raid1_personality;
52 static void unplug_slaves(mddev_t *mddev);
55 static void * r1bio_pool_alloc(unsigned int __nocast gfp_flags, void *data)
57 struct pool_info *pi = data;
58 r1bio_t *r1_bio;
59 int size = offsetof(r1bio_t, bios[pi->raid_disks]);
61 /* allocate a r1bio with room for raid_disks entries in the bios array */
62 r1_bio = kmalloc(size, gfp_flags);
63 if (r1_bio)
64 memset(r1_bio, 0, size);
65 else
66 unplug_slaves(pi->mddev);
68 return r1_bio;
71 static void r1bio_pool_free(void *r1_bio, void *data)
73 kfree(r1_bio);
76 #define RESYNC_BLOCK_SIZE (64*1024)
77 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
78 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
79 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
80 #define RESYNC_WINDOW (2048*1024)
82 static void * r1buf_pool_alloc(unsigned int __nocast gfp_flags, void *data)
84 struct pool_info *pi = data;
85 struct page *page;
86 r1bio_t *r1_bio;
87 struct bio *bio;
88 int i, j;
90 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
91 if (!r1_bio) {
92 unplug_slaves(pi->mddev);
93 return NULL;
97 * Allocate bios : 1 for reading, n-1 for writing
99 for (j = pi->raid_disks ; j-- ; ) {
100 bio = bio_alloc(gfp_flags, RESYNC_PAGES);
101 if (!bio)
102 goto out_free_bio;
103 r1_bio->bios[j] = bio;
106 * Allocate RESYNC_PAGES data pages and attach them to
107 * the first bio;
109 bio = r1_bio->bios[0];
110 for (i = 0; i < RESYNC_PAGES; i++) {
111 page = alloc_page(gfp_flags);
112 if (unlikely(!page))
113 goto out_free_pages;
115 bio->bi_io_vec[i].bv_page = page;
118 r1_bio->master_bio = NULL;
120 return r1_bio;
122 out_free_pages:
123 for ( ; i > 0 ; i--)
124 __free_page(bio->bi_io_vec[i-1].bv_page);
125 out_free_bio:
126 while ( ++j < pi->raid_disks )
127 bio_put(r1_bio->bios[j]);
128 r1bio_pool_free(r1_bio, data);
129 return NULL;
132 static void r1buf_pool_free(void *__r1_bio, void *data)
134 struct pool_info *pi = data;
135 int i;
136 r1bio_t *r1bio = __r1_bio;
137 struct bio *bio = r1bio->bios[0];
139 for (i = 0; i < RESYNC_PAGES; i++) {
140 __free_page(bio->bi_io_vec[i].bv_page);
141 bio->bi_io_vec[i].bv_page = NULL;
143 for (i=0 ; i < pi->raid_disks; i++)
144 bio_put(r1bio->bios[i]);
146 r1bio_pool_free(r1bio, data);
149 static void put_all_bios(conf_t *conf, r1bio_t *r1_bio)
151 int i;
153 for (i = 0; i < conf->raid_disks; i++) {
154 struct bio **bio = r1_bio->bios + i;
155 if (*bio)
156 bio_put(*bio);
157 *bio = NULL;
161 static inline void free_r1bio(r1bio_t *r1_bio)
163 unsigned long flags;
165 conf_t *conf = mddev_to_conf(r1_bio->mddev);
168 * Wake up any possible resync thread that waits for the device
169 * to go idle.
171 spin_lock_irqsave(&conf->resync_lock, flags);
172 if (!--conf->nr_pending) {
173 wake_up(&conf->wait_idle);
174 wake_up(&conf->wait_resume);
176 spin_unlock_irqrestore(&conf->resync_lock, flags);
178 put_all_bios(conf, r1_bio);
179 mempool_free(r1_bio, conf->r1bio_pool);
182 static inline void put_buf(r1bio_t *r1_bio)
184 conf_t *conf = mddev_to_conf(r1_bio->mddev);
185 unsigned long flags;
187 mempool_free(r1_bio, conf->r1buf_pool);
189 spin_lock_irqsave(&conf->resync_lock, flags);
190 if (!conf->barrier)
191 BUG();
192 --conf->barrier;
193 wake_up(&conf->wait_resume);
194 wake_up(&conf->wait_idle);
196 if (!--conf->nr_pending) {
197 wake_up(&conf->wait_idle);
198 wake_up(&conf->wait_resume);
200 spin_unlock_irqrestore(&conf->resync_lock, flags);
203 static void reschedule_retry(r1bio_t *r1_bio)
205 unsigned long flags;
206 mddev_t *mddev = r1_bio->mddev;
207 conf_t *conf = mddev_to_conf(mddev);
209 spin_lock_irqsave(&conf->device_lock, flags);
210 list_add(&r1_bio->retry_list, &conf->retry_list);
211 spin_unlock_irqrestore(&conf->device_lock, flags);
213 md_wakeup_thread(mddev->thread);
217 * raid_end_bio_io() is called when we have finished servicing a mirrored
218 * operation and are ready to return a success/failure code to the buffer
219 * cache layer.
221 static void raid_end_bio_io(r1bio_t *r1_bio)
223 struct bio *bio = r1_bio->master_bio;
225 bio_endio(bio, bio->bi_size,
226 test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO);
227 free_r1bio(r1_bio);
231 * Update disk head position estimator based on IRQ completion info.
233 static inline void update_head_pos(int disk, r1bio_t *r1_bio)
235 conf_t *conf = mddev_to_conf(r1_bio->mddev);
237 conf->mirrors[disk].head_position =
238 r1_bio->sector + (r1_bio->sectors);
241 static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error)
243 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
244 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
245 int mirror;
246 conf_t *conf = mddev_to_conf(r1_bio->mddev);
248 if (bio->bi_size)
249 return 1;
251 mirror = r1_bio->read_disk;
253 * this branch is our 'one mirror IO has finished' event handler:
255 if (!uptodate)
256 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
257 else
259 * Set R1BIO_Uptodate in our master bio, so that
260 * we will return a good error code for to the higher
261 * levels even if IO on some other mirrored buffer fails.
263 * The 'master' represents the composite IO operation to
264 * user-side. So if something waits for IO, then it will
265 * wait for the 'master' bio.
267 set_bit(R1BIO_Uptodate, &r1_bio->state);
269 update_head_pos(mirror, r1_bio);
272 * we have only one bio on the read side
274 if (uptodate)
275 raid_end_bio_io(r1_bio);
276 else {
278 * oops, read error:
280 char b[BDEVNAME_SIZE];
281 if (printk_ratelimit())
282 printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n",
283 bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector);
284 reschedule_retry(r1_bio);
287 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
288 return 0;
291 static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error)
293 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
294 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
295 int mirror;
296 conf_t *conf = mddev_to_conf(r1_bio->mddev);
298 if (bio->bi_size)
299 return 1;
301 for (mirror = 0; mirror < conf->raid_disks; mirror++)
302 if (r1_bio->bios[mirror] == bio)
303 break;
306 * this branch is our 'one mirror IO has finished' event handler:
308 if (!uptodate) {
309 md_error(r1_bio->mddev, conf->mirrors[mirror].rdev);
310 /* an I/O failed, we can't clear the bitmap */
311 set_bit(R1BIO_Degraded, &r1_bio->state);
312 } else
314 * Set R1BIO_Uptodate in our master bio, so that
315 * we will return a good error code for to the higher
316 * levels even if IO on some other mirrored buffer fails.
318 * The 'master' represents the composite IO operation to
319 * user-side. So if something waits for IO, then it will
320 * wait for the 'master' bio.
322 set_bit(R1BIO_Uptodate, &r1_bio->state);
324 update_head_pos(mirror, r1_bio);
328 * Let's see if all mirrored write operations have finished
329 * already.
331 if (atomic_dec_and_test(&r1_bio->remaining)) {
332 /* clear the bitmap if all writes complete successfully */
333 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
334 r1_bio->sectors,
335 !test_bit(R1BIO_Degraded, &r1_bio->state));
336 md_write_end(r1_bio->mddev);
337 raid_end_bio_io(r1_bio);
340 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
341 return 0;
346 * This routine returns the disk from which the requested read should
347 * be done. There is a per-array 'next expected sequential IO' sector
348 * number - if this matches on the next IO then we use the last disk.
349 * There is also a per-disk 'last know head position' sector that is
350 * maintained from IRQ contexts, both the normal and the resync IO
351 * completion handlers update this position correctly. If there is no
352 * perfect sequential match then we pick the disk whose head is closest.
354 * If there are 2 mirrors in the same 2 devices, performance degrades
355 * because position is mirror, not device based.
357 * The rdev for the device selected will have nr_pending incremented.
359 static int read_balance(conf_t *conf, r1bio_t *r1_bio)
361 const unsigned long this_sector = r1_bio->sector;
362 int new_disk = conf->last_used, disk = new_disk;
363 const int sectors = r1_bio->sectors;
364 sector_t new_distance, current_distance;
365 mdk_rdev_t *new_rdev, *rdev;
367 rcu_read_lock();
369 * Check if it if we can balance. We can balance on the whole
370 * device if no resync is going on, or below the resync window.
371 * We take the first readable disk when above the resync window.
373 retry:
374 if (conf->mddev->recovery_cp < MaxSector &&
375 (this_sector + sectors >= conf->next_resync)) {
376 /* Choose the first operation device, for consistancy */
377 new_disk = 0;
379 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
380 !new_rdev->in_sync) {
381 new_disk++;
382 if (new_disk == conf->raid_disks) {
383 new_disk = -1;
384 break;
387 goto rb_out;
391 /* make sure the disk is operational */
392 while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL ||
393 !new_rdev->in_sync) {
394 if (new_disk <= 0)
395 new_disk = conf->raid_disks;
396 new_disk--;
397 if (new_disk == disk) {
398 new_disk = -1;
399 goto rb_out;
402 disk = new_disk;
403 /* now disk == new_disk == starting point for search */
406 * Don't change to another disk for sequential reads:
408 if (conf->next_seq_sect == this_sector)
409 goto rb_out;
410 if (this_sector == conf->mirrors[new_disk].head_position)
411 goto rb_out;
413 current_distance = abs(this_sector - conf->mirrors[disk].head_position);
415 /* Find the disk whose head is closest */
417 do {
418 if (disk <= 0)
419 disk = conf->raid_disks;
420 disk--;
422 if ((rdev=conf->mirrors[disk].rdev) == NULL ||
423 !rdev->in_sync)
424 continue;
426 if (!atomic_read(&rdev->nr_pending)) {
427 new_disk = disk;
428 new_rdev = rdev;
429 break;
431 new_distance = abs(this_sector - conf->mirrors[disk].head_position);
432 if (new_distance < current_distance) {
433 current_distance = new_distance;
434 new_disk = disk;
435 new_rdev = rdev;
437 } while (disk != conf->last_used);
439 rb_out:
442 if (new_disk >= 0) {
443 conf->next_seq_sect = this_sector + sectors;
444 conf->last_used = new_disk;
445 atomic_inc(&new_rdev->nr_pending);
446 if (!new_rdev->in_sync) {
447 /* cannot risk returning a device that failed
448 * before we inc'ed nr_pending
450 atomic_dec(&new_rdev->nr_pending);
451 goto retry;
454 rcu_read_unlock();
456 return new_disk;
459 static void unplug_slaves(mddev_t *mddev)
461 conf_t *conf = mddev_to_conf(mddev);
462 int i;
464 rcu_read_lock();
465 for (i=0; i<mddev->raid_disks; i++) {
466 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
467 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
468 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
470 atomic_inc(&rdev->nr_pending);
471 rcu_read_unlock();
473 if (r_queue->unplug_fn)
474 r_queue->unplug_fn(r_queue);
476 rdev_dec_pending(rdev, mddev);
477 rcu_read_lock();
480 rcu_read_unlock();
483 static void raid1_unplug(request_queue_t *q)
485 mddev_t *mddev = q->queuedata;
487 unplug_slaves(mddev);
488 md_wakeup_thread(mddev->thread);
491 static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk,
492 sector_t *error_sector)
494 mddev_t *mddev = q->queuedata;
495 conf_t *conf = mddev_to_conf(mddev);
496 int i, ret = 0;
498 rcu_read_lock();
499 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
500 mdk_rdev_t *rdev = conf->mirrors[i].rdev;
501 if (rdev && !rdev->faulty) {
502 struct block_device *bdev = rdev->bdev;
503 request_queue_t *r_queue = bdev_get_queue(bdev);
505 if (!r_queue->issue_flush_fn)
506 ret = -EOPNOTSUPP;
507 else {
508 atomic_inc(&rdev->nr_pending);
509 rcu_read_unlock();
510 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
511 error_sector);
512 rdev_dec_pending(rdev, mddev);
513 rcu_read_lock();
517 rcu_read_unlock();
518 return ret;
522 * Throttle resync depth, so that we can both get proper overlapping of
523 * requests, but are still able to handle normal requests quickly.
525 #define RESYNC_DEPTH 32
527 static void device_barrier(conf_t *conf, sector_t sect)
529 spin_lock_irq(&conf->resync_lock);
530 wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume),
531 conf->resync_lock, raid1_unplug(conf->mddev->queue));
533 if (!conf->barrier++) {
534 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
535 conf->resync_lock, raid1_unplug(conf->mddev->queue));
536 if (conf->nr_pending)
537 BUG();
539 wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH,
540 conf->resync_lock, raid1_unplug(conf->mddev->queue));
541 conf->next_resync = sect;
542 spin_unlock_irq(&conf->resync_lock);
545 static int make_request(request_queue_t *q, struct bio * bio)
547 mddev_t *mddev = q->queuedata;
548 conf_t *conf = mddev_to_conf(mddev);
549 mirror_info_t *mirror;
550 r1bio_t *r1_bio;
551 struct bio *read_bio;
552 int i, targets = 0, disks;
553 mdk_rdev_t *rdev;
554 struct bitmap *bitmap = mddev->bitmap;
555 unsigned long flags;
556 struct bio_list bl;
560 * Register the new request and wait if the reconstruction
561 * thread has put up a bar for new requests.
562 * Continue immediately if no resync is active currently.
564 md_write_start(mddev, bio); /* wait on superblock update early */
566 spin_lock_irq(&conf->resync_lock);
567 wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, );
568 conf->nr_pending++;
569 spin_unlock_irq(&conf->resync_lock);
571 if (bio_data_dir(bio)==WRITE) {
572 disk_stat_inc(mddev->gendisk, writes);
573 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio));
574 } else {
575 disk_stat_inc(mddev->gendisk, reads);
576 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio));
580 * make_request() can abort the operation when READA is being
581 * used and no empty request is available.
584 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
586 r1_bio->master_bio = bio;
587 r1_bio->sectors = bio->bi_size >> 9;
588 r1_bio->state = 0;
589 r1_bio->mddev = mddev;
590 r1_bio->sector = bio->bi_sector;
592 r1_bio->state = 0;
594 if (bio_data_dir(bio) == READ) {
596 * read balancing logic:
598 int rdisk = read_balance(conf, r1_bio);
600 if (rdisk < 0) {
601 /* couldn't find anywhere to read from */
602 raid_end_bio_io(r1_bio);
603 return 0;
605 mirror = conf->mirrors + rdisk;
607 r1_bio->read_disk = rdisk;
609 read_bio = bio_clone(bio, GFP_NOIO);
611 r1_bio->bios[rdisk] = read_bio;
613 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
614 read_bio->bi_bdev = mirror->rdev->bdev;
615 read_bio->bi_end_io = raid1_end_read_request;
616 read_bio->bi_rw = READ;
617 read_bio->bi_private = r1_bio;
619 generic_make_request(read_bio);
620 return 0;
624 * WRITE:
626 /* first select target devices under spinlock and
627 * inc refcount on their rdev. Record them by setting
628 * bios[x] to bio
630 disks = conf->raid_disks;
631 #if 0
632 { static int first=1;
633 if (first) printk("First Write sector %llu disks %d\n",
634 (unsigned long long)r1_bio->sector, disks);
635 first = 0;
637 #endif
638 rcu_read_lock();
639 for (i = 0; i < disks; i++) {
640 if ((rdev=conf->mirrors[i].rdev) != NULL &&
641 !rdev->faulty) {
642 atomic_inc(&rdev->nr_pending);
643 if (rdev->faulty) {
644 atomic_dec(&rdev->nr_pending);
645 r1_bio->bios[i] = NULL;
646 } else
647 r1_bio->bios[i] = bio;
648 targets++;
649 } else
650 r1_bio->bios[i] = NULL;
652 rcu_read_unlock();
654 if (targets < conf->raid_disks) {
655 /* array is degraded, we will not clear the bitmap
656 * on I/O completion (see raid1_end_write_request) */
657 set_bit(R1BIO_Degraded, &r1_bio->state);
660 atomic_set(&r1_bio->remaining, 0);
662 bio_list_init(&bl);
663 for (i = 0; i < disks; i++) {
664 struct bio *mbio;
665 if (!r1_bio->bios[i])
666 continue;
668 mbio = bio_clone(bio, GFP_NOIO);
669 r1_bio->bios[i] = mbio;
671 mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset;
672 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
673 mbio->bi_end_io = raid1_end_write_request;
674 mbio->bi_rw = WRITE;
675 mbio->bi_private = r1_bio;
677 atomic_inc(&r1_bio->remaining);
679 bio_list_add(&bl, mbio);
682 bitmap_startwrite(bitmap, bio->bi_sector, r1_bio->sectors);
683 spin_lock_irqsave(&conf->device_lock, flags);
684 bio_list_merge(&conf->pending_bio_list, &bl);
685 bio_list_init(&bl);
687 blk_plug_device(mddev->queue);
688 spin_unlock_irqrestore(&conf->device_lock, flags);
690 #if 0
691 while ((bio = bio_list_pop(&bl)) != NULL)
692 generic_make_request(bio);
693 #endif
695 return 0;
698 static void status(struct seq_file *seq, mddev_t *mddev)
700 conf_t *conf = mddev_to_conf(mddev);
701 int i;
703 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
704 conf->working_disks);
705 for (i = 0; i < conf->raid_disks; i++)
706 seq_printf(seq, "%s",
707 conf->mirrors[i].rdev &&
708 conf->mirrors[i].rdev->in_sync ? "U" : "_");
709 seq_printf(seq, "]");
713 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
715 char b[BDEVNAME_SIZE];
716 conf_t *conf = mddev_to_conf(mddev);
719 * If it is not operational, then we have already marked it as dead
720 * else if it is the last working disks, ignore the error, let the
721 * next level up know.
722 * else mark the drive as failed
724 if (rdev->in_sync
725 && conf->working_disks == 1)
727 * Don't fail the drive, act as though we were just a
728 * normal single drive
730 return;
731 if (rdev->in_sync) {
732 mddev->degraded++;
733 conf->working_disks--;
735 * if recovery is running, make sure it aborts.
737 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
739 rdev->in_sync = 0;
740 rdev->faulty = 1;
741 mddev->sb_dirty = 1;
742 printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n"
743 " Operation continuing on %d devices\n",
744 bdevname(rdev->bdev,b), conf->working_disks);
747 static void print_conf(conf_t *conf)
749 int i;
750 mirror_info_t *tmp;
752 printk("RAID1 conf printout:\n");
753 if (!conf) {
754 printk("(!conf)\n");
755 return;
757 printk(" --- wd:%d rd:%d\n", conf->working_disks,
758 conf->raid_disks);
760 for (i = 0; i < conf->raid_disks; i++) {
761 char b[BDEVNAME_SIZE];
762 tmp = conf->mirrors + i;
763 if (tmp->rdev)
764 printk(" disk %d, wo:%d, o:%d, dev:%s\n",
765 i, !tmp->rdev->in_sync, !tmp->rdev->faulty,
766 bdevname(tmp->rdev->bdev,b));
770 static void close_sync(conf_t *conf)
772 spin_lock_irq(&conf->resync_lock);
773 wait_event_lock_irq(conf->wait_resume, !conf->barrier,
774 conf->resync_lock, raid1_unplug(conf->mddev->queue));
775 spin_unlock_irq(&conf->resync_lock);
777 if (conf->barrier) BUG();
778 if (waitqueue_active(&conf->wait_idle)) BUG();
780 mempool_destroy(conf->r1buf_pool);
781 conf->r1buf_pool = NULL;
784 static int raid1_spare_active(mddev_t *mddev)
786 int i;
787 conf_t *conf = mddev->private;
788 mirror_info_t *tmp;
791 * Find all failed disks within the RAID1 configuration
792 * and mark them readable
794 for (i = 0; i < conf->raid_disks; i++) {
795 tmp = conf->mirrors + i;
796 if (tmp->rdev
797 && !tmp->rdev->faulty
798 && !tmp->rdev->in_sync) {
799 conf->working_disks++;
800 mddev->degraded--;
801 tmp->rdev->in_sync = 1;
805 print_conf(conf);
806 return 0;
810 static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
812 conf_t *conf = mddev->private;
813 int found = 0;
814 int mirror = 0;
815 mirror_info_t *p;
817 if (rdev->saved_raid_disk >= 0 &&
818 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
819 mirror = rdev->saved_raid_disk;
820 for (mirror=0; mirror < mddev->raid_disks; mirror++)
821 if ( !(p=conf->mirrors+mirror)->rdev) {
823 blk_queue_stack_limits(mddev->queue,
824 rdev->bdev->bd_disk->queue);
825 /* as we don't honour merge_bvec_fn, we must never risk
826 * violating it, so limit ->max_sector to one PAGE, as
827 * a one page request is never in violation.
829 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
830 mddev->queue->max_sectors > (PAGE_SIZE>>9))
831 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
833 p->head_position = 0;
834 rdev->raid_disk = mirror;
835 found = 1;
836 if (rdev->saved_raid_disk != mirror)
837 conf->fullsync = 1;
838 p->rdev = rdev;
839 break;
842 print_conf(conf);
843 return found;
846 static int raid1_remove_disk(mddev_t *mddev, int number)
848 conf_t *conf = mddev->private;
849 int err = 0;
850 mdk_rdev_t *rdev;
851 mirror_info_t *p = conf->mirrors+ number;
853 print_conf(conf);
854 rdev = p->rdev;
855 if (rdev) {
856 if (rdev->in_sync ||
857 atomic_read(&rdev->nr_pending)) {
858 err = -EBUSY;
859 goto abort;
861 p->rdev = NULL;
862 synchronize_rcu();
863 if (atomic_read(&rdev->nr_pending)) {
864 /* lost the race, try later */
865 err = -EBUSY;
866 p->rdev = rdev;
869 abort:
871 print_conf(conf);
872 return err;
876 static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error)
878 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
879 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
880 conf_t *conf = mddev_to_conf(r1_bio->mddev);
882 if (bio->bi_size)
883 return 1;
885 if (r1_bio->bios[r1_bio->read_disk] != bio)
886 BUG();
887 update_head_pos(r1_bio->read_disk, r1_bio);
889 * we have read a block, now it needs to be re-written,
890 * or re-read if the read failed.
891 * We don't do much here, just schedule handling by raid1d
893 if (!uptodate) {
894 md_error(r1_bio->mddev,
895 conf->mirrors[r1_bio->read_disk].rdev);
896 set_bit(R1BIO_Degraded, &r1_bio->state);
897 } else
898 set_bit(R1BIO_Uptodate, &r1_bio->state);
899 rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev);
900 reschedule_retry(r1_bio);
901 return 0;
904 static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error)
906 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
907 r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private);
908 mddev_t *mddev = r1_bio->mddev;
909 conf_t *conf = mddev_to_conf(mddev);
910 int i;
911 int mirror=0;
913 if (bio->bi_size)
914 return 1;
916 for (i = 0; i < conf->raid_disks; i++)
917 if (r1_bio->bios[i] == bio) {
918 mirror = i;
919 break;
921 if (!uptodate) {
922 md_error(mddev, conf->mirrors[mirror].rdev);
923 set_bit(R1BIO_Degraded, &r1_bio->state);
925 update_head_pos(mirror, r1_bio);
927 if (atomic_dec_and_test(&r1_bio->remaining)) {
928 md_done_sync(mddev, r1_bio->sectors, uptodate);
929 put_buf(r1_bio);
931 rdev_dec_pending(conf->mirrors[mirror].rdev, mddev);
932 return 0;
935 static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio)
937 conf_t *conf = mddev_to_conf(mddev);
938 int i;
939 int disks = conf->raid_disks;
940 struct bio *bio, *wbio;
942 bio = r1_bio->bios[r1_bio->read_disk];
945 if (r1_bio->sector == 0) printk("First sync write startss\n");
948 * schedule writes
950 if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) {
952 * There is no point trying a read-for-reconstruct as
953 * reconstruct is about to be aborted
955 char b[BDEVNAME_SIZE];
956 printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error"
957 " for block %llu\n",
958 bdevname(bio->bi_bdev,b),
959 (unsigned long long)r1_bio->sector);
960 md_done_sync(mddev, r1_bio->sectors, 0);
961 put_buf(r1_bio);
962 return;
965 atomic_set(&r1_bio->remaining, 1);
966 for (i = 0; i < disks ; i++) {
967 wbio = r1_bio->bios[i];
968 if (wbio->bi_end_io != end_sync_write)
969 continue;
971 atomic_inc(&conf->mirrors[i].rdev->nr_pending);
972 atomic_inc(&r1_bio->remaining);
973 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
975 generic_make_request(wbio);
978 if (atomic_dec_and_test(&r1_bio->remaining)) {
979 /* if we're here, all write(s) have completed, so clean up */
980 md_done_sync(mddev, r1_bio->sectors, 1);
981 put_buf(r1_bio);
986 * This is a kernel thread which:
988 * 1. Retries failed read operations on working mirrors.
989 * 2. Updates the raid superblock when problems encounter.
990 * 3. Performs writes following reads for array syncronising.
993 static void raid1d(mddev_t *mddev)
995 r1bio_t *r1_bio;
996 struct bio *bio;
997 unsigned long flags;
998 conf_t *conf = mddev_to_conf(mddev);
999 struct list_head *head = &conf->retry_list;
1000 int unplug=0;
1001 mdk_rdev_t *rdev;
1003 md_check_recovery(mddev);
1005 for (;;) {
1006 char b[BDEVNAME_SIZE];
1007 spin_lock_irqsave(&conf->device_lock, flags);
1009 if (conf->pending_bio_list.head) {
1010 bio = bio_list_get(&conf->pending_bio_list);
1011 blk_remove_plug(mddev->queue);
1012 spin_unlock_irqrestore(&conf->device_lock, flags);
1013 /* flush any pending bitmap writes to disk before proceeding w/ I/O */
1014 if (bitmap_unplug(mddev->bitmap) != 0)
1015 printk("%s: bitmap file write failed!\n", mdname(mddev));
1017 while (bio) { /* submit pending writes */
1018 struct bio *next = bio->bi_next;
1019 bio->bi_next = NULL;
1020 generic_make_request(bio);
1021 bio = next;
1023 unplug = 1;
1025 continue;
1028 if (list_empty(head))
1029 break;
1030 r1_bio = list_entry(head->prev, r1bio_t, retry_list);
1031 list_del(head->prev);
1032 spin_unlock_irqrestore(&conf->device_lock, flags);
1034 mddev = r1_bio->mddev;
1035 conf = mddev_to_conf(mddev);
1036 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
1037 sync_request_write(mddev, r1_bio);
1038 unplug = 1;
1039 } else {
1040 int disk;
1041 bio = r1_bio->bios[r1_bio->read_disk];
1042 if ((disk=read_balance(conf, r1_bio)) == -1) {
1043 printk(KERN_ALERT "raid1: %s: unrecoverable I/O"
1044 " read error for block %llu\n",
1045 bdevname(bio->bi_bdev,b),
1046 (unsigned long long)r1_bio->sector);
1047 raid_end_bio_io(r1_bio);
1048 } else {
1049 r1_bio->bios[r1_bio->read_disk] = NULL;
1050 r1_bio->read_disk = disk;
1051 bio_put(bio);
1052 bio = bio_clone(r1_bio->master_bio, GFP_NOIO);
1053 r1_bio->bios[r1_bio->read_disk] = bio;
1054 rdev = conf->mirrors[disk].rdev;
1055 if (printk_ratelimit())
1056 printk(KERN_ERR "raid1: %s: redirecting sector %llu to"
1057 " another mirror\n",
1058 bdevname(rdev->bdev,b),
1059 (unsigned long long)r1_bio->sector);
1060 bio->bi_sector = r1_bio->sector + rdev->data_offset;
1061 bio->bi_bdev = rdev->bdev;
1062 bio->bi_end_io = raid1_end_read_request;
1063 bio->bi_rw = READ;
1064 bio->bi_private = r1_bio;
1065 unplug = 1;
1066 generic_make_request(bio);
1070 spin_unlock_irqrestore(&conf->device_lock, flags);
1071 if (unplug)
1072 unplug_slaves(mddev);
1076 static int init_resync(conf_t *conf)
1078 int buffs;
1080 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
1081 if (conf->r1buf_pool)
1082 BUG();
1083 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
1084 conf->poolinfo);
1085 if (!conf->r1buf_pool)
1086 return -ENOMEM;
1087 conf->next_resync = 0;
1088 return 0;
1092 * perform a "sync" on one "block"
1094 * We need to make sure that no normal I/O request - particularly write
1095 * requests - conflict with active sync requests.
1097 * This is achieved by tracking pending requests and a 'barrier' concept
1098 * that can be installed to exclude normal IO requests.
1101 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1103 conf_t *conf = mddev_to_conf(mddev);
1104 mirror_info_t *mirror;
1105 r1bio_t *r1_bio;
1106 struct bio *bio;
1107 sector_t max_sector, nr_sectors;
1108 int disk;
1109 int i;
1110 int write_targets = 0;
1111 int sync_blocks;
1113 if (!conf->r1buf_pool)
1116 printk("sync start - bitmap %p\n", mddev->bitmap);
1118 if (init_resync(conf))
1119 return 0;
1122 max_sector = mddev->size << 1;
1123 if (sector_nr >= max_sector) {
1124 /* If we aborted, we need to abort the
1125 * sync on the 'current' bitmap chunk (there will
1126 * only be one in raid1 resync.
1127 * We can find the current addess in mddev->curr_resync
1129 if (!conf->fullsync) {
1130 if (mddev->curr_resync < max_sector)
1131 bitmap_end_sync(mddev->bitmap,
1132 mddev->curr_resync,
1133 &sync_blocks, 1);
1134 bitmap_close_sync(mddev->bitmap);
1136 if (mddev->curr_resync >= max_sector)
1137 conf->fullsync = 0;
1138 close_sync(conf);
1139 return 0;
1142 if (!conf->fullsync &&
1143 !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks)) {
1144 /* We can skip this block, and probably several more */
1145 *skipped = 1;
1146 return sync_blocks;
1149 * If there is non-resync activity waiting for us then
1150 * put in a delay to throttle resync.
1152 if (!go_faster && waitqueue_active(&conf->wait_resume))
1153 msleep_interruptible(1000);
1154 device_barrier(conf, sector_nr + RESYNC_SECTORS);
1157 * If reconstructing, and >1 working disc,
1158 * could dedicate one to rebuild and others to
1159 * service read requests ..
1161 disk = conf->last_used;
1162 /* make sure disk is operational */
1164 while (conf->mirrors[disk].rdev == NULL ||
1165 !conf->mirrors[disk].rdev->in_sync) {
1166 if (disk <= 0)
1167 disk = conf->raid_disks;
1168 disk--;
1169 if (disk == conf->last_used)
1170 break;
1172 conf->last_used = disk;
1173 atomic_inc(&conf->mirrors[disk].rdev->nr_pending);
1176 mirror = conf->mirrors + disk;
1178 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
1180 spin_lock_irq(&conf->resync_lock);
1181 conf->nr_pending++;
1182 spin_unlock_irq(&conf->resync_lock);
1184 r1_bio->mddev = mddev;
1185 r1_bio->sector = sector_nr;
1186 r1_bio->state = 0;
1187 set_bit(R1BIO_IsSync, &r1_bio->state);
1188 r1_bio->read_disk = disk;
1190 for (i=0; i < conf->raid_disks; i++) {
1191 bio = r1_bio->bios[i];
1193 /* take from bio_init */
1194 bio->bi_next = NULL;
1195 bio->bi_flags |= 1 << BIO_UPTODATE;
1196 bio->bi_rw = 0;
1197 bio->bi_vcnt = 0;
1198 bio->bi_idx = 0;
1199 bio->bi_phys_segments = 0;
1200 bio->bi_hw_segments = 0;
1201 bio->bi_size = 0;
1202 bio->bi_end_io = NULL;
1203 bio->bi_private = NULL;
1205 if (i == disk) {
1206 bio->bi_rw = READ;
1207 bio->bi_end_io = end_sync_read;
1208 } else if (conf->mirrors[i].rdev &&
1209 !conf->mirrors[i].rdev->faulty &&
1210 (!conf->mirrors[i].rdev->in_sync ||
1211 sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) {
1212 bio->bi_rw = WRITE;
1213 bio->bi_end_io = end_sync_write;
1214 write_targets ++;
1215 } else
1216 continue;
1217 bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset;
1218 bio->bi_bdev = conf->mirrors[i].rdev->bdev;
1219 bio->bi_private = r1_bio;
1222 if (write_targets + 1 < conf->raid_disks)
1223 /* array degraded, can't clear bitmap */
1224 set_bit(R1BIO_Degraded, &r1_bio->state);
1226 if (write_targets == 0) {
1227 /* There is nowhere to write, so all non-sync
1228 * drives must be failed - so we are finished
1230 sector_t rv = max_sector - sector_nr;
1231 *skipped = 1;
1232 put_buf(r1_bio);
1233 rdev_dec_pending(conf->mirrors[disk].rdev, mddev);
1234 return rv;
1237 nr_sectors = 0;
1238 sync_blocks = 0;
1239 do {
1240 struct page *page;
1241 int len = PAGE_SIZE;
1242 if (sector_nr + (len>>9) > max_sector)
1243 len = (max_sector - sector_nr) << 9;
1244 if (len == 0)
1245 break;
1246 if (!conf->fullsync) {
1247 if (sync_blocks == 0) {
1248 if (!bitmap_start_sync(mddev->bitmap,
1249 sector_nr, &sync_blocks))
1250 break;
1251 if (sync_blocks < (PAGE_SIZE>>9))
1252 BUG();
1253 if (len > (sync_blocks<<9)) len = sync_blocks<<9;
1257 for (i=0 ; i < conf->raid_disks; i++) {
1258 bio = r1_bio->bios[i];
1259 if (bio->bi_end_io) {
1260 page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page;
1261 if (bio_add_page(bio, page, len, 0) == 0) {
1262 /* stop here */
1263 r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page;
1264 while (i > 0) {
1265 i--;
1266 bio = r1_bio->bios[i];
1267 if (bio->bi_end_io==NULL) continue;
1268 /* remove last page from this bio */
1269 bio->bi_vcnt--;
1270 bio->bi_size -= len;
1271 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
1273 goto bio_full;
1277 nr_sectors += len>>9;
1278 sector_nr += len>>9;
1279 sync_blocks -= (len>>9);
1280 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
1281 bio_full:
1282 bio = r1_bio->bios[disk];
1283 r1_bio->sectors = nr_sectors;
1285 md_sync_acct(mirror->rdev->bdev, nr_sectors);
1287 generic_make_request(bio);
1289 return nr_sectors;
1292 static int run(mddev_t *mddev)
1294 conf_t *conf;
1295 int i, j, disk_idx;
1296 mirror_info_t *disk;
1297 mdk_rdev_t *rdev;
1298 struct list_head *tmp;
1300 if (mddev->level != 1) {
1301 printk("raid1: %s: raid level not set to mirroring (%d)\n",
1302 mdname(mddev), mddev->level);
1303 goto out;
1306 * copy the already verified devices into our private RAID1
1307 * bookkeeping area. [whatever we allocate in run(),
1308 * should be freed in stop()]
1310 conf = kmalloc(sizeof(conf_t), GFP_KERNEL);
1311 mddev->private = conf;
1312 if (!conf)
1313 goto out_no_mem;
1315 memset(conf, 0, sizeof(*conf));
1316 conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks,
1317 GFP_KERNEL);
1318 if (!conf->mirrors)
1319 goto out_no_mem;
1321 memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks);
1323 conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
1324 if (!conf->poolinfo)
1325 goto out_no_mem;
1326 conf->poolinfo->mddev = mddev;
1327 conf->poolinfo->raid_disks = mddev->raid_disks;
1328 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1329 r1bio_pool_free,
1330 conf->poolinfo);
1331 if (!conf->r1bio_pool)
1332 goto out_no_mem;
1334 ITERATE_RDEV(mddev, rdev, tmp) {
1335 disk_idx = rdev->raid_disk;
1336 if (disk_idx >= mddev->raid_disks
1337 || disk_idx < 0)
1338 continue;
1339 disk = conf->mirrors + disk_idx;
1341 disk->rdev = rdev;
1343 blk_queue_stack_limits(mddev->queue,
1344 rdev->bdev->bd_disk->queue);
1345 /* as we don't honour merge_bvec_fn, we must never risk
1346 * violating it, so limit ->max_sector to one PAGE, as
1347 * a one page request is never in violation.
1349 if (rdev->bdev->bd_disk->queue->merge_bvec_fn &&
1350 mddev->queue->max_sectors > (PAGE_SIZE>>9))
1351 blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9);
1353 disk->head_position = 0;
1354 if (!rdev->faulty && rdev->in_sync)
1355 conf->working_disks++;
1357 conf->raid_disks = mddev->raid_disks;
1358 conf->mddev = mddev;
1359 spin_lock_init(&conf->device_lock);
1360 INIT_LIST_HEAD(&conf->retry_list);
1361 if (conf->working_disks == 1)
1362 mddev->recovery_cp = MaxSector;
1364 spin_lock_init(&conf->resync_lock);
1365 init_waitqueue_head(&conf->wait_idle);
1366 init_waitqueue_head(&conf->wait_resume);
1368 bio_list_init(&conf->pending_bio_list);
1369 bio_list_init(&conf->flushing_bio_list);
1371 if (!conf->working_disks) {
1372 printk(KERN_ERR "raid1: no operational mirrors for %s\n",
1373 mdname(mddev));
1374 goto out_free_conf;
1377 mddev->degraded = 0;
1378 for (i = 0; i < conf->raid_disks; i++) {
1380 disk = conf->mirrors + i;
1382 if (!disk->rdev) {
1383 disk->head_position = 0;
1384 mddev->degraded++;
1389 * find the first working one and use it as a starting point
1390 * to read balancing.
1392 for (j = 0; j < conf->raid_disks &&
1393 (!conf->mirrors[j].rdev ||
1394 !conf->mirrors[j].rdev->in_sync) ; j++)
1395 /* nothing */;
1396 conf->last_used = j;
1399 mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1");
1400 if (!mddev->thread) {
1401 printk(KERN_ERR
1402 "raid1: couldn't allocate thread for %s\n",
1403 mdname(mddev));
1404 goto out_free_conf;
1406 if (mddev->bitmap) mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1408 printk(KERN_INFO
1409 "raid1: raid set %s active with %d out of %d mirrors\n",
1410 mdname(mddev), mddev->raid_disks - mddev->degraded,
1411 mddev->raid_disks);
1413 * Ok, everything is just fine now
1415 mddev->array_size = mddev->size;
1417 mddev->queue->unplug_fn = raid1_unplug;
1418 mddev->queue->issue_flush_fn = raid1_issue_flush;
1420 return 0;
1422 out_no_mem:
1423 printk(KERN_ERR "raid1: couldn't allocate memory for %s\n",
1424 mdname(mddev));
1426 out_free_conf:
1427 if (conf) {
1428 if (conf->r1bio_pool)
1429 mempool_destroy(conf->r1bio_pool);
1430 kfree(conf->mirrors);
1431 kfree(conf->poolinfo);
1432 kfree(conf);
1433 mddev->private = NULL;
1435 out:
1436 return -EIO;
1439 static int stop(mddev_t *mddev)
1441 conf_t *conf = mddev_to_conf(mddev);
1443 md_unregister_thread(mddev->thread);
1444 mddev->thread = NULL;
1445 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1446 if (conf->r1bio_pool)
1447 mempool_destroy(conf->r1bio_pool);
1448 kfree(conf->mirrors);
1449 kfree(conf->poolinfo);
1450 kfree(conf);
1451 mddev->private = NULL;
1452 return 0;
1455 static int raid1_resize(mddev_t *mddev, sector_t sectors)
1457 /* no resync is happening, and there is enough space
1458 * on all devices, so we can resize.
1459 * We need to make sure resync covers any new space.
1460 * If the array is shrinking we should possibly wait until
1461 * any io in the removed space completes, but it hardly seems
1462 * worth it.
1464 mddev->array_size = sectors>>1;
1465 set_capacity(mddev->gendisk, mddev->array_size << 1);
1466 mddev->changed = 1;
1467 if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) {
1468 mddev->recovery_cp = mddev->size << 1;
1469 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1471 mddev->size = mddev->array_size;
1472 return 0;
1475 static int raid1_reshape(mddev_t *mddev, int raid_disks)
1477 /* We need to:
1478 * 1/ resize the r1bio_pool
1479 * 2/ resize conf->mirrors
1481 * We allocate a new r1bio_pool if we can.
1482 * Then raise a device barrier and wait until all IO stops.
1483 * Then resize conf->mirrors and swap in the new r1bio pool.
1485 * At the same time, we "pack" the devices so that all the missing
1486 * devices have the higher raid_disk numbers.
1488 mempool_t *newpool, *oldpool;
1489 struct pool_info *newpoolinfo;
1490 mirror_info_t *newmirrors;
1491 conf_t *conf = mddev_to_conf(mddev);
1492 int cnt;
1494 int d, d2;
1496 if (raid_disks < conf->raid_disks) {
1497 cnt=0;
1498 for (d= 0; d < conf->raid_disks; d++)
1499 if (conf->mirrors[d].rdev)
1500 cnt++;
1501 if (cnt > raid_disks)
1502 return -EBUSY;
1505 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
1506 if (!newpoolinfo)
1507 return -ENOMEM;
1508 newpoolinfo->mddev = mddev;
1509 newpoolinfo->raid_disks = raid_disks;
1511 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
1512 r1bio_pool_free, newpoolinfo);
1513 if (!newpool) {
1514 kfree(newpoolinfo);
1515 return -ENOMEM;
1517 newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL);
1518 if (!newmirrors) {
1519 kfree(newpoolinfo);
1520 mempool_destroy(newpool);
1521 return -ENOMEM;
1523 memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks);
1525 spin_lock_irq(&conf->resync_lock);
1526 conf->barrier++;
1527 wait_event_lock_irq(conf->wait_idle, !conf->nr_pending,
1528 conf->resync_lock, raid1_unplug(mddev->queue));
1529 spin_unlock_irq(&conf->resync_lock);
1531 /* ok, everything is stopped */
1532 oldpool = conf->r1bio_pool;
1533 conf->r1bio_pool = newpool;
1535 for (d=d2=0; d < conf->raid_disks; d++)
1536 if (conf->mirrors[d].rdev) {
1537 conf->mirrors[d].rdev->raid_disk = d2;
1538 newmirrors[d2++].rdev = conf->mirrors[d].rdev;
1540 kfree(conf->mirrors);
1541 conf->mirrors = newmirrors;
1542 kfree(conf->poolinfo);
1543 conf->poolinfo = newpoolinfo;
1545 mddev->degraded += (raid_disks - conf->raid_disks);
1546 conf->raid_disks = mddev->raid_disks = raid_disks;
1548 conf->last_used = 0; /* just make sure it is in-range */
1549 spin_lock_irq(&conf->resync_lock);
1550 conf->barrier--;
1551 spin_unlock_irq(&conf->resync_lock);
1552 wake_up(&conf->wait_resume);
1553 wake_up(&conf->wait_idle);
1556 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1557 md_wakeup_thread(mddev->thread);
1559 mempool_destroy(oldpool);
1560 return 0;
1564 static mdk_personality_t raid1_personality =
1566 .name = "raid1",
1567 .owner = THIS_MODULE,
1568 .make_request = make_request,
1569 .run = run,
1570 .stop = stop,
1571 .status = status,
1572 .error_handler = error,
1573 .hot_add_disk = raid1_add_disk,
1574 .hot_remove_disk= raid1_remove_disk,
1575 .spare_active = raid1_spare_active,
1576 .sync_request = sync_request,
1577 .resize = raid1_resize,
1578 .reshape = raid1_reshape,
1581 static int __init raid_init(void)
1583 return register_md_personality(RAID1, &raid1_personality);
1586 static void raid_exit(void)
1588 unregister_md_personality(RAID1);
1591 module_init(raid_init);
1592 module_exit(raid_exit);
1593 MODULE_LICENSE("GPL");
1594 MODULE_ALIAS("md-personality-3"); /* RAID1 */