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