Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * raid10.c : Multiple Devices driver for Linux |
| 3 | * |
| 4 | * Copyright (C) 2000-2004 Neil Brown |
| 5 | * |
| 6 | * RAID-10 support for md. |
| 7 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 8 | * Base on code in raid1.c. See raid1.c for further copyright information. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | * |
| 10 | * |
| 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) |
| 14 | * any later version. |
| 15 | * |
| 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. |
| 19 | */ |
| 20 | |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 21 | #include <linux/slab.h> |
Stephen Rothwell | 2557072 | 2008-10-15 09:09:21 +1100 | [diff] [blame] | 22 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 23 | #include <linux/blkdev.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 24 | #include <linux/module.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 25 | #include <linux/seq_file.h> |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 26 | #include <linux/ratelimit.h> |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 27 | #include <linux/kthread.h> |
Guoqing Jiang | afd7562 | 2018-10-18 16:37:41 +0800 | [diff] [blame] | 28 | #include <linux/raid/md_p.h> |
NeilBrown | 109e376 | 2016-11-18 13:22:04 +1100 | [diff] [blame] | 29 | #include <trace/events/block.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 30 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 31 | #include "raid10.h" |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 32 | #include "raid0.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 33 | #include "md-bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
| 35 | /* |
| 36 | * RAID10 provides a combination of RAID0 and RAID1 functionality. |
| 37 | * The layout of data is defined by |
| 38 | * chunk_size |
| 39 | * raid_disks |
| 40 | * near_copies (stored in low byte of layout) |
| 41 | * far_copies (stored in second byte of layout) |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 42 | * far_offset (stored in bit 16 of layout ) |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 43 | * use_far_sets (stored in bit 17 of layout ) |
NeilBrown | 8bce6d3 | 2015-10-22 13:20:15 +1100 | [diff] [blame] | 44 | * use_far_sets_bugfixed (stored in bit 18 of layout ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | * |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 46 | * The data to be stored is divided into chunks using chunksize. Each device |
| 47 | * is divided into far_copies sections. In each section, chunks are laid out |
| 48 | * in a style similar to raid0, but near_copies copies of each chunk is stored |
| 49 | * (each on a different drive). The starting device for each section is offset |
| 50 | * near_copies from the starting device of the previous section. Thus there |
| 51 | * are (near_copies * far_copies) of each chunk, and each is on a different |
| 52 | * drive. near_copies and far_copies must be at least one, and their product |
| 53 | * is at most raid_disks. |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 54 | * |
| 55 | * If far_offset is true, then the far_copies are handled a bit differently. |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 56 | * The copies are still in different stripes, but instead of being very far |
| 57 | * apart on disk, there are adjacent stripes. |
| 58 | * |
| 59 | * The far and offset algorithms are handled slightly differently if |
| 60 | * 'use_far_sets' is true. In this case, the array's devices are grouped into |
| 61 | * sets that are (near_copies * far_copies) in size. The far copied stripes |
| 62 | * are still shifted by 'near_copies' devices, but this shifting stays confined |
| 63 | * to the set rather than the entire array. This is done to improve the number |
| 64 | * of device combinations that can fail without causing the array to fail. |
| 65 | * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk |
| 66 | * on a device): |
| 67 | * A B C D A B C D E |
| 68 | * ... ... |
| 69 | * D A B C E A B C D |
| 70 | * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s): |
| 71 | * [A B] [C D] [A B] [C D E] |
| 72 | * |...| |...| |...| | ... | |
| 73 | * [B A] [D C] [B A] [E C D] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | */ |
| 75 | |
| 76 | /* |
| 77 | * Number of guaranteed r10bios in case of extreme VM load: |
| 78 | */ |
| 79 | #define NR_RAID10_BIOS 256 |
| 80 | |
Jonathan Brassow | 473e87c | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 81 | /* when we get a read error on a read-only array, we redirect to another |
| 82 | * device without failing the first device, or trying to over-write to |
| 83 | * correct the read error. To keep track of bad blocks on a per-bio |
| 84 | * level, we store IO_BLOCKED in the appropriate 'bios' pointer |
| 85 | */ |
| 86 | #define IO_BLOCKED ((struct bio *)1) |
| 87 | /* When we successfully write to a known bad-block, we need to remove the |
| 88 | * bad-block marking which must be done from process context. So we record |
| 89 | * the success by setting devs[n].bio to IO_MADE_GOOD |
| 90 | */ |
| 91 | #define IO_MADE_GOOD ((struct bio *)2) |
| 92 | |
| 93 | #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2) |
| 94 | |
| 95 | /* When there are this many requests queued to be written by |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 96 | * the raid10 thread, we become 'congested' to provide back-pressure |
| 97 | * for writeback. |
| 98 | */ |
| 99 | static int max_queued_requests = 1024; |
| 100 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 101 | static void allow_barrier(struct r10conf *conf); |
| 102 | static void lower_barrier(struct r10conf *conf); |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 103 | static int _enough(struct r10conf *conf, int previous, int ignore); |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 104 | static int enough(struct r10conf *conf, int ignore); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 105 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, |
| 106 | int *skipped); |
| 107 | static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 108 | static void end_reshape_write(struct bio *bio); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 109 | static void end_reshape(struct r10conf *conf); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 110 | |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 111 | #define raid10_log(md, fmt, args...) \ |
| 112 | do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0) |
| 113 | |
Ming Lei | fb0eb5d | 2017-07-14 16:14:43 +0800 | [diff] [blame] | 114 | #include "raid1-10.c" |
| 115 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 116 | /* |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 117 | * for resync bio, r10bio pointer can be retrieved from the per-bio |
| 118 | * 'struct resync_pages'. |
| 119 | */ |
| 120 | static inline struct r10bio *get_resync_r10bio(struct bio *bio) |
| 121 | { |
| 122 | return get_resync_pages(bio)->raid_bio; |
| 123 | } |
| 124 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 125 | static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 127 | struct r10conf *conf = data; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 128 | int size = offsetof(struct r10bio, devs[conf->copies]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 130 | /* allocate a r10bio with room for raid_disks entries in the |
| 131 | * bios array */ |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 132 | return kzalloc(size, gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static void r10bio_pool_free(void *r10_bio, void *data) |
| 136 | { |
| 137 | kfree(r10_bio); |
| 138 | } |
| 139 | |
Guoqing Jiang | 8db8791 | 2017-10-24 15:11:52 +0800 | [diff] [blame] | 140 | #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) |
NeilBrown | 0310fa2 | 2008-08-05 15:54:14 +1000 | [diff] [blame] | 141 | /* amount of memory to reserve for resync requests */ |
| 142 | #define RESYNC_WINDOW (1024*1024) |
| 143 | /* maximum number of concurrent requests, memory permitting */ |
| 144 | #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) |
Guoqing Jiang | 4b242e9 | 2018-01-19 11:37:56 +0800 | [diff] [blame] | 145 | #define CLUSTER_RESYNC_WINDOW (32 * RESYNC_WINDOW) |
Guoqing Jiang | 8db8791 | 2017-10-24 15:11:52 +0800 | [diff] [blame] | 146 | #define CLUSTER_RESYNC_WINDOW_SECTORS (CLUSTER_RESYNC_WINDOW >> 9) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | |
| 148 | /* |
| 149 | * When performing a resync, we need to read and compare, so |
| 150 | * we need as many pages are there are copies. |
| 151 | * When performing a recovery, we need 2 bios, one for read, |
| 152 | * one for write (we recover only one drive per r10buf) |
| 153 | * |
| 154 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 155 | static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 157 | struct r10conf *conf = data; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 158 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | struct bio *bio; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 160 | int j; |
| 161 | int nalloc, nalloc_rp; |
| 162 | struct resync_pages *rps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
| 164 | r10_bio = r10bio_pool_alloc(gfp_flags, conf); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 165 | if (!r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 168 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) || |
| 169 | test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | nalloc = conf->copies; /* resync */ |
| 171 | else |
| 172 | nalloc = 2; /* recovery */ |
| 173 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 174 | /* allocate once for all bios */ |
| 175 | if (!conf->have_replacement) |
| 176 | nalloc_rp = nalloc; |
| 177 | else |
| 178 | nalloc_rp = nalloc * 2; |
Kees Cook | 6da2ec5 | 2018-06-12 13:55:00 -0700 | [diff] [blame] | 179 | rps = kmalloc_array(nalloc_rp, sizeof(struct resync_pages), gfp_flags); |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 180 | if (!rps) |
| 181 | goto out_free_r10bio; |
| 182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | /* |
| 184 | * Allocate bios. |
| 185 | */ |
| 186 | for (j = nalloc ; j-- ; ) { |
NeilBrown | 6746557 | 2010-10-26 17:33:54 +1100 | [diff] [blame] | 187 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 188 | if (!bio) |
| 189 | goto out_free_bio; |
| 190 | r10_bio->devs[j].bio = bio; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 191 | if (!conf->have_replacement) |
| 192 | continue; |
| 193 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
| 194 | if (!bio) |
| 195 | goto out_free_bio; |
| 196 | r10_bio->devs[j].repl_bio = bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 197 | } |
| 198 | /* |
| 199 | * Allocate RESYNC_PAGES data pages and attach them |
| 200 | * where needed. |
| 201 | */ |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 202 | for (j = 0; j < nalloc; j++) { |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 203 | struct bio *rbio = r10_bio->devs[j].repl_bio; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 204 | struct resync_pages *rp, *rp_repl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 206 | rp = &rps[j]; |
| 207 | if (rbio) |
| 208 | rp_repl = &rps[nalloc + j]; |
| 209 | |
| 210 | bio = r10_bio->devs[j].bio; |
| 211 | |
| 212 | if (!j || test_bit(MD_RECOVERY_SYNC, |
| 213 | &conf->mddev->recovery)) { |
| 214 | if (resync_alloc_pages(rp, gfp_flags)) |
| 215 | goto out_free_pages; |
| 216 | } else { |
| 217 | memcpy(rp, &rps[0], sizeof(*rp)); |
| 218 | resync_get_all_pages(rp); |
| 219 | } |
| 220 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 221 | rp->raid_bio = r10_bio; |
| 222 | bio->bi_private = rp; |
| 223 | if (rbio) { |
| 224 | memcpy(rp_repl, rp, sizeof(*rp)); |
| 225 | rbio->bi_private = rp_repl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | } |
| 228 | |
| 229 | return r10_bio; |
| 230 | |
| 231 | out_free_pages: |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 232 | while (--j >= 0) |
| 233 | resync_free_pages(&rps[j * 2]); |
| 234 | |
majianpeng | 5fdd2cf | 2012-05-22 13:55:03 +1000 | [diff] [blame] | 235 | j = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | out_free_bio: |
majianpeng | 5fdd2cf | 2012-05-22 13:55:03 +1000 | [diff] [blame] | 237 | for ( ; j < nalloc; j++) { |
| 238 | if (r10_bio->devs[j].bio) |
| 239 | bio_put(r10_bio->devs[j].bio); |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 240 | if (r10_bio->devs[j].repl_bio) |
| 241 | bio_put(r10_bio->devs[j].repl_bio); |
| 242 | } |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 243 | kfree(rps); |
| 244 | out_free_r10bio: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | r10bio_pool_free(r10_bio, conf); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
| 249 | static void r10buf_pool_free(void *__r10_bio, void *data) |
| 250 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 251 | struct r10conf *conf = data; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 252 | struct r10bio *r10bio = __r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | int j; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 254 | struct resync_pages *rp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 255 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 256 | for (j = conf->copies; j--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | struct bio *bio = r10bio->devs[j].bio; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 258 | |
Guoqing Jiang | eb81b32 | 2018-04-26 10:56:37 +0800 | [diff] [blame] | 259 | if (bio) { |
| 260 | rp = get_resync_pages(bio); |
| 261 | resync_free_pages(rp); |
| 262 | bio_put(bio); |
| 263 | } |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 264 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 265 | bio = r10bio->devs[j].repl_bio; |
| 266 | if (bio) |
| 267 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 268 | } |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 269 | |
| 270 | /* resync pages array stored in the 1st bio's .bi_private */ |
| 271 | kfree(rp); |
| 272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | r10bio_pool_free(r10bio, conf); |
| 274 | } |
| 275 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 276 | static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | { |
| 278 | int i; |
| 279 | |
| 280 | for (i = 0; i < conf->copies; i++) { |
| 281 | struct bio **bio = & r10_bio->devs[i].bio; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 282 | if (!BIO_SPECIAL(*bio)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | bio_put(*bio); |
| 284 | *bio = NULL; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 285 | bio = &r10_bio->devs[i].repl_bio; |
| 286 | if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio)) |
| 287 | bio_put(*bio); |
| 288 | *bio = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 292 | static void free_r10bio(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 294 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | put_all_bios(conf, r10_bio); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 297 | mempool_free(r10_bio, &conf->r10bio_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | } |
| 299 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 300 | static void put_buf(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 302 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 304 | mempool_free(r10_bio, &conf->r10buf_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 306 | lower_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | } |
| 308 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 309 | static void reschedule_retry(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | { |
| 311 | unsigned long flags; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 312 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 313 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 314 | |
| 315 | spin_lock_irqsave(&conf->device_lock, flags); |
| 316 | list_add(&r10_bio->retry_list, &conf->retry_list); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 317 | conf->nr_queued ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 319 | |
Arthur Jones | 388667b | 2008-07-25 12:03:38 -0700 | [diff] [blame] | 320 | /* wake up frozen array... */ |
| 321 | wake_up(&conf->wait_barrier); |
| 322 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | md_wakeup_thread(mddev->thread); |
| 324 | } |
| 325 | |
| 326 | /* |
| 327 | * raid_end_bio_io() is called when we have finished servicing a mirrored |
| 328 | * operation and are ready to return a success/failure code to the buffer |
| 329 | * cache layer. |
| 330 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 331 | static void raid_end_bio_io(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | { |
| 333 | struct bio *bio = r10_bio->master_bio; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 334 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 336 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 337 | bio->bi_status = BLK_STS_IOERR; |
NeilBrown | fd16f2e | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 338 | |
| 339 | bio_endio(bio); |
| 340 | /* |
| 341 | * Wake up any possible resync thread that waits for the device |
| 342 | * to go idle. |
| 343 | */ |
| 344 | allow_barrier(conf); |
| 345 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | free_r10bio(r10_bio); |
| 347 | } |
| 348 | |
| 349 | /* |
| 350 | * Update disk head position estimator based on IRQ completion info. |
| 351 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 352 | static inline void update_head_pos(int slot, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 354 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | |
| 356 | conf->mirrors[r10_bio->devs[slot].devnum].head_position = |
| 357 | r10_bio->devs[slot].addr + (r10_bio->sectors); |
| 358 | } |
| 359 | |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 360 | /* |
| 361 | * Find the disk number which triggered given bio |
| 362 | */ |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 363 | static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio, |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 364 | struct bio *bio, int *slotp, int *replp) |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 365 | { |
| 366 | int slot; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 367 | int repl = 0; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 368 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 369 | for (slot = 0; slot < conf->copies; slot++) { |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 370 | if (r10_bio->devs[slot].bio == bio) |
| 371 | break; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 372 | if (r10_bio->devs[slot].repl_bio == bio) { |
| 373 | repl = 1; |
| 374 | break; |
| 375 | } |
| 376 | } |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 377 | |
| 378 | BUG_ON(slot == conf->copies); |
| 379 | update_head_pos(slot, r10_bio); |
| 380 | |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 381 | if (slotp) |
| 382 | *slotp = slot; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 383 | if (replp) |
| 384 | *replp = repl; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 385 | return r10_bio->devs[slot].devnum; |
| 386 | } |
| 387 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 388 | static void raid10_end_read_request(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 390 | int uptodate = !bio->bi_status; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 391 | struct r10bio *r10_bio = bio->bi_private; |
Colin Ian King | a0e764c | 2017-10-11 11:46:54 +0100 | [diff] [blame] | 392 | int slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 393 | struct md_rdev *rdev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 394 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 395 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 396 | slot = r10_bio->read_slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 397 | rdev = r10_bio->devs[slot].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | /* |
| 399 | * this branch is our 'one mirror IO has finished' event handler: |
| 400 | */ |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 401 | update_head_pos(slot, r10_bio); |
| 402 | |
| 403 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | /* |
| 405 | * Set R10BIO_Uptodate in our master bio, so that |
| 406 | * we will return a good error code to the higher |
| 407 | * levels even if IO on some other mirrored buffer fails. |
| 408 | * |
| 409 | * The 'master' represents the composite IO operation to |
| 410 | * user-side. So if something waits for IO, then it will |
| 411 | * wait for the 'master' bio. |
| 412 | */ |
| 413 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 414 | } else { |
| 415 | /* If all other devices that store this block have |
| 416 | * failed, we want to return the error upwards rather |
| 417 | * than fail the last device. Here we redefine |
| 418 | * "uptodate" to mean "Don't want to retry" |
| 419 | */ |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 420 | if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state), |
| 421 | rdev->raid_disk)) |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 422 | uptodate = 1; |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 423 | } |
| 424 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | raid_end_bio_io(r10_bio); |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 426 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 427 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | /* |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 429 | * oops, read error - keep the refcount on the rdev |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | */ |
| 431 | char b[BDEVNAME_SIZE]; |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 432 | pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 433 | mdname(conf->mddev), |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 434 | bdevname(rdev->bdev, b), |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 435 | (unsigned long long)r10_bio->sector); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 436 | set_bit(R10BIO_ReadError, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 437 | reschedule_retry(r10_bio); |
| 438 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | } |
| 440 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 441 | static void close_write(struct r10bio *r10_bio) |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 442 | { |
| 443 | /* clear the bitmap if all writes complete successfully */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 444 | md_bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, |
| 445 | r10_bio->sectors, |
| 446 | !test_bit(R10BIO_Degraded, &r10_bio->state), |
| 447 | 0); |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 448 | md_write_end(r10_bio->mddev); |
| 449 | } |
| 450 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 451 | static void one_write_done(struct r10bio *r10_bio) |
NeilBrown | 19d5f83 | 2011-09-10 17:21:17 +1000 | [diff] [blame] | 452 | { |
| 453 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 454 | if (test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 455 | reschedule_retry(r10_bio); |
| 456 | else { |
| 457 | close_write(r10_bio); |
| 458 | if (test_bit(R10BIO_MadeGood, &r10_bio->state)) |
| 459 | reschedule_retry(r10_bio); |
| 460 | else |
| 461 | raid_end_bio_io(r10_bio); |
| 462 | } |
| 463 | } |
| 464 | } |
| 465 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 466 | static void raid10_end_write_request(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 468 | struct r10bio *r10_bio = bio->bi_private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 469 | int dev; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 470 | int dec_rdev = 1; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 471 | struct r10conf *conf = r10_bio->mddev->private; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 472 | int slot, repl; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 473 | struct md_rdev *rdev = NULL; |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 474 | struct bio *to_put = NULL; |
Shaohua Li | 579ed34 | 2016-10-06 14:13:52 -0700 | [diff] [blame] | 475 | bool discard_error; |
| 476 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 477 | discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 479 | dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 481 | if (repl) |
| 482 | rdev = conf->mirrors[dev].replacement; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 483 | if (!rdev) { |
| 484 | smp_rmb(); |
| 485 | repl = 0; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 486 | rdev = conf->mirrors[dev].rdev; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 487 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | /* |
| 489 | * this branch is our 'one mirror IO has finished' event handler: |
| 490 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 491 | if (bio->bi_status && !discard_error) { |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 492 | if (repl) |
| 493 | /* Never record new bad blocks to replacement, |
| 494 | * just fail it. |
| 495 | */ |
| 496 | md_error(rdev->mddev, rdev); |
| 497 | else { |
| 498 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 499 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 500 | set_bit(MD_RECOVERY_NEEDED, |
| 501 | &rdev->mddev->recovery); |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 502 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 503 | dec_rdev = 0; |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 504 | if (test_bit(FailFast, &rdev->flags) && |
| 505 | (bio->bi_opf & MD_FAILFAST)) { |
| 506 | md_error(rdev->mddev, rdev); |
| 507 | if (!test_bit(Faulty, &rdev->flags)) |
| 508 | /* This is the only remaining device, |
| 509 | * We need to retry the write without |
| 510 | * FailFast |
| 511 | */ |
| 512 | set_bit(R10BIO_WriteError, &r10_bio->state); |
| 513 | else { |
| 514 | r10_bio->devs[slot].bio = NULL; |
| 515 | to_put = bio; |
| 516 | dec_rdev = 1; |
| 517 | } |
| 518 | } else |
| 519 | set_bit(R10BIO_WriteError, &r10_bio->state); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 520 | } |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 521 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | /* |
| 523 | * Set R10BIO_Uptodate in our master bio, so that |
| 524 | * we will return a good error code for to the higher |
| 525 | * levels even if IO on some other mirrored buffer fails. |
| 526 | * |
| 527 | * The 'master' represents the composite IO operation to |
| 528 | * user-side. So if something waits for IO, then it will |
| 529 | * wait for the 'master' bio. |
| 530 | */ |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 531 | sector_t first_bad; |
| 532 | int bad_sectors; |
| 533 | |
Alex Lyakas | 3056e3a | 2013-06-04 20:42:21 +0300 | [diff] [blame] | 534 | /* |
| 535 | * Do not set R10BIO_Uptodate if the current device is |
| 536 | * rebuilding or Faulty. This is because we cannot use |
| 537 | * such device for properly reading the data back (we could |
| 538 | * potentially use it, if the current write would have felt |
| 539 | * before rdev->recovery_offset, but for simplicity we don't |
| 540 | * check this here. |
| 541 | */ |
| 542 | if (test_bit(In_sync, &rdev->flags) && |
| 543 | !test_bit(Faulty, &rdev->flags)) |
| 544 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 546 | /* Maybe we can clear some bad blocks. */ |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 547 | if (is_badblock(rdev, |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 548 | r10_bio->devs[slot].addr, |
| 549 | r10_bio->sectors, |
Shaohua Li | 579ed34 | 2016-10-06 14:13:52 -0700 | [diff] [blame] | 550 | &first_bad, &bad_sectors) && !discard_error) { |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 551 | bio_put(bio); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 552 | if (repl) |
| 553 | r10_bio->devs[slot].repl_bio = IO_MADE_GOOD; |
| 554 | else |
| 555 | r10_bio->devs[slot].bio = IO_MADE_GOOD; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 556 | dec_rdev = 0; |
| 557 | set_bit(R10BIO_MadeGood, &r10_bio->state); |
| 558 | } |
| 559 | } |
| 560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | /* |
| 562 | * |
| 563 | * Let's see if all mirrored write operations have finished |
| 564 | * already. |
| 565 | */ |
NeilBrown | 19d5f83 | 2011-09-10 17:21:17 +1000 | [diff] [blame] | 566 | one_write_done(r10_bio); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 567 | if (dec_rdev) |
NeilBrown | 884162d | 2012-11-22 15:12:09 +1100 | [diff] [blame] | 568 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 569 | if (to_put) |
| 570 | bio_put(to_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 571 | } |
| 572 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 573 | /* |
| 574 | * RAID10 layout manager |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 575 | * As well as the chunksize and raid_disks count, there are two |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | * parameters: near_copies and far_copies. |
| 577 | * near_copies * far_copies must be <= raid_disks. |
| 578 | * Normally one of these will be 1. |
| 579 | * If both are 1, we get raid0. |
| 580 | * If near_copies == raid_disks, we get raid1. |
| 581 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 582 | * Chunks are laid out in raid0 style with near_copies copies of the |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | * first chunk, followed by near_copies copies of the next chunk and |
| 584 | * so on. |
| 585 | * If far_copies > 1, then after 1/far_copies of the array has been assigned |
| 586 | * as described above, we start again with a device offset of near_copies. |
| 587 | * So we effectively have another copy of the whole array further down all |
| 588 | * the drives, but with blocks on different drives. |
| 589 | * With this layout, and block is never stored twice on the one device. |
| 590 | * |
| 591 | * raid10_find_phys finds the sector offset of a given virtual sector |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 592 | * on each device that it is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | * |
| 594 | * raid10_find_virt does the reverse mapping, from a device and a |
| 595 | * sector offset to a virtual address |
| 596 | */ |
| 597 | |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 598 | static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | { |
| 600 | int n,f; |
| 601 | sector_t sector; |
| 602 | sector_t chunk; |
| 603 | sector_t stripe; |
| 604 | int dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | int slot = 0; |
Jonathan Brassow | 9a3152a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 606 | int last_far_set_start, last_far_set_size; |
| 607 | |
| 608 | last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1; |
| 609 | last_far_set_start *= geo->far_set_size; |
| 610 | |
| 611 | last_far_set_size = geo->far_set_size; |
| 612 | last_far_set_size += (geo->raid_disks % geo->far_set_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | |
| 614 | /* now calculate first sector/dev */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 615 | chunk = r10bio->sector >> geo->chunk_shift; |
| 616 | sector = r10bio->sector & geo->chunk_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 618 | chunk *= geo->near_copies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | stripe = chunk; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 620 | dev = sector_div(stripe, geo->raid_disks); |
| 621 | if (geo->far_offset) |
| 622 | stripe *= geo->far_copies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 623 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 624 | sector += stripe << geo->chunk_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | |
| 626 | /* and calculate all the others */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 627 | for (n = 0; n < geo->near_copies; n++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 628 | int d = dev; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 629 | int set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 630 | sector_t s = sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 631 | r10bio->devs[slot].devnum = d; |
Jonathan Brassow | 4c0ca26 | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 632 | r10bio->devs[slot].addr = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | slot++; |
| 634 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 635 | for (f = 1; f < geo->far_copies; f++) { |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 636 | set = d / geo->far_set_size; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 637 | d += geo->near_copies; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 638 | |
Jonathan Brassow | 9a3152a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 639 | if ((geo->raid_disks % geo->far_set_size) && |
| 640 | (d > last_far_set_start)) { |
| 641 | d -= last_far_set_start; |
| 642 | d %= last_far_set_size; |
| 643 | d += last_far_set_start; |
| 644 | } else { |
| 645 | d %= geo->far_set_size; |
| 646 | d += geo->far_set_size * set; |
| 647 | } |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 648 | s += geo->stride; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | r10bio->devs[slot].devnum = d; |
| 650 | r10bio->devs[slot].addr = s; |
| 651 | slot++; |
| 652 | } |
| 653 | dev++; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 654 | if (dev >= geo->raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | dev = 0; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 656 | sector += (geo->chunk_mask + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | } |
| 658 | } |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio) |
| 662 | { |
| 663 | struct geom *geo = &conf->geo; |
| 664 | |
| 665 | if (conf->reshape_progress != MaxSector && |
| 666 | ((r10bio->sector >= conf->reshape_progress) != |
| 667 | conf->mddev->reshape_backwards)) { |
| 668 | set_bit(R10BIO_Previous, &r10bio->state); |
| 669 | geo = &conf->prev; |
| 670 | } else |
| 671 | clear_bit(R10BIO_Previous, &r10bio->state); |
| 672 | |
| 673 | __raid10_find_phys(geo, r10bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 674 | } |
| 675 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 676 | static sector_t raid10_find_virt(struct r10conf *conf, sector_t sector, int dev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | { |
| 678 | sector_t offset, chunk, vchunk; |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 679 | /* Never use conf->prev as this is only called during resync |
| 680 | * or recovery, so reshape isn't happening |
| 681 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 682 | struct geom *geo = &conf->geo; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 683 | int far_set_start = (dev / geo->far_set_size) * geo->far_set_size; |
| 684 | int far_set_size = geo->far_set_size; |
Jonathan Brassow | 9a3152a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 685 | int last_far_set_start; |
| 686 | |
| 687 | if (geo->raid_disks % geo->far_set_size) { |
| 688 | last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1; |
| 689 | last_far_set_start *= geo->far_set_size; |
| 690 | |
| 691 | if (dev >= last_far_set_start) { |
| 692 | far_set_size = geo->far_set_size; |
| 693 | far_set_size += (geo->raid_disks % geo->far_set_size); |
| 694 | far_set_start = last_far_set_start; |
| 695 | } |
| 696 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 698 | offset = sector & geo->chunk_mask; |
| 699 | if (geo->far_offset) { |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 700 | int fc; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 701 | chunk = sector >> geo->chunk_shift; |
| 702 | fc = sector_div(chunk, geo->far_copies); |
| 703 | dev -= fc * geo->near_copies; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 704 | if (dev < far_set_start) |
| 705 | dev += far_set_size; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 706 | } else { |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 707 | while (sector >= geo->stride) { |
| 708 | sector -= geo->stride; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 709 | if (dev < (geo->near_copies + far_set_start)) |
| 710 | dev += far_set_size - geo->near_copies; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 711 | else |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 712 | dev -= geo->near_copies; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 713 | } |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 714 | chunk = sector >> geo->chunk_shift; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 715 | } |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 716 | vchunk = chunk * geo->raid_disks + dev; |
| 717 | sector_div(vchunk, geo->near_copies); |
| 718 | return (vchunk << geo->chunk_shift) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 721 | /* |
| 722 | * This routine returns the disk from which the requested read should |
| 723 | * be done. There is a per-array 'next expected sequential IO' sector |
| 724 | * number - if this matches on the next IO then we use the last disk. |
| 725 | * There is also a per-disk 'last know head position' sector that is |
| 726 | * maintained from IRQ contexts, both the normal and the resync IO |
| 727 | * completion handlers update this position correctly. If there is no |
| 728 | * perfect sequential match then we pick the disk whose head is closest. |
| 729 | * |
| 730 | * If there are 2 mirrors in the same 2 devices, performance degrades |
| 731 | * because position is mirror, not device based. |
| 732 | * |
| 733 | * The rdev for the device selected will have nr_pending incremented. |
| 734 | */ |
| 735 | |
| 736 | /* |
| 737 | * FIXME: possibly should rethink readbalancing and do it differently |
| 738 | * depending on near_copies / far_copies geometry. |
| 739 | */ |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 740 | static struct md_rdev *read_balance(struct r10conf *conf, |
| 741 | struct r10bio *r10_bio, |
| 742 | int *max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 743 | { |
NeilBrown | af3a2cd | 2010-05-08 08:20:17 +1000 | [diff] [blame] | 744 | const sector_t this_sector = r10_bio->sector; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 745 | int disk, slot; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 746 | int sectors = r10_bio->sectors; |
| 747 | int best_good_sectors; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 748 | sector_t new_distance, best_dist; |
Jonathan Brassow | 3bbae04 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 749 | struct md_rdev *best_rdev, *rdev = NULL; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 750 | int do_balance; |
| 751 | int best_slot; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 752 | struct geom *geo = &conf->geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 753 | |
| 754 | raid10_find_phys(conf, r10_bio); |
| 755 | rcu_read_lock(); |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 756 | best_slot = -1; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 757 | best_rdev = NULL; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 758 | best_dist = MaxSector; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 759 | best_good_sectors = 0; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 760 | do_balance = 1; |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 761 | clear_bit(R10BIO_FailFast, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 762 | /* |
| 763 | * Check if we can balance. We can balance on the whole |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 764 | * device if no resync is going on (recovery is ok), or below |
| 765 | * the resync window. We take the first readable disk when |
| 766 | * above the resync window. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 767 | */ |
Guoqing Jiang | d4098c7 | 2017-10-24 15:11:50 +0800 | [diff] [blame] | 768 | if ((conf->mddev->recovery_cp < MaxSector |
| 769 | && (this_sector + sectors >= conf->next_resync)) || |
| 770 | (mddev_is_clustered(conf->mddev) && |
| 771 | md_cluster_ops->area_resyncing(conf->mddev, READ, this_sector, |
| 772 | this_sector + sectors))) |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 773 | do_balance = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 775 | for (slot = 0; slot < conf->copies ; slot++) { |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 776 | sector_t first_bad; |
| 777 | int bad_sectors; |
| 778 | sector_t dev_sector; |
| 779 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 780 | if (r10_bio->devs[slot].bio == IO_BLOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 781 | continue; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 782 | disk = r10_bio->devs[slot].devnum; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 783 | rdev = rcu_dereference(conf->mirrors[disk].replacement); |
| 784 | if (rdev == NULL || test_bit(Faulty, &rdev->flags) || |
| 785 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) |
| 786 | rdev = rcu_dereference(conf->mirrors[disk].rdev); |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 787 | if (rdev == NULL || |
Kent Overstreet | 8ae1266 | 2015-04-27 23:48:34 -0700 | [diff] [blame] | 788 | test_bit(Faulty, &rdev->flags)) |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 789 | continue; |
| 790 | if (!test_bit(In_sync, &rdev->flags) && |
| 791 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 792 | continue; |
| 793 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 794 | dev_sector = r10_bio->devs[slot].addr; |
| 795 | if (is_badblock(rdev, dev_sector, sectors, |
| 796 | &first_bad, &bad_sectors)) { |
| 797 | if (best_dist < MaxSector) |
| 798 | /* Already have a better slot */ |
| 799 | continue; |
| 800 | if (first_bad <= dev_sector) { |
| 801 | /* Cannot read here. If this is the |
| 802 | * 'primary' device, then we must not read |
| 803 | * beyond 'bad_sectors' from another device. |
| 804 | */ |
| 805 | bad_sectors -= (dev_sector - first_bad); |
| 806 | if (!do_balance && sectors > bad_sectors) |
| 807 | sectors = bad_sectors; |
| 808 | if (best_good_sectors > sectors) |
| 809 | best_good_sectors = sectors; |
| 810 | } else { |
| 811 | sector_t good_sectors = |
| 812 | first_bad - dev_sector; |
| 813 | if (good_sectors > best_good_sectors) { |
| 814 | best_good_sectors = good_sectors; |
| 815 | best_slot = slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 816 | best_rdev = rdev; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 817 | } |
| 818 | if (!do_balance) |
| 819 | /* Must read from here */ |
| 820 | break; |
| 821 | } |
| 822 | continue; |
| 823 | } else |
| 824 | best_good_sectors = sectors; |
| 825 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 826 | if (!do_balance) |
| 827 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 828 | |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 829 | if (best_slot >= 0) |
| 830 | /* At least 2 disks to choose from so failfast is OK */ |
| 831 | set_bit(R10BIO_FailFast, &r10_bio->state); |
NeilBrown | 22dfdf5 | 2005-11-28 13:44:09 -0800 | [diff] [blame] | 832 | /* This optimisation is debatable, and completely destroys |
| 833 | * sequential read speed for 'far copies' arrays. So only |
| 834 | * keep it for 'near' arrays, and review those later. |
| 835 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 836 | if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending)) |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 837 | new_distance = 0; |
Keld Simonsen | 8ed3a19 | 2008-03-04 14:29:34 -0800 | [diff] [blame] | 838 | |
| 839 | /* for far > 1 always use the lowest address */ |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 840 | else if (geo->far_copies > 1) |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 841 | new_distance = r10_bio->devs[slot].addr; |
Keld Simonsen | 8ed3a19 | 2008-03-04 14:29:34 -0800 | [diff] [blame] | 842 | else |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 843 | new_distance = abs(r10_bio->devs[slot].addr - |
| 844 | conf->mirrors[disk].head_position); |
| 845 | if (new_distance < best_dist) { |
| 846 | best_dist = new_distance; |
| 847 | best_slot = slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 848 | best_rdev = rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 849 | } |
| 850 | } |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 851 | if (slot >= conf->copies) { |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 852 | slot = best_slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 853 | rdev = best_rdev; |
| 854 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 856 | if (slot >= 0) { |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 857 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 858 | r10_bio->read_slot = slot; |
| 859 | } else |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 860 | rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 861 | rcu_read_unlock(); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 862 | *max_sectors = best_good_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 864 | return rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 865 | } |
| 866 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 867 | static int raid10_congested(struct mddev *mddev, int bits) |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 868 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 869 | struct r10conf *conf = mddev->private; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 870 | int i, ret = 0; |
| 871 | |
Tejun Heo | 4452226 | 2015-05-22 17:13:26 -0400 | [diff] [blame] | 872 | if ((bits & (1 << WB_async_congested)) && |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 873 | conf->pending_count >= max_queued_requests) |
| 874 | return 1; |
| 875 | |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 876 | rcu_read_lock(); |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 877 | for (i = 0; |
| 878 | (i < conf->geo.raid_disks || i < conf->prev.raid_disks) |
| 879 | && ret == 0; |
| 880 | i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 881 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 882 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 883 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 884 | |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 885 | ret |= bdi_congested(q->backing_dev_info, bits); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 886 | } |
| 887 | } |
| 888 | rcu_read_unlock(); |
| 889 | return ret; |
| 890 | } |
| 891 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 892 | static void flush_pending_writes(struct r10conf *conf) |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 893 | { |
| 894 | /* Any writes that have been queued but are awaiting |
| 895 | * bitmap updates get flushed here. |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 896 | */ |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 897 | spin_lock_irq(&conf->device_lock); |
| 898 | |
| 899 | if (conf->pending_bio_list.head) { |
Shaohua Li | 18022a1 | 2017-12-01 12:12:34 -0800 | [diff] [blame] | 900 | struct blk_plug plug; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 901 | struct bio *bio; |
Shaohua Li | 18022a1 | 2017-12-01 12:12:34 -0800 | [diff] [blame] | 902 | |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 903 | bio = bio_list_get(&conf->pending_bio_list); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 904 | conf->pending_count = 0; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 905 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 474beb5 | 2017-12-04 08:21:04 +1100 | [diff] [blame] | 906 | |
| 907 | /* |
| 908 | * As this is called in a wait_event() loop (see freeze_array), |
| 909 | * current->state might be TASK_UNINTERRUPTIBLE which will |
| 910 | * cause a warning when we prepare to wait again. As it is |
| 911 | * rare that this path is taken, it is perfectly safe to force |
| 912 | * us to go around the wait_event() loop again, so the warning |
| 913 | * is a false-positive. Silence the warning by resetting |
| 914 | * thread state |
| 915 | */ |
| 916 | __set_current_state(TASK_RUNNING); |
| 917 | |
Shaohua Li | 18022a1 | 2017-12-01 12:12:34 -0800 | [diff] [blame] | 918 | blk_start_plug(&plug); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 919 | /* flush any pending bitmap writes to disk |
| 920 | * before proceeding w/ I/O */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 921 | md_bitmap_unplug(conf->mddev->bitmap); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 922 | wake_up(&conf->wait_barrier); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 923 | |
| 924 | while (bio) { /* submit pending writes */ |
| 925 | struct bio *next = bio->bi_next; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 926 | struct md_rdev *rdev = (void*)bio->bi_disk; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 927 | bio->bi_next = NULL; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 928 | bio_set_dev(bio, rdev->bdev); |
NeilBrown | a9ae93c | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 929 | if (test_bit(Faulty, &rdev->flags)) { |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 930 | bio_io_error(bio); |
NeilBrown | a9ae93c | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 931 | } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) && |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 932 | !blk_queue_discard(bio->bi_disk->queue))) |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 933 | /* Just ignore it */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 934 | bio_endio(bio); |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 935 | else |
| 936 | generic_make_request(bio); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 937 | bio = next; |
| 938 | } |
Shaohua Li | 18022a1 | 2017-12-01 12:12:34 -0800 | [diff] [blame] | 939 | blk_finish_plug(&plug); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 940 | } else |
| 941 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 942 | } |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 943 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 944 | /* Barriers.... |
| 945 | * Sometimes we need to suspend IO while we do something else, |
| 946 | * either some resync/recovery, or reconfigure the array. |
| 947 | * To do this we raise a 'barrier'. |
| 948 | * The 'barrier' is a counter that can be raised multiple times |
| 949 | * to count how many activities are happening which preclude |
| 950 | * normal IO. |
| 951 | * We can only raise the barrier if there is no pending IO. |
| 952 | * i.e. if nr_pending == 0. |
| 953 | * We choose only to raise the barrier if no-one is waiting for the |
| 954 | * barrier to go down. This means that as soon as an IO request |
| 955 | * is ready, no other operations which require a barrier will start |
| 956 | * until the IO request has had a chance. |
| 957 | * |
| 958 | * So: regular IO calls 'wait_barrier'. When that returns there |
| 959 | * is no backgroup IO happening, It must arrange to call |
| 960 | * allow_barrier when it has finished its IO. |
| 961 | * backgroup IO calls must call raise_barrier. Once that returns |
| 962 | * there is no normal IO happeing. It must arrange to call |
| 963 | * lower_barrier when the particular background IO completes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 965 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 966 | static void raise_barrier(struct r10conf *conf, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 967 | { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 968 | BUG_ON(force && !conf->barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | spin_lock_irq(&conf->resync_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 970 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 971 | /* Wait until no block IO is waiting (unless 'force') */ |
| 972 | wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 973 | conf->resync_lock); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 974 | |
| 975 | /* block any new IO from starting */ |
| 976 | conf->barrier++; |
| 977 | |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 978 | /* Now wait for all pending IO to complete */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 979 | wait_event_lock_irq(conf->wait_barrier, |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 980 | !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 981 | conf->resync_lock); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 982 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | spin_unlock_irq(&conf->resync_lock); |
| 984 | } |
| 985 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 986 | static void lower_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 987 | { |
| 988 | unsigned long flags; |
| 989 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 990 | conf->barrier--; |
| 991 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 992 | wake_up(&conf->wait_barrier); |
| 993 | } |
| 994 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 995 | static void wait_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 996 | { |
| 997 | spin_lock_irq(&conf->resync_lock); |
| 998 | if (conf->barrier) { |
| 999 | conf->nr_waiting++; |
NeilBrown | d6b42dc | 2012-03-19 12:46:38 +1100 | [diff] [blame] | 1000 | /* Wait for the barrier to drop. |
| 1001 | * However if there are already pending |
| 1002 | * requests (preventing the barrier from |
| 1003 | * rising completely), and the |
| 1004 | * pre-process bio queue isn't empty, |
| 1005 | * then don't wait, as we need to empty |
| 1006 | * that queue to get the nr_pending |
| 1007 | * count down. |
| 1008 | */ |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1009 | raid10_log(conf->mddev, "wait barrier"); |
NeilBrown | d6b42dc | 2012-03-19 12:46:38 +1100 | [diff] [blame] | 1010 | wait_event_lock_irq(conf->wait_barrier, |
| 1011 | !conf->barrier || |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1012 | (atomic_read(&conf->nr_pending) && |
NeilBrown | d6b42dc | 2012-03-19 12:46:38 +1100 | [diff] [blame] | 1013 | current->bio_list && |
NeilBrown | f5fe1b5 | 2017-03-10 17:00:47 +1100 | [diff] [blame] | 1014 | (!bio_list_empty(¤t->bio_list[0]) || |
| 1015 | !bio_list_empty(¤t->bio_list[1]))), |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1016 | conf->resync_lock); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1017 | conf->nr_waiting--; |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1018 | if (!conf->nr_waiting) |
| 1019 | wake_up(&conf->wait_barrier); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1020 | } |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1021 | atomic_inc(&conf->nr_pending); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1022 | spin_unlock_irq(&conf->resync_lock); |
| 1023 | } |
| 1024 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1025 | static void allow_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1026 | { |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1027 | if ((atomic_dec_and_test(&conf->nr_pending)) || |
| 1028 | (conf->array_freeze_pending)) |
| 1029 | wake_up(&conf->wait_barrier); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1030 | } |
| 1031 | |
NeilBrown | e2d5992 | 2013-06-12 11:01:22 +1000 | [diff] [blame] | 1032 | static void freeze_array(struct r10conf *conf, int extra) |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1033 | { |
| 1034 | /* stop syncio and normal IO and wait for everything to |
NeilBrown | f188593 | 2006-01-06 00:20:42 -0800 | [diff] [blame] | 1035 | * go quiet. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1036 | * We increment barrier and nr_waiting, and then |
NeilBrown | e2d5992 | 2013-06-12 11:01:22 +1000 | [diff] [blame] | 1037 | * wait until nr_pending match nr_queued+extra |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 1038 | * This is called in the context of one normal IO request |
| 1039 | * that has failed. Thus any sync request that might be pending |
| 1040 | * will be blocked by nr_pending, and we need to wait for |
| 1041 | * pending IO requests to complete or be queued for re-try. |
NeilBrown | e2d5992 | 2013-06-12 11:01:22 +1000 | [diff] [blame] | 1042 | * Thus the number queued (nr_queued) plus this request (extra) |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 1043 | * must match the number of pending IOs (nr_pending) before |
| 1044 | * we continue. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1045 | */ |
| 1046 | spin_lock_irq(&conf->resync_lock); |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1047 | conf->array_freeze_pending++; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1048 | conf->barrier++; |
| 1049 | conf->nr_waiting++; |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1050 | wait_event_lock_irq_cmd(conf->wait_barrier, |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1051 | atomic_read(&conf->nr_pending) == conf->nr_queued+extra, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1052 | conf->resync_lock, |
| 1053 | flush_pending_writes(conf)); |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1054 | |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1055 | conf->array_freeze_pending--; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1056 | spin_unlock_irq(&conf->resync_lock); |
| 1057 | } |
| 1058 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1059 | static void unfreeze_array(struct r10conf *conf) |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1060 | { |
| 1061 | /* reverse the effect of the freeze */ |
| 1062 | spin_lock_irq(&conf->resync_lock); |
| 1063 | conf->barrier--; |
| 1064 | conf->nr_waiting--; |
| 1065 | wake_up(&conf->wait_barrier); |
| 1066 | spin_unlock_irq(&conf->resync_lock); |
| 1067 | } |
| 1068 | |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 1069 | static sector_t choose_data_offset(struct r10bio *r10_bio, |
| 1070 | struct md_rdev *rdev) |
| 1071 | { |
| 1072 | if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) || |
| 1073 | test_bit(R10BIO_Previous, &r10_bio->state)) |
| 1074 | return rdev->data_offset; |
| 1075 | else |
| 1076 | return rdev->new_data_offset; |
| 1077 | } |
| 1078 | |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1079 | struct raid10_plug_cb { |
| 1080 | struct blk_plug_cb cb; |
| 1081 | struct bio_list pending; |
| 1082 | int pending_cnt; |
| 1083 | }; |
| 1084 | |
| 1085 | static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule) |
| 1086 | { |
| 1087 | struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb, |
| 1088 | cb); |
| 1089 | struct mddev *mddev = plug->cb.data; |
| 1090 | struct r10conf *conf = mddev->private; |
| 1091 | struct bio *bio; |
| 1092 | |
NeilBrown | 874807a | 2012-11-27 12:14:40 +1100 | [diff] [blame] | 1093 | if (from_schedule || current->bio_list) { |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1094 | spin_lock_irq(&conf->device_lock); |
| 1095 | bio_list_merge(&conf->pending_bio_list, &plug->pending); |
| 1096 | conf->pending_count += plug->pending_cnt; |
| 1097 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ee0b024 | 2013-02-25 12:38:29 +1100 | [diff] [blame] | 1098 | wake_up(&conf->wait_barrier); |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1099 | md_wakeup_thread(mddev->thread); |
| 1100 | kfree(plug); |
| 1101 | return; |
| 1102 | } |
| 1103 | |
| 1104 | /* we aren't scheduling, so we can do the write-out directly. */ |
| 1105 | bio = bio_list_get(&plug->pending); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1106 | md_bitmap_unplug(mddev->bitmap); |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1107 | wake_up(&conf->wait_barrier); |
| 1108 | |
| 1109 | while (bio) { /* submit pending writes */ |
| 1110 | struct bio *next = bio->bi_next; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1111 | struct md_rdev *rdev = (void*)bio->bi_disk; |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1112 | bio->bi_next = NULL; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1113 | bio_set_dev(bio, rdev->bdev); |
NeilBrown | a9ae93c | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 1114 | if (test_bit(Faulty, &rdev->flags)) { |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 1115 | bio_io_error(bio); |
NeilBrown | a9ae93c | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 1116 | } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) && |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1117 | !blk_queue_discard(bio->bi_disk->queue))) |
Shaohua Li | 32f9f57 | 2013-04-28 18:26:38 +0800 | [diff] [blame] | 1118 | /* Just ignore it */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1119 | bio_endio(bio); |
Shaohua Li | 32f9f57 | 2013-04-28 18:26:38 +0800 | [diff] [blame] | 1120 | else |
| 1121 | generic_make_request(bio); |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1122 | bio = next; |
| 1123 | } |
| 1124 | kfree(plug); |
| 1125 | } |
| 1126 | |
Guoqing Jiang | caea3c4 | 2018-12-07 18:24:21 +0800 | [diff] [blame] | 1127 | /* |
| 1128 | * 1. Register the new request and wait if the reconstruction thread has put |
| 1129 | * up a bar for new requests. Continue immediately if no resync is active |
| 1130 | * currently. |
| 1131 | * 2. If IO spans the reshape position. Need to wait for reshape to pass. |
| 1132 | */ |
| 1133 | static void regular_request_wait(struct mddev *mddev, struct r10conf *conf, |
| 1134 | struct bio *bio, sector_t sectors) |
| 1135 | { |
| 1136 | wait_barrier(conf); |
| 1137 | while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 1138 | bio->bi_iter.bi_sector < conf->reshape_progress && |
| 1139 | bio->bi_iter.bi_sector + sectors > conf->reshape_progress) { |
| 1140 | raid10_log(conf->mddev, "wait reshape"); |
| 1141 | allow_barrier(conf); |
| 1142 | wait_event(conf->wait_barrier, |
| 1143 | conf->reshape_progress <= bio->bi_iter.bi_sector || |
| 1144 | conf->reshape_progress >= bio->bi_iter.bi_sector + |
| 1145 | sectors); |
| 1146 | wait_barrier(conf); |
| 1147 | } |
| 1148 | } |
| 1149 | |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1150 | static void raid10_read_request(struct mddev *mddev, struct bio *bio, |
| 1151 | struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1152 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1153 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | struct bio *read_bio; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1155 | const int op = bio_op(bio); |
| 1156 | const unsigned long do_sync = (bio->bi_opf & REQ_SYNC); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1157 | int max_sectors; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1158 | struct md_rdev *rdev; |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1159 | char b[BDEVNAME_SIZE]; |
| 1160 | int slot = r10_bio->read_slot; |
| 1161 | struct md_rdev *err_rdev = NULL; |
| 1162 | gfp_t gfp = GFP_NOIO; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1163 | |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1164 | if (r10_bio->devs[slot].rdev) { |
| 1165 | /* |
| 1166 | * This is an error retry, but we cannot |
| 1167 | * safely dereference the rdev in the r10_bio, |
| 1168 | * we must use the one in conf. |
| 1169 | * If it has already been disconnected (unlikely) |
| 1170 | * we lose the device name in error messages. |
| 1171 | */ |
| 1172 | int disk; |
| 1173 | /* |
| 1174 | * As we are blocking raid10, it is a little safer to |
| 1175 | * use __GFP_HIGH. |
| 1176 | */ |
| 1177 | gfp = GFP_NOIO | __GFP_HIGH; |
| 1178 | |
| 1179 | rcu_read_lock(); |
| 1180 | disk = r10_bio->devs[slot].devnum; |
| 1181 | err_rdev = rcu_dereference(conf->mirrors[disk].rdev); |
| 1182 | if (err_rdev) |
| 1183 | bdevname(err_rdev->bdev, b); |
| 1184 | else { |
| 1185 | strcpy(b, "???"); |
| 1186 | /* This never gets dereferenced */ |
| 1187 | err_rdev = r10_bio->devs[slot].rdev; |
| 1188 | } |
| 1189 | rcu_read_unlock(); |
| 1190 | } |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1191 | |
Guoqing Jiang | caea3c4 | 2018-12-07 18:24:21 +0800 | [diff] [blame] | 1192 | regular_request_wait(mddev, conf, bio, r10_bio->sectors); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1193 | rdev = read_balance(conf, r10_bio, &max_sectors); |
| 1194 | if (!rdev) { |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1195 | if (err_rdev) { |
| 1196 | pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n", |
| 1197 | mdname(mddev), b, |
| 1198 | (unsigned long long)r10_bio->sector); |
| 1199 | } |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1200 | raid_end_bio_io(r10_bio); |
| 1201 | return; |
| 1202 | } |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1203 | if (err_rdev) |
| 1204 | pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n", |
| 1205 | mdname(mddev), |
| 1206 | bdevname(rdev->bdev, b), |
| 1207 | (unsigned long long)r10_bio->sector); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1208 | if (max_sectors < bio_sectors(bio)) { |
| 1209 | struct bio *split = bio_split(bio, max_sectors, |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 1210 | gfp, &conf->bio_split); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1211 | bio_chain(split, bio); |
Guoqing Jiang | e820d55 | 2018-12-19 14:19:25 +0800 | [diff] [blame^] | 1212 | allow_barrier(conf); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1213 | generic_make_request(bio); |
Guoqing Jiang | e820d55 | 2018-12-19 14:19:25 +0800 | [diff] [blame^] | 1214 | wait_barrier(conf); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1215 | bio = split; |
| 1216 | r10_bio->master_bio = bio; |
| 1217 | r10_bio->sectors = max_sectors; |
| 1218 | } |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1219 | slot = r10_bio->read_slot; |
| 1220 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 1221 | read_bio = bio_clone_fast(bio, gfp, &mddev->bio_set); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1222 | |
| 1223 | r10_bio->devs[slot].bio = read_bio; |
| 1224 | r10_bio->devs[slot].rdev = rdev; |
| 1225 | |
| 1226 | read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr + |
| 1227 | choose_data_offset(r10_bio, rdev); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1228 | bio_set_dev(read_bio, rdev->bdev); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1229 | read_bio->bi_end_io = raid10_end_read_request; |
| 1230 | bio_set_op_attrs(read_bio, op, do_sync); |
| 1231 | if (test_bit(FailFast, &rdev->flags) && |
| 1232 | test_bit(R10BIO_FailFast, &r10_bio->state)) |
| 1233 | read_bio->bi_opf |= MD_FAILFAST; |
| 1234 | read_bio->bi_private = r10_bio; |
| 1235 | |
| 1236 | if (mddev->gendisk) |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1237 | trace_block_bio_remap(read_bio->bi_disk->queue, |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1238 | read_bio, disk_devt(mddev->gendisk), |
| 1239 | r10_bio->sector); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1240 | generic_make_request(read_bio); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1241 | return; |
| 1242 | } |
| 1243 | |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1244 | static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio, |
| 1245 | struct bio *bio, bool replacement, |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1246 | int n_copy) |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1247 | { |
| 1248 | const int op = bio_op(bio); |
| 1249 | const unsigned long do_sync = (bio->bi_opf & REQ_SYNC); |
| 1250 | const unsigned long do_fua = (bio->bi_opf & REQ_FUA); |
| 1251 | unsigned long flags; |
| 1252 | struct blk_plug_cb *cb; |
| 1253 | struct raid10_plug_cb *plug = NULL; |
| 1254 | struct r10conf *conf = mddev->private; |
| 1255 | struct md_rdev *rdev; |
| 1256 | int devnum = r10_bio->devs[n_copy].devnum; |
| 1257 | struct bio *mbio; |
| 1258 | |
| 1259 | if (replacement) { |
| 1260 | rdev = conf->mirrors[devnum].replacement; |
| 1261 | if (rdev == NULL) { |
| 1262 | /* Replacement just got moved to main 'rdev' */ |
| 1263 | smp_mb(); |
| 1264 | rdev = conf->mirrors[devnum].rdev; |
| 1265 | } |
| 1266 | } else |
| 1267 | rdev = conf->mirrors[devnum].rdev; |
| 1268 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 1269 | mbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1270 | if (replacement) |
| 1271 | r10_bio->devs[n_copy].repl_bio = mbio; |
| 1272 | else |
| 1273 | r10_bio->devs[n_copy].bio = mbio; |
| 1274 | |
| 1275 | mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr + |
| 1276 | choose_data_offset(r10_bio, rdev)); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1277 | bio_set_dev(mbio, rdev->bdev); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1278 | mbio->bi_end_io = raid10_end_write_request; |
| 1279 | bio_set_op_attrs(mbio, op, do_sync | do_fua); |
| 1280 | if (!replacement && test_bit(FailFast, |
| 1281 | &conf->mirrors[devnum].rdev->flags) |
| 1282 | && enough(conf, devnum)) |
| 1283 | mbio->bi_opf |= MD_FAILFAST; |
| 1284 | mbio->bi_private = r10_bio; |
| 1285 | |
| 1286 | if (conf->mddev->gendisk) |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1287 | trace_block_bio_remap(mbio->bi_disk->queue, |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1288 | mbio, disk_devt(conf->mddev->gendisk), |
| 1289 | r10_bio->sector); |
| 1290 | /* flush_pending_writes() needs access to the rdev so...*/ |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1291 | mbio->bi_disk = (void *)rdev; |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1292 | |
| 1293 | atomic_inc(&r10_bio->remaining); |
| 1294 | |
| 1295 | cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug)); |
| 1296 | if (cb) |
| 1297 | plug = container_of(cb, struct raid10_plug_cb, cb); |
| 1298 | else |
| 1299 | plug = NULL; |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1300 | if (plug) { |
| 1301 | bio_list_add(&plug->pending, mbio); |
| 1302 | plug->pending_cnt++; |
| 1303 | } else { |
Shaohua Li | 23b245c | 2017-05-10 08:47:11 -0700 | [diff] [blame] | 1304 | spin_lock_irqsave(&conf->device_lock, flags); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1305 | bio_list_add(&conf->pending_bio_list, mbio); |
| 1306 | conf->pending_count++; |
Shaohua Li | 23b245c | 2017-05-10 08:47:11 -0700 | [diff] [blame] | 1307 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1308 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 23b245c | 2017-05-10 08:47:11 -0700 | [diff] [blame] | 1309 | } |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1310 | } |
| 1311 | |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1312 | static void raid10_write_request(struct mddev *mddev, struct bio *bio, |
| 1313 | struct r10bio *r10_bio) |
| 1314 | { |
| 1315 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1316 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1317 | struct md_rdev *blocked_rdev; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1318 | sector_t sectors; |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1319 | int max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1320 | |
Guoqing Jiang | cb8a7a7 | 2017-10-24 15:11:51 +0800 | [diff] [blame] | 1321 | if ((mddev_is_clustered(mddev) && |
| 1322 | md_cluster_ops->area_resyncing(mddev, WRITE, |
| 1323 | bio->bi_iter.bi_sector, |
| 1324 | bio_end_sector(bio)))) { |
| 1325 | DEFINE_WAIT(w); |
| 1326 | for (;;) { |
| 1327 | prepare_to_wait(&conf->wait_barrier, |
| 1328 | &w, TASK_IDLE); |
| 1329 | if (!md_cluster_ops->area_resyncing(mddev, WRITE, |
| 1330 | bio->bi_iter.bi_sector, bio_end_sector(bio))) |
| 1331 | break; |
| 1332 | schedule(); |
| 1333 | } |
| 1334 | finish_wait(&conf->wait_barrier, &w); |
| 1335 | } |
| 1336 | |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1337 | sectors = r10_bio->sectors; |
Guoqing Jiang | caea3c4 | 2018-12-07 18:24:21 +0800 | [diff] [blame] | 1338 | regular_request_wait(mddev, conf, bio, sectors); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1339 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1340 | (mddev->reshape_backwards |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1341 | ? (bio->bi_iter.bi_sector < conf->reshape_safe && |
| 1342 | bio->bi_iter.bi_sector + sectors > conf->reshape_progress) |
| 1343 | : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe && |
| 1344 | bio->bi_iter.bi_sector < conf->reshape_progress))) { |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1345 | /* Need to update reshape_position in metadata */ |
| 1346 | mddev->reshape_position = conf->reshape_progress; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1347 | set_mask_bits(&mddev->sb_flags, 0, |
| 1348 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1349 | md_wakeup_thread(mddev->thread); |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1350 | raid10_log(conf->mddev, "wait reshape metadata"); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1351 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1352 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1353 | |
| 1354 | conf->reshape_safe = mddev->reshape_position; |
| 1355 | } |
| 1356 | |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 1357 | if (conf->pending_count >= max_queued_requests) { |
| 1358 | md_wakeup_thread(mddev->thread); |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1359 | raid10_log(mddev, "wait queued"); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 1360 | wait_event(conf->wait_barrier, |
| 1361 | conf->pending_count < max_queued_requests); |
| 1362 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1363 | /* first select target devices under rcu_lock and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | * inc refcount on their rdev. Record them by setting |
| 1365 | * bios[x] to bio |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1366 | * If there are known/acknowledged bad blocks on any device |
| 1367 | * on which we have seen a write error, we want to avoid |
| 1368 | * writing to those blocks. This potentially requires several |
| 1369 | * writes to write around the bad blocks. Each set of writes |
NeilBrown | fd16f2e | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 1370 | * gets its own r10_bio with a set of bios attached. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | */ |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1372 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1373 | r10_bio->read_slot = -1; /* make sure repl_bio gets freed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1374 | raid10_find_phys(conf, r10_bio); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1375 | retry_write: |
Harvey Harrison | cb6969e | 2008-05-06 20:42:32 -0700 | [diff] [blame] | 1376 | blocked_rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1377 | rcu_read_lock(); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1378 | max_sectors = r10_bio->sectors; |
| 1379 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | for (i = 0; i < conf->copies; i++) { |
| 1381 | int d = r10_bio->devs[i].devnum; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1382 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1383 | struct md_rdev *rrdev = rcu_dereference( |
| 1384 | conf->mirrors[d].replacement); |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1385 | if (rdev == rrdev) |
| 1386 | rrdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1387 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
| 1388 | atomic_inc(&rdev->nr_pending); |
| 1389 | blocked_rdev = rdev; |
| 1390 | break; |
| 1391 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1392 | if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) { |
| 1393 | atomic_inc(&rrdev->nr_pending); |
| 1394 | blocked_rdev = rrdev; |
| 1395 | break; |
| 1396 | } |
Kent Overstreet | 8ae1266 | 2015-04-27 23:48:34 -0700 | [diff] [blame] | 1397 | if (rdev && (test_bit(Faulty, &rdev->flags))) |
NeilBrown | e7c0c3f | 2012-11-22 14:42:49 +1100 | [diff] [blame] | 1398 | rdev = NULL; |
Kent Overstreet | 8ae1266 | 2015-04-27 23:48:34 -0700 | [diff] [blame] | 1399 | if (rrdev && (test_bit(Faulty, &rrdev->flags))) |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1400 | rrdev = NULL; |
| 1401 | |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1402 | r10_bio->devs[i].bio = NULL; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1403 | r10_bio->devs[i].repl_bio = NULL; |
NeilBrown | e7c0c3f | 2012-11-22 14:42:49 +1100 | [diff] [blame] | 1404 | |
| 1405 | if (!rdev && !rrdev) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1406 | set_bit(R10BIO_Degraded, &r10_bio->state); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1407 | continue; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1408 | } |
NeilBrown | e7c0c3f | 2012-11-22 14:42:49 +1100 | [diff] [blame] | 1409 | if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) { |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1410 | sector_t first_bad; |
| 1411 | sector_t dev_sector = r10_bio->devs[i].addr; |
| 1412 | int bad_sectors; |
| 1413 | int is_bad; |
| 1414 | |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1415 | is_bad = is_badblock(rdev, dev_sector, max_sectors, |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1416 | &first_bad, &bad_sectors); |
| 1417 | if (is_bad < 0) { |
| 1418 | /* Mustn't write here until the bad block |
| 1419 | * is acknowledged |
| 1420 | */ |
| 1421 | atomic_inc(&rdev->nr_pending); |
| 1422 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 1423 | blocked_rdev = rdev; |
| 1424 | break; |
| 1425 | } |
| 1426 | if (is_bad && first_bad <= dev_sector) { |
| 1427 | /* Cannot write here at all */ |
| 1428 | bad_sectors -= (dev_sector - first_bad); |
| 1429 | if (bad_sectors < max_sectors) |
| 1430 | /* Mustn't write more than bad_sectors |
| 1431 | * to other devices yet |
| 1432 | */ |
| 1433 | max_sectors = bad_sectors; |
| 1434 | /* We don't set R10BIO_Degraded as that |
| 1435 | * only applies if the disk is missing, |
| 1436 | * so it might be re-added, and we want to |
| 1437 | * know to recover this chunk. |
| 1438 | * In this case the device is here, and the |
| 1439 | * fact that this chunk is not in-sync is |
| 1440 | * recorded in the bad block log. |
| 1441 | */ |
| 1442 | continue; |
| 1443 | } |
| 1444 | if (is_bad) { |
| 1445 | int good_sectors = first_bad - dev_sector; |
| 1446 | if (good_sectors < max_sectors) |
| 1447 | max_sectors = good_sectors; |
| 1448 | } |
| 1449 | } |
NeilBrown | e7c0c3f | 2012-11-22 14:42:49 +1100 | [diff] [blame] | 1450 | if (rdev) { |
| 1451 | r10_bio->devs[i].bio = bio; |
| 1452 | atomic_inc(&rdev->nr_pending); |
| 1453 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1454 | if (rrdev) { |
| 1455 | r10_bio->devs[i].repl_bio = bio; |
| 1456 | atomic_inc(&rrdev->nr_pending); |
| 1457 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1458 | } |
| 1459 | rcu_read_unlock(); |
| 1460 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1461 | if (unlikely(blocked_rdev)) { |
| 1462 | /* Have to wait for this device to get unblocked, then retry */ |
| 1463 | int j; |
| 1464 | int d; |
| 1465 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1466 | for (j = 0; j < i; j++) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1467 | if (r10_bio->devs[j].bio) { |
| 1468 | d = r10_bio->devs[j].devnum; |
| 1469 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); |
| 1470 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1471 | if (r10_bio->devs[j].repl_bio) { |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1472 | struct md_rdev *rdev; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1473 | d = r10_bio->devs[j].devnum; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1474 | rdev = conf->mirrors[d].replacement; |
| 1475 | if (!rdev) { |
| 1476 | /* Race with remove_disk */ |
| 1477 | smp_mb(); |
| 1478 | rdev = conf->mirrors[d].rdev; |
| 1479 | } |
| 1480 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1481 | } |
| 1482 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1483 | allow_barrier(conf); |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1484 | raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1485 | md_wait_for_blocked_rdev(blocked_rdev, mddev); |
| 1486 | wait_barrier(conf); |
| 1487 | goto retry_write; |
| 1488 | } |
| 1489 | |
NeilBrown | 6b6c811 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 1490 | if (max_sectors < r10_bio->sectors) |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1491 | r10_bio->sectors = max_sectors; |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1492 | |
| 1493 | if (r10_bio->sectors < bio_sectors(bio)) { |
| 1494 | struct bio *split = bio_split(bio, r10_bio->sectors, |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 1495 | GFP_NOIO, &conf->bio_split); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1496 | bio_chain(split, bio); |
Guoqing Jiang | e820d55 | 2018-12-19 14:19:25 +0800 | [diff] [blame^] | 1497 | allow_barrier(conf); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1498 | generic_make_request(bio); |
Guoqing Jiang | e820d55 | 2018-12-19 14:19:25 +0800 | [diff] [blame^] | 1499 | wait_barrier(conf); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1500 | bio = split; |
| 1501 | r10_bio->master_bio = bio; |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1502 | } |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1503 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1504 | atomic_set(&r10_bio->remaining, 1); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 1505 | md_bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1506 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1507 | for (i = 0; i < conf->copies; i++) { |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1508 | if (r10_bio->devs[i].bio) |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1509 | raid10_write_one_disk(mddev, r10_bio, bio, false, i); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1510 | if (r10_bio->devs[i].repl_bio) |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1511 | raid10_write_one_disk(mddev, r10_bio, bio, true, i); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1512 | } |
NeilBrown | 079fa16 | 2011-09-10 17:21:23 +1000 | [diff] [blame] | 1513 | one_write_done(r10_bio); |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1514 | } |
| 1515 | |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1516 | static void __make_request(struct mddev *mddev, struct bio *bio, int sectors) |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1517 | { |
| 1518 | struct r10conf *conf = mddev->private; |
| 1519 | struct r10bio *r10_bio; |
| 1520 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 1521 | r10_bio = mempool_alloc(&conf->r10bio_pool, GFP_NOIO); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1522 | |
| 1523 | r10_bio->master_bio = bio; |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1524 | r10_bio->sectors = sectors; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1525 | |
| 1526 | r10_bio->mddev = mddev; |
| 1527 | r10_bio->sector = bio->bi_iter.bi_sector; |
| 1528 | r10_bio->state = 0; |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1529 | memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1530 | |
| 1531 | if (bio_data_dir(bio) == READ) |
| 1532 | raid10_read_request(mddev, bio, r10_bio); |
| 1533 | else |
| 1534 | raid10_write_request(mddev, bio, r10_bio); |
| 1535 | } |
| 1536 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 1537 | static bool raid10_make_request(struct mddev *mddev, struct bio *bio) |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1538 | { |
| 1539 | struct r10conf *conf = mddev->private; |
| 1540 | sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask); |
| 1541 | int chunk_sects = chunk_mask + 1; |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1542 | int sectors = bio_sectors(bio); |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1543 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1544 | if (unlikely(bio->bi_opf & REQ_PREFLUSH)) { |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1545 | md_flush_request(mddev, bio); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 1546 | return true; |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1547 | } |
| 1548 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 1549 | if (!md_write_start(mddev, bio)) |
| 1550 | return false; |
| 1551 | |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1552 | /* |
| 1553 | * If this request crosses a chunk boundary, we need to split |
| 1554 | * it. |
| 1555 | */ |
| 1556 | if (unlikely((bio->bi_iter.bi_sector & chunk_mask) + |
| 1557 | sectors > chunk_sects |
| 1558 | && (conf->geo.near_copies < conf->geo.raid_disks |
| 1559 | || conf->prev.near_copies < |
| 1560 | conf->prev.raid_disks))) |
| 1561 | sectors = chunk_sects - |
| 1562 | (bio->bi_iter.bi_sector & |
| 1563 | (chunk_sects - 1)); |
| 1564 | __make_request(mddev, bio, sectors); |
NeilBrown | 079fa16 | 2011-09-10 17:21:23 +1000 | [diff] [blame] | 1565 | |
| 1566 | /* In case raid10d snuck in to freeze_array */ |
| 1567 | wake_up(&conf->wait_barrier); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 1568 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1569 | } |
| 1570 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 1571 | static void raid10_status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1572 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1573 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1574 | int i; |
| 1575 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1576 | if (conf->geo.near_copies < conf->geo.raid_disks) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1577 | seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1578 | if (conf->geo.near_copies > 1) |
| 1579 | seq_printf(seq, " %d near-copies", conf->geo.near_copies); |
| 1580 | if (conf->geo.far_copies > 1) { |
| 1581 | if (conf->geo.far_offset) |
| 1582 | seq_printf(seq, " %d offset-copies", conf->geo.far_copies); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1583 | else |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1584 | seq_printf(seq, " %d far-copies", conf->geo.far_copies); |
NeilBrown | 8bce6d3 | 2015-10-22 13:20:15 +1100 | [diff] [blame] | 1585 | if (conf->geo.far_set_size != conf->geo.raid_disks) |
| 1586 | seq_printf(seq, " %d devices per set", conf->geo.far_set_size); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1587 | } |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1588 | seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks, |
| 1589 | conf->geo.raid_disks - mddev->degraded); |
NeilBrown | d44b0a9 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 1590 | rcu_read_lock(); |
| 1591 | for (i = 0; i < conf->geo.raid_disks; i++) { |
| 1592 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 1593 | seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 1594 | } |
| 1595 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1596 | seq_printf(seq, "]"); |
| 1597 | } |
| 1598 | |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1599 | /* check if there are enough drives for |
| 1600 | * every block to appear on atleast one. |
| 1601 | * Don't consider the device numbered 'ignore' |
| 1602 | * as we might be about to remove it. |
| 1603 | */ |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1604 | static int _enough(struct r10conf *conf, int previous, int ignore) |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1605 | { |
| 1606 | int first = 0; |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1607 | int has_enough = 0; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1608 | int disks, ncopies; |
| 1609 | if (previous) { |
| 1610 | disks = conf->prev.raid_disks; |
| 1611 | ncopies = conf->prev.near_copies; |
| 1612 | } else { |
| 1613 | disks = conf->geo.raid_disks; |
| 1614 | ncopies = conf->geo.near_copies; |
| 1615 | } |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1616 | |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1617 | rcu_read_lock(); |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1618 | do { |
| 1619 | int n = conf->copies; |
| 1620 | int cnt = 0; |
NeilBrown | 80b4812 | 2012-09-27 12:35:21 +1000 | [diff] [blame] | 1621 | int this = first; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1622 | while (n--) { |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1623 | struct md_rdev *rdev; |
| 1624 | if (this != ignore && |
| 1625 | (rdev = rcu_dereference(conf->mirrors[this].rdev)) && |
| 1626 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1627 | cnt++; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1628 | this = (this+1) % disks; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1629 | } |
| 1630 | if (cnt == 0) |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1631 | goto out; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1632 | first = (first + ncopies) % disks; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1633 | } while (first != 0); |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1634 | has_enough = 1; |
| 1635 | out: |
| 1636 | rcu_read_unlock(); |
| 1637 | return has_enough; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1638 | } |
| 1639 | |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 1640 | static int enough(struct r10conf *conf, int ignore) |
| 1641 | { |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1642 | /* when calling 'enough', both 'prev' and 'geo' must |
| 1643 | * be stable. |
| 1644 | * This is ensured if ->reconfig_mutex or ->device_lock |
| 1645 | * is held. |
| 1646 | */ |
| 1647 | return _enough(conf, 0, ignore) && |
| 1648 | _enough(conf, 1, ignore); |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 1649 | } |
| 1650 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 1651 | static void raid10_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1652 | { |
| 1653 | char b[BDEVNAME_SIZE]; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1654 | struct r10conf *conf = mddev->private; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1655 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1656 | |
| 1657 | /* |
| 1658 | * If it is not operational, then we have already marked it as dead |
| 1659 | * else if it is the last working disks, ignore the error, let the |
| 1660 | * next level up know. |
| 1661 | * else mark the drive as failed |
| 1662 | */ |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1663 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1664 | if (test_bit(In_sync, &rdev->flags) |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1665 | && !enough(conf, rdev->raid_disk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | /* |
| 1667 | * Don't fail the drive, just return an IO error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1668 | */ |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1669 | spin_unlock_irqrestore(&conf->device_lock, flags); |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1670 | return; |
| 1671 | } |
NeilBrown | 2446dba | 2014-07-31 10:16:29 +1000 | [diff] [blame] | 1672 | if (test_and_clear_bit(In_sync, &rdev->flags)) |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1673 | mddev->degraded++; |
NeilBrown | 2446dba | 2014-07-31 10:16:29 +1000 | [diff] [blame] | 1674 | /* |
| 1675 | * If recovery is running, make sure it aborts. |
| 1676 | */ |
| 1677 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1678 | set_bit(Blocked, &rdev->flags); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1679 | set_bit(Faulty, &rdev->flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1680 | set_mask_bits(&mddev->sb_flags, 0, |
| 1681 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1682 | spin_unlock_irqrestore(&conf->device_lock, flags); |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1683 | pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n" |
| 1684 | "md/raid10:%s: Operation continuing on %d devices.\n", |
| 1685 | mdname(mddev), bdevname(rdev->bdev, b), |
| 1686 | mdname(mddev), conf->geo.raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1687 | } |
| 1688 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1689 | static void print_conf(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | { |
| 1691 | int i; |
NeilBrown | 4056ca5 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 1692 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1694 | pr_debug("RAID10 conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1695 | if (!conf) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1696 | pr_debug("(!conf)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | return; |
| 1698 | } |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1699 | pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded, |
| 1700 | conf->geo.raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | |
NeilBrown | 4056ca5 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 1702 | /* This is only called with ->reconfix_mutex held, so |
| 1703 | * rcu protection of rdev is not needed */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1704 | for (i = 0; i < conf->geo.raid_disks; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1705 | char b[BDEVNAME_SIZE]; |
NeilBrown | 4056ca5 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 1706 | rdev = conf->mirrors[i].rdev; |
| 1707 | if (rdev) |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1708 | pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n", |
| 1709 | i, !test_bit(In_sync, &rdev->flags), |
| 1710 | !test_bit(Faulty, &rdev->flags), |
| 1711 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1715 | static void close_sync(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | { |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1717 | wait_barrier(conf); |
| 1718 | allow_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 1720 | mempool_exit(&conf->r10buf_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1721 | } |
| 1722 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1723 | static int raid10_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | { |
| 1725 | int i; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1726 | struct r10conf *conf = mddev->private; |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 1727 | struct raid10_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1728 | int count = 0; |
| 1729 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1730 | |
| 1731 | /* |
| 1732 | * Find all non-in_sync disks within the RAID10 configuration |
| 1733 | * and mark them in_sync |
| 1734 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1735 | for (i = 0; i < conf->geo.raid_disks; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1736 | tmp = conf->mirrors + i; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1737 | if (tmp->replacement |
| 1738 | && tmp->replacement->recovery_offset == MaxSector |
| 1739 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 1740 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 1741 | /* Replacement has just become active */ |
| 1742 | if (!tmp->rdev |
| 1743 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 1744 | count++; |
| 1745 | if (tmp->rdev) { |
| 1746 | /* Replaced device not technically faulty, |
| 1747 | * but we need to be sure it gets removed |
| 1748 | * and never re-added. |
| 1749 | */ |
| 1750 | set_bit(Faulty, &tmp->rdev->flags); |
| 1751 | sysfs_notify_dirent_safe( |
| 1752 | tmp->rdev->sysfs_state); |
| 1753 | } |
| 1754 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 1755 | } else if (tmp->rdev |
Lukasz Dorau | 61e4947 | 2013-10-24 12:55:17 +1100 | [diff] [blame] | 1756 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1757 | && !test_bit(Faulty, &tmp->rdev->flags) |
| 1758 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1759 | count++; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 1760 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1761 | } |
| 1762 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1763 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1764 | mddev->degraded -= count; |
| 1765 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1766 | |
| 1767 | print_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1768 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1769 | } |
| 1770 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1771 | static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1773 | struct r10conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1774 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | int mirror; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1776 | int first = 0; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1777 | int last = conf->geo.raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1778 | |
| 1779 | if (mddev->recovery_cp < MaxSector) |
| 1780 | /* only hot-add to in-sync arrays, as recovery is |
| 1781 | * very different from resync |
| 1782 | */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1783 | return -EBUSY; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1784 | if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1)) |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1785 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 1787 | if (md_integrity_add_rdev(rdev, mddev)) |
| 1788 | return -ENXIO; |
| 1789 | |
NeilBrown | a53a6c8 | 2008-11-06 17:28:20 +1100 | [diff] [blame] | 1790 | if (rdev->raid_disk >= 0) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1791 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1792 | |
Namhyung Kim | 2c4193d | 2011-07-18 17:38:43 +1000 | [diff] [blame] | 1793 | if (rdev->saved_raid_disk >= first && |
Shaohua Li | 9e753ba | 2018-10-14 17:05:07 -0700 | [diff] [blame] | 1794 | rdev->saved_raid_disk < conf->geo.raid_disks && |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1795 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) |
| 1796 | mirror = rdev->saved_raid_disk; |
| 1797 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1798 | mirror = first; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1799 | for ( ; mirror <= last ; mirror++) { |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 1800 | struct raid10_info *p = &conf->mirrors[mirror]; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1801 | if (p->recovery_disabled == mddev->recovery_disabled) |
| 1802 | continue; |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1803 | if (p->rdev) { |
| 1804 | if (!test_bit(WantReplacement, &p->rdev->flags) || |
| 1805 | p->replacement != NULL) |
| 1806 | continue; |
| 1807 | clear_bit(In_sync, &rdev->flags); |
| 1808 | set_bit(Replacement, &rdev->flags); |
| 1809 | rdev->raid_disk = mirror; |
| 1810 | err = 0; |
Jonathan Brassow | 9092c02 | 2013-05-02 14:19:24 -0500 | [diff] [blame] | 1811 | if (mddev->gendisk) |
| 1812 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 1813 | rdev->data_offset << 9); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1814 | conf->fullsync = 1; |
| 1815 | rcu_assign_pointer(p->replacement, rdev); |
| 1816 | break; |
| 1817 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1818 | |
Jonathan Brassow | 9092c02 | 2013-05-02 14:19:24 -0500 | [diff] [blame] | 1819 | if (mddev->gendisk) |
| 1820 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 1821 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1822 | |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1823 | p->head_position = 0; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 1824 | p->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1825 | rdev->raid_disk = mirror; |
| 1826 | err = 0; |
| 1827 | if (rdev->saved_raid_disk != mirror) |
| 1828 | conf->fullsync = 1; |
| 1829 | rcu_assign_pointer(p->rdev, rdev); |
| 1830 | break; |
| 1831 | } |
Jonathan Brassow | ed30be0 | 2012-10-31 11:42:30 +1100 | [diff] [blame] | 1832 | if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev))) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 1833 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, mddev->queue); |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 1834 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | print_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1836 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1837 | } |
| 1838 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1839 | static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1840 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1841 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1843 | int number = rdev->raid_disk; |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1844 | struct md_rdev **rdevp; |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 1845 | struct raid10_info *p = conf->mirrors + number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1846 | |
| 1847 | print_conf(conf); |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1848 | if (rdev == p->rdev) |
| 1849 | rdevp = &p->rdev; |
| 1850 | else if (rdev == p->replacement) |
| 1851 | rdevp = &p->replacement; |
| 1852 | else |
| 1853 | return 0; |
| 1854 | |
| 1855 | if (test_bit(In_sync, &rdev->flags) || |
| 1856 | atomic_read(&rdev->nr_pending)) { |
| 1857 | err = -EBUSY; |
| 1858 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1859 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 1860 | /* Only remove non-faulty devices if recovery |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1861 | * is not possible. |
| 1862 | */ |
| 1863 | if (!test_bit(Faulty, &rdev->flags) && |
| 1864 | mddev->recovery_disabled != p->recovery_disabled && |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1865 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 1866 | number < conf->geo.raid_disks && |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1867 | enough(conf, -1)) { |
| 1868 | err = -EBUSY; |
| 1869 | goto abort; |
| 1870 | } |
| 1871 | *rdevp = NULL; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 1872 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
| 1873 | synchronize_rcu(); |
| 1874 | if (atomic_read(&rdev->nr_pending)) { |
| 1875 | /* lost the race, try later */ |
| 1876 | err = -EBUSY; |
| 1877 | *rdevp = rdev; |
| 1878 | goto abort; |
| 1879 | } |
| 1880 | } |
| 1881 | if (p->replacement) { |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1882 | /* We must have just cleared 'rdev' */ |
| 1883 | p->rdev = p->replacement; |
| 1884 | clear_bit(Replacement, &p->replacement->flags); |
| 1885 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 1886 | * but will never see neither -- if they are careful. |
| 1887 | */ |
| 1888 | p->replacement = NULL; |
Guoqing Jiang | e5bc9c3 | 2017-04-24 15:58:04 +0800 | [diff] [blame] | 1889 | } |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1890 | |
Guoqing Jiang | e5bc9c3 | 2017-04-24 15:58:04 +0800 | [diff] [blame] | 1891 | clear_bit(WantReplacement, &rdev->flags); |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1892 | err = md_integrity_register(mddev); |
| 1893 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | abort: |
| 1895 | |
| 1896 | print_conf(conf); |
| 1897 | return err; |
| 1898 | } |
| 1899 | |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 1900 | static void __end_sync_read(struct r10bio *r10_bio, struct bio *bio, int d) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1902 | struct r10conf *conf = r10_bio->mddev->private; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1903 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1904 | if (!bio->bi_status) |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1905 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
NeilBrown | e684e41 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1906 | else |
| 1907 | /* The write handler will notice the lack of |
| 1908 | * R10BIO_Uptodate and record any errors etc |
| 1909 | */ |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1910 | atomic_add(r10_bio->sectors, |
| 1911 | &conf->mirrors[d].rdev->corrected_errors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | |
| 1913 | /* for reconstruct, we always reschedule after a read. |
| 1914 | * for resync, only after all reads |
| 1915 | */ |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1916 | rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1917 | if (test_bit(R10BIO_IsRecover, &r10_bio->state) || |
| 1918 | atomic_dec_and_test(&r10_bio->remaining)) { |
| 1919 | /* we have read all the blocks, |
| 1920 | * do the comparison in process context in raid10d |
| 1921 | */ |
| 1922 | reschedule_retry(r10_bio); |
| 1923 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 1926 | static void end_sync_read(struct bio *bio) |
| 1927 | { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 1928 | struct r10bio *r10_bio = get_resync_r10bio(bio); |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 1929 | struct r10conf *conf = r10_bio->mddev->private; |
| 1930 | int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL); |
| 1931 | |
| 1932 | __end_sync_read(r10_bio, bio, d); |
| 1933 | } |
| 1934 | |
| 1935 | static void end_reshape_read(struct bio *bio) |
| 1936 | { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 1937 | /* reshape read bio isn't allocated from r10buf_pool */ |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 1938 | struct r10bio *r10_bio = bio->bi_private; |
| 1939 | |
| 1940 | __end_sync_read(r10_bio, bio, r10_bio->read_slot); |
| 1941 | } |
| 1942 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1943 | static void end_sync_request(struct r10bio *r10_bio) |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1944 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1945 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1946 | |
| 1947 | while (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1948 | if (r10_bio->master_bio == NULL) { |
| 1949 | /* the primary of several recovery bios */ |
| 1950 | sector_t s = r10_bio->sectors; |
| 1951 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 1952 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 1953 | reschedule_retry(r10_bio); |
| 1954 | else |
| 1955 | put_buf(r10_bio); |
| 1956 | md_done_sync(mddev, s, 1); |
| 1957 | break; |
| 1958 | } else { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1959 | struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1960 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 1961 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 1962 | reschedule_retry(r10_bio); |
| 1963 | else |
| 1964 | put_buf(r10_bio); |
| 1965 | r10_bio = r10_bio2; |
| 1966 | } |
| 1967 | } |
| 1968 | } |
| 1969 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1970 | static void end_sync_write(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1971 | { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 1972 | struct r10bio *r10_bio = get_resync_r10bio(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1973 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1974 | struct r10conf *conf = mddev->private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 1975 | int d; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1976 | sector_t first_bad; |
| 1977 | int bad_sectors; |
| 1978 | int slot; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1979 | int repl; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1980 | struct md_rdev *rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1982 | d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
| 1983 | if (repl) |
| 1984 | rdev = conf->mirrors[d].replacement; |
NeilBrown | 547414d | 2012-03-13 11:21:20 +1100 | [diff] [blame] | 1985 | else |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1986 | rdev = conf->mirrors[d].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1988 | if (bio->bi_status) { |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1989 | if (repl) |
| 1990 | md_error(mddev, rdev); |
| 1991 | else { |
| 1992 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1993 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1994 | set_bit(MD_RECOVERY_NEEDED, |
| 1995 | &rdev->mddev->recovery); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1996 | set_bit(R10BIO_WriteError, &r10_bio->state); |
| 1997 | } |
| 1998 | } else if (is_badblock(rdev, |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1999 | r10_bio->devs[slot].addr, |
| 2000 | r10_bio->sectors, |
| 2001 | &first_bad, &bad_sectors)) |
| 2002 | set_bit(R10BIO_MadeGood, &r10_bio->state); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 2003 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2004 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2005 | |
| 2006 | end_sync_request(r10_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | } |
| 2008 | |
| 2009 | /* |
| 2010 | * Note: sync and recover and handled very differently for raid10 |
| 2011 | * This code is for resync. |
| 2012 | * For resync, we read through virtual addresses and read all blocks. |
| 2013 | * If there is any error, we schedule a write. The lowest numbered |
| 2014 | * drive is authoritative. |
| 2015 | * However requests come for physical address, so we need to map. |
| 2016 | * For every physical address there are raid_disks/copies virtual addresses, |
| 2017 | * which is always are least one, but is not necessarly an integer. |
| 2018 | * This means that a physical address can span multiple chunks, so we may |
| 2019 | * have to submit multiple io requests for a single sync request. |
| 2020 | */ |
| 2021 | /* |
| 2022 | * We check if all blocks are in-sync and only write to blocks that |
| 2023 | * aren't in sync |
| 2024 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2025 | static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2027 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2028 | int i, first; |
| 2029 | struct bio *tbio, *fbio; |
majianpeng | f4380a9 | 2012-04-12 16:04:47 +1000 | [diff] [blame] | 2030 | int vcnt; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2031 | struct page **tpages, **fpages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
| 2033 | atomic_set(&r10_bio->remaining, 1); |
| 2034 | |
| 2035 | /* find the first device with a block */ |
| 2036 | for (i=0; i<conf->copies; i++) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2037 | if (!r10_bio->devs[i].bio->bi_status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | break; |
| 2039 | |
| 2040 | if (i == conf->copies) |
| 2041 | goto done; |
| 2042 | |
| 2043 | first = i; |
| 2044 | fbio = r10_bio->devs[i].bio; |
Artur Paszkiewicz | cc57858 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2045 | fbio->bi_iter.bi_size = r10_bio->sectors << 9; |
| 2046 | fbio->bi_iter.bi_idx = 0; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2047 | fpages = get_resync_pages(fbio)->pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2048 | |
majianpeng | f4380a9 | 2012-04-12 16:04:47 +1000 | [diff] [blame] | 2049 | vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2050 | /* now find blocks with errors */ |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2051 | for (i=0 ; i < conf->copies ; i++) { |
| 2052 | int j, d; |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2053 | struct md_rdev *rdev; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 2054 | struct resync_pages *rp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2056 | tbio = r10_bio->devs[i].bio; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2057 | |
| 2058 | if (tbio->bi_end_io != end_sync_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2059 | continue; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2060 | if (i == first) |
| 2061 | continue; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2062 | |
| 2063 | tpages = get_resync_pages(tbio)->pages; |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2064 | d = r10_bio->devs[i].devnum; |
| 2065 | rdev = conf->mirrors[d].rdev; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2066 | if (!r10_bio->devs[i].bio->bi_status) { |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2067 | /* We know that the bi_io_vec layout is the same for |
| 2068 | * both 'first' and 'i', so we just compare them. |
| 2069 | * All vec entries are PAGE_SIZE; |
| 2070 | */ |
NeilBrown | 7bb23c4 | 2013-07-16 16:50:47 +1000 | [diff] [blame] | 2071 | int sectors = r10_bio->sectors; |
| 2072 | for (j = 0; j < vcnt; j++) { |
| 2073 | int len = PAGE_SIZE; |
| 2074 | if (sectors < (len / 512)) |
| 2075 | len = sectors * 512; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2076 | if (memcmp(page_address(fpages[j]), |
| 2077 | page_address(tpages[j]), |
NeilBrown | 7bb23c4 | 2013-07-16 16:50:47 +1000 | [diff] [blame] | 2078 | len)) |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2079 | break; |
NeilBrown | 7bb23c4 | 2013-07-16 16:50:47 +1000 | [diff] [blame] | 2080 | sectors -= len/512; |
| 2081 | } |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2082 | if (j == vcnt) |
| 2083 | continue; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 2084 | atomic64_add(r10_bio->sectors, &mddev->resync_mismatches); |
NeilBrown | f84ee36 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2085 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2086 | /* Don't fix anything. */ |
| 2087 | continue; |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2088 | } else if (test_bit(FailFast, &rdev->flags)) { |
| 2089 | /* Just give up on this device */ |
| 2090 | md_error(rdev->mddev, rdev); |
| 2091 | continue; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2092 | } |
NeilBrown | f84ee36 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2093 | /* Ok, we need to write this bio, either to correct an |
| 2094 | * inconsistency or to correct an unreadable block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2095 | * First we need to fixup bv_offset, bv_len and |
| 2096 | * bi_vecs, as the read request might have corrupted these |
| 2097 | */ |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 2098 | rp = get_resync_pages(tbio); |
Kent Overstreet | 8be185f | 2012-09-06 14:14:43 -0700 | [diff] [blame] | 2099 | bio_reset(tbio); |
| 2100 | |
Ming Lei | fb0eb5d | 2017-07-14 16:14:43 +0800 | [diff] [blame] | 2101 | md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size); |
| 2102 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 2103 | rp->raid_bio = r10_bio; |
| 2104 | tbio->bi_private = rp; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2105 | tbio->bi_iter.bi_sector = r10_bio->devs[i].addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2106 | tbio->bi_end_io = end_sync_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2107 | bio_set_op_attrs(tbio, REQ_OP_WRITE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | |
Kent Overstreet | c31df25 | 2015-05-06 23:34:20 -0700 | [diff] [blame] | 2109 | bio_copy_data(tbio, fbio); |
| 2110 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 2112 | atomic_inc(&r10_bio->remaining); |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 2113 | md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2115 | if (test_bit(FailFast, &conf->mirrors[d].rdev->flags)) |
| 2116 | tbio->bi_opf |= MD_FAILFAST; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2117 | tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2118 | bio_set_dev(tbio, conf->mirrors[d].rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | generic_make_request(tbio); |
| 2120 | } |
| 2121 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2122 | /* Now write out to any replacement devices |
| 2123 | * that are active |
| 2124 | */ |
| 2125 | for (i = 0; i < conf->copies; i++) { |
Kent Overstreet | c31df25 | 2015-05-06 23:34:20 -0700 | [diff] [blame] | 2126 | int d; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2127 | |
| 2128 | tbio = r10_bio->devs[i].repl_bio; |
| 2129 | if (!tbio || !tbio->bi_end_io) |
| 2130 | continue; |
| 2131 | if (r10_bio->devs[i].bio->bi_end_io != end_sync_write |
| 2132 | && r10_bio->devs[i].bio != fbio) |
Kent Overstreet | c31df25 | 2015-05-06 23:34:20 -0700 | [diff] [blame] | 2133 | bio_copy_data(tbio, fbio); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2134 | d = r10_bio->devs[i].devnum; |
| 2135 | atomic_inc(&r10_bio->remaining); |
| 2136 | md_sync_acct(conf->mirrors[d].replacement->bdev, |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 2137 | bio_sectors(tbio)); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2138 | generic_make_request(tbio); |
| 2139 | } |
| 2140 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2141 | done: |
| 2142 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 2143 | md_done_sync(mddev, r10_bio->sectors, 1); |
| 2144 | put_buf(r10_bio); |
| 2145 | } |
| 2146 | } |
| 2147 | |
| 2148 | /* |
| 2149 | * Now for the recovery code. |
| 2150 | * Recovery happens across physical sectors. |
| 2151 | * We recover all non-is_sync drives by finding the virtual address of |
| 2152 | * each, and then choose a working drive that also has that virt address. |
| 2153 | * There is a separate r10_bio for each non-in_sync drive. |
| 2154 | * Only the first two slots are in use. The first for reading, |
| 2155 | * The second for writing. |
| 2156 | * |
| 2157 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2158 | static void fix_recovery_read_error(struct r10bio *r10_bio) |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2159 | { |
| 2160 | /* We got a read error during recovery. |
| 2161 | * We repeat the read in smaller page-sized sections. |
| 2162 | * If a read succeeds, write it to the new device or record |
| 2163 | * a bad block if we cannot. |
| 2164 | * If a read fails, record a bad block on both old and |
| 2165 | * new devices. |
| 2166 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2167 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2168 | struct r10conf *conf = mddev->private; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2169 | struct bio *bio = r10_bio->devs[0].bio; |
| 2170 | sector_t sect = 0; |
| 2171 | int sectors = r10_bio->sectors; |
| 2172 | int idx = 0; |
| 2173 | int dr = r10_bio->devs[0].devnum; |
| 2174 | int dw = r10_bio->devs[1].devnum; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2175 | struct page **pages = get_resync_pages(bio)->pages; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2176 | |
| 2177 | while (sectors) { |
| 2178 | int s = sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2179 | struct md_rdev *rdev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2180 | sector_t addr; |
| 2181 | int ok; |
| 2182 | |
| 2183 | if (s > (PAGE_SIZE>>9)) |
| 2184 | s = PAGE_SIZE >> 9; |
| 2185 | |
| 2186 | rdev = conf->mirrors[dr].rdev; |
| 2187 | addr = r10_bio->devs[0].addr + sect, |
| 2188 | ok = sync_page_io(rdev, |
| 2189 | addr, |
| 2190 | s << 9, |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2191 | pages[idx], |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2192 | REQ_OP_READ, 0, false); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2193 | if (ok) { |
| 2194 | rdev = conf->mirrors[dw].rdev; |
| 2195 | addr = r10_bio->devs[1].addr + sect; |
| 2196 | ok = sync_page_io(rdev, |
| 2197 | addr, |
| 2198 | s << 9, |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2199 | pages[idx], |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2200 | REQ_OP_WRITE, 0, false); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2201 | if (!ok) { |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2202 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2203 | if (!test_and_set_bit(WantReplacement, |
| 2204 | &rdev->flags)) |
| 2205 | set_bit(MD_RECOVERY_NEEDED, |
| 2206 | &rdev->mddev->recovery); |
| 2207 | } |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2208 | } |
| 2209 | if (!ok) { |
| 2210 | /* We don't worry if we cannot set a bad block - |
| 2211 | * it really is bad so there is no loss in not |
| 2212 | * recording it yet |
| 2213 | */ |
| 2214 | rdev_set_badblocks(rdev, addr, s, 0); |
| 2215 | |
| 2216 | if (rdev != conf->mirrors[dw].rdev) { |
| 2217 | /* need bad block on destination too */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2218 | struct md_rdev *rdev2 = conf->mirrors[dw].rdev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2219 | addr = r10_bio->devs[1].addr + sect; |
| 2220 | ok = rdev_set_badblocks(rdev2, addr, s, 0); |
| 2221 | if (!ok) { |
| 2222 | /* just abort the recovery */ |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2223 | pr_notice("md/raid10:%s: recovery aborted due to read error\n", |
| 2224 | mdname(mddev)); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2225 | |
| 2226 | conf->mirrors[dw].recovery_disabled |
| 2227 | = mddev->recovery_disabled; |
| 2228 | set_bit(MD_RECOVERY_INTR, |
| 2229 | &mddev->recovery); |
| 2230 | break; |
| 2231 | } |
| 2232 | } |
| 2233 | } |
| 2234 | |
| 2235 | sectors -= s; |
| 2236 | sect += s; |
| 2237 | idx++; |
| 2238 | } |
| 2239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2240 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2241 | static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2242 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2243 | struct r10conf *conf = mddev->private; |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 2244 | int d; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2245 | struct bio *wbio, *wbio2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2246 | |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2247 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) { |
| 2248 | fix_recovery_read_error(r10_bio); |
| 2249 | end_sync_request(r10_bio); |
| 2250 | return; |
| 2251 | } |
| 2252 | |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 2253 | /* |
| 2254 | * share the pages with the first bio |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | * and submit the write request |
| 2256 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2257 | d = r10_bio->devs[1].devnum; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2258 | wbio = r10_bio->devs[1].bio; |
| 2259 | wbio2 = r10_bio->devs[1].repl_bio; |
NeilBrown | 0eb25bb | 2013-07-24 15:37:42 +1000 | [diff] [blame] | 2260 | /* Need to test wbio2->bi_end_io before we call |
| 2261 | * generic_make_request as if the former is NULL, |
| 2262 | * the latter is free to free wbio2. |
| 2263 | */ |
| 2264 | if (wbio2 && !wbio2->bi_end_io) |
| 2265 | wbio2 = NULL; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2266 | if (wbio->bi_end_io) { |
| 2267 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 2268 | md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio)); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2269 | generic_make_request(wbio); |
| 2270 | } |
NeilBrown | 0eb25bb | 2013-07-24 15:37:42 +1000 | [diff] [blame] | 2271 | if (wbio2) { |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2272 | atomic_inc(&conf->mirrors[d].replacement->nr_pending); |
| 2273 | md_sync_acct(conf->mirrors[d].replacement->bdev, |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 2274 | bio_sectors(wbio2)); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2275 | generic_make_request(wbio2); |
| 2276 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | } |
| 2278 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2280 | * Used by fix_read_error() to decay the per rdev read_errors. |
| 2281 | * We halve the read error count for every hour that has elapsed |
| 2282 | * since the last recorded read error. |
| 2283 | * |
| 2284 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2285 | static void check_decay_read_errors(struct mddev *mddev, struct md_rdev *rdev) |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2286 | { |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 2287 | long cur_time_mon; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2288 | unsigned long hours_since_last; |
| 2289 | unsigned int read_errors = atomic_read(&rdev->read_errors); |
| 2290 | |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 2291 | cur_time_mon = ktime_get_seconds(); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2292 | |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 2293 | if (rdev->last_read_error == 0) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2294 | /* first time we've seen a read error */ |
| 2295 | rdev->last_read_error = cur_time_mon; |
| 2296 | return; |
| 2297 | } |
| 2298 | |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 2299 | hours_since_last = (long)(cur_time_mon - |
| 2300 | rdev->last_read_error) / 3600; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2301 | |
| 2302 | rdev->last_read_error = cur_time_mon; |
| 2303 | |
| 2304 | /* |
| 2305 | * if hours_since_last is > the number of bits in read_errors |
| 2306 | * just set read errors to 0. We do this to avoid |
| 2307 | * overflowing the shift of read_errors by hours_since_last. |
| 2308 | */ |
| 2309 | if (hours_since_last >= 8 * sizeof(read_errors)) |
| 2310 | atomic_set(&rdev->read_errors, 0); |
| 2311 | else |
| 2312 | atomic_set(&rdev->read_errors, read_errors >> hours_since_last); |
| 2313 | } |
| 2314 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2315 | static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2316 | int sectors, struct page *page, int rw) |
| 2317 | { |
| 2318 | sector_t first_bad; |
| 2319 | int bad_sectors; |
| 2320 | |
| 2321 | if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors) |
| 2322 | && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags))) |
| 2323 | return -1; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2324 | if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false)) |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2325 | /* success */ |
| 2326 | return 1; |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2327 | if (rw == WRITE) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2328 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2329 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2330 | set_bit(MD_RECOVERY_NEEDED, |
| 2331 | &rdev->mddev->recovery); |
| 2332 | } |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2333 | /* need to record an error - either for the block or the device */ |
| 2334 | if (!rdev_set_badblocks(rdev, sector, sectors, 0)) |
| 2335 | md_error(rdev->mddev, rdev); |
| 2336 | return 0; |
| 2337 | } |
| 2338 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2339 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2340 | * This is a kernel thread which: |
| 2341 | * |
| 2342 | * 1. Retries failed read operations on working mirrors. |
| 2343 | * 2. Updates the raid superblock when problems encounter. |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2344 | * 3. Performs writes following reads for array synchronising. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2345 | */ |
| 2346 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2347 | static void fix_read_error(struct r10conf *conf, struct mddev *mddev, struct r10bio *r10_bio) |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2348 | { |
| 2349 | int sect = 0; /* Offset from r10_bio->sector */ |
| 2350 | int sectors = r10_bio->sectors; |
Yufen Yu | 13db16d | 2018-04-23 17:37:30 +0800 | [diff] [blame] | 2351 | struct md_rdev *rdev; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2352 | int max_read_errors = atomic_read(&mddev->max_corr_read_errors); |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2353 | int d = r10_bio->devs[r10_bio->read_slot].devnum; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2354 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2355 | /* still own a reference to this rdev, so it cannot |
| 2356 | * have been cleared recently. |
| 2357 | */ |
| 2358 | rdev = conf->mirrors[d].rdev; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2359 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2360 | if (test_bit(Faulty, &rdev->flags)) |
| 2361 | /* drive has already been failed, just ignore any |
| 2362 | more fix_read_error() attempts */ |
| 2363 | return; |
| 2364 | |
| 2365 | check_decay_read_errors(mddev, rdev); |
| 2366 | atomic_inc(&rdev->read_errors); |
| 2367 | if (atomic_read(&rdev->read_errors) > max_read_errors) { |
| 2368 | char b[BDEVNAME_SIZE]; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2369 | bdevname(rdev->bdev, b); |
| 2370 | |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2371 | pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n", |
| 2372 | mdname(mddev), b, |
| 2373 | atomic_read(&rdev->read_errors), max_read_errors); |
| 2374 | pr_notice("md/raid10:%s: %s: Failing raid device\n", |
| 2375 | mdname(mddev), b); |
NeilBrown | d683c8e | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 2376 | md_error(mddev, rdev); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2377 | r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED; |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2378 | return; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2379 | } |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2380 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2381 | while(sectors) { |
| 2382 | int s = sectors; |
| 2383 | int sl = r10_bio->read_slot; |
| 2384 | int success = 0; |
| 2385 | int start; |
| 2386 | |
| 2387 | if (s > (PAGE_SIZE>>9)) |
| 2388 | s = PAGE_SIZE >> 9; |
| 2389 | |
| 2390 | rcu_read_lock(); |
| 2391 | do { |
NeilBrown | 8dbed5c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2392 | sector_t first_bad; |
| 2393 | int bad_sectors; |
| 2394 | |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2395 | d = r10_bio->devs[sl].devnum; |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2396 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 2397 | if (rdev && |
NeilBrown | 8dbed5c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2398 | test_bit(In_sync, &rdev->flags) && |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 2399 | !test_bit(Faulty, &rdev->flags) && |
NeilBrown | 8dbed5c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2400 | is_badblock(rdev, r10_bio->devs[sl].addr + sect, s, |
| 2401 | &first_bad, &bad_sectors) == 0) { |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2402 | atomic_inc(&rdev->nr_pending); |
| 2403 | rcu_read_unlock(); |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 2404 | success = sync_page_io(rdev, |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2405 | r10_bio->devs[sl].addr + |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 2406 | sect, |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2407 | s<<9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2408 | conf->tmppage, |
| 2409 | REQ_OP_READ, 0, false); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2410 | rdev_dec_pending(rdev, mddev); |
| 2411 | rcu_read_lock(); |
| 2412 | if (success) |
| 2413 | break; |
| 2414 | } |
| 2415 | sl++; |
| 2416 | if (sl == conf->copies) |
| 2417 | sl = 0; |
| 2418 | } while (!success && sl != r10_bio->read_slot); |
| 2419 | rcu_read_unlock(); |
| 2420 | |
| 2421 | if (!success) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2422 | /* Cannot read from anywhere, just mark the block |
| 2423 | * as bad on the first device to discourage future |
| 2424 | * reads. |
| 2425 | */ |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2426 | int dn = r10_bio->devs[r10_bio->read_slot].devnum; |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2427 | rdev = conf->mirrors[dn].rdev; |
| 2428 | |
| 2429 | if (!rdev_set_badblocks( |
| 2430 | rdev, |
| 2431 | r10_bio->devs[r10_bio->read_slot].addr |
| 2432 | + sect, |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2433 | s, 0)) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2434 | md_error(mddev, rdev); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2435 | r10_bio->devs[r10_bio->read_slot].bio |
| 2436 | = IO_BLOCKED; |
| 2437 | } |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2438 | break; |
| 2439 | } |
| 2440 | |
| 2441 | start = sl; |
| 2442 | /* write it back and re-read */ |
| 2443 | rcu_read_lock(); |
| 2444 | while (sl != r10_bio->read_slot) { |
Robert Becker | 67b8dc4 | 2009-12-14 12:49:57 +1100 | [diff] [blame] | 2445 | char b[BDEVNAME_SIZE]; |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2446 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2447 | if (sl==0) |
| 2448 | sl = conf->copies; |
| 2449 | sl--; |
| 2450 | d = r10_bio->devs[sl].devnum; |
| 2451 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2452 | if (!rdev || |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 2453 | test_bit(Faulty, &rdev->flags) || |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2454 | !test_bit(In_sync, &rdev->flags)) |
| 2455 | continue; |
| 2456 | |
| 2457 | atomic_inc(&rdev->nr_pending); |
| 2458 | rcu_read_unlock(); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2459 | if (r10_sync_page_io(rdev, |
| 2460 | r10_bio->devs[sl].addr + |
| 2461 | sect, |
NeilBrown | 055d374 | 2012-07-03 15:55:33 +1000 | [diff] [blame] | 2462 | s, conf->tmppage, WRITE) |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2463 | == 0) { |
| 2464 | /* Well, this device is dead */ |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2465 | pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n", |
| 2466 | mdname(mddev), s, |
| 2467 | (unsigned long long)( |
| 2468 | sect + |
| 2469 | choose_data_offset(r10_bio, |
| 2470 | rdev)), |
| 2471 | bdevname(rdev->bdev, b)); |
| 2472 | pr_notice("md/raid10:%s: %s: failing drive\n", |
| 2473 | mdname(mddev), |
| 2474 | bdevname(rdev->bdev, b)); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2475 | } |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2476 | rdev_dec_pending(rdev, mddev); |
| 2477 | rcu_read_lock(); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2478 | } |
| 2479 | sl = start; |
| 2480 | while (sl != r10_bio->read_slot) { |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2481 | char b[BDEVNAME_SIZE]; |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2482 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2483 | if (sl==0) |
| 2484 | sl = conf->copies; |
| 2485 | sl--; |
| 2486 | d = r10_bio->devs[sl].devnum; |
| 2487 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2488 | if (!rdev || |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 2489 | test_bit(Faulty, &rdev->flags) || |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2490 | !test_bit(In_sync, &rdev->flags)) |
| 2491 | continue; |
Robert Becker | 67b8dc4 | 2009-12-14 12:49:57 +1100 | [diff] [blame] | 2492 | |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2493 | atomic_inc(&rdev->nr_pending); |
| 2494 | rcu_read_unlock(); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2495 | switch (r10_sync_page_io(rdev, |
| 2496 | r10_bio->devs[sl].addr + |
| 2497 | sect, |
NeilBrown | 055d374 | 2012-07-03 15:55:33 +1000 | [diff] [blame] | 2498 | s, conf->tmppage, |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2499 | READ)) { |
| 2500 | case 0: |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2501 | /* Well, this device is dead */ |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2502 | pr_notice("md/raid10:%s: unable to read back corrected sectors (%d sectors at %llu on %s)\n", |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2503 | mdname(mddev), s, |
| 2504 | (unsigned long long)( |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 2505 | sect + |
| 2506 | choose_data_offset(r10_bio, rdev)), |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2507 | bdevname(rdev->bdev, b)); |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2508 | pr_notice("md/raid10:%s: %s: failing drive\n", |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2509 | mdname(mddev), |
| 2510 | bdevname(rdev->bdev, b)); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2511 | break; |
| 2512 | case 1: |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2513 | pr_info("md/raid10:%s: read error corrected (%d sectors at %llu on %s)\n", |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2514 | mdname(mddev), s, |
| 2515 | (unsigned long long)( |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 2516 | sect + |
| 2517 | choose_data_offset(r10_bio, rdev)), |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2518 | bdevname(rdev->bdev, b)); |
| 2519 | atomic_add(s, &rdev->corrected_errors); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2520 | } |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2521 | |
| 2522 | rdev_dec_pending(rdev, mddev); |
| 2523 | rcu_read_lock(); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2524 | } |
| 2525 | rcu_read_unlock(); |
| 2526 | |
| 2527 | sectors -= s; |
| 2528 | sect += s; |
| 2529 | } |
| 2530 | } |
| 2531 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2532 | static int narrow_write_error(struct r10bio *r10_bio, int i) |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2533 | { |
| 2534 | struct bio *bio = r10_bio->master_bio; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2535 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2536 | struct r10conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2537 | struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2538 | /* bio has the data to be written to slot 'i' where |
| 2539 | * we just recently had a write error. |
| 2540 | * We repeatedly clone the bio and trim down to one block, |
| 2541 | * then try the write. Where the write fails we record |
| 2542 | * a bad block. |
| 2543 | * It is conceivable that the bio doesn't exactly align with |
| 2544 | * blocks. We must handle this. |
| 2545 | * |
| 2546 | * We currently own a reference to the rdev. |
| 2547 | */ |
| 2548 | |
| 2549 | int block_sectors; |
| 2550 | sector_t sector; |
| 2551 | int sectors; |
| 2552 | int sect_to_write = r10_bio->sectors; |
| 2553 | int ok = 1; |
| 2554 | |
| 2555 | if (rdev->badblocks.shift < 0) |
| 2556 | return 0; |
| 2557 | |
NeilBrown | f04ebb0 | 2015-02-16 14:51:54 +1100 | [diff] [blame] | 2558 | block_sectors = roundup(1 << rdev->badblocks.shift, |
| 2559 | bdev_logical_block_size(rdev->bdev) >> 9); |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2560 | sector = r10_bio->sector; |
| 2561 | sectors = ((r10_bio->sector + block_sectors) |
| 2562 | & ~(sector_t)(block_sectors - 1)) |
| 2563 | - sector; |
| 2564 | |
| 2565 | while (sect_to_write) { |
| 2566 | struct bio *wbio; |
Tomasz Majchrzak | 2702862 | 2016-08-23 10:53:57 +0200 | [diff] [blame] | 2567 | sector_t wsector; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2568 | if (sectors > sect_to_write) |
| 2569 | sectors = sect_to_write; |
| 2570 | /* Write at 'sector' for 'sectors' */ |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2571 | wbio = bio_clone_fast(bio, GFP_NOIO, &mddev->bio_set); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2572 | bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors); |
Tomasz Majchrzak | 2702862 | 2016-08-23 10:53:57 +0200 | [diff] [blame] | 2573 | wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector); |
| 2574 | wbio->bi_iter.bi_sector = wsector + |
| 2575 | choose_data_offset(r10_bio, rdev); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2576 | bio_set_dev(wbio, rdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2577 | bio_set_op_attrs(wbio, REQ_OP_WRITE, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2578 | |
| 2579 | if (submit_bio_wait(wbio) < 0) |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2580 | /* Failure! */ |
Tomasz Majchrzak | 2702862 | 2016-08-23 10:53:57 +0200 | [diff] [blame] | 2581 | ok = rdev_set_badblocks(rdev, wsector, |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2582 | sectors, 0) |
| 2583 | && ok; |
| 2584 | |
| 2585 | bio_put(wbio); |
| 2586 | sect_to_write -= sectors; |
| 2587 | sector += sectors; |
| 2588 | sectors = block_sectors; |
| 2589 | } |
| 2590 | return ok; |
| 2591 | } |
| 2592 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2593 | static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio) |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2594 | { |
| 2595 | int slot = r10_bio->read_slot; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2596 | struct bio *bio; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2597 | struct r10conf *conf = mddev->private; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2598 | struct md_rdev *rdev = r10_bio->devs[slot].rdev; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2599 | |
| 2600 | /* we got a read error. Maybe the drive is bad. Maybe just |
| 2601 | * the block and we can fix it. |
| 2602 | * We freeze all other IO, and try reading the block from |
| 2603 | * other devices. When we find one, we re-write |
| 2604 | * and check it that fixes the read error. |
| 2605 | * This is all done synchronously while the array is |
| 2606 | * frozen. |
| 2607 | */ |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2608 | bio = r10_bio->devs[slot].bio; |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2609 | bio_put(bio); |
| 2610 | r10_bio->devs[slot].bio = NULL; |
| 2611 | |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2612 | if (mddev->ro) |
| 2613 | r10_bio->devs[slot].bio = IO_BLOCKED; |
| 2614 | else if (!test_bit(FailFast, &rdev->flags)) { |
NeilBrown | e2d5992 | 2013-06-12 11:01:22 +1000 | [diff] [blame] | 2615 | freeze_array(conf, 1); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2616 | fix_read_error(conf, mddev, r10_bio); |
| 2617 | unfreeze_array(conf); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2618 | } else |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2619 | md_error(mddev, rdev); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2620 | |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2621 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 2622 | allow_barrier(conf); |
| 2623 | r10_bio->state = 0; |
| 2624 | raid10_read_request(mddev, r10_bio->master_bio, r10_bio); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2625 | } |
| 2626 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2627 | static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2628 | { |
| 2629 | /* Some sort of write request has finished and it |
| 2630 | * succeeded in writing where we thought there was a |
| 2631 | * bad block. So forget the bad block. |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2632 | * Or possibly if failed and we need to record |
| 2633 | * a bad block. |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2634 | */ |
| 2635 | int m; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2636 | struct md_rdev *rdev; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2637 | |
| 2638 | if (test_bit(R10BIO_IsSync, &r10_bio->state) || |
| 2639 | test_bit(R10BIO_IsRecover, &r10_bio->state)) { |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2640 | for (m = 0; m < conf->copies; m++) { |
| 2641 | int dev = r10_bio->devs[m].devnum; |
| 2642 | rdev = conf->mirrors[dev].rdev; |
Yufen Yu | 01a69ca | 2018-02-06 17:39:15 +0800 | [diff] [blame] | 2643 | if (r10_bio->devs[m].bio == NULL || |
| 2644 | r10_bio->devs[m].bio->bi_end_io == NULL) |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2645 | continue; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2646 | if (!r10_bio->devs[m].bio->bi_status) { |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2647 | rdev_clear_badblocks( |
| 2648 | rdev, |
| 2649 | r10_bio->devs[m].addr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2650 | r10_bio->sectors, 0); |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2651 | } else { |
| 2652 | if (!rdev_set_badblocks( |
| 2653 | rdev, |
| 2654 | r10_bio->devs[m].addr, |
| 2655 | r10_bio->sectors, 0)) |
| 2656 | md_error(conf->mddev, rdev); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2657 | } |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2658 | rdev = conf->mirrors[dev].replacement; |
Yufen Yu | 01a69ca | 2018-02-06 17:39:15 +0800 | [diff] [blame] | 2659 | if (r10_bio->devs[m].repl_bio == NULL || |
| 2660 | r10_bio->devs[m].repl_bio->bi_end_io == NULL) |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2661 | continue; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2662 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2663 | if (!r10_bio->devs[m].repl_bio->bi_status) { |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2664 | rdev_clear_badblocks( |
| 2665 | rdev, |
| 2666 | r10_bio->devs[m].addr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2667 | r10_bio->sectors, 0); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2668 | } else { |
| 2669 | if (!rdev_set_badblocks( |
| 2670 | rdev, |
| 2671 | r10_bio->devs[m].addr, |
| 2672 | r10_bio->sectors, 0)) |
| 2673 | md_error(conf->mddev, rdev); |
| 2674 | } |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2675 | } |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2676 | put_buf(r10_bio); |
| 2677 | } else { |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2678 | bool fail = false; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2679 | for (m = 0; m < conf->copies; m++) { |
| 2680 | int dev = r10_bio->devs[m].devnum; |
| 2681 | struct bio *bio = r10_bio->devs[m].bio; |
| 2682 | rdev = conf->mirrors[dev].rdev; |
| 2683 | if (bio == IO_MADE_GOOD) { |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2684 | rdev_clear_badblocks( |
| 2685 | rdev, |
| 2686 | r10_bio->devs[m].addr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2687 | r10_bio->sectors, 0); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2688 | rdev_dec_pending(rdev, conf->mddev); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2689 | } else if (bio != NULL && bio->bi_status) { |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2690 | fail = true; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2691 | if (!narrow_write_error(r10_bio, m)) { |
| 2692 | md_error(conf->mddev, rdev); |
| 2693 | set_bit(R10BIO_Degraded, |
| 2694 | &r10_bio->state); |
| 2695 | } |
| 2696 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2697 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2698 | bio = r10_bio->devs[m].repl_bio; |
| 2699 | rdev = conf->mirrors[dev].replacement; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2700 | if (rdev && bio == IO_MADE_GOOD) { |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2701 | rdev_clear_badblocks( |
| 2702 | rdev, |
| 2703 | r10_bio->devs[m].addr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2704 | r10_bio->sectors, 0); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2705 | rdev_dec_pending(rdev, conf->mddev); |
| 2706 | } |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2707 | } |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2708 | if (fail) { |
| 2709 | spin_lock_irq(&conf->device_lock); |
| 2710 | list_add(&r10_bio->retry_list, &conf->bio_end_io_list); |
Shaohua Li | 23ddba8 | 2016-03-14 11:49:32 -0700 | [diff] [blame] | 2711 | conf->nr_queued++; |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2712 | spin_unlock_irq(&conf->device_lock); |
Guoqing Jiang | cf25ae7 | 2017-04-17 17:11:05 +0800 | [diff] [blame] | 2713 | /* |
| 2714 | * In case freeze_array() is waiting for condition |
| 2715 | * nr_pending == nr_queued + extra to be true. |
| 2716 | */ |
| 2717 | wake_up(&conf->wait_barrier); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2718 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | c340702 | 2015-10-24 16:23:48 +1100 | [diff] [blame] | 2719 | } else { |
| 2720 | if (test_bit(R10BIO_WriteError, |
| 2721 | &r10_bio->state)) |
| 2722 | close_write(r10_bio); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2723 | raid_end_bio_io(r10_bio); |
NeilBrown | c340702 | 2015-10-24 16:23:48 +1100 | [diff] [blame] | 2724 | } |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2725 | } |
| 2726 | } |
| 2727 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 2728 | static void raid10d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 2730 | struct mddev *mddev = thread->mddev; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2731 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | unsigned long flags; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2733 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2734 | struct list_head *head = &conf->retry_list; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2735 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | |
| 2737 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2738 | |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2739 | if (!list_empty_careful(&conf->bio_end_io_list) && |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2740 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2741 | LIST_HEAD(tmp); |
| 2742 | spin_lock_irqsave(&conf->device_lock, flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2743 | if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
Shaohua Li | 23ddba8 | 2016-03-14 11:49:32 -0700 | [diff] [blame] | 2744 | while (!list_empty(&conf->bio_end_io_list)) { |
| 2745 | list_move(conf->bio_end_io_list.prev, &tmp); |
| 2746 | conf->nr_queued--; |
| 2747 | } |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2748 | } |
| 2749 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2750 | while (!list_empty(&tmp)) { |
Mikulas Patocka | a452744 | 2015-10-01 15:17:43 -0400 | [diff] [blame] | 2751 | r10_bio = list_first_entry(&tmp, struct r10bio, |
| 2752 | retry_list); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2753 | list_del(&r10_bio->retry_list); |
NeilBrown | c340702 | 2015-10-24 16:23:48 +1100 | [diff] [blame] | 2754 | if (mddev->degraded) |
| 2755 | set_bit(R10BIO_Degraded, &r10_bio->state); |
| 2756 | |
| 2757 | if (test_bit(R10BIO_WriteError, |
| 2758 | &r10_bio->state)) |
| 2759 | close_write(r10_bio); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2760 | raid_end_bio_io(r10_bio); |
| 2761 | } |
| 2762 | } |
| 2763 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2764 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | for (;;) { |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2766 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 2767 | flush_pending_writes(conf); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2768 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2769 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2770 | if (list_empty(head)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2771 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2772 | break; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2773 | } |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2774 | r10_bio = list_entry(head->prev, struct r10bio, retry_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | list_del(head->prev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2776 | conf->nr_queued--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2777 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2778 | |
| 2779 | mddev = r10_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2780 | conf = mddev->private; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2781 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 2782 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2783 | handle_write_completed(conf, r10_bio); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2784 | else if (test_bit(R10BIO_IsReshape, &r10_bio->state)) |
| 2785 | reshape_request_write(mddev, r10_bio); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2786 | else if (test_bit(R10BIO_IsSync, &r10_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2787 | sync_request_write(mddev, r10_bio); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 2788 | else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | recovery_request_write(mddev, r10_bio); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2790 | else if (test_bit(R10BIO_ReadError, &r10_bio->state)) |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2791 | handle_read_error(mddev, r10_bio); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 2792 | else |
| 2793 | WARN_ON_ONCE(1); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2794 | |
NeilBrown | 1d9d524 | 2009-10-16 15:55:32 +1100 | [diff] [blame] | 2795 | cond_resched(); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2796 | if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING)) |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2797 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | } |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2799 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | } |
| 2801 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2802 | static int init_resync(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2804 | int ret, buffs, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2805 | |
| 2806 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2807 | BUG_ON(mempool_initialized(&conf->r10buf_pool)); |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2808 | conf->have_replacement = 0; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2809 | for (i = 0; i < conf->geo.raid_disks; i++) |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2810 | if (conf->mirrors[i].replacement) |
| 2811 | conf->have_replacement = 1; |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2812 | ret = mempool_init(&conf->r10buf_pool, buffs, |
| 2813 | r10buf_pool_alloc, r10buf_pool_free, conf); |
| 2814 | if (ret) |
| 2815 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2816 | conf->next_resync = 0; |
| 2817 | return 0; |
| 2818 | } |
| 2819 | |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 2820 | static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf) |
| 2821 | { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2822 | struct r10bio *r10bio = mempool_alloc(&conf->r10buf_pool, GFP_NOIO); |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 2823 | struct rsync_pages *rp; |
| 2824 | struct bio *bio; |
| 2825 | int nalloc; |
| 2826 | int i; |
| 2827 | |
| 2828 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) || |
| 2829 | test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery)) |
| 2830 | nalloc = conf->copies; /* resync */ |
| 2831 | else |
| 2832 | nalloc = 2; /* recovery */ |
| 2833 | |
| 2834 | for (i = 0; i < nalloc; i++) { |
| 2835 | bio = r10bio->devs[i].bio; |
| 2836 | rp = bio->bi_private; |
| 2837 | bio_reset(bio); |
| 2838 | bio->bi_private = rp; |
| 2839 | bio = r10bio->devs[i].repl_bio; |
| 2840 | if (bio) { |
| 2841 | rp = bio->bi_private; |
| 2842 | bio_reset(bio); |
| 2843 | bio->bi_private = rp; |
| 2844 | } |
| 2845 | } |
| 2846 | return r10bio; |
| 2847 | } |
| 2848 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | /* |
Guoqing Jiang | 8db8791 | 2017-10-24 15:11:52 +0800 | [diff] [blame] | 2850 | * Set cluster_sync_high since we need other nodes to add the |
| 2851 | * range [cluster_sync_low, cluster_sync_high] to suspend list. |
| 2852 | */ |
| 2853 | static void raid10_set_cluster_sync_high(struct r10conf *conf) |
| 2854 | { |
| 2855 | sector_t window_size; |
| 2856 | int extra_chunk, chunks; |
| 2857 | |
| 2858 | /* |
| 2859 | * First, here we define "stripe" as a unit which across |
| 2860 | * all member devices one time, so we get chunks by use |
| 2861 | * raid_disks / near_copies. Otherwise, if near_copies is |
| 2862 | * close to raid_disks, then resync window could increases |
| 2863 | * linearly with the increase of raid_disks, which means |
| 2864 | * we will suspend a really large IO window while it is not |
| 2865 | * necessary. If raid_disks is not divisible by near_copies, |
| 2866 | * an extra chunk is needed to ensure the whole "stripe" is |
| 2867 | * covered. |
| 2868 | */ |
| 2869 | |
| 2870 | chunks = conf->geo.raid_disks / conf->geo.near_copies; |
| 2871 | if (conf->geo.raid_disks % conf->geo.near_copies == 0) |
| 2872 | extra_chunk = 0; |
| 2873 | else |
| 2874 | extra_chunk = 1; |
| 2875 | window_size = (chunks + extra_chunk) * conf->mddev->chunk_sectors; |
| 2876 | |
| 2877 | /* |
| 2878 | * At least use a 32M window to align with raid1's resync window |
| 2879 | */ |
| 2880 | window_size = (CLUSTER_RESYNC_WINDOW_SECTORS > window_size) ? |
| 2881 | CLUSTER_RESYNC_WINDOW_SECTORS : window_size; |
| 2882 | |
| 2883 | conf->cluster_sync_high = conf->cluster_sync_low + window_size; |
| 2884 | } |
| 2885 | |
| 2886 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2887 | * perform a "sync" on one "block" |
| 2888 | * |
| 2889 | * We need to make sure that no normal I/O request - particularly write |
| 2890 | * requests - conflict with active sync requests. |
| 2891 | * |
| 2892 | * This is achieved by tracking pending requests and a 'barrier' concept |
| 2893 | * that can be installed to exclude normal IO requests. |
| 2894 | * |
| 2895 | * Resync and recovery are handled very differently. |
| 2896 | * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. |
| 2897 | * |
| 2898 | * For resync, we iterate over virtual addresses, read all copies, |
| 2899 | * and update if there are differences. If only one copy is live, |
| 2900 | * skip it. |
| 2901 | * For recovery, we iterate over physical addresses, read a good |
| 2902 | * value for each non-in_sync drive, and over-write. |
| 2903 | * |
| 2904 | * So, for recovery we may have several outstanding complex requests for a |
| 2905 | * given address, one for each out-of-sync device. We model this by allocating |
| 2906 | * a number of r10_bio structures, one for each out-of-sync device. |
| 2907 | * As we setup these structures, we collect all bio's together into a list |
| 2908 | * which we then process collectively to add pages, and then process again |
| 2909 | * to pass to generic_make_request. |
| 2910 | * |
| 2911 | * The r10_bio structures are linked using a borrowed master_bio pointer. |
| 2912 | * This link is counted in ->remaining. When the r10_bio that points to NULL |
| 2913 | * has its remaining count decremented to 0, the whole complex operation |
| 2914 | * is complete. |
| 2915 | * |
| 2916 | */ |
| 2917 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 2918 | static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr, |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 2919 | int *skipped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2921 | struct r10conf *conf = mddev->private; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2922 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2923 | struct bio *biolist = NULL, *bio; |
| 2924 | sector_t max_sector, nr_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | int i; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2926 | int max_sync; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 2927 | sector_t sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2928 | sector_t sectors_skipped = 0; |
| 2929 | int chunks_skipped = 0; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2930 | sector_t chunk_mask = conf->geo.chunk_mask; |
Ming Lei | 022e510 | 2017-07-14 16:14:42 +0800 | [diff] [blame] | 2931 | int page_idx = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 2933 | if (!mempool_initialized(&conf->r10buf_pool)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2934 | if (init_resync(conf)) |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2935 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2936 | |
Martin Wilck | 7e83ccb | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2937 | /* |
| 2938 | * Allow skipping a full rebuild for incremental assembly |
| 2939 | * of a clean array, like RAID1 does. |
| 2940 | */ |
| 2941 | if (mddev->bitmap == NULL && |
| 2942 | mddev->recovery_cp == MaxSector && |
NeilBrown | 1376512 | 2013-07-04 16:41:53 +1000 | [diff] [blame] | 2943 | mddev->reshape_position == MaxSector && |
| 2944 | !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
Martin Wilck | 7e83ccb | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2945 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 1376512 | 2013-07-04 16:41:53 +1000 | [diff] [blame] | 2946 | !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Martin Wilck | 7e83ccb | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2947 | conf->fullsync == 0) { |
| 2948 | *skipped = 1; |
NeilBrown | 1376512 | 2013-07-04 16:41:53 +1000 | [diff] [blame] | 2949 | return mddev->dev_sectors - sector_nr; |
Martin Wilck | 7e83ccb | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2950 | } |
| 2951 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2952 | skipped: |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2953 | max_sector = mddev->dev_sectors; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2954 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 2955 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2956 | max_sector = mddev->resync_max_sectors; |
| 2957 | if (sector_nr >= max_sector) { |
Guoqing Jiang | 8db8791 | 2017-10-24 15:11:52 +0800 | [diff] [blame] | 2958 | conf->cluster_sync_low = 0; |
| 2959 | conf->cluster_sync_high = 0; |
| 2960 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2961 | /* If we aborted, we need to abort the |
| 2962 | * sync on the 'current' bitmap chucks (there can |
| 2963 | * be several when recovering multiple devices). |
| 2964 | * as we may have started syncing it but not finished. |
| 2965 | * We can find the current address in |
| 2966 | * mddev->curr_resync, but for recovery, |
| 2967 | * we need to convert that to several |
| 2968 | * virtual addresses. |
| 2969 | */ |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2970 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 2971 | end_reshape(conf); |
NeilBrown | b396855 | 2014-08-18 13:59:50 +1000 | [diff] [blame] | 2972 | close_sync(conf); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2973 | return 0; |
| 2974 | } |
| 2975 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2976 | if (mddev->curr_resync < max_sector) { /* aborted */ |
| 2977 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2978 | md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 2979 | &sync_blocks, 1); |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2980 | else for (i = 0; i < conf->geo.raid_disks; i++) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2981 | sector_t sect = |
| 2982 | raid10_find_virt(conf, mddev->curr_resync, i); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 2983 | md_bitmap_end_sync(mddev->bitmap, sect, |
| 2984 | &sync_blocks, 1); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2985 | } |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2986 | } else { |
| 2987 | /* completed sync */ |
| 2988 | if ((!mddev->bitmap || conf->fullsync) |
| 2989 | && conf->have_replacement |
| 2990 | && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 2991 | /* Completed a full sync so the replacements |
| 2992 | * are now fully recovered. |
| 2993 | */ |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 2994 | rcu_read_lock(); |
| 2995 | for (i = 0; i < conf->geo.raid_disks; i++) { |
| 2996 | struct md_rdev *rdev = |
| 2997 | rcu_dereference(conf->mirrors[i].replacement); |
| 2998 | if (rdev) |
| 2999 | rdev->recovery_offset = MaxSector; |
| 3000 | } |
| 3001 | rcu_read_unlock(); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3002 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3003 | conf->fullsync = 0; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3004 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3005 | md_bitmap_close_sync(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3006 | close_sync(conf); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3007 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | return sectors_skipped; |
| 3009 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3010 | |
| 3011 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 3012 | return reshape_request(mddev, sector_nr, skipped); |
| 3013 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3014 | if (chunks_skipped >= conf->geo.raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3015 | /* if there has been nothing to do on any drive, |
| 3016 | * then there is nothing to do at all.. |
| 3017 | */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3018 | *skipped = 1; |
| 3019 | return (max_sector - sector_nr) + sectors_skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3020 | } |
| 3021 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 3022 | if (max_sector > mddev->resync_max) |
| 3023 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ |
| 3024 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3025 | /* make sure whole request will fit in a chunk - if chunks |
| 3026 | * are meaningful |
| 3027 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3028 | if (conf->geo.near_copies < conf->geo.raid_disks && |
| 3029 | max_sector > (sector_nr | chunk_mask)) |
| 3030 | max_sector = (sector_nr | chunk_mask) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3031 | |
Tomasz Majchrzak | 7ac5044 | 2016-06-13 15:51:19 +0200 | [diff] [blame] | 3032 | /* |
| 3033 | * If there is non-resync activity waiting for a turn, then let it |
| 3034 | * though before starting on this new sync request. |
| 3035 | */ |
| 3036 | if (conf->nr_waiting) |
| 3037 | schedule_timeout_uninterruptible(1); |
| 3038 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3039 | /* Again, very different code for resync and recovery. |
| 3040 | * Both must result in an r10bio with a list of bios that |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3041 | * have bi_end_io, bi_sector, bi_disk set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3042 | * and bi_private set to the r10bio. |
| 3043 | * For recovery, we may actually create several r10bios |
| 3044 | * with 2 bios in each, that correspond to the bios in the main one. |
| 3045 | * In this case, the subordinate r10bios link back through a |
| 3046 | * borrowed master_bio pointer, and the counter in the master |
| 3047 | * includes a ref from each subordinate. |
| 3048 | */ |
| 3049 | /* First, we decide what to do and set ->bi_end_io |
| 3050 | * To end_sync_read if we want to read, and |
| 3051 | * end_sync_write if we will want to write. |
| 3052 | */ |
| 3053 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3054 | max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3055 | if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 3056 | /* recovery... the complicated one */ |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3057 | int j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3058 | r10_bio = NULL; |
| 3059 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3060 | for (i = 0 ; i < conf->geo.raid_disks; i++) { |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3061 | int still_degraded; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 3062 | struct r10bio *rb2; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3063 | sector_t sect; |
| 3064 | int must_sync; |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3065 | int any_working; |
Alex Wu | ee37d73 | 2018-09-21 16:05:03 +0800 | [diff] [blame] | 3066 | int need_recover = 0; |
| 3067 | int need_replace = 0; |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 3068 | struct raid10_info *mirror = &conf->mirrors[i]; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3069 | struct md_rdev *mrdev, *mreplace; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3070 | |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3071 | rcu_read_lock(); |
| 3072 | mrdev = rcu_dereference(mirror->rdev); |
| 3073 | mreplace = rcu_dereference(mirror->replacement); |
| 3074 | |
Alex Wu | ee37d73 | 2018-09-21 16:05:03 +0800 | [diff] [blame] | 3075 | if (mrdev != NULL && |
| 3076 | !test_bit(Faulty, &mrdev->flags) && |
| 3077 | !test_bit(In_sync, &mrdev->flags)) |
| 3078 | need_recover = 1; |
| 3079 | if (mreplace != NULL && |
| 3080 | !test_bit(Faulty, &mreplace->flags)) |
| 3081 | need_replace = 1; |
| 3082 | |
| 3083 | if (!need_recover && !need_replace) { |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3084 | rcu_read_unlock(); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3085 | continue; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3086 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3087 | |
| 3088 | still_degraded = 0; |
| 3089 | /* want to reconstruct this device */ |
| 3090 | rb2 = r10_bio; |
| 3091 | sect = raid10_find_virt(conf, sector_nr, i); |
NeilBrown | fc448a1 | 2012-07-03 10:37:30 +1000 | [diff] [blame] | 3092 | if (sect >= mddev->resync_max_sectors) { |
| 3093 | /* last stripe is not complete - don't |
| 3094 | * try to recover this sector. |
| 3095 | */ |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3096 | rcu_read_unlock(); |
NeilBrown | fc448a1 | 2012-07-03 10:37:30 +1000 | [diff] [blame] | 3097 | continue; |
| 3098 | } |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 3099 | if (mreplace && test_bit(Faulty, &mreplace->flags)) |
| 3100 | mreplace = NULL; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3101 | /* Unless we are doing a full sync, or a replacement |
| 3102 | * we only need to recover the block if it is set in |
| 3103 | * the bitmap |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3104 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3105 | must_sync = md_bitmap_start_sync(mddev->bitmap, sect, |
| 3106 | &sync_blocks, 1); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3107 | if (sync_blocks < max_sync) |
| 3108 | max_sync = sync_blocks; |
| 3109 | if (!must_sync && |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3110 | mreplace == NULL && |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3111 | !conf->fullsync) { |
| 3112 | /* yep, skip the sync_blocks here, but don't assume |
| 3113 | * that there will never be anything to do here |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3114 | */ |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3115 | chunks_skipped = -1; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3116 | rcu_read_unlock(); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3117 | continue; |
| 3118 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3119 | atomic_inc(&mrdev->nr_pending); |
| 3120 | if (mreplace) |
| 3121 | atomic_inc(&mreplace->nr_pending); |
| 3122 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 3124 | r10_bio = raid10_alloc_init_r10buf(conf); |
NeilBrown | cb8b12b | 2014-08-18 14:38:45 +1000 | [diff] [blame] | 3125 | r10_bio->state = 0; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3126 | raise_barrier(conf, rb2 != NULL); |
| 3127 | atomic_set(&r10_bio->remaining, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3128 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3129 | r10_bio->master_bio = (struct bio*)rb2; |
| 3130 | if (rb2) |
| 3131 | atomic_inc(&rb2->remaining); |
| 3132 | r10_bio->mddev = mddev; |
| 3133 | set_bit(R10BIO_IsRecover, &r10_bio->state); |
| 3134 | r10_bio->sector = sect; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3135 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3136 | raid10_find_phys(conf, r10_bio); |
NeilBrown | 1805556 | 2009-05-07 12:48:10 +1000 | [diff] [blame] | 3137 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3138 | /* Need to check if the array will still be |
| 3139 | * degraded |
| 3140 | */ |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3141 | rcu_read_lock(); |
| 3142 | for (j = 0; j < conf->geo.raid_disks; j++) { |
| 3143 | struct md_rdev *rdev = rcu_dereference( |
| 3144 | conf->mirrors[j].rdev); |
| 3145 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) { |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3146 | still_degraded = 1; |
NeilBrown | 87fc767 | 2005-09-09 16:24:04 -0700 | [diff] [blame] | 3147 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3148 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3149 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3150 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3151 | must_sync = md_bitmap_start_sync(mddev->bitmap, sect, |
| 3152 | &sync_blocks, still_degraded); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3153 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3154 | any_working = 0; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3155 | for (j=0; j<conf->copies;j++) { |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3156 | int k; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3157 | int d = r10_bio->devs[j].devnum; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3158 | sector_t from_addr, to_addr; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3159 | struct md_rdev *rdev = |
| 3160 | rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3161 | sector_t sector, first_bad; |
| 3162 | int bad_sectors; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3163 | if (!rdev || |
| 3164 | !test_bit(In_sync, &rdev->flags)) |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3165 | continue; |
| 3166 | /* This is where we read from */ |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3167 | any_working = 1; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3168 | sector = r10_bio->devs[j].addr; |
| 3169 | |
| 3170 | if (is_badblock(rdev, sector, max_sync, |
| 3171 | &first_bad, &bad_sectors)) { |
| 3172 | if (first_bad > sector) |
| 3173 | max_sync = first_bad - sector; |
| 3174 | else { |
| 3175 | bad_sectors -= (sector |
| 3176 | - first_bad); |
| 3177 | if (max_sync > bad_sectors) |
| 3178 | max_sync = bad_sectors; |
| 3179 | continue; |
| 3180 | } |
| 3181 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3182 | bio = r10_bio->devs[0].bio; |
| 3183 | bio->bi_next = biolist; |
| 3184 | biolist = bio; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3185 | bio->bi_end_io = end_sync_read; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3186 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 3187 | if (test_bit(FailFast, &rdev->flags)) |
| 3188 | bio->bi_opf |= MD_FAILFAST; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3189 | from_addr = r10_bio->devs[j].addr; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3190 | bio->bi_iter.bi_sector = from_addr + |
| 3191 | rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3192 | bio_set_dev(bio, rdev->bdev); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3193 | atomic_inc(&rdev->nr_pending); |
| 3194 | /* and we write to 'i' (if not in_sync) */ |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3195 | |
| 3196 | for (k=0; k<conf->copies; k++) |
| 3197 | if (r10_bio->devs[k].devnum == i) |
| 3198 | break; |
| 3199 | BUG_ON(k == conf->copies); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3200 | to_addr = r10_bio->devs[k].addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3201 | r10_bio->devs[0].devnum = d; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3202 | r10_bio->devs[0].addr = from_addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3203 | r10_bio->devs[1].devnum = i; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3204 | r10_bio->devs[1].addr = to_addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3205 | |
Alex Wu | ee37d73 | 2018-09-21 16:05:03 +0800 | [diff] [blame] | 3206 | if (need_recover) { |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3207 | bio = r10_bio->devs[1].bio; |
| 3208 | bio->bi_next = biolist; |
| 3209 | biolist = bio; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3210 | bio->bi_end_io = end_sync_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3211 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3212 | bio->bi_iter.bi_sector = to_addr |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3213 | + mrdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3214 | bio_set_dev(bio, mrdev->bdev); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3215 | atomic_inc(&r10_bio->remaining); |
| 3216 | } else |
| 3217 | r10_bio->devs[1].bio->bi_end_io = NULL; |
| 3218 | |
| 3219 | /* and maybe write to replacement */ |
| 3220 | bio = r10_bio->devs[1].repl_bio; |
| 3221 | if (bio) |
| 3222 | bio->bi_end_io = NULL; |
Alex Wu | ee37d73 | 2018-09-21 16:05:03 +0800 | [diff] [blame] | 3223 | /* Note: if need_replace, then bio |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3224 | * cannot be NULL as r10buf_pool_alloc will |
| 3225 | * have allocated it. |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3226 | */ |
Alex Wu | ee37d73 | 2018-09-21 16:05:03 +0800 | [diff] [blame] | 3227 | if (!need_replace) |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3228 | break; |
| 3229 | bio->bi_next = biolist; |
| 3230 | biolist = bio; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3231 | bio->bi_end_io = end_sync_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3232 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3233 | bio->bi_iter.bi_sector = to_addr + |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3234 | mreplace->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3235 | bio_set_dev(bio, mreplace->bdev); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3236 | atomic_inc(&r10_bio->remaining); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3237 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3238 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3239 | rcu_read_unlock(); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3240 | if (j == conf->copies) { |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3241 | /* Cannot recover, so abort the recovery or |
| 3242 | * record a bad block */ |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3243 | if (any_working) { |
| 3244 | /* problem is that there are bad blocks |
| 3245 | * on other device(s) |
| 3246 | */ |
| 3247 | int k; |
| 3248 | for (k = 0; k < conf->copies; k++) |
| 3249 | if (r10_bio->devs[k].devnum == i) |
| 3250 | break; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3251 | if (!test_bit(In_sync, |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3252 | &mrdev->flags) |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3253 | && !rdev_set_badblocks( |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3254 | mrdev, |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3255 | r10_bio->devs[k].addr, |
| 3256 | max_sync, 0)) |
| 3257 | any_working = 0; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3258 | if (mreplace && |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3259 | !rdev_set_badblocks( |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3260 | mreplace, |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3261 | r10_bio->devs[k].addr, |
| 3262 | max_sync, 0)) |
| 3263 | any_working = 0; |
| 3264 | } |
| 3265 | if (!any_working) { |
| 3266 | if (!test_and_set_bit(MD_RECOVERY_INTR, |
| 3267 | &mddev->recovery)) |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3268 | pr_warn("md/raid10:%s: insufficient working devices for recovery.\n", |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3269 | mdname(mddev)); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3270 | mirror->recovery_disabled |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3271 | = mddev->recovery_disabled; |
| 3272 | } |
NeilBrown | e8b8491 | 2014-01-06 10:35:34 +1100 | [diff] [blame] | 3273 | put_buf(r10_bio); |
| 3274 | if (rb2) |
| 3275 | atomic_dec(&rb2->remaining); |
| 3276 | r10_bio = rb2; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3277 | rdev_dec_pending(mrdev, mddev); |
| 3278 | if (mreplace) |
| 3279 | rdev_dec_pending(mreplace, mddev); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3280 | break; |
| 3281 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3282 | rdev_dec_pending(mrdev, mddev); |
| 3283 | if (mreplace) |
| 3284 | rdev_dec_pending(mreplace, mddev); |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 3285 | if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) { |
| 3286 | /* Only want this if there is elsewhere to |
| 3287 | * read from. 'j' is currently the first |
| 3288 | * readable copy. |
| 3289 | */ |
| 3290 | int targets = 1; |
| 3291 | for (; j < conf->copies; j++) { |
| 3292 | int d = r10_bio->devs[j].devnum; |
| 3293 | if (conf->mirrors[d].rdev && |
| 3294 | test_bit(In_sync, |
| 3295 | &conf->mirrors[d].rdev->flags)) |
| 3296 | targets++; |
| 3297 | } |
| 3298 | if (targets == 1) |
| 3299 | r10_bio->devs[0].bio->bi_opf |
| 3300 | &= ~MD_FAILFAST; |
| 3301 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3302 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3303 | if (biolist == NULL) { |
| 3304 | while (r10_bio) { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 3305 | struct r10bio *rb2 = r10_bio; |
| 3306 | r10_bio = (struct r10bio*) rb2->master_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | rb2->master_bio = NULL; |
| 3308 | put_buf(rb2); |
| 3309 | } |
| 3310 | goto giveup; |
| 3311 | } |
| 3312 | } else { |
| 3313 | /* resync. Schedule a read for every block at this virt offset */ |
| 3314 | int count = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3315 | |
Guoqing Jiang | 8db8791 | 2017-10-24 15:11:52 +0800 | [diff] [blame] | 3316 | /* |
| 3317 | * Since curr_resync_completed could probably not update in |
| 3318 | * time, and we will set cluster_sync_low based on it. |
| 3319 | * Let's check against "sector_nr + 2 * RESYNC_SECTORS" for |
| 3320 | * safety reason, which ensures curr_resync_completed is |
| 3321 | * updated in bitmap_cond_end_sync. |
| 3322 | */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3323 | md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, |
| 3324 | mddev_is_clustered(mddev) && |
| 3325 | (sector_nr + 2 * RESYNC_SECTORS > conf->cluster_sync_high)); |
NeilBrown | 78200d4 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 3326 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3327 | if (!md_bitmap_start_sync(mddev->bitmap, sector_nr, |
| 3328 | &sync_blocks, mddev->degraded) && |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3329 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, |
| 3330 | &mddev->recovery)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3331 | /* We can skip this block */ |
| 3332 | *skipped = 1; |
| 3333 | return sync_blocks + sectors_skipped; |
| 3334 | } |
| 3335 | if (sync_blocks < max_sync) |
| 3336 | max_sync = sync_blocks; |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 3337 | r10_bio = raid10_alloc_init_r10buf(conf); |
NeilBrown | cb8b12b | 2014-08-18 14:38:45 +1000 | [diff] [blame] | 3338 | r10_bio->state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | r10_bio->mddev = mddev; |
| 3341 | atomic_set(&r10_bio->remaining, 0); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3342 | raise_barrier(conf, 0); |
| 3343 | conf->next_resync = sector_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3344 | |
| 3345 | r10_bio->master_bio = NULL; |
| 3346 | r10_bio->sector = sector_nr; |
| 3347 | set_bit(R10BIO_IsSync, &r10_bio->state); |
| 3348 | raid10_find_phys(conf, r10_bio); |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3349 | r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3351 | for (i = 0; i < conf->copies; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3352 | int d = r10_bio->devs[i].devnum; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3353 | sector_t first_bad, sector; |
| 3354 | int bad_sectors; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3355 | struct md_rdev *rdev; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3356 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3357 | if (r10_bio->devs[i].repl_bio) |
| 3358 | r10_bio->devs[i].repl_bio->bi_end_io = NULL; |
| 3359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | bio = r10_bio->devs[i].bio; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3361 | bio->bi_status = BLK_STS_IOERR; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3362 | rcu_read_lock(); |
| 3363 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 3364 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) { |
| 3365 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | continue; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3367 | } |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3368 | sector = r10_bio->devs[i].addr; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3369 | if (is_badblock(rdev, sector, max_sync, |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3370 | &first_bad, &bad_sectors)) { |
| 3371 | if (first_bad > sector) |
| 3372 | max_sync = first_bad - sector; |
| 3373 | else { |
| 3374 | bad_sectors -= (sector - first_bad); |
| 3375 | if (max_sync > bad_sectors) |
Dan Carpenter | 91502f0 | 2012-10-11 14:20:58 +1100 | [diff] [blame] | 3376 | max_sync = bad_sectors; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3377 | rcu_read_unlock(); |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3378 | continue; |
| 3379 | } |
| 3380 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3381 | atomic_inc(&rdev->nr_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | atomic_inc(&r10_bio->remaining); |
| 3383 | bio->bi_next = biolist; |
| 3384 | biolist = bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3385 | bio->bi_end_io = end_sync_read; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3386 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
Guoqing Jiang | 1cdd125 | 2017-06-13 11:16:08 +0800 | [diff] [blame] | 3387 | if (test_bit(FailFast, &rdev->flags)) |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 3388 | bio->bi_opf |= MD_FAILFAST; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3389 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3390 | bio_set_dev(bio, rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | count++; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3392 | |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3393 | rdev = rcu_dereference(conf->mirrors[d].replacement); |
| 3394 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) { |
| 3395 | rcu_read_unlock(); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3396 | continue; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3397 | } |
| 3398 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3399 | |
| 3400 | /* Need to set up for writing to the replacement */ |
| 3401 | bio = r10_bio->devs[i].repl_bio; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3402 | bio->bi_status = BLK_STS_IOERR; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3403 | |
| 3404 | sector = r10_bio->devs[i].addr; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3405 | bio->bi_next = biolist; |
| 3406 | biolist = bio; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3407 | bio->bi_end_io = end_sync_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3408 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Guoqing Jiang | 1cdd125 | 2017-06-13 11:16:08 +0800 | [diff] [blame] | 3409 | if (test_bit(FailFast, &rdev->flags)) |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 3410 | bio->bi_opf |= MD_FAILFAST; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3411 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3412 | bio_set_dev(bio, rdev->bdev); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3413 | count++; |
Guoqing Jiang | 1cdd125 | 2017-06-13 11:16:08 +0800 | [diff] [blame] | 3414 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | } |
| 3416 | |
| 3417 | if (count < 2) { |
| 3418 | for (i=0; i<conf->copies; i++) { |
| 3419 | int d = r10_bio->devs[i].devnum; |
| 3420 | if (r10_bio->devs[i].bio->bi_end_io) |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3421 | rdev_dec_pending(conf->mirrors[d].rdev, |
| 3422 | mddev); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3423 | if (r10_bio->devs[i].repl_bio && |
| 3424 | r10_bio->devs[i].repl_bio->bi_end_io) |
| 3425 | rdev_dec_pending( |
| 3426 | conf->mirrors[d].replacement, |
| 3427 | mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | } |
| 3429 | put_buf(r10_bio); |
| 3430 | biolist = NULL; |
| 3431 | goto giveup; |
| 3432 | } |
| 3433 | } |
| 3434 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | nr_sectors = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3436 | if (sector_nr + max_sync < max_sector) |
| 3437 | max_sector = sector_nr + max_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | do { |
| 3439 | struct page *page; |
| 3440 | int len = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3441 | if (sector_nr + (len>>9) > max_sector) |
| 3442 | len = (max_sector - sector_nr) << 9; |
| 3443 | if (len == 0) |
| 3444 | break; |
| 3445 | for (bio= biolist ; bio ; bio=bio->bi_next) { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 3446 | struct resync_pages *rp = get_resync_pages(bio); |
Ming Lei | 022e510 | 2017-07-14 16:14:42 +0800 | [diff] [blame] | 3447 | page = resync_fetch_page(rp, page_idx); |
Ming Lei | c85ba14 | 2017-03-17 00:12:22 +0800 | [diff] [blame] | 3448 | /* |
| 3449 | * won't fail because the vec table is big enough |
| 3450 | * to hold all these pages |
| 3451 | */ |
| 3452 | bio_add_page(bio, page, len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3453 | } |
| 3454 | nr_sectors += len>>9; |
| 3455 | sector_nr += len>>9; |
Ming Lei | 022e510 | 2017-07-14 16:14:42 +0800 | [diff] [blame] | 3456 | } while (++page_idx < RESYNC_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3457 | r10_bio->sectors = nr_sectors; |
| 3458 | |
Guoqing Jiang | 8db8791 | 2017-10-24 15:11:52 +0800 | [diff] [blame] | 3459 | if (mddev_is_clustered(mddev) && |
| 3460 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 3461 | /* It is resync not recovery */ |
| 3462 | if (conf->cluster_sync_high < sector_nr + nr_sectors) { |
| 3463 | conf->cluster_sync_low = mddev->curr_resync_completed; |
| 3464 | raid10_set_cluster_sync_high(conf); |
| 3465 | /* Send resync message */ |
| 3466 | md_cluster_ops->resync_info_update(mddev, |
| 3467 | conf->cluster_sync_low, |
| 3468 | conf->cluster_sync_high); |
| 3469 | } |
| 3470 | } else if (mddev_is_clustered(mddev)) { |
| 3471 | /* This is recovery not resync */ |
| 3472 | sector_t sect_va1, sect_va2; |
| 3473 | bool broadcast_msg = false; |
| 3474 | |
| 3475 | for (i = 0; i < conf->geo.raid_disks; i++) { |
| 3476 | /* |
| 3477 | * sector_nr is a device address for recovery, so we |
| 3478 | * need translate it to array address before compare |
| 3479 | * with cluster_sync_high. |
| 3480 | */ |
| 3481 | sect_va1 = raid10_find_virt(conf, sector_nr, i); |
| 3482 | |
| 3483 | if (conf->cluster_sync_high < sect_va1 + nr_sectors) { |
| 3484 | broadcast_msg = true; |
| 3485 | /* |
| 3486 | * curr_resync_completed is similar as |
| 3487 | * sector_nr, so make the translation too. |
| 3488 | */ |
| 3489 | sect_va2 = raid10_find_virt(conf, |
| 3490 | mddev->curr_resync_completed, i); |
| 3491 | |
| 3492 | if (conf->cluster_sync_low == 0 || |
| 3493 | conf->cluster_sync_low > sect_va2) |
| 3494 | conf->cluster_sync_low = sect_va2; |
| 3495 | } |
| 3496 | } |
| 3497 | if (broadcast_msg) { |
| 3498 | raid10_set_cluster_sync_high(conf); |
| 3499 | md_cluster_ops->resync_info_update(mddev, |
| 3500 | conf->cluster_sync_low, |
| 3501 | conf->cluster_sync_high); |
| 3502 | } |
| 3503 | } |
| 3504 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3505 | while (biolist) { |
| 3506 | bio = biolist; |
| 3507 | biolist = biolist->bi_next; |
| 3508 | |
| 3509 | bio->bi_next = NULL; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 3510 | r10_bio = get_resync_r10bio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3511 | r10_bio->sectors = nr_sectors; |
| 3512 | |
| 3513 | if (bio->bi_end_io == end_sync_read) { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3514 | md_sync_acct_bio(bio, nr_sectors); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3515 | bio->bi_status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3516 | generic_make_request(bio); |
| 3517 | } |
| 3518 | } |
| 3519 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3520 | if (sectors_skipped) |
| 3521 | /* pretend they weren't skipped, it makes |
| 3522 | * no important difference in this case |
| 3523 | */ |
| 3524 | md_done_sync(mddev, sectors_skipped, 1); |
| 3525 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | return sectors_skipped + nr_sectors; |
| 3527 | giveup: |
| 3528 | /* There is nowhere to write, so all non-sync |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3529 | * drives must be failed or in resync, all drives |
| 3530 | * have a bad block, so try the next chunk... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3531 | */ |
NeilBrown | 09b4068 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 3532 | if (sector_nr + max_sync < max_sector) |
| 3533 | max_sector = sector_nr + max_sync; |
| 3534 | |
| 3535 | sectors_skipped += (max_sector - sector_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3536 | chunks_skipped ++; |
| 3537 | sector_nr = max_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3538 | goto skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | } |
| 3540 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3541 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3542 | raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3543 | { |
| 3544 | sector_t size; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3545 | struct r10conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3546 | |
| 3547 | if (!raid_disks) |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3548 | raid_disks = min(conf->geo.raid_disks, |
| 3549 | conf->prev.raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3550 | if (!sectors) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3551 | sectors = conf->dev_sectors; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3552 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3553 | size = sectors >> conf->geo.chunk_shift; |
| 3554 | sector_div(size, conf->geo.far_copies); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3555 | size = size * raid_disks; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3556 | sector_div(size, conf->geo.near_copies); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3557 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3558 | return size << conf->geo.chunk_shift; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3559 | } |
| 3560 | |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3561 | static void calc_sectors(struct r10conf *conf, sector_t size) |
| 3562 | { |
| 3563 | /* Calculate the number of sectors-per-device that will |
| 3564 | * actually be used, and set conf->dev_sectors and |
| 3565 | * conf->stride |
| 3566 | */ |
| 3567 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3568 | size = size >> conf->geo.chunk_shift; |
| 3569 | sector_div(size, conf->geo.far_copies); |
| 3570 | size = size * conf->geo.raid_disks; |
| 3571 | sector_div(size, conf->geo.near_copies); |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3572 | /* 'size' is now the number of chunks in the array */ |
| 3573 | /* calculate "used chunks per device" */ |
| 3574 | size = size * conf->copies; |
| 3575 | |
| 3576 | /* We need to round up when dividing by raid_disks to |
| 3577 | * get the stride size. |
| 3578 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3579 | size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks); |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3580 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3581 | conf->dev_sectors = size << conf->geo.chunk_shift; |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3582 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3583 | if (conf->geo.far_offset) |
| 3584 | conf->geo.stride = 1 << conf->geo.chunk_shift; |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3585 | else { |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3586 | sector_div(size, conf->geo.far_copies); |
| 3587 | conf->geo.stride = size << conf->geo.chunk_shift; |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3588 | } |
| 3589 | } |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3590 | |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3591 | enum geo_type {geo_new, geo_old, geo_start}; |
| 3592 | static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new) |
| 3593 | { |
| 3594 | int nc, fc, fo; |
| 3595 | int layout, chunk, disks; |
| 3596 | switch (new) { |
| 3597 | case geo_old: |
| 3598 | layout = mddev->layout; |
| 3599 | chunk = mddev->chunk_sectors; |
| 3600 | disks = mddev->raid_disks - mddev->delta_disks; |
| 3601 | break; |
| 3602 | case geo_new: |
| 3603 | layout = mddev->new_layout; |
| 3604 | chunk = mddev->new_chunk_sectors; |
| 3605 | disks = mddev->raid_disks; |
| 3606 | break; |
| 3607 | default: /* avoid 'may be unused' warnings */ |
| 3608 | case geo_start: /* new when starting reshape - raid_disks not |
| 3609 | * updated yet. */ |
| 3610 | layout = mddev->new_layout; |
| 3611 | chunk = mddev->new_chunk_sectors; |
| 3612 | disks = mddev->raid_disks + mddev->delta_disks; |
| 3613 | break; |
| 3614 | } |
NeilBrown | 8bce6d3 | 2015-10-22 13:20:15 +1100 | [diff] [blame] | 3615 | if (layout >> 19) |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3616 | return -1; |
| 3617 | if (chunk < (PAGE_SIZE >> 9) || |
| 3618 | !is_power_of_2(chunk)) |
| 3619 | return -2; |
| 3620 | nc = layout & 255; |
| 3621 | fc = (layout >> 8) & 255; |
| 3622 | fo = layout & (1<<16); |
| 3623 | geo->raid_disks = disks; |
| 3624 | geo->near_copies = nc; |
| 3625 | geo->far_copies = fc; |
| 3626 | geo->far_offset = fo; |
NeilBrown | 8bce6d3 | 2015-10-22 13:20:15 +1100 | [diff] [blame] | 3627 | switch (layout >> 17) { |
| 3628 | case 0: /* original layout. simple but not always optimal */ |
| 3629 | geo->far_set_size = disks; |
| 3630 | break; |
| 3631 | case 1: /* "improved" layout which was buggy. Hopefully no-one is |
| 3632 | * actually using this, but leave code here just in case.*/ |
| 3633 | geo->far_set_size = disks/fc; |
| 3634 | WARN(geo->far_set_size < fc, |
| 3635 | "This RAID10 layout does not provide data safety - please backup and create new array\n"); |
| 3636 | break; |
| 3637 | case 2: /* "improved" layout fixed to match documentation */ |
| 3638 | geo->far_set_size = fc * nc; |
| 3639 | break; |
| 3640 | default: /* Not a valid layout */ |
| 3641 | return -1; |
| 3642 | } |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3643 | geo->chunk_mask = chunk - 1; |
| 3644 | geo->chunk_shift = ffz(~chunk); |
| 3645 | return nc*fc; |
| 3646 | } |
| 3647 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3648 | static struct r10conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3649 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3650 | struct r10conf *conf = NULL; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3651 | int err = -EINVAL; |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3652 | struct geom geo; |
| 3653 | int copies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3654 | |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3655 | copies = setup_geo(&geo, mddev, geo_new); |
| 3656 | |
| 3657 | if (copies == -2) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3658 | pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n", |
| 3659 | mdname(mddev), PAGE_SIZE); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3660 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3661 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3662 | |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3663 | if (copies < 2 || copies > mddev->raid_disks) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3664 | pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n", |
| 3665 | mdname(mddev), mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3666 | goto out; |
| 3667 | } |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3668 | |
| 3669 | err = -ENOMEM; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3670 | conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3671 | if (!conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3672 | goto out; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3673 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3674 | /* FIXME calc properly */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 3675 | conf->mirrors = kcalloc(mddev->raid_disks + max(0, -mddev->delta_disks), |
| 3676 | sizeof(struct raid10_info), |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3677 | GFP_KERNEL); |
| 3678 | if (!conf->mirrors) |
| 3679 | goto out; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 3680 | |
| 3681 | conf->tmppage = alloc_page(GFP_KERNEL); |
| 3682 | if (!conf->tmppage) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3683 | goto out; |
| 3684 | |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3685 | conf->geo = geo; |
| 3686 | conf->copies = copies; |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 3687 | err = mempool_init(&conf->r10bio_pool, NR_RAID10_BIOS, r10bio_pool_alloc, |
| 3688 | r10bio_pool_free, conf); |
| 3689 | if (err) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3690 | goto out; |
| 3691 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 3692 | err = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0); |
| 3693 | if (err) |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 3694 | goto out; |
| 3695 | |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3696 | calc_sectors(conf, mddev->dev_sectors); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3697 | if (mddev->reshape_position == MaxSector) { |
| 3698 | conf->prev = conf->geo; |
| 3699 | conf->reshape_progress = MaxSector; |
| 3700 | } else { |
| 3701 | if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) { |
| 3702 | err = -EINVAL; |
| 3703 | goto out; |
| 3704 | } |
| 3705 | conf->reshape_progress = mddev->reshape_position; |
| 3706 | if (conf->prev.far_offset) |
| 3707 | conf->prev.stride = 1 << conf->prev.chunk_shift; |
| 3708 | else |
| 3709 | /* far_copies must be 1 */ |
| 3710 | conf->prev.stride = conf->dev_sectors; |
| 3711 | } |
NeilBrown | 299b068 | 2015-07-06 17:37:49 +1000 | [diff] [blame] | 3712 | conf->reshape_safe = conf->reshape_progress; |
Neil Brown | e7e72bf | 2008-05-14 16:05:54 -0700 | [diff] [blame] | 3713 | spin_lock_init(&conf->device_lock); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3714 | INIT_LIST_HEAD(&conf->retry_list); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 3715 | INIT_LIST_HEAD(&conf->bio_end_io_list); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3716 | |
| 3717 | spin_lock_init(&conf->resync_lock); |
| 3718 | init_waitqueue_head(&conf->wait_barrier); |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 3719 | atomic_set(&conf->nr_pending, 0); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3720 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 3721 | err = -ENOMEM; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 3722 | conf->thread = md_register_thread(raid10d, mddev, "raid10"); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3723 | if (!conf->thread) |
| 3724 | goto out; |
| 3725 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3726 | conf->mddev = mddev; |
| 3727 | return conf; |
| 3728 | |
| 3729 | out: |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3730 | if (conf) { |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 3731 | mempool_exit(&conf->r10bio_pool); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3732 | kfree(conf->mirrors); |
| 3733 | safe_put_page(conf->tmppage); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 3734 | bioset_exit(&conf->bio_split); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3735 | kfree(conf); |
| 3736 | } |
| 3737 | return ERR_PTR(err); |
| 3738 | } |
| 3739 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 3740 | static int raid10_run(struct mddev *mddev) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3741 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3742 | struct r10conf *conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3743 | int i, disk_idx, chunk_size; |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 3744 | struct raid10_info *disk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3745 | struct md_rdev *rdev; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3746 | sector_t size; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3747 | sector_t min_offset_diff = 0; |
| 3748 | int first = 1; |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 3749 | bool discard_supported = false; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3750 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 3751 | if (mddev_init_writes_pending(mddev) < 0) |
| 3752 | return -ENOMEM; |
| 3753 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3754 | if (mddev->private == NULL) { |
| 3755 | conf = setup_conf(mddev); |
| 3756 | if (IS_ERR(conf)) |
| 3757 | return PTR_ERR(conf); |
| 3758 | mddev->private = conf; |
| 3759 | } |
| 3760 | conf = mddev->private; |
| 3761 | if (!conf) |
| 3762 | goto out; |
| 3763 | |
Guoqing Jiang | 8db8791 | 2017-10-24 15:11:52 +0800 | [diff] [blame] | 3764 | if (mddev_is_clustered(conf->mddev)) { |
| 3765 | int fc, fo; |
| 3766 | |
| 3767 | fc = (mddev->layout >> 8) & 255; |
| 3768 | fo = mddev->layout & (1<<16); |
| 3769 | if (fc > 1 || fo > 0) { |
| 3770 | pr_err("only near layout is supported by clustered" |
| 3771 | " raid10\n"); |
Lidong Zhong | 43a5212 | 2018-01-23 23:06:12 +0800 | [diff] [blame] | 3772 | goto out_free_conf; |
Guoqing Jiang | 8db8791 | 2017-10-24 15:11:52 +0800 | [diff] [blame] | 3773 | } |
| 3774 | } |
| 3775 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3776 | mddev->thread = conf->thread; |
| 3777 | conf->thread = NULL; |
| 3778 | |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 3779 | chunk_size = mddev->chunk_sectors << 9; |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3780 | if (mddev->queue) { |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 3781 | blk_queue_max_discard_sectors(mddev->queue, |
| 3782 | mddev->chunk_sectors); |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 3783 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
Christoph Hellwig | 3deff1a | 2017-04-05 19:21:03 +0200 | [diff] [blame] | 3784 | blk_queue_max_write_zeroes_sectors(mddev->queue, 0); |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3785 | blk_queue_io_min(mddev->queue, chunk_size); |
| 3786 | if (conf->geo.raid_disks % conf->geo.near_copies) |
| 3787 | blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks); |
| 3788 | else |
| 3789 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 3790 | (conf->geo.raid_disks / conf->geo.near_copies)); |
| 3791 | } |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 3792 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3793 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3794 | long long diff; |
NeilBrown | 34b343c | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3795 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3796 | disk_idx = rdev->raid_disk; |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3797 | if (disk_idx < 0) |
| 3798 | continue; |
| 3799 | if (disk_idx >= conf->geo.raid_disks && |
| 3800 | disk_idx >= conf->prev.raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3801 | continue; |
| 3802 | disk = conf->mirrors + disk_idx; |
| 3803 | |
NeilBrown | 56a2559b | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3804 | if (test_bit(Replacement, &rdev->flags)) { |
| 3805 | if (disk->replacement) |
| 3806 | goto out_free_conf; |
| 3807 | disk->replacement = rdev; |
| 3808 | } else { |
| 3809 | if (disk->rdev) |
| 3810 | goto out_free_conf; |
| 3811 | disk->rdev = rdev; |
| 3812 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3813 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 3814 | if (!mddev->reshape_backwards) |
| 3815 | diff = -diff; |
| 3816 | if (diff < 0) |
| 3817 | diff = 0; |
| 3818 | if (first || diff < min_offset_diff) |
| 3819 | min_offset_diff = diff; |
NeilBrown | 56a2559b | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3820 | |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3821 | if (mddev->gendisk) |
| 3822 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 3823 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3824 | |
| 3825 | disk->head_position = 0; |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 3826 | |
| 3827 | if (blk_queue_discard(bdev_get_queue(rdev->bdev))) |
| 3828 | discard_supported = true; |
Guoqing Jiang | 6f287ca | 2017-04-06 09:12:18 +0800 | [diff] [blame] | 3829 | first = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3830 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3831 | |
Jonathan Brassow | ed30be0 | 2012-10-31 11:42:30 +1100 | [diff] [blame] | 3832 | if (mddev->queue) { |
| 3833 | if (discard_supported) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 3834 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, |
Jonathan Brassow | ed30be0 | 2012-10-31 11:42:30 +1100 | [diff] [blame] | 3835 | mddev->queue); |
| 3836 | else |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 3837 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, |
Jonathan Brassow | ed30be0 | 2012-10-31 11:42:30 +1100 | [diff] [blame] | 3838 | mddev->queue); |
| 3839 | } |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 3840 | /* need to check that every block has at least one working mirror */ |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3841 | if (!enough(conf, -1)) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3842 | pr_err("md/raid10:%s: not enough operational mirrors.\n", |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 3843 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | goto out_free_conf; |
| 3845 | } |
| 3846 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3847 | if (conf->reshape_progress != MaxSector) { |
| 3848 | /* must ensure that shape change is supported */ |
| 3849 | if (conf->geo.far_copies != 1 && |
| 3850 | conf->geo.far_offset == 0) |
| 3851 | goto out_free_conf; |
| 3852 | if (conf->prev.far_copies != 1 && |
NeilBrown | 78eaa0d | 2013-07-02 15:58:05 +1000 | [diff] [blame] | 3853 | conf->prev.far_offset == 0) |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3854 | goto out_free_conf; |
| 3855 | } |
| 3856 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3857 | mddev->degraded = 0; |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3858 | for (i = 0; |
| 3859 | i < conf->geo.raid_disks |
| 3860 | || i < conf->prev.raid_disks; |
| 3861 | i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3862 | |
| 3863 | disk = conf->mirrors + i; |
| 3864 | |
NeilBrown | 56a2559b | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3865 | if (!disk->rdev && disk->replacement) { |
| 3866 | /* The replacement is all we have - use it */ |
| 3867 | disk->rdev = disk->replacement; |
| 3868 | disk->replacement = NULL; |
| 3869 | clear_bit(Replacement, &disk->rdev->flags); |
| 3870 | } |
| 3871 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3872 | if (!disk->rdev || |
NeilBrown | 2e333e8 | 2006-10-21 10:24:07 -0700 | [diff] [blame] | 3873 | !test_bit(In_sync, &disk->rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3874 | disk->head_position = 0; |
| 3875 | mddev->degraded++; |
NeilBrown | 0b59bb6 | 2014-01-14 16:30:10 +1100 | [diff] [blame] | 3876 | if (disk->rdev && |
| 3877 | disk->rdev->saved_raid_disk < 0) |
Neil Brown | 8c2e870 | 2008-06-28 08:30:52 +1000 | [diff] [blame] | 3878 | conf->fullsync = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3879 | } |
BingJing Chang | bda3153 | 2018-06-28 18:40:11 +0800 | [diff] [blame] | 3880 | |
| 3881 | if (disk->replacement && |
| 3882 | !test_bit(In_sync, &disk->replacement->flags) && |
| 3883 | disk->replacement->saved_raid_disk < 0) { |
| 3884 | conf->fullsync = 1; |
| 3885 | } |
| 3886 | |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 3887 | disk->recovery_disabled = mddev->recovery_disabled - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3888 | } |
| 3889 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 3890 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3891 | pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n", |
| 3892 | mdname(mddev)); |
| 3893 | pr_info("md/raid10:%s: active with %d out of %d devices\n", |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3894 | mdname(mddev), conf->geo.raid_disks - mddev->degraded, |
| 3895 | conf->geo.raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3896 | /* |
| 3897 | * Ok, everything is just fine now |
| 3898 | */ |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3899 | mddev->dev_sectors = conf->dev_sectors; |
| 3900 | size = raid10_size(mddev, 0, 0); |
| 3901 | md_set_array_sectors(mddev, size); |
| 3902 | mddev->resync_max_sectors = size; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3903 | set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3904 | |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3905 | if (mddev->queue) { |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3906 | int stripe = conf->geo.raid_disks * |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3907 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3908 | |
| 3909 | /* Calculate max read-ahead size. |
| 3910 | * We need to readahead at least twice a whole stripe.... |
| 3911 | * maybe... |
| 3912 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3913 | stripe /= conf->geo.near_copies; |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 3914 | if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
| 3915 | mddev->queue->backing_dev_info->ra_pages = 2 * stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3916 | } |
| 3917 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 3918 | if (md_integrity_register(mddev)) |
| 3919 | goto out_free_conf; |
| 3920 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3921 | if (conf->reshape_progress != MaxSector) { |
| 3922 | unsigned long before_length, after_length; |
| 3923 | |
| 3924 | before_length = ((1 << conf->prev.chunk_shift) * |
| 3925 | conf->prev.far_copies); |
| 3926 | after_length = ((1 << conf->geo.chunk_shift) * |
| 3927 | conf->geo.far_copies); |
| 3928 | |
| 3929 | if (max(before_length, after_length) > min_offset_diff) { |
| 3930 | /* This cannot work */ |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3931 | pr_warn("md/raid10: offset difference not enough to continue reshape\n"); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3932 | goto out_free_conf; |
| 3933 | } |
| 3934 | conf->offset_diff = min_offset_diff; |
| 3935 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3936 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 3937 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 3938 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 3939 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3940 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 3941 | "reshape"); |
| 3942 | } |
| 3943 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3944 | return 0; |
| 3945 | |
| 3946 | out_free_conf: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 3947 | md_unregister_thread(&mddev->thread); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 3948 | mempool_exit(&conf->r10bio_pool); |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 3949 | safe_put_page(conf->tmppage); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 3950 | kfree(conf->mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3951 | kfree(conf); |
| 3952 | mddev->private = NULL; |
| 3953 | out: |
| 3954 | return -EIO; |
| 3955 | } |
| 3956 | |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3957 | static void raid10_free(struct mddev *mddev, void *priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3958 | { |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3959 | struct r10conf *conf = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3960 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 3961 | mempool_exit(&conf->r10bio_pool); |
Hirokazu Takahashi | 0fea7ed | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3962 | safe_put_page(conf->tmppage); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 3963 | kfree(conf->mirrors); |
NeilBrown | c4796e2 | 2014-08-23 20:19:26 +1000 | [diff] [blame] | 3964 | kfree(conf->mirrors_old); |
| 3965 | kfree(conf->mirrors_new); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 3966 | bioset_exit(&conf->bio_split); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3967 | kfree(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3968 | } |
| 3969 | |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 3970 | static void raid10_quiesce(struct mddev *mddev, int quiesce) |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3971 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3972 | struct r10conf *conf = mddev->private; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3973 | |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 3974 | if (quiesce) |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3975 | raise_barrier(conf, 0); |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 3976 | else |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3977 | lower_barrier(conf); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3978 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3979 | |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 3980 | static int raid10_resize(struct mddev *mddev, sector_t sectors) |
| 3981 | { |
| 3982 | /* Resize of 'far' arrays is not supported. |
| 3983 | * For 'near' and 'offset' arrays we can set the |
| 3984 | * number of sectors used to be an appropriate multiple |
| 3985 | * of the chunk size. |
| 3986 | * For 'offset', this is far_copies*chunksize. |
| 3987 | * For 'near' the multiplier is the LCM of |
| 3988 | * near_copies and raid_disks. |
| 3989 | * So if far_copies > 1 && !far_offset, fail. |
| 3990 | * Else find LCM(raid_disks, near_copy)*far_copies and |
| 3991 | * multiply by chunk_size. Then round to this number. |
| 3992 | * This is mostly done by raid10_size() |
| 3993 | */ |
| 3994 | struct r10conf *conf = mddev->private; |
| 3995 | sector_t oldsize, size; |
| 3996 | |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3997 | if (mddev->reshape_position != MaxSector) |
| 3998 | return -EBUSY; |
| 3999 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 4000 | if (conf->geo.far_copies > 1 && !conf->geo.far_offset) |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 4001 | return -EINVAL; |
| 4002 | |
| 4003 | oldsize = raid10_size(mddev, 0, 0); |
| 4004 | size = raid10_size(mddev, sectors, 0); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 4005 | if (mddev->external_size && |
| 4006 | mddev->array_sectors > size) |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 4007 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 4008 | if (mddev->bitmap) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4009 | int ret = md_bitmap_resize(mddev->bitmap, size, 0, 0); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 4010 | if (ret) |
| 4011 | return ret; |
| 4012 | } |
| 4013 | md_set_array_sectors(mddev, size); |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 4014 | if (sectors > mddev->dev_sectors && |
| 4015 | mddev->recovery_cp > oldsize) { |
| 4016 | mddev->recovery_cp = oldsize; |
| 4017 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4018 | } |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 4019 | calc_sectors(conf, sectors); |
| 4020 | mddev->dev_sectors = conf->dev_sectors; |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 4021 | mddev->resync_max_sectors = size; |
| 4022 | return 0; |
| 4023 | } |
| 4024 | |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 4025 | static void *raid10_takeover_raid0(struct mddev *mddev, sector_t size, int devs) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4026 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4027 | struct md_rdev *rdev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 4028 | struct r10conf *conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4029 | |
| 4030 | if (mddev->degraded > 0) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4031 | pr_warn("md/raid10:%s: Error: degraded raid0!\n", |
| 4032 | mdname(mddev)); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4033 | return ERR_PTR(-EINVAL); |
| 4034 | } |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 4035 | sector_div(size, devs); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4036 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4037 | /* Set new parameters */ |
| 4038 | mddev->new_level = 10; |
| 4039 | /* new layout: far_copies = 1, near_copies = 2 */ |
| 4040 | mddev->new_layout = (1<<8) + 2; |
| 4041 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 4042 | mddev->delta_disks = mddev->raid_disks; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4043 | mddev->raid_disks *= 2; |
| 4044 | /* make sure it will be not marked as dirty */ |
| 4045 | mddev->recovery_cp = MaxSector; |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 4046 | mddev->dev_sectors = size; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4047 | |
| 4048 | conf = setup_conf(mddev); |
Krzysztof Wojcik | 02214dc | 2011-02-04 14:18:26 +0100 | [diff] [blame] | 4049 | if (!IS_ERR(conf)) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 4050 | rdev_for_each(rdev, mddev) |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 4051 | if (rdev->raid_disk >= 0) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 4052 | rdev->new_raid_disk = rdev->raid_disk * 2; |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 4053 | rdev->sectors = size; |
| 4054 | } |
Krzysztof Wojcik | 02214dc | 2011-02-04 14:18:26 +0100 | [diff] [blame] | 4055 | conf->barrier = 1; |
| 4056 | } |
| 4057 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4058 | return conf; |
| 4059 | } |
| 4060 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4061 | static void *raid10_takeover(struct mddev *mddev) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4062 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 4063 | struct r0conf *raid0_conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4064 | |
| 4065 | /* raid10 can take over: |
| 4066 | * raid0 - providing it has only two drives |
| 4067 | */ |
| 4068 | if (mddev->level == 0) { |
| 4069 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 4070 | raid0_conf = mddev->private; |
| 4071 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4072 | pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n", |
| 4073 | mdname(mddev)); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4074 | return ERR_PTR(-EINVAL); |
| 4075 | } |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 4076 | return raid10_takeover_raid0(mddev, |
| 4077 | raid0_conf->strip_zone->zone_end, |
| 4078 | raid0_conf->strip_zone->nb_dev); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4079 | } |
| 4080 | return ERR_PTR(-EINVAL); |
| 4081 | } |
| 4082 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4083 | static int raid10_check_reshape(struct mddev *mddev) |
| 4084 | { |
| 4085 | /* Called when there is a request to change |
| 4086 | * - layout (to ->new_layout) |
| 4087 | * - chunk size (to ->new_chunk_sectors) |
| 4088 | * - raid_disks (by delta_disks) |
| 4089 | * or when trying to restart a reshape that was ongoing. |
| 4090 | * |
| 4091 | * We need to validate the request and possibly allocate |
| 4092 | * space if that might be an issue later. |
| 4093 | * |
| 4094 | * Currently we reject any reshape of a 'far' mode array, |
| 4095 | * allow chunk size to change if new is generally acceptable, |
| 4096 | * allow raid_disks to increase, and allow |
| 4097 | * a switch between 'near' mode and 'offset' mode. |
| 4098 | */ |
| 4099 | struct r10conf *conf = mddev->private; |
| 4100 | struct geom geo; |
| 4101 | |
| 4102 | if (conf->geo.far_copies != 1 && !conf->geo.far_offset) |
| 4103 | return -EINVAL; |
| 4104 | |
| 4105 | if (setup_geo(&geo, mddev, geo_start) != conf->copies) |
| 4106 | /* mustn't change number of copies */ |
| 4107 | return -EINVAL; |
| 4108 | if (geo.far_copies > 1 && !geo.far_offset) |
| 4109 | /* Cannot switch to 'far' mode */ |
| 4110 | return -EINVAL; |
| 4111 | |
| 4112 | if (mddev->array_sectors & geo.chunk_mask) |
| 4113 | /* not factor of array size */ |
| 4114 | return -EINVAL; |
| 4115 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4116 | if (!enough(conf, -1)) |
| 4117 | return -EINVAL; |
| 4118 | |
| 4119 | kfree(conf->mirrors_new); |
| 4120 | conf->mirrors_new = NULL; |
| 4121 | if (mddev->delta_disks > 0) { |
| 4122 | /* allocate new 'mirrors' list */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 4123 | conf->mirrors_new = |
| 4124 | kcalloc(mddev->raid_disks + mddev->delta_disks, |
| 4125 | sizeof(struct raid10_info), |
| 4126 | GFP_KERNEL); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4127 | if (!conf->mirrors_new) |
| 4128 | return -ENOMEM; |
| 4129 | } |
| 4130 | return 0; |
| 4131 | } |
| 4132 | |
| 4133 | /* |
| 4134 | * Need to check if array has failed when deciding whether to: |
| 4135 | * - start an array |
| 4136 | * - remove non-faulty devices |
| 4137 | * - add a spare |
| 4138 | * - allow a reshape |
| 4139 | * This determination is simple when no reshape is happening. |
| 4140 | * However if there is a reshape, we need to carefully check |
| 4141 | * both the before and after sections. |
| 4142 | * This is because some failed devices may only affect one |
| 4143 | * of the two sections, and some non-in_sync devices may |
| 4144 | * be insync in the section most affected by failed devices. |
| 4145 | */ |
| 4146 | static int calc_degraded(struct r10conf *conf) |
| 4147 | { |
| 4148 | int degraded, degraded2; |
| 4149 | int i; |
| 4150 | |
| 4151 | rcu_read_lock(); |
| 4152 | degraded = 0; |
| 4153 | /* 'prev' section first */ |
| 4154 | for (i = 0; i < conf->prev.raid_disks; i++) { |
| 4155 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 4156 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 4157 | degraded++; |
| 4158 | else if (!test_bit(In_sync, &rdev->flags)) |
| 4159 | /* When we can reduce the number of devices in |
| 4160 | * an array, this might not contribute to |
| 4161 | * 'degraded'. It does now. |
| 4162 | */ |
| 4163 | degraded++; |
| 4164 | } |
| 4165 | rcu_read_unlock(); |
| 4166 | if (conf->geo.raid_disks == conf->prev.raid_disks) |
| 4167 | return degraded; |
| 4168 | rcu_read_lock(); |
| 4169 | degraded2 = 0; |
| 4170 | for (i = 0; i < conf->geo.raid_disks; i++) { |
| 4171 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 4172 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 4173 | degraded2++; |
| 4174 | else if (!test_bit(In_sync, &rdev->flags)) { |
| 4175 | /* If reshape is increasing the number of devices, |
| 4176 | * this section has already been recovered, so |
| 4177 | * it doesn't contribute to degraded. |
| 4178 | * else it does. |
| 4179 | */ |
| 4180 | if (conf->geo.raid_disks <= conf->prev.raid_disks) |
| 4181 | degraded2++; |
| 4182 | } |
| 4183 | } |
| 4184 | rcu_read_unlock(); |
| 4185 | if (degraded2 > degraded) |
| 4186 | return degraded2; |
| 4187 | return degraded; |
| 4188 | } |
| 4189 | |
| 4190 | static int raid10_start_reshape(struct mddev *mddev) |
| 4191 | { |
| 4192 | /* A 'reshape' has been requested. This commits |
| 4193 | * the various 'new' fields and sets MD_RECOVER_RESHAPE |
| 4194 | * This also checks if there are enough spares and adds them |
| 4195 | * to the array. |
| 4196 | * We currently require enough spares to make the final |
| 4197 | * array non-degraded. We also require that the difference |
| 4198 | * between old and new data_offset - on each device - is |
| 4199 | * enough that we never risk over-writing. |
| 4200 | */ |
| 4201 | |
| 4202 | unsigned long before_length, after_length; |
| 4203 | sector_t min_offset_diff = 0; |
| 4204 | int first = 1; |
| 4205 | struct geom new; |
| 4206 | struct r10conf *conf = mddev->private; |
| 4207 | struct md_rdev *rdev; |
| 4208 | int spares = 0; |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4209 | int ret; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4210 | |
| 4211 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4212 | return -EBUSY; |
| 4213 | |
| 4214 | if (setup_geo(&new, mddev, geo_start) != conf->copies) |
| 4215 | return -EINVAL; |
| 4216 | |
| 4217 | before_length = ((1 << conf->prev.chunk_shift) * |
| 4218 | conf->prev.far_copies); |
| 4219 | after_length = ((1 << conf->geo.chunk_shift) * |
| 4220 | conf->geo.far_copies); |
| 4221 | |
| 4222 | rdev_for_each(rdev, mddev) { |
| 4223 | if (!test_bit(In_sync, &rdev->flags) |
| 4224 | && !test_bit(Faulty, &rdev->flags)) |
| 4225 | spares++; |
| 4226 | if (rdev->raid_disk >= 0) { |
| 4227 | long long diff = (rdev->new_data_offset |
| 4228 | - rdev->data_offset); |
| 4229 | if (!mddev->reshape_backwards) |
| 4230 | diff = -diff; |
| 4231 | if (diff < 0) |
| 4232 | diff = 0; |
| 4233 | if (first || diff < min_offset_diff) |
| 4234 | min_offset_diff = diff; |
Shaohua Li | b506335 | 2017-05-01 12:15:07 -0700 | [diff] [blame] | 4235 | first = 0; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4236 | } |
| 4237 | } |
| 4238 | |
| 4239 | if (max(before_length, after_length) > min_offset_diff) |
| 4240 | return -EINVAL; |
| 4241 | |
| 4242 | if (spares < mddev->delta_disks) |
| 4243 | return -EINVAL; |
| 4244 | |
| 4245 | conf->offset_diff = min_offset_diff; |
| 4246 | spin_lock_irq(&conf->device_lock); |
| 4247 | if (conf->mirrors_new) { |
| 4248 | memcpy(conf->mirrors_new, conf->mirrors, |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 4249 | sizeof(struct raid10_info)*conf->prev.raid_disks); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4250 | smp_mb(); |
NeilBrown | c4796e2 | 2014-08-23 20:19:26 +1000 | [diff] [blame] | 4251 | kfree(conf->mirrors_old); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4252 | conf->mirrors_old = conf->mirrors; |
| 4253 | conf->mirrors = conf->mirrors_new; |
| 4254 | conf->mirrors_new = NULL; |
| 4255 | } |
| 4256 | setup_geo(&conf->geo, mddev, geo_start); |
| 4257 | smp_mb(); |
| 4258 | if (mddev->reshape_backwards) { |
| 4259 | sector_t size = raid10_size(mddev, 0, 0); |
| 4260 | if (size < mddev->array_sectors) { |
| 4261 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4262 | pr_warn("md/raid10:%s: array size must be reduce before number of disks\n", |
| 4263 | mdname(mddev)); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4264 | return -EINVAL; |
| 4265 | } |
| 4266 | mddev->resync_max_sectors = size; |
| 4267 | conf->reshape_progress = size; |
| 4268 | } else |
| 4269 | conf->reshape_progress = 0; |
NeilBrown | 299b068 | 2015-07-06 17:37:49 +1000 | [diff] [blame] | 4270 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4271 | spin_unlock_irq(&conf->device_lock); |
| 4272 | |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4273 | if (mddev->delta_disks && mddev->bitmap) { |
Guoqing Jiang | afd7562 | 2018-10-18 16:37:41 +0800 | [diff] [blame] | 4274 | struct mdp_superblock_1 *sb = NULL; |
| 4275 | sector_t oldsize, newsize; |
| 4276 | |
| 4277 | oldsize = raid10_size(mddev, 0, 0); |
| 4278 | newsize = raid10_size(mddev, 0, conf->geo.raid_disks); |
| 4279 | |
| 4280 | if (!mddev_is_clustered(mddev)) { |
| 4281 | ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0); |
| 4282 | if (ret) |
| 4283 | goto abort; |
| 4284 | else |
| 4285 | goto out; |
| 4286 | } |
| 4287 | |
| 4288 | rdev_for_each(rdev, mddev) { |
| 4289 | if (rdev->raid_disk > -1 && |
| 4290 | !test_bit(Faulty, &rdev->flags)) |
| 4291 | sb = page_address(rdev->sb_page); |
| 4292 | } |
| 4293 | |
| 4294 | /* |
| 4295 | * some node is already performing reshape, and no need to |
| 4296 | * call md_bitmap_resize again since it should be called when |
| 4297 | * receiving BITMAP_RESIZE msg |
| 4298 | */ |
| 4299 | if ((sb && (le32_to_cpu(sb->feature_map) & |
| 4300 | MD_FEATURE_RESHAPE_ACTIVE)) || (oldsize == newsize)) |
| 4301 | goto out; |
| 4302 | |
| 4303 | ret = md_bitmap_resize(mddev->bitmap, newsize, 0, 0); |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4304 | if (ret) |
| 4305 | goto abort; |
Guoqing Jiang | afd7562 | 2018-10-18 16:37:41 +0800 | [diff] [blame] | 4306 | |
| 4307 | ret = md_cluster_ops->resize_bitmaps(mddev, newsize, oldsize); |
| 4308 | if (ret) { |
| 4309 | md_bitmap_resize(mddev->bitmap, oldsize, 0, 0); |
| 4310 | goto abort; |
| 4311 | } |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4312 | } |
Guoqing Jiang | afd7562 | 2018-10-18 16:37:41 +0800 | [diff] [blame] | 4313 | out: |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4314 | if (mddev->delta_disks > 0) { |
| 4315 | rdev_for_each(rdev, mddev) |
| 4316 | if (rdev->raid_disk < 0 && |
| 4317 | !test_bit(Faulty, &rdev->flags)) { |
| 4318 | if (raid10_add_disk(mddev, rdev) == 0) { |
| 4319 | if (rdev->raid_disk >= |
| 4320 | conf->prev.raid_disks) |
| 4321 | set_bit(In_sync, &rdev->flags); |
| 4322 | else |
| 4323 | rdev->recovery_offset = 0; |
| 4324 | |
| 4325 | if (sysfs_link_rdev(mddev, rdev)) |
| 4326 | /* Failure here is OK */; |
| 4327 | } |
| 4328 | } else if (rdev->raid_disk >= conf->prev.raid_disks |
| 4329 | && !test_bit(Faulty, &rdev->flags)) { |
| 4330 | /* This is a spare that was manually added */ |
| 4331 | set_bit(In_sync, &rdev->flags); |
| 4332 | } |
| 4333 | } |
| 4334 | /* When a reshape changes the number of devices, |
| 4335 | * ->degraded is measured against the larger of the |
| 4336 | * pre and post numbers. |
| 4337 | */ |
| 4338 | spin_lock_irq(&conf->device_lock); |
| 4339 | mddev->degraded = calc_degraded(conf); |
| 4340 | spin_unlock_irq(&conf->device_lock); |
| 4341 | mddev->raid_disks = conf->geo.raid_disks; |
| 4342 | mddev->reshape_position = conf->reshape_progress; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4343 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4344 | |
| 4345 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4346 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 4347 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4348 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 4349 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4350 | |
| 4351 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 4352 | "reshape"); |
| 4353 | if (!mddev->sync_thread) { |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4354 | ret = -EAGAIN; |
| 4355 | goto abort; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4356 | } |
| 4357 | conf->reshape_checkpoint = jiffies; |
| 4358 | md_wakeup_thread(mddev->sync_thread); |
| 4359 | md_new_event(mddev); |
| 4360 | return 0; |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4361 | |
| 4362 | abort: |
| 4363 | mddev->recovery = 0; |
| 4364 | spin_lock_irq(&conf->device_lock); |
| 4365 | conf->geo = conf->prev; |
| 4366 | mddev->raid_disks = conf->geo.raid_disks; |
| 4367 | rdev_for_each(rdev, mddev) |
| 4368 | rdev->new_data_offset = rdev->data_offset; |
| 4369 | smp_wmb(); |
| 4370 | conf->reshape_progress = MaxSector; |
NeilBrown | 299b068 | 2015-07-06 17:37:49 +1000 | [diff] [blame] | 4371 | conf->reshape_safe = MaxSector; |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4372 | mddev->reshape_position = MaxSector; |
| 4373 | spin_unlock_irq(&conf->device_lock); |
| 4374 | return ret; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | /* Calculate the last device-address that could contain |
| 4378 | * any block from the chunk that includes the array-address 's' |
| 4379 | * and report the next address. |
| 4380 | * i.e. the address returned will be chunk-aligned and after |
| 4381 | * any data that is in the chunk containing 's'. |
| 4382 | */ |
| 4383 | static sector_t last_dev_address(sector_t s, struct geom *geo) |
| 4384 | { |
| 4385 | s = (s | geo->chunk_mask) + 1; |
| 4386 | s >>= geo->chunk_shift; |
| 4387 | s *= geo->near_copies; |
| 4388 | s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks); |
| 4389 | s *= geo->far_copies; |
| 4390 | s <<= geo->chunk_shift; |
| 4391 | return s; |
| 4392 | } |
| 4393 | |
| 4394 | /* Calculate the first device-address that could contain |
| 4395 | * any block from the chunk that includes the array-address 's'. |
| 4396 | * This too will be the start of a chunk |
| 4397 | */ |
| 4398 | static sector_t first_dev_address(sector_t s, struct geom *geo) |
| 4399 | { |
| 4400 | s >>= geo->chunk_shift; |
| 4401 | s *= geo->near_copies; |
| 4402 | sector_div(s, geo->raid_disks); |
| 4403 | s *= geo->far_copies; |
| 4404 | s <<= geo->chunk_shift; |
| 4405 | return s; |
| 4406 | } |
| 4407 | |
| 4408 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, |
| 4409 | int *skipped) |
| 4410 | { |
| 4411 | /* We simply copy at most one chunk (smallest of old and new) |
| 4412 | * at a time, possibly less if that exceeds RESYNC_PAGES, |
| 4413 | * or we hit a bad block or something. |
| 4414 | * This might mean we pause for normal IO in the middle of |
NeilBrown | 02ec502 | 2015-07-06 16:33:47 +1000 | [diff] [blame] | 4415 | * a chunk, but that is not a problem as mddev->reshape_position |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4416 | * can record any location. |
| 4417 | * |
| 4418 | * If we will want to write to a location that isn't |
| 4419 | * yet recorded as 'safe' (i.e. in metadata on disk) then |
| 4420 | * we need to flush all reshape requests and update the metadata. |
| 4421 | * |
| 4422 | * When reshaping forwards (e.g. to more devices), we interpret |
| 4423 | * 'safe' as the earliest block which might not have been copied |
| 4424 | * down yet. We divide this by previous stripe size and multiply |
| 4425 | * by previous stripe length to get lowest device offset that we |
| 4426 | * cannot write to yet. |
| 4427 | * We interpret 'sector_nr' as an address that we want to write to. |
| 4428 | * From this we use last_device_address() to find where we might |
| 4429 | * write to, and first_device_address on the 'safe' position. |
| 4430 | * If this 'next' write position is after the 'safe' position, |
| 4431 | * we must update the metadata to increase the 'safe' position. |
| 4432 | * |
| 4433 | * When reshaping backwards, we round in the opposite direction |
| 4434 | * and perform the reverse test: next write position must not be |
| 4435 | * less than current safe position. |
| 4436 | * |
| 4437 | * In all this the minimum difference in data offsets |
| 4438 | * (conf->offset_diff - always positive) allows a bit of slack, |
NeilBrown | 02ec502 | 2015-07-06 16:33:47 +1000 | [diff] [blame] | 4439 | * so next can be after 'safe', but not by more than offset_diff |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4440 | * |
| 4441 | * We need to prepare all the bios here before we start any IO |
| 4442 | * to ensure the size we choose is acceptable to all devices. |
| 4443 | * The means one for each copy for write-out and an extra one for |
| 4444 | * read-in. |
| 4445 | * We store the read-in bio in ->master_bio and the others in |
| 4446 | * ->devs[x].bio and ->devs[x].repl_bio. |
| 4447 | */ |
| 4448 | struct r10conf *conf = mddev->private; |
| 4449 | struct r10bio *r10_bio; |
| 4450 | sector_t next, safe, last; |
| 4451 | int max_sectors; |
| 4452 | int nr_sectors; |
| 4453 | int s; |
| 4454 | struct md_rdev *rdev; |
| 4455 | int need_flush = 0; |
| 4456 | struct bio *blist; |
| 4457 | struct bio *bio, *read_bio; |
| 4458 | int sectors_done = 0; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 4459 | struct page **pages; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4460 | |
| 4461 | if (sector_nr == 0) { |
| 4462 | /* If restarting in the middle, skip the initial sectors */ |
| 4463 | if (mddev->reshape_backwards && |
| 4464 | conf->reshape_progress < raid10_size(mddev, 0, 0)) { |
| 4465 | sector_nr = (raid10_size(mddev, 0, 0) |
| 4466 | - conf->reshape_progress); |
| 4467 | } else if (!mddev->reshape_backwards && |
| 4468 | conf->reshape_progress > 0) |
| 4469 | sector_nr = conf->reshape_progress; |
| 4470 | if (sector_nr) { |
| 4471 | mddev->curr_resync_completed = sector_nr; |
| 4472 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 4473 | *skipped = 1; |
| 4474 | return sector_nr; |
| 4475 | } |
| 4476 | } |
| 4477 | |
| 4478 | /* We don't use sector_nr to track where we are up to |
| 4479 | * as that doesn't work well for ->reshape_backwards. |
| 4480 | * So just use ->reshape_progress. |
| 4481 | */ |
| 4482 | if (mddev->reshape_backwards) { |
| 4483 | /* 'next' is the earliest device address that we might |
| 4484 | * write to for this chunk in the new layout |
| 4485 | */ |
| 4486 | next = first_dev_address(conf->reshape_progress - 1, |
| 4487 | &conf->geo); |
| 4488 | |
| 4489 | /* 'safe' is the last device address that we might read from |
| 4490 | * in the old layout after a restart |
| 4491 | */ |
| 4492 | safe = last_dev_address(conf->reshape_safe - 1, |
| 4493 | &conf->prev); |
| 4494 | |
| 4495 | if (next + conf->offset_diff < safe) |
| 4496 | need_flush = 1; |
| 4497 | |
| 4498 | last = conf->reshape_progress - 1; |
| 4499 | sector_nr = last & ~(sector_t)(conf->geo.chunk_mask |
| 4500 | & conf->prev.chunk_mask); |
| 4501 | if (sector_nr + RESYNC_BLOCK_SIZE/512 < last) |
| 4502 | sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512; |
| 4503 | } else { |
| 4504 | /* 'next' is after the last device address that we |
| 4505 | * might write to for this chunk in the new layout |
| 4506 | */ |
| 4507 | next = last_dev_address(conf->reshape_progress, &conf->geo); |
| 4508 | |
| 4509 | /* 'safe' is the earliest device address that we might |
| 4510 | * read from in the old layout after a restart |
| 4511 | */ |
| 4512 | safe = first_dev_address(conf->reshape_safe, &conf->prev); |
| 4513 | |
| 4514 | /* Need to update metadata if 'next' might be beyond 'safe' |
| 4515 | * as that would possibly corrupt data |
| 4516 | */ |
| 4517 | if (next > safe + conf->offset_diff) |
| 4518 | need_flush = 1; |
| 4519 | |
| 4520 | sector_nr = conf->reshape_progress; |
| 4521 | last = sector_nr | (conf->geo.chunk_mask |
| 4522 | & conf->prev.chunk_mask); |
| 4523 | |
| 4524 | if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last) |
| 4525 | last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1; |
| 4526 | } |
| 4527 | |
| 4528 | if (need_flush || |
| 4529 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
| 4530 | /* Need to update reshape_position in metadata */ |
| 4531 | wait_barrier(conf); |
| 4532 | mddev->reshape_position = conf->reshape_progress; |
| 4533 | if (mddev->reshape_backwards) |
| 4534 | mddev->curr_resync_completed = raid10_size(mddev, 0, 0) |
| 4535 | - conf->reshape_progress; |
| 4536 | else |
| 4537 | mddev->curr_resync_completed = conf->reshape_progress; |
| 4538 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4539 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4540 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4541 | wait_event(mddev->sb_wait, mddev->sb_flags == 0 || |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 4542 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 4543 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 4544 | allow_barrier(conf); |
| 4545 | return sectors_done; |
| 4546 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4547 | conf->reshape_safe = mddev->reshape_position; |
| 4548 | allow_barrier(conf); |
| 4549 | } |
| 4550 | |
Xiao Ni | 1d0ffd2 | 2018-08-30 15:57:09 +0800 | [diff] [blame] | 4551 | raise_barrier(conf, 0); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4552 | read_more: |
| 4553 | /* Now schedule reads for blocks from sector_nr to last */ |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 4554 | r10_bio = raid10_alloc_init_r10buf(conf); |
NeilBrown | cb8b12b | 2014-08-18 14:38:45 +1000 | [diff] [blame] | 4555 | r10_bio->state = 0; |
Xiao Ni | 1d0ffd2 | 2018-08-30 15:57:09 +0800 | [diff] [blame] | 4556 | raise_barrier(conf, 1); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4557 | atomic_set(&r10_bio->remaining, 0); |
| 4558 | r10_bio->mddev = mddev; |
| 4559 | r10_bio->sector = sector_nr; |
| 4560 | set_bit(R10BIO_IsReshape, &r10_bio->state); |
| 4561 | r10_bio->sectors = last - sector_nr + 1; |
| 4562 | rdev = read_balance(conf, r10_bio, &max_sectors); |
| 4563 | BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state)); |
| 4564 | |
| 4565 | if (!rdev) { |
| 4566 | /* Cannot read from here, so need to record bad blocks |
| 4567 | * on all the target devices. |
| 4568 | */ |
| 4569 | // FIXME |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 4570 | mempool_free(r10_bio, &conf->r10buf_pool); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4571 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4572 | return sectors_done; |
| 4573 | } |
| 4574 | |
| 4575 | read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev); |
| 4576 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 4577 | bio_set_dev(read_bio, rdev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4578 | read_bio->bi_iter.bi_sector = (r10_bio->devs[r10_bio->read_slot].addr |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4579 | + rdev->data_offset); |
| 4580 | read_bio->bi_private = r10_bio; |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 4581 | read_bio->bi_end_io = end_reshape_read; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 4582 | bio_set_op_attrs(read_bio, REQ_OP_READ, 0); |
NeilBrown | ce0b0a4 | 2014-08-18 13:56:38 +1000 | [diff] [blame] | 4583 | read_bio->bi_flags &= (~0UL << BIO_RESET_BITS); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 4584 | read_bio->bi_status = 0; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4585 | read_bio->bi_vcnt = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4586 | read_bio->bi_iter.bi_size = 0; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4587 | r10_bio->master_bio = read_bio; |
| 4588 | r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum; |
| 4589 | |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 4590 | /* |
| 4591 | * Broadcast RESYNC message to other nodes, so all nodes would not |
| 4592 | * write to the region to avoid conflict. |
| 4593 | */ |
| 4594 | if (mddev_is_clustered(mddev) && conf->cluster_sync_high <= sector_nr) { |
| 4595 | struct mdp_superblock_1 *sb = NULL; |
| 4596 | int sb_reshape_pos = 0; |
| 4597 | |
| 4598 | conf->cluster_sync_low = sector_nr; |
| 4599 | conf->cluster_sync_high = sector_nr + CLUSTER_RESYNC_WINDOW_SECTORS; |
| 4600 | sb = page_address(rdev->sb_page); |
| 4601 | if (sb) { |
| 4602 | sb_reshape_pos = le64_to_cpu(sb->reshape_position); |
| 4603 | /* |
| 4604 | * Set cluster_sync_low again if next address for array |
| 4605 | * reshape is less than cluster_sync_low. Since we can't |
| 4606 | * update cluster_sync_low until it has finished reshape. |
| 4607 | */ |
| 4608 | if (sb_reshape_pos < conf->cluster_sync_low) |
| 4609 | conf->cluster_sync_low = sb_reshape_pos; |
| 4610 | } |
| 4611 | |
| 4612 | md_cluster_ops->resync_info_update(mddev, conf->cluster_sync_low, |
| 4613 | conf->cluster_sync_high); |
| 4614 | } |
| 4615 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4616 | /* Now find the locations in the new layout */ |
| 4617 | __raid10_find_phys(&conf->geo, r10_bio); |
| 4618 | |
| 4619 | blist = read_bio; |
| 4620 | read_bio->bi_next = NULL; |
| 4621 | |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4622 | rcu_read_lock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4623 | for (s = 0; s < conf->copies*2; s++) { |
| 4624 | struct bio *b; |
| 4625 | int d = r10_bio->devs[s/2].devnum; |
| 4626 | struct md_rdev *rdev2; |
| 4627 | if (s&1) { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4628 | rdev2 = rcu_dereference(conf->mirrors[d].replacement); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4629 | b = r10_bio->devs[s/2].repl_bio; |
| 4630 | } else { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4631 | rdev2 = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4632 | b = r10_bio->devs[s/2].bio; |
| 4633 | } |
| 4634 | if (!rdev2 || test_bit(Faulty, &rdev2->flags)) |
| 4635 | continue; |
Kent Overstreet | 8be185f | 2012-09-06 14:14:43 -0700 | [diff] [blame] | 4636 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 4637 | bio_set_dev(b, rdev2->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4638 | b->bi_iter.bi_sector = r10_bio->devs[s/2].addr + |
| 4639 | rdev2->new_data_offset; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4640 | b->bi_end_io = end_reshape_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 4641 | bio_set_op_attrs(b, REQ_OP_WRITE, 0); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4642 | b->bi_next = blist; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4643 | blist = b; |
| 4644 | } |
| 4645 | |
| 4646 | /* Now add as many pages as possible to all of these bios. */ |
| 4647 | |
| 4648 | nr_sectors = 0; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 4649 | pages = get_resync_pages(r10_bio->devs[0].bio)->pages; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4650 | for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 4651 | struct page *page = pages[s / (PAGE_SIZE >> 9)]; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4652 | int len = (max_sectors - s) << 9; |
| 4653 | if (len > PAGE_SIZE) |
| 4654 | len = PAGE_SIZE; |
| 4655 | for (bio = blist; bio ; bio = bio->bi_next) { |
Ming Lei | c85ba14 | 2017-03-17 00:12:22 +0800 | [diff] [blame] | 4656 | /* |
| 4657 | * won't fail because the vec table is big enough |
| 4658 | * to hold all these pages |
| 4659 | */ |
| 4660 | bio_add_page(bio, page, len, 0); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4661 | } |
| 4662 | sector_nr += len >> 9; |
| 4663 | nr_sectors += len >> 9; |
| 4664 | } |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4665 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4666 | r10_bio->sectors = nr_sectors; |
| 4667 | |
| 4668 | /* Now submit the read */ |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 4669 | md_sync_acct_bio(read_bio, r10_bio->sectors); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4670 | atomic_inc(&r10_bio->remaining); |
| 4671 | read_bio->bi_next = NULL; |
| 4672 | generic_make_request(read_bio); |
| 4673 | sector_nr += nr_sectors; |
| 4674 | sectors_done += nr_sectors; |
| 4675 | if (sector_nr <= last) |
| 4676 | goto read_more; |
| 4677 | |
Xiao Ni | 1d0ffd2 | 2018-08-30 15:57:09 +0800 | [diff] [blame] | 4678 | lower_barrier(conf); |
| 4679 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4680 | /* Now that we have done the whole section we can |
| 4681 | * update reshape_progress |
| 4682 | */ |
| 4683 | if (mddev->reshape_backwards) |
| 4684 | conf->reshape_progress -= sectors_done; |
| 4685 | else |
| 4686 | conf->reshape_progress += sectors_done; |
| 4687 | |
| 4688 | return sectors_done; |
| 4689 | } |
| 4690 | |
| 4691 | static void end_reshape_request(struct r10bio *r10_bio); |
| 4692 | static int handle_reshape_read_error(struct mddev *mddev, |
| 4693 | struct r10bio *r10_bio); |
| 4694 | static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
| 4695 | { |
| 4696 | /* Reshape read completed. Hopefully we have a block |
| 4697 | * to write out. |
| 4698 | * If we got a read error then we do sync 1-page reads from |
| 4699 | * elsewhere until we find the data - or give up. |
| 4700 | */ |
| 4701 | struct r10conf *conf = mddev->private; |
| 4702 | int s; |
| 4703 | |
| 4704 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) |
| 4705 | if (handle_reshape_read_error(mddev, r10_bio) < 0) { |
| 4706 | /* Reshape has been aborted */ |
| 4707 | md_done_sync(mddev, r10_bio->sectors, 0); |
| 4708 | return; |
| 4709 | } |
| 4710 | |
| 4711 | /* We definitely have the data in the pages, schedule the |
| 4712 | * writes. |
| 4713 | */ |
| 4714 | atomic_set(&r10_bio->remaining, 1); |
| 4715 | for (s = 0; s < conf->copies*2; s++) { |
| 4716 | struct bio *b; |
| 4717 | int d = r10_bio->devs[s/2].devnum; |
| 4718 | struct md_rdev *rdev; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4719 | rcu_read_lock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4720 | if (s&1) { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4721 | rdev = rcu_dereference(conf->mirrors[d].replacement); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4722 | b = r10_bio->devs[s/2].repl_bio; |
| 4723 | } else { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4724 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4725 | b = r10_bio->devs[s/2].bio; |
| 4726 | } |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4727 | if (!rdev || test_bit(Faulty, &rdev->flags)) { |
| 4728 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4729 | continue; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4730 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4731 | atomic_inc(&rdev->nr_pending); |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4732 | rcu_read_unlock(); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 4733 | md_sync_acct_bio(b, r10_bio->sectors); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4734 | atomic_inc(&r10_bio->remaining); |
| 4735 | b->bi_next = NULL; |
| 4736 | generic_make_request(b); |
| 4737 | } |
| 4738 | end_reshape_request(r10_bio); |
| 4739 | } |
| 4740 | |
| 4741 | static void end_reshape(struct r10conf *conf) |
| 4742 | { |
| 4743 | if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) |
| 4744 | return; |
| 4745 | |
| 4746 | spin_lock_irq(&conf->device_lock); |
| 4747 | conf->prev = conf->geo; |
| 4748 | md_finish_reshape(conf->mddev); |
| 4749 | smp_wmb(); |
| 4750 | conf->reshape_progress = MaxSector; |
NeilBrown | 299b068 | 2015-07-06 17:37:49 +1000 | [diff] [blame] | 4751 | conf->reshape_safe = MaxSector; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4752 | spin_unlock_irq(&conf->device_lock); |
| 4753 | |
| 4754 | /* read-ahead size must cover two whole stripes, which is |
| 4755 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 4756 | */ |
| 4757 | if (conf->mddev->queue) { |
| 4758 | int stripe = conf->geo.raid_disks * |
| 4759 | ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 4760 | stripe /= conf->geo.near_copies; |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4761 | if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
| 4762 | conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4763 | } |
| 4764 | conf->fullsync = 0; |
| 4765 | } |
| 4766 | |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 4767 | static void raid10_update_reshape_pos(struct mddev *mddev) |
| 4768 | { |
| 4769 | struct r10conf *conf = mddev->private; |
Guoqing Jiang | 5ebaf80 | 2018-10-18 16:37:43 +0800 | [diff] [blame] | 4770 | sector_t lo, hi; |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 4771 | |
Guoqing Jiang | 5ebaf80 | 2018-10-18 16:37:43 +0800 | [diff] [blame] | 4772 | md_cluster_ops->resync_info_get(mddev, &lo, &hi); |
| 4773 | if (((mddev->reshape_position <= hi) && (mddev->reshape_position >= lo)) |
| 4774 | || mddev->reshape_position == MaxSector) |
| 4775 | conf->reshape_progress = mddev->reshape_position; |
| 4776 | else |
| 4777 | WARN_ON_ONCE(1); |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 4778 | } |
| 4779 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4780 | static int handle_reshape_read_error(struct mddev *mddev, |
| 4781 | struct r10bio *r10_bio) |
| 4782 | { |
| 4783 | /* Use sync reads to get the blocks from somewhere else */ |
| 4784 | int sectors = r10_bio->sectors; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4785 | struct r10conf *conf = mddev->private; |
Matthias Kaehlcke | 584ed9fa | 2017-10-05 11:28:47 -0700 | [diff] [blame] | 4786 | struct r10bio *r10b; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4787 | int slot = 0; |
| 4788 | int idx = 0; |
Ming Lei | 2d06e3b | 2017-03-17 00:12:35 +0800 | [diff] [blame] | 4789 | struct page **pages; |
| 4790 | |
Matthias Kaehlcke | 584ed9fa | 2017-10-05 11:28:47 -0700 | [diff] [blame] | 4791 | r10b = kmalloc(sizeof(*r10b) + |
| 4792 | sizeof(struct r10dev) * conf->copies, GFP_NOIO); |
| 4793 | if (!r10b) { |
| 4794 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4795 | return -ENOMEM; |
| 4796 | } |
| 4797 | |
Ming Lei | 2d06e3b | 2017-03-17 00:12:35 +0800 | [diff] [blame] | 4798 | /* reshape IOs share pages from .devs[0].bio */ |
| 4799 | pages = get_resync_pages(r10_bio->devs[0].bio)->pages; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4800 | |
NeilBrown | e0ee778 | 2012-08-18 09:51:42 +1000 | [diff] [blame] | 4801 | r10b->sector = r10_bio->sector; |
| 4802 | __raid10_find_phys(&conf->prev, r10b); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4803 | |
| 4804 | while (sectors) { |
| 4805 | int s = sectors; |
| 4806 | int success = 0; |
| 4807 | int first_slot = slot; |
| 4808 | |
| 4809 | if (s > (PAGE_SIZE >> 9)) |
| 4810 | s = PAGE_SIZE >> 9; |
| 4811 | |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4812 | rcu_read_lock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4813 | while (!success) { |
NeilBrown | e0ee778 | 2012-08-18 09:51:42 +1000 | [diff] [blame] | 4814 | int d = r10b->devs[slot].devnum; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4815 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4816 | sector_t addr; |
| 4817 | if (rdev == NULL || |
| 4818 | test_bit(Faulty, &rdev->flags) || |
| 4819 | !test_bit(In_sync, &rdev->flags)) |
| 4820 | goto failed; |
| 4821 | |
NeilBrown | e0ee778 | 2012-08-18 09:51:42 +1000 | [diff] [blame] | 4822 | addr = r10b->devs[slot].addr + idx * PAGE_SIZE; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4823 | atomic_inc(&rdev->nr_pending); |
| 4824 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4825 | success = sync_page_io(rdev, |
| 4826 | addr, |
| 4827 | s << 9, |
Ming Lei | 2d06e3b | 2017-03-17 00:12:35 +0800 | [diff] [blame] | 4828 | pages[idx], |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 4829 | REQ_OP_READ, 0, false); |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4830 | rdev_dec_pending(rdev, mddev); |
| 4831 | rcu_read_lock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4832 | if (success) |
| 4833 | break; |
| 4834 | failed: |
| 4835 | slot++; |
| 4836 | if (slot >= conf->copies) |
| 4837 | slot = 0; |
| 4838 | if (slot == first_slot) |
| 4839 | break; |
| 4840 | } |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4841 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4842 | if (!success) { |
| 4843 | /* couldn't read this block, must give up */ |
| 4844 | set_bit(MD_RECOVERY_INTR, |
| 4845 | &mddev->recovery); |
Matthias Kaehlcke | 584ed9fa | 2017-10-05 11:28:47 -0700 | [diff] [blame] | 4846 | kfree(r10b); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4847 | return -EIO; |
| 4848 | } |
| 4849 | sectors -= s; |
| 4850 | idx++; |
| 4851 | } |
Matthias Kaehlcke | 584ed9fa | 2017-10-05 11:28:47 -0700 | [diff] [blame] | 4852 | kfree(r10b); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4853 | return 0; |
| 4854 | } |
| 4855 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 4856 | static void end_reshape_write(struct bio *bio) |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4857 | { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 4858 | struct r10bio *r10_bio = get_resync_r10bio(bio); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4859 | struct mddev *mddev = r10_bio->mddev; |
| 4860 | struct r10conf *conf = mddev->private; |
| 4861 | int d; |
| 4862 | int slot; |
| 4863 | int repl; |
| 4864 | struct md_rdev *rdev = NULL; |
| 4865 | |
| 4866 | d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
| 4867 | if (repl) |
| 4868 | rdev = conf->mirrors[d].replacement; |
| 4869 | if (!rdev) { |
| 4870 | smp_mb(); |
| 4871 | rdev = conf->mirrors[d].rdev; |
| 4872 | } |
| 4873 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 4874 | if (bio->bi_status) { |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4875 | /* FIXME should record badblock */ |
| 4876 | md_error(mddev, rdev); |
| 4877 | } |
| 4878 | |
| 4879 | rdev_dec_pending(rdev, mddev); |
| 4880 | end_reshape_request(r10_bio); |
| 4881 | } |
| 4882 | |
| 4883 | static void end_reshape_request(struct r10bio *r10_bio) |
| 4884 | { |
| 4885 | if (!atomic_dec_and_test(&r10_bio->remaining)) |
| 4886 | return; |
| 4887 | md_done_sync(r10_bio->mddev, r10_bio->sectors, 1); |
| 4888 | bio_put(r10_bio->master_bio); |
| 4889 | put_buf(r10_bio); |
| 4890 | } |
| 4891 | |
| 4892 | static void raid10_finish_reshape(struct mddev *mddev) |
| 4893 | { |
| 4894 | struct r10conf *conf = mddev->private; |
| 4895 | |
| 4896 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 4897 | return; |
| 4898 | |
| 4899 | if (mddev->delta_disks > 0) { |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4900 | if (mddev->recovery_cp > mddev->resync_max_sectors) { |
| 4901 | mddev->recovery_cp = mddev->resync_max_sectors; |
| 4902 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4903 | } |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 4904 | mddev->resync_max_sectors = mddev->array_sectors; |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 4905 | } else { |
| 4906 | int d; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4907 | rcu_read_lock(); |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 4908 | for (d = conf->geo.raid_disks ; |
| 4909 | d < conf->geo.raid_disks - mddev->delta_disks; |
| 4910 | d++) { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4911 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 4912 | if (rdev) |
| 4913 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4914 | rdev = rcu_dereference(conf->mirrors[d].replacement); |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 4915 | if (rdev) |
| 4916 | clear_bit(In_sync, &rdev->flags); |
| 4917 | } |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4918 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4919 | } |
| 4920 | mddev->layout = mddev->new_layout; |
| 4921 | mddev->chunk_sectors = 1 << conf->geo.chunk_shift; |
| 4922 | mddev->reshape_position = MaxSector; |
| 4923 | mddev->delta_disks = 0; |
| 4924 | mddev->reshape_backwards = 0; |
| 4925 | } |
| 4926 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4927 | static struct md_personality raid10_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4928 | { |
| 4929 | .name = "raid10", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4930 | .level = 10, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4931 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 4932 | .make_request = raid10_make_request, |
| 4933 | .run = raid10_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4934 | .free = raid10_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 4935 | .status = raid10_status, |
| 4936 | .error_handler = raid10_error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4937 | .hot_add_disk = raid10_add_disk, |
| 4938 | .hot_remove_disk= raid10_remove_disk, |
| 4939 | .spare_active = raid10_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 4940 | .sync_request = raid10_sync_request, |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4941 | .quiesce = raid10_quiesce, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4942 | .size = raid10_size, |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 4943 | .resize = raid10_resize, |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4944 | .takeover = raid10_takeover, |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4945 | .check_reshape = raid10_check_reshape, |
| 4946 | .start_reshape = raid10_start_reshape, |
| 4947 | .finish_reshape = raid10_finish_reshape, |
Guoqing Jiang | 7564bed | 2018-10-18 16:37:42 +0800 | [diff] [blame] | 4948 | .update_reshape_pos = raid10_update_reshape_pos, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4949 | .congested = raid10_congested, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4950 | }; |
| 4951 | |
| 4952 | static int __init raid_init(void) |
| 4953 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4954 | return register_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4955 | } |
| 4956 | |
| 4957 | static void raid_exit(void) |
| 4958 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4959 | unregister_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4960 | } |
| 4961 | |
| 4962 | module_init(raid_init); |
| 4963 | module_exit(raid_exit); |
| 4964 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4965 | MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4966 | MODULE_ALIAS("md-personality-9"); /* RAID10 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4967 | MODULE_ALIAS("md-raid10"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4968 | MODULE_ALIAS("md-level-10"); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 4969 | |
| 4970 | module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); |