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> |
NeilBrown | 109e376 | 2016-11-18 13:22:04 +1100 | [diff] [blame] | 28 | #include <trace/events/block.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 29 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 30 | #include "raid10.h" |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 31 | #include "raid0.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 32 | #include "md-bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 33 | |
| 34 | /* |
| 35 | * RAID10 provides a combination of RAID0 and RAID1 functionality. |
| 36 | * The layout of data is defined by |
| 37 | * chunk_size |
| 38 | * raid_disks |
| 39 | * near_copies (stored in low byte of layout) |
| 40 | * far_copies (stored in second byte of layout) |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 41 | * far_offset (stored in bit 16 of layout ) |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 42 | * use_far_sets (stored in bit 17 of layout ) |
NeilBrown | 8bce6d3 | 2015-10-22 13:20:15 +1100 | [diff] [blame] | 43 | * use_far_sets_bugfixed (stored in bit 18 of layout ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 44 | * |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 45 | * The data to be stored is divided into chunks using chunksize. Each device |
| 46 | * is divided into far_copies sections. In each section, chunks are laid out |
| 47 | * in a style similar to raid0, but near_copies copies of each chunk is stored |
| 48 | * (each on a different drive). The starting device for each section is offset |
| 49 | * near_copies from the starting device of the previous section. Thus there |
| 50 | * are (near_copies * far_copies) of each chunk, and each is on a different |
| 51 | * drive. near_copies and far_copies must be at least one, and their product |
| 52 | * is at most raid_disks. |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 53 | * |
| 54 | * 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] | 55 | * The copies are still in different stripes, but instead of being very far |
| 56 | * apart on disk, there are adjacent stripes. |
| 57 | * |
| 58 | * The far and offset algorithms are handled slightly differently if |
| 59 | * 'use_far_sets' is true. In this case, the array's devices are grouped into |
| 60 | * sets that are (near_copies * far_copies) in size. The far copied stripes |
| 61 | * are still shifted by 'near_copies' devices, but this shifting stays confined |
| 62 | * to the set rather than the entire array. This is done to improve the number |
| 63 | * of device combinations that can fail without causing the array to fail. |
| 64 | * Example 'far' algorithm w/o 'use_far_sets' (each letter represents a chunk |
| 65 | * on a device): |
| 66 | * A B C D A B C D E |
| 67 | * ... ... |
| 68 | * D A B C E A B C D |
| 69 | * Example 'far' algorithm w/ 'use_far_sets' enabled (sets illustrated w/ []'s): |
| 70 | * [A B] [C D] [A B] [C D E] |
| 71 | * |...| |...| |...| | ... | |
| 72 | * [B A] [D C] [B A] [E C D] |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | */ |
| 74 | |
| 75 | /* |
| 76 | * Number of guaranteed r10bios in case of extreme VM load: |
| 77 | */ |
| 78 | #define NR_RAID10_BIOS 256 |
| 79 | |
Jonathan Brassow | 473e87c | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 80 | /* when we get a read error on a read-only array, we redirect to another |
| 81 | * device without failing the first device, or trying to over-write to |
| 82 | * correct the read error. To keep track of bad blocks on a per-bio |
| 83 | * level, we store IO_BLOCKED in the appropriate 'bios' pointer |
| 84 | */ |
| 85 | #define IO_BLOCKED ((struct bio *)1) |
| 86 | /* When we successfully write to a known bad-block, we need to remove the |
| 87 | * bad-block marking which must be done from process context. So we record |
| 88 | * the success by setting devs[n].bio to IO_MADE_GOOD |
| 89 | */ |
| 90 | #define IO_MADE_GOOD ((struct bio *)2) |
| 91 | |
| 92 | #define BIO_SPECIAL(bio) ((unsigned long)bio <= 2) |
| 93 | |
| 94 | /* When there are this many requests queued to be written by |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 95 | * the raid10 thread, we become 'congested' to provide back-pressure |
| 96 | * for writeback. |
| 97 | */ |
| 98 | static int max_queued_requests = 1024; |
| 99 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 100 | static void allow_barrier(struct r10conf *conf); |
| 101 | static void lower_barrier(struct r10conf *conf); |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 102 | static int _enough(struct r10conf *conf, int previous, int ignore); |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 103 | static int enough(struct r10conf *conf, int ignore); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 104 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, |
| 105 | int *skipped); |
| 106 | static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 107 | static void end_reshape_write(struct bio *bio); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 108 | static void end_reshape(struct r10conf *conf); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 109 | |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 110 | #define raid10_log(md, fmt, args...) \ |
| 111 | do { if ((md)->queue) blk_add_trace_msg((md)->queue, "raid10 " fmt, ##args); } while (0) |
| 112 | |
Ming Lei | fb0eb5d | 2017-07-14 16:14:43 +0800 | [diff] [blame] | 113 | #include "raid1-10.c" |
| 114 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 115 | /* |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 116 | * for resync bio, r10bio pointer can be retrieved from the per-bio |
| 117 | * 'struct resync_pages'. |
| 118 | */ |
| 119 | static inline struct r10bio *get_resync_r10bio(struct bio *bio) |
| 120 | { |
| 121 | return get_resync_pages(bio)->raid_bio; |
| 122 | } |
| 123 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 124 | static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 126 | struct r10conf *conf = data; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 127 | int size = offsetof(struct r10bio, devs[conf->copies]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 129 | /* allocate a r10bio with room for raid_disks entries in the |
| 130 | * bios array */ |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 131 | return kzalloc(size, gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | static void r10bio_pool_free(void *r10_bio, void *data) |
| 135 | { |
| 136 | kfree(r10_bio); |
| 137 | } |
| 138 | |
NeilBrown | 0310fa2 | 2008-08-05 15:54:14 +1000 | [diff] [blame] | 139 | /* amount of memory to reserve for resync requests */ |
| 140 | #define RESYNC_WINDOW (1024*1024) |
| 141 | /* maximum number of concurrent requests, memory permitting */ |
| 142 | #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * When performing a resync, we need to read and compare, so |
| 146 | * we need as many pages are there are copies. |
| 147 | * When performing a recovery, we need 2 bios, one for read, |
| 148 | * one for write (we recover only one drive per r10buf) |
| 149 | * |
| 150 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 151 | static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 152 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 153 | struct r10conf *conf = data; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 154 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | struct bio *bio; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 156 | int j; |
| 157 | int nalloc, nalloc_rp; |
| 158 | struct resync_pages *rps; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | |
| 160 | r10_bio = r10bio_pool_alloc(gfp_flags, conf); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 161 | if (!r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 163 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 164 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) || |
| 165 | test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | nalloc = conf->copies; /* resync */ |
| 167 | else |
| 168 | nalloc = 2; /* recovery */ |
| 169 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 170 | /* allocate once for all bios */ |
| 171 | if (!conf->have_replacement) |
| 172 | nalloc_rp = nalloc; |
| 173 | else |
| 174 | nalloc_rp = nalloc * 2; |
| 175 | rps = kmalloc(sizeof(struct resync_pages) * nalloc_rp, gfp_flags); |
| 176 | if (!rps) |
| 177 | goto out_free_r10bio; |
| 178 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 179 | /* |
| 180 | * Allocate bios. |
| 181 | */ |
| 182 | for (j = nalloc ; j-- ; ) { |
NeilBrown | 6746557 | 2010-10-26 17:33:54 +1100 | [diff] [blame] | 183 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | if (!bio) |
| 185 | goto out_free_bio; |
| 186 | r10_bio->devs[j].bio = bio; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 187 | if (!conf->have_replacement) |
| 188 | continue; |
| 189 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
| 190 | if (!bio) |
| 191 | goto out_free_bio; |
| 192 | r10_bio->devs[j].repl_bio = bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | } |
| 194 | /* |
| 195 | * Allocate RESYNC_PAGES data pages and attach them |
| 196 | * where needed. |
| 197 | */ |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 198 | for (j = 0; j < nalloc; j++) { |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 199 | struct bio *rbio = r10_bio->devs[j].repl_bio; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 200 | struct resync_pages *rp, *rp_repl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 202 | rp = &rps[j]; |
| 203 | if (rbio) |
| 204 | rp_repl = &rps[nalloc + j]; |
| 205 | |
| 206 | bio = r10_bio->devs[j].bio; |
| 207 | |
| 208 | if (!j || test_bit(MD_RECOVERY_SYNC, |
| 209 | &conf->mddev->recovery)) { |
| 210 | if (resync_alloc_pages(rp, gfp_flags)) |
| 211 | goto out_free_pages; |
| 212 | } else { |
| 213 | memcpy(rp, &rps[0], sizeof(*rp)); |
| 214 | resync_get_all_pages(rp); |
| 215 | } |
| 216 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 217 | rp->raid_bio = r10_bio; |
| 218 | bio->bi_private = rp; |
| 219 | if (rbio) { |
| 220 | memcpy(rp_repl, rp, sizeof(*rp)); |
| 221 | rbio->bi_private = rp_repl; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | } |
| 223 | } |
| 224 | |
| 225 | return r10_bio; |
| 226 | |
| 227 | out_free_pages: |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 228 | while (--j >= 0) |
| 229 | resync_free_pages(&rps[j * 2]); |
| 230 | |
majianpeng | 5fdd2cf | 2012-05-22 13:55:03 +1000 | [diff] [blame] | 231 | j = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | out_free_bio: |
majianpeng | 5fdd2cf | 2012-05-22 13:55:03 +1000 | [diff] [blame] | 233 | for ( ; j < nalloc; j++) { |
| 234 | if (r10_bio->devs[j].bio) |
| 235 | bio_put(r10_bio->devs[j].bio); |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 236 | if (r10_bio->devs[j].repl_bio) |
| 237 | bio_put(r10_bio->devs[j].repl_bio); |
| 238 | } |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 239 | kfree(rps); |
| 240 | out_free_r10bio: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | r10bio_pool_free(r10_bio, conf); |
| 242 | return NULL; |
| 243 | } |
| 244 | |
| 245 | static void r10buf_pool_free(void *__r10_bio, void *data) |
| 246 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 247 | struct r10conf *conf = data; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 248 | struct r10bio *r10bio = __r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | int j; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 250 | struct resync_pages *rp = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 252 | for (j = conf->copies; j--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | struct bio *bio = r10bio->devs[j].bio; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 254 | |
| 255 | rp = get_resync_pages(bio); |
| 256 | resync_free_pages(rp); |
| 257 | bio_put(bio); |
| 258 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 259 | bio = r10bio->devs[j].repl_bio; |
| 260 | if (bio) |
| 261 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | } |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 263 | |
| 264 | /* resync pages array stored in the 1st bio's .bi_private */ |
| 265 | kfree(rp); |
| 266 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | r10bio_pool_free(r10bio, conf); |
| 268 | } |
| 269 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 270 | static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | { |
| 272 | int i; |
| 273 | |
| 274 | for (i = 0; i < conf->copies; i++) { |
| 275 | struct bio **bio = & r10_bio->devs[i].bio; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 276 | if (!BIO_SPECIAL(*bio)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 277 | bio_put(*bio); |
| 278 | *bio = NULL; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 279 | bio = &r10_bio->devs[i].repl_bio; |
| 280 | if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio)) |
| 281 | bio_put(*bio); |
| 282 | *bio = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 283 | } |
| 284 | } |
| 285 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 286 | static void free_r10bio(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 288 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | put_all_bios(conf, r10_bio); |
| 291 | mempool_free(r10_bio, conf->r10bio_pool); |
| 292 | } |
| 293 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 294 | static void put_buf(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 296 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 297 | |
| 298 | mempool_free(r10_bio, conf->r10buf_pool); |
| 299 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 300 | lower_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 303 | static void reschedule_retry(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 304 | { |
| 305 | unsigned long flags; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 306 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 307 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
| 309 | spin_lock_irqsave(&conf->device_lock, flags); |
| 310 | list_add(&r10_bio->retry_list, &conf->retry_list); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 311 | conf->nr_queued ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 313 | |
Arthur Jones | 388667b | 2008-07-25 12:03:38 -0700 | [diff] [blame] | 314 | /* wake up frozen array... */ |
| 315 | wake_up(&conf->wait_barrier); |
| 316 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | md_wakeup_thread(mddev->thread); |
| 318 | } |
| 319 | |
| 320 | /* |
| 321 | * raid_end_bio_io() is called when we have finished servicing a mirrored |
| 322 | * operation and are ready to return a success/failure code to the buffer |
| 323 | * cache layer. |
| 324 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 325 | static void raid_end_bio_io(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | { |
| 327 | struct bio *bio = r10_bio->master_bio; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 328 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 330 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 331 | bio->bi_status = BLK_STS_IOERR; |
NeilBrown | fd16f2e | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 332 | |
| 333 | bio_endio(bio); |
| 334 | /* |
| 335 | * Wake up any possible resync thread that waits for the device |
| 336 | * to go idle. |
| 337 | */ |
| 338 | allow_barrier(conf); |
| 339 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | free_r10bio(r10_bio); |
| 341 | } |
| 342 | |
| 343 | /* |
| 344 | * Update disk head position estimator based on IRQ completion info. |
| 345 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 346 | static inline void update_head_pos(int slot, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 348 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | |
| 350 | conf->mirrors[r10_bio->devs[slot].devnum].head_position = |
| 351 | r10_bio->devs[slot].addr + (r10_bio->sectors); |
| 352 | } |
| 353 | |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 354 | /* |
| 355 | * Find the disk number which triggered given bio |
| 356 | */ |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 357 | static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio, |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 358 | struct bio *bio, int *slotp, int *replp) |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 359 | { |
| 360 | int slot; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 361 | int repl = 0; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 362 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 363 | for (slot = 0; slot < conf->copies; slot++) { |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 364 | if (r10_bio->devs[slot].bio == bio) |
| 365 | break; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 366 | if (r10_bio->devs[slot].repl_bio == bio) { |
| 367 | repl = 1; |
| 368 | break; |
| 369 | } |
| 370 | } |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 371 | |
| 372 | BUG_ON(slot == conf->copies); |
| 373 | update_head_pos(slot, r10_bio); |
| 374 | |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 375 | if (slotp) |
| 376 | *slotp = slot; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 377 | if (replp) |
| 378 | *replp = repl; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 379 | return r10_bio->devs[slot].devnum; |
| 380 | } |
| 381 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 382 | static void raid10_end_read_request(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 384 | int uptodate = !bio->bi_status; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 385 | struct r10bio *r10_bio = bio->bi_private; |
Colin Ian King | a0e764c | 2017-10-11 11:46:54 +0100 | [diff] [blame^] | 386 | int slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 387 | struct md_rdev *rdev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 388 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | slot = r10_bio->read_slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 391 | rdev = r10_bio->devs[slot].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | /* |
| 393 | * this branch is our 'one mirror IO has finished' event handler: |
| 394 | */ |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 395 | update_head_pos(slot, r10_bio); |
| 396 | |
| 397 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | /* |
| 399 | * Set R10BIO_Uptodate in our master bio, so that |
| 400 | * we will return a good error code to the higher |
| 401 | * levels even if IO on some other mirrored buffer fails. |
| 402 | * |
| 403 | * The 'master' represents the composite IO operation to |
| 404 | * user-side. So if something waits for IO, then it will |
| 405 | * wait for the 'master' bio. |
| 406 | */ |
| 407 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 408 | } else { |
| 409 | /* If all other devices that store this block have |
| 410 | * failed, we want to return the error upwards rather |
| 411 | * than fail the last device. Here we redefine |
| 412 | * "uptodate" to mean "Don't want to retry" |
| 413 | */ |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 414 | if (!_enough(conf, test_bit(R10BIO_Previous, &r10_bio->state), |
| 415 | rdev->raid_disk)) |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 416 | uptodate = 1; |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 417 | } |
| 418 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | raid_end_bio_io(r10_bio); |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 420 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 421 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | /* |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 423 | * oops, read error - keep the refcount on the rdev |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | */ |
| 425 | char b[BDEVNAME_SIZE]; |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 426 | pr_err_ratelimited("md/raid10:%s: %s: rescheduling sector %llu\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 427 | mdname(conf->mddev), |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 428 | bdevname(rdev->bdev, b), |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 429 | (unsigned long long)r10_bio->sector); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 430 | set_bit(R10BIO_ReadError, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | reschedule_retry(r10_bio); |
| 432 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | } |
| 434 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 435 | static void close_write(struct r10bio *r10_bio) |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 436 | { |
| 437 | /* clear the bitmap if all writes complete successfully */ |
| 438 | bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, |
| 439 | r10_bio->sectors, |
| 440 | !test_bit(R10BIO_Degraded, &r10_bio->state), |
| 441 | 0); |
| 442 | md_write_end(r10_bio->mddev); |
| 443 | } |
| 444 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 445 | static void one_write_done(struct r10bio *r10_bio) |
NeilBrown | 19d5f83 | 2011-09-10 17:21:17 +1000 | [diff] [blame] | 446 | { |
| 447 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 448 | if (test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 449 | reschedule_retry(r10_bio); |
| 450 | else { |
| 451 | close_write(r10_bio); |
| 452 | if (test_bit(R10BIO_MadeGood, &r10_bio->state)) |
| 453 | reschedule_retry(r10_bio); |
| 454 | else |
| 455 | raid_end_bio_io(r10_bio); |
| 456 | } |
| 457 | } |
| 458 | } |
| 459 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 460 | static void raid10_end_write_request(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 462 | struct r10bio *r10_bio = bio->bi_private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 463 | int dev; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 464 | int dec_rdev = 1; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 465 | struct r10conf *conf = r10_bio->mddev->private; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 466 | int slot, repl; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 467 | struct md_rdev *rdev = NULL; |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 468 | struct bio *to_put = NULL; |
Shaohua Li | 579ed34 | 2016-10-06 14:13:52 -0700 | [diff] [blame] | 469 | bool discard_error; |
| 470 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 471 | discard_error = bio->bi_status && bio_op(bio) == REQ_OP_DISCARD; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 473 | dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 475 | if (repl) |
| 476 | rdev = conf->mirrors[dev].replacement; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 477 | if (!rdev) { |
| 478 | smp_rmb(); |
| 479 | repl = 0; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 480 | rdev = conf->mirrors[dev].rdev; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 481 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | /* |
| 483 | * this branch is our 'one mirror IO has finished' event handler: |
| 484 | */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 485 | if (bio->bi_status && !discard_error) { |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 486 | if (repl) |
| 487 | /* Never record new bad blocks to replacement, |
| 488 | * just fail it. |
| 489 | */ |
| 490 | md_error(rdev->mddev, rdev); |
| 491 | else { |
| 492 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 493 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 494 | set_bit(MD_RECOVERY_NEEDED, |
| 495 | &rdev->mddev->recovery); |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 496 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 497 | dec_rdev = 0; |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 498 | if (test_bit(FailFast, &rdev->flags) && |
| 499 | (bio->bi_opf & MD_FAILFAST)) { |
| 500 | md_error(rdev->mddev, rdev); |
| 501 | if (!test_bit(Faulty, &rdev->flags)) |
| 502 | /* This is the only remaining device, |
| 503 | * We need to retry the write without |
| 504 | * FailFast |
| 505 | */ |
| 506 | set_bit(R10BIO_WriteError, &r10_bio->state); |
| 507 | else { |
| 508 | r10_bio->devs[slot].bio = NULL; |
| 509 | to_put = bio; |
| 510 | dec_rdev = 1; |
| 511 | } |
| 512 | } else |
| 513 | set_bit(R10BIO_WriteError, &r10_bio->state); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 514 | } |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 515 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | /* |
| 517 | * Set R10BIO_Uptodate in our master bio, so that |
| 518 | * we will return a good error code for to the higher |
| 519 | * levels even if IO on some other mirrored buffer fails. |
| 520 | * |
| 521 | * The 'master' represents the composite IO operation to |
| 522 | * user-side. So if something waits for IO, then it will |
| 523 | * wait for the 'master' bio. |
| 524 | */ |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 525 | sector_t first_bad; |
| 526 | int bad_sectors; |
| 527 | |
Alex Lyakas | 3056e3a | 2013-06-04 20:42:21 +0300 | [diff] [blame] | 528 | /* |
| 529 | * Do not set R10BIO_Uptodate if the current device is |
| 530 | * rebuilding or Faulty. This is because we cannot use |
| 531 | * such device for properly reading the data back (we could |
| 532 | * potentially use it, if the current write would have felt |
| 533 | * before rdev->recovery_offset, but for simplicity we don't |
| 534 | * check this here. |
| 535 | */ |
| 536 | if (test_bit(In_sync, &rdev->flags) && |
| 537 | !test_bit(Faulty, &rdev->flags)) |
| 538 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 540 | /* Maybe we can clear some bad blocks. */ |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 541 | if (is_badblock(rdev, |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 542 | r10_bio->devs[slot].addr, |
| 543 | r10_bio->sectors, |
Shaohua Li | 579ed34 | 2016-10-06 14:13:52 -0700 | [diff] [blame] | 544 | &first_bad, &bad_sectors) && !discard_error) { |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 545 | bio_put(bio); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 546 | if (repl) |
| 547 | r10_bio->devs[slot].repl_bio = IO_MADE_GOOD; |
| 548 | else |
| 549 | r10_bio->devs[slot].bio = IO_MADE_GOOD; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 550 | dec_rdev = 0; |
| 551 | set_bit(R10BIO_MadeGood, &r10_bio->state); |
| 552 | } |
| 553 | } |
| 554 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 555 | /* |
| 556 | * |
| 557 | * Let's see if all mirrored write operations have finished |
| 558 | * already. |
| 559 | */ |
NeilBrown | 19d5f83 | 2011-09-10 17:21:17 +1000 | [diff] [blame] | 560 | one_write_done(r10_bio); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 561 | if (dec_rdev) |
NeilBrown | 884162d | 2012-11-22 15:12:09 +1100 | [diff] [blame] | 562 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 563 | if (to_put) |
| 564 | bio_put(to_put); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 565 | } |
| 566 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 567 | /* |
| 568 | * RAID10 layout manager |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 569 | * As well as the chunksize and raid_disks count, there are two |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 570 | * parameters: near_copies and far_copies. |
| 571 | * near_copies * far_copies must be <= raid_disks. |
| 572 | * Normally one of these will be 1. |
| 573 | * If both are 1, we get raid0. |
| 574 | * If near_copies == raid_disks, we get raid1. |
| 575 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 576 | * 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] | 577 | * first chunk, followed by near_copies copies of the next chunk and |
| 578 | * so on. |
| 579 | * If far_copies > 1, then after 1/far_copies of the array has been assigned |
| 580 | * as described above, we start again with a device offset of near_copies. |
| 581 | * So we effectively have another copy of the whole array further down all |
| 582 | * the drives, but with blocks on different drives. |
| 583 | * With this layout, and block is never stored twice on the one device. |
| 584 | * |
| 585 | * raid10_find_phys finds the sector offset of a given virtual sector |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 586 | * on each device that it is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 587 | * |
| 588 | * raid10_find_virt does the reverse mapping, from a device and a |
| 589 | * sector offset to a virtual address |
| 590 | */ |
| 591 | |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 592 | static void __raid10_find_phys(struct geom *geo, struct r10bio *r10bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | { |
| 594 | int n,f; |
| 595 | sector_t sector; |
| 596 | sector_t chunk; |
| 597 | sector_t stripe; |
| 598 | int dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | int slot = 0; |
Jonathan Brassow | 9a3152a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 600 | int last_far_set_start, last_far_set_size; |
| 601 | |
| 602 | last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1; |
| 603 | last_far_set_start *= geo->far_set_size; |
| 604 | |
| 605 | last_far_set_size = geo->far_set_size; |
| 606 | last_far_set_size += (geo->raid_disks % geo->far_set_size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 607 | |
| 608 | /* now calculate first sector/dev */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 609 | chunk = r10bio->sector >> geo->chunk_shift; |
| 610 | sector = r10bio->sector & geo->chunk_mask; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 611 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 612 | chunk *= geo->near_copies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 613 | stripe = chunk; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 614 | dev = sector_div(stripe, geo->raid_disks); |
| 615 | if (geo->far_offset) |
| 616 | stripe *= geo->far_copies; |
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 | sector += stripe << geo->chunk_shift; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 619 | |
| 620 | /* and calculate all the others */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 621 | for (n = 0; n < geo->near_copies; n++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 622 | int d = dev; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 623 | int set; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 624 | sector_t s = sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 625 | r10bio->devs[slot].devnum = d; |
Jonathan Brassow | 4c0ca26 | 2013-02-21 13:28:09 +1100 | [diff] [blame] | 626 | r10bio->devs[slot].addr = s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 627 | slot++; |
| 628 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 629 | for (f = 1; f < geo->far_copies; f++) { |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 630 | set = d / geo->far_set_size; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 631 | d += geo->near_copies; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 632 | |
Jonathan Brassow | 9a3152a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 633 | if ((geo->raid_disks % geo->far_set_size) && |
| 634 | (d > last_far_set_start)) { |
| 635 | d -= last_far_set_start; |
| 636 | d %= last_far_set_size; |
| 637 | d += last_far_set_start; |
| 638 | } else { |
| 639 | d %= geo->far_set_size; |
| 640 | d += geo->far_set_size * set; |
| 641 | } |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 642 | s += geo->stride; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | r10bio->devs[slot].devnum = d; |
| 644 | r10bio->devs[slot].addr = s; |
| 645 | slot++; |
| 646 | } |
| 647 | dev++; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 648 | if (dev >= geo->raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | dev = 0; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 650 | sector += (geo->chunk_mask + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | } |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio) |
| 656 | { |
| 657 | struct geom *geo = &conf->geo; |
| 658 | |
| 659 | if (conf->reshape_progress != MaxSector && |
| 660 | ((r10bio->sector >= conf->reshape_progress) != |
| 661 | conf->mddev->reshape_backwards)) { |
| 662 | set_bit(R10BIO_Previous, &r10bio->state); |
| 663 | geo = &conf->prev; |
| 664 | } else |
| 665 | clear_bit(R10BIO_Previous, &r10bio->state); |
| 666 | |
| 667 | __raid10_find_phys(geo, r10bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 668 | } |
| 669 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 670 | 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] | 671 | { |
| 672 | sector_t offset, chunk, vchunk; |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 673 | /* Never use conf->prev as this is only called during resync |
| 674 | * or recovery, so reshape isn't happening |
| 675 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 676 | struct geom *geo = &conf->geo; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 677 | int far_set_start = (dev / geo->far_set_size) * geo->far_set_size; |
| 678 | int far_set_size = geo->far_set_size; |
Jonathan Brassow | 9a3152a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 679 | int last_far_set_start; |
| 680 | |
| 681 | if (geo->raid_disks % geo->far_set_size) { |
| 682 | last_far_set_start = (geo->raid_disks / geo->far_set_size) - 1; |
| 683 | last_far_set_start *= geo->far_set_size; |
| 684 | |
| 685 | if (dev >= last_far_set_start) { |
| 686 | far_set_size = geo->far_set_size; |
| 687 | far_set_size += (geo->raid_disks % geo->far_set_size); |
| 688 | far_set_start = last_far_set_start; |
| 689 | } |
| 690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 692 | offset = sector & geo->chunk_mask; |
| 693 | if (geo->far_offset) { |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 694 | int fc; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 695 | chunk = sector >> geo->chunk_shift; |
| 696 | fc = sector_div(chunk, geo->far_copies); |
| 697 | dev -= fc * geo->near_copies; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 698 | if (dev < far_set_start) |
| 699 | dev += far_set_size; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 700 | } else { |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 701 | while (sector >= geo->stride) { |
| 702 | sector -= geo->stride; |
Jonathan Brassow | 475901a | 2013-02-21 13:28:10 +1100 | [diff] [blame] | 703 | if (dev < (geo->near_copies + far_set_start)) |
| 704 | dev += far_set_size - geo->near_copies; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 705 | else |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 706 | dev -= geo->near_copies; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 707 | } |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 708 | chunk = sector >> geo->chunk_shift; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 709 | } |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 710 | vchunk = chunk * geo->raid_disks + dev; |
| 711 | sector_div(vchunk, geo->near_copies); |
| 712 | return (vchunk << geo->chunk_shift) + offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | /* |
| 716 | * This routine returns the disk from which the requested read should |
| 717 | * be done. There is a per-array 'next expected sequential IO' sector |
| 718 | * number - if this matches on the next IO then we use the last disk. |
| 719 | * There is also a per-disk 'last know head position' sector that is |
| 720 | * maintained from IRQ contexts, both the normal and the resync IO |
| 721 | * completion handlers update this position correctly. If there is no |
| 722 | * perfect sequential match then we pick the disk whose head is closest. |
| 723 | * |
| 724 | * If there are 2 mirrors in the same 2 devices, performance degrades |
| 725 | * because position is mirror, not device based. |
| 726 | * |
| 727 | * The rdev for the device selected will have nr_pending incremented. |
| 728 | */ |
| 729 | |
| 730 | /* |
| 731 | * FIXME: possibly should rethink readbalancing and do it differently |
| 732 | * depending on near_copies / far_copies geometry. |
| 733 | */ |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 734 | static struct md_rdev *read_balance(struct r10conf *conf, |
| 735 | struct r10bio *r10_bio, |
| 736 | int *max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 737 | { |
NeilBrown | af3a2cd | 2010-05-08 08:20:17 +1000 | [diff] [blame] | 738 | const sector_t this_sector = r10_bio->sector; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 739 | int disk, slot; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 740 | int sectors = r10_bio->sectors; |
| 741 | int best_good_sectors; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 742 | sector_t new_distance, best_dist; |
Jonathan Brassow | 3bbae04 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 743 | struct md_rdev *best_rdev, *rdev = NULL; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 744 | int do_balance; |
| 745 | int best_slot; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 746 | struct geom *geo = &conf->geo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 747 | |
| 748 | raid10_find_phys(conf, r10_bio); |
| 749 | rcu_read_lock(); |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 750 | best_slot = -1; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 751 | best_rdev = NULL; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 752 | best_dist = MaxSector; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 753 | best_good_sectors = 0; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 754 | do_balance = 1; |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 755 | clear_bit(R10BIO_FailFast, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 756 | /* |
| 757 | * Check if we can balance. We can balance on the whole |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 758 | * device if no resync is going on (recovery is ok), or below |
| 759 | * the resync window. We take the first readable disk when |
| 760 | * above the resync window. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 761 | */ |
| 762 | if (conf->mddev->recovery_cp < MaxSector |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 763 | && (this_sector + sectors >= conf->next_resync)) |
| 764 | do_balance = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 766 | for (slot = 0; slot < conf->copies ; slot++) { |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 767 | sector_t first_bad; |
| 768 | int bad_sectors; |
| 769 | sector_t dev_sector; |
| 770 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 771 | if (r10_bio->devs[slot].bio == IO_BLOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 772 | continue; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 773 | disk = r10_bio->devs[slot].devnum; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 774 | rdev = rcu_dereference(conf->mirrors[disk].replacement); |
| 775 | if (rdev == NULL || test_bit(Faulty, &rdev->flags) || |
| 776 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) |
| 777 | rdev = rcu_dereference(conf->mirrors[disk].rdev); |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 778 | if (rdev == NULL || |
Kent Overstreet | 8ae1266 | 2015-04-27 23:48:34 -0700 | [diff] [blame] | 779 | test_bit(Faulty, &rdev->flags)) |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 780 | continue; |
| 781 | if (!test_bit(In_sync, &rdev->flags) && |
| 782 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 783 | continue; |
| 784 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 785 | dev_sector = r10_bio->devs[slot].addr; |
| 786 | if (is_badblock(rdev, dev_sector, sectors, |
| 787 | &first_bad, &bad_sectors)) { |
| 788 | if (best_dist < MaxSector) |
| 789 | /* Already have a better slot */ |
| 790 | continue; |
| 791 | if (first_bad <= dev_sector) { |
| 792 | /* Cannot read here. If this is the |
| 793 | * 'primary' device, then we must not read |
| 794 | * beyond 'bad_sectors' from another device. |
| 795 | */ |
| 796 | bad_sectors -= (dev_sector - first_bad); |
| 797 | if (!do_balance && sectors > bad_sectors) |
| 798 | sectors = bad_sectors; |
| 799 | if (best_good_sectors > sectors) |
| 800 | best_good_sectors = sectors; |
| 801 | } else { |
| 802 | sector_t good_sectors = |
| 803 | first_bad - dev_sector; |
| 804 | if (good_sectors > best_good_sectors) { |
| 805 | best_good_sectors = good_sectors; |
| 806 | best_slot = slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 807 | best_rdev = rdev; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 808 | } |
| 809 | if (!do_balance) |
| 810 | /* Must read from here */ |
| 811 | break; |
| 812 | } |
| 813 | continue; |
| 814 | } else |
| 815 | best_good_sectors = sectors; |
| 816 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 817 | if (!do_balance) |
| 818 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 820 | if (best_slot >= 0) |
| 821 | /* At least 2 disks to choose from so failfast is OK */ |
| 822 | set_bit(R10BIO_FailFast, &r10_bio->state); |
NeilBrown | 22dfdf5 | 2005-11-28 13:44:09 -0800 | [diff] [blame] | 823 | /* This optimisation is debatable, and completely destroys |
| 824 | * sequential read speed for 'far copies' arrays. So only |
| 825 | * keep it for 'near' arrays, and review those later. |
| 826 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 827 | if (geo->near_copies > 1 && !atomic_read(&rdev->nr_pending)) |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 828 | new_distance = 0; |
Keld Simonsen | 8ed3a19 | 2008-03-04 14:29:34 -0800 | [diff] [blame] | 829 | |
| 830 | /* for far > 1 always use the lowest address */ |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 831 | else if (geo->far_copies > 1) |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 832 | new_distance = r10_bio->devs[slot].addr; |
Keld Simonsen | 8ed3a19 | 2008-03-04 14:29:34 -0800 | [diff] [blame] | 833 | else |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 834 | new_distance = abs(r10_bio->devs[slot].addr - |
| 835 | conf->mirrors[disk].head_position); |
| 836 | if (new_distance < best_dist) { |
| 837 | best_dist = new_distance; |
| 838 | best_slot = slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 839 | best_rdev = rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 842 | if (slot >= conf->copies) { |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 843 | slot = best_slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 844 | rdev = best_rdev; |
| 845 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 847 | if (slot >= 0) { |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 848 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 849 | r10_bio->read_slot = slot; |
| 850 | } else |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 851 | rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 852 | rcu_read_unlock(); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 853 | *max_sectors = best_good_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 854 | |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 855 | return rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 856 | } |
| 857 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 858 | static int raid10_congested(struct mddev *mddev, int bits) |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 859 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 860 | struct r10conf *conf = mddev->private; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 861 | int i, ret = 0; |
| 862 | |
Tejun Heo | 4452226 | 2015-05-22 17:13:26 -0400 | [diff] [blame] | 863 | if ((bits & (1 << WB_async_congested)) && |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 864 | conf->pending_count >= max_queued_requests) |
| 865 | return 1; |
| 866 | |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 867 | rcu_read_lock(); |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 868 | for (i = 0; |
| 869 | (i < conf->geo.raid_disks || i < conf->prev.raid_disks) |
| 870 | && ret == 0; |
| 871 | i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 872 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 873 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 874 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 875 | |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 876 | ret |= bdi_congested(q->backing_dev_info, bits); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 877 | } |
| 878 | } |
| 879 | rcu_read_unlock(); |
| 880 | return ret; |
| 881 | } |
| 882 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 883 | static void flush_pending_writes(struct r10conf *conf) |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 884 | { |
| 885 | /* Any writes that have been queued but are awaiting |
| 886 | * bitmap updates get flushed here. |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 887 | */ |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 888 | spin_lock_irq(&conf->device_lock); |
| 889 | |
| 890 | if (conf->pending_bio_list.head) { |
| 891 | struct bio *bio; |
| 892 | bio = bio_list_get(&conf->pending_bio_list); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 893 | conf->pending_count = 0; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 894 | spin_unlock_irq(&conf->device_lock); |
| 895 | /* flush any pending bitmap writes to disk |
| 896 | * before proceeding w/ I/O */ |
| 897 | bitmap_unplug(conf->mddev->bitmap); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 898 | wake_up(&conf->wait_barrier); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 899 | |
| 900 | while (bio) { /* submit pending writes */ |
| 901 | struct bio *next = bio->bi_next; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 902 | struct md_rdev *rdev = (void*)bio->bi_disk; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 903 | bio->bi_next = NULL; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 904 | bio_set_dev(bio, rdev->bdev); |
NeilBrown | a9ae93c | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 905 | if (test_bit(Faulty, &rdev->flags)) { |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 906 | bio_io_error(bio); |
NeilBrown | a9ae93c | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 907 | } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) && |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 908 | !blk_queue_discard(bio->bi_disk->queue))) |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 909 | /* Just ignore it */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 910 | bio_endio(bio); |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 911 | else |
| 912 | generic_make_request(bio); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 913 | bio = next; |
| 914 | } |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 915 | } else |
| 916 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 917 | } |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 918 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 919 | /* Barriers.... |
| 920 | * Sometimes we need to suspend IO while we do something else, |
| 921 | * either some resync/recovery, or reconfigure the array. |
| 922 | * To do this we raise a 'barrier'. |
| 923 | * The 'barrier' is a counter that can be raised multiple times |
| 924 | * to count how many activities are happening which preclude |
| 925 | * normal IO. |
| 926 | * We can only raise the barrier if there is no pending IO. |
| 927 | * i.e. if nr_pending == 0. |
| 928 | * We choose only to raise the barrier if no-one is waiting for the |
| 929 | * barrier to go down. This means that as soon as an IO request |
| 930 | * is ready, no other operations which require a barrier will start |
| 931 | * until the IO request has had a chance. |
| 932 | * |
| 933 | * So: regular IO calls 'wait_barrier'. When that returns there |
| 934 | * is no backgroup IO happening, It must arrange to call |
| 935 | * allow_barrier when it has finished its IO. |
| 936 | * backgroup IO calls must call raise_barrier. Once that returns |
| 937 | * there is no normal IO happeing. It must arrange to call |
| 938 | * lower_barrier when the particular background IO completes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 939 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 940 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 941 | static void raise_barrier(struct r10conf *conf, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 942 | { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 943 | BUG_ON(force && !conf->barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 944 | spin_lock_irq(&conf->resync_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 945 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 946 | /* Wait until no block IO is waiting (unless 'force') */ |
| 947 | wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 948 | conf->resync_lock); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 949 | |
| 950 | /* block any new IO from starting */ |
| 951 | conf->barrier++; |
| 952 | |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 953 | /* Now wait for all pending IO to complete */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 954 | wait_event_lock_irq(conf->wait_barrier, |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 955 | !atomic_read(&conf->nr_pending) && conf->barrier < RESYNC_DEPTH, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 956 | conf->resync_lock); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 957 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 958 | spin_unlock_irq(&conf->resync_lock); |
| 959 | } |
| 960 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 961 | static void lower_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 962 | { |
| 963 | unsigned long flags; |
| 964 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 965 | conf->barrier--; |
| 966 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 967 | wake_up(&conf->wait_barrier); |
| 968 | } |
| 969 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 970 | static void wait_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 971 | { |
| 972 | spin_lock_irq(&conf->resync_lock); |
| 973 | if (conf->barrier) { |
| 974 | conf->nr_waiting++; |
NeilBrown | d6b42dc | 2012-03-19 12:46:38 +1100 | [diff] [blame] | 975 | /* Wait for the barrier to drop. |
| 976 | * However if there are already pending |
| 977 | * requests (preventing the barrier from |
| 978 | * rising completely), and the |
| 979 | * pre-process bio queue isn't empty, |
| 980 | * then don't wait, as we need to empty |
| 981 | * that queue to get the nr_pending |
| 982 | * count down. |
| 983 | */ |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 984 | raid10_log(conf->mddev, "wait barrier"); |
NeilBrown | d6b42dc | 2012-03-19 12:46:38 +1100 | [diff] [blame] | 985 | wait_event_lock_irq(conf->wait_barrier, |
| 986 | !conf->barrier || |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 987 | (atomic_read(&conf->nr_pending) && |
NeilBrown | d6b42dc | 2012-03-19 12:46:38 +1100 | [diff] [blame] | 988 | current->bio_list && |
NeilBrown | f5fe1b5 | 2017-03-10 17:00:47 +1100 | [diff] [blame] | 989 | (!bio_list_empty(¤t->bio_list[0]) || |
| 990 | !bio_list_empty(¤t->bio_list[1]))), |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 991 | conf->resync_lock); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 992 | conf->nr_waiting--; |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 993 | if (!conf->nr_waiting) |
| 994 | wake_up(&conf->wait_barrier); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 995 | } |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 996 | atomic_inc(&conf->nr_pending); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 997 | spin_unlock_irq(&conf->resync_lock); |
| 998 | } |
| 999 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1000 | static void allow_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1001 | { |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1002 | if ((atomic_dec_and_test(&conf->nr_pending)) || |
| 1003 | (conf->array_freeze_pending)) |
| 1004 | wake_up(&conf->wait_barrier); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1005 | } |
| 1006 | |
NeilBrown | e2d5992 | 2013-06-12 11:01:22 +1000 | [diff] [blame] | 1007 | static void freeze_array(struct r10conf *conf, int extra) |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1008 | { |
| 1009 | /* stop syncio and normal IO and wait for everything to |
NeilBrown | f188593 | 2006-01-06 00:20:42 -0800 | [diff] [blame] | 1010 | * go quiet. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1011 | * We increment barrier and nr_waiting, and then |
NeilBrown | e2d5992 | 2013-06-12 11:01:22 +1000 | [diff] [blame] | 1012 | * wait until nr_pending match nr_queued+extra |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 1013 | * This is called in the context of one normal IO request |
| 1014 | * that has failed. Thus any sync request that might be pending |
| 1015 | * will be blocked by nr_pending, and we need to wait for |
| 1016 | * pending IO requests to complete or be queued for re-try. |
NeilBrown | e2d5992 | 2013-06-12 11:01:22 +1000 | [diff] [blame] | 1017 | * Thus the number queued (nr_queued) plus this request (extra) |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 1018 | * must match the number of pending IOs (nr_pending) before |
| 1019 | * we continue. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1020 | */ |
| 1021 | spin_lock_irq(&conf->resync_lock); |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1022 | conf->array_freeze_pending++; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1023 | conf->barrier++; |
| 1024 | conf->nr_waiting++; |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1025 | wait_event_lock_irq_cmd(conf->wait_barrier, |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1026 | atomic_read(&conf->nr_pending) == conf->nr_queued+extra, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1027 | conf->resync_lock, |
| 1028 | flush_pending_writes(conf)); |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1029 | |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 1030 | conf->array_freeze_pending--; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1031 | spin_unlock_irq(&conf->resync_lock); |
| 1032 | } |
| 1033 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1034 | static void unfreeze_array(struct r10conf *conf) |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1035 | { |
| 1036 | /* reverse the effect of the freeze */ |
| 1037 | spin_lock_irq(&conf->resync_lock); |
| 1038 | conf->barrier--; |
| 1039 | conf->nr_waiting--; |
| 1040 | wake_up(&conf->wait_barrier); |
| 1041 | spin_unlock_irq(&conf->resync_lock); |
| 1042 | } |
| 1043 | |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 1044 | static sector_t choose_data_offset(struct r10bio *r10_bio, |
| 1045 | struct md_rdev *rdev) |
| 1046 | { |
| 1047 | if (!test_bit(MD_RECOVERY_RESHAPE, &rdev->mddev->recovery) || |
| 1048 | test_bit(R10BIO_Previous, &r10_bio->state)) |
| 1049 | return rdev->data_offset; |
| 1050 | else |
| 1051 | return rdev->new_data_offset; |
| 1052 | } |
| 1053 | |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1054 | struct raid10_plug_cb { |
| 1055 | struct blk_plug_cb cb; |
| 1056 | struct bio_list pending; |
| 1057 | int pending_cnt; |
| 1058 | }; |
| 1059 | |
| 1060 | static void raid10_unplug(struct blk_plug_cb *cb, bool from_schedule) |
| 1061 | { |
| 1062 | struct raid10_plug_cb *plug = container_of(cb, struct raid10_plug_cb, |
| 1063 | cb); |
| 1064 | struct mddev *mddev = plug->cb.data; |
| 1065 | struct r10conf *conf = mddev->private; |
| 1066 | struct bio *bio; |
| 1067 | |
NeilBrown | 874807a | 2012-11-27 12:14:40 +1100 | [diff] [blame] | 1068 | if (from_schedule || current->bio_list) { |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1069 | spin_lock_irq(&conf->device_lock); |
| 1070 | bio_list_merge(&conf->pending_bio_list, &plug->pending); |
| 1071 | conf->pending_count += plug->pending_cnt; |
| 1072 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ee0b024 | 2013-02-25 12:38:29 +1100 | [diff] [blame] | 1073 | wake_up(&conf->wait_barrier); |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1074 | md_wakeup_thread(mddev->thread); |
| 1075 | kfree(plug); |
| 1076 | return; |
| 1077 | } |
| 1078 | |
| 1079 | /* we aren't scheduling, so we can do the write-out directly. */ |
| 1080 | bio = bio_list_get(&plug->pending); |
| 1081 | bitmap_unplug(mddev->bitmap); |
| 1082 | wake_up(&conf->wait_barrier); |
| 1083 | |
| 1084 | while (bio) { /* submit pending writes */ |
| 1085 | struct bio *next = bio->bi_next; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1086 | struct md_rdev *rdev = (void*)bio->bi_disk; |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1087 | bio->bi_next = NULL; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1088 | bio_set_dev(bio, rdev->bdev); |
NeilBrown | a9ae93c | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 1089 | if (test_bit(Faulty, &rdev->flags)) { |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 1090 | bio_io_error(bio); |
NeilBrown | a9ae93c | 2016-11-04 16:46:03 +1100 | [diff] [blame] | 1091 | } else if (unlikely((bio_op(bio) == REQ_OP_DISCARD) && |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1092 | !blk_queue_discard(bio->bi_disk->queue))) |
Shaohua Li | 32f9f57 | 2013-04-28 18:26:38 +0800 | [diff] [blame] | 1093 | /* Just ignore it */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1094 | bio_endio(bio); |
Shaohua Li | 32f9f57 | 2013-04-28 18:26:38 +0800 | [diff] [blame] | 1095 | else |
| 1096 | generic_make_request(bio); |
NeilBrown | 57c67df | 2012-10-11 13:32:13 +1100 | [diff] [blame] | 1097 | bio = next; |
| 1098 | } |
| 1099 | kfree(plug); |
| 1100 | } |
| 1101 | |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1102 | static void raid10_read_request(struct mddev *mddev, struct bio *bio, |
| 1103 | struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1104 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1105 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1106 | struct bio *read_bio; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1107 | const int op = bio_op(bio); |
| 1108 | const unsigned long do_sync = (bio->bi_opf & REQ_SYNC); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1109 | int max_sectors; |
| 1110 | sector_t sectors; |
| 1111 | struct md_rdev *rdev; |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1112 | char b[BDEVNAME_SIZE]; |
| 1113 | int slot = r10_bio->read_slot; |
| 1114 | struct md_rdev *err_rdev = NULL; |
| 1115 | gfp_t gfp = GFP_NOIO; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1116 | |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1117 | if (r10_bio->devs[slot].rdev) { |
| 1118 | /* |
| 1119 | * This is an error retry, but we cannot |
| 1120 | * safely dereference the rdev in the r10_bio, |
| 1121 | * we must use the one in conf. |
| 1122 | * If it has already been disconnected (unlikely) |
| 1123 | * we lose the device name in error messages. |
| 1124 | */ |
| 1125 | int disk; |
| 1126 | /* |
| 1127 | * As we are blocking raid10, it is a little safer to |
| 1128 | * use __GFP_HIGH. |
| 1129 | */ |
| 1130 | gfp = GFP_NOIO | __GFP_HIGH; |
| 1131 | |
| 1132 | rcu_read_lock(); |
| 1133 | disk = r10_bio->devs[slot].devnum; |
| 1134 | err_rdev = rcu_dereference(conf->mirrors[disk].rdev); |
| 1135 | if (err_rdev) |
| 1136 | bdevname(err_rdev->bdev, b); |
| 1137 | else { |
| 1138 | strcpy(b, "???"); |
| 1139 | /* This never gets dereferenced */ |
| 1140 | err_rdev = r10_bio->devs[slot].rdev; |
| 1141 | } |
| 1142 | rcu_read_unlock(); |
| 1143 | } |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1144 | /* |
| 1145 | * Register the new request and wait if the reconstruction |
| 1146 | * thread has put up a bar for new requests. |
| 1147 | * Continue immediately if no resync is active currently. |
| 1148 | */ |
| 1149 | wait_barrier(conf); |
| 1150 | |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1151 | sectors = r10_bio->sectors; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1152 | while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
| 1153 | bio->bi_iter.bi_sector < conf->reshape_progress && |
| 1154 | bio->bi_iter.bi_sector + sectors > conf->reshape_progress) { |
| 1155 | /* |
| 1156 | * IO spans the reshape position. Need to wait for reshape to |
| 1157 | * pass |
| 1158 | */ |
| 1159 | raid10_log(conf->mddev, "wait reshape"); |
| 1160 | allow_barrier(conf); |
| 1161 | wait_event(conf->wait_barrier, |
| 1162 | conf->reshape_progress <= bio->bi_iter.bi_sector || |
| 1163 | conf->reshape_progress >= bio->bi_iter.bi_sector + |
| 1164 | sectors); |
| 1165 | wait_barrier(conf); |
| 1166 | } |
| 1167 | |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1168 | rdev = read_balance(conf, r10_bio, &max_sectors); |
| 1169 | if (!rdev) { |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1170 | if (err_rdev) { |
| 1171 | pr_crit_ratelimited("md/raid10:%s: %s: unrecoverable I/O read error for block %llu\n", |
| 1172 | mdname(mddev), b, |
| 1173 | (unsigned long long)r10_bio->sector); |
| 1174 | } |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1175 | raid_end_bio_io(r10_bio); |
| 1176 | return; |
| 1177 | } |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1178 | if (err_rdev) |
| 1179 | pr_err_ratelimited("md/raid10:%s: %s: redirecting sector %llu to another mirror\n", |
| 1180 | mdname(mddev), |
| 1181 | bdevname(rdev->bdev, b), |
| 1182 | (unsigned long long)r10_bio->sector); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1183 | if (max_sectors < bio_sectors(bio)) { |
| 1184 | struct bio *split = bio_split(bio, max_sectors, |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1185 | gfp, conf->bio_split); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1186 | bio_chain(split, bio); |
| 1187 | generic_make_request(bio); |
| 1188 | bio = split; |
| 1189 | r10_bio->master_bio = bio; |
| 1190 | r10_bio->sectors = max_sectors; |
| 1191 | } |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1192 | slot = r10_bio->read_slot; |
| 1193 | |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1194 | read_bio = bio_clone_fast(bio, gfp, mddev->bio_set); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1195 | |
| 1196 | r10_bio->devs[slot].bio = read_bio; |
| 1197 | r10_bio->devs[slot].rdev = rdev; |
| 1198 | |
| 1199 | read_bio->bi_iter.bi_sector = r10_bio->devs[slot].addr + |
| 1200 | choose_data_offset(r10_bio, rdev); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1201 | bio_set_dev(read_bio, rdev->bdev); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1202 | read_bio->bi_end_io = raid10_end_read_request; |
| 1203 | bio_set_op_attrs(read_bio, op, do_sync); |
| 1204 | if (test_bit(FailFast, &rdev->flags) && |
| 1205 | test_bit(R10BIO_FailFast, &r10_bio->state)) |
| 1206 | read_bio->bi_opf |= MD_FAILFAST; |
| 1207 | read_bio->bi_private = r10_bio; |
| 1208 | |
| 1209 | if (mddev->gendisk) |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1210 | trace_block_bio_remap(read_bio->bi_disk->queue, |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1211 | read_bio, disk_devt(mddev->gendisk), |
| 1212 | r10_bio->sector); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1213 | generic_make_request(read_bio); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1214 | return; |
| 1215 | } |
| 1216 | |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1217 | static void raid10_write_one_disk(struct mddev *mddev, struct r10bio *r10_bio, |
| 1218 | struct bio *bio, bool replacement, |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1219 | int n_copy) |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1220 | { |
| 1221 | const int op = bio_op(bio); |
| 1222 | const unsigned long do_sync = (bio->bi_opf & REQ_SYNC); |
| 1223 | const unsigned long do_fua = (bio->bi_opf & REQ_FUA); |
| 1224 | unsigned long flags; |
| 1225 | struct blk_plug_cb *cb; |
| 1226 | struct raid10_plug_cb *plug = NULL; |
| 1227 | struct r10conf *conf = mddev->private; |
| 1228 | struct md_rdev *rdev; |
| 1229 | int devnum = r10_bio->devs[n_copy].devnum; |
| 1230 | struct bio *mbio; |
| 1231 | |
| 1232 | if (replacement) { |
| 1233 | rdev = conf->mirrors[devnum].replacement; |
| 1234 | if (rdev == NULL) { |
| 1235 | /* Replacement just got moved to main 'rdev' */ |
| 1236 | smp_mb(); |
| 1237 | rdev = conf->mirrors[devnum].rdev; |
| 1238 | } |
| 1239 | } else |
| 1240 | rdev = conf->mirrors[devnum].rdev; |
| 1241 | |
| 1242 | mbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1243 | if (replacement) |
| 1244 | r10_bio->devs[n_copy].repl_bio = mbio; |
| 1245 | else |
| 1246 | r10_bio->devs[n_copy].bio = mbio; |
| 1247 | |
| 1248 | mbio->bi_iter.bi_sector = (r10_bio->devs[n_copy].addr + |
| 1249 | choose_data_offset(r10_bio, rdev)); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1250 | bio_set_dev(mbio, rdev->bdev); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1251 | mbio->bi_end_io = raid10_end_write_request; |
| 1252 | bio_set_op_attrs(mbio, op, do_sync | do_fua); |
| 1253 | if (!replacement && test_bit(FailFast, |
| 1254 | &conf->mirrors[devnum].rdev->flags) |
| 1255 | && enough(conf, devnum)) |
| 1256 | mbio->bi_opf |= MD_FAILFAST; |
| 1257 | mbio->bi_private = r10_bio; |
| 1258 | |
| 1259 | if (conf->mddev->gendisk) |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1260 | trace_block_bio_remap(mbio->bi_disk->queue, |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1261 | mbio, disk_devt(conf->mddev->gendisk), |
| 1262 | r10_bio->sector); |
| 1263 | /* flush_pending_writes() needs access to the rdev so...*/ |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1264 | mbio->bi_disk = (void *)rdev; |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1265 | |
| 1266 | atomic_inc(&r10_bio->remaining); |
| 1267 | |
| 1268 | cb = blk_check_plugged(raid10_unplug, mddev, sizeof(*plug)); |
| 1269 | if (cb) |
| 1270 | plug = container_of(cb, struct raid10_plug_cb, cb); |
| 1271 | else |
| 1272 | plug = NULL; |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1273 | if (plug) { |
| 1274 | bio_list_add(&plug->pending, mbio); |
| 1275 | plug->pending_cnt++; |
| 1276 | } else { |
Shaohua Li | 23b245c | 2017-05-10 08:47:11 -0700 | [diff] [blame] | 1277 | spin_lock_irqsave(&conf->device_lock, flags); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1278 | bio_list_add(&conf->pending_bio_list, mbio); |
| 1279 | conf->pending_count++; |
Shaohua Li | 23b245c | 2017-05-10 08:47:11 -0700 | [diff] [blame] | 1280 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1281 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 23b245c | 2017-05-10 08:47:11 -0700 | [diff] [blame] | 1282 | } |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1283 | } |
| 1284 | |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1285 | static void raid10_write_request(struct mddev *mddev, struct bio *bio, |
| 1286 | struct r10bio *r10_bio) |
| 1287 | { |
| 1288 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1289 | int i; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1290 | struct md_rdev *blocked_rdev; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1291 | sector_t sectors; |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1292 | int max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | |
NeilBrown | cc13b1d | 2014-05-05 13:34:37 +1000 | [diff] [blame] | 1294 | /* |
| 1295 | * Register the new request and wait if the reconstruction |
| 1296 | * thread has put up a bar for new requests. |
| 1297 | * Continue immediately if no resync is active currently. |
| 1298 | */ |
| 1299 | wait_barrier(conf); |
| 1300 | |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1301 | sectors = r10_bio->sectors; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1302 | while (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1303 | bio->bi_iter.bi_sector < conf->reshape_progress && |
| 1304 | bio->bi_iter.bi_sector + sectors > conf->reshape_progress) { |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1305 | /* |
| 1306 | * IO spans the reshape position. Need to wait for reshape to |
| 1307 | * pass |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1308 | */ |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1309 | raid10_log(conf->mddev, "wait reshape"); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1310 | allow_barrier(conf); |
| 1311 | wait_event(conf->wait_barrier, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1312 | conf->reshape_progress <= bio->bi_iter.bi_sector || |
| 1313 | conf->reshape_progress >= bio->bi_iter.bi_sector + |
| 1314 | sectors); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1315 | wait_barrier(conf); |
| 1316 | } |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1317 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1318 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1319 | (mddev->reshape_backwards |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1320 | ? (bio->bi_iter.bi_sector < conf->reshape_safe && |
| 1321 | bio->bi_iter.bi_sector + sectors > conf->reshape_progress) |
| 1322 | : (bio->bi_iter.bi_sector + sectors > conf->reshape_safe && |
| 1323 | bio->bi_iter.bi_sector < conf->reshape_progress))) { |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1324 | /* Need to update reshape_position in metadata */ |
| 1325 | mddev->reshape_position = conf->reshape_progress; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1326 | set_mask_bits(&mddev->sb_flags, 0, |
| 1327 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1328 | md_wakeup_thread(mddev->thread); |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1329 | raid10_log(conf->mddev, "wait reshape metadata"); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1330 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1331 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 1332 | |
| 1333 | conf->reshape_safe = mddev->reshape_position; |
| 1334 | } |
| 1335 | |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 1336 | if (conf->pending_count >= max_queued_requests) { |
| 1337 | md_wakeup_thread(mddev->thread); |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1338 | raid10_log(mddev, "wait queued"); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 1339 | wait_event(conf->wait_barrier, |
| 1340 | conf->pending_count < max_queued_requests); |
| 1341 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1342 | /* first select target devices under rcu_lock and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1343 | * inc refcount on their rdev. Record them by setting |
| 1344 | * bios[x] to bio |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1345 | * If there are known/acknowledged bad blocks on any device |
| 1346 | * on which we have seen a write error, we want to avoid |
| 1347 | * writing to those blocks. This potentially requires several |
| 1348 | * writes to write around the bad blocks. Each set of writes |
NeilBrown | fd16f2e | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 1349 | * gets its own r10_bio with a set of bios attached. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | */ |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1351 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1352 | r10_bio->read_slot = -1; /* make sure repl_bio gets freed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1353 | raid10_find_phys(conf, r10_bio); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1354 | retry_write: |
Harvey Harrison | cb6969e | 2008-05-06 20:42:32 -0700 | [diff] [blame] | 1355 | blocked_rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | rcu_read_lock(); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1357 | max_sectors = r10_bio->sectors; |
| 1358 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | for (i = 0; i < conf->copies; i++) { |
| 1360 | int d = r10_bio->devs[i].devnum; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1361 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1362 | struct md_rdev *rrdev = rcu_dereference( |
| 1363 | conf->mirrors[d].replacement); |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1364 | if (rdev == rrdev) |
| 1365 | rrdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1366 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
| 1367 | atomic_inc(&rdev->nr_pending); |
| 1368 | blocked_rdev = rdev; |
| 1369 | break; |
| 1370 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1371 | if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) { |
| 1372 | atomic_inc(&rrdev->nr_pending); |
| 1373 | blocked_rdev = rrdev; |
| 1374 | break; |
| 1375 | } |
Kent Overstreet | 8ae1266 | 2015-04-27 23:48:34 -0700 | [diff] [blame] | 1376 | if (rdev && (test_bit(Faulty, &rdev->flags))) |
NeilBrown | e7c0c3f | 2012-11-22 14:42:49 +1100 | [diff] [blame] | 1377 | rdev = NULL; |
Kent Overstreet | 8ae1266 | 2015-04-27 23:48:34 -0700 | [diff] [blame] | 1378 | if (rrdev && (test_bit(Faulty, &rrdev->flags))) |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1379 | rrdev = NULL; |
| 1380 | |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1381 | r10_bio->devs[i].bio = NULL; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1382 | r10_bio->devs[i].repl_bio = NULL; |
NeilBrown | e7c0c3f | 2012-11-22 14:42:49 +1100 | [diff] [blame] | 1383 | |
| 1384 | if (!rdev && !rrdev) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1385 | set_bit(R10BIO_Degraded, &r10_bio->state); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1386 | continue; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1387 | } |
NeilBrown | e7c0c3f | 2012-11-22 14:42:49 +1100 | [diff] [blame] | 1388 | if (rdev && test_bit(WriteErrorSeen, &rdev->flags)) { |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1389 | sector_t first_bad; |
| 1390 | sector_t dev_sector = r10_bio->devs[i].addr; |
| 1391 | int bad_sectors; |
| 1392 | int is_bad; |
| 1393 | |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1394 | is_bad = is_badblock(rdev, dev_sector, max_sectors, |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1395 | &first_bad, &bad_sectors); |
| 1396 | if (is_bad < 0) { |
| 1397 | /* Mustn't write here until the bad block |
| 1398 | * is acknowledged |
| 1399 | */ |
| 1400 | atomic_inc(&rdev->nr_pending); |
| 1401 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 1402 | blocked_rdev = rdev; |
| 1403 | break; |
| 1404 | } |
| 1405 | if (is_bad && first_bad <= dev_sector) { |
| 1406 | /* Cannot write here at all */ |
| 1407 | bad_sectors -= (dev_sector - first_bad); |
| 1408 | if (bad_sectors < max_sectors) |
| 1409 | /* Mustn't write more than bad_sectors |
| 1410 | * to other devices yet |
| 1411 | */ |
| 1412 | max_sectors = bad_sectors; |
| 1413 | /* We don't set R10BIO_Degraded as that |
| 1414 | * only applies if the disk is missing, |
| 1415 | * so it might be re-added, and we want to |
| 1416 | * know to recover this chunk. |
| 1417 | * In this case the device is here, and the |
| 1418 | * fact that this chunk is not in-sync is |
| 1419 | * recorded in the bad block log. |
| 1420 | */ |
| 1421 | continue; |
| 1422 | } |
| 1423 | if (is_bad) { |
| 1424 | int good_sectors = first_bad - dev_sector; |
| 1425 | if (good_sectors < max_sectors) |
| 1426 | max_sectors = good_sectors; |
| 1427 | } |
| 1428 | } |
NeilBrown | e7c0c3f | 2012-11-22 14:42:49 +1100 | [diff] [blame] | 1429 | if (rdev) { |
| 1430 | r10_bio->devs[i].bio = bio; |
| 1431 | atomic_inc(&rdev->nr_pending); |
| 1432 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1433 | if (rrdev) { |
| 1434 | r10_bio->devs[i].repl_bio = bio; |
| 1435 | atomic_inc(&rrdev->nr_pending); |
| 1436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1437 | } |
| 1438 | rcu_read_unlock(); |
| 1439 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1440 | if (unlikely(blocked_rdev)) { |
| 1441 | /* Have to wait for this device to get unblocked, then retry */ |
| 1442 | int j; |
| 1443 | int d; |
| 1444 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1445 | for (j = 0; j < i; j++) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1446 | if (r10_bio->devs[j].bio) { |
| 1447 | d = r10_bio->devs[j].devnum; |
| 1448 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); |
| 1449 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1450 | if (r10_bio->devs[j].repl_bio) { |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1451 | struct md_rdev *rdev; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1452 | d = r10_bio->devs[j].devnum; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1453 | rdev = conf->mirrors[d].replacement; |
| 1454 | if (!rdev) { |
| 1455 | /* Race with remove_disk */ |
| 1456 | smp_mb(); |
| 1457 | rdev = conf->mirrors[d].rdev; |
| 1458 | } |
| 1459 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1460 | } |
| 1461 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1462 | allow_barrier(conf); |
NeilBrown | 578b54a | 2016-11-14 16:30:21 +1100 | [diff] [blame] | 1463 | raid10_log(conf->mddev, "wait rdev %d blocked", blocked_rdev->raid_disk); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1464 | md_wait_for_blocked_rdev(blocked_rdev, mddev); |
| 1465 | wait_barrier(conf); |
| 1466 | goto retry_write; |
| 1467 | } |
| 1468 | |
NeilBrown | 6b6c811 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 1469 | if (max_sectors < r10_bio->sectors) |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1470 | r10_bio->sectors = max_sectors; |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1471 | |
| 1472 | if (r10_bio->sectors < bio_sectors(bio)) { |
| 1473 | struct bio *split = bio_split(bio, r10_bio->sectors, |
| 1474 | GFP_NOIO, conf->bio_split); |
| 1475 | bio_chain(split, bio); |
| 1476 | generic_make_request(bio); |
| 1477 | bio = split; |
| 1478 | r10_bio->master_bio = bio; |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1479 | } |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1480 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1481 | atomic_set(&r10_bio->remaining, 1); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1482 | bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1483 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1484 | for (i = 0; i < conf->copies; i++) { |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1485 | if (r10_bio->devs[i].bio) |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1486 | raid10_write_one_disk(mddev, r10_bio, bio, false, i); |
Guoqing Jiang | 27f26a0 | 2017-03-20 17:46:04 +0800 | [diff] [blame] | 1487 | if (r10_bio->devs[i].repl_bio) |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1488 | raid10_write_one_disk(mddev, r10_bio, bio, true, i); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1489 | } |
NeilBrown | 079fa16 | 2011-09-10 17:21:23 +1000 | [diff] [blame] | 1490 | one_write_done(r10_bio); |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1491 | } |
| 1492 | |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1493 | static void __make_request(struct mddev *mddev, struct bio *bio, int sectors) |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1494 | { |
| 1495 | struct r10conf *conf = mddev->private; |
| 1496 | struct r10bio *r10_bio; |
| 1497 | |
| 1498 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
| 1499 | |
| 1500 | r10_bio->master_bio = bio; |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1501 | r10_bio->sectors = sectors; |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1502 | |
| 1503 | r10_bio->mddev = mddev; |
| 1504 | r10_bio->sector = bio->bi_iter.bi_sector; |
| 1505 | r10_bio->state = 0; |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1506 | memset(r10_bio->devs, 0, sizeof(r10_bio->devs[0]) * conf->copies); |
Robert LeBlanc | bb5f1ed | 2016-12-05 13:02:58 -0700 | [diff] [blame] | 1507 | |
| 1508 | if (bio_data_dir(bio) == READ) |
| 1509 | raid10_read_request(mddev, bio, r10_bio); |
| 1510 | else |
| 1511 | raid10_write_request(mddev, bio, r10_bio); |
| 1512 | } |
| 1513 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 1514 | static bool raid10_make_request(struct mddev *mddev, struct bio *bio) |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1515 | { |
| 1516 | struct r10conf *conf = mddev->private; |
| 1517 | sector_t chunk_mask = (conf->geo.chunk_mask & conf->prev.chunk_mask); |
| 1518 | int chunk_sects = chunk_mask + 1; |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1519 | int sectors = bio_sectors(bio); |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1520 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1521 | if (unlikely(bio->bi_opf & REQ_PREFLUSH)) { |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1522 | md_flush_request(mddev, bio); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 1523 | return true; |
Kent Overstreet | 20d0189 | 2013-11-23 18:21:01 -0800 | [diff] [blame] | 1524 | } |
| 1525 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 1526 | if (!md_write_start(mddev, bio)) |
| 1527 | return false; |
| 1528 | |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 1529 | /* |
| 1530 | * If this request crosses a chunk boundary, we need to split |
| 1531 | * it. |
| 1532 | */ |
| 1533 | if (unlikely((bio->bi_iter.bi_sector & chunk_mask) + |
| 1534 | sectors > chunk_sects |
| 1535 | && (conf->geo.near_copies < conf->geo.raid_disks |
| 1536 | || conf->prev.near_copies < |
| 1537 | conf->prev.raid_disks))) |
| 1538 | sectors = chunk_sects - |
| 1539 | (bio->bi_iter.bi_sector & |
| 1540 | (chunk_sects - 1)); |
| 1541 | __make_request(mddev, bio, sectors); |
NeilBrown | 079fa16 | 2011-09-10 17:21:23 +1000 | [diff] [blame] | 1542 | |
| 1543 | /* In case raid10d snuck in to freeze_array */ |
| 1544 | wake_up(&conf->wait_barrier); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 1545 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | } |
| 1547 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 1548 | static void raid10_status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1549 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1550 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | int i; |
| 1552 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1553 | if (conf->geo.near_copies < conf->geo.raid_disks) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1554 | seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1555 | if (conf->geo.near_copies > 1) |
| 1556 | seq_printf(seq, " %d near-copies", conf->geo.near_copies); |
| 1557 | if (conf->geo.far_copies > 1) { |
| 1558 | if (conf->geo.far_offset) |
| 1559 | seq_printf(seq, " %d offset-copies", conf->geo.far_copies); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1560 | else |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1561 | seq_printf(seq, " %d far-copies", conf->geo.far_copies); |
NeilBrown | 8bce6d3 | 2015-10-22 13:20:15 +1100 | [diff] [blame] | 1562 | if (conf->geo.far_set_size != conf->geo.raid_disks) |
| 1563 | seq_printf(seq, " %d devices per set", conf->geo.far_set_size); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1564 | } |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1565 | seq_printf(seq, " [%d/%d] [", conf->geo.raid_disks, |
| 1566 | conf->geo.raid_disks - mddev->degraded); |
NeilBrown | d44b0a9 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 1567 | rcu_read_lock(); |
| 1568 | for (i = 0; i < conf->geo.raid_disks; i++) { |
| 1569 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 1570 | seq_printf(seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 1571 | } |
| 1572 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1573 | seq_printf(seq, "]"); |
| 1574 | } |
| 1575 | |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1576 | /* check if there are enough drives for |
| 1577 | * every block to appear on atleast one. |
| 1578 | * Don't consider the device numbered 'ignore' |
| 1579 | * as we might be about to remove it. |
| 1580 | */ |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1581 | static int _enough(struct r10conf *conf, int previous, int ignore) |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1582 | { |
| 1583 | int first = 0; |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1584 | int has_enough = 0; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1585 | int disks, ncopies; |
| 1586 | if (previous) { |
| 1587 | disks = conf->prev.raid_disks; |
| 1588 | ncopies = conf->prev.near_copies; |
| 1589 | } else { |
| 1590 | disks = conf->geo.raid_disks; |
| 1591 | ncopies = conf->geo.near_copies; |
| 1592 | } |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1593 | |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1594 | rcu_read_lock(); |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1595 | do { |
| 1596 | int n = conf->copies; |
| 1597 | int cnt = 0; |
NeilBrown | 80b4812 | 2012-09-27 12:35:21 +1000 | [diff] [blame] | 1598 | int this = first; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1599 | while (n--) { |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1600 | struct md_rdev *rdev; |
| 1601 | if (this != ignore && |
| 1602 | (rdev = rcu_dereference(conf->mirrors[this].rdev)) && |
| 1603 | test_bit(In_sync, &rdev->flags)) |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1604 | cnt++; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1605 | this = (this+1) % disks; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1606 | } |
| 1607 | if (cnt == 0) |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1608 | goto out; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1609 | first = (first + ncopies) % disks; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1610 | } while (first != 0); |
NeilBrown | 725d6e5 | 2013-06-11 15:08:03 +1000 | [diff] [blame] | 1611 | has_enough = 1; |
| 1612 | out: |
| 1613 | rcu_read_unlock(); |
| 1614 | return has_enough; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1615 | } |
| 1616 | |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 1617 | static int enough(struct r10conf *conf, int ignore) |
| 1618 | { |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1619 | /* when calling 'enough', both 'prev' and 'geo' must |
| 1620 | * be stable. |
| 1621 | * This is ensured if ->reconfig_mutex or ->device_lock |
| 1622 | * is held. |
| 1623 | */ |
| 1624 | return _enough(conf, 0, ignore) && |
| 1625 | _enough(conf, 1, ignore); |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 1626 | } |
| 1627 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 1628 | static void raid10_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1629 | { |
| 1630 | char b[BDEVNAME_SIZE]; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1631 | struct r10conf *conf = mddev->private; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1632 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | |
| 1634 | /* |
| 1635 | * If it is not operational, then we have already marked it as dead |
| 1636 | * else if it is the last working disks, ignore the error, let the |
| 1637 | * next level up know. |
| 1638 | * else mark the drive as failed |
| 1639 | */ |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1640 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1641 | if (test_bit(In_sync, &rdev->flags) |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1642 | && !enough(conf, rdev->raid_disk)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1643 | /* |
| 1644 | * Don't fail the drive, just return an IO error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1645 | */ |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1646 | spin_unlock_irqrestore(&conf->device_lock, flags); |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1647 | return; |
| 1648 | } |
NeilBrown | 2446dba | 2014-07-31 10:16:29 +1000 | [diff] [blame] | 1649 | if (test_and_clear_bit(In_sync, &rdev->flags)) |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1650 | mddev->degraded++; |
NeilBrown | 2446dba | 2014-07-31 10:16:29 +1000 | [diff] [blame] | 1651 | /* |
| 1652 | * If recovery is running, make sure it aborts. |
| 1653 | */ |
| 1654 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1655 | set_bit(Blocked, &rdev->flags); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1656 | set_bit(Faulty, &rdev->flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1657 | set_mask_bits(&mddev->sb_flags, 0, |
| 1658 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1659 | spin_unlock_irqrestore(&conf->device_lock, flags); |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1660 | pr_crit("md/raid10:%s: Disk failure on %s, disabling device.\n" |
| 1661 | "md/raid10:%s: Operation continuing on %d devices.\n", |
| 1662 | mdname(mddev), bdevname(rdev->bdev, b), |
| 1663 | mdname(mddev), conf->geo.raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1664 | } |
| 1665 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1666 | static void print_conf(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | { |
| 1668 | int i; |
NeilBrown | 4056ca5 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 1669 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1670 | |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1671 | pr_debug("RAID10 conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1672 | if (!conf) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1673 | pr_debug("(!conf)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | return; |
| 1675 | } |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1676 | pr_debug(" --- wd:%d rd:%d\n", conf->geo.raid_disks - conf->mddev->degraded, |
| 1677 | conf->geo.raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
NeilBrown | 4056ca5 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 1679 | /* This is only called with ->reconfix_mutex held, so |
| 1680 | * rcu protection of rdev is not needed */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1681 | for (i = 0; i < conf->geo.raid_disks; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1682 | char b[BDEVNAME_SIZE]; |
NeilBrown | 4056ca5 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 1683 | rdev = conf->mirrors[i].rdev; |
| 1684 | if (rdev) |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 1685 | pr_debug(" disk %d, wo:%d, o:%d, dev:%s\n", |
| 1686 | i, !test_bit(In_sync, &rdev->flags), |
| 1687 | !test_bit(Faulty, &rdev->flags), |
| 1688 | bdevname(rdev->bdev,b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | } |
| 1690 | } |
| 1691 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1692 | static void close_sync(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | { |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1694 | wait_barrier(conf); |
| 1695 | allow_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1696 | |
| 1697 | mempool_destroy(conf->r10buf_pool); |
| 1698 | conf->r10buf_pool = NULL; |
| 1699 | } |
| 1700 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1701 | static int raid10_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1702 | { |
| 1703 | int i; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1704 | struct r10conf *conf = mddev->private; |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 1705 | struct raid10_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1706 | int count = 0; |
| 1707 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1708 | |
| 1709 | /* |
| 1710 | * Find all non-in_sync disks within the RAID10 configuration |
| 1711 | * and mark them in_sync |
| 1712 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1713 | for (i = 0; i < conf->geo.raid_disks; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | tmp = conf->mirrors + i; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1715 | if (tmp->replacement |
| 1716 | && tmp->replacement->recovery_offset == MaxSector |
| 1717 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 1718 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 1719 | /* Replacement has just become active */ |
| 1720 | if (!tmp->rdev |
| 1721 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 1722 | count++; |
| 1723 | if (tmp->rdev) { |
| 1724 | /* Replaced device not technically faulty, |
| 1725 | * but we need to be sure it gets removed |
| 1726 | * and never re-added. |
| 1727 | */ |
| 1728 | set_bit(Faulty, &tmp->rdev->flags); |
| 1729 | sysfs_notify_dirent_safe( |
| 1730 | tmp->rdev->sysfs_state); |
| 1731 | } |
| 1732 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 1733 | } else if (tmp->rdev |
Lukasz Dorau | 61e4947 | 2013-10-24 12:55:17 +1100 | [diff] [blame] | 1734 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1735 | && !test_bit(Faulty, &tmp->rdev->flags) |
| 1736 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1737 | count++; |
Jonathan Brassow | 2863b9e | 2012-10-11 13:38:58 +1100 | [diff] [blame] | 1738 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1739 | } |
| 1740 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1741 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1742 | mddev->degraded -= count; |
| 1743 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1744 | |
| 1745 | print_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1746 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1749 | static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1751 | struct r10conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1752 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | int mirror; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1754 | int first = 0; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 1755 | int last = conf->geo.raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | |
| 1757 | if (mddev->recovery_cp < MaxSector) |
| 1758 | /* only hot-add to in-sync arrays, as recovery is |
| 1759 | * very different from resync |
| 1760 | */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1761 | return -EBUSY; |
NeilBrown | 635f641 | 2013-06-11 14:57:09 +1000 | [diff] [blame] | 1762 | if (rdev->saved_raid_disk < 0 && !_enough(conf, 1, -1)) |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1763 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1764 | |
Dan Williams | 1501efa | 2016-01-13 16:00:07 -0800 | [diff] [blame] | 1765 | if (md_integrity_add_rdev(rdev, mddev)) |
| 1766 | return -ENXIO; |
| 1767 | |
NeilBrown | a53a6c8 | 2008-11-06 17:28:20 +1100 | [diff] [blame] | 1768 | if (rdev->raid_disk >= 0) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1769 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1770 | |
Namhyung Kim | 2c4193d | 2011-07-18 17:38:43 +1000 | [diff] [blame] | 1771 | if (rdev->saved_raid_disk >= first && |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1772 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) |
| 1773 | mirror = rdev->saved_raid_disk; |
| 1774 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1775 | mirror = first; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1776 | for ( ; mirror <= last ; mirror++) { |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 1777 | struct raid10_info *p = &conf->mirrors[mirror]; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1778 | if (p->recovery_disabled == mddev->recovery_disabled) |
| 1779 | continue; |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1780 | if (p->rdev) { |
| 1781 | if (!test_bit(WantReplacement, &p->rdev->flags) || |
| 1782 | p->replacement != NULL) |
| 1783 | continue; |
| 1784 | clear_bit(In_sync, &rdev->flags); |
| 1785 | set_bit(Replacement, &rdev->flags); |
| 1786 | rdev->raid_disk = mirror; |
| 1787 | err = 0; |
Jonathan Brassow | 9092c02 | 2013-05-02 14:19:24 -0500 | [diff] [blame] | 1788 | if (mddev->gendisk) |
| 1789 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 1790 | rdev->data_offset << 9); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1791 | conf->fullsync = 1; |
| 1792 | rcu_assign_pointer(p->replacement, rdev); |
| 1793 | break; |
| 1794 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | |
Jonathan Brassow | 9092c02 | 2013-05-02 14:19:24 -0500 | [diff] [blame] | 1796 | if (mddev->gendisk) |
| 1797 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 1798 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1799 | |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1800 | p->head_position = 0; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 1801 | p->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1802 | rdev->raid_disk = mirror; |
| 1803 | err = 0; |
| 1804 | if (rdev->saved_raid_disk != mirror) |
| 1805 | conf->fullsync = 1; |
| 1806 | rcu_assign_pointer(p->rdev, rdev); |
| 1807 | break; |
| 1808 | } |
Jonathan Brassow | ed30be0 | 2012-10-31 11:42:30 +1100 | [diff] [blame] | 1809 | if (mddev->queue && blk_queue_discard(bdev_get_queue(rdev->bdev))) |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 1810 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, mddev->queue); |
| 1811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1812 | print_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1813 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | } |
| 1815 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1816 | static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1817 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1818 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1819 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1820 | int number = rdev->raid_disk; |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1821 | struct md_rdev **rdevp; |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 1822 | struct raid10_info *p = conf->mirrors + number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1823 | |
| 1824 | print_conf(conf); |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1825 | if (rdev == p->rdev) |
| 1826 | rdevp = &p->rdev; |
| 1827 | else if (rdev == p->replacement) |
| 1828 | rdevp = &p->replacement; |
| 1829 | else |
| 1830 | return 0; |
| 1831 | |
| 1832 | if (test_bit(In_sync, &rdev->flags) || |
| 1833 | atomic_read(&rdev->nr_pending)) { |
| 1834 | err = -EBUSY; |
| 1835 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 1837 | /* Only remove non-faulty devices if recovery |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1838 | * is not possible. |
| 1839 | */ |
| 1840 | if (!test_bit(Faulty, &rdev->flags) && |
| 1841 | mddev->recovery_disabled != p->recovery_disabled && |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1842 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 1843 | number < conf->geo.raid_disks && |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1844 | enough(conf, -1)) { |
| 1845 | err = -EBUSY; |
| 1846 | goto abort; |
| 1847 | } |
| 1848 | *rdevp = NULL; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 1849 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
| 1850 | synchronize_rcu(); |
| 1851 | if (atomic_read(&rdev->nr_pending)) { |
| 1852 | /* lost the race, try later */ |
| 1853 | err = -EBUSY; |
| 1854 | *rdevp = rdev; |
| 1855 | goto abort; |
| 1856 | } |
| 1857 | } |
| 1858 | if (p->replacement) { |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1859 | /* We must have just cleared 'rdev' */ |
| 1860 | p->rdev = p->replacement; |
| 1861 | clear_bit(Replacement, &p->replacement->flags); |
| 1862 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 1863 | * but will never see neither -- if they are careful. |
| 1864 | */ |
| 1865 | p->replacement = NULL; |
Guoqing Jiang | e5bc9c3 | 2017-04-24 15:58:04 +0800 | [diff] [blame] | 1866 | } |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1867 | |
Guoqing Jiang | e5bc9c3 | 2017-04-24 15:58:04 +0800 | [diff] [blame] | 1868 | clear_bit(WantReplacement, &rdev->flags); |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1869 | err = md_integrity_register(mddev); |
| 1870 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1871 | abort: |
| 1872 | |
| 1873 | print_conf(conf); |
| 1874 | return err; |
| 1875 | } |
| 1876 | |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 1877 | 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] | 1878 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1879 | struct r10conf *conf = r10_bio->mddev->private; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1880 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1881 | if (!bio->bi_status) |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1882 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
NeilBrown | e684e41 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1883 | else |
| 1884 | /* The write handler will notice the lack of |
| 1885 | * R10BIO_Uptodate and record any errors etc |
| 1886 | */ |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1887 | atomic_add(r10_bio->sectors, |
| 1888 | &conf->mirrors[d].rdev->corrected_errors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1889 | |
| 1890 | /* for reconstruct, we always reschedule after a read. |
| 1891 | * for resync, only after all reads |
| 1892 | */ |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1893 | rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1894 | if (test_bit(R10BIO_IsRecover, &r10_bio->state) || |
| 1895 | atomic_dec_and_test(&r10_bio->remaining)) { |
| 1896 | /* we have read all the blocks, |
| 1897 | * do the comparison in process context in raid10d |
| 1898 | */ |
| 1899 | reschedule_retry(r10_bio); |
| 1900 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1901 | } |
| 1902 | |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 1903 | static void end_sync_read(struct bio *bio) |
| 1904 | { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 1905 | struct r10bio *r10_bio = get_resync_r10bio(bio); |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 1906 | struct r10conf *conf = r10_bio->mddev->private; |
| 1907 | int d = find_bio_disk(conf, r10_bio, bio, NULL, NULL); |
| 1908 | |
| 1909 | __end_sync_read(r10_bio, bio, d); |
| 1910 | } |
| 1911 | |
| 1912 | static void end_reshape_read(struct bio *bio) |
| 1913 | { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 1914 | /* reshape read bio isn't allocated from r10buf_pool */ |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 1915 | struct r10bio *r10_bio = bio->bi_private; |
| 1916 | |
| 1917 | __end_sync_read(r10_bio, bio, r10_bio->read_slot); |
| 1918 | } |
| 1919 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1920 | static void end_sync_request(struct r10bio *r10_bio) |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1921 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1922 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1923 | |
| 1924 | while (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1925 | if (r10_bio->master_bio == NULL) { |
| 1926 | /* the primary of several recovery bios */ |
| 1927 | sector_t s = r10_bio->sectors; |
| 1928 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 1929 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 1930 | reschedule_retry(r10_bio); |
| 1931 | else |
| 1932 | put_buf(r10_bio); |
| 1933 | md_done_sync(mddev, s, 1); |
| 1934 | break; |
| 1935 | } else { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1936 | struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1937 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 1938 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 1939 | reschedule_retry(r10_bio); |
| 1940 | else |
| 1941 | put_buf(r10_bio); |
| 1942 | r10_bio = r10_bio2; |
| 1943 | } |
| 1944 | } |
| 1945 | } |
| 1946 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1947 | static void end_sync_write(struct bio *bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1948 | { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 1949 | struct r10bio *r10_bio = get_resync_r10bio(bio); |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1950 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1951 | struct r10conf *conf = mddev->private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 1952 | int d; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1953 | sector_t first_bad; |
| 1954 | int bad_sectors; |
| 1955 | int slot; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1956 | int repl; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1957 | struct md_rdev *rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1959 | d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
| 1960 | if (repl) |
| 1961 | rdev = conf->mirrors[d].replacement; |
NeilBrown | 547414d | 2012-03-13 11:21:20 +1100 | [diff] [blame] | 1962 | else |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1963 | rdev = conf->mirrors[d].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 1965 | if (bio->bi_status) { |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1966 | if (repl) |
| 1967 | md_error(mddev, rdev); |
| 1968 | else { |
| 1969 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1970 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1971 | set_bit(MD_RECOVERY_NEEDED, |
| 1972 | &rdev->mddev->recovery); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1973 | set_bit(R10BIO_WriteError, &r10_bio->state); |
| 1974 | } |
| 1975 | } else if (is_badblock(rdev, |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1976 | r10_bio->devs[slot].addr, |
| 1977 | r10_bio->sectors, |
| 1978 | &first_bad, &bad_sectors)) |
| 1979 | set_bit(R10BIO_MadeGood, &r10_bio->state); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1980 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1981 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1982 | |
| 1983 | end_sync_request(r10_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | } |
| 1985 | |
| 1986 | /* |
| 1987 | * Note: sync and recover and handled very differently for raid10 |
| 1988 | * This code is for resync. |
| 1989 | * For resync, we read through virtual addresses and read all blocks. |
| 1990 | * If there is any error, we schedule a write. The lowest numbered |
| 1991 | * drive is authoritative. |
| 1992 | * However requests come for physical address, so we need to map. |
| 1993 | * For every physical address there are raid_disks/copies virtual addresses, |
| 1994 | * which is always are least one, but is not necessarly an integer. |
| 1995 | * This means that a physical address can span multiple chunks, so we may |
| 1996 | * have to submit multiple io requests for a single sync request. |
| 1997 | */ |
| 1998 | /* |
| 1999 | * We check if all blocks are in-sync and only write to blocks that |
| 2000 | * aren't in sync |
| 2001 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2002 | static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2003 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2004 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | int i, first; |
| 2006 | struct bio *tbio, *fbio; |
majianpeng | f4380a9 | 2012-04-12 16:04:47 +1000 | [diff] [blame] | 2007 | int vcnt; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2008 | struct page **tpages, **fpages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | |
| 2010 | atomic_set(&r10_bio->remaining, 1); |
| 2011 | |
| 2012 | /* find the first device with a block */ |
| 2013 | for (i=0; i<conf->copies; i++) |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2014 | if (!r10_bio->devs[i].bio->bi_status) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2015 | break; |
| 2016 | |
| 2017 | if (i == conf->copies) |
| 2018 | goto done; |
| 2019 | |
| 2020 | first = i; |
| 2021 | fbio = r10_bio->devs[i].bio; |
Artur Paszkiewicz | cc57858 | 2015-12-18 15:19:16 +1100 | [diff] [blame] | 2022 | fbio->bi_iter.bi_size = r10_bio->sectors << 9; |
| 2023 | fbio->bi_iter.bi_idx = 0; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2024 | fpages = get_resync_pages(fbio)->pages; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2025 | |
majianpeng | f4380a9 | 2012-04-12 16:04:47 +1000 | [diff] [blame] | 2026 | vcnt = (r10_bio->sectors + (PAGE_SIZE >> 9) - 1) >> (PAGE_SHIFT - 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | /* now find blocks with errors */ |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2028 | for (i=0 ; i < conf->copies ; i++) { |
| 2029 | int j, d; |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2030 | struct md_rdev *rdev; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 2031 | struct resync_pages *rp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2032 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | tbio = r10_bio->devs[i].bio; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2034 | |
| 2035 | if (tbio->bi_end_io != end_sync_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2036 | continue; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2037 | if (i == first) |
| 2038 | continue; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2039 | |
| 2040 | tpages = get_resync_pages(tbio)->pages; |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2041 | d = r10_bio->devs[i].devnum; |
| 2042 | rdev = conf->mirrors[d].rdev; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2043 | if (!r10_bio->devs[i].bio->bi_status) { |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2044 | /* We know that the bi_io_vec layout is the same for |
| 2045 | * both 'first' and 'i', so we just compare them. |
| 2046 | * All vec entries are PAGE_SIZE; |
| 2047 | */ |
NeilBrown | 7bb23c4 | 2013-07-16 16:50:47 +1000 | [diff] [blame] | 2048 | int sectors = r10_bio->sectors; |
| 2049 | for (j = 0; j < vcnt; j++) { |
| 2050 | int len = PAGE_SIZE; |
| 2051 | if (sectors < (len / 512)) |
| 2052 | len = sectors * 512; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2053 | if (memcmp(page_address(fpages[j]), |
| 2054 | page_address(tpages[j]), |
NeilBrown | 7bb23c4 | 2013-07-16 16:50:47 +1000 | [diff] [blame] | 2055 | len)) |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2056 | break; |
NeilBrown | 7bb23c4 | 2013-07-16 16:50:47 +1000 | [diff] [blame] | 2057 | sectors -= len/512; |
| 2058 | } |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2059 | if (j == vcnt) |
| 2060 | continue; |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 2061 | atomic64_add(r10_bio->sectors, &mddev->resync_mismatches); |
NeilBrown | f84ee36 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2062 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 2063 | /* Don't fix anything. */ |
| 2064 | continue; |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2065 | } else if (test_bit(FailFast, &rdev->flags)) { |
| 2066 | /* Just give up on this device */ |
| 2067 | md_error(rdev->mddev, rdev); |
| 2068 | continue; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 2069 | } |
NeilBrown | f84ee36 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2070 | /* Ok, we need to write this bio, either to correct an |
| 2071 | * inconsistency or to correct an unreadable block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | * First we need to fixup bv_offset, bv_len and |
| 2073 | * bi_vecs, as the read request might have corrupted these |
| 2074 | */ |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 2075 | rp = get_resync_pages(tbio); |
Kent Overstreet | 8be185f | 2012-09-06 14:14:43 -0700 | [diff] [blame] | 2076 | bio_reset(tbio); |
| 2077 | |
Ming Lei | fb0eb5d | 2017-07-14 16:14:43 +0800 | [diff] [blame] | 2078 | md_bio_reset_resync_pages(tbio, rp, fbio->bi_iter.bi_size); |
| 2079 | |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 2080 | rp->raid_bio = r10_bio; |
| 2081 | tbio->bi_private = rp; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2082 | tbio->bi_iter.bi_sector = r10_bio->devs[i].addr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | tbio->bi_end_io = end_sync_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2084 | bio_set_op_attrs(tbio, REQ_OP_WRITE, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2085 | |
Kent Overstreet | c31df25 | 2015-05-06 23:34:20 -0700 | [diff] [blame] | 2086 | bio_copy_data(tbio, fbio); |
| 2087 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 2089 | atomic_inc(&r10_bio->remaining); |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 2090 | md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(tbio)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2091 | |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2092 | if (test_bit(FailFast, &conf->mirrors[d].rdev->flags)) |
| 2093 | tbio->bi_opf |= MD_FAILFAST; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2094 | tbio->bi_iter.bi_sector += conf->mirrors[d].rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2095 | bio_set_dev(tbio, conf->mirrors[d].rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2096 | generic_make_request(tbio); |
| 2097 | } |
| 2098 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2099 | /* Now write out to any replacement devices |
| 2100 | * that are active |
| 2101 | */ |
| 2102 | for (i = 0; i < conf->copies; i++) { |
Kent Overstreet | c31df25 | 2015-05-06 23:34:20 -0700 | [diff] [blame] | 2103 | int d; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2104 | |
| 2105 | tbio = r10_bio->devs[i].repl_bio; |
| 2106 | if (!tbio || !tbio->bi_end_io) |
| 2107 | continue; |
| 2108 | if (r10_bio->devs[i].bio->bi_end_io != end_sync_write |
| 2109 | && r10_bio->devs[i].bio != fbio) |
Kent Overstreet | c31df25 | 2015-05-06 23:34:20 -0700 | [diff] [blame] | 2110 | bio_copy_data(tbio, fbio); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2111 | d = r10_bio->devs[i].devnum; |
| 2112 | atomic_inc(&r10_bio->remaining); |
| 2113 | md_sync_acct(conf->mirrors[d].replacement->bdev, |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 2114 | bio_sectors(tbio)); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2115 | generic_make_request(tbio); |
| 2116 | } |
| 2117 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2118 | done: |
| 2119 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 2120 | md_done_sync(mddev, r10_bio->sectors, 1); |
| 2121 | put_buf(r10_bio); |
| 2122 | } |
| 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * Now for the recovery code. |
| 2127 | * Recovery happens across physical sectors. |
| 2128 | * We recover all non-is_sync drives by finding the virtual address of |
| 2129 | * each, and then choose a working drive that also has that virt address. |
| 2130 | * There is a separate r10_bio for each non-in_sync drive. |
| 2131 | * Only the first two slots are in use. The first for reading, |
| 2132 | * The second for writing. |
| 2133 | * |
| 2134 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2135 | static void fix_recovery_read_error(struct r10bio *r10_bio) |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2136 | { |
| 2137 | /* We got a read error during recovery. |
| 2138 | * We repeat the read in smaller page-sized sections. |
| 2139 | * If a read succeeds, write it to the new device or record |
| 2140 | * a bad block if we cannot. |
| 2141 | * If a read fails, record a bad block on both old and |
| 2142 | * new devices. |
| 2143 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2144 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2145 | struct r10conf *conf = mddev->private; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2146 | struct bio *bio = r10_bio->devs[0].bio; |
| 2147 | sector_t sect = 0; |
| 2148 | int sectors = r10_bio->sectors; |
| 2149 | int idx = 0; |
| 2150 | int dr = r10_bio->devs[0].devnum; |
| 2151 | int dw = r10_bio->devs[1].devnum; |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2152 | struct page **pages = get_resync_pages(bio)->pages; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2153 | |
| 2154 | while (sectors) { |
| 2155 | int s = sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2156 | struct md_rdev *rdev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2157 | sector_t addr; |
| 2158 | int ok; |
| 2159 | |
| 2160 | if (s > (PAGE_SIZE>>9)) |
| 2161 | s = PAGE_SIZE >> 9; |
| 2162 | |
| 2163 | rdev = conf->mirrors[dr].rdev; |
| 2164 | addr = r10_bio->devs[0].addr + sect, |
| 2165 | ok = sync_page_io(rdev, |
| 2166 | addr, |
| 2167 | s << 9, |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2168 | pages[idx], |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2169 | REQ_OP_READ, 0, false); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2170 | if (ok) { |
| 2171 | rdev = conf->mirrors[dw].rdev; |
| 2172 | addr = r10_bio->devs[1].addr + sect; |
| 2173 | ok = sync_page_io(rdev, |
| 2174 | addr, |
| 2175 | s << 9, |
Ming Lei | cdb76be | 2017-03-17 00:12:34 +0800 | [diff] [blame] | 2176 | pages[idx], |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2177 | REQ_OP_WRITE, 0, false); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2178 | if (!ok) { |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2179 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2180 | if (!test_and_set_bit(WantReplacement, |
| 2181 | &rdev->flags)) |
| 2182 | set_bit(MD_RECOVERY_NEEDED, |
| 2183 | &rdev->mddev->recovery); |
| 2184 | } |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2185 | } |
| 2186 | if (!ok) { |
| 2187 | /* We don't worry if we cannot set a bad block - |
| 2188 | * it really is bad so there is no loss in not |
| 2189 | * recording it yet |
| 2190 | */ |
| 2191 | rdev_set_badblocks(rdev, addr, s, 0); |
| 2192 | |
| 2193 | if (rdev != conf->mirrors[dw].rdev) { |
| 2194 | /* need bad block on destination too */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2195 | struct md_rdev *rdev2 = conf->mirrors[dw].rdev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2196 | addr = r10_bio->devs[1].addr + sect; |
| 2197 | ok = rdev_set_badblocks(rdev2, addr, s, 0); |
| 2198 | if (!ok) { |
| 2199 | /* just abort the recovery */ |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2200 | pr_notice("md/raid10:%s: recovery aborted due to read error\n", |
| 2201 | mdname(mddev)); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2202 | |
| 2203 | conf->mirrors[dw].recovery_disabled |
| 2204 | = mddev->recovery_disabled; |
| 2205 | set_bit(MD_RECOVERY_INTR, |
| 2206 | &mddev->recovery); |
| 2207 | break; |
| 2208 | } |
| 2209 | } |
| 2210 | } |
| 2211 | |
| 2212 | sectors -= s; |
| 2213 | sect += s; |
| 2214 | idx++; |
| 2215 | } |
| 2216 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2217 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2218 | static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2219 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2220 | struct r10conf *conf = mddev->private; |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 2221 | int d; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2222 | struct bio *wbio, *wbio2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2224 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) { |
| 2225 | fix_recovery_read_error(r10_bio); |
| 2226 | end_sync_request(r10_bio); |
| 2227 | return; |
| 2228 | } |
| 2229 | |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 2230 | /* |
| 2231 | * share the pages with the first bio |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | * and submit the write request |
| 2233 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2234 | d = r10_bio->devs[1].devnum; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2235 | wbio = r10_bio->devs[1].bio; |
| 2236 | wbio2 = r10_bio->devs[1].repl_bio; |
NeilBrown | 0eb25bb | 2013-07-24 15:37:42 +1000 | [diff] [blame] | 2237 | /* Need to test wbio2->bi_end_io before we call |
| 2238 | * generic_make_request as if the former is NULL, |
| 2239 | * the latter is free to free wbio2. |
| 2240 | */ |
| 2241 | if (wbio2 && !wbio2->bi_end_io) |
| 2242 | wbio2 = NULL; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2243 | if (wbio->bi_end_io) { |
| 2244 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 2245 | md_sync_acct(conf->mirrors[d].rdev->bdev, bio_sectors(wbio)); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2246 | generic_make_request(wbio); |
| 2247 | } |
NeilBrown | 0eb25bb | 2013-07-24 15:37:42 +1000 | [diff] [blame] | 2248 | if (wbio2) { |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2249 | atomic_inc(&conf->mirrors[d].replacement->nr_pending); |
| 2250 | md_sync_acct(conf->mirrors[d].replacement->bdev, |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 2251 | bio_sectors(wbio2)); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2252 | generic_make_request(wbio2); |
| 2253 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2254 | } |
| 2255 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2256 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2257 | * Used by fix_read_error() to decay the per rdev read_errors. |
| 2258 | * We halve the read error count for every hour that has elapsed |
| 2259 | * since the last recorded read error. |
| 2260 | * |
| 2261 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2262 | 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] | 2263 | { |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 2264 | long cur_time_mon; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2265 | unsigned long hours_since_last; |
| 2266 | unsigned int read_errors = atomic_read(&rdev->read_errors); |
| 2267 | |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 2268 | cur_time_mon = ktime_get_seconds(); |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2269 | |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 2270 | if (rdev->last_read_error == 0) { |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2271 | /* first time we've seen a read error */ |
| 2272 | rdev->last_read_error = cur_time_mon; |
| 2273 | return; |
| 2274 | } |
| 2275 | |
Arnd Bergmann | 0e3ef49 | 2016-06-17 17:33:10 +0200 | [diff] [blame] | 2276 | hours_since_last = (long)(cur_time_mon - |
| 2277 | rdev->last_read_error) / 3600; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2278 | |
| 2279 | rdev->last_read_error = cur_time_mon; |
| 2280 | |
| 2281 | /* |
| 2282 | * if hours_since_last is > the number of bits in read_errors |
| 2283 | * just set read errors to 0. We do this to avoid |
| 2284 | * overflowing the shift of read_errors by hours_since_last. |
| 2285 | */ |
| 2286 | if (hours_since_last >= 8 * sizeof(read_errors)) |
| 2287 | atomic_set(&rdev->read_errors, 0); |
| 2288 | else |
| 2289 | atomic_set(&rdev->read_errors, read_errors >> hours_since_last); |
| 2290 | } |
| 2291 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2292 | static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2293 | int sectors, struct page *page, int rw) |
| 2294 | { |
| 2295 | sector_t first_bad; |
| 2296 | int bad_sectors; |
| 2297 | |
| 2298 | if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors) |
| 2299 | && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags))) |
| 2300 | return -1; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2301 | if (sync_page_io(rdev, sector, sectors << 9, page, rw, 0, false)) |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2302 | /* success */ |
| 2303 | return 1; |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2304 | if (rw == WRITE) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2305 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2306 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2307 | set_bit(MD_RECOVERY_NEEDED, |
| 2308 | &rdev->mddev->recovery); |
| 2309 | } |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2310 | /* need to record an error - either for the block or the device */ |
| 2311 | if (!rdev_set_badblocks(rdev, sector, sectors, 0)) |
| 2312 | md_error(rdev->mddev, rdev); |
| 2313 | return 0; |
| 2314 | } |
| 2315 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2316 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | * This is a kernel thread which: |
| 2318 | * |
| 2319 | * 1. Retries failed read operations on working mirrors. |
| 2320 | * 2. Updates the raid superblock when problems encounter. |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2321 | * 3. Performs writes following reads for array synchronising. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2322 | */ |
| 2323 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2324 | 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] | 2325 | { |
| 2326 | int sect = 0; /* Offset from r10_bio->sector */ |
| 2327 | int sectors = r10_bio->sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2328 | struct md_rdev*rdev; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2329 | int max_read_errors = atomic_read(&mddev->max_corr_read_errors); |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2330 | int d = r10_bio->devs[r10_bio->read_slot].devnum; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2331 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2332 | /* still own a reference to this rdev, so it cannot |
| 2333 | * have been cleared recently. |
| 2334 | */ |
| 2335 | rdev = conf->mirrors[d].rdev; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2336 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2337 | if (test_bit(Faulty, &rdev->flags)) |
| 2338 | /* drive has already been failed, just ignore any |
| 2339 | more fix_read_error() attempts */ |
| 2340 | return; |
| 2341 | |
| 2342 | check_decay_read_errors(mddev, rdev); |
| 2343 | atomic_inc(&rdev->read_errors); |
| 2344 | if (atomic_read(&rdev->read_errors) > max_read_errors) { |
| 2345 | char b[BDEVNAME_SIZE]; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2346 | bdevname(rdev->bdev, b); |
| 2347 | |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2348 | pr_notice("md/raid10:%s: %s: Raid device exceeded read_error threshold [cur %d:max %d]\n", |
| 2349 | mdname(mddev), b, |
| 2350 | atomic_read(&rdev->read_errors), max_read_errors); |
| 2351 | pr_notice("md/raid10:%s: %s: Failing raid device\n", |
| 2352 | mdname(mddev), b); |
NeilBrown | d683c8e | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 2353 | md_error(mddev, rdev); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2354 | r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED; |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2355 | return; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2356 | } |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2357 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2358 | while(sectors) { |
| 2359 | int s = sectors; |
| 2360 | int sl = r10_bio->read_slot; |
| 2361 | int success = 0; |
| 2362 | int start; |
| 2363 | |
| 2364 | if (s > (PAGE_SIZE>>9)) |
| 2365 | s = PAGE_SIZE >> 9; |
| 2366 | |
| 2367 | rcu_read_lock(); |
| 2368 | do { |
NeilBrown | 8dbed5c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2369 | sector_t first_bad; |
| 2370 | int bad_sectors; |
| 2371 | |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2372 | d = r10_bio->devs[sl].devnum; |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2373 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 2374 | if (rdev && |
NeilBrown | 8dbed5c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2375 | test_bit(In_sync, &rdev->flags) && |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 2376 | !test_bit(Faulty, &rdev->flags) && |
NeilBrown | 8dbed5c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2377 | is_badblock(rdev, r10_bio->devs[sl].addr + sect, s, |
| 2378 | &first_bad, &bad_sectors) == 0) { |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2379 | atomic_inc(&rdev->nr_pending); |
| 2380 | rcu_read_unlock(); |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 2381 | success = sync_page_io(rdev, |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2382 | r10_bio->devs[sl].addr + |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 2383 | sect, |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2384 | s<<9, |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2385 | conf->tmppage, |
| 2386 | REQ_OP_READ, 0, false); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2387 | rdev_dec_pending(rdev, mddev); |
| 2388 | rcu_read_lock(); |
| 2389 | if (success) |
| 2390 | break; |
| 2391 | } |
| 2392 | sl++; |
| 2393 | if (sl == conf->copies) |
| 2394 | sl = 0; |
| 2395 | } while (!success && sl != r10_bio->read_slot); |
| 2396 | rcu_read_unlock(); |
| 2397 | |
| 2398 | if (!success) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2399 | /* Cannot read from anywhere, just mark the block |
| 2400 | * as bad on the first device to discourage future |
| 2401 | * reads. |
| 2402 | */ |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2403 | int dn = r10_bio->devs[r10_bio->read_slot].devnum; |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2404 | rdev = conf->mirrors[dn].rdev; |
| 2405 | |
| 2406 | if (!rdev_set_badblocks( |
| 2407 | rdev, |
| 2408 | r10_bio->devs[r10_bio->read_slot].addr |
| 2409 | + sect, |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2410 | s, 0)) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2411 | md_error(mddev, rdev); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2412 | r10_bio->devs[r10_bio->read_slot].bio |
| 2413 | = IO_BLOCKED; |
| 2414 | } |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2415 | break; |
| 2416 | } |
| 2417 | |
| 2418 | start = sl; |
| 2419 | /* write it back and re-read */ |
| 2420 | rcu_read_lock(); |
| 2421 | while (sl != r10_bio->read_slot) { |
Robert Becker | 67b8dc4 | 2009-12-14 12:49:57 +1100 | [diff] [blame] | 2422 | char b[BDEVNAME_SIZE]; |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2423 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2424 | if (sl==0) |
| 2425 | sl = conf->copies; |
| 2426 | sl--; |
| 2427 | d = r10_bio->devs[sl].devnum; |
| 2428 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2429 | if (!rdev || |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 2430 | test_bit(Faulty, &rdev->flags) || |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2431 | !test_bit(In_sync, &rdev->flags)) |
| 2432 | continue; |
| 2433 | |
| 2434 | atomic_inc(&rdev->nr_pending); |
| 2435 | rcu_read_unlock(); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2436 | if (r10_sync_page_io(rdev, |
| 2437 | r10_bio->devs[sl].addr + |
| 2438 | sect, |
NeilBrown | 055d374 | 2012-07-03 15:55:33 +1000 | [diff] [blame] | 2439 | s, conf->tmppage, WRITE) |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2440 | == 0) { |
| 2441 | /* Well, this device is dead */ |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2442 | pr_notice("md/raid10:%s: read correction write failed (%d sectors at %llu on %s)\n", |
| 2443 | mdname(mddev), s, |
| 2444 | (unsigned long long)( |
| 2445 | sect + |
| 2446 | choose_data_offset(r10_bio, |
| 2447 | rdev)), |
| 2448 | bdevname(rdev->bdev, b)); |
| 2449 | pr_notice("md/raid10:%s: %s: failing drive\n", |
| 2450 | mdname(mddev), |
| 2451 | bdevname(rdev->bdev, b)); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2452 | } |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2453 | rdev_dec_pending(rdev, mddev); |
| 2454 | rcu_read_lock(); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2455 | } |
| 2456 | sl = start; |
| 2457 | while (sl != r10_bio->read_slot) { |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2458 | char b[BDEVNAME_SIZE]; |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2459 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2460 | if (sl==0) |
| 2461 | sl = conf->copies; |
| 2462 | sl--; |
| 2463 | d = r10_bio->devs[sl].devnum; |
| 2464 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2465 | if (!rdev || |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 2466 | test_bit(Faulty, &rdev->flags) || |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2467 | !test_bit(In_sync, &rdev->flags)) |
| 2468 | continue; |
Robert Becker | 67b8dc4 | 2009-12-14 12:49:57 +1100 | [diff] [blame] | 2469 | |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2470 | atomic_inc(&rdev->nr_pending); |
| 2471 | rcu_read_unlock(); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2472 | switch (r10_sync_page_io(rdev, |
| 2473 | r10_bio->devs[sl].addr + |
| 2474 | sect, |
NeilBrown | 055d374 | 2012-07-03 15:55:33 +1000 | [diff] [blame] | 2475 | s, conf->tmppage, |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2476 | READ)) { |
| 2477 | case 0: |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2478 | /* Well, this device is dead */ |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2479 | 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] | 2480 | mdname(mddev), s, |
| 2481 | (unsigned long long)( |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 2482 | sect + |
| 2483 | choose_data_offset(r10_bio, rdev)), |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2484 | bdevname(rdev->bdev, b)); |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2485 | pr_notice("md/raid10:%s: %s: failing drive\n", |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2486 | mdname(mddev), |
| 2487 | bdevname(rdev->bdev, b)); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2488 | break; |
| 2489 | case 1: |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2490 | 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] | 2491 | mdname(mddev), s, |
| 2492 | (unsigned long long)( |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 2493 | sect + |
| 2494 | choose_data_offset(r10_bio, rdev)), |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2495 | bdevname(rdev->bdev, b)); |
| 2496 | atomic_add(s, &rdev->corrected_errors); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2497 | } |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2498 | |
| 2499 | rdev_dec_pending(rdev, mddev); |
| 2500 | rcu_read_lock(); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2501 | } |
| 2502 | rcu_read_unlock(); |
| 2503 | |
| 2504 | sectors -= s; |
| 2505 | sect += s; |
| 2506 | } |
| 2507 | } |
| 2508 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2509 | static int narrow_write_error(struct r10bio *r10_bio, int i) |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2510 | { |
| 2511 | struct bio *bio = r10_bio->master_bio; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2512 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2513 | struct r10conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2514 | struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2515 | /* bio has the data to be written to slot 'i' where |
| 2516 | * we just recently had a write error. |
| 2517 | * We repeatedly clone the bio and trim down to one block, |
| 2518 | * then try the write. Where the write fails we record |
| 2519 | * a bad block. |
| 2520 | * It is conceivable that the bio doesn't exactly align with |
| 2521 | * blocks. We must handle this. |
| 2522 | * |
| 2523 | * We currently own a reference to the rdev. |
| 2524 | */ |
| 2525 | |
| 2526 | int block_sectors; |
| 2527 | sector_t sector; |
| 2528 | int sectors; |
| 2529 | int sect_to_write = r10_bio->sectors; |
| 2530 | int ok = 1; |
| 2531 | |
| 2532 | if (rdev->badblocks.shift < 0) |
| 2533 | return 0; |
| 2534 | |
NeilBrown | f04ebb0 | 2015-02-16 14:51:54 +1100 | [diff] [blame] | 2535 | block_sectors = roundup(1 << rdev->badblocks.shift, |
| 2536 | bdev_logical_block_size(rdev->bdev) >> 9); |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2537 | sector = r10_bio->sector; |
| 2538 | sectors = ((r10_bio->sector + block_sectors) |
| 2539 | & ~(sector_t)(block_sectors - 1)) |
| 2540 | - sector; |
| 2541 | |
| 2542 | while (sect_to_write) { |
| 2543 | struct bio *wbio; |
Tomasz Majchrzak | 2702862 | 2016-08-23 10:53:57 +0200 | [diff] [blame] | 2544 | sector_t wsector; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2545 | if (sectors > sect_to_write) |
| 2546 | sectors = sect_to_write; |
| 2547 | /* Write at 'sector' for 'sectors' */ |
Ming Lei | d7a1030 | 2017-02-14 23:29:03 +0800 | [diff] [blame] | 2548 | wbio = bio_clone_fast(bio, GFP_NOIO, mddev->bio_set); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2549 | bio_trim(wbio, sector - bio->bi_iter.bi_sector, sectors); |
Tomasz Majchrzak | 2702862 | 2016-08-23 10:53:57 +0200 | [diff] [blame] | 2550 | wsector = r10_bio->devs[i].addr + (sector - r10_bio->sector); |
| 2551 | wbio->bi_iter.bi_sector = wsector + |
| 2552 | choose_data_offset(r10_bio, rdev); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2553 | bio_set_dev(wbio, rdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 2554 | bio_set_op_attrs(wbio, REQ_OP_WRITE, 0); |
Mike Christie | 4e49ea4 | 2016-06-05 14:31:41 -0500 | [diff] [blame] | 2555 | |
| 2556 | if (submit_bio_wait(wbio) < 0) |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2557 | /* Failure! */ |
Tomasz Majchrzak | 2702862 | 2016-08-23 10:53:57 +0200 | [diff] [blame] | 2558 | ok = rdev_set_badblocks(rdev, wsector, |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2559 | sectors, 0) |
| 2560 | && ok; |
| 2561 | |
| 2562 | bio_put(wbio); |
| 2563 | sect_to_write -= sectors; |
| 2564 | sector += sectors; |
| 2565 | sectors = block_sectors; |
| 2566 | } |
| 2567 | return ok; |
| 2568 | } |
| 2569 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2570 | static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio) |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2571 | { |
| 2572 | int slot = r10_bio->read_slot; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2573 | struct bio *bio; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2574 | struct r10conf *conf = mddev->private; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2575 | struct md_rdev *rdev = r10_bio->devs[slot].rdev; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2576 | |
| 2577 | /* we got a read error. Maybe the drive is bad. Maybe just |
| 2578 | * the block and we can fix it. |
| 2579 | * We freeze all other IO, and try reading the block from |
| 2580 | * other devices. When we find one, we re-write |
| 2581 | * and check it that fixes the read error. |
| 2582 | * This is all done synchronously while the array is |
| 2583 | * frozen. |
| 2584 | */ |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2585 | bio = r10_bio->devs[slot].bio; |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2586 | bio_put(bio); |
| 2587 | r10_bio->devs[slot].bio = NULL; |
| 2588 | |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2589 | if (mddev->ro) |
| 2590 | r10_bio->devs[slot].bio = IO_BLOCKED; |
| 2591 | else if (!test_bit(FailFast, &rdev->flags)) { |
NeilBrown | e2d5992 | 2013-06-12 11:01:22 +1000 | [diff] [blame] | 2592 | freeze_array(conf, 1); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2593 | fix_read_error(conf, mddev, r10_bio); |
| 2594 | unfreeze_array(conf); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2595 | } else |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 2596 | md_error(mddev, rdev); |
NeilBrown | fae8cc5e | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2597 | |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2598 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 545250f | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 2599 | allow_barrier(conf); |
| 2600 | r10_bio->state = 0; |
| 2601 | raid10_read_request(mddev, r10_bio->master_bio, r10_bio); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2602 | } |
| 2603 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2604 | static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2605 | { |
| 2606 | /* Some sort of write request has finished and it |
| 2607 | * succeeded in writing where we thought there was a |
| 2608 | * bad block. So forget the bad block. |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2609 | * Or possibly if failed and we need to record |
| 2610 | * a bad block. |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2611 | */ |
| 2612 | int m; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2613 | struct md_rdev *rdev; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2614 | |
| 2615 | if (test_bit(R10BIO_IsSync, &r10_bio->state) || |
| 2616 | test_bit(R10BIO_IsRecover, &r10_bio->state)) { |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2617 | for (m = 0; m < conf->copies; m++) { |
| 2618 | int dev = r10_bio->devs[m].devnum; |
| 2619 | rdev = conf->mirrors[dev].rdev; |
| 2620 | if (r10_bio->devs[m].bio == NULL) |
| 2621 | continue; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2622 | if (!r10_bio->devs[m].bio->bi_status) { |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2623 | rdev_clear_badblocks( |
| 2624 | rdev, |
| 2625 | r10_bio->devs[m].addr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2626 | r10_bio->sectors, 0); |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2627 | } else { |
| 2628 | if (!rdev_set_badblocks( |
| 2629 | rdev, |
| 2630 | r10_bio->devs[m].addr, |
| 2631 | r10_bio->sectors, 0)) |
| 2632 | md_error(conf->mddev, rdev); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2633 | } |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2634 | rdev = conf->mirrors[dev].replacement; |
| 2635 | if (r10_bio->devs[m].repl_bio == NULL) |
| 2636 | continue; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2637 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2638 | if (!r10_bio->devs[m].repl_bio->bi_status) { |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2639 | rdev_clear_badblocks( |
| 2640 | rdev, |
| 2641 | r10_bio->devs[m].addr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2642 | r10_bio->sectors, 0); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2643 | } else { |
| 2644 | if (!rdev_set_badblocks( |
| 2645 | rdev, |
| 2646 | r10_bio->devs[m].addr, |
| 2647 | r10_bio->sectors, 0)) |
| 2648 | md_error(conf->mddev, rdev); |
| 2649 | } |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2650 | } |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2651 | put_buf(r10_bio); |
| 2652 | } else { |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2653 | bool fail = false; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2654 | for (m = 0; m < conf->copies; m++) { |
| 2655 | int dev = r10_bio->devs[m].devnum; |
| 2656 | struct bio *bio = r10_bio->devs[m].bio; |
| 2657 | rdev = conf->mirrors[dev].rdev; |
| 2658 | if (bio == IO_MADE_GOOD) { |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2659 | rdev_clear_badblocks( |
| 2660 | rdev, |
| 2661 | r10_bio->devs[m].addr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2662 | r10_bio->sectors, 0); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2663 | rdev_dec_pending(rdev, conf->mddev); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2664 | } else if (bio != NULL && bio->bi_status) { |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2665 | fail = true; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2666 | if (!narrow_write_error(r10_bio, m)) { |
| 2667 | md_error(conf->mddev, rdev); |
| 2668 | set_bit(R10BIO_Degraded, |
| 2669 | &r10_bio->state); |
| 2670 | } |
| 2671 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2672 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2673 | bio = r10_bio->devs[m].repl_bio; |
| 2674 | rdev = conf->mirrors[dev].replacement; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2675 | if (rdev && bio == IO_MADE_GOOD) { |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2676 | rdev_clear_badblocks( |
| 2677 | rdev, |
| 2678 | r10_bio->devs[m].addr, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2679 | r10_bio->sectors, 0); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2680 | rdev_dec_pending(rdev, conf->mddev); |
| 2681 | } |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2682 | } |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2683 | if (fail) { |
| 2684 | spin_lock_irq(&conf->device_lock); |
| 2685 | list_add(&r10_bio->retry_list, &conf->bio_end_io_list); |
Shaohua Li | 23ddba8 | 2016-03-14 11:49:32 -0700 | [diff] [blame] | 2686 | conf->nr_queued++; |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2687 | spin_unlock_irq(&conf->device_lock); |
Guoqing Jiang | cf25ae7 | 2017-04-17 17:11:05 +0800 | [diff] [blame] | 2688 | /* |
| 2689 | * In case freeze_array() is waiting for condition |
| 2690 | * nr_pending == nr_queued + extra to be true. |
| 2691 | */ |
| 2692 | wake_up(&conf->wait_barrier); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2693 | md_wakeup_thread(conf->mddev->thread); |
NeilBrown | c340702 | 2015-10-24 16:23:48 +1100 | [diff] [blame] | 2694 | } else { |
| 2695 | if (test_bit(R10BIO_WriteError, |
| 2696 | &r10_bio->state)) |
| 2697 | close_write(r10_bio); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2698 | raid_end_bio_io(r10_bio); |
NeilBrown | c340702 | 2015-10-24 16:23:48 +1100 | [diff] [blame] | 2699 | } |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2700 | } |
| 2701 | } |
| 2702 | |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 2703 | static void raid10d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2704 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 2705 | struct mddev *mddev = thread->mddev; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2706 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | unsigned long flags; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2708 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2709 | struct list_head *head = &conf->retry_list; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2710 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2711 | |
| 2712 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2714 | if (!list_empty_careful(&conf->bio_end_io_list) && |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2715 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2716 | LIST_HEAD(tmp); |
| 2717 | spin_lock_irqsave(&conf->device_lock, flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2718 | if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
Shaohua Li | 23ddba8 | 2016-03-14 11:49:32 -0700 | [diff] [blame] | 2719 | while (!list_empty(&conf->bio_end_io_list)) { |
| 2720 | list_move(conf->bio_end_io_list.prev, &tmp); |
| 2721 | conf->nr_queued--; |
| 2722 | } |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2723 | } |
| 2724 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2725 | while (!list_empty(&tmp)) { |
Mikulas Patocka | a452744 | 2015-10-01 15:17:43 -0400 | [diff] [blame] | 2726 | r10_bio = list_first_entry(&tmp, struct r10bio, |
| 2727 | retry_list); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2728 | list_del(&r10_bio->retry_list); |
NeilBrown | c340702 | 2015-10-24 16:23:48 +1100 | [diff] [blame] | 2729 | if (mddev->degraded) |
| 2730 | set_bit(R10BIO_Degraded, &r10_bio->state); |
| 2731 | |
| 2732 | if (test_bit(R10BIO_WriteError, |
| 2733 | &r10_bio->state)) |
| 2734 | close_write(r10_bio); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 2735 | raid_end_bio_io(r10_bio); |
| 2736 | } |
| 2737 | } |
| 2738 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2739 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2740 | for (;;) { |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2741 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 2742 | flush_pending_writes(conf); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2743 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2744 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2745 | if (list_empty(head)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2746 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2747 | break; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2748 | } |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2749 | r10_bio = list_entry(head->prev, struct r10bio, retry_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | list_del(head->prev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2751 | conf->nr_queued--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2752 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2753 | |
| 2754 | mddev = r10_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2755 | conf = mddev->private; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2756 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 2757 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2758 | handle_write_completed(conf, r10_bio); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2759 | else if (test_bit(R10BIO_IsReshape, &r10_bio->state)) |
| 2760 | reshape_request_write(mddev, r10_bio); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2761 | else if (test_bit(R10BIO_IsSync, &r10_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2762 | sync_request_write(mddev, r10_bio); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 2763 | else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | recovery_request_write(mddev, r10_bio); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2765 | else if (test_bit(R10BIO_ReadError, &r10_bio->state)) |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2766 | handle_read_error(mddev, r10_bio); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 2767 | else |
| 2768 | WARN_ON_ONCE(1); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2769 | |
NeilBrown | 1d9d524 | 2009-10-16 15:55:32 +1100 | [diff] [blame] | 2770 | cond_resched(); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2771 | if (mddev->sb_flags & ~(1<<MD_SB_CHANGE_PENDING)) |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2772 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2773 | } |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2774 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | } |
| 2776 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2777 | static int init_resync(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | { |
| 2779 | int buffs; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2780 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2781 | |
| 2782 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; |
Eric Sesterhenn | b638548 | 2006-04-02 13:34:29 +0200 | [diff] [blame] | 2783 | BUG_ON(conf->r10buf_pool); |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2784 | conf->have_replacement = 0; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2785 | for (i = 0; i < conf->geo.raid_disks; i++) |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2786 | if (conf->mirrors[i].replacement) |
| 2787 | conf->have_replacement = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2788 | conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf); |
| 2789 | if (!conf->r10buf_pool) |
| 2790 | return -ENOMEM; |
| 2791 | conf->next_resync = 0; |
| 2792 | return 0; |
| 2793 | } |
| 2794 | |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 2795 | static struct r10bio *raid10_alloc_init_r10buf(struct r10conf *conf) |
| 2796 | { |
| 2797 | struct r10bio *r10bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
| 2798 | struct rsync_pages *rp; |
| 2799 | struct bio *bio; |
| 2800 | int nalloc; |
| 2801 | int i; |
| 2802 | |
| 2803 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery) || |
| 2804 | test_bit(MD_RECOVERY_RESHAPE, &conf->mddev->recovery)) |
| 2805 | nalloc = conf->copies; /* resync */ |
| 2806 | else |
| 2807 | nalloc = 2; /* recovery */ |
| 2808 | |
| 2809 | for (i = 0; i < nalloc; i++) { |
| 2810 | bio = r10bio->devs[i].bio; |
| 2811 | rp = bio->bi_private; |
| 2812 | bio_reset(bio); |
| 2813 | bio->bi_private = rp; |
| 2814 | bio = r10bio->devs[i].repl_bio; |
| 2815 | if (bio) { |
| 2816 | rp = bio->bi_private; |
| 2817 | bio_reset(bio); |
| 2818 | bio->bi_private = rp; |
| 2819 | } |
| 2820 | } |
| 2821 | return r10bio; |
| 2822 | } |
| 2823 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2824 | /* |
| 2825 | * perform a "sync" on one "block" |
| 2826 | * |
| 2827 | * We need to make sure that no normal I/O request - particularly write |
| 2828 | * requests - conflict with active sync requests. |
| 2829 | * |
| 2830 | * This is achieved by tracking pending requests and a 'barrier' concept |
| 2831 | * that can be installed to exclude normal IO requests. |
| 2832 | * |
| 2833 | * Resync and recovery are handled very differently. |
| 2834 | * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. |
| 2835 | * |
| 2836 | * For resync, we iterate over virtual addresses, read all copies, |
| 2837 | * and update if there are differences. If only one copy is live, |
| 2838 | * skip it. |
| 2839 | * For recovery, we iterate over physical addresses, read a good |
| 2840 | * value for each non-in_sync drive, and over-write. |
| 2841 | * |
| 2842 | * So, for recovery we may have several outstanding complex requests for a |
| 2843 | * given address, one for each out-of-sync device. We model this by allocating |
| 2844 | * a number of r10_bio structures, one for each out-of-sync device. |
| 2845 | * As we setup these structures, we collect all bio's together into a list |
| 2846 | * which we then process collectively to add pages, and then process again |
| 2847 | * to pass to generic_make_request. |
| 2848 | * |
| 2849 | * The r10_bio structures are linked using a borrowed master_bio pointer. |
| 2850 | * This link is counted in ->remaining. When the r10_bio that points to NULL |
| 2851 | * has its remaining count decremented to 0, the whole complex operation |
| 2852 | * is complete. |
| 2853 | * |
| 2854 | */ |
| 2855 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 2856 | static sector_t raid10_sync_request(struct mddev *mddev, sector_t sector_nr, |
NeilBrown | 0931479 | 2015-02-19 16:04:40 +1100 | [diff] [blame] | 2857 | int *skipped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2859 | struct r10conf *conf = mddev->private; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2860 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2861 | struct bio *biolist = NULL, *bio; |
| 2862 | sector_t max_sector, nr_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2863 | int i; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2864 | int max_sync; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 2865 | sector_t sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | sector_t sectors_skipped = 0; |
| 2867 | int chunks_skipped = 0; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2868 | sector_t chunk_mask = conf->geo.chunk_mask; |
Ming Lei | 022e510 | 2017-07-14 16:14:42 +0800 | [diff] [blame] | 2869 | int page_idx = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2870 | |
| 2871 | if (!conf->r10buf_pool) |
| 2872 | if (init_resync(conf)) |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2873 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | |
Martin Wilck | 7e83ccb | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2875 | /* |
| 2876 | * Allow skipping a full rebuild for incremental assembly |
| 2877 | * of a clean array, like RAID1 does. |
| 2878 | */ |
| 2879 | if (mddev->bitmap == NULL && |
| 2880 | mddev->recovery_cp == MaxSector && |
NeilBrown | 1376512 | 2013-07-04 16:41:53 +1000 | [diff] [blame] | 2881 | mddev->reshape_position == MaxSector && |
| 2882 | !test_bit(MD_RECOVERY_SYNC, &mddev->recovery) && |
Martin Wilck | 7e83ccb | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2883 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 1376512 | 2013-07-04 16:41:53 +1000 | [diff] [blame] | 2884 | !test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery) && |
Martin Wilck | 7e83ccb | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2885 | conf->fullsync == 0) { |
| 2886 | *skipped = 1; |
NeilBrown | 1376512 | 2013-07-04 16:41:53 +1000 | [diff] [blame] | 2887 | return mddev->dev_sectors - sector_nr; |
Martin Wilck | 7e83ccb | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2888 | } |
| 2889 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2890 | skipped: |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2891 | max_sector = mddev->dev_sectors; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2892 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery) || |
| 2893 | test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2894 | max_sector = mddev->resync_max_sectors; |
| 2895 | if (sector_nr >= max_sector) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2896 | /* If we aborted, we need to abort the |
| 2897 | * sync on the 'current' bitmap chucks (there can |
| 2898 | * be several when recovering multiple devices). |
| 2899 | * as we may have started syncing it but not finished. |
| 2900 | * We can find the current address in |
| 2901 | * mddev->curr_resync, but for recovery, |
| 2902 | * we need to convert that to several |
| 2903 | * virtual addresses. |
| 2904 | */ |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2905 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 2906 | end_reshape(conf); |
NeilBrown | b396855 | 2014-08-18 13:59:50 +1000 | [diff] [blame] | 2907 | close_sync(conf); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2908 | return 0; |
| 2909 | } |
| 2910 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2911 | if (mddev->curr_resync < max_sector) { /* aborted */ |
| 2912 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 2913 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 2914 | &sync_blocks, 1); |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2915 | else for (i = 0; i < conf->geo.raid_disks; i++) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2916 | sector_t sect = |
| 2917 | raid10_find_virt(conf, mddev->curr_resync, i); |
| 2918 | bitmap_end_sync(mddev->bitmap, sect, |
| 2919 | &sync_blocks, 1); |
| 2920 | } |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2921 | } else { |
| 2922 | /* completed sync */ |
| 2923 | if ((!mddev->bitmap || conf->fullsync) |
| 2924 | && conf->have_replacement |
| 2925 | && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 2926 | /* Completed a full sync so the replacements |
| 2927 | * are now fully recovered. |
| 2928 | */ |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 2929 | rcu_read_lock(); |
| 2930 | for (i = 0; i < conf->geo.raid_disks; i++) { |
| 2931 | struct md_rdev *rdev = |
| 2932 | rcu_dereference(conf->mirrors[i].replacement); |
| 2933 | if (rdev) |
| 2934 | rdev->recovery_offset = MaxSector; |
| 2935 | } |
| 2936 | rcu_read_unlock(); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2937 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2938 | conf->fullsync = 0; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2939 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2940 | bitmap_close_sync(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2941 | close_sync(conf); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2942 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2943 | return sectors_skipped; |
| 2944 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 2945 | |
| 2946 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 2947 | return reshape_request(mddev, sector_nr, skipped); |
| 2948 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2949 | if (chunks_skipped >= conf->geo.raid_disks) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2950 | /* if there has been nothing to do on any drive, |
| 2951 | * then there is nothing to do at all.. |
| 2952 | */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2953 | *skipped = 1; |
| 2954 | return (max_sector - sector_nr) + sectors_skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | } |
| 2956 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 2957 | if (max_sector > mddev->resync_max) |
| 2958 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ |
| 2959 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | /* make sure whole request will fit in a chunk - if chunks |
| 2961 | * are meaningful |
| 2962 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2963 | if (conf->geo.near_copies < conf->geo.raid_disks && |
| 2964 | max_sector > (sector_nr | chunk_mask)) |
| 2965 | max_sector = (sector_nr | chunk_mask) + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2966 | |
Tomasz Majchrzak | 7ac5044 | 2016-06-13 15:51:19 +0200 | [diff] [blame] | 2967 | /* |
| 2968 | * If there is non-resync activity waiting for a turn, then let it |
| 2969 | * though before starting on this new sync request. |
| 2970 | */ |
| 2971 | if (conf->nr_waiting) |
| 2972 | schedule_timeout_uninterruptible(1); |
| 2973 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | /* Again, very different code for resync and recovery. |
| 2975 | * Both must result in an r10bio with a list of bios that |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 2976 | * have bi_end_io, bi_sector, bi_disk set, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2977 | * and bi_private set to the r10bio. |
| 2978 | * For recovery, we may actually create several r10bios |
| 2979 | * with 2 bios in each, that correspond to the bios in the main one. |
| 2980 | * In this case, the subordinate r10bios link back through a |
| 2981 | * borrowed master_bio pointer, and the counter in the master |
| 2982 | * includes a ref from each subordinate. |
| 2983 | */ |
| 2984 | /* First, we decide what to do and set ->bi_end_io |
| 2985 | * To end_sync_read if we want to read, and |
| 2986 | * end_sync_write if we will want to write. |
| 2987 | */ |
| 2988 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2989 | max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2990 | if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 2991 | /* recovery... the complicated one */ |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2992 | int j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2993 | r10_bio = NULL; |
| 2994 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 2995 | for (i = 0 ; i < conf->geo.raid_disks; i++) { |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2996 | int still_degraded; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2997 | struct r10bio *rb2; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2998 | sector_t sect; |
| 2999 | int must_sync; |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3000 | int any_working; |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 3001 | struct raid10_info *mirror = &conf->mirrors[i]; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3002 | struct md_rdev *mrdev, *mreplace; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3003 | |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3004 | rcu_read_lock(); |
| 3005 | mrdev = rcu_dereference(mirror->rdev); |
| 3006 | mreplace = rcu_dereference(mirror->replacement); |
| 3007 | |
| 3008 | if ((mrdev == NULL || |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 3009 | test_bit(Faulty, &mrdev->flags) || |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3010 | test_bit(In_sync, &mrdev->flags)) && |
| 3011 | (mreplace == NULL || |
| 3012 | test_bit(Faulty, &mreplace->flags))) { |
| 3013 | rcu_read_unlock(); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3014 | continue; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3015 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3016 | |
| 3017 | still_degraded = 0; |
| 3018 | /* want to reconstruct this device */ |
| 3019 | rb2 = r10_bio; |
| 3020 | sect = raid10_find_virt(conf, sector_nr, i); |
NeilBrown | fc448a1 | 2012-07-03 10:37:30 +1000 | [diff] [blame] | 3021 | if (sect >= mddev->resync_max_sectors) { |
| 3022 | /* last stripe is not complete - don't |
| 3023 | * try to recover this sector. |
| 3024 | */ |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3025 | rcu_read_unlock(); |
NeilBrown | fc448a1 | 2012-07-03 10:37:30 +1000 | [diff] [blame] | 3026 | continue; |
| 3027 | } |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 3028 | if (mreplace && test_bit(Faulty, &mreplace->flags)) |
| 3029 | mreplace = NULL; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3030 | /* Unless we are doing a full sync, or a replacement |
| 3031 | * we only need to recover the block if it is set in |
| 3032 | * the bitmap |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3033 | */ |
| 3034 | must_sync = bitmap_start_sync(mddev->bitmap, sect, |
| 3035 | &sync_blocks, 1); |
| 3036 | if (sync_blocks < max_sync) |
| 3037 | max_sync = sync_blocks; |
| 3038 | if (!must_sync && |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3039 | mreplace == NULL && |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3040 | !conf->fullsync) { |
| 3041 | /* yep, skip the sync_blocks here, but don't assume |
| 3042 | * that there will never be anything to do here |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3043 | */ |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3044 | chunks_skipped = -1; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3045 | rcu_read_unlock(); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3046 | continue; |
| 3047 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3048 | atomic_inc(&mrdev->nr_pending); |
| 3049 | if (mreplace) |
| 3050 | atomic_inc(&mreplace->nr_pending); |
| 3051 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3052 | |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 3053 | r10_bio = raid10_alloc_init_r10buf(conf); |
NeilBrown | cb8b12b | 2014-08-18 14:38:45 +1000 | [diff] [blame] | 3054 | r10_bio->state = 0; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3055 | raise_barrier(conf, rb2 != NULL); |
| 3056 | atomic_set(&r10_bio->remaining, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3057 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3058 | r10_bio->master_bio = (struct bio*)rb2; |
| 3059 | if (rb2) |
| 3060 | atomic_inc(&rb2->remaining); |
| 3061 | r10_bio->mddev = mddev; |
| 3062 | set_bit(R10BIO_IsRecover, &r10_bio->state); |
| 3063 | r10_bio->sector = sect; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3064 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3065 | raid10_find_phys(conf, r10_bio); |
NeilBrown | 1805556 | 2009-05-07 12:48:10 +1000 | [diff] [blame] | 3066 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3067 | /* Need to check if the array will still be |
| 3068 | * degraded |
| 3069 | */ |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3070 | rcu_read_lock(); |
| 3071 | for (j = 0; j < conf->geo.raid_disks; j++) { |
| 3072 | struct md_rdev *rdev = rcu_dereference( |
| 3073 | conf->mirrors[j].rdev); |
| 3074 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) { |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3075 | still_degraded = 1; |
NeilBrown | 87fc767 | 2005-09-09 16:24:04 -0700 | [diff] [blame] | 3076 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3077 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3078 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3079 | |
| 3080 | must_sync = bitmap_start_sync(mddev->bitmap, sect, |
| 3081 | &sync_blocks, still_degraded); |
| 3082 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3083 | any_working = 0; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3084 | for (j=0; j<conf->copies;j++) { |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3085 | int k; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3086 | int d = r10_bio->devs[j].devnum; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3087 | sector_t from_addr, to_addr; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3088 | struct md_rdev *rdev = |
| 3089 | rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3090 | sector_t sector, first_bad; |
| 3091 | int bad_sectors; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3092 | if (!rdev || |
| 3093 | !test_bit(In_sync, &rdev->flags)) |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3094 | continue; |
| 3095 | /* This is where we read from */ |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3096 | any_working = 1; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3097 | sector = r10_bio->devs[j].addr; |
| 3098 | |
| 3099 | if (is_badblock(rdev, sector, max_sync, |
| 3100 | &first_bad, &bad_sectors)) { |
| 3101 | if (first_bad > sector) |
| 3102 | max_sync = first_bad - sector; |
| 3103 | else { |
| 3104 | bad_sectors -= (sector |
| 3105 | - first_bad); |
| 3106 | if (max_sync > bad_sectors) |
| 3107 | max_sync = bad_sectors; |
| 3108 | continue; |
| 3109 | } |
| 3110 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3111 | bio = r10_bio->devs[0].bio; |
| 3112 | bio->bi_next = biolist; |
| 3113 | biolist = bio; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3114 | bio->bi_end_io = end_sync_read; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3115 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 3116 | if (test_bit(FailFast, &rdev->flags)) |
| 3117 | bio->bi_opf |= MD_FAILFAST; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3118 | from_addr = r10_bio->devs[j].addr; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3119 | bio->bi_iter.bi_sector = from_addr + |
| 3120 | rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3121 | bio_set_dev(bio, rdev->bdev); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3122 | atomic_inc(&rdev->nr_pending); |
| 3123 | /* and we write to 'i' (if not in_sync) */ |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3124 | |
| 3125 | for (k=0; k<conf->copies; k++) |
| 3126 | if (r10_bio->devs[k].devnum == i) |
| 3127 | break; |
| 3128 | BUG_ON(k == conf->copies); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3129 | to_addr = r10_bio->devs[k].addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3130 | r10_bio->devs[0].devnum = d; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3131 | r10_bio->devs[0].addr = from_addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3132 | r10_bio->devs[1].devnum = i; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 3133 | r10_bio->devs[1].addr = to_addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3134 | |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3135 | if (!test_bit(In_sync, &mrdev->flags)) { |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3136 | bio = r10_bio->devs[1].bio; |
| 3137 | bio->bi_next = biolist; |
| 3138 | biolist = bio; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3139 | bio->bi_end_io = end_sync_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3140 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3141 | bio->bi_iter.bi_sector = to_addr |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3142 | + mrdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3143 | bio_set_dev(bio, mrdev->bdev); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3144 | atomic_inc(&r10_bio->remaining); |
| 3145 | } else |
| 3146 | r10_bio->devs[1].bio->bi_end_io = NULL; |
| 3147 | |
| 3148 | /* and maybe write to replacement */ |
| 3149 | bio = r10_bio->devs[1].repl_bio; |
| 3150 | if (bio) |
| 3151 | bio->bi_end_io = NULL; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3152 | /* Note: if mreplace != NULL, then bio |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3153 | * cannot be NULL as r10buf_pool_alloc will |
| 3154 | * have allocated it. |
| 3155 | * So the second test here is pointless. |
| 3156 | * But it keeps semantic-checkers happy, and |
| 3157 | * this comment keeps human reviewers |
| 3158 | * happy. |
| 3159 | */ |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3160 | if (mreplace == NULL || bio == NULL || |
| 3161 | test_bit(Faulty, &mreplace->flags)) |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3162 | break; |
| 3163 | bio->bi_next = biolist; |
| 3164 | biolist = bio; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3165 | bio->bi_end_io = end_sync_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3166 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3167 | bio->bi_iter.bi_sector = to_addr + |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3168 | mreplace->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3169 | bio_set_dev(bio, mreplace->bdev); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3170 | atomic_inc(&r10_bio->remaining); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3171 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3173 | rcu_read_unlock(); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3174 | if (j == conf->copies) { |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3175 | /* Cannot recover, so abort the recovery or |
| 3176 | * record a bad block */ |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3177 | if (any_working) { |
| 3178 | /* problem is that there are bad blocks |
| 3179 | * on other device(s) |
| 3180 | */ |
| 3181 | int k; |
| 3182 | for (k = 0; k < conf->copies; k++) |
| 3183 | if (r10_bio->devs[k].devnum == i) |
| 3184 | break; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3185 | if (!test_bit(In_sync, |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3186 | &mrdev->flags) |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3187 | && !rdev_set_badblocks( |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3188 | mrdev, |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3189 | r10_bio->devs[k].addr, |
| 3190 | max_sync, 0)) |
| 3191 | any_working = 0; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3192 | if (mreplace && |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3193 | !rdev_set_badblocks( |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3194 | mreplace, |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3195 | r10_bio->devs[k].addr, |
| 3196 | max_sync, 0)) |
| 3197 | any_working = 0; |
| 3198 | } |
| 3199 | if (!any_working) { |
| 3200 | if (!test_and_set_bit(MD_RECOVERY_INTR, |
| 3201 | &mddev->recovery)) |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3202 | pr_warn("md/raid10:%s: insufficient working devices for recovery.\n", |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3203 | mdname(mddev)); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3204 | mirror->recovery_disabled |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3205 | = mddev->recovery_disabled; |
| 3206 | } |
NeilBrown | e8b8491 | 2014-01-06 10:35:34 +1100 | [diff] [blame] | 3207 | put_buf(r10_bio); |
| 3208 | if (rb2) |
| 3209 | atomic_dec(&rb2->remaining); |
| 3210 | r10_bio = rb2; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3211 | rdev_dec_pending(mrdev, mddev); |
| 3212 | if (mreplace) |
| 3213 | rdev_dec_pending(mreplace, mddev); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3214 | break; |
| 3215 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3216 | rdev_dec_pending(mrdev, mddev); |
| 3217 | if (mreplace) |
| 3218 | rdev_dec_pending(mreplace, mddev); |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 3219 | if (r10_bio->devs[0].bio->bi_opf & MD_FAILFAST) { |
| 3220 | /* Only want this if there is elsewhere to |
| 3221 | * read from. 'j' is currently the first |
| 3222 | * readable copy. |
| 3223 | */ |
| 3224 | int targets = 1; |
| 3225 | for (; j < conf->copies; j++) { |
| 3226 | int d = r10_bio->devs[j].devnum; |
| 3227 | if (conf->mirrors[d].rdev && |
| 3228 | test_bit(In_sync, |
| 3229 | &conf->mirrors[d].rdev->flags)) |
| 3230 | targets++; |
| 3231 | } |
| 3232 | if (targets == 1) |
| 3233 | r10_bio->devs[0].bio->bi_opf |
| 3234 | &= ~MD_FAILFAST; |
| 3235 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3236 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3237 | if (biolist == NULL) { |
| 3238 | while (r10_bio) { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 3239 | struct r10bio *rb2 = r10_bio; |
| 3240 | r10_bio = (struct r10bio*) rb2->master_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | rb2->master_bio = NULL; |
| 3242 | put_buf(rb2); |
| 3243 | } |
| 3244 | goto giveup; |
| 3245 | } |
| 3246 | } else { |
| 3247 | /* resync. Schedule a read for every block at this virt offset */ |
| 3248 | int count = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3249 | |
Goldwyn Rodrigues | c40f341 | 2015-08-19 08:14:42 +1000 | [diff] [blame] | 3250 | bitmap_cond_end_sync(mddev->bitmap, sector_nr, 0); |
NeilBrown | 78200d4 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 3251 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3252 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, |
| 3253 | &sync_blocks, mddev->degraded) && |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3254 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, |
| 3255 | &mddev->recovery)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3256 | /* We can skip this block */ |
| 3257 | *skipped = 1; |
| 3258 | return sync_blocks + sectors_skipped; |
| 3259 | } |
| 3260 | if (sync_blocks < max_sync) |
| 3261 | max_sync = sync_blocks; |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 3262 | r10_bio = raid10_alloc_init_r10buf(conf); |
NeilBrown | cb8b12b | 2014-08-18 14:38:45 +1000 | [diff] [blame] | 3263 | r10_bio->state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | r10_bio->mddev = mddev; |
| 3266 | atomic_set(&r10_bio->remaining, 0); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3267 | raise_barrier(conf, 0); |
| 3268 | conf->next_resync = sector_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3269 | |
| 3270 | r10_bio->master_bio = NULL; |
| 3271 | r10_bio->sector = sector_nr; |
| 3272 | set_bit(R10BIO_IsSync, &r10_bio->state); |
| 3273 | raid10_find_phys(conf, r10_bio); |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3274 | r10_bio->sectors = (sector_nr | chunk_mask) - sector_nr + 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3275 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3276 | for (i = 0; i < conf->copies; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3277 | int d = r10_bio->devs[i].devnum; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3278 | sector_t first_bad, sector; |
| 3279 | int bad_sectors; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3280 | struct md_rdev *rdev; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3281 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3282 | if (r10_bio->devs[i].repl_bio) |
| 3283 | r10_bio->devs[i].repl_bio->bi_end_io = NULL; |
| 3284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3285 | bio = r10_bio->devs[i].bio; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3286 | bio->bi_status = BLK_STS_IOERR; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3287 | rcu_read_lock(); |
| 3288 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 3289 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) { |
| 3290 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3291 | continue; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3292 | } |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3293 | sector = r10_bio->devs[i].addr; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3294 | if (is_badblock(rdev, sector, max_sync, |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3295 | &first_bad, &bad_sectors)) { |
| 3296 | if (first_bad > sector) |
| 3297 | max_sync = first_bad - sector; |
| 3298 | else { |
| 3299 | bad_sectors -= (sector - first_bad); |
| 3300 | if (max_sync > bad_sectors) |
Dan Carpenter | 91502f0 | 2012-10-11 14:20:58 +1100 | [diff] [blame] | 3301 | max_sync = bad_sectors; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3302 | rcu_read_unlock(); |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3303 | continue; |
| 3304 | } |
| 3305 | } |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3306 | atomic_inc(&rdev->nr_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3307 | atomic_inc(&r10_bio->remaining); |
| 3308 | bio->bi_next = biolist; |
| 3309 | biolist = bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | bio->bi_end_io = end_sync_read; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3311 | bio_set_op_attrs(bio, REQ_OP_READ, 0); |
Guoqing Jiang | 1cdd125 | 2017-06-13 11:16:08 +0800 | [diff] [blame] | 3312 | if (test_bit(FailFast, &rdev->flags)) |
NeilBrown | 8d3ca83 | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 3313 | bio->bi_opf |= MD_FAILFAST; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3314 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3315 | bio_set_dev(bio, rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3316 | count++; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3317 | |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3318 | rdev = rcu_dereference(conf->mirrors[d].replacement); |
| 3319 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) { |
| 3320 | rcu_read_unlock(); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3321 | continue; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3322 | } |
| 3323 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3324 | |
| 3325 | /* Need to set up for writing to the replacement */ |
| 3326 | bio = r10_bio->devs[i].repl_bio; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3327 | bio->bi_status = BLK_STS_IOERR; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3328 | |
| 3329 | sector = r10_bio->devs[i].addr; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3330 | bio->bi_next = biolist; |
| 3331 | biolist = bio; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3332 | bio->bi_end_io = end_sync_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 3333 | bio_set_op_attrs(bio, REQ_OP_WRITE, 0); |
Guoqing Jiang | 1cdd125 | 2017-06-13 11:16:08 +0800 | [diff] [blame] | 3334 | if (test_bit(FailFast, &rdev->flags)) |
NeilBrown | 1919cbb | 2016-11-18 16:16:12 +1100 | [diff] [blame] | 3335 | bio->bi_opf |= MD_FAILFAST; |
NeilBrown | f90145f | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3336 | bio->bi_iter.bi_sector = sector + rdev->data_offset; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3337 | bio_set_dev(bio, rdev->bdev); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3338 | count++; |
Guoqing Jiang | 1cdd125 | 2017-06-13 11:16:08 +0800 | [diff] [blame] | 3339 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3340 | } |
| 3341 | |
| 3342 | if (count < 2) { |
| 3343 | for (i=0; i<conf->copies; i++) { |
| 3344 | int d = r10_bio->devs[i].devnum; |
| 3345 | if (r10_bio->devs[i].bio->bi_end_io) |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3346 | rdev_dec_pending(conf->mirrors[d].rdev, |
| 3347 | mddev); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3348 | if (r10_bio->devs[i].repl_bio && |
| 3349 | r10_bio->devs[i].repl_bio->bi_end_io) |
| 3350 | rdev_dec_pending( |
| 3351 | conf->mirrors[d].replacement, |
| 3352 | mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3353 | } |
| 3354 | put_buf(r10_bio); |
| 3355 | biolist = NULL; |
| 3356 | goto giveup; |
| 3357 | } |
| 3358 | } |
| 3359 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3360 | nr_sectors = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3361 | if (sector_nr + max_sync < max_sector) |
| 3362 | max_sector = sector_nr + max_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3363 | do { |
| 3364 | struct page *page; |
| 3365 | int len = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | if (sector_nr + (len>>9) > max_sector) |
| 3367 | len = (max_sector - sector_nr) << 9; |
| 3368 | if (len == 0) |
| 3369 | break; |
| 3370 | for (bio= biolist ; bio ; bio=bio->bi_next) { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 3371 | struct resync_pages *rp = get_resync_pages(bio); |
Ming Lei | 022e510 | 2017-07-14 16:14:42 +0800 | [diff] [blame] | 3372 | page = resync_fetch_page(rp, page_idx); |
Ming Lei | c85ba14 | 2017-03-17 00:12:22 +0800 | [diff] [blame] | 3373 | /* |
| 3374 | * won't fail because the vec table is big enough |
| 3375 | * to hold all these pages |
| 3376 | */ |
| 3377 | bio_add_page(bio, page, len, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3378 | } |
| 3379 | nr_sectors += len>>9; |
| 3380 | sector_nr += len>>9; |
Ming Lei | 022e510 | 2017-07-14 16:14:42 +0800 | [diff] [blame] | 3381 | } while (++page_idx < RESYNC_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3382 | r10_bio->sectors = nr_sectors; |
| 3383 | |
| 3384 | while (biolist) { |
| 3385 | bio = biolist; |
| 3386 | biolist = biolist->bi_next; |
| 3387 | |
| 3388 | bio->bi_next = NULL; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 3389 | r10_bio = get_resync_r10bio(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3390 | r10_bio->sectors = nr_sectors; |
| 3391 | |
| 3392 | if (bio->bi_end_io == end_sync_read) { |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 3393 | md_sync_acct_bio(bio, nr_sectors); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 3394 | bio->bi_status = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3395 | generic_make_request(bio); |
| 3396 | } |
| 3397 | } |
| 3398 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3399 | if (sectors_skipped) |
| 3400 | /* pretend they weren't skipped, it makes |
| 3401 | * no important difference in this case |
| 3402 | */ |
| 3403 | md_done_sync(mddev, sectors_skipped, 1); |
| 3404 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | return sectors_skipped + nr_sectors; |
| 3406 | giveup: |
| 3407 | /* There is nowhere to write, so all non-sync |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3408 | * drives must be failed or in resync, all drives |
| 3409 | * have a bad block, so try the next chunk... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | */ |
NeilBrown | 09b4068 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 3411 | if (sector_nr + max_sync < max_sector) |
| 3412 | max_sector = sector_nr + max_sync; |
| 3413 | |
| 3414 | sectors_skipped += (max_sector - sector_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3415 | chunks_skipped ++; |
| 3416 | sector_nr = max_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3417 | goto skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3418 | } |
| 3419 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3420 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3421 | raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3422 | { |
| 3423 | sector_t size; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3424 | struct r10conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3425 | |
| 3426 | if (!raid_disks) |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3427 | raid_disks = min(conf->geo.raid_disks, |
| 3428 | conf->prev.raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3429 | if (!sectors) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3430 | sectors = conf->dev_sectors; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3431 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3432 | size = sectors >> conf->geo.chunk_shift; |
| 3433 | sector_div(size, conf->geo.far_copies); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3434 | size = size * raid_disks; |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3435 | sector_div(size, conf->geo.near_copies); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3436 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3437 | return size << conf->geo.chunk_shift; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3438 | } |
| 3439 | |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3440 | static void calc_sectors(struct r10conf *conf, sector_t size) |
| 3441 | { |
| 3442 | /* Calculate the number of sectors-per-device that will |
| 3443 | * actually be used, and set conf->dev_sectors and |
| 3444 | * conf->stride |
| 3445 | */ |
| 3446 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3447 | size = size >> conf->geo.chunk_shift; |
| 3448 | sector_div(size, conf->geo.far_copies); |
| 3449 | size = size * conf->geo.raid_disks; |
| 3450 | sector_div(size, conf->geo.near_copies); |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3451 | /* 'size' is now the number of chunks in the array */ |
| 3452 | /* calculate "used chunks per device" */ |
| 3453 | size = size * conf->copies; |
| 3454 | |
| 3455 | /* We need to round up when dividing by raid_disks to |
| 3456 | * get the stride size. |
| 3457 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3458 | size = DIV_ROUND_UP_SECTOR_T(size, conf->geo.raid_disks); |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3459 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3460 | conf->dev_sectors = size << conf->geo.chunk_shift; |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3461 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3462 | if (conf->geo.far_offset) |
| 3463 | conf->geo.stride = 1 << conf->geo.chunk_shift; |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3464 | else { |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3465 | sector_div(size, conf->geo.far_copies); |
| 3466 | conf->geo.stride = size << conf->geo.chunk_shift; |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3467 | } |
| 3468 | } |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3469 | |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3470 | enum geo_type {geo_new, geo_old, geo_start}; |
| 3471 | static int setup_geo(struct geom *geo, struct mddev *mddev, enum geo_type new) |
| 3472 | { |
| 3473 | int nc, fc, fo; |
| 3474 | int layout, chunk, disks; |
| 3475 | switch (new) { |
| 3476 | case geo_old: |
| 3477 | layout = mddev->layout; |
| 3478 | chunk = mddev->chunk_sectors; |
| 3479 | disks = mddev->raid_disks - mddev->delta_disks; |
| 3480 | break; |
| 3481 | case geo_new: |
| 3482 | layout = mddev->new_layout; |
| 3483 | chunk = mddev->new_chunk_sectors; |
| 3484 | disks = mddev->raid_disks; |
| 3485 | break; |
| 3486 | default: /* avoid 'may be unused' warnings */ |
| 3487 | case geo_start: /* new when starting reshape - raid_disks not |
| 3488 | * updated yet. */ |
| 3489 | layout = mddev->new_layout; |
| 3490 | chunk = mddev->new_chunk_sectors; |
| 3491 | disks = mddev->raid_disks + mddev->delta_disks; |
| 3492 | break; |
| 3493 | } |
NeilBrown | 8bce6d3 | 2015-10-22 13:20:15 +1100 | [diff] [blame] | 3494 | if (layout >> 19) |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3495 | return -1; |
| 3496 | if (chunk < (PAGE_SIZE >> 9) || |
| 3497 | !is_power_of_2(chunk)) |
| 3498 | return -2; |
| 3499 | nc = layout & 255; |
| 3500 | fc = (layout >> 8) & 255; |
| 3501 | fo = layout & (1<<16); |
| 3502 | geo->raid_disks = disks; |
| 3503 | geo->near_copies = nc; |
| 3504 | geo->far_copies = fc; |
| 3505 | geo->far_offset = fo; |
NeilBrown | 8bce6d3 | 2015-10-22 13:20:15 +1100 | [diff] [blame] | 3506 | switch (layout >> 17) { |
| 3507 | case 0: /* original layout. simple but not always optimal */ |
| 3508 | geo->far_set_size = disks; |
| 3509 | break; |
| 3510 | case 1: /* "improved" layout which was buggy. Hopefully no-one is |
| 3511 | * actually using this, but leave code here just in case.*/ |
| 3512 | geo->far_set_size = disks/fc; |
| 3513 | WARN(geo->far_set_size < fc, |
| 3514 | "This RAID10 layout does not provide data safety - please backup and create new array\n"); |
| 3515 | break; |
| 3516 | case 2: /* "improved" layout fixed to match documentation */ |
| 3517 | geo->far_set_size = fc * nc; |
| 3518 | break; |
| 3519 | default: /* Not a valid layout */ |
| 3520 | return -1; |
| 3521 | } |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3522 | geo->chunk_mask = chunk - 1; |
| 3523 | geo->chunk_shift = ffz(~chunk); |
| 3524 | return nc*fc; |
| 3525 | } |
| 3526 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3527 | static struct r10conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3528 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3529 | struct r10conf *conf = NULL; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3530 | int err = -EINVAL; |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3531 | struct geom geo; |
| 3532 | int copies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3534 | copies = setup_geo(&geo, mddev, geo_new); |
| 3535 | |
| 3536 | if (copies == -2) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3537 | pr_warn("md/raid10:%s: chunk size must be at least PAGE_SIZE(%ld) and be a power of 2.\n", |
| 3538 | mdname(mddev), PAGE_SIZE); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3539 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3540 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3541 | |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3542 | if (copies < 2 || copies > mddev->raid_disks) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3543 | pr_warn("md/raid10:%s: unsupported raid10 layout: 0x%8x\n", |
| 3544 | mdname(mddev), mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3545 | goto out; |
| 3546 | } |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3547 | |
| 3548 | err = -ENOMEM; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3549 | conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3550 | if (!conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3551 | goto out; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3552 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3553 | /* FIXME calc properly */ |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 3554 | conf->mirrors = kzalloc(sizeof(struct raid10_info)*(mddev->raid_disks + |
NeilBrown | 78eaa0d | 2013-07-02 15:58:05 +1000 | [diff] [blame] | 3555 | max(0,-mddev->delta_disks)), |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3556 | GFP_KERNEL); |
| 3557 | if (!conf->mirrors) |
| 3558 | goto out; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 3559 | |
| 3560 | conf->tmppage = alloc_page(GFP_KERNEL); |
| 3561 | if (!conf->tmppage) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3562 | goto out; |
| 3563 | |
NeilBrown | deb200d | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3564 | conf->geo = geo; |
| 3565 | conf->copies = copies; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3566 | conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc, |
| 3567 | r10bio_pool_free, conf); |
| 3568 | if (!conf->r10bio_pool) |
| 3569 | goto out; |
| 3570 | |
NeilBrown | 011067b | 2017-06-18 14:38:57 +1000 | [diff] [blame] | 3571 | conf->bio_split = bioset_create(BIO_POOL_SIZE, 0, 0); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 3572 | if (!conf->bio_split) |
| 3573 | goto out; |
| 3574 | |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3575 | calc_sectors(conf, mddev->dev_sectors); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3576 | if (mddev->reshape_position == MaxSector) { |
| 3577 | conf->prev = conf->geo; |
| 3578 | conf->reshape_progress = MaxSector; |
| 3579 | } else { |
| 3580 | if (setup_geo(&conf->prev, mddev, geo_old) != conf->copies) { |
| 3581 | err = -EINVAL; |
| 3582 | goto out; |
| 3583 | } |
| 3584 | conf->reshape_progress = mddev->reshape_position; |
| 3585 | if (conf->prev.far_offset) |
| 3586 | conf->prev.stride = 1 << conf->prev.chunk_shift; |
| 3587 | else |
| 3588 | /* far_copies must be 1 */ |
| 3589 | conf->prev.stride = conf->dev_sectors; |
| 3590 | } |
NeilBrown | 299b068 | 2015-07-06 17:37:49 +1000 | [diff] [blame] | 3591 | conf->reshape_safe = conf->reshape_progress; |
Neil Brown | e7e72bf | 2008-05-14 16:05:54 -0700 | [diff] [blame] | 3592 | spin_lock_init(&conf->device_lock); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3593 | INIT_LIST_HEAD(&conf->retry_list); |
NeilBrown | 95af587 | 2015-08-14 11:26:17 +1000 | [diff] [blame] | 3594 | INIT_LIST_HEAD(&conf->bio_end_io_list); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3595 | |
| 3596 | spin_lock_init(&conf->resync_lock); |
| 3597 | init_waitqueue_head(&conf->wait_barrier); |
Tomasz Majchrzak | 0e5313e | 2016-06-24 14:20:16 +0200 | [diff] [blame] | 3598 | atomic_set(&conf->nr_pending, 0); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3599 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 3600 | conf->thread = md_register_thread(raid10d, mddev, "raid10"); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3601 | if (!conf->thread) |
| 3602 | goto out; |
| 3603 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3604 | conf->mddev = mddev; |
| 3605 | return conf; |
| 3606 | |
| 3607 | out: |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3608 | if (conf) { |
Julia Lawall | 644df1a | 2015-09-13 14:15:10 +0200 | [diff] [blame] | 3609 | mempool_destroy(conf->r10bio_pool); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3610 | kfree(conf->mirrors); |
| 3611 | safe_put_page(conf->tmppage); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 3612 | if (conf->bio_split) |
| 3613 | bioset_free(conf->bio_split); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3614 | kfree(conf); |
| 3615 | } |
| 3616 | return ERR_PTR(err); |
| 3617 | } |
| 3618 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 3619 | static int raid10_run(struct mddev *mddev) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3620 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3621 | struct r10conf *conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3622 | int i, disk_idx, chunk_size; |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 3623 | struct raid10_info *disk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3624 | struct md_rdev *rdev; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3625 | sector_t size; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3626 | sector_t min_offset_diff = 0; |
| 3627 | int first = 1; |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 3628 | bool discard_supported = false; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3629 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 3630 | if (mddev_init_writes_pending(mddev) < 0) |
| 3631 | return -ENOMEM; |
| 3632 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3633 | if (mddev->private == NULL) { |
| 3634 | conf = setup_conf(mddev); |
| 3635 | if (IS_ERR(conf)) |
| 3636 | return PTR_ERR(conf); |
| 3637 | mddev->private = conf; |
| 3638 | } |
| 3639 | conf = mddev->private; |
| 3640 | if (!conf) |
| 3641 | goto out; |
| 3642 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3643 | mddev->thread = conf->thread; |
| 3644 | conf->thread = NULL; |
| 3645 | |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 3646 | chunk_size = mddev->chunk_sectors << 9; |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3647 | if (mddev->queue) { |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 3648 | blk_queue_max_discard_sectors(mddev->queue, |
| 3649 | mddev->chunk_sectors); |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 3650 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
Christoph Hellwig | 3deff1a | 2017-04-05 19:21:03 +0200 | [diff] [blame] | 3651 | blk_queue_max_write_zeroes_sectors(mddev->queue, 0); |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3652 | blk_queue_io_min(mddev->queue, chunk_size); |
| 3653 | if (conf->geo.raid_disks % conf->geo.near_copies) |
| 3654 | blk_queue_io_opt(mddev->queue, chunk_size * conf->geo.raid_disks); |
| 3655 | else |
| 3656 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 3657 | (conf->geo.raid_disks / conf->geo.near_copies)); |
| 3658 | } |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 3659 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3660 | rdev_for_each(rdev, mddev) { |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3661 | long long diff; |
NeilBrown | 34b343c | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3662 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3663 | disk_idx = rdev->raid_disk; |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3664 | if (disk_idx < 0) |
| 3665 | continue; |
| 3666 | if (disk_idx >= conf->geo.raid_disks && |
| 3667 | disk_idx >= conf->prev.raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3668 | continue; |
| 3669 | disk = conf->mirrors + disk_idx; |
| 3670 | |
NeilBrown | 56a2559b | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3671 | if (test_bit(Replacement, &rdev->flags)) { |
| 3672 | if (disk->replacement) |
| 3673 | goto out_free_conf; |
| 3674 | disk->replacement = rdev; |
| 3675 | } else { |
| 3676 | if (disk->rdev) |
| 3677 | goto out_free_conf; |
| 3678 | disk->rdev = rdev; |
| 3679 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3680 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 3681 | if (!mddev->reshape_backwards) |
| 3682 | diff = -diff; |
| 3683 | if (diff < 0) |
| 3684 | diff = 0; |
| 3685 | if (first || diff < min_offset_diff) |
| 3686 | min_offset_diff = diff; |
NeilBrown | 56a2559b | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3687 | |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3688 | if (mddev->gendisk) |
| 3689 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 3690 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3691 | |
| 3692 | disk->head_position = 0; |
Shaohua Li | 532a2a3 | 2012-10-11 13:30:52 +1100 | [diff] [blame] | 3693 | |
| 3694 | if (blk_queue_discard(bdev_get_queue(rdev->bdev))) |
| 3695 | discard_supported = true; |
Guoqing Jiang | 6f287ca | 2017-04-06 09:12:18 +0800 | [diff] [blame] | 3696 | first = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3697 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3698 | |
Jonathan Brassow | ed30be0 | 2012-10-31 11:42:30 +1100 | [diff] [blame] | 3699 | if (mddev->queue) { |
| 3700 | if (discard_supported) |
| 3701 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 3702 | mddev->queue); |
| 3703 | else |
| 3704 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, |
| 3705 | mddev->queue); |
| 3706 | } |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 3707 | /* need to check that every block has at least one working mirror */ |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3708 | if (!enough(conf, -1)) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3709 | pr_err("md/raid10:%s: not enough operational mirrors.\n", |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 3710 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3711 | goto out_free_conf; |
| 3712 | } |
| 3713 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3714 | if (conf->reshape_progress != MaxSector) { |
| 3715 | /* must ensure that shape change is supported */ |
| 3716 | if (conf->geo.far_copies != 1 && |
| 3717 | conf->geo.far_offset == 0) |
| 3718 | goto out_free_conf; |
| 3719 | if (conf->prev.far_copies != 1 && |
NeilBrown | 78eaa0d | 2013-07-02 15:58:05 +1000 | [diff] [blame] | 3720 | conf->prev.far_offset == 0) |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3721 | goto out_free_conf; |
| 3722 | } |
| 3723 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3724 | mddev->degraded = 0; |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3725 | for (i = 0; |
| 3726 | i < conf->geo.raid_disks |
| 3727 | || i < conf->prev.raid_disks; |
| 3728 | i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3729 | |
| 3730 | disk = conf->mirrors + i; |
| 3731 | |
NeilBrown | 56a2559b | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3732 | if (!disk->rdev && disk->replacement) { |
| 3733 | /* The replacement is all we have - use it */ |
| 3734 | disk->rdev = disk->replacement; |
| 3735 | disk->replacement = NULL; |
| 3736 | clear_bit(Replacement, &disk->rdev->flags); |
| 3737 | } |
| 3738 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3739 | if (!disk->rdev || |
NeilBrown | 2e333e8 | 2006-10-21 10:24:07 -0700 | [diff] [blame] | 3740 | !test_bit(In_sync, &disk->rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3741 | disk->head_position = 0; |
| 3742 | mddev->degraded++; |
NeilBrown | 0b59bb6 | 2014-01-14 16:30:10 +1100 | [diff] [blame] | 3743 | if (disk->rdev && |
| 3744 | disk->rdev->saved_raid_disk < 0) |
Neil Brown | 8c2e870 | 2008-06-28 08:30:52 +1000 | [diff] [blame] | 3745 | conf->fullsync = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3746 | } |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 3747 | disk->recovery_disabled = mddev->recovery_disabled - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3748 | } |
| 3749 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 3750 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3751 | pr_notice("md/raid10:%s: not clean -- starting background reconstruction\n", |
| 3752 | mdname(mddev)); |
| 3753 | pr_info("md/raid10:%s: active with %d out of %d devices\n", |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3754 | mdname(mddev), conf->geo.raid_disks - mddev->degraded, |
| 3755 | conf->geo.raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | /* |
| 3757 | * Ok, everything is just fine now |
| 3758 | */ |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3759 | mddev->dev_sectors = conf->dev_sectors; |
| 3760 | size = raid10_size(mddev, 0, 0); |
| 3761 | md_set_array_sectors(mddev, size); |
| 3762 | mddev->resync_max_sectors = size; |
NeilBrown | 46533ff | 2016-11-18 16:16:11 +1100 | [diff] [blame] | 3763 | set_bit(MD_FAILFAST_SUPPORTED, &mddev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3764 | |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3765 | if (mddev->queue) { |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3766 | int stripe = conf->geo.raid_disks * |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3767 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Jonathan Brassow | cc4d1ef | 2012-07-31 10:03:53 +1000 | [diff] [blame] | 3768 | |
| 3769 | /* Calculate max read-ahead size. |
| 3770 | * We need to readahead at least twice a whole stripe.... |
| 3771 | * maybe... |
| 3772 | */ |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3773 | stripe /= conf->geo.near_copies; |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 3774 | if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
| 3775 | mddev->queue->backing_dev_info->ra_pages = 2 * stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3776 | } |
| 3777 | |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 3778 | if (md_integrity_register(mddev)) |
| 3779 | goto out_free_conf; |
| 3780 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3781 | if (conf->reshape_progress != MaxSector) { |
| 3782 | unsigned long before_length, after_length; |
| 3783 | |
| 3784 | before_length = ((1 << conf->prev.chunk_shift) * |
| 3785 | conf->prev.far_copies); |
| 3786 | after_length = ((1 << conf->geo.chunk_shift) * |
| 3787 | conf->geo.far_copies); |
| 3788 | |
| 3789 | if (max(before_length, after_length) > min_offset_diff) { |
| 3790 | /* This cannot work */ |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3791 | pr_warn("md/raid10: offset difference not enough to continue reshape\n"); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3792 | goto out_free_conf; |
| 3793 | } |
| 3794 | conf->offset_diff = min_offset_diff; |
| 3795 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3796 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 3797 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 3798 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 3799 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 3800 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 3801 | "reshape"); |
| 3802 | } |
| 3803 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3804 | return 0; |
| 3805 | |
| 3806 | out_free_conf: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 3807 | md_unregister_thread(&mddev->thread); |
Julia Lawall | 644df1a | 2015-09-13 14:15:10 +0200 | [diff] [blame] | 3808 | mempool_destroy(conf->r10bio_pool); |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 3809 | safe_put_page(conf->tmppage); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 3810 | kfree(conf->mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3811 | kfree(conf); |
| 3812 | mddev->private = NULL; |
| 3813 | out: |
| 3814 | return -EIO; |
| 3815 | } |
| 3816 | |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3817 | static void raid10_free(struct mddev *mddev, void *priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3818 | { |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 3819 | struct r10conf *conf = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3820 | |
Julia Lawall | 644df1a | 2015-09-13 14:15:10 +0200 | [diff] [blame] | 3821 | mempool_destroy(conf->r10bio_pool); |
Hirokazu Takahashi | 0fea7ed | 2013-04-24 11:42:44 +1000 | [diff] [blame] | 3822 | safe_put_page(conf->tmppage); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 3823 | kfree(conf->mirrors); |
NeilBrown | c4796e2 | 2014-08-23 20:19:26 +1000 | [diff] [blame] | 3824 | kfree(conf->mirrors_old); |
| 3825 | kfree(conf->mirrors_new); |
NeilBrown | fc9977d | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 3826 | if (conf->bio_split) |
| 3827 | bioset_free(conf->bio_split); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3828 | kfree(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3829 | } |
| 3830 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3831 | static void raid10_quiesce(struct mddev *mddev, int state) |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3832 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3833 | struct r10conf *conf = mddev->private; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3834 | |
| 3835 | switch(state) { |
| 3836 | case 1: |
| 3837 | raise_barrier(conf, 0); |
| 3838 | break; |
| 3839 | case 0: |
| 3840 | lower_barrier(conf); |
| 3841 | break; |
| 3842 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3843 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3844 | |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 3845 | static int raid10_resize(struct mddev *mddev, sector_t sectors) |
| 3846 | { |
| 3847 | /* Resize of 'far' arrays is not supported. |
| 3848 | * For 'near' and 'offset' arrays we can set the |
| 3849 | * number of sectors used to be an appropriate multiple |
| 3850 | * of the chunk size. |
| 3851 | * For 'offset', this is far_copies*chunksize. |
| 3852 | * For 'near' the multiplier is the LCM of |
| 3853 | * near_copies and raid_disks. |
| 3854 | * So if far_copies > 1 && !far_offset, fail. |
| 3855 | * Else find LCM(raid_disks, near_copy)*far_copies and |
| 3856 | * multiply by chunk_size. Then round to this number. |
| 3857 | * This is mostly done by raid10_size() |
| 3858 | */ |
| 3859 | struct r10conf *conf = mddev->private; |
| 3860 | sector_t oldsize, size; |
| 3861 | |
NeilBrown | f8c9e74 | 2012-05-21 09:28:33 +1000 | [diff] [blame] | 3862 | if (mddev->reshape_position != MaxSector) |
| 3863 | return -EBUSY; |
| 3864 | |
NeilBrown | 5cf00fc | 2012-05-21 09:28:20 +1000 | [diff] [blame] | 3865 | if (conf->geo.far_copies > 1 && !conf->geo.far_offset) |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 3866 | return -EINVAL; |
| 3867 | |
| 3868 | oldsize = raid10_size(mddev, 0, 0); |
| 3869 | size = raid10_size(mddev, sectors, 0); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 3870 | if (mddev->external_size && |
| 3871 | mddev->array_sectors > size) |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 3872 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 3873 | if (mddev->bitmap) { |
| 3874 | int ret = bitmap_resize(mddev->bitmap, size, 0, 0); |
| 3875 | if (ret) |
| 3876 | return ret; |
| 3877 | } |
| 3878 | md_set_array_sectors(mddev, size); |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 3879 | if (sectors > mddev->dev_sectors && |
| 3880 | mddev->recovery_cp > oldsize) { |
| 3881 | mddev->recovery_cp = oldsize; |
| 3882 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3883 | } |
NeilBrown | 6508fdb | 2012-05-17 10:08:45 +1000 | [diff] [blame] | 3884 | calc_sectors(conf, sectors); |
| 3885 | mddev->dev_sectors = conf->dev_sectors; |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 3886 | mddev->resync_max_sectors = size; |
| 3887 | return 0; |
| 3888 | } |
| 3889 | |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 3890 | 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] | 3891 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3892 | struct md_rdev *rdev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3893 | struct r10conf *conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3894 | |
| 3895 | if (mddev->degraded > 0) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3896 | pr_warn("md/raid10:%s: Error: degraded raid0!\n", |
| 3897 | mdname(mddev)); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3898 | return ERR_PTR(-EINVAL); |
| 3899 | } |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 3900 | sector_div(size, devs); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3901 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3902 | /* Set new parameters */ |
| 3903 | mddev->new_level = 10; |
| 3904 | /* new layout: far_copies = 1, near_copies = 2 */ |
| 3905 | mddev->new_layout = (1<<8) + 2; |
| 3906 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 3907 | mddev->delta_disks = mddev->raid_disks; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3908 | mddev->raid_disks *= 2; |
| 3909 | /* make sure it will be not marked as dirty */ |
| 3910 | mddev->recovery_cp = MaxSector; |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 3911 | mddev->dev_sectors = size; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3912 | |
| 3913 | conf = setup_conf(mddev); |
Krzysztof Wojcik | 02214dc | 2011-02-04 14:18:26 +0100 | [diff] [blame] | 3914 | if (!IS_ERR(conf)) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3915 | rdev_for_each(rdev, mddev) |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 3916 | if (rdev->raid_disk >= 0) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3917 | rdev->new_raid_disk = rdev->raid_disk * 2; |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 3918 | rdev->sectors = size; |
| 3919 | } |
Krzysztof Wojcik | 02214dc | 2011-02-04 14:18:26 +0100 | [diff] [blame] | 3920 | conf->barrier = 1; |
| 3921 | } |
| 3922 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3923 | return conf; |
| 3924 | } |
| 3925 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3926 | static void *raid10_takeover(struct mddev *mddev) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3927 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 3928 | struct r0conf *raid0_conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3929 | |
| 3930 | /* raid10 can take over: |
| 3931 | * raid0 - providing it has only two drives |
| 3932 | */ |
| 3933 | if (mddev->level == 0) { |
| 3934 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 3935 | raid0_conf = mddev->private; |
| 3936 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3937 | pr_warn("md/raid10:%s: cannot takeover raid 0 with more than one zone.\n", |
| 3938 | mdname(mddev)); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3939 | return ERR_PTR(-EINVAL); |
| 3940 | } |
NeilBrown | 53a6ab4 | 2015-02-12 14:09:57 +1100 | [diff] [blame] | 3941 | return raid10_takeover_raid0(mddev, |
| 3942 | raid0_conf->strip_zone->zone_end, |
| 3943 | raid0_conf->strip_zone->nb_dev); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3944 | } |
| 3945 | return ERR_PTR(-EINVAL); |
| 3946 | } |
| 3947 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3948 | static int raid10_check_reshape(struct mddev *mddev) |
| 3949 | { |
| 3950 | /* Called when there is a request to change |
| 3951 | * - layout (to ->new_layout) |
| 3952 | * - chunk size (to ->new_chunk_sectors) |
| 3953 | * - raid_disks (by delta_disks) |
| 3954 | * or when trying to restart a reshape that was ongoing. |
| 3955 | * |
| 3956 | * We need to validate the request and possibly allocate |
| 3957 | * space if that might be an issue later. |
| 3958 | * |
| 3959 | * Currently we reject any reshape of a 'far' mode array, |
| 3960 | * allow chunk size to change if new is generally acceptable, |
| 3961 | * allow raid_disks to increase, and allow |
| 3962 | * a switch between 'near' mode and 'offset' mode. |
| 3963 | */ |
| 3964 | struct r10conf *conf = mddev->private; |
| 3965 | struct geom geo; |
| 3966 | |
| 3967 | if (conf->geo.far_copies != 1 && !conf->geo.far_offset) |
| 3968 | return -EINVAL; |
| 3969 | |
| 3970 | if (setup_geo(&geo, mddev, geo_start) != conf->copies) |
| 3971 | /* mustn't change number of copies */ |
| 3972 | return -EINVAL; |
| 3973 | if (geo.far_copies > 1 && !geo.far_offset) |
| 3974 | /* Cannot switch to 'far' mode */ |
| 3975 | return -EINVAL; |
| 3976 | |
| 3977 | if (mddev->array_sectors & geo.chunk_mask) |
| 3978 | /* not factor of array size */ |
| 3979 | return -EINVAL; |
| 3980 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3981 | if (!enough(conf, -1)) |
| 3982 | return -EINVAL; |
| 3983 | |
| 3984 | kfree(conf->mirrors_new); |
| 3985 | conf->mirrors_new = NULL; |
| 3986 | if (mddev->delta_disks > 0) { |
| 3987 | /* allocate new 'mirrors' list */ |
| 3988 | conf->mirrors_new = kzalloc( |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 3989 | sizeof(struct raid10_info) |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 3990 | *(mddev->raid_disks + |
| 3991 | mddev->delta_disks), |
| 3992 | GFP_KERNEL); |
| 3993 | if (!conf->mirrors_new) |
| 3994 | return -ENOMEM; |
| 3995 | } |
| 3996 | return 0; |
| 3997 | } |
| 3998 | |
| 3999 | /* |
| 4000 | * Need to check if array has failed when deciding whether to: |
| 4001 | * - start an array |
| 4002 | * - remove non-faulty devices |
| 4003 | * - add a spare |
| 4004 | * - allow a reshape |
| 4005 | * This determination is simple when no reshape is happening. |
| 4006 | * However if there is a reshape, we need to carefully check |
| 4007 | * both the before and after sections. |
| 4008 | * This is because some failed devices may only affect one |
| 4009 | * of the two sections, and some non-in_sync devices may |
| 4010 | * be insync in the section most affected by failed devices. |
| 4011 | */ |
| 4012 | static int calc_degraded(struct r10conf *conf) |
| 4013 | { |
| 4014 | int degraded, degraded2; |
| 4015 | int i; |
| 4016 | |
| 4017 | rcu_read_lock(); |
| 4018 | degraded = 0; |
| 4019 | /* 'prev' section first */ |
| 4020 | for (i = 0; i < conf->prev.raid_disks; i++) { |
| 4021 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 4022 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 4023 | degraded++; |
| 4024 | else if (!test_bit(In_sync, &rdev->flags)) |
| 4025 | /* When we can reduce the number of devices in |
| 4026 | * an array, this might not contribute to |
| 4027 | * 'degraded'. It does now. |
| 4028 | */ |
| 4029 | degraded++; |
| 4030 | } |
| 4031 | rcu_read_unlock(); |
| 4032 | if (conf->geo.raid_disks == conf->prev.raid_disks) |
| 4033 | return degraded; |
| 4034 | rcu_read_lock(); |
| 4035 | degraded2 = 0; |
| 4036 | for (i = 0; i < conf->geo.raid_disks; i++) { |
| 4037 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 4038 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 4039 | degraded2++; |
| 4040 | else if (!test_bit(In_sync, &rdev->flags)) { |
| 4041 | /* If reshape is increasing the number of devices, |
| 4042 | * this section has already been recovered, so |
| 4043 | * it doesn't contribute to degraded. |
| 4044 | * else it does. |
| 4045 | */ |
| 4046 | if (conf->geo.raid_disks <= conf->prev.raid_disks) |
| 4047 | degraded2++; |
| 4048 | } |
| 4049 | } |
| 4050 | rcu_read_unlock(); |
| 4051 | if (degraded2 > degraded) |
| 4052 | return degraded2; |
| 4053 | return degraded; |
| 4054 | } |
| 4055 | |
| 4056 | static int raid10_start_reshape(struct mddev *mddev) |
| 4057 | { |
| 4058 | /* A 'reshape' has been requested. This commits |
| 4059 | * the various 'new' fields and sets MD_RECOVER_RESHAPE |
| 4060 | * This also checks if there are enough spares and adds them |
| 4061 | * to the array. |
| 4062 | * We currently require enough spares to make the final |
| 4063 | * array non-degraded. We also require that the difference |
| 4064 | * between old and new data_offset - on each device - is |
| 4065 | * enough that we never risk over-writing. |
| 4066 | */ |
| 4067 | |
| 4068 | unsigned long before_length, after_length; |
| 4069 | sector_t min_offset_diff = 0; |
| 4070 | int first = 1; |
| 4071 | struct geom new; |
| 4072 | struct r10conf *conf = mddev->private; |
| 4073 | struct md_rdev *rdev; |
| 4074 | int spares = 0; |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4075 | int ret; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4076 | |
| 4077 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
| 4078 | return -EBUSY; |
| 4079 | |
| 4080 | if (setup_geo(&new, mddev, geo_start) != conf->copies) |
| 4081 | return -EINVAL; |
| 4082 | |
| 4083 | before_length = ((1 << conf->prev.chunk_shift) * |
| 4084 | conf->prev.far_copies); |
| 4085 | after_length = ((1 << conf->geo.chunk_shift) * |
| 4086 | conf->geo.far_copies); |
| 4087 | |
| 4088 | rdev_for_each(rdev, mddev) { |
| 4089 | if (!test_bit(In_sync, &rdev->flags) |
| 4090 | && !test_bit(Faulty, &rdev->flags)) |
| 4091 | spares++; |
| 4092 | if (rdev->raid_disk >= 0) { |
| 4093 | long long diff = (rdev->new_data_offset |
| 4094 | - rdev->data_offset); |
| 4095 | if (!mddev->reshape_backwards) |
| 4096 | diff = -diff; |
| 4097 | if (diff < 0) |
| 4098 | diff = 0; |
| 4099 | if (first || diff < min_offset_diff) |
| 4100 | min_offset_diff = diff; |
Shaohua Li | b506335 | 2017-05-01 12:15:07 -0700 | [diff] [blame] | 4101 | first = 0; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4102 | } |
| 4103 | } |
| 4104 | |
| 4105 | if (max(before_length, after_length) > min_offset_diff) |
| 4106 | return -EINVAL; |
| 4107 | |
| 4108 | if (spares < mddev->delta_disks) |
| 4109 | return -EINVAL; |
| 4110 | |
| 4111 | conf->offset_diff = min_offset_diff; |
| 4112 | spin_lock_irq(&conf->device_lock); |
| 4113 | if (conf->mirrors_new) { |
| 4114 | memcpy(conf->mirrors_new, conf->mirrors, |
Jonathan Brassow | dc280d98 | 2012-07-31 10:03:52 +1000 | [diff] [blame] | 4115 | sizeof(struct raid10_info)*conf->prev.raid_disks); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4116 | smp_mb(); |
NeilBrown | c4796e2 | 2014-08-23 20:19:26 +1000 | [diff] [blame] | 4117 | kfree(conf->mirrors_old); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4118 | conf->mirrors_old = conf->mirrors; |
| 4119 | conf->mirrors = conf->mirrors_new; |
| 4120 | conf->mirrors_new = NULL; |
| 4121 | } |
| 4122 | setup_geo(&conf->geo, mddev, geo_start); |
| 4123 | smp_mb(); |
| 4124 | if (mddev->reshape_backwards) { |
| 4125 | sector_t size = raid10_size(mddev, 0, 0); |
| 4126 | if (size < mddev->array_sectors) { |
| 4127 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 08464e0 | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4128 | pr_warn("md/raid10:%s: array size must be reduce before number of disks\n", |
| 4129 | mdname(mddev)); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4130 | return -EINVAL; |
| 4131 | } |
| 4132 | mddev->resync_max_sectors = size; |
| 4133 | conf->reshape_progress = size; |
| 4134 | } else |
| 4135 | conf->reshape_progress = 0; |
NeilBrown | 299b068 | 2015-07-06 17:37:49 +1000 | [diff] [blame] | 4136 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4137 | spin_unlock_irq(&conf->device_lock); |
| 4138 | |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4139 | if (mddev->delta_disks && mddev->bitmap) { |
| 4140 | ret = bitmap_resize(mddev->bitmap, |
| 4141 | raid10_size(mddev, 0, |
| 4142 | conf->geo.raid_disks), |
| 4143 | 0, 0); |
| 4144 | if (ret) |
| 4145 | goto abort; |
| 4146 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4147 | if (mddev->delta_disks > 0) { |
| 4148 | rdev_for_each(rdev, mddev) |
| 4149 | if (rdev->raid_disk < 0 && |
| 4150 | !test_bit(Faulty, &rdev->flags)) { |
| 4151 | if (raid10_add_disk(mddev, rdev) == 0) { |
| 4152 | if (rdev->raid_disk >= |
| 4153 | conf->prev.raid_disks) |
| 4154 | set_bit(In_sync, &rdev->flags); |
| 4155 | else |
| 4156 | rdev->recovery_offset = 0; |
| 4157 | |
| 4158 | if (sysfs_link_rdev(mddev, rdev)) |
| 4159 | /* Failure here is OK */; |
| 4160 | } |
| 4161 | } else if (rdev->raid_disk >= conf->prev.raid_disks |
| 4162 | && !test_bit(Faulty, &rdev->flags)) { |
| 4163 | /* This is a spare that was manually added */ |
| 4164 | set_bit(In_sync, &rdev->flags); |
| 4165 | } |
| 4166 | } |
| 4167 | /* When a reshape changes the number of devices, |
| 4168 | * ->degraded is measured against the larger of the |
| 4169 | * pre and post numbers. |
| 4170 | */ |
| 4171 | spin_lock_irq(&conf->device_lock); |
| 4172 | mddev->degraded = calc_degraded(conf); |
| 4173 | spin_unlock_irq(&conf->device_lock); |
| 4174 | mddev->raid_disks = conf->geo.raid_disks; |
| 4175 | mddev->reshape_position = conf->reshape_progress; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4176 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4177 | |
| 4178 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 4179 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 4180 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4181 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 4182 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 4183 | |
| 4184 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
| 4185 | "reshape"); |
| 4186 | if (!mddev->sync_thread) { |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4187 | ret = -EAGAIN; |
| 4188 | goto abort; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4189 | } |
| 4190 | conf->reshape_checkpoint = jiffies; |
| 4191 | md_wakeup_thread(mddev->sync_thread); |
| 4192 | md_new_event(mddev); |
| 4193 | return 0; |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4194 | |
| 4195 | abort: |
| 4196 | mddev->recovery = 0; |
| 4197 | spin_lock_irq(&conf->device_lock); |
| 4198 | conf->geo = conf->prev; |
| 4199 | mddev->raid_disks = conf->geo.raid_disks; |
| 4200 | rdev_for_each(rdev, mddev) |
| 4201 | rdev->new_data_offset = rdev->data_offset; |
| 4202 | smp_wmb(); |
| 4203 | conf->reshape_progress = MaxSector; |
NeilBrown | 299b068 | 2015-07-06 17:37:49 +1000 | [diff] [blame] | 4204 | conf->reshape_safe = MaxSector; |
NeilBrown | bb63a70 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 4205 | mddev->reshape_position = MaxSector; |
| 4206 | spin_unlock_irq(&conf->device_lock); |
| 4207 | return ret; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | /* Calculate the last device-address that could contain |
| 4211 | * any block from the chunk that includes the array-address 's' |
| 4212 | * and report the next address. |
| 4213 | * i.e. the address returned will be chunk-aligned and after |
| 4214 | * any data that is in the chunk containing 's'. |
| 4215 | */ |
| 4216 | static sector_t last_dev_address(sector_t s, struct geom *geo) |
| 4217 | { |
| 4218 | s = (s | geo->chunk_mask) + 1; |
| 4219 | s >>= geo->chunk_shift; |
| 4220 | s *= geo->near_copies; |
| 4221 | s = DIV_ROUND_UP_SECTOR_T(s, geo->raid_disks); |
| 4222 | s *= geo->far_copies; |
| 4223 | s <<= geo->chunk_shift; |
| 4224 | return s; |
| 4225 | } |
| 4226 | |
| 4227 | /* Calculate the first device-address that could contain |
| 4228 | * any block from the chunk that includes the array-address 's'. |
| 4229 | * This too will be the start of a chunk |
| 4230 | */ |
| 4231 | static sector_t first_dev_address(sector_t s, struct geom *geo) |
| 4232 | { |
| 4233 | s >>= geo->chunk_shift; |
| 4234 | s *= geo->near_copies; |
| 4235 | sector_div(s, geo->raid_disks); |
| 4236 | s *= geo->far_copies; |
| 4237 | s <<= geo->chunk_shift; |
| 4238 | return s; |
| 4239 | } |
| 4240 | |
| 4241 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, |
| 4242 | int *skipped) |
| 4243 | { |
| 4244 | /* We simply copy at most one chunk (smallest of old and new) |
| 4245 | * at a time, possibly less if that exceeds RESYNC_PAGES, |
| 4246 | * or we hit a bad block or something. |
| 4247 | * This might mean we pause for normal IO in the middle of |
NeilBrown | 02ec502 | 2015-07-06 16:33:47 +1000 | [diff] [blame] | 4248 | * a chunk, but that is not a problem as mddev->reshape_position |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4249 | * can record any location. |
| 4250 | * |
| 4251 | * If we will want to write to a location that isn't |
| 4252 | * yet recorded as 'safe' (i.e. in metadata on disk) then |
| 4253 | * we need to flush all reshape requests and update the metadata. |
| 4254 | * |
| 4255 | * When reshaping forwards (e.g. to more devices), we interpret |
| 4256 | * 'safe' as the earliest block which might not have been copied |
| 4257 | * down yet. We divide this by previous stripe size and multiply |
| 4258 | * by previous stripe length to get lowest device offset that we |
| 4259 | * cannot write to yet. |
| 4260 | * We interpret 'sector_nr' as an address that we want to write to. |
| 4261 | * From this we use last_device_address() to find where we might |
| 4262 | * write to, and first_device_address on the 'safe' position. |
| 4263 | * If this 'next' write position is after the 'safe' position, |
| 4264 | * we must update the metadata to increase the 'safe' position. |
| 4265 | * |
| 4266 | * When reshaping backwards, we round in the opposite direction |
| 4267 | * and perform the reverse test: next write position must not be |
| 4268 | * less than current safe position. |
| 4269 | * |
| 4270 | * In all this the minimum difference in data offsets |
| 4271 | * (conf->offset_diff - always positive) allows a bit of slack, |
NeilBrown | 02ec502 | 2015-07-06 16:33:47 +1000 | [diff] [blame] | 4272 | * so next can be after 'safe', but not by more than offset_diff |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4273 | * |
| 4274 | * We need to prepare all the bios here before we start any IO |
| 4275 | * to ensure the size we choose is acceptable to all devices. |
| 4276 | * The means one for each copy for write-out and an extra one for |
| 4277 | * read-in. |
| 4278 | * We store the read-in bio in ->master_bio and the others in |
| 4279 | * ->devs[x].bio and ->devs[x].repl_bio. |
| 4280 | */ |
| 4281 | struct r10conf *conf = mddev->private; |
| 4282 | struct r10bio *r10_bio; |
| 4283 | sector_t next, safe, last; |
| 4284 | int max_sectors; |
| 4285 | int nr_sectors; |
| 4286 | int s; |
| 4287 | struct md_rdev *rdev; |
| 4288 | int need_flush = 0; |
| 4289 | struct bio *blist; |
| 4290 | struct bio *bio, *read_bio; |
| 4291 | int sectors_done = 0; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 4292 | struct page **pages; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4293 | |
| 4294 | if (sector_nr == 0) { |
| 4295 | /* If restarting in the middle, skip the initial sectors */ |
| 4296 | if (mddev->reshape_backwards && |
| 4297 | conf->reshape_progress < raid10_size(mddev, 0, 0)) { |
| 4298 | sector_nr = (raid10_size(mddev, 0, 0) |
| 4299 | - conf->reshape_progress); |
| 4300 | } else if (!mddev->reshape_backwards && |
| 4301 | conf->reshape_progress > 0) |
| 4302 | sector_nr = conf->reshape_progress; |
| 4303 | if (sector_nr) { |
| 4304 | mddev->curr_resync_completed = sector_nr; |
| 4305 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
| 4306 | *skipped = 1; |
| 4307 | return sector_nr; |
| 4308 | } |
| 4309 | } |
| 4310 | |
| 4311 | /* We don't use sector_nr to track where we are up to |
| 4312 | * as that doesn't work well for ->reshape_backwards. |
| 4313 | * So just use ->reshape_progress. |
| 4314 | */ |
| 4315 | if (mddev->reshape_backwards) { |
| 4316 | /* 'next' is the earliest device address that we might |
| 4317 | * write to for this chunk in the new layout |
| 4318 | */ |
| 4319 | next = first_dev_address(conf->reshape_progress - 1, |
| 4320 | &conf->geo); |
| 4321 | |
| 4322 | /* 'safe' is the last device address that we might read from |
| 4323 | * in the old layout after a restart |
| 4324 | */ |
| 4325 | safe = last_dev_address(conf->reshape_safe - 1, |
| 4326 | &conf->prev); |
| 4327 | |
| 4328 | if (next + conf->offset_diff < safe) |
| 4329 | need_flush = 1; |
| 4330 | |
| 4331 | last = conf->reshape_progress - 1; |
| 4332 | sector_nr = last & ~(sector_t)(conf->geo.chunk_mask |
| 4333 | & conf->prev.chunk_mask); |
| 4334 | if (sector_nr + RESYNC_BLOCK_SIZE/512 < last) |
| 4335 | sector_nr = last + 1 - RESYNC_BLOCK_SIZE/512; |
| 4336 | } else { |
| 4337 | /* 'next' is after the last device address that we |
| 4338 | * might write to for this chunk in the new layout |
| 4339 | */ |
| 4340 | next = last_dev_address(conf->reshape_progress, &conf->geo); |
| 4341 | |
| 4342 | /* 'safe' is the earliest device address that we might |
| 4343 | * read from in the old layout after a restart |
| 4344 | */ |
| 4345 | safe = first_dev_address(conf->reshape_safe, &conf->prev); |
| 4346 | |
| 4347 | /* Need to update metadata if 'next' might be beyond 'safe' |
| 4348 | * as that would possibly corrupt data |
| 4349 | */ |
| 4350 | if (next > safe + conf->offset_diff) |
| 4351 | need_flush = 1; |
| 4352 | |
| 4353 | sector_nr = conf->reshape_progress; |
| 4354 | last = sector_nr | (conf->geo.chunk_mask |
| 4355 | & conf->prev.chunk_mask); |
| 4356 | |
| 4357 | if (sector_nr + RESYNC_BLOCK_SIZE/512 <= last) |
| 4358 | last = sector_nr + RESYNC_BLOCK_SIZE/512 - 1; |
| 4359 | } |
| 4360 | |
| 4361 | if (need_flush || |
| 4362 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
| 4363 | /* Need to update reshape_position in metadata */ |
| 4364 | wait_barrier(conf); |
| 4365 | mddev->reshape_position = conf->reshape_progress; |
| 4366 | if (mddev->reshape_backwards) |
| 4367 | mddev->curr_resync_completed = raid10_size(mddev, 0, 0) |
| 4368 | - conf->reshape_progress; |
| 4369 | else |
| 4370 | mddev->curr_resync_completed = conf->reshape_progress; |
| 4371 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4372 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4373 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4374 | wait_event(mddev->sb_wait, mddev->sb_flags == 0 || |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 4375 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 4376 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 4377 | allow_barrier(conf); |
| 4378 | return sectors_done; |
| 4379 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4380 | conf->reshape_safe = mddev->reshape_position; |
| 4381 | allow_barrier(conf); |
| 4382 | } |
| 4383 | |
| 4384 | read_more: |
| 4385 | /* Now schedule reads for blocks from sector_nr to last */ |
Shaohua Li | 208410b | 2017-08-24 17:50:40 -0700 | [diff] [blame] | 4386 | r10_bio = raid10_alloc_init_r10buf(conf); |
NeilBrown | cb8b12b | 2014-08-18 14:38:45 +1000 | [diff] [blame] | 4387 | r10_bio->state = 0; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4388 | raise_barrier(conf, sectors_done != 0); |
| 4389 | atomic_set(&r10_bio->remaining, 0); |
| 4390 | r10_bio->mddev = mddev; |
| 4391 | r10_bio->sector = sector_nr; |
| 4392 | set_bit(R10BIO_IsReshape, &r10_bio->state); |
| 4393 | r10_bio->sectors = last - sector_nr + 1; |
| 4394 | rdev = read_balance(conf, r10_bio, &max_sectors); |
| 4395 | BUG_ON(!test_bit(R10BIO_Previous, &r10_bio->state)); |
| 4396 | |
| 4397 | if (!rdev) { |
| 4398 | /* Cannot read from here, so need to record bad blocks |
| 4399 | * on all the target devices. |
| 4400 | */ |
| 4401 | // FIXME |
NeilBrown | e337aea | 2014-08-18 14:48:54 +1000 | [diff] [blame] | 4402 | mempool_free(r10_bio, conf->r10buf_pool); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4403 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4404 | return sectors_done; |
| 4405 | } |
| 4406 | |
| 4407 | read_bio = bio_alloc_mddev(GFP_KERNEL, RESYNC_PAGES, mddev); |
| 4408 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 4409 | bio_set_dev(read_bio, rdev->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4410 | 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] | 4411 | + rdev->data_offset); |
| 4412 | read_bio->bi_private = r10_bio; |
Ming Lei | 81fa152 | 2017-03-17 00:12:32 +0800 | [diff] [blame] | 4413 | read_bio->bi_end_io = end_reshape_read; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 4414 | bio_set_op_attrs(read_bio, REQ_OP_READ, 0); |
NeilBrown | ce0b0a4 | 2014-08-18 13:56:38 +1000 | [diff] [blame] | 4415 | read_bio->bi_flags &= (~0UL << BIO_RESET_BITS); |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 4416 | read_bio->bi_status = 0; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4417 | read_bio->bi_vcnt = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4418 | read_bio->bi_iter.bi_size = 0; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4419 | r10_bio->master_bio = read_bio; |
| 4420 | r10_bio->read_slot = r10_bio->devs[r10_bio->read_slot].devnum; |
| 4421 | |
| 4422 | /* Now find the locations in the new layout */ |
| 4423 | __raid10_find_phys(&conf->geo, r10_bio); |
| 4424 | |
| 4425 | blist = read_bio; |
| 4426 | read_bio->bi_next = NULL; |
| 4427 | |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4428 | rcu_read_lock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4429 | for (s = 0; s < conf->copies*2; s++) { |
| 4430 | struct bio *b; |
| 4431 | int d = r10_bio->devs[s/2].devnum; |
| 4432 | struct md_rdev *rdev2; |
| 4433 | if (s&1) { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4434 | rdev2 = rcu_dereference(conf->mirrors[d].replacement); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4435 | b = r10_bio->devs[s/2].repl_bio; |
| 4436 | } else { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4437 | rdev2 = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4438 | b = r10_bio->devs[s/2].bio; |
| 4439 | } |
| 4440 | if (!rdev2 || test_bit(Faulty, &rdev2->flags)) |
| 4441 | continue; |
Kent Overstreet | 8be185f | 2012-09-06 14:14:43 -0700 | [diff] [blame] | 4442 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 4443 | bio_set_dev(b, rdev2->bdev); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4444 | b->bi_iter.bi_sector = r10_bio->devs[s/2].addr + |
| 4445 | rdev2->new_data_offset; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4446 | b->bi_end_io = end_reshape_write; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 4447 | bio_set_op_attrs(b, REQ_OP_WRITE, 0); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4448 | b->bi_next = blist; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4449 | blist = b; |
| 4450 | } |
| 4451 | |
| 4452 | /* Now add as many pages as possible to all of these bios. */ |
| 4453 | |
| 4454 | nr_sectors = 0; |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 4455 | pages = get_resync_pages(r10_bio->devs[0].bio)->pages; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4456 | for (s = 0 ; s < max_sectors; s += PAGE_SIZE >> 9) { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 4457 | struct page *page = pages[s / (PAGE_SIZE >> 9)]; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4458 | int len = (max_sectors - s) << 9; |
| 4459 | if (len > PAGE_SIZE) |
| 4460 | len = PAGE_SIZE; |
| 4461 | for (bio = blist; bio ; bio = bio->bi_next) { |
Ming Lei | c85ba14 | 2017-03-17 00:12:22 +0800 | [diff] [blame] | 4462 | /* |
| 4463 | * won't fail because the vec table is big enough |
| 4464 | * to hold all these pages |
| 4465 | */ |
| 4466 | bio_add_page(bio, page, len, 0); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4467 | } |
| 4468 | sector_nr += len >> 9; |
| 4469 | nr_sectors += len >> 9; |
| 4470 | } |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4471 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4472 | r10_bio->sectors = nr_sectors; |
| 4473 | |
| 4474 | /* Now submit the read */ |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 4475 | md_sync_acct_bio(read_bio, r10_bio->sectors); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4476 | atomic_inc(&r10_bio->remaining); |
| 4477 | read_bio->bi_next = NULL; |
| 4478 | generic_make_request(read_bio); |
| 4479 | sector_nr += nr_sectors; |
| 4480 | sectors_done += nr_sectors; |
| 4481 | if (sector_nr <= last) |
| 4482 | goto read_more; |
| 4483 | |
| 4484 | /* Now that we have done the whole section we can |
| 4485 | * update reshape_progress |
| 4486 | */ |
| 4487 | if (mddev->reshape_backwards) |
| 4488 | conf->reshape_progress -= sectors_done; |
| 4489 | else |
| 4490 | conf->reshape_progress += sectors_done; |
| 4491 | |
| 4492 | return sectors_done; |
| 4493 | } |
| 4494 | |
| 4495 | static void end_reshape_request(struct r10bio *r10_bio); |
| 4496 | static int handle_reshape_read_error(struct mddev *mddev, |
| 4497 | struct r10bio *r10_bio); |
| 4498 | static void reshape_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
| 4499 | { |
| 4500 | /* Reshape read completed. Hopefully we have a block |
| 4501 | * to write out. |
| 4502 | * If we got a read error then we do sync 1-page reads from |
| 4503 | * elsewhere until we find the data - or give up. |
| 4504 | */ |
| 4505 | struct r10conf *conf = mddev->private; |
| 4506 | int s; |
| 4507 | |
| 4508 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) |
| 4509 | if (handle_reshape_read_error(mddev, r10_bio) < 0) { |
| 4510 | /* Reshape has been aborted */ |
| 4511 | md_done_sync(mddev, r10_bio->sectors, 0); |
| 4512 | return; |
| 4513 | } |
| 4514 | |
| 4515 | /* We definitely have the data in the pages, schedule the |
| 4516 | * writes. |
| 4517 | */ |
| 4518 | atomic_set(&r10_bio->remaining, 1); |
| 4519 | for (s = 0; s < conf->copies*2; s++) { |
| 4520 | struct bio *b; |
| 4521 | int d = r10_bio->devs[s/2].devnum; |
| 4522 | struct md_rdev *rdev; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4523 | rcu_read_lock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4524 | if (s&1) { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4525 | rdev = rcu_dereference(conf->mirrors[d].replacement); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4526 | b = r10_bio->devs[s/2].repl_bio; |
| 4527 | } else { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4528 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4529 | b = r10_bio->devs[s/2].bio; |
| 4530 | } |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4531 | if (!rdev || test_bit(Faulty, &rdev->flags)) { |
| 4532 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4533 | continue; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4534 | } |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4535 | atomic_inc(&rdev->nr_pending); |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4536 | rcu_read_unlock(); |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 4537 | md_sync_acct_bio(b, r10_bio->sectors); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4538 | atomic_inc(&r10_bio->remaining); |
| 4539 | b->bi_next = NULL; |
| 4540 | generic_make_request(b); |
| 4541 | } |
| 4542 | end_reshape_request(r10_bio); |
| 4543 | } |
| 4544 | |
| 4545 | static void end_reshape(struct r10conf *conf) |
| 4546 | { |
| 4547 | if (test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) |
| 4548 | return; |
| 4549 | |
| 4550 | spin_lock_irq(&conf->device_lock); |
| 4551 | conf->prev = conf->geo; |
| 4552 | md_finish_reshape(conf->mddev); |
| 4553 | smp_wmb(); |
| 4554 | conf->reshape_progress = MaxSector; |
NeilBrown | 299b068 | 2015-07-06 17:37:49 +1000 | [diff] [blame] | 4555 | conf->reshape_safe = MaxSector; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4556 | spin_unlock_irq(&conf->device_lock); |
| 4557 | |
| 4558 | /* read-ahead size must cover two whole stripes, which is |
| 4559 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 4560 | */ |
| 4561 | if (conf->mddev->queue) { |
| 4562 | int stripe = conf->geo.raid_disks * |
| 4563 | ((conf->mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 4564 | stripe /= conf->geo.near_copies; |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 4565 | if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
| 4566 | conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4567 | } |
| 4568 | conf->fullsync = 0; |
| 4569 | } |
| 4570 | |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4571 | static int handle_reshape_read_error(struct mddev *mddev, |
| 4572 | struct r10bio *r10_bio) |
| 4573 | { |
| 4574 | /* Use sync reads to get the blocks from somewhere else */ |
| 4575 | int sectors = r10_bio->sectors; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4576 | struct r10conf *conf = mddev->private; |
Matthias Kaehlcke | 584ed9fa | 2017-10-05 11:28:47 -0700 | [diff] [blame] | 4577 | struct r10bio *r10b; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4578 | int slot = 0; |
| 4579 | int idx = 0; |
Ming Lei | 2d06e3b | 2017-03-17 00:12:35 +0800 | [diff] [blame] | 4580 | struct page **pages; |
| 4581 | |
Matthias Kaehlcke | 584ed9fa | 2017-10-05 11:28:47 -0700 | [diff] [blame] | 4582 | r10b = kmalloc(sizeof(*r10b) + |
| 4583 | sizeof(struct r10dev) * conf->copies, GFP_NOIO); |
| 4584 | if (!r10b) { |
| 4585 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 4586 | return -ENOMEM; |
| 4587 | } |
| 4588 | |
Ming Lei | 2d06e3b | 2017-03-17 00:12:35 +0800 | [diff] [blame] | 4589 | /* reshape IOs share pages from .devs[0].bio */ |
| 4590 | pages = get_resync_pages(r10_bio->devs[0].bio)->pages; |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4591 | |
NeilBrown | e0ee778 | 2012-08-18 09:51:42 +1000 | [diff] [blame] | 4592 | r10b->sector = r10_bio->sector; |
| 4593 | __raid10_find_phys(&conf->prev, r10b); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4594 | |
| 4595 | while (sectors) { |
| 4596 | int s = sectors; |
| 4597 | int success = 0; |
| 4598 | int first_slot = slot; |
| 4599 | |
| 4600 | if (s > (PAGE_SIZE >> 9)) |
| 4601 | s = PAGE_SIZE >> 9; |
| 4602 | |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4603 | rcu_read_lock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4604 | while (!success) { |
NeilBrown | e0ee778 | 2012-08-18 09:51:42 +1000 | [diff] [blame] | 4605 | int d = r10b->devs[slot].devnum; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4606 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4607 | sector_t addr; |
| 4608 | if (rdev == NULL || |
| 4609 | test_bit(Faulty, &rdev->flags) || |
| 4610 | !test_bit(In_sync, &rdev->flags)) |
| 4611 | goto failed; |
| 4612 | |
NeilBrown | e0ee778 | 2012-08-18 09:51:42 +1000 | [diff] [blame] | 4613 | addr = r10b->devs[slot].addr + idx * PAGE_SIZE; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4614 | atomic_inc(&rdev->nr_pending); |
| 4615 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4616 | success = sync_page_io(rdev, |
| 4617 | addr, |
| 4618 | s << 9, |
Ming Lei | 2d06e3b | 2017-03-17 00:12:35 +0800 | [diff] [blame] | 4619 | pages[idx], |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 4620 | REQ_OP_READ, 0, false); |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4621 | rdev_dec_pending(rdev, mddev); |
| 4622 | rcu_read_lock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4623 | if (success) |
| 4624 | break; |
| 4625 | failed: |
| 4626 | slot++; |
| 4627 | if (slot >= conf->copies) |
| 4628 | slot = 0; |
| 4629 | if (slot == first_slot) |
| 4630 | break; |
| 4631 | } |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4632 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4633 | if (!success) { |
| 4634 | /* couldn't read this block, must give up */ |
| 4635 | set_bit(MD_RECOVERY_INTR, |
| 4636 | &mddev->recovery); |
Matthias Kaehlcke | 584ed9fa | 2017-10-05 11:28:47 -0700 | [diff] [blame] | 4637 | kfree(r10b); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4638 | return -EIO; |
| 4639 | } |
| 4640 | sectors -= s; |
| 4641 | idx++; |
| 4642 | } |
Matthias Kaehlcke | 584ed9fa | 2017-10-05 11:28:47 -0700 | [diff] [blame] | 4643 | kfree(r10b); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4644 | return 0; |
| 4645 | } |
| 4646 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 4647 | static void end_reshape_write(struct bio *bio) |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4648 | { |
Ming Lei | f025061 | 2017-03-17 00:12:33 +0800 | [diff] [blame] | 4649 | struct r10bio *r10_bio = get_resync_r10bio(bio); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4650 | struct mddev *mddev = r10_bio->mddev; |
| 4651 | struct r10conf *conf = mddev->private; |
| 4652 | int d; |
| 4653 | int slot; |
| 4654 | int repl; |
| 4655 | struct md_rdev *rdev = NULL; |
| 4656 | |
| 4657 | d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
| 4658 | if (repl) |
| 4659 | rdev = conf->mirrors[d].replacement; |
| 4660 | if (!rdev) { |
| 4661 | smp_mb(); |
| 4662 | rdev = conf->mirrors[d].rdev; |
| 4663 | } |
| 4664 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 4665 | if (bio->bi_status) { |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4666 | /* FIXME should record badblock */ |
| 4667 | md_error(mddev, rdev); |
| 4668 | } |
| 4669 | |
| 4670 | rdev_dec_pending(rdev, mddev); |
| 4671 | end_reshape_request(r10_bio); |
| 4672 | } |
| 4673 | |
| 4674 | static void end_reshape_request(struct r10bio *r10_bio) |
| 4675 | { |
| 4676 | if (!atomic_dec_and_test(&r10_bio->remaining)) |
| 4677 | return; |
| 4678 | md_done_sync(r10_bio->mddev, r10_bio->sectors, 1); |
| 4679 | bio_put(r10_bio->master_bio); |
| 4680 | put_buf(r10_bio); |
| 4681 | } |
| 4682 | |
| 4683 | static void raid10_finish_reshape(struct mddev *mddev) |
| 4684 | { |
| 4685 | struct r10conf *conf = mddev->private; |
| 4686 | |
| 4687 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 4688 | return; |
| 4689 | |
| 4690 | if (mddev->delta_disks > 0) { |
| 4691 | sector_t size = raid10_size(mddev, 0, 0); |
| 4692 | md_set_array_sectors(mddev, size); |
| 4693 | if (mddev->recovery_cp > mddev->resync_max_sectors) { |
| 4694 | mddev->recovery_cp = mddev->resync_max_sectors; |
| 4695 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 4696 | } |
| 4697 | mddev->resync_max_sectors = size; |
Heinz Mauelshagen | 859644f | 2016-05-03 19:43:24 +0200 | [diff] [blame] | 4698 | if (mddev->queue) { |
| 4699 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 4700 | revalidate_disk(mddev->gendisk); |
| 4701 | } |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 4702 | } else { |
| 4703 | int d; |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4704 | rcu_read_lock(); |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 4705 | for (d = conf->geo.raid_disks ; |
| 4706 | d < conf->geo.raid_disks - mddev->delta_disks; |
| 4707 | d++) { |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4708 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 4709 | if (rdev) |
| 4710 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4711 | rdev = rcu_dereference(conf->mirrors[d].replacement); |
NeilBrown | 63aced6 | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 4712 | if (rdev) |
| 4713 | clear_bit(In_sync, &rdev->flags); |
| 4714 | } |
NeilBrown | d094d68 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 4715 | rcu_read_unlock(); |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4716 | } |
| 4717 | mddev->layout = mddev->new_layout; |
| 4718 | mddev->chunk_sectors = 1 << conf->geo.chunk_shift; |
| 4719 | mddev->reshape_position = MaxSector; |
| 4720 | mddev->delta_disks = 0; |
| 4721 | mddev->reshape_backwards = 0; |
| 4722 | } |
| 4723 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 4724 | static struct md_personality raid10_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4725 | { |
| 4726 | .name = "raid10", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4727 | .level = 10, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4728 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 4729 | .make_request = raid10_make_request, |
| 4730 | .run = raid10_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 4731 | .free = raid10_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 4732 | .status = raid10_status, |
| 4733 | .error_handler = raid10_error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4734 | .hot_add_disk = raid10_add_disk, |
| 4735 | .hot_remove_disk= raid10_remove_disk, |
| 4736 | .spare_active = raid10_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 4737 | .sync_request = raid10_sync_request, |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 4738 | .quiesce = raid10_quiesce, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4739 | .size = raid10_size, |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame] | 4740 | .resize = raid10_resize, |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 4741 | .takeover = raid10_takeover, |
NeilBrown | 3ea7daa | 2012-05-22 13:53:47 +1000 | [diff] [blame] | 4742 | .check_reshape = raid10_check_reshape, |
| 4743 | .start_reshape = raid10_start_reshape, |
| 4744 | .finish_reshape = raid10_finish_reshape, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4745 | .congested = raid10_congested, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | }; |
| 4747 | |
| 4748 | static int __init raid_init(void) |
| 4749 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4750 | return register_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | } |
| 4752 | |
| 4753 | static void raid_exit(void) |
| 4754 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4755 | unregister_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | module_init(raid_init); |
| 4759 | module_exit(raid_exit); |
| 4760 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 4761 | MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4762 | MODULE_ALIAS("md-personality-9"); /* RAID10 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 4763 | MODULE_ALIAS("md-raid10"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 4764 | MODULE_ALIAS("md-level-10"); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 4765 | |
| 4766 | module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); |