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 we get a read error on a read-only array, we redirect to another
50 * device without failing the first device, or trying to over-write to
51 * correct the read error. To keep track of bad blocks on a per-bio
52 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
54 #define IO_BLOCKED ((struct bio *)1)
55 /* When we successfully write to a known bad-block, we need to remove the
56 * bad-block marking which must be done from process context. So we record
57 * the success by setting devs[n].bio to IO_MADE_GOOD
59 #define IO_MADE_GOOD ((struct bio *)2)
61 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
63 /* When there are this many requests queue to be written by
64 * the raid1 thread, we become 'congested' to provide back-pressure
67 static int max_queued_requests
= 1024;
69 static void allow_barrier(struct r1conf
*conf
, sector_t start_next_window
,
71 static void lower_barrier(struct r1conf
*conf
);
73 static void * r1bio_pool_alloc(gfp_t gfp_flags
, void *data
)
75 struct pool_info
*pi
= data
;
76 int size
= offsetof(struct r1bio
, bios
[pi
->raid_disks
]);
78 /* allocate a r1bio with room for raid_disks entries in the bios array */
79 return kzalloc(size
, gfp_flags
);
82 static void r1bio_pool_free(void *r1_bio
, void *data
)
87 #define RESYNC_BLOCK_SIZE (64*1024)
88 #define RESYNC_DEPTH 32
89 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
90 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
91 #define RESYNC_WINDOW (RESYNC_BLOCK_SIZE * RESYNC_DEPTH)
92 #define RESYNC_WINDOW_SECTORS (RESYNC_WINDOW >> 9)
93 #define NEXT_NORMALIO_DISTANCE (3 * RESYNC_WINDOW_SECTORS)
95 static void * r1buf_pool_alloc(gfp_t gfp_flags
, void *data
)
97 struct pool_info
*pi
= data
;
103 r1_bio
= r1bio_pool_alloc(gfp_flags
, pi
);
108 * Allocate bios : 1 for reading, n-1 for writing
110 for (j
= pi
->raid_disks
; j
-- ; ) {
111 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
114 r1_bio
->bios
[j
] = bio
;
117 * Allocate RESYNC_PAGES data pages and attach them to
119 * If this is a user-requested check/repair, allocate
120 * RESYNC_PAGES for each bio.
122 if (test_bit(MD_RECOVERY_REQUESTED
, &pi
->mddev
->recovery
))
123 need_pages
= pi
->raid_disks
;
126 for (j
= 0; j
< need_pages
; j
++) {
127 bio
= r1_bio
->bios
[j
];
128 bio
->bi_vcnt
= RESYNC_PAGES
;
130 if (bio_alloc_pages(bio
, gfp_flags
))
133 /* If not user-requests, copy the page pointers to all bios */
134 if (!test_bit(MD_RECOVERY_REQUESTED
, &pi
->mddev
->recovery
)) {
135 for (i
=0; i
<RESYNC_PAGES
; i
++)
136 for (j
=1; j
<pi
->raid_disks
; j
++)
137 r1_bio
->bios
[j
]->bi_io_vec
[i
].bv_page
=
138 r1_bio
->bios
[0]->bi_io_vec
[i
].bv_page
;
141 r1_bio
->master_bio
= NULL
;
149 bio_for_each_segment_all(bv
, r1_bio
->bios
[j
], i
)
150 __free_page(bv
->bv_page
);
154 while (++j
< pi
->raid_disks
)
155 bio_put(r1_bio
->bios
[j
]);
156 r1bio_pool_free(r1_bio
, data
);
160 static void r1buf_pool_free(void *__r1_bio
, void *data
)
162 struct pool_info
*pi
= data
;
164 struct r1bio
*r1bio
= __r1_bio
;
166 for (i
= 0; i
< RESYNC_PAGES
; i
++)
167 for (j
= pi
->raid_disks
; j
-- ;) {
169 r1bio
->bios
[j
]->bi_io_vec
[i
].bv_page
!=
170 r1bio
->bios
[0]->bi_io_vec
[i
].bv_page
)
171 safe_put_page(r1bio
->bios
[j
]->bi_io_vec
[i
].bv_page
);
173 for (i
=0 ; i
< pi
->raid_disks
; i
++)
174 bio_put(r1bio
->bios
[i
]);
176 r1bio_pool_free(r1bio
, data
);
179 static void put_all_bios(struct r1conf
*conf
, struct r1bio
*r1_bio
)
183 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
184 struct bio
**bio
= r1_bio
->bios
+ i
;
185 if (!BIO_SPECIAL(*bio
))
191 static void free_r1bio(struct r1bio
*r1_bio
)
193 struct r1conf
*conf
= r1_bio
->mddev
->private;
195 put_all_bios(conf
, r1_bio
);
196 mempool_free(r1_bio
, conf
->r1bio_pool
);
199 static void put_buf(struct r1bio
*r1_bio
)
201 struct r1conf
*conf
= r1_bio
->mddev
->private;
204 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
205 struct bio
*bio
= r1_bio
->bios
[i
];
207 rdev_dec_pending(conf
->mirrors
[i
].rdev
, r1_bio
->mddev
);
210 mempool_free(r1_bio
, conf
->r1buf_pool
);
215 static void reschedule_retry(struct r1bio
*r1_bio
)
218 struct mddev
*mddev
= r1_bio
->mddev
;
219 struct r1conf
*conf
= mddev
->private;
221 spin_lock_irqsave(&conf
->device_lock
, flags
);
222 list_add(&r1_bio
->retry_list
, &conf
->retry_list
);
224 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
226 wake_up(&conf
->wait_barrier
);
227 md_wakeup_thread(mddev
->thread
);
231 * raid_end_bio_io() is called when we have finished servicing a mirrored
232 * operation and are ready to return a success/failure code to the buffer
235 static void call_bio_endio(struct r1bio
*r1_bio
)
237 struct bio
*bio
= r1_bio
->master_bio
;
239 struct r1conf
*conf
= r1_bio
->mddev
->private;
240 sector_t start_next_window
= r1_bio
->start_next_window
;
241 sector_t bi_sector
= bio
->bi_iter
.bi_sector
;
243 if (bio
->bi_phys_segments
) {
245 spin_lock_irqsave(&conf
->device_lock
, flags
);
246 bio
->bi_phys_segments
--;
247 done
= (bio
->bi_phys_segments
== 0);
248 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
250 * make_request() might be waiting for
251 * bi_phys_segments to decrease
253 wake_up(&conf
->wait_barrier
);
257 if (!test_bit(R1BIO_Uptodate
, &r1_bio
->state
))
258 clear_bit(BIO_UPTODATE
, &bio
->bi_flags
);
262 * Wake up any possible resync thread that waits for the device
265 allow_barrier(conf
, start_next_window
, bi_sector
);
269 static void raid_end_bio_io(struct r1bio
*r1_bio
)
271 struct bio
*bio
= r1_bio
->master_bio
;
273 /* if nobody has done the final endio yet, do it now */
274 if (!test_and_set_bit(R1BIO_Returned
, &r1_bio
->state
)) {
275 pr_debug("raid1: sync end %s on sectors %llu-%llu\n",
276 (bio_data_dir(bio
) == WRITE
) ? "write" : "read",
277 (unsigned long long) bio
->bi_iter
.bi_sector
,
278 (unsigned long long) bio_end_sector(bio
) - 1);
280 call_bio_endio(r1_bio
);
286 * Update disk head position estimator based on IRQ completion info.
288 static inline void update_head_pos(int disk
, struct r1bio
*r1_bio
)
290 struct r1conf
*conf
= r1_bio
->mddev
->private;
292 conf
->mirrors
[disk
].head_position
=
293 r1_bio
->sector
+ (r1_bio
->sectors
);
297 * Find the disk number which triggered given bio
299 static int find_bio_disk(struct r1bio
*r1_bio
, struct bio
*bio
)
302 struct r1conf
*conf
= r1_bio
->mddev
->private;
303 int raid_disks
= conf
->raid_disks
;
305 for (mirror
= 0; mirror
< raid_disks
* 2; mirror
++)
306 if (r1_bio
->bios
[mirror
] == bio
)
309 BUG_ON(mirror
== raid_disks
* 2);
310 update_head_pos(mirror
, r1_bio
);
315 static void raid1_end_read_request(struct bio
*bio
, int error
)
317 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
318 struct r1bio
*r1_bio
= bio
->bi_private
;
320 struct r1conf
*conf
= r1_bio
->mddev
->private;
322 mirror
= r1_bio
->read_disk
;
324 * this branch is our 'one mirror IO has finished' event handler:
326 update_head_pos(mirror
, r1_bio
);
329 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
331 /* If all other devices have failed, we want to return
332 * the error upwards rather than fail the last device.
333 * Here we redefine "uptodate" to mean "Don't want to retry"
336 spin_lock_irqsave(&conf
->device_lock
, flags
);
337 if (r1_bio
->mddev
->degraded
== conf
->raid_disks
||
338 (r1_bio
->mddev
->degraded
== conf
->raid_disks
-1 &&
339 test_bit(In_sync
, &conf
->mirrors
[mirror
].rdev
->flags
)))
341 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
345 raid_end_bio_io(r1_bio
);
346 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
, conf
->mddev
);
351 char b
[BDEVNAME_SIZE
];
353 KERN_ERR
"md/raid1:%s: %s: "
354 "rescheduling sector %llu\n",
356 bdevname(conf
->mirrors
[mirror
].rdev
->bdev
,
358 (unsigned long long)r1_bio
->sector
);
359 set_bit(R1BIO_ReadError
, &r1_bio
->state
);
360 reschedule_retry(r1_bio
);
361 /* don't drop the reference on read_disk yet */
365 static void close_write(struct r1bio
*r1_bio
)
367 /* it really is the end of this request */
368 if (test_bit(R1BIO_BehindIO
, &r1_bio
->state
)) {
369 /* free extra copy of the data pages */
370 int i
= r1_bio
->behind_page_count
;
372 safe_put_page(r1_bio
->behind_bvecs
[i
].bv_page
);
373 kfree(r1_bio
->behind_bvecs
);
374 r1_bio
->behind_bvecs
= NULL
;
376 /* clear the bitmap if all writes complete successfully */
377 bitmap_endwrite(r1_bio
->mddev
->bitmap
, r1_bio
->sector
,
379 !test_bit(R1BIO_Degraded
, &r1_bio
->state
),
380 test_bit(R1BIO_BehindIO
, &r1_bio
->state
));
381 md_write_end(r1_bio
->mddev
);
384 static void r1_bio_write_done(struct r1bio
*r1_bio
)
386 if (!atomic_dec_and_test(&r1_bio
->remaining
))
389 if (test_bit(R1BIO_WriteError
, &r1_bio
->state
))
390 reschedule_retry(r1_bio
);
393 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
))
394 reschedule_retry(r1_bio
);
396 raid_end_bio_io(r1_bio
);
400 static void raid1_end_write_request(struct bio
*bio
, int error
)
402 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
403 struct r1bio
*r1_bio
= bio
->bi_private
;
404 int mirror
, behind
= test_bit(R1BIO_BehindIO
, &r1_bio
->state
);
405 struct r1conf
*conf
= r1_bio
->mddev
->private;
406 struct bio
*to_put
= NULL
;
408 mirror
= find_bio_disk(r1_bio
, bio
);
411 * 'one mirror IO has finished' event handler:
414 set_bit(WriteErrorSeen
,
415 &conf
->mirrors
[mirror
].rdev
->flags
);
416 if (!test_and_set_bit(WantReplacement
,
417 &conf
->mirrors
[mirror
].rdev
->flags
))
418 set_bit(MD_RECOVERY_NEEDED
, &
419 conf
->mddev
->recovery
);
421 set_bit(R1BIO_WriteError
, &r1_bio
->state
);
424 * Set R1BIO_Uptodate in our master bio, so that we
425 * will return a good error code for to the higher
426 * levels even if IO on some other mirrored buffer
429 * The 'master' represents the composite IO operation
430 * to user-side. So if something waits for IO, then it
431 * will wait for the 'master' bio.
436 r1_bio
->bios
[mirror
] = NULL
;
439 * Do not set R1BIO_Uptodate if the current device is
440 * rebuilding or Faulty. This is because we cannot use
441 * such device for properly reading the data back (we could
442 * potentially use it, if the current write would have felt
443 * before rdev->recovery_offset, but for simplicity we don't
446 if (test_bit(In_sync
, &conf
->mirrors
[mirror
].rdev
->flags
) &&
447 !test_bit(Faulty
, &conf
->mirrors
[mirror
].rdev
->flags
))
448 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
450 /* Maybe we can clear some bad blocks. */
451 if (is_badblock(conf
->mirrors
[mirror
].rdev
,
452 r1_bio
->sector
, r1_bio
->sectors
,
453 &first_bad
, &bad_sectors
)) {
454 r1_bio
->bios
[mirror
] = IO_MADE_GOOD
;
455 set_bit(R1BIO_MadeGood
, &r1_bio
->state
);
460 if (test_bit(WriteMostly
, &conf
->mirrors
[mirror
].rdev
->flags
))
461 atomic_dec(&r1_bio
->behind_remaining
);
464 * In behind mode, we ACK the master bio once the I/O
465 * has safely reached all non-writemostly
466 * disks. Setting the Returned bit ensures that this
467 * gets done only once -- we don't ever want to return
468 * -EIO here, instead we'll wait
470 if (atomic_read(&r1_bio
->behind_remaining
) >= (atomic_read(&r1_bio
->remaining
)-1) &&
471 test_bit(R1BIO_Uptodate
, &r1_bio
->state
)) {
472 /* Maybe we can return now */
473 if (!test_and_set_bit(R1BIO_Returned
, &r1_bio
->state
)) {
474 struct bio
*mbio
= r1_bio
->master_bio
;
475 pr_debug("raid1: behind end write sectors"
477 (unsigned long long) mbio
->bi_iter
.bi_sector
,
478 (unsigned long long) bio_end_sector(mbio
) - 1);
479 call_bio_endio(r1_bio
);
483 if (r1_bio
->bios
[mirror
] == NULL
)
484 rdev_dec_pending(conf
->mirrors
[mirror
].rdev
,
488 * Let's see if all mirrored write operations have finished
491 r1_bio_write_done(r1_bio
);
499 * This routine returns the disk from which the requested read should
500 * be done. There is a per-array 'next expected sequential IO' sector
501 * number - if this matches on the next IO then we use the last disk.
502 * There is also a per-disk 'last know head position' sector that is
503 * maintained from IRQ contexts, both the normal and the resync IO
504 * completion handlers update this position correctly. If there is no
505 * perfect sequential match then we pick the disk whose head is closest.
507 * If there are 2 mirrors in the same 2 devices, performance degrades
508 * because position is mirror, not device based.
510 * The rdev for the device selected will have nr_pending incremented.
512 static int read_balance(struct r1conf
*conf
, struct r1bio
*r1_bio
, int *max_sectors
)
514 const sector_t this_sector
= r1_bio
->sector
;
516 int best_good_sectors
;
517 int best_disk
, best_dist_disk
, best_pending_disk
;
521 unsigned int min_pending
;
522 struct md_rdev
*rdev
;
524 int choose_next_idle
;
528 * Check if we can balance. We can balance on the whole
529 * device if no resync is going on, or below the resync window.
530 * We take the first readable disk when above the resync window.
533 sectors
= r1_bio
->sectors
;
536 best_dist
= MaxSector
;
537 best_pending_disk
= -1;
538 min_pending
= UINT_MAX
;
539 best_good_sectors
= 0;
541 choose_next_idle
= 0;
543 choose_first
= (conf
->mddev
->recovery_cp
< this_sector
+ sectors
);
545 for (disk
= 0 ; disk
< conf
->raid_disks
* 2 ; disk
++) {
549 unsigned int pending
;
552 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
553 if (r1_bio
->bios
[disk
] == IO_BLOCKED
555 || test_bit(Unmerged
, &rdev
->flags
)
556 || test_bit(Faulty
, &rdev
->flags
))
558 if (!test_bit(In_sync
, &rdev
->flags
) &&
559 rdev
->recovery_offset
< this_sector
+ sectors
)
561 if (test_bit(WriteMostly
, &rdev
->flags
)) {
562 /* Don't balance among write-mostly, just
563 * use the first as a last resort */
564 if (best_dist_disk
< 0) {
565 if (is_badblock(rdev
, this_sector
, sectors
,
566 &first_bad
, &bad_sectors
)) {
567 if (first_bad
< this_sector
)
568 /* Cannot use this */
570 best_good_sectors
= first_bad
- this_sector
;
572 best_good_sectors
= sectors
;
573 best_dist_disk
= disk
;
574 best_pending_disk
= disk
;
578 /* This is a reasonable device to use. It might
581 if (is_badblock(rdev
, this_sector
, sectors
,
582 &first_bad
, &bad_sectors
)) {
583 if (best_dist
< MaxSector
)
584 /* already have a better device */
586 if (first_bad
<= this_sector
) {
587 /* cannot read here. If this is the 'primary'
588 * device, then we must not read beyond
589 * bad_sectors from another device..
591 bad_sectors
-= (this_sector
- first_bad
);
592 if (choose_first
&& sectors
> bad_sectors
)
593 sectors
= bad_sectors
;
594 if (best_good_sectors
> sectors
)
595 best_good_sectors
= sectors
;
598 sector_t good_sectors
= first_bad
- this_sector
;
599 if (good_sectors
> best_good_sectors
) {
600 best_good_sectors
= good_sectors
;
608 best_good_sectors
= sectors
;
610 nonrot
= blk_queue_nonrot(bdev_get_queue(rdev
->bdev
));
611 has_nonrot_disk
|= nonrot
;
612 pending
= atomic_read(&rdev
->nr_pending
);
613 dist
= abs(this_sector
- conf
->mirrors
[disk
].head_position
);
618 /* Don't change to another disk for sequential reads */
619 if (conf
->mirrors
[disk
].next_seq_sect
== this_sector
621 int opt_iosize
= bdev_io_opt(rdev
->bdev
) >> 9;
622 struct raid1_info
*mirror
= &conf
->mirrors
[disk
];
626 * If buffered sequential IO size exceeds optimal
627 * iosize, check if there is idle disk. If yes, choose
628 * the idle disk. read_balance could already choose an
629 * idle disk before noticing it's a sequential IO in
630 * this disk. This doesn't matter because this disk
631 * will idle, next time it will be utilized after the
632 * first disk has IO size exceeds optimal iosize. In
633 * this way, iosize of the first disk will be optimal
634 * iosize at least. iosize of the second disk might be
635 * small, but not a big deal since when the second disk
636 * starts IO, the first disk is likely still busy.
638 if (nonrot
&& opt_iosize
> 0 &&
639 mirror
->seq_start
!= MaxSector
&&
640 mirror
->next_seq_sect
> opt_iosize
&&
641 mirror
->next_seq_sect
- opt_iosize
>=
643 choose_next_idle
= 1;
648 /* If device is idle, use it */
654 if (choose_next_idle
)
657 if (min_pending
> pending
) {
658 min_pending
= pending
;
659 best_pending_disk
= disk
;
662 if (dist
< best_dist
) {
664 best_dist_disk
= disk
;
669 * If all disks are rotational, choose the closest disk. If any disk is
670 * non-rotational, choose the disk with less pending request even the
671 * disk is rotational, which might/might not be optimal for raids with
672 * mixed ratation/non-rotational disks depending on workload.
674 if (best_disk
== -1) {
676 best_disk
= best_pending_disk
;
678 best_disk
= best_dist_disk
;
681 if (best_disk
>= 0) {
682 rdev
= rcu_dereference(conf
->mirrors
[best_disk
].rdev
);
685 atomic_inc(&rdev
->nr_pending
);
686 if (test_bit(Faulty
, &rdev
->flags
)) {
687 /* cannot risk returning a device that failed
688 * before we inc'ed nr_pending
690 rdev_dec_pending(rdev
, conf
->mddev
);
693 sectors
= best_good_sectors
;
695 if (conf
->mirrors
[best_disk
].next_seq_sect
!= this_sector
)
696 conf
->mirrors
[best_disk
].seq_start
= this_sector
;
698 conf
->mirrors
[best_disk
].next_seq_sect
= this_sector
+ sectors
;
701 *max_sectors
= sectors
;
706 static int raid1_mergeable_bvec(struct request_queue
*q
,
707 struct bvec_merge_data
*bvm
,
708 struct bio_vec
*biovec
)
710 struct mddev
*mddev
= q
->queuedata
;
711 struct r1conf
*conf
= mddev
->private;
712 sector_t sector
= bvm
->bi_sector
+ get_start_sect(bvm
->bi_bdev
);
713 int max
= biovec
->bv_len
;
715 if (mddev
->merge_check_needed
) {
718 for (disk
= 0; disk
< conf
->raid_disks
* 2; disk
++) {
719 struct md_rdev
*rdev
= rcu_dereference(
720 conf
->mirrors
[disk
].rdev
);
721 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
722 struct request_queue
*q
=
723 bdev_get_queue(rdev
->bdev
);
724 if (q
->merge_bvec_fn
) {
725 bvm
->bi_sector
= sector
+
727 bvm
->bi_bdev
= rdev
->bdev
;
728 max
= min(max
, q
->merge_bvec_fn(
739 int md_raid1_congested(struct mddev
*mddev
, int bits
)
741 struct r1conf
*conf
= mddev
->private;
744 if ((bits
& (1 << BDI_async_congested
)) &&
745 conf
->pending_count
>= max_queued_requests
)
749 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
750 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
751 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
752 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
756 /* Note the '|| 1' - when read_balance prefers
757 * non-congested targets, it can be removed
759 if ((bits
& (1<<BDI_async_congested
)) || 1)
760 ret
|= bdi_congested(&q
->backing_dev_info
, bits
);
762 ret
&= bdi_congested(&q
->backing_dev_info
, bits
);
768 EXPORT_SYMBOL_GPL(md_raid1_congested
);
770 static int raid1_congested(void *data
, int bits
)
772 struct mddev
*mddev
= data
;
774 return mddev_congested(mddev
, bits
) ||
775 md_raid1_congested(mddev
, bits
);
778 static void flush_pending_writes(struct r1conf
*conf
)
780 /* Any writes that have been queued but are awaiting
781 * bitmap updates get flushed here.
783 spin_lock_irq(&conf
->device_lock
);
785 if (conf
->pending_bio_list
.head
) {
787 bio
= bio_list_get(&conf
->pending_bio_list
);
788 conf
->pending_count
= 0;
789 spin_unlock_irq(&conf
->device_lock
);
790 /* flush any pending bitmap writes to
791 * disk before proceeding w/ I/O */
792 bitmap_unplug(conf
->mddev
->bitmap
);
793 wake_up(&conf
->wait_barrier
);
795 while (bio
) { /* submit pending writes */
796 struct bio
*next
= bio
->bi_next
;
798 if (unlikely((bio
->bi_rw
& REQ_DISCARD
) &&
799 !blk_queue_discard(bdev_get_queue(bio
->bi_bdev
))))
803 generic_make_request(bio
);
807 spin_unlock_irq(&conf
->device_lock
);
811 * Sometimes we need to suspend IO while we do something else,
812 * either some resync/recovery, or reconfigure the array.
813 * To do this we raise a 'barrier'.
814 * The 'barrier' is a counter that can be raised multiple times
815 * to count how many activities are happening which preclude
817 * We can only raise the barrier if there is no pending IO.
818 * i.e. if nr_pending == 0.
819 * We choose only to raise the barrier if no-one is waiting for the
820 * barrier to go down. This means that as soon as an IO request
821 * is ready, no other operations which require a barrier will start
822 * until the IO request has had a chance.
824 * So: regular IO calls 'wait_barrier'. When that returns there
825 * is no backgroup IO happening, It must arrange to call
826 * allow_barrier when it has finished its IO.
827 * backgroup IO calls must call raise_barrier. Once that returns
828 * there is no normal IO happeing. It must arrange to call
829 * lower_barrier when the particular background IO completes.
831 static void raise_barrier(struct r1conf
*conf
, sector_t sector_nr
)
833 spin_lock_irq(&conf
->resync_lock
);
835 /* Wait until no block IO is waiting */
836 wait_event_lock_irq(conf
->wait_barrier
, !conf
->nr_waiting
,
839 /* block any new IO from starting */
841 conf
->next_resync
= sector_nr
;
843 /* For these conditions we must wait:
844 * A: while the array is in frozen state
845 * B: while barrier >= RESYNC_DEPTH, meaning resync reach
846 * the max count which allowed.
847 * C: next_resync + RESYNC_SECTORS > start_next_window, meaning
848 * next resync will reach to the window which normal bios are
850 * D: while there are any active requests in the current window.
852 wait_event_lock_irq(conf
->wait_barrier
,
853 !conf
->array_frozen
&&
854 conf
->barrier
< RESYNC_DEPTH
&&
855 conf
->current_window_requests
== 0 &&
856 (conf
->start_next_window
>=
857 conf
->next_resync
+ RESYNC_SECTORS
),
861 spin_unlock_irq(&conf
->resync_lock
);
864 static void lower_barrier(struct r1conf
*conf
)
867 BUG_ON(conf
->barrier
<= 0);
868 spin_lock_irqsave(&conf
->resync_lock
, flags
);
871 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
872 wake_up(&conf
->wait_barrier
);
875 static bool need_to_wait_for_sync(struct r1conf
*conf
, struct bio
*bio
)
879 if (conf
->array_frozen
|| !bio
)
881 else if (conf
->barrier
&& bio_data_dir(bio
) == WRITE
) {
882 if ((conf
->mddev
->curr_resync_completed
883 >= bio_end_sector(bio
)) ||
884 (conf
->next_resync
+ NEXT_NORMALIO_DISTANCE
885 <= bio
->bi_iter
.bi_sector
))
894 static sector_t
wait_barrier(struct r1conf
*conf
, struct bio
*bio
)
898 spin_lock_irq(&conf
->resync_lock
);
899 if (need_to_wait_for_sync(conf
, bio
)) {
901 /* Wait for the barrier to drop.
902 * However if there are already pending
903 * requests (preventing the barrier from
904 * rising completely), and the
905 * pre-process bio queue isn't empty,
906 * then don't wait, as we need to empty
907 * that queue to get the nr_pending
910 wait_event_lock_irq(conf
->wait_barrier
,
911 !conf
->array_frozen
&&
913 ((conf
->start_next_window
<
914 conf
->next_resync
+ RESYNC_SECTORS
) &&
916 !bio_list_empty(current
->bio_list
))),
921 if (bio
&& bio_data_dir(bio
) == WRITE
) {
922 if (bio
->bi_iter
.bi_sector
>=
923 conf
->mddev
->curr_resync_completed
) {
924 if (conf
->start_next_window
== MaxSector
)
925 conf
->start_next_window
=
927 NEXT_NORMALIO_DISTANCE
;
929 if ((conf
->start_next_window
+ NEXT_NORMALIO_DISTANCE
)
930 <= bio
->bi_iter
.bi_sector
)
931 conf
->next_window_requests
++;
933 conf
->current_window_requests
++;
934 sector
= conf
->start_next_window
;
939 spin_unlock_irq(&conf
->resync_lock
);
943 static void allow_barrier(struct r1conf
*conf
, sector_t start_next_window
,
948 spin_lock_irqsave(&conf
->resync_lock
, flags
);
950 if (start_next_window
) {
951 if (start_next_window
== conf
->start_next_window
) {
952 if (conf
->start_next_window
+ NEXT_NORMALIO_DISTANCE
954 conf
->next_window_requests
--;
956 conf
->current_window_requests
--;
958 conf
->current_window_requests
--;
960 if (!conf
->current_window_requests
) {
961 if (conf
->next_window_requests
) {
962 conf
->current_window_requests
=
963 conf
->next_window_requests
;
964 conf
->next_window_requests
= 0;
965 conf
->start_next_window
+=
966 NEXT_NORMALIO_DISTANCE
;
968 conf
->start_next_window
= MaxSector
;
971 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
972 wake_up(&conf
->wait_barrier
);
975 static void freeze_array(struct r1conf
*conf
, int extra
)
977 /* stop syncio and normal IO and wait for everything to
979 * We wait until nr_pending match nr_queued+extra
980 * This is called in the context of one normal IO request
981 * that has failed. Thus any sync request that might be pending
982 * will be blocked by nr_pending, and we need to wait for
983 * pending IO requests to complete or be queued for re-try.
984 * Thus the number queued (nr_queued) plus this request (extra)
985 * must match the number of pending IOs (nr_pending) before
988 spin_lock_irq(&conf
->resync_lock
);
989 conf
->array_frozen
= 1;
990 wait_event_lock_irq_cmd(conf
->wait_barrier
,
991 conf
->nr_pending
== conf
->nr_queued
+extra
,
993 flush_pending_writes(conf
));
994 spin_unlock_irq(&conf
->resync_lock
);
996 static void unfreeze_array(struct r1conf
*conf
)
998 /* reverse the effect of the freeze */
999 spin_lock_irq(&conf
->resync_lock
);
1000 conf
->array_frozen
= 0;
1001 wake_up(&conf
->wait_barrier
);
1002 spin_unlock_irq(&conf
->resync_lock
);
1006 /* duplicate the data pages for behind I/O
1008 static void alloc_behind_pages(struct bio
*bio
, struct r1bio
*r1_bio
)
1011 struct bio_vec
*bvec
;
1012 struct bio_vec
*bvecs
= kzalloc(bio
->bi_vcnt
* sizeof(struct bio_vec
),
1014 if (unlikely(!bvecs
))
1017 bio_for_each_segment_all(bvec
, bio
, i
) {
1019 bvecs
[i
].bv_page
= alloc_page(GFP_NOIO
);
1020 if (unlikely(!bvecs
[i
].bv_page
))
1022 memcpy(kmap(bvecs
[i
].bv_page
) + bvec
->bv_offset
,
1023 kmap(bvec
->bv_page
) + bvec
->bv_offset
, bvec
->bv_len
);
1024 kunmap(bvecs
[i
].bv_page
);
1025 kunmap(bvec
->bv_page
);
1027 r1_bio
->behind_bvecs
= bvecs
;
1028 r1_bio
->behind_page_count
= bio
->bi_vcnt
;
1029 set_bit(R1BIO_BehindIO
, &r1_bio
->state
);
1033 for (i
= 0; i
< bio
->bi_vcnt
; i
++)
1034 if (bvecs
[i
].bv_page
)
1035 put_page(bvecs
[i
].bv_page
);
1037 pr_debug("%dB behind alloc failed, doing sync I/O\n",
1038 bio
->bi_iter
.bi_size
);
1041 struct raid1_plug_cb
{
1042 struct blk_plug_cb cb
;
1043 struct bio_list pending
;
1047 static void raid1_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
1049 struct raid1_plug_cb
*plug
= container_of(cb
, struct raid1_plug_cb
,
1051 struct mddev
*mddev
= plug
->cb
.data
;
1052 struct r1conf
*conf
= mddev
->private;
1055 if (from_schedule
|| current
->bio_list
) {
1056 spin_lock_irq(&conf
->device_lock
);
1057 bio_list_merge(&conf
->pending_bio_list
, &plug
->pending
);
1058 conf
->pending_count
+= plug
->pending_cnt
;
1059 spin_unlock_irq(&conf
->device_lock
);
1060 wake_up(&conf
->wait_barrier
);
1061 md_wakeup_thread(mddev
->thread
);
1066 /* we aren't scheduling, so we can do the write-out directly. */
1067 bio
= bio_list_get(&plug
->pending
);
1068 bitmap_unplug(mddev
->bitmap
);
1069 wake_up(&conf
->wait_barrier
);
1071 while (bio
) { /* submit pending writes */
1072 struct bio
*next
= bio
->bi_next
;
1073 bio
->bi_next
= NULL
;
1074 if (unlikely((bio
->bi_rw
& REQ_DISCARD
) &&
1075 !blk_queue_discard(bdev_get_queue(bio
->bi_bdev
))))
1076 /* Just ignore it */
1079 generic_make_request(bio
);
1085 static void make_request(struct mddev
*mddev
, struct bio
* bio
)
1087 struct r1conf
*conf
= mddev
->private;
1088 struct raid1_info
*mirror
;
1089 struct r1bio
*r1_bio
;
1090 struct bio
*read_bio
;
1092 struct bitmap
*bitmap
;
1093 unsigned long flags
;
1094 const int rw
= bio_data_dir(bio
);
1095 const unsigned long do_sync
= (bio
->bi_rw
& REQ_SYNC
);
1096 const unsigned long do_flush_fua
= (bio
->bi_rw
& (REQ_FLUSH
| REQ_FUA
));
1097 const unsigned long do_discard
= (bio
->bi_rw
1098 & (REQ_DISCARD
| REQ_SECURE
));
1099 const unsigned long do_same
= (bio
->bi_rw
& REQ_WRITE_SAME
);
1100 struct md_rdev
*blocked_rdev
;
1101 struct blk_plug_cb
*cb
;
1102 struct raid1_plug_cb
*plug
= NULL
;
1104 int sectors_handled
;
1106 sector_t start_next_window
;
1109 * Register the new request and wait if the reconstruction
1110 * thread has put up a bar for new requests.
1111 * Continue immediately if no resync is active currently.
1114 md_write_start(mddev
, bio
); /* wait on superblock update early */
1116 if (bio_data_dir(bio
) == WRITE
&&
1117 bio_end_sector(bio
) > mddev
->suspend_lo
&&
1118 bio
->bi_iter
.bi_sector
< mddev
->suspend_hi
) {
1119 /* As the suspend_* range is controlled by
1120 * userspace, we want an interruptible
1125 flush_signals(current
);
1126 prepare_to_wait(&conf
->wait_barrier
,
1127 &w
, TASK_INTERRUPTIBLE
);
1128 if (bio_end_sector(bio
) <= mddev
->suspend_lo
||
1129 bio
->bi_iter
.bi_sector
>= mddev
->suspend_hi
)
1133 finish_wait(&conf
->wait_barrier
, &w
);
1136 start_next_window
= wait_barrier(conf
, bio
);
1138 bitmap
= mddev
->bitmap
;
1141 * make_request() can abort the operation when READA is being
1142 * used and no empty request is available.
1145 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
1147 r1_bio
->master_bio
= bio
;
1148 r1_bio
->sectors
= bio_sectors(bio
);
1150 r1_bio
->mddev
= mddev
;
1151 r1_bio
->sector
= bio
->bi_iter
.bi_sector
;
1153 /* We might need to issue multiple reads to different
1154 * devices if there are bad blocks around, so we keep
1155 * track of the number of reads in bio->bi_phys_segments.
1156 * If this is 0, there is only one r1_bio and no locking
1157 * will be needed when requests complete. If it is
1158 * non-zero, then it is the number of not-completed requests.
1160 bio
->bi_phys_segments
= 0;
1161 clear_bit(BIO_SEG_VALID
, &bio
->bi_flags
);
1165 * read balancing logic:
1170 rdisk
= read_balance(conf
, r1_bio
, &max_sectors
);
1173 /* couldn't find anywhere to read from */
1174 raid_end_bio_io(r1_bio
);
1177 mirror
= conf
->mirrors
+ rdisk
;
1179 if (test_bit(WriteMostly
, &mirror
->rdev
->flags
) &&
1181 /* Reading from a write-mostly device must
1182 * take care not to over-take any writes
1185 wait_event(bitmap
->behind_wait
,
1186 atomic_read(&bitmap
->behind_writes
) == 0);
1188 r1_bio
->read_disk
= rdisk
;
1189 r1_bio
->start_next_window
= 0;
1191 read_bio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1192 bio_trim(read_bio
, r1_bio
->sector
- bio
->bi_iter
.bi_sector
,
1195 r1_bio
->bios
[rdisk
] = read_bio
;
1197 read_bio
->bi_iter
.bi_sector
= r1_bio
->sector
+
1198 mirror
->rdev
->data_offset
;
1199 read_bio
->bi_bdev
= mirror
->rdev
->bdev
;
1200 read_bio
->bi_end_io
= raid1_end_read_request
;
1201 read_bio
->bi_rw
= READ
| do_sync
;
1202 read_bio
->bi_private
= r1_bio
;
1204 if (max_sectors
< r1_bio
->sectors
) {
1205 /* could not read all from this device, so we will
1206 * need another r1_bio.
1209 sectors_handled
= (r1_bio
->sector
+ max_sectors
1210 - bio
->bi_iter
.bi_sector
);
1211 r1_bio
->sectors
= max_sectors
;
1212 spin_lock_irq(&conf
->device_lock
);
1213 if (bio
->bi_phys_segments
== 0)
1214 bio
->bi_phys_segments
= 2;
1216 bio
->bi_phys_segments
++;
1217 spin_unlock_irq(&conf
->device_lock
);
1218 /* Cannot call generic_make_request directly
1219 * as that will be queued in __make_request
1220 * and subsequent mempool_alloc might block waiting
1221 * for it. So hand bio over to raid1d.
1223 reschedule_retry(r1_bio
);
1225 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
1227 r1_bio
->master_bio
= bio
;
1228 r1_bio
->sectors
= bio_sectors(bio
) - sectors_handled
;
1230 r1_bio
->mddev
= mddev
;
1231 r1_bio
->sector
= bio
->bi_iter
.bi_sector
+
1235 generic_make_request(read_bio
);
1242 if (conf
->pending_count
>= max_queued_requests
) {
1243 md_wakeup_thread(mddev
->thread
);
1244 wait_event(conf
->wait_barrier
,
1245 conf
->pending_count
< max_queued_requests
);
1247 /* first select target devices under rcu_lock and
1248 * inc refcount on their rdev. Record them by setting
1250 * If there are known/acknowledged bad blocks on any device on
1251 * which we have seen a write error, we want to avoid writing those
1253 * This potentially requires several writes to write around
1254 * the bad blocks. Each set of writes gets it's own r1bio
1255 * with a set of bios attached.
1258 disks
= conf
->raid_disks
* 2;
1260 r1_bio
->start_next_window
= start_next_window
;
1261 blocked_rdev
= NULL
;
1263 max_sectors
= r1_bio
->sectors
;
1264 for (i
= 0; i
< disks
; i
++) {
1265 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1266 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
1267 atomic_inc(&rdev
->nr_pending
);
1268 blocked_rdev
= rdev
;
1271 r1_bio
->bios
[i
] = NULL
;
1272 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
)
1273 || test_bit(Unmerged
, &rdev
->flags
)) {
1274 if (i
< conf
->raid_disks
)
1275 set_bit(R1BIO_Degraded
, &r1_bio
->state
);
1279 atomic_inc(&rdev
->nr_pending
);
1280 if (test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1285 is_bad
= is_badblock(rdev
, r1_bio
->sector
,
1287 &first_bad
, &bad_sectors
);
1289 /* mustn't write here until the bad block is
1291 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1292 blocked_rdev
= rdev
;
1295 if (is_bad
&& first_bad
<= r1_bio
->sector
) {
1296 /* Cannot write here at all */
1297 bad_sectors
-= (r1_bio
->sector
- first_bad
);
1298 if (bad_sectors
< max_sectors
)
1299 /* mustn't write more than bad_sectors
1300 * to other devices yet
1302 max_sectors
= bad_sectors
;
1303 rdev_dec_pending(rdev
, mddev
);
1304 /* We don't set R1BIO_Degraded as that
1305 * only applies if the disk is
1306 * missing, so it might be re-added,
1307 * and we want to know to recover this
1309 * In this case the device is here,
1310 * and the fact that this chunk is not
1311 * in-sync is recorded in the bad
1317 int good_sectors
= first_bad
- r1_bio
->sector
;
1318 if (good_sectors
< max_sectors
)
1319 max_sectors
= good_sectors
;
1322 r1_bio
->bios
[i
] = bio
;
1326 if (unlikely(blocked_rdev
)) {
1327 /* Wait for this device to become unblocked */
1329 sector_t old
= start_next_window
;
1331 for (j
= 0; j
< i
; j
++)
1332 if (r1_bio
->bios
[j
])
1333 rdev_dec_pending(conf
->mirrors
[j
].rdev
, mddev
);
1335 allow_barrier(conf
, start_next_window
, bio
->bi_iter
.bi_sector
);
1336 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1337 start_next_window
= wait_barrier(conf
, bio
);
1339 * We must make sure the multi r1bios of bio have
1340 * the same value of bi_phys_segments
1342 if (bio
->bi_phys_segments
&& old
&&
1343 old
!= start_next_window
)
1344 /* Wait for the former r1bio(s) to complete */
1345 wait_event(conf
->wait_barrier
,
1346 bio
->bi_phys_segments
== 1);
1350 if (max_sectors
< r1_bio
->sectors
) {
1351 /* We are splitting this write into multiple parts, so
1352 * we need to prepare for allocating another r1_bio.
1354 r1_bio
->sectors
= max_sectors
;
1355 spin_lock_irq(&conf
->device_lock
);
1356 if (bio
->bi_phys_segments
== 0)
1357 bio
->bi_phys_segments
= 2;
1359 bio
->bi_phys_segments
++;
1360 spin_unlock_irq(&conf
->device_lock
);
1362 sectors_handled
= r1_bio
->sector
+ max_sectors
- bio
->bi_iter
.bi_sector
;
1364 atomic_set(&r1_bio
->remaining
, 1);
1365 atomic_set(&r1_bio
->behind_remaining
, 0);
1368 for (i
= 0; i
< disks
; i
++) {
1370 if (!r1_bio
->bios
[i
])
1373 mbio
= bio_clone_mddev(bio
, GFP_NOIO
, mddev
);
1374 bio_trim(mbio
, r1_bio
->sector
- bio
->bi_iter
.bi_sector
, max_sectors
);
1378 * Not if there are too many, or cannot
1379 * allocate memory, or a reader on WriteMostly
1380 * is waiting for behind writes to flush */
1382 (atomic_read(&bitmap
->behind_writes
)
1383 < mddev
->bitmap_info
.max_write_behind
) &&
1384 !waitqueue_active(&bitmap
->behind_wait
))
1385 alloc_behind_pages(mbio
, r1_bio
);
1387 bitmap_startwrite(bitmap
, r1_bio
->sector
,
1389 test_bit(R1BIO_BehindIO
,
1393 if (r1_bio
->behind_bvecs
) {
1394 struct bio_vec
*bvec
;
1398 * We trimmed the bio, so _all is legit
1400 bio_for_each_segment_all(bvec
, mbio
, j
)
1401 bvec
->bv_page
= r1_bio
->behind_bvecs
[j
].bv_page
;
1402 if (test_bit(WriteMostly
, &conf
->mirrors
[i
].rdev
->flags
))
1403 atomic_inc(&r1_bio
->behind_remaining
);
1406 r1_bio
->bios
[i
] = mbio
;
1408 mbio
->bi_iter
.bi_sector
= (r1_bio
->sector
+
1409 conf
->mirrors
[i
].rdev
->data_offset
);
1410 mbio
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1411 mbio
->bi_end_io
= raid1_end_write_request
;
1413 WRITE
| do_flush_fua
| do_sync
| do_discard
| do_same
;
1414 mbio
->bi_private
= r1_bio
;
1416 atomic_inc(&r1_bio
->remaining
);
1418 cb
= blk_check_plugged(raid1_unplug
, mddev
, sizeof(*plug
));
1420 plug
= container_of(cb
, struct raid1_plug_cb
, cb
);
1423 spin_lock_irqsave(&conf
->device_lock
, flags
);
1425 bio_list_add(&plug
->pending
, mbio
);
1426 plug
->pending_cnt
++;
1428 bio_list_add(&conf
->pending_bio_list
, mbio
);
1429 conf
->pending_count
++;
1431 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1433 md_wakeup_thread(mddev
->thread
);
1435 /* Mustn't call r1_bio_write_done before this next test,
1436 * as it could result in the bio being freed.
1438 if (sectors_handled
< bio_sectors(bio
)) {
1439 r1_bio_write_done(r1_bio
);
1440 /* We need another r1_bio. It has already been counted
1441 * in bio->bi_phys_segments
1443 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
1444 r1_bio
->master_bio
= bio
;
1445 r1_bio
->sectors
= bio_sectors(bio
) - sectors_handled
;
1447 r1_bio
->mddev
= mddev
;
1448 r1_bio
->sector
= bio
->bi_iter
.bi_sector
+ sectors_handled
;
1452 r1_bio_write_done(r1_bio
);
1454 /* In case raid1d snuck in to freeze_array */
1455 wake_up(&conf
->wait_barrier
);
1458 static void status(struct seq_file
*seq
, struct mddev
*mddev
)
1460 struct r1conf
*conf
= mddev
->private;
1463 seq_printf(seq
, " [%d/%d] [", conf
->raid_disks
,
1464 conf
->raid_disks
- mddev
->degraded
);
1466 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1467 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1468 seq_printf(seq
, "%s",
1469 rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
1472 seq_printf(seq
, "]");
1476 static void error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1478 char b
[BDEVNAME_SIZE
];
1479 struct r1conf
*conf
= mddev
->private;
1480 unsigned long flags
;
1483 * If it is not operational, then we have already marked it as dead
1484 * else if it is the last working disks, ignore the error, let the
1485 * next level up know.
1486 * else mark the drive as failed
1488 if (test_bit(In_sync
, &rdev
->flags
)
1489 && (conf
->raid_disks
- mddev
->degraded
) == 1) {
1491 * Don't fail the drive, act as though we were just a
1492 * normal single drive.
1493 * However don't try a recovery from this drive as
1494 * it is very likely to fail.
1496 conf
->recovery_disabled
= mddev
->recovery_disabled
;
1499 set_bit(Blocked
, &rdev
->flags
);
1500 spin_lock_irqsave(&conf
->device_lock
, flags
);
1501 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
1503 set_bit(Faulty
, &rdev
->flags
);
1505 set_bit(Faulty
, &rdev
->flags
);
1506 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1508 * if recovery is running, make sure it aborts.
1510 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1511 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
1513 "md/raid1:%s: Disk failure on %s, disabling device.\n"
1514 "md/raid1:%s: Operation continuing on %d devices.\n",
1515 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1516 mdname(mddev
), conf
->raid_disks
- mddev
->degraded
);
1519 static void print_conf(struct r1conf
*conf
)
1523 printk(KERN_DEBUG
"RAID1 conf printout:\n");
1525 printk(KERN_DEBUG
"(!conf)\n");
1528 printk(KERN_DEBUG
" --- wd:%d rd:%d\n", conf
->raid_disks
- conf
->mddev
->degraded
,
1532 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1533 char b
[BDEVNAME_SIZE
];
1534 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1536 printk(KERN_DEBUG
" disk %d, wo:%d, o:%d, dev:%s\n",
1537 i
, !test_bit(In_sync
, &rdev
->flags
),
1538 !test_bit(Faulty
, &rdev
->flags
),
1539 bdevname(rdev
->bdev
,b
));
1544 static void close_sync(struct r1conf
*conf
)
1546 wait_barrier(conf
, NULL
);
1547 allow_barrier(conf
, 0, 0);
1549 mempool_destroy(conf
->r1buf_pool
);
1550 conf
->r1buf_pool
= NULL
;
1552 spin_lock_irq(&conf
->resync_lock
);
1553 conf
->next_resync
= 0;
1554 conf
->start_next_window
= MaxSector
;
1555 conf
->current_window_requests
+=
1556 conf
->next_window_requests
;
1557 conf
->next_window_requests
= 0;
1558 spin_unlock_irq(&conf
->resync_lock
);
1561 static int raid1_spare_active(struct mddev
*mddev
)
1564 struct r1conf
*conf
= mddev
->private;
1566 unsigned long flags
;
1569 * Find all failed disks within the RAID1 configuration
1570 * and mark them readable.
1571 * Called under mddev lock, so rcu protection not needed.
1572 * device_lock used to avoid races with raid1_end_read_request
1573 * which expects 'In_sync' flags and ->degraded to be consistent.
1575 spin_lock_irqsave(&conf
->device_lock
, flags
);
1576 for (i
= 0; i
< conf
->raid_disks
; i
++) {
1577 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
1578 struct md_rdev
*repl
= conf
->mirrors
[conf
->raid_disks
+ i
].rdev
;
1580 && repl
->recovery_offset
== MaxSector
1581 && !test_bit(Faulty
, &repl
->flags
)
1582 && !test_and_set_bit(In_sync
, &repl
->flags
)) {
1583 /* replacement has just become active */
1585 !test_and_clear_bit(In_sync
, &rdev
->flags
))
1588 /* Replaced device not technically
1589 * faulty, but we need to be sure
1590 * it gets removed and never re-added
1592 set_bit(Faulty
, &rdev
->flags
);
1593 sysfs_notify_dirent_safe(
1598 && rdev
->recovery_offset
== MaxSector
1599 && !test_bit(Faulty
, &rdev
->flags
)
1600 && !test_and_set_bit(In_sync
, &rdev
->flags
)) {
1602 sysfs_notify_dirent_safe(rdev
->sysfs_state
);
1605 mddev
->degraded
-= count
;
1606 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1613 static int raid1_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1615 struct r1conf
*conf
= mddev
->private;
1618 struct raid1_info
*p
;
1620 int last
= conf
->raid_disks
- 1;
1621 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
1623 if (mddev
->recovery_disabled
== conf
->recovery_disabled
)
1626 if (rdev
->raid_disk
>= 0)
1627 first
= last
= rdev
->raid_disk
;
1629 if (q
->merge_bvec_fn
) {
1630 set_bit(Unmerged
, &rdev
->flags
);
1631 mddev
->merge_check_needed
= 1;
1634 for (mirror
= first
; mirror
<= last
; mirror
++) {
1635 p
= conf
->mirrors
+mirror
;
1639 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1640 rdev
->data_offset
<< 9);
1642 p
->head_position
= 0;
1643 rdev
->raid_disk
= mirror
;
1645 /* As all devices are equivalent, we don't need a full recovery
1646 * if this was recently any drive of the array
1648 if (rdev
->saved_raid_disk
< 0)
1650 rcu_assign_pointer(p
->rdev
, rdev
);
1653 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
1654 p
[conf
->raid_disks
].rdev
== NULL
) {
1655 /* Add this device as a replacement */
1656 clear_bit(In_sync
, &rdev
->flags
);
1657 set_bit(Replacement
, &rdev
->flags
);
1658 rdev
->raid_disk
= mirror
;
1661 rcu_assign_pointer(p
[conf
->raid_disks
].rdev
, rdev
);
1665 if (err
== 0 && test_bit(Unmerged
, &rdev
->flags
)) {
1666 /* Some requests might not have seen this new
1667 * merge_bvec_fn. We must wait for them to complete
1668 * before merging the device fully.
1669 * First we make sure any code which has tested
1670 * our function has submitted the request, then
1671 * we wait for all outstanding requests to complete.
1673 synchronize_sched();
1674 freeze_array(conf
, 0);
1675 unfreeze_array(conf
);
1676 clear_bit(Unmerged
, &rdev
->flags
);
1678 md_integrity_add_rdev(rdev
, mddev
);
1679 if (mddev
->queue
&& blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
1680 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, mddev
->queue
);
1685 static int raid1_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1687 struct r1conf
*conf
= mddev
->private;
1689 int number
= rdev
->raid_disk
;
1690 struct raid1_info
*p
= conf
->mirrors
+ number
;
1692 if (rdev
!= p
->rdev
)
1693 p
= conf
->mirrors
+ conf
->raid_disks
+ number
;
1696 if (rdev
== p
->rdev
) {
1697 if (test_bit(In_sync
, &rdev
->flags
) ||
1698 atomic_read(&rdev
->nr_pending
)) {
1702 /* Only remove non-faulty devices if recovery
1705 if (!test_bit(Faulty
, &rdev
->flags
) &&
1706 mddev
->recovery_disabled
!= conf
->recovery_disabled
&&
1707 mddev
->degraded
< conf
->raid_disks
) {
1713 if (atomic_read(&rdev
->nr_pending
)) {
1714 /* lost the race, try later */
1718 } else if (conf
->mirrors
[conf
->raid_disks
+ number
].rdev
) {
1719 /* We just removed a device that is being replaced.
1720 * Move down the replacement. We drain all IO before
1721 * doing this to avoid confusion.
1723 struct md_rdev
*repl
=
1724 conf
->mirrors
[conf
->raid_disks
+ number
].rdev
;
1725 freeze_array(conf
, 0);
1726 clear_bit(Replacement
, &repl
->flags
);
1728 conf
->mirrors
[conf
->raid_disks
+ number
].rdev
= NULL
;
1729 unfreeze_array(conf
);
1730 clear_bit(WantReplacement
, &rdev
->flags
);
1732 clear_bit(WantReplacement
, &rdev
->flags
);
1733 err
= md_integrity_register(mddev
);
1742 static void end_sync_read(struct bio
*bio
, int error
)
1744 struct r1bio
*r1_bio
= bio
->bi_private
;
1746 update_head_pos(r1_bio
->read_disk
, r1_bio
);
1749 * we have read a block, now it needs to be re-written,
1750 * or re-read if the read failed.
1751 * We don't do much here, just schedule handling by raid1d
1753 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
))
1754 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
1756 if (atomic_dec_and_test(&r1_bio
->remaining
))
1757 reschedule_retry(r1_bio
);
1760 static void end_sync_write(struct bio
*bio
, int error
)
1762 int uptodate
= test_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1763 struct r1bio
*r1_bio
= bio
->bi_private
;
1764 struct mddev
*mddev
= r1_bio
->mddev
;
1765 struct r1conf
*conf
= mddev
->private;
1770 mirror
= find_bio_disk(r1_bio
, bio
);
1773 sector_t sync_blocks
= 0;
1774 sector_t s
= r1_bio
->sector
;
1775 long sectors_to_go
= r1_bio
->sectors
;
1776 /* make sure these bits doesn't get cleared. */
1778 bitmap_end_sync(mddev
->bitmap
, s
,
1781 sectors_to_go
-= sync_blocks
;
1782 } while (sectors_to_go
> 0);
1783 set_bit(WriteErrorSeen
,
1784 &conf
->mirrors
[mirror
].rdev
->flags
);
1785 if (!test_and_set_bit(WantReplacement
,
1786 &conf
->mirrors
[mirror
].rdev
->flags
))
1787 set_bit(MD_RECOVERY_NEEDED
, &
1789 set_bit(R1BIO_WriteError
, &r1_bio
->state
);
1790 } else if (is_badblock(conf
->mirrors
[mirror
].rdev
,
1793 &first_bad
, &bad_sectors
) &&
1794 !is_badblock(conf
->mirrors
[r1_bio
->read_disk
].rdev
,
1797 &first_bad
, &bad_sectors
)
1799 set_bit(R1BIO_MadeGood
, &r1_bio
->state
);
1801 if (atomic_dec_and_test(&r1_bio
->remaining
)) {
1802 int s
= r1_bio
->sectors
;
1803 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
) ||
1804 test_bit(R1BIO_WriteError
, &r1_bio
->state
))
1805 reschedule_retry(r1_bio
);
1808 md_done_sync(mddev
, s
, uptodate
);
1813 static int r1_sync_page_io(struct md_rdev
*rdev
, sector_t sector
,
1814 int sectors
, struct page
*page
, int rw
)
1816 if (sync_page_io(rdev
, sector
, sectors
<< 9, page
, rw
, false))
1820 set_bit(WriteErrorSeen
, &rdev
->flags
);
1821 if (!test_and_set_bit(WantReplacement
,
1823 set_bit(MD_RECOVERY_NEEDED
, &
1824 rdev
->mddev
->recovery
);
1826 /* need to record an error - either for the block or the device */
1827 if (!rdev_set_badblocks(rdev
, sector
, sectors
, 0))
1828 md_error(rdev
->mddev
, rdev
);
1832 static int fix_sync_read_error(struct r1bio
*r1_bio
)
1834 /* Try some synchronous reads of other devices to get
1835 * good data, much like with normal read errors. Only
1836 * read into the pages we already have so we don't
1837 * need to re-issue the read request.
1838 * We don't need to freeze the array, because being in an
1839 * active sync request, there is no normal IO, and
1840 * no overlapping syncs.
1841 * We don't need to check is_badblock() again as we
1842 * made sure that anything with a bad block in range
1843 * will have bi_end_io clear.
1845 struct mddev
*mddev
= r1_bio
->mddev
;
1846 struct r1conf
*conf
= mddev
->private;
1847 struct bio
*bio
= r1_bio
->bios
[r1_bio
->read_disk
];
1848 sector_t sect
= r1_bio
->sector
;
1849 int sectors
= r1_bio
->sectors
;
1854 int d
= r1_bio
->read_disk
;
1856 struct md_rdev
*rdev
;
1859 if (s
> (PAGE_SIZE
>>9))
1862 if (r1_bio
->bios
[d
]->bi_end_io
== end_sync_read
) {
1863 /* No rcu protection needed here devices
1864 * can only be removed when no resync is
1865 * active, and resync is currently active
1867 rdev
= conf
->mirrors
[d
].rdev
;
1868 if (sync_page_io(rdev
, sect
, s
<<9,
1869 bio
->bi_io_vec
[idx
].bv_page
,
1876 if (d
== conf
->raid_disks
* 2)
1878 } while (!success
&& d
!= r1_bio
->read_disk
);
1881 char b
[BDEVNAME_SIZE
];
1883 /* Cannot read from anywhere, this block is lost.
1884 * Record a bad block on each device. If that doesn't
1885 * work just disable and interrupt the recovery.
1886 * Don't fail devices as that won't really help.
1888 printk(KERN_ALERT
"md/raid1:%s: %s: unrecoverable I/O read error"
1889 " for block %llu\n",
1891 bdevname(bio
->bi_bdev
, b
),
1892 (unsigned long long)r1_bio
->sector
);
1893 for (d
= 0; d
< conf
->raid_disks
* 2; d
++) {
1894 rdev
= conf
->mirrors
[d
].rdev
;
1895 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
1897 if (!rdev_set_badblocks(rdev
, sect
, s
, 0))
1901 conf
->recovery_disabled
=
1902 mddev
->recovery_disabled
;
1903 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1904 md_done_sync(mddev
, r1_bio
->sectors
, 0);
1916 /* write it back and re-read */
1917 while (d
!= r1_bio
->read_disk
) {
1919 d
= conf
->raid_disks
* 2;
1921 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
1923 rdev
= conf
->mirrors
[d
].rdev
;
1924 if (r1_sync_page_io(rdev
, sect
, s
,
1925 bio
->bi_io_vec
[idx
].bv_page
,
1927 r1_bio
->bios
[d
]->bi_end_io
= NULL
;
1928 rdev_dec_pending(rdev
, mddev
);
1932 while (d
!= r1_bio
->read_disk
) {
1934 d
= conf
->raid_disks
* 2;
1936 if (r1_bio
->bios
[d
]->bi_end_io
!= end_sync_read
)
1938 rdev
= conf
->mirrors
[d
].rdev
;
1939 if (r1_sync_page_io(rdev
, sect
, s
,
1940 bio
->bi_io_vec
[idx
].bv_page
,
1942 atomic_add(s
, &rdev
->corrected_errors
);
1948 set_bit(R1BIO_Uptodate
, &r1_bio
->state
);
1949 set_bit(BIO_UPTODATE
, &bio
->bi_flags
);
1953 static int process_checks(struct r1bio
*r1_bio
)
1955 /* We have read all readable devices. If we haven't
1956 * got the block, then there is no hope left.
1957 * If we have, then we want to do a comparison
1958 * and skip the write if everything is the same.
1959 * If any blocks failed to read, then we need to
1960 * attempt an over-write
1962 struct mddev
*mddev
= r1_bio
->mddev
;
1963 struct r1conf
*conf
= mddev
->private;
1968 /* Fix variable parts of all bios */
1969 vcnt
= (r1_bio
->sectors
+ PAGE_SIZE
/ 512 - 1) >> (PAGE_SHIFT
- 9);
1970 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
1974 struct bio
*b
= r1_bio
->bios
[i
];
1975 if (b
->bi_end_io
!= end_sync_read
)
1977 /* fixup the bio for reuse, but preserve BIO_UPTODATE */
1978 uptodate
= test_bit(BIO_UPTODATE
, &b
->bi_flags
);
1981 clear_bit(BIO_UPTODATE
, &b
->bi_flags
);
1983 b
->bi_iter
.bi_size
= r1_bio
->sectors
<< 9;
1984 b
->bi_iter
.bi_sector
= r1_bio
->sector
+
1985 conf
->mirrors
[i
].rdev
->data_offset
;
1986 b
->bi_bdev
= conf
->mirrors
[i
].rdev
->bdev
;
1987 b
->bi_end_io
= end_sync_read
;
1988 b
->bi_private
= r1_bio
;
1990 size
= b
->bi_iter
.bi_size
;
1991 for (j
= 0; j
< vcnt
; j
++) {
1993 bi
= &b
->bi_io_vec
[j
];
1995 if (size
> PAGE_SIZE
)
1996 bi
->bv_len
= PAGE_SIZE
;
2002 for (primary
= 0; primary
< conf
->raid_disks
* 2; primary
++)
2003 if (r1_bio
->bios
[primary
]->bi_end_io
== end_sync_read
&&
2004 test_bit(BIO_UPTODATE
, &r1_bio
->bios
[primary
]->bi_flags
)) {
2005 r1_bio
->bios
[primary
]->bi_end_io
= NULL
;
2006 rdev_dec_pending(conf
->mirrors
[primary
].rdev
, mddev
);
2009 r1_bio
->read_disk
= primary
;
2010 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
2012 struct bio
*pbio
= r1_bio
->bios
[primary
];
2013 struct bio
*sbio
= r1_bio
->bios
[i
];
2014 int uptodate
= test_bit(BIO_UPTODATE
, &sbio
->bi_flags
);
2016 if (sbio
->bi_end_io
!= end_sync_read
)
2018 /* Now we can 'fixup' the BIO_UPTODATE flag */
2019 set_bit(BIO_UPTODATE
, &sbio
->bi_flags
);
2022 for (j
= vcnt
; j
-- ; ) {
2024 p
= pbio
->bi_io_vec
[j
].bv_page
;
2025 s
= sbio
->bi_io_vec
[j
].bv_page
;
2026 if (memcmp(page_address(p
),
2028 sbio
->bi_io_vec
[j
].bv_len
))
2034 atomic64_add(r1_bio
->sectors
, &mddev
->resync_mismatches
);
2035 if (j
< 0 || (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)
2037 /* No need to write to this device. */
2038 sbio
->bi_end_io
= NULL
;
2039 rdev_dec_pending(conf
->mirrors
[i
].rdev
, mddev
);
2043 bio_copy_data(sbio
, pbio
);
2048 static void sync_request_write(struct mddev
*mddev
, struct r1bio
*r1_bio
)
2050 struct r1conf
*conf
= mddev
->private;
2052 int disks
= conf
->raid_disks
* 2;
2053 struct bio
*bio
, *wbio
;
2055 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
2057 if (!test_bit(R1BIO_Uptodate
, &r1_bio
->state
))
2058 /* ouch - failed to read all of that. */
2059 if (!fix_sync_read_error(r1_bio
))
2062 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2063 if (process_checks(r1_bio
) < 0)
2068 atomic_set(&r1_bio
->remaining
, 1);
2069 for (i
= 0; i
< disks
; i
++) {
2070 wbio
= r1_bio
->bios
[i
];
2071 if (wbio
->bi_end_io
== NULL
||
2072 (wbio
->bi_end_io
== end_sync_read
&&
2073 (i
== r1_bio
->read_disk
||
2074 !test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))))
2077 wbio
->bi_rw
= WRITE
;
2078 wbio
->bi_end_io
= end_sync_write
;
2079 atomic_inc(&r1_bio
->remaining
);
2080 md_sync_acct(conf
->mirrors
[i
].rdev
->bdev
, bio_sectors(wbio
));
2082 generic_make_request(wbio
);
2085 if (atomic_dec_and_test(&r1_bio
->remaining
)) {
2086 /* if we're here, all write(s) have completed, so clean up */
2087 int s
= r1_bio
->sectors
;
2088 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
) ||
2089 test_bit(R1BIO_WriteError
, &r1_bio
->state
))
2090 reschedule_retry(r1_bio
);
2093 md_done_sync(mddev
, s
, 1);
2099 * This is a kernel thread which:
2101 * 1. Retries failed read operations on working mirrors.
2102 * 2. Updates the raid superblock when problems encounter.
2103 * 3. Performs writes following reads for array synchronising.
2106 static void fix_read_error(struct r1conf
*conf
, int read_disk
,
2107 sector_t sect
, int sectors
)
2109 struct mddev
*mddev
= conf
->mddev
;
2115 struct md_rdev
*rdev
;
2117 if (s
> (PAGE_SIZE
>>9))
2121 /* Note: no rcu protection needed here
2122 * as this is synchronous in the raid1d thread
2123 * which is the thread that might remove
2124 * a device. If raid1d ever becomes multi-threaded....
2129 rdev
= conf
->mirrors
[d
].rdev
;
2131 (test_bit(In_sync
, &rdev
->flags
) ||
2132 (!test_bit(Faulty
, &rdev
->flags
) &&
2133 rdev
->recovery_offset
>= sect
+ s
)) &&
2134 is_badblock(rdev
, sect
, s
,
2135 &first_bad
, &bad_sectors
) == 0 &&
2136 sync_page_io(rdev
, sect
, s
<<9,
2137 conf
->tmppage
, READ
, false))
2141 if (d
== conf
->raid_disks
* 2)
2144 } while (!success
&& d
!= read_disk
);
2147 /* Cannot read from anywhere - mark it bad */
2148 struct md_rdev
*rdev
= conf
->mirrors
[read_disk
].rdev
;
2149 if (!rdev_set_badblocks(rdev
, sect
, s
, 0))
2150 md_error(mddev
, rdev
);
2153 /* write it back and re-read */
2155 while (d
!= read_disk
) {
2157 d
= conf
->raid_disks
* 2;
2159 rdev
= conf
->mirrors
[d
].rdev
;
2161 !test_bit(Faulty
, &rdev
->flags
))
2162 r1_sync_page_io(rdev
, sect
, s
,
2163 conf
->tmppage
, WRITE
);
2166 while (d
!= read_disk
) {
2167 char b
[BDEVNAME_SIZE
];
2169 d
= conf
->raid_disks
* 2;
2171 rdev
= conf
->mirrors
[d
].rdev
;
2173 !test_bit(Faulty
, &rdev
->flags
)) {
2174 if (r1_sync_page_io(rdev
, sect
, s
,
2175 conf
->tmppage
, READ
)) {
2176 atomic_add(s
, &rdev
->corrected_errors
);
2178 "md/raid1:%s: read error corrected "
2179 "(%d sectors at %llu on %s)\n",
2181 (unsigned long long)(sect
+
2183 bdevname(rdev
->bdev
, b
));
2192 static int narrow_write_error(struct r1bio
*r1_bio
, int i
)
2194 struct mddev
*mddev
= r1_bio
->mddev
;
2195 struct r1conf
*conf
= mddev
->private;
2196 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
2198 /* bio has the data to be written to device 'i' where
2199 * we just recently had a write error.
2200 * We repeatedly clone the bio and trim down to one block,
2201 * then try the write. Where the write fails we record
2203 * It is conceivable that the bio doesn't exactly align with
2204 * blocks. We must handle this somehow.
2206 * We currently own a reference on the rdev.
2212 int sect_to_write
= r1_bio
->sectors
;
2215 if (rdev
->badblocks
.shift
< 0)
2218 block_sectors
= 1 << rdev
->badblocks
.shift
;
2219 sector
= r1_bio
->sector
;
2220 sectors
= ((sector
+ block_sectors
)
2221 & ~(sector_t
)(block_sectors
- 1))
2224 while (sect_to_write
) {
2226 if (sectors
> sect_to_write
)
2227 sectors
= sect_to_write
;
2228 /* Write at 'sector' for 'sectors'*/
2230 if (test_bit(R1BIO_BehindIO
, &r1_bio
->state
)) {
2231 unsigned vcnt
= r1_bio
->behind_page_count
;
2232 struct bio_vec
*vec
= r1_bio
->behind_bvecs
;
2234 while (!vec
->bv_page
) {
2239 wbio
= bio_alloc_mddev(GFP_NOIO
, vcnt
, mddev
);
2240 memcpy(wbio
->bi_io_vec
, vec
, vcnt
* sizeof(struct bio_vec
));
2242 wbio
->bi_vcnt
= vcnt
;
2244 wbio
= bio_clone_mddev(r1_bio
->master_bio
, GFP_NOIO
, mddev
);
2247 wbio
->bi_rw
= WRITE
;
2248 wbio
->bi_iter
.bi_sector
= r1_bio
->sector
;
2249 wbio
->bi_iter
.bi_size
= r1_bio
->sectors
<< 9;
2251 bio_trim(wbio
, sector
- r1_bio
->sector
, sectors
);
2252 wbio
->bi_iter
.bi_sector
+= rdev
->data_offset
;
2253 wbio
->bi_bdev
= rdev
->bdev
;
2254 if (submit_bio_wait(WRITE
, wbio
) < 0)
2256 ok
= rdev_set_badblocks(rdev
, sector
,
2261 sect_to_write
-= sectors
;
2263 sectors
= block_sectors
;
2268 static void handle_sync_write_finished(struct r1conf
*conf
, struct r1bio
*r1_bio
)
2271 int s
= r1_bio
->sectors
;
2272 for (m
= 0; m
< conf
->raid_disks
* 2 ; m
++) {
2273 struct md_rdev
*rdev
= conf
->mirrors
[m
].rdev
;
2274 struct bio
*bio
= r1_bio
->bios
[m
];
2275 if (bio
->bi_end_io
== NULL
)
2277 if (test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
2278 test_bit(R1BIO_MadeGood
, &r1_bio
->state
)) {
2279 rdev_clear_badblocks(rdev
, r1_bio
->sector
, s
, 0);
2281 if (!test_bit(BIO_UPTODATE
, &bio
->bi_flags
) &&
2282 test_bit(R1BIO_WriteError
, &r1_bio
->state
)) {
2283 if (!rdev_set_badblocks(rdev
, r1_bio
->sector
, s
, 0))
2284 md_error(conf
->mddev
, rdev
);
2288 md_done_sync(conf
->mddev
, s
, 1);
2291 static void handle_write_finished(struct r1conf
*conf
, struct r1bio
*r1_bio
)
2294 for (m
= 0; m
< conf
->raid_disks
* 2 ; m
++)
2295 if (r1_bio
->bios
[m
] == IO_MADE_GOOD
) {
2296 struct md_rdev
*rdev
= conf
->mirrors
[m
].rdev
;
2297 rdev_clear_badblocks(rdev
,
2299 r1_bio
->sectors
, 0);
2300 rdev_dec_pending(rdev
, conf
->mddev
);
2301 } else if (r1_bio
->bios
[m
] != NULL
) {
2302 /* This drive got a write error. We need to
2303 * narrow down and record precise write
2306 if (!narrow_write_error(r1_bio
, m
)) {
2307 md_error(conf
->mddev
,
2308 conf
->mirrors
[m
].rdev
);
2309 /* an I/O failed, we can't clear the bitmap */
2310 set_bit(R1BIO_Degraded
, &r1_bio
->state
);
2312 rdev_dec_pending(conf
->mirrors
[m
].rdev
,
2315 if (test_bit(R1BIO_WriteError
, &r1_bio
->state
))
2316 close_write(r1_bio
);
2317 raid_end_bio_io(r1_bio
);
2320 static void handle_read_error(struct r1conf
*conf
, struct r1bio
*r1_bio
)
2324 struct mddev
*mddev
= conf
->mddev
;
2326 char b
[BDEVNAME_SIZE
];
2327 struct md_rdev
*rdev
;
2329 clear_bit(R1BIO_ReadError
, &r1_bio
->state
);
2330 /* we got a read error. Maybe the drive is bad. Maybe just
2331 * the block and we can fix it.
2332 * We freeze all other IO, and try reading the block from
2333 * other devices. When we find one, we re-write
2334 * and check it that fixes the read error.
2335 * This is all done synchronously while the array is
2338 if (mddev
->ro
== 0) {
2339 freeze_array(conf
, 1);
2340 fix_read_error(conf
, r1_bio
->read_disk
,
2341 r1_bio
->sector
, r1_bio
->sectors
);
2342 unfreeze_array(conf
);
2344 md_error(mddev
, conf
->mirrors
[r1_bio
->read_disk
].rdev
);
2345 rdev_dec_pending(conf
->mirrors
[r1_bio
->read_disk
].rdev
, conf
->mddev
);
2347 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
2348 bdevname(bio
->bi_bdev
, b
);
2350 disk
= read_balance(conf
, r1_bio
, &max_sectors
);
2352 printk(KERN_ALERT
"md/raid1:%s: %s: unrecoverable I/O"
2353 " read error for block %llu\n",
2354 mdname(mddev
), b
, (unsigned long long)r1_bio
->sector
);
2355 raid_end_bio_io(r1_bio
);
2357 const unsigned long do_sync
2358 = r1_bio
->master_bio
->bi_rw
& REQ_SYNC
;
2360 r1_bio
->bios
[r1_bio
->read_disk
] =
2361 mddev
->ro
? IO_BLOCKED
: NULL
;
2364 r1_bio
->read_disk
= disk
;
2365 bio
= bio_clone_mddev(r1_bio
->master_bio
, GFP_NOIO
, mddev
);
2366 bio_trim(bio
, r1_bio
->sector
- bio
->bi_iter
.bi_sector
,
2368 r1_bio
->bios
[r1_bio
->read_disk
] = bio
;
2369 rdev
= conf
->mirrors
[disk
].rdev
;
2370 printk_ratelimited(KERN_ERR
2371 "md/raid1:%s: redirecting sector %llu"
2372 " to other mirror: %s\n",
2374 (unsigned long long)r1_bio
->sector
,
2375 bdevname(rdev
->bdev
, b
));
2376 bio
->bi_iter
.bi_sector
= r1_bio
->sector
+ rdev
->data_offset
;
2377 bio
->bi_bdev
= rdev
->bdev
;
2378 bio
->bi_end_io
= raid1_end_read_request
;
2379 bio
->bi_rw
= READ
| do_sync
;
2380 bio
->bi_private
= r1_bio
;
2381 if (max_sectors
< r1_bio
->sectors
) {
2382 /* Drat - have to split this up more */
2383 struct bio
*mbio
= r1_bio
->master_bio
;
2384 int sectors_handled
= (r1_bio
->sector
+ max_sectors
2385 - mbio
->bi_iter
.bi_sector
);
2386 r1_bio
->sectors
= max_sectors
;
2387 spin_lock_irq(&conf
->device_lock
);
2388 if (mbio
->bi_phys_segments
== 0)
2389 mbio
->bi_phys_segments
= 2;
2391 mbio
->bi_phys_segments
++;
2392 spin_unlock_irq(&conf
->device_lock
);
2393 generic_make_request(bio
);
2396 r1_bio
= mempool_alloc(conf
->r1bio_pool
, GFP_NOIO
);
2398 r1_bio
->master_bio
= mbio
;
2399 r1_bio
->sectors
= bio_sectors(mbio
) - sectors_handled
;
2401 set_bit(R1BIO_ReadError
, &r1_bio
->state
);
2402 r1_bio
->mddev
= mddev
;
2403 r1_bio
->sector
= mbio
->bi_iter
.bi_sector
+
2408 generic_make_request(bio
);
2412 static void raid1d(struct md_thread
*thread
)
2414 struct mddev
*mddev
= thread
->mddev
;
2415 struct r1bio
*r1_bio
;
2416 unsigned long flags
;
2417 struct r1conf
*conf
= mddev
->private;
2418 struct list_head
*head
= &conf
->retry_list
;
2419 struct blk_plug plug
;
2421 md_check_recovery(mddev
);
2423 blk_start_plug(&plug
);
2426 flush_pending_writes(conf
);
2428 spin_lock_irqsave(&conf
->device_lock
, flags
);
2429 if (list_empty(head
)) {
2430 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2433 r1_bio
= list_entry(head
->prev
, struct r1bio
, retry_list
);
2434 list_del(head
->prev
);
2436 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2438 mddev
= r1_bio
->mddev
;
2439 conf
= mddev
->private;
2440 if (test_bit(R1BIO_IsSync
, &r1_bio
->state
)) {
2441 if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
) ||
2442 test_bit(R1BIO_WriteError
, &r1_bio
->state
))
2443 handle_sync_write_finished(conf
, r1_bio
);
2445 sync_request_write(mddev
, r1_bio
);
2446 } else if (test_bit(R1BIO_MadeGood
, &r1_bio
->state
) ||
2447 test_bit(R1BIO_WriteError
, &r1_bio
->state
))
2448 handle_write_finished(conf
, r1_bio
);
2449 else if (test_bit(R1BIO_ReadError
, &r1_bio
->state
))
2450 handle_read_error(conf
, r1_bio
);
2452 /* just a partial read to be scheduled from separate
2455 generic_make_request(r1_bio
->bios
[r1_bio
->read_disk
]);
2458 if (mddev
->flags
& ~(1<<MD_CHANGE_PENDING
))
2459 md_check_recovery(mddev
);
2461 blk_finish_plug(&plug
);
2465 static int init_resync(struct r1conf
*conf
)
2469 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
2470 BUG_ON(conf
->r1buf_pool
);
2471 conf
->r1buf_pool
= mempool_create(buffs
, r1buf_pool_alloc
, r1buf_pool_free
,
2473 if (!conf
->r1buf_pool
)
2475 conf
->next_resync
= 0;
2480 * perform a "sync" on one "block"
2482 * We need to make sure that no normal I/O request - particularly write
2483 * requests - conflict with active sync requests.
2485 * This is achieved by tracking pending requests and a 'barrier' concept
2486 * that can be installed to exclude normal IO requests.
2489 static sector_t
sync_request(struct mddev
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
2491 struct r1conf
*conf
= mddev
->private;
2492 struct r1bio
*r1_bio
;
2494 sector_t max_sector
, nr_sectors
;
2498 int write_targets
= 0, read_targets
= 0;
2499 sector_t sync_blocks
;
2500 int still_degraded
= 0;
2501 int good_sectors
= RESYNC_SECTORS
;
2502 int min_bad
= 0; /* number of sectors that are bad in all devices */
2504 if (!conf
->r1buf_pool
)
2505 if (init_resync(conf
))
2508 max_sector
= mddev
->dev_sectors
;
2509 if (sector_nr
>= max_sector
) {
2510 /* If we aborted, we need to abort the
2511 * sync on the 'current' bitmap chunk (there will
2512 * only be one in raid1 resync.
2513 * We can find the current addess in mddev->curr_resync
2515 if (mddev
->curr_resync
< max_sector
) /* aborted */
2516 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
2518 else /* completed sync */
2521 bitmap_close_sync(mddev
->bitmap
);
2526 if (mddev
->bitmap
== NULL
&&
2527 mddev
->recovery_cp
== MaxSector
&&
2528 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
2529 conf
->fullsync
== 0) {
2531 return max_sector
- sector_nr
;
2533 /* before building a request, check if we can skip these blocks..
2534 * This call the bitmap_start_sync doesn't actually record anything
2536 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
2537 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
2538 /* We can skip this block, and probably several more */
2543 * If there is non-resync activity waiting for a turn,
2544 * and resync is going fast enough,
2545 * then let it though before starting on this new sync request.
2547 if (!go_faster
&& conf
->nr_waiting
)
2548 msleep_interruptible(1000);
2550 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
);
2551 r1_bio
= mempool_alloc(conf
->r1buf_pool
, GFP_NOIO
);
2553 raise_barrier(conf
, sector_nr
);
2557 * If we get a correctably read error during resync or recovery,
2558 * we might want to read from a different device. So we
2559 * flag all drives that could conceivably be read from for READ,
2560 * and any others (which will be non-In_sync devices) for WRITE.
2561 * If a read fails, we try reading from something else for which READ
2565 r1_bio
->mddev
= mddev
;
2566 r1_bio
->sector
= sector_nr
;
2568 set_bit(R1BIO_IsSync
, &r1_bio
->state
);
2570 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
2571 struct md_rdev
*rdev
;
2572 bio
= r1_bio
->bios
[i
];
2575 rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
2577 test_bit(Faulty
, &rdev
->flags
)) {
2578 if (i
< conf
->raid_disks
)
2580 } else if (!test_bit(In_sync
, &rdev
->flags
)) {
2582 bio
->bi_end_io
= end_sync_write
;
2585 /* may need to read from here */
2586 sector_t first_bad
= MaxSector
;
2589 if (is_badblock(rdev
, sector_nr
, good_sectors
,
2590 &first_bad
, &bad_sectors
)) {
2591 if (first_bad
> sector_nr
)
2592 good_sectors
= first_bad
- sector_nr
;
2594 bad_sectors
-= (sector_nr
- first_bad
);
2596 min_bad
> bad_sectors
)
2597 min_bad
= bad_sectors
;
2600 if (sector_nr
< first_bad
) {
2601 if (test_bit(WriteMostly
, &rdev
->flags
)) {
2609 bio
->bi_end_io
= end_sync_read
;
2611 } else if (!test_bit(WriteErrorSeen
, &rdev
->flags
) &&
2612 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
2613 !test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
)) {
2615 * The device is suitable for reading (InSync),
2616 * but has bad block(s) here. Let's try to correct them,
2617 * if we are doing resync or repair. Otherwise, leave
2618 * this device alone for this sync request.
2621 bio
->bi_end_io
= end_sync_write
;
2625 if (bio
->bi_end_io
) {
2626 atomic_inc(&rdev
->nr_pending
);
2627 bio
->bi_iter
.bi_sector
= sector_nr
+ rdev
->data_offset
;
2628 bio
->bi_bdev
= rdev
->bdev
;
2629 bio
->bi_private
= r1_bio
;
2635 r1_bio
->read_disk
= disk
;
2637 if (read_targets
== 0 && min_bad
> 0) {
2638 /* These sectors are bad on all InSync devices, so we
2639 * need to mark them bad on all write targets
2642 for (i
= 0 ; i
< conf
->raid_disks
* 2 ; i
++)
2643 if (r1_bio
->bios
[i
]->bi_end_io
== end_sync_write
) {
2644 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
2645 ok
= rdev_set_badblocks(rdev
, sector_nr
,
2649 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2654 /* Cannot record the badblocks, so need to
2656 * If there are multiple read targets, could just
2657 * fail the really bad ones ???
2659 conf
->recovery_disabled
= mddev
->recovery_disabled
;
2660 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2666 if (min_bad
> 0 && min_bad
< good_sectors
) {
2667 /* only resync enough to reach the next bad->good
2669 good_sectors
= min_bad
;
2672 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) && read_targets
> 0)
2673 /* extra read targets are also write targets */
2674 write_targets
+= read_targets
-1;
2676 if (write_targets
== 0 || read_targets
== 0) {
2677 /* There is nowhere to write, so all non-sync
2678 * drives must be failed - so we are finished
2682 max_sector
= sector_nr
+ min_bad
;
2683 rv
= max_sector
- sector_nr
;
2689 if (max_sector
> mddev
->resync_max
)
2690 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
2691 if (max_sector
> sector_nr
+ good_sectors
)
2692 max_sector
= sector_nr
+ good_sectors
;
2697 int len
= PAGE_SIZE
;
2698 if (sector_nr
+ (len
>>9) > max_sector
)
2699 len
= (max_sector
- sector_nr
) << 9;
2702 if (sync_blocks
== 0) {
2703 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
2704 &sync_blocks
, still_degraded
) &&
2706 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
))
2708 BUG_ON(sync_blocks
< (PAGE_SIZE
>>9));
2709 if ((len
>> 9) > sync_blocks
)
2710 len
= sync_blocks
<<9;
2713 for (i
= 0 ; i
< conf
->raid_disks
* 2; i
++) {
2714 bio
= r1_bio
->bios
[i
];
2715 if (bio
->bi_end_io
) {
2716 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
2717 if (bio_add_page(bio
, page
, len
, 0) == 0) {
2719 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
2722 bio
= r1_bio
->bios
[i
];
2723 if (bio
->bi_end_io
==NULL
)
2725 /* remove last page from this bio */
2727 bio
->bi_iter
.bi_size
-= len
;
2728 bio
->bi_flags
&= ~(1<< BIO_SEG_VALID
);
2734 nr_sectors
+= len
>>9;
2735 sector_nr
+= len
>>9;
2736 sync_blocks
-= (len
>>9);
2737 } while (r1_bio
->bios
[disk
]->bi_vcnt
< RESYNC_PAGES
);
2739 r1_bio
->sectors
= nr_sectors
;
2741 /* For a user-requested sync, we read all readable devices and do a
2744 if (test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
)) {
2745 atomic_set(&r1_bio
->remaining
, read_targets
);
2746 for (i
= 0; i
< conf
->raid_disks
* 2 && read_targets
; i
++) {
2747 bio
= r1_bio
->bios
[i
];
2748 if (bio
->bi_end_io
== end_sync_read
) {
2750 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2751 generic_make_request(bio
);
2755 atomic_set(&r1_bio
->remaining
, 1);
2756 bio
= r1_bio
->bios
[r1_bio
->read_disk
];
2757 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
2758 generic_make_request(bio
);
2764 static sector_t
raid1_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
2769 return mddev
->dev_sectors
;
2772 static struct r1conf
*setup_conf(struct mddev
*mddev
)
2774 struct r1conf
*conf
;
2776 struct raid1_info
*disk
;
2777 struct md_rdev
*rdev
;
2780 conf
= kzalloc(sizeof(struct r1conf
), GFP_KERNEL
);
2784 conf
->mirrors
= kzalloc(sizeof(struct raid1_info
)
2785 * mddev
->raid_disks
* 2,
2790 conf
->tmppage
= alloc_page(GFP_KERNEL
);
2794 conf
->poolinfo
= kzalloc(sizeof(*conf
->poolinfo
), GFP_KERNEL
);
2795 if (!conf
->poolinfo
)
2797 conf
->poolinfo
->raid_disks
= mddev
->raid_disks
* 2;
2798 conf
->r1bio_pool
= mempool_create(NR_RAID1_BIOS
, r1bio_pool_alloc
,
2801 if (!conf
->r1bio_pool
)
2804 conf
->poolinfo
->mddev
= mddev
;
2807 spin_lock_init(&conf
->device_lock
);
2808 rdev_for_each(rdev
, mddev
) {
2809 struct request_queue
*q
;
2810 int disk_idx
= rdev
->raid_disk
;
2811 if (disk_idx
>= mddev
->raid_disks
2814 if (test_bit(Replacement
, &rdev
->flags
))
2815 disk
= conf
->mirrors
+ mddev
->raid_disks
+ disk_idx
;
2817 disk
= conf
->mirrors
+ disk_idx
;
2822 q
= bdev_get_queue(rdev
->bdev
);
2823 if (q
->merge_bvec_fn
)
2824 mddev
->merge_check_needed
= 1;
2826 disk
->head_position
= 0;
2827 disk
->seq_start
= MaxSector
;
2829 conf
->raid_disks
= mddev
->raid_disks
;
2830 conf
->mddev
= mddev
;
2831 INIT_LIST_HEAD(&conf
->retry_list
);
2833 spin_lock_init(&conf
->resync_lock
);
2834 init_waitqueue_head(&conf
->wait_barrier
);
2836 bio_list_init(&conf
->pending_bio_list
);
2837 conf
->pending_count
= 0;
2838 conf
->recovery_disabled
= mddev
->recovery_disabled
- 1;
2840 conf
->start_next_window
= MaxSector
;
2841 conf
->current_window_requests
= conf
->next_window_requests
= 0;
2844 for (i
= 0; i
< conf
->raid_disks
* 2; i
++) {
2846 disk
= conf
->mirrors
+ i
;
2848 if (i
< conf
->raid_disks
&&
2849 disk
[conf
->raid_disks
].rdev
) {
2850 /* This slot has a replacement. */
2852 /* No original, just make the replacement
2853 * a recovering spare
2856 disk
[conf
->raid_disks
].rdev
;
2857 disk
[conf
->raid_disks
].rdev
= NULL
;
2858 } else if (!test_bit(In_sync
, &disk
->rdev
->flags
))
2859 /* Original is not in_sync - bad */
2864 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
2865 disk
->head_position
= 0;
2867 (disk
->rdev
->saved_raid_disk
< 0))
2873 conf
->thread
= md_register_thread(raid1d
, mddev
, "raid1");
2874 if (!conf
->thread
) {
2876 "md/raid1:%s: couldn't allocate thread\n",
2885 if (conf
->r1bio_pool
)
2886 mempool_destroy(conf
->r1bio_pool
);
2887 kfree(conf
->mirrors
);
2888 safe_put_page(conf
->tmppage
);
2889 kfree(conf
->poolinfo
);
2892 return ERR_PTR(err
);
2895 static int stop(struct mddev
*mddev
);
2896 static int run(struct mddev
*mddev
)
2898 struct r1conf
*conf
;
2900 struct md_rdev
*rdev
;
2902 bool discard_supported
= false;
2904 if (mddev
->level
!= 1) {
2905 printk(KERN_ERR
"md/raid1:%s: raid level not set to mirroring (%d)\n",
2906 mdname(mddev
), mddev
->level
);
2909 if (mddev
->reshape_position
!= MaxSector
) {
2910 printk(KERN_ERR
"md/raid1:%s: reshape_position set but not supported\n",
2915 * copy the already verified devices into our private RAID1
2916 * bookkeeping area. [whatever we allocate in run(),
2917 * should be freed in stop()]
2919 if (mddev
->private == NULL
)
2920 conf
= setup_conf(mddev
);
2922 conf
= mddev
->private;
2925 return PTR_ERR(conf
);
2928 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
2930 rdev_for_each(rdev
, mddev
) {
2931 if (!mddev
->gendisk
)
2933 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
2934 rdev
->data_offset
<< 9);
2935 if (blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
2936 discard_supported
= true;
2939 mddev
->degraded
= 0;
2940 for (i
=0; i
< conf
->raid_disks
; i
++)
2941 if (conf
->mirrors
[i
].rdev
== NULL
||
2942 !test_bit(In_sync
, &conf
->mirrors
[i
].rdev
->flags
) ||
2943 test_bit(Faulty
, &conf
->mirrors
[i
].rdev
->flags
))
2946 if (conf
->raid_disks
- mddev
->degraded
== 1)
2947 mddev
->recovery_cp
= MaxSector
;
2949 if (mddev
->recovery_cp
!= MaxSector
)
2950 printk(KERN_NOTICE
"md/raid1:%s: not clean"
2951 " -- starting background reconstruction\n",
2954 "md/raid1:%s: active with %d out of %d mirrors\n",
2955 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2959 * Ok, everything is just fine now
2961 mddev
->thread
= conf
->thread
;
2962 conf
->thread
= NULL
;
2963 mddev
->private = conf
;
2965 md_set_array_sectors(mddev
, raid1_size(mddev
, 0, 0));
2968 mddev
->queue
->backing_dev_info
.congested_fn
= raid1_congested
;
2969 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
2970 blk_queue_merge_bvec(mddev
->queue
, raid1_mergeable_bvec
);
2972 if (discard_supported
)
2973 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
2976 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
2980 ret
= md_integrity_register(mddev
);
2986 static int stop(struct mddev
*mddev
)
2988 struct r1conf
*conf
= mddev
->private;
2989 struct bitmap
*bitmap
= mddev
->bitmap
;
2991 /* wait for behind writes to complete */
2992 if (bitmap
&& atomic_read(&bitmap
->behind_writes
) > 0) {
2993 printk(KERN_INFO
"md/raid1:%s: behind writes in progress - waiting to stop.\n",
2995 /* need to kick something here to make sure I/O goes? */
2996 wait_event(bitmap
->behind_wait
,
2997 atomic_read(&bitmap
->behind_writes
) == 0);
3000 freeze_array(conf
, 0);
3001 unfreeze_array(conf
);
3003 md_unregister_thread(&mddev
->thread
);
3004 if (conf
->r1bio_pool
)
3005 mempool_destroy(conf
->r1bio_pool
);
3006 kfree(conf
->mirrors
);
3007 safe_put_page(conf
->tmppage
);
3008 kfree(conf
->poolinfo
);
3010 mddev
->private = NULL
;
3014 static int raid1_resize(struct mddev
*mddev
, sector_t sectors
)
3016 /* no resync is happening, and there is enough space
3017 * on all devices, so we can resize.
3018 * We need to make sure resync covers any new space.
3019 * If the array is shrinking we should possibly wait until
3020 * any io in the removed space completes, but it hardly seems
3023 sector_t newsize
= raid1_size(mddev
, sectors
, 0);
3024 if (mddev
->external_size
&&
3025 mddev
->array_sectors
> newsize
)
3027 if (mddev
->bitmap
) {
3028 int ret
= bitmap_resize(mddev
->bitmap
, newsize
, 0, 0);
3032 md_set_array_sectors(mddev
, newsize
);
3033 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3034 revalidate_disk(mddev
->gendisk
);
3035 if (sectors
> mddev
->dev_sectors
&&
3036 mddev
->recovery_cp
> mddev
->dev_sectors
) {
3037 mddev
->recovery_cp
= mddev
->dev_sectors
;
3038 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3040 mddev
->dev_sectors
= sectors
;
3041 mddev
->resync_max_sectors
= sectors
;
3045 static int raid1_reshape(struct mddev
*mddev
)
3048 * 1/ resize the r1bio_pool
3049 * 2/ resize conf->mirrors
3051 * We allocate a new r1bio_pool if we can.
3052 * Then raise a device barrier and wait until all IO stops.
3053 * Then resize conf->mirrors and swap in the new r1bio pool.
3055 * At the same time, we "pack" the devices so that all the missing
3056 * devices have the higher raid_disk numbers.
3058 mempool_t
*newpool
, *oldpool
;
3059 struct pool_info
*newpoolinfo
;
3060 struct raid1_info
*newmirrors
;
3061 struct r1conf
*conf
= mddev
->private;
3062 int cnt
, raid_disks
;
3063 unsigned long flags
;
3066 /* Cannot change chunk_size, layout, or level */
3067 if (mddev
->chunk_sectors
!= mddev
->new_chunk_sectors
||
3068 mddev
->layout
!= mddev
->new_layout
||
3069 mddev
->level
!= mddev
->new_level
) {
3070 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3071 mddev
->new_layout
= mddev
->layout
;
3072 mddev
->new_level
= mddev
->level
;
3076 err
= md_allow_write(mddev
);
3080 raid_disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
3082 if (raid_disks
< conf
->raid_disks
) {
3084 for (d
= 0; d
< conf
->raid_disks
; d
++)
3085 if (conf
->mirrors
[d
].rdev
)
3087 if (cnt
> raid_disks
)
3091 newpoolinfo
= kmalloc(sizeof(*newpoolinfo
), GFP_KERNEL
);
3094 newpoolinfo
->mddev
= mddev
;
3095 newpoolinfo
->raid_disks
= raid_disks
* 2;
3097 newpool
= mempool_create(NR_RAID1_BIOS
, r1bio_pool_alloc
,
3098 r1bio_pool_free
, newpoolinfo
);
3103 newmirrors
= kzalloc(sizeof(struct raid1_info
) * raid_disks
* 2,
3107 mempool_destroy(newpool
);
3111 freeze_array(conf
, 0);
3113 /* ok, everything is stopped */
3114 oldpool
= conf
->r1bio_pool
;
3115 conf
->r1bio_pool
= newpool
;
3117 for (d
= d2
= 0; d
< conf
->raid_disks
; d
++) {
3118 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
3119 if (rdev
&& rdev
->raid_disk
!= d2
) {
3120 sysfs_unlink_rdev(mddev
, rdev
);
3121 rdev
->raid_disk
= d2
;
3122 sysfs_unlink_rdev(mddev
, rdev
);
3123 if (sysfs_link_rdev(mddev
, rdev
))
3125 "md/raid1:%s: cannot register rd%d\n",
3126 mdname(mddev
), rdev
->raid_disk
);
3129 newmirrors
[d2
++].rdev
= rdev
;
3131 kfree(conf
->mirrors
);
3132 conf
->mirrors
= newmirrors
;
3133 kfree(conf
->poolinfo
);
3134 conf
->poolinfo
= newpoolinfo
;
3136 spin_lock_irqsave(&conf
->device_lock
, flags
);
3137 mddev
->degraded
+= (raid_disks
- conf
->raid_disks
);
3138 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3139 conf
->raid_disks
= mddev
->raid_disks
= raid_disks
;
3140 mddev
->delta_disks
= 0;
3142 unfreeze_array(conf
);
3144 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3145 md_wakeup_thread(mddev
->thread
);
3147 mempool_destroy(oldpool
);
3151 static void raid1_quiesce(struct mddev
*mddev
, int state
)
3153 struct r1conf
*conf
= mddev
->private;
3156 case 2: /* wake for suspend */
3157 wake_up(&conf
->wait_barrier
);
3160 freeze_array(conf
, 0);
3163 unfreeze_array(conf
);
3168 static void *raid1_takeover(struct mddev
*mddev
)
3170 /* raid1 can take over:
3171 * raid5 with 2 devices, any layout or chunk size
3173 if (mddev
->level
== 5 && mddev
->raid_disks
== 2) {
3174 struct r1conf
*conf
;
3175 mddev
->new_level
= 1;
3176 mddev
->new_layout
= 0;
3177 mddev
->new_chunk_sectors
= 0;
3178 conf
= setup_conf(mddev
);
3180 /* Array must appear to be quiesced */
3181 conf
->array_frozen
= 1;
3184 return ERR_PTR(-EINVAL
);
3187 static struct md_personality raid1_personality
=
3191 .owner
= THIS_MODULE
,
3192 .make_request
= make_request
,
3196 .error_handler
= error
,
3197 .hot_add_disk
= raid1_add_disk
,
3198 .hot_remove_disk
= raid1_remove_disk
,
3199 .spare_active
= raid1_spare_active
,
3200 .sync_request
= sync_request
,
3201 .resize
= raid1_resize
,
3203 .check_reshape
= raid1_reshape
,
3204 .quiesce
= raid1_quiesce
,
3205 .takeover
= raid1_takeover
,
3208 static int __init
raid_init(void)
3210 return register_md_personality(&raid1_personality
);
3213 static void raid_exit(void)
3215 unregister_md_personality(&raid1_personality
);
3218 module_init(raid_init
);
3219 module_exit(raid_exit
);
3220 MODULE_LICENSE("GPL");
3221 MODULE_DESCRIPTION("RAID1 (mirroring) personality for MD");
3222 MODULE_ALIAS("md-personality-3"); /* RAID1 */
3223 MODULE_ALIAS("md-raid1");
3224 MODULE_ALIAS("md-level-1");
3226 module_param(max_queued_requests
, int, S_IRUGO
|S_IWUSR
);