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)
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>
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
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
58 #define RAID5_PARANOIA 1
59 #if RAID5_PARANOIA && defined(CONFIG_SMP)
60 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
62 # define CHECK_DEVLOCK()
65 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
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);
85 clear_bit(STRIPE_BIT_DELAY, &sh->state);
86 list_add_tail(&sh->lru, &conf->handle_list);
88 md_wakeup_thread(conf->mddev->thread);
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;
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);
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;
138 if (list_empty(&conf->inactive_list))
140 first = conf->inactive_list.next;
141 sh = list_entry(first, struct stripe_head, lru);
142 list_del_init(first);
144 atomic_inc(&conf->active_stripes);
149 static void shrink_buffers(struct stripe_head *sh, int num)
154 for (i=0; i<num ; i++) {
158 sh->dev[i].page = NULL;
163 static int grow_buffers(struct stripe_head *sh, int num)
167 for (i=0; i<num; i++) {
170 if (!(page = alloc_page(GFP_KERNEL))) {
173 sh->dev[i].page = page;
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;
185 BUG_ON(atomic_read(&sh->count) != 0);
186 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
189 PRINTK("init_stripe called, stripe %llu\n",
190 (unsigned long long)sh->sector);
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));
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;
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)
227 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
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);
244 wait_event_lock_irq(conf->wait_for_stripe,
246 conf->device_lock, /* nothing */);
247 sh = __find_stripe(conf, sector, disks);
249 if (!conf->inactive_blocked)
250 sh = get_free_stripe(conf);
251 if (noblock && sh == NULL)
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),
261 unplug_slaves(conf->mddev)
263 conf->inactive_blocked = 0;
265 init_stripe(sh, sector, pd_idx, disks);
267 if (atomic_read(&sh->count)) {
268 BUG_ON(!list_empty(&sh->lru));
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);
279 atomic_inc(&sh->count);
281 spin_unlock_irq(&conf->device_lock);
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);
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);
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);
309 static int grow_stripes(raid5_conf_t *conf, int num)
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),
322 conf->slab_cache = sc;
323 conf->pool_size = devs;
325 if (!grow_one_stripe(conf))
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
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
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;
364 if (newsize <= conf->pool_size)
365 return 0; /* never bother to shrink */
368 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
369 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
374 for (i = conf->max_nr_stripes; i; i--) {
375 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
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);
387 /* didn't get enough, give up */
388 while (!list_empty(&newstripes)) {
389 nsh = list_entry(newstripes.next, struct stripe_head, lru);
391 kmem_cache_free(sc, nsh);
393 kmem_cache_destroy(sc);
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),
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);
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
423 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
425 for (i=0; i<conf->raid_disks; i++)
426 ndisks[i] = conf->disks[i];
428 conf->disks = ndisks;
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;
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;
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);
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);
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,
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);
491 for (i=0 ; i<disks; i++)
492 if (bi == &sh->dev[i].req)
495 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
496 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
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
513 buffer = sh->bh_read[i];
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;
522 spin_unlock_irqrestore(&conf->device_lock, flags);
523 if (sh->bh_page[i]==bh->b_page)
524 set_buffer_uptodate(bh);
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);
531 set_bit(R5_UPTODATE, &sh->dev[i].flags);
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);
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))
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)
552 "raid5: Too many read errors, failing device.\n");
556 set_bit(R5_ReadError, &sh->dev[i].flags);
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);
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);
572 clear_bit(R5_LOCKED, &sh->dev[i].flags);
573 set_bit(STRIPE_HANDLE, &sh->state);
578 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
581 struct stripe_head *sh = bi->bi_private;
582 raid5_conf_t *conf = sh->raid_conf;
583 int disks = sh->disks, i;
585 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
590 for (i=0 ; i<disks; i++)
591 if (bi == &sh->dev[i].req)
594 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
595 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
602 spin_lock_irqsave(&conf->device_lock, flags);
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);
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];
623 dev->req.bi_io_vec = &dev->vec;
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;
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)) {
646 if (test_bit(In_sync, &rdev->flags)) {
647 conf->working_disks--;
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);
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)
673 unsigned long chunk_number;
674 unsigned int chunk_offset;
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)
708 case ALGORITHM_RIGHT_ASYMMETRIC:
709 *pd_idx = stripe % raid_disks;
710 if (*dd_idx >= *pd_idx)
713 case ALGORITHM_LEFT_SYMMETRIC:
714 *pd_idx = data_disks - stripe % raid_disks;
715 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
717 case ALGORITHM_RIGHT_SYMMETRIC:
718 *pd_idx = stripe % raid_disks;
719 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
722 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
727 * Finally, compute the new sector number
729 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
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;
742 int chunk_number, dummy1, dummy2, dd_idx = i;
745 chunk_offset = sector_div(new_sector, sectors_per_chunk);
747 BUG_ON(new_sector != stripe);
750 switch (conf->algorithm) {
751 case ALGORITHM_LEFT_ASYMMETRIC:
752 case ALGORITHM_RIGHT_ASYMMETRIC:
756 case ALGORITHM_LEFT_SYMMETRIC:
757 case ALGORITHM_RIGHT_SYMMETRIC:
760 i -= (sh->pd_idx + 1);
763 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
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");
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,
790 char *pa = page_address(page);
795 if (bio->bi_sector >= sector)
796 page_offset = (signed)(bio->bi_sector - sector) * 512;
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;
804 if (page_offset < 0) {
805 b_offset = -page_offset;
806 page_offset += b_offset;
810 if (len > 0 && page_offset + len > STRIPE_SIZE)
811 clen = STRIPE_SIZE - page_offset;
815 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
817 memcpy(pa+page_offset, ba+b_offset, clen);
819 memcpy(ba+b_offset, pa+page_offset, clen);
820 __bio_kunmap_atomic(ba, KM_USER0);
822 if (clen < len) /* hit end of page */
828 #define check_xor() do { \
829 if (count == MAX_XOR_BLOCKS) { \
830 xor_block(count, STRIPE_SIZE, ptr); \
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);
847 for (i = disks ; i--; ) {
850 p = page_address(sh->dev[i].page);
851 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
854 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
855 " not present\n", dd_idx,
856 (unsigned long long)sh->sector, i);
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];
872 PRINTK("compute_parity, stripe %llu, method %d\n",
873 (unsigned long long)sh->sector, method);
876 ptr[0] = page_address(sh->dev[pd_idx].page);
878 case READ_MODIFY_WRITE:
879 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
880 for (i=disks ; i-- ;) {
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;
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;
916 xor_block(count, STRIPE_SIZE, ptr);
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);
934 case RECONSTRUCT_WRITE:
938 ptr[count++] = page_address(sh->dev[i].page);
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);
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);
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)
967 raid5_conf_t *conf = sh->raid_conf;
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);
978 bip = &sh->dev[dd_idx].towrite;
979 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
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)
986 bip = & (*bip)->bi_next;
988 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
991 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
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,
1007 set_bit(STRIPE_BIT_DELAY, &sh->state);
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);
1026 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1027 spin_unlock_irq(&conf->device_lock);
1028 spin_unlock(&sh->lock);
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;
1039 int chunk_offset = sector_div(x, sectors_per_chunk);
1041 raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
1042 + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
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.
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;
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;
1078 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1079 (unsigned long long)sh->sector, atomic_read(&sh->count),
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 */
1092 for (i=disks; 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);
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;
1117 spin_unlock_irq(&conf->device_lock);
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++;
1130 if (!test_bit(R5_OVERWRITE, &dev->flags))
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)) {
1145 set_bit(R5_Insync, &dev->flags);
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
1154 if (failed > 1 && to_read+to_write+written) {
1155 for (i=disks; i--; ) {
1158 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
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);
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;
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;
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);
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;
1220 spin_unlock_irq(&conf->device_lock);
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);
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];
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) {
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;
1253 PRINTK("Return write for disc %d\n", i);
1254 spin_lock_irq(&conf->device_lock);
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;
1266 if (dev->towrite == NULL)
1268 spin_unlock_irq(&conf->device_lock);
1270 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
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--;) {
1284 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1286 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
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);
1300 } else if (test_bit(R5_Insync, &dev->flags)) {
1301 set_bit(R5_LOCKED, &dev->flags);
1302 set_bit(R5_Wantread, &dev->flags);
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;
1313 PRINTK("Reading block %d (sync=%d)\n",
1318 set_bit(STRIPE_HANDLE, &sh->state);
1321 /* now to consider writing and what else, if anything should be read */
1324 for (i=disks ; i--;) {
1325 /* would I have to read this buffer for read_modify_write */
1327 if ((dev->towrite || i == sh->pd_idx) &&
1328 (!test_bit(R5_LOCKED, &dev->flags)
1330 || sh->bh_page[i]!=bh->b_page
1333 !test_bit(R5_UPTODATE, &dev->flags)) {
1334 if (test_bit(R5_Insync, &dev->flags)
1335 /* && !(!mddev->insync && i == sh->pd_idx) */
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)
1344 || sh->bh_page[i] != bh->b_page
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--;) {
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);
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--;) {
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);
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 */
1400 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1401 PRINTK("Writing block %d\n", i);
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
1420 if (syncing && locked == 0 &&
1421 !test_bit(STRIPE_INSYNC, &sh->state)) {
1422 set_bit(STRIPE_HANDLE, &sh->state);
1425 BUG_ON(uptodate != disks);
1426 compute_parity(sh, CHECK_PARITY);
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);
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);
1439 compute_block(sh, sh->pd_idx);
1444 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1445 /* either failed parity check, or recovery is happening */
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);
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);
1479 /* let's read it back */
1480 set_bit(R5_Wantread, &dev->flags);
1481 set_bit(R5_LOCKED, &dev->flags);
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);
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,
1517 &dd_idx, &pd_idx, conf);
1518 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1520 /* so far only the early blocks of this stripe
1521 * have been requested. When later blocks
1522 * get requested, we will try again
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);
1531 memcpy(page_address(sh2->dev[dd_idx].page),
1532 page_address(sh->dev[i].page),
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))
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;
1556 bi->bi_end_io(bi, bytes, 0);
1558 for (i=disks; i-- ;) {
1562 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1564 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1569 bi = &sh->dev[i].req;
1573 bi->bi_end_io = raid5_end_write_request;
1575 bi->bi_end_io = raid5_end_read_request;
1578 rdev = rcu_dereference(conf->disks[i].rdev);
1579 if (rdev && test_bit(Faulty, &rdev->flags))
1582 atomic_inc(&rdev->nr_pending);
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;
1596 bi->bi_max_vecs = 1;
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;
1604 test_bit(R5_ReWrite, &sh->dev[i].flags))
1605 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1606 generic_make_request(bi);
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);
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);
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);
1662 if (r_queue->unplug_fn)
1663 r_queue->unplug_fn(r_queue);
1665 rdev_dec_pending(rdev, mddev);
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)) {
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);
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)
1708 atomic_inc(&rdev->nr_pending);
1710 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1712 rdev_dec_pending(rdev, mddev);
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);
1739 if (unlikely(bio_barrier(bi))) {
1740 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
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);
1752 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1754 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1759 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1760 if (likely(conf->expand_progress == MaxSector))
1761 disks = conf->raid_disks;
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
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;
1776 if (logical_sector >= conf->expand_lo) {
1777 spin_unlock_irq(&conf->device_lock);
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));
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.
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 */
1807 spin_unlock_irq(&conf->device_lock);
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) {
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
1829 raid5_unplug_device(mddev->queue);
1834 finish_wait(&conf->wait_for_overlap, &w);
1835 raid5_plug_device(conf);
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);
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);
1855 bi->bi_end_io(bi, bytes, 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;
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;
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)) {
1880 if (mddev->curr_resync < max_sector) /* aborted */
1881 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1883 else /* compelted sync */
1885 bitmap_close_sync(mddev->bitmap);
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.
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);
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) {
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--;) {
1957 if (j == sh->pd_idx)
1959 s = compute_blocknr(sh, j);
1960 if (s < (mddev->array_size<<1)) {
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);
1969 set_bit(STRIPE_EXPAND_READY, &sh->state);
1970 set_bit(STRIPE_HANDLE, &sh->state);
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;
1985 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
1986 raid_disks, data_disks,
1987 &dd_idx, &pd_idx, conf);
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);
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;
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;
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);
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);
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);
2058 PRINTK("+++ raid5d active\n");
2060 md_check_recovery(mddev);
2063 spin_lock_irq(&conf->device_lock);
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))
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);
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");
2109 raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
2111 raid5_conf_t *conf = mddev_to_conf(mddev);
2113 return sprintf(page, "%d\n", conf->max_nr_stripes);
2119 raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
2121 raid5_conf_t *conf = mddev_to_conf(mddev);
2124 if (len >= PAGE_SIZE)
2129 new = simple_strtoul(page, &end, 10);
2130 if (!*page || (*end && *end != '\n') )
2132 if (new <= 16 || new > 32768)
2134 while (new < conf->max_nr_stripes) {
2135 if (drop_one_stripe(conf))
2136 conf->max_nr_stripes--;
2140 while (new > conf->max_nr_stripes) {
2141 if (grow_one_stripe(conf))
2142 conf->max_nr_stripes++;
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);
2154 stripe_cache_active_show(mddev_t *mddev, char *page)
2156 raid5_conf_t *conf = mddev_to_conf(mddev);
2158 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
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,
2171 static struct attribute_group raid5_attrs_group = {
2173 .attrs = raid5_attrs,
2176 static int run(mddev_t *mddev)
2179 int raid_disk, memory;
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);
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;
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",
2206 if (mddev->delta_disks <= 0) {
2207 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
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");
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");
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)
2237 if (mddev->reshape_position == MaxSector) {
2238 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
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),
2249 conf->mddev = mddev;
2251 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
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
2271 disk = conf->disks + raid_disk;
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),
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));
2304 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
2306 "raid5: unsupported parity algorithm %d for %s\n",
2307 conf->algorithm, mdname(mddev));
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);
2317 if (mddev->degraded == 1 &&
2318 mddev->recovery_cp != MaxSector) {
2319 if (mddev->ok_start_degraded)
2321 "raid5: starting dirty degraded array: %s"
2322 "- data corruption possible.\n",
2326 "raid5: cannot start dirty degraded array for %s\n",
2333 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
2334 if (!mddev->thread) {
2336 "raid5: couldn't allocate thread for %s\n",
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)) {
2345 "raid5: couldn't allocate %dkB for buffers\n", memory);
2346 shrink_stripes(conf);
2347 md_unregister_thread(mddev->thread);
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,
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,
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
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);
2401 print_raid5_conf(conf);
2403 kfree(conf->stripe_hashtbl);
2406 mddev->private = NULL;
2407 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
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);
2425 mddev->private = NULL;
2430 static void print_sh (struct stripe_head *sh)
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);
2446 static void printall (raid5_conf_t *conf)
2448 struct stripe_head *sh;
2449 struct hlist_node *hn;
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)
2460 spin_unlock_irq(&conf->device_lock);
2464 static void status (struct seq_file *seq, mddev_t *mddev)
2466 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
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, "]");
2478 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
2483 static void print_raid5_conf (raid5_conf_t *conf)
2486 struct disk_info *tmp;
2488 printk("RAID5 conf printout:\n");
2490 printk("(conf==NULL)\n");
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;
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)
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;
2515 && !test_bit(Faulty, &tmp->rdev->flags)
2516 && !test_bit(In_sync, &tmp->rdev->flags)) {
2518 conf->failed_disks--;
2519 conf->working_disks++;
2520 set_bit(In_sync, &tmp->rdev->flags);
2523 print_raid5_conf(conf);
2527 static int raid5_remove_disk(mddev_t *mddev, int number)
2529 raid5_conf_t *conf = mddev->private;
2532 struct disk_info *p = conf->disks + number;
2534 print_raid5_conf(conf);
2537 if (test_bit(In_sync, &rdev->flags) ||
2538 atomic_read(&rdev->nr_pending)) {
2544 if (atomic_read(&rdev->nr_pending)) {
2545 /* lost the race, try later */
2552 print_raid5_conf(conf);
2556 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2558 raid5_conf_t *conf = mddev->private;
2561 struct disk_info *p;
2563 if (mddev->degraded > 1)
2564 /* no point adding a device */
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;
2575 if (rdev->saved_raid_disk != disk)
2577 rcu_assign_pointer(p->rdev, rdev);
2580 print_raid5_conf(conf);
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
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);
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;
2606 #ifdef CONFIG_MD_RAID5_RESHAPE
2607 static int raid5_check_reshape(mddev_t *mddev)
2609 raid5_conf_t *conf = mddev_to_conf(mddev);
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);
2633 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
2637 /* looks like we might be able to manage this */
2641 static int raid5_start_reshape(mddev_t *mddev)
2643 raid5_conf_t *conf = mddev_to_conf(mddev);
2645 struct list_head *rtmp;
2647 int added_devices = 0;
2649 if (mddev->degraded ||
2650 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2653 ITERATE_RDEV(mddev, rdev, rtmp)
2654 if (rdev->raid_disk < 0 &&
2655 !test_bit(Faulty, &rdev->flags))
2658 if (spares < mddev->delta_disks-1)
2659 /* Not enough devices even to make a degraded array
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)) {
2680 set_bit(In_sync, &rdev->flags);
2681 conf->working_disks++;
2683 sprintf(nm, "rd%d", rdev->raid_disk);
2684 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
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,
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);
2708 md_wakeup_thread(mddev->sync_thread);
2709 md_new_event(mddev);
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);
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);
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);
2742 case 2: /* resume for a suspend */
2743 wake_up(&conf->wait_for_overlap);
2746 case 1: /* stop all writes */
2747 spin_lock_irq(&conf->device_lock);
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);
2755 case 0: /* re-enable writes */
2756 spin_lock_irq(&conf->device_lock);
2758 wake_up(&conf->wait_for_stripe);
2759 wake_up(&conf->wait_for_overlap);
2760 spin_unlock_irq(&conf->device_lock);
2765 static struct mdk_personality raid5_personality =
2769 .owner = THIS_MODULE,
2770 .make_request = make_request,
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,
2784 .quiesce = raid5_quiesce,
2787 static struct mdk_personality raid4_personality =
2791 .owner = THIS_MODULE,
2792 .make_request = make_request,
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);
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");