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