2 * raid6main.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-6 management functions. This code is derived from raid5.c.
8 * Last merge from raid5.c bkcvs version 1.79 (kernel 2.6.1).
10 * Thanks to Penguin Computing for making the RAID-6 development possible
11 * by donating a test server!
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * You should have received a copy of the GNU General Public License
19 * (for example /usr/src/linux/COPYING); if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/highmem.h>
28 #include <linux/bitops.h>
29 #include <asm/atomic.h>
32 #include <linux/raid/bitmap.h>
38 #define NR_STRIPES 256
39 #define STRIPE_SIZE PAGE_SIZE
40 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
41 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
42 #define IO_THRESHOLD 1
43 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
44 #define HASH_MASK (NR_HASH - 1)
46 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
48 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
49 * order without overlap. There may be several bio's per stripe+device, and
50 * a bio could span several devices.
51 * When walking this list for a particular stripe+device, we must never proceed
52 * beyond a bio that extends past this device, as the next bio might no longer
54 * This macro is used to determine the 'next' bio in the list, given the sector
55 * of the current stripe+device
57 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
59 * The following can be used to debug the driver
61 #define RAID6_DEBUG 0 /* Extremely verbose printk */
62 #define RAID6_PARANOIA 1 /* Check spinlocks */
63 #define RAID6_DUMPSTATE 0 /* Include stripe cache state in /proc/mdstat */
64 #if RAID6_PARANOIA && defined(CONFIG_SMP)
65 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
67 # define CHECK_DEVLOCK()
70 #define PRINTK(x...) ((void)(RAID6_DEBUG && printk(KERN_DEBUG x)))
78 #if !RAID6_USE_EMPTY_ZERO_PAGE
79 /* In .bss so it's zeroed */
80 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
83 static inline int raid6_next_disk(int disk, int raid_disks)
86 return (disk < raid_disks) ? disk : 0;
89 static void print_raid6_conf (raid6_conf_t *conf);
91 static void __release_stripe(raid6_conf_t *conf, struct stripe_head *sh)
93 if (atomic_dec_and_test(&sh->count)) {
94 if (!list_empty(&sh->lru))
96 if (atomic_read(&conf->active_stripes)==0)
98 if (test_bit(STRIPE_HANDLE, &sh->state)) {
99 if (test_bit(STRIPE_DELAYED, &sh->state))
100 list_add_tail(&sh->lru, &conf->delayed_list);
101 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
102 conf->seq_write == sh->bm_seq)
103 list_add_tail(&sh->lru, &conf->bitmap_list);
105 clear_bit(STRIPE_BIT_DELAY, &sh->state);
106 list_add_tail(&sh->lru, &conf->handle_list);
108 md_wakeup_thread(conf->mddev->thread);
110 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
111 atomic_dec(&conf->preread_active_stripes);
112 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
113 md_wakeup_thread(conf->mddev->thread);
115 list_add_tail(&sh->lru, &conf->inactive_list);
116 atomic_dec(&conf->active_stripes);
117 if (!conf->inactive_blocked ||
118 atomic_read(&conf->active_stripes) < (conf->max_nr_stripes*3/4))
119 wake_up(&conf->wait_for_stripe);
123 static void release_stripe(struct stripe_head *sh)
125 raid6_conf_t *conf = sh->raid_conf;
128 spin_lock_irqsave(&conf->device_lock, flags);
129 __release_stripe(conf, sh);
130 spin_unlock_irqrestore(&conf->device_lock, flags);
133 static inline void remove_hash(struct stripe_head *sh)
135 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
137 hlist_del_init(&sh->hash);
140 static inline void insert_hash(raid6_conf_t *conf, struct stripe_head *sh)
142 struct hlist_head *hp = stripe_hash(conf, sh->sector);
144 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
147 hlist_add_head(&sh->hash, hp);
151 /* find an idle stripe, make sure it is unhashed, and return it. */
152 static struct stripe_head *get_free_stripe(raid6_conf_t *conf)
154 struct stripe_head *sh = NULL;
155 struct list_head *first;
158 if (list_empty(&conf->inactive_list))
160 first = conf->inactive_list.next;
161 sh = list_entry(first, struct stripe_head, lru);
162 list_del_init(first);
164 atomic_inc(&conf->active_stripes);
169 static void shrink_buffers(struct stripe_head *sh, int num)
174 for (i=0; i<num ; i++) {
178 sh->dev[i].page = NULL;
183 static int grow_buffers(struct stripe_head *sh, int num)
187 for (i=0; i<num; i++) {
190 if (!(page = alloc_page(GFP_KERNEL))) {
193 sh->dev[i].page = page;
198 static void raid6_build_block (struct stripe_head *sh, int i);
200 static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
202 raid6_conf_t *conf = sh->raid_conf;
203 int disks = conf->raid_disks, i;
205 if (atomic_read(&sh->count) != 0)
207 if (test_bit(STRIPE_HANDLE, &sh->state))
211 PRINTK("init_stripe called, stripe %llu\n",
212 (unsigned long long)sh->sector);
220 for (i=disks; i--; ) {
221 struct r5dev *dev = &sh->dev[i];
223 if (dev->toread || dev->towrite || dev->written ||
224 test_bit(R5_LOCKED, &dev->flags)) {
225 PRINTK("sector=%llx i=%d %p %p %p %d\n",
226 (unsigned long long)sh->sector, i, dev->toread,
227 dev->towrite, dev->written,
228 test_bit(R5_LOCKED, &dev->flags));
232 raid6_build_block(sh, i);
234 insert_hash(conf, sh);
237 static struct stripe_head *__find_stripe(raid6_conf_t *conf, sector_t sector)
239 struct stripe_head *sh;
240 struct hlist_node *hn;
243 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
244 hlist_for_each_entry (sh, hn, stripe_hash(conf, sector), hash)
245 if (sh->sector == sector)
247 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
251 static void unplug_slaves(mddev_t *mddev);
253 static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector,
254 int pd_idx, int noblock)
256 struct stripe_head *sh;
258 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
260 spin_lock_irq(&conf->device_lock);
263 wait_event_lock_irq(conf->wait_for_stripe,
265 conf->device_lock, /* nothing */);
266 sh = __find_stripe(conf, sector);
268 if (!conf->inactive_blocked)
269 sh = get_free_stripe(conf);
270 if (noblock && sh == NULL)
273 conf->inactive_blocked = 1;
274 wait_event_lock_irq(conf->wait_for_stripe,
275 !list_empty(&conf->inactive_list) &&
276 (atomic_read(&conf->active_stripes)
277 < (conf->max_nr_stripes *3/4)
278 || !conf->inactive_blocked),
280 unplug_slaves(conf->mddev);
282 conf->inactive_blocked = 0;
284 init_stripe(sh, sector, pd_idx);
286 if (atomic_read(&sh->count)) {
287 if (!list_empty(&sh->lru))
290 if (!test_bit(STRIPE_HANDLE, &sh->state))
291 atomic_inc(&conf->active_stripes);
292 if (list_empty(&sh->lru))
294 list_del_init(&sh->lru);
297 } while (sh == NULL);
300 atomic_inc(&sh->count);
302 spin_unlock_irq(&conf->device_lock);
306 static int grow_one_stripe(raid6_conf_t *conf)
308 struct stripe_head *sh;
309 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
312 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
313 sh->raid_conf = conf;
314 spin_lock_init(&sh->lock);
316 if (grow_buffers(sh, conf->raid_disks)) {
317 shrink_buffers(sh, conf->raid_disks);
318 kmem_cache_free(conf->slab_cache, sh);
321 /* we just created an active stripe so... */
322 atomic_set(&sh->count, 1);
323 atomic_inc(&conf->active_stripes);
324 INIT_LIST_HEAD(&sh->lru);
329 static int grow_stripes(raid6_conf_t *conf, int num)
332 int devs = conf->raid_disks;
334 sprintf(conf->cache_name, "raid6/%s", mdname(conf->mddev));
336 sc = kmem_cache_create(conf->cache_name,
337 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
341 conf->slab_cache = sc;
343 if (!grow_one_stripe(conf))
348 static int drop_one_stripe(raid6_conf_t *conf)
350 struct stripe_head *sh;
351 spin_lock_irq(&conf->device_lock);
352 sh = get_free_stripe(conf);
353 spin_unlock_irq(&conf->device_lock);
356 if (atomic_read(&sh->count))
358 shrink_buffers(sh, conf->raid_disks);
359 kmem_cache_free(conf->slab_cache, sh);
360 atomic_dec(&conf->active_stripes);
364 static void shrink_stripes(raid6_conf_t *conf)
366 while (drop_one_stripe(conf))
369 kmem_cache_destroy(conf->slab_cache);
370 conf->slab_cache = NULL;
373 static int raid6_end_read_request(struct bio * bi, unsigned int bytes_done,
376 struct stripe_head *sh = bi->bi_private;
377 raid6_conf_t *conf = sh->raid_conf;
378 int disks = conf->raid_disks, i;
379 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
384 for (i=0 ; i<disks; i++)
385 if (bi == &sh->dev[i].req)
388 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
389 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
400 spin_lock_irqsave(&conf->device_lock, flags);
401 /* we can return a buffer if we bypassed the cache or
402 * if the top buffer is not in highmem. If there are
403 * multiple buffers, leave the extra work to
406 buffer = sh->bh_read[i];
408 (!PageHighMem(buffer->b_page)
409 || buffer->b_page == bh->b_page )
411 sh->bh_read[i] = buffer->b_reqnext;
412 buffer->b_reqnext = NULL;
415 spin_unlock_irqrestore(&conf->device_lock, flags);
416 if (sh->bh_page[i]==bh->b_page)
417 set_buffer_uptodate(bh);
419 if (buffer->b_page != bh->b_page)
420 memcpy(buffer->b_data, bh->b_data, bh->b_size);
421 buffer->b_end_io(buffer, 1);
424 set_bit(R5_UPTODATE, &sh->dev[i].flags);
426 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
427 printk(KERN_INFO "raid6: read error corrected!!\n");
428 clear_bit(R5_ReadError, &sh->dev[i].flags);
429 clear_bit(R5_ReWrite, &sh->dev[i].flags);
431 if (atomic_read(&conf->disks[i].rdev->read_errors))
432 atomic_set(&conf->disks[i].rdev->read_errors, 0);
435 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
436 atomic_inc(&conf->disks[i].rdev->read_errors);
437 if (conf->mddev->degraded)
438 printk(KERN_WARNING "raid6: read error not correctable.\n");
439 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
441 printk(KERN_WARNING "raid6: read error NOT corrected!!\n");
442 else if (atomic_read(&conf->disks[i].rdev->read_errors)
443 > conf->max_nr_stripes)
445 "raid6: Too many read errors, failing device.\n");
449 set_bit(R5_ReadError, &sh->dev[i].flags);
451 clear_bit(R5_ReadError, &sh->dev[i].flags);
452 clear_bit(R5_ReWrite, &sh->dev[i].flags);
453 md_error(conf->mddev, conf->disks[i].rdev);
456 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
458 /* must restore b_page before unlocking buffer... */
459 if (sh->bh_page[i] != bh->b_page) {
460 bh->b_page = sh->bh_page[i];
461 bh->b_data = page_address(bh->b_page);
462 clear_buffer_uptodate(bh);
465 clear_bit(R5_LOCKED, &sh->dev[i].flags);
466 set_bit(STRIPE_HANDLE, &sh->state);
471 static int raid6_end_write_request (struct bio *bi, unsigned int bytes_done,
474 struct stripe_head *sh = bi->bi_private;
475 raid6_conf_t *conf = sh->raid_conf;
476 int disks = conf->raid_disks, i;
478 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
483 for (i=0 ; i<disks; i++)
484 if (bi == &sh->dev[i].req)
487 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
488 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
495 spin_lock_irqsave(&conf->device_lock, flags);
497 md_error(conf->mddev, conf->disks[i].rdev);
499 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
501 clear_bit(R5_LOCKED, &sh->dev[i].flags);
502 set_bit(STRIPE_HANDLE, &sh->state);
503 __release_stripe(conf, sh);
504 spin_unlock_irqrestore(&conf->device_lock, flags);
509 static sector_t compute_blocknr(struct stripe_head *sh, int i);
511 static void raid6_build_block (struct stripe_head *sh, int i)
513 struct r5dev *dev = &sh->dev[i];
514 int pd_idx = sh->pd_idx;
515 int qd_idx = raid6_next_disk(pd_idx, sh->raid_conf->raid_disks);
518 dev->req.bi_io_vec = &dev->vec;
520 dev->req.bi_max_vecs++;
521 dev->vec.bv_page = dev->page;
522 dev->vec.bv_len = STRIPE_SIZE;
523 dev->vec.bv_offset = 0;
525 dev->req.bi_sector = sh->sector;
526 dev->req.bi_private = sh;
529 if (i != pd_idx && i != qd_idx)
530 dev->sector = compute_blocknr(sh, i);
533 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
535 char b[BDEVNAME_SIZE];
536 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
537 PRINTK("raid6: error called\n");
539 if (!test_bit(Faulty, &rdev->flags)) {
541 if (test_bit(In_sync, &rdev->flags)) {
542 conf->working_disks--;
544 conf->failed_disks++;
545 clear_bit(In_sync, &rdev->flags);
547 * if recovery was running, make sure it aborts.
549 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
551 set_bit(Faulty, &rdev->flags);
553 "raid6: Disk failure on %s, disabling device."
554 " Operation continuing on %d devices\n",
555 bdevname(rdev->bdev,b), conf->working_disks);
560 * Input: a 'big' sector number,
561 * Output: index of the data and parity disk, and the sector # in them.
563 static sector_t raid6_compute_sector(sector_t r_sector, unsigned int raid_disks,
564 unsigned int data_disks, unsigned int * dd_idx,
565 unsigned int * pd_idx, raid6_conf_t *conf)
568 unsigned long chunk_number;
569 unsigned int chunk_offset;
571 int sectors_per_chunk = conf->chunk_size >> 9;
573 /* First compute the information on this sector */
576 * Compute the chunk number and the sector offset inside the chunk
578 chunk_offset = sector_div(r_sector, sectors_per_chunk);
579 chunk_number = r_sector;
580 if ( r_sector != chunk_number ) {
581 printk(KERN_CRIT "raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
582 (unsigned long long)r_sector, (unsigned long)chunk_number);
587 * Compute the stripe number
589 stripe = chunk_number / data_disks;
592 * Compute the data disk and parity disk indexes inside the stripe
594 *dd_idx = chunk_number % data_disks;
597 * Select the parity disk based on the user selected algorithm.
601 switch (conf->algorithm) {
602 case ALGORITHM_LEFT_ASYMMETRIC:
603 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
604 if (*pd_idx == raid_disks-1)
605 (*dd_idx)++; /* Q D D D P */
606 else if (*dd_idx >= *pd_idx)
607 (*dd_idx) += 2; /* D D P Q D */
609 case ALGORITHM_RIGHT_ASYMMETRIC:
610 *pd_idx = stripe % raid_disks;
611 if (*pd_idx == raid_disks-1)
612 (*dd_idx)++; /* Q D D D P */
613 else if (*dd_idx >= *pd_idx)
614 (*dd_idx) += 2; /* D D P Q D */
616 case ALGORITHM_LEFT_SYMMETRIC:
617 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
618 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
620 case ALGORITHM_RIGHT_SYMMETRIC:
621 *pd_idx = stripe % raid_disks;
622 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
625 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
629 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
630 chunk_number, *pd_idx, *dd_idx);
633 * Finally, compute the new sector number
635 new_sector = (sector_t) stripe * sectors_per_chunk + chunk_offset;
640 static sector_t compute_blocknr(struct stripe_head *sh, int i)
642 raid6_conf_t *conf = sh->raid_conf;
643 int raid_disks = conf->raid_disks, data_disks = raid_disks - 2;
644 sector_t new_sector = sh->sector, check;
645 int sectors_per_chunk = conf->chunk_size >> 9;
648 int chunk_number, dummy1, dummy2, dd_idx = i;
652 chunk_offset = sector_div(new_sector, sectors_per_chunk);
654 if ( new_sector != stripe ) {
655 printk(KERN_CRIT "raid6: ERROR: new_sector = %llu, stripe = %lu\n",
656 (unsigned long long)new_sector, (unsigned long)stripe);
660 switch (conf->algorithm) {
661 case ALGORITHM_LEFT_ASYMMETRIC:
662 case ALGORITHM_RIGHT_ASYMMETRIC:
663 if (sh->pd_idx == raid_disks-1)
665 else if (i > sh->pd_idx)
666 i -= 2; /* D D P Q D */
668 case ALGORITHM_LEFT_SYMMETRIC:
669 case ALGORITHM_RIGHT_SYMMETRIC:
670 if (sh->pd_idx == raid_disks-1)
676 i -= (sh->pd_idx + 2);
680 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
684 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh->pd_idx, i0, i);
686 chunk_number = stripe * data_disks + i;
687 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
689 check = raid6_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
690 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
691 printk(KERN_CRIT "raid6: compute_blocknr: map not correct\n");
700 * Copy data between a page in the stripe cache, and one or more bion
701 * The page could align with the middle of the bio, or there could be
702 * several bion, each with several bio_vecs, which cover part of the page
703 * Multiple bion are linked together on bi_next. There may be extras
704 * at the end of this list. We ignore them.
706 static void copy_data(int frombio, struct bio *bio,
710 char *pa = page_address(page);
715 if (bio->bi_sector >= sector)
716 page_offset = (signed)(bio->bi_sector - sector) * 512;
718 page_offset = (signed)(sector - bio->bi_sector) * -512;
719 bio_for_each_segment(bvl, bio, i) {
720 int len = bio_iovec_idx(bio,i)->bv_len;
724 if (page_offset < 0) {
725 b_offset = -page_offset;
726 page_offset += b_offset;
730 if (len > 0 && page_offset + len > STRIPE_SIZE)
731 clen = STRIPE_SIZE - page_offset;
735 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
737 memcpy(pa+page_offset, ba+b_offset, clen);
739 memcpy(ba+b_offset, pa+page_offset, clen);
740 __bio_kunmap_atomic(ba, KM_USER0);
742 if (clen < len) /* hit end of page */
748 #define check_xor() do { \
749 if (count == MAX_XOR_BLOCKS) { \
750 xor_block(count, STRIPE_SIZE, ptr); \
755 /* Compute P and Q syndromes */
756 static void compute_parity(struct stripe_head *sh, int method)
758 raid6_conf_t *conf = sh->raid_conf;
759 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
761 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
764 qd_idx = raid6_next_disk(pd_idx, disks);
765 d0_idx = raid6_next_disk(qd_idx, disks);
767 PRINTK("compute_parity, stripe %llu, method %d\n",
768 (unsigned long long)sh->sector, method);
771 case READ_MODIFY_WRITE:
772 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
773 case RECONSTRUCT_WRITE:
774 for (i= disks; i-- ;)
775 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
776 chosen = sh->dev[i].towrite;
777 sh->dev[i].towrite = NULL;
779 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
780 wake_up(&conf->wait_for_overlap);
782 if (sh->dev[i].written) BUG();
783 sh->dev[i].written = chosen;
787 BUG(); /* Not implemented yet */
790 for (i = disks; i--;)
791 if (sh->dev[i].written) {
792 sector_t sector = sh->dev[i].sector;
793 struct bio *wbi = sh->dev[i].written;
794 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
795 copy_data(1, wbi, sh->dev[i].page, sector);
796 wbi = r5_next_bio(wbi, sector);
799 set_bit(R5_LOCKED, &sh->dev[i].flags);
800 set_bit(R5_UPTODATE, &sh->dev[i].flags);
804 // case RECONSTRUCT_WRITE:
805 // case CHECK_PARITY:
806 // case UPDATE_PARITY:
807 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
808 /* FIX: Is this ordering of drives even remotely optimal? */
812 ptrs[count++] = page_address(sh->dev[i].page);
813 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
814 printk("block %d/%d not uptodate on parity calc\n", i,count);
815 i = raid6_next_disk(i, disks);
816 } while ( i != d0_idx );
820 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
823 case RECONSTRUCT_WRITE:
824 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
825 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
826 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
827 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
830 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
831 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
836 /* Compute one missing block */
837 static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
839 raid6_conf_t *conf = sh->raid_conf;
840 int i, count, disks = conf->raid_disks;
841 void *ptr[MAX_XOR_BLOCKS], *p;
842 int pd_idx = sh->pd_idx;
843 int qd_idx = raid6_next_disk(pd_idx, disks);
845 PRINTK("compute_block_1, stripe %llu, idx %d\n",
846 (unsigned long long)sh->sector, dd_idx);
848 if ( dd_idx == qd_idx ) {
849 /* We're actually computing the Q drive */
850 compute_parity(sh, UPDATE_PARITY);
852 ptr[0] = page_address(sh->dev[dd_idx].page);
853 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
855 for (i = disks ; i--; ) {
856 if (i == dd_idx || i == qd_idx)
858 p = page_address(sh->dev[i].page);
859 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
862 printk("compute_block() %d, stripe %llu, %d"
863 " not present\n", dd_idx,
864 (unsigned long long)sh->sector, i);
869 xor_block(count, STRIPE_SIZE, ptr);
870 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
871 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
875 /* Compute two missing blocks */
876 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
878 raid6_conf_t *conf = sh->raid_conf;
879 int i, count, disks = conf->raid_disks;
880 int pd_idx = sh->pd_idx;
881 int qd_idx = raid6_next_disk(pd_idx, disks);
882 int d0_idx = raid6_next_disk(qd_idx, disks);
885 /* faila and failb are disk numbers relative to d0_idx */
886 /* pd_idx become disks-2 and qd_idx become disks-1 */
887 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
888 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
890 BUG_ON(faila == failb);
891 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
893 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
894 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
896 if ( failb == disks-1 ) {
897 /* Q disk is one of the missing disks */
898 if ( faila == disks-2 ) {
899 /* Missing P+Q, just recompute */
900 compute_parity(sh, UPDATE_PARITY);
903 /* We're missing D+Q; recompute D from P */
904 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
905 compute_parity(sh, UPDATE_PARITY); /* Is this necessary? */
910 /* We're missing D+P or D+D; build pointer table */
912 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
918 ptrs[count++] = page_address(sh->dev[i].page);
919 i = raid6_next_disk(i, disks);
920 if (i != dd_idx1 && i != dd_idx2 &&
921 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
922 printk("compute_2 with missing block %d/%d\n", count, i);
923 } while ( i != d0_idx );
925 if ( failb == disks-2 ) {
926 /* We're missing D+P. */
927 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
929 /* We're missing D+D. */
930 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
933 /* Both the above update both missing blocks */
934 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
935 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
941 * Each stripe/dev can have one or more bion attached.
942 * toread/towrite point to the first in a chain.
943 * The bi_next chain must be in order.
945 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
948 raid6_conf_t *conf = sh->raid_conf;
951 PRINTK("adding bh b#%llu to stripe s#%llu\n",
952 (unsigned long long)bi->bi_sector,
953 (unsigned long long)sh->sector);
956 spin_lock(&sh->lock);
957 spin_lock_irq(&conf->device_lock);
959 bip = &sh->dev[dd_idx].towrite;
960 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
963 bip = &sh->dev[dd_idx].toread;
964 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
965 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
967 bip = &(*bip)->bi_next;
969 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
972 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
977 bi->bi_phys_segments ++;
978 spin_unlock_irq(&conf->device_lock);
979 spin_unlock(&sh->lock);
981 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
982 (unsigned long long)bi->bi_sector,
983 (unsigned long long)sh->sector, dd_idx);
985 if (conf->mddev->bitmap && firstwrite) {
986 sh->bm_seq = conf->seq_write;
987 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
989 set_bit(STRIPE_BIT_DELAY, &sh->state);
993 /* check if page is covered */
994 sector_t sector = sh->dev[dd_idx].sector;
995 for (bi=sh->dev[dd_idx].towrite;
996 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
997 bi && bi->bi_sector <= sector;
998 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
999 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1000 sector = bi->bi_sector + (bi->bi_size>>9);
1002 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1003 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1008 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1009 spin_unlock_irq(&conf->device_lock);
1010 spin_unlock(&sh->lock);
1015 static int page_is_zero(struct page *p)
1017 char *a = page_address(p);
1018 return ((*(u32*)a) == 0 &&
1019 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1022 * handle_stripe - do things to a stripe.
1024 * We lock the stripe and then examine the state of various bits
1025 * to see what needs to be done.
1027 * return some read request which now have data
1028 * return some write requests which are safely on disc
1029 * schedule a read on some buffers
1030 * schedule a write of some buffers
1031 * return confirmation of parity correctness
1033 * Parity calculations are done inside the stripe lock
1034 * buffers are taken off read_list or write_list, and bh_cache buffers
1035 * get BH_Lock set before the stripe lock is released.
1039 static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
1041 raid6_conf_t *conf = sh->raid_conf;
1042 int disks = conf->raid_disks;
1043 struct bio *return_bi= NULL;
1047 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1048 int non_overwrite = 0;
1049 int failed_num[2] = {0, 0};
1050 struct r5dev *dev, *pdev, *qdev;
1051 int pd_idx = sh->pd_idx;
1052 int qd_idx = raid6_next_disk(pd_idx, disks);
1053 int p_failed, q_failed;
1055 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1056 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1059 spin_lock(&sh->lock);
1060 clear_bit(STRIPE_HANDLE, &sh->state);
1061 clear_bit(STRIPE_DELAYED, &sh->state);
1063 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1064 /* Now to look around and see what can be done */
1067 for (i=disks; i--; ) {
1070 clear_bit(R5_Insync, &dev->flags);
1072 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1073 i, dev->flags, dev->toread, dev->towrite, dev->written);
1074 /* maybe we can reply to a read */
1075 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1076 struct bio *rbi, *rbi2;
1077 PRINTK("Return read for disc %d\n", i);
1078 spin_lock_irq(&conf->device_lock);
1081 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1082 wake_up(&conf->wait_for_overlap);
1083 spin_unlock_irq(&conf->device_lock);
1084 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1085 copy_data(0, rbi, dev->page, dev->sector);
1086 rbi2 = r5_next_bio(rbi, dev->sector);
1087 spin_lock_irq(&conf->device_lock);
1088 if (--rbi->bi_phys_segments == 0) {
1089 rbi->bi_next = return_bi;
1092 spin_unlock_irq(&conf->device_lock);
1097 /* now count some things */
1098 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1099 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1102 if (dev->toread) to_read++;
1105 if (!test_bit(R5_OVERWRITE, &dev->flags))
1108 if (dev->written) written++;
1109 rdev = rcu_dereference(conf->disks[i].rdev);
1110 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1111 /* The ReadError flag will just be confusing now */
1112 clear_bit(R5_ReadError, &dev->flags);
1113 clear_bit(R5_ReWrite, &dev->flags);
1115 if (!rdev || !test_bit(In_sync, &rdev->flags)
1116 || test_bit(R5_ReadError, &dev->flags)) {
1118 failed_num[failed] = i;
1121 set_bit(R5_Insync, &dev->flags);
1124 PRINTK("locked=%d uptodate=%d to_read=%d"
1125 " to_write=%d failed=%d failed_num=%d,%d\n",
1126 locked, uptodate, to_read, to_write, failed,
1127 failed_num[0], failed_num[1]);
1128 /* check if the array has lost >2 devices and, if so, some requests might
1131 if (failed > 2 && to_read+to_write+written) {
1132 for (i=disks; i--; ) {
1135 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1138 rdev = rcu_dereference(conf->disks[i].rdev);
1139 if (rdev && test_bit(In_sync, &rdev->flags))
1140 /* multiple read failures in one stripe */
1141 md_error(conf->mddev, rdev);
1145 spin_lock_irq(&conf->device_lock);
1146 /* fail all writes first */
1147 bi = sh->dev[i].towrite;
1148 sh->dev[i].towrite = NULL;
1149 if (bi) { to_write--; bitmap_end = 1; }
1151 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1152 wake_up(&conf->wait_for_overlap);
1154 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1155 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1156 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1157 if (--bi->bi_phys_segments == 0) {
1158 md_write_end(conf->mddev);
1159 bi->bi_next = return_bi;
1164 /* and fail all 'written' */
1165 bi = sh->dev[i].written;
1166 sh->dev[i].written = NULL;
1167 if (bi) bitmap_end = 1;
1168 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1169 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1170 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1171 if (--bi->bi_phys_segments == 0) {
1172 md_write_end(conf->mddev);
1173 bi->bi_next = return_bi;
1179 /* fail any reads if this device is non-operational */
1180 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1181 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1182 bi = sh->dev[i].toread;
1183 sh->dev[i].toread = NULL;
1184 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1185 wake_up(&conf->wait_for_overlap);
1187 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1188 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1189 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1190 if (--bi->bi_phys_segments == 0) {
1191 bi->bi_next = return_bi;
1197 spin_unlock_irq(&conf->device_lock);
1199 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1200 STRIPE_SECTORS, 0, 0);
1203 if (failed > 2 && syncing) {
1204 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1205 clear_bit(STRIPE_SYNCING, &sh->state);
1210 * might be able to return some write requests if the parity blocks
1211 * are safe, or on a failed drive
1213 pdev = &sh->dev[pd_idx];
1214 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
1215 || (failed >= 2 && failed_num[1] == pd_idx);
1216 qdev = &sh->dev[qd_idx];
1217 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
1218 || (failed >= 2 && failed_num[1] == qd_idx);
1221 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
1222 && !test_bit(R5_LOCKED, &pdev->flags)
1223 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
1224 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
1225 && !test_bit(R5_LOCKED, &qdev->flags)
1226 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
1227 /* any written block on an uptodate or failed drive can be
1228 * returned. Note that if we 'wrote' to a failed drive,
1229 * it will be UPTODATE, but never LOCKED, so we don't need
1230 * to test 'failed' directly.
1232 for (i=disks; i--; )
1233 if (sh->dev[i].written) {
1235 if (!test_bit(R5_LOCKED, &dev->flags) &&
1236 test_bit(R5_UPTODATE, &dev->flags) ) {
1237 /* We can return any write requests */
1239 struct bio *wbi, *wbi2;
1240 PRINTK("Return write for stripe %llu disc %d\n",
1241 (unsigned long long)sh->sector, i);
1242 spin_lock_irq(&conf->device_lock);
1244 dev->written = NULL;
1245 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1246 wbi2 = r5_next_bio(wbi, dev->sector);
1247 if (--wbi->bi_phys_segments == 0) {
1248 md_write_end(conf->mddev);
1249 wbi->bi_next = return_bi;
1254 if (dev->towrite == NULL)
1256 spin_unlock_irq(&conf->device_lock);
1258 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1260 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1265 /* Now we might consider reading some blocks, either to check/generate
1266 * parity, or to satisfy requests
1267 * or to load a block that is being partially written.
1269 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
1270 for (i=disks; i--;) {
1272 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1274 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1276 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
1277 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
1280 /* we would like to get this block, possibly
1281 * by computing it, but we might not be able to
1283 if (uptodate == disks-1) {
1284 PRINTK("Computing stripe %llu block %d\n",
1285 (unsigned long long)sh->sector, i);
1286 compute_block_1(sh, i, 0);
1288 } else if ( uptodate == disks-2 && failed >= 2 ) {
1289 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1291 for (other=disks; other--;) {
1294 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
1298 PRINTK("Computing stripe %llu blocks %d,%d\n",
1299 (unsigned long long)sh->sector, i, other);
1300 compute_block_2(sh, i, other);
1302 } else if (test_bit(R5_Insync, &dev->flags)) {
1303 set_bit(R5_LOCKED, &dev->flags);
1304 set_bit(R5_Wantread, &dev->flags);
1306 /* if I am just reading this block and we don't have
1307 a failed drive, or any pending writes then sidestep the cache */
1308 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1309 ! syncing && !failed && !to_write) {
1310 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1311 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1315 PRINTK("Reading block %d (sync=%d)\n",
1320 set_bit(STRIPE_HANDLE, &sh->state);
1323 /* now to consider writing and what else, if anything should be read */
1325 int rcw=0, must_compute=0;
1326 for (i=disks ; i--;) {
1328 /* Would I have to read this buffer for reconstruct_write */
1329 if (!test_bit(R5_OVERWRITE, &dev->flags)
1330 && i != pd_idx && i != qd_idx
1331 && (!test_bit(R5_LOCKED, &dev->flags)
1333 || sh->bh_page[i] != bh->b_page
1336 !test_bit(R5_UPTODATE, &dev->flags)) {
1337 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1339 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
1344 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1345 (unsigned long long)sh->sector, rcw, must_compute);
1346 set_bit(STRIPE_HANDLE, &sh->state);
1349 /* want reconstruct write, but need to get some data */
1350 for (i=disks; i--;) {
1352 if (!test_bit(R5_OVERWRITE, &dev->flags)
1353 && !(failed == 0 && (i == pd_idx || i == qd_idx))
1354 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1355 test_bit(R5_Insync, &dev->flags)) {
1356 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1358 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1359 (unsigned long long)sh->sector, i);
1360 set_bit(R5_LOCKED, &dev->flags);
1361 set_bit(R5_Wantread, &dev->flags);
1364 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1365 (unsigned long long)sh->sector, i);
1366 set_bit(STRIPE_DELAYED, &sh->state);
1367 set_bit(STRIPE_HANDLE, &sh->state);
1371 /* now if nothing is locked, and if we have enough data, we can start a write request */
1372 if (locked == 0 && rcw == 0 &&
1373 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1374 if ( must_compute > 0 ) {
1375 /* We have failed blocks and need to compute them */
1378 case 1: compute_block_1(sh, failed_num[0], 0); break;
1379 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
1380 default: BUG(); /* This request should have been failed? */
1384 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
1385 compute_parity(sh, RECONSTRUCT_WRITE);
1386 /* now every locked buffer is ready to be written */
1388 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1389 PRINTK("Writing stripe %llu block %d\n",
1390 (unsigned long long)sh->sector, i);
1392 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1394 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
1395 set_bit(STRIPE_INSYNC, &sh->state);
1397 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1398 atomic_dec(&conf->preread_active_stripes);
1399 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1400 md_wakeup_thread(conf->mddev->thread);
1405 /* maybe we need to check and possibly fix the parity for this stripe
1406 * Any reads will already have been scheduled, so we just see if enough data
1409 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
1410 int update_p = 0, update_q = 0;
1413 set_bit(STRIPE_HANDLE, &sh->state);
1416 BUG_ON(uptodate < disks);
1417 /* Want to check and possibly repair P and Q.
1418 * However there could be one 'failed' device, in which
1419 * case we can only check one of them, possibly using the
1420 * other to generate missing data
1423 /* If !tmp_page, we cannot do the calculations,
1424 * but as we have set STRIPE_HANDLE, we will soon be called
1425 * by stripe_handle with a tmp_page - just wait until then.
1428 if (failed == q_failed) {
1429 /* The only possible failed device holds 'Q', so it makes
1430 * sense to check P (If anything else were failed, we would
1431 * have used P to recreate it).
1433 compute_block_1(sh, pd_idx, 1);
1434 if (!page_is_zero(sh->dev[pd_idx].page)) {
1435 compute_block_1(sh,pd_idx,0);
1439 if (!q_failed && failed < 2) {
1440 /* q is not failed, and we didn't use it to generate
1441 * anything, so it makes sense to check it
1443 memcpy(page_address(tmp_page),
1444 page_address(sh->dev[qd_idx].page),
1446 compute_parity(sh, UPDATE_PARITY);
1447 if (memcmp(page_address(tmp_page),
1448 page_address(sh->dev[qd_idx].page),
1450 clear_bit(STRIPE_INSYNC, &sh->state);
1454 if (update_p || update_q) {
1455 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1456 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1457 /* don't try to repair!! */
1458 update_p = update_q = 0;
1461 /* now write out any block on a failed drive,
1462 * or P or Q if they need it
1466 dev = &sh->dev[failed_num[1]];
1468 set_bit(R5_LOCKED, &dev->flags);
1469 set_bit(R5_Wantwrite, &dev->flags);
1472 dev = &sh->dev[failed_num[0]];
1474 set_bit(R5_LOCKED, &dev->flags);
1475 set_bit(R5_Wantwrite, &dev->flags);
1479 dev = &sh->dev[pd_idx];
1481 set_bit(R5_LOCKED, &dev->flags);
1482 set_bit(R5_Wantwrite, &dev->flags);
1485 dev = &sh->dev[qd_idx];
1487 set_bit(R5_LOCKED, &dev->flags);
1488 set_bit(R5_Wantwrite, &dev->flags);
1490 clear_bit(STRIPE_DEGRADED, &sh->state);
1492 set_bit(STRIPE_INSYNC, &sh->state);
1496 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1497 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1498 clear_bit(STRIPE_SYNCING, &sh->state);
1501 /* If the failed drives are just a ReadError, then we might need
1502 * to progress the repair/check process
1504 if (failed <= 2 && ! conf->mddev->ro)
1505 for (i=0; i<failed;i++) {
1506 dev = &sh->dev[failed_num[i]];
1507 if (test_bit(R5_ReadError, &dev->flags)
1508 && !test_bit(R5_LOCKED, &dev->flags)
1509 && test_bit(R5_UPTODATE, &dev->flags)
1511 if (!test_bit(R5_ReWrite, &dev->flags)) {
1512 set_bit(R5_Wantwrite, &dev->flags);
1513 set_bit(R5_ReWrite, &dev->flags);
1514 set_bit(R5_LOCKED, &dev->flags);
1516 /* let's read it back */
1517 set_bit(R5_Wantread, &dev->flags);
1518 set_bit(R5_LOCKED, &dev->flags);
1522 spin_unlock(&sh->lock);
1524 while ((bi=return_bi)) {
1525 int bytes = bi->bi_size;
1527 return_bi = bi->bi_next;
1530 bi->bi_end_io(bi, bytes, 0);
1532 for (i=disks; i-- ;) {
1536 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1538 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1543 bi = &sh->dev[i].req;
1547 bi->bi_end_io = raid6_end_write_request;
1549 bi->bi_end_io = raid6_end_read_request;
1552 rdev = rcu_dereference(conf->disks[i].rdev);
1553 if (rdev && test_bit(Faulty, &rdev->flags))
1556 atomic_inc(&rdev->nr_pending);
1561 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1563 bi->bi_bdev = rdev->bdev;
1564 PRINTK("for %llu schedule op %ld on disc %d\n",
1565 (unsigned long long)sh->sector, bi->bi_rw, i);
1566 atomic_inc(&sh->count);
1567 bi->bi_sector = sh->sector + rdev->data_offset;
1568 bi->bi_flags = 1 << BIO_UPTODATE;
1570 bi->bi_max_vecs = 1;
1572 bi->bi_io_vec = &sh->dev[i].vec;
1573 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1574 bi->bi_io_vec[0].bv_offset = 0;
1575 bi->bi_size = STRIPE_SIZE;
1578 test_bit(R5_ReWrite, &sh->dev[i].flags))
1579 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1580 generic_make_request(bi);
1583 set_bit(STRIPE_DEGRADED, &sh->state);
1584 PRINTK("skip op %ld on disc %d for sector %llu\n",
1585 bi->bi_rw, i, (unsigned long long)sh->sector);
1586 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1587 set_bit(STRIPE_HANDLE, &sh->state);
1592 static void raid6_activate_delayed(raid6_conf_t *conf)
1594 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1595 while (!list_empty(&conf->delayed_list)) {
1596 struct list_head *l = conf->delayed_list.next;
1597 struct stripe_head *sh;
1598 sh = list_entry(l, struct stripe_head, lru);
1600 clear_bit(STRIPE_DELAYED, &sh->state);
1601 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1602 atomic_inc(&conf->preread_active_stripes);
1603 list_add_tail(&sh->lru, &conf->handle_list);
1608 static void activate_bit_delay(raid6_conf_t *conf)
1610 /* device_lock is held */
1611 struct list_head head;
1612 list_add(&head, &conf->bitmap_list);
1613 list_del_init(&conf->bitmap_list);
1614 while (!list_empty(&head)) {
1615 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1616 list_del_init(&sh->lru);
1617 atomic_inc(&sh->count);
1618 __release_stripe(conf, sh);
1622 static void unplug_slaves(mddev_t *mddev)
1624 raid6_conf_t *conf = mddev_to_conf(mddev);
1628 for (i=0; i<mddev->raid_disks; i++) {
1629 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1630 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
1631 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1633 atomic_inc(&rdev->nr_pending);
1636 if (r_queue->unplug_fn)
1637 r_queue->unplug_fn(r_queue);
1639 rdev_dec_pending(rdev, mddev);
1646 static void raid6_unplug_device(request_queue_t *q)
1648 mddev_t *mddev = q->queuedata;
1649 raid6_conf_t *conf = mddev_to_conf(mddev);
1650 unsigned long flags;
1652 spin_lock_irqsave(&conf->device_lock, flags);
1654 if (blk_remove_plug(q)) {
1656 raid6_activate_delayed(conf);
1658 md_wakeup_thread(mddev->thread);
1660 spin_unlock_irqrestore(&conf->device_lock, flags);
1662 unplug_slaves(mddev);
1665 static int raid6_issue_flush(request_queue_t *q, struct gendisk *disk,
1666 sector_t *error_sector)
1668 mddev_t *mddev = q->queuedata;
1669 raid6_conf_t *conf = mddev_to_conf(mddev);
1673 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1674 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1675 if (rdev && !test_bit(Faulty, &rdev->flags)) {
1676 struct block_device *bdev = rdev->bdev;
1677 request_queue_t *r_queue = bdev_get_queue(bdev);
1679 if (!r_queue->issue_flush_fn)
1682 atomic_inc(&rdev->nr_pending);
1684 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1686 rdev_dec_pending(rdev, mddev);
1695 static inline void raid6_plug_device(raid6_conf_t *conf)
1697 spin_lock_irq(&conf->device_lock);
1698 blk_plug_device(conf->mddev->queue);
1699 spin_unlock_irq(&conf->device_lock);
1702 static int make_request (request_queue_t *q, struct bio * bi)
1704 mddev_t *mddev = q->queuedata;
1705 raid6_conf_t *conf = mddev_to_conf(mddev);
1706 const unsigned int raid_disks = conf->raid_disks;
1707 const unsigned int data_disks = raid_disks - 2;
1708 unsigned int dd_idx, pd_idx;
1709 sector_t new_sector;
1710 sector_t logical_sector, last_sector;
1711 struct stripe_head *sh;
1712 const int rw = bio_data_dir(bi);
1714 if (unlikely(bio_barrier(bi))) {
1715 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1719 md_write_start(mddev, bi);
1721 disk_stat_inc(mddev->gendisk, ios[rw]);
1722 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1724 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1725 last_sector = bi->bi_sector + (bi->bi_size>>9);
1728 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1730 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1733 new_sector = raid6_compute_sector(logical_sector,
1734 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1736 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1737 (unsigned long long)new_sector,
1738 (unsigned long long)logical_sector);
1741 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1742 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1744 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1745 /* Add failed due to overlap. Flush everything
1748 raid6_unplug_device(mddev->queue);
1753 finish_wait(&conf->wait_for_overlap, &w);
1754 raid6_plug_device(conf);
1755 handle_stripe(sh, NULL);
1758 /* cannot get stripe for read-ahead, just give-up */
1759 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1760 finish_wait(&conf->wait_for_overlap, &w);
1765 spin_lock_irq(&conf->device_lock);
1766 if (--bi->bi_phys_segments == 0) {
1767 int bytes = bi->bi_size;
1770 md_write_end(mddev);
1772 bi->bi_end_io(bi, bytes, 0);
1774 spin_unlock_irq(&conf->device_lock);
1778 /* FIXME go_faster isn't used */
1779 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1781 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1782 struct stripe_head *sh;
1783 int sectors_per_chunk = conf->chunk_size >> 9;
1785 unsigned long stripe;
1788 sector_t first_sector;
1789 int raid_disks = conf->raid_disks;
1790 int data_disks = raid_disks - 2;
1791 sector_t max_sector = mddev->size << 1;
1793 int still_degraded = 0;
1796 if (sector_nr >= max_sector) {
1797 /* just being told to finish up .. nothing much to do */
1798 unplug_slaves(mddev);
1800 if (mddev->curr_resync < max_sector) /* aborted */
1801 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1803 else /* completed sync */
1805 bitmap_close_sync(mddev->bitmap);
1809 /* if there are 2 or more failed drives and we are trying
1810 * to resync, then assert that we are finished, because there is
1811 * nothing we can do.
1813 if (mddev->degraded >= 2 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1814 sector_t rv = (mddev->size << 1) - sector_nr;
1818 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1819 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
1820 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1821 /* we can skip this block, and probably more */
1822 sync_blocks /= STRIPE_SECTORS;
1824 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1828 chunk_offset = sector_div(x, sectors_per_chunk);
1830 BUG_ON(x != stripe);
1832 first_sector = raid6_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1833 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1834 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1836 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1837 /* make sure we don't swamp the stripe cache if someone else
1838 * is trying to get access
1840 schedule_timeout_uninterruptible(1);
1842 /* Need to check if array will still be degraded after recovery/resync
1843 * We don't need to check the 'failed' flag as when that gets set,
1846 for (i=0; i<mddev->raid_disks; i++)
1847 if (conf->disks[i].rdev == NULL)
1850 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
1852 spin_lock(&sh->lock);
1853 set_bit(STRIPE_SYNCING, &sh->state);
1854 clear_bit(STRIPE_INSYNC, &sh->state);
1855 spin_unlock(&sh->lock);
1857 handle_stripe(sh, NULL);
1860 return STRIPE_SECTORS;
1864 * This is our raid6 kernel thread.
1866 * We scan the hash table for stripes which can be handled now.
1867 * During the scan, completed stripes are saved for us by the interrupt
1868 * handler, so that they will not have to wait for our next wakeup.
1870 static void raid6d (mddev_t *mddev)
1872 struct stripe_head *sh;
1873 raid6_conf_t *conf = mddev_to_conf(mddev);
1876 PRINTK("+++ raid6d active\n");
1878 md_check_recovery(mddev);
1881 spin_lock_irq(&conf->device_lock);
1883 struct list_head *first;
1885 if (conf->seq_flush - conf->seq_write > 0) {
1886 int seq = conf->seq_flush;
1887 spin_unlock_irq(&conf->device_lock);
1888 bitmap_unplug(mddev->bitmap);
1889 spin_lock_irq(&conf->device_lock);
1890 conf->seq_write = seq;
1891 activate_bit_delay(conf);
1894 if (list_empty(&conf->handle_list) &&
1895 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1896 !blk_queue_plugged(mddev->queue) &&
1897 !list_empty(&conf->delayed_list))
1898 raid6_activate_delayed(conf);
1900 if (list_empty(&conf->handle_list))
1903 first = conf->handle_list.next;
1904 sh = list_entry(first, struct stripe_head, lru);
1906 list_del_init(first);
1907 atomic_inc(&sh->count);
1908 if (atomic_read(&sh->count)!= 1)
1910 spin_unlock_irq(&conf->device_lock);
1913 handle_stripe(sh, conf->spare_page);
1916 spin_lock_irq(&conf->device_lock);
1918 PRINTK("%d stripes handled\n", handled);
1920 spin_unlock_irq(&conf->device_lock);
1922 unplug_slaves(mddev);
1924 PRINTK("--- raid6d inactive\n");
1928 raid6_show_stripe_cache_size(mddev_t *mddev, char *page)
1930 raid6_conf_t *conf = mddev_to_conf(mddev);
1932 return sprintf(page, "%d\n", conf->max_nr_stripes);
1938 raid6_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
1940 raid6_conf_t *conf = mddev_to_conf(mddev);
1943 if (len >= PAGE_SIZE)
1948 new = simple_strtoul(page, &end, 10);
1949 if (!*page || (*end && *end != '\n') )
1951 if (new <= 16 || new > 32768)
1953 while (new < conf->max_nr_stripes) {
1954 if (drop_one_stripe(conf))
1955 conf->max_nr_stripes--;
1959 while (new > conf->max_nr_stripes) {
1960 if (grow_one_stripe(conf))
1961 conf->max_nr_stripes++;
1967 static struct md_sysfs_entry
1968 raid6_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
1969 raid6_show_stripe_cache_size,
1970 raid6_store_stripe_cache_size);
1973 stripe_cache_active_show(mddev_t *mddev, char *page)
1975 raid6_conf_t *conf = mddev_to_conf(mddev);
1977 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
1982 static struct md_sysfs_entry
1983 raid6_stripecache_active = __ATTR_RO(stripe_cache_active);
1985 static struct attribute *raid6_attrs[] = {
1986 &raid6_stripecache_size.attr,
1987 &raid6_stripecache_active.attr,
1990 static struct attribute_group raid6_attrs_group = {
1992 .attrs = raid6_attrs,
1995 static int run(mddev_t *mddev)
1998 int raid_disk, memory;
2000 struct disk_info *disk;
2001 struct list_head *tmp;
2003 if (mddev->level != 6) {
2004 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev), mddev->level);
2008 mddev->private = kzalloc(sizeof (raid6_conf_t)
2009 + mddev->raid_disks * sizeof(struct disk_info),
2011 if ((conf = mddev->private) == NULL)
2013 conf->mddev = mddev;
2015 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
2018 conf->spare_page = alloc_page(GFP_KERNEL);
2019 if (!conf->spare_page)
2022 spin_lock_init(&conf->device_lock);
2023 init_waitqueue_head(&conf->wait_for_stripe);
2024 init_waitqueue_head(&conf->wait_for_overlap);
2025 INIT_LIST_HEAD(&conf->handle_list);
2026 INIT_LIST_HEAD(&conf->delayed_list);
2027 INIT_LIST_HEAD(&conf->bitmap_list);
2028 INIT_LIST_HEAD(&conf->inactive_list);
2029 atomic_set(&conf->active_stripes, 0);
2030 atomic_set(&conf->preread_active_stripes, 0);
2032 PRINTK("raid6: run(%s) called.\n", mdname(mddev));
2034 ITERATE_RDEV(mddev,rdev,tmp) {
2035 raid_disk = rdev->raid_disk;
2036 if (raid_disk >= mddev->raid_disks
2039 disk = conf->disks + raid_disk;
2043 if (test_bit(In_sync, &rdev->flags)) {
2044 char b[BDEVNAME_SIZE];
2045 printk(KERN_INFO "raid6: device %s operational as raid"
2046 " disk %d\n", bdevname(rdev->bdev,b),
2048 conf->working_disks++;
2052 conf->raid_disks = mddev->raid_disks;
2055 * 0 for a fully functional array, 1 or 2 for a degraded array.
2057 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
2058 conf->mddev = mddev;
2059 conf->chunk_size = mddev->chunk_size;
2060 conf->level = mddev->level;
2061 conf->algorithm = mddev->layout;
2062 conf->max_nr_stripes = NR_STRIPES;
2064 /* device size must be a multiple of chunk size */
2065 mddev->size &= ~(mddev->chunk_size/1024 -1);
2066 mddev->resync_max_sectors = mddev->size << 1;
2068 if (conf->raid_disks < 4) {
2069 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
2070 mdname(mddev), conf->raid_disks);
2073 if (!conf->chunk_size || conf->chunk_size % 4) {
2074 printk(KERN_ERR "raid6: invalid chunk size %d for %s\n",
2075 conf->chunk_size, mdname(mddev));
2078 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
2080 "raid6: unsupported parity algorithm %d for %s\n",
2081 conf->algorithm, mdname(mddev));
2084 if (mddev->degraded > 2) {
2085 printk(KERN_ERR "raid6: not enough operational devices for %s"
2086 " (%d/%d failed)\n",
2087 mdname(mddev), conf->failed_disks, conf->raid_disks);
2091 if (mddev->degraded > 0 &&
2092 mddev->recovery_cp != MaxSector) {
2093 if (mddev->ok_start_degraded)
2094 printk(KERN_WARNING "raid6: starting dirty degraded array:%s"
2095 "- data corruption possible.\n",
2098 printk(KERN_ERR "raid6: cannot start dirty degraded array"
2099 " for %s\n", mdname(mddev));
2105 mddev->thread = md_register_thread(raid6d, mddev, "%s_raid6");
2106 if (!mddev->thread) {
2108 "raid6: couldn't allocate thread for %s\n",
2114 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
2115 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
2116 if (grow_stripes(conf, conf->max_nr_stripes)) {
2118 "raid6: couldn't allocate %dkB for buffers\n", memory);
2119 shrink_stripes(conf);
2120 md_unregister_thread(mddev->thread);
2123 printk(KERN_INFO "raid6: allocated %dkB for %s\n",
2124 memory, mdname(mddev));
2126 if (mddev->degraded == 0)
2127 printk(KERN_INFO "raid6: raid level %d set %s active with %d out of %d"
2128 " devices, algorithm %d\n", conf->level, mdname(mddev),
2129 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
2132 printk(KERN_ALERT "raid6: raid level %d set %s active with %d"
2133 " out of %d devices, algorithm %d\n", conf->level,
2134 mdname(mddev), mddev->raid_disks - mddev->degraded,
2135 mddev->raid_disks, conf->algorithm);
2137 print_raid6_conf(conf);
2139 /* read-ahead size must cover two whole stripes, which is
2140 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
2143 int stripe = (mddev->raid_disks-2) * mddev->chunk_size
2145 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
2146 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
2149 /* Ok, everything is just fine now */
2150 mddev->array_size = mddev->size * (mddev->raid_disks - 2);
2152 mddev->queue->unplug_fn = raid6_unplug_device;
2153 mddev->queue->issue_flush_fn = raid6_issue_flush;
2157 print_raid6_conf(conf);
2158 safe_put_page(conf->spare_page);
2159 kfree(conf->stripe_hashtbl);
2162 mddev->private = NULL;
2163 printk(KERN_ALERT "raid6: failed to run raid set %s\n", mdname(mddev));
2169 static int stop (mddev_t *mddev)
2171 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2173 md_unregister_thread(mddev->thread);
2174 mddev->thread = NULL;
2175 shrink_stripes(conf);
2176 kfree(conf->stripe_hashtbl);
2177 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2178 sysfs_remove_group(&mddev->kobj, &raid6_attrs_group);
2180 mddev->private = NULL;
2185 static void print_sh (struct seq_file *seq, struct stripe_head *sh)
2189 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
2190 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2191 seq_printf(seq, "sh %llu, count %d.\n",
2192 (unsigned long long)sh->sector, atomic_read(&sh->count));
2193 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
2194 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
2195 seq_printf(seq, "(cache%d: %p %ld) ",
2196 i, sh->dev[i].page, sh->dev[i].flags);
2198 seq_printf(seq, "\n");
2201 static void printall (struct seq_file *seq, raid6_conf_t *conf)
2203 struct stripe_head *sh;
2204 struct hlist_node *hn;
2207 spin_lock_irq(&conf->device_lock);
2208 for (i = 0; i < NR_HASH; i++) {
2209 sh = conf->stripe_hashtbl[i];
2210 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
2211 if (sh->raid_conf != conf)
2216 spin_unlock_irq(&conf->device_lock);
2220 static void status (struct seq_file *seq, mddev_t *mddev)
2222 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2225 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2226 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2227 for (i = 0; i < conf->raid_disks; i++)
2228 seq_printf (seq, "%s",
2229 conf->disks[i].rdev &&
2230 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
2231 seq_printf (seq, "]");
2233 seq_printf (seq, "\n");
2234 printall(seq, conf);
2238 static void print_raid6_conf (raid6_conf_t *conf)
2241 struct disk_info *tmp;
2243 printk("RAID6 conf printout:\n");
2245 printk("(conf==NULL)\n");
2248 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2249 conf->working_disks, conf->failed_disks);
2251 for (i = 0; i < conf->raid_disks; i++) {
2252 char b[BDEVNAME_SIZE];
2253 tmp = conf->disks + i;
2255 printk(" disk %d, o:%d, dev:%s\n",
2256 i, !test_bit(Faulty, &tmp->rdev->flags),
2257 bdevname(tmp->rdev->bdev,b));
2261 static int raid6_spare_active(mddev_t *mddev)
2264 raid6_conf_t *conf = mddev->private;
2265 struct disk_info *tmp;
2267 for (i = 0; i < conf->raid_disks; i++) {
2268 tmp = conf->disks + i;
2270 && !test_bit(Faulty, &tmp->rdev->flags)
2271 && !test_bit(In_sync, &tmp->rdev->flags)) {
2273 conf->failed_disks--;
2274 conf->working_disks++;
2275 set_bit(In_sync, &tmp->rdev->flags);
2278 print_raid6_conf(conf);
2282 static int raid6_remove_disk(mddev_t *mddev, int number)
2284 raid6_conf_t *conf = mddev->private;
2287 struct disk_info *p = conf->disks + number;
2289 print_raid6_conf(conf);
2292 if (test_bit(In_sync, &rdev->flags) ||
2293 atomic_read(&rdev->nr_pending)) {
2299 if (atomic_read(&rdev->nr_pending)) {
2300 /* lost the race, try later */
2308 print_raid6_conf(conf);
2312 static int raid6_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2314 raid6_conf_t *conf = mddev->private;
2317 struct disk_info *p;
2319 if (mddev->degraded > 2)
2320 /* no point adding a device */
2323 * find the disk ... but prefer rdev->saved_raid_disk
2326 if (rdev->saved_raid_disk >= 0 &&
2327 conf->disks[rdev->saved_raid_disk].rdev == NULL)
2328 disk = rdev->saved_raid_disk;
2331 for ( ; disk < mddev->raid_disks; disk++)
2332 if ((p=conf->disks + disk)->rdev == NULL) {
2333 clear_bit(In_sync, &rdev->flags);
2334 rdev->raid_disk = disk;
2336 if (rdev->saved_raid_disk != disk)
2338 rcu_assign_pointer(p->rdev, rdev);
2341 print_raid6_conf(conf);
2345 static int raid6_resize(mddev_t *mddev, sector_t sectors)
2347 /* no resync is happening, and there is enough space
2348 * on all devices, so we can resize.
2349 * We need to make sure resync covers any new space.
2350 * If the array is shrinking we should possibly wait until
2351 * any io in the removed space completes, but it hardly seems
2354 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2355 mddev->array_size = (sectors * (mddev->raid_disks-2))>>1;
2356 set_capacity(mddev->gendisk, mddev->array_size << 1);
2358 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2359 mddev->recovery_cp = mddev->size << 1;
2360 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2362 mddev->size = sectors /2;
2363 mddev->resync_max_sectors = sectors;
2367 static void raid6_quiesce(mddev_t *mddev, int state)
2369 raid6_conf_t *conf = mddev_to_conf(mddev);
2372 case 1: /* stop all writes */
2373 spin_lock_irq(&conf->device_lock);
2375 wait_event_lock_irq(conf->wait_for_stripe,
2376 atomic_read(&conf->active_stripes) == 0,
2377 conf->device_lock, /* nothing */);
2378 spin_unlock_irq(&conf->device_lock);
2381 case 0: /* re-enable writes */
2382 spin_lock_irq(&conf->device_lock);
2384 wake_up(&conf->wait_for_stripe);
2385 spin_unlock_irq(&conf->device_lock);
2390 static struct mdk_personality raid6_personality =
2394 .owner = THIS_MODULE,
2395 .make_request = make_request,
2399 .error_handler = error,
2400 .hot_add_disk = raid6_add_disk,
2401 .hot_remove_disk= raid6_remove_disk,
2402 .spare_active = raid6_spare_active,
2403 .sync_request = sync_request,
2404 .resize = raid6_resize,
2405 .quiesce = raid6_quiesce,
2408 static int __init raid6_init(void)
2412 e = raid6_select_algo();
2416 return register_md_personality(&raid6_personality);
2419 static void raid6_exit (void)
2421 unregister_md_personality(&raid6_personality);
2424 module_init(raid6_init);
2425 module_exit(raid6_exit);
2426 MODULE_LICENSE("GPL");
2427 MODULE_ALIAS("md-personality-8"); /* RAID6 */
2428 MODULE_ALIAS("md-raid6");
2429 MODULE_ALIAS("md-level-6");