2 * raid10.c : Multiple Devices driver for Linux
4 * Copyright (C) 2000-2004 Neil Brown
6 * RAID-10 support for md.
8 * Base on code in raid1.c. See raid1.c for further copyright information.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/blkdev.h>
24 #include <linux/module.h>
25 #include <linux/seq_file.h>
26 #include <linux/ratelimit.h>
27 #include <linux/kthread.h>
28 #include <trace/events/block.h>
35 * RAID10 provides a combination of RAID0 and RAID1 functionality.
36 * The layout of data is defined by
39 * near_copies (stored in low byte of layout)
40 * far_copies (stored in second byte of layout)
41 * far_offset (stored in bit 16 of layout )
42 * use_far_sets (stored in bit 17 of layout )
43 * use_far_sets_bugfixed (stored in bit 18 of layout )
45 * The data to be stored is divided into chunks using chunksize. Each device
46 * is divided into far_copies sections. In each section, chunks are laid out
47 * in a style similar to raid0, but near_copies copies of each chunk is stored
48 * (each on a different drive). The starting device for each section is offset
49 * near_copies from the starting device of the previous section. Thus there
50 * are (near_copies * far_copies) of each chunk, and each is on a different
51 * drive. near_copies and far_copies must be at least one, and their product
52 * is at most raid_disks.
54 * If far_offset is true, then the far_copies are handled a bit differently.
55 * The copies are still in different stripes, but instead of being very far
56 * apart on disk, there are adjacent stripes.
58 * The far and offset algorithms are handled slightly differently if
59 * 'use_far_sets' is true. In this case, the array's devices are grouped into
60 * sets that are (near_copies * far_copies) in size. The far copied stripes
61 * are still shifted by 'near_copies' devices, but this shifting stays confined
62 * to the set rather than the entire array. This is done to improve the number
63 * of device combinations that can fail without causing the array to fail.
64 * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk
69 * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s):
70 * [A B] [C D] [A B] [C D E]
71 * |...| |...| |...| | ... |
72 * [B A] [D C] [B A] [E C D]
76 * Number of guaranteed r10bios in case of extreme VM load:
78 #define NR_RAID10_BIOS 256
80 /* when we get a read error on a read-only array, we redirect to another
81 * device without failing the first device, or trying to over-write to
82 * correct the read error. To keep track of bad blocks on a per-bio
83 * level, we store IO_BLOCKED in the appropriate 'bios' pointer
85 #define IO_BLOCKED ((struct bio *)1)
86 /* When we successfully write to a known bad-block, we need to remove the
87 * bad-block marking which must be done from process context. So we record
88 * the success by setting devs[n].bio to IO_MADE_GOOD
90 #define IO_MADE_GOOD ((struct bio *)2)
92 #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2)
94 /* When there are this many requests queued to be written by
95 * the raid10 thread, we become 'congested' to provide back-pressure
98 static int max_queued_requests = 1024;
100 static void allow_barrier(struct r10conf *conf);
101 static void lower_barrier(struct r10conf *conf);
102 static int _enough(struct r10conf *conf, int previous, int ignore);
103 static int enough(struct r10conf *conf, int ignore);
104 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
106 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio);
107 static void end_reshape_write(struct bio *bio);
108 static void end_reshape(struct r10conf *conf);
110 #define raid10_log(md, fmt, args...) \
111 do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0)
113 #include "raid1-10.c"
116 * for resync bio, r10bio pointer can be retrieved from the per-bio
117 * 'struct resync_pages'.
119 static inline struct r10bio *get_resync_r10bio(struct bio *bio)
121 return get_resync_pages(bio)->raid_bio;
124 static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data)
126 struct r10conf *conf = data;
127 int size = offsetof(struct r10bio, devs[conf->copies]);
129 /* allocate a r10bio with room for raid_disks entries in the
131 return kzalloc(size, gfp_flags);
134 static void r10bio_pool_free(void *r10_bio, void *data)
139 /* amount of memory to reserve for resync requests */
140 #define RESYNC_WINDOW (1024*1024)
141 /* maximum number of concurrent requests, memory permitting */
142 #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE)
145 * When performing a resync, we need to read and compare, so
146 * we need as many pages are there are copies.
147 * When performing a recovery, we need 2 bios, one for read,
148 * one for write (we recover only one drive per r10buf)
151 static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data)
153 struct r10conf *conf = data;
154 struct r10bio *r10_bio;
157 int nalloc, nalloc_rp;
158 struct resync_pages *rps;
160 r10_bio = r10bio_pool_alloc(gfp_flags, conf);
164 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
165 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
166 nalloc = conf->copies; /* resync */
168 nalloc = 2; /* recovery */
170 /* allocate once for all bios */
171 if (!conf->have_replacement)
174 nalloc_rp = nalloc * 2;
175 rps = kmalloc(sizeof(struct resync_pages) * nalloc_rp, gfp_flags);
177 goto out_free_r10bio;
182 for (j = nalloc ; j-- ; ) {
183 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
186 r10_bio->devs[j].bio = bio;
187 if (!conf->have_replacement)
189 bio = bio_kmalloc(gfp_flags, RESYNC_PAGES);
192 r10_bio->devs[j].repl_bio = bio;
195 * Allocate RESYNC_PAGES data pages and attach them
198 for (j = 0; j < nalloc; j++) {
199 struct bio *rbio = r10_bio->devs[j].repl_bio;
200 struct resync_pages *rp, *rp_repl;
204 rp_repl = &rps[nalloc + j];
206 bio = r10_bio->devs[j].bio;
208 if (!j || test_bit(MD_RECOVERY_SYNC,
209 &conf->mddev->recovery)) {
210 if (resync_alloc_pages(rp, gfp_flags))
213 memcpy(rp, &rps[0], sizeof(*rp));
214 resync_get_all_pages(rp);
217 rp->raid_bio = r10_bio;
218 bio->bi_private = rp;
220 memcpy(rp_repl, rp, sizeof(*rp));
221 rbio->bi_private = rp_repl;
229 resync_free_pages(&rps[j]);
233 for ( ; j < nalloc; j++) {
234 if (r10_bio->devs[j].bio)
235 bio_put(r10_bio->devs[j].bio);
236 if (r10_bio->devs[j].repl_bio)
237 bio_put(r10_bio->devs[j].repl_bio);
241 r10bio_pool_free(r10_bio, conf);
245 static void r10buf_pool_free(void *__r10_bio, void *data)
247 struct r10conf *conf = data;
248 struct r10bio *r10bio = __r10_bio;
250 struct resync_pages *rp = NULL;
252 for (j = conf->copies; j--; ) {
253 struct bio *bio = r10bio->devs[j].bio;
255 rp = get_resync_pages(bio);
256 resync_free_pages(rp);
259 bio = r10bio->devs[j].repl_bio;
264 /* resync pages array stored in the 1st bio's .bi_private */
267 r10bio_pool_free(r10bio, conf);
270 static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio)
274 for (i = 0; i < conf->copies; i++) {
275 struct bio **bio = & r10_bio->devs[i].bio;
276 if (!BIO_SPECIAL(*bio))
279 bio = &r10_bio->devs[i].repl_bio;
280 if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio))
286 static void free_r10bio(struct r10bio *r10_bio)
288 struct r10conf *conf = r10_bio->mddev->private;
290 put_all_bios(conf, r10_bio);
291 mempool_free(r10_bio, conf->r10bio_pool);
294 static void put_buf(struct r10bio *r10_bio)
296 struct r10conf *conf = r10_bio->mddev->private;
298 mempool_free(r10_bio, conf->r10buf_pool);
303 static void reschedule_retry(struct r10bio *r10_bio)
306 struct mddev *mddev = r10_bio->mddev;
307 struct r10conf *conf = mddev->private;
309 spin_lock_irqsave(&conf->device_lock, flags);
310 list_add(&r10_bio->retry_list, &conf->retry_list);
312 spin_unlock_irqrestore(&conf->device_lock, flags);
314 /* wake up frozen array... */
315 wake_up(&conf->wait_barrier);
317 md_wakeup_thread(mddev->thread);
321 * raid_end_bio_io() is called when we have finished servicing a mirrored
322 * operation and are ready to return a success/failure code to the buffer
325 static void raid_end_bio_io(struct r10bio *r10_bio)
327 struct bio *bio = r10_bio->master_bio;
328 struct r10conf *conf = r10_bio->mddev->private;
330 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
331 bio->bi_status = BLK_STS_IOERR;
335 * Wake up any possible resync thread that waits for the device
340 free_r10bio(r10_bio);
344 * Update disk head position estimator based on IRQ completion info.
346 static inline void update_head_pos(int slot, struct r10bio *r10_bio)
348 struct r10conf *conf = r10_bio->mddev->private;
350 conf->mirrors[r10_bio->devs[slot].devnum].head_position =
351 r10_bio->devs[slot].addr + (r10_bio->sectors);
355 * Find the disk number which triggered given bio
357 static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio,
358 struct bio *bio, int *slotp, int *replp)
363 for (slot = 0; slot < conf->copies; slot++) {
364 if (r10_bio->devs[slot].bio == bio)
366 if (r10_bio->devs[slot].repl_bio == bio) {
372 BUG_ON(slot == conf->copies);
373 update_head_pos(slot, r10_bio);
379 return r10_bio->devs[slot].devnum;
382 static void raid10_end_read_request(struct bio *bio)
384 int uptodate = !bio->bi_status;
385 struct r10bio *r10_bio = bio->bi_private;
387 struct md_rdev *rdev;
388 struct r10conf *conf = r10_bio->mddev->private;
390 slot = r10_bio->read_slot;
391 dev = r10_bio->devs[slot].devnum;
392 rdev = r10_bio->devs[slot].rdev;
394 * this branch is our 'one mirror IO has finished' event handler:
396 update_head_pos(slot, r10_bio);
400 * Set R10BIO_Uptodate in our master bio, so that
401 * we will return a good error code to the higher
402 * levels even if IO on some other mirrored buffer fails.
404 * The 'master' represents the composite IO operation to
405 * user-side. So if something waits for IO, then it will
406 * wait for the 'master' bio.
408 set_bit(R10BIO_Uptodate, &r10_bio->state);
410 /* If all other devices that store this block have
411 * failed, we want to return the error upwards rather
412 * than fail the last device. Here we redefine
413 * "uptodate" to mean "Don't want to retry"
415 if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state),
420 raid_end_bio_io(r10_bio);
421 rdev_dec_pending(rdev, conf->mddev);
424 * oops, read error - keep the refcount on the rdev
426 char b[BDEVNAME_SIZE];
427 pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n",
429 bdevname(rdev->bdev, b),
430 (unsigned long long)r10_bio->sector);
431 set_bit(R10BIO_ReadError, &r10_bio->state);
432 reschedule_retry(r10_bio);
436 static void close_write(struct r10bio *r10_bio)
438 /* clear the bitmap if all writes complete successfully */
439 bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector,
441 !test_bit(R10BIO_Degraded, &r10_bio->state),
443 md_write_end(r10_bio->mddev);
446 static void one_write_done(struct r10bio *r10_bio)
448 if (atomic_dec_and_test(&r10_bio->remaining)) {
449 if (test_bit(R10BIO_WriteError, &r10_bio->state))
450 reschedule_retry(r10_bio);
452 close_write(r10_bio);
453 if (test_bit(R10BIO_MadeGood, &r10_bio->state))
454 reschedule_retry(r10_bio);
456 raid_end_bio_io(r10_bio);
461 static void raid10_end_write_request(struct bio *bio)
463 struct r10bio *r10_bio = bio->bi_private;
466 struct r10conf *conf = r10_bio->mddev->private;
468 struct md_rdev *rdev = NULL;
469 struct bio *to_put = NULL;
472 discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD;
474 dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
477 rdev = conf->mirrors[dev].replacement;
481 rdev = conf->mirrors[dev].rdev;
484 * this branch is our 'one mirror IO has finished' event handler:
486 if (bio->bi_status && !discard_error) {
488 /* Never record new bad blocks to replacement,
491 md_error(rdev->mddev, rdev);
493 set_bit(WriteErrorSeen, &rdev->flags);
494 if (!test_and_set_bit(WantReplacement, &rdev->flags))
495 set_bit(MD_RECOVERY_NEEDED,
496 &rdev->mddev->recovery);
499 if (test_bit(FailFast, &rdev->flags) &&
500 (bio->bi_opf & MD_FAILFAST)) {
501 md_error(rdev->mddev, rdev);
502 if (!test_bit(Faulty, &rdev->flags))
503 /* This is the only remaining device,
504 * We need to retry the write without
507 set_bit(R10BIO_WriteError, &r10_bio->state);
509 r10_bio->devs[slot].bio = NULL;
514 set_bit(R10BIO_WriteError, &r10_bio->state);
518 * Set R10BIO_Uptodate in our master bio, so that
519 * we will return a good error code for to the higher
520 * levels even if IO on some other mirrored buffer fails.
522 * The 'master' represents the composite IO operation to
523 * user-side. So if something waits for IO, then it will
524 * wait for the 'master' bio.
530 * Do not set R10BIO_Uptodate if the current device is
531 * rebuilding or Faulty. This is because we cannot use
532 * such device for properly reading the data back (we could
533 * potentially use it, if the current write would have felt
534 * before rdev->recovery_offset, but for simplicity we don't
537 if (test_bit(In_sync, &rdev->flags) &&
538 !test_bit(Faulty, &rdev->flags))
539 set_bit(R10BIO_Uptodate, &r10_bio->state);
541 /* Maybe we can clear some bad blocks. */
542 if (is_badblock(rdev,
543 r10_bio->devs[slot].addr,
545 &first_bad, &bad_sectors) && !discard_error) {
548 r10_bio->devs[slot].repl_bio = IO_MADE_GOOD;
550 r10_bio->devs[slot].bio = IO_MADE_GOOD;
552 set_bit(R10BIO_MadeGood, &r10_bio->state);
558 * Let's see if all mirrored write operations have finished
561 one_write_done(r10_bio);
563 rdev_dec_pending(rdev, conf->mddev);
569 * RAID10 layout manager
570 * As well as the chunksize and raid_disks count, there are two
571 * parameters: near_copies and far_copies.
572 * near_copies * far_copies must be <= raid_disks.
573 * Normally one of these will be 1.
574 * If both are 1, we get raid0.
575 * If near_copies == raid_disks, we get raid1.
577 * Chunks are laid out in raid0 style with near_copies copies of the
578 * first chunk, followed by near_copies copies of the next chunk and
580 * If far_copies > 1, then after 1/far_copies of the array has been assigned
581 * as described above, we start again with a device offset of near_copies.
582 * So we effectively have another copy of the whole array further down all
583 * the drives, but with blocks on different drives.
584 * With this layout, and block is never stored twice on the one device.
586 * raid10_find_phys finds the sector offset of a given virtual sector
587 * on each device that it is on.
589 * raid10_find_virt does the reverse mapping, from a device and a
590 * sector offset to a virtual address
593 static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio)
601 int last_far_set_start, last_far_set_size;
603 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
604 last_far_set_start *= geo->far_set_size;
606 last_far_set_size = geo->far_set_size;
607 last_far_set_size += (geo->raid_disks % geo->far_set_size);
609 /* now calculate first sector/dev */
610 chunk = r10bio->sector >> geo->chunk_shift;
611 sector = r10bio->sector & geo->chunk_mask;
613 chunk *= geo->near_copies;
615 dev = sector_div(stripe, geo->raid_disks);
617 stripe *= geo->far_copies;
619 sector += stripe << geo->chunk_shift;
621 /* and calculate all the others */
622 for (n = 0; n < geo->near_copies; n++) {
626 r10bio->devs[slot].devnum = d;
627 r10bio->devs[slot].addr = s;
630 for (f = 1; f < geo->far_copies; f++) {
631 set = d / geo->far_set_size;
632 d += geo->near_copies;
634 if ((geo->raid_disks % geo->far_set_size) &&
635 (d > last_far_set_start)) {
636 d -= last_far_set_start;
637 d %= last_far_set_size;
638 d += last_far_set_start;
640 d %= geo->far_set_size;
641 d += geo->far_set_size * set;
644 r10bio->devs[slot].devnum = d;
645 r10bio->devs[slot].addr = s;
649 if (dev >= geo->raid_disks) {
651 sector += (geo->chunk_mask + 1);
656 static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio)
658 struct geom *geo = &conf->geo;
660 if (conf->reshape_progress != MaxSector &&
661 ((r10bio->sector >= conf->reshape_progress) !=
662 conf->mddev->reshape_backwards)) {
663 set_bit(R10BIO_Previous, &r10bio->state);
666 clear_bit(R10BIO_Previous, &r10bio->state);
668 __raid10_find_phys(geo, r10bio);
671 static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev)
673 sector_t offset, chunk, vchunk;
674 /* Never use conf->prev as this is only called during resync
675 * or recovery, so reshape isn't happening
677 struct geom *geo = &conf->geo;
678 int far_set_start = (dev / geo->far_set_size) * geo->far_set_size;
679 int far_set_size = geo->far_set_size;
680 int last_far_set_start;
682 if (geo->raid_disks % geo->far_set_size) {
683 last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1;
684 last_far_set_start *= geo->far_set_size;
686 if (dev >= last_far_set_start) {
687 far_set_size = geo->far_set_size;
688 far_set_size += (geo->raid_disks % geo->far_set_size);
689 far_set_start = last_far_set_start;
693 offset = sector & geo->chunk_mask;
694 if (geo->far_offset) {
696 chunk = sector >> geo->chunk_shift;
697 fc = sector_div(chunk, geo->far_copies);
698 dev -= fc * geo->near_copies;
699 if (dev < far_set_start)
702 while (sector >= geo->stride) {
703 sector -= geo->stride;
704 if (dev < (geo->near_copies + far_set_start))
705 dev += far_set_size - geo->near_copies;
707 dev -= geo->near_copies;
709 chunk = sector >> geo->chunk_shift;
711 vchunk = chunk * geo->raid_disks + dev;
712 sector_div(vchunk, geo->near_copies);
713 return (vchunk << geo->chunk_shift) + offset;
717 * This routine returns the disk from which the requested read should
718 * be done. There is a per-array 'next expected sequential IO' sector
719 * number - if this matches on the next IO then we use the last disk.
720 * There is also a per-disk 'last know head position' sector that is
721 * maintained from IRQ contexts, both the normal and the resync IO
722 * completion handlers update this position correctly. If there is no
723 * perfect sequential match then we pick the disk whose head is closest.
725 * If there are 2 mirrors in the same 2 devices, performance degrades
726 * because position is mirror, not device based.
728 * The rdev for the device selected will have nr_pending incremented.
732 * FIXME: possibly should rethink readbalancing and do it differently
733 * depending on near_copies / far_copies geometry.
735 static struct md_rdev *read_balance(struct r10conf *conf,
736 struct r10bio *r10_bio,
739 const sector_t this_sector = r10_bio->sector;
741 int sectors = r10_bio->sectors;
742 int best_good_sectors;
743 sector_t new_distance, best_dist;
744 struct md_rdev *best_rdev, *rdev = NULL;
747 struct geom *geo = &conf->geo;
749 raid10_find_phys(conf, r10_bio);
751 sectors = r10_bio->sectors;
754 best_dist = MaxSector;
755 best_good_sectors = 0;
757 clear_bit(R10BIO_FailFast, &r10_bio->state);
759 * Check if we can balance. We can balance on the whole
760 * device if no resync is going on (recovery is ok), or below
761 * the resync window. We take the first readable disk when
762 * above the resync window.
764 if (conf->mddev->recovery_cp < MaxSector
765 && (this_sector + sectors >= conf->next_resync))
768 for (slot = 0; slot < conf->copies ; slot++) {
773 if (r10_bio->devs[slot].bio == IO_BLOCKED)
775 disk = r10_bio->devs[slot].devnum;
776 rdev = rcu_dereference(conf->mirrors[disk].replacement);
777 if (rdev == NULL || test_bit(Faulty, &rdev->flags) ||
778 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
779 rdev = rcu_dereference(conf->mirrors[disk].rdev);
781 test_bit(Faulty, &rdev->flags))
783 if (!test_bit(In_sync, &rdev->flags) &&
784 r10_bio->devs[slot].addr + sectors > rdev->recovery_offset)
787 dev_sector = r10_bio->devs[slot].addr;
788 if (is_badblock(rdev, dev_sector, sectors,
789 &first_bad, &bad_sectors)) {
790 if (best_dist < MaxSector)
791 /* Already have a better slot */
793 if (first_bad <= dev_sector) {
794 /* Cannot read here. If this is the
795 * 'primary' device, then we must not read
796 * beyond 'bad_sectors' from another device.
798 bad_sectors -= (dev_sector - first_bad);
799 if (!do_balance && sectors > bad_sectors)
800 sectors = bad_sectors;
801 if (best_good_sectors > sectors)
802 best_good_sectors = sectors;
804 sector_t good_sectors =
805 first_bad - dev_sector;
806 if (good_sectors > best_good_sectors) {
807 best_good_sectors = good_sectors;
812 /* Must read from here */
817 best_good_sectors = sectors;
823 /* At least 2 disks to choose from so failfast is OK */
824 set_bit(R10BIO_FailFast, &r10_bio->state);
825 /* This optimisation is debatable, and completely destroys
826 * sequential read speed for 'far copies' arrays. So only
827 * keep it for 'near' arrays, and review those later.
829 if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending))
832 /* for far > 1 always use the lowest address */
833 else if (geo->far_copies > 1)
834 new_distance = r10_bio->devs[slot].addr;
836 new_distance = abs(r10_bio->devs[slot].addr -
837 conf->mirrors[disk].head_position);
838 if (new_distance < best_dist) {
839 best_dist = new_distance;
844 if (slot >= conf->copies) {
850 atomic_inc(&rdev->nr_pending);
851 r10_bio->read_slot = slot;
855 *max_sectors = best_good_sectors;
860 static int raid10_congested(struct mddev *mddev, int bits)
862 struct r10conf *conf = mddev->private;
865 if ((bits & (1 << WB_async_congested)) &&
866 conf->pending_count >= max_queued_requests)
871 (i < conf->geo.raid_disks || i < conf->prev.raid_disks)
874 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
875 if (rdev && !test_bit(Faulty, &rdev->flags)) {
876 struct request_queue *q = bdev_get_queue(rdev->bdev);
878 ret |= bdi_congested(q->backing_dev_info, bits);
885 static void flush_pending_writes(struct r10conf *conf)
887 /* Any writes that have been queued but are awaiting
888 * bitmap updates get flushed here.
890 spin_lock_irq(&conf->device_lock);
892 if (conf->pending_bio_list.head) {
893 struct blk_plug plug;
896 bio = bio_list_get(&conf->pending_bio_list);
897 conf->pending_count = 0;
898 spin_unlock_irq(&conf->device_lock);
899 blk_start_plug(&plug);
900 /* flush any pending bitmap writes to disk
901 * before proceeding w/ I/O */
902 bitmap_unplug(conf->mddev->bitmap);
903 wake_up(&conf->wait_barrier);
905 while (bio) { /* submit pending writes */
906 struct bio *next = bio->bi_next;
907 struct md_rdev *rdev = (void*)bio->bi_disk;
909 bio_set_dev(bio, rdev->bdev);
910 if (test_bit(Faulty, &rdev->flags)) {
912 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
913 !blk_queue_discard(bio->bi_disk->queue)))
917 generic_make_request(bio);
920 blk_finish_plug(&plug);
922 spin_unlock_irq(&conf->device_lock);
926 * Sometimes we need to suspend IO while we do something else,
927 * either some resync/recovery, or reconfigure the array.
928 * To do this we raise a 'barrier'.
929 * The 'barrier' is a counter that can be raised multiple times
930 * to count how many activities are happening which preclude
932 * We can only raise the barrier if there is no pending IO.
933 * i.e. if nr_pending == 0.
934 * We choose only to raise the barrier if no-one is waiting for the
935 * barrier to go down. This means that as soon as an IO request
936 * is ready, no other operations which require a barrier will start
937 * until the IO request has had a chance.
939 * So: regular IO calls 'wait_barrier'. When that returns there
940 * is no backgroup IO happening, It must arrange to call
941 * allow_barrier when it has finished its IO.
942 * backgroup IO calls must call raise_barrier. Once that returns
943 * there is no normal IO happeing. It must arrange to call
944 * lower_barrier when the particular background IO completes.
947 static void raise_barrier(struct r10conf *conf, int force)
949 BUG_ON(force && !conf->barrier);
950 spin_lock_irq(&conf->resync_lock);
952 /* Wait until no block IO is waiting (unless 'force') */
953 wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting,
956 /* block any new IO from starting */
959 /* Now wait for all pending IO to complete */
960 wait_event_lock_irq(conf->wait_barrier,
961 !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH,
964 spin_unlock_irq(&conf->resync_lock);
967 static void lower_barrier(struct r10conf *conf)
970 spin_lock_irqsave(&conf->resync_lock, flags);
972 spin_unlock_irqrestore(&conf->resync_lock, flags);
973 wake_up(&conf->wait_barrier);
976 static void wait_barrier(struct r10conf *conf)
978 spin_lock_irq(&conf->resync_lock);
981 /* Wait for the barrier to drop.
982 * However if there are already pending
983 * requests (preventing the barrier from
984 * rising completely), and the
985 * pre-process bio queue isn't empty,
986 * then don't wait, as we need to empty
987 * that queue to get the nr_pending
990 raid10_log(conf->mddev, "wait barrier");
991 wait_event_lock_irq(conf->wait_barrier,
993 (atomic_read(&conf->nr_pending) &&
995 (!bio_list_empty(¤t->bio_list[0]) ||
996 !bio_list_empty(¤t->bio_list[1]))),
999 if (!conf->nr_waiting)
1000 wake_up(&conf->wait_barrier);
1002 atomic_inc(&conf->nr_pending);
1003 spin_unlock_irq(&conf->resync_lock);
1006 static void allow_barrier(struct r10conf *conf)
1008 if ((atomic_dec_and_test(&conf->nr_pending)) ||
1009 (conf->array_freeze_pending))
1010 wake_up(&conf->wait_barrier);
1013 static void freeze_array(struct r10conf *conf, int extra)
1015 /* stop syncio and normal IO and wait for everything to
1017 * We increment barrier and nr_waiting, and then
1018 * wait until nr_pending match nr_queued+extra
1019 * This is called in the context of one normal IO request
1020 * that has failed. Thus any sync request that might be pending
1021 * will be blocked by nr_pending, and we need to wait for
1022 * pending IO requests to complete or be queued for re-try.
1023 * Thus the number queued (nr_queued) plus this request (extra)
1024 * must match the number of pending IOs (nr_pending) before
1027 spin_lock_irq(&conf->resync_lock);
1028 conf->array_freeze_pending++;
1031 wait_event_lock_irq_cmd(conf->wait_barrier,
1032 atomic_read(&conf->nr_pending) == conf->nr_queued+extra,
1034 flush_pending_writes(conf));
1036 conf->array_freeze_pending--;
1037 spin_unlock_irq(&conf->resync_lock);
1040 static void unfreeze_array(struct r10conf *conf)
1042 /* reverse the effect of the freeze */
1043 spin_lock_irq(&conf->resync_lock);
1046 wake_up(&conf->wait_barrier);
1047 spin_unlock_irq(&conf->resync_lock);
1050 static sector_t choose_data_offset(struct r10bio *r10_bio,
1051 struct md_rdev *rdev)
1053 if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) ||
1054 test_bit(R10BIO_Previous, &r10_bio->state))
1055 return rdev->data_offset;
1057 return rdev->new_data_offset;
1060 struct raid10_plug_cb {
1061 struct blk_plug_cb cb;
1062 struct bio_list pending;
1066 static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule)
1068 struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb,
1070 struct mddev *mddev = plug->cb.data;
1071 struct r10conf *conf = mddev->private;
1074 if (from_schedule || current->bio_list) {
1075 spin_lock_irq(&conf->device_lock);
1076 bio_list_merge(&conf->pending_bio_list, &plug->pending);
1077 conf->pending_count += plug->pending_cnt;
1078 spin_unlock_irq(&conf->device_lock);
1079 wake_up(&conf->wait_barrier);
1080 md_wakeup_thread(mddev->thread);
1085 /* we aren't scheduling, so we can do the write-out directly. */
1086 bio = bio_list_get(&plug->pending);
1087 bitmap_unplug(mddev->bitmap);
1088 wake_up(&conf->wait_barrier);
1090 while (bio) { /* submit pending writes */
1091 struct bio *next = bio->bi_next;
1092 struct md_rdev *rdev = (void*)bio->bi_disk;
1093 bio->bi_next = NULL;
1094 bio_set_dev(bio, rdev->bdev);
1095 if (test_bit(Faulty, &rdev->flags)) {
1097 } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) &&
1098 !blk_queue_discard(bio->bi_disk->queue)))
1099 /* Just ignore it */
1102 generic_make_request(bio);
1108 static void raid10_read_request(struct mddev *mddev, struct bio *bio,
1109 struct r10bio *r10_bio)
1111 struct r10conf *conf = mddev->private;
1112 struct bio *read_bio;
1113 const int op = bio_op(bio);
1114 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1117 struct md_rdev *rdev;
1118 char b[BDEVNAME_SIZE];
1119 int slot = r10_bio->read_slot;
1120 struct md_rdev *err_rdev = NULL;
1121 gfp_t gfp = GFP_NOIO;
1123 if (slot >= 0 && r10_bio->devs[slot].rdev) {
1125 * This is an error retry, but we cannot
1126 * safely dereference the rdev in the r10_bio,
1127 * we must use the one in conf.
1128 * If it has already been disconnected (unlikely)
1129 * we lose the device name in error messages.
1133 * As we are blocking raid10, it is a little safer to
1136 gfp = GFP_NOIO | __GFP_HIGH;
1139 disk = r10_bio->devs[slot].devnum;
1140 err_rdev = rcu_dereference(conf->mirrors[disk].rdev);
1142 bdevname(err_rdev->bdev, b);
1145 /* This never gets dereferenced */
1146 err_rdev = r10_bio->devs[slot].rdev;
1151 * Register the new request and wait if the reconstruction
1152 * thread has put up a bar for new requests.
1153 * Continue immediately if no resync is active currently.
1157 sectors = r10_bio->sectors;
1158 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1159 bio->bi_iter.bi_sector < conf->reshape_progress &&
1160 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1162 * IO spans the reshape position. Need to wait for reshape to
1165 raid10_log(conf->mddev, "wait reshape");
1166 allow_barrier(conf);
1167 wait_event(conf->wait_barrier,
1168 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1169 conf->reshape_progress >= bio->bi_iter.bi_sector +
1174 rdev = read_balance(conf, r10_bio, &max_sectors);
1177 pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n",
1179 (unsigned long long)r10_bio->sector);
1181 raid_end_bio_io(r10_bio);
1185 pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n",
1187 bdevname(rdev->bdev, b),
1188 (unsigned long long)r10_bio->sector);
1189 if (max_sectors < bio_sectors(bio)) {
1190 struct bio *split = bio_split(bio, max_sectors,
1191 gfp, conf->bio_split);
1192 bio_chain(split, bio);
1193 allow_barrier(conf);
1194 generic_make_request(bio);
1197 r10_bio->master_bio = bio;
1198 r10_bio->sectors = max_sectors;
1200 slot = r10_bio->read_slot;
1202 read_bio = bio_clone_fast(bio, gfp, mddev->bio_set);
1204 r10_bio->devs[slot].bio = read_bio;
1205 r10_bio->devs[slot].rdev = rdev;
1207 read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr +
1208 choose_data_offset(r10_bio, rdev);
1209 bio_set_dev(read_bio, rdev->bdev);
1210 read_bio->bi_end_io = raid10_end_read_request;
1211 bio_set_op_attrs(read_bio, op, do_sync);
1212 if (test_bit(FailFast, &rdev->flags) &&
1213 test_bit(R10BIO_FailFast, &r10_bio->state))
1214 read_bio->bi_opf |= MD_FAILFAST;
1215 read_bio->bi_private = r10_bio;
1218 trace_block_bio_remap(read_bio->bi_disk->queue,
1219 read_bio, disk_devt(mddev->gendisk),
1221 generic_make_request(read_bio);
1225 static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio,
1226 struct bio *bio, bool replacement,
1229 const int op = bio_op(bio);
1230 const unsigned long do_sync = (bio->bi_opf & REQ_SYNC);
1231 const unsigned long do_fua = (bio->bi_opf & REQ_FUA);
1232 unsigned long flags;
1233 struct blk_plug_cb *cb;
1234 struct raid10_plug_cb *plug = NULL;
1235 struct r10conf *conf = mddev->private;
1236 struct md_rdev *rdev;
1237 int devnum = r10_bio->devs[n_copy].devnum;
1241 rdev = conf->mirrors[devnum].replacement;
1243 /* Replacement just got moved to main 'rdev' */
1245 rdev = conf->mirrors[devnum].rdev;
1248 rdev = conf->mirrors[devnum].rdev;
1250 mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
1252 r10_bio->devs[n_copy].repl_bio = mbio;
1254 r10_bio->devs[n_copy].bio = mbio;
1256 mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr +
1257 choose_data_offset(r10_bio, rdev));
1258 bio_set_dev(mbio, rdev->bdev);
1259 mbio->bi_end_io = raid10_end_write_request;
1260 bio_set_op_attrs(mbio, op, do_sync | do_fua);
1261 if (!replacement && test_bit(FailFast,
1262 &conf->mirrors[devnum].rdev->flags)
1263 && enough(conf, devnum))
1264 mbio->bi_opf |= MD_FAILFAST;
1265 mbio->bi_private = r10_bio;
1267 if (conf->mddev->gendisk)
1268 trace_block_bio_remap(mbio->bi_disk->queue,
1269 mbio, disk_devt(conf->mddev->gendisk),
1271 /* flush_pending_writes() needs access to the rdev so...*/
1272 mbio->bi_disk = (void *)rdev;
1274 atomic_inc(&r10_bio->remaining);
1276 cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug));
1278 plug = container_of(cb, struct raid10_plug_cb, cb);
1282 bio_list_add(&plug->pending, mbio);
1283 plug->pending_cnt++;
1285 spin_lock_irqsave(&conf->device_lock, flags);
1286 bio_list_add(&conf->pending_bio_list, mbio);
1287 conf->pending_count++;
1288 spin_unlock_irqrestore(&conf->device_lock, flags);
1289 md_wakeup_thread(mddev->thread);
1293 static void raid10_write_request(struct mddev *mddev, struct bio *bio,
1294 struct r10bio *r10_bio)
1296 struct r10conf *conf = mddev->private;
1298 struct md_rdev *blocked_rdev;
1303 * Register the new request and wait if the reconstruction
1304 * thread has put up a bar for new requests.
1305 * Continue immediately if no resync is active currently.
1309 sectors = r10_bio->sectors;
1310 while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1311 bio->bi_iter.bi_sector < conf->reshape_progress &&
1312 bio->bi_iter.bi_sector + sectors > conf->reshape_progress) {
1314 * IO spans the reshape position. Need to wait for reshape to
1317 raid10_log(conf->mddev, "wait reshape");
1318 allow_barrier(conf);
1319 wait_event(conf->wait_barrier,
1320 conf->reshape_progress <= bio->bi_iter.bi_sector ||
1321 conf->reshape_progress >= bio->bi_iter.bi_sector +
1326 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
1327 (mddev->reshape_backwards
1328 ? (bio->bi_iter.bi_sector < conf->reshape_safe &&
1329 bio->bi_iter.bi_sector + sectors > conf->reshape_progress)
1330 : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe &&
1331 bio->bi_iter.bi_sector < conf->reshape_progress))) {
1332 /* Need to update reshape_position in metadata */
1333 mddev->reshape_position = conf->reshape_progress;
1334 set_mask_bits(&mddev->sb_flags, 0,
1335 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1336 md_wakeup_thread(mddev->thread);
1337 raid10_log(conf->mddev, "wait reshape metadata");
1338 wait_event(mddev->sb_wait,
1339 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags));
1341 conf->reshape_safe = mddev->reshape_position;
1344 if (conf->pending_count >= max_queued_requests) {
1345 md_wakeup_thread(mddev->thread);
1346 raid10_log(mddev, "wait queued");
1347 wait_event(conf->wait_barrier,
1348 conf->pending_count < max_queued_requests);
1350 /* first select target devices under rcu_lock and
1351 * inc refcount on their rdev. Record them by setting
1353 * If there are known/acknowledged bad blocks on any device
1354 * on which we have seen a write error, we want to avoid
1355 * writing to those blocks. This potentially requires several
1356 * writes to write around the bad blocks. Each set of writes
1357 * gets its own r10_bio with a set of bios attached.
1360 r10_bio->read_slot = -1; /* make sure repl_bio gets freed */
1361 raid10_find_phys(conf, r10_bio);
1363 blocked_rdev = NULL;
1365 max_sectors = r10_bio->sectors;
1367 for (i = 0; i < conf->copies; i++) {
1368 int d = r10_bio->devs[i].devnum;
1369 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
1370 struct md_rdev *rrdev = rcu_dereference(
1371 conf->mirrors[d].replacement);
1374 if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) {
1375 atomic_inc(&rdev->nr_pending);
1376 blocked_rdev = rdev;
1379 if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) {
1380 atomic_inc(&rrdev->nr_pending);
1381 blocked_rdev = rrdev;
1384 if (rdev && (test_bit(Faulty, &rdev->flags)))
1386 if (rrdev && (test_bit(Faulty, &rrdev->flags)))
1389 r10_bio->devs[i].bio = NULL;
1390 r10_bio->devs[i].repl_bio = NULL;
1392 if (!rdev && !rrdev) {
1393 set_bit(R10BIO_Degraded, &r10_bio->state);
1396 if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) {
1398 sector_t dev_sector = r10_bio->devs[i].addr;
1402 is_bad = is_badblock(rdev, dev_sector, max_sectors,
1403 &first_bad, &bad_sectors);
1405 /* Mustn't write here until the bad block
1408 atomic_inc(&rdev->nr_pending);
1409 set_bit(BlockedBadBlocks, &rdev->flags);
1410 blocked_rdev = rdev;
1413 if (is_bad && first_bad <= dev_sector) {
1414 /* Cannot write here at all */
1415 bad_sectors -= (dev_sector - first_bad);
1416 if (bad_sectors < max_sectors)
1417 /* Mustn't write more than bad_sectors
1418 * to other devices yet
1420 max_sectors = bad_sectors;
1421 /* We don't set R10BIO_Degraded as that
1422 * only applies if the disk is missing,
1423 * so it might be re-added, and we want to
1424 * know to recover this chunk.
1425 * In this case the device is here, and the
1426 * fact that this chunk is not in-sync is
1427 * recorded in the bad block log.
1432 int good_sectors = first_bad - dev_sector;
1433 if (good_sectors < max_sectors)
1434 max_sectors = good_sectors;
1438 r10_bio->devs[i].bio = bio;
1439 atomic_inc(&rdev->nr_pending);
1442 r10_bio->devs[i].repl_bio = bio;
1443 atomic_inc(&rrdev->nr_pending);
1448 if (unlikely(blocked_rdev)) {
1449 /* Have to wait for this device to get unblocked, then retry */
1453 for (j = 0; j < i; j++) {
1454 if (r10_bio->devs[j].bio) {
1455 d = r10_bio->devs[j].devnum;
1456 rdev_dec_pending(conf->mirrors[d].rdev, mddev);
1458 if (r10_bio->devs[j].repl_bio) {
1459 struct md_rdev *rdev;
1460 d = r10_bio->devs[j].devnum;
1461 rdev = conf->mirrors[d].replacement;
1463 /* Race with remove_disk */
1465 rdev = conf->mirrors[d].rdev;
1467 rdev_dec_pending(rdev, mddev);
1470 allow_barrier(conf);
1471 raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk);
1472 md_wait_for_blocked_rdev(blocked_rdev, mddev);
1477 if (max_sectors < r10_bio->sectors)
1478 r10_bio->sectors = max_sectors;
1480 if (r10_bio->sectors < bio_sectors(bio)) {
1481 struct bio *split = bio_split(bio, r10_bio->sectors,
1482 GFP_NOIO, conf->bio_split);
1483 bio_chain(split, bio);
1484 allow_barrier(conf);
1485 generic_make_request(bio);
1488 r10_bio->master_bio = bio;
1491 atomic_set(&r10_bio->remaining, 1);
1492 bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0);
1494 for (i = 0; i < conf->copies; i++) {
1495 if (r10_bio->devs[i].bio)
1496 raid10_write_one_disk(mddev, r10_bio, bio, false, i);
1497 if (r10_bio->devs[i].repl_bio)
1498 raid10_write_one_disk(mddev, r10_bio, bio, true, i);
1500 one_write_done(r10_bio);
1503 static void __make_request(struct mddev *mddev, struct bio *bio, int sectors)
1505 struct r10conf *conf = mddev->private;
1506 struct r10bio *r10_bio;
1508 r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO);
1510 r10_bio->master_bio = bio;
1511 r10_bio->sectors = sectors;
1513 r10_bio->mddev = mddev;
1514 r10_bio->sector = bio->bi_iter.bi_sector;
1516 r10_bio->read_slot = -1;
1517 memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies);
1519 if (bio_data_dir(bio) == READ)
1520 raid10_read_request(mddev, bio, r10_bio);
1522 raid10_write_request(mddev, bio, r10_bio);
1525 static bool raid10_make_request(struct mddev *mddev, struct bio *bio)
1527 struct r10conf *conf = mddev->private;
1528 sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask);
1529 int chunk_sects = chunk_mask + 1;
1530 int sectors = bio_sectors(bio);
1532 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1533 md_flush_request(mddev, bio);
1537 if (!md_write_start(mddev, bio))
1541 * If this request crosses a chunk boundary, we need to split
1544 if (unlikely((bio->bi_iter.bi_sector & chunk_mask) +
1545 sectors > chunk_sects
1546 && (conf->geo.near_copies < conf->geo.raid_disks
1547 || conf->prev.near_copies <
1548 conf->prev.raid_disks)))
1549 sectors = chunk_sects -
1550 (bio->bi_iter.bi_sector &
1552 __make_request(mddev, bio, sectors);
1554 /* In case raid10d snuck in to freeze_array */
1555 wake_up(&conf->wait_barrier);
1559 static void raid10_status(struct seq_file *seq, struct mddev *mddev)
1561 struct r10conf *conf = mddev->private;
1564 if (conf->geo.near_copies < conf->geo.raid_disks)
1565 seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2);
1566 if (conf->geo.near_copies > 1)
1567 seq_printf(seq, " %d near-copies", conf->geo.near_copies);
1568 if (conf->geo.far_copies > 1) {
1569 if (conf->geo.far_offset)
1570 seq_printf(seq, " %d offset-copies", conf->geo.far_copies);
1572 seq_printf(seq, " %d far-copies", conf->geo.far_copies);
1573 if (conf->geo.far_set_size != conf->geo.raid_disks)
1574 seq_printf(seq, " %d devices per set", conf->geo.far_set_size);
1576 seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks,
1577 conf->geo.raid_disks - mddev->degraded);
1579 for (i = 0; i < conf->geo.raid_disks; i++) {
1580 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
1581 seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
1584 seq_printf(seq, "]");
1587 /* check if there are enough drives for
1588 * every block to appear on atleast one.
1589 * Don't consider the device numbered 'ignore'
1590 * as we might be about to remove it.
1592 static int _enough(struct r10conf *conf, int previous, int ignore)
1598 disks = conf->prev.raid_disks;
1599 ncopies = conf->prev.near_copies;
1601 disks = conf->geo.raid_disks;
1602 ncopies = conf->geo.near_copies;
1607 int n = conf->copies;
1611 struct md_rdev *rdev;
1612 if (this != ignore &&
1613 (rdev = rcu_dereference(conf->mirrors[this].rdev)) &&
1614 test_bit(In_sync, &rdev->flags))
1616 this = (this+1) % disks;
1620 first = (first + ncopies) % disks;
1621 } while (first != 0);
1628 static int enough(struct r10conf *conf, int ignore)
1630 /* when calling 'enough', both 'prev' and 'geo' must
1632 * This is ensured if ->reconfig_mutex or ->device_lock
1635 return _enough(conf, 0, ignore) &&
1636 _enough(conf, 1, ignore);
1639 static void raid10_error(struct mddev *mddev, struct md_rdev *rdev)
1641 char b[BDEVNAME_SIZE];
1642 struct r10conf *conf = mddev->private;
1643 unsigned long flags;
1646 * If it is not operational, then we have already marked it as dead
1647 * else if it is the last working disks, ignore the error, let the
1648 * next level up know.
1649 * else mark the drive as failed
1651 spin_lock_irqsave(&conf->device_lock, flags);
1652 if (test_bit(In_sync, &rdev->flags)
1653 && !enough(conf, rdev->raid_disk)) {
1655 * Don't fail the drive, just return an IO error.
1657 spin_unlock_irqrestore(&conf->device_lock, flags);
1660 if (test_and_clear_bit(In_sync, &rdev->flags))
1663 * If recovery is running, make sure it aborts.
1665 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1666 set_bit(Blocked, &rdev->flags);
1667 set_bit(Faulty, &rdev->flags);
1668 set_mask_bits(&mddev->sb_flags, 0,
1669 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
1670 spin_unlock_irqrestore(&conf->device_lock, flags);
1671 pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n"
1672 "md/raid10:%s: Operation continuing on %d devices.\n",
1673 mdname(mddev), bdevname(rdev->bdev, b),
1674 mdname(mddev), conf->geo.raid_disks - mddev->degraded);
1677 static void print_conf(struct r10conf *conf)
1680 struct md_rdev *rdev;
1682 pr_debug("RAID10 conf printout:\n");
1684 pr_debug("(!conf)\n");
1687 pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded,
1688 conf->geo.raid_disks);
1690 /* This is only called with ->reconfix_mutex held, so
1691 * rcu protection of rdev is not needed */
1692 for (i = 0; i < conf->geo.raid_disks; i++) {
1693 char b[BDEVNAME_SIZE];
1694 rdev = conf->mirrors[i].rdev;
1696 pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n",
1697 i, !test_bit(In_sync, &rdev->flags),
1698 !test_bit(Faulty, &rdev->flags),
1699 bdevname(rdev->bdev,b));
1703 static void close_sync(struct r10conf *conf)
1706 allow_barrier(conf);
1708 mempool_destroy(conf->r10buf_pool);
1709 conf->r10buf_pool = NULL;
1712 static int raid10_spare_active(struct mddev *mddev)
1715 struct r10conf *conf = mddev->private;
1716 struct raid10_info *tmp;
1718 unsigned long flags;
1721 * Find all non-in_sync disks within the RAID10 configuration
1722 * and mark them in_sync
1724 for (i = 0; i < conf->geo.raid_disks; i++) {
1725 tmp = conf->mirrors + i;
1726 if (tmp->replacement
1727 && tmp->replacement->recovery_offset == MaxSector
1728 && !test_bit(Faulty, &tmp->replacement->flags)
1729 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
1730 /* Replacement has just become active */
1732 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
1735 /* Replaced device not technically faulty,
1736 * but we need to be sure it gets removed
1737 * and never re-added.
1739 set_bit(Faulty, &tmp->rdev->flags);
1740 sysfs_notify_dirent_safe(
1741 tmp->rdev->sysfs_state);
1743 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
1744 } else if (tmp->rdev
1745 && tmp->rdev->recovery_offset == MaxSector
1746 && !test_bit(Faulty, &tmp->rdev->flags)
1747 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
1749 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
1752 spin_lock_irqsave(&conf->device_lock, flags);
1753 mddev->degraded -= count;
1754 spin_unlock_irqrestore(&conf->device_lock, flags);
1760 static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev)
1762 struct r10conf *conf = mddev->private;
1766 int last = conf->geo.raid_disks - 1;
1768 if (mddev->recovery_cp < MaxSector)
1769 /* only hot-add to in-sync arrays, as recovery is
1770 * very different from resync
1773 if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1))
1776 if (md_integrity_add_rdev(rdev, mddev))
1779 if (rdev->raid_disk >= 0)
1780 first = last = rdev->raid_disk;
1782 if (rdev->saved_raid_disk >= first &&
1783 rdev->saved_raid_disk < conf->geo.raid_disks &&
1784 conf->mirrors[rdev->saved_raid_disk].rdev == NULL)
1785 mirror = rdev->saved_raid_disk;
1788 for ( ; mirror <= last ; mirror++) {
1789 struct raid10_info *p = &conf->mirrors[mirror];
1790 if (p->recovery_disabled == mddev->recovery_disabled)
1793 if (!test_bit(WantReplacement, &p->rdev->flags) ||
1794 p->replacement != NULL)
1796 clear_bit(In_sync, &rdev->flags);
1797 set_bit(Replacement, &rdev->flags);
1798 rdev->raid_disk = mirror;
1801 disk_stack_limits(mddev->gendisk, rdev->bdev,
1802 rdev->data_offset << 9);
1804 rcu_assign_pointer(p->replacement, rdev);
1809 disk_stack_limits(mddev->gendisk, rdev->bdev,
1810 rdev->data_offset << 9);
1812 p->head_position = 0;
1813 p->recovery_disabled = mddev->recovery_disabled - 1;
1814 rdev->raid_disk = mirror;
1816 if (rdev->saved_raid_disk != mirror)
1818 rcu_assign_pointer(p->rdev, rdev);
1821 if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev)))
1822 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue);
1828 static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
1830 struct r10conf *conf = mddev->private;
1832 int number = rdev->raid_disk;
1833 struct md_rdev **rdevp;
1834 struct raid10_info *p = conf->mirrors + number;
1837 if (rdev == p->rdev)
1839 else if (rdev == p->replacement)
1840 rdevp = &p->replacement;
1844 if (test_bit(In_sync, &rdev->flags) ||
1845 atomic_read(&rdev->nr_pending)) {
1849 /* Only remove non-faulty devices if recovery
1852 if (!test_bit(Faulty, &rdev->flags) &&
1853 mddev->recovery_disabled != p->recovery_disabled &&
1854 (!p->replacement || p->replacement == rdev) &&
1855 number < conf->geo.raid_disks &&
1861 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
1863 if (atomic_read(&rdev->nr_pending)) {
1864 /* lost the race, try later */
1870 if (p->replacement) {
1871 /* We must have just cleared 'rdev' */
1872 p->rdev = p->replacement;
1873 clear_bit(Replacement, &p->replacement->flags);
1874 smp_mb(); /* Make sure other CPUs may see both as identical
1875 * but will never see neither -- if they are careful.
1877 p->replacement = NULL;
1880 clear_bit(WantReplacement, &rdev->flags);
1881 err = md_integrity_register(mddev);
1889 static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d)
1891 struct r10conf *conf = r10_bio->mddev->private;
1893 if (!bio->bi_status)
1894 set_bit(R10BIO_Uptodate, &r10_bio->state);
1896 /* The write handler will notice the lack of
1897 * R10BIO_Uptodate and record any errors etc
1899 atomic_add(r10_bio->sectors,
1900 &conf->mirrors[d].rdev->corrected_errors);
1902 /* for reconstruct, we always reschedule after a read.
1903 * for resync, only after all reads
1905 rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev);
1906 if (test_bit(R10BIO_IsRecover, &r10_bio->state) ||
1907 atomic_dec_and_test(&r10_bio->remaining)) {
1908 /* we have read all the blocks,
1909 * do the comparison in process context in raid10d
1911 reschedule_retry(r10_bio);
1915 static void end_sync_read(struct bio *bio)
1917 struct r10bio *r10_bio = get_resync_r10bio(bio);
1918 struct r10conf *conf = r10_bio->mddev->private;
1919 int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL);
1921 __end_sync_read(r10_bio, bio, d);
1924 static void end_reshape_read(struct bio *bio)
1926 /* reshape read bio isn't allocated from r10buf_pool */
1927 struct r10bio *r10_bio = bio->bi_private;
1929 __end_sync_read(r10_bio, bio, r10_bio->read_slot);
1932 static void end_sync_request(struct r10bio *r10_bio)
1934 struct mddev *mddev = r10_bio->mddev;
1936 while (atomic_dec_and_test(&r10_bio->remaining)) {
1937 if (r10_bio->master_bio == NULL) {
1938 /* the primary of several recovery bios */
1939 sector_t s = r10_bio->sectors;
1940 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1941 test_bit(R10BIO_WriteError, &r10_bio->state))
1942 reschedule_retry(r10_bio);
1945 md_done_sync(mddev, s, 1);
1948 struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio;
1949 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
1950 test_bit(R10BIO_WriteError, &r10_bio->state))
1951 reschedule_retry(r10_bio);
1959 static void end_sync_write(struct bio *bio)
1961 struct r10bio *r10_bio = get_resync_r10bio(bio);
1962 struct mddev *mddev = r10_bio->mddev;
1963 struct r10conf *conf = mddev->private;
1969 struct md_rdev *rdev = NULL;
1971 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
1973 rdev = conf->mirrors[d].replacement;
1975 rdev = conf->mirrors[d].rdev;
1977 if (bio->bi_status) {
1979 md_error(mddev, rdev);
1981 set_bit(WriteErrorSeen, &rdev->flags);
1982 if (!test_and_set_bit(WantReplacement, &rdev->flags))
1983 set_bit(MD_RECOVERY_NEEDED,
1984 &rdev->mddev->recovery);
1985 set_bit(R10BIO_WriteError, &r10_bio->state);
1987 } else if (is_badblock(rdev,
1988 r10_bio->devs[slot].addr,
1990 &first_bad, &bad_sectors))
1991 set_bit(R10BIO_MadeGood, &r10_bio->state);
1993 rdev_dec_pending(rdev, mddev);
1995 end_sync_request(r10_bio);
1999 * Note: sync and recover and handled very differently for raid10
2000 * This code is for resync.
2001 * For resync, we read through virtual addresses and read all blocks.
2002 * If there is any error, we schedule a write. The lowest numbered
2003 * drive is authoritative.
2004 * However requests come for physical address, so we need to map.
2005 * For every physical address there are raid_disks/copies virtual addresses,
2006 * which is always are least one, but is not necessarly an integer.
2007 * This means that a physical address can span multiple chunks, so we may
2008 * have to submit multiple io requests for a single sync request.
2011 * We check if all blocks are in-sync and only write to blocks that
2014 static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2016 struct r10conf *conf = mddev->private;
2018 struct bio *tbio, *fbio;
2020 struct page **tpages, **fpages;
2022 atomic_set(&r10_bio->remaining, 1);
2024 /* find the first device with a block */
2025 for (i=0; i<conf->copies; i++)
2026 if (!r10_bio->devs[i].bio->bi_status)
2029 if (i == conf->copies)
2033 fbio = r10_bio->devs[i].bio;
2034 fbio->bi_iter.bi_size = r10_bio->sectors << 9;
2035 fbio->bi_iter.bi_idx = 0;
2036 fpages = get_resync_pages(fbio)->pages;
2038 vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9);
2039 /* now find blocks with errors */
2040 for (i=0 ; i < conf->copies ; i++) {
2042 struct md_rdev *rdev;
2043 struct resync_pages *rp;
2045 tbio = r10_bio->devs[i].bio;
2047 if (tbio->bi_end_io != end_sync_read)
2052 tpages = get_resync_pages(tbio)->pages;
2053 d = r10_bio->devs[i].devnum;
2054 rdev = conf->mirrors[d].rdev;
2055 if (!r10_bio->devs[i].bio->bi_status) {
2056 /* We know that the bi_io_vec layout is the same for
2057 * both 'first' and 'i', so we just compare them.
2058 * All vec entries are PAGE_SIZE;
2060 int sectors = r10_bio->sectors;
2061 for (j = 0; j < vcnt; j++) {
2062 int len = PAGE_SIZE;
2063 if (sectors < (len / 512))
2064 len = sectors * 512;
2065 if (memcmp(page_address(fpages[j]),
2066 page_address(tpages[j]),
2073 atomic64_add(r10_bio->sectors, &mddev->resync_mismatches);
2074 if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery))
2075 /* Don't fix anything. */
2077 } else if (test_bit(FailFast, &rdev->flags)) {
2078 /* Just give up on this device */
2079 md_error(rdev->mddev, rdev);
2082 /* Ok, we need to write this bio, either to correct an
2083 * inconsistency or to correct an unreadable block.
2084 * First we need to fixup bv_offset, bv_len and
2085 * bi_vecs, as the read request might have corrupted these
2087 rp = get_resync_pages(tbio);
2090 md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size);
2092 rp->raid_bio = r10_bio;
2093 tbio->bi_private = rp;
2094 tbio->bi_iter.bi_sector = r10_bio->devs[i].addr;
2095 tbio->bi_end_io = end_sync_write;
2096 bio_set_op_attrs(tbio, REQ_OP_WRITE, 0);
2098 bio_copy_data(tbio, fbio);
2100 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2101 atomic_inc(&r10_bio->remaining);
2102 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio));
2104 if (test_bit(FailFast, &conf->mirrors[d].rdev->flags))
2105 tbio->bi_opf |= MD_FAILFAST;
2106 tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset;
2107 bio_set_dev(tbio, conf->mirrors[d].rdev->bdev);
2108 generic_make_request(tbio);
2111 /* Now write out to any replacement devices
2114 for (i = 0; i < conf->copies; i++) {
2117 tbio = r10_bio->devs[i].repl_bio;
2118 if (!tbio || !tbio->bi_end_io)
2120 if (r10_bio->devs[i].bio->bi_end_io != end_sync_write
2121 && r10_bio->devs[i].bio != fbio)
2122 bio_copy_data(tbio, fbio);
2123 d = r10_bio->devs[i].devnum;
2124 atomic_inc(&r10_bio->remaining);
2125 md_sync_acct(conf->mirrors[d].replacement->bdev,
2127 generic_make_request(tbio);
2131 if (atomic_dec_and_test(&r10_bio->remaining)) {
2132 md_done_sync(mddev, r10_bio->sectors, 1);
2138 * Now for the recovery code.
2139 * Recovery happens across physical sectors.
2140 * We recover all non-is_sync drives by finding the virtual address of
2141 * each, and then choose a working drive that also has that virt address.
2142 * There is a separate r10_bio for each non-in_sync drive.
2143 * Only the first two slots are in use. The first for reading,
2144 * The second for writing.
2147 static void fix_recovery_read_error(struct r10bio *r10_bio)
2149 /* We got a read error during recovery.
2150 * We repeat the read in smaller page-sized sections.
2151 * If a read succeeds, write it to the new device or record
2152 * a bad block if we cannot.
2153 * If a read fails, record a bad block on both old and
2156 struct mddev *mddev = r10_bio->mddev;
2157 struct r10conf *conf = mddev->private;
2158 struct bio *bio = r10_bio->devs[0].bio;
2160 int sectors = r10_bio->sectors;
2162 int dr = r10_bio->devs[0].devnum;
2163 int dw = r10_bio->devs[1].devnum;
2164 struct page **pages = get_resync_pages(bio)->pages;
2168 struct md_rdev *rdev;
2172 if (s > (PAGE_SIZE>>9))
2175 rdev = conf->mirrors[dr].rdev;
2176 addr = r10_bio->devs[0].addr + sect,
2177 ok = sync_page_io(rdev,
2181 REQ_OP_READ, 0, false);
2183 rdev = conf->mirrors[dw].rdev;
2184 addr = r10_bio->devs[1].addr + sect;
2185 ok = sync_page_io(rdev,
2189 REQ_OP_WRITE, 0, false);
2191 set_bit(WriteErrorSeen, &rdev->flags);
2192 if (!test_and_set_bit(WantReplacement,
2194 set_bit(MD_RECOVERY_NEEDED,
2195 &rdev->mddev->recovery);
2199 /* We don't worry if we cannot set a bad block -
2200 * it really is bad so there is no loss in not
2203 rdev_set_badblocks(rdev, addr, s, 0);
2205 if (rdev != conf->mirrors[dw].rdev) {
2206 /* need bad block on destination too */
2207 struct md_rdev *rdev2 = conf->mirrors[dw].rdev;
2208 addr = r10_bio->devs[1].addr + sect;
2209 ok = rdev_set_badblocks(rdev2, addr, s, 0);
2211 /* just abort the recovery */
2212 pr_notice("md/raid10:%s: recovery aborted due to read error\n",
2215 conf->mirrors[dw].recovery_disabled
2216 = mddev->recovery_disabled;
2217 set_bit(MD_RECOVERY_INTR,
2230 static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio)
2232 struct r10conf *conf = mddev->private;
2234 struct bio *wbio, *wbio2;
2236 if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) {
2237 fix_recovery_read_error(r10_bio);
2238 end_sync_request(r10_bio);
2243 * share the pages with the first bio
2244 * and submit the write request
2246 d = r10_bio->devs[1].devnum;
2247 wbio = r10_bio->devs[1].bio;
2248 wbio2 = r10_bio->devs[1].repl_bio;
2249 /* Need to test wbio2->bi_end_io before we call
2250 * generic_make_request as if the former is NULL,
2251 * the latter is free to free wbio2.
2253 if (wbio2 && !wbio2->bi_end_io)
2255 if (wbio->bi_end_io) {
2256 atomic_inc(&conf->mirrors[d].rdev->nr_pending);
2257 md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio));
2258 generic_make_request(wbio);
2261 atomic_inc(&conf->mirrors[d].replacement->nr_pending);
2262 md_sync_acct(conf->mirrors[d].replacement->bdev,
2263 bio_sectors(wbio2));
2264 generic_make_request(wbio2);
2269 * Used by fix_read_error() to decay the per rdev read_errors.
2270 * We halve the read error count for every hour that has elapsed
2271 * since the last recorded read error.
2274 static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev)
2277 unsigned long hours_since_last;
2278 unsigned int read_errors = atomic_read(&rdev->read_errors);
2280 cur_time_mon = ktime_get_seconds();
2282 if (rdev->last_read_error == 0) {
2283 /* first time we've seen a read error */
2284 rdev->last_read_error = cur_time_mon;
2288 hours_since_last = (long)(cur_time_mon -
2289 rdev->last_read_error) / 3600;
2291 rdev->last_read_error = cur_time_mon;
2294 * if hours_since_last is > the number of bits in read_errors
2295 * just set read errors to 0. We do this to avoid
2296 * overflowing the shift of read_errors by hours_since_last.
2298 if (hours_since_last >= 8 * sizeof(read_errors))
2299 atomic_set(&rdev->read_errors, 0);
2301 atomic_set(&rdev->read_errors, read_errors >> hours_since_last);
2304 static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector,
2305 int sectors, struct page *page, int rw)
2310 if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors)
2311 && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags)))
2313 if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false))
2317 set_bit(WriteErrorSeen, &rdev->flags);
2318 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2319 set_bit(MD_RECOVERY_NEEDED,
2320 &rdev->mddev->recovery);
2322 /* need to record an error - either for the block or the device */
2323 if (!rdev_set_badblocks(rdev, sector, sectors, 0))
2324 md_error(rdev->mddev, rdev);
2329 * This is a kernel thread which:
2331 * 1. Retries failed read operations on working mirrors.
2332 * 2. Updates the raid superblock when problems encounter.
2333 * 3. Performs writes following reads for array synchronising.
2336 static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio)
2338 int sect = 0; /* Offset from r10_bio->sector */
2339 int sectors = r10_bio->sectors;
2340 struct md_rdev*rdev;
2341 int max_read_errors = atomic_read(&mddev->max_corr_read_errors);
2342 int d = r10_bio->devs[r10_bio->read_slot].devnum;
2344 /* still own a reference to this rdev, so it cannot
2345 * have been cleared recently.
2347 rdev = conf->mirrors[d].rdev;
2349 if (test_bit(Faulty, &rdev->flags))
2350 /* drive has already been failed, just ignore any
2351 more fix_read_error() attempts */
2354 check_decay_read_errors(mddev, rdev);
2355 atomic_inc(&rdev->read_errors);
2356 if (atomic_read(&rdev->read_errors) > max_read_errors) {
2357 char b[BDEVNAME_SIZE];
2358 bdevname(rdev->bdev, b);
2360 pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n",
2362 atomic_read(&rdev->read_errors), max_read_errors);
2363 pr_notice("md/raid10:%s: %s: Failing raid device\n",
2365 md_error(mddev, rdev);
2366 r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED;
2372 int sl = r10_bio->read_slot;
2376 if (s > (PAGE_SIZE>>9))
2384 d = r10_bio->devs[sl].devnum;
2385 rdev = rcu_dereference(conf->mirrors[d].rdev);
2387 test_bit(In_sync, &rdev->flags) &&
2388 !test_bit(Faulty, &rdev->flags) &&
2389 is_badblock(rdev, r10_bio->devs[sl].addr + sect, s,
2390 &first_bad, &bad_sectors) == 0) {
2391 atomic_inc(&rdev->nr_pending);
2393 success = sync_page_io(rdev,
2394 r10_bio->devs[sl].addr +
2398 REQ_OP_READ, 0, false);
2399 rdev_dec_pending(rdev, mddev);
2405 if (sl == conf->copies)
2407 } while (!success && sl != r10_bio->read_slot);
2411 /* Cannot read from anywhere, just mark the block
2412 * as bad on the first device to discourage future
2415 int dn = r10_bio->devs[r10_bio->read_slot].devnum;
2416 rdev = conf->mirrors[dn].rdev;
2418 if (!rdev_set_badblocks(
2420 r10_bio->devs[r10_bio->read_slot].addr
2423 md_error(mddev, rdev);
2424 r10_bio->devs[r10_bio->read_slot].bio
2431 /* write it back and re-read */
2433 while (sl != r10_bio->read_slot) {
2434 char b[BDEVNAME_SIZE];
2439 d = r10_bio->devs[sl].devnum;
2440 rdev = rcu_dereference(conf->mirrors[d].rdev);
2442 test_bit(Faulty, &rdev->flags) ||
2443 !test_bit(In_sync, &rdev->flags))
2446 atomic_inc(&rdev->nr_pending);
2448 if (r10_sync_page_io(rdev,
2449 r10_bio->devs[sl].addr +
2451 s, conf->tmppage, WRITE)
2453 /* Well, this device is dead */
2454 pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n",
2456 (unsigned long long)(
2458 choose_data_offset(r10_bio,
2460 bdevname(rdev->bdev, b));
2461 pr_notice("md/raid10:%s: %s: failing drive\n",
2463 bdevname(rdev->bdev, b));
2465 rdev_dec_pending(rdev, mddev);
2469 while (sl != r10_bio->read_slot) {
2470 char b[BDEVNAME_SIZE];
2475 d = r10_bio->devs[sl].devnum;
2476 rdev = rcu_dereference(conf->mirrors[d].rdev);
2478 test_bit(Faulty, &rdev->flags) ||
2479 !test_bit(In_sync, &rdev->flags))
2482 atomic_inc(&rdev->nr_pending);
2484 switch (r10_sync_page_io(rdev,
2485 r10_bio->devs[sl].addr +
2490 /* Well, this device is dead */
2491 pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n",
2493 (unsigned long long)(
2495 choose_data_offset(r10_bio, rdev)),
2496 bdevname(rdev->bdev, b));
2497 pr_notice("md/raid10:%s: %s: failing drive\n",
2499 bdevname(rdev->bdev, b));
2502 pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n",
2504 (unsigned long long)(
2506 choose_data_offset(r10_bio, rdev)),
2507 bdevname(rdev->bdev, b));
2508 atomic_add(s, &rdev->corrected_errors);
2511 rdev_dec_pending(rdev, mddev);
2521 static int narrow_write_error(struct r10bio *r10_bio, int i)
2523 struct bio *bio = r10_bio->master_bio;
2524 struct mddev *mddev = r10_bio->mddev;
2525 struct r10conf *conf = mddev->private;
2526 struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev;
2527 /* bio has the data to be written to slot 'i' where
2528 * we just recently had a write error.
2529 * We repeatedly clone the bio and trim down to one block,
2530 * then try the write. Where the write fails we record
2532 * It is conceivable that the bio doesn't exactly align with
2533 * blocks. We must handle this.
2535 * We currently own a reference to the rdev.
2541 int sect_to_write = r10_bio->sectors;
2544 if (rdev->badblocks.shift < 0)
2547 block_sectors = roundup(1 << rdev->badblocks.shift,
2548 bdev_logical_block_size(rdev->bdev) >> 9);
2549 sector = r10_bio->sector;
2550 sectors = ((r10_bio->sector + block_sectors)
2551 & ~(sector_t)(block_sectors - 1))
2554 while (sect_to_write) {
2557 if (sectors > sect_to_write)
2558 sectors = sect_to_write;
2559 /* Write at 'sector' for 'sectors' */
2560 wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set);
2561 bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors);
2562 wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector);
2563 wbio->bi_iter.bi_sector = wsector +
2564 choose_data_offset(r10_bio, rdev);
2565 bio_set_dev(wbio, rdev->bdev);
2566 bio_set_op_attrs(wbio, REQ_OP_WRITE, 0);
2568 if (submit_bio_wait(wbio) < 0)
2570 ok = rdev_set_badblocks(rdev, wsector,
2575 sect_to_write -= sectors;
2577 sectors = block_sectors;
2582 static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio)
2584 int slot = r10_bio->read_slot;
2586 struct r10conf *conf = mddev->private;
2587 struct md_rdev *rdev = r10_bio->devs[slot].rdev;
2588 sector_t bio_last_sector;
2590 /* we got a read error. Maybe the drive is bad. Maybe just
2591 * the block and we can fix it.
2592 * We freeze all other IO, and try reading the block from
2593 * other devices. When we find one, we re-write
2594 * and check it that fixes the read error.
2595 * This is all done synchronously while the array is
2598 bio = r10_bio->devs[slot].bio;
2599 bio_last_sector = r10_bio->devs[slot].addr + rdev->data_offset + r10_bio->sectors;
2601 r10_bio->devs[slot].bio = NULL;
2604 r10_bio->devs[slot].bio = IO_BLOCKED;
2605 else if (!test_bit(FailFast, &rdev->flags)) {
2606 freeze_array(conf, 1);
2607 fix_read_error(conf, mddev, r10_bio);
2608 unfreeze_array(conf);
2610 md_error(mddev, rdev);
2612 rdev_dec_pending(rdev, mddev);
2613 allow_barrier(conf);
2615 raid10_read_request(mddev, r10_bio->master_bio, r10_bio);
2618 static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio)
2620 /* Some sort of write request has finished and it
2621 * succeeded in writing where we thought there was a
2622 * bad block. So forget the bad block.
2623 * Or possibly if failed and we need to record
2627 struct md_rdev *rdev;
2629 if (test_bit(R10BIO_IsSync, &r10_bio->state) ||
2630 test_bit(R10BIO_IsRecover, &r10_bio->state)) {
2631 for (m = 0; m < conf->copies; m++) {
2632 int dev = r10_bio->devs[m].devnum;
2633 rdev = conf->mirrors[dev].rdev;
2634 if (r10_bio->devs[m].bio == NULL ||
2635 r10_bio->devs[m].bio->bi_end_io == NULL)
2637 if (!r10_bio->devs[m].bio->bi_status) {
2638 rdev_clear_badblocks(
2640 r10_bio->devs[m].addr,
2641 r10_bio->sectors, 0);
2643 if (!rdev_set_badblocks(
2645 r10_bio->devs[m].addr,
2646 r10_bio->sectors, 0))
2647 md_error(conf->mddev, rdev);
2649 rdev = conf->mirrors[dev].replacement;
2650 if (r10_bio->devs[m].repl_bio == NULL ||
2651 r10_bio->devs[m].repl_bio->bi_end_io == NULL)
2654 if (!r10_bio->devs[m].repl_bio->bi_status) {
2655 rdev_clear_badblocks(
2657 r10_bio->devs[m].addr,
2658 r10_bio->sectors, 0);
2660 if (!rdev_set_badblocks(
2662 r10_bio->devs[m].addr,
2663 r10_bio->sectors, 0))
2664 md_error(conf->mddev, rdev);
2670 for (m = 0; m < conf->copies; m++) {
2671 int dev = r10_bio->devs[m].devnum;
2672 struct bio *bio = r10_bio->devs[m].bio;
2673 rdev = conf->mirrors[dev].rdev;
2674 if (bio == IO_MADE_GOOD) {
2675 rdev_clear_badblocks(
2677 r10_bio->devs[m].addr,
2678 r10_bio->sectors, 0);
2679 rdev_dec_pending(rdev, conf->mddev);
2680 } else if (bio != NULL && bio->bi_status) {
2682 if (!narrow_write_error(r10_bio, m)) {
2683 md_error(conf->mddev, rdev);
2684 set_bit(R10BIO_Degraded,
2687 rdev_dec_pending(rdev, conf->mddev);
2689 bio = r10_bio->devs[m].repl_bio;
2690 rdev = conf->mirrors[dev].replacement;
2691 if (rdev && bio == IO_MADE_GOOD) {
2692 rdev_clear_badblocks(
2694 r10_bio->devs[m].addr,
2695 r10_bio->sectors, 0);
2696 rdev_dec_pending(rdev, conf->mddev);
2700 spin_lock_irq(&conf->device_lock);
2701 list_add(&r10_bio->retry_list, &conf->bio_end_io_list);
2703 spin_unlock_irq(&conf->device_lock);
2705 * In case freeze_array() is waiting for condition
2706 * nr_pending == nr_queued + extra to be true.
2708 wake_up(&conf->wait_barrier);
2709 md_wakeup_thread(conf->mddev->thread);
2711 if (test_bit(R10BIO_WriteError,
2713 close_write(r10_bio);
2714 raid_end_bio_io(r10_bio);
2719 static void raid10d(struct md_thread *thread)
2721 struct mddev *mddev = thread->mddev;
2722 struct r10bio *r10_bio;
2723 unsigned long flags;
2724 struct r10conf *conf = mddev->private;
2725 struct list_head *head = &conf->retry_list;
2726 struct blk_plug plug;
2728 md_check_recovery(mddev);
2730 if (!list_empty_careful(&conf->bio_end_io_list) &&
2731 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2733 spin_lock_irqsave(&conf->device_lock, flags);
2734 if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) {
2735 while (!list_empty(&conf->bio_end_io_list)) {
2736 list_move(conf->bio_end_io_list.prev, &tmp);
2740 spin_unlock_irqrestore(&conf->device_lock, flags);
2741 while (!list_empty(&tmp)) {
2742 r10_bio = list_first_entry(&tmp, struct r10bio,
2744 list_del(&r10_bio->retry_list);
2745 if (mddev->degraded)
2746 set_bit(R10BIO_Degraded, &r10_bio->state);
2748 if (test_bit(R10BIO_WriteError,
2750 close_write(r10_bio);
2751 raid_end_bio_io(r10_bio);
2755 blk_start_plug(&plug);
2758 flush_pending_writes(conf);
2760 spin_lock_irqsave(&conf->device_lock, flags);
2761 if (list_empty(head)) {
2762 spin_unlock_irqrestore(&conf->device_lock, flags);
2765 r10_bio = list_entry(head->prev, struct r10bio, retry_list);
2766 list_del(head->prev);
2768 spin_unlock_irqrestore(&conf->device_lock, flags);
2770 mddev = r10_bio->mddev;
2771 conf = mddev->private;
2772 if (test_bit(R10BIO_MadeGood, &r10_bio->state) ||
2773 test_bit(R10BIO_WriteError, &r10_bio->state))
2774 handle_write_completed(conf, r10_bio);
2775 else if (test_bit(R10BIO_IsReshape, &r10_bio->state))
2776 reshape_request_write(mddev, r10_bio);
2777 else if (test_bit(R10BIO_IsSync, &r10_bio->state))
2778 sync_request_write(mddev, r10_bio);
2779 else if (test_bit(R10BIO_IsRecover, &r10_bio->state))
2780 recovery_request_write(mddev, r10_bio);
2781 else if (test_bit(R10BIO_ReadError, &r10_bio->state))
2782 handle_read_error(mddev, r10_bio);
2787 if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING))
2788 md_check_recovery(mddev);
2790 blk_finish_plug(&plug);
2793 static int init_resync(struct r10conf *conf)
2798 buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE;
2799 BUG_ON(conf->r10buf_pool);
2800 conf->have_replacement = 0;
2801 for (i = 0; i < conf->geo.raid_disks; i++)
2802 if (conf->mirrors[i].replacement)
2803 conf->have_replacement = 1;
2804 conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf);
2805 if (!conf->r10buf_pool)
2807 conf->next_resync = 0;
2811 static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf)
2813 struct r10bio *r10bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO);
2814 struct rsync_pages *rp;
2819 if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) ||
2820 test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery))
2821 nalloc = conf->copies; /* resync */
2823 nalloc = 2; /* recovery */
2825 for (i = 0; i < nalloc; i++) {
2826 bio = r10bio->devs[i].bio;
2827 rp = bio->bi_private;
2829 bio->bi_private = rp;
2830 bio = r10bio->devs[i].repl_bio;
2832 rp = bio->bi_private;
2834 bio->bi_private = rp;
2841 * perform a "sync" on one "block"
2843 * We need to make sure that no normal I/O request - particularly write
2844 * requests - conflict with active sync requests.
2846 * This is achieved by tracking pending requests and a 'barrier' concept
2847 * that can be installed to exclude normal IO requests.
2849 * Resync and recovery are handled very differently.
2850 * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery.
2852 * For resync, we iterate over virtual addresses, read all copies,
2853 * and update if there are differences. If only one copy is live,
2855 * For recovery, we iterate over physical addresses, read a good
2856 * value for each non-in_sync drive, and over-write.
2858 * So, for recovery we may have several outstanding complex requests for a
2859 * given address, one for each out-of-sync device. We model this by allocating
2860 * a number of r10_bio structures, one for each out-of-sync device.
2861 * As we setup these structures, we collect all bio's together into a list
2862 * which we then process collectively to add pages, and then process again
2863 * to pass to generic_make_request.
2865 * The r10_bio structures are linked using a borrowed master_bio pointer.
2866 * This link is counted in ->remaining. When the r10_bio that points to NULL
2867 * has its remaining count decremented to 0, the whole complex operation
2872 static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr,
2875 struct r10conf *conf = mddev->private;
2876 struct r10bio *r10_bio;
2877 struct bio *biolist = NULL, *bio;
2878 sector_t max_sector, nr_sectors;
2881 sector_t sync_blocks;
2882 sector_t sectors_skipped = 0;
2883 int chunks_skipped = 0;
2884 sector_t chunk_mask = conf->geo.chunk_mask;
2887 if (!conf->r10buf_pool)
2888 if (init_resync(conf))
2892 * Allow skipping a full rebuild for incremental assembly
2893 * of a clean array, like RAID1 does.
2895 if (mddev->bitmap == NULL &&
2896 mddev->recovery_cp == MaxSector &&
2897 mddev->reshape_position == MaxSector &&
2898 !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) &&
2899 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2900 !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) &&
2901 conf->fullsync == 0) {
2903 return mddev->dev_sectors - sector_nr;
2907 max_sector = mddev->dev_sectors;
2908 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) ||
2909 test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2910 max_sector = mddev->resync_max_sectors;
2911 if (sector_nr >= max_sector) {
2912 /* If we aborted, we need to abort the
2913 * sync on the 'current' bitmap chucks (there can
2914 * be several when recovering multiple devices).
2915 * as we may have started syncing it but not finished.
2916 * We can find the current address in
2917 * mddev->curr_resync, but for recovery,
2918 * we need to convert that to several
2919 * virtual addresses.
2921 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2927 if (mddev->curr_resync < max_sector) { /* aborted */
2928 if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery))
2929 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2931 else for (i = 0; i < conf->geo.raid_disks; i++) {
2933 raid10_find_virt(conf, mddev->curr_resync, i);
2934 bitmap_end_sync(mddev->bitmap, sect,
2938 /* completed sync */
2939 if ((!mddev->bitmap || conf->fullsync)
2940 && conf->have_replacement
2941 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2942 /* Completed a full sync so the replacements
2943 * are now fully recovered.
2946 for (i = 0; i < conf->geo.raid_disks; i++) {
2947 struct md_rdev *rdev =
2948 rcu_dereference(conf->mirrors[i].replacement);
2950 rdev->recovery_offset = MaxSector;
2956 bitmap_close_sync(mddev->bitmap);
2959 return sectors_skipped;
2962 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2963 return reshape_request(mddev, sector_nr, skipped);
2965 if (chunks_skipped >= conf->geo.raid_disks) {
2966 /* if there has been nothing to do on any drive,
2967 * then there is nothing to do at all..
2970 return (max_sector - sector_nr) + sectors_skipped;
2973 if (max_sector > mddev->resync_max)
2974 max_sector = mddev->resync_max; /* Don't do IO beyond here */
2976 /* make sure whole request will fit in a chunk - if chunks
2979 if (conf->geo.near_copies < conf->geo.raid_disks &&
2980 max_sector > (sector_nr | chunk_mask))
2981 max_sector = (sector_nr | chunk_mask) + 1;
2984 * If there is non-resync activity waiting for a turn, then let it
2985 * though before starting on this new sync request.
2987 if (conf->nr_waiting)
2988 schedule_timeout_uninterruptible(1);
2990 /* Again, very different code for resync and recovery.
2991 * Both must result in an r10bio with a list of bios that
2992 * have bi_end_io, bi_sector, bi_disk set,
2993 * and bi_private set to the r10bio.
2994 * For recovery, we may actually create several r10bios
2995 * with 2 bios in each, that correspond to the bios in the main one.
2996 * In this case, the subordinate r10bios link back through a
2997 * borrowed master_bio pointer, and the counter in the master
2998 * includes a ref from each subordinate.
3000 /* First, we decide what to do and set ->bi_end_io
3001 * To end_sync_read if we want to read, and
3002 * end_sync_write if we will want to write.
3005 max_sync = RESYNC_PAGES << (PAGE_SHIFT-9);
3006 if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3007 /* recovery... the complicated one */
3011 for (i = 0 ; i < conf->geo.raid_disks; i++) {
3017 struct raid10_info *mirror = &conf->mirrors[i];
3018 struct md_rdev *mrdev, *mreplace;
3021 mrdev = rcu_dereference(mirror->rdev);
3022 mreplace = rcu_dereference(mirror->replacement);
3024 if ((mrdev == NULL ||
3025 test_bit(Faulty, &mrdev->flags) ||
3026 test_bit(In_sync, &mrdev->flags)) &&
3027 (mreplace == NULL ||
3028 test_bit(Faulty, &mreplace->flags))) {
3034 /* want to reconstruct this device */
3036 sect = raid10_find_virt(conf, sector_nr, i);
3037 if (sect >= mddev->resync_max_sectors) {
3038 /* last stripe is not complete - don't
3039 * try to recover this sector.
3044 if (mreplace && test_bit(Faulty, &mreplace->flags))
3046 /* Unless we are doing a full sync, or a replacement
3047 * we only need to recover the block if it is set in
3050 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3052 if (sync_blocks < max_sync)
3053 max_sync = sync_blocks;
3057 /* yep, skip the sync_blocks here, but don't assume
3058 * that there will never be anything to do here
3060 chunks_skipped = -1;
3064 atomic_inc(&mrdev->nr_pending);
3066 atomic_inc(&mreplace->nr_pending);
3069 r10_bio = raid10_alloc_init_r10buf(conf);
3071 raise_barrier(conf, rb2 != NULL);
3072 atomic_set(&r10_bio->remaining, 0);
3074 r10_bio->master_bio = (struct bio*)rb2;
3076 atomic_inc(&rb2->remaining);
3077 r10_bio->mddev = mddev;
3078 set_bit(R10BIO_IsRecover, &r10_bio->state);
3079 r10_bio->sector = sect;
3081 raid10_find_phys(conf, r10_bio);
3083 /* Need to check if the array will still be
3087 for (j = 0; j < conf->geo.raid_disks; j++) {
3088 struct md_rdev *rdev = rcu_dereference(
3089 conf->mirrors[j].rdev);
3090 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3096 must_sync = bitmap_start_sync(mddev->bitmap, sect,
3097 &sync_blocks, still_degraded);
3100 for (j=0; j<conf->copies;j++) {
3102 int d = r10_bio->devs[j].devnum;
3103 sector_t from_addr, to_addr;
3104 struct md_rdev *rdev =
3105 rcu_dereference(conf->mirrors[d].rdev);
3106 sector_t sector, first_bad;
3109 !test_bit(In_sync, &rdev->flags))
3111 /* This is where we read from */
3113 sector = r10_bio->devs[j].addr;
3115 if (is_badblock(rdev, sector, max_sync,
3116 &first_bad, &bad_sectors)) {
3117 if (first_bad > sector)
3118 max_sync = first_bad - sector;
3120 bad_sectors -= (sector
3122 if (max_sync > bad_sectors)
3123 max_sync = bad_sectors;
3127 bio = r10_bio->devs[0].bio;
3128 bio->bi_next = biolist;
3130 bio->bi_end_io = end_sync_read;
3131 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3132 if (test_bit(FailFast, &rdev->flags))
3133 bio->bi_opf |= MD_FAILFAST;
3134 from_addr = r10_bio->devs[j].addr;
3135 bio->bi_iter.bi_sector = from_addr +
3137 bio_set_dev(bio, rdev->bdev);
3138 atomic_inc(&rdev->nr_pending);
3139 /* and we write to 'i' (if not in_sync) */
3141 for (k=0; k<conf->copies; k++)
3142 if (r10_bio->devs[k].devnum == i)
3144 BUG_ON(k == conf->copies);
3145 to_addr = r10_bio->devs[k].addr;
3146 r10_bio->devs[0].devnum = d;
3147 r10_bio->devs[0].addr = from_addr;
3148 r10_bio->devs[1].devnum = i;
3149 r10_bio->devs[1].addr = to_addr;
3151 if (!test_bit(In_sync, &mrdev->flags)) {
3152 bio = r10_bio->devs[1].bio;
3153 bio->bi_next = biolist;
3155 bio->bi_end_io = end_sync_write;
3156 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3157 bio->bi_iter.bi_sector = to_addr
3158 + mrdev->data_offset;
3159 bio_set_dev(bio, mrdev->bdev);
3160 atomic_inc(&r10_bio->remaining);
3162 r10_bio->devs[1].bio->bi_end_io = NULL;
3164 /* and maybe write to replacement */
3165 bio = r10_bio->devs[1].repl_bio;
3167 bio->bi_end_io = NULL;
3168 /* Note: if mreplace != NULL, then bio
3169 * cannot be NULL as r10buf_pool_alloc will
3170 * have allocated it.
3171 * So the second test here is pointless.
3172 * But it keeps semantic-checkers happy, and
3173 * this comment keeps human reviewers
3176 if (mreplace == NULL || bio == NULL ||
3177 test_bit(Faulty, &mreplace->flags))
3179 bio->bi_next = biolist;
3181 bio->bi_end_io = end_sync_write;
3182 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3183 bio->bi_iter.bi_sector = to_addr +
3184 mreplace->data_offset;
3185 bio_set_dev(bio, mreplace->bdev);
3186 atomic_inc(&r10_bio->remaining);
3190 if (j == conf->copies) {
3191 /* Cannot recover, so abort the recovery or
3192 * record a bad block */
3194 /* problem is that there are bad blocks
3195 * on other device(s)
3198 for (k = 0; k < conf->copies; k++)
3199 if (r10_bio->devs[k].devnum == i)
3201 if (!test_bit(In_sync,
3203 && !rdev_set_badblocks(
3205 r10_bio->devs[k].addr,
3209 !rdev_set_badblocks(
3211 r10_bio->devs[k].addr,
3216 if (!test_and_set_bit(MD_RECOVERY_INTR,
3218 pr_warn("md/raid10:%s: insufficient working devices for recovery.\n",
3220 mirror->recovery_disabled
3221 = mddev->recovery_disabled;
3225 atomic_dec(&rb2->remaining);
3227 rdev_dec_pending(mrdev, mddev);
3229 rdev_dec_pending(mreplace, mddev);
3232 rdev_dec_pending(mrdev, mddev);
3234 rdev_dec_pending(mreplace, mddev);
3235 if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) {
3236 /* Only want this if there is elsewhere to
3237 * read from. 'j' is currently the first
3241 for (; j < conf->copies; j++) {
3242 int d = r10_bio->devs[j].devnum;
3243 if (conf->mirrors[d].rdev &&
3245 &conf->mirrors[d].rdev->flags))
3249 r10_bio->devs[0].bio->bi_opf
3253 if (biolist == NULL) {
3255 struct r10bio *rb2 = r10_bio;
3256 r10_bio = (struct r10bio*) rb2->master_bio;
3257 rb2->master_bio = NULL;
3263 /* resync. Schedule a read for every block at this virt offset */
3266 bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0);
3268 if (!bitmap_start_sync(mddev->bitmap, sector_nr,
3269 &sync_blocks, mddev->degraded) &&
3270 !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED,
3271 &mddev->recovery)) {
3272 /* We can skip this block */
3274 return sync_blocks + sectors_skipped;
3276 if (sync_blocks < max_sync)
3277 max_sync = sync_blocks;
3278 r10_bio = raid10_alloc_init_r10buf(conf);
3281 r10_bio->mddev = mddev;
3282 atomic_set(&r10_bio->remaining, 0);
3283 raise_barrier(conf, 0);
3284 conf->next_resync = sector_nr;
3286 r10_bio->master_bio = NULL;
3287 r10_bio->sector = sector_nr;
3288 set_bit(R10BIO_IsSync, &r10_bio->state);
3289 raid10_find_phys(conf, r10_bio);
3290 r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1;
3292 for (i = 0; i < conf->copies; i++) {
3293 int d = r10_bio->devs[i].devnum;
3294 sector_t first_bad, sector;
3296 struct md_rdev *rdev;
3298 if (r10_bio->devs[i].repl_bio)
3299 r10_bio->devs[i].repl_bio->bi_end_io = NULL;
3301 bio = r10_bio->devs[i].bio;
3302 bio->bi_status = BLK_STS_IOERR;
3304 rdev = rcu_dereference(conf->mirrors[d].rdev);
3305 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3309 sector = r10_bio->devs[i].addr;
3310 if (is_badblock(rdev, sector, max_sync,
3311 &first_bad, &bad_sectors)) {
3312 if (first_bad > sector)
3313 max_sync = first_bad - sector;
3315 bad_sectors -= (sector - first_bad);
3316 if (max_sync > bad_sectors)
3317 max_sync = bad_sectors;
3322 atomic_inc(&rdev->nr_pending);
3323 atomic_inc(&r10_bio->remaining);
3324 bio->bi_next = biolist;
3326 bio->bi_end_io = end_sync_read;
3327 bio_set_op_attrs(bio, REQ_OP_READ, 0);
3328 if (test_bit(FailFast, &rdev->flags))
3329 bio->bi_opf |= MD_FAILFAST;
3330 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3331 bio_set_dev(bio, rdev->bdev);
3334 rdev = rcu_dereference(conf->mirrors[d].replacement);
3335 if (rdev == NULL || test_bit(Faulty, &rdev->flags)) {
3339 atomic_inc(&rdev->nr_pending);
3341 /* Need to set up for writing to the replacement */
3342 bio = r10_bio->devs[i].repl_bio;
3343 bio->bi_status = BLK_STS_IOERR;
3345 sector = r10_bio->devs[i].addr;
3346 bio->bi_next = biolist;
3348 bio->bi_end_io = end_sync_write;
3349 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
3350 if (test_bit(FailFast, &rdev->flags))
3351 bio->bi_opf |= MD_FAILFAST;
3352 bio->bi_iter.bi_sector = sector + rdev->data_offset;
3353 bio_set_dev(bio, rdev->bdev);
3359 for (i=0; i<conf->copies; i++) {
3360 int d = r10_bio->devs[i].devnum;
3361 if (r10_bio->devs[i].bio->bi_end_io)
3362 rdev_dec_pending(conf->mirrors[d].rdev,
3364 if (r10_bio->devs[i].repl_bio &&
3365 r10_bio->devs[i].repl_bio->bi_end_io)
3367 conf->mirrors[d].replacement,
3377 if (sector_nr + max_sync < max_sector)
3378 max_sector = sector_nr + max_sync;
3381 int len = PAGE_SIZE;
3382 if (sector_nr + (len>>9) > max_sector)
3383 len = (max_sector - sector_nr) << 9;
3386 for (bio= biolist ; bio ; bio=bio->bi_next) {
3387 struct resync_pages *rp = get_resync_pages(bio);
3388 page = resync_fetch_page(rp, page_idx);
3390 * won't fail because the vec table is big enough
3391 * to hold all these pages
3393 bio_add_page(bio, page, len, 0);
3395 nr_sectors += len>>9;
3396 sector_nr += len>>9;
3397 } while (++page_idx < RESYNC_PAGES);
3398 r10_bio->sectors = nr_sectors;
3402 biolist = biolist->bi_next;
3404 bio->bi_next = NULL;
3405 r10_bio = get_resync_r10bio(bio);
3406 r10_bio->sectors = nr_sectors;
3408 if (bio->bi_end_io == end_sync_read) {
3409 md_sync_acct_bio(bio, nr_sectors);
3411 generic_make_request(bio);
3415 if (sectors_skipped)
3416 /* pretend they weren't skipped, it makes
3417 * no important difference in this case
3419 md_done_sync(mddev, sectors_skipped, 1);
3421 return sectors_skipped + nr_sectors;
3423 /* There is nowhere to write, so all non-sync
3424 * drives must be failed or in resync, all drives
3425 * have a bad block, so try the next chunk...
3427 if (sector_nr + max_sync < max_sector)
3428 max_sector = sector_nr + max_sync;
3430 sectors_skipped += (max_sector - sector_nr);
3432 sector_nr = max_sector;
3437 raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks)
3440 struct r10conf *conf = mddev->private;
3443 raid_disks = min(conf->geo.raid_disks,
3444 conf->prev.raid_disks);
3446 sectors = conf->dev_sectors;
3448 size = sectors >> conf->geo.chunk_shift;
3449 sector_div(size, conf->geo.far_copies);
3450 size = size * raid_disks;
3451 sector_div(size, conf->geo.near_copies);
3453 return size << conf->geo.chunk_shift;
3456 static void calc_sectors(struct r10conf *conf, sector_t size)
3458 /* Calculate the number of sectors-per-device that will
3459 * actually be used, and set conf->dev_sectors and
3463 size = size >> conf->geo.chunk_shift;
3464 sector_div(size, conf->geo.far_copies);
3465 size = size * conf->geo.raid_disks;
3466 sector_div(size, conf->geo.near_copies);
3467 /* 'size' is now the number of chunks in the array */
3468 /* calculate "used chunks per device" */
3469 size = size * conf->copies;
3471 /* We need to round up when dividing by raid_disks to
3472 * get the stride size.
3474 size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks);
3476 conf->dev_sectors = size << conf->geo.chunk_shift;
3478 if (conf->geo.far_offset)
3479 conf->geo.stride = 1 << conf->geo.chunk_shift;
3481 sector_div(size, conf->geo.far_copies);
3482 conf->geo.stride = size << conf->geo.chunk_shift;
3486 enum geo_type {geo_new, geo_old, geo_start};
3487 static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new)
3490 int layout, chunk, disks;
3493 layout = mddev->layout;
3494 chunk = mddev->chunk_sectors;
3495 disks = mddev->raid_disks - mddev->delta_disks;
3498 layout = mddev->new_layout;
3499 chunk = mddev->new_chunk_sectors;
3500 disks = mddev->raid_disks;
3502 default: /* avoid 'may be unused' warnings */
3503 case geo_start: /* new when starting reshape - raid_disks not
3505 layout = mddev->new_layout;
3506 chunk = mddev->new_chunk_sectors;
3507 disks = mddev->raid_disks + mddev->delta_disks;
3512 if (chunk < (PAGE_SIZE >> 9) ||
3513 !is_power_of_2(chunk))
3516 fc = (layout >> 8) & 255;
3517 fo = layout & (1<<16);
3518 geo->raid_disks = disks;
3519 geo->near_copies = nc;
3520 geo->far_copies = fc;
3521 geo->far_offset = fo;
3522 switch (layout >> 17) {
3523 case 0: /* original layout. simple but not always optimal */
3524 geo->far_set_size = disks;
3526 case 1: /* "improved" layout which was buggy. Hopefully no-one is
3527 * actually using this, but leave code here just in case.*/
3528 geo->far_set_size = disks/fc;
3529 WARN(geo->far_set_size < fc,
3530 "This RAID10 layout does not provide data safety - please backup and create new array\n");
3532 case 2: /* "improved" layout fixed to match documentation */
3533 geo->far_set_size = fc * nc;
3535 default: /* Not a valid layout */
3538 geo->chunk_mask = chunk - 1;
3539 geo->chunk_shift = ffz(~chunk);
3543 static struct r10conf *setup_conf(struct mddev *mddev)
3545 struct r10conf *conf = NULL;
3550 copies = setup_geo(&geo, mddev, geo_new);
3553 pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n",
3554 mdname(mddev), PAGE_SIZE);
3558 if (copies < 2 || copies > mddev->raid_disks) {
3559 pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n",
3560 mdname(mddev), mddev->new_layout);
3565 conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL);
3569 /* FIXME calc properly */
3570 conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks +
3571 max(0,-mddev->delta_disks)),
3576 conf->tmppage = alloc_page(GFP_KERNEL);
3581 conf->copies = copies;
3582 conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc,
3583 r10bio_pool_free, conf);
3584 if (!conf->r10bio_pool)
3587 conf->bio_split = bioset_create(BIO_POOL_SIZE, 0, 0);
3588 if (!conf->bio_split)
3591 calc_sectors(conf, mddev->dev_sectors);
3592 if (mddev->reshape_position == MaxSector) {
3593 conf->prev = conf->geo;
3594 conf->reshape_progress = MaxSector;
3596 if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) {
3600 conf->reshape_progress = mddev->reshape_position;
3601 if (conf->prev.far_offset)
3602 conf->prev.stride = 1 << conf->prev.chunk_shift;
3604 /* far_copies must be 1 */
3605 conf->prev.stride = conf->dev_sectors;
3607 conf->reshape_safe = conf->reshape_progress;
3608 spin_lock_init(&conf->device_lock);
3609 INIT_LIST_HEAD(&conf->retry_list);
3610 INIT_LIST_HEAD(&conf->bio_end_io_list);
3612 spin_lock_init(&conf->resync_lock);
3613 init_waitqueue_head(&conf->wait_barrier);
3614 atomic_set(&conf->nr_pending, 0);
3616 conf->thread = md_register_thread(raid10d, mddev, "raid10");
3620 conf->mddev = mddev;
3625 mempool_destroy(conf->r10bio_pool);
3626 kfree(conf->mirrors);
3627 safe_put_page(conf->tmppage);
3628 if (conf->bio_split)
3629 bioset_free(conf->bio_split);
3632 return ERR_PTR(err);
3635 static int raid10_run(struct mddev *mddev)
3637 struct r10conf *conf;
3638 int i, disk_idx, chunk_size;
3639 struct raid10_info *disk;
3640 struct md_rdev *rdev;
3642 sector_t min_offset_diff = 0;
3644 bool discard_supported = false;
3646 if (mddev_init_writes_pending(mddev) < 0)
3649 if (mddev->private == NULL) {
3650 conf = setup_conf(mddev);
3652 return PTR_ERR(conf);
3653 mddev->private = conf;
3655 conf = mddev->private;
3659 mddev->thread = conf->thread;
3660 conf->thread = NULL;
3662 chunk_size = mddev->chunk_sectors << 9;
3664 blk_queue_max_discard_sectors(mddev->queue,
3665 mddev->chunk_sectors);
3666 blk_queue_max_write_same_sectors(mddev->queue, 0);
3667 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
3668 blk_queue_io_min(mddev->queue, chunk_size);
3669 if (conf->geo.raid_disks % conf->geo.near_copies)
3670 blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks);
3672 blk_queue_io_opt(mddev->queue, chunk_size *
3673 (conf->geo.raid_disks / conf->geo.near_copies));
3676 rdev_for_each(rdev, mddev) {
3679 disk_idx = rdev->raid_disk;
3682 if (disk_idx >= conf->geo.raid_disks &&
3683 disk_idx >= conf->prev.raid_disks)
3685 disk = conf->mirrors + disk_idx;
3687 if (test_bit(Replacement, &rdev->flags)) {
3688 if (disk->replacement)
3690 disk->replacement = rdev;
3696 diff = (rdev->new_data_offset - rdev->data_offset);
3697 if (!mddev->reshape_backwards)
3701 if (first || diff < min_offset_diff)
3702 min_offset_diff = diff;
3705 disk_stack_limits(mddev->gendisk, rdev->bdev,
3706 rdev->data_offset << 9);
3708 disk->head_position = 0;
3710 if (blk_queue_discard(bdev_get_queue(rdev->bdev)))
3711 discard_supported = true;
3716 if (discard_supported)
3717 queue_flag_set_unlocked(QUEUE_FLAG_DISCARD,
3720 queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD,
3723 /* need to check that every block has at least one working mirror */
3724 if (!enough(conf, -1)) {
3725 pr_err("md/raid10:%s: not enough operational mirrors.\n",
3730 if (conf->reshape_progress != MaxSector) {
3731 /* must ensure that shape change is supported */
3732 if (conf->geo.far_copies != 1 &&
3733 conf->geo.far_offset == 0)
3735 if (conf->prev.far_copies != 1 &&
3736 conf->prev.far_offset == 0)
3740 mddev->degraded = 0;
3742 i < conf->geo.raid_disks
3743 || i < conf->prev.raid_disks;
3746 disk = conf->mirrors + i;
3748 if (!disk->rdev && disk->replacement) {
3749 /* The replacement is all we have - use it */
3750 disk->rdev = disk->replacement;
3751 disk->replacement = NULL;
3752 clear_bit(Replacement, &disk->rdev->flags);
3756 !test_bit(In_sync, &disk->rdev->flags)) {
3757 disk->head_position = 0;
3760 disk->rdev->saved_raid_disk < 0)
3764 if (disk->replacement &&
3765 !test_bit(In_sync, &disk->replacement->flags) &&
3766 disk->replacement->saved_raid_disk < 0) {
3770 disk->recovery_disabled = mddev->recovery_disabled - 1;
3773 if (mddev->recovery_cp != MaxSector)
3774 pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n",
3776 pr_info("md/raid10:%s: active with %d out of %d devices\n",
3777 mdname(mddev), conf->geo.raid_disks - mddev->degraded,
3778 conf->geo.raid_disks);
3780 * Ok, everything is just fine now
3782 mddev->dev_sectors = conf->dev_sectors;
3783 size = raid10_size(mddev, 0, 0);
3784 md_set_array_sectors(mddev, size);
3785 mddev->resync_max_sectors = size;
3786 set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags);
3789 int stripe = conf->geo.raid_disks *
3790 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
3792 /* Calculate max read-ahead size.
3793 * We need to readahead at least twice a whole stripe....
3796 stripe /= conf->geo.near_copies;
3797 if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
3798 mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
3801 if (md_integrity_register(mddev))
3804 if (conf->reshape_progress != MaxSector) {
3805 unsigned long before_length, after_length;
3807 before_length = ((1 << conf->prev.chunk_shift) *
3808 conf->prev.far_copies);
3809 after_length = ((1 << conf->geo.chunk_shift) *
3810 conf->geo.far_copies);
3812 if (max(before_length, after_length) > min_offset_diff) {
3813 /* This cannot work */
3814 pr_warn("md/raid10: offset difference not enough to continue reshape\n");
3817 conf->offset_diff = min_offset_diff;
3819 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3820 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3821 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3822 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3823 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3825 if (!mddev->sync_thread)
3832 md_unregister_thread(&mddev->thread);
3833 mempool_destroy(conf->r10bio_pool);
3834 safe_put_page(conf->tmppage);
3835 kfree(conf->mirrors);
3837 mddev->private = NULL;
3842 static void raid10_free(struct mddev *mddev, void *priv)
3844 struct r10conf *conf = priv;
3846 mempool_destroy(conf->r10bio_pool);
3847 safe_put_page(conf->tmppage);
3848 kfree(conf->mirrors);
3849 kfree(conf->mirrors_old);
3850 kfree(conf->mirrors_new);
3851 if (conf->bio_split)
3852 bioset_free(conf->bio_split);
3856 static void raid10_quiesce(struct mddev *mddev, int quiesce)
3858 struct r10conf *conf = mddev->private;
3861 raise_barrier(conf, 0);
3863 lower_barrier(conf);
3866 static int raid10_resize(struct mddev *mddev, sector_t sectors)
3868 /* Resize of 'far' arrays is not supported.
3869 * For 'near' and 'offset' arrays we can set the
3870 * number of sectors used to be an appropriate multiple
3871 * of the chunk size.
3872 * For 'offset', this is far_copies*chunksize.
3873 * For 'near' the multiplier is the LCM of
3874 * near_copies and raid_disks.
3875 * So if far_copies > 1 && !far_offset, fail.
3876 * Else find LCM(raid_disks, near_copy)*far_copies and
3877 * multiply by chunk_size. Then round to this number.
3878 * This is mostly done by raid10_size()
3880 struct r10conf *conf = mddev->private;
3881 sector_t oldsize, size;
3883 if (mddev->reshape_position != MaxSector)
3886 if (conf->geo.far_copies > 1 && !conf->geo.far_offset)
3889 oldsize = raid10_size(mddev, 0, 0);
3890 size = raid10_size(mddev, sectors, 0);
3891 if (mddev->external_size &&
3892 mddev->array_sectors > size)
3894 if (mddev->bitmap) {
3895 int ret = bitmap_resize(mddev->bitmap, size, 0, 0);
3899 md_set_array_sectors(mddev, size);
3900 if (sectors > mddev->dev_sectors &&
3901 mddev->recovery_cp > oldsize) {
3902 mddev->recovery_cp = oldsize;
3903 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3905 calc_sectors(conf, sectors);
3906 mddev->dev_sectors = conf->dev_sectors;
3907 mddev->resync_max_sectors = size;
3911 static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs)
3913 struct md_rdev *rdev;
3914 struct r10conf *conf;
3916 if (mddev->degraded > 0) {
3917 pr_warn("md/raid10:%s: Error: degraded raid0!\n",
3919 return ERR_PTR(-EINVAL);
3921 sector_div(size, devs);
3923 /* Set new parameters */
3924 mddev->new_level = 10;
3925 /* new layout: far_copies = 1, near_copies = 2 */
3926 mddev->new_layout = (1<<8) + 2;
3927 mddev->new_chunk_sectors = mddev->chunk_sectors;
3928 mddev->delta_disks = mddev->raid_disks;
3929 mddev->raid_disks *= 2;
3930 /* make sure it will be not marked as dirty */
3931 mddev->recovery_cp = MaxSector;
3932 mddev->dev_sectors = size;
3934 conf = setup_conf(mddev);
3935 if (!IS_ERR(conf)) {
3936 rdev_for_each(rdev, mddev)
3937 if (rdev->raid_disk >= 0) {
3938 rdev->new_raid_disk = rdev->raid_disk * 2;
3939 rdev->sectors = size;
3947 static void *raid10_takeover(struct mddev *mddev)
3949 struct r0conf *raid0_conf;
3951 /* raid10 can take over:
3952 * raid0 - providing it has only two drives
3954 if (mddev->level == 0) {
3955 /* for raid0 takeover only one zone is supported */
3956 raid0_conf = mddev->private;
3957 if (raid0_conf->nr_strip_zones > 1) {
3958 pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n",
3960 return ERR_PTR(-EINVAL);
3962 return raid10_takeover_raid0(mddev,
3963 raid0_conf->strip_zone->zone_end,
3964 raid0_conf->strip_zone->nb_dev);
3966 return ERR_PTR(-EINVAL);
3969 static int raid10_check_reshape(struct mddev *mddev)
3971 /* Called when there is a request to change
3972 * - layout (to ->new_layout)
3973 * - chunk size (to ->new_chunk_sectors)
3974 * - raid_disks (by delta_disks)
3975 * or when trying to restart a reshape that was ongoing.
3977 * We need to validate the request and possibly allocate
3978 * space if that might be an issue later.
3980 * Currently we reject any reshape of a 'far' mode array,
3981 * allow chunk size to change if new is generally acceptable,
3982 * allow raid_disks to increase, and allow
3983 * a switch between 'near' mode and 'offset' mode.
3985 struct r10conf *conf = mddev->private;
3988 if (conf->geo.far_copies != 1 && !conf->geo.far_offset)
3991 if (setup_geo(&geo, mddev, geo_start) != conf->copies)
3992 /* mustn't change number of copies */
3994 if (geo.far_copies > 1 && !geo.far_offset)
3995 /* Cannot switch to 'far' mode */
3998 if (mddev->array_sectors & geo.chunk_mask)
3999 /* not factor of array size */
4002 if (!enough(conf, -1))
4005 kfree(conf->mirrors_new);
4006 conf->mirrors_new = NULL;
4007 if (mddev->delta_disks > 0) {
4008 /* allocate new 'mirrors' list */
4009 conf->mirrors_new = kzalloc(
4010 sizeof(struct raid10_info)
4011 *(mddev->raid_disks +
4012 mddev->delta_disks),
4014 if (!conf->mirrors_new)
4021 * Need to check if array has failed when deciding whether to:
4023 * - remove non-faulty devices
4026 * This determination is simple when no reshape is happening.
4027 * However if there is a reshape, we need to carefully check
4028 * both the before and after sections.
4029 * This is because some failed devices may only affect one
4030 * of the two sections, and some non-in_sync devices may
4031 * be insync in the section most affected by failed devices.
4033 static int calc_degraded(struct r10conf *conf)
4035 int degraded, degraded2;
4040 /* 'prev' section first */
4041 for (i = 0; i < conf->prev.raid_disks; i++) {
4042 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4043 if (!rdev || test_bit(Faulty, &rdev->flags))
4045 else if (!test_bit(In_sync, &rdev->flags))
4046 /* When we can reduce the number of devices in
4047 * an array, this might not contribute to
4048 * 'degraded'. It does now.
4053 if (conf->geo.raid_disks == conf->prev.raid_disks)
4057 for (i = 0; i < conf->geo.raid_disks; i++) {
4058 struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev);
4059 if (!rdev || test_bit(Faulty, &rdev->flags))
4061 else if (!test_bit(In_sync, &rdev->flags)) {
4062 /* If reshape is increasing the number of devices,
4063 * this section has already been recovered, so
4064 * it doesn't contribute to degraded.
4067 if (conf->geo.raid_disks <= conf->prev.raid_disks)
4072 if (degraded2 > degraded)
4077 static int raid10_start_reshape(struct mddev *mddev)
4079 /* A 'reshape' has been requested. This commits
4080 * the various 'new' fields and sets MD_RECOVER_RESHAPE
4081 * This also checks if there are enough spares and adds them
4083 * We currently require enough spares to make the final
4084 * array non-degraded. We also require that the difference
4085 * between old and new data_offset - on each device - is
4086 * enough that we never risk over-writing.
4089 unsigned long before_length, after_length;
4090 sector_t min_offset_diff = 0;
4093 struct r10conf *conf = mddev->private;
4094 struct md_rdev *rdev;
4098 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
4101 if (setup_geo(&new, mddev, geo_start) != conf->copies)
4104 before_length = ((1 << conf->prev.chunk_shift) *
4105 conf->prev.far_copies);
4106 after_length = ((1 << conf->geo.chunk_shift) *
4107 conf->geo.far_copies);
4109 rdev_for_each(rdev, mddev) {
4110 if (!test_bit(In_sync, &rdev->flags)
4111 && !test_bit(Faulty, &rdev->flags))
4113 if (rdev->raid_disk >= 0) {
4114 long long diff = (rdev->new_data_offset
4115 - rdev->data_offset);
4116 if (!mddev->reshape_backwards)
4120 if (first || diff < min_offset_diff)
4121 min_offset_diff = diff;
4126 if (max(before_length, after_length) > min_offset_diff)
4129 if (spares < mddev->delta_disks)
4132 conf->offset_diff = min_offset_diff;
4133 spin_lock_irq(&conf->device_lock);
4134 if (conf->mirrors_new) {
4135 memcpy(conf->mirrors_new, conf->mirrors,
4136 sizeof(struct raid10_info)*conf->prev.raid_disks);
4138 kfree(conf->mirrors_old);
4139 conf->mirrors_old = conf->mirrors;
4140 conf->mirrors = conf->mirrors_new;
4141 conf->mirrors_new = NULL;
4143 setup_geo(&conf->geo, mddev, geo_start);
4145 if (mddev->reshape_backwards) {
4146 sector_t size = raid10_size(mddev, 0, 0);
4147 if (size < mddev->array_sectors) {
4148 spin_unlock_irq(&conf->device_lock);
4149 pr_warn("md/raid10:%s: array size must be reduce before number of disks\n",
4153 mddev->resync_max_sectors = size;
4154 conf->reshape_progress = size;
4156 conf->reshape_progress = 0;
4157 conf->reshape_safe = conf->reshape_progress;
4158 spin_unlock_irq(&conf->device_lock);
4160 if (mddev->delta_disks && mddev->bitmap) {
4161 ret = bitmap_resize(mddev->bitmap,
4162 raid10_size(mddev, 0,
4163 conf->geo.raid_disks),
4168 if (mddev->delta_disks > 0) {
4169 rdev_for_each(rdev, mddev)
4170 if (rdev->raid_disk < 0 &&
4171 !test_bit(Faulty, &rdev->flags)) {
4172 if (raid10_add_disk(mddev, rdev) == 0) {
4173 if (rdev->raid_disk >=
4174 conf->prev.raid_disks)
4175 set_bit(In_sync, &rdev->flags);
4177 rdev->recovery_offset = 0;
4179 if (sysfs_link_rdev(mddev, rdev))
4180 /* Failure here is OK */;
4182 } else if (rdev->raid_disk >= conf->prev.raid_disks
4183 && !test_bit(Faulty, &rdev->flags)) {
4184 /* This is a spare that was manually added */
4185 set_bit(In_sync, &rdev->flags);
4188 /* When a reshape changes the number of devices,
4189 * ->degraded is measured against the larger of the
4190 * pre and post numbers.
4192 spin_lock_irq(&conf->device_lock);
4193 mddev->degraded = calc_degraded(conf);
4194 spin_unlock_irq(&conf->device_lock);
4195 mddev->raid_disks = conf->geo.raid_disks;
4196 mddev->reshape_position = conf->reshape_progress;
4197 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4199 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4200 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4201 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
4202 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4203 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4205 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4207 if (!mddev->sync_thread) {
4211 conf->reshape_checkpoint = jiffies;
4212 md_wakeup_thread(mddev->sync_thread);
4213 md_new_event(mddev);
4217 mddev->recovery = 0;
4218 spin_lock_irq(&conf->device_lock);
4219 conf->geo = conf->prev;
4220 mddev->raid_disks = conf->geo.raid_disks;
4221 rdev_for_each(rdev, mddev)
4222 rdev->new_data_offset = rdev->data_offset;
4224 conf->reshape_progress = MaxSector;
4225 conf->reshape_safe = MaxSector;
4226 mddev->reshape_position = MaxSector;
4227 spin_unlock_irq(&conf->device_lock);
4231 /* Calculate the last device-address that could contain
4232 * any block from the chunk that includes the array-address 's'
4233 * and report the next address.
4234 * i.e. the address returned will be chunk-aligned and after
4235 * any data that is in the chunk containing 's'.
4237 static sector_t last_dev_address(sector_t s, struct geom *geo)
4239 s = (s | geo->chunk_mask) + 1;
4240 s >>= geo->chunk_shift;
4241 s *= geo->near_copies;
4242 s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks);
4243 s *= geo->far_copies;
4244 s <<= geo->chunk_shift;
4248 /* Calculate the first device-address that could contain
4249 * any block from the chunk that includes the array-address 's'.
4250 * This too will be the start of a chunk
4252 static sector_t first_dev_address(sector_t s, struct geom *geo)
4254 s >>= geo->chunk_shift;
4255 s *= geo->near_copies;
4256 sector_div(s, geo->raid_disks);
4257 s *= geo->far_copies;
4258 s <<= geo->chunk_shift;
4262 static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr,
4265 /* We simply copy at most one chunk (smallest of old and new)
4266 * at a time, possibly less if that exceeds RESYNC_PAGES,
4267 * or we hit a bad block or something.
4268 * This might mean we pause for normal IO in the middle of
4269 * a chunk, but that is not a problem as mddev->reshape_position
4270 * can record any location.
4272 * If we will want to write to a location that isn't
4273 * yet recorded as 'safe' (i.e. in metadata on disk) then
4274 * we need to flush all reshape requests and update the metadata.
4276 * When reshaping forwards (e.g. to more devices), we interpret
4277 * 'safe' as the earliest block which might not have been copied
4278 * down yet. We divide this by previous stripe size and multiply
4279 * by previous stripe length to get lowest device offset that we
4280 * cannot write to yet.
4281 * We interpret 'sector_nr' as an address that we want to write to.
4282 * From this we use last_device_address() to find where we might
4283 * write to, and first_device_address on the 'safe' position.
4284 * If this 'next' write position is after the 'safe' position,
4285 * we must update the metadata to increase the 'safe' position.
4287 * When reshaping backwards, we round in the opposite direction
4288 * and perform the reverse test: next write position must not be
4289 * less than current safe position.
4291 * In all this the minimum difference in data offsets
4292 * (conf->offset_diff - always positive) allows a bit of slack,
4293 * so next can be after 'safe', but not by more than offset_diff
4295 * We need to prepare all the bios here before we start any IO
4296 * to ensure the size we choose is acceptable to all devices.
4297 * The means one for each copy for write-out and an extra one for
4299 * We store the read-in bio in ->master_bio and the others in
4300 * ->devs[x].bio and ->devs[x].repl_bio.
4302 struct r10conf *conf = mddev->private;
4303 struct r10bio *r10_bio;
4304 sector_t next, safe, last;
4308 struct md_rdev *rdev;
4311 struct bio *bio, *read_bio;
4312 int sectors_done = 0;
4313 struct page **pages;
4315 if (sector_nr == 0) {
4316 /* If restarting in the middle, skip the initial sectors */
4317 if (mddev->reshape_backwards &&
4318 conf->reshape_progress < raid10_size(mddev, 0, 0)) {
4319 sector_nr = (raid10_size(mddev, 0, 0)
4320 - conf->reshape_progress);
4321 } else if (!mddev->reshape_backwards &&
4322 conf->reshape_progress > 0)
4323 sector_nr = conf->reshape_progress;
4325 mddev->curr_resync_completed = sector_nr;
4326 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4332 /* We don't use sector_nr to track where we are up to
4333 * as that doesn't work well for ->reshape_backwards.
4334 * So just use ->reshape_progress.
4336 if (mddev->reshape_backwards) {
4337 /* 'next' is the earliest device address that we might
4338 * write to for this chunk in the new layout
4340 next = first_dev_address(conf->reshape_progress - 1,
4343 /* 'safe' is the last device address that we might read from
4344 * in the old layout after a restart
4346 safe = last_dev_address(conf->reshape_safe - 1,
4349 if (next + conf->offset_diff < safe)
4352 last = conf->reshape_progress - 1;
4353 sector_nr = last & ~(sector_t)(conf->geo.chunk_mask
4354 & conf->prev.chunk_mask);
4355 if (sector_nr + RESYNC_BLOCK_SIZE/512 < last)
4356 sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512;
4358 /* 'next' is after the last device address that we
4359 * might write to for this chunk in the new layout
4361 next = last_dev_address(conf->reshape_progress, &conf->geo);
4363 /* 'safe' is the earliest device address that we might
4364 * read from in the old layout after a restart
4366 safe = first_dev_address(conf->reshape_safe, &conf->prev);
4368 /* Need to update metadata if 'next' might be beyond 'safe'
4369 * as that would possibly corrupt data
4371 if (next > safe + conf->offset_diff)
4374 sector_nr = conf->reshape_progress;
4375 last = sector_nr | (conf->geo.chunk_mask
4376 & conf->prev.chunk_mask);
4378 if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last)
4379 last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1;
4383 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
4384 /* Need to update reshape_position in metadata */
4386 mddev->reshape_position = conf->reshape_progress;
4387 if (mddev->reshape_backwards)
4388 mddev->curr_resync_completed = raid10_size(mddev, 0, 0)
4389 - conf->reshape_progress;
4391 mddev->curr_resync_completed = conf->reshape_progress;
4392 conf->reshape_checkpoint = jiffies;
4393 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
4394 md_wakeup_thread(mddev->thread);
4395 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
4396 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
4397 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
4398 allow_barrier(conf);
4399 return sectors_done;
4401 conf->reshape_safe = mddev->reshape_position;
4402 allow_barrier(conf);
4405 raise_barrier(conf, 0);
4407 /* Now schedule reads for blocks from sector_nr to last */
4408 r10_bio = raid10_alloc_init_r10buf(conf);
4410 raise_barrier(conf, 1);
4411 atomic_set(&r10_bio->remaining, 0);
4412 r10_bio->mddev = mddev;
4413 r10_bio->sector = sector_nr;
4414 set_bit(R10BIO_IsReshape, &r10_bio->state);
4415 r10_bio->sectors = last - sector_nr + 1;
4416 rdev = read_balance(conf, r10_bio, &max_sectors);
4417 BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state));
4420 /* Cannot read from here, so need to record bad blocks
4421 * on all the target devices.
4424 mempool_free(r10_bio, conf->r10buf_pool);
4425 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4426 return sectors_done;
4429 read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev);
4431 bio_set_dev(read_bio, rdev->bdev);
4432 read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr
4433 + rdev->data_offset);
4434 read_bio->bi_private = r10_bio;
4435 read_bio->bi_end_io = end_reshape_read;
4436 bio_set_op_attrs(read_bio, REQ_OP_READ, 0);
4437 read_bio->bi_flags &= (~0UL << BIO_RESET_BITS);
4438 read_bio->bi_status = 0;
4439 read_bio->bi_vcnt = 0;
4440 read_bio->bi_iter.bi_size = 0;
4441 r10_bio->master_bio = read_bio;
4442 r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum;
4444 /* Now find the locations in the new layout */
4445 __raid10_find_phys(&conf->geo, r10_bio);
4448 read_bio->bi_next = NULL;
4451 for (s = 0; s < conf->copies*2; s++) {
4453 int d = r10_bio->devs[s/2].devnum;
4454 struct md_rdev *rdev2;
4456 rdev2 = rcu_dereference(conf->mirrors[d].replacement);
4457 b = r10_bio->devs[s/2].repl_bio;
4459 rdev2 = rcu_dereference(conf->mirrors[d].rdev);
4460 b = r10_bio->devs[s/2].bio;
4462 if (!rdev2 || test_bit(Faulty, &rdev2->flags))
4465 bio_set_dev(b, rdev2->bdev);
4466 b->bi_iter.bi_sector = r10_bio->devs[s/2].addr +
4467 rdev2->new_data_offset;
4468 b->bi_end_io = end_reshape_write;
4469 bio_set_op_attrs(b, REQ_OP_WRITE, 0);
4474 /* Now add as many pages as possible to all of these bios. */
4477 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4478 for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) {
4479 struct page *page = pages[s / (PAGE_SIZE >> 9)];
4480 int len = (max_sectors - s) << 9;
4481 if (len > PAGE_SIZE)
4483 for (bio = blist; bio ; bio = bio->bi_next) {
4485 * won't fail because the vec table is big enough
4486 * to hold all these pages
4488 bio_add_page(bio, page, len, 0);
4490 sector_nr += len >> 9;
4491 nr_sectors += len >> 9;
4494 r10_bio->sectors = nr_sectors;
4496 /* Now submit the read */
4497 md_sync_acct_bio(read_bio, r10_bio->sectors);
4498 atomic_inc(&r10_bio->remaining);
4499 read_bio->bi_next = NULL;
4500 generic_make_request(read_bio);
4501 sectors_done += nr_sectors;
4502 if (sector_nr <= last)
4505 lower_barrier(conf);
4507 /* Now that we have done the whole section we can
4508 * update reshape_progress
4510 if (mddev->reshape_backwards)
4511 conf->reshape_progress -= sectors_done;
4513 conf->reshape_progress += sectors_done;
4515 return sectors_done;
4518 static void end_reshape_request(struct r10bio *r10_bio);
4519 static int handle_reshape_read_error(struct mddev *mddev,
4520 struct r10bio *r10_bio);
4521 static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio)
4523 /* Reshape read completed. Hopefully we have a block
4525 * If we got a read error then we do sync 1-page reads from
4526 * elsewhere until we find the data - or give up.
4528 struct r10conf *conf = mddev->private;
4531 if (!test_bit(R10BIO_Uptodate, &r10_bio->state))
4532 if (handle_reshape_read_error(mddev, r10_bio) < 0) {
4533 /* Reshape has been aborted */
4534 md_done_sync(mddev, r10_bio->sectors, 0);
4538 /* We definitely have the data in the pages, schedule the
4541 atomic_set(&r10_bio->remaining, 1);
4542 for (s = 0; s < conf->copies*2; s++) {
4544 int d = r10_bio->devs[s/2].devnum;
4545 struct md_rdev *rdev;
4548 rdev = rcu_dereference(conf->mirrors[d].replacement);
4549 b = r10_bio->devs[s/2].repl_bio;
4551 rdev = rcu_dereference(conf->mirrors[d].rdev);
4552 b = r10_bio->devs[s/2].bio;
4554 if (!rdev || test_bit(Faulty, &rdev->flags)) {
4558 atomic_inc(&rdev->nr_pending);
4560 md_sync_acct_bio(b, r10_bio->sectors);
4561 atomic_inc(&r10_bio->remaining);
4563 generic_make_request(b);
4565 end_reshape_request(r10_bio);
4568 static void end_reshape(struct r10conf *conf)
4570 if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery))
4573 spin_lock_irq(&conf->device_lock);
4574 conf->prev = conf->geo;
4575 md_finish_reshape(conf->mddev);
4577 conf->reshape_progress = MaxSector;
4578 conf->reshape_safe = MaxSector;
4579 spin_unlock_irq(&conf->device_lock);
4581 /* read-ahead size must cover two whole stripes, which is
4582 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
4584 if (conf->mddev->queue) {
4585 int stripe = conf->geo.raid_disks *
4586 ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE);
4587 stripe /= conf->geo.near_copies;
4588 if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe)
4589 conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe;
4594 static int handle_reshape_read_error(struct mddev *mddev,
4595 struct r10bio *r10_bio)
4597 /* Use sync reads to get the blocks from somewhere else */
4598 int sectors = r10_bio->sectors;
4599 struct r10conf *conf = mddev->private;
4600 struct r10bio *r10b;
4603 struct page **pages;
4605 r10b = kmalloc(sizeof(*r10b) +
4606 sizeof(struct r10dev) * conf->copies, GFP_NOIO);
4608 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
4612 /* reshape IOs share pages from .devs[0].bio */
4613 pages = get_resync_pages(r10_bio->devs[0].bio)->pages;
4615 r10b->sector = r10_bio->sector;
4616 __raid10_find_phys(&conf->prev, r10b);
4621 int first_slot = slot;
4623 if (s > (PAGE_SIZE >> 9))
4628 int d = r10b->devs[slot].devnum;
4629 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4632 test_bit(Faulty, &rdev->flags) ||
4633 !test_bit(In_sync, &rdev->flags))
4636 addr = r10b->devs[slot].addr + idx * PAGE_SIZE;
4637 atomic_inc(&rdev->nr_pending);
4639 success = sync_page_io(rdev,
4643 REQ_OP_READ, 0, false);
4644 rdev_dec_pending(rdev, mddev);
4650 if (slot >= conf->copies)
4652 if (slot == first_slot)
4657 /* couldn't read this block, must give up */
4658 set_bit(MD_RECOVERY_INTR,
4670 static void end_reshape_write(struct bio *bio)
4672 struct r10bio *r10_bio = get_resync_r10bio(bio);
4673 struct mddev *mddev = r10_bio->mddev;
4674 struct r10conf *conf = mddev->private;
4678 struct md_rdev *rdev = NULL;
4680 d = find_bio_disk(conf, r10_bio, bio, &slot, &repl);
4682 rdev = conf->mirrors[d].replacement;
4685 rdev = conf->mirrors[d].rdev;
4688 if (bio->bi_status) {
4689 /* FIXME should record badblock */
4690 md_error(mddev, rdev);
4693 rdev_dec_pending(rdev, mddev);
4694 end_reshape_request(r10_bio);
4697 static void end_reshape_request(struct r10bio *r10_bio)
4699 if (!atomic_dec_and_test(&r10_bio->remaining))
4701 md_done_sync(r10_bio->mddev, r10_bio->sectors, 1);
4702 bio_put(r10_bio->master_bio);
4706 static void raid10_finish_reshape(struct mddev *mddev)
4708 struct r10conf *conf = mddev->private;
4710 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
4713 if (mddev->delta_disks > 0) {
4714 if (mddev->recovery_cp > mddev->resync_max_sectors) {
4715 mddev->recovery_cp = mddev->resync_max_sectors;
4716 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
4718 mddev->resync_max_sectors = mddev->array_sectors;
4722 for (d = conf->geo.raid_disks ;
4723 d < conf->geo.raid_disks - mddev->delta_disks;
4725 struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev);
4727 clear_bit(In_sync, &rdev->flags);
4728 rdev = rcu_dereference(conf->mirrors[d].replacement);
4730 clear_bit(In_sync, &rdev->flags);
4734 mddev->layout = mddev->new_layout;
4735 mddev->chunk_sectors = 1 << conf->geo.chunk_shift;
4736 mddev->reshape_position = MaxSector;
4737 mddev->delta_disks = 0;
4738 mddev->reshape_backwards = 0;
4741 static struct md_personality raid10_personality =
4745 .owner = THIS_MODULE,
4746 .make_request = raid10_make_request,
4748 .free = raid10_free,
4749 .status = raid10_status,
4750 .error_handler = raid10_error,
4751 .hot_add_disk = raid10_add_disk,
4752 .hot_remove_disk= raid10_remove_disk,
4753 .spare_active = raid10_spare_active,
4754 .sync_request = raid10_sync_request,
4755 .quiesce = raid10_quiesce,
4756 .size = raid10_size,
4757 .resize = raid10_resize,
4758 .takeover = raid10_takeover,
4759 .check_reshape = raid10_check_reshape,
4760 .start_reshape = raid10_start_reshape,
4761 .finish_reshape = raid10_finish_reshape,
4762 .congested = raid10_congested,
4765 static int __init raid_init(void)
4767 return register_md_personality(&raid10_personality);
4770 static void raid_exit(void)
4772 unregister_md_personality(&raid10_personality);
4775 module_init(raid_init);
4776 module_exit(raid_exit);
4777 MODULE_LICENSE("GPL");
4778 MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD");
4779 MODULE_ALIAS("md-personality-9"); /* RAID10 */
4780 MODULE_ALIAS("md-raid10");
4781 MODULE_ALIAS("md-level-10");
4783 module_param(max_queued_requests, int, S_IRUGO|S_IWUSR);