Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * raid1.c : Multiple Devices driver for Linux |
| 3 | * |
| 4 | * Copyright (C) 1999, 2000, 2001 Ingo Molnar, Red Hat |
| 5 | * |
| 6 | * Copyright (C) 1996, 1997, 1998 Ingo Molnar, Miguel de Icaza, Gadi Oxman |
| 7 | * |
| 8 | * RAID-1 management functions. |
| 9 | * |
| 10 | * Better read-balancing code written by Mika Kuoppala <miku@iki.fi>, 2000 |
| 11 | * |
| 12 | * Fixes to reconstruction by Jakob Østergaard" <jakob@ostenfeld.dk> |
| 13 | * Various fixes by Neil Brown <neilb@cse.unsw.edu.au> |
| 14 | * |
| 15 | * This program is free software; you can redistribute it and/or modify |
| 16 | * it under the terms of the GNU General Public License as published by |
| 17 | * the Free Software Foundation; either version 2, or (at your option) |
| 18 | * any later version. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * (for example /usr/src/linux/COPYING); if not, write to the Free |
| 22 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 23 | */ |
| 24 | |
| 25 | #include <linux/raid/raid1.h> |
| 26 | |
| 27 | /* |
| 28 | * Number of guaranteed r1bios in case of extreme VM load: |
| 29 | */ |
| 30 | #define NR_RAID1_BIOS 256 |
| 31 | |
| 32 | static mdk_personality_t raid1_personality; |
| 33 | |
| 34 | static void unplug_slaves(mddev_t *mddev); |
| 35 | |
| 36 | |
| 37 | static void * r1bio_pool_alloc(unsigned int __nocast gfp_flags, void *data) |
| 38 | { |
| 39 | struct pool_info *pi = data; |
| 40 | r1bio_t *r1_bio; |
| 41 | int size = offsetof(r1bio_t, bios[pi->raid_disks]); |
| 42 | |
| 43 | /* allocate a r1bio with room for raid_disks entries in the bios array */ |
| 44 | r1_bio = kmalloc(size, gfp_flags); |
| 45 | if (r1_bio) |
| 46 | memset(r1_bio, 0, size); |
| 47 | else |
| 48 | unplug_slaves(pi->mddev); |
| 49 | |
| 50 | return r1_bio; |
| 51 | } |
| 52 | |
| 53 | static void r1bio_pool_free(void *r1_bio, void *data) |
| 54 | { |
| 55 | kfree(r1_bio); |
| 56 | } |
| 57 | |
| 58 | #define RESYNC_BLOCK_SIZE (64*1024) |
| 59 | //#define RESYNC_BLOCK_SIZE PAGE_SIZE |
| 60 | #define RESYNC_SECTORS (RESYNC_BLOCK_SIZE >> 9) |
| 61 | #define RESYNC_PAGES ((RESYNC_BLOCK_SIZE + PAGE_SIZE-1) / PAGE_SIZE) |
| 62 | #define RESYNC_WINDOW (2048*1024) |
| 63 | |
| 64 | static void * r1buf_pool_alloc(unsigned int __nocast gfp_flags, void *data) |
| 65 | { |
| 66 | struct pool_info *pi = data; |
| 67 | struct page *page; |
| 68 | r1bio_t *r1_bio; |
| 69 | struct bio *bio; |
| 70 | int i, j; |
| 71 | |
| 72 | r1_bio = r1bio_pool_alloc(gfp_flags, pi); |
| 73 | if (!r1_bio) { |
| 74 | unplug_slaves(pi->mddev); |
| 75 | return NULL; |
| 76 | } |
| 77 | |
| 78 | /* |
| 79 | * Allocate bios : 1 for reading, n-1 for writing |
| 80 | */ |
| 81 | for (j = pi->raid_disks ; j-- ; ) { |
| 82 | bio = bio_alloc(gfp_flags, RESYNC_PAGES); |
| 83 | if (!bio) |
| 84 | goto out_free_bio; |
| 85 | r1_bio->bios[j] = bio; |
| 86 | } |
| 87 | /* |
| 88 | * Allocate RESYNC_PAGES data pages and attach them to |
| 89 | * the first bio; |
| 90 | */ |
| 91 | bio = r1_bio->bios[0]; |
| 92 | for (i = 0; i < RESYNC_PAGES; i++) { |
| 93 | page = alloc_page(gfp_flags); |
| 94 | if (unlikely(!page)) |
| 95 | goto out_free_pages; |
| 96 | |
| 97 | bio->bi_io_vec[i].bv_page = page; |
| 98 | } |
| 99 | |
| 100 | r1_bio->master_bio = NULL; |
| 101 | |
| 102 | return r1_bio; |
| 103 | |
| 104 | out_free_pages: |
| 105 | for ( ; i > 0 ; i--) |
| 106 | __free_page(bio->bi_io_vec[i-1].bv_page); |
| 107 | out_free_bio: |
| 108 | while ( ++j < pi->raid_disks ) |
| 109 | bio_put(r1_bio->bios[j]); |
| 110 | r1bio_pool_free(r1_bio, data); |
| 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | static void r1buf_pool_free(void *__r1_bio, void *data) |
| 115 | { |
| 116 | struct pool_info *pi = data; |
| 117 | int i; |
| 118 | r1bio_t *r1bio = __r1_bio; |
| 119 | struct bio *bio = r1bio->bios[0]; |
| 120 | |
| 121 | for (i = 0; i < RESYNC_PAGES; i++) { |
| 122 | __free_page(bio->bi_io_vec[i].bv_page); |
| 123 | bio->bi_io_vec[i].bv_page = NULL; |
| 124 | } |
| 125 | for (i=0 ; i < pi->raid_disks; i++) |
| 126 | bio_put(r1bio->bios[i]); |
| 127 | |
| 128 | r1bio_pool_free(r1bio, data); |
| 129 | } |
| 130 | |
| 131 | static void put_all_bios(conf_t *conf, r1bio_t *r1_bio) |
| 132 | { |
| 133 | int i; |
| 134 | |
| 135 | for (i = 0; i < conf->raid_disks; i++) { |
| 136 | struct bio **bio = r1_bio->bios + i; |
| 137 | if (*bio) |
| 138 | bio_put(*bio); |
| 139 | *bio = NULL; |
| 140 | } |
| 141 | } |
| 142 | |
| 143 | static inline void free_r1bio(r1bio_t *r1_bio) |
| 144 | { |
| 145 | unsigned long flags; |
| 146 | |
| 147 | conf_t *conf = mddev_to_conf(r1_bio->mddev); |
| 148 | |
| 149 | /* |
| 150 | * Wake up any possible resync thread that waits for the device |
| 151 | * to go idle. |
| 152 | */ |
| 153 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 154 | if (!--conf->nr_pending) { |
| 155 | wake_up(&conf->wait_idle); |
| 156 | wake_up(&conf->wait_resume); |
| 157 | } |
| 158 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 159 | |
| 160 | put_all_bios(conf, r1_bio); |
| 161 | mempool_free(r1_bio, conf->r1bio_pool); |
| 162 | } |
| 163 | |
| 164 | static inline void put_buf(r1bio_t *r1_bio) |
| 165 | { |
| 166 | conf_t *conf = mddev_to_conf(r1_bio->mddev); |
| 167 | unsigned long flags; |
| 168 | |
| 169 | mempool_free(r1_bio, conf->r1buf_pool); |
| 170 | |
| 171 | spin_lock_irqsave(&conf->resync_lock, flags); |
| 172 | if (!conf->barrier) |
| 173 | BUG(); |
| 174 | --conf->barrier; |
| 175 | wake_up(&conf->wait_resume); |
| 176 | wake_up(&conf->wait_idle); |
| 177 | |
| 178 | if (!--conf->nr_pending) { |
| 179 | wake_up(&conf->wait_idle); |
| 180 | wake_up(&conf->wait_resume); |
| 181 | } |
| 182 | spin_unlock_irqrestore(&conf->resync_lock, flags); |
| 183 | } |
| 184 | |
| 185 | static void reschedule_retry(r1bio_t *r1_bio) |
| 186 | { |
| 187 | unsigned long flags; |
| 188 | mddev_t *mddev = r1_bio->mddev; |
| 189 | conf_t *conf = mddev_to_conf(mddev); |
| 190 | |
| 191 | spin_lock_irqsave(&conf->device_lock, flags); |
| 192 | list_add(&r1_bio->retry_list, &conf->retry_list); |
| 193 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 194 | |
| 195 | md_wakeup_thread(mddev->thread); |
| 196 | } |
| 197 | |
| 198 | /* |
| 199 | * raid_end_bio_io() is called when we have finished servicing a mirrored |
| 200 | * operation and are ready to return a success/failure code to the buffer |
| 201 | * cache layer. |
| 202 | */ |
| 203 | static void raid_end_bio_io(r1bio_t *r1_bio) |
| 204 | { |
| 205 | struct bio *bio = r1_bio->master_bio; |
| 206 | |
| 207 | bio_endio(bio, bio->bi_size, |
| 208 | test_bit(R1BIO_Uptodate, &r1_bio->state) ? 0 : -EIO); |
| 209 | free_r1bio(r1_bio); |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | * Update disk head position estimator based on IRQ completion info. |
| 214 | */ |
| 215 | static inline void update_head_pos(int disk, r1bio_t *r1_bio) |
| 216 | { |
| 217 | conf_t *conf = mddev_to_conf(r1_bio->mddev); |
| 218 | |
| 219 | conf->mirrors[disk].head_position = |
| 220 | r1_bio->sector + (r1_bio->sectors); |
| 221 | } |
| 222 | |
| 223 | static int raid1_end_read_request(struct bio *bio, unsigned int bytes_done, int error) |
| 224 | { |
| 225 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 226 | r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private); |
| 227 | int mirror; |
| 228 | conf_t *conf = mddev_to_conf(r1_bio->mddev); |
| 229 | |
| 230 | if (bio->bi_size) |
| 231 | return 1; |
| 232 | |
| 233 | mirror = r1_bio->read_disk; |
| 234 | /* |
| 235 | * this branch is our 'one mirror IO has finished' event handler: |
| 236 | */ |
| 237 | if (!uptodate) |
| 238 | md_error(r1_bio->mddev, conf->mirrors[mirror].rdev); |
| 239 | else |
| 240 | /* |
| 241 | * Set R1BIO_Uptodate in our master bio, so that |
| 242 | * we will return a good error code for to the higher |
| 243 | * levels even if IO on some other mirrored buffer fails. |
| 244 | * |
| 245 | * The 'master' represents the composite IO operation to |
| 246 | * user-side. So if something waits for IO, then it will |
| 247 | * wait for the 'master' bio. |
| 248 | */ |
| 249 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
| 250 | |
| 251 | update_head_pos(mirror, r1_bio); |
| 252 | |
| 253 | /* |
| 254 | * we have only one bio on the read side |
| 255 | */ |
| 256 | if (uptodate) |
| 257 | raid_end_bio_io(r1_bio); |
| 258 | else { |
| 259 | /* |
| 260 | * oops, read error: |
| 261 | */ |
| 262 | char b[BDEVNAME_SIZE]; |
| 263 | if (printk_ratelimit()) |
| 264 | printk(KERN_ERR "raid1: %s: rescheduling sector %llu\n", |
| 265 | bdevname(conf->mirrors[mirror].rdev->bdev,b), (unsigned long long)r1_bio->sector); |
| 266 | reschedule_retry(r1_bio); |
| 267 | } |
| 268 | |
| 269 | rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); |
| 270 | return 0; |
| 271 | } |
| 272 | |
| 273 | static int raid1_end_write_request(struct bio *bio, unsigned int bytes_done, int error) |
| 274 | { |
| 275 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 276 | r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private); |
| 277 | int mirror; |
| 278 | conf_t *conf = mddev_to_conf(r1_bio->mddev); |
| 279 | |
| 280 | if (bio->bi_size) |
| 281 | return 1; |
| 282 | |
| 283 | for (mirror = 0; mirror < conf->raid_disks; mirror++) |
| 284 | if (r1_bio->bios[mirror] == bio) |
| 285 | break; |
| 286 | |
| 287 | /* |
| 288 | * this branch is our 'one mirror IO has finished' event handler: |
| 289 | */ |
| 290 | if (!uptodate) |
| 291 | md_error(r1_bio->mddev, conf->mirrors[mirror].rdev); |
| 292 | else |
| 293 | /* |
| 294 | * Set R1BIO_Uptodate in our master bio, so that |
| 295 | * we will return a good error code for to the higher |
| 296 | * levels even if IO on some other mirrored buffer fails. |
| 297 | * |
| 298 | * The 'master' represents the composite IO operation to |
| 299 | * user-side. So if something waits for IO, then it will |
| 300 | * wait for the 'master' bio. |
| 301 | */ |
| 302 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
| 303 | |
| 304 | update_head_pos(mirror, r1_bio); |
| 305 | |
| 306 | /* |
| 307 | * |
| 308 | * Let's see if all mirrored write operations have finished |
| 309 | * already. |
| 310 | */ |
| 311 | if (atomic_dec_and_test(&r1_bio->remaining)) { |
| 312 | md_write_end(r1_bio->mddev); |
| 313 | raid_end_bio_io(r1_bio); |
| 314 | } |
| 315 | |
| 316 | rdev_dec_pending(conf->mirrors[mirror].rdev, conf->mddev); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | |
| 321 | /* |
| 322 | * This routine returns the disk from which the requested read should |
| 323 | * be done. There is a per-array 'next expected sequential IO' sector |
| 324 | * number - if this matches on the next IO then we use the last disk. |
| 325 | * There is also a per-disk 'last know head position' sector that is |
| 326 | * maintained from IRQ contexts, both the normal and the resync IO |
| 327 | * completion handlers update this position correctly. If there is no |
| 328 | * perfect sequential match then we pick the disk whose head is closest. |
| 329 | * |
| 330 | * If there are 2 mirrors in the same 2 devices, performance degrades |
| 331 | * because position is mirror, not device based. |
| 332 | * |
| 333 | * The rdev for the device selected will have nr_pending incremented. |
| 334 | */ |
| 335 | static int read_balance(conf_t *conf, r1bio_t *r1_bio) |
| 336 | { |
| 337 | const unsigned long this_sector = r1_bio->sector; |
| 338 | int new_disk = conf->last_used, disk = new_disk; |
| 339 | const int sectors = r1_bio->sectors; |
| 340 | sector_t new_distance, current_distance; |
| 341 | mdk_rdev_t *new_rdev, *rdev; |
| 342 | |
| 343 | rcu_read_lock(); |
| 344 | /* |
| 345 | * Check if it if we can balance. We can balance on the whole |
| 346 | * device if no resync is going on, or below the resync window. |
| 347 | * We take the first readable disk when above the resync window. |
| 348 | */ |
| 349 | retry: |
| 350 | if (conf->mddev->recovery_cp < MaxSector && |
| 351 | (this_sector + sectors >= conf->next_resync)) { |
| 352 | /* Choose the first operation device, for consistancy */ |
| 353 | new_disk = 0; |
| 354 | |
| 355 | while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL || |
| 356 | !new_rdev->in_sync) { |
| 357 | new_disk++; |
| 358 | if (new_disk == conf->raid_disks) { |
| 359 | new_disk = -1; |
| 360 | break; |
| 361 | } |
| 362 | } |
| 363 | goto rb_out; |
| 364 | } |
| 365 | |
| 366 | |
| 367 | /* make sure the disk is operational */ |
| 368 | while ((new_rdev=conf->mirrors[new_disk].rdev) == NULL || |
| 369 | !new_rdev->in_sync) { |
| 370 | if (new_disk <= 0) |
| 371 | new_disk = conf->raid_disks; |
| 372 | new_disk--; |
| 373 | if (new_disk == disk) { |
| 374 | new_disk = -1; |
| 375 | goto rb_out; |
| 376 | } |
| 377 | } |
| 378 | disk = new_disk; |
| 379 | /* now disk == new_disk == starting point for search */ |
| 380 | |
| 381 | /* |
| 382 | * Don't change to another disk for sequential reads: |
| 383 | */ |
| 384 | if (conf->next_seq_sect == this_sector) |
| 385 | goto rb_out; |
| 386 | if (this_sector == conf->mirrors[new_disk].head_position) |
| 387 | goto rb_out; |
| 388 | |
| 389 | current_distance = abs(this_sector - conf->mirrors[disk].head_position); |
| 390 | |
| 391 | /* Find the disk whose head is closest */ |
| 392 | |
| 393 | do { |
| 394 | if (disk <= 0) |
| 395 | disk = conf->raid_disks; |
| 396 | disk--; |
| 397 | |
| 398 | if ((rdev=conf->mirrors[disk].rdev) == NULL || |
| 399 | !rdev->in_sync) |
| 400 | continue; |
| 401 | |
| 402 | if (!atomic_read(&rdev->nr_pending)) { |
| 403 | new_disk = disk; |
| 404 | new_rdev = rdev; |
| 405 | break; |
| 406 | } |
| 407 | new_distance = abs(this_sector - conf->mirrors[disk].head_position); |
| 408 | if (new_distance < current_distance) { |
| 409 | current_distance = new_distance; |
| 410 | new_disk = disk; |
| 411 | new_rdev = rdev; |
| 412 | } |
| 413 | } while (disk != conf->last_used); |
| 414 | |
| 415 | rb_out: |
| 416 | |
| 417 | |
| 418 | if (new_disk >= 0) { |
| 419 | conf->next_seq_sect = this_sector + sectors; |
| 420 | conf->last_used = new_disk; |
| 421 | atomic_inc(&new_rdev->nr_pending); |
| 422 | if (!new_rdev->in_sync) { |
| 423 | /* cannot risk returning a device that failed |
| 424 | * before we inc'ed nr_pending |
| 425 | */ |
| 426 | atomic_dec(&new_rdev->nr_pending); |
| 427 | goto retry; |
| 428 | } |
| 429 | } |
| 430 | rcu_read_unlock(); |
| 431 | |
| 432 | return new_disk; |
| 433 | } |
| 434 | |
| 435 | static void unplug_slaves(mddev_t *mddev) |
| 436 | { |
| 437 | conf_t *conf = mddev_to_conf(mddev); |
| 438 | int i; |
| 439 | |
| 440 | rcu_read_lock(); |
| 441 | for (i=0; i<mddev->raid_disks; i++) { |
| 442 | mdk_rdev_t *rdev = conf->mirrors[i].rdev; |
| 443 | if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) { |
| 444 | request_queue_t *r_queue = bdev_get_queue(rdev->bdev); |
| 445 | |
| 446 | atomic_inc(&rdev->nr_pending); |
| 447 | rcu_read_unlock(); |
| 448 | |
| 449 | if (r_queue->unplug_fn) |
| 450 | r_queue->unplug_fn(r_queue); |
| 451 | |
| 452 | rdev_dec_pending(rdev, mddev); |
| 453 | rcu_read_lock(); |
| 454 | } |
| 455 | } |
| 456 | rcu_read_unlock(); |
| 457 | } |
| 458 | |
| 459 | static void raid1_unplug(request_queue_t *q) |
| 460 | { |
| 461 | unplug_slaves(q->queuedata); |
| 462 | } |
| 463 | |
| 464 | static int raid1_issue_flush(request_queue_t *q, struct gendisk *disk, |
| 465 | sector_t *error_sector) |
| 466 | { |
| 467 | mddev_t *mddev = q->queuedata; |
| 468 | conf_t *conf = mddev_to_conf(mddev); |
| 469 | int i, ret = 0; |
| 470 | |
| 471 | rcu_read_lock(); |
| 472 | for (i=0; i<mddev->raid_disks && ret == 0; i++) { |
| 473 | mdk_rdev_t *rdev = conf->mirrors[i].rdev; |
| 474 | if (rdev && !rdev->faulty) { |
| 475 | struct block_device *bdev = rdev->bdev; |
| 476 | request_queue_t *r_queue = bdev_get_queue(bdev); |
| 477 | |
| 478 | if (!r_queue->issue_flush_fn) |
| 479 | ret = -EOPNOTSUPP; |
| 480 | else { |
| 481 | atomic_inc(&rdev->nr_pending); |
| 482 | rcu_read_unlock(); |
| 483 | ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, |
| 484 | error_sector); |
| 485 | rdev_dec_pending(rdev, mddev); |
| 486 | rcu_read_lock(); |
| 487 | } |
| 488 | } |
| 489 | } |
| 490 | rcu_read_unlock(); |
| 491 | return ret; |
| 492 | } |
| 493 | |
| 494 | /* |
| 495 | * Throttle resync depth, so that we can both get proper overlapping of |
| 496 | * requests, but are still able to handle normal requests quickly. |
| 497 | */ |
| 498 | #define RESYNC_DEPTH 32 |
| 499 | |
| 500 | static void device_barrier(conf_t *conf, sector_t sect) |
| 501 | { |
| 502 | spin_lock_irq(&conf->resync_lock); |
| 503 | wait_event_lock_irq(conf->wait_idle, !waitqueue_active(&conf->wait_resume), |
| 504 | conf->resync_lock, unplug_slaves(conf->mddev)); |
| 505 | |
| 506 | if (!conf->barrier++) { |
| 507 | wait_event_lock_irq(conf->wait_idle, !conf->nr_pending, |
| 508 | conf->resync_lock, unplug_slaves(conf->mddev)); |
| 509 | if (conf->nr_pending) |
| 510 | BUG(); |
| 511 | } |
| 512 | wait_event_lock_irq(conf->wait_resume, conf->barrier < RESYNC_DEPTH, |
| 513 | conf->resync_lock, unplug_slaves(conf->mddev)); |
| 514 | conf->next_resync = sect; |
| 515 | spin_unlock_irq(&conf->resync_lock); |
| 516 | } |
| 517 | |
| 518 | static int make_request(request_queue_t *q, struct bio * bio) |
| 519 | { |
| 520 | mddev_t *mddev = q->queuedata; |
| 521 | conf_t *conf = mddev_to_conf(mddev); |
| 522 | mirror_info_t *mirror; |
| 523 | r1bio_t *r1_bio; |
| 524 | struct bio *read_bio; |
| 525 | int i, disks; |
| 526 | mdk_rdev_t *rdev; |
| 527 | |
| 528 | /* |
| 529 | * Register the new request and wait if the reconstruction |
| 530 | * thread has put up a bar for new requests. |
| 531 | * Continue immediately if no resync is active currently. |
| 532 | */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame^] | 533 | if (md_write_start(mddev, bio)==0) |
| 534 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | spin_lock_irq(&conf->resync_lock); |
| 536 | wait_event_lock_irq(conf->wait_resume, !conf->barrier, conf->resync_lock, ); |
| 537 | conf->nr_pending++; |
| 538 | spin_unlock_irq(&conf->resync_lock); |
| 539 | |
| 540 | if (bio_data_dir(bio)==WRITE) { |
| 541 | disk_stat_inc(mddev->gendisk, writes); |
| 542 | disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bio)); |
| 543 | } else { |
| 544 | disk_stat_inc(mddev->gendisk, reads); |
| 545 | disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bio)); |
| 546 | } |
| 547 | |
| 548 | /* |
| 549 | * make_request() can abort the operation when READA is being |
| 550 | * used and no empty request is available. |
| 551 | * |
| 552 | */ |
| 553 | r1_bio = mempool_alloc(conf->r1bio_pool, GFP_NOIO); |
| 554 | |
| 555 | r1_bio->master_bio = bio; |
| 556 | r1_bio->sectors = bio->bi_size >> 9; |
| 557 | |
| 558 | r1_bio->mddev = mddev; |
| 559 | r1_bio->sector = bio->bi_sector; |
| 560 | |
| 561 | r1_bio->state = 0; |
| 562 | |
| 563 | if (bio_data_dir(bio) == READ) { |
| 564 | /* |
| 565 | * read balancing logic: |
| 566 | */ |
| 567 | int rdisk = read_balance(conf, r1_bio); |
| 568 | |
| 569 | if (rdisk < 0) { |
| 570 | /* couldn't find anywhere to read from */ |
| 571 | raid_end_bio_io(r1_bio); |
| 572 | return 0; |
| 573 | } |
| 574 | mirror = conf->mirrors + rdisk; |
| 575 | |
| 576 | r1_bio->read_disk = rdisk; |
| 577 | |
| 578 | read_bio = bio_clone(bio, GFP_NOIO); |
| 579 | |
| 580 | r1_bio->bios[rdisk] = read_bio; |
| 581 | |
| 582 | read_bio->bi_sector = r1_bio->sector + mirror->rdev->data_offset; |
| 583 | read_bio->bi_bdev = mirror->rdev->bdev; |
| 584 | read_bio->bi_end_io = raid1_end_read_request; |
| 585 | read_bio->bi_rw = READ; |
| 586 | read_bio->bi_private = r1_bio; |
| 587 | |
| 588 | generic_make_request(read_bio); |
| 589 | return 0; |
| 590 | } |
| 591 | |
| 592 | /* |
| 593 | * WRITE: |
| 594 | */ |
| 595 | /* first select target devices under spinlock and |
| 596 | * inc refcount on their rdev. Record them by setting |
| 597 | * bios[x] to bio |
| 598 | */ |
| 599 | disks = conf->raid_disks; |
| 600 | rcu_read_lock(); |
| 601 | for (i = 0; i < disks; i++) { |
| 602 | if ((rdev=conf->mirrors[i].rdev) != NULL && |
| 603 | !rdev->faulty) { |
| 604 | atomic_inc(&rdev->nr_pending); |
| 605 | if (rdev->faulty) { |
| 606 | atomic_dec(&rdev->nr_pending); |
| 607 | r1_bio->bios[i] = NULL; |
| 608 | } else |
| 609 | r1_bio->bios[i] = bio; |
| 610 | } else |
| 611 | r1_bio->bios[i] = NULL; |
| 612 | } |
| 613 | rcu_read_unlock(); |
| 614 | |
| 615 | atomic_set(&r1_bio->remaining, 1); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame^] | 616 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | for (i = 0; i < disks; i++) { |
| 618 | struct bio *mbio; |
| 619 | if (!r1_bio->bios[i]) |
| 620 | continue; |
| 621 | |
| 622 | mbio = bio_clone(bio, GFP_NOIO); |
| 623 | r1_bio->bios[i] = mbio; |
| 624 | |
| 625 | mbio->bi_sector = r1_bio->sector + conf->mirrors[i].rdev->data_offset; |
| 626 | mbio->bi_bdev = conf->mirrors[i].rdev->bdev; |
| 627 | mbio->bi_end_io = raid1_end_write_request; |
| 628 | mbio->bi_rw = WRITE; |
| 629 | mbio->bi_private = r1_bio; |
| 630 | |
| 631 | atomic_inc(&r1_bio->remaining); |
| 632 | generic_make_request(mbio); |
| 633 | } |
| 634 | |
| 635 | if (atomic_dec_and_test(&r1_bio->remaining)) { |
| 636 | md_write_end(mddev); |
| 637 | raid_end_bio_io(r1_bio); |
| 638 | } |
| 639 | |
| 640 | return 0; |
| 641 | } |
| 642 | |
| 643 | static void status(struct seq_file *seq, mddev_t *mddev) |
| 644 | { |
| 645 | conf_t *conf = mddev_to_conf(mddev); |
| 646 | int i; |
| 647 | |
| 648 | seq_printf(seq, " [%d/%d] [", conf->raid_disks, |
| 649 | conf->working_disks); |
| 650 | for (i = 0; i < conf->raid_disks; i++) |
| 651 | seq_printf(seq, "%s", |
| 652 | conf->mirrors[i].rdev && |
| 653 | conf->mirrors[i].rdev->in_sync ? "U" : "_"); |
| 654 | seq_printf(seq, "]"); |
| 655 | } |
| 656 | |
| 657 | |
| 658 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 659 | { |
| 660 | char b[BDEVNAME_SIZE]; |
| 661 | conf_t *conf = mddev_to_conf(mddev); |
| 662 | |
| 663 | /* |
| 664 | * If it is not operational, then we have already marked it as dead |
| 665 | * else if it is the last working disks, ignore the error, let the |
| 666 | * next level up know. |
| 667 | * else mark the drive as failed |
| 668 | */ |
| 669 | if (rdev->in_sync |
| 670 | && conf->working_disks == 1) |
| 671 | /* |
| 672 | * Don't fail the drive, act as though we were just a |
| 673 | * normal single drive |
| 674 | */ |
| 675 | return; |
| 676 | if (rdev->in_sync) { |
| 677 | mddev->degraded++; |
| 678 | conf->working_disks--; |
| 679 | /* |
| 680 | * if recovery is running, make sure it aborts. |
| 681 | */ |
| 682 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 683 | } |
| 684 | rdev->in_sync = 0; |
| 685 | rdev->faulty = 1; |
| 686 | mddev->sb_dirty = 1; |
| 687 | printk(KERN_ALERT "raid1: Disk failure on %s, disabling device. \n" |
| 688 | " Operation continuing on %d devices\n", |
| 689 | bdevname(rdev->bdev,b), conf->working_disks); |
| 690 | } |
| 691 | |
| 692 | static void print_conf(conf_t *conf) |
| 693 | { |
| 694 | int i; |
| 695 | mirror_info_t *tmp; |
| 696 | |
| 697 | printk("RAID1 conf printout:\n"); |
| 698 | if (!conf) { |
| 699 | printk("(!conf)\n"); |
| 700 | return; |
| 701 | } |
| 702 | printk(" --- wd:%d rd:%d\n", conf->working_disks, |
| 703 | conf->raid_disks); |
| 704 | |
| 705 | for (i = 0; i < conf->raid_disks; i++) { |
| 706 | char b[BDEVNAME_SIZE]; |
| 707 | tmp = conf->mirrors + i; |
| 708 | if (tmp->rdev) |
| 709 | printk(" disk %d, wo:%d, o:%d, dev:%s\n", |
| 710 | i, !tmp->rdev->in_sync, !tmp->rdev->faulty, |
| 711 | bdevname(tmp->rdev->bdev,b)); |
| 712 | } |
| 713 | } |
| 714 | |
| 715 | static void close_sync(conf_t *conf) |
| 716 | { |
| 717 | spin_lock_irq(&conf->resync_lock); |
| 718 | wait_event_lock_irq(conf->wait_resume, !conf->barrier, |
| 719 | conf->resync_lock, unplug_slaves(conf->mddev)); |
| 720 | spin_unlock_irq(&conf->resync_lock); |
| 721 | |
| 722 | if (conf->barrier) BUG(); |
| 723 | if (waitqueue_active(&conf->wait_idle)) BUG(); |
| 724 | |
| 725 | mempool_destroy(conf->r1buf_pool); |
| 726 | conf->r1buf_pool = NULL; |
| 727 | } |
| 728 | |
| 729 | static int raid1_spare_active(mddev_t *mddev) |
| 730 | { |
| 731 | int i; |
| 732 | conf_t *conf = mddev->private; |
| 733 | mirror_info_t *tmp; |
| 734 | |
| 735 | /* |
| 736 | * Find all failed disks within the RAID1 configuration |
| 737 | * and mark them readable |
| 738 | */ |
| 739 | for (i = 0; i < conf->raid_disks; i++) { |
| 740 | tmp = conf->mirrors + i; |
| 741 | if (tmp->rdev |
| 742 | && !tmp->rdev->faulty |
| 743 | && !tmp->rdev->in_sync) { |
| 744 | conf->working_disks++; |
| 745 | mddev->degraded--; |
| 746 | tmp->rdev->in_sync = 1; |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | print_conf(conf); |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | |
| 755 | static int raid1_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 756 | { |
| 757 | conf_t *conf = mddev->private; |
| 758 | int found = 0; |
| 759 | int mirror; |
| 760 | mirror_info_t *p; |
| 761 | |
| 762 | for (mirror=0; mirror < mddev->raid_disks; mirror++) |
| 763 | if ( !(p=conf->mirrors+mirror)->rdev) { |
| 764 | |
| 765 | blk_queue_stack_limits(mddev->queue, |
| 766 | rdev->bdev->bd_disk->queue); |
| 767 | /* as we don't honour merge_bvec_fn, we must never risk |
| 768 | * violating it, so limit ->max_sector to one PAGE, as |
| 769 | * a one page request is never in violation. |
| 770 | */ |
| 771 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn && |
| 772 | mddev->queue->max_sectors > (PAGE_SIZE>>9)) |
| 773 | blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9); |
| 774 | |
| 775 | p->head_position = 0; |
| 776 | rdev->raid_disk = mirror; |
| 777 | found = 1; |
| 778 | p->rdev = rdev; |
| 779 | break; |
| 780 | } |
| 781 | |
| 782 | print_conf(conf); |
| 783 | return found; |
| 784 | } |
| 785 | |
| 786 | static int raid1_remove_disk(mddev_t *mddev, int number) |
| 787 | { |
| 788 | conf_t *conf = mddev->private; |
| 789 | int err = 0; |
| 790 | mdk_rdev_t *rdev; |
| 791 | mirror_info_t *p = conf->mirrors+ number; |
| 792 | |
| 793 | print_conf(conf); |
| 794 | rdev = p->rdev; |
| 795 | if (rdev) { |
| 796 | if (rdev->in_sync || |
| 797 | atomic_read(&rdev->nr_pending)) { |
| 798 | err = -EBUSY; |
| 799 | goto abort; |
| 800 | } |
| 801 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 802 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 803 | if (atomic_read(&rdev->nr_pending)) { |
| 804 | /* lost the race, try later */ |
| 805 | err = -EBUSY; |
| 806 | p->rdev = rdev; |
| 807 | } |
| 808 | } |
| 809 | abort: |
| 810 | |
| 811 | print_conf(conf); |
| 812 | return err; |
| 813 | } |
| 814 | |
| 815 | |
| 816 | static int end_sync_read(struct bio *bio, unsigned int bytes_done, int error) |
| 817 | { |
| 818 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 819 | r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private); |
| 820 | conf_t *conf = mddev_to_conf(r1_bio->mddev); |
| 821 | |
| 822 | if (bio->bi_size) |
| 823 | return 1; |
| 824 | |
| 825 | if (r1_bio->bios[r1_bio->read_disk] != bio) |
| 826 | BUG(); |
| 827 | update_head_pos(r1_bio->read_disk, r1_bio); |
| 828 | /* |
| 829 | * we have read a block, now it needs to be re-written, |
| 830 | * or re-read if the read failed. |
| 831 | * We don't do much here, just schedule handling by raid1d |
| 832 | */ |
| 833 | if (!uptodate) |
| 834 | md_error(r1_bio->mddev, |
| 835 | conf->mirrors[r1_bio->read_disk].rdev); |
| 836 | else |
| 837 | set_bit(R1BIO_Uptodate, &r1_bio->state); |
| 838 | rdev_dec_pending(conf->mirrors[r1_bio->read_disk].rdev, conf->mddev); |
| 839 | reschedule_retry(r1_bio); |
| 840 | return 0; |
| 841 | } |
| 842 | |
| 843 | static int end_sync_write(struct bio *bio, unsigned int bytes_done, int error) |
| 844 | { |
| 845 | int uptodate = test_bit(BIO_UPTODATE, &bio->bi_flags); |
| 846 | r1bio_t * r1_bio = (r1bio_t *)(bio->bi_private); |
| 847 | mddev_t *mddev = r1_bio->mddev; |
| 848 | conf_t *conf = mddev_to_conf(mddev); |
| 849 | int i; |
| 850 | int mirror=0; |
| 851 | |
| 852 | if (bio->bi_size) |
| 853 | return 1; |
| 854 | |
| 855 | for (i = 0; i < conf->raid_disks; i++) |
| 856 | if (r1_bio->bios[i] == bio) { |
| 857 | mirror = i; |
| 858 | break; |
| 859 | } |
| 860 | if (!uptodate) |
| 861 | md_error(mddev, conf->mirrors[mirror].rdev); |
| 862 | update_head_pos(mirror, r1_bio); |
| 863 | |
| 864 | if (atomic_dec_and_test(&r1_bio->remaining)) { |
| 865 | md_done_sync(mddev, r1_bio->sectors, uptodate); |
| 866 | put_buf(r1_bio); |
| 867 | } |
| 868 | rdev_dec_pending(conf->mirrors[mirror].rdev, mddev); |
| 869 | return 0; |
| 870 | } |
| 871 | |
| 872 | static void sync_request_write(mddev_t *mddev, r1bio_t *r1_bio) |
| 873 | { |
| 874 | conf_t *conf = mddev_to_conf(mddev); |
| 875 | int i; |
| 876 | int disks = conf->raid_disks; |
| 877 | struct bio *bio, *wbio; |
| 878 | |
| 879 | bio = r1_bio->bios[r1_bio->read_disk]; |
| 880 | |
| 881 | /* |
| 882 | * schedule writes |
| 883 | */ |
| 884 | if (!test_bit(R1BIO_Uptodate, &r1_bio->state)) { |
| 885 | /* |
| 886 | * There is no point trying a read-for-reconstruct as |
| 887 | * reconstruct is about to be aborted |
| 888 | */ |
| 889 | char b[BDEVNAME_SIZE]; |
| 890 | printk(KERN_ALERT "raid1: %s: unrecoverable I/O read error" |
| 891 | " for block %llu\n", |
| 892 | bdevname(bio->bi_bdev,b), |
| 893 | (unsigned long long)r1_bio->sector); |
| 894 | md_done_sync(mddev, r1_bio->sectors, 0); |
| 895 | put_buf(r1_bio); |
| 896 | return; |
| 897 | } |
| 898 | |
| 899 | atomic_set(&r1_bio->remaining, 1); |
| 900 | for (i = 0; i < disks ; i++) { |
| 901 | wbio = r1_bio->bios[i]; |
| 902 | if (wbio->bi_end_io != end_sync_write) |
| 903 | continue; |
| 904 | |
| 905 | atomic_inc(&conf->mirrors[i].rdev->nr_pending); |
| 906 | atomic_inc(&r1_bio->remaining); |
| 907 | md_sync_acct(conf->mirrors[i].rdev->bdev, wbio->bi_size >> 9); |
| 908 | generic_make_request(wbio); |
| 909 | } |
| 910 | |
| 911 | if (atomic_dec_and_test(&r1_bio->remaining)) { |
| 912 | md_done_sync(mddev, r1_bio->sectors, 1); |
| 913 | put_buf(r1_bio); |
| 914 | } |
| 915 | } |
| 916 | |
| 917 | /* |
| 918 | * This is a kernel thread which: |
| 919 | * |
| 920 | * 1. Retries failed read operations on working mirrors. |
| 921 | * 2. Updates the raid superblock when problems encounter. |
| 922 | * 3. Performs writes following reads for array syncronising. |
| 923 | */ |
| 924 | |
| 925 | static void raid1d(mddev_t *mddev) |
| 926 | { |
| 927 | r1bio_t *r1_bio; |
| 928 | struct bio *bio; |
| 929 | unsigned long flags; |
| 930 | conf_t *conf = mddev_to_conf(mddev); |
| 931 | struct list_head *head = &conf->retry_list; |
| 932 | int unplug=0; |
| 933 | mdk_rdev_t *rdev; |
| 934 | |
| 935 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 936 | |
| 937 | for (;;) { |
| 938 | char b[BDEVNAME_SIZE]; |
| 939 | spin_lock_irqsave(&conf->device_lock, flags); |
| 940 | if (list_empty(head)) |
| 941 | break; |
| 942 | r1_bio = list_entry(head->prev, r1bio_t, retry_list); |
| 943 | list_del(head->prev); |
| 944 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 945 | |
| 946 | mddev = r1_bio->mddev; |
| 947 | conf = mddev_to_conf(mddev); |
| 948 | if (test_bit(R1BIO_IsSync, &r1_bio->state)) { |
| 949 | sync_request_write(mddev, r1_bio); |
| 950 | unplug = 1; |
| 951 | } else { |
| 952 | int disk; |
| 953 | bio = r1_bio->bios[r1_bio->read_disk]; |
| 954 | if ((disk=read_balance(conf, r1_bio)) == -1) { |
| 955 | printk(KERN_ALERT "raid1: %s: unrecoverable I/O" |
| 956 | " read error for block %llu\n", |
| 957 | bdevname(bio->bi_bdev,b), |
| 958 | (unsigned long long)r1_bio->sector); |
| 959 | raid_end_bio_io(r1_bio); |
| 960 | } else { |
| 961 | r1_bio->bios[r1_bio->read_disk] = NULL; |
| 962 | r1_bio->read_disk = disk; |
| 963 | bio_put(bio); |
| 964 | bio = bio_clone(r1_bio->master_bio, GFP_NOIO); |
| 965 | r1_bio->bios[r1_bio->read_disk] = bio; |
| 966 | rdev = conf->mirrors[disk].rdev; |
| 967 | if (printk_ratelimit()) |
| 968 | printk(KERN_ERR "raid1: %s: redirecting sector %llu to" |
| 969 | " another mirror\n", |
| 970 | bdevname(rdev->bdev,b), |
| 971 | (unsigned long long)r1_bio->sector); |
| 972 | bio->bi_sector = r1_bio->sector + rdev->data_offset; |
| 973 | bio->bi_bdev = rdev->bdev; |
| 974 | bio->bi_end_io = raid1_end_read_request; |
| 975 | bio->bi_rw = READ; |
| 976 | bio->bi_private = r1_bio; |
| 977 | unplug = 1; |
| 978 | generic_make_request(bio); |
| 979 | } |
| 980 | } |
| 981 | } |
| 982 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 983 | if (unplug) |
| 984 | unplug_slaves(mddev); |
| 985 | } |
| 986 | |
| 987 | |
| 988 | static int init_resync(conf_t *conf) |
| 989 | { |
| 990 | int buffs; |
| 991 | |
| 992 | buffs = RESYNC_WINDOW / RESYNC_BLOCK_SIZE; |
| 993 | if (conf->r1buf_pool) |
| 994 | BUG(); |
| 995 | conf->r1buf_pool = mempool_create(buffs, r1buf_pool_alloc, r1buf_pool_free, |
| 996 | conf->poolinfo); |
| 997 | if (!conf->r1buf_pool) |
| 998 | return -ENOMEM; |
| 999 | conf->next_resync = 0; |
| 1000 | return 0; |
| 1001 | } |
| 1002 | |
| 1003 | /* |
| 1004 | * perform a "sync" on one "block" |
| 1005 | * |
| 1006 | * We need to make sure that no normal I/O request - particularly write |
| 1007 | * requests - conflict with active sync requests. |
| 1008 | * |
| 1009 | * This is achieved by tracking pending requests and a 'barrier' concept |
| 1010 | * that can be installed to exclude normal IO requests. |
| 1011 | */ |
| 1012 | |
| 1013 | static int sync_request(mddev_t *mddev, sector_t sector_nr, int go_faster) |
| 1014 | { |
| 1015 | conf_t *conf = mddev_to_conf(mddev); |
| 1016 | mirror_info_t *mirror; |
| 1017 | r1bio_t *r1_bio; |
| 1018 | struct bio *bio; |
| 1019 | sector_t max_sector, nr_sectors; |
| 1020 | int disk; |
| 1021 | int i; |
| 1022 | int write_targets = 0; |
| 1023 | |
| 1024 | if (!conf->r1buf_pool) |
| 1025 | if (init_resync(conf)) |
| 1026 | return -ENOMEM; |
| 1027 | |
| 1028 | max_sector = mddev->size << 1; |
| 1029 | if (sector_nr >= max_sector) { |
| 1030 | close_sync(conf); |
| 1031 | return 0; |
| 1032 | } |
| 1033 | |
| 1034 | /* |
| 1035 | * If there is non-resync activity waiting for us then |
| 1036 | * put in a delay to throttle resync. |
| 1037 | */ |
| 1038 | if (!go_faster && waitqueue_active(&conf->wait_resume)) |
| 1039 | msleep_interruptible(1000); |
| 1040 | device_barrier(conf, sector_nr + RESYNC_SECTORS); |
| 1041 | |
| 1042 | /* |
| 1043 | * If reconstructing, and >1 working disc, |
| 1044 | * could dedicate one to rebuild and others to |
| 1045 | * service read requests .. |
| 1046 | */ |
| 1047 | disk = conf->last_used; |
| 1048 | /* make sure disk is operational */ |
| 1049 | |
| 1050 | while (conf->mirrors[disk].rdev == NULL || |
| 1051 | !conf->mirrors[disk].rdev->in_sync) { |
| 1052 | if (disk <= 0) |
| 1053 | disk = conf->raid_disks; |
| 1054 | disk--; |
| 1055 | if (disk == conf->last_used) |
| 1056 | break; |
| 1057 | } |
| 1058 | conf->last_used = disk; |
| 1059 | atomic_inc(&conf->mirrors[disk].rdev->nr_pending); |
| 1060 | |
| 1061 | |
| 1062 | mirror = conf->mirrors + disk; |
| 1063 | |
| 1064 | r1_bio = mempool_alloc(conf->r1buf_pool, GFP_NOIO); |
| 1065 | |
| 1066 | spin_lock_irq(&conf->resync_lock); |
| 1067 | conf->nr_pending++; |
| 1068 | spin_unlock_irq(&conf->resync_lock); |
| 1069 | |
| 1070 | r1_bio->mddev = mddev; |
| 1071 | r1_bio->sector = sector_nr; |
| 1072 | set_bit(R1BIO_IsSync, &r1_bio->state); |
| 1073 | r1_bio->read_disk = disk; |
| 1074 | |
| 1075 | for (i=0; i < conf->raid_disks; i++) { |
| 1076 | bio = r1_bio->bios[i]; |
| 1077 | |
| 1078 | /* take from bio_init */ |
| 1079 | bio->bi_next = NULL; |
| 1080 | bio->bi_flags |= 1 << BIO_UPTODATE; |
| 1081 | bio->bi_rw = 0; |
| 1082 | bio->bi_vcnt = 0; |
| 1083 | bio->bi_idx = 0; |
| 1084 | bio->bi_phys_segments = 0; |
| 1085 | bio->bi_hw_segments = 0; |
| 1086 | bio->bi_size = 0; |
| 1087 | bio->bi_end_io = NULL; |
| 1088 | bio->bi_private = NULL; |
| 1089 | |
| 1090 | if (i == disk) { |
| 1091 | bio->bi_rw = READ; |
| 1092 | bio->bi_end_io = end_sync_read; |
| 1093 | } else if (conf->mirrors[i].rdev && |
| 1094 | !conf->mirrors[i].rdev->faulty && |
| 1095 | (!conf->mirrors[i].rdev->in_sync || |
| 1096 | sector_nr + RESYNC_SECTORS > mddev->recovery_cp)) { |
| 1097 | bio->bi_rw = WRITE; |
| 1098 | bio->bi_end_io = end_sync_write; |
| 1099 | write_targets ++; |
| 1100 | } else |
| 1101 | continue; |
| 1102 | bio->bi_sector = sector_nr + conf->mirrors[i].rdev->data_offset; |
| 1103 | bio->bi_bdev = conf->mirrors[i].rdev->bdev; |
| 1104 | bio->bi_private = r1_bio; |
| 1105 | } |
| 1106 | if (write_targets == 0) { |
| 1107 | /* There is nowhere to write, so all non-sync |
| 1108 | * drives must be failed - so we are finished |
| 1109 | */ |
| 1110 | int rv = max_sector - sector_nr; |
| 1111 | md_done_sync(mddev, rv, 1); |
| 1112 | put_buf(r1_bio); |
| 1113 | rdev_dec_pending(conf->mirrors[disk].rdev, mddev); |
| 1114 | return rv; |
| 1115 | } |
| 1116 | |
| 1117 | nr_sectors = 0; |
| 1118 | do { |
| 1119 | struct page *page; |
| 1120 | int len = PAGE_SIZE; |
| 1121 | if (sector_nr + (len>>9) > max_sector) |
| 1122 | len = (max_sector - sector_nr) << 9; |
| 1123 | if (len == 0) |
| 1124 | break; |
| 1125 | for (i=0 ; i < conf->raid_disks; i++) { |
| 1126 | bio = r1_bio->bios[i]; |
| 1127 | if (bio->bi_end_io) { |
| 1128 | page = r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page; |
| 1129 | if (bio_add_page(bio, page, len, 0) == 0) { |
| 1130 | /* stop here */ |
| 1131 | r1_bio->bios[0]->bi_io_vec[bio->bi_vcnt].bv_page = page; |
| 1132 | while (i > 0) { |
| 1133 | i--; |
| 1134 | bio = r1_bio->bios[i]; |
| 1135 | if (bio->bi_end_io==NULL) continue; |
| 1136 | /* remove last page from this bio */ |
| 1137 | bio->bi_vcnt--; |
| 1138 | bio->bi_size -= len; |
| 1139 | bio->bi_flags &= ~(1<< BIO_SEG_VALID); |
| 1140 | } |
| 1141 | goto bio_full; |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | nr_sectors += len>>9; |
| 1146 | sector_nr += len>>9; |
| 1147 | } while (r1_bio->bios[disk]->bi_vcnt < RESYNC_PAGES); |
| 1148 | bio_full: |
| 1149 | bio = r1_bio->bios[disk]; |
| 1150 | r1_bio->sectors = nr_sectors; |
| 1151 | |
| 1152 | md_sync_acct(mirror->rdev->bdev, nr_sectors); |
| 1153 | |
| 1154 | generic_make_request(bio); |
| 1155 | |
| 1156 | return nr_sectors; |
| 1157 | } |
| 1158 | |
| 1159 | static int run(mddev_t *mddev) |
| 1160 | { |
| 1161 | conf_t *conf; |
| 1162 | int i, j, disk_idx; |
| 1163 | mirror_info_t *disk; |
| 1164 | mdk_rdev_t *rdev; |
| 1165 | struct list_head *tmp; |
| 1166 | |
| 1167 | if (mddev->level != 1) { |
| 1168 | printk("raid1: %s: raid level not set to mirroring (%d)\n", |
| 1169 | mdname(mddev), mddev->level); |
| 1170 | goto out; |
| 1171 | } |
| 1172 | /* |
| 1173 | * copy the already verified devices into our private RAID1 |
| 1174 | * bookkeeping area. [whatever we allocate in run(), |
| 1175 | * should be freed in stop()] |
| 1176 | */ |
| 1177 | conf = kmalloc(sizeof(conf_t), GFP_KERNEL); |
| 1178 | mddev->private = conf; |
| 1179 | if (!conf) |
| 1180 | goto out_no_mem; |
| 1181 | |
| 1182 | memset(conf, 0, sizeof(*conf)); |
| 1183 | conf->mirrors = kmalloc(sizeof(struct mirror_info)*mddev->raid_disks, |
| 1184 | GFP_KERNEL); |
| 1185 | if (!conf->mirrors) |
| 1186 | goto out_no_mem; |
| 1187 | |
| 1188 | memset(conf->mirrors, 0, sizeof(struct mirror_info)*mddev->raid_disks); |
| 1189 | |
| 1190 | conf->poolinfo = kmalloc(sizeof(*conf->poolinfo), GFP_KERNEL); |
| 1191 | if (!conf->poolinfo) |
| 1192 | goto out_no_mem; |
| 1193 | conf->poolinfo->mddev = mddev; |
| 1194 | conf->poolinfo->raid_disks = mddev->raid_disks; |
| 1195 | conf->r1bio_pool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, |
| 1196 | r1bio_pool_free, |
| 1197 | conf->poolinfo); |
| 1198 | if (!conf->r1bio_pool) |
| 1199 | goto out_no_mem; |
| 1200 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1201 | ITERATE_RDEV(mddev, rdev, tmp) { |
| 1202 | disk_idx = rdev->raid_disk; |
| 1203 | if (disk_idx >= mddev->raid_disks |
| 1204 | || disk_idx < 0) |
| 1205 | continue; |
| 1206 | disk = conf->mirrors + disk_idx; |
| 1207 | |
| 1208 | disk->rdev = rdev; |
| 1209 | |
| 1210 | blk_queue_stack_limits(mddev->queue, |
| 1211 | rdev->bdev->bd_disk->queue); |
| 1212 | /* as we don't honour merge_bvec_fn, we must never risk |
| 1213 | * violating it, so limit ->max_sector to one PAGE, as |
| 1214 | * a one page request is never in violation. |
| 1215 | */ |
| 1216 | if (rdev->bdev->bd_disk->queue->merge_bvec_fn && |
| 1217 | mddev->queue->max_sectors > (PAGE_SIZE>>9)) |
| 1218 | blk_queue_max_sectors(mddev->queue, PAGE_SIZE>>9); |
| 1219 | |
| 1220 | disk->head_position = 0; |
| 1221 | if (!rdev->faulty && rdev->in_sync) |
| 1222 | conf->working_disks++; |
| 1223 | } |
| 1224 | conf->raid_disks = mddev->raid_disks; |
| 1225 | conf->mddev = mddev; |
| 1226 | spin_lock_init(&conf->device_lock); |
| 1227 | INIT_LIST_HEAD(&conf->retry_list); |
| 1228 | if (conf->working_disks == 1) |
| 1229 | mddev->recovery_cp = MaxSector; |
| 1230 | |
| 1231 | spin_lock_init(&conf->resync_lock); |
| 1232 | init_waitqueue_head(&conf->wait_idle); |
| 1233 | init_waitqueue_head(&conf->wait_resume); |
| 1234 | |
| 1235 | if (!conf->working_disks) { |
| 1236 | printk(KERN_ERR "raid1: no operational mirrors for %s\n", |
| 1237 | mdname(mddev)); |
| 1238 | goto out_free_conf; |
| 1239 | } |
| 1240 | |
| 1241 | mddev->degraded = 0; |
| 1242 | for (i = 0; i < conf->raid_disks; i++) { |
| 1243 | |
| 1244 | disk = conf->mirrors + i; |
| 1245 | |
| 1246 | if (!disk->rdev) { |
| 1247 | disk->head_position = 0; |
| 1248 | mddev->degraded++; |
| 1249 | } |
| 1250 | } |
| 1251 | |
| 1252 | /* |
| 1253 | * find the first working one and use it as a starting point |
| 1254 | * to read balancing. |
| 1255 | */ |
| 1256 | for (j = 0; j < conf->raid_disks && |
| 1257 | (!conf->mirrors[j].rdev || |
| 1258 | !conf->mirrors[j].rdev->in_sync) ; j++) |
| 1259 | /* nothing */; |
| 1260 | conf->last_used = j; |
| 1261 | |
| 1262 | |
| 1263 | |
| 1264 | { |
| 1265 | mddev->thread = md_register_thread(raid1d, mddev, "%s_raid1"); |
| 1266 | if (!mddev->thread) { |
| 1267 | printk(KERN_ERR |
| 1268 | "raid1: couldn't allocate thread for %s\n", |
| 1269 | mdname(mddev)); |
| 1270 | goto out_free_conf; |
| 1271 | } |
| 1272 | } |
| 1273 | printk(KERN_INFO |
| 1274 | "raid1: raid set %s active with %d out of %d mirrors\n", |
| 1275 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
| 1276 | mddev->raid_disks); |
| 1277 | /* |
| 1278 | * Ok, everything is just fine now |
| 1279 | */ |
| 1280 | mddev->array_size = mddev->size; |
| 1281 | |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 1282 | mddev->queue->unplug_fn = raid1_unplug; |
| 1283 | mddev->queue->issue_flush_fn = raid1_issue_flush; |
| 1284 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1285 | return 0; |
| 1286 | |
| 1287 | out_no_mem: |
| 1288 | printk(KERN_ERR "raid1: couldn't allocate memory for %s\n", |
| 1289 | mdname(mddev)); |
| 1290 | |
| 1291 | out_free_conf: |
| 1292 | if (conf) { |
| 1293 | if (conf->r1bio_pool) |
| 1294 | mempool_destroy(conf->r1bio_pool); |
| 1295 | if (conf->mirrors) |
| 1296 | kfree(conf->mirrors); |
| 1297 | if (conf->poolinfo) |
| 1298 | kfree(conf->poolinfo); |
| 1299 | kfree(conf); |
| 1300 | mddev->private = NULL; |
| 1301 | } |
| 1302 | out: |
| 1303 | return -EIO; |
| 1304 | } |
| 1305 | |
| 1306 | static int stop(mddev_t *mddev) |
| 1307 | { |
| 1308 | conf_t *conf = mddev_to_conf(mddev); |
| 1309 | |
| 1310 | md_unregister_thread(mddev->thread); |
| 1311 | mddev->thread = NULL; |
| 1312 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 1313 | if (conf->r1bio_pool) |
| 1314 | mempool_destroy(conf->r1bio_pool); |
| 1315 | if (conf->mirrors) |
| 1316 | kfree(conf->mirrors); |
| 1317 | if (conf->poolinfo) |
| 1318 | kfree(conf->poolinfo); |
| 1319 | kfree(conf); |
| 1320 | mddev->private = NULL; |
| 1321 | return 0; |
| 1322 | } |
| 1323 | |
| 1324 | static int raid1_resize(mddev_t *mddev, sector_t sectors) |
| 1325 | { |
| 1326 | /* no resync is happening, and there is enough space |
| 1327 | * on all devices, so we can resize. |
| 1328 | * We need to make sure resync covers any new space. |
| 1329 | * If the array is shrinking we should possibly wait until |
| 1330 | * any io in the removed space completes, but it hardly seems |
| 1331 | * worth it. |
| 1332 | */ |
| 1333 | mddev->array_size = sectors>>1; |
| 1334 | set_capacity(mddev->gendisk, mddev->array_size << 1); |
| 1335 | mddev->changed = 1; |
| 1336 | if (mddev->array_size > mddev->size && mddev->recovery_cp == MaxSector) { |
| 1337 | mddev->recovery_cp = mddev->size << 1; |
| 1338 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 1339 | } |
| 1340 | mddev->size = mddev->array_size; |
| 1341 | return 0; |
| 1342 | } |
| 1343 | |
| 1344 | static int raid1_reshape(mddev_t *mddev, int raid_disks) |
| 1345 | { |
| 1346 | /* We need to: |
| 1347 | * 1/ resize the r1bio_pool |
| 1348 | * 2/ resize conf->mirrors |
| 1349 | * |
| 1350 | * We allocate a new r1bio_pool if we can. |
| 1351 | * Then raise a device barrier and wait until all IO stops. |
| 1352 | * Then resize conf->mirrors and swap in the new r1bio pool. |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 1353 | * |
| 1354 | * At the same time, we "pack" the devices so that all the missing |
| 1355 | * devices have the higher raid_disk numbers. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1356 | */ |
| 1357 | mempool_t *newpool, *oldpool; |
| 1358 | struct pool_info *newpoolinfo; |
| 1359 | mirror_info_t *newmirrors; |
| 1360 | conf_t *conf = mddev_to_conf(mddev); |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 1361 | int cnt; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1362 | |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 1363 | int d, d2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1364 | |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 1365 | if (raid_disks < conf->raid_disks) { |
| 1366 | cnt=0; |
| 1367 | for (d= 0; d < conf->raid_disks; d++) |
| 1368 | if (conf->mirrors[d].rdev) |
| 1369 | cnt++; |
| 1370 | if (cnt > raid_disks) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1371 | return -EBUSY; |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 1372 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1373 | |
| 1374 | newpoolinfo = kmalloc(sizeof(*newpoolinfo), GFP_KERNEL); |
| 1375 | if (!newpoolinfo) |
| 1376 | return -ENOMEM; |
| 1377 | newpoolinfo->mddev = mddev; |
| 1378 | newpoolinfo->raid_disks = raid_disks; |
| 1379 | |
| 1380 | newpool = mempool_create(NR_RAID1_BIOS, r1bio_pool_alloc, |
| 1381 | r1bio_pool_free, newpoolinfo); |
| 1382 | if (!newpool) { |
| 1383 | kfree(newpoolinfo); |
| 1384 | return -ENOMEM; |
| 1385 | } |
| 1386 | newmirrors = kmalloc(sizeof(struct mirror_info) * raid_disks, GFP_KERNEL); |
| 1387 | if (!newmirrors) { |
| 1388 | kfree(newpoolinfo); |
| 1389 | mempool_destroy(newpool); |
| 1390 | return -ENOMEM; |
| 1391 | } |
| 1392 | memset(newmirrors, 0, sizeof(struct mirror_info)*raid_disks); |
| 1393 | |
| 1394 | spin_lock_irq(&conf->resync_lock); |
| 1395 | conf->barrier++; |
| 1396 | wait_event_lock_irq(conf->wait_idle, !conf->nr_pending, |
| 1397 | conf->resync_lock, unplug_slaves(mddev)); |
| 1398 | spin_unlock_irq(&conf->resync_lock); |
| 1399 | |
| 1400 | /* ok, everything is stopped */ |
| 1401 | oldpool = conf->r1bio_pool; |
| 1402 | conf->r1bio_pool = newpool; |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 1403 | |
| 1404 | for (d=d2=0; d < conf->raid_disks; d++) |
| 1405 | if (conf->mirrors[d].rdev) { |
| 1406 | conf->mirrors[d].rdev->raid_disk = d2; |
| 1407 | newmirrors[d2++].rdev = conf->mirrors[d].rdev; |
| 1408 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | kfree(conf->mirrors); |
| 1410 | conf->mirrors = newmirrors; |
| 1411 | kfree(conf->poolinfo); |
| 1412 | conf->poolinfo = newpoolinfo; |
| 1413 | |
| 1414 | mddev->degraded += (raid_disks - conf->raid_disks); |
| 1415 | conf->raid_disks = mddev->raid_disks = raid_disks; |
| 1416 | |
NeilBrown | 6ea9c07 | 2005-06-21 17:17:09 -0700 | [diff] [blame] | 1417 | conf->last_used = 0; /* just make sure it is in-range */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1418 | spin_lock_irq(&conf->resync_lock); |
| 1419 | conf->barrier--; |
| 1420 | spin_unlock_irq(&conf->resync_lock); |
| 1421 | wake_up(&conf->wait_resume); |
| 1422 | wake_up(&conf->wait_idle); |
| 1423 | |
| 1424 | |
| 1425 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 1426 | md_wakeup_thread(mddev->thread); |
| 1427 | |
| 1428 | mempool_destroy(oldpool); |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
| 1432 | |
| 1433 | static mdk_personality_t raid1_personality = |
| 1434 | { |
| 1435 | .name = "raid1", |
| 1436 | .owner = THIS_MODULE, |
| 1437 | .make_request = make_request, |
| 1438 | .run = run, |
| 1439 | .stop = stop, |
| 1440 | .status = status, |
| 1441 | .error_handler = error, |
| 1442 | .hot_add_disk = raid1_add_disk, |
| 1443 | .hot_remove_disk= raid1_remove_disk, |
| 1444 | .spare_active = raid1_spare_active, |
| 1445 | .sync_request = sync_request, |
| 1446 | .resize = raid1_resize, |
| 1447 | .reshape = raid1_reshape, |
| 1448 | }; |
| 1449 | |
| 1450 | static int __init raid_init(void) |
| 1451 | { |
| 1452 | return register_md_personality(RAID1, &raid1_personality); |
| 1453 | } |
| 1454 | |
| 1455 | static void raid_exit(void) |
| 1456 | { |
| 1457 | unregister_md_personality(RAID1); |
| 1458 | } |
| 1459 | |
| 1460 | module_init(raid_init); |
| 1461 | module_exit(raid_exit); |
| 1462 | MODULE_LICENSE("GPL"); |
| 1463 | MODULE_ALIAS("md-personality-3"); /* RAID1 */ |