2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/module.h>
25 #include <linux/seq_file.h>
26 #include <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include <trace/events/block.h>
35 * RAID10 provides a combination of RAID0 and RAID1 functionality.
36 * The layout of data is defined by
39 * near_copies (stored in low byte of layout)
40 * far_copies (stored in second byte of layout)
41 * far_offset (stored in bit 16 of layout )
42 * use_far_sets (stored in bit 17 of layout )
43 * use_far_sets_bugfixed (stored in bit 18 of layout )
45 * The data to be stored is divided into chunks using chunksize. Each device
46 * is divided into far_copies sections. In each section, chunks are laid out
47 * in a style similar to raid0, but near_copies copies of each chunk is stored
48 * (each on a different drive). The starting device for each section is offset
49 * near_copies from the starting device of the previous section. Thus there
50 * are (near_copies * far_copies) of each chunk, and each is on a different
51 * drive. near_copies and far_copies must be at least one, and their product
52 * is at most raid_disks.
54 * If far_offset is true, then the far_copies are handled a bit differently.
55 * The copies are still in different stripes, but instead of being very far
56 * apart on disk, there are adjacent stripes.
58 * The far and offset algorithms are handled slightly differently if
59 * 'use_far_sets' is true. In this case, the array's devices are grouped into
60 * sets that are (near_copies * far_copies) in size. The far copied stripes
61 * are still shifted by 'near_copies' devices, but this shifting stays confined
62 * to the set rather than the entire array. This is done to improve the number
63 * of device combinations that can fail without causing the array to fail.
64 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
69 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
70 * [A B] [C D] [A B] [C D E]
71 * |...| |...| |...| | ... |
72 * [B A] [D C] [B A] [E C D]
76 * Number of guaranteed r10bios in case of extreme VM load:
78 #define NR_RAID10_BIOS 256
80 /* when we get a read error on a read-only array, we redirect to another
81 * device without failing the first device, or trying to over-write to
82 * correct the read error. To keep track of bad blocks on a per-bio
83 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
85 #define IO_BLOCKED ((struct bio *)1)
86 /* When we successfully write to a known bad-block, we need to remove the
87 * bad-block marking which must be done from process context. So we record
88 * the success by setting devs[n].bio to IO_MADE_GOOD
90 #define IO_MADE_GOOD ((struct bio *)2)
92 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
94 /* When there are this many requests queued to be written by
95 * the raid10 thread, we become 'congested' to provide back-pressure
98 static int max_queued_requests
= 1024;
100 static void allow_barrier(struct r10conf
*conf
);
101 static void lower_barrier(struct r10conf
*conf
);
102 static int _enough(struct r10conf
*conf
, int previous
, int ignore
);
103 static int enough(struct r10conf
*conf
, int ignore
);
104 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
106 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
);
107 static void end_reshape_write(struct bio
*bio
);
108 static void end_reshape(struct r10conf
*conf
);
110 #define raid10_log(md, fmt, args...) \
111 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
113 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
115 struct r10conf
*conf
= data
;
116 int size
= offsetof(struct r10bio
, devs
[conf
->copies
]);
118 /* allocate a r10bio with room for raid_disks entries in the
120 return kzalloc(size
, gfp_flags
);
123 static void r10bio_pool_free(void *r10_bio
, void *data
)
128 /* Maximum size of each resync request */
129 #define RESYNC_BLOCK_SIZE (64*1024)
130 #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE)
131 /* amount of memory to reserve for resync requests */
132 #define RESYNC_WINDOW (1024*1024)
133 /* maximum number of concurrent requests, memory permitting */
134 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
137 * When performing a resync, we need to read and compare, so
138 * we need as many pages are there are copies.
139 * When performing a recovery, we need 2 bios, one for read,
140 * one for write (we recover only one drive per r10buf)
143 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
145 struct r10conf
*conf
= data
;
147 struct r10bio
*r10_bio
;
152 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
156 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
) ||
157 test_bit(MD_RECOVERY_RESHAPE
, &conf
->mddev
->recovery
))
158 nalloc
= conf
->copies
; /* resync */
160 nalloc
= 2; /* recovery */
165 for (j
= nalloc
; j
-- ; ) {
166 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
169 r10_bio
->devs
[j
].bio
= bio
;
170 if (!conf
->have_replacement
)
172 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
175 r10_bio
->devs
[j
].repl_bio
= bio
;
178 * Allocate RESYNC_PAGES data pages and attach them
181 for (j
= 0 ; j
< nalloc
; j
++) {
182 struct bio
*rbio
= r10_bio
->devs
[j
].repl_bio
;
183 bio
= r10_bio
->devs
[j
].bio
;
184 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
185 if (j
> 0 && !test_bit(MD_RECOVERY_SYNC
,
186 &conf
->mddev
->recovery
)) {
187 /* we can share bv_page's during recovery
189 struct bio
*rbio
= r10_bio
->devs
[0].bio
;
190 page
= rbio
->bi_io_vec
[i
].bv_page
;
193 page
= alloc_page(gfp_flags
);
197 bio
->bi_io_vec
[i
].bv_page
= page
;
199 rbio
->bi_io_vec
[i
].bv_page
= page
;
207 safe_put_page(bio
->bi_io_vec
[i
-1].bv_page
);
209 for (i
= 0; i
< RESYNC_PAGES
; i
++)
210 safe_put_page(r10_bio
->devs
[j
].bio
->bi_io_vec
[i
].bv_page
);
213 for ( ; j
< nalloc
; j
++) {
214 if (r10_bio
->devs
[j
].bio
)
215 bio_put(r10_bio
->devs
[j
].bio
);
216 if (r10_bio
->devs
[j
].repl_bio
)
217 bio_put(r10_bio
->devs
[j
].repl_bio
);
219 r10bio_pool_free(r10_bio
, conf
);
223 static void r10buf_pool_free(void *__r10_bio
, void *data
)
226 struct r10conf
*conf
= data
;
227 struct r10bio
*r10bio
= __r10_bio
;
230 for (j
=0; j
< conf
->copies
; j
++) {
231 struct bio
*bio
= r10bio
->devs
[j
].bio
;
233 for (i
= 0; i
< RESYNC_PAGES
; i
++) {
234 safe_put_page(bio
->bi_io_vec
[i
].bv_page
);
235 bio
->bi_io_vec
[i
].bv_page
= NULL
;
239 bio
= r10bio
->devs
[j
].repl_bio
;
243 r10bio_pool_free(r10bio
, conf
);
246 static void put_all_bios(struct r10conf
*conf
, struct r10bio
*r10_bio
)
250 for (i
= 0; i
< conf
->copies
; i
++) {
251 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
252 if (!BIO_SPECIAL(*bio
))
255 bio
= &r10_bio
->devs
[i
].repl_bio
;
256 if (r10_bio
->read_slot
< 0 && !BIO_SPECIAL(*bio
))
262 static void free_r10bio(struct r10bio
*r10_bio
)
264 struct r10conf
*conf
= r10_bio
->mddev
->private;
266 put_all_bios(conf
, r10_bio
);
267 mempool_free(r10_bio
, conf
->r10bio_pool
);
270 static void put_buf(struct r10bio
*r10_bio
)
272 struct r10conf
*conf
= r10_bio
->mddev
->private;
274 mempool_free(r10_bio
, conf
->r10buf_pool
);
279 static void reschedule_retry(struct r10bio
*r10_bio
)
282 struct mddev
*mddev
= r10_bio
->mddev
;
283 struct r10conf
*conf
= mddev
->private;
285 spin_lock_irqsave(&conf
->device_lock
, flags
);
286 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
288 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
290 /* wake up frozen array... */
291 wake_up(&conf
->wait_barrier
);
293 md_wakeup_thread(mddev
->thread
);
297 * raid_end_bio_io() is called when we have finished servicing a mirrored
298 * operation and are ready to return a success/failure code to the buffer
301 static void raid_end_bio_io(struct r10bio
*r10_bio
)
303 struct bio
*bio
= r10_bio
->master_bio
;
305 struct r10conf
*conf
= r10_bio
->mddev
->private;
307 if (bio
->bi_phys_segments
) {
309 spin_lock_irqsave(&conf
->device_lock
, flags
);
310 bio
->bi_phys_segments
--;
311 done
= (bio
->bi_phys_segments
== 0);
312 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
315 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
316 bio
->bi_error
= -EIO
;
320 * Wake up any possible resync thread that waits for the device
325 free_r10bio(r10_bio
);
329 * Update disk head position estimator based on IRQ completion info.
331 static inline void update_head_pos(int slot
, struct r10bio
*r10_bio
)
333 struct r10conf
*conf
= r10_bio
->mddev
->private;
335 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
336 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
340 * Find the disk number which triggered given bio
342 static int find_bio_disk(struct r10conf
*conf
, struct r10bio
*r10_bio
,
343 struct bio
*bio
, int *slotp
, int *replp
)
348 for (slot
= 0; slot
< conf
->copies
; slot
++) {
349 if (r10_bio
->devs
[slot
].bio
== bio
)
351 if (r10_bio
->devs
[slot
].repl_bio
== bio
) {
357 BUG_ON(slot
== conf
->copies
);
358 update_head_pos(slot
, r10_bio
);
364 return r10_bio
->devs
[slot
].devnum
;
367 static void raid10_end_read_request(struct bio
*bio
)
369 int uptodate
= !bio
->bi_error
;
370 struct r10bio
*r10_bio
= bio
->bi_private
;
372 struct md_rdev
*rdev
;
373 struct r10conf
*conf
= r10_bio
->mddev
->private;
375 slot
= r10_bio
->read_slot
;
376 dev
= r10_bio
->devs
[slot
].devnum
;
377 rdev
= r10_bio
->devs
[slot
].rdev
;
379 * this branch is our 'one mirror IO has finished' event handler:
381 update_head_pos(slot
, r10_bio
);
385 * Set R10BIO_Uptodate in our master bio, so that
386 * we will return a good error code to the higher
387 * levels even if IO on some other mirrored buffer fails.
389 * The 'master' represents the composite IO operation to
390 * user-side. So if something waits for IO, then it will
391 * wait for the 'master' bio.
393 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
395 /* If all other devices that store this block have
396 * failed, we want to return the error upwards rather
397 * than fail the last device. Here we redefine
398 * "uptodate" to mean "Don't want to retry"
400 if (!_enough(conf
, test_bit(R10BIO_Previous
, &r10_bio
->state
),
405 raid_end_bio_io(r10_bio
);
406 rdev_dec_pending(rdev
, conf
->mddev
);
409 * oops, read error - keep the refcount on the rdev
411 char b
[BDEVNAME_SIZE
];
412 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
414 bdevname(rdev
->bdev
, b
),
415 (unsigned long long)r10_bio
->sector
);
416 set_bit(R10BIO_ReadError
, &r10_bio
->state
);
417 reschedule_retry(r10_bio
);
421 static void close_write(struct r10bio
*r10_bio
)
423 /* clear the bitmap if all writes complete successfully */
424 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
426 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
428 md_write_end(r10_bio
->mddev
);
431 static void one_write_done(struct r10bio
*r10_bio
)
433 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
434 if (test_bit(R10BIO_WriteError
, &r10_bio
->state
))
435 reschedule_retry(r10_bio
);
437 close_write(r10_bio
);
438 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
439 reschedule_retry(r10_bio
);
441 raid_end_bio_io(r10_bio
);
446 static void raid10_end_write_request(struct bio
*bio
)
448 struct r10bio
*r10_bio
= bio
->bi_private
;
451 struct r10conf
*conf
= r10_bio
->mddev
->private;
453 struct md_rdev
*rdev
= NULL
;
454 struct bio
*to_put
= NULL
;
457 discard_error
= bio
->bi_error
&& bio_op(bio
) == REQ_OP_DISCARD
;
459 dev
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
462 rdev
= conf
->mirrors
[dev
].replacement
;
466 rdev
= conf
->mirrors
[dev
].rdev
;
469 * this branch is our 'one mirror IO has finished' event handler:
471 if (bio
->bi_error
&& !discard_error
) {
473 /* Never record new bad blocks to replacement,
476 md_error(rdev
->mddev
, rdev
);
478 set_bit(WriteErrorSeen
, &rdev
->flags
);
479 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
480 set_bit(MD_RECOVERY_NEEDED
,
481 &rdev
->mddev
->recovery
);
484 if (test_bit(FailFast
, &rdev
->flags
) &&
485 (bio
->bi_opf
& MD_FAILFAST
)) {
486 md_error(rdev
->mddev
, rdev
);
487 if (!test_bit(Faulty
, &rdev
->flags
))
488 /* This is the only remaining device,
489 * We need to retry the write without
492 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
494 r10_bio
->devs
[slot
].bio
= NULL
;
499 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
503 * Set R10BIO_Uptodate in our master bio, so that
504 * we will return a good error code for to the higher
505 * levels even if IO on some other mirrored buffer fails.
507 * The 'master' represents the composite IO operation to
508 * user-side. So if something waits for IO, then it will
509 * wait for the 'master' bio.
515 * Do not set R10BIO_Uptodate if the current device is
516 * rebuilding or Faulty. This is because we cannot use
517 * such device for properly reading the data back (we could
518 * potentially use it, if the current write would have felt
519 * before rdev->recovery_offset, but for simplicity we don't
522 if (test_bit(In_sync
, &rdev
->flags
) &&
523 !test_bit(Faulty
, &rdev
->flags
))
524 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
526 /* Maybe we can clear some bad blocks. */
527 if (is_badblock(rdev
,
528 r10_bio
->devs
[slot
].addr
,
530 &first_bad
, &bad_sectors
) && !discard_error
) {
533 r10_bio
->devs
[slot
].repl_bio
= IO_MADE_GOOD
;
535 r10_bio
->devs
[slot
].bio
= IO_MADE_GOOD
;
537 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
543 * Let's see if all mirrored write operations have finished
546 one_write_done(r10_bio
);
548 rdev_dec_pending(rdev
, conf
->mddev
);
554 * RAID10 layout manager
555 * As well as the chunksize and raid_disks count, there are two
556 * parameters: near_copies and far_copies.
557 * near_copies * far_copies must be <= raid_disks.
558 * Normally one of these will be 1.
559 * If both are 1, we get raid0.
560 * If near_copies == raid_disks, we get raid1.
562 * Chunks are laid out in raid0 style with near_copies copies of the
563 * first chunk, followed by near_copies copies of the next chunk and
565 * If far_copies > 1, then after 1/far_copies of the array has been assigned
566 * as described above, we start again with a device offset of near_copies.
567 * So we effectively have another copy of the whole array further down all
568 * the drives, but with blocks on different drives.
569 * With this layout, and block is never stored twice on the one device.
571 * raid10_find_phys finds the sector offset of a given virtual sector
572 * on each device that it is on.
574 * raid10_find_virt does the reverse mapping, from a device and a
575 * sector offset to a virtual address
578 static void __raid10_find_phys(struct geom
*geo
, struct r10bio
*r10bio
)
586 int last_far_set_start
, last_far_set_size
;
588 last_far_set_start
= (geo
->raid_disks
/ geo
->far_set_size
) - 1;
589 last_far_set_start
*= geo
->far_set_size
;
591 last_far_set_size
= geo
->far_set_size
;
592 last_far_set_size
+= (geo
->raid_disks
% geo
->far_set_size
);
594 /* now calculate first sector/dev */
595 chunk
= r10bio
->sector
>> geo
->chunk_shift
;
596 sector
= r10bio
->sector
& geo
->chunk_mask
;
598 chunk
*= geo
->near_copies
;
600 dev
= sector_div(stripe
, geo
->raid_disks
);
602 stripe
*= geo
->far_copies
;
604 sector
+= stripe
<< geo
->chunk_shift
;
606 /* and calculate all the others */
607 for (n
= 0; n
< geo
->near_copies
; n
++) {
611 r10bio
->devs
[slot
].devnum
= d
;
612 r10bio
->devs
[slot
].addr
= s
;
615 for (f
= 1; f
< geo
->far_copies
; f
++) {
616 set
= d
/ geo
->far_set_size
;
617 d
+= geo
->near_copies
;
619 if ((geo
->raid_disks
% geo
->far_set_size
) &&
620 (d
> last_far_set_start
)) {
621 d
-= last_far_set_start
;
622 d
%= last_far_set_size
;
623 d
+= last_far_set_start
;
625 d
%= geo
->far_set_size
;
626 d
+= geo
->far_set_size
* set
;
629 r10bio
->devs
[slot
].devnum
= d
;
630 r10bio
->devs
[slot
].addr
= s
;
634 if (dev
>= geo
->raid_disks
) {
636 sector
+= (geo
->chunk_mask
+ 1);
641 static void raid10_find_phys(struct r10conf
*conf
, struct r10bio
*r10bio
)
643 struct geom
*geo
= &conf
->geo
;
645 if (conf
->reshape_progress
!= MaxSector
&&
646 ((r10bio
->sector
>= conf
->reshape_progress
) !=
647 conf
->mddev
->reshape_backwards
)) {
648 set_bit(R10BIO_Previous
, &r10bio
->state
);
651 clear_bit(R10BIO_Previous
, &r10bio
->state
);
653 __raid10_find_phys(geo
, r10bio
);
656 static sector_t
raid10_find_virt(struct r10conf
*conf
, sector_t sector
, int dev
)
658 sector_t offset
, chunk
, vchunk
;
659 /* Never use conf->prev as this is only called during resync
660 * or recovery, so reshape isn't happening
662 struct geom
*geo
= &conf
->geo
;
663 int far_set_start
= (dev
/ geo
->far_set_size
) * geo
->far_set_size
;
664 int far_set_size
= geo
->far_set_size
;
665 int last_far_set_start
;
667 if (geo
->raid_disks
% geo
->far_set_size
) {
668 last_far_set_start
= (geo
->raid_disks
/ geo
->far_set_size
) - 1;
669 last_far_set_start
*= geo
->far_set_size
;
671 if (dev
>= last_far_set_start
) {
672 far_set_size
= geo
->far_set_size
;
673 far_set_size
+= (geo
->raid_disks
% geo
->far_set_size
);
674 far_set_start
= last_far_set_start
;
678 offset
= sector
& geo
->chunk_mask
;
679 if (geo
->far_offset
) {
681 chunk
= sector
>> geo
->chunk_shift
;
682 fc
= sector_div(chunk
, geo
->far_copies
);
683 dev
-= fc
* geo
->near_copies
;
684 if (dev
< far_set_start
)
687 while (sector
>= geo
->stride
) {
688 sector
-= geo
->stride
;
689 if (dev
< (geo
->near_copies
+ far_set_start
))
690 dev
+= far_set_size
- geo
->near_copies
;
692 dev
-= geo
->near_copies
;
694 chunk
= sector
>> geo
->chunk_shift
;
696 vchunk
= chunk
* geo
->raid_disks
+ dev
;
697 sector_div(vchunk
, geo
->near_copies
);
698 return (vchunk
<< geo
->chunk_shift
) + offset
;
702 * This routine returns the disk from which the requested read should
703 * be done. There is a per-array 'next expected sequential IO' sector
704 * number - if this matches on the next IO then we use the last disk.
705 * There is also a per-disk 'last know head position' sector that is
706 * maintained from IRQ contexts, both the normal and the resync IO
707 * completion handlers update this position correctly. If there is no
708 * perfect sequential match then we pick the disk whose head is closest.
710 * If there are 2 mirrors in the same 2 devices, performance degrades
711 * because position is mirror, not device based.
713 * The rdev for the device selected will have nr_pending incremented.
717 * FIXME: possibly should rethink readbalancing and do it differently
718 * depending on near_copies / far_copies geometry.
720 static struct md_rdev
*read_balance(struct r10conf
*conf
,
721 struct r10bio
*r10_bio
,
724 const sector_t this_sector
= r10_bio
->sector
;
726 int sectors
= r10_bio
->sectors
;
727 int best_good_sectors
;
728 sector_t new_distance
, best_dist
;
729 struct md_rdev
*best_rdev
, *rdev
= NULL
;
732 struct geom
*geo
= &conf
->geo
;
734 raid10_find_phys(conf
, r10_bio
);
736 sectors
= r10_bio
->sectors
;
739 best_dist
= MaxSector
;
740 best_good_sectors
= 0;
742 clear_bit(R10BIO_FailFast
, &r10_bio
->state
);
744 * Check if we can balance. We can balance on the whole
745 * device if no resync is going on (recovery is ok), or below
746 * the resync window. We take the first readable disk when
747 * above the resync window.
749 if (conf
->mddev
->recovery_cp
< MaxSector
750 && (this_sector
+ sectors
>= conf
->next_resync
))
753 for (slot
= 0; slot
< conf
->copies
; slot
++) {
758 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
760 disk
= r10_bio
->devs
[slot
].devnum
;
761 rdev
= rcu_dereference(conf
->mirrors
[disk
].replacement
);
762 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
) ||
763 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
764 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
766 test_bit(Faulty
, &rdev
->flags
))
768 if (!test_bit(In_sync
, &rdev
->flags
) &&
769 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
772 dev_sector
= r10_bio
->devs
[slot
].addr
;
773 if (is_badblock(rdev
, dev_sector
, sectors
,
774 &first_bad
, &bad_sectors
)) {
775 if (best_dist
< MaxSector
)
776 /* Already have a better slot */
778 if (first_bad
<= dev_sector
) {
779 /* Cannot read here. If this is the
780 * 'primary' device, then we must not read
781 * beyond 'bad_sectors' from another device.
783 bad_sectors
-= (dev_sector
- first_bad
);
784 if (!do_balance
&& sectors
> bad_sectors
)
785 sectors
= bad_sectors
;
786 if (best_good_sectors
> sectors
)
787 best_good_sectors
= sectors
;
789 sector_t good_sectors
=
790 first_bad
- dev_sector
;
791 if (good_sectors
> best_good_sectors
) {
792 best_good_sectors
= good_sectors
;
797 /* Must read from here */
802 best_good_sectors
= sectors
;
808 /* At least 2 disks to choose from so failfast is OK */
809 set_bit(R10BIO_FailFast
, &r10_bio
->state
);
810 /* This optimisation is debatable, and completely destroys
811 * sequential read speed for 'far copies' arrays. So only
812 * keep it for 'near' arrays, and review those later.
814 if (geo
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
))
817 /* for far > 1 always use the lowest address */
818 else if (geo
->far_copies
> 1)
819 new_distance
= r10_bio
->devs
[slot
].addr
;
821 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
822 conf
->mirrors
[disk
].head_position
);
823 if (new_distance
< best_dist
) {
824 best_dist
= new_distance
;
829 if (slot
>= conf
->copies
) {
835 atomic_inc(&rdev
->nr_pending
);
836 r10_bio
->read_slot
= slot
;
840 *max_sectors
= best_good_sectors
;
845 static int raid10_congested(struct mddev
*mddev
, int bits
)
847 struct r10conf
*conf
= mddev
->private;
850 if ((bits
& (1 << WB_async_congested
)) &&
851 conf
->pending_count
>= max_queued_requests
)
856 (i
< conf
->geo
.raid_disks
|| i
< conf
->prev
.raid_disks
)
859 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
860 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
861 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
863 ret
|= bdi_congested(q
->backing_dev_info
, bits
);
870 static void flush_pending_writes(struct r10conf
*conf
)
872 /* Any writes that have been queued but are awaiting
873 * bitmap updates get flushed here.
875 spin_lock_irq(&conf
->device_lock
);
877 if (conf
->pending_bio_list
.head
) {
879 bio
= bio_list_get(&conf
->pending_bio_list
);
880 conf
->pending_count
= 0;
881 spin_unlock_irq(&conf
->device_lock
);
882 /* flush any pending bitmap writes to disk
883 * before proceeding w/ I/O */
884 bitmap_unplug(conf
->mddev
->bitmap
);
885 wake_up(&conf
->wait_barrier
);
887 while (bio
) { /* submit pending writes */
888 struct bio
*next
= bio
->bi_next
;
889 struct md_rdev
*rdev
= (void*)bio
->bi_bdev
;
891 bio
->bi_bdev
= rdev
->bdev
;
892 if (test_bit(Faulty
, &rdev
->flags
)) {
893 bio
->bi_error
= -EIO
;
895 } else if (unlikely((bio_op(bio
) == REQ_OP_DISCARD
) &&
896 !blk_queue_discard(bdev_get_queue(bio
->bi_bdev
))))
900 generic_make_request(bio
);
904 spin_unlock_irq(&conf
->device_lock
);
908 * Sometimes we need to suspend IO while we do something else,
909 * either some resync/recovery, or reconfigure the array.
910 * To do this we raise a 'barrier'.
911 * The 'barrier' is a counter that can be raised multiple times
912 * to count how many activities are happening which preclude
914 * We can only raise the barrier if there is no pending IO.
915 * i.e. if nr_pending == 0.
916 * We choose only to raise the barrier if no-one is waiting for the
917 * barrier to go down. This means that as soon as an IO request
918 * is ready, no other operations which require a barrier will start
919 * until the IO request has had a chance.
921 * So: regular IO calls 'wait_barrier'. When that returns there
922 * is no backgroup IO happening, It must arrange to call
923 * allow_barrier when it has finished its IO.
924 * backgroup IO calls must call raise_barrier. Once that returns
925 * there is no normal IO happeing. It must arrange to call
926 * lower_barrier when the particular background IO completes.
929 static void raise_barrier(struct r10conf
*conf
, int force
)
931 BUG_ON(force
&& !conf
->barrier
);
932 spin_lock_irq(&conf
->resync_lock
);
934 /* Wait until no block IO is waiting (unless 'force') */
935 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
938 /* block any new IO from starting */
941 /* Now wait for all pending IO to complete */
942 wait_event_lock_irq(conf
->wait_barrier
,
943 !atomic_read(&conf
->nr_pending
) && conf
->barrier
< RESYNC_DEPTH
,
946 spin_unlock_irq(&conf
->resync_lock
);
949 static void lower_barrier(struct r10conf
*conf
)
952 spin_lock_irqsave(&conf
->resync_lock
, flags
);
954 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
955 wake_up(&conf
->wait_barrier
);
958 static void wait_barrier(struct r10conf
*conf
)
960 spin_lock_irq(&conf
->resync_lock
);
963 /* Wait for the barrier to drop.
964 * However if there are already pending
965 * requests (preventing the barrier from
966 * rising completely), and the
967 * pre-process bio queue isn't empty,
968 * then don't wait, as we need to empty
969 * that queue to get the nr_pending
972 raid10_log(conf
->mddev
, "wait barrier");
973 wait_event_lock_irq(conf
->wait_barrier
,
975 (atomic_read(&conf
->nr_pending
) &&
977 !bio_list_empty(current
->bio_list
)),
980 if (!conf
->nr_waiting
)
981 wake_up(&conf
->wait_barrier
);
983 atomic_inc(&conf
->nr_pending
);
984 spin_unlock_irq(&conf
->resync_lock
);
987 static void allow_barrier(struct r10conf
*conf
)
989 if ((atomic_dec_and_test(&conf
->nr_pending
)) ||
990 (conf
->array_freeze_pending
))
991 wake_up(&conf
->wait_barrier
);
994 static void freeze_array(struct r10conf
*conf
, int extra
)
996 /* stop syncio and normal IO and wait for everything to
998 * We increment barrier and nr_waiting, and then
999 * wait until nr_pending match nr_queued+extra
1000 * This is called in the context of one normal IO request
1001 * that has failed. Thus any sync request that might be pending
1002 * will be blocked by nr_pending, and we need to wait for
1003 * pending IO requests to complete or be queued for re-try.
1004 * Thus the number queued (nr_queued) plus this request (extra)
1005 * must match the number of pending IOs (nr_pending) before
1008 spin_lock_irq(&conf
->resync_lock
);
1009 conf
->array_freeze_pending
++;
1012 wait_event_lock_irq_cmd(conf
->wait_barrier
,
1013 atomic_read(&conf
->nr_pending
) == conf
->nr_queued
+extra
,
1015 flush_pending_writes(conf
));
1017 conf
->array_freeze_pending
--;
1018 spin_unlock_irq(&conf
->resync_lock
);
1021 static void unfreeze_array(struct r10conf
*conf
)
1023 /* reverse the effect of the freeze */
1024 spin_lock_irq(&conf
->resync_lock
);
1027 wake_up(&conf
->wait_barrier
);
1028 spin_unlock_irq(&conf
->resync_lock
);
1031 static sector_t
choose_data_offset(struct r10bio
*r10_bio
,
1032 struct md_rdev
*rdev
)
1034 if (!test_bit(MD_RECOVERY_RESHAPE
, &rdev
->mddev
->recovery
) ||
1035 test_bit(R10BIO_Previous
, &r10_bio
->state
))
1036 return rdev
->data_offset
;
1038 return rdev
->new_data_offset
;
1041 struct raid10_plug_cb
{
1042 struct blk_plug_cb cb
;
1043 struct bio_list pending
;
1047 static void raid10_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
1049 struct raid10_plug_cb
*plug
= container_of(cb
, struct raid10_plug_cb
,
1051 struct mddev
*mddev
= plug
->cb
.data
;
1052 struct r10conf
*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 struct md_rdev
*rdev
= (void*)bio
->bi_bdev
;
1074 bio
->bi_next
= NULL
;
1075 bio
->bi_bdev
= rdev
->bdev
;
1076 if (test_bit(Faulty
, &rdev
->flags
)) {
1077 bio
->bi_error
= -EIO
;
1079 } else if (unlikely((bio_op(bio
) == REQ_OP_DISCARD
) &&
1080 !blk_queue_discard(bdev_get_queue(bio
->bi_bdev
))))
1081 /* Just ignore it */
1084 generic_make_request(bio
);
1090 static void raid10_read_request(struct mddev
*mddev
, struct bio
*bio
,
1091 struct r10bio
*r10_bio
)
1093 struct r10conf
*conf
= mddev
->private;
1094 struct bio
*read_bio
;
1095 const int op
= bio_op(bio
);
1096 const unsigned long do_sync
= (bio
->bi_opf
& REQ_SYNC
);
1097 int sectors_handled
;
1100 struct md_rdev
*rdev
;
1104 * Register the new request and wait if the reconstruction
1105 * thread has put up a bar for new requests.
1106 * Continue immediately if no resync is active currently.
1110 sectors
= bio_sectors(bio
);
1111 while (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1112 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
&&
1113 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
) {
1115 * IO spans the reshape position. Need to wait for reshape to
1118 raid10_log(conf
->mddev
, "wait reshape");
1119 allow_barrier(conf
);
1120 wait_event(conf
->wait_barrier
,
1121 conf
->reshape_progress
<= bio
->bi_iter
.bi_sector
||
1122 conf
->reshape_progress
>= bio
->bi_iter
.bi_sector
+
1128 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
1130 raid_end_bio_io(r10_bio
);
1133 slot
= r10_bio
->read_slot
;
1135 read_bio
= bio_clone_fast(bio
, GFP_NOIO
, mddev
->bio_set
);
1136 bio_trim(read_bio
, r10_bio
->sector
- bio
->bi_iter
.bi_sector
,
1139 r10_bio
->devs
[slot
].bio
= read_bio
;
1140 r10_bio
->devs
[slot
].rdev
= rdev
;
1142 read_bio
->bi_iter
.bi_sector
= r10_bio
->devs
[slot
].addr
+
1143 choose_data_offset(r10_bio
, rdev
);
1144 read_bio
->bi_bdev
= rdev
->bdev
;
1145 read_bio
->bi_end_io
= raid10_end_read_request
;
1146 bio_set_op_attrs(read_bio
, op
, do_sync
);
1147 if (test_bit(FailFast
, &rdev
->flags
) &&
1148 test_bit(R10BIO_FailFast
, &r10_bio
->state
))
1149 read_bio
->bi_opf
|= MD_FAILFAST
;
1150 read_bio
->bi_private
= r10_bio
;
1153 trace_block_bio_remap(bdev_get_queue(read_bio
->bi_bdev
),
1154 read_bio
, disk_devt(mddev
->gendisk
),
1156 if (max_sectors
< r10_bio
->sectors
) {
1158 * Could not read all from this device, so we will need another
1161 sectors_handled
= (r10_bio
->sector
+ max_sectors
1162 - bio
->bi_iter
.bi_sector
);
1163 r10_bio
->sectors
= max_sectors
;
1164 spin_lock_irq(&conf
->device_lock
);
1165 if (bio
->bi_phys_segments
== 0)
1166 bio
->bi_phys_segments
= 2;
1168 bio
->bi_phys_segments
++;
1169 spin_unlock_irq(&conf
->device_lock
);
1171 * Cannot call generic_make_request directly as that will be
1172 * queued in __generic_make_request and subsequent
1173 * mempool_alloc might block waiting for it. so hand bio over
1176 reschedule_retry(r10_bio
);
1178 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
1180 r10_bio
->master_bio
= bio
;
1181 r10_bio
->sectors
= bio_sectors(bio
) - sectors_handled
;
1183 r10_bio
->mddev
= mddev
;
1184 r10_bio
->sector
= bio
->bi_iter
.bi_sector
+ sectors_handled
;
1187 generic_make_request(read_bio
);
1191 static void raid10_write_request(struct mddev
*mddev
, struct bio
*bio
,
1192 struct r10bio
*r10_bio
)
1194 struct r10conf
*conf
= mddev
->private;
1196 const int op
= bio_op(bio
);
1197 const unsigned long do_sync
= (bio
->bi_opf
& REQ_SYNC
);
1198 const unsigned long do_fua
= (bio
->bi_opf
& REQ_FUA
);
1199 unsigned long flags
;
1200 struct md_rdev
*blocked_rdev
;
1201 struct blk_plug_cb
*cb
;
1202 struct raid10_plug_cb
*plug
= NULL
;
1204 int sectors_handled
;
1207 md_write_start(mddev
, bio
);
1210 * Register the new request and wait if the reconstruction
1211 * thread has put up a bar for new requests.
1212 * Continue immediately if no resync is active currently.
1216 sectors
= bio_sectors(bio
);
1217 while (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1218 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
&&
1219 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
) {
1221 * IO spans the reshape position. Need to wait for reshape to
1224 raid10_log(conf
->mddev
, "wait reshape");
1225 allow_barrier(conf
);
1226 wait_event(conf
->wait_barrier
,
1227 conf
->reshape_progress
<= bio
->bi_iter
.bi_sector
||
1228 conf
->reshape_progress
>= bio
->bi_iter
.bi_sector
+
1233 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1234 (mddev
->reshape_backwards
1235 ? (bio
->bi_iter
.bi_sector
< conf
->reshape_safe
&&
1236 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
)
1237 : (bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_safe
&&
1238 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
))) {
1239 /* Need to update reshape_position in metadata */
1240 mddev
->reshape_position
= conf
->reshape_progress
;
1241 set_mask_bits(&mddev
->sb_flags
, 0,
1242 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
1243 md_wakeup_thread(mddev
->thread
);
1244 raid10_log(conf
->mddev
, "wait reshape metadata");
1245 wait_event(mddev
->sb_wait
,
1246 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
1248 conf
->reshape_safe
= mddev
->reshape_position
;
1251 if (conf
->pending_count
>= max_queued_requests
) {
1252 md_wakeup_thread(mddev
->thread
);
1253 raid10_log(mddev
, "wait queued");
1254 wait_event(conf
->wait_barrier
,
1255 conf
->pending_count
< max_queued_requests
);
1257 /* first select target devices under rcu_lock and
1258 * inc refcount on their rdev. Record them by setting
1260 * If there are known/acknowledged bad blocks on any device
1261 * on which we have seen a write error, we want to avoid
1262 * writing to those blocks. This potentially requires several
1263 * writes to write around the bad blocks. Each set of writes
1264 * gets its own r10_bio with a set of bios attached. The number
1265 * of r10_bios is recored in bio->bi_phys_segments just as with
1269 r10_bio
->read_slot
= -1; /* make sure repl_bio gets freed */
1270 raid10_find_phys(conf
, r10_bio
);
1272 blocked_rdev
= NULL
;
1274 max_sectors
= r10_bio
->sectors
;
1276 for (i
= 0; i
< conf
->copies
; i
++) {
1277 int d
= r10_bio
->devs
[i
].devnum
;
1278 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1279 struct md_rdev
*rrdev
= rcu_dereference(
1280 conf
->mirrors
[d
].replacement
);
1283 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
1284 atomic_inc(&rdev
->nr_pending
);
1285 blocked_rdev
= rdev
;
1288 if (rrdev
&& unlikely(test_bit(Blocked
, &rrdev
->flags
))) {
1289 atomic_inc(&rrdev
->nr_pending
);
1290 blocked_rdev
= rrdev
;
1293 if (rdev
&& (test_bit(Faulty
, &rdev
->flags
)))
1295 if (rrdev
&& (test_bit(Faulty
, &rrdev
->flags
)))
1298 r10_bio
->devs
[i
].bio
= NULL
;
1299 r10_bio
->devs
[i
].repl_bio
= NULL
;
1301 if (!rdev
&& !rrdev
) {
1302 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
1305 if (rdev
&& test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1307 sector_t dev_sector
= r10_bio
->devs
[i
].addr
;
1311 is_bad
= is_badblock(rdev
, dev_sector
, max_sectors
,
1312 &first_bad
, &bad_sectors
);
1314 /* Mustn't write here until the bad block
1317 atomic_inc(&rdev
->nr_pending
);
1318 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1319 blocked_rdev
= rdev
;
1322 if (is_bad
&& first_bad
<= dev_sector
) {
1323 /* Cannot write here at all */
1324 bad_sectors
-= (dev_sector
- first_bad
);
1325 if (bad_sectors
< max_sectors
)
1326 /* Mustn't write more than bad_sectors
1327 * to other devices yet
1329 max_sectors
= bad_sectors
;
1330 /* We don't set R10BIO_Degraded as that
1331 * only applies if the disk is missing,
1332 * so it might be re-added, and we want to
1333 * know to recover this chunk.
1334 * In this case the device is here, and the
1335 * fact that this chunk is not in-sync is
1336 * recorded in the bad block log.
1341 int good_sectors
= first_bad
- dev_sector
;
1342 if (good_sectors
< max_sectors
)
1343 max_sectors
= good_sectors
;
1347 r10_bio
->devs
[i
].bio
= bio
;
1348 atomic_inc(&rdev
->nr_pending
);
1351 r10_bio
->devs
[i
].repl_bio
= bio
;
1352 atomic_inc(&rrdev
->nr_pending
);
1357 if (unlikely(blocked_rdev
)) {
1358 /* Have to wait for this device to get unblocked, then retry */
1362 for (j
= 0; j
< i
; j
++) {
1363 if (r10_bio
->devs
[j
].bio
) {
1364 d
= r10_bio
->devs
[j
].devnum
;
1365 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1367 if (r10_bio
->devs
[j
].repl_bio
) {
1368 struct md_rdev
*rdev
;
1369 d
= r10_bio
->devs
[j
].devnum
;
1370 rdev
= conf
->mirrors
[d
].replacement
;
1372 /* Race with remove_disk */
1374 rdev
= conf
->mirrors
[d
].rdev
;
1376 rdev_dec_pending(rdev
, mddev
);
1379 allow_barrier(conf
);
1380 raid10_log(conf
->mddev
, "wait rdev %d blocked", blocked_rdev
->raid_disk
);
1381 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1386 if (max_sectors
< r10_bio
->sectors
) {
1387 /* We are splitting this into multiple parts, so
1388 * we need to prepare for allocating another r10_bio.
1390 r10_bio
->sectors
= max_sectors
;
1391 spin_lock_irq(&conf
->device_lock
);
1392 if (bio
->bi_phys_segments
== 0)
1393 bio
->bi_phys_segments
= 2;
1395 bio
->bi_phys_segments
++;
1396 spin_unlock_irq(&conf
->device_lock
);
1398 sectors_handled
= r10_bio
->sector
+ max_sectors
-
1399 bio
->bi_iter
.bi_sector
;
1401 atomic_set(&r10_bio
->remaining
, 1);
1402 bitmap_startwrite(mddev
->bitmap
, r10_bio
->sector
, r10_bio
->sectors
, 0);
1404 for (i
= 0; i
< conf
->copies
; i
++) {
1406 int d
= r10_bio
->devs
[i
].devnum
;
1407 if (r10_bio
->devs
[i
].bio
) {
1408 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
1409 mbio
= bio_clone_fast(bio
, GFP_NOIO
, mddev
->bio_set
);
1410 bio_trim(mbio
, r10_bio
->sector
- bio
->bi_iter
.bi_sector
,
1412 r10_bio
->devs
[i
].bio
= mbio
;
1414 mbio
->bi_iter
.bi_sector
= (r10_bio
->devs
[i
].addr
+
1415 choose_data_offset(r10_bio
, rdev
));
1416 mbio
->bi_bdev
= rdev
->bdev
;
1417 mbio
->bi_end_io
= raid10_end_write_request
;
1418 bio_set_op_attrs(mbio
, op
, do_sync
| do_fua
);
1419 if (test_bit(FailFast
, &conf
->mirrors
[d
].rdev
->flags
) &&
1421 mbio
->bi_opf
|= MD_FAILFAST
;
1422 mbio
->bi_private
= r10_bio
;
1424 if (conf
->mddev
->gendisk
)
1425 trace_block_bio_remap(bdev_get_queue(mbio
->bi_bdev
),
1426 mbio
, disk_devt(conf
->mddev
->gendisk
),
1428 /* flush_pending_writes() needs access to the rdev so...*/
1429 mbio
->bi_bdev
= (void*)rdev
;
1431 atomic_inc(&r10_bio
->remaining
);
1433 cb
= blk_check_plugged(raid10_unplug
, mddev
,
1436 plug
= container_of(cb
, struct raid10_plug_cb
,
1440 spin_lock_irqsave(&conf
->device_lock
, flags
);
1442 bio_list_add(&plug
->pending
, mbio
);
1443 plug
->pending_cnt
++;
1445 bio_list_add(&conf
->pending_bio_list
, mbio
);
1446 conf
->pending_count
++;
1448 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1450 md_wakeup_thread(mddev
->thread
);
1453 if (r10_bio
->devs
[i
].repl_bio
) {
1454 struct md_rdev
*rdev
= conf
->mirrors
[d
].replacement
;
1456 /* Replacement just got moved to main 'rdev' */
1458 rdev
= conf
->mirrors
[d
].rdev
;
1460 mbio
= bio_clone_fast(bio
, GFP_NOIO
, mddev
->bio_set
);
1461 bio_trim(mbio
, r10_bio
->sector
- bio
->bi_iter
.bi_sector
,
1463 r10_bio
->devs
[i
].repl_bio
= mbio
;
1465 mbio
->bi_iter
.bi_sector
= (r10_bio
->devs
[i
].addr
+
1466 choose_data_offset(r10_bio
, rdev
));
1467 mbio
->bi_bdev
= rdev
->bdev
;
1468 mbio
->bi_end_io
= raid10_end_write_request
;
1469 bio_set_op_attrs(mbio
, op
, do_sync
| do_fua
);
1470 mbio
->bi_private
= r10_bio
;
1472 if (conf
->mddev
->gendisk
)
1473 trace_block_bio_remap(bdev_get_queue(mbio
->bi_bdev
),
1474 mbio
, disk_devt(conf
->mddev
->gendisk
),
1476 /* flush_pending_writes() needs access to the rdev so...*/
1477 mbio
->bi_bdev
= (void*)rdev
;
1479 atomic_inc(&r10_bio
->remaining
);
1480 spin_lock_irqsave(&conf
->device_lock
, flags
);
1481 bio_list_add(&conf
->pending_bio_list
, mbio
);
1482 conf
->pending_count
++;
1483 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1484 if (!mddev_check_plugged(mddev
))
1485 md_wakeup_thread(mddev
->thread
);
1489 /* Don't remove the bias on 'remaining' (one_write_done) until
1490 * after checking if we need to go around again.
1493 if (sectors_handled
< bio_sectors(bio
)) {
1494 one_write_done(r10_bio
);
1495 /* We need another r10_bio. It has already been counted
1496 * in bio->bi_phys_segments.
1498 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
1500 r10_bio
->master_bio
= bio
;
1501 r10_bio
->sectors
= bio_sectors(bio
) - sectors_handled
;
1503 r10_bio
->mddev
= mddev
;
1504 r10_bio
->sector
= bio
->bi_iter
.bi_sector
+ sectors_handled
;
1508 one_write_done(r10_bio
);
1511 static void __make_request(struct mddev
*mddev
, struct bio
*bio
)
1513 struct r10conf
*conf
= mddev
->private;
1514 struct r10bio
*r10_bio
;
1516 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
1518 r10_bio
->master_bio
= bio
;
1519 r10_bio
->sectors
= bio_sectors(bio
);
1521 r10_bio
->mddev
= mddev
;
1522 r10_bio
->sector
= bio
->bi_iter
.bi_sector
;
1526 * We might need to issue multiple reads to different devices if there
1527 * are bad blocks around, so we keep track of the number of reads in
1528 * bio->bi_phys_segments. If this is 0, there is only one r10_bio and
1529 * no locking will be needed when the request completes. If it is
1530 * non-zero, then it is the number of not-completed requests.
1532 bio
->bi_phys_segments
= 0;
1533 bio_clear_flag(bio
, BIO_SEG_VALID
);
1535 if (bio_data_dir(bio
) == READ
)
1536 raid10_read_request(mddev
, bio
, r10_bio
);
1538 raid10_write_request(mddev
, bio
, r10_bio
);
1541 static void raid10_make_request(struct mddev
*mddev
, struct bio
*bio
)
1543 struct r10conf
*conf
= mddev
->private;
1544 sector_t chunk_mask
= (conf
->geo
.chunk_mask
& conf
->prev
.chunk_mask
);
1545 int chunk_sects
= chunk_mask
+ 1;
1549 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
1550 md_flush_request(mddev
, bio
);
1557 * If this request crosses a chunk boundary, we need to split
1560 if (unlikely((bio
->bi_iter
.bi_sector
& chunk_mask
) +
1561 bio_sectors(bio
) > chunk_sects
1562 && (conf
->geo
.near_copies
< conf
->geo
.raid_disks
1563 || conf
->prev
.near_copies
<
1564 conf
->prev
.raid_disks
))) {
1565 split
= bio_split(bio
, chunk_sects
-
1566 (bio
->bi_iter
.bi_sector
&
1568 GFP_NOIO
, fs_bio_set
);
1569 bio_chain(split
, bio
);
1574 __make_request(mddev
, split
);
1575 } while (split
!= bio
);
1577 /* In case raid10d snuck in to freeze_array */
1578 wake_up(&conf
->wait_barrier
);
1581 static void raid10_status(struct seq_file
*seq
, struct mddev
*mddev
)
1583 struct r10conf
*conf
= mddev
->private;
1586 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
)
1587 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
1588 if (conf
->geo
.near_copies
> 1)
1589 seq_printf(seq
, " %d near-copies", conf
->geo
.near_copies
);
1590 if (conf
->geo
.far_copies
> 1) {
1591 if (conf
->geo
.far_offset
)
1592 seq_printf(seq
, " %d offset-copies", conf
->geo
.far_copies
);
1594 seq_printf(seq
, " %d far-copies", conf
->geo
.far_copies
);
1595 if (conf
->geo
.far_set_size
!= conf
->geo
.raid_disks
)
1596 seq_printf(seq
, " %d devices per set", conf
->geo
.far_set_size
);
1598 seq_printf(seq
, " [%d/%d] [", conf
->geo
.raid_disks
,
1599 conf
->geo
.raid_disks
- mddev
->degraded
);
1601 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1602 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1603 seq_printf(seq
, "%s", rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
1606 seq_printf(seq
, "]");
1609 /* check if there are enough drives for
1610 * every block to appear on atleast one.
1611 * Don't consider the device numbered 'ignore'
1612 * as we might be about to remove it.
1614 static int _enough(struct r10conf
*conf
, int previous
, int ignore
)
1620 disks
= conf
->prev
.raid_disks
;
1621 ncopies
= conf
->prev
.near_copies
;
1623 disks
= conf
->geo
.raid_disks
;
1624 ncopies
= conf
->geo
.near_copies
;
1629 int n
= conf
->copies
;
1633 struct md_rdev
*rdev
;
1634 if (this != ignore
&&
1635 (rdev
= rcu_dereference(conf
->mirrors
[this].rdev
)) &&
1636 test_bit(In_sync
, &rdev
->flags
))
1638 this = (this+1) % disks
;
1642 first
= (first
+ ncopies
) % disks
;
1643 } while (first
!= 0);
1650 static int enough(struct r10conf
*conf
, int ignore
)
1652 /* when calling 'enough', both 'prev' and 'geo' must
1654 * This is ensured if ->reconfig_mutex or ->device_lock
1657 return _enough(conf
, 0, ignore
) &&
1658 _enough(conf
, 1, ignore
);
1661 static void raid10_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1663 char b
[BDEVNAME_SIZE
];
1664 struct r10conf
*conf
= mddev
->private;
1665 unsigned long flags
;
1668 * If it is not operational, then we have already marked it as dead
1669 * else if it is the last working disks, ignore the error, let the
1670 * next level up know.
1671 * else mark the drive as failed
1673 spin_lock_irqsave(&conf
->device_lock
, flags
);
1674 if (test_bit(In_sync
, &rdev
->flags
)
1675 && !enough(conf
, rdev
->raid_disk
)) {
1677 * Don't fail the drive, just return an IO error.
1679 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1682 if (test_and_clear_bit(In_sync
, &rdev
->flags
))
1685 * If recovery is running, make sure it aborts.
1687 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1688 set_bit(Blocked
, &rdev
->flags
);
1689 set_bit(Faulty
, &rdev
->flags
);
1690 set_mask_bits(&mddev
->sb_flags
, 0,
1691 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
1692 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1693 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1694 "md/raid10:%s: Operation continuing on %d devices.\n",
1695 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1696 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
);
1699 static void print_conf(struct r10conf
*conf
)
1702 struct md_rdev
*rdev
;
1704 pr_debug("RAID10 conf printout:\n");
1706 pr_debug("(!conf)\n");
1709 pr_debug(" --- wd:%d rd:%d\n", conf
->geo
.raid_disks
- conf
->mddev
->degraded
,
1710 conf
->geo
.raid_disks
);
1712 /* This is only called with ->reconfix_mutex held, so
1713 * rcu protection of rdev is not needed */
1714 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1715 char b
[BDEVNAME_SIZE
];
1716 rdev
= conf
->mirrors
[i
].rdev
;
1718 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1719 i
, !test_bit(In_sync
, &rdev
->flags
),
1720 !test_bit(Faulty
, &rdev
->flags
),
1721 bdevname(rdev
->bdev
,b
));
1725 static void close_sync(struct r10conf
*conf
)
1728 allow_barrier(conf
);
1730 mempool_destroy(conf
->r10buf_pool
);
1731 conf
->r10buf_pool
= NULL
;
1734 static int raid10_spare_active(struct mddev
*mddev
)
1737 struct r10conf
*conf
= mddev
->private;
1738 struct raid10_info
*tmp
;
1740 unsigned long flags
;
1743 * Find all non-in_sync disks within the RAID10 configuration
1744 * and mark them in_sync
1746 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1747 tmp
= conf
->mirrors
+ i
;
1748 if (tmp
->replacement
1749 && tmp
->replacement
->recovery_offset
== MaxSector
1750 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
1751 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
1752 /* Replacement has just become active */
1754 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
1757 /* Replaced device not technically faulty,
1758 * but we need to be sure it gets removed
1759 * and never re-added.
1761 set_bit(Faulty
, &tmp
->rdev
->flags
);
1762 sysfs_notify_dirent_safe(
1763 tmp
->rdev
->sysfs_state
);
1765 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
1766 } else if (tmp
->rdev
1767 && tmp
->rdev
->recovery_offset
== MaxSector
1768 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1769 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1771 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
1774 spin_lock_irqsave(&conf
->device_lock
, flags
);
1775 mddev
->degraded
-= count
;
1776 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1782 static int raid10_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1784 struct r10conf
*conf
= mddev
->private;
1788 int last
= conf
->geo
.raid_disks
- 1;
1790 if (mddev
->recovery_cp
< MaxSector
)
1791 /* only hot-add to in-sync arrays, as recovery is
1792 * very different from resync
1795 if (rdev
->saved_raid_disk
< 0 && !_enough(conf
, 1, -1))
1798 if (md_integrity_add_rdev(rdev
, mddev
))
1801 if (rdev
->raid_disk
>= 0)
1802 first
= last
= rdev
->raid_disk
;
1804 if (rdev
->saved_raid_disk
>= first
&&
1805 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1806 mirror
= rdev
->saved_raid_disk
;
1809 for ( ; mirror
<= last
; mirror
++) {
1810 struct raid10_info
*p
= &conf
->mirrors
[mirror
];
1811 if (p
->recovery_disabled
== mddev
->recovery_disabled
)
1814 if (!test_bit(WantReplacement
, &p
->rdev
->flags
) ||
1815 p
->replacement
!= NULL
)
1817 clear_bit(In_sync
, &rdev
->flags
);
1818 set_bit(Replacement
, &rdev
->flags
);
1819 rdev
->raid_disk
= mirror
;
1822 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1823 rdev
->data_offset
<< 9);
1825 rcu_assign_pointer(p
->replacement
, rdev
);
1830 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1831 rdev
->data_offset
<< 9);
1833 p
->head_position
= 0;
1834 p
->recovery_disabled
= mddev
->recovery_disabled
- 1;
1835 rdev
->raid_disk
= mirror
;
1837 if (rdev
->saved_raid_disk
!= mirror
)
1839 rcu_assign_pointer(p
->rdev
, rdev
);
1842 if (mddev
->queue
&& blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
1843 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, mddev
->queue
);
1849 static int raid10_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1851 struct r10conf
*conf
= mddev
->private;
1853 int number
= rdev
->raid_disk
;
1854 struct md_rdev
**rdevp
;
1855 struct raid10_info
*p
= conf
->mirrors
+ number
;
1858 if (rdev
== p
->rdev
)
1860 else if (rdev
== p
->replacement
)
1861 rdevp
= &p
->replacement
;
1865 if (test_bit(In_sync
, &rdev
->flags
) ||
1866 atomic_read(&rdev
->nr_pending
)) {
1870 /* Only remove non-faulty devices if recovery
1873 if (!test_bit(Faulty
, &rdev
->flags
) &&
1874 mddev
->recovery_disabled
!= p
->recovery_disabled
&&
1875 (!p
->replacement
|| p
->replacement
== rdev
) &&
1876 number
< conf
->geo
.raid_disks
&&
1882 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
1884 if (atomic_read(&rdev
->nr_pending
)) {
1885 /* lost the race, try later */
1891 if (p
->replacement
) {
1892 /* We must have just cleared 'rdev' */
1893 p
->rdev
= p
->replacement
;
1894 clear_bit(Replacement
, &p
->replacement
->flags
);
1895 smp_mb(); /* Make sure other CPUs may see both as identical
1896 * but will never see neither -- if they are careful.
1898 p
->replacement
= NULL
;
1899 clear_bit(WantReplacement
, &rdev
->flags
);
1901 /* We might have just remove the Replacement as faulty
1902 * Clear the flag just in case
1904 clear_bit(WantReplacement
, &rdev
->flags
);
1906 err
= md_integrity_register(mddev
);
1914 static void end_sync_read(struct bio
*bio
)
1916 struct r10bio
*r10_bio
= bio
->bi_private
;
1917 struct r10conf
*conf
= r10_bio
->mddev
->private;
1920 if (bio
== r10_bio
->master_bio
) {
1921 /* this is a reshape read */
1922 d
= r10_bio
->read_slot
; /* really the read dev */
1924 d
= find_bio_disk(conf
, r10_bio
, bio
, NULL
, NULL
);
1927 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1929 /* The write handler will notice the lack of
1930 * R10BIO_Uptodate and record any errors etc
1932 atomic_add(r10_bio
->sectors
,
1933 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1935 /* for reconstruct, we always reschedule after a read.
1936 * for resync, only after all reads
1938 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1939 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1940 atomic_dec_and_test(&r10_bio
->remaining
)) {
1941 /* we have read all the blocks,
1942 * do the comparison in process context in raid10d
1944 reschedule_retry(r10_bio
);
1948 static void end_sync_request(struct r10bio
*r10_bio
)
1950 struct mddev
*mddev
= r10_bio
->mddev
;
1952 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1953 if (r10_bio
->master_bio
== NULL
) {
1954 /* the primary of several recovery bios */
1955 sector_t s
= r10_bio
->sectors
;
1956 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
1957 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
1958 reschedule_retry(r10_bio
);
1961 md_done_sync(mddev
, s
, 1);
1964 struct r10bio
*r10_bio2
= (struct r10bio
*)r10_bio
->master_bio
;
1965 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
1966 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
1967 reschedule_retry(r10_bio
);
1975 static void end_sync_write(struct bio
*bio
)
1977 struct r10bio
*r10_bio
= bio
->bi_private
;
1978 struct mddev
*mddev
= r10_bio
->mddev
;
1979 struct r10conf
*conf
= mddev
->private;
1985 struct md_rdev
*rdev
= NULL
;
1987 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
1989 rdev
= conf
->mirrors
[d
].replacement
;
1991 rdev
= conf
->mirrors
[d
].rdev
;
1993 if (bio
->bi_error
) {
1995 md_error(mddev
, rdev
);
1997 set_bit(WriteErrorSeen
, &rdev
->flags
);
1998 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1999 set_bit(MD_RECOVERY_NEEDED
,
2000 &rdev
->mddev
->recovery
);
2001 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
2003 } else if (is_badblock(rdev
,
2004 r10_bio
->devs
[slot
].addr
,
2006 &first_bad
, &bad_sectors
))
2007 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
2009 rdev_dec_pending(rdev
, mddev
);
2011 end_sync_request(r10_bio
);
2015 * Note: sync and recover and handled very differently for raid10
2016 * This code is for resync.
2017 * For resync, we read through virtual addresses and read all blocks.
2018 * If there is any error, we schedule a write. The lowest numbered
2019 * drive is authoritative.
2020 * However requests come for physical address, so we need to map.
2021 * For every physical address there are raid_disks/copies virtual addresses,
2022 * which is always are least one, but is not necessarly an integer.
2023 * This means that a physical address can span multiple chunks, so we may
2024 * have to submit multiple io requests for a single sync request.
2027 * We check if all blocks are in-sync and only write to blocks that
2030 static void sync_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2032 struct r10conf
*conf
= mddev
->private;
2034 struct bio
*tbio
, *fbio
;
2037 atomic_set(&r10_bio
->remaining
, 1);
2039 /* find the first device with a block */
2040 for (i
=0; i
<conf
->copies
; i
++)
2041 if (!r10_bio
->devs
[i
].bio
->bi_error
)
2044 if (i
== conf
->copies
)
2048 fbio
= r10_bio
->devs
[i
].bio
;
2049 fbio
->bi_iter
.bi_size
= r10_bio
->sectors
<< 9;
2050 fbio
->bi_iter
.bi_idx
= 0;
2052 vcnt
= (r10_bio
->sectors
+ (PAGE_SIZE
>> 9) - 1) >> (PAGE_SHIFT
- 9);
2053 /* now find blocks with errors */
2054 for (i
=0 ; i
< conf
->copies
; i
++) {
2056 struct md_rdev
*rdev
;
2058 tbio
= r10_bio
->devs
[i
].bio
;
2060 if (tbio
->bi_end_io
!= end_sync_read
)
2064 d
= r10_bio
->devs
[i
].devnum
;
2065 rdev
= conf
->mirrors
[d
].rdev
;
2066 if (!r10_bio
->devs
[i
].bio
->bi_error
) {
2067 /* We know that the bi_io_vec layout is the same for
2068 * both 'first' and 'i', so we just compare them.
2069 * All vec entries are PAGE_SIZE;
2071 int sectors
= r10_bio
->sectors
;
2072 for (j
= 0; j
< vcnt
; j
++) {
2073 int len
= PAGE_SIZE
;
2074 if (sectors
< (len
/ 512))
2075 len
= sectors
* 512;
2076 if (memcmp(page_address(fbio
->bi_io_vec
[j
].bv_page
),
2077 page_address(tbio
->bi_io_vec
[j
].bv_page
),
2084 atomic64_add(r10_bio
->sectors
, &mddev
->resync_mismatches
);
2085 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2086 /* Don't fix anything. */
2088 } else if (test_bit(FailFast
, &rdev
->flags
)) {
2089 /* Just give up on this device */
2090 md_error(rdev
->mddev
, rdev
);
2093 /* Ok, we need to write this bio, either to correct an
2094 * inconsistency or to correct an unreadable block.
2095 * First we need to fixup bv_offset, bv_len and
2096 * bi_vecs, as the read request might have corrupted these
2100 tbio
->bi_vcnt
= vcnt
;
2101 tbio
->bi_iter
.bi_size
= fbio
->bi_iter
.bi_size
;
2102 tbio
->bi_private
= r10_bio
;
2103 tbio
->bi_iter
.bi_sector
= r10_bio
->devs
[i
].addr
;
2104 tbio
->bi_end_io
= end_sync_write
;
2105 bio_set_op_attrs(tbio
, REQ_OP_WRITE
, 0);
2107 bio_copy_data(tbio
, fbio
);
2109 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2110 atomic_inc(&r10_bio
->remaining
);
2111 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, bio_sectors(tbio
));
2113 if (test_bit(FailFast
, &conf
->mirrors
[d
].rdev
->flags
))
2114 tbio
->bi_opf
|= MD_FAILFAST
;
2115 tbio
->bi_iter
.bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
2116 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2117 generic_make_request(tbio
);
2120 /* Now write out to any replacement devices
2123 for (i
= 0; i
< conf
->copies
; i
++) {
2126 tbio
= r10_bio
->devs
[i
].repl_bio
;
2127 if (!tbio
|| !tbio
->bi_end_io
)
2129 if (r10_bio
->devs
[i
].bio
->bi_end_io
!= end_sync_write
2130 && r10_bio
->devs
[i
].bio
!= fbio
)
2131 bio_copy_data(tbio
, fbio
);
2132 d
= r10_bio
->devs
[i
].devnum
;
2133 atomic_inc(&r10_bio
->remaining
);
2134 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2136 generic_make_request(tbio
);
2140 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
2141 md_done_sync(mddev
, r10_bio
->sectors
, 1);
2147 * Now for the recovery code.
2148 * Recovery happens across physical sectors.
2149 * We recover all non-is_sync drives by finding the virtual address of
2150 * each, and then choose a working drive that also has that virt address.
2151 * There is a separate r10_bio for each non-in_sync drive.
2152 * Only the first two slots are in use. The first for reading,
2153 * The second for writing.
2156 static void fix_recovery_read_error(struct r10bio
*r10_bio
)
2158 /* We got a read error during recovery.
2159 * We repeat the read in smaller page-sized sections.
2160 * If a read succeeds, write it to the new device or record
2161 * a bad block if we cannot.
2162 * If a read fails, record a bad block on both old and
2165 struct mddev
*mddev
= r10_bio
->mddev
;
2166 struct r10conf
*conf
= mddev
->private;
2167 struct bio
*bio
= r10_bio
->devs
[0].bio
;
2169 int sectors
= r10_bio
->sectors
;
2171 int dr
= r10_bio
->devs
[0].devnum
;
2172 int dw
= r10_bio
->devs
[1].devnum
;
2176 struct md_rdev
*rdev
;
2180 if (s
> (PAGE_SIZE
>>9))
2183 rdev
= conf
->mirrors
[dr
].rdev
;
2184 addr
= r10_bio
->devs
[0].addr
+ sect
,
2185 ok
= sync_page_io(rdev
,
2188 bio
->bi_io_vec
[idx
].bv_page
,
2189 REQ_OP_READ
, 0, false);
2191 rdev
= conf
->mirrors
[dw
].rdev
;
2192 addr
= r10_bio
->devs
[1].addr
+ sect
;
2193 ok
= sync_page_io(rdev
,
2196 bio
->bi_io_vec
[idx
].bv_page
,
2197 REQ_OP_WRITE
, 0, false);
2199 set_bit(WriteErrorSeen
, &rdev
->flags
);
2200 if (!test_and_set_bit(WantReplacement
,
2202 set_bit(MD_RECOVERY_NEEDED
,
2203 &rdev
->mddev
->recovery
);
2207 /* We don't worry if we cannot set a bad block -
2208 * it really is bad so there is no loss in not
2211 rdev_set_badblocks(rdev
, addr
, s
, 0);
2213 if (rdev
!= conf
->mirrors
[dw
].rdev
) {
2214 /* need bad block on destination too */
2215 struct md_rdev
*rdev2
= conf
->mirrors
[dw
].rdev
;
2216 addr
= r10_bio
->devs
[1].addr
+ sect
;
2217 ok
= rdev_set_badblocks(rdev2
, addr
, s
, 0);
2219 /* just abort the recovery */
2220 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2223 conf
->mirrors
[dw
].recovery_disabled
2224 = mddev
->recovery_disabled
;
2225 set_bit(MD_RECOVERY_INTR
,
2238 static void recovery_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2240 struct r10conf
*conf
= mddev
->private;
2242 struct bio
*wbio
, *wbio2
;
2244 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
)) {
2245 fix_recovery_read_error(r10_bio
);
2246 end_sync_request(r10_bio
);
2251 * share the pages with the first bio
2252 * and submit the write request
2254 d
= r10_bio
->devs
[1].devnum
;
2255 wbio
= r10_bio
->devs
[1].bio
;
2256 wbio2
= r10_bio
->devs
[1].repl_bio
;
2257 /* Need to test wbio2->bi_end_io before we call
2258 * generic_make_request as if the former is NULL,
2259 * the latter is free to free wbio2.
2261 if (wbio2
&& !wbio2
->bi_end_io
)
2263 if (wbio
->bi_end_io
) {
2264 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2265 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, bio_sectors(wbio
));
2266 generic_make_request(wbio
);
2269 atomic_inc(&conf
->mirrors
[d
].replacement
->nr_pending
);
2270 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2271 bio_sectors(wbio2
));
2272 generic_make_request(wbio2
);
2277 * Used by fix_read_error() to decay the per rdev read_errors.
2278 * We halve the read error count for every hour that has elapsed
2279 * since the last recorded read error.
2282 static void check_decay_read_errors(struct mddev
*mddev
, struct md_rdev
*rdev
)
2285 unsigned long hours_since_last
;
2286 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
2288 cur_time_mon
= ktime_get_seconds();
2290 if (rdev
->last_read_error
== 0) {
2291 /* first time we've seen a read error */
2292 rdev
->last_read_error
= cur_time_mon
;
2296 hours_since_last
= (long)(cur_time_mon
-
2297 rdev
->last_read_error
) / 3600;
2299 rdev
->last_read_error
= cur_time_mon
;
2302 * if hours_since_last is > the number of bits in read_errors
2303 * just set read errors to 0. We do this to avoid
2304 * overflowing the shift of read_errors by hours_since_last.
2306 if (hours_since_last
>= 8 * sizeof(read_errors
))
2307 atomic_set(&rdev
->read_errors
, 0);
2309 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
2312 static int r10_sync_page_io(struct md_rdev
*rdev
, sector_t sector
,
2313 int sectors
, struct page
*page
, int rw
)
2318 if (is_badblock(rdev
, sector
, sectors
, &first_bad
, &bad_sectors
)
2319 && (rw
== READ
|| test_bit(WriteErrorSeen
, &rdev
->flags
)))
2321 if (sync_page_io(rdev
, sector
, sectors
<< 9, page
, rw
, 0, false))
2325 set_bit(WriteErrorSeen
, &rdev
->flags
);
2326 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2327 set_bit(MD_RECOVERY_NEEDED
,
2328 &rdev
->mddev
->recovery
);
2330 /* need to record an error - either for the block or the device */
2331 if (!rdev_set_badblocks(rdev
, sector
, sectors
, 0))
2332 md_error(rdev
->mddev
, rdev
);
2337 * This is a kernel thread which:
2339 * 1. Retries failed read operations on working mirrors.
2340 * 2. Updates the raid superblock when problems encounter.
2341 * 3. Performs writes following reads for array synchronising.
2344 static void fix_read_error(struct r10conf
*conf
, struct mddev
*mddev
, struct r10bio
*r10_bio
)
2346 int sect
= 0; /* Offset from r10_bio->sector */
2347 int sectors
= r10_bio
->sectors
;
2348 struct md_rdev
*rdev
;
2349 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
2350 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
2352 /* still own a reference to this rdev, so it cannot
2353 * have been cleared recently.
2355 rdev
= conf
->mirrors
[d
].rdev
;
2357 if (test_bit(Faulty
, &rdev
->flags
))
2358 /* drive has already been failed, just ignore any
2359 more fix_read_error() attempts */
2362 check_decay_read_errors(mddev
, rdev
);
2363 atomic_inc(&rdev
->read_errors
);
2364 if (atomic_read(&rdev
->read_errors
) > max_read_errors
) {
2365 char b
[BDEVNAME_SIZE
];
2366 bdevname(rdev
->bdev
, b
);
2368 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2370 atomic_read(&rdev
->read_errors
), max_read_errors
);
2371 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2373 md_error(mddev
, rdev
);
2374 r10_bio
->devs
[r10_bio
->read_slot
].bio
= IO_BLOCKED
;
2380 int sl
= r10_bio
->read_slot
;
2384 if (s
> (PAGE_SIZE
>>9))
2392 d
= r10_bio
->devs
[sl
].devnum
;
2393 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2395 test_bit(In_sync
, &rdev
->flags
) &&
2396 !test_bit(Faulty
, &rdev
->flags
) &&
2397 is_badblock(rdev
, r10_bio
->devs
[sl
].addr
+ sect
, s
,
2398 &first_bad
, &bad_sectors
) == 0) {
2399 atomic_inc(&rdev
->nr_pending
);
2401 success
= sync_page_io(rdev
,
2402 r10_bio
->devs
[sl
].addr
+
2406 REQ_OP_READ
, 0, false);
2407 rdev_dec_pending(rdev
, mddev
);
2413 if (sl
== conf
->copies
)
2415 } while (!success
&& sl
!= r10_bio
->read_slot
);
2419 /* Cannot read from anywhere, just mark the block
2420 * as bad on the first device to discourage future
2423 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
2424 rdev
= conf
->mirrors
[dn
].rdev
;
2426 if (!rdev_set_badblocks(
2428 r10_bio
->devs
[r10_bio
->read_slot
].addr
2431 md_error(mddev
, rdev
);
2432 r10_bio
->devs
[r10_bio
->read_slot
].bio
2439 /* write it back and re-read */
2441 while (sl
!= r10_bio
->read_slot
) {
2442 char b
[BDEVNAME_SIZE
];
2447 d
= r10_bio
->devs
[sl
].devnum
;
2448 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2450 test_bit(Faulty
, &rdev
->flags
) ||
2451 !test_bit(In_sync
, &rdev
->flags
))
2454 atomic_inc(&rdev
->nr_pending
);
2456 if (r10_sync_page_io(rdev
,
2457 r10_bio
->devs
[sl
].addr
+
2459 s
, conf
->tmppage
, WRITE
)
2461 /* Well, this device is dead */
2462 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2464 (unsigned long long)(
2466 choose_data_offset(r10_bio
,
2468 bdevname(rdev
->bdev
, b
));
2469 pr_notice("md/raid10:%s: %s: failing drive\n",
2471 bdevname(rdev
->bdev
, b
));
2473 rdev_dec_pending(rdev
, mddev
);
2477 while (sl
!= r10_bio
->read_slot
) {
2478 char b
[BDEVNAME_SIZE
];
2483 d
= r10_bio
->devs
[sl
].devnum
;
2484 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2486 test_bit(Faulty
, &rdev
->flags
) ||
2487 !test_bit(In_sync
, &rdev
->flags
))
2490 atomic_inc(&rdev
->nr_pending
);
2492 switch (r10_sync_page_io(rdev
,
2493 r10_bio
->devs
[sl
].addr
+
2498 /* Well, this device is dead */
2499 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2501 (unsigned long long)(
2503 choose_data_offset(r10_bio
, rdev
)),
2504 bdevname(rdev
->bdev
, b
));
2505 pr_notice("md/raid10:%s: %s: failing drive\n",
2507 bdevname(rdev
->bdev
, b
));
2510 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2512 (unsigned long long)(
2514 choose_data_offset(r10_bio
, rdev
)),
2515 bdevname(rdev
->bdev
, b
));
2516 atomic_add(s
, &rdev
->corrected_errors
);
2519 rdev_dec_pending(rdev
, mddev
);
2529 static int narrow_write_error(struct r10bio
*r10_bio
, int i
)
2531 struct bio
*bio
= r10_bio
->master_bio
;
2532 struct mddev
*mddev
= r10_bio
->mddev
;
2533 struct r10conf
*conf
= mddev
->private;
2534 struct md_rdev
*rdev
= conf
->mirrors
[r10_bio
->devs
[i
].devnum
].rdev
;
2535 /* bio has the data to be written to slot 'i' where
2536 * we just recently had a write error.
2537 * We repeatedly clone the bio and trim down to one block,
2538 * then try the write. Where the write fails we record
2540 * It is conceivable that the bio doesn't exactly align with
2541 * blocks. We must handle this.
2543 * We currently own a reference to the rdev.
2549 int sect_to_write
= r10_bio
->sectors
;
2552 if (rdev
->badblocks
.shift
< 0)
2555 block_sectors
= roundup(1 << rdev
->badblocks
.shift
,
2556 bdev_logical_block_size(rdev
->bdev
) >> 9);
2557 sector
= r10_bio
->sector
;
2558 sectors
= ((r10_bio
->sector
+ block_sectors
)
2559 & ~(sector_t
)(block_sectors
- 1))
2562 while (sect_to_write
) {
2565 if (sectors
> sect_to_write
)
2566 sectors
= sect_to_write
;
2567 /* Write at 'sector' for 'sectors' */
2568 wbio
= bio_clone_fast(bio
, GFP_NOIO
, mddev
->bio_set
);
2569 bio_trim(wbio
, sector
- bio
->bi_iter
.bi_sector
, sectors
);
2570 wsector
= r10_bio
->devs
[i
].addr
+ (sector
- r10_bio
->sector
);
2571 wbio
->bi_iter
.bi_sector
= wsector
+
2572 choose_data_offset(r10_bio
, rdev
);
2573 wbio
->bi_bdev
= rdev
->bdev
;
2574 bio_set_op_attrs(wbio
, REQ_OP_WRITE
, 0);
2576 if (submit_bio_wait(wbio
) < 0)
2578 ok
= rdev_set_badblocks(rdev
, wsector
,
2583 sect_to_write
-= sectors
;
2585 sectors
= block_sectors
;
2590 static void handle_read_error(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2592 int slot
= r10_bio
->read_slot
;
2594 struct r10conf
*conf
= mddev
->private;
2595 struct md_rdev
*rdev
= r10_bio
->devs
[slot
].rdev
;
2596 char b
[BDEVNAME_SIZE
];
2597 unsigned long do_sync
;
2600 sector_t bio_last_sector
;
2602 /* we got a read error. Maybe the drive is bad. Maybe just
2603 * the block and we can fix it.
2604 * We freeze all other IO, and try reading the block from
2605 * other devices. When we find one, we re-write
2606 * and check it that fixes the read error.
2607 * This is all done synchronously while the array is
2610 bio
= r10_bio
->devs
[slot
].bio
;
2611 bdevname(bio
->bi_bdev
, b
);
2612 bio_dev
= bio
->bi_bdev
->bd_dev
;
2613 bio_last_sector
= r10_bio
->devs
[slot
].addr
+ rdev
->data_offset
+ r10_bio
->sectors
;
2615 r10_bio
->devs
[slot
].bio
= NULL
;
2618 r10_bio
->devs
[slot
].bio
= IO_BLOCKED
;
2619 else if (!test_bit(FailFast
, &rdev
->flags
)) {
2620 freeze_array(conf
, 1);
2621 fix_read_error(conf
, mddev
, r10_bio
);
2622 unfreeze_array(conf
);
2624 md_error(mddev
, rdev
);
2626 rdev_dec_pending(rdev
, mddev
);
2629 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
2631 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
2633 (unsigned long long)r10_bio
->sector
);
2634 raid_end_bio_io(r10_bio
);
2638 do_sync
= (r10_bio
->master_bio
->bi_opf
& REQ_SYNC
);
2639 slot
= r10_bio
->read_slot
;
2640 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
2642 bdevname(rdev
->bdev
, b
),
2643 (unsigned long long)r10_bio
->sector
);
2644 bio
= bio_clone_fast(r10_bio
->master_bio
, GFP_NOIO
, mddev
->bio_set
);
2645 bio_trim(bio
, r10_bio
->sector
- bio
->bi_iter
.bi_sector
, max_sectors
);
2646 r10_bio
->devs
[slot
].bio
= bio
;
2647 r10_bio
->devs
[slot
].rdev
= rdev
;
2648 bio
->bi_iter
.bi_sector
= r10_bio
->devs
[slot
].addr
2649 + choose_data_offset(r10_bio
, rdev
);
2650 bio
->bi_bdev
= rdev
->bdev
;
2651 bio_set_op_attrs(bio
, REQ_OP_READ
, do_sync
);
2652 if (test_bit(FailFast
, &rdev
->flags
) &&
2653 test_bit(R10BIO_FailFast
, &r10_bio
->state
))
2654 bio
->bi_opf
|= MD_FAILFAST
;
2655 bio
->bi_private
= r10_bio
;
2656 bio
->bi_end_io
= raid10_end_read_request
;
2657 trace_block_bio_remap(bdev_get_queue(bio
->bi_bdev
),
2659 bio_last_sector
- r10_bio
->sectors
);
2661 if (max_sectors
< r10_bio
->sectors
) {
2662 /* Drat - have to split this up more */
2663 struct bio
*mbio
= r10_bio
->master_bio
;
2664 int sectors_handled
=
2665 r10_bio
->sector
+ max_sectors
2666 - mbio
->bi_iter
.bi_sector
;
2667 r10_bio
->sectors
= max_sectors
;
2668 spin_lock_irq(&conf
->device_lock
);
2669 if (mbio
->bi_phys_segments
== 0)
2670 mbio
->bi_phys_segments
= 2;
2672 mbio
->bi_phys_segments
++;
2673 spin_unlock_irq(&conf
->device_lock
);
2674 generic_make_request(bio
);
2676 r10_bio
= mempool_alloc(conf
->r10bio_pool
,
2678 r10_bio
->master_bio
= mbio
;
2679 r10_bio
->sectors
= bio_sectors(mbio
) - sectors_handled
;
2681 set_bit(R10BIO_ReadError
,
2683 r10_bio
->mddev
= mddev
;
2684 r10_bio
->sector
= mbio
->bi_iter
.bi_sector
2689 generic_make_request(bio
);
2692 static void handle_write_completed(struct r10conf
*conf
, struct r10bio
*r10_bio
)
2694 /* Some sort of write request has finished and it
2695 * succeeded in writing where we thought there was a
2696 * bad block. So forget the bad block.
2697 * Or possibly if failed and we need to record
2701 struct md_rdev
*rdev
;
2703 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
) ||
2704 test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
2705 for (m
= 0; m
< conf
->copies
; m
++) {
2706 int dev
= r10_bio
->devs
[m
].devnum
;
2707 rdev
= conf
->mirrors
[dev
].rdev
;
2708 if (r10_bio
->devs
[m
].bio
== NULL
)
2710 if (!r10_bio
->devs
[m
].bio
->bi_error
) {
2711 rdev_clear_badblocks(
2713 r10_bio
->devs
[m
].addr
,
2714 r10_bio
->sectors
, 0);
2716 if (!rdev_set_badblocks(
2718 r10_bio
->devs
[m
].addr
,
2719 r10_bio
->sectors
, 0))
2720 md_error(conf
->mddev
, rdev
);
2722 rdev
= conf
->mirrors
[dev
].replacement
;
2723 if (r10_bio
->devs
[m
].repl_bio
== NULL
)
2726 if (!r10_bio
->devs
[m
].repl_bio
->bi_error
) {
2727 rdev_clear_badblocks(
2729 r10_bio
->devs
[m
].addr
,
2730 r10_bio
->sectors
, 0);
2732 if (!rdev_set_badblocks(
2734 r10_bio
->devs
[m
].addr
,
2735 r10_bio
->sectors
, 0))
2736 md_error(conf
->mddev
, rdev
);
2742 for (m
= 0; m
< conf
->copies
; m
++) {
2743 int dev
= r10_bio
->devs
[m
].devnum
;
2744 struct bio
*bio
= r10_bio
->devs
[m
].bio
;
2745 rdev
= conf
->mirrors
[dev
].rdev
;
2746 if (bio
== IO_MADE_GOOD
) {
2747 rdev_clear_badblocks(
2749 r10_bio
->devs
[m
].addr
,
2750 r10_bio
->sectors
, 0);
2751 rdev_dec_pending(rdev
, conf
->mddev
);
2752 } else if (bio
!= NULL
&& bio
->bi_error
) {
2754 if (!narrow_write_error(r10_bio
, m
)) {
2755 md_error(conf
->mddev
, rdev
);
2756 set_bit(R10BIO_Degraded
,
2759 rdev_dec_pending(rdev
, conf
->mddev
);
2761 bio
= r10_bio
->devs
[m
].repl_bio
;
2762 rdev
= conf
->mirrors
[dev
].replacement
;
2763 if (rdev
&& bio
== IO_MADE_GOOD
) {
2764 rdev_clear_badblocks(
2766 r10_bio
->devs
[m
].addr
,
2767 r10_bio
->sectors
, 0);
2768 rdev_dec_pending(rdev
, conf
->mddev
);
2772 spin_lock_irq(&conf
->device_lock
);
2773 list_add(&r10_bio
->retry_list
, &conf
->bio_end_io_list
);
2775 spin_unlock_irq(&conf
->device_lock
);
2776 md_wakeup_thread(conf
->mddev
->thread
);
2778 if (test_bit(R10BIO_WriteError
,
2780 close_write(r10_bio
);
2781 raid_end_bio_io(r10_bio
);
2786 static void raid10d(struct md_thread
*thread
)
2788 struct mddev
*mddev
= thread
->mddev
;
2789 struct r10bio
*r10_bio
;
2790 unsigned long flags
;
2791 struct r10conf
*conf
= mddev
->private;
2792 struct list_head
*head
= &conf
->retry_list
;
2793 struct blk_plug plug
;
2795 md_check_recovery(mddev
);
2797 if (!list_empty_careful(&conf
->bio_end_io_list
) &&
2798 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
2800 spin_lock_irqsave(&conf
->device_lock
, flags
);
2801 if (!test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
2802 while (!list_empty(&conf
->bio_end_io_list
)) {
2803 list_move(conf
->bio_end_io_list
.prev
, &tmp
);
2807 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2808 while (!list_empty(&tmp
)) {
2809 r10_bio
= list_first_entry(&tmp
, struct r10bio
,
2811 list_del(&r10_bio
->retry_list
);
2812 if (mddev
->degraded
)
2813 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
2815 if (test_bit(R10BIO_WriteError
,
2817 close_write(r10_bio
);
2818 raid_end_bio_io(r10_bio
);
2822 blk_start_plug(&plug
);
2825 flush_pending_writes(conf
);
2827 spin_lock_irqsave(&conf
->device_lock
, flags
);
2828 if (list_empty(head
)) {
2829 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2832 r10_bio
= list_entry(head
->prev
, struct r10bio
, retry_list
);
2833 list_del(head
->prev
);
2835 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2837 mddev
= r10_bio
->mddev
;
2838 conf
= mddev
->private;
2839 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
2840 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
2841 handle_write_completed(conf
, r10_bio
);
2842 else if (test_bit(R10BIO_IsReshape
, &r10_bio
->state
))
2843 reshape_request_write(mddev
, r10_bio
);
2844 else if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
2845 sync_request_write(mddev
, r10_bio
);
2846 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
2847 recovery_request_write(mddev
, r10_bio
);
2848 else if (test_bit(R10BIO_ReadError
, &r10_bio
->state
))
2849 handle_read_error(mddev
, r10_bio
);
2851 /* just a partial read to be scheduled from a
2854 int slot
= r10_bio
->read_slot
;
2855 generic_make_request(r10_bio
->devs
[slot
].bio
);
2859 if (mddev
->sb_flags
& ~(1<<MD_SB_CHANGE_PENDING
))
2860 md_check_recovery(mddev
);
2862 blk_finish_plug(&plug
);
2865 static int init_resync(struct r10conf
*conf
)
2870 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
2871 BUG_ON(conf
->r10buf_pool
);
2872 conf
->have_replacement
= 0;
2873 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++)
2874 if (conf
->mirrors
[i
].replacement
)
2875 conf
->have_replacement
= 1;
2876 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
2877 if (!conf
->r10buf_pool
)
2879 conf
->next_resync
= 0;
2884 * perform a "sync" on one "block"
2886 * We need to make sure that no normal I/O request - particularly write
2887 * requests - conflict with active sync requests.
2889 * This is achieved by tracking pending requests and a 'barrier' concept
2890 * that can be installed to exclude normal IO requests.
2892 * Resync and recovery are handled very differently.
2893 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2895 * For resync, we iterate over virtual addresses, read all copies,
2896 * and update if there are differences. If only one copy is live,
2898 * For recovery, we iterate over physical addresses, read a good
2899 * value for each non-in_sync drive, and over-write.
2901 * So, for recovery we may have several outstanding complex requests for a
2902 * given address, one for each out-of-sync device. We model this by allocating
2903 * a number of r10_bio structures, one for each out-of-sync device.
2904 * As we setup these structures, we collect all bio's together into a list
2905 * which we then process collectively to add pages, and then process again
2906 * to pass to generic_make_request.
2908 * The r10_bio structures are linked using a borrowed master_bio pointer.
2909 * This link is counted in ->remaining. When the r10_bio that points to NULL
2910 * has its remaining count decremented to 0, the whole complex operation
2915 static sector_t
raid10_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
2918 struct r10conf
*conf
= mddev
->private;
2919 struct r10bio
*r10_bio
;
2920 struct bio
*biolist
= NULL
, *bio
;
2921 sector_t max_sector
, nr_sectors
;
2924 sector_t sync_blocks
;
2925 sector_t sectors_skipped
= 0;
2926 int chunks_skipped
= 0;
2927 sector_t chunk_mask
= conf
->geo
.chunk_mask
;
2929 if (!conf
->r10buf_pool
)
2930 if (init_resync(conf
))
2934 * Allow skipping a full rebuild for incremental assembly
2935 * of a clean array, like RAID1 does.
2937 if (mddev
->bitmap
== NULL
&&
2938 mddev
->recovery_cp
== MaxSector
&&
2939 mddev
->reshape_position
== MaxSector
&&
2940 !test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
2941 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
2942 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2943 conf
->fullsync
== 0) {
2945 return mddev
->dev_sectors
- sector_nr
;
2949 max_sector
= mddev
->dev_sectors
;
2950 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
2951 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2952 max_sector
= mddev
->resync_max_sectors
;
2953 if (sector_nr
>= max_sector
) {
2954 /* If we aborted, we need to abort the
2955 * sync on the 'current' bitmap chucks (there can
2956 * be several when recovering multiple devices).
2957 * as we may have started syncing it but not finished.
2958 * We can find the current address in
2959 * mddev->curr_resync, but for recovery,
2960 * we need to convert that to several
2961 * virtual addresses.
2963 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
2969 if (mddev
->curr_resync
< max_sector
) { /* aborted */
2970 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2971 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
2973 else for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2975 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
2976 bitmap_end_sync(mddev
->bitmap
, sect
,
2980 /* completed sync */
2981 if ((!mddev
->bitmap
|| conf
->fullsync
)
2982 && conf
->have_replacement
2983 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2984 /* Completed a full sync so the replacements
2985 * are now fully recovered.
2988 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2989 struct md_rdev
*rdev
=
2990 rcu_dereference(conf
->mirrors
[i
].replacement
);
2992 rdev
->recovery_offset
= MaxSector
;
2998 bitmap_close_sync(mddev
->bitmap
);
3001 return sectors_skipped
;
3004 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3005 return reshape_request(mddev
, sector_nr
, skipped
);
3007 if (chunks_skipped
>= conf
->geo
.raid_disks
) {
3008 /* if there has been nothing to do on any drive,
3009 * then there is nothing to do at all..
3012 return (max_sector
- sector_nr
) + sectors_skipped
;
3015 if (max_sector
> mddev
->resync_max
)
3016 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
3018 /* make sure whole request will fit in a chunk - if chunks
3021 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
&&
3022 max_sector
> (sector_nr
| chunk_mask
))
3023 max_sector
= (sector_nr
| chunk_mask
) + 1;
3026 * If there is non-resync activity waiting for a turn, then let it
3027 * though before starting on this new sync request.
3029 if (conf
->nr_waiting
)
3030 schedule_timeout_uninterruptible(1);
3032 /* Again, very different code for resync and recovery.
3033 * Both must result in an r10bio with a list of bios that
3034 * have bi_end_io, bi_sector, bi_bdev set,
3035 * and bi_private set to the r10bio.
3036 * For recovery, we may actually create several r10bios
3037 * with 2 bios in each, that correspond to the bios in the main one.
3038 * In this case, the subordinate r10bios link back through a
3039 * borrowed master_bio pointer, and the counter in the master
3040 * includes a ref from each subordinate.
3042 /* First, we decide what to do and set ->bi_end_io
3043 * To end_sync_read if we want to read, and
3044 * end_sync_write if we will want to write.
3047 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
3048 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3049 /* recovery... the complicated one */
3053 for (i
= 0 ; i
< conf
->geo
.raid_disks
; i
++) {
3059 struct raid10_info
*mirror
= &conf
->mirrors
[i
];
3060 struct md_rdev
*mrdev
, *mreplace
;
3063 mrdev
= rcu_dereference(mirror
->rdev
);
3064 mreplace
= rcu_dereference(mirror
->replacement
);
3066 if ((mrdev
== NULL
||
3067 test_bit(Faulty
, &mrdev
->flags
) ||
3068 test_bit(In_sync
, &mrdev
->flags
)) &&
3069 (mreplace
== NULL
||
3070 test_bit(Faulty
, &mreplace
->flags
))) {
3076 /* want to reconstruct this device */
3078 sect
= raid10_find_virt(conf
, sector_nr
, i
);
3079 if (sect
>= mddev
->resync_max_sectors
) {
3080 /* last stripe is not complete - don't
3081 * try to recover this sector.
3086 if (mreplace
&& test_bit(Faulty
, &mreplace
->flags
))
3088 /* Unless we are doing a full sync, or a replacement
3089 * we only need to recover the block if it is set in
3092 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
3094 if (sync_blocks
< max_sync
)
3095 max_sync
= sync_blocks
;
3099 /* yep, skip the sync_blocks here, but don't assume
3100 * that there will never be anything to do here
3102 chunks_skipped
= -1;
3106 atomic_inc(&mrdev
->nr_pending
);
3108 atomic_inc(&mreplace
->nr_pending
);
3111 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
3113 raise_barrier(conf
, rb2
!= NULL
);
3114 atomic_set(&r10_bio
->remaining
, 0);
3116 r10_bio
->master_bio
= (struct bio
*)rb2
;
3118 atomic_inc(&rb2
->remaining
);
3119 r10_bio
->mddev
= mddev
;
3120 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
3121 r10_bio
->sector
= sect
;
3123 raid10_find_phys(conf
, r10_bio
);
3125 /* Need to check if the array will still be
3129 for (j
= 0; j
< conf
->geo
.raid_disks
; j
++) {
3130 struct md_rdev
*rdev
= rcu_dereference(
3131 conf
->mirrors
[j
].rdev
);
3132 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3138 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
3139 &sync_blocks
, still_degraded
);
3142 for (j
=0; j
<conf
->copies
;j
++) {
3144 int d
= r10_bio
->devs
[j
].devnum
;
3145 sector_t from_addr
, to_addr
;
3146 struct md_rdev
*rdev
=
3147 rcu_dereference(conf
->mirrors
[d
].rdev
);
3148 sector_t sector
, first_bad
;
3151 !test_bit(In_sync
, &rdev
->flags
))
3153 /* This is where we read from */
3155 sector
= r10_bio
->devs
[j
].addr
;
3157 if (is_badblock(rdev
, sector
, max_sync
,
3158 &first_bad
, &bad_sectors
)) {
3159 if (first_bad
> sector
)
3160 max_sync
= first_bad
- sector
;
3162 bad_sectors
-= (sector
3164 if (max_sync
> bad_sectors
)
3165 max_sync
= bad_sectors
;
3169 bio
= r10_bio
->devs
[0].bio
;
3171 bio
->bi_next
= biolist
;
3173 bio
->bi_private
= r10_bio
;
3174 bio
->bi_end_io
= end_sync_read
;
3175 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
3176 if (test_bit(FailFast
, &rdev
->flags
))
3177 bio
->bi_opf
|= MD_FAILFAST
;
3178 from_addr
= r10_bio
->devs
[j
].addr
;
3179 bio
->bi_iter
.bi_sector
= from_addr
+
3181 bio
->bi_bdev
= rdev
->bdev
;
3182 atomic_inc(&rdev
->nr_pending
);
3183 /* and we write to 'i' (if not in_sync) */
3185 for (k
=0; k
<conf
->copies
; k
++)
3186 if (r10_bio
->devs
[k
].devnum
== i
)
3188 BUG_ON(k
== conf
->copies
);
3189 to_addr
= r10_bio
->devs
[k
].addr
;
3190 r10_bio
->devs
[0].devnum
= d
;
3191 r10_bio
->devs
[0].addr
= from_addr
;
3192 r10_bio
->devs
[1].devnum
= i
;
3193 r10_bio
->devs
[1].addr
= to_addr
;
3195 if (!test_bit(In_sync
, &mrdev
->flags
)) {
3196 bio
= r10_bio
->devs
[1].bio
;
3198 bio
->bi_next
= biolist
;
3200 bio
->bi_private
= r10_bio
;
3201 bio
->bi_end_io
= end_sync_write
;
3202 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3203 bio
->bi_iter
.bi_sector
= to_addr
3204 + mrdev
->data_offset
;
3205 bio
->bi_bdev
= mrdev
->bdev
;
3206 atomic_inc(&r10_bio
->remaining
);
3208 r10_bio
->devs
[1].bio
->bi_end_io
= NULL
;
3210 /* and maybe write to replacement */
3211 bio
= r10_bio
->devs
[1].repl_bio
;
3213 bio
->bi_end_io
= NULL
;
3214 /* Note: if mreplace != NULL, then bio
3215 * cannot be NULL as r10buf_pool_alloc will
3216 * have allocated it.
3217 * So the second test here is pointless.
3218 * But it keeps semantic-checkers happy, and
3219 * this comment keeps human reviewers
3222 if (mreplace
== NULL
|| bio
== NULL
||
3223 test_bit(Faulty
, &mreplace
->flags
))
3226 bio
->bi_next
= biolist
;
3228 bio
->bi_private
= r10_bio
;
3229 bio
->bi_end_io
= end_sync_write
;
3230 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3231 bio
->bi_iter
.bi_sector
= to_addr
+
3232 mreplace
->data_offset
;
3233 bio
->bi_bdev
= mreplace
->bdev
;
3234 atomic_inc(&r10_bio
->remaining
);
3238 if (j
== conf
->copies
) {
3239 /* Cannot recover, so abort the recovery or
3240 * record a bad block */
3242 /* problem is that there are bad blocks
3243 * on other device(s)
3246 for (k
= 0; k
< conf
->copies
; k
++)
3247 if (r10_bio
->devs
[k
].devnum
== i
)
3249 if (!test_bit(In_sync
,
3251 && !rdev_set_badblocks(
3253 r10_bio
->devs
[k
].addr
,
3257 !rdev_set_badblocks(
3259 r10_bio
->devs
[k
].addr
,
3264 if (!test_and_set_bit(MD_RECOVERY_INTR
,
3266 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3268 mirror
->recovery_disabled
3269 = mddev
->recovery_disabled
;
3273 atomic_dec(&rb2
->remaining
);
3275 rdev_dec_pending(mrdev
, mddev
);
3277 rdev_dec_pending(mreplace
, mddev
);
3280 rdev_dec_pending(mrdev
, mddev
);
3282 rdev_dec_pending(mreplace
, mddev
);
3283 if (r10_bio
->devs
[0].bio
->bi_opf
& MD_FAILFAST
) {
3284 /* Only want this if there is elsewhere to
3285 * read from. 'j' is currently the first
3289 for (; j
< conf
->copies
; j
++) {
3290 int d
= r10_bio
->devs
[j
].devnum
;
3291 if (conf
->mirrors
[d
].rdev
&&
3293 &conf
->mirrors
[d
].rdev
->flags
))
3297 r10_bio
->devs
[0].bio
->bi_opf
3301 if (biolist
== NULL
) {
3303 struct r10bio
*rb2
= r10_bio
;
3304 r10_bio
= (struct r10bio
*) rb2
->master_bio
;
3305 rb2
->master_bio
= NULL
;
3311 /* resync. Schedule a read for every block at this virt offset */
3314 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
, 0);
3316 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
3317 &sync_blocks
, mddev
->degraded
) &&
3318 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
3319 &mddev
->recovery
)) {
3320 /* We can skip this block */
3322 return sync_blocks
+ sectors_skipped
;
3324 if (sync_blocks
< max_sync
)
3325 max_sync
= sync_blocks
;
3326 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
3329 r10_bio
->mddev
= mddev
;
3330 atomic_set(&r10_bio
->remaining
, 0);
3331 raise_barrier(conf
, 0);
3332 conf
->next_resync
= sector_nr
;
3334 r10_bio
->master_bio
= NULL
;
3335 r10_bio
->sector
= sector_nr
;
3336 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
3337 raid10_find_phys(conf
, r10_bio
);
3338 r10_bio
->sectors
= (sector_nr
| chunk_mask
) - sector_nr
+ 1;
3340 for (i
= 0; i
< conf
->copies
; i
++) {
3341 int d
= r10_bio
->devs
[i
].devnum
;
3342 sector_t first_bad
, sector
;
3344 struct md_rdev
*rdev
;
3346 if (r10_bio
->devs
[i
].repl_bio
)
3347 r10_bio
->devs
[i
].repl_bio
->bi_end_io
= NULL
;
3349 bio
= r10_bio
->devs
[i
].bio
;
3351 bio
->bi_error
= -EIO
;
3353 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
3354 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3358 sector
= r10_bio
->devs
[i
].addr
;
3359 if (is_badblock(rdev
, sector
, max_sync
,
3360 &first_bad
, &bad_sectors
)) {
3361 if (first_bad
> sector
)
3362 max_sync
= first_bad
- sector
;
3364 bad_sectors
-= (sector
- first_bad
);
3365 if (max_sync
> bad_sectors
)
3366 max_sync
= bad_sectors
;
3371 atomic_inc(&rdev
->nr_pending
);
3372 atomic_inc(&r10_bio
->remaining
);
3373 bio
->bi_next
= biolist
;
3375 bio
->bi_private
= r10_bio
;
3376 bio
->bi_end_io
= end_sync_read
;
3377 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
3378 if (test_bit(FailFast
, &conf
->mirrors
[d
].rdev
->flags
))
3379 bio
->bi_opf
|= MD_FAILFAST
;
3380 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
3381 bio
->bi_bdev
= rdev
->bdev
;
3384 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
3385 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3389 atomic_inc(&rdev
->nr_pending
);
3392 /* Need to set up for writing to the replacement */
3393 bio
= r10_bio
->devs
[i
].repl_bio
;
3395 bio
->bi_error
= -EIO
;
3397 sector
= r10_bio
->devs
[i
].addr
;
3398 bio
->bi_next
= biolist
;
3400 bio
->bi_private
= r10_bio
;
3401 bio
->bi_end_io
= end_sync_write
;
3402 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3403 if (test_bit(FailFast
, &conf
->mirrors
[d
].rdev
->flags
))
3404 bio
->bi_opf
|= MD_FAILFAST
;
3405 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
3406 bio
->bi_bdev
= rdev
->bdev
;
3411 for (i
=0; i
<conf
->copies
; i
++) {
3412 int d
= r10_bio
->devs
[i
].devnum
;
3413 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
3414 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
3416 if (r10_bio
->devs
[i
].repl_bio
&&
3417 r10_bio
->devs
[i
].repl_bio
->bi_end_io
)
3419 conf
->mirrors
[d
].replacement
,
3429 if (sector_nr
+ max_sync
< max_sector
)
3430 max_sector
= sector_nr
+ max_sync
;
3433 int len
= PAGE_SIZE
;
3434 if (sector_nr
+ (len
>>9) > max_sector
)
3435 len
= (max_sector
- sector_nr
) << 9;
3438 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
3440 page
= bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
;
3441 if (bio_add_page(bio
, page
, len
, 0))
3445 bio
->bi_io_vec
[bio
->bi_vcnt
].bv_page
= page
;
3446 for (bio2
= biolist
;
3447 bio2
&& bio2
!= bio
;
3448 bio2
= bio2
->bi_next
) {
3449 /* remove last page from this bio */
3451 bio2
->bi_iter
.bi_size
-= len
;
3452 bio_clear_flag(bio2
, BIO_SEG_VALID
);
3456 nr_sectors
+= len
>>9;
3457 sector_nr
+= len
>>9;
3458 } while (biolist
->bi_vcnt
< RESYNC_PAGES
);
3460 r10_bio
->sectors
= nr_sectors
;
3464 biolist
= biolist
->bi_next
;
3466 bio
->bi_next
= NULL
;
3467 r10_bio
= bio
->bi_private
;
3468 r10_bio
->sectors
= nr_sectors
;
3470 if (bio
->bi_end_io
== end_sync_read
) {
3471 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
3473 generic_make_request(bio
);
3477 if (sectors_skipped
)
3478 /* pretend they weren't skipped, it makes
3479 * no important difference in this case
3481 md_done_sync(mddev
, sectors_skipped
, 1);
3483 return sectors_skipped
+ nr_sectors
;
3485 /* There is nowhere to write, so all non-sync
3486 * drives must be failed or in resync, all drives
3487 * have a bad block, so try the next chunk...
3489 if (sector_nr
+ max_sync
< max_sector
)
3490 max_sector
= sector_nr
+ max_sync
;
3492 sectors_skipped
+= (max_sector
- sector_nr
);
3494 sector_nr
= max_sector
;
3499 raid10_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
3502 struct r10conf
*conf
= mddev
->private;
3505 raid_disks
= min(conf
->geo
.raid_disks
,
3506 conf
->prev
.raid_disks
);
3508 sectors
= conf
->dev_sectors
;
3510 size
= sectors
>> conf
->geo
.chunk_shift
;
3511 sector_div(size
, conf
->geo
.far_copies
);
3512 size
= size
* raid_disks
;
3513 sector_div(size
, conf
->geo
.near_copies
);
3515 return size
<< conf
->geo
.chunk_shift
;
3518 static void calc_sectors(struct r10conf
*conf
, sector_t size
)
3520 /* Calculate the number of sectors-per-device that will
3521 * actually be used, and set conf->dev_sectors and
3525 size
= size
>> conf
->geo
.chunk_shift
;
3526 sector_div(size
, conf
->geo
.far_copies
);
3527 size
= size
* conf
->geo
.raid_disks
;
3528 sector_div(size
, conf
->geo
.near_copies
);
3529 /* 'size' is now the number of chunks in the array */
3530 /* calculate "used chunks per device" */
3531 size
= size
* conf
->copies
;
3533 /* We need to round up when dividing by raid_disks to
3534 * get the stride size.
3536 size
= DIV_ROUND_UP_SECTOR_T(size
, conf
->geo
.raid_disks
);
3538 conf
->dev_sectors
= size
<< conf
->geo
.chunk_shift
;
3540 if (conf
->geo
.far_offset
)
3541 conf
->geo
.stride
= 1 << conf
->geo
.chunk_shift
;
3543 sector_div(size
, conf
->geo
.far_copies
);
3544 conf
->geo
.stride
= size
<< conf
->geo
.chunk_shift
;
3548 enum geo_type
{geo_new
, geo_old
, geo_start
};
3549 static int setup_geo(struct geom
*geo
, struct mddev
*mddev
, enum geo_type
new)
3552 int layout
, chunk
, disks
;
3555 layout
= mddev
->layout
;
3556 chunk
= mddev
->chunk_sectors
;
3557 disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3560 layout
= mddev
->new_layout
;
3561 chunk
= mddev
->new_chunk_sectors
;
3562 disks
= mddev
->raid_disks
;
3564 default: /* avoid 'may be unused' warnings */
3565 case geo_start
: /* new when starting reshape - raid_disks not
3567 layout
= mddev
->new_layout
;
3568 chunk
= mddev
->new_chunk_sectors
;
3569 disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
3574 if (chunk
< (PAGE_SIZE
>> 9) ||
3575 !is_power_of_2(chunk
))
3578 fc
= (layout
>> 8) & 255;
3579 fo
= layout
& (1<<16);
3580 geo
->raid_disks
= disks
;
3581 geo
->near_copies
= nc
;
3582 geo
->far_copies
= fc
;
3583 geo
->far_offset
= fo
;
3584 switch (layout
>> 17) {
3585 case 0: /* original layout. simple but not always optimal */
3586 geo
->far_set_size
= disks
;
3588 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3589 * actually using this, but leave code here just in case.*/
3590 geo
->far_set_size
= disks
/fc
;
3591 WARN(geo
->far_set_size
< fc
,
3592 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3594 case 2: /* "improved" layout fixed to match documentation */
3595 geo
->far_set_size
= fc
* nc
;
3597 default: /* Not a valid layout */
3600 geo
->chunk_mask
= chunk
- 1;
3601 geo
->chunk_shift
= ffz(~chunk
);
3605 static struct r10conf
*setup_conf(struct mddev
*mddev
)
3607 struct r10conf
*conf
= NULL
;
3612 copies
= setup_geo(&geo
, mddev
, geo_new
);
3615 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3616 mdname(mddev
), PAGE_SIZE
);
3620 if (copies
< 2 || copies
> mddev
->raid_disks
) {
3621 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3622 mdname(mddev
), mddev
->new_layout
);
3627 conf
= kzalloc(sizeof(struct r10conf
), GFP_KERNEL
);
3631 /* FIXME calc properly */
3632 conf
->mirrors
= kzalloc(sizeof(struct raid10_info
)*(mddev
->raid_disks
+
3633 max(0,-mddev
->delta_disks
)),
3638 conf
->tmppage
= alloc_page(GFP_KERNEL
);
3643 conf
->copies
= copies
;
3644 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
3645 r10bio_pool_free
, conf
);
3646 if (!conf
->r10bio_pool
)
3649 calc_sectors(conf
, mddev
->dev_sectors
);
3650 if (mddev
->reshape_position
== MaxSector
) {
3651 conf
->prev
= conf
->geo
;
3652 conf
->reshape_progress
= MaxSector
;
3654 if (setup_geo(&conf
->prev
, mddev
, geo_old
) != conf
->copies
) {
3658 conf
->reshape_progress
= mddev
->reshape_position
;
3659 if (conf
->prev
.far_offset
)
3660 conf
->prev
.stride
= 1 << conf
->prev
.chunk_shift
;
3662 /* far_copies must be 1 */
3663 conf
->prev
.stride
= conf
->dev_sectors
;
3665 conf
->reshape_safe
= conf
->reshape_progress
;
3666 spin_lock_init(&conf
->device_lock
);
3667 INIT_LIST_HEAD(&conf
->retry_list
);
3668 INIT_LIST_HEAD(&conf
->bio_end_io_list
);
3670 spin_lock_init(&conf
->resync_lock
);
3671 init_waitqueue_head(&conf
->wait_barrier
);
3672 atomic_set(&conf
->nr_pending
, 0);
3674 conf
->thread
= md_register_thread(raid10d
, mddev
, "raid10");
3678 conf
->mddev
= mddev
;
3683 mempool_destroy(conf
->r10bio_pool
);
3684 kfree(conf
->mirrors
);
3685 safe_put_page(conf
->tmppage
);
3688 return ERR_PTR(err
);
3691 static int raid10_run(struct mddev
*mddev
)
3693 struct r10conf
*conf
;
3694 int i
, disk_idx
, chunk_size
;
3695 struct raid10_info
*disk
;
3696 struct md_rdev
*rdev
;
3698 sector_t min_offset_diff
= 0;
3700 bool discard_supported
= false;
3702 if (mddev
->private == NULL
) {
3703 conf
= setup_conf(mddev
);
3705 return PTR_ERR(conf
);
3706 mddev
->private = conf
;
3708 conf
= mddev
->private;
3712 mddev
->thread
= conf
->thread
;
3713 conf
->thread
= NULL
;
3715 chunk_size
= mddev
->chunk_sectors
<< 9;
3717 blk_queue_max_discard_sectors(mddev
->queue
,
3718 mddev
->chunk_sectors
);
3719 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
3720 blk_queue_io_min(mddev
->queue
, chunk_size
);
3721 if (conf
->geo
.raid_disks
% conf
->geo
.near_copies
)
3722 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->geo
.raid_disks
);
3724 blk_queue_io_opt(mddev
->queue
, chunk_size
*
3725 (conf
->geo
.raid_disks
/ conf
->geo
.near_copies
));
3728 rdev_for_each(rdev
, mddev
) {
3730 struct request_queue
*q
;
3732 disk_idx
= rdev
->raid_disk
;
3735 if (disk_idx
>= conf
->geo
.raid_disks
&&
3736 disk_idx
>= conf
->prev
.raid_disks
)
3738 disk
= conf
->mirrors
+ disk_idx
;
3740 if (test_bit(Replacement
, &rdev
->flags
)) {
3741 if (disk
->replacement
)
3743 disk
->replacement
= rdev
;
3749 q
= bdev_get_queue(rdev
->bdev
);
3750 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
3751 if (!mddev
->reshape_backwards
)
3755 if (first
|| diff
< min_offset_diff
)
3756 min_offset_diff
= diff
;
3759 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
3760 rdev
->data_offset
<< 9);
3762 disk
->head_position
= 0;
3764 if (blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
3765 discard_supported
= true;
3769 if (discard_supported
)
3770 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
3773 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
3776 /* need to check that every block has at least one working mirror */
3777 if (!enough(conf
, -1)) {
3778 pr_err("md/raid10:%s: not enough operational mirrors.\n",
3783 if (conf
->reshape_progress
!= MaxSector
) {
3784 /* must ensure that shape change is supported */
3785 if (conf
->geo
.far_copies
!= 1 &&
3786 conf
->geo
.far_offset
== 0)
3788 if (conf
->prev
.far_copies
!= 1 &&
3789 conf
->prev
.far_offset
== 0)
3793 mddev
->degraded
= 0;
3795 i
< conf
->geo
.raid_disks
3796 || i
< conf
->prev
.raid_disks
;
3799 disk
= conf
->mirrors
+ i
;
3801 if (!disk
->rdev
&& disk
->replacement
) {
3802 /* The replacement is all we have - use it */
3803 disk
->rdev
= disk
->replacement
;
3804 disk
->replacement
= NULL
;
3805 clear_bit(Replacement
, &disk
->rdev
->flags
);
3809 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
3810 disk
->head_position
= 0;
3813 disk
->rdev
->saved_raid_disk
< 0)
3816 disk
->recovery_disabled
= mddev
->recovery_disabled
- 1;
3819 if (mddev
->recovery_cp
!= MaxSector
)
3820 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3822 pr_info("md/raid10:%s: active with %d out of %d devices\n",
3823 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
,
3824 conf
->geo
.raid_disks
);
3826 * Ok, everything is just fine now
3828 mddev
->dev_sectors
= conf
->dev_sectors
;
3829 size
= raid10_size(mddev
, 0, 0);
3830 md_set_array_sectors(mddev
, size
);
3831 mddev
->resync_max_sectors
= size
;
3832 set_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
);
3835 int stripe
= conf
->geo
.raid_disks
*
3836 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
3838 /* Calculate max read-ahead size.
3839 * We need to readahead at least twice a whole stripe....
3842 stripe
/= conf
->geo
.near_copies
;
3843 if (mddev
->queue
->backing_dev_info
->ra_pages
< 2 * stripe
)
3844 mddev
->queue
->backing_dev_info
->ra_pages
= 2 * stripe
;
3847 if (md_integrity_register(mddev
))
3850 if (conf
->reshape_progress
!= MaxSector
) {
3851 unsigned long before_length
, after_length
;
3853 before_length
= ((1 << conf
->prev
.chunk_shift
) *
3854 conf
->prev
.far_copies
);
3855 after_length
= ((1 << conf
->geo
.chunk_shift
) *
3856 conf
->geo
.far_copies
);
3858 if (max(before_length
, after_length
) > min_offset_diff
) {
3859 /* This cannot work */
3860 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3863 conf
->offset_diff
= min_offset_diff
;
3865 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3866 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3867 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3868 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3869 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
3876 md_unregister_thread(&mddev
->thread
);
3877 mempool_destroy(conf
->r10bio_pool
);
3878 safe_put_page(conf
->tmppage
);
3879 kfree(conf
->mirrors
);
3881 mddev
->private = NULL
;
3886 static void raid10_free(struct mddev
*mddev
, void *priv
)
3888 struct r10conf
*conf
= priv
;
3890 mempool_destroy(conf
->r10bio_pool
);
3891 safe_put_page(conf
->tmppage
);
3892 kfree(conf
->mirrors
);
3893 kfree(conf
->mirrors_old
);
3894 kfree(conf
->mirrors_new
);
3898 static void raid10_quiesce(struct mddev
*mddev
, int state
)
3900 struct r10conf
*conf
= mddev
->private;
3904 raise_barrier(conf
, 0);
3907 lower_barrier(conf
);
3912 static int raid10_resize(struct mddev
*mddev
, sector_t sectors
)
3914 /* Resize of 'far' arrays is not supported.
3915 * For 'near' and 'offset' arrays we can set the
3916 * number of sectors used to be an appropriate multiple
3917 * of the chunk size.
3918 * For 'offset', this is far_copies*chunksize.
3919 * For 'near' the multiplier is the LCM of
3920 * near_copies and raid_disks.
3921 * So if far_copies > 1 && !far_offset, fail.
3922 * Else find LCM(raid_disks, near_copy)*far_copies and
3923 * multiply by chunk_size. Then round to this number.
3924 * This is mostly done by raid10_size()
3926 struct r10conf
*conf
= mddev
->private;
3927 sector_t oldsize
, size
;
3929 if (mddev
->reshape_position
!= MaxSector
)
3932 if (conf
->geo
.far_copies
> 1 && !conf
->geo
.far_offset
)
3935 oldsize
= raid10_size(mddev
, 0, 0);
3936 size
= raid10_size(mddev
, sectors
, 0);
3937 if (mddev
->external_size
&&
3938 mddev
->array_sectors
> size
)
3940 if (mddev
->bitmap
) {
3941 int ret
= bitmap_resize(mddev
->bitmap
, size
, 0, 0);
3945 md_set_array_sectors(mddev
, size
);
3947 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
3948 revalidate_disk(mddev
->gendisk
);
3950 if (sectors
> mddev
->dev_sectors
&&
3951 mddev
->recovery_cp
> oldsize
) {
3952 mddev
->recovery_cp
= oldsize
;
3953 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3955 calc_sectors(conf
, sectors
);
3956 mddev
->dev_sectors
= conf
->dev_sectors
;
3957 mddev
->resync_max_sectors
= size
;
3961 static void *raid10_takeover_raid0(struct mddev
*mddev
, sector_t size
, int devs
)
3963 struct md_rdev
*rdev
;
3964 struct r10conf
*conf
;
3966 if (mddev
->degraded
> 0) {
3967 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3969 return ERR_PTR(-EINVAL
);
3971 sector_div(size
, devs
);
3973 /* Set new parameters */
3974 mddev
->new_level
= 10;
3975 /* new layout: far_copies = 1, near_copies = 2 */
3976 mddev
->new_layout
= (1<<8) + 2;
3977 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3978 mddev
->delta_disks
= mddev
->raid_disks
;
3979 mddev
->raid_disks
*= 2;
3980 /* make sure it will be not marked as dirty */
3981 mddev
->recovery_cp
= MaxSector
;
3982 mddev
->dev_sectors
= size
;
3984 conf
= setup_conf(mddev
);
3985 if (!IS_ERR(conf
)) {
3986 rdev_for_each(rdev
, mddev
)
3987 if (rdev
->raid_disk
>= 0) {
3988 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
3989 rdev
->sectors
= size
;
3997 static void *raid10_takeover(struct mddev
*mddev
)
3999 struct r0conf
*raid0_conf
;
4001 /* raid10 can take over:
4002 * raid0 - providing it has only two drives
4004 if (mddev
->level
== 0) {
4005 /* for raid0 takeover only one zone is supported */
4006 raid0_conf
= mddev
->private;
4007 if (raid0_conf
->nr_strip_zones
> 1) {
4008 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4010 return ERR_PTR(-EINVAL
);
4012 return raid10_takeover_raid0(mddev
,
4013 raid0_conf
->strip_zone
->zone_end
,
4014 raid0_conf
->strip_zone
->nb_dev
);
4016 return ERR_PTR(-EINVAL
);
4019 static int raid10_check_reshape(struct mddev
*mddev
)
4021 /* Called when there is a request to change
4022 * - layout (to ->new_layout)
4023 * - chunk size (to ->new_chunk_sectors)
4024 * - raid_disks (by delta_disks)
4025 * or when trying to restart a reshape that was ongoing.
4027 * We need to validate the request and possibly allocate
4028 * space if that might be an issue later.
4030 * Currently we reject any reshape of a 'far' mode array,
4031 * allow chunk size to change if new is generally acceptable,
4032 * allow raid_disks to increase, and allow
4033 * a switch between 'near' mode and 'offset' mode.
4035 struct r10conf
*conf
= mddev
->private;
4038 if (conf
->geo
.far_copies
!= 1 && !conf
->geo
.far_offset
)
4041 if (setup_geo(&geo
, mddev
, geo_start
) != conf
->copies
)
4042 /* mustn't change number of copies */
4044 if (geo
.far_copies
> 1 && !geo
.far_offset
)
4045 /* Cannot switch to 'far' mode */
4048 if (mddev
->array_sectors
& geo
.chunk_mask
)
4049 /* not factor of array size */
4052 if (!enough(conf
, -1))
4055 kfree(conf
->mirrors_new
);
4056 conf
->mirrors_new
= NULL
;
4057 if (mddev
->delta_disks
> 0) {
4058 /* allocate new 'mirrors' list */
4059 conf
->mirrors_new
= kzalloc(
4060 sizeof(struct raid10_info
)
4061 *(mddev
->raid_disks
+
4062 mddev
->delta_disks
),
4064 if (!conf
->mirrors_new
)
4071 * Need to check if array has failed when deciding whether to:
4073 * - remove non-faulty devices
4076 * This determination is simple when no reshape is happening.
4077 * However if there is a reshape, we need to carefully check
4078 * both the before and after sections.
4079 * This is because some failed devices may only affect one
4080 * of the two sections, and some non-in_sync devices may
4081 * be insync in the section most affected by failed devices.
4083 static int calc_degraded(struct r10conf
*conf
)
4085 int degraded
, degraded2
;
4090 /* 'prev' section first */
4091 for (i
= 0; i
< conf
->prev
.raid_disks
; i
++) {
4092 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
4093 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4095 else if (!test_bit(In_sync
, &rdev
->flags
))
4096 /* When we can reduce the number of devices in
4097 * an array, this might not contribute to
4098 * 'degraded'. It does now.
4103 if (conf
->geo
.raid_disks
== conf
->prev
.raid_disks
)
4107 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
4108 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
4109 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4111 else if (!test_bit(In_sync
, &rdev
->flags
)) {
4112 /* If reshape is increasing the number of devices,
4113 * this section has already been recovered, so
4114 * it doesn't contribute to degraded.
4117 if (conf
->geo
.raid_disks
<= conf
->prev
.raid_disks
)
4122 if (degraded2
> degraded
)
4127 static int raid10_start_reshape(struct mddev
*mddev
)
4129 /* A 'reshape' has been requested. This commits
4130 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4131 * This also checks if there are enough spares and adds them
4133 * We currently require enough spares to make the final
4134 * array non-degraded. We also require that the difference
4135 * between old and new data_offset - on each device - is
4136 * enough that we never risk over-writing.
4139 unsigned long before_length
, after_length
;
4140 sector_t min_offset_diff
= 0;
4143 struct r10conf
*conf
= mddev
->private;
4144 struct md_rdev
*rdev
;
4148 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4151 if (setup_geo(&new, mddev
, geo_start
) != conf
->copies
)
4154 before_length
= ((1 << conf
->prev
.chunk_shift
) *
4155 conf
->prev
.far_copies
);
4156 after_length
= ((1 << conf
->geo
.chunk_shift
) *
4157 conf
->geo
.far_copies
);
4159 rdev_for_each(rdev
, mddev
) {
4160 if (!test_bit(In_sync
, &rdev
->flags
)
4161 && !test_bit(Faulty
, &rdev
->flags
))
4163 if (rdev
->raid_disk
>= 0) {
4164 long long diff
= (rdev
->new_data_offset
4165 - rdev
->data_offset
);
4166 if (!mddev
->reshape_backwards
)
4170 if (first
|| diff
< min_offset_diff
)
4171 min_offset_diff
= diff
;
4175 if (max(before_length
, after_length
) > min_offset_diff
)
4178 if (spares
< mddev
->delta_disks
)
4181 conf
->offset_diff
= min_offset_diff
;
4182 spin_lock_irq(&conf
->device_lock
);
4183 if (conf
->mirrors_new
) {
4184 memcpy(conf
->mirrors_new
, conf
->mirrors
,
4185 sizeof(struct raid10_info
)*conf
->prev
.raid_disks
);
4187 kfree(conf
->mirrors_old
);
4188 conf
->mirrors_old
= conf
->mirrors
;
4189 conf
->mirrors
= conf
->mirrors_new
;
4190 conf
->mirrors_new
= NULL
;
4192 setup_geo(&conf
->geo
, mddev
, geo_start
);
4194 if (mddev
->reshape_backwards
) {
4195 sector_t size
= raid10_size(mddev
, 0, 0);
4196 if (size
< mddev
->array_sectors
) {
4197 spin_unlock_irq(&conf
->device_lock
);
4198 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4202 mddev
->resync_max_sectors
= size
;
4203 conf
->reshape_progress
= size
;
4205 conf
->reshape_progress
= 0;
4206 conf
->reshape_safe
= conf
->reshape_progress
;
4207 spin_unlock_irq(&conf
->device_lock
);
4209 if (mddev
->delta_disks
&& mddev
->bitmap
) {
4210 ret
= bitmap_resize(mddev
->bitmap
,
4211 raid10_size(mddev
, 0,
4212 conf
->geo
.raid_disks
),
4217 if (mddev
->delta_disks
> 0) {
4218 rdev_for_each(rdev
, mddev
)
4219 if (rdev
->raid_disk
< 0 &&
4220 !test_bit(Faulty
, &rdev
->flags
)) {
4221 if (raid10_add_disk(mddev
, rdev
) == 0) {
4222 if (rdev
->raid_disk
>=
4223 conf
->prev
.raid_disks
)
4224 set_bit(In_sync
, &rdev
->flags
);
4226 rdev
->recovery_offset
= 0;
4228 if (sysfs_link_rdev(mddev
, rdev
))
4229 /* Failure here is OK */;
4231 } else if (rdev
->raid_disk
>= conf
->prev
.raid_disks
4232 && !test_bit(Faulty
, &rdev
->flags
)) {
4233 /* This is a spare that was manually added */
4234 set_bit(In_sync
, &rdev
->flags
);
4237 /* When a reshape changes the number of devices,
4238 * ->degraded is measured against the larger of the
4239 * pre and post numbers.
4241 spin_lock_irq(&conf
->device_lock
);
4242 mddev
->degraded
= calc_degraded(conf
);
4243 spin_unlock_irq(&conf
->device_lock
);
4244 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4245 mddev
->reshape_position
= conf
->reshape_progress
;
4246 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4248 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4249 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4250 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
4251 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
4252 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4254 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
4256 if (!mddev
->sync_thread
) {
4260 conf
->reshape_checkpoint
= jiffies
;
4261 md_wakeup_thread(mddev
->sync_thread
);
4262 md_new_event(mddev
);
4266 mddev
->recovery
= 0;
4267 spin_lock_irq(&conf
->device_lock
);
4268 conf
->geo
= conf
->prev
;
4269 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4270 rdev_for_each(rdev
, mddev
)
4271 rdev
->new_data_offset
= rdev
->data_offset
;
4273 conf
->reshape_progress
= MaxSector
;
4274 conf
->reshape_safe
= MaxSector
;
4275 mddev
->reshape_position
= MaxSector
;
4276 spin_unlock_irq(&conf
->device_lock
);
4280 /* Calculate the last device-address that could contain
4281 * any block from the chunk that includes the array-address 's'
4282 * and report the next address.
4283 * i.e. the address returned will be chunk-aligned and after
4284 * any data that is in the chunk containing 's'.
4286 static sector_t
last_dev_address(sector_t s
, struct geom
*geo
)
4288 s
= (s
| geo
->chunk_mask
) + 1;
4289 s
>>= geo
->chunk_shift
;
4290 s
*= geo
->near_copies
;
4291 s
= DIV_ROUND_UP_SECTOR_T(s
, geo
->raid_disks
);
4292 s
*= geo
->far_copies
;
4293 s
<<= geo
->chunk_shift
;
4297 /* Calculate the first device-address that could contain
4298 * any block from the chunk that includes the array-address 's'.
4299 * This too will be the start of a chunk
4301 static sector_t
first_dev_address(sector_t s
, struct geom
*geo
)
4303 s
>>= geo
->chunk_shift
;
4304 s
*= geo
->near_copies
;
4305 sector_div(s
, geo
->raid_disks
);
4306 s
*= geo
->far_copies
;
4307 s
<<= geo
->chunk_shift
;
4311 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
4314 /* We simply copy at most one chunk (smallest of old and new)
4315 * at a time, possibly less if that exceeds RESYNC_PAGES,
4316 * or we hit a bad block or something.
4317 * This might mean we pause for normal IO in the middle of
4318 * a chunk, but that is not a problem as mddev->reshape_position
4319 * can record any location.
4321 * If we will want to write to a location that isn't
4322 * yet recorded as 'safe' (i.e. in metadata on disk) then
4323 * we need to flush all reshape requests and update the metadata.
4325 * When reshaping forwards (e.g. to more devices), we interpret
4326 * 'safe' as the earliest block which might not have been copied
4327 * down yet. We divide this by previous stripe size and multiply
4328 * by previous stripe length to get lowest device offset that we
4329 * cannot write to yet.
4330 * We interpret 'sector_nr' as an address that we want to write to.
4331 * From this we use last_device_address() to find where we might
4332 * write to, and first_device_address on the 'safe' position.
4333 * If this 'next' write position is after the 'safe' position,
4334 * we must update the metadata to increase the 'safe' position.
4336 * When reshaping backwards, we round in the opposite direction
4337 * and perform the reverse test: next write position must not be
4338 * less than current safe position.
4340 * In all this the minimum difference in data offsets
4341 * (conf->offset_diff - always positive) allows a bit of slack,
4342 * so next can be after 'safe', but not by more than offset_diff
4344 * We need to prepare all the bios here before we start any IO
4345 * to ensure the size we choose is acceptable to all devices.
4346 * The means one for each copy for write-out and an extra one for
4348 * We store the read-in bio in ->master_bio and the others in
4349 * ->devs[x].bio and ->devs[x].repl_bio.
4351 struct r10conf
*conf
= mddev
->private;
4352 struct r10bio
*r10_bio
;
4353 sector_t next
, safe
, last
;
4357 struct md_rdev
*rdev
;
4360 struct bio
*bio
, *read_bio
;
4361 int sectors_done
= 0;
4363 if (sector_nr
== 0) {
4364 /* If restarting in the middle, skip the initial sectors */
4365 if (mddev
->reshape_backwards
&&
4366 conf
->reshape_progress
< raid10_size(mddev
, 0, 0)) {
4367 sector_nr
= (raid10_size(mddev
, 0, 0)
4368 - conf
->reshape_progress
);
4369 } else if (!mddev
->reshape_backwards
&&
4370 conf
->reshape_progress
> 0)
4371 sector_nr
= conf
->reshape_progress
;
4373 mddev
->curr_resync_completed
= sector_nr
;
4374 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4380 /* We don't use sector_nr to track where we are up to
4381 * as that doesn't work well for ->reshape_backwards.
4382 * So just use ->reshape_progress.
4384 if (mddev
->reshape_backwards
) {
4385 /* 'next' is the earliest device address that we might
4386 * write to for this chunk in the new layout
4388 next
= first_dev_address(conf
->reshape_progress
- 1,
4391 /* 'safe' is the last device address that we might read from
4392 * in the old layout after a restart
4394 safe
= last_dev_address(conf
->reshape_safe
- 1,
4397 if (next
+ conf
->offset_diff
< safe
)
4400 last
= conf
->reshape_progress
- 1;
4401 sector_nr
= last
& ~(sector_t
)(conf
->geo
.chunk_mask
4402 & conf
->prev
.chunk_mask
);
4403 if (sector_nr
+ RESYNC_BLOCK_SIZE
/512 < last
)
4404 sector_nr
= last
+ 1 - RESYNC_BLOCK_SIZE
/512;
4406 /* 'next' is after the last device address that we
4407 * might write to for this chunk in the new layout
4409 next
= last_dev_address(conf
->reshape_progress
, &conf
->geo
);
4411 /* 'safe' is the earliest device address that we might
4412 * read from in the old layout after a restart
4414 safe
= first_dev_address(conf
->reshape_safe
, &conf
->prev
);
4416 /* Need to update metadata if 'next' might be beyond 'safe'
4417 * as that would possibly corrupt data
4419 if (next
> safe
+ conf
->offset_diff
)
4422 sector_nr
= conf
->reshape_progress
;
4423 last
= sector_nr
| (conf
->geo
.chunk_mask
4424 & conf
->prev
.chunk_mask
);
4426 if (sector_nr
+ RESYNC_BLOCK_SIZE
/512 <= last
)
4427 last
= sector_nr
+ RESYNC_BLOCK_SIZE
/512 - 1;
4431 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4432 /* Need to update reshape_position in metadata */
4434 mddev
->reshape_position
= conf
->reshape_progress
;
4435 if (mddev
->reshape_backwards
)
4436 mddev
->curr_resync_completed
= raid10_size(mddev
, 0, 0)
4437 - conf
->reshape_progress
;
4439 mddev
->curr_resync_completed
= conf
->reshape_progress
;
4440 conf
->reshape_checkpoint
= jiffies
;
4441 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4442 md_wakeup_thread(mddev
->thread
);
4443 wait_event(mddev
->sb_wait
, mddev
->sb_flags
== 0 ||
4444 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
4445 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4446 allow_barrier(conf
);
4447 return sectors_done
;
4449 conf
->reshape_safe
= mddev
->reshape_position
;
4450 allow_barrier(conf
);
4454 /* Now schedule reads for blocks from sector_nr to last */
4455 r10_bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
4457 raise_barrier(conf
, sectors_done
!= 0);
4458 atomic_set(&r10_bio
->remaining
, 0);
4459 r10_bio
->mddev
= mddev
;
4460 r10_bio
->sector
= sector_nr
;
4461 set_bit(R10BIO_IsReshape
, &r10_bio
->state
);
4462 r10_bio
->sectors
= last
- sector_nr
+ 1;
4463 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
4464 BUG_ON(!test_bit(R10BIO_Previous
, &r10_bio
->state
));
4467 /* Cannot read from here, so need to record bad blocks
4468 * on all the target devices.
4471 mempool_free(r10_bio
, conf
->r10buf_pool
);
4472 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4473 return sectors_done
;
4476 read_bio
= bio_alloc_mddev(GFP_KERNEL
, RESYNC_PAGES
, mddev
);
4478 read_bio
->bi_bdev
= rdev
->bdev
;
4479 read_bio
->bi_iter
.bi_sector
= (r10_bio
->devs
[r10_bio
->read_slot
].addr
4480 + rdev
->data_offset
);
4481 read_bio
->bi_private
= r10_bio
;
4482 read_bio
->bi_end_io
= end_sync_read
;
4483 bio_set_op_attrs(read_bio
, REQ_OP_READ
, 0);
4484 read_bio
->bi_flags
&= (~0UL << BIO_RESET_BITS
);
4485 read_bio
->bi_error
= 0;
4486 read_bio
->bi_vcnt
= 0;
4487 read_bio
->bi_iter
.bi_size
= 0;
4488 r10_bio
->master_bio
= read_bio
;
4489 r10_bio
->read_slot
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
4491 /* Now find the locations in the new layout */
4492 __raid10_find_phys(&conf
->geo
, r10_bio
);
4495 read_bio
->bi_next
= NULL
;
4498 for (s
= 0; s
< conf
->copies
*2; s
++) {
4500 int d
= r10_bio
->devs
[s
/2].devnum
;
4501 struct md_rdev
*rdev2
;
4503 rdev2
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4504 b
= r10_bio
->devs
[s
/2].repl_bio
;
4506 rdev2
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4507 b
= r10_bio
->devs
[s
/2].bio
;
4509 if (!rdev2
|| test_bit(Faulty
, &rdev2
->flags
))
4513 b
->bi_bdev
= rdev2
->bdev
;
4514 b
->bi_iter
.bi_sector
= r10_bio
->devs
[s
/2].addr
+
4515 rdev2
->new_data_offset
;
4516 b
->bi_private
= r10_bio
;
4517 b
->bi_end_io
= end_reshape_write
;
4518 bio_set_op_attrs(b
, REQ_OP_WRITE
, 0);
4523 /* Now add as many pages as possible to all of these bios. */
4526 for (s
= 0 ; s
< max_sectors
; s
+= PAGE_SIZE
>> 9) {
4527 struct page
*page
= r10_bio
->devs
[0].bio
->bi_io_vec
[s
/(PAGE_SIZE
>>9)].bv_page
;
4528 int len
= (max_sectors
- s
) << 9;
4529 if (len
> PAGE_SIZE
)
4531 for (bio
= blist
; bio
; bio
= bio
->bi_next
) {
4533 if (bio_add_page(bio
, page
, len
, 0))
4536 /* Didn't fit, must stop */
4538 bio2
&& bio2
!= bio
;
4539 bio2
= bio2
->bi_next
) {
4540 /* Remove last page from this bio */
4542 bio2
->bi_iter
.bi_size
-= len
;
4543 bio_clear_flag(bio2
, BIO_SEG_VALID
);
4547 sector_nr
+= len
>> 9;
4548 nr_sectors
+= len
>> 9;
4552 r10_bio
->sectors
= nr_sectors
;
4554 /* Now submit the read */
4555 md_sync_acct(read_bio
->bi_bdev
, r10_bio
->sectors
);
4556 atomic_inc(&r10_bio
->remaining
);
4557 read_bio
->bi_next
= NULL
;
4558 generic_make_request(read_bio
);
4559 sector_nr
+= nr_sectors
;
4560 sectors_done
+= nr_sectors
;
4561 if (sector_nr
<= last
)
4564 /* Now that we have done the whole section we can
4565 * update reshape_progress
4567 if (mddev
->reshape_backwards
)
4568 conf
->reshape_progress
-= sectors_done
;
4570 conf
->reshape_progress
+= sectors_done
;
4572 return sectors_done
;
4575 static void end_reshape_request(struct r10bio
*r10_bio
);
4576 static int handle_reshape_read_error(struct mddev
*mddev
,
4577 struct r10bio
*r10_bio
);
4578 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
4580 /* Reshape read completed. Hopefully we have a block
4582 * If we got a read error then we do sync 1-page reads from
4583 * elsewhere until we find the data - or give up.
4585 struct r10conf
*conf
= mddev
->private;
4588 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
4589 if (handle_reshape_read_error(mddev
, r10_bio
) < 0) {
4590 /* Reshape has been aborted */
4591 md_done_sync(mddev
, r10_bio
->sectors
, 0);
4595 /* We definitely have the data in the pages, schedule the
4598 atomic_set(&r10_bio
->remaining
, 1);
4599 for (s
= 0; s
< conf
->copies
*2; s
++) {
4601 int d
= r10_bio
->devs
[s
/2].devnum
;
4602 struct md_rdev
*rdev
;
4605 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4606 b
= r10_bio
->devs
[s
/2].repl_bio
;
4608 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4609 b
= r10_bio
->devs
[s
/2].bio
;
4611 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
)) {
4615 atomic_inc(&rdev
->nr_pending
);
4617 md_sync_acct(b
->bi_bdev
, r10_bio
->sectors
);
4618 atomic_inc(&r10_bio
->remaining
);
4620 generic_make_request(b
);
4622 end_reshape_request(r10_bio
);
4625 static void end_reshape(struct r10conf
*conf
)
4627 if (test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
))
4630 spin_lock_irq(&conf
->device_lock
);
4631 conf
->prev
= conf
->geo
;
4632 md_finish_reshape(conf
->mddev
);
4634 conf
->reshape_progress
= MaxSector
;
4635 conf
->reshape_safe
= MaxSector
;
4636 spin_unlock_irq(&conf
->device_lock
);
4638 /* read-ahead size must cover two whole stripes, which is
4639 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4641 if (conf
->mddev
->queue
) {
4642 int stripe
= conf
->geo
.raid_disks
*
4643 ((conf
->mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
4644 stripe
/= conf
->geo
.near_copies
;
4645 if (conf
->mddev
->queue
->backing_dev_info
->ra_pages
< 2 * stripe
)
4646 conf
->mddev
->queue
->backing_dev_info
->ra_pages
= 2 * stripe
;
4651 static int handle_reshape_read_error(struct mddev
*mddev
,
4652 struct r10bio
*r10_bio
)
4654 /* Use sync reads to get the blocks from somewhere else */
4655 int sectors
= r10_bio
->sectors
;
4656 struct r10conf
*conf
= mddev
->private;
4658 struct r10bio r10_bio
;
4659 struct r10dev devs
[conf
->copies
];
4661 struct r10bio
*r10b
= &on_stack
.r10_bio
;
4664 struct bio_vec
*bvec
= r10_bio
->master_bio
->bi_io_vec
;
4666 r10b
->sector
= r10_bio
->sector
;
4667 __raid10_find_phys(&conf
->prev
, r10b
);
4672 int first_slot
= slot
;
4674 if (s
> (PAGE_SIZE
>> 9))
4679 int d
= r10b
->devs
[slot
].devnum
;
4680 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4683 test_bit(Faulty
, &rdev
->flags
) ||
4684 !test_bit(In_sync
, &rdev
->flags
))
4687 addr
= r10b
->devs
[slot
].addr
+ idx
* PAGE_SIZE
;
4688 atomic_inc(&rdev
->nr_pending
);
4690 success
= sync_page_io(rdev
,
4694 REQ_OP_READ
, 0, false);
4695 rdev_dec_pending(rdev
, mddev
);
4701 if (slot
>= conf
->copies
)
4703 if (slot
== first_slot
)
4708 /* couldn't read this block, must give up */
4709 set_bit(MD_RECOVERY_INTR
,
4719 static void end_reshape_write(struct bio
*bio
)
4721 struct r10bio
*r10_bio
= bio
->bi_private
;
4722 struct mddev
*mddev
= r10_bio
->mddev
;
4723 struct r10conf
*conf
= mddev
->private;
4727 struct md_rdev
*rdev
= NULL
;
4729 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
4731 rdev
= conf
->mirrors
[d
].replacement
;
4734 rdev
= conf
->mirrors
[d
].rdev
;
4737 if (bio
->bi_error
) {
4738 /* FIXME should record badblock */
4739 md_error(mddev
, rdev
);
4742 rdev_dec_pending(rdev
, mddev
);
4743 end_reshape_request(r10_bio
);
4746 static void end_reshape_request(struct r10bio
*r10_bio
)
4748 if (!atomic_dec_and_test(&r10_bio
->remaining
))
4750 md_done_sync(r10_bio
->mddev
, r10_bio
->sectors
, 1);
4751 bio_put(r10_bio
->master_bio
);
4755 static void raid10_finish_reshape(struct mddev
*mddev
)
4757 struct r10conf
*conf
= mddev
->private;
4759 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
4762 if (mddev
->delta_disks
> 0) {
4763 sector_t size
= raid10_size(mddev
, 0, 0);
4764 md_set_array_sectors(mddev
, size
);
4765 if (mddev
->recovery_cp
> mddev
->resync_max_sectors
) {
4766 mddev
->recovery_cp
= mddev
->resync_max_sectors
;
4767 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4769 mddev
->resync_max_sectors
= size
;
4771 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4772 revalidate_disk(mddev
->gendisk
);
4777 for (d
= conf
->geo
.raid_disks
;
4778 d
< conf
->geo
.raid_disks
- mddev
->delta_disks
;
4780 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4782 clear_bit(In_sync
, &rdev
->flags
);
4783 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4785 clear_bit(In_sync
, &rdev
->flags
);
4789 mddev
->layout
= mddev
->new_layout
;
4790 mddev
->chunk_sectors
= 1 << conf
->geo
.chunk_shift
;
4791 mddev
->reshape_position
= MaxSector
;
4792 mddev
->delta_disks
= 0;
4793 mddev
->reshape_backwards
= 0;
4796 static struct md_personality raid10_personality
=
4800 .owner
= THIS_MODULE
,
4801 .make_request
= raid10_make_request
,
4803 .free
= raid10_free
,
4804 .status
= raid10_status
,
4805 .error_handler
= raid10_error
,
4806 .hot_add_disk
= raid10_add_disk
,
4807 .hot_remove_disk
= raid10_remove_disk
,
4808 .spare_active
= raid10_spare_active
,
4809 .sync_request
= raid10_sync_request
,
4810 .quiesce
= raid10_quiesce
,
4811 .size
= raid10_size
,
4812 .resize
= raid10_resize
,
4813 .takeover
= raid10_takeover
,
4814 .check_reshape
= raid10_check_reshape
,
4815 .start_reshape
= raid10_start_reshape
,
4816 .finish_reshape
= raid10_finish_reshape
,
4817 .congested
= raid10_congested
,
4820 static int __init
raid_init(void)
4822 return register_md_personality(&raid10_personality
);
4825 static void raid_exit(void)
4827 unregister_md_personality(&raid10_personality
);
4830 module_init(raid_init
);
4831 module_exit(raid_exit
);
4832 MODULE_LICENSE("GPL");
4833 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4834 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4835 MODULE_ALIAS("md-raid10");
4836 MODULE_ALIAS("md-level-10");
4838 module_param(max_queued_requests
, int, S_IRUGO
|S_IWUSR
);