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> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 24 | #include <linux/seq_file.h> |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 25 | #include <linux/ratelimit.h> |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 26 | #include "md.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 27 | #include "raid10.h" |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 28 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 29 | #include "bitmap.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
| 31 | /* |
| 32 | * RAID10 provides a combination of RAID0 and RAID1 functionality. |
| 33 | * The layout of data is defined by |
| 34 | * chunk_size |
| 35 | * raid_disks |
| 36 | * near_copies (stored in low byte of layout) |
| 37 | * far_copies (stored in second byte of layout) |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 38 | * far_offset (stored in bit 16 of layout ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | * |
| 40 | * The data to be stored is divided into chunks using chunksize. |
| 41 | * Each device is divided into far_copies sections. |
| 42 | * In each section, chunks are laid out in a style similar to raid0, but |
| 43 | * near_copies copies of each chunk is stored (each on a different drive). |
| 44 | * The starting device for each section is offset near_copies from the starting |
| 45 | * device of the previous section. |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 46 | * 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] | 47 | * drive. |
| 48 | * near_copies and far_copies must be at least one, and their product is at most |
| 49 | * raid_disks. |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 50 | * |
| 51 | * If far_offset is true, then the far_copies are handled a bit differently. |
| 52 | * The copies are still in different stripes, but instead of be very far apart |
| 53 | * on disk, there are adjacent stripes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | */ |
| 55 | |
| 56 | /* |
| 57 | * Number of guaranteed r10bios in case of extreme VM load: |
| 58 | */ |
| 59 | #define NR_RAID10_BIOS 256 |
| 60 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 61 | static void allow_barrier(conf_t *conf); |
| 62 | static void lower_barrier(conf_t *conf); |
| 63 | |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 64 | static void * r10bio_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | { |
| 66 | conf_t *conf = data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | int size = offsetof(struct r10bio_s, devs[conf->copies]); |
| 68 | |
| 69 | /* allocate a r10bio with room for raid_disks entries in the bios array */ |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 70 | return kzalloc(size, gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | static void r10bio_pool_free(void *r10_bio, void *data) |
| 74 | { |
| 75 | kfree(r10_bio); |
| 76 | } |
| 77 | |
NeilBrown | 0310fa2 | 2008-08-05 15:54:14 +1000 | [diff] [blame] | 78 | /* Maximum size of each resync request */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | #define RESYNC_BLOCK_SIZE (64*1024) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) |
NeilBrown | 0310fa2 | 2008-08-05 15:54:14 +1000 | [diff] [blame] | 81 | /* amount of memory to reserve for resync requests */ |
| 82 | #define RESYNC_WINDOW (1024*1024) |
| 83 | /* maximum number of concurrent requests, memory permitting */ |
| 84 | #define RESYNC_DEPTH (32*1024*1024/RESYNC_BLOCK_SIZE) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
| 86 | /* |
| 87 | * When performing a resync, we need to read and compare, so |
| 88 | * we need as many pages are there are copies. |
| 89 | * When performing a recovery, we need 2 bios, one for read, |
| 90 | * one for write (we recover only one drive per r10buf) |
| 91 | * |
| 92 | */ |
Al Viro | dd0fc66 | 2005-10-07 07:46:04 +0100 | [diff] [blame] | 93 | static void * r10buf_pool_alloc(gfp_t gfp_flags, void *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | { |
| 95 | conf_t *conf = data; |
| 96 | struct page *page; |
| 97 | r10bio_t *r10_bio; |
| 98 | struct bio *bio; |
| 99 | int i, j; |
| 100 | int nalloc; |
| 101 | |
| 102 | r10_bio = r10bio_pool_alloc(gfp_flags, conf); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 103 | if (!r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | |
| 106 | if (test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) |
| 107 | nalloc = conf->copies; /* resync */ |
| 108 | else |
| 109 | nalloc = 2; /* recovery */ |
| 110 | |
| 111 | /* |
| 112 | * Allocate bios. |
| 113 | */ |
| 114 | for (j = nalloc ; j-- ; ) { |
NeilBrown | 6746557 | 2010-10-26 17:33:54 +1100 | [diff] [blame] | 115 | bio = bio_kmalloc(gfp_flags, RESYNC_PAGES); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | if (!bio) |
| 117 | goto out_free_bio; |
| 118 | r10_bio->devs[j].bio = bio; |
| 119 | } |
| 120 | /* |
| 121 | * Allocate RESYNC_PAGES data pages and attach them |
| 122 | * where needed. |
| 123 | */ |
| 124 | for (j = 0 ; j < nalloc; j++) { |
| 125 | bio = r10_bio->devs[j].bio; |
| 126 | for (i = 0; i < RESYNC_PAGES; i++) { |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 127 | if (j == 1 && !test_bit(MD_RECOVERY_SYNC, |
| 128 | &conf->mddev->recovery)) { |
| 129 | /* we can share bv_page's during recovery */ |
| 130 | struct bio *rbio = r10_bio->devs[0].bio; |
| 131 | page = rbio->bi_io_vec[i].bv_page; |
| 132 | get_page(page); |
| 133 | } else |
| 134 | page = alloc_page(gfp_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if (unlikely(!page)) |
| 136 | goto out_free_pages; |
| 137 | |
| 138 | bio->bi_io_vec[i].bv_page = page; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | return r10_bio; |
| 143 | |
| 144 | out_free_pages: |
| 145 | for ( ; i > 0 ; i--) |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 146 | safe_put_page(bio->bi_io_vec[i-1].bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | while (j--) |
| 148 | for (i = 0; i < RESYNC_PAGES ; i++) |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 149 | 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] | 150 | j = -1; |
| 151 | out_free_bio: |
| 152 | while ( ++j < nalloc ) |
| 153 | bio_put(r10_bio->devs[j].bio); |
| 154 | r10bio_pool_free(r10_bio, conf); |
| 155 | return NULL; |
| 156 | } |
| 157 | |
| 158 | static void r10buf_pool_free(void *__r10_bio, void *data) |
| 159 | { |
| 160 | int i; |
| 161 | conf_t *conf = data; |
| 162 | r10bio_t *r10bio = __r10_bio; |
| 163 | int j; |
| 164 | |
| 165 | for (j=0; j < conf->copies; j++) { |
| 166 | struct bio *bio = r10bio->devs[j].bio; |
| 167 | if (bio) { |
| 168 | for (i = 0; i < RESYNC_PAGES; i++) { |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 169 | safe_put_page(bio->bi_io_vec[i].bv_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | bio->bi_io_vec[i].bv_page = NULL; |
| 171 | } |
| 172 | bio_put(bio); |
| 173 | } |
| 174 | } |
| 175 | r10bio_pool_free(r10bio, conf); |
| 176 | } |
| 177 | |
| 178 | static void put_all_bios(conf_t *conf, r10bio_t *r10_bio) |
| 179 | { |
| 180 | int i; |
| 181 | |
| 182 | for (i = 0; i < conf->copies; i++) { |
| 183 | struct bio **bio = & r10_bio->devs[i].bio; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 184 | if (*bio && *bio != IO_BLOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | bio_put(*bio); |
| 186 | *bio = NULL; |
| 187 | } |
| 188 | } |
| 189 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 190 | static void free_r10bio(r10bio_t *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 192 | conf_t *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | put_all_bios(conf, r10_bio); |
| 195 | mempool_free(r10_bio, conf->r10bio_pool); |
| 196 | } |
| 197 | |
Arjan van de Ven | 858119e | 2006-01-14 13:20:43 -0800 | [diff] [blame] | 198 | static void put_buf(r10bio_t *r10_bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 200 | conf_t *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | |
| 202 | mempool_free(r10_bio, conf->r10buf_pool); |
| 203 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 204 | lower_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void reschedule_retry(r10bio_t *r10_bio) |
| 208 | { |
| 209 | unsigned long flags; |
| 210 | mddev_t *mddev = r10_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 211 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
| 213 | spin_lock_irqsave(&conf->device_lock, flags); |
| 214 | list_add(&r10_bio->retry_list, &conf->retry_list); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 215 | conf->nr_queued ++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 217 | |
Arthur Jones | 388667b | 2008-07-25 12:03:38 -0700 | [diff] [blame] | 218 | /* wake up frozen array... */ |
| 219 | wake_up(&conf->wait_barrier); |
| 220 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | md_wakeup_thread(mddev->thread); |
| 222 | } |
| 223 | |
| 224 | /* |
| 225 | * raid_end_bio_io() is called when we have finished servicing a mirrored |
| 226 | * operation and are ready to return a success/failure code to the buffer |
| 227 | * cache layer. |
| 228 | */ |
| 229 | static void raid_end_bio_io(r10bio_t *r10_bio) |
| 230 | { |
| 231 | struct bio *bio = r10_bio->master_bio; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 232 | int done; |
| 233 | conf_t *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 234 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 235 | if (bio->bi_phys_segments) { |
| 236 | unsigned long flags; |
| 237 | spin_lock_irqsave(&conf->device_lock, flags); |
| 238 | bio->bi_phys_segments--; |
| 239 | done = (bio->bi_phys_segments == 0); |
| 240 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 241 | } else |
| 242 | done = 1; |
| 243 | if (!test_bit(R10BIO_Uptodate, &r10_bio->state)) |
| 244 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
| 245 | if (done) { |
| 246 | bio_endio(bio, 0); |
| 247 | /* |
| 248 | * Wake up any possible resync thread that waits for the device |
| 249 | * to go idle. |
| 250 | */ |
| 251 | allow_barrier(conf); |
| 252 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | free_r10bio(r10_bio); |
| 254 | } |
| 255 | |
| 256 | /* |
| 257 | * Update disk head position estimator based on IRQ completion info. |
| 258 | */ |
| 259 | static inline void update_head_pos(int slot, r10bio_t *r10_bio) |
| 260 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 261 | conf_t *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | |
| 263 | conf->mirrors[r10_bio->devs[slot].devnum].head_position = |
| 264 | r10_bio->devs[slot].addr + (r10_bio->sectors); |
| 265 | } |
| 266 | |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 267 | /* |
| 268 | * Find the disk number which triggered given bio |
| 269 | */ |
| 270 | static int find_bio_disk(conf_t *conf, r10bio_t *r10_bio, struct bio *bio) |
| 271 | { |
| 272 | int slot; |
| 273 | |
| 274 | for (slot = 0; slot < conf->copies; slot++) |
| 275 | if (r10_bio->devs[slot].bio == bio) |
| 276 | break; |
| 277 | |
| 278 | BUG_ON(slot == conf->copies); |
| 279 | update_head_pos(slot, r10_bio); |
| 280 | |
| 281 | return r10_bio->devs[slot].devnum; |
| 282 | } |
| 283 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 284 | static void raid10_end_read_request(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | { |
| 286 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 287 | r10bio_t *r10_bio = bio->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 288 | int slot, dev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 289 | conf_t *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | |
| 292 | slot = r10_bio->read_slot; |
| 293 | dev = r10_bio->devs[slot].devnum; |
| 294 | /* |
| 295 | * this branch is our 'one mirror IO has finished' event handler: |
| 296 | */ |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 297 | update_head_pos(slot, r10_bio); |
| 298 | |
| 299 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 300 | /* |
| 301 | * Set R10BIO_Uptodate in our master bio, so that |
| 302 | * we will return a good error code to the higher |
| 303 | * levels even if IO on some other mirrored buffer fails. |
| 304 | * |
| 305 | * The 'master' represents the composite IO operation to |
| 306 | * user-side. So if something waits for IO, then it will |
| 307 | * wait for the 'master' bio. |
| 308 | */ |
| 309 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | raid_end_bio_io(r10_bio); |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 311 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 312 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | /* |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 314 | * oops, read error - keep the refcount on the rdev |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | */ |
| 316 | char b[BDEVNAME_SIZE]; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 317 | printk_ratelimited(KERN_ERR |
| 318 | "md/raid10:%s: %s: rescheduling sector %llu\n", |
| 319 | mdname(conf->mddev), |
| 320 | bdevname(conf->mirrors[dev].rdev->bdev, b), |
| 321 | (unsigned long long)r10_bio->sector); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 322 | set_bit(R10BIO_ReadError, &r10_bio->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | reschedule_retry(r10_bio); |
| 324 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | } |
| 326 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 327 | static void raid10_end_write_request(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | { |
| 329 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 330 | r10bio_t *r10_bio = bio->bi_private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 331 | int dev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 332 | conf_t *conf = r10_bio->mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 334 | dev = find_bio_disk(conf, r10_bio, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 335 | |
| 336 | /* |
| 337 | * this branch is our 'one mirror IO has finished' event handler: |
| 338 | */ |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 339 | if (!uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | md_error(r10_bio->mddev, conf->mirrors[dev].rdev); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 341 | /* an I/O failed, we can't clear the bitmap */ |
| 342 | set_bit(R10BIO_Degraded, &r10_bio->state); |
| 343 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | /* |
| 345 | * Set R10BIO_Uptodate in our master bio, so that |
| 346 | * we will return a good error code for to the higher |
| 347 | * levels even if IO on some other mirrored buffer fails. |
| 348 | * |
| 349 | * The 'master' represents the composite IO operation to |
| 350 | * user-side. So if something waits for IO, then it will |
| 351 | * wait for the 'master' bio. |
| 352 | */ |
| 353 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
| 354 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | /* |
| 356 | * |
| 357 | * Let's see if all mirrored write operations have finished |
| 358 | * already. |
| 359 | */ |
| 360 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 361 | /* clear the bitmap if all writes complete successfully */ |
| 362 | bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, |
| 363 | r10_bio->sectors, |
| 364 | !test_bit(R10BIO_Degraded, &r10_bio->state), |
| 365 | 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | md_write_end(r10_bio->mddev); |
| 367 | raid_end_bio_io(r10_bio); |
| 368 | } |
| 369 | |
| 370 | rdev_dec_pending(conf->mirrors[dev].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 371 | } |
| 372 | |
| 373 | |
| 374 | /* |
| 375 | * RAID10 layout manager |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 376 | * As well as the chunksize and raid_disks count, there are two |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | * parameters: near_copies and far_copies. |
| 378 | * near_copies * far_copies must be <= raid_disks. |
| 379 | * Normally one of these will be 1. |
| 380 | * If both are 1, we get raid0. |
| 381 | * If near_copies == raid_disks, we get raid1. |
| 382 | * |
Lucas De Marchi | 25985ed | 2011-03-30 22:57:33 -0300 | [diff] [blame] | 383 | * 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] | 384 | * first chunk, followed by near_copies copies of the next chunk and |
| 385 | * so on. |
| 386 | * If far_copies > 1, then after 1/far_copies of the array has been assigned |
| 387 | * as described above, we start again with a device offset of near_copies. |
| 388 | * So we effectively have another copy of the whole array further down all |
| 389 | * the drives, but with blocks on different drives. |
| 390 | * With this layout, and block is never stored twice on the one device. |
| 391 | * |
| 392 | * raid10_find_phys finds the sector offset of a given virtual sector |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 393 | * on each device that it is on. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 394 | * |
| 395 | * raid10_find_virt does the reverse mapping, from a device and a |
| 396 | * sector offset to a virtual address |
| 397 | */ |
| 398 | |
| 399 | static void raid10_find_phys(conf_t *conf, r10bio_t *r10bio) |
| 400 | { |
| 401 | int n,f; |
| 402 | sector_t sector; |
| 403 | sector_t chunk; |
| 404 | sector_t stripe; |
| 405 | int dev; |
| 406 | |
| 407 | int slot = 0; |
| 408 | |
| 409 | /* now calculate first sector/dev */ |
| 410 | chunk = r10bio->sector >> conf->chunk_shift; |
| 411 | sector = r10bio->sector & conf->chunk_mask; |
| 412 | |
| 413 | chunk *= conf->near_copies; |
| 414 | stripe = chunk; |
| 415 | dev = sector_div(stripe, conf->raid_disks); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 416 | if (conf->far_offset) |
| 417 | stripe *= conf->far_copies; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
| 419 | sector += stripe << conf->chunk_shift; |
| 420 | |
| 421 | /* and calculate all the others */ |
| 422 | for (n=0; n < conf->near_copies; n++) { |
| 423 | int d = dev; |
| 424 | sector_t s = sector; |
| 425 | r10bio->devs[slot].addr = sector; |
| 426 | r10bio->devs[slot].devnum = d; |
| 427 | slot++; |
| 428 | |
| 429 | for (f = 1; f < conf->far_copies; f++) { |
| 430 | d += conf->near_copies; |
| 431 | if (d >= conf->raid_disks) |
| 432 | d -= conf->raid_disks; |
| 433 | s += conf->stride; |
| 434 | r10bio->devs[slot].devnum = d; |
| 435 | r10bio->devs[slot].addr = s; |
| 436 | slot++; |
| 437 | } |
| 438 | dev++; |
| 439 | if (dev >= conf->raid_disks) { |
| 440 | dev = 0; |
| 441 | sector += (conf->chunk_mask + 1); |
| 442 | } |
| 443 | } |
| 444 | BUG_ON(slot != conf->copies); |
| 445 | } |
| 446 | |
| 447 | static sector_t raid10_find_virt(conf_t *conf, sector_t sector, int dev) |
| 448 | { |
| 449 | sector_t offset, chunk, vchunk; |
| 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | offset = sector & conf->chunk_mask; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 452 | if (conf->far_offset) { |
| 453 | int fc; |
| 454 | chunk = sector >> conf->chunk_shift; |
| 455 | fc = sector_div(chunk, conf->far_copies); |
| 456 | dev -= fc * conf->near_copies; |
| 457 | if (dev < 0) |
| 458 | dev += conf->raid_disks; |
| 459 | } else { |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 460 | while (sector >= conf->stride) { |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 461 | sector -= conf->stride; |
| 462 | if (dev < conf->near_copies) |
| 463 | dev += conf->raid_disks - conf->near_copies; |
| 464 | else |
| 465 | dev -= conf->near_copies; |
| 466 | } |
| 467 | chunk = sector >> conf->chunk_shift; |
| 468 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | vchunk = chunk * conf->raid_disks + dev; |
| 470 | sector_div(vchunk, conf->near_copies); |
| 471 | return (vchunk << conf->chunk_shift) + offset; |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * raid10_mergeable_bvec -- tell bio layer if a two requests can be merged |
| 476 | * @q: request queue |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 477 | * @bvm: properties of new bio |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 478 | * @biovec: the request that could be merged to it. |
| 479 | * |
| 480 | * Return amount of bytes we can accept at this offset |
| 481 | * If near_copies == raid_disk, there are no striping issues, |
| 482 | * but in that case, the function isn't called at all. |
| 483 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 484 | static int raid10_mergeable_bvec(struct request_queue *q, |
| 485 | struct bvec_merge_data *bvm, |
| 486 | struct bio_vec *biovec) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | { |
| 488 | mddev_t *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 489 | sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 491 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 492 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
| 494 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 495 | if (max < 0) max = 0; /* bio_add cannot handle a negative return */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 496 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 497 | return biovec->bv_len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 498 | else |
| 499 | return max; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | * This routine returns the disk from which the requested read should |
| 504 | * be done. There is a per-array 'next expected sequential IO' sector |
| 505 | * number - if this matches on the next IO then we use the last disk. |
| 506 | * There is also a per-disk 'last know head position' sector that is |
| 507 | * maintained from IRQ contexts, both the normal and the resync IO |
| 508 | * completion handlers update this position correctly. If there is no |
| 509 | * perfect sequential match then we pick the disk whose head is closest. |
| 510 | * |
| 511 | * If there are 2 mirrors in the same 2 devices, performance degrades |
| 512 | * because position is mirror, not device based. |
| 513 | * |
| 514 | * The rdev for the device selected will have nr_pending incremented. |
| 515 | */ |
| 516 | |
| 517 | /* |
| 518 | * FIXME: possibly should rethink readbalancing and do it differently |
| 519 | * depending on near_copies / far_copies geometry. |
| 520 | */ |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 521 | static int read_balance(conf_t *conf, r10bio_t *r10_bio, int *max_sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | { |
NeilBrown | af3a2cd | 2010-05-08 08:20:17 +1000 | [diff] [blame] | 523 | const sector_t this_sector = r10_bio->sector; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 524 | int disk, slot; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 525 | int sectors = r10_bio->sectors; |
| 526 | int best_good_sectors; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 527 | sector_t new_distance, best_dist; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 528 | mdk_rdev_t *rdev; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 529 | int do_balance; |
| 530 | int best_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | |
| 532 | raid10_find_phys(conf, r10_bio); |
| 533 | rcu_read_lock(); |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 534 | retry: |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 535 | sectors = r10_bio->sectors; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 536 | best_slot = -1; |
| 537 | best_dist = MaxSector; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 538 | best_good_sectors = 0; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 539 | do_balance = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | /* |
| 541 | * Check if we can balance. We can balance on the whole |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 542 | * device if no resync is going on (recovery is ok), or below |
| 543 | * the resync window. We take the first readable disk when |
| 544 | * above the resync window. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 545 | */ |
| 546 | if (conf->mddev->recovery_cp < MaxSector |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 547 | && (this_sector + sectors >= conf->next_resync)) |
| 548 | do_balance = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 550 | for (slot = 0; slot < conf->copies ; slot++) { |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 551 | sector_t first_bad; |
| 552 | int bad_sectors; |
| 553 | sector_t dev_sector; |
| 554 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 555 | if (r10_bio->devs[slot].bio == IO_BLOCKED) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | continue; |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 557 | disk = r10_bio->devs[slot].devnum; |
| 558 | rdev = rcu_dereference(conf->mirrors[disk].rdev); |
| 559 | if (rdev == NULL) |
| 560 | continue; |
| 561 | if (!test_bit(In_sync, &rdev->flags)) |
| 562 | continue; |
| 563 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 564 | dev_sector = r10_bio->devs[slot].addr; |
| 565 | if (is_badblock(rdev, dev_sector, sectors, |
| 566 | &first_bad, &bad_sectors)) { |
| 567 | if (best_dist < MaxSector) |
| 568 | /* Already have a better slot */ |
| 569 | continue; |
| 570 | if (first_bad <= dev_sector) { |
| 571 | /* Cannot read here. If this is the |
| 572 | * 'primary' device, then we must not read |
| 573 | * beyond 'bad_sectors' from another device. |
| 574 | */ |
| 575 | bad_sectors -= (dev_sector - first_bad); |
| 576 | if (!do_balance && sectors > bad_sectors) |
| 577 | sectors = bad_sectors; |
| 578 | if (best_good_sectors > sectors) |
| 579 | best_good_sectors = sectors; |
| 580 | } else { |
| 581 | sector_t good_sectors = |
| 582 | first_bad - dev_sector; |
| 583 | if (good_sectors > best_good_sectors) { |
| 584 | best_good_sectors = good_sectors; |
| 585 | best_slot = slot; |
| 586 | } |
| 587 | if (!do_balance) |
| 588 | /* Must read from here */ |
| 589 | break; |
| 590 | } |
| 591 | continue; |
| 592 | } else |
| 593 | best_good_sectors = sectors; |
| 594 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 595 | if (!do_balance) |
| 596 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 597 | |
NeilBrown | 22dfdf5 | 2005-11-28 13:44:09 -0800 | [diff] [blame] | 598 | /* This optimisation is debatable, and completely destroys |
| 599 | * sequential read speed for 'far copies' arrays. So only |
| 600 | * keep it for 'near' arrays, and review those later. |
| 601 | */ |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 602 | if (conf->near_copies > 1 && !atomic_read(&rdev->nr_pending)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 603 | break; |
Keld Simonsen | 8ed3a19 | 2008-03-04 14:29:34 -0800 | [diff] [blame] | 604 | |
| 605 | /* for far > 1 always use the lowest address */ |
| 606 | if (conf->far_copies > 1) |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 607 | new_distance = r10_bio->devs[slot].addr; |
Keld Simonsen | 8ed3a19 | 2008-03-04 14:29:34 -0800 | [diff] [blame] | 608 | else |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 609 | new_distance = abs(r10_bio->devs[slot].addr - |
| 610 | conf->mirrors[disk].head_position); |
| 611 | if (new_distance < best_dist) { |
| 612 | best_dist = new_distance; |
| 613 | best_slot = slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 614 | } |
| 615 | } |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 616 | if (slot == conf->copies) |
| 617 | slot = best_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 618 | |
NeilBrown | 56d9912 | 2011-05-11 14:27:03 +1000 | [diff] [blame] | 619 | if (slot >= 0) { |
| 620 | disk = r10_bio->devs[slot].devnum; |
| 621 | rdev = rcu_dereference(conf->mirrors[disk].rdev); |
| 622 | if (!rdev) |
| 623 | goto retry; |
| 624 | atomic_inc(&rdev->nr_pending); |
| 625 | if (test_bit(Faulty, &rdev->flags)) { |
| 626 | /* Cannot risk returning a device that failed |
| 627 | * before we inc'ed nr_pending |
| 628 | */ |
| 629 | rdev_dec_pending(rdev, conf->mddev); |
| 630 | goto retry; |
| 631 | } |
| 632 | r10_bio->read_slot = slot; |
| 633 | } else |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 634 | disk = -1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | rcu_read_unlock(); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 636 | *max_sectors = best_good_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | return disk; |
| 639 | } |
| 640 | |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 641 | static int raid10_congested(void *data, int bits) |
| 642 | { |
| 643 | mddev_t *mddev = data; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 644 | conf_t *conf = mddev->private; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 645 | int i, ret = 0; |
| 646 | |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 647 | if (mddev_congested(mddev, bits)) |
| 648 | return 1; |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 649 | rcu_read_lock(); |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 650 | for (i = 0; i < conf->raid_disks && ret == 0; i++) { |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 651 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[i].rdev); |
| 652 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 653 | struct request_queue *q = bdev_get_queue(rdev->bdev); |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 654 | |
| 655 | ret |= bdi_congested(&q->backing_dev_info, bits); |
| 656 | } |
| 657 | } |
| 658 | rcu_read_unlock(); |
| 659 | return ret; |
| 660 | } |
| 661 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 662 | static void flush_pending_writes(conf_t *conf) |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 663 | { |
| 664 | /* Any writes that have been queued but are awaiting |
| 665 | * bitmap updates get flushed here. |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 666 | */ |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 667 | spin_lock_irq(&conf->device_lock); |
| 668 | |
| 669 | if (conf->pending_bio_list.head) { |
| 670 | struct bio *bio; |
| 671 | bio = bio_list_get(&conf->pending_bio_list); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 672 | spin_unlock_irq(&conf->device_lock); |
| 673 | /* flush any pending bitmap writes to disk |
| 674 | * before proceeding w/ I/O */ |
| 675 | bitmap_unplug(conf->mddev->bitmap); |
| 676 | |
| 677 | while (bio) { /* submit pending writes */ |
| 678 | struct bio *next = bio->bi_next; |
| 679 | bio->bi_next = NULL; |
| 680 | generic_make_request(bio); |
| 681 | bio = next; |
| 682 | } |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 683 | } else |
| 684 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 685 | } |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 686 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 687 | /* Barriers.... |
| 688 | * Sometimes we need to suspend IO while we do something else, |
| 689 | * either some resync/recovery, or reconfigure the array. |
| 690 | * To do this we raise a 'barrier'. |
| 691 | * The 'barrier' is a counter that can be raised multiple times |
| 692 | * to count how many activities are happening which preclude |
| 693 | * normal IO. |
| 694 | * We can only raise the barrier if there is no pending IO. |
| 695 | * i.e. if nr_pending == 0. |
| 696 | * We choose only to raise the barrier if no-one is waiting for the |
| 697 | * barrier to go down. This means that as soon as an IO request |
| 698 | * is ready, no other operations which require a barrier will start |
| 699 | * until the IO request has had a chance. |
| 700 | * |
| 701 | * So: regular IO calls 'wait_barrier'. When that returns there |
| 702 | * is no backgroup IO happening, It must arrange to call |
| 703 | * allow_barrier when it has finished its IO. |
| 704 | * backgroup IO calls must call raise_barrier. Once that returns |
| 705 | * there is no normal IO happeing. It must arrange to call |
| 706 | * lower_barrier when the particular background IO completes. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 707 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 709 | static void raise_barrier(conf_t *conf, int force) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 711 | BUG_ON(force && !conf->barrier); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 712 | spin_lock_irq(&conf->resync_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 714 | /* Wait until no block IO is waiting (unless 'force') */ |
| 715 | wait_event_lock_irq(conf->wait_barrier, force || !conf->nr_waiting, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 716 | conf->resync_lock, ); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 717 | |
| 718 | /* block any new IO from starting */ |
| 719 | conf->barrier++; |
| 720 | |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 721 | /* Now wait for all pending IO to complete */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 722 | wait_event_lock_irq(conf->wait_barrier, |
| 723 | !conf->nr_pending && conf->barrier < RESYNC_DEPTH, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 724 | conf->resync_lock, ); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 725 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 726 | spin_unlock_irq(&conf->resync_lock); |
| 727 | } |
| 728 | |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 729 | static void lower_barrier(conf_t *conf) |
| 730 | { |
| 731 | unsigned long flags; |
| 732 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 733 | conf->barrier--; |
| 734 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 735 | wake_up(&conf->wait_barrier); |
| 736 | } |
| 737 | |
| 738 | static void wait_barrier(conf_t *conf) |
| 739 | { |
| 740 | spin_lock_irq(&conf->resync_lock); |
| 741 | if (conf->barrier) { |
| 742 | conf->nr_waiting++; |
| 743 | wait_event_lock_irq(conf->wait_barrier, !conf->barrier, |
| 744 | conf->resync_lock, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 745 | ); |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 746 | conf->nr_waiting--; |
| 747 | } |
| 748 | conf->nr_pending++; |
| 749 | spin_unlock_irq(&conf->resync_lock); |
| 750 | } |
| 751 | |
| 752 | static void allow_barrier(conf_t *conf) |
| 753 | { |
| 754 | unsigned long flags; |
| 755 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 756 | conf->nr_pending--; |
| 757 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 758 | wake_up(&conf->wait_barrier); |
| 759 | } |
| 760 | |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 761 | static void freeze_array(conf_t *conf) |
| 762 | { |
| 763 | /* stop syncio and normal IO and wait for everything to |
NeilBrown | f188593 | 2006-01-06 00:20:42 -0800 | [diff] [blame] | 764 | * go quiet. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 765 | * We increment barrier and nr_waiting, and then |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 766 | * wait until nr_pending match nr_queued+1 |
| 767 | * This is called in the context of one normal IO request |
| 768 | * that has failed. Thus any sync request that might be pending |
| 769 | * will be blocked by nr_pending, and we need to wait for |
| 770 | * pending IO requests to complete or be queued for re-try. |
| 771 | * Thus the number queued (nr_queued) plus this request (1) |
| 772 | * must match the number of pending IOs (nr_pending) before |
| 773 | * we continue. |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 774 | */ |
| 775 | spin_lock_irq(&conf->resync_lock); |
| 776 | conf->barrier++; |
| 777 | conf->nr_waiting++; |
| 778 | wait_event_lock_irq(conf->wait_barrier, |
NeilBrown | 1c83053 | 2008-03-04 14:29:35 -0800 | [diff] [blame] | 779 | conf->nr_pending == conf->nr_queued+1, |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 780 | conf->resync_lock, |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 781 | flush_pending_writes(conf)); |
| 782 | |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 783 | spin_unlock_irq(&conf->resync_lock); |
| 784 | } |
| 785 | |
| 786 | static void unfreeze_array(conf_t *conf) |
| 787 | { |
| 788 | /* reverse the effect of the freeze */ |
| 789 | spin_lock_irq(&conf->resync_lock); |
| 790 | conf->barrier--; |
| 791 | conf->nr_waiting--; |
| 792 | wake_up(&conf->wait_barrier); |
| 793 | spin_unlock_irq(&conf->resync_lock); |
| 794 | } |
| 795 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 796 | static int make_request(mddev_t *mddev, struct bio * bio) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 797 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 798 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 799 | mirror_info_t *mirror; |
| 800 | r10bio_t *r10_bio; |
| 801 | struct bio *read_bio; |
| 802 | int i; |
| 803 | int chunk_sects = conf->chunk_mask + 1; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 804 | const int rw = bio_data_dir(bio); |
NeilBrown | 2c7d46e | 2010-08-18 16:16:05 +1000 | [diff] [blame] | 805 | const unsigned long do_sync = (bio->bi_rw & REQ_SYNC); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 806 | const unsigned long do_fua = (bio->bi_rw & REQ_FUA); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 807 | unsigned long flags; |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 808 | mdk_rdev_t *blocked_rdev; |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 809 | int plugged; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 810 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 811 | if (unlikely(bio->bi_rw & REQ_FLUSH)) { |
| 812 | md_flush_request(mddev, bio); |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 813 | return 0; |
| 814 | } |
| 815 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 816 | /* If this request crosses a chunk boundary, we need to |
| 817 | * split it. This will only happen for 1 PAGE (or less) requests. |
| 818 | */ |
| 819 | if (unlikely( (bio->bi_sector & conf->chunk_mask) + (bio->bi_size >> 9) |
| 820 | > chunk_sects && |
| 821 | conf->near_copies < conf->raid_disks)) { |
| 822 | struct bio_pair *bp; |
| 823 | /* Sanity check -- queue functions should prevent this happening */ |
| 824 | if (bio->bi_vcnt != 1 || |
| 825 | bio->bi_idx != 0) |
| 826 | goto bad_map; |
| 827 | /* This is a one page bio that upper layers |
| 828 | * refuse to split for us, so we need to split it. |
| 829 | */ |
Denis ChengRq | 6feef53 | 2008-10-09 08:57:05 +0200 | [diff] [blame] | 830 | bp = bio_split(bio, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 831 | chunk_sects - (bio->bi_sector & (chunk_sects - 1)) ); |
NeilBrown | 51e9ac7 | 2010-08-07 21:17:00 +1000 | [diff] [blame] | 832 | |
| 833 | /* Each of these 'make_request' calls will call 'wait_barrier'. |
| 834 | * If the first succeeds but the second blocks due to the resync |
| 835 | * thread raising the barrier, we will deadlock because the |
| 836 | * IO to the underlying device will be queued in generic_make_request |
| 837 | * and will never complete, so will never reduce nr_pending. |
| 838 | * So increment nr_waiting here so no new raise_barriers will |
| 839 | * succeed, and so the second wait_barrier cannot block. |
| 840 | */ |
| 841 | spin_lock_irq(&conf->resync_lock); |
| 842 | conf->nr_waiting++; |
| 843 | spin_unlock_irq(&conf->resync_lock); |
| 844 | |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 845 | if (make_request(mddev, &bp->bio1)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 846 | generic_make_request(&bp->bio1); |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 847 | if (make_request(mddev, &bp->bio2)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 848 | generic_make_request(&bp->bio2); |
| 849 | |
NeilBrown | 51e9ac7 | 2010-08-07 21:17:00 +1000 | [diff] [blame] | 850 | spin_lock_irq(&conf->resync_lock); |
| 851 | conf->nr_waiting--; |
| 852 | wake_up(&conf->wait_barrier); |
| 853 | spin_unlock_irq(&conf->resync_lock); |
| 854 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 855 | bio_pair_release(bp); |
| 856 | return 0; |
| 857 | bad_map: |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 858 | printk("md/raid10:%s: make_request bug: can't convert block across chunks" |
| 859 | " or bigger than %dk %llu %d\n", mdname(mddev), chunk_sects/2, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 860 | (unsigned long long)bio->bi_sector, bio->bi_size >> 10); |
| 861 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 862 | bio_io_error(bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 863 | return 0; |
| 864 | } |
| 865 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 866 | md_write_start(mddev, bio); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 867 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 868 | /* |
| 869 | * Register the new request and wait if the reconstruction |
| 870 | * thread has put up a bar for new requests. |
| 871 | * Continue immediately if no resync is active currently. |
| 872 | */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 873 | wait_barrier(conf); |
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 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
| 876 | |
| 877 | r10_bio->master_bio = bio; |
| 878 | r10_bio->sectors = bio->bi_size >> 9; |
| 879 | |
| 880 | r10_bio->mddev = mddev; |
| 881 | r10_bio->sector = bio->bi_sector; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 882 | r10_bio->state = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 883 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 884 | /* We might need to issue multiple reads to different |
| 885 | * devices if there are bad blocks around, so we keep |
| 886 | * track of the number of reads in bio->bi_phys_segments. |
| 887 | * If this is 0, there is only one r10_bio and no locking |
| 888 | * will be needed when the request completes. If it is |
| 889 | * non-zero, then it is the number of not-completed requests. |
| 890 | */ |
| 891 | bio->bi_phys_segments = 0; |
| 892 | clear_bit(BIO_SEG_VALID, &bio->bi_flags); |
| 893 | |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 894 | if (rw == READ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 895 | /* |
| 896 | * read balancing logic: |
| 897 | */ |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 898 | int max_sectors; |
| 899 | int disk; |
| 900 | int slot; |
| 901 | |
| 902 | read_again: |
| 903 | disk = read_balance(conf, r10_bio, &max_sectors); |
| 904 | slot = r10_bio->read_slot; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 905 | if (disk < 0) { |
| 906 | raid_end_bio_io(r10_bio); |
| 907 | return 0; |
| 908 | } |
| 909 | mirror = conf->mirrors + disk; |
| 910 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 911 | read_bio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 912 | md_trim_bio(read_bio, r10_bio->sector - bio->bi_sector, |
| 913 | max_sectors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 914 | |
| 915 | r10_bio->devs[slot].bio = read_bio; |
| 916 | |
| 917 | read_bio->bi_sector = r10_bio->devs[slot].addr + |
| 918 | mirror->rdev->data_offset; |
| 919 | read_bio->bi_bdev = mirror->rdev->bdev; |
| 920 | read_bio->bi_end_io = raid10_end_read_request; |
Christoph Hellwig | 7b6d91d | 2010-08-07 18:20:39 +0200 | [diff] [blame] | 921 | read_bio->bi_rw = READ | do_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 922 | read_bio->bi_private = r10_bio; |
| 923 | |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 924 | if (max_sectors < r10_bio->sectors) { |
| 925 | /* Could not read all from this device, so we will |
| 926 | * need another r10_bio. |
| 927 | */ |
| 928 | int sectors_handled; |
| 929 | |
| 930 | sectors_handled = (r10_bio->sectors + max_sectors |
| 931 | - bio->bi_sector); |
| 932 | r10_bio->sectors = max_sectors; |
| 933 | spin_lock_irq(&conf->device_lock); |
| 934 | if (bio->bi_phys_segments == 0) |
| 935 | bio->bi_phys_segments = 2; |
| 936 | else |
| 937 | bio->bi_phys_segments++; |
| 938 | spin_unlock(&conf->device_lock); |
| 939 | /* Cannot call generic_make_request directly |
| 940 | * as that will be queued in __generic_make_request |
| 941 | * and subsequent mempool_alloc might block |
| 942 | * waiting for it. so hand bio over to raid10d. |
| 943 | */ |
| 944 | reschedule_retry(r10_bio); |
| 945 | |
| 946 | r10_bio = mempool_alloc(conf->r10bio_pool, GFP_NOIO); |
| 947 | |
| 948 | r10_bio->master_bio = bio; |
| 949 | r10_bio->sectors = ((bio->bi_size >> 9) |
| 950 | - sectors_handled); |
| 951 | r10_bio->state = 0; |
| 952 | r10_bio->mddev = mddev; |
| 953 | r10_bio->sector = bio->bi_sector + sectors_handled; |
| 954 | goto read_again; |
| 955 | } else |
| 956 | generic_make_request(read_bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 957 | return 0; |
| 958 | } |
| 959 | |
| 960 | /* |
| 961 | * WRITE: |
| 962 | */ |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 963 | /* first select target devices under rcu_lock and |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 964 | * inc refcount on their rdev. Record them by setting |
| 965 | * bios[x] to bio |
| 966 | */ |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 967 | plugged = mddev_check_plugged(mddev); |
| 968 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 969 | raid10_find_phys(conf, r10_bio); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 970 | retry_write: |
Harvey Harrison | cb6969e | 2008-05-06 20:42:32 -0700 | [diff] [blame] | 971 | blocked_rdev = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 972 | rcu_read_lock(); |
| 973 | for (i = 0; i < conf->copies; i++) { |
| 974 | int d = r10_bio->devs[i].devnum; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 975 | mdk_rdev_t *rdev = rcu_dereference(conf->mirrors[d].rdev); |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 976 | if (rdev && unlikely(test_bit(Blocked, &rdev->flags))) { |
| 977 | atomic_inc(&rdev->nr_pending); |
| 978 | blocked_rdev = rdev; |
| 979 | break; |
| 980 | } |
| 981 | if (rdev && !test_bit(Faulty, &rdev->flags)) { |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 982 | atomic_inc(&rdev->nr_pending); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 983 | r10_bio->devs[i].bio = bio; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 984 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 985 | r10_bio->devs[i].bio = NULL; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 986 | set_bit(R10BIO_Degraded, &r10_bio->state); |
| 987 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | } |
| 989 | rcu_read_unlock(); |
| 990 | |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 991 | if (unlikely(blocked_rdev)) { |
| 992 | /* Have to wait for this device to get unblocked, then retry */ |
| 993 | int j; |
| 994 | int d; |
| 995 | |
| 996 | for (j = 0; j < i; j++) |
| 997 | if (r10_bio->devs[j].bio) { |
| 998 | d = r10_bio->devs[j].devnum; |
| 999 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); |
| 1000 | } |
| 1001 | allow_barrier(conf); |
| 1002 | md_wait_for_blocked_rdev(blocked_rdev, mddev); |
| 1003 | wait_barrier(conf); |
| 1004 | goto retry_write; |
| 1005 | } |
| 1006 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1007 | atomic_set(&r10_bio->remaining, 1); |
| 1008 | bitmap_startwrite(mddev->bitmap, bio->bi_sector, r10_bio->sectors, 0); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1009 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1010 | for (i = 0; i < conf->copies; i++) { |
| 1011 | struct bio *mbio; |
| 1012 | int d = r10_bio->devs[i].devnum; |
| 1013 | if (!r10_bio->devs[i].bio) |
| 1014 | continue; |
| 1015 | |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 1016 | mbio = bio_clone_mddev(bio, GFP_NOIO, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1017 | r10_bio->devs[i].bio = mbio; |
| 1018 | |
| 1019 | mbio->bi_sector = r10_bio->devs[i].addr+ |
| 1020 | conf->mirrors[d].rdev->data_offset; |
| 1021 | mbio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 1022 | mbio->bi_end_io = raid10_end_write_request; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1023 | mbio->bi_rw = WRITE | do_sync | do_fua; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1024 | mbio->bi_private = r10_bio; |
| 1025 | |
| 1026 | atomic_inc(&r10_bio->remaining); |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1027 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1028 | bio_list_add(&conf->pending_bio_list, mbio); |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1029 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1030 | } |
| 1031 | |
NeilBrown | 4e78064 | 2010-10-19 12:54:01 +1100 | [diff] [blame] | 1032 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1033 | /* This matches the end of raid10_end_write_request() */ |
| 1034 | bitmap_endwrite(r10_bio->mddev->bitmap, r10_bio->sector, |
| 1035 | r10_bio->sectors, |
| 1036 | !test_bit(R10BIO_Degraded, &r10_bio->state), |
| 1037 | 0); |
Arne Redlich | f6f953a | 2007-07-31 00:37:57 -0700 | [diff] [blame] | 1038 | md_write_end(mddev); |
| 1039 | raid_end_bio_io(r10_bio); |
Arne Redlich | f6f953a | 2007-07-31 00:37:57 -0700 | [diff] [blame] | 1040 | } |
| 1041 | |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1042 | /* In case raid10d snuck in to freeze_array */ |
| 1043 | wake_up(&conf->wait_barrier); |
| 1044 | |
NeilBrown | c3b328a | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 1045 | if (do_sync || !mddev->bitmap || !plugged) |
Lars Ellenberg | e3881a6 | 2007-01-10 23:15:37 -0800 | [diff] [blame] | 1046 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1047 | return 0; |
| 1048 | } |
| 1049 | |
| 1050 | static void status(struct seq_file *seq, mddev_t *mddev) |
| 1051 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1052 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1053 | int i; |
| 1054 | |
| 1055 | if (conf->near_copies < conf->raid_disks) |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 1056 | seq_printf(seq, " %dK chunks", mddev->chunk_sectors / 2); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1057 | if (conf->near_copies > 1) |
| 1058 | seq_printf(seq, " %d near-copies", conf->near_copies); |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 1059 | if (conf->far_copies > 1) { |
| 1060 | if (conf->far_offset) |
| 1061 | seq_printf(seq, " %d offset-copies", conf->far_copies); |
| 1062 | else |
| 1063 | seq_printf(seq, " %d far-copies", conf->far_copies); |
| 1064 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1065 | seq_printf(seq, " [%d/%d] [", conf->raid_disks, |
NeilBrown | 76186dd | 2006-10-03 01:15:48 -0700 | [diff] [blame] | 1066 | conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | for (i = 0; i < conf->raid_disks; i++) |
| 1068 | seq_printf(seq, "%s", |
| 1069 | conf->mirrors[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1070 | test_bit(In_sync, &conf->mirrors[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1071 | seq_printf(seq, "]"); |
| 1072 | } |
| 1073 | |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1074 | /* check if there are enough drives for |
| 1075 | * every block to appear on atleast one. |
| 1076 | * Don't consider the device numbered 'ignore' |
| 1077 | * as we might be about to remove it. |
| 1078 | */ |
| 1079 | static int enough(conf_t *conf, int ignore) |
| 1080 | { |
| 1081 | int first = 0; |
| 1082 | |
| 1083 | do { |
| 1084 | int n = conf->copies; |
| 1085 | int cnt = 0; |
| 1086 | while (n--) { |
| 1087 | if (conf->mirrors[first].rdev && |
| 1088 | first != ignore) |
| 1089 | cnt++; |
| 1090 | first = (first+1) % conf->raid_disks; |
| 1091 | } |
| 1092 | if (cnt == 0) |
| 1093 | return 0; |
| 1094 | } while (first != 0); |
| 1095 | return 1; |
| 1096 | } |
| 1097 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1098 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1099 | { |
| 1100 | char b[BDEVNAME_SIZE]; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1101 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1102 | |
| 1103 | /* |
| 1104 | * If it is not operational, then we have already marked it as dead |
| 1105 | * else if it is the last working disks, ignore the error, let the |
| 1106 | * next level up know. |
| 1107 | * else mark the drive as failed |
| 1108 | */ |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1109 | if (test_bit(In_sync, &rdev->flags) |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1110 | && !enough(conf, rdev->raid_disk)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1111 | /* |
| 1112 | * Don't fail the drive, just return an IO error. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1113 | */ |
| 1114 | return; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1115 | if (test_and_clear_bit(In_sync, &rdev->flags)) { |
| 1116 | unsigned long flags; |
| 1117 | spin_lock_irqsave(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1118 | mddev->degraded++; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1119 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1120 | /* |
| 1121 | * if recovery is running, make sure it aborts. |
| 1122 | */ |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1123 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1124 | } |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1125 | set_bit(Blocked, &rdev->flags); |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1126 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 1127 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
Joe Perches | 067032b | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1128 | printk(KERN_ALERT |
| 1129 | "md/raid10:%s: Disk failure on %s, disabling device.\n" |
| 1130 | "md/raid10:%s: Operation continuing on %d devices.\n", |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1131 | mdname(mddev), bdevname(rdev->bdev, b), |
| 1132 | mdname(mddev), conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | static void print_conf(conf_t *conf) |
| 1136 | { |
| 1137 | int i; |
| 1138 | mirror_info_t *tmp; |
| 1139 | |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1140 | printk(KERN_DEBUG "RAID10 conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1141 | if (!conf) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1142 | printk(KERN_DEBUG "(!conf)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1143 | return; |
| 1144 | } |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1145 | 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] | 1146 | conf->raid_disks); |
| 1147 | |
| 1148 | for (i = 0; i < conf->raid_disks; i++) { |
| 1149 | char b[BDEVNAME_SIZE]; |
| 1150 | tmp = conf->mirrors + i; |
| 1151 | if (tmp->rdev) |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 1152 | printk(KERN_DEBUG " disk %d, wo:%d, o:%d, dev:%s\n", |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1153 | i, !test_bit(In_sync, &tmp->rdev->flags), |
| 1154 | !test_bit(Faulty, &tmp->rdev->flags), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1155 | bdevname(tmp->rdev->bdev,b)); |
| 1156 | } |
| 1157 | } |
| 1158 | |
| 1159 | static void close_sync(conf_t *conf) |
| 1160 | { |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1161 | wait_barrier(conf); |
| 1162 | allow_barrier(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1163 | |
| 1164 | mempool_destroy(conf->r10buf_pool); |
| 1165 | conf->r10buf_pool = NULL; |
| 1166 | } |
| 1167 | |
| 1168 | static int raid10_spare_active(mddev_t *mddev) |
| 1169 | { |
| 1170 | int i; |
| 1171 | conf_t *conf = mddev->private; |
| 1172 | mirror_info_t *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1173 | int count = 0; |
| 1174 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1175 | |
| 1176 | /* |
| 1177 | * Find all non-in_sync disks within the RAID10 configuration |
| 1178 | * and mark them in_sync |
| 1179 | */ |
| 1180 | for (i = 0; i < conf->raid_disks; i++) { |
| 1181 | tmp = conf->mirrors + i; |
| 1182 | if (tmp->rdev |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1183 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1184 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1185 | count++; |
Adrian Drzewiecki | e6ffbcb | 2010-08-18 11:49:02 +1000 | [diff] [blame] | 1186 | sysfs_notify_dirent(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1187 | } |
| 1188 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1189 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1190 | mddev->degraded -= count; |
| 1191 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1192 | |
| 1193 | print_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 1194 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | static int raid10_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1199 | { |
| 1200 | conf_t *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1201 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1202 | int mirror; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1203 | int first = 0; |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 1204 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1205 | |
NeilBrown | 34b343c | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 1206 | if (rdev->badblocks.count) |
| 1207 | return -EINVAL; |
| 1208 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1209 | if (mddev->recovery_cp < MaxSector) |
| 1210 | /* only hot-add to in-sync arrays, as recovery is |
| 1211 | * very different from resync |
| 1212 | */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1213 | return -EBUSY; |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1214 | if (!enough(conf, -1)) |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1215 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1216 | |
NeilBrown | a53a6c8 | 2008-11-06 17:28:20 +1100 | [diff] [blame] | 1217 | if (rdev->raid_disk >= 0) |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1218 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1219 | |
Namhyung Kim | 2c4193d | 2011-07-18 17:38:43 +1000 | [diff] [blame] | 1220 | if (rdev->saved_raid_disk >= first && |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1221 | conf->mirrors[rdev->saved_raid_disk].rdev == NULL) |
| 1222 | mirror = rdev->saved_raid_disk; |
| 1223 | else |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 1224 | mirror = first; |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1225 | for ( ; mirror <= last ; mirror++) { |
| 1226 | mirror_info_t *p = &conf->mirrors[mirror]; |
| 1227 | if (p->recovery_disabled == mddev->recovery_disabled) |
| 1228 | continue; |
| 1229 | if (!p->rdev) |
| 1230 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1231 | |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1232 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 1233 | rdev->data_offset << 9); |
| 1234 | /* as we don't honour merge_bvec_fn, we must |
| 1235 | * never risk violating it, so limit |
| 1236 | * ->max_segments to one lying with a single |
| 1237 | * page, as a one page request is never in |
| 1238 | * violation. |
| 1239 | */ |
| 1240 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { |
| 1241 | blk_queue_max_segments(mddev->queue, 1); |
| 1242 | blk_queue_segment_boundary(mddev->queue, |
| 1243 | PAGE_CACHE_SIZE - 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1244 | } |
| 1245 | |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1246 | p->head_position = 0; |
| 1247 | rdev->raid_disk = mirror; |
| 1248 | err = 0; |
| 1249 | if (rdev->saved_raid_disk != mirror) |
| 1250 | conf->fullsync = 1; |
| 1251 | rcu_assign_pointer(p->rdev, rdev); |
| 1252 | break; |
| 1253 | } |
| 1254 | |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1255 | md_integrity_add_rdev(rdev, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1256 | print_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 1257 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1258 | } |
| 1259 | |
| 1260 | static int raid10_remove_disk(mddev_t *mddev, int number) |
| 1261 | { |
| 1262 | conf_t *conf = mddev->private; |
| 1263 | int err = 0; |
| 1264 | mdk_rdev_t *rdev; |
| 1265 | mirror_info_t *p = conf->mirrors+ number; |
| 1266 | |
| 1267 | print_conf(conf); |
| 1268 | rdev = p->rdev; |
| 1269 | if (rdev) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1270 | if (test_bit(In_sync, &rdev->flags) || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1271 | atomic_read(&rdev->nr_pending)) { |
| 1272 | err = -EBUSY; |
| 1273 | goto abort; |
| 1274 | } |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1275 | /* Only remove faulty devices in recovery |
| 1276 | * is not possible. |
| 1277 | */ |
| 1278 | if (!test_bit(Faulty, &rdev->flags) && |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1279 | mddev->recovery_disabled != p->recovery_disabled && |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1280 | enough(conf, -1)) { |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1281 | err = -EBUSY; |
| 1282 | goto abort; |
| 1283 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1284 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 1285 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1286 | if (atomic_read(&rdev->nr_pending)) { |
| 1287 | /* lost the race, try later */ |
| 1288 | err = -EBUSY; |
| 1289 | p->rdev = rdev; |
Andre Noll | ac5e711 | 2009-08-03 10:59:47 +1000 | [diff] [blame] | 1290 | goto abort; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1291 | } |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 1292 | err = md_integrity_register(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1293 | } |
| 1294 | abort: |
| 1295 | |
| 1296 | print_conf(conf); |
| 1297 | return err; |
| 1298 | } |
| 1299 | |
| 1300 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1301 | static void end_sync_read(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1302 | { |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 1303 | r10bio_t *r10_bio = bio->bi_private; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1304 | conf_t *conf = r10_bio->mddev->private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 1305 | int d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1306 | |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 1307 | d = find_bio_disk(conf, r10_bio, bio); |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1308 | |
| 1309 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 1310 | set_bit(R10BIO_Uptodate, &r10_bio->state); |
NeilBrown | 4dbcdc7 | 2006-01-06 00:20:52 -0800 | [diff] [blame] | 1311 | else { |
| 1312 | atomic_add(r10_bio->sectors, |
| 1313 | &conf->mirrors[d].rdev->corrected_errors); |
| 1314 | if (!test_bit(MD_RECOVERY_SYNC, &conf->mddev->recovery)) |
| 1315 | md_error(r10_bio->mddev, |
| 1316 | conf->mirrors[d].rdev); |
| 1317 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1318 | |
| 1319 | /* for reconstruct, we always reschedule after a read. |
| 1320 | * for resync, only after all reads |
| 1321 | */ |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1322 | rdev_dec_pending(conf->mirrors[d].rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1323 | if (test_bit(R10BIO_IsRecover, &r10_bio->state) || |
| 1324 | atomic_dec_and_test(&r10_bio->remaining)) { |
| 1325 | /* we have read all the blocks, |
| 1326 | * do the comparison in process context in raid10d |
| 1327 | */ |
| 1328 | reschedule_retry(r10_bio); |
| 1329 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1332 | static void end_sync_write(struct bio *bio, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1333 | { |
| 1334 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
H Hartley Sweeten | 7b92813 | 2010-03-08 16:02:40 +1100 | [diff] [blame] | 1335 | r10bio_t *r10_bio = bio->bi_private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1336 | mddev_t *mddev = r10_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1337 | conf_t *conf = mddev->private; |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 1338 | int d; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1339 | |
Namhyung Kim | 778ca01 | 2011-07-18 17:38:47 +1000 | [diff] [blame] | 1340 | d = find_bio_disk(conf, r10_bio, bio); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1341 | |
| 1342 | if (!uptodate) |
| 1343 | md_error(mddev, conf->mirrors[d].rdev); |
NeilBrown | dfc7064 | 2008-05-23 13:04:39 -0700 | [diff] [blame] | 1344 | |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1345 | rdev_dec_pending(conf->mirrors[d].rdev, mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | while (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1347 | if (r10_bio->master_bio == NULL) { |
| 1348 | /* the primary of several recovery bios */ |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1349 | sector_t s = r10_bio->sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1350 | put_buf(r10_bio); |
NeilBrown | 73d5c38 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 1351 | md_done_sync(mddev, s, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1352 | break; |
| 1353 | } else { |
| 1354 | r10bio_t *r10_bio2 = (r10bio_t *)r10_bio->master_bio; |
| 1355 | put_buf(r10_bio); |
| 1356 | r10_bio = r10_bio2; |
| 1357 | } |
| 1358 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1359 | } |
| 1360 | |
| 1361 | /* |
| 1362 | * Note: sync and recover and handled very differently for raid10 |
| 1363 | * This code is for resync. |
| 1364 | * For resync, we read through virtual addresses and read all blocks. |
| 1365 | * If there is any error, we schedule a write. The lowest numbered |
| 1366 | * drive is authoritative. |
| 1367 | * However requests come for physical address, so we need to map. |
| 1368 | * For every physical address there are raid_disks/copies virtual addresses, |
| 1369 | * which is always are least one, but is not necessarly an integer. |
| 1370 | * This means that a physical address can span multiple chunks, so we may |
| 1371 | * have to submit multiple io requests for a single sync request. |
| 1372 | */ |
| 1373 | /* |
| 1374 | * We check if all blocks are in-sync and only write to blocks that |
| 1375 | * aren't in sync |
| 1376 | */ |
| 1377 | static void sync_request_write(mddev_t *mddev, r10bio_t *r10_bio) |
| 1378 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1379 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1380 | int i, first; |
| 1381 | struct bio *tbio, *fbio; |
| 1382 | |
| 1383 | atomic_set(&r10_bio->remaining, 1); |
| 1384 | |
| 1385 | /* find the first device with a block */ |
| 1386 | for (i=0; i<conf->copies; i++) |
| 1387 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) |
| 1388 | break; |
| 1389 | |
| 1390 | if (i == conf->copies) |
| 1391 | goto done; |
| 1392 | |
| 1393 | first = i; |
| 1394 | fbio = r10_bio->devs[i].bio; |
| 1395 | |
| 1396 | /* now find blocks with errors */ |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1397 | for (i=0 ; i < conf->copies ; i++) { |
| 1398 | int j, d; |
| 1399 | int vcnt = r10_bio->sectors >> (PAGE_SHIFT-9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1400 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1401 | tbio = r10_bio->devs[i].bio; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1402 | |
| 1403 | if (tbio->bi_end_io != end_sync_read) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1404 | continue; |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1405 | if (i == first) |
| 1406 | continue; |
| 1407 | if (test_bit(BIO_UPTODATE, &r10_bio->devs[i].bio->bi_flags)) { |
| 1408 | /* We know that the bi_io_vec layout is the same for |
| 1409 | * both 'first' and 'i', so we just compare them. |
| 1410 | * All vec entries are PAGE_SIZE; |
| 1411 | */ |
| 1412 | for (j = 0; j < vcnt; j++) |
| 1413 | if (memcmp(page_address(fbio->bi_io_vec[j].bv_page), |
| 1414 | page_address(tbio->bi_io_vec[j].bv_page), |
| 1415 | PAGE_SIZE)) |
| 1416 | break; |
| 1417 | if (j == vcnt) |
| 1418 | continue; |
| 1419 | mddev->resync_mismatches += r10_bio->sectors; |
| 1420 | } |
NeilBrown | 18f0881 | 2006-01-06 00:20:25 -0800 | [diff] [blame] | 1421 | if (test_bit(MD_RECOVERY_CHECK, &mddev->recovery)) |
| 1422 | /* Don't fix anything. */ |
| 1423 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1424 | /* Ok, we need to write this bio |
| 1425 | * First we need to fixup bv_offset, bv_len and |
| 1426 | * bi_vecs, as the read request might have corrupted these |
| 1427 | */ |
| 1428 | tbio->bi_vcnt = vcnt; |
| 1429 | tbio->bi_size = r10_bio->sectors << 9; |
| 1430 | tbio->bi_idx = 0; |
| 1431 | tbio->bi_phys_segments = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1432 | tbio->bi_flags &= ~(BIO_POOL_MASK - 1); |
| 1433 | tbio->bi_flags |= 1 << BIO_UPTODATE; |
| 1434 | tbio->bi_next = NULL; |
| 1435 | tbio->bi_rw = WRITE; |
| 1436 | tbio->bi_private = r10_bio; |
| 1437 | tbio->bi_sector = r10_bio->devs[i].addr; |
| 1438 | |
| 1439 | for (j=0; j < vcnt ; j++) { |
| 1440 | tbio->bi_io_vec[j].bv_offset = 0; |
| 1441 | tbio->bi_io_vec[j].bv_len = PAGE_SIZE; |
| 1442 | |
| 1443 | memcpy(page_address(tbio->bi_io_vec[j].bv_page), |
| 1444 | page_address(fbio->bi_io_vec[j].bv_page), |
| 1445 | PAGE_SIZE); |
| 1446 | } |
| 1447 | tbio->bi_end_io = end_sync_write; |
| 1448 | |
| 1449 | d = r10_bio->devs[i].devnum; |
| 1450 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 1451 | atomic_inc(&r10_bio->remaining); |
| 1452 | md_sync_acct(conf->mirrors[d].rdev->bdev, tbio->bi_size >> 9); |
| 1453 | |
| 1454 | tbio->bi_sector += conf->mirrors[d].rdev->data_offset; |
| 1455 | tbio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 1456 | generic_make_request(tbio); |
| 1457 | } |
| 1458 | |
| 1459 | done: |
| 1460 | if (atomic_dec_and_test(&r10_bio->remaining)) { |
| 1461 | md_done_sync(mddev, r10_bio->sectors, 1); |
| 1462 | put_buf(r10_bio); |
| 1463 | } |
| 1464 | } |
| 1465 | |
| 1466 | /* |
| 1467 | * Now for the recovery code. |
| 1468 | * Recovery happens across physical sectors. |
| 1469 | * We recover all non-is_sync drives by finding the virtual address of |
| 1470 | * each, and then choose a working drive that also has that virt address. |
| 1471 | * There is a separate r10_bio for each non-in_sync drive. |
| 1472 | * Only the first two slots are in use. The first for reading, |
| 1473 | * The second for writing. |
| 1474 | * |
| 1475 | */ |
| 1476 | |
| 1477 | static void recovery_request_write(mddev_t *mddev, r10bio_t *r10_bio) |
| 1478 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1479 | conf_t *conf = mddev->private; |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 1480 | int d; |
| 1481 | struct bio *wbio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | |
Namhyung Kim | c65060a | 2011-07-18 17:38:49 +1000 | [diff] [blame] | 1483 | /* |
| 1484 | * share the pages with the first bio |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1485 | * and submit the write request |
| 1486 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1487 | wbio = r10_bio->devs[1].bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1488 | d = r10_bio->devs[1].devnum; |
| 1489 | |
| 1490 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 1491 | md_sync_acct(conf->mirrors[d].rdev->bdev, wbio->bi_size >> 9); |
NeilBrown | 0eb3ff1 | 2006-01-06 00:20:29 -0800 | [diff] [blame] | 1492 | if (test_bit(R10BIO_Uptodate, &r10_bio->state)) |
| 1493 | generic_make_request(wbio); |
NeilBrown | 2bb7773 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1494 | else { |
| 1495 | printk(KERN_NOTICE |
| 1496 | "md/raid10:%s: recovery aborted due to read error\n", |
| 1497 | mdname(mddev)); |
| 1498 | conf->mirrors[d].recovery_disabled = mddev->recovery_disabled; |
| 1499 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1500 | bio_endio(wbio, 0); |
| 1501 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | } |
| 1503 | |
| 1504 | |
| 1505 | /* |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 1506 | * Used by fix_read_error() to decay the per rdev read_errors. |
| 1507 | * We halve the read error count for every hour that has elapsed |
| 1508 | * since the last recorded read error. |
| 1509 | * |
| 1510 | */ |
| 1511 | static void check_decay_read_errors(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1512 | { |
| 1513 | struct timespec cur_time_mon; |
| 1514 | unsigned long hours_since_last; |
| 1515 | unsigned int read_errors = atomic_read(&rdev->read_errors); |
| 1516 | |
| 1517 | ktime_get_ts(&cur_time_mon); |
| 1518 | |
| 1519 | if (rdev->last_read_error.tv_sec == 0 && |
| 1520 | rdev->last_read_error.tv_nsec == 0) { |
| 1521 | /* first time we've seen a read error */ |
| 1522 | rdev->last_read_error = cur_time_mon; |
| 1523 | return; |
| 1524 | } |
| 1525 | |
| 1526 | hours_since_last = (cur_time_mon.tv_sec - |
| 1527 | rdev->last_read_error.tv_sec) / 3600; |
| 1528 | |
| 1529 | rdev->last_read_error = cur_time_mon; |
| 1530 | |
| 1531 | /* |
| 1532 | * if hours_since_last is > the number of bits in read_errors |
| 1533 | * just set read errors to 0. We do this to avoid |
| 1534 | * overflowing the shift of read_errors by hours_since_last. |
| 1535 | */ |
| 1536 | if (hours_since_last >= 8 * sizeof(read_errors)) |
| 1537 | atomic_set(&rdev->read_errors, 0); |
| 1538 | else |
| 1539 | atomic_set(&rdev->read_errors, read_errors >> hours_since_last); |
| 1540 | } |
| 1541 | |
| 1542 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1543 | * This is a kernel thread which: |
| 1544 | * |
| 1545 | * 1. Retries failed read operations on working mirrors. |
| 1546 | * 2. Updates the raid superblock when problems encounter. |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1547 | * 3. Performs writes following reads for array synchronising. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | */ |
| 1549 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1550 | static void fix_read_error(conf_t *conf, mddev_t *mddev, r10bio_t *r10_bio) |
| 1551 | { |
| 1552 | int sect = 0; /* Offset from r10_bio->sector */ |
| 1553 | int sectors = r10_bio->sectors; |
| 1554 | mdk_rdev_t*rdev; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 1555 | int max_read_errors = atomic_read(&mddev->max_corr_read_errors); |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 1556 | int d = r10_bio->devs[r10_bio->read_slot].devnum; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 1557 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 1558 | /* still own a reference to this rdev, so it cannot |
| 1559 | * have been cleared recently. |
| 1560 | */ |
| 1561 | rdev = conf->mirrors[d].rdev; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 1562 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 1563 | if (test_bit(Faulty, &rdev->flags)) |
| 1564 | /* drive has already been failed, just ignore any |
| 1565 | more fix_read_error() attempts */ |
| 1566 | return; |
| 1567 | |
| 1568 | check_decay_read_errors(mddev, rdev); |
| 1569 | atomic_inc(&rdev->read_errors); |
| 1570 | if (atomic_read(&rdev->read_errors) > max_read_errors) { |
| 1571 | char b[BDEVNAME_SIZE]; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 1572 | bdevname(rdev->bdev, b); |
| 1573 | |
NeilBrown | 7c4e06f | 2011-05-11 14:53:17 +1000 | [diff] [blame] | 1574 | printk(KERN_NOTICE |
| 1575 | "md/raid10:%s: %s: Raid device exceeded " |
| 1576 | "read_error threshold [cur %d:max %d]\n", |
| 1577 | mdname(mddev), b, |
| 1578 | atomic_read(&rdev->read_errors), max_read_errors); |
| 1579 | printk(KERN_NOTICE |
| 1580 | "md/raid10:%s: %s: Failing raid device\n", |
| 1581 | mdname(mddev), b); |
| 1582 | md_error(mddev, conf->mirrors[d].rdev); |
| 1583 | return; |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 1584 | } |
Robert Becker | 1e50915 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 1585 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1586 | while(sectors) { |
| 1587 | int s = sectors; |
| 1588 | int sl = r10_bio->read_slot; |
| 1589 | int success = 0; |
| 1590 | int start; |
| 1591 | |
| 1592 | if (s > (PAGE_SIZE>>9)) |
| 1593 | s = PAGE_SIZE >> 9; |
| 1594 | |
| 1595 | rcu_read_lock(); |
| 1596 | do { |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 1597 | d = r10_bio->devs[sl].devnum; |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1598 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
| 1599 | if (rdev && |
| 1600 | test_bit(In_sync, &rdev->flags)) { |
| 1601 | atomic_inc(&rdev->nr_pending); |
| 1602 | rcu_read_unlock(); |
NeilBrown | 2b19336 | 2010-10-27 15:16:40 +1100 | [diff] [blame] | 1603 | success = sync_page_io(rdev, |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1604 | r10_bio->devs[sl].addr + |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1605 | sect, |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1606 | s<<9, |
Jonathan Brassow | ccebd4c | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 1607 | conf->tmppage, READ, false); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1608 | rdev_dec_pending(rdev, mddev); |
| 1609 | rcu_read_lock(); |
| 1610 | if (success) |
| 1611 | break; |
| 1612 | } |
| 1613 | sl++; |
| 1614 | if (sl == conf->copies) |
| 1615 | sl = 0; |
| 1616 | } while (!success && sl != r10_bio->read_slot); |
| 1617 | rcu_read_unlock(); |
| 1618 | |
| 1619 | if (!success) { |
| 1620 | /* Cannot read from anywhere -- bye bye array */ |
| 1621 | int dn = r10_bio->devs[r10_bio->read_slot].devnum; |
| 1622 | md_error(mddev, conf->mirrors[dn].rdev); |
| 1623 | break; |
| 1624 | } |
| 1625 | |
| 1626 | start = sl; |
| 1627 | /* write it back and re-read */ |
| 1628 | rcu_read_lock(); |
| 1629 | while (sl != r10_bio->read_slot) { |
Robert Becker | 67b8dc4 | 2009-12-14 12:49:57 +1100 | [diff] [blame] | 1630 | char b[BDEVNAME_SIZE]; |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 1631 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1632 | if (sl==0) |
| 1633 | sl = conf->copies; |
| 1634 | sl--; |
| 1635 | d = r10_bio->devs[sl].devnum; |
| 1636 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1637 | if (!rdev || |
| 1638 | !test_bit(In_sync, &rdev->flags)) |
| 1639 | continue; |
| 1640 | |
| 1641 | atomic_inc(&rdev->nr_pending); |
| 1642 | rcu_read_unlock(); |
| 1643 | if (sync_page_io(rdev, |
| 1644 | r10_bio->devs[sl].addr + |
| 1645 | sect, |
| 1646 | s<<9, conf->tmppage, WRITE, false) |
| 1647 | == 0) { |
| 1648 | /* Well, this device is dead */ |
| 1649 | printk(KERN_NOTICE |
| 1650 | "md/raid10:%s: read correction " |
| 1651 | "write failed" |
| 1652 | " (%d sectors at %llu on %s)\n", |
| 1653 | mdname(mddev), s, |
| 1654 | (unsigned long long)( |
| 1655 | sect + rdev->data_offset), |
| 1656 | bdevname(rdev->bdev, b)); |
| 1657 | printk(KERN_NOTICE "md/raid10:%s: %s: failing " |
| 1658 | "drive\n", |
| 1659 | mdname(mddev), |
| 1660 | bdevname(rdev->bdev, b)); |
| 1661 | md_error(mddev, rdev); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1662 | } |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1663 | rdev_dec_pending(rdev, mddev); |
| 1664 | rcu_read_lock(); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1665 | } |
| 1666 | sl = start; |
| 1667 | while (sl != r10_bio->read_slot) { |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1668 | char b[BDEVNAME_SIZE]; |
Prasanna S. Panchamukhi | 0544a21 | 2010-06-24 13:31:03 +1000 | [diff] [blame] | 1669 | |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1670 | if (sl==0) |
| 1671 | sl = conf->copies; |
| 1672 | sl--; |
| 1673 | d = r10_bio->devs[sl].devnum; |
| 1674 | rdev = rcu_dereference(conf->mirrors[d].rdev); |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1675 | if (!rdev || |
| 1676 | !test_bit(In_sync, &rdev->flags)) |
| 1677 | continue; |
Robert Becker | 67b8dc4 | 2009-12-14 12:49:57 +1100 | [diff] [blame] | 1678 | |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1679 | atomic_inc(&rdev->nr_pending); |
| 1680 | rcu_read_unlock(); |
| 1681 | if (sync_page_io(rdev, |
| 1682 | r10_bio->devs[sl].addr + |
| 1683 | sect, |
| 1684 | s<<9, conf->tmppage, |
| 1685 | READ, false) == 0) { |
| 1686 | /* Well, this device is dead */ |
| 1687 | printk(KERN_NOTICE |
| 1688 | "md/raid10:%s: unable to read back " |
| 1689 | "corrected sectors" |
| 1690 | " (%d sectors at %llu on %s)\n", |
| 1691 | mdname(mddev), s, |
| 1692 | (unsigned long long)( |
| 1693 | sect + rdev->data_offset), |
| 1694 | bdevname(rdev->bdev, b)); |
| 1695 | printk(KERN_NOTICE "md/raid10:%s: %s: failing " |
| 1696 | "drive\n", |
| 1697 | mdname(mddev), |
| 1698 | bdevname(rdev->bdev, b)); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1699 | |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1700 | md_error(mddev, rdev); |
| 1701 | } else { |
| 1702 | printk(KERN_INFO |
| 1703 | "md/raid10:%s: read error corrected" |
| 1704 | " (%d sectors at %llu on %s)\n", |
| 1705 | mdname(mddev), s, |
| 1706 | (unsigned long long)( |
| 1707 | sect + rdev->data_offset), |
| 1708 | bdevname(rdev->bdev, b)); |
| 1709 | atomic_add(s, &rdev->corrected_errors); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1710 | } |
NeilBrown | 1294b9c | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1711 | |
| 1712 | rdev_dec_pending(rdev, mddev); |
| 1713 | rcu_read_lock(); |
NeilBrown | 6814d53 | 2006-10-03 01:15:45 -0700 | [diff] [blame] | 1714 | } |
| 1715 | rcu_read_unlock(); |
| 1716 | |
| 1717 | sectors -= s; |
| 1718 | sect += s; |
| 1719 | } |
| 1720 | } |
| 1721 | |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1722 | static void handle_read_error(mddev_t *mddev, r10bio_t *r10_bio) |
| 1723 | { |
| 1724 | int slot = r10_bio->read_slot; |
| 1725 | int mirror = r10_bio->devs[slot].devnum; |
| 1726 | struct bio *bio; |
| 1727 | conf_t *conf = mddev->private; |
| 1728 | mdk_rdev_t *rdev; |
| 1729 | char b[BDEVNAME_SIZE]; |
| 1730 | unsigned long do_sync; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 1731 | int max_sectors; |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1732 | |
| 1733 | /* we got a read error. Maybe the drive is bad. Maybe just |
| 1734 | * the block and we can fix it. |
| 1735 | * We freeze all other IO, and try reading the block from |
| 1736 | * other devices. When we find one, we re-write |
| 1737 | * and check it that fixes the read error. |
| 1738 | * This is all done synchronously while the array is |
| 1739 | * frozen. |
| 1740 | */ |
| 1741 | if (mddev->ro == 0) { |
| 1742 | freeze_array(conf); |
| 1743 | fix_read_error(conf, mddev, r10_bio); |
| 1744 | unfreeze_array(conf); |
| 1745 | } |
| 1746 | rdev_dec_pending(conf->mirrors[mirror].rdev, mddev); |
| 1747 | |
| 1748 | bio = r10_bio->devs[slot].bio; |
| 1749 | r10_bio->devs[slot].bio = |
| 1750 | mddev->ro ? IO_BLOCKED : NULL; |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 1751 | mirror = read_balance(conf, r10_bio, &max_sectors); |
| 1752 | if (mirror == -1 || max_sectors < r10_bio->sectors) { |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1753 | printk(KERN_ALERT "md/raid10:%s: %s: unrecoverable I/O" |
| 1754 | " read error for block %llu\n", |
| 1755 | mdname(mddev), |
| 1756 | bdevname(bio->bi_bdev, b), |
| 1757 | (unsigned long long)r10_bio->sector); |
| 1758 | raid_end_bio_io(r10_bio); |
| 1759 | bio_put(bio); |
| 1760 | return; |
| 1761 | } |
| 1762 | |
| 1763 | do_sync = (r10_bio->master_bio->bi_rw & REQ_SYNC); |
| 1764 | bio_put(bio); |
| 1765 | slot = r10_bio->read_slot; |
| 1766 | rdev = conf->mirrors[mirror].rdev; |
| 1767 | printk_ratelimited( |
| 1768 | KERN_ERR |
| 1769 | "md/raid10:%s: %s: redirecting" |
| 1770 | "sector %llu to another mirror\n", |
| 1771 | mdname(mddev), |
| 1772 | bdevname(rdev->bdev, b), |
| 1773 | (unsigned long long)r10_bio->sector); |
| 1774 | bio = bio_clone_mddev(r10_bio->master_bio, |
| 1775 | GFP_NOIO, mddev); |
| 1776 | r10_bio->devs[slot].bio = bio; |
| 1777 | bio->bi_sector = r10_bio->devs[slot].addr |
| 1778 | + rdev->data_offset; |
| 1779 | bio->bi_bdev = rdev->bdev; |
| 1780 | bio->bi_rw = READ | do_sync; |
| 1781 | bio->bi_private = r10_bio; |
| 1782 | bio->bi_end_io = raid10_end_read_request; |
| 1783 | generic_make_request(bio); |
| 1784 | } |
| 1785 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1786 | static void raid10d(mddev_t *mddev) |
| 1787 | { |
| 1788 | r10bio_t *r10_bio; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1789 | unsigned long flags; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1790 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1791 | struct list_head *head = &conf->retry_list; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 1792 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1793 | |
| 1794 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1795 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 1796 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1797 | for (;;) { |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1798 | |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1799 | flush_pending_writes(conf); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1800 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1801 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1802 | if (list_empty(head)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1803 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1804 | break; |
NeilBrown | a35e63e | 2008-03-04 14:29:29 -0800 | [diff] [blame] | 1805 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1806 | r10_bio = list_entry(head->prev, r10bio_t, retry_list); |
| 1807 | list_del(head->prev); |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1808 | conf->nr_queued--; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1809 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1810 | |
| 1811 | mddev = r10_bio->mddev; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1812 | conf = mddev->private; |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1813 | if (test_bit(R10BIO_IsSync, &r10_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | sync_request_write(mddev, r10_bio); |
Jens Axboe | 7eaceac | 2011-03-10 08:52:07 +0100 | [diff] [blame] | 1815 | else if (test_bit(R10BIO_IsRecover, &r10_bio->state)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1816 | recovery_request_write(mddev, r10_bio); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 1817 | else if (test_bit(R10BIO_ReadError, &r10_bio->state)) |
NeilBrown | 560f8e5 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1818 | handle_read_error(mddev, r10_bio); |
NeilBrown | 856e08e | 2011-07-28 11:39:23 +1000 | [diff] [blame^] | 1819 | else { |
| 1820 | /* just a partial read to be scheduled from a |
| 1821 | * separate context |
| 1822 | */ |
| 1823 | int slot = r10_bio->read_slot; |
| 1824 | generic_make_request(r10_bio->devs[slot].bio); |
| 1825 | } |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 1826 | |
NeilBrown | 1d9d524 | 2009-10-16 15:55:32 +1100 | [diff] [blame] | 1827 | cond_resched(); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1828 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) |
| 1829 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | } |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 1831 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | |
| 1835 | static int init_resync(conf_t *conf) |
| 1836 | { |
| 1837 | int buffs; |
| 1838 | |
| 1839 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; |
Eric Sesterhenn | b638548 | 2006-04-02 13:34:29 +0200 | [diff] [blame] | 1840 | BUG_ON(conf->r10buf_pool); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | conf->r10buf_pool = mempool_create(buffs, r10buf_pool_alloc, r10buf_pool_free, conf); |
| 1842 | if (!conf->r10buf_pool) |
| 1843 | return -ENOMEM; |
| 1844 | conf->next_resync = 0; |
| 1845 | return 0; |
| 1846 | } |
| 1847 | |
| 1848 | /* |
| 1849 | * perform a "sync" on one "block" |
| 1850 | * |
| 1851 | * We need to make sure that no normal I/O request - particularly write |
| 1852 | * requests - conflict with active sync requests. |
| 1853 | * |
| 1854 | * This is achieved by tracking pending requests and a 'barrier' concept |
| 1855 | * that can be installed to exclude normal IO requests. |
| 1856 | * |
| 1857 | * Resync and recovery are handled very differently. |
| 1858 | * We differentiate by looking at MD_RECOVERY_SYNC in mddev->recovery. |
| 1859 | * |
| 1860 | * For resync, we iterate over virtual addresses, read all copies, |
| 1861 | * and update if there are differences. If only one copy is live, |
| 1862 | * skip it. |
| 1863 | * For recovery, we iterate over physical addresses, read a good |
| 1864 | * value for each non-in_sync drive, and over-write. |
| 1865 | * |
| 1866 | * So, for recovery we may have several outstanding complex requests for a |
| 1867 | * given address, one for each out-of-sync device. We model this by allocating |
| 1868 | * a number of r10_bio structures, one for each out-of-sync device. |
| 1869 | * As we setup these structures, we collect all bio's together into a list |
| 1870 | * which we then process collectively to add pages, and then process again |
| 1871 | * to pass to generic_make_request. |
| 1872 | * |
| 1873 | * The r10_bio structures are linked using a borrowed master_bio pointer. |
| 1874 | * This link is counted in ->remaining. When the r10_bio that points to NULL |
| 1875 | * has its remaining count decremented to 0, the whole complex operation |
| 1876 | * is complete. |
| 1877 | * |
| 1878 | */ |
| 1879 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 1880 | static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, |
| 1881 | int *skipped, int go_faster) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1882 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 1883 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1884 | r10bio_t *r10_bio; |
| 1885 | struct bio *biolist = NULL, *bio; |
| 1886 | sector_t max_sector, nr_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1887 | int i; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1888 | int max_sync; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 1889 | sector_t sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1890 | |
| 1891 | sector_t sectors_skipped = 0; |
| 1892 | int chunks_skipped = 0; |
| 1893 | |
| 1894 | if (!conf->r10buf_pool) |
| 1895 | if (init_resync(conf)) |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1896 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | |
| 1898 | skipped: |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 1899 | max_sector = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 1901 | max_sector = mddev->resync_max_sectors; |
| 1902 | if (sector_nr >= max_sector) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1903 | /* If we aborted, we need to abort the |
| 1904 | * sync on the 'current' bitmap chucks (there can |
| 1905 | * be several when recovering multiple devices). |
| 1906 | * as we may have started syncing it but not finished. |
| 1907 | * We can find the current address in |
| 1908 | * mddev->curr_resync, but for recovery, |
| 1909 | * we need to convert that to several |
| 1910 | * virtual addresses. |
| 1911 | */ |
| 1912 | if (mddev->curr_resync < max_sector) { /* aborted */ |
| 1913 | if (test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) |
| 1914 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 1915 | &sync_blocks, 1); |
| 1916 | else for (i=0; i<conf->raid_disks; i++) { |
| 1917 | sector_t sect = |
| 1918 | raid10_find_virt(conf, mddev->curr_resync, i); |
| 1919 | bitmap_end_sync(mddev->bitmap, sect, |
| 1920 | &sync_blocks, 1); |
| 1921 | } |
| 1922 | } else /* completed sync */ |
| 1923 | conf->fullsync = 0; |
| 1924 | |
| 1925 | bitmap_close_sync(mddev->bitmap); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1926 | close_sync(conf); |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1927 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1928 | return sectors_skipped; |
| 1929 | } |
| 1930 | if (chunks_skipped >= conf->raid_disks) { |
| 1931 | /* if there has been nothing to do on any drive, |
| 1932 | * then there is nothing to do at all.. |
| 1933 | */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1934 | *skipped = 1; |
| 1935 | return (max_sector - sector_nr) + sectors_skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1936 | } |
| 1937 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 1938 | if (max_sector > mddev->resync_max) |
| 1939 | max_sector = mddev->resync_max; /* Don't do IO beyond here */ |
| 1940 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1941 | /* make sure whole request will fit in a chunk - if chunks |
| 1942 | * are meaningful |
| 1943 | */ |
| 1944 | if (conf->near_copies < conf->raid_disks && |
| 1945 | max_sector > (sector_nr | conf->chunk_mask)) |
| 1946 | max_sector = (sector_nr | conf->chunk_mask) + 1; |
| 1947 | /* |
| 1948 | * If there is non-resync activity waiting for us then |
| 1949 | * put in a delay to throttle resync. |
| 1950 | */ |
NeilBrown | 0a27ec9 | 2006-01-06 00:20:13 -0800 | [diff] [blame] | 1951 | if (!go_faster && conf->nr_waiting) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1952 | msleep_interruptible(1000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1953 | |
| 1954 | /* Again, very different code for resync and recovery. |
| 1955 | * Both must result in an r10bio with a list of bios that |
| 1956 | * have bi_end_io, bi_sector, bi_bdev set, |
| 1957 | * and bi_private set to the r10bio. |
| 1958 | * For recovery, we may actually create several r10bios |
| 1959 | * with 2 bios in each, that correspond to the bios in the main one. |
| 1960 | * In this case, the subordinate r10bios link back through a |
| 1961 | * borrowed master_bio pointer, and the counter in the master |
| 1962 | * includes a ref from each subordinate. |
| 1963 | */ |
| 1964 | /* First, we decide what to do and set ->bi_end_io |
| 1965 | * To end_sync_read if we want to read, and |
| 1966 | * end_sync_write if we will want to write. |
| 1967 | */ |
| 1968 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 1969 | max_sync = RESYNC_PAGES << (PAGE_SHIFT-9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1970 | if (!test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
| 1971 | /* recovery... the complicated one */ |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 1972 | int j, k; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | r10_bio = NULL; |
| 1974 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 1975 | for (i=0 ; i<conf->raid_disks; i++) { |
| 1976 | int still_degraded; |
| 1977 | r10bio_t *rb2; |
| 1978 | sector_t sect; |
| 1979 | int must_sync; |
| 1980 | |
| 1981 | if (conf->mirrors[i].rdev == NULL || |
| 1982 | test_bit(In_sync, &conf->mirrors[i].rdev->flags)) |
| 1983 | continue; |
| 1984 | |
| 1985 | still_degraded = 0; |
| 1986 | /* want to reconstruct this device */ |
| 1987 | rb2 = r10_bio; |
| 1988 | sect = raid10_find_virt(conf, sector_nr, i); |
| 1989 | /* Unless we are doing a full sync, we only need |
| 1990 | * to recover the block if it is set in the bitmap |
| 1991 | */ |
| 1992 | must_sync = bitmap_start_sync(mddev->bitmap, sect, |
| 1993 | &sync_blocks, 1); |
| 1994 | if (sync_blocks < max_sync) |
| 1995 | max_sync = sync_blocks; |
| 1996 | if (!must_sync && |
| 1997 | !conf->fullsync) { |
| 1998 | /* yep, skip the sync_blocks here, but don't assume |
| 1999 | * that there will never be anything to do here |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2000 | */ |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2001 | chunks_skipped = -1; |
| 2002 | continue; |
| 2003 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2004 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2005 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
| 2006 | raise_barrier(conf, rb2 != NULL); |
| 2007 | atomic_set(&r10_bio->remaining, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2008 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2009 | r10_bio->master_bio = (struct bio*)rb2; |
| 2010 | if (rb2) |
| 2011 | atomic_inc(&rb2->remaining); |
| 2012 | r10_bio->mddev = mddev; |
| 2013 | set_bit(R10BIO_IsRecover, &r10_bio->state); |
| 2014 | r10_bio->sector = sect; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2015 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2016 | raid10_find_phys(conf, r10_bio); |
NeilBrown | 1805556 | 2009-05-07 12:48:10 +1000 | [diff] [blame] | 2017 | |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2018 | /* Need to check if the array will still be |
| 2019 | * degraded |
| 2020 | */ |
| 2021 | for (j=0; j<conf->raid_disks; j++) |
| 2022 | if (conf->mirrors[j].rdev == NULL || |
| 2023 | test_bit(Faulty, &conf->mirrors[j].rdev->flags)) { |
| 2024 | still_degraded = 1; |
NeilBrown | 87fc767 | 2005-09-09 16:24:04 -0700 | [diff] [blame] | 2025 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2026 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2027 | |
| 2028 | must_sync = bitmap_start_sync(mddev->bitmap, sect, |
| 2029 | &sync_blocks, still_degraded); |
| 2030 | |
| 2031 | for (j=0; j<conf->copies;j++) { |
| 2032 | int d = r10_bio->devs[j].devnum; |
| 2033 | if (!conf->mirrors[d].rdev || |
| 2034 | !test_bit(In_sync, &conf->mirrors[d].rdev->flags)) |
| 2035 | continue; |
| 2036 | /* This is where we read from */ |
| 2037 | bio = r10_bio->devs[0].bio; |
| 2038 | bio->bi_next = biolist; |
| 2039 | biolist = bio; |
| 2040 | bio->bi_private = r10_bio; |
| 2041 | bio->bi_end_io = end_sync_read; |
| 2042 | bio->bi_rw = READ; |
| 2043 | bio->bi_sector = r10_bio->devs[j].addr + |
| 2044 | conf->mirrors[d].rdev->data_offset; |
| 2045 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 2046 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 2047 | atomic_inc(&r10_bio->remaining); |
| 2048 | /* and we write to 'i' */ |
| 2049 | |
| 2050 | for (k=0; k<conf->copies; k++) |
| 2051 | if (r10_bio->devs[k].devnum == i) |
| 2052 | break; |
| 2053 | BUG_ON(k == conf->copies); |
| 2054 | bio = r10_bio->devs[1].bio; |
| 2055 | bio->bi_next = biolist; |
| 2056 | biolist = bio; |
| 2057 | bio->bi_private = r10_bio; |
| 2058 | bio->bi_end_io = end_sync_write; |
| 2059 | bio->bi_rw = WRITE; |
| 2060 | bio->bi_sector = r10_bio->devs[k].addr + |
| 2061 | conf->mirrors[i].rdev->data_offset; |
| 2062 | bio->bi_bdev = conf->mirrors[i].rdev->bdev; |
| 2063 | |
| 2064 | r10_bio->devs[0].devnum = d; |
| 2065 | r10_bio->devs[1].devnum = i; |
| 2066 | |
| 2067 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2069 | if (j == conf->copies) { |
| 2070 | /* Cannot recover, so abort the recovery */ |
| 2071 | put_buf(r10_bio); |
| 2072 | if (rb2) |
| 2073 | atomic_dec(&rb2->remaining); |
| 2074 | r10_bio = rb2; |
| 2075 | if (!test_and_set_bit(MD_RECOVERY_INTR, |
| 2076 | &mddev->recovery)) |
| 2077 | printk(KERN_INFO "md/raid10:%s: insufficient " |
| 2078 | "working devices for recovery.\n", |
| 2079 | mdname(mddev)); |
| 2080 | break; |
| 2081 | } |
| 2082 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2083 | if (biolist == NULL) { |
| 2084 | while (r10_bio) { |
| 2085 | r10bio_t *rb2 = r10_bio; |
| 2086 | r10_bio = (r10bio_t*) rb2->master_bio; |
| 2087 | rb2->master_bio = NULL; |
| 2088 | put_buf(rb2); |
| 2089 | } |
| 2090 | goto giveup; |
| 2091 | } |
| 2092 | } else { |
| 2093 | /* resync. Schedule a read for every block at this virt offset */ |
| 2094 | int count = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2095 | |
NeilBrown | 78200d4 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 2096 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 2097 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2098 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, |
| 2099 | &sync_blocks, mddev->degraded) && |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2100 | !conf->fullsync && !test_bit(MD_RECOVERY_REQUESTED, |
| 2101 | &mddev->recovery)) { |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2102 | /* We can skip this block */ |
| 2103 | *skipped = 1; |
| 2104 | return sync_blocks + sectors_skipped; |
| 2105 | } |
| 2106 | if (sync_blocks < max_sync) |
| 2107 | max_sync = sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2108 | r10_bio = mempool_alloc(conf->r10buf_pool, GFP_NOIO); |
| 2109 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2110 | r10_bio->mddev = mddev; |
| 2111 | atomic_set(&r10_bio->remaining, 0); |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2112 | raise_barrier(conf, 0); |
| 2113 | conf->next_resync = sector_nr; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2114 | |
| 2115 | r10_bio->master_bio = NULL; |
| 2116 | r10_bio->sector = sector_nr; |
| 2117 | set_bit(R10BIO_IsSync, &r10_bio->state); |
| 2118 | raid10_find_phys(conf, r10_bio); |
| 2119 | r10_bio->sectors = (sector_nr | conf->chunk_mask) - sector_nr +1; |
| 2120 | |
| 2121 | for (i=0; i<conf->copies; i++) { |
| 2122 | int d = r10_bio->devs[i].devnum; |
| 2123 | bio = r10_bio->devs[i].bio; |
| 2124 | bio->bi_end_io = NULL; |
NeilBrown | af03b8e | 2007-06-16 10:16:06 -0700 | [diff] [blame] | 2125 | clear_bit(BIO_UPTODATE, &bio->bi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2126 | if (conf->mirrors[d].rdev == NULL || |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2127 | test_bit(Faulty, &conf->mirrors[d].rdev->flags)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2128 | continue; |
| 2129 | atomic_inc(&conf->mirrors[d].rdev->nr_pending); |
| 2130 | atomic_inc(&r10_bio->remaining); |
| 2131 | bio->bi_next = biolist; |
| 2132 | biolist = bio; |
| 2133 | bio->bi_private = r10_bio; |
| 2134 | bio->bi_end_io = end_sync_read; |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 2135 | bio->bi_rw = READ; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2136 | bio->bi_sector = r10_bio->devs[i].addr + |
| 2137 | conf->mirrors[d].rdev->data_offset; |
| 2138 | bio->bi_bdev = conf->mirrors[d].rdev->bdev; |
| 2139 | count++; |
| 2140 | } |
| 2141 | |
| 2142 | if (count < 2) { |
| 2143 | for (i=0; i<conf->copies; i++) { |
| 2144 | int d = r10_bio->devs[i].devnum; |
| 2145 | if (r10_bio->devs[i].bio->bi_end_io) |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2146 | rdev_dec_pending(conf->mirrors[d].rdev, |
| 2147 | mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2148 | } |
| 2149 | put_buf(r10_bio); |
| 2150 | biolist = NULL; |
| 2151 | goto giveup; |
| 2152 | } |
| 2153 | } |
| 2154 | |
| 2155 | for (bio = biolist; bio ; bio=bio->bi_next) { |
| 2156 | |
| 2157 | bio->bi_flags &= ~(BIO_POOL_MASK - 1); |
| 2158 | if (bio->bi_end_io) |
| 2159 | bio->bi_flags |= 1 << BIO_UPTODATE; |
| 2160 | bio->bi_vcnt = 0; |
| 2161 | bio->bi_idx = 0; |
| 2162 | bio->bi_phys_segments = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | bio->bi_size = 0; |
| 2164 | } |
| 2165 | |
| 2166 | nr_sectors = 0; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2167 | if (sector_nr + max_sync < max_sector) |
| 2168 | max_sector = sector_nr + max_sync; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | do { |
| 2170 | struct page *page; |
| 2171 | int len = PAGE_SIZE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | if (sector_nr + (len>>9) > max_sector) |
| 2173 | len = (max_sector - sector_nr) << 9; |
| 2174 | if (len == 0) |
| 2175 | break; |
| 2176 | for (bio= biolist ; bio ; bio=bio->bi_next) { |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2177 | struct bio *bio2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | page = bio->bi_io_vec[bio->bi_vcnt].bv_page; |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2179 | if (bio_add_page(bio, page, len, 0)) |
| 2180 | continue; |
| 2181 | |
| 2182 | /* stop here */ |
| 2183 | bio->bi_io_vec[bio->bi_vcnt].bv_page = page; |
| 2184 | for (bio2 = biolist; |
| 2185 | bio2 && bio2 != bio; |
| 2186 | bio2 = bio2->bi_next) { |
| 2187 | /* remove last page from this bio */ |
| 2188 | bio2->bi_vcnt--; |
| 2189 | bio2->bi_size -= len; |
| 2190 | bio2->bi_flags &= ~(1<< BIO_SEG_VALID); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2191 | } |
NeilBrown | ab9d47e | 2011-05-11 14:54:41 +1000 | [diff] [blame] | 2192 | goto bio_full; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2193 | } |
| 2194 | nr_sectors += len>>9; |
| 2195 | sector_nr += len>>9; |
| 2196 | } while (biolist->bi_vcnt < RESYNC_PAGES); |
| 2197 | bio_full: |
| 2198 | r10_bio->sectors = nr_sectors; |
| 2199 | |
| 2200 | while (biolist) { |
| 2201 | bio = biolist; |
| 2202 | biolist = biolist->bi_next; |
| 2203 | |
| 2204 | bio->bi_next = NULL; |
| 2205 | r10_bio = bio->bi_private; |
| 2206 | r10_bio->sectors = nr_sectors; |
| 2207 | |
| 2208 | if (bio->bi_end_io == end_sync_read) { |
| 2209 | md_sync_acct(bio->bi_bdev, nr_sectors); |
| 2210 | generic_make_request(bio); |
| 2211 | } |
| 2212 | } |
| 2213 | |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 2214 | if (sectors_skipped) |
| 2215 | /* pretend they weren't skipped, it makes |
| 2216 | * no important difference in this case |
| 2217 | */ |
| 2218 | md_done_sync(mddev, sectors_skipped, 1); |
| 2219 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2220 | return sectors_skipped + nr_sectors; |
| 2221 | giveup: |
| 2222 | /* There is nowhere to write, so all non-sync |
| 2223 | * drives must be failed, so try the next chunk... |
| 2224 | */ |
NeilBrown | 09b4068 | 2009-02-25 13:18:47 +1100 | [diff] [blame] | 2225 | if (sector_nr + max_sync < max_sector) |
| 2226 | max_sector = sector_nr + max_sync; |
| 2227 | |
| 2228 | sectors_skipped += (max_sector - sector_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | chunks_skipped ++; |
| 2230 | sector_nr = max_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2231 | goto skipped; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2232 | } |
| 2233 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 2234 | static sector_t |
| 2235 | raid10_size(mddev_t *mddev, sector_t sectors, int raid_disks) |
| 2236 | { |
| 2237 | sector_t size; |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2238 | conf_t *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 2239 | |
| 2240 | if (!raid_disks) |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 2241 | raid_disks = conf->raid_disks; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 2242 | if (!sectors) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2243 | sectors = conf->dev_sectors; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 2244 | |
| 2245 | size = sectors >> conf->chunk_shift; |
| 2246 | sector_div(size, conf->far_copies); |
| 2247 | size = size * raid_disks; |
| 2248 | sector_div(size, conf->near_copies); |
| 2249 | |
| 2250 | return size << conf->chunk_shift; |
| 2251 | } |
| 2252 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2253 | |
| 2254 | static conf_t *setup_conf(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2255 | { |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2256 | conf_t *conf = NULL; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 2257 | int nc, fc, fo; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2258 | sector_t stride, size; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2259 | int err = -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | |
Maciej Trela | f73ea87 | 2010-06-16 11:46:29 +0100 | [diff] [blame] | 2261 | if (mddev->new_chunk_sectors < (PAGE_SIZE >> 9) || |
| 2262 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 2263 | printk(KERN_ERR "md/raid10:%s: chunk size must be " |
| 2264 | "at least PAGE_SIZE(%ld) and be a power of 2.\n", |
| 2265 | mdname(mddev), PAGE_SIZE); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2266 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2268 | |
Maciej Trela | f73ea87 | 2010-06-16 11:46:29 +0100 | [diff] [blame] | 2269 | nc = mddev->new_layout & 255; |
| 2270 | fc = (mddev->new_layout >> 8) & 255; |
| 2271 | fo = mddev->new_layout & (1<<16); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2272 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2273 | if ((nc*fc) <2 || (nc*fc) > mddev->raid_disks || |
Maciej Trela | f73ea87 | 2010-06-16 11:46:29 +0100 | [diff] [blame] | 2274 | (mddev->new_layout >> 17)) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 2275 | printk(KERN_ERR "md/raid10:%s: unsupported raid10 layout: 0x%8x\n", |
Maciej Trela | f73ea87 | 2010-06-16 11:46:29 +0100 | [diff] [blame] | 2276 | mdname(mddev), mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2277 | goto out; |
| 2278 | } |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2279 | |
| 2280 | err = -ENOMEM; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2281 | conf = kzalloc(sizeof(conf_t), GFP_KERNEL); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2282 | if (!conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2283 | goto out; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2284 | |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2285 | conf->mirrors = kzalloc(sizeof(struct mirror_info)*mddev->raid_disks, |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2286 | GFP_KERNEL); |
| 2287 | if (!conf->mirrors) |
| 2288 | goto out; |
NeilBrown | 4443ae1 | 2006-01-06 00:20:28 -0800 | [diff] [blame] | 2289 | |
| 2290 | conf->tmppage = alloc_page(GFP_KERNEL); |
| 2291 | if (!conf->tmppage) |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2292 | goto out; |
| 2293 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2294 | |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2295 | conf->raid_disks = mddev->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2296 | conf->near_copies = nc; |
| 2297 | conf->far_copies = fc; |
| 2298 | conf->copies = nc*fc; |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 2299 | conf->far_offset = fo; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2300 | conf->chunk_mask = mddev->new_chunk_sectors - 1; |
| 2301 | conf->chunk_shift = ffz(~mddev->new_chunk_sectors); |
| 2302 | |
| 2303 | conf->r10bio_pool = mempool_create(NR_RAID10_BIOS, r10bio_pool_alloc, |
| 2304 | r10bio_pool_free, conf); |
| 2305 | if (!conf->r10bio_pool) |
| 2306 | goto out; |
| 2307 | |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 2308 | size = mddev->dev_sectors >> conf->chunk_shift; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2309 | sector_div(size, fc); |
| 2310 | size = size * conf->raid_disks; |
| 2311 | sector_div(size, nc); |
| 2312 | /* 'size' is now the number of chunks in the array */ |
| 2313 | /* calculate "used chunks per device" in 'stride' */ |
| 2314 | stride = size * conf->copies; |
NeilBrown | af03b8e | 2007-06-16 10:16:06 -0700 | [diff] [blame] | 2315 | |
| 2316 | /* We need to round up when dividing by raid_disks to |
| 2317 | * get the stride size. |
| 2318 | */ |
| 2319 | stride += conf->raid_disks - 1; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2320 | sector_div(stride, conf->raid_disks); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2321 | |
| 2322 | conf->dev_sectors = stride << conf->chunk_shift; |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2323 | |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 2324 | if (fo) |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2325 | stride = 1; |
| 2326 | else |
NeilBrown | c93983b | 2006-06-26 00:27:41 -0700 | [diff] [blame] | 2327 | sector_div(stride, fc); |
NeilBrown | 64a742b | 2007-02-28 20:11:18 -0800 | [diff] [blame] | 2328 | conf->stride = stride << conf->chunk_shift; |
| 2329 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2330 | |
Neil Brown | e7e72bf | 2008-05-14 16:05:54 -0700 | [diff] [blame] | 2331 | spin_lock_init(&conf->device_lock); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2332 | INIT_LIST_HEAD(&conf->retry_list); |
| 2333 | |
| 2334 | spin_lock_init(&conf->resync_lock); |
| 2335 | init_waitqueue_head(&conf->wait_barrier); |
| 2336 | |
| 2337 | conf->thread = md_register_thread(raid10d, mddev, NULL); |
| 2338 | if (!conf->thread) |
| 2339 | goto out; |
| 2340 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2341 | conf->mddev = mddev; |
| 2342 | return conf; |
| 2343 | |
| 2344 | out: |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 2345 | printk(KERN_ERR "md/raid10:%s: couldn't allocate memory.\n", |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2346 | mdname(mddev)); |
| 2347 | if (conf) { |
| 2348 | if (conf->r10bio_pool) |
| 2349 | mempool_destroy(conf->r10bio_pool); |
| 2350 | kfree(conf->mirrors); |
| 2351 | safe_put_page(conf->tmppage); |
| 2352 | kfree(conf); |
| 2353 | } |
| 2354 | return ERR_PTR(err); |
| 2355 | } |
| 2356 | |
| 2357 | static int run(mddev_t *mddev) |
| 2358 | { |
| 2359 | conf_t *conf; |
| 2360 | int i, disk_idx, chunk_size; |
| 2361 | mirror_info_t *disk; |
| 2362 | mdk_rdev_t *rdev; |
| 2363 | sector_t size; |
| 2364 | |
| 2365 | /* |
| 2366 | * copy the already verified devices into our private RAID10 |
| 2367 | * bookkeeping area. [whatever we allocate in run(), |
| 2368 | * should be freed in stop()] |
| 2369 | */ |
| 2370 | |
| 2371 | if (mddev->private == NULL) { |
| 2372 | conf = setup_conf(mddev); |
| 2373 | if (IS_ERR(conf)) |
| 2374 | return PTR_ERR(conf); |
| 2375 | mddev->private = conf; |
| 2376 | } |
| 2377 | conf = mddev->private; |
| 2378 | if (!conf) |
| 2379 | goto out; |
| 2380 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2381 | mddev->thread = conf->thread; |
| 2382 | conf->thread = NULL; |
| 2383 | |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 2384 | chunk_size = mddev->chunk_sectors << 9; |
| 2385 | blk_queue_io_min(mddev->queue, chunk_size); |
| 2386 | if (conf->raid_disks % conf->near_copies) |
| 2387 | blk_queue_io_opt(mddev->queue, chunk_size * conf->raid_disks); |
| 2388 | else |
| 2389 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 2390 | (conf->raid_disks / conf->near_copies)); |
| 2391 | |
Cheng Renquan | 159ec1f | 2009-01-09 08:31:08 +1100 | [diff] [blame] | 2392 | list_for_each_entry(rdev, &mddev->disks, same_set) { |
NeilBrown | 34b343c | 2011-07-28 11:31:47 +1000 | [diff] [blame] | 2393 | |
| 2394 | if (rdev->badblocks.count) { |
| 2395 | printk(KERN_ERR "md/raid10: cannot handle bad blocks yet\n"); |
| 2396 | goto out_free_conf; |
| 2397 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2398 | disk_idx = rdev->raid_disk; |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 2399 | if (disk_idx >= conf->raid_disks |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2400 | || disk_idx < 0) |
| 2401 | continue; |
| 2402 | disk = conf->mirrors + disk_idx; |
| 2403 | |
| 2404 | disk->rdev = rdev; |
Martin K. Petersen | 8f6c2e4 | 2009-07-01 11:13:45 +1000 | [diff] [blame] | 2405 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 2406 | rdev->data_offset << 9); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | /* as we don't honour merge_bvec_fn, we must never risk |
NeilBrown | 627a2d3 | 2010-03-08 16:44:38 +1100 | [diff] [blame] | 2408 | * violating it, so limit max_segments to 1 lying |
| 2409 | * within a single page. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2410 | */ |
NeilBrown | 627a2d3 | 2010-03-08 16:44:38 +1100 | [diff] [blame] | 2411 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn) { |
| 2412 | blk_queue_max_segments(mddev->queue, 1); |
| 2413 | blk_queue_segment_boundary(mddev->queue, |
| 2414 | PAGE_CACHE_SIZE - 1); |
| 2415 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2416 | |
| 2417 | disk->head_position = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2418 | } |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 2419 | /* need to check that every block has at least one working mirror */ |
NeilBrown | 700c721 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2420 | if (!enough(conf, -1)) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 2421 | printk(KERN_ERR "md/raid10:%s: not enough operational mirrors.\n", |
NeilBrown | 6d50824 | 2005-09-09 16:24:03 -0700 | [diff] [blame] | 2422 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | goto out_free_conf; |
| 2424 | } |
| 2425 | |
| 2426 | mddev->degraded = 0; |
| 2427 | for (i = 0; i < conf->raid_disks; i++) { |
| 2428 | |
| 2429 | disk = conf->mirrors + i; |
| 2430 | |
NeilBrown | 5fd6c1d | 2006-06-26 00:27:40 -0700 | [diff] [blame] | 2431 | if (!disk->rdev || |
NeilBrown | 2e333e8 | 2006-10-21 10:24:07 -0700 | [diff] [blame] | 2432 | !test_bit(In_sync, &disk->rdev->flags)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | disk->head_position = 0; |
| 2434 | mddev->degraded++; |
Neil Brown | 8c2e870 | 2008-06-28 08:30:52 +1000 | [diff] [blame] | 2435 | if (disk->rdev) |
| 2436 | conf->fullsync = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | } |
| 2438 | } |
| 2439 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 2440 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 2441 | printk(KERN_NOTICE "md/raid10:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 2442 | " -- starting background reconstruction\n", |
| 2443 | mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | printk(KERN_INFO |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 2445 | "md/raid10:%s: active with %d out of %d devices\n", |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 2446 | mdname(mddev), conf->raid_disks - mddev->degraded, |
| 2447 | conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2448 | /* |
| 2449 | * Ok, everything is just fine now |
| 2450 | */ |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2451 | mddev->dev_sectors = conf->dev_sectors; |
| 2452 | size = raid10_size(mddev, 0, 0); |
| 2453 | md_set_array_sectors(mddev, size); |
| 2454 | mddev->resync_max_sectors = size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2455 | |
NeilBrown | 0d12922 | 2006-10-03 01:15:54 -0700 | [diff] [blame] | 2456 | mddev->queue->backing_dev_info.congested_fn = raid10_congested; |
| 2457 | mddev->queue->backing_dev_info.congested_data = mddev; |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 2458 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2459 | /* Calculate max read-ahead size. |
| 2460 | * We need to readahead at least twice a whole stripe.... |
| 2461 | * maybe... |
| 2462 | */ |
| 2463 | { |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 2464 | int stripe = conf->raid_disks * |
| 2465 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | stripe /= conf->near_copies; |
| 2467 | if (mddev->queue->backing_dev_info.ra_pages < 2* stripe) |
| 2468 | mddev->queue->backing_dev_info.ra_pages = 2* stripe; |
| 2469 | } |
| 2470 | |
NeilBrown | 84707f3 | 2010-03-16 17:23:35 +1100 | [diff] [blame] | 2471 | if (conf->near_copies < conf->raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2472 | blk_queue_merge_bvec(mddev->queue, raid10_mergeable_bvec); |
Martin K. Petersen | a91a278 | 2011-03-17 11:11:05 +0100 | [diff] [blame] | 2473 | |
| 2474 | if (md_integrity_register(mddev)) |
| 2475 | goto out_free_conf; |
| 2476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2477 | return 0; |
| 2478 | |
| 2479 | out_free_conf: |
NeilBrown | 589a594 | 2010-12-09 17:02:14 +1100 | [diff] [blame] | 2480 | md_unregister_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | if (conf->r10bio_pool) |
| 2482 | mempool_destroy(conf->r10bio_pool); |
NeilBrown | 1345b1d | 2006-01-06 00:20:40 -0800 | [diff] [blame] | 2483 | safe_put_page(conf->tmppage); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 2484 | kfree(conf->mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | kfree(conf); |
| 2486 | mddev->private = NULL; |
| 2487 | out: |
| 2488 | return -EIO; |
| 2489 | } |
| 2490 | |
| 2491 | static int stop(mddev_t *mddev) |
| 2492 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2493 | conf_t *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2494 | |
NeilBrown | 409c57f | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 2495 | raise_barrier(conf, 0); |
| 2496 | lower_barrier(conf); |
| 2497 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | md_unregister_thread(mddev->thread); |
| 2499 | mddev->thread = NULL; |
| 2500 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 2501 | if (conf->r10bio_pool) |
| 2502 | mempool_destroy(conf->r10bio_pool); |
Jesper Juhl | 990a8ba | 2005-06-21 17:17:30 -0700 | [diff] [blame] | 2503 | kfree(conf->mirrors); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2504 | kfree(conf); |
| 2505 | mddev->private = NULL; |
| 2506 | return 0; |
| 2507 | } |
| 2508 | |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2509 | static void raid10_quiesce(mddev_t *mddev, int state) |
| 2510 | { |
NeilBrown | 070ec55 | 2009-06-16 16:54:21 +1000 | [diff] [blame] | 2511 | conf_t *conf = mddev->private; |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2512 | |
| 2513 | switch(state) { |
| 2514 | case 1: |
| 2515 | raise_barrier(conf, 0); |
| 2516 | break; |
| 2517 | case 0: |
| 2518 | lower_barrier(conf); |
| 2519 | break; |
| 2520 | } |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2521 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2522 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2523 | static void *raid10_takeover_raid0(mddev_t *mddev) |
| 2524 | { |
| 2525 | mdk_rdev_t *rdev; |
| 2526 | conf_t *conf; |
| 2527 | |
| 2528 | if (mddev->degraded > 0) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 2529 | printk(KERN_ERR "md/raid10:%s: Error: degraded raid0!\n", |
| 2530 | mdname(mddev)); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2531 | return ERR_PTR(-EINVAL); |
| 2532 | } |
| 2533 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2534 | /* Set new parameters */ |
| 2535 | mddev->new_level = 10; |
| 2536 | /* new layout: far_copies = 1, near_copies = 2 */ |
| 2537 | mddev->new_layout = (1<<8) + 2; |
| 2538 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 2539 | mddev->delta_disks = mddev->raid_disks; |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2540 | mddev->raid_disks *= 2; |
| 2541 | /* make sure it will be not marked as dirty */ |
| 2542 | mddev->recovery_cp = MaxSector; |
| 2543 | |
| 2544 | conf = setup_conf(mddev); |
Krzysztof Wojcik | 02214dc | 2011-02-04 14:18:26 +0100 | [diff] [blame] | 2545 | if (!IS_ERR(conf)) { |
NeilBrown | e93f68a | 2010-06-15 09:36:03 +0100 | [diff] [blame] | 2546 | list_for_each_entry(rdev, &mddev->disks, same_set) |
| 2547 | if (rdev->raid_disk >= 0) |
| 2548 | rdev->new_raid_disk = rdev->raid_disk * 2; |
Krzysztof Wojcik | 02214dc | 2011-02-04 14:18:26 +0100 | [diff] [blame] | 2549 | conf->barrier = 1; |
| 2550 | } |
| 2551 | |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2552 | return conf; |
| 2553 | } |
| 2554 | |
| 2555 | static void *raid10_takeover(mddev_t *mddev) |
| 2556 | { |
| 2557 | struct raid0_private_data *raid0_priv; |
| 2558 | |
| 2559 | /* raid10 can take over: |
| 2560 | * raid0 - providing it has only two drives |
| 2561 | */ |
| 2562 | if (mddev->level == 0) { |
| 2563 | /* for raid0 takeover only one zone is supported */ |
| 2564 | raid0_priv = mddev->private; |
| 2565 | if (raid0_priv->nr_strip_zones > 1) { |
NeilBrown | 128595e | 2010-05-03 14:47:14 +1000 | [diff] [blame] | 2566 | printk(KERN_ERR "md/raid10:%s: cannot takeover raid 0" |
| 2567 | " with more than one zone.\n", |
| 2568 | mdname(mddev)); |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2569 | return ERR_PTR(-EINVAL); |
| 2570 | } |
| 2571 | return raid10_takeover_raid0(mddev); |
| 2572 | } |
| 2573 | return ERR_PTR(-EINVAL); |
| 2574 | } |
| 2575 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2576 | static struct mdk_personality raid10_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | { |
| 2578 | .name = "raid10", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2579 | .level = 10, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | .owner = THIS_MODULE, |
| 2581 | .make_request = make_request, |
| 2582 | .run = run, |
| 2583 | .stop = stop, |
| 2584 | .status = status, |
| 2585 | .error_handler = error, |
| 2586 | .hot_add_disk = raid10_add_disk, |
| 2587 | .hot_remove_disk= raid10_remove_disk, |
| 2588 | .spare_active = raid10_spare_active, |
| 2589 | .sync_request = sync_request, |
NeilBrown | 6cce3b2 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 2590 | .quiesce = raid10_quiesce, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 2591 | .size = raid10_size, |
Trela, Maciej | dab8b29 | 2010-03-08 16:02:45 +1100 | [diff] [blame] | 2592 | .takeover = raid10_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2593 | }; |
| 2594 | |
| 2595 | static int __init raid_init(void) |
| 2596 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2597 | return register_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2598 | } |
| 2599 | |
| 2600 | static void raid_exit(void) |
| 2601 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2602 | unregister_md_personality(&raid10_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | module_init(raid_init); |
| 2606 | module_exit(raid_exit); |
| 2607 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 2608 | MODULE_DESCRIPTION("RAID10 (striped mirror) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | MODULE_ALIAS("md-personality-9"); /* RAID10 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 2610 | MODULE_ALIAS("md-raid10"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 2611 | MODULE_ALIAS("md-level-10"); |