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 <linux/raid/md_p.h>
29 #include <trace/events/block.h>
33 #include "md-bitmap.h"
36 * RAID10 provides a combination of RAID0 and RAID1 functionality.
37 * The layout of data is defined by
40 * near_copies (stored in low byte of layout)
41 * far_copies (stored in second byte of layout)
42 * far_offset (stored in bit 16 of layout )
43 * use_far_sets (stored in bit 17 of layout )
44 * use_far_sets_bugfixed (stored in bit 18 of layout )
46 * The data to be stored is divided into chunks using chunksize. Each device
47 * is divided into far_copies sections. In each section, chunks are laid out
48 * in a style similar to raid0, but near_copies copies of each chunk is stored
49 * (each on a different drive). The starting device for each section is offset
50 * near_copies from the starting device of the previous section. Thus there
51 * are (near_copies * far_copies) of each chunk, and each is on a different
52 * drive. near_copies and far_copies must be at least one, and their product
53 * is at most raid_disks.
55 * If far_offset is true, then the far_copies are handled a bit differently.
56 * The copies are still in different stripes, but instead of being very far
57 * apart on disk, there are adjacent stripes.
59 * The far and offset algorithms are handled slightly differently if
60 * 'use_far_sets' is true. In this case, the array's devices are grouped into
61 * sets that are (near_copies * far_copies) in size. The far copied stripes
62 * are still shifted by 'near_copies' devices, but this shifting stays confined
63 * to the set rather than the entire array. This is done to improve the number
64 * of device combinations that can fail without causing the array to fail.
65 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
70 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
71 * [A B] [C D] [A B] [C D E]
72 * |...| |...| |...| | ... |
73 * [B A] [D C] [B A] [E C D]
77 * Number of guaranteed r10bios in case of extreme VM load:
79 #define NR_RAID10_BIOS 256
81 /* when we get a read error on a read-only array, we redirect to another
82 * device without failing the first device, or trying to over-write to
83 * correct the read error. To keep track of bad blocks on a per-bio
84 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
86 #define IO_BLOCKED ((struct bio *)1)
87 /* When we successfully write to a known bad-block, we need to remove the
88 * bad-block marking which must be done from process context. So we record
89 * the success by setting devs[n].bio to IO_MADE_GOOD
91 #define IO_MADE_GOOD ((struct bio *)2)
93 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
95 /* When there are this many requests queued to be written by
96 * the raid10 thread, we become 'congested' to provide back-pressure
99 static int max_queued_requests
= 1024;
101 static void allow_barrier(struct r10conf
*conf
);
102 static void lower_barrier(struct r10conf
*conf
);
103 static int _enough(struct r10conf
*conf
, int previous
, int ignore
);
104 static int enough(struct r10conf
*conf
, int ignore
);
105 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
107 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
);
108 static void end_reshape_write(struct bio
*bio
);
109 static void end_reshape(struct r10conf
*conf
);
111 #define raid10_log(md, fmt, args...) \
112 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
114 #include "raid1-10.c"
117 * for resync bio, r10bio pointer can be retrieved from the per-bio
118 * 'struct resync_pages'.
120 static inline struct r10bio
*get_resync_r10bio(struct bio
*bio
)
122 return get_resync_pages(bio
)->raid_bio
;
125 static void * r10bio_pool_alloc(gfp_t gfp_flags
, void *data
)
127 struct r10conf
*conf
= data
;
128 int size
= offsetof(struct r10bio
, devs
[conf
->copies
]);
130 /* allocate a r10bio with room for raid_disks entries in the
132 return kzalloc(size
, gfp_flags
);
135 static void r10bio_pool_free(void *r10_bio
, void *data
)
140 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
141 /* amount of memory to reserve for resync requests */
142 #define RESYNC_WINDOW (1024*1024)
143 /* maximum number of concurrent requests, memory permitting */
144 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
145 #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
146 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
149 * When performing a resync, we need to read and compare, so
150 * we need as many pages are there are copies.
151 * When performing a recovery, we need 2 bios, one for read,
152 * one for write (we recover only one drive per r10buf)
155 static void * r10buf_pool_alloc(gfp_t gfp_flags
, void *data
)
157 struct r10conf
*conf
= data
;
158 struct r10bio
*r10_bio
;
161 int nalloc
, nalloc_rp
;
162 struct resync_pages
*rps
;
164 r10_bio
= r10bio_pool_alloc(gfp_flags
, conf
);
168 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
) ||
169 test_bit(MD_RECOVERY_RESHAPE
, &conf
->mddev
->recovery
))
170 nalloc
= conf
->copies
; /* resync */
172 nalloc
= 2; /* recovery */
174 /* allocate once for all bios */
175 if (!conf
->have_replacement
)
178 nalloc_rp
= nalloc
* 2;
179 rps
= kmalloc_array(nalloc_rp
, sizeof(struct resync_pages
), gfp_flags
);
181 goto out_free_r10bio
;
186 for (j
= nalloc
; j
-- ; ) {
187 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
190 r10_bio
->devs
[j
].bio
= bio
;
191 if (!conf
->have_replacement
)
193 bio
= bio_kmalloc(gfp_flags
, RESYNC_PAGES
);
196 r10_bio
->devs
[j
].repl_bio
= bio
;
199 * Allocate RESYNC_PAGES data pages and attach them
202 for (j
= 0; j
< nalloc
; j
++) {
203 struct bio
*rbio
= r10_bio
->devs
[j
].repl_bio
;
204 struct resync_pages
*rp
, *rp_repl
;
208 rp_repl
= &rps
[nalloc
+ j
];
210 bio
= r10_bio
->devs
[j
].bio
;
212 if (!j
|| test_bit(MD_RECOVERY_SYNC
,
213 &conf
->mddev
->recovery
)) {
214 if (resync_alloc_pages(rp
, gfp_flags
))
217 memcpy(rp
, &rps
[0], sizeof(*rp
));
218 resync_get_all_pages(rp
);
221 rp
->raid_bio
= r10_bio
;
222 bio
->bi_private
= rp
;
224 memcpy(rp_repl
, rp
, sizeof(*rp
));
225 rbio
->bi_private
= rp_repl
;
233 resync_free_pages(&rps
[j
* 2]);
237 for ( ; j
< nalloc
; j
++) {
238 if (r10_bio
->devs
[j
].bio
)
239 bio_put(r10_bio
->devs
[j
].bio
);
240 if (r10_bio
->devs
[j
].repl_bio
)
241 bio_put(r10_bio
->devs
[j
].repl_bio
);
245 r10bio_pool_free(r10_bio
, conf
);
249 static void r10buf_pool_free(void *__r10_bio
, void *data
)
251 struct r10conf
*conf
= data
;
252 struct r10bio
*r10bio
= __r10_bio
;
254 struct resync_pages
*rp
= NULL
;
256 for (j
= conf
->copies
; j
--; ) {
257 struct bio
*bio
= r10bio
->devs
[j
].bio
;
260 rp
= get_resync_pages(bio
);
261 resync_free_pages(rp
);
265 bio
= r10bio
->devs
[j
].repl_bio
;
270 /* resync pages array stored in the 1st bio's .bi_private */
273 r10bio_pool_free(r10bio
, conf
);
276 static void put_all_bios(struct r10conf
*conf
, struct r10bio
*r10_bio
)
280 for (i
= 0; i
< conf
->copies
; i
++) {
281 struct bio
**bio
= & r10_bio
->devs
[i
].bio
;
282 if (!BIO_SPECIAL(*bio
))
285 bio
= &r10_bio
->devs
[i
].repl_bio
;
286 if (r10_bio
->read_slot
< 0 && !BIO_SPECIAL(*bio
))
292 static void free_r10bio(struct r10bio
*r10_bio
)
294 struct r10conf
*conf
= r10_bio
->mddev
->private;
296 put_all_bios(conf
, r10_bio
);
297 mempool_free(r10_bio
, &conf
->r10bio_pool
);
300 static void put_buf(struct r10bio
*r10_bio
)
302 struct r10conf
*conf
= r10_bio
->mddev
->private;
304 mempool_free(r10_bio
, &conf
->r10buf_pool
);
309 static void reschedule_retry(struct r10bio
*r10_bio
)
312 struct mddev
*mddev
= r10_bio
->mddev
;
313 struct r10conf
*conf
= mddev
->private;
315 spin_lock_irqsave(&conf
->device_lock
, flags
);
316 list_add(&r10_bio
->retry_list
, &conf
->retry_list
);
318 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
320 /* wake up frozen array... */
321 wake_up(&conf
->wait_barrier
);
323 md_wakeup_thread(mddev
->thread
);
327 * raid_end_bio_io() is called when we have finished servicing a mirrored
328 * operation and are ready to return a success/failure code to the buffer
331 static void raid_end_bio_io(struct r10bio
*r10_bio
)
333 struct bio
*bio
= r10_bio
->master_bio
;
334 struct r10conf
*conf
= r10_bio
->mddev
->private;
336 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
337 bio
->bi_status
= BLK_STS_IOERR
;
341 * Wake up any possible resync thread that waits for the device
346 free_r10bio(r10_bio
);
350 * Update disk head position estimator based on IRQ completion info.
352 static inline void update_head_pos(int slot
, struct r10bio
*r10_bio
)
354 struct r10conf
*conf
= r10_bio
->mddev
->private;
356 conf
->mirrors
[r10_bio
->devs
[slot
].devnum
].head_position
=
357 r10_bio
->devs
[slot
].addr
+ (r10_bio
->sectors
);
361 * Find the disk number which triggered given bio
363 static int find_bio_disk(struct r10conf
*conf
, struct r10bio
*r10_bio
,
364 struct bio
*bio
, int *slotp
, int *replp
)
369 for (slot
= 0; slot
< conf
->copies
; slot
++) {
370 if (r10_bio
->devs
[slot
].bio
== bio
)
372 if (r10_bio
->devs
[slot
].repl_bio
== bio
) {
378 BUG_ON(slot
== conf
->copies
);
379 update_head_pos(slot
, r10_bio
);
385 return r10_bio
->devs
[slot
].devnum
;
388 static void raid10_end_read_request(struct bio
*bio
)
390 int uptodate
= !bio
->bi_status
;
391 struct r10bio
*r10_bio
= bio
->bi_private
;
393 struct md_rdev
*rdev
;
394 struct r10conf
*conf
= r10_bio
->mddev
->private;
396 slot
= r10_bio
->read_slot
;
397 rdev
= r10_bio
->devs
[slot
].rdev
;
399 * this branch is our 'one mirror IO has finished' event handler:
401 update_head_pos(slot
, r10_bio
);
405 * Set R10BIO_Uptodate in our master bio, so that
406 * we will return a good error code to the higher
407 * levels even if IO on some other mirrored buffer fails.
409 * The 'master' represents the composite IO operation to
410 * user-side. So if something waits for IO, then it will
411 * wait for the 'master' bio.
413 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
415 /* If all other devices that store this block have
416 * failed, we want to return the error upwards rather
417 * than fail the last device. Here we redefine
418 * "uptodate" to mean "Don't want to retry"
420 if (!_enough(conf
, test_bit(R10BIO_Previous
, &r10_bio
->state
),
425 raid_end_bio_io(r10_bio
);
426 rdev_dec_pending(rdev
, conf
->mddev
);
429 * oops, read error - keep the refcount on the rdev
431 char b
[BDEVNAME_SIZE
];
432 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
434 bdevname(rdev
->bdev
, b
),
435 (unsigned long long)r10_bio
->sector
);
436 set_bit(R10BIO_ReadError
, &r10_bio
->state
);
437 reschedule_retry(r10_bio
);
441 static void close_write(struct r10bio
*r10_bio
)
443 /* clear the bitmap if all writes complete successfully */
444 md_bitmap_endwrite(r10_bio
->mddev
->bitmap
, r10_bio
->sector
,
446 !test_bit(R10BIO_Degraded
, &r10_bio
->state
),
448 md_write_end(r10_bio
->mddev
);
451 static void one_write_done(struct r10bio
*r10_bio
)
453 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
454 if (test_bit(R10BIO_WriteError
, &r10_bio
->state
))
455 reschedule_retry(r10_bio
);
457 close_write(r10_bio
);
458 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
))
459 reschedule_retry(r10_bio
);
461 raid_end_bio_io(r10_bio
);
466 static void raid10_end_write_request(struct bio
*bio
)
468 struct r10bio
*r10_bio
= bio
->bi_private
;
471 struct r10conf
*conf
= r10_bio
->mddev
->private;
473 struct md_rdev
*rdev
= NULL
;
474 struct bio
*to_put
= NULL
;
477 discard_error
= bio
->bi_status
&& bio_op(bio
) == REQ_OP_DISCARD
;
479 dev
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
482 rdev
= conf
->mirrors
[dev
].replacement
;
486 rdev
= conf
->mirrors
[dev
].rdev
;
489 * this branch is our 'one mirror IO has finished' event handler:
491 if (bio
->bi_status
&& !discard_error
) {
493 /* Never record new bad blocks to replacement,
496 md_error(rdev
->mddev
, rdev
);
498 set_bit(WriteErrorSeen
, &rdev
->flags
);
499 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
500 set_bit(MD_RECOVERY_NEEDED
,
501 &rdev
->mddev
->recovery
);
504 if (test_bit(FailFast
, &rdev
->flags
) &&
505 (bio
->bi_opf
& MD_FAILFAST
)) {
506 md_error(rdev
->mddev
, rdev
);
507 if (!test_bit(Faulty
, &rdev
->flags
))
508 /* This is the only remaining device,
509 * We need to retry the write without
512 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
514 r10_bio
->devs
[slot
].bio
= NULL
;
519 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
523 * Set R10BIO_Uptodate in our master bio, so that
524 * we will return a good error code for to the higher
525 * levels even if IO on some other mirrored buffer fails.
527 * The 'master' represents the composite IO operation to
528 * user-side. So if something waits for IO, then it will
529 * wait for the 'master' bio.
535 * Do not set R10BIO_Uptodate if the current device is
536 * rebuilding or Faulty. This is because we cannot use
537 * such device for properly reading the data back (we could
538 * potentially use it, if the current write would have felt
539 * before rdev->recovery_offset, but for simplicity we don't
542 if (test_bit(In_sync
, &rdev
->flags
) &&
543 !test_bit(Faulty
, &rdev
->flags
))
544 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
546 /* Maybe we can clear some bad blocks. */
547 if (is_badblock(rdev
,
548 r10_bio
->devs
[slot
].addr
,
550 &first_bad
, &bad_sectors
) && !discard_error
) {
553 r10_bio
->devs
[slot
].repl_bio
= IO_MADE_GOOD
;
555 r10_bio
->devs
[slot
].bio
= IO_MADE_GOOD
;
557 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
563 * Let's see if all mirrored write operations have finished
566 one_write_done(r10_bio
);
568 rdev_dec_pending(rdev
, conf
->mddev
);
574 * RAID10 layout manager
575 * As well as the chunksize and raid_disks count, there are two
576 * parameters: near_copies and far_copies.
577 * near_copies * far_copies must be <= raid_disks.
578 * Normally one of these will be 1.
579 * If both are 1, we get raid0.
580 * If near_copies == raid_disks, we get raid1.
582 * Chunks are laid out in raid0 style with near_copies copies of the
583 * first chunk, followed by near_copies copies of the next chunk and
585 * If far_copies > 1, then after 1/far_copies of the array has been assigned
586 * as described above, we start again with a device offset of near_copies.
587 * So we effectively have another copy of the whole array further down all
588 * the drives, but with blocks on different drives.
589 * With this layout, and block is never stored twice on the one device.
591 * raid10_find_phys finds the sector offset of a given virtual sector
592 * on each device that it is on.
594 * raid10_find_virt does the reverse mapping, from a device and a
595 * sector offset to a virtual address
598 static void __raid10_find_phys(struct geom
*geo
, struct r10bio
*r10bio
)
606 int last_far_set_start
, last_far_set_size
;
608 last_far_set_start
= (geo
->raid_disks
/ geo
->far_set_size
) - 1;
609 last_far_set_start
*= geo
->far_set_size
;
611 last_far_set_size
= geo
->far_set_size
;
612 last_far_set_size
+= (geo
->raid_disks
% geo
->far_set_size
);
614 /* now calculate first sector/dev */
615 chunk
= r10bio
->sector
>> geo
->chunk_shift
;
616 sector
= r10bio
->sector
& geo
->chunk_mask
;
618 chunk
*= geo
->near_copies
;
620 dev
= sector_div(stripe
, geo
->raid_disks
);
622 stripe
*= geo
->far_copies
;
624 sector
+= stripe
<< geo
->chunk_shift
;
626 /* and calculate all the others */
627 for (n
= 0; n
< geo
->near_copies
; n
++) {
631 r10bio
->devs
[slot
].devnum
= d
;
632 r10bio
->devs
[slot
].addr
= s
;
635 for (f
= 1; f
< geo
->far_copies
; f
++) {
636 set
= d
/ geo
->far_set_size
;
637 d
+= geo
->near_copies
;
639 if ((geo
->raid_disks
% geo
->far_set_size
) &&
640 (d
> last_far_set_start
)) {
641 d
-= last_far_set_start
;
642 d
%= last_far_set_size
;
643 d
+= last_far_set_start
;
645 d
%= geo
->far_set_size
;
646 d
+= geo
->far_set_size
* set
;
649 r10bio
->devs
[slot
].devnum
= d
;
650 r10bio
->devs
[slot
].addr
= s
;
654 if (dev
>= geo
->raid_disks
) {
656 sector
+= (geo
->chunk_mask
+ 1);
661 static void raid10_find_phys(struct r10conf
*conf
, struct r10bio
*r10bio
)
663 struct geom
*geo
= &conf
->geo
;
665 if (conf
->reshape_progress
!= MaxSector
&&
666 ((r10bio
->sector
>= conf
->reshape_progress
) !=
667 conf
->mddev
->reshape_backwards
)) {
668 set_bit(R10BIO_Previous
, &r10bio
->state
);
671 clear_bit(R10BIO_Previous
, &r10bio
->state
);
673 __raid10_find_phys(geo
, r10bio
);
676 static sector_t
raid10_find_virt(struct r10conf
*conf
, sector_t sector
, int dev
)
678 sector_t offset
, chunk
, vchunk
;
679 /* Never use conf->prev as this is only called during resync
680 * or recovery, so reshape isn't happening
682 struct geom
*geo
= &conf
->geo
;
683 int far_set_start
= (dev
/ geo
->far_set_size
) * geo
->far_set_size
;
684 int far_set_size
= geo
->far_set_size
;
685 int last_far_set_start
;
687 if (geo
->raid_disks
% geo
->far_set_size
) {
688 last_far_set_start
= (geo
->raid_disks
/ geo
->far_set_size
) - 1;
689 last_far_set_start
*= geo
->far_set_size
;
691 if (dev
>= last_far_set_start
) {
692 far_set_size
= geo
->far_set_size
;
693 far_set_size
+= (geo
->raid_disks
% geo
->far_set_size
);
694 far_set_start
= last_far_set_start
;
698 offset
= sector
& geo
->chunk_mask
;
699 if (geo
->far_offset
) {
701 chunk
= sector
>> geo
->chunk_shift
;
702 fc
= sector_div(chunk
, geo
->far_copies
);
703 dev
-= fc
* geo
->near_copies
;
704 if (dev
< far_set_start
)
707 while (sector
>= geo
->stride
) {
708 sector
-= geo
->stride
;
709 if (dev
< (geo
->near_copies
+ far_set_start
))
710 dev
+= far_set_size
- geo
->near_copies
;
712 dev
-= geo
->near_copies
;
714 chunk
= sector
>> geo
->chunk_shift
;
716 vchunk
= chunk
* geo
->raid_disks
+ dev
;
717 sector_div(vchunk
, geo
->near_copies
);
718 return (vchunk
<< geo
->chunk_shift
) + offset
;
722 * This routine returns the disk from which the requested read should
723 * be done. There is a per-array 'next expected sequential IO' sector
724 * number - if this matches on the next IO then we use the last disk.
725 * There is also a per-disk 'last know head position' sector that is
726 * maintained from IRQ contexts, both the normal and the resync IO
727 * completion handlers update this position correctly. If there is no
728 * perfect sequential match then we pick the disk whose head is closest.
730 * If there are 2 mirrors in the same 2 devices, performance degrades
731 * because position is mirror, not device based.
733 * The rdev for the device selected will have nr_pending incremented.
737 * FIXME: possibly should rethink readbalancing and do it differently
738 * depending on near_copies / far_copies geometry.
740 static struct md_rdev
*read_balance(struct r10conf
*conf
,
741 struct r10bio
*r10_bio
,
744 const sector_t this_sector
= r10_bio
->sector
;
746 int sectors
= r10_bio
->sectors
;
747 int best_good_sectors
;
748 sector_t new_distance
, best_dist
;
749 struct md_rdev
*best_rdev
, *rdev
= NULL
;
752 struct geom
*geo
= &conf
->geo
;
754 raid10_find_phys(conf
, r10_bio
);
758 best_dist
= MaxSector
;
759 best_good_sectors
= 0;
761 clear_bit(R10BIO_FailFast
, &r10_bio
->state
);
763 * Check if we can balance. We can balance on the whole
764 * device if no resync is going on (recovery is ok), or below
765 * the resync window. We take the first readable disk when
766 * above the resync window.
768 if ((conf
->mddev
->recovery_cp
< MaxSector
769 && (this_sector
+ sectors
>= conf
->next_resync
)) ||
770 (mddev_is_clustered(conf
->mddev
) &&
771 md_cluster_ops
->area_resyncing(conf
->mddev
, READ
, this_sector
,
772 this_sector
+ sectors
)))
775 for (slot
= 0; slot
< conf
->copies
; slot
++) {
780 if (r10_bio
->devs
[slot
].bio
== IO_BLOCKED
)
782 disk
= r10_bio
->devs
[slot
].devnum
;
783 rdev
= rcu_dereference(conf
->mirrors
[disk
].replacement
);
784 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
) ||
785 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
786 rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
788 test_bit(Faulty
, &rdev
->flags
))
790 if (!test_bit(In_sync
, &rdev
->flags
) &&
791 r10_bio
->devs
[slot
].addr
+ sectors
> rdev
->recovery_offset
)
794 dev_sector
= r10_bio
->devs
[slot
].addr
;
795 if (is_badblock(rdev
, dev_sector
, sectors
,
796 &first_bad
, &bad_sectors
)) {
797 if (best_dist
< MaxSector
)
798 /* Already have a better slot */
800 if (first_bad
<= dev_sector
) {
801 /* Cannot read here. If this is the
802 * 'primary' device, then we must not read
803 * beyond 'bad_sectors' from another device.
805 bad_sectors
-= (dev_sector
- first_bad
);
806 if (!do_balance
&& sectors
> bad_sectors
)
807 sectors
= bad_sectors
;
808 if (best_good_sectors
> sectors
)
809 best_good_sectors
= sectors
;
811 sector_t good_sectors
=
812 first_bad
- dev_sector
;
813 if (good_sectors
> best_good_sectors
) {
814 best_good_sectors
= good_sectors
;
819 /* Must read from here */
824 best_good_sectors
= sectors
;
830 /* At least 2 disks to choose from so failfast is OK */
831 set_bit(R10BIO_FailFast
, &r10_bio
->state
);
832 /* This optimisation is debatable, and completely destroys
833 * sequential read speed for 'far copies' arrays. So only
834 * keep it for 'near' arrays, and review those later.
836 if (geo
->near_copies
> 1 && !atomic_read(&rdev
->nr_pending
))
839 /* for far > 1 always use the lowest address */
840 else if (geo
->far_copies
> 1)
841 new_distance
= r10_bio
->devs
[slot
].addr
;
843 new_distance
= abs(r10_bio
->devs
[slot
].addr
-
844 conf
->mirrors
[disk
].head_position
);
845 if (new_distance
< best_dist
) {
846 best_dist
= new_distance
;
851 if (slot
>= conf
->copies
) {
857 atomic_inc(&rdev
->nr_pending
);
858 r10_bio
->read_slot
= slot
;
862 *max_sectors
= best_good_sectors
;
867 static int raid10_congested(struct mddev
*mddev
, int bits
)
869 struct r10conf
*conf
= mddev
->private;
872 if ((bits
& (1 << WB_async_congested
)) &&
873 conf
->pending_count
>= max_queued_requests
)
878 (i
< conf
->geo
.raid_disks
|| i
< conf
->prev
.raid_disks
)
881 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
882 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
883 struct request_queue
*q
= bdev_get_queue(rdev
->bdev
);
885 ret
|= bdi_congested(q
->backing_dev_info
, bits
);
892 static void flush_pending_writes(struct r10conf
*conf
)
894 /* Any writes that have been queued but are awaiting
895 * bitmap updates get flushed here.
897 spin_lock_irq(&conf
->device_lock
);
899 if (conf
->pending_bio_list
.head
) {
900 struct blk_plug plug
;
903 bio
= bio_list_get(&conf
->pending_bio_list
);
904 conf
->pending_count
= 0;
905 spin_unlock_irq(&conf
->device_lock
);
908 * As this is called in a wait_event() loop (see freeze_array),
909 * current->state might be TASK_UNINTERRUPTIBLE which will
910 * cause a warning when we prepare to wait again. As it is
911 * rare that this path is taken, it is perfectly safe to force
912 * us to go around the wait_event() loop again, so the warning
913 * is a false-positive. Silence the warning by resetting
916 __set_current_state(TASK_RUNNING
);
918 blk_start_plug(&plug
);
919 /* flush any pending bitmap writes to disk
920 * before proceeding w/ I/O */
921 md_bitmap_unplug(conf
->mddev
->bitmap
);
922 wake_up(&conf
->wait_barrier
);
924 while (bio
) { /* submit pending writes */
925 struct bio
*next
= bio
->bi_next
;
926 struct md_rdev
*rdev
= (void*)bio
->bi_disk
;
928 bio_set_dev(bio
, rdev
->bdev
);
929 if (test_bit(Faulty
, &rdev
->flags
)) {
931 } else if (unlikely((bio_op(bio
) == REQ_OP_DISCARD
) &&
932 !blk_queue_discard(bio
->bi_disk
->queue
)))
936 generic_make_request(bio
);
939 blk_finish_plug(&plug
);
941 spin_unlock_irq(&conf
->device_lock
);
945 * Sometimes we need to suspend IO while we do something else,
946 * either some resync/recovery, or reconfigure the array.
947 * To do this we raise a 'barrier'.
948 * The 'barrier' is a counter that can be raised multiple times
949 * to count how many activities are happening which preclude
951 * We can only raise the barrier if there is no pending IO.
952 * i.e. if nr_pending == 0.
953 * We choose only to raise the barrier if no-one is waiting for the
954 * barrier to go down. This means that as soon as an IO request
955 * is ready, no other operations which require a barrier will start
956 * until the IO request has had a chance.
958 * So: regular IO calls 'wait_barrier'. When that returns there
959 * is no backgroup IO happening, It must arrange to call
960 * allow_barrier when it has finished its IO.
961 * backgroup IO calls must call raise_barrier. Once that returns
962 * there is no normal IO happeing. It must arrange to call
963 * lower_barrier when the particular background IO completes.
966 static void raise_barrier(struct r10conf
*conf
, int force
)
968 BUG_ON(force
&& !conf
->barrier
);
969 spin_lock_irq(&conf
->resync_lock
);
971 /* Wait until no block IO is waiting (unless 'force') */
972 wait_event_lock_irq(conf
->wait_barrier
, force
|| !conf
->nr_waiting
,
975 /* block any new IO from starting */
978 /* Now wait for all pending IO to complete */
979 wait_event_lock_irq(conf
->wait_barrier
,
980 !atomic_read(&conf
->nr_pending
) && conf
->barrier
< RESYNC_DEPTH
,
983 spin_unlock_irq(&conf
->resync_lock
);
986 static void lower_barrier(struct r10conf
*conf
)
989 spin_lock_irqsave(&conf
->resync_lock
, flags
);
991 spin_unlock_irqrestore(&conf
->resync_lock
, flags
);
992 wake_up(&conf
->wait_barrier
);
995 static void wait_barrier(struct r10conf
*conf
)
997 spin_lock_irq(&conf
->resync_lock
);
1000 /* Wait for the barrier to drop.
1001 * However if there are already pending
1002 * requests (preventing the barrier from
1003 * rising completely), and the
1004 * pre-process bio queue isn't empty,
1005 * then don't wait, as we need to empty
1006 * that queue to get the nr_pending
1009 raid10_log(conf
->mddev
, "wait barrier");
1010 wait_event_lock_irq(conf
->wait_barrier
,
1012 (atomic_read(&conf
->nr_pending
) &&
1013 current
->bio_list
&&
1014 (!bio_list_empty(¤t
->bio_list
[0]) ||
1015 !bio_list_empty(¤t
->bio_list
[1]))),
1018 if (!conf
->nr_waiting
)
1019 wake_up(&conf
->wait_barrier
);
1021 atomic_inc(&conf
->nr_pending
);
1022 spin_unlock_irq(&conf
->resync_lock
);
1025 static void allow_barrier(struct r10conf
*conf
)
1027 if ((atomic_dec_and_test(&conf
->nr_pending
)) ||
1028 (conf
->array_freeze_pending
))
1029 wake_up(&conf
->wait_barrier
);
1032 static void freeze_array(struct r10conf
*conf
, int extra
)
1034 /* stop syncio and normal IO and wait for everything to
1036 * We increment barrier and nr_waiting, and then
1037 * wait until nr_pending match nr_queued+extra
1038 * This is called in the context of one normal IO request
1039 * that has failed. Thus any sync request that might be pending
1040 * will be blocked by nr_pending, and we need to wait for
1041 * pending IO requests to complete or be queued for re-try.
1042 * Thus the number queued (nr_queued) plus this request (extra)
1043 * must match the number of pending IOs (nr_pending) before
1046 spin_lock_irq(&conf
->resync_lock
);
1047 conf
->array_freeze_pending
++;
1050 wait_event_lock_irq_cmd(conf
->wait_barrier
,
1051 atomic_read(&conf
->nr_pending
) == conf
->nr_queued
+extra
,
1053 flush_pending_writes(conf
));
1055 conf
->array_freeze_pending
--;
1056 spin_unlock_irq(&conf
->resync_lock
);
1059 static void unfreeze_array(struct r10conf
*conf
)
1061 /* reverse the effect of the freeze */
1062 spin_lock_irq(&conf
->resync_lock
);
1065 wake_up(&conf
->wait_barrier
);
1066 spin_unlock_irq(&conf
->resync_lock
);
1069 static sector_t
choose_data_offset(struct r10bio
*r10_bio
,
1070 struct md_rdev
*rdev
)
1072 if (!test_bit(MD_RECOVERY_RESHAPE
, &rdev
->mddev
->recovery
) ||
1073 test_bit(R10BIO_Previous
, &r10_bio
->state
))
1074 return rdev
->data_offset
;
1076 return rdev
->new_data_offset
;
1079 struct raid10_plug_cb
{
1080 struct blk_plug_cb cb
;
1081 struct bio_list pending
;
1085 static void raid10_unplug(struct blk_plug_cb
*cb
, bool from_schedule
)
1087 struct raid10_plug_cb
*plug
= container_of(cb
, struct raid10_plug_cb
,
1089 struct mddev
*mddev
= plug
->cb
.data
;
1090 struct r10conf
*conf
= mddev
->private;
1093 if (from_schedule
|| current
->bio_list
) {
1094 spin_lock_irq(&conf
->device_lock
);
1095 bio_list_merge(&conf
->pending_bio_list
, &plug
->pending
);
1096 conf
->pending_count
+= plug
->pending_cnt
;
1097 spin_unlock_irq(&conf
->device_lock
);
1098 wake_up(&conf
->wait_barrier
);
1099 md_wakeup_thread(mddev
->thread
);
1104 /* we aren't scheduling, so we can do the write-out directly. */
1105 bio
= bio_list_get(&plug
->pending
);
1106 md_bitmap_unplug(mddev
->bitmap
);
1107 wake_up(&conf
->wait_barrier
);
1109 while (bio
) { /* submit pending writes */
1110 struct bio
*next
= bio
->bi_next
;
1111 struct md_rdev
*rdev
= (void*)bio
->bi_disk
;
1112 bio
->bi_next
= NULL
;
1113 bio_set_dev(bio
, rdev
->bdev
);
1114 if (test_bit(Faulty
, &rdev
->flags
)) {
1116 } else if (unlikely((bio_op(bio
) == REQ_OP_DISCARD
) &&
1117 !blk_queue_discard(bio
->bi_disk
->queue
)))
1118 /* Just ignore it */
1121 generic_make_request(bio
);
1128 * 1. Register the new request and wait if the reconstruction thread has put
1129 * up a bar for new requests. Continue immediately if no resync is active
1131 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1133 static void regular_request_wait(struct mddev
*mddev
, struct r10conf
*conf
,
1134 struct bio
*bio
, sector_t sectors
)
1137 while (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1138 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
&&
1139 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
) {
1140 raid10_log(conf
->mddev
, "wait reshape");
1141 allow_barrier(conf
);
1142 wait_event(conf
->wait_barrier
,
1143 conf
->reshape_progress
<= bio
->bi_iter
.bi_sector
||
1144 conf
->reshape_progress
>= bio
->bi_iter
.bi_sector
+
1150 static void raid10_read_request(struct mddev
*mddev
, struct bio
*bio
,
1151 struct r10bio
*r10_bio
)
1153 struct r10conf
*conf
= mddev
->private;
1154 struct bio
*read_bio
;
1155 const int op
= bio_op(bio
);
1156 const unsigned long do_sync
= (bio
->bi_opf
& REQ_SYNC
);
1158 struct md_rdev
*rdev
;
1159 char b
[BDEVNAME_SIZE
];
1160 int slot
= r10_bio
->read_slot
;
1161 struct md_rdev
*err_rdev
= NULL
;
1162 gfp_t gfp
= GFP_NOIO
;
1164 if (r10_bio
->devs
[slot
].rdev
) {
1166 * This is an error retry, but we cannot
1167 * safely dereference the rdev in the r10_bio,
1168 * we must use the one in conf.
1169 * If it has already been disconnected (unlikely)
1170 * we lose the device name in error messages.
1174 * As we are blocking raid10, it is a little safer to
1177 gfp
= GFP_NOIO
| __GFP_HIGH
;
1180 disk
= r10_bio
->devs
[slot
].devnum
;
1181 err_rdev
= rcu_dereference(conf
->mirrors
[disk
].rdev
);
1183 bdevname(err_rdev
->bdev
, b
);
1186 /* This never gets dereferenced */
1187 err_rdev
= r10_bio
->devs
[slot
].rdev
;
1192 regular_request_wait(mddev
, conf
, bio
, r10_bio
->sectors
);
1193 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
1196 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1198 (unsigned long long)r10_bio
->sector
);
1200 raid_end_bio_io(r10_bio
);
1204 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1206 bdevname(rdev
->bdev
, b
),
1207 (unsigned long long)r10_bio
->sector
);
1208 if (max_sectors
< bio_sectors(bio
)) {
1209 struct bio
*split
= bio_split(bio
, max_sectors
,
1210 gfp
, &conf
->bio_split
);
1211 bio_chain(split
, bio
);
1212 allow_barrier(conf
);
1213 generic_make_request(bio
);
1216 r10_bio
->master_bio
= bio
;
1217 r10_bio
->sectors
= max_sectors
;
1219 slot
= r10_bio
->read_slot
;
1221 read_bio
= bio_clone_fast(bio
, gfp
, &mddev
->bio_set
);
1223 r10_bio
->devs
[slot
].bio
= read_bio
;
1224 r10_bio
->devs
[slot
].rdev
= rdev
;
1226 read_bio
->bi_iter
.bi_sector
= r10_bio
->devs
[slot
].addr
+
1227 choose_data_offset(r10_bio
, rdev
);
1228 bio_set_dev(read_bio
, rdev
->bdev
);
1229 read_bio
->bi_end_io
= raid10_end_read_request
;
1230 bio_set_op_attrs(read_bio
, op
, do_sync
);
1231 if (test_bit(FailFast
, &rdev
->flags
) &&
1232 test_bit(R10BIO_FailFast
, &r10_bio
->state
))
1233 read_bio
->bi_opf
|= MD_FAILFAST
;
1234 read_bio
->bi_private
= r10_bio
;
1237 trace_block_bio_remap(read_bio
->bi_disk
->queue
,
1238 read_bio
, disk_devt(mddev
->gendisk
),
1240 generic_make_request(read_bio
);
1244 static void raid10_write_one_disk(struct mddev
*mddev
, struct r10bio
*r10_bio
,
1245 struct bio
*bio
, bool replacement
,
1248 const int op
= bio_op(bio
);
1249 const unsigned long do_sync
= (bio
->bi_opf
& REQ_SYNC
);
1250 const unsigned long do_fua
= (bio
->bi_opf
& REQ_FUA
);
1251 unsigned long flags
;
1252 struct blk_plug_cb
*cb
;
1253 struct raid10_plug_cb
*plug
= NULL
;
1254 struct r10conf
*conf
= mddev
->private;
1255 struct md_rdev
*rdev
;
1256 int devnum
= r10_bio
->devs
[n_copy
].devnum
;
1260 rdev
= conf
->mirrors
[devnum
].replacement
;
1262 /* Replacement just got moved to main 'rdev' */
1264 rdev
= conf
->mirrors
[devnum
].rdev
;
1267 rdev
= conf
->mirrors
[devnum
].rdev
;
1269 mbio
= bio_clone_fast(bio
, GFP_NOIO
, &mddev
->bio_set
);
1271 r10_bio
->devs
[n_copy
].repl_bio
= mbio
;
1273 r10_bio
->devs
[n_copy
].bio
= mbio
;
1275 mbio
->bi_iter
.bi_sector
= (r10_bio
->devs
[n_copy
].addr
+
1276 choose_data_offset(r10_bio
, rdev
));
1277 bio_set_dev(mbio
, rdev
->bdev
);
1278 mbio
->bi_end_io
= raid10_end_write_request
;
1279 bio_set_op_attrs(mbio
, op
, do_sync
| do_fua
);
1280 if (!replacement
&& test_bit(FailFast
,
1281 &conf
->mirrors
[devnum
].rdev
->flags
)
1282 && enough(conf
, devnum
))
1283 mbio
->bi_opf
|= MD_FAILFAST
;
1284 mbio
->bi_private
= r10_bio
;
1286 if (conf
->mddev
->gendisk
)
1287 trace_block_bio_remap(mbio
->bi_disk
->queue
,
1288 mbio
, disk_devt(conf
->mddev
->gendisk
),
1290 /* flush_pending_writes() needs access to the rdev so...*/
1291 mbio
->bi_disk
= (void *)rdev
;
1293 atomic_inc(&r10_bio
->remaining
);
1295 cb
= blk_check_plugged(raid10_unplug
, mddev
, sizeof(*plug
));
1297 plug
= container_of(cb
, struct raid10_plug_cb
, cb
);
1301 bio_list_add(&plug
->pending
, mbio
);
1302 plug
->pending_cnt
++;
1304 spin_lock_irqsave(&conf
->device_lock
, flags
);
1305 bio_list_add(&conf
->pending_bio_list
, mbio
);
1306 conf
->pending_count
++;
1307 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1308 md_wakeup_thread(mddev
->thread
);
1312 static void raid10_write_request(struct mddev
*mddev
, struct bio
*bio
,
1313 struct r10bio
*r10_bio
)
1315 struct r10conf
*conf
= mddev
->private;
1317 struct md_rdev
*blocked_rdev
;
1321 if ((mddev_is_clustered(mddev
) &&
1322 md_cluster_ops
->area_resyncing(mddev
, WRITE
,
1323 bio
->bi_iter
.bi_sector
,
1324 bio_end_sector(bio
)))) {
1327 prepare_to_wait(&conf
->wait_barrier
,
1329 if (!md_cluster_ops
->area_resyncing(mddev
, WRITE
,
1330 bio
->bi_iter
.bi_sector
, bio_end_sector(bio
)))
1334 finish_wait(&conf
->wait_barrier
, &w
);
1337 sectors
= r10_bio
->sectors
;
1338 regular_request_wait(mddev
, conf
, bio
, sectors
);
1339 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
1340 (mddev
->reshape_backwards
1341 ? (bio
->bi_iter
.bi_sector
< conf
->reshape_safe
&&
1342 bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_progress
)
1343 : (bio
->bi_iter
.bi_sector
+ sectors
> conf
->reshape_safe
&&
1344 bio
->bi_iter
.bi_sector
< conf
->reshape_progress
))) {
1345 /* Need to update reshape_position in metadata */
1346 mddev
->reshape_position
= conf
->reshape_progress
;
1347 set_mask_bits(&mddev
->sb_flags
, 0,
1348 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
1349 md_wakeup_thread(mddev
->thread
);
1350 raid10_log(conf
->mddev
, "wait reshape metadata");
1351 wait_event(mddev
->sb_wait
,
1352 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
));
1354 conf
->reshape_safe
= mddev
->reshape_position
;
1357 if (conf
->pending_count
>= max_queued_requests
) {
1358 md_wakeup_thread(mddev
->thread
);
1359 raid10_log(mddev
, "wait queued");
1360 wait_event(conf
->wait_barrier
,
1361 conf
->pending_count
< max_queued_requests
);
1363 /* first select target devices under rcu_lock and
1364 * inc refcount on their rdev. Record them by setting
1366 * If there are known/acknowledged bad blocks on any device
1367 * on which we have seen a write error, we want to avoid
1368 * writing to those blocks. This potentially requires several
1369 * writes to write around the bad blocks. Each set of writes
1370 * gets its own r10_bio with a set of bios attached.
1373 r10_bio
->read_slot
= -1; /* make sure repl_bio gets freed */
1374 raid10_find_phys(conf
, r10_bio
);
1376 blocked_rdev
= NULL
;
1378 max_sectors
= r10_bio
->sectors
;
1380 for (i
= 0; i
< conf
->copies
; i
++) {
1381 int d
= r10_bio
->devs
[i
].devnum
;
1382 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
1383 struct md_rdev
*rrdev
= rcu_dereference(
1384 conf
->mirrors
[d
].replacement
);
1387 if (rdev
&& unlikely(test_bit(Blocked
, &rdev
->flags
))) {
1388 atomic_inc(&rdev
->nr_pending
);
1389 blocked_rdev
= rdev
;
1392 if (rrdev
&& unlikely(test_bit(Blocked
, &rrdev
->flags
))) {
1393 atomic_inc(&rrdev
->nr_pending
);
1394 blocked_rdev
= rrdev
;
1397 if (rdev
&& (test_bit(Faulty
, &rdev
->flags
)))
1399 if (rrdev
&& (test_bit(Faulty
, &rrdev
->flags
)))
1402 r10_bio
->devs
[i
].bio
= NULL
;
1403 r10_bio
->devs
[i
].repl_bio
= NULL
;
1405 if (!rdev
&& !rrdev
) {
1406 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
1409 if (rdev
&& test_bit(WriteErrorSeen
, &rdev
->flags
)) {
1411 sector_t dev_sector
= r10_bio
->devs
[i
].addr
;
1415 is_bad
= is_badblock(rdev
, dev_sector
, max_sectors
,
1416 &first_bad
, &bad_sectors
);
1418 /* Mustn't write here until the bad block
1421 atomic_inc(&rdev
->nr_pending
);
1422 set_bit(BlockedBadBlocks
, &rdev
->flags
);
1423 blocked_rdev
= rdev
;
1426 if (is_bad
&& first_bad
<= dev_sector
) {
1427 /* Cannot write here at all */
1428 bad_sectors
-= (dev_sector
- first_bad
);
1429 if (bad_sectors
< max_sectors
)
1430 /* Mustn't write more than bad_sectors
1431 * to other devices yet
1433 max_sectors
= bad_sectors
;
1434 /* We don't set R10BIO_Degraded as that
1435 * only applies if the disk is missing,
1436 * so it might be re-added, and we want to
1437 * know to recover this chunk.
1438 * In this case the device is here, and the
1439 * fact that this chunk is not in-sync is
1440 * recorded in the bad block log.
1445 int good_sectors
= first_bad
- dev_sector
;
1446 if (good_sectors
< max_sectors
)
1447 max_sectors
= good_sectors
;
1451 r10_bio
->devs
[i
].bio
= bio
;
1452 atomic_inc(&rdev
->nr_pending
);
1455 r10_bio
->devs
[i
].repl_bio
= bio
;
1456 atomic_inc(&rrdev
->nr_pending
);
1461 if (unlikely(blocked_rdev
)) {
1462 /* Have to wait for this device to get unblocked, then retry */
1466 for (j
= 0; j
< i
; j
++) {
1467 if (r10_bio
->devs
[j
].bio
) {
1468 d
= r10_bio
->devs
[j
].devnum
;
1469 rdev_dec_pending(conf
->mirrors
[d
].rdev
, mddev
);
1471 if (r10_bio
->devs
[j
].repl_bio
) {
1472 struct md_rdev
*rdev
;
1473 d
= r10_bio
->devs
[j
].devnum
;
1474 rdev
= conf
->mirrors
[d
].replacement
;
1476 /* Race with remove_disk */
1478 rdev
= conf
->mirrors
[d
].rdev
;
1480 rdev_dec_pending(rdev
, mddev
);
1483 allow_barrier(conf
);
1484 raid10_log(conf
->mddev
, "wait rdev %d blocked", blocked_rdev
->raid_disk
);
1485 md_wait_for_blocked_rdev(blocked_rdev
, mddev
);
1490 if (max_sectors
< r10_bio
->sectors
)
1491 r10_bio
->sectors
= max_sectors
;
1493 if (r10_bio
->sectors
< bio_sectors(bio
)) {
1494 struct bio
*split
= bio_split(bio
, r10_bio
->sectors
,
1495 GFP_NOIO
, &conf
->bio_split
);
1496 bio_chain(split
, bio
);
1497 allow_barrier(conf
);
1498 generic_make_request(bio
);
1501 r10_bio
->master_bio
= bio
;
1504 atomic_set(&r10_bio
->remaining
, 1);
1505 md_bitmap_startwrite(mddev
->bitmap
, r10_bio
->sector
, r10_bio
->sectors
, 0);
1507 for (i
= 0; i
< conf
->copies
; i
++) {
1508 if (r10_bio
->devs
[i
].bio
)
1509 raid10_write_one_disk(mddev
, r10_bio
, bio
, false, i
);
1510 if (r10_bio
->devs
[i
].repl_bio
)
1511 raid10_write_one_disk(mddev
, r10_bio
, bio
, true, i
);
1513 one_write_done(r10_bio
);
1516 static void __make_request(struct mddev
*mddev
, struct bio
*bio
, int sectors
)
1518 struct r10conf
*conf
= mddev
->private;
1519 struct r10bio
*r10_bio
;
1521 r10_bio
= mempool_alloc(&conf
->r10bio_pool
, GFP_NOIO
);
1523 r10_bio
->master_bio
= bio
;
1524 r10_bio
->sectors
= sectors
;
1526 r10_bio
->mddev
= mddev
;
1527 r10_bio
->sector
= bio
->bi_iter
.bi_sector
;
1529 memset(r10_bio
->devs
, 0, sizeof(r10_bio
->devs
[0]) * conf
->copies
);
1531 if (bio_data_dir(bio
) == READ
)
1532 raid10_read_request(mddev
, bio
, r10_bio
);
1534 raid10_write_request(mddev
, bio
, r10_bio
);
1537 static bool raid10_make_request(struct mddev
*mddev
, struct bio
*bio
)
1539 struct r10conf
*conf
= mddev
->private;
1540 sector_t chunk_mask
= (conf
->geo
.chunk_mask
& conf
->prev
.chunk_mask
);
1541 int chunk_sects
= chunk_mask
+ 1;
1542 int sectors
= bio_sectors(bio
);
1544 if (unlikely(bio
->bi_opf
& REQ_PREFLUSH
)) {
1545 md_flush_request(mddev
, bio
);
1549 if (!md_write_start(mddev
, bio
))
1553 * If this request crosses a chunk boundary, we need to split
1556 if (unlikely((bio
->bi_iter
.bi_sector
& chunk_mask
) +
1557 sectors
> chunk_sects
1558 && (conf
->geo
.near_copies
< conf
->geo
.raid_disks
1559 || conf
->prev
.near_copies
<
1560 conf
->prev
.raid_disks
)))
1561 sectors
= chunk_sects
-
1562 (bio
->bi_iter
.bi_sector
&
1564 __make_request(mddev
, bio
, sectors
);
1566 /* In case raid10d snuck in to freeze_array */
1567 wake_up(&conf
->wait_barrier
);
1571 static void raid10_status(struct seq_file
*seq
, struct mddev
*mddev
)
1573 struct r10conf
*conf
= mddev
->private;
1576 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
)
1577 seq_printf(seq
, " %dK chunks", mddev
->chunk_sectors
/ 2);
1578 if (conf
->geo
.near_copies
> 1)
1579 seq_printf(seq
, " %d near-copies", conf
->geo
.near_copies
);
1580 if (conf
->geo
.far_copies
> 1) {
1581 if (conf
->geo
.far_offset
)
1582 seq_printf(seq
, " %d offset-copies", conf
->geo
.far_copies
);
1584 seq_printf(seq
, " %d far-copies", conf
->geo
.far_copies
);
1585 if (conf
->geo
.far_set_size
!= conf
->geo
.raid_disks
)
1586 seq_printf(seq
, " %d devices per set", conf
->geo
.far_set_size
);
1588 seq_printf(seq
, " [%d/%d] [", conf
->geo
.raid_disks
,
1589 conf
->geo
.raid_disks
- mddev
->degraded
);
1591 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1592 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
1593 seq_printf(seq
, "%s", rdev
&& test_bit(In_sync
, &rdev
->flags
) ? "U" : "_");
1596 seq_printf(seq
, "]");
1599 /* check if there are enough drives for
1600 * every block to appear on atleast one.
1601 * Don't consider the device numbered 'ignore'
1602 * as we might be about to remove it.
1604 static int _enough(struct r10conf
*conf
, int previous
, int ignore
)
1610 disks
= conf
->prev
.raid_disks
;
1611 ncopies
= conf
->prev
.near_copies
;
1613 disks
= conf
->geo
.raid_disks
;
1614 ncopies
= conf
->geo
.near_copies
;
1619 int n
= conf
->copies
;
1623 struct md_rdev
*rdev
;
1624 if (this != ignore
&&
1625 (rdev
= rcu_dereference(conf
->mirrors
[this].rdev
)) &&
1626 test_bit(In_sync
, &rdev
->flags
))
1628 this = (this+1) % disks
;
1632 first
= (first
+ ncopies
) % disks
;
1633 } while (first
!= 0);
1640 static int enough(struct r10conf
*conf
, int ignore
)
1642 /* when calling 'enough', both 'prev' and 'geo' must
1644 * This is ensured if ->reconfig_mutex or ->device_lock
1647 return _enough(conf
, 0, ignore
) &&
1648 _enough(conf
, 1, ignore
);
1651 static void raid10_error(struct mddev
*mddev
, struct md_rdev
*rdev
)
1653 char b
[BDEVNAME_SIZE
];
1654 struct r10conf
*conf
= mddev
->private;
1655 unsigned long flags
;
1658 * If it is not operational, then we have already marked it as dead
1659 * else if it is the last working disks, ignore the error, let the
1660 * next level up know.
1661 * else mark the drive as failed
1663 spin_lock_irqsave(&conf
->device_lock
, flags
);
1664 if (test_bit(In_sync
, &rdev
->flags
)
1665 && !enough(conf
, rdev
->raid_disk
)) {
1667 * Don't fail the drive, just return an IO error.
1669 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1672 if (test_and_clear_bit(In_sync
, &rdev
->flags
))
1675 * If recovery is running, make sure it aborts.
1677 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
1678 set_bit(Blocked
, &rdev
->flags
);
1679 set_bit(Faulty
, &rdev
->flags
);
1680 set_mask_bits(&mddev
->sb_flags
, 0,
1681 BIT(MD_SB_CHANGE_DEVS
) | BIT(MD_SB_CHANGE_PENDING
));
1682 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1683 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1684 "md/raid10:%s: Operation continuing on %d devices.\n",
1685 mdname(mddev
), bdevname(rdev
->bdev
, b
),
1686 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
);
1689 static void print_conf(struct r10conf
*conf
)
1692 struct md_rdev
*rdev
;
1694 pr_debug("RAID10 conf printout:\n");
1696 pr_debug("(!conf)\n");
1699 pr_debug(" --- wd:%d rd:%d\n", conf
->geo
.raid_disks
- conf
->mddev
->degraded
,
1700 conf
->geo
.raid_disks
);
1702 /* This is only called with ->reconfix_mutex held, so
1703 * rcu protection of rdev is not needed */
1704 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1705 char b
[BDEVNAME_SIZE
];
1706 rdev
= conf
->mirrors
[i
].rdev
;
1708 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1709 i
, !test_bit(In_sync
, &rdev
->flags
),
1710 !test_bit(Faulty
, &rdev
->flags
),
1711 bdevname(rdev
->bdev
,b
));
1715 static void close_sync(struct r10conf
*conf
)
1718 allow_barrier(conf
);
1720 mempool_exit(&conf
->r10buf_pool
);
1723 static int raid10_spare_active(struct mddev
*mddev
)
1726 struct r10conf
*conf
= mddev
->private;
1727 struct raid10_info
*tmp
;
1729 unsigned long flags
;
1732 * Find all non-in_sync disks within the RAID10 configuration
1733 * and mark them in_sync
1735 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
1736 tmp
= conf
->mirrors
+ i
;
1737 if (tmp
->replacement
1738 && tmp
->replacement
->recovery_offset
== MaxSector
1739 && !test_bit(Faulty
, &tmp
->replacement
->flags
)
1740 && !test_and_set_bit(In_sync
, &tmp
->replacement
->flags
)) {
1741 /* Replacement has just become active */
1743 || !test_and_clear_bit(In_sync
, &tmp
->rdev
->flags
))
1746 /* Replaced device not technically faulty,
1747 * but we need to be sure it gets removed
1748 * and never re-added.
1750 set_bit(Faulty
, &tmp
->rdev
->flags
);
1751 sysfs_notify_dirent_safe(
1752 tmp
->rdev
->sysfs_state
);
1754 sysfs_notify_dirent_safe(tmp
->replacement
->sysfs_state
);
1755 } else if (tmp
->rdev
1756 && tmp
->rdev
->recovery_offset
== MaxSector
1757 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
1758 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
1760 sysfs_notify_dirent_safe(tmp
->rdev
->sysfs_state
);
1763 spin_lock_irqsave(&conf
->device_lock
, flags
);
1764 mddev
->degraded
-= count
;
1765 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1771 static int raid10_add_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1773 struct r10conf
*conf
= mddev
->private;
1777 int last
= conf
->geo
.raid_disks
- 1;
1779 if (mddev
->recovery_cp
< MaxSector
)
1780 /* only hot-add to in-sync arrays, as recovery is
1781 * very different from resync
1784 if (rdev
->saved_raid_disk
< 0 && !_enough(conf
, 1, -1))
1787 if (md_integrity_add_rdev(rdev
, mddev
))
1790 if (rdev
->raid_disk
>= 0)
1791 first
= last
= rdev
->raid_disk
;
1793 if (rdev
->saved_raid_disk
>= first
&&
1794 rdev
->saved_raid_disk
< conf
->geo
.raid_disks
&&
1795 conf
->mirrors
[rdev
->saved_raid_disk
].rdev
== NULL
)
1796 mirror
= rdev
->saved_raid_disk
;
1799 for ( ; mirror
<= last
; mirror
++) {
1800 struct raid10_info
*p
= &conf
->mirrors
[mirror
];
1801 if (p
->recovery_disabled
== mddev
->recovery_disabled
)
1804 if (!test_bit(WantReplacement
, &p
->rdev
->flags
) ||
1805 p
->replacement
!= NULL
)
1807 clear_bit(In_sync
, &rdev
->flags
);
1808 set_bit(Replacement
, &rdev
->flags
);
1809 rdev
->raid_disk
= mirror
;
1812 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1813 rdev
->data_offset
<< 9);
1815 rcu_assign_pointer(p
->replacement
, rdev
);
1820 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
1821 rdev
->data_offset
<< 9);
1823 p
->head_position
= 0;
1824 p
->recovery_disabled
= mddev
->recovery_disabled
- 1;
1825 rdev
->raid_disk
= mirror
;
1827 if (rdev
->saved_raid_disk
!= mirror
)
1829 rcu_assign_pointer(p
->rdev
, rdev
);
1832 if (mddev
->queue
&& blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
1833 blk_queue_flag_set(QUEUE_FLAG_DISCARD
, mddev
->queue
);
1839 static int raid10_remove_disk(struct mddev
*mddev
, struct md_rdev
*rdev
)
1841 struct r10conf
*conf
= mddev
->private;
1843 int number
= rdev
->raid_disk
;
1844 struct md_rdev
**rdevp
;
1845 struct raid10_info
*p
= conf
->mirrors
+ number
;
1848 if (rdev
== p
->rdev
)
1850 else if (rdev
== p
->replacement
)
1851 rdevp
= &p
->replacement
;
1855 if (test_bit(In_sync
, &rdev
->flags
) ||
1856 atomic_read(&rdev
->nr_pending
)) {
1860 /* Only remove non-faulty devices if recovery
1863 if (!test_bit(Faulty
, &rdev
->flags
) &&
1864 mddev
->recovery_disabled
!= p
->recovery_disabled
&&
1865 (!p
->replacement
|| p
->replacement
== rdev
) &&
1866 number
< conf
->geo
.raid_disks
&&
1872 if (!test_bit(RemoveSynchronized
, &rdev
->flags
)) {
1874 if (atomic_read(&rdev
->nr_pending
)) {
1875 /* lost the race, try later */
1881 if (p
->replacement
) {
1882 /* We must have just cleared 'rdev' */
1883 p
->rdev
= p
->replacement
;
1884 clear_bit(Replacement
, &p
->replacement
->flags
);
1885 smp_mb(); /* Make sure other CPUs may see both as identical
1886 * but will never see neither -- if they are careful.
1888 p
->replacement
= NULL
;
1891 clear_bit(WantReplacement
, &rdev
->flags
);
1892 err
= md_integrity_register(mddev
);
1900 static void __end_sync_read(struct r10bio
*r10_bio
, struct bio
*bio
, int d
)
1902 struct r10conf
*conf
= r10_bio
->mddev
->private;
1904 if (!bio
->bi_status
)
1905 set_bit(R10BIO_Uptodate
, &r10_bio
->state
);
1907 /* The write handler will notice the lack of
1908 * R10BIO_Uptodate and record any errors etc
1910 atomic_add(r10_bio
->sectors
,
1911 &conf
->mirrors
[d
].rdev
->corrected_errors
);
1913 /* for reconstruct, we always reschedule after a read.
1914 * for resync, only after all reads
1916 rdev_dec_pending(conf
->mirrors
[d
].rdev
, conf
->mddev
);
1917 if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
) ||
1918 atomic_dec_and_test(&r10_bio
->remaining
)) {
1919 /* we have read all the blocks,
1920 * do the comparison in process context in raid10d
1922 reschedule_retry(r10_bio
);
1926 static void end_sync_read(struct bio
*bio
)
1928 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
1929 struct r10conf
*conf
= r10_bio
->mddev
->private;
1930 int d
= find_bio_disk(conf
, r10_bio
, bio
, NULL
, NULL
);
1932 __end_sync_read(r10_bio
, bio
, d
);
1935 static void end_reshape_read(struct bio
*bio
)
1937 /* reshape read bio isn't allocated from r10buf_pool */
1938 struct r10bio
*r10_bio
= bio
->bi_private
;
1940 __end_sync_read(r10_bio
, bio
, r10_bio
->read_slot
);
1943 static void end_sync_request(struct r10bio
*r10_bio
)
1945 struct mddev
*mddev
= r10_bio
->mddev
;
1947 while (atomic_dec_and_test(&r10_bio
->remaining
)) {
1948 if (r10_bio
->master_bio
== NULL
) {
1949 /* the primary of several recovery bios */
1950 sector_t s
= r10_bio
->sectors
;
1951 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
1952 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
1953 reschedule_retry(r10_bio
);
1956 md_done_sync(mddev
, s
, 1);
1959 struct r10bio
*r10_bio2
= (struct r10bio
*)r10_bio
->master_bio
;
1960 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
1961 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
1962 reschedule_retry(r10_bio
);
1970 static void end_sync_write(struct bio
*bio
)
1972 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
1973 struct mddev
*mddev
= r10_bio
->mddev
;
1974 struct r10conf
*conf
= mddev
->private;
1980 struct md_rdev
*rdev
= NULL
;
1982 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
1984 rdev
= conf
->mirrors
[d
].replacement
;
1986 rdev
= conf
->mirrors
[d
].rdev
;
1988 if (bio
->bi_status
) {
1990 md_error(mddev
, rdev
);
1992 set_bit(WriteErrorSeen
, &rdev
->flags
);
1993 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
1994 set_bit(MD_RECOVERY_NEEDED
,
1995 &rdev
->mddev
->recovery
);
1996 set_bit(R10BIO_WriteError
, &r10_bio
->state
);
1998 } else if (is_badblock(rdev
,
1999 r10_bio
->devs
[slot
].addr
,
2001 &first_bad
, &bad_sectors
))
2002 set_bit(R10BIO_MadeGood
, &r10_bio
->state
);
2004 rdev_dec_pending(rdev
, mddev
);
2006 end_sync_request(r10_bio
);
2010 * Note: sync and recover and handled very differently for raid10
2011 * This code is for resync.
2012 * For resync, we read through virtual addresses and read all blocks.
2013 * If there is any error, we schedule a write. The lowest numbered
2014 * drive is authoritative.
2015 * However requests come for physical address, so we need to map.
2016 * For every physical address there are raid_disks/copies virtual addresses,
2017 * which is always are least one, but is not necessarly an integer.
2018 * This means that a physical address can span multiple chunks, so we may
2019 * have to submit multiple io requests for a single sync request.
2022 * We check if all blocks are in-sync and only write to blocks that
2025 static void sync_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2027 struct r10conf
*conf
= mddev
->private;
2029 struct bio
*tbio
, *fbio
;
2031 struct page
**tpages
, **fpages
;
2033 atomic_set(&r10_bio
->remaining
, 1);
2035 /* find the first device with a block */
2036 for (i
=0; i
<conf
->copies
; i
++)
2037 if (!r10_bio
->devs
[i
].bio
->bi_status
)
2040 if (i
== conf
->copies
)
2044 fbio
= r10_bio
->devs
[i
].bio
;
2045 fbio
->bi_iter
.bi_size
= r10_bio
->sectors
<< 9;
2046 fbio
->bi_iter
.bi_idx
= 0;
2047 fpages
= get_resync_pages(fbio
)->pages
;
2049 vcnt
= (r10_bio
->sectors
+ (PAGE_SIZE
>> 9) - 1) >> (PAGE_SHIFT
- 9);
2050 /* now find blocks with errors */
2051 for (i
=0 ; i
< conf
->copies
; i
++) {
2053 struct md_rdev
*rdev
;
2054 struct resync_pages
*rp
;
2056 tbio
= r10_bio
->devs
[i
].bio
;
2058 if (tbio
->bi_end_io
!= end_sync_read
)
2063 tpages
= get_resync_pages(tbio
)->pages
;
2064 d
= r10_bio
->devs
[i
].devnum
;
2065 rdev
= conf
->mirrors
[d
].rdev
;
2066 if (!r10_bio
->devs
[i
].bio
->bi_status
) {
2067 /* We know that the bi_io_vec layout is the same for
2068 * both 'first' and 'i', so we just compare them.
2069 * All vec entries are PAGE_SIZE;
2071 int sectors
= r10_bio
->sectors
;
2072 for (j
= 0; j
< vcnt
; j
++) {
2073 int len
= PAGE_SIZE
;
2074 if (sectors
< (len
/ 512))
2075 len
= sectors
* 512;
2076 if (memcmp(page_address(fpages
[j
]),
2077 page_address(tpages
[j
]),
2084 atomic64_add(r10_bio
->sectors
, &mddev
->resync_mismatches
);
2085 if (test_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
))
2086 /* Don't fix anything. */
2088 } else if (test_bit(FailFast
, &rdev
->flags
)) {
2089 /* Just give up on this device */
2090 md_error(rdev
->mddev
, rdev
);
2093 /* Ok, we need to write this bio, either to correct an
2094 * inconsistency or to correct an unreadable block.
2095 * First we need to fixup bv_offset, bv_len and
2096 * bi_vecs, as the read request might have corrupted these
2098 rp
= get_resync_pages(tbio
);
2101 md_bio_reset_resync_pages(tbio
, rp
, fbio
->bi_iter
.bi_size
);
2103 rp
->raid_bio
= r10_bio
;
2104 tbio
->bi_private
= rp
;
2105 tbio
->bi_iter
.bi_sector
= r10_bio
->devs
[i
].addr
;
2106 tbio
->bi_end_io
= end_sync_write
;
2107 bio_set_op_attrs(tbio
, REQ_OP_WRITE
, 0);
2109 bio_copy_data(tbio
, fbio
);
2111 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2112 atomic_inc(&r10_bio
->remaining
);
2113 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, bio_sectors(tbio
));
2115 if (test_bit(FailFast
, &conf
->mirrors
[d
].rdev
->flags
))
2116 tbio
->bi_opf
|= MD_FAILFAST
;
2117 tbio
->bi_iter
.bi_sector
+= conf
->mirrors
[d
].rdev
->data_offset
;
2118 bio_set_dev(tbio
, conf
->mirrors
[d
].rdev
->bdev
);
2119 generic_make_request(tbio
);
2122 /* Now write out to any replacement devices
2125 for (i
= 0; i
< conf
->copies
; i
++) {
2128 tbio
= r10_bio
->devs
[i
].repl_bio
;
2129 if (!tbio
|| !tbio
->bi_end_io
)
2131 if (r10_bio
->devs
[i
].bio
->bi_end_io
!= end_sync_write
2132 && r10_bio
->devs
[i
].bio
!= fbio
)
2133 bio_copy_data(tbio
, fbio
);
2134 d
= r10_bio
->devs
[i
].devnum
;
2135 atomic_inc(&r10_bio
->remaining
);
2136 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2138 generic_make_request(tbio
);
2142 if (atomic_dec_and_test(&r10_bio
->remaining
)) {
2143 md_done_sync(mddev
, r10_bio
->sectors
, 1);
2149 * Now for the recovery code.
2150 * Recovery happens across physical sectors.
2151 * We recover all non-is_sync drives by finding the virtual address of
2152 * each, and then choose a working drive that also has that virt address.
2153 * There is a separate r10_bio for each non-in_sync drive.
2154 * Only the first two slots are in use. The first for reading,
2155 * The second for writing.
2158 static void fix_recovery_read_error(struct r10bio
*r10_bio
)
2160 /* We got a read error during recovery.
2161 * We repeat the read in smaller page-sized sections.
2162 * If a read succeeds, write it to the new device or record
2163 * a bad block if we cannot.
2164 * If a read fails, record a bad block on both old and
2167 struct mddev
*mddev
= r10_bio
->mddev
;
2168 struct r10conf
*conf
= mddev
->private;
2169 struct bio
*bio
= r10_bio
->devs
[0].bio
;
2171 int sectors
= r10_bio
->sectors
;
2173 int dr
= r10_bio
->devs
[0].devnum
;
2174 int dw
= r10_bio
->devs
[1].devnum
;
2175 struct page
**pages
= get_resync_pages(bio
)->pages
;
2179 struct md_rdev
*rdev
;
2183 if (s
> (PAGE_SIZE
>>9))
2186 rdev
= conf
->mirrors
[dr
].rdev
;
2187 addr
= r10_bio
->devs
[0].addr
+ sect
,
2188 ok
= sync_page_io(rdev
,
2192 REQ_OP_READ
, 0, false);
2194 rdev
= conf
->mirrors
[dw
].rdev
;
2195 addr
= r10_bio
->devs
[1].addr
+ sect
;
2196 ok
= sync_page_io(rdev
,
2200 REQ_OP_WRITE
, 0, false);
2202 set_bit(WriteErrorSeen
, &rdev
->flags
);
2203 if (!test_and_set_bit(WantReplacement
,
2205 set_bit(MD_RECOVERY_NEEDED
,
2206 &rdev
->mddev
->recovery
);
2210 /* We don't worry if we cannot set a bad block -
2211 * it really is bad so there is no loss in not
2214 rdev_set_badblocks(rdev
, addr
, s
, 0);
2216 if (rdev
!= conf
->mirrors
[dw
].rdev
) {
2217 /* need bad block on destination too */
2218 struct md_rdev
*rdev2
= conf
->mirrors
[dw
].rdev
;
2219 addr
= r10_bio
->devs
[1].addr
+ sect
;
2220 ok
= rdev_set_badblocks(rdev2
, addr
, s
, 0);
2222 /* just abort the recovery */
2223 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2226 conf
->mirrors
[dw
].recovery_disabled
2227 = mddev
->recovery_disabled
;
2228 set_bit(MD_RECOVERY_INTR
,
2241 static void recovery_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2243 struct r10conf
*conf
= mddev
->private;
2245 struct bio
*wbio
, *wbio2
;
2247 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
)) {
2248 fix_recovery_read_error(r10_bio
);
2249 end_sync_request(r10_bio
);
2254 * share the pages with the first bio
2255 * and submit the write request
2257 d
= r10_bio
->devs
[1].devnum
;
2258 wbio
= r10_bio
->devs
[1].bio
;
2259 wbio2
= r10_bio
->devs
[1].repl_bio
;
2260 /* Need to test wbio2->bi_end_io before we call
2261 * generic_make_request as if the former is NULL,
2262 * the latter is free to free wbio2.
2264 if (wbio2
&& !wbio2
->bi_end_io
)
2266 if (wbio
->bi_end_io
) {
2267 atomic_inc(&conf
->mirrors
[d
].rdev
->nr_pending
);
2268 md_sync_acct(conf
->mirrors
[d
].rdev
->bdev
, bio_sectors(wbio
));
2269 generic_make_request(wbio
);
2272 atomic_inc(&conf
->mirrors
[d
].replacement
->nr_pending
);
2273 md_sync_acct(conf
->mirrors
[d
].replacement
->bdev
,
2274 bio_sectors(wbio2
));
2275 generic_make_request(wbio2
);
2280 * Used by fix_read_error() to decay the per rdev read_errors.
2281 * We halve the read error count for every hour that has elapsed
2282 * since the last recorded read error.
2285 static void check_decay_read_errors(struct mddev
*mddev
, struct md_rdev
*rdev
)
2288 unsigned long hours_since_last
;
2289 unsigned int read_errors
= atomic_read(&rdev
->read_errors
);
2291 cur_time_mon
= ktime_get_seconds();
2293 if (rdev
->last_read_error
== 0) {
2294 /* first time we've seen a read error */
2295 rdev
->last_read_error
= cur_time_mon
;
2299 hours_since_last
= (long)(cur_time_mon
-
2300 rdev
->last_read_error
) / 3600;
2302 rdev
->last_read_error
= cur_time_mon
;
2305 * if hours_since_last is > the number of bits in read_errors
2306 * just set read errors to 0. We do this to avoid
2307 * overflowing the shift of read_errors by hours_since_last.
2309 if (hours_since_last
>= 8 * sizeof(read_errors
))
2310 atomic_set(&rdev
->read_errors
, 0);
2312 atomic_set(&rdev
->read_errors
, read_errors
>> hours_since_last
);
2315 static int r10_sync_page_io(struct md_rdev
*rdev
, sector_t sector
,
2316 int sectors
, struct page
*page
, int rw
)
2321 if (is_badblock(rdev
, sector
, sectors
, &first_bad
, &bad_sectors
)
2322 && (rw
== READ
|| test_bit(WriteErrorSeen
, &rdev
->flags
)))
2324 if (sync_page_io(rdev
, sector
, sectors
<< 9, page
, rw
, 0, false))
2328 set_bit(WriteErrorSeen
, &rdev
->flags
);
2329 if (!test_and_set_bit(WantReplacement
, &rdev
->flags
))
2330 set_bit(MD_RECOVERY_NEEDED
,
2331 &rdev
->mddev
->recovery
);
2333 /* need to record an error - either for the block or the device */
2334 if (!rdev_set_badblocks(rdev
, sector
, sectors
, 0))
2335 md_error(rdev
->mddev
, rdev
);
2340 * This is a kernel thread which:
2342 * 1. Retries failed read operations on working mirrors.
2343 * 2. Updates the raid superblock when problems encounter.
2344 * 3. Performs writes following reads for array synchronising.
2347 static void fix_read_error(struct r10conf
*conf
, struct mddev
*mddev
, struct r10bio
*r10_bio
)
2349 int sect
= 0; /* Offset from r10_bio->sector */
2350 int sectors
= r10_bio
->sectors
;
2351 struct md_rdev
*rdev
;
2352 int max_read_errors
= atomic_read(&mddev
->max_corr_read_errors
);
2353 int d
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
2355 /* still own a reference to this rdev, so it cannot
2356 * have been cleared recently.
2358 rdev
= conf
->mirrors
[d
].rdev
;
2360 if (test_bit(Faulty
, &rdev
->flags
))
2361 /* drive has already been failed, just ignore any
2362 more fix_read_error() attempts */
2365 check_decay_read_errors(mddev
, rdev
);
2366 atomic_inc(&rdev
->read_errors
);
2367 if (atomic_read(&rdev
->read_errors
) > max_read_errors
) {
2368 char b
[BDEVNAME_SIZE
];
2369 bdevname(rdev
->bdev
, b
);
2371 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2373 atomic_read(&rdev
->read_errors
), max_read_errors
);
2374 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2376 md_error(mddev
, rdev
);
2377 r10_bio
->devs
[r10_bio
->read_slot
].bio
= IO_BLOCKED
;
2383 int sl
= r10_bio
->read_slot
;
2387 if (s
> (PAGE_SIZE
>>9))
2395 d
= r10_bio
->devs
[sl
].devnum
;
2396 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2398 test_bit(In_sync
, &rdev
->flags
) &&
2399 !test_bit(Faulty
, &rdev
->flags
) &&
2400 is_badblock(rdev
, r10_bio
->devs
[sl
].addr
+ sect
, s
,
2401 &first_bad
, &bad_sectors
) == 0) {
2402 atomic_inc(&rdev
->nr_pending
);
2404 success
= sync_page_io(rdev
,
2405 r10_bio
->devs
[sl
].addr
+
2409 REQ_OP_READ
, 0, false);
2410 rdev_dec_pending(rdev
, mddev
);
2416 if (sl
== conf
->copies
)
2418 } while (!success
&& sl
!= r10_bio
->read_slot
);
2422 /* Cannot read from anywhere, just mark the block
2423 * as bad on the first device to discourage future
2426 int dn
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
2427 rdev
= conf
->mirrors
[dn
].rdev
;
2429 if (!rdev_set_badblocks(
2431 r10_bio
->devs
[r10_bio
->read_slot
].addr
2434 md_error(mddev
, rdev
);
2435 r10_bio
->devs
[r10_bio
->read_slot
].bio
2442 /* write it back and re-read */
2444 while (sl
!= r10_bio
->read_slot
) {
2445 char b
[BDEVNAME_SIZE
];
2450 d
= r10_bio
->devs
[sl
].devnum
;
2451 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2453 test_bit(Faulty
, &rdev
->flags
) ||
2454 !test_bit(In_sync
, &rdev
->flags
))
2457 atomic_inc(&rdev
->nr_pending
);
2459 if (r10_sync_page_io(rdev
,
2460 r10_bio
->devs
[sl
].addr
+
2462 s
, conf
->tmppage
, WRITE
)
2464 /* Well, this device is dead */
2465 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2467 (unsigned long long)(
2469 choose_data_offset(r10_bio
,
2471 bdevname(rdev
->bdev
, b
));
2472 pr_notice("md/raid10:%s: %s: failing drive\n",
2474 bdevname(rdev
->bdev
, b
));
2476 rdev_dec_pending(rdev
, mddev
);
2480 while (sl
!= r10_bio
->read_slot
) {
2481 char b
[BDEVNAME_SIZE
];
2486 d
= r10_bio
->devs
[sl
].devnum
;
2487 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
2489 test_bit(Faulty
, &rdev
->flags
) ||
2490 !test_bit(In_sync
, &rdev
->flags
))
2493 atomic_inc(&rdev
->nr_pending
);
2495 switch (r10_sync_page_io(rdev
,
2496 r10_bio
->devs
[sl
].addr
+
2501 /* Well, this device is dead */
2502 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2504 (unsigned long long)(
2506 choose_data_offset(r10_bio
, rdev
)),
2507 bdevname(rdev
->bdev
, b
));
2508 pr_notice("md/raid10:%s: %s: failing drive\n",
2510 bdevname(rdev
->bdev
, b
));
2513 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2515 (unsigned long long)(
2517 choose_data_offset(r10_bio
, rdev
)),
2518 bdevname(rdev
->bdev
, b
));
2519 atomic_add(s
, &rdev
->corrected_errors
);
2522 rdev_dec_pending(rdev
, mddev
);
2532 static int narrow_write_error(struct r10bio
*r10_bio
, int i
)
2534 struct bio
*bio
= r10_bio
->master_bio
;
2535 struct mddev
*mddev
= r10_bio
->mddev
;
2536 struct r10conf
*conf
= mddev
->private;
2537 struct md_rdev
*rdev
= conf
->mirrors
[r10_bio
->devs
[i
].devnum
].rdev
;
2538 /* bio has the data to be written to slot 'i' where
2539 * we just recently had a write error.
2540 * We repeatedly clone the bio and trim down to one block,
2541 * then try the write. Where the write fails we record
2543 * It is conceivable that the bio doesn't exactly align with
2544 * blocks. We must handle this.
2546 * We currently own a reference to the rdev.
2552 int sect_to_write
= r10_bio
->sectors
;
2555 if (rdev
->badblocks
.shift
< 0)
2558 block_sectors
= roundup(1 << rdev
->badblocks
.shift
,
2559 bdev_logical_block_size(rdev
->bdev
) >> 9);
2560 sector
= r10_bio
->sector
;
2561 sectors
= ((r10_bio
->sector
+ block_sectors
)
2562 & ~(sector_t
)(block_sectors
- 1))
2565 while (sect_to_write
) {
2568 if (sectors
> sect_to_write
)
2569 sectors
= sect_to_write
;
2570 /* Write at 'sector' for 'sectors' */
2571 wbio
= bio_clone_fast(bio
, GFP_NOIO
, &mddev
->bio_set
);
2572 bio_trim(wbio
, sector
- bio
->bi_iter
.bi_sector
, sectors
);
2573 wsector
= r10_bio
->devs
[i
].addr
+ (sector
- r10_bio
->sector
);
2574 wbio
->bi_iter
.bi_sector
= wsector
+
2575 choose_data_offset(r10_bio
, rdev
);
2576 bio_set_dev(wbio
, rdev
->bdev
);
2577 bio_set_op_attrs(wbio
, REQ_OP_WRITE
, 0);
2579 if (submit_bio_wait(wbio
) < 0)
2581 ok
= rdev_set_badblocks(rdev
, wsector
,
2586 sect_to_write
-= sectors
;
2588 sectors
= block_sectors
;
2593 static void handle_read_error(struct mddev
*mddev
, struct r10bio
*r10_bio
)
2595 int slot
= r10_bio
->read_slot
;
2597 struct r10conf
*conf
= mddev
->private;
2598 struct md_rdev
*rdev
= r10_bio
->devs
[slot
].rdev
;
2600 /* we got a read error. Maybe the drive is bad. Maybe just
2601 * the block and we can fix it.
2602 * We freeze all other IO, and try reading the block from
2603 * other devices. When we find one, we re-write
2604 * and check it that fixes the read error.
2605 * This is all done synchronously while the array is
2608 bio
= r10_bio
->devs
[slot
].bio
;
2610 r10_bio
->devs
[slot
].bio
= NULL
;
2613 r10_bio
->devs
[slot
].bio
= IO_BLOCKED
;
2614 else if (!test_bit(FailFast
, &rdev
->flags
)) {
2615 freeze_array(conf
, 1);
2616 fix_read_error(conf
, mddev
, r10_bio
);
2617 unfreeze_array(conf
);
2619 md_error(mddev
, rdev
);
2621 rdev_dec_pending(rdev
, mddev
);
2622 allow_barrier(conf
);
2624 raid10_read_request(mddev
, r10_bio
->master_bio
, r10_bio
);
2627 static void handle_write_completed(struct r10conf
*conf
, struct r10bio
*r10_bio
)
2629 /* Some sort of write request has finished and it
2630 * succeeded in writing where we thought there was a
2631 * bad block. So forget the bad block.
2632 * Or possibly if failed and we need to record
2636 struct md_rdev
*rdev
;
2638 if (test_bit(R10BIO_IsSync
, &r10_bio
->state
) ||
2639 test_bit(R10BIO_IsRecover
, &r10_bio
->state
)) {
2640 for (m
= 0; m
< conf
->copies
; m
++) {
2641 int dev
= r10_bio
->devs
[m
].devnum
;
2642 rdev
= conf
->mirrors
[dev
].rdev
;
2643 if (r10_bio
->devs
[m
].bio
== NULL
||
2644 r10_bio
->devs
[m
].bio
->bi_end_io
== NULL
)
2646 if (!r10_bio
->devs
[m
].bio
->bi_status
) {
2647 rdev_clear_badblocks(
2649 r10_bio
->devs
[m
].addr
,
2650 r10_bio
->sectors
, 0);
2652 if (!rdev_set_badblocks(
2654 r10_bio
->devs
[m
].addr
,
2655 r10_bio
->sectors
, 0))
2656 md_error(conf
->mddev
, rdev
);
2658 rdev
= conf
->mirrors
[dev
].replacement
;
2659 if (r10_bio
->devs
[m
].repl_bio
== NULL
||
2660 r10_bio
->devs
[m
].repl_bio
->bi_end_io
== NULL
)
2663 if (!r10_bio
->devs
[m
].repl_bio
->bi_status
) {
2664 rdev_clear_badblocks(
2666 r10_bio
->devs
[m
].addr
,
2667 r10_bio
->sectors
, 0);
2669 if (!rdev_set_badblocks(
2671 r10_bio
->devs
[m
].addr
,
2672 r10_bio
->sectors
, 0))
2673 md_error(conf
->mddev
, rdev
);
2679 for (m
= 0; m
< conf
->copies
; m
++) {
2680 int dev
= r10_bio
->devs
[m
].devnum
;
2681 struct bio
*bio
= r10_bio
->devs
[m
].bio
;
2682 rdev
= conf
->mirrors
[dev
].rdev
;
2683 if (bio
== IO_MADE_GOOD
) {
2684 rdev_clear_badblocks(
2686 r10_bio
->devs
[m
].addr
,
2687 r10_bio
->sectors
, 0);
2688 rdev_dec_pending(rdev
, conf
->mddev
);
2689 } else if (bio
!= NULL
&& bio
->bi_status
) {
2691 if (!narrow_write_error(r10_bio
, m
)) {
2692 md_error(conf
->mddev
, rdev
);
2693 set_bit(R10BIO_Degraded
,
2696 rdev_dec_pending(rdev
, conf
->mddev
);
2698 bio
= r10_bio
->devs
[m
].repl_bio
;
2699 rdev
= conf
->mirrors
[dev
].replacement
;
2700 if (rdev
&& bio
== IO_MADE_GOOD
) {
2701 rdev_clear_badblocks(
2703 r10_bio
->devs
[m
].addr
,
2704 r10_bio
->sectors
, 0);
2705 rdev_dec_pending(rdev
, conf
->mddev
);
2709 spin_lock_irq(&conf
->device_lock
);
2710 list_add(&r10_bio
->retry_list
, &conf
->bio_end_io_list
);
2712 spin_unlock_irq(&conf
->device_lock
);
2714 * In case freeze_array() is waiting for condition
2715 * nr_pending == nr_queued + extra to be true.
2717 wake_up(&conf
->wait_barrier
);
2718 md_wakeup_thread(conf
->mddev
->thread
);
2720 if (test_bit(R10BIO_WriteError
,
2722 close_write(r10_bio
);
2723 raid_end_bio_io(r10_bio
);
2728 static void raid10d(struct md_thread
*thread
)
2730 struct mddev
*mddev
= thread
->mddev
;
2731 struct r10bio
*r10_bio
;
2732 unsigned long flags
;
2733 struct r10conf
*conf
= mddev
->private;
2734 struct list_head
*head
= &conf
->retry_list
;
2735 struct blk_plug plug
;
2737 md_check_recovery(mddev
);
2739 if (!list_empty_careful(&conf
->bio_end_io_list
) &&
2740 !test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
2742 spin_lock_irqsave(&conf
->device_lock
, flags
);
2743 if (!test_bit(MD_SB_CHANGE_PENDING
, &mddev
->sb_flags
)) {
2744 while (!list_empty(&conf
->bio_end_io_list
)) {
2745 list_move(conf
->bio_end_io_list
.prev
, &tmp
);
2749 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2750 while (!list_empty(&tmp
)) {
2751 r10_bio
= list_first_entry(&tmp
, struct r10bio
,
2753 list_del(&r10_bio
->retry_list
);
2754 if (mddev
->degraded
)
2755 set_bit(R10BIO_Degraded
, &r10_bio
->state
);
2757 if (test_bit(R10BIO_WriteError
,
2759 close_write(r10_bio
);
2760 raid_end_bio_io(r10_bio
);
2764 blk_start_plug(&plug
);
2767 flush_pending_writes(conf
);
2769 spin_lock_irqsave(&conf
->device_lock
, flags
);
2770 if (list_empty(head
)) {
2771 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2774 r10_bio
= list_entry(head
->prev
, struct r10bio
, retry_list
);
2775 list_del(head
->prev
);
2777 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2779 mddev
= r10_bio
->mddev
;
2780 conf
= mddev
->private;
2781 if (test_bit(R10BIO_MadeGood
, &r10_bio
->state
) ||
2782 test_bit(R10BIO_WriteError
, &r10_bio
->state
))
2783 handle_write_completed(conf
, r10_bio
);
2784 else if (test_bit(R10BIO_IsReshape
, &r10_bio
->state
))
2785 reshape_request_write(mddev
, r10_bio
);
2786 else if (test_bit(R10BIO_IsSync
, &r10_bio
->state
))
2787 sync_request_write(mddev
, r10_bio
);
2788 else if (test_bit(R10BIO_IsRecover
, &r10_bio
->state
))
2789 recovery_request_write(mddev
, r10_bio
);
2790 else if (test_bit(R10BIO_ReadError
, &r10_bio
->state
))
2791 handle_read_error(mddev
, r10_bio
);
2796 if (mddev
->sb_flags
& ~(1<<MD_SB_CHANGE_PENDING
))
2797 md_check_recovery(mddev
);
2799 blk_finish_plug(&plug
);
2802 static int init_resync(struct r10conf
*conf
)
2806 buffs
= RESYNC_WINDOW
/ RESYNC_BLOCK_SIZE
;
2807 BUG_ON(mempool_initialized(&conf
->r10buf_pool
));
2808 conf
->have_replacement
= 0;
2809 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++)
2810 if (conf
->mirrors
[i
].replacement
)
2811 conf
->have_replacement
= 1;
2812 ret
= mempool_init(&conf
->r10buf_pool
, buffs
,
2813 r10buf_pool_alloc
, r10buf_pool_free
, conf
);
2816 conf
->next_resync
= 0;
2820 static struct r10bio
*raid10_alloc_init_r10buf(struct r10conf
*conf
)
2822 struct r10bio
*r10bio
= mempool_alloc(&conf
->r10buf_pool
, GFP_NOIO
);
2823 struct rsync_pages
*rp
;
2828 if (test_bit(MD_RECOVERY_SYNC
, &conf
->mddev
->recovery
) ||
2829 test_bit(MD_RECOVERY_RESHAPE
, &conf
->mddev
->recovery
))
2830 nalloc
= conf
->copies
; /* resync */
2832 nalloc
= 2; /* recovery */
2834 for (i
= 0; i
< nalloc
; i
++) {
2835 bio
= r10bio
->devs
[i
].bio
;
2836 rp
= bio
->bi_private
;
2838 bio
->bi_private
= rp
;
2839 bio
= r10bio
->devs
[i
].repl_bio
;
2841 rp
= bio
->bi_private
;
2843 bio
->bi_private
= rp
;
2850 * Set cluster_sync_high since we need other nodes to add the
2851 * range [cluster_sync_low, cluster_sync_high] to suspend list.
2853 static void raid10_set_cluster_sync_high(struct r10conf
*conf
)
2855 sector_t window_size
;
2856 int extra_chunk
, chunks
;
2859 * First, here we define "stripe" as a unit which across
2860 * all member devices one time, so we get chunks by use
2861 * raid_disks / near_copies. Otherwise, if near_copies is
2862 * close to raid_disks, then resync window could increases
2863 * linearly with the increase of raid_disks, which means
2864 * we will suspend a really large IO window while it is not
2865 * necessary. If raid_disks is not divisible by near_copies,
2866 * an extra chunk is needed to ensure the whole "stripe" is
2870 chunks
= conf
->geo
.raid_disks
/ conf
->geo
.near_copies
;
2871 if (conf
->geo
.raid_disks
% conf
->geo
.near_copies
== 0)
2875 window_size
= (chunks
+ extra_chunk
) * conf
->mddev
->chunk_sectors
;
2878 * At least use a 32M window to align with raid1's resync window
2880 window_size
= (CLUSTER_RESYNC_WINDOW_SECTORS
> window_size
) ?
2881 CLUSTER_RESYNC_WINDOW_SECTORS
: window_size
;
2883 conf
->cluster_sync_high
= conf
->cluster_sync_low
+ window_size
;
2887 * perform a "sync" on one "block"
2889 * We need to make sure that no normal I/O request - particularly write
2890 * requests - conflict with active sync requests.
2892 * This is achieved by tracking pending requests and a 'barrier' concept
2893 * that can be installed to exclude normal IO requests.
2895 * Resync and recovery are handled very differently.
2896 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2898 * For resync, we iterate over virtual addresses, read all copies,
2899 * and update if there are differences. If only one copy is live,
2901 * For recovery, we iterate over physical addresses, read a good
2902 * value for each non-in_sync drive, and over-write.
2904 * So, for recovery we may have several outstanding complex requests for a
2905 * given address, one for each out-of-sync device. We model this by allocating
2906 * a number of r10_bio structures, one for each out-of-sync device.
2907 * As we setup these structures, we collect all bio's together into a list
2908 * which we then process collectively to add pages, and then process again
2909 * to pass to generic_make_request.
2911 * The r10_bio structures are linked using a borrowed master_bio pointer.
2912 * This link is counted in ->remaining. When the r10_bio that points to NULL
2913 * has its remaining count decremented to 0, the whole complex operation
2918 static sector_t
raid10_sync_request(struct mddev
*mddev
, sector_t sector_nr
,
2921 struct r10conf
*conf
= mddev
->private;
2922 struct r10bio
*r10_bio
;
2923 struct bio
*biolist
= NULL
, *bio
;
2924 sector_t max_sector
, nr_sectors
;
2927 sector_t sync_blocks
;
2928 sector_t sectors_skipped
= 0;
2929 int chunks_skipped
= 0;
2930 sector_t chunk_mask
= conf
->geo
.chunk_mask
;
2933 if (!mempool_initialized(&conf
->r10buf_pool
))
2934 if (init_resync(conf
))
2938 * Allow skipping a full rebuild for incremental assembly
2939 * of a clean array, like RAID1 does.
2941 if (mddev
->bitmap
== NULL
&&
2942 mddev
->recovery_cp
== MaxSector
&&
2943 mddev
->reshape_position
== MaxSector
&&
2944 !test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) &&
2945 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
2946 !test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
) &&
2947 conf
->fullsync
== 0) {
2949 return mddev
->dev_sectors
- sector_nr
;
2953 max_sector
= mddev
->dev_sectors
;
2954 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
) ||
2955 test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2956 max_sector
= mddev
->resync_max_sectors
;
2957 if (sector_nr
>= max_sector
) {
2958 conf
->cluster_sync_low
= 0;
2959 conf
->cluster_sync_high
= 0;
2961 /* If we aborted, we need to abort the
2962 * sync on the 'current' bitmap chucks (there can
2963 * be several when recovering multiple devices).
2964 * as we may have started syncing it but not finished.
2965 * We can find the current address in
2966 * mddev->curr_resync, but for recovery,
2967 * we need to convert that to several
2968 * virtual addresses.
2970 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
2976 if (mddev
->curr_resync
< max_sector
) { /* aborted */
2977 if (test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
))
2978 md_bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
2980 else for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2982 raid10_find_virt(conf
, mddev
->curr_resync
, i
);
2983 md_bitmap_end_sync(mddev
->bitmap
, sect
,
2987 /* completed sync */
2988 if ((!mddev
->bitmap
|| conf
->fullsync
)
2989 && conf
->have_replacement
2990 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2991 /* Completed a full sync so the replacements
2992 * are now fully recovered.
2995 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
2996 struct md_rdev
*rdev
=
2997 rcu_dereference(conf
->mirrors
[i
].replacement
);
2999 rdev
->recovery_offset
= MaxSector
;
3005 md_bitmap_close_sync(mddev
->bitmap
);
3008 return sectors_skipped
;
3011 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3012 return reshape_request(mddev
, sector_nr
, skipped
);
3014 if (chunks_skipped
>= conf
->geo
.raid_disks
) {
3015 /* if there has been nothing to do on any drive,
3016 * then there is nothing to do at all..
3019 return (max_sector
- sector_nr
) + sectors_skipped
;
3022 if (max_sector
> mddev
->resync_max
)
3023 max_sector
= mddev
->resync_max
; /* Don't do IO beyond here */
3025 /* make sure whole request will fit in a chunk - if chunks
3028 if (conf
->geo
.near_copies
< conf
->geo
.raid_disks
&&
3029 max_sector
> (sector_nr
| chunk_mask
))
3030 max_sector
= (sector_nr
| chunk_mask
) + 1;
3033 * If there is non-resync activity waiting for a turn, then let it
3034 * though before starting on this new sync request.
3036 if (conf
->nr_waiting
)
3037 schedule_timeout_uninterruptible(1);
3039 /* Again, very different code for resync and recovery.
3040 * Both must result in an r10bio with a list of bios that
3041 * have bi_end_io, bi_sector, bi_disk set,
3042 * and bi_private set to the r10bio.
3043 * For recovery, we may actually create several r10bios
3044 * with 2 bios in each, that correspond to the bios in the main one.
3045 * In this case, the subordinate r10bios link back through a
3046 * borrowed master_bio pointer, and the counter in the master
3047 * includes a ref from each subordinate.
3049 /* First, we decide what to do and set ->bi_end_io
3050 * To end_sync_read if we want to read, and
3051 * end_sync_write if we will want to write.
3054 max_sync
= RESYNC_PAGES
<< (PAGE_SHIFT
-9);
3055 if (!test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3056 /* recovery... the complicated one */
3060 for (i
= 0 ; i
< conf
->geo
.raid_disks
; i
++) {
3066 int need_recover
= 0;
3067 int need_replace
= 0;
3068 struct raid10_info
*mirror
= &conf
->mirrors
[i
];
3069 struct md_rdev
*mrdev
, *mreplace
;
3072 mrdev
= rcu_dereference(mirror
->rdev
);
3073 mreplace
= rcu_dereference(mirror
->replacement
);
3075 if (mrdev
!= NULL
&&
3076 !test_bit(Faulty
, &mrdev
->flags
) &&
3077 !test_bit(In_sync
, &mrdev
->flags
))
3079 if (mreplace
!= NULL
&&
3080 !test_bit(Faulty
, &mreplace
->flags
))
3083 if (!need_recover
&& !need_replace
) {
3089 /* want to reconstruct this device */
3091 sect
= raid10_find_virt(conf
, sector_nr
, i
);
3092 if (sect
>= mddev
->resync_max_sectors
) {
3093 /* last stripe is not complete - don't
3094 * try to recover this sector.
3099 if (mreplace
&& test_bit(Faulty
, &mreplace
->flags
))
3101 /* Unless we are doing a full sync, or a replacement
3102 * we only need to recover the block if it is set in
3105 must_sync
= md_bitmap_start_sync(mddev
->bitmap
, sect
,
3107 if (sync_blocks
< max_sync
)
3108 max_sync
= sync_blocks
;
3112 /* yep, skip the sync_blocks here, but don't assume
3113 * that there will never be anything to do here
3115 chunks_skipped
= -1;
3119 atomic_inc(&mrdev
->nr_pending
);
3121 atomic_inc(&mreplace
->nr_pending
);
3124 r10_bio
= raid10_alloc_init_r10buf(conf
);
3126 raise_barrier(conf
, rb2
!= NULL
);
3127 atomic_set(&r10_bio
->remaining
, 0);
3129 r10_bio
->master_bio
= (struct bio
*)rb2
;
3131 atomic_inc(&rb2
->remaining
);
3132 r10_bio
->mddev
= mddev
;
3133 set_bit(R10BIO_IsRecover
, &r10_bio
->state
);
3134 r10_bio
->sector
= sect
;
3136 raid10_find_phys(conf
, r10_bio
);
3138 /* Need to check if the array will still be
3142 for (j
= 0; j
< conf
->geo
.raid_disks
; j
++) {
3143 struct md_rdev
*rdev
= rcu_dereference(
3144 conf
->mirrors
[j
].rdev
);
3145 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3151 must_sync
= md_bitmap_start_sync(mddev
->bitmap
, sect
,
3152 &sync_blocks
, still_degraded
);
3155 for (j
=0; j
<conf
->copies
;j
++) {
3157 int d
= r10_bio
->devs
[j
].devnum
;
3158 sector_t from_addr
, to_addr
;
3159 struct md_rdev
*rdev
=
3160 rcu_dereference(conf
->mirrors
[d
].rdev
);
3161 sector_t sector
, first_bad
;
3164 !test_bit(In_sync
, &rdev
->flags
))
3166 /* This is where we read from */
3168 sector
= r10_bio
->devs
[j
].addr
;
3170 if (is_badblock(rdev
, sector
, max_sync
,
3171 &first_bad
, &bad_sectors
)) {
3172 if (first_bad
> sector
)
3173 max_sync
= first_bad
- sector
;
3175 bad_sectors
-= (sector
3177 if (max_sync
> bad_sectors
)
3178 max_sync
= bad_sectors
;
3182 bio
= r10_bio
->devs
[0].bio
;
3183 bio
->bi_next
= biolist
;
3185 bio
->bi_end_io
= end_sync_read
;
3186 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
3187 if (test_bit(FailFast
, &rdev
->flags
))
3188 bio
->bi_opf
|= MD_FAILFAST
;
3189 from_addr
= r10_bio
->devs
[j
].addr
;
3190 bio
->bi_iter
.bi_sector
= from_addr
+
3192 bio_set_dev(bio
, rdev
->bdev
);
3193 atomic_inc(&rdev
->nr_pending
);
3194 /* and we write to 'i' (if not in_sync) */
3196 for (k
=0; k
<conf
->copies
; k
++)
3197 if (r10_bio
->devs
[k
].devnum
== i
)
3199 BUG_ON(k
== conf
->copies
);
3200 to_addr
= r10_bio
->devs
[k
].addr
;
3201 r10_bio
->devs
[0].devnum
= d
;
3202 r10_bio
->devs
[0].addr
= from_addr
;
3203 r10_bio
->devs
[1].devnum
= i
;
3204 r10_bio
->devs
[1].addr
= to_addr
;
3207 bio
= r10_bio
->devs
[1].bio
;
3208 bio
->bi_next
= biolist
;
3210 bio
->bi_end_io
= end_sync_write
;
3211 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3212 bio
->bi_iter
.bi_sector
= to_addr
3213 + mrdev
->data_offset
;
3214 bio_set_dev(bio
, mrdev
->bdev
);
3215 atomic_inc(&r10_bio
->remaining
);
3217 r10_bio
->devs
[1].bio
->bi_end_io
= NULL
;
3219 /* and maybe write to replacement */
3220 bio
= r10_bio
->devs
[1].repl_bio
;
3222 bio
->bi_end_io
= NULL
;
3223 /* Note: if need_replace, then bio
3224 * cannot be NULL as r10buf_pool_alloc will
3225 * have allocated it.
3229 bio
->bi_next
= biolist
;
3231 bio
->bi_end_io
= end_sync_write
;
3232 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3233 bio
->bi_iter
.bi_sector
= to_addr
+
3234 mreplace
->data_offset
;
3235 bio_set_dev(bio
, mreplace
->bdev
);
3236 atomic_inc(&r10_bio
->remaining
);
3240 if (j
== conf
->copies
) {
3241 /* Cannot recover, so abort the recovery or
3242 * record a bad block */
3244 /* problem is that there are bad blocks
3245 * on other device(s)
3248 for (k
= 0; k
< conf
->copies
; k
++)
3249 if (r10_bio
->devs
[k
].devnum
== i
)
3251 if (!test_bit(In_sync
,
3253 && !rdev_set_badblocks(
3255 r10_bio
->devs
[k
].addr
,
3259 !rdev_set_badblocks(
3261 r10_bio
->devs
[k
].addr
,
3266 if (!test_and_set_bit(MD_RECOVERY_INTR
,
3268 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3270 mirror
->recovery_disabled
3271 = mddev
->recovery_disabled
;
3275 atomic_dec(&rb2
->remaining
);
3277 rdev_dec_pending(mrdev
, mddev
);
3279 rdev_dec_pending(mreplace
, mddev
);
3282 rdev_dec_pending(mrdev
, mddev
);
3284 rdev_dec_pending(mreplace
, mddev
);
3285 if (r10_bio
->devs
[0].bio
->bi_opf
& MD_FAILFAST
) {
3286 /* Only want this if there is elsewhere to
3287 * read from. 'j' is currently the first
3291 for (; j
< conf
->copies
; j
++) {
3292 int d
= r10_bio
->devs
[j
].devnum
;
3293 if (conf
->mirrors
[d
].rdev
&&
3295 &conf
->mirrors
[d
].rdev
->flags
))
3299 r10_bio
->devs
[0].bio
->bi_opf
3303 if (biolist
== NULL
) {
3305 struct r10bio
*rb2
= r10_bio
;
3306 r10_bio
= (struct r10bio
*) rb2
->master_bio
;
3307 rb2
->master_bio
= NULL
;
3313 /* resync. Schedule a read for every block at this virt offset */
3317 * Since curr_resync_completed could probably not update in
3318 * time, and we will set cluster_sync_low based on it.
3319 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3320 * safety reason, which ensures curr_resync_completed is
3321 * updated in bitmap_cond_end_sync.
3323 md_bitmap_cond_end_sync(mddev
->bitmap
, sector_nr
,
3324 mddev_is_clustered(mddev
) &&
3325 (sector_nr
+ 2 * RESYNC_SECTORS
> conf
->cluster_sync_high
));
3327 if (!md_bitmap_start_sync(mddev
->bitmap
, sector_nr
,
3328 &sync_blocks
, mddev
->degraded
) &&
3329 !conf
->fullsync
&& !test_bit(MD_RECOVERY_REQUESTED
,
3330 &mddev
->recovery
)) {
3331 /* We can skip this block */
3333 return sync_blocks
+ sectors_skipped
;
3335 if (sync_blocks
< max_sync
)
3336 max_sync
= sync_blocks
;
3337 r10_bio
= raid10_alloc_init_r10buf(conf
);
3340 r10_bio
->mddev
= mddev
;
3341 atomic_set(&r10_bio
->remaining
, 0);
3342 raise_barrier(conf
, 0);
3343 conf
->next_resync
= sector_nr
;
3345 r10_bio
->master_bio
= NULL
;
3346 r10_bio
->sector
= sector_nr
;
3347 set_bit(R10BIO_IsSync
, &r10_bio
->state
);
3348 raid10_find_phys(conf
, r10_bio
);
3349 r10_bio
->sectors
= (sector_nr
| chunk_mask
) - sector_nr
+ 1;
3351 for (i
= 0; i
< conf
->copies
; i
++) {
3352 int d
= r10_bio
->devs
[i
].devnum
;
3353 sector_t first_bad
, sector
;
3355 struct md_rdev
*rdev
;
3357 if (r10_bio
->devs
[i
].repl_bio
)
3358 r10_bio
->devs
[i
].repl_bio
->bi_end_io
= NULL
;
3360 bio
= r10_bio
->devs
[i
].bio
;
3361 bio
->bi_status
= BLK_STS_IOERR
;
3363 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
3364 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3368 sector
= r10_bio
->devs
[i
].addr
;
3369 if (is_badblock(rdev
, sector
, max_sync
,
3370 &first_bad
, &bad_sectors
)) {
3371 if (first_bad
> sector
)
3372 max_sync
= first_bad
- sector
;
3374 bad_sectors
-= (sector
- first_bad
);
3375 if (max_sync
> bad_sectors
)
3376 max_sync
= bad_sectors
;
3381 atomic_inc(&rdev
->nr_pending
);
3382 atomic_inc(&r10_bio
->remaining
);
3383 bio
->bi_next
= biolist
;
3385 bio
->bi_end_io
= end_sync_read
;
3386 bio_set_op_attrs(bio
, REQ_OP_READ
, 0);
3387 if (test_bit(FailFast
, &rdev
->flags
))
3388 bio
->bi_opf
|= MD_FAILFAST
;
3389 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
3390 bio_set_dev(bio
, rdev
->bdev
);
3393 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
3394 if (rdev
== NULL
|| test_bit(Faulty
, &rdev
->flags
)) {
3398 atomic_inc(&rdev
->nr_pending
);
3400 /* Need to set up for writing to the replacement */
3401 bio
= r10_bio
->devs
[i
].repl_bio
;
3402 bio
->bi_status
= BLK_STS_IOERR
;
3404 sector
= r10_bio
->devs
[i
].addr
;
3405 bio
->bi_next
= biolist
;
3407 bio
->bi_end_io
= end_sync_write
;
3408 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
3409 if (test_bit(FailFast
, &rdev
->flags
))
3410 bio
->bi_opf
|= MD_FAILFAST
;
3411 bio
->bi_iter
.bi_sector
= sector
+ rdev
->data_offset
;
3412 bio_set_dev(bio
, rdev
->bdev
);
3418 for (i
=0; i
<conf
->copies
; i
++) {
3419 int d
= r10_bio
->devs
[i
].devnum
;
3420 if (r10_bio
->devs
[i
].bio
->bi_end_io
)
3421 rdev_dec_pending(conf
->mirrors
[d
].rdev
,
3423 if (r10_bio
->devs
[i
].repl_bio
&&
3424 r10_bio
->devs
[i
].repl_bio
->bi_end_io
)
3426 conf
->mirrors
[d
].replacement
,
3436 if (sector_nr
+ max_sync
< max_sector
)
3437 max_sector
= sector_nr
+ max_sync
;
3440 int len
= PAGE_SIZE
;
3441 if (sector_nr
+ (len
>>9) > max_sector
)
3442 len
= (max_sector
- sector_nr
) << 9;
3445 for (bio
= biolist
; bio
; bio
=bio
->bi_next
) {
3446 struct resync_pages
*rp
= get_resync_pages(bio
);
3447 page
= resync_fetch_page(rp
, page_idx
);
3449 * won't fail because the vec table is big enough
3450 * to hold all these pages
3452 bio_add_page(bio
, page
, len
, 0);
3454 nr_sectors
+= len
>>9;
3455 sector_nr
+= len
>>9;
3456 } while (++page_idx
< RESYNC_PAGES
);
3457 r10_bio
->sectors
= nr_sectors
;
3459 if (mddev_is_clustered(mddev
) &&
3460 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3461 /* It is resync not recovery */
3462 if (conf
->cluster_sync_high
< sector_nr
+ nr_sectors
) {
3463 conf
->cluster_sync_low
= mddev
->curr_resync_completed
;
3464 raid10_set_cluster_sync_high(conf
);
3465 /* Send resync message */
3466 md_cluster_ops
->resync_info_update(mddev
,
3467 conf
->cluster_sync_low
,
3468 conf
->cluster_sync_high
);
3470 } else if (mddev_is_clustered(mddev
)) {
3471 /* This is recovery not resync */
3472 sector_t sect_va1
, sect_va2
;
3473 bool broadcast_msg
= false;
3475 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
3477 * sector_nr is a device address for recovery, so we
3478 * need translate it to array address before compare
3479 * with cluster_sync_high.
3481 sect_va1
= raid10_find_virt(conf
, sector_nr
, i
);
3483 if (conf
->cluster_sync_high
< sect_va1
+ nr_sectors
) {
3484 broadcast_msg
= true;
3486 * curr_resync_completed is similar as
3487 * sector_nr, so make the translation too.
3489 sect_va2
= raid10_find_virt(conf
,
3490 mddev
->curr_resync_completed
, i
);
3492 if (conf
->cluster_sync_low
== 0 ||
3493 conf
->cluster_sync_low
> sect_va2
)
3494 conf
->cluster_sync_low
= sect_va2
;
3497 if (broadcast_msg
) {
3498 raid10_set_cluster_sync_high(conf
);
3499 md_cluster_ops
->resync_info_update(mddev
,
3500 conf
->cluster_sync_low
,
3501 conf
->cluster_sync_high
);
3507 biolist
= biolist
->bi_next
;
3509 bio
->bi_next
= NULL
;
3510 r10_bio
= get_resync_r10bio(bio
);
3511 r10_bio
->sectors
= nr_sectors
;
3513 if (bio
->bi_end_io
== end_sync_read
) {
3514 md_sync_acct_bio(bio
, nr_sectors
);
3516 generic_make_request(bio
);
3520 if (sectors_skipped
)
3521 /* pretend they weren't skipped, it makes
3522 * no important difference in this case
3524 md_done_sync(mddev
, sectors_skipped
, 1);
3526 return sectors_skipped
+ nr_sectors
;
3528 /* There is nowhere to write, so all non-sync
3529 * drives must be failed or in resync, all drives
3530 * have a bad block, so try the next chunk...
3532 if (sector_nr
+ max_sync
< max_sector
)
3533 max_sector
= sector_nr
+ max_sync
;
3535 sectors_skipped
+= (max_sector
- sector_nr
);
3537 sector_nr
= max_sector
;
3542 raid10_size(struct mddev
*mddev
, sector_t sectors
, int raid_disks
)
3545 struct r10conf
*conf
= mddev
->private;
3548 raid_disks
= min(conf
->geo
.raid_disks
,
3549 conf
->prev
.raid_disks
);
3551 sectors
= conf
->dev_sectors
;
3553 size
= sectors
>> conf
->geo
.chunk_shift
;
3554 sector_div(size
, conf
->geo
.far_copies
);
3555 size
= size
* raid_disks
;
3556 sector_div(size
, conf
->geo
.near_copies
);
3558 return size
<< conf
->geo
.chunk_shift
;
3561 static void calc_sectors(struct r10conf
*conf
, sector_t size
)
3563 /* Calculate the number of sectors-per-device that will
3564 * actually be used, and set conf->dev_sectors and
3568 size
= size
>> conf
->geo
.chunk_shift
;
3569 sector_div(size
, conf
->geo
.far_copies
);
3570 size
= size
* conf
->geo
.raid_disks
;
3571 sector_div(size
, conf
->geo
.near_copies
);
3572 /* 'size' is now the number of chunks in the array */
3573 /* calculate "used chunks per device" */
3574 size
= size
* conf
->copies
;
3576 /* We need to round up when dividing by raid_disks to
3577 * get the stride size.
3579 size
= DIV_ROUND_UP_SECTOR_T(size
, conf
->geo
.raid_disks
);
3581 conf
->dev_sectors
= size
<< conf
->geo
.chunk_shift
;
3583 if (conf
->geo
.far_offset
)
3584 conf
->geo
.stride
= 1 << conf
->geo
.chunk_shift
;
3586 sector_div(size
, conf
->geo
.far_copies
);
3587 conf
->geo
.stride
= size
<< conf
->geo
.chunk_shift
;
3591 enum geo_type
{geo_new
, geo_old
, geo_start
};
3592 static int setup_geo(struct geom
*geo
, struct mddev
*mddev
, enum geo_type
new)
3595 int layout
, chunk
, disks
;
3598 layout
= mddev
->layout
;
3599 chunk
= mddev
->chunk_sectors
;
3600 disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3603 layout
= mddev
->new_layout
;
3604 chunk
= mddev
->new_chunk_sectors
;
3605 disks
= mddev
->raid_disks
;
3607 default: /* avoid 'may be unused' warnings */
3608 case geo_start
: /* new when starting reshape - raid_disks not
3610 layout
= mddev
->new_layout
;
3611 chunk
= mddev
->new_chunk_sectors
;
3612 disks
= mddev
->raid_disks
+ mddev
->delta_disks
;
3617 if (chunk
< (PAGE_SIZE
>> 9) ||
3618 !is_power_of_2(chunk
))
3621 fc
= (layout
>> 8) & 255;
3622 fo
= layout
& (1<<16);
3623 geo
->raid_disks
= disks
;
3624 geo
->near_copies
= nc
;
3625 geo
->far_copies
= fc
;
3626 geo
->far_offset
= fo
;
3627 switch (layout
>> 17) {
3628 case 0: /* original layout. simple but not always optimal */
3629 geo
->far_set_size
= disks
;
3631 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3632 * actually using this, but leave code here just in case.*/
3633 geo
->far_set_size
= disks
/fc
;
3634 WARN(geo
->far_set_size
< fc
,
3635 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3637 case 2: /* "improved" layout fixed to match documentation */
3638 geo
->far_set_size
= fc
* nc
;
3640 default: /* Not a valid layout */
3643 geo
->chunk_mask
= chunk
- 1;
3644 geo
->chunk_shift
= ffz(~chunk
);
3648 static struct r10conf
*setup_conf(struct mddev
*mddev
)
3650 struct r10conf
*conf
= NULL
;
3655 copies
= setup_geo(&geo
, mddev
, geo_new
);
3658 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3659 mdname(mddev
), PAGE_SIZE
);
3663 if (copies
< 2 || copies
> mddev
->raid_disks
) {
3664 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3665 mdname(mddev
), mddev
->new_layout
);
3670 conf
= kzalloc(sizeof(struct r10conf
), GFP_KERNEL
);
3674 /* FIXME calc properly */
3675 conf
->mirrors
= kcalloc(mddev
->raid_disks
+ max(0, -mddev
->delta_disks
),
3676 sizeof(struct raid10_info
),
3681 conf
->tmppage
= alloc_page(GFP_KERNEL
);
3686 conf
->copies
= copies
;
3687 err
= mempool_init(&conf
->r10bio_pool
, NR_RAID10_BIOS
, r10bio_pool_alloc
,
3688 r10bio_pool_free
, conf
);
3692 err
= bioset_init(&conf
->bio_split
, BIO_POOL_SIZE
, 0, 0);
3696 calc_sectors(conf
, mddev
->dev_sectors
);
3697 if (mddev
->reshape_position
== MaxSector
) {
3698 conf
->prev
= conf
->geo
;
3699 conf
->reshape_progress
= MaxSector
;
3701 if (setup_geo(&conf
->prev
, mddev
, geo_old
) != conf
->copies
) {
3705 conf
->reshape_progress
= mddev
->reshape_position
;
3706 if (conf
->prev
.far_offset
)
3707 conf
->prev
.stride
= 1 << conf
->prev
.chunk_shift
;
3709 /* far_copies must be 1 */
3710 conf
->prev
.stride
= conf
->dev_sectors
;
3712 conf
->reshape_safe
= conf
->reshape_progress
;
3713 spin_lock_init(&conf
->device_lock
);
3714 INIT_LIST_HEAD(&conf
->retry_list
);
3715 INIT_LIST_HEAD(&conf
->bio_end_io_list
);
3717 spin_lock_init(&conf
->resync_lock
);
3718 init_waitqueue_head(&conf
->wait_barrier
);
3719 atomic_set(&conf
->nr_pending
, 0);
3722 conf
->thread
= md_register_thread(raid10d
, mddev
, "raid10");
3726 conf
->mddev
= mddev
;
3731 mempool_exit(&conf
->r10bio_pool
);
3732 kfree(conf
->mirrors
);
3733 safe_put_page(conf
->tmppage
);
3734 bioset_exit(&conf
->bio_split
);
3737 return ERR_PTR(err
);
3740 static int raid10_run(struct mddev
*mddev
)
3742 struct r10conf
*conf
;
3743 int i
, disk_idx
, chunk_size
;
3744 struct raid10_info
*disk
;
3745 struct md_rdev
*rdev
;
3747 sector_t min_offset_diff
= 0;
3749 bool discard_supported
= false;
3751 if (mddev_init_writes_pending(mddev
) < 0)
3754 if (mddev
->private == NULL
) {
3755 conf
= setup_conf(mddev
);
3757 return PTR_ERR(conf
);
3758 mddev
->private = conf
;
3760 conf
= mddev
->private;
3764 if (mddev_is_clustered(conf
->mddev
)) {
3767 fc
= (mddev
->layout
>> 8) & 255;
3768 fo
= mddev
->layout
& (1<<16);
3769 if (fc
> 1 || fo
> 0) {
3770 pr_err("only near layout is supported by clustered"
3776 mddev
->thread
= conf
->thread
;
3777 conf
->thread
= NULL
;
3779 chunk_size
= mddev
->chunk_sectors
<< 9;
3781 blk_queue_max_discard_sectors(mddev
->queue
,
3782 mddev
->chunk_sectors
);
3783 blk_queue_max_write_same_sectors(mddev
->queue
, 0);
3784 blk_queue_max_write_zeroes_sectors(mddev
->queue
, 0);
3785 blk_queue_io_min(mddev
->queue
, chunk_size
);
3786 if (conf
->geo
.raid_disks
% conf
->geo
.near_copies
)
3787 blk_queue_io_opt(mddev
->queue
, chunk_size
* conf
->geo
.raid_disks
);
3789 blk_queue_io_opt(mddev
->queue
, chunk_size
*
3790 (conf
->geo
.raid_disks
/ conf
->geo
.near_copies
));
3793 rdev_for_each(rdev
, mddev
) {
3796 disk_idx
= rdev
->raid_disk
;
3799 if (disk_idx
>= conf
->geo
.raid_disks
&&
3800 disk_idx
>= conf
->prev
.raid_disks
)
3802 disk
= conf
->mirrors
+ disk_idx
;
3804 if (test_bit(Replacement
, &rdev
->flags
)) {
3805 if (disk
->replacement
)
3807 disk
->replacement
= rdev
;
3813 diff
= (rdev
->new_data_offset
- rdev
->data_offset
);
3814 if (!mddev
->reshape_backwards
)
3818 if (first
|| diff
< min_offset_diff
)
3819 min_offset_diff
= diff
;
3822 disk_stack_limits(mddev
->gendisk
, rdev
->bdev
,
3823 rdev
->data_offset
<< 9);
3825 disk
->head_position
= 0;
3827 if (blk_queue_discard(bdev_get_queue(rdev
->bdev
)))
3828 discard_supported
= true;
3833 if (discard_supported
)
3834 blk_queue_flag_set(QUEUE_FLAG_DISCARD
,
3837 blk_queue_flag_clear(QUEUE_FLAG_DISCARD
,
3840 /* need to check that every block has at least one working mirror */
3841 if (!enough(conf
, -1)) {
3842 pr_err("md/raid10:%s: not enough operational mirrors.\n",
3847 if (conf
->reshape_progress
!= MaxSector
) {
3848 /* must ensure that shape change is supported */
3849 if (conf
->geo
.far_copies
!= 1 &&
3850 conf
->geo
.far_offset
== 0)
3852 if (conf
->prev
.far_copies
!= 1 &&
3853 conf
->prev
.far_offset
== 0)
3857 mddev
->degraded
= 0;
3859 i
< conf
->geo
.raid_disks
3860 || i
< conf
->prev
.raid_disks
;
3863 disk
= conf
->mirrors
+ i
;
3865 if (!disk
->rdev
&& disk
->replacement
) {
3866 /* The replacement is all we have - use it */
3867 disk
->rdev
= disk
->replacement
;
3868 disk
->replacement
= NULL
;
3869 clear_bit(Replacement
, &disk
->rdev
->flags
);
3873 !test_bit(In_sync
, &disk
->rdev
->flags
)) {
3874 disk
->head_position
= 0;
3877 disk
->rdev
->saved_raid_disk
< 0)
3881 if (disk
->replacement
&&
3882 !test_bit(In_sync
, &disk
->replacement
->flags
) &&
3883 disk
->replacement
->saved_raid_disk
< 0) {
3887 disk
->recovery_disabled
= mddev
->recovery_disabled
- 1;
3890 if (mddev
->recovery_cp
!= MaxSector
)
3891 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3893 pr_info("md/raid10:%s: active with %d out of %d devices\n",
3894 mdname(mddev
), conf
->geo
.raid_disks
- mddev
->degraded
,
3895 conf
->geo
.raid_disks
);
3897 * Ok, everything is just fine now
3899 mddev
->dev_sectors
= conf
->dev_sectors
;
3900 size
= raid10_size(mddev
, 0, 0);
3901 md_set_array_sectors(mddev
, size
);
3902 mddev
->resync_max_sectors
= size
;
3903 set_bit(MD_FAILFAST_SUPPORTED
, &mddev
->flags
);
3906 int stripe
= conf
->geo
.raid_disks
*
3907 ((mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
3909 /* Calculate max read-ahead size.
3910 * We need to readahead at least twice a whole stripe....
3913 stripe
/= conf
->geo
.near_copies
;
3914 if (mddev
->queue
->backing_dev_info
->ra_pages
< 2 * stripe
)
3915 mddev
->queue
->backing_dev_info
->ra_pages
= 2 * stripe
;
3918 if (md_integrity_register(mddev
))
3921 if (conf
->reshape_progress
!= MaxSector
) {
3922 unsigned long before_length
, after_length
;
3924 before_length
= ((1 << conf
->prev
.chunk_shift
) *
3925 conf
->prev
.far_copies
);
3926 after_length
= ((1 << conf
->geo
.chunk_shift
) *
3927 conf
->geo
.far_copies
);
3929 if (max(before_length
, after_length
) > min_offset_diff
) {
3930 /* This cannot work */
3931 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3934 conf
->offset_diff
= min_offset_diff
;
3936 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3937 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3938 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3939 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3940 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
3942 if (!mddev
->sync_thread
)
3949 md_unregister_thread(&mddev
->thread
);
3950 mempool_exit(&conf
->r10bio_pool
);
3951 safe_put_page(conf
->tmppage
);
3952 kfree(conf
->mirrors
);
3954 mddev
->private = NULL
;
3959 static void raid10_free(struct mddev
*mddev
, void *priv
)
3961 struct r10conf
*conf
= priv
;
3963 mempool_exit(&conf
->r10bio_pool
);
3964 safe_put_page(conf
->tmppage
);
3965 kfree(conf
->mirrors
);
3966 kfree(conf
->mirrors_old
);
3967 kfree(conf
->mirrors_new
);
3968 bioset_exit(&conf
->bio_split
);
3972 static void raid10_quiesce(struct mddev
*mddev
, int quiesce
)
3974 struct r10conf
*conf
= mddev
->private;
3977 raise_barrier(conf
, 0);
3979 lower_barrier(conf
);
3982 static int raid10_resize(struct mddev
*mddev
, sector_t sectors
)
3984 /* Resize of 'far' arrays is not supported.
3985 * For 'near' and 'offset' arrays we can set the
3986 * number of sectors used to be an appropriate multiple
3987 * of the chunk size.
3988 * For 'offset', this is far_copies*chunksize.
3989 * For 'near' the multiplier is the LCM of
3990 * near_copies and raid_disks.
3991 * So if far_copies > 1 && !far_offset, fail.
3992 * Else find LCM(raid_disks, near_copy)*far_copies and
3993 * multiply by chunk_size. Then round to this number.
3994 * This is mostly done by raid10_size()
3996 struct r10conf
*conf
= mddev
->private;
3997 sector_t oldsize
, size
;
3999 if (mddev
->reshape_position
!= MaxSector
)
4002 if (conf
->geo
.far_copies
> 1 && !conf
->geo
.far_offset
)
4005 oldsize
= raid10_size(mddev
, 0, 0);
4006 size
= raid10_size(mddev
, sectors
, 0);
4007 if (mddev
->external_size
&&
4008 mddev
->array_sectors
> size
)
4010 if (mddev
->bitmap
) {
4011 int ret
= md_bitmap_resize(mddev
->bitmap
, size
, 0, 0);
4015 md_set_array_sectors(mddev
, size
);
4016 if (sectors
> mddev
->dev_sectors
&&
4017 mddev
->recovery_cp
> oldsize
) {
4018 mddev
->recovery_cp
= oldsize
;
4019 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4021 calc_sectors(conf
, sectors
);
4022 mddev
->dev_sectors
= conf
->dev_sectors
;
4023 mddev
->resync_max_sectors
= size
;
4027 static void *raid10_takeover_raid0(struct mddev
*mddev
, sector_t size
, int devs
)
4029 struct md_rdev
*rdev
;
4030 struct r10conf
*conf
;
4032 if (mddev
->degraded
> 0) {
4033 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4035 return ERR_PTR(-EINVAL
);
4037 sector_div(size
, devs
);
4039 /* Set new parameters */
4040 mddev
->new_level
= 10;
4041 /* new layout: far_copies = 1, near_copies = 2 */
4042 mddev
->new_layout
= (1<<8) + 2;
4043 mddev
->new_chunk_sectors
= mddev
->chunk_sectors
;
4044 mddev
->delta_disks
= mddev
->raid_disks
;
4045 mddev
->raid_disks
*= 2;
4046 /* make sure it will be not marked as dirty */
4047 mddev
->recovery_cp
= MaxSector
;
4048 mddev
->dev_sectors
= size
;
4050 conf
= setup_conf(mddev
);
4051 if (!IS_ERR(conf
)) {
4052 rdev_for_each(rdev
, mddev
)
4053 if (rdev
->raid_disk
>= 0) {
4054 rdev
->new_raid_disk
= rdev
->raid_disk
* 2;
4055 rdev
->sectors
= size
;
4063 static void *raid10_takeover(struct mddev
*mddev
)
4065 struct r0conf
*raid0_conf
;
4067 /* raid10 can take over:
4068 * raid0 - providing it has only two drives
4070 if (mddev
->level
== 0) {
4071 /* for raid0 takeover only one zone is supported */
4072 raid0_conf
= mddev
->private;
4073 if (raid0_conf
->nr_strip_zones
> 1) {
4074 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4076 return ERR_PTR(-EINVAL
);
4078 return raid10_takeover_raid0(mddev
,
4079 raid0_conf
->strip_zone
->zone_end
,
4080 raid0_conf
->strip_zone
->nb_dev
);
4082 return ERR_PTR(-EINVAL
);
4085 static int raid10_check_reshape(struct mddev
*mddev
)
4087 /* Called when there is a request to change
4088 * - layout (to ->new_layout)
4089 * - chunk size (to ->new_chunk_sectors)
4090 * - raid_disks (by delta_disks)
4091 * or when trying to restart a reshape that was ongoing.
4093 * We need to validate the request and possibly allocate
4094 * space if that might be an issue later.
4096 * Currently we reject any reshape of a 'far' mode array,
4097 * allow chunk size to change if new is generally acceptable,
4098 * allow raid_disks to increase, and allow
4099 * a switch between 'near' mode and 'offset' mode.
4101 struct r10conf
*conf
= mddev
->private;
4104 if (conf
->geo
.far_copies
!= 1 && !conf
->geo
.far_offset
)
4107 if (setup_geo(&geo
, mddev
, geo_start
) != conf
->copies
)
4108 /* mustn't change number of copies */
4110 if (geo
.far_copies
> 1 && !geo
.far_offset
)
4111 /* Cannot switch to 'far' mode */
4114 if (mddev
->array_sectors
& geo
.chunk_mask
)
4115 /* not factor of array size */
4118 if (!enough(conf
, -1))
4121 kfree(conf
->mirrors_new
);
4122 conf
->mirrors_new
= NULL
;
4123 if (mddev
->delta_disks
> 0) {
4124 /* allocate new 'mirrors' list */
4126 kcalloc(mddev
->raid_disks
+ mddev
->delta_disks
,
4127 sizeof(struct raid10_info
),
4129 if (!conf
->mirrors_new
)
4136 * Need to check if array has failed when deciding whether to:
4138 * - remove non-faulty devices
4141 * This determination is simple when no reshape is happening.
4142 * However if there is a reshape, we need to carefully check
4143 * both the before and after sections.
4144 * This is because some failed devices may only affect one
4145 * of the two sections, and some non-in_sync devices may
4146 * be insync in the section most affected by failed devices.
4148 static int calc_degraded(struct r10conf
*conf
)
4150 int degraded
, degraded2
;
4155 /* 'prev' section first */
4156 for (i
= 0; i
< conf
->prev
.raid_disks
; i
++) {
4157 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
4158 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4160 else if (!test_bit(In_sync
, &rdev
->flags
))
4161 /* When we can reduce the number of devices in
4162 * an array, this might not contribute to
4163 * 'degraded'. It does now.
4168 if (conf
->geo
.raid_disks
== conf
->prev
.raid_disks
)
4172 for (i
= 0; i
< conf
->geo
.raid_disks
; i
++) {
4173 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[i
].rdev
);
4174 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
))
4176 else if (!test_bit(In_sync
, &rdev
->flags
)) {
4177 /* If reshape is increasing the number of devices,
4178 * this section has already been recovered, so
4179 * it doesn't contribute to degraded.
4182 if (conf
->geo
.raid_disks
<= conf
->prev
.raid_disks
)
4187 if (degraded2
> degraded
)
4192 static int raid10_start_reshape(struct mddev
*mddev
)
4194 /* A 'reshape' has been requested. This commits
4195 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4196 * This also checks if there are enough spares and adds them
4198 * We currently require enough spares to make the final
4199 * array non-degraded. We also require that the difference
4200 * between old and new data_offset - on each device - is
4201 * enough that we never risk over-writing.
4204 unsigned long before_length
, after_length
;
4205 sector_t min_offset_diff
= 0;
4208 struct r10conf
*conf
= mddev
->private;
4209 struct md_rdev
*rdev
;
4213 if (test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
4216 if (setup_geo(&new, mddev
, geo_start
) != conf
->copies
)
4219 before_length
= ((1 << conf
->prev
.chunk_shift
) *
4220 conf
->prev
.far_copies
);
4221 after_length
= ((1 << conf
->geo
.chunk_shift
) *
4222 conf
->geo
.far_copies
);
4224 rdev_for_each(rdev
, mddev
) {
4225 if (!test_bit(In_sync
, &rdev
->flags
)
4226 && !test_bit(Faulty
, &rdev
->flags
))
4228 if (rdev
->raid_disk
>= 0) {
4229 long long diff
= (rdev
->new_data_offset
4230 - rdev
->data_offset
);
4231 if (!mddev
->reshape_backwards
)
4235 if (first
|| diff
< min_offset_diff
)
4236 min_offset_diff
= diff
;
4241 if (max(before_length
, after_length
) > min_offset_diff
)
4244 if (spares
< mddev
->delta_disks
)
4247 conf
->offset_diff
= min_offset_diff
;
4248 spin_lock_irq(&conf
->device_lock
);
4249 if (conf
->mirrors_new
) {
4250 memcpy(conf
->mirrors_new
, conf
->mirrors
,
4251 sizeof(struct raid10_info
)*conf
->prev
.raid_disks
);
4253 kfree(conf
->mirrors_old
);
4254 conf
->mirrors_old
= conf
->mirrors
;
4255 conf
->mirrors
= conf
->mirrors_new
;
4256 conf
->mirrors_new
= NULL
;
4258 setup_geo(&conf
->geo
, mddev
, geo_start
);
4260 if (mddev
->reshape_backwards
) {
4261 sector_t size
= raid10_size(mddev
, 0, 0);
4262 if (size
< mddev
->array_sectors
) {
4263 spin_unlock_irq(&conf
->device_lock
);
4264 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4268 mddev
->resync_max_sectors
= size
;
4269 conf
->reshape_progress
= size
;
4271 conf
->reshape_progress
= 0;
4272 conf
->reshape_safe
= conf
->reshape_progress
;
4273 spin_unlock_irq(&conf
->device_lock
);
4275 if (mddev
->delta_disks
&& mddev
->bitmap
) {
4276 struct mdp_superblock_1
*sb
= NULL
;
4277 sector_t oldsize
, newsize
;
4279 oldsize
= raid10_size(mddev
, 0, 0);
4280 newsize
= raid10_size(mddev
, 0, conf
->geo
.raid_disks
);
4282 if (!mddev_is_clustered(mddev
)) {
4283 ret
= md_bitmap_resize(mddev
->bitmap
, newsize
, 0, 0);
4290 rdev_for_each(rdev
, mddev
) {
4291 if (rdev
->raid_disk
> -1 &&
4292 !test_bit(Faulty
, &rdev
->flags
))
4293 sb
= page_address(rdev
->sb_page
);
4297 * some node is already performing reshape, and no need to
4298 * call md_bitmap_resize again since it should be called when
4299 * receiving BITMAP_RESIZE msg
4301 if ((sb
&& (le32_to_cpu(sb
->feature_map
) &
4302 MD_FEATURE_RESHAPE_ACTIVE
)) || (oldsize
== newsize
))
4305 ret
= md_bitmap_resize(mddev
->bitmap
, newsize
, 0, 0);
4309 ret
= md_cluster_ops
->resize_bitmaps(mddev
, newsize
, oldsize
);
4311 md_bitmap_resize(mddev
->bitmap
, oldsize
, 0, 0);
4316 if (mddev
->delta_disks
> 0) {
4317 rdev_for_each(rdev
, mddev
)
4318 if (rdev
->raid_disk
< 0 &&
4319 !test_bit(Faulty
, &rdev
->flags
)) {
4320 if (raid10_add_disk(mddev
, rdev
) == 0) {
4321 if (rdev
->raid_disk
>=
4322 conf
->prev
.raid_disks
)
4323 set_bit(In_sync
, &rdev
->flags
);
4325 rdev
->recovery_offset
= 0;
4327 if (sysfs_link_rdev(mddev
, rdev
))
4328 /* Failure here is OK */;
4330 } else if (rdev
->raid_disk
>= conf
->prev
.raid_disks
4331 && !test_bit(Faulty
, &rdev
->flags
)) {
4332 /* This is a spare that was manually added */
4333 set_bit(In_sync
, &rdev
->flags
);
4336 /* When a reshape changes the number of devices,
4337 * ->degraded is measured against the larger of the
4338 * pre and post numbers.
4340 spin_lock_irq(&conf
->device_lock
);
4341 mddev
->degraded
= calc_degraded(conf
);
4342 spin_unlock_irq(&conf
->device_lock
);
4343 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4344 mddev
->reshape_position
= conf
->reshape_progress
;
4345 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4347 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
4348 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
4349 clear_bit(MD_RECOVERY_DONE
, &mddev
->recovery
);
4350 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
4351 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
4353 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
4355 if (!mddev
->sync_thread
) {
4359 conf
->reshape_checkpoint
= jiffies
;
4360 md_wakeup_thread(mddev
->sync_thread
);
4361 md_new_event(mddev
);
4365 mddev
->recovery
= 0;
4366 spin_lock_irq(&conf
->device_lock
);
4367 conf
->geo
= conf
->prev
;
4368 mddev
->raid_disks
= conf
->geo
.raid_disks
;
4369 rdev_for_each(rdev
, mddev
)
4370 rdev
->new_data_offset
= rdev
->data_offset
;
4372 conf
->reshape_progress
= MaxSector
;
4373 conf
->reshape_safe
= MaxSector
;
4374 mddev
->reshape_position
= MaxSector
;
4375 spin_unlock_irq(&conf
->device_lock
);
4379 /* Calculate the last device-address that could contain
4380 * any block from the chunk that includes the array-address 's'
4381 * and report the next address.
4382 * i.e. the address returned will be chunk-aligned and after
4383 * any data that is in the chunk containing 's'.
4385 static sector_t
last_dev_address(sector_t s
, struct geom
*geo
)
4387 s
= (s
| geo
->chunk_mask
) + 1;
4388 s
>>= geo
->chunk_shift
;
4389 s
*= geo
->near_copies
;
4390 s
= DIV_ROUND_UP_SECTOR_T(s
, geo
->raid_disks
);
4391 s
*= geo
->far_copies
;
4392 s
<<= geo
->chunk_shift
;
4396 /* Calculate the first device-address that could contain
4397 * any block from the chunk that includes the array-address 's'.
4398 * This too will be the start of a chunk
4400 static sector_t
first_dev_address(sector_t s
, struct geom
*geo
)
4402 s
>>= geo
->chunk_shift
;
4403 s
*= geo
->near_copies
;
4404 sector_div(s
, geo
->raid_disks
);
4405 s
*= geo
->far_copies
;
4406 s
<<= geo
->chunk_shift
;
4410 static sector_t
reshape_request(struct mddev
*mddev
, sector_t sector_nr
,
4413 /* We simply copy at most one chunk (smallest of old and new)
4414 * at a time, possibly less if that exceeds RESYNC_PAGES,
4415 * or we hit a bad block or something.
4416 * This might mean we pause for normal IO in the middle of
4417 * a chunk, but that is not a problem as mddev->reshape_position
4418 * can record any location.
4420 * If we will want to write to a location that isn't
4421 * yet recorded as 'safe' (i.e. in metadata on disk) then
4422 * we need to flush all reshape requests and update the metadata.
4424 * When reshaping forwards (e.g. to more devices), we interpret
4425 * 'safe' as the earliest block which might not have been copied
4426 * down yet. We divide this by previous stripe size and multiply
4427 * by previous stripe length to get lowest device offset that we
4428 * cannot write to yet.
4429 * We interpret 'sector_nr' as an address that we want to write to.
4430 * From this we use last_device_address() to find where we might
4431 * write to, and first_device_address on the 'safe' position.
4432 * If this 'next' write position is after the 'safe' position,
4433 * we must update the metadata to increase the 'safe' position.
4435 * When reshaping backwards, we round in the opposite direction
4436 * and perform the reverse test: next write position must not be
4437 * less than current safe position.
4439 * In all this the minimum difference in data offsets
4440 * (conf->offset_diff - always positive) allows a bit of slack,
4441 * so next can be after 'safe', but not by more than offset_diff
4443 * We need to prepare all the bios here before we start any IO
4444 * to ensure the size we choose is acceptable to all devices.
4445 * The means one for each copy for write-out and an extra one for
4447 * We store the read-in bio in ->master_bio and the others in
4448 * ->devs[x].bio and ->devs[x].repl_bio.
4450 struct r10conf
*conf
= mddev
->private;
4451 struct r10bio
*r10_bio
;
4452 sector_t next
, safe
, last
;
4456 struct md_rdev
*rdev
;
4459 struct bio
*bio
, *read_bio
;
4460 int sectors_done
= 0;
4461 struct page
**pages
;
4463 if (sector_nr
== 0) {
4464 /* If restarting in the middle, skip the initial sectors */
4465 if (mddev
->reshape_backwards
&&
4466 conf
->reshape_progress
< raid10_size(mddev
, 0, 0)) {
4467 sector_nr
= (raid10_size(mddev
, 0, 0)
4468 - conf
->reshape_progress
);
4469 } else if (!mddev
->reshape_backwards
&&
4470 conf
->reshape_progress
> 0)
4471 sector_nr
= conf
->reshape_progress
;
4473 mddev
->curr_resync_completed
= sector_nr
;
4474 sysfs_notify(&mddev
->kobj
, NULL
, "sync_completed");
4480 /* We don't use sector_nr to track where we are up to
4481 * as that doesn't work well for ->reshape_backwards.
4482 * So just use ->reshape_progress.
4484 if (mddev
->reshape_backwards
) {
4485 /* 'next' is the earliest device address that we might
4486 * write to for this chunk in the new layout
4488 next
= first_dev_address(conf
->reshape_progress
- 1,
4491 /* 'safe' is the last device address that we might read from
4492 * in the old layout after a restart
4494 safe
= last_dev_address(conf
->reshape_safe
- 1,
4497 if (next
+ conf
->offset_diff
< safe
)
4500 last
= conf
->reshape_progress
- 1;
4501 sector_nr
= last
& ~(sector_t
)(conf
->geo
.chunk_mask
4502 & conf
->prev
.chunk_mask
);
4503 if (sector_nr
+ RESYNC_BLOCK_SIZE
/512 < last
)
4504 sector_nr
= last
+ 1 - RESYNC_BLOCK_SIZE
/512;
4506 /* 'next' is after the last device address that we
4507 * might write to for this chunk in the new layout
4509 next
= last_dev_address(conf
->reshape_progress
, &conf
->geo
);
4511 /* 'safe' is the earliest device address that we might
4512 * read from in the old layout after a restart
4514 safe
= first_dev_address(conf
->reshape_safe
, &conf
->prev
);
4516 /* Need to update metadata if 'next' might be beyond 'safe'
4517 * as that would possibly corrupt data
4519 if (next
> safe
+ conf
->offset_diff
)
4522 sector_nr
= conf
->reshape_progress
;
4523 last
= sector_nr
| (conf
->geo
.chunk_mask
4524 & conf
->prev
.chunk_mask
);
4526 if (sector_nr
+ RESYNC_BLOCK_SIZE
/512 <= last
)
4527 last
= sector_nr
+ RESYNC_BLOCK_SIZE
/512 - 1;
4531 time_after(jiffies
, conf
->reshape_checkpoint
+ 10*HZ
)) {
4532 /* Need to update reshape_position in metadata */
4534 mddev
->reshape_position
= conf
->reshape_progress
;
4535 if (mddev
->reshape_backwards
)
4536 mddev
->curr_resync_completed
= raid10_size(mddev
, 0, 0)
4537 - conf
->reshape_progress
;
4539 mddev
->curr_resync_completed
= conf
->reshape_progress
;
4540 conf
->reshape_checkpoint
= jiffies
;
4541 set_bit(MD_SB_CHANGE_DEVS
, &mddev
->sb_flags
);
4542 md_wakeup_thread(mddev
->thread
);
4543 wait_event(mddev
->sb_wait
, mddev
->sb_flags
== 0 ||
4544 test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
));
4545 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
)) {
4546 allow_barrier(conf
);
4547 return sectors_done
;
4549 conf
->reshape_safe
= mddev
->reshape_position
;
4550 allow_barrier(conf
);
4553 raise_barrier(conf
, 0);
4555 /* Now schedule reads for blocks from sector_nr to last */
4556 r10_bio
= raid10_alloc_init_r10buf(conf
);
4558 raise_barrier(conf
, 1);
4559 atomic_set(&r10_bio
->remaining
, 0);
4560 r10_bio
->mddev
= mddev
;
4561 r10_bio
->sector
= sector_nr
;
4562 set_bit(R10BIO_IsReshape
, &r10_bio
->state
);
4563 r10_bio
->sectors
= last
- sector_nr
+ 1;
4564 rdev
= read_balance(conf
, r10_bio
, &max_sectors
);
4565 BUG_ON(!test_bit(R10BIO_Previous
, &r10_bio
->state
));
4568 /* Cannot read from here, so need to record bad blocks
4569 * on all the target devices.
4572 mempool_free(r10_bio
, &conf
->r10buf_pool
);
4573 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4574 return sectors_done
;
4577 read_bio
= bio_alloc_mddev(GFP_KERNEL
, RESYNC_PAGES
, mddev
);
4579 bio_set_dev(read_bio
, rdev
->bdev
);
4580 read_bio
->bi_iter
.bi_sector
= (r10_bio
->devs
[r10_bio
->read_slot
].addr
4581 + rdev
->data_offset
);
4582 read_bio
->bi_private
= r10_bio
;
4583 read_bio
->bi_end_io
= end_reshape_read
;
4584 bio_set_op_attrs(read_bio
, REQ_OP_READ
, 0);
4585 read_bio
->bi_flags
&= (~0UL << BIO_RESET_BITS
);
4586 read_bio
->bi_status
= 0;
4587 read_bio
->bi_vcnt
= 0;
4588 read_bio
->bi_iter
.bi_size
= 0;
4589 r10_bio
->master_bio
= read_bio
;
4590 r10_bio
->read_slot
= r10_bio
->devs
[r10_bio
->read_slot
].devnum
;
4593 * Broadcast RESYNC message to other nodes, so all nodes would not
4594 * write to the region to avoid conflict.
4596 if (mddev_is_clustered(mddev
) && conf
->cluster_sync_high
<= sector_nr
) {
4597 struct mdp_superblock_1
*sb
= NULL
;
4598 int sb_reshape_pos
= 0;
4600 conf
->cluster_sync_low
= sector_nr
;
4601 conf
->cluster_sync_high
= sector_nr
+ CLUSTER_RESYNC_WINDOW_SECTORS
;
4602 sb
= page_address(rdev
->sb_page
);
4604 sb_reshape_pos
= le64_to_cpu(sb
->reshape_position
);
4606 * Set cluster_sync_low again if next address for array
4607 * reshape is less than cluster_sync_low. Since we can't
4608 * update cluster_sync_low until it has finished reshape.
4610 if (sb_reshape_pos
< conf
->cluster_sync_low
)
4611 conf
->cluster_sync_low
= sb_reshape_pos
;
4614 md_cluster_ops
->resync_info_update(mddev
, conf
->cluster_sync_low
,
4615 conf
->cluster_sync_high
);
4618 /* Now find the locations in the new layout */
4619 __raid10_find_phys(&conf
->geo
, r10_bio
);
4622 read_bio
->bi_next
= NULL
;
4625 for (s
= 0; s
< conf
->copies
*2; s
++) {
4627 int d
= r10_bio
->devs
[s
/2].devnum
;
4628 struct md_rdev
*rdev2
;
4630 rdev2
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4631 b
= r10_bio
->devs
[s
/2].repl_bio
;
4633 rdev2
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4634 b
= r10_bio
->devs
[s
/2].bio
;
4636 if (!rdev2
|| test_bit(Faulty
, &rdev2
->flags
))
4639 bio_set_dev(b
, rdev2
->bdev
);
4640 b
->bi_iter
.bi_sector
= r10_bio
->devs
[s
/2].addr
+
4641 rdev2
->new_data_offset
;
4642 b
->bi_end_io
= end_reshape_write
;
4643 bio_set_op_attrs(b
, REQ_OP_WRITE
, 0);
4648 /* Now add as many pages as possible to all of these bios. */
4651 pages
= get_resync_pages(r10_bio
->devs
[0].bio
)->pages
;
4652 for (s
= 0 ; s
< max_sectors
; s
+= PAGE_SIZE
>> 9) {
4653 struct page
*page
= pages
[s
/ (PAGE_SIZE
>> 9)];
4654 int len
= (max_sectors
- s
) << 9;
4655 if (len
> PAGE_SIZE
)
4657 for (bio
= blist
; bio
; bio
= bio
->bi_next
) {
4659 * won't fail because the vec table is big enough
4660 * to hold all these pages
4662 bio_add_page(bio
, page
, len
, 0);
4664 sector_nr
+= len
>> 9;
4665 nr_sectors
+= len
>> 9;
4668 r10_bio
->sectors
= nr_sectors
;
4670 /* Now submit the read */
4671 md_sync_acct_bio(read_bio
, r10_bio
->sectors
);
4672 atomic_inc(&r10_bio
->remaining
);
4673 read_bio
->bi_next
= NULL
;
4674 generic_make_request(read_bio
);
4675 sectors_done
+= nr_sectors
;
4676 if (sector_nr
<= last
)
4679 lower_barrier(conf
);
4681 /* Now that we have done the whole section we can
4682 * update reshape_progress
4684 if (mddev
->reshape_backwards
)
4685 conf
->reshape_progress
-= sectors_done
;
4687 conf
->reshape_progress
+= sectors_done
;
4689 return sectors_done
;
4692 static void end_reshape_request(struct r10bio
*r10_bio
);
4693 static int handle_reshape_read_error(struct mddev
*mddev
,
4694 struct r10bio
*r10_bio
);
4695 static void reshape_request_write(struct mddev
*mddev
, struct r10bio
*r10_bio
)
4697 /* Reshape read completed. Hopefully we have a block
4699 * If we got a read error then we do sync 1-page reads from
4700 * elsewhere until we find the data - or give up.
4702 struct r10conf
*conf
= mddev
->private;
4705 if (!test_bit(R10BIO_Uptodate
, &r10_bio
->state
))
4706 if (handle_reshape_read_error(mddev
, r10_bio
) < 0) {
4707 /* Reshape has been aborted */
4708 md_done_sync(mddev
, r10_bio
->sectors
, 0);
4712 /* We definitely have the data in the pages, schedule the
4715 atomic_set(&r10_bio
->remaining
, 1);
4716 for (s
= 0; s
< conf
->copies
*2; s
++) {
4718 int d
= r10_bio
->devs
[s
/2].devnum
;
4719 struct md_rdev
*rdev
;
4722 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4723 b
= r10_bio
->devs
[s
/2].repl_bio
;
4725 rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4726 b
= r10_bio
->devs
[s
/2].bio
;
4728 if (!rdev
|| test_bit(Faulty
, &rdev
->flags
)) {
4732 atomic_inc(&rdev
->nr_pending
);
4734 md_sync_acct_bio(b
, r10_bio
->sectors
);
4735 atomic_inc(&r10_bio
->remaining
);
4737 generic_make_request(b
);
4739 end_reshape_request(r10_bio
);
4742 static void end_reshape(struct r10conf
*conf
)
4744 if (test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
))
4747 spin_lock_irq(&conf
->device_lock
);
4748 conf
->prev
= conf
->geo
;
4749 md_finish_reshape(conf
->mddev
);
4751 conf
->reshape_progress
= MaxSector
;
4752 conf
->reshape_safe
= MaxSector
;
4753 spin_unlock_irq(&conf
->device_lock
);
4755 /* read-ahead size must cover two whole stripes, which is
4756 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4758 if (conf
->mddev
->queue
) {
4759 int stripe
= conf
->geo
.raid_disks
*
4760 ((conf
->mddev
->chunk_sectors
<< 9) / PAGE_SIZE
);
4761 stripe
/= conf
->geo
.near_copies
;
4762 if (conf
->mddev
->queue
->backing_dev_info
->ra_pages
< 2 * stripe
)
4763 conf
->mddev
->queue
->backing_dev_info
->ra_pages
= 2 * stripe
;
4768 static void raid10_update_reshape_pos(struct mddev
*mddev
)
4770 struct r10conf
*conf
= mddev
->private;
4773 md_cluster_ops
->resync_info_get(mddev
, &lo
, &hi
);
4774 if (((mddev
->reshape_position
<= hi
) && (mddev
->reshape_position
>= lo
))
4775 || mddev
->reshape_position
== MaxSector
)
4776 conf
->reshape_progress
= mddev
->reshape_position
;
4781 static int handle_reshape_read_error(struct mddev
*mddev
,
4782 struct r10bio
*r10_bio
)
4784 /* Use sync reads to get the blocks from somewhere else */
4785 int sectors
= r10_bio
->sectors
;
4786 struct r10conf
*conf
= mddev
->private;
4787 struct r10bio
*r10b
;
4790 struct page
**pages
;
4792 r10b
= kmalloc(sizeof(*r10b
) +
4793 sizeof(struct r10dev
) * conf
->copies
, GFP_NOIO
);
4795 set_bit(MD_RECOVERY_INTR
, &mddev
->recovery
);
4799 /* reshape IOs share pages from .devs[0].bio */
4800 pages
= get_resync_pages(r10_bio
->devs
[0].bio
)->pages
;
4802 r10b
->sector
= r10_bio
->sector
;
4803 __raid10_find_phys(&conf
->prev
, r10b
);
4808 int first_slot
= slot
;
4810 if (s
> (PAGE_SIZE
>> 9))
4815 int d
= r10b
->devs
[slot
].devnum
;
4816 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4819 test_bit(Faulty
, &rdev
->flags
) ||
4820 !test_bit(In_sync
, &rdev
->flags
))
4823 addr
= r10b
->devs
[slot
].addr
+ idx
* PAGE_SIZE
;
4824 atomic_inc(&rdev
->nr_pending
);
4826 success
= sync_page_io(rdev
,
4830 REQ_OP_READ
, 0, false);
4831 rdev_dec_pending(rdev
, mddev
);
4837 if (slot
>= conf
->copies
)
4839 if (slot
== first_slot
)
4844 /* couldn't read this block, must give up */
4845 set_bit(MD_RECOVERY_INTR
,
4857 static void end_reshape_write(struct bio
*bio
)
4859 struct r10bio
*r10_bio
= get_resync_r10bio(bio
);
4860 struct mddev
*mddev
= r10_bio
->mddev
;
4861 struct r10conf
*conf
= mddev
->private;
4865 struct md_rdev
*rdev
= NULL
;
4867 d
= find_bio_disk(conf
, r10_bio
, bio
, &slot
, &repl
);
4869 rdev
= conf
->mirrors
[d
].replacement
;
4872 rdev
= conf
->mirrors
[d
].rdev
;
4875 if (bio
->bi_status
) {
4876 /* FIXME should record badblock */
4877 md_error(mddev
, rdev
);
4880 rdev_dec_pending(rdev
, mddev
);
4881 end_reshape_request(r10_bio
);
4884 static void end_reshape_request(struct r10bio
*r10_bio
)
4886 if (!atomic_dec_and_test(&r10_bio
->remaining
))
4888 md_done_sync(r10_bio
->mddev
, r10_bio
->sectors
, 1);
4889 bio_put(r10_bio
->master_bio
);
4893 static void raid10_finish_reshape(struct mddev
*mddev
)
4895 struct r10conf
*conf
= mddev
->private;
4897 if (test_bit(MD_RECOVERY_INTR
, &mddev
->recovery
))
4900 if (mddev
->delta_disks
> 0) {
4901 if (mddev
->recovery_cp
> mddev
->resync_max_sectors
) {
4902 mddev
->recovery_cp
= mddev
->resync_max_sectors
;
4903 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
4905 mddev
->resync_max_sectors
= mddev
->array_sectors
;
4909 for (d
= conf
->geo
.raid_disks
;
4910 d
< conf
->geo
.raid_disks
- mddev
->delta_disks
;
4912 struct md_rdev
*rdev
= rcu_dereference(conf
->mirrors
[d
].rdev
);
4914 clear_bit(In_sync
, &rdev
->flags
);
4915 rdev
= rcu_dereference(conf
->mirrors
[d
].replacement
);
4917 clear_bit(In_sync
, &rdev
->flags
);
4921 mddev
->layout
= mddev
->new_layout
;
4922 mddev
->chunk_sectors
= 1 << conf
->geo
.chunk_shift
;
4923 mddev
->reshape_position
= MaxSector
;
4924 mddev
->delta_disks
= 0;
4925 mddev
->reshape_backwards
= 0;
4928 static struct md_personality raid10_personality
=
4932 .owner
= THIS_MODULE
,
4933 .make_request
= raid10_make_request
,
4935 .free
= raid10_free
,
4936 .status
= raid10_status
,
4937 .error_handler
= raid10_error
,
4938 .hot_add_disk
= raid10_add_disk
,
4939 .hot_remove_disk
= raid10_remove_disk
,
4940 .spare_active
= raid10_spare_active
,
4941 .sync_request
= raid10_sync_request
,
4942 .quiesce
= raid10_quiesce
,
4943 .size
= raid10_size
,
4944 .resize
= raid10_resize
,
4945 .takeover
= raid10_takeover
,
4946 .check_reshape
= raid10_check_reshape
,
4947 .start_reshape
= raid10_start_reshape
,
4948 .finish_reshape
= raid10_finish_reshape
,
4949 .update_reshape_pos
= raid10_update_reshape_pos
,
4950 .congested
= raid10_congested
,
4953 static int __init
raid_init(void)
4955 return register_md_personality(&raid10_personality
);
4958 static void raid_exit(void)
4960 unregister_md_personality(&raid10_personality
);
4963 module_init(raid_init
);
4964 module_exit(raid_exit
);
4965 MODULE_LICENSE("GPL");
4966 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4967 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4968 MODULE_ALIAS("md-raid10");
4969 MODULE_ALIAS("md-level-10");
4971 module_param(max_queued_requests
, int, S_IRUGO
|S_IWUSR
);