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 <asm/bitops.h>
29 #include <asm/atomic.h>
36 #define NR_STRIPES 256
37 #define STRIPE_SIZE PAGE_SIZE
38 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
39 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
40 #define IO_THRESHOLD 1
42 #define HASH_PAGES_ORDER 0
43 #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_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 && CONFIG_SMP
65 # define CHECK_DEVLOCK() if (!spin_is_locked(&conf->device_lock)) BUG()
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 inline 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);
102 list_add_tail(&sh->lru, &conf->handle_list);
103 md_wakeup_thread(conf->mddev->thread);
105 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
106 atomic_dec(&conf->preread_active_stripes);
107 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
108 md_wakeup_thread(conf->mddev->thread);
110 list_add_tail(&sh->lru, &conf->inactive_list);
111 atomic_dec(&conf->active_stripes);
112 if (!conf->inactive_blocked ||
113 atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
114 wake_up(&conf->wait_for_stripe);
118 static void release_stripe(struct stripe_head *sh)
120 raid6_conf_t *conf = sh->raid_conf;
123 spin_lock_irqsave(&conf->device_lock, flags);
124 __release_stripe(conf, sh);
125 spin_unlock_irqrestore(&conf->device_lock, flags);
128 static void remove_hash(struct stripe_head *sh)
130 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
132 if (sh->hash_pprev) {
134 sh->hash_next->hash_pprev = sh->hash_pprev;
135 *sh->hash_pprev = sh->hash_next;
136 sh->hash_pprev = NULL;
140 static __inline__ void insert_hash(raid6_conf_t *conf, struct stripe_head *sh)
142 struct stripe_head **shp = &stripe_hash(conf, sh->sector);
144 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
147 if ((sh->hash_next = *shp) != NULL)
148 (*shp)->hash_pprev = &sh->hash_next;
150 sh->hash_pprev = shp;
154 /* find an idle stripe, make sure it is unhashed, and return it. */
155 static struct stripe_head *get_free_stripe(raid6_conf_t *conf)
157 struct stripe_head *sh = NULL;
158 struct list_head *first;
161 if (list_empty(&conf->inactive_list))
163 first = conf->inactive_list.next;
164 sh = list_entry(first, struct stripe_head, lru);
165 list_del_init(first);
167 atomic_inc(&conf->active_stripes);
172 static void shrink_buffers(struct stripe_head *sh, int num)
177 for (i=0; i<num ; i++) {
181 sh->dev[i].page = NULL;
182 page_cache_release(p);
186 static int grow_buffers(struct stripe_head *sh, int num)
190 for (i=0; i<num; i++) {
193 if (!(page = alloc_page(GFP_KERNEL))) {
196 sh->dev[i].page = page;
201 static void raid6_build_block (struct stripe_head *sh, int i);
203 static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
205 raid6_conf_t *conf = sh->raid_conf;
206 int disks = conf->raid_disks, i;
208 if (atomic_read(&sh->count) != 0)
210 if (test_bit(STRIPE_HANDLE, &sh->state))
214 PRINTK("init_stripe called, stripe %llu\n",
215 (unsigned long long)sh->sector);
223 for (i=disks; i--; ) {
224 struct r5dev *dev = &sh->dev[i];
226 if (dev->toread || dev->towrite || dev->written ||
227 test_bit(R5_LOCKED, &dev->flags)) {
228 PRINTK("sector=%llx i=%d %p %p %p %d\n",
229 (unsigned long long)sh->sector, i, dev->toread,
230 dev->towrite, dev->written,
231 test_bit(R5_LOCKED, &dev->flags));
235 raid6_build_block(sh, i);
237 insert_hash(conf, sh);
240 static struct stripe_head *__find_stripe(raid6_conf_t *conf, sector_t sector)
242 struct stripe_head *sh;
245 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
246 for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
247 if (sh->sector == sector)
249 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
253 static void unplug_slaves(mddev_t *mddev);
255 static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector,
256 int pd_idx, int noblock)
258 struct stripe_head *sh;
260 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
262 spin_lock_irq(&conf->device_lock);
265 sh = __find_stripe(conf, sector);
267 if (!conf->inactive_blocked)
268 sh = get_free_stripe(conf);
269 if (noblock && sh == NULL)
272 conf->inactive_blocked = 1;
273 wait_event_lock_irq(conf->wait_for_stripe,
274 !list_empty(&conf->inactive_list) &&
275 (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
276 || !conf->inactive_blocked),
278 unplug_slaves(conf->mddev);
280 conf->inactive_blocked = 0;
282 init_stripe(sh, sector, pd_idx);
284 if (atomic_read(&sh->count)) {
285 if (!list_empty(&sh->lru))
288 if (!test_bit(STRIPE_HANDLE, &sh->state))
289 atomic_inc(&conf->active_stripes);
290 if (list_empty(&sh->lru))
292 list_del_init(&sh->lru);
295 } while (sh == NULL);
298 atomic_inc(&sh->count);
300 spin_unlock_irq(&conf->device_lock);
304 static int grow_stripes(raid6_conf_t *conf, int num)
306 struct stripe_head *sh;
308 int devs = conf->raid_disks;
310 sprintf(conf->cache_name, "raid6/%s", mdname(conf->mddev));
312 sc = kmem_cache_create(conf->cache_name,
313 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
317 conf->slab_cache = sc;
319 sh = kmem_cache_alloc(sc, GFP_KERNEL);
322 memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
323 sh->raid_conf = conf;
324 sh->lock = SPIN_LOCK_UNLOCKED;
326 if (grow_buffers(sh, conf->raid_disks)) {
327 shrink_buffers(sh, conf->raid_disks);
328 kmem_cache_free(sc, sh);
331 /* we just created an active stripe so... */
332 atomic_set(&sh->count, 1);
333 atomic_inc(&conf->active_stripes);
334 INIT_LIST_HEAD(&sh->lru);
340 static void shrink_stripes(raid6_conf_t *conf)
342 struct stripe_head *sh;
345 spin_lock_irq(&conf->device_lock);
346 sh = get_free_stripe(conf);
347 spin_unlock_irq(&conf->device_lock);
350 if (atomic_read(&sh->count))
352 shrink_buffers(sh, conf->raid_disks);
353 kmem_cache_free(conf->slab_cache, sh);
354 atomic_dec(&conf->active_stripes);
356 kmem_cache_destroy(conf->slab_cache);
357 conf->slab_cache = NULL;
360 static int raid6_end_read_request (struct bio * bi, unsigned int bytes_done,
363 struct stripe_head *sh = bi->bi_private;
364 raid6_conf_t *conf = sh->raid_conf;
365 int disks = conf->raid_disks, i;
366 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
371 for (i=0 ; i<disks; i++)
372 if (bi == &sh->dev[i].req)
375 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
376 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
387 spin_lock_irqsave(&conf->device_lock, flags);
388 /* we can return a buffer if we bypassed the cache or
389 * if the top buffer is not in highmem. If there are
390 * multiple buffers, leave the extra work to
393 buffer = sh->bh_read[i];
395 (!PageHighMem(buffer->b_page)
396 || buffer->b_page == bh->b_page )
398 sh->bh_read[i] = buffer->b_reqnext;
399 buffer->b_reqnext = NULL;
402 spin_unlock_irqrestore(&conf->device_lock, flags);
403 if (sh->bh_page[i]==bh->b_page)
404 set_buffer_uptodate(bh);
406 if (buffer->b_page != bh->b_page)
407 memcpy(buffer->b_data, bh->b_data, bh->b_size);
408 buffer->b_end_io(buffer, 1);
411 set_bit(R5_UPTODATE, &sh->dev[i].flags);
414 md_error(conf->mddev, conf->disks[i].rdev);
415 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
417 atomic_dec(&conf->disks[i].rdev->nr_pending);
419 /* must restore b_page before unlocking buffer... */
420 if (sh->bh_page[i] != bh->b_page) {
421 bh->b_page = sh->bh_page[i];
422 bh->b_data = page_address(bh->b_page);
423 clear_buffer_uptodate(bh);
426 clear_bit(R5_LOCKED, &sh->dev[i].flags);
427 set_bit(STRIPE_HANDLE, &sh->state);
432 static int raid6_end_write_request (struct bio *bi, unsigned int bytes_done,
435 struct stripe_head *sh = bi->bi_private;
436 raid6_conf_t *conf = sh->raid_conf;
437 int disks = conf->raid_disks, i;
439 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
444 for (i=0 ; i<disks; i++)
445 if (bi == &sh->dev[i].req)
448 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
449 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
456 spin_lock_irqsave(&conf->device_lock, flags);
458 md_error(conf->mddev, conf->disks[i].rdev);
460 atomic_dec(&conf->disks[i].rdev->nr_pending);
462 clear_bit(R5_LOCKED, &sh->dev[i].flags);
463 set_bit(STRIPE_HANDLE, &sh->state);
464 __release_stripe(conf, sh);
465 spin_unlock_irqrestore(&conf->device_lock, flags);
470 static sector_t compute_blocknr(struct stripe_head *sh, int i);
472 static void raid6_build_block (struct stripe_head *sh, int i)
474 struct r5dev *dev = &sh->dev[i];
475 int pd_idx = sh->pd_idx;
476 int qd_idx = raid6_next_disk(pd_idx, sh->raid_conf->raid_disks);
479 dev->req.bi_io_vec = &dev->vec;
481 dev->vec.bv_page = dev->page;
482 dev->vec.bv_len = STRIPE_SIZE;
483 dev->vec.bv_offset = 0;
485 dev->req.bi_sector = sh->sector;
486 dev->req.bi_private = sh;
489 if (i != pd_idx && i != qd_idx)
490 dev->sector = compute_blocknr(sh, i);
493 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
495 char b[BDEVNAME_SIZE];
496 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
497 PRINTK("raid6: error called\n");
501 conf->working_disks--;
504 conf->failed_disks++;
507 * if recovery was running, make sure it aborts.
509 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
513 "raid6: Disk failure on %s, disabling device."
514 " Operation continuing on %d devices\n",
515 bdevname(rdev->bdev,b), conf->working_disks);
520 * Input: a 'big' sector number,
521 * Output: index of the data and parity disk, and the sector # in them.
523 static sector_t raid6_compute_sector(sector_t r_sector, unsigned int raid_disks,
524 unsigned int data_disks, unsigned int * dd_idx,
525 unsigned int * pd_idx, raid6_conf_t *conf)
528 unsigned long chunk_number;
529 unsigned int chunk_offset;
531 int sectors_per_chunk = conf->chunk_size >> 9;
533 /* First compute the information on this sector */
536 * Compute the chunk number and the sector offset inside the chunk
538 chunk_offset = sector_div(r_sector, sectors_per_chunk);
539 chunk_number = r_sector;
540 if ( r_sector != chunk_number ) {
541 printk(KERN_CRIT "raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
542 (unsigned long long)r_sector, (unsigned long)chunk_number);
547 * Compute the stripe number
549 stripe = chunk_number / data_disks;
552 * Compute the data disk and parity disk indexes inside the stripe
554 *dd_idx = chunk_number % data_disks;
557 * Select the parity disk based on the user selected algorithm.
561 switch (conf->algorithm) {
562 case ALGORITHM_LEFT_ASYMMETRIC:
563 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
564 if (*pd_idx == raid_disks-1)
565 (*dd_idx)++; /* Q D D D P */
566 else if (*dd_idx >= *pd_idx)
567 (*dd_idx) += 2; /* D D P Q D */
569 case ALGORITHM_RIGHT_ASYMMETRIC:
570 *pd_idx = stripe % raid_disks;
571 if (*pd_idx == raid_disks-1)
572 (*dd_idx)++; /* Q D D D P */
573 else if (*dd_idx >= *pd_idx)
574 (*dd_idx) += 2; /* D D P Q D */
576 case ALGORITHM_LEFT_SYMMETRIC:
577 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
578 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
580 case ALGORITHM_RIGHT_SYMMETRIC:
581 *pd_idx = stripe % raid_disks;
582 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
585 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
589 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
590 chunk_number, *pd_idx, *dd_idx);
593 * Finally, compute the new sector number
595 new_sector = (sector_t) stripe * sectors_per_chunk + chunk_offset;
600 static sector_t compute_blocknr(struct stripe_head *sh, int i)
602 raid6_conf_t *conf = sh->raid_conf;
603 int raid_disks = conf->raid_disks, data_disks = raid_disks - 2;
604 sector_t new_sector = sh->sector, check;
605 int sectors_per_chunk = conf->chunk_size >> 9;
608 int chunk_number, dummy1, dummy2, dd_idx = i;
612 chunk_offset = sector_div(new_sector, sectors_per_chunk);
614 if ( new_sector != stripe ) {
615 printk(KERN_CRIT "raid6: ERROR: new_sector = %llu, stripe = %lu\n",
616 (unsigned long long)new_sector, (unsigned long)stripe);
620 switch (conf->algorithm) {
621 case ALGORITHM_LEFT_ASYMMETRIC:
622 case ALGORITHM_RIGHT_ASYMMETRIC:
623 if (sh->pd_idx == raid_disks-1)
625 else if (i > sh->pd_idx)
626 i -= 2; /* D D P Q D */
628 case ALGORITHM_LEFT_SYMMETRIC:
629 case ALGORITHM_RIGHT_SYMMETRIC:
630 if (sh->pd_idx == raid_disks-1)
636 i -= (sh->pd_idx + 2);
640 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
644 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh->pd_idx, i0, i);
646 chunk_number = stripe * data_disks + i;
647 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
649 check = raid6_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
650 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
651 printk(KERN_CRIT "raid6: compute_blocknr: map not correct\n");
660 * Copy data between a page in the stripe cache, and one or more bion
661 * The page could align with the middle of the bio, or there could be
662 * several bion, each with several bio_vecs, which cover part of the page
663 * Multiple bion are linked together on bi_next. There may be extras
664 * at the end of this list. We ignore them.
666 static void copy_data(int frombio, struct bio *bio,
670 char *pa = page_address(page);
674 for (;bio && bio->bi_sector < sector+STRIPE_SECTORS;
675 bio = r5_next_bio(bio, sector) ) {
677 if (bio->bi_sector >= sector)
678 page_offset = (signed)(bio->bi_sector - sector) * 512;
680 page_offset = (signed)(sector - bio->bi_sector) * -512;
681 bio_for_each_segment(bvl, bio, i) {
682 int len = bio_iovec_idx(bio,i)->bv_len;
686 if (page_offset < 0) {
687 b_offset = -page_offset;
688 page_offset += b_offset;
692 if (len > 0 && page_offset + len > STRIPE_SIZE)
693 clen = STRIPE_SIZE - page_offset;
697 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
699 memcpy(pa+page_offset, ba+b_offset, clen);
701 memcpy(ba+b_offset, pa+page_offset, clen);
702 __bio_kunmap_atomic(ba, KM_USER0);
704 if (clen < len) /* hit end of page */
711 #define check_xor() do { \
712 if (count == MAX_XOR_BLOCKS) { \
713 xor_block(count, STRIPE_SIZE, ptr); \
718 /* Compute P and Q syndromes */
719 static void compute_parity(struct stripe_head *sh, int method)
721 raid6_conf_t *conf = sh->raid_conf;
722 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
724 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
727 qd_idx = raid6_next_disk(pd_idx, disks);
728 d0_idx = raid6_next_disk(qd_idx, disks);
730 PRINTK("compute_parity, stripe %llu, method %d\n",
731 (unsigned long long)sh->sector, method);
734 case READ_MODIFY_WRITE:
735 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
736 case RECONSTRUCT_WRITE:
737 case UPDATE_PARITY: /* Is this right? */
738 for (i= disks; i-- ;)
739 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
740 chosen = sh->dev[i].towrite;
741 sh->dev[i].towrite = NULL;
742 if (sh->dev[i].written) BUG();
743 sh->dev[i].written = chosen;
747 BUG(); /* Not implemented yet */
750 for (i = disks; i--;)
751 if (sh->dev[i].written) {
752 sector_t sector = sh->dev[i].sector;
753 struct bio *wbi = sh->dev[i].written;
754 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
755 copy_data(1, wbi, sh->dev[i].page, sector);
756 wbi = r5_next_bio(wbi, sector);
759 set_bit(R5_LOCKED, &sh->dev[i].flags);
760 set_bit(R5_UPTODATE, &sh->dev[i].flags);
764 // case RECONSTRUCT_WRITE:
765 // case CHECK_PARITY:
766 // case UPDATE_PARITY:
767 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
768 /* FIX: Is this ordering of drives even remotely optimal? */
772 ptrs[count++] = page_address(sh->dev[i].page);
774 i = raid6_next_disk(i, disks);
775 } while ( i != d0_idx );
779 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
782 case RECONSTRUCT_WRITE:
783 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
784 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
785 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
786 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
789 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
790 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
795 /* Compute one missing block */
796 static void compute_block_1(struct stripe_head *sh, int dd_idx)
798 raid6_conf_t *conf = sh->raid_conf;
799 int i, count, disks = conf->raid_disks;
800 void *ptr[MAX_XOR_BLOCKS], *p;
801 int pd_idx = sh->pd_idx;
802 int qd_idx = raid6_next_disk(pd_idx, disks);
804 PRINTK("compute_block_1, stripe %llu, idx %d\n",
805 (unsigned long long)sh->sector, dd_idx);
807 if ( dd_idx == qd_idx ) {
808 /* We're actually computing the Q drive */
809 compute_parity(sh, UPDATE_PARITY);
811 ptr[0] = page_address(sh->dev[dd_idx].page);
812 memset(ptr[0], 0, STRIPE_SIZE);
814 for (i = disks ; i--; ) {
815 if (i == dd_idx || i == qd_idx)
817 p = page_address(sh->dev[i].page);
818 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
821 PRINTK("compute_block() %d, stripe %llu, %d"
822 " not present\n", dd_idx,
823 (unsigned long long)sh->sector, i);
828 xor_block(count, STRIPE_SIZE, ptr);
829 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
833 /* Compute two missing blocks */
834 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
836 raid6_conf_t *conf = sh->raid_conf;
837 int i, count, disks = conf->raid_disks;
838 int pd_idx = sh->pd_idx;
839 int qd_idx = raid6_next_disk(pd_idx, disks);
840 int d0_idx = raid6_next_disk(qd_idx, disks);
843 /* faila and failb are disk numbers relative to d0_idx */
844 /* pd_idx become disks-2 and qd_idx become disks-1 */
845 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
846 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
848 BUG_ON(faila == failb);
849 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
851 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
852 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
854 if ( failb == disks-1 ) {
855 /* Q disk is one of the missing disks */
856 if ( faila == disks-2 ) {
857 /* Missing P+Q, just recompute */
858 compute_parity(sh, UPDATE_PARITY);
861 /* We're missing D+Q; recompute D from P */
862 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1);
863 compute_parity(sh, UPDATE_PARITY); /* Is this necessary? */
868 /* We're missing D+P or D+D; build pointer table */
870 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
876 ptrs[count++] = page_address(sh->dev[i].page);
877 i = raid6_next_disk(i, disks);
878 } while ( i != d0_idx );
880 if ( failb == disks-2 ) {
881 /* We're missing D+P. */
882 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
884 /* We're missing D+D. */
885 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
888 /* Both the above update both missing blocks */
889 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
890 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
896 * Each stripe/dev can have one or more bion attached.
897 * toread/towrite point to the first in a chain.
898 * The bi_next chain must be in order.
900 static void add_stripe_bio (struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
903 raid6_conf_t *conf = sh->raid_conf;
905 PRINTK("adding bh b#%llu to stripe s#%llu\n",
906 (unsigned long long)bi->bi_sector,
907 (unsigned long long)sh->sector);
910 spin_lock(&sh->lock);
911 spin_lock_irq(&conf->device_lock);
913 bip = &sh->dev[dd_idx].towrite;
915 bip = &sh->dev[dd_idx].toread;
916 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
917 BUG_ON((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector);
918 bip = & (*bip)->bi_next;
920 /* FIXME do I need to worry about overlapping bion */
921 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
926 bi->bi_phys_segments ++;
927 spin_unlock_irq(&conf->device_lock);
928 spin_unlock(&sh->lock);
930 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
931 (unsigned long long)bi->bi_sector,
932 (unsigned long long)sh->sector, dd_idx);
935 /* check if page is coverred */
936 sector_t sector = sh->dev[dd_idx].sector;
937 for (bi=sh->dev[dd_idx].towrite;
938 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
939 bi && bi->bi_sector <= sector;
940 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
941 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
942 sector = bi->bi_sector + (bi->bi_size>>9);
944 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
945 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
951 * handle_stripe - do things to a stripe.
953 * We lock the stripe and then examine the state of various bits
954 * to see what needs to be done.
956 * return some read request which now have data
957 * return some write requests which are safely on disc
958 * schedule a read on some buffers
959 * schedule a write of some buffers
960 * return confirmation of parity correctness
962 * Parity calculations are done inside the stripe lock
963 * buffers are taken off read_list or write_list, and bh_cache buffers
964 * get BH_Lock set before the stripe lock is released.
968 static void handle_stripe(struct stripe_head *sh)
970 raid6_conf_t *conf = sh->raid_conf;
971 int disks = conf->raid_disks;
972 struct bio *return_bi= NULL;
976 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
977 int non_overwrite = 0;
978 int failed_num[2] = {0, 0};
979 struct r5dev *dev, *pdev, *qdev;
980 int pd_idx = sh->pd_idx;
981 int qd_idx = raid6_next_disk(pd_idx, disks);
982 int p_failed, q_failed;
984 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
985 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
988 spin_lock(&sh->lock);
989 clear_bit(STRIPE_HANDLE, &sh->state);
990 clear_bit(STRIPE_DELAYED, &sh->state);
992 syncing = test_bit(STRIPE_SYNCING, &sh->state);
993 /* Now to look around and see what can be done */
995 for (i=disks; i--; ) {
998 clear_bit(R5_Insync, &dev->flags);
999 clear_bit(R5_Syncio, &dev->flags);
1001 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1002 i, dev->flags, dev->toread, dev->towrite, dev->written);
1003 /* maybe we can reply to a read */
1004 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1005 struct bio *rbi, *rbi2;
1006 PRINTK("Return read for disc %d\n", i);
1007 spin_lock_irq(&conf->device_lock);
1010 spin_unlock_irq(&conf->device_lock);
1011 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1012 copy_data(0, rbi, dev->page, dev->sector);
1013 rbi2 = r5_next_bio(rbi, dev->sector);
1014 spin_lock_irq(&conf->device_lock);
1015 if (--rbi->bi_phys_segments == 0) {
1016 rbi->bi_next = return_bi;
1019 spin_unlock_irq(&conf->device_lock);
1024 /* now count some things */
1025 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1026 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1029 if (dev->toread) to_read++;
1032 if (!test_bit(R5_OVERWRITE, &dev->flags))
1035 if (dev->written) written++;
1036 rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
1037 if (!rdev || !rdev->in_sync) {
1039 failed_num[failed] = i;
1042 set_bit(R5_Insync, &dev->flags);
1044 PRINTK("locked=%d uptodate=%d to_read=%d"
1045 " to_write=%d failed=%d failed_num=%d,%d\n",
1046 locked, uptodate, to_read, to_write, failed,
1047 failed_num[0], failed_num[1]);
1048 /* check if the array has lost >2 devices and, if so, some requests might
1051 if (failed > 2 && to_read+to_write+written) {
1052 spin_lock_irq(&conf->device_lock);
1053 for (i=disks; i--; ) {
1054 /* fail all writes first */
1055 bi = sh->dev[i].towrite;
1056 sh->dev[i].towrite = NULL;
1059 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1060 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1061 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1062 if (--bi->bi_phys_segments == 0) {
1063 md_write_end(conf->mddev);
1064 bi->bi_next = return_bi;
1069 /* and fail all 'written' */
1070 bi = sh->dev[i].written;
1071 sh->dev[i].written = NULL;
1072 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1073 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1074 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1075 if (--bi->bi_phys_segments == 0) {
1076 md_write_end(conf->mddev);
1077 bi->bi_next = return_bi;
1083 /* fail any reads if this device is non-operational */
1084 if (!test_bit(R5_Insync, &sh->dev[i].flags)) {
1085 bi = sh->dev[i].toread;
1086 sh->dev[i].toread = NULL;
1088 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1089 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1090 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1091 if (--bi->bi_phys_segments == 0) {
1092 bi->bi_next = return_bi;
1099 spin_unlock_irq(&conf->device_lock);
1101 if (failed > 2 && syncing) {
1102 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1103 clear_bit(STRIPE_SYNCING, &sh->state);
1108 * might be able to return some write requests if the parity blocks
1109 * are safe, or on a failed drive
1111 pdev = &sh->dev[pd_idx];
1112 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
1113 || (failed >= 2 && failed_num[1] == pd_idx);
1114 qdev = &sh->dev[qd_idx];
1115 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
1116 || (failed >= 2 && failed_num[1] == qd_idx);
1119 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
1120 && !test_bit(R5_LOCKED, &pdev->flags)
1121 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
1122 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
1123 && !test_bit(R5_LOCKED, &qdev->flags)
1124 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
1125 /* any written block on an uptodate or failed drive can be
1126 * returned. Note that if we 'wrote' to a failed drive,
1127 * it will be UPTODATE, but never LOCKED, so we don't need
1128 * to test 'failed' directly.
1130 for (i=disks; i--; )
1131 if (sh->dev[i].written) {
1133 if (!test_bit(R5_LOCKED, &dev->flags) &&
1134 test_bit(R5_UPTODATE, &dev->flags) ) {
1135 /* We can return any write requests */
1136 struct bio *wbi, *wbi2;
1137 PRINTK("Return write for stripe %llu disc %d\n",
1138 (unsigned long long)sh->sector, i);
1139 spin_lock_irq(&conf->device_lock);
1141 dev->written = NULL;
1142 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1143 wbi2 = r5_next_bio(wbi, dev->sector);
1144 if (--wbi->bi_phys_segments == 0) {
1145 md_write_end(conf->mddev);
1146 wbi->bi_next = return_bi;
1151 spin_unlock_irq(&conf->device_lock);
1156 /* Now we might consider reading some blocks, either to check/generate
1157 * parity, or to satisfy requests
1158 * or to load a block that is being partially written.
1160 if (to_read || non_overwrite || (syncing && (uptodate+failed < disks))) {
1161 for (i=disks; i--;) {
1163 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1165 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1167 (failed >= 1 && (sh->dev[failed_num[0]].toread ||
1168 (sh->dev[failed_num[0]].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num[0]].flags)))) ||
1169 (failed >= 2 && (sh->dev[failed_num[1]].toread ||
1170 (sh->dev[failed_num[1]].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num[1]].flags))))
1173 /* we would like to get this block, possibly
1174 * by computing it, but we might not be able to
1176 if (uptodate == disks-1) {
1177 PRINTK("Computing stripe %llu block %d\n",
1178 (unsigned long long)sh->sector, i);
1179 compute_block_1(sh, i);
1181 } else if ( uptodate == disks-2 && failed >= 2 ) {
1182 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1184 for (other=disks; other--;) {
1187 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
1191 PRINTK("Computing stripe %llu blocks %d,%d\n",
1192 (unsigned long long)sh->sector, i, other);
1193 compute_block_2(sh, i, other);
1195 } else if (test_bit(R5_Insync, &dev->flags)) {
1196 set_bit(R5_LOCKED, &dev->flags);
1197 set_bit(R5_Wantread, &dev->flags);
1199 /* if I am just reading this block and we don't have
1200 a failed drive, or any pending writes then sidestep the cache */
1201 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1202 ! syncing && !failed && !to_write) {
1203 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1204 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1208 PRINTK("Reading block %d (sync=%d)\n",
1211 md_sync_acct(conf->disks[i].rdev, STRIPE_SECTORS);
1215 set_bit(STRIPE_HANDLE, &sh->state);
1218 /* now to consider writing and what else, if anything should be read */
1220 int rcw=0, must_compute=0;
1221 for (i=disks ; i--;) {
1223 /* Would I have to read this buffer for reconstruct_write */
1224 if (!test_bit(R5_OVERWRITE, &dev->flags)
1225 && i != pd_idx && i != qd_idx
1226 && (!test_bit(R5_LOCKED, &dev->flags)
1228 || sh->bh_page[i] != bh->b_page
1231 !test_bit(R5_UPTODATE, &dev->flags)) {
1232 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1234 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
1239 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1240 (unsigned long long)sh->sector, rcw, must_compute);
1241 set_bit(STRIPE_HANDLE, &sh->state);
1244 /* want reconstruct write, but need to get some data */
1245 for (i=disks; i--;) {
1247 if (!test_bit(R5_OVERWRITE, &dev->flags)
1248 && !(failed == 0 && (i == pd_idx || i == qd_idx))
1249 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1250 test_bit(R5_Insync, &dev->flags)) {
1251 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1253 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1254 (unsigned long long)sh->sector, i);
1255 set_bit(R5_LOCKED, &dev->flags);
1256 set_bit(R5_Wantread, &dev->flags);
1259 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1260 (unsigned long long)sh->sector, i);
1261 set_bit(STRIPE_DELAYED, &sh->state);
1262 set_bit(STRIPE_HANDLE, &sh->state);
1266 /* now if nothing is locked, and if we have enough data, we can start a write request */
1267 if (locked == 0 && rcw == 0) {
1268 if ( must_compute > 0 ) {
1269 /* We have failed blocks and need to compute them */
1272 case 1: compute_block_1(sh, failed_num[0]); break;
1273 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
1274 default: BUG(); /* This request should have been failed? */
1278 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
1279 compute_parity(sh, RECONSTRUCT_WRITE);
1280 /* now every locked buffer is ready to be written */
1282 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1283 PRINTK("Writing stripe %llu block %d\n",
1284 (unsigned long long)sh->sector, i);
1286 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1287 #if 0 /**** FIX: I don't understand the logic here... ****/
1288 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1289 || ((i==pd_idx || i==qd_idx) && failed == 0)) /* FIX? */
1290 set_bit(STRIPE_INSYNC, &sh->state);
1293 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1294 atomic_dec(&conf->preread_active_stripes);
1295 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1296 md_wakeup_thread(conf->mddev->thread);
1301 /* maybe we need to check and possibly fix the parity for this stripe
1302 * Any reads will already have been scheduled, so we just see if enough data
1305 if (syncing && locked == 0 &&
1306 !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 2) {
1307 set_bit(STRIPE_HANDLE, &sh->state);
1308 #if 0 /* RAID-6: Don't support CHECK PARITY yet */
1311 if (uptodate != disks)
1313 compute_parity(sh, CHECK_PARITY);
1315 pagea = page_address(sh->dev[pd_idx].page);
1316 if ((*(u32*)pagea) == 0 &&
1317 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1318 /* parity is correct (on disc, not in buffer any more) */
1319 set_bit(STRIPE_INSYNC, &sh->state);
1323 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1324 int failed_needupdate[2];
1325 struct r5dev *adev, *bdev;
1328 failed_num[0] = pd_idx;
1330 failed_num[1] = (failed_num[0] == qd_idx) ? pd_idx : qd_idx;
1332 failed_needupdate[0] = !test_bit(R5_UPTODATE, &sh->dev[failed_num[0]].flags);
1333 failed_needupdate[1] = !test_bit(R5_UPTODATE, &sh->dev[failed_num[1]].flags);
1335 PRINTK("sync: failed=%d num=%d,%d fnu=%u%u\n",
1336 failed, failed_num[0], failed_num[1], failed_needupdate[0], failed_needupdate[1]);
1338 #if 0 /* RAID-6: This code seems to require that CHECK_PARITY destroys the uptodateness of the parity */
1339 /* should be able to compute the missing block(s) and write to spare */
1340 if ( failed_needupdate[0] ^ failed_needupdate[1] ) {
1341 if (uptodate+1 != disks)
1343 compute_block_1(sh, failed_needupdate[0] ? failed_num[0] : failed_num[1]);
1345 } else if ( failed_needupdate[0] & failed_needupdate[1] ) {
1346 if (uptodate+2 != disks)
1348 compute_block_2(sh, failed_num[0], failed_num[1]);
1352 compute_block_2(sh, failed_num[0], failed_num[1]);
1353 uptodate += failed_needupdate[0] + failed_needupdate[1];
1356 if (uptodate != disks)
1359 PRINTK("Marking for sync stripe %llu blocks %d,%d\n",
1360 (unsigned long long)sh->sector, failed_num[0], failed_num[1]);
1362 /**** FIX: Should we really do both of these unconditionally? ****/
1363 adev = &sh->dev[failed_num[0]];
1364 locked += !test_bit(R5_LOCKED, &adev->flags);
1365 set_bit(R5_LOCKED, &adev->flags);
1366 set_bit(R5_Wantwrite, &adev->flags);
1367 bdev = &sh->dev[failed_num[1]];
1368 locked += !test_bit(R5_LOCKED, &bdev->flags);
1369 set_bit(R5_LOCKED, &bdev->flags);
1370 set_bit(R5_Wantwrite, &bdev->flags);
1372 set_bit(STRIPE_INSYNC, &sh->state);
1373 set_bit(R5_Syncio, &adev->flags);
1374 set_bit(R5_Syncio, &bdev->flags);
1377 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1378 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1379 clear_bit(STRIPE_SYNCING, &sh->state);
1382 spin_unlock(&sh->lock);
1384 while ((bi=return_bi)) {
1385 int bytes = bi->bi_size;
1387 return_bi = bi->bi_next;
1390 bi->bi_end_io(bi, bytes, 0);
1392 for (i=disks; i-- ;) {
1396 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1398 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1403 bi = &sh->dev[i].req;
1407 bi->bi_end_io = raid6_end_write_request;
1409 bi->bi_end_io = raid6_end_read_request;
1411 spin_lock_irq(&conf->device_lock);
1412 rdev = conf->disks[i].rdev;
1413 if (rdev && rdev->faulty)
1416 atomic_inc(&rdev->nr_pending);
1417 spin_unlock_irq(&conf->device_lock);
1420 if (test_bit(R5_Syncio, &sh->dev[i].flags))
1421 md_sync_acct(rdev, STRIPE_SECTORS);
1423 bi->bi_bdev = rdev->bdev;
1424 PRINTK("for %llu schedule op %ld on disc %d\n",
1425 (unsigned long long)sh->sector, bi->bi_rw, i);
1426 atomic_inc(&sh->count);
1427 bi->bi_sector = sh->sector + rdev->data_offset;
1428 bi->bi_flags = 1 << BIO_UPTODATE;
1431 bi->bi_io_vec = &sh->dev[i].vec;
1432 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1433 bi->bi_io_vec[0].bv_offset = 0;
1434 bi->bi_size = STRIPE_SIZE;
1436 generic_make_request(bi);
1438 PRINTK("skip op %ld on disc %d for sector %llu\n",
1439 bi->bi_rw, i, (unsigned long long)sh->sector);
1440 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1441 set_bit(STRIPE_HANDLE, &sh->state);
1446 static inline void raid6_activate_delayed(raid6_conf_t *conf)
1448 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1449 while (!list_empty(&conf->delayed_list)) {
1450 struct list_head *l = conf->delayed_list.next;
1451 struct stripe_head *sh;
1452 sh = list_entry(l, struct stripe_head, lru);
1454 clear_bit(STRIPE_DELAYED, &sh->state);
1455 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1456 atomic_inc(&conf->preread_active_stripes);
1457 list_add_tail(&sh->lru, &conf->handle_list);
1462 static void unplug_slaves(mddev_t *mddev)
1464 /* note: this is always called with device_lock held */
1465 raid6_conf_t *conf = mddev_to_conf(mddev);
1468 for (i=0; i<mddev->raid_disks; i++) {
1469 mdk_rdev_t *rdev = conf->disks[i].rdev;
1470 if (rdev && !rdev->faulty) {
1471 struct block_device *bdev = rdev->bdev;
1473 request_queue_t *r_queue = bdev_get_queue(bdev);
1474 if (r_queue && r_queue->unplug_fn)
1475 r_queue->unplug_fn(r_queue);
1481 static void raid6_unplug_device(request_queue_t *q)
1483 mddev_t *mddev = q->queuedata;
1484 raid6_conf_t *conf = mddev_to_conf(mddev);
1485 unsigned long flags;
1487 spin_lock_irqsave(&conf->device_lock, flags);
1489 if (blk_remove_plug(q))
1490 raid6_activate_delayed(conf);
1491 md_wakeup_thread(mddev->thread);
1493 spin_unlock_irqrestore(&conf->device_lock, flags);
1495 unplug_slaves(mddev);
1498 static inline void raid6_plug_device(raid6_conf_t *conf)
1500 spin_lock_irq(&conf->device_lock);
1501 blk_plug_device(conf->mddev->queue);
1502 spin_unlock_irq(&conf->device_lock);
1505 static int make_request (request_queue_t *q, struct bio * bi)
1507 mddev_t *mddev = q->queuedata;
1508 raid6_conf_t *conf = mddev_to_conf(mddev);
1509 const unsigned int raid_disks = conf->raid_disks;
1510 const unsigned int data_disks = raid_disks - 2;
1511 unsigned int dd_idx, pd_idx;
1512 sector_t new_sector;
1513 sector_t logical_sector, last_sector;
1514 struct stripe_head *sh;
1516 if (bio_data_dir(bi)==WRITE) {
1517 disk_stat_inc(mddev->gendisk, writes);
1518 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi));
1520 disk_stat_inc(mddev->gendisk, reads);
1521 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi));
1524 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1525 last_sector = bi->bi_sector + (bi->bi_size>>9);
1528 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1529 if ( bio_data_dir(bi) == WRITE )
1530 md_write_start(mddev);
1531 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1533 new_sector = raid6_compute_sector(logical_sector,
1534 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1536 PRINTK("raid6: make_request, sector %Lu logical %Lu\n",
1537 (unsigned long long)new_sector,
1538 (unsigned long long)logical_sector);
1540 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1543 add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK));
1545 raid6_plug_device(conf);
1549 /* cannot get stripe for read-ahead, just give-up */
1550 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1555 spin_lock_irq(&conf->device_lock);
1556 if (--bi->bi_phys_segments == 0) {
1557 int bytes = bi->bi_size;
1559 if ( bio_data_dir(bi) == WRITE )
1560 md_write_end(mddev);
1562 bi->bi_end_io(bi, bytes, 0);
1564 spin_unlock_irq(&conf->device_lock);
1568 /* FIXME go_faster isn't used */
1569 static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
1571 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1572 struct stripe_head *sh;
1573 int sectors_per_chunk = conf->chunk_size >> 9;
1575 unsigned long stripe;
1578 sector_t first_sector;
1579 int raid_disks = conf->raid_disks;
1580 int data_disks = raid_disks - 2;
1582 if (sector_nr >= mddev->size <<1) {
1583 /* just being told to finish up .. nothing much to do */
1584 unplug_slaves(mddev);
1589 chunk_offset = sector_div(x, sectors_per_chunk);
1591 BUG_ON(x != stripe);
1593 first_sector = raid6_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1594 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1595 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1597 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1598 /* make sure we don't swamp the stripe cache if someone else
1599 * is trying to get access
1601 set_current_state(TASK_UNINTERRUPTIBLE);
1602 schedule_timeout(1);
1604 spin_lock(&sh->lock);
1605 set_bit(STRIPE_SYNCING, &sh->state);
1606 clear_bit(STRIPE_INSYNC, &sh->state);
1607 spin_unlock(&sh->lock);
1612 return STRIPE_SECTORS;
1616 * This is our raid6 kernel thread.
1618 * We scan the hash table for stripes which can be handled now.
1619 * During the scan, completed stripes are saved for us by the interrupt
1620 * handler, so that they will not have to wait for our next wakeup.
1622 static void raid6d (mddev_t *mddev)
1624 struct stripe_head *sh;
1625 raid6_conf_t *conf = mddev_to_conf(mddev);
1628 PRINTK("+++ raid6d active\n");
1630 md_check_recovery(mddev);
1631 md_handle_safemode(mddev);
1634 spin_lock_irq(&conf->device_lock);
1636 struct list_head *first;
1638 if (list_empty(&conf->handle_list) &&
1639 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1640 !blk_queue_plugged(mddev->queue) &&
1641 !list_empty(&conf->delayed_list))
1642 raid6_activate_delayed(conf);
1644 if (list_empty(&conf->handle_list))
1647 first = conf->handle_list.next;
1648 sh = list_entry(first, struct stripe_head, lru);
1650 list_del_init(first);
1651 atomic_inc(&sh->count);
1652 if (atomic_read(&sh->count)!= 1)
1654 spin_unlock_irq(&conf->device_lock);
1660 spin_lock_irq(&conf->device_lock);
1662 PRINTK("%d stripes handled\n", handled);
1664 spin_unlock_irq(&conf->device_lock);
1666 unplug_slaves(mddev);
1668 PRINTK("--- raid6d inactive\n");
1671 static int run (mddev_t *mddev)
1674 int raid_disk, memory;
1676 struct disk_info *disk;
1677 struct list_head *tmp;
1679 if (mddev->level != 6) {
1680 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev), mddev->level);
1684 mddev->private = kmalloc (sizeof (raid6_conf_t)
1685 + mddev->raid_disks * sizeof(struct disk_info),
1687 if ((conf = mddev->private) == NULL)
1689 memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1690 conf->mddev = mddev;
1692 if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1694 memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1696 conf->device_lock = SPIN_LOCK_UNLOCKED;
1697 init_waitqueue_head(&conf->wait_for_stripe);
1698 INIT_LIST_HEAD(&conf->handle_list);
1699 INIT_LIST_HEAD(&conf->delayed_list);
1700 INIT_LIST_HEAD(&conf->inactive_list);
1701 atomic_set(&conf->active_stripes, 0);
1702 atomic_set(&conf->preread_active_stripes, 0);
1704 mddev->queue->unplug_fn = raid6_unplug_device;
1706 PRINTK("raid6: run(%s) called.\n", mdname(mddev));
1708 ITERATE_RDEV(mddev,rdev,tmp) {
1709 raid_disk = rdev->raid_disk;
1710 if (raid_disk >= mddev->raid_disks
1713 disk = conf->disks + raid_disk;
1717 if (rdev->in_sync) {
1718 char b[BDEVNAME_SIZE];
1719 printk(KERN_INFO "raid6: device %s operational as raid"
1720 " disk %d\n", bdevname(rdev->bdev,b),
1722 conf->working_disks++;
1726 conf->raid_disks = mddev->raid_disks;
1729 * 0 for a fully functional array, 1 or 2 for a degraded array.
1731 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1732 conf->mddev = mddev;
1733 conf->chunk_size = mddev->chunk_size;
1734 conf->level = mddev->level;
1735 conf->algorithm = mddev->layout;
1736 conf->max_nr_stripes = NR_STRIPES;
1738 if (conf->raid_disks < 4) {
1739 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
1740 mdname(mddev), conf->raid_disks);
1743 if (!conf->chunk_size || conf->chunk_size % 4) {
1744 printk(KERN_ERR "raid6: invalid chunk size %d for %s\n",
1745 conf->chunk_size, mdname(mddev));
1748 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1750 "raid6: unsupported parity algorithm %d for %s\n",
1751 conf->algorithm, mdname(mddev));
1754 if (mddev->degraded > 2) {
1755 printk(KERN_ERR "raid6: not enough operational devices for %s"
1756 " (%d/%d failed)\n",
1757 mdname(mddev), conf->failed_disks, conf->raid_disks);
1761 #if 0 /* FIX: For now */
1762 if (mddev->degraded > 0 &&
1763 mddev->recovery_cp != MaxSector) {
1764 printk(KERN_ERR "raid6: cannot start dirty degraded array for %s\n", mdname(mddev));
1770 mddev->thread = md_register_thread(raid6d, mddev, "%s_raid6");
1771 if (!mddev->thread) {
1773 "raid6: couldn't allocate thread for %s\n",
1779 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1780 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1781 if (grow_stripes(conf, conf->max_nr_stripes)) {
1783 "raid6: couldn't allocate %dkB for buffers\n", memory);
1784 shrink_stripes(conf);
1785 md_unregister_thread(mddev->thread);
1788 printk(KERN_INFO "raid6: allocated %dkB for %s\n",
1789 memory, mdname(mddev));
1791 if (mddev->degraded == 0)
1792 printk(KERN_INFO "raid6: raid level %d set %s active with %d out of %d"
1793 " devices, algorithm %d\n", conf->level, mdname(mddev),
1794 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1797 printk(KERN_ALERT "raid6: raid level %d set %s active with %d"
1798 " out of %d devices, algorithm %d\n", conf->level,
1799 mdname(mddev), mddev->raid_disks - mddev->degraded,
1800 mddev->raid_disks, conf->algorithm);
1802 print_raid6_conf(conf);
1804 /* read-ahead size must cover two whole stripes, which is
1805 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
1808 int stripe = (mddev->raid_disks-2) * mddev->chunk_size
1810 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1811 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1814 /* Ok, everything is just fine now */
1815 mddev->array_size = mddev->size * (mddev->raid_disks - 2);
1819 print_raid6_conf(conf);
1820 if (conf->stripe_hashtbl)
1821 free_pages((unsigned long) conf->stripe_hashtbl,
1825 mddev->private = NULL;
1826 printk(KERN_ALERT "raid6: failed to run raid set %s\n", mdname(mddev));
1832 static int stop (mddev_t *mddev)
1834 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1836 md_unregister_thread(mddev->thread);
1837 mddev->thread = NULL;
1838 shrink_stripes(conf);
1839 free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
1841 mddev->private = NULL;
1846 static void print_sh (struct seq_file *seq, struct stripe_head *sh)
1850 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
1851 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
1852 seq_printf(seq, "sh %llu, count %d.\n",
1853 (unsigned long long)sh->sector, atomic_read(&sh->count));
1854 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
1855 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
1856 seq_printf(seq, "(cache%d: %p %ld) ",
1857 i, sh->dev[i].page, sh->dev[i].flags);
1859 seq_printf(seq, "\n");
1862 static void printall (struct seq_file *seq, raid6_conf_t *conf)
1864 struct stripe_head *sh;
1867 spin_lock_irq(&conf->device_lock);
1868 for (i = 0; i < NR_HASH; i++) {
1869 sh = conf->stripe_hashtbl[i];
1870 for (; sh; sh = sh->hash_next) {
1871 if (sh->raid_conf != conf)
1876 spin_unlock_irq(&conf->device_lock);
1880 static void status (struct seq_file *seq, mddev_t *mddev)
1882 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1885 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
1886 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
1887 for (i = 0; i < conf->raid_disks; i++)
1888 seq_printf (seq, "%s",
1889 conf->disks[i].rdev &&
1890 conf->disks[i].rdev->in_sync ? "U" : "_");
1891 seq_printf (seq, "]");
1893 seq_printf (seq, "\n");
1894 printall(seq, conf);
1898 static void print_raid6_conf (raid6_conf_t *conf)
1901 struct disk_info *tmp;
1903 printk("RAID6 conf printout:\n");
1905 printk("(conf==NULL)\n");
1908 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
1909 conf->working_disks, conf->failed_disks);
1911 for (i = 0; i < conf->raid_disks; i++) {
1912 char b[BDEVNAME_SIZE];
1913 tmp = conf->disks + i;
1915 printk(" disk %d, o:%d, dev:%s\n",
1916 i, !tmp->rdev->faulty,
1917 bdevname(tmp->rdev->bdev,b));
1921 static int raid6_spare_active(mddev_t *mddev)
1924 raid6_conf_t *conf = mddev->private;
1925 struct disk_info *tmp;
1927 spin_lock_irq(&conf->device_lock);
1928 for (i = 0; i < conf->raid_disks; i++) {
1929 tmp = conf->disks + i;
1931 && !tmp->rdev->faulty
1932 && !tmp->rdev->in_sync) {
1934 conf->failed_disks--;
1935 conf->working_disks++;
1936 tmp->rdev->in_sync = 1;
1939 spin_unlock_irq(&conf->device_lock);
1940 print_raid6_conf(conf);
1944 static int raid6_remove_disk(mddev_t *mddev, int number)
1946 raid6_conf_t *conf = mddev->private;
1948 struct disk_info *p = conf->disks + number;
1950 print_raid6_conf(conf);
1951 spin_lock_irq(&conf->device_lock);
1954 if (p->rdev->in_sync ||
1955 atomic_read(&p->rdev->nr_pending)) {
1965 spin_unlock_irq(&conf->device_lock);
1966 print_raid6_conf(conf);
1970 static int raid6_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1972 raid6_conf_t *conf = mddev->private;
1975 struct disk_info *p;
1977 spin_lock_irq(&conf->device_lock);
1981 for (disk=0; disk < mddev->raid_disks; disk++)
1982 if ((p=conf->disks + disk)->rdev == NULL) {
1985 rdev->raid_disk = disk;
1989 spin_unlock_irq(&conf->device_lock);
1990 print_raid6_conf(conf);
1994 static mdk_personality_t raid6_personality=
1997 .owner = THIS_MODULE,
1998 .make_request = make_request,
2002 .error_handler = error,
2003 .hot_add_disk = raid6_add_disk,
2004 .hot_remove_disk= raid6_remove_disk,
2005 .spare_active = raid6_spare_active,
2006 .sync_request = sync_request,
2009 static int __init raid6_init (void)
2013 e = raid6_select_algo();
2017 return register_md_personality (RAID6, &raid6_personality);
2020 static void raid6_exit (void)
2022 unregister_md_personality (RAID6);
2025 module_init(raid6_init);
2026 module_exit(raid6_exit);
2027 MODULE_LICENSE("GPL");
2028 MODULE_ALIAS("md-personality-8"); /* RAID6 */