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