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