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 | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 27 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 28 | #include "raid10.h" |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 29 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 30 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | /* |
| 33 | * RAID10 provides a combination of RAID0 and RAID1 functionality. |
| 34 | * The layout of data is defined by |
| 35 | * chunk_size |
| 36 | * raid_disks |
| 37 | * near_copies (stored in low byte of layout) |
| 38 | * far_copies (stored in second byte of layout) |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 39 | * far_offset (stored in bit 16 of layout ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | * |
| 41 | * The data to be stored is divided into chunks using chunksize. |
| 42 | * Each device is divided into far_copies sections. |
| 43 | * In each section, chunks are laid out in a style similar to raid0, but |
| 44 | * near_copies copies of each chunk is stored (each on a different drive). |
| 45 | * The starting device for each section is offset near_copies from the starting |
| 46 | * device of the previous section. |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 47 | * 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] | 48 | * drive. |
| 49 | * near_copies and far_copies must be at least one, and their product is at most |
| 50 | * raid_disks. |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 51 | * |
| 52 | * If far_offset is true, then the far_copies are handled a bit differently. |
| 53 | * The copies are still in different stripes, but instead of be very far apart |
| 54 | * on disk, there are adjacent stripes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 55 | */ |
| 56 | |
| 57 | /* |
| 58 | * Number of guaranteed r10bios in case of extreme VM load: |
| 59 | */ |
| 60 | #define NR_RAID10_BIOS 256 |
| 61 | |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 62 | /* When there are this many requests queue to be written by |
| 63 | * the raid10 thread, we become 'congested' to provide back-pressure |
| 64 | * for writeback. |
| 65 | */ |
| 66 | static int max_queued_requests = 1024; |
| 67 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 68 | static void allow_barrier(struct r10conf *conf); |
| 69 | static void lower_barrier(struct r10conf *conf); |
NeilBrown | fae8cc5 | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 70 | static int enough(struct r10conf *conf, int ignore); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 71 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 72 | static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 74 | struct r10conf *conf = data; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 75 | int size = offsetof(struct r10bio, devs[conf->copies]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 77 | /* allocate a r10bio with room for raid_disks entries in the |
| 78 | * bios array */ |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 79 | return kzalloc(size, gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | static void r10bio_pool_free(void *r10_bio, void *data) |
| 83 | { |
| 84 | kfree(r10_bio); |
| 85 | } |
| 86 | |
NeilBrown | 0310fa2 | 2008-08-05 15:54:14 +1000 | [diff] [blame] | 87 | /* Maximum size of each resync request */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 88 | #define RESYNC_BLOCK_SIZE (64*1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) |
NeilBrown | 0310fa2 | 2008-08-05 15:54:14 +1000 | [diff] [blame] | 90 | /* amount of memory to reserve for resync requests */ |
| 91 | #define RESYNC_WINDOW (1024*1024) |
| 92 | /* maximum number of concurrent requests, memory permitting */ |
| 93 | #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * When performing a resync, we need to read and compare, so |
| 97 | * we need as many pages are there are copies. |
| 98 | * When performing a recovery, we need 2 bios, one for read, |
| 99 | * one for write (we recover only one drive per r10buf) |
| 100 | * |
| 101 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 102 | static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 104 | struct r10conf *conf = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | struct page *page; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 106 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | struct bio *bio; |
| 108 | int i, j; |
| 109 | int nalloc; |
| 110 | |
| 111 | r10_bio = r10bio_pool_alloc(gfp_flags, conf); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 112 | if (!r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 113 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | |
| 115 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) |
| 116 | nalloc = conf->copies; /* resync */ |
| 117 | else |
| 118 | nalloc = 2; /* recovery */ |
| 119 | |
| 120 | /* |
| 121 | * Allocate bios. |
| 122 | */ |
| 123 | for (j = nalloc ; j-- ; ) { |
NeilBrown | 6746557 | 2010-10-26 17:33:54 +1100 | [diff] [blame] | 124 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | if (!bio) |
| 126 | goto out_free_bio; |
| 127 | r10_bio->devs[j].bio = bio; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 128 | if (!conf->have_replacement) |
| 129 | continue; |
| 130 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
| 131 | if (!bio) |
| 132 | goto out_free_bio; |
| 133 | r10_bio->devs[j].repl_bio = bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | } |
| 135 | /* |
| 136 | * Allocate RESYNC_PAGES data pages and attach them |
| 137 | * where needed. |
| 138 | */ |
| 139 | for (j = 0 ; j < nalloc; j++) { |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 140 | struct bio *rbio = r10_bio->devs[j].repl_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | bio = r10_bio->devs[j].bio; |
| 142 | for (i = 0; i < RESYNC_PAGES; i++) { |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 143 | if (j == 1 && !test_bit(MD_RECOVERY_SYNC, |
| 144 | &conf->mddev->recovery)) { |
| 145 | /* we can share bv_page's during recovery */ |
| 146 | struct bio *rbio = r10_bio->devs[0].bio; |
| 147 | page = rbio->bi_io_vec[i].bv_page; |
| 148 | get_page(page); |
| 149 | } else |
| 150 | page = alloc_page(gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | if (unlikely(!page)) |
| 152 | goto out_free_pages; |
| 153 | |
| 154 | bio->bi_io_vec[i].bv_page = page; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 155 | if (rbio) |
| 156 | rbio->bi_io_vec[i].bv_page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | } |
| 158 | } |
| 159 | |
| 160 | return r10_bio; |
| 161 | |
| 162 | out_free_pages: |
| 163 | for ( ; i > 0 ; i--) |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 164 | safe_put_page(bio->bi_io_vec[i-1].bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | while (j--) |
| 166 | for (i = 0; i < RESYNC_PAGES ; i++) |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 167 | safe_put_page(r10_bio->devs[j].bio->bi_io_vec[i].bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 168 | j = -1; |
| 169 | out_free_bio: |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 170 | while (++j < nalloc) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 171 | bio_put(r10_bio->devs[j].bio); |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 172 | if (r10_bio->devs[j].repl_bio) |
| 173 | bio_put(r10_bio->devs[j].repl_bio); |
| 174 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | r10bio_pool_free(r10_bio, conf); |
| 176 | return NULL; |
| 177 | } |
| 178 | |
| 179 | static void r10buf_pool_free(void *__r10_bio, void *data) |
| 180 | { |
| 181 | int i; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 182 | struct r10conf *conf = data; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 183 | struct r10bio *r10bio = __r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 184 | int j; |
| 185 | |
| 186 | for (j=0; j < conf->copies; j++) { |
| 187 | struct bio *bio = r10bio->devs[j].bio; |
| 188 | if (bio) { |
| 189 | for (i = 0; i < RESYNC_PAGES; i++) { |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 190 | safe_put_page(bio->bi_io_vec[i].bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | bio->bi_io_vec[i].bv_page = NULL; |
| 192 | } |
| 193 | bio_put(bio); |
| 194 | } |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 195 | bio = r10bio->devs[j].repl_bio; |
| 196 | if (bio) |
| 197 | bio_put(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | } |
| 199 | r10bio_pool_free(r10bio, conf); |
| 200 | } |
| 201 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 202 | static void put_all_bios(struct r10conf *conf, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | { |
| 204 | int i; |
| 205 | |
| 206 | for (i = 0; i < conf->copies; i++) { |
| 207 | struct bio **bio = & r10_bio->devs[i].bio; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 208 | if (!BIO_SPECIAL(*bio)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | bio_put(*bio); |
| 210 | *bio = NULL; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 211 | bio = &r10_bio->devs[i].repl_bio; |
| 212 | if (r10_bio->read_slot < 0 && !BIO_SPECIAL(*bio)) |
| 213 | bio_put(*bio); |
| 214 | *bio = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | } |
| 216 | } |
| 217 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 218 | static void free_r10bio(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 220 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | put_all_bios(conf, r10_bio); |
| 223 | mempool_free(r10_bio, conf->r10bio_pool); |
| 224 | } |
| 225 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 226 | static void put_buf(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 227 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 228 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | mempool_free(r10_bio, conf->r10buf_pool); |
| 231 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 232 | lower_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | } |
| 234 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 235 | static void reschedule_retry(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | { |
| 237 | unsigned long flags; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 238 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 239 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | |
| 241 | spin_lock_irqsave(&conf->device_lock, flags); |
| 242 | list_add(&r10_bio->retry_list, &conf->retry_list); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 243 | conf->nr_queued ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 244 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 245 | |
Arthur Jones | 388667b | 2008-07-25 12:03:38 -0700 | [diff] [blame] | 246 | /* wake up frozen array... */ |
| 247 | wake_up(&conf->wait_barrier); |
| 248 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | md_wakeup_thread(mddev->thread); |
| 250 | } |
| 251 | |
| 252 | /* |
| 253 | * raid_end_bio_io() is called when we have finished servicing a mirrored |
| 254 | * operation and are ready to return a success/failure code to the buffer |
| 255 | * cache layer. |
| 256 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 257 | static void raid_end_bio_io(struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | { |
| 259 | struct bio *bio = r10_bio->master_bio; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 260 | int done; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 261 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 263 | if (bio->bi_phys_segments) { |
| 264 | unsigned long flags; |
| 265 | spin_lock_irqsave(&conf->device_lock, flags); |
| 266 | bio->bi_phys_segments--; |
| 267 | done = (bio->bi_phys_segments == 0); |
| 268 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 269 | } else |
| 270 | done = 1; |
| 271 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) |
| 272 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
| 273 | if (done) { |
| 274 | bio_endio(bio, 0); |
| 275 | /* |
| 276 | * Wake up any possible resync thread that waits for the device |
| 277 | * to go idle. |
| 278 | */ |
| 279 | allow_barrier(conf); |
| 280 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 281 | free_r10bio(r10_bio); |
| 282 | } |
| 283 | |
| 284 | /* |
| 285 | * Update disk head position estimator based on IRQ completion info. |
| 286 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 287 | static inline void update_head_pos(int slot, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 289 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
| 291 | conf->mirrors[r10_bio->devs[slot].devnum].head_position = |
| 292 | r10_bio->devs[slot].addr + (r10_bio->sectors); |
| 293 | } |
| 294 | |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 295 | /* |
| 296 | * Find the disk number which triggered given bio |
| 297 | */ |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 298 | static int find_bio_disk(struct r10conf *conf, struct r10bio *r10_bio, |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 299 | struct bio *bio, int *slotp, int *replp) |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 300 | { |
| 301 | int slot; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 302 | int repl = 0; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 303 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 304 | for (slot = 0; slot < conf->copies; slot++) { |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 305 | if (r10_bio->devs[slot].bio == bio) |
| 306 | break; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 307 | if (r10_bio->devs[slot].repl_bio == bio) { |
| 308 | repl = 1; |
| 309 | break; |
| 310 | } |
| 311 | } |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 312 | |
| 313 | BUG_ON(slot == conf->copies); |
| 314 | update_head_pos(slot, r10_bio); |
| 315 | |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 316 | if (slotp) |
| 317 | *slotp = slot; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 318 | if (replp) |
| 319 | *replp = repl; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 320 | return r10_bio->devs[slot].devnum; |
| 321 | } |
| 322 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 323 | static void raid10_end_read_request(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 324 | { |
| 325 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 326 | struct r10bio *r10_bio = bio->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | int slot, dev; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 328 | struct md_rdev *rdev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 329 | struct r10conf *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | |
| 332 | slot = r10_bio->read_slot; |
| 333 | dev = r10_bio->devs[slot].devnum; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 334 | rdev = r10_bio->devs[slot].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | /* |
| 336 | * this branch is our 'one mirror IO has finished' event handler: |
| 337 | */ |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 338 | update_head_pos(slot, r10_bio); |
| 339 | |
| 340 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | /* |
| 342 | * Set R10BIO_Uptodate in our master bio, so that |
| 343 | * we will return a good error code to the higher |
| 344 | * levels even if IO on some other mirrored buffer fails. |
| 345 | * |
| 346 | * The 'master' represents the composite IO operation to |
| 347 | * user-side. So if something waits for IO, then it will |
| 348 | * wait for the 'master' bio. |
| 349 | */ |
| 350 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
NeilBrown | fae8cc5 | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 351 | } else { |
| 352 | /* If all other devices that store this block have |
| 353 | * failed, we want to return the error upwards rather |
| 354 | * than fail the last device. Here we redefine |
| 355 | * "uptodate" to mean "Don't want to retry" |
| 356 | */ |
| 357 | unsigned long flags; |
| 358 | spin_lock_irqsave(&conf->device_lock, flags); |
| 359 | if (!enough(conf, rdev->raid_disk)) |
| 360 | uptodate = 1; |
| 361 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 362 | } |
| 363 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | raid_end_bio_io(r10_bio); |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 365 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 366 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 367 | /* |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 368 | * oops, read error - keep the refcount on the rdev |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | */ |
| 370 | char b[BDEVNAME_SIZE]; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 371 | printk_ratelimited(KERN_ERR |
| 372 | "md/raid10:%s: %s: rescheduling sector %llu\n", |
| 373 | mdname(conf->mddev), |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 374 | bdevname(rdev->bdev, b), |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 375 | (unsigned long long)r10_bio->sector); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 376 | set_bit(R10BIO_ReadError, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | reschedule_retry(r10_bio); |
| 378 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | } |
| 380 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 381 | static void close_write(struct r10bio *r10_bio) |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 382 | { |
| 383 | /* clear the bitmap if all writes complete successfully */ |
| 384 | bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, |
| 385 | r10_bio->sectors, |
| 386 | !test_bit(R10BIO_Degraded, &r10_bio->state), |
| 387 | 0); |
| 388 | md_write_end(r10_bio->mddev); |
| 389 | } |
| 390 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 391 | static void one_write_done(struct r10bio *r10_bio) |
NeilBrown | 19d5f83 | 2011-09-10 17:21:17 +1000 | [diff] [blame] | 392 | { |
| 393 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 394 | if (test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 395 | reschedule_retry(r10_bio); |
| 396 | else { |
| 397 | close_write(r10_bio); |
| 398 | if (test_bit(R10BIO_MadeGood, &r10_bio->state)) |
| 399 | reschedule_retry(r10_bio); |
| 400 | else |
| 401 | raid_end_bio_io(r10_bio); |
| 402 | } |
| 403 | } |
| 404 | } |
| 405 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 406 | static void raid10_end_write_request(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | { |
| 408 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 409 | struct r10bio *r10_bio = bio->bi_private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 410 | int dev; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 411 | int dec_rdev = 1; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 412 | struct r10conf *conf = r10_bio->mddev->private; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 413 | int slot, repl; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 414 | struct md_rdev *rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 416 | dev = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 418 | if (repl) |
| 419 | rdev = conf->mirrors[dev].replacement; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 420 | if (!rdev) { |
| 421 | smp_rmb(); |
| 422 | repl = 0; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 423 | rdev = conf->mirrors[dev].rdev; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 424 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | /* |
| 426 | * this branch is our 'one mirror IO has finished' event handler: |
| 427 | */ |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 428 | if (!uptodate) { |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 429 | if (repl) |
| 430 | /* Never record new bad blocks to replacement, |
| 431 | * just fail it. |
| 432 | */ |
| 433 | md_error(rdev->mddev, rdev); |
| 434 | else { |
| 435 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 436 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 437 | set_bit(MD_RECOVERY_NEEDED, |
| 438 | &rdev->mddev->recovery); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 439 | set_bit(R10BIO_WriteError, &r10_bio->state); |
| 440 | dec_rdev = 0; |
| 441 | } |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 442 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | /* |
| 444 | * Set R10BIO_Uptodate in our master bio, so that |
| 445 | * we will return a good error code for to the higher |
| 446 | * levels even if IO on some other mirrored buffer fails. |
| 447 | * |
| 448 | * The 'master' represents the composite IO operation to |
| 449 | * user-side. So if something waits for IO, then it will |
| 450 | * wait for the 'master' bio. |
| 451 | */ |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 452 | sector_t first_bad; |
| 453 | int bad_sectors; |
| 454 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
| 456 | |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 457 | /* Maybe we can clear some bad blocks. */ |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 458 | if (is_badblock(rdev, |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 459 | r10_bio->devs[slot].addr, |
| 460 | r10_bio->sectors, |
| 461 | &first_bad, &bad_sectors)) { |
| 462 | bio_put(bio); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 463 | if (repl) |
| 464 | r10_bio->devs[slot].repl_bio = IO_MADE_GOOD; |
| 465 | else |
| 466 | r10_bio->devs[slot].bio = IO_MADE_GOOD; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 467 | dec_rdev = 0; |
| 468 | set_bit(R10BIO_MadeGood, &r10_bio->state); |
| 469 | } |
| 470 | } |
| 471 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | /* |
| 473 | * |
| 474 | * Let's see if all mirrored write operations have finished |
| 475 | * already. |
| 476 | */ |
NeilBrown | 19d5f83 | 2011-09-10 17:21:17 +1000 | [diff] [blame] | 477 | one_write_done(r10_bio); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 478 | if (dec_rdev) |
| 479 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 480 | } |
| 481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | /* |
| 483 | * RAID10 layout manager |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 484 | * As well as the chunksize and raid_disks count, there are two |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | * parameters: near_copies and far_copies. |
| 486 | * near_copies * far_copies must be <= raid_disks. |
| 487 | * Normally one of these will be 1. |
| 488 | * If both are 1, we get raid0. |
| 489 | * If near_copies == raid_disks, we get raid1. |
| 490 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 491 | * 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] | 492 | * first chunk, followed by near_copies copies of the next chunk and |
| 493 | * so on. |
| 494 | * If far_copies > 1, then after 1/far_copies of the array has been assigned |
| 495 | * as described above, we start again with a device offset of near_copies. |
| 496 | * So we effectively have another copy of the whole array further down all |
| 497 | * the drives, but with blocks on different drives. |
| 498 | * With this layout, and block is never stored twice on the one device. |
| 499 | * |
| 500 | * raid10_find_phys finds the sector offset of a given virtual sector |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 501 | * on each device that it is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | * |
| 503 | * raid10_find_virt does the reverse mapping, from a device and a |
| 504 | * sector offset to a virtual address |
| 505 | */ |
| 506 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 507 | static void raid10_find_phys(struct r10conf *conf, struct r10bio *r10bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | { |
| 509 | int n,f; |
| 510 | sector_t sector; |
| 511 | sector_t chunk; |
| 512 | sector_t stripe; |
| 513 | int dev; |
| 514 | |
| 515 | int slot = 0; |
| 516 | |
| 517 | /* now calculate first sector/dev */ |
| 518 | chunk = r10bio->sector >> conf->chunk_shift; |
| 519 | sector = r10bio->sector & conf->chunk_mask; |
| 520 | |
| 521 | chunk *= conf->near_copies; |
| 522 | stripe = chunk; |
| 523 | dev = sector_div(stripe, conf->raid_disks); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 524 | if (conf->far_offset) |
| 525 | stripe *= conf->far_copies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | |
| 527 | sector += stripe << conf->chunk_shift; |
| 528 | |
| 529 | /* and calculate all the others */ |
| 530 | for (n=0; n < conf->near_copies; n++) { |
| 531 | int d = dev; |
| 532 | sector_t s = sector; |
| 533 | r10bio->devs[slot].addr = sector; |
| 534 | r10bio->devs[slot].devnum = d; |
| 535 | slot++; |
| 536 | |
| 537 | for (f = 1; f < conf->far_copies; f++) { |
| 538 | d += conf->near_copies; |
| 539 | if (d >= conf->raid_disks) |
| 540 | d -= conf->raid_disks; |
| 541 | s += conf->stride; |
| 542 | r10bio->devs[slot].devnum = d; |
| 543 | r10bio->devs[slot].addr = s; |
| 544 | slot++; |
| 545 | } |
| 546 | dev++; |
| 547 | if (dev >= conf->raid_disks) { |
| 548 | dev = 0; |
| 549 | sector += (conf->chunk_mask + 1); |
| 550 | } |
| 551 | } |
| 552 | BUG_ON(slot != conf->copies); |
| 553 | } |
| 554 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 555 | 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] | 556 | { |
| 557 | sector_t offset, chunk, vchunk; |
| 558 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | offset = sector & conf->chunk_mask; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 560 | if (conf->far_offset) { |
| 561 | int fc; |
| 562 | chunk = sector >> conf->chunk_shift; |
| 563 | fc = sector_div(chunk, conf->far_copies); |
| 564 | dev -= fc * conf->near_copies; |
| 565 | if (dev < 0) |
| 566 | dev += conf->raid_disks; |
| 567 | } else { |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 568 | while (sector >= conf->stride) { |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 569 | sector -= conf->stride; |
| 570 | if (dev < conf->near_copies) |
| 571 | dev += conf->raid_disks - conf->near_copies; |
| 572 | else |
| 573 | dev -= conf->near_copies; |
| 574 | } |
| 575 | chunk = sector >> conf->chunk_shift; |
| 576 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 577 | vchunk = chunk * conf->raid_disks + dev; |
| 578 | sector_div(vchunk, conf->near_copies); |
| 579 | return (vchunk << conf->chunk_shift) + offset; |
| 580 | } |
| 581 | |
| 582 | /** |
| 583 | * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged |
| 584 | * @q: request queue |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 585 | * @bvm: properties of new bio |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | * @biovec: the request that could be merged to it. |
| 587 | * |
| 588 | * Return amount of bytes we can accept at this offset |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 589 | * This requires checking for end-of-chunk if near_copies != raid_disks, |
| 590 | * and for subordinate merge_bvec_fns if merge_check_needed. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 592 | static int raid10_mergeable_bvec(struct request_queue *q, |
| 593 | struct bvec_merge_data *bvm, |
| 594 | struct bio_vec *biovec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 596 | struct mddev *mddev = q->queuedata; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 597 | struct r10conf *conf = mddev->private; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 598 | sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 599 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 600 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 601 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 603 | if (conf->near_copies < conf->raid_disks) { |
| 604 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) |
| 605 | + bio_sectors)) << 9; |
| 606 | if (max < 0) |
| 607 | /* bio_add cannot handle a negative return */ |
| 608 | max = 0; |
| 609 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 610 | return biovec->bv_len; |
| 611 | } else |
| 612 | max = biovec->bv_len; |
| 613 | |
| 614 | if (mddev->merge_check_needed) { |
| 615 | struct r10bio r10_bio; |
| 616 | int s; |
| 617 | r10_bio.sector = sector; |
| 618 | raid10_find_phys(conf, &r10_bio); |
| 619 | rcu_read_lock(); |
| 620 | for (s = 0; s < conf->copies; s++) { |
| 621 | int disk = r10_bio.devs[s].devnum; |
| 622 | struct md_rdev *rdev = rcu_dereference( |
| 623 | conf->mirrors[disk].rdev); |
| 624 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
| 625 | struct request_queue *q = |
| 626 | bdev_get_queue(rdev->bdev); |
| 627 | if (q->merge_bvec_fn) { |
| 628 | bvm->bi_sector = r10_bio.devs[s].addr |
| 629 | + rdev->data_offset; |
| 630 | bvm->bi_bdev = rdev->bdev; |
| 631 | max = min(max, q->merge_bvec_fn( |
| 632 | q, bvm, biovec)); |
| 633 | } |
| 634 | } |
| 635 | rdev = rcu_dereference(conf->mirrors[disk].replacement); |
| 636 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
| 637 | struct request_queue *q = |
| 638 | bdev_get_queue(rdev->bdev); |
| 639 | if (q->merge_bvec_fn) { |
| 640 | bvm->bi_sector = r10_bio.devs[s].addr |
| 641 | + rdev->data_offset; |
| 642 | bvm->bi_bdev = rdev->bdev; |
| 643 | max = min(max, q->merge_bvec_fn( |
| 644 | q, bvm, biovec)); |
| 645 | } |
| 646 | } |
| 647 | } |
| 648 | rcu_read_unlock(); |
| 649 | } |
| 650 | return max; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | } |
| 652 | |
| 653 | /* |
| 654 | * This routine returns the disk from which the requested read should |
| 655 | * be done. There is a per-array 'next expected sequential IO' sector |
| 656 | * number - if this matches on the next IO then we use the last disk. |
| 657 | * There is also a per-disk 'last know head position' sector that is |
| 658 | * maintained from IRQ contexts, both the normal and the resync IO |
| 659 | * completion handlers update this position correctly. If there is no |
| 660 | * perfect sequential match then we pick the disk whose head is closest. |
| 661 | * |
| 662 | * If there are 2 mirrors in the same 2 devices, performance degrades |
| 663 | * because position is mirror, not device based. |
| 664 | * |
| 665 | * The rdev for the device selected will have nr_pending incremented. |
| 666 | */ |
| 667 | |
| 668 | /* |
| 669 | * FIXME: possibly should rethink readbalancing and do it differently |
| 670 | * depending on near_copies / far_copies geometry. |
| 671 | */ |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 672 | static struct md_rdev *read_balance(struct r10conf *conf, |
| 673 | struct r10bio *r10_bio, |
| 674 | int *max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | { |
NeilBrown | af3a2cd | 2010-05-08 08:20:17 +1000 | [diff] [blame] | 676 | const sector_t this_sector = r10_bio->sector; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 677 | int disk, slot; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 678 | int sectors = r10_bio->sectors; |
| 679 | int best_good_sectors; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 680 | sector_t new_distance, best_dist; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 681 | struct md_rdev *rdev, *best_rdev; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 682 | int do_balance; |
| 683 | int best_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 684 | |
| 685 | raid10_find_phys(conf, r10_bio); |
| 686 | rcu_read_lock(); |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 687 | retry: |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 688 | sectors = r10_bio->sectors; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 689 | best_slot = -1; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 690 | best_rdev = NULL; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 691 | best_dist = MaxSector; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 692 | best_good_sectors = 0; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 693 | do_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 694 | /* |
| 695 | * Check if we can balance. We can balance on the whole |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 696 | * device if no resync is going on (recovery is ok), or below |
| 697 | * the resync window. We take the first readable disk when |
| 698 | * above the resync window. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | */ |
| 700 | if (conf->mddev->recovery_cp < MaxSector |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 701 | && (this_sector + sectors >= conf->next_resync)) |
| 702 | do_balance = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 703 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 704 | for (slot = 0; slot < conf->copies ; slot++) { |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 705 | sector_t first_bad; |
| 706 | int bad_sectors; |
| 707 | sector_t dev_sector; |
| 708 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 709 | if (r10_bio->devs[slot].bio == IO_BLOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | continue; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 711 | disk = r10_bio->devs[slot].devnum; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 712 | rdev = rcu_dereference(conf->mirrors[disk].replacement); |
| 713 | if (rdev == NULL || test_bit(Faulty, &rdev->flags) || |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 714 | test_bit(Unmerged, &rdev->flags) || |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 715 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) |
| 716 | rdev = rcu_dereference(conf->mirrors[disk].rdev); |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 717 | if (rdev == NULL || |
| 718 | test_bit(Faulty, &rdev->flags) || |
| 719 | test_bit(Unmerged, &rdev->flags)) |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 720 | continue; |
| 721 | if (!test_bit(In_sync, &rdev->flags) && |
| 722 | r10_bio->devs[slot].addr + sectors > rdev->recovery_offset) |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 723 | continue; |
| 724 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 725 | dev_sector = r10_bio->devs[slot].addr; |
| 726 | if (is_badblock(rdev, dev_sector, sectors, |
| 727 | &first_bad, &bad_sectors)) { |
| 728 | if (best_dist < MaxSector) |
| 729 | /* Already have a better slot */ |
| 730 | continue; |
| 731 | if (first_bad <= dev_sector) { |
| 732 | /* Cannot read here. If this is the |
| 733 | * 'primary' device, then we must not read |
| 734 | * beyond 'bad_sectors' from another device. |
| 735 | */ |
| 736 | bad_sectors -= (dev_sector - first_bad); |
| 737 | if (!do_balance && sectors > bad_sectors) |
| 738 | sectors = bad_sectors; |
| 739 | if (best_good_sectors > sectors) |
| 740 | best_good_sectors = sectors; |
| 741 | } else { |
| 742 | sector_t good_sectors = |
| 743 | first_bad - dev_sector; |
| 744 | if (good_sectors > best_good_sectors) { |
| 745 | best_good_sectors = good_sectors; |
| 746 | best_slot = slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 747 | best_rdev = rdev; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 748 | } |
| 749 | if (!do_balance) |
| 750 | /* Must read from here */ |
| 751 | break; |
| 752 | } |
| 753 | continue; |
| 754 | } else |
| 755 | best_good_sectors = sectors; |
| 756 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 757 | if (!do_balance) |
| 758 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | |
NeilBrown | 22dfdf5 | 2005-11-28 13:44:09 -0800 | [diff] [blame] | 760 | /* This optimisation is debatable, and completely destroys |
| 761 | * sequential read speed for 'far copies' arrays. So only |
| 762 | * keep it for 'near' arrays, and review those later. |
| 763 | */ |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 764 | if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 765 | break; |
Keld Simonsen | 8ed3a19 | 2008-03-04 14:29:34 -0800 | [diff] [blame] | 766 | |
| 767 | /* for far > 1 always use the lowest address */ |
| 768 | if (conf->far_copies > 1) |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 769 | new_distance = r10_bio->devs[slot].addr; |
Keld Simonsen | 8ed3a19 | 2008-03-04 14:29:34 -0800 | [diff] [blame] | 770 | else |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 771 | new_distance = abs(r10_bio->devs[slot].addr - |
| 772 | conf->mirrors[disk].head_position); |
| 773 | if (new_distance < best_dist) { |
| 774 | best_dist = new_distance; |
| 775 | best_slot = slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 776 | best_rdev = rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | } |
| 778 | } |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 779 | if (slot >= conf->copies) { |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 780 | slot = best_slot; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 781 | rdev = best_rdev; |
| 782 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 783 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 784 | if (slot >= 0) { |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 785 | atomic_inc(&rdev->nr_pending); |
| 786 | if (test_bit(Faulty, &rdev->flags)) { |
| 787 | /* Cannot risk returning a device that failed |
| 788 | * before we inc'ed nr_pending |
| 789 | */ |
| 790 | rdev_dec_pending(rdev, conf->mddev); |
| 791 | goto retry; |
| 792 | } |
| 793 | r10_bio->read_slot = slot; |
| 794 | } else |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 795 | rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 796 | rcu_read_unlock(); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 797 | *max_sectors = best_good_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 798 | |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 799 | return rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 800 | } |
| 801 | |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 802 | static int raid10_congested(void *data, int bits) |
| 803 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 804 | struct mddev *mddev = data; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 805 | struct r10conf *conf = mddev->private; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 806 | int i, ret = 0; |
| 807 | |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 808 | if ((bits & (1 << BDI_async_congested)) && |
| 809 | conf->pending_count >= max_queued_requests) |
| 810 | return 1; |
| 811 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 812 | if (mddev_congested(mddev, bits)) |
| 813 | return 1; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 814 | rcu_read_lock(); |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 815 | for (i = 0; i < conf->raid_disks && ret == 0; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 816 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[i].rdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 817 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 818 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 819 | |
| 820 | ret |= bdi_congested(&q->backing_dev_info, bits); |
| 821 | } |
| 822 | } |
| 823 | rcu_read_unlock(); |
| 824 | return ret; |
| 825 | } |
| 826 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 827 | static void flush_pending_writes(struct r10conf *conf) |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 828 | { |
| 829 | /* Any writes that have been queued but are awaiting |
| 830 | * bitmap updates get flushed here. |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 831 | */ |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 832 | spin_lock_irq(&conf->device_lock); |
| 833 | |
| 834 | if (conf->pending_bio_list.head) { |
| 835 | struct bio *bio; |
| 836 | bio = bio_list_get(&conf->pending_bio_list); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 837 | conf->pending_count = 0; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 838 | spin_unlock_irq(&conf->device_lock); |
| 839 | /* flush any pending bitmap writes to disk |
| 840 | * before proceeding w/ I/O */ |
| 841 | bitmap_unplug(conf->mddev->bitmap); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 842 | wake_up(&conf->wait_barrier); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 843 | |
| 844 | while (bio) { /* submit pending writes */ |
| 845 | struct bio *next = bio->bi_next; |
| 846 | bio->bi_next = NULL; |
| 847 | generic_make_request(bio); |
| 848 | bio = next; |
| 849 | } |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 850 | } else |
| 851 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 852 | } |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 853 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 854 | /* Barriers.... |
| 855 | * Sometimes we need to suspend IO while we do something else, |
| 856 | * either some resync/recovery, or reconfigure the array. |
| 857 | * To do this we raise a 'barrier'. |
| 858 | * The 'barrier' is a counter that can be raised multiple times |
| 859 | * to count how many activities are happening which preclude |
| 860 | * normal IO. |
| 861 | * We can only raise the barrier if there is no pending IO. |
| 862 | * i.e. if nr_pending == 0. |
| 863 | * We choose only to raise the barrier if no-one is waiting for the |
| 864 | * barrier to go down. This means that as soon as an IO request |
| 865 | * is ready, no other operations which require a barrier will start |
| 866 | * until the IO request has had a chance. |
| 867 | * |
| 868 | * So: regular IO calls 'wait_barrier'. When that returns there |
| 869 | * is no backgroup IO happening, It must arrange to call |
| 870 | * allow_barrier when it has finished its IO. |
| 871 | * backgroup IO calls must call raise_barrier. Once that returns |
| 872 | * there is no normal IO happeing. It must arrange to call |
| 873 | * lower_barrier when the particular background IO completes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 874 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 875 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 876 | static void raise_barrier(struct r10conf *conf, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 877 | { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 878 | BUG_ON(force && !conf->barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 879 | spin_lock_irq(&conf->resync_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 880 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 881 | /* Wait until no block IO is waiting (unless 'force') */ |
| 882 | wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 883 | conf->resync_lock, ); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 884 | |
| 885 | /* block any new IO from starting */ |
| 886 | conf->barrier++; |
| 887 | |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 888 | /* Now wait for all pending IO to complete */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 889 | wait_event_lock_irq(conf->wait_barrier, |
| 890 | !conf->nr_pending && conf->barrier < RESYNC_DEPTH, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 891 | conf->resync_lock, ); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 892 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 893 | spin_unlock_irq(&conf->resync_lock); |
| 894 | } |
| 895 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 896 | static void lower_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 897 | { |
| 898 | unsigned long flags; |
| 899 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 900 | conf->barrier--; |
| 901 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 902 | wake_up(&conf->wait_barrier); |
| 903 | } |
| 904 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 905 | static void wait_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 906 | { |
| 907 | spin_lock_irq(&conf->resync_lock); |
| 908 | if (conf->barrier) { |
| 909 | conf->nr_waiting++; |
NeilBrown | d6b42dc | 2012-03-19 12:46:38 +1100 | [diff] [blame] | 910 | /* Wait for the barrier to drop. |
| 911 | * However if there are already pending |
| 912 | * requests (preventing the barrier from |
| 913 | * rising completely), and the |
| 914 | * pre-process bio queue isn't empty, |
| 915 | * then don't wait, as we need to empty |
| 916 | * that queue to get the nr_pending |
| 917 | * count down. |
| 918 | */ |
| 919 | wait_event_lock_irq(conf->wait_barrier, |
| 920 | !conf->barrier || |
| 921 | (conf->nr_pending && |
| 922 | current->bio_list && |
| 923 | !bio_list_empty(current->bio_list)), |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 924 | conf->resync_lock, |
NeilBrown | d6b42dc | 2012-03-19 12:46:38 +1100 | [diff] [blame] | 925 | ); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 926 | conf->nr_waiting--; |
| 927 | } |
| 928 | conf->nr_pending++; |
| 929 | spin_unlock_irq(&conf->resync_lock); |
| 930 | } |
| 931 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 932 | static void allow_barrier(struct r10conf *conf) |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 933 | { |
| 934 | unsigned long flags; |
| 935 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 936 | conf->nr_pending--; |
| 937 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 938 | wake_up(&conf->wait_barrier); |
| 939 | } |
| 940 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 941 | static void freeze_array(struct r10conf *conf) |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 942 | { |
| 943 | /* stop syncio and normal IO and wait for everything to |
NeilBrown | f188593 | 2006-01-06 00:20:42 -0800 | [diff] [blame] | 944 | * go quiet. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 945 | * We increment barrier and nr_waiting, and then |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 946 | * wait until nr_pending match nr_queued+1 |
| 947 | * This is called in the context of one normal IO request |
| 948 | * that has failed. Thus any sync request that might be pending |
| 949 | * will be blocked by nr_pending, and we need to wait for |
| 950 | * pending IO requests to complete or be queued for re-try. |
| 951 | * Thus the number queued (nr_queued) plus this request (1) |
| 952 | * must match the number of pending IOs (nr_pending) before |
| 953 | * we continue. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 954 | */ |
| 955 | spin_lock_irq(&conf->resync_lock); |
| 956 | conf->barrier++; |
| 957 | conf->nr_waiting++; |
| 958 | wait_event_lock_irq(conf->wait_barrier, |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 959 | conf->nr_pending == conf->nr_queued+1, |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 960 | conf->resync_lock, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 961 | flush_pending_writes(conf)); |
| 962 | |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 963 | spin_unlock_irq(&conf->resync_lock); |
| 964 | } |
| 965 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 966 | static void unfreeze_array(struct r10conf *conf) |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 967 | { |
| 968 | /* reverse the effect of the freeze */ |
| 969 | spin_lock_irq(&conf->resync_lock); |
| 970 | conf->barrier--; |
| 971 | conf->nr_waiting--; |
| 972 | wake_up(&conf->wait_barrier); |
| 973 | spin_unlock_irq(&conf->resync_lock); |
| 974 | } |
| 975 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 976 | static void make_request(struct mddev *mddev, struct bio * bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 977 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 978 | struct r10conf *conf = mddev->private; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 979 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 980 | struct bio *read_bio; |
| 981 | int i; |
| 982 | int chunk_sects = conf->chunk_mask + 1; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 983 | const int rw = bio_data_dir(bio); |
NeilBrown | 2c7d46e | 2010-08-18 16:16:05 +1000 | [diff] [blame] | 984 | const unsigned long do_sync = (bio->bi_rw & REQ_SYNC); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 985 | const unsigned long do_fua = (bio->bi_rw & REQ_FUA); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 986 | unsigned long flags; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 987 | struct md_rdev *blocked_rdev; |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 988 | int plugged; |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 989 | int sectors_handled; |
| 990 | int max_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 991 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 992 | if (unlikely(bio->bi_rw & REQ_FLUSH)) { |
| 993 | md_flush_request(mddev, bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 994 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 995 | } |
| 996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 997 | /* If this request crosses a chunk boundary, we need to |
| 998 | * split it. This will only happen for 1 PAGE (or less) requests. |
| 999 | */ |
| 1000 | if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9) |
| 1001 | > chunk_sects && |
| 1002 | conf->near_copies < conf->raid_disks)) { |
| 1003 | struct bio_pair *bp; |
| 1004 | /* Sanity check -- queue functions should prevent this happening */ |
| 1005 | if (bio->bi_vcnt != 1 || |
| 1006 | bio->bi_idx != 0) |
| 1007 | goto bad_map; |
| 1008 | /* This is a one page bio that upper layers |
| 1009 | * refuse to split for us, so we need to split it. |
| 1010 | */ |
Denis ChengRq | 6feef53 | 2008-10-09 08:57:05 +0200 | [diff] [blame] | 1011 | bp = bio_split(bio, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1012 | chunk_sects - (bio->bi_sector & (chunk_sects - 1)) ); |
NeilBrown | 51e9ac7 | 2010-08-07 21:17:00 +1000 | [diff] [blame] | 1013 | |
| 1014 | /* Each of these 'make_request' calls will call 'wait_barrier'. |
| 1015 | * If the first succeeds but the second blocks due to the resync |
| 1016 | * thread raising the barrier, we will deadlock because the |
| 1017 | * IO to the underlying device will be queued in generic_make_request |
| 1018 | * and will never complete, so will never reduce nr_pending. |
| 1019 | * So increment nr_waiting here so no new raise_barriers will |
| 1020 | * succeed, and so the second wait_barrier cannot block. |
| 1021 | */ |
| 1022 | spin_lock_irq(&conf->resync_lock); |
| 1023 | conf->nr_waiting++; |
| 1024 | spin_unlock_irq(&conf->resync_lock); |
| 1025 | |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 1026 | make_request(mddev, &bp->bio1); |
| 1027 | make_request(mddev, &bp->bio2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1028 | |
NeilBrown | 51e9ac7 | 2010-08-07 21:17:00 +1000 | [diff] [blame] | 1029 | spin_lock_irq(&conf->resync_lock); |
| 1030 | conf->nr_waiting--; |
| 1031 | wake_up(&conf->wait_barrier); |
| 1032 | spin_unlock_irq(&conf->resync_lock); |
| 1033 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1034 | bio_pair_release(bp); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 1035 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1036 | bad_map: |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1037 | printk("md/raid10:%s: make_request bug: can't convert block across chunks" |
| 1038 | " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | (unsigned long long)bio->bi_sector, bio->bi_size >> 10); |
| 1040 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1041 | bio_io_error(bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 1042 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1045 | md_write_start(mddev, bio); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1046 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | /* |
| 1048 | * Register the new request and wait if the reconstruction |
| 1049 | * thread has put up a bar for new requests. |
| 1050 | * Continue immediately if no resync is active currently. |
| 1051 | */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1052 | wait_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1054 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
| 1055 | |
| 1056 | r10_bio->master_bio = bio; |
| 1057 | r10_bio->sectors = bio->bi_size >> 9; |
| 1058 | |
| 1059 | r10_bio->mddev = mddev; |
| 1060 | r10_bio->sector = bio->bi_sector; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1061 | r10_bio->state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1062 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1063 | /* We might need to issue multiple reads to different |
| 1064 | * devices if there are bad blocks around, so we keep |
| 1065 | * track of the number of reads in bio->bi_phys_segments. |
| 1066 | * If this is 0, there is only one r10_bio and no locking |
| 1067 | * will be needed when the request completes. If it is |
| 1068 | * non-zero, then it is the number of not-completed requests. |
| 1069 | */ |
| 1070 | bio->bi_phys_segments = 0; |
| 1071 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 1072 | |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 1073 | if (rw == READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1074 | /* |
| 1075 | * read balancing logic: |
| 1076 | */ |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1077 | struct md_rdev *rdev; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1078 | int slot; |
| 1079 | |
| 1080 | read_again: |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1081 | rdev = read_balance(conf, r10_bio, &max_sectors); |
| 1082 | if (!rdev) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1083 | raid_end_bio_io(r10_bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 1084 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1085 | } |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1086 | slot = r10_bio->read_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 1088 | read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1089 | md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector, |
| 1090 | max_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1091 | |
| 1092 | r10_bio->devs[slot].bio = read_bio; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1093 | r10_bio->devs[slot].rdev = rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1094 | |
| 1095 | read_bio->bi_sector = r10_bio->devs[slot].addr + |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1096 | rdev->data_offset; |
| 1097 | read_bio->bi_bdev = rdev->bdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | read_bio->bi_end_io = raid10_end_read_request; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 1099 | read_bio->bi_rw = READ | do_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1100 | read_bio->bi_private = r10_bio; |
| 1101 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1102 | if (max_sectors < r10_bio->sectors) { |
| 1103 | /* Could not read all from this device, so we will |
| 1104 | * need another r10_bio. |
| 1105 | */ |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1106 | sectors_handled = (r10_bio->sectors + max_sectors |
| 1107 | - bio->bi_sector); |
| 1108 | r10_bio->sectors = max_sectors; |
| 1109 | spin_lock_irq(&conf->device_lock); |
| 1110 | if (bio->bi_phys_segments == 0) |
| 1111 | bio->bi_phys_segments = 2; |
| 1112 | else |
| 1113 | bio->bi_phys_segments++; |
| 1114 | spin_unlock(&conf->device_lock); |
| 1115 | /* Cannot call generic_make_request directly |
| 1116 | * as that will be queued in __generic_make_request |
| 1117 | * and subsequent mempool_alloc might block |
| 1118 | * waiting for it. so hand bio over to raid10d. |
| 1119 | */ |
| 1120 | reschedule_retry(r10_bio); |
| 1121 | |
| 1122 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
| 1123 | |
| 1124 | r10_bio->master_bio = bio; |
| 1125 | r10_bio->sectors = ((bio->bi_size >> 9) |
| 1126 | - sectors_handled); |
| 1127 | r10_bio->state = 0; |
| 1128 | r10_bio->mddev = mddev; |
| 1129 | r10_bio->sector = bio->bi_sector + sectors_handled; |
| 1130 | goto read_again; |
| 1131 | } else |
| 1132 | generic_make_request(read_bio); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 1133 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1134 | } |
| 1135 | |
| 1136 | /* |
| 1137 | * WRITE: |
| 1138 | */ |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 1139 | if (conf->pending_count >= max_queued_requests) { |
| 1140 | md_wakeup_thread(mddev->thread); |
| 1141 | wait_event(conf->wait_barrier, |
| 1142 | conf->pending_count < max_queued_requests); |
| 1143 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1144 | /* first select target devices under rcu_lock and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1145 | * inc refcount on their rdev. Record them by setting |
| 1146 | * bios[x] to bio |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1147 | * If there are known/acknowledged bad blocks on any device |
| 1148 | * on which we have seen a write error, we want to avoid |
| 1149 | * writing to those blocks. This potentially requires several |
| 1150 | * writes to write around the bad blocks. Each set of writes |
| 1151 | * gets its own r10_bio with a set of bios attached. The number |
| 1152 | * of r10_bios is recored in bio->bi_phys_segments just as with |
| 1153 | * the read case. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1154 | */ |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1155 | plugged = mddev_check_plugged(mddev); |
| 1156 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1157 | r10_bio->read_slot = -1; /* make sure repl_bio gets freed */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1158 | raid10_find_phys(conf, r10_bio); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1159 | retry_write: |
Harvey Harrison | cb6969e | 2008-05-06 20:42:32 -0700 | [diff] [blame] | 1160 | blocked_rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1161 | rcu_read_lock(); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1162 | max_sectors = r10_bio->sectors; |
| 1163 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1164 | for (i = 0; i < conf->copies; i++) { |
| 1165 | int d = r10_bio->devs[i].devnum; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1166 | struct md_rdev *rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1167 | struct md_rdev *rrdev = rcu_dereference( |
| 1168 | conf->mirrors[d].replacement); |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1169 | if (rdev == rrdev) |
| 1170 | rrdev = NULL; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1171 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
| 1172 | atomic_inc(&rdev->nr_pending); |
| 1173 | blocked_rdev = rdev; |
| 1174 | break; |
| 1175 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1176 | if (rrdev && unlikely(test_bit(Blocked, &rrdev->flags))) { |
| 1177 | atomic_inc(&rrdev->nr_pending); |
| 1178 | blocked_rdev = rrdev; |
| 1179 | break; |
| 1180 | } |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1181 | if (rrdev && (test_bit(Faulty, &rrdev->flags) |
| 1182 | || test_bit(Unmerged, &rrdev->flags))) |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1183 | rrdev = NULL; |
| 1184 | |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1185 | r10_bio->devs[i].bio = NULL; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1186 | r10_bio->devs[i].repl_bio = NULL; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1187 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 1188 | test_bit(Unmerged, &rdev->flags)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1189 | set_bit(R10BIO_Degraded, &r10_bio->state); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1190 | continue; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1191 | } |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1192 | if (test_bit(WriteErrorSeen, &rdev->flags)) { |
| 1193 | sector_t first_bad; |
| 1194 | sector_t dev_sector = r10_bio->devs[i].addr; |
| 1195 | int bad_sectors; |
| 1196 | int is_bad; |
| 1197 | |
| 1198 | is_bad = is_badblock(rdev, dev_sector, |
| 1199 | max_sectors, |
| 1200 | &first_bad, &bad_sectors); |
| 1201 | if (is_bad < 0) { |
| 1202 | /* Mustn't write here until the bad block |
| 1203 | * is acknowledged |
| 1204 | */ |
| 1205 | atomic_inc(&rdev->nr_pending); |
| 1206 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 1207 | blocked_rdev = rdev; |
| 1208 | break; |
| 1209 | } |
| 1210 | if (is_bad && first_bad <= dev_sector) { |
| 1211 | /* Cannot write here at all */ |
| 1212 | bad_sectors -= (dev_sector - first_bad); |
| 1213 | if (bad_sectors < max_sectors) |
| 1214 | /* Mustn't write more than bad_sectors |
| 1215 | * to other devices yet |
| 1216 | */ |
| 1217 | max_sectors = bad_sectors; |
| 1218 | /* We don't set R10BIO_Degraded as that |
| 1219 | * only applies if the disk is missing, |
| 1220 | * so it might be re-added, and we want to |
| 1221 | * know to recover this chunk. |
| 1222 | * In this case the device is here, and the |
| 1223 | * fact that this chunk is not in-sync is |
| 1224 | * recorded in the bad block log. |
| 1225 | */ |
| 1226 | continue; |
| 1227 | } |
| 1228 | if (is_bad) { |
| 1229 | int good_sectors = first_bad - dev_sector; |
| 1230 | if (good_sectors < max_sectors) |
| 1231 | max_sectors = good_sectors; |
| 1232 | } |
| 1233 | } |
| 1234 | r10_bio->devs[i].bio = bio; |
| 1235 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1236 | if (rrdev) { |
| 1237 | r10_bio->devs[i].repl_bio = bio; |
| 1238 | atomic_inc(&rrdev->nr_pending); |
| 1239 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1240 | } |
| 1241 | rcu_read_unlock(); |
| 1242 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1243 | if (unlikely(blocked_rdev)) { |
| 1244 | /* Have to wait for this device to get unblocked, then retry */ |
| 1245 | int j; |
| 1246 | int d; |
| 1247 | |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1248 | for (j = 0; j < i; j++) { |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1249 | if (r10_bio->devs[j].bio) { |
| 1250 | d = r10_bio->devs[j].devnum; |
| 1251 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); |
| 1252 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1253 | if (r10_bio->devs[j].repl_bio) { |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1254 | struct md_rdev *rdev; |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1255 | d = r10_bio->devs[j].devnum; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1256 | rdev = conf->mirrors[d].replacement; |
| 1257 | if (!rdev) { |
| 1258 | /* Race with remove_disk */ |
| 1259 | smp_mb(); |
| 1260 | rdev = conf->mirrors[d].rdev; |
| 1261 | } |
| 1262 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1263 | } |
| 1264 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 1265 | allow_barrier(conf); |
| 1266 | md_wait_for_blocked_rdev(blocked_rdev, mddev); |
| 1267 | wait_barrier(conf); |
| 1268 | goto retry_write; |
| 1269 | } |
| 1270 | |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1271 | if (max_sectors < r10_bio->sectors) { |
| 1272 | /* We are splitting this into multiple parts, so |
| 1273 | * we need to prepare for allocating another r10_bio. |
| 1274 | */ |
| 1275 | r10_bio->sectors = max_sectors; |
| 1276 | spin_lock_irq(&conf->device_lock); |
| 1277 | if (bio->bi_phys_segments == 0) |
| 1278 | bio->bi_phys_segments = 2; |
| 1279 | else |
| 1280 | bio->bi_phys_segments++; |
| 1281 | spin_unlock_irq(&conf->device_lock); |
| 1282 | } |
| 1283 | sectors_handled = r10_bio->sector + max_sectors - bio->bi_sector; |
| 1284 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1285 | atomic_set(&r10_bio->remaining, 1); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1286 | bitmap_startwrite(mddev->bitmap, r10_bio->sector, r10_bio->sectors, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1287 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1288 | for (i = 0; i < conf->copies; i++) { |
| 1289 | struct bio *mbio; |
| 1290 | int d = r10_bio->devs[i].devnum; |
| 1291 | if (!r10_bio->devs[i].bio) |
| 1292 | continue; |
| 1293 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 1294 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1295 | md_trim_bio(mbio, r10_bio->sector - bio->bi_sector, |
| 1296 | max_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1297 | r10_bio->devs[i].bio = mbio; |
| 1298 | |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1299 | mbio->bi_sector = (r10_bio->devs[i].addr+ |
| 1300 | conf->mirrors[d].rdev->data_offset); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | mbio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 1302 | mbio->bi_end_io = raid10_end_write_request; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1303 | mbio->bi_rw = WRITE | do_sync | do_fua; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | mbio->bi_private = r10_bio; |
| 1305 | |
| 1306 | atomic_inc(&r10_bio->remaining); |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1307 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1308 | bio_list_add(&conf->pending_bio_list, mbio); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 1309 | conf->pending_count++; |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1310 | spin_unlock_irqrestore(&conf->device_lock, flags); |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1311 | |
| 1312 | if (!r10_bio->devs[i].repl_bio) |
| 1313 | continue; |
| 1314 | |
| 1315 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
| 1316 | md_trim_bio(mbio, r10_bio->sector - bio->bi_sector, |
| 1317 | max_sectors); |
| 1318 | r10_bio->devs[i].repl_bio = mbio; |
| 1319 | |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1320 | /* We are actively writing to the original device |
| 1321 | * so it cannot disappear, so the replacement cannot |
| 1322 | * become NULL here |
| 1323 | */ |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1324 | mbio->bi_sector = (r10_bio->devs[i].addr+ |
| 1325 | conf->mirrors[d].replacement->data_offset); |
| 1326 | mbio->bi_bdev = conf->mirrors[d].replacement->bdev; |
| 1327 | mbio->bi_end_io = raid10_end_write_request; |
| 1328 | mbio->bi_rw = WRITE | do_sync | do_fua; |
| 1329 | mbio->bi_private = r10_bio; |
| 1330 | |
| 1331 | atomic_inc(&r10_bio->remaining); |
| 1332 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1333 | bio_list_add(&conf->pending_bio_list, mbio); |
| 1334 | conf->pending_count++; |
| 1335 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | } |
| 1337 | |
NeilBrown | 079fa16 | 2011-09-10 17:21:23 +1000 | [diff] [blame] | 1338 | /* Don't remove the bias on 'remaining' (one_write_done) until |
| 1339 | * after checking if we need to go around again. |
| 1340 | */ |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1341 | |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1342 | if (sectors_handled < (bio->bi_size >> 9)) { |
NeilBrown | 079fa16 | 2011-09-10 17:21:23 +1000 | [diff] [blame] | 1343 | one_write_done(r10_bio); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1344 | /* We need another r10_bio. It has already been counted |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1345 | * in bio->bi_phys_segments. |
| 1346 | */ |
| 1347 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
| 1348 | |
| 1349 | r10_bio->master_bio = bio; |
| 1350 | r10_bio->sectors = (bio->bi_size >> 9) - sectors_handled; |
| 1351 | |
| 1352 | r10_bio->mddev = mddev; |
| 1353 | r10_bio->sector = bio->bi_sector + sectors_handled; |
| 1354 | r10_bio->state = 0; |
| 1355 | goto retry_write; |
| 1356 | } |
NeilBrown | 079fa16 | 2011-09-10 17:21:23 +1000 | [diff] [blame] | 1357 | one_write_done(r10_bio); |
| 1358 | |
| 1359 | /* In case raid10d snuck in to freeze_array */ |
| 1360 | wake_up(&conf->wait_barrier); |
NeilBrown | d4432c2 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1361 | |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1362 | if (do_sync || !mddev->bitmap || !plugged) |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 1363 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | } |
| 1365 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1366 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1367 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1368 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1369 | int i; |
| 1370 | |
| 1371 | if (conf->near_copies < conf->raid_disks) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1372 | seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | if (conf->near_copies > 1) |
| 1374 | seq_printf(seq, " %d near-copies", conf->near_copies); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1375 | if (conf->far_copies > 1) { |
| 1376 | if (conf->far_offset) |
| 1377 | seq_printf(seq, " %d offset-copies", conf->far_copies); |
| 1378 | else |
| 1379 | seq_printf(seq, " %d far-copies", conf->far_copies); |
| 1380 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1381 | seq_printf(seq, " [%d/%d] [", conf->raid_disks, |
NeilBrown | 76186dd | 2006-10-03 01:15:48 -0700 | [diff] [blame] | 1382 | conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1383 | for (i = 0; i < conf->raid_disks; i++) |
| 1384 | seq_printf(seq, "%s", |
| 1385 | conf->mirrors[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1386 | test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1387 | seq_printf(seq, "]"); |
| 1388 | } |
| 1389 | |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1390 | /* check if there are enough drives for |
| 1391 | * every block to appear on atleast one. |
| 1392 | * Don't consider the device numbered 'ignore' |
| 1393 | * as we might be about to remove it. |
| 1394 | */ |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1395 | static int enough(struct r10conf *conf, int ignore) |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1396 | { |
| 1397 | int first = 0; |
| 1398 | |
| 1399 | do { |
| 1400 | int n = conf->copies; |
| 1401 | int cnt = 0; |
| 1402 | while (n--) { |
| 1403 | if (conf->mirrors[first].rdev && |
| 1404 | first != ignore) |
| 1405 | cnt++; |
| 1406 | first = (first+1) % conf->raid_disks; |
| 1407 | } |
| 1408 | if (cnt == 0) |
| 1409 | return 0; |
| 1410 | } while (first != 0); |
| 1411 | return 1; |
| 1412 | } |
| 1413 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1414 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1415 | { |
| 1416 | char b[BDEVNAME_SIZE]; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1417 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | |
| 1419 | /* |
| 1420 | * If it is not operational, then we have already marked it as dead |
| 1421 | * else if it is the last working disks, ignore the error, let the |
| 1422 | * next level up know. |
| 1423 | * else mark the drive as failed |
| 1424 | */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1425 | if (test_bit(In_sync, &rdev->flags) |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1426 | && !enough(conf, rdev->raid_disk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1427 | /* |
| 1428 | * Don't fail the drive, just return an IO error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1429 | */ |
| 1430 | return; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1431 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1432 | unsigned long flags; |
| 1433 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1434 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1435 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1436 | /* |
| 1437 | * if recovery is running, make sure it aborts. |
| 1438 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1439 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1440 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1441 | set_bit(Blocked, &rdev->flags); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1442 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1443 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Joe Perches | 067032b | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1444 | printk(KERN_ALERT |
| 1445 | "md/raid10:%s: Disk failure on %s, disabling device.\n" |
| 1446 | "md/raid10:%s: Operation continuing on %d devices.\n", |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1447 | mdname(mddev), bdevname(rdev->bdev, b), |
| 1448 | mdname(mddev), conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1449 | } |
| 1450 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1451 | static void print_conf(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1452 | { |
| 1453 | int i; |
NeilBrown | 0f6d02d | 2011-10-11 16:48:46 +1100 | [diff] [blame] | 1454 | struct mirror_info *tmp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1455 | |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1456 | printk(KERN_DEBUG "RAID10 conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1457 | if (!conf) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1458 | printk(KERN_DEBUG "(!conf)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1459 | return; |
| 1460 | } |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1461 | printk(KERN_DEBUG " --- wd:%d rd:%d\n", conf->raid_disks - conf->mddev->degraded, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1462 | conf->raid_disks); |
| 1463 | |
| 1464 | for (i = 0; i < conf->raid_disks; i++) { |
| 1465 | char b[BDEVNAME_SIZE]; |
| 1466 | tmp = conf->mirrors + i; |
| 1467 | if (tmp->rdev) |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1468 | printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n", |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1469 | i, !test_bit(In_sync, &tmp->rdev->flags), |
| 1470 | !test_bit(Faulty, &tmp->rdev->flags), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1471 | bdevname(tmp->rdev->bdev,b)); |
| 1472 | } |
| 1473 | } |
| 1474 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1475 | static void close_sync(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | { |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1477 | wait_barrier(conf); |
| 1478 | allow_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1479 | |
| 1480 | mempool_destroy(conf->r10buf_pool); |
| 1481 | conf->r10buf_pool = NULL; |
| 1482 | } |
| 1483 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1484 | static int raid10_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | { |
| 1486 | int i; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1487 | struct r10conf *conf = mddev->private; |
NeilBrown | 0f6d02d | 2011-10-11 16:48:46 +1100 | [diff] [blame] | 1488 | struct mirror_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1489 | int count = 0; |
| 1490 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1491 | |
| 1492 | /* |
| 1493 | * Find all non-in_sync disks within the RAID10 configuration |
| 1494 | * and mark them in_sync |
| 1495 | */ |
| 1496 | for (i = 0; i < conf->raid_disks; i++) { |
| 1497 | tmp = conf->mirrors + i; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1498 | if (tmp->replacement |
| 1499 | && tmp->replacement->recovery_offset == MaxSector |
| 1500 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 1501 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 1502 | /* Replacement has just become active */ |
| 1503 | if (!tmp->rdev |
| 1504 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 1505 | count++; |
| 1506 | if (tmp->rdev) { |
| 1507 | /* Replaced device not technically faulty, |
| 1508 | * but we need to be sure it gets removed |
| 1509 | * and never re-added. |
| 1510 | */ |
| 1511 | set_bit(Faulty, &tmp->rdev->flags); |
| 1512 | sysfs_notify_dirent_safe( |
| 1513 | tmp->rdev->sysfs_state); |
| 1514 | } |
| 1515 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 1516 | } else if (tmp->rdev |
| 1517 | && !test_bit(Faulty, &tmp->rdev->flags) |
| 1518 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1519 | count++; |
Adrian Drzewiecki | e6ffbcb | 2010-08-18 11:49:02 +1000 | [diff] [blame] | 1520 | sysfs_notify_dirent(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | } |
| 1522 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1523 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1524 | mddev->degraded -= count; |
| 1525 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1526 | |
| 1527 | print_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1528 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1529 | } |
| 1530 | |
| 1531 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1532 | static int raid10_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1533 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1534 | struct r10conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1535 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1536 | int mirror; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1537 | int first = 0; |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 1538 | int last = conf->raid_disks - 1; |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1539 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1540 | |
| 1541 | if (mddev->recovery_cp < MaxSector) |
| 1542 | /* only hot-add to in-sync arrays, as recovery is |
| 1543 | * very different from resync |
| 1544 | */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1545 | return -EBUSY; |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 1546 | if (rdev->saved_raid_disk < 0 && !enough(conf, -1)) |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1547 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | |
NeilBrown | a53a6c8 | 2008-11-06 17:28:20 +1100 | [diff] [blame] | 1549 | if (rdev->raid_disk >= 0) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1550 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1551 | |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1552 | if (q->merge_bvec_fn) { |
| 1553 | set_bit(Unmerged, &rdev->flags); |
| 1554 | mddev->merge_check_needed = 1; |
| 1555 | } |
| 1556 | |
Namhyung Kim | 2c4193d | 2011-07-18 17:38:43 +1000 | [diff] [blame] | 1557 | if (rdev->saved_raid_disk >= first && |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1558 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) |
| 1559 | mirror = rdev->saved_raid_disk; |
| 1560 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1561 | mirror = first; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1562 | for ( ; mirror <= last ; mirror++) { |
NeilBrown | 0f6d02d | 2011-10-11 16:48:46 +1100 | [diff] [blame] | 1563 | struct mirror_info *p = &conf->mirrors[mirror]; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1564 | if (p->recovery_disabled == mddev->recovery_disabled) |
| 1565 | continue; |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1566 | if (p->rdev) { |
| 1567 | if (!test_bit(WantReplacement, &p->rdev->flags) || |
| 1568 | p->replacement != NULL) |
| 1569 | continue; |
| 1570 | clear_bit(In_sync, &rdev->flags); |
| 1571 | set_bit(Replacement, &rdev->flags); |
| 1572 | rdev->raid_disk = mirror; |
| 1573 | err = 0; |
| 1574 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 1575 | rdev->data_offset << 9); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1576 | conf->fullsync = 1; |
| 1577 | rcu_assign_pointer(p->replacement, rdev); |
| 1578 | break; |
| 1579 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1580 | |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1581 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 1582 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1583 | |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1584 | p->head_position = 0; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 1585 | p->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1586 | rdev->raid_disk = mirror; |
| 1587 | err = 0; |
| 1588 | if (rdev->saved_raid_disk != mirror) |
| 1589 | conf->fullsync = 1; |
| 1590 | rcu_assign_pointer(p->rdev, rdev); |
| 1591 | break; |
| 1592 | } |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 1593 | if (err == 0 && test_bit(Unmerged, &rdev->flags)) { |
| 1594 | /* Some requests might not have seen this new |
| 1595 | * merge_bvec_fn. We must wait for them to complete |
| 1596 | * before merging the device fully. |
| 1597 | * First we make sure any code which has tested |
| 1598 | * our function has submitted the request, then |
| 1599 | * we wait for all outstanding requests to complete. |
| 1600 | */ |
| 1601 | synchronize_sched(); |
| 1602 | raise_barrier(conf, 0); |
| 1603 | lower_barrier(conf); |
| 1604 | clear_bit(Unmerged, &rdev->flags); |
| 1605 | } |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1606 | md_integrity_add_rdev(rdev, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1607 | print_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1608 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1609 | } |
| 1610 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1611 | static int raid10_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1612 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1613 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1614 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 1615 | int number = rdev->raid_disk; |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1616 | struct md_rdev **rdevp; |
| 1617 | struct mirror_info *p = conf->mirrors + number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1618 | |
| 1619 | print_conf(conf); |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1620 | if (rdev == p->rdev) |
| 1621 | rdevp = &p->rdev; |
| 1622 | else if (rdev == p->replacement) |
| 1623 | rdevp = &p->replacement; |
| 1624 | else |
| 1625 | return 0; |
| 1626 | |
| 1627 | if (test_bit(In_sync, &rdev->flags) || |
| 1628 | atomic_read(&rdev->nr_pending)) { |
| 1629 | err = -EBUSY; |
| 1630 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1631 | } |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1632 | /* Only remove faulty devices if recovery |
| 1633 | * is not possible. |
| 1634 | */ |
| 1635 | if (!test_bit(Faulty, &rdev->flags) && |
| 1636 | mddev->recovery_disabled != p->recovery_disabled && |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1637 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1638 | enough(conf, -1)) { |
| 1639 | err = -EBUSY; |
| 1640 | goto abort; |
| 1641 | } |
| 1642 | *rdevp = NULL; |
| 1643 | synchronize_rcu(); |
| 1644 | if (atomic_read(&rdev->nr_pending)) { |
| 1645 | /* lost the race, try later */ |
| 1646 | err = -EBUSY; |
| 1647 | *rdevp = rdev; |
| 1648 | goto abort; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1649 | } else if (p->replacement) { |
| 1650 | /* We must have just cleared 'rdev' */ |
| 1651 | p->rdev = p->replacement; |
| 1652 | clear_bit(Replacement, &p->replacement->flags); |
| 1653 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 1654 | * but will never see neither -- if they are careful. |
| 1655 | */ |
| 1656 | p->replacement = NULL; |
| 1657 | clear_bit(WantReplacement, &rdev->flags); |
| 1658 | } else |
| 1659 | /* We might have just remove the Replacement as faulty |
| 1660 | * Clear the flag just in case |
| 1661 | */ |
| 1662 | clear_bit(WantReplacement, &rdev->flags); |
| 1663 | |
NeilBrown | c8ab903 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1664 | err = md_integrity_register(mddev); |
| 1665 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1666 | abort: |
| 1667 | |
| 1668 | print_conf(conf); |
| 1669 | return err; |
| 1670 | } |
| 1671 | |
| 1672 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1673 | static void end_sync_read(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1674 | { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1675 | struct r10bio *r10_bio = bio->bi_private; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1676 | struct r10conf *conf = r10_bio->mddev->private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 1677 | int d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1678 | |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1679 | d = find_bio_disk(conf, r10_bio, bio, NULL, NULL); |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1680 | |
| 1681 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 1682 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
NeilBrown | e684e41 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1683 | else |
| 1684 | /* The write handler will notice the lack of |
| 1685 | * R10BIO_Uptodate and record any errors etc |
| 1686 | */ |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1687 | atomic_add(r10_bio->sectors, |
| 1688 | &conf->mirrors[d].rdev->corrected_errors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1689 | |
| 1690 | /* for reconstruct, we always reschedule after a read. |
| 1691 | * for resync, only after all reads |
| 1692 | */ |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1693 | rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1694 | if (test_bit(R10BIO_IsRecover, &r10_bio->state) || |
| 1695 | atomic_dec_and_test(&r10_bio->remaining)) { |
| 1696 | /* we have read all the blocks, |
| 1697 | * do the comparison in process context in raid10d |
| 1698 | */ |
| 1699 | reschedule_retry(r10_bio); |
| 1700 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | } |
| 1702 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1703 | static void end_sync_request(struct r10bio *r10_bio) |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1704 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1705 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1706 | |
| 1707 | while (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1708 | if (r10_bio->master_bio == NULL) { |
| 1709 | /* the primary of several recovery bios */ |
| 1710 | sector_t s = r10_bio->sectors; |
| 1711 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 1712 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 1713 | reschedule_retry(r10_bio); |
| 1714 | else |
| 1715 | put_buf(r10_bio); |
| 1716 | md_done_sync(mddev, s, 1); |
| 1717 | break; |
| 1718 | } else { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1719 | struct r10bio *r10_bio2 = (struct r10bio *)r10_bio->master_bio; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1720 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 1721 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
| 1722 | reschedule_retry(r10_bio); |
| 1723 | else |
| 1724 | put_buf(r10_bio); |
| 1725 | r10_bio = r10_bio2; |
| 1726 | } |
| 1727 | } |
| 1728 | } |
| 1729 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1730 | static void end_sync_write(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | { |
| 1732 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1733 | struct r10bio *r10_bio = bio->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1734 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1735 | struct r10conf *conf = mddev->private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 1736 | int d; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1737 | sector_t first_bad; |
| 1738 | int bad_sectors; |
| 1739 | int slot; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1740 | int repl; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1741 | struct md_rdev *rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1742 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1743 | d = find_bio_disk(conf, r10_bio, bio, &slot, &repl); |
| 1744 | if (repl) |
| 1745 | rdev = conf->mirrors[d].replacement; |
NeilBrown | 547414d | 2012-03-13 11:21:20 +1100 | [diff] [blame] | 1746 | else |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1747 | rdev = conf->mirrors[d].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1749 | if (!uptodate) { |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1750 | if (repl) |
| 1751 | md_error(mddev, rdev); |
| 1752 | else { |
| 1753 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1754 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1755 | set_bit(MD_RECOVERY_NEEDED, |
| 1756 | &rdev->mddev->recovery); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1757 | set_bit(R10BIO_WriteError, &r10_bio->state); |
| 1758 | } |
| 1759 | } else if (is_badblock(rdev, |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 1760 | r10_bio->devs[slot].addr, |
| 1761 | r10_bio->sectors, |
| 1762 | &first_bad, &bad_sectors)) |
| 1763 | set_bit(R10BIO_MadeGood, &r10_bio->state); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1764 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1765 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1766 | |
| 1767 | end_sync_request(r10_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1768 | } |
| 1769 | |
| 1770 | /* |
| 1771 | * Note: sync and recover and handled very differently for raid10 |
| 1772 | * This code is for resync. |
| 1773 | * For resync, we read through virtual addresses and read all blocks. |
| 1774 | * If there is any error, we schedule a write. The lowest numbered |
| 1775 | * drive is authoritative. |
| 1776 | * However requests come for physical address, so we need to map. |
| 1777 | * For every physical address there are raid_disks/copies virtual addresses, |
| 1778 | * which is always are least one, but is not necessarly an integer. |
| 1779 | * This means that a physical address can span multiple chunks, so we may |
| 1780 | * have to submit multiple io requests for a single sync request. |
| 1781 | */ |
| 1782 | /* |
| 1783 | * We check if all blocks are in-sync and only write to blocks that |
| 1784 | * aren't in sync |
| 1785 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1786 | static void sync_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1787 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1788 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | int i, first; |
| 1790 | struct bio *tbio, *fbio; |
| 1791 | |
| 1792 | atomic_set(&r10_bio->remaining, 1); |
| 1793 | |
| 1794 | /* find the first device with a block */ |
| 1795 | for (i=0; i<conf->copies; i++) |
| 1796 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) |
| 1797 | break; |
| 1798 | |
| 1799 | if (i == conf->copies) |
| 1800 | goto done; |
| 1801 | |
| 1802 | first = i; |
| 1803 | fbio = r10_bio->devs[i].bio; |
| 1804 | |
| 1805 | /* now find blocks with errors */ |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1806 | for (i=0 ; i < conf->copies ; i++) { |
| 1807 | int j, d; |
| 1808 | int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1810 | tbio = r10_bio->devs[i].bio; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1811 | |
| 1812 | if (tbio->bi_end_io != end_sync_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1813 | continue; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1814 | if (i == first) |
| 1815 | continue; |
| 1816 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) { |
| 1817 | /* We know that the bi_io_vec layout is the same for |
| 1818 | * both 'first' and 'i', so we just compare them. |
| 1819 | * All vec entries are PAGE_SIZE; |
| 1820 | */ |
| 1821 | for (j = 0; j < vcnt; j++) |
| 1822 | if (memcmp(page_address(fbio->bi_io_vec[j].bv_page), |
| 1823 | page_address(tbio->bi_io_vec[j].bv_page), |
| 1824 | PAGE_SIZE)) |
| 1825 | break; |
| 1826 | if (j == vcnt) |
| 1827 | continue; |
| 1828 | mddev->resync_mismatches += r10_bio->sectors; |
NeilBrown | f84ee36 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1829 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 1830 | /* Don't fix anything. */ |
| 1831 | continue; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1832 | } |
NeilBrown | f84ee36 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1833 | /* Ok, we need to write this bio, either to correct an |
| 1834 | * inconsistency or to correct an unreadable block. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1835 | * First we need to fixup bv_offset, bv_len and |
| 1836 | * bi_vecs, as the read request might have corrupted these |
| 1837 | */ |
| 1838 | tbio->bi_vcnt = vcnt; |
| 1839 | tbio->bi_size = r10_bio->sectors << 9; |
| 1840 | tbio->bi_idx = 0; |
| 1841 | tbio->bi_phys_segments = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1842 | tbio->bi_flags &= ~(BIO_POOL_MASK - 1); |
| 1843 | tbio->bi_flags |= 1 << BIO_UPTODATE; |
| 1844 | tbio->bi_next = NULL; |
| 1845 | tbio->bi_rw = WRITE; |
| 1846 | tbio->bi_private = r10_bio; |
| 1847 | tbio->bi_sector = r10_bio->devs[i].addr; |
| 1848 | |
| 1849 | for (j=0; j < vcnt ; j++) { |
| 1850 | tbio->bi_io_vec[j].bv_offset = 0; |
| 1851 | tbio->bi_io_vec[j].bv_len = PAGE_SIZE; |
| 1852 | |
| 1853 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), |
| 1854 | page_address(fbio->bi_io_vec[j].bv_page), |
| 1855 | PAGE_SIZE); |
| 1856 | } |
| 1857 | tbio->bi_end_io = end_sync_write; |
| 1858 | |
| 1859 | d = r10_bio->devs[i].devnum; |
| 1860 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 1861 | atomic_inc(&r10_bio->remaining); |
| 1862 | md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9); |
| 1863 | |
| 1864 | tbio->bi_sector += conf->mirrors[d].rdev->data_offset; |
| 1865 | tbio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 1866 | generic_make_request(tbio); |
| 1867 | } |
| 1868 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1869 | /* Now write out to any replacement devices |
| 1870 | * that are active |
| 1871 | */ |
| 1872 | for (i = 0; i < conf->copies; i++) { |
| 1873 | int j, d; |
| 1874 | int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9); |
| 1875 | |
| 1876 | tbio = r10_bio->devs[i].repl_bio; |
| 1877 | if (!tbio || !tbio->bi_end_io) |
| 1878 | continue; |
| 1879 | if (r10_bio->devs[i].bio->bi_end_io != end_sync_write |
| 1880 | && r10_bio->devs[i].bio != fbio) |
| 1881 | for (j = 0; j < vcnt; j++) |
| 1882 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), |
| 1883 | page_address(fbio->bi_io_vec[j].bv_page), |
| 1884 | PAGE_SIZE); |
| 1885 | d = r10_bio->devs[i].devnum; |
| 1886 | atomic_inc(&r10_bio->remaining); |
| 1887 | md_sync_acct(conf->mirrors[d].replacement->bdev, |
| 1888 | tbio->bi_size >> 9); |
| 1889 | generic_make_request(tbio); |
| 1890 | } |
| 1891 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | done: |
| 1893 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1894 | md_done_sync(mddev, r10_bio->sectors, 1); |
| 1895 | put_buf(r10_bio); |
| 1896 | } |
| 1897 | } |
| 1898 | |
| 1899 | /* |
| 1900 | * Now for the recovery code. |
| 1901 | * Recovery happens across physical sectors. |
| 1902 | * We recover all non-is_sync drives by finding the virtual address of |
| 1903 | * each, and then choose a working drive that also has that virt address. |
| 1904 | * There is a separate r10_bio for each non-in_sync drive. |
| 1905 | * Only the first two slots are in use. The first for reading, |
| 1906 | * The second for writing. |
| 1907 | * |
| 1908 | */ |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1909 | static void fix_recovery_read_error(struct r10bio *r10_bio) |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1910 | { |
| 1911 | /* We got a read error during recovery. |
| 1912 | * We repeat the read in smaller page-sized sections. |
| 1913 | * If a read succeeds, write it to the new device or record |
| 1914 | * a bad block if we cannot. |
| 1915 | * If a read fails, record a bad block on both old and |
| 1916 | * new devices. |
| 1917 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1918 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1919 | struct r10conf *conf = mddev->private; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1920 | struct bio *bio = r10_bio->devs[0].bio; |
| 1921 | sector_t sect = 0; |
| 1922 | int sectors = r10_bio->sectors; |
| 1923 | int idx = 0; |
| 1924 | int dr = r10_bio->devs[0].devnum; |
| 1925 | int dw = r10_bio->devs[1].devnum; |
| 1926 | |
| 1927 | while (sectors) { |
| 1928 | int s = sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1929 | struct md_rdev *rdev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1930 | sector_t addr; |
| 1931 | int ok; |
| 1932 | |
| 1933 | if (s > (PAGE_SIZE>>9)) |
| 1934 | s = PAGE_SIZE >> 9; |
| 1935 | |
| 1936 | rdev = conf->mirrors[dr].rdev; |
| 1937 | addr = r10_bio->devs[0].addr + sect, |
| 1938 | ok = sync_page_io(rdev, |
| 1939 | addr, |
| 1940 | s << 9, |
| 1941 | bio->bi_io_vec[idx].bv_page, |
| 1942 | READ, false); |
| 1943 | if (ok) { |
| 1944 | rdev = conf->mirrors[dw].rdev; |
| 1945 | addr = r10_bio->devs[1].addr + sect; |
| 1946 | ok = sync_page_io(rdev, |
| 1947 | addr, |
| 1948 | s << 9, |
| 1949 | bio->bi_io_vec[idx].bv_page, |
| 1950 | WRITE, false); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1951 | if (!ok) { |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1952 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 1953 | if (!test_and_set_bit(WantReplacement, |
| 1954 | &rdev->flags)) |
| 1955 | set_bit(MD_RECOVERY_NEEDED, |
| 1956 | &rdev->mddev->recovery); |
| 1957 | } |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1958 | } |
| 1959 | if (!ok) { |
| 1960 | /* We don't worry if we cannot set a bad block - |
| 1961 | * it really is bad so there is no loss in not |
| 1962 | * recording it yet |
| 1963 | */ |
| 1964 | rdev_set_badblocks(rdev, addr, s, 0); |
| 1965 | |
| 1966 | if (rdev != conf->mirrors[dw].rdev) { |
| 1967 | /* need bad block on destination too */ |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 1968 | struct md_rdev *rdev2 = conf->mirrors[dw].rdev; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1969 | addr = r10_bio->devs[1].addr + sect; |
| 1970 | ok = rdev_set_badblocks(rdev2, addr, s, 0); |
| 1971 | if (!ok) { |
| 1972 | /* just abort the recovery */ |
| 1973 | printk(KERN_NOTICE |
| 1974 | "md/raid10:%s: recovery aborted" |
| 1975 | " due to read error\n", |
| 1976 | mdname(mddev)); |
| 1977 | |
| 1978 | conf->mirrors[dw].recovery_disabled |
| 1979 | = mddev->recovery_disabled; |
| 1980 | set_bit(MD_RECOVERY_INTR, |
| 1981 | &mddev->recovery); |
| 1982 | break; |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | sectors -= s; |
| 1988 | sect += s; |
| 1989 | idx++; |
| 1990 | } |
| 1991 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 1993 | static void recovery_request_write(struct mddev *mddev, struct r10bio *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 1995 | struct r10conf *conf = mddev->private; |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 1996 | int d; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 1997 | struct bio *wbio, *wbio2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1998 | |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 1999 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) { |
| 2000 | fix_recovery_read_error(r10_bio); |
| 2001 | end_sync_request(r10_bio); |
| 2002 | return; |
| 2003 | } |
| 2004 | |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 2005 | /* |
| 2006 | * share the pages with the first bio |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | * and submit the write request |
| 2008 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2009 | d = r10_bio->devs[1].devnum; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2010 | wbio = r10_bio->devs[1].bio; |
| 2011 | wbio2 = r10_bio->devs[1].repl_bio; |
| 2012 | if (wbio->bi_end_io) { |
| 2013 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 2014 | md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9); |
| 2015 | generic_make_request(wbio); |
| 2016 | } |
| 2017 | if (wbio2 && wbio2->bi_end_io) { |
| 2018 | atomic_inc(&conf->mirrors[d].replacement->nr_pending); |
| 2019 | md_sync_acct(conf->mirrors[d].replacement->bdev, |
| 2020 | wbio2->bi_size >> 9); |
| 2021 | generic_make_request(wbio2); |
| 2022 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | |
| 2026 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2027 | * Used by fix_read_error() to decay the per rdev read_errors. |
| 2028 | * We halve the read error count for every hour that has elapsed |
| 2029 | * since the last recorded read error. |
| 2030 | * |
| 2031 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2032 | 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] | 2033 | { |
| 2034 | struct timespec cur_time_mon; |
| 2035 | unsigned long hours_since_last; |
| 2036 | unsigned int read_errors = atomic_read(&rdev->read_errors); |
| 2037 | |
| 2038 | ktime_get_ts(&cur_time_mon); |
| 2039 | |
| 2040 | if (rdev->last_read_error.tv_sec == 0 && |
| 2041 | rdev->last_read_error.tv_nsec == 0) { |
| 2042 | /* first time we've seen a read error */ |
| 2043 | rdev->last_read_error = cur_time_mon; |
| 2044 | return; |
| 2045 | } |
| 2046 | |
| 2047 | hours_since_last = (cur_time_mon.tv_sec - |
| 2048 | rdev->last_read_error.tv_sec) / 3600; |
| 2049 | |
| 2050 | rdev->last_read_error = cur_time_mon; |
| 2051 | |
| 2052 | /* |
| 2053 | * if hours_since_last is > the number of bits in read_errors |
| 2054 | * just set read errors to 0. We do this to avoid |
| 2055 | * overflowing the shift of read_errors by hours_since_last. |
| 2056 | */ |
| 2057 | if (hours_since_last >= 8 * sizeof(read_errors)) |
| 2058 | atomic_set(&rdev->read_errors, 0); |
| 2059 | else |
| 2060 | atomic_set(&rdev->read_errors, read_errors >> hours_since_last); |
| 2061 | } |
| 2062 | |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2063 | static int r10_sync_page_io(struct md_rdev *rdev, sector_t sector, |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2064 | int sectors, struct page *page, int rw) |
| 2065 | { |
| 2066 | sector_t first_bad; |
| 2067 | int bad_sectors; |
| 2068 | |
| 2069 | if (is_badblock(rdev, sector, sectors, &first_bad, &bad_sectors) |
| 2070 | && (rw == READ || test_bit(WriteErrorSeen, &rdev->flags))) |
| 2071 | return -1; |
| 2072 | if (sync_page_io(rdev, sector, sectors << 9, page, rw, false)) |
| 2073 | /* success */ |
| 2074 | return 1; |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2075 | if (rw == WRITE) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2076 | set_bit(WriteErrorSeen, &rdev->flags); |
NeilBrown | b7044d4 | 2011-12-23 10:17:56 +1100 | [diff] [blame] | 2077 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2078 | set_bit(MD_RECOVERY_NEEDED, |
| 2079 | &rdev->mddev->recovery); |
| 2080 | } |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2081 | /* need to record an error - either for the block or the device */ |
| 2082 | if (!rdev_set_badblocks(rdev, sector, sectors, 0)) |
| 2083 | md_error(rdev->mddev, rdev); |
| 2084 | return 0; |
| 2085 | } |
| 2086 | |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2087 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2088 | * This is a kernel thread which: |
| 2089 | * |
| 2090 | * 1. Retries failed read operations on working mirrors. |
| 2091 | * 2. Updates the raid superblock when problems encounter. |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2092 | * 3. Performs writes following reads for array synchronising. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2093 | */ |
| 2094 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2095 | 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] | 2096 | { |
| 2097 | int sect = 0; /* Offset from r10_bio->sector */ |
| 2098 | int sectors = r10_bio->sectors; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2099 | struct md_rdev*rdev; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2100 | int max_read_errors = atomic_read(&mddev->max_corr_read_errors); |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2101 | int d = r10_bio->devs[r10_bio->read_slot].devnum; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2102 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2103 | /* still own a reference to this rdev, so it cannot |
| 2104 | * have been cleared recently. |
| 2105 | */ |
| 2106 | rdev = conf->mirrors[d].rdev; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2107 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2108 | if (test_bit(Faulty, &rdev->flags)) |
| 2109 | /* drive has already been failed, just ignore any |
| 2110 | more fix_read_error() attempts */ |
| 2111 | return; |
| 2112 | |
| 2113 | check_decay_read_errors(mddev, rdev); |
| 2114 | atomic_inc(&rdev->read_errors); |
| 2115 | if (atomic_read(&rdev->read_errors) > max_read_errors) { |
| 2116 | char b[BDEVNAME_SIZE]; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2117 | bdevname(rdev->bdev, b); |
| 2118 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2119 | printk(KERN_NOTICE |
| 2120 | "md/raid10:%s: %s: Raid device exceeded " |
| 2121 | "read_error threshold [cur %d:max %d]\n", |
| 2122 | mdname(mddev), b, |
| 2123 | atomic_read(&rdev->read_errors), max_read_errors); |
| 2124 | printk(KERN_NOTICE |
| 2125 | "md/raid10:%s: %s: Failing raid device\n", |
| 2126 | mdname(mddev), b); |
| 2127 | md_error(mddev, conf->mirrors[d].rdev); |
NeilBrown | fae8cc5 | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2128 | r10_bio->devs[r10_bio->read_slot].bio = IO_BLOCKED; |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 2129 | return; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2130 | } |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2131 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2132 | while(sectors) { |
| 2133 | int s = sectors; |
| 2134 | int sl = r10_bio->read_slot; |
| 2135 | int success = 0; |
| 2136 | int start; |
| 2137 | |
| 2138 | if (s > (PAGE_SIZE>>9)) |
| 2139 | s = PAGE_SIZE >> 9; |
| 2140 | |
| 2141 | rcu_read_lock(); |
| 2142 | do { |
NeilBrown | 8dbed5c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2143 | sector_t first_bad; |
| 2144 | int bad_sectors; |
| 2145 | |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2146 | d = r10_bio->devs[sl].devnum; |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2147 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 2148 | if (rdev && |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2149 | !test_bit(Unmerged, &rdev->flags) && |
NeilBrown | 8dbed5c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2150 | test_bit(In_sync, &rdev->flags) && |
| 2151 | is_badblock(rdev, r10_bio->devs[sl].addr + sect, s, |
| 2152 | &first_bad, &bad_sectors) == 0) { |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2153 | atomic_inc(&rdev->nr_pending); |
| 2154 | rcu_read_unlock(); |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 2155 | success = sync_page_io(rdev, |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2156 | r10_bio->devs[sl].addr + |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 2157 | sect, |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2158 | s<<9, |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 2159 | conf->tmppage, READ, false); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2160 | rdev_dec_pending(rdev, mddev); |
| 2161 | rcu_read_lock(); |
| 2162 | if (success) |
| 2163 | break; |
| 2164 | } |
| 2165 | sl++; |
| 2166 | if (sl == conf->copies) |
| 2167 | sl = 0; |
| 2168 | } while (!success && sl != r10_bio->read_slot); |
| 2169 | rcu_read_unlock(); |
| 2170 | |
| 2171 | if (!success) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2172 | /* Cannot read from anywhere, just mark the block |
| 2173 | * as bad on the first device to discourage future |
| 2174 | * reads. |
| 2175 | */ |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2176 | int dn = r10_bio->devs[r10_bio->read_slot].devnum; |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2177 | rdev = conf->mirrors[dn].rdev; |
| 2178 | |
| 2179 | if (!rdev_set_badblocks( |
| 2180 | rdev, |
| 2181 | r10_bio->devs[r10_bio->read_slot].addr |
| 2182 | + sect, |
NeilBrown | fae8cc5 | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2183 | s, 0)) { |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2184 | md_error(mddev, rdev); |
NeilBrown | fae8cc5 | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2185 | r10_bio->devs[r10_bio->read_slot].bio |
| 2186 | = IO_BLOCKED; |
| 2187 | } |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | start = sl; |
| 2192 | /* write it back and re-read */ |
| 2193 | rcu_read_lock(); |
| 2194 | while (sl != r10_bio->read_slot) { |
Robert Becker | 67b8dc4 | 2009-12-14 12:49:57 +1100 | [diff] [blame] | 2195 | char b[BDEVNAME_SIZE]; |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2196 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2197 | if (sl==0) |
| 2198 | sl = conf->copies; |
| 2199 | sl--; |
| 2200 | d = r10_bio->devs[sl].devnum; |
| 2201 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2202 | if (!rdev || |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 2203 | test_bit(Unmerged, &rdev->flags) || |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2204 | !test_bit(In_sync, &rdev->flags)) |
| 2205 | continue; |
| 2206 | |
| 2207 | atomic_inc(&rdev->nr_pending); |
| 2208 | rcu_read_unlock(); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2209 | if (r10_sync_page_io(rdev, |
| 2210 | r10_bio->devs[sl].addr + |
| 2211 | sect, |
| 2212 | s<<9, conf->tmppage, WRITE) |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2213 | == 0) { |
| 2214 | /* Well, this device is dead */ |
| 2215 | printk(KERN_NOTICE |
| 2216 | "md/raid10:%s: read correction " |
| 2217 | "write failed" |
| 2218 | " (%d sectors at %llu on %s)\n", |
| 2219 | mdname(mddev), s, |
| 2220 | (unsigned long long)( |
| 2221 | sect + rdev->data_offset), |
| 2222 | bdevname(rdev->bdev, b)); |
| 2223 | printk(KERN_NOTICE "md/raid10:%s: %s: failing " |
| 2224 | "drive\n", |
| 2225 | mdname(mddev), |
| 2226 | bdevname(rdev->bdev, b)); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2227 | } |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2228 | rdev_dec_pending(rdev, mddev); |
| 2229 | rcu_read_lock(); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2230 | } |
| 2231 | sl = start; |
| 2232 | while (sl != r10_bio->read_slot) { |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2233 | char b[BDEVNAME_SIZE]; |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 2234 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2235 | if (sl==0) |
| 2236 | sl = conf->copies; |
| 2237 | sl--; |
| 2238 | d = r10_bio->devs[sl].devnum; |
| 2239 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2240 | if (!rdev || |
| 2241 | !test_bit(In_sync, &rdev->flags)) |
| 2242 | continue; |
Robert Becker | 67b8dc4 | 2009-12-14 12:49:57 +1100 | [diff] [blame] | 2243 | |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2244 | atomic_inc(&rdev->nr_pending); |
| 2245 | rcu_read_unlock(); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2246 | switch (r10_sync_page_io(rdev, |
| 2247 | r10_bio->devs[sl].addr + |
| 2248 | sect, |
| 2249 | s<<9, conf->tmppage, |
| 2250 | READ)) { |
| 2251 | case 0: |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2252 | /* Well, this device is dead */ |
| 2253 | printk(KERN_NOTICE |
| 2254 | "md/raid10:%s: unable to read back " |
| 2255 | "corrected sectors" |
| 2256 | " (%d sectors at %llu on %s)\n", |
| 2257 | mdname(mddev), s, |
| 2258 | (unsigned long long)( |
| 2259 | sect + rdev->data_offset), |
| 2260 | bdevname(rdev->bdev, b)); |
| 2261 | printk(KERN_NOTICE "md/raid10:%s: %s: failing " |
| 2262 | "drive\n", |
| 2263 | mdname(mddev), |
| 2264 | bdevname(rdev->bdev, b)); |
NeilBrown | 58c54fc | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2265 | break; |
| 2266 | case 1: |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2267 | printk(KERN_INFO |
| 2268 | "md/raid10:%s: read error corrected" |
| 2269 | " (%d sectors at %llu on %s)\n", |
| 2270 | mdname(mddev), s, |
| 2271 | (unsigned long long)( |
| 2272 | sect + rdev->data_offset), |
| 2273 | bdevname(rdev->bdev, b)); |
| 2274 | atomic_add(s, &rdev->corrected_errors); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2275 | } |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2276 | |
| 2277 | rdev_dec_pending(rdev, mddev); |
| 2278 | rcu_read_lock(); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 2279 | } |
| 2280 | rcu_read_unlock(); |
| 2281 | |
| 2282 | sectors -= s; |
| 2283 | sect += s; |
| 2284 | } |
| 2285 | } |
| 2286 | |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2287 | static void bi_complete(struct bio *bio, int error) |
| 2288 | { |
| 2289 | complete((struct completion *)bio->bi_private); |
| 2290 | } |
| 2291 | |
| 2292 | static int submit_bio_wait(int rw, struct bio *bio) |
| 2293 | { |
| 2294 | struct completion event; |
| 2295 | rw |= REQ_SYNC; |
| 2296 | |
| 2297 | init_completion(&event); |
| 2298 | bio->bi_private = &event; |
| 2299 | bio->bi_end_io = bi_complete; |
| 2300 | submit_bio(rw, bio); |
| 2301 | wait_for_completion(&event); |
| 2302 | |
| 2303 | return test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 2304 | } |
| 2305 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2306 | static int narrow_write_error(struct r10bio *r10_bio, int i) |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2307 | { |
| 2308 | struct bio *bio = r10_bio->master_bio; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2309 | struct mddev *mddev = r10_bio->mddev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2310 | struct r10conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2311 | struct md_rdev *rdev = conf->mirrors[r10_bio->devs[i].devnum].rdev; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2312 | /* bio has the data to be written to slot 'i' where |
| 2313 | * we just recently had a write error. |
| 2314 | * We repeatedly clone the bio and trim down to one block, |
| 2315 | * then try the write. Where the write fails we record |
| 2316 | * a bad block. |
| 2317 | * It is conceivable that the bio doesn't exactly align with |
| 2318 | * blocks. We must handle this. |
| 2319 | * |
| 2320 | * We currently own a reference to the rdev. |
| 2321 | */ |
| 2322 | |
| 2323 | int block_sectors; |
| 2324 | sector_t sector; |
| 2325 | int sectors; |
| 2326 | int sect_to_write = r10_bio->sectors; |
| 2327 | int ok = 1; |
| 2328 | |
| 2329 | if (rdev->badblocks.shift < 0) |
| 2330 | return 0; |
| 2331 | |
| 2332 | block_sectors = 1 << rdev->badblocks.shift; |
| 2333 | sector = r10_bio->sector; |
| 2334 | sectors = ((r10_bio->sector + block_sectors) |
| 2335 | & ~(sector_t)(block_sectors - 1)) |
| 2336 | - sector; |
| 2337 | |
| 2338 | while (sect_to_write) { |
| 2339 | struct bio *wbio; |
| 2340 | if (sectors > sect_to_write) |
| 2341 | sectors = sect_to_write; |
| 2342 | /* Write at 'sector' for 'sectors' */ |
| 2343 | wbio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
| 2344 | md_trim_bio(wbio, sector - bio->bi_sector, sectors); |
| 2345 | wbio->bi_sector = (r10_bio->devs[i].addr+ |
| 2346 | rdev->data_offset+ |
| 2347 | (sector - r10_bio->sector)); |
| 2348 | wbio->bi_bdev = rdev->bdev; |
| 2349 | if (submit_bio_wait(WRITE, wbio) == 0) |
| 2350 | /* Failure! */ |
| 2351 | ok = rdev_set_badblocks(rdev, sector, |
| 2352 | sectors, 0) |
| 2353 | && ok; |
| 2354 | |
| 2355 | bio_put(wbio); |
| 2356 | sect_to_write -= sectors; |
| 2357 | sector += sectors; |
| 2358 | sectors = block_sectors; |
| 2359 | } |
| 2360 | return ok; |
| 2361 | } |
| 2362 | |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2363 | static void handle_read_error(struct mddev *mddev, struct r10bio *r10_bio) |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2364 | { |
| 2365 | int slot = r10_bio->read_slot; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2366 | struct bio *bio; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2367 | struct r10conf *conf = mddev->private; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2368 | struct md_rdev *rdev = r10_bio->devs[slot].rdev; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2369 | char b[BDEVNAME_SIZE]; |
| 2370 | unsigned long do_sync; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2371 | int max_sectors; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2372 | |
| 2373 | /* we got a read error. Maybe the drive is bad. Maybe just |
| 2374 | * the block and we can fix it. |
| 2375 | * We freeze all other IO, and try reading the block from |
| 2376 | * other devices. When we find one, we re-write |
| 2377 | * and check it that fixes the read error. |
| 2378 | * This is all done synchronously while the array is |
| 2379 | * frozen. |
| 2380 | */ |
NeilBrown | fae8cc5 | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2381 | bio = r10_bio->devs[slot].bio; |
| 2382 | bdevname(bio->bi_bdev, b); |
| 2383 | bio_put(bio); |
| 2384 | r10_bio->devs[slot].bio = NULL; |
| 2385 | |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2386 | if (mddev->ro == 0) { |
| 2387 | freeze_array(conf); |
| 2388 | fix_read_error(conf, mddev, r10_bio); |
| 2389 | unfreeze_array(conf); |
NeilBrown | fae8cc5 | 2012-02-14 11:10:10 +1100 | [diff] [blame] | 2390 | } else |
| 2391 | r10_bio->devs[slot].bio = IO_BLOCKED; |
| 2392 | |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2393 | rdev_dec_pending(rdev, mddev); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2394 | |
NeilBrown | 7399c31 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2395 | read_more: |
NeilBrown | 96c3fd1 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2396 | rdev = read_balance(conf, r10_bio, &max_sectors); |
| 2397 | if (rdev == NULL) { |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2398 | printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O" |
| 2399 | " read error for block %llu\n", |
NeilBrown | 7399c31 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2400 | mdname(mddev), b, |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2401 | (unsigned long long)r10_bio->sector); |
| 2402 | raid_end_bio_io(r10_bio); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2403 | return; |
| 2404 | } |
| 2405 | |
| 2406 | do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2407 | slot = r10_bio->read_slot; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2408 | printk_ratelimited( |
| 2409 | KERN_ERR |
| 2410 | "md/raid10:%s: %s: redirecting" |
| 2411 | "sector %llu to another mirror\n", |
| 2412 | mdname(mddev), |
| 2413 | bdevname(rdev->bdev, b), |
| 2414 | (unsigned long long)r10_bio->sector); |
| 2415 | bio = bio_clone_mddev(r10_bio->master_bio, |
| 2416 | GFP_NOIO, mddev); |
NeilBrown | 7399c31 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2417 | md_trim_bio(bio, |
| 2418 | r10_bio->sector - bio->bi_sector, |
| 2419 | max_sectors); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2420 | r10_bio->devs[slot].bio = bio; |
NeilBrown | abbf098 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2421 | r10_bio->devs[slot].rdev = rdev; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2422 | bio->bi_sector = r10_bio->devs[slot].addr |
| 2423 | + rdev->data_offset; |
| 2424 | bio->bi_bdev = rdev->bdev; |
| 2425 | bio->bi_rw = READ | do_sync; |
| 2426 | bio->bi_private = r10_bio; |
| 2427 | bio->bi_end_io = raid10_end_read_request; |
NeilBrown | 7399c31 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2428 | if (max_sectors < r10_bio->sectors) { |
| 2429 | /* Drat - have to split this up more */ |
| 2430 | struct bio *mbio = r10_bio->master_bio; |
| 2431 | int sectors_handled = |
| 2432 | r10_bio->sector + max_sectors |
| 2433 | - mbio->bi_sector; |
| 2434 | r10_bio->sectors = max_sectors; |
| 2435 | spin_lock_irq(&conf->device_lock); |
| 2436 | if (mbio->bi_phys_segments == 0) |
| 2437 | mbio->bi_phys_segments = 2; |
| 2438 | else |
| 2439 | mbio->bi_phys_segments++; |
| 2440 | spin_unlock_irq(&conf->device_lock); |
| 2441 | generic_make_request(bio); |
NeilBrown | 7399c31 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2442 | |
| 2443 | r10_bio = mempool_alloc(conf->r10bio_pool, |
| 2444 | GFP_NOIO); |
| 2445 | r10_bio->master_bio = mbio; |
| 2446 | r10_bio->sectors = (mbio->bi_size >> 9) |
| 2447 | - sectors_handled; |
| 2448 | r10_bio->state = 0; |
| 2449 | set_bit(R10BIO_ReadError, |
| 2450 | &r10_bio->state); |
| 2451 | r10_bio->mddev = mddev; |
| 2452 | r10_bio->sector = mbio->bi_sector |
| 2453 | + sectors_handled; |
| 2454 | |
| 2455 | goto read_more; |
| 2456 | } else |
| 2457 | generic_make_request(bio); |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2458 | } |
| 2459 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2460 | static void handle_write_completed(struct r10conf *conf, struct r10bio *r10_bio) |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2461 | { |
| 2462 | /* Some sort of write request has finished and it |
| 2463 | * succeeded in writing where we thought there was a |
| 2464 | * bad block. So forget the bad block. |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2465 | * Or possibly if failed and we need to record |
| 2466 | * a bad block. |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2467 | */ |
| 2468 | int m; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2469 | struct md_rdev *rdev; |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2470 | |
| 2471 | if (test_bit(R10BIO_IsSync, &r10_bio->state) || |
| 2472 | test_bit(R10BIO_IsRecover, &r10_bio->state)) { |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2473 | for (m = 0; m < conf->copies; m++) { |
| 2474 | int dev = r10_bio->devs[m].devnum; |
| 2475 | rdev = conf->mirrors[dev].rdev; |
| 2476 | if (r10_bio->devs[m].bio == NULL) |
| 2477 | continue; |
| 2478 | if (test_bit(BIO_UPTODATE, |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2479 | &r10_bio->devs[m].bio->bi_flags)) { |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2480 | rdev_clear_badblocks( |
| 2481 | rdev, |
| 2482 | r10_bio->devs[m].addr, |
| 2483 | r10_bio->sectors); |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2484 | } else { |
| 2485 | if (!rdev_set_badblocks( |
| 2486 | rdev, |
| 2487 | r10_bio->devs[m].addr, |
| 2488 | r10_bio->sectors, 0)) |
| 2489 | md_error(conf->mddev, rdev); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2490 | } |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2491 | rdev = conf->mirrors[dev].replacement; |
| 2492 | if (r10_bio->devs[m].repl_bio == NULL) |
| 2493 | continue; |
| 2494 | if (test_bit(BIO_UPTODATE, |
| 2495 | &r10_bio->devs[m].repl_bio->bi_flags)) { |
| 2496 | rdev_clear_badblocks( |
| 2497 | rdev, |
| 2498 | r10_bio->devs[m].addr, |
| 2499 | r10_bio->sectors); |
| 2500 | } else { |
| 2501 | if (!rdev_set_badblocks( |
| 2502 | rdev, |
| 2503 | r10_bio->devs[m].addr, |
| 2504 | r10_bio->sectors, 0)) |
| 2505 | md_error(conf->mddev, rdev); |
| 2506 | } |
NeilBrown | 1a0b7cd | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2507 | } |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2508 | put_buf(r10_bio); |
| 2509 | } else { |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2510 | for (m = 0; m < conf->copies; m++) { |
| 2511 | int dev = r10_bio->devs[m].devnum; |
| 2512 | struct bio *bio = r10_bio->devs[m].bio; |
| 2513 | rdev = conf->mirrors[dev].rdev; |
| 2514 | if (bio == IO_MADE_GOOD) { |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2515 | rdev_clear_badblocks( |
| 2516 | rdev, |
| 2517 | r10_bio->devs[m].addr, |
| 2518 | r10_bio->sectors); |
| 2519 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2520 | } else if (bio != NULL && |
| 2521 | !test_bit(BIO_UPTODATE, &bio->bi_flags)) { |
| 2522 | if (!narrow_write_error(r10_bio, m)) { |
| 2523 | md_error(conf->mddev, rdev); |
| 2524 | set_bit(R10BIO_Degraded, |
| 2525 | &r10_bio->state); |
| 2526 | } |
| 2527 | rdev_dec_pending(rdev, conf->mddev); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2528 | } |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2529 | bio = r10_bio->devs[m].repl_bio; |
| 2530 | rdev = conf->mirrors[dev].replacement; |
NeilBrown | 4ca40c2 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2531 | if (rdev && bio == IO_MADE_GOOD) { |
NeilBrown | 475b032 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2532 | rdev_clear_badblocks( |
| 2533 | rdev, |
| 2534 | r10_bio->devs[m].addr, |
| 2535 | r10_bio->sectors); |
| 2536 | rdev_dec_pending(rdev, conf->mddev); |
| 2537 | } |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2538 | } |
| 2539 | if (test_bit(R10BIO_WriteError, |
| 2540 | &r10_bio->state)) |
| 2541 | close_write(r10_bio); |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2542 | raid_end_bio_io(r10_bio); |
| 2543 | } |
| 2544 | } |
| 2545 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2546 | static void raid10d(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2547 | { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2548 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2549 | unsigned long flags; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2550 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2551 | struct list_head *head = &conf->retry_list; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2552 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | |
| 2554 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2555 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2556 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2557 | for (;;) { |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2558 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 2559 | flush_pending_writes(conf); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2560 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2561 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2562 | if (list_empty(head)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2563 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | break; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 2565 | } |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2566 | r10_bio = list_entry(head->prev, struct r10bio, retry_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2567 | list_del(head->prev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2568 | conf->nr_queued--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2570 | |
| 2571 | mddev = r10_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2572 | conf = mddev->private; |
NeilBrown | bd870a1 | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2573 | if (test_bit(R10BIO_MadeGood, &r10_bio->state) || |
| 2574 | test_bit(R10BIO_WriteError, &r10_bio->state)) |
NeilBrown | 749c55e | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2575 | handle_write_completed(conf, r10_bio); |
| 2576 | else if (test_bit(R10BIO_IsSync, &r10_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | sync_request_write(mddev, r10_bio); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 2578 | else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | recovery_request_write(mddev, r10_bio); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2580 | else if (test_bit(R10BIO_ReadError, &r10_bio->state)) |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2581 | handle_read_error(mddev, r10_bio); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2582 | else { |
| 2583 | /* just a partial read to be scheduled from a |
| 2584 | * separate context |
| 2585 | */ |
| 2586 | int slot = r10_bio->read_slot; |
| 2587 | generic_make_request(r10_bio->devs[slot].bio); |
| 2588 | } |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2589 | |
NeilBrown | 1d9d524 | 2009-10-16 15:55:32 +1100 | [diff] [blame] | 2590 | cond_resched(); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2591 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
| 2592 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | } |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 2594 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2595 | } |
| 2596 | |
| 2597 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2598 | static int init_resync(struct r10conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2599 | { |
| 2600 | int buffs; |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2601 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | |
| 2603 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; |
Eric Sesterhenn | b638548 | 2006-04-02 13:34:29 +0200 | [diff] [blame] | 2604 | BUG_ON(conf->r10buf_pool); |
NeilBrown | 69335ef | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2605 | conf->have_replacement = 0; |
| 2606 | for (i = 0; i < conf->raid_disks; i++) |
| 2607 | if (conf->mirrors[i].replacement) |
| 2608 | conf->have_replacement = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf); |
| 2610 | if (!conf->r10buf_pool) |
| 2611 | return -ENOMEM; |
| 2612 | conf->next_resync = 0; |
| 2613 | return 0; |
| 2614 | } |
| 2615 | |
| 2616 | /* |
| 2617 | * perform a "sync" on one "block" |
| 2618 | * |
| 2619 | * We need to make sure that no normal I/O request - particularly write |
| 2620 | * requests - conflict with active sync requests. |
| 2621 | * |
| 2622 | * This is achieved by tracking pending requests and a 'barrier' concept |
| 2623 | * that can be installed to exclude normal IO requests. |
| 2624 | * |
| 2625 | * Resync and recovery are handled very differently. |
| 2626 | * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. |
| 2627 | * |
| 2628 | * For resync, we iterate over virtual addresses, read all copies, |
| 2629 | * and update if there are differences. If only one copy is live, |
| 2630 | * skip it. |
| 2631 | * For recovery, we iterate over physical addresses, read a good |
| 2632 | * value for each non-in_sync drive, and over-write. |
| 2633 | * |
| 2634 | * So, for recovery we may have several outstanding complex requests for a |
| 2635 | * given address, one for each out-of-sync device. We model this by allocating |
| 2636 | * a number of r10_bio structures, one for each out-of-sync device. |
| 2637 | * As we setup these structures, we collect all bio's together into a list |
| 2638 | * which we then process collectively to add pages, and then process again |
| 2639 | * to pass to generic_make_request. |
| 2640 | * |
| 2641 | * The r10_bio structures are linked using a borrowed master_bio pointer. |
| 2642 | * This link is counted in ->remaining. When the r10_bio that points to NULL |
| 2643 | * has its remaining count decremented to 0, the whole complex operation |
| 2644 | * is complete. |
| 2645 | * |
| 2646 | */ |
| 2647 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2648 | static sector_t sync_request(struct mddev *mddev, sector_t sector_nr, |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2649 | int *skipped, int go_faster) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2650 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 2651 | struct r10conf *conf = mddev->private; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2652 | struct r10bio *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2653 | struct bio *biolist = NULL, *bio; |
| 2654 | sector_t max_sector, nr_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2655 | int i; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2656 | int max_sync; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 2657 | sector_t sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | sector_t sectors_skipped = 0; |
| 2659 | int chunks_skipped = 0; |
| 2660 | |
| 2661 | if (!conf->r10buf_pool) |
| 2662 | if (init_resync(conf)) |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2663 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | |
| 2665 | skipped: |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2666 | max_sector = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2667 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 2668 | max_sector = mddev->resync_max_sectors; |
| 2669 | if (sector_nr >= max_sector) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2670 | /* If we aborted, we need to abort the |
| 2671 | * sync on the 'current' bitmap chucks (there can |
| 2672 | * be several when recovering multiple devices). |
| 2673 | * as we may have started syncing it but not finished. |
| 2674 | * We can find the current address in |
| 2675 | * mddev->curr_resync, but for recovery, |
| 2676 | * we need to convert that to several |
| 2677 | * virtual addresses. |
| 2678 | */ |
| 2679 | if (mddev->curr_resync < max_sector) { /* aborted */ |
| 2680 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 2681 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 2682 | &sync_blocks, 1); |
| 2683 | else for (i=0; i<conf->raid_disks; i++) { |
| 2684 | sector_t sect = |
| 2685 | raid10_find_virt(conf, mddev->curr_resync, i); |
| 2686 | bitmap_end_sync(mddev->bitmap, sect, |
| 2687 | &sync_blocks, 1); |
| 2688 | } |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2689 | } else { |
| 2690 | /* completed sync */ |
| 2691 | if ((!mddev->bitmap || conf->fullsync) |
| 2692 | && conf->have_replacement |
| 2693 | && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 2694 | /* Completed a full sync so the replacements |
| 2695 | * are now fully recovered. |
| 2696 | */ |
| 2697 | for (i = 0; i < conf->raid_disks; i++) |
| 2698 | if (conf->mirrors[i].replacement) |
| 2699 | conf->mirrors[i].replacement |
| 2700 | ->recovery_offset |
| 2701 | = MaxSector; |
| 2702 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2703 | conf->fullsync = 0; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2704 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2705 | bitmap_close_sync(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2706 | close_sync(conf); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2707 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2708 | return sectors_skipped; |
| 2709 | } |
| 2710 | if (chunks_skipped >= conf->raid_disks) { |
| 2711 | /* if there has been nothing to do on any drive, |
| 2712 | * then there is nothing to do at all.. |
| 2713 | */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2714 | *skipped = 1; |
| 2715 | return (max_sector - sector_nr) + sectors_skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2716 | } |
| 2717 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 2718 | if (max_sector > mddev->resync_max) |
| 2719 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ |
| 2720 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | /* make sure whole request will fit in a chunk - if chunks |
| 2722 | * are meaningful |
| 2723 | */ |
| 2724 | if (conf->near_copies < conf->raid_disks && |
| 2725 | max_sector > (sector_nr | conf->chunk_mask)) |
| 2726 | max_sector = (sector_nr | conf->chunk_mask) + 1; |
| 2727 | /* |
| 2728 | * If there is non-resync activity waiting for us then |
| 2729 | * put in a delay to throttle resync. |
| 2730 | */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 2731 | if (!go_faster && conf->nr_waiting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2732 | msleep_interruptible(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2733 | |
| 2734 | /* Again, very different code for resync and recovery. |
| 2735 | * Both must result in an r10bio with a list of bios that |
| 2736 | * have bi_end_io, bi_sector, bi_bdev set, |
| 2737 | * and bi_private set to the r10bio. |
| 2738 | * For recovery, we may actually create several r10bios |
| 2739 | * with 2 bios in each, that correspond to the bios in the main one. |
| 2740 | * In this case, the subordinate r10bios link back through a |
| 2741 | * borrowed master_bio pointer, and the counter in the master |
| 2742 | * includes a ref from each subordinate. |
| 2743 | */ |
| 2744 | /* First, we decide what to do and set ->bi_end_io |
| 2745 | * To end_sync_read if we want to read, and |
| 2746 | * end_sync_write if we will want to write. |
| 2747 | */ |
| 2748 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2749 | max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2750 | if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 2751 | /* recovery... the complicated one */ |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2752 | int j; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2753 | r10_bio = NULL; |
| 2754 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2755 | for (i=0 ; i<conf->raid_disks; i++) { |
| 2756 | int still_degraded; |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2757 | struct r10bio *rb2; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2758 | sector_t sect; |
| 2759 | int must_sync; |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2760 | int any_working; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2761 | struct mirror_info *mirror = &conf->mirrors[i]; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2762 | |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2763 | if ((mirror->rdev == NULL || |
| 2764 | test_bit(In_sync, &mirror->rdev->flags)) |
| 2765 | && |
| 2766 | (mirror->replacement == NULL || |
| 2767 | test_bit(Faulty, |
| 2768 | &mirror->replacement->flags))) |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2769 | continue; |
| 2770 | |
| 2771 | still_degraded = 0; |
| 2772 | /* want to reconstruct this device */ |
| 2773 | rb2 = r10_bio; |
| 2774 | sect = raid10_find_virt(conf, sector_nr, i); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2775 | /* Unless we are doing a full sync, or a replacement |
| 2776 | * we only need to recover the block if it is set in |
| 2777 | * the bitmap |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2778 | */ |
| 2779 | must_sync = bitmap_start_sync(mddev->bitmap, sect, |
| 2780 | &sync_blocks, 1); |
| 2781 | if (sync_blocks < max_sync) |
| 2782 | max_sync = sync_blocks; |
| 2783 | if (!must_sync && |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2784 | mirror->replacement == NULL && |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2785 | !conf->fullsync) { |
| 2786 | /* yep, skip the sync_blocks here, but don't assume |
| 2787 | * that there will never be anything to do here |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2788 | */ |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2789 | chunks_skipped = -1; |
| 2790 | continue; |
| 2791 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2793 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
| 2794 | raise_barrier(conf, rb2 != NULL); |
| 2795 | atomic_set(&r10_bio->remaining, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2796 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2797 | r10_bio->master_bio = (struct bio*)rb2; |
| 2798 | if (rb2) |
| 2799 | atomic_inc(&rb2->remaining); |
| 2800 | r10_bio->mddev = mddev; |
| 2801 | set_bit(R10BIO_IsRecover, &r10_bio->state); |
| 2802 | r10_bio->sector = sect; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2803 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2804 | raid10_find_phys(conf, r10_bio); |
NeilBrown | 1805556 | 2009-05-07 12:48:10 +1000 | [diff] [blame] | 2805 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2806 | /* Need to check if the array will still be |
| 2807 | * degraded |
| 2808 | */ |
| 2809 | for (j=0; j<conf->raid_disks; j++) |
| 2810 | if (conf->mirrors[j].rdev == NULL || |
| 2811 | test_bit(Faulty, &conf->mirrors[j].rdev->flags)) { |
| 2812 | still_degraded = 1; |
NeilBrown | 87fc767 | 2005-09-09 16:24:04 -0700 | [diff] [blame] | 2813 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2814 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2815 | |
| 2816 | must_sync = bitmap_start_sync(mddev->bitmap, sect, |
| 2817 | &sync_blocks, still_degraded); |
| 2818 | |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2819 | any_working = 0; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2820 | for (j=0; j<conf->copies;j++) { |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2821 | int k; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2822 | int d = r10_bio->devs[j].devnum; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2823 | sector_t from_addr, to_addr; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2824 | struct md_rdev *rdev; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2825 | sector_t sector, first_bad; |
| 2826 | int bad_sectors; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2827 | if (!conf->mirrors[d].rdev || |
| 2828 | !test_bit(In_sync, &conf->mirrors[d].rdev->flags)) |
| 2829 | continue; |
| 2830 | /* This is where we read from */ |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2831 | any_working = 1; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2832 | rdev = conf->mirrors[d].rdev; |
| 2833 | sector = r10_bio->devs[j].addr; |
| 2834 | |
| 2835 | if (is_badblock(rdev, sector, max_sync, |
| 2836 | &first_bad, &bad_sectors)) { |
| 2837 | if (first_bad > sector) |
| 2838 | max_sync = first_bad - sector; |
| 2839 | else { |
| 2840 | bad_sectors -= (sector |
| 2841 | - first_bad); |
| 2842 | if (max_sync > bad_sectors) |
| 2843 | max_sync = bad_sectors; |
| 2844 | continue; |
| 2845 | } |
| 2846 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2847 | bio = r10_bio->devs[0].bio; |
| 2848 | bio->bi_next = biolist; |
| 2849 | biolist = bio; |
| 2850 | bio->bi_private = r10_bio; |
| 2851 | bio->bi_end_io = end_sync_read; |
| 2852 | bio->bi_rw = READ; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2853 | from_addr = r10_bio->devs[j].addr; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2854 | bio->bi_sector = from_addr + rdev->data_offset; |
| 2855 | bio->bi_bdev = rdev->bdev; |
| 2856 | atomic_inc(&rdev->nr_pending); |
| 2857 | /* and we write to 'i' (if not in_sync) */ |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2858 | |
| 2859 | for (k=0; k<conf->copies; k++) |
| 2860 | if (r10_bio->devs[k].devnum == i) |
| 2861 | break; |
| 2862 | BUG_ON(k == conf->copies); |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2863 | to_addr = r10_bio->devs[k].addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2864 | r10_bio->devs[0].devnum = d; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2865 | r10_bio->devs[0].addr = from_addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2866 | r10_bio->devs[1].devnum = i; |
NeilBrown | 5e57028 | 2011-07-28 11:39:25 +1000 | [diff] [blame] | 2867 | r10_bio->devs[1].addr = to_addr; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2868 | |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2869 | rdev = mirror->rdev; |
| 2870 | if (!test_bit(In_sync, &rdev->flags)) { |
| 2871 | bio = r10_bio->devs[1].bio; |
| 2872 | bio->bi_next = biolist; |
| 2873 | biolist = bio; |
| 2874 | bio->bi_private = r10_bio; |
| 2875 | bio->bi_end_io = end_sync_write; |
| 2876 | bio->bi_rw = WRITE; |
| 2877 | bio->bi_sector = to_addr |
| 2878 | + rdev->data_offset; |
| 2879 | bio->bi_bdev = rdev->bdev; |
| 2880 | atomic_inc(&r10_bio->remaining); |
| 2881 | } else |
| 2882 | r10_bio->devs[1].bio->bi_end_io = NULL; |
| 2883 | |
| 2884 | /* and maybe write to replacement */ |
| 2885 | bio = r10_bio->devs[1].repl_bio; |
| 2886 | if (bio) |
| 2887 | bio->bi_end_io = NULL; |
| 2888 | rdev = mirror->replacement; |
| 2889 | /* Note: if rdev != NULL, then bio |
| 2890 | * cannot be NULL as r10buf_pool_alloc will |
| 2891 | * have allocated it. |
| 2892 | * So the second test here is pointless. |
| 2893 | * But it keeps semantic-checkers happy, and |
| 2894 | * this comment keeps human reviewers |
| 2895 | * happy. |
| 2896 | */ |
| 2897 | if (rdev == NULL || bio == NULL || |
| 2898 | test_bit(Faulty, &rdev->flags)) |
| 2899 | break; |
| 2900 | bio->bi_next = biolist; |
| 2901 | biolist = bio; |
| 2902 | bio->bi_private = r10_bio; |
| 2903 | bio->bi_end_io = end_sync_write; |
| 2904 | bio->bi_rw = WRITE; |
| 2905 | bio->bi_sector = to_addr + rdev->data_offset; |
| 2906 | bio->bi_bdev = rdev->bdev; |
| 2907 | atomic_inc(&r10_bio->remaining); |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2908 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2909 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2910 | if (j == conf->copies) { |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2911 | /* Cannot recover, so abort the recovery or |
| 2912 | * record a bad block */ |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2913 | put_buf(r10_bio); |
| 2914 | if (rb2) |
| 2915 | atomic_dec(&rb2->remaining); |
| 2916 | r10_bio = rb2; |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2917 | if (any_working) { |
| 2918 | /* problem is that there are bad blocks |
| 2919 | * on other device(s) |
| 2920 | */ |
| 2921 | int k; |
| 2922 | for (k = 0; k < conf->copies; k++) |
| 2923 | if (r10_bio->devs[k].devnum == i) |
| 2924 | break; |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2925 | if (!test_bit(In_sync, |
| 2926 | &mirror->rdev->flags) |
| 2927 | && !rdev_set_badblocks( |
| 2928 | mirror->rdev, |
| 2929 | r10_bio->devs[k].addr, |
| 2930 | max_sync, 0)) |
| 2931 | any_working = 0; |
| 2932 | if (mirror->replacement && |
| 2933 | !rdev_set_badblocks( |
| 2934 | mirror->replacement, |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2935 | r10_bio->devs[k].addr, |
| 2936 | max_sync, 0)) |
| 2937 | any_working = 0; |
| 2938 | } |
| 2939 | if (!any_working) { |
| 2940 | if (!test_and_set_bit(MD_RECOVERY_INTR, |
| 2941 | &mddev->recovery)) |
| 2942 | printk(KERN_INFO "md/raid10:%s: insufficient " |
| 2943 | "working devices for recovery.\n", |
| 2944 | mdname(mddev)); |
NeilBrown | 24afd80 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2945 | mirror->recovery_disabled |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2946 | = mddev->recovery_disabled; |
| 2947 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2948 | break; |
| 2949 | } |
| 2950 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2951 | if (biolist == NULL) { |
| 2952 | while (r10_bio) { |
NeilBrown | 9f2c9d1 | 2011-10-11 16:48:43 +1100 | [diff] [blame] | 2953 | struct r10bio *rb2 = r10_bio; |
| 2954 | r10_bio = (struct r10bio*) rb2->master_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2955 | rb2->master_bio = NULL; |
| 2956 | put_buf(rb2); |
| 2957 | } |
| 2958 | goto giveup; |
| 2959 | } |
| 2960 | } else { |
| 2961 | /* resync. Schedule a read for every block at this virt offset */ |
| 2962 | int count = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2963 | |
NeilBrown | 78200d4 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 2964 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 2965 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2966 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, |
| 2967 | &sync_blocks, mddev->degraded) && |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2968 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, |
| 2969 | &mddev->recovery)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2970 | /* We can skip this block */ |
| 2971 | *skipped = 1; |
| 2972 | return sync_blocks + sectors_skipped; |
| 2973 | } |
| 2974 | if (sync_blocks < max_sync) |
| 2975 | max_sync = sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2976 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
| 2977 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2978 | r10_bio->mddev = mddev; |
| 2979 | atomic_set(&r10_bio->remaining, 0); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2980 | raise_barrier(conf, 0); |
| 2981 | conf->next_resync = sector_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | |
| 2983 | r10_bio->master_bio = NULL; |
| 2984 | r10_bio->sector = sector_nr; |
| 2985 | set_bit(R10BIO_IsSync, &r10_bio->state); |
| 2986 | raid10_find_phys(conf, r10_bio); |
| 2987 | r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1; |
| 2988 | |
| 2989 | for (i=0; i<conf->copies; i++) { |
| 2990 | int d = r10_bio->devs[i].devnum; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 2991 | sector_t first_bad, sector; |
| 2992 | int bad_sectors; |
| 2993 | |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 2994 | if (r10_bio->devs[i].repl_bio) |
| 2995 | r10_bio->devs[i].repl_bio->bi_end_io = NULL; |
| 2996 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2997 | bio = r10_bio->devs[i].bio; |
| 2998 | bio->bi_end_io = NULL; |
NeilBrown | af03b8e | 2007-06-16 10:16:06 -0700 | [diff] [blame] | 2999 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3000 | if (conf->mirrors[d].rdev == NULL || |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 3001 | test_bit(Faulty, &conf->mirrors[d].rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3002 | continue; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3003 | sector = r10_bio->devs[i].addr; |
| 3004 | if (is_badblock(conf->mirrors[d].rdev, |
| 3005 | sector, max_sync, |
| 3006 | &first_bad, &bad_sectors)) { |
| 3007 | if (first_bad > sector) |
| 3008 | max_sync = first_bad - sector; |
| 3009 | else { |
| 3010 | bad_sectors -= (sector - first_bad); |
| 3011 | if (max_sync > bad_sectors) |
| 3012 | max_sync = max_sync; |
| 3013 | continue; |
| 3014 | } |
| 3015 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3016 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 3017 | atomic_inc(&r10_bio->remaining); |
| 3018 | bio->bi_next = biolist; |
| 3019 | biolist = bio; |
| 3020 | bio->bi_private = r10_bio; |
| 3021 | bio->bi_end_io = end_sync_read; |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 3022 | bio->bi_rw = READ; |
NeilBrown | 40c356c | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3023 | bio->bi_sector = sector + |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3024 | conf->mirrors[d].rdev->data_offset; |
| 3025 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 3026 | count++; |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3027 | |
| 3028 | if (conf->mirrors[d].replacement == NULL || |
| 3029 | test_bit(Faulty, |
| 3030 | &conf->mirrors[d].replacement->flags)) |
| 3031 | continue; |
| 3032 | |
| 3033 | /* Need to set up for writing to the replacement */ |
| 3034 | bio = r10_bio->devs[i].repl_bio; |
| 3035 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
| 3036 | |
| 3037 | sector = r10_bio->devs[i].addr; |
| 3038 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 3039 | bio->bi_next = biolist; |
| 3040 | biolist = bio; |
| 3041 | bio->bi_private = r10_bio; |
| 3042 | bio->bi_end_io = end_sync_write; |
| 3043 | bio->bi_rw = WRITE; |
| 3044 | bio->bi_sector = sector + |
| 3045 | conf->mirrors[d].replacement->data_offset; |
| 3046 | bio->bi_bdev = conf->mirrors[d].replacement->bdev; |
| 3047 | count++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | if (count < 2) { |
| 3051 | for (i=0; i<conf->copies; i++) { |
| 3052 | int d = r10_bio->devs[i].devnum; |
| 3053 | if (r10_bio->devs[i].bio->bi_end_io) |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3054 | rdev_dec_pending(conf->mirrors[d].rdev, |
| 3055 | mddev); |
NeilBrown | 9ad1aef | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3056 | if (r10_bio->devs[i].repl_bio && |
| 3057 | r10_bio->devs[i].repl_bio->bi_end_io) |
| 3058 | rdev_dec_pending( |
| 3059 | conf->mirrors[d].replacement, |
| 3060 | mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3061 | } |
| 3062 | put_buf(r10_bio); |
| 3063 | biolist = NULL; |
| 3064 | goto giveup; |
| 3065 | } |
| 3066 | } |
| 3067 | |
| 3068 | for (bio = biolist; bio ; bio=bio->bi_next) { |
| 3069 | |
| 3070 | bio->bi_flags &= ~(BIO_POOL_MASK - 1); |
| 3071 | if (bio->bi_end_io) |
| 3072 | bio->bi_flags |= 1 << BIO_UPTODATE; |
| 3073 | bio->bi_vcnt = 0; |
| 3074 | bio->bi_idx = 0; |
| 3075 | bio->bi_phys_segments = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3076 | bio->bi_size = 0; |
| 3077 | } |
| 3078 | |
| 3079 | nr_sectors = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3080 | if (sector_nr + max_sync < max_sector) |
| 3081 | max_sector = sector_nr + max_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3082 | do { |
| 3083 | struct page *page; |
| 3084 | int len = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | if (sector_nr + (len>>9) > max_sector) |
| 3086 | len = (max_sector - sector_nr) << 9; |
| 3087 | if (len == 0) |
| 3088 | break; |
| 3089 | for (bio= biolist ; bio ; bio=bio->bi_next) { |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3090 | struct bio *bio2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3091 | page = bio->bi_io_vec[bio->bi_vcnt].bv_page; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3092 | if (bio_add_page(bio, page, len, 0)) |
| 3093 | continue; |
| 3094 | |
| 3095 | /* stop here */ |
| 3096 | bio->bi_io_vec[bio->bi_vcnt].bv_page = page; |
| 3097 | for (bio2 = biolist; |
| 3098 | bio2 && bio2 != bio; |
| 3099 | bio2 = bio2->bi_next) { |
| 3100 | /* remove last page from this bio */ |
| 3101 | bio2->bi_vcnt--; |
| 3102 | bio2->bi_size -= len; |
| 3103 | bio2->bi_flags &= ~(1<< BIO_SEG_VALID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3104 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 3105 | goto bio_full; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | } |
| 3107 | nr_sectors += len>>9; |
| 3108 | sector_nr += len>>9; |
| 3109 | } while (biolist->bi_vcnt < RESYNC_PAGES); |
| 3110 | bio_full: |
| 3111 | r10_bio->sectors = nr_sectors; |
| 3112 | |
| 3113 | while (biolist) { |
| 3114 | bio = biolist; |
| 3115 | biolist = biolist->bi_next; |
| 3116 | |
| 3117 | bio->bi_next = NULL; |
| 3118 | r10_bio = bio->bi_private; |
| 3119 | r10_bio->sectors = nr_sectors; |
| 3120 | |
| 3121 | if (bio->bi_end_io == end_sync_read) { |
| 3122 | md_sync_acct(bio->bi_bdev, nr_sectors); |
| 3123 | generic_make_request(bio); |
| 3124 | } |
| 3125 | } |
| 3126 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 3127 | if (sectors_skipped) |
| 3128 | /* pretend they weren't skipped, it makes |
| 3129 | * no important difference in this case |
| 3130 | */ |
| 3131 | md_done_sync(mddev, sectors_skipped, 1); |
| 3132 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | return sectors_skipped + nr_sectors; |
| 3134 | giveup: |
| 3135 | /* There is nowhere to write, so all non-sync |
NeilBrown | e875ece | 2011-07-28 11:39:24 +1000 | [diff] [blame] | 3136 | * drives must be failed or in resync, all drives |
| 3137 | * have a bad block, so try the next chunk... |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | */ |
NeilBrown | 09b4068 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 3139 | if (sector_nr + max_sync < max_sector) |
| 3140 | max_sector = sector_nr + max_sync; |
| 3141 | |
| 3142 | sectors_skipped += (max_sector - sector_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | chunks_skipped ++; |
| 3144 | sector_nr = max_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3145 | goto skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3146 | } |
| 3147 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3148 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3149 | raid10_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3150 | { |
| 3151 | sector_t size; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3152 | struct r10conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3153 | |
| 3154 | if (!raid_disks) |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 3155 | raid_disks = conf->raid_disks; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3156 | if (!sectors) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3157 | sectors = conf->dev_sectors; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3158 | |
| 3159 | size = sectors >> conf->chunk_shift; |
| 3160 | sector_div(size, conf->far_copies); |
| 3161 | size = size * raid_disks; |
| 3162 | sector_div(size, conf->near_copies); |
| 3163 | |
| 3164 | return size << conf->chunk_shift; |
| 3165 | } |
| 3166 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3167 | |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3168 | static struct r10conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3169 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3170 | struct r10conf *conf = NULL; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 3171 | int nc, fc, fo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3172 | sector_t stride, size; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3173 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3174 | |
Maciej Trela | f73ea87 | 2010-06-16 11:46:29 +0100 | [diff] [blame] | 3175 | if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) || |
| 3176 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 3177 | printk(KERN_ERR "md/raid10:%s: chunk size must be " |
| 3178 | "at least PAGE_SIZE(%ld) and be a power of 2.\n", |
| 3179 | mdname(mddev), PAGE_SIZE); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3180 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3181 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3182 | |
Maciej Trela | f73ea87 | 2010-06-16 11:46:29 +0100 | [diff] [blame] | 3183 | nc = mddev->new_layout & 255; |
| 3184 | fc = (mddev->new_layout >> 8) & 255; |
| 3185 | fo = mddev->new_layout & (1<<16); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3187 | if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks || |
Maciej Trela | f73ea87 | 2010-06-16 11:46:29 +0100 | [diff] [blame] | 3188 | (mddev->new_layout >> 17)) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 3189 | printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n", |
Maciej Trela | f73ea87 | 2010-06-16 11:46:29 +0100 | [diff] [blame] | 3190 | mdname(mddev), mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3191 | goto out; |
| 3192 | } |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3193 | |
| 3194 | err = -ENOMEM; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3195 | conf = kzalloc(sizeof(struct r10conf), GFP_KERNEL); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3196 | if (!conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | goto out; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3198 | |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 3199 | conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks, |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3200 | GFP_KERNEL); |
| 3201 | if (!conf->mirrors) |
| 3202 | goto out; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 3203 | |
| 3204 | conf->tmppage = alloc_page(GFP_KERNEL); |
| 3205 | if (!conf->tmppage) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3206 | goto out; |
| 3207 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3208 | |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 3209 | conf->raid_disks = mddev->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3210 | conf->near_copies = nc; |
| 3211 | conf->far_copies = fc; |
| 3212 | conf->copies = nc*fc; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 3213 | conf->far_offset = fo; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3214 | conf->chunk_mask = mddev->new_chunk_sectors - 1; |
| 3215 | conf->chunk_shift = ffz(~mddev->new_chunk_sectors); |
| 3216 | |
| 3217 | conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc, |
| 3218 | r10bio_pool_free, conf); |
| 3219 | if (!conf->r10bio_pool) |
| 3220 | goto out; |
| 3221 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3222 | size = mddev->dev_sectors >> conf->chunk_shift; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 3223 | sector_div(size, fc); |
| 3224 | size = size * conf->raid_disks; |
| 3225 | sector_div(size, nc); |
| 3226 | /* 'size' is now the number of chunks in the array */ |
| 3227 | /* calculate "used chunks per device" in 'stride' */ |
| 3228 | stride = size * conf->copies; |
NeilBrown | af03b8e | 2007-06-16 10:16:06 -0700 | [diff] [blame] | 3229 | |
| 3230 | /* We need to round up when dividing by raid_disks to |
| 3231 | * get the stride size. |
| 3232 | */ |
| 3233 | stride += conf->raid_disks - 1; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 3234 | sector_div(stride, conf->raid_disks); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3235 | |
| 3236 | conf->dev_sectors = stride << conf->chunk_shift; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 3237 | |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 3238 | if (fo) |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 3239 | stride = 1; |
| 3240 | else |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 3241 | sector_div(stride, fc); |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 3242 | conf->stride = stride << conf->chunk_shift; |
| 3243 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | |
Neil Brown | e7e72bf | 2008-05-14 16:05:54 -0700 | [diff] [blame] | 3245 | spin_lock_init(&conf->device_lock); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3246 | INIT_LIST_HEAD(&conf->retry_list); |
| 3247 | |
| 3248 | spin_lock_init(&conf->resync_lock); |
| 3249 | init_waitqueue_head(&conf->wait_barrier); |
| 3250 | |
| 3251 | conf->thread = md_register_thread(raid10d, mddev, NULL); |
| 3252 | if (!conf->thread) |
| 3253 | goto out; |
| 3254 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3255 | conf->mddev = mddev; |
| 3256 | return conf; |
| 3257 | |
| 3258 | out: |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 3259 | printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n", |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3260 | mdname(mddev)); |
| 3261 | if (conf) { |
| 3262 | if (conf->r10bio_pool) |
| 3263 | mempool_destroy(conf->r10bio_pool); |
| 3264 | kfree(conf->mirrors); |
| 3265 | safe_put_page(conf->tmppage); |
| 3266 | kfree(conf); |
| 3267 | } |
| 3268 | return ERR_PTR(err); |
| 3269 | } |
| 3270 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3271 | static int run(struct mddev *mddev) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3272 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3273 | struct r10conf *conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3274 | int i, disk_idx, chunk_size; |
NeilBrown | 0f6d02d | 2011-10-11 16:48:46 +1100 | [diff] [blame] | 3275 | struct mirror_info *disk; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3276 | struct md_rdev *rdev; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3277 | sector_t size; |
| 3278 | |
| 3279 | /* |
| 3280 | * copy the already verified devices into our private RAID10 |
| 3281 | * bookkeeping area. [whatever we allocate in run(), |
| 3282 | * should be freed in stop()] |
| 3283 | */ |
| 3284 | |
| 3285 | if (mddev->private == NULL) { |
| 3286 | conf = setup_conf(mddev); |
| 3287 | if (IS_ERR(conf)) |
| 3288 | return PTR_ERR(conf); |
| 3289 | mddev->private = conf; |
| 3290 | } |
| 3291 | conf = mddev->private; |
| 3292 | if (!conf) |
| 3293 | goto out; |
| 3294 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3295 | mddev->thread = conf->thread; |
| 3296 | conf->thread = NULL; |
| 3297 | |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 3298 | chunk_size = mddev->chunk_sectors << 9; |
| 3299 | blk_queue_io_min(mddev->queue, chunk_size); |
| 3300 | if (conf->raid_disks % conf->near_copies) |
| 3301 | blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks); |
| 3302 | else |
| 3303 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 3304 | (conf->raid_disks / conf->near_copies)); |
| 3305 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3306 | rdev_for_each(rdev, mddev) { |
NeilBrown | 34b343c | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 3307 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3308 | disk_idx = rdev->raid_disk; |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 3309 | if (disk_idx >= conf->raid_disks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3310 | || disk_idx < 0) |
| 3311 | continue; |
| 3312 | disk = conf->mirrors + disk_idx; |
| 3313 | |
NeilBrown | 56a2559 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3314 | if (test_bit(Replacement, &rdev->flags)) { |
| 3315 | if (disk->replacement) |
| 3316 | goto out_free_conf; |
| 3317 | disk->replacement = rdev; |
| 3318 | } else { |
| 3319 | if (disk->rdev) |
| 3320 | goto out_free_conf; |
| 3321 | disk->rdev = rdev; |
| 3322 | } |
| 3323 | |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 3324 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 3325 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3326 | |
| 3327 | disk->head_position = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3328 | } |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 3329 | /* need to check that every block has at least one working mirror */ |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3330 | if (!enough(conf, -1)) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 3331 | printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n", |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 3332 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3333 | goto out_free_conf; |
| 3334 | } |
| 3335 | |
| 3336 | mddev->degraded = 0; |
| 3337 | for (i = 0; i < conf->raid_disks; i++) { |
| 3338 | |
| 3339 | disk = conf->mirrors + i; |
| 3340 | |
NeilBrown | 56a2559 | 2011-12-23 10:17:55 +1100 | [diff] [blame] | 3341 | if (!disk->rdev && disk->replacement) { |
| 3342 | /* The replacement is all we have - use it */ |
| 3343 | disk->rdev = disk->replacement; |
| 3344 | disk->replacement = NULL; |
| 3345 | clear_bit(Replacement, &disk->rdev->flags); |
| 3346 | } |
| 3347 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 3348 | if (!disk->rdev || |
NeilBrown | 2e333e8 | 2006-10-21 10:24:07 -0700 | [diff] [blame] | 3349 | !test_bit(In_sync, &disk->rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3350 | disk->head_position = 0; |
| 3351 | mddev->degraded++; |
Neil Brown | 8c2e870 | 2008-06-28 08:30:52 +1000 | [diff] [blame] | 3352 | if (disk->rdev) |
| 3353 | conf->fullsync = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3354 | } |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 3355 | disk->recovery_disabled = mddev->recovery_disabled - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3356 | } |
| 3357 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 3358 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 3359 | printk(KERN_NOTICE "md/raid10:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 3360 | " -- starting background reconstruction\n", |
| 3361 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3362 | printk(KERN_INFO |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 3363 | "md/raid10:%s: active with %d out of %d devices\n", |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 3364 | mdname(mddev), conf->raid_disks - mddev->degraded, |
| 3365 | conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3366 | /* |
| 3367 | * Ok, everything is just fine now |
| 3368 | */ |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3369 | mddev->dev_sectors = conf->dev_sectors; |
| 3370 | size = raid10_size(mddev, 0, 0); |
| 3371 | md_set_array_sectors(mddev, size); |
| 3372 | mddev->resync_max_sectors = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3373 | |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 3374 | mddev->queue->backing_dev_info.congested_fn = raid10_congested; |
| 3375 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 3376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | /* Calculate max read-ahead size. |
| 3378 | * We need to readahead at least twice a whole stripe.... |
| 3379 | * maybe... |
| 3380 | */ |
| 3381 | { |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3382 | int stripe = conf->raid_disks * |
| 3383 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | stripe /= conf->near_copies; |
| 3385 | if (mddev->queue->backing_dev_info.ra_pages < 2* stripe) |
| 3386 | mddev->queue->backing_dev_info.ra_pages = 2* stripe; |
| 3387 | } |
| 3388 | |
NeilBrown | 050b661 | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3389 | blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec); |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 3390 | |
| 3391 | if (md_integrity_register(mddev)) |
| 3392 | goto out_free_conf; |
| 3393 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3394 | return 0; |
| 3395 | |
| 3396 | out_free_conf: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 3397 | md_unregister_thread(&mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3398 | if (conf->r10bio_pool) |
| 3399 | mempool_destroy(conf->r10bio_pool); |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 3400 | safe_put_page(conf->tmppage); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 3401 | kfree(conf->mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | kfree(conf); |
| 3403 | mddev->private = NULL; |
| 3404 | out: |
| 3405 | return -EIO; |
| 3406 | } |
| 3407 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3408 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3410 | struct r10conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3411 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 3412 | raise_barrier(conf, 0); |
| 3413 | lower_barrier(conf); |
| 3414 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 3415 | md_unregister_thread(&mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 3417 | if (conf->r10bio_pool) |
| 3418 | mempool_destroy(conf->r10bio_pool); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 3419 | kfree(conf->mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3420 | kfree(conf); |
| 3421 | mddev->private = NULL; |
| 3422 | return 0; |
| 3423 | } |
| 3424 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3425 | static void raid10_quiesce(struct mddev *mddev, int state) |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3426 | { |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3427 | struct r10conf *conf = mddev->private; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3428 | |
| 3429 | switch(state) { |
| 3430 | case 1: |
| 3431 | raise_barrier(conf, 0); |
| 3432 | break; |
| 3433 | case 0: |
| 3434 | lower_barrier(conf); |
| 3435 | break; |
| 3436 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3437 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3438 | |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame^] | 3439 | static int raid10_resize(struct mddev *mddev, sector_t sectors) |
| 3440 | { |
| 3441 | /* Resize of 'far' arrays is not supported. |
| 3442 | * For 'near' and 'offset' arrays we can set the |
| 3443 | * number of sectors used to be an appropriate multiple |
| 3444 | * of the chunk size. |
| 3445 | * For 'offset', this is far_copies*chunksize. |
| 3446 | * For 'near' the multiplier is the LCM of |
| 3447 | * near_copies and raid_disks. |
| 3448 | * So if far_copies > 1 && !far_offset, fail. |
| 3449 | * Else find LCM(raid_disks, near_copy)*far_copies and |
| 3450 | * multiply by chunk_size. Then round to this number. |
| 3451 | * This is mostly done by raid10_size() |
| 3452 | */ |
| 3453 | struct r10conf *conf = mddev->private; |
| 3454 | sector_t oldsize, size; |
| 3455 | |
| 3456 | if (conf->far_copies > 1 && !conf->far_offset) |
| 3457 | return -EINVAL; |
| 3458 | |
| 3459 | oldsize = raid10_size(mddev, 0, 0); |
| 3460 | size = raid10_size(mddev, sectors, 0); |
| 3461 | md_set_array_sectors(mddev, size); |
| 3462 | if (mddev->array_sectors > size) |
| 3463 | return -EINVAL; |
| 3464 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 3465 | revalidate_disk(mddev->gendisk); |
| 3466 | if (sectors > mddev->dev_sectors && |
| 3467 | mddev->recovery_cp > oldsize) { |
| 3468 | mddev->recovery_cp = oldsize; |
| 3469 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 3470 | } |
| 3471 | mddev->dev_sectors = sectors; |
| 3472 | mddev->resync_max_sectors = size; |
| 3473 | return 0; |
| 3474 | } |
| 3475 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3476 | static void *raid10_takeover_raid0(struct mddev *mddev) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3477 | { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3478 | struct md_rdev *rdev; |
NeilBrown | e879a87 | 2011-10-11 16:49:02 +1100 | [diff] [blame] | 3479 | struct r10conf *conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3480 | |
| 3481 | if (mddev->degraded > 0) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 3482 | printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n", |
| 3483 | mdname(mddev)); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3484 | return ERR_PTR(-EINVAL); |
| 3485 | } |
| 3486 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3487 | /* Set new parameters */ |
| 3488 | mddev->new_level = 10; |
| 3489 | /* new layout: far_copies = 1, near_copies = 2 */ |
| 3490 | mddev->new_layout = (1<<8) + 2; |
| 3491 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 3492 | mddev->delta_disks = mddev->raid_disks; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3493 | mddev->raid_disks *= 2; |
| 3494 | /* make sure it will be not marked as dirty */ |
| 3495 | mddev->recovery_cp = MaxSector; |
| 3496 | |
| 3497 | conf = setup_conf(mddev); |
Krzysztof Wojcik | 02214dc | 2011-02-04 14:18:26 +0100 | [diff] [blame] | 3498 | if (!IS_ERR(conf)) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 3499 | rdev_for_each(rdev, mddev) |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 3500 | if (rdev->raid_disk >= 0) |
| 3501 | rdev->new_raid_disk = rdev->raid_disk * 2; |
Krzysztof Wojcik | 02214dc | 2011-02-04 14:18:26 +0100 | [diff] [blame] | 3502 | conf->barrier = 1; |
| 3503 | } |
| 3504 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3505 | return conf; |
| 3506 | } |
| 3507 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3508 | static void *raid10_takeover(struct mddev *mddev) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3509 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 3510 | struct r0conf *raid0_conf; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3511 | |
| 3512 | /* raid10 can take over: |
| 3513 | * raid0 - providing it has only two drives |
| 3514 | */ |
| 3515 | if (mddev->level == 0) { |
| 3516 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 3517 | raid0_conf = mddev->private; |
| 3518 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 3519 | printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0" |
| 3520 | " with more than one zone.\n", |
| 3521 | mdname(mddev)); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3522 | return ERR_PTR(-EINVAL); |
| 3523 | } |
| 3524 | return raid10_takeover_raid0(mddev); |
| 3525 | } |
| 3526 | return ERR_PTR(-EINVAL); |
| 3527 | } |
| 3528 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 3529 | static struct md_personality raid10_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3530 | { |
| 3531 | .name = "raid10", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3532 | .level = 10, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3533 | .owner = THIS_MODULE, |
| 3534 | .make_request = make_request, |
| 3535 | .run = run, |
| 3536 | .stop = stop, |
| 3537 | .status = status, |
| 3538 | .error_handler = error, |
| 3539 | .hot_add_disk = raid10_add_disk, |
| 3540 | .hot_remove_disk= raid10_remove_disk, |
| 3541 | .spare_active = raid10_spare_active, |
| 3542 | .sync_request = sync_request, |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 3543 | .quiesce = raid10_quiesce, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 3544 | .size = raid10_size, |
NeilBrown | 006a09a | 2012-03-19 12:46:40 +1100 | [diff] [blame^] | 3545 | .resize = raid10_resize, |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 3546 | .takeover = raid10_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3547 | }; |
| 3548 | |
| 3549 | static int __init raid_init(void) |
| 3550 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3551 | return register_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3552 | } |
| 3553 | |
| 3554 | static void raid_exit(void) |
| 3555 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3556 | unregister_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3557 | } |
| 3558 | |
| 3559 | module_init(raid_init); |
| 3560 | module_exit(raid_exit); |
| 3561 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 3562 | MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3563 | MODULE_ALIAS("md-personality-9"); /* RAID10 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 3564 | MODULE_ALIAS("md-raid10"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 3565 | MODULE_ALIAS("md-level-10"); |
NeilBrown | 34db0cd | 2011-10-11 16:50:01 +1100 | [diff] [blame] | 3566 | |
| 3567 | module_param(max_queued_requests, int, S_IRUGO|S_IWUSR); |