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>
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 && 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 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 spin_lock_init(&sh->lock);
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 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
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 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
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->req.bi_max_vecs++;
482 dev->vec.bv_page = dev->page;
483 dev->vec.bv_len = STRIPE_SIZE;
484 dev->vec.bv_offset = 0;
486 dev->req.bi_sector = sh->sector;
487 dev->req.bi_private = sh;
490 if (i != pd_idx && i != qd_idx)
491 dev->sector = compute_blocknr(sh, i);
494 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
496 char b[BDEVNAME_SIZE];
497 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
498 PRINTK("raid6: error called\n");
503 conf->working_disks--;
505 conf->failed_disks++;
508 * if recovery was running, make sure it aborts.
510 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
514 "raid6: Disk failure on %s, disabling device."
515 " Operation continuing on %d devices\n",
516 bdevname(rdev->bdev,b), conf->working_disks);
521 * Input: a 'big' sector number,
522 * Output: index of the data and parity disk, and the sector # in them.
524 static sector_t raid6_compute_sector(sector_t r_sector, unsigned int raid_disks,
525 unsigned int data_disks, unsigned int * dd_idx,
526 unsigned int * pd_idx, raid6_conf_t *conf)
529 unsigned long chunk_number;
530 unsigned int chunk_offset;
532 int sectors_per_chunk = conf->chunk_size >> 9;
534 /* First compute the information on this sector */
537 * Compute the chunk number and the sector offset inside the chunk
539 chunk_offset = sector_div(r_sector, sectors_per_chunk);
540 chunk_number = r_sector;
541 if ( r_sector != chunk_number ) {
542 printk(KERN_CRIT "raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
543 (unsigned long long)r_sector, (unsigned long)chunk_number);
548 * Compute the stripe number
550 stripe = chunk_number / data_disks;
553 * Compute the data disk and parity disk indexes inside the stripe
555 *dd_idx = chunk_number % data_disks;
558 * Select the parity disk based on the user selected algorithm.
562 switch (conf->algorithm) {
563 case ALGORITHM_LEFT_ASYMMETRIC:
564 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
565 if (*pd_idx == raid_disks-1)
566 (*dd_idx)++; /* Q D D D P */
567 else if (*dd_idx >= *pd_idx)
568 (*dd_idx) += 2; /* D D P Q D */
570 case ALGORITHM_RIGHT_ASYMMETRIC:
571 *pd_idx = stripe % raid_disks;
572 if (*pd_idx == raid_disks-1)
573 (*dd_idx)++; /* Q D D D P */
574 else if (*dd_idx >= *pd_idx)
575 (*dd_idx) += 2; /* D D P Q D */
577 case ALGORITHM_LEFT_SYMMETRIC:
578 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
579 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
581 case ALGORITHM_RIGHT_SYMMETRIC:
582 *pd_idx = stripe % raid_disks;
583 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
586 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
590 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
591 chunk_number, *pd_idx, *dd_idx);
594 * Finally, compute the new sector number
596 new_sector = (sector_t) stripe * sectors_per_chunk + chunk_offset;
601 static sector_t compute_blocknr(struct stripe_head *sh, int i)
603 raid6_conf_t *conf = sh->raid_conf;
604 int raid_disks = conf->raid_disks, data_disks = raid_disks - 2;
605 sector_t new_sector = sh->sector, check;
606 int sectors_per_chunk = conf->chunk_size >> 9;
609 int chunk_number, dummy1, dummy2, dd_idx = i;
613 chunk_offset = sector_div(new_sector, sectors_per_chunk);
615 if ( new_sector != stripe ) {
616 printk(KERN_CRIT "raid6: ERROR: new_sector = %llu, stripe = %lu\n",
617 (unsigned long long)new_sector, (unsigned long)stripe);
621 switch (conf->algorithm) {
622 case ALGORITHM_LEFT_ASYMMETRIC:
623 case ALGORITHM_RIGHT_ASYMMETRIC:
624 if (sh->pd_idx == raid_disks-1)
626 else if (i > sh->pd_idx)
627 i -= 2; /* D D P Q D */
629 case ALGORITHM_LEFT_SYMMETRIC:
630 case ALGORITHM_RIGHT_SYMMETRIC:
631 if (sh->pd_idx == raid_disks-1)
637 i -= (sh->pd_idx + 2);
641 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
645 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh->pd_idx, i0, i);
647 chunk_number = stripe * data_disks + i;
648 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
650 check = raid6_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
651 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
652 printk(KERN_CRIT "raid6: compute_blocknr: map not correct\n");
661 * Copy data between a page in the stripe cache, and one or more bion
662 * The page could align with the middle of the bio, or there could be
663 * several bion, each with several bio_vecs, which cover part of the page
664 * Multiple bion are linked together on bi_next. There may be extras
665 * at the end of this list. We ignore them.
667 static void copy_data(int frombio, struct bio *bio,
671 char *pa = page_address(page);
676 if (bio->bi_sector >= sector)
677 page_offset = (signed)(bio->bi_sector - sector) * 512;
679 page_offset = (signed)(sector - bio->bi_sector) * -512;
680 bio_for_each_segment(bvl, bio, i) {
681 int len = bio_iovec_idx(bio,i)->bv_len;
685 if (page_offset < 0) {
686 b_offset = -page_offset;
687 page_offset += b_offset;
691 if (len > 0 && page_offset + len > STRIPE_SIZE)
692 clen = STRIPE_SIZE - page_offset;
696 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
698 memcpy(pa+page_offset, ba+b_offset, clen);
700 memcpy(ba+b_offset, pa+page_offset, clen);
701 __bio_kunmap_atomic(ba, KM_USER0);
703 if (clen < len) /* hit end of page */
709 #define check_xor() do { \
710 if (count == MAX_XOR_BLOCKS) { \
711 xor_block(count, STRIPE_SIZE, ptr); \
716 /* Compute P and Q syndromes */
717 static void compute_parity(struct stripe_head *sh, int method)
719 raid6_conf_t *conf = sh->raid_conf;
720 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
722 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
725 qd_idx = raid6_next_disk(pd_idx, disks);
726 d0_idx = raid6_next_disk(qd_idx, disks);
728 PRINTK("compute_parity, stripe %llu, method %d\n",
729 (unsigned long long)sh->sector, method);
732 case READ_MODIFY_WRITE:
733 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
734 case RECONSTRUCT_WRITE:
735 for (i= disks; i-- ;)
736 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
737 chosen = sh->dev[i].towrite;
738 sh->dev[i].towrite = NULL;
740 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
741 wake_up(&conf->wait_for_overlap);
743 if (sh->dev[i].written) BUG();
744 sh->dev[i].written = chosen;
748 BUG(); /* Not implemented yet */
751 for (i = disks; i--;)
752 if (sh->dev[i].written) {
753 sector_t sector = sh->dev[i].sector;
754 struct bio *wbi = sh->dev[i].written;
755 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
756 copy_data(1, wbi, sh->dev[i].page, sector);
757 wbi = r5_next_bio(wbi, sector);
760 set_bit(R5_LOCKED, &sh->dev[i].flags);
761 set_bit(R5_UPTODATE, &sh->dev[i].flags);
765 // case RECONSTRUCT_WRITE:
766 // case CHECK_PARITY:
767 // case UPDATE_PARITY:
768 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
769 /* FIX: Is this ordering of drives even remotely optimal? */
773 ptrs[count++] = page_address(sh->dev[i].page);
774 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
775 printk("block %d/%d not uptodate on parity calc\n", i,count);
776 i = raid6_next_disk(i, disks);
777 } while ( i != d0_idx );
781 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
784 case RECONSTRUCT_WRITE:
785 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
786 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
787 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
788 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
791 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
792 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
797 /* Compute one missing block */
798 static void compute_block_1(struct stripe_head *sh, int dd_idx)
800 raid6_conf_t *conf = sh->raid_conf;
801 int i, count, disks = conf->raid_disks;
802 void *ptr[MAX_XOR_BLOCKS], *p;
803 int pd_idx = sh->pd_idx;
804 int qd_idx = raid6_next_disk(pd_idx, disks);
806 PRINTK("compute_block_1, stripe %llu, idx %d\n",
807 (unsigned long long)sh->sector, dd_idx);
809 if ( dd_idx == qd_idx ) {
810 /* We're actually computing the Q drive */
811 compute_parity(sh, UPDATE_PARITY);
813 ptr[0] = page_address(sh->dev[dd_idx].page);
814 memset(ptr[0], 0, STRIPE_SIZE);
816 for (i = disks ; i--; ) {
817 if (i == dd_idx || i == qd_idx)
819 p = page_address(sh->dev[i].page);
820 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
823 printk("compute_block() %d, stripe %llu, %d"
824 " not present\n", dd_idx,
825 (unsigned long long)sh->sector, i);
830 xor_block(count, STRIPE_SIZE, ptr);
831 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
835 /* Compute two missing blocks */
836 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
838 raid6_conf_t *conf = sh->raid_conf;
839 int i, count, disks = conf->raid_disks;
840 int pd_idx = sh->pd_idx;
841 int qd_idx = raid6_next_disk(pd_idx, disks);
842 int d0_idx = raid6_next_disk(qd_idx, disks);
845 /* faila and failb are disk numbers relative to d0_idx */
846 /* pd_idx become disks-2 and qd_idx become disks-1 */
847 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
848 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
850 BUG_ON(faila == failb);
851 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
853 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
854 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
856 if ( failb == disks-1 ) {
857 /* Q disk is one of the missing disks */
858 if ( faila == disks-2 ) {
859 /* Missing P+Q, just recompute */
860 compute_parity(sh, UPDATE_PARITY);
863 /* We're missing D+Q; recompute D from P */
864 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1);
865 compute_parity(sh, UPDATE_PARITY); /* Is this necessary? */
870 /* We're missing D+P or D+D; build pointer table */
872 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
878 ptrs[count++] = page_address(sh->dev[i].page);
879 i = raid6_next_disk(i, disks);
880 if (i != dd_idx1 && i != dd_idx2 &&
881 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
882 printk("compute_2 with missing block %d/%d\n", count, i);
883 } while ( i != d0_idx );
885 if ( failb == disks-2 ) {
886 /* We're missing D+P. */
887 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
889 /* We're missing D+D. */
890 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
893 /* Both the above update both missing blocks */
894 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
895 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
901 * Each stripe/dev can have one or more bion attached.
902 * toread/towrite point to the first in a chain.
903 * The bi_next chain must be in order.
905 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
908 raid6_conf_t *conf = sh->raid_conf;
910 PRINTK("adding bh b#%llu to stripe s#%llu\n",
911 (unsigned long long)bi->bi_sector,
912 (unsigned long long)sh->sector);
915 spin_lock(&sh->lock);
916 spin_lock_irq(&conf->device_lock);
918 bip = &sh->dev[dd_idx].towrite;
920 bip = &sh->dev[dd_idx].toread;
921 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
922 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
924 bip = &(*bip)->bi_next;
926 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
929 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
934 bi->bi_phys_segments ++;
935 spin_unlock_irq(&conf->device_lock);
936 spin_unlock(&sh->lock);
938 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
939 (unsigned long long)bi->bi_sector,
940 (unsigned long long)sh->sector, dd_idx);
943 /* check if page is covered */
944 sector_t sector = sh->dev[dd_idx].sector;
945 for (bi=sh->dev[dd_idx].towrite;
946 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
947 bi && bi->bi_sector <= sector;
948 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
949 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
950 sector = bi->bi_sector + (bi->bi_size>>9);
952 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
953 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
958 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
959 spin_unlock_irq(&conf->device_lock);
960 spin_unlock(&sh->lock);
966 * handle_stripe - do things to a stripe.
968 * We lock the stripe and then examine the state of various bits
969 * to see what needs to be done.
971 * return some read request which now have data
972 * return some write requests which are safely on disc
973 * schedule a read on some buffers
974 * schedule a write of some buffers
975 * return confirmation of parity correctness
977 * Parity calculations are done inside the stripe lock
978 * buffers are taken off read_list or write_list, and bh_cache buffers
979 * get BH_Lock set before the stripe lock is released.
983 static void handle_stripe(struct stripe_head *sh)
985 raid6_conf_t *conf = sh->raid_conf;
986 int disks = conf->raid_disks;
987 struct bio *return_bi= NULL;
991 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
992 int non_overwrite = 0;
993 int failed_num[2] = {0, 0};
994 struct r5dev *dev, *pdev, *qdev;
995 int pd_idx = sh->pd_idx;
996 int qd_idx = raid6_next_disk(pd_idx, disks);
997 int p_failed, q_failed;
999 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1000 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1003 spin_lock(&sh->lock);
1004 clear_bit(STRIPE_HANDLE, &sh->state);
1005 clear_bit(STRIPE_DELAYED, &sh->state);
1007 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1008 /* Now to look around and see what can be done */
1010 for (i=disks; i--; ) {
1013 clear_bit(R5_Insync, &dev->flags);
1014 clear_bit(R5_Syncio, &dev->flags);
1016 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1017 i, dev->flags, dev->toread, dev->towrite, dev->written);
1018 /* maybe we can reply to a read */
1019 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1020 struct bio *rbi, *rbi2;
1021 PRINTK("Return read for disc %d\n", i);
1022 spin_lock_irq(&conf->device_lock);
1025 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1026 wake_up(&conf->wait_for_overlap);
1027 spin_unlock_irq(&conf->device_lock);
1028 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1029 copy_data(0, rbi, dev->page, dev->sector);
1030 rbi2 = r5_next_bio(rbi, dev->sector);
1031 spin_lock_irq(&conf->device_lock);
1032 if (--rbi->bi_phys_segments == 0) {
1033 rbi->bi_next = return_bi;
1036 spin_unlock_irq(&conf->device_lock);
1041 /* now count some things */
1042 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1043 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1046 if (dev->toread) to_read++;
1049 if (!test_bit(R5_OVERWRITE, &dev->flags))
1052 if (dev->written) written++;
1053 rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
1054 if (!rdev || !rdev->in_sync) {
1056 failed_num[failed] = i;
1059 set_bit(R5_Insync, &dev->flags);
1061 PRINTK("locked=%d uptodate=%d to_read=%d"
1062 " to_write=%d failed=%d failed_num=%d,%d\n",
1063 locked, uptodate, to_read, to_write, failed,
1064 failed_num[0], failed_num[1]);
1065 /* check if the array has lost >2 devices and, if so, some requests might
1068 if (failed > 2 && to_read+to_write+written) {
1069 spin_lock_irq(&conf->device_lock);
1070 for (i=disks; i--; ) {
1071 /* fail all writes first */
1072 bi = sh->dev[i].towrite;
1073 sh->dev[i].towrite = NULL;
1076 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1077 wake_up(&conf->wait_for_overlap);
1079 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1080 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1081 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1082 if (--bi->bi_phys_segments == 0) {
1083 md_write_end(conf->mddev);
1084 bi->bi_next = return_bi;
1089 /* and fail all 'written' */
1090 bi = sh->dev[i].written;
1091 sh->dev[i].written = NULL;
1092 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1093 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1094 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1095 if (--bi->bi_phys_segments == 0) {
1096 md_write_end(conf->mddev);
1097 bi->bi_next = return_bi;
1103 /* fail any reads if this device is non-operational */
1104 if (!test_bit(R5_Insync, &sh->dev[i].flags)) {
1105 bi = sh->dev[i].toread;
1106 sh->dev[i].toread = NULL;
1107 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1108 wake_up(&conf->wait_for_overlap);
1110 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1111 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1112 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1113 if (--bi->bi_phys_segments == 0) {
1114 bi->bi_next = return_bi;
1121 spin_unlock_irq(&conf->device_lock);
1123 if (failed > 2 && syncing) {
1124 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1125 clear_bit(STRIPE_SYNCING, &sh->state);
1130 * might be able to return some write requests if the parity blocks
1131 * are safe, or on a failed drive
1133 pdev = &sh->dev[pd_idx];
1134 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
1135 || (failed >= 2 && failed_num[1] == pd_idx);
1136 qdev = &sh->dev[qd_idx];
1137 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
1138 || (failed >= 2 && failed_num[1] == qd_idx);
1141 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
1142 && !test_bit(R5_LOCKED, &pdev->flags)
1143 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
1144 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
1145 && !test_bit(R5_LOCKED, &qdev->flags)
1146 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
1147 /* any written block on an uptodate or failed drive can be
1148 * returned. Note that if we 'wrote' to a failed drive,
1149 * it will be UPTODATE, but never LOCKED, so we don't need
1150 * to test 'failed' directly.
1152 for (i=disks; i--; )
1153 if (sh->dev[i].written) {
1155 if (!test_bit(R5_LOCKED, &dev->flags) &&
1156 test_bit(R5_UPTODATE, &dev->flags) ) {
1157 /* We can return any write requests */
1158 struct bio *wbi, *wbi2;
1159 PRINTK("Return write for stripe %llu disc %d\n",
1160 (unsigned long long)sh->sector, i);
1161 spin_lock_irq(&conf->device_lock);
1163 dev->written = NULL;
1164 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1165 wbi2 = r5_next_bio(wbi, dev->sector);
1166 if (--wbi->bi_phys_segments == 0) {
1167 md_write_end(conf->mddev);
1168 wbi->bi_next = return_bi;
1173 spin_unlock_irq(&conf->device_lock);
1178 /* Now we might consider reading some blocks, either to check/generate
1179 * parity, or to satisfy requests
1180 * or to load a block that is being partially written.
1182 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
1183 for (i=disks; i--;) {
1185 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1187 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1189 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
1190 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
1193 /* we would like to get this block, possibly
1194 * by computing it, but we might not be able to
1196 if (uptodate == disks-1) {
1197 PRINTK("Computing stripe %llu block %d\n",
1198 (unsigned long long)sh->sector, i);
1199 compute_block_1(sh, i);
1201 } else if ( uptodate == disks-2 && failed >= 2 ) {
1202 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1204 for (other=disks; other--;) {
1207 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
1211 PRINTK("Computing stripe %llu blocks %d,%d\n",
1212 (unsigned long long)sh->sector, i, other);
1213 compute_block_2(sh, i, other);
1215 } else if (test_bit(R5_Insync, &dev->flags)) {
1216 set_bit(R5_LOCKED, &dev->flags);
1217 set_bit(R5_Wantread, &dev->flags);
1219 /* if I am just reading this block and we don't have
1220 a failed drive, or any pending writes then sidestep the cache */
1221 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1222 ! syncing && !failed && !to_write) {
1223 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1224 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1228 PRINTK("Reading block %d (sync=%d)\n",
1231 md_sync_acct(conf->disks[i].rdev->bdev,
1236 set_bit(STRIPE_HANDLE, &sh->state);
1239 /* now to consider writing and what else, if anything should be read */
1241 int rcw=0, must_compute=0;
1242 for (i=disks ; i--;) {
1244 /* Would I have to read this buffer for reconstruct_write */
1245 if (!test_bit(R5_OVERWRITE, &dev->flags)
1246 && i != pd_idx && i != qd_idx
1247 && (!test_bit(R5_LOCKED, &dev->flags)
1249 || sh->bh_page[i] != bh->b_page
1252 !test_bit(R5_UPTODATE, &dev->flags)) {
1253 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1255 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
1260 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1261 (unsigned long long)sh->sector, rcw, must_compute);
1262 set_bit(STRIPE_HANDLE, &sh->state);
1265 /* want reconstruct write, but need to get some data */
1266 for (i=disks; i--;) {
1268 if (!test_bit(R5_OVERWRITE, &dev->flags)
1269 && !(failed == 0 && (i == pd_idx || i == qd_idx))
1270 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1271 test_bit(R5_Insync, &dev->flags)) {
1272 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1274 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1275 (unsigned long long)sh->sector, i);
1276 set_bit(R5_LOCKED, &dev->flags);
1277 set_bit(R5_Wantread, &dev->flags);
1280 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1281 (unsigned long long)sh->sector, i);
1282 set_bit(STRIPE_DELAYED, &sh->state);
1283 set_bit(STRIPE_HANDLE, &sh->state);
1287 /* now if nothing is locked, and if we have enough data, we can start a write request */
1288 if (locked == 0 && rcw == 0) {
1289 if ( must_compute > 0 ) {
1290 /* We have failed blocks and need to compute them */
1293 case 1: compute_block_1(sh, failed_num[0]); break;
1294 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
1295 default: BUG(); /* This request should have been failed? */
1299 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
1300 compute_parity(sh, RECONSTRUCT_WRITE);
1301 /* now every locked buffer is ready to be written */
1303 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1304 PRINTK("Writing stripe %llu block %d\n",
1305 (unsigned long long)sh->sector, i);
1307 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1308 #if 0 /**** FIX: I don't understand the logic here... ****/
1309 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1310 || ((i==pd_idx || i==qd_idx) && failed == 0)) /* FIX? */
1311 set_bit(STRIPE_INSYNC, &sh->state);
1314 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1315 atomic_dec(&conf->preread_active_stripes);
1316 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1317 md_wakeup_thread(conf->mddev->thread);
1322 /* maybe we need to check and possibly fix the parity for this stripe
1323 * Any reads will already have been scheduled, so we just see if enough data
1326 if (syncing && locked == 0 &&
1327 !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 2) {
1328 set_bit(STRIPE_HANDLE, &sh->state);
1329 #if 0 /* RAID-6: Don't support CHECK PARITY yet */
1332 if (uptodate != disks)
1334 compute_parity(sh, CHECK_PARITY);
1336 pagea = page_address(sh->dev[pd_idx].page);
1337 if ((*(u32*)pagea) == 0 &&
1338 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1339 /* parity is correct (on disc, not in buffer any more) */
1340 set_bit(STRIPE_INSYNC, &sh->state);
1344 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1345 int failed_needupdate[2];
1346 struct r5dev *adev, *bdev;
1349 failed_num[0] = pd_idx;
1351 failed_num[1] = (failed_num[0] == qd_idx) ? pd_idx : qd_idx;
1353 failed_needupdate[0] = !test_bit(R5_UPTODATE, &sh->dev[failed_num[0]].flags);
1354 failed_needupdate[1] = !test_bit(R5_UPTODATE, &sh->dev[failed_num[1]].flags);
1356 PRINTK("sync: failed=%d num=%d,%d fnu=%u%u\n",
1357 failed, failed_num[0], failed_num[1], failed_needupdate[0], failed_needupdate[1]);
1359 #if 0 /* RAID-6: This code seems to require that CHECK_PARITY destroys the uptodateness of the parity */
1360 /* should be able to compute the missing block(s) and write to spare */
1361 if ( failed_needupdate[0] ^ failed_needupdate[1] ) {
1362 if (uptodate+1 != disks)
1364 compute_block_1(sh, failed_needupdate[0] ? failed_num[0] : failed_num[1]);
1366 } else if ( failed_needupdate[0] & failed_needupdate[1] ) {
1367 if (uptodate+2 != disks)
1369 compute_block_2(sh, failed_num[0], failed_num[1]);
1373 compute_block_2(sh, failed_num[0], failed_num[1]);
1374 uptodate += failed_needupdate[0] + failed_needupdate[1];
1377 if (uptodate != disks)
1380 PRINTK("Marking for sync stripe %llu blocks %d,%d\n",
1381 (unsigned long long)sh->sector, failed_num[0], failed_num[1]);
1383 /**** FIX: Should we really do both of these unconditionally? ****/
1384 adev = &sh->dev[failed_num[0]];
1385 locked += !test_bit(R5_LOCKED, &adev->flags);
1386 set_bit(R5_LOCKED, &adev->flags);
1387 set_bit(R5_Wantwrite, &adev->flags);
1388 bdev = &sh->dev[failed_num[1]];
1389 locked += !test_bit(R5_LOCKED, &bdev->flags);
1390 set_bit(R5_LOCKED, &bdev->flags);
1391 set_bit(R5_Wantwrite, &bdev->flags);
1393 set_bit(STRIPE_INSYNC, &sh->state);
1394 set_bit(R5_Syncio, &adev->flags);
1395 set_bit(R5_Syncio, &bdev->flags);
1398 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1399 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1400 clear_bit(STRIPE_SYNCING, &sh->state);
1403 spin_unlock(&sh->lock);
1405 while ((bi=return_bi)) {
1406 int bytes = bi->bi_size;
1408 return_bi = bi->bi_next;
1411 bi->bi_end_io(bi, bytes, 0);
1413 for (i=disks; i-- ;) {
1417 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1419 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1424 bi = &sh->dev[i].req;
1428 bi->bi_end_io = raid6_end_write_request;
1430 bi->bi_end_io = raid6_end_read_request;
1433 rdev = conf->disks[i].rdev;
1434 if (rdev && rdev->faulty)
1437 atomic_inc(&rdev->nr_pending);
1441 if (test_bit(R5_Syncio, &sh->dev[i].flags))
1442 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1444 bi->bi_bdev = rdev->bdev;
1445 PRINTK("for %llu schedule op %ld on disc %d\n",
1446 (unsigned long long)sh->sector, bi->bi_rw, i);
1447 atomic_inc(&sh->count);
1448 bi->bi_sector = sh->sector + rdev->data_offset;
1449 bi->bi_flags = 1 << BIO_UPTODATE;
1451 bi->bi_max_vecs = 1;
1453 bi->bi_io_vec = &sh->dev[i].vec;
1454 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1455 bi->bi_io_vec[0].bv_offset = 0;
1456 bi->bi_size = STRIPE_SIZE;
1458 generic_make_request(bi);
1460 PRINTK("skip op %ld on disc %d for sector %llu\n",
1461 bi->bi_rw, i, (unsigned long long)sh->sector);
1462 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1463 set_bit(STRIPE_HANDLE, &sh->state);
1468 static inline void raid6_activate_delayed(raid6_conf_t *conf)
1470 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1471 while (!list_empty(&conf->delayed_list)) {
1472 struct list_head *l = conf->delayed_list.next;
1473 struct stripe_head *sh;
1474 sh = list_entry(l, struct stripe_head, lru);
1476 clear_bit(STRIPE_DELAYED, &sh->state);
1477 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1478 atomic_inc(&conf->preread_active_stripes);
1479 list_add_tail(&sh->lru, &conf->handle_list);
1484 static void unplug_slaves(mddev_t *mddev)
1486 raid6_conf_t *conf = mddev_to_conf(mddev);
1490 for (i=0; i<mddev->raid_disks; i++) {
1491 mdk_rdev_t *rdev = conf->disks[i].rdev;
1492 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
1493 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1495 atomic_inc(&rdev->nr_pending);
1498 if (r_queue->unplug_fn)
1499 r_queue->unplug_fn(r_queue);
1501 rdev_dec_pending(rdev, mddev);
1508 static void raid6_unplug_device(request_queue_t *q)
1510 mddev_t *mddev = q->queuedata;
1511 raid6_conf_t *conf = mddev_to_conf(mddev);
1512 unsigned long flags;
1514 spin_lock_irqsave(&conf->device_lock, flags);
1516 if (blk_remove_plug(q))
1517 raid6_activate_delayed(conf);
1518 md_wakeup_thread(mddev->thread);
1520 spin_unlock_irqrestore(&conf->device_lock, flags);
1522 unplug_slaves(mddev);
1525 static int raid6_issue_flush(request_queue_t *q, struct gendisk *disk,
1526 sector_t *error_sector)
1528 mddev_t *mddev = q->queuedata;
1529 raid6_conf_t *conf = mddev_to_conf(mddev);
1533 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1534 mdk_rdev_t *rdev = conf->disks[i].rdev;
1535 if (rdev && !rdev->faulty) {
1536 struct block_device *bdev = rdev->bdev;
1537 request_queue_t *r_queue = bdev_get_queue(bdev);
1539 if (!r_queue->issue_flush_fn)
1542 atomic_inc(&rdev->nr_pending);
1544 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1546 rdev_dec_pending(rdev, mddev);
1555 static inline void raid6_plug_device(raid6_conf_t *conf)
1557 spin_lock_irq(&conf->device_lock);
1558 blk_plug_device(conf->mddev->queue);
1559 spin_unlock_irq(&conf->device_lock);
1562 static int make_request (request_queue_t *q, struct bio * bi)
1564 mddev_t *mddev = q->queuedata;
1565 raid6_conf_t *conf = mddev_to_conf(mddev);
1566 const unsigned int raid_disks = conf->raid_disks;
1567 const unsigned int data_disks = raid_disks - 2;
1568 unsigned int dd_idx, pd_idx;
1569 sector_t new_sector;
1570 sector_t logical_sector, last_sector;
1571 struct stripe_head *sh;
1573 if (bio_data_dir(bi)==WRITE) {
1574 disk_stat_inc(mddev->gendisk, writes);
1575 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi));
1577 disk_stat_inc(mddev->gendisk, reads);
1578 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi));
1581 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1582 last_sector = bi->bi_sector + (bi->bi_size>>9);
1585 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1586 if ( bio_data_dir(bi) == WRITE )
1587 md_write_start(mddev);
1588 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1591 new_sector = raid6_compute_sector(logical_sector,
1592 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1594 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1595 (unsigned long long)new_sector,
1596 (unsigned long long)logical_sector);
1599 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1600 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1602 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1603 /* Add failed due to overlap. Flush everything
1606 raid6_unplug_device(mddev->queue);
1611 finish_wait(&conf->wait_for_overlap, &w);
1612 raid6_plug_device(conf);
1616 /* cannot get stripe for read-ahead, just give-up */
1617 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1618 finish_wait(&conf->wait_for_overlap, &w);
1623 spin_lock_irq(&conf->device_lock);
1624 if (--bi->bi_phys_segments == 0) {
1625 int bytes = bi->bi_size;
1627 if ( bio_data_dir(bi) == WRITE )
1628 md_write_end(mddev);
1630 bi->bi_end_io(bi, bytes, 0);
1632 spin_unlock_irq(&conf->device_lock);
1636 /* FIXME go_faster isn't used */
1637 static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
1639 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1640 struct stripe_head *sh;
1641 int sectors_per_chunk = conf->chunk_size >> 9;
1643 unsigned long stripe;
1646 sector_t first_sector;
1647 int raid_disks = conf->raid_disks;
1648 int data_disks = raid_disks - 2;
1650 if (sector_nr >= mddev->size <<1) {
1651 /* just being told to finish up .. nothing much to do */
1652 unplug_slaves(mddev);
1655 /* if there are 2 or more failed drives and we are trying
1656 * to resync, then assert that we are finished, because there is
1657 * nothing we can do.
1659 if (mddev->degraded >= 2 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1660 int rv = (mddev->size << 1) - sector_nr;
1661 md_done_sync(mddev, rv, 1);
1666 chunk_offset = sector_div(x, sectors_per_chunk);
1668 BUG_ON(x != stripe);
1670 first_sector = raid6_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1671 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1672 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1674 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1675 /* make sure we don't swamp the stripe cache if someone else
1676 * is trying to get access
1678 set_current_state(TASK_UNINTERRUPTIBLE);
1679 schedule_timeout(1);
1681 spin_lock(&sh->lock);
1682 set_bit(STRIPE_SYNCING, &sh->state);
1683 clear_bit(STRIPE_INSYNC, &sh->state);
1684 spin_unlock(&sh->lock);
1689 return STRIPE_SECTORS;
1693 * This is our raid6 kernel thread.
1695 * We scan the hash table for stripes which can be handled now.
1696 * During the scan, completed stripes are saved for us by the interrupt
1697 * handler, so that they will not have to wait for our next wakeup.
1699 static void raid6d (mddev_t *mddev)
1701 struct stripe_head *sh;
1702 raid6_conf_t *conf = mddev_to_conf(mddev);
1705 PRINTK("+++ raid6d active\n");
1707 md_check_recovery(mddev);
1708 md_handle_safemode(mddev);
1711 spin_lock_irq(&conf->device_lock);
1713 struct list_head *first;
1715 if (list_empty(&conf->handle_list) &&
1716 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1717 !blk_queue_plugged(mddev->queue) &&
1718 !list_empty(&conf->delayed_list))
1719 raid6_activate_delayed(conf);
1721 if (list_empty(&conf->handle_list))
1724 first = conf->handle_list.next;
1725 sh = list_entry(first, struct stripe_head, lru);
1727 list_del_init(first);
1728 atomic_inc(&sh->count);
1729 if (atomic_read(&sh->count)!= 1)
1731 spin_unlock_irq(&conf->device_lock);
1737 spin_lock_irq(&conf->device_lock);
1739 PRINTK("%d stripes handled\n", handled);
1741 spin_unlock_irq(&conf->device_lock);
1743 unplug_slaves(mddev);
1745 PRINTK("--- raid6d inactive\n");
1748 static int run (mddev_t *mddev)
1751 int raid_disk, memory;
1753 struct disk_info *disk;
1754 struct list_head *tmp;
1756 if (mddev->level != 6) {
1757 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev), mddev->level);
1761 mddev->private = kmalloc (sizeof (raid6_conf_t)
1762 + mddev->raid_disks * sizeof(struct disk_info),
1764 if ((conf = mddev->private) == NULL)
1766 memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1767 conf->mddev = mddev;
1769 if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1771 memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1773 spin_lock_init(&conf->device_lock);
1774 init_waitqueue_head(&conf->wait_for_stripe);
1775 init_waitqueue_head(&conf->wait_for_overlap);
1776 INIT_LIST_HEAD(&conf->handle_list);
1777 INIT_LIST_HEAD(&conf->delayed_list);
1778 INIT_LIST_HEAD(&conf->inactive_list);
1779 atomic_set(&conf->active_stripes, 0);
1780 atomic_set(&conf->preread_active_stripes, 0);
1782 PRINTK("raid6: run(%s) called.\n", mdname(mddev));
1784 ITERATE_RDEV(mddev,rdev,tmp) {
1785 raid_disk = rdev->raid_disk;
1786 if (raid_disk >= mddev->raid_disks
1789 disk = conf->disks + raid_disk;
1793 if (rdev->in_sync) {
1794 char b[BDEVNAME_SIZE];
1795 printk(KERN_INFO "raid6: device %s operational as raid"
1796 " disk %d\n", bdevname(rdev->bdev,b),
1798 conf->working_disks++;
1802 conf->raid_disks = mddev->raid_disks;
1805 * 0 for a fully functional array, 1 or 2 for a degraded array.
1807 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1808 conf->mddev = mddev;
1809 conf->chunk_size = mddev->chunk_size;
1810 conf->level = mddev->level;
1811 conf->algorithm = mddev->layout;
1812 conf->max_nr_stripes = NR_STRIPES;
1814 /* device size must be a multiple of chunk size */
1815 mddev->size &= ~(mddev->chunk_size/1024 -1);
1817 if (conf->raid_disks < 4) {
1818 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
1819 mdname(mddev), conf->raid_disks);
1822 if (!conf->chunk_size || conf->chunk_size % 4) {
1823 printk(KERN_ERR "raid6: invalid chunk size %d for %s\n",
1824 conf->chunk_size, mdname(mddev));
1827 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1829 "raid6: unsupported parity algorithm %d for %s\n",
1830 conf->algorithm, mdname(mddev));
1833 if (mddev->degraded > 2) {
1834 printk(KERN_ERR "raid6: not enough operational devices for %s"
1835 " (%d/%d failed)\n",
1836 mdname(mddev), conf->failed_disks, conf->raid_disks);
1840 #if 0 /* FIX: For now */
1841 if (mddev->degraded > 0 &&
1842 mddev->recovery_cp != MaxSector) {
1843 printk(KERN_ERR "raid6: cannot start dirty degraded array for %s\n", mdname(mddev));
1849 mddev->thread = md_register_thread(raid6d, mddev, "%s_raid6");
1850 if (!mddev->thread) {
1852 "raid6: couldn't allocate thread for %s\n",
1858 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1859 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1860 if (grow_stripes(conf, conf->max_nr_stripes)) {
1862 "raid6: couldn't allocate %dkB for buffers\n", memory);
1863 shrink_stripes(conf);
1864 md_unregister_thread(mddev->thread);
1867 printk(KERN_INFO "raid6: allocated %dkB for %s\n",
1868 memory, mdname(mddev));
1870 if (mddev->degraded == 0)
1871 printk(KERN_INFO "raid6: raid level %d set %s active with %d out of %d"
1872 " devices, algorithm %d\n", conf->level, mdname(mddev),
1873 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1876 printk(KERN_ALERT "raid6: raid level %d set %s active with %d"
1877 " out of %d devices, algorithm %d\n", conf->level,
1878 mdname(mddev), mddev->raid_disks - mddev->degraded,
1879 mddev->raid_disks, conf->algorithm);
1881 print_raid6_conf(conf);
1883 /* read-ahead size must cover two whole stripes, which is
1884 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
1887 int stripe = (mddev->raid_disks-2) * mddev->chunk_size
1889 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1890 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1893 /* Ok, everything is just fine now */
1894 mddev->array_size = mddev->size * (mddev->raid_disks - 2);
1896 mddev->queue->unplug_fn = raid6_unplug_device;
1897 mddev->queue->issue_flush_fn = raid6_issue_flush;
1901 print_raid6_conf(conf);
1902 if (conf->stripe_hashtbl)
1903 free_pages((unsigned long) conf->stripe_hashtbl,
1907 mddev->private = NULL;
1908 printk(KERN_ALERT "raid6: failed to run raid set %s\n", mdname(mddev));
1914 static int stop (mddev_t *mddev)
1916 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1918 md_unregister_thread(mddev->thread);
1919 mddev->thread = NULL;
1920 shrink_stripes(conf);
1921 free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
1922 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1924 mddev->private = NULL;
1929 static void print_sh (struct seq_file *seq, struct stripe_head *sh)
1933 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
1934 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
1935 seq_printf(seq, "sh %llu, count %d.\n",
1936 (unsigned long long)sh->sector, atomic_read(&sh->count));
1937 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
1938 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
1939 seq_printf(seq, "(cache%d: %p %ld) ",
1940 i, sh->dev[i].page, sh->dev[i].flags);
1942 seq_printf(seq, "\n");
1945 static void printall (struct seq_file *seq, raid6_conf_t *conf)
1947 struct stripe_head *sh;
1950 spin_lock_irq(&conf->device_lock);
1951 for (i = 0; i < NR_HASH; i++) {
1952 sh = conf->stripe_hashtbl[i];
1953 for (; sh; sh = sh->hash_next) {
1954 if (sh->raid_conf != conf)
1959 spin_unlock_irq(&conf->device_lock);
1963 static void status (struct seq_file *seq, mddev_t *mddev)
1965 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1968 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
1969 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
1970 for (i = 0; i < conf->raid_disks; i++)
1971 seq_printf (seq, "%s",
1972 conf->disks[i].rdev &&
1973 conf->disks[i].rdev->in_sync ? "U" : "_");
1974 seq_printf (seq, "]");
1976 seq_printf (seq, "\n");
1977 printall(seq, conf);
1981 static void print_raid6_conf (raid6_conf_t *conf)
1984 struct disk_info *tmp;
1986 printk("RAID6 conf printout:\n");
1988 printk("(conf==NULL)\n");
1991 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
1992 conf->working_disks, conf->failed_disks);
1994 for (i = 0; i < conf->raid_disks; i++) {
1995 char b[BDEVNAME_SIZE];
1996 tmp = conf->disks + i;
1998 printk(" disk %d, o:%d, dev:%s\n",
1999 i, !tmp->rdev->faulty,
2000 bdevname(tmp->rdev->bdev,b));
2004 static int raid6_spare_active(mddev_t *mddev)
2007 raid6_conf_t *conf = mddev->private;
2008 struct disk_info *tmp;
2010 for (i = 0; i < conf->raid_disks; i++) {
2011 tmp = conf->disks + i;
2013 && !tmp->rdev->faulty
2014 && !tmp->rdev->in_sync) {
2016 conf->failed_disks--;
2017 conf->working_disks++;
2018 tmp->rdev->in_sync = 1;
2021 print_raid6_conf(conf);
2025 static int raid6_remove_disk(mddev_t *mddev, int number)
2027 raid6_conf_t *conf = mddev->private;
2030 struct disk_info *p = conf->disks + number;
2032 print_raid6_conf(conf);
2035 if (rdev->in_sync ||
2036 atomic_read(&rdev->nr_pending)) {
2042 if (atomic_read(&rdev->nr_pending)) {
2043 /* lost the race, try later */
2051 print_raid6_conf(conf);
2055 static int raid6_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2057 raid6_conf_t *conf = mddev->private;
2060 struct disk_info *p;
2062 if (mddev->degraded > 2)
2063 /* no point adding a device */
2068 for (disk=0; disk < mddev->raid_disks; disk++)
2069 if ((p=conf->disks + disk)->rdev == NULL) {
2071 rdev->raid_disk = disk;
2076 print_raid6_conf(conf);
2080 static int raid6_resize(mddev_t *mddev, sector_t sectors)
2082 /* no resync is happening, and there is enough space
2083 * on all devices, so we can resize.
2084 * We need to make sure resync covers any new space.
2085 * If the array is shrinking we should possibly wait until
2086 * any io in the removed space completes, but it hardly seems
2089 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2090 mddev->array_size = (sectors * (mddev->raid_disks-2))>>1;
2091 set_capacity(mddev->gendisk, mddev->array_size << 1);
2093 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2094 mddev->recovery_cp = mddev->size << 1;
2095 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2097 mddev->size = sectors /2;
2101 static mdk_personality_t raid6_personality=
2104 .owner = THIS_MODULE,
2105 .make_request = make_request,
2109 .error_handler = error,
2110 .hot_add_disk = raid6_add_disk,
2111 .hot_remove_disk= raid6_remove_disk,
2112 .spare_active = raid6_spare_active,
2113 .sync_request = sync_request,
2114 .resize = raid6_resize,
2117 static int __init raid6_init (void)
2121 e = raid6_select_algo();
2125 return register_md_personality (RAID6, &raid6_personality);
2128 static void raid6_exit (void)
2130 unregister_md_personality (RAID6);
2133 module_init(raid6_init);
2134 module_exit(raid6_exit);
2135 MODULE_LICENSE("GPL");
2136 MODULE_ALIAS("md-personality-8"); /* RAID6 */