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 BUG_ON(!list_empty(&sh
->lru
));
95 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
96 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
97 if (test_bit(STRIPE_DELAYED
, &sh
->state
))
98 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
99 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
100 conf
->seq_write
== sh
->bm_seq
)
101 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
103 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
104 list_add_tail(&sh
->lru
, &conf
->handle_list
);
106 md_wakeup_thread(conf
->mddev
->thread
);
108 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
109 atomic_dec(&conf
->preread_active_stripes
);
110 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
111 md_wakeup_thread(conf
->mddev
->thread
);
113 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
114 atomic_dec(&conf
->active_stripes
);
115 if (!conf
->inactive_blocked
||
116 atomic_read(&conf
->active_stripes
) < (conf
->max_nr_stripes
*3/4))
117 wake_up(&conf
->wait_for_stripe
);
121 static void release_stripe(struct stripe_head
*sh
)
123 raid6_conf_t
*conf
= sh
->raid_conf
;
126 spin_lock_irqsave(&conf
->device_lock
, flags
);
127 __release_stripe(conf
, sh
);
128 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
131 static inline void remove_hash(struct stripe_head
*sh
)
133 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
135 hlist_del_init(&sh
->hash
);
138 static inline void insert_hash(raid6_conf_t
*conf
, struct stripe_head
*sh
)
140 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
142 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
145 hlist_add_head(&sh
->hash
, hp
);
149 /* find an idle stripe, make sure it is unhashed, and return it. */
150 static struct stripe_head
*get_free_stripe(raid6_conf_t
*conf
)
152 struct stripe_head
*sh
= NULL
;
153 struct list_head
*first
;
156 if (list_empty(&conf
->inactive_list
))
158 first
= conf
->inactive_list
.next
;
159 sh
= list_entry(first
, struct stripe_head
, lru
);
160 list_del_init(first
);
162 atomic_inc(&conf
->active_stripes
);
167 static void shrink_buffers(struct stripe_head
*sh
, int num
)
172 for (i
=0; i
<num
; i
++) {
176 sh
->dev
[i
].page
= NULL
;
181 static int grow_buffers(struct stripe_head
*sh
, int num
)
185 for (i
=0; i
<num
; i
++) {
188 if (!(page
= alloc_page(GFP_KERNEL
))) {
191 sh
->dev
[i
].page
= page
;
196 static void raid6_build_block (struct stripe_head
*sh
, int i
);
198 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int pd_idx
)
200 raid6_conf_t
*conf
= sh
->raid_conf
;
201 int disks
= conf
->raid_disks
, i
;
203 BUG_ON(atomic_read(&sh
->count
) != 0);
204 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
207 PRINTK("init_stripe called, stripe %llu\n",
208 (unsigned long long)sh
->sector
);
216 for (i
=disks
; i
--; ) {
217 struct r5dev
*dev
= &sh
->dev
[i
];
219 if (dev
->toread
|| dev
->towrite
|| dev
->written
||
220 test_bit(R5_LOCKED
, &dev
->flags
)) {
221 PRINTK("sector=%llx i=%d %p %p %p %d\n",
222 (unsigned long long)sh
->sector
, i
, dev
->toread
,
223 dev
->towrite
, dev
->written
,
224 test_bit(R5_LOCKED
, &dev
->flags
));
228 raid6_build_block(sh
, i
);
230 insert_hash(conf
, sh
);
233 static struct stripe_head
*__find_stripe(raid6_conf_t
*conf
, sector_t sector
)
235 struct stripe_head
*sh
;
236 struct hlist_node
*hn
;
239 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector
);
240 hlist_for_each_entry (sh
, hn
, stripe_hash(conf
, sector
), hash
)
241 if (sh
->sector
== sector
)
243 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector
);
247 static void unplug_slaves(mddev_t
*mddev
);
249 static struct stripe_head
*get_active_stripe(raid6_conf_t
*conf
, sector_t sector
,
250 int pd_idx
, int noblock
)
252 struct stripe_head
*sh
;
254 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector
);
256 spin_lock_irq(&conf
->device_lock
);
259 wait_event_lock_irq(conf
->wait_for_stripe
,
261 conf
->device_lock
, /* nothing */);
262 sh
= __find_stripe(conf
, sector
);
264 if (!conf
->inactive_blocked
)
265 sh
= get_free_stripe(conf
);
266 if (noblock
&& sh
== NULL
)
269 conf
->inactive_blocked
= 1;
270 wait_event_lock_irq(conf
->wait_for_stripe
,
271 !list_empty(&conf
->inactive_list
) &&
272 (atomic_read(&conf
->active_stripes
)
273 < (conf
->max_nr_stripes
*3/4)
274 || !conf
->inactive_blocked
),
276 unplug_slaves(conf
->mddev
);
278 conf
->inactive_blocked
= 0;
280 init_stripe(sh
, sector
, pd_idx
);
282 if (atomic_read(&sh
->count
)) {
283 BUG_ON(!list_empty(&sh
->lru
));
285 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
286 atomic_inc(&conf
->active_stripes
);
287 BUG_ON(list_empty(&sh
->lru
));
288 list_del_init(&sh
->lru
);
291 } while (sh
== NULL
);
294 atomic_inc(&sh
->count
);
296 spin_unlock_irq(&conf
->device_lock
);
300 static int grow_one_stripe(raid6_conf_t
*conf
)
302 struct stripe_head
*sh
;
303 sh
= kmem_cache_alloc(conf
->slab_cache
, GFP_KERNEL
);
306 memset(sh
, 0, sizeof(*sh
) + (conf
->raid_disks
-1)*sizeof(struct r5dev
));
307 sh
->raid_conf
= conf
;
308 spin_lock_init(&sh
->lock
);
310 if (grow_buffers(sh
, conf
->raid_disks
)) {
311 shrink_buffers(sh
, conf
->raid_disks
);
312 kmem_cache_free(conf
->slab_cache
, sh
);
315 /* we just created an active stripe so... */
316 atomic_set(&sh
->count
, 1);
317 atomic_inc(&conf
->active_stripes
);
318 INIT_LIST_HEAD(&sh
->lru
);
323 static int grow_stripes(raid6_conf_t
*conf
, int num
)
326 int devs
= conf
->raid_disks
;
328 sprintf(conf
->cache_name
[0], "raid6/%s", mdname(conf
->mddev
));
330 sc
= kmem_cache_create(conf
->cache_name
[0],
331 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
335 conf
->slab_cache
= sc
;
337 if (!grow_one_stripe(conf
))
342 static int drop_one_stripe(raid6_conf_t
*conf
)
344 struct stripe_head
*sh
;
345 spin_lock_irq(&conf
->device_lock
);
346 sh
= get_free_stripe(conf
);
347 spin_unlock_irq(&conf
->device_lock
);
350 BUG_ON(atomic_read(&sh
->count
));
351 shrink_buffers(sh
, conf
->raid_disks
);
352 kmem_cache_free(conf
->slab_cache
, sh
);
353 atomic_dec(&conf
->active_stripes
);
357 static void shrink_stripes(raid6_conf_t
*conf
)
359 while (drop_one_stripe(conf
))
362 if (conf
->slab_cache
)
363 kmem_cache_destroy(conf
->slab_cache
);
364 conf
->slab_cache
= NULL
;
367 static int raid6_end_read_request(struct bio
* bi
, unsigned int bytes_done
,
370 struct stripe_head
*sh
= bi
->bi_private
;
371 raid6_conf_t
*conf
= sh
->raid_conf
;
372 int disks
= conf
->raid_disks
, i
;
373 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
378 for (i
=0 ; i
<disks
; i
++)
379 if (bi
== &sh
->dev
[i
].req
)
382 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
383 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
394 spin_lock_irqsave(&conf
->device_lock
, flags
);
395 /* we can return a buffer if we bypassed the cache or
396 * if the top buffer is not in highmem. If there are
397 * multiple buffers, leave the extra work to
400 buffer
= sh
->bh_read
[i
];
402 (!PageHighMem(buffer
->b_page
)
403 || buffer
->b_page
== bh
->b_page
)
405 sh
->bh_read
[i
] = buffer
->b_reqnext
;
406 buffer
->b_reqnext
= NULL
;
409 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
410 if (sh
->bh_page
[i
]==bh
->b_page
)
411 set_buffer_uptodate(bh
);
413 if (buffer
->b_page
!= bh
->b_page
)
414 memcpy(buffer
->b_data
, bh
->b_data
, bh
->b_size
);
415 buffer
->b_end_io(buffer
, 1);
418 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
420 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
421 printk(KERN_INFO
"raid6: read error corrected!!\n");
422 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
423 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
425 if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
))
426 atomic_set(&conf
->disks
[i
].rdev
->read_errors
, 0);
429 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
430 atomic_inc(&conf
->disks
[i
].rdev
->read_errors
);
431 if (conf
->mddev
->degraded
)
432 printk(KERN_WARNING
"raid6: read error not correctable.\n");
433 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
435 printk(KERN_WARNING
"raid6: read error NOT corrected!!\n");
436 else if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
)
437 > conf
->max_nr_stripes
)
439 "raid6: Too many read errors, failing device.\n");
443 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
445 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
446 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
447 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
450 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
452 /* must restore b_page before unlocking buffer... */
453 if (sh
->bh_page
[i
] != bh
->b_page
) {
454 bh
->b_page
= sh
->bh_page
[i
];
455 bh
->b_data
= page_address(bh
->b_page
);
456 clear_buffer_uptodate(bh
);
459 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
460 set_bit(STRIPE_HANDLE
, &sh
->state
);
465 static int raid6_end_write_request (struct bio
*bi
, unsigned int bytes_done
,
468 struct stripe_head
*sh
= bi
->bi_private
;
469 raid6_conf_t
*conf
= sh
->raid_conf
;
470 int disks
= conf
->raid_disks
, i
;
472 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
477 for (i
=0 ; i
<disks
; i
++)
478 if (bi
== &sh
->dev
[i
].req
)
481 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
482 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
489 spin_lock_irqsave(&conf
->device_lock
, flags
);
491 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
493 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
495 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
496 set_bit(STRIPE_HANDLE
, &sh
->state
);
497 __release_stripe(conf
, sh
);
498 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
503 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
);
505 static void raid6_build_block (struct stripe_head
*sh
, int i
)
507 struct r5dev
*dev
= &sh
->dev
[i
];
508 int pd_idx
= sh
->pd_idx
;
509 int qd_idx
= raid6_next_disk(pd_idx
, sh
->raid_conf
->raid_disks
);
512 dev
->req
.bi_io_vec
= &dev
->vec
;
514 dev
->req
.bi_max_vecs
++;
515 dev
->vec
.bv_page
= dev
->page
;
516 dev
->vec
.bv_len
= STRIPE_SIZE
;
517 dev
->vec
.bv_offset
= 0;
519 dev
->req
.bi_sector
= sh
->sector
;
520 dev
->req
.bi_private
= sh
;
523 if (i
!= pd_idx
&& i
!= qd_idx
)
524 dev
->sector
= compute_blocknr(sh
, i
);
527 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
529 char b
[BDEVNAME_SIZE
];
530 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
531 PRINTK("raid6: error called\n");
533 if (!test_bit(Faulty
, &rdev
->flags
)) {
535 if (test_bit(In_sync
, &rdev
->flags
)) {
536 conf
->working_disks
--;
538 conf
->failed_disks
++;
539 clear_bit(In_sync
, &rdev
->flags
);
541 * if recovery was running, make sure it aborts.
543 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
545 set_bit(Faulty
, &rdev
->flags
);
547 "raid6: Disk failure on %s, disabling device."
548 " Operation continuing on %d devices\n",
549 bdevname(rdev
->bdev
,b
), conf
->working_disks
);
554 * Input: a 'big' sector number,
555 * Output: index of the data and parity disk, and the sector # in them.
557 static sector_t
raid6_compute_sector(sector_t r_sector
, unsigned int raid_disks
,
558 unsigned int data_disks
, unsigned int * dd_idx
,
559 unsigned int * pd_idx
, raid6_conf_t
*conf
)
562 unsigned long chunk_number
;
563 unsigned int chunk_offset
;
565 int sectors_per_chunk
= conf
->chunk_size
>> 9;
567 /* First compute the information on this sector */
570 * Compute the chunk number and the sector offset inside the chunk
572 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
573 chunk_number
= r_sector
;
574 if ( r_sector
!= chunk_number
) {
575 printk(KERN_CRIT
"raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
576 (unsigned long long)r_sector
, (unsigned long)chunk_number
);
581 * Compute the stripe number
583 stripe
= chunk_number
/ data_disks
;
586 * Compute the data disk and parity disk indexes inside the stripe
588 *dd_idx
= chunk_number
% data_disks
;
591 * Select the parity disk based on the user selected algorithm.
595 switch (conf
->algorithm
) {
596 case ALGORITHM_LEFT_ASYMMETRIC
:
597 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
598 if (*pd_idx
== raid_disks
-1)
599 (*dd_idx
)++; /* Q D D D P */
600 else if (*dd_idx
>= *pd_idx
)
601 (*dd_idx
) += 2; /* D D P Q D */
603 case ALGORITHM_RIGHT_ASYMMETRIC
:
604 *pd_idx
= stripe
% raid_disks
;
605 if (*pd_idx
== raid_disks
-1)
606 (*dd_idx
)++; /* Q D D D P */
607 else if (*dd_idx
>= *pd_idx
)
608 (*dd_idx
) += 2; /* D D P Q D */
610 case ALGORITHM_LEFT_SYMMETRIC
:
611 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
612 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
614 case ALGORITHM_RIGHT_SYMMETRIC
:
615 *pd_idx
= stripe
% raid_disks
;
616 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
619 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
623 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
624 chunk_number
, *pd_idx
, *dd_idx
);
627 * Finally, compute the new sector number
629 new_sector
= (sector_t
) stripe
* sectors_per_chunk
+ chunk_offset
;
634 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
)
636 raid6_conf_t
*conf
= sh
->raid_conf
;
637 int raid_disks
= conf
->raid_disks
, data_disks
= raid_disks
- 2;
638 sector_t new_sector
= sh
->sector
, check
;
639 int sectors_per_chunk
= conf
->chunk_size
>> 9;
642 int chunk_number
, dummy1
, dummy2
, dd_idx
= i
;
646 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
648 if ( new_sector
!= stripe
) {
649 printk(KERN_CRIT
"raid6: ERROR: new_sector = %llu, stripe = %lu\n",
650 (unsigned long long)new_sector
, (unsigned long)stripe
);
654 switch (conf
->algorithm
) {
655 case ALGORITHM_LEFT_ASYMMETRIC
:
656 case ALGORITHM_RIGHT_ASYMMETRIC
:
657 if (sh
->pd_idx
== raid_disks
-1)
659 else if (i
> sh
->pd_idx
)
660 i
-= 2; /* D D P Q D */
662 case ALGORITHM_LEFT_SYMMETRIC
:
663 case ALGORITHM_RIGHT_SYMMETRIC
:
664 if (sh
->pd_idx
== raid_disks
-1)
670 i
-= (sh
->pd_idx
+ 2);
674 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
678 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh
->pd_idx
, i0
, i
);
680 chunk_number
= stripe
* data_disks
+ i
;
681 r_sector
= (sector_t
)chunk_number
* sectors_per_chunk
+ chunk_offset
;
683 check
= raid6_compute_sector (r_sector
, raid_disks
, data_disks
, &dummy1
, &dummy2
, conf
);
684 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| dummy2
!= sh
->pd_idx
) {
685 printk(KERN_CRIT
"raid6: compute_blocknr: map not correct\n");
694 * Copy data between a page in the stripe cache, and one or more bion
695 * The page could align with the middle of the bio, or there could be
696 * several bion, each with several bio_vecs, which cover part of the page
697 * Multiple bion are linked together on bi_next. There may be extras
698 * at the end of this list. We ignore them.
700 static void copy_data(int frombio
, struct bio
*bio
,
704 char *pa
= page_address(page
);
709 if (bio
->bi_sector
>= sector
)
710 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
712 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
713 bio_for_each_segment(bvl
, bio
, i
) {
714 int len
= bio_iovec_idx(bio
,i
)->bv_len
;
718 if (page_offset
< 0) {
719 b_offset
= -page_offset
;
720 page_offset
+= b_offset
;
724 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
725 clen
= STRIPE_SIZE
- page_offset
;
729 char *ba
= __bio_kmap_atomic(bio
, i
, KM_USER0
);
731 memcpy(pa
+page_offset
, ba
+b_offset
, clen
);
733 memcpy(ba
+b_offset
, pa
+page_offset
, clen
);
734 __bio_kunmap_atomic(ba
, KM_USER0
);
736 if (clen
< len
) /* hit end of page */
742 #define check_xor() do { \
743 if (count == MAX_XOR_BLOCKS) { \
744 xor_block(count, STRIPE_SIZE, ptr); \
749 /* Compute P and Q syndromes */
750 static void compute_parity(struct stripe_head
*sh
, int method
)
752 raid6_conf_t
*conf
= sh
->raid_conf
;
753 int i
, pd_idx
= sh
->pd_idx
, qd_idx
, d0_idx
, disks
= conf
->raid_disks
, count
;
755 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
758 qd_idx
= raid6_next_disk(pd_idx
, disks
);
759 d0_idx
= raid6_next_disk(qd_idx
, disks
);
761 PRINTK("compute_parity, stripe %llu, method %d\n",
762 (unsigned long long)sh
->sector
, method
);
765 case READ_MODIFY_WRITE
:
766 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
767 case RECONSTRUCT_WRITE
:
768 for (i
= disks
; i
-- ;)
769 if ( i
!= pd_idx
&& i
!= qd_idx
&& sh
->dev
[i
].towrite
) {
770 chosen
= sh
->dev
[i
].towrite
;
771 sh
->dev
[i
].towrite
= NULL
;
773 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
774 wake_up(&conf
->wait_for_overlap
);
776 BUG_ON(sh
->dev
[i
].written
);
777 sh
->dev
[i
].written
= chosen
;
781 BUG(); /* Not implemented yet */
784 for (i
= disks
; i
--;)
785 if (sh
->dev
[i
].written
) {
786 sector_t sector
= sh
->dev
[i
].sector
;
787 struct bio
*wbi
= sh
->dev
[i
].written
;
788 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
789 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
790 wbi
= r5_next_bio(wbi
, sector
);
793 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
794 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
798 // case RECONSTRUCT_WRITE:
799 // case CHECK_PARITY:
800 // case UPDATE_PARITY:
801 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
802 /* FIX: Is this ordering of drives even remotely optimal? */
806 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
807 if (count
<= disks
-2 && !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
808 printk("block %d/%d not uptodate on parity calc\n", i
,count
);
809 i
= raid6_next_disk(i
, disks
);
810 } while ( i
!= d0_idx
);
814 raid6_call
.gen_syndrome(disks
, STRIPE_SIZE
, ptrs
);
817 case RECONSTRUCT_WRITE
:
818 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
819 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
820 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
821 set_bit(R5_LOCKED
, &sh
->dev
[qd_idx
].flags
);
824 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
825 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
830 /* Compute one missing block */
831 static void compute_block_1(struct stripe_head
*sh
, int dd_idx
, int nozero
)
833 raid6_conf_t
*conf
= sh
->raid_conf
;
834 int i
, count
, disks
= conf
->raid_disks
;
835 void *ptr
[MAX_XOR_BLOCKS
], *p
;
836 int pd_idx
= sh
->pd_idx
;
837 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
839 PRINTK("compute_block_1, stripe %llu, idx %d\n",
840 (unsigned long long)sh
->sector
, dd_idx
);
842 if ( dd_idx
== qd_idx
) {
843 /* We're actually computing the Q drive */
844 compute_parity(sh
, UPDATE_PARITY
);
846 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
847 if (!nozero
) memset(ptr
[0], 0, STRIPE_SIZE
);
849 for (i
= disks
; i
--; ) {
850 if (i
== dd_idx
|| i
== qd_idx
)
852 p
= page_address(sh
->dev
[i
].page
);
853 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
856 printk("compute_block() %d, stripe %llu, %d"
857 " not present\n", dd_idx
,
858 (unsigned long long)sh
->sector
, i
);
863 xor_block(count
, STRIPE_SIZE
, ptr
);
864 if (!nozero
) set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
865 else clear_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
869 /* Compute two missing blocks */
870 static void compute_block_2(struct stripe_head
*sh
, int dd_idx1
, int dd_idx2
)
872 raid6_conf_t
*conf
= sh
->raid_conf
;
873 int i
, count
, disks
= conf
->raid_disks
;
874 int pd_idx
= sh
->pd_idx
;
875 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
876 int d0_idx
= raid6_next_disk(qd_idx
, disks
);
879 /* faila and failb are disk numbers relative to d0_idx */
880 /* pd_idx become disks-2 and qd_idx become disks-1 */
881 faila
= (dd_idx1
< d0_idx
) ? dd_idx1
+(disks
-d0_idx
) : dd_idx1
-d0_idx
;
882 failb
= (dd_idx2
< d0_idx
) ? dd_idx2
+(disks
-d0_idx
) : dd_idx2
-d0_idx
;
884 BUG_ON(faila
== failb
);
885 if ( failb
< faila
) { int tmp
= faila
; faila
= failb
; failb
= tmp
; }
887 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
888 (unsigned long long)sh
->sector
, dd_idx1
, dd_idx2
, faila
, failb
);
890 if ( failb
== disks
-1 ) {
891 /* Q disk is one of the missing disks */
892 if ( faila
== disks
-2 ) {
893 /* Missing P+Q, just recompute */
894 compute_parity(sh
, UPDATE_PARITY
);
897 /* We're missing D+Q; recompute D from P */
898 compute_block_1(sh
, (dd_idx1
== qd_idx
) ? dd_idx2
: dd_idx1
, 0);
899 compute_parity(sh
, UPDATE_PARITY
); /* Is this necessary? */
904 /* We're missing D+P or D+D; build pointer table */
906 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
912 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
913 i
= raid6_next_disk(i
, disks
);
914 if (i
!= dd_idx1
&& i
!= dd_idx2
&&
915 !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
916 printk("compute_2 with missing block %d/%d\n", count
, i
);
917 } while ( i
!= d0_idx
);
919 if ( failb
== disks
-2 ) {
920 /* We're missing D+P. */
921 raid6_datap_recov(disks
, STRIPE_SIZE
, faila
, ptrs
);
923 /* We're missing D+D. */
924 raid6_2data_recov(disks
, STRIPE_SIZE
, faila
, failb
, ptrs
);
927 /* Both the above update both missing blocks */
928 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx1
].flags
);
929 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx2
].flags
);
935 * Each stripe/dev can have one or more bion attached.
936 * toread/towrite point to the first in a chain.
937 * The bi_next chain must be in order.
939 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
942 raid6_conf_t
*conf
= sh
->raid_conf
;
945 PRINTK("adding bh b#%llu to stripe s#%llu\n",
946 (unsigned long long)bi
->bi_sector
,
947 (unsigned long long)sh
->sector
);
950 spin_lock(&sh
->lock
);
951 spin_lock_irq(&conf
->device_lock
);
953 bip
= &sh
->dev
[dd_idx
].towrite
;
954 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
957 bip
= &sh
->dev
[dd_idx
].toread
;
958 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
959 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
961 bip
= &(*bip
)->bi_next
;
963 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
966 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
970 bi
->bi_phys_segments
++;
971 spin_unlock_irq(&conf
->device_lock
);
972 spin_unlock(&sh
->lock
);
974 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
975 (unsigned long long)bi
->bi_sector
,
976 (unsigned long long)sh
->sector
, dd_idx
);
978 if (conf
->mddev
->bitmap
&& firstwrite
) {
979 sh
->bm_seq
= conf
->seq_write
;
980 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
982 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
986 /* check if page is covered */
987 sector_t sector
= sh
->dev
[dd_idx
].sector
;
988 for (bi
=sh
->dev
[dd_idx
].towrite
;
989 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
990 bi
&& bi
->bi_sector
<= sector
;
991 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
992 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
993 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
995 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
996 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
1001 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
1002 spin_unlock_irq(&conf
->device_lock
);
1003 spin_unlock(&sh
->lock
);
1008 static int page_is_zero(struct page
*p
)
1010 char *a
= page_address(p
);
1011 return ((*(u32
*)a
) == 0 &&
1012 memcmp(a
, a
+4, STRIPE_SIZE
-4)==0);
1015 * handle_stripe - do things to a stripe.
1017 * We lock the stripe and then examine the state of various bits
1018 * to see what needs to be done.
1020 * return some read request which now have data
1021 * return some write requests which are safely on disc
1022 * schedule a read on some buffers
1023 * schedule a write of some buffers
1024 * return confirmation of parity correctness
1026 * Parity calculations are done inside the stripe lock
1027 * buffers are taken off read_list or write_list, and bh_cache buffers
1028 * get BH_Lock set before the stripe lock is released.
1032 static void handle_stripe(struct stripe_head
*sh
, struct page
*tmp_page
)
1034 raid6_conf_t
*conf
= sh
->raid_conf
;
1035 int disks
= conf
->raid_disks
;
1036 struct bio
*return_bi
= NULL
;
1040 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
1041 int non_overwrite
= 0;
1042 int failed_num
[2] = {0, 0};
1043 struct r5dev
*dev
, *pdev
, *qdev
;
1044 int pd_idx
= sh
->pd_idx
;
1045 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1046 int p_failed
, q_failed
;
1048 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1049 (unsigned long long)sh
->sector
, sh
->state
, atomic_read(&sh
->count
),
1052 spin_lock(&sh
->lock
);
1053 clear_bit(STRIPE_HANDLE
, &sh
->state
);
1054 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1056 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
1057 /* Now to look around and see what can be done */
1060 for (i
=disks
; i
--; ) {
1063 clear_bit(R5_Insync
, &dev
->flags
);
1065 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1066 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
1067 /* maybe we can reply to a read */
1068 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
1069 struct bio
*rbi
, *rbi2
;
1070 PRINTK("Return read for disc %d\n", i
);
1071 spin_lock_irq(&conf
->device_lock
);
1074 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1075 wake_up(&conf
->wait_for_overlap
);
1076 spin_unlock_irq(&conf
->device_lock
);
1077 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1078 copy_data(0, rbi
, dev
->page
, dev
->sector
);
1079 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1080 spin_lock_irq(&conf
->device_lock
);
1081 if (--rbi
->bi_phys_segments
== 0) {
1082 rbi
->bi_next
= return_bi
;
1085 spin_unlock_irq(&conf
->device_lock
);
1090 /* now count some things */
1091 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
1092 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
1095 if (dev
->toread
) to_read
++;
1098 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1101 if (dev
->written
) written
++;
1102 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1103 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1104 /* The ReadError flag will just be confusing now */
1105 clear_bit(R5_ReadError
, &dev
->flags
);
1106 clear_bit(R5_ReWrite
, &dev
->flags
);
1108 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1109 || test_bit(R5_ReadError
, &dev
->flags
)) {
1111 failed_num
[failed
] = i
;
1114 set_bit(R5_Insync
, &dev
->flags
);
1117 PRINTK("locked=%d uptodate=%d to_read=%d"
1118 " to_write=%d failed=%d failed_num=%d,%d\n",
1119 locked
, uptodate
, to_read
, to_write
, failed
,
1120 failed_num
[0], failed_num
[1]);
1121 /* check if the array has lost >2 devices and, if so, some requests might
1124 if (failed
> 2 && to_read
+to_write
+written
) {
1125 for (i
=disks
; i
--; ) {
1128 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1131 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1132 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1133 /* multiple read failures in one stripe */
1134 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",
1313 set_bit(STRIPE_HANDLE
, &sh
->state
);
1316 /* now to consider writing and what else, if anything should be read */
1318 int rcw
=0, must_compute
=0;
1319 for (i
=disks
; i
--;) {
1321 /* Would I have to read this buffer for reconstruct_write */
1322 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
1323 && i
!= pd_idx
&& i
!= qd_idx
1324 && (!test_bit(R5_LOCKED
, &dev
->flags
)
1326 || sh
->bh_page
[i
] != bh
->b_page
1329 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1330 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
1332 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i
, dev
->flags
);
1337 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1338 (unsigned long long)sh
->sector
, rcw
, must_compute
);
1339 set_bit(STRIPE_HANDLE
, &sh
->state
);
1342 /* want reconstruct write, but need to get some data */
1343 for (i
=disks
; i
--;) {
1345 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
1346 && !(failed
== 0 && (i
== pd_idx
|| i
== qd_idx
))
1347 && !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1348 test_bit(R5_Insync
, &dev
->flags
)) {
1349 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1351 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1352 (unsigned long long)sh
->sector
, i
);
1353 set_bit(R5_LOCKED
, &dev
->flags
);
1354 set_bit(R5_Wantread
, &dev
->flags
);
1357 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1358 (unsigned long long)sh
->sector
, i
);
1359 set_bit(STRIPE_DELAYED
, &sh
->state
);
1360 set_bit(STRIPE_HANDLE
, &sh
->state
);
1364 /* now if nothing is locked, and if we have enough data, we can start a write request */
1365 if (locked
== 0 && rcw
== 0 &&
1366 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
1367 if ( must_compute
> 0 ) {
1368 /* We have failed blocks and need to compute them */
1371 case 1: compute_block_1(sh
, failed_num
[0], 0); break;
1372 case 2: compute_block_2(sh
, failed_num
[0], failed_num
[1]); break;
1373 default: BUG(); /* This request should have been failed? */
1377 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh
->sector
);
1378 compute_parity(sh
, RECONSTRUCT_WRITE
);
1379 /* now every locked buffer is ready to be written */
1381 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
1382 PRINTK("Writing stripe %llu block %d\n",
1383 (unsigned long long)sh
->sector
, i
);
1385 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1387 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
1388 set_bit(STRIPE_INSYNC
, &sh
->state
);
1390 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
1391 atomic_dec(&conf
->preread_active_stripes
);
1392 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
1393 md_wakeup_thread(conf
->mddev
->thread
);
1398 /* maybe we need to check and possibly fix the parity for this stripe
1399 * Any reads will already have been scheduled, so we just see if enough data
1402 if (syncing
&& locked
== 0 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1403 int update_p
= 0, update_q
= 0;
1406 set_bit(STRIPE_HANDLE
, &sh
->state
);
1409 BUG_ON(uptodate
< disks
);
1410 /* Want to check and possibly repair P and Q.
1411 * However there could be one 'failed' device, in which
1412 * case we can only check one of them, possibly using the
1413 * other to generate missing data
1416 /* If !tmp_page, we cannot do the calculations,
1417 * but as we have set STRIPE_HANDLE, we will soon be called
1418 * by stripe_handle with a tmp_page - just wait until then.
1421 if (failed
== q_failed
) {
1422 /* The only possible failed device holds 'Q', so it makes
1423 * sense to check P (If anything else were failed, we would
1424 * have used P to recreate it).
1426 compute_block_1(sh
, pd_idx
, 1);
1427 if (!page_is_zero(sh
->dev
[pd_idx
].page
)) {
1428 compute_block_1(sh
,pd_idx
,0);
1432 if (!q_failed
&& failed
< 2) {
1433 /* q is not failed, and we didn't use it to generate
1434 * anything, so it makes sense to check it
1436 memcpy(page_address(tmp_page
),
1437 page_address(sh
->dev
[qd_idx
].page
),
1439 compute_parity(sh
, UPDATE_PARITY
);
1440 if (memcmp(page_address(tmp_page
),
1441 page_address(sh
->dev
[qd_idx
].page
),
1443 clear_bit(STRIPE_INSYNC
, &sh
->state
);
1447 if (update_p
|| update_q
) {
1448 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
1449 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
1450 /* don't try to repair!! */
1451 update_p
= update_q
= 0;
1454 /* now write out any block on a failed drive,
1455 * or P or Q if they need it
1459 dev
= &sh
->dev
[failed_num
[1]];
1461 set_bit(R5_LOCKED
, &dev
->flags
);
1462 set_bit(R5_Wantwrite
, &dev
->flags
);
1465 dev
= &sh
->dev
[failed_num
[0]];
1467 set_bit(R5_LOCKED
, &dev
->flags
);
1468 set_bit(R5_Wantwrite
, &dev
->flags
);
1472 dev
= &sh
->dev
[pd_idx
];
1474 set_bit(R5_LOCKED
, &dev
->flags
);
1475 set_bit(R5_Wantwrite
, &dev
->flags
);
1478 dev
= &sh
->dev
[qd_idx
];
1480 set_bit(R5_LOCKED
, &dev
->flags
);
1481 set_bit(R5_Wantwrite
, &dev
->flags
);
1483 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
1485 set_bit(STRIPE_INSYNC
, &sh
->state
);
1489 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1490 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
1491 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1494 /* If the failed drives are just a ReadError, then we might need
1495 * to progress the repair/check process
1497 if (failed
<= 2 && ! conf
->mddev
->ro
)
1498 for (i
=0; i
<failed
;i
++) {
1499 dev
= &sh
->dev
[failed_num
[i
]];
1500 if (test_bit(R5_ReadError
, &dev
->flags
)
1501 && !test_bit(R5_LOCKED
, &dev
->flags
)
1502 && test_bit(R5_UPTODATE
, &dev
->flags
)
1504 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
1505 set_bit(R5_Wantwrite
, &dev
->flags
);
1506 set_bit(R5_ReWrite
, &dev
->flags
);
1507 set_bit(R5_LOCKED
, &dev
->flags
);
1509 /* let's read it back */
1510 set_bit(R5_Wantread
, &dev
->flags
);
1511 set_bit(R5_LOCKED
, &dev
->flags
);
1515 spin_unlock(&sh
->lock
);
1517 while ((bi
=return_bi
)) {
1518 int bytes
= bi
->bi_size
;
1520 return_bi
= bi
->bi_next
;
1523 bi
->bi_end_io(bi
, bytes
, 0);
1525 for (i
=disks
; i
-- ;) {
1529 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
1531 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
1536 bi
= &sh
->dev
[i
].req
;
1540 bi
->bi_end_io
= raid6_end_write_request
;
1542 bi
->bi_end_io
= raid6_end_read_request
;
1545 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1546 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
1549 atomic_inc(&rdev
->nr_pending
);
1554 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
1556 bi
->bi_bdev
= rdev
->bdev
;
1557 PRINTK("for %llu schedule op %ld on disc %d\n",
1558 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
1559 atomic_inc(&sh
->count
);
1560 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
1561 bi
->bi_flags
= 1 << BIO_UPTODATE
;
1563 bi
->bi_max_vecs
= 1;
1565 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
1566 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1567 bi
->bi_io_vec
[0].bv_offset
= 0;
1568 bi
->bi_size
= STRIPE_SIZE
;
1571 test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1572 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1573 generic_make_request(bi
);
1576 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1577 PRINTK("skip op %ld on disc %d for sector %llu\n",
1578 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
1579 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1580 set_bit(STRIPE_HANDLE
, &sh
->state
);
1585 static void raid6_activate_delayed(raid6_conf_t
*conf
)
1587 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
1588 while (!list_empty(&conf
->delayed_list
)) {
1589 struct list_head
*l
= conf
->delayed_list
.next
;
1590 struct stripe_head
*sh
;
1591 sh
= list_entry(l
, struct stripe_head
, lru
);
1593 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1594 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1595 atomic_inc(&conf
->preread_active_stripes
);
1596 list_add_tail(&sh
->lru
, &conf
->handle_list
);
1601 static void activate_bit_delay(raid6_conf_t
*conf
)
1603 /* device_lock is held */
1604 struct list_head head
;
1605 list_add(&head
, &conf
->bitmap_list
);
1606 list_del_init(&conf
->bitmap_list
);
1607 while (!list_empty(&head
)) {
1608 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
1609 list_del_init(&sh
->lru
);
1610 atomic_inc(&sh
->count
);
1611 __release_stripe(conf
, sh
);
1615 static void unplug_slaves(mddev_t
*mddev
)
1617 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1621 for (i
=0; i
<mddev
->raid_disks
; i
++) {
1622 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1623 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
1624 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
1626 atomic_inc(&rdev
->nr_pending
);
1629 if (r_queue
->unplug_fn
)
1630 r_queue
->unplug_fn(r_queue
);
1632 rdev_dec_pending(rdev
, mddev
);
1639 static void raid6_unplug_device(request_queue_t
*q
)
1641 mddev_t
*mddev
= q
->queuedata
;
1642 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1643 unsigned long flags
;
1645 spin_lock_irqsave(&conf
->device_lock
, flags
);
1647 if (blk_remove_plug(q
)) {
1649 raid6_activate_delayed(conf
);
1651 md_wakeup_thread(mddev
->thread
);
1653 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1655 unplug_slaves(mddev
);
1658 static int raid6_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
1659 sector_t
*error_sector
)
1661 mddev_t
*mddev
= q
->queuedata
;
1662 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1666 for (i
=0; i
<mddev
->raid_disks
&& ret
== 0; i
++) {
1667 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1668 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
1669 struct block_device
*bdev
= rdev
->bdev
;
1670 request_queue_t
*r_queue
= bdev_get_queue(bdev
);
1672 if (!r_queue
->issue_flush_fn
)
1675 atomic_inc(&rdev
->nr_pending
);
1677 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
1679 rdev_dec_pending(rdev
, mddev
);
1688 static inline void raid6_plug_device(raid6_conf_t
*conf
)
1690 spin_lock_irq(&conf
->device_lock
);
1691 blk_plug_device(conf
->mddev
->queue
);
1692 spin_unlock_irq(&conf
->device_lock
);
1695 static int make_request (request_queue_t
*q
, struct bio
* bi
)
1697 mddev_t
*mddev
= q
->queuedata
;
1698 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1699 const unsigned int raid_disks
= conf
->raid_disks
;
1700 const unsigned int data_disks
= raid_disks
- 2;
1701 unsigned int dd_idx
, pd_idx
;
1702 sector_t new_sector
;
1703 sector_t logical_sector
, last_sector
;
1704 struct stripe_head
*sh
;
1705 const int rw
= bio_data_dir(bi
);
1707 if (unlikely(bio_barrier(bi
))) {
1708 bio_endio(bi
, bi
->bi_size
, -EOPNOTSUPP
);
1712 md_write_start(mddev
, bi
);
1714 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
1715 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bi
));
1717 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
1718 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1721 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
1723 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
1726 new_sector
= raid6_compute_sector(logical_sector
,
1727 raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1729 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1730 (unsigned long long)new_sector
,
1731 (unsigned long long)logical_sector
);
1734 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
1735 sh
= get_active_stripe(conf
, new_sector
, pd_idx
, (bi
->bi_rw
&RWA_MASK
));
1737 if (!add_stripe_bio(sh
, bi
, dd_idx
, (bi
->bi_rw
&RW_MASK
))) {
1738 /* Add failed due to overlap. Flush everything
1741 raid6_unplug_device(mddev
->queue
);
1746 finish_wait(&conf
->wait_for_overlap
, &w
);
1747 raid6_plug_device(conf
);
1748 handle_stripe(sh
, NULL
);
1751 /* cannot get stripe for read-ahead, just give-up */
1752 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1753 finish_wait(&conf
->wait_for_overlap
, &w
);
1758 spin_lock_irq(&conf
->device_lock
);
1759 if (--bi
->bi_phys_segments
== 0) {
1760 int bytes
= bi
->bi_size
;
1763 md_write_end(mddev
);
1765 bi
->bi_end_io(bi
, bytes
, 0);
1767 spin_unlock_irq(&conf
->device_lock
);
1771 /* FIXME go_faster isn't used */
1772 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1774 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
1775 struct stripe_head
*sh
;
1776 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1778 unsigned long stripe
;
1781 sector_t first_sector
;
1782 int raid_disks
= conf
->raid_disks
;
1783 int data_disks
= raid_disks
- 2;
1784 sector_t max_sector
= mddev
->size
<< 1;
1786 int still_degraded
= 0;
1789 if (sector_nr
>= max_sector
) {
1790 /* just being told to finish up .. nothing much to do */
1791 unplug_slaves(mddev
);
1793 if (mddev
->curr_resync
< max_sector
) /* aborted */
1794 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1796 else /* completed sync */
1798 bitmap_close_sync(mddev
->bitmap
);
1802 /* if there are 2 or more failed drives and we are trying
1803 * to resync, then assert that we are finished, because there is
1804 * nothing we can do.
1806 if (mddev
->degraded
>= 2 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1807 sector_t rv
= (mddev
->size
<< 1) - sector_nr
;
1811 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
1812 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
1813 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
1814 /* we can skip this block, and probably more */
1815 sync_blocks
/= STRIPE_SECTORS
;
1817 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
1821 chunk_offset
= sector_div(x
, sectors_per_chunk
);
1823 BUG_ON(x
!= stripe
);
1825 first_sector
= raid6_compute_sector((sector_t
)stripe
*data_disks
*sectors_per_chunk
1826 + chunk_offset
, raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1827 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 1);
1829 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 0);
1830 /* make sure we don't swamp the stripe cache if someone else
1831 * is trying to get access
1833 schedule_timeout_uninterruptible(1);
1835 /* Need to check if array will still be degraded after recovery/resync
1836 * We don't need to check the 'failed' flag as when that gets set,
1839 for (i
=0; i
<mddev
->raid_disks
; i
++)
1840 if (conf
->disks
[i
].rdev
== NULL
)
1843 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
1845 spin_lock(&sh
->lock
);
1846 set_bit(STRIPE_SYNCING
, &sh
->state
);
1847 clear_bit(STRIPE_INSYNC
, &sh
->state
);
1848 spin_unlock(&sh
->lock
);
1850 handle_stripe(sh
, NULL
);
1853 return STRIPE_SECTORS
;
1857 * This is our raid6 kernel thread.
1859 * We scan the hash table for stripes which can be handled now.
1860 * During the scan, completed stripes are saved for us by the interrupt
1861 * handler, so that they will not have to wait for our next wakeup.
1863 static void raid6d (mddev_t
*mddev
)
1865 struct stripe_head
*sh
;
1866 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1869 PRINTK("+++ raid6d active\n");
1871 md_check_recovery(mddev
);
1874 spin_lock_irq(&conf
->device_lock
);
1876 struct list_head
*first
;
1878 if (conf
->seq_flush
- conf
->seq_write
> 0) {
1879 int seq
= conf
->seq_flush
;
1880 spin_unlock_irq(&conf
->device_lock
);
1881 bitmap_unplug(mddev
->bitmap
);
1882 spin_lock_irq(&conf
->device_lock
);
1883 conf
->seq_write
= seq
;
1884 activate_bit_delay(conf
);
1887 if (list_empty(&conf
->handle_list
) &&
1888 atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
&&
1889 !blk_queue_plugged(mddev
->queue
) &&
1890 !list_empty(&conf
->delayed_list
))
1891 raid6_activate_delayed(conf
);
1893 if (list_empty(&conf
->handle_list
))
1896 first
= conf
->handle_list
.next
;
1897 sh
= list_entry(first
, struct stripe_head
, lru
);
1899 list_del_init(first
);
1900 atomic_inc(&sh
->count
);
1901 BUG_ON(atomic_read(&sh
->count
)!= 1);
1902 spin_unlock_irq(&conf
->device_lock
);
1905 handle_stripe(sh
, conf
->spare_page
);
1908 spin_lock_irq(&conf
->device_lock
);
1910 PRINTK("%d stripes handled\n", handled
);
1912 spin_unlock_irq(&conf
->device_lock
);
1914 unplug_slaves(mddev
);
1916 PRINTK("--- raid6d inactive\n");
1920 raid6_show_stripe_cache_size(mddev_t
*mddev
, char *page
)
1922 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1924 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
1930 raid6_store_stripe_cache_size(mddev_t
*mddev
, const char *page
, size_t len
)
1932 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1935 if (len
>= PAGE_SIZE
)
1940 new = simple_strtoul(page
, &end
, 10);
1941 if (!*page
|| (*end
&& *end
!= '\n') )
1943 if (new <= 16 || new > 32768)
1945 while (new < conf
->max_nr_stripes
) {
1946 if (drop_one_stripe(conf
))
1947 conf
->max_nr_stripes
--;
1951 while (new > conf
->max_nr_stripes
) {
1952 if (grow_one_stripe(conf
))
1953 conf
->max_nr_stripes
++;
1959 static struct md_sysfs_entry
1960 raid6_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
1961 raid6_show_stripe_cache_size
,
1962 raid6_store_stripe_cache_size
);
1965 stripe_cache_active_show(mddev_t
*mddev
, char *page
)
1967 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
1969 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
1974 static struct md_sysfs_entry
1975 raid6_stripecache_active
= __ATTR_RO(stripe_cache_active
);
1977 static struct attribute
*raid6_attrs
[] = {
1978 &raid6_stripecache_size
.attr
,
1979 &raid6_stripecache_active
.attr
,
1982 static struct attribute_group raid6_attrs_group
= {
1984 .attrs
= raid6_attrs
,
1987 static int run(mddev_t
*mddev
)
1990 int raid_disk
, memory
;
1992 struct disk_info
*disk
;
1993 struct list_head
*tmp
;
1995 if (mddev
->level
!= 6) {
1996 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev
), mddev
->level
);
2000 mddev
->private = kzalloc(sizeof (raid6_conf_t
), GFP_KERNEL
);
2001 if ((conf
= mddev
->private) == NULL
)
2003 conf
->disks
= kzalloc(mddev
->raid_disks
* sizeof(struct disk_info
),
2008 conf
->mddev
= mddev
;
2010 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
2013 conf
->spare_page
= alloc_page(GFP_KERNEL
);
2014 if (!conf
->spare_page
)
2017 spin_lock_init(&conf
->device_lock
);
2018 init_waitqueue_head(&conf
->wait_for_stripe
);
2019 init_waitqueue_head(&conf
->wait_for_overlap
);
2020 INIT_LIST_HEAD(&conf
->handle_list
);
2021 INIT_LIST_HEAD(&conf
->delayed_list
);
2022 INIT_LIST_HEAD(&conf
->bitmap_list
);
2023 INIT_LIST_HEAD(&conf
->inactive_list
);
2024 atomic_set(&conf
->active_stripes
, 0);
2025 atomic_set(&conf
->preread_active_stripes
, 0);
2027 PRINTK("raid6: run(%s) called.\n", mdname(mddev
));
2029 ITERATE_RDEV(mddev
,rdev
,tmp
) {
2030 raid_disk
= rdev
->raid_disk
;
2031 if (raid_disk
>= mddev
->raid_disks
2034 disk
= conf
->disks
+ raid_disk
;
2038 if (test_bit(In_sync
, &rdev
->flags
)) {
2039 char b
[BDEVNAME_SIZE
];
2040 printk(KERN_INFO
"raid6: device %s operational as raid"
2041 " disk %d\n", bdevname(rdev
->bdev
,b
),
2043 conf
->working_disks
++;
2047 conf
->raid_disks
= mddev
->raid_disks
;
2050 * 0 for a fully functional array, 1 or 2 for a degraded array.
2052 mddev
->degraded
= conf
->failed_disks
= conf
->raid_disks
- conf
->working_disks
;
2053 conf
->mddev
= mddev
;
2054 conf
->chunk_size
= mddev
->chunk_size
;
2055 conf
->level
= mddev
->level
;
2056 conf
->algorithm
= mddev
->layout
;
2057 conf
->max_nr_stripes
= NR_STRIPES
;
2059 /* device size must be a multiple of chunk size */
2060 mddev
->size
&= ~(mddev
->chunk_size
/1024 -1);
2061 mddev
->resync_max_sectors
= mddev
->size
<< 1;
2063 if (conf
->raid_disks
< 4) {
2064 printk(KERN_ERR
"raid6: not enough configured devices for %s (%d, minimum 4)\n",
2065 mdname(mddev
), conf
->raid_disks
);
2068 if (!conf
->chunk_size
|| conf
->chunk_size
% 4) {
2069 printk(KERN_ERR
"raid6: invalid chunk size %d for %s\n",
2070 conf
->chunk_size
, mdname(mddev
));
2073 if (conf
->algorithm
> ALGORITHM_RIGHT_SYMMETRIC
) {
2075 "raid6: unsupported parity algorithm %d for %s\n",
2076 conf
->algorithm
, mdname(mddev
));
2079 if (mddev
->degraded
> 2) {
2080 printk(KERN_ERR
"raid6: not enough operational devices for %s"
2081 " (%d/%d failed)\n",
2082 mdname(mddev
), conf
->failed_disks
, conf
->raid_disks
);
2086 if (mddev
->degraded
> 0 &&
2087 mddev
->recovery_cp
!= MaxSector
) {
2088 if (mddev
->ok_start_degraded
)
2089 printk(KERN_WARNING
"raid6: starting dirty degraded array:%s"
2090 "- data corruption possible.\n",
2093 printk(KERN_ERR
"raid6: cannot start dirty degraded array"
2094 " for %s\n", mdname(mddev
));
2100 mddev
->thread
= md_register_thread(raid6d
, mddev
, "%s_raid6");
2101 if (!mddev
->thread
) {
2103 "raid6: couldn't allocate thread for %s\n",
2109 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
2110 conf
->raid_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
2111 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
2113 "raid6: couldn't allocate %dkB for buffers\n", memory
);
2114 shrink_stripes(conf
);
2115 md_unregister_thread(mddev
->thread
);
2118 printk(KERN_INFO
"raid6: allocated %dkB for %s\n",
2119 memory
, mdname(mddev
));
2121 if (mddev
->degraded
== 0)
2122 printk(KERN_INFO
"raid6: raid level %d set %s active with %d out of %d"
2123 " devices, algorithm %d\n", conf
->level
, mdname(mddev
),
2124 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
2127 printk(KERN_ALERT
"raid6: raid level %d set %s active with %d"
2128 " out of %d devices, algorithm %d\n", conf
->level
,
2129 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2130 mddev
->raid_disks
, conf
->algorithm
);
2132 print_raid6_conf(conf
);
2134 /* read-ahead size must cover two whole stripes, which is
2135 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
2138 int stripe
= (mddev
->raid_disks
-2) * mddev
->chunk_size
2140 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
2141 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
2144 /* Ok, everything is just fine now */
2145 sysfs_create_group(&mddev
->kobj
, &raid6_attrs_group
);
2147 mddev
->array_size
= mddev
->size
* (mddev
->raid_disks
- 2);
2149 mddev
->queue
->unplug_fn
= raid6_unplug_device
;
2150 mddev
->queue
->issue_flush_fn
= raid6_issue_flush
;
2154 print_raid6_conf(conf
);
2155 safe_put_page(conf
->spare_page
);
2156 kfree(conf
->stripe_hashtbl
);
2160 mddev
->private = NULL
;
2161 printk(KERN_ALERT
"raid6: failed to run raid set %s\n", mdname(mddev
));
2167 static int stop (mddev_t
*mddev
)
2169 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
2171 md_unregister_thread(mddev
->thread
);
2172 mddev
->thread
= NULL
;
2173 shrink_stripes(conf
);
2174 kfree(conf
->stripe_hashtbl
);
2175 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2176 sysfs_remove_group(&mddev
->kobj
, &raid6_attrs_group
);
2178 mddev
->private = NULL
;
2183 static void print_sh (struct seq_file
*seq
, struct stripe_head
*sh
)
2187 seq_printf(seq
, "sh %llu, pd_idx %d, state %ld.\n",
2188 (unsigned long long)sh
->sector
, sh
->pd_idx
, sh
->state
);
2189 seq_printf(seq
, "sh %llu, count %d.\n",
2190 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
));
2191 seq_printf(seq
, "sh %llu, ", (unsigned long long)sh
->sector
);
2192 for (i
= 0; i
< sh
->raid_conf
->raid_disks
; i
++) {
2193 seq_printf(seq
, "(cache%d: %p %ld) ",
2194 i
, sh
->dev
[i
].page
, sh
->dev
[i
].flags
);
2196 seq_printf(seq
, "\n");
2199 static void printall (struct seq_file
*seq
, raid6_conf_t
*conf
)
2201 struct stripe_head
*sh
;
2202 struct hlist_node
*hn
;
2205 spin_lock_irq(&conf
->device_lock
);
2206 for (i
= 0; i
< NR_HASH
; i
++) {
2207 sh
= conf
->stripe_hashtbl
[i
];
2208 hlist_for_each_entry(sh
, hn
, &conf
->stripe_hashtbl
[i
], hash
) {
2209 if (sh
->raid_conf
!= conf
)
2214 spin_unlock_irq(&conf
->device_lock
);
2218 static void status (struct seq_file
*seq
, mddev_t
*mddev
)
2220 raid6_conf_t
*conf
= (raid6_conf_t
*) mddev
->private;
2223 seq_printf (seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
, mddev
->chunk_size
>> 10, mddev
->layout
);
2224 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->working_disks
);
2225 for (i
= 0; i
< conf
->raid_disks
; i
++)
2226 seq_printf (seq
, "%s",
2227 conf
->disks
[i
].rdev
&&
2228 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
2229 seq_printf (seq
, "]");
2231 seq_printf (seq
, "\n");
2232 printall(seq
, conf
);
2236 static void print_raid6_conf (raid6_conf_t
*conf
)
2239 struct disk_info
*tmp
;
2241 printk("RAID6 conf printout:\n");
2243 printk("(conf==NULL)\n");
2246 printk(" --- rd:%d wd:%d fd:%d\n", conf
->raid_disks
,
2247 conf
->working_disks
, conf
->failed_disks
);
2249 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2250 char b
[BDEVNAME_SIZE
];
2251 tmp
= conf
->disks
+ i
;
2253 printk(" disk %d, o:%d, dev:%s\n",
2254 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
2255 bdevname(tmp
->rdev
->bdev
,b
));
2259 static int raid6_spare_active(mddev_t
*mddev
)
2262 raid6_conf_t
*conf
= mddev
->private;
2263 struct disk_info
*tmp
;
2265 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2266 tmp
= conf
->disks
+ i
;
2268 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
2269 && !test_bit(In_sync
, &tmp
->rdev
->flags
)) {
2271 conf
->failed_disks
--;
2272 conf
->working_disks
++;
2273 set_bit(In_sync
, &tmp
->rdev
->flags
);
2276 print_raid6_conf(conf
);
2280 static int raid6_remove_disk(mddev_t
*mddev
, int number
)
2282 raid6_conf_t
*conf
= mddev
->private;
2285 struct disk_info
*p
= conf
->disks
+ number
;
2287 print_raid6_conf(conf
);
2290 if (test_bit(In_sync
, &rdev
->flags
) ||
2291 atomic_read(&rdev
->nr_pending
)) {
2297 if (atomic_read(&rdev
->nr_pending
)) {
2298 /* lost the race, try later */
2306 print_raid6_conf(conf
);
2310 static int raid6_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
2312 raid6_conf_t
*conf
= mddev
->private;
2315 struct disk_info
*p
;
2317 if (mddev
->degraded
> 2)
2318 /* no point adding a device */
2321 * find the disk ... but prefer rdev->saved_raid_disk
2324 if (rdev
->saved_raid_disk
>= 0 &&
2325 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
2326 disk
= rdev
->saved_raid_disk
;
2329 for ( ; disk
< mddev
->raid_disks
; disk
++)
2330 if ((p
=conf
->disks
+ disk
)->rdev
== NULL
) {
2331 clear_bit(In_sync
, &rdev
->flags
);
2332 rdev
->raid_disk
= disk
;
2334 if (rdev
->saved_raid_disk
!= disk
)
2336 rcu_assign_pointer(p
->rdev
, rdev
);
2339 print_raid6_conf(conf
);
2343 static int raid6_resize(mddev_t
*mddev
, sector_t sectors
)
2345 /* no resync is happening, and there is enough space
2346 * on all devices, so we can resize.
2347 * We need to make sure resync covers any new space.
2348 * If the array is shrinking we should possibly wait until
2349 * any io in the removed space completes, but it hardly seems
2352 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
2353 mddev
->array_size
= (sectors
* (mddev
->raid_disks
-2))>>1;
2354 set_capacity(mddev
->gendisk
, mddev
->array_size
<< 1);
2356 if (sectors
/2 > mddev
->size
&& mddev
->recovery_cp
== MaxSector
) {
2357 mddev
->recovery_cp
= mddev
->size
<< 1;
2358 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2360 mddev
->size
= sectors
/2;
2361 mddev
->resync_max_sectors
= sectors
;
2365 static void raid6_quiesce(mddev_t
*mddev
, int state
)
2367 raid6_conf_t
*conf
= mddev_to_conf(mddev
);
2370 case 1: /* stop all writes */
2371 spin_lock_irq(&conf
->device_lock
);
2373 wait_event_lock_irq(conf
->wait_for_stripe
,
2374 atomic_read(&conf
->active_stripes
) == 0,
2375 conf
->device_lock
, /* nothing */);
2376 spin_unlock_irq(&conf
->device_lock
);
2379 case 0: /* re-enable writes */
2380 spin_lock_irq(&conf
->device_lock
);
2382 wake_up(&conf
->wait_for_stripe
);
2383 spin_unlock_irq(&conf
->device_lock
);
2388 static struct mdk_personality raid6_personality
=
2392 .owner
= THIS_MODULE
,
2393 .make_request
= make_request
,
2397 .error_handler
= error
,
2398 .hot_add_disk
= raid6_add_disk
,
2399 .hot_remove_disk
= raid6_remove_disk
,
2400 .spare_active
= raid6_spare_active
,
2401 .sync_request
= sync_request
,
2402 .resize
= raid6_resize
,
2403 .quiesce
= raid6_quiesce
,
2406 static int __init
raid6_init(void)
2410 e
= raid6_select_algo();
2414 return register_md_personality(&raid6_personality
);
2417 static void raid6_exit (void)
2419 unregister_md_personality(&raid6_personality
);
2422 module_init(raid6_init
);
2423 module_exit(raid6_exit
);
2424 MODULE_LICENSE("GPL");
2425 MODULE_ALIAS("md-personality-8"); /* RAID6 */
2426 MODULE_ALIAS("md-raid6");
2427 MODULE_ALIAS("md-level-6");