[PATCH] dm: fix mapped device ref counting
[linux-2.6/verdex.git] / drivers / md / raid5.c
blob31843604049cdb7d6f916fee3861c1994869cf6d
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
6 * RAID-5 management functions.
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.
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.
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 <linux/kthread.h>
26 #include <asm/atomic.h>
28 #include <linux/raid/bitmap.h>
31 * Stripe cache
34 #define NR_STRIPES 256
35 #define STRIPE_SIZE PAGE_SIZE
36 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
37 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
38 #define IO_THRESHOLD 1
39 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
40 #define HASH_MASK (NR_HASH - 1)
42 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
44 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
45 * order without overlap. There may be several bio's per stripe+device, and
46 * a bio could span several devices.
47 * When walking this list for a particular stripe+device, we must never proceed
48 * beyond a bio that extends past this device, as the next bio might no longer
49 * be valid.
50 * This macro is used to determine the 'next' bio in the list, given the sector
51 * of the current stripe+device
53 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
55 * The following can be used to debug the driver
57 #define RAID5_DEBUG 0
58 #define RAID5_PARANOIA 1
59 #if RAID5_PARANOIA && defined(CONFIG_SMP)
60 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
61 #else
62 # define CHECK_DEVLOCK()
63 #endif
65 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
66 #if RAID5_DEBUG
67 #define inline
68 #define __inline__
69 #endif
71 static void print_raid5_conf (raid5_conf_t *conf);
73 static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
75 if (atomic_dec_and_test(&sh->count)) {
76 BUG_ON(!list_empty(&sh->lru));
77 BUG_ON(atomic_read(&conf->active_stripes)==0);
78 if (test_bit(STRIPE_HANDLE, &sh->state)) {
79 if (test_bit(STRIPE_DELAYED, &sh->state))
80 list_add_tail(&sh->lru, &conf->delayed_list);
81 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
82 conf->seq_write == sh->bm_seq)
83 list_add_tail(&sh->lru, &conf->bitmap_list);
84 else {
85 clear_bit(STRIPE_BIT_DELAY, &sh->state);
86 list_add_tail(&sh->lru, &conf->handle_list);
88 md_wakeup_thread(conf->mddev->thread);
89 } else {
90 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
91 atomic_dec(&conf->preread_active_stripes);
92 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
93 md_wakeup_thread(conf->mddev->thread);
95 atomic_dec(&conf->active_stripes);
96 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
97 list_add_tail(&sh->lru, &conf->inactive_list);
98 wake_up(&conf->wait_for_stripe);
103 static void release_stripe(struct stripe_head *sh)
105 raid5_conf_t *conf = sh->raid_conf;
106 unsigned long flags;
108 spin_lock_irqsave(&conf->device_lock, flags);
109 __release_stripe(conf, sh);
110 spin_unlock_irqrestore(&conf->device_lock, flags);
113 static inline void remove_hash(struct stripe_head *sh)
115 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
117 hlist_del_init(&sh->hash);
120 static void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
122 struct hlist_head *hp = stripe_hash(conf, sh->sector);
124 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
126 CHECK_DEVLOCK();
127 hlist_add_head(&sh->hash, hp);
131 /* find an idle stripe, make sure it is unhashed, and return it. */
132 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
134 struct stripe_head *sh = NULL;
135 struct list_head *first;
137 CHECK_DEVLOCK();
138 if (list_empty(&conf->inactive_list))
139 goto out;
140 first = conf->inactive_list.next;
141 sh = list_entry(first, struct stripe_head, lru);
142 list_del_init(first);
143 remove_hash(sh);
144 atomic_inc(&conf->active_stripes);
145 out:
146 return sh;
149 static void shrink_buffers(struct stripe_head *sh, int num)
151 struct page *p;
152 int i;
154 for (i=0; i<num ; i++) {
155 p = sh->dev[i].page;
156 if (!p)
157 continue;
158 sh->dev[i].page = NULL;
159 put_page(p);
163 static int grow_buffers(struct stripe_head *sh, int num)
165 int i;
167 for (i=0; i<num; i++) {
168 struct page *page;
170 if (!(page = alloc_page(GFP_KERNEL))) {
171 return 1;
173 sh->dev[i].page = page;
175 return 0;
178 static void raid5_build_block (struct stripe_head *sh, int i);
180 static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
182 raid5_conf_t *conf = sh->raid_conf;
183 int i;
185 BUG_ON(atomic_read(&sh->count) != 0);
186 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
188 CHECK_DEVLOCK();
189 PRINTK("init_stripe called, stripe %llu\n",
190 (unsigned long long)sh->sector);
192 remove_hash(sh);
194 sh->sector = sector;
195 sh->pd_idx = pd_idx;
196 sh->state = 0;
198 sh->disks = disks;
200 for (i = sh->disks; i--; ) {
201 struct r5dev *dev = &sh->dev[i];
203 if (dev->toread || dev->towrite || dev->written ||
204 test_bit(R5_LOCKED, &dev->flags)) {
205 printk("sector=%llx i=%d %p %p %p %d\n",
206 (unsigned long long)sh->sector, i, dev->toread,
207 dev->towrite, dev->written,
208 test_bit(R5_LOCKED, &dev->flags));
209 BUG();
211 dev->flags = 0;
212 raid5_build_block(sh, i);
214 insert_hash(conf, sh);
217 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
219 struct stripe_head *sh;
220 struct hlist_node *hn;
222 CHECK_DEVLOCK();
223 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
224 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
225 if (sh->sector == sector && sh->disks == disks)
226 return sh;
227 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
228 return NULL;
231 static void unplug_slaves(mddev_t *mddev);
232 static void raid5_unplug_device(request_queue_t *q);
234 static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
235 int pd_idx, int noblock)
237 struct stripe_head *sh;
239 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
241 spin_lock_irq(&conf->device_lock);
243 do {
244 wait_event_lock_irq(conf->wait_for_stripe,
245 conf->quiesce == 0,
246 conf->device_lock, /* nothing */);
247 sh = __find_stripe(conf, sector, disks);
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)
258 < (conf->max_nr_stripes *3/4)
259 || !conf->inactive_blocked),
260 conf->device_lock,
261 unplug_slaves(conf->mddev)
263 conf->inactive_blocked = 0;
264 } else
265 init_stripe(sh, sector, pd_idx, disks);
266 } else {
267 if (atomic_read(&sh->count)) {
268 BUG_ON(!list_empty(&sh->lru));
269 } else {
270 if (!test_bit(STRIPE_HANDLE, &sh->state))
271 atomic_inc(&conf->active_stripes);
272 if (!list_empty(&sh->lru))
273 list_del_init(&sh->lru);
276 } while (sh == NULL);
278 if (sh)
279 atomic_inc(&sh->count);
281 spin_unlock_irq(&conf->device_lock);
282 return sh;
285 static int grow_one_stripe(raid5_conf_t *conf)
287 struct stripe_head *sh;
288 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
289 if (!sh)
290 return 0;
291 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
292 sh->raid_conf = conf;
293 spin_lock_init(&sh->lock);
295 if (grow_buffers(sh, conf->raid_disks)) {
296 shrink_buffers(sh, conf->raid_disks);
297 kmem_cache_free(conf->slab_cache, sh);
298 return 0;
300 sh->disks = conf->raid_disks;
301 /* we just created an active stripe so... */
302 atomic_set(&sh->count, 1);
303 atomic_inc(&conf->active_stripes);
304 INIT_LIST_HEAD(&sh->lru);
305 release_stripe(sh);
306 return 1;
309 static int grow_stripes(raid5_conf_t *conf, int num)
311 kmem_cache_t *sc;
312 int devs = conf->raid_disks;
314 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
315 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
316 conf->active_name = 0;
317 sc = kmem_cache_create(conf->cache_name[conf->active_name],
318 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
319 0, 0, NULL, NULL);
320 if (!sc)
321 return 1;
322 conf->slab_cache = sc;
323 conf->pool_size = devs;
324 while (num--) {
325 if (!grow_one_stripe(conf))
326 return 1;
328 return 0;
331 #ifdef CONFIG_MD_RAID5_RESHAPE
332 static int resize_stripes(raid5_conf_t *conf, int newsize)
334 /* Make all the stripes able to hold 'newsize' devices.
335 * New slots in each stripe get 'page' set to a new page.
337 * This happens in stages:
338 * 1/ create a new kmem_cache and allocate the required number of
339 * stripe_heads.
340 * 2/ gather all the old stripe_heads and tranfer the pages across
341 * to the new stripe_heads. This will have the side effect of
342 * freezing the array as once all stripe_heads have been collected,
343 * no IO will be possible. Old stripe heads are freed once their
344 * pages have been transferred over, and the old kmem_cache is
345 * freed when all stripes are done.
346 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
347 * we simple return a failre status - no need to clean anything up.
348 * 4/ allocate new pages for the new slots in the new stripe_heads.
349 * If this fails, we don't bother trying the shrink the
350 * stripe_heads down again, we just leave them as they are.
351 * As each stripe_head is processed the new one is released into
352 * active service.
354 * Once step2 is started, we cannot afford to wait for a write,
355 * so we use GFP_NOIO allocations.
357 struct stripe_head *osh, *nsh;
358 LIST_HEAD(newstripes);
359 struct disk_info *ndisks;
360 int err = 0;
361 kmem_cache_t *sc;
362 int i;
364 if (newsize <= conf->pool_size)
365 return 0; /* never bother to shrink */
367 /* Step 1 */
368 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
369 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
370 0, 0, NULL, NULL);
371 if (!sc)
372 return -ENOMEM;
374 for (i = conf->max_nr_stripes; i; i--) {
375 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
376 if (!nsh)
377 break;
379 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
381 nsh->raid_conf = conf;
382 spin_lock_init(&nsh->lock);
384 list_add(&nsh->lru, &newstripes);
386 if (i) {
387 /* didn't get enough, give up */
388 while (!list_empty(&newstripes)) {
389 nsh = list_entry(newstripes.next, struct stripe_head, lru);
390 list_del(&nsh->lru);
391 kmem_cache_free(sc, nsh);
393 kmem_cache_destroy(sc);
394 return -ENOMEM;
396 /* Step 2 - Must use GFP_NOIO now.
397 * OK, we have enough stripes, start collecting inactive
398 * stripes and copying them over
400 list_for_each_entry(nsh, &newstripes, lru) {
401 spin_lock_irq(&conf->device_lock);
402 wait_event_lock_irq(conf->wait_for_stripe,
403 !list_empty(&conf->inactive_list),
404 conf->device_lock,
405 unplug_slaves(conf->mddev)
407 osh = get_free_stripe(conf);
408 spin_unlock_irq(&conf->device_lock);
409 atomic_set(&nsh->count, 1);
410 for(i=0; i<conf->pool_size; i++)
411 nsh->dev[i].page = osh->dev[i].page;
412 for( ; i<newsize; i++)
413 nsh->dev[i].page = NULL;
414 kmem_cache_free(conf->slab_cache, osh);
416 kmem_cache_destroy(conf->slab_cache);
418 /* Step 3.
419 * At this point, we are holding all the stripes so the array
420 * is completely stalled, so now is a good time to resize
421 * conf->disks.
423 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
424 if (ndisks) {
425 for (i=0; i<conf->raid_disks; i++)
426 ndisks[i] = conf->disks[i];
427 kfree(conf->disks);
428 conf->disks = ndisks;
429 } else
430 err = -ENOMEM;
432 /* Step 4, return new stripes to service */
433 while(!list_empty(&newstripes)) {
434 nsh = list_entry(newstripes.next, struct stripe_head, lru);
435 list_del_init(&nsh->lru);
436 for (i=conf->raid_disks; i < newsize; i++)
437 if (nsh->dev[i].page == NULL) {
438 struct page *p = alloc_page(GFP_NOIO);
439 nsh->dev[i].page = p;
440 if (!p)
441 err = -ENOMEM;
443 release_stripe(nsh);
445 /* critical section pass, GFP_NOIO no longer needed */
447 conf->slab_cache = sc;
448 conf->active_name = 1-conf->active_name;
449 conf->pool_size = newsize;
450 return err;
452 #endif
454 static int drop_one_stripe(raid5_conf_t *conf)
456 struct stripe_head *sh;
458 spin_lock_irq(&conf->device_lock);
459 sh = get_free_stripe(conf);
460 spin_unlock_irq(&conf->device_lock);
461 if (!sh)
462 return 0;
463 BUG_ON(atomic_read(&sh->count));
464 shrink_buffers(sh, conf->pool_size);
465 kmem_cache_free(conf->slab_cache, sh);
466 atomic_dec(&conf->active_stripes);
467 return 1;
470 static void shrink_stripes(raid5_conf_t *conf)
472 while (drop_one_stripe(conf))
475 if (conf->slab_cache)
476 kmem_cache_destroy(conf->slab_cache);
477 conf->slab_cache = NULL;
480 static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
481 int error)
483 struct stripe_head *sh = bi->bi_private;
484 raid5_conf_t *conf = sh->raid_conf;
485 int disks = sh->disks, i;
486 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
488 if (bi->bi_size)
489 return 1;
491 for (i=0 ; i<disks; i++)
492 if (bi == &sh->dev[i].req)
493 break;
495 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
496 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
497 uptodate);
498 if (i == disks) {
499 BUG();
500 return 0;
503 if (uptodate) {
504 #if 0
505 struct bio *bio;
506 unsigned long flags;
507 spin_lock_irqsave(&conf->device_lock, flags);
508 /* we can return a buffer if we bypassed the cache or
509 * if the top buffer is not in highmem. If there are
510 * multiple buffers, leave the extra work to
511 * handle_stripe
513 buffer = sh->bh_read[i];
514 if (buffer &&
515 (!PageHighMem(buffer->b_page)
516 || buffer->b_page == bh->b_page )
518 sh->bh_read[i] = buffer->b_reqnext;
519 buffer->b_reqnext = NULL;
520 } else
521 buffer = NULL;
522 spin_unlock_irqrestore(&conf->device_lock, flags);
523 if (sh->bh_page[i]==bh->b_page)
524 set_buffer_uptodate(bh);
525 if (buffer) {
526 if (buffer->b_page != bh->b_page)
527 memcpy(buffer->b_data, bh->b_data, bh->b_size);
528 buffer->b_end_io(buffer, 1);
530 #else
531 set_bit(R5_UPTODATE, &sh->dev[i].flags);
532 #endif
533 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
534 printk(KERN_INFO "raid5: read error corrected!!\n");
535 clear_bit(R5_ReadError, &sh->dev[i].flags);
536 clear_bit(R5_ReWrite, &sh->dev[i].flags);
538 if (atomic_read(&conf->disks[i].rdev->read_errors))
539 atomic_set(&conf->disks[i].rdev->read_errors, 0);
540 } else {
541 int retry = 0;
542 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
543 atomic_inc(&conf->disks[i].rdev->read_errors);
544 if (conf->mddev->degraded)
545 printk(KERN_WARNING "raid5: read error not correctable.\n");
546 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
547 /* Oh, no!!! */
548 printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
549 else if (atomic_read(&conf->disks[i].rdev->read_errors)
550 > conf->max_nr_stripes)
551 printk(KERN_WARNING
552 "raid5: Too many read errors, failing device.\n");
553 else
554 retry = 1;
555 if (retry)
556 set_bit(R5_ReadError, &sh->dev[i].flags);
557 else {
558 clear_bit(R5_ReadError, &sh->dev[i].flags);
559 clear_bit(R5_ReWrite, &sh->dev[i].flags);
560 md_error(conf->mddev, conf->disks[i].rdev);
563 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
564 #if 0
565 /* must restore b_page before unlocking buffer... */
566 if (sh->bh_page[i] != bh->b_page) {
567 bh->b_page = sh->bh_page[i];
568 bh->b_data = page_address(bh->b_page);
569 clear_buffer_uptodate(bh);
571 #endif
572 clear_bit(R5_LOCKED, &sh->dev[i].flags);
573 set_bit(STRIPE_HANDLE, &sh->state);
574 release_stripe(sh);
575 return 0;
578 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
579 int error)
581 struct stripe_head *sh = bi->bi_private;
582 raid5_conf_t *conf = sh->raid_conf;
583 int disks = sh->disks, i;
584 unsigned long flags;
585 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
587 if (bi->bi_size)
588 return 1;
590 for (i=0 ; i<disks; i++)
591 if (bi == &sh->dev[i].req)
592 break;
594 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
595 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
596 uptodate);
597 if (i == disks) {
598 BUG();
599 return 0;
602 spin_lock_irqsave(&conf->device_lock, flags);
603 if (!uptodate)
604 md_error(conf->mddev, conf->disks[i].rdev);
606 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
608 clear_bit(R5_LOCKED, &sh->dev[i].flags);
609 set_bit(STRIPE_HANDLE, &sh->state);
610 __release_stripe(conf, sh);
611 spin_unlock_irqrestore(&conf->device_lock, flags);
612 return 0;
616 static sector_t compute_blocknr(struct stripe_head *sh, int i);
618 static void raid5_build_block (struct stripe_head *sh, int i)
620 struct r5dev *dev = &sh->dev[i];
622 bio_init(&dev->req);
623 dev->req.bi_io_vec = &dev->vec;
624 dev->req.bi_vcnt++;
625 dev->req.bi_max_vecs++;
626 dev->vec.bv_page = dev->page;
627 dev->vec.bv_len = STRIPE_SIZE;
628 dev->vec.bv_offset = 0;
630 dev->req.bi_sector = sh->sector;
631 dev->req.bi_private = sh;
633 dev->flags = 0;
634 if (i != sh->pd_idx)
635 dev->sector = compute_blocknr(sh, i);
638 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
640 char b[BDEVNAME_SIZE];
641 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
642 PRINTK("raid5: error called\n");
644 if (!test_bit(Faulty, &rdev->flags)) {
645 mddev->sb_dirty = 1;
646 if (test_bit(In_sync, &rdev->flags)) {
647 conf->working_disks--;
648 mddev->degraded++;
649 conf->failed_disks++;
650 clear_bit(In_sync, &rdev->flags);
652 * if recovery was running, make sure it aborts.
654 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
656 set_bit(Faulty, &rdev->flags);
657 printk (KERN_ALERT
658 "raid5: Disk failure on %s, disabling device."
659 " Operation continuing on %d devices\n",
660 bdevname(rdev->bdev,b), conf->working_disks);
665 * Input: a 'big' sector number,
666 * Output: index of the data and parity disk, and the sector # in them.
668 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
669 unsigned int data_disks, unsigned int * dd_idx,
670 unsigned int * pd_idx, raid5_conf_t *conf)
672 long stripe;
673 unsigned long chunk_number;
674 unsigned int chunk_offset;
675 sector_t new_sector;
676 int sectors_per_chunk = conf->chunk_size >> 9;
678 /* First compute the information on this sector */
681 * Compute the chunk number and the sector offset inside the chunk
683 chunk_offset = sector_div(r_sector, sectors_per_chunk);
684 chunk_number = r_sector;
685 BUG_ON(r_sector != chunk_number);
688 * Compute the stripe number
690 stripe = chunk_number / data_disks;
693 * Compute the data disk and parity disk indexes inside the stripe
695 *dd_idx = chunk_number % data_disks;
698 * Select the parity disk based on the user selected algorithm.
700 if (conf->level == 4)
701 *pd_idx = data_disks;
702 else switch (conf->algorithm) {
703 case ALGORITHM_LEFT_ASYMMETRIC:
704 *pd_idx = data_disks - stripe % raid_disks;
705 if (*dd_idx >= *pd_idx)
706 (*dd_idx)++;
707 break;
708 case ALGORITHM_RIGHT_ASYMMETRIC:
709 *pd_idx = stripe % raid_disks;
710 if (*dd_idx >= *pd_idx)
711 (*dd_idx)++;
712 break;
713 case ALGORITHM_LEFT_SYMMETRIC:
714 *pd_idx = data_disks - stripe % raid_disks;
715 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
716 break;
717 case ALGORITHM_RIGHT_SYMMETRIC:
718 *pd_idx = stripe % raid_disks;
719 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
720 break;
721 default:
722 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
723 conf->algorithm);
727 * Finally, compute the new sector number
729 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
730 return new_sector;
734 static sector_t compute_blocknr(struct stripe_head *sh, int i)
736 raid5_conf_t *conf = sh->raid_conf;
737 int raid_disks = sh->disks, data_disks = raid_disks - 1;
738 sector_t new_sector = sh->sector, check;
739 int sectors_per_chunk = conf->chunk_size >> 9;
740 sector_t stripe;
741 int chunk_offset;
742 int chunk_number, dummy1, dummy2, dd_idx = i;
743 sector_t r_sector;
745 chunk_offset = sector_div(new_sector, sectors_per_chunk);
746 stripe = new_sector;
747 BUG_ON(new_sector != stripe);
750 switch (conf->algorithm) {
751 case ALGORITHM_LEFT_ASYMMETRIC:
752 case ALGORITHM_RIGHT_ASYMMETRIC:
753 if (i > sh->pd_idx)
754 i--;
755 break;
756 case ALGORITHM_LEFT_SYMMETRIC:
757 case ALGORITHM_RIGHT_SYMMETRIC:
758 if (i < sh->pd_idx)
759 i += raid_disks;
760 i -= (sh->pd_idx + 1);
761 break;
762 default:
763 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
764 conf->algorithm);
767 chunk_number = stripe * data_disks + i;
768 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
770 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
771 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
772 printk(KERN_ERR "compute_blocknr: map not correct\n");
773 return 0;
775 return r_sector;
781 * Copy data between a page in the stripe cache, and a bio.
782 * There are no alignment or size guarantees between the page or the
783 * bio except that there is some overlap.
784 * All iovecs in the bio must be considered.
786 static void copy_data(int frombio, struct bio *bio,
787 struct page *page,
788 sector_t sector)
790 char *pa = page_address(page);
791 struct bio_vec *bvl;
792 int i;
793 int page_offset;
795 if (bio->bi_sector >= sector)
796 page_offset = (signed)(bio->bi_sector - sector) * 512;
797 else
798 page_offset = (signed)(sector - bio->bi_sector) * -512;
799 bio_for_each_segment(bvl, bio, i) {
800 int len = bio_iovec_idx(bio,i)->bv_len;
801 int clen;
802 int b_offset = 0;
804 if (page_offset < 0) {
805 b_offset = -page_offset;
806 page_offset += b_offset;
807 len -= b_offset;
810 if (len > 0 && page_offset + len > STRIPE_SIZE)
811 clen = STRIPE_SIZE - page_offset;
812 else clen = len;
814 if (clen > 0) {
815 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
816 if (frombio)
817 memcpy(pa+page_offset, ba+b_offset, clen);
818 else
819 memcpy(ba+b_offset, pa+page_offset, clen);
820 __bio_kunmap_atomic(ba, KM_USER0);
822 if (clen < len) /* hit end of page */
823 break;
824 page_offset += len;
828 #define check_xor() do { \
829 if (count == MAX_XOR_BLOCKS) { \
830 xor_block(count, STRIPE_SIZE, ptr); \
831 count = 1; \
833 } while(0)
836 static void compute_block(struct stripe_head *sh, int dd_idx)
838 int i, count, disks = sh->disks;
839 void *ptr[MAX_XOR_BLOCKS], *p;
841 PRINTK("compute_block, stripe %llu, idx %d\n",
842 (unsigned long long)sh->sector, dd_idx);
844 ptr[0] = page_address(sh->dev[dd_idx].page);
845 memset(ptr[0], 0, STRIPE_SIZE);
846 count = 1;
847 for (i = disks ; i--; ) {
848 if (i == dd_idx)
849 continue;
850 p = page_address(sh->dev[i].page);
851 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
852 ptr[count++] = p;
853 else
854 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
855 " not present\n", dd_idx,
856 (unsigned long long)sh->sector, i);
858 check_xor();
860 if (count != 1)
861 xor_block(count, STRIPE_SIZE, ptr);
862 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
865 static void compute_parity(struct stripe_head *sh, int method)
867 raid5_conf_t *conf = sh->raid_conf;
868 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
869 void *ptr[MAX_XOR_BLOCKS];
870 struct bio *chosen;
872 PRINTK("compute_parity, stripe %llu, method %d\n",
873 (unsigned long long)sh->sector, method);
875 count = 1;
876 ptr[0] = page_address(sh->dev[pd_idx].page);
877 switch(method) {
878 case READ_MODIFY_WRITE:
879 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
880 for (i=disks ; i-- ;) {
881 if (i==pd_idx)
882 continue;
883 if (sh->dev[i].towrite &&
884 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
885 ptr[count++] = page_address(sh->dev[i].page);
886 chosen = sh->dev[i].towrite;
887 sh->dev[i].towrite = NULL;
889 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
890 wake_up(&conf->wait_for_overlap);
892 BUG_ON(sh->dev[i].written);
893 sh->dev[i].written = chosen;
894 check_xor();
897 break;
898 case RECONSTRUCT_WRITE:
899 memset(ptr[0], 0, STRIPE_SIZE);
900 for (i= disks; i-- ;)
901 if (i!=pd_idx && sh->dev[i].towrite) {
902 chosen = sh->dev[i].towrite;
903 sh->dev[i].towrite = NULL;
905 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
906 wake_up(&conf->wait_for_overlap);
908 BUG_ON(sh->dev[i].written);
909 sh->dev[i].written = chosen;
911 break;
912 case CHECK_PARITY:
913 break;
915 if (count>1) {
916 xor_block(count, STRIPE_SIZE, ptr);
917 count = 1;
920 for (i = disks; i--;)
921 if (sh->dev[i].written) {
922 sector_t sector = sh->dev[i].sector;
923 struct bio *wbi = sh->dev[i].written;
924 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
925 copy_data(1, wbi, sh->dev[i].page, sector);
926 wbi = r5_next_bio(wbi, sector);
929 set_bit(R5_LOCKED, &sh->dev[i].flags);
930 set_bit(R5_UPTODATE, &sh->dev[i].flags);
933 switch(method) {
934 case RECONSTRUCT_WRITE:
935 case CHECK_PARITY:
936 for (i=disks; i--;)
937 if (i != pd_idx) {
938 ptr[count++] = page_address(sh->dev[i].page);
939 check_xor();
941 break;
942 case READ_MODIFY_WRITE:
943 for (i = disks; i--;)
944 if (sh->dev[i].written) {
945 ptr[count++] = page_address(sh->dev[i].page);
946 check_xor();
949 if (count != 1)
950 xor_block(count, STRIPE_SIZE, ptr);
952 if (method != CHECK_PARITY) {
953 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
954 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
955 } else
956 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
960 * Each stripe/dev can have one or more bion attached.
961 * toread/towrite point to the first in a chain.
962 * The bi_next chain must be in order.
964 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
966 struct bio **bip;
967 raid5_conf_t *conf = sh->raid_conf;
968 int firstwrite=0;
970 PRINTK("adding bh b#%llu to stripe s#%llu\n",
971 (unsigned long long)bi->bi_sector,
972 (unsigned long long)sh->sector);
975 spin_lock(&sh->lock);
976 spin_lock_irq(&conf->device_lock);
977 if (forwrite) {
978 bip = &sh->dev[dd_idx].towrite;
979 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
980 firstwrite = 1;
981 } else
982 bip = &sh->dev[dd_idx].toread;
983 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
984 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
985 goto overlap;
986 bip = & (*bip)->bi_next;
988 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
989 goto overlap;
991 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
992 if (*bip)
993 bi->bi_next = *bip;
994 *bip = bi;
995 bi->bi_phys_segments ++;
996 spin_unlock_irq(&conf->device_lock);
997 spin_unlock(&sh->lock);
999 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1000 (unsigned long long)bi->bi_sector,
1001 (unsigned long long)sh->sector, dd_idx);
1003 if (conf->mddev->bitmap && firstwrite) {
1004 sh->bm_seq = conf->seq_write;
1005 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1006 STRIPE_SECTORS, 0);
1007 set_bit(STRIPE_BIT_DELAY, &sh->state);
1010 if (forwrite) {
1011 /* check if page is covered */
1012 sector_t sector = sh->dev[dd_idx].sector;
1013 for (bi=sh->dev[dd_idx].towrite;
1014 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1015 bi && bi->bi_sector <= sector;
1016 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1017 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1018 sector = bi->bi_sector + (bi->bi_size>>9);
1020 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1021 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1023 return 1;
1025 overlap:
1026 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1027 spin_unlock_irq(&conf->device_lock);
1028 spin_unlock(&sh->lock);
1029 return 0;
1032 static void end_reshape(raid5_conf_t *conf);
1034 static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1036 int sectors_per_chunk = conf->chunk_size >> 9;
1037 sector_t x = stripe;
1038 int pd_idx, dd_idx;
1039 int chunk_offset = sector_div(x, sectors_per_chunk);
1040 stripe = x;
1041 raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
1042 + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
1043 return pd_idx;
1048 * handle_stripe - do things to a stripe.
1050 * We lock the stripe and then examine the state of various bits
1051 * to see what needs to be done.
1052 * Possible results:
1053 * return some read request which now have data
1054 * return some write requests which are safely on disc
1055 * schedule a read on some buffers
1056 * schedule a write of some buffers
1057 * return confirmation of parity correctness
1059 * Parity calculations are done inside the stripe lock
1060 * buffers are taken off read_list or write_list, and bh_cache buffers
1061 * get BH_Lock set before the stripe lock is released.
1065 static void handle_stripe(struct stripe_head *sh)
1067 raid5_conf_t *conf = sh->raid_conf;
1068 int disks = sh->disks;
1069 struct bio *return_bi= NULL;
1070 struct bio *bi;
1071 int i;
1072 int syncing, expanding, expanded;
1073 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1074 int non_overwrite = 0;
1075 int failed_num=0;
1076 struct r5dev *dev;
1078 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1079 (unsigned long long)sh->sector, atomic_read(&sh->count),
1080 sh->pd_idx);
1082 spin_lock(&sh->lock);
1083 clear_bit(STRIPE_HANDLE, &sh->state);
1084 clear_bit(STRIPE_DELAYED, &sh->state);
1086 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1087 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1088 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
1089 /* Now to look around and see what can be done */
1091 rcu_read_lock();
1092 for (i=disks; i--; ) {
1093 mdk_rdev_t *rdev;
1094 dev = &sh->dev[i];
1095 clear_bit(R5_Insync, &dev->flags);
1097 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1098 i, dev->flags, dev->toread, dev->towrite, dev->written);
1099 /* maybe we can reply to a read */
1100 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1101 struct bio *rbi, *rbi2;
1102 PRINTK("Return read for disc %d\n", i);
1103 spin_lock_irq(&conf->device_lock);
1104 rbi = dev->toread;
1105 dev->toread = NULL;
1106 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1107 wake_up(&conf->wait_for_overlap);
1108 spin_unlock_irq(&conf->device_lock);
1109 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1110 copy_data(0, rbi, dev->page, dev->sector);
1111 rbi2 = r5_next_bio(rbi, dev->sector);
1112 spin_lock_irq(&conf->device_lock);
1113 if (--rbi->bi_phys_segments == 0) {
1114 rbi->bi_next = return_bi;
1115 return_bi = rbi;
1117 spin_unlock_irq(&conf->device_lock);
1118 rbi = rbi2;
1122 /* now count some things */
1123 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1124 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1127 if (dev->toread) to_read++;
1128 if (dev->towrite) {
1129 to_write++;
1130 if (!test_bit(R5_OVERWRITE, &dev->flags))
1131 non_overwrite++;
1133 if (dev->written) written++;
1134 rdev = rcu_dereference(conf->disks[i].rdev);
1135 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1136 /* The ReadError flag will just be confusing now */
1137 clear_bit(R5_ReadError, &dev->flags);
1138 clear_bit(R5_ReWrite, &dev->flags);
1140 if (!rdev || !test_bit(In_sync, &rdev->flags)
1141 || test_bit(R5_ReadError, &dev->flags)) {
1142 failed++;
1143 failed_num = i;
1144 } else
1145 set_bit(R5_Insync, &dev->flags);
1147 rcu_read_unlock();
1148 PRINTK("locked=%d uptodate=%d to_read=%d"
1149 " to_write=%d failed=%d failed_num=%d\n",
1150 locked, uptodate, to_read, to_write, failed, failed_num);
1151 /* check if the array has lost two devices and, if so, some requests might
1152 * need to be failed
1154 if (failed > 1 && to_read+to_write+written) {
1155 for (i=disks; i--; ) {
1156 int bitmap_end = 0;
1158 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1159 mdk_rdev_t *rdev;
1160 rcu_read_lock();
1161 rdev = rcu_dereference(conf->disks[i].rdev);
1162 if (rdev && test_bit(In_sync, &rdev->flags))
1163 /* multiple read failures in one stripe */
1164 md_error(conf->mddev, rdev);
1165 rcu_read_unlock();
1168 spin_lock_irq(&conf->device_lock);
1169 /* fail all writes first */
1170 bi = sh->dev[i].towrite;
1171 sh->dev[i].towrite = NULL;
1172 if (bi) { to_write--; bitmap_end = 1; }
1174 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1175 wake_up(&conf->wait_for_overlap);
1177 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1178 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1179 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1180 if (--bi->bi_phys_segments == 0) {
1181 md_write_end(conf->mddev);
1182 bi->bi_next = return_bi;
1183 return_bi = bi;
1185 bi = nextbi;
1187 /* and fail all 'written' */
1188 bi = sh->dev[i].written;
1189 sh->dev[i].written = NULL;
1190 if (bi) bitmap_end = 1;
1191 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1192 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1193 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1194 if (--bi->bi_phys_segments == 0) {
1195 md_write_end(conf->mddev);
1196 bi->bi_next = return_bi;
1197 return_bi = bi;
1199 bi = bi2;
1202 /* fail any reads if this device is non-operational */
1203 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1204 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1205 bi = sh->dev[i].toread;
1206 sh->dev[i].toread = NULL;
1207 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1208 wake_up(&conf->wait_for_overlap);
1209 if (bi) to_read--;
1210 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1211 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1212 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1213 if (--bi->bi_phys_segments == 0) {
1214 bi->bi_next = return_bi;
1215 return_bi = bi;
1217 bi = nextbi;
1220 spin_unlock_irq(&conf->device_lock);
1221 if (bitmap_end)
1222 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1223 STRIPE_SECTORS, 0, 0);
1226 if (failed > 1 && syncing) {
1227 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1228 clear_bit(STRIPE_SYNCING, &sh->state);
1229 syncing = 0;
1232 /* might be able to return some write requests if the parity block
1233 * is safe, or on a failed drive
1235 dev = &sh->dev[sh->pd_idx];
1236 if ( written &&
1237 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1238 test_bit(R5_UPTODATE, &dev->flags))
1239 || (failed == 1 && failed_num == sh->pd_idx))
1241 /* any written block on an uptodate or failed drive can be returned.
1242 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1243 * never LOCKED, so we don't need to test 'failed' directly.
1245 for (i=disks; i--; )
1246 if (sh->dev[i].written) {
1247 dev = &sh->dev[i];
1248 if (!test_bit(R5_LOCKED, &dev->flags) &&
1249 test_bit(R5_UPTODATE, &dev->flags) ) {
1250 /* We can return any write requests */
1251 struct bio *wbi, *wbi2;
1252 int bitmap_end = 0;
1253 PRINTK("Return write for disc %d\n", i);
1254 spin_lock_irq(&conf->device_lock);
1255 wbi = dev->written;
1256 dev->written = NULL;
1257 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1258 wbi2 = r5_next_bio(wbi, dev->sector);
1259 if (--wbi->bi_phys_segments == 0) {
1260 md_write_end(conf->mddev);
1261 wbi->bi_next = return_bi;
1262 return_bi = wbi;
1264 wbi = wbi2;
1266 if (dev->towrite == NULL)
1267 bitmap_end = 1;
1268 spin_unlock_irq(&conf->device_lock);
1269 if (bitmap_end)
1270 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1271 STRIPE_SECTORS,
1272 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1277 /* Now we might consider reading some blocks, either to check/generate
1278 * parity, or to satisfy requests
1279 * or to load a block that is being partially written.
1281 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
1282 for (i=disks; i--;) {
1283 dev = &sh->dev[i];
1284 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1285 (dev->toread ||
1286 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1287 syncing ||
1288 expanding ||
1289 (failed && (sh->dev[failed_num].toread ||
1290 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1293 /* we would like to get this block, possibly
1294 * by computing it, but we might not be able to
1296 if (uptodate == disks-1) {
1297 PRINTK("Computing block %d\n", i);
1298 compute_block(sh, i);
1299 uptodate++;
1300 } else if (test_bit(R5_Insync, &dev->flags)) {
1301 set_bit(R5_LOCKED, &dev->flags);
1302 set_bit(R5_Wantread, &dev->flags);
1303 #if 0
1304 /* if I am just reading this block and we don't have
1305 a failed drive, or any pending writes then sidestep the cache */
1306 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1307 ! syncing && !failed && !to_write) {
1308 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1309 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1311 #endif
1312 locked++;
1313 PRINTK("Reading block %d (sync=%d)\n",
1314 i, syncing);
1318 set_bit(STRIPE_HANDLE, &sh->state);
1321 /* now to consider writing and what else, if anything should be read */
1322 if (to_write) {
1323 int rmw=0, rcw=0;
1324 for (i=disks ; i--;) {
1325 /* would I have to read this buffer for read_modify_write */
1326 dev = &sh->dev[i];
1327 if ((dev->towrite || i == sh->pd_idx) &&
1328 (!test_bit(R5_LOCKED, &dev->flags)
1329 #if 0
1330 || sh->bh_page[i]!=bh->b_page
1331 #endif
1332 ) &&
1333 !test_bit(R5_UPTODATE, &dev->flags)) {
1334 if (test_bit(R5_Insync, &dev->flags)
1335 /* && !(!mddev->insync && i == sh->pd_idx) */
1337 rmw++;
1338 else rmw += 2*disks; /* cannot read it */
1340 /* Would I have to read this buffer for reconstruct_write */
1341 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1342 (!test_bit(R5_LOCKED, &dev->flags)
1343 #if 0
1344 || sh->bh_page[i] != bh->b_page
1345 #endif
1346 ) &&
1347 !test_bit(R5_UPTODATE, &dev->flags)) {
1348 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1349 else rcw += 2*disks;
1352 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1353 (unsigned long long)sh->sector, rmw, rcw);
1354 set_bit(STRIPE_HANDLE, &sh->state);
1355 if (rmw < rcw && rmw > 0)
1356 /* prefer read-modify-write, but need to get some data */
1357 for (i=disks; i--;) {
1358 dev = &sh->dev[i];
1359 if ((dev->towrite || i == sh->pd_idx) &&
1360 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1361 test_bit(R5_Insync, &dev->flags)) {
1362 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1364 PRINTK("Read_old block %d for r-m-w\n", i);
1365 set_bit(R5_LOCKED, &dev->flags);
1366 set_bit(R5_Wantread, &dev->flags);
1367 locked++;
1368 } else {
1369 set_bit(STRIPE_DELAYED, &sh->state);
1370 set_bit(STRIPE_HANDLE, &sh->state);
1374 if (rcw <= rmw && rcw > 0)
1375 /* want reconstruct write, but need to get some data */
1376 for (i=disks; i--;) {
1377 dev = &sh->dev[i];
1378 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1379 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1380 test_bit(R5_Insync, &dev->flags)) {
1381 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1383 PRINTK("Read_old block %d for Reconstruct\n", i);
1384 set_bit(R5_LOCKED, &dev->flags);
1385 set_bit(R5_Wantread, &dev->flags);
1386 locked++;
1387 } else {
1388 set_bit(STRIPE_DELAYED, &sh->state);
1389 set_bit(STRIPE_HANDLE, &sh->state);
1393 /* now if nothing is locked, and if we have enough data, we can start a write request */
1394 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1395 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1396 PRINTK("Computing parity...\n");
1397 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1398 /* now every locked buffer is ready to be written */
1399 for (i=disks; i--;)
1400 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1401 PRINTK("Writing block %d\n", i);
1402 locked++;
1403 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1404 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1405 || (i==sh->pd_idx && failed == 0))
1406 set_bit(STRIPE_INSYNC, &sh->state);
1408 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1409 atomic_dec(&conf->preread_active_stripes);
1410 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1411 md_wakeup_thread(conf->mddev->thread);
1416 /* maybe we need to check and possibly fix the parity for this stripe
1417 * Any reads will already have been scheduled, so we just see if enough data
1418 * is available
1420 if (syncing && locked == 0 &&
1421 !test_bit(STRIPE_INSYNC, &sh->state)) {
1422 set_bit(STRIPE_HANDLE, &sh->state);
1423 if (failed == 0) {
1424 char *pagea;
1425 BUG_ON(uptodate != disks);
1426 compute_parity(sh, CHECK_PARITY);
1427 uptodate--;
1428 pagea = page_address(sh->dev[sh->pd_idx].page);
1429 if ((*(u32*)pagea) == 0 &&
1430 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1431 /* parity is correct (on disc, not in buffer any more) */
1432 set_bit(STRIPE_INSYNC, &sh->state);
1433 } else {
1434 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1435 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1436 /* don't try to repair!! */
1437 set_bit(STRIPE_INSYNC, &sh->state);
1438 else {
1439 compute_block(sh, sh->pd_idx);
1440 uptodate++;
1444 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1445 /* either failed parity check, or recovery is happening */
1446 if (failed==0)
1447 failed_num = sh->pd_idx;
1448 dev = &sh->dev[failed_num];
1449 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1450 BUG_ON(uptodate != disks);
1452 set_bit(R5_LOCKED, &dev->flags);
1453 set_bit(R5_Wantwrite, &dev->flags);
1454 clear_bit(STRIPE_DEGRADED, &sh->state);
1455 locked++;
1456 set_bit(STRIPE_INSYNC, &sh->state);
1459 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1460 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1461 clear_bit(STRIPE_SYNCING, &sh->state);
1464 /* If the failed drive is just a ReadError, then we might need to progress
1465 * the repair/check process
1467 if (failed == 1 && ! conf->mddev->ro &&
1468 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
1469 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1470 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1472 dev = &sh->dev[failed_num];
1473 if (!test_bit(R5_ReWrite, &dev->flags)) {
1474 set_bit(R5_Wantwrite, &dev->flags);
1475 set_bit(R5_ReWrite, &dev->flags);
1476 set_bit(R5_LOCKED, &dev->flags);
1477 locked++;
1478 } else {
1479 /* let's read it back */
1480 set_bit(R5_Wantread, &dev->flags);
1481 set_bit(R5_LOCKED, &dev->flags);
1482 locked++;
1486 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1487 /* Need to write out all blocks after computing parity */
1488 sh->disks = conf->raid_disks;
1489 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
1490 compute_parity(sh, RECONSTRUCT_WRITE);
1491 for (i= conf->raid_disks; i--;) {
1492 set_bit(R5_LOCKED, &sh->dev[i].flags);
1493 locked++;
1494 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1496 clear_bit(STRIPE_EXPANDING, &sh->state);
1497 } else if (expanded) {
1498 clear_bit(STRIPE_EXPAND_READY, &sh->state);
1499 atomic_dec(&conf->reshape_stripes);
1500 wake_up(&conf->wait_for_overlap);
1501 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1504 if (expanding && locked == 0) {
1505 /* We have read all the blocks in this stripe and now we need to
1506 * copy some of them into a target stripe for expand.
1508 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1509 for (i=0; i< sh->disks; i++)
1510 if (i != sh->pd_idx) {
1511 int dd_idx, pd_idx, j;
1512 struct stripe_head *sh2;
1514 sector_t bn = compute_blocknr(sh, i);
1515 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1516 conf->raid_disks-1,
1517 &dd_idx, &pd_idx, conf);
1518 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1519 if (sh2 == NULL)
1520 /* so far only the early blocks of this stripe
1521 * have been requested. When later blocks
1522 * get requested, we will try again
1524 continue;
1525 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1526 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1527 /* must have already done this block */
1528 release_stripe(sh2);
1529 continue;
1531 memcpy(page_address(sh2->dev[dd_idx].page),
1532 page_address(sh->dev[i].page),
1533 STRIPE_SIZE);
1534 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1535 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1536 for (j=0; j<conf->raid_disks; j++)
1537 if (j != sh2->pd_idx &&
1538 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1539 break;
1540 if (j == conf->raid_disks) {
1541 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1542 set_bit(STRIPE_HANDLE, &sh2->state);
1544 release_stripe(sh2);
1548 spin_unlock(&sh->lock);
1550 while ((bi=return_bi)) {
1551 int bytes = bi->bi_size;
1553 return_bi = bi->bi_next;
1554 bi->bi_next = NULL;
1555 bi->bi_size = 0;
1556 bi->bi_end_io(bi, bytes, 0);
1558 for (i=disks; i-- ;) {
1559 int rw;
1560 struct bio *bi;
1561 mdk_rdev_t *rdev;
1562 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1563 rw = 1;
1564 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1565 rw = 0;
1566 else
1567 continue;
1569 bi = &sh->dev[i].req;
1571 bi->bi_rw = rw;
1572 if (rw)
1573 bi->bi_end_io = raid5_end_write_request;
1574 else
1575 bi->bi_end_io = raid5_end_read_request;
1577 rcu_read_lock();
1578 rdev = rcu_dereference(conf->disks[i].rdev);
1579 if (rdev && test_bit(Faulty, &rdev->flags))
1580 rdev = NULL;
1581 if (rdev)
1582 atomic_inc(&rdev->nr_pending);
1583 rcu_read_unlock();
1585 if (rdev) {
1586 if (syncing || expanding || expanded)
1587 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1589 bi->bi_bdev = rdev->bdev;
1590 PRINTK("for %llu schedule op %ld on disc %d\n",
1591 (unsigned long long)sh->sector, bi->bi_rw, i);
1592 atomic_inc(&sh->count);
1593 bi->bi_sector = sh->sector + rdev->data_offset;
1594 bi->bi_flags = 1 << BIO_UPTODATE;
1595 bi->bi_vcnt = 1;
1596 bi->bi_max_vecs = 1;
1597 bi->bi_idx = 0;
1598 bi->bi_io_vec = &sh->dev[i].vec;
1599 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1600 bi->bi_io_vec[0].bv_offset = 0;
1601 bi->bi_size = STRIPE_SIZE;
1602 bi->bi_next = NULL;
1603 if (rw == WRITE &&
1604 test_bit(R5_ReWrite, &sh->dev[i].flags))
1605 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1606 generic_make_request(bi);
1607 } else {
1608 if (rw == 1)
1609 set_bit(STRIPE_DEGRADED, &sh->state);
1610 PRINTK("skip op %ld on disc %d for sector %llu\n",
1611 bi->bi_rw, i, (unsigned long long)sh->sector);
1612 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1613 set_bit(STRIPE_HANDLE, &sh->state);
1618 static void raid5_activate_delayed(raid5_conf_t *conf)
1620 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1621 while (!list_empty(&conf->delayed_list)) {
1622 struct list_head *l = conf->delayed_list.next;
1623 struct stripe_head *sh;
1624 sh = list_entry(l, struct stripe_head, lru);
1625 list_del_init(l);
1626 clear_bit(STRIPE_DELAYED, &sh->state);
1627 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1628 atomic_inc(&conf->preread_active_stripes);
1629 list_add_tail(&sh->lru, &conf->handle_list);
1634 static void activate_bit_delay(raid5_conf_t *conf)
1636 /* device_lock is held */
1637 struct list_head head;
1638 list_add(&head, &conf->bitmap_list);
1639 list_del_init(&conf->bitmap_list);
1640 while (!list_empty(&head)) {
1641 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1642 list_del_init(&sh->lru);
1643 atomic_inc(&sh->count);
1644 __release_stripe(conf, sh);
1648 static void unplug_slaves(mddev_t *mddev)
1650 raid5_conf_t *conf = mddev_to_conf(mddev);
1651 int i;
1653 rcu_read_lock();
1654 for (i=0; i<mddev->raid_disks; i++) {
1655 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1656 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
1657 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1659 atomic_inc(&rdev->nr_pending);
1660 rcu_read_unlock();
1662 if (r_queue->unplug_fn)
1663 r_queue->unplug_fn(r_queue);
1665 rdev_dec_pending(rdev, mddev);
1666 rcu_read_lock();
1669 rcu_read_unlock();
1672 static void raid5_unplug_device(request_queue_t *q)
1674 mddev_t *mddev = q->queuedata;
1675 raid5_conf_t *conf = mddev_to_conf(mddev);
1676 unsigned long flags;
1678 spin_lock_irqsave(&conf->device_lock, flags);
1680 if (blk_remove_plug(q)) {
1681 conf->seq_flush++;
1682 raid5_activate_delayed(conf);
1684 md_wakeup_thread(mddev->thread);
1686 spin_unlock_irqrestore(&conf->device_lock, flags);
1688 unplug_slaves(mddev);
1691 static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
1692 sector_t *error_sector)
1694 mddev_t *mddev = q->queuedata;
1695 raid5_conf_t *conf = mddev_to_conf(mddev);
1696 int i, ret = 0;
1698 rcu_read_lock();
1699 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1700 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1701 if (rdev && !test_bit(Faulty, &rdev->flags)) {
1702 struct block_device *bdev = rdev->bdev;
1703 request_queue_t *r_queue = bdev_get_queue(bdev);
1705 if (!r_queue->issue_flush_fn)
1706 ret = -EOPNOTSUPP;
1707 else {
1708 atomic_inc(&rdev->nr_pending);
1709 rcu_read_unlock();
1710 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1711 error_sector);
1712 rdev_dec_pending(rdev, mddev);
1713 rcu_read_lock();
1717 rcu_read_unlock();
1718 return ret;
1721 static inline void raid5_plug_device(raid5_conf_t *conf)
1723 spin_lock_irq(&conf->device_lock);
1724 blk_plug_device(conf->mddev->queue);
1725 spin_unlock_irq(&conf->device_lock);
1728 static int make_request(request_queue_t *q, struct bio * bi)
1730 mddev_t *mddev = q->queuedata;
1731 raid5_conf_t *conf = mddev_to_conf(mddev);
1732 unsigned int dd_idx, pd_idx;
1733 sector_t new_sector;
1734 sector_t logical_sector, last_sector;
1735 struct stripe_head *sh;
1736 const int rw = bio_data_dir(bi);
1737 int remaining;
1739 if (unlikely(bio_barrier(bi))) {
1740 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1741 return 0;
1744 md_write_start(mddev, bi);
1746 disk_stat_inc(mddev->gendisk, ios[rw]);
1747 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1749 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1750 last_sector = bi->bi_sector + (bi->bi_size>>9);
1751 bi->bi_next = NULL;
1752 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1754 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1755 DEFINE_WAIT(w);
1756 int disks;
1758 retry:
1759 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1760 if (likely(conf->expand_progress == MaxSector))
1761 disks = conf->raid_disks;
1762 else {
1763 /* spinlock is needed as expand_progress may be
1764 * 64bit on a 32bit platform, and so it might be
1765 * possible to see a half-updated value
1766 * Ofcourse expand_progress could change after
1767 * the lock is dropped, so once we get a reference
1768 * to the stripe that we think it is, we will have
1769 * to check again.
1771 spin_lock_irq(&conf->device_lock);
1772 disks = conf->raid_disks;
1773 if (logical_sector >= conf->expand_progress)
1774 disks = conf->previous_raid_disks;
1775 else {
1776 if (logical_sector >= conf->expand_lo) {
1777 spin_unlock_irq(&conf->device_lock);
1778 schedule();
1779 goto retry;
1782 spin_unlock_irq(&conf->device_lock);
1784 new_sector = raid5_compute_sector(logical_sector, disks, disks - 1,
1785 &dd_idx, &pd_idx, conf);
1786 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1787 (unsigned long long)new_sector,
1788 (unsigned long long)logical_sector);
1790 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
1791 if (sh) {
1792 if (unlikely(conf->expand_progress != MaxSector)) {
1793 /* expansion might have moved on while waiting for a
1794 * stripe, so we must do the range check again.
1795 * Expansion could still move past after this
1796 * test, but as we are holding a reference to
1797 * 'sh', we know that if that happens,
1798 * STRIPE_EXPANDING will get set and the expansion
1799 * won't proceed until we finish with the stripe.
1801 int must_retry = 0;
1802 spin_lock_irq(&conf->device_lock);
1803 if (logical_sector < conf->expand_progress &&
1804 disks == conf->previous_raid_disks)
1805 /* mismatch, need to try again */
1806 must_retry = 1;
1807 spin_unlock_irq(&conf->device_lock);
1808 if (must_retry) {
1809 release_stripe(sh);
1810 goto retry;
1813 /* FIXME what if we get a false positive because these
1814 * are being updated.
1816 if (logical_sector >= mddev->suspend_lo &&
1817 logical_sector < mddev->suspend_hi) {
1818 release_stripe(sh);
1819 schedule();
1820 goto retry;
1823 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
1824 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1825 /* Stripe is busy expanding or
1826 * add failed due to overlap. Flush everything
1827 * and wait a while
1829 raid5_unplug_device(mddev->queue);
1830 release_stripe(sh);
1831 schedule();
1832 goto retry;
1834 finish_wait(&conf->wait_for_overlap, &w);
1835 raid5_plug_device(conf);
1836 handle_stripe(sh);
1837 release_stripe(sh);
1838 } else {
1839 /* cannot get stripe for read-ahead, just give-up */
1840 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1841 finish_wait(&conf->wait_for_overlap, &w);
1842 break;
1846 spin_lock_irq(&conf->device_lock);
1847 remaining = --bi->bi_phys_segments;
1848 spin_unlock_irq(&conf->device_lock);
1849 if (remaining == 0) {
1850 int bytes = bi->bi_size;
1852 if ( bio_data_dir(bi) == WRITE )
1853 md_write_end(mddev);
1854 bi->bi_size = 0;
1855 bi->bi_end_io(bi, bytes, 0);
1857 return 0;
1860 /* FIXME go_faster isn't used */
1861 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1863 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1864 struct stripe_head *sh;
1865 int pd_idx;
1866 sector_t first_sector, last_sector;
1867 int raid_disks = conf->raid_disks;
1868 int data_disks = raid_disks-1;
1869 sector_t max_sector = mddev->size << 1;
1870 int sync_blocks;
1872 if (sector_nr >= max_sector) {
1873 /* just being told to finish up .. nothing much to do */
1874 unplug_slaves(mddev);
1875 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
1876 end_reshape(conf);
1877 return 0;
1880 if (mddev->curr_resync < max_sector) /* aborted */
1881 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1882 &sync_blocks, 1);
1883 else /* compelted sync */
1884 conf->fullsync = 0;
1885 bitmap_close_sync(mddev->bitmap);
1887 return 0;
1890 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
1891 /* reshaping is quite different to recovery/resync so it is
1892 * handled quite separately ... here.
1894 * On each call to sync_request, we gather one chunk worth of
1895 * destination stripes and flag them as expanding.
1896 * Then we find all the source stripes and request reads.
1897 * As the reads complete, handle_stripe will copy the data
1898 * into the destination stripe and release that stripe.
1900 int i;
1901 int dd_idx;
1902 sector_t writepos, safepos, gap;
1904 if (sector_nr == 0 &&
1905 conf->expand_progress != 0) {
1906 /* restarting in the middle, skip the initial sectors */
1907 sector_nr = conf->expand_progress;
1908 sector_div(sector_nr, conf->raid_disks-1);
1909 *skipped = 1;
1910 return sector_nr;
1913 /* we update the metadata when there is more than 3Meg
1914 * in the block range (that is rather arbitrary, should
1915 * probably be time based) or when the data about to be
1916 * copied would over-write the source of the data at
1917 * the front of the range.
1918 * i.e. one new_stripe forward from expand_progress new_maps
1919 * to after where expand_lo old_maps to
1921 writepos = conf->expand_progress +
1922 conf->chunk_size/512*(conf->raid_disks-1);
1923 sector_div(writepos, conf->raid_disks-1);
1924 safepos = conf->expand_lo;
1925 sector_div(safepos, conf->previous_raid_disks-1);
1926 gap = conf->expand_progress - conf->expand_lo;
1928 if (writepos >= safepos ||
1929 gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
1930 /* Cannot proceed until we've updated the superblock... */
1931 wait_event(conf->wait_for_overlap,
1932 atomic_read(&conf->reshape_stripes)==0);
1933 mddev->reshape_position = conf->expand_progress;
1934 mddev->sb_dirty = 1;
1935 md_wakeup_thread(mddev->thread);
1936 wait_event(mddev->sb_wait, mddev->sb_dirty == 0 ||
1937 kthread_should_stop());
1938 spin_lock_irq(&conf->device_lock);
1939 conf->expand_lo = mddev->reshape_position;
1940 spin_unlock_irq(&conf->device_lock);
1941 wake_up(&conf->wait_for_overlap);
1944 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
1945 int j;
1946 int skipped = 0;
1947 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
1948 sh = get_active_stripe(conf, sector_nr+i,
1949 conf->raid_disks, pd_idx, 0);
1950 set_bit(STRIPE_EXPANDING, &sh->state);
1951 atomic_inc(&conf->reshape_stripes);
1952 /* If any of this stripe is beyond the end of the old
1953 * array, then we need to zero those blocks
1955 for (j=sh->disks; j--;) {
1956 sector_t s;
1957 if (j == sh->pd_idx)
1958 continue;
1959 s = compute_blocknr(sh, j);
1960 if (s < (mddev->array_size<<1)) {
1961 skipped = 1;
1962 continue;
1964 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
1965 set_bit(R5_Expanded, &sh->dev[j].flags);
1966 set_bit(R5_UPTODATE, &sh->dev[j].flags);
1968 if (!skipped) {
1969 set_bit(STRIPE_EXPAND_READY, &sh->state);
1970 set_bit(STRIPE_HANDLE, &sh->state);
1972 release_stripe(sh);
1974 spin_lock_irq(&conf->device_lock);
1975 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
1976 spin_unlock_irq(&conf->device_lock);
1977 /* Ok, those stripe are ready. We can start scheduling
1978 * reads on the source stripes.
1979 * The source stripes are determined by mapping the first and last
1980 * block on the destination stripes.
1982 raid_disks = conf->previous_raid_disks;
1983 data_disks = raid_disks - 1;
1984 first_sector =
1985 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
1986 raid_disks, data_disks,
1987 &dd_idx, &pd_idx, conf);
1988 last_sector =
1989 raid5_compute_sector((sector_nr+conf->chunk_size/512)
1990 *(conf->raid_disks-1) -1,
1991 raid_disks, data_disks,
1992 &dd_idx, &pd_idx, conf);
1993 if (last_sector >= (mddev->size<<1))
1994 last_sector = (mddev->size<<1)-1;
1995 while (first_sector <= last_sector) {
1996 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
1997 sh = get_active_stripe(conf, first_sector,
1998 conf->previous_raid_disks, pd_idx, 0);
1999 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2000 set_bit(STRIPE_HANDLE, &sh->state);
2001 release_stripe(sh);
2002 first_sector += STRIPE_SECTORS;
2004 return conf->chunk_size>>9;
2006 /* if there is 1 or more failed drives and we are trying
2007 * to resync, then assert that we are finished, because there is
2008 * nothing we can do.
2010 if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2011 sector_t rv = (mddev->size << 1) - sector_nr;
2012 *skipped = 1;
2013 return rv;
2015 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2016 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2017 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
2018 /* we can skip this block, and probably more */
2019 sync_blocks /= STRIPE_SECTORS;
2020 *skipped = 1;
2021 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
2024 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
2025 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
2026 if (sh == NULL) {
2027 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
2028 /* make sure we don't swamp the stripe cache if someone else
2029 * is trying to get access
2031 schedule_timeout_uninterruptible(1);
2033 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
2034 spin_lock(&sh->lock);
2035 set_bit(STRIPE_SYNCING, &sh->state);
2036 clear_bit(STRIPE_INSYNC, &sh->state);
2037 spin_unlock(&sh->lock);
2039 handle_stripe(sh);
2040 release_stripe(sh);
2042 return STRIPE_SECTORS;
2046 * This is our raid5 kernel thread.
2048 * We scan the hash table for stripes which can be handled now.
2049 * During the scan, completed stripes are saved for us by the interrupt
2050 * handler, so that they will not have to wait for our next wakeup.
2052 static void raid5d (mddev_t *mddev)
2054 struct stripe_head *sh;
2055 raid5_conf_t *conf = mddev_to_conf(mddev);
2056 int handled;
2058 PRINTK("+++ raid5d active\n");
2060 md_check_recovery(mddev);
2062 handled = 0;
2063 spin_lock_irq(&conf->device_lock);
2064 while (1) {
2065 struct list_head *first;
2067 if (conf->seq_flush - conf->seq_write > 0) {
2068 int seq = conf->seq_flush;
2069 spin_unlock_irq(&conf->device_lock);
2070 bitmap_unplug(mddev->bitmap);
2071 spin_lock_irq(&conf->device_lock);
2072 conf->seq_write = seq;
2073 activate_bit_delay(conf);
2076 if (list_empty(&conf->handle_list) &&
2077 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
2078 !blk_queue_plugged(mddev->queue) &&
2079 !list_empty(&conf->delayed_list))
2080 raid5_activate_delayed(conf);
2082 if (list_empty(&conf->handle_list))
2083 break;
2085 first = conf->handle_list.next;
2086 sh = list_entry(first, struct stripe_head, lru);
2088 list_del_init(first);
2089 atomic_inc(&sh->count);
2090 BUG_ON(atomic_read(&sh->count)!= 1);
2091 spin_unlock_irq(&conf->device_lock);
2093 handled++;
2094 handle_stripe(sh);
2095 release_stripe(sh);
2097 spin_lock_irq(&conf->device_lock);
2099 PRINTK("%d stripes handled\n", handled);
2101 spin_unlock_irq(&conf->device_lock);
2103 unplug_slaves(mddev);
2105 PRINTK("--- raid5d inactive\n");
2108 static ssize_t
2109 raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
2111 raid5_conf_t *conf = mddev_to_conf(mddev);
2112 if (conf)
2113 return sprintf(page, "%d\n", conf->max_nr_stripes);
2114 else
2115 return 0;
2118 static ssize_t
2119 raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
2121 raid5_conf_t *conf = mddev_to_conf(mddev);
2122 char *end;
2123 int new;
2124 if (len >= PAGE_SIZE)
2125 return -EINVAL;
2126 if (!conf)
2127 return -ENODEV;
2129 new = simple_strtoul(page, &end, 10);
2130 if (!*page || (*end && *end != '\n') )
2131 return -EINVAL;
2132 if (new <= 16 || new > 32768)
2133 return -EINVAL;
2134 while (new < conf->max_nr_stripes) {
2135 if (drop_one_stripe(conf))
2136 conf->max_nr_stripes--;
2137 else
2138 break;
2140 while (new > conf->max_nr_stripes) {
2141 if (grow_one_stripe(conf))
2142 conf->max_nr_stripes++;
2143 else break;
2145 return len;
2148 static struct md_sysfs_entry
2149 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
2150 raid5_show_stripe_cache_size,
2151 raid5_store_stripe_cache_size);
2153 static ssize_t
2154 stripe_cache_active_show(mddev_t *mddev, char *page)
2156 raid5_conf_t *conf = mddev_to_conf(mddev);
2157 if (conf)
2158 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
2159 else
2160 return 0;
2163 static struct md_sysfs_entry
2164 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
2166 static struct attribute *raid5_attrs[] = {
2167 &raid5_stripecache_size.attr,
2168 &raid5_stripecache_active.attr,
2169 NULL,
2171 static struct attribute_group raid5_attrs_group = {
2172 .name = NULL,
2173 .attrs = raid5_attrs,
2176 static int run(mddev_t *mddev)
2178 raid5_conf_t *conf;
2179 int raid_disk, memory;
2180 mdk_rdev_t *rdev;
2181 struct disk_info *disk;
2182 struct list_head *tmp;
2184 if (mddev->level != 5 && mddev->level != 4) {
2185 printk(KERN_ERR "raid5: %s: raid level not set to 4/5 (%d)\n",
2186 mdname(mddev), mddev->level);
2187 return -EIO;
2190 if (mddev->reshape_position != MaxSector) {
2191 /* Check that we can continue the reshape.
2192 * Currently only disks can change, it must
2193 * increase, and we must be past the point where
2194 * a stripe over-writes itself
2196 sector_t here_new, here_old;
2197 int old_disks;
2199 if (mddev->new_level != mddev->level ||
2200 mddev->new_layout != mddev->layout ||
2201 mddev->new_chunk != mddev->chunk_size) {
2202 printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
2203 mdname(mddev));
2204 return -EINVAL;
2206 if (mddev->delta_disks <= 0) {
2207 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
2208 mdname(mddev));
2209 return -EINVAL;
2211 old_disks = mddev->raid_disks - mddev->delta_disks;
2212 /* reshape_position must be on a new-stripe boundary, and one
2213 * further up in new geometry must map after here in old geometry.
2215 here_new = mddev->reshape_position;
2216 if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
2217 printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
2218 return -EINVAL;
2220 /* here_new is the stripe we will write to */
2221 here_old = mddev->reshape_position;
2222 sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
2223 /* here_old is the first stripe that we might need to read from */
2224 if (here_new >= here_old) {
2225 /* Reading from the same stripe as writing to - bad */
2226 printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
2227 return -EINVAL;
2229 printk(KERN_INFO "raid5: reshape will continue\n");
2230 /* OK, we should be able to continue; */
2234 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
2235 if ((conf = mddev->private) == NULL)
2236 goto abort;
2237 if (mddev->reshape_position == MaxSector) {
2238 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
2239 } else {
2240 conf->raid_disks = mddev->raid_disks;
2241 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
2244 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
2245 GFP_KERNEL);
2246 if (!conf->disks)
2247 goto abort;
2249 conf->mddev = mddev;
2251 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
2252 goto abort;
2254 spin_lock_init(&conf->device_lock);
2255 init_waitqueue_head(&conf->wait_for_stripe);
2256 init_waitqueue_head(&conf->wait_for_overlap);
2257 INIT_LIST_HEAD(&conf->handle_list);
2258 INIT_LIST_HEAD(&conf->delayed_list);
2259 INIT_LIST_HEAD(&conf->bitmap_list);
2260 INIT_LIST_HEAD(&conf->inactive_list);
2261 atomic_set(&conf->active_stripes, 0);
2262 atomic_set(&conf->preread_active_stripes, 0);
2264 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
2266 ITERATE_RDEV(mddev,rdev,tmp) {
2267 raid_disk = rdev->raid_disk;
2268 if (raid_disk >= conf->raid_disks
2269 || raid_disk < 0)
2270 continue;
2271 disk = conf->disks + raid_disk;
2273 disk->rdev = rdev;
2275 if (test_bit(In_sync, &rdev->flags)) {
2276 char b[BDEVNAME_SIZE];
2277 printk(KERN_INFO "raid5: device %s operational as raid"
2278 " disk %d\n", bdevname(rdev->bdev,b),
2279 raid_disk);
2280 conf->working_disks++;
2285 * 0 for a fully functional array, 1 for a degraded array.
2287 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
2288 conf->mddev = mddev;
2289 conf->chunk_size = mddev->chunk_size;
2290 conf->level = mddev->level;
2291 conf->algorithm = mddev->layout;
2292 conf->max_nr_stripes = NR_STRIPES;
2293 conf->expand_progress = mddev->reshape_position;
2295 /* device size must be a multiple of chunk size */
2296 mddev->size &= ~(mddev->chunk_size/1024 -1);
2297 mddev->resync_max_sectors = mddev->size << 1;
2299 if (!conf->chunk_size || conf->chunk_size % 4) {
2300 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
2301 conf->chunk_size, mdname(mddev));
2302 goto abort;
2304 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
2305 printk(KERN_ERR
2306 "raid5: unsupported parity algorithm %d for %s\n",
2307 conf->algorithm, mdname(mddev));
2308 goto abort;
2310 if (mddev->degraded > 1) {
2311 printk(KERN_ERR "raid5: not enough operational devices for %s"
2312 " (%d/%d failed)\n",
2313 mdname(mddev), conf->failed_disks, conf->raid_disks);
2314 goto abort;
2317 if (mddev->degraded == 1 &&
2318 mddev->recovery_cp != MaxSector) {
2319 if (mddev->ok_start_degraded)
2320 printk(KERN_WARNING
2321 "raid5: starting dirty degraded array: %s"
2322 "- data corruption possible.\n",
2323 mdname(mddev));
2324 else {
2325 printk(KERN_ERR
2326 "raid5: cannot start dirty degraded array for %s\n",
2327 mdname(mddev));
2328 goto abort;
2333 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
2334 if (!mddev->thread) {
2335 printk(KERN_ERR
2336 "raid5: couldn't allocate thread for %s\n",
2337 mdname(mddev));
2338 goto abort;
2341 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
2342 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
2343 if (grow_stripes(conf, conf->max_nr_stripes)) {
2344 printk(KERN_ERR
2345 "raid5: couldn't allocate %dkB for buffers\n", memory);
2346 shrink_stripes(conf);
2347 md_unregister_thread(mddev->thread);
2348 goto abort;
2349 } else
2350 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
2351 memory, mdname(mddev));
2353 if (mddev->degraded == 0)
2354 printk("raid5: raid level %d set %s active with %d out of %d"
2355 " devices, algorithm %d\n", conf->level, mdname(mddev),
2356 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
2357 conf->algorithm);
2358 else
2359 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
2360 " out of %d devices, algorithm %d\n", conf->level,
2361 mdname(mddev), mddev->raid_disks - mddev->degraded,
2362 mddev->raid_disks, conf->algorithm);
2364 print_raid5_conf(conf);
2366 if (conf->expand_progress != MaxSector) {
2367 printk("...ok start reshape thread\n");
2368 conf->expand_lo = conf->expand_progress;
2369 atomic_set(&conf->reshape_stripes, 0);
2370 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2371 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2372 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
2373 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
2374 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
2375 "%s_reshape");
2376 /* FIXME if md_register_thread fails?? */
2377 md_wakeup_thread(mddev->sync_thread);
2381 /* read-ahead size must cover two whole stripes, which is
2382 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
2385 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
2386 / PAGE_SIZE;
2387 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
2388 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
2391 /* Ok, everything is just fine now */
2392 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
2394 mddev->queue->unplug_fn = raid5_unplug_device;
2395 mddev->queue->issue_flush_fn = raid5_issue_flush;
2396 mddev->array_size = mddev->size * (conf->previous_raid_disks - 1);
2398 return 0;
2399 abort:
2400 if (conf) {
2401 print_raid5_conf(conf);
2402 kfree(conf->disks);
2403 kfree(conf->stripe_hashtbl);
2404 kfree(conf);
2406 mddev->private = NULL;
2407 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
2408 return -EIO;
2413 static int stop(mddev_t *mddev)
2415 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2417 md_unregister_thread(mddev->thread);
2418 mddev->thread = NULL;
2419 shrink_stripes(conf);
2420 kfree(conf->stripe_hashtbl);
2421 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2422 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
2423 kfree(conf->disks);
2424 kfree(conf);
2425 mddev->private = NULL;
2426 return 0;
2429 #if RAID5_DEBUG
2430 static void print_sh (struct stripe_head *sh)
2432 int i;
2434 printk("sh %llu, pd_idx %d, state %ld.\n",
2435 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2436 printk("sh %llu, count %d.\n",
2437 (unsigned long long)sh->sector, atomic_read(&sh->count));
2438 printk("sh %llu, ", (unsigned long long)sh->sector);
2439 for (i = 0; i < sh->disks; i++) {
2440 printk("(cache%d: %p %ld) ",
2441 i, sh->dev[i].page, sh->dev[i].flags);
2443 printk("\n");
2446 static void printall (raid5_conf_t *conf)
2448 struct stripe_head *sh;
2449 struct hlist_node *hn;
2450 int i;
2452 spin_lock_irq(&conf->device_lock);
2453 for (i = 0; i < NR_HASH; i++) {
2454 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
2455 if (sh->raid_conf != conf)
2456 continue;
2457 print_sh(sh);
2460 spin_unlock_irq(&conf->device_lock);
2462 #endif
2464 static void status (struct seq_file *seq, mddev_t *mddev)
2466 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2467 int i;
2469 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2470 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2471 for (i = 0; i < conf->raid_disks; i++)
2472 seq_printf (seq, "%s",
2473 conf->disks[i].rdev &&
2474 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
2475 seq_printf (seq, "]");
2476 #if RAID5_DEBUG
2477 #define D(x) \
2478 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
2479 printall(conf);
2480 #endif
2483 static void print_raid5_conf (raid5_conf_t *conf)
2485 int i;
2486 struct disk_info *tmp;
2488 printk("RAID5 conf printout:\n");
2489 if (!conf) {
2490 printk("(conf==NULL)\n");
2491 return;
2493 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2494 conf->working_disks, conf->failed_disks);
2496 for (i = 0; i < conf->raid_disks; i++) {
2497 char b[BDEVNAME_SIZE];
2498 tmp = conf->disks + i;
2499 if (tmp->rdev)
2500 printk(" disk %d, o:%d, dev:%s\n",
2501 i, !test_bit(Faulty, &tmp->rdev->flags),
2502 bdevname(tmp->rdev->bdev,b));
2506 static int raid5_spare_active(mddev_t *mddev)
2508 int i;
2509 raid5_conf_t *conf = mddev->private;
2510 struct disk_info *tmp;
2512 for (i = 0; i < conf->raid_disks; i++) {
2513 tmp = conf->disks + i;
2514 if (tmp->rdev
2515 && !test_bit(Faulty, &tmp->rdev->flags)
2516 && !test_bit(In_sync, &tmp->rdev->flags)) {
2517 mddev->degraded--;
2518 conf->failed_disks--;
2519 conf->working_disks++;
2520 set_bit(In_sync, &tmp->rdev->flags);
2523 print_raid5_conf(conf);
2524 return 0;
2527 static int raid5_remove_disk(mddev_t *mddev, int number)
2529 raid5_conf_t *conf = mddev->private;
2530 int err = 0;
2531 mdk_rdev_t *rdev;
2532 struct disk_info *p = conf->disks + number;
2534 print_raid5_conf(conf);
2535 rdev = p->rdev;
2536 if (rdev) {
2537 if (test_bit(In_sync, &rdev->flags) ||
2538 atomic_read(&rdev->nr_pending)) {
2539 err = -EBUSY;
2540 goto abort;
2542 p->rdev = NULL;
2543 synchronize_rcu();
2544 if (atomic_read(&rdev->nr_pending)) {
2545 /* lost the race, try later */
2546 err = -EBUSY;
2547 p->rdev = rdev;
2550 abort:
2552 print_raid5_conf(conf);
2553 return err;
2556 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2558 raid5_conf_t *conf = mddev->private;
2559 int found = 0;
2560 int disk;
2561 struct disk_info *p;
2563 if (mddev->degraded > 1)
2564 /* no point adding a device */
2565 return 0;
2568 * find the disk ...
2570 for (disk=0; disk < conf->raid_disks; disk++)
2571 if ((p=conf->disks + disk)->rdev == NULL) {
2572 clear_bit(In_sync, &rdev->flags);
2573 rdev->raid_disk = disk;
2574 found = 1;
2575 if (rdev->saved_raid_disk != disk)
2576 conf->fullsync = 1;
2577 rcu_assign_pointer(p->rdev, rdev);
2578 break;
2580 print_raid5_conf(conf);
2581 return found;
2584 static int raid5_resize(mddev_t *mddev, sector_t sectors)
2586 /* no resync is happening, and there is enough space
2587 * on all devices, so we can resize.
2588 * We need to make sure resync covers any new space.
2589 * If the array is shrinking we should possibly wait until
2590 * any io in the removed space completes, but it hardly seems
2591 * worth it.
2593 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2594 mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
2595 set_capacity(mddev->gendisk, mddev->array_size << 1);
2596 mddev->changed = 1;
2597 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2598 mddev->recovery_cp = mddev->size << 1;
2599 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2601 mddev->size = sectors /2;
2602 mddev->resync_max_sectors = sectors;
2603 return 0;
2606 #ifdef CONFIG_MD_RAID5_RESHAPE
2607 static int raid5_check_reshape(mddev_t *mddev)
2609 raid5_conf_t *conf = mddev_to_conf(mddev);
2610 int err;
2612 if (mddev->delta_disks < 0 ||
2613 mddev->new_level != mddev->level)
2614 return -EINVAL; /* Cannot shrink array or change level yet */
2615 if (mddev->delta_disks == 0)
2616 return 0; /* nothing to do */
2618 /* Can only proceed if there are plenty of stripe_heads.
2619 * We need a minimum of one full stripe,, and for sensible progress
2620 * it is best to have about 4 times that.
2621 * If we require 4 times, then the default 256 4K stripe_heads will
2622 * allow for chunk sizes up to 256K, which is probably OK.
2623 * If the chunk size is greater, user-space should request more
2624 * stripe_heads first.
2626 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
2627 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
2628 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
2629 (mddev->chunk_size / STRIPE_SIZE)*4);
2630 return -ENOSPC;
2633 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
2634 if (err)
2635 return err;
2637 /* looks like we might be able to manage this */
2638 return 0;
2641 static int raid5_start_reshape(mddev_t *mddev)
2643 raid5_conf_t *conf = mddev_to_conf(mddev);
2644 mdk_rdev_t *rdev;
2645 struct list_head *rtmp;
2646 int spares = 0;
2647 int added_devices = 0;
2649 if (mddev->degraded ||
2650 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2651 return -EBUSY;
2653 ITERATE_RDEV(mddev, rdev, rtmp)
2654 if (rdev->raid_disk < 0 &&
2655 !test_bit(Faulty, &rdev->flags))
2656 spares++;
2658 if (spares < mddev->delta_disks-1)
2659 /* Not enough devices even to make a degraded array
2660 * of that size
2662 return -EINVAL;
2664 atomic_set(&conf->reshape_stripes, 0);
2665 spin_lock_irq(&conf->device_lock);
2666 conf->previous_raid_disks = conf->raid_disks;
2667 conf->raid_disks += mddev->delta_disks;
2668 conf->expand_progress = 0;
2669 conf->expand_lo = 0;
2670 spin_unlock_irq(&conf->device_lock);
2672 /* Add some new drives, as many as will fit.
2673 * We know there are enough to make the newly sized array work.
2675 ITERATE_RDEV(mddev, rdev, rtmp)
2676 if (rdev->raid_disk < 0 &&
2677 !test_bit(Faulty, &rdev->flags)) {
2678 if (raid5_add_disk(mddev, rdev)) {
2679 char nm[20];
2680 set_bit(In_sync, &rdev->flags);
2681 conf->working_disks++;
2682 added_devices++;
2683 sprintf(nm, "rd%d", rdev->raid_disk);
2684 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2685 } else
2686 break;
2689 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
2690 mddev->raid_disks = conf->raid_disks;
2691 mddev->reshape_position = 0;
2692 mddev->sb_dirty = 1;
2694 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2695 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2696 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
2697 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
2698 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
2699 "%s_reshape");
2700 if (!mddev->sync_thread) {
2701 mddev->recovery = 0;
2702 spin_lock_irq(&conf->device_lock);
2703 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
2704 conf->expand_progress = MaxSector;
2705 spin_unlock_irq(&conf->device_lock);
2706 return -EAGAIN;
2708 md_wakeup_thread(mddev->sync_thread);
2709 md_new_event(mddev);
2710 return 0;
2712 #endif
2714 static void end_reshape(raid5_conf_t *conf)
2716 struct block_device *bdev;
2718 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
2719 conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
2720 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
2721 conf->mddev->changed = 1;
2723 bdev = bdget_disk(conf->mddev->gendisk, 0);
2724 if (bdev) {
2725 mutex_lock(&bdev->bd_inode->i_mutex);
2726 i_size_write(bdev->bd_inode, conf->mddev->array_size << 10);
2727 mutex_unlock(&bdev->bd_inode->i_mutex);
2728 bdput(bdev);
2730 spin_lock_irq(&conf->device_lock);
2731 conf->expand_progress = MaxSector;
2732 spin_unlock_irq(&conf->device_lock);
2733 conf->mddev->reshape_position = MaxSector;
2737 static void raid5_quiesce(mddev_t *mddev, int state)
2739 raid5_conf_t *conf = mddev_to_conf(mddev);
2741 switch(state) {
2742 case 2: /* resume for a suspend */
2743 wake_up(&conf->wait_for_overlap);
2744 break;
2746 case 1: /* stop all writes */
2747 spin_lock_irq(&conf->device_lock);
2748 conf->quiesce = 1;
2749 wait_event_lock_irq(conf->wait_for_stripe,
2750 atomic_read(&conf->active_stripes) == 0,
2751 conf->device_lock, /* nothing */);
2752 spin_unlock_irq(&conf->device_lock);
2753 break;
2755 case 0: /* re-enable writes */
2756 spin_lock_irq(&conf->device_lock);
2757 conf->quiesce = 0;
2758 wake_up(&conf->wait_for_stripe);
2759 wake_up(&conf->wait_for_overlap);
2760 spin_unlock_irq(&conf->device_lock);
2761 break;
2765 static struct mdk_personality raid5_personality =
2767 .name = "raid5",
2768 .level = 5,
2769 .owner = THIS_MODULE,
2770 .make_request = make_request,
2771 .run = run,
2772 .stop = stop,
2773 .status = status,
2774 .error_handler = error,
2775 .hot_add_disk = raid5_add_disk,
2776 .hot_remove_disk= raid5_remove_disk,
2777 .spare_active = raid5_spare_active,
2778 .sync_request = sync_request,
2779 .resize = raid5_resize,
2780 #ifdef CONFIG_MD_RAID5_RESHAPE
2781 .check_reshape = raid5_check_reshape,
2782 .start_reshape = raid5_start_reshape,
2783 #endif
2784 .quiesce = raid5_quiesce,
2787 static struct mdk_personality raid4_personality =
2789 .name = "raid4",
2790 .level = 4,
2791 .owner = THIS_MODULE,
2792 .make_request = make_request,
2793 .run = run,
2794 .stop = stop,
2795 .status = status,
2796 .error_handler = error,
2797 .hot_add_disk = raid5_add_disk,
2798 .hot_remove_disk= raid5_remove_disk,
2799 .spare_active = raid5_spare_active,
2800 .sync_request = sync_request,
2801 .resize = raid5_resize,
2802 .quiesce = raid5_quiesce,
2805 static int __init raid5_init(void)
2807 register_md_personality(&raid5_personality);
2808 register_md_personality(&raid4_personality);
2809 return 0;
2812 static void raid5_exit(void)
2814 unregister_md_personality(&raid5_personality);
2815 unregister_md_personality(&raid4_personality);
2818 module_init(raid5_init);
2819 module_exit(raid5_exit);
2820 MODULE_LICENSE("GPL");
2821 MODULE_ALIAS("md-personality-4"); /* RAID5 */
2822 MODULE_ALIAS("md-raid5");
2823 MODULE_ALIAS("md-raid4");
2824 MODULE_ALIAS("md-level-5");
2825 MODULE_ALIAS("md-level-4");