1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * raid10.c : Multiple Devices driver for Linux
5 * Copyright (C) 2000-2004 Neil Brown
7 * RAID-10 support for md.
9 * Base on code in raid1.c. See raid1.c for further copyright information.
12 #include <linux/slab.h>
13 #include <linux/delay.h>
14 #include <linux/blkdev.h>
15 #include <linux/module.h>
16 #include <linux/seq_file.h>
17 #include <linux/ratelimit.h>
18 #include <linux/kthread.h>
19 #include <linux/raid/md_p.h>
20 #include <trace/events/block.h>
24 #include "md-bitmap.h"
27 * RAID10 provides a combination of RAID0 and RAID1 functionality.
28 * The layout of data is defined by
31 * near_copies (stored in low byte of layout)
32 * far_copies (stored in second byte of layout)
33 * far_offset (stored in bit 16 of layout )
34 * use_far_sets (stored in bit 17 of layout )
35 * use_far_sets_bugfixed (stored in bit 18 of layout )
37 * The data to be stored is divided into chunks using chunksize. Each device
38 * is divided into far_copies sections. In each section, chunks are laid out
39 * in a style similar to raid0, but near_copies copies of each chunk is stored
40 * (each on a different drive). The starting device for each section is offset
41 * near_copies from the starting device of the previous section. Thus there
42 * are (near_copies * far_copies) of each chunk, and each is on a different
43 * drive. near_copies and far_copies must be at least one, and their product
44 * is at most raid_disks.
46 * If far_offset is true, then the far_copies are handled a bit differently.
47 * The copies are still in different stripes, but instead of being very far
48 * apart on disk, there are adjacent stripes.
50 * The far and offset algorithms are handled slightly differently if
51 * 'use_far_sets' is true. In this case, the array's devices are grouped into
52 * sets that are (near_copies * far_copies) in size. The far copied stripes
53 * are still shifted by 'near_copies' devices, but this shifting stays confined
54 * to the set rather than the entire array. This is done to improve the number
55 * of device combinations that can fail without causing the array to fail.
56 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
61 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
62 * [A B] [C D] [A B] [C D E]
63 * |...| |...| |...| | ... |
64 * [B A] [D C] [B A] [E C D]
67 static void allow_barrier(struct r10conf *conf);
68 static void lower_barrier(struct r10conf *conf);
69 static int _enough(struct r10conf *conf, int previous, int ignore);
70 static int enough(struct r10conf *conf, int ignore);
71 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
73 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
74 static void end_reshape_write(struct bio *bio);
75 static void end_reshape(struct r10conf *conf);
77 #define raid10_log(md, fmt, args...) \
78 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
83 * for resync bio, r10bio pointer can be retrieved from the per-bio
84 * 'struct resync_pages'.
86 static inline struct r10bio *get_resync_r10bio(struct bio *bio)
88 return get_resync_pages(bio)->raid_bio;
91 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
93 struct r10conf *conf = data;
94 int size = offsetof(struct r10bio, devs[conf->geo.raid_disks]);
96 /* allocate a r10bio with room for raid_disks entries in the
98 return kzalloc(size, gfp_flags);
101 #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9)
102 /* amount of memory to reserve for resync requests */
103 #define RESYNC_WINDOW (1024*1024)
104 /* maximum number of concurrent requests, memory permitting */
105 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
106 #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW)
107 #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9)
110 * When performing a resync, we need to read and compare, so
111 * we need as many pages are there are copies.
112 * When performing a recovery, we need 2 bios, one for read,
113 * one for write (we recover only one drive per r10buf)
116 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
118 struct r10conf *conf = data;
119 struct r10bio *r10_bio;
122 int nalloc, nalloc_rp;
123 struct resync_pages *rps;
125 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
129 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
130 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
131 nalloc = conf->copies; /* resync */
133 nalloc = 2; /* recovery */
135 /* allocate once for all bios */
136 if (!conf->have_replacement)
139 nalloc_rp = nalloc * 2;
140 rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags);
142 goto out_free_r10bio;
147 for (j = nalloc ; j-- ; ) {
148 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
151 r10_bio->devs[j].bio = bio;
152 if (!conf->have_replacement)
154 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
157 r10_bio->devs[j].repl_bio = bio;
160 * Allocate RESYNC_PAGES data pages and attach them
163 for (j = 0; j < nalloc; j++) {
164 struct bio *rbio = r10_bio->devs[j].repl_bio;
165 struct resync_pages *rp, *rp_repl;
169 rp_repl = &rps[nalloc + j];
171 bio = r10_bio->devs[j].bio;
173 if (!j || test_bit(MD_RECOVERY_SYNC,
174 &conf->mddev->recovery)) {
175 if (resync_alloc_pages(rp, gfp_flags))
178 memcpy(rp, &rps[0], sizeof(*rp));
179 resync_get_all_pages(rp);
182 rp->raid_bio = r10_bio;
183 bio->bi_private = rp;
185 memcpy(rp_repl, rp, sizeof(*rp));
186 rbio->bi_private = rp_repl;
194 resync_free_pages(&rps[j]);
198 for ( ; j < nalloc; j++) {
199 if (r10_bio->devs[j].bio)
200 bio_put(r10_bio->devs[j].bio);
201 if (r10_bio->devs[j].repl_bio)
202 bio_put(r10_bio->devs[j].repl_bio);
206 rbio_pool_free(r10_bio, conf);
210 static void r10buf_pool_free(void *__r10_bio, void *data)
212 struct r10conf *conf = data;
213 struct r10bio *r10bio = __r10_bio;
215 struct resync_pages *rp = NULL;
217 for (j = conf->copies; j--; ) {
218 struct bio *bio = r10bio->devs[j].bio;
221 rp = get_resync_pages(bio);
222 resync_free_pages(rp);
226 bio = r10bio->devs[j].repl_bio;
231 /* resync pages array stored in the 1st bio's .bi_private */
234 rbio_pool_free(r10bio, conf);
237 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
241 for (i = 0; i < conf->geo.raid_disks; i++) {
242 struct bio **bio = & r10_bio->devs[i].bio;
243 if (!BIO_SPECIAL(*bio))
246 bio = &r10_bio->devs[i].repl_bio;
247 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
253 static void free_r10bio(struct r10bio *r10_bio)
255 struct r10conf *conf = r10_bio->mddev->private;
257 put_all_bios(conf, r10_bio);
258 mempool_free(r10_bio, &conf->r10bio_pool);
261 static void put_buf(struct r10bio *r10_bio)
263 struct r10conf *conf = r10_bio->mddev->private;
265 mempool_free(r10_bio, &conf->r10buf_pool);
270 static void reschedule_retry(struct r10bio *r10_bio)
273 struct mddev *mddev = r10_bio->mddev;
274 struct r10conf *conf = mddev->private;
276 spin_lock_irqsave(&conf->device_lock, flags);
277 list_add(&r10_bio->retry_list, &conf->retry_list);
279 spin_unlock_irqrestore(&conf->device_lock, flags);
281 /* wake up frozen array... */
282 wake_up(&conf->wait_barrier);
284 md_wakeup_thread(mddev->thread);
288 * raid_end_bio_io() is called when we have finished servicing a mirrored
289 * operation and are ready to return a success/failure code to the buffer
292 static void raid_end_bio_io(struct r10bio *r10_bio)
294 struct bio *bio = r10_bio->master_bio;
295 struct r10conf *conf = r10_bio->mddev->private;
297 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
298 bio->bi_status = BLK_STS_IOERR;
302 * Wake up any possible resync thread that waits for the device
307 free_r10bio(r10_bio);
311 * Update disk head position estimator based on IRQ completion info.
313 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
315 struct r10conf *conf = r10_bio->mddev->private;
317 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
318 r10_bio->devs[slot].addr + (r10_bio->sectors);
322 * Find the disk number which triggered given bio
324 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
325 struct bio *bio, int *slotp, int *replp)
330 for (slot = 0; slot < conf->geo.raid_disks; slot++) {
331 if (r10_bio->devs[slot].bio == bio)
333 if (r10_bio->devs[slot].repl_bio == bio) {
339 update_head_pos(slot, r10_bio);
345 return r10_bio->devs[slot].devnum;
348 static void raid10_end_read_request(struct bio *bio)
350 int uptodate = !bio->bi_status;
351 struct r10bio *r10_bio = bio->bi_private;
353 struct md_rdev *rdev;
354 struct r10conf *conf = r10_bio->mddev->private;
356 slot = r10_bio->read_slot;
357 rdev = r10_bio->devs[slot].rdev;
359 * this branch is our 'one mirror IO has finished' event handler:
361 update_head_pos(slot, r10_bio);
365 * Set R10BIO_Uptodate in our master bio, so that
366 * we will return a good error code to the higher
367 * levels even if IO on some other mirrored buffer fails.
369 * The 'master' represents the composite IO operation to
370 * user-side. So if something waits for IO, then it will
371 * wait for the 'master' bio.
373 set_bit(R10BIO_Uptodate, &r10_bio->state);
375 /* If all other devices that store this block have
376 * failed, we want to return the error upwards rather
377 * than fail the last device. Here we redefine
378 * "uptodate" to mean "Don't want to retry"
380 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
385 raid_end_bio_io(r10_bio);
386 rdev_dec_pending(rdev, conf->mddev);
389 * oops, read error - keep the refcount on the rdev
391 char b[BDEVNAME_SIZE];
392 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
394 bdevname(rdev->bdev, b),
395 (unsigned long long)r10_bio->sector);
396 set_bit(R10BIO_ReadError, &r10_bio->state);
397 reschedule_retry(r10_bio);
401 static void close_write(struct r10bio *r10_bio)
403 /* clear the bitmap if all writes complete successfully */
404 md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
406 !test_bit(R10BIO_Degraded, &r10_bio->state),
408 md_write_end(r10_bio->mddev);
411 static void one_write_done(struct r10bio *r10_bio)
413 if (atomic_dec_and_test(&r10_bio->remaining)) {
414 if (test_bit(R10BIO_WriteError, &r10_bio->state))
415 reschedule_retry(r10_bio);
417 close_write(r10_bio);
418 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
419 reschedule_retry(r10_bio);
421 raid_end_bio_io(r10_bio);
426 static void raid10_end_write_request(struct bio *bio)
428 struct r10bio *r10_bio = bio->bi_private;
431 struct r10conf *conf = r10_bio->mddev->private;
433 struct md_rdev *rdev = NULL;
434 struct bio *to_put = NULL;
437 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
439 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
442 rdev = conf->mirrors[dev].replacement;
446 rdev = conf->mirrors[dev].rdev;
449 * this branch is our 'one mirror IO has finished' event handler:
451 if (bio->bi_status && !discard_error) {
453 /* Never record new bad blocks to replacement,
456 md_error(rdev->mddev, rdev);
458 set_bit(WriteErrorSeen, &rdev->flags);
459 if (!test_and_set_bit(WantReplacement, &rdev->flags))
460 set_bit(MD_RECOVERY_NEEDED,
461 &rdev->mddev->recovery);
464 if (test_bit(FailFast, &rdev->flags) &&
465 (bio->bi_opf & MD_FAILFAST)) {
466 md_error(rdev->mddev, rdev);
470 * When the device is faulty, it is not necessary to
471 * handle write error.
472 * For failfast, this is the only remaining device,
473 * We need to retry the write without FailFast.
475 if (!test_bit(Faulty, &rdev->flags))
476 set_bit(R10BIO_WriteError, &r10_bio->state);
478 r10_bio->devs[slot].bio = NULL;
485 * Set R10BIO_Uptodate in our master bio, so that
486 * we will return a good error code for to the higher
487 * levels even if IO on some other mirrored buffer fails.
489 * The 'master' represents the composite IO operation to
490 * user-side. So if something waits for IO, then it will
491 * wait for the 'master' bio.
497 * Do not set R10BIO_Uptodate if the current device is
498 * rebuilding or Faulty. This is because we cannot use
499 * such device for properly reading the data back (we could
500 * potentially use it, if the current write would have felt
501 * before rdev->recovery_offset, but for simplicity we don't
504 if (test_bit(In_sync, &rdev->flags) &&
505 !test_bit(Faulty, &rdev->flags))
506 set_bit(R10BIO_Uptodate, &r10_bio->state);
508 /* Maybe we can clear some bad blocks. */
509 if (is_badblock(rdev,
510 r10_bio->devs[slot].addr,
512 &first_bad, &bad_sectors) && !discard_error) {
515 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
517 r10_bio->devs[slot].bio = IO_MADE_GOOD;
519 set_bit(R10BIO_MadeGood, &r10_bio->state);
525 * Let's see if all mirrored write operations have finished
528 one_write_done(r10_bio);
530 rdev_dec_pending(rdev, conf->mddev);
536 * RAID10 layout manager
537 * As well as the chunksize and raid_disks count, there are two
538 * parameters: near_copies and far_copies.
539 * near_copies * far_copies must be <= raid_disks.
540 * Normally one of these will be 1.
541 * If both are 1, we get raid0.
542 * If near_copies == raid_disks, we get raid1.
544 * Chunks are laid out in raid0 style with near_copies copies of the
545 * first chunk, followed by near_copies copies of the next chunk and
547 * If far_copies > 1, then after 1/far_copies of the array has been assigned
548 * as described above, we start again with a device offset of near_copies.
549 * So we effectively have another copy of the whole array further down all
550 * the drives, but with blocks on different drives.
551 * With this layout, and block is never stored twice on the one device.
553 * raid10_find_phys finds the sector offset of a given virtual sector
554 * on each device that it is on.
556 * raid10_find_virt does the reverse mapping, from a device and a
557 * sector offset to a virtual address
560 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
568 int last_far_set_start, last_far_set_size;
570 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
571 last_far_set_start *= geo->far_set_size;
573 last_far_set_size = geo->far_set_size;
574 last_far_set_size += (geo->raid_disks % geo->far_set_size);
576 /* now calculate first sector/dev */
577 chunk = r10bio->sector >> geo->chunk_shift;
578 sector = r10bio->sector & geo->chunk_mask;
580 chunk *= geo->near_copies;
582 dev = sector_div(stripe, geo->raid_disks);
584 stripe *= geo->far_copies;
586 sector += stripe << geo->chunk_shift;
588 /* and calculate all the others */
589 for (n = 0; n < geo->near_copies; n++) {
593 r10bio->devs[slot].devnum = d;
594 r10bio->devs[slot].addr = s;
597 for (f = 1; f < geo->far_copies; f++) {
598 set = d / geo->far_set_size;
599 d += geo->near_copies;
601 if ((geo->raid_disks % geo->far_set_size) &&
602 (d > last_far_set_start)) {
603 d -= last_far_set_start;
604 d %= last_far_set_size;
605 d += last_far_set_start;
607 d %= geo->far_set_size;
608 d += geo->far_set_size * set;
611 r10bio->devs[slot].devnum = d;
612 r10bio->devs[slot].addr = s;
616 if (dev >= geo->raid_disks) {
618 sector += (geo->chunk_mask + 1);
623 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
625 struct geom *geo = &conf->geo;
627 if (conf->reshape_progress != MaxSector &&
628 ((r10bio->sector >= conf->reshape_progress) !=
629 conf->mddev->reshape_backwards)) {
630 set_bit(R10BIO_Previous, &r10bio->state);
633 clear_bit(R10BIO_Previous, &r10bio->state);
635 __raid10_find_phys(geo, r10bio);
638 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
640 sector_t offset, chunk, vchunk;
641 /* Never use conf->prev as this is only called during resync
642 * or recovery, so reshape isn't happening
644 struct geom *geo = &conf->geo;
645 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
646 int far_set_size = geo->far_set_size;
647 int last_far_set_start;
649 if (geo->raid_disks % geo->far_set_size) {
650 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
651 last_far_set_start *= geo->far_set_size;
653 if (dev >= last_far_set_start) {
654 far_set_size = geo->far_set_size;
655 far_set_size += (geo->raid_disks % geo->far_set_size);
656 far_set_start = last_far_set_start;
660 offset = sector & geo->chunk_mask;
661 if (geo->far_offset) {
663 chunk = sector >> geo->chunk_shift;
664 fc = sector_div(chunk, geo->far_copies);
665 dev -= fc * geo->near_copies;
666 if (dev < far_set_start)
669 while (sector >= geo->stride) {
670 sector -= geo->stride;
671 if (dev < (geo->near_copies + far_set_start))
672 dev += far_set_size - geo->near_copies;
674 dev -= geo->near_copies;
676 chunk = sector >> geo->chunk_shift;
678 vchunk = chunk * geo->raid_disks + dev;
679 sector_div(vchunk, geo->near_copies);
680 return (vchunk << geo->chunk_shift) + offset;
684 * This routine returns the disk from which the requested read should
685 * be done. There is a per-array 'next expected sequential IO' sector
686 * number - if this matches on the next IO then we use the last disk.
687 * There is also a per-disk 'last know head position' sector that is
688 * maintained from IRQ contexts, both the normal and the resync IO
689 * completion handlers update this position correctly. If there is no
690 * perfect sequential match then we pick the disk whose head is closest.
692 * If there are 2 mirrors in the same 2 devices, performance degrades
693 * because position is mirror, not device based.
695 * The rdev for the device selected will have nr_pending incremented.
699 * FIXME: possibly should rethink readbalancing and do it differently
700 * depending on near_copies / far_copies geometry.
702 static struct md_rdev *read_balance(struct r10conf *conf,
703 struct r10bio *r10_bio,
706 const sector_t this_sector = r10_bio->sector;
708 int sectors = r10_bio->sectors;
709 int best_good_sectors;
710 sector_t new_distance, best_dist;
711 struct md_rdev *best_dist_rdev, *best_pending_rdev, *rdev = NULL;
713 int best_dist_slot, best_pending_slot;
714 bool has_nonrot_disk = false;
715 unsigned int min_pending;
716 struct geom *geo = &conf->geo;
718 raid10_find_phys(conf, r10_bio);
721 min_pending = UINT_MAX;
722 best_dist_rdev = NULL;
723 best_pending_rdev = NULL;
724 best_dist = MaxSector;
725 best_good_sectors = 0;
727 clear_bit(R10BIO_FailFast, &r10_bio->state);
729 * Check if we can balance. We can balance on the whole
730 * device if no resync is going on (recovery is ok), or below
731 * the resync window. We take the first readable disk when
732 * above the resync window.
734 if ((conf->mddev->recovery_cp < MaxSector
735 && (this_sector + sectors >= conf->next_resync)) ||
736 (mddev_is_clustered(conf->mddev) &&
737 md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector,
738 this_sector + sectors)))
741 for (slot = 0; slot < conf->copies ; slot++) {
745 unsigned int pending;
748 if (r10_bio->devs[slot].bio == IO_BLOCKED)
750 disk = r10_bio->devs[slot].devnum;
751 rdev = rcu_dereference(conf->mirrors[disk].replacement);
752 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
753 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
754 rdev = rcu_dereference(conf->mirrors[disk].rdev);
756 test_bit(Faulty, &rdev->flags))
758 if (!test_bit(In_sync, &rdev->flags) &&
759 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
762 dev_sector = r10_bio->devs[slot].addr;
763 if (is_badblock(rdev, dev_sector, sectors,
764 &first_bad, &bad_sectors)) {
765 if (best_dist < MaxSector)
766 /* Already have a better slot */
768 if (first_bad <= dev_sector) {
769 /* Cannot read here. If this is the
770 * 'primary' device, then we must not read
771 * beyond 'bad_sectors' from another device.
773 bad_sectors -= (dev_sector - first_bad);
774 if (!do_balance && sectors > bad_sectors)
775 sectors = bad_sectors;
776 if (best_good_sectors > sectors)
777 best_good_sectors = sectors;
779 sector_t good_sectors =
780 first_bad - dev_sector;
781 if (good_sectors > best_good_sectors) {
782 best_good_sectors = good_sectors;
783 best_dist_slot = slot;
784 best_dist_rdev = rdev;
787 /* Must read from here */
792 best_good_sectors = sectors;
797 nonrot = blk_queue_nonrot(bdev_get_queue(rdev->bdev));
798 has_nonrot_disk |= nonrot;
799 pending = atomic_read(&rdev->nr_pending);
800 if (min_pending > pending && nonrot) {
801 min_pending = pending;
802 best_pending_slot = slot;
803 best_pending_rdev = rdev;
806 if (best_dist_slot >= 0)
807 /* At least 2 disks to choose from so failfast is OK */
808 set_bit(R10BIO_FailFast, &r10_bio->state);
809 /* This optimisation is debatable, and completely destroys
810 * sequential read speed for 'far copies' arrays. So only
811 * keep it for 'near' arrays, and review those later.
813 if (geo->near_copies > 1 && !pending)
816 /* for far > 1 always use the lowest address */
817 else if (geo->far_copies > 1)
818 new_distance = r10_bio->devs[slot].addr;
820 new_distance = abs(r10_bio->devs[slot].addr -
821 conf->mirrors[disk].head_position);
823 if (new_distance < best_dist) {
824 best_dist = new_distance;
825 best_dist_slot = slot;
826 best_dist_rdev = rdev;
829 if (slot >= conf->copies) {
830 if (has_nonrot_disk) {
831 slot = best_pending_slot;
832 rdev = best_pending_rdev;
834 slot = best_dist_slot;
835 rdev = best_dist_rdev;
840 atomic_inc(&rdev->nr_pending);
841 r10_bio->read_slot = slot;
845 *max_sectors = best_good_sectors;
850 static void flush_pending_writes(struct r10conf *conf)
852 /* Any writes that have been queued but are awaiting
853 * bitmap updates get flushed here.
855 spin_lock_irq(&conf->device_lock);
857 if (conf->pending_bio_list.head) {
858 struct blk_plug plug;
861 bio = bio_list_get(&conf->pending_bio_list);
862 conf->pending_count = 0;
863 spin_unlock_irq(&conf->device_lock);
866 * As this is called in a wait_event() loop (see freeze_array),
867 * current->state might be TASK_UNINTERRUPTIBLE which will
868 * cause a warning when we prepare to wait again. As it is
869 * rare that this path is taken, it is perfectly safe to force
870 * us to go around the wait_event() loop again, so the warning
871 * is a false-positive. Silence the warning by resetting
874 __set_current_state(TASK_RUNNING);
876 blk_start_plug(&plug);
877 /* flush any pending bitmap writes to disk
878 * before proceeding w/ I/O */
879 md_bitmap_unplug(conf->mddev->bitmap);
880 wake_up(&conf->wait_barrier);
882 while (bio) { /* submit pending writes */
883 struct bio *next = bio->bi_next;
884 struct md_rdev *rdev = (void*)bio->bi_disk;
886 bio_set_dev(bio, rdev->bdev);
887 if (test_bit(Faulty, &rdev->flags)) {
889 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
890 !blk_queue_discard(bio->bi_disk->queue)))
894 submit_bio_noacct(bio);
897 blk_finish_plug(&plug);
899 spin_unlock_irq(&conf->device_lock);
903 * Sometimes we need to suspend IO while we do something else,
904 * either some resync/recovery, or reconfigure the array.
905 * To do this we raise a 'barrier'.
906 * The 'barrier' is a counter that can be raised multiple times
907 * to count how many activities are happening which preclude
909 * We can only raise the barrier if there is no pending IO.
910 * i.e. if nr_pending == 0.
911 * We choose only to raise the barrier if no-one is waiting for the
912 * barrier to go down. This means that as soon as an IO request
913 * is ready, no other operations which require a barrier will start
914 * until the IO request has had a chance.
916 * So: regular IO calls 'wait_barrier'. When that returns there
917 * is no backgroup IO happening, It must arrange to call
918 * allow_barrier when it has finished its IO.
919 * backgroup IO calls must call raise_barrier. Once that returns
920 * there is no normal IO happeing. It must arrange to call
921 * lower_barrier when the particular background IO completes.
924 static void raise_barrier(struct r10conf *conf, int force)
926 BUG_ON(force && !conf->barrier);
927 spin_lock_irq(&conf->resync_lock);
929 /* Wait until no block IO is waiting (unless 'force') */
930 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
933 /* block any new IO from starting */
936 /* Now wait for all pending IO to complete */
937 wait_event_lock_irq(conf->wait_barrier,
938 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
941 spin_unlock_irq(&conf->resync_lock);
944 static void lower_barrier(struct r10conf *conf)
947 spin_lock_irqsave(&conf->resync_lock, flags);
949 spin_unlock_irqrestore(&conf->resync_lock, flags);
950 wake_up(&conf->wait_barrier);
953 static void wait_barrier(struct r10conf *conf)
955 spin_lock_irq(&conf->resync_lock);
957 struct bio_list *bio_list = current->bio_list;
959 /* Wait for the barrier to drop.
960 * However if there are already pending
961 * requests (preventing the barrier from
962 * rising completely), and the
963 * pre-process bio queue isn't empty,
964 * then don't wait, as we need to empty
965 * that queue to get the nr_pending
968 raid10_log(conf->mddev, "wait barrier");
969 wait_event_lock_irq(conf->wait_barrier,
971 (atomic_read(&conf->nr_pending) &&
973 (!bio_list_empty(&bio_list[0]) ||
974 !bio_list_empty(&bio_list[1]))) ||
975 /* move on if recovery thread is
978 (conf->mddev->thread->tsk == current &&
979 test_bit(MD_RECOVERY_RUNNING,
980 &conf->mddev->recovery) &&
981 conf->nr_queued > 0),
984 if (!conf->nr_waiting)
985 wake_up(&conf->wait_barrier);
987 atomic_inc(&conf->nr_pending);
988 spin_unlock_irq(&conf->resync_lock);
991 static void allow_barrier(struct r10conf *conf)
993 if ((atomic_dec_and_test(&conf->nr_pending)) ||
994 (conf->array_freeze_pending))
995 wake_up(&conf->wait_barrier);
998 static void freeze_array(struct r10conf *conf, int extra)
1000 /* stop syncio and normal IO and wait for everything to
1002 * We increment barrier and nr_waiting, and then
1003 * wait until nr_pending match nr_queued+extra
1004 * This is called in the context of one normal IO request
1005 * that has failed. Thus any sync request that might be pending
1006 * will be blocked by nr_pending, and we need to wait for
1007 * pending IO requests to complete or be queued for re-try.
1008 * Thus the number queued (nr_queued) plus this request (extra)
1009 * must match the number of pending IOs (nr_pending) before
1012 spin_lock_irq(&conf->resync_lock);
1013 conf->array_freeze_pending++;
1016 wait_event_lock_irq_cmd(conf->wait_barrier,
1017 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
1019 flush_pending_writes(conf));
1021 conf->array_freeze_pending--;
1022 spin_unlock_irq(&conf->resync_lock);
1025 static void unfreeze_array(struct r10conf *conf)
1027 /* reverse the effect of the freeze */
1028 spin_lock_irq(&conf->resync_lock);
1031 wake_up(&conf->wait_barrier);
1032 spin_unlock_irq(&conf->resync_lock);
1035 static sector_t choose_data_offset(struct r10bio *r10_bio,
1036 struct md_rdev *rdev)
1038 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1039 test_bit(R10BIO_Previous, &r10_bio->state))
1040 return rdev->data_offset;
1042 return rdev->new_data_offset;
1045 struct raid10_plug_cb {
1046 struct blk_plug_cb cb;
1047 struct bio_list pending;
1051 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1053 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1055 struct mddev *mddev = plug->cb.data;
1056 struct r10conf *conf = mddev->private;
1059 if (from_schedule || current->bio_list) {
1060 spin_lock_irq(&conf->device_lock);
1061 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1062 conf->pending_count += plug->pending_cnt;
1063 spin_unlock_irq(&conf->device_lock);
1064 wake_up(&conf->wait_barrier);
1065 md_wakeup_thread(mddev->thread);
1070 /* we aren't scheduling, so we can do the write-out directly. */
1071 bio = bio_list_get(&plug->pending);
1072 md_bitmap_unplug(mddev->bitmap);
1073 wake_up(&conf->wait_barrier);
1075 while (bio) { /* submit pending writes */
1076 struct bio *next = bio->bi_next;
1077 struct md_rdev *rdev = (void*)bio->bi_disk;
1078 bio->bi_next = NULL;
1079 bio_set_dev(bio, rdev->bdev);
1080 if (test_bit(Faulty, &rdev->flags)) {
1082 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1083 !blk_queue_discard(bio->bi_disk->queue)))
1084 /* Just ignore it */
1087 submit_bio_noacct(bio);
1094 * 1. Register the new request and wait if the reconstruction thread has put
1095 * up a bar for new requests. Continue immediately if no resync is active
1097 * 2. If IO spans the reshape position. Need to wait for reshape to pass.
1099 static void regular_request_wait(struct mddev *mddev, struct r10conf *conf,
1100 struct bio *bio, sector_t sectors)
1103 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1104 bio->bi_iter.bi_sector < conf->reshape_progress &&
1105 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1106 raid10_log(conf->mddev, "wait reshape");
1107 allow_barrier(conf);
1108 wait_event(conf->wait_barrier,
1109 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1110 conf->reshape_progress >= bio->bi_iter.bi_sector +
1116 static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1117 struct r10bio *r10_bio)
1119 struct r10conf *conf = mddev->private;
1120 struct bio *read_bio;
1121 const int op = bio_op(bio);
1122 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1124 struct md_rdev *rdev;
1125 char b[BDEVNAME_SIZE];
1126 int slot = r10_bio->read_slot;
1127 struct md_rdev *err_rdev = NULL;
1128 gfp_t gfp = GFP_NOIO;
1130 if (r10_bio->devs[slot].rdev) {
1132 * This is an error retry, but we cannot
1133 * safely dereference the rdev in the r10_bio,
1134 * we must use the one in conf.
1135 * If it has already been disconnected (unlikely)
1136 * we lose the device name in error messages.
1140 * As we are blocking raid10, it is a little safer to
1143 gfp = GFP_NOIO | __GFP_HIGH;
1146 disk = r10_bio->devs[slot].devnum;
1147 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1149 bdevname(err_rdev->bdev, b);
1152 /* This never gets dereferenced */
1153 err_rdev = r10_bio->devs[slot].rdev;
1158 regular_request_wait(mddev, conf, bio, r10_bio->sectors);
1159 rdev = read_balance(conf, r10_bio, &max_sectors);
1162 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1164 (unsigned long long)r10_bio->sector);
1166 raid_end_bio_io(r10_bio);
1170 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1172 bdevname(rdev->bdev, b),
1173 (unsigned long long)r10_bio->sector);
1174 if (max_sectors < bio_sectors(bio)) {
1175 struct bio *split = bio_split(bio, max_sectors,
1176 gfp, &conf->bio_split);
1177 bio_chain(split, bio);
1178 allow_barrier(conf);
1179 submit_bio_noacct(bio);
1182 r10_bio->master_bio = bio;
1183 r10_bio->sectors = max_sectors;
1185 slot = r10_bio->read_slot;
1187 read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set);
1189 r10_bio->devs[slot].bio = read_bio;
1190 r10_bio->devs[slot].rdev = rdev;
1192 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1193 choose_data_offset(r10_bio, rdev);
1194 bio_set_dev(read_bio, rdev->bdev);
1195 read_bio->bi_end_io = raid10_end_read_request;
1196 bio_set_op_attrs(read_bio, op, do_sync);
1197 if (test_bit(FailFast, &rdev->flags) &&
1198 test_bit(R10BIO_FailFast, &r10_bio->state))
1199 read_bio->bi_opf |= MD_FAILFAST;
1200 read_bio->bi_private = r10_bio;
1203 trace_block_bio_remap(read_bio->bi_disk->queue,
1204 read_bio, disk_devt(mddev->gendisk),
1206 submit_bio_noacct(read_bio);
1210 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1211 struct bio *bio, bool replacement,
1214 const int op = bio_op(bio);
1215 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1216 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1217 unsigned long flags;
1218 struct blk_plug_cb *cb;
1219 struct raid10_plug_cb *plug = NULL;
1220 struct r10conf *conf = mddev->private;
1221 struct md_rdev *rdev;
1222 int devnum = r10_bio->devs[n_copy].devnum;
1226 rdev = conf->mirrors[devnum].replacement;
1228 /* Replacement just got moved to main 'rdev' */
1230 rdev = conf->mirrors[devnum].rdev;
1233 rdev = conf->mirrors[devnum].rdev;
1235 mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
1237 r10_bio->devs[n_copy].repl_bio = mbio;
1239 r10_bio->devs[n_copy].bio = mbio;
1241 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1242 choose_data_offset(r10_bio, rdev));
1243 bio_set_dev(mbio, rdev->bdev);
1244 mbio->bi_end_io = raid10_end_write_request;
1245 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1246 if (!replacement && test_bit(FailFast,
1247 &conf->mirrors[devnum].rdev->flags)
1248 && enough(conf, devnum))
1249 mbio->bi_opf |= MD_FAILFAST;
1250 mbio->bi_private = r10_bio;
1252 if (conf->mddev->gendisk)
1253 trace_block_bio_remap(mbio->bi_disk->queue,
1254 mbio, disk_devt(conf->mddev->gendisk),
1256 /* flush_pending_writes() needs access to the rdev so...*/
1257 mbio->bi_disk = (void *)rdev;
1259 atomic_inc(&r10_bio->remaining);
1261 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1263 plug = container_of(cb, struct raid10_plug_cb, cb);
1267 bio_list_add(&plug->pending, mbio);
1268 plug->pending_cnt++;
1270 spin_lock_irqsave(&conf->device_lock, flags);
1271 bio_list_add(&conf->pending_bio_list, mbio);
1272 conf->pending_count++;
1273 spin_unlock_irqrestore(&conf->device_lock, flags);
1274 md_wakeup_thread(mddev->thread);
1278 static void wait_blocked_dev(struct mddev *mddev, struct r10bio *r10_bio)
1281 struct r10conf *conf = mddev->private;
1282 struct md_rdev *blocked_rdev;
1285 blocked_rdev = NULL;
1287 for (i = 0; i < conf->copies; i++) {
1288 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1289 struct md_rdev *rrdev = rcu_dereference(
1290 conf->mirrors[i].replacement);
1293 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1294 atomic_inc(&rdev->nr_pending);
1295 blocked_rdev = rdev;
1298 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1299 atomic_inc(&rrdev->nr_pending);
1300 blocked_rdev = rrdev;
1304 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1306 sector_t dev_sector = r10_bio->devs[i].addr;
1310 /* Discard request doesn't care the write result
1311 * so it doesn't need to wait blocked disk here.
1313 if (!r10_bio->sectors)
1316 is_bad = is_badblock(rdev, dev_sector, r10_bio->sectors,
1317 &first_bad, &bad_sectors);
1319 /* Mustn't write here until the bad block
1322 atomic_inc(&rdev->nr_pending);
1323 set_bit(BlockedBadBlocks, &rdev->flags);
1324 blocked_rdev = rdev;
1331 if (unlikely(blocked_rdev)) {
1332 /* Have to wait for this device to get unblocked, then retry */
1333 allow_barrier(conf);
1334 raid10_log(conf->mddev, "%s wait rdev %d blocked",
1335 __func__, blocked_rdev->raid_disk);
1336 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1342 static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1343 struct r10bio *r10_bio)
1345 struct r10conf *conf = mddev->private;
1350 if ((mddev_is_clustered(mddev) &&
1351 md_cluster_ops->area_resyncing(mddev, WRITE,
1352 bio->bi_iter.bi_sector,
1353 bio_end_sector(bio)))) {
1356 prepare_to_wait(&conf->wait_barrier,
1358 if (!md_cluster_ops->area_resyncing(mddev, WRITE,
1359 bio->bi_iter.bi_sector, bio_end_sector(bio)))
1363 finish_wait(&conf->wait_barrier, &w);
1366 sectors = r10_bio->sectors;
1367 regular_request_wait(mddev, conf, bio, sectors);
1368 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1369 (mddev->reshape_backwards
1370 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1371 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1372 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1373 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1374 /* Need to update reshape_position in metadata */
1375 mddev->reshape_position = conf->reshape_progress;
1376 set_mask_bits(&mddev->sb_flags, 0,
1377 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1378 md_wakeup_thread(mddev->thread);
1379 raid10_log(conf->mddev, "wait reshape metadata");
1380 wait_event(mddev->sb_wait,
1381 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1383 conf->reshape_safe = mddev->reshape_position;
1386 if (conf->pending_count >= max_queued_requests) {
1387 md_wakeup_thread(mddev->thread);
1388 raid10_log(mddev, "wait queued");
1389 wait_event(conf->wait_barrier,
1390 conf->pending_count < max_queued_requests);
1392 /* first select target devices under rcu_lock and
1393 * inc refcount on their rdev. Record them by setting
1395 * If there are known/acknowledged bad blocks on any device
1396 * on which we have seen a write error, we want to avoid
1397 * writing to those blocks. This potentially requires several
1398 * writes to write around the bad blocks. Each set of writes
1399 * gets its own r10_bio with a set of bios attached.
1402 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1403 raid10_find_phys(conf, r10_bio);
1405 wait_blocked_dev(mddev, r10_bio);
1408 max_sectors = r10_bio->sectors;
1410 for (i = 0; i < conf->copies; i++) {
1411 int d = r10_bio->devs[i].devnum;
1412 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1413 struct md_rdev *rrdev = rcu_dereference(
1414 conf->mirrors[d].replacement);
1417 if (rdev && (test_bit(Faulty, &rdev->flags)))
1419 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1422 r10_bio->devs[i].bio = NULL;
1423 r10_bio->devs[i].repl_bio = NULL;
1425 if (!rdev && !rrdev) {
1426 set_bit(R10BIO_Degraded, &r10_bio->state);
1429 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1431 sector_t dev_sector = r10_bio->devs[i].addr;
1435 is_bad = is_badblock(rdev, dev_sector, max_sectors,
1436 &first_bad, &bad_sectors);
1437 if (is_bad && first_bad <= dev_sector) {
1438 /* Cannot write here at all */
1439 bad_sectors -= (dev_sector - first_bad);
1440 if (bad_sectors < max_sectors)
1441 /* Mustn't write more than bad_sectors
1442 * to other devices yet
1444 max_sectors = bad_sectors;
1445 /* We don't set R10BIO_Degraded as that
1446 * only applies if the disk is missing,
1447 * so it might be re-added, and we want to
1448 * know to recover this chunk.
1449 * In this case the device is here, and the
1450 * fact that this chunk is not in-sync is
1451 * recorded in the bad block log.
1456 int good_sectors = first_bad - dev_sector;
1457 if (good_sectors < max_sectors)
1458 max_sectors = good_sectors;
1462 r10_bio->devs[i].bio = bio;
1463 atomic_inc(&rdev->nr_pending);
1466 r10_bio->devs[i].repl_bio = bio;
1467 atomic_inc(&rrdev->nr_pending);
1472 if (max_sectors < r10_bio->sectors)
1473 r10_bio->sectors = max_sectors;
1475 if (r10_bio->sectors < bio_sectors(bio)) {
1476 struct bio *split = bio_split(bio, r10_bio->sectors,
1477 GFP_NOIO, &conf->bio_split);
1478 bio_chain(split, bio);
1479 allow_barrier(conf);
1480 submit_bio_noacct(bio);
1483 r10_bio->master_bio = bio;
1486 atomic_set(&r10_bio->remaining, 1);
1487 md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1489 for (i = 0; i < conf->copies; i++) {
1490 if (r10_bio->devs[i].bio)
1491 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
1492 if (r10_bio->devs[i].repl_bio)
1493 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
1495 one_write_done(r10_bio);
1498 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
1500 struct r10conf *conf = mddev->private;
1501 struct r10bio *r10_bio;
1503 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1505 r10_bio->master_bio = bio;
1506 r10_bio->sectors = sectors;
1508 r10_bio->mddev = mddev;
1509 r10_bio->sector = bio->bi_iter.bi_sector;
1511 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->geo.raid_disks);
1513 if (bio_data_dir(bio) == READ)
1514 raid10_read_request(mddev, bio, r10_bio);
1516 raid10_write_request(mddev, bio, r10_bio);
1519 static struct bio *raid10_split_bio(struct r10conf *conf,
1520 struct bio *bio, sector_t sectors, bool want_first)
1524 split = bio_split(bio, sectors, GFP_NOIO, &conf->bio_split);
1525 bio_chain(split, bio);
1526 allow_barrier(conf);
1528 submit_bio_noacct(bio);
1531 submit_bio_noacct(split);
1537 static void raid_end_discard_bio(struct r10bio *r10bio)
1539 struct r10conf *conf = r10bio->mddev->private;
1540 struct r10bio *first_r10bio;
1542 while (atomic_dec_and_test(&r10bio->remaining)) {
1544 allow_barrier(conf);
1546 if (!test_bit(R10BIO_Discard, &r10bio->state)) {
1547 first_r10bio = (struct r10bio *)r10bio->master_bio;
1548 free_r10bio(r10bio);
1549 r10bio = first_r10bio;
1551 md_write_end(r10bio->mddev);
1552 bio_endio(r10bio->master_bio);
1553 free_r10bio(r10bio);
1559 static void raid10_end_discard_request(struct bio *bio)
1561 struct r10bio *r10_bio = bio->bi_private;
1562 struct r10conf *conf = r10_bio->mddev->private;
1563 struct md_rdev *rdev = NULL;
1568 * We don't care the return value of discard bio
1570 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
1571 set_bit(R10BIO_Uptodate, &r10_bio->state);
1573 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1575 rdev = conf->mirrors[dev].replacement;
1577 /* raid10_remove_disk uses smp_mb to make sure rdev is set to
1578 * replacement before setting replacement to NULL. It can read
1579 * rdev first without barrier protect even replacment is NULL
1582 rdev = conf->mirrors[dev].rdev;
1585 raid_end_discard_bio(r10_bio);
1586 rdev_dec_pending(rdev, conf->mddev);
1589 /* There are some limitations to handle discard bio
1590 * 1st, the discard size is bigger than stripe_size*2.
1591 * 2st, if the discard bio spans reshape progress, we use the old way to
1592 * handle discard bio
1594 static int raid10_handle_discard(struct mddev *mddev, struct bio *bio)
1596 struct r10conf *conf = mddev->private;
1597 struct geom *geo = &conf->geo;
1598 struct r10bio *r10_bio, *first_r10bio;
1599 int far_copies = geo->far_copies;
1600 bool first_copy = true;
1604 unsigned int stripe_size;
1605 sector_t split_size;
1607 sector_t bio_start, bio_end;
1608 sector_t first_stripe_index, last_stripe_index;
1609 sector_t start_disk_offset;
1610 unsigned int start_disk_index;
1611 sector_t end_disk_offset;
1612 unsigned int end_disk_index;
1613 unsigned int remainder;
1615 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1620 /* Check reshape again to avoid reshape happens after checking
1621 * MD_RECOVERY_RESHAPE and before wait_barrier
1623 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
1626 stripe_size = geo->raid_disks << geo->chunk_shift;
1627 bio_start = bio->bi_iter.bi_sector;
1628 bio_end = bio_end_sector(bio);
1630 /* Maybe one discard bio is smaller than strip size or across one stripe
1631 * and discard region is larger than one stripe size. For far offset layout,
1632 * if the discard region is not aligned with stripe size, there is hole
1633 * when we submit discard bio to member disk. For simplicity, we only
1634 * handle discard bio which discard region is bigger than stripe_size*2
1636 if (bio_sectors(bio) < stripe_size*2)
1639 /* For far and far offset layout, if bio is not aligned with stripe size,
1640 * it splits the part that is not aligned with strip size.
1642 div_u64_rem(bio_start, stripe_size, &remainder);
1643 if ((far_copies > 1) && remainder) {
1644 split_size = stripe_size - remainder;
1645 bio = raid10_split_bio(conf, bio, split_size, false);
1647 div_u64_rem(bio_end, stripe_size, &remainder);
1648 if ((far_copies > 1) && remainder) {
1649 split_size = bio_sectors(bio) - remainder;
1650 bio = raid10_split_bio(conf, bio, split_size, true);
1653 bio_start = bio->bi_iter.bi_sector;
1654 bio_end = bio_end_sector(bio);
1656 /* raid10 uses chunk as the unit to store data. It's similar like raid0.
1657 * One stripe contains the chunks from all member disk (one chunk from
1658 * one disk at the same HBA address). For layout detail, see 'man md 4'
1660 chunk = bio_start >> geo->chunk_shift;
1661 chunk *= geo->near_copies;
1662 first_stripe_index = chunk;
1663 start_disk_index = sector_div(first_stripe_index, geo->raid_disks);
1664 if (geo->far_offset)
1665 first_stripe_index *= geo->far_copies;
1666 start_disk_offset = (bio_start & geo->chunk_mask) +
1667 (first_stripe_index << geo->chunk_shift);
1669 chunk = bio_end >> geo->chunk_shift;
1670 chunk *= geo->near_copies;
1671 last_stripe_index = chunk;
1672 end_disk_index = sector_div(last_stripe_index, geo->raid_disks);
1673 if (geo->far_offset)
1674 last_stripe_index *= geo->far_copies;
1675 end_disk_offset = (bio_end & geo->chunk_mask) +
1676 (last_stripe_index << geo->chunk_shift);
1679 r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO);
1680 r10_bio->mddev = mddev;
1682 r10_bio->sectors = 0;
1683 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * geo->raid_disks);
1684 wait_blocked_dev(mddev, r10_bio);
1686 /* For far layout it needs more than one r10bio to cover all regions.
1687 * Inspired by raid10_sync_request, we can use the first r10bio->master_bio
1688 * to record the discard bio. Other r10bio->master_bio record the first
1689 * r10bio. The first r10bio only release after all other r10bios finish.
1690 * The discard bio returns only first r10bio finishes
1693 r10_bio->master_bio = bio;
1694 set_bit(R10BIO_Discard, &r10_bio->state);
1696 first_r10bio = r10_bio;
1698 r10_bio->master_bio = (struct bio *)first_r10bio;
1701 for (disk = 0; disk < geo->raid_disks; disk++) {
1702 struct md_rdev *rdev = rcu_dereference(conf->mirrors[disk].rdev);
1703 struct md_rdev *rrdev = rcu_dereference(
1704 conf->mirrors[disk].replacement);
1706 r10_bio->devs[disk].bio = NULL;
1707 r10_bio->devs[disk].repl_bio = NULL;
1709 if (rdev && (test_bit(Faulty, &rdev->flags)))
1711 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1713 if (!rdev && !rrdev)
1717 r10_bio->devs[disk].bio = bio;
1718 atomic_inc(&rdev->nr_pending);
1721 r10_bio->devs[disk].repl_bio = bio;
1722 atomic_inc(&rrdev->nr_pending);
1727 atomic_set(&r10_bio->remaining, 1);
1728 for (disk = 0; disk < geo->raid_disks; disk++) {
1729 sector_t dev_start, dev_end;
1730 struct bio *mbio, *rbio = NULL;
1731 struct md_rdev *rdev = rcu_dereference(conf->mirrors[disk].rdev);
1732 struct md_rdev *rrdev = rcu_dereference(
1733 conf->mirrors[disk].replacement);
1736 * Now start to calculate the start and end address for each disk.
1737 * The space between dev_start and dev_end is the discard region.
1739 * For dev_start, it needs to consider three conditions:
1740 * 1st, the disk is before start_disk, you can imagine the disk in
1741 * the next stripe. So the dev_start is the start address of next
1743 * 2st, the disk is after start_disk, it means the disk is at the
1744 * same stripe of first disk
1745 * 3st, the first disk itself, we can use start_disk_offset directly
1747 if (disk < start_disk_index)
1748 dev_start = (first_stripe_index + 1) * mddev->chunk_sectors;
1749 else if (disk > start_disk_index)
1750 dev_start = first_stripe_index * mddev->chunk_sectors;
1752 dev_start = start_disk_offset;
1754 if (disk < end_disk_index)
1755 dev_end = (last_stripe_index + 1) * mddev->chunk_sectors;
1756 else if (disk > end_disk_index)
1757 dev_end = last_stripe_index * mddev->chunk_sectors;
1759 dev_end = end_disk_offset;
1761 /* It only handles discard bio which size is >= stripe size, so
1762 * dev_end > dev_start all the time
1764 if (r10_bio->devs[disk].bio) {
1765 mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
1766 mbio->bi_end_io = raid10_end_discard_request;
1767 mbio->bi_private = r10_bio;
1768 r10_bio->devs[disk].bio = mbio;
1769 r10_bio->devs[disk].devnum = disk;
1770 atomic_inc(&r10_bio->remaining);
1771 md_submit_discard_bio(mddev, rdev, mbio,
1772 dev_start + choose_data_offset(r10_bio, rdev),
1773 dev_end - dev_start);
1776 if (r10_bio->devs[disk].repl_bio) {
1777 rbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
1778 rbio->bi_end_io = raid10_end_discard_request;
1779 rbio->bi_private = r10_bio;
1780 r10_bio->devs[disk].repl_bio = rbio;
1781 r10_bio->devs[disk].devnum = disk;
1782 atomic_inc(&r10_bio->remaining);
1783 md_submit_discard_bio(mddev, rrdev, rbio,
1784 dev_start + choose_data_offset(r10_bio, rrdev),
1785 dev_end - dev_start);
1790 if (!geo->far_offset && --far_copies) {
1791 first_stripe_index += geo->stride >> geo->chunk_shift;
1792 start_disk_offset += geo->stride;
1793 last_stripe_index += geo->stride >> geo->chunk_shift;
1794 end_disk_offset += geo->stride;
1795 atomic_inc(&first_r10bio->remaining);
1796 raid_end_discard_bio(r10_bio);
1801 raid_end_discard_bio(r10_bio);
1805 allow_barrier(conf);
1809 static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
1811 struct r10conf *conf = mddev->private;
1812 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1813 int chunk_sects = chunk_mask + 1;
1814 int sectors = bio_sectors(bio);
1816 if (unlikely(bio->bi_opf & REQ_PREFLUSH)
1817 && md_flush_request(mddev, bio))
1820 if (!md_write_start(mddev, bio))
1823 if (unlikely(bio_op(bio) == REQ_OP_DISCARD))
1824 if (!raid10_handle_discard(mddev, bio))
1828 * If this request crosses a chunk boundary, we need to split
1831 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1832 sectors > chunk_sects
1833 && (conf->geo.near_copies < conf->geo.raid_disks
1834 || conf->prev.near_copies <
1835 conf->prev.raid_disks)))
1836 sectors = chunk_sects -
1837 (bio->bi_iter.bi_sector &
1839 __make_request(mddev, bio, sectors);
1841 /* In case raid10d snuck in to freeze_array */
1842 wake_up(&conf->wait_barrier);
1846 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1848 struct r10conf *conf = mddev->private;
1851 if (conf->geo.near_copies < conf->geo.raid_disks)
1852 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1853 if (conf->geo.near_copies > 1)
1854 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1855 if (conf->geo.far_copies > 1) {
1856 if (conf->geo.far_offset)
1857 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1859 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1860 if (conf->geo.far_set_size != conf->geo.raid_disks)
1861 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1863 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1864 conf->geo.raid_disks - mddev->degraded);
1866 for (i = 0; i < conf->geo.raid_disks; i++) {
1867 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1868 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1871 seq_printf(seq, "]");
1874 /* check if there are enough drives for
1875 * every block to appear on atleast one.
1876 * Don't consider the device numbered 'ignore'
1877 * as we might be about to remove it.
1879 static int _enough(struct r10conf *conf, int previous, int ignore)
1885 disks = conf->prev.raid_disks;
1886 ncopies = conf->prev.near_copies;
1888 disks = conf->geo.raid_disks;
1889 ncopies = conf->geo.near_copies;
1894 int n = conf->copies;
1898 struct md_rdev *rdev;
1899 if (this != ignore &&
1900 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1901 test_bit(In_sync, &rdev->flags))
1903 this = (this+1) % disks;
1907 first = (first + ncopies) % disks;
1908 } while (first != 0);
1915 static int enough(struct r10conf *conf, int ignore)
1917 /* when calling 'enough', both 'prev' and 'geo' must
1919 * This is ensured if ->reconfig_mutex or ->device_lock
1922 return _enough(conf, 0, ignore) &&
1923 _enough(conf, 1, ignore);
1926 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
1928 char b[BDEVNAME_SIZE];
1929 struct r10conf *conf = mddev->private;
1930 unsigned long flags;
1933 * If it is not operational, then we have already marked it as dead
1934 * else if it is the last working disks with "fail_last_dev == false",
1935 * ignore the error, let the next level up know.
1936 * else mark the drive as failed
1938 spin_lock_irqsave(&conf->device_lock, flags);
1939 if (test_bit(In_sync, &rdev->flags) && !mddev->fail_last_dev
1940 && !enough(conf, rdev->raid_disk)) {
1942 * Don't fail the drive, just return an IO error.
1944 spin_unlock_irqrestore(&conf->device_lock, flags);
1947 if (test_and_clear_bit(In_sync, &rdev->flags))
1950 * If recovery is running, make sure it aborts.
1952 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1953 set_bit(Blocked, &rdev->flags);
1954 set_bit(Faulty, &rdev->flags);
1955 set_mask_bits(&mddev->sb_flags, 0,
1956 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1957 spin_unlock_irqrestore(&conf->device_lock, flags);
1958 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1959 "md/raid10:%s: Operation continuing on %d devices.\n",
1960 mdname(mddev), bdevname(rdev->bdev, b),
1961 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1964 static void print_conf(struct r10conf *conf)
1967 struct md_rdev *rdev;
1969 pr_debug("RAID10 conf printout:\n");
1971 pr_debug("(!conf)\n");
1974 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1975 conf->geo.raid_disks);
1977 /* This is only called with ->reconfix_mutex held, so
1978 * rcu protection of rdev is not needed */
1979 for (i = 0; i < conf->geo.raid_disks; i++) {
1980 char b[BDEVNAME_SIZE];
1981 rdev = conf->mirrors[i].rdev;
1983 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1984 i, !test_bit(In_sync, &rdev->flags),
1985 !test_bit(Faulty, &rdev->flags),
1986 bdevname(rdev->bdev,b));
1990 static void close_sync(struct r10conf *conf)
1993 allow_barrier(conf);
1995 mempool_exit(&conf->r10buf_pool);
1998 static int raid10_spare_active(struct mddev *mddev)
2001 struct r10conf *conf = mddev->private;
2002 struct raid10_info *tmp;
2004 unsigned long flags;
2007 * Find all non-in_sync disks within the RAID10 configuration
2008 * and mark them in_sync
2010 for (i = 0; i < conf->geo.raid_disks; i++) {
2011 tmp = conf->mirrors + i;
2012 if (tmp->replacement
2013 && tmp->replacement->recovery_offset == MaxSector
2014 && !test_bit(Faulty, &tmp->replacement->flags)
2015 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
2016 /* Replacement has just become active */
2018 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
2021 /* Replaced device not technically faulty,
2022 * but we need to be sure it gets removed
2023 * and never re-added.
2025 set_bit(Faulty, &tmp->rdev->flags);
2026 sysfs_notify_dirent_safe(
2027 tmp->rdev->sysfs_state);
2029 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
2030 } else if (tmp->rdev
2031 && tmp->rdev->recovery_offset == MaxSector
2032 && !test_bit(Faulty, &tmp->rdev->flags)
2033 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
2035 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
2038 spin_lock_irqsave(&conf->device_lock, flags);
2039 mddev->degraded -= count;
2040 spin_unlock_irqrestore(&conf->device_lock, flags);
2046 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
2048 struct r10conf *conf = mddev->private;
2052 int last = conf->geo.raid_disks - 1;
2054 if (mddev->recovery_cp < MaxSector)
2055 /* only hot-add to in-sync arrays, as recovery is
2056 * very different from resync
2059 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
2062 if (md_integrity_add_rdev(rdev, mddev))
2065 if (rdev->raid_disk >= 0)
2066 first = last = rdev->raid_disk;
2068 if (rdev->saved_raid_disk >= first &&
2069 rdev->saved_raid_disk < conf->geo.raid_disks &&
2070 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
2071 mirror = rdev->saved_raid_disk;
2074 for ( ; mirror <= last ; mirror++) {
2075 struct raid10_info *p = &conf->mirrors[mirror];
2076 if (p->recovery_disabled == mddev->recovery_disabled)
2079 if (!test_bit(WantReplacement, &p->rdev->flags) ||
2080 p->replacement != NULL)
2082 clear_bit(In_sync, &rdev->flags);
2083 set_bit(Replacement, &rdev->flags);
2084 rdev->raid_disk = mirror;
2087 disk_stack_limits(mddev->gendisk, rdev->bdev,
2088 rdev->data_offset << 9);
2090 rcu_assign_pointer(p->replacement, rdev);
2095 disk_stack_limits(mddev->gendisk, rdev->bdev,
2096 rdev->data_offset << 9);
2098 p->head_position = 0;
2099 p->recovery_disabled = mddev->recovery_disabled - 1;
2100 rdev->raid_disk = mirror;
2102 if (rdev->saved_raid_disk != mirror)
2104 rcu_assign_pointer(p->rdev, rdev);
2107 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
2108 blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue);
2114 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
2116 struct r10conf *conf = mddev->private;
2118 int number = rdev->raid_disk;
2119 struct md_rdev **rdevp;
2120 struct raid10_info *p = conf->mirrors + number;
2123 if (rdev == p->rdev)
2125 else if (rdev == p->replacement)
2126 rdevp = &p->replacement;
2130 if (test_bit(In_sync, &rdev->flags) ||
2131 atomic_read(&rdev->nr_pending)) {
2135 /* Only remove non-faulty devices if recovery
2138 if (!test_bit(Faulty, &rdev->flags) &&
2139 mddev->recovery_disabled != p->recovery_disabled &&
2140 (!p->replacement || p->replacement == rdev) &&
2141 number < conf->geo.raid_disks &&
2147 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
2149 if (atomic_read(&rdev->nr_pending)) {
2150 /* lost the race, try later */
2156 if (p->replacement) {
2157 /* We must have just cleared 'rdev' */
2158 p->rdev = p->replacement;
2159 clear_bit(Replacement, &p->replacement->flags);
2160 smp_mb(); /* Make sure other CPUs may see both as identical
2161 * but will never see neither -- if they are careful.
2163 p->replacement = NULL;
2166 clear_bit(WantReplacement, &rdev->flags);
2167 err = md_integrity_register(mddev);
2175 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
2177 struct r10conf *conf = r10_bio->mddev->private;
2179 if (!bio->bi_status)
2180 set_bit(R10BIO_Uptodate, &r10_bio->state);
2182 /* The write handler will notice the lack of
2183 * R10BIO_Uptodate and record any errors etc
2185 atomic_add(r10_bio->sectors,
2186 &conf->mirrors[d].rdev->corrected_errors);
2188 /* for reconstruct, we always reschedule after a read.
2189 * for resync, only after all reads
2191 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
2192 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
2193 atomic_dec_and_test(&r10_bio->remaining)) {
2194 /* we have read all the blocks,
2195 * do the comparison in process context in raid10d
2197 reschedule_retry(r10_bio);
2201 static void end_sync_read(struct bio *bio)
2203 struct r10bio *r10_bio = get_resync_r10bio(bio);
2204 struct r10conf *conf = r10_bio->mddev->private;
2205 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
2207 __end_sync_read(r10_bio, bio, d);
2210 static void end_reshape_read(struct bio *bio)
2212 /* reshape read bio isn't allocated from r10buf_pool */
2213 struct r10bio *r10_bio = bio->bi_private;
2215 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
2218 static void end_sync_request(struct r10bio *r10_bio)
2220 struct mddev *mddev = r10_bio->mddev;
2222 while (atomic_dec_and_test(&r10_bio->remaining)) {
2223 if (r10_bio->master_bio == NULL) {
2224 /* the primary of several recovery bios */
2225 sector_t s = r10_bio->sectors;
2226 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2227 test_bit(R10BIO_WriteError, &r10_bio->state))
2228 reschedule_retry(r10_bio);
2231 md_done_sync(mddev, s, 1);
2234 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
2235 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2236 test_bit(R10BIO_WriteError, &r10_bio->state))
2237 reschedule_retry(r10_bio);
2245 static void end_sync_write(struct bio *bio)
2247 struct r10bio *r10_bio = get_resync_r10bio(bio);
2248 struct mddev *mddev = r10_bio->mddev;
2249 struct r10conf *conf = mddev->private;
2255 struct md_rdev *rdev = NULL;
2257 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
2259 rdev = conf->mirrors[d].replacement;
2261 rdev = conf->mirrors[d].rdev;
2263 if (bio->bi_status) {
2265 md_error(mddev, rdev);
2267 set_bit(WriteErrorSeen, &rdev->flags);
2268 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2269 set_bit(MD_RECOVERY_NEEDED,
2270 &rdev->mddev->recovery);
2271 set_bit(R10BIO_WriteError, &r10_bio->state);
2273 } else if (is_badblock(rdev,
2274 r10_bio->devs[slot].addr,
2276 &first_bad, &bad_sectors))
2277 set_bit(R10BIO_MadeGood, &r10_bio->state);
2279 rdev_dec_pending(rdev, mddev);
2281 end_sync_request(r10_bio);
2285 * Note: sync and recover and handled very differently for raid10
2286 * This code is for resync.
2287 * For resync, we read through virtual addresses and read all blocks.
2288 * If there is any error, we schedule a write. The lowest numbered
2289 * drive is authoritative.
2290 * However requests come for physical address, so we need to map.
2291 * For every physical address there are raid_disks/copies virtual addresses,
2292 * which is always are least one, but is not necessarly an integer.
2293 * This means that a physical address can span multiple chunks, so we may
2294 * have to submit multiple io requests for a single sync request.
2297 * We check if all blocks are in-sync and only write to blocks that
2300 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2302 struct r10conf *conf = mddev->private;
2304 struct bio *tbio, *fbio;
2306 struct page **tpages, **fpages;
2308 atomic_set(&r10_bio->remaining, 1);
2310 /* find the first device with a block */
2311 for (i=0; i<conf->copies; i++)
2312 if (!r10_bio->devs[i].bio->bi_status)
2315 if (i == conf->copies)
2319 fbio = r10_bio->devs[i].bio;
2320 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2321 fbio->bi_iter.bi_idx = 0;
2322 fpages = get_resync_pages(fbio)->pages;
2324 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2325 /* now find blocks with errors */
2326 for (i=0 ; i < conf->copies ; i++) {
2328 struct md_rdev *rdev;
2329 struct resync_pages *rp;
2331 tbio = r10_bio->devs[i].bio;
2333 if (tbio->bi_end_io != end_sync_read)
2338 tpages = get_resync_pages(tbio)->pages;
2339 d = r10_bio->devs[i].devnum;
2340 rdev = conf->mirrors[d].rdev;
2341 if (!r10_bio->devs[i].bio->bi_status) {
2342 /* We know that the bi_io_vec layout is the same for
2343 * both 'first' and 'i', so we just compare them.
2344 * All vec entries are PAGE_SIZE;
2346 int sectors = r10_bio->sectors;
2347 for (j = 0; j < vcnt; j++) {
2348 int len = PAGE_SIZE;
2349 if (sectors < (len / 512))
2350 len = sectors * 512;
2351 if (memcmp(page_address(fpages[j]),
2352 page_address(tpages[j]),
2359 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2360 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2361 /* Don't fix anything. */
2363 } else if (test_bit(FailFast, &rdev->flags)) {
2364 /* Just give up on this device */
2365 md_error(rdev->mddev, rdev);
2368 /* Ok, we need to write this bio, either to correct an
2369 * inconsistency or to correct an unreadable block.
2370 * First we need to fixup bv_offset, bv_len and
2371 * bi_vecs, as the read request might have corrupted these
2373 rp = get_resync_pages(tbio);
2376 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2378 rp->raid_bio = r10_bio;
2379 tbio->bi_private = rp;
2380 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2381 tbio->bi_end_io = end_sync_write;
2382 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
2384 bio_copy_data(tbio, fbio);
2386 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2387 atomic_inc(&r10_bio->remaining);
2388 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2390 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2391 tbio->bi_opf |= MD_FAILFAST;
2392 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2393 bio_set_dev(tbio, conf->mirrors[d].rdev->bdev);
2394 submit_bio_noacct(tbio);
2397 /* Now write out to any replacement devices
2400 for (i = 0; i < conf->copies; i++) {
2403 tbio = r10_bio->devs[i].repl_bio;
2404 if (!tbio || !tbio->bi_end_io)
2406 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2407 && r10_bio->devs[i].bio != fbio)
2408 bio_copy_data(tbio, fbio);
2409 d = r10_bio->devs[i].devnum;
2410 atomic_inc(&r10_bio->remaining);
2411 md_sync_acct(conf->mirrors[d].replacement->bdev,
2413 submit_bio_noacct(tbio);
2417 if (atomic_dec_and_test(&r10_bio->remaining)) {
2418 md_done_sync(mddev, r10_bio->sectors, 1);
2424 * Now for the recovery code.
2425 * Recovery happens across physical sectors.
2426 * We recover all non-is_sync drives by finding the virtual address of
2427 * each, and then choose a working drive that also has that virt address.
2428 * There is a separate r10_bio for each non-in_sync drive.
2429 * Only the first two slots are in use. The first for reading,
2430 * The second for writing.
2433 static void fix_recovery_read_error(struct r10bio *r10_bio)
2435 /* We got a read error during recovery.
2436 * We repeat the read in smaller page-sized sections.
2437 * If a read succeeds, write it to the new device or record
2438 * a bad block if we cannot.
2439 * If a read fails, record a bad block on both old and
2442 struct mddev *mddev = r10_bio->mddev;
2443 struct r10conf *conf = mddev->private;
2444 struct bio *bio = r10_bio->devs[0].bio;
2446 int sectors = r10_bio->sectors;
2448 int dr = r10_bio->devs[0].devnum;
2449 int dw = r10_bio->devs[1].devnum;
2450 struct page **pages = get_resync_pages(bio)->pages;
2454 struct md_rdev *rdev;
2458 if (s > (PAGE_SIZE>>9))
2461 rdev = conf->mirrors[dr].rdev;
2462 addr = r10_bio->devs[0].addr + sect,
2463 ok = sync_page_io(rdev,
2467 REQ_OP_READ, 0, false);
2469 rdev = conf->mirrors[dw].rdev;
2470 addr = r10_bio->devs[1].addr + sect;
2471 ok = sync_page_io(rdev,
2475 REQ_OP_WRITE, 0, false);
2477 set_bit(WriteErrorSeen, &rdev->flags);
2478 if (!test_and_set_bit(WantReplacement,
2480 set_bit(MD_RECOVERY_NEEDED,
2481 &rdev->mddev->recovery);
2485 /* We don't worry if we cannot set a bad block -
2486 * it really is bad so there is no loss in not
2489 rdev_set_badblocks(rdev, addr, s, 0);
2491 if (rdev != conf->mirrors[dw].rdev) {
2492 /* need bad block on destination too */
2493 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2494 addr = r10_bio->devs[1].addr + sect;
2495 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2497 /* just abort the recovery */
2498 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2501 conf->mirrors[dw].recovery_disabled
2502 = mddev->recovery_disabled;
2503 set_bit(MD_RECOVERY_INTR,
2516 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2518 struct r10conf *conf = mddev->private;
2520 struct bio *wbio, *wbio2;
2522 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2523 fix_recovery_read_error(r10_bio);
2524 end_sync_request(r10_bio);
2529 * share the pages with the first bio
2530 * and submit the write request
2532 d = r10_bio->devs[1].devnum;
2533 wbio = r10_bio->devs[1].bio;
2534 wbio2 = r10_bio->devs[1].repl_bio;
2535 /* Need to test wbio2->bi_end_io before we call
2536 * submit_bio_noacct as if the former is NULL,
2537 * the latter is free to free wbio2.
2539 if (wbio2 && !wbio2->bi_end_io)
2541 if (wbio->bi_end_io) {
2542 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2543 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2544 submit_bio_noacct(wbio);
2547 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2548 md_sync_acct(conf->mirrors[d].replacement->bdev,
2549 bio_sectors(wbio2));
2550 submit_bio_noacct(wbio2);
2555 * Used by fix_read_error() to decay the per rdev read_errors.
2556 * We halve the read error count for every hour that has elapsed
2557 * since the last recorded read error.
2560 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2563 unsigned long hours_since_last;
2564 unsigned int read_errors = atomic_read(&rdev->read_errors);
2566 cur_time_mon = ktime_get_seconds();
2568 if (rdev->last_read_error == 0) {
2569 /* first time we've seen a read error */
2570 rdev->last_read_error = cur_time_mon;
2574 hours_since_last = (long)(cur_time_mon -
2575 rdev->last_read_error) / 3600;
2577 rdev->last_read_error = cur_time_mon;
2580 * if hours_since_last is > the number of bits in read_errors
2581 * just set read errors to 0. We do this to avoid
2582 * overflowing the shift of read_errors by hours_since_last.
2584 if (hours_since_last >= 8 * sizeof(read_errors))
2585 atomic_set(&rdev->read_errors, 0);
2587 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2590 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2591 int sectors, struct page *page, int rw)
2596 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2597 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2599 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
2603 set_bit(WriteErrorSeen, &rdev->flags);
2604 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2605 set_bit(MD_RECOVERY_NEEDED,
2606 &rdev->mddev->recovery);
2608 /* need to record an error - either for the block or the device */
2609 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2610 md_error(rdev->mddev, rdev);
2615 * This is a kernel thread which:
2617 * 1. Retries failed read operations on working mirrors.
2618 * 2. Updates the raid superblock when problems encounter.
2619 * 3. Performs writes following reads for array synchronising.
2622 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2624 int sect = 0; /* Offset from r10_bio->sector */
2625 int sectors = r10_bio->sectors;
2626 struct md_rdev *rdev;
2627 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2628 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2630 /* still own a reference to this rdev, so it cannot
2631 * have been cleared recently.
2633 rdev = conf->mirrors[d].rdev;
2635 if (test_bit(Faulty, &rdev->flags))
2636 /* drive has already been failed, just ignore any
2637 more fix_read_error() attempts */
2640 check_decay_read_errors(mddev, rdev);
2641 atomic_inc(&rdev->read_errors);
2642 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2643 char b[BDEVNAME_SIZE];
2644 bdevname(rdev->bdev, b);
2646 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2648 atomic_read(&rdev->read_errors), max_read_errors);
2649 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2651 md_error(mddev, rdev);
2652 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2658 int sl = r10_bio->read_slot;
2662 if (s > (PAGE_SIZE>>9))
2670 d = r10_bio->devs[sl].devnum;
2671 rdev = rcu_dereference(conf->mirrors[d].rdev);
2673 test_bit(In_sync, &rdev->flags) &&
2674 !test_bit(Faulty, &rdev->flags) &&
2675 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2676 &first_bad, &bad_sectors) == 0) {
2677 atomic_inc(&rdev->nr_pending);
2679 success = sync_page_io(rdev,
2680 r10_bio->devs[sl].addr +
2684 REQ_OP_READ, 0, false);
2685 rdev_dec_pending(rdev, mddev);
2691 if (sl == conf->copies)
2693 } while (!success && sl != r10_bio->read_slot);
2697 /* Cannot read from anywhere, just mark the block
2698 * as bad on the first device to discourage future
2701 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2702 rdev = conf->mirrors[dn].rdev;
2704 if (!rdev_set_badblocks(
2706 r10_bio->devs[r10_bio->read_slot].addr
2709 md_error(mddev, rdev);
2710 r10_bio->devs[r10_bio->read_slot].bio
2717 /* write it back and re-read */
2719 while (sl != r10_bio->read_slot) {
2720 char b[BDEVNAME_SIZE];
2725 d = r10_bio->devs[sl].devnum;
2726 rdev = rcu_dereference(conf->mirrors[d].rdev);
2728 test_bit(Faulty, &rdev->flags) ||
2729 !test_bit(In_sync, &rdev->flags))
2732 atomic_inc(&rdev->nr_pending);
2734 if (r10_sync_page_io(rdev,
2735 r10_bio->devs[sl].addr +
2737 s, conf->tmppage, WRITE)
2739 /* Well, this device is dead */
2740 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2742 (unsigned long long)(
2744 choose_data_offset(r10_bio,
2746 bdevname(rdev->bdev, b));
2747 pr_notice("md/raid10:%s: %s: failing drive\n",
2749 bdevname(rdev->bdev, b));
2751 rdev_dec_pending(rdev, mddev);
2755 while (sl != r10_bio->read_slot) {
2756 char b[BDEVNAME_SIZE];
2761 d = r10_bio->devs[sl].devnum;
2762 rdev = rcu_dereference(conf->mirrors[d].rdev);
2764 test_bit(Faulty, &rdev->flags) ||
2765 !test_bit(In_sync, &rdev->flags))
2768 atomic_inc(&rdev->nr_pending);
2770 switch (r10_sync_page_io(rdev,
2771 r10_bio->devs[sl].addr +
2776 /* Well, this device is dead */
2777 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2779 (unsigned long long)(
2781 choose_data_offset(r10_bio, rdev)),
2782 bdevname(rdev->bdev, b));
2783 pr_notice("md/raid10:%s: %s: failing drive\n",
2785 bdevname(rdev->bdev, b));
2788 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2790 (unsigned long long)(
2792 choose_data_offset(r10_bio, rdev)),
2793 bdevname(rdev->bdev, b));
2794 atomic_add(s, &rdev->corrected_errors);
2797 rdev_dec_pending(rdev, mddev);
2807 static int narrow_write_error(struct r10bio *r10_bio, int i)
2809 struct bio *bio = r10_bio->master_bio;
2810 struct mddev *mddev = r10_bio->mddev;
2811 struct r10conf *conf = mddev->private;
2812 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2813 /* bio has the data to be written to slot 'i' where
2814 * we just recently had a write error.
2815 * We repeatedly clone the bio and trim down to one block,
2816 * then try the write. Where the write fails we record
2818 * It is conceivable that the bio doesn't exactly align with
2819 * blocks. We must handle this.
2821 * We currently own a reference to the rdev.
2827 int sect_to_write = r10_bio->sectors;
2830 if (rdev->badblocks.shift < 0)
2833 block_sectors = roundup(1 << rdev->badblocks.shift,
2834 bdev_logical_block_size(rdev->bdev) >> 9);
2835 sector = r10_bio->sector;
2836 sectors = ((r10_bio->sector + block_sectors)
2837 & ~(sector_t)(block_sectors - 1))
2840 while (sect_to_write) {
2843 if (sectors > sect_to_write)
2844 sectors = sect_to_write;
2845 /* Write at 'sector' for 'sectors' */
2846 wbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set);
2847 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2848 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2849 wbio->bi_iter.bi_sector = wsector +
2850 choose_data_offset(r10_bio, rdev);
2851 bio_set_dev(wbio, rdev->bdev);
2852 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2854 if (submit_bio_wait(wbio) < 0)
2856 ok = rdev_set_badblocks(rdev, wsector,
2861 sect_to_write -= sectors;
2863 sectors = block_sectors;
2868 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2870 int slot = r10_bio->read_slot;
2872 struct r10conf *conf = mddev->private;
2873 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2875 /* we got a read error. Maybe the drive is bad. Maybe just
2876 * the block and we can fix it.
2877 * We freeze all other IO, and try reading the block from
2878 * other devices. When we find one, we re-write
2879 * and check it that fixes the read error.
2880 * This is all done synchronously while the array is
2883 bio = r10_bio->devs[slot].bio;
2885 r10_bio->devs[slot].bio = NULL;
2888 r10_bio->devs[slot].bio = IO_BLOCKED;
2889 else if (!test_bit(FailFast, &rdev->flags)) {
2890 freeze_array(conf, 1);
2891 fix_read_error(conf, mddev, r10_bio);
2892 unfreeze_array(conf);
2894 md_error(mddev, rdev);
2896 rdev_dec_pending(rdev, mddev);
2897 allow_barrier(conf);
2899 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
2902 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2904 /* Some sort of write request has finished and it
2905 * succeeded in writing where we thought there was a
2906 * bad block. So forget the bad block.
2907 * Or possibly if failed and we need to record
2911 struct md_rdev *rdev;
2913 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2914 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2915 for (m = 0; m < conf->copies; m++) {
2916 int dev = r10_bio->devs[m].devnum;
2917 rdev = conf->mirrors[dev].rdev;
2918 if (r10_bio->devs[m].bio == NULL ||
2919 r10_bio->devs[m].bio->bi_end_io == NULL)
2921 if (!r10_bio->devs[m].bio->bi_status) {
2922 rdev_clear_badblocks(
2924 r10_bio->devs[m].addr,
2925 r10_bio->sectors, 0);
2927 if (!rdev_set_badblocks(
2929 r10_bio->devs[m].addr,
2930 r10_bio->sectors, 0))
2931 md_error(conf->mddev, rdev);
2933 rdev = conf->mirrors[dev].replacement;
2934 if (r10_bio->devs[m].repl_bio == NULL ||
2935 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
2938 if (!r10_bio->devs[m].repl_bio->bi_status) {
2939 rdev_clear_badblocks(
2941 r10_bio->devs[m].addr,
2942 r10_bio->sectors, 0);
2944 if (!rdev_set_badblocks(
2946 r10_bio->devs[m].addr,
2947 r10_bio->sectors, 0))
2948 md_error(conf->mddev, rdev);
2954 for (m = 0; m < conf->copies; m++) {
2955 int dev = r10_bio->devs[m].devnum;
2956 struct bio *bio = r10_bio->devs[m].bio;
2957 rdev = conf->mirrors[dev].rdev;
2958 if (bio == IO_MADE_GOOD) {
2959 rdev_clear_badblocks(
2961 r10_bio->devs[m].addr,
2962 r10_bio->sectors, 0);
2963 rdev_dec_pending(rdev, conf->mddev);
2964 } else if (bio != NULL && bio->bi_status) {
2966 if (!narrow_write_error(r10_bio, m)) {
2967 md_error(conf->mddev, rdev);
2968 set_bit(R10BIO_Degraded,
2971 rdev_dec_pending(rdev, conf->mddev);
2973 bio = r10_bio->devs[m].repl_bio;
2974 rdev = conf->mirrors[dev].replacement;
2975 if (rdev && bio == IO_MADE_GOOD) {
2976 rdev_clear_badblocks(
2978 r10_bio->devs[m].addr,
2979 r10_bio->sectors, 0);
2980 rdev_dec_pending(rdev, conf->mddev);
2984 spin_lock_irq(&conf->device_lock);
2985 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
2987 spin_unlock_irq(&conf->device_lock);
2989 * In case freeze_array() is waiting for condition
2990 * nr_pending == nr_queued + extra to be true.
2992 wake_up(&conf->wait_barrier);
2993 md_wakeup_thread(conf->mddev->thread);
2995 if (test_bit(R10BIO_WriteError,
2997 close_write(r10_bio);
2998 raid_end_bio_io(r10_bio);
3003 static void raid10d(struct md_thread *thread)
3005 struct mddev *mddev = thread->mddev;
3006 struct r10bio *r10_bio;
3007 unsigned long flags;
3008 struct r10conf *conf = mddev->private;
3009 struct list_head *head = &conf->retry_list;
3010 struct blk_plug plug;
3012 md_check_recovery(mddev);
3014 if (!list_empty_careful(&conf->bio_end_io_list) &&
3015 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
3017 spin_lock_irqsave(&conf->device_lock, flags);
3018 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
3019 while (!list_empty(&conf->bio_end_io_list)) {
3020 list_move(conf->bio_end_io_list.prev, &tmp);
3024 spin_unlock_irqrestore(&conf->device_lock, flags);
3025 while (!list_empty(&tmp)) {
3026 r10_bio = list_first_entry(&tmp, struct r10bio,
3028 list_del(&r10_bio->retry_list);
3029 if (mddev->degraded)
3030 set_bit(R10BIO_Degraded, &r10_bio->state);
3032 if (test_bit(R10BIO_WriteError,
3034 close_write(r10_bio);
3035 raid_end_bio_io(r10_bio);
3039 blk_start_plug(&plug);
3042 flush_pending_writes(conf);
3044 spin_lock_irqsave(&conf->device_lock, flags);
3045 if (list_empty(head)) {
3046 spin_unlock_irqrestore(&conf->device_lock, flags);
3049 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
3050 list_del(head->prev);
3052 spin_unlock_irqrestore(&conf->device_lock, flags);
3054 mddev = r10_bio->mddev;
3055 conf = mddev->private;
3056 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
3057 test_bit(R10BIO_WriteError, &r10_bio->state))
3058 handle_write_completed(conf, r10_bio);
3059 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
3060 reshape_request_write(mddev, r10_bio);
3061 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
3062 sync_request_write(mddev, r10_bio);
3063 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
3064 recovery_request_write(mddev, r10_bio);
3065 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
3066 handle_read_error(mddev, r10_bio);
3071 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
3072 md_check_recovery(mddev);
3074 blk_finish_plug(&plug);
3077 static int init_resync(struct r10conf *conf)
3081 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
3082 BUG_ON(mempool_initialized(&conf->r10buf_pool));
3083 conf->have_replacement = 0;
3084 for (i = 0; i < conf->geo.raid_disks; i++)
3085 if (conf->mirrors[i].replacement)
3086 conf->have_replacement = 1;
3087 ret = mempool_init(&conf->r10buf_pool, buffs,
3088 r10buf_pool_alloc, r10buf_pool_free, conf);
3091 conf->next_resync = 0;
3095 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
3097 struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO);
3098 struct rsync_pages *rp;
3103 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
3104 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
3105 nalloc = conf->copies; /* resync */
3107 nalloc = 2; /* recovery */
3109 for (i = 0; i < nalloc; i++) {
3110 bio = r10bio->devs[i].bio;
3111 rp = bio->bi_private;
3113 bio->bi_private = rp;
3114 bio = r10bio->devs[i].repl_bio;
3116 rp = bio->bi_private;
3118 bio->bi_private = rp;
3125 * Set cluster_sync_high since we need other nodes to add the
3126 * range [cluster_sync_low, cluster_sync_high] to suspend list.
3128 static void raid10_set_cluster_sync_high(struct r10conf *conf)
3130 sector_t window_size;
3131 int extra_chunk, chunks;
3134 * First, here we define "stripe" as a unit which across
3135 * all member devices one time, so we get chunks by use
3136 * raid_disks / near_copies. Otherwise, if near_copies is
3137 * close to raid_disks, then resync window could increases
3138 * linearly with the increase of raid_disks, which means
3139 * we will suspend a really large IO window while it is not
3140 * necessary. If raid_disks is not divisible by near_copies,
3141 * an extra chunk is needed to ensure the whole "stripe" is
3145 chunks = conf->geo.raid_disks / conf->geo.near_copies;
3146 if (conf->geo.raid_disks % conf->geo.near_copies == 0)
3150 window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors;
3153 * At least use a 32M window to align with raid1's resync window
3155 window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ?
3156 CLUSTER_RESYNC_WINDOW_SECTORS : window_size;
3158 conf->cluster_sync_high = conf->cluster_sync_low + window_size;
3162 * perform a "sync" on one "block"
3164 * We need to make sure that no normal I/O request - particularly write
3165 * requests - conflict with active sync requests.
3167 * This is achieved by tracking pending requests and a 'barrier' concept
3168 * that can be installed to exclude normal IO requests.
3170 * Resync and recovery are handled very differently.
3171 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
3173 * For resync, we iterate over virtual addresses, read all copies,
3174 * and update if there are differences. If only one copy is live,
3176 * For recovery, we iterate over physical addresses, read a good
3177 * value for each non-in_sync drive, and over-write.
3179 * So, for recovery we may have several outstanding complex requests for a
3180 * given address, one for each out-of-sync device. We model this by allocating
3181 * a number of r10_bio structures, one for each out-of-sync device.
3182 * As we setup these structures, we collect all bio's together into a list
3183 * which we then process collectively to add pages, and then process again
3184 * to pass to submit_bio_noacct.
3186 * The r10_bio structures are linked using a borrowed master_bio pointer.
3187 * This link is counted in ->remaining. When the r10_bio that points to NULL
3188 * has its remaining count decremented to 0, the whole complex operation
3193 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
3196 struct r10conf *conf = mddev->private;
3197 struct r10bio *r10_bio;
3198 struct bio *biolist = NULL, *bio;
3199 sector_t max_sector, nr_sectors;
3202 sector_t sync_blocks;
3203 sector_t sectors_skipped = 0;
3204 int chunks_skipped = 0;
3205 sector_t chunk_mask = conf->geo.chunk_mask;
3208 if (!mempool_initialized(&conf->r10buf_pool))
3209 if (init_resync(conf))
3213 * Allow skipping a full rebuild for incremental assembly
3214 * of a clean array, like RAID1 does.
3216 if (mddev->bitmap == NULL &&
3217 mddev->recovery_cp == MaxSector &&
3218 mddev->reshape_position == MaxSector &&
3219 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
3220 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
3221 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
3222 conf->fullsync == 0) {
3224 return mddev->dev_sectors - sector_nr;
3228 max_sector = mddev->dev_sectors;
3229 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
3230 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3231 max_sector = mddev->resync_max_sectors;
3232 if (sector_nr >= max_sector) {
3233 conf->cluster_sync_low = 0;
3234 conf->cluster_sync_high = 0;
3236 /* If we aborted, we need to abort the
3237 * sync on the 'current' bitmap chucks (there can
3238 * be several when recovering multiple devices).
3239 * as we may have started syncing it but not finished.
3240 * We can find the current address in
3241 * mddev->curr_resync, but for recovery,
3242 * we need to convert that to several
3243 * virtual addresses.
3245 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3251 if (mddev->curr_resync < max_sector) { /* aborted */
3252 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
3253 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3255 else for (i = 0; i < conf->geo.raid_disks; i++) {
3257 raid10_find_virt(conf, mddev->curr_resync, i);
3258 md_bitmap_end_sync(mddev->bitmap, sect,
3262 /* completed sync */
3263 if ((!mddev->bitmap || conf->fullsync)
3264 && conf->have_replacement
3265 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3266 /* Completed a full sync so the replacements
3267 * are now fully recovered.
3270 for (i = 0; i < conf->geo.raid_disks; i++) {
3271 struct md_rdev *rdev =
3272 rcu_dereference(conf->mirrors[i].replacement);
3274 rdev->recovery_offset = MaxSector;
3280 md_bitmap_close_sync(mddev->bitmap);
3283 return sectors_skipped;
3286 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3287 return reshape_request(mddev, sector_nr, skipped);
3289 if (chunks_skipped >= conf->geo.raid_disks) {
3290 /* if there has been nothing to do on any drive,
3291 * then there is nothing to do at all..
3294 return (max_sector - sector_nr) + sectors_skipped;
3297 if (max_sector > mddev->resync_max)
3298 max_sector = mddev->resync_max; /* Don't do IO beyond here */
3300 /* make sure whole request will fit in a chunk - if chunks
3303 if (conf->geo.near_copies < conf->geo.raid_disks &&
3304 max_sector > (sector_nr | chunk_mask))
3305 max_sector = (sector_nr | chunk_mask) + 1;
3308 * If there is non-resync activity waiting for a turn, then let it
3309 * though before starting on this new sync request.
3311 if (conf->nr_waiting)
3312 schedule_timeout_uninterruptible(1);
3314 /* Again, very different code for resync and recovery.
3315 * Both must result in an r10bio with a list of bios that
3316 * have bi_end_io, bi_sector, bi_disk set,
3317 * and bi_private set to the r10bio.
3318 * For recovery, we may actually create several r10bios
3319 * with 2 bios in each, that correspond to the bios in the main one.
3320 * In this case, the subordinate r10bios link back through a
3321 * borrowed master_bio pointer, and the counter in the master
3322 * includes a ref from each subordinate.
3324 /* First, we decide what to do and set ->bi_end_io
3325 * To end_sync_read if we want to read, and
3326 * end_sync_write if we will want to write.
3329 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
3330 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3331 /* recovery... the complicated one */
3335 for (i = 0 ; i < conf->geo.raid_disks; i++) {
3341 int need_recover = 0;
3342 int need_replace = 0;
3343 struct raid10_info *mirror = &conf->mirrors[i];
3344 struct md_rdev *mrdev, *mreplace;
3347 mrdev = rcu_dereference(mirror->rdev);
3348 mreplace = rcu_dereference(mirror->replacement);
3350 if (mrdev != NULL &&
3351 !test_bit(Faulty, &mrdev->flags) &&
3352 !test_bit(In_sync, &mrdev->flags))
3354 if (mreplace != NULL &&
3355 !test_bit(Faulty, &mreplace->flags))
3358 if (!need_recover && !need_replace) {
3364 /* want to reconstruct this device */
3366 sect = raid10_find_virt(conf, sector_nr, i);
3367 if (sect >= mddev->resync_max_sectors) {
3368 /* last stripe is not complete - don't
3369 * try to recover this sector.
3374 if (mreplace && test_bit(Faulty, &mreplace->flags))
3376 /* Unless we are doing a full sync, or a replacement
3377 * we only need to recover the block if it is set in
3380 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3382 if (sync_blocks < max_sync)
3383 max_sync = sync_blocks;
3387 /* yep, skip the sync_blocks here, but don't assume
3388 * that there will never be anything to do here
3390 chunks_skipped = -1;
3394 atomic_inc(&mrdev->nr_pending);
3396 atomic_inc(&mreplace->nr_pending);
3399 r10_bio = raid10_alloc_init_r10buf(conf);
3401 raise_barrier(conf, rb2 != NULL);
3402 atomic_set(&r10_bio->remaining, 0);
3404 r10_bio->master_bio = (struct bio*)rb2;
3406 atomic_inc(&rb2->remaining);
3407 r10_bio->mddev = mddev;
3408 set_bit(R10BIO_IsRecover, &r10_bio->state);
3409 r10_bio->sector = sect;
3411 raid10_find_phys(conf, r10_bio);
3413 /* Need to check if the array will still be
3417 for (j = 0; j < conf->geo.raid_disks; j++) {
3418 struct md_rdev *rdev = rcu_dereference(
3419 conf->mirrors[j].rdev);
3420 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3426 must_sync = md_bitmap_start_sync(mddev->bitmap, sect,
3427 &sync_blocks, still_degraded);
3430 for (j=0; j<conf->copies;j++) {
3432 int d = r10_bio->devs[j].devnum;
3433 sector_t from_addr, to_addr;
3434 struct md_rdev *rdev =
3435 rcu_dereference(conf->mirrors[d].rdev);
3436 sector_t sector, first_bad;
3439 !test_bit(In_sync, &rdev->flags))
3441 /* This is where we read from */
3443 sector = r10_bio->devs[j].addr;
3445 if (is_badblock(rdev, sector, max_sync,
3446 &first_bad, &bad_sectors)) {
3447 if (first_bad > sector)
3448 max_sync = first_bad - sector;
3450 bad_sectors -= (sector
3452 if (max_sync > bad_sectors)
3453 max_sync = bad_sectors;
3457 bio = r10_bio->devs[0].bio;
3458 bio->bi_next = biolist;
3460 bio->bi_end_io = end_sync_read;
3461 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3462 if (test_bit(FailFast, &rdev->flags))
3463 bio->bi_opf |= MD_FAILFAST;
3464 from_addr = r10_bio->devs[j].addr;
3465 bio->bi_iter.bi_sector = from_addr +
3467 bio_set_dev(bio, rdev->bdev);
3468 atomic_inc(&rdev->nr_pending);
3469 /* and we write to 'i' (if not in_sync) */
3471 for (k=0; k<conf->copies; k++)
3472 if (r10_bio->devs[k].devnum == i)
3474 BUG_ON(k == conf->copies);
3475 to_addr = r10_bio->devs[k].addr;
3476 r10_bio->devs[0].devnum = d;
3477 r10_bio->devs[0].addr = from_addr;
3478 r10_bio->devs[1].devnum = i;
3479 r10_bio->devs[1].addr = to_addr;
3482 bio = r10_bio->devs[1].bio;
3483 bio->bi_next = biolist;
3485 bio->bi_end_io = end_sync_write;
3486 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3487 bio->bi_iter.bi_sector = to_addr
3488 + mrdev->data_offset;
3489 bio_set_dev(bio, mrdev->bdev);
3490 atomic_inc(&r10_bio->remaining);
3492 r10_bio->devs[1].bio->bi_end_io = NULL;
3494 /* and maybe write to replacement */
3495 bio = r10_bio->devs[1].repl_bio;
3497 bio->bi_end_io = NULL;
3498 /* Note: if need_replace, then bio
3499 * cannot be NULL as r10buf_pool_alloc will
3500 * have allocated it.
3504 bio->bi_next = biolist;
3506 bio->bi_end_io = end_sync_write;
3507 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3508 bio->bi_iter.bi_sector = to_addr +
3509 mreplace->data_offset;
3510 bio_set_dev(bio, mreplace->bdev);
3511 atomic_inc(&r10_bio->remaining);
3515 if (j == conf->copies) {
3516 /* Cannot recover, so abort the recovery or
3517 * record a bad block */
3519 /* problem is that there are bad blocks
3520 * on other device(s)
3523 for (k = 0; k < conf->copies; k++)
3524 if (r10_bio->devs[k].devnum == i)
3526 if (!test_bit(In_sync,
3528 && !rdev_set_badblocks(
3530 r10_bio->devs[k].addr,
3534 !rdev_set_badblocks(
3536 r10_bio->devs[k].addr,
3541 if (!test_and_set_bit(MD_RECOVERY_INTR,
3543 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3545 mirror->recovery_disabled
3546 = mddev->recovery_disabled;
3550 atomic_dec(&rb2->remaining);
3552 rdev_dec_pending(mrdev, mddev);
3554 rdev_dec_pending(mreplace, mddev);
3557 rdev_dec_pending(mrdev, mddev);
3559 rdev_dec_pending(mreplace, mddev);
3560 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3561 /* Only want this if there is elsewhere to
3562 * read from. 'j' is currently the first
3566 for (; j < conf->copies; j++) {
3567 int d = r10_bio->devs[j].devnum;
3568 if (conf->mirrors[d].rdev &&
3570 &conf->mirrors[d].rdev->flags))
3574 r10_bio->devs[0].bio->bi_opf
3578 if (biolist == NULL) {
3580 struct r10bio *rb2 = r10_bio;
3581 r10_bio = (struct r10bio*) rb2->master_bio;
3582 rb2->master_bio = NULL;
3588 /* resync. Schedule a read for every block at this virt offset */
3592 * Since curr_resync_completed could probably not update in
3593 * time, and we will set cluster_sync_low based on it.
3594 * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for
3595 * safety reason, which ensures curr_resync_completed is
3596 * updated in bitmap_cond_end_sync.
3598 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr,
3599 mddev_is_clustered(mddev) &&
3600 (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high));
3602 if (!md_bitmap_start_sync(mddev->bitmap, sector_nr,
3603 &sync_blocks, mddev->degraded) &&
3604 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3605 &mddev->recovery)) {
3606 /* We can skip this block */
3608 return sync_blocks + sectors_skipped;
3610 if (sync_blocks < max_sync)
3611 max_sync = sync_blocks;
3612 r10_bio = raid10_alloc_init_r10buf(conf);
3615 r10_bio->mddev = mddev;
3616 atomic_set(&r10_bio->remaining, 0);
3617 raise_barrier(conf, 0);
3618 conf->next_resync = sector_nr;
3620 r10_bio->master_bio = NULL;
3621 r10_bio->sector = sector_nr;
3622 set_bit(R10BIO_IsSync, &r10_bio->state);
3623 raid10_find_phys(conf, r10_bio);
3624 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3626 for (i = 0; i < conf->copies; i++) {
3627 int d = r10_bio->devs[i].devnum;
3628 sector_t first_bad, sector;
3630 struct md_rdev *rdev;
3632 if (r10_bio->devs[i].repl_bio)
3633 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3635 bio = r10_bio->devs[i].bio;
3636 bio->bi_status = BLK_STS_IOERR;
3638 rdev = rcu_dereference(conf->mirrors[d].rdev);
3639 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3643 sector = r10_bio->devs[i].addr;
3644 if (is_badblock(rdev, sector, max_sync,
3645 &first_bad, &bad_sectors)) {
3646 if (first_bad > sector)
3647 max_sync = first_bad - sector;
3649 bad_sectors -= (sector - first_bad);
3650 if (max_sync > bad_sectors)
3651 max_sync = bad_sectors;
3656 atomic_inc(&rdev->nr_pending);
3657 atomic_inc(&r10_bio->remaining);
3658 bio->bi_next = biolist;
3660 bio->bi_end_io = end_sync_read;
3661 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3662 if (test_bit(FailFast, &rdev->flags))
3663 bio->bi_opf |= MD_FAILFAST;
3664 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3665 bio_set_dev(bio, rdev->bdev);
3668 rdev = rcu_dereference(conf->mirrors[d].replacement);
3669 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3673 atomic_inc(&rdev->nr_pending);
3675 /* Need to set up for writing to the replacement */
3676 bio = r10_bio->devs[i].repl_bio;
3677 bio->bi_status = BLK_STS_IOERR;
3679 sector = r10_bio->devs[i].addr;
3680 bio->bi_next = biolist;
3682 bio->bi_end_io = end_sync_write;
3683 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3684 if (test_bit(FailFast, &rdev->flags))
3685 bio->bi_opf |= MD_FAILFAST;
3686 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3687 bio_set_dev(bio, rdev->bdev);
3693 for (i=0; i<conf->copies; i++) {
3694 int d = r10_bio->devs[i].devnum;
3695 if (r10_bio->devs[i].bio->bi_end_io)
3696 rdev_dec_pending(conf->mirrors[d].rdev,
3698 if (r10_bio->devs[i].repl_bio &&
3699 r10_bio->devs[i].repl_bio->bi_end_io)
3701 conf->mirrors[d].replacement,
3711 if (sector_nr + max_sync < max_sector)
3712 max_sector = sector_nr + max_sync;
3715 int len = PAGE_SIZE;
3716 if (sector_nr + (len>>9) > max_sector)
3717 len = (max_sector - sector_nr) << 9;
3720 for (bio= biolist ; bio ; bio=bio->bi_next) {
3721 struct resync_pages *rp = get_resync_pages(bio);
3722 page = resync_fetch_page(rp, page_idx);
3724 * won't fail because the vec table is big enough
3725 * to hold all these pages
3727 bio_add_page(bio, page, len, 0);
3729 nr_sectors += len>>9;
3730 sector_nr += len>>9;
3731 } while (++page_idx < RESYNC_PAGES);
3732 r10_bio->sectors = nr_sectors;
3734 if (mddev_is_clustered(mddev) &&
3735 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3736 /* It is resync not recovery */
3737 if (conf->cluster_sync_high < sector_nr + nr_sectors) {
3738 conf->cluster_sync_low = mddev->curr_resync_completed;
3739 raid10_set_cluster_sync_high(conf);
3740 /* Send resync message */
3741 md_cluster_ops->resync_info_update(mddev,
3742 conf->cluster_sync_low,
3743 conf->cluster_sync_high);
3745 } else if (mddev_is_clustered(mddev)) {
3746 /* This is recovery not resync */
3747 sector_t sect_va1, sect_va2;
3748 bool broadcast_msg = false;
3750 for (i = 0; i < conf->geo.raid_disks; i++) {
3752 * sector_nr is a device address for recovery, so we
3753 * need translate it to array address before compare
3754 * with cluster_sync_high.
3756 sect_va1 = raid10_find_virt(conf, sector_nr, i);
3758 if (conf->cluster_sync_high < sect_va1 + nr_sectors) {
3759 broadcast_msg = true;
3761 * curr_resync_completed is similar as
3762 * sector_nr, so make the translation too.
3764 sect_va2 = raid10_find_virt(conf,
3765 mddev->curr_resync_completed, i);
3767 if (conf->cluster_sync_low == 0 ||
3768 conf->cluster_sync_low > sect_va2)
3769 conf->cluster_sync_low = sect_va2;
3772 if (broadcast_msg) {
3773 raid10_set_cluster_sync_high(conf);
3774 md_cluster_ops->resync_info_update(mddev,
3775 conf->cluster_sync_low,
3776 conf->cluster_sync_high);
3782 biolist = biolist->bi_next;
3784 bio->bi_next = NULL;
3785 r10_bio = get_resync_r10bio(bio);
3786 r10_bio->sectors = nr_sectors;
3788 if (bio->bi_end_io == end_sync_read) {
3789 md_sync_acct_bio(bio, nr_sectors);
3791 submit_bio_noacct(bio);
3795 if (sectors_skipped)
3796 /* pretend they weren't skipped, it makes
3797 * no important difference in this case
3799 md_done_sync(mddev, sectors_skipped, 1);
3801 return sectors_skipped + nr_sectors;
3803 /* There is nowhere to write, so all non-sync
3804 * drives must be failed or in resync, all drives
3805 * have a bad block, so try the next chunk...
3807 if (sector_nr + max_sync < max_sector)
3808 max_sector = sector_nr + max_sync;
3810 sectors_skipped += (max_sector - sector_nr);
3812 sector_nr = max_sector;
3817 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3820 struct r10conf *conf = mddev->private;
3823 raid_disks = min(conf->geo.raid_disks,
3824 conf->prev.raid_disks);
3826 sectors = conf->dev_sectors;
3828 size = sectors >> conf->geo.chunk_shift;
3829 sector_div(size, conf->geo.far_copies);
3830 size = size * raid_disks;
3831 sector_div(size, conf->geo.near_copies);
3833 return size << conf->geo.chunk_shift;
3836 static void calc_sectors(struct r10conf *conf, sector_t size)
3838 /* Calculate the number of sectors-per-device that will
3839 * actually be used, and set conf->dev_sectors and
3843 size = size >> conf->geo.chunk_shift;
3844 sector_div(size, conf->geo.far_copies);
3845 size = size * conf->geo.raid_disks;
3846 sector_div(size, conf->geo.near_copies);
3847 /* 'size' is now the number of chunks in the array */
3848 /* calculate "used chunks per device" */
3849 size = size * conf->copies;
3851 /* We need to round up when dividing by raid_disks to
3852 * get the stride size.
3854 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3856 conf->dev_sectors = size << conf->geo.chunk_shift;
3858 if (conf->geo.far_offset)
3859 conf->geo.stride = 1 << conf->geo.chunk_shift;
3861 sector_div(size, conf->geo.far_copies);
3862 conf->geo.stride = size << conf->geo.chunk_shift;
3866 enum geo_type {geo_new, geo_old, geo_start};
3867 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3870 int layout, chunk, disks;
3873 layout = mddev->layout;
3874 chunk = mddev->chunk_sectors;
3875 disks = mddev->raid_disks - mddev->delta_disks;
3878 layout = mddev->new_layout;
3879 chunk = mddev->new_chunk_sectors;
3880 disks = mddev->raid_disks;
3882 default: /* avoid 'may be unused' warnings */
3883 case geo_start: /* new when starting reshape - raid_disks not
3885 layout = mddev->new_layout;
3886 chunk = mddev->new_chunk_sectors;
3887 disks = mddev->raid_disks + mddev->delta_disks;
3892 if (chunk < (PAGE_SIZE >> 9) ||
3893 !is_power_of_2(chunk))
3896 fc = (layout >> 8) & 255;
3897 fo = layout & (1<<16);
3898 geo->raid_disks = disks;
3899 geo->near_copies = nc;
3900 geo->far_copies = fc;
3901 geo->far_offset = fo;
3902 switch (layout >> 17) {
3903 case 0: /* original layout. simple but not always optimal */
3904 geo->far_set_size = disks;
3906 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3907 * actually using this, but leave code here just in case.*/
3908 geo->far_set_size = disks/fc;
3909 WARN(geo->far_set_size < fc,
3910 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3912 case 2: /* "improved" layout fixed to match documentation */
3913 geo->far_set_size = fc * nc;
3915 default: /* Not a valid layout */
3918 geo->chunk_mask = chunk - 1;
3919 geo->chunk_shift = ffz(~chunk);
3923 static struct r10conf *setup_conf(struct mddev *mddev)
3925 struct r10conf *conf = NULL;
3930 copies = setup_geo(&geo, mddev, geo_new);
3933 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3934 mdname(mddev), PAGE_SIZE);
3938 if (copies < 2 || copies > mddev->raid_disks) {
3939 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3940 mdname(mddev), mddev->new_layout);
3945 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3949 /* FIXME calc properly */
3950 conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks),
3951 sizeof(struct raid10_info),
3956 conf->tmppage = alloc_page(GFP_KERNEL);
3961 conf->copies = copies;
3962 err = mempool_init(&conf->r10bio_pool, NR_RAID_BIOS, r10bio_pool_alloc,
3963 rbio_pool_free, conf);
3967 err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
3971 calc_sectors(conf, mddev->dev_sectors);
3972 if (mddev->reshape_position == MaxSector) {
3973 conf->prev = conf->geo;
3974 conf->reshape_progress = MaxSector;
3976 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3980 conf->reshape_progress = mddev->reshape_position;
3981 if (conf->prev.far_offset)
3982 conf->prev.stride = 1 << conf->prev.chunk_shift;
3984 /* far_copies must be 1 */
3985 conf->prev.stride = conf->dev_sectors;
3987 conf->reshape_safe = conf->reshape_progress;
3988 spin_lock_init(&conf->device_lock);
3989 INIT_LIST_HEAD(&conf->retry_list);
3990 INIT_LIST_HEAD(&conf->bio_end_io_list);
3992 spin_lock_init(&conf->resync_lock);
3993 init_waitqueue_head(&conf->wait_barrier);
3994 atomic_set(&conf->nr_pending, 0);
3997 conf->thread = md_register_thread(raid10d, mddev, "raid10");
4001 conf->mddev = mddev;
4006 mempool_exit(&conf->r10bio_pool);
4007 kfree(conf->mirrors);
4008 safe_put_page(conf->tmppage);
4009 bioset_exit(&conf->bio_split);
4012 return ERR_PTR(err);
4015 static void raid10_set_io_opt(struct r10conf *conf)
4017 int raid_disks = conf->geo.raid_disks;
4019 if (!(conf->geo.raid_disks % conf->geo.near_copies))
4020 raid_disks /= conf->geo.near_copies;
4021 blk_queue_io_opt(conf->mddev->queue, (conf->mddev->chunk_sectors << 9) *
4025 static int raid10_run(struct mddev *mddev)
4027 struct r10conf *conf;
4029 struct raid10_info *disk;
4030 struct md_rdev *rdev;
4032 sector_t min_offset_diff = 0;
4034 bool discard_supported = false;
4036 if (mddev_init_writes_pending(mddev) < 0)
4039 if (mddev->private == NULL) {
4040 conf = setup_conf(mddev);
4042 return PTR_ERR(conf);
4043 mddev->private = conf;
4045 conf = mddev->private;
4049 if (mddev_is_clustered(conf->mddev)) {
4052 fc = (mddev->layout >> 8) & 255;
4053 fo = mddev->layout & (1<<16);
4054 if (fc > 1 || fo > 0) {
4055 pr_err("only near layout is supported by clustered"
4061 mddev->thread = conf->thread;
4062 conf->thread = NULL;
4065 blk_queue_max_discard_sectors(mddev->queue,
4067 blk_queue_max_write_same_sectors(mddev->queue, 0);
4068 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
4069 blk_queue_io_min(mddev->queue, mddev->chunk_sectors << 9);
4070 raid10_set_io_opt(conf);
4073 rdev_for_each(rdev, mddev) {
4076 disk_idx = rdev->raid_disk;
4079 if (disk_idx >= conf->geo.raid_disks &&
4080 disk_idx >= conf->prev.raid_disks)
4082 disk = conf->mirrors + disk_idx;
4084 if (test_bit(Replacement, &rdev->flags)) {
4085 if (disk->replacement)
4087 disk->replacement = rdev;
4093 diff = (rdev->new_data_offset - rdev->data_offset);
4094 if (!mddev->reshape_backwards)
4098 if (first || diff < min_offset_diff)
4099 min_offset_diff = diff;
4102 disk_stack_limits(mddev->gendisk, rdev->bdev,
4103 rdev->data_offset << 9);
4105 disk->head_position = 0;
4107 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
4108 discard_supported = true;
4113 if (discard_supported)
4114 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
4117 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
4120 /* need to check that every block has at least one working mirror */
4121 if (!enough(conf, -1)) {
4122 pr_err("md/raid10:%s: not enough operational mirrors.\n",
4127 if (conf->reshape_progress != MaxSector) {
4128 /* must ensure that shape change is supported */
4129 if (conf->geo.far_copies != 1 &&
4130 conf->geo.far_offset == 0)
4132 if (conf->prev.far_copies != 1 &&
4133 conf->prev.far_offset == 0)
4137 mddev->degraded = 0;
4139 i < conf->geo.raid_disks
4140 || i < conf->prev.raid_disks;
4143 disk = conf->mirrors + i;
4145 if (!disk->rdev && disk->replacement) {
4146 /* The replacement is all we have - use it */
4147 disk->rdev = disk->replacement;
4148 disk->replacement = NULL;
4149 clear_bit(Replacement, &disk->rdev->flags);
4153 !test_bit(In_sync, &disk->rdev->flags)) {
4154 disk->head_position = 0;
4157 disk->rdev->saved_raid_disk < 0)
4161 if (disk->replacement &&
4162 !test_bit(In_sync, &disk->replacement->flags) &&
4163 disk->replacement->saved_raid_disk < 0) {
4167 disk->recovery_disabled = mddev->recovery_disabled - 1;
4170 if (mddev->recovery_cp != MaxSector)
4171 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
4173 pr_info("md/raid10:%s: active with %d out of %d devices\n",
4174 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
4175 conf->geo.raid_disks);
4177 * Ok, everything is just fine now
4179 mddev->dev_sectors = conf->dev_sectors;
4180 size = raid10_size(mddev, 0, 0);
4181 md_set_array_sectors(mddev, size);
4182 mddev->resync_max_sectors = size;
4183 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
4185 if (md_integrity_register(mddev))
4188 if (conf->reshape_progress != MaxSector) {
4189 unsigned long before_length, after_length;
4191 before_length = ((1 << conf->prev.chunk_shift) *
4192 conf->prev.far_copies);
4193 after_length = ((1 << conf->geo.chunk_shift) *
4194 conf->geo.far_copies);
4196 if (max(before_length, after_length) > min_offset_diff) {
4197 /* This cannot work */
4198 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
4201 conf->offset_diff = min_offset_diff;
4203 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4204 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4205 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4206 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4207 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4209 if (!mddev->sync_thread)
4216 md_unregister_thread(&mddev->thread);
4217 mempool_exit(&conf->r10bio_pool);
4218 safe_put_page(conf->tmppage);
4219 kfree(conf->mirrors);
4221 mddev->private = NULL;
4226 static void raid10_free(struct mddev *mddev, void *priv)
4228 struct r10conf *conf = priv;
4230 mempool_exit(&conf->r10bio_pool);
4231 safe_put_page(conf->tmppage);
4232 kfree(conf->mirrors);
4233 kfree(conf->mirrors_old);
4234 kfree(conf->mirrors_new);
4235 bioset_exit(&conf->bio_split);
4239 static void raid10_quiesce(struct mddev *mddev, int quiesce)
4241 struct r10conf *conf = mddev->private;
4244 raise_barrier(conf, 0);
4246 lower_barrier(conf);
4249 static int raid10_resize(struct mddev *mddev, sector_t sectors)
4251 /* Resize of 'far' arrays is not supported.
4252 * For 'near' and 'offset' arrays we can set the
4253 * number of sectors used to be an appropriate multiple
4254 * of the chunk size.
4255 * For 'offset', this is far_copies*chunksize.
4256 * For 'near' the multiplier is the LCM of
4257 * near_copies and raid_disks.
4258 * So if far_copies > 1 && !far_offset, fail.
4259 * Else find LCM(raid_disks, near_copy)*far_copies and
4260 * multiply by chunk_size. Then round to this number.
4261 * This is mostly done by raid10_size()
4263 struct r10conf *conf = mddev->private;
4264 sector_t oldsize, size;
4266 if (mddev->reshape_position != MaxSector)
4269 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
4272 oldsize = raid10_size(mddev, 0, 0);
4273 size = raid10_size(mddev, sectors, 0);
4274 if (mddev->external_size &&
4275 mddev->array_sectors > size)
4277 if (mddev->bitmap) {
4278 int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0);
4282 md_set_array_sectors(mddev, size);
4283 if (sectors > mddev->dev_sectors &&
4284 mddev->recovery_cp > oldsize) {
4285 mddev->recovery_cp = oldsize;
4286 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4288 calc_sectors(conf, sectors);
4289 mddev->dev_sectors = conf->dev_sectors;
4290 mddev->resync_max_sectors = size;
4294 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
4296 struct md_rdev *rdev;
4297 struct r10conf *conf;
4299 if (mddev->degraded > 0) {
4300 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
4302 return ERR_PTR(-EINVAL);
4304 sector_div(size, devs);
4306 /* Set new parameters */
4307 mddev->new_level = 10;
4308 /* new layout: far_copies = 1, near_copies = 2 */
4309 mddev->new_layout = (1<<8) + 2;
4310 mddev->new_chunk_sectors = mddev->chunk_sectors;
4311 mddev->delta_disks = mddev->raid_disks;
4312 mddev->raid_disks *= 2;
4313 /* make sure it will be not marked as dirty */
4314 mddev->recovery_cp = MaxSector;
4315 mddev->dev_sectors = size;
4317 conf = setup_conf(mddev);
4318 if (!IS_ERR(conf)) {
4319 rdev_for_each(rdev, mddev)
4320 if (rdev->raid_disk >= 0) {
4321 rdev->new_raid_disk = rdev->raid_disk * 2;
4322 rdev->sectors = size;
4330 static void *raid10_takeover(struct mddev *mddev)
4332 struct r0conf *raid0_conf;
4334 /* raid10 can take over:
4335 * raid0 - providing it has only two drives
4337 if (mddev->level == 0) {
4338 /* for raid0 takeover only one zone is supported */
4339 raid0_conf = mddev->private;
4340 if (raid0_conf->nr_strip_zones > 1) {
4341 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
4343 return ERR_PTR(-EINVAL);
4345 return raid10_takeover_raid0(mddev,
4346 raid0_conf->strip_zone->zone_end,
4347 raid0_conf->strip_zone->nb_dev);
4349 return ERR_PTR(-EINVAL);
4352 static int raid10_check_reshape(struct mddev *mddev)
4354 /* Called when there is a request to change
4355 * - layout (to ->new_layout)
4356 * - chunk size (to ->new_chunk_sectors)
4357 * - raid_disks (by delta_disks)
4358 * or when trying to restart a reshape that was ongoing.
4360 * We need to validate the request and possibly allocate
4361 * space if that might be an issue later.
4363 * Currently we reject any reshape of a 'far' mode array,
4364 * allow chunk size to change if new is generally acceptable,
4365 * allow raid_disks to increase, and allow
4366 * a switch between 'near' mode and 'offset' mode.
4368 struct r10conf *conf = mddev->private;
4371 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
4374 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
4375 /* mustn't change number of copies */
4377 if (geo.far_copies > 1 && !geo.far_offset)
4378 /* Cannot switch to 'far' mode */
4381 if (mddev->array_sectors & geo.chunk_mask)
4382 /* not factor of array size */
4385 if (!enough(conf, -1))
4388 kfree(conf->mirrors_new);
4389 conf->mirrors_new = NULL;
4390 if (mddev->delta_disks > 0) {
4391 /* allocate new 'mirrors' list */
4393 kcalloc(mddev->raid_disks + mddev->delta_disks,
4394 sizeof(struct raid10_info),
4396 if (!conf->mirrors_new)
4403 * Need to check if array has failed when deciding whether to:
4405 * - remove non-faulty devices
4408 * This determination is simple when no reshape is happening.
4409 * However if there is a reshape, we need to carefully check
4410 * both the before and after sections.
4411 * This is because some failed devices may only affect one
4412 * of the two sections, and some non-in_sync devices may
4413 * be insync in the section most affected by failed devices.
4415 static int calc_degraded(struct r10conf *conf)
4417 int degraded, degraded2;
4422 /* 'prev' section first */
4423 for (i = 0; i < conf->prev.raid_disks; i++) {
4424 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4425 if (!rdev || test_bit(Faulty, &rdev->flags))
4427 else if (!test_bit(In_sync, &rdev->flags))
4428 /* When we can reduce the number of devices in
4429 * an array, this might not contribute to
4430 * 'degraded'. It does now.
4435 if (conf->geo.raid_disks == conf->prev.raid_disks)
4439 for (i = 0; i < conf->geo.raid_disks; i++) {
4440 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4441 if (!rdev || test_bit(Faulty, &rdev->flags))
4443 else if (!test_bit(In_sync, &rdev->flags)) {
4444 /* If reshape is increasing the number of devices,
4445 * this section has already been recovered, so
4446 * it doesn't contribute to degraded.
4449 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4454 if (degraded2 > degraded)
4459 static int raid10_start_reshape(struct mddev *mddev)
4461 /* A 'reshape' has been requested. This commits
4462 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4463 * This also checks if there are enough spares and adds them
4465 * We currently require enough spares to make the final
4466 * array non-degraded. We also require that the difference
4467 * between old and new data_offset - on each device - is
4468 * enough that we never risk over-writing.
4471 unsigned long before_length, after_length;
4472 sector_t min_offset_diff = 0;
4475 struct r10conf *conf = mddev->private;
4476 struct md_rdev *rdev;
4480 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4483 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4486 before_length = ((1 << conf->prev.chunk_shift) *
4487 conf->prev.far_copies);
4488 after_length = ((1 << conf->geo.chunk_shift) *
4489 conf->geo.far_copies);
4491 rdev_for_each(rdev, mddev) {
4492 if (!test_bit(In_sync, &rdev->flags)
4493 && !test_bit(Faulty, &rdev->flags))
4495 if (rdev->raid_disk >= 0) {
4496 long long diff = (rdev->new_data_offset
4497 - rdev->data_offset);
4498 if (!mddev->reshape_backwards)
4502 if (first || diff < min_offset_diff)
4503 min_offset_diff = diff;
4508 if (max(before_length, after_length) > min_offset_diff)
4511 if (spares < mddev->delta_disks)
4514 conf->offset_diff = min_offset_diff;
4515 spin_lock_irq(&conf->device_lock);
4516 if (conf->mirrors_new) {
4517 memcpy(conf->mirrors_new, conf->mirrors,
4518 sizeof(struct raid10_info)*conf->prev.raid_disks);
4520 kfree(conf->mirrors_old);
4521 conf->mirrors_old = conf->mirrors;
4522 conf->mirrors = conf->mirrors_new;
4523 conf->mirrors_new = NULL;
4525 setup_geo(&conf->geo, mddev, geo_start);
4527 if (mddev->reshape_backwards) {
4528 sector_t size = raid10_size(mddev, 0, 0);
4529 if (size < mddev->array_sectors) {
4530 spin_unlock_irq(&conf->device_lock);
4531 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4535 mddev->resync_max_sectors = size;
4536 conf->reshape_progress = size;
4538 conf->reshape_progress = 0;
4539 conf->reshape_safe = conf->reshape_progress;
4540 spin_unlock_irq(&conf->device_lock);
4542 if (mddev->delta_disks && mddev->bitmap) {
4543 struct mdp_superblock_1 *sb = NULL;
4544 sector_t oldsize, newsize;
4546 oldsize = raid10_size(mddev, 0, 0);
4547 newsize = raid10_size(mddev, 0, conf->geo.raid_disks);
4549 if (!mddev_is_clustered(mddev)) {
4550 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4557 rdev_for_each(rdev, mddev) {
4558 if (rdev->raid_disk > -1 &&
4559 !test_bit(Faulty, &rdev->flags))
4560 sb = page_address(rdev->sb_page);
4564 * some node is already performing reshape, and no need to
4565 * call md_bitmap_resize again since it should be called when
4566 * receiving BITMAP_RESIZE msg
4568 if ((sb && (le32_to_cpu(sb->feature_map) &
4569 MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize))
4572 ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0);
4576 ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize);
4578 md_bitmap_resize(mddev->bitmap, oldsize, 0, 0);
4583 if (mddev->delta_disks > 0) {
4584 rdev_for_each(rdev, mddev)
4585 if (rdev->raid_disk < 0 &&
4586 !test_bit(Faulty, &rdev->flags)) {
4587 if (raid10_add_disk(mddev, rdev) == 0) {
4588 if (rdev->raid_disk >=
4589 conf->prev.raid_disks)
4590 set_bit(In_sync, &rdev->flags);
4592 rdev->recovery_offset = 0;
4594 /* Failure here is OK */
4595 sysfs_link_rdev(mddev, rdev);
4597 } else if (rdev->raid_disk >= conf->prev.raid_disks
4598 && !test_bit(Faulty, &rdev->flags)) {
4599 /* This is a spare that was manually added */
4600 set_bit(In_sync, &rdev->flags);
4603 /* When a reshape changes the number of devices,
4604 * ->degraded is measured against the larger of the
4605 * pre and post numbers.
4607 spin_lock_irq(&conf->device_lock);
4608 mddev->degraded = calc_degraded(conf);
4609 spin_unlock_irq(&conf->device_lock);
4610 mddev->raid_disks = conf->geo.raid_disks;
4611 mddev->reshape_position = conf->reshape_progress;
4612 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4614 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4615 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4616 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4617 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4618 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4620 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4622 if (!mddev->sync_thread) {
4626 conf->reshape_checkpoint = jiffies;
4627 md_wakeup_thread(mddev->sync_thread);
4628 md_new_event(mddev);
4632 mddev->recovery = 0;
4633 spin_lock_irq(&conf->device_lock);
4634 conf->geo = conf->prev;
4635 mddev->raid_disks = conf->geo.raid_disks;
4636 rdev_for_each(rdev, mddev)
4637 rdev->new_data_offset = rdev->data_offset;
4639 conf->reshape_progress = MaxSector;
4640 conf->reshape_safe = MaxSector;
4641 mddev->reshape_position = MaxSector;
4642 spin_unlock_irq(&conf->device_lock);
4646 /* Calculate the last device-address that could contain
4647 * any block from the chunk that includes the array-address 's'
4648 * and report the next address.
4649 * i.e. the address returned will be chunk-aligned and after
4650 * any data that is in the chunk containing 's'.
4652 static sector_t last_dev_address(sector_t s, struct geom *geo)
4654 s = (s | geo->chunk_mask) + 1;
4655 s >>= geo->chunk_shift;
4656 s *= geo->near_copies;
4657 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4658 s *= geo->far_copies;
4659 s <<= geo->chunk_shift;
4663 /* Calculate the first device-address that could contain
4664 * any block from the chunk that includes the array-address 's'.
4665 * This too will be the start of a chunk
4667 static sector_t first_dev_address(sector_t s, struct geom *geo)
4669 s >>= geo->chunk_shift;
4670 s *= geo->near_copies;
4671 sector_div(s, geo->raid_disks);
4672 s *= geo->far_copies;
4673 s <<= geo->chunk_shift;
4677 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4680 /* We simply copy at most one chunk (smallest of old and new)
4681 * at a time, possibly less if that exceeds RESYNC_PAGES,
4682 * or we hit a bad block or something.
4683 * This might mean we pause for normal IO in the middle of
4684 * a chunk, but that is not a problem as mddev->reshape_position
4685 * can record any location.
4687 * If we will want to write to a location that isn't
4688 * yet recorded as 'safe' (i.e. in metadata on disk) then
4689 * we need to flush all reshape requests and update the metadata.
4691 * When reshaping forwards (e.g. to more devices), we interpret
4692 * 'safe' as the earliest block which might not have been copied
4693 * down yet. We divide this by previous stripe size and multiply
4694 * by previous stripe length to get lowest device offset that we
4695 * cannot write to yet.
4696 * We interpret 'sector_nr' as an address that we want to write to.
4697 * From this we use last_device_address() to find where we might
4698 * write to, and first_device_address on the 'safe' position.
4699 * If this 'next' write position is after the 'safe' position,
4700 * we must update the metadata to increase the 'safe' position.
4702 * When reshaping backwards, we round in the opposite direction
4703 * and perform the reverse test: next write position must not be
4704 * less than current safe position.
4706 * In all this the minimum difference in data offsets
4707 * (conf->offset_diff - always positive) allows a bit of slack,
4708 * so next can be after 'safe', but not by more than offset_diff
4710 * We need to prepare all the bios here before we start any IO
4711 * to ensure the size we choose is acceptable to all devices.
4712 * The means one for each copy for write-out and an extra one for
4714 * We store the read-in bio in ->master_bio and the others in
4715 * ->devs[x].bio and ->devs[x].repl_bio.
4717 struct r10conf *conf = mddev->private;
4718 struct r10bio *r10_bio;
4719 sector_t next, safe, last;
4723 struct md_rdev *rdev;
4726 struct bio *bio, *read_bio;
4727 int sectors_done = 0;
4728 struct page **pages;
4730 if (sector_nr == 0) {
4731 /* If restarting in the middle, skip the initial sectors */
4732 if (mddev->reshape_backwards &&
4733 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4734 sector_nr = (raid10_size(mddev, 0, 0)
4735 - conf->reshape_progress);
4736 } else if (!mddev->reshape_backwards &&
4737 conf->reshape_progress > 0)
4738 sector_nr = conf->reshape_progress;
4740 mddev->curr_resync_completed = sector_nr;
4741 sysfs_notify_dirent_safe(mddev->sysfs_completed);
4747 /* We don't use sector_nr to track where we are up to
4748 * as that doesn't work well for ->reshape_backwards.
4749 * So just use ->reshape_progress.
4751 if (mddev->reshape_backwards) {
4752 /* 'next' is the earliest device address that we might
4753 * write to for this chunk in the new layout
4755 next = first_dev_address(conf->reshape_progress - 1,
4758 /* 'safe' is the last device address that we might read from
4759 * in the old layout after a restart
4761 safe = last_dev_address(conf->reshape_safe - 1,
4764 if (next + conf->offset_diff < safe)
4767 last = conf->reshape_progress - 1;
4768 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4769 & conf->prev.chunk_mask);
4770 if (sector_nr + RESYNC_SECTORS < last)
4771 sector_nr = last + 1 - RESYNC_SECTORS;
4773 /* 'next' is after the last device address that we
4774 * might write to for this chunk in the new layout
4776 next = last_dev_address(conf->reshape_progress, &conf->geo);
4778 /* 'safe' is the earliest device address that we might
4779 * read from in the old layout after a restart
4781 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4783 /* Need to update metadata if 'next' might be beyond 'safe'
4784 * as that would possibly corrupt data
4786 if (next > safe + conf->offset_diff)
4789 sector_nr = conf->reshape_progress;
4790 last = sector_nr | (conf->geo.chunk_mask
4791 & conf->prev.chunk_mask);
4793 if (sector_nr + RESYNC_SECTORS <= last)
4794 last = sector_nr + RESYNC_SECTORS - 1;
4798 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4799 /* Need to update reshape_position in metadata */
4801 mddev->reshape_position = conf->reshape_progress;
4802 if (mddev->reshape_backwards)
4803 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4804 - conf->reshape_progress;
4806 mddev->curr_resync_completed = conf->reshape_progress;
4807 conf->reshape_checkpoint = jiffies;
4808 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4809 md_wakeup_thread(mddev->thread);
4810 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
4811 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4812 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4813 allow_barrier(conf);
4814 return sectors_done;
4816 conf->reshape_safe = mddev->reshape_position;
4817 allow_barrier(conf);
4820 raise_barrier(conf, 0);
4822 /* Now schedule reads for blocks from sector_nr to last */
4823 r10_bio = raid10_alloc_init_r10buf(conf);
4825 raise_barrier(conf, 1);
4826 atomic_set(&r10_bio->remaining, 0);
4827 r10_bio->mddev = mddev;
4828 r10_bio->sector = sector_nr;
4829 set_bit(R10BIO_IsReshape, &r10_bio->state);
4830 r10_bio->sectors = last - sector_nr + 1;
4831 rdev = read_balance(conf, r10_bio, &max_sectors);
4832 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4835 /* Cannot read from here, so need to record bad blocks
4836 * on all the target devices.
4839 mempool_free(r10_bio, &conf->r10buf_pool);
4840 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4841 return sectors_done;
4844 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4846 bio_set_dev(read_bio, rdev->bdev);
4847 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4848 + rdev->data_offset);
4849 read_bio->bi_private = r10_bio;
4850 read_bio->bi_end_io = end_reshape_read;
4851 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
4852 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
4853 read_bio->bi_status = 0;
4854 read_bio->bi_vcnt = 0;
4855 read_bio->bi_iter.bi_size = 0;
4856 r10_bio->master_bio = read_bio;
4857 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4860 * Broadcast RESYNC message to other nodes, so all nodes would not
4861 * write to the region to avoid conflict.
4863 if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) {
4864 struct mdp_superblock_1 *sb = NULL;
4865 int sb_reshape_pos = 0;
4867 conf->cluster_sync_low = sector_nr;
4868 conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS;
4869 sb = page_address(rdev->sb_page);
4871 sb_reshape_pos = le64_to_cpu(sb->reshape_position);
4873 * Set cluster_sync_low again if next address for array
4874 * reshape is less than cluster_sync_low. Since we can't
4875 * update cluster_sync_low until it has finished reshape.
4877 if (sb_reshape_pos < conf->cluster_sync_low)
4878 conf->cluster_sync_low = sb_reshape_pos;
4881 md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low,
4882 conf->cluster_sync_high);
4885 /* Now find the locations in the new layout */
4886 __raid10_find_phys(&conf->geo, r10_bio);
4889 read_bio->bi_next = NULL;
4892 for (s = 0; s < conf->copies*2; s++) {
4894 int d = r10_bio->devs[s/2].devnum;
4895 struct md_rdev *rdev2;
4897 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4898 b = r10_bio->devs[s/2].repl_bio;
4900 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4901 b = r10_bio->devs[s/2].bio;
4903 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4906 bio_set_dev(b, rdev2->bdev);
4907 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4908 rdev2->new_data_offset;
4909 b->bi_end_io = end_reshape_write;
4910 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
4915 /* Now add as many pages as possible to all of these bios. */
4918 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4919 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4920 struct page *page = pages[s / (PAGE_SIZE >> 9)];
4921 int len = (max_sectors - s) << 9;
4922 if (len > PAGE_SIZE)
4924 for (bio = blist; bio ; bio = bio->bi_next) {
4926 * won't fail because the vec table is big enough
4927 * to hold all these pages
4929 bio_add_page(bio, page, len, 0);
4931 sector_nr += len >> 9;
4932 nr_sectors += len >> 9;
4935 r10_bio->sectors = nr_sectors;
4937 /* Now submit the read */
4938 md_sync_acct_bio(read_bio, r10_bio->sectors);
4939 atomic_inc(&r10_bio->remaining);
4940 read_bio->bi_next = NULL;
4941 submit_bio_noacct(read_bio);
4942 sectors_done += nr_sectors;
4943 if (sector_nr <= last)
4946 lower_barrier(conf);
4948 /* Now that we have done the whole section we can
4949 * update reshape_progress
4951 if (mddev->reshape_backwards)
4952 conf->reshape_progress -= sectors_done;
4954 conf->reshape_progress += sectors_done;
4956 return sectors_done;
4959 static void end_reshape_request(struct r10bio *r10_bio);
4960 static int handle_reshape_read_error(struct mddev *mddev,
4961 struct r10bio *r10_bio);
4962 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4964 /* Reshape read completed. Hopefully we have a block
4966 * If we got a read error then we do sync 1-page reads from
4967 * elsewhere until we find the data - or give up.
4969 struct r10conf *conf = mddev->private;
4972 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4973 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4974 /* Reshape has been aborted */
4975 md_done_sync(mddev, r10_bio->sectors, 0);
4979 /* We definitely have the data in the pages, schedule the
4982 atomic_set(&r10_bio->remaining, 1);
4983 for (s = 0; s < conf->copies*2; s++) {
4985 int d = r10_bio->devs[s/2].devnum;
4986 struct md_rdev *rdev;
4989 rdev = rcu_dereference(conf->mirrors[d].replacement);
4990 b = r10_bio->devs[s/2].repl_bio;
4992 rdev = rcu_dereference(conf->mirrors[d].rdev);
4993 b = r10_bio->devs[s/2].bio;
4995 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4999 atomic_inc(&rdev->nr_pending);
5001 md_sync_acct_bio(b, r10_bio->sectors);
5002 atomic_inc(&r10_bio->remaining);
5004 submit_bio_noacct(b);
5006 end_reshape_request(r10_bio);
5009 static void end_reshape(struct r10conf *conf)
5011 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
5014 spin_lock_irq(&conf->device_lock);
5015 conf->prev = conf->geo;
5016 md_finish_reshape(conf->mddev);
5018 conf->reshape_progress = MaxSector;
5019 conf->reshape_safe = MaxSector;
5020 spin_unlock_irq(&conf->device_lock);
5022 if (conf->mddev->queue)
5023 raid10_set_io_opt(conf);
5027 static void raid10_update_reshape_pos(struct mddev *mddev)
5029 struct r10conf *conf = mddev->private;
5032 md_cluster_ops->resync_info_get(mddev, &lo, &hi);
5033 if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo))
5034 || mddev->reshape_position == MaxSector)
5035 conf->reshape_progress = mddev->reshape_position;
5040 static int handle_reshape_read_error(struct mddev *mddev,
5041 struct r10bio *r10_bio)
5043 /* Use sync reads to get the blocks from somewhere else */
5044 int sectors = r10_bio->sectors;
5045 struct r10conf *conf = mddev->private;
5046 struct r10bio *r10b;
5049 struct page **pages;
5051 r10b = kmalloc(struct_size(r10b, devs, conf->copies), GFP_NOIO);
5053 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
5057 /* reshape IOs share pages from .devs[0].bio */
5058 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
5060 r10b->sector = r10_bio->sector;
5061 __raid10_find_phys(&conf->prev, r10b);
5066 int first_slot = slot;
5068 if (s > (PAGE_SIZE >> 9))
5073 int d = r10b->devs[slot].devnum;
5074 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
5077 test_bit(Faulty, &rdev->flags) ||
5078 !test_bit(In_sync, &rdev->flags))
5081 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
5082 atomic_inc(&rdev->nr_pending);
5084 success = sync_page_io(rdev,
5088 REQ_OP_READ, 0, false);
5089 rdev_dec_pending(rdev, mddev);
5095 if (slot >= conf->copies)
5097 if (slot == first_slot)
5102 /* couldn't read this block, must give up */
5103 set_bit(MD_RECOVERY_INTR,
5115 static void end_reshape_write(struct bio *bio)
5117 struct r10bio *r10_bio = get_resync_r10bio(bio);
5118 struct mddev *mddev = r10_bio->mddev;
5119 struct r10conf *conf = mddev->private;
5123 struct md_rdev *rdev = NULL;
5125 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
5127 rdev = conf->mirrors[d].replacement;
5130 rdev = conf->mirrors[d].rdev;
5133 if (bio->bi_status) {
5134 /* FIXME should record badblock */
5135 md_error(mddev, rdev);
5138 rdev_dec_pending(rdev, mddev);
5139 end_reshape_request(r10_bio);
5142 static void end_reshape_request(struct r10bio *r10_bio)
5144 if (!atomic_dec_and_test(&r10_bio->remaining))
5146 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
5147 bio_put(r10_bio->master_bio);
5151 static void raid10_finish_reshape(struct mddev *mddev)
5153 struct r10conf *conf = mddev->private;
5155 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5158 if (mddev->delta_disks > 0) {
5159 if (mddev->recovery_cp > mddev->resync_max_sectors) {
5160 mddev->recovery_cp = mddev->resync_max_sectors;
5161 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5163 mddev->resync_max_sectors = mddev->array_sectors;
5167 for (d = conf->geo.raid_disks ;
5168 d < conf->geo.raid_disks - mddev->delta_disks;
5170 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
5172 clear_bit(In_sync, &rdev->flags);
5173 rdev = rcu_dereference(conf->mirrors[d].replacement);
5175 clear_bit(In_sync, &rdev->flags);
5179 mddev->layout = mddev->new_layout;
5180 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
5181 mddev->reshape_position = MaxSector;
5182 mddev->delta_disks = 0;
5183 mddev->reshape_backwards = 0;
5186 static struct md_personality raid10_personality =
5190 .owner = THIS_MODULE,
5191 .make_request = raid10_make_request,
5193 .free = raid10_free,
5194 .status = raid10_status,
5195 .error_handler = raid10_error,
5196 .hot_add_disk = raid10_add_disk,
5197 .hot_remove_disk= raid10_remove_disk,
5198 .spare_active = raid10_spare_active,
5199 .sync_request = raid10_sync_request,
5200 .quiesce = raid10_quiesce,
5201 .size = raid10_size,
5202 .resize = raid10_resize,
5203 .takeover = raid10_takeover,
5204 .check_reshape = raid10_check_reshape,
5205 .start_reshape = raid10_start_reshape,
5206 .finish_reshape = raid10_finish_reshape,
5207 .update_reshape_pos = raid10_update_reshape_pos,
5210 static int __init raid_init(void)
5212 return register_md_personality(&raid10_personality);
5215 static void raid_exit(void)
5217 unregister_md_personality(&raid10_personality);
5220 module_init(raid_init);
5221 module_exit(raid_exit);
5222 MODULE_LICENSE("GPL");
5223 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
5224 MODULE_ALIAS("md-personality-9"); /* RAID10 */
5225 MODULE_ALIAS("md-raid10");
5226 MODULE_ALIAS("md-level-10");
5228 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);