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/seq_file.h>
38 #include <linux/ratelimit.h>
44 #define PRINTK(x...) do { if (DEBUG) printk(x); } while (0)
47 * Number of guaranteed r1bios in case of extreme VM load:
49 #define NR_RAID1_BIOS 256
52 static void allow_barrier(conf_t
*conf
);
53 static void lower_barrier(conf_t
*conf
);
55 static void * r1bio_pool_alloc(gfp_t gfp_flags
, void *data
)
57 struct pool_info
*pi
= data
;
58 int size
= offsetof(r1bio_t
, bios
[pi
->raid_disks
]);
60 /* allocate a r1bio with room for raid_disks entries in the bios array */
61 return kzalloc(size
, gfp_flags
);
64 static void r1bio_pool_free(void *r1_bio
, void *data
)
69 #define RESYNC_BLOCK_SIZE (64*1024)
70 //#define RESYNC_BLOCK_SIZE PAGE_SIZE
71 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
72 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
73 #define RESYNC_WINDOW (2048*1024)
75 static void * r1buf_pool_alloc(gfp_t gfp_flags
, void *data
)
77 struct pool_info
*pi
= data
;
83 r1_bio
= r1bio_pool_alloc(gfp_flags
, pi
);
88 * Allocate bios : 1 for reading, n-1 for writing
90 for (j
= pi
->raid_disks
; j
-- ; ) {
91 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
94 r1_bio
->bios
[j
] = bio
;
97 * Allocate RESYNC_PAGES data pages and attach them to
99 * If this is a user-requested check/repair, allocate
100 * RESYNC_PAGES for each bio.
102 if (test_bit(MD_RECOVERY_REQUESTED
, &pi
->mddev
->recovery
))
107 bio
= r1_bio
->bios
[j
];
108 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
109 page
= alloc_page(gfp_flags
);
113 bio
->bi_io_vec
[i
].bv_page
= page
;
117 /* If not user-requests, copy the page pointers to all bios */
118 if (!test_bit(MD_RECOVERY_REQUESTED
, &pi
->mddev
->recovery
)) {
119 for (i
=0; i
<RESYNC_PAGES
; i
++)
120 for (j
=1; j
<pi
->raid_disks
; j
++)
121 r1_bio
->bios
[j
]->bi_io_vec
[i
].bv_page
=
122 r1_bio
->bios
[0]->bi_io_vec
[i
].bv_page
;
125 r1_bio
->master_bio
= NULL
;
130 for (j
=0 ; j
< pi
->raid_disks
; j
++)
131 for (i
=0; i
< r1_bio
->bios
[j
]->bi_vcnt
; i
++)
132 put_page(r1_bio
->bios
[j
]->bi_io_vec
[i
].bv_page
);
135 while ( ++j
< pi
->raid_disks
)
136 bio_put(r1_bio
->bios
[j
]);
137 r1bio_pool_free(r1_bio
, data
);
141 static void r1buf_pool_free(void *__r1_bio
, void *data
)
143 struct pool_info
*pi
= data
;
145 r1bio_t
*r1bio
= __r1_bio
;
147 for (i
= 0; i
< RESYNC_PAGES
; i
++)
148 for (j
= pi
->raid_disks
; j
-- ;) {
150 r1bio
->bios
[j
]->bi_io_vec
[i
].bv_page
!=
151 r1bio
->bios
[0]->bi_io_vec
[i
].bv_page
)
152 safe_put_page(r1bio
->bios
[j
]->bi_io_vec
[i
].bv_page
);
154 for (i
=0 ; i
< pi
->raid_disks
; i
++)
155 bio_put(r1bio
->bios
[i
]);
157 r1bio_pool_free(r1bio
, data
);
160 static void put_all_bios(conf_t
*conf
, r1bio_t
*r1_bio
)
164 for (i
= 0; i
< conf
->raid_disks
; i
++) {
165 struct bio
**bio
= r1_bio
->bios
+ i
;
166 if (!BIO_SPECIAL(*bio
))
172 static void free_r1bio(r1bio_t
*r1_bio
)
174 conf_t
*conf
= r1_bio
->mddev
->private;
176 put_all_bios(conf
, r1_bio
);
177 mempool_free(r1_bio
, conf
->r1bio_pool
);
180 static void put_buf(r1bio_t
*r1_bio
)
182 conf_t
*conf
= r1_bio
->mddev
->private;
185 for (i
=0; i
<conf
->raid_disks
; i
++) {
186 struct bio
*bio
= r1_bio
->bios
[i
];
188 rdev_dec_pending(conf
->mirrors
[i
].rdev
, r1_bio
->mddev
);
191 mempool_free(r1_bio
, conf
->r1buf_pool
);
196 static void reschedule_retry(r1bio_t
*r1_bio
)
199 mddev_t
*mddev
= r1_bio
->mddev
;
200 conf_t
*conf
= mddev
->private;
202 spin_lock_irqsave(&conf
->device_lock
, flags
);
203 list_add(&r1_bio
->retry_list
, &conf
->retry_list
);
205 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
207 wake_up(&conf
->wait_barrier
);
208 md_wakeup_thread(mddev
->thread
);
212 * raid_end_bio_io() is called when we have finished servicing a mirrored
213 * operation and are ready to return a success/failure code to the buffer
216 static void call_bio_endio(r1bio_t
*r1_bio
)
218 struct bio
*bio
= r1_bio
->master_bio
;
220 conf_t
*conf
= r1_bio
->mddev
->private;
222 if (bio
->bi_phys_segments
) {
224 spin_lock_irqsave(&conf
->device_lock
, flags
);
225 bio
->bi_phys_segments
--;
226 done
= (bio
->bi_phys_segments
== 0);
227 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
231 if (!test_bit(R1BIO_Uptodate
, &r1_bio
->state
))
232 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
236 * Wake up any possible resync thread that waits for the device
243 static void raid_end_bio_io(r1bio_t
*r1_bio
)
245 struct bio
*bio
= r1_bio
->master_bio
;
247 /* if nobody has done the final endio yet, do it now */
248 if (!test_and_set_bit(R1BIO_Returned
, &r1_bio
->state
)) {
249 PRINTK(KERN_DEBUG
"raid1: sync end %s on sectors %llu-%llu\n",
250 (bio_data_dir(bio
) == WRITE
) ? "write" : "read",
251 (unsigned long long) bio
->bi_sector
,
252 (unsigned long long) bio
->bi_sector
+
253 (bio
->bi_size
>> 9) - 1);
255 call_bio_endio(r1_bio
);
261 * Update disk head position estimator based on IRQ completion info.
263 static inline void update_head_pos(int disk
, r1bio_t
*r1_bio
)
265 conf_t
*conf
= r1_bio
->mddev
->private;
267 conf
->mirrors
[disk
].head_position
=
268 r1_bio
->sector
+ (r1_bio
->sectors
);
271 static void raid1_end_read_request(struct bio
*bio
, int error
)
273 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
274 r1bio_t
*r1_bio
= bio
->bi_private
;
276 conf_t
*conf
= r1_bio
->mddev
->private;
278 mirror
= r1_bio
->read_disk
;
280 * this branch is our 'one mirror IO has finished' event handler:
282 update_head_pos(mirror
, r1_bio
);
285 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
287 /* If all other devices have failed, we want to return
288 * the error upwards rather than fail the last device.
289 * Here we redefine "uptodate" to mean "Don't want to retry"
292 spin_lock_irqsave(&conf
->device_lock
, flags
);
293 if (r1_bio
->mddev
->degraded
== conf
->raid_disks
||
294 (r1_bio
->mddev
->degraded
== conf
->raid_disks
-1 &&
295 !test_bit(Faulty
, &conf
->mirrors
[mirror
].rdev
->flags
)))
297 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
301 raid_end_bio_io(r1_bio
);
306 char b
[BDEVNAME_SIZE
];
308 KERN_ERR
"md/raid1:%s: %s: "
309 "rescheduling sector %llu\n",
311 bdevname(conf
->mirrors
[mirror
].rdev
->bdev
,
313 (unsigned long long)r1_bio
->sector
);
314 set_bit(R1BIO_ReadError
, &r1_bio
->state
);
315 reschedule_retry(r1_bio
);
318 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
, conf
->mddev
);
321 static void r1_bio_write_done(r1bio_t
*r1_bio
)
323 if (atomic_dec_and_test(&r1_bio
->remaining
))
325 /* it really is the end of this request */
326 if (test_bit(R1BIO_BehindIO
, &r1_bio
->state
)) {
327 /* free extra copy of the data pages */
328 int i
= r1_bio
->behind_page_count
;
330 safe_put_page(r1_bio
->behind_bvecs
[i
].bv_page
);
331 kfree(r1_bio
->behind_bvecs
);
332 r1_bio
->behind_bvecs
= NULL
;
334 /* clear the bitmap if all writes complete successfully */
335 bitmap_endwrite(r1_bio
->mddev
->bitmap
, r1_bio
->sector
,
337 !test_bit(R1BIO_Degraded
, &r1_bio
->state
),
338 test_bit(R1BIO_BehindIO
, &r1_bio
->state
));
339 md_write_end(r1_bio
->mddev
);
340 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
))
341 reschedule_retry(r1_bio
);
343 raid_end_bio_io(r1_bio
);
347 static void raid1_end_write_request(struct bio
*bio
, int error
)
349 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
350 r1bio_t
*r1_bio
= bio
->bi_private
;
351 int mirror
, behind
= test_bit(R1BIO_BehindIO
, &r1_bio
->state
);
352 conf_t
*conf
= r1_bio
->mddev
->private;
353 struct bio
*to_put
= NULL
;
356 for (mirror
= 0; mirror
< conf
->raid_disks
; mirror
++)
357 if (r1_bio
->bios
[mirror
] == bio
)
361 * 'one mirror IO has finished' event handler:
363 r1_bio
->bios
[mirror
] = NULL
;
366 md_error(r1_bio
->mddev
, conf
->mirrors
[mirror
].rdev
);
367 /* an I/O failed, we can't clear the bitmap */
368 set_bit(R1BIO_Degraded
, &r1_bio
->state
);
371 * Set R1BIO_Uptodate in our master bio, so that we
372 * will return a good error code for to the higher
373 * levels even if IO on some other mirrored buffer
376 * The 'master' represents the composite IO operation
377 * to user-side. So if something waits for IO, then it
378 * will wait for the 'master' bio.
383 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
385 /* Maybe we can clear some bad blocks. */
386 if (is_badblock(conf
->mirrors
[mirror
].rdev
,
387 r1_bio
->sector
, r1_bio
->sectors
,
388 &first_bad
, &bad_sectors
)) {
389 r1_bio
->bios
[mirror
] = IO_MADE_GOOD
;
390 set_bit(R1BIO_MadeGood
, &r1_bio
->state
);
394 update_head_pos(mirror
, r1_bio
);
397 if (test_bit(WriteMostly
, &conf
->mirrors
[mirror
].rdev
->flags
))
398 atomic_dec(&r1_bio
->behind_remaining
);
401 * In behind mode, we ACK the master bio once the I/O
402 * has safely reached all non-writemostly
403 * disks. Setting the Returned bit ensures that this
404 * gets done only once -- we don't ever want to return
405 * -EIO here, instead we'll wait
407 if (atomic_read(&r1_bio
->behind_remaining
) >= (atomic_read(&r1_bio
->remaining
)-1) &&
408 test_bit(R1BIO_Uptodate
, &r1_bio
->state
)) {
409 /* Maybe we can return now */
410 if (!test_and_set_bit(R1BIO_Returned
, &r1_bio
->state
)) {
411 struct bio
*mbio
= r1_bio
->master_bio
;
412 PRINTK(KERN_DEBUG
"raid1: behind end write sectors %llu-%llu\n",
413 (unsigned long long) mbio
->bi_sector
,
414 (unsigned long long) mbio
->bi_sector
+
415 (mbio
->bi_size
>> 9) - 1);
416 call_bio_endio(r1_bio
);
420 if (r1_bio
->bios
[mirror
] == NULL
)
421 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
,
425 * Let's see if all mirrored write operations have finished
428 r1_bio_write_done(r1_bio
);
436 * This routine returns the disk from which the requested read should
437 * be done. There is a per-array 'next expected sequential IO' sector
438 * number - if this matches on the next IO then we use the last disk.
439 * There is also a per-disk 'last know head position' sector that is
440 * maintained from IRQ contexts, both the normal and the resync IO
441 * completion handlers update this position correctly. If there is no
442 * perfect sequential match then we pick the disk whose head is closest.
444 * If there are 2 mirrors in the same 2 devices, performance degrades
445 * because position is mirror, not device based.
447 * The rdev for the device selected will have nr_pending incremented.
449 static int read_balance(conf_t
*conf
, r1bio_t
*r1_bio
, int *max_sectors
)
451 const sector_t this_sector
= r1_bio
->sector
;
453 int best_good_sectors
;
463 * Check if we can balance. We can balance on the whole
464 * device if no resync is going on, or below the resync window.
465 * We take the first readable disk when above the resync window.
468 sectors
= r1_bio
->sectors
;
470 best_dist
= MaxSector
;
471 best_good_sectors
= 0;
473 if (conf
->mddev
->recovery_cp
< MaxSector
&&
474 (this_sector
+ sectors
>= conf
->next_resync
)) {
479 start_disk
= conf
->last_used
;
482 for (i
= 0 ; i
< conf
->raid_disks
; i
++) {
487 int disk
= start_disk
+ i
;
488 if (disk
>= conf
->raid_disks
)
489 disk
-= conf
->raid_disks
;
491 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
492 if (r1_bio
->bios
[disk
] == IO_BLOCKED
494 || test_bit(Faulty
, &rdev
->flags
))
496 if (!test_bit(In_sync
, &rdev
->flags
) &&
497 rdev
->recovery_offset
< this_sector
+ sectors
)
499 if (test_bit(WriteMostly
, &rdev
->flags
)) {
500 /* Don't balance among write-mostly, just
501 * use the first as a last resort */
506 /* This is a reasonable device to use. It might
509 if (is_badblock(rdev
, this_sector
, sectors
,
510 &first_bad
, &bad_sectors
)) {
511 if (best_dist
< MaxSector
)
512 /* already have a better device */
514 if (first_bad
<= this_sector
) {
515 /* cannot read here. If this is the 'primary'
516 * device, then we must not read beyond
517 * bad_sectors from another device..
519 bad_sectors
-= (this_sector
- first_bad
);
520 if (choose_first
&& sectors
> bad_sectors
)
521 sectors
= bad_sectors
;
522 if (best_good_sectors
> sectors
)
523 best_good_sectors
= sectors
;
526 sector_t good_sectors
= first_bad
- this_sector
;
527 if (good_sectors
> best_good_sectors
) {
528 best_good_sectors
= good_sectors
;
536 best_good_sectors
= sectors
;
538 dist
= abs(this_sector
- conf
->mirrors
[disk
].head_position
);
540 /* Don't change to another disk for sequential reads */
541 || conf
->next_seq_sect
== this_sector
543 /* If device is idle, use it */
544 || atomic_read(&rdev
->nr_pending
) == 0) {
548 if (dist
< best_dist
) {
554 if (best_disk
>= 0) {
555 rdev
= rcu_dereference(conf
->mirrors
[best_disk
].rdev
);
558 atomic_inc(&rdev
->nr_pending
);
559 if (test_bit(Faulty
, &rdev
->flags
)) {
560 /* cannot risk returning a device that failed
561 * before we inc'ed nr_pending
563 rdev_dec_pending(rdev
, conf
->mddev
);
566 sectors
= best_good_sectors
;
567 conf
->next_seq_sect
= this_sector
+ sectors
;
568 conf
->last_used
= best_disk
;
571 *max_sectors
= sectors
;
576 int md_raid1_congested(mddev_t
*mddev
, int bits
)
578 conf_t
*conf
= mddev
->private;
582 for (i
= 0; i
< mddev
->raid_disks
; i
++) {
583 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
584 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
585 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
589 /* Note the '|| 1' - when read_balance prefers
590 * non-congested targets, it can be removed
592 if ((bits
& (1<<BDI_async_congested
)) || 1)
593 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
595 ret
&= bdi_congested(&q
->backing_dev_info
, bits
);
601 EXPORT_SYMBOL_GPL(md_raid1_congested
);
603 static int raid1_congested(void *data
, int bits
)
605 mddev_t
*mddev
= data
;
607 return mddev_congested(mddev
, bits
) ||
608 md_raid1_congested(mddev
, bits
);
611 static void flush_pending_writes(conf_t
*conf
)
613 /* Any writes that have been queued but are awaiting
614 * bitmap updates get flushed here.
616 spin_lock_irq(&conf
->device_lock
);
618 if (conf
->pending_bio_list
.head
) {
620 bio
= bio_list_get(&conf
->pending_bio_list
);
621 spin_unlock_irq(&conf
->device_lock
);
622 /* flush any pending bitmap writes to
623 * disk before proceeding w/ I/O */
624 bitmap_unplug(conf
->mddev
->bitmap
);
626 while (bio
) { /* submit pending writes */
627 struct bio
*next
= bio
->bi_next
;
629 generic_make_request(bio
);
633 spin_unlock_irq(&conf
->device_lock
);
637 * Sometimes we need to suspend IO while we do something else,
638 * either some resync/recovery, or reconfigure the array.
639 * To do this we raise a 'barrier'.
640 * The 'barrier' is a counter that can be raised multiple times
641 * to count how many activities are happening which preclude
643 * We can only raise the barrier if there is no pending IO.
644 * i.e. if nr_pending == 0.
645 * We choose only to raise the barrier if no-one is waiting for the
646 * barrier to go down. This means that as soon as an IO request
647 * is ready, no other operations which require a barrier will start
648 * until the IO request has had a chance.
650 * So: regular IO calls 'wait_barrier'. When that returns there
651 * is no backgroup IO happening, It must arrange to call
652 * allow_barrier when it has finished its IO.
653 * backgroup IO calls must call raise_barrier. Once that returns
654 * there is no normal IO happeing. It must arrange to call
655 * lower_barrier when the particular background IO completes.
657 #define RESYNC_DEPTH 32
659 static void raise_barrier(conf_t
*conf
)
661 spin_lock_irq(&conf
->resync_lock
);
663 /* Wait until no block IO is waiting */
664 wait_event_lock_irq(conf
->wait_barrier
, !conf
->nr_waiting
,
665 conf
->resync_lock
, );
667 /* block any new IO from starting */
670 /* Now wait for all pending IO to complete */
671 wait_event_lock_irq(conf
->wait_barrier
,
672 !conf
->nr_pending
&& conf
->barrier
< RESYNC_DEPTH
,
673 conf
->resync_lock
, );
675 spin_unlock_irq(&conf
->resync_lock
);
678 static void lower_barrier(conf_t
*conf
)
681 BUG_ON(conf
->barrier
<= 0);
682 spin_lock_irqsave(&conf
->resync_lock
, flags
);
684 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
685 wake_up(&conf
->wait_barrier
);
688 static void wait_barrier(conf_t
*conf
)
690 spin_lock_irq(&conf
->resync_lock
);
693 wait_event_lock_irq(conf
->wait_barrier
, !conf
->barrier
,
699 spin_unlock_irq(&conf
->resync_lock
);
702 static void allow_barrier(conf_t
*conf
)
705 spin_lock_irqsave(&conf
->resync_lock
, flags
);
707 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
708 wake_up(&conf
->wait_barrier
);
711 static void freeze_array(conf_t
*conf
)
713 /* stop syncio and normal IO and wait for everything to
715 * We increment barrier and nr_waiting, and then
716 * wait until nr_pending match nr_queued+1
717 * This is called in the context of one normal IO request
718 * that has failed. Thus any sync request that might be pending
719 * will be blocked by nr_pending, and we need to wait for
720 * pending IO requests to complete or be queued for re-try.
721 * Thus the number queued (nr_queued) plus this request (1)
722 * must match the number of pending IOs (nr_pending) before
725 spin_lock_irq(&conf
->resync_lock
);
728 wait_event_lock_irq(conf
->wait_barrier
,
729 conf
->nr_pending
== conf
->nr_queued
+1,
731 flush_pending_writes(conf
));
732 spin_unlock_irq(&conf
->resync_lock
);
734 static void unfreeze_array(conf_t
*conf
)
736 /* reverse the effect of the freeze */
737 spin_lock_irq(&conf
->resync_lock
);
740 wake_up(&conf
->wait_barrier
);
741 spin_unlock_irq(&conf
->resync_lock
);
745 /* duplicate the data pages for behind I/O
747 static void alloc_behind_pages(struct bio
*bio
, r1bio_t
*r1_bio
)
750 struct bio_vec
*bvec
;
751 struct bio_vec
*bvecs
= kzalloc(bio
->bi_vcnt
* sizeof(struct bio_vec
),
753 if (unlikely(!bvecs
))
756 bio_for_each_segment(bvec
, bio
, i
) {
758 bvecs
[i
].bv_page
= alloc_page(GFP_NOIO
);
759 if (unlikely(!bvecs
[i
].bv_page
))
761 memcpy(kmap(bvecs
[i
].bv_page
) + bvec
->bv_offset
,
762 kmap(bvec
->bv_page
) + bvec
->bv_offset
, bvec
->bv_len
);
763 kunmap(bvecs
[i
].bv_page
);
764 kunmap(bvec
->bv_page
);
766 r1_bio
->behind_bvecs
= bvecs
;
767 r1_bio
->behind_page_count
= bio
->bi_vcnt
;
768 set_bit(R1BIO_BehindIO
, &r1_bio
->state
);
772 for (i
= 0; i
< bio
->bi_vcnt
; i
++)
773 if (bvecs
[i
].bv_page
)
774 put_page(bvecs
[i
].bv_page
);
776 PRINTK("%dB behind alloc failed, doing sync I/O\n", bio
->bi_size
);
779 static int make_request(mddev_t
*mddev
, struct bio
* bio
)
781 conf_t
*conf
= mddev
->private;
782 mirror_info_t
*mirror
;
784 struct bio
*read_bio
;
786 struct bitmap
*bitmap
;
788 const int rw
= bio_data_dir(bio
);
789 const unsigned long do_sync
= (bio
->bi_rw
& REQ_SYNC
);
790 const unsigned long do_flush_fua
= (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
));
791 mdk_rdev_t
*blocked_rdev
;
798 * Register the new request and wait if the reconstruction
799 * thread has put up a bar for new requests.
800 * Continue immediately if no resync is active currently.
803 md_write_start(mddev
, bio
); /* wait on superblock update early */
805 if (bio_data_dir(bio
) == WRITE
&&
806 bio
->bi_sector
+ bio
->bi_size
/512 > mddev
->suspend_lo
&&
807 bio
->bi_sector
< mddev
->suspend_hi
) {
808 /* As the suspend_* range is controlled by
809 * userspace, we want an interruptible
814 flush_signals(current
);
815 prepare_to_wait(&conf
->wait_barrier
,
816 &w
, TASK_INTERRUPTIBLE
);
817 if (bio
->bi_sector
+ bio
->bi_size
/512 <= mddev
->suspend_lo
||
818 bio
->bi_sector
>= mddev
->suspend_hi
)
822 finish_wait(&conf
->wait_barrier
, &w
);
827 bitmap
= mddev
->bitmap
;
830 * make_request() can abort the operation when READA is being
831 * used and no empty request is available.
834 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
836 r1_bio
->master_bio
= bio
;
837 r1_bio
->sectors
= bio
->bi_size
>> 9;
839 r1_bio
->mddev
= mddev
;
840 r1_bio
->sector
= bio
->bi_sector
;
842 /* We might need to issue multiple reads to different
843 * devices if there are bad blocks around, so we keep
844 * track of the number of reads in bio->bi_phys_segments.
845 * If this is 0, there is only one r1_bio and no locking
846 * will be needed when requests complete. If it is
847 * non-zero, then it is the number of not-completed requests.
849 bio
->bi_phys_segments
= 0;
850 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
854 * read balancing logic:
859 rdisk
= read_balance(conf
, r1_bio
, &max_sectors
);
862 /* couldn't find anywhere to read from */
863 raid_end_bio_io(r1_bio
);
866 mirror
= conf
->mirrors
+ rdisk
;
868 if (test_bit(WriteMostly
, &mirror
->rdev
->flags
) &&
870 /* Reading from a write-mostly device must
871 * take care not to over-take any writes
874 wait_event(bitmap
->behind_wait
,
875 atomic_read(&bitmap
->behind_writes
) == 0);
877 r1_bio
->read_disk
= rdisk
;
879 read_bio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
880 md_trim_bio(read_bio
, r1_bio
->sector
- bio
->bi_sector
,
883 r1_bio
->bios
[rdisk
] = read_bio
;
885 read_bio
->bi_sector
= r1_bio
->sector
+ mirror
->rdev
->data_offset
;
886 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
887 read_bio
->bi_end_io
= raid1_end_read_request
;
888 read_bio
->bi_rw
= READ
| do_sync
;
889 read_bio
->bi_private
= r1_bio
;
891 if (max_sectors
< r1_bio
->sectors
) {
892 /* could not read all from this device, so we will
893 * need another r1_bio.
896 sectors_handled
= (r1_bio
->sector
+ max_sectors
898 r1_bio
->sectors
= max_sectors
;
899 spin_lock_irq(&conf
->device_lock
);
900 if (bio
->bi_phys_segments
== 0)
901 bio
->bi_phys_segments
= 2;
903 bio
->bi_phys_segments
++;
904 spin_unlock_irq(&conf
->device_lock
);
905 /* Cannot call generic_make_request directly
906 * as that will be queued in __make_request
907 * and subsequent mempool_alloc might block waiting
908 * for it. So hand bio over to raid1d.
910 reschedule_retry(r1_bio
);
912 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
914 r1_bio
->master_bio
= bio
;
915 r1_bio
->sectors
= (bio
->bi_size
>> 9) - sectors_handled
;
917 r1_bio
->mddev
= mddev
;
918 r1_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
921 generic_make_request(read_bio
);
928 /* first select target devices under rcu_lock and
929 * inc refcount on their rdev. Record them by setting
931 * If there are known/acknowledged bad blocks on any device on
932 * which we have seen a write error, we want to avoid writing those
934 * This potentially requires several writes to write around
935 * the bad blocks. Each set of writes gets it's own r1bio
936 * with a set of bios attached.
938 plugged
= mddev_check_plugged(mddev
);
940 disks
= conf
->raid_disks
;
944 max_sectors
= r1_bio
->sectors
;
945 for (i
= 0; i
< disks
; i
++) {
946 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
947 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
948 atomic_inc(&rdev
->nr_pending
);
952 r1_bio
->bios
[i
] = NULL
;
953 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
)) {
954 set_bit(R1BIO_Degraded
, &r1_bio
->state
);
958 atomic_inc(&rdev
->nr_pending
);
959 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
964 is_bad
= is_badblock(rdev
, r1_bio
->sector
,
966 &first_bad
, &bad_sectors
);
968 /* mustn't write here until the bad block is
970 set_bit(BlockedBadBlocks
, &rdev
->flags
);
974 if (is_bad
&& first_bad
<= r1_bio
->sector
) {
975 /* Cannot write here at all */
976 bad_sectors
-= (r1_bio
->sector
- first_bad
);
977 if (bad_sectors
< max_sectors
)
978 /* mustn't write more than bad_sectors
979 * to other devices yet
981 max_sectors
= bad_sectors
;
982 rdev_dec_pending(rdev
, mddev
);
983 /* We don't set R1BIO_Degraded as that
984 * only applies if the disk is
985 * missing, so it might be re-added,
986 * and we want to know to recover this
988 * In this case the device is here,
989 * and the fact that this chunk is not
990 * in-sync is recorded in the bad
996 int good_sectors
= first_bad
- r1_bio
->sector
;
997 if (good_sectors
< max_sectors
)
998 max_sectors
= good_sectors
;
1001 r1_bio
->bios
[i
] = bio
;
1005 if (unlikely(blocked_rdev
)) {
1006 /* Wait for this device to become unblocked */
1009 for (j
= 0; j
< i
; j
++)
1010 if (r1_bio
->bios
[j
])
1011 rdev_dec_pending(conf
->mirrors
[j
].rdev
, mddev
);
1013 allow_barrier(conf
);
1014 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1019 if (max_sectors
< r1_bio
->sectors
) {
1020 /* We are splitting this write into multiple parts, so
1021 * we need to prepare for allocating another r1_bio.
1023 r1_bio
->sectors
= max_sectors
;
1024 spin_lock_irq(&conf
->device_lock
);
1025 if (bio
->bi_phys_segments
== 0)
1026 bio
->bi_phys_segments
= 2;
1028 bio
->bi_phys_segments
++;
1029 spin_unlock_irq(&conf
->device_lock
);
1031 sectors_handled
= r1_bio
->sector
+ max_sectors
- bio
->bi_sector
;
1033 atomic_set(&r1_bio
->remaining
, 1);
1034 atomic_set(&r1_bio
->behind_remaining
, 0);
1037 for (i
= 0; i
< disks
; i
++) {
1039 if (!r1_bio
->bios
[i
])
1042 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1043 md_trim_bio(mbio
, r1_bio
->sector
- bio
->bi_sector
, max_sectors
);
1047 * Not if there are too many, or cannot
1048 * allocate memory, or a reader on WriteMostly
1049 * is waiting for behind writes to flush */
1051 (atomic_read(&bitmap
->behind_writes
)
1052 < mddev
->bitmap_info
.max_write_behind
) &&
1053 !waitqueue_active(&bitmap
->behind_wait
))
1054 alloc_behind_pages(mbio
, r1_bio
);
1056 bitmap_startwrite(bitmap
, r1_bio
->sector
,
1058 test_bit(R1BIO_BehindIO
,
1062 if (r1_bio
->behind_bvecs
) {
1063 struct bio_vec
*bvec
;
1066 /* Yes, I really want the '__' version so that
1067 * we clear any unused pointer in the io_vec, rather
1068 * than leave them unchanged. This is important
1069 * because when we come to free the pages, we won't
1070 * know the original bi_idx, so we just free
1073 __bio_for_each_segment(bvec
, mbio
, j
, 0)
1074 bvec
->bv_page
= r1_bio
->behind_bvecs
[j
].bv_page
;
1075 if (test_bit(WriteMostly
, &conf
->mirrors
[i
].rdev
->flags
))
1076 atomic_inc(&r1_bio
->behind_remaining
);
1079 r1_bio
->bios
[i
] = mbio
;
1081 mbio
->bi_sector
= (r1_bio
->sector
+
1082 conf
->mirrors
[i
].rdev
->data_offset
);
1083 mbio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1084 mbio
->bi_end_io
= raid1_end_write_request
;
1085 mbio
->bi_rw
= WRITE
| do_flush_fua
| do_sync
;
1086 mbio
->bi_private
= r1_bio
;
1088 atomic_inc(&r1_bio
->remaining
);
1089 spin_lock_irqsave(&conf
->device_lock
, flags
);
1090 bio_list_add(&conf
->pending_bio_list
, mbio
);
1091 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1093 r1_bio_write_done(r1_bio
);
1095 /* In case raid1d snuck in to freeze_array */
1096 wake_up(&conf
->wait_barrier
);
1098 if (sectors_handled
< (bio
->bi_size
>> 9)) {
1099 /* We need another r1_bio. It has already been counted
1100 * in bio->bi_phys_segments
1102 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
1103 r1_bio
->master_bio
= bio
;
1104 r1_bio
->sectors
= (bio
->bi_size
>> 9) - sectors_handled
;
1106 r1_bio
->mddev
= mddev
;
1107 r1_bio
->sector
= bio
->bi_sector
+ sectors_handled
;
1111 if (do_sync
|| !bitmap
|| !plugged
)
1112 md_wakeup_thread(mddev
->thread
);
1117 static void status(struct seq_file
*seq
, mddev_t
*mddev
)
1119 conf_t
*conf
= mddev
->private;
1122 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
1123 conf
->raid_disks
- mddev
->degraded
);
1125 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1126 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1127 seq_printf(seq
, "%s",
1128 rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
1131 seq_printf(seq
, "]");
1135 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1137 char b
[BDEVNAME_SIZE
];
1138 conf_t
*conf
= mddev
->private;
1141 * If it is not operational, then we have already marked it as dead
1142 * else if it is the last working disks, ignore the error, let the
1143 * next level up know.
1144 * else mark the drive as failed
1146 if (test_bit(In_sync
, &rdev
->flags
)
1147 && (conf
->raid_disks
- mddev
->degraded
) == 1) {
1149 * Don't fail the drive, act as though we were just a
1150 * normal single drive.
1151 * However don't try a recovery from this drive as
1152 * it is very likely to fail.
1154 conf
->recovery_disabled
= mddev
->recovery_disabled
;
1157 set_bit(Blocked
, &rdev
->flags
);
1158 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1159 unsigned long flags
;
1160 spin_lock_irqsave(&conf
->device_lock
, flags
);
1162 set_bit(Faulty
, &rdev
->flags
);
1163 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1165 * if recovery is running, make sure it aborts.
1167 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1169 set_bit(Faulty
, &rdev
->flags
);
1170 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1172 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1173 "md/raid1:%s: Operation continuing on %d devices.\n",
1174 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1175 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
);
1178 static void print_conf(conf_t
*conf
)
1182 printk(KERN_DEBUG
"RAID1 conf printout:\n");
1184 printk(KERN_DEBUG
"(!conf)\n");
1187 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1191 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1192 char b
[BDEVNAME_SIZE
];
1193 mdk_rdev_t
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1195 printk(KERN_DEBUG
" disk %d, wo:%d, o:%d, dev:%s\n",
1196 i
, !test_bit(In_sync
, &rdev
->flags
),
1197 !test_bit(Faulty
, &rdev
->flags
),
1198 bdevname(rdev
->bdev
,b
));
1203 static void close_sync(conf_t
*conf
)
1206 allow_barrier(conf
);
1208 mempool_destroy(conf
->r1buf_pool
);
1209 conf
->r1buf_pool
= NULL
;
1212 static int raid1_spare_active(mddev_t
*mddev
)
1215 conf_t
*conf
= mddev
->private;
1217 unsigned long flags
;
1220 * Find all failed disks within the RAID1 configuration
1221 * and mark them readable.
1222 * Called under mddev lock, so rcu protection not needed.
1224 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1225 mdk_rdev_t
*rdev
= conf
->mirrors
[i
].rdev
;
1227 && !test_bit(Faulty
, &rdev
->flags
)
1228 && !test_and_set_bit(In_sync
, &rdev
->flags
)) {
1230 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
1233 spin_lock_irqsave(&conf
->device_lock
, flags
);
1234 mddev
->degraded
-= count
;
1235 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1242 static int raid1_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
1244 conf_t
*conf
= mddev
->private;
1249 int last
= mddev
->raid_disks
- 1;
1251 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
1254 if (rdev
->raid_disk
>= 0)
1255 first
= last
= rdev
->raid_disk
;
1257 for (mirror
= first
; mirror
<= last
; mirror
++)
1258 if ( !(p
=conf
->mirrors
+mirror
)->rdev
) {
1260 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1261 rdev
->data_offset
<< 9);
1262 /* as we don't honour merge_bvec_fn, we must
1263 * never risk violating it, so limit
1264 * ->max_segments to one lying with a single
1265 * page, as a one page request is never in
1268 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
1269 blk_queue_max_segments(mddev
->queue
, 1);
1270 blk_queue_segment_boundary(mddev
->queue
,
1271 PAGE_CACHE_SIZE
- 1);
1274 p
->head_position
= 0;
1275 rdev
->raid_disk
= mirror
;
1277 /* As all devices are equivalent, we don't need a full recovery
1278 * if this was recently any drive of the array
1280 if (rdev
->saved_raid_disk
< 0)
1282 rcu_assign_pointer(p
->rdev
, rdev
);
1285 md_integrity_add_rdev(rdev
, mddev
);
1290 static int raid1_remove_disk(mddev_t
*mddev
, int number
)
1292 conf_t
*conf
= mddev
->private;
1295 mirror_info_t
*p
= conf
->mirrors
+ number
;
1300 if (test_bit(In_sync
, &rdev
->flags
) ||
1301 atomic_read(&rdev
->nr_pending
)) {
1305 /* Only remove non-faulty devices if recovery
1308 if (!test_bit(Faulty
, &rdev
->flags
) &&
1309 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
1310 mddev
->degraded
< conf
->raid_disks
) {
1316 if (atomic_read(&rdev
->nr_pending
)) {
1317 /* lost the race, try later */
1322 err
= md_integrity_register(mddev
);
1331 static void end_sync_read(struct bio
*bio
, int error
)
1333 r1bio_t
*r1_bio
= bio
->bi_private
;
1336 for (i
=r1_bio
->mddev
->raid_disks
; i
--; )
1337 if (r1_bio
->bios
[i
] == bio
)
1340 update_head_pos(i
, r1_bio
);
1342 * we have read a block, now it needs to be re-written,
1343 * or re-read if the read failed.
1344 * We don't do much here, just schedule handling by raid1d
1346 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1347 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
1349 if (atomic_dec_and_test(&r1_bio
->remaining
))
1350 reschedule_retry(r1_bio
);
1353 static void end_sync_write(struct bio
*bio
, int error
)
1355 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1356 r1bio_t
*r1_bio
= bio
->bi_private
;
1357 mddev_t
*mddev
= r1_bio
->mddev
;
1358 conf_t
*conf
= mddev
->private;
1364 for (i
= 0; i
< conf
->raid_disks
; i
++)
1365 if (r1_bio
->bios
[i
] == bio
) {
1370 sector_t sync_blocks
= 0;
1371 sector_t s
= r1_bio
->sector
;
1372 long sectors_to_go
= r1_bio
->sectors
;
1373 /* make sure these bits doesn't get cleared. */
1375 bitmap_end_sync(mddev
->bitmap
, s
,
1378 sectors_to_go
-= sync_blocks
;
1379 } while (sectors_to_go
> 0);
1380 md_error(mddev
, conf
->mirrors
[mirror
].rdev
);
1381 } else if (is_badblock(conf
->mirrors
[mirror
].rdev
,
1384 &first_bad
, &bad_sectors
))
1385 set_bit(R1BIO_MadeGood
, &r1_bio
->state
);
1387 update_head_pos(mirror
, r1_bio
);
1389 if (atomic_dec_and_test(&r1_bio
->remaining
)) {
1390 int s
= r1_bio
->sectors
;
1391 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
))
1392 reschedule_retry(r1_bio
);
1395 md_done_sync(mddev
, s
, uptodate
);
1400 static int fix_sync_read_error(r1bio_t
*r1_bio
)
1402 /* Try some synchronous reads of other devices to get
1403 * good data, much like with normal read errors. Only
1404 * read into the pages we already have so we don't
1405 * need to re-issue the read request.
1406 * We don't need to freeze the array, because being in an
1407 * active sync request, there is no normal IO, and
1408 * no overlapping syncs.
1409 * We don't need to check is_badblock() again as we
1410 * made sure that anything with a bad block in range
1411 * will have bi_end_io clear.
1413 mddev_t
*mddev
= r1_bio
->mddev
;
1414 conf_t
*conf
= mddev
->private;
1415 struct bio
*bio
= r1_bio
->bios
[r1_bio
->read_disk
];
1416 sector_t sect
= r1_bio
->sector
;
1417 int sectors
= r1_bio
->sectors
;
1422 int d
= r1_bio
->read_disk
;
1427 if (s
> (PAGE_SIZE
>>9))
1430 if (r1_bio
->bios
[d
]->bi_end_io
== end_sync_read
) {
1431 /* No rcu protection needed here devices
1432 * can only be removed when no resync is
1433 * active, and resync is currently active
1435 rdev
= conf
->mirrors
[d
].rdev
;
1436 if (sync_page_io(rdev
, sect
, s
<<9,
1437 bio
->bi_io_vec
[idx
].bv_page
,
1444 if (d
== conf
->raid_disks
)
1446 } while (!success
&& d
!= r1_bio
->read_disk
);
1449 char b
[BDEVNAME_SIZE
];
1450 /* Cannot read from anywhere, array is toast */
1451 md_error(mddev
, conf
->mirrors
[r1_bio
->read_disk
].rdev
);
1452 printk(KERN_ALERT
"md/raid1:%s: %s: unrecoverable I/O read error"
1453 " for block %llu\n",
1455 bdevname(bio
->bi_bdev
, b
),
1456 (unsigned long long)r1_bio
->sector
);
1457 md_done_sync(mddev
, r1_bio
->sectors
, 0);
1463 /* write it back and re-read */
1464 while (d
!= r1_bio
->read_disk
) {
1466 d
= conf
->raid_disks
;
1468 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
1470 rdev
= conf
->mirrors
[d
].rdev
;
1471 if (sync_page_io(rdev
, sect
, s
<<9,
1472 bio
->bi_io_vec
[idx
].bv_page
,
1473 WRITE
, false) == 0) {
1474 r1_bio
->bios
[d
]->bi_end_io
= NULL
;
1475 rdev_dec_pending(rdev
, mddev
);
1476 md_error(mddev
, rdev
);
1480 while (d
!= r1_bio
->read_disk
) {
1482 d
= conf
->raid_disks
;
1484 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
1486 rdev
= conf
->mirrors
[d
].rdev
;
1487 if (sync_page_io(rdev
, sect
, s
<<9,
1488 bio
->bi_io_vec
[idx
].bv_page
,
1490 md_error(mddev
, rdev
);
1492 atomic_add(s
, &rdev
->corrected_errors
);
1498 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
1499 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1503 static int process_checks(r1bio_t
*r1_bio
)
1505 /* We have read all readable devices. If we haven't
1506 * got the block, then there is no hope left.
1507 * If we have, then we want to do a comparison
1508 * and skip the write if everything is the same.
1509 * If any blocks failed to read, then we need to
1510 * attempt an over-write
1512 mddev_t
*mddev
= r1_bio
->mddev
;
1513 conf_t
*conf
= mddev
->private;
1517 for (primary
= 0; primary
< conf
->raid_disks
; primary
++)
1518 if (r1_bio
->bios
[primary
]->bi_end_io
== end_sync_read
&&
1519 test_bit(BIO_UPTODATE
, &r1_bio
->bios
[primary
]->bi_flags
)) {
1520 r1_bio
->bios
[primary
]->bi_end_io
= NULL
;
1521 rdev_dec_pending(conf
->mirrors
[primary
].rdev
, mddev
);
1524 r1_bio
->read_disk
= primary
;
1525 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1527 int vcnt
= r1_bio
->sectors
>> (PAGE_SHIFT
- 9);
1528 struct bio
*pbio
= r1_bio
->bios
[primary
];
1529 struct bio
*sbio
= r1_bio
->bios
[i
];
1532 if (r1_bio
->bios
[i
]->bi_end_io
!= end_sync_read
)
1535 if (test_bit(BIO_UPTODATE
, &sbio
->bi_flags
)) {
1536 for (j
= vcnt
; j
-- ; ) {
1538 p
= pbio
->bi_io_vec
[j
].bv_page
;
1539 s
= sbio
->bi_io_vec
[j
].bv_page
;
1540 if (memcmp(page_address(p
),
1548 mddev
->resync_mismatches
+= r1_bio
->sectors
;
1549 if (j
< 0 || (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)
1550 && test_bit(BIO_UPTODATE
, &sbio
->bi_flags
))) {
1551 /* No need to write to this device. */
1552 sbio
->bi_end_io
= NULL
;
1553 rdev_dec_pending(conf
->mirrors
[i
].rdev
, mddev
);
1556 /* fixup the bio for reuse */
1557 sbio
->bi_vcnt
= vcnt
;
1558 sbio
->bi_size
= r1_bio
->sectors
<< 9;
1560 sbio
->bi_phys_segments
= 0;
1561 sbio
->bi_flags
&= ~(BIO_POOL_MASK
- 1);
1562 sbio
->bi_flags
|= 1 << BIO_UPTODATE
;
1563 sbio
->bi_next
= NULL
;
1564 sbio
->bi_sector
= r1_bio
->sector
+
1565 conf
->mirrors
[i
].rdev
->data_offset
;
1566 sbio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1567 size
= sbio
->bi_size
;
1568 for (j
= 0; j
< vcnt
; j
++) {
1570 bi
= &sbio
->bi_io_vec
[j
];
1572 if (size
> PAGE_SIZE
)
1573 bi
->bv_len
= PAGE_SIZE
;
1577 memcpy(page_address(bi
->bv_page
),
1578 page_address(pbio
->bi_io_vec
[j
].bv_page
),
1585 static void sync_request_write(mddev_t
*mddev
, r1bio_t
*r1_bio
)
1587 conf_t
*conf
= mddev
->private;
1589 int disks
= conf
->raid_disks
;
1590 struct bio
*bio
, *wbio
;
1592 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
1594 if (!test_bit(R1BIO_Uptodate
, &r1_bio
->state
))
1595 /* ouch - failed to read all of that. */
1596 if (!fix_sync_read_error(r1_bio
))
1599 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
1600 if (process_checks(r1_bio
) < 0)
1605 atomic_set(&r1_bio
->remaining
, 1);
1606 for (i
= 0; i
< disks
; i
++) {
1607 wbio
= r1_bio
->bios
[i
];
1608 if (wbio
->bi_end_io
== NULL
||
1609 (wbio
->bi_end_io
== end_sync_read
&&
1610 (i
== r1_bio
->read_disk
||
1611 !test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))))
1614 wbio
->bi_rw
= WRITE
;
1615 wbio
->bi_end_io
= end_sync_write
;
1616 atomic_inc(&r1_bio
->remaining
);
1617 md_sync_acct(conf
->mirrors
[i
].rdev
->bdev
, wbio
->bi_size
>> 9);
1619 generic_make_request(wbio
);
1622 if (atomic_dec_and_test(&r1_bio
->remaining
)) {
1623 /* if we're here, all write(s) have completed, so clean up */
1624 md_done_sync(mddev
, r1_bio
->sectors
, 1);
1630 * This is a kernel thread which:
1632 * 1. Retries failed read operations on working mirrors.
1633 * 2. Updates the raid superblock when problems encounter.
1634 * 3. Performs writes following reads for array synchronising.
1637 static void fix_read_error(conf_t
*conf
, int read_disk
,
1638 sector_t sect
, int sectors
)
1640 mddev_t
*mddev
= conf
->mddev
;
1648 if (s
> (PAGE_SIZE
>>9))
1652 /* Note: no rcu protection needed here
1653 * as this is synchronous in the raid1d thread
1654 * which is the thread that might remove
1655 * a device. If raid1d ever becomes multi-threaded....
1660 rdev
= conf
->mirrors
[d
].rdev
;
1662 test_bit(In_sync
, &rdev
->flags
) &&
1663 is_badblock(rdev
, sect
, s
,
1664 &first_bad
, &bad_sectors
) == 0 &&
1665 sync_page_io(rdev
, sect
, s
<<9,
1666 conf
->tmppage
, READ
, false))
1670 if (d
== conf
->raid_disks
)
1673 } while (!success
&& d
!= read_disk
);
1676 /* Cannot read from anywhere -- bye bye array */
1677 md_error(mddev
, conf
->mirrors
[read_disk
].rdev
);
1680 /* write it back and re-read */
1682 while (d
!= read_disk
) {
1684 d
= conf
->raid_disks
;
1686 rdev
= conf
->mirrors
[d
].rdev
;
1688 test_bit(In_sync
, &rdev
->flags
)) {
1689 if (sync_page_io(rdev
, sect
, s
<<9,
1690 conf
->tmppage
, WRITE
, false)
1692 /* Well, this device is dead */
1693 md_error(mddev
, rdev
);
1697 while (d
!= read_disk
) {
1698 char b
[BDEVNAME_SIZE
];
1700 d
= conf
->raid_disks
;
1702 rdev
= conf
->mirrors
[d
].rdev
;
1704 test_bit(In_sync
, &rdev
->flags
)) {
1705 if (sync_page_io(rdev
, sect
, s
<<9,
1706 conf
->tmppage
, READ
, false)
1708 /* Well, this device is dead */
1709 md_error(mddev
, rdev
);
1711 atomic_add(s
, &rdev
->corrected_errors
);
1713 "md/raid1:%s: read error corrected "
1714 "(%d sectors at %llu on %s)\n",
1716 (unsigned long long)(sect
+
1718 bdevname(rdev
->bdev
, b
));
1727 static void raid1d(mddev_t
*mddev
)
1731 unsigned long flags
;
1732 conf_t
*conf
= mddev
->private;
1733 struct list_head
*head
= &conf
->retry_list
;
1735 struct blk_plug plug
;
1737 md_check_recovery(mddev
);
1739 blk_start_plug(&plug
);
1741 char b
[BDEVNAME_SIZE
];
1743 if (atomic_read(&mddev
->plug_cnt
) == 0)
1744 flush_pending_writes(conf
);
1746 spin_lock_irqsave(&conf
->device_lock
, flags
);
1747 if (list_empty(head
)) {
1748 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1751 r1_bio
= list_entry(head
->prev
, r1bio_t
, retry_list
);
1752 list_del(head
->prev
);
1754 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1756 mddev
= r1_bio
->mddev
;
1757 conf
= mddev
->private;
1758 if (test_bit(R1BIO_IsSync
, &r1_bio
->state
)) {
1759 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
)) {
1761 int s
= r1_bio
->sectors
;
1762 for (m
= 0; m
< conf
->raid_disks
; m
++) {
1763 struct bio
*bio
= r1_bio
->bios
[m
];
1764 if (bio
->bi_end_io
!= NULL
&&
1765 test_bit(BIO_UPTODATE
,
1767 rdev
= conf
->mirrors
[m
].rdev
;
1768 rdev_clear_badblocks(
1775 md_done_sync(mddev
, s
, 1);
1777 sync_request_write(mddev
, r1_bio
);
1778 } else if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
)) {
1780 for (m
= 0; m
< conf
->raid_disks
; m
++)
1781 if (r1_bio
->bios
[m
] == IO_MADE_GOOD
) {
1782 rdev
= conf
->mirrors
[m
].rdev
;
1783 rdev_clear_badblocks(
1787 rdev_dec_pending(rdev
, mddev
);
1789 raid_end_bio_io(r1_bio
);
1790 } else if (test_bit(R1BIO_ReadError
, &r1_bio
->state
)) {
1794 clear_bit(R1BIO_ReadError
, &r1_bio
->state
);
1795 /* we got a read error. Maybe the drive is bad. Maybe just
1796 * the block and we can fix it.
1797 * We freeze all other IO, and try reading the block from
1798 * other devices. When we find one, we re-write
1799 * and check it that fixes the read error.
1800 * This is all done synchronously while the array is
1803 if (mddev
->ro
== 0) {
1805 fix_read_error(conf
, r1_bio
->read_disk
,
1808 unfreeze_array(conf
);
1811 conf
->mirrors
[r1_bio
->read_disk
].rdev
);
1813 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
1814 bdevname(bio
->bi_bdev
, b
);
1816 disk
= read_balance(conf
, r1_bio
, &max_sectors
);
1818 printk(KERN_ALERT
"md/raid1:%s: %s: unrecoverable I/O"
1819 " read error for block %llu\n",
1821 (unsigned long long)r1_bio
->sector
);
1822 raid_end_bio_io(r1_bio
);
1824 const unsigned long do_sync
= r1_bio
->master_bio
->bi_rw
& REQ_SYNC
;
1826 r1_bio
->bios
[r1_bio
->read_disk
] =
1827 mddev
->ro
? IO_BLOCKED
: NULL
;
1830 r1_bio
->read_disk
= disk
;
1831 bio
= bio_clone_mddev(r1_bio
->master_bio
,
1834 r1_bio
->sector
- bio
->bi_sector
,
1836 r1_bio
->bios
[r1_bio
->read_disk
] = bio
;
1837 rdev
= conf
->mirrors
[disk
].rdev
;
1840 "md/raid1:%s: redirecting sector %llu"
1841 " to other mirror: %s\n",
1843 (unsigned long long)r1_bio
->sector
,
1844 bdevname(rdev
->bdev
, b
));
1845 bio
->bi_sector
= r1_bio
->sector
+ rdev
->data_offset
;
1846 bio
->bi_bdev
= rdev
->bdev
;
1847 bio
->bi_end_io
= raid1_end_read_request
;
1848 bio
->bi_rw
= READ
| do_sync
;
1849 bio
->bi_private
= r1_bio
;
1850 if (max_sectors
< r1_bio
->sectors
) {
1851 /* Drat - have to split this up more */
1852 struct bio
*mbio
= r1_bio
->master_bio
;
1853 int sectors_handled
=
1854 r1_bio
->sector
+ max_sectors
1856 r1_bio
->sectors
= max_sectors
;
1857 spin_lock_irq(&conf
->device_lock
);
1858 if (mbio
->bi_phys_segments
== 0)
1859 mbio
->bi_phys_segments
= 2;
1861 mbio
->bi_phys_segments
++;
1862 spin_unlock_irq(&conf
->device_lock
);
1863 generic_make_request(bio
);
1866 r1_bio
= mempool_alloc(conf
->r1bio_pool
,
1869 r1_bio
->master_bio
= mbio
;
1870 r1_bio
->sectors
= (mbio
->bi_size
>> 9)
1873 set_bit(R1BIO_ReadError
,
1875 r1_bio
->mddev
= mddev
;
1876 r1_bio
->sector
= mbio
->bi_sector
1881 generic_make_request(bio
);
1884 /* just a partial read to be scheduled from separate
1887 generic_make_request(r1_bio
->bios
[r1_bio
->read_disk
]);
1890 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
))
1891 md_check_recovery(mddev
);
1893 blk_finish_plug(&plug
);
1897 static int init_resync(conf_t
*conf
)
1901 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
1902 BUG_ON(conf
->r1buf_pool
);
1903 conf
->r1buf_pool
= mempool_create(buffs
, r1buf_pool_alloc
, r1buf_pool_free
,
1905 if (!conf
->r1buf_pool
)
1907 conf
->next_resync
= 0;
1912 * perform a "sync" on one "block"
1914 * We need to make sure that no normal I/O request - particularly write
1915 * requests - conflict with active sync requests.
1917 * This is achieved by tracking pending requests and a 'barrier' concept
1918 * that can be installed to exclude normal IO requests.
1921 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1923 conf_t
*conf
= mddev
->private;
1926 sector_t max_sector
, nr_sectors
;
1930 int write_targets
= 0, read_targets
= 0;
1931 sector_t sync_blocks
;
1932 int still_degraded
= 0;
1933 int good_sectors
= RESYNC_SECTORS
;
1934 int min_bad
= 0; /* number of sectors that are bad in all devices */
1936 if (!conf
->r1buf_pool
)
1937 if (init_resync(conf
))
1940 max_sector
= mddev
->dev_sectors
;
1941 if (sector_nr
>= max_sector
) {
1942 /* If we aborted, we need to abort the
1943 * sync on the 'current' bitmap chunk (there will
1944 * only be one in raid1 resync.
1945 * We can find the current addess in mddev->curr_resync
1947 if (mddev
->curr_resync
< max_sector
) /* aborted */
1948 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1950 else /* completed sync */
1953 bitmap_close_sync(mddev
->bitmap
);
1958 if (mddev
->bitmap
== NULL
&&
1959 mddev
->recovery_cp
== MaxSector
&&
1960 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
1961 conf
->fullsync
== 0) {
1963 return max_sector
- sector_nr
;
1965 /* before building a request, check if we can skip these blocks..
1966 * This call the bitmap_start_sync doesn't actually record anything
1968 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
1969 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
1970 /* We can skip this block, and probably several more */
1975 * If there is non-resync activity waiting for a turn,
1976 * and resync is going fast enough,
1977 * then let it though before starting on this new sync request.
1979 if (!go_faster
&& conf
->nr_waiting
)
1980 msleep_interruptible(1000);
1982 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
1983 r1_bio
= mempool_alloc(conf
->r1buf_pool
, GFP_NOIO
);
1984 raise_barrier(conf
);
1986 conf
->next_resync
= sector_nr
;
1990 * If we get a correctably read error during resync or recovery,
1991 * we might want to read from a different device. So we
1992 * flag all drives that could conceivably be read from for READ,
1993 * and any others (which will be non-In_sync devices) for WRITE.
1994 * If a read fails, we try reading from something else for which READ
1998 r1_bio
->mddev
= mddev
;
1999 r1_bio
->sector
= sector_nr
;
2001 set_bit(R1BIO_IsSync
, &r1_bio
->state
);
2003 for (i
=0; i
< conf
->raid_disks
; i
++) {
2005 bio
= r1_bio
->bios
[i
];
2007 /* take from bio_init */
2008 bio
->bi_next
= NULL
;
2009 bio
->bi_flags
&= ~(BIO_POOL_MASK
-1);
2010 bio
->bi_flags
|= 1 << BIO_UPTODATE
;
2011 bio
->bi_comp_cpu
= -1;
2015 bio
->bi_phys_segments
= 0;
2017 bio
->bi_end_io
= NULL
;
2018 bio
->bi_private
= NULL
;
2020 rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
2022 test_bit(Faulty
, &rdev
->flags
)) {
2024 } else if (!test_bit(In_sync
, &rdev
->flags
)) {
2026 bio
->bi_end_io
= end_sync_write
;
2029 /* may need to read from here */
2030 sector_t first_bad
= MaxSector
;
2033 if (is_badblock(rdev
, sector_nr
, good_sectors
,
2034 &first_bad
, &bad_sectors
)) {
2035 if (first_bad
> sector_nr
)
2036 good_sectors
= first_bad
- sector_nr
;
2038 bad_sectors
-= (sector_nr
- first_bad
);
2040 min_bad
> bad_sectors
)
2041 min_bad
= bad_sectors
;
2044 if (sector_nr
< first_bad
) {
2045 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2053 bio
->bi_end_io
= end_sync_read
;
2057 if (bio
->bi_end_io
) {
2058 atomic_inc(&rdev
->nr_pending
);
2059 bio
->bi_sector
= sector_nr
+ rdev
->data_offset
;
2060 bio
->bi_bdev
= rdev
->bdev
;
2061 bio
->bi_private
= r1_bio
;
2067 r1_bio
->read_disk
= disk
;
2069 if (read_targets
== 0 && min_bad
> 0) {
2070 /* These sectors are bad on all InSync devices, so we
2071 * need to mark them bad on all write targets
2074 for (i
= 0 ; i
< conf
->raid_disks
; i
++)
2075 if (r1_bio
->bios
[i
]->bi_end_io
== end_sync_write
) {
2077 rcu_dereference(conf
->mirrors
[i
].rdev
);
2078 ok
= rdev_set_badblocks(rdev
, sector_nr
,
2082 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2087 /* Cannot record the badblocks, so need to
2089 * If there are multiple read targets, could just
2090 * fail the really bad ones ???
2092 conf
->recovery_disabled
= mddev
->recovery_disabled
;
2093 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2099 if (min_bad
> 0 && min_bad
< good_sectors
) {
2100 /* only resync enough to reach the next bad->good
2102 good_sectors
= min_bad
;
2105 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) && read_targets
> 0)
2106 /* extra read targets are also write targets */
2107 write_targets
+= read_targets
-1;
2109 if (write_targets
== 0 || read_targets
== 0) {
2110 /* There is nowhere to write, so all non-sync
2111 * drives must be failed - so we are finished
2113 sector_t rv
= max_sector
- sector_nr
;
2119 if (max_sector
> mddev
->resync_max
)
2120 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
2121 if (max_sector
> sector_nr
+ good_sectors
)
2122 max_sector
= sector_nr
+ good_sectors
;
2127 int len
= PAGE_SIZE
;
2128 if (sector_nr
+ (len
>>9) > max_sector
)
2129 len
= (max_sector
- sector_nr
) << 9;
2132 if (sync_blocks
== 0) {
2133 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
2134 &sync_blocks
, still_degraded
) &&
2136 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2138 BUG_ON(sync_blocks
< (PAGE_SIZE
>>9));
2139 if ((len
>> 9) > sync_blocks
)
2140 len
= sync_blocks
<<9;
2143 for (i
=0 ; i
< conf
->raid_disks
; i
++) {
2144 bio
= r1_bio
->bios
[i
];
2145 if (bio
->bi_end_io
) {
2146 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
2147 if (bio_add_page(bio
, page
, len
, 0) == 0) {
2149 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2152 bio
= r1_bio
->bios
[i
];
2153 if (bio
->bi_end_io
==NULL
)
2155 /* remove last page from this bio */
2157 bio
->bi_size
-= len
;
2158 bio
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
2164 nr_sectors
+= len
>>9;
2165 sector_nr
+= len
>>9;
2166 sync_blocks
-= (len
>>9);
2167 } while (r1_bio
->bios
[disk
]->bi_vcnt
< RESYNC_PAGES
);
2169 r1_bio
->sectors
= nr_sectors
;
2171 /* For a user-requested sync, we read all readable devices and do a
2174 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
2175 atomic_set(&r1_bio
->remaining
, read_targets
);
2176 for (i
=0; i
<conf
->raid_disks
; i
++) {
2177 bio
= r1_bio
->bios
[i
];
2178 if (bio
->bi_end_io
== end_sync_read
) {
2179 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2180 generic_make_request(bio
);
2184 atomic_set(&r1_bio
->remaining
, 1);
2185 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
2186 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2187 generic_make_request(bio
);
2193 static sector_t
raid1_size(mddev_t
*mddev
, sector_t sectors
, int raid_disks
)
2198 return mddev
->dev_sectors
;
2201 static conf_t
*setup_conf(mddev_t
*mddev
)
2205 mirror_info_t
*disk
;
2209 conf
= kzalloc(sizeof(conf_t
), GFP_KERNEL
);
2213 conf
->mirrors
= kzalloc(sizeof(struct mirror_info
)*mddev
->raid_disks
,
2218 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2222 conf
->poolinfo
= kzalloc(sizeof(*conf
->poolinfo
), GFP_KERNEL
);
2223 if (!conf
->poolinfo
)
2225 conf
->poolinfo
->raid_disks
= mddev
->raid_disks
;
2226 conf
->r1bio_pool
= mempool_create(NR_RAID1_BIOS
, r1bio_pool_alloc
,
2229 if (!conf
->r1bio_pool
)
2232 conf
->poolinfo
->mddev
= mddev
;
2234 spin_lock_init(&conf
->device_lock
);
2235 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2236 int disk_idx
= rdev
->raid_disk
;
2237 if (disk_idx
>= mddev
->raid_disks
2240 disk
= conf
->mirrors
+ disk_idx
;
2244 disk
->head_position
= 0;
2246 conf
->raid_disks
= mddev
->raid_disks
;
2247 conf
->mddev
= mddev
;
2248 INIT_LIST_HEAD(&conf
->retry_list
);
2250 spin_lock_init(&conf
->resync_lock
);
2251 init_waitqueue_head(&conf
->wait_barrier
);
2253 bio_list_init(&conf
->pending_bio_list
);
2255 conf
->last_used
= -1;
2256 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2258 disk
= conf
->mirrors
+ i
;
2261 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2262 disk
->head_position
= 0;
2265 } else if (conf
->last_used
< 0)
2267 * The first working device is used as a
2268 * starting point to read balancing.
2270 conf
->last_used
= i
;
2274 if (conf
->last_used
< 0) {
2275 printk(KERN_ERR
"md/raid1:%s: no operational mirrors\n",
2280 conf
->thread
= md_register_thread(raid1d
, mddev
, NULL
);
2281 if (!conf
->thread
) {
2283 "md/raid1:%s: couldn't allocate thread\n",
2292 if (conf
->r1bio_pool
)
2293 mempool_destroy(conf
->r1bio_pool
);
2294 kfree(conf
->mirrors
);
2295 safe_put_page(conf
->tmppage
);
2296 kfree(conf
->poolinfo
);
2299 return ERR_PTR(err
);
2302 static int run(mddev_t
*mddev
)
2308 if (mddev
->level
!= 1) {
2309 printk(KERN_ERR
"md/raid1:%s: raid level not set to mirroring (%d)\n",
2310 mdname(mddev
), mddev
->level
);
2313 if (mddev
->reshape_position
!= MaxSector
) {
2314 printk(KERN_ERR
"md/raid1:%s: reshape_position set but not supported\n",
2319 * copy the already verified devices into our private RAID1
2320 * bookkeeping area. [whatever we allocate in run(),
2321 * should be freed in stop()]
2323 if (mddev
->private == NULL
)
2324 conf
= setup_conf(mddev
);
2326 conf
= mddev
->private;
2329 return PTR_ERR(conf
);
2331 list_for_each_entry(rdev
, &mddev
->disks
, same_set
) {
2332 if (!mddev
->gendisk
)
2334 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
2335 rdev
->data_offset
<< 9);
2336 /* as we don't honour merge_bvec_fn, we must never risk
2337 * violating it, so limit ->max_segments to 1 lying within
2338 * a single page, as a one page request is never in violation.
2340 if (rdev
->bdev
->bd_disk
->queue
->merge_bvec_fn
) {
2341 blk_queue_max_segments(mddev
->queue
, 1);
2342 blk_queue_segment_boundary(mddev
->queue
,
2343 PAGE_CACHE_SIZE
- 1);
2347 mddev
->degraded
= 0;
2348 for (i
=0; i
< conf
->raid_disks
; i
++)
2349 if (conf
->mirrors
[i
].rdev
== NULL
||
2350 !test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ||
2351 test_bit(Faulty
, &conf
->mirrors
[i
].rdev
->flags
))
2354 if (conf
->raid_disks
- mddev
->degraded
== 1)
2355 mddev
->recovery_cp
= MaxSector
;
2357 if (mddev
->recovery_cp
!= MaxSector
)
2358 printk(KERN_NOTICE
"md/raid1:%s: not clean"
2359 " -- starting background reconstruction\n",
2362 "md/raid1:%s: active with %d out of %d mirrors\n",
2363 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2367 * Ok, everything is just fine now
2369 mddev
->thread
= conf
->thread
;
2370 conf
->thread
= NULL
;
2371 mddev
->private = conf
;
2373 md_set_array_sectors(mddev
, raid1_size(mddev
, 0, 0));
2376 mddev
->queue
->backing_dev_info
.congested_fn
= raid1_congested
;
2377 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2379 return md_integrity_register(mddev
);
2382 static int stop(mddev_t
*mddev
)
2384 conf_t
*conf
= mddev
->private;
2385 struct bitmap
*bitmap
= mddev
->bitmap
;
2387 /* wait for behind writes to complete */
2388 if (bitmap
&& atomic_read(&bitmap
->behind_writes
) > 0) {
2389 printk(KERN_INFO
"md/raid1:%s: behind writes in progress - waiting to stop.\n",
2391 /* need to kick something here to make sure I/O goes? */
2392 wait_event(bitmap
->behind_wait
,
2393 atomic_read(&bitmap
->behind_writes
) == 0);
2396 raise_barrier(conf
);
2397 lower_barrier(conf
);
2399 md_unregister_thread(mddev
->thread
);
2400 mddev
->thread
= NULL
;
2401 if (conf
->r1bio_pool
)
2402 mempool_destroy(conf
->r1bio_pool
);
2403 kfree(conf
->mirrors
);
2404 kfree(conf
->poolinfo
);
2406 mddev
->private = NULL
;
2410 static int raid1_resize(mddev_t
*mddev
, sector_t sectors
)
2412 /* no resync is happening, and there is enough space
2413 * on all devices, so we can resize.
2414 * We need to make sure resync covers any new space.
2415 * If the array is shrinking we should possibly wait until
2416 * any io in the removed space completes, but it hardly seems
2419 md_set_array_sectors(mddev
, raid1_size(mddev
, sectors
, 0));
2420 if (mddev
->array_sectors
> raid1_size(mddev
, sectors
, 0))
2422 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
2423 revalidate_disk(mddev
->gendisk
);
2424 if (sectors
> mddev
->dev_sectors
&&
2425 mddev
->recovery_cp
> mddev
->dev_sectors
) {
2426 mddev
->recovery_cp
= mddev
->dev_sectors
;
2427 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2429 mddev
->dev_sectors
= sectors
;
2430 mddev
->resync_max_sectors
= sectors
;
2434 static int raid1_reshape(mddev_t
*mddev
)
2437 * 1/ resize the r1bio_pool
2438 * 2/ resize conf->mirrors
2440 * We allocate a new r1bio_pool if we can.
2441 * Then raise a device barrier and wait until all IO stops.
2442 * Then resize conf->mirrors and swap in the new r1bio pool.
2444 * At the same time, we "pack" the devices so that all the missing
2445 * devices have the higher raid_disk numbers.
2447 mempool_t
*newpool
, *oldpool
;
2448 struct pool_info
*newpoolinfo
;
2449 mirror_info_t
*newmirrors
;
2450 conf_t
*conf
= mddev
->private;
2451 int cnt
, raid_disks
;
2452 unsigned long flags
;
2455 /* Cannot change chunk_size, layout, or level */
2456 if (mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
||
2457 mddev
->layout
!= mddev
->new_layout
||
2458 mddev
->level
!= mddev
->new_level
) {
2459 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
2460 mddev
->new_layout
= mddev
->layout
;
2461 mddev
->new_level
= mddev
->level
;
2465 err
= md_allow_write(mddev
);
2469 raid_disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
2471 if (raid_disks
< conf
->raid_disks
) {
2473 for (d
= 0; d
< conf
->raid_disks
; d
++)
2474 if (conf
->mirrors
[d
].rdev
)
2476 if (cnt
> raid_disks
)
2480 newpoolinfo
= kmalloc(sizeof(*newpoolinfo
), GFP_KERNEL
);
2483 newpoolinfo
->mddev
= mddev
;
2484 newpoolinfo
->raid_disks
= raid_disks
;
2486 newpool
= mempool_create(NR_RAID1_BIOS
, r1bio_pool_alloc
,
2487 r1bio_pool_free
, newpoolinfo
);
2492 newmirrors
= kzalloc(sizeof(struct mirror_info
) * raid_disks
, GFP_KERNEL
);
2495 mempool_destroy(newpool
);
2499 raise_barrier(conf
);
2501 /* ok, everything is stopped */
2502 oldpool
= conf
->r1bio_pool
;
2503 conf
->r1bio_pool
= newpool
;
2505 for (d
= d2
= 0; d
< conf
->raid_disks
; d
++) {
2506 mdk_rdev_t
*rdev
= conf
->mirrors
[d
].rdev
;
2507 if (rdev
&& rdev
->raid_disk
!= d2
) {
2508 sysfs_unlink_rdev(mddev
, rdev
);
2509 rdev
->raid_disk
= d2
;
2510 sysfs_unlink_rdev(mddev
, rdev
);
2511 if (sysfs_link_rdev(mddev
, rdev
))
2513 "md/raid1:%s: cannot register rd%d\n",
2514 mdname(mddev
), rdev
->raid_disk
);
2517 newmirrors
[d2
++].rdev
= rdev
;
2519 kfree(conf
->mirrors
);
2520 conf
->mirrors
= newmirrors
;
2521 kfree(conf
->poolinfo
);
2522 conf
->poolinfo
= newpoolinfo
;
2524 spin_lock_irqsave(&conf
->device_lock
, flags
);
2525 mddev
->degraded
+= (raid_disks
- conf
->raid_disks
);
2526 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2527 conf
->raid_disks
= mddev
->raid_disks
= raid_disks
;
2528 mddev
->delta_disks
= 0;
2530 conf
->last_used
= 0; /* just make sure it is in-range */
2531 lower_barrier(conf
);
2533 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2534 md_wakeup_thread(mddev
->thread
);
2536 mempool_destroy(oldpool
);
2540 static void raid1_quiesce(mddev_t
*mddev
, int state
)
2542 conf_t
*conf
= mddev
->private;
2545 case 2: /* wake for suspend */
2546 wake_up(&conf
->wait_barrier
);
2549 raise_barrier(conf
);
2552 lower_barrier(conf
);
2557 static void *raid1_takeover(mddev_t
*mddev
)
2559 /* raid1 can take over:
2560 * raid5 with 2 devices, any layout or chunk size
2562 if (mddev
->level
== 5 && mddev
->raid_disks
== 2) {
2564 mddev
->new_level
= 1;
2565 mddev
->new_layout
= 0;
2566 mddev
->new_chunk_sectors
= 0;
2567 conf
= setup_conf(mddev
);
2572 return ERR_PTR(-EINVAL
);
2575 static struct mdk_personality raid1_personality
=
2579 .owner
= THIS_MODULE
,
2580 .make_request
= make_request
,
2584 .error_handler
= error
,
2585 .hot_add_disk
= raid1_add_disk
,
2586 .hot_remove_disk
= raid1_remove_disk
,
2587 .spare_active
= raid1_spare_active
,
2588 .sync_request
= sync_request
,
2589 .resize
= raid1_resize
,
2591 .check_reshape
= raid1_reshape
,
2592 .quiesce
= raid1_quiesce
,
2593 .takeover
= raid1_takeover
,
2596 static int __init
raid_init(void)
2598 return register_md_personality(&raid1_personality
);
2601 static void raid_exit(void)
2603 unregister_md_personality(&raid1_personality
);
2606 module_init(raid_init
);
2607 module_exit(raid_exit
);
2608 MODULE_LICENSE("GPL");
2609 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
2610 MODULE_ALIAS("md-personality-3"); /* RAID1 */
2611 MODULE_ALIAS("md-raid1");
2612 MODULE_ALIAS("md-level-1");