1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * raid10.c : Multiple Devices driver for Linux
5 * Copyright (C) 2000-2004 Neil Brown
7 * RAID-10 support for md.
9 * Base on code in raid1.c. See raid1.c for further copyright information.
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/blkdev.h>
15 #include <linux/module.h>
16 #include <linux/seq_file.h>
17 #include <linux/ratelimit.h>
18 #include <linux/kthread.h>
19 #include <linux/raid/md_p.h>
20 #include <trace/events/block.h>
23 #define RAID_1_10_NAME "raid10"
26 #include "md-bitmap.h"
29 * RAID10 provides a combination of RAID0 and RAID1 functionality.
30 * The layout of data is defined by
33 * near_copies (stored in low byte of layout)
34 * far_copies (stored in second byte of layout)
35 * far_offset (stored in bit 16 of layout )
36 * use_far_sets (stored in bit 17 of layout )
37 * use_far_sets_bugfixed (stored in bit 18 of layout )
39 * The data to be stored is divided into chunks using chunksize. Each device
40 * is divided into far_copies sections. In each section, chunks are laid out
41 * in a style similar to raid0, but near_copies copies of each chunk is stored
42 * (each on a different drive). The starting device for each section is offset
43 * near_copies from the starting device of the previous section. Thus there
44 * are (near_copies * far_copies) of each chunk, and each is on a different
45 * drive. near_copies and far_copies must be at least one, and their product
46 * is at most raid_disks.
48 * If far_offset is true, then the far_copies are handled a bit differently.
49 * The copies are still in different stripes, but instead of being very far
50 * apart on disk, there are adjacent stripes.
52 * The far and offset algorithms are handled slightly differently if
53 * 'use_far_sets' is true. In this case, the array's devices are grouped into
54 * sets that are (near_copies * far_copies) in size. The far copied stripes
55 * are still shifted by 'near_copies' devices, but this shifting stays confined
56 * to the set rather than the entire array. This is done to improve the number
57 * of device combinations that can fail without causing the array to fail.
58 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
63 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
64 * [A B] [C D] [A B] [C D E]
65 * |...| |...| |...| | ... |
66 * [B A] [D C] [B A] [E C D]
69 static void allow_barrier(struct r10conf
*conf
);
70 static void lower_barrier(struct r10conf
*conf
);
71 static int _enough(struct r10conf
*conf
, int previous
, int ignore
);
72 static int enough(struct r10conf
*conf
, int ignore
);
73 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
75 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
);
76 static void end_reshape_write(struct bio
*bio
);
77 static void end_reshape(struct r10conf
*conf
);
82 #define cmd_before(conf, cmd) \
84 write_sequnlock_irq(&(conf)->resync_lock); \
87 #define cmd_after(conf) write_seqlock_irq(&(conf)->resync_lock)
89 #define wait_event_barrier_cmd(conf, cond, cmd) \
90 wait_event_cmd((conf)->wait_barrier, cond, cmd_before(conf, cmd), \
93 #define wait_event_barrier(conf, cond) \
94 wait_event_barrier_cmd(conf, cond, NULL_CMD)
97 * for resync bio, r10bio pointer can be retrieved from the per-bio
98 * 'struct resync_pages'.
100 static inline struct r10bio
*get_resync_r10bio(struct bio
*bio
)
102 return get_resync_pages(bio
)->raid_bio
;
105 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
107 struct r10conf
*conf
= data
;
108 int size
= offsetof(struct r10bio
, devs
[conf
->geo
.raid_disks
]);
110 /* allocate a r10bio with room for raid_disks entries in the
112 return kzalloc(size
, gfp_flags
);
115 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
116 /* amount of memory to reserve for resync requests */
117 #define RESYNC_WINDOW (1024*1024)
118 /* maximum number of concurrent requests, memory permitting */
119 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
120 #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
121 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
124 * When performing a resync, we need to read and compare, so
125 * we need as many pages are there are copies.
126 * When performing a recovery, we need 2 bios, one for read,
127 * one for write (we recover only one drive per r10buf)
130 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
132 struct r10conf
*conf
= data
;
133 struct r10bio
*r10_bio
;
136 int nalloc
, nalloc_rp
;
137 struct resync_pages
*rps
;
139 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
143 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
) ||
144 test_bit(MD_RECOVERY_RESHAPE
, &conf
->mddev
->recovery
))
145 nalloc
= conf
->copies
; /* resync */
147 nalloc
= 2; /* recovery */
149 /* allocate once for all bios */
150 if (!conf
->have_replacement
)
153 nalloc_rp
= nalloc
* 2;
154 rps
= kmalloc_array(nalloc_rp
, sizeof(struct resync_pages
), gfp_flags
);
156 goto out_free_r10bio
;
161 for (j
= nalloc
; j
-- ; ) {
162 bio
= bio_kmalloc(RESYNC_PAGES
, gfp_flags
);
165 bio_init(bio
, NULL
, bio
->bi_inline_vecs
, RESYNC_PAGES
, 0);
166 r10_bio
->devs
[j
].bio
= bio
;
167 if (!conf
->have_replacement
)
169 bio
= bio_kmalloc(RESYNC_PAGES
, gfp_flags
);
172 bio_init(bio
, NULL
, bio
->bi_inline_vecs
, RESYNC_PAGES
, 0);
173 r10_bio
->devs
[j
].repl_bio
= bio
;
176 * Allocate RESYNC_PAGES data pages and attach them
179 for (j
= 0; j
< nalloc
; j
++) {
180 struct bio
*rbio
= r10_bio
->devs
[j
].repl_bio
;
181 struct resync_pages
*rp
, *rp_repl
;
185 rp_repl
= &rps
[nalloc
+ j
];
187 bio
= r10_bio
->devs
[j
].bio
;
189 if (!j
|| test_bit(MD_RECOVERY_SYNC
,
190 &conf
->mddev
->recovery
)) {
191 if (resync_alloc_pages(rp
, gfp_flags
))
194 memcpy(rp
, &rps
[0], sizeof(*rp
));
195 resync_get_all_pages(rp
);
198 rp
->raid_bio
= r10_bio
;
199 bio
->bi_private
= rp
;
201 memcpy(rp_repl
, rp
, sizeof(*rp
));
202 rbio
->bi_private
= rp_repl
;
210 resync_free_pages(&rps
[j
]);
214 for ( ; j
< nalloc
; j
++) {
215 if (r10_bio
->devs
[j
].bio
)
216 bio_uninit(r10_bio
->devs
[j
].bio
);
217 kfree(r10_bio
->devs
[j
].bio
);
218 if (r10_bio
->devs
[j
].repl_bio
)
219 bio_uninit(r10_bio
->devs
[j
].repl_bio
);
220 kfree(r10_bio
->devs
[j
].repl_bio
);
224 rbio_pool_free(r10_bio
, conf
);
228 static void r10buf_pool_free(void *__r10_bio
, void *data
)
230 struct r10conf
*conf
= data
;
231 struct r10bio
*r10bio
= __r10_bio
;
233 struct resync_pages
*rp
= NULL
;
235 for (j
= conf
->copies
; j
--; ) {
236 struct bio
*bio
= r10bio
->devs
[j
].bio
;
239 rp
= get_resync_pages(bio
);
240 resync_free_pages(rp
);
245 bio
= r10bio
->devs
[j
].repl_bio
;
252 /* resync pages array stored in the 1st bio's .bi_private */
255 rbio_pool_free(r10bio
, conf
);
258 static void put_all_bios(struct r10conf
*conf
, struct r10bio
*r10_bio
)
262 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
263 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
264 if (!BIO_SPECIAL(*bio
))
267 bio
= &r10_bio
->devs
[i
].repl_bio
;
268 if (r10_bio
->read_slot
< 0 && !BIO_SPECIAL(*bio
))
274 static void free_r10bio(struct r10bio
*r10_bio
)
276 struct r10conf
*conf
= r10_bio
->mddev
->private;
278 put_all_bios(conf
, r10_bio
);
279 mempool_free(r10_bio
, &conf
->r10bio_pool
);
282 static void put_buf(struct r10bio
*r10_bio
)
284 struct r10conf
*conf
= r10_bio
->mddev
->private;
286 mempool_free(r10_bio
, &conf
->r10buf_pool
);
291 static void wake_up_barrier(struct r10conf
*conf
)
293 if (wq_has_sleeper(&conf
->wait_barrier
))
294 wake_up(&conf
->wait_barrier
);
297 static void reschedule_retry(struct r10bio
*r10_bio
)
300 struct mddev
*mddev
= r10_bio
->mddev
;
301 struct r10conf
*conf
= mddev
->private;
303 spin_lock_irqsave(&conf
->device_lock
, flags
);
304 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
306 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
308 /* wake up frozen array... */
309 wake_up(&conf
->wait_barrier
);
311 md_wakeup_thread(mddev
->thread
);
315 * raid_end_bio_io() is called when we have finished servicing a mirrored
316 * operation and are ready to return a success/failure code to the buffer
319 static void raid_end_bio_io(struct r10bio
*r10_bio
)
321 struct bio
*bio
= r10_bio
->master_bio
;
322 struct r10conf
*conf
= r10_bio
->mddev
->private;
324 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
325 bio
->bi_status
= BLK_STS_IOERR
;
329 * Wake up any possible resync thread that waits for the device
334 free_r10bio(r10_bio
);
338 * Update disk head position estimator based on IRQ completion info.
340 static inline void update_head_pos(int slot
, struct r10bio
*r10_bio
)
342 struct r10conf
*conf
= r10_bio
->mddev
->private;
344 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
345 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
349 * Find the disk number which triggered given bio
351 static int find_bio_disk(struct r10conf
*conf
, struct r10bio
*r10_bio
,
352 struct bio
*bio
, int *slotp
, int *replp
)
357 for (slot
= 0; slot
< conf
->geo
.raid_disks
; slot
++) {
358 if (r10_bio
->devs
[slot
].bio
== bio
)
360 if (r10_bio
->devs
[slot
].repl_bio
== bio
) {
366 update_head_pos(slot
, r10_bio
);
372 return r10_bio
->devs
[slot
].devnum
;
375 static void raid10_end_read_request(struct bio
*bio
)
377 int uptodate
= !bio
->bi_status
;
378 struct r10bio
*r10_bio
= bio
->bi_private
;
380 struct md_rdev
*rdev
;
381 struct r10conf
*conf
= r10_bio
->mddev
->private;
383 slot
= r10_bio
->read_slot
;
384 rdev
= r10_bio
->devs
[slot
].rdev
;
386 * this branch is our 'one mirror IO has finished' event handler:
388 update_head_pos(slot
, r10_bio
);
392 * Set R10BIO_Uptodate in our master bio, so that
393 * we will return a good error code to the higher
394 * levels even if IO on some other mirrored buffer fails.
396 * The 'master' represents the composite IO operation to
397 * user-side. So if something waits for IO, then it will
398 * wait for the 'master' bio.
400 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
402 /* If all other devices that store this block have
403 * failed, we want to return the error upwards rather
404 * than fail the last device. Here we redefine
405 * "uptodate" to mean "Don't want to retry"
407 if (!_enough(conf
, test_bit(R10BIO_Previous
, &r10_bio
->state
),
412 raid_end_bio_io(r10_bio
);
413 rdev_dec_pending(rdev
, conf
->mddev
);
416 * oops, read error - keep the refcount on the rdev
418 pr_err_ratelimited("md/raid10:%s: %pg: rescheduling sector %llu\n",
421 (unsigned long long)r10_bio
->sector
);
422 set_bit(R10BIO_ReadError
, &r10_bio
->state
);
423 reschedule_retry(r10_bio
);
427 static void close_write(struct r10bio
*r10_bio
)
429 struct mddev
*mddev
= r10_bio
->mddev
;
431 /* clear the bitmap if all writes complete successfully */
432 mddev
->bitmap_ops
->endwrite(mddev
, r10_bio
->sector
, r10_bio
->sectors
,
433 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
438 static void one_write_done(struct r10bio
*r10_bio
)
440 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
441 if (test_bit(R10BIO_WriteError
, &r10_bio
->state
))
442 reschedule_retry(r10_bio
);
444 close_write(r10_bio
);
445 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
446 reschedule_retry(r10_bio
);
448 raid_end_bio_io(r10_bio
);
453 static void raid10_end_write_request(struct bio
*bio
)
455 struct r10bio
*r10_bio
= bio
->bi_private
;
458 struct r10conf
*conf
= r10_bio
->mddev
->private;
460 struct md_rdev
*rdev
= NULL
;
461 struct bio
*to_put
= NULL
;
464 discard_error
= bio
->bi_status
&& bio_op(bio
) == REQ_OP_DISCARD
;
466 dev
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
469 rdev
= conf
->mirrors
[dev
].replacement
;
473 rdev
= conf
->mirrors
[dev
].rdev
;
476 * this branch is our 'one mirror IO has finished' event handler:
478 if (bio
->bi_status
&& !discard_error
) {
480 /* Never record new bad blocks to replacement,
483 md_error(rdev
->mddev
, rdev
);
485 set_bit(WriteErrorSeen
, &rdev
->flags
);
486 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
487 set_bit(MD_RECOVERY_NEEDED
,
488 &rdev
->mddev
->recovery
);
491 if (test_bit(FailFast
, &rdev
->flags
) &&
492 (bio
->bi_opf
& MD_FAILFAST
)) {
493 md_error(rdev
->mddev
, rdev
);
497 * When the device is faulty, it is not necessary to
498 * handle write error.
500 if (!test_bit(Faulty
, &rdev
->flags
))
501 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
503 /* Fail the request */
504 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
505 r10_bio
->devs
[slot
].bio
= NULL
;
512 * Set R10BIO_Uptodate in our master bio, so that
513 * we will return a good error code for to the higher
514 * levels even if IO on some other mirrored buffer fails.
516 * The 'master' represents the composite IO operation to
517 * user-side. So if something waits for IO, then it will
518 * wait for the 'master' bio.
520 * Do not set R10BIO_Uptodate if the current device is
521 * rebuilding or Faulty. This is because we cannot use
522 * such device for properly reading the data back (we could
523 * potentially use it, if the current write would have felt
524 * before rdev->recovery_offset, but for simplicity we don't
527 if (test_bit(In_sync
, &rdev
->flags
) &&
528 !test_bit(Faulty
, &rdev
->flags
))
529 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
531 /* Maybe we can clear some bad blocks. */
532 if (rdev_has_badblock(rdev
, r10_bio
->devs
[slot
].addr
,
537 r10_bio
->devs
[slot
].repl_bio
= IO_MADE_GOOD
;
539 r10_bio
->devs
[slot
].bio
= IO_MADE_GOOD
;
541 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
547 * Let's see if all mirrored write operations have finished
550 one_write_done(r10_bio
);
552 rdev_dec_pending(rdev
, conf
->mddev
);
558 * RAID10 layout manager
559 * As well as the chunksize and raid_disks count, there are two
560 * parameters: near_copies and far_copies.
561 * near_copies * far_copies must be <= raid_disks.
562 * Normally one of these will be 1.
563 * If both are 1, we get raid0.
564 * If near_copies == raid_disks, we get raid1.
566 * Chunks are laid out in raid0 style with near_copies copies of the
567 * first chunk, followed by near_copies copies of the next chunk and
569 * If far_copies > 1, then after 1/far_copies of the array has been assigned
570 * as described above, we start again with a device offset of near_copies.
571 * So we effectively have another copy of the whole array further down all
572 * the drives, but with blocks on different drives.
573 * With this layout, and block is never stored twice on the one device.
575 * raid10_find_phys finds the sector offset of a given virtual sector
576 * on each device that it is on.
578 * raid10_find_virt does the reverse mapping, from a device and a
579 * sector offset to a virtual address
582 static void __raid10_find_phys(struct geom
*geo
, struct r10bio
*r10bio
)
590 int last_far_set_start
, last_far_set_size
;
592 last_far_set_start
= (geo
->raid_disks
/ geo
->far_set_size
) - 1;
593 last_far_set_start
*= geo
->far_set_size
;
595 last_far_set_size
= geo
->far_set_size
;
596 last_far_set_size
+= (geo
->raid_disks
% geo
->far_set_size
);
598 /* now calculate first sector/dev */
599 chunk
= r10bio
->sector
>> geo
->chunk_shift
;
600 sector
= r10bio
->sector
& geo
->chunk_mask
;
602 chunk
*= geo
->near_copies
;
604 dev
= sector_div(stripe
, geo
->raid_disks
);
606 stripe
*= geo
->far_copies
;
608 sector
+= stripe
<< geo
->chunk_shift
;
610 /* and calculate all the others */
611 for (n
= 0; n
< geo
->near_copies
; n
++) {
615 r10bio
->devs
[slot
].devnum
= d
;
616 r10bio
->devs
[slot
].addr
= s
;
619 for (f
= 1; f
< geo
->far_copies
; f
++) {
620 set
= d
/ geo
->far_set_size
;
621 d
+= geo
->near_copies
;
623 if ((geo
->raid_disks
% geo
->far_set_size
) &&
624 (d
> last_far_set_start
)) {
625 d
-= last_far_set_start
;
626 d
%= last_far_set_size
;
627 d
+= last_far_set_start
;
629 d
%= geo
->far_set_size
;
630 d
+= geo
->far_set_size
* set
;
633 r10bio
->devs
[slot
].devnum
= d
;
634 r10bio
->devs
[slot
].addr
= s
;
638 if (dev
>= geo
->raid_disks
) {
640 sector
+= (geo
->chunk_mask
+ 1);
645 static void raid10_find_phys(struct r10conf
*conf
, struct r10bio
*r10bio
)
647 struct geom
*geo
= &conf
->geo
;
649 if (conf
->reshape_progress
!= MaxSector
&&
650 ((r10bio
->sector
>= conf
->reshape_progress
) !=
651 conf
->mddev
->reshape_backwards
)) {
652 set_bit(R10BIO_Previous
, &r10bio
->state
);
655 clear_bit(R10BIO_Previous
, &r10bio
->state
);
657 __raid10_find_phys(geo
, r10bio
);
660 static sector_t
raid10_find_virt(struct r10conf
*conf
, sector_t sector
, int dev
)
662 sector_t offset
, chunk
, vchunk
;
663 /* Never use conf->prev as this is only called during resync
664 * or recovery, so reshape isn't happening
666 struct geom
*geo
= &conf
->geo
;
667 int far_set_start
= (dev
/ geo
->far_set_size
) * geo
->far_set_size
;
668 int far_set_size
= geo
->far_set_size
;
669 int last_far_set_start
;
671 if (geo
->raid_disks
% geo
->far_set_size
) {
672 last_far_set_start
= (geo
->raid_disks
/ geo
->far_set_size
) - 1;
673 last_far_set_start
*= geo
->far_set_size
;
675 if (dev
>= last_far_set_start
) {
676 far_set_size
= geo
->far_set_size
;
677 far_set_size
+= (geo
->raid_disks
% geo
->far_set_size
);
678 far_set_start
= last_far_set_start
;
682 offset
= sector
& geo
->chunk_mask
;
683 if (geo
->far_offset
) {
685 chunk
= sector
>> geo
->chunk_shift
;
686 fc
= sector_div(chunk
, geo
->far_copies
);
687 dev
-= fc
* geo
->near_copies
;
688 if (dev
< far_set_start
)
691 while (sector
>= geo
->stride
) {
692 sector
-= geo
->stride
;
693 if (dev
< (geo
->near_copies
+ far_set_start
))
694 dev
+= far_set_size
- geo
->near_copies
;
696 dev
-= geo
->near_copies
;
698 chunk
= sector
>> geo
->chunk_shift
;
700 vchunk
= chunk
* geo
->raid_disks
+ dev
;
701 sector_div(vchunk
, geo
->near_copies
);
702 return (vchunk
<< geo
->chunk_shift
) + offset
;
706 * This routine returns the disk from which the requested read should
707 * be done. There is a per-array 'next expected sequential IO' sector
708 * number - if this matches on the next IO then we use the last disk.
709 * There is also a per-disk 'last know head position' sector that is
710 * maintained from IRQ contexts, both the normal and the resync IO
711 * completion handlers update this position correctly. If there is no
712 * perfect sequential match then we pick the disk whose head is closest.
714 * If there are 2 mirrors in the same 2 devices, performance degrades
715 * because position is mirror, not device based.
717 * The rdev for the device selected will have nr_pending incremented.
721 * FIXME: possibly should rethink readbalancing and do it differently
722 * depending on near_copies / far_copies geometry.
724 static struct md_rdev
*read_balance(struct r10conf
*conf
,
725 struct r10bio
*r10_bio
,
728 const sector_t this_sector
= r10_bio
->sector
;
730 int sectors
= r10_bio
->sectors
;
731 int best_good_sectors
;
732 sector_t new_distance
, best_dist
;
733 struct md_rdev
*best_dist_rdev
, *best_pending_rdev
, *rdev
= NULL
;
735 int best_dist_slot
, best_pending_slot
;
736 bool has_nonrot_disk
= false;
737 unsigned int min_pending
;
738 struct geom
*geo
= &conf
->geo
;
740 raid10_find_phys(conf
, r10_bio
);
742 min_pending
= UINT_MAX
;
743 best_dist_rdev
= NULL
;
744 best_pending_rdev
= NULL
;
745 best_dist
= MaxSector
;
746 best_good_sectors
= 0;
748 clear_bit(R10BIO_FailFast
, &r10_bio
->state
);
750 if (raid1_should_read_first(conf
->mddev
, this_sector
, sectors
))
753 for (slot
= 0; slot
< conf
->copies
; slot
++) {
757 unsigned int pending
;
760 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
762 disk
= r10_bio
->devs
[slot
].devnum
;
763 rdev
= conf
->mirrors
[disk
].replacement
;
764 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
) ||
765 r10_bio
->devs
[slot
].addr
+ sectors
>
766 rdev
->recovery_offset
)
767 rdev
= conf
->mirrors
[disk
].rdev
;
769 test_bit(Faulty
, &rdev
->flags
))
771 if (!test_bit(In_sync
, &rdev
->flags
) &&
772 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
775 dev_sector
= r10_bio
->devs
[slot
].addr
;
776 if (is_badblock(rdev
, dev_sector
, sectors
,
777 &first_bad
, &bad_sectors
)) {
778 if (best_dist
< MaxSector
)
779 /* Already have a better slot */
781 if (first_bad
<= dev_sector
) {
782 /* Cannot read here. If this is the
783 * 'primary' device, then we must not read
784 * beyond 'bad_sectors' from another device.
786 bad_sectors
-= (dev_sector
- first_bad
);
787 if (!do_balance
&& sectors
> bad_sectors
)
788 sectors
= bad_sectors
;
789 if (best_good_sectors
> sectors
)
790 best_good_sectors
= sectors
;
792 sector_t good_sectors
=
793 first_bad
- dev_sector
;
794 if (good_sectors
> best_good_sectors
) {
795 best_good_sectors
= good_sectors
;
796 best_dist_slot
= slot
;
797 best_dist_rdev
= rdev
;
800 /* Must read from here */
805 best_good_sectors
= sectors
;
810 nonrot
= bdev_nonrot(rdev
->bdev
);
811 has_nonrot_disk
|= nonrot
;
812 pending
= atomic_read(&rdev
->nr_pending
);
813 if (min_pending
> pending
&& nonrot
) {
814 min_pending
= pending
;
815 best_pending_slot
= slot
;
816 best_pending_rdev
= rdev
;
819 if (best_dist_slot
>= 0)
820 /* At least 2 disks to choose from so failfast is OK */
821 set_bit(R10BIO_FailFast
, &r10_bio
->state
);
822 /* This optimisation is debatable, and completely destroys
823 * sequential read speed for 'far copies' arrays. So only
824 * keep it for 'near' arrays, and review those later.
826 if (geo
->near_copies
> 1 && !pending
)
829 /* for far > 1 always use the lowest address */
830 else if (geo
->far_copies
> 1)
831 new_distance
= r10_bio
->devs
[slot
].addr
;
833 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
834 conf
->mirrors
[disk
].head_position
);
836 if (new_distance
< best_dist
) {
837 best_dist
= new_distance
;
838 best_dist_slot
= slot
;
839 best_dist_rdev
= rdev
;
842 if (slot
>= conf
->copies
) {
843 if (has_nonrot_disk
) {
844 slot
= best_pending_slot
;
845 rdev
= best_pending_rdev
;
847 slot
= best_dist_slot
;
848 rdev
= best_dist_rdev
;
853 atomic_inc(&rdev
->nr_pending
);
854 r10_bio
->read_slot
= slot
;
857 *max_sectors
= best_good_sectors
;
862 static void flush_pending_writes(struct r10conf
*conf
)
864 /* Any writes that have been queued but are awaiting
865 * bitmap updates get flushed here.
867 spin_lock_irq(&conf
->device_lock
);
869 if (conf
->pending_bio_list
.head
) {
870 struct blk_plug plug
;
873 bio
= bio_list_get(&conf
->pending_bio_list
);
874 spin_unlock_irq(&conf
->device_lock
);
877 * As this is called in a wait_event() loop (see freeze_array),
878 * current->state might be TASK_UNINTERRUPTIBLE which will
879 * cause a warning when we prepare to wait again. As it is
880 * rare that this path is taken, it is perfectly safe to force
881 * us to go around the wait_event() loop again, so the warning
882 * is a false-positive. Silence the warning by resetting
885 __set_current_state(TASK_RUNNING
);
887 blk_start_plug(&plug
);
888 raid1_prepare_flush_writes(conf
->mddev
);
889 wake_up(&conf
->wait_barrier
);
891 while (bio
) { /* submit pending writes */
892 struct bio
*next
= bio
->bi_next
;
894 raid1_submit_write(bio
);
898 blk_finish_plug(&plug
);
900 spin_unlock_irq(&conf
->device_lock
);
904 * Sometimes we need to suspend IO while we do something else,
905 * either some resync/recovery, or reconfigure the array.
906 * To do this we raise a 'barrier'.
907 * The 'barrier' is a counter that can be raised multiple times
908 * to count how many activities are happening which preclude
910 * We can only raise the barrier if there is no pending IO.
911 * i.e. if nr_pending == 0.
912 * We choose only to raise the barrier if no-one is waiting for the
913 * barrier to go down. This means that as soon as an IO request
914 * is ready, no other operations which require a barrier will start
915 * until the IO request has had a chance.
917 * So: regular IO calls 'wait_barrier'. When that returns there
918 * is no backgroup IO happening, It must arrange to call
919 * allow_barrier when it has finished its IO.
920 * backgroup IO calls must call raise_barrier. Once that returns
921 * there is no normal IO happeing. It must arrange to call
922 * lower_barrier when the particular background IO completes.
925 static void raise_barrier(struct r10conf
*conf
, int force
)
927 write_seqlock_irq(&conf
->resync_lock
);
929 if (WARN_ON_ONCE(force
&& !conf
->barrier
))
932 /* Wait until no block IO is waiting (unless 'force') */
933 wait_event_barrier(conf
, force
|| !conf
->nr_waiting
);
935 /* block any new IO from starting */
936 WRITE_ONCE(conf
->barrier
, conf
->barrier
+ 1);
938 /* Now wait for all pending IO to complete */
939 wait_event_barrier(conf
, !atomic_read(&conf
->nr_pending
) &&
940 conf
->barrier
< RESYNC_DEPTH
);
942 write_sequnlock_irq(&conf
->resync_lock
);
945 static void lower_barrier(struct r10conf
*conf
)
949 write_seqlock_irqsave(&conf
->resync_lock
, flags
);
950 WRITE_ONCE(conf
->barrier
, conf
->barrier
- 1);
951 write_sequnlock_irqrestore(&conf
->resync_lock
, flags
);
952 wake_up(&conf
->wait_barrier
);
955 static bool stop_waiting_barrier(struct r10conf
*conf
)
957 struct bio_list
*bio_list
= current
->bio_list
;
958 struct md_thread
*thread
;
960 /* barrier is dropped */
965 * If there are already pending requests (preventing the barrier from
966 * rising completely), and the pre-process bio queue isn't empty, then
967 * don't wait, as we need to empty that queue to get the nr_pending
970 if (atomic_read(&conf
->nr_pending
) && bio_list
&&
971 (!bio_list_empty(&bio_list
[0]) || !bio_list_empty(&bio_list
[1])))
974 /* daemon thread must exist while handling io */
975 thread
= rcu_dereference_protected(conf
->mddev
->thread
, true);
977 * move on if io is issued from raid10d(), nr_pending is not released
978 * from original io(see handle_read_error()). All raise barrier is
979 * blocked until this io is done.
981 if (thread
->tsk
== current
) {
982 WARN_ON_ONCE(atomic_read(&conf
->nr_pending
) == 0);
989 static bool wait_barrier_nolock(struct r10conf
*conf
)
991 unsigned int seq
= read_seqbegin(&conf
->resync_lock
);
993 if (READ_ONCE(conf
->barrier
))
996 atomic_inc(&conf
->nr_pending
);
997 if (!read_seqretry(&conf
->resync_lock
, seq
))
1000 if (atomic_dec_and_test(&conf
->nr_pending
))
1001 wake_up_barrier(conf
);
1006 static bool wait_barrier(struct r10conf
*conf
, bool nowait
)
1010 if (wait_barrier_nolock(conf
))
1013 write_seqlock_irq(&conf
->resync_lock
);
1014 if (conf
->barrier
) {
1015 /* Return false when nowait flag is set */
1020 mddev_add_trace_msg(conf
->mddev
, "raid10 wait barrier");
1021 wait_event_barrier(conf
, stop_waiting_barrier(conf
));
1024 if (!conf
->nr_waiting
)
1025 wake_up(&conf
->wait_barrier
);
1027 /* Only increment nr_pending when we wait */
1029 atomic_inc(&conf
->nr_pending
);
1030 write_sequnlock_irq(&conf
->resync_lock
);
1034 static void allow_barrier(struct r10conf
*conf
)
1036 if ((atomic_dec_and_test(&conf
->nr_pending
)) ||
1037 (conf
->array_freeze_pending
))
1038 wake_up_barrier(conf
);
1041 static void freeze_array(struct r10conf
*conf
, int extra
)
1043 /* stop syncio and normal IO and wait for everything to
1045 * We increment barrier and nr_waiting, and then
1046 * wait until nr_pending match nr_queued+extra
1047 * This is called in the context of one normal IO request
1048 * that has failed. Thus any sync request that might be pending
1049 * will be blocked by nr_pending, and we need to wait for
1050 * pending IO requests to complete or be queued for re-try.
1051 * Thus the number queued (nr_queued) plus this request (extra)
1052 * must match the number of pending IOs (nr_pending) before
1055 write_seqlock_irq(&conf
->resync_lock
);
1056 conf
->array_freeze_pending
++;
1057 WRITE_ONCE(conf
->barrier
, conf
->barrier
+ 1);
1059 wait_event_barrier_cmd(conf
, atomic_read(&conf
->nr_pending
) ==
1060 conf
->nr_queued
+ extra
, flush_pending_writes(conf
));
1061 conf
->array_freeze_pending
--;
1062 write_sequnlock_irq(&conf
->resync_lock
);
1065 static void unfreeze_array(struct r10conf
*conf
)
1067 /* reverse the effect of the freeze */
1068 write_seqlock_irq(&conf
->resync_lock
);
1069 WRITE_ONCE(conf
->barrier
, conf
->barrier
- 1);
1071 wake_up(&conf
->wait_barrier
);
1072 write_sequnlock_irq(&conf
->resync_lock
);
1075 static sector_t
choose_data_offset(struct r10bio
*r10_bio
,
1076 struct md_rdev
*rdev
)
1078 if (!test_bit(MD_RECOVERY_RESHAPE
, &rdev
->mddev
->recovery
) ||
1079 test_bit(R10BIO_Previous
, &r10_bio
->state
))
1080 return rdev
->data_offset
;
1082 return rdev
->new_data_offset
;
1085 static void raid10_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
1087 struct raid1_plug_cb
*plug
= container_of(cb
, struct raid1_plug_cb
, cb
);
1088 struct mddev
*mddev
= plug
->cb
.data
;
1089 struct r10conf
*conf
= mddev
->private;
1092 if (from_schedule
) {
1093 spin_lock_irq(&conf
->device_lock
);
1094 bio_list_merge(&conf
->pending_bio_list
, &plug
->pending
);
1095 spin_unlock_irq(&conf
->device_lock
);
1096 wake_up_barrier(conf
);
1097 md_wakeup_thread(mddev
->thread
);
1102 /* we aren't scheduling, so we can do the write-out directly. */
1103 bio
= bio_list_get(&plug
->pending
);
1104 raid1_prepare_flush_writes(mddev
);
1105 wake_up_barrier(conf
);
1107 while (bio
) { /* submit pending writes */
1108 struct bio
*next
= bio
->bi_next
;
1110 raid1_submit_write(bio
);
1118 * 1. Register the new request and wait if the reconstruction thread has put
1119 * up a bar for new requests. Continue immediately if no resync is active
1121 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1123 static bool regular_request_wait(struct mddev
*mddev
, struct r10conf
*conf
,
1124 struct bio
*bio
, sector_t sectors
)
1126 /* Bail out if REQ_NOWAIT is set for the bio */
1127 if (!wait_barrier(conf
, bio
->bi_opf
& REQ_NOWAIT
)) {
1128 bio_wouldblock_error(bio
);
1131 while (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1132 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
&&
1133 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
) {
1134 allow_barrier(conf
);
1135 if (bio
->bi_opf
& REQ_NOWAIT
) {
1136 bio_wouldblock_error(bio
);
1139 mddev_add_trace_msg(conf
->mddev
, "raid10 wait reshape");
1140 wait_event(conf
->wait_barrier
,
1141 conf
->reshape_progress
<= bio
->bi_iter
.bi_sector
||
1142 conf
->reshape_progress
>= bio
->bi_iter
.bi_sector
+
1144 wait_barrier(conf
, false);
1149 static void raid10_read_request(struct mddev
*mddev
, struct bio
*bio
,
1150 struct r10bio
*r10_bio
, bool io_accounting
)
1152 struct r10conf
*conf
= mddev
->private;
1153 struct bio
*read_bio
;
1154 const enum req_op op
= bio_op(bio
);
1155 const blk_opf_t do_sync
= bio
->bi_opf
& REQ_SYNC
;
1157 struct md_rdev
*rdev
;
1158 char b
[BDEVNAME_SIZE
];
1159 int slot
= r10_bio
->read_slot
;
1160 struct md_rdev
*err_rdev
= NULL
;
1161 gfp_t gfp
= GFP_NOIO
;
1164 if (slot
>= 0 && r10_bio
->devs
[slot
].rdev
) {
1166 * This is an error retry, but we cannot
1167 * safely dereference the rdev in the r10_bio,
1168 * we must use the one in conf.
1169 * If it has already been disconnected (unlikely)
1170 * we lose the device name in error messages.
1174 * As we are blocking raid10, it is a little safer to
1177 gfp
= GFP_NOIO
| __GFP_HIGH
;
1179 disk
= r10_bio
->devs
[slot
].devnum
;
1180 err_rdev
= conf
->mirrors
[disk
].rdev
;
1182 snprintf(b
, sizeof(b
), "%pg", err_rdev
->bdev
);
1185 /* This never gets dereferenced */
1186 err_rdev
= r10_bio
->devs
[slot
].rdev
;
1190 if (!regular_request_wait(mddev
, conf
, bio
, r10_bio
->sectors
))
1192 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
1195 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1197 (unsigned long long)r10_bio
->sector
);
1199 raid_end_bio_io(r10_bio
);
1203 pr_err_ratelimited("md/raid10:%s: %pg: redirecting sector %llu to another mirror\n",
1206 (unsigned long long)r10_bio
->sector
);
1207 if (max_sectors
< bio_sectors(bio
)) {
1208 struct bio
*split
= bio_split(bio
, max_sectors
,
1209 gfp
, &conf
->bio_split
);
1210 if (IS_ERR(split
)) {
1211 error
= PTR_ERR(split
);
1214 bio_chain(split
, bio
);
1215 allow_barrier(conf
);
1216 submit_bio_noacct(bio
);
1217 wait_barrier(conf
, false);
1219 r10_bio
->master_bio
= bio
;
1220 r10_bio
->sectors
= max_sectors
;
1222 slot
= r10_bio
->read_slot
;
1224 if (io_accounting
) {
1225 md_account_bio(mddev
, &bio
);
1226 r10_bio
->master_bio
= bio
;
1228 read_bio
= bio_alloc_clone(rdev
->bdev
, bio
, gfp
, &mddev
->bio_set
);
1230 r10_bio
->devs
[slot
].bio
= read_bio
;
1231 r10_bio
->devs
[slot
].rdev
= rdev
;
1233 read_bio
->bi_iter
.bi_sector
= r10_bio
->devs
[slot
].addr
+
1234 choose_data_offset(r10_bio
, rdev
);
1235 read_bio
->bi_end_io
= raid10_end_read_request
;
1236 read_bio
->bi_opf
= op
| do_sync
;
1237 if (test_bit(FailFast
, &rdev
->flags
) &&
1238 test_bit(R10BIO_FailFast
, &r10_bio
->state
))
1239 read_bio
->bi_opf
|= MD_FAILFAST
;
1240 read_bio
->bi_private
= r10_bio
;
1241 mddev_trace_remap(mddev
, read_bio
, r10_bio
->sector
);
1242 submit_bio_noacct(read_bio
);
1245 atomic_dec(&rdev
->nr_pending
);
1246 bio
->bi_status
= errno_to_blk_status(error
);
1247 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1248 raid_end_bio_io(r10_bio
);
1251 static void raid10_write_one_disk(struct mddev
*mddev
, struct r10bio
*r10_bio
,
1252 struct bio
*bio
, bool replacement
,
1255 const enum req_op op
= bio_op(bio
);
1256 const blk_opf_t do_sync
= bio
->bi_opf
& REQ_SYNC
;
1257 const blk_opf_t do_fua
= bio
->bi_opf
& REQ_FUA
;
1258 const blk_opf_t do_atomic
= bio
->bi_opf
& REQ_ATOMIC
;
1259 unsigned long flags
;
1260 struct r10conf
*conf
= mddev
->private;
1261 struct md_rdev
*rdev
;
1262 int devnum
= r10_bio
->devs
[n_copy
].devnum
;
1265 rdev
= replacement
? conf
->mirrors
[devnum
].replacement
:
1266 conf
->mirrors
[devnum
].rdev
;
1268 mbio
= bio_alloc_clone(rdev
->bdev
, bio
, GFP_NOIO
, &mddev
->bio_set
);
1270 r10_bio
->devs
[n_copy
].repl_bio
= mbio
;
1272 r10_bio
->devs
[n_copy
].bio
= mbio
;
1274 mbio
->bi_iter
.bi_sector
= (r10_bio
->devs
[n_copy
].addr
+
1275 choose_data_offset(r10_bio
, rdev
));
1276 mbio
->bi_end_io
= raid10_end_write_request
;
1277 mbio
->bi_opf
= op
| do_sync
| do_fua
| do_atomic
;
1278 if (!replacement
&& test_bit(FailFast
,
1279 &conf
->mirrors
[devnum
].rdev
->flags
)
1280 && enough(conf
, devnum
))
1281 mbio
->bi_opf
|= MD_FAILFAST
;
1282 mbio
->bi_private
= r10_bio
;
1283 mddev_trace_remap(mddev
, mbio
, r10_bio
->sector
);
1284 /* flush_pending_writes() needs access to the rdev so...*/
1285 mbio
->bi_bdev
= (void *)rdev
;
1287 atomic_inc(&r10_bio
->remaining
);
1289 if (!raid1_add_bio_to_plug(mddev
, mbio
, raid10_unplug
, conf
->copies
)) {
1290 spin_lock_irqsave(&conf
->device_lock
, flags
);
1291 bio_list_add(&conf
->pending_bio_list
, mbio
);
1292 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1293 md_wakeup_thread(mddev
->thread
);
1297 static void wait_blocked_dev(struct mddev
*mddev
, struct r10bio
*r10_bio
)
1299 struct r10conf
*conf
= mddev
->private;
1300 struct md_rdev
*blocked_rdev
;
1304 blocked_rdev
= NULL
;
1305 for (i
= 0; i
< conf
->copies
; i
++) {
1306 struct md_rdev
*rdev
, *rrdev
;
1308 rdev
= conf
->mirrors
[i
].rdev
;
1310 sector_t dev_sector
= r10_bio
->devs
[i
].addr
;
1313 * Discard request doesn't care the write result
1314 * so it doesn't need to wait blocked disk here.
1316 if (test_bit(WriteErrorSeen
, &rdev
->flags
) &&
1318 rdev_has_badblock(rdev
, dev_sector
,
1319 r10_bio
->sectors
) < 0)
1321 * Mustn't write here until the bad
1322 * block is acknowledged
1324 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1326 if (rdev_blocked(rdev
)) {
1327 blocked_rdev
= rdev
;
1328 atomic_inc(&rdev
->nr_pending
);
1333 rrdev
= conf
->mirrors
[i
].replacement
;
1334 if (rrdev
&& rdev_blocked(rrdev
)) {
1335 atomic_inc(&rrdev
->nr_pending
);
1336 blocked_rdev
= rrdev
;
1341 if (unlikely(blocked_rdev
)) {
1342 /* Have to wait for this device to get unblocked, then retry */
1343 allow_barrier(conf
);
1344 mddev_add_trace_msg(conf
->mddev
,
1345 "raid10 %s wait rdev %d blocked",
1346 __func__
, blocked_rdev
->raid_disk
);
1347 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1348 wait_barrier(conf
, false);
1353 static void raid10_write_request(struct mddev
*mddev
, struct bio
*bio
,
1354 struct r10bio
*r10_bio
)
1356 struct r10conf
*conf
= mddev
->private;
1362 if ((mddev_is_clustered(mddev
) &&
1363 md_cluster_ops
->area_resyncing(mddev
, WRITE
,
1364 bio
->bi_iter
.bi_sector
,
1365 bio_end_sector(bio
)))) {
1367 /* Bail out if REQ_NOWAIT is set for the bio */
1368 if (bio
->bi_opf
& REQ_NOWAIT
) {
1369 bio_wouldblock_error(bio
);
1373 prepare_to_wait(&conf
->wait_barrier
,
1375 if (!md_cluster_ops
->area_resyncing(mddev
, WRITE
,
1376 bio
->bi_iter
.bi_sector
, bio_end_sector(bio
)))
1380 finish_wait(&conf
->wait_barrier
, &w
);
1383 sectors
= r10_bio
->sectors
;
1384 if (!regular_request_wait(mddev
, conf
, bio
, sectors
))
1386 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1387 (mddev
->reshape_backwards
1388 ? (bio
->bi_iter
.bi_sector
< conf
->reshape_safe
&&
1389 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
)
1390 : (bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_safe
&&
1391 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
))) {
1392 /* Need to update reshape_position in metadata */
1393 mddev
->reshape_position
= conf
->reshape_progress
;
1394 set_mask_bits(&mddev
->sb_flags
, 0,
1395 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
1396 md_wakeup_thread(mddev
->thread
);
1397 if (bio
->bi_opf
& REQ_NOWAIT
) {
1398 allow_barrier(conf
);
1399 bio_wouldblock_error(bio
);
1402 mddev_add_trace_msg(conf
->mddev
,
1403 "raid10 wait reshape metadata");
1404 wait_event(mddev
->sb_wait
,
1405 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
1407 conf
->reshape_safe
= mddev
->reshape_position
;
1410 /* first select target devices under rcu_lock and
1411 * inc refcount on their rdev. Record them by setting
1413 * If there are known/acknowledged bad blocks on any device
1414 * on which we have seen a write error, we want to avoid
1415 * writing to those blocks. This potentially requires several
1416 * writes to write around the bad blocks. Each set of writes
1417 * gets its own r10_bio with a set of bios attached.
1420 r10_bio
->read_slot
= -1; /* make sure repl_bio gets freed */
1421 raid10_find_phys(conf
, r10_bio
);
1423 wait_blocked_dev(mddev
, r10_bio
);
1425 max_sectors
= r10_bio
->sectors
;
1427 for (i
= 0; i
< conf
->copies
; i
++) {
1428 int d
= r10_bio
->devs
[i
].devnum
;
1429 struct md_rdev
*rdev
, *rrdev
;
1431 rdev
= conf
->mirrors
[d
].rdev
;
1432 rrdev
= conf
->mirrors
[d
].replacement
;
1433 if (rdev
&& (test_bit(Faulty
, &rdev
->flags
)))
1435 if (rrdev
&& (test_bit(Faulty
, &rrdev
->flags
)))
1438 r10_bio
->devs
[i
].bio
= NULL
;
1439 r10_bio
->devs
[i
].repl_bio
= NULL
;
1441 if (!rdev
&& !rrdev
) {
1442 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
1445 if (rdev
&& test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1447 sector_t dev_sector
= r10_bio
->devs
[i
].addr
;
1451 is_bad
= is_badblock(rdev
, dev_sector
, max_sectors
,
1452 &first_bad
, &bad_sectors
);
1453 if (is_bad
&& first_bad
<= dev_sector
) {
1454 /* Cannot write here at all */
1455 bad_sectors
-= (dev_sector
- first_bad
);
1456 if (bad_sectors
< max_sectors
)
1457 /* Mustn't write more than bad_sectors
1458 * to other devices yet
1460 max_sectors
= bad_sectors
;
1461 /* We don't set R10BIO_Degraded as that
1462 * only applies if the disk is missing,
1463 * so it might be re-added, and we want to
1464 * know to recover this chunk.
1465 * In this case the device is here, and the
1466 * fact that this chunk is not in-sync is
1467 * recorded in the bad block log.
1475 * We cannot atomically write this, so just
1476 * error in that case. It could be possible to
1477 * atomically write other mirrors, but the
1478 * complexity of supporting that is not worth
1481 if (bio
->bi_opf
& REQ_ATOMIC
) {
1486 good_sectors
= first_bad
- dev_sector
;
1487 if (good_sectors
< max_sectors
)
1488 max_sectors
= good_sectors
;
1492 r10_bio
->devs
[i
].bio
= bio
;
1493 atomic_inc(&rdev
->nr_pending
);
1496 r10_bio
->devs
[i
].repl_bio
= bio
;
1497 atomic_inc(&rrdev
->nr_pending
);
1501 if (max_sectors
< r10_bio
->sectors
)
1502 r10_bio
->sectors
= max_sectors
;
1504 if (r10_bio
->sectors
< bio_sectors(bio
)) {
1505 struct bio
*split
= bio_split(bio
, r10_bio
->sectors
,
1506 GFP_NOIO
, &conf
->bio_split
);
1507 if (IS_ERR(split
)) {
1508 error
= PTR_ERR(split
);
1511 bio_chain(split
, bio
);
1512 allow_barrier(conf
);
1513 submit_bio_noacct(bio
);
1514 wait_barrier(conf
, false);
1516 r10_bio
->master_bio
= bio
;
1519 md_account_bio(mddev
, &bio
);
1520 r10_bio
->master_bio
= bio
;
1521 atomic_set(&r10_bio
->remaining
, 1);
1522 mddev
->bitmap_ops
->startwrite(mddev
, r10_bio
->sector
, r10_bio
->sectors
,
1525 for (i
= 0; i
< conf
->copies
; i
++) {
1526 if (r10_bio
->devs
[i
].bio
)
1527 raid10_write_one_disk(mddev
, r10_bio
, bio
, false, i
);
1528 if (r10_bio
->devs
[i
].repl_bio
)
1529 raid10_write_one_disk(mddev
, r10_bio
, bio
, true, i
);
1531 one_write_done(r10_bio
);
1534 for (k
= 0; k
< i
; k
++) {
1535 int d
= r10_bio
->devs
[k
].devnum
;
1536 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
1537 struct md_rdev
*rrdev
= conf
->mirrors
[d
].replacement
;
1539 if (r10_bio
->devs
[k
].bio
) {
1540 rdev_dec_pending(rdev
, mddev
);
1541 r10_bio
->devs
[k
].bio
= NULL
;
1543 if (r10_bio
->devs
[k
].repl_bio
) {
1544 rdev_dec_pending(rrdev
, mddev
);
1545 r10_bio
->devs
[k
].repl_bio
= NULL
;
1549 bio
->bi_status
= errno_to_blk_status(error
);
1550 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1551 raid_end_bio_io(r10_bio
);
1554 static void __make_request(struct mddev
*mddev
, struct bio
*bio
, int sectors
)
1556 struct r10conf
*conf
= mddev
->private;
1557 struct r10bio
*r10_bio
;
1559 r10_bio
= mempool_alloc(&conf
->r10bio_pool
, GFP_NOIO
);
1561 r10_bio
->master_bio
= bio
;
1562 r10_bio
->sectors
= sectors
;
1564 r10_bio
->mddev
= mddev
;
1565 r10_bio
->sector
= bio
->bi_iter
.bi_sector
;
1567 r10_bio
->read_slot
= -1;
1568 memset(r10_bio
->devs
, 0, sizeof(r10_bio
->devs
[0]) *
1569 conf
->geo
.raid_disks
);
1571 if (bio_data_dir(bio
) == READ
)
1572 raid10_read_request(mddev
, bio
, r10_bio
, true);
1574 raid10_write_request(mddev
, bio
, r10_bio
);
1577 static void raid_end_discard_bio(struct r10bio
*r10bio
)
1579 struct r10conf
*conf
= r10bio
->mddev
->private;
1580 struct r10bio
*first_r10bio
;
1582 while (atomic_dec_and_test(&r10bio
->remaining
)) {
1584 allow_barrier(conf
);
1586 if (!test_bit(R10BIO_Discard
, &r10bio
->state
)) {
1587 first_r10bio
= (struct r10bio
*)r10bio
->master_bio
;
1588 free_r10bio(r10bio
);
1589 r10bio
= first_r10bio
;
1591 md_write_end(r10bio
->mddev
);
1592 bio_endio(r10bio
->master_bio
);
1593 free_r10bio(r10bio
);
1599 static void raid10_end_discard_request(struct bio
*bio
)
1601 struct r10bio
*r10_bio
= bio
->bi_private
;
1602 struct r10conf
*conf
= r10_bio
->mddev
->private;
1603 struct md_rdev
*rdev
= NULL
;
1608 * We don't care the return value of discard bio
1610 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
1611 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1613 dev
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
1614 rdev
= repl
? conf
->mirrors
[dev
].replacement
:
1615 conf
->mirrors
[dev
].rdev
;
1617 raid_end_discard_bio(r10_bio
);
1618 rdev_dec_pending(rdev
, conf
->mddev
);
1622 * There are some limitations to handle discard bio
1623 * 1st, the discard size is bigger than stripe_size*2.
1624 * 2st, if the discard bio spans reshape progress, we use the old way to
1625 * handle discard bio
1627 static int raid10_handle_discard(struct mddev
*mddev
, struct bio
*bio
)
1629 struct r10conf
*conf
= mddev
->private;
1630 struct geom
*geo
= &conf
->geo
;
1631 int far_copies
= geo
->far_copies
;
1632 bool first_copy
= true;
1633 struct r10bio
*r10_bio
, *first_r10bio
;
1637 unsigned int stripe_size
;
1638 unsigned int stripe_data_disks
;
1639 sector_t split_size
;
1640 sector_t bio_start
, bio_end
;
1641 sector_t first_stripe_index
, last_stripe_index
;
1642 sector_t start_disk_offset
;
1643 unsigned int start_disk_index
;
1644 sector_t end_disk_offset
;
1645 unsigned int end_disk_index
;
1646 unsigned int remainder
;
1648 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
1651 if (WARN_ON_ONCE(bio
->bi_opf
& REQ_NOWAIT
)) {
1652 bio_wouldblock_error(bio
);
1655 wait_barrier(conf
, false);
1658 * Check reshape again to avoid reshape happens after checking
1659 * MD_RECOVERY_RESHAPE and before wait_barrier
1661 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
1664 if (geo
->near_copies
)
1665 stripe_data_disks
= geo
->raid_disks
/ geo
->near_copies
+
1666 geo
->raid_disks
% geo
->near_copies
;
1668 stripe_data_disks
= geo
->raid_disks
;
1670 stripe_size
= stripe_data_disks
<< geo
->chunk_shift
;
1672 bio_start
= bio
->bi_iter
.bi_sector
;
1673 bio_end
= bio_end_sector(bio
);
1676 * Maybe one discard bio is smaller than strip size or across one
1677 * stripe and discard region is larger than one stripe size. For far
1678 * offset layout, if the discard region is not aligned with stripe
1679 * size, there is hole when we submit discard bio to member disk.
1680 * For simplicity, we only handle discard bio which discard region
1681 * is bigger than stripe_size * 2
1683 if (bio_sectors(bio
) < stripe_size
*2)
1687 * Keep bio aligned with strip size.
1689 div_u64_rem(bio_start
, stripe_size
, &remainder
);
1691 split_size
= stripe_size
- remainder
;
1692 split
= bio_split(bio
, split_size
, GFP_NOIO
, &conf
->bio_split
);
1693 if (IS_ERR(split
)) {
1694 bio
->bi_status
= errno_to_blk_status(PTR_ERR(split
));
1698 bio_chain(split
, bio
);
1699 allow_barrier(conf
);
1700 /* Resend the fist split part */
1701 submit_bio_noacct(split
);
1702 wait_barrier(conf
, false);
1704 div_u64_rem(bio_end
, stripe_size
, &remainder
);
1706 split_size
= bio_sectors(bio
) - remainder
;
1707 split
= bio_split(bio
, split_size
, GFP_NOIO
, &conf
->bio_split
);
1708 if (IS_ERR(split
)) {
1709 bio
->bi_status
= errno_to_blk_status(PTR_ERR(split
));
1713 bio_chain(split
, bio
);
1714 allow_barrier(conf
);
1715 /* Resend the second split part */
1716 submit_bio_noacct(bio
);
1718 wait_barrier(conf
, false);
1721 bio_start
= bio
->bi_iter
.bi_sector
;
1722 bio_end
= bio_end_sector(bio
);
1725 * Raid10 uses chunk as the unit to store data. It's similar like raid0.
1726 * One stripe contains the chunks from all member disk (one chunk from
1727 * one disk at the same HBA address). For layout detail, see 'man md 4'
1729 chunk
= bio_start
>> geo
->chunk_shift
;
1730 chunk
*= geo
->near_copies
;
1731 first_stripe_index
= chunk
;
1732 start_disk_index
= sector_div(first_stripe_index
, geo
->raid_disks
);
1733 if (geo
->far_offset
)
1734 first_stripe_index
*= geo
->far_copies
;
1735 start_disk_offset
= (bio_start
& geo
->chunk_mask
) +
1736 (first_stripe_index
<< geo
->chunk_shift
);
1738 chunk
= bio_end
>> geo
->chunk_shift
;
1739 chunk
*= geo
->near_copies
;
1740 last_stripe_index
= chunk
;
1741 end_disk_index
= sector_div(last_stripe_index
, geo
->raid_disks
);
1742 if (geo
->far_offset
)
1743 last_stripe_index
*= geo
->far_copies
;
1744 end_disk_offset
= (bio_end
& geo
->chunk_mask
) +
1745 (last_stripe_index
<< geo
->chunk_shift
);
1748 r10_bio
= mempool_alloc(&conf
->r10bio_pool
, GFP_NOIO
);
1749 r10_bio
->mddev
= mddev
;
1751 r10_bio
->sectors
= 0;
1752 memset(r10_bio
->devs
, 0, sizeof(r10_bio
->devs
[0]) * geo
->raid_disks
);
1753 wait_blocked_dev(mddev
, r10_bio
);
1756 * For far layout it needs more than one r10bio to cover all regions.
1757 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1758 * to record the discard bio. Other r10bio->master_bio record the first
1759 * r10bio. The first r10bio only release after all other r10bios finish.
1760 * The discard bio returns only first r10bio finishes
1763 r10_bio
->master_bio
= bio
;
1764 set_bit(R10BIO_Discard
, &r10_bio
->state
);
1766 first_r10bio
= r10_bio
;
1768 r10_bio
->master_bio
= (struct bio
*)first_r10bio
;
1771 * first select target devices under rcu_lock and
1772 * inc refcount on their rdev. Record them by setting
1775 for (disk
= 0; disk
< geo
->raid_disks
; disk
++) {
1776 struct md_rdev
*rdev
, *rrdev
;
1778 rdev
= conf
->mirrors
[disk
].rdev
;
1779 rrdev
= conf
->mirrors
[disk
].replacement
;
1780 r10_bio
->devs
[disk
].bio
= NULL
;
1781 r10_bio
->devs
[disk
].repl_bio
= NULL
;
1783 if (rdev
&& (test_bit(Faulty
, &rdev
->flags
)))
1785 if (rrdev
&& (test_bit(Faulty
, &rrdev
->flags
)))
1787 if (!rdev
&& !rrdev
)
1791 r10_bio
->devs
[disk
].bio
= bio
;
1792 atomic_inc(&rdev
->nr_pending
);
1795 r10_bio
->devs
[disk
].repl_bio
= bio
;
1796 atomic_inc(&rrdev
->nr_pending
);
1800 atomic_set(&r10_bio
->remaining
, 1);
1801 for (disk
= 0; disk
< geo
->raid_disks
; disk
++) {
1802 sector_t dev_start
, dev_end
;
1803 struct bio
*mbio
, *rbio
= NULL
;
1806 * Now start to calculate the start and end address for each disk.
1807 * The space between dev_start and dev_end is the discard region.
1809 * For dev_start, it needs to consider three conditions:
1810 * 1st, the disk is before start_disk, you can imagine the disk in
1811 * the next stripe. So the dev_start is the start address of next
1813 * 2st, the disk is after start_disk, it means the disk is at the
1814 * same stripe of first disk
1815 * 3st, the first disk itself, we can use start_disk_offset directly
1817 if (disk
< start_disk_index
)
1818 dev_start
= (first_stripe_index
+ 1) * mddev
->chunk_sectors
;
1819 else if (disk
> start_disk_index
)
1820 dev_start
= first_stripe_index
* mddev
->chunk_sectors
;
1822 dev_start
= start_disk_offset
;
1824 if (disk
< end_disk_index
)
1825 dev_end
= (last_stripe_index
+ 1) * mddev
->chunk_sectors
;
1826 else if (disk
> end_disk_index
)
1827 dev_end
= last_stripe_index
* mddev
->chunk_sectors
;
1829 dev_end
= end_disk_offset
;
1832 * It only handles discard bio which size is >= stripe size, so
1833 * dev_end > dev_start all the time.
1834 * It doesn't need to use rcu lock to get rdev here. We already
1835 * add rdev->nr_pending in the first loop.
1837 if (r10_bio
->devs
[disk
].bio
) {
1838 struct md_rdev
*rdev
= conf
->mirrors
[disk
].rdev
;
1839 mbio
= bio_alloc_clone(bio
->bi_bdev
, bio
, GFP_NOIO
,
1841 mbio
->bi_end_io
= raid10_end_discard_request
;
1842 mbio
->bi_private
= r10_bio
;
1843 r10_bio
->devs
[disk
].bio
= mbio
;
1844 r10_bio
->devs
[disk
].devnum
= disk
;
1845 atomic_inc(&r10_bio
->remaining
);
1846 md_submit_discard_bio(mddev
, rdev
, mbio
,
1847 dev_start
+ choose_data_offset(r10_bio
, rdev
),
1848 dev_end
- dev_start
);
1851 if (r10_bio
->devs
[disk
].repl_bio
) {
1852 struct md_rdev
*rrdev
= conf
->mirrors
[disk
].replacement
;
1853 rbio
= bio_alloc_clone(bio
->bi_bdev
, bio
, GFP_NOIO
,
1855 rbio
->bi_end_io
= raid10_end_discard_request
;
1856 rbio
->bi_private
= r10_bio
;
1857 r10_bio
->devs
[disk
].repl_bio
= rbio
;
1858 r10_bio
->devs
[disk
].devnum
= disk
;
1859 atomic_inc(&r10_bio
->remaining
);
1860 md_submit_discard_bio(mddev
, rrdev
, rbio
,
1861 dev_start
+ choose_data_offset(r10_bio
, rrdev
),
1862 dev_end
- dev_start
);
1867 if (!geo
->far_offset
&& --far_copies
) {
1868 first_stripe_index
+= geo
->stride
>> geo
->chunk_shift
;
1869 start_disk_offset
+= geo
->stride
;
1870 last_stripe_index
+= geo
->stride
>> geo
->chunk_shift
;
1871 end_disk_offset
+= geo
->stride
;
1872 atomic_inc(&first_r10bio
->remaining
);
1873 raid_end_discard_bio(r10_bio
);
1874 wait_barrier(conf
, false);
1878 raid_end_discard_bio(r10_bio
);
1882 allow_barrier(conf
);
1886 static bool raid10_make_request(struct mddev
*mddev
, struct bio
*bio
)
1888 struct r10conf
*conf
= mddev
->private;
1889 sector_t chunk_mask
= (conf
->geo
.chunk_mask
& conf
->prev
.chunk_mask
);
1890 int chunk_sects
= chunk_mask
+ 1;
1891 int sectors
= bio_sectors(bio
);
1893 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)
1894 && md_flush_request(mddev
, bio
))
1897 md_write_start(mddev
, bio
);
1899 if (unlikely(bio_op(bio
) == REQ_OP_DISCARD
))
1900 if (!raid10_handle_discard(mddev
, bio
))
1904 * If this request crosses a chunk boundary, we need to split
1907 if (unlikely((bio
->bi_iter
.bi_sector
& chunk_mask
) +
1908 sectors
> chunk_sects
1909 && (conf
->geo
.near_copies
< conf
->geo
.raid_disks
1910 || conf
->prev
.near_copies
<
1911 conf
->prev
.raid_disks
)))
1912 sectors
= chunk_sects
-
1913 (bio
->bi_iter
.bi_sector
&
1915 __make_request(mddev
, bio
, sectors
);
1917 /* In case raid10d snuck in to freeze_array */
1918 wake_up_barrier(conf
);
1922 static void raid10_status(struct seq_file
*seq
, struct mddev
*mddev
)
1924 struct r10conf
*conf
= mddev
->private;
1927 lockdep_assert_held(&mddev
->lock
);
1929 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
)
1930 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
1931 if (conf
->geo
.near_copies
> 1)
1932 seq_printf(seq
, " %d near-copies", conf
->geo
.near_copies
);
1933 if (conf
->geo
.far_copies
> 1) {
1934 if (conf
->geo
.far_offset
)
1935 seq_printf(seq
, " %d offset-copies", conf
->geo
.far_copies
);
1937 seq_printf(seq
, " %d far-copies", conf
->geo
.far_copies
);
1938 if (conf
->geo
.far_set_size
!= conf
->geo
.raid_disks
)
1939 seq_printf(seq
, " %d devices per set", conf
->geo
.far_set_size
);
1941 seq_printf(seq
, " [%d/%d] [", conf
->geo
.raid_disks
,
1942 conf
->geo
.raid_disks
- mddev
->degraded
);
1943 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1944 struct md_rdev
*rdev
= READ_ONCE(conf
->mirrors
[i
].rdev
);
1946 seq_printf(seq
, "%s", rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
1948 seq_printf(seq
, "]");
1951 /* check if there are enough drives for
1952 * every block to appear on atleast one.
1953 * Don't consider the device numbered 'ignore'
1954 * as we might be about to remove it.
1956 static int _enough(struct r10conf
*conf
, int previous
, int ignore
)
1962 disks
= conf
->prev
.raid_disks
;
1963 ncopies
= conf
->prev
.near_copies
;
1965 disks
= conf
->geo
.raid_disks
;
1966 ncopies
= conf
->geo
.near_copies
;
1970 int n
= conf
->copies
;
1974 struct md_rdev
*rdev
;
1975 if (this != ignore
&&
1976 (rdev
= conf
->mirrors
[this].rdev
) &&
1977 test_bit(In_sync
, &rdev
->flags
))
1979 this = (this+1) % disks
;
1983 first
= (first
+ ncopies
) % disks
;
1984 } while (first
!= 0);
1990 static int enough(struct r10conf
*conf
, int ignore
)
1992 /* when calling 'enough', both 'prev' and 'geo' must
1994 * This is ensured if ->reconfig_mutex or ->device_lock
1997 return _enough(conf
, 0, ignore
) &&
1998 _enough(conf
, 1, ignore
);
2002 * raid10_error() - RAID10 error handler.
2003 * @mddev: affected md device.
2004 * @rdev: member device to fail.
2006 * The routine acknowledges &rdev failure and determines new @mddev state.
2007 * If it failed, then:
2008 * - &MD_BROKEN flag is set in &mddev->flags.
2009 * Otherwise, it must be degraded:
2010 * - recovery is interrupted.
2011 * - &mddev->degraded is bumped.
2013 * @rdev is marked as &Faulty excluding case when array is failed and
2014 * &mddev->fail_last_dev is off.
2016 static void raid10_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
2018 struct r10conf
*conf
= mddev
->private;
2019 unsigned long flags
;
2021 spin_lock_irqsave(&conf
->device_lock
, flags
);
2023 if (test_bit(In_sync
, &rdev
->flags
) && !enough(conf
, rdev
->raid_disk
)) {
2024 set_bit(MD_BROKEN
, &mddev
->flags
);
2026 if (!mddev
->fail_last_dev
) {
2027 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2031 if (test_and_clear_bit(In_sync
, &rdev
->flags
))
2034 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
2035 set_bit(Blocked
, &rdev
->flags
);
2036 set_bit(Faulty
, &rdev
->flags
);
2037 set_mask_bits(&mddev
->sb_flags
, 0,
2038 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
2039 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2040 pr_crit("md/raid10:%s: Disk failure on %pg, disabling device.\n"
2041 "md/raid10:%s: Operation continuing on %d devices.\n",
2042 mdname(mddev
), rdev
->bdev
,
2043 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
);
2046 static void print_conf(struct r10conf
*conf
)
2049 struct md_rdev
*rdev
;
2051 pr_debug("RAID10 conf printout:\n");
2053 pr_debug("(!conf)\n");
2056 pr_debug(" --- wd:%d rd:%d\n", conf
->geo
.raid_disks
- conf
->mddev
->degraded
,
2057 conf
->geo
.raid_disks
);
2059 lockdep_assert_held(&conf
->mddev
->reconfig_mutex
);
2060 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2061 rdev
= conf
->mirrors
[i
].rdev
;
2063 pr_debug(" disk %d, wo:%d, o:%d, dev:%pg\n",
2064 i
, !test_bit(In_sync
, &rdev
->flags
),
2065 !test_bit(Faulty
, &rdev
->flags
),
2070 static void close_sync(struct r10conf
*conf
)
2072 wait_barrier(conf
, false);
2073 allow_barrier(conf
);
2075 mempool_exit(&conf
->r10buf_pool
);
2078 static int raid10_spare_active(struct mddev
*mddev
)
2081 struct r10conf
*conf
= mddev
->private;
2082 struct raid10_info
*tmp
;
2084 unsigned long flags
;
2087 * Find all non-in_sync disks within the RAID10 configuration
2088 * and mark them in_sync
2090 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2091 tmp
= conf
->mirrors
+ i
;
2092 if (tmp
->replacement
2093 && tmp
->replacement
->recovery_offset
== MaxSector
2094 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
2095 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
2096 /* Replacement has just become active */
2098 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
2101 /* Replaced device not technically faulty,
2102 * but we need to be sure it gets removed
2103 * and never re-added.
2105 set_bit(Faulty
, &tmp
->rdev
->flags
);
2106 sysfs_notify_dirent_safe(
2107 tmp
->rdev
->sysfs_state
);
2109 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
2110 } else if (tmp
->rdev
2111 && tmp
->rdev
->recovery_offset
== MaxSector
2112 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
2113 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
2115 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
2118 spin_lock_irqsave(&conf
->device_lock
, flags
);
2119 mddev
->degraded
-= count
;
2120 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2126 static int raid10_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
2128 struct r10conf
*conf
= mddev
->private;
2130 int mirror
, repl_slot
= -1;
2132 int last
= conf
->geo
.raid_disks
- 1;
2133 struct raid10_info
*p
;
2135 if (mddev
->recovery_cp
< MaxSector
)
2136 /* only hot-add to in-sync arrays, as recovery is
2137 * very different from resync
2140 if (rdev
->saved_raid_disk
< 0 && !_enough(conf
, 1, -1))
2143 if (rdev
->raid_disk
>= 0)
2144 first
= last
= rdev
->raid_disk
;
2146 if (rdev
->saved_raid_disk
>= first
&&
2147 rdev
->saved_raid_disk
< conf
->geo
.raid_disks
&&
2148 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
2149 mirror
= rdev
->saved_raid_disk
;
2152 for ( ; mirror
<= last
; mirror
++) {
2153 p
= &conf
->mirrors
[mirror
];
2154 if (p
->recovery_disabled
== mddev
->recovery_disabled
)
2157 if (test_bit(WantReplacement
, &p
->rdev
->flags
) &&
2158 p
->replacement
== NULL
&& repl_slot
< 0)
2163 err
= mddev_stack_new_rdev(mddev
, rdev
);
2166 p
->head_position
= 0;
2167 p
->recovery_disabled
= mddev
->recovery_disabled
- 1;
2168 rdev
->raid_disk
= mirror
;
2170 if (rdev
->saved_raid_disk
!= mirror
)
2172 WRITE_ONCE(p
->rdev
, rdev
);
2176 if (err
&& repl_slot
>= 0) {
2177 p
= &conf
->mirrors
[repl_slot
];
2178 clear_bit(In_sync
, &rdev
->flags
);
2179 set_bit(Replacement
, &rdev
->flags
);
2180 rdev
->raid_disk
= repl_slot
;
2181 err
= mddev_stack_new_rdev(mddev
, rdev
);
2185 WRITE_ONCE(p
->replacement
, rdev
);
2192 static int raid10_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
2194 struct r10conf
*conf
= mddev
->private;
2196 int number
= rdev
->raid_disk
;
2197 struct md_rdev
**rdevp
;
2198 struct raid10_info
*p
;
2201 if (unlikely(number
>= mddev
->raid_disks
))
2203 p
= conf
->mirrors
+ number
;
2204 if (rdev
== p
->rdev
)
2206 else if (rdev
== p
->replacement
)
2207 rdevp
= &p
->replacement
;
2211 if (test_bit(In_sync
, &rdev
->flags
) ||
2212 atomic_read(&rdev
->nr_pending
)) {
2216 /* Only remove non-faulty devices if recovery
2219 if (!test_bit(Faulty
, &rdev
->flags
) &&
2220 mddev
->recovery_disabled
!= p
->recovery_disabled
&&
2221 (!p
->replacement
|| p
->replacement
== rdev
) &&
2222 number
< conf
->geo
.raid_disks
&&
2227 WRITE_ONCE(*rdevp
, NULL
);
2228 if (p
->replacement
) {
2229 /* We must have just cleared 'rdev' */
2230 WRITE_ONCE(p
->rdev
, p
->replacement
);
2231 clear_bit(Replacement
, &p
->replacement
->flags
);
2232 WRITE_ONCE(p
->replacement
, NULL
);
2235 clear_bit(WantReplacement
, &rdev
->flags
);
2236 err
= md_integrity_register(mddev
);
2244 static void __end_sync_read(struct r10bio
*r10_bio
, struct bio
*bio
, int d
)
2246 struct r10conf
*conf
= r10_bio
->mddev
->private;
2248 if (!bio
->bi_status
)
2249 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
2251 /* The write handler will notice the lack of
2252 * R10BIO_Uptodate and record any errors etc
2254 atomic_add(r10_bio
->sectors
,
2255 &conf
->mirrors
[d
].rdev
->corrected_errors
);
2257 /* for reconstruct, we always reschedule after a read.
2258 * for resync, only after all reads
2260 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
2261 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
2262 atomic_dec_and_test(&r10_bio
->remaining
)) {
2263 /* we have read all the blocks,
2264 * do the comparison in process context in raid10d
2266 reschedule_retry(r10_bio
);
2270 static void end_sync_read(struct bio
*bio
)
2272 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
2273 struct r10conf
*conf
= r10_bio
->mddev
->private;
2274 int d
= find_bio_disk(conf
, r10_bio
, bio
, NULL
, NULL
);
2276 __end_sync_read(r10_bio
, bio
, d
);
2279 static void end_reshape_read(struct bio
*bio
)
2281 /* reshape read bio isn't allocated from r10buf_pool */
2282 struct r10bio
*r10_bio
= bio
->bi_private
;
2284 __end_sync_read(r10_bio
, bio
, r10_bio
->read_slot
);
2287 static void end_sync_request(struct r10bio
*r10_bio
)
2289 struct mddev
*mddev
= r10_bio
->mddev
;
2291 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
2292 if (r10_bio
->master_bio
== NULL
) {
2293 /* the primary of several recovery bios */
2294 sector_t s
= r10_bio
->sectors
;
2295 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
2296 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
2297 reschedule_retry(r10_bio
);
2300 md_done_sync(mddev
, s
, 1);
2303 struct r10bio
*r10_bio2
= (struct r10bio
*)r10_bio
->master_bio
;
2304 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
2305 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
2306 reschedule_retry(r10_bio
);
2314 static void end_sync_write(struct bio
*bio
)
2316 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
2317 struct mddev
*mddev
= r10_bio
->mddev
;
2318 struct r10conf
*conf
= mddev
->private;
2322 struct md_rdev
*rdev
= NULL
;
2324 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
2326 rdev
= conf
->mirrors
[d
].replacement
;
2328 rdev
= conf
->mirrors
[d
].rdev
;
2330 if (bio
->bi_status
) {
2332 md_error(mddev
, rdev
);
2334 set_bit(WriteErrorSeen
, &rdev
->flags
);
2335 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2336 set_bit(MD_RECOVERY_NEEDED
,
2337 &rdev
->mddev
->recovery
);
2338 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
2340 } else if (rdev_has_badblock(rdev
, r10_bio
->devs
[slot
].addr
,
2341 r10_bio
->sectors
)) {
2342 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
2345 rdev_dec_pending(rdev
, mddev
);
2347 end_sync_request(r10_bio
);
2351 * Note: sync and recover and handled very differently for raid10
2352 * This code is for resync.
2353 * For resync, we read through virtual addresses and read all blocks.
2354 * If there is any error, we schedule a write. The lowest numbered
2355 * drive is authoritative.
2356 * However requests come for physical address, so we need to map.
2357 * For every physical address there are raid_disks/copies virtual addresses,
2358 * which is always are least one, but is not necessarly an integer.
2359 * This means that a physical address can span multiple chunks, so we may
2360 * have to submit multiple io requests for a single sync request.
2363 * We check if all blocks are in-sync and only write to blocks that
2366 static void sync_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2368 struct r10conf
*conf
= mddev
->private;
2370 struct bio
*tbio
, *fbio
;
2372 struct page
**tpages
, **fpages
;
2374 atomic_set(&r10_bio
->remaining
, 1);
2376 /* find the first device with a block */
2377 for (i
=0; i
<conf
->copies
; i
++)
2378 if (!r10_bio
->devs
[i
].bio
->bi_status
)
2381 if (i
== conf
->copies
)
2385 fbio
= r10_bio
->devs
[i
].bio
;
2386 fbio
->bi_iter
.bi_size
= r10_bio
->sectors
<< 9;
2387 fbio
->bi_iter
.bi_idx
= 0;
2388 fpages
= get_resync_pages(fbio
)->pages
;
2390 vcnt
= (r10_bio
->sectors
+ (PAGE_SIZE
>> 9) - 1) >> (PAGE_SHIFT
- 9);
2391 /* now find blocks with errors */
2392 for (i
=0 ; i
< conf
->copies
; i
++) {
2394 struct md_rdev
*rdev
;
2395 struct resync_pages
*rp
;
2397 tbio
= r10_bio
->devs
[i
].bio
;
2399 if (tbio
->bi_end_io
!= end_sync_read
)
2404 tpages
= get_resync_pages(tbio
)->pages
;
2405 d
= r10_bio
->devs
[i
].devnum
;
2406 rdev
= conf
->mirrors
[d
].rdev
;
2407 if (!r10_bio
->devs
[i
].bio
->bi_status
) {
2408 /* We know that the bi_io_vec layout is the same for
2409 * both 'first' and 'i', so we just compare them.
2410 * All vec entries are PAGE_SIZE;
2412 int sectors
= r10_bio
->sectors
;
2413 for (j
= 0; j
< vcnt
; j
++) {
2414 int len
= PAGE_SIZE
;
2415 if (sectors
< (len
/ 512))
2416 len
= sectors
* 512;
2417 if (memcmp(page_address(fpages
[j
]),
2418 page_address(tpages
[j
]),
2425 atomic64_add(r10_bio
->sectors
, &mddev
->resync_mismatches
);
2426 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2427 /* Don't fix anything. */
2429 } else if (test_bit(FailFast
, &rdev
->flags
)) {
2430 /* Just give up on this device */
2431 md_error(rdev
->mddev
, rdev
);
2434 /* Ok, we need to write this bio, either to correct an
2435 * inconsistency or to correct an unreadable block.
2436 * First we need to fixup bv_offset, bv_len and
2437 * bi_vecs, as the read request might have corrupted these
2439 rp
= get_resync_pages(tbio
);
2440 bio_reset(tbio
, conf
->mirrors
[d
].rdev
->bdev
, REQ_OP_WRITE
);
2442 md_bio_reset_resync_pages(tbio
, rp
, fbio
->bi_iter
.bi_size
);
2444 rp
->raid_bio
= r10_bio
;
2445 tbio
->bi_private
= rp
;
2446 tbio
->bi_iter
.bi_sector
= r10_bio
->devs
[i
].addr
;
2447 tbio
->bi_end_io
= end_sync_write
;
2449 bio_copy_data(tbio
, fbio
);
2451 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2452 atomic_inc(&r10_bio
->remaining
);
2453 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, bio_sectors(tbio
));
2455 if (test_bit(FailFast
, &conf
->mirrors
[d
].rdev
->flags
))
2456 tbio
->bi_opf
|= MD_FAILFAST
;
2457 tbio
->bi_iter
.bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
2458 submit_bio_noacct(tbio
);
2461 /* Now write out to any replacement devices
2464 for (i
= 0; i
< conf
->copies
; i
++) {
2467 tbio
= r10_bio
->devs
[i
].repl_bio
;
2468 if (!tbio
|| !tbio
->bi_end_io
)
2470 if (r10_bio
->devs
[i
].bio
->bi_end_io
!= end_sync_write
2471 && r10_bio
->devs
[i
].bio
!= fbio
)
2472 bio_copy_data(tbio
, fbio
);
2473 d
= r10_bio
->devs
[i
].devnum
;
2474 atomic_inc(&r10_bio
->remaining
);
2475 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2477 submit_bio_noacct(tbio
);
2481 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
2482 md_done_sync(mddev
, r10_bio
->sectors
, 1);
2488 * Now for the recovery code.
2489 * Recovery happens across physical sectors.
2490 * We recover all non-is_sync drives by finding the virtual address of
2491 * each, and then choose a working drive that also has that virt address.
2492 * There is a separate r10_bio for each non-in_sync drive.
2493 * Only the first two slots are in use. The first for reading,
2494 * The second for writing.
2497 static void fix_recovery_read_error(struct r10bio
*r10_bio
)
2499 /* We got a read error during recovery.
2500 * We repeat the read in smaller page-sized sections.
2501 * If a read succeeds, write it to the new device or record
2502 * a bad block if we cannot.
2503 * If a read fails, record a bad block on both old and
2506 struct mddev
*mddev
= r10_bio
->mddev
;
2507 struct r10conf
*conf
= mddev
->private;
2508 struct bio
*bio
= r10_bio
->devs
[0].bio
;
2510 int sectors
= r10_bio
->sectors
;
2512 int dr
= r10_bio
->devs
[0].devnum
;
2513 int dw
= r10_bio
->devs
[1].devnum
;
2514 struct page
**pages
= get_resync_pages(bio
)->pages
;
2518 struct md_rdev
*rdev
;
2522 if (s
> (PAGE_SIZE
>>9))
2525 rdev
= conf
->mirrors
[dr
].rdev
;
2526 addr
= r10_bio
->devs
[0].addr
+ sect
;
2527 ok
= sync_page_io(rdev
,
2531 REQ_OP_READ
, false);
2533 rdev
= conf
->mirrors
[dw
].rdev
;
2534 addr
= r10_bio
->devs
[1].addr
+ sect
;
2535 ok
= sync_page_io(rdev
,
2539 REQ_OP_WRITE
, false);
2541 set_bit(WriteErrorSeen
, &rdev
->flags
);
2542 if (!test_and_set_bit(WantReplacement
,
2544 set_bit(MD_RECOVERY_NEEDED
,
2545 &rdev
->mddev
->recovery
);
2549 /* We don't worry if we cannot set a bad block -
2550 * it really is bad so there is no loss in not
2553 rdev_set_badblocks(rdev
, addr
, s
, 0);
2555 if (rdev
!= conf
->mirrors
[dw
].rdev
) {
2556 /* need bad block on destination too */
2557 struct md_rdev
*rdev2
= conf
->mirrors
[dw
].rdev
;
2558 addr
= r10_bio
->devs
[1].addr
+ sect
;
2559 ok
= rdev_set_badblocks(rdev2
, addr
, s
, 0);
2561 /* just abort the recovery */
2562 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2565 conf
->mirrors
[dw
].recovery_disabled
2566 = mddev
->recovery_disabled
;
2567 set_bit(MD_RECOVERY_INTR
,
2580 static void recovery_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2582 struct r10conf
*conf
= mddev
->private;
2584 struct bio
*wbio
= r10_bio
->devs
[1].bio
;
2585 struct bio
*wbio2
= r10_bio
->devs
[1].repl_bio
;
2587 /* Need to test wbio2->bi_end_io before we call
2588 * submit_bio_noacct as if the former is NULL,
2589 * the latter is free to free wbio2.
2591 if (wbio2
&& !wbio2
->bi_end_io
)
2594 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
)) {
2595 fix_recovery_read_error(r10_bio
);
2596 if (wbio
->bi_end_io
)
2597 end_sync_request(r10_bio
);
2599 end_sync_request(r10_bio
);
2604 * share the pages with the first bio
2605 * and submit the write request
2607 d
= r10_bio
->devs
[1].devnum
;
2608 if (wbio
->bi_end_io
) {
2609 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2610 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, bio_sectors(wbio
));
2611 submit_bio_noacct(wbio
);
2614 atomic_inc(&conf
->mirrors
[d
].replacement
->nr_pending
);
2615 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2616 bio_sectors(wbio2
));
2617 submit_bio_noacct(wbio2
);
2621 static int r10_sync_page_io(struct md_rdev
*rdev
, sector_t sector
,
2622 int sectors
, struct page
*page
, enum req_op op
)
2624 if (rdev_has_badblock(rdev
, sector
, sectors
) &&
2625 (op
== REQ_OP_READ
|| test_bit(WriteErrorSeen
, &rdev
->flags
)))
2627 if (sync_page_io(rdev
, sector
, sectors
<< 9, page
, op
, false))
2630 if (op
== REQ_OP_WRITE
) {
2631 set_bit(WriteErrorSeen
, &rdev
->flags
);
2632 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2633 set_bit(MD_RECOVERY_NEEDED
,
2634 &rdev
->mddev
->recovery
);
2636 /* need to record an error - either for the block or the device */
2637 if (!rdev_set_badblocks(rdev
, sector
, sectors
, 0))
2638 md_error(rdev
->mddev
, rdev
);
2643 * This is a kernel thread which:
2645 * 1. Retries failed read operations on working mirrors.
2646 * 2. Updates the raid superblock when problems encounter.
2647 * 3. Performs writes following reads for array synchronising.
2650 static void fix_read_error(struct r10conf
*conf
, struct mddev
*mddev
, struct r10bio
*r10_bio
)
2652 int sect
= 0; /* Offset from r10_bio->sector */
2653 int sectors
= r10_bio
->sectors
, slot
= r10_bio
->read_slot
;
2654 struct md_rdev
*rdev
;
2655 int d
= r10_bio
->devs
[slot
].devnum
;
2657 /* still own a reference to this rdev, so it cannot
2658 * have been cleared recently.
2660 rdev
= conf
->mirrors
[d
].rdev
;
2662 if (test_bit(Faulty
, &rdev
->flags
))
2663 /* drive has already been failed, just ignore any
2664 more fix_read_error() attempts */
2667 if (exceed_read_errors(mddev
, rdev
)) {
2668 r10_bio
->devs
[slot
].bio
= IO_BLOCKED
;
2678 if (s
> (PAGE_SIZE
>>9))
2682 d
= r10_bio
->devs
[sl
].devnum
;
2683 rdev
= conf
->mirrors
[d
].rdev
;
2685 test_bit(In_sync
, &rdev
->flags
) &&
2686 !test_bit(Faulty
, &rdev
->flags
) &&
2687 rdev_has_badblock(rdev
,
2688 r10_bio
->devs
[sl
].addr
+ sect
,
2690 atomic_inc(&rdev
->nr_pending
);
2691 success
= sync_page_io(rdev
,
2692 r10_bio
->devs
[sl
].addr
+
2696 REQ_OP_READ
, false);
2697 rdev_dec_pending(rdev
, mddev
);
2702 if (sl
== conf
->copies
)
2704 } while (sl
!= slot
);
2707 /* Cannot read from anywhere, just mark the block
2708 * as bad on the first device to discourage future
2711 int dn
= r10_bio
->devs
[slot
].devnum
;
2712 rdev
= conf
->mirrors
[dn
].rdev
;
2714 if (!rdev_set_badblocks(
2716 r10_bio
->devs
[slot
].addr
2719 md_error(mddev
, rdev
);
2720 r10_bio
->devs
[slot
].bio
2727 /* write it back and re-read */
2728 while (sl
!= slot
) {
2732 d
= r10_bio
->devs
[sl
].devnum
;
2733 rdev
= conf
->mirrors
[d
].rdev
;
2735 test_bit(Faulty
, &rdev
->flags
) ||
2736 !test_bit(In_sync
, &rdev
->flags
))
2739 atomic_inc(&rdev
->nr_pending
);
2740 if (r10_sync_page_io(rdev
,
2741 r10_bio
->devs
[sl
].addr
+
2743 s
, conf
->tmppage
, REQ_OP_WRITE
)
2745 /* Well, this device is dead */
2746 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %pg)\n",
2748 (unsigned long long)(
2750 choose_data_offset(r10_bio
,
2753 pr_notice("md/raid10:%s: %pg: failing drive\n",
2757 rdev_dec_pending(rdev
, mddev
);
2760 while (sl
!= slot
) {
2764 d
= r10_bio
->devs
[sl
].devnum
;
2765 rdev
= conf
->mirrors
[d
].rdev
;
2767 test_bit(Faulty
, &rdev
->flags
) ||
2768 !test_bit(In_sync
, &rdev
->flags
))
2771 atomic_inc(&rdev
->nr_pending
);
2772 switch (r10_sync_page_io(rdev
,
2773 r10_bio
->devs
[sl
].addr
+
2775 s
, conf
->tmppage
, REQ_OP_READ
)) {
2777 /* Well, this device is dead */
2778 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %pg)\n",
2780 (unsigned long long)(
2782 choose_data_offset(r10_bio
, rdev
)),
2784 pr_notice("md/raid10:%s: %pg: failing drive\n",
2789 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %pg)\n",
2791 (unsigned long long)(
2793 choose_data_offset(r10_bio
, rdev
)),
2795 atomic_add(s
, &rdev
->corrected_errors
);
2798 rdev_dec_pending(rdev
, mddev
);
2806 static int narrow_write_error(struct r10bio
*r10_bio
, int i
)
2808 struct bio
*bio
= r10_bio
->master_bio
;
2809 struct mddev
*mddev
= r10_bio
->mddev
;
2810 struct r10conf
*conf
= mddev
->private;
2811 struct md_rdev
*rdev
= conf
->mirrors
[r10_bio
->devs
[i
].devnum
].rdev
;
2812 /* bio has the data to be written to slot 'i' where
2813 * we just recently had a write error.
2814 * We repeatedly clone the bio and trim down to one block,
2815 * then try the write. Where the write fails we record
2817 * It is conceivable that the bio doesn't exactly align with
2818 * blocks. We must handle this.
2820 * We currently own a reference to the rdev.
2826 int sect_to_write
= r10_bio
->sectors
;
2829 if (rdev
->badblocks
.shift
< 0)
2832 block_sectors
= roundup(1 << rdev
->badblocks
.shift
,
2833 bdev_logical_block_size(rdev
->bdev
) >> 9);
2834 sector
= r10_bio
->sector
;
2835 sectors
= ((r10_bio
->sector
+ block_sectors
)
2836 & ~(sector_t
)(block_sectors
- 1))
2839 while (sect_to_write
) {
2842 if (sectors
> sect_to_write
)
2843 sectors
= sect_to_write
;
2844 /* Write at 'sector' for 'sectors' */
2845 wbio
= bio_alloc_clone(rdev
->bdev
, bio
, GFP_NOIO
,
2847 bio_trim(wbio
, sector
- bio
->bi_iter
.bi_sector
, sectors
);
2848 wsector
= r10_bio
->devs
[i
].addr
+ (sector
- r10_bio
->sector
);
2849 wbio
->bi_iter
.bi_sector
= wsector
+
2850 choose_data_offset(r10_bio
, rdev
);
2851 wbio
->bi_opf
= REQ_OP_WRITE
;
2853 if (submit_bio_wait(wbio
) < 0)
2855 ok
= rdev_set_badblocks(rdev
, wsector
,
2860 sect_to_write
-= sectors
;
2862 sectors
= block_sectors
;
2867 static void handle_read_error(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2869 int slot
= r10_bio
->read_slot
;
2871 struct r10conf
*conf
= mddev
->private;
2872 struct md_rdev
*rdev
= r10_bio
->devs
[slot
].rdev
;
2874 /* we got a read error. Maybe the drive is bad. Maybe just
2875 * the block and we can fix it.
2876 * We freeze all other IO, and try reading the block from
2877 * other devices. When we find one, we re-write
2878 * and check it that fixes the read error.
2879 * This is all done synchronously while the array is
2882 bio
= r10_bio
->devs
[slot
].bio
;
2884 r10_bio
->devs
[slot
].bio
= NULL
;
2887 r10_bio
->devs
[slot
].bio
= IO_BLOCKED
;
2888 else if (!test_bit(FailFast
, &rdev
->flags
)) {
2889 freeze_array(conf
, 1);
2890 fix_read_error(conf
, mddev
, r10_bio
);
2891 unfreeze_array(conf
);
2893 md_error(mddev
, rdev
);
2895 rdev_dec_pending(rdev
, mddev
);
2897 raid10_read_request(mddev
, r10_bio
->master_bio
, r10_bio
, false);
2899 * allow_barrier after re-submit to ensure no sync io
2900 * can be issued while regular io pending.
2902 allow_barrier(conf
);
2905 static void handle_write_completed(struct r10conf
*conf
, struct r10bio
*r10_bio
)
2907 /* Some sort of write request has finished and it
2908 * succeeded in writing where we thought there was a
2909 * bad block. So forget the bad block.
2910 * Or possibly if failed and we need to record
2914 struct md_rdev
*rdev
;
2916 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
) ||
2917 test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
2918 for (m
= 0; m
< conf
->copies
; m
++) {
2919 int dev
= r10_bio
->devs
[m
].devnum
;
2920 rdev
= conf
->mirrors
[dev
].rdev
;
2921 if (r10_bio
->devs
[m
].bio
== NULL
||
2922 r10_bio
->devs
[m
].bio
->bi_end_io
== NULL
)
2924 if (!r10_bio
->devs
[m
].bio
->bi_status
) {
2925 rdev_clear_badblocks(
2927 r10_bio
->devs
[m
].addr
,
2928 r10_bio
->sectors
, 0);
2930 if (!rdev_set_badblocks(
2932 r10_bio
->devs
[m
].addr
,
2933 r10_bio
->sectors
, 0))
2934 md_error(conf
->mddev
, rdev
);
2936 rdev
= conf
->mirrors
[dev
].replacement
;
2937 if (r10_bio
->devs
[m
].repl_bio
== NULL
||
2938 r10_bio
->devs
[m
].repl_bio
->bi_end_io
== NULL
)
2941 if (!r10_bio
->devs
[m
].repl_bio
->bi_status
) {
2942 rdev_clear_badblocks(
2944 r10_bio
->devs
[m
].addr
,
2945 r10_bio
->sectors
, 0);
2947 if (!rdev_set_badblocks(
2949 r10_bio
->devs
[m
].addr
,
2950 r10_bio
->sectors
, 0))
2951 md_error(conf
->mddev
, rdev
);
2957 for (m
= 0; m
< conf
->copies
; m
++) {
2958 int dev
= r10_bio
->devs
[m
].devnum
;
2959 struct bio
*bio
= r10_bio
->devs
[m
].bio
;
2960 rdev
= conf
->mirrors
[dev
].rdev
;
2961 if (bio
== IO_MADE_GOOD
) {
2962 rdev_clear_badblocks(
2964 r10_bio
->devs
[m
].addr
,
2965 r10_bio
->sectors
, 0);
2966 rdev_dec_pending(rdev
, conf
->mddev
);
2967 } else if (bio
!= NULL
&& bio
->bi_status
) {
2969 if (!narrow_write_error(r10_bio
, m
)) {
2970 md_error(conf
->mddev
, rdev
);
2971 set_bit(R10BIO_Degraded
,
2974 rdev_dec_pending(rdev
, conf
->mddev
);
2976 bio
= r10_bio
->devs
[m
].repl_bio
;
2977 rdev
= conf
->mirrors
[dev
].replacement
;
2978 if (rdev
&& bio
== IO_MADE_GOOD
) {
2979 rdev_clear_badblocks(
2981 r10_bio
->devs
[m
].addr
,
2982 r10_bio
->sectors
, 0);
2983 rdev_dec_pending(rdev
, conf
->mddev
);
2987 spin_lock_irq(&conf
->device_lock
);
2988 list_add(&r10_bio
->retry_list
, &conf
->bio_end_io_list
);
2990 spin_unlock_irq(&conf
->device_lock
);
2992 * In case freeze_array() is waiting for condition
2993 * nr_pending == nr_queued + extra to be true.
2995 wake_up(&conf
->wait_barrier
);
2996 md_wakeup_thread(conf
->mddev
->thread
);
2998 if (test_bit(R10BIO_WriteError
,
3000 close_write(r10_bio
);
3001 raid_end_bio_io(r10_bio
);
3006 static void raid10d(struct md_thread
*thread
)
3008 struct mddev
*mddev
= thread
->mddev
;
3009 struct r10bio
*r10_bio
;
3010 unsigned long flags
;
3011 struct r10conf
*conf
= mddev
->private;
3012 struct list_head
*head
= &conf
->retry_list
;
3013 struct blk_plug plug
;
3015 md_check_recovery(mddev
);
3017 if (!list_empty_careful(&conf
->bio_end_io_list
) &&
3018 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
3020 spin_lock_irqsave(&conf
->device_lock
, flags
);
3021 if (!test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
3022 while (!list_empty(&conf
->bio_end_io_list
)) {
3023 list_move(conf
->bio_end_io_list
.prev
, &tmp
);
3027 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3028 while (!list_empty(&tmp
)) {
3029 r10_bio
= list_first_entry(&tmp
, struct r10bio
,
3031 list_del(&r10_bio
->retry_list
);
3032 if (mddev
->degraded
)
3033 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
3035 if (test_bit(R10BIO_WriteError
,
3037 close_write(r10_bio
);
3038 raid_end_bio_io(r10_bio
);
3042 blk_start_plug(&plug
);
3045 flush_pending_writes(conf
);
3047 spin_lock_irqsave(&conf
->device_lock
, flags
);
3048 if (list_empty(head
)) {
3049 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3052 r10_bio
= list_entry(head
->prev
, struct r10bio
, retry_list
);
3053 list_del(head
->prev
);
3055 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3057 mddev
= r10_bio
->mddev
;
3058 conf
= mddev
->private;
3059 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
3060 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
3061 handle_write_completed(conf
, r10_bio
);
3062 else if (test_bit(R10BIO_IsReshape
, &r10_bio
->state
))
3063 reshape_request_write(mddev
, r10_bio
);
3064 else if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
3065 sync_request_write(mddev
, r10_bio
);
3066 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
3067 recovery_request_write(mddev
, r10_bio
);
3068 else if (test_bit(R10BIO_ReadError
, &r10_bio
->state
))
3069 handle_read_error(mddev
, r10_bio
);
3074 if (mddev
->sb_flags
& ~(1<<MD_SB_CHANGE_PENDING
))
3075 md_check_recovery(mddev
);
3077 blk_finish_plug(&plug
);
3080 static int init_resync(struct r10conf
*conf
)
3084 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
3085 BUG_ON(mempool_initialized(&conf
->r10buf_pool
));
3086 conf
->have_replacement
= 0;
3087 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++)
3088 if (conf
->mirrors
[i
].replacement
)
3089 conf
->have_replacement
= 1;
3090 ret
= mempool_init(&conf
->r10buf_pool
, buffs
,
3091 r10buf_pool_alloc
, r10buf_pool_free
, conf
);
3094 conf
->next_resync
= 0;
3098 static struct r10bio
*raid10_alloc_init_r10buf(struct r10conf
*conf
)
3100 struct r10bio
*r10bio
= mempool_alloc(&conf
->r10buf_pool
, GFP_NOIO
);
3101 struct rsync_pages
*rp
;
3106 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
) ||
3107 test_bit(MD_RECOVERY_RESHAPE
, &conf
->mddev
->recovery
))
3108 nalloc
= conf
->copies
; /* resync */
3110 nalloc
= 2; /* recovery */
3112 for (i
= 0; i
< nalloc
; i
++) {
3113 bio
= r10bio
->devs
[i
].bio
;
3114 rp
= bio
->bi_private
;
3115 bio_reset(bio
, NULL
, 0);
3116 bio
->bi_private
= rp
;
3117 bio
= r10bio
->devs
[i
].repl_bio
;
3119 rp
= bio
->bi_private
;
3120 bio_reset(bio
, NULL
, 0);
3121 bio
->bi_private
= rp
;
3128 * Set cluster_sync_high since we need other nodes to add the
3129 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3131 static void raid10_set_cluster_sync_high(struct r10conf
*conf
)
3133 sector_t window_size
;
3134 int extra_chunk
, chunks
;
3137 * First, here we define "stripe" as a unit which across
3138 * all member devices one time, so we get chunks by use
3139 * raid_disks / near_copies. Otherwise, if near_copies is
3140 * close to raid_disks, then resync window could increases
3141 * linearly with the increase of raid_disks, which means
3142 * we will suspend a really large IO window while it is not
3143 * necessary. If raid_disks is not divisible by near_copies,
3144 * an extra chunk is needed to ensure the whole "stripe" is
3148 chunks
= conf
->geo
.raid_disks
/ conf
->geo
.near_copies
;
3149 if (conf
->geo
.raid_disks
% conf
->geo
.near_copies
== 0)
3153 window_size
= (chunks
+ extra_chunk
) * conf
->mddev
->chunk_sectors
;
3156 * At least use a 32M window to align with raid1's resync window
3158 window_size
= (CLUSTER_RESYNC_WINDOW_SECTORS
> window_size
) ?
3159 CLUSTER_RESYNC_WINDOW_SECTORS
: window_size
;
3161 conf
->cluster_sync_high
= conf
->cluster_sync_low
+ window_size
;
3165 * perform a "sync" on one "block"
3167 * We need to make sure that no normal I/O request - particularly write
3168 * requests - conflict with active sync requests.
3170 * This is achieved by tracking pending requests and a 'barrier' concept
3171 * that can be installed to exclude normal IO requests.
3173 * Resync and recovery are handled very differently.
3174 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3176 * For resync, we iterate over virtual addresses, read all copies,
3177 * and update if there are differences. If only one copy is live,
3179 * For recovery, we iterate over physical addresses, read a good
3180 * value for each non-in_sync drive, and over-write.
3182 * So, for recovery we may have several outstanding complex requests for a
3183 * given address, one for each out-of-sync device. We model this by allocating
3184 * a number of r10_bio structures, one for each out-of-sync device.
3185 * As we setup these structures, we collect all bio's together into a list
3186 * which we then process collectively to add pages, and then process again
3187 * to pass to submit_bio_noacct.
3189 * The r10_bio structures are linked using a borrowed master_bio pointer.
3190 * This link is counted in ->remaining. When the r10_bio that points to NULL
3191 * has its remaining count decremented to 0, the whole complex operation
3196 static sector_t
raid10_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
3197 sector_t max_sector
, int *skipped
)
3199 struct r10conf
*conf
= mddev
->private;
3200 struct r10bio
*r10_bio
;
3201 struct bio
*biolist
= NULL
, *bio
;
3202 sector_t nr_sectors
;
3205 sector_t sync_blocks
;
3206 sector_t sectors_skipped
= 0;
3207 int chunks_skipped
= 0;
3208 sector_t chunk_mask
= conf
->geo
.chunk_mask
;
3210 int error_disk
= -1;
3213 * Allow skipping a full rebuild for incremental assembly
3214 * of a clean array, like RAID1 does.
3216 if (mddev
->bitmap
== NULL
&&
3217 mddev
->recovery_cp
== MaxSector
&&
3218 mddev
->reshape_position
== MaxSector
&&
3219 !test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
3220 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
3221 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
3222 conf
->fullsync
== 0) {
3224 return mddev
->dev_sectors
- sector_nr
;
3227 if (!mempool_initialized(&conf
->r10buf_pool
))
3228 if (init_resync(conf
))
3232 if (sector_nr
>= max_sector
) {
3233 conf
->cluster_sync_low
= 0;
3234 conf
->cluster_sync_high
= 0;
3236 /* If we aborted, we need to abort the
3237 * sync on the 'current' bitmap chucks (there can
3238 * be several when recovering multiple devices).
3239 * as we may have started syncing it but not finished.
3240 * We can find the current address in
3241 * mddev->curr_resync, but for recovery,
3242 * we need to convert that to several
3243 * virtual addresses.
3245 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
3251 if (mddev
->curr_resync
< max_sector
) { /* aborted */
3252 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
3253 mddev
->bitmap_ops
->end_sync(mddev
,
3256 else for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
3258 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
3260 mddev
->bitmap_ops
->end_sync(mddev
, sect
,
3264 /* completed sync */
3265 if ((!mddev
->bitmap
|| conf
->fullsync
)
3266 && conf
->have_replacement
3267 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3268 /* Completed a full sync so the replacements
3269 * are now fully recovered.
3271 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
3272 struct md_rdev
*rdev
=
3273 conf
->mirrors
[i
].replacement
;
3276 rdev
->recovery_offset
= MaxSector
;
3281 mddev
->bitmap_ops
->close_sync(mddev
);
3284 return sectors_skipped
;
3287 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3288 return reshape_request(mddev
, sector_nr
, skipped
);
3290 if (chunks_skipped
>= conf
->geo
.raid_disks
) {
3291 pr_err("md/raid10:%s: %s fails\n", mdname(mddev
),
3292 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ? "resync" : "recovery");
3293 if (error_disk
>= 0 &&
3294 !test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3296 * recovery fails, set mirrors.recovery_disabled,
3297 * device shouldn't be added to there.
3299 conf
->mirrors
[error_disk
].recovery_disabled
=
3300 mddev
->recovery_disabled
;
3304 * if there has been nothing to do on any drive,
3305 * then there is nothing to do at all.
3308 return (max_sector
- sector_nr
) + sectors_skipped
;
3311 if (max_sector
> mddev
->resync_max
)
3312 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
3314 /* make sure whole request will fit in a chunk - if chunks
3317 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
&&
3318 max_sector
> (sector_nr
| chunk_mask
))
3319 max_sector
= (sector_nr
| chunk_mask
) + 1;
3322 * If there is non-resync activity waiting for a turn, then let it
3323 * though before starting on this new sync request.
3325 if (conf
->nr_waiting
)
3326 schedule_timeout_uninterruptible(1);
3328 /* Again, very different code for resync and recovery.
3329 * Both must result in an r10bio with a list of bios that
3330 * have bi_end_io, bi_sector, bi_bdev set,
3331 * and bi_private set to the r10bio.
3332 * For recovery, we may actually create several r10bios
3333 * with 2 bios in each, that correspond to the bios in the main one.
3334 * In this case, the subordinate r10bios link back through a
3335 * borrowed master_bio pointer, and the counter in the master
3336 * includes a ref from each subordinate.
3338 /* First, we decide what to do and set ->bi_end_io
3339 * To end_sync_read if we want to read, and
3340 * end_sync_write if we will want to write.
3343 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
3344 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3345 /* recovery... the complicated one */
3349 for (i
= 0 ; i
< conf
->geo
.raid_disks
; i
++) {
3350 bool still_degraded
;
3355 struct raid10_info
*mirror
= &conf
->mirrors
[i
];
3356 struct md_rdev
*mrdev
, *mreplace
;
3358 mrdev
= mirror
->rdev
;
3359 mreplace
= mirror
->replacement
;
3361 if (mrdev
&& (test_bit(Faulty
, &mrdev
->flags
) ||
3362 test_bit(In_sync
, &mrdev
->flags
)))
3364 if (mreplace
&& test_bit(Faulty
, &mreplace
->flags
))
3367 if (!mrdev
&& !mreplace
)
3370 still_degraded
= false;
3371 /* want to reconstruct this device */
3373 sect
= raid10_find_virt(conf
, sector_nr
, i
);
3374 if (sect
>= mddev
->resync_max_sectors
)
3375 /* last stripe is not complete - don't
3376 * try to recover this sector.
3379 /* Unless we are doing a full sync, or a replacement
3380 * we only need to recover the block if it is set in
3383 must_sync
= mddev
->bitmap_ops
->start_sync(mddev
, sect
,
3386 if (sync_blocks
< max_sync
)
3387 max_sync
= sync_blocks
;
3391 /* yep, skip the sync_blocks here, but don't assume
3392 * that there will never be anything to do here
3394 chunks_skipped
= -1;
3398 atomic_inc(&mrdev
->nr_pending
);
3400 atomic_inc(&mreplace
->nr_pending
);
3402 r10_bio
= raid10_alloc_init_r10buf(conf
);
3404 raise_barrier(conf
, rb2
!= NULL
);
3405 atomic_set(&r10_bio
->remaining
, 0);
3407 r10_bio
->master_bio
= (struct bio
*)rb2
;
3409 atomic_inc(&rb2
->remaining
);
3410 r10_bio
->mddev
= mddev
;
3411 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
3412 r10_bio
->sector
= sect
;
3414 raid10_find_phys(conf
, r10_bio
);
3416 /* Need to check if the array will still be
3419 for (j
= 0; j
< conf
->geo
.raid_disks
; j
++) {
3420 struct md_rdev
*rdev
= conf
->mirrors
[j
].rdev
;
3422 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3423 still_degraded
= false;
3428 must_sync
= mddev
->bitmap_ops
->start_sync(mddev
, sect
,
3429 &sync_blocks
, still_degraded
);
3432 for (j
=0; j
<conf
->copies
;j
++) {
3434 int d
= r10_bio
->devs
[j
].devnum
;
3435 sector_t from_addr
, to_addr
;
3436 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
3437 sector_t sector
, first_bad
;
3440 !test_bit(In_sync
, &rdev
->flags
))
3442 /* This is where we read from */
3444 sector
= r10_bio
->devs
[j
].addr
;
3446 if (is_badblock(rdev
, sector
, max_sync
,
3447 &first_bad
, &bad_sectors
)) {
3448 if (first_bad
> sector
)
3449 max_sync
= first_bad
- sector
;
3451 bad_sectors
-= (sector
3453 if (max_sync
> bad_sectors
)
3454 max_sync
= bad_sectors
;
3458 bio
= r10_bio
->devs
[0].bio
;
3459 bio
->bi_next
= biolist
;
3461 bio
->bi_end_io
= end_sync_read
;
3462 bio
->bi_opf
= REQ_OP_READ
;
3463 if (test_bit(FailFast
, &rdev
->flags
))
3464 bio
->bi_opf
|= MD_FAILFAST
;
3465 from_addr
= r10_bio
->devs
[j
].addr
;
3466 bio
->bi_iter
.bi_sector
= from_addr
+
3468 bio_set_dev(bio
, rdev
->bdev
);
3469 atomic_inc(&rdev
->nr_pending
);
3470 /* and we write to 'i' (if not in_sync) */
3472 for (k
=0; k
<conf
->copies
; k
++)
3473 if (r10_bio
->devs
[k
].devnum
== i
)
3475 BUG_ON(k
== conf
->copies
);
3476 to_addr
= r10_bio
->devs
[k
].addr
;
3477 r10_bio
->devs
[0].devnum
= d
;
3478 r10_bio
->devs
[0].addr
= from_addr
;
3479 r10_bio
->devs
[1].devnum
= i
;
3480 r10_bio
->devs
[1].addr
= to_addr
;
3483 bio
= r10_bio
->devs
[1].bio
;
3484 bio
->bi_next
= biolist
;
3486 bio
->bi_end_io
= end_sync_write
;
3487 bio
->bi_opf
= REQ_OP_WRITE
;
3488 bio
->bi_iter
.bi_sector
= to_addr
3489 + mrdev
->data_offset
;
3490 bio_set_dev(bio
, mrdev
->bdev
);
3491 atomic_inc(&r10_bio
->remaining
);
3493 r10_bio
->devs
[1].bio
->bi_end_io
= NULL
;
3495 /* and maybe write to replacement */
3496 bio
= r10_bio
->devs
[1].repl_bio
;
3498 bio
->bi_end_io
= NULL
;
3499 /* Note: if replace is not NULL, then bio
3500 * cannot be NULL as r10buf_pool_alloc will
3501 * have allocated it.
3505 bio
->bi_next
= biolist
;
3507 bio
->bi_end_io
= end_sync_write
;
3508 bio
->bi_opf
= REQ_OP_WRITE
;
3509 bio
->bi_iter
.bi_sector
= to_addr
+
3510 mreplace
->data_offset
;
3511 bio_set_dev(bio
, mreplace
->bdev
);
3512 atomic_inc(&r10_bio
->remaining
);
3515 if (j
== conf
->copies
) {
3516 /* Cannot recover, so abort the recovery or
3517 * record a bad block */
3519 /* problem is that there are bad blocks
3520 * on other device(s)
3523 for (k
= 0; k
< conf
->copies
; k
++)
3524 if (r10_bio
->devs
[k
].devnum
== i
)
3526 if (mrdev
&& !test_bit(In_sync
,
3528 && !rdev_set_badblocks(
3530 r10_bio
->devs
[k
].addr
,
3534 !rdev_set_badblocks(
3536 r10_bio
->devs
[k
].addr
,
3541 if (!test_and_set_bit(MD_RECOVERY_INTR
,
3543 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3545 mirror
->recovery_disabled
3546 = mddev
->recovery_disabled
;
3552 atomic_dec(&rb2
->remaining
);
3555 rdev_dec_pending(mrdev
, mddev
);
3557 rdev_dec_pending(mreplace
, mddev
);
3561 rdev_dec_pending(mrdev
, mddev
);
3563 rdev_dec_pending(mreplace
, mddev
);
3564 if (r10_bio
->devs
[0].bio
->bi_opf
& MD_FAILFAST
) {
3565 /* Only want this if there is elsewhere to
3566 * read from. 'j' is currently the first
3570 for (; j
< conf
->copies
; j
++) {
3571 int d
= r10_bio
->devs
[j
].devnum
;
3572 if (conf
->mirrors
[d
].rdev
&&
3574 &conf
->mirrors
[d
].rdev
->flags
))
3578 r10_bio
->devs
[0].bio
->bi_opf
3582 if (biolist
== NULL
) {
3584 struct r10bio
*rb2
= r10_bio
;
3585 r10_bio
= (struct r10bio
*) rb2
->master_bio
;
3586 rb2
->master_bio
= NULL
;
3592 /* resync. Schedule a read for every block at this virt offset */
3596 * Since curr_resync_completed could probably not update in
3597 * time, and we will set cluster_sync_low based on it.
3598 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3599 * safety reason, which ensures curr_resync_completed is
3600 * updated in bitmap_cond_end_sync.
3602 mddev
->bitmap_ops
->cond_end_sync(mddev
, sector_nr
,
3603 mddev_is_clustered(mddev
) &&
3604 (sector_nr
+ 2 * RESYNC_SECTORS
> conf
->cluster_sync_high
));
3606 if (!mddev
->bitmap_ops
->start_sync(mddev
, sector_nr
,
3609 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
3610 &mddev
->recovery
)) {
3611 /* We can skip this block */
3613 return sync_blocks
+ sectors_skipped
;
3615 if (sync_blocks
< max_sync
)
3616 max_sync
= sync_blocks
;
3617 r10_bio
= raid10_alloc_init_r10buf(conf
);
3620 r10_bio
->mddev
= mddev
;
3621 atomic_set(&r10_bio
->remaining
, 0);
3622 raise_barrier(conf
, 0);
3623 conf
->next_resync
= sector_nr
;
3625 r10_bio
->master_bio
= NULL
;
3626 r10_bio
->sector
= sector_nr
;
3627 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
3628 raid10_find_phys(conf
, r10_bio
);
3629 r10_bio
->sectors
= (sector_nr
| chunk_mask
) - sector_nr
+ 1;
3631 for (i
= 0; i
< conf
->copies
; i
++) {
3632 int d
= r10_bio
->devs
[i
].devnum
;
3633 sector_t first_bad
, sector
;
3635 struct md_rdev
*rdev
;
3637 if (r10_bio
->devs
[i
].repl_bio
)
3638 r10_bio
->devs
[i
].repl_bio
->bi_end_io
= NULL
;
3640 bio
= r10_bio
->devs
[i
].bio
;
3641 bio
->bi_status
= BLK_STS_IOERR
;
3642 rdev
= conf
->mirrors
[d
].rdev
;
3643 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
))
3646 sector
= r10_bio
->devs
[i
].addr
;
3647 if (is_badblock(rdev
, sector
, max_sync
,
3648 &first_bad
, &bad_sectors
)) {
3649 if (first_bad
> sector
)
3650 max_sync
= first_bad
- sector
;
3652 bad_sectors
-= (sector
- first_bad
);
3653 if (max_sync
> bad_sectors
)
3654 max_sync
= bad_sectors
;
3658 atomic_inc(&rdev
->nr_pending
);
3659 atomic_inc(&r10_bio
->remaining
);
3660 bio
->bi_next
= biolist
;
3662 bio
->bi_end_io
= end_sync_read
;
3663 bio
->bi_opf
= REQ_OP_READ
;
3664 if (test_bit(FailFast
, &rdev
->flags
))
3665 bio
->bi_opf
|= MD_FAILFAST
;
3666 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
3667 bio_set_dev(bio
, rdev
->bdev
);
3670 rdev
= conf
->mirrors
[d
].replacement
;
3671 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
))
3674 atomic_inc(&rdev
->nr_pending
);
3676 /* Need to set up for writing to the replacement */
3677 bio
= r10_bio
->devs
[i
].repl_bio
;
3678 bio
->bi_status
= BLK_STS_IOERR
;
3680 sector
= r10_bio
->devs
[i
].addr
;
3681 bio
->bi_next
= biolist
;
3683 bio
->bi_end_io
= end_sync_write
;
3684 bio
->bi_opf
= REQ_OP_WRITE
;
3685 if (test_bit(FailFast
, &rdev
->flags
))
3686 bio
->bi_opf
|= MD_FAILFAST
;
3687 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
3688 bio_set_dev(bio
, rdev
->bdev
);
3693 for (i
=0; i
<conf
->copies
; i
++) {
3694 int d
= r10_bio
->devs
[i
].devnum
;
3695 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
3696 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
3698 if (r10_bio
->devs
[i
].repl_bio
&&
3699 r10_bio
->devs
[i
].repl_bio
->bi_end_io
)
3701 conf
->mirrors
[d
].replacement
,
3711 if (sector_nr
+ max_sync
< max_sector
)
3712 max_sector
= sector_nr
+ max_sync
;
3715 int len
= PAGE_SIZE
;
3716 if (sector_nr
+ (len
>>9) > max_sector
)
3717 len
= (max_sector
- sector_nr
) << 9;
3720 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
3721 struct resync_pages
*rp
= get_resync_pages(bio
);
3722 page
= resync_fetch_page(rp
, page_idx
);
3723 if (WARN_ON(!bio_add_page(bio
, page
, len
, 0))) {
3724 bio
->bi_status
= BLK_STS_RESOURCE
;
3729 nr_sectors
+= len
>>9;
3730 sector_nr
+= len
>>9;
3731 } while (++page_idx
< RESYNC_PAGES
);
3732 r10_bio
->sectors
= nr_sectors
;
3734 if (mddev_is_clustered(mddev
) &&
3735 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3736 /* It is resync not recovery */
3737 if (conf
->cluster_sync_high
< sector_nr
+ nr_sectors
) {
3738 conf
->cluster_sync_low
= mddev
->curr_resync_completed
;
3739 raid10_set_cluster_sync_high(conf
);
3740 /* Send resync message */
3741 md_cluster_ops
->resync_info_update(mddev
,
3742 conf
->cluster_sync_low
,
3743 conf
->cluster_sync_high
);
3745 } else if (mddev_is_clustered(mddev
)) {
3746 /* This is recovery not resync */
3747 sector_t sect_va1
, sect_va2
;
3748 bool broadcast_msg
= false;
3750 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
3752 * sector_nr is a device address for recovery, so we
3753 * need translate it to array address before compare
3754 * with cluster_sync_high.
3756 sect_va1
= raid10_find_virt(conf
, sector_nr
, i
);
3758 if (conf
->cluster_sync_high
< sect_va1
+ nr_sectors
) {
3759 broadcast_msg
= true;
3761 * curr_resync_completed is similar as
3762 * sector_nr, so make the translation too.
3764 sect_va2
= raid10_find_virt(conf
,
3765 mddev
->curr_resync_completed
, i
);
3767 if (conf
->cluster_sync_low
== 0 ||
3768 conf
->cluster_sync_low
> sect_va2
)
3769 conf
->cluster_sync_low
= sect_va2
;
3772 if (broadcast_msg
) {
3773 raid10_set_cluster_sync_high(conf
);
3774 md_cluster_ops
->resync_info_update(mddev
,
3775 conf
->cluster_sync_low
,
3776 conf
->cluster_sync_high
);
3782 biolist
= biolist
->bi_next
;
3784 bio
->bi_next
= NULL
;
3785 r10_bio
= get_resync_r10bio(bio
);
3786 r10_bio
->sectors
= nr_sectors
;
3788 if (bio
->bi_end_io
== end_sync_read
) {
3789 md_sync_acct_bio(bio
, nr_sectors
);
3791 submit_bio_noacct(bio
);
3795 if (sectors_skipped
)
3796 /* pretend they weren't skipped, it makes
3797 * no important difference in this case
3799 md_done_sync(mddev
, sectors_skipped
, 1);
3801 return sectors_skipped
+ nr_sectors
;
3803 /* There is nowhere to write, so all non-sync
3804 * drives must be failed or in resync, all drives
3805 * have a bad block, so try the next chunk...
3807 if (sector_nr
+ max_sync
< max_sector
)
3808 max_sector
= sector_nr
+ max_sync
;
3810 sectors_skipped
+= (max_sector
- sector_nr
);
3812 sector_nr
= max_sector
;
3817 raid10_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
3820 struct r10conf
*conf
= mddev
->private;
3823 raid_disks
= min(conf
->geo
.raid_disks
,
3824 conf
->prev
.raid_disks
);
3826 sectors
= conf
->dev_sectors
;
3828 size
= sectors
>> conf
->geo
.chunk_shift
;
3829 sector_div(size
, conf
->geo
.far_copies
);
3830 size
= size
* raid_disks
;
3831 sector_div(size
, conf
->geo
.near_copies
);
3833 return size
<< conf
->geo
.chunk_shift
;
3836 static void calc_sectors(struct r10conf
*conf
, sector_t size
)
3838 /* Calculate the number of sectors-per-device that will
3839 * actually be used, and set conf->dev_sectors and
3843 size
= size
>> conf
->geo
.chunk_shift
;
3844 sector_div(size
, conf
->geo
.far_copies
);
3845 size
= size
* conf
->geo
.raid_disks
;
3846 sector_div(size
, conf
->geo
.near_copies
);
3847 /* 'size' is now the number of chunks in the array */
3848 /* calculate "used chunks per device" */
3849 size
= size
* conf
->copies
;
3851 /* We need to round up when dividing by raid_disks to
3852 * get the stride size.
3854 size
= DIV_ROUND_UP_SECTOR_T(size
, conf
->geo
.raid_disks
);
3856 conf
->dev_sectors
= size
<< conf
->geo
.chunk_shift
;
3858 if (conf
->geo
.far_offset
)
3859 conf
->geo
.stride
= 1 << conf
->geo
.chunk_shift
;
3861 sector_div(size
, conf
->geo
.far_copies
);
3862 conf
->geo
.stride
= size
<< conf
->geo
.chunk_shift
;
3866 enum geo_type
{geo_new
, geo_old
, geo_start
};
3867 static int setup_geo(struct geom
*geo
, struct mddev
*mddev
, enum geo_type
new)
3870 int layout
, chunk
, disks
;
3873 layout
= mddev
->layout
;
3874 chunk
= mddev
->chunk_sectors
;
3875 disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3878 layout
= mddev
->new_layout
;
3879 chunk
= mddev
->new_chunk_sectors
;
3880 disks
= mddev
->raid_disks
;
3882 default: /* avoid 'may be unused' warnings */
3883 case geo_start
: /* new when starting reshape - raid_disks not
3885 layout
= mddev
->new_layout
;
3886 chunk
= mddev
->new_chunk_sectors
;
3887 disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
3892 if (chunk
< (PAGE_SIZE
>> 9) ||
3893 !is_power_of_2(chunk
))
3896 fc
= (layout
>> 8) & 255;
3897 fo
= layout
& (1<<16);
3898 geo
->raid_disks
= disks
;
3899 geo
->near_copies
= nc
;
3900 geo
->far_copies
= fc
;
3901 geo
->far_offset
= fo
;
3902 switch (layout
>> 17) {
3903 case 0: /* original layout. simple but not always optimal */
3904 geo
->far_set_size
= disks
;
3906 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3907 * actually using this, but leave code here just in case.*/
3908 geo
->far_set_size
= disks
/fc
;
3909 WARN(geo
->far_set_size
< fc
,
3910 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3912 case 2: /* "improved" layout fixed to match documentation */
3913 geo
->far_set_size
= fc
* nc
;
3915 default: /* Not a valid layout */
3918 geo
->chunk_mask
= chunk
- 1;
3919 geo
->chunk_shift
= ffz(~chunk
);
3923 static void raid10_free_conf(struct r10conf
*conf
)
3928 mempool_exit(&conf
->r10bio_pool
);
3929 kfree(conf
->mirrors
);
3930 kfree(conf
->mirrors_old
);
3931 kfree(conf
->mirrors_new
);
3932 safe_put_page(conf
->tmppage
);
3933 bioset_exit(&conf
->bio_split
);
3937 static struct r10conf
*setup_conf(struct mddev
*mddev
)
3939 struct r10conf
*conf
= NULL
;
3944 copies
= setup_geo(&geo
, mddev
, geo_new
);
3947 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3948 mdname(mddev
), PAGE_SIZE
);
3952 if (copies
< 2 || copies
> mddev
->raid_disks
) {
3953 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3954 mdname(mddev
), mddev
->new_layout
);
3959 conf
= kzalloc(sizeof(struct r10conf
), GFP_KERNEL
);
3963 /* FIXME calc properly */
3964 conf
->mirrors
= kcalloc(mddev
->raid_disks
+ max(0, -mddev
->delta_disks
),
3965 sizeof(struct raid10_info
),
3970 conf
->tmppage
= alloc_page(GFP_KERNEL
);
3975 conf
->copies
= copies
;
3976 err
= mempool_init(&conf
->r10bio_pool
, NR_RAID_BIOS
, r10bio_pool_alloc
,
3977 rbio_pool_free
, conf
);
3981 err
= bioset_init(&conf
->bio_split
, BIO_POOL_SIZE
, 0, 0);
3985 calc_sectors(conf
, mddev
->dev_sectors
);
3986 if (mddev
->reshape_position
== MaxSector
) {
3987 conf
->prev
= conf
->geo
;
3988 conf
->reshape_progress
= MaxSector
;
3990 if (setup_geo(&conf
->prev
, mddev
, geo_old
) != conf
->copies
) {
3994 conf
->reshape_progress
= mddev
->reshape_position
;
3995 if (conf
->prev
.far_offset
)
3996 conf
->prev
.stride
= 1 << conf
->prev
.chunk_shift
;
3998 /* far_copies must be 1 */
3999 conf
->prev
.stride
= conf
->dev_sectors
;
4001 conf
->reshape_safe
= conf
->reshape_progress
;
4002 spin_lock_init(&conf
->device_lock
);
4003 INIT_LIST_HEAD(&conf
->retry_list
);
4004 INIT_LIST_HEAD(&conf
->bio_end_io_list
);
4006 seqlock_init(&conf
->resync_lock
);
4007 init_waitqueue_head(&conf
->wait_barrier
);
4008 atomic_set(&conf
->nr_pending
, 0);
4011 rcu_assign_pointer(conf
->thread
,
4012 md_register_thread(raid10d
, mddev
, "raid10"));
4016 conf
->mddev
= mddev
;
4020 raid10_free_conf(conf
);
4021 return ERR_PTR(err
);
4024 static unsigned int raid10_nr_stripes(struct r10conf
*conf
)
4026 unsigned int raid_disks
= conf
->geo
.raid_disks
;
4028 if (conf
->geo
.raid_disks
% conf
->geo
.near_copies
)
4030 return raid_disks
/ conf
->geo
.near_copies
;
4033 static int raid10_set_queue_limits(struct mddev
*mddev
)
4035 struct r10conf
*conf
= mddev
->private;
4036 struct queue_limits lim
;
4039 md_init_stacking_limits(&lim
);
4040 lim
.max_write_zeroes_sectors
= 0;
4041 lim
.io_min
= mddev
->chunk_sectors
<< 9;
4042 lim
.io_opt
= lim
.io_min
* raid10_nr_stripes(conf
);
4043 lim
.features
|= BLK_FEAT_ATOMIC_WRITES_STACKED
;
4044 err
= mddev_stack_rdev_limits(mddev
, &lim
, MDDEV_STACK_INTEGRITY
);
4046 queue_limits_cancel_update(mddev
->gendisk
->queue
);
4049 return queue_limits_set(mddev
->gendisk
->queue
, &lim
);
4052 static int raid10_run(struct mddev
*mddev
)
4054 struct r10conf
*conf
;
4056 struct raid10_info
*disk
;
4057 struct md_rdev
*rdev
;
4059 sector_t min_offset_diff
= 0;
4063 if (mddev
->private == NULL
) {
4064 conf
= setup_conf(mddev
);
4066 return PTR_ERR(conf
);
4067 mddev
->private = conf
;
4069 conf
= mddev
->private;
4073 rcu_assign_pointer(mddev
->thread
, conf
->thread
);
4074 rcu_assign_pointer(conf
->thread
, NULL
);
4076 if (mddev_is_clustered(conf
->mddev
)) {
4079 fc
= (mddev
->layout
>> 8) & 255;
4080 fo
= mddev
->layout
& (1<<16);
4081 if (fc
> 1 || fo
> 0) {
4082 pr_err("only near layout is supported by clustered"
4088 rdev_for_each(rdev
, mddev
) {
4091 disk_idx
= rdev
->raid_disk
;
4094 if (disk_idx
>= conf
->geo
.raid_disks
&&
4095 disk_idx
>= conf
->prev
.raid_disks
)
4097 disk
= conf
->mirrors
+ disk_idx
;
4099 if (test_bit(Replacement
, &rdev
->flags
)) {
4100 if (disk
->replacement
)
4102 disk
->replacement
= rdev
;
4108 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
4109 if (!mddev
->reshape_backwards
)
4113 if (first
|| diff
< min_offset_diff
)
4114 min_offset_diff
= diff
;
4116 disk
->head_position
= 0;
4120 if (!mddev_is_dm(conf
->mddev
)) {
4121 int err
= raid10_set_queue_limits(mddev
);
4129 /* need to check that every block has at least one working mirror */
4130 if (!enough(conf
, -1)) {
4131 pr_err("md/raid10:%s: not enough operational mirrors.\n",
4136 if (conf
->reshape_progress
!= MaxSector
) {
4137 /* must ensure that shape change is supported */
4138 if (conf
->geo
.far_copies
!= 1 &&
4139 conf
->geo
.far_offset
== 0)
4141 if (conf
->prev
.far_copies
!= 1 &&
4142 conf
->prev
.far_offset
== 0)
4146 mddev
->degraded
= 0;
4148 i
< conf
->geo
.raid_disks
4149 || i
< conf
->prev
.raid_disks
;
4152 disk
= conf
->mirrors
+ i
;
4154 if (!disk
->rdev
&& disk
->replacement
) {
4155 /* The replacement is all we have - use it */
4156 disk
->rdev
= disk
->replacement
;
4157 disk
->replacement
= NULL
;
4158 clear_bit(Replacement
, &disk
->rdev
->flags
);
4162 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
4163 disk
->head_position
= 0;
4166 disk
->rdev
->saved_raid_disk
< 0)
4170 if (disk
->replacement
&&
4171 !test_bit(In_sync
, &disk
->replacement
->flags
) &&
4172 disk
->replacement
->saved_raid_disk
< 0) {
4176 disk
->recovery_disabled
= mddev
->recovery_disabled
- 1;
4179 if (mddev
->recovery_cp
!= MaxSector
)
4180 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4182 pr_info("md/raid10:%s: active with %d out of %d devices\n",
4183 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
,
4184 conf
->geo
.raid_disks
);
4186 * Ok, everything is just fine now
4188 mddev
->dev_sectors
= conf
->dev_sectors
;
4189 size
= raid10_size(mddev
, 0, 0);
4190 md_set_array_sectors(mddev
, size
);
4191 mddev
->resync_max_sectors
= size
;
4192 set_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
);
4194 if (md_integrity_register(mddev
))
4197 if (conf
->reshape_progress
!= MaxSector
) {
4198 unsigned long before_length
, after_length
;
4200 before_length
= ((1 << conf
->prev
.chunk_shift
) *
4201 conf
->prev
.far_copies
);
4202 after_length
= ((1 << conf
->geo
.chunk_shift
) *
4203 conf
->geo
.far_copies
);
4205 if (max(before_length
, after_length
) > min_offset_diff
) {
4206 /* This cannot work */
4207 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
4210 conf
->offset_diff
= min_offset_diff
;
4212 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4213 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4214 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
4215 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4221 md_unregister_thread(mddev
, &mddev
->thread
);
4222 raid10_free_conf(conf
);
4223 mddev
->private = NULL
;
4228 static void raid10_free(struct mddev
*mddev
, void *priv
)
4230 raid10_free_conf(priv
);
4233 static void raid10_quiesce(struct mddev
*mddev
, int quiesce
)
4235 struct r10conf
*conf
= mddev
->private;
4238 raise_barrier(conf
, 0);
4240 lower_barrier(conf
);
4243 static int raid10_resize(struct mddev
*mddev
, sector_t sectors
)
4245 /* Resize of 'far' arrays is not supported.
4246 * For 'near' and 'offset' arrays we can set the
4247 * number of sectors used to be an appropriate multiple
4248 * of the chunk size.
4249 * For 'offset', this is far_copies*chunksize.
4250 * For 'near' the multiplier is the LCM of
4251 * near_copies and raid_disks.
4252 * So if far_copies > 1 && !far_offset, fail.
4253 * Else find LCM(raid_disks, near_copy)*far_copies and
4254 * multiply by chunk_size. Then round to this number.
4255 * This is mostly done by raid10_size()
4257 struct r10conf
*conf
= mddev
->private;
4258 sector_t oldsize
, size
;
4261 if (mddev
->reshape_position
!= MaxSector
)
4264 if (conf
->geo
.far_copies
> 1 && !conf
->geo
.far_offset
)
4267 oldsize
= raid10_size(mddev
, 0, 0);
4268 size
= raid10_size(mddev
, sectors
, 0);
4269 if (mddev
->external_size
&&
4270 mddev
->array_sectors
> size
)
4273 ret
= mddev
->bitmap_ops
->resize(mddev
, size
, 0, false);
4277 md_set_array_sectors(mddev
, size
);
4278 if (sectors
> mddev
->dev_sectors
&&
4279 mddev
->recovery_cp
> oldsize
) {
4280 mddev
->recovery_cp
= oldsize
;
4281 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4283 calc_sectors(conf
, sectors
);
4284 mddev
->dev_sectors
= conf
->dev_sectors
;
4285 mddev
->resync_max_sectors
= size
;
4289 static void *raid10_takeover_raid0(struct mddev
*mddev
, sector_t size
, int devs
)
4291 struct md_rdev
*rdev
;
4292 struct r10conf
*conf
;
4294 if (mddev
->degraded
> 0) {
4295 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4297 return ERR_PTR(-EINVAL
);
4299 sector_div(size
, devs
);
4301 /* Set new parameters */
4302 mddev
->new_level
= 10;
4303 /* new layout: far_copies = 1, near_copies = 2 */
4304 mddev
->new_layout
= (1<<8) + 2;
4305 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4306 mddev
->delta_disks
= mddev
->raid_disks
;
4307 mddev
->raid_disks
*= 2;
4308 /* make sure it will be not marked as dirty */
4309 mddev
->recovery_cp
= MaxSector
;
4310 mddev
->dev_sectors
= size
;
4312 conf
= setup_conf(mddev
);
4313 if (!IS_ERR(conf
)) {
4314 rdev_for_each(rdev
, mddev
)
4315 if (rdev
->raid_disk
>= 0) {
4316 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
4317 rdev
->sectors
= size
;
4324 static void *raid10_takeover(struct mddev
*mddev
)
4326 struct r0conf
*raid0_conf
;
4328 /* raid10 can take over:
4329 * raid0 - providing it has only two drives
4331 if (mddev
->level
== 0) {
4332 /* for raid0 takeover only one zone is supported */
4333 raid0_conf
= mddev
->private;
4334 if (raid0_conf
->nr_strip_zones
> 1) {
4335 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4337 return ERR_PTR(-EINVAL
);
4339 return raid10_takeover_raid0(mddev
,
4340 raid0_conf
->strip_zone
->zone_end
,
4341 raid0_conf
->strip_zone
->nb_dev
);
4343 return ERR_PTR(-EINVAL
);
4346 static int raid10_check_reshape(struct mddev
*mddev
)
4348 /* Called when there is a request to change
4349 * - layout (to ->new_layout)
4350 * - chunk size (to ->new_chunk_sectors)
4351 * - raid_disks (by delta_disks)
4352 * or when trying to restart a reshape that was ongoing.
4354 * We need to validate the request and possibly allocate
4355 * space if that might be an issue later.
4357 * Currently we reject any reshape of a 'far' mode array,
4358 * allow chunk size to change if new is generally acceptable,
4359 * allow raid_disks to increase, and allow
4360 * a switch between 'near' mode and 'offset' mode.
4362 struct r10conf
*conf
= mddev
->private;
4365 if (conf
->geo
.far_copies
!= 1 && !conf
->geo
.far_offset
)
4368 if (setup_geo(&geo
, mddev
, geo_start
) != conf
->copies
)
4369 /* mustn't change number of copies */
4371 if (geo
.far_copies
> 1 && !geo
.far_offset
)
4372 /* Cannot switch to 'far' mode */
4375 if (mddev
->array_sectors
& geo
.chunk_mask
)
4376 /* not factor of array size */
4379 if (!enough(conf
, -1))
4382 kfree(conf
->mirrors_new
);
4383 conf
->mirrors_new
= NULL
;
4384 if (mddev
->delta_disks
> 0) {
4385 /* allocate new 'mirrors' list */
4387 kcalloc(mddev
->raid_disks
+ mddev
->delta_disks
,
4388 sizeof(struct raid10_info
),
4390 if (!conf
->mirrors_new
)
4397 * Need to check if array has failed when deciding whether to:
4399 * - remove non-faulty devices
4402 * This determination is simple when no reshape is happening.
4403 * However if there is a reshape, we need to carefully check
4404 * both the before and after sections.
4405 * This is because some failed devices may only affect one
4406 * of the two sections, and some non-in_sync devices may
4407 * be insync in the section most affected by failed devices.
4409 static int calc_degraded(struct r10conf
*conf
)
4411 int degraded
, degraded2
;
4415 /* 'prev' section first */
4416 for (i
= 0; i
< conf
->prev
.raid_disks
; i
++) {
4417 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
4419 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4421 else if (!test_bit(In_sync
, &rdev
->flags
))
4422 /* When we can reduce the number of devices in
4423 * an array, this might not contribute to
4424 * 'degraded'. It does now.
4428 if (conf
->geo
.raid_disks
== conf
->prev
.raid_disks
)
4431 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
4432 struct md_rdev
*rdev
= conf
->mirrors
[i
].rdev
;
4434 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4436 else if (!test_bit(In_sync
, &rdev
->flags
)) {
4437 /* If reshape is increasing the number of devices,
4438 * this section has already been recovered, so
4439 * it doesn't contribute to degraded.
4442 if (conf
->geo
.raid_disks
<= conf
->prev
.raid_disks
)
4446 if (degraded2
> degraded
)
4451 static int raid10_start_reshape(struct mddev
*mddev
)
4453 /* A 'reshape' has been requested. This commits
4454 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4455 * This also checks if there are enough spares and adds them
4457 * We currently require enough spares to make the final
4458 * array non-degraded. We also require that the difference
4459 * between old and new data_offset - on each device - is
4460 * enough that we never risk over-writing.
4463 unsigned long before_length
, after_length
;
4464 sector_t min_offset_diff
= 0;
4467 struct r10conf
*conf
= mddev
->private;
4468 struct md_rdev
*rdev
;
4472 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4475 if (setup_geo(&new, mddev
, geo_start
) != conf
->copies
)
4478 before_length
= ((1 << conf
->prev
.chunk_shift
) *
4479 conf
->prev
.far_copies
);
4480 after_length
= ((1 << conf
->geo
.chunk_shift
) *
4481 conf
->geo
.far_copies
);
4483 rdev_for_each(rdev
, mddev
) {
4484 if (!test_bit(In_sync
, &rdev
->flags
)
4485 && !test_bit(Faulty
, &rdev
->flags
))
4487 if (rdev
->raid_disk
>= 0) {
4488 long long diff
= (rdev
->new_data_offset
4489 - rdev
->data_offset
);
4490 if (!mddev
->reshape_backwards
)
4494 if (first
|| diff
< min_offset_diff
)
4495 min_offset_diff
= diff
;
4500 if (max(before_length
, after_length
) > min_offset_diff
)
4503 if (spares
< mddev
->delta_disks
)
4506 conf
->offset_diff
= min_offset_diff
;
4507 spin_lock_irq(&conf
->device_lock
);
4508 if (conf
->mirrors_new
) {
4509 memcpy(conf
->mirrors_new
, conf
->mirrors
,
4510 sizeof(struct raid10_info
)*conf
->prev
.raid_disks
);
4512 kfree(conf
->mirrors_old
);
4513 conf
->mirrors_old
= conf
->mirrors
;
4514 conf
->mirrors
= conf
->mirrors_new
;
4515 conf
->mirrors_new
= NULL
;
4517 setup_geo(&conf
->geo
, mddev
, geo_start
);
4519 if (mddev
->reshape_backwards
) {
4520 sector_t size
= raid10_size(mddev
, 0, 0);
4521 if (size
< mddev
->array_sectors
) {
4522 spin_unlock_irq(&conf
->device_lock
);
4523 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4527 mddev
->resync_max_sectors
= size
;
4528 conf
->reshape_progress
= size
;
4530 conf
->reshape_progress
= 0;
4531 conf
->reshape_safe
= conf
->reshape_progress
;
4532 spin_unlock_irq(&conf
->device_lock
);
4534 if (mddev
->delta_disks
&& mddev
->bitmap
) {
4535 struct mdp_superblock_1
*sb
= NULL
;
4536 sector_t oldsize
, newsize
;
4538 oldsize
= raid10_size(mddev
, 0, 0);
4539 newsize
= raid10_size(mddev
, 0, conf
->geo
.raid_disks
);
4541 if (!mddev_is_clustered(mddev
)) {
4542 ret
= mddev
->bitmap_ops
->resize(mddev
, newsize
, 0, false);
4549 rdev_for_each(rdev
, mddev
) {
4550 if (rdev
->raid_disk
> -1 &&
4551 !test_bit(Faulty
, &rdev
->flags
))
4552 sb
= page_address(rdev
->sb_page
);
4556 * some node is already performing reshape, and no need to
4557 * call bitmap_ops->resize again since it should be called when
4558 * receiving BITMAP_RESIZE msg
4560 if ((sb
&& (le32_to_cpu(sb
->feature_map
) &
4561 MD_FEATURE_RESHAPE_ACTIVE
)) || (oldsize
== newsize
))
4564 ret
= mddev
->bitmap_ops
->resize(mddev
, newsize
, 0, false);
4568 ret
= md_cluster_ops
->resize_bitmaps(mddev
, newsize
, oldsize
);
4570 mddev
->bitmap_ops
->resize(mddev
, oldsize
, 0, false);
4575 if (mddev
->delta_disks
> 0) {
4576 rdev_for_each(rdev
, mddev
)
4577 if (rdev
->raid_disk
< 0 &&
4578 !test_bit(Faulty
, &rdev
->flags
)) {
4579 if (raid10_add_disk(mddev
, rdev
) == 0) {
4580 if (rdev
->raid_disk
>=
4581 conf
->prev
.raid_disks
)
4582 set_bit(In_sync
, &rdev
->flags
);
4584 rdev
->recovery_offset
= 0;
4586 /* Failure here is OK */
4587 sysfs_link_rdev(mddev
, rdev
);
4589 } else if (rdev
->raid_disk
>= conf
->prev
.raid_disks
4590 && !test_bit(Faulty
, &rdev
->flags
)) {
4591 /* This is a spare that was manually added */
4592 set_bit(In_sync
, &rdev
->flags
);
4595 /* When a reshape changes the number of devices,
4596 * ->degraded is measured against the larger of the
4597 * pre and post numbers.
4599 spin_lock_irq(&conf
->device_lock
);
4600 mddev
->degraded
= calc_degraded(conf
);
4601 spin_unlock_irq(&conf
->device_lock
);
4602 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4603 mddev
->reshape_position
= conf
->reshape_progress
;
4604 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4606 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4607 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4608 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
4609 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
4610 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4611 conf
->reshape_checkpoint
= jiffies
;
4616 mddev
->recovery
= 0;
4617 spin_lock_irq(&conf
->device_lock
);
4618 conf
->geo
= conf
->prev
;
4619 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4620 rdev_for_each(rdev
, mddev
)
4621 rdev
->new_data_offset
= rdev
->data_offset
;
4623 conf
->reshape_progress
= MaxSector
;
4624 conf
->reshape_safe
= MaxSector
;
4625 mddev
->reshape_position
= MaxSector
;
4626 spin_unlock_irq(&conf
->device_lock
);
4630 /* Calculate the last device-address that could contain
4631 * any block from the chunk that includes the array-address 's'
4632 * and report the next address.
4633 * i.e. the address returned will be chunk-aligned and after
4634 * any data that is in the chunk containing 's'.
4636 static sector_t
last_dev_address(sector_t s
, struct geom
*geo
)
4638 s
= (s
| geo
->chunk_mask
) + 1;
4639 s
>>= geo
->chunk_shift
;
4640 s
*= geo
->near_copies
;
4641 s
= DIV_ROUND_UP_SECTOR_T(s
, geo
->raid_disks
);
4642 s
*= geo
->far_copies
;
4643 s
<<= geo
->chunk_shift
;
4647 /* Calculate the first device-address that could contain
4648 * any block from the chunk that includes the array-address 's'.
4649 * This too will be the start of a chunk
4651 static sector_t
first_dev_address(sector_t s
, struct geom
*geo
)
4653 s
>>= geo
->chunk_shift
;
4654 s
*= geo
->near_copies
;
4655 sector_div(s
, geo
->raid_disks
);
4656 s
*= geo
->far_copies
;
4657 s
<<= geo
->chunk_shift
;
4661 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
4664 /* We simply copy at most one chunk (smallest of old and new)
4665 * at a time, possibly less if that exceeds RESYNC_PAGES,
4666 * or we hit a bad block or something.
4667 * This might mean we pause for normal IO in the middle of
4668 * a chunk, but that is not a problem as mddev->reshape_position
4669 * can record any location.
4671 * If we will want to write to a location that isn't
4672 * yet recorded as 'safe' (i.e. in metadata on disk) then
4673 * we need to flush all reshape requests and update the metadata.
4675 * When reshaping forwards (e.g. to more devices), we interpret
4676 * 'safe' as the earliest block which might not have been copied
4677 * down yet. We divide this by previous stripe size and multiply
4678 * by previous stripe length to get lowest device offset that we
4679 * cannot write to yet.
4680 * We interpret 'sector_nr' as an address that we want to write to.
4681 * From this we use last_device_address() to find where we might
4682 * write to, and first_device_address on the 'safe' position.
4683 * If this 'next' write position is after the 'safe' position,
4684 * we must update the metadata to increase the 'safe' position.
4686 * When reshaping backwards, we round in the opposite direction
4687 * and perform the reverse test: next write position must not be
4688 * less than current safe position.
4690 * In all this the minimum difference in data offsets
4691 * (conf->offset_diff - always positive) allows a bit of slack,
4692 * so next can be after 'safe', but not by more than offset_diff
4694 * We need to prepare all the bios here before we start any IO
4695 * to ensure the size we choose is acceptable to all devices.
4696 * The means one for each copy for write-out and an extra one for
4698 * We store the read-in bio in ->master_bio and the others in
4699 * ->devs[x].bio and ->devs[x].repl_bio.
4701 struct r10conf
*conf
= mddev
->private;
4702 struct r10bio
*r10_bio
;
4703 sector_t next
, safe
, last
;
4707 struct md_rdev
*rdev
;
4710 struct bio
*bio
, *read_bio
;
4711 int sectors_done
= 0;
4712 struct page
**pages
;
4714 if (sector_nr
== 0) {
4715 /* If restarting in the middle, skip the initial sectors */
4716 if (mddev
->reshape_backwards
&&
4717 conf
->reshape_progress
< raid10_size(mddev
, 0, 0)) {
4718 sector_nr
= (raid10_size(mddev
, 0, 0)
4719 - conf
->reshape_progress
);
4720 } else if (!mddev
->reshape_backwards
&&
4721 conf
->reshape_progress
> 0)
4722 sector_nr
= conf
->reshape_progress
;
4724 mddev
->curr_resync_completed
= sector_nr
;
4725 sysfs_notify_dirent_safe(mddev
->sysfs_completed
);
4731 /* We don't use sector_nr to track where we are up to
4732 * as that doesn't work well for ->reshape_backwards.
4733 * So just use ->reshape_progress.
4735 if (mddev
->reshape_backwards
) {
4736 /* 'next' is the earliest device address that we might
4737 * write to for this chunk in the new layout
4739 next
= first_dev_address(conf
->reshape_progress
- 1,
4742 /* 'safe' is the last device address that we might read from
4743 * in the old layout after a restart
4745 safe
= last_dev_address(conf
->reshape_safe
- 1,
4748 if (next
+ conf
->offset_diff
< safe
)
4751 last
= conf
->reshape_progress
- 1;
4752 sector_nr
= last
& ~(sector_t
)(conf
->geo
.chunk_mask
4753 & conf
->prev
.chunk_mask
);
4754 if (sector_nr
+ RESYNC_SECTORS
< last
)
4755 sector_nr
= last
+ 1 - RESYNC_SECTORS
;
4757 /* 'next' is after the last device address that we
4758 * might write to for this chunk in the new layout
4760 next
= last_dev_address(conf
->reshape_progress
, &conf
->geo
);
4762 /* 'safe' is the earliest device address that we might
4763 * read from in the old layout after a restart
4765 safe
= first_dev_address(conf
->reshape_safe
, &conf
->prev
);
4767 /* Need to update metadata if 'next' might be beyond 'safe'
4768 * as that would possibly corrupt data
4770 if (next
> safe
+ conf
->offset_diff
)
4773 sector_nr
= conf
->reshape_progress
;
4774 last
= sector_nr
| (conf
->geo
.chunk_mask
4775 & conf
->prev
.chunk_mask
);
4777 if (sector_nr
+ RESYNC_SECTORS
<= last
)
4778 last
= sector_nr
+ RESYNC_SECTORS
- 1;
4782 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4783 /* Need to update reshape_position in metadata */
4784 wait_barrier(conf
, false);
4785 mddev
->reshape_position
= conf
->reshape_progress
;
4786 if (mddev
->reshape_backwards
)
4787 mddev
->curr_resync_completed
= raid10_size(mddev
, 0, 0)
4788 - conf
->reshape_progress
;
4790 mddev
->curr_resync_completed
= conf
->reshape_progress
;
4791 conf
->reshape_checkpoint
= jiffies
;
4792 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4793 md_wakeup_thread(mddev
->thread
);
4794 wait_event(mddev
->sb_wait
, mddev
->sb_flags
== 0 ||
4795 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
4796 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4797 allow_barrier(conf
);
4798 return sectors_done
;
4800 conf
->reshape_safe
= mddev
->reshape_position
;
4801 allow_barrier(conf
);
4804 raise_barrier(conf
, 0);
4806 /* Now schedule reads for blocks from sector_nr to last */
4807 r10_bio
= raid10_alloc_init_r10buf(conf
);
4809 raise_barrier(conf
, 1);
4810 atomic_set(&r10_bio
->remaining
, 0);
4811 r10_bio
->mddev
= mddev
;
4812 r10_bio
->sector
= sector_nr
;
4813 set_bit(R10BIO_IsReshape
, &r10_bio
->state
);
4814 r10_bio
->sectors
= last
- sector_nr
+ 1;
4815 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
4816 BUG_ON(!test_bit(R10BIO_Previous
, &r10_bio
->state
));
4819 /* Cannot read from here, so need to record bad blocks
4820 * on all the target devices.
4823 mempool_free(r10_bio
, &conf
->r10buf_pool
);
4824 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4825 return sectors_done
;
4828 read_bio
= bio_alloc_bioset(rdev
->bdev
, RESYNC_PAGES
, REQ_OP_READ
,
4829 GFP_KERNEL
, &mddev
->bio_set
);
4830 read_bio
->bi_iter
.bi_sector
= (r10_bio
->devs
[r10_bio
->read_slot
].addr
4831 + rdev
->data_offset
);
4832 read_bio
->bi_private
= r10_bio
;
4833 read_bio
->bi_end_io
= end_reshape_read
;
4834 r10_bio
->master_bio
= read_bio
;
4835 r10_bio
->read_slot
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
4838 * Broadcast RESYNC message to other nodes, so all nodes would not
4839 * write to the region to avoid conflict.
4841 if (mddev_is_clustered(mddev
) && conf
->cluster_sync_high
<= sector_nr
) {
4842 struct mdp_superblock_1
*sb
= NULL
;
4843 int sb_reshape_pos
= 0;
4845 conf
->cluster_sync_low
= sector_nr
;
4846 conf
->cluster_sync_high
= sector_nr
+ CLUSTER_RESYNC_WINDOW_SECTORS
;
4847 sb
= page_address(rdev
->sb_page
);
4849 sb_reshape_pos
= le64_to_cpu(sb
->reshape_position
);
4851 * Set cluster_sync_low again if next address for array
4852 * reshape is less than cluster_sync_low. Since we can't
4853 * update cluster_sync_low until it has finished reshape.
4855 if (sb_reshape_pos
< conf
->cluster_sync_low
)
4856 conf
->cluster_sync_low
= sb_reshape_pos
;
4859 md_cluster_ops
->resync_info_update(mddev
, conf
->cluster_sync_low
,
4860 conf
->cluster_sync_high
);
4863 /* Now find the locations in the new layout */
4864 __raid10_find_phys(&conf
->geo
, r10_bio
);
4867 read_bio
->bi_next
= NULL
;
4869 for (s
= 0; s
< conf
->copies
*2; s
++) {
4871 int d
= r10_bio
->devs
[s
/2].devnum
;
4872 struct md_rdev
*rdev2
;
4874 rdev2
= conf
->mirrors
[d
].replacement
;
4875 b
= r10_bio
->devs
[s
/2].repl_bio
;
4877 rdev2
= conf
->mirrors
[d
].rdev
;
4878 b
= r10_bio
->devs
[s
/2].bio
;
4880 if (!rdev2
|| test_bit(Faulty
, &rdev2
->flags
))
4883 bio_set_dev(b
, rdev2
->bdev
);
4884 b
->bi_iter
.bi_sector
= r10_bio
->devs
[s
/2].addr
+
4885 rdev2
->new_data_offset
;
4886 b
->bi_end_io
= end_reshape_write
;
4887 b
->bi_opf
= REQ_OP_WRITE
;
4892 /* Now add as many pages as possible to all of these bios. */
4895 pages
= get_resync_pages(r10_bio
->devs
[0].bio
)->pages
;
4896 for (s
= 0 ; s
< max_sectors
; s
+= PAGE_SIZE
>> 9) {
4897 struct page
*page
= pages
[s
/ (PAGE_SIZE
>> 9)];
4898 int len
= (max_sectors
- s
) << 9;
4899 if (len
> PAGE_SIZE
)
4901 for (bio
= blist
; bio
; bio
= bio
->bi_next
) {
4902 if (WARN_ON(!bio_add_page(bio
, page
, len
, 0))) {
4903 bio
->bi_status
= BLK_STS_RESOURCE
;
4905 return sectors_done
;
4908 sector_nr
+= len
>> 9;
4909 nr_sectors
+= len
>> 9;
4911 r10_bio
->sectors
= nr_sectors
;
4913 /* Now submit the read */
4914 md_sync_acct_bio(read_bio
, r10_bio
->sectors
);
4915 atomic_inc(&r10_bio
->remaining
);
4916 read_bio
->bi_next
= NULL
;
4917 submit_bio_noacct(read_bio
);
4918 sectors_done
+= nr_sectors
;
4919 if (sector_nr
<= last
)
4922 lower_barrier(conf
);
4924 /* Now that we have done the whole section we can
4925 * update reshape_progress
4927 if (mddev
->reshape_backwards
)
4928 conf
->reshape_progress
-= sectors_done
;
4930 conf
->reshape_progress
+= sectors_done
;
4932 return sectors_done
;
4935 static void end_reshape_request(struct r10bio
*r10_bio
);
4936 static int handle_reshape_read_error(struct mddev
*mddev
,
4937 struct r10bio
*r10_bio
);
4938 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
4940 /* Reshape read completed. Hopefully we have a block
4942 * If we got a read error then we do sync 1-page reads from
4943 * elsewhere until we find the data - or give up.
4945 struct r10conf
*conf
= mddev
->private;
4948 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
4949 if (handle_reshape_read_error(mddev
, r10_bio
) < 0) {
4950 /* Reshape has been aborted */
4951 md_done_sync(mddev
, r10_bio
->sectors
, 0);
4955 /* We definitely have the data in the pages, schedule the
4958 atomic_set(&r10_bio
->remaining
, 1);
4959 for (s
= 0; s
< conf
->copies
*2; s
++) {
4961 int d
= r10_bio
->devs
[s
/2].devnum
;
4962 struct md_rdev
*rdev
;
4964 rdev
= conf
->mirrors
[d
].replacement
;
4965 b
= r10_bio
->devs
[s
/2].repl_bio
;
4967 rdev
= conf
->mirrors
[d
].rdev
;
4968 b
= r10_bio
->devs
[s
/2].bio
;
4970 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4973 atomic_inc(&rdev
->nr_pending
);
4974 md_sync_acct_bio(b
, r10_bio
->sectors
);
4975 atomic_inc(&r10_bio
->remaining
);
4977 submit_bio_noacct(b
);
4979 end_reshape_request(r10_bio
);
4982 static void end_reshape(struct r10conf
*conf
)
4984 if (test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
))
4987 spin_lock_irq(&conf
->device_lock
);
4988 conf
->prev
= conf
->geo
;
4989 md_finish_reshape(conf
->mddev
);
4991 conf
->reshape_progress
= MaxSector
;
4992 conf
->reshape_safe
= MaxSector
;
4993 spin_unlock_irq(&conf
->device_lock
);
4995 mddev_update_io_opt(conf
->mddev
, raid10_nr_stripes(conf
));
4999 static void raid10_update_reshape_pos(struct mddev
*mddev
)
5001 struct r10conf
*conf
= mddev
->private;
5004 md_cluster_ops
->resync_info_get(mddev
, &lo
, &hi
);
5005 if (((mddev
->reshape_position
<= hi
) && (mddev
->reshape_position
>= lo
))
5006 || mddev
->reshape_position
== MaxSector
)
5007 conf
->reshape_progress
= mddev
->reshape_position
;
5012 static int handle_reshape_read_error(struct mddev
*mddev
,
5013 struct r10bio
*r10_bio
)
5015 /* Use sync reads to get the blocks from somewhere else */
5016 int sectors
= r10_bio
->sectors
;
5017 struct r10conf
*conf
= mddev
->private;
5018 struct r10bio
*r10b
;
5021 struct page
**pages
;
5023 r10b
= kmalloc(struct_size(r10b
, devs
, conf
->copies
), GFP_NOIO
);
5025 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
5029 /* reshape IOs share pages from .devs[0].bio */
5030 pages
= get_resync_pages(r10_bio
->devs
[0].bio
)->pages
;
5032 r10b
->sector
= r10_bio
->sector
;
5033 __raid10_find_phys(&conf
->prev
, r10b
);
5038 int first_slot
= slot
;
5040 if (s
> (PAGE_SIZE
>> 9))
5044 int d
= r10b
->devs
[slot
].devnum
;
5045 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
5048 test_bit(Faulty
, &rdev
->flags
) ||
5049 !test_bit(In_sync
, &rdev
->flags
))
5052 addr
= r10b
->devs
[slot
].addr
+ idx
* PAGE_SIZE
;
5053 atomic_inc(&rdev
->nr_pending
);
5054 success
= sync_page_io(rdev
,
5058 REQ_OP_READ
, false);
5059 rdev_dec_pending(rdev
, mddev
);
5064 if (slot
>= conf
->copies
)
5066 if (slot
== first_slot
)
5070 /* couldn't read this block, must give up */
5071 set_bit(MD_RECOVERY_INTR
,
5083 static void end_reshape_write(struct bio
*bio
)
5085 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
5086 struct mddev
*mddev
= r10_bio
->mddev
;
5087 struct r10conf
*conf
= mddev
->private;
5091 struct md_rdev
*rdev
= NULL
;
5093 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
5094 rdev
= repl
? conf
->mirrors
[d
].replacement
:
5095 conf
->mirrors
[d
].rdev
;
5097 if (bio
->bi_status
) {
5098 /* FIXME should record badblock */
5099 md_error(mddev
, rdev
);
5102 rdev_dec_pending(rdev
, mddev
);
5103 end_reshape_request(r10_bio
);
5106 static void end_reshape_request(struct r10bio
*r10_bio
)
5108 if (!atomic_dec_and_test(&r10_bio
->remaining
))
5110 md_done_sync(r10_bio
->mddev
, r10_bio
->sectors
, 1);
5111 bio_put(r10_bio
->master_bio
);
5115 static void raid10_finish_reshape(struct mddev
*mddev
)
5117 struct r10conf
*conf
= mddev
->private;
5119 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
5122 if (mddev
->delta_disks
> 0) {
5123 if (mddev
->recovery_cp
> mddev
->resync_max_sectors
) {
5124 mddev
->recovery_cp
= mddev
->resync_max_sectors
;
5125 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
5127 mddev
->resync_max_sectors
= mddev
->array_sectors
;
5130 for (d
= conf
->geo
.raid_disks
;
5131 d
< conf
->geo
.raid_disks
- mddev
->delta_disks
;
5133 struct md_rdev
*rdev
= conf
->mirrors
[d
].rdev
;
5135 clear_bit(In_sync
, &rdev
->flags
);
5136 rdev
= conf
->mirrors
[d
].replacement
;
5138 clear_bit(In_sync
, &rdev
->flags
);
5141 mddev
->layout
= mddev
->new_layout
;
5142 mddev
->chunk_sectors
= 1 << conf
->geo
.chunk_shift
;
5143 mddev
->reshape_position
= MaxSector
;
5144 mddev
->delta_disks
= 0;
5145 mddev
->reshape_backwards
= 0;
5148 static struct md_personality raid10_personality
=
5152 .owner
= THIS_MODULE
,
5153 .make_request
= raid10_make_request
,
5155 .free
= raid10_free
,
5156 .status
= raid10_status
,
5157 .error_handler
= raid10_error
,
5158 .hot_add_disk
= raid10_add_disk
,
5159 .hot_remove_disk
= raid10_remove_disk
,
5160 .spare_active
= raid10_spare_active
,
5161 .sync_request
= raid10_sync_request
,
5162 .quiesce
= raid10_quiesce
,
5163 .size
= raid10_size
,
5164 .resize
= raid10_resize
,
5165 .takeover
= raid10_takeover
,
5166 .check_reshape
= raid10_check_reshape
,
5167 .start_reshape
= raid10_start_reshape
,
5168 .finish_reshape
= raid10_finish_reshape
,
5169 .update_reshape_pos
= raid10_update_reshape_pos
,
5172 static int __init
raid_init(void)
5174 return register_md_personality(&raid10_personality
);
5177 static void raid_exit(void)
5179 unregister_md_personality(&raid10_personality
);
5182 module_init(raid_init
);
5183 module_exit(raid_exit
);
5184 MODULE_LICENSE("GPL");
5185 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
5186 MODULE_ALIAS("md-personality-9"); /* RAID10 */
5187 MODULE_ALIAS("md-raid10");
5188 MODULE_ALIAS("md-level-10");