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