2 * raid6main.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-6 management functions. This code is derived from raid5.c.
8 * Last merge from raid5.c bkcvs version 1.79 (kernel 2.6.1).
10 * Thanks to Penguin Computing for making the RAID-6 development possible
11 * by donating a test server!
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
18 * You should have received a copy of the GNU General Public License
19 * (for example /usr/src/linux/COPYING); if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/highmem.h>
28 #include <linux/bitops.h>
29 #include <asm/atomic.h>
32 #include <linux/raid/bitmap.h>
38 #define NR_STRIPES 256
39 #define STRIPE_SIZE PAGE_SIZE
40 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
41 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
42 #define IO_THRESHOLD 1
43 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
44 #define HASH_MASK (NR_HASH - 1)
46 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
48 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
49 * order without overlap. There may be several bio's per stripe+device, and
50 * a bio could span several devices.
51 * When walking this list for a particular stripe+device, we must never proceed
52 * beyond a bio that extends past this device, as the next bio might no longer
54 * This macro is used to determine the 'next' bio in the list, given the sector
55 * of the current stripe+device
57 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
59 * The following can be used to debug the driver
61 #define RAID6_DEBUG 0 /* Extremely verbose printk */
62 #define RAID6_PARANOIA 1 /* Check spinlocks */
63 #define RAID6_DUMPSTATE 0 /* Include stripe cache state in /proc/mdstat */
64 #if RAID6_PARANOIA && defined(CONFIG_SMP)
65 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
67 # define CHECK_DEVLOCK()
70 #define PRINTK(x...) ((void)(RAID6_DEBUG && printk(KERN_DEBUG x)))
78 #if !RAID6_USE_EMPTY_ZERO_PAGE
79 /* In .bss so it's zeroed */
80 const char raid6_empty_zero_page
[PAGE_SIZE
] __attribute__((aligned(256)));
83 static inline int raid6_next_disk(int disk
, int raid_disks
)
86 return (disk
< raid_disks
) ? disk
: 0;
89 static void print_raid6_conf (raid6_conf_t
*conf
);
91 static void __release_stripe(raid6_conf_t
*conf
, struct stripe_head
*sh
)
93 if (atomic_dec_and_test(&sh
->count
)) {
94 if (!list_empty(&sh
->lru
))
96 if (atomic_read(&conf
->active_stripes
)==0)
98 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
99 if (test_bit(STRIPE_DELAYED
, &sh
->state
))
100 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
101 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
102 conf
->seq_write
== sh
->bm_seq
)
103 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
105 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
106 list_add_tail(&sh
->lru
, &conf
->handle_list
);
108 md_wakeup_thread(conf
->mddev
->thread
);
110 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
111 atomic_dec(&conf
->preread_active_stripes
);
112 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
113 md_wakeup_thread(conf
->mddev
->thread
);
115 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
116 atomic_dec(&conf
->active_stripes
);
117 if (!conf
->inactive_blocked
||
118 atomic_read(&conf
->active_stripes
) < (NR_STRIPES
*3/4))
119 wake_up(&conf
->wait_for_stripe
);
123 static void release_stripe(struct stripe_head
*sh
)
125 raid6_conf_t
*conf
= sh
->raid_conf
;
128 spin_lock_irqsave(&conf
->device_lock
, flags
);
129 __release_stripe(conf
, sh
);
130 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
133 static inline void remove_hash(struct stripe_head
*sh
)
135 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
137 hlist_del_init(&sh
->hash
);
140 static inline void insert_hash(raid6_conf_t
*conf
, struct stripe_head
*sh
)
142 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
144 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
147 hlist_add_head(&sh
->hash
, hp
);
151 /* find an idle stripe, make sure it is unhashed, and return it. */
152 static struct stripe_head
*get_free_stripe(raid6_conf_t
*conf
)
154 struct stripe_head
*sh
= NULL
;
155 struct list_head
*first
;
158 if (list_empty(&conf
->inactive_list
))
160 first
= conf
->inactive_list
.next
;
161 sh
= list_entry(first
, struct stripe_head
, lru
);
162 list_del_init(first
);
164 atomic_inc(&conf
->active_stripes
);
169 static void shrink_buffers(struct stripe_head
*sh
, int num
)
174 for (i
=0; i
<num
; i
++) {
178 sh
->dev
[i
].page
= NULL
;
183 static int grow_buffers(struct stripe_head
*sh
, int num
)
187 for (i
=0; i
<num
; i
++) {
190 if (!(page
= alloc_page(GFP_KERNEL
))) {
193 sh
->dev
[i
].page
= page
;
198 static void raid6_build_block (struct stripe_head
*sh
, int i
);
200 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int pd_idx
)
202 raid6_conf_t
*conf
= sh
->raid_conf
;
203 int disks
= conf
->raid_disks
, i
;
205 if (atomic_read(&sh
->count
) != 0)
207 if (test_bit(STRIPE_HANDLE
, &sh
->state
))
211 PRINTK("init_stripe called, stripe %llu\n",
212 (unsigned long long)sh
->sector
);
220 for (i
=disks
; i
--; ) {
221 struct r5dev
*dev
= &sh
->dev
[i
];
223 if (dev
->toread
|| dev
->towrite
|| dev
->written
||
224 test_bit(R5_LOCKED
, &dev
->flags
)) {
225 PRINTK("sector=%llx i=%d %p %p %p %d\n",
226 (unsigned long long)sh
->sector
, i
, dev
->toread
,
227 dev
->towrite
, dev
->written
,
228 test_bit(R5_LOCKED
, &dev
->flags
));
232 raid6_build_block(sh
, i
);
234 insert_hash(conf
, sh
);
237 static struct stripe_head
*__find_stripe(raid6_conf_t
*conf
, sector_t sector
)
239 struct stripe_head
*sh
;
240 struct hlist_node
*hn
;
243 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector
);
244 hlist_for_each_entry (sh
, hn
, stripe_hash(conf
, sector
), hash
)
245 if (sh
->sector
== sector
)
247 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector
);
251 static void unplug_slaves(mddev_t
*mddev
);
253 static struct stripe_head
*get_active_stripe(raid6_conf_t
*conf
, sector_t sector
,
254 int pd_idx
, int noblock
)
256 struct stripe_head
*sh
;
258 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector
);
260 spin_lock_irq(&conf
->device_lock
);
263 wait_event_lock_irq(conf
->wait_for_stripe
,
265 conf
->device_lock
, /* nothing */);
266 sh
= __find_stripe(conf
, sector
);
268 if (!conf
->inactive_blocked
)
269 sh
= get_free_stripe(conf
);
270 if (noblock
&& sh
== NULL
)
273 conf
->inactive_blocked
= 1;
274 wait_event_lock_irq(conf
->wait_for_stripe
,
275 !list_empty(&conf
->inactive_list
) &&
276 (atomic_read(&conf
->active_stripes
) < (NR_STRIPES
*3/4)
277 || !conf
->inactive_blocked
),
279 unplug_slaves(conf
->mddev
);
281 conf
->inactive_blocked
= 0;
283 init_stripe(sh
, sector
, pd_idx
);
285 if (atomic_read(&sh
->count
)) {
286 if (!list_empty(&sh
->lru
))
289 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
290 atomic_inc(&conf
->active_stripes
);
291 if (list_empty(&sh
->lru
))
293 list_del_init(&sh
->lru
);
296 } while (sh
== NULL
);
299 atomic_inc(&sh
->count
);
301 spin_unlock_irq(&conf
->device_lock
);
305 static int grow_stripes(raid6_conf_t
*conf
, int num
)
307 struct stripe_head
*sh
;
309 int devs
= conf
->raid_disks
;
311 sprintf(conf
->cache_name
, "raid6/%s", mdname(conf
->mddev
));
313 sc
= kmem_cache_create(conf
->cache_name
,
314 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
318 conf
->slab_cache
= sc
;
320 sh
= kmem_cache_alloc(sc
, GFP_KERNEL
);
323 memset(sh
, 0, sizeof(*sh
) + (devs
-1)*sizeof(struct r5dev
));
324 sh
->raid_conf
= conf
;
325 spin_lock_init(&sh
->lock
);
327 if (grow_buffers(sh
, conf
->raid_disks
)) {
328 shrink_buffers(sh
, conf
->raid_disks
);
329 kmem_cache_free(sc
, sh
);
332 /* we just created an active stripe so... */
333 atomic_set(&sh
->count
, 1);
334 atomic_inc(&conf
->active_stripes
);
335 INIT_LIST_HEAD(&sh
->lru
);
341 static void shrink_stripes(raid6_conf_t
*conf
)
343 struct stripe_head
*sh
;
346 spin_lock_irq(&conf
->device_lock
);
347 sh
= get_free_stripe(conf
);
348 spin_unlock_irq(&conf
->device_lock
);
351 if (atomic_read(&sh
->count
))
353 shrink_buffers(sh
, conf
->raid_disks
);
354 kmem_cache_free(conf
->slab_cache
, sh
);
355 atomic_dec(&conf
->active_stripes
);
357 kmem_cache_destroy(conf
->slab_cache
);
358 conf
->slab_cache
= NULL
;
361 static int raid6_end_read_request(struct bio
* bi
, unsigned int bytes_done
,
364 struct stripe_head
*sh
= bi
->bi_private
;
365 raid6_conf_t
*conf
= sh
->raid_conf
;
366 int disks
= conf
->raid_disks
, i
;
367 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
372 for (i
=0 ; i
<disks
; i
++)
373 if (bi
== &sh
->dev
[i
].req
)
376 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
377 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
388 spin_lock_irqsave(&conf
->device_lock
, flags
);
389 /* we can return a buffer if we bypassed the cache or
390 * if the top buffer is not in highmem. If there are
391 * multiple buffers, leave the extra work to
394 buffer
= sh
->bh_read
[i
];
396 (!PageHighMem(buffer
->b_page
)
397 || buffer
->b_page
== bh
->b_page
)
399 sh
->bh_read
[i
] = buffer
->b_reqnext
;
400 buffer
->b_reqnext
= NULL
;
403 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
404 if (sh
->bh_page
[i
]==bh
->b_page
)
405 set_buffer_uptodate(bh
);
407 if (buffer
->b_page
!= bh
->b_page
)
408 memcpy(buffer
->b_data
, bh
->b_data
, bh
->b_size
);
409 buffer
->b_end_io(buffer
, 1);
412 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
414 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
415 printk(KERN_INFO
"raid6: read error corrected!!\n");
416 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
417 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
419 if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
))
420 atomic_set(&conf
->disks
[i
].rdev
->read_errors
, 0);
423 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
424 atomic_inc(&conf
->disks
[i
].rdev
->read_errors
);
425 if (conf
->mddev
->degraded
)
426 printk(KERN_WARNING
"raid6: read error not correctable.\n");
427 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
429 printk(KERN_WARNING
"raid6: read error NOT corrected!!\n");
430 else if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
)
431 > conf
->max_nr_stripes
)
433 "raid6: Too many read errors, failing device.\n");
437 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
439 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
440 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
441 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
444 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
446 /* must restore b_page before unlocking buffer... */
447 if (sh
->bh_page
[i
] != bh
->b_page
) {
448 bh
->b_page
= sh
->bh_page
[i
];
449 bh
->b_data
= page_address(bh
->b_page
);
450 clear_buffer_uptodate(bh
);
453 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
454 set_bit(STRIPE_HANDLE
, &sh
->state
);
459 static int raid6_end_write_request (struct bio
*bi
, unsigned int bytes_done
,
462 struct stripe_head
*sh
= bi
->bi_private
;
463 raid6_conf_t
*conf
= sh
->raid_conf
;
464 int disks
= conf
->raid_disks
, i
;
466 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
471 for (i
=0 ; i
<disks
; i
++)
472 if (bi
== &sh
->dev
[i
].req
)
475 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
476 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
483 spin_lock_irqsave(&conf
->device_lock
, flags
);
485 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
487 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
489 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
490 set_bit(STRIPE_HANDLE
, &sh
->state
);
491 __release_stripe(conf
, sh
);
492 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
497 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
);
499 static void raid6_build_block (struct stripe_head
*sh
, int i
)
501 struct r5dev
*dev
= &sh
->dev
[i
];
502 int pd_idx
= sh
->pd_idx
;
503 int qd_idx
= raid6_next_disk(pd_idx
, sh
->raid_conf
->raid_disks
);
506 dev
->req
.bi_io_vec
= &dev
->vec
;
508 dev
->req
.bi_max_vecs
++;
509 dev
->vec
.bv_page
= dev
->page
;
510 dev
->vec
.bv_len
= STRIPE_SIZE
;
511 dev
->vec
.bv_offset
= 0;
513 dev
->req
.bi_sector
= sh
->sector
;
514 dev
->req
.bi_private
= sh
;
517 if (i
!= pd_idx
&& i
!= qd_idx
)
518 dev
->sector
= compute_blocknr(sh
, i
);
521 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
523 char b
[BDEVNAME_SIZE
];
524 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
525 PRINTK("raid6: error called\n");
527 if (!test_bit(Faulty
, &rdev
->flags
)) {
529 if (test_bit(In_sync
, &rdev
->flags
)) {
530 conf
->working_disks
--;
532 conf
->failed_disks
++;
533 clear_bit(In_sync
, &rdev
->flags
);
535 * if recovery was running, make sure it aborts.
537 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
539 set_bit(Faulty
, &rdev
->flags
);
541 "raid6: Disk failure on %s, disabling device."
542 " Operation continuing on %d devices\n",
543 bdevname(rdev
->bdev
,b
), conf
->working_disks
);
548 * Input: a 'big' sector number,
549 * Output: index of the data and parity disk, and the sector # in them.
551 static sector_t
raid6_compute_sector(sector_t r_sector
, unsigned int raid_disks
,
552 unsigned int data_disks
, unsigned int * dd_idx
,
553 unsigned int * pd_idx
, raid6_conf_t
*conf
)
556 unsigned long chunk_number
;
557 unsigned int chunk_offset
;
559 int sectors_per_chunk
= conf
->chunk_size
>> 9;
561 /* First compute the information on this sector */
564 * Compute the chunk number and the sector offset inside the chunk
566 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
567 chunk_number
= r_sector
;
568 if ( r_sector
!= chunk_number
) {
569 printk(KERN_CRIT
"raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
570 (unsigned long long)r_sector
, (unsigned long)chunk_number
);
575 * Compute the stripe number
577 stripe
= chunk_number
/ data_disks
;
580 * Compute the data disk and parity disk indexes inside the stripe
582 *dd_idx
= chunk_number
% data_disks
;
585 * Select the parity disk based on the user selected algorithm.
589 switch (conf
->algorithm
) {
590 case ALGORITHM_LEFT_ASYMMETRIC
:
591 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
592 if (*pd_idx
== raid_disks
-1)
593 (*dd_idx
)++; /* Q D D D P */
594 else if (*dd_idx
>= *pd_idx
)
595 (*dd_idx
) += 2; /* D D P Q D */
597 case ALGORITHM_RIGHT_ASYMMETRIC
:
598 *pd_idx
= stripe
% raid_disks
;
599 if (*pd_idx
== raid_disks
-1)
600 (*dd_idx
)++; /* Q D D D P */
601 else if (*dd_idx
>= *pd_idx
)
602 (*dd_idx
) += 2; /* D D P Q D */
604 case ALGORITHM_LEFT_SYMMETRIC
:
605 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
606 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
608 case ALGORITHM_RIGHT_SYMMETRIC
:
609 *pd_idx
= stripe
% raid_disks
;
610 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
613 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
617 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
618 chunk_number
, *pd_idx
, *dd_idx
);
621 * Finally, compute the new sector number
623 new_sector
= (sector_t
) stripe
* sectors_per_chunk
+ chunk_offset
;
628 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
)
630 raid6_conf_t
*conf
= sh
->raid_conf
;
631 int raid_disks
= conf
->raid_disks
, data_disks
= raid_disks
- 2;
632 sector_t new_sector
= sh
->sector
, check
;
633 int sectors_per_chunk
= conf
->chunk_size
>> 9;
636 int chunk_number
, dummy1
, dummy2
, dd_idx
= i
;
640 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
642 if ( new_sector
!= stripe
) {
643 printk(KERN_CRIT
"raid6: ERROR: new_sector = %llu, stripe = %lu\n",
644 (unsigned long long)new_sector
, (unsigned long)stripe
);
648 switch (conf
->algorithm
) {
649 case ALGORITHM_LEFT_ASYMMETRIC
:
650 case ALGORITHM_RIGHT_ASYMMETRIC
:
651 if (sh
->pd_idx
== raid_disks
-1)
653 else if (i
> sh
->pd_idx
)
654 i
-= 2; /* D D P Q D */
656 case ALGORITHM_LEFT_SYMMETRIC
:
657 case ALGORITHM_RIGHT_SYMMETRIC
:
658 if (sh
->pd_idx
== raid_disks
-1)
664 i
-= (sh
->pd_idx
+ 2);
668 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
672 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh
->pd_idx
, i0
, i
);
674 chunk_number
= stripe
* data_disks
+ i
;
675 r_sector
= (sector_t
)chunk_number
* sectors_per_chunk
+ chunk_offset
;
677 check
= raid6_compute_sector (r_sector
, raid_disks
, data_disks
, &dummy1
, &dummy2
, conf
);
678 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| dummy2
!= sh
->pd_idx
) {
679 printk(KERN_CRIT
"raid6: compute_blocknr: map not correct\n");
688 * Copy data between a page in the stripe cache, and one or more bion
689 * The page could align with the middle of the bio, or there could be
690 * several bion, each with several bio_vecs, which cover part of the page
691 * Multiple bion are linked together on bi_next. There may be extras
692 * at the end of this list. We ignore them.
694 static void copy_data(int frombio
, struct bio
*bio
,
698 char *pa
= page_address(page
);
703 if (bio
->bi_sector
>= sector
)
704 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
706 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
707 bio_for_each_segment(bvl
, bio
, i
) {
708 int len
= bio_iovec_idx(bio
,i
)->bv_len
;
712 if (page_offset
< 0) {
713 b_offset
= -page_offset
;
714 page_offset
+= b_offset
;
718 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
719 clen
= STRIPE_SIZE
- page_offset
;
723 char *ba
= __bio_kmap_atomic(bio
, i
, KM_USER0
);
725 memcpy(pa
+page_offset
, ba
+b_offset
, clen
);
727 memcpy(ba
+b_offset
, pa
+page_offset
, clen
);
728 __bio_kunmap_atomic(ba
, KM_USER0
);
730 if (clen
< len
) /* hit end of page */
736 #define check_xor() do { \
737 if (count == MAX_XOR_BLOCKS) { \
738 xor_block(count, STRIPE_SIZE, ptr); \
743 /* Compute P and Q syndromes */
744 static void compute_parity(struct stripe_head
*sh
, int method
)
746 raid6_conf_t
*conf
= sh
->raid_conf
;
747 int i
, pd_idx
= sh
->pd_idx
, qd_idx
, d0_idx
, disks
= conf
->raid_disks
, count
;
749 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
752 qd_idx
= raid6_next_disk(pd_idx
, disks
);
753 d0_idx
= raid6_next_disk(qd_idx
, disks
);
755 PRINTK("compute_parity, stripe %llu, method %d\n",
756 (unsigned long long)sh
->sector
, method
);
759 case READ_MODIFY_WRITE
:
760 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
761 case RECONSTRUCT_WRITE
:
762 for (i
= disks
; i
-- ;)
763 if ( i
!= pd_idx
&& i
!= qd_idx
&& sh
->dev
[i
].towrite
) {
764 chosen
= sh
->dev
[i
].towrite
;
765 sh
->dev
[i
].towrite
= NULL
;
767 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
768 wake_up(&conf
->wait_for_overlap
);
770 if (sh
->dev
[i
].written
) BUG();
771 sh
->dev
[i
].written
= chosen
;
775 BUG(); /* Not implemented yet */
778 for (i
= disks
; i
--;)
779 if (sh
->dev
[i
].written
) {
780 sector_t sector
= sh
->dev
[i
].sector
;
781 struct bio
*wbi
= sh
->dev
[i
].written
;
782 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
783 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
784 wbi
= r5_next_bio(wbi
, sector
);
787 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
788 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
792 // case RECONSTRUCT_WRITE:
793 // case CHECK_PARITY:
794 // case UPDATE_PARITY:
795 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
796 /* FIX: Is this ordering of drives even remotely optimal? */
800 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
801 if (count
<= disks
-2 && !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
802 printk("block %d/%d not uptodate on parity calc\n", i
,count
);
803 i
= raid6_next_disk(i
, disks
);
804 } while ( i
!= d0_idx
);
808 raid6_call
.gen_syndrome(disks
, STRIPE_SIZE
, ptrs
);
811 case RECONSTRUCT_WRITE
:
812 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
813 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
814 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
815 set_bit(R5_LOCKED
, &sh
->dev
[qd_idx
].flags
);
818 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
819 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
824 /* Compute one missing block */
825 static void compute_block_1(struct stripe_head
*sh
, int dd_idx
, int nozero
)
827 raid6_conf_t
*conf
= sh
->raid_conf
;
828 int i
, count
, disks
= conf
->raid_disks
;
829 void *ptr
[MAX_XOR_BLOCKS
], *p
;
830 int pd_idx
= sh
->pd_idx
;
831 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
833 PRINTK("compute_block_1, stripe %llu, idx %d\n",
834 (unsigned long long)sh
->sector
, dd_idx
);
836 if ( dd_idx
== qd_idx
) {
837 /* We're actually computing the Q drive */
838 compute_parity(sh
, UPDATE_PARITY
);
840 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
841 if (!nozero
) memset(ptr
[0], 0, STRIPE_SIZE
);
843 for (i
= disks
; i
--; ) {
844 if (i
== dd_idx
|| i
== qd_idx
)
846 p
= page_address(sh
->dev
[i
].page
);
847 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
850 printk("compute_block() %d, stripe %llu, %d"
851 " not present\n", dd_idx
,
852 (unsigned long long)sh
->sector
, i
);
857 xor_block(count
, STRIPE_SIZE
, ptr
);
858 if (!nozero
) set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
859 else clear_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
863 /* Compute two missing blocks */
864 static void compute_block_2(struct stripe_head
*sh
, int dd_idx1
, int dd_idx2
)
866 raid6_conf_t
*conf
= sh
->raid_conf
;
867 int i
, count
, disks
= conf
->raid_disks
;
868 int pd_idx
= sh
->pd_idx
;
869 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
870 int d0_idx
= raid6_next_disk(qd_idx
, disks
);
873 /* faila and failb are disk numbers relative to d0_idx */
874 /* pd_idx become disks-2 and qd_idx become disks-1 */
875 faila
= (dd_idx1
< d0_idx
) ? dd_idx1
+(disks
-d0_idx
) : dd_idx1
-d0_idx
;
876 failb
= (dd_idx2
< d0_idx
) ? dd_idx2
+(disks
-d0_idx
) : dd_idx2
-d0_idx
;
878 BUG_ON(faila
== failb
);
879 if ( failb
< faila
) { int tmp
= faila
; faila
= failb
; failb
= tmp
; }
881 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
882 (unsigned long long)sh
->sector
, dd_idx1
, dd_idx2
, faila
, failb
);
884 if ( failb
== disks
-1 ) {
885 /* Q disk is one of the missing disks */
886 if ( faila
== disks
-2 ) {
887 /* Missing P+Q, just recompute */
888 compute_parity(sh
, UPDATE_PARITY
);
891 /* We're missing D+Q; recompute D from P */
892 compute_block_1(sh
, (dd_idx1
== qd_idx
) ? dd_idx2
: dd_idx1
, 0);
893 compute_parity(sh
, UPDATE_PARITY
); /* Is this necessary? */
898 /* We're missing D+P or D+D; build pointer table */
900 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
906 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
907 i
= raid6_next_disk(i
, disks
);
908 if (i
!= dd_idx1
&& i
!= dd_idx2
&&
909 !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
910 printk("compute_2 with missing block %d/%d\n", count
, i
);
911 } while ( i
!= d0_idx
);
913 if ( failb
== disks
-2 ) {
914 /* We're missing D+P. */
915 raid6_datap_recov(disks
, STRIPE_SIZE
, faila
, ptrs
);
917 /* We're missing D+D. */
918 raid6_2data_recov(disks
, STRIPE_SIZE
, faila
, failb
, ptrs
);
921 /* Both the above update both missing blocks */
922 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx1
].flags
);
923 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx2
].flags
);
929 * Each stripe/dev can have one or more bion attached.
930 * toread/towrite point to the first in a chain.
931 * The bi_next chain must be in order.
933 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
936 raid6_conf_t
*conf
= sh
->raid_conf
;
939 PRINTK("adding bh b#%llu to stripe s#%llu\n",
940 (unsigned long long)bi
->bi_sector
,
941 (unsigned long long)sh
->sector
);
944 spin_lock(&sh
->lock
);
945 spin_lock_irq(&conf
->device_lock
);
947 bip
= &sh
->dev
[dd_idx
].towrite
;
948 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
951 bip
= &sh
->dev
[dd_idx
].toread
;
952 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
953 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
955 bip
= &(*bip
)->bi_next
;
957 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
960 if (*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
)
965 bi
->bi_phys_segments
++;
966 spin_unlock_irq(&conf
->device_lock
);
967 spin_unlock(&sh
->lock
);
969 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
970 (unsigned long long)bi
->bi_sector
,
971 (unsigned long long)sh
->sector
, dd_idx
);
973 if (conf
->mddev
->bitmap
&& firstwrite
) {
974 sh
->bm_seq
= conf
->seq_write
;
975 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
977 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
981 /* check if page is covered */
982 sector_t sector
= sh
->dev
[dd_idx
].sector
;
983 for (bi
=sh
->dev
[dd_idx
].towrite
;
984 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
985 bi
&& bi
->bi_sector
<= sector
;
986 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
987 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
988 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
990 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
991 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
996 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
997 spin_unlock_irq(&conf
->device_lock
);
998 spin_unlock(&sh
->lock
);
1003 static int page_is_zero(struct page
*p
)
1005 char *a
= page_address(p
);
1006 return ((*(u32
*)a
) == 0 &&
1007 memcmp(a
, a
+4, STRIPE_SIZE
-4)==0);
1010 * handle_stripe - do things to a stripe.
1012 * We lock the stripe and then examine the state of various bits
1013 * to see what needs to be done.
1015 * return some read request which now have data
1016 * return some write requests which are safely on disc
1017 * schedule a read on some buffers
1018 * schedule a write of some buffers
1019 * return confirmation of parity correctness
1021 * Parity calculations are done inside the stripe lock
1022 * buffers are taken off read_list or write_list, and bh_cache buffers
1023 * get BH_Lock set before the stripe lock is released.
1027 static void handle_stripe(struct stripe_head
*sh
, struct page
*tmp_page
)
1029 raid6_conf_t
*conf
= sh
->raid_conf
;
1030 int disks
= conf
->raid_disks
;
1031 struct bio
*return_bi
= NULL
;
1035 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
1036 int non_overwrite
= 0;
1037 int failed_num
[2] = {0, 0};
1038 struct r5dev
*dev
, *pdev
, *qdev
;
1039 int pd_idx
= sh
->pd_idx
;
1040 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1041 int p_failed
, q_failed
;
1043 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1044 (unsigned long long)sh
->sector
, sh
->state
, atomic_read(&sh
->count
),
1047 spin_lock(&sh
->lock
);
1048 clear_bit(STRIPE_HANDLE
, &sh
->state
);
1049 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1051 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
1052 /* Now to look around and see what can be done */
1055 for (i
=disks
; i
--; ) {
1058 clear_bit(R5_Insync
, &dev
->flags
);
1060 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1061 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
1062 /* maybe we can reply to a read */
1063 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
1064 struct bio
*rbi
, *rbi2
;
1065 PRINTK("Return read for disc %d\n", i
);
1066 spin_lock_irq(&conf
->device_lock
);
1069 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1070 wake_up(&conf
->wait_for_overlap
);
1071 spin_unlock_irq(&conf
->device_lock
);
1072 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1073 copy_data(0, rbi
, dev
->page
, dev
->sector
);
1074 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1075 spin_lock_irq(&conf
->device_lock
);
1076 if (--rbi
->bi_phys_segments
== 0) {
1077 rbi
->bi_next
= return_bi
;
1080 spin_unlock_irq(&conf
->device_lock
);
1085 /* now count some things */
1086 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
1087 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
1090 if (dev
->toread
) to_read
++;
1093 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1096 if (dev
->written
) written
++;
1097 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1098 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1099 /* The ReadError flag will just be confusing now */
1100 clear_bit(R5_ReadError
, &dev
->flags
);
1101 clear_bit(R5_ReWrite
, &dev
->flags
);
1103 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1104 || test_bit(R5_ReadError
, &dev
->flags
)) {
1106 failed_num
[failed
] = i
;
1109 set_bit(R5_Insync
, &dev
->flags
);
1112 PRINTK("locked=%d uptodate=%d to_read=%d"
1113 " to_write=%d failed=%d failed_num=%d,%d\n",
1114 locked
, uptodate
, to_read
, to_write
, failed
,
1115 failed_num
[0], failed_num
[1]);
1116 /* check if the array has lost >2 devices and, if so, some requests might
1119 if (failed
> 2 && to_read
+to_write
+written
) {
1120 for (i
=disks
; i
--; ) {
1123 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1126 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1127 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1128 /* multiple read failures in one stripe */
1129 md_error(conf
->mddev
, rdev
);
1133 spin_lock_irq(&conf
->device_lock
);
1134 /* fail all writes first */
1135 bi
= sh
->dev
[i
].towrite
;
1136 sh
->dev
[i
].towrite
= NULL
;
1137 if (bi
) { to_write
--; bitmap_end
= 1; }
1139 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1140 wake_up(&conf
->wait_for_overlap
);
1142 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1143 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1144 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1145 if (--bi
->bi_phys_segments
== 0) {
1146 md_write_end(conf
->mddev
);
1147 bi
->bi_next
= return_bi
;
1152 /* and fail all 'written' */
1153 bi
= sh
->dev
[i
].written
;
1154 sh
->dev
[i
].written
= NULL
;
1155 if (bi
) bitmap_end
= 1;
1156 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
1157 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1158 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1159 if (--bi
->bi_phys_segments
== 0) {
1160 md_write_end(conf
->mddev
);
1161 bi
->bi_next
= return_bi
;
1167 /* fail any reads if this device is non-operational */
1168 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
1169 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1170 bi
= sh
->dev
[i
].toread
;
1171 sh
->dev
[i
].toread
= NULL
;
1172 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1173 wake_up(&conf
->wait_for_overlap
);
1175 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1176 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1177 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1178 if (--bi
->bi_phys_segments
== 0) {
1179 bi
->bi_next
= return_bi
;
1185 spin_unlock_irq(&conf
->device_lock
);
1187 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1188 STRIPE_SECTORS
, 0, 0);
1191 if (failed
> 2 && syncing
) {
1192 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
1193 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1198 * might be able to return some write requests if the parity blocks
1199 * are safe, or on a failed drive
1201 pdev
= &sh
->dev
[pd_idx
];
1202 p_failed
= (failed
>= 1 && failed_num
[0] == pd_idx
)
1203 || (failed
>= 2 && failed_num
[1] == pd_idx
);
1204 qdev
= &sh
->dev
[qd_idx
];
1205 q_failed
= (failed
>= 1 && failed_num
[0] == qd_idx
)
1206 || (failed
>= 2 && failed_num
[1] == qd_idx
);
1209 ( p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
1210 && !test_bit(R5_LOCKED
, &pdev
->flags
)
1211 && test_bit(R5_UPTODATE
, &pdev
->flags
))) ) &&
1212 ( q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
1213 && !test_bit(R5_LOCKED
, &qdev
->flags
)
1214 && test_bit(R5_UPTODATE
, &qdev
->flags
))) ) ) {
1215 /* any written block on an uptodate or failed drive can be
1216 * returned. Note that if we 'wrote' to a failed drive,
1217 * it will be UPTODATE, but never LOCKED, so we don't need
1218 * to test 'failed' directly.
1220 for (i
=disks
; i
--; )
1221 if (sh
->dev
[i
].written
) {
1223 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
1224 test_bit(R5_UPTODATE
, &dev
->flags
) ) {
1225 /* We can return any write requests */
1227 struct bio
*wbi
, *wbi2
;
1228 PRINTK("Return write for stripe %llu disc %d\n",
1229 (unsigned long long)sh
->sector
, i
);
1230 spin_lock_irq(&conf
->device_lock
);
1232 dev
->written
= NULL
;
1233 while (wbi
&& wbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1234 wbi2
= r5_next_bio(wbi
, dev
->sector
);
1235 if (--wbi
->bi_phys_segments
== 0) {
1236 md_write_end(conf
->mddev
);
1237 wbi
->bi_next
= return_bi
;
1242 if (dev
->towrite
== NULL
)
1244 spin_unlock_irq(&conf
->device_lock
);
1246 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1248 !test_bit(STRIPE_DEGRADED
, &sh
->state
), 0);
1253 /* Now we might consider reading some blocks, either to check/generate
1254 * parity, or to satisfy requests
1255 * or to load a block that is being partially written.
1257 if (to_read
|| non_overwrite
|| (to_write
&& failed
) || (syncing
&& (uptodate
< disks
))) {
1258 for (i
=disks
; i
--;) {
1260 if (!test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1262 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
1264 (failed
>= 1 && (sh
->dev
[failed_num
[0]].toread
|| to_write
)) ||
1265 (failed
>= 2 && (sh
->dev
[failed_num
[1]].toread
|| to_write
))
1268 /* we would like to get this block, possibly
1269 * by computing it, but we might not be able to
1271 if (uptodate
== disks
-1) {
1272 PRINTK("Computing stripe %llu block %d\n",
1273 (unsigned long long)sh
->sector
, i
);
1274 compute_block_1(sh
, i
, 0);
1276 } else if ( uptodate
== disks
-2 && failed
>= 2 ) {
1277 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1279 for (other
=disks
; other
--;) {
1282 if ( !test_bit(R5_UPTODATE
, &sh
->dev
[other
].flags
) )
1286 PRINTK("Computing stripe %llu blocks %d,%d\n",
1287 (unsigned long long)sh
->sector
, i
, other
);
1288 compute_block_2(sh
, i
, other
);
1290 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
1291 set_bit(R5_LOCKED
, &dev
->flags
);
1292 set_bit(R5_Wantread
, &dev
->flags
);
1294 /* if I am just reading this block and we don't have
1295 a failed drive, or any pending writes then sidestep the cache */
1296 if (sh
->bh_read
[i
] && !sh
->bh_read
[i
]->b_reqnext
&&
1297 ! syncing
&& !failed
&& !to_write
) {
1298 sh
->bh_cache
[i
]->b_page
= sh
->bh_read
[i
]->b_page
;
1299 sh
->bh_cache
[i
]->b_data
= sh
->bh_read
[i
]->b_data
;
1303 PRINTK("Reading block %d (sync=%d)\n",
1308 set_bit(STRIPE_HANDLE
, &sh
->state
);
1311 /* now to consider writing and what else, if anything should be read */
1313 int rcw
=0, must_compute
=0;
1314 for (i
=disks
; i
--;) {
1316 /* Would I have to read this buffer for reconstruct_write */
1317 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
1318 && i
!= pd_idx
&& i
!= qd_idx
1319 && (!test_bit(R5_LOCKED
, &dev
->flags
)
1321 || sh
->bh_page
[i
] != bh
->b_page
1324 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1325 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
1327 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i
, dev
->flags
);
1332 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1333 (unsigned long long)sh
->sector
, rcw
, must_compute
);
1334 set_bit(STRIPE_HANDLE
, &sh
->state
);
1337 /* want reconstruct write, but need to get some data */
1338 for (i
=disks
; i
--;) {
1340 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
1341 && !(failed
== 0 && (i
== pd_idx
|| i
== qd_idx
))
1342 && !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1343 test_bit(R5_Insync
, &dev
->flags
)) {
1344 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1346 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1347 (unsigned long long)sh
->sector
, i
);
1348 set_bit(R5_LOCKED
, &dev
->flags
);
1349 set_bit(R5_Wantread
, &dev
->flags
);
1352 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1353 (unsigned long long)sh
->sector
, i
);
1354 set_bit(STRIPE_DELAYED
, &sh
->state
);
1355 set_bit(STRIPE_HANDLE
, &sh
->state
);
1359 /* now if nothing is locked, and if we have enough data, we can start a write request */
1360 if (locked
== 0 && rcw
== 0 &&
1361 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
1362 if ( must_compute
> 0 ) {
1363 /* We have failed blocks and need to compute them */
1366 case 1: compute_block_1(sh
, failed_num
[0], 0); break;
1367 case 2: compute_block_2(sh
, failed_num
[0], failed_num
[1]); break;
1368 default: BUG(); /* This request should have been failed? */
1372 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh
->sector
);
1373 compute_parity(sh
, RECONSTRUCT_WRITE
);
1374 /* now every locked buffer is ready to be written */
1376 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
1377 PRINTK("Writing stripe %llu block %d\n",
1378 (unsigned long long)sh
->sector
, i
);
1380 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1382 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
1383 set_bit(STRIPE_INSYNC
, &sh
->state
);
1385 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
1386 atomic_dec(&conf
->preread_active_stripes
);
1387 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
1388 md_wakeup_thread(conf
->mddev
->thread
);
1393 /* maybe we need to check and possibly fix the parity for this stripe
1394 * Any reads will already have been scheduled, so we just see if enough data
1397 if (syncing
&& locked
== 0 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1398 int update_p
= 0, update_q
= 0;
1401 set_bit(STRIPE_HANDLE
, &sh
->state
);
1404 BUG_ON(uptodate
< disks
);
1405 /* Want to check and possibly repair P and Q.
1406 * However there could be one 'failed' device, in which
1407 * case we can only check one of them, possibly using the
1408 * other to generate missing data
1411 /* If !tmp_page, we cannot do the calculations,
1412 * but as we have set STRIPE_HANDLE, we will soon be called
1413 * by stripe_handle with a tmp_page - just wait until then.
1416 if (failed
== q_failed
) {
1417 /* The only possible failed device holds 'Q', so it makes
1418 * sense to check P (If anything else were failed, we would
1419 * have used P to recreate it).
1421 compute_block_1(sh
, pd_idx
, 1);
1422 if (!page_is_zero(sh
->dev
[pd_idx
].page
)) {
1423 compute_block_1(sh
,pd_idx
,0);
1427 if (!q_failed
&& failed
< 2) {
1428 /* q is not failed, and we didn't use it to generate
1429 * anything, so it makes sense to check it
1431 memcpy(page_address(tmp_page
),
1432 page_address(sh
->dev
[qd_idx
].page
),
1434 compute_parity(sh
, UPDATE_PARITY
);
1435 if (memcmp(page_address(tmp_page
),
1436 page_address(sh
->dev
[qd_idx
].page
),
1438 clear_bit(STRIPE_INSYNC
, &sh
->state
);
1442 if (update_p
|| update_q
) {
1443 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
1444 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
1445 /* don't try to repair!! */
1446 update_p
= update_q
= 0;
1449 /* now write out any block on a failed drive,
1450 * or P or Q if they need it
1454 dev
= &sh
->dev
[failed_num
[1]];
1456 set_bit(R5_LOCKED
, &dev
->flags
);
1457 set_bit(R5_Wantwrite
, &dev
->flags
);
1460 dev
= &sh
->dev
[failed_num
[0]];
1462 set_bit(R5_LOCKED
, &dev
->flags
);
1463 set_bit(R5_Wantwrite
, &dev
->flags
);
1467 dev
= &sh
->dev
[pd_idx
];
1469 set_bit(R5_LOCKED
, &dev
->flags
);
1470 set_bit(R5_Wantwrite
, &dev
->flags
);
1473 dev
= &sh
->dev
[qd_idx
];
1475 set_bit(R5_LOCKED
, &dev
->flags
);
1476 set_bit(R5_Wantwrite
, &dev
->flags
);
1478 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
1480 set_bit(STRIPE_INSYNC
, &sh
->state
);
1484 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1485 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
1486 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1489 /* If the failed drives are just a ReadError, then we might need
1490 * to progress the repair/check process
1492 if (failed
<= 2 && ! conf
->mddev
->ro
)
1493 for (i
=0; i
<failed
;i
++) {
1494 dev
= &sh
->dev
[failed_num
[i
]];
1495 if (test_bit(R5_ReadError
, &dev
->flags
)
1496 && !test_bit(R5_LOCKED
, &dev
->flags
)
1497 && test_bit(R5_UPTODATE
, &dev
->flags
)
1499 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
1500 set_bit(R5_Wantwrite
, &dev
->flags
);
1501 set_bit(R5_ReWrite
, &dev
->flags
);
1502 set_bit(R5_LOCKED
, &dev
->flags
);
1504 /* let's read it back */
1505 set_bit(R5_Wantread
, &dev
->flags
);
1506 set_bit(R5_LOCKED
, &dev
->flags
);
1510 spin_unlock(&sh
->lock
);
1512 while ((bi
=return_bi
)) {
1513 int bytes
= bi
->bi_size
;
1515 return_bi
= bi
->bi_next
;
1518 bi
->bi_end_io(bi
, bytes
, 0);
1520 for (i
=disks
; i
-- ;) {
1524 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
1526 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
1531 bi
= &sh
->dev
[i
].req
;
1535 bi
->bi_end_io
= raid6_end_write_request
;
1537 bi
->bi_end_io
= raid6_end_read_request
;
1540 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1541 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
1544 atomic_inc(&rdev
->nr_pending
);
1549 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
1551 bi
->bi_bdev
= rdev
->bdev
;
1552 PRINTK("for %llu schedule op %ld on disc %d\n",
1553 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
1554 atomic_inc(&sh
->count
);
1555 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
1556 bi
->bi_flags
= 1 << BIO_UPTODATE
;
1558 bi
->bi_max_vecs
= 1;
1560 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
1561 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1562 bi
->bi_io_vec
[0].bv_offset
= 0;
1563 bi
->bi_size
= STRIPE_SIZE
;
1566 test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1567 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1568 generic_make_request(bi
);
1571 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1572 PRINTK("skip op %ld on disc %d for sector %llu\n",
1573 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
1574 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1575 set_bit(STRIPE_HANDLE
, &sh
->state
);
1580 static void raid6_activate_delayed(raid6_conf_t
*conf
)
1582 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
1583 while (!list_empty(&conf
->delayed_list
)) {
1584 struct list_head
*l
= conf
->delayed_list
.next
;
1585 struct stripe_head
*sh
;
1586 sh
= list_entry(l
, struct stripe_head
, lru
);
1588 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1589 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1590 atomic_inc(&conf
->preread_active_stripes
);
1591 list_add_tail(&sh
->lru
, &conf
->handle_list
);
1596 static void activate_bit_delay(raid6_conf_t
*conf
)
1598 /* device_lock is held */
1599 struct list_head head
;
1600 list_add(&head
, &conf
->bitmap_list
);
1601 list_del_init(&conf
->bitmap_list
);
1602 while (!list_empty(&head
)) {
1603 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
1604 list_del_init(&sh
->lru
);
1605 atomic_inc(&sh
->count
);
1606 __release_stripe(conf
, sh
);
1610 static void unplug_slaves(mddev_t
*mddev
)
1612 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1616 for (i
=0; i
<mddev
->raid_disks
; i
++) {
1617 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1618 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
1619 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
1621 atomic_inc(&rdev
->nr_pending
);
1624 if (r_queue
->unplug_fn
)
1625 r_queue
->unplug_fn(r_queue
);
1627 rdev_dec_pending(rdev
, mddev
);
1634 static void raid6_unplug_device(request_queue_t
*q
)
1636 mddev_t
*mddev
= q
->queuedata
;
1637 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1638 unsigned long flags
;
1640 spin_lock_irqsave(&conf
->device_lock
, flags
);
1642 if (blk_remove_plug(q
)) {
1644 raid6_activate_delayed(conf
);
1646 md_wakeup_thread(mddev
->thread
);
1648 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1650 unplug_slaves(mddev
);
1653 static int raid6_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
1654 sector_t
*error_sector
)
1656 mddev_t
*mddev
= q
->queuedata
;
1657 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1661 for (i
=0; i
<mddev
->raid_disks
&& ret
== 0; i
++) {
1662 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1663 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
1664 struct block_device
*bdev
= rdev
->bdev
;
1665 request_queue_t
*r_queue
= bdev_get_queue(bdev
);
1667 if (!r_queue
->issue_flush_fn
)
1670 atomic_inc(&rdev
->nr_pending
);
1672 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
1674 rdev_dec_pending(rdev
, mddev
);
1683 static inline void raid6_plug_device(raid6_conf_t
*conf
)
1685 spin_lock_irq(&conf
->device_lock
);
1686 blk_plug_device(conf
->mddev
->queue
);
1687 spin_unlock_irq(&conf
->device_lock
);
1690 static int make_request (request_queue_t
*q
, struct bio
* bi
)
1692 mddev_t
*mddev
= q
->queuedata
;
1693 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1694 const unsigned int raid_disks
= conf
->raid_disks
;
1695 const unsigned int data_disks
= raid_disks
- 2;
1696 unsigned int dd_idx
, pd_idx
;
1697 sector_t new_sector
;
1698 sector_t logical_sector
, last_sector
;
1699 struct stripe_head
*sh
;
1700 const int rw
= bio_data_dir(bi
);
1702 if (unlikely(bio_barrier(bi
))) {
1703 bio_endio(bi
, bi
->bi_size
, -EOPNOTSUPP
);
1707 md_write_start(mddev
, bi
);
1709 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
1710 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bi
));
1712 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
1713 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1716 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
1718 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
1721 new_sector
= raid6_compute_sector(logical_sector
,
1722 raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1724 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1725 (unsigned long long)new_sector
,
1726 (unsigned long long)logical_sector
);
1729 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
1730 sh
= get_active_stripe(conf
, new_sector
, pd_idx
, (bi
->bi_rw
&RWA_MASK
));
1732 if (!add_stripe_bio(sh
, bi
, dd_idx
, (bi
->bi_rw
&RW_MASK
))) {
1733 /* Add failed due to overlap. Flush everything
1736 raid6_unplug_device(mddev
->queue
);
1741 finish_wait(&conf
->wait_for_overlap
, &w
);
1742 raid6_plug_device(conf
);
1743 handle_stripe(sh
, NULL
);
1746 /* cannot get stripe for read-ahead, just give-up */
1747 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1748 finish_wait(&conf
->wait_for_overlap
, &w
);
1753 spin_lock_irq(&conf
->device_lock
);
1754 if (--bi
->bi_phys_segments
== 0) {
1755 int bytes
= bi
->bi_size
;
1758 md_write_end(mddev
);
1760 bi
->bi_end_io(bi
, bytes
, 0);
1762 spin_unlock_irq(&conf
->device_lock
);
1766 /* FIXME go_faster isn't used */
1767 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1769 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
1770 struct stripe_head
*sh
;
1771 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1773 unsigned long stripe
;
1776 sector_t first_sector
;
1777 int raid_disks
= conf
->raid_disks
;
1778 int data_disks
= raid_disks
- 2;
1779 sector_t max_sector
= mddev
->size
<< 1;
1781 int still_degraded
= 0;
1784 if (sector_nr
>= max_sector
) {
1785 /* just being told to finish up .. nothing much to do */
1786 unplug_slaves(mddev
);
1788 if (mddev
->curr_resync
< max_sector
) /* aborted */
1789 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1791 else /* completed sync */
1793 bitmap_close_sync(mddev
->bitmap
);
1797 /* if there are 2 or more failed drives and we are trying
1798 * to resync, then assert that we are finished, because there is
1799 * nothing we can do.
1801 if (mddev
->degraded
>= 2 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1802 sector_t rv
= (mddev
->size
<< 1) - sector_nr
;
1806 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
1807 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
1808 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
1809 /* we can skip this block, and probably more */
1810 sync_blocks
/= STRIPE_SECTORS
;
1812 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
1816 chunk_offset
= sector_div(x
, sectors_per_chunk
);
1818 BUG_ON(x
!= stripe
);
1820 first_sector
= raid6_compute_sector((sector_t
)stripe
*data_disks
*sectors_per_chunk
1821 + chunk_offset
, raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1822 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 1);
1824 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 0);
1825 /* make sure we don't swamp the stripe cache if someone else
1826 * is trying to get access
1828 schedule_timeout_uninterruptible(1);
1830 /* Need to check if array will still be degraded after recovery/resync
1831 * We don't need to check the 'failed' flag as when that gets set,
1834 for (i
=0; i
<mddev
->raid_disks
; i
++)
1835 if (conf
->disks
[i
].rdev
== NULL
)
1838 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
1840 spin_lock(&sh
->lock
);
1841 set_bit(STRIPE_SYNCING
, &sh
->state
);
1842 clear_bit(STRIPE_INSYNC
, &sh
->state
);
1843 spin_unlock(&sh
->lock
);
1845 handle_stripe(sh
, NULL
);
1848 return STRIPE_SECTORS
;
1852 * This is our raid6 kernel thread.
1854 * We scan the hash table for stripes which can be handled now.
1855 * During the scan, completed stripes are saved for us by the interrupt
1856 * handler, so that they will not have to wait for our next wakeup.
1858 static void raid6d (mddev_t
*mddev
)
1860 struct stripe_head
*sh
;
1861 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1864 PRINTK("+++ raid6d active\n");
1866 md_check_recovery(mddev
);
1869 spin_lock_irq(&conf
->device_lock
);
1871 struct list_head
*first
;
1873 if (conf
->seq_flush
- conf
->seq_write
> 0) {
1874 int seq
= conf
->seq_flush
;
1875 spin_unlock_irq(&conf
->device_lock
);
1876 bitmap_unplug(mddev
->bitmap
);
1877 spin_lock_irq(&conf
->device_lock
);
1878 conf
->seq_write
= seq
;
1879 activate_bit_delay(conf
);
1882 if (list_empty(&conf
->handle_list
) &&
1883 atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
&&
1884 !blk_queue_plugged(mddev
->queue
) &&
1885 !list_empty(&conf
->delayed_list
))
1886 raid6_activate_delayed(conf
);
1888 if (list_empty(&conf
->handle_list
))
1891 first
= conf
->handle_list
.next
;
1892 sh
= list_entry(first
, struct stripe_head
, lru
);
1894 list_del_init(first
);
1895 atomic_inc(&sh
->count
);
1896 if (atomic_read(&sh
->count
)!= 1)
1898 spin_unlock_irq(&conf
->device_lock
);
1901 handle_stripe(sh
, conf
->spare_page
);
1904 spin_lock_irq(&conf
->device_lock
);
1906 PRINTK("%d stripes handled\n", handled
);
1908 spin_unlock_irq(&conf
->device_lock
);
1910 unplug_slaves(mddev
);
1912 PRINTK("--- raid6d inactive\n");
1915 static int run(mddev_t
*mddev
)
1918 int raid_disk
, memory
;
1920 struct disk_info
*disk
;
1921 struct list_head
*tmp
;
1923 if (mddev
->level
!= 6) {
1924 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev
), mddev
->level
);
1928 mddev
->private = kzalloc(sizeof (raid6_conf_t
)
1929 + mddev
->raid_disks
* sizeof(struct disk_info
),
1931 if ((conf
= mddev
->private) == NULL
)
1933 conf
->mddev
= mddev
;
1935 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
1938 conf
->spare_page
= alloc_page(GFP_KERNEL
);
1939 if (!conf
->spare_page
)
1942 spin_lock_init(&conf
->device_lock
);
1943 init_waitqueue_head(&conf
->wait_for_stripe
);
1944 init_waitqueue_head(&conf
->wait_for_overlap
);
1945 INIT_LIST_HEAD(&conf
->handle_list
);
1946 INIT_LIST_HEAD(&conf
->delayed_list
);
1947 INIT_LIST_HEAD(&conf
->bitmap_list
);
1948 INIT_LIST_HEAD(&conf
->inactive_list
);
1949 atomic_set(&conf
->active_stripes
, 0);
1950 atomic_set(&conf
->preread_active_stripes
, 0);
1952 PRINTK("raid6: run(%s) called.\n", mdname(mddev
));
1954 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1955 raid_disk
= rdev
->raid_disk
;
1956 if (raid_disk
>= mddev
->raid_disks
1959 disk
= conf
->disks
+ raid_disk
;
1963 if (test_bit(In_sync
, &rdev
->flags
)) {
1964 char b
[BDEVNAME_SIZE
];
1965 printk(KERN_INFO
"raid6: device %s operational as raid"
1966 " disk %d\n", bdevname(rdev
->bdev
,b
),
1968 conf
->working_disks
++;
1972 conf
->raid_disks
= mddev
->raid_disks
;
1975 * 0 for a fully functional array, 1 or 2 for a degraded array.
1977 mddev
->degraded
= conf
->failed_disks
= conf
->raid_disks
- conf
->working_disks
;
1978 conf
->mddev
= mddev
;
1979 conf
->chunk_size
= mddev
->chunk_size
;
1980 conf
->level
= mddev
->level
;
1981 conf
->algorithm
= mddev
->layout
;
1982 conf
->max_nr_stripes
= NR_STRIPES
;
1984 /* device size must be a multiple of chunk size */
1985 mddev
->size
&= ~(mddev
->chunk_size
/1024 -1);
1986 mddev
->resync_max_sectors
= mddev
->size
<< 1;
1988 if (conf
->raid_disks
< 4) {
1989 printk(KERN_ERR
"raid6: not enough configured devices for %s (%d, minimum 4)\n",
1990 mdname(mddev
), conf
->raid_disks
);
1993 if (!conf
->chunk_size
|| conf
->chunk_size
% 4) {
1994 printk(KERN_ERR
"raid6: invalid chunk size %d for %s\n",
1995 conf
->chunk_size
, mdname(mddev
));
1998 if (conf
->algorithm
> ALGORITHM_RIGHT_SYMMETRIC
) {
2000 "raid6: unsupported parity algorithm %d for %s\n",
2001 conf
->algorithm
, mdname(mddev
));
2004 if (mddev
->degraded
> 2) {
2005 printk(KERN_ERR
"raid6: not enough operational devices for %s"
2006 " (%d/%d failed)\n",
2007 mdname(mddev
), conf
->failed_disks
, conf
->raid_disks
);
2011 if (mddev
->degraded
> 0 &&
2012 mddev
->recovery_cp
!= MaxSector
) {
2013 if (mddev
->ok_start_degraded
)
2014 printk(KERN_WARNING
"raid6: starting dirty degraded array:%s"
2015 "- data corruption possible.\n",
2018 printk(KERN_ERR
"raid6: cannot start dirty degraded array"
2019 " for %s\n", mdname(mddev
));
2025 mddev
->thread
= md_register_thread(raid6d
, mddev
, "%s_raid6");
2026 if (!mddev
->thread
) {
2028 "raid6: couldn't allocate thread for %s\n",
2034 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
2035 conf
->raid_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
2036 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
2038 "raid6: couldn't allocate %dkB for buffers\n", memory
);
2039 shrink_stripes(conf
);
2040 md_unregister_thread(mddev
->thread
);
2043 printk(KERN_INFO
"raid6: allocated %dkB for %s\n",
2044 memory
, mdname(mddev
));
2046 if (mddev
->degraded
== 0)
2047 printk(KERN_INFO
"raid6: raid level %d set %s active with %d out of %d"
2048 " devices, algorithm %d\n", conf
->level
, mdname(mddev
),
2049 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
2052 printk(KERN_ALERT
"raid6: raid level %d set %s active with %d"
2053 " out of %d devices, algorithm %d\n", conf
->level
,
2054 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2055 mddev
->raid_disks
, conf
->algorithm
);
2057 print_raid6_conf(conf
);
2059 /* read-ahead size must cover two whole stripes, which is
2060 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
2063 int stripe
= (mddev
->raid_disks
-2) * mddev
->chunk_size
2065 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
2066 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
2069 /* Ok, everything is just fine now */
2070 mddev
->array_size
= mddev
->size
* (mddev
->raid_disks
- 2);
2072 mddev
->queue
->unplug_fn
= raid6_unplug_device
;
2073 mddev
->queue
->issue_flush_fn
= raid6_issue_flush
;
2077 print_raid6_conf(conf
);
2078 safe_put_page(conf
->spare_page
);
2079 kfree(conf
->stripe_hashtbl
);
2082 mddev
->private = NULL
;
2083 printk(KERN_ALERT
"raid6: failed to run raid set %s\n", mdname(mddev
));
2089 static int stop (mddev_t
*mddev
)
2091 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
2093 md_unregister_thread(mddev
->thread
);
2094 mddev
->thread
= NULL
;
2095 shrink_stripes(conf
);
2096 kfree(conf
->stripe_hashtbl
);
2097 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2099 mddev
->private = NULL
;
2104 static void print_sh (struct seq_file
*seq
, struct stripe_head
*sh
)
2108 seq_printf(seq
, "sh %llu, pd_idx %d, state %ld.\n",
2109 (unsigned long long)sh
->sector
, sh
->pd_idx
, sh
->state
);
2110 seq_printf(seq
, "sh %llu, count %d.\n",
2111 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
));
2112 seq_printf(seq
, "sh %llu, ", (unsigned long long)sh
->sector
);
2113 for (i
= 0; i
< sh
->raid_conf
->raid_disks
; i
++) {
2114 seq_printf(seq
, "(cache%d: %p %ld) ",
2115 i
, sh
->dev
[i
].page
, sh
->dev
[i
].flags
);
2117 seq_printf(seq
, "\n");
2120 static void printall (struct seq_file
*seq
, raid6_conf_t
*conf
)
2122 struct stripe_head
*sh
;
2123 struct hlist_node
*hn
;
2126 spin_lock_irq(&conf
->device_lock
);
2127 for (i
= 0; i
< NR_HASH
; i
++) {
2128 sh
= conf
->stripe_hashtbl
[i
];
2129 hlist_for_each_entry(sh
, hn
, &conf
->stripe_hashtbl
[i
], hash
) {
2130 if (sh
->raid_conf
!= conf
)
2135 spin_unlock_irq(&conf
->device_lock
);
2139 static void status (struct seq_file
*seq
, mddev_t
*mddev
)
2141 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
2144 seq_printf (seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
, mddev
->chunk_size
>> 10, mddev
->layout
);
2145 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->working_disks
);
2146 for (i
= 0; i
< conf
->raid_disks
; i
++)
2147 seq_printf (seq
, "%s",
2148 conf
->disks
[i
].rdev
&&
2149 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
2150 seq_printf (seq
, "]");
2152 seq_printf (seq
, "\n");
2153 printall(seq
, conf
);
2157 static void print_raid6_conf (raid6_conf_t
*conf
)
2160 struct disk_info
*tmp
;
2162 printk("RAID6 conf printout:\n");
2164 printk("(conf==NULL)\n");
2167 printk(" --- rd:%d wd:%d fd:%d\n", conf
->raid_disks
,
2168 conf
->working_disks
, conf
->failed_disks
);
2170 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2171 char b
[BDEVNAME_SIZE
];
2172 tmp
= conf
->disks
+ i
;
2174 printk(" disk %d, o:%d, dev:%s\n",
2175 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
2176 bdevname(tmp
->rdev
->bdev
,b
));
2180 static int raid6_spare_active(mddev_t
*mddev
)
2183 raid6_conf_t
*conf
= mddev
->private;
2184 struct disk_info
*tmp
;
2186 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2187 tmp
= conf
->disks
+ i
;
2189 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
2190 && !test_bit(In_sync
, &tmp
->rdev
->flags
)) {
2192 conf
->failed_disks
--;
2193 conf
->working_disks
++;
2194 set_bit(In_sync
, &tmp
->rdev
->flags
);
2197 print_raid6_conf(conf
);
2201 static int raid6_remove_disk(mddev_t
*mddev
, int number
)
2203 raid6_conf_t
*conf
= mddev
->private;
2206 struct disk_info
*p
= conf
->disks
+ number
;
2208 print_raid6_conf(conf
);
2211 if (test_bit(In_sync
, &rdev
->flags
) ||
2212 atomic_read(&rdev
->nr_pending
)) {
2218 if (atomic_read(&rdev
->nr_pending
)) {
2219 /* lost the race, try later */
2227 print_raid6_conf(conf
);
2231 static int raid6_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
2233 raid6_conf_t
*conf
= mddev
->private;
2236 struct disk_info
*p
;
2238 if (mddev
->degraded
> 2)
2239 /* no point adding a device */
2242 * find the disk ... but prefer rdev->saved_raid_disk
2245 if (rdev
->saved_raid_disk
>= 0 &&
2246 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
2247 disk
= rdev
->saved_raid_disk
;
2250 for ( ; disk
< mddev
->raid_disks
; disk
++)
2251 if ((p
=conf
->disks
+ disk
)->rdev
== NULL
) {
2252 clear_bit(In_sync
, &rdev
->flags
);
2253 rdev
->raid_disk
= disk
;
2255 if (rdev
->saved_raid_disk
!= disk
)
2257 rcu_assign_pointer(p
->rdev
, rdev
);
2260 print_raid6_conf(conf
);
2264 static int raid6_resize(mddev_t
*mddev
, sector_t sectors
)
2266 /* no resync is happening, and there is enough space
2267 * on all devices, so we can resize.
2268 * We need to make sure resync covers any new space.
2269 * If the array is shrinking we should possibly wait until
2270 * any io in the removed space completes, but it hardly seems
2273 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
2274 mddev
->array_size
= (sectors
* (mddev
->raid_disks
-2))>>1;
2275 set_capacity(mddev
->gendisk
, mddev
->array_size
<< 1);
2277 if (sectors
/2 > mddev
->size
&& mddev
->recovery_cp
== MaxSector
) {
2278 mddev
->recovery_cp
= mddev
->size
<< 1;
2279 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2281 mddev
->size
= sectors
/2;
2282 mddev
->resync_max_sectors
= sectors
;
2286 static void raid6_quiesce(mddev_t
*mddev
, int state
)
2288 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
2291 case 1: /* stop all writes */
2292 spin_lock_irq(&conf
->device_lock
);
2294 wait_event_lock_irq(conf
->wait_for_stripe
,
2295 atomic_read(&conf
->active_stripes
) == 0,
2296 conf
->device_lock
, /* nothing */);
2297 spin_unlock_irq(&conf
->device_lock
);
2300 case 0: /* re-enable writes */
2301 spin_lock_irq(&conf
->device_lock
);
2303 wake_up(&conf
->wait_for_stripe
);
2304 spin_unlock_irq(&conf
->device_lock
);
2309 static struct mdk_personality raid6_personality
=
2313 .owner
= THIS_MODULE
,
2314 .make_request
= make_request
,
2318 .error_handler
= error
,
2319 .hot_add_disk
= raid6_add_disk
,
2320 .hot_remove_disk
= raid6_remove_disk
,
2321 .spare_active
= raid6_spare_active
,
2322 .sync_request
= sync_request
,
2323 .resize
= raid6_resize
,
2324 .quiesce
= raid6_quiesce
,
2327 static int __init
raid6_init(void)
2331 e
= raid6_select_algo();
2335 return register_md_personality(&raid6_personality
);
2338 static void raid6_exit (void)
2340 unregister_md_personality(&raid6_personality
);
2343 module_init(raid6_init
);
2344 module_exit(raid6_exit
);
2345 MODULE_LICENSE("GPL");
2346 MODULE_ALIAS("md-personality-8"); /* RAID6 */
2347 MODULE_ALIAS("md-raid6");
2348 MODULE_ALIAS("md-level-6");