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