2 * raid6main.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-6 management functions. This code is derived from raid5.c.
8 * Last merge from raid5.c bkcvs version 1.79 (kernel 2.6.1).
10 * Thanks to Penguin Computing for making the RAID-6 development possible
11 * by donating a test server!
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * You should have received a copy of the GNU General Public License
19 * (for example /usr/src/linux/COPYING); if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/highmem.h>
28 #include <linux/bitops.h>
29 #include <asm/atomic.h>
32 #include <linux/raid/bitmap.h>
38 #define NR_STRIPES 256
39 #define STRIPE_SIZE PAGE_SIZE
40 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
41 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
42 #define IO_THRESHOLD 1
44 #define HASH_PAGES_ORDER 0
45 #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
46 #define HASH_MASK (NR_HASH - 1)
48 #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
50 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
51 * order without overlap. There may be several bio's per stripe+device, and
52 * a bio could span several devices.
53 * When walking this list for a particular stripe+device, we must never proceed
54 * beyond a bio that extends past this device, as the next bio might no longer
56 * This macro is used to determine the 'next' bio in the list, given the sector
57 * of the current stripe+device
59 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
61 * The following can be used to debug the driver
63 #define RAID6_DEBUG 0 /* Extremely verbose printk */
64 #define RAID6_PARANOIA 1 /* Check spinlocks */
65 #define RAID6_DUMPSTATE 0 /* Include stripe cache state in /proc/mdstat */
66 #if RAID6_PARANOIA && defined(CONFIG_SMP)
67 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
69 # define CHECK_DEVLOCK()
72 #define PRINTK(x...) ((void)(RAID6_DEBUG && printk(KERN_DEBUG x)))
80 #if !RAID6_USE_EMPTY_ZERO_PAGE
81 /* In .bss so it's zeroed */
82 const char raid6_empty_zero_page
[PAGE_SIZE
] __attribute__((aligned(256)));
85 static inline int raid6_next_disk(int disk
, int raid_disks
)
88 return (disk
< raid_disks
) ? disk
: 0;
91 static void print_raid6_conf (raid6_conf_t
*conf
);
93 static inline void __release_stripe(raid6_conf_t
*conf
, struct stripe_head
*sh
)
95 if (atomic_dec_and_test(&sh
->count
)) {
96 if (!list_empty(&sh
->lru
))
98 if (atomic_read(&conf
->active_stripes
)==0)
100 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
101 if (test_bit(STRIPE_DELAYED
, &sh
->state
))
102 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
103 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
104 conf
->seq_write
== sh
->bm_seq
)
105 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
107 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
108 list_add_tail(&sh
->lru
, &conf
->handle_list
);
110 md_wakeup_thread(conf
->mddev
->thread
);
112 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
113 atomic_dec(&conf
->preread_active_stripes
);
114 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
115 md_wakeup_thread(conf
->mddev
->thread
);
117 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
118 atomic_dec(&conf
->active_stripes
);
119 if (!conf
->inactive_blocked
||
120 atomic_read(&conf
->active_stripes
) < (NR_STRIPES
*3/4))
121 wake_up(&conf
->wait_for_stripe
);
125 static void release_stripe(struct stripe_head
*sh
)
127 raid6_conf_t
*conf
= sh
->raid_conf
;
130 spin_lock_irqsave(&conf
->device_lock
, flags
);
131 __release_stripe(conf
, sh
);
132 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
135 static void remove_hash(struct stripe_head
*sh
)
137 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
139 if (sh
->hash_pprev
) {
141 sh
->hash_next
->hash_pprev
= sh
->hash_pprev
;
142 *sh
->hash_pprev
= sh
->hash_next
;
143 sh
->hash_pprev
= NULL
;
147 static __inline__
void insert_hash(raid6_conf_t
*conf
, struct stripe_head
*sh
)
149 struct stripe_head
**shp
= &stripe_hash(conf
, sh
->sector
);
151 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
154 if ((sh
->hash_next
= *shp
) != NULL
)
155 (*shp
)->hash_pprev
= &sh
->hash_next
;
157 sh
->hash_pprev
= shp
;
161 /* find an idle stripe, make sure it is unhashed, and return it. */
162 static struct stripe_head
*get_free_stripe(raid6_conf_t
*conf
)
164 struct stripe_head
*sh
= NULL
;
165 struct list_head
*first
;
168 if (list_empty(&conf
->inactive_list
))
170 first
= conf
->inactive_list
.next
;
171 sh
= list_entry(first
, struct stripe_head
, lru
);
172 list_del_init(first
);
174 atomic_inc(&conf
->active_stripes
);
179 static void shrink_buffers(struct stripe_head
*sh
, int num
)
184 for (i
=0; i
<num
; i
++) {
188 sh
->dev
[i
].page
= NULL
;
189 page_cache_release(p
);
193 static int grow_buffers(struct stripe_head
*sh
, int num
)
197 for (i
=0; i
<num
; i
++) {
200 if (!(page
= alloc_page(GFP_KERNEL
))) {
203 sh
->dev
[i
].page
= page
;
208 static void raid6_build_block (struct stripe_head
*sh
, int i
);
210 static inline void init_stripe(struct stripe_head
*sh
, sector_t sector
, int pd_idx
)
212 raid6_conf_t
*conf
= sh
->raid_conf
;
213 int disks
= conf
->raid_disks
, i
;
215 if (atomic_read(&sh
->count
) != 0)
217 if (test_bit(STRIPE_HANDLE
, &sh
->state
))
221 PRINTK("init_stripe called, stripe %llu\n",
222 (unsigned long long)sh
->sector
);
230 for (i
=disks
; i
--; ) {
231 struct r5dev
*dev
= &sh
->dev
[i
];
233 if (dev
->toread
|| dev
->towrite
|| dev
->written
||
234 test_bit(R5_LOCKED
, &dev
->flags
)) {
235 PRINTK("sector=%llx i=%d %p %p %p %d\n",
236 (unsigned long long)sh
->sector
, i
, dev
->toread
,
237 dev
->towrite
, dev
->written
,
238 test_bit(R5_LOCKED
, &dev
->flags
));
242 raid6_build_block(sh
, i
);
244 insert_hash(conf
, sh
);
247 static struct stripe_head
*__find_stripe(raid6_conf_t
*conf
, sector_t sector
)
249 struct stripe_head
*sh
;
252 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector
);
253 for (sh
= stripe_hash(conf
, sector
); sh
; sh
= sh
->hash_next
)
254 if (sh
->sector
== sector
)
256 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector
);
260 static void unplug_slaves(mddev_t
*mddev
);
262 static struct stripe_head
*get_active_stripe(raid6_conf_t
*conf
, sector_t sector
,
263 int pd_idx
, int noblock
)
265 struct stripe_head
*sh
;
267 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector
);
269 spin_lock_irq(&conf
->device_lock
);
272 wait_event_lock_irq(conf
->wait_for_stripe
,
274 conf
->device_lock
, /* nothing */);
275 sh
= __find_stripe(conf
, sector
);
277 if (!conf
->inactive_blocked
)
278 sh
= get_free_stripe(conf
);
279 if (noblock
&& sh
== NULL
)
282 conf
->inactive_blocked
= 1;
283 wait_event_lock_irq(conf
->wait_for_stripe
,
284 !list_empty(&conf
->inactive_list
) &&
285 (atomic_read(&conf
->active_stripes
) < (NR_STRIPES
*3/4)
286 || !conf
->inactive_blocked
),
288 unplug_slaves(conf
->mddev
);
290 conf
->inactive_blocked
= 0;
292 init_stripe(sh
, sector
, pd_idx
);
294 if (atomic_read(&sh
->count
)) {
295 if (!list_empty(&sh
->lru
))
298 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
299 atomic_inc(&conf
->active_stripes
);
300 if (list_empty(&sh
->lru
))
302 list_del_init(&sh
->lru
);
305 } while (sh
== NULL
);
308 atomic_inc(&sh
->count
);
310 spin_unlock_irq(&conf
->device_lock
);
314 static int grow_stripes(raid6_conf_t
*conf
, int num
)
316 struct stripe_head
*sh
;
318 int devs
= conf
->raid_disks
;
320 sprintf(conf
->cache_name
, "raid6/%s", mdname(conf
->mddev
));
322 sc
= kmem_cache_create(conf
->cache_name
,
323 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
327 conf
->slab_cache
= sc
;
329 sh
= kmem_cache_alloc(sc
, GFP_KERNEL
);
332 memset(sh
, 0, sizeof(*sh
) + (devs
-1)*sizeof(struct r5dev
));
333 sh
->raid_conf
= conf
;
334 spin_lock_init(&sh
->lock
);
336 if (grow_buffers(sh
, conf
->raid_disks
)) {
337 shrink_buffers(sh
, conf
->raid_disks
);
338 kmem_cache_free(sc
, sh
);
341 /* we just created an active stripe so... */
342 atomic_set(&sh
->count
, 1);
343 atomic_inc(&conf
->active_stripes
);
344 INIT_LIST_HEAD(&sh
->lru
);
350 static void shrink_stripes(raid6_conf_t
*conf
)
352 struct stripe_head
*sh
;
355 spin_lock_irq(&conf
->device_lock
);
356 sh
= get_free_stripe(conf
);
357 spin_unlock_irq(&conf
->device_lock
);
360 if (atomic_read(&sh
->count
))
362 shrink_buffers(sh
, conf
->raid_disks
);
363 kmem_cache_free(conf
->slab_cache
, sh
);
364 atomic_dec(&conf
->active_stripes
);
366 kmem_cache_destroy(conf
->slab_cache
);
367 conf
->slab_cache
= NULL
;
370 static int raid6_end_read_request(struct bio
* bi
, unsigned int bytes_done
,
373 struct stripe_head
*sh
= bi
->bi_private
;
374 raid6_conf_t
*conf
= sh
->raid_conf
;
375 int disks
= conf
->raid_disks
, i
;
376 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
381 for (i
=0 ; i
<disks
; i
++)
382 if (bi
== &sh
->dev
[i
].req
)
385 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
386 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
397 spin_lock_irqsave(&conf
->device_lock
, flags
);
398 /* we can return a buffer if we bypassed the cache or
399 * if the top buffer is not in highmem. If there are
400 * multiple buffers, leave the extra work to
403 buffer
= sh
->bh_read
[i
];
405 (!PageHighMem(buffer
->b_page
)
406 || buffer
->b_page
== bh
->b_page
)
408 sh
->bh_read
[i
] = buffer
->b_reqnext
;
409 buffer
->b_reqnext
= NULL
;
412 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
413 if (sh
->bh_page
[i
]==bh
->b_page
)
414 set_buffer_uptodate(bh
);
416 if (buffer
->b_page
!= bh
->b_page
)
417 memcpy(buffer
->b_data
, bh
->b_data
, bh
->b_size
);
418 buffer
->b_end_io(buffer
, 1);
421 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
423 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
424 printk(KERN_INFO
"raid6: read error corrected!!\n");
425 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
426 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
428 if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
))
429 atomic_set(&conf
->disks
[i
].rdev
->read_errors
, 0);
432 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
433 atomic_inc(&conf
->disks
[i
].rdev
->read_errors
);
434 if (conf
->mddev
->degraded
)
435 printk(KERN_WARNING
"raid6: read error not correctable.\n");
436 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
438 printk(KERN_WARNING
"raid6: read error NOT corrected!!\n");
439 else if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
)
440 > conf
->max_nr_stripes
)
442 "raid6: Too many read errors, failing device.\n");
446 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
448 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
449 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
450 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
453 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
455 /* must restore b_page before unlocking buffer... */
456 if (sh
->bh_page
[i
] != bh
->b_page
) {
457 bh
->b_page
= sh
->bh_page
[i
];
458 bh
->b_data
= page_address(bh
->b_page
);
459 clear_buffer_uptodate(bh
);
462 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
463 set_bit(STRIPE_HANDLE
, &sh
->state
);
468 static int raid6_end_write_request (struct bio
*bi
, unsigned int bytes_done
,
471 struct stripe_head
*sh
= bi
->bi_private
;
472 raid6_conf_t
*conf
= sh
->raid_conf
;
473 int disks
= conf
->raid_disks
, i
;
475 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
480 for (i
=0 ; i
<disks
; i
++)
481 if (bi
== &sh
->dev
[i
].req
)
484 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
485 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
492 spin_lock_irqsave(&conf
->device_lock
, flags
);
494 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
496 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
498 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
499 set_bit(STRIPE_HANDLE
, &sh
->state
);
500 __release_stripe(conf
, sh
);
501 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
506 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
);
508 static void raid6_build_block (struct stripe_head
*sh
, int i
)
510 struct r5dev
*dev
= &sh
->dev
[i
];
511 int pd_idx
= sh
->pd_idx
;
512 int qd_idx
= raid6_next_disk(pd_idx
, sh
->raid_conf
->raid_disks
);
515 dev
->req
.bi_io_vec
= &dev
->vec
;
517 dev
->req
.bi_max_vecs
++;
518 dev
->vec
.bv_page
= dev
->page
;
519 dev
->vec
.bv_len
= STRIPE_SIZE
;
520 dev
->vec
.bv_offset
= 0;
522 dev
->req
.bi_sector
= sh
->sector
;
523 dev
->req
.bi_private
= sh
;
526 if (i
!= pd_idx
&& i
!= qd_idx
)
527 dev
->sector
= compute_blocknr(sh
, i
);
530 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
532 char b
[BDEVNAME_SIZE
];
533 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
534 PRINTK("raid6: error called\n");
536 if (!test_bit(Faulty
, &rdev
->flags
)) {
538 if (test_bit(In_sync
, &rdev
->flags
)) {
539 conf
->working_disks
--;
541 conf
->failed_disks
++;
542 clear_bit(In_sync
, &rdev
->flags
);
544 * if recovery was running, make sure it aborts.
546 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
548 set_bit(Faulty
, &rdev
->flags
);
550 "raid6: Disk failure on %s, disabling device."
551 " Operation continuing on %d devices\n",
552 bdevname(rdev
->bdev
,b
), conf
->working_disks
);
557 * Input: a 'big' sector number,
558 * Output: index of the data and parity disk, and the sector # in them.
560 static sector_t
raid6_compute_sector(sector_t r_sector
, unsigned int raid_disks
,
561 unsigned int data_disks
, unsigned int * dd_idx
,
562 unsigned int * pd_idx
, raid6_conf_t
*conf
)
565 unsigned long chunk_number
;
566 unsigned int chunk_offset
;
568 int sectors_per_chunk
= conf
->chunk_size
>> 9;
570 /* First compute the information on this sector */
573 * Compute the chunk number and the sector offset inside the chunk
575 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
576 chunk_number
= r_sector
;
577 if ( r_sector
!= chunk_number
) {
578 printk(KERN_CRIT
"raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
579 (unsigned long long)r_sector
, (unsigned long)chunk_number
);
584 * Compute the stripe number
586 stripe
= chunk_number
/ data_disks
;
589 * Compute the data disk and parity disk indexes inside the stripe
591 *dd_idx
= chunk_number
% data_disks
;
594 * Select the parity disk based on the user selected algorithm.
598 switch (conf
->algorithm
) {
599 case ALGORITHM_LEFT_ASYMMETRIC
:
600 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
601 if (*pd_idx
== raid_disks
-1)
602 (*dd_idx
)++; /* Q D D D P */
603 else if (*dd_idx
>= *pd_idx
)
604 (*dd_idx
) += 2; /* D D P Q D */
606 case ALGORITHM_RIGHT_ASYMMETRIC
:
607 *pd_idx
= stripe
% raid_disks
;
608 if (*pd_idx
== raid_disks
-1)
609 (*dd_idx
)++; /* Q D D D P */
610 else if (*dd_idx
>= *pd_idx
)
611 (*dd_idx
) += 2; /* D D P Q D */
613 case ALGORITHM_LEFT_SYMMETRIC
:
614 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
615 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
617 case ALGORITHM_RIGHT_SYMMETRIC
:
618 *pd_idx
= stripe
% raid_disks
;
619 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
622 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
626 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
627 chunk_number
, *pd_idx
, *dd_idx
);
630 * Finally, compute the new sector number
632 new_sector
= (sector_t
) stripe
* sectors_per_chunk
+ chunk_offset
;
637 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
)
639 raid6_conf_t
*conf
= sh
->raid_conf
;
640 int raid_disks
= conf
->raid_disks
, data_disks
= raid_disks
- 2;
641 sector_t new_sector
= sh
->sector
, check
;
642 int sectors_per_chunk
= conf
->chunk_size
>> 9;
645 int chunk_number
, dummy1
, dummy2
, dd_idx
= i
;
649 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
651 if ( new_sector
!= stripe
) {
652 printk(KERN_CRIT
"raid6: ERROR: new_sector = %llu, stripe = %lu\n",
653 (unsigned long long)new_sector
, (unsigned long)stripe
);
657 switch (conf
->algorithm
) {
658 case ALGORITHM_LEFT_ASYMMETRIC
:
659 case ALGORITHM_RIGHT_ASYMMETRIC
:
660 if (sh
->pd_idx
== raid_disks
-1)
662 else if (i
> sh
->pd_idx
)
663 i
-= 2; /* D D P Q D */
665 case ALGORITHM_LEFT_SYMMETRIC
:
666 case ALGORITHM_RIGHT_SYMMETRIC
:
667 if (sh
->pd_idx
== raid_disks
-1)
673 i
-= (sh
->pd_idx
+ 2);
677 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
681 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh
->pd_idx
, i0
, i
);
683 chunk_number
= stripe
* data_disks
+ i
;
684 r_sector
= (sector_t
)chunk_number
* sectors_per_chunk
+ chunk_offset
;
686 check
= raid6_compute_sector (r_sector
, raid_disks
, data_disks
, &dummy1
, &dummy2
, conf
);
687 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| dummy2
!= sh
->pd_idx
) {
688 printk(KERN_CRIT
"raid6: compute_blocknr: map not correct\n");
697 * Copy data between a page in the stripe cache, and one or more bion
698 * The page could align with the middle of the bio, or there could be
699 * several bion, each with several bio_vecs, which cover part of the page
700 * Multiple bion are linked together on bi_next. There may be extras
701 * at the end of this list. We ignore them.
703 static void copy_data(int frombio
, struct bio
*bio
,
707 char *pa
= page_address(page
);
712 if (bio
->bi_sector
>= sector
)
713 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
715 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
716 bio_for_each_segment(bvl
, bio
, i
) {
717 int len
= bio_iovec_idx(bio
,i
)->bv_len
;
721 if (page_offset
< 0) {
722 b_offset
= -page_offset
;
723 page_offset
+= b_offset
;
727 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
728 clen
= STRIPE_SIZE
- page_offset
;
732 char *ba
= __bio_kmap_atomic(bio
, i
, KM_USER0
);
734 memcpy(pa
+page_offset
, ba
+b_offset
, clen
);
736 memcpy(ba
+b_offset
, pa
+page_offset
, clen
);
737 __bio_kunmap_atomic(ba
, KM_USER0
);
739 if (clen
< len
) /* hit end of page */
745 #define check_xor() do { \
746 if (count == MAX_XOR_BLOCKS) { \
747 xor_block(count, STRIPE_SIZE, ptr); \
752 /* Compute P and Q syndromes */
753 static void compute_parity(struct stripe_head
*sh
, int method
)
755 raid6_conf_t
*conf
= sh
->raid_conf
;
756 int i
, pd_idx
= sh
->pd_idx
, qd_idx
, d0_idx
, disks
= conf
->raid_disks
, count
;
758 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
761 qd_idx
= raid6_next_disk(pd_idx
, disks
);
762 d0_idx
= raid6_next_disk(qd_idx
, disks
);
764 PRINTK("compute_parity, stripe %llu, method %d\n",
765 (unsigned long long)sh
->sector
, method
);
768 case READ_MODIFY_WRITE
:
769 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
770 case RECONSTRUCT_WRITE
:
771 for (i
= disks
; i
-- ;)
772 if ( i
!= pd_idx
&& i
!= qd_idx
&& sh
->dev
[i
].towrite
) {
773 chosen
= sh
->dev
[i
].towrite
;
774 sh
->dev
[i
].towrite
= NULL
;
776 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
777 wake_up(&conf
->wait_for_overlap
);
779 if (sh
->dev
[i
].written
) BUG();
780 sh
->dev
[i
].written
= chosen
;
784 BUG(); /* Not implemented yet */
787 for (i
= disks
; i
--;)
788 if (sh
->dev
[i
].written
) {
789 sector_t sector
= sh
->dev
[i
].sector
;
790 struct bio
*wbi
= sh
->dev
[i
].written
;
791 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
792 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
793 wbi
= r5_next_bio(wbi
, sector
);
796 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
797 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
801 // case RECONSTRUCT_WRITE:
802 // case CHECK_PARITY:
803 // case UPDATE_PARITY:
804 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
805 /* FIX: Is this ordering of drives even remotely optimal? */
809 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
810 if (count
<= disks
-2 && !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
811 printk("block %d/%d not uptodate on parity calc\n", i
,count
);
812 i
= raid6_next_disk(i
, disks
);
813 } while ( i
!= d0_idx
);
817 raid6_call
.gen_syndrome(disks
, STRIPE_SIZE
, ptrs
);
820 case RECONSTRUCT_WRITE
:
821 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
822 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
823 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
824 set_bit(R5_LOCKED
, &sh
->dev
[qd_idx
].flags
);
827 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
828 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
833 /* Compute one missing block */
834 static void compute_block_1(struct stripe_head
*sh
, int dd_idx
, int nozero
)
836 raid6_conf_t
*conf
= sh
->raid_conf
;
837 int i
, count
, disks
= conf
->raid_disks
;
838 void *ptr
[MAX_XOR_BLOCKS
], *p
;
839 int pd_idx
= sh
->pd_idx
;
840 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
842 PRINTK("compute_block_1, stripe %llu, idx %d\n",
843 (unsigned long long)sh
->sector
, dd_idx
);
845 if ( dd_idx
== qd_idx
) {
846 /* We're actually computing the Q drive */
847 compute_parity(sh
, UPDATE_PARITY
);
849 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
850 if (!nozero
) memset(ptr
[0], 0, STRIPE_SIZE
);
852 for (i
= disks
; i
--; ) {
853 if (i
== dd_idx
|| i
== qd_idx
)
855 p
= page_address(sh
->dev
[i
].page
);
856 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
859 printk("compute_block() %d, stripe %llu, %d"
860 " not present\n", dd_idx
,
861 (unsigned long long)sh
->sector
, i
);
866 xor_block(count
, STRIPE_SIZE
, ptr
);
867 if (!nozero
) set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
868 else clear_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
872 /* Compute two missing blocks */
873 static void compute_block_2(struct stripe_head
*sh
, int dd_idx1
, int dd_idx2
)
875 raid6_conf_t
*conf
= sh
->raid_conf
;
876 int i
, count
, disks
= conf
->raid_disks
;
877 int pd_idx
= sh
->pd_idx
;
878 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
879 int d0_idx
= raid6_next_disk(qd_idx
, disks
);
882 /* faila and failb are disk numbers relative to d0_idx */
883 /* pd_idx become disks-2 and qd_idx become disks-1 */
884 faila
= (dd_idx1
< d0_idx
) ? dd_idx1
+(disks
-d0_idx
) : dd_idx1
-d0_idx
;
885 failb
= (dd_idx2
< d0_idx
) ? dd_idx2
+(disks
-d0_idx
) : dd_idx2
-d0_idx
;
887 BUG_ON(faila
== failb
);
888 if ( failb
< faila
) { int tmp
= faila
; faila
= failb
; failb
= tmp
; }
890 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
891 (unsigned long long)sh
->sector
, dd_idx1
, dd_idx2
, faila
, failb
);
893 if ( failb
== disks
-1 ) {
894 /* Q disk is one of the missing disks */
895 if ( faila
== disks
-2 ) {
896 /* Missing P+Q, just recompute */
897 compute_parity(sh
, UPDATE_PARITY
);
900 /* We're missing D+Q; recompute D from P */
901 compute_block_1(sh
, (dd_idx1
== qd_idx
) ? dd_idx2
: dd_idx1
, 0);
902 compute_parity(sh
, UPDATE_PARITY
); /* Is this necessary? */
907 /* We're missing D+P or D+D; build pointer table */
909 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
915 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
916 i
= raid6_next_disk(i
, disks
);
917 if (i
!= dd_idx1
&& i
!= dd_idx2
&&
918 !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
919 printk("compute_2 with missing block %d/%d\n", count
, i
);
920 } while ( i
!= d0_idx
);
922 if ( failb
== disks
-2 ) {
923 /* We're missing D+P. */
924 raid6_datap_recov(disks
, STRIPE_SIZE
, faila
, ptrs
);
926 /* We're missing D+D. */
927 raid6_2data_recov(disks
, STRIPE_SIZE
, faila
, failb
, ptrs
);
930 /* Both the above update both missing blocks */
931 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx1
].flags
);
932 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx2
].flags
);
938 * Each stripe/dev can have one or more bion attached.
939 * toread/towrite point to the first in a chain.
940 * The bi_next chain must be in order.
942 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
945 raid6_conf_t
*conf
= sh
->raid_conf
;
948 PRINTK("adding bh b#%llu to stripe s#%llu\n",
949 (unsigned long long)bi
->bi_sector
,
950 (unsigned long long)sh
->sector
);
953 spin_lock(&sh
->lock
);
954 spin_lock_irq(&conf
->device_lock
);
956 bip
= &sh
->dev
[dd_idx
].towrite
;
957 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
960 bip
= &sh
->dev
[dd_idx
].toread
;
961 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
962 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
964 bip
= &(*bip
)->bi_next
;
966 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
969 if (*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
)
974 bi
->bi_phys_segments
++;
975 spin_unlock_irq(&conf
->device_lock
);
976 spin_unlock(&sh
->lock
);
978 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
979 (unsigned long long)bi
->bi_sector
,
980 (unsigned long long)sh
->sector
, dd_idx
);
982 if (conf
->mddev
->bitmap
&& firstwrite
) {
983 sh
->bm_seq
= conf
->seq_write
;
984 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
986 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
990 /* check if page is covered */
991 sector_t sector
= sh
->dev
[dd_idx
].sector
;
992 for (bi
=sh
->dev
[dd_idx
].towrite
;
993 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
994 bi
&& bi
->bi_sector
<= sector
;
995 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
996 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
997 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
999 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
1000 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
1005 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
1006 spin_unlock_irq(&conf
->device_lock
);
1007 spin_unlock(&sh
->lock
);
1012 static int page_is_zero(struct page
*p
)
1014 char *a
= page_address(p
);
1015 return ((*(u32
*)a
) == 0 &&
1016 memcmp(a
, a
+4, STRIPE_SIZE
-4)==0);
1019 * handle_stripe - do things to a stripe.
1021 * We lock the stripe and then examine the state of various bits
1022 * to see what needs to be done.
1024 * return some read request which now have data
1025 * return some write requests which are safely on disc
1026 * schedule a read on some buffers
1027 * schedule a write of some buffers
1028 * return confirmation of parity correctness
1030 * Parity calculations are done inside the stripe lock
1031 * buffers are taken off read_list or write_list, and bh_cache buffers
1032 * get BH_Lock set before the stripe lock is released.
1036 static void handle_stripe(struct stripe_head
*sh
, struct page
*tmp_page
)
1038 raid6_conf_t
*conf
= sh
->raid_conf
;
1039 int disks
= conf
->raid_disks
;
1040 struct bio
*return_bi
= NULL
;
1044 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
1045 int non_overwrite
= 0;
1046 int failed_num
[2] = {0, 0};
1047 struct r5dev
*dev
, *pdev
, *qdev
;
1048 int pd_idx
= sh
->pd_idx
;
1049 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1050 int p_failed
, q_failed
;
1052 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1053 (unsigned long long)sh
->sector
, sh
->state
, atomic_read(&sh
->count
),
1056 spin_lock(&sh
->lock
);
1057 clear_bit(STRIPE_HANDLE
, &sh
->state
);
1058 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1060 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
1061 /* Now to look around and see what can be done */
1063 for (i
=disks
; i
--; ) {
1066 clear_bit(R5_Insync
, &dev
->flags
);
1067 clear_bit(R5_Syncio
, &dev
->flags
);
1069 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1070 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
1071 /* maybe we can reply to a read */
1072 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
1073 struct bio
*rbi
, *rbi2
;
1074 PRINTK("Return read for disc %d\n", i
);
1075 spin_lock_irq(&conf
->device_lock
);
1078 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1079 wake_up(&conf
->wait_for_overlap
);
1080 spin_unlock_irq(&conf
->device_lock
);
1081 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1082 copy_data(0, rbi
, dev
->page
, dev
->sector
);
1083 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1084 spin_lock_irq(&conf
->device_lock
);
1085 if (--rbi
->bi_phys_segments
== 0) {
1086 rbi
->bi_next
= return_bi
;
1089 spin_unlock_irq(&conf
->device_lock
);
1094 /* now count some things */
1095 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
1096 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
1099 if (dev
->toread
) to_read
++;
1102 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1105 if (dev
->written
) written
++;
1106 rdev
= conf
->disks
[i
].rdev
; /* FIXME, should I be looking rdev */
1107 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1108 /* The ReadError flag will just be confusing now */
1109 clear_bit(R5_ReadError
, &dev
->flags
);
1110 clear_bit(R5_ReWrite
, &dev
->flags
);
1112 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1113 || test_bit(R5_ReadError
, &dev
->flags
)) {
1115 failed_num
[failed
] = i
;
1118 set_bit(R5_Insync
, &dev
->flags
);
1120 PRINTK("locked=%d uptodate=%d to_read=%d"
1121 " to_write=%d failed=%d failed_num=%d,%d\n",
1122 locked
, uptodate
, to_read
, to_write
, failed
,
1123 failed_num
[0], failed_num
[1]);
1124 /* check if the array has lost >2 devices and, if so, some requests might
1127 if (failed
> 2 && to_read
+to_write
+written
) {
1128 for (i
=disks
; i
--; ) {
1131 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1132 mdk_rdev_t
*rdev
= conf
->disks
[i
].rdev
;
1133 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1134 /* multiple read failures in one stripe */
1135 md_error(conf
->mddev
, rdev
);
1138 spin_lock_irq(&conf
->device_lock
);
1139 /* fail all writes first */
1140 bi
= sh
->dev
[i
].towrite
;
1141 sh
->dev
[i
].towrite
= NULL
;
1142 if (bi
) { to_write
--; bitmap_end
= 1; }
1144 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1145 wake_up(&conf
->wait_for_overlap
);
1147 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1148 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1149 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1150 if (--bi
->bi_phys_segments
== 0) {
1151 md_write_end(conf
->mddev
);
1152 bi
->bi_next
= return_bi
;
1157 /* and fail all 'written' */
1158 bi
= sh
->dev
[i
].written
;
1159 sh
->dev
[i
].written
= NULL
;
1160 if (bi
) bitmap_end
= 1;
1161 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
1162 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1163 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1164 if (--bi
->bi_phys_segments
== 0) {
1165 md_write_end(conf
->mddev
);
1166 bi
->bi_next
= return_bi
;
1172 /* fail any reads if this device is non-operational */
1173 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
1174 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1175 bi
= sh
->dev
[i
].toread
;
1176 sh
->dev
[i
].toread
= NULL
;
1177 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1178 wake_up(&conf
->wait_for_overlap
);
1180 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1181 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1182 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1183 if (--bi
->bi_phys_segments
== 0) {
1184 bi
->bi_next
= return_bi
;
1190 spin_unlock_irq(&conf
->device_lock
);
1192 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1193 STRIPE_SECTORS
, 0, 0);
1196 if (failed
> 2 && syncing
) {
1197 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
1198 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1203 * might be able to return some write requests if the parity blocks
1204 * are safe, or on a failed drive
1206 pdev
= &sh
->dev
[pd_idx
];
1207 p_failed
= (failed
>= 1 && failed_num
[0] == pd_idx
)
1208 || (failed
>= 2 && failed_num
[1] == pd_idx
);
1209 qdev
= &sh
->dev
[qd_idx
];
1210 q_failed
= (failed
>= 1 && failed_num
[0] == qd_idx
)
1211 || (failed
>= 2 && failed_num
[1] == qd_idx
);
1214 ( p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
1215 && !test_bit(R5_LOCKED
, &pdev
->flags
)
1216 && test_bit(R5_UPTODATE
, &pdev
->flags
))) ) &&
1217 ( q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
1218 && !test_bit(R5_LOCKED
, &qdev
->flags
)
1219 && test_bit(R5_UPTODATE
, &qdev
->flags
))) ) ) {
1220 /* any written block on an uptodate or failed drive can be
1221 * returned. Note that if we 'wrote' to a failed drive,
1222 * it will be UPTODATE, but never LOCKED, so we don't need
1223 * to test 'failed' directly.
1225 for (i
=disks
; i
--; )
1226 if (sh
->dev
[i
].written
) {
1228 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
1229 test_bit(R5_UPTODATE
, &dev
->flags
) ) {
1230 /* We can return any write requests */
1232 struct bio
*wbi
, *wbi2
;
1233 PRINTK("Return write for stripe %llu disc %d\n",
1234 (unsigned long long)sh
->sector
, i
);
1235 spin_lock_irq(&conf
->device_lock
);
1237 dev
->written
= NULL
;
1238 while (wbi
&& wbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1239 wbi2
= r5_next_bio(wbi
, dev
->sector
);
1240 if (--wbi
->bi_phys_segments
== 0) {
1241 md_write_end(conf
->mddev
);
1242 wbi
->bi_next
= return_bi
;
1247 if (dev
->towrite
== NULL
)
1249 spin_unlock_irq(&conf
->device_lock
);
1251 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1253 !test_bit(STRIPE_DEGRADED
, &sh
->state
), 0);
1258 /* Now we might consider reading some blocks, either to check/generate
1259 * parity, or to satisfy requests
1260 * or to load a block that is being partially written.
1262 if (to_read
|| non_overwrite
|| (to_write
&& failed
) || (syncing
&& (uptodate
< disks
))) {
1263 for (i
=disks
; i
--;) {
1265 if (!test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1267 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
1269 (failed
>= 1 && (sh
->dev
[failed_num
[0]].toread
|| to_write
)) ||
1270 (failed
>= 2 && (sh
->dev
[failed_num
[1]].toread
|| to_write
))
1273 /* we would like to get this block, possibly
1274 * by computing it, but we might not be able to
1276 if (uptodate
== disks
-1) {
1277 PRINTK("Computing stripe %llu block %d\n",
1278 (unsigned long long)sh
->sector
, i
);
1279 compute_block_1(sh
, i
, 0);
1281 } else if ( uptodate
== disks
-2 && failed
>= 2 ) {
1282 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1284 for (other
=disks
; other
--;) {
1287 if ( !test_bit(R5_UPTODATE
, &sh
->dev
[other
].flags
) )
1291 PRINTK("Computing stripe %llu blocks %d,%d\n",
1292 (unsigned long long)sh
->sector
, i
, other
);
1293 compute_block_2(sh
, i
, other
);
1295 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
1296 set_bit(R5_LOCKED
, &dev
->flags
);
1297 set_bit(R5_Wantread
, &dev
->flags
);
1299 /* if I am just reading this block and we don't have
1300 a failed drive, or any pending writes then sidestep the cache */
1301 if (sh
->bh_read
[i
] && !sh
->bh_read
[i
]->b_reqnext
&&
1302 ! syncing
&& !failed
&& !to_write
) {
1303 sh
->bh_cache
[i
]->b_page
= sh
->bh_read
[i
]->b_page
;
1304 sh
->bh_cache
[i
]->b_data
= sh
->bh_read
[i
]->b_data
;
1308 PRINTK("Reading block %d (sync=%d)\n",
1311 md_sync_acct(conf
->disks
[i
].rdev
->bdev
,
1316 set_bit(STRIPE_HANDLE
, &sh
->state
);
1319 /* now to consider writing and what else, if anything should be read */
1321 int rcw
=0, must_compute
=0;
1322 for (i
=disks
; i
--;) {
1324 /* Would I have to read this buffer for reconstruct_write */
1325 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
1326 && i
!= pd_idx
&& i
!= qd_idx
1327 && (!test_bit(R5_LOCKED
, &dev
->flags
)
1329 || sh
->bh_page
[i
] != bh
->b_page
1332 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1333 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
1335 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i
, dev
->flags
);
1340 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1341 (unsigned long long)sh
->sector
, rcw
, must_compute
);
1342 set_bit(STRIPE_HANDLE
, &sh
->state
);
1345 /* want reconstruct write, but need to get some data */
1346 for (i
=disks
; i
--;) {
1348 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
1349 && !(failed
== 0 && (i
== pd_idx
|| i
== qd_idx
))
1350 && !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1351 test_bit(R5_Insync
, &dev
->flags
)) {
1352 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1354 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1355 (unsigned long long)sh
->sector
, i
);
1356 set_bit(R5_LOCKED
, &dev
->flags
);
1357 set_bit(R5_Wantread
, &dev
->flags
);
1360 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1361 (unsigned long long)sh
->sector
, i
);
1362 set_bit(STRIPE_DELAYED
, &sh
->state
);
1363 set_bit(STRIPE_HANDLE
, &sh
->state
);
1367 /* now if nothing is locked, and if we have enough data, we can start a write request */
1368 if (locked
== 0 && rcw
== 0 &&
1369 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
1370 if ( must_compute
> 0 ) {
1371 /* We have failed blocks and need to compute them */
1374 case 1: compute_block_1(sh
, failed_num
[0], 0); break;
1375 case 2: compute_block_2(sh
, failed_num
[0], failed_num
[1]); break;
1376 default: BUG(); /* This request should have been failed? */
1380 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh
->sector
);
1381 compute_parity(sh
, RECONSTRUCT_WRITE
);
1382 /* now every locked buffer is ready to be written */
1384 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
1385 PRINTK("Writing stripe %llu block %d\n",
1386 (unsigned long long)sh
->sector
, i
);
1388 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1390 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
1391 set_bit(STRIPE_INSYNC
, &sh
->state
);
1393 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
1394 atomic_dec(&conf
->preread_active_stripes
);
1395 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
1396 md_wakeup_thread(conf
->mddev
->thread
);
1401 /* maybe we need to check and possibly fix the parity for this stripe
1402 * Any reads will already have been scheduled, so we just see if enough data
1405 if (syncing
&& locked
== 0 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1406 int update_p
= 0, update_q
= 0;
1409 set_bit(STRIPE_HANDLE
, &sh
->state
);
1412 BUG_ON(uptodate
< disks
);
1413 /* Want to check and possibly repair P and Q.
1414 * However there could be one 'failed' device, in which
1415 * case we can only check one of them, possibly using the
1416 * other to generate missing data
1419 /* If !tmp_page, we cannot do the calculations,
1420 * but as we have set STRIPE_HANDLE, we will soon be called
1421 * by stripe_handle with a tmp_page - just wait until then.
1424 if (failed
== q_failed
) {
1425 /* The only possible failed device holds 'Q', so it makes
1426 * sense to check P (If anything else were failed, we would
1427 * have used P to recreate it).
1429 compute_block_1(sh
, pd_idx
, 1);
1430 if (!page_is_zero(sh
->dev
[pd_idx
].page
)) {
1431 compute_block_1(sh
,pd_idx
,0);
1435 if (!q_failed
&& failed
< 2) {
1436 /* q is not failed, and we didn't use it to generate
1437 * anything, so it makes sense to check it
1439 memcpy(page_address(tmp_page
),
1440 page_address(sh
->dev
[qd_idx
].page
),
1442 compute_parity(sh
, UPDATE_PARITY
);
1443 if (memcmp(page_address(tmp_page
),
1444 page_address(sh
->dev
[qd_idx
].page
),
1446 clear_bit(STRIPE_INSYNC
, &sh
->state
);
1450 if (update_p
|| update_q
) {
1451 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
1452 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
1453 /* don't try to repair!! */
1454 update_p
= update_q
= 0;
1457 /* now write out any block on a failed drive,
1458 * or P or Q if they need it
1462 dev
= &sh
->dev
[failed_num
[1]];
1464 set_bit(R5_LOCKED
, &dev
->flags
);
1465 set_bit(R5_Wantwrite
, &dev
->flags
);
1466 set_bit(R5_Syncio
, &dev
->flags
);
1469 dev
= &sh
->dev
[failed_num
[0]];
1471 set_bit(R5_LOCKED
, &dev
->flags
);
1472 set_bit(R5_Wantwrite
, &dev
->flags
);
1473 set_bit(R5_Syncio
, &dev
->flags
);
1477 dev
= &sh
->dev
[pd_idx
];
1479 set_bit(R5_LOCKED
, &dev
->flags
);
1480 set_bit(R5_Wantwrite
, &dev
->flags
);
1481 set_bit(R5_Syncio
, &dev
->flags
);
1484 dev
= &sh
->dev
[qd_idx
];
1486 set_bit(R5_LOCKED
, &dev
->flags
);
1487 set_bit(R5_Wantwrite
, &dev
->flags
);
1488 set_bit(R5_Syncio
, &dev
->flags
);
1490 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
1492 set_bit(STRIPE_INSYNC
, &sh
->state
);
1496 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1497 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
1498 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1501 /* If the failed drives are just a ReadError, then we might need
1502 * to progress the repair/check process
1504 if (failed
<= 2 && ! conf
->mddev
->ro
)
1505 for (i
=0; i
<failed
;i
++) {
1506 dev
= &sh
->dev
[failed_num
[i
]];
1507 if (test_bit(R5_ReadError
, &dev
->flags
)
1508 && !test_bit(R5_LOCKED
, &dev
->flags
)
1509 && test_bit(R5_UPTODATE
, &dev
->flags
)
1511 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
1512 set_bit(R5_Wantwrite
, &dev
->flags
);
1513 set_bit(R5_ReWrite
, &dev
->flags
);
1514 set_bit(R5_LOCKED
, &dev
->flags
);
1516 /* let's read it back */
1517 set_bit(R5_Wantread
, &dev
->flags
);
1518 set_bit(R5_LOCKED
, &dev
->flags
);
1522 spin_unlock(&sh
->lock
);
1524 while ((bi
=return_bi
)) {
1525 int bytes
= bi
->bi_size
;
1527 return_bi
= bi
->bi_next
;
1530 bi
->bi_end_io(bi
, bytes
, 0);
1532 for (i
=disks
; i
-- ;) {
1536 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
1538 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
1543 bi
= &sh
->dev
[i
].req
;
1547 bi
->bi_end_io
= raid6_end_write_request
;
1549 bi
->bi_end_io
= raid6_end_read_request
;
1552 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1553 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
1556 atomic_inc(&rdev
->nr_pending
);
1560 if (test_bit(R5_Syncio
, &sh
->dev
[i
].flags
))
1561 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
1563 bi
->bi_bdev
= rdev
->bdev
;
1564 PRINTK("for %llu schedule op %ld on disc %d\n",
1565 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
1566 atomic_inc(&sh
->count
);
1567 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
1568 bi
->bi_flags
= 1 << BIO_UPTODATE
;
1570 bi
->bi_max_vecs
= 1;
1572 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
1573 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1574 bi
->bi_io_vec
[0].bv_offset
= 0;
1575 bi
->bi_size
= STRIPE_SIZE
;
1577 generic_make_request(bi
);
1580 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1581 PRINTK("skip op %ld on disc %d for sector %llu\n",
1582 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
1583 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1584 set_bit(STRIPE_HANDLE
, &sh
->state
);
1589 static inline void raid6_activate_delayed(raid6_conf_t
*conf
)
1591 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
1592 while (!list_empty(&conf
->delayed_list
)) {
1593 struct list_head
*l
= conf
->delayed_list
.next
;
1594 struct stripe_head
*sh
;
1595 sh
= list_entry(l
, struct stripe_head
, lru
);
1597 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1598 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1599 atomic_inc(&conf
->preread_active_stripes
);
1600 list_add_tail(&sh
->lru
, &conf
->handle_list
);
1605 static inline void activate_bit_delay(raid6_conf_t
*conf
)
1607 /* device_lock is held */
1608 struct list_head head
;
1609 list_add(&head
, &conf
->bitmap_list
);
1610 list_del_init(&conf
->bitmap_list
);
1611 while (!list_empty(&head
)) {
1612 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
1613 list_del_init(&sh
->lru
);
1614 atomic_inc(&sh
->count
);
1615 __release_stripe(conf
, sh
);
1619 static void unplug_slaves(mddev_t
*mddev
)
1621 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1625 for (i
=0; i
<mddev
->raid_disks
; i
++) {
1626 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1627 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
1628 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
1630 atomic_inc(&rdev
->nr_pending
);
1633 if (r_queue
->unplug_fn
)
1634 r_queue
->unplug_fn(r_queue
);
1636 rdev_dec_pending(rdev
, mddev
);
1643 static void raid6_unplug_device(request_queue_t
*q
)
1645 mddev_t
*mddev
= q
->queuedata
;
1646 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1647 unsigned long flags
;
1649 spin_lock_irqsave(&conf
->device_lock
, flags
);
1651 if (blk_remove_plug(q
)) {
1653 raid6_activate_delayed(conf
);
1655 md_wakeup_thread(mddev
->thread
);
1657 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1659 unplug_slaves(mddev
);
1662 static int raid6_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
1663 sector_t
*error_sector
)
1665 mddev_t
*mddev
= q
->queuedata
;
1666 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1670 for (i
=0; i
<mddev
->raid_disks
&& ret
== 0; i
++) {
1671 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1672 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
1673 struct block_device
*bdev
= rdev
->bdev
;
1674 request_queue_t
*r_queue
= bdev_get_queue(bdev
);
1676 if (!r_queue
->issue_flush_fn
)
1679 atomic_inc(&rdev
->nr_pending
);
1681 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
1683 rdev_dec_pending(rdev
, mddev
);
1692 static inline void raid6_plug_device(raid6_conf_t
*conf
)
1694 spin_lock_irq(&conf
->device_lock
);
1695 blk_plug_device(conf
->mddev
->queue
);
1696 spin_unlock_irq(&conf
->device_lock
);
1699 static int make_request (request_queue_t
*q
, struct bio
* bi
)
1701 mddev_t
*mddev
= q
->queuedata
;
1702 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1703 const unsigned int raid_disks
= conf
->raid_disks
;
1704 const unsigned int data_disks
= raid_disks
- 2;
1705 unsigned int dd_idx
, pd_idx
;
1706 sector_t new_sector
;
1707 sector_t logical_sector
, last_sector
;
1708 struct stripe_head
*sh
;
1709 const int rw
= bio_data_dir(bi
);
1711 if (unlikely(bio_barrier(bi
))) {
1712 bio_endio(bi
, bi
->bi_size
, -EOPNOTSUPP
);
1716 md_write_start(mddev
, bi
);
1718 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
1719 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bi
));
1721 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
1722 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1725 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
1727 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
1730 new_sector
= raid6_compute_sector(logical_sector
,
1731 raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1733 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1734 (unsigned long long)new_sector
,
1735 (unsigned long long)logical_sector
);
1738 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
1739 sh
= get_active_stripe(conf
, new_sector
, pd_idx
, (bi
->bi_rw
&RWA_MASK
));
1741 if (!add_stripe_bio(sh
, bi
, dd_idx
, (bi
->bi_rw
&RW_MASK
))) {
1742 /* Add failed due to overlap. Flush everything
1745 raid6_unplug_device(mddev
->queue
);
1750 finish_wait(&conf
->wait_for_overlap
, &w
);
1751 raid6_plug_device(conf
);
1752 handle_stripe(sh
, NULL
);
1755 /* cannot get stripe for read-ahead, just give-up */
1756 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1757 finish_wait(&conf
->wait_for_overlap
, &w
);
1762 spin_lock_irq(&conf
->device_lock
);
1763 if (--bi
->bi_phys_segments
== 0) {
1764 int bytes
= bi
->bi_size
;
1767 md_write_end(mddev
);
1769 bi
->bi_end_io(bi
, bytes
, 0);
1771 spin_unlock_irq(&conf
->device_lock
);
1775 /* FIXME go_faster isn't used */
1776 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1778 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
1779 struct stripe_head
*sh
;
1780 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1782 unsigned long stripe
;
1785 sector_t first_sector
;
1786 int raid_disks
= conf
->raid_disks
;
1787 int data_disks
= raid_disks
- 2;
1788 sector_t max_sector
= mddev
->size
<< 1;
1790 int still_degraded
= 0;
1793 if (sector_nr
>= max_sector
) {
1794 /* just being told to finish up .. nothing much to do */
1795 unplug_slaves(mddev
);
1797 if (mddev
->curr_resync
< max_sector
) /* aborted */
1798 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1800 else /* completed sync */
1802 bitmap_close_sync(mddev
->bitmap
);
1806 /* if there are 2 or more failed drives and we are trying
1807 * to resync, then assert that we are finished, because there is
1808 * nothing we can do.
1810 if (mddev
->degraded
>= 2 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1811 sector_t rv
= (mddev
->size
<< 1) - sector_nr
;
1815 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
1816 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
1817 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
1818 /* we can skip this block, and probably more */
1819 sync_blocks
/= STRIPE_SECTORS
;
1821 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
1825 chunk_offset
= sector_div(x
, sectors_per_chunk
);
1827 BUG_ON(x
!= stripe
);
1829 first_sector
= raid6_compute_sector((sector_t
)stripe
*data_disks
*sectors_per_chunk
1830 + chunk_offset
, raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1831 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 1);
1833 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 0);
1834 /* make sure we don't swamp the stripe cache if someone else
1835 * is trying to get access
1837 schedule_timeout_uninterruptible(1);
1839 /* Need to check if array will still be degraded after recovery/resync
1840 * We don't need to check the 'failed' flag as when that gets set,
1843 for (i
=0; i
<mddev
->raid_disks
; i
++)
1844 if (conf
->disks
[i
].rdev
== NULL
)
1847 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
1849 spin_lock(&sh
->lock
);
1850 set_bit(STRIPE_SYNCING
, &sh
->state
);
1851 clear_bit(STRIPE_INSYNC
, &sh
->state
);
1852 spin_unlock(&sh
->lock
);
1854 handle_stripe(sh
, NULL
);
1857 return STRIPE_SECTORS
;
1861 * This is our raid6 kernel thread.
1863 * We scan the hash table for stripes which can be handled now.
1864 * During the scan, completed stripes are saved for us by the interrupt
1865 * handler, so that they will not have to wait for our next wakeup.
1867 static void raid6d (mddev_t
*mddev
)
1869 struct stripe_head
*sh
;
1870 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1873 PRINTK("+++ raid6d active\n");
1875 md_check_recovery(mddev
);
1878 spin_lock_irq(&conf
->device_lock
);
1880 struct list_head
*first
;
1882 if (conf
->seq_flush
- conf
->seq_write
> 0) {
1883 int seq
= conf
->seq_flush
;
1884 spin_unlock_irq(&conf
->device_lock
);
1885 bitmap_unplug(mddev
->bitmap
);
1886 spin_lock_irq(&conf
->device_lock
);
1887 conf
->seq_write
= seq
;
1888 activate_bit_delay(conf
);
1891 if (list_empty(&conf
->handle_list
) &&
1892 atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
&&
1893 !blk_queue_plugged(mddev
->queue
) &&
1894 !list_empty(&conf
->delayed_list
))
1895 raid6_activate_delayed(conf
);
1897 if (list_empty(&conf
->handle_list
))
1900 first
= conf
->handle_list
.next
;
1901 sh
= list_entry(first
, struct stripe_head
, lru
);
1903 list_del_init(first
);
1904 atomic_inc(&sh
->count
);
1905 if (atomic_read(&sh
->count
)!= 1)
1907 spin_unlock_irq(&conf
->device_lock
);
1910 handle_stripe(sh
, conf
->spare_page
);
1913 spin_lock_irq(&conf
->device_lock
);
1915 PRINTK("%d stripes handled\n", handled
);
1917 spin_unlock_irq(&conf
->device_lock
);
1919 unplug_slaves(mddev
);
1921 PRINTK("--- raid6d inactive\n");
1924 static int run(mddev_t
*mddev
)
1927 int raid_disk
, memory
;
1929 struct disk_info
*disk
;
1930 struct list_head
*tmp
;
1932 if (mddev
->level
!= 6) {
1933 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev
), mddev
->level
);
1937 mddev
->private = kmalloc (sizeof (raid6_conf_t
)
1938 + mddev
->raid_disks
* sizeof(struct disk_info
),
1940 if ((conf
= mddev
->private) == NULL
)
1942 memset (conf
, 0, sizeof (*conf
) + mddev
->raid_disks
* sizeof(struct disk_info
) );
1943 conf
->mddev
= mddev
;
1945 if ((conf
->stripe_hashtbl
= (struct stripe_head
**) __get_free_pages(GFP_ATOMIC
, HASH_PAGES_ORDER
)) == NULL
)
1947 memset(conf
->stripe_hashtbl
, 0, HASH_PAGES
* PAGE_SIZE
);
1949 conf
->spare_page
= alloc_page(GFP_KERNEL
);
1950 if (!conf
->spare_page
)
1953 spin_lock_init(&conf
->device_lock
);
1954 init_waitqueue_head(&conf
->wait_for_stripe
);
1955 init_waitqueue_head(&conf
->wait_for_overlap
);
1956 INIT_LIST_HEAD(&conf
->handle_list
);
1957 INIT_LIST_HEAD(&conf
->delayed_list
);
1958 INIT_LIST_HEAD(&conf
->bitmap_list
);
1959 INIT_LIST_HEAD(&conf
->inactive_list
);
1960 atomic_set(&conf
->active_stripes
, 0);
1961 atomic_set(&conf
->preread_active_stripes
, 0);
1963 PRINTK("raid6: run(%s) called.\n", mdname(mddev
));
1965 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1966 raid_disk
= rdev
->raid_disk
;
1967 if (raid_disk
>= mddev
->raid_disks
1970 disk
= conf
->disks
+ raid_disk
;
1974 if (test_bit(In_sync
, &rdev
->flags
)) {
1975 char b
[BDEVNAME_SIZE
];
1976 printk(KERN_INFO
"raid6: device %s operational as raid"
1977 " disk %d\n", bdevname(rdev
->bdev
,b
),
1979 conf
->working_disks
++;
1983 conf
->raid_disks
= mddev
->raid_disks
;
1986 * 0 for a fully functional array, 1 or 2 for a degraded array.
1988 mddev
->degraded
= conf
->failed_disks
= conf
->raid_disks
- conf
->working_disks
;
1989 conf
->mddev
= mddev
;
1990 conf
->chunk_size
= mddev
->chunk_size
;
1991 conf
->level
= mddev
->level
;
1992 conf
->algorithm
= mddev
->layout
;
1993 conf
->max_nr_stripes
= NR_STRIPES
;
1995 /* device size must be a multiple of chunk size */
1996 mddev
->size
&= ~(mddev
->chunk_size
/1024 -1);
1997 mddev
->resync_max_sectors
= mddev
->size
<< 1;
1999 if (conf
->raid_disks
< 4) {
2000 printk(KERN_ERR
"raid6: not enough configured devices for %s (%d, minimum 4)\n",
2001 mdname(mddev
), conf
->raid_disks
);
2004 if (!conf
->chunk_size
|| conf
->chunk_size
% 4) {
2005 printk(KERN_ERR
"raid6: invalid chunk size %d for %s\n",
2006 conf
->chunk_size
, mdname(mddev
));
2009 if (conf
->algorithm
> ALGORITHM_RIGHT_SYMMETRIC
) {
2011 "raid6: unsupported parity algorithm %d for %s\n",
2012 conf
->algorithm
, mdname(mddev
));
2015 if (mddev
->degraded
> 2) {
2016 printk(KERN_ERR
"raid6: not enough operational devices for %s"
2017 " (%d/%d failed)\n",
2018 mdname(mddev
), conf
->failed_disks
, conf
->raid_disks
);
2022 if (mddev
->degraded
> 0 &&
2023 mddev
->recovery_cp
!= MaxSector
) {
2024 if (mddev
->ok_start_degraded
)
2025 printk(KERN_WARNING
"raid6: starting dirty degraded array:%s"
2026 "- data corruption possible.\n",
2029 printk(KERN_ERR
"raid6: cannot start dirty degraded array"
2030 " for %s\n", mdname(mddev
));
2036 mddev
->thread
= md_register_thread(raid6d
, mddev
, "%s_raid6");
2037 if (!mddev
->thread
) {
2039 "raid6: couldn't allocate thread for %s\n",
2045 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
2046 conf
->raid_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
2047 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
2049 "raid6: couldn't allocate %dkB for buffers\n", memory
);
2050 shrink_stripes(conf
);
2051 md_unregister_thread(mddev
->thread
);
2054 printk(KERN_INFO
"raid6: allocated %dkB for %s\n",
2055 memory
, mdname(mddev
));
2057 if (mddev
->degraded
== 0)
2058 printk(KERN_INFO
"raid6: raid level %d set %s active with %d out of %d"
2059 " devices, algorithm %d\n", conf
->level
, mdname(mddev
),
2060 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
2063 printk(KERN_ALERT
"raid6: raid level %d set %s active with %d"
2064 " out of %d devices, algorithm %d\n", conf
->level
,
2065 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2066 mddev
->raid_disks
, conf
->algorithm
);
2068 print_raid6_conf(conf
);
2070 /* read-ahead size must cover two whole stripes, which is
2071 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
2074 int stripe
= (mddev
->raid_disks
-2) * mddev
->chunk_size
2076 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
2077 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
2080 /* Ok, everything is just fine now */
2081 mddev
->array_size
= mddev
->size
* (mddev
->raid_disks
- 2);
2083 mddev
->queue
->unplug_fn
= raid6_unplug_device
;
2084 mddev
->queue
->issue_flush_fn
= raid6_issue_flush
;
2088 print_raid6_conf(conf
);
2089 if (conf
->spare_page
)
2090 page_cache_release(conf
->spare_page
);
2091 if (conf
->stripe_hashtbl
)
2092 free_pages((unsigned long) conf
->stripe_hashtbl
,
2096 mddev
->private = NULL
;
2097 printk(KERN_ALERT
"raid6: failed to run raid set %s\n", mdname(mddev
));
2103 static int stop (mddev_t
*mddev
)
2105 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
2107 md_unregister_thread(mddev
->thread
);
2108 mddev
->thread
= NULL
;
2109 shrink_stripes(conf
);
2110 free_pages((unsigned long) conf
->stripe_hashtbl
, HASH_PAGES_ORDER
);
2111 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2113 mddev
->private = NULL
;
2118 static void print_sh (struct seq_file
*seq
, struct stripe_head
*sh
)
2122 seq_printf(seq
, "sh %llu, pd_idx %d, state %ld.\n",
2123 (unsigned long long)sh
->sector
, sh
->pd_idx
, sh
->state
);
2124 seq_printf(seq
, "sh %llu, count %d.\n",
2125 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
));
2126 seq_printf(seq
, "sh %llu, ", (unsigned long long)sh
->sector
);
2127 for (i
= 0; i
< sh
->raid_conf
->raid_disks
; i
++) {
2128 seq_printf(seq
, "(cache%d: %p %ld) ",
2129 i
, sh
->dev
[i
].page
, sh
->dev
[i
].flags
);
2131 seq_printf(seq
, "\n");
2134 static void printall (struct seq_file
*seq
, raid6_conf_t
*conf
)
2136 struct stripe_head
*sh
;
2139 spin_lock_irq(&conf
->device_lock
);
2140 for (i
= 0; i
< NR_HASH
; i
++) {
2141 sh
= conf
->stripe_hashtbl
[i
];
2142 for (; sh
; sh
= sh
->hash_next
) {
2143 if (sh
->raid_conf
!= conf
)
2148 spin_unlock_irq(&conf
->device_lock
);
2152 static void status (struct seq_file
*seq
, mddev_t
*mddev
)
2154 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
2157 seq_printf (seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
, mddev
->chunk_size
>> 10, mddev
->layout
);
2158 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->working_disks
);
2159 for (i
= 0; i
< conf
->raid_disks
; i
++)
2160 seq_printf (seq
, "%s",
2161 conf
->disks
[i
].rdev
&&
2162 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
2163 seq_printf (seq
, "]");
2165 seq_printf (seq
, "\n");
2166 printall(seq
, conf
);
2170 static void print_raid6_conf (raid6_conf_t
*conf
)
2173 struct disk_info
*tmp
;
2175 printk("RAID6 conf printout:\n");
2177 printk("(conf==NULL)\n");
2180 printk(" --- rd:%d wd:%d fd:%d\n", conf
->raid_disks
,
2181 conf
->working_disks
, conf
->failed_disks
);
2183 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2184 char b
[BDEVNAME_SIZE
];
2185 tmp
= conf
->disks
+ i
;
2187 printk(" disk %d, o:%d, dev:%s\n",
2188 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
2189 bdevname(tmp
->rdev
->bdev
,b
));
2193 static int raid6_spare_active(mddev_t
*mddev
)
2196 raid6_conf_t
*conf
= mddev
->private;
2197 struct disk_info
*tmp
;
2199 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2200 tmp
= conf
->disks
+ i
;
2202 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
2203 && !test_bit(In_sync
, &tmp
->rdev
->flags
)) {
2205 conf
->failed_disks
--;
2206 conf
->working_disks
++;
2207 set_bit(In_sync
, &tmp
->rdev
->flags
);
2210 print_raid6_conf(conf
);
2214 static int raid6_remove_disk(mddev_t
*mddev
, int number
)
2216 raid6_conf_t
*conf
= mddev
->private;
2219 struct disk_info
*p
= conf
->disks
+ number
;
2221 print_raid6_conf(conf
);
2224 if (test_bit(In_sync
, &rdev
->flags
) ||
2225 atomic_read(&rdev
->nr_pending
)) {
2231 if (atomic_read(&rdev
->nr_pending
)) {
2232 /* lost the race, try later */
2240 print_raid6_conf(conf
);
2244 static int raid6_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
2246 raid6_conf_t
*conf
= mddev
->private;
2249 struct disk_info
*p
;
2251 if (mddev
->degraded
> 2)
2252 /* no point adding a device */
2255 * find the disk ... but prefer rdev->saved_raid_disk
2258 if (rdev
->saved_raid_disk
>= 0 &&
2259 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
2260 disk
= rdev
->saved_raid_disk
;
2263 for ( ; disk
< mddev
->raid_disks
; disk
++)
2264 if ((p
=conf
->disks
+ disk
)->rdev
== NULL
) {
2265 clear_bit(In_sync
, &rdev
->flags
);
2266 rdev
->raid_disk
= disk
;
2268 if (rdev
->saved_raid_disk
!= disk
)
2270 rcu_assign_pointer(p
->rdev
, rdev
);
2273 print_raid6_conf(conf
);
2277 static int raid6_resize(mddev_t
*mddev
, sector_t sectors
)
2279 /* no resync is happening, and there is enough space
2280 * on all devices, so we can resize.
2281 * We need to make sure resync covers any new space.
2282 * If the array is shrinking we should possibly wait until
2283 * any io in the removed space completes, but it hardly seems
2286 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
2287 mddev
->array_size
= (sectors
* (mddev
->raid_disks
-2))>>1;
2288 set_capacity(mddev
->gendisk
, mddev
->array_size
<< 1);
2290 if (sectors
/2 > mddev
->size
&& mddev
->recovery_cp
== MaxSector
) {
2291 mddev
->recovery_cp
= mddev
->size
<< 1;
2292 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2294 mddev
->size
= sectors
/2;
2295 mddev
->resync_max_sectors
= sectors
;
2299 static void raid6_quiesce(mddev_t
*mddev
, int state
)
2301 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
2304 case 1: /* stop all writes */
2305 spin_lock_irq(&conf
->device_lock
);
2307 wait_event_lock_irq(conf
->wait_for_stripe
,
2308 atomic_read(&conf
->active_stripes
) == 0,
2309 conf
->device_lock
, /* nothing */);
2310 spin_unlock_irq(&conf
->device_lock
);
2313 case 0: /* re-enable writes */
2314 spin_lock_irq(&conf
->device_lock
);
2316 wake_up(&conf
->wait_for_stripe
);
2317 spin_unlock_irq(&conf
->device_lock
);
2322 static mdk_personality_t raid6_personality
=
2325 .owner
= THIS_MODULE
,
2326 .make_request
= make_request
,
2330 .error_handler
= error
,
2331 .hot_add_disk
= raid6_add_disk
,
2332 .hot_remove_disk
= raid6_remove_disk
,
2333 .spare_active
= raid6_spare_active
,
2334 .sync_request
= sync_request
,
2335 .resize
= raid6_resize
,
2336 .quiesce
= raid6_quiesce
,
2339 static int __init
raid6_init (void)
2343 e
= raid6_select_algo();
2347 return register_md_personality (RAID6
, &raid6_personality
);
2350 static void raid6_exit (void)
2352 unregister_md_personality (RAID6
);
2355 module_init(raid6_init
);
2356 module_exit(raid6_exit
);
2357 MODULE_LICENSE("GPL");
2358 MODULE_ALIAS("md-personality-8"); /* RAID6 */