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