2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/module.h>
25 #include <linux/seq_file.h>
26 #include <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include <trace/events/block.h>
35 * RAID10 provides a combination of RAID0 and RAID1 functionality.
36 * The layout of data is defined by
39 * near_copies (stored in low byte of layout)
40 * far_copies (stored in second byte of layout)
41 * far_offset (stored in bit 16 of layout )
42 * use_far_sets (stored in bit 17 of layout )
43 * use_far_sets_bugfixed (stored in bit 18 of layout )
45 * The data to be stored is divided into chunks using chunksize. Each device
46 * is divided into far_copies sections. In each section, chunks are laid out
47 * in a style similar to raid0, but near_copies copies of each chunk is stored
48 * (each on a different drive). The starting device for each section is offset
49 * near_copies from the starting device of the previous section. Thus there
50 * are (near_copies * far_copies) of each chunk, and each is on a different
51 * drive. near_copies and far_copies must be at least one, and their product
52 * is at most raid_disks.
54 * If far_offset is true, then the far_copies are handled a bit differently.
55 * The copies are still in different stripes, but instead of being very far
56 * apart on disk, there are adjacent stripes.
58 * The far and offset algorithms are handled slightly differently if
59 * 'use_far_sets' is true. In this case, the array's devices are grouped into
60 * sets that are (near_copies * far_copies) in size. The far copied stripes
61 * are still shifted by 'near_copies' devices, but this shifting stays confined
62 * to the set rather than the entire array. This is done to improve the number
63 * of device combinations that can fail without causing the array to fail.
64 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
69 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
70 * [A B] [C D] [A B] [C D E]
71 * |...| |...| |...| | ... |
72 * [B A] [D C] [B A] [E C D]
76 * Number of guaranteed r10bios in case of extreme VM load:
78 #define NR_RAID10_BIOS 256
80 /* when we get a read error on a read-only array, we redirect to another
81 * device without failing the first device, or trying to over-write to
82 * correct the read error. To keep track of bad blocks on a per-bio
83 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
85 #define IO_BLOCKED ((struct bio *)1)
86 /* When we successfully write to a known bad-block, we need to remove the
87 * bad-block marking which must be done from process context. So we record
88 * the success by setting devs[n].bio to IO_MADE_GOOD
90 #define IO_MADE_GOOD ((struct bio *)2)
92 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
94 /* When there are this many requests queued to be written by
95 * the raid10 thread, we become 'congested' to provide back-pressure
98 static int max_queued_requests
= 1024;
100 static void allow_barrier(struct r10conf
*conf
);
101 static void lower_barrier(struct r10conf
*conf
);
102 static int _enough(struct r10conf
*conf
, int previous
, int ignore
);
103 static int enough(struct r10conf
*conf
, int ignore
);
104 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
106 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
);
107 static void end_reshape_write(struct bio
*bio
);
108 static void end_reshape(struct r10conf
*conf
);
110 #define raid10_log(md, fmt, args...) \
111 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
113 #include "raid1-10.c"
116 * for resync bio, r10bio pointer can be retrieved from the per-bio
117 * 'struct resync_pages'.
119 static inline struct r10bio
*get_resync_r10bio(struct bio
*bio
)
121 return get_resync_pages(bio
)->raid_bio
;
124 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
126 struct r10conf
*conf
= data
;
127 int size
= offsetof(struct r10bio
, devs
[conf
->copies
]);
129 /* allocate a r10bio with room for raid_disks entries in the
131 return kzalloc(size
, gfp_flags
);
134 static void r10bio_pool_free(void *r10_bio
, void *data
)
139 /* amount of memory to reserve for resync requests */
140 #define RESYNC_WINDOW (1024*1024)
141 /* maximum number of concurrent requests, memory permitting */
142 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
145 * When performing a resync, we need to read and compare, so
146 * we need as many pages are there are copies.
147 * When performing a recovery, we need 2 bios, one for read,
148 * one for write (we recover only one drive per r10buf)
151 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
153 struct r10conf
*conf
= data
;
154 struct r10bio
*r10_bio
;
157 int nalloc
, nalloc_rp
;
158 struct resync_pages
*rps
;
160 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
164 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
) ||
165 test_bit(MD_RECOVERY_RESHAPE
, &conf
->mddev
->recovery
))
166 nalloc
= conf
->copies
; /* resync */
168 nalloc
= 2; /* recovery */
170 /* allocate once for all bios */
171 if (!conf
->have_replacement
)
174 nalloc_rp
= nalloc
* 2;
175 rps
= kmalloc(sizeof(struct resync_pages
) * nalloc_rp
, gfp_flags
);
177 goto out_free_r10bio
;
182 for (j
= nalloc
; j
-- ; ) {
183 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
186 r10_bio
->devs
[j
].bio
= bio
;
187 if (!conf
->have_replacement
)
189 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
192 r10_bio
->devs
[j
].repl_bio
= bio
;
195 * Allocate RESYNC_PAGES data pages and attach them
198 for (j
= 0; j
< nalloc
; j
++) {
199 struct bio
*rbio
= r10_bio
->devs
[j
].repl_bio
;
200 struct resync_pages
*rp
, *rp_repl
;
204 rp_repl
= &rps
[nalloc
+ j
];
206 bio
= r10_bio
->devs
[j
].bio
;
208 if (!j
|| test_bit(MD_RECOVERY_SYNC
,
209 &conf
->mddev
->recovery
)) {
210 if (resync_alloc_pages(rp
, gfp_flags
))
213 memcpy(rp
, &rps
[0], sizeof(*rp
));
214 resync_get_all_pages(rp
);
217 rp
->raid_bio
= r10_bio
;
218 bio
->bi_private
= rp
;
220 memcpy(rp_repl
, rp
, sizeof(*rp
));
221 rbio
->bi_private
= rp_repl
;
229 resync_free_pages(&rps
[j
* 2]);
233 for ( ; j
< nalloc
; j
++) {
234 if (r10_bio
->devs
[j
].bio
)
235 bio_put(r10_bio
->devs
[j
].bio
);
236 if (r10_bio
->devs
[j
].repl_bio
)
237 bio_put(r10_bio
->devs
[j
].repl_bio
);
241 r10bio_pool_free(r10_bio
, conf
);
245 static void r10buf_pool_free(void *__r10_bio
, void *data
)
247 struct r10conf
*conf
= data
;
248 struct r10bio
*r10bio
= __r10_bio
;
250 struct resync_pages
*rp
= NULL
;
252 for (j
= conf
->copies
; j
--; ) {
253 struct bio
*bio
= r10bio
->devs
[j
].bio
;
255 rp
= get_resync_pages(bio
);
256 resync_free_pages(rp
);
259 bio
= r10bio
->devs
[j
].repl_bio
;
264 /* resync pages array stored in the 1st bio's .bi_private */
267 r10bio_pool_free(r10bio
, conf
);
270 static void put_all_bios(struct r10conf
*conf
, struct r10bio
*r10_bio
)
274 for (i
= 0; i
< conf
->copies
; i
++) {
275 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
276 if (!BIO_SPECIAL(*bio
))
279 bio
= &r10_bio
->devs
[i
].repl_bio
;
280 if (r10_bio
->read_slot
< 0 && !BIO_SPECIAL(*bio
))
286 static void free_r10bio(struct r10bio
*r10_bio
)
288 struct r10conf
*conf
= r10_bio
->mddev
->private;
290 put_all_bios(conf
, r10_bio
);
291 mempool_free(r10_bio
, conf
->r10bio_pool
);
294 static void put_buf(struct r10bio
*r10_bio
)
296 struct r10conf
*conf
= r10_bio
->mddev
->private;
298 mempool_free(r10_bio
, conf
->r10buf_pool
);
303 static void reschedule_retry(struct r10bio
*r10_bio
)
306 struct mddev
*mddev
= r10_bio
->mddev
;
307 struct r10conf
*conf
= mddev
->private;
309 spin_lock_irqsave(&conf
->device_lock
, flags
);
310 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
312 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
314 /* wake up frozen array... */
315 wake_up(&conf
->wait_barrier
);
317 md_wakeup_thread(mddev
->thread
);
321 * raid_end_bio_io() is called when we have finished servicing a mirrored
322 * operation and are ready to return a success/failure code to the buffer
325 static void raid_end_bio_io(struct r10bio
*r10_bio
)
327 struct bio
*bio
= r10_bio
->master_bio
;
328 struct r10conf
*conf
= r10_bio
->mddev
->private;
330 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
331 bio
->bi_status
= BLK_STS_IOERR
;
335 * Wake up any possible resync thread that waits for the device
340 free_r10bio(r10_bio
);
344 * Update disk head position estimator based on IRQ completion info.
346 static inline void update_head_pos(int slot
, struct r10bio
*r10_bio
)
348 struct r10conf
*conf
= r10_bio
->mddev
->private;
350 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
351 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
355 * Find the disk number which triggered given bio
357 static int find_bio_disk(struct r10conf
*conf
, struct r10bio
*r10_bio
,
358 struct bio
*bio
, int *slotp
, int *replp
)
363 for (slot
= 0; slot
< conf
->copies
; slot
++) {
364 if (r10_bio
->devs
[slot
].bio
== bio
)
366 if (r10_bio
->devs
[slot
].repl_bio
== bio
) {
372 BUG_ON(slot
== conf
->copies
);
373 update_head_pos(slot
, r10_bio
);
379 return r10_bio
->devs
[slot
].devnum
;
382 static void raid10_end_read_request(struct bio
*bio
)
384 int uptodate
= !bio
->bi_status
;
385 struct r10bio
*r10_bio
= bio
->bi_private
;
387 struct md_rdev
*rdev
;
388 struct r10conf
*conf
= r10_bio
->mddev
->private;
390 slot
= r10_bio
->read_slot
;
391 dev
= r10_bio
->devs
[slot
].devnum
;
392 rdev
= r10_bio
->devs
[slot
].rdev
;
394 * this branch is our 'one mirror IO has finished' event handler:
396 update_head_pos(slot
, r10_bio
);
400 * Set R10BIO_Uptodate in our master bio, so that
401 * we will return a good error code to the higher
402 * levels even if IO on some other mirrored buffer fails.
404 * The 'master' represents the composite IO operation to
405 * user-side. So if something waits for IO, then it will
406 * wait for the 'master' bio.
408 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
410 /* If all other devices that store this block have
411 * failed, we want to return the error upwards rather
412 * than fail the last device. Here we redefine
413 * "uptodate" to mean "Don't want to retry"
415 if (!_enough(conf
, test_bit(R10BIO_Previous
, &r10_bio
->state
),
420 raid_end_bio_io(r10_bio
);
421 rdev_dec_pending(rdev
, conf
->mddev
);
424 * oops, read error - keep the refcount on the rdev
426 char b
[BDEVNAME_SIZE
];
427 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
429 bdevname(rdev
->bdev
, b
),
430 (unsigned long long)r10_bio
->sector
);
431 set_bit(R10BIO_ReadError
, &r10_bio
->state
);
432 reschedule_retry(r10_bio
);
436 static void close_write(struct r10bio
*r10_bio
)
438 /* clear the bitmap if all writes complete successfully */
439 bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
441 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
443 md_write_end(r10_bio
->mddev
);
446 static void one_write_done(struct r10bio
*r10_bio
)
448 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
449 if (test_bit(R10BIO_WriteError
, &r10_bio
->state
))
450 reschedule_retry(r10_bio
);
452 close_write(r10_bio
);
453 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
454 reschedule_retry(r10_bio
);
456 raid_end_bio_io(r10_bio
);
461 static void raid10_end_write_request(struct bio
*bio
)
463 struct r10bio
*r10_bio
= bio
->bi_private
;
466 struct r10conf
*conf
= r10_bio
->mddev
->private;
468 struct md_rdev
*rdev
= NULL
;
469 struct bio
*to_put
= NULL
;
472 discard_error
= bio
->bi_status
&& bio_op(bio
) == REQ_OP_DISCARD
;
474 dev
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
477 rdev
= conf
->mirrors
[dev
].replacement
;
481 rdev
= conf
->mirrors
[dev
].rdev
;
484 * this branch is our 'one mirror IO has finished' event handler:
486 if (bio
->bi_status
&& !discard_error
) {
488 /* Never record new bad blocks to replacement,
491 md_error(rdev
->mddev
, rdev
);
493 set_bit(WriteErrorSeen
, &rdev
->flags
);
494 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
495 set_bit(MD_RECOVERY_NEEDED
,
496 &rdev
->mddev
->recovery
);
499 if (test_bit(FailFast
, &rdev
->flags
) &&
500 (bio
->bi_opf
& MD_FAILFAST
)) {
501 md_error(rdev
->mddev
, rdev
);
502 if (!test_bit(Faulty
, &rdev
->flags
))
503 /* This is the only remaining device,
504 * We need to retry the write without
507 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
509 r10_bio
->devs
[slot
].bio
= NULL
;
514 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
518 * Set R10BIO_Uptodate in our master bio, so that
519 * we will return a good error code for to the higher
520 * levels even if IO on some other mirrored buffer fails.
522 * The 'master' represents the composite IO operation to
523 * user-side. So if something waits for IO, then it will
524 * wait for the 'master' bio.
530 * Do not set R10BIO_Uptodate if the current device is
531 * rebuilding or Faulty. This is because we cannot use
532 * such device for properly reading the data back (we could
533 * potentially use it, if the current write would have felt
534 * before rdev->recovery_offset, but for simplicity we don't
537 if (test_bit(In_sync
, &rdev
->flags
) &&
538 !test_bit(Faulty
, &rdev
->flags
))
539 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
541 /* Maybe we can clear some bad blocks. */
542 if (is_badblock(rdev
,
543 r10_bio
->devs
[slot
].addr
,
545 &first_bad
, &bad_sectors
) && !discard_error
) {
548 r10_bio
->devs
[slot
].repl_bio
= IO_MADE_GOOD
;
550 r10_bio
->devs
[slot
].bio
= IO_MADE_GOOD
;
552 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
558 * Let's see if all mirrored write operations have finished
561 one_write_done(r10_bio
);
563 rdev_dec_pending(rdev
, conf
->mddev
);
569 * RAID10 layout manager
570 * As well as the chunksize and raid_disks count, there are two
571 * parameters: near_copies and far_copies.
572 * near_copies * far_copies must be <= raid_disks.
573 * Normally one of these will be 1.
574 * If both are 1, we get raid0.
575 * If near_copies == raid_disks, we get raid1.
577 * Chunks are laid out in raid0 style with near_copies copies of the
578 * first chunk, followed by near_copies copies of the next chunk and
580 * If far_copies > 1, then after 1/far_copies of the array has been assigned
581 * as described above, we start again with a device offset of near_copies.
582 * So we effectively have another copy of the whole array further down all
583 * the drives, but with blocks on different drives.
584 * With this layout, and block is never stored twice on the one device.
586 * raid10_find_phys finds the sector offset of a given virtual sector
587 * on each device that it is on.
589 * raid10_find_virt does the reverse mapping, from a device and a
590 * sector offset to a virtual address
593 static void __raid10_find_phys(struct geom
*geo
, struct r10bio
*r10bio
)
601 int last_far_set_start
, last_far_set_size
;
603 last_far_set_start
= (geo
->raid_disks
/ geo
->far_set_size
) - 1;
604 last_far_set_start
*= geo
->far_set_size
;
606 last_far_set_size
= geo
->far_set_size
;
607 last_far_set_size
+= (geo
->raid_disks
% geo
->far_set_size
);
609 /* now calculate first sector/dev */
610 chunk
= r10bio
->sector
>> geo
->chunk_shift
;
611 sector
= r10bio
->sector
& geo
->chunk_mask
;
613 chunk
*= geo
->near_copies
;
615 dev
= sector_div(stripe
, geo
->raid_disks
);
617 stripe
*= geo
->far_copies
;
619 sector
+= stripe
<< geo
->chunk_shift
;
621 /* and calculate all the others */
622 for (n
= 0; n
< geo
->near_copies
; n
++) {
626 r10bio
->devs
[slot
].devnum
= d
;
627 r10bio
->devs
[slot
].addr
= s
;
630 for (f
= 1; f
< geo
->far_copies
; f
++) {
631 set
= d
/ geo
->far_set_size
;
632 d
+= geo
->near_copies
;
634 if ((geo
->raid_disks
% geo
->far_set_size
) &&
635 (d
> last_far_set_start
)) {
636 d
-= last_far_set_start
;
637 d
%= last_far_set_size
;
638 d
+= last_far_set_start
;
640 d
%= geo
->far_set_size
;
641 d
+= geo
->far_set_size
* set
;
644 r10bio
->devs
[slot
].devnum
= d
;
645 r10bio
->devs
[slot
].addr
= s
;
649 if (dev
>= geo
->raid_disks
) {
651 sector
+= (geo
->chunk_mask
+ 1);
656 static void raid10_find_phys(struct r10conf
*conf
, struct r10bio
*r10bio
)
658 struct geom
*geo
= &conf
->geo
;
660 if (conf
->reshape_progress
!= MaxSector
&&
661 ((r10bio
->sector
>= conf
->reshape_progress
) !=
662 conf
->mddev
->reshape_backwards
)) {
663 set_bit(R10BIO_Previous
, &r10bio
->state
);
666 clear_bit(R10BIO_Previous
, &r10bio
->state
);
668 __raid10_find_phys(geo
, r10bio
);
671 static sector_t
raid10_find_virt(struct r10conf
*conf
, sector_t sector
, int dev
)
673 sector_t offset
, chunk
, vchunk
;
674 /* Never use conf->prev as this is only called during resync
675 * or recovery, so reshape isn't happening
677 struct geom
*geo
= &conf
->geo
;
678 int far_set_start
= (dev
/ geo
->far_set_size
) * geo
->far_set_size
;
679 int far_set_size
= geo
->far_set_size
;
680 int last_far_set_start
;
682 if (geo
->raid_disks
% geo
->far_set_size
) {
683 last_far_set_start
= (geo
->raid_disks
/ geo
->far_set_size
) - 1;
684 last_far_set_start
*= geo
->far_set_size
;
686 if (dev
>= last_far_set_start
) {
687 far_set_size
= geo
->far_set_size
;
688 far_set_size
+= (geo
->raid_disks
% geo
->far_set_size
);
689 far_set_start
= last_far_set_start
;
693 offset
= sector
& geo
->chunk_mask
;
694 if (geo
->far_offset
) {
696 chunk
= sector
>> geo
->chunk_shift
;
697 fc
= sector_div(chunk
, geo
->far_copies
);
698 dev
-= fc
* geo
->near_copies
;
699 if (dev
< far_set_start
)
702 while (sector
>= geo
->stride
) {
703 sector
-= geo
->stride
;
704 if (dev
< (geo
->near_copies
+ far_set_start
))
705 dev
+= far_set_size
- geo
->near_copies
;
707 dev
-= geo
->near_copies
;
709 chunk
= sector
>> geo
->chunk_shift
;
711 vchunk
= chunk
* geo
->raid_disks
+ dev
;
712 sector_div(vchunk
, geo
->near_copies
);
713 return (vchunk
<< geo
->chunk_shift
) + offset
;
717 * This routine returns the disk from which the requested read should
718 * be done. There is a per-array 'next expected sequential IO' sector
719 * number - if this matches on the next IO then we use the last disk.
720 * There is also a per-disk 'last know head position' sector that is
721 * maintained from IRQ contexts, both the normal and the resync IO
722 * completion handlers update this position correctly. If there is no
723 * perfect sequential match then we pick the disk whose head is closest.
725 * If there are 2 mirrors in the same 2 devices, performance degrades
726 * because position is mirror, not device based.
728 * The rdev for the device selected will have nr_pending incremented.
732 * FIXME: possibly should rethink readbalancing and do it differently
733 * depending on near_copies / far_copies geometry.
735 static struct md_rdev
*read_balance(struct r10conf
*conf
,
736 struct r10bio
*r10_bio
,
739 const sector_t this_sector
= r10_bio
->sector
;
741 int sectors
= r10_bio
->sectors
;
742 int best_good_sectors
;
743 sector_t new_distance
, best_dist
;
744 struct md_rdev
*best_rdev
, *rdev
= NULL
;
747 struct geom
*geo
= &conf
->geo
;
749 raid10_find_phys(conf
, r10_bio
);
751 sectors
= r10_bio
->sectors
;
754 best_dist
= MaxSector
;
755 best_good_sectors
= 0;
757 clear_bit(R10BIO_FailFast
, &r10_bio
->state
);
759 * Check if we can balance. We can balance on the whole
760 * device if no resync is going on (recovery is ok), or below
761 * the resync window. We take the first readable disk when
762 * above the resync window.
764 if (conf
->mddev
->recovery_cp
< MaxSector
765 && (this_sector
+ sectors
>= conf
->next_resync
))
768 for (slot
= 0; slot
< conf
->copies
; slot
++) {
773 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
775 disk
= r10_bio
->devs
[slot
].devnum
;
776 rdev
= rcu_dereference(conf
->mirrors
[disk
].replacement
);
777 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
) ||
778 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
779 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
781 test_bit(Faulty
, &rdev
->flags
))
783 if (!test_bit(In_sync
, &rdev
->flags
) &&
784 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
787 dev_sector
= r10_bio
->devs
[slot
].addr
;
788 if (is_badblock(rdev
, dev_sector
, sectors
,
789 &first_bad
, &bad_sectors
)) {
790 if (best_dist
< MaxSector
)
791 /* Already have a better slot */
793 if (first_bad
<= dev_sector
) {
794 /* Cannot read here. If this is the
795 * 'primary' device, then we must not read
796 * beyond 'bad_sectors' from another device.
798 bad_sectors
-= (dev_sector
- first_bad
);
799 if (!do_balance
&& sectors
> bad_sectors
)
800 sectors
= bad_sectors
;
801 if (best_good_sectors
> sectors
)
802 best_good_sectors
= sectors
;
804 sector_t good_sectors
=
805 first_bad
- dev_sector
;
806 if (good_sectors
> best_good_sectors
) {
807 best_good_sectors
= good_sectors
;
812 /* Must read from here */
817 best_good_sectors
= sectors
;
823 /* At least 2 disks to choose from so failfast is OK */
824 set_bit(R10BIO_FailFast
, &r10_bio
->state
);
825 /* This optimisation is debatable, and completely destroys
826 * sequential read speed for 'far copies' arrays. So only
827 * keep it for 'near' arrays, and review those later.
829 if (geo
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
))
832 /* for far > 1 always use the lowest address */
833 else if (geo
->far_copies
> 1)
834 new_distance
= r10_bio
->devs
[slot
].addr
;
836 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
837 conf
->mirrors
[disk
].head_position
);
838 if (new_distance
< best_dist
) {
839 best_dist
= new_distance
;
844 if (slot
>= conf
->copies
) {
850 atomic_inc(&rdev
->nr_pending
);
851 r10_bio
->read_slot
= slot
;
855 *max_sectors
= best_good_sectors
;
860 static int raid10_congested(struct mddev
*mddev
, int bits
)
862 struct r10conf
*conf
= mddev
->private;
865 if ((bits
& (1 << WB_async_congested
)) &&
866 conf
->pending_count
>= max_queued_requests
)
871 (i
< conf
->geo
.raid_disks
|| i
< conf
->prev
.raid_disks
)
874 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
875 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
876 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
878 ret
|= bdi_congested(q
->backing_dev_info
, bits
);
885 static void flush_pending_writes(struct r10conf
*conf
)
887 /* Any writes that have been queued but are awaiting
888 * bitmap updates get flushed here.
890 spin_lock_irq(&conf
->device_lock
);
892 if (conf
->pending_bio_list
.head
) {
894 bio
= bio_list_get(&conf
->pending_bio_list
);
895 conf
->pending_count
= 0;
896 spin_unlock_irq(&conf
->device_lock
);
897 /* flush any pending bitmap writes to disk
898 * before proceeding w/ I/O */
899 bitmap_unplug(conf
->mddev
->bitmap
);
900 wake_up(&conf
->wait_barrier
);
902 while (bio
) { /* submit pending writes */
903 struct bio
*next
= bio
->bi_next
;
904 struct md_rdev
*rdev
= (void*)bio
->bi_bdev
;
906 bio
->bi_bdev
= rdev
->bdev
;
907 if (test_bit(Faulty
, &rdev
->flags
)) {
909 } else if (unlikely((bio_op(bio
) == REQ_OP_DISCARD
) &&
910 !blk_queue_discard(bdev_get_queue(bio
->bi_bdev
))))
914 generic_make_request(bio
);
918 spin_unlock_irq(&conf
->device_lock
);
922 * Sometimes we need to suspend IO while we do something else,
923 * either some resync/recovery, or reconfigure the array.
924 * To do this we raise a 'barrier'.
925 * The 'barrier' is a counter that can be raised multiple times
926 * to count how many activities are happening which preclude
928 * We can only raise the barrier if there is no pending IO.
929 * i.e. if nr_pending == 0.
930 * We choose only to raise the barrier if no-one is waiting for the
931 * barrier to go down. This means that as soon as an IO request
932 * is ready, no other operations which require a barrier will start
933 * until the IO request has had a chance.
935 * So: regular IO calls 'wait_barrier'. When that returns there
936 * is no backgroup IO happening, It must arrange to call
937 * allow_barrier when it has finished its IO.
938 * backgroup IO calls must call raise_barrier. Once that returns
939 * there is no normal IO happeing. It must arrange to call
940 * lower_barrier when the particular background IO completes.
943 static void raise_barrier(struct r10conf
*conf
, int force
)
945 BUG_ON(force
&& !conf
->barrier
);
946 spin_lock_irq(&conf
->resync_lock
);
948 /* Wait until no block IO is waiting (unless 'force') */
949 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
952 /* block any new IO from starting */
955 /* Now wait for all pending IO to complete */
956 wait_event_lock_irq(conf
->wait_barrier
,
957 !atomic_read(&conf
->nr_pending
) && conf
->barrier
< RESYNC_DEPTH
,
960 spin_unlock_irq(&conf
->resync_lock
);
963 static void lower_barrier(struct r10conf
*conf
)
966 spin_lock_irqsave(&conf
->resync_lock
, flags
);
968 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
969 wake_up(&conf
->wait_barrier
);
972 static void wait_barrier(struct r10conf
*conf
)
974 spin_lock_irq(&conf
->resync_lock
);
977 /* Wait for the barrier to drop.
978 * However if there are already pending
979 * requests (preventing the barrier from
980 * rising completely), and the
981 * pre-process bio queue isn't empty,
982 * then don't wait, as we need to empty
983 * that queue to get the nr_pending
986 raid10_log(conf
->mddev
, "wait barrier");
987 wait_event_lock_irq(conf
->wait_barrier
,
989 (atomic_read(&conf
->nr_pending
) &&
991 (!bio_list_empty(¤t
->bio_list
[0]) ||
992 !bio_list_empty(¤t
->bio_list
[1]))),
995 if (!conf
->nr_waiting
)
996 wake_up(&conf
->wait_barrier
);
998 atomic_inc(&conf
->nr_pending
);
999 spin_unlock_irq(&conf
->resync_lock
);
1002 static void allow_barrier(struct r10conf
*conf
)
1004 if ((atomic_dec_and_test(&conf
->nr_pending
)) ||
1005 (conf
->array_freeze_pending
))
1006 wake_up(&conf
->wait_barrier
);
1009 static void freeze_array(struct r10conf
*conf
, int extra
)
1011 /* stop syncio and normal IO and wait for everything to
1013 * We increment barrier and nr_waiting, and then
1014 * wait until nr_pending match nr_queued+extra
1015 * This is called in the context of one normal IO request
1016 * that has failed. Thus any sync request that might be pending
1017 * will be blocked by nr_pending, and we need to wait for
1018 * pending IO requests to complete or be queued for re-try.
1019 * Thus the number queued (nr_queued) plus this request (extra)
1020 * must match the number of pending IOs (nr_pending) before
1023 spin_lock_irq(&conf
->resync_lock
);
1024 conf
->array_freeze_pending
++;
1027 wait_event_lock_irq_cmd(conf
->wait_barrier
,
1028 atomic_read(&conf
->nr_pending
) == conf
->nr_queued
+extra
,
1030 flush_pending_writes(conf
));
1032 conf
->array_freeze_pending
--;
1033 spin_unlock_irq(&conf
->resync_lock
);
1036 static void unfreeze_array(struct r10conf
*conf
)
1038 /* reverse the effect of the freeze */
1039 spin_lock_irq(&conf
->resync_lock
);
1042 wake_up(&conf
->wait_barrier
);
1043 spin_unlock_irq(&conf
->resync_lock
);
1046 static sector_t
choose_data_offset(struct r10bio
*r10_bio
,
1047 struct md_rdev
*rdev
)
1049 if (!test_bit(MD_RECOVERY_RESHAPE
, &rdev
->mddev
->recovery
) ||
1050 test_bit(R10BIO_Previous
, &r10_bio
->state
))
1051 return rdev
->data_offset
;
1053 return rdev
->new_data_offset
;
1056 struct raid10_plug_cb
{
1057 struct blk_plug_cb cb
;
1058 struct bio_list pending
;
1062 static void raid10_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
1064 struct raid10_plug_cb
*plug
= container_of(cb
, struct raid10_plug_cb
,
1066 struct mddev
*mddev
= plug
->cb
.data
;
1067 struct r10conf
*conf
= mddev
->private;
1070 if (from_schedule
|| current
->bio_list
) {
1071 spin_lock_irq(&conf
->device_lock
);
1072 bio_list_merge(&conf
->pending_bio_list
, &plug
->pending
);
1073 conf
->pending_count
+= plug
->pending_cnt
;
1074 spin_unlock_irq(&conf
->device_lock
);
1075 wake_up(&conf
->wait_barrier
);
1076 md_wakeup_thread(mddev
->thread
);
1081 /* we aren't scheduling, so we can do the write-out directly. */
1082 bio
= bio_list_get(&plug
->pending
);
1083 bitmap_unplug(mddev
->bitmap
);
1084 wake_up(&conf
->wait_barrier
);
1086 while (bio
) { /* submit pending writes */
1087 struct bio
*next
= bio
->bi_next
;
1088 struct md_rdev
*rdev
= (void*)bio
->bi_bdev
;
1089 bio
->bi_next
= NULL
;
1090 bio
->bi_bdev
= rdev
->bdev
;
1091 if (test_bit(Faulty
, &rdev
->flags
)) {
1093 } else if (unlikely((bio_op(bio
) == REQ_OP_DISCARD
) &&
1094 !blk_queue_discard(bdev_get_queue(bio
->bi_bdev
))))
1095 /* Just ignore it */
1098 generic_make_request(bio
);
1104 static void raid10_read_request(struct mddev
*mddev
, struct bio
*bio
,
1105 struct r10bio
*r10_bio
)
1107 struct r10conf
*conf
= mddev
->private;
1108 struct bio
*read_bio
;
1109 const int op
= bio_op(bio
);
1110 const unsigned long do_sync
= (bio
->bi_opf
& REQ_SYNC
);
1113 struct md_rdev
*rdev
;
1114 char b
[BDEVNAME_SIZE
];
1115 int slot
= r10_bio
->read_slot
;
1116 struct md_rdev
*err_rdev
= NULL
;
1117 gfp_t gfp
= GFP_NOIO
;
1119 if (r10_bio
->devs
[slot
].rdev
) {
1121 * This is an error retry, but we cannot
1122 * safely dereference the rdev in the r10_bio,
1123 * we must use the one in conf.
1124 * If it has already been disconnected (unlikely)
1125 * we lose the device name in error messages.
1129 * As we are blocking raid10, it is a little safer to
1132 gfp
= GFP_NOIO
| __GFP_HIGH
;
1135 disk
= r10_bio
->devs
[slot
].devnum
;
1136 err_rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
1138 bdevname(err_rdev
->bdev
, b
);
1141 /* This never gets dereferenced */
1142 err_rdev
= r10_bio
->devs
[slot
].rdev
;
1147 * Register the new request and wait if the reconstruction
1148 * thread has put up a bar for new requests.
1149 * Continue immediately if no resync is active currently.
1153 sectors
= r10_bio
->sectors
;
1154 while (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1155 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
&&
1156 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
) {
1158 * IO spans the reshape position. Need to wait for reshape to
1161 raid10_log(conf
->mddev
, "wait reshape");
1162 allow_barrier(conf
);
1163 wait_event(conf
->wait_barrier
,
1164 conf
->reshape_progress
<= bio
->bi_iter
.bi_sector
||
1165 conf
->reshape_progress
>= bio
->bi_iter
.bi_sector
+
1170 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
1173 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1175 (unsigned long long)r10_bio
->sector
);
1177 raid_end_bio_io(r10_bio
);
1181 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1183 bdevname(rdev
->bdev
, b
),
1184 (unsigned long long)r10_bio
->sector
);
1185 if (max_sectors
< bio_sectors(bio
)) {
1186 struct bio
*split
= bio_split(bio
, max_sectors
,
1187 gfp
, conf
->bio_split
);
1188 bio_chain(split
, bio
);
1189 generic_make_request(bio
);
1191 r10_bio
->master_bio
= bio
;
1192 r10_bio
->sectors
= max_sectors
;
1194 slot
= r10_bio
->read_slot
;
1196 read_bio
= bio_clone_fast(bio
, gfp
, mddev
->bio_set
);
1198 r10_bio
->devs
[slot
].bio
= read_bio
;
1199 r10_bio
->devs
[slot
].rdev
= rdev
;
1201 read_bio
->bi_iter
.bi_sector
= r10_bio
->devs
[slot
].addr
+
1202 choose_data_offset(r10_bio
, rdev
);
1203 read_bio
->bi_bdev
= rdev
->bdev
;
1204 read_bio
->bi_end_io
= raid10_end_read_request
;
1205 bio_set_op_attrs(read_bio
, op
, do_sync
);
1206 if (test_bit(FailFast
, &rdev
->flags
) &&
1207 test_bit(R10BIO_FailFast
, &r10_bio
->state
))
1208 read_bio
->bi_opf
|= MD_FAILFAST
;
1209 read_bio
->bi_private
= r10_bio
;
1212 trace_block_bio_remap(bdev_get_queue(read_bio
->bi_bdev
),
1213 read_bio
, disk_devt(mddev
->gendisk
),
1215 generic_make_request(read_bio
);
1219 static void raid10_write_one_disk(struct mddev
*mddev
, struct r10bio
*r10_bio
,
1220 struct bio
*bio
, bool replacement
,
1223 const int op
= bio_op(bio
);
1224 const unsigned long do_sync
= (bio
->bi_opf
& REQ_SYNC
);
1225 const unsigned long do_fua
= (bio
->bi_opf
& REQ_FUA
);
1226 unsigned long flags
;
1227 struct blk_plug_cb
*cb
;
1228 struct raid10_plug_cb
*plug
= NULL
;
1229 struct r10conf
*conf
= mddev
->private;
1230 struct md_rdev
*rdev
;
1231 int devnum
= r10_bio
->devs
[n_copy
].devnum
;
1235 rdev
= conf
->mirrors
[devnum
].replacement
;
1237 /* Replacement just got moved to main 'rdev' */
1239 rdev
= conf
->mirrors
[devnum
].rdev
;
1242 rdev
= conf
->mirrors
[devnum
].rdev
;
1244 mbio
= bio_clone_fast(bio
, GFP_NOIO
, mddev
->bio_set
);
1246 r10_bio
->devs
[n_copy
].repl_bio
= mbio
;
1248 r10_bio
->devs
[n_copy
].bio
= mbio
;
1250 mbio
->bi_iter
.bi_sector
= (r10_bio
->devs
[n_copy
].addr
+
1251 choose_data_offset(r10_bio
, rdev
));
1252 mbio
->bi_bdev
= rdev
->bdev
;
1253 mbio
->bi_end_io
= raid10_end_write_request
;
1254 bio_set_op_attrs(mbio
, op
, do_sync
| do_fua
);
1255 if (!replacement
&& test_bit(FailFast
,
1256 &conf
->mirrors
[devnum
].rdev
->flags
)
1257 && enough(conf
, devnum
))
1258 mbio
->bi_opf
|= MD_FAILFAST
;
1259 mbio
->bi_private
= r10_bio
;
1261 if (conf
->mddev
->gendisk
)
1262 trace_block_bio_remap(bdev_get_queue(mbio
->bi_bdev
),
1263 mbio
, disk_devt(conf
->mddev
->gendisk
),
1265 /* flush_pending_writes() needs access to the rdev so...*/
1266 mbio
->bi_bdev
= (void *)rdev
;
1268 atomic_inc(&r10_bio
->remaining
);
1270 cb
= blk_check_plugged(raid10_unplug
, mddev
, sizeof(*plug
));
1272 plug
= container_of(cb
, struct raid10_plug_cb
, cb
);
1276 bio_list_add(&plug
->pending
, mbio
);
1277 plug
->pending_cnt
++;
1279 spin_lock_irqsave(&conf
->device_lock
, flags
);
1280 bio_list_add(&conf
->pending_bio_list
, mbio
);
1281 conf
->pending_count
++;
1282 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1283 md_wakeup_thread(mddev
->thread
);
1287 static void raid10_write_request(struct mddev
*mddev
, struct bio
*bio
,
1288 struct r10bio
*r10_bio
)
1290 struct r10conf
*conf
= mddev
->private;
1292 struct md_rdev
*blocked_rdev
;
1297 * Register the new request and wait if the reconstruction
1298 * thread has put up a bar for new requests.
1299 * Continue immediately if no resync is active currently.
1303 sectors
= r10_bio
->sectors
;
1304 while (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1305 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
&&
1306 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
) {
1308 * IO spans the reshape position. Need to wait for reshape to
1311 raid10_log(conf
->mddev
, "wait reshape");
1312 allow_barrier(conf
);
1313 wait_event(conf
->wait_barrier
,
1314 conf
->reshape_progress
<= bio
->bi_iter
.bi_sector
||
1315 conf
->reshape_progress
>= bio
->bi_iter
.bi_sector
+
1320 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1321 (mddev
->reshape_backwards
1322 ? (bio
->bi_iter
.bi_sector
< conf
->reshape_safe
&&
1323 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
)
1324 : (bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_safe
&&
1325 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
))) {
1326 /* Need to update reshape_position in metadata */
1327 mddev
->reshape_position
= conf
->reshape_progress
;
1328 set_mask_bits(&mddev
->sb_flags
, 0,
1329 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
1330 md_wakeup_thread(mddev
->thread
);
1331 raid10_log(conf
->mddev
, "wait reshape metadata");
1332 wait_event(mddev
->sb_wait
,
1333 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
1335 conf
->reshape_safe
= mddev
->reshape_position
;
1338 if (conf
->pending_count
>= max_queued_requests
) {
1339 md_wakeup_thread(mddev
->thread
);
1340 raid10_log(mddev
, "wait queued");
1341 wait_event(conf
->wait_barrier
,
1342 conf
->pending_count
< max_queued_requests
);
1344 /* first select target devices under rcu_lock and
1345 * inc refcount on their rdev. Record them by setting
1347 * If there are known/acknowledged bad blocks on any device
1348 * on which we have seen a write error, we want to avoid
1349 * writing to those blocks. This potentially requires several
1350 * writes to write around the bad blocks. Each set of writes
1351 * gets its own r10_bio with a set of bios attached.
1354 r10_bio
->read_slot
= -1; /* make sure repl_bio gets freed */
1355 raid10_find_phys(conf
, r10_bio
);
1357 blocked_rdev
= NULL
;
1359 max_sectors
= r10_bio
->sectors
;
1361 for (i
= 0; i
< conf
->copies
; i
++) {
1362 int d
= r10_bio
->devs
[i
].devnum
;
1363 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1364 struct md_rdev
*rrdev
= rcu_dereference(
1365 conf
->mirrors
[d
].replacement
);
1368 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
1369 atomic_inc(&rdev
->nr_pending
);
1370 blocked_rdev
= rdev
;
1373 if (rrdev
&& unlikely(test_bit(Blocked
, &rrdev
->flags
))) {
1374 atomic_inc(&rrdev
->nr_pending
);
1375 blocked_rdev
= rrdev
;
1378 if (rdev
&& (test_bit(Faulty
, &rdev
->flags
)))
1380 if (rrdev
&& (test_bit(Faulty
, &rrdev
->flags
)))
1383 r10_bio
->devs
[i
].bio
= NULL
;
1384 r10_bio
->devs
[i
].repl_bio
= NULL
;
1386 if (!rdev
&& !rrdev
) {
1387 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
1390 if (rdev
&& test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1392 sector_t dev_sector
= r10_bio
->devs
[i
].addr
;
1396 is_bad
= is_badblock(rdev
, dev_sector
, max_sectors
,
1397 &first_bad
, &bad_sectors
);
1399 /* Mustn't write here until the bad block
1402 atomic_inc(&rdev
->nr_pending
);
1403 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1404 blocked_rdev
= rdev
;
1407 if (is_bad
&& first_bad
<= dev_sector
) {
1408 /* Cannot write here at all */
1409 bad_sectors
-= (dev_sector
- first_bad
);
1410 if (bad_sectors
< max_sectors
)
1411 /* Mustn't write more than bad_sectors
1412 * to other devices yet
1414 max_sectors
= bad_sectors
;
1415 /* We don't set R10BIO_Degraded as that
1416 * only applies if the disk is missing,
1417 * so it might be re-added, and we want to
1418 * know to recover this chunk.
1419 * In this case the device is here, and the
1420 * fact that this chunk is not in-sync is
1421 * recorded in the bad block log.
1426 int good_sectors
= first_bad
- dev_sector
;
1427 if (good_sectors
< max_sectors
)
1428 max_sectors
= good_sectors
;
1432 r10_bio
->devs
[i
].bio
= bio
;
1433 atomic_inc(&rdev
->nr_pending
);
1436 r10_bio
->devs
[i
].repl_bio
= bio
;
1437 atomic_inc(&rrdev
->nr_pending
);
1442 if (unlikely(blocked_rdev
)) {
1443 /* Have to wait for this device to get unblocked, then retry */
1447 for (j
= 0; j
< i
; j
++) {
1448 if (r10_bio
->devs
[j
].bio
) {
1449 d
= r10_bio
->devs
[j
].devnum
;
1450 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1452 if (r10_bio
->devs
[j
].repl_bio
) {
1453 struct md_rdev
*rdev
;
1454 d
= r10_bio
->devs
[j
].devnum
;
1455 rdev
= conf
->mirrors
[d
].replacement
;
1457 /* Race with remove_disk */
1459 rdev
= conf
->mirrors
[d
].rdev
;
1461 rdev_dec_pending(rdev
, mddev
);
1464 allow_barrier(conf
);
1465 raid10_log(conf
->mddev
, "wait rdev %d blocked", blocked_rdev
->raid_disk
);
1466 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1471 if (max_sectors
< r10_bio
->sectors
)
1472 r10_bio
->sectors
= max_sectors
;
1474 if (r10_bio
->sectors
< bio_sectors(bio
)) {
1475 struct bio
*split
= bio_split(bio
, r10_bio
->sectors
,
1476 GFP_NOIO
, conf
->bio_split
);
1477 bio_chain(split
, bio
);
1478 generic_make_request(bio
);
1480 r10_bio
->master_bio
= bio
;
1483 atomic_set(&r10_bio
->remaining
, 1);
1484 bitmap_startwrite(mddev
->bitmap
, r10_bio
->sector
, r10_bio
->sectors
, 0);
1486 for (i
= 0; i
< conf
->copies
; i
++) {
1487 if (r10_bio
->devs
[i
].bio
)
1488 raid10_write_one_disk(mddev
, r10_bio
, bio
, false, i
);
1489 if (r10_bio
->devs
[i
].repl_bio
)
1490 raid10_write_one_disk(mddev
, r10_bio
, bio
, true, i
);
1492 one_write_done(r10_bio
);
1495 static void __make_request(struct mddev
*mddev
, struct bio
*bio
, int sectors
)
1497 struct r10conf
*conf
= mddev
->private;
1498 struct r10bio
*r10_bio
;
1500 r10_bio
= mempool_alloc(conf
->r10bio_pool
, GFP_NOIO
);
1502 r10_bio
->master_bio
= bio
;
1503 r10_bio
->sectors
= sectors
;
1505 r10_bio
->mddev
= mddev
;
1506 r10_bio
->sector
= bio
->bi_iter
.bi_sector
;
1508 memset(r10_bio
->devs
, 0, sizeof(r10_bio
->devs
[0]) * conf
->copies
);
1510 if (bio_data_dir(bio
) == READ
)
1511 raid10_read_request(mddev
, bio
, r10_bio
);
1513 raid10_write_request(mddev
, bio
, r10_bio
);
1516 static bool raid10_make_request(struct mddev
*mddev
, struct bio
*bio
)
1518 struct r10conf
*conf
= mddev
->private;
1519 sector_t chunk_mask
= (conf
->geo
.chunk_mask
& conf
->prev
.chunk_mask
);
1520 int chunk_sects
= chunk_mask
+ 1;
1521 int sectors
= bio_sectors(bio
);
1523 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
1524 md_flush_request(mddev
, bio
);
1528 if (!md_write_start(mddev
, bio
))
1532 * If this request crosses a chunk boundary, we need to split
1535 if (unlikely((bio
->bi_iter
.bi_sector
& chunk_mask
) +
1536 sectors
> chunk_sects
1537 && (conf
->geo
.near_copies
< conf
->geo
.raid_disks
1538 || conf
->prev
.near_copies
<
1539 conf
->prev
.raid_disks
)))
1540 sectors
= chunk_sects
-
1541 (bio
->bi_iter
.bi_sector
&
1543 __make_request(mddev
, bio
, sectors
);
1545 /* In case raid10d snuck in to freeze_array */
1546 wake_up(&conf
->wait_barrier
);
1550 static void raid10_status(struct seq_file
*seq
, struct mddev
*mddev
)
1552 struct r10conf
*conf
= mddev
->private;
1555 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
)
1556 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
1557 if (conf
->geo
.near_copies
> 1)
1558 seq_printf(seq
, " %d near-copies", conf
->geo
.near_copies
);
1559 if (conf
->geo
.far_copies
> 1) {
1560 if (conf
->geo
.far_offset
)
1561 seq_printf(seq
, " %d offset-copies", conf
->geo
.far_copies
);
1563 seq_printf(seq
, " %d far-copies", conf
->geo
.far_copies
);
1564 if (conf
->geo
.far_set_size
!= conf
->geo
.raid_disks
)
1565 seq_printf(seq
, " %d devices per set", conf
->geo
.far_set_size
);
1567 seq_printf(seq
, " [%d/%d] [", conf
->geo
.raid_disks
,
1568 conf
->geo
.raid_disks
- mddev
->degraded
);
1570 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1571 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1572 seq_printf(seq
, "%s", rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
1575 seq_printf(seq
, "]");
1578 /* check if there are enough drives for
1579 * every block to appear on atleast one.
1580 * Don't consider the device numbered 'ignore'
1581 * as we might be about to remove it.
1583 static int _enough(struct r10conf
*conf
, int previous
, int ignore
)
1589 disks
= conf
->prev
.raid_disks
;
1590 ncopies
= conf
->prev
.near_copies
;
1592 disks
= conf
->geo
.raid_disks
;
1593 ncopies
= conf
->geo
.near_copies
;
1598 int n
= conf
->copies
;
1602 struct md_rdev
*rdev
;
1603 if (this != ignore
&&
1604 (rdev
= rcu_dereference(conf
->mirrors
[this].rdev
)) &&
1605 test_bit(In_sync
, &rdev
->flags
))
1607 this = (this+1) % disks
;
1611 first
= (first
+ ncopies
) % disks
;
1612 } while (first
!= 0);
1619 static int enough(struct r10conf
*conf
, int ignore
)
1621 /* when calling 'enough', both 'prev' and 'geo' must
1623 * This is ensured if ->reconfig_mutex or ->device_lock
1626 return _enough(conf
, 0, ignore
) &&
1627 _enough(conf
, 1, ignore
);
1630 static void raid10_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1632 char b
[BDEVNAME_SIZE
];
1633 struct r10conf
*conf
= mddev
->private;
1634 unsigned long flags
;
1637 * If it is not operational, then we have already marked it as dead
1638 * else if it is the last working disks, ignore the error, let the
1639 * next level up know.
1640 * else mark the drive as failed
1642 spin_lock_irqsave(&conf
->device_lock
, flags
);
1643 if (test_bit(In_sync
, &rdev
->flags
)
1644 && !enough(conf
, rdev
->raid_disk
)) {
1646 * Don't fail the drive, just return an IO error.
1648 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1651 if (test_and_clear_bit(In_sync
, &rdev
->flags
))
1654 * If recovery is running, make sure it aborts.
1656 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1657 set_bit(Blocked
, &rdev
->flags
);
1658 set_bit(Faulty
, &rdev
->flags
);
1659 set_mask_bits(&mddev
->sb_flags
, 0,
1660 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
1661 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1662 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1663 "md/raid10:%s: Operation continuing on %d devices.\n",
1664 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1665 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
);
1668 static void print_conf(struct r10conf
*conf
)
1671 struct md_rdev
*rdev
;
1673 pr_debug("RAID10 conf printout:\n");
1675 pr_debug("(!conf)\n");
1678 pr_debug(" --- wd:%d rd:%d\n", conf
->geo
.raid_disks
- conf
->mddev
->degraded
,
1679 conf
->geo
.raid_disks
);
1681 /* This is only called with ->reconfix_mutex held, so
1682 * rcu protection of rdev is not needed */
1683 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1684 char b
[BDEVNAME_SIZE
];
1685 rdev
= conf
->mirrors
[i
].rdev
;
1687 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1688 i
, !test_bit(In_sync
, &rdev
->flags
),
1689 !test_bit(Faulty
, &rdev
->flags
),
1690 bdevname(rdev
->bdev
,b
));
1694 static void close_sync(struct r10conf
*conf
)
1697 allow_barrier(conf
);
1699 mempool_destroy(conf
->r10buf_pool
);
1700 conf
->r10buf_pool
= NULL
;
1703 static int raid10_spare_active(struct mddev
*mddev
)
1706 struct r10conf
*conf
= mddev
->private;
1707 struct raid10_info
*tmp
;
1709 unsigned long flags
;
1712 * Find all non-in_sync disks within the RAID10 configuration
1713 * and mark them in_sync
1715 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1716 tmp
= conf
->mirrors
+ i
;
1717 if (tmp
->replacement
1718 && tmp
->replacement
->recovery_offset
== MaxSector
1719 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
1720 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
1721 /* Replacement has just become active */
1723 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
1726 /* Replaced device not technically faulty,
1727 * but we need to be sure it gets removed
1728 * and never re-added.
1730 set_bit(Faulty
, &tmp
->rdev
->flags
);
1731 sysfs_notify_dirent_safe(
1732 tmp
->rdev
->sysfs_state
);
1734 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
1735 } else if (tmp
->rdev
1736 && tmp
->rdev
->recovery_offset
== MaxSector
1737 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1738 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1740 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
1743 spin_lock_irqsave(&conf
->device_lock
, flags
);
1744 mddev
->degraded
-= count
;
1745 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1751 static int raid10_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1753 struct r10conf
*conf
= mddev
->private;
1757 int last
= conf
->geo
.raid_disks
- 1;
1759 if (mddev
->recovery_cp
< MaxSector
)
1760 /* only hot-add to in-sync arrays, as recovery is
1761 * very different from resync
1764 if (rdev
->saved_raid_disk
< 0 && !_enough(conf
, 1, -1))
1767 if (md_integrity_add_rdev(rdev
, mddev
))
1770 if (rdev
->raid_disk
>= 0)
1771 first
= last
= rdev
->raid_disk
;
1773 if (rdev
->saved_raid_disk
>= first
&&
1774 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1775 mirror
= rdev
->saved_raid_disk
;
1778 for ( ; mirror
<= last
; mirror
++) {
1779 struct raid10_info
*p
= &conf
->mirrors
[mirror
];
1780 if (p
->recovery_disabled
== mddev
->recovery_disabled
)
1783 if (!test_bit(WantReplacement
, &p
->rdev
->flags
) ||
1784 p
->replacement
!= NULL
)
1786 clear_bit(In_sync
, &rdev
->flags
);
1787 set_bit(Replacement
, &rdev
->flags
);
1788 rdev
->raid_disk
= mirror
;
1791 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1792 rdev
->data_offset
<< 9);
1794 rcu_assign_pointer(p
->replacement
, rdev
);
1799 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1800 rdev
->data_offset
<< 9);
1802 p
->head_position
= 0;
1803 p
->recovery_disabled
= mddev
->recovery_disabled
- 1;
1804 rdev
->raid_disk
= mirror
;
1806 if (rdev
->saved_raid_disk
!= mirror
)
1808 rcu_assign_pointer(p
->rdev
, rdev
);
1811 if (mddev
->queue
&& blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
1812 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
, mddev
->queue
);
1818 static int raid10_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1820 struct r10conf
*conf
= mddev
->private;
1822 int number
= rdev
->raid_disk
;
1823 struct md_rdev
**rdevp
;
1824 struct raid10_info
*p
= conf
->mirrors
+ number
;
1827 if (rdev
== p
->rdev
)
1829 else if (rdev
== p
->replacement
)
1830 rdevp
= &p
->replacement
;
1834 if (test_bit(In_sync
, &rdev
->flags
) ||
1835 atomic_read(&rdev
->nr_pending
)) {
1839 /* Only remove non-faulty devices if recovery
1842 if (!test_bit(Faulty
, &rdev
->flags
) &&
1843 mddev
->recovery_disabled
!= p
->recovery_disabled
&&
1844 (!p
->replacement
|| p
->replacement
== rdev
) &&
1845 number
< conf
->geo
.raid_disks
&&
1851 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
1853 if (atomic_read(&rdev
->nr_pending
)) {
1854 /* lost the race, try later */
1860 if (p
->replacement
) {
1861 /* We must have just cleared 'rdev' */
1862 p
->rdev
= p
->replacement
;
1863 clear_bit(Replacement
, &p
->replacement
->flags
);
1864 smp_mb(); /* Make sure other CPUs may see both as identical
1865 * but will never see neither -- if they are careful.
1867 p
->replacement
= NULL
;
1870 clear_bit(WantReplacement
, &rdev
->flags
);
1871 err
= md_integrity_register(mddev
);
1879 static void __end_sync_read(struct r10bio
*r10_bio
, struct bio
*bio
, int d
)
1881 struct r10conf
*conf
= r10_bio
->mddev
->private;
1883 if (!bio
->bi_status
)
1884 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1886 /* The write handler will notice the lack of
1887 * R10BIO_Uptodate and record any errors etc
1889 atomic_add(r10_bio
->sectors
,
1890 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1892 /* for reconstruct, we always reschedule after a read.
1893 * for resync, only after all reads
1895 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1896 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1897 atomic_dec_and_test(&r10_bio
->remaining
)) {
1898 /* we have read all the blocks,
1899 * do the comparison in process context in raid10d
1901 reschedule_retry(r10_bio
);
1905 static void end_sync_read(struct bio
*bio
)
1907 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
1908 struct r10conf
*conf
= r10_bio
->mddev
->private;
1909 int d
= find_bio_disk(conf
, r10_bio
, bio
, NULL
, NULL
);
1911 __end_sync_read(r10_bio
, bio
, d
);
1914 static void end_reshape_read(struct bio
*bio
)
1916 /* reshape read bio isn't allocated from r10buf_pool */
1917 struct r10bio
*r10_bio
= bio
->bi_private
;
1919 __end_sync_read(r10_bio
, bio
, r10_bio
->read_slot
);
1922 static void end_sync_request(struct r10bio
*r10_bio
)
1924 struct mddev
*mddev
= r10_bio
->mddev
;
1926 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1927 if (r10_bio
->master_bio
== NULL
) {
1928 /* the primary of several recovery bios */
1929 sector_t s
= r10_bio
->sectors
;
1930 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
1931 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
1932 reschedule_retry(r10_bio
);
1935 md_done_sync(mddev
, s
, 1);
1938 struct r10bio
*r10_bio2
= (struct r10bio
*)r10_bio
->master_bio
;
1939 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
1940 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
1941 reschedule_retry(r10_bio
);
1949 static void end_sync_write(struct bio
*bio
)
1951 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
1952 struct mddev
*mddev
= r10_bio
->mddev
;
1953 struct r10conf
*conf
= mddev
->private;
1959 struct md_rdev
*rdev
= NULL
;
1961 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
1963 rdev
= conf
->mirrors
[d
].replacement
;
1965 rdev
= conf
->mirrors
[d
].rdev
;
1967 if (bio
->bi_status
) {
1969 md_error(mddev
, rdev
);
1971 set_bit(WriteErrorSeen
, &rdev
->flags
);
1972 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1973 set_bit(MD_RECOVERY_NEEDED
,
1974 &rdev
->mddev
->recovery
);
1975 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
1977 } else if (is_badblock(rdev
,
1978 r10_bio
->devs
[slot
].addr
,
1980 &first_bad
, &bad_sectors
))
1981 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
1983 rdev_dec_pending(rdev
, mddev
);
1985 end_sync_request(r10_bio
);
1989 * Note: sync and recover and handled very differently for raid10
1990 * This code is for resync.
1991 * For resync, we read through virtual addresses and read all blocks.
1992 * If there is any error, we schedule a write. The lowest numbered
1993 * drive is authoritative.
1994 * However requests come for physical address, so we need to map.
1995 * For every physical address there are raid_disks/copies virtual addresses,
1996 * which is always are least one, but is not necessarly an integer.
1997 * This means that a physical address can span multiple chunks, so we may
1998 * have to submit multiple io requests for a single sync request.
2001 * We check if all blocks are in-sync and only write to blocks that
2004 static void sync_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2006 struct r10conf
*conf
= mddev
->private;
2008 struct bio
*tbio
, *fbio
;
2010 struct page
**tpages
, **fpages
;
2012 atomic_set(&r10_bio
->remaining
, 1);
2014 /* find the first device with a block */
2015 for (i
=0; i
<conf
->copies
; i
++)
2016 if (!r10_bio
->devs
[i
].bio
->bi_status
)
2019 if (i
== conf
->copies
)
2023 fbio
= r10_bio
->devs
[i
].bio
;
2024 fbio
->bi_iter
.bi_size
= r10_bio
->sectors
<< 9;
2025 fbio
->bi_iter
.bi_idx
= 0;
2026 fpages
= get_resync_pages(fbio
)->pages
;
2028 vcnt
= (r10_bio
->sectors
+ (PAGE_SIZE
>> 9) - 1) >> (PAGE_SHIFT
- 9);
2029 /* now find blocks with errors */
2030 for (i
=0 ; i
< conf
->copies
; i
++) {
2032 struct md_rdev
*rdev
;
2033 struct resync_pages
*rp
;
2035 tbio
= r10_bio
->devs
[i
].bio
;
2037 if (tbio
->bi_end_io
!= end_sync_read
)
2042 tpages
= get_resync_pages(tbio
)->pages
;
2043 d
= r10_bio
->devs
[i
].devnum
;
2044 rdev
= conf
->mirrors
[d
].rdev
;
2045 if (!r10_bio
->devs
[i
].bio
->bi_status
) {
2046 /* We know that the bi_io_vec layout is the same for
2047 * both 'first' and 'i', so we just compare them.
2048 * All vec entries are PAGE_SIZE;
2050 int sectors
= r10_bio
->sectors
;
2051 for (j
= 0; j
< vcnt
; j
++) {
2052 int len
= PAGE_SIZE
;
2053 if (sectors
< (len
/ 512))
2054 len
= sectors
* 512;
2055 if (memcmp(page_address(fpages
[j
]),
2056 page_address(tpages
[j
]),
2063 atomic64_add(r10_bio
->sectors
, &mddev
->resync_mismatches
);
2064 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2065 /* Don't fix anything. */
2067 } else if (test_bit(FailFast
, &rdev
->flags
)) {
2068 /* Just give up on this device */
2069 md_error(rdev
->mddev
, rdev
);
2072 /* Ok, we need to write this bio, either to correct an
2073 * inconsistency or to correct an unreadable block.
2074 * First we need to fixup bv_offset, bv_len and
2075 * bi_vecs, as the read request might have corrupted these
2077 rp
= get_resync_pages(tbio
);
2080 md_bio_reset_resync_pages(tbio
, rp
, fbio
->bi_iter
.bi_size
);
2082 rp
->raid_bio
= r10_bio
;
2083 tbio
->bi_private
= rp
;
2084 tbio
->bi_iter
.bi_sector
= r10_bio
->devs
[i
].addr
;
2085 tbio
->bi_end_io
= end_sync_write
;
2086 bio_set_op_attrs(tbio
, REQ_OP_WRITE
, 0);
2088 bio_copy_data(tbio
, fbio
);
2090 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2091 atomic_inc(&r10_bio
->remaining
);
2092 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, bio_sectors(tbio
));
2094 if (test_bit(FailFast
, &conf
->mirrors
[d
].rdev
->flags
))
2095 tbio
->bi_opf
|= MD_FAILFAST
;
2096 tbio
->bi_iter
.bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
2097 tbio
->bi_bdev
= conf
->mirrors
[d
].rdev
->bdev
;
2098 generic_make_request(tbio
);
2101 /* Now write out to any replacement devices
2104 for (i
= 0; i
< conf
->copies
; i
++) {
2107 tbio
= r10_bio
->devs
[i
].repl_bio
;
2108 if (!tbio
|| !tbio
->bi_end_io
)
2110 if (r10_bio
->devs
[i
].bio
->bi_end_io
!= end_sync_write
2111 && r10_bio
->devs
[i
].bio
!= fbio
)
2112 bio_copy_data(tbio
, fbio
);
2113 d
= r10_bio
->devs
[i
].devnum
;
2114 atomic_inc(&r10_bio
->remaining
);
2115 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2117 generic_make_request(tbio
);
2121 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
2122 md_done_sync(mddev
, r10_bio
->sectors
, 1);
2128 * Now for the recovery code.
2129 * Recovery happens across physical sectors.
2130 * We recover all non-is_sync drives by finding the virtual address of
2131 * each, and then choose a working drive that also has that virt address.
2132 * There is a separate r10_bio for each non-in_sync drive.
2133 * Only the first two slots are in use. The first for reading,
2134 * The second for writing.
2137 static void fix_recovery_read_error(struct r10bio
*r10_bio
)
2139 /* We got a read error during recovery.
2140 * We repeat the read in smaller page-sized sections.
2141 * If a read succeeds, write it to the new device or record
2142 * a bad block if we cannot.
2143 * If a read fails, record a bad block on both old and
2146 struct mddev
*mddev
= r10_bio
->mddev
;
2147 struct r10conf
*conf
= mddev
->private;
2148 struct bio
*bio
= r10_bio
->devs
[0].bio
;
2150 int sectors
= r10_bio
->sectors
;
2152 int dr
= r10_bio
->devs
[0].devnum
;
2153 int dw
= r10_bio
->devs
[1].devnum
;
2154 struct page
**pages
= get_resync_pages(bio
)->pages
;
2158 struct md_rdev
*rdev
;
2162 if (s
> (PAGE_SIZE
>>9))
2165 rdev
= conf
->mirrors
[dr
].rdev
;
2166 addr
= r10_bio
->devs
[0].addr
+ sect
,
2167 ok
= sync_page_io(rdev
,
2171 REQ_OP_READ
, 0, false);
2173 rdev
= conf
->mirrors
[dw
].rdev
;
2174 addr
= r10_bio
->devs
[1].addr
+ sect
;
2175 ok
= sync_page_io(rdev
,
2179 REQ_OP_WRITE
, 0, false);
2181 set_bit(WriteErrorSeen
, &rdev
->flags
);
2182 if (!test_and_set_bit(WantReplacement
,
2184 set_bit(MD_RECOVERY_NEEDED
,
2185 &rdev
->mddev
->recovery
);
2189 /* We don't worry if we cannot set a bad block -
2190 * it really is bad so there is no loss in not
2193 rdev_set_badblocks(rdev
, addr
, s
, 0);
2195 if (rdev
!= conf
->mirrors
[dw
].rdev
) {
2196 /* need bad block on destination too */
2197 struct md_rdev
*rdev2
= conf
->mirrors
[dw
].rdev
;
2198 addr
= r10_bio
->devs
[1].addr
+ sect
;
2199 ok
= rdev_set_badblocks(rdev2
, addr
, s
, 0);
2201 /* just abort the recovery */
2202 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2205 conf
->mirrors
[dw
].recovery_disabled
2206 = mddev
->recovery_disabled
;
2207 set_bit(MD_RECOVERY_INTR
,
2220 static void recovery_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2222 struct r10conf
*conf
= mddev
->private;
2224 struct bio
*wbio
, *wbio2
;
2226 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
)) {
2227 fix_recovery_read_error(r10_bio
);
2228 end_sync_request(r10_bio
);
2233 * share the pages with the first bio
2234 * and submit the write request
2236 d
= r10_bio
->devs
[1].devnum
;
2237 wbio
= r10_bio
->devs
[1].bio
;
2238 wbio2
= r10_bio
->devs
[1].repl_bio
;
2239 /* Need to test wbio2->bi_end_io before we call
2240 * generic_make_request as if the former is NULL,
2241 * the latter is free to free wbio2.
2243 if (wbio2
&& !wbio2
->bi_end_io
)
2245 if (wbio
->bi_end_io
) {
2246 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2247 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, bio_sectors(wbio
));
2248 generic_make_request(wbio
);
2251 atomic_inc(&conf
->mirrors
[d
].replacement
->nr_pending
);
2252 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2253 bio_sectors(wbio2
));
2254 generic_make_request(wbio2
);
2259 * Used by fix_read_error() to decay the per rdev read_errors.
2260 * We halve the read error count for every hour that has elapsed
2261 * since the last recorded read error.
2264 static void check_decay_read_errors(struct mddev
*mddev
, struct md_rdev
*rdev
)
2267 unsigned long hours_since_last
;
2268 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
2270 cur_time_mon
= ktime_get_seconds();
2272 if (rdev
->last_read_error
== 0) {
2273 /* first time we've seen a read error */
2274 rdev
->last_read_error
= cur_time_mon
;
2278 hours_since_last
= (long)(cur_time_mon
-
2279 rdev
->last_read_error
) / 3600;
2281 rdev
->last_read_error
= cur_time_mon
;
2284 * if hours_since_last is > the number of bits in read_errors
2285 * just set read errors to 0. We do this to avoid
2286 * overflowing the shift of read_errors by hours_since_last.
2288 if (hours_since_last
>= 8 * sizeof(read_errors
))
2289 atomic_set(&rdev
->read_errors
, 0);
2291 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
2294 static int r10_sync_page_io(struct md_rdev
*rdev
, sector_t sector
,
2295 int sectors
, struct page
*page
, int rw
)
2300 if (is_badblock(rdev
, sector
, sectors
, &first_bad
, &bad_sectors
)
2301 && (rw
== READ
|| test_bit(WriteErrorSeen
, &rdev
->flags
)))
2303 if (sync_page_io(rdev
, sector
, sectors
<< 9, page
, rw
, 0, false))
2307 set_bit(WriteErrorSeen
, &rdev
->flags
);
2308 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2309 set_bit(MD_RECOVERY_NEEDED
,
2310 &rdev
->mddev
->recovery
);
2312 /* need to record an error - either for the block or the device */
2313 if (!rdev_set_badblocks(rdev
, sector
, sectors
, 0))
2314 md_error(rdev
->mddev
, rdev
);
2319 * This is a kernel thread which:
2321 * 1. Retries failed read operations on working mirrors.
2322 * 2. Updates the raid superblock when problems encounter.
2323 * 3. Performs writes following reads for array synchronising.
2326 static void fix_read_error(struct r10conf
*conf
, struct mddev
*mddev
, struct r10bio
*r10_bio
)
2328 int sect
= 0; /* Offset from r10_bio->sector */
2329 int sectors
= r10_bio
->sectors
;
2330 struct md_rdev
*rdev
;
2331 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
2332 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
2334 /* still own a reference to this rdev, so it cannot
2335 * have been cleared recently.
2337 rdev
= conf
->mirrors
[d
].rdev
;
2339 if (test_bit(Faulty
, &rdev
->flags
))
2340 /* drive has already been failed, just ignore any
2341 more fix_read_error() attempts */
2344 check_decay_read_errors(mddev
, rdev
);
2345 atomic_inc(&rdev
->read_errors
);
2346 if (atomic_read(&rdev
->read_errors
) > max_read_errors
) {
2347 char b
[BDEVNAME_SIZE
];
2348 bdevname(rdev
->bdev
, b
);
2350 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2352 atomic_read(&rdev
->read_errors
), max_read_errors
);
2353 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2355 md_error(mddev
, rdev
);
2356 r10_bio
->devs
[r10_bio
->read_slot
].bio
= IO_BLOCKED
;
2362 int sl
= r10_bio
->read_slot
;
2366 if (s
> (PAGE_SIZE
>>9))
2374 d
= r10_bio
->devs
[sl
].devnum
;
2375 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2377 test_bit(In_sync
, &rdev
->flags
) &&
2378 !test_bit(Faulty
, &rdev
->flags
) &&
2379 is_badblock(rdev
, r10_bio
->devs
[sl
].addr
+ sect
, s
,
2380 &first_bad
, &bad_sectors
) == 0) {
2381 atomic_inc(&rdev
->nr_pending
);
2383 success
= sync_page_io(rdev
,
2384 r10_bio
->devs
[sl
].addr
+
2388 REQ_OP_READ
, 0, false);
2389 rdev_dec_pending(rdev
, mddev
);
2395 if (sl
== conf
->copies
)
2397 } while (!success
&& sl
!= r10_bio
->read_slot
);
2401 /* Cannot read from anywhere, just mark the block
2402 * as bad on the first device to discourage future
2405 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
2406 rdev
= conf
->mirrors
[dn
].rdev
;
2408 if (!rdev_set_badblocks(
2410 r10_bio
->devs
[r10_bio
->read_slot
].addr
2413 md_error(mddev
, rdev
);
2414 r10_bio
->devs
[r10_bio
->read_slot
].bio
2421 /* write it back and re-read */
2423 while (sl
!= r10_bio
->read_slot
) {
2424 char b
[BDEVNAME_SIZE
];
2429 d
= r10_bio
->devs
[sl
].devnum
;
2430 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2432 test_bit(Faulty
, &rdev
->flags
) ||
2433 !test_bit(In_sync
, &rdev
->flags
))
2436 atomic_inc(&rdev
->nr_pending
);
2438 if (r10_sync_page_io(rdev
,
2439 r10_bio
->devs
[sl
].addr
+
2441 s
, conf
->tmppage
, WRITE
)
2443 /* Well, this device is dead */
2444 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2446 (unsigned long long)(
2448 choose_data_offset(r10_bio
,
2450 bdevname(rdev
->bdev
, b
));
2451 pr_notice("md/raid10:%s: %s: failing drive\n",
2453 bdevname(rdev
->bdev
, b
));
2455 rdev_dec_pending(rdev
, mddev
);
2459 while (sl
!= r10_bio
->read_slot
) {
2460 char b
[BDEVNAME_SIZE
];
2465 d
= r10_bio
->devs
[sl
].devnum
;
2466 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2468 test_bit(Faulty
, &rdev
->flags
) ||
2469 !test_bit(In_sync
, &rdev
->flags
))
2472 atomic_inc(&rdev
->nr_pending
);
2474 switch (r10_sync_page_io(rdev
,
2475 r10_bio
->devs
[sl
].addr
+
2480 /* Well, this device is dead */
2481 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2483 (unsigned long long)(
2485 choose_data_offset(r10_bio
, rdev
)),
2486 bdevname(rdev
->bdev
, b
));
2487 pr_notice("md/raid10:%s: %s: failing drive\n",
2489 bdevname(rdev
->bdev
, b
));
2492 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2494 (unsigned long long)(
2496 choose_data_offset(r10_bio
, rdev
)),
2497 bdevname(rdev
->bdev
, b
));
2498 atomic_add(s
, &rdev
->corrected_errors
);
2501 rdev_dec_pending(rdev
, mddev
);
2511 static int narrow_write_error(struct r10bio
*r10_bio
, int i
)
2513 struct bio
*bio
= r10_bio
->master_bio
;
2514 struct mddev
*mddev
= r10_bio
->mddev
;
2515 struct r10conf
*conf
= mddev
->private;
2516 struct md_rdev
*rdev
= conf
->mirrors
[r10_bio
->devs
[i
].devnum
].rdev
;
2517 /* bio has the data to be written to slot 'i' where
2518 * we just recently had a write error.
2519 * We repeatedly clone the bio and trim down to one block,
2520 * then try the write. Where the write fails we record
2522 * It is conceivable that the bio doesn't exactly align with
2523 * blocks. We must handle this.
2525 * We currently own a reference to the rdev.
2531 int sect_to_write
= r10_bio
->sectors
;
2534 if (rdev
->badblocks
.shift
< 0)
2537 block_sectors
= roundup(1 << rdev
->badblocks
.shift
,
2538 bdev_logical_block_size(rdev
->bdev
) >> 9);
2539 sector
= r10_bio
->sector
;
2540 sectors
= ((r10_bio
->sector
+ block_sectors
)
2541 & ~(sector_t
)(block_sectors
- 1))
2544 while (sect_to_write
) {
2547 if (sectors
> sect_to_write
)
2548 sectors
= sect_to_write
;
2549 /* Write at 'sector' for 'sectors' */
2550 wbio
= bio_clone_fast(bio
, GFP_NOIO
, mddev
->bio_set
);
2551 bio_trim(wbio
, sector
- bio
->bi_iter
.bi_sector
, sectors
);
2552 wsector
= r10_bio
->devs
[i
].addr
+ (sector
- r10_bio
->sector
);
2553 wbio
->bi_iter
.bi_sector
= wsector
+
2554 choose_data_offset(r10_bio
, rdev
);
2555 wbio
->bi_bdev
= rdev
->bdev
;
2556 bio_set_op_attrs(wbio
, REQ_OP_WRITE
, 0);
2558 if (submit_bio_wait(wbio
) < 0)
2560 ok
= rdev_set_badblocks(rdev
, wsector
,
2565 sect_to_write
-= sectors
;
2567 sectors
= block_sectors
;
2572 static void handle_read_error(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2574 int slot
= r10_bio
->read_slot
;
2576 struct r10conf
*conf
= mddev
->private;
2577 struct md_rdev
*rdev
= r10_bio
->devs
[slot
].rdev
;
2579 sector_t bio_last_sector
;
2581 /* we got a read error. Maybe the drive is bad. Maybe just
2582 * the block and we can fix it.
2583 * We freeze all other IO, and try reading the block from
2584 * other devices. When we find one, we re-write
2585 * and check it that fixes the read error.
2586 * This is all done synchronously while the array is
2589 bio
= r10_bio
->devs
[slot
].bio
;
2590 bio_dev
= bio
->bi_bdev
->bd_dev
;
2591 bio_last_sector
= r10_bio
->devs
[slot
].addr
+ rdev
->data_offset
+ r10_bio
->sectors
;
2593 r10_bio
->devs
[slot
].bio
= NULL
;
2596 r10_bio
->devs
[slot
].bio
= IO_BLOCKED
;
2597 else if (!test_bit(FailFast
, &rdev
->flags
)) {
2598 freeze_array(conf
, 1);
2599 fix_read_error(conf
, mddev
, r10_bio
);
2600 unfreeze_array(conf
);
2602 md_error(mddev
, rdev
);
2604 rdev_dec_pending(rdev
, mddev
);
2605 allow_barrier(conf
);
2607 raid10_read_request(mddev
, r10_bio
->master_bio
, r10_bio
);
2610 static void handle_write_completed(struct r10conf
*conf
, struct r10bio
*r10_bio
)
2612 /* Some sort of write request has finished and it
2613 * succeeded in writing where we thought there was a
2614 * bad block. So forget the bad block.
2615 * Or possibly if failed and we need to record
2619 struct md_rdev
*rdev
;
2621 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
) ||
2622 test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
2623 for (m
= 0; m
< conf
->copies
; m
++) {
2624 int dev
= r10_bio
->devs
[m
].devnum
;
2625 rdev
= conf
->mirrors
[dev
].rdev
;
2626 if (r10_bio
->devs
[m
].bio
== NULL
)
2628 if (!r10_bio
->devs
[m
].bio
->bi_status
) {
2629 rdev_clear_badblocks(
2631 r10_bio
->devs
[m
].addr
,
2632 r10_bio
->sectors
, 0);
2634 if (!rdev_set_badblocks(
2636 r10_bio
->devs
[m
].addr
,
2637 r10_bio
->sectors
, 0))
2638 md_error(conf
->mddev
, rdev
);
2640 rdev
= conf
->mirrors
[dev
].replacement
;
2641 if (r10_bio
->devs
[m
].repl_bio
== NULL
)
2644 if (!r10_bio
->devs
[m
].repl_bio
->bi_status
) {
2645 rdev_clear_badblocks(
2647 r10_bio
->devs
[m
].addr
,
2648 r10_bio
->sectors
, 0);
2650 if (!rdev_set_badblocks(
2652 r10_bio
->devs
[m
].addr
,
2653 r10_bio
->sectors
, 0))
2654 md_error(conf
->mddev
, rdev
);
2660 for (m
= 0; m
< conf
->copies
; m
++) {
2661 int dev
= r10_bio
->devs
[m
].devnum
;
2662 struct bio
*bio
= r10_bio
->devs
[m
].bio
;
2663 rdev
= conf
->mirrors
[dev
].rdev
;
2664 if (bio
== IO_MADE_GOOD
) {
2665 rdev_clear_badblocks(
2667 r10_bio
->devs
[m
].addr
,
2668 r10_bio
->sectors
, 0);
2669 rdev_dec_pending(rdev
, conf
->mddev
);
2670 } else if (bio
!= NULL
&& bio
->bi_status
) {
2672 if (!narrow_write_error(r10_bio
, m
)) {
2673 md_error(conf
->mddev
, rdev
);
2674 set_bit(R10BIO_Degraded
,
2677 rdev_dec_pending(rdev
, conf
->mddev
);
2679 bio
= r10_bio
->devs
[m
].repl_bio
;
2680 rdev
= conf
->mirrors
[dev
].replacement
;
2681 if (rdev
&& bio
== IO_MADE_GOOD
) {
2682 rdev_clear_badblocks(
2684 r10_bio
->devs
[m
].addr
,
2685 r10_bio
->sectors
, 0);
2686 rdev_dec_pending(rdev
, conf
->mddev
);
2690 spin_lock_irq(&conf
->device_lock
);
2691 list_add(&r10_bio
->retry_list
, &conf
->bio_end_io_list
);
2693 spin_unlock_irq(&conf
->device_lock
);
2695 * In case freeze_array() is waiting for condition
2696 * nr_pending == nr_queued + extra to be true.
2698 wake_up(&conf
->wait_barrier
);
2699 md_wakeup_thread(conf
->mddev
->thread
);
2701 if (test_bit(R10BIO_WriteError
,
2703 close_write(r10_bio
);
2704 raid_end_bio_io(r10_bio
);
2709 static void raid10d(struct md_thread
*thread
)
2711 struct mddev
*mddev
= thread
->mddev
;
2712 struct r10bio
*r10_bio
;
2713 unsigned long flags
;
2714 struct r10conf
*conf
= mddev
->private;
2715 struct list_head
*head
= &conf
->retry_list
;
2716 struct blk_plug plug
;
2718 md_check_recovery(mddev
);
2720 if (!list_empty_careful(&conf
->bio_end_io_list
) &&
2721 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
2723 spin_lock_irqsave(&conf
->device_lock
, flags
);
2724 if (!test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
2725 while (!list_empty(&conf
->bio_end_io_list
)) {
2726 list_move(conf
->bio_end_io_list
.prev
, &tmp
);
2730 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2731 while (!list_empty(&tmp
)) {
2732 r10_bio
= list_first_entry(&tmp
, struct r10bio
,
2734 list_del(&r10_bio
->retry_list
);
2735 if (mddev
->degraded
)
2736 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
2738 if (test_bit(R10BIO_WriteError
,
2740 close_write(r10_bio
);
2741 raid_end_bio_io(r10_bio
);
2745 blk_start_plug(&plug
);
2748 flush_pending_writes(conf
);
2750 spin_lock_irqsave(&conf
->device_lock
, flags
);
2751 if (list_empty(head
)) {
2752 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2755 r10_bio
= list_entry(head
->prev
, struct r10bio
, retry_list
);
2756 list_del(head
->prev
);
2758 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2760 mddev
= r10_bio
->mddev
;
2761 conf
= mddev
->private;
2762 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
2763 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
2764 handle_write_completed(conf
, r10_bio
);
2765 else if (test_bit(R10BIO_IsReshape
, &r10_bio
->state
))
2766 reshape_request_write(mddev
, r10_bio
);
2767 else if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
2768 sync_request_write(mddev
, r10_bio
);
2769 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
2770 recovery_request_write(mddev
, r10_bio
);
2771 else if (test_bit(R10BIO_ReadError
, &r10_bio
->state
))
2772 handle_read_error(mddev
, r10_bio
);
2777 if (mddev
->sb_flags
& ~(1<<MD_SB_CHANGE_PENDING
))
2778 md_check_recovery(mddev
);
2780 blk_finish_plug(&plug
);
2783 static int init_resync(struct r10conf
*conf
)
2788 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
2789 BUG_ON(conf
->r10buf_pool
);
2790 conf
->have_replacement
= 0;
2791 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++)
2792 if (conf
->mirrors
[i
].replacement
)
2793 conf
->have_replacement
= 1;
2794 conf
->r10buf_pool
= mempool_create(buffs
, r10buf_pool_alloc
, r10buf_pool_free
, conf
);
2795 if (!conf
->r10buf_pool
)
2797 conf
->next_resync
= 0;
2801 static struct r10bio
*raid10_alloc_init_r10buf(struct r10conf
*conf
)
2803 struct r10bio
*r10bio
= mempool_alloc(conf
->r10buf_pool
, GFP_NOIO
);
2804 struct rsync_pages
*rp
;
2809 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
) ||
2810 test_bit(MD_RECOVERY_RESHAPE
, &conf
->mddev
->recovery
))
2811 nalloc
= conf
->copies
; /* resync */
2813 nalloc
= 2; /* recovery */
2815 for (i
= 0; i
< nalloc
; i
++) {
2816 bio
= r10bio
->devs
[i
].bio
;
2817 rp
= bio
->bi_private
;
2819 bio
->bi_private
= rp
;
2820 bio
= r10bio
->devs
[i
].repl_bio
;
2822 rp
= bio
->bi_private
;
2824 bio
->bi_private
= rp
;
2831 * perform a "sync" on one "block"
2833 * We need to make sure that no normal I/O request - particularly write
2834 * requests - conflict with active sync requests.
2836 * This is achieved by tracking pending requests and a 'barrier' concept
2837 * that can be installed to exclude normal IO requests.
2839 * Resync and recovery are handled very differently.
2840 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2842 * For resync, we iterate over virtual addresses, read all copies,
2843 * and update if there are differences. If only one copy is live,
2845 * For recovery, we iterate over physical addresses, read a good
2846 * value for each non-in_sync drive, and over-write.
2848 * So, for recovery we may have several outstanding complex requests for a
2849 * given address, one for each out-of-sync device. We model this by allocating
2850 * a number of r10_bio structures, one for each out-of-sync device.
2851 * As we setup these structures, we collect all bio's together into a list
2852 * which we then process collectively to add pages, and then process again
2853 * to pass to generic_make_request.
2855 * The r10_bio structures are linked using a borrowed master_bio pointer.
2856 * This link is counted in ->remaining. When the r10_bio that points to NULL
2857 * has its remaining count decremented to 0, the whole complex operation
2862 static sector_t
raid10_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
2865 struct r10conf
*conf
= mddev
->private;
2866 struct r10bio
*r10_bio
;
2867 struct bio
*biolist
= NULL
, *bio
;
2868 sector_t max_sector
, nr_sectors
;
2871 sector_t sync_blocks
;
2872 sector_t sectors_skipped
= 0;
2873 int chunks_skipped
= 0;
2874 sector_t chunk_mask
= conf
->geo
.chunk_mask
;
2877 if (!conf
->r10buf_pool
)
2878 if (init_resync(conf
))
2882 * Allow skipping a full rebuild for incremental assembly
2883 * of a clean array, like RAID1 does.
2885 if (mddev
->bitmap
== NULL
&&
2886 mddev
->recovery_cp
== MaxSector
&&
2887 mddev
->reshape_position
== MaxSector
&&
2888 !test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
2889 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
2890 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2891 conf
->fullsync
== 0) {
2893 return mddev
->dev_sectors
- sector_nr
;
2897 max_sector
= mddev
->dev_sectors
;
2898 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
2899 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2900 max_sector
= mddev
->resync_max_sectors
;
2901 if (sector_nr
>= max_sector
) {
2902 /* If we aborted, we need to abort the
2903 * sync on the 'current' bitmap chucks (there can
2904 * be several when recovering multiple devices).
2905 * as we may have started syncing it but not finished.
2906 * We can find the current address in
2907 * mddev->curr_resync, but for recovery,
2908 * we need to convert that to several
2909 * virtual addresses.
2911 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
2917 if (mddev
->curr_resync
< max_sector
) { /* aborted */
2918 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2919 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
2921 else for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2923 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
2924 bitmap_end_sync(mddev
->bitmap
, sect
,
2928 /* completed sync */
2929 if ((!mddev
->bitmap
|| conf
->fullsync
)
2930 && conf
->have_replacement
2931 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2932 /* Completed a full sync so the replacements
2933 * are now fully recovered.
2936 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2937 struct md_rdev
*rdev
=
2938 rcu_dereference(conf
->mirrors
[i
].replacement
);
2940 rdev
->recovery_offset
= MaxSector
;
2946 bitmap_close_sync(mddev
->bitmap
);
2949 return sectors_skipped
;
2952 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2953 return reshape_request(mddev
, sector_nr
, skipped
);
2955 if (chunks_skipped
>= conf
->geo
.raid_disks
) {
2956 /* if there has been nothing to do on any drive,
2957 * then there is nothing to do at all..
2960 return (max_sector
- sector_nr
) + sectors_skipped
;
2963 if (max_sector
> mddev
->resync_max
)
2964 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
2966 /* make sure whole request will fit in a chunk - if chunks
2969 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
&&
2970 max_sector
> (sector_nr
| chunk_mask
))
2971 max_sector
= (sector_nr
| chunk_mask
) + 1;
2974 * If there is non-resync activity waiting for a turn, then let it
2975 * though before starting on this new sync request.
2977 if (conf
->nr_waiting
)
2978 schedule_timeout_uninterruptible(1);
2980 /* Again, very different code for resync and recovery.
2981 * Both must result in an r10bio with a list of bios that
2982 * have bi_end_io, bi_sector, bi_bdev set,
2983 * and bi_private set to the r10bio.
2984 * For recovery, we may actually create several r10bios
2985 * with 2 bios in each, that correspond to the bios in the main one.
2986 * In this case, the subordinate r10bios link back through a
2987 * borrowed master_bio pointer, and the counter in the master
2988 * includes a ref from each subordinate.
2990 /* First, we decide what to do and set ->bi_end_io
2991 * To end_sync_read if we want to read, and
2992 * end_sync_write if we will want to write.
2995 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
2996 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2997 /* recovery... the complicated one */
3001 for (i
= 0 ; i
< conf
->geo
.raid_disks
; i
++) {
3007 struct raid10_info
*mirror
= &conf
->mirrors
[i
];
3008 struct md_rdev
*mrdev
, *mreplace
;
3011 mrdev
= rcu_dereference(mirror
->rdev
);
3012 mreplace
= rcu_dereference(mirror
->replacement
);
3014 if ((mrdev
== NULL
||
3015 test_bit(Faulty
, &mrdev
->flags
) ||
3016 test_bit(In_sync
, &mrdev
->flags
)) &&
3017 (mreplace
== NULL
||
3018 test_bit(Faulty
, &mreplace
->flags
))) {
3024 /* want to reconstruct this device */
3026 sect
= raid10_find_virt(conf
, sector_nr
, i
);
3027 if (sect
>= mddev
->resync_max_sectors
) {
3028 /* last stripe is not complete - don't
3029 * try to recover this sector.
3034 if (mreplace
&& test_bit(Faulty
, &mreplace
->flags
))
3036 /* Unless we are doing a full sync, or a replacement
3037 * we only need to recover the block if it is set in
3040 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
3042 if (sync_blocks
< max_sync
)
3043 max_sync
= sync_blocks
;
3047 /* yep, skip the sync_blocks here, but don't assume
3048 * that there will never be anything to do here
3050 chunks_skipped
= -1;
3054 atomic_inc(&mrdev
->nr_pending
);
3056 atomic_inc(&mreplace
->nr_pending
);
3059 r10_bio
= raid10_alloc_init_r10buf(conf
);
3061 raise_barrier(conf
, rb2
!= NULL
);
3062 atomic_set(&r10_bio
->remaining
, 0);
3064 r10_bio
->master_bio
= (struct bio
*)rb2
;
3066 atomic_inc(&rb2
->remaining
);
3067 r10_bio
->mddev
= mddev
;
3068 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
3069 r10_bio
->sector
= sect
;
3071 raid10_find_phys(conf
, r10_bio
);
3073 /* Need to check if the array will still be
3077 for (j
= 0; j
< conf
->geo
.raid_disks
; j
++) {
3078 struct md_rdev
*rdev
= rcu_dereference(
3079 conf
->mirrors
[j
].rdev
);
3080 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3086 must_sync
= bitmap_start_sync(mddev
->bitmap
, sect
,
3087 &sync_blocks
, still_degraded
);
3090 for (j
=0; j
<conf
->copies
;j
++) {
3092 int d
= r10_bio
->devs
[j
].devnum
;
3093 sector_t from_addr
, to_addr
;
3094 struct md_rdev
*rdev
=
3095 rcu_dereference(conf
->mirrors
[d
].rdev
);
3096 sector_t sector
, first_bad
;
3099 !test_bit(In_sync
, &rdev
->flags
))
3101 /* This is where we read from */
3103 sector
= r10_bio
->devs
[j
].addr
;
3105 if (is_badblock(rdev
, sector
, max_sync
,
3106 &first_bad
, &bad_sectors
)) {
3107 if (first_bad
> sector
)
3108 max_sync
= first_bad
- sector
;
3110 bad_sectors
-= (sector
3112 if (max_sync
> bad_sectors
)
3113 max_sync
= bad_sectors
;
3117 bio
= r10_bio
->devs
[0].bio
;
3118 bio
->bi_next
= biolist
;
3120 bio
->bi_end_io
= end_sync_read
;
3121 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
3122 if (test_bit(FailFast
, &rdev
->flags
))
3123 bio
->bi_opf
|= MD_FAILFAST
;
3124 from_addr
= r10_bio
->devs
[j
].addr
;
3125 bio
->bi_iter
.bi_sector
= from_addr
+
3127 bio
->bi_bdev
= rdev
->bdev
;
3128 atomic_inc(&rdev
->nr_pending
);
3129 /* and we write to 'i' (if not in_sync) */
3131 for (k
=0; k
<conf
->copies
; k
++)
3132 if (r10_bio
->devs
[k
].devnum
== i
)
3134 BUG_ON(k
== conf
->copies
);
3135 to_addr
= r10_bio
->devs
[k
].addr
;
3136 r10_bio
->devs
[0].devnum
= d
;
3137 r10_bio
->devs
[0].addr
= from_addr
;
3138 r10_bio
->devs
[1].devnum
= i
;
3139 r10_bio
->devs
[1].addr
= to_addr
;
3141 if (!test_bit(In_sync
, &mrdev
->flags
)) {
3142 bio
= r10_bio
->devs
[1].bio
;
3143 bio
->bi_next
= biolist
;
3145 bio
->bi_end_io
= end_sync_write
;
3146 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3147 bio
->bi_iter
.bi_sector
= to_addr
3148 + mrdev
->data_offset
;
3149 bio
->bi_bdev
= mrdev
->bdev
;
3150 atomic_inc(&r10_bio
->remaining
);
3152 r10_bio
->devs
[1].bio
->bi_end_io
= NULL
;
3154 /* and maybe write to replacement */
3155 bio
= r10_bio
->devs
[1].repl_bio
;
3157 bio
->bi_end_io
= NULL
;
3158 /* Note: if mreplace != NULL, then bio
3159 * cannot be NULL as r10buf_pool_alloc will
3160 * have allocated it.
3161 * So the second test here is pointless.
3162 * But it keeps semantic-checkers happy, and
3163 * this comment keeps human reviewers
3166 if (mreplace
== NULL
|| bio
== NULL
||
3167 test_bit(Faulty
, &mreplace
->flags
))
3169 bio
->bi_next
= biolist
;
3171 bio
->bi_end_io
= end_sync_write
;
3172 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3173 bio
->bi_iter
.bi_sector
= to_addr
+
3174 mreplace
->data_offset
;
3175 bio
->bi_bdev
= mreplace
->bdev
;
3176 atomic_inc(&r10_bio
->remaining
);
3180 if (j
== conf
->copies
) {
3181 /* Cannot recover, so abort the recovery or
3182 * record a bad block */
3184 /* problem is that there are bad blocks
3185 * on other device(s)
3188 for (k
= 0; k
< conf
->copies
; k
++)
3189 if (r10_bio
->devs
[k
].devnum
== i
)
3191 if (!test_bit(In_sync
,
3193 && !rdev_set_badblocks(
3195 r10_bio
->devs
[k
].addr
,
3199 !rdev_set_badblocks(
3201 r10_bio
->devs
[k
].addr
,
3206 if (!test_and_set_bit(MD_RECOVERY_INTR
,
3208 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3210 mirror
->recovery_disabled
3211 = mddev
->recovery_disabled
;
3215 atomic_dec(&rb2
->remaining
);
3217 rdev_dec_pending(mrdev
, mddev
);
3219 rdev_dec_pending(mreplace
, mddev
);
3222 rdev_dec_pending(mrdev
, mddev
);
3224 rdev_dec_pending(mreplace
, mddev
);
3225 if (r10_bio
->devs
[0].bio
->bi_opf
& MD_FAILFAST
) {
3226 /* Only want this if there is elsewhere to
3227 * read from. 'j' is currently the first
3231 for (; j
< conf
->copies
; j
++) {
3232 int d
= r10_bio
->devs
[j
].devnum
;
3233 if (conf
->mirrors
[d
].rdev
&&
3235 &conf
->mirrors
[d
].rdev
->flags
))
3239 r10_bio
->devs
[0].bio
->bi_opf
3243 if (biolist
== NULL
) {
3245 struct r10bio
*rb2
= r10_bio
;
3246 r10_bio
= (struct r10bio
*) rb2
->master_bio
;
3247 rb2
->master_bio
= NULL
;
3253 /* resync. Schedule a read for every block at this virt offset */
3256 bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
, 0);
3258 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
,
3259 &sync_blocks
, mddev
->degraded
) &&
3260 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
3261 &mddev
->recovery
)) {
3262 /* We can skip this block */
3264 return sync_blocks
+ sectors_skipped
;
3266 if (sync_blocks
< max_sync
)
3267 max_sync
= sync_blocks
;
3268 r10_bio
= raid10_alloc_init_r10buf(conf
);
3271 r10_bio
->mddev
= mddev
;
3272 atomic_set(&r10_bio
->remaining
, 0);
3273 raise_barrier(conf
, 0);
3274 conf
->next_resync
= sector_nr
;
3276 r10_bio
->master_bio
= NULL
;
3277 r10_bio
->sector
= sector_nr
;
3278 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
3279 raid10_find_phys(conf
, r10_bio
);
3280 r10_bio
->sectors
= (sector_nr
| chunk_mask
) - sector_nr
+ 1;
3282 for (i
= 0; i
< conf
->copies
; i
++) {
3283 int d
= r10_bio
->devs
[i
].devnum
;
3284 sector_t first_bad
, sector
;
3286 struct md_rdev
*rdev
;
3288 if (r10_bio
->devs
[i
].repl_bio
)
3289 r10_bio
->devs
[i
].repl_bio
->bi_end_io
= NULL
;
3291 bio
= r10_bio
->devs
[i
].bio
;
3292 bio
->bi_status
= BLK_STS_IOERR
;
3294 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
3295 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3299 sector
= r10_bio
->devs
[i
].addr
;
3300 if (is_badblock(rdev
, sector
, max_sync
,
3301 &first_bad
, &bad_sectors
)) {
3302 if (first_bad
> sector
)
3303 max_sync
= first_bad
- sector
;
3305 bad_sectors
-= (sector
- first_bad
);
3306 if (max_sync
> bad_sectors
)
3307 max_sync
= bad_sectors
;
3312 atomic_inc(&rdev
->nr_pending
);
3313 atomic_inc(&r10_bio
->remaining
);
3314 bio
->bi_next
= biolist
;
3316 bio
->bi_end_io
= end_sync_read
;
3317 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
3318 if (test_bit(FailFast
, &rdev
->flags
))
3319 bio
->bi_opf
|= MD_FAILFAST
;
3320 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
3321 bio
->bi_bdev
= rdev
->bdev
;
3324 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
3325 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3329 atomic_inc(&rdev
->nr_pending
);
3331 /* Need to set up for writing to the replacement */
3332 bio
= r10_bio
->devs
[i
].repl_bio
;
3333 bio
->bi_status
= BLK_STS_IOERR
;
3335 sector
= r10_bio
->devs
[i
].addr
;
3336 bio
->bi_next
= biolist
;
3338 bio
->bi_end_io
= end_sync_write
;
3339 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3340 if (test_bit(FailFast
, &rdev
->flags
))
3341 bio
->bi_opf
|= MD_FAILFAST
;
3342 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
3343 bio
->bi_bdev
= rdev
->bdev
;
3349 for (i
=0; i
<conf
->copies
; i
++) {
3350 int d
= r10_bio
->devs
[i
].devnum
;
3351 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
3352 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
3354 if (r10_bio
->devs
[i
].repl_bio
&&
3355 r10_bio
->devs
[i
].repl_bio
->bi_end_io
)
3357 conf
->mirrors
[d
].replacement
,
3367 if (sector_nr
+ max_sync
< max_sector
)
3368 max_sector
= sector_nr
+ max_sync
;
3371 int len
= PAGE_SIZE
;
3372 if (sector_nr
+ (len
>>9) > max_sector
)
3373 len
= (max_sector
- sector_nr
) << 9;
3376 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
3377 struct resync_pages
*rp
= get_resync_pages(bio
);
3378 page
= resync_fetch_page(rp
, page_idx
);
3380 * won't fail because the vec table is big enough
3381 * to hold all these pages
3383 bio_add_page(bio
, page
, len
, 0);
3385 nr_sectors
+= len
>>9;
3386 sector_nr
+= len
>>9;
3387 } while (++page_idx
< RESYNC_PAGES
);
3388 r10_bio
->sectors
= nr_sectors
;
3392 biolist
= biolist
->bi_next
;
3394 bio
->bi_next
= NULL
;
3395 r10_bio
= get_resync_r10bio(bio
);
3396 r10_bio
->sectors
= nr_sectors
;
3398 if (bio
->bi_end_io
== end_sync_read
) {
3399 md_sync_acct(bio
->bi_bdev
, nr_sectors
);
3401 generic_make_request(bio
);
3405 if (sectors_skipped
)
3406 /* pretend they weren't skipped, it makes
3407 * no important difference in this case
3409 md_done_sync(mddev
, sectors_skipped
, 1);
3411 return sectors_skipped
+ nr_sectors
;
3413 /* There is nowhere to write, so all non-sync
3414 * drives must be failed or in resync, all drives
3415 * have a bad block, so try the next chunk...
3417 if (sector_nr
+ max_sync
< max_sector
)
3418 max_sector
= sector_nr
+ max_sync
;
3420 sectors_skipped
+= (max_sector
- sector_nr
);
3422 sector_nr
= max_sector
;
3427 raid10_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
3430 struct r10conf
*conf
= mddev
->private;
3433 raid_disks
= min(conf
->geo
.raid_disks
,
3434 conf
->prev
.raid_disks
);
3436 sectors
= conf
->dev_sectors
;
3438 size
= sectors
>> conf
->geo
.chunk_shift
;
3439 sector_div(size
, conf
->geo
.far_copies
);
3440 size
= size
* raid_disks
;
3441 sector_div(size
, conf
->geo
.near_copies
);
3443 return size
<< conf
->geo
.chunk_shift
;
3446 static void calc_sectors(struct r10conf
*conf
, sector_t size
)
3448 /* Calculate the number of sectors-per-device that will
3449 * actually be used, and set conf->dev_sectors and
3453 size
= size
>> conf
->geo
.chunk_shift
;
3454 sector_div(size
, conf
->geo
.far_copies
);
3455 size
= size
* conf
->geo
.raid_disks
;
3456 sector_div(size
, conf
->geo
.near_copies
);
3457 /* 'size' is now the number of chunks in the array */
3458 /* calculate "used chunks per device" */
3459 size
= size
* conf
->copies
;
3461 /* We need to round up when dividing by raid_disks to
3462 * get the stride size.
3464 size
= DIV_ROUND_UP_SECTOR_T(size
, conf
->geo
.raid_disks
);
3466 conf
->dev_sectors
= size
<< conf
->geo
.chunk_shift
;
3468 if (conf
->geo
.far_offset
)
3469 conf
->geo
.stride
= 1 << conf
->geo
.chunk_shift
;
3471 sector_div(size
, conf
->geo
.far_copies
);
3472 conf
->geo
.stride
= size
<< conf
->geo
.chunk_shift
;
3476 enum geo_type
{geo_new
, geo_old
, geo_start
};
3477 static int setup_geo(struct geom
*geo
, struct mddev
*mddev
, enum geo_type
new)
3480 int layout
, chunk
, disks
;
3483 layout
= mddev
->layout
;
3484 chunk
= mddev
->chunk_sectors
;
3485 disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3488 layout
= mddev
->new_layout
;
3489 chunk
= mddev
->new_chunk_sectors
;
3490 disks
= mddev
->raid_disks
;
3492 default: /* avoid 'may be unused' warnings */
3493 case geo_start
: /* new when starting reshape - raid_disks not
3495 layout
= mddev
->new_layout
;
3496 chunk
= mddev
->new_chunk_sectors
;
3497 disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
3502 if (chunk
< (PAGE_SIZE
>> 9) ||
3503 !is_power_of_2(chunk
))
3506 fc
= (layout
>> 8) & 255;
3507 fo
= layout
& (1<<16);
3508 geo
->raid_disks
= disks
;
3509 geo
->near_copies
= nc
;
3510 geo
->far_copies
= fc
;
3511 geo
->far_offset
= fo
;
3512 switch (layout
>> 17) {
3513 case 0: /* original layout. simple but not always optimal */
3514 geo
->far_set_size
= disks
;
3516 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3517 * actually using this, but leave code here just in case.*/
3518 geo
->far_set_size
= disks
/fc
;
3519 WARN(geo
->far_set_size
< fc
,
3520 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3522 case 2: /* "improved" layout fixed to match documentation */
3523 geo
->far_set_size
= fc
* nc
;
3525 default: /* Not a valid layout */
3528 geo
->chunk_mask
= chunk
- 1;
3529 geo
->chunk_shift
= ffz(~chunk
);
3533 static struct r10conf
*setup_conf(struct mddev
*mddev
)
3535 struct r10conf
*conf
= NULL
;
3540 copies
= setup_geo(&geo
, mddev
, geo_new
);
3543 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3544 mdname(mddev
), PAGE_SIZE
);
3548 if (copies
< 2 || copies
> mddev
->raid_disks
) {
3549 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3550 mdname(mddev
), mddev
->new_layout
);
3555 conf
= kzalloc(sizeof(struct r10conf
), GFP_KERNEL
);
3559 /* FIXME calc properly */
3560 conf
->mirrors
= kzalloc(sizeof(struct raid10_info
)*(mddev
->raid_disks
+
3561 max(0,-mddev
->delta_disks
)),
3566 conf
->tmppage
= alloc_page(GFP_KERNEL
);
3571 conf
->copies
= copies
;
3572 conf
->r10bio_pool
= mempool_create(NR_RAID10_BIOS
, r10bio_pool_alloc
,
3573 r10bio_pool_free
, conf
);
3574 if (!conf
->r10bio_pool
)
3577 conf
->bio_split
= bioset_create(BIO_POOL_SIZE
, 0, 0);
3578 if (!conf
->bio_split
)
3581 calc_sectors(conf
, mddev
->dev_sectors
);
3582 if (mddev
->reshape_position
== MaxSector
) {
3583 conf
->prev
= conf
->geo
;
3584 conf
->reshape_progress
= MaxSector
;
3586 if (setup_geo(&conf
->prev
, mddev
, geo_old
) != conf
->copies
) {
3590 conf
->reshape_progress
= mddev
->reshape_position
;
3591 if (conf
->prev
.far_offset
)
3592 conf
->prev
.stride
= 1 << conf
->prev
.chunk_shift
;
3594 /* far_copies must be 1 */
3595 conf
->prev
.stride
= conf
->dev_sectors
;
3597 conf
->reshape_safe
= conf
->reshape_progress
;
3598 spin_lock_init(&conf
->device_lock
);
3599 INIT_LIST_HEAD(&conf
->retry_list
);
3600 INIT_LIST_HEAD(&conf
->bio_end_io_list
);
3602 spin_lock_init(&conf
->resync_lock
);
3603 init_waitqueue_head(&conf
->wait_barrier
);
3604 atomic_set(&conf
->nr_pending
, 0);
3606 conf
->thread
= md_register_thread(raid10d
, mddev
, "raid10");
3610 conf
->mddev
= mddev
;
3615 mempool_destroy(conf
->r10bio_pool
);
3616 kfree(conf
->mirrors
);
3617 safe_put_page(conf
->tmppage
);
3618 if (conf
->bio_split
)
3619 bioset_free(conf
->bio_split
);
3622 return ERR_PTR(err
);
3625 static int raid10_run(struct mddev
*mddev
)
3627 struct r10conf
*conf
;
3628 int i
, disk_idx
, chunk_size
;
3629 struct raid10_info
*disk
;
3630 struct md_rdev
*rdev
;
3632 sector_t min_offset_diff
= 0;
3634 bool discard_supported
= false;
3636 if (mddev_init_writes_pending(mddev
) < 0)
3639 if (mddev
->private == NULL
) {
3640 conf
= setup_conf(mddev
);
3642 return PTR_ERR(conf
);
3643 mddev
->private = conf
;
3645 conf
= mddev
->private;
3649 mddev
->thread
= conf
->thread
;
3650 conf
->thread
= NULL
;
3652 chunk_size
= mddev
->chunk_sectors
<< 9;
3654 blk_queue_max_discard_sectors(mddev
->queue
,
3655 mddev
->chunk_sectors
);
3656 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
3657 blk_queue_max_write_zeroes_sectors(mddev
->queue
, 0);
3658 blk_queue_io_min(mddev
->queue
, chunk_size
);
3659 if (conf
->geo
.raid_disks
% conf
->geo
.near_copies
)
3660 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->geo
.raid_disks
);
3662 blk_queue_io_opt(mddev
->queue
, chunk_size
*
3663 (conf
->geo
.raid_disks
/ conf
->geo
.near_copies
));
3666 rdev_for_each(rdev
, mddev
) {
3669 disk_idx
= rdev
->raid_disk
;
3672 if (disk_idx
>= conf
->geo
.raid_disks
&&
3673 disk_idx
>= conf
->prev
.raid_disks
)
3675 disk
= conf
->mirrors
+ disk_idx
;
3677 if (test_bit(Replacement
, &rdev
->flags
)) {
3678 if (disk
->replacement
)
3680 disk
->replacement
= rdev
;
3686 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
3687 if (!mddev
->reshape_backwards
)
3691 if (first
|| diff
< min_offset_diff
)
3692 min_offset_diff
= diff
;
3695 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
3696 rdev
->data_offset
<< 9);
3698 disk
->head_position
= 0;
3700 if (blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
3701 discard_supported
= true;
3706 if (discard_supported
)
3707 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD
,
3710 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD
,
3713 /* need to check that every block has at least one working mirror */
3714 if (!enough(conf
, -1)) {
3715 pr_err("md/raid10:%s: not enough operational mirrors.\n",
3720 if (conf
->reshape_progress
!= MaxSector
) {
3721 /* must ensure that shape change is supported */
3722 if (conf
->geo
.far_copies
!= 1 &&
3723 conf
->geo
.far_offset
== 0)
3725 if (conf
->prev
.far_copies
!= 1 &&
3726 conf
->prev
.far_offset
== 0)
3730 mddev
->degraded
= 0;
3732 i
< conf
->geo
.raid_disks
3733 || i
< conf
->prev
.raid_disks
;
3736 disk
= conf
->mirrors
+ i
;
3738 if (!disk
->rdev
&& disk
->replacement
) {
3739 /* The replacement is all we have - use it */
3740 disk
->rdev
= disk
->replacement
;
3741 disk
->replacement
= NULL
;
3742 clear_bit(Replacement
, &disk
->rdev
->flags
);
3746 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
3747 disk
->head_position
= 0;
3750 disk
->rdev
->saved_raid_disk
< 0)
3753 disk
->recovery_disabled
= mddev
->recovery_disabled
- 1;
3756 if (mddev
->recovery_cp
!= MaxSector
)
3757 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3759 pr_info("md/raid10:%s: active with %d out of %d devices\n",
3760 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
,
3761 conf
->geo
.raid_disks
);
3763 * Ok, everything is just fine now
3765 mddev
->dev_sectors
= conf
->dev_sectors
;
3766 size
= raid10_size(mddev
, 0, 0);
3767 md_set_array_sectors(mddev
, size
);
3768 mddev
->resync_max_sectors
= size
;
3769 set_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
);
3772 int stripe
= conf
->geo
.raid_disks
*
3773 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
3775 /* Calculate max read-ahead size.
3776 * We need to readahead at least twice a whole stripe....
3779 stripe
/= conf
->geo
.near_copies
;
3780 if (mddev
->queue
->backing_dev_info
->ra_pages
< 2 * stripe
)
3781 mddev
->queue
->backing_dev_info
->ra_pages
= 2 * stripe
;
3784 if (md_integrity_register(mddev
))
3787 if (conf
->reshape_progress
!= MaxSector
) {
3788 unsigned long before_length
, after_length
;
3790 before_length
= ((1 << conf
->prev
.chunk_shift
) *
3791 conf
->prev
.far_copies
);
3792 after_length
= ((1 << conf
->geo
.chunk_shift
) *
3793 conf
->geo
.far_copies
);
3795 if (max(before_length
, after_length
) > min_offset_diff
) {
3796 /* This cannot work */
3797 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3800 conf
->offset_diff
= min_offset_diff
;
3802 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3803 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3804 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3805 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3806 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
3813 md_unregister_thread(&mddev
->thread
);
3814 mempool_destroy(conf
->r10bio_pool
);
3815 safe_put_page(conf
->tmppage
);
3816 kfree(conf
->mirrors
);
3818 mddev
->private = NULL
;
3823 static void raid10_free(struct mddev
*mddev
, void *priv
)
3825 struct r10conf
*conf
= priv
;
3827 mempool_destroy(conf
->r10bio_pool
);
3828 safe_put_page(conf
->tmppage
);
3829 kfree(conf
->mirrors
);
3830 kfree(conf
->mirrors_old
);
3831 kfree(conf
->mirrors_new
);
3832 if (conf
->bio_split
)
3833 bioset_free(conf
->bio_split
);
3837 static void raid10_quiesce(struct mddev
*mddev
, int state
)
3839 struct r10conf
*conf
= mddev
->private;
3843 raise_barrier(conf
, 0);
3846 lower_barrier(conf
);
3851 static int raid10_resize(struct mddev
*mddev
, sector_t sectors
)
3853 /* Resize of 'far' arrays is not supported.
3854 * For 'near' and 'offset' arrays we can set the
3855 * number of sectors used to be an appropriate multiple
3856 * of the chunk size.
3857 * For 'offset', this is far_copies*chunksize.
3858 * For 'near' the multiplier is the LCM of
3859 * near_copies and raid_disks.
3860 * So if far_copies > 1 && !far_offset, fail.
3861 * Else find LCM(raid_disks, near_copy)*far_copies and
3862 * multiply by chunk_size. Then round to this number.
3863 * This is mostly done by raid10_size()
3865 struct r10conf
*conf
= mddev
->private;
3866 sector_t oldsize
, size
;
3868 if (mddev
->reshape_position
!= MaxSector
)
3871 if (conf
->geo
.far_copies
> 1 && !conf
->geo
.far_offset
)
3874 oldsize
= raid10_size(mddev
, 0, 0);
3875 size
= raid10_size(mddev
, sectors
, 0);
3876 if (mddev
->external_size
&&
3877 mddev
->array_sectors
> size
)
3879 if (mddev
->bitmap
) {
3880 int ret
= bitmap_resize(mddev
->bitmap
, size
, 0, 0);
3884 md_set_array_sectors(mddev
, size
);
3885 if (sectors
> mddev
->dev_sectors
&&
3886 mddev
->recovery_cp
> oldsize
) {
3887 mddev
->recovery_cp
= oldsize
;
3888 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3890 calc_sectors(conf
, sectors
);
3891 mddev
->dev_sectors
= conf
->dev_sectors
;
3892 mddev
->resync_max_sectors
= size
;
3896 static void *raid10_takeover_raid0(struct mddev
*mddev
, sector_t size
, int devs
)
3898 struct md_rdev
*rdev
;
3899 struct r10conf
*conf
;
3901 if (mddev
->degraded
> 0) {
3902 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3904 return ERR_PTR(-EINVAL
);
3906 sector_div(size
, devs
);
3908 /* Set new parameters */
3909 mddev
->new_level
= 10;
3910 /* new layout: far_copies = 1, near_copies = 2 */
3911 mddev
->new_layout
= (1<<8) + 2;
3912 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
3913 mddev
->delta_disks
= mddev
->raid_disks
;
3914 mddev
->raid_disks
*= 2;
3915 /* make sure it will be not marked as dirty */
3916 mddev
->recovery_cp
= MaxSector
;
3917 mddev
->dev_sectors
= size
;
3919 conf
= setup_conf(mddev
);
3920 if (!IS_ERR(conf
)) {
3921 rdev_for_each(rdev
, mddev
)
3922 if (rdev
->raid_disk
>= 0) {
3923 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
3924 rdev
->sectors
= size
;
3932 static void *raid10_takeover(struct mddev
*mddev
)
3934 struct r0conf
*raid0_conf
;
3936 /* raid10 can take over:
3937 * raid0 - providing it has only two drives
3939 if (mddev
->level
== 0) {
3940 /* for raid0 takeover only one zone is supported */
3941 raid0_conf
= mddev
->private;
3942 if (raid0_conf
->nr_strip_zones
> 1) {
3943 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
3945 return ERR_PTR(-EINVAL
);
3947 return raid10_takeover_raid0(mddev
,
3948 raid0_conf
->strip_zone
->zone_end
,
3949 raid0_conf
->strip_zone
->nb_dev
);
3951 return ERR_PTR(-EINVAL
);
3954 static int raid10_check_reshape(struct mddev
*mddev
)
3956 /* Called when there is a request to change
3957 * - layout (to ->new_layout)
3958 * - chunk size (to ->new_chunk_sectors)
3959 * - raid_disks (by delta_disks)
3960 * or when trying to restart a reshape that was ongoing.
3962 * We need to validate the request and possibly allocate
3963 * space if that might be an issue later.
3965 * Currently we reject any reshape of a 'far' mode array,
3966 * allow chunk size to change if new is generally acceptable,
3967 * allow raid_disks to increase, and allow
3968 * a switch between 'near' mode and 'offset' mode.
3970 struct r10conf
*conf
= mddev
->private;
3973 if (conf
->geo
.far_copies
!= 1 && !conf
->geo
.far_offset
)
3976 if (setup_geo(&geo
, mddev
, geo_start
) != conf
->copies
)
3977 /* mustn't change number of copies */
3979 if (geo
.far_copies
> 1 && !geo
.far_offset
)
3980 /* Cannot switch to 'far' mode */
3983 if (mddev
->array_sectors
& geo
.chunk_mask
)
3984 /* not factor of array size */
3987 if (!enough(conf
, -1))
3990 kfree(conf
->mirrors_new
);
3991 conf
->mirrors_new
= NULL
;
3992 if (mddev
->delta_disks
> 0) {
3993 /* allocate new 'mirrors' list */
3994 conf
->mirrors_new
= kzalloc(
3995 sizeof(struct raid10_info
)
3996 *(mddev
->raid_disks
+
3997 mddev
->delta_disks
),
3999 if (!conf
->mirrors_new
)
4006 * Need to check if array has failed when deciding whether to:
4008 * - remove non-faulty devices
4011 * This determination is simple when no reshape is happening.
4012 * However if there is a reshape, we need to carefully check
4013 * both the before and after sections.
4014 * This is because some failed devices may only affect one
4015 * of the two sections, and some non-in_sync devices may
4016 * be insync in the section most affected by failed devices.
4018 static int calc_degraded(struct r10conf
*conf
)
4020 int degraded
, degraded2
;
4025 /* 'prev' section first */
4026 for (i
= 0; i
< conf
->prev
.raid_disks
; i
++) {
4027 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
4028 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4030 else if (!test_bit(In_sync
, &rdev
->flags
))
4031 /* When we can reduce the number of devices in
4032 * an array, this might not contribute to
4033 * 'degraded'. It does now.
4038 if (conf
->geo
.raid_disks
== conf
->prev
.raid_disks
)
4042 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
4043 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
4044 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4046 else if (!test_bit(In_sync
, &rdev
->flags
)) {
4047 /* If reshape is increasing the number of devices,
4048 * this section has already been recovered, so
4049 * it doesn't contribute to degraded.
4052 if (conf
->geo
.raid_disks
<= conf
->prev
.raid_disks
)
4057 if (degraded2
> degraded
)
4062 static int raid10_start_reshape(struct mddev
*mddev
)
4064 /* A 'reshape' has been requested. This commits
4065 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4066 * This also checks if there are enough spares and adds them
4068 * We currently require enough spares to make the final
4069 * array non-degraded. We also require that the difference
4070 * between old and new data_offset - on each device - is
4071 * enough that we never risk over-writing.
4074 unsigned long before_length
, after_length
;
4075 sector_t min_offset_diff
= 0;
4078 struct r10conf
*conf
= mddev
->private;
4079 struct md_rdev
*rdev
;
4083 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4086 if (setup_geo(&new, mddev
, geo_start
) != conf
->copies
)
4089 before_length
= ((1 << conf
->prev
.chunk_shift
) *
4090 conf
->prev
.far_copies
);
4091 after_length
= ((1 << conf
->geo
.chunk_shift
) *
4092 conf
->geo
.far_copies
);
4094 rdev_for_each(rdev
, mddev
) {
4095 if (!test_bit(In_sync
, &rdev
->flags
)
4096 && !test_bit(Faulty
, &rdev
->flags
))
4098 if (rdev
->raid_disk
>= 0) {
4099 long long diff
= (rdev
->new_data_offset
4100 - rdev
->data_offset
);
4101 if (!mddev
->reshape_backwards
)
4105 if (first
|| diff
< min_offset_diff
)
4106 min_offset_diff
= diff
;
4111 if (max(before_length
, after_length
) > min_offset_diff
)
4114 if (spares
< mddev
->delta_disks
)
4117 conf
->offset_diff
= min_offset_diff
;
4118 spin_lock_irq(&conf
->device_lock
);
4119 if (conf
->mirrors_new
) {
4120 memcpy(conf
->mirrors_new
, conf
->mirrors
,
4121 sizeof(struct raid10_info
)*conf
->prev
.raid_disks
);
4123 kfree(conf
->mirrors_old
);
4124 conf
->mirrors_old
= conf
->mirrors
;
4125 conf
->mirrors
= conf
->mirrors_new
;
4126 conf
->mirrors_new
= NULL
;
4128 setup_geo(&conf
->geo
, mddev
, geo_start
);
4130 if (mddev
->reshape_backwards
) {
4131 sector_t size
= raid10_size(mddev
, 0, 0);
4132 if (size
< mddev
->array_sectors
) {
4133 spin_unlock_irq(&conf
->device_lock
);
4134 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4138 mddev
->resync_max_sectors
= size
;
4139 conf
->reshape_progress
= size
;
4141 conf
->reshape_progress
= 0;
4142 conf
->reshape_safe
= conf
->reshape_progress
;
4143 spin_unlock_irq(&conf
->device_lock
);
4145 if (mddev
->delta_disks
&& mddev
->bitmap
) {
4146 ret
= bitmap_resize(mddev
->bitmap
,
4147 raid10_size(mddev
, 0,
4148 conf
->geo
.raid_disks
),
4153 if (mddev
->delta_disks
> 0) {
4154 rdev_for_each(rdev
, mddev
)
4155 if (rdev
->raid_disk
< 0 &&
4156 !test_bit(Faulty
, &rdev
->flags
)) {
4157 if (raid10_add_disk(mddev
, rdev
) == 0) {
4158 if (rdev
->raid_disk
>=
4159 conf
->prev
.raid_disks
)
4160 set_bit(In_sync
, &rdev
->flags
);
4162 rdev
->recovery_offset
= 0;
4164 if (sysfs_link_rdev(mddev
, rdev
))
4165 /* Failure here is OK */;
4167 } else if (rdev
->raid_disk
>= conf
->prev
.raid_disks
4168 && !test_bit(Faulty
, &rdev
->flags
)) {
4169 /* This is a spare that was manually added */
4170 set_bit(In_sync
, &rdev
->flags
);
4173 /* When a reshape changes the number of devices,
4174 * ->degraded is measured against the larger of the
4175 * pre and post numbers.
4177 spin_lock_irq(&conf
->device_lock
);
4178 mddev
->degraded
= calc_degraded(conf
);
4179 spin_unlock_irq(&conf
->device_lock
);
4180 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4181 mddev
->reshape_position
= conf
->reshape_progress
;
4182 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4184 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4185 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4186 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
4187 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
4188 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4190 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
4192 if (!mddev
->sync_thread
) {
4196 conf
->reshape_checkpoint
= jiffies
;
4197 md_wakeup_thread(mddev
->sync_thread
);
4198 md_new_event(mddev
);
4202 mddev
->recovery
= 0;
4203 spin_lock_irq(&conf
->device_lock
);
4204 conf
->geo
= conf
->prev
;
4205 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4206 rdev_for_each(rdev
, mddev
)
4207 rdev
->new_data_offset
= rdev
->data_offset
;
4209 conf
->reshape_progress
= MaxSector
;
4210 conf
->reshape_safe
= MaxSector
;
4211 mddev
->reshape_position
= MaxSector
;
4212 spin_unlock_irq(&conf
->device_lock
);
4216 /* Calculate the last device-address that could contain
4217 * any block from the chunk that includes the array-address 's'
4218 * and report the next address.
4219 * i.e. the address returned will be chunk-aligned and after
4220 * any data that is in the chunk containing 's'.
4222 static sector_t
last_dev_address(sector_t s
, struct geom
*geo
)
4224 s
= (s
| geo
->chunk_mask
) + 1;
4225 s
>>= geo
->chunk_shift
;
4226 s
*= geo
->near_copies
;
4227 s
= DIV_ROUND_UP_SECTOR_T(s
, geo
->raid_disks
);
4228 s
*= geo
->far_copies
;
4229 s
<<= geo
->chunk_shift
;
4233 /* Calculate the first device-address that could contain
4234 * any block from the chunk that includes the array-address 's'.
4235 * This too will be the start of a chunk
4237 static sector_t
first_dev_address(sector_t s
, struct geom
*geo
)
4239 s
>>= geo
->chunk_shift
;
4240 s
*= geo
->near_copies
;
4241 sector_div(s
, geo
->raid_disks
);
4242 s
*= geo
->far_copies
;
4243 s
<<= geo
->chunk_shift
;
4247 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
4250 /* We simply copy at most one chunk (smallest of old and new)
4251 * at a time, possibly less if that exceeds RESYNC_PAGES,
4252 * or we hit a bad block or something.
4253 * This might mean we pause for normal IO in the middle of
4254 * a chunk, but that is not a problem as mddev->reshape_position
4255 * can record any location.
4257 * If we will want to write to a location that isn't
4258 * yet recorded as 'safe' (i.e. in metadata on disk) then
4259 * we need to flush all reshape requests and update the metadata.
4261 * When reshaping forwards (e.g. to more devices), we interpret
4262 * 'safe' as the earliest block which might not have been copied
4263 * down yet. We divide this by previous stripe size and multiply
4264 * by previous stripe length to get lowest device offset that we
4265 * cannot write to yet.
4266 * We interpret 'sector_nr' as an address that we want to write to.
4267 * From this we use last_device_address() to find where we might
4268 * write to, and first_device_address on the 'safe' position.
4269 * If this 'next' write position is after the 'safe' position,
4270 * we must update the metadata to increase the 'safe' position.
4272 * When reshaping backwards, we round in the opposite direction
4273 * and perform the reverse test: next write position must not be
4274 * less than current safe position.
4276 * In all this the minimum difference in data offsets
4277 * (conf->offset_diff - always positive) allows a bit of slack,
4278 * so next can be after 'safe', but not by more than offset_diff
4280 * We need to prepare all the bios here before we start any IO
4281 * to ensure the size we choose is acceptable to all devices.
4282 * The means one for each copy for write-out and an extra one for
4284 * We store the read-in bio in ->master_bio and the others in
4285 * ->devs[x].bio and ->devs[x].repl_bio.
4287 struct r10conf
*conf
= mddev
->private;
4288 struct r10bio
*r10_bio
;
4289 sector_t next
, safe
, last
;
4293 struct md_rdev
*rdev
;
4296 struct bio
*bio
, *read_bio
;
4297 int sectors_done
= 0;
4298 struct page
**pages
;
4300 if (sector_nr
== 0) {
4301 /* If restarting in the middle, skip the initial sectors */
4302 if (mddev
->reshape_backwards
&&
4303 conf
->reshape_progress
< raid10_size(mddev
, 0, 0)) {
4304 sector_nr
= (raid10_size(mddev
, 0, 0)
4305 - conf
->reshape_progress
);
4306 } else if (!mddev
->reshape_backwards
&&
4307 conf
->reshape_progress
> 0)
4308 sector_nr
= conf
->reshape_progress
;
4310 mddev
->curr_resync_completed
= sector_nr
;
4311 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4317 /* We don't use sector_nr to track where we are up to
4318 * as that doesn't work well for ->reshape_backwards.
4319 * So just use ->reshape_progress.
4321 if (mddev
->reshape_backwards
) {
4322 /* 'next' is the earliest device address that we might
4323 * write to for this chunk in the new layout
4325 next
= first_dev_address(conf
->reshape_progress
- 1,
4328 /* 'safe' is the last device address that we might read from
4329 * in the old layout after a restart
4331 safe
= last_dev_address(conf
->reshape_safe
- 1,
4334 if (next
+ conf
->offset_diff
< safe
)
4337 last
= conf
->reshape_progress
- 1;
4338 sector_nr
= last
& ~(sector_t
)(conf
->geo
.chunk_mask
4339 & conf
->prev
.chunk_mask
);
4340 if (sector_nr
+ RESYNC_BLOCK_SIZE
/512 < last
)
4341 sector_nr
= last
+ 1 - RESYNC_BLOCK_SIZE
/512;
4343 /* 'next' is after the last device address that we
4344 * might write to for this chunk in the new layout
4346 next
= last_dev_address(conf
->reshape_progress
, &conf
->geo
);
4348 /* 'safe' is the earliest device address that we might
4349 * read from in the old layout after a restart
4351 safe
= first_dev_address(conf
->reshape_safe
, &conf
->prev
);
4353 /* Need to update metadata if 'next' might be beyond 'safe'
4354 * as that would possibly corrupt data
4356 if (next
> safe
+ conf
->offset_diff
)
4359 sector_nr
= conf
->reshape_progress
;
4360 last
= sector_nr
| (conf
->geo
.chunk_mask
4361 & conf
->prev
.chunk_mask
);
4363 if (sector_nr
+ RESYNC_BLOCK_SIZE
/512 <= last
)
4364 last
= sector_nr
+ RESYNC_BLOCK_SIZE
/512 - 1;
4368 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4369 /* Need to update reshape_position in metadata */
4371 mddev
->reshape_position
= conf
->reshape_progress
;
4372 if (mddev
->reshape_backwards
)
4373 mddev
->curr_resync_completed
= raid10_size(mddev
, 0, 0)
4374 - conf
->reshape_progress
;
4376 mddev
->curr_resync_completed
= conf
->reshape_progress
;
4377 conf
->reshape_checkpoint
= jiffies
;
4378 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4379 md_wakeup_thread(mddev
->thread
);
4380 wait_event(mddev
->sb_wait
, mddev
->sb_flags
== 0 ||
4381 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
4382 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4383 allow_barrier(conf
);
4384 return sectors_done
;
4386 conf
->reshape_safe
= mddev
->reshape_position
;
4387 allow_barrier(conf
);
4391 /* Now schedule reads for blocks from sector_nr to last */
4392 r10_bio
= raid10_alloc_init_r10buf(conf
);
4394 raise_barrier(conf
, sectors_done
!= 0);
4395 atomic_set(&r10_bio
->remaining
, 0);
4396 r10_bio
->mddev
= mddev
;
4397 r10_bio
->sector
= sector_nr
;
4398 set_bit(R10BIO_IsReshape
, &r10_bio
->state
);
4399 r10_bio
->sectors
= last
- sector_nr
+ 1;
4400 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
4401 BUG_ON(!test_bit(R10BIO_Previous
, &r10_bio
->state
));
4404 /* Cannot read from here, so need to record bad blocks
4405 * on all the target devices.
4408 mempool_free(r10_bio
, conf
->r10buf_pool
);
4409 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4410 return sectors_done
;
4413 read_bio
= bio_alloc_mddev(GFP_KERNEL
, RESYNC_PAGES
, mddev
);
4415 read_bio
->bi_bdev
= rdev
->bdev
;
4416 read_bio
->bi_iter
.bi_sector
= (r10_bio
->devs
[r10_bio
->read_slot
].addr
4417 + rdev
->data_offset
);
4418 read_bio
->bi_private
= r10_bio
;
4419 read_bio
->bi_end_io
= end_reshape_read
;
4420 bio_set_op_attrs(read_bio
, REQ_OP_READ
, 0);
4421 read_bio
->bi_flags
&= (~0UL << BIO_RESET_BITS
);
4422 read_bio
->bi_status
= 0;
4423 read_bio
->bi_vcnt
= 0;
4424 read_bio
->bi_iter
.bi_size
= 0;
4425 r10_bio
->master_bio
= read_bio
;
4426 r10_bio
->read_slot
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
4428 /* Now find the locations in the new layout */
4429 __raid10_find_phys(&conf
->geo
, r10_bio
);
4432 read_bio
->bi_next
= NULL
;
4435 for (s
= 0; s
< conf
->copies
*2; s
++) {
4437 int d
= r10_bio
->devs
[s
/2].devnum
;
4438 struct md_rdev
*rdev2
;
4440 rdev2
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4441 b
= r10_bio
->devs
[s
/2].repl_bio
;
4443 rdev2
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4444 b
= r10_bio
->devs
[s
/2].bio
;
4446 if (!rdev2
|| test_bit(Faulty
, &rdev2
->flags
))
4449 b
->bi_bdev
= rdev2
->bdev
;
4450 b
->bi_iter
.bi_sector
= r10_bio
->devs
[s
/2].addr
+
4451 rdev2
->new_data_offset
;
4452 b
->bi_end_io
= end_reshape_write
;
4453 bio_set_op_attrs(b
, REQ_OP_WRITE
, 0);
4458 /* Now add as many pages as possible to all of these bios. */
4461 pages
= get_resync_pages(r10_bio
->devs
[0].bio
)->pages
;
4462 for (s
= 0 ; s
< max_sectors
; s
+= PAGE_SIZE
>> 9) {
4463 struct page
*page
= pages
[s
/ (PAGE_SIZE
>> 9)];
4464 int len
= (max_sectors
- s
) << 9;
4465 if (len
> PAGE_SIZE
)
4467 for (bio
= blist
; bio
; bio
= bio
->bi_next
) {
4469 * won't fail because the vec table is big enough
4470 * to hold all these pages
4472 bio_add_page(bio
, page
, len
, 0);
4474 sector_nr
+= len
>> 9;
4475 nr_sectors
+= len
>> 9;
4478 r10_bio
->sectors
= nr_sectors
;
4480 /* Now submit the read */
4481 md_sync_acct(read_bio
->bi_bdev
, r10_bio
->sectors
);
4482 atomic_inc(&r10_bio
->remaining
);
4483 read_bio
->bi_next
= NULL
;
4484 generic_make_request(read_bio
);
4485 sector_nr
+= nr_sectors
;
4486 sectors_done
+= nr_sectors
;
4487 if (sector_nr
<= last
)
4490 /* Now that we have done the whole section we can
4491 * update reshape_progress
4493 if (mddev
->reshape_backwards
)
4494 conf
->reshape_progress
-= sectors_done
;
4496 conf
->reshape_progress
+= sectors_done
;
4498 return sectors_done
;
4501 static void end_reshape_request(struct r10bio
*r10_bio
);
4502 static int handle_reshape_read_error(struct mddev
*mddev
,
4503 struct r10bio
*r10_bio
);
4504 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
4506 /* Reshape read completed. Hopefully we have a block
4508 * If we got a read error then we do sync 1-page reads from
4509 * elsewhere until we find the data - or give up.
4511 struct r10conf
*conf
= mddev
->private;
4514 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
4515 if (handle_reshape_read_error(mddev
, r10_bio
) < 0) {
4516 /* Reshape has been aborted */
4517 md_done_sync(mddev
, r10_bio
->sectors
, 0);
4521 /* We definitely have the data in the pages, schedule the
4524 atomic_set(&r10_bio
->remaining
, 1);
4525 for (s
= 0; s
< conf
->copies
*2; s
++) {
4527 int d
= r10_bio
->devs
[s
/2].devnum
;
4528 struct md_rdev
*rdev
;
4531 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4532 b
= r10_bio
->devs
[s
/2].repl_bio
;
4534 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4535 b
= r10_bio
->devs
[s
/2].bio
;
4537 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
)) {
4541 atomic_inc(&rdev
->nr_pending
);
4543 md_sync_acct(b
->bi_bdev
, r10_bio
->sectors
);
4544 atomic_inc(&r10_bio
->remaining
);
4546 generic_make_request(b
);
4548 end_reshape_request(r10_bio
);
4551 static void end_reshape(struct r10conf
*conf
)
4553 if (test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
))
4556 spin_lock_irq(&conf
->device_lock
);
4557 conf
->prev
= conf
->geo
;
4558 md_finish_reshape(conf
->mddev
);
4560 conf
->reshape_progress
= MaxSector
;
4561 conf
->reshape_safe
= MaxSector
;
4562 spin_unlock_irq(&conf
->device_lock
);
4564 /* read-ahead size must cover two whole stripes, which is
4565 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4567 if (conf
->mddev
->queue
) {
4568 int stripe
= conf
->geo
.raid_disks
*
4569 ((conf
->mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
4570 stripe
/= conf
->geo
.near_copies
;
4571 if (conf
->mddev
->queue
->backing_dev_info
->ra_pages
< 2 * stripe
)
4572 conf
->mddev
->queue
->backing_dev_info
->ra_pages
= 2 * stripe
;
4577 static int handle_reshape_read_error(struct mddev
*mddev
,
4578 struct r10bio
*r10_bio
)
4580 /* Use sync reads to get the blocks from somewhere else */
4581 int sectors
= r10_bio
->sectors
;
4582 struct r10conf
*conf
= mddev
->private;
4584 struct r10bio r10_bio
;
4585 struct r10dev devs
[conf
->copies
];
4587 struct r10bio
*r10b
= &on_stack
.r10_bio
;
4590 struct page
**pages
;
4592 /* reshape IOs share pages from .devs[0].bio */
4593 pages
= get_resync_pages(r10_bio
->devs
[0].bio
)->pages
;
4595 r10b
->sector
= r10_bio
->sector
;
4596 __raid10_find_phys(&conf
->prev
, r10b
);
4601 int first_slot
= slot
;
4603 if (s
> (PAGE_SIZE
>> 9))
4608 int d
= r10b
->devs
[slot
].devnum
;
4609 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4612 test_bit(Faulty
, &rdev
->flags
) ||
4613 !test_bit(In_sync
, &rdev
->flags
))
4616 addr
= r10b
->devs
[slot
].addr
+ idx
* PAGE_SIZE
;
4617 atomic_inc(&rdev
->nr_pending
);
4619 success
= sync_page_io(rdev
,
4623 REQ_OP_READ
, 0, false);
4624 rdev_dec_pending(rdev
, mddev
);
4630 if (slot
>= conf
->copies
)
4632 if (slot
== first_slot
)
4637 /* couldn't read this block, must give up */
4638 set_bit(MD_RECOVERY_INTR
,
4648 static void end_reshape_write(struct bio
*bio
)
4650 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
4651 struct mddev
*mddev
= r10_bio
->mddev
;
4652 struct r10conf
*conf
= mddev
->private;
4656 struct md_rdev
*rdev
= NULL
;
4658 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
4660 rdev
= conf
->mirrors
[d
].replacement
;
4663 rdev
= conf
->mirrors
[d
].rdev
;
4666 if (bio
->bi_status
) {
4667 /* FIXME should record badblock */
4668 md_error(mddev
, rdev
);
4671 rdev_dec_pending(rdev
, mddev
);
4672 end_reshape_request(r10_bio
);
4675 static void end_reshape_request(struct r10bio
*r10_bio
)
4677 if (!atomic_dec_and_test(&r10_bio
->remaining
))
4679 md_done_sync(r10_bio
->mddev
, r10_bio
->sectors
, 1);
4680 bio_put(r10_bio
->master_bio
);
4684 static void raid10_finish_reshape(struct mddev
*mddev
)
4686 struct r10conf
*conf
= mddev
->private;
4688 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
4691 if (mddev
->delta_disks
> 0) {
4692 sector_t size
= raid10_size(mddev
, 0, 0);
4693 md_set_array_sectors(mddev
, size
);
4694 if (mddev
->recovery_cp
> mddev
->resync_max_sectors
) {
4695 mddev
->recovery_cp
= mddev
->resync_max_sectors
;
4696 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4698 mddev
->resync_max_sectors
= size
;
4700 set_capacity(mddev
->gendisk
, mddev
->array_sectors
);
4701 revalidate_disk(mddev
->gendisk
);
4706 for (d
= conf
->geo
.raid_disks
;
4707 d
< conf
->geo
.raid_disks
- mddev
->delta_disks
;
4709 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4711 clear_bit(In_sync
, &rdev
->flags
);
4712 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4714 clear_bit(In_sync
, &rdev
->flags
);
4718 mddev
->layout
= mddev
->new_layout
;
4719 mddev
->chunk_sectors
= 1 << conf
->geo
.chunk_shift
;
4720 mddev
->reshape_position
= MaxSector
;
4721 mddev
->delta_disks
= 0;
4722 mddev
->reshape_backwards
= 0;
4725 static struct md_personality raid10_personality
=
4729 .owner
= THIS_MODULE
,
4730 .make_request
= raid10_make_request
,
4732 .free
= raid10_free
,
4733 .status
= raid10_status
,
4734 .error_handler
= raid10_error
,
4735 .hot_add_disk
= raid10_add_disk
,
4736 .hot_remove_disk
= raid10_remove_disk
,
4737 .spare_active
= raid10_spare_active
,
4738 .sync_request
= raid10_sync_request
,
4739 .quiesce
= raid10_quiesce
,
4740 .size
= raid10_size
,
4741 .resize
= raid10_resize
,
4742 .takeover
= raid10_takeover
,
4743 .check_reshape
= raid10_check_reshape
,
4744 .start_reshape
= raid10_start_reshape
,
4745 .finish_reshape
= raid10_finish_reshape
,
4746 .congested
= raid10_congested
,
4749 static int __init
raid_init(void)
4751 return register_md_personality(&raid10_personality
);
4754 static void raid_exit(void)
4756 unregister_md_personality(&raid10_personality
);
4759 module_init(raid_init
);
4760 module_exit(raid_exit
);
4761 MODULE_LICENSE("GPL");
4762 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4763 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4764 MODULE_ALIAS("md-raid10");
4765 MODULE_ALIAS("md-level-10");
4767 module_param(max_queued_requests
, int, S_IRUGO
|S_IWUSR
);