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)
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>
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
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
)
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
;
86 r1_bio
= r1bio_pool_alloc(gfp_flags
, pi
);
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
);
97 r1_bio
->bios
[j
] = bio
;
100 * Allocate RESYNC_PAGES data pages and attach them to
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
))
110 bio
= r1_bio
->bios
[j
];
111 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
112 page
= alloc_page(gfp_flags
);
116 bio
->bi_io_vec
[i
].bv_page
= page
;
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
;
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
);
138 while (++j
< pi
->raid_disks
)
139 bio_put(r1_bio
->bios
[j
]);
140 r1bio_pool_free(r1_bio
, data
);
144 static void r1buf_pool_free(void *__r1_bio
, void *data
)
146 struct pool_info
*pi
= data
;
148 struct r1bio
*r1bio
= __r1_bio
;
150 for (i
= 0; i
< RESYNC_PAGES
; i
++)
151 for (j
= pi
->raid_disks
; j
-- ;) {
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
)
167 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
168 struct bio
**bio
= r1_bio
->bios
+ i
;
169 if (!BIO_SPECIAL(*bio
))
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;
188 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
189 struct bio
*bio
= r1_bio
->bios
[i
];
191 rdev_dec_pending(conf
->mirrors
[i
].rdev
, r1_bio
->mddev
);
194 mempool_free(r1_bio
, conf
->r1buf_pool
);
199 static void reschedule_retry(struct r1bio
*r1_bio
)
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
);
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
219 static void call_bio_endio(struct r1bio
*r1_bio
)
221 struct bio
*bio
= r1_bio
->master_bio
;
223 struct r1conf
*conf
= r1_bio
->mddev
->private;
225 if (bio
->bi_phys_segments
) {
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
);
234 if (!test_bit(R1BIO_Uptodate
, &r1_bio
->state
))
235 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
239 * Wake up any possible resync thread that waits for the device
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
);
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
)
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
)
287 BUG_ON(mirror
== raid_disks
* 2);
288 update_head_pos(mirror
, r1_bio
);
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
;
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
);
307 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
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"
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
)))
319 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
323 raid_end_bio_io(r1_bio
);
328 char b
[BDEVNAME_SIZE
];
330 KERN_ERR
"md/raid1:%s: %s: "
331 "rescheduling sector %llu\n",
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
;
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
,
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
))
367 if (test_bit(R1BIO_WriteError
, &r1_bio
->state
))
368 reschedule_retry(r1_bio
);
371 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
))
372 reschedule_retry(r1_bio
);
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:
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
);
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
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.
414 r1_bio
->bios
[mirror
] = NULL
;
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
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
);
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"
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
,
467 * Let's see if all mirrored write operations have finished
470 r1_bio_write_done(r1_bio
);
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
;
495 int best_good_sectors
;
500 struct md_rdev
*rdev
;
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.
510 sectors
= r1_bio
->sectors
;
512 best_dist
= MaxSector
;
513 best_good_sectors
= 0;
515 if (conf
->mddev
->recovery_cp
< MaxSector
&&
516 (this_sector
+ sectors
>= conf
->next_resync
)) {
521 start_disk
= conf
->last_used
;
524 for (i
= 0 ; i
< conf
->raid_disks
* 2 ; i
++) {
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
536 || test_bit(Unmerged
, &rdev
->flags
)
537 || test_bit(Faulty
, &rdev
->flags
))
539 if (!test_bit(In_sync
, &rdev
->flags
) &&
540 rdev
->recovery_offset
< this_sector
+ sectors
)
542 if (test_bit(WriteMostly
, &rdev
->flags
)) {
543 /* Don't balance among write-mostly, just
544 * use the first as a last resort */
546 if (is_badblock(rdev
, this_sector
, sectors
,
547 &first_bad
, &bad_sectors
)) {
548 if (first_bad
< this_sector
)
549 /* Cannot use this */
551 best_good_sectors
= first_bad
- this_sector
;
553 best_good_sectors
= sectors
;
558 /* This is a reasonable device to use. It might
561 if (is_badblock(rdev
, this_sector
, sectors
,
562 &first_bad
, &bad_sectors
)) {
563 if (best_dist
< MaxSector
)
564 /* already have a better device */
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
;
578 sector_t good_sectors
= first_bad
- this_sector
;
579 if (good_sectors
> best_good_sectors
) {
580 best_good_sectors
= good_sectors
;
588 best_good_sectors
= sectors
;
590 dist
= abs(this_sector
- conf
->mirrors
[disk
].head_position
);
592 /* Don't change to another disk for sequential reads */
593 || conf
->next_seq_sect
== this_sector
595 /* If device is idle, use it */
596 || atomic_read(&rdev
->nr_pending
) == 0) {
600 if (dist
< best_dist
) {
606 if (best_disk
>= 0) {
607 rdev
= rcu_dereference(conf
->mirrors
[best_disk
].rdev
);
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
);
618 sectors
= best_good_sectors
;
619 conf
->next_seq_sect
= this_sector
+ sectors
;
620 conf
->last_used
= best_disk
;
623 *max_sectors
= sectors
;
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
) {
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
+
649 bvm
->bi_bdev
= rdev
->bdev
;
650 max
= min(max
, q
->merge_bvec_fn(
661 int md_raid1_congested(struct mddev
*mddev
, int bits
)
663 struct r1conf
*conf
= mddev
->private;
666 if ((bits
& (1 << BDI_async_congested
)) &&
667 conf
->pending_count
>= max_queued_requests
)
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
);
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
);
684 ret
&= bdi_congested(&q
->backing_dev_info
, bits
);
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
) {
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
;
720 generic_make_request(bio
);
724 spin_unlock_irq(&conf
->device_lock
);
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
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 */
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
)
772 BUG_ON(conf
->barrier
<= 0);
773 spin_lock_irqsave(&conf
->resync_lock
, flags
);
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
);
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
793 wait_event_lock_irq(conf
->wait_barrier
,
797 !bio_list_empty(current
->bio_list
)),
803 spin_unlock_irq(&conf
->resync_lock
);
806 static void allow_barrier(struct r1conf
*conf
)
809 spin_lock_irqsave(&conf
->resync_lock
, flags
);
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
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
829 spin_lock_irq(&conf
->resync_lock
);
832 wait_event_lock_irq(conf
->wait_barrier
,
833 conf
->nr_pending
== conf
->nr_queued
+extra
,
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
);
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
)
854 struct bio_vec
*bvec
;
855 struct bio_vec
*bvecs
= kzalloc(bio
->bi_vcnt
* sizeof(struct bio_vec
),
857 if (unlikely(!bvecs
))
860 bio_for_each_segment(bvec
, bio
, i
) {
862 bvecs
[i
].bv_page
= alloc_page(GFP_NOIO
);
863 if (unlikely(!bvecs
[i
].bv_page
))
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
);
876 for (i
= 0; i
< bio
->bi_vcnt
; i
++)
877 if (bvecs
[i
].bv_page
)
878 put_page(bvecs
[i
].bv_page
);
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
;
890 struct bitmap
*bitmap
;
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
;
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
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
)
926 finish_wait(&conf
->wait_barrier
, &w
);
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;
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
);
958 * read balancing logic:
963 rdisk
= read_balance(conf
, r1_bio
, &max_sectors
);
966 /* couldn't find anywhere to read from */
967 raid_end_bio_io(r1_bio
);
970 mirror
= conf
->mirrors
+ rdisk
;
972 if (test_bit(WriteMostly
, &mirror
->rdev
->flags
) &&
974 /* Reading from a write-mostly device must
975 * take care not to over-take any writes
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
,
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
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;
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
;
1021 r1_bio
->mddev
= mddev
;
1022 r1_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
1025 generic_make_request(read_bio
);
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
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
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;
1051 blocked_rdev
= NULL
;
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
;
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
);
1069 atomic_inc(&rdev
->nr_pending
);
1070 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1075 is_bad
= is_badblock(rdev
, r1_bio
->sector
,
1077 &first_bad
, &bad_sectors
);
1079 /* mustn't write here until the bad block is
1081 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1082 blocked_rdev
= rdev
;
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
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
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
;
1116 if (unlikely(blocked_rdev
)) {
1117 /* Wait for this device to become unblocked */
1120 for (j
= 0; j
< i
; j
++)
1121 if (r1_bio
->bios
[j
])
1122 rdev_dec_pending(conf
->mirrors
[j
].rdev
, mddev
);
1124 allow_barrier(conf
);
1125 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
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;
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);
1148 for (i
= 0; i
< disks
; i
++) {
1150 if (!r1_bio
->bios
[i
])
1153 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1154 md_trim_bio(mbio
, r1_bio
->sector
- bio
->bi_sector
, max_sectors
);
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 */
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
,
1169 test_bit(R1BIO_BehindIO
,
1173 if (r1_bio
->behind_bvecs
) {
1174 struct bio_vec
*bvec
;
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
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
;
1217 r1_bio
->mddev
= mddev
;
1218 r1_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
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;
1236 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
1237 conf
->raid_disks
- mddev
->degraded
);
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" : "_");
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
;
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
);
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
);
1283 set_bit(Faulty
, &rdev
->flags
);
1284 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
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
)
1296 printk(KERN_DEBUG
"RAID1 conf printout:\n");
1298 printk(KERN_DEBUG
"(!conf)\n");
1301 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
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
);
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
));
1317 static void close_sync(struct r1conf
*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
)
1329 struct r1conf
*conf
= mddev
->private;
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
;
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 */
1347 !test_and_clear_bit(In_sync
, &rdev
->flags
))
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(
1360 && rdev
->recovery_offset
== MaxSector
1361 && !test_bit(Faulty
, &rdev
->flags
)
1362 && !test_and_set_bit(In_sync
, &rdev
->flags
)) {
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
);
1376 static int raid1_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1378 struct r1conf
*conf
= mddev
->private;
1381 struct mirror_info
*p
;
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
)
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
;
1401 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1402 rdev
->data_offset
<< 9);
1404 p
->head_position
= 0;
1405 rdev
->raid_disk
= mirror
;
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)
1412 rcu_assign_pointer(p
->rdev
, rdev
);
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
;
1423 rcu_assign_pointer(p
[conf
->raid_disks
].rdev
, rdev
);
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
);
1445 static int raid1_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1447 struct r1conf
*conf
= mddev
->private;
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
;
1456 if (rdev
== p
->rdev
) {
1457 if (test_bit(In_sync
, &rdev
->flags
) ||
1458 atomic_read(&rdev
->nr_pending
)) {
1462 /* Only remove non-faulty devices if recovery
1465 if (!test_bit(Faulty
, &rdev
->flags
) &&
1466 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
1467 mddev
->degraded
< conf
->raid_disks
) {
1473 if (atomic_read(&rdev
->nr_pending
)) {
1474 /* lost the race, try later */
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
);
1488 conf
->mirrors
[conf
->raid_disks
+ number
].rdev
= NULL
;
1489 unfreeze_array(conf
);
1490 clear_bit(WantReplacement
, &rdev
->flags
);
1492 clear_bit(WantReplacement
, &rdev
->flags
);
1493 err
= md_integrity_register(mddev
);
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;
1530 mirror
= find_bio_disk(r1_bio
, bio
);
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. */
1538 bitmap_end_sync(mddev
->bitmap
, s
,
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
, &
1549 set_bit(R1BIO_WriteError
, &r1_bio
->state
);
1550 } else if (is_badblock(conf
->mirrors
[mirror
].rdev
,
1553 &first_bad
, &bad_sectors
) &&
1554 !is_badblock(conf
->mirrors
[r1_bio
->read_disk
].rdev
,
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
);
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))
1580 set_bit(WriteErrorSeen
, &rdev
->flags
);
1581 if (!test_and_set_bit(WantReplacement
,
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
);
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
;
1614 int d
= r1_bio
->read_disk
;
1616 struct md_rdev
*rdev
;
1619 if (s
> (PAGE_SIZE
>>9))
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
,
1636 if (d
== conf
->raid_disks
* 2)
1638 } while (!success
&& d
!= r1_bio
->read_disk
);
1641 char b
[BDEVNAME_SIZE
];
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",
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
))
1657 if (!rdev_set_badblocks(rdev
, sect
, s
, 0))
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);
1676 /* write it back and re-read */
1677 while (d
!= r1_bio
->read_disk
) {
1679 d
= conf
->raid_disks
* 2;
1681 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
1683 rdev
= conf
->mirrors
[d
].rdev
;
1684 if (r1_sync_page_io(rdev
, sect
, s
,
1685 bio
->bi_io_vec
[idx
].bv_page
,
1687 r1_bio
->bios
[d
]->bi_end_io
= NULL
;
1688 rdev_dec_pending(rdev
, mddev
);
1692 while (d
!= r1_bio
->read_disk
) {
1694 d
= conf
->raid_disks
* 2;
1696 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
1698 rdev
= conf
->mirrors
[d
].rdev
;
1699 if (r1_sync_page_io(rdev
, sect
, s
,
1700 bio
->bi_io_vec
[idx
].bv_page
,
1702 atomic_add(s
, &rdev
->corrected_errors
);
1708 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
1709 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
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;
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
);
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
++) {
1739 struct bio
*pbio
= r1_bio
->bios
[primary
];
1740 struct bio
*sbio
= r1_bio
->bios
[i
];
1743 if (r1_bio
->bios
[i
]->bi_end_io
!= end_sync_read
)
1746 if (test_bit(BIO_UPTODATE
, &sbio
->bi_flags
)) {
1747 for (j
= vcnt
; j
-- ; ) {
1749 p
= pbio
->bi_io_vec
[j
].bv_page
;
1750 s
= sbio
->bi_io_vec
[j
].bv_page
;
1751 if (memcmp(page_address(p
),
1753 sbio
->bi_io_vec
[j
].bv_len
))
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
);
1767 /* fixup the bio for reuse */
1768 sbio
->bi_vcnt
= vcnt
;
1769 sbio
->bi_size
= r1_bio
->sectors
<< 9;
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
++) {
1781 bi
= &sbio
->bi_io_vec
[j
];
1783 if (size
> PAGE_SIZE
)
1784 bi
->bv_len
= PAGE_SIZE
;
1788 memcpy(page_address(bi
->bv_page
),
1789 page_address(pbio
->bi_io_vec
[j
].bv_page
),
1796 static void sync_request_write(struct mddev
*mddev
, struct r1bio
*r1_bio
)
1798 struct r1conf
*conf
= mddev
->private;
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
))
1810 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
1811 if (process_checks(r1_bio
) < 0)
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
))))
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
);
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
;
1863 struct md_rdev
*rdev
;
1865 if (s
> (PAGE_SIZE
>>9))
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....
1877 rdev
= conf
->mirrors
[d
].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))
1887 if (d
== conf
->raid_disks
* 2)
1890 } while (!success
&& d
!= read_disk
);
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
);
1899 /* write it back and re-read */
1901 while (d
!= read_disk
) {
1903 d
= conf
->raid_disks
* 2;
1905 rdev
= conf
->mirrors
[d
].rdev
;
1907 test_bit(In_sync
, &rdev
->flags
))
1908 r1_sync_page_io(rdev
, sect
, s
,
1909 conf
->tmppage
, WRITE
);
1912 while (d
!= read_disk
) {
1913 char b
[BDEVNAME_SIZE
];
1915 d
= conf
->raid_disks
* 2;
1917 rdev
= conf
->mirrors
[d
].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
);
1924 "md/raid1:%s: read error corrected "
1925 "(%d sectors at %llu on %s)\n",
1927 (unsigned long long)(sect
+
1929 bdevname(rdev
->bdev
, b
));
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
;
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
;
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
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.
1979 int sect_to_write
= r1_bio
->sectors
;
1982 if (rdev
->badblocks
.shift
< 0)
1985 block_sectors
= 1 << rdev
->badblocks
.shift
;
1986 sector
= r1_bio
->sector
;
1987 sectors
= ((sector
+ block_sectors
)
1988 & ~(sector_t
)(block_sectors
- 1))
1991 if (test_bit(R1BIO_BehindIO
, &r1_bio
->state
)) {
1992 vcnt
= r1_bio
->behind_page_count
;
1993 vec
= r1_bio
->behind_bvecs
;
1995 while (vec
[idx
].bv_page
== NULL
)
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
) {
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;
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)
2021 ok
= rdev_set_badblocks(rdev
, sector
,
2026 sect_to_write
-= sectors
;
2028 sectors
= block_sectors
;
2033 static void handle_sync_write_finished(struct r1conf
*conf
, struct r1bio
*r1_bio
)
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
)
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
);
2053 md_done_sync(conf
->mddev
, s
, 1);
2056 static void handle_write_finished(struct r1conf
*conf
, struct r1bio
*r1_bio
)
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
,
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
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
,
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
)
2089 struct mddev
*mddev
= conf
->mddev
;
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
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
);
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
);
2114 disk
= read_balance(conf
, r1_bio
, &max_sectors
);
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
);
2121 const unsigned long do_sync
2122 = r1_bio
->master_bio
->bi_rw
& REQ_SYNC
;
2124 r1_bio
->bios
[r1_bio
->read_disk
] =
2125 mddev
->ro
? IO_BLOCKED
: NULL
;
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",
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
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;
2154 mbio
->bi_phys_segments
++;
2155 spin_unlock_irq(&conf
->device_lock
);
2156 generic_make_request(bio
);
2159 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
2161 r1_bio
->master_bio
= mbio
;
2162 r1_bio
->sectors
= (mbio
->bi_size
>> 9)
2165 set_bit(R1BIO_ReadError
, &r1_bio
->state
);
2166 r1_bio
->mddev
= mddev
;
2167 r1_bio
->sector
= mbio
->bi_sector
+ sectors_handled
;
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
);
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
);
2196 r1_bio
= list_entry(head
->prev
, struct r1bio
, retry_list
);
2197 list_del(head
->prev
);
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
);
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
);
2215 /* just a partial read to be scheduled from separate
2218 generic_make_request(r1_bio
->bios
[r1_bio
->read_disk
]);
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
)
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
,
2236 if (!conf
->r1buf_pool
)
2238 conf
->next_resync
= 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
;
2257 sector_t max_sector
, nr_sectors
;
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
))
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
,
2281 else /* completed sync */
2284 bitmap_close_sync(mddev
->bitmap
);
2289 if (mddev
->bitmap
== NULL
&&
2290 mddev
->recovery_cp
== MaxSector
&&
2291 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
2292 conf
->fullsync
== 0) {
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 */
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
;
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
2329 r1_bio
->mddev
= mddev
;
2330 r1_bio
->sector
= sector_nr
;
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
;
2345 bio
->bi_phys_segments
= 0;
2347 bio
->bi_end_io
= NULL
;
2348 bio
->bi_private
= NULL
;
2350 rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
2352 test_bit(Faulty
, &rdev
->flags
)) {
2353 if (i
< conf
->raid_disks
)
2355 } else if (!test_bit(In_sync
, &rdev
->flags
)) {
2357 bio
->bi_end_io
= end_sync_write
;
2360 /* may need to read from here */
2361 sector_t first_bad
= MaxSector
;
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
;
2369 bad_sectors
-= (sector_nr
- first_bad
);
2371 min_bad
> bad_sectors
)
2372 min_bad
= bad_sectors
;
2375 if (sector_nr
< first_bad
) {
2376 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2384 bio
->bi_end_io
= end_sync_read
;
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
;
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
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
,
2412 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2417 /* Cannot record the badblocks, so need to
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
);
2429 if (min_bad
> 0 && min_bad
< good_sectors
) {
2430 /* only resync enough to reach the next bad->good
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
2445 max_sector
= sector_nr
+ min_bad
;
2446 rv
= max_sector
- sector_nr
;
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
;
2460 int len
= PAGE_SIZE
;
2461 if (sector_nr
+ (len
>>9) > max_sector
)
2462 len
= (max_sector
- sector_nr
) << 9;
2465 if (sync_blocks
== 0) {
2466 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
2467 &sync_blocks
, still_degraded
) &&
2469 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
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) {
2482 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2485 bio
= r1_bio
->bios
[i
];
2486 if (bio
->bi_end_io
==NULL
)
2488 /* remove last page from this bio */
2490 bio
->bi_size
-= len
;
2491 bio
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
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
);
2502 r1_bio
->sectors
= nr_sectors
;
2504 /* For a user-requested sync, we read all readable devices and do a
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
) {
2513 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2514 generic_make_request(bio
);
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
);
2527 static sector_t
raid1_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
2532 return mddev
->dev_sectors
;
2535 static struct r1conf
*setup_conf(struct mddev
*mddev
)
2537 struct r1conf
*conf
;
2539 struct mirror_info
*disk
;
2540 struct md_rdev
*rdev
;
2543 conf
= kzalloc(sizeof(struct r1conf
), GFP_KERNEL
);
2547 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)
2548 * mddev
->raid_disks
* 2,
2553 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2557 conf
->poolinfo
= kzalloc(sizeof(*conf
->poolinfo
), GFP_KERNEL
);
2558 if (!conf
->poolinfo
)
2560 conf
->poolinfo
->raid_disks
= mddev
->raid_disks
* 2;
2561 conf
->r1bio_pool
= mempool_create(NR_RAID1_BIOS
, r1bio_pool_alloc
,
2564 if (!conf
->r1bio_pool
)
2567 conf
->poolinfo
->mddev
= mddev
;
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
2577 if (test_bit(Replacement
, &rdev
->flags
))
2578 disk
= conf
->mirrors
+ mddev
->raid_disks
+ disk_idx
;
2580 disk
= conf
->mirrors
+ disk_idx
;
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;
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. */
2612 /* No original, just make the replacement
2613 * a recovering spare
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 */
2624 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2625 disk
->head_position
= 0;
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",
2642 conf
->thread
= md_register_thread(raid1d
, mddev
, NULL
);
2643 if (!conf
->thread
) {
2645 "md/raid1:%s: couldn't allocate thread\n",
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
);
2661 return ERR_PTR(err
);
2664 static int stop(struct mddev
*mddev
);
2665 static int run(struct mddev
*mddev
)
2667 struct r1conf
*conf
;
2669 struct md_rdev
*rdev
;
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
);
2677 if (mddev
->reshape_position
!= MaxSector
) {
2678 printk(KERN_ERR
"md/raid1:%s: reshape_position set but not supported\n",
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
);
2690 conf
= mddev
->private;
2693 return PTR_ERR(conf
);
2695 rdev_for_each(rdev
, mddev
) {
2696 if (!mddev
->gendisk
)
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
))
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",
2717 "md/raid1:%s: active with %d out of %d mirrors\n",
2718 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
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));
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
);
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",
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
);
2765 mddev
->private = NULL
;
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
2778 md_set_array_sectors(mddev
, raid1_size(mddev
, sectors
, 0));
2779 if (mddev
->array_sectors
> raid1_size(mddev
, sectors
, 0))
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
;
2793 static int raid1_reshape(struct mddev
*mddev
)
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
;
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
;
2824 err
= md_allow_write(mddev
);
2828 raid_disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
2830 if (raid_disks
< conf
->raid_disks
) {
2832 for (d
= 0; d
< conf
->raid_disks
; d
++)
2833 if (conf
->mirrors
[d
].rdev
)
2835 if (cnt
> raid_disks
)
2839 newpoolinfo
= kmalloc(sizeof(*newpoolinfo
), GFP_KERNEL
);
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
);
2851 newmirrors
= kzalloc(sizeof(struct mirror_info
) * raid_disks
* 2,
2855 mempool_destroy(newpool
);
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
))
2873 "md/raid1:%s: cannot register rd%d\n",
2874 mdname(mddev
), rdev
->raid_disk
);
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
);
2900 static void raid1_quiesce(struct mddev
*mddev
, int state
)
2902 struct r1conf
*conf
= mddev
->private;
2905 case 2: /* wake for suspend */
2906 wake_up(&conf
->wait_barrier
);
2909 raise_barrier(conf
);
2912 lower_barrier(conf
);
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
);
2932 return ERR_PTR(-EINVAL
);
2935 static struct md_personality raid1_personality
=
2939 .owner
= THIS_MODULE
,
2940 .make_request
= make_request
,
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
,
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
);