Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * raid5.c : Multiple Devices driver for Linux |
| 3 | * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman |
| 4 | * Copyright (C) 1999, 2000 Ingo Molnar |
| 5 | * |
| 6 | * RAID-5 management functions. |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public License |
| 14 | * (for example /usr/src/linux/COPYING); if not, write to the Free |
| 15 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 16 | */ |
| 17 | |
| 18 | |
| 19 | #include <linux/config.h> |
| 20 | #include <linux/module.h> |
| 21 | #include <linux/slab.h> |
| 22 | #include <linux/raid/raid5.h> |
| 23 | #include <linux/highmem.h> |
| 24 | #include <linux/bitops.h> |
| 25 | #include <asm/atomic.h> |
| 26 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 27 | #include <linux/raid/bitmap.h> |
| 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | /* |
| 30 | * Stripe cache |
| 31 | */ |
| 32 | |
| 33 | #define NR_STRIPES 256 |
| 34 | #define STRIPE_SIZE PAGE_SIZE |
| 35 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 36 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 37 | #define IO_THRESHOLD 1 |
| 38 | #define HASH_PAGES 1 |
| 39 | #define HASH_PAGES_ORDER 0 |
| 40 | #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *)) |
| 41 | #define HASH_MASK (NR_HASH - 1) |
| 42 | |
| 43 | #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]) |
| 44 | |
| 45 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 46 | * order without overlap. There may be several bio's per stripe+device, and |
| 47 | * a bio could span several devices. |
| 48 | * When walking this list for a particular stripe+device, we must never proceed |
| 49 | * beyond a bio that extends past this device, as the next bio might no longer |
| 50 | * be valid. |
| 51 | * This macro is used to determine the 'next' bio in the list, given the sector |
| 52 | * of the current stripe+device |
| 53 | */ |
| 54 | #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL) |
| 55 | /* |
| 56 | * The following can be used to debug the driver |
| 57 | */ |
| 58 | #define RAID5_DEBUG 0 |
| 59 | #define RAID5_PARANOIA 1 |
| 60 | #if RAID5_PARANOIA && defined(CONFIG_SMP) |
| 61 | # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock) |
| 62 | #else |
| 63 | # define CHECK_DEVLOCK() |
| 64 | #endif |
| 65 | |
| 66 | #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x))) |
| 67 | #if RAID5_DEBUG |
| 68 | #define inline |
| 69 | #define __inline__ |
| 70 | #endif |
| 71 | |
| 72 | static void print_raid5_conf (raid5_conf_t *conf); |
| 73 | |
| 74 | static inline void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh) |
| 75 | { |
| 76 | if (atomic_dec_and_test(&sh->count)) { |
| 77 | if (!list_empty(&sh->lru)) |
| 78 | BUG(); |
| 79 | if (atomic_read(&conf->active_stripes)==0) |
| 80 | BUG(); |
| 81 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 82 | if (test_bit(STRIPE_DELAYED, &sh->state)) |
| 83 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 84 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
| 85 | conf->seq_write == sh->bm_seq) |
| 86 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 87 | else { |
| 88 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | list_add_tail(&sh->lru, &conf->handle_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | md_wakeup_thread(conf->mddev->thread); |
| 92 | } else { |
| 93 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 94 | atomic_dec(&conf->preread_active_stripes); |
| 95 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 96 | md_wakeup_thread(conf->mddev->thread); |
| 97 | } |
| 98 | list_add_tail(&sh->lru, &conf->inactive_list); |
| 99 | atomic_dec(&conf->active_stripes); |
| 100 | if (!conf->inactive_blocked || |
| 101 | atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4)) |
| 102 | wake_up(&conf->wait_for_stripe); |
| 103 | } |
| 104 | } |
| 105 | } |
| 106 | static void release_stripe(struct stripe_head *sh) |
| 107 | { |
| 108 | raid5_conf_t *conf = sh->raid_conf; |
| 109 | unsigned long flags; |
| 110 | |
| 111 | spin_lock_irqsave(&conf->device_lock, flags); |
| 112 | __release_stripe(conf, sh); |
| 113 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 114 | } |
| 115 | |
| 116 | static void remove_hash(struct stripe_head *sh) |
| 117 | { |
| 118 | PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector); |
| 119 | |
| 120 | if (sh->hash_pprev) { |
| 121 | if (sh->hash_next) |
| 122 | sh->hash_next->hash_pprev = sh->hash_pprev; |
| 123 | *sh->hash_pprev = sh->hash_next; |
| 124 | sh->hash_pprev = NULL; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | static __inline__ void insert_hash(raid5_conf_t *conf, struct stripe_head *sh) |
| 129 | { |
| 130 | struct stripe_head **shp = &stripe_hash(conf, sh->sector); |
| 131 | |
| 132 | PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector); |
| 133 | |
| 134 | CHECK_DEVLOCK(); |
| 135 | if ((sh->hash_next = *shp) != NULL) |
| 136 | (*shp)->hash_pprev = &sh->hash_next; |
| 137 | *shp = sh; |
| 138 | sh->hash_pprev = shp; |
| 139 | } |
| 140 | |
| 141 | |
| 142 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
| 143 | static struct stripe_head *get_free_stripe(raid5_conf_t *conf) |
| 144 | { |
| 145 | struct stripe_head *sh = NULL; |
| 146 | struct list_head *first; |
| 147 | |
| 148 | CHECK_DEVLOCK(); |
| 149 | if (list_empty(&conf->inactive_list)) |
| 150 | goto out; |
| 151 | first = conf->inactive_list.next; |
| 152 | sh = list_entry(first, struct stripe_head, lru); |
| 153 | list_del_init(first); |
| 154 | remove_hash(sh); |
| 155 | atomic_inc(&conf->active_stripes); |
| 156 | out: |
| 157 | return sh; |
| 158 | } |
| 159 | |
| 160 | static void shrink_buffers(struct stripe_head *sh, int num) |
| 161 | { |
| 162 | struct page *p; |
| 163 | int i; |
| 164 | |
| 165 | for (i=0; i<num ; i++) { |
| 166 | p = sh->dev[i].page; |
| 167 | if (!p) |
| 168 | continue; |
| 169 | sh->dev[i].page = NULL; |
| 170 | page_cache_release(p); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | static int grow_buffers(struct stripe_head *sh, int num) |
| 175 | { |
| 176 | int i; |
| 177 | |
| 178 | for (i=0; i<num; i++) { |
| 179 | struct page *page; |
| 180 | |
| 181 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 182 | return 1; |
| 183 | } |
| 184 | sh->dev[i].page = page; |
| 185 | } |
| 186 | return 0; |
| 187 | } |
| 188 | |
| 189 | static void raid5_build_block (struct stripe_head *sh, int i); |
| 190 | |
| 191 | static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx) |
| 192 | { |
| 193 | raid5_conf_t *conf = sh->raid_conf; |
| 194 | int disks = conf->raid_disks, i; |
| 195 | |
| 196 | if (atomic_read(&sh->count) != 0) |
| 197 | BUG(); |
| 198 | if (test_bit(STRIPE_HANDLE, &sh->state)) |
| 199 | BUG(); |
| 200 | |
| 201 | CHECK_DEVLOCK(); |
| 202 | PRINTK("init_stripe called, stripe %llu\n", |
| 203 | (unsigned long long)sh->sector); |
| 204 | |
| 205 | remove_hash(sh); |
| 206 | |
| 207 | sh->sector = sector; |
| 208 | sh->pd_idx = pd_idx; |
| 209 | sh->state = 0; |
| 210 | |
| 211 | for (i=disks; i--; ) { |
| 212 | struct r5dev *dev = &sh->dev[i]; |
| 213 | |
| 214 | if (dev->toread || dev->towrite || dev->written || |
| 215 | test_bit(R5_LOCKED, &dev->flags)) { |
| 216 | printk("sector=%llx i=%d %p %p %p %d\n", |
| 217 | (unsigned long long)sh->sector, i, dev->toread, |
| 218 | dev->towrite, dev->written, |
| 219 | test_bit(R5_LOCKED, &dev->flags)); |
| 220 | BUG(); |
| 221 | } |
| 222 | dev->flags = 0; |
| 223 | raid5_build_block(sh, i); |
| 224 | } |
| 225 | insert_hash(conf, sh); |
| 226 | } |
| 227 | |
| 228 | static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector) |
| 229 | { |
| 230 | struct stripe_head *sh; |
| 231 | |
| 232 | CHECK_DEVLOCK(); |
| 233 | PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector); |
| 234 | for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next) |
| 235 | if (sh->sector == sector) |
| 236 | return sh; |
| 237 | PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector); |
| 238 | return NULL; |
| 239 | } |
| 240 | |
| 241 | static void unplug_slaves(mddev_t *mddev); |
| 242 | static void raid5_unplug_device(request_queue_t *q); |
| 243 | |
| 244 | static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, |
| 245 | int pd_idx, int noblock) |
| 246 | { |
| 247 | struct stripe_head *sh; |
| 248 | |
| 249 | PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector); |
| 250 | |
| 251 | spin_lock_irq(&conf->device_lock); |
| 252 | |
| 253 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 254 | wait_event_lock_irq(conf->wait_for_stripe, |
| 255 | conf->quiesce == 0, |
| 256 | conf->device_lock, /* nothing */); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | sh = __find_stripe(conf, sector); |
| 258 | if (!sh) { |
| 259 | if (!conf->inactive_blocked) |
| 260 | sh = get_free_stripe(conf); |
| 261 | if (noblock && sh == NULL) |
| 262 | break; |
| 263 | if (!sh) { |
| 264 | conf->inactive_blocked = 1; |
| 265 | wait_event_lock_irq(conf->wait_for_stripe, |
| 266 | !list_empty(&conf->inactive_list) && |
| 267 | (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4) |
| 268 | || !conf->inactive_blocked), |
| 269 | conf->device_lock, |
| 270 | unplug_slaves(conf->mddev); |
| 271 | ); |
| 272 | conf->inactive_blocked = 0; |
| 273 | } else |
| 274 | init_stripe(sh, sector, pd_idx); |
| 275 | } else { |
| 276 | if (atomic_read(&sh->count)) { |
| 277 | if (!list_empty(&sh->lru)) |
| 278 | BUG(); |
| 279 | } else { |
| 280 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 281 | atomic_inc(&conf->active_stripes); |
| 282 | if (list_empty(&sh->lru)) |
| 283 | BUG(); |
| 284 | list_del_init(&sh->lru); |
| 285 | } |
| 286 | } |
| 287 | } while (sh == NULL); |
| 288 | |
| 289 | if (sh) |
| 290 | atomic_inc(&sh->count); |
| 291 | |
| 292 | spin_unlock_irq(&conf->device_lock); |
| 293 | return sh; |
| 294 | } |
| 295 | |
| 296 | static int grow_stripes(raid5_conf_t *conf, int num) |
| 297 | { |
| 298 | struct stripe_head *sh; |
| 299 | kmem_cache_t *sc; |
| 300 | int devs = conf->raid_disks; |
| 301 | |
| 302 | sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev)); |
| 303 | |
| 304 | sc = kmem_cache_create(conf->cache_name, |
| 305 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
| 306 | 0, 0, NULL, NULL); |
| 307 | if (!sc) |
| 308 | return 1; |
| 309 | conf->slab_cache = sc; |
| 310 | while (num--) { |
| 311 | sh = kmem_cache_alloc(sc, GFP_KERNEL); |
| 312 | if (!sh) |
| 313 | return 1; |
| 314 | memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev)); |
| 315 | sh->raid_conf = conf; |
| 316 | spin_lock_init(&sh->lock); |
| 317 | |
| 318 | if (grow_buffers(sh, conf->raid_disks)) { |
| 319 | shrink_buffers(sh, conf->raid_disks); |
| 320 | kmem_cache_free(sc, sh); |
| 321 | return 1; |
| 322 | } |
| 323 | /* we just created an active stripe so... */ |
| 324 | atomic_set(&sh->count, 1); |
| 325 | atomic_inc(&conf->active_stripes); |
| 326 | INIT_LIST_HEAD(&sh->lru); |
| 327 | release_stripe(sh); |
| 328 | } |
| 329 | return 0; |
| 330 | } |
| 331 | |
| 332 | static void shrink_stripes(raid5_conf_t *conf) |
| 333 | { |
| 334 | struct stripe_head *sh; |
| 335 | |
| 336 | while (1) { |
| 337 | spin_lock_irq(&conf->device_lock); |
| 338 | sh = get_free_stripe(conf); |
| 339 | spin_unlock_irq(&conf->device_lock); |
| 340 | if (!sh) |
| 341 | break; |
| 342 | if (atomic_read(&sh->count)) |
| 343 | BUG(); |
| 344 | shrink_buffers(sh, conf->raid_disks); |
| 345 | kmem_cache_free(conf->slab_cache, sh); |
| 346 | atomic_dec(&conf->active_stripes); |
| 347 | } |
| 348 | kmem_cache_destroy(conf->slab_cache); |
| 349 | conf->slab_cache = NULL; |
| 350 | } |
| 351 | |
| 352 | static int raid5_end_read_request (struct bio * bi, unsigned int bytes_done, |
| 353 | int error) |
| 354 | { |
| 355 | struct stripe_head *sh = bi->bi_private; |
| 356 | raid5_conf_t *conf = sh->raid_conf; |
| 357 | int disks = conf->raid_disks, i; |
| 358 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 359 | |
| 360 | if (bi->bi_size) |
| 361 | return 1; |
| 362 | |
| 363 | for (i=0 ; i<disks; i++) |
| 364 | if (bi == &sh->dev[i].req) |
| 365 | break; |
| 366 | |
| 367 | PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 368 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 369 | uptodate); |
| 370 | if (i == disks) { |
| 371 | BUG(); |
| 372 | return 0; |
| 373 | } |
| 374 | |
| 375 | if (uptodate) { |
| 376 | #if 0 |
| 377 | struct bio *bio; |
| 378 | unsigned long flags; |
| 379 | spin_lock_irqsave(&conf->device_lock, flags); |
| 380 | /* we can return a buffer if we bypassed the cache or |
| 381 | * if the top buffer is not in highmem. If there are |
| 382 | * multiple buffers, leave the extra work to |
| 383 | * handle_stripe |
| 384 | */ |
| 385 | buffer = sh->bh_read[i]; |
| 386 | if (buffer && |
| 387 | (!PageHighMem(buffer->b_page) |
| 388 | || buffer->b_page == bh->b_page ) |
| 389 | ) { |
| 390 | sh->bh_read[i] = buffer->b_reqnext; |
| 391 | buffer->b_reqnext = NULL; |
| 392 | } else |
| 393 | buffer = NULL; |
| 394 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 395 | if (sh->bh_page[i]==bh->b_page) |
| 396 | set_buffer_uptodate(bh); |
| 397 | if (buffer) { |
| 398 | if (buffer->b_page != bh->b_page) |
| 399 | memcpy(buffer->b_data, bh->b_data, bh->b_size); |
| 400 | buffer->b_end_io(buffer, 1); |
| 401 | } |
| 402 | #else |
| 403 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 404 | #endif |
| 405 | } else { |
| 406 | md_error(conf->mddev, conf->disks[i].rdev); |
| 407 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 408 | } |
| 409 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 410 | #if 0 |
| 411 | /* must restore b_page before unlocking buffer... */ |
| 412 | if (sh->bh_page[i] != bh->b_page) { |
| 413 | bh->b_page = sh->bh_page[i]; |
| 414 | bh->b_data = page_address(bh->b_page); |
| 415 | clear_buffer_uptodate(bh); |
| 416 | } |
| 417 | #endif |
| 418 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 419 | set_bit(STRIPE_HANDLE, &sh->state); |
| 420 | release_stripe(sh); |
| 421 | return 0; |
| 422 | } |
| 423 | |
| 424 | static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done, |
| 425 | int error) |
| 426 | { |
| 427 | struct stripe_head *sh = bi->bi_private; |
| 428 | raid5_conf_t *conf = sh->raid_conf; |
| 429 | int disks = conf->raid_disks, i; |
| 430 | unsigned long flags; |
| 431 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
| 432 | |
| 433 | if (bi->bi_size) |
| 434 | return 1; |
| 435 | |
| 436 | for (i=0 ; i<disks; i++) |
| 437 | if (bi == &sh->dev[i].req) |
| 438 | break; |
| 439 | |
| 440 | PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
| 441 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 442 | uptodate); |
| 443 | if (i == disks) { |
| 444 | BUG(); |
| 445 | return 0; |
| 446 | } |
| 447 | |
| 448 | spin_lock_irqsave(&conf->device_lock, flags); |
| 449 | if (!uptodate) |
| 450 | md_error(conf->mddev, conf->disks[i].rdev); |
| 451 | |
| 452 | rdev_dec_pending(conf->disks[i].rdev, conf->mddev); |
| 453 | |
| 454 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 455 | set_bit(STRIPE_HANDLE, &sh->state); |
| 456 | __release_stripe(conf, sh); |
| 457 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 458 | return 0; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | static sector_t compute_blocknr(struct stripe_head *sh, int i); |
| 463 | |
| 464 | static void raid5_build_block (struct stripe_head *sh, int i) |
| 465 | { |
| 466 | struct r5dev *dev = &sh->dev[i]; |
| 467 | |
| 468 | bio_init(&dev->req); |
| 469 | dev->req.bi_io_vec = &dev->vec; |
| 470 | dev->req.bi_vcnt++; |
| 471 | dev->req.bi_max_vecs++; |
| 472 | dev->vec.bv_page = dev->page; |
| 473 | dev->vec.bv_len = STRIPE_SIZE; |
| 474 | dev->vec.bv_offset = 0; |
| 475 | |
| 476 | dev->req.bi_sector = sh->sector; |
| 477 | dev->req.bi_private = sh; |
| 478 | |
| 479 | dev->flags = 0; |
| 480 | if (i != sh->pd_idx) |
| 481 | dev->sector = compute_blocknr(sh, i); |
| 482 | } |
| 483 | |
| 484 | static void error(mddev_t *mddev, mdk_rdev_t *rdev) |
| 485 | { |
| 486 | char b[BDEVNAME_SIZE]; |
| 487 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 488 | PRINTK("raid5: error called\n"); |
| 489 | |
| 490 | if (!rdev->faulty) { |
| 491 | mddev->sb_dirty = 1; |
| 492 | if (rdev->in_sync) { |
| 493 | conf->working_disks--; |
| 494 | mddev->degraded++; |
| 495 | conf->failed_disks++; |
| 496 | rdev->in_sync = 0; |
| 497 | /* |
| 498 | * if recovery was running, make sure it aborts. |
| 499 | */ |
| 500 | set_bit(MD_RECOVERY_ERR, &mddev->recovery); |
| 501 | } |
| 502 | rdev->faulty = 1; |
| 503 | printk (KERN_ALERT |
| 504 | "raid5: Disk failure on %s, disabling device." |
| 505 | " Operation continuing on %d devices\n", |
| 506 | bdevname(rdev->bdev,b), conf->working_disks); |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Input: a 'big' sector number, |
| 512 | * Output: index of the data and parity disk, and the sector # in them. |
| 513 | */ |
| 514 | static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks, |
| 515 | unsigned int data_disks, unsigned int * dd_idx, |
| 516 | unsigned int * pd_idx, raid5_conf_t *conf) |
| 517 | { |
| 518 | long stripe; |
| 519 | unsigned long chunk_number; |
| 520 | unsigned int chunk_offset; |
| 521 | sector_t new_sector; |
| 522 | int sectors_per_chunk = conf->chunk_size >> 9; |
| 523 | |
| 524 | /* First compute the information on this sector */ |
| 525 | |
| 526 | /* |
| 527 | * Compute the chunk number and the sector offset inside the chunk |
| 528 | */ |
| 529 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 530 | chunk_number = r_sector; |
| 531 | BUG_ON(r_sector != chunk_number); |
| 532 | |
| 533 | /* |
| 534 | * Compute the stripe number |
| 535 | */ |
| 536 | stripe = chunk_number / data_disks; |
| 537 | |
| 538 | /* |
| 539 | * Compute the data disk and parity disk indexes inside the stripe |
| 540 | */ |
| 541 | *dd_idx = chunk_number % data_disks; |
| 542 | |
| 543 | /* |
| 544 | * Select the parity disk based on the user selected algorithm. |
| 545 | */ |
| 546 | if (conf->level == 4) |
| 547 | *pd_idx = data_disks; |
| 548 | else switch (conf->algorithm) { |
| 549 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 550 | *pd_idx = data_disks - stripe % raid_disks; |
| 551 | if (*dd_idx >= *pd_idx) |
| 552 | (*dd_idx)++; |
| 553 | break; |
| 554 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 555 | *pd_idx = stripe % raid_disks; |
| 556 | if (*dd_idx >= *pd_idx) |
| 557 | (*dd_idx)++; |
| 558 | break; |
| 559 | case ALGORITHM_LEFT_SYMMETRIC: |
| 560 | *pd_idx = data_disks - stripe % raid_disks; |
| 561 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; |
| 562 | break; |
| 563 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 564 | *pd_idx = stripe % raid_disks; |
| 565 | *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks; |
| 566 | break; |
| 567 | default: |
| 568 | printk("raid5: unsupported algorithm %d\n", |
| 569 | conf->algorithm); |
| 570 | } |
| 571 | |
| 572 | /* |
| 573 | * Finally, compute the new sector number |
| 574 | */ |
| 575 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 576 | return new_sector; |
| 577 | } |
| 578 | |
| 579 | |
| 580 | static sector_t compute_blocknr(struct stripe_head *sh, int i) |
| 581 | { |
| 582 | raid5_conf_t *conf = sh->raid_conf; |
| 583 | int raid_disks = conf->raid_disks, data_disks = raid_disks - 1; |
| 584 | sector_t new_sector = sh->sector, check; |
| 585 | int sectors_per_chunk = conf->chunk_size >> 9; |
| 586 | sector_t stripe; |
| 587 | int chunk_offset; |
| 588 | int chunk_number, dummy1, dummy2, dd_idx = i; |
| 589 | sector_t r_sector; |
| 590 | |
| 591 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 592 | stripe = new_sector; |
| 593 | BUG_ON(new_sector != stripe); |
| 594 | |
| 595 | |
| 596 | switch (conf->algorithm) { |
| 597 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 598 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 599 | if (i > sh->pd_idx) |
| 600 | i--; |
| 601 | break; |
| 602 | case ALGORITHM_LEFT_SYMMETRIC: |
| 603 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 604 | if (i < sh->pd_idx) |
| 605 | i += raid_disks; |
| 606 | i -= (sh->pd_idx + 1); |
| 607 | break; |
| 608 | default: |
| 609 | printk("raid5: unsupported algorithm %d\n", |
| 610 | conf->algorithm); |
| 611 | } |
| 612 | |
| 613 | chunk_number = stripe * data_disks + i; |
| 614 | r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset; |
| 615 | |
| 616 | check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf); |
| 617 | if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) { |
| 618 | printk("compute_blocknr: map not correct\n"); |
| 619 | return 0; |
| 620 | } |
| 621 | return r_sector; |
| 622 | } |
| 623 | |
| 624 | |
| 625 | |
| 626 | /* |
| 627 | * Copy data between a page in the stripe cache, and a bio. |
| 628 | * There are no alignment or size guarantees between the page or the |
| 629 | * bio except that there is some overlap. |
| 630 | * All iovecs in the bio must be considered. |
| 631 | */ |
| 632 | static void copy_data(int frombio, struct bio *bio, |
| 633 | struct page *page, |
| 634 | sector_t sector) |
| 635 | { |
| 636 | char *pa = page_address(page); |
| 637 | struct bio_vec *bvl; |
| 638 | int i; |
| 639 | int page_offset; |
| 640 | |
| 641 | if (bio->bi_sector >= sector) |
| 642 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 643 | else |
| 644 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
| 645 | bio_for_each_segment(bvl, bio, i) { |
| 646 | int len = bio_iovec_idx(bio,i)->bv_len; |
| 647 | int clen; |
| 648 | int b_offset = 0; |
| 649 | |
| 650 | if (page_offset < 0) { |
| 651 | b_offset = -page_offset; |
| 652 | page_offset += b_offset; |
| 653 | len -= b_offset; |
| 654 | } |
| 655 | |
| 656 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 657 | clen = STRIPE_SIZE - page_offset; |
| 658 | else clen = len; |
| 659 | |
| 660 | if (clen > 0) { |
| 661 | char *ba = __bio_kmap_atomic(bio, i, KM_USER0); |
| 662 | if (frombio) |
| 663 | memcpy(pa+page_offset, ba+b_offset, clen); |
| 664 | else |
| 665 | memcpy(ba+b_offset, pa+page_offset, clen); |
| 666 | __bio_kunmap_atomic(ba, KM_USER0); |
| 667 | } |
| 668 | if (clen < len) /* hit end of page */ |
| 669 | break; |
| 670 | page_offset += len; |
| 671 | } |
| 672 | } |
| 673 | |
| 674 | #define check_xor() do { \ |
| 675 | if (count == MAX_XOR_BLOCKS) { \ |
| 676 | xor_block(count, STRIPE_SIZE, ptr); \ |
| 677 | count = 1; \ |
| 678 | } \ |
| 679 | } while(0) |
| 680 | |
| 681 | |
| 682 | static void compute_block(struct stripe_head *sh, int dd_idx) |
| 683 | { |
| 684 | raid5_conf_t *conf = sh->raid_conf; |
| 685 | int i, count, disks = conf->raid_disks; |
| 686 | void *ptr[MAX_XOR_BLOCKS], *p; |
| 687 | |
| 688 | PRINTK("compute_block, stripe %llu, idx %d\n", |
| 689 | (unsigned long long)sh->sector, dd_idx); |
| 690 | |
| 691 | ptr[0] = page_address(sh->dev[dd_idx].page); |
| 692 | memset(ptr[0], 0, STRIPE_SIZE); |
| 693 | count = 1; |
| 694 | for (i = disks ; i--; ) { |
| 695 | if (i == dd_idx) |
| 696 | continue; |
| 697 | p = page_address(sh->dev[i].page); |
| 698 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags)) |
| 699 | ptr[count++] = p; |
| 700 | else |
| 701 | printk("compute_block() %d, stripe %llu, %d" |
| 702 | " not present\n", dd_idx, |
| 703 | (unsigned long long)sh->sector, i); |
| 704 | |
| 705 | check_xor(); |
| 706 | } |
| 707 | if (count != 1) |
| 708 | xor_block(count, STRIPE_SIZE, ptr); |
| 709 | set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags); |
| 710 | } |
| 711 | |
| 712 | static void compute_parity(struct stripe_head *sh, int method) |
| 713 | { |
| 714 | raid5_conf_t *conf = sh->raid_conf; |
| 715 | int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count; |
| 716 | void *ptr[MAX_XOR_BLOCKS]; |
| 717 | struct bio *chosen; |
| 718 | |
| 719 | PRINTK("compute_parity, stripe %llu, method %d\n", |
| 720 | (unsigned long long)sh->sector, method); |
| 721 | |
| 722 | count = 1; |
| 723 | ptr[0] = page_address(sh->dev[pd_idx].page); |
| 724 | switch(method) { |
| 725 | case READ_MODIFY_WRITE: |
| 726 | if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags)) |
| 727 | BUG(); |
| 728 | for (i=disks ; i-- ;) { |
| 729 | if (i==pd_idx) |
| 730 | continue; |
| 731 | if (sh->dev[i].towrite && |
| 732 | test_bit(R5_UPTODATE, &sh->dev[i].flags)) { |
| 733 | ptr[count++] = page_address(sh->dev[i].page); |
| 734 | chosen = sh->dev[i].towrite; |
| 735 | sh->dev[i].towrite = NULL; |
| 736 | |
| 737 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 738 | wake_up(&conf->wait_for_overlap); |
| 739 | |
| 740 | if (sh->dev[i].written) BUG(); |
| 741 | sh->dev[i].written = chosen; |
| 742 | check_xor(); |
| 743 | } |
| 744 | } |
| 745 | break; |
| 746 | case RECONSTRUCT_WRITE: |
| 747 | memset(ptr[0], 0, STRIPE_SIZE); |
| 748 | for (i= disks; i-- ;) |
| 749 | if (i!=pd_idx && sh->dev[i].towrite) { |
| 750 | chosen = sh->dev[i].towrite; |
| 751 | sh->dev[i].towrite = NULL; |
| 752 | |
| 753 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 754 | wake_up(&conf->wait_for_overlap); |
| 755 | |
| 756 | if (sh->dev[i].written) BUG(); |
| 757 | sh->dev[i].written = chosen; |
| 758 | } |
| 759 | break; |
| 760 | case CHECK_PARITY: |
| 761 | break; |
| 762 | } |
| 763 | if (count>1) { |
| 764 | xor_block(count, STRIPE_SIZE, ptr); |
| 765 | count = 1; |
| 766 | } |
| 767 | |
| 768 | for (i = disks; i--;) |
| 769 | if (sh->dev[i].written) { |
| 770 | sector_t sector = sh->dev[i].sector; |
| 771 | struct bio *wbi = sh->dev[i].written; |
| 772 | while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) { |
| 773 | copy_data(1, wbi, sh->dev[i].page, sector); |
| 774 | wbi = r5_next_bio(wbi, sector); |
| 775 | } |
| 776 | |
| 777 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 778 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
| 779 | } |
| 780 | |
| 781 | switch(method) { |
| 782 | case RECONSTRUCT_WRITE: |
| 783 | case CHECK_PARITY: |
| 784 | for (i=disks; i--;) |
| 785 | if (i != pd_idx) { |
| 786 | ptr[count++] = page_address(sh->dev[i].page); |
| 787 | check_xor(); |
| 788 | } |
| 789 | break; |
| 790 | case READ_MODIFY_WRITE: |
| 791 | for (i = disks; i--;) |
| 792 | if (sh->dev[i].written) { |
| 793 | ptr[count++] = page_address(sh->dev[i].page); |
| 794 | check_xor(); |
| 795 | } |
| 796 | } |
| 797 | if (count != 1) |
| 798 | xor_block(count, STRIPE_SIZE, ptr); |
| 799 | |
| 800 | if (method != CHECK_PARITY) { |
| 801 | set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 802 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 803 | } else |
| 804 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 805 | } |
| 806 | |
| 807 | /* |
| 808 | * Each stripe/dev can have one or more bion attached. |
| 809 | * toread/towrite point to the first in a chain. |
| 810 | * The bi_next chain must be in order. |
| 811 | */ |
| 812 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 813 | { |
| 814 | struct bio **bip; |
| 815 | raid5_conf_t *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 816 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | |
| 818 | PRINTK("adding bh b#%llu to stripe s#%llu\n", |
| 819 | (unsigned long long)bi->bi_sector, |
| 820 | (unsigned long long)sh->sector); |
| 821 | |
| 822 | |
| 823 | spin_lock(&sh->lock); |
| 824 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 825 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 826 | bip = &sh->dev[dd_idx].towrite; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 827 | if (*bip == NULL && sh->dev[dd_idx].written == NULL) |
| 828 | firstwrite = 1; |
| 829 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 830 | bip = &sh->dev[dd_idx].toread; |
| 831 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 832 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 833 | goto overlap; |
| 834 | bip = & (*bip)->bi_next; |
| 835 | } |
| 836 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 837 | goto overlap; |
| 838 | |
| 839 | if (*bip && bi->bi_next && (*bip) != bi->bi_next) |
| 840 | BUG(); |
| 841 | if (*bip) |
| 842 | bi->bi_next = *bip; |
| 843 | *bip = bi; |
| 844 | bi->bi_phys_segments ++; |
| 845 | spin_unlock_irq(&conf->device_lock); |
| 846 | spin_unlock(&sh->lock); |
| 847 | |
| 848 | PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 849 | (unsigned long long)bi->bi_sector, |
| 850 | (unsigned long long)sh->sector, dd_idx); |
| 851 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 852 | if (conf->mddev->bitmap && firstwrite) { |
| 853 | sh->bm_seq = conf->seq_write; |
| 854 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 855 | STRIPE_SECTORS, 0); |
| 856 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 857 | } |
| 858 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 859 | if (forwrite) { |
| 860 | /* check if page is covered */ |
| 861 | sector_t sector = sh->dev[dd_idx].sector; |
| 862 | for (bi=sh->dev[dd_idx].towrite; |
| 863 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 864 | bi && bi->bi_sector <= sector; |
| 865 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 866 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 867 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 868 | } |
| 869 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 870 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 871 | } |
| 872 | return 1; |
| 873 | |
| 874 | overlap: |
| 875 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
| 876 | spin_unlock_irq(&conf->device_lock); |
| 877 | spin_unlock(&sh->lock); |
| 878 | return 0; |
| 879 | } |
| 880 | |
| 881 | |
| 882 | /* |
| 883 | * handle_stripe - do things to a stripe. |
| 884 | * |
| 885 | * We lock the stripe and then examine the state of various bits |
| 886 | * to see what needs to be done. |
| 887 | * Possible results: |
| 888 | * return some read request which now have data |
| 889 | * return some write requests which are safely on disc |
| 890 | * schedule a read on some buffers |
| 891 | * schedule a write of some buffers |
| 892 | * return confirmation of parity correctness |
| 893 | * |
| 894 | * Parity calculations are done inside the stripe lock |
| 895 | * buffers are taken off read_list or write_list, and bh_cache buffers |
| 896 | * get BH_Lock set before the stripe lock is released. |
| 897 | * |
| 898 | */ |
| 899 | |
| 900 | static void handle_stripe(struct stripe_head *sh) |
| 901 | { |
| 902 | raid5_conf_t *conf = sh->raid_conf; |
| 903 | int disks = conf->raid_disks; |
| 904 | struct bio *return_bi= NULL; |
| 905 | struct bio *bi; |
| 906 | int i; |
| 907 | int syncing; |
| 908 | int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0; |
| 909 | int non_overwrite = 0; |
| 910 | int failed_num=0; |
| 911 | struct r5dev *dev; |
| 912 | |
| 913 | PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n", |
| 914 | (unsigned long long)sh->sector, atomic_read(&sh->count), |
| 915 | sh->pd_idx); |
| 916 | |
| 917 | spin_lock(&sh->lock); |
| 918 | clear_bit(STRIPE_HANDLE, &sh->state); |
| 919 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 920 | |
| 921 | syncing = test_bit(STRIPE_SYNCING, &sh->state); |
| 922 | /* Now to look around and see what can be done */ |
| 923 | |
| 924 | for (i=disks; i--; ) { |
| 925 | mdk_rdev_t *rdev; |
| 926 | dev = &sh->dev[i]; |
| 927 | clear_bit(R5_Insync, &dev->flags); |
| 928 | clear_bit(R5_Syncio, &dev->flags); |
| 929 | |
| 930 | PRINTK("check %d: state 0x%lx read %p write %p written %p\n", |
| 931 | i, dev->flags, dev->toread, dev->towrite, dev->written); |
| 932 | /* maybe we can reply to a read */ |
| 933 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) { |
| 934 | struct bio *rbi, *rbi2; |
| 935 | PRINTK("Return read for disc %d\n", i); |
| 936 | spin_lock_irq(&conf->device_lock); |
| 937 | rbi = dev->toread; |
| 938 | dev->toread = NULL; |
| 939 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 940 | wake_up(&conf->wait_for_overlap); |
| 941 | spin_unlock_irq(&conf->device_lock); |
| 942 | while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 943 | copy_data(0, rbi, dev->page, dev->sector); |
| 944 | rbi2 = r5_next_bio(rbi, dev->sector); |
| 945 | spin_lock_irq(&conf->device_lock); |
| 946 | if (--rbi->bi_phys_segments == 0) { |
| 947 | rbi->bi_next = return_bi; |
| 948 | return_bi = rbi; |
| 949 | } |
| 950 | spin_unlock_irq(&conf->device_lock); |
| 951 | rbi = rbi2; |
| 952 | } |
| 953 | } |
| 954 | |
| 955 | /* now count some things */ |
| 956 | if (test_bit(R5_LOCKED, &dev->flags)) locked++; |
| 957 | if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++; |
| 958 | |
| 959 | |
| 960 | if (dev->toread) to_read++; |
| 961 | if (dev->towrite) { |
| 962 | to_write++; |
| 963 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
| 964 | non_overwrite++; |
| 965 | } |
| 966 | if (dev->written) written++; |
| 967 | rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */ |
| 968 | if (!rdev || !rdev->in_sync) { |
| 969 | failed++; |
| 970 | failed_num = i; |
| 971 | } else |
| 972 | set_bit(R5_Insync, &dev->flags); |
| 973 | } |
| 974 | PRINTK("locked=%d uptodate=%d to_read=%d" |
| 975 | " to_write=%d failed=%d failed_num=%d\n", |
| 976 | locked, uptodate, to_read, to_write, failed, failed_num); |
| 977 | /* check if the array has lost two devices and, if so, some requests might |
| 978 | * need to be failed |
| 979 | */ |
| 980 | if (failed > 1 && to_read+to_write+written) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 981 | for (i=disks; i--; ) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 982 | int bitmap_end = 0; |
| 983 | spin_lock_irq(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 984 | /* fail all writes first */ |
| 985 | bi = sh->dev[i].towrite; |
| 986 | sh->dev[i].towrite = NULL; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 987 | if (bi) { to_write--; bitmap_end = 1; } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 988 | |
| 989 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 990 | wake_up(&conf->wait_for_overlap); |
| 991 | |
| 992 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ |
| 993 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 994 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 995 | if (--bi->bi_phys_segments == 0) { |
| 996 | md_write_end(conf->mddev); |
| 997 | bi->bi_next = return_bi; |
| 998 | return_bi = bi; |
| 999 | } |
| 1000 | bi = nextbi; |
| 1001 | } |
| 1002 | /* and fail all 'written' */ |
| 1003 | bi = sh->dev[i].written; |
| 1004 | sh->dev[i].written = NULL; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1005 | if (bi) bitmap_end = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1006 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) { |
| 1007 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 1008 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1009 | if (--bi->bi_phys_segments == 0) { |
| 1010 | md_write_end(conf->mddev); |
| 1011 | bi->bi_next = return_bi; |
| 1012 | return_bi = bi; |
| 1013 | } |
| 1014 | bi = bi2; |
| 1015 | } |
| 1016 | |
| 1017 | /* fail any reads if this device is non-operational */ |
| 1018 | if (!test_bit(R5_Insync, &sh->dev[i].flags)) { |
| 1019 | bi = sh->dev[i].toread; |
| 1020 | sh->dev[i].toread = NULL; |
| 1021 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 1022 | wake_up(&conf->wait_for_overlap); |
| 1023 | if (bi) to_read--; |
| 1024 | while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){ |
| 1025 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 1026 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1027 | if (--bi->bi_phys_segments == 0) { |
| 1028 | bi->bi_next = return_bi; |
| 1029 | return_bi = bi; |
| 1030 | } |
| 1031 | bi = nextbi; |
| 1032 | } |
| 1033 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1034 | spin_unlock_irq(&conf->device_lock); |
| 1035 | if (bitmap_end) |
| 1036 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 1037 | STRIPE_SECTORS, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1038 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1039 | } |
| 1040 | if (failed > 1 && syncing) { |
| 1041 | md_done_sync(conf->mddev, STRIPE_SECTORS,0); |
| 1042 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 1043 | syncing = 0; |
| 1044 | } |
| 1045 | |
| 1046 | /* might be able to return some write requests if the parity block |
| 1047 | * is safe, or on a failed drive |
| 1048 | */ |
| 1049 | dev = &sh->dev[sh->pd_idx]; |
| 1050 | if ( written && |
| 1051 | ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) && |
| 1052 | test_bit(R5_UPTODATE, &dev->flags)) |
| 1053 | || (failed == 1 && failed_num == sh->pd_idx)) |
| 1054 | ) { |
| 1055 | /* any written block on an uptodate or failed drive can be returned. |
| 1056 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 1057 | * never LOCKED, so we don't need to test 'failed' directly. |
| 1058 | */ |
| 1059 | for (i=disks; i--; ) |
| 1060 | if (sh->dev[i].written) { |
| 1061 | dev = &sh->dev[i]; |
| 1062 | if (!test_bit(R5_LOCKED, &dev->flags) && |
| 1063 | test_bit(R5_UPTODATE, &dev->flags) ) { |
| 1064 | /* We can return any write requests */ |
| 1065 | struct bio *wbi, *wbi2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1066 | int bitmap_end = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1067 | PRINTK("Return write for disc %d\n", i); |
| 1068 | spin_lock_irq(&conf->device_lock); |
| 1069 | wbi = dev->written; |
| 1070 | dev->written = NULL; |
| 1071 | while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) { |
| 1072 | wbi2 = r5_next_bio(wbi, dev->sector); |
| 1073 | if (--wbi->bi_phys_segments == 0) { |
| 1074 | md_write_end(conf->mddev); |
| 1075 | wbi->bi_next = return_bi; |
| 1076 | return_bi = wbi; |
| 1077 | } |
| 1078 | wbi = wbi2; |
| 1079 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1080 | if (dev->towrite == NULL) |
| 1081 | bitmap_end = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1082 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1083 | if (bitmap_end) |
| 1084 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 1085 | STRIPE_SECTORS, |
| 1086 | !test_bit(STRIPE_DEGRADED, &sh->state), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | /* Now we might consider reading some blocks, either to check/generate |
| 1092 | * parity, or to satisfy requests |
| 1093 | * or to load a block that is being partially written. |
| 1094 | */ |
| 1095 | if (to_read || non_overwrite || (syncing && (uptodate < disks))) { |
| 1096 | for (i=disks; i--;) { |
| 1097 | dev = &sh->dev[i]; |
| 1098 | if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && |
| 1099 | (dev->toread || |
| 1100 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 1101 | syncing || |
| 1102 | (failed && (sh->dev[failed_num].toread || |
| 1103 | (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags)))) |
| 1104 | ) |
| 1105 | ) { |
| 1106 | /* we would like to get this block, possibly |
| 1107 | * by computing it, but we might not be able to |
| 1108 | */ |
| 1109 | if (uptodate == disks-1) { |
| 1110 | PRINTK("Computing block %d\n", i); |
| 1111 | compute_block(sh, i); |
| 1112 | uptodate++; |
| 1113 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 1114 | set_bit(R5_LOCKED, &dev->flags); |
| 1115 | set_bit(R5_Wantread, &dev->flags); |
| 1116 | #if 0 |
| 1117 | /* if I am just reading this block and we don't have |
| 1118 | a failed drive, or any pending writes then sidestep the cache */ |
| 1119 | if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext && |
| 1120 | ! syncing && !failed && !to_write) { |
| 1121 | sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page; |
| 1122 | sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data; |
| 1123 | } |
| 1124 | #endif |
| 1125 | locked++; |
| 1126 | PRINTK("Reading block %d (sync=%d)\n", |
| 1127 | i, syncing); |
| 1128 | if (syncing) |
| 1129 | md_sync_acct(conf->disks[i].rdev->bdev, |
| 1130 | STRIPE_SECTORS); |
| 1131 | } |
| 1132 | } |
| 1133 | } |
| 1134 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1135 | } |
| 1136 | |
| 1137 | /* now to consider writing and what else, if anything should be read */ |
| 1138 | if (to_write) { |
| 1139 | int rmw=0, rcw=0; |
| 1140 | for (i=disks ; i--;) { |
| 1141 | /* would I have to read this buffer for read_modify_write */ |
| 1142 | dev = &sh->dev[i]; |
| 1143 | if ((dev->towrite || i == sh->pd_idx) && |
| 1144 | (!test_bit(R5_LOCKED, &dev->flags) |
| 1145 | #if 0 |
| 1146 | || sh->bh_page[i]!=bh->b_page |
| 1147 | #endif |
| 1148 | ) && |
| 1149 | !test_bit(R5_UPTODATE, &dev->flags)) { |
| 1150 | if (test_bit(R5_Insync, &dev->flags) |
| 1151 | /* && !(!mddev->insync && i == sh->pd_idx) */ |
| 1152 | ) |
| 1153 | rmw++; |
| 1154 | else rmw += 2*disks; /* cannot read it */ |
| 1155 | } |
| 1156 | /* Would I have to read this buffer for reconstruct_write */ |
| 1157 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 1158 | (!test_bit(R5_LOCKED, &dev->flags) |
| 1159 | #if 0 |
| 1160 | || sh->bh_page[i] != bh->b_page |
| 1161 | #endif |
| 1162 | ) && |
| 1163 | !test_bit(R5_UPTODATE, &dev->flags)) { |
| 1164 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
| 1165 | else rcw += 2*disks; |
| 1166 | } |
| 1167 | } |
| 1168 | PRINTK("for sector %llu, rmw=%d rcw=%d\n", |
| 1169 | (unsigned long long)sh->sector, rmw, rcw); |
| 1170 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1171 | if (rmw < rcw && rmw > 0) |
| 1172 | /* prefer read-modify-write, but need to get some data */ |
| 1173 | for (i=disks; i--;) { |
| 1174 | dev = &sh->dev[i]; |
| 1175 | if ((dev->towrite || i == sh->pd_idx) && |
| 1176 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && |
| 1177 | test_bit(R5_Insync, &dev->flags)) { |
| 1178 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 1179 | { |
| 1180 | PRINTK("Read_old block %d for r-m-w\n", i); |
| 1181 | set_bit(R5_LOCKED, &dev->flags); |
| 1182 | set_bit(R5_Wantread, &dev->flags); |
| 1183 | locked++; |
| 1184 | } else { |
| 1185 | set_bit(STRIPE_DELAYED, &sh->state); |
| 1186 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | if (rcw <= rmw && rcw > 0) |
| 1191 | /* want reconstruct write, but need to get some data */ |
| 1192 | for (i=disks; i--;) { |
| 1193 | dev = &sh->dev[i]; |
| 1194 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 1195 | !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) && |
| 1196 | test_bit(R5_Insync, &dev->flags)) { |
| 1197 | if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 1198 | { |
| 1199 | PRINTK("Read_old block %d for Reconstruct\n", i); |
| 1200 | set_bit(R5_LOCKED, &dev->flags); |
| 1201 | set_bit(R5_Wantread, &dev->flags); |
| 1202 | locked++; |
| 1203 | } else { |
| 1204 | set_bit(STRIPE_DELAYED, &sh->state); |
| 1205 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1206 | } |
| 1207 | } |
| 1208 | } |
| 1209 | /* now if nothing is locked, and if we have enough data, we can start a write request */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1210 | if (locked == 0 && (rcw == 0 ||rmw == 0) && |
| 1211 | !test_bit(STRIPE_BIT_DELAY, &sh->state)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1212 | PRINTK("Computing parity...\n"); |
| 1213 | compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE); |
| 1214 | /* now every locked buffer is ready to be written */ |
| 1215 | for (i=disks; i--;) |
| 1216 | if (test_bit(R5_LOCKED, &sh->dev[i].flags)) { |
| 1217 | PRINTK("Writing block %d\n", i); |
| 1218 | locked++; |
| 1219 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 1220 | if (!test_bit(R5_Insync, &sh->dev[i].flags) |
| 1221 | || (i==sh->pd_idx && failed == 0)) |
| 1222 | set_bit(STRIPE_INSYNC, &sh->state); |
| 1223 | } |
| 1224 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
| 1225 | atomic_dec(&conf->preread_active_stripes); |
| 1226 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) |
| 1227 | md_wakeup_thread(conf->mddev->thread); |
| 1228 | } |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | /* maybe we need to check and possibly fix the parity for this stripe |
| 1233 | * Any reads will already have been scheduled, so we just see if enough data |
| 1234 | * is available |
| 1235 | */ |
| 1236 | if (syncing && locked == 0 && |
| 1237 | !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) { |
| 1238 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1239 | if (failed == 0) { |
| 1240 | char *pagea; |
| 1241 | if (uptodate != disks) |
| 1242 | BUG(); |
| 1243 | compute_parity(sh, CHECK_PARITY); |
| 1244 | uptodate--; |
| 1245 | pagea = page_address(sh->dev[sh->pd_idx].page); |
| 1246 | if ((*(u32*)pagea) == 0 && |
| 1247 | !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) { |
| 1248 | /* parity is correct (on disc, not in buffer any more) */ |
| 1249 | set_bit(STRIPE_INSYNC, &sh->state); |
| 1250 | } |
| 1251 | } |
| 1252 | if (!test_bit(STRIPE_INSYNC, &sh->state)) { |
| 1253 | if (failed==0) |
| 1254 | failed_num = sh->pd_idx; |
| 1255 | /* should be able to compute the missing block and write it to spare */ |
| 1256 | if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) { |
| 1257 | if (uptodate+1 != disks) |
| 1258 | BUG(); |
| 1259 | compute_block(sh, failed_num); |
| 1260 | uptodate++; |
| 1261 | } |
| 1262 | if (uptodate != disks) |
| 1263 | BUG(); |
| 1264 | dev = &sh->dev[failed_num]; |
| 1265 | set_bit(R5_LOCKED, &dev->flags); |
| 1266 | set_bit(R5_Wantwrite, &dev->flags); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1267 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1268 | locked++; |
| 1269 | set_bit(STRIPE_INSYNC, &sh->state); |
| 1270 | set_bit(R5_Syncio, &dev->flags); |
| 1271 | } |
| 1272 | } |
| 1273 | if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) { |
| 1274 | md_done_sync(conf->mddev, STRIPE_SECTORS,1); |
| 1275 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 1276 | } |
| 1277 | |
| 1278 | spin_unlock(&sh->lock); |
| 1279 | |
| 1280 | while ((bi=return_bi)) { |
| 1281 | int bytes = bi->bi_size; |
| 1282 | |
| 1283 | return_bi = bi->bi_next; |
| 1284 | bi->bi_next = NULL; |
| 1285 | bi->bi_size = 0; |
| 1286 | bi->bi_end_io(bi, bytes, 0); |
| 1287 | } |
| 1288 | for (i=disks; i-- ;) { |
| 1289 | int rw; |
| 1290 | struct bio *bi; |
| 1291 | mdk_rdev_t *rdev; |
| 1292 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) |
| 1293 | rw = 1; |
| 1294 | else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
| 1295 | rw = 0; |
| 1296 | else |
| 1297 | continue; |
| 1298 | |
| 1299 | bi = &sh->dev[i].req; |
| 1300 | |
| 1301 | bi->bi_rw = rw; |
| 1302 | if (rw) |
| 1303 | bi->bi_end_io = raid5_end_write_request; |
| 1304 | else |
| 1305 | bi->bi_end_io = raid5_end_read_request; |
| 1306 | |
| 1307 | rcu_read_lock(); |
| 1308 | rdev = conf->disks[i].rdev; |
| 1309 | if (rdev && rdev->faulty) |
| 1310 | rdev = NULL; |
| 1311 | if (rdev) |
| 1312 | atomic_inc(&rdev->nr_pending); |
| 1313 | rcu_read_unlock(); |
| 1314 | |
| 1315 | if (rdev) { |
| 1316 | if (test_bit(R5_Syncio, &sh->dev[i].flags)) |
| 1317 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 1318 | |
| 1319 | bi->bi_bdev = rdev->bdev; |
| 1320 | PRINTK("for %llu schedule op %ld on disc %d\n", |
| 1321 | (unsigned long long)sh->sector, bi->bi_rw, i); |
| 1322 | atomic_inc(&sh->count); |
| 1323 | bi->bi_sector = sh->sector + rdev->data_offset; |
| 1324 | bi->bi_flags = 1 << BIO_UPTODATE; |
| 1325 | bi->bi_vcnt = 1; |
| 1326 | bi->bi_max_vecs = 1; |
| 1327 | bi->bi_idx = 0; |
| 1328 | bi->bi_io_vec = &sh->dev[i].vec; |
| 1329 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 1330 | bi->bi_io_vec[0].bv_offset = 0; |
| 1331 | bi->bi_size = STRIPE_SIZE; |
| 1332 | bi->bi_next = NULL; |
| 1333 | generic_make_request(bi); |
| 1334 | } else { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1335 | if (rw == 1) |
| 1336 | set_bit(STRIPE_DEGRADED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1337 | PRINTK("skip op %ld on disc %d for sector %llu\n", |
| 1338 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 1339 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1340 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1341 | } |
| 1342 | } |
| 1343 | } |
| 1344 | |
| 1345 | static inline void raid5_activate_delayed(raid5_conf_t *conf) |
| 1346 | { |
| 1347 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 1348 | while (!list_empty(&conf->delayed_list)) { |
| 1349 | struct list_head *l = conf->delayed_list.next; |
| 1350 | struct stripe_head *sh; |
| 1351 | sh = list_entry(l, struct stripe_head, lru); |
| 1352 | list_del_init(l); |
| 1353 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 1354 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 1355 | atomic_inc(&conf->preread_active_stripes); |
| 1356 | list_add_tail(&sh->lru, &conf->handle_list); |
| 1357 | } |
| 1358 | } |
| 1359 | } |
| 1360 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1361 | static inline void activate_bit_delay(raid5_conf_t *conf) |
| 1362 | { |
| 1363 | /* device_lock is held */ |
| 1364 | struct list_head head; |
| 1365 | list_add(&head, &conf->bitmap_list); |
| 1366 | list_del_init(&conf->bitmap_list); |
| 1367 | while (!list_empty(&head)) { |
| 1368 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 1369 | list_del_init(&sh->lru); |
| 1370 | atomic_inc(&sh->count); |
| 1371 | __release_stripe(conf, sh); |
| 1372 | } |
| 1373 | } |
| 1374 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1375 | static void unplug_slaves(mddev_t *mddev) |
| 1376 | { |
| 1377 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 1378 | int i; |
| 1379 | |
| 1380 | rcu_read_lock(); |
| 1381 | for (i=0; i<mddev->raid_disks; i++) { |
| 1382 | mdk_rdev_t *rdev = conf->disks[i].rdev; |
| 1383 | if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) { |
| 1384 | request_queue_t *r_queue = bdev_get_queue(rdev->bdev); |
| 1385 | |
| 1386 | atomic_inc(&rdev->nr_pending); |
| 1387 | rcu_read_unlock(); |
| 1388 | |
| 1389 | if (r_queue->unplug_fn) |
| 1390 | r_queue->unplug_fn(r_queue); |
| 1391 | |
| 1392 | rdev_dec_pending(rdev, mddev); |
| 1393 | rcu_read_lock(); |
| 1394 | } |
| 1395 | } |
| 1396 | rcu_read_unlock(); |
| 1397 | } |
| 1398 | |
| 1399 | static void raid5_unplug_device(request_queue_t *q) |
| 1400 | { |
| 1401 | mddev_t *mddev = q->queuedata; |
| 1402 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 1403 | unsigned long flags; |
| 1404 | |
| 1405 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1406 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1407 | if (blk_remove_plug(q)) { |
| 1408 | conf->seq_flush++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1409 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1411 | md_wakeup_thread(mddev->thread); |
| 1412 | |
| 1413 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1414 | |
| 1415 | unplug_slaves(mddev); |
| 1416 | } |
| 1417 | |
| 1418 | static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk, |
| 1419 | sector_t *error_sector) |
| 1420 | { |
| 1421 | mddev_t *mddev = q->queuedata; |
| 1422 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 1423 | int i, ret = 0; |
| 1424 | |
| 1425 | rcu_read_lock(); |
| 1426 | for (i=0; i<mddev->raid_disks && ret == 0; i++) { |
| 1427 | mdk_rdev_t *rdev = conf->disks[i].rdev; |
| 1428 | if (rdev && !rdev->faulty) { |
| 1429 | struct block_device *bdev = rdev->bdev; |
| 1430 | request_queue_t *r_queue = bdev_get_queue(bdev); |
| 1431 | |
| 1432 | if (!r_queue->issue_flush_fn) |
| 1433 | ret = -EOPNOTSUPP; |
| 1434 | else { |
| 1435 | atomic_inc(&rdev->nr_pending); |
| 1436 | rcu_read_unlock(); |
| 1437 | ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk, |
| 1438 | error_sector); |
| 1439 | rdev_dec_pending(rdev, mddev); |
| 1440 | rcu_read_lock(); |
| 1441 | } |
| 1442 | } |
| 1443 | } |
| 1444 | rcu_read_unlock(); |
| 1445 | return ret; |
| 1446 | } |
| 1447 | |
| 1448 | static inline void raid5_plug_device(raid5_conf_t *conf) |
| 1449 | { |
| 1450 | spin_lock_irq(&conf->device_lock); |
| 1451 | blk_plug_device(conf->mddev->queue); |
| 1452 | spin_unlock_irq(&conf->device_lock); |
| 1453 | } |
| 1454 | |
| 1455 | static int make_request (request_queue_t *q, struct bio * bi) |
| 1456 | { |
| 1457 | mddev_t *mddev = q->queuedata; |
| 1458 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 1459 | const unsigned int raid_disks = conf->raid_disks; |
| 1460 | const unsigned int data_disks = raid_disks - 1; |
| 1461 | unsigned int dd_idx, pd_idx; |
| 1462 | sector_t new_sector; |
| 1463 | sector_t logical_sector, last_sector; |
| 1464 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 1465 | const int rw = bio_data_dir(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1466 | |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 1467 | if (unlikely(bio_barrier(bi))) { |
| 1468 | bio_endio(bi, bi->bi_size, -EOPNOTSUPP); |
| 1469 | return 0; |
| 1470 | } |
| 1471 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 1472 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1473 | |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 1474 | disk_stat_inc(mddev->gendisk, ios[rw]); |
| 1475 | disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1476 | |
| 1477 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 1478 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 1479 | bi->bi_next = NULL; |
| 1480 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 1481 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1482 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 1483 | DEFINE_WAIT(w); |
| 1484 | |
| 1485 | new_sector = raid5_compute_sector(logical_sector, |
| 1486 | raid_disks, data_disks, &dd_idx, &pd_idx, conf); |
| 1487 | |
| 1488 | PRINTK("raid5: make_request, sector %llu logical %llu\n", |
| 1489 | (unsigned long long)new_sector, |
| 1490 | (unsigned long long)logical_sector); |
| 1491 | |
| 1492 | retry: |
| 1493 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
| 1494 | sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK)); |
| 1495 | if (sh) { |
| 1496 | if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) { |
| 1497 | /* Add failed due to overlap. Flush everything |
| 1498 | * and wait a while |
| 1499 | */ |
| 1500 | raid5_unplug_device(mddev->queue); |
| 1501 | release_stripe(sh); |
| 1502 | schedule(); |
| 1503 | goto retry; |
| 1504 | } |
| 1505 | finish_wait(&conf->wait_for_overlap, &w); |
| 1506 | raid5_plug_device(conf); |
| 1507 | handle_stripe(sh); |
| 1508 | release_stripe(sh); |
| 1509 | |
| 1510 | } else { |
| 1511 | /* cannot get stripe for read-ahead, just give-up */ |
| 1512 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 1513 | finish_wait(&conf->wait_for_overlap, &w); |
| 1514 | break; |
| 1515 | } |
| 1516 | |
| 1517 | } |
| 1518 | spin_lock_irq(&conf->device_lock); |
| 1519 | if (--bi->bi_phys_segments == 0) { |
| 1520 | int bytes = bi->bi_size; |
| 1521 | |
| 1522 | if ( bio_data_dir(bi) == WRITE ) |
| 1523 | md_write_end(mddev); |
| 1524 | bi->bi_size = 0; |
| 1525 | bi->bi_end_io(bi, bytes, 0); |
| 1526 | } |
| 1527 | spin_unlock_irq(&conf->device_lock); |
| 1528 | return 0; |
| 1529 | } |
| 1530 | |
| 1531 | /* FIXME go_faster isn't used */ |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1532 | 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] | 1533 | { |
| 1534 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 1535 | struct stripe_head *sh; |
| 1536 | int sectors_per_chunk = conf->chunk_size >> 9; |
| 1537 | sector_t x; |
| 1538 | unsigned long stripe; |
| 1539 | int chunk_offset; |
| 1540 | int dd_idx, pd_idx; |
| 1541 | sector_t first_sector; |
| 1542 | int raid_disks = conf->raid_disks; |
| 1543 | int data_disks = raid_disks-1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1544 | sector_t max_sector = mddev->size << 1; |
| 1545 | int sync_blocks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1546 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1547 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1548 | /* just being told to finish up .. nothing much to do */ |
| 1549 | unplug_slaves(mddev); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1550 | |
| 1551 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 1552 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 1553 | &sync_blocks, 1); |
| 1554 | else /* compelted sync */ |
| 1555 | conf->fullsync = 0; |
| 1556 | bitmap_close_sync(mddev->bitmap); |
| 1557 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1558 | return 0; |
| 1559 | } |
| 1560 | /* if there is 1 or more failed drives and we are trying |
| 1561 | * to resync, then assert that we are finished, because there is |
| 1562 | * nothing we can do. |
| 1563 | */ |
| 1564 | if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 1565 | sector_t rv = (mddev->size << 1) - sector_nr; |
| 1566 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1567 | return rv; |
| 1568 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1569 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
| 1570 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 1571 | /* we can skip this block, and probably more */ |
| 1572 | sync_blocks /= STRIPE_SECTORS; |
| 1573 | *skipped = 1; |
| 1574 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 1575 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1576 | |
| 1577 | x = sector_nr; |
| 1578 | chunk_offset = sector_div(x, sectors_per_chunk); |
| 1579 | stripe = x; |
| 1580 | BUG_ON(x != stripe); |
| 1581 | |
| 1582 | first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk |
| 1583 | + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf); |
| 1584 | sh = get_active_stripe(conf, sector_nr, pd_idx, 1); |
| 1585 | if (sh == NULL) { |
| 1586 | sh = get_active_stripe(conf, sector_nr, pd_idx, 0); |
| 1587 | /* make sure we don't swamp the stripe cache if someone else |
| 1588 | * is trying to get access |
| 1589 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 1590 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1591 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1592 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1593 | spin_lock(&sh->lock); |
| 1594 | set_bit(STRIPE_SYNCING, &sh->state); |
| 1595 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 1596 | spin_unlock(&sh->lock); |
| 1597 | |
| 1598 | handle_stripe(sh); |
| 1599 | release_stripe(sh); |
| 1600 | |
| 1601 | return STRIPE_SECTORS; |
| 1602 | } |
| 1603 | |
| 1604 | /* |
| 1605 | * This is our raid5 kernel thread. |
| 1606 | * |
| 1607 | * We scan the hash table for stripes which can be handled now. |
| 1608 | * During the scan, completed stripes are saved for us by the interrupt |
| 1609 | * handler, so that they will not have to wait for our next wakeup. |
| 1610 | */ |
| 1611 | static void raid5d (mddev_t *mddev) |
| 1612 | { |
| 1613 | struct stripe_head *sh; |
| 1614 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 1615 | int handled; |
| 1616 | |
| 1617 | PRINTK("+++ raid5d active\n"); |
| 1618 | |
| 1619 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1620 | |
| 1621 | handled = 0; |
| 1622 | spin_lock_irq(&conf->device_lock); |
| 1623 | while (1) { |
| 1624 | struct list_head *first; |
| 1625 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1626 | if (conf->seq_flush - conf->seq_write > 0) { |
| 1627 | int seq = conf->seq_flush; |
| 1628 | bitmap_unplug(mddev->bitmap); |
| 1629 | conf->seq_write = seq; |
| 1630 | activate_bit_delay(conf); |
| 1631 | } |
| 1632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1633 | if (list_empty(&conf->handle_list) && |
| 1634 | atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD && |
| 1635 | !blk_queue_plugged(mddev->queue) && |
| 1636 | !list_empty(&conf->delayed_list)) |
| 1637 | raid5_activate_delayed(conf); |
| 1638 | |
| 1639 | if (list_empty(&conf->handle_list)) |
| 1640 | break; |
| 1641 | |
| 1642 | first = conf->handle_list.next; |
| 1643 | sh = list_entry(first, struct stripe_head, lru); |
| 1644 | |
| 1645 | list_del_init(first); |
| 1646 | atomic_inc(&sh->count); |
| 1647 | if (atomic_read(&sh->count)!= 1) |
| 1648 | BUG(); |
| 1649 | spin_unlock_irq(&conf->device_lock); |
| 1650 | |
| 1651 | handled++; |
| 1652 | handle_stripe(sh); |
| 1653 | release_stripe(sh); |
| 1654 | |
| 1655 | spin_lock_irq(&conf->device_lock); |
| 1656 | } |
| 1657 | PRINTK("%d stripes handled\n", handled); |
| 1658 | |
| 1659 | spin_unlock_irq(&conf->device_lock); |
| 1660 | |
| 1661 | unplug_slaves(mddev); |
| 1662 | |
| 1663 | PRINTK("--- raid5d inactive\n"); |
| 1664 | } |
| 1665 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1666 | static int run(mddev_t *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1667 | { |
| 1668 | raid5_conf_t *conf; |
| 1669 | int raid_disk, memory; |
| 1670 | mdk_rdev_t *rdev; |
| 1671 | struct disk_info *disk; |
| 1672 | struct list_head *tmp; |
| 1673 | |
| 1674 | if (mddev->level != 5 && mddev->level != 4) { |
| 1675 | printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level); |
| 1676 | return -EIO; |
| 1677 | } |
| 1678 | |
| 1679 | mddev->private = kmalloc (sizeof (raid5_conf_t) |
| 1680 | + mddev->raid_disks * sizeof(struct disk_info), |
| 1681 | GFP_KERNEL); |
| 1682 | if ((conf = mddev->private) == NULL) |
| 1683 | goto abort; |
| 1684 | memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) ); |
| 1685 | conf->mddev = mddev; |
| 1686 | |
| 1687 | if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL) |
| 1688 | goto abort; |
| 1689 | memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE); |
| 1690 | |
| 1691 | spin_lock_init(&conf->device_lock); |
| 1692 | init_waitqueue_head(&conf->wait_for_stripe); |
| 1693 | init_waitqueue_head(&conf->wait_for_overlap); |
| 1694 | INIT_LIST_HEAD(&conf->handle_list); |
| 1695 | INIT_LIST_HEAD(&conf->delayed_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1696 | INIT_LIST_HEAD(&conf->bitmap_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1697 | INIT_LIST_HEAD(&conf->inactive_list); |
| 1698 | atomic_set(&conf->active_stripes, 0); |
| 1699 | atomic_set(&conf->preread_active_stripes, 0); |
| 1700 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1701 | PRINTK("raid5: run(%s) called.\n", mdname(mddev)); |
| 1702 | |
| 1703 | ITERATE_RDEV(mddev,rdev,tmp) { |
| 1704 | raid_disk = rdev->raid_disk; |
| 1705 | if (raid_disk >= mddev->raid_disks |
| 1706 | || raid_disk < 0) |
| 1707 | continue; |
| 1708 | disk = conf->disks + raid_disk; |
| 1709 | |
| 1710 | disk->rdev = rdev; |
| 1711 | |
| 1712 | if (rdev->in_sync) { |
| 1713 | char b[BDEVNAME_SIZE]; |
| 1714 | printk(KERN_INFO "raid5: device %s operational as raid" |
| 1715 | " disk %d\n", bdevname(rdev->bdev,b), |
| 1716 | raid_disk); |
| 1717 | conf->working_disks++; |
| 1718 | } |
| 1719 | } |
| 1720 | |
| 1721 | conf->raid_disks = mddev->raid_disks; |
| 1722 | /* |
| 1723 | * 0 for a fully functional array, 1 for a degraded array. |
| 1724 | */ |
| 1725 | mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks; |
| 1726 | conf->mddev = mddev; |
| 1727 | conf->chunk_size = mddev->chunk_size; |
| 1728 | conf->level = mddev->level; |
| 1729 | conf->algorithm = mddev->layout; |
| 1730 | conf->max_nr_stripes = NR_STRIPES; |
| 1731 | |
| 1732 | /* device size must be a multiple of chunk size */ |
| 1733 | mddev->size &= ~(mddev->chunk_size/1024 -1); |
NeilBrown | b158156 | 2005-07-31 22:34:50 -0700 | [diff] [blame] | 1734 | mddev->resync_max_sectors = mddev->size << 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | |
| 1736 | if (!conf->chunk_size || conf->chunk_size % 4) { |
| 1737 | printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", |
| 1738 | conf->chunk_size, mdname(mddev)); |
| 1739 | goto abort; |
| 1740 | } |
| 1741 | if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) { |
| 1742 | printk(KERN_ERR |
| 1743 | "raid5: unsupported parity algorithm %d for %s\n", |
| 1744 | conf->algorithm, mdname(mddev)); |
| 1745 | goto abort; |
| 1746 | } |
| 1747 | if (mddev->degraded > 1) { |
| 1748 | printk(KERN_ERR "raid5: not enough operational devices for %s" |
| 1749 | " (%d/%d failed)\n", |
| 1750 | mdname(mddev), conf->failed_disks, conf->raid_disks); |
| 1751 | goto abort; |
| 1752 | } |
| 1753 | |
| 1754 | if (mddev->degraded == 1 && |
| 1755 | mddev->recovery_cp != MaxSector) { |
| 1756 | printk(KERN_ERR |
| 1757 | "raid5: cannot start dirty degraded array for %s\n", |
| 1758 | mdname(mddev)); |
| 1759 | goto abort; |
| 1760 | } |
| 1761 | |
| 1762 | { |
| 1763 | mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5"); |
| 1764 | if (!mddev->thread) { |
| 1765 | printk(KERN_ERR |
| 1766 | "raid5: couldn't allocate thread for %s\n", |
| 1767 | mdname(mddev)); |
| 1768 | goto abort; |
| 1769 | } |
| 1770 | } |
| 1771 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
| 1772 | conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
| 1773 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 1774 | printk(KERN_ERR |
| 1775 | "raid5: couldn't allocate %dkB for buffers\n", memory); |
| 1776 | shrink_stripes(conf); |
| 1777 | md_unregister_thread(mddev->thread); |
| 1778 | goto abort; |
| 1779 | } else |
| 1780 | printk(KERN_INFO "raid5: allocated %dkB for %s\n", |
| 1781 | memory, mdname(mddev)); |
| 1782 | |
| 1783 | if (mddev->degraded == 0) |
| 1784 | printk("raid5: raid level %d set %s active with %d out of %d" |
| 1785 | " devices, algorithm %d\n", conf->level, mdname(mddev), |
| 1786 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 1787 | conf->algorithm); |
| 1788 | else |
| 1789 | printk(KERN_ALERT "raid5: raid level %d set %s active with %d" |
| 1790 | " out of %d devices, algorithm %d\n", conf->level, |
| 1791 | mdname(mddev), mddev->raid_disks - mddev->degraded, |
| 1792 | mddev->raid_disks, conf->algorithm); |
| 1793 | |
| 1794 | print_raid5_conf(conf); |
| 1795 | |
| 1796 | /* read-ahead size must cover two whole stripes, which is |
| 1797 | * 2 * (n-1) * chunksize where 'n' is the number of raid devices |
| 1798 | */ |
| 1799 | { |
| 1800 | int stripe = (mddev->raid_disks-1) * mddev->chunk_size |
| 1801 | / PAGE_CACHE_SIZE; |
| 1802 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 1803 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 1804 | } |
| 1805 | |
| 1806 | /* Ok, everything is just fine now */ |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 1807 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1808 | if (mddev->bitmap) |
| 1809 | mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ; |
| 1810 | |
NeilBrown | 7a5febe | 2005-05-16 21:53:16 -0700 | [diff] [blame] | 1811 | mddev->queue->unplug_fn = raid5_unplug_device; |
| 1812 | mddev->queue->issue_flush_fn = raid5_issue_flush; |
| 1813 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1814 | mddev->array_size = mddev->size * (mddev->raid_disks - 1); |
| 1815 | return 0; |
| 1816 | abort: |
| 1817 | if (conf) { |
| 1818 | print_raid5_conf(conf); |
| 1819 | if (conf->stripe_hashtbl) |
| 1820 | free_pages((unsigned long) conf->stripe_hashtbl, |
| 1821 | HASH_PAGES_ORDER); |
| 1822 | kfree(conf); |
| 1823 | } |
| 1824 | mddev->private = NULL; |
| 1825 | printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev)); |
| 1826 | return -EIO; |
| 1827 | } |
| 1828 | |
| 1829 | |
| 1830 | |
| 1831 | static int stop (mddev_t *mddev) |
| 1832 | { |
| 1833 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 1834 | |
| 1835 | md_unregister_thread(mddev->thread); |
| 1836 | mddev->thread = NULL; |
| 1837 | shrink_stripes(conf); |
| 1838 | free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER); |
| 1839 | blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/ |
| 1840 | kfree(conf); |
| 1841 | mddev->private = NULL; |
| 1842 | return 0; |
| 1843 | } |
| 1844 | |
| 1845 | #if RAID5_DEBUG |
| 1846 | static void print_sh (struct stripe_head *sh) |
| 1847 | { |
| 1848 | int i; |
| 1849 | |
| 1850 | printk("sh %llu, pd_idx %d, state %ld.\n", |
| 1851 | (unsigned long long)sh->sector, sh->pd_idx, sh->state); |
| 1852 | printk("sh %llu, count %d.\n", |
| 1853 | (unsigned long long)sh->sector, atomic_read(&sh->count)); |
| 1854 | printk("sh %llu, ", (unsigned long long)sh->sector); |
| 1855 | for (i = 0; i < sh->raid_conf->raid_disks; i++) { |
| 1856 | printk("(cache%d: %p %ld) ", |
| 1857 | i, sh->dev[i].page, sh->dev[i].flags); |
| 1858 | } |
| 1859 | printk("\n"); |
| 1860 | } |
| 1861 | |
| 1862 | static void printall (raid5_conf_t *conf) |
| 1863 | { |
| 1864 | struct stripe_head *sh; |
| 1865 | int i; |
| 1866 | |
| 1867 | spin_lock_irq(&conf->device_lock); |
| 1868 | for (i = 0; i < NR_HASH; i++) { |
| 1869 | sh = conf->stripe_hashtbl[i]; |
| 1870 | for (; sh; sh = sh->hash_next) { |
| 1871 | if (sh->raid_conf != conf) |
| 1872 | continue; |
| 1873 | print_sh(sh); |
| 1874 | } |
| 1875 | } |
| 1876 | spin_unlock_irq(&conf->device_lock); |
| 1877 | } |
| 1878 | #endif |
| 1879 | |
| 1880 | static void status (struct seq_file *seq, mddev_t *mddev) |
| 1881 | { |
| 1882 | raid5_conf_t *conf = (raid5_conf_t *) mddev->private; |
| 1883 | int i; |
| 1884 | |
| 1885 | seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout); |
| 1886 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks); |
| 1887 | for (i = 0; i < conf->raid_disks; i++) |
| 1888 | seq_printf (seq, "%s", |
| 1889 | conf->disks[i].rdev && |
| 1890 | conf->disks[i].rdev->in_sync ? "U" : "_"); |
| 1891 | seq_printf (seq, "]"); |
| 1892 | #if RAID5_DEBUG |
| 1893 | #define D(x) \ |
| 1894 | seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x)) |
| 1895 | printall(conf); |
| 1896 | #endif |
| 1897 | } |
| 1898 | |
| 1899 | static void print_raid5_conf (raid5_conf_t *conf) |
| 1900 | { |
| 1901 | int i; |
| 1902 | struct disk_info *tmp; |
| 1903 | |
| 1904 | printk("RAID5 conf printout:\n"); |
| 1905 | if (!conf) { |
| 1906 | printk("(conf==NULL)\n"); |
| 1907 | return; |
| 1908 | } |
| 1909 | printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks, |
| 1910 | conf->working_disks, conf->failed_disks); |
| 1911 | |
| 1912 | for (i = 0; i < conf->raid_disks; i++) { |
| 1913 | char b[BDEVNAME_SIZE]; |
| 1914 | tmp = conf->disks + i; |
| 1915 | if (tmp->rdev) |
| 1916 | printk(" disk %d, o:%d, dev:%s\n", |
| 1917 | i, !tmp->rdev->faulty, |
| 1918 | bdevname(tmp->rdev->bdev,b)); |
| 1919 | } |
| 1920 | } |
| 1921 | |
| 1922 | static int raid5_spare_active(mddev_t *mddev) |
| 1923 | { |
| 1924 | int i; |
| 1925 | raid5_conf_t *conf = mddev->private; |
| 1926 | struct disk_info *tmp; |
| 1927 | |
| 1928 | for (i = 0; i < conf->raid_disks; i++) { |
| 1929 | tmp = conf->disks + i; |
| 1930 | if (tmp->rdev |
| 1931 | && !tmp->rdev->faulty |
| 1932 | && !tmp->rdev->in_sync) { |
| 1933 | mddev->degraded--; |
| 1934 | conf->failed_disks--; |
| 1935 | conf->working_disks++; |
| 1936 | tmp->rdev->in_sync = 1; |
| 1937 | } |
| 1938 | } |
| 1939 | print_raid5_conf(conf); |
| 1940 | return 0; |
| 1941 | } |
| 1942 | |
| 1943 | static int raid5_remove_disk(mddev_t *mddev, int number) |
| 1944 | { |
| 1945 | raid5_conf_t *conf = mddev->private; |
| 1946 | int err = 0; |
| 1947 | mdk_rdev_t *rdev; |
| 1948 | struct disk_info *p = conf->disks + number; |
| 1949 | |
| 1950 | print_raid5_conf(conf); |
| 1951 | rdev = p->rdev; |
| 1952 | if (rdev) { |
| 1953 | if (rdev->in_sync || |
| 1954 | atomic_read(&rdev->nr_pending)) { |
| 1955 | err = -EBUSY; |
| 1956 | goto abort; |
| 1957 | } |
| 1958 | p->rdev = NULL; |
Paul E. McKenney | fbd568a3e | 2005-05-01 08:59:04 -0700 | [diff] [blame] | 1959 | synchronize_rcu(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1960 | if (atomic_read(&rdev->nr_pending)) { |
| 1961 | /* lost the race, try later */ |
| 1962 | err = -EBUSY; |
| 1963 | p->rdev = rdev; |
| 1964 | } |
| 1965 | } |
| 1966 | abort: |
| 1967 | |
| 1968 | print_raid5_conf(conf); |
| 1969 | return err; |
| 1970 | } |
| 1971 | |
| 1972 | static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev) |
| 1973 | { |
| 1974 | raid5_conf_t *conf = mddev->private; |
| 1975 | int found = 0; |
| 1976 | int disk; |
| 1977 | struct disk_info *p; |
| 1978 | |
| 1979 | if (mddev->degraded > 1) |
| 1980 | /* no point adding a device */ |
| 1981 | return 0; |
| 1982 | |
| 1983 | /* |
| 1984 | * find the disk ... |
| 1985 | */ |
| 1986 | for (disk=0; disk < mddev->raid_disks; disk++) |
| 1987 | if ((p=conf->disks + disk)->rdev == NULL) { |
| 1988 | rdev->in_sync = 0; |
| 1989 | rdev->raid_disk = disk; |
| 1990 | found = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 1991 | if (rdev->saved_raid_disk != disk) |
| 1992 | conf->fullsync = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1993 | p->rdev = rdev; |
| 1994 | break; |
| 1995 | } |
| 1996 | print_raid5_conf(conf); |
| 1997 | return found; |
| 1998 | } |
| 1999 | |
| 2000 | static int raid5_resize(mddev_t *mddev, sector_t sectors) |
| 2001 | { |
| 2002 | /* no resync is happening, and there is enough space |
| 2003 | * on all devices, so we can resize. |
| 2004 | * We need to make sure resync covers any new space. |
| 2005 | * If the array is shrinking we should possibly wait until |
| 2006 | * any io in the removed space completes, but it hardly seems |
| 2007 | * worth it. |
| 2008 | */ |
| 2009 | sectors &= ~((sector_t)mddev->chunk_size/512 - 1); |
| 2010 | mddev->array_size = (sectors * (mddev->raid_disks-1))>>1; |
| 2011 | set_capacity(mddev->gendisk, mddev->array_size << 1); |
| 2012 | mddev->changed = 1; |
| 2013 | if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) { |
| 2014 | mddev->recovery_cp = mddev->size << 1; |
| 2015 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 2016 | } |
| 2017 | mddev->size = sectors /2; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 2018 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | return 0; |
| 2020 | } |
| 2021 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2022 | static void raid5_quiesce(mddev_t *mddev, int state) |
| 2023 | { |
| 2024 | raid5_conf_t *conf = mddev_to_conf(mddev); |
| 2025 | |
| 2026 | switch(state) { |
| 2027 | case 1: /* stop all writes */ |
| 2028 | spin_lock_irq(&conf->device_lock); |
| 2029 | conf->quiesce = 1; |
| 2030 | wait_event_lock_irq(conf->wait_for_stripe, |
| 2031 | atomic_read(&conf->active_stripes) == 0, |
| 2032 | conf->device_lock, /* nothing */); |
| 2033 | spin_unlock_irq(&conf->device_lock); |
| 2034 | break; |
| 2035 | |
| 2036 | case 0: /* re-enable writes */ |
| 2037 | spin_lock_irq(&conf->device_lock); |
| 2038 | conf->quiesce = 0; |
| 2039 | wake_up(&conf->wait_for_stripe); |
| 2040 | spin_unlock_irq(&conf->device_lock); |
| 2041 | break; |
| 2042 | } |
| 2043 | if (mddev->thread) { |
| 2044 | if (mddev->bitmap) |
| 2045 | mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ; |
| 2046 | else |
| 2047 | mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT; |
| 2048 | md_wakeup_thread(mddev->thread); |
| 2049 | } |
| 2050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2051 | static mdk_personality_t raid5_personality= |
| 2052 | { |
| 2053 | .name = "raid5", |
| 2054 | .owner = THIS_MODULE, |
| 2055 | .make_request = make_request, |
| 2056 | .run = run, |
| 2057 | .stop = stop, |
| 2058 | .status = status, |
| 2059 | .error_handler = error, |
| 2060 | .hot_add_disk = raid5_add_disk, |
| 2061 | .hot_remove_disk= raid5_remove_disk, |
| 2062 | .spare_active = raid5_spare_active, |
| 2063 | .sync_request = sync_request, |
| 2064 | .resize = raid5_resize, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2065 | .quiesce = raid5_quiesce, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | }; |
| 2067 | |
| 2068 | static int __init raid5_init (void) |
| 2069 | { |
| 2070 | return register_md_personality (RAID5, &raid5_personality); |
| 2071 | } |
| 2072 | |
| 2073 | static void raid5_exit (void) |
| 2074 | { |
| 2075 | unregister_md_personality (RAID5); |
| 2076 | } |
| 2077 | |
| 2078 | module_init(raid5_init); |
| 2079 | module_exit(raid5_exit); |
| 2080 | MODULE_LICENSE("GPL"); |
| 2081 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |