Linux 3.4.102
[linux/fpc-iii.git] / drivers / md / raid1.c
blob75e66c61250580f1c693e320c9238ad972898195
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 <linux/slab.h>
35 #include <linux/delay.h>
36 #include <linux/blkdev.h>
37 #include <linux/module.h>
38 #include <linux/seq_file.h>
39 #include <linux/ratelimit.h>
40 #include "md.h"
41 #include "raid1.h"
42 #include "bitmap.h"
45 * Number of guaranteed r1bios in case of extreme VM load:
47 #define NR_RAID1_BIOS 256
49 /* When there are this many requests queue to be written by
50 * the raid1 thread, we become 'congested' to provide back-pressure
51 * for writeback.
53 static int max_queued_requests = 1024;
55 static void allow_barrier(struct r1conf *conf);
56 static void lower_barrier(struct r1conf *conf);
58 static void * r1bio_pool_alloc(gfp_t gfp_flags, void *data)
60 struct pool_info *pi = data;
61 int size = offsetof(struct r1bio, bios[pi->raid_disks]);
63 /* allocate a r1bio with room for raid_disks entries in the bios array */
64 return kzalloc(size, gfp_flags);
67 static void r1bio_pool_free(void *r1_bio, void *data)
69 kfree(r1_bio);
72 #define RESYNC_BLOCK_SIZE (64*1024)
73 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
74 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
75 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
76 #define RESYNC_WINDOW (2048*1024)
78 static void * r1buf_pool_alloc(gfp_t gfp_flags, void *data)
80 struct pool_info *pi = data;
81 struct page *page;
82 struct r1bio *r1_bio;
83 struct bio *bio;
84 int i, j;
86 r1_bio = r1bio_pool_alloc(gfp_flags, pi);
87 if (!r1_bio)
88 return NULL;
91 * Allocate bios : 1 for reading, n-1 for writing
93 for (j = pi->raid_disks ; j-- ; ) {
94 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
95 if (!bio)
96 goto out_free_bio;
97 r1_bio->bios[j] = bio;
100 * Allocate RESYNC_PAGES data pages and attach them to
101 * the first bio.
102 * If this is a user-requested check/repair, allocate
103 * RESYNC_PAGES for each bio.
105 if (test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery))
106 j = pi->raid_disks;
107 else
108 j = 1;
109 while(j--) {
110 bio = r1_bio->bios[j];
111 for (i = 0; i < RESYNC_PAGES; i++) {
112 page = alloc_page(gfp_flags);
113 if (unlikely(!page))
114 goto out_free_pages;
116 bio->bi_io_vec[i].bv_page = page;
117 bio->bi_vcnt = i+1;
120 /* If not user-requests, copy the page pointers to all bios */
121 if (!test_bit(MD_RECOVERY_REQUESTED, &pi->mddev->recovery)) {
122 for (i=0; i<RESYNC_PAGES ; i++)
123 for (j=1; j<pi->raid_disks; j++)
124 r1_bio->bios[j]->bi_io_vec[i].bv_page =
125 r1_bio->bios[0]->bi_io_vec[i].bv_page;
128 r1_bio->master_bio = NULL;
130 return r1_bio;
132 out_free_pages:
133 for (j=0 ; j < pi->raid_disks; j++)
134 for (i=0; i < r1_bio->bios[j]->bi_vcnt ; i++)
135 put_page(r1_bio->bios[j]->bi_io_vec[i].bv_page);
136 j = -1;
137 out_free_bio:
138 while (++j < pi->raid_disks)
139 bio_put(r1_bio->bios[j]);
140 r1bio_pool_free(r1_bio, data);
141 return NULL;
144 static void r1buf_pool_free(void *__r1_bio, void *data)
146 struct pool_info *pi = data;
147 int i,j;
148 struct r1bio *r1bio = __r1_bio;
150 for (i = 0; i < RESYNC_PAGES; i++)
151 for (j = pi->raid_disks; j-- ;) {
152 if (j == 0 ||
153 r1bio->bios[j]->bi_io_vec[i].bv_page !=
154 r1bio->bios[0]->bi_io_vec[i].bv_page)
155 safe_put_page(r1bio->bios[j]->bi_io_vec[i].bv_page);
157 for (i=0 ; i < pi->raid_disks; i++)
158 bio_put(r1bio->bios[i]);
160 r1bio_pool_free(r1bio, data);
163 static void put_all_bios(struct r1conf *conf, struct r1bio *r1_bio)
165 int i;
167 for (i = 0; i < conf->raid_disks * 2; i++) {
168 struct bio **bio = r1_bio->bios + i;
169 if (!BIO_SPECIAL(*bio))
170 bio_put(*bio);
171 *bio = NULL;
175 static void free_r1bio(struct r1bio *r1_bio)
177 struct r1conf *conf = r1_bio->mddev->private;
179 put_all_bios(conf, r1_bio);
180 mempool_free(r1_bio, conf->r1bio_pool);
183 static void put_buf(struct r1bio *r1_bio)
185 struct r1conf *conf = r1_bio->mddev->private;
186 int i;
188 for (i = 0; i < conf->raid_disks * 2; i++) {
189 struct bio *bio = r1_bio->bios[i];
190 if (bio->bi_end_io)
191 rdev_dec_pending(conf->mirrors[i].rdev, r1_bio->mddev);
194 mempool_free(r1_bio, conf->r1buf_pool);
196 lower_barrier(conf);
199 static void reschedule_retry(struct r1bio *r1_bio)
201 unsigned long flags;
202 struct mddev *mddev = r1_bio->mddev;
203 struct r1conf *conf = mddev->private;
205 spin_lock_irqsave(&conf->device_lock, flags);
206 list_add(&r1_bio->retry_list, &conf->retry_list);
207 conf->nr_queued ++;
208 spin_unlock_irqrestore(&conf->device_lock, flags);
210 wake_up(&conf->wait_barrier);
211 md_wakeup_thread(mddev->thread);
215 * raid_end_bio_io() is called when we have finished servicing a mirrored
216 * operation and are ready to return a success/failure code to the buffer
217 * cache layer.
219 static void call_bio_endio(struct r1bio *r1_bio)
221 struct bio *bio = r1_bio->master_bio;
222 int done;
223 struct r1conf *conf = r1_bio->mddev->private;
225 if (bio->bi_phys_segments) {
226 unsigned long flags;
227 spin_lock_irqsave(&conf->device_lock, flags);
228 bio->bi_phys_segments--;
229 done = (bio->bi_phys_segments == 0);
230 spin_unlock_irqrestore(&conf->device_lock, flags);
231 } else
232 done = 1;
234 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
235 clear_bit(BIO_UPTODATE, &bio->bi_flags);
236 if (done) {
237 bio_endio(bio, 0);
239 * Wake up any possible resync thread that waits for the device
240 * to go idle.
242 allow_barrier(conf);
246 static void raid_end_bio_io(struct r1bio *r1_bio)
248 struct bio *bio = r1_bio->master_bio;
250 /* if nobody has done the final endio yet, do it now */
251 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
252 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
253 (bio_data_dir(bio) == WRITE) ? "write" : "read",
254 (unsigned long long) bio->bi_sector,
255 (unsigned long long) bio->bi_sector +
256 (bio->bi_size >> 9) - 1);
258 call_bio_endio(r1_bio);
260 free_r1bio(r1_bio);
264 * Update disk head position estimator based on IRQ completion info.
266 static inline void update_head_pos(int disk, struct r1bio *r1_bio)
268 struct r1conf *conf = r1_bio->mddev->private;
270 conf->mirrors[disk].head_position =
271 r1_bio->sector + (r1_bio->sectors);
275 * Find the disk number which triggered given bio
277 static int find_bio_disk(struct r1bio *r1_bio, struct bio *bio)
279 int mirror;
280 struct r1conf *conf = r1_bio->mddev->private;
281 int raid_disks = conf->raid_disks;
283 for (mirror = 0; mirror < raid_disks * 2; mirror++)
284 if (r1_bio->bios[mirror] == bio)
285 break;
287 BUG_ON(mirror == raid_disks * 2);
288 update_head_pos(mirror, r1_bio);
290 return mirror;
293 static void raid1_end_read_request(struct bio *bio, int error)
295 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
296 struct r1bio *r1_bio = bio->bi_private;
297 int mirror;
298 struct r1conf *conf = r1_bio->mddev->private;
300 mirror = r1_bio->read_disk;
302 * this branch is our 'one mirror IO has finished' event handler:
304 update_head_pos(mirror, r1_bio);
306 if (uptodate)
307 set_bit(R1BIO_Uptodate, &r1_bio->state);
308 else {
309 /* If all other devices have failed, we want to return
310 * the error upwards rather than fail the last device.
311 * Here we redefine "uptodate" to mean "Don't want to retry"
313 unsigned long flags;
314 spin_lock_irqsave(&conf->device_lock, flags);
315 if (r1_bio->mddev->degraded == conf->raid_disks ||
316 (r1_bio->mddev->degraded == conf->raid_disks-1 &&
317 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags)))
318 uptodate = 1;
319 spin_unlock_irqrestore(&conf->device_lock, flags);
322 if (uptodate)
323 raid_end_bio_io(r1_bio);
324 else {
326 * oops, read error:
328 char b[BDEVNAME_SIZE];
329 printk_ratelimited(
330 KERN_ERR "md/raid1:%s: %s: "
331 "rescheduling sector %llu\n",
332 mdname(conf->mddev),
333 bdevname(conf->mirrors[mirror].rdev->bdev,
335 (unsigned long long)r1_bio->sector);
336 set_bit(R1BIO_ReadError, &r1_bio->state);
337 reschedule_retry(r1_bio);
340 rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev);
343 static void close_write(struct r1bio *r1_bio)
345 /* it really is the end of this request */
346 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
347 /* free extra copy of the data pages */
348 int i = r1_bio->behind_page_count;
349 while (i--)
350 safe_put_page(r1_bio->behind_bvecs[i].bv_page);
351 kfree(r1_bio->behind_bvecs);
352 r1_bio->behind_bvecs = NULL;
354 /* clear the bitmap if all writes complete successfully */
355 bitmap_endwrite(r1_bio->mddev->bitmap, r1_bio->sector,
356 r1_bio->sectors,
357 !test_bit(R1BIO_Degraded, &r1_bio->state),
358 test_bit(R1BIO_BehindIO, &r1_bio->state));
359 md_write_end(r1_bio->mddev);
362 static void r1_bio_write_done(struct r1bio *r1_bio)
364 if (!atomic_dec_and_test(&r1_bio->remaining))
365 return;
367 if (test_bit(R1BIO_WriteError, &r1_bio->state))
368 reschedule_retry(r1_bio);
369 else {
370 close_write(r1_bio);
371 if (test_bit(R1BIO_MadeGood, &r1_bio->state))
372 reschedule_retry(r1_bio);
373 else
374 raid_end_bio_io(r1_bio);
378 static void raid1_end_write_request(struct bio *bio, int error)
380 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
381 struct r1bio *r1_bio = bio->bi_private;
382 int mirror, behind = test_bit(R1BIO_BehindIO, &r1_bio->state);
383 struct r1conf *conf = r1_bio->mddev->private;
384 struct bio *to_put = NULL;
386 mirror = find_bio_disk(r1_bio, bio);
389 * 'one mirror IO has finished' event handler:
391 if (!uptodate) {
392 set_bit(WriteErrorSeen,
393 &conf->mirrors[mirror].rdev->flags);
394 if (!test_and_set_bit(WantReplacement,
395 &conf->mirrors[mirror].rdev->flags))
396 set_bit(MD_RECOVERY_NEEDED, &
397 conf->mddev->recovery);
399 set_bit(R1BIO_WriteError, &r1_bio->state);
400 } else {
402 * Set R1BIO_Uptodate in our master bio, so that we
403 * will return a good error code for to the higher
404 * levels even if IO on some other mirrored buffer
405 * fails.
407 * The 'master' represents the composite IO operation
408 * to user-side. So if something waits for IO, then it
409 * will wait for the 'master' bio.
411 sector_t first_bad;
412 int bad_sectors;
414 r1_bio->bios[mirror] = NULL;
415 to_put = bio;
417 * Do not set R1BIO_Uptodate if the current device is
418 * rebuilding or Faulty. This is because we cannot use
419 * such device for properly reading the data back (we could
420 * potentially use it, if the current write would have felt
421 * before rdev->recovery_offset, but for simplicity we don't
422 * check this here.
424 if (test_bit(In_sync, &conf->mirrors[mirror].rdev->flags) &&
425 !test_bit(Faulty, &conf->mirrors[mirror].rdev->flags))
426 set_bit(R1BIO_Uptodate, &r1_bio->state);
428 /* Maybe we can clear some bad blocks. */
429 if (is_badblock(conf->mirrors[mirror].rdev,
430 r1_bio->sector, r1_bio->sectors,
431 &first_bad, &bad_sectors)) {
432 r1_bio->bios[mirror] = IO_MADE_GOOD;
433 set_bit(R1BIO_MadeGood, &r1_bio->state);
437 if (behind) {
438 if (test_bit(WriteMostly, &conf->mirrors[mirror].rdev->flags))
439 atomic_dec(&r1_bio->behind_remaining);
442 * In behind mode, we ACK the master bio once the I/O
443 * has safely reached all non-writemostly
444 * disks. Setting the Returned bit ensures that this
445 * gets done only once -- we don't ever want to return
446 * -EIO here, instead we'll wait
448 if (atomic_read(&r1_bio->behind_remaining) >= (atomic_read(&r1_bio->remaining)-1) &&
449 test_bit(R1BIO_Uptodate, &r1_bio->state)) {
450 /* Maybe we can return now */
451 if (!test_and_set_bit(R1BIO_Returned, &r1_bio->state)) {
452 struct bio *mbio = r1_bio->master_bio;
453 pr_debug("raid1: behind end write sectors"
454 " %llu-%llu\n",
455 (unsigned long long) mbio->bi_sector,
456 (unsigned long long) mbio->bi_sector +
457 (mbio->bi_size >> 9) - 1);
458 call_bio_endio(r1_bio);
462 if (r1_bio->bios[mirror] == NULL)
463 rdev_dec_pending(conf->mirrors[mirror].rdev,
464 conf->mddev);
467 * Let's see if all mirrored write operations have finished
468 * already.
470 r1_bio_write_done(r1_bio);
472 if (to_put)
473 bio_put(to_put);
478 * This routine returns the disk from which the requested read should
479 * be done. There is a per-array 'next expected sequential IO' sector
480 * number - if this matches on the next IO then we use the last disk.
481 * There is also a per-disk 'last know head position' sector that is
482 * maintained from IRQ contexts, both the normal and the resync IO
483 * completion handlers update this position correctly. If there is no
484 * perfect sequential match then we pick the disk whose head is closest.
486 * If there are 2 mirrors in the same 2 devices, performance degrades
487 * because position is mirror, not device based.
489 * The rdev for the device selected will have nr_pending incremented.
491 static int read_balance(struct r1conf *conf, struct r1bio *r1_bio, int *max_sectors)
493 const sector_t this_sector = r1_bio->sector;
494 int sectors;
495 int best_good_sectors;
496 int start_disk;
497 int best_disk;
498 int i;
499 sector_t best_dist;
500 struct md_rdev *rdev;
501 int choose_first;
503 rcu_read_lock();
505 * Check if we can balance. We can balance on the whole
506 * device if no resync is going on, or below the resync window.
507 * We take the first readable disk when above the resync window.
509 retry:
510 sectors = r1_bio->sectors;
511 best_disk = -1;
512 best_dist = MaxSector;
513 best_good_sectors = 0;
515 if (conf->mddev->recovery_cp < MaxSector &&
516 (this_sector + sectors >= conf->next_resync)) {
517 choose_first = 1;
518 start_disk = 0;
519 } else {
520 choose_first = 0;
521 start_disk = conf->last_used;
524 for (i = 0 ; i < conf->raid_disks * 2 ; i++) {
525 sector_t dist;
526 sector_t first_bad;
527 int bad_sectors;
529 int disk = start_disk + i;
530 if (disk >= conf->raid_disks)
531 disk -= conf->raid_disks;
533 rdev = rcu_dereference(conf->mirrors[disk].rdev);
534 if (r1_bio->bios[disk] == IO_BLOCKED
535 || rdev == NULL
536 || test_bit(Unmerged, &rdev->flags)
537 || test_bit(Faulty, &rdev->flags))
538 continue;
539 if (!test_bit(In_sync, &rdev->flags) &&
540 rdev->recovery_offset < this_sector + sectors)
541 continue;
542 if (test_bit(WriteMostly, &rdev->flags)) {
543 /* Don't balance among write-mostly, just
544 * use the first as a last resort */
545 if (best_disk < 0) {
546 if (is_badblock(rdev, this_sector, sectors,
547 &first_bad, &bad_sectors)) {
548 if (first_bad < this_sector)
549 /* Cannot use this */
550 continue;
551 best_good_sectors = first_bad - this_sector;
552 } else
553 best_good_sectors = sectors;
554 best_disk = disk;
556 continue;
558 /* This is a reasonable device to use. It might
559 * even be best.
561 if (is_badblock(rdev, this_sector, sectors,
562 &first_bad, &bad_sectors)) {
563 if (best_dist < MaxSector)
564 /* already have a better device */
565 continue;
566 if (first_bad <= this_sector) {
567 /* cannot read here. If this is the 'primary'
568 * device, then we must not read beyond
569 * bad_sectors from another device..
571 bad_sectors -= (this_sector - first_bad);
572 if (choose_first && sectors > bad_sectors)
573 sectors = bad_sectors;
574 if (best_good_sectors > sectors)
575 best_good_sectors = sectors;
577 } else {
578 sector_t good_sectors = first_bad - this_sector;
579 if (good_sectors > best_good_sectors) {
580 best_good_sectors = good_sectors;
581 best_disk = disk;
583 if (choose_first)
584 break;
586 continue;
587 } else
588 best_good_sectors = sectors;
590 dist = abs(this_sector - conf->mirrors[disk].head_position);
591 if (choose_first
592 /* Don't change to another disk for sequential reads */
593 || conf->next_seq_sect == this_sector
594 || dist == 0
595 /* If device is idle, use it */
596 || atomic_read(&rdev->nr_pending) == 0) {
597 best_disk = disk;
598 break;
600 if (dist < best_dist) {
601 best_dist = dist;
602 best_disk = disk;
606 if (best_disk >= 0) {
607 rdev = rcu_dereference(conf->mirrors[best_disk].rdev);
608 if (!rdev)
609 goto retry;
610 atomic_inc(&rdev->nr_pending);
611 if (test_bit(Faulty, &rdev->flags)) {
612 /* cannot risk returning a device that failed
613 * before we inc'ed nr_pending
615 rdev_dec_pending(rdev, conf->mddev);
616 goto retry;
618 sectors = best_good_sectors;
619 conf->next_seq_sect = this_sector + sectors;
620 conf->last_used = best_disk;
622 rcu_read_unlock();
623 *max_sectors = sectors;
625 return best_disk;
628 static int raid1_mergeable_bvec(struct request_queue *q,
629 struct bvec_merge_data *bvm,
630 struct bio_vec *biovec)
632 struct mddev *mddev = q->queuedata;
633 struct r1conf *conf = mddev->private;
634 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
635 int max = biovec->bv_len;
637 if (mddev->merge_check_needed) {
638 int disk;
639 rcu_read_lock();
640 for (disk = 0; disk < conf->raid_disks * 2; disk++) {
641 struct md_rdev *rdev = rcu_dereference(
642 conf->mirrors[disk].rdev);
643 if (rdev && !test_bit(Faulty, &rdev->flags)) {
644 struct request_queue *q =
645 bdev_get_queue(rdev->bdev);
646 if (q->merge_bvec_fn) {
647 bvm->bi_sector = sector +
648 rdev->data_offset;
649 bvm->bi_bdev = rdev->bdev;
650 max = min(max, q->merge_bvec_fn(
651 q, bvm, biovec));
655 rcu_read_unlock();
657 return max;
661 int md_raid1_congested(struct mddev *mddev, int bits)
663 struct r1conf *conf = mddev->private;
664 int i, ret = 0;
666 if ((bits & (1 << BDI_async_congested)) &&
667 conf->pending_count >= max_queued_requests)
668 return 1;
670 rcu_read_lock();
671 for (i = 0; i < conf->raid_disks * 2; i++) {
672 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
673 if (rdev && !test_bit(Faulty, &rdev->flags)) {
674 struct request_queue *q = bdev_get_queue(rdev->bdev);
676 BUG_ON(!q);
678 /* Note the '|| 1' - when read_balance prefers
679 * non-congested targets, it can be removed
681 if ((bits & (1<<BDI_async_congested)) || 1)
682 ret |= bdi_congested(&q->backing_dev_info, bits);
683 else
684 ret &= bdi_congested(&q->backing_dev_info, bits);
687 rcu_read_unlock();
688 return ret;
690 EXPORT_SYMBOL_GPL(md_raid1_congested);
692 static int raid1_congested(void *data, int bits)
694 struct mddev *mddev = data;
696 return mddev_congested(mddev, bits) ||
697 md_raid1_congested(mddev, bits);
700 static void flush_pending_writes(struct r1conf *conf)
702 /* Any writes that have been queued but are awaiting
703 * bitmap updates get flushed here.
705 spin_lock_irq(&conf->device_lock);
707 if (conf->pending_bio_list.head) {
708 struct bio *bio;
709 bio = bio_list_get(&conf->pending_bio_list);
710 conf->pending_count = 0;
711 spin_unlock_irq(&conf->device_lock);
712 /* flush any pending bitmap writes to
713 * disk before proceeding w/ I/O */
714 bitmap_unplug(conf->mddev->bitmap);
715 wake_up(&conf->wait_barrier);
717 while (bio) { /* submit pending writes */
718 struct bio *next = bio->bi_next;
719 bio->bi_next = NULL;
720 generic_make_request(bio);
721 bio = next;
723 } else
724 spin_unlock_irq(&conf->device_lock);
727 /* Barriers....
728 * Sometimes we need to suspend IO while we do something else,
729 * either some resync/recovery, or reconfigure the array.
730 * To do this we raise a 'barrier'.
731 * The 'barrier' is a counter that can be raised multiple times
732 * to count how many activities are happening which preclude
733 * normal IO.
734 * We can only raise the barrier if there is no pending IO.
735 * i.e. if nr_pending == 0.
736 * We choose only to raise the barrier if no-one is waiting for the
737 * barrier to go down. This means that as soon as an IO request
738 * is ready, no other operations which require a barrier will start
739 * until the IO request has had a chance.
741 * So: regular IO calls 'wait_barrier'. When that returns there
742 * is no backgroup IO happening, It must arrange to call
743 * allow_barrier when it has finished its IO.
744 * backgroup IO calls must call raise_barrier. Once that returns
745 * there is no normal IO happeing. It must arrange to call
746 * lower_barrier when the particular background IO completes.
748 #define RESYNC_DEPTH 32
750 static void raise_barrier(struct r1conf *conf)
752 spin_lock_irq(&conf->resync_lock);
754 /* Wait until no block IO is waiting */
755 wait_event_lock_irq(conf->wait_barrier, !conf->nr_waiting,
756 conf->resync_lock, );
758 /* block any new IO from starting */
759 conf->barrier++;
761 /* Now wait for all pending IO to complete */
762 wait_event_lock_irq(conf->wait_barrier,
763 !conf->nr_pending && conf->barrier < RESYNC_DEPTH,
764 conf->resync_lock, );
766 spin_unlock_irq(&conf->resync_lock);
769 static void lower_barrier(struct r1conf *conf)
771 unsigned long flags;
772 BUG_ON(conf->barrier <= 0);
773 spin_lock_irqsave(&conf->resync_lock, flags);
774 conf->barrier--;
775 spin_unlock_irqrestore(&conf->resync_lock, flags);
776 wake_up(&conf->wait_barrier);
779 static void wait_barrier(struct r1conf *conf)
781 spin_lock_irq(&conf->resync_lock);
782 if (conf->barrier) {
783 conf->nr_waiting++;
784 /* Wait for the barrier to drop.
785 * However if there are already pending
786 * requests (preventing the barrier from
787 * rising completely), and the
788 * pre-process bio queue isn't empty,
789 * then don't wait, as we need to empty
790 * that queue to get the nr_pending
791 * count down.
793 wait_event_lock_irq(conf->wait_barrier,
794 !conf->barrier ||
795 (conf->nr_pending &&
796 current->bio_list &&
797 !bio_list_empty(current->bio_list)),
798 conf->resync_lock,
800 conf->nr_waiting--;
802 conf->nr_pending++;
803 spin_unlock_irq(&conf->resync_lock);
806 static void allow_barrier(struct r1conf *conf)
808 unsigned long flags;
809 spin_lock_irqsave(&conf->resync_lock, flags);
810 conf->nr_pending--;
811 spin_unlock_irqrestore(&conf->resync_lock, flags);
812 wake_up(&conf->wait_barrier);
815 static void freeze_array(struct r1conf *conf, int extra)
817 /* stop syncio and normal IO and wait for everything to
818 * go quite.
819 * We increment barrier and nr_waiting, and then
820 * wait until nr_pending match nr_queued+extra
821 * This is called in the context of one normal IO request
822 * that has failed. Thus any sync request that might be pending
823 * will be blocked by nr_pending, and we need to wait for
824 * pending IO requests to complete or be queued for re-try.
825 * Thus the number queued (nr_queued) plus this request (extra)
826 * must match the number of pending IOs (nr_pending) before
827 * we continue.
829 spin_lock_irq(&conf->resync_lock);
830 conf->barrier++;
831 conf->nr_waiting++;
832 wait_event_lock_irq(conf->wait_barrier,
833 conf->nr_pending == conf->nr_queued+extra,
834 conf->resync_lock,
835 flush_pending_writes(conf));
836 spin_unlock_irq(&conf->resync_lock);
838 static void unfreeze_array(struct r1conf *conf)
840 /* reverse the effect of the freeze */
841 spin_lock_irq(&conf->resync_lock);
842 conf->barrier--;
843 conf->nr_waiting--;
844 wake_up(&conf->wait_barrier);
845 spin_unlock_irq(&conf->resync_lock);
849 /* duplicate the data pages for behind I/O
851 static void alloc_behind_pages(struct bio *bio, struct r1bio *r1_bio)
853 int i;
854 struct bio_vec *bvec;
855 struct bio_vec *bvecs = kzalloc(bio->bi_vcnt * sizeof(struct bio_vec),
856 GFP_NOIO);
857 if (unlikely(!bvecs))
858 return;
860 bio_for_each_segment(bvec, bio, i) {
861 bvecs[i] = *bvec;
862 bvecs[i].bv_page = alloc_page(GFP_NOIO);
863 if (unlikely(!bvecs[i].bv_page))
864 goto do_sync_io;
865 memcpy(kmap(bvecs[i].bv_page) + bvec->bv_offset,
866 kmap(bvec->bv_page) + bvec->bv_offset, bvec->bv_len);
867 kunmap(bvecs[i].bv_page);
868 kunmap(bvec->bv_page);
870 r1_bio->behind_bvecs = bvecs;
871 r1_bio->behind_page_count = bio->bi_vcnt;
872 set_bit(R1BIO_BehindIO, &r1_bio->state);
873 return;
875 do_sync_io:
876 for (i = 0; i < bio->bi_vcnt; i++)
877 if (bvecs[i].bv_page)
878 put_page(bvecs[i].bv_page);
879 kfree(bvecs);
880 pr_debug("%dB behind alloc failed, doing sync I/O\n", bio->bi_size);
883 static void make_request(struct mddev *mddev, struct bio * bio)
885 struct r1conf *conf = mddev->private;
886 struct mirror_info *mirror;
887 struct r1bio *r1_bio;
888 struct bio *read_bio;
889 int i, disks;
890 struct bitmap *bitmap;
891 unsigned long flags;
892 const int rw = bio_data_dir(bio);
893 const unsigned long do_sync = (bio->bi_rw & REQ_SYNC);
894 const unsigned long do_flush_fua = (bio->bi_rw & (REQ_FLUSH | REQ_FUA));
895 struct md_rdev *blocked_rdev;
896 int plugged;
897 int first_clone;
898 int sectors_handled;
899 int max_sectors;
902 * Register the new request and wait if the reconstruction
903 * thread has put up a bar for new requests.
904 * Continue immediately if no resync is active currently.
907 md_write_start(mddev, bio); /* wait on superblock update early */
909 if (bio_data_dir(bio) == WRITE &&
910 bio->bi_sector + bio->bi_size/512 > mddev->suspend_lo &&
911 bio->bi_sector < mddev->suspend_hi) {
912 /* As the suspend_* range is controlled by
913 * userspace, we want an interruptible
914 * wait.
916 DEFINE_WAIT(w);
917 for (;;) {
918 flush_signals(current);
919 prepare_to_wait(&conf->wait_barrier,
920 &w, TASK_INTERRUPTIBLE);
921 if (bio->bi_sector + bio->bi_size/512 <= mddev->suspend_lo ||
922 bio->bi_sector >= mddev->suspend_hi)
923 break;
924 schedule();
926 finish_wait(&conf->wait_barrier, &w);
929 wait_barrier(conf);
931 bitmap = mddev->bitmap;
934 * make_request() can abort the operation when READA is being
935 * used and no empty request is available.
938 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
940 r1_bio->master_bio = bio;
941 r1_bio->sectors = bio->bi_size >> 9;
942 r1_bio->state = 0;
943 r1_bio->mddev = mddev;
944 r1_bio->sector = bio->bi_sector;
946 /* We might need to issue multiple reads to different
947 * devices if there are bad blocks around, so we keep
948 * track of the number of reads in bio->bi_phys_segments.
949 * If this is 0, there is only one r1_bio and no locking
950 * will be needed when requests complete. If it is
951 * non-zero, then it is the number of not-completed requests.
953 bio->bi_phys_segments = 0;
954 clear_bit(BIO_SEG_VALID, &bio->bi_flags);
956 if (rw == READ) {
958 * read balancing logic:
960 int rdisk;
962 read_again:
963 rdisk = read_balance(conf, r1_bio, &max_sectors);
965 if (rdisk < 0) {
966 /* couldn't find anywhere to read from */
967 raid_end_bio_io(r1_bio);
968 return;
970 mirror = conf->mirrors + rdisk;
972 if (test_bit(WriteMostly, &mirror->rdev->flags) &&
973 bitmap) {
974 /* Reading from a write-mostly device must
975 * take care not to over-take any writes
976 * that are 'behind'
978 wait_event(bitmap->behind_wait,
979 atomic_read(&bitmap->behind_writes) == 0);
981 r1_bio->read_disk = rdisk;
983 read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev);
984 md_trim_bio(read_bio, r1_bio->sector - bio->bi_sector,
985 max_sectors);
987 r1_bio->bios[rdisk] = read_bio;
989 read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset;
990 read_bio->bi_bdev = mirror->rdev->bdev;
991 read_bio->bi_end_io = raid1_end_read_request;
992 read_bio->bi_rw = READ | do_sync;
993 read_bio->bi_private = r1_bio;
995 if (max_sectors < r1_bio->sectors) {
996 /* could not read all from this device, so we will
997 * need another r1_bio.
1000 sectors_handled = (r1_bio->sector + max_sectors
1001 - bio->bi_sector);
1002 r1_bio->sectors = max_sectors;
1003 spin_lock_irq(&conf->device_lock);
1004 if (bio->bi_phys_segments == 0)
1005 bio->bi_phys_segments = 2;
1006 else
1007 bio->bi_phys_segments++;
1008 spin_unlock_irq(&conf->device_lock);
1009 /* Cannot call generic_make_request directly
1010 * as that will be queued in __make_request
1011 * and subsequent mempool_alloc might block waiting
1012 * for it. So hand bio over to raid1d.
1014 reschedule_retry(r1_bio);
1016 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1018 r1_bio->master_bio = bio;
1019 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1020 r1_bio->state = 0;
1021 r1_bio->mddev = mddev;
1022 r1_bio->sector = bio->bi_sector + sectors_handled;
1023 goto read_again;
1024 } else
1025 generic_make_request(read_bio);
1026 return;
1030 * WRITE:
1032 if (conf->pending_count >= max_queued_requests) {
1033 md_wakeup_thread(mddev->thread);
1034 wait_event(conf->wait_barrier,
1035 conf->pending_count < max_queued_requests);
1037 /* first select target devices under rcu_lock and
1038 * inc refcount on their rdev. Record them by setting
1039 * bios[x] to bio
1040 * If there are known/acknowledged bad blocks on any device on
1041 * which we have seen a write error, we want to avoid writing those
1042 * blocks.
1043 * This potentially requires several writes to write around
1044 * the bad blocks. Each set of writes gets it's own r1bio
1045 * with a set of bios attached.
1047 plugged = mddev_check_plugged(mddev);
1049 disks = conf->raid_disks * 2;
1050 retry_write:
1051 blocked_rdev = NULL;
1052 rcu_read_lock();
1053 max_sectors = r1_bio->sectors;
1054 for (i = 0; i < disks; i++) {
1055 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1056 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1057 atomic_inc(&rdev->nr_pending);
1058 blocked_rdev = rdev;
1059 break;
1061 r1_bio->bios[i] = NULL;
1062 if (!rdev || test_bit(Faulty, &rdev->flags)
1063 || test_bit(Unmerged, &rdev->flags)) {
1064 if (i < conf->raid_disks)
1065 set_bit(R1BIO_Degraded, &r1_bio->state);
1066 continue;
1069 atomic_inc(&rdev->nr_pending);
1070 if (test_bit(WriteErrorSeen, &rdev->flags)) {
1071 sector_t first_bad;
1072 int bad_sectors;
1073 int is_bad;
1075 is_bad = is_badblock(rdev, r1_bio->sector,
1076 max_sectors,
1077 &first_bad, &bad_sectors);
1078 if (is_bad < 0) {
1079 /* mustn't write here until the bad block is
1080 * acknowledged*/
1081 set_bit(BlockedBadBlocks, &rdev->flags);
1082 blocked_rdev = rdev;
1083 break;
1085 if (is_bad && first_bad <= r1_bio->sector) {
1086 /* Cannot write here at all */
1087 bad_sectors -= (r1_bio->sector - first_bad);
1088 if (bad_sectors < max_sectors)
1089 /* mustn't write more than bad_sectors
1090 * to other devices yet
1092 max_sectors = bad_sectors;
1093 rdev_dec_pending(rdev, mddev);
1094 /* We don't set R1BIO_Degraded as that
1095 * only applies if the disk is
1096 * missing, so it might be re-added,
1097 * and we want to know to recover this
1098 * chunk.
1099 * In this case the device is here,
1100 * and the fact that this chunk is not
1101 * in-sync is recorded in the bad
1102 * block log
1104 continue;
1106 if (is_bad) {
1107 int good_sectors = first_bad - r1_bio->sector;
1108 if (good_sectors < max_sectors)
1109 max_sectors = good_sectors;
1112 r1_bio->bios[i] = bio;
1114 rcu_read_unlock();
1116 if (unlikely(blocked_rdev)) {
1117 /* Wait for this device to become unblocked */
1118 int j;
1120 for (j = 0; j < i; j++)
1121 if (r1_bio->bios[j])
1122 rdev_dec_pending(conf->mirrors[j].rdev, mddev);
1123 r1_bio->state = 0;
1124 allow_barrier(conf);
1125 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1126 wait_barrier(conf);
1127 goto retry_write;
1130 if (max_sectors < r1_bio->sectors) {
1131 /* We are splitting this write into multiple parts, so
1132 * we need to prepare for allocating another r1_bio.
1134 r1_bio->sectors = max_sectors;
1135 spin_lock_irq(&conf->device_lock);
1136 if (bio->bi_phys_segments == 0)
1137 bio->bi_phys_segments = 2;
1138 else
1139 bio->bi_phys_segments++;
1140 spin_unlock_irq(&conf->device_lock);
1142 sectors_handled = r1_bio->sector + max_sectors - bio->bi_sector;
1144 atomic_set(&r1_bio->remaining, 1);
1145 atomic_set(&r1_bio->behind_remaining, 0);
1147 first_clone = 1;
1148 for (i = 0; i < disks; i++) {
1149 struct bio *mbio;
1150 if (!r1_bio->bios[i])
1151 continue;
1153 mbio = bio_clone_mddev(bio, GFP_NOIO, mddev);
1154 md_trim_bio(mbio, r1_bio->sector - bio->bi_sector, max_sectors);
1156 if (first_clone) {
1157 /* do behind I/O ?
1158 * Not if there are too many, or cannot
1159 * allocate memory, or a reader on WriteMostly
1160 * is waiting for behind writes to flush */
1161 if (bitmap &&
1162 (atomic_read(&bitmap->behind_writes)
1163 < mddev->bitmap_info.max_write_behind) &&
1164 !waitqueue_active(&bitmap->behind_wait))
1165 alloc_behind_pages(mbio, r1_bio);
1167 bitmap_startwrite(bitmap, r1_bio->sector,
1168 r1_bio->sectors,
1169 test_bit(R1BIO_BehindIO,
1170 &r1_bio->state));
1171 first_clone = 0;
1173 if (r1_bio->behind_bvecs) {
1174 struct bio_vec *bvec;
1175 int j;
1177 /* Yes, I really want the '__' version so that
1178 * we clear any unused pointer in the io_vec, rather
1179 * than leave them unchanged. This is important
1180 * because when we come to free the pages, we won't
1181 * know the original bi_idx, so we just free
1182 * them all
1184 __bio_for_each_segment(bvec, mbio, j, 0)
1185 bvec->bv_page = r1_bio->behind_bvecs[j].bv_page;
1186 if (test_bit(WriteMostly, &conf->mirrors[i].rdev->flags))
1187 atomic_inc(&r1_bio->behind_remaining);
1190 r1_bio->bios[i] = mbio;
1192 mbio->bi_sector = (r1_bio->sector +
1193 conf->mirrors[i].rdev->data_offset);
1194 mbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1195 mbio->bi_end_io = raid1_end_write_request;
1196 mbio->bi_rw = WRITE | do_flush_fua | do_sync;
1197 mbio->bi_private = r1_bio;
1199 atomic_inc(&r1_bio->remaining);
1200 spin_lock_irqsave(&conf->device_lock, flags);
1201 bio_list_add(&conf->pending_bio_list, mbio);
1202 conf->pending_count++;
1203 spin_unlock_irqrestore(&conf->device_lock, flags);
1205 /* Mustn't call r1_bio_write_done before this next test,
1206 * as it could result in the bio being freed.
1208 if (sectors_handled < (bio->bi_size >> 9)) {
1209 r1_bio_write_done(r1_bio);
1210 /* We need another r1_bio. It has already been counted
1211 * in bio->bi_phys_segments
1213 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
1214 r1_bio->master_bio = bio;
1215 r1_bio->sectors = (bio->bi_size >> 9) - sectors_handled;
1216 r1_bio->state = 0;
1217 r1_bio->mddev = mddev;
1218 r1_bio->sector = bio->bi_sector + sectors_handled;
1219 goto retry_write;
1222 r1_bio_write_done(r1_bio);
1224 /* In case raid1d snuck in to freeze_array */
1225 wake_up(&conf->wait_barrier);
1227 if (do_sync || !bitmap || !plugged)
1228 md_wakeup_thread(mddev->thread);
1231 static void status(struct seq_file *seq, struct mddev *mddev)
1233 struct r1conf *conf = mddev->private;
1234 int i;
1236 seq_printf(seq, " [%d/%d] [", conf->raid_disks,
1237 conf->raid_disks - mddev->degraded);
1238 rcu_read_lock();
1239 for (i = 0; i < conf->raid_disks; i++) {
1240 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1241 seq_printf(seq, "%s",
1242 rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1244 rcu_read_unlock();
1245 seq_printf(seq, "]");
1249 static void error(struct mddev *mddev, struct md_rdev *rdev)
1251 char b[BDEVNAME_SIZE];
1252 struct r1conf *conf = mddev->private;
1255 * If it is not operational, then we have already marked it as dead
1256 * else if it is the last working disks, ignore the error, let the
1257 * next level up know.
1258 * else mark the drive as failed
1260 if (test_bit(In_sync, &rdev->flags)
1261 && (conf->raid_disks - mddev->degraded) == 1) {
1263 * Don't fail the drive, act as though we were just a
1264 * normal single drive.
1265 * However don't try a recovery from this drive as
1266 * it is very likely to fail.
1268 conf->recovery_disabled = mddev->recovery_disabled;
1269 return;
1271 set_bit(Blocked, &rdev->flags);
1272 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1273 unsigned long flags;
1274 spin_lock_irqsave(&conf->device_lock, flags);
1275 mddev->degraded++;
1276 set_bit(Faulty, &rdev->flags);
1277 spin_unlock_irqrestore(&conf->device_lock, flags);
1279 * if recovery is running, make sure it aborts.
1281 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1282 } else
1283 set_bit(Faulty, &rdev->flags);
1284 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1285 printk(KERN_ALERT
1286 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1287 "md/raid1:%s: Operation continuing on %d devices.\n",
1288 mdname(mddev), bdevname(rdev->bdev, b),
1289 mdname(mddev), conf->raid_disks - mddev->degraded);
1292 static void print_conf(struct r1conf *conf)
1294 int i;
1296 printk(KERN_DEBUG "RAID1 conf printout:\n");
1297 if (!conf) {
1298 printk(KERN_DEBUG "(!conf)\n");
1299 return;
1301 printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded,
1302 conf->raid_disks);
1304 rcu_read_lock();
1305 for (i = 0; i < conf->raid_disks; i++) {
1306 char b[BDEVNAME_SIZE];
1307 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1308 if (rdev)
1309 printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n",
1310 i, !test_bit(In_sync, &rdev->flags),
1311 !test_bit(Faulty, &rdev->flags),
1312 bdevname(rdev->bdev,b));
1314 rcu_read_unlock();
1317 static void close_sync(struct r1conf *conf)
1319 wait_barrier(conf);
1320 allow_barrier(conf);
1322 mempool_destroy(conf->r1buf_pool);
1323 conf->r1buf_pool = NULL;
1326 static int raid1_spare_active(struct mddev *mddev)
1328 int i;
1329 struct r1conf *conf = mddev->private;
1330 int count = 0;
1331 unsigned long flags;
1334 * Find all failed disks within the RAID1 configuration
1335 * and mark them readable.
1336 * Called under mddev lock, so rcu protection not needed.
1338 for (i = 0; i < conf->raid_disks; i++) {
1339 struct md_rdev *rdev = conf->mirrors[i].rdev;
1340 struct md_rdev *repl = conf->mirrors[conf->raid_disks + i].rdev;
1341 if (repl
1342 && repl->recovery_offset == MaxSector
1343 && !test_bit(Faulty, &repl->flags)
1344 && !test_and_set_bit(In_sync, &repl->flags)) {
1345 /* replacement has just become active */
1346 if (!rdev ||
1347 !test_and_clear_bit(In_sync, &rdev->flags))
1348 count++;
1349 if (rdev) {
1350 /* Replaced device not technically
1351 * faulty, but we need to be sure
1352 * it gets removed and never re-added
1354 set_bit(Faulty, &rdev->flags);
1355 sysfs_notify_dirent_safe(
1356 rdev->sysfs_state);
1359 if (rdev
1360 && rdev->recovery_offset == MaxSector
1361 && !test_bit(Faulty, &rdev->flags)
1362 && !test_and_set_bit(In_sync, &rdev->flags)) {
1363 count++;
1364 sysfs_notify_dirent_safe(rdev->sysfs_state);
1367 spin_lock_irqsave(&conf->device_lock, flags);
1368 mddev->degraded -= count;
1369 spin_unlock_irqrestore(&conf->device_lock, flags);
1371 print_conf(conf);
1372 return count;
1376 static int raid1_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1378 struct r1conf *conf = mddev->private;
1379 int err = -EEXIST;
1380 int mirror = 0;
1381 struct mirror_info *p;
1382 int first = 0;
1383 int last = conf->raid_disks - 1;
1384 struct request_queue *q = bdev_get_queue(rdev->bdev);
1386 if (mddev->recovery_disabled == conf->recovery_disabled)
1387 return -EBUSY;
1389 if (rdev->raid_disk >= 0)
1390 first = last = rdev->raid_disk;
1392 if (q->merge_bvec_fn) {
1393 set_bit(Unmerged, &rdev->flags);
1394 mddev->merge_check_needed = 1;
1397 for (mirror = first; mirror <= last; mirror++) {
1398 p = conf->mirrors+mirror;
1399 if (!p->rdev) {
1401 disk_stack_limits(mddev->gendisk, rdev->bdev,
1402 rdev->data_offset << 9);
1404 p->head_position = 0;
1405 rdev->raid_disk = mirror;
1406 err = 0;
1407 /* As all devices are equivalent, we don't need a full recovery
1408 * if this was recently any drive of the array
1410 if (rdev->saved_raid_disk < 0)
1411 conf->fullsync = 1;
1412 rcu_assign_pointer(p->rdev, rdev);
1413 break;
1415 if (test_bit(WantReplacement, &p->rdev->flags) &&
1416 p[conf->raid_disks].rdev == NULL) {
1417 /* Add this device as a replacement */
1418 clear_bit(In_sync, &rdev->flags);
1419 set_bit(Replacement, &rdev->flags);
1420 rdev->raid_disk = mirror;
1421 err = 0;
1422 conf->fullsync = 1;
1423 rcu_assign_pointer(p[conf->raid_disks].rdev, rdev);
1424 break;
1427 if (err == 0 && test_bit(Unmerged, &rdev->flags)) {
1428 /* Some requests might not have seen this new
1429 * merge_bvec_fn. We must wait for them to complete
1430 * before merging the device fully.
1431 * First we make sure any code which has tested
1432 * our function has submitted the request, then
1433 * we wait for all outstanding requests to complete.
1435 synchronize_sched();
1436 freeze_array(conf, 0);
1437 unfreeze_array(conf);
1438 clear_bit(Unmerged, &rdev->flags);
1440 md_integrity_add_rdev(rdev, mddev);
1441 print_conf(conf);
1442 return err;
1445 static int raid1_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1447 struct r1conf *conf = mddev->private;
1448 int err = 0;
1449 int number = rdev->raid_disk;
1450 struct mirror_info *p = conf->mirrors+ number;
1452 if (rdev != p->rdev)
1453 p = conf->mirrors + conf->raid_disks + number;
1455 print_conf(conf);
1456 if (rdev == p->rdev) {
1457 if (test_bit(In_sync, &rdev->flags) ||
1458 atomic_read(&rdev->nr_pending)) {
1459 err = -EBUSY;
1460 goto abort;
1462 /* Only remove non-faulty devices if recovery
1463 * is not possible.
1465 if (!test_bit(Faulty, &rdev->flags) &&
1466 mddev->recovery_disabled != conf->recovery_disabled &&
1467 mddev->degraded < conf->raid_disks) {
1468 err = -EBUSY;
1469 goto abort;
1471 p->rdev = NULL;
1472 synchronize_rcu();
1473 if (atomic_read(&rdev->nr_pending)) {
1474 /* lost the race, try later */
1475 err = -EBUSY;
1476 p->rdev = rdev;
1477 goto abort;
1478 } else if (conf->mirrors[conf->raid_disks + number].rdev) {
1479 /* We just removed a device that is being replaced.
1480 * Move down the replacement. We drain all IO before
1481 * doing this to avoid confusion.
1483 struct md_rdev *repl =
1484 conf->mirrors[conf->raid_disks + number].rdev;
1485 freeze_array(conf, 0);
1486 clear_bit(Replacement, &repl->flags);
1487 p->rdev = repl;
1488 conf->mirrors[conf->raid_disks + number].rdev = NULL;
1489 unfreeze_array(conf);
1490 clear_bit(WantReplacement, &rdev->flags);
1491 } else
1492 clear_bit(WantReplacement, &rdev->flags);
1493 err = md_integrity_register(mddev);
1495 abort:
1497 print_conf(conf);
1498 return err;
1502 static void end_sync_read(struct bio *bio, int error)
1504 struct r1bio *r1_bio = bio->bi_private;
1506 update_head_pos(r1_bio->read_disk, r1_bio);
1509 * we have read a block, now it needs to be re-written,
1510 * or re-read if the read failed.
1511 * We don't do much here, just schedule handling by raid1d
1513 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
1514 set_bit(R1BIO_Uptodate, &r1_bio->state);
1516 if (atomic_dec_and_test(&r1_bio->remaining))
1517 reschedule_retry(r1_bio);
1520 static void end_sync_write(struct bio *bio, int error)
1522 int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags);
1523 struct r1bio *r1_bio = bio->bi_private;
1524 struct mddev *mddev = r1_bio->mddev;
1525 struct r1conf *conf = mddev->private;
1526 int mirror=0;
1527 sector_t first_bad;
1528 int bad_sectors;
1530 mirror = find_bio_disk(r1_bio, bio);
1532 if (!uptodate) {
1533 sector_t sync_blocks = 0;
1534 sector_t s = r1_bio->sector;
1535 long sectors_to_go = r1_bio->sectors;
1536 /* make sure these bits doesn't get cleared. */
1537 do {
1538 bitmap_end_sync(mddev->bitmap, s,
1539 &sync_blocks, 1);
1540 s += sync_blocks;
1541 sectors_to_go -= sync_blocks;
1542 } while (sectors_to_go > 0);
1543 set_bit(WriteErrorSeen,
1544 &conf->mirrors[mirror].rdev->flags);
1545 if (!test_and_set_bit(WantReplacement,
1546 &conf->mirrors[mirror].rdev->flags))
1547 set_bit(MD_RECOVERY_NEEDED, &
1548 mddev->recovery);
1549 set_bit(R1BIO_WriteError, &r1_bio->state);
1550 } else if (is_badblock(conf->mirrors[mirror].rdev,
1551 r1_bio->sector,
1552 r1_bio->sectors,
1553 &first_bad, &bad_sectors) &&
1554 !is_badblock(conf->mirrors[r1_bio->read_disk].rdev,
1555 r1_bio->sector,
1556 r1_bio->sectors,
1557 &first_bad, &bad_sectors)
1559 set_bit(R1BIO_MadeGood, &r1_bio->state);
1561 if (atomic_dec_and_test(&r1_bio->remaining)) {
1562 int s = r1_bio->sectors;
1563 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1564 test_bit(R1BIO_WriteError, &r1_bio->state))
1565 reschedule_retry(r1_bio);
1566 else {
1567 put_buf(r1_bio);
1568 md_done_sync(mddev, s, uptodate);
1573 static int r1_sync_page_io(struct md_rdev *rdev, sector_t sector,
1574 int sectors, struct page *page, int rw)
1576 if (sync_page_io(rdev, sector, sectors << 9, page, rw, false))
1577 /* success */
1578 return 1;
1579 if (rw == WRITE) {
1580 set_bit(WriteErrorSeen, &rdev->flags);
1581 if (!test_and_set_bit(WantReplacement,
1582 &rdev->flags))
1583 set_bit(MD_RECOVERY_NEEDED, &
1584 rdev->mddev->recovery);
1586 /* need to record an error - either for the block or the device */
1587 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
1588 md_error(rdev->mddev, rdev);
1589 return 0;
1592 static int fix_sync_read_error(struct r1bio *r1_bio)
1594 /* Try some synchronous reads of other devices to get
1595 * good data, much like with normal read errors. Only
1596 * read into the pages we already have so we don't
1597 * need to re-issue the read request.
1598 * We don't need to freeze the array, because being in an
1599 * active sync request, there is no normal IO, and
1600 * no overlapping syncs.
1601 * We don't need to check is_badblock() again as we
1602 * made sure that anything with a bad block in range
1603 * will have bi_end_io clear.
1605 struct mddev *mddev = r1_bio->mddev;
1606 struct r1conf *conf = mddev->private;
1607 struct bio *bio = r1_bio->bios[r1_bio->read_disk];
1608 sector_t sect = r1_bio->sector;
1609 int sectors = r1_bio->sectors;
1610 int idx = 0;
1612 while(sectors) {
1613 int s = sectors;
1614 int d = r1_bio->read_disk;
1615 int success = 0;
1616 struct md_rdev *rdev;
1617 int start;
1619 if (s > (PAGE_SIZE>>9))
1620 s = PAGE_SIZE >> 9;
1621 do {
1622 if (r1_bio->bios[d]->bi_end_io == end_sync_read) {
1623 /* No rcu protection needed here devices
1624 * can only be removed when no resync is
1625 * active, and resync is currently active
1627 rdev = conf->mirrors[d].rdev;
1628 if (sync_page_io(rdev, sect, s<<9,
1629 bio->bi_io_vec[idx].bv_page,
1630 READ, false)) {
1631 success = 1;
1632 break;
1635 d++;
1636 if (d == conf->raid_disks * 2)
1637 d = 0;
1638 } while (!success && d != r1_bio->read_disk);
1640 if (!success) {
1641 char b[BDEVNAME_SIZE];
1642 int abort = 0;
1643 /* Cannot read from anywhere, this block is lost.
1644 * Record a bad block on each device. If that doesn't
1645 * work just disable and interrupt the recovery.
1646 * Don't fail devices as that won't really help.
1648 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O read error"
1649 " for block %llu\n",
1650 mdname(mddev),
1651 bdevname(bio->bi_bdev, b),
1652 (unsigned long long)r1_bio->sector);
1653 for (d = 0; d < conf->raid_disks * 2; d++) {
1654 rdev = conf->mirrors[d].rdev;
1655 if (!rdev || test_bit(Faulty, &rdev->flags))
1656 continue;
1657 if (!rdev_set_badblocks(rdev, sect, s, 0))
1658 abort = 1;
1660 if (abort) {
1661 conf->recovery_disabled =
1662 mddev->recovery_disabled;
1663 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1664 md_done_sync(mddev, r1_bio->sectors, 0);
1665 put_buf(r1_bio);
1666 return 0;
1668 /* Try next page */
1669 sectors -= s;
1670 sect += s;
1671 idx++;
1672 continue;
1675 start = d;
1676 /* write it back and re-read */
1677 while (d != r1_bio->read_disk) {
1678 if (d == 0)
1679 d = conf->raid_disks * 2;
1680 d--;
1681 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1682 continue;
1683 rdev = conf->mirrors[d].rdev;
1684 if (r1_sync_page_io(rdev, sect, s,
1685 bio->bi_io_vec[idx].bv_page,
1686 WRITE) == 0) {
1687 r1_bio->bios[d]->bi_end_io = NULL;
1688 rdev_dec_pending(rdev, mddev);
1691 d = start;
1692 while (d != r1_bio->read_disk) {
1693 if (d == 0)
1694 d = conf->raid_disks * 2;
1695 d--;
1696 if (r1_bio->bios[d]->bi_end_io != end_sync_read)
1697 continue;
1698 rdev = conf->mirrors[d].rdev;
1699 if (r1_sync_page_io(rdev, sect, s,
1700 bio->bi_io_vec[idx].bv_page,
1701 READ) != 0)
1702 atomic_add(s, &rdev->corrected_errors);
1704 sectors -= s;
1705 sect += s;
1706 idx ++;
1708 set_bit(R1BIO_Uptodate, &r1_bio->state);
1709 set_bit(BIO_UPTODATE, &bio->bi_flags);
1710 return 1;
1713 static int process_checks(struct r1bio *r1_bio)
1715 /* We have read all readable devices. If we haven't
1716 * got the block, then there is no hope left.
1717 * If we have, then we want to do a comparison
1718 * and skip the write if everything is the same.
1719 * If any blocks failed to read, then we need to
1720 * attempt an over-write
1722 struct mddev *mddev = r1_bio->mddev;
1723 struct r1conf *conf = mddev->private;
1724 int primary;
1725 int i;
1726 int vcnt;
1728 for (primary = 0; primary < conf->raid_disks * 2; primary++)
1729 if (r1_bio->bios[primary]->bi_end_io == end_sync_read &&
1730 test_bit(BIO_UPTODATE, &r1_bio->bios[primary]->bi_flags)) {
1731 r1_bio->bios[primary]->bi_end_io = NULL;
1732 rdev_dec_pending(conf->mirrors[primary].rdev, mddev);
1733 break;
1735 r1_bio->read_disk = primary;
1736 vcnt = (r1_bio->sectors + PAGE_SIZE / 512 - 1) >> (PAGE_SHIFT - 9);
1737 for (i = 0; i < conf->raid_disks * 2; i++) {
1738 int j;
1739 struct bio *pbio = r1_bio->bios[primary];
1740 struct bio *sbio = r1_bio->bios[i];
1741 int size;
1743 if (r1_bio->bios[i]->bi_end_io != end_sync_read)
1744 continue;
1746 if (test_bit(BIO_UPTODATE, &sbio->bi_flags)) {
1747 for (j = vcnt; j-- ; ) {
1748 struct page *p, *s;
1749 p = pbio->bi_io_vec[j].bv_page;
1750 s = sbio->bi_io_vec[j].bv_page;
1751 if (memcmp(page_address(p),
1752 page_address(s),
1753 sbio->bi_io_vec[j].bv_len))
1754 break;
1756 } else
1757 j = 0;
1758 if (j >= 0)
1759 mddev->resync_mismatches += r1_bio->sectors;
1760 if (j < 0 || (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)
1761 && test_bit(BIO_UPTODATE, &sbio->bi_flags))) {
1762 /* No need to write to this device. */
1763 sbio->bi_end_io = NULL;
1764 rdev_dec_pending(conf->mirrors[i].rdev, mddev);
1765 continue;
1767 /* fixup the bio for reuse */
1768 sbio->bi_vcnt = vcnt;
1769 sbio->bi_size = r1_bio->sectors << 9;
1770 sbio->bi_idx = 0;
1771 sbio->bi_phys_segments = 0;
1772 sbio->bi_flags &= ~(BIO_POOL_MASK - 1);
1773 sbio->bi_flags |= 1 << BIO_UPTODATE;
1774 sbio->bi_next = NULL;
1775 sbio->bi_sector = r1_bio->sector +
1776 conf->mirrors[i].rdev->data_offset;
1777 sbio->bi_bdev = conf->mirrors[i].rdev->bdev;
1778 size = sbio->bi_size;
1779 for (j = 0; j < vcnt ; j++) {
1780 struct bio_vec *bi;
1781 bi = &sbio->bi_io_vec[j];
1782 bi->bv_offset = 0;
1783 if (size > PAGE_SIZE)
1784 bi->bv_len = PAGE_SIZE;
1785 else
1786 bi->bv_len = size;
1787 size -= PAGE_SIZE;
1788 memcpy(page_address(bi->bv_page),
1789 page_address(pbio->bi_io_vec[j].bv_page),
1790 PAGE_SIZE);
1793 return 0;
1796 static void sync_request_write(struct mddev *mddev, struct r1bio *r1_bio)
1798 struct r1conf *conf = mddev->private;
1799 int i;
1800 int disks = conf->raid_disks * 2;
1801 struct bio *bio, *wbio;
1803 bio = r1_bio->bios[r1_bio->read_disk];
1805 if (!test_bit(R1BIO_Uptodate, &r1_bio->state))
1806 /* ouch - failed to read all of that. */
1807 if (!fix_sync_read_error(r1_bio))
1808 return;
1810 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
1811 if (process_checks(r1_bio) < 0)
1812 return;
1814 * schedule writes
1816 atomic_set(&r1_bio->remaining, 1);
1817 for (i = 0; i < disks ; i++) {
1818 wbio = r1_bio->bios[i];
1819 if (wbio->bi_end_io == NULL ||
1820 (wbio->bi_end_io == end_sync_read &&
1821 (i == r1_bio->read_disk ||
1822 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery))))
1823 continue;
1825 wbio->bi_rw = WRITE;
1826 wbio->bi_end_io = end_sync_write;
1827 atomic_inc(&r1_bio->remaining);
1828 md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9);
1830 generic_make_request(wbio);
1833 if (atomic_dec_and_test(&r1_bio->remaining)) {
1834 /* if we're here, all write(s) have completed, so clean up */
1835 int s = r1_bio->sectors;
1836 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
1837 test_bit(R1BIO_WriteError, &r1_bio->state))
1838 reschedule_retry(r1_bio);
1839 else {
1840 put_buf(r1_bio);
1841 md_done_sync(mddev, s, 1);
1847 * This is a kernel thread which:
1849 * 1. Retries failed read operations on working mirrors.
1850 * 2. Updates the raid superblock when problems encounter.
1851 * 3. Performs writes following reads for array synchronising.
1854 static void fix_read_error(struct r1conf *conf, int read_disk,
1855 sector_t sect, int sectors)
1857 struct mddev *mddev = conf->mddev;
1858 while(sectors) {
1859 int s = sectors;
1860 int d = read_disk;
1861 int success = 0;
1862 int start;
1863 struct md_rdev *rdev;
1865 if (s > (PAGE_SIZE>>9))
1866 s = PAGE_SIZE >> 9;
1868 do {
1869 /* Note: no rcu protection needed here
1870 * as this is synchronous in the raid1d thread
1871 * which is the thread that might remove
1872 * a device. If raid1d ever becomes multi-threaded....
1874 sector_t first_bad;
1875 int bad_sectors;
1877 rdev = conf->mirrors[d].rdev;
1878 if (rdev &&
1879 test_bit(In_sync, &rdev->flags) &&
1880 is_badblock(rdev, sect, s,
1881 &first_bad, &bad_sectors) == 0 &&
1882 sync_page_io(rdev, sect, s<<9,
1883 conf->tmppage, READ, false))
1884 success = 1;
1885 else {
1886 d++;
1887 if (d == conf->raid_disks * 2)
1888 d = 0;
1890 } while (!success && d != read_disk);
1892 if (!success) {
1893 /* Cannot read from anywhere - mark it bad */
1894 struct md_rdev *rdev = conf->mirrors[read_disk].rdev;
1895 if (!rdev_set_badblocks(rdev, sect, s, 0))
1896 md_error(mddev, rdev);
1897 break;
1899 /* write it back and re-read */
1900 start = d;
1901 while (d != read_disk) {
1902 if (d==0)
1903 d = conf->raid_disks * 2;
1904 d--;
1905 rdev = conf->mirrors[d].rdev;
1906 if (rdev &&
1907 test_bit(In_sync, &rdev->flags))
1908 r1_sync_page_io(rdev, sect, s,
1909 conf->tmppage, WRITE);
1911 d = start;
1912 while (d != read_disk) {
1913 char b[BDEVNAME_SIZE];
1914 if (d==0)
1915 d = conf->raid_disks * 2;
1916 d--;
1917 rdev = conf->mirrors[d].rdev;
1918 if (rdev &&
1919 test_bit(In_sync, &rdev->flags)) {
1920 if (r1_sync_page_io(rdev, sect, s,
1921 conf->tmppage, READ)) {
1922 atomic_add(s, &rdev->corrected_errors);
1923 printk(KERN_INFO
1924 "md/raid1:%s: read error corrected "
1925 "(%d sectors at %llu on %s)\n",
1926 mdname(mddev), s,
1927 (unsigned long long)(sect +
1928 rdev->data_offset),
1929 bdevname(rdev->bdev, b));
1933 sectors -= s;
1934 sect += s;
1938 static void bi_complete(struct bio *bio, int error)
1940 complete((struct completion *)bio->bi_private);
1943 static int submit_bio_wait(int rw, struct bio *bio)
1945 struct completion event;
1946 rw |= REQ_SYNC;
1948 init_completion(&event);
1949 bio->bi_private = &event;
1950 bio->bi_end_io = bi_complete;
1951 submit_bio(rw, bio);
1952 wait_for_completion(&event);
1954 return test_bit(BIO_UPTODATE, &bio->bi_flags);
1957 static int narrow_write_error(struct r1bio *r1_bio, int i)
1959 struct mddev *mddev = r1_bio->mddev;
1960 struct r1conf *conf = mddev->private;
1961 struct md_rdev *rdev = conf->mirrors[i].rdev;
1962 int vcnt, idx;
1963 struct bio_vec *vec;
1965 /* bio has the data to be written to device 'i' where
1966 * we just recently had a write error.
1967 * We repeatedly clone the bio and trim down to one block,
1968 * then try the write. Where the write fails we record
1969 * a bad block.
1970 * It is conceivable that the bio doesn't exactly align with
1971 * blocks. We must handle this somehow.
1973 * We currently own a reference on the rdev.
1976 int block_sectors;
1977 sector_t sector;
1978 int sectors;
1979 int sect_to_write = r1_bio->sectors;
1980 int ok = 1;
1982 if (rdev->badblocks.shift < 0)
1983 return 0;
1985 block_sectors = 1 << rdev->badblocks.shift;
1986 sector = r1_bio->sector;
1987 sectors = ((sector + block_sectors)
1988 & ~(sector_t)(block_sectors - 1))
1989 - sector;
1991 if (test_bit(R1BIO_BehindIO, &r1_bio->state)) {
1992 vcnt = r1_bio->behind_page_count;
1993 vec = r1_bio->behind_bvecs;
1994 idx = 0;
1995 while (vec[idx].bv_page == NULL)
1996 idx++;
1997 } else {
1998 vcnt = r1_bio->master_bio->bi_vcnt;
1999 vec = r1_bio->master_bio->bi_io_vec;
2000 idx = r1_bio->master_bio->bi_idx;
2002 while (sect_to_write) {
2003 struct bio *wbio;
2004 if (sectors > sect_to_write)
2005 sectors = sect_to_write;
2006 /* Write at 'sector' for 'sectors'*/
2008 wbio = bio_alloc_mddev(GFP_NOIO, vcnt, mddev);
2009 memcpy(wbio->bi_io_vec, vec, vcnt * sizeof(struct bio_vec));
2010 wbio->bi_sector = r1_bio->sector;
2011 wbio->bi_rw = WRITE;
2012 wbio->bi_vcnt = vcnt;
2013 wbio->bi_size = r1_bio->sectors << 9;
2014 wbio->bi_idx = idx;
2016 md_trim_bio(wbio, sector - r1_bio->sector, sectors);
2017 wbio->bi_sector += rdev->data_offset;
2018 wbio->bi_bdev = rdev->bdev;
2019 if (submit_bio_wait(WRITE, wbio) == 0)
2020 /* failure! */
2021 ok = rdev_set_badblocks(rdev, sector,
2022 sectors, 0)
2023 && ok;
2025 bio_put(wbio);
2026 sect_to_write -= sectors;
2027 sector += sectors;
2028 sectors = block_sectors;
2030 return ok;
2033 static void handle_sync_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2035 int m;
2036 int s = r1_bio->sectors;
2037 for (m = 0; m < conf->raid_disks * 2 ; m++) {
2038 struct md_rdev *rdev = conf->mirrors[m].rdev;
2039 struct bio *bio = r1_bio->bios[m];
2040 if (bio->bi_end_io == NULL)
2041 continue;
2042 if (test_bit(BIO_UPTODATE, &bio->bi_flags) &&
2043 test_bit(R1BIO_MadeGood, &r1_bio->state)) {
2044 rdev_clear_badblocks(rdev, r1_bio->sector, s);
2046 if (!test_bit(BIO_UPTODATE, &bio->bi_flags) &&
2047 test_bit(R1BIO_WriteError, &r1_bio->state)) {
2048 if (!rdev_set_badblocks(rdev, r1_bio->sector, s, 0))
2049 md_error(conf->mddev, rdev);
2052 put_buf(r1_bio);
2053 md_done_sync(conf->mddev, s, 1);
2056 static void handle_write_finished(struct r1conf *conf, struct r1bio *r1_bio)
2058 int m;
2059 for (m = 0; m < conf->raid_disks * 2 ; m++)
2060 if (r1_bio->bios[m] == IO_MADE_GOOD) {
2061 struct md_rdev *rdev = conf->mirrors[m].rdev;
2062 rdev_clear_badblocks(rdev,
2063 r1_bio->sector,
2064 r1_bio->sectors);
2065 rdev_dec_pending(rdev, conf->mddev);
2066 } else if (r1_bio->bios[m] != NULL) {
2067 /* This drive got a write error. We need to
2068 * narrow down and record precise write
2069 * errors.
2071 if (!narrow_write_error(r1_bio, m)) {
2072 md_error(conf->mddev,
2073 conf->mirrors[m].rdev);
2074 /* an I/O failed, we can't clear the bitmap */
2075 set_bit(R1BIO_Degraded, &r1_bio->state);
2077 rdev_dec_pending(conf->mirrors[m].rdev,
2078 conf->mddev);
2080 if (test_bit(R1BIO_WriteError, &r1_bio->state))
2081 close_write(r1_bio);
2082 raid_end_bio_io(r1_bio);
2085 static void handle_read_error(struct r1conf *conf, struct r1bio *r1_bio)
2087 int disk;
2088 int max_sectors;
2089 struct mddev *mddev = conf->mddev;
2090 struct bio *bio;
2091 char b[BDEVNAME_SIZE];
2092 struct md_rdev *rdev;
2094 clear_bit(R1BIO_ReadError, &r1_bio->state);
2095 /* we got a read error. Maybe the drive is bad. Maybe just
2096 * the block and we can fix it.
2097 * We freeze all other IO, and try reading the block from
2098 * other devices. When we find one, we re-write
2099 * and check it that fixes the read error.
2100 * This is all done synchronously while the array is
2101 * frozen
2103 if (mddev->ro == 0) {
2104 freeze_array(conf, 1);
2105 fix_read_error(conf, r1_bio->read_disk,
2106 r1_bio->sector, r1_bio->sectors);
2107 unfreeze_array(conf);
2108 } else
2109 md_error(mddev, conf->mirrors[r1_bio->read_disk].rdev);
2111 bio = r1_bio->bios[r1_bio->read_disk];
2112 bdevname(bio->bi_bdev, b);
2113 read_more:
2114 disk = read_balance(conf, r1_bio, &max_sectors);
2115 if (disk == -1) {
2116 printk(KERN_ALERT "md/raid1:%s: %s: unrecoverable I/O"
2117 " read error for block %llu\n",
2118 mdname(mddev), b, (unsigned long long)r1_bio->sector);
2119 raid_end_bio_io(r1_bio);
2120 } else {
2121 const unsigned long do_sync
2122 = r1_bio->master_bio->bi_rw & REQ_SYNC;
2123 if (bio) {
2124 r1_bio->bios[r1_bio->read_disk] =
2125 mddev->ro ? IO_BLOCKED : NULL;
2126 bio_put(bio);
2128 r1_bio->read_disk = disk;
2129 bio = bio_clone_mddev(r1_bio->master_bio, GFP_NOIO, mddev);
2130 md_trim_bio(bio, r1_bio->sector - bio->bi_sector, max_sectors);
2131 r1_bio->bios[r1_bio->read_disk] = bio;
2132 rdev = conf->mirrors[disk].rdev;
2133 printk_ratelimited(KERN_ERR
2134 "md/raid1:%s: redirecting sector %llu"
2135 " to other mirror: %s\n",
2136 mdname(mddev),
2137 (unsigned long long)r1_bio->sector,
2138 bdevname(rdev->bdev, b));
2139 bio->bi_sector = r1_bio->sector + rdev->data_offset;
2140 bio->bi_bdev = rdev->bdev;
2141 bio->bi_end_io = raid1_end_read_request;
2142 bio->bi_rw = READ | do_sync;
2143 bio->bi_private = r1_bio;
2144 if (max_sectors < r1_bio->sectors) {
2145 /* Drat - have to split this up more */
2146 struct bio *mbio = r1_bio->master_bio;
2147 int sectors_handled = (r1_bio->sector + max_sectors
2148 - mbio->bi_sector);
2149 r1_bio->sectors = max_sectors;
2150 spin_lock_irq(&conf->device_lock);
2151 if (mbio->bi_phys_segments == 0)
2152 mbio->bi_phys_segments = 2;
2153 else
2154 mbio->bi_phys_segments++;
2155 spin_unlock_irq(&conf->device_lock);
2156 generic_make_request(bio);
2157 bio = NULL;
2159 r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO);
2161 r1_bio->master_bio = mbio;
2162 r1_bio->sectors = (mbio->bi_size >> 9)
2163 - sectors_handled;
2164 r1_bio->state = 0;
2165 set_bit(R1BIO_ReadError, &r1_bio->state);
2166 r1_bio->mddev = mddev;
2167 r1_bio->sector = mbio->bi_sector + sectors_handled;
2169 goto read_more;
2170 } else
2171 generic_make_request(bio);
2175 static void raid1d(struct mddev *mddev)
2177 struct r1bio *r1_bio;
2178 unsigned long flags;
2179 struct r1conf *conf = mddev->private;
2180 struct list_head *head = &conf->retry_list;
2181 struct blk_plug plug;
2183 md_check_recovery(mddev);
2185 blk_start_plug(&plug);
2186 for (;;) {
2188 if (atomic_read(&mddev->plug_cnt) == 0)
2189 flush_pending_writes(conf);
2191 spin_lock_irqsave(&conf->device_lock, flags);
2192 if (list_empty(head)) {
2193 spin_unlock_irqrestore(&conf->device_lock, flags);
2194 break;
2196 r1_bio = list_entry(head->prev, struct r1bio, retry_list);
2197 list_del(head->prev);
2198 conf->nr_queued--;
2199 spin_unlock_irqrestore(&conf->device_lock, flags);
2201 mddev = r1_bio->mddev;
2202 conf = mddev->private;
2203 if (test_bit(R1BIO_IsSync, &r1_bio->state)) {
2204 if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2205 test_bit(R1BIO_WriteError, &r1_bio->state))
2206 handle_sync_write_finished(conf, r1_bio);
2207 else
2208 sync_request_write(mddev, r1_bio);
2209 } else if (test_bit(R1BIO_MadeGood, &r1_bio->state) ||
2210 test_bit(R1BIO_WriteError, &r1_bio->state))
2211 handle_write_finished(conf, r1_bio);
2212 else if (test_bit(R1BIO_ReadError, &r1_bio->state))
2213 handle_read_error(conf, r1_bio);
2214 else
2215 /* just a partial read to be scheduled from separate
2216 * context
2218 generic_make_request(r1_bio->bios[r1_bio->read_disk]);
2220 cond_resched();
2221 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
2222 md_check_recovery(mddev);
2224 blk_finish_plug(&plug);
2228 static int init_resync(struct r1conf *conf)
2230 int buffs;
2232 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2233 BUG_ON(conf->r1buf_pool);
2234 conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free,
2235 conf->poolinfo);
2236 if (!conf->r1buf_pool)
2237 return -ENOMEM;
2238 conf->next_resync = 0;
2239 return 0;
2243 * perform a "sync" on one "block"
2245 * We need to make sure that no normal I/O request - particularly write
2246 * requests - conflict with active sync requests.
2248 * This is achieved by tracking pending requests and a 'barrier' concept
2249 * that can be installed to exclude normal IO requests.
2252 static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster)
2254 struct r1conf *conf = mddev->private;
2255 struct r1bio *r1_bio;
2256 struct bio *bio;
2257 sector_t max_sector, nr_sectors;
2258 int disk = -1;
2259 int i;
2260 int wonly = -1;
2261 int write_targets = 0, read_targets = 0;
2262 sector_t sync_blocks;
2263 int still_degraded = 0;
2264 int good_sectors = RESYNC_SECTORS;
2265 int min_bad = 0; /* number of sectors that are bad in all devices */
2267 if (!conf->r1buf_pool)
2268 if (init_resync(conf))
2269 return 0;
2271 max_sector = mddev->dev_sectors;
2272 if (sector_nr >= max_sector) {
2273 /* If we aborted, we need to abort the
2274 * sync on the 'current' bitmap chunk (there will
2275 * only be one in raid1 resync.
2276 * We can find the current addess in mddev->curr_resync
2278 if (mddev->curr_resync < max_sector) /* aborted */
2279 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2280 &sync_blocks, 1);
2281 else /* completed sync */
2282 conf->fullsync = 0;
2284 bitmap_close_sync(mddev->bitmap);
2285 close_sync(conf);
2286 return 0;
2289 if (mddev->bitmap == NULL &&
2290 mddev->recovery_cp == MaxSector &&
2291 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2292 conf->fullsync == 0) {
2293 *skipped = 1;
2294 return max_sector - sector_nr;
2296 /* before building a request, check if we can skip these blocks..
2297 * This call the bitmap_start_sync doesn't actually record anything
2299 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2300 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2301 /* We can skip this block, and probably several more */
2302 *skipped = 1;
2303 return sync_blocks;
2306 * If there is non-resync activity waiting for a turn,
2307 * and resync is going fast enough,
2308 * then let it though before starting on this new sync request.
2310 if (!go_faster && conf->nr_waiting)
2311 msleep_interruptible(1000);
2313 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
2314 r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO);
2315 raise_barrier(conf);
2317 conf->next_resync = sector_nr;
2319 rcu_read_lock();
2321 * If we get a correctably read error during resync or recovery,
2322 * we might want to read from a different device. So we
2323 * flag all drives that could conceivably be read from for READ,
2324 * and any others (which will be non-In_sync devices) for WRITE.
2325 * If a read fails, we try reading from something else for which READ
2326 * is OK.
2329 r1_bio->mddev = mddev;
2330 r1_bio->sector = sector_nr;
2331 r1_bio->state = 0;
2332 set_bit(R1BIO_IsSync, &r1_bio->state);
2334 for (i = 0; i < conf->raid_disks * 2; i++) {
2335 struct md_rdev *rdev;
2336 bio = r1_bio->bios[i];
2338 /* take from bio_init */
2339 bio->bi_next = NULL;
2340 bio->bi_flags &= ~(BIO_POOL_MASK-1);
2341 bio->bi_flags |= 1 << BIO_UPTODATE;
2342 bio->bi_rw = READ;
2343 bio->bi_vcnt = 0;
2344 bio->bi_idx = 0;
2345 bio->bi_phys_segments = 0;
2346 bio->bi_size = 0;
2347 bio->bi_end_io = NULL;
2348 bio->bi_private = NULL;
2350 rdev = rcu_dereference(conf->mirrors[i].rdev);
2351 if (rdev == NULL ||
2352 test_bit(Faulty, &rdev->flags)) {
2353 if (i < conf->raid_disks)
2354 still_degraded = 1;
2355 } else if (!test_bit(In_sync, &rdev->flags)) {
2356 bio->bi_rw = WRITE;
2357 bio->bi_end_io = end_sync_write;
2358 write_targets ++;
2359 } else {
2360 /* may need to read from here */
2361 sector_t first_bad = MaxSector;
2362 int bad_sectors;
2364 if (is_badblock(rdev, sector_nr, good_sectors,
2365 &first_bad, &bad_sectors)) {
2366 if (first_bad > sector_nr)
2367 good_sectors = first_bad - sector_nr;
2368 else {
2369 bad_sectors -= (sector_nr - first_bad);
2370 if (min_bad == 0 ||
2371 min_bad > bad_sectors)
2372 min_bad = bad_sectors;
2375 if (sector_nr < first_bad) {
2376 if (test_bit(WriteMostly, &rdev->flags)) {
2377 if (wonly < 0)
2378 wonly = i;
2379 } else {
2380 if (disk < 0)
2381 disk = i;
2383 bio->bi_rw = READ;
2384 bio->bi_end_io = end_sync_read;
2385 read_targets++;
2388 if (bio->bi_end_io) {
2389 atomic_inc(&rdev->nr_pending);
2390 bio->bi_sector = sector_nr + rdev->data_offset;
2391 bio->bi_bdev = rdev->bdev;
2392 bio->bi_private = r1_bio;
2395 rcu_read_unlock();
2396 if (disk < 0)
2397 disk = wonly;
2398 r1_bio->read_disk = disk;
2400 if (read_targets == 0 && min_bad > 0) {
2401 /* These sectors are bad on all InSync devices, so we
2402 * need to mark them bad on all write targets
2404 int ok = 1;
2405 for (i = 0 ; i < conf->raid_disks * 2 ; i++)
2406 if (r1_bio->bios[i]->bi_end_io == end_sync_write) {
2407 struct md_rdev *rdev = conf->mirrors[i].rdev;
2408 ok = rdev_set_badblocks(rdev, sector_nr,
2409 min_bad, 0
2410 ) && ok;
2412 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2413 *skipped = 1;
2414 put_buf(r1_bio);
2416 if (!ok) {
2417 /* Cannot record the badblocks, so need to
2418 * abort the resync.
2419 * If there are multiple read targets, could just
2420 * fail the really bad ones ???
2422 conf->recovery_disabled = mddev->recovery_disabled;
2423 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2424 return 0;
2425 } else
2426 return min_bad;
2429 if (min_bad > 0 && min_bad < good_sectors) {
2430 /* only resync enough to reach the next bad->good
2431 * transition */
2432 good_sectors = min_bad;
2435 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && read_targets > 0)
2436 /* extra read targets are also write targets */
2437 write_targets += read_targets-1;
2439 if (write_targets == 0 || read_targets == 0) {
2440 /* There is nowhere to write, so all non-sync
2441 * drives must be failed - so we are finished
2443 sector_t rv;
2444 if (min_bad > 0)
2445 max_sector = sector_nr + min_bad;
2446 rv = max_sector - sector_nr;
2447 *skipped = 1;
2448 put_buf(r1_bio);
2449 return rv;
2452 if (max_sector > mddev->resync_max)
2453 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2454 if (max_sector > sector_nr + good_sectors)
2455 max_sector = sector_nr + good_sectors;
2456 nr_sectors = 0;
2457 sync_blocks = 0;
2458 do {
2459 struct page *page;
2460 int len = PAGE_SIZE;
2461 if (sector_nr + (len>>9) > max_sector)
2462 len = (max_sector - sector_nr) << 9;
2463 if (len == 0)
2464 break;
2465 if (sync_blocks == 0) {
2466 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
2467 &sync_blocks, still_degraded) &&
2468 !conf->fullsync &&
2469 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery))
2470 break;
2471 BUG_ON(sync_blocks < (PAGE_SIZE>>9));
2472 if ((len >> 9) > sync_blocks)
2473 len = sync_blocks<<9;
2476 for (i = 0 ; i < conf->raid_disks * 2; i++) {
2477 bio = r1_bio->bios[i];
2478 if (bio->bi_end_io) {
2479 page = bio->bi_io_vec[bio->bi_vcnt].bv_page;
2480 if (bio_add_page(bio, page, len, 0) == 0) {
2481 /* stop here */
2482 bio->bi_io_vec[bio->bi_vcnt].bv_page = page;
2483 while (i > 0) {
2484 i--;
2485 bio = r1_bio->bios[i];
2486 if (bio->bi_end_io==NULL)
2487 continue;
2488 /* remove last page from this bio */
2489 bio->bi_vcnt--;
2490 bio->bi_size -= len;
2491 bio->bi_flags &= ~(1<< BIO_SEG_VALID);
2493 goto bio_full;
2497 nr_sectors += len>>9;
2498 sector_nr += len>>9;
2499 sync_blocks -= (len>>9);
2500 } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES);
2501 bio_full:
2502 r1_bio->sectors = nr_sectors;
2504 /* For a user-requested sync, we read all readable devices and do a
2505 * compare
2507 if (test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery)) {
2508 atomic_set(&r1_bio->remaining, read_targets);
2509 for (i = 0; i < conf->raid_disks * 2 && read_targets; i++) {
2510 bio = r1_bio->bios[i];
2511 if (bio->bi_end_io == end_sync_read) {
2512 read_targets--;
2513 md_sync_acct(bio->bi_bdev, nr_sectors);
2514 generic_make_request(bio);
2517 } else {
2518 atomic_set(&r1_bio->remaining, 1);
2519 bio = r1_bio->bios[r1_bio->read_disk];
2520 md_sync_acct(bio->bi_bdev, nr_sectors);
2521 generic_make_request(bio);
2524 return nr_sectors;
2527 static sector_t raid1_size(struct mddev *mddev, sector_t sectors, int raid_disks)
2529 if (sectors)
2530 return sectors;
2532 return mddev->dev_sectors;
2535 static struct r1conf *setup_conf(struct mddev *mddev)
2537 struct r1conf *conf;
2538 int i;
2539 struct mirror_info *disk;
2540 struct md_rdev *rdev;
2541 int err = -ENOMEM;
2543 conf = kzalloc(sizeof(struct r1conf), GFP_KERNEL);
2544 if (!conf)
2545 goto abort;
2547 conf->mirrors = kzalloc(sizeof(struct mirror_info)
2548 * mddev->raid_disks * 2,
2549 GFP_KERNEL);
2550 if (!conf->mirrors)
2551 goto abort;
2553 conf->tmppage = alloc_page(GFP_KERNEL);
2554 if (!conf->tmppage)
2555 goto abort;
2557 conf->poolinfo = kzalloc(sizeof(*conf->poolinfo), GFP_KERNEL);
2558 if (!conf->poolinfo)
2559 goto abort;
2560 conf->poolinfo->raid_disks = mddev->raid_disks * 2;
2561 conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2562 r1bio_pool_free,
2563 conf->poolinfo);
2564 if (!conf->r1bio_pool)
2565 goto abort;
2567 conf->poolinfo->mddev = mddev;
2569 err = -EINVAL;
2570 spin_lock_init(&conf->device_lock);
2571 rdev_for_each(rdev, mddev) {
2572 struct request_queue *q;
2573 int disk_idx = rdev->raid_disk;
2574 if (disk_idx >= mddev->raid_disks
2575 || disk_idx < 0)
2576 continue;
2577 if (test_bit(Replacement, &rdev->flags))
2578 disk = conf->mirrors + mddev->raid_disks + disk_idx;
2579 else
2580 disk = conf->mirrors + disk_idx;
2582 if (disk->rdev)
2583 goto abort;
2584 disk->rdev = rdev;
2585 q = bdev_get_queue(rdev->bdev);
2586 if (q->merge_bvec_fn)
2587 mddev->merge_check_needed = 1;
2589 disk->head_position = 0;
2591 conf->raid_disks = mddev->raid_disks;
2592 conf->mddev = mddev;
2593 INIT_LIST_HEAD(&conf->retry_list);
2595 spin_lock_init(&conf->resync_lock);
2596 init_waitqueue_head(&conf->wait_barrier);
2598 bio_list_init(&conf->pending_bio_list);
2599 conf->pending_count = 0;
2600 conf->recovery_disabled = mddev->recovery_disabled - 1;
2602 err = -EIO;
2603 conf->last_used = -1;
2604 for (i = 0; i < conf->raid_disks * 2; i++) {
2606 disk = conf->mirrors + i;
2608 if (i < conf->raid_disks &&
2609 disk[conf->raid_disks].rdev) {
2610 /* This slot has a replacement. */
2611 if (!disk->rdev) {
2612 /* No original, just make the replacement
2613 * a recovering spare
2615 disk->rdev =
2616 disk[conf->raid_disks].rdev;
2617 disk[conf->raid_disks].rdev = NULL;
2618 } else if (!test_bit(In_sync, &disk->rdev->flags))
2619 /* Original is not in_sync - bad */
2620 goto abort;
2623 if (!disk->rdev ||
2624 !test_bit(In_sync, &disk->rdev->flags)) {
2625 disk->head_position = 0;
2626 if (disk->rdev)
2627 conf->fullsync = 1;
2628 } else if (conf->last_used < 0)
2630 * The first working device is used as a
2631 * starting point to read balancing.
2633 conf->last_used = i;
2636 if (conf->last_used < 0) {
2637 printk(KERN_ERR "md/raid1:%s: no operational mirrors\n",
2638 mdname(mddev));
2639 goto abort;
2641 err = -ENOMEM;
2642 conf->thread = md_register_thread(raid1d, mddev, NULL);
2643 if (!conf->thread) {
2644 printk(KERN_ERR
2645 "md/raid1:%s: couldn't allocate thread\n",
2646 mdname(mddev));
2647 goto abort;
2650 return conf;
2652 abort:
2653 if (conf) {
2654 if (conf->r1bio_pool)
2655 mempool_destroy(conf->r1bio_pool);
2656 kfree(conf->mirrors);
2657 safe_put_page(conf->tmppage);
2658 kfree(conf->poolinfo);
2659 kfree(conf);
2661 return ERR_PTR(err);
2664 static int stop(struct mddev *mddev);
2665 static int run(struct mddev *mddev)
2667 struct r1conf *conf;
2668 int i;
2669 struct md_rdev *rdev;
2670 int ret;
2672 if (mddev->level != 1) {
2673 printk(KERN_ERR "md/raid1:%s: raid level not set to mirroring (%d)\n",
2674 mdname(mddev), mddev->level);
2675 return -EIO;
2677 if (mddev->reshape_position != MaxSector) {
2678 printk(KERN_ERR "md/raid1:%s: reshape_position set but not supported\n",
2679 mdname(mddev));
2680 return -EIO;
2683 * copy the already verified devices into our private RAID1
2684 * bookkeeping area. [whatever we allocate in run(),
2685 * should be freed in stop()]
2687 if (mddev->private == NULL)
2688 conf = setup_conf(mddev);
2689 else
2690 conf = mddev->private;
2692 if (IS_ERR(conf))
2693 return PTR_ERR(conf);
2695 rdev_for_each(rdev, mddev) {
2696 if (!mddev->gendisk)
2697 continue;
2698 disk_stack_limits(mddev->gendisk, rdev->bdev,
2699 rdev->data_offset << 9);
2702 mddev->degraded = 0;
2703 for (i=0; i < conf->raid_disks; i++)
2704 if (conf->mirrors[i].rdev == NULL ||
2705 !test_bit(In_sync, &conf->mirrors[i].rdev->flags) ||
2706 test_bit(Faulty, &conf->mirrors[i].rdev->flags))
2707 mddev->degraded++;
2709 if (conf->raid_disks - mddev->degraded == 1)
2710 mddev->recovery_cp = MaxSector;
2712 if (mddev->recovery_cp != MaxSector)
2713 printk(KERN_NOTICE "md/raid1:%s: not clean"
2714 " -- starting background reconstruction\n",
2715 mdname(mddev));
2716 printk(KERN_INFO
2717 "md/raid1:%s: active with %d out of %d mirrors\n",
2718 mdname(mddev), mddev->raid_disks - mddev->degraded,
2719 mddev->raid_disks);
2722 * Ok, everything is just fine now
2724 mddev->thread = conf->thread;
2725 conf->thread = NULL;
2726 mddev->private = conf;
2728 md_set_array_sectors(mddev, raid1_size(mddev, 0, 0));
2730 if (mddev->queue) {
2731 mddev->queue->backing_dev_info.congested_fn = raid1_congested;
2732 mddev->queue->backing_dev_info.congested_data = mddev;
2733 blk_queue_merge_bvec(mddev->queue, raid1_mergeable_bvec);
2736 ret = md_integrity_register(mddev);
2737 if (ret)
2738 stop(mddev);
2739 return ret;
2742 static int stop(struct mddev *mddev)
2744 struct r1conf *conf = mddev->private;
2745 struct bitmap *bitmap = mddev->bitmap;
2747 /* wait for behind writes to complete */
2748 if (bitmap && atomic_read(&bitmap->behind_writes) > 0) {
2749 printk(KERN_INFO "md/raid1:%s: behind writes in progress - waiting to stop.\n",
2750 mdname(mddev));
2751 /* need to kick something here to make sure I/O goes? */
2752 wait_event(bitmap->behind_wait,
2753 atomic_read(&bitmap->behind_writes) == 0);
2756 raise_barrier(conf);
2757 lower_barrier(conf);
2759 md_unregister_thread(&mddev->thread);
2760 if (conf->r1bio_pool)
2761 mempool_destroy(conf->r1bio_pool);
2762 kfree(conf->mirrors);
2763 kfree(conf->poolinfo);
2764 kfree(conf);
2765 mddev->private = NULL;
2766 return 0;
2769 static int raid1_resize(struct mddev *mddev, sector_t sectors)
2771 /* no resync is happening, and there is enough space
2772 * on all devices, so we can resize.
2773 * We need to make sure resync covers any new space.
2774 * If the array is shrinking we should possibly wait until
2775 * any io in the removed space completes, but it hardly seems
2776 * worth it.
2778 md_set_array_sectors(mddev, raid1_size(mddev, sectors, 0));
2779 if (mddev->array_sectors > raid1_size(mddev, sectors, 0))
2780 return -EINVAL;
2781 set_capacity(mddev->gendisk, mddev->array_sectors);
2782 revalidate_disk(mddev->gendisk);
2783 if (sectors > mddev->dev_sectors &&
2784 mddev->recovery_cp > mddev->dev_sectors) {
2785 mddev->recovery_cp = mddev->dev_sectors;
2786 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2788 mddev->dev_sectors = sectors;
2789 mddev->resync_max_sectors = sectors;
2790 return 0;
2793 static int raid1_reshape(struct mddev *mddev)
2795 /* We need to:
2796 * 1/ resize the r1bio_pool
2797 * 2/ resize conf->mirrors
2799 * We allocate a new r1bio_pool if we can.
2800 * Then raise a device barrier and wait until all IO stops.
2801 * Then resize conf->mirrors and swap in the new r1bio pool.
2803 * At the same time, we "pack" the devices so that all the missing
2804 * devices have the higher raid_disk numbers.
2806 mempool_t *newpool, *oldpool;
2807 struct pool_info *newpoolinfo;
2808 struct mirror_info *newmirrors;
2809 struct r1conf *conf = mddev->private;
2810 int cnt, raid_disks;
2811 unsigned long flags;
2812 int d, d2, err;
2814 /* Cannot change chunk_size, layout, or level */
2815 if (mddev->chunk_sectors != mddev->new_chunk_sectors ||
2816 mddev->layout != mddev->new_layout ||
2817 mddev->level != mddev->new_level) {
2818 mddev->new_chunk_sectors = mddev->chunk_sectors;
2819 mddev->new_layout = mddev->layout;
2820 mddev->new_level = mddev->level;
2821 return -EINVAL;
2824 err = md_allow_write(mddev);
2825 if (err)
2826 return err;
2828 raid_disks = mddev->raid_disks + mddev->delta_disks;
2830 if (raid_disks < conf->raid_disks) {
2831 cnt=0;
2832 for (d= 0; d < conf->raid_disks; d++)
2833 if (conf->mirrors[d].rdev)
2834 cnt++;
2835 if (cnt > raid_disks)
2836 return -EBUSY;
2839 newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL);
2840 if (!newpoolinfo)
2841 return -ENOMEM;
2842 newpoolinfo->mddev = mddev;
2843 newpoolinfo->raid_disks = raid_disks * 2;
2845 newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc,
2846 r1bio_pool_free, newpoolinfo);
2847 if (!newpool) {
2848 kfree(newpoolinfo);
2849 return -ENOMEM;
2851 newmirrors = kzalloc(sizeof(struct mirror_info) * raid_disks * 2,
2852 GFP_KERNEL);
2853 if (!newmirrors) {
2854 kfree(newpoolinfo);
2855 mempool_destroy(newpool);
2856 return -ENOMEM;
2859 freeze_array(conf, 0);
2861 /* ok, everything is stopped */
2862 oldpool = conf->r1bio_pool;
2863 conf->r1bio_pool = newpool;
2865 for (d = d2 = 0; d < conf->raid_disks; d++) {
2866 struct md_rdev *rdev = conf->mirrors[d].rdev;
2867 if (rdev && rdev->raid_disk != d2) {
2868 sysfs_unlink_rdev(mddev, rdev);
2869 rdev->raid_disk = d2;
2870 sysfs_unlink_rdev(mddev, rdev);
2871 if (sysfs_link_rdev(mddev, rdev))
2872 printk(KERN_WARNING
2873 "md/raid1:%s: cannot register rd%d\n",
2874 mdname(mddev), rdev->raid_disk);
2876 if (rdev)
2877 newmirrors[d2++].rdev = rdev;
2879 kfree(conf->mirrors);
2880 conf->mirrors = newmirrors;
2881 kfree(conf->poolinfo);
2882 conf->poolinfo = newpoolinfo;
2884 spin_lock_irqsave(&conf->device_lock, flags);
2885 mddev->degraded += (raid_disks - conf->raid_disks);
2886 spin_unlock_irqrestore(&conf->device_lock, flags);
2887 conf->raid_disks = mddev->raid_disks = raid_disks;
2888 mddev->delta_disks = 0;
2890 conf->last_used = 0; /* just make sure it is in-range */
2891 unfreeze_array(conf);
2893 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2894 md_wakeup_thread(mddev->thread);
2896 mempool_destroy(oldpool);
2897 return 0;
2900 static void raid1_quiesce(struct mddev *mddev, int state)
2902 struct r1conf *conf = mddev->private;
2904 switch(state) {
2905 case 2: /* wake for suspend */
2906 wake_up(&conf->wait_barrier);
2907 break;
2908 case 1:
2909 raise_barrier(conf);
2910 break;
2911 case 0:
2912 lower_barrier(conf);
2913 break;
2917 static void *raid1_takeover(struct mddev *mddev)
2919 /* raid1 can take over:
2920 * raid5 with 2 devices, any layout or chunk size
2922 if (mddev->level == 5 && mddev->raid_disks == 2) {
2923 struct r1conf *conf;
2924 mddev->new_level = 1;
2925 mddev->new_layout = 0;
2926 mddev->new_chunk_sectors = 0;
2927 conf = setup_conf(mddev);
2928 if (!IS_ERR(conf))
2929 conf->barrier = 1;
2930 return conf;
2932 return ERR_PTR(-EINVAL);
2935 static struct md_personality raid1_personality =
2937 .name = "raid1",
2938 .level = 1,
2939 .owner = THIS_MODULE,
2940 .make_request = make_request,
2941 .run = run,
2942 .stop = stop,
2943 .status = status,
2944 .error_handler = error,
2945 .hot_add_disk = raid1_add_disk,
2946 .hot_remove_disk= raid1_remove_disk,
2947 .spare_active = raid1_spare_active,
2948 .sync_request = sync_request,
2949 .resize = raid1_resize,
2950 .size = raid1_size,
2951 .check_reshape = raid1_reshape,
2952 .quiesce = raid1_quiesce,
2953 .takeover = raid1_takeover,
2956 static int __init raid_init(void)
2958 return register_md_personality(&raid1_personality);
2961 static void raid_exit(void)
2963 unregister_md_personality(&raid1_personality);
2966 module_init(raid_init);
2967 module_exit(raid_exit);
2968 MODULE_LICENSE("GPL");
2969 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
2970 MODULE_ALIAS("md-personality-3"); /* RAID1 */
2971 MODULE_ALIAS("md-raid1");
2972 MODULE_ALIAS("md-level-1");
2974 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);