2 * raid5.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-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->bm_write is the number of the last batch successfully written.
31 * conf->bm_flush is the number of the last batch that was closed to
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is bm_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
46 #include <linux/module.h>
47 #include <linux/slab.h>
48 #include <linux/highmem.h>
49 #include <linux/bitops.h>
50 #include <linux/kthread.h>
51 #include <asm/atomic.h>
54 #include <linux/raid/bitmap.h>
60 #define NR_STRIPES 256
61 #define STRIPE_SIZE PAGE_SIZE
62 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
63 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
64 #define IO_THRESHOLD 1
65 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
66 #define HASH_MASK (NR_HASH - 1)
68 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
70 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
71 * order without overlap. There may be several bio's per stripe+device, and
72 * a bio could span several devices.
73 * When walking this list for a particular stripe+device, we must never proceed
74 * beyond a bio that extends past this device, as the next bio might no longer
76 * This macro is used to determine the 'next' bio in the list, given the sector
77 * of the current stripe+device
79 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
81 * The following can be used to debug the driver
84 #define RAID5_PARANOIA 1
85 #if RAID5_PARANOIA && defined(CONFIG_SMP)
86 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
88 # define CHECK_DEVLOCK()
91 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
97 #if !RAID6_USE_EMPTY_ZERO_PAGE
98 /* In .bss so it's zeroed */
99 const char raid6_empty_zero_page
[PAGE_SIZE
] __attribute__((aligned(256)));
102 static inline int raid6_next_disk(int disk
, int raid_disks
)
105 return (disk
< raid_disks
) ? disk
: 0;
107 static void print_raid5_conf (raid5_conf_t
*conf
);
109 static void __release_stripe(raid5_conf_t
*conf
, struct stripe_head
*sh
)
111 if (atomic_dec_and_test(&sh
->count
)) {
112 BUG_ON(!list_empty(&sh
->lru
));
113 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
114 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
115 if (test_bit(STRIPE_DELAYED
, &sh
->state
)) {
116 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
117 blk_plug_device(conf
->mddev
->queue
);
118 } else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
119 sh
->bm_seq
- conf
->seq_write
> 0) {
120 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
121 blk_plug_device(conf
->mddev
->queue
);
123 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
124 list_add_tail(&sh
->lru
, &conf
->handle_list
);
126 md_wakeup_thread(conf
->mddev
->thread
);
128 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
129 atomic_dec(&conf
->preread_active_stripes
);
130 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
131 md_wakeup_thread(conf
->mddev
->thread
);
133 atomic_dec(&conf
->active_stripes
);
134 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
135 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
136 wake_up(&conf
->wait_for_stripe
);
137 if (conf
->retry_read_aligned
)
138 md_wakeup_thread(conf
->mddev
->thread
);
143 static void release_stripe(struct stripe_head
*sh
)
145 raid5_conf_t
*conf
= sh
->raid_conf
;
148 spin_lock_irqsave(&conf
->device_lock
, flags
);
149 __release_stripe(conf
, sh
);
150 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
153 static inline void remove_hash(struct stripe_head
*sh
)
155 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
157 hlist_del_init(&sh
->hash
);
160 static inline void insert_hash(raid5_conf_t
*conf
, struct stripe_head
*sh
)
162 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
164 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
167 hlist_add_head(&sh
->hash
, hp
);
171 /* find an idle stripe, make sure it is unhashed, and return it. */
172 static struct stripe_head
*get_free_stripe(raid5_conf_t
*conf
)
174 struct stripe_head
*sh
= NULL
;
175 struct list_head
*first
;
178 if (list_empty(&conf
->inactive_list
))
180 first
= conf
->inactive_list
.next
;
181 sh
= list_entry(first
, struct stripe_head
, lru
);
182 list_del_init(first
);
184 atomic_inc(&conf
->active_stripes
);
189 static void shrink_buffers(struct stripe_head
*sh
, int num
)
194 for (i
=0; i
<num
; i
++) {
198 sh
->dev
[i
].page
= NULL
;
203 static int grow_buffers(struct stripe_head
*sh
, int num
)
207 for (i
=0; i
<num
; i
++) {
210 if (!(page
= alloc_page(GFP_KERNEL
))) {
213 sh
->dev
[i
].page
= page
;
218 static void raid5_build_block (struct stripe_head
*sh
, int i
);
220 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int pd_idx
, int disks
)
222 raid5_conf_t
*conf
= sh
->raid_conf
;
225 BUG_ON(atomic_read(&sh
->count
) != 0);
226 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
229 PRINTK("init_stripe called, stripe %llu\n",
230 (unsigned long long)sh
->sector
);
240 for (i
= sh
->disks
; i
--; ) {
241 struct r5dev
*dev
= &sh
->dev
[i
];
243 if (dev
->toread
|| dev
->towrite
|| dev
->written
||
244 test_bit(R5_LOCKED
, &dev
->flags
)) {
245 printk("sector=%llx i=%d %p %p %p %d\n",
246 (unsigned long long)sh
->sector
, i
, dev
->toread
,
247 dev
->towrite
, dev
->written
,
248 test_bit(R5_LOCKED
, &dev
->flags
));
252 raid5_build_block(sh
, i
);
254 insert_hash(conf
, sh
);
257 static struct stripe_head
*__find_stripe(raid5_conf_t
*conf
, sector_t sector
, int disks
)
259 struct stripe_head
*sh
;
260 struct hlist_node
*hn
;
263 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector
);
264 hlist_for_each_entry(sh
, hn
, stripe_hash(conf
, sector
), hash
)
265 if (sh
->sector
== sector
&& sh
->disks
== disks
)
267 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector
);
271 static void unplug_slaves(mddev_t
*mddev
);
272 static void raid5_unplug_device(request_queue_t
*q
);
274 static struct stripe_head
*get_active_stripe(raid5_conf_t
*conf
, sector_t sector
, int disks
,
275 int pd_idx
, int noblock
)
277 struct stripe_head
*sh
;
279 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector
);
281 spin_lock_irq(&conf
->device_lock
);
284 wait_event_lock_irq(conf
->wait_for_stripe
,
286 conf
->device_lock
, /* nothing */);
287 sh
= __find_stripe(conf
, sector
, disks
);
289 if (!conf
->inactive_blocked
)
290 sh
= get_free_stripe(conf
);
291 if (noblock
&& sh
== NULL
)
294 conf
->inactive_blocked
= 1;
295 wait_event_lock_irq(conf
->wait_for_stripe
,
296 !list_empty(&conf
->inactive_list
) &&
297 (atomic_read(&conf
->active_stripes
)
298 < (conf
->max_nr_stripes
*3/4)
299 || !conf
->inactive_blocked
),
301 raid5_unplug_device(conf
->mddev
->queue
)
303 conf
->inactive_blocked
= 0;
305 init_stripe(sh
, sector
, pd_idx
, disks
);
307 if (atomic_read(&sh
->count
)) {
308 BUG_ON(!list_empty(&sh
->lru
));
310 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
311 atomic_inc(&conf
->active_stripes
);
312 if (list_empty(&sh
->lru
) &&
313 !test_bit(STRIPE_EXPANDING
, &sh
->state
))
315 list_del_init(&sh
->lru
);
318 } while (sh
== NULL
);
321 atomic_inc(&sh
->count
);
323 spin_unlock_irq(&conf
->device_lock
);
327 static int grow_one_stripe(raid5_conf_t
*conf
)
329 struct stripe_head
*sh
;
330 sh
= kmem_cache_alloc(conf
->slab_cache
, GFP_KERNEL
);
333 memset(sh
, 0, sizeof(*sh
) + (conf
->raid_disks
-1)*sizeof(struct r5dev
));
334 sh
->raid_conf
= conf
;
335 spin_lock_init(&sh
->lock
);
337 if (grow_buffers(sh
, conf
->raid_disks
)) {
338 shrink_buffers(sh
, conf
->raid_disks
);
339 kmem_cache_free(conf
->slab_cache
, sh
);
342 sh
->disks
= conf
->raid_disks
;
343 /* we just created an active stripe so... */
344 atomic_set(&sh
->count
, 1);
345 atomic_inc(&conf
->active_stripes
);
346 INIT_LIST_HEAD(&sh
->lru
);
351 static int grow_stripes(raid5_conf_t
*conf
, int num
)
353 struct kmem_cache
*sc
;
354 int devs
= conf
->raid_disks
;
356 sprintf(conf
->cache_name
[0], "raid5/%s", mdname(conf
->mddev
));
357 sprintf(conf
->cache_name
[1], "raid5/%s-alt", mdname(conf
->mddev
));
358 conf
->active_name
= 0;
359 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
360 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
364 conf
->slab_cache
= sc
;
365 conf
->pool_size
= devs
;
367 if (!grow_one_stripe(conf
))
372 #ifdef CONFIG_MD_RAID5_RESHAPE
373 static int resize_stripes(raid5_conf_t
*conf
, int newsize
)
375 /* Make all the stripes able to hold 'newsize' devices.
376 * New slots in each stripe get 'page' set to a new page.
378 * This happens in stages:
379 * 1/ create a new kmem_cache and allocate the required number of
381 * 2/ gather all the old stripe_heads and tranfer the pages across
382 * to the new stripe_heads. This will have the side effect of
383 * freezing the array as once all stripe_heads have been collected,
384 * no IO will be possible. Old stripe heads are freed once their
385 * pages have been transferred over, and the old kmem_cache is
386 * freed when all stripes are done.
387 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
388 * we simple return a failre status - no need to clean anything up.
389 * 4/ allocate new pages for the new slots in the new stripe_heads.
390 * If this fails, we don't bother trying the shrink the
391 * stripe_heads down again, we just leave them as they are.
392 * As each stripe_head is processed the new one is released into
395 * Once step2 is started, we cannot afford to wait for a write,
396 * so we use GFP_NOIO allocations.
398 struct stripe_head
*osh
, *nsh
;
399 LIST_HEAD(newstripes
);
400 struct disk_info
*ndisks
;
402 struct kmem_cache
*sc
;
405 if (newsize
<= conf
->pool_size
)
406 return 0; /* never bother to shrink */
409 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
410 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
415 for (i
= conf
->max_nr_stripes
; i
; i
--) {
416 nsh
= kmem_cache_alloc(sc
, GFP_KERNEL
);
420 memset(nsh
, 0, sizeof(*nsh
) + (newsize
-1)*sizeof(struct r5dev
));
422 nsh
->raid_conf
= conf
;
423 spin_lock_init(&nsh
->lock
);
425 list_add(&nsh
->lru
, &newstripes
);
428 /* didn't get enough, give up */
429 while (!list_empty(&newstripes
)) {
430 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
432 kmem_cache_free(sc
, nsh
);
434 kmem_cache_destroy(sc
);
437 /* Step 2 - Must use GFP_NOIO now.
438 * OK, we have enough stripes, start collecting inactive
439 * stripes and copying them over
441 list_for_each_entry(nsh
, &newstripes
, lru
) {
442 spin_lock_irq(&conf
->device_lock
);
443 wait_event_lock_irq(conf
->wait_for_stripe
,
444 !list_empty(&conf
->inactive_list
),
446 unplug_slaves(conf
->mddev
)
448 osh
= get_free_stripe(conf
);
449 spin_unlock_irq(&conf
->device_lock
);
450 atomic_set(&nsh
->count
, 1);
451 for(i
=0; i
<conf
->pool_size
; i
++)
452 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
453 for( ; i
<newsize
; i
++)
454 nsh
->dev
[i
].page
= NULL
;
455 kmem_cache_free(conf
->slab_cache
, osh
);
457 kmem_cache_destroy(conf
->slab_cache
);
460 * At this point, we are holding all the stripes so the array
461 * is completely stalled, so now is a good time to resize
464 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
466 for (i
=0; i
<conf
->raid_disks
; i
++)
467 ndisks
[i
] = conf
->disks
[i
];
469 conf
->disks
= ndisks
;
473 /* Step 4, return new stripes to service */
474 while(!list_empty(&newstripes
)) {
475 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
476 list_del_init(&nsh
->lru
);
477 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
478 if (nsh
->dev
[i
].page
== NULL
) {
479 struct page
*p
= alloc_page(GFP_NOIO
);
480 nsh
->dev
[i
].page
= p
;
486 /* critical section pass, GFP_NOIO no longer needed */
488 conf
->slab_cache
= sc
;
489 conf
->active_name
= 1-conf
->active_name
;
490 conf
->pool_size
= newsize
;
495 static int drop_one_stripe(raid5_conf_t
*conf
)
497 struct stripe_head
*sh
;
499 spin_lock_irq(&conf
->device_lock
);
500 sh
= get_free_stripe(conf
);
501 spin_unlock_irq(&conf
->device_lock
);
504 BUG_ON(atomic_read(&sh
->count
));
505 shrink_buffers(sh
, conf
->pool_size
);
506 kmem_cache_free(conf
->slab_cache
, sh
);
507 atomic_dec(&conf
->active_stripes
);
511 static void shrink_stripes(raid5_conf_t
*conf
)
513 while (drop_one_stripe(conf
))
516 if (conf
->slab_cache
)
517 kmem_cache_destroy(conf
->slab_cache
);
518 conf
->slab_cache
= NULL
;
521 static int raid5_end_read_request(struct bio
* bi
, unsigned int bytes_done
,
524 struct stripe_head
*sh
= bi
->bi_private
;
525 raid5_conf_t
*conf
= sh
->raid_conf
;
526 int disks
= sh
->disks
, i
;
527 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
528 char b
[BDEVNAME_SIZE
];
534 for (i
=0 ; i
<disks
; i
++)
535 if (bi
== &sh
->dev
[i
].req
)
538 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
539 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
547 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
548 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
549 rdev
= conf
->disks
[i
].rdev
;
550 printk(KERN_INFO
"raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
551 mdname(conf
->mddev
), STRIPE_SECTORS
,
552 (unsigned long long)sh
->sector
+ rdev
->data_offset
,
553 bdevname(rdev
->bdev
, b
));
554 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
555 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
557 if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
))
558 atomic_set(&conf
->disks
[i
].rdev
->read_errors
, 0);
560 const char *bdn
= bdevname(conf
->disks
[i
].rdev
->bdev
, b
);
562 rdev
= conf
->disks
[i
].rdev
;
564 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
565 atomic_inc(&rdev
->read_errors
);
566 if (conf
->mddev
->degraded
)
567 printk(KERN_WARNING
"raid5:%s: read error not correctable (sector %llu on %s).\n",
569 (unsigned long long)sh
->sector
+ rdev
->data_offset
,
571 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
573 printk(KERN_WARNING
"raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
575 (unsigned long long)sh
->sector
+ rdev
->data_offset
,
577 else if (atomic_read(&rdev
->read_errors
)
578 > conf
->max_nr_stripes
)
580 "raid5:%s: Too many read errors, failing device %s.\n",
581 mdname(conf
->mddev
), bdn
);
585 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
587 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
588 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
589 md_error(conf
->mddev
, rdev
);
592 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
593 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
594 set_bit(STRIPE_HANDLE
, &sh
->state
);
599 static int raid5_end_write_request (struct bio
*bi
, unsigned int bytes_done
,
602 struct stripe_head
*sh
= bi
->bi_private
;
603 raid5_conf_t
*conf
= sh
->raid_conf
;
604 int disks
= sh
->disks
, i
;
605 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
610 for (i
=0 ; i
<disks
; i
++)
611 if (bi
== &sh
->dev
[i
].req
)
614 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
615 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
623 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
625 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
627 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
628 set_bit(STRIPE_HANDLE
, &sh
->state
);
634 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
);
636 static void raid5_build_block (struct stripe_head
*sh
, int i
)
638 struct r5dev
*dev
= &sh
->dev
[i
];
641 dev
->req
.bi_io_vec
= &dev
->vec
;
643 dev
->req
.bi_max_vecs
++;
644 dev
->vec
.bv_page
= dev
->page
;
645 dev
->vec
.bv_len
= STRIPE_SIZE
;
646 dev
->vec
.bv_offset
= 0;
648 dev
->req
.bi_sector
= sh
->sector
;
649 dev
->req
.bi_private
= sh
;
652 dev
->sector
= compute_blocknr(sh
, i
);
655 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
657 char b
[BDEVNAME_SIZE
];
658 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
659 PRINTK("raid5: error called\n");
661 if (!test_bit(Faulty
, &rdev
->flags
)) {
662 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
663 if (test_and_clear_bit(In_sync
, &rdev
->flags
)) {
665 spin_lock_irqsave(&conf
->device_lock
, flags
);
667 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
669 * if recovery was running, make sure it aborts.
671 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
673 set_bit(Faulty
, &rdev
->flags
);
675 "raid5: Disk failure on %s, disabling device."
676 " Operation continuing on %d devices\n",
677 bdevname(rdev
->bdev
,b
), conf
->raid_disks
- mddev
->degraded
);
682 * Input: a 'big' sector number,
683 * Output: index of the data and parity disk, and the sector # in them.
685 static sector_t
raid5_compute_sector(sector_t r_sector
, unsigned int raid_disks
,
686 unsigned int data_disks
, unsigned int * dd_idx
,
687 unsigned int * pd_idx
, raid5_conf_t
*conf
)
690 unsigned long chunk_number
;
691 unsigned int chunk_offset
;
693 int sectors_per_chunk
= conf
->chunk_size
>> 9;
695 /* First compute the information on this sector */
698 * Compute the chunk number and the sector offset inside the chunk
700 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
701 chunk_number
= r_sector
;
702 BUG_ON(r_sector
!= chunk_number
);
705 * Compute the stripe number
707 stripe
= chunk_number
/ data_disks
;
710 * Compute the data disk and parity disk indexes inside the stripe
712 *dd_idx
= chunk_number
% data_disks
;
715 * Select the parity disk based on the user selected algorithm.
717 switch(conf
->level
) {
719 *pd_idx
= data_disks
;
722 switch (conf
->algorithm
) {
723 case ALGORITHM_LEFT_ASYMMETRIC
:
724 *pd_idx
= data_disks
- stripe
% raid_disks
;
725 if (*dd_idx
>= *pd_idx
)
728 case ALGORITHM_RIGHT_ASYMMETRIC
:
729 *pd_idx
= stripe
% raid_disks
;
730 if (*dd_idx
>= *pd_idx
)
733 case ALGORITHM_LEFT_SYMMETRIC
:
734 *pd_idx
= data_disks
- stripe
% raid_disks
;
735 *dd_idx
= (*pd_idx
+ 1 + *dd_idx
) % raid_disks
;
737 case ALGORITHM_RIGHT_SYMMETRIC
:
738 *pd_idx
= stripe
% raid_disks
;
739 *dd_idx
= (*pd_idx
+ 1 + *dd_idx
) % raid_disks
;
742 printk(KERN_ERR
"raid5: unsupported algorithm %d\n",
749 switch (conf
->algorithm
) {
750 case ALGORITHM_LEFT_ASYMMETRIC
:
751 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
752 if (*pd_idx
== raid_disks
-1)
753 (*dd_idx
)++; /* Q D D D P */
754 else if (*dd_idx
>= *pd_idx
)
755 (*dd_idx
) += 2; /* D D P Q D */
757 case ALGORITHM_RIGHT_ASYMMETRIC
:
758 *pd_idx
= stripe
% raid_disks
;
759 if (*pd_idx
== raid_disks
-1)
760 (*dd_idx
)++; /* Q D D D P */
761 else if (*dd_idx
>= *pd_idx
)
762 (*dd_idx
) += 2; /* D D P Q D */
764 case ALGORITHM_LEFT_SYMMETRIC
:
765 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
766 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
768 case ALGORITHM_RIGHT_SYMMETRIC
:
769 *pd_idx
= stripe
% raid_disks
;
770 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
773 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
780 * Finally, compute the new sector number
782 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
787 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
)
789 raid5_conf_t
*conf
= sh
->raid_conf
;
790 int raid_disks
= sh
->disks
;
791 int data_disks
= raid_disks
- conf
->max_degraded
;
792 sector_t new_sector
= sh
->sector
, check
;
793 int sectors_per_chunk
= conf
->chunk_size
>> 9;
796 int chunk_number
, dummy1
, dummy2
, dd_idx
= i
;
800 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
802 BUG_ON(new_sector
!= stripe
);
806 switch(conf
->level
) {
809 switch (conf
->algorithm
) {
810 case ALGORITHM_LEFT_ASYMMETRIC
:
811 case ALGORITHM_RIGHT_ASYMMETRIC
:
815 case ALGORITHM_LEFT_SYMMETRIC
:
816 case ALGORITHM_RIGHT_SYMMETRIC
:
819 i
-= (sh
->pd_idx
+ 1);
822 printk(KERN_ERR
"raid5: unsupported algorithm %d\n",
827 if (i
== raid6_next_disk(sh
->pd_idx
, raid_disks
))
828 return 0; /* It is the Q disk */
829 switch (conf
->algorithm
) {
830 case ALGORITHM_LEFT_ASYMMETRIC
:
831 case ALGORITHM_RIGHT_ASYMMETRIC
:
832 if (sh
->pd_idx
== raid_disks
-1)
834 else if (i
> sh
->pd_idx
)
835 i
-= 2; /* D D P Q D */
837 case ALGORITHM_LEFT_SYMMETRIC
:
838 case ALGORITHM_RIGHT_SYMMETRIC
:
839 if (sh
->pd_idx
== raid_disks
-1)
845 i
-= (sh
->pd_idx
+ 2);
849 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
855 chunk_number
= stripe
* data_disks
+ i
;
856 r_sector
= (sector_t
)chunk_number
* sectors_per_chunk
+ chunk_offset
;
858 check
= raid5_compute_sector (r_sector
, raid_disks
, data_disks
, &dummy1
, &dummy2
, conf
);
859 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| dummy2
!= sh
->pd_idx
) {
860 printk(KERN_ERR
"compute_blocknr: map not correct\n");
869 * Copy data between a page in the stripe cache, and one or more bion
870 * The page could align with the middle of the bio, or there could be
871 * several bion, each with several bio_vecs, which cover part of the page
872 * Multiple bion are linked together on bi_next. There may be extras
873 * at the end of this list. We ignore them.
875 static void copy_data(int frombio
, struct bio
*bio
,
879 char *pa
= page_address(page
);
884 if (bio
->bi_sector
>= sector
)
885 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
887 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
888 bio_for_each_segment(bvl
, bio
, i
) {
889 int len
= bio_iovec_idx(bio
,i
)->bv_len
;
893 if (page_offset
< 0) {
894 b_offset
= -page_offset
;
895 page_offset
+= b_offset
;
899 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
900 clen
= STRIPE_SIZE
- page_offset
;
904 char *ba
= __bio_kmap_atomic(bio
, i
, KM_USER0
);
906 memcpy(pa
+page_offset
, ba
+b_offset
, clen
);
908 memcpy(ba
+b_offset
, pa
+page_offset
, clen
);
909 __bio_kunmap_atomic(ba
, KM_USER0
);
911 if (clen
< len
) /* hit end of page */
917 #define check_xor() do { \
918 if (count == MAX_XOR_BLOCKS) { \
919 xor_block(count, STRIPE_SIZE, ptr); \
925 static void compute_block(struct stripe_head
*sh
, int dd_idx
)
927 int i
, count
, disks
= sh
->disks
;
928 void *ptr
[MAX_XOR_BLOCKS
], *p
;
930 PRINTK("compute_block, stripe %llu, idx %d\n",
931 (unsigned long long)sh
->sector
, dd_idx
);
933 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
934 memset(ptr
[0], 0, STRIPE_SIZE
);
936 for (i
= disks
; i
--; ) {
939 p
= page_address(sh
->dev
[i
].page
);
940 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
943 printk(KERN_ERR
"compute_block() %d, stripe %llu, %d"
944 " not present\n", dd_idx
,
945 (unsigned long long)sh
->sector
, i
);
950 xor_block(count
, STRIPE_SIZE
, ptr
);
951 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
954 static void compute_parity5(struct stripe_head
*sh
, int method
)
956 raid5_conf_t
*conf
= sh
->raid_conf
;
957 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
, count
;
958 void *ptr
[MAX_XOR_BLOCKS
];
961 PRINTK("compute_parity5, stripe %llu, method %d\n",
962 (unsigned long long)sh
->sector
, method
);
965 ptr
[0] = page_address(sh
->dev
[pd_idx
].page
);
967 case READ_MODIFY_WRITE
:
968 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
));
969 for (i
=disks
; i
-- ;) {
972 if (sh
->dev
[i
].towrite
&&
973 test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
)) {
974 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
975 chosen
= sh
->dev
[i
].towrite
;
976 sh
->dev
[i
].towrite
= NULL
;
978 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
979 wake_up(&conf
->wait_for_overlap
);
981 BUG_ON(sh
->dev
[i
].written
);
982 sh
->dev
[i
].written
= chosen
;
987 case RECONSTRUCT_WRITE
:
988 memset(ptr
[0], 0, STRIPE_SIZE
);
989 for (i
= disks
; i
-- ;)
990 if (i
!=pd_idx
&& sh
->dev
[i
].towrite
) {
991 chosen
= sh
->dev
[i
].towrite
;
992 sh
->dev
[i
].towrite
= NULL
;
994 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
995 wake_up(&conf
->wait_for_overlap
);
997 BUG_ON(sh
->dev
[i
].written
);
998 sh
->dev
[i
].written
= chosen
;
1005 xor_block(count
, STRIPE_SIZE
, ptr
);
1009 for (i
= disks
; i
--;)
1010 if (sh
->dev
[i
].written
) {
1011 sector_t sector
= sh
->dev
[i
].sector
;
1012 struct bio
*wbi
= sh
->dev
[i
].written
;
1013 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
1014 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
1015 wbi
= r5_next_bio(wbi
, sector
);
1018 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1019 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1023 case RECONSTRUCT_WRITE
:
1027 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
1031 case READ_MODIFY_WRITE
:
1032 for (i
= disks
; i
--;)
1033 if (sh
->dev
[i
].written
) {
1034 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
1039 xor_block(count
, STRIPE_SIZE
, ptr
);
1041 if (method
!= CHECK_PARITY
) {
1042 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1043 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
1045 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1048 static void compute_parity6(struct stripe_head
*sh
, int method
)
1050 raid6_conf_t
*conf
= sh
->raid_conf
;
1051 int i
, pd_idx
= sh
->pd_idx
, qd_idx
, d0_idx
, disks
= conf
->raid_disks
, count
;
1053 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1056 qd_idx
= raid6_next_disk(pd_idx
, disks
);
1057 d0_idx
= raid6_next_disk(qd_idx
, disks
);
1059 PRINTK("compute_parity, stripe %llu, method %d\n",
1060 (unsigned long long)sh
->sector
, method
);
1063 case READ_MODIFY_WRITE
:
1064 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1065 case RECONSTRUCT_WRITE
:
1066 for (i
= disks
; i
-- ;)
1067 if ( i
!= pd_idx
&& i
!= qd_idx
&& sh
->dev
[i
].towrite
) {
1068 chosen
= sh
->dev
[i
].towrite
;
1069 sh
->dev
[i
].towrite
= NULL
;
1071 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1072 wake_up(&conf
->wait_for_overlap
);
1074 BUG_ON(sh
->dev
[i
].written
);
1075 sh
->dev
[i
].written
= chosen
;
1079 BUG(); /* Not implemented yet */
1082 for (i
= disks
; i
--;)
1083 if (sh
->dev
[i
].written
) {
1084 sector_t sector
= sh
->dev
[i
].sector
;
1085 struct bio
*wbi
= sh
->dev
[i
].written
;
1086 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
1087 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
1088 wbi
= r5_next_bio(wbi
, sector
);
1091 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1092 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1096 // case RECONSTRUCT_WRITE:
1097 // case CHECK_PARITY:
1098 // case UPDATE_PARITY:
1099 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1100 /* FIX: Is this ordering of drives even remotely optimal? */
1104 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
1105 if (count
<= disks
-2 && !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
1106 printk("block %d/%d not uptodate on parity calc\n", i
,count
);
1107 i
= raid6_next_disk(i
, disks
);
1108 } while ( i
!= d0_idx
);
1112 raid6_call
.gen_syndrome(disks
, STRIPE_SIZE
, ptrs
);
1115 case RECONSTRUCT_WRITE
:
1116 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1117 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
1118 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
1119 set_bit(R5_LOCKED
, &sh
->dev
[qd_idx
].flags
);
1122 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1123 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
1129 /* Compute one missing block */
1130 static void compute_block_1(struct stripe_head
*sh
, int dd_idx
, int nozero
)
1132 raid6_conf_t
*conf
= sh
->raid_conf
;
1133 int i
, count
, disks
= conf
->raid_disks
;
1134 void *ptr
[MAX_XOR_BLOCKS
], *p
;
1135 int pd_idx
= sh
->pd_idx
;
1136 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1138 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1139 (unsigned long long)sh
->sector
, dd_idx
);
1141 if ( dd_idx
== qd_idx
) {
1142 /* We're actually computing the Q drive */
1143 compute_parity6(sh
, UPDATE_PARITY
);
1145 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
1146 if (!nozero
) memset(ptr
[0], 0, STRIPE_SIZE
);
1148 for (i
= disks
; i
--; ) {
1149 if (i
== dd_idx
|| i
== qd_idx
)
1151 p
= page_address(sh
->dev
[i
].page
);
1152 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
1155 printk("compute_block() %d, stripe %llu, %d"
1156 " not present\n", dd_idx
,
1157 (unsigned long long)sh
->sector
, i
);
1162 xor_block(count
, STRIPE_SIZE
, ptr
);
1163 if (!nozero
) set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
1164 else clear_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
1168 /* Compute two missing blocks */
1169 static void compute_block_2(struct stripe_head
*sh
, int dd_idx1
, int dd_idx2
)
1171 raid6_conf_t
*conf
= sh
->raid_conf
;
1172 int i
, count
, disks
= conf
->raid_disks
;
1173 int pd_idx
= sh
->pd_idx
;
1174 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1175 int d0_idx
= raid6_next_disk(qd_idx
, disks
);
1178 /* faila and failb are disk numbers relative to d0_idx */
1179 /* pd_idx become disks-2 and qd_idx become disks-1 */
1180 faila
= (dd_idx1
< d0_idx
) ? dd_idx1
+(disks
-d0_idx
) : dd_idx1
-d0_idx
;
1181 failb
= (dd_idx2
< d0_idx
) ? dd_idx2
+(disks
-d0_idx
) : dd_idx2
-d0_idx
;
1183 BUG_ON(faila
== failb
);
1184 if ( failb
< faila
) { int tmp
= faila
; faila
= failb
; failb
= tmp
; }
1186 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1187 (unsigned long long)sh
->sector
, dd_idx1
, dd_idx2
, faila
, failb
);
1189 if ( failb
== disks
-1 ) {
1190 /* Q disk is one of the missing disks */
1191 if ( faila
== disks
-2 ) {
1192 /* Missing P+Q, just recompute */
1193 compute_parity6(sh
, UPDATE_PARITY
);
1196 /* We're missing D+Q; recompute D from P */
1197 compute_block_1(sh
, (dd_idx1
== qd_idx
) ? dd_idx2
: dd_idx1
, 0);
1198 compute_parity6(sh
, UPDATE_PARITY
); /* Is this necessary? */
1203 /* We're missing D+P or D+D; build pointer table */
1205 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1211 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
1212 i
= raid6_next_disk(i
, disks
);
1213 if (i
!= dd_idx1
&& i
!= dd_idx2
&&
1214 !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
1215 printk("compute_2 with missing block %d/%d\n", count
, i
);
1216 } while ( i
!= d0_idx
);
1218 if ( failb
== disks
-2 ) {
1219 /* We're missing D+P. */
1220 raid6_datap_recov(disks
, STRIPE_SIZE
, faila
, ptrs
);
1222 /* We're missing D+D. */
1223 raid6_2data_recov(disks
, STRIPE_SIZE
, faila
, failb
, ptrs
);
1226 /* Both the above update both missing blocks */
1227 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx1
].flags
);
1228 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx2
].flags
);
1235 * Each stripe/dev can have one or more bion attached.
1236 * toread/towrite point to the first in a chain.
1237 * The bi_next chain must be in order.
1239 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
1242 raid5_conf_t
*conf
= sh
->raid_conf
;
1245 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1246 (unsigned long long)bi
->bi_sector
,
1247 (unsigned long long)sh
->sector
);
1250 spin_lock(&sh
->lock
);
1251 spin_lock_irq(&conf
->device_lock
);
1253 bip
= &sh
->dev
[dd_idx
].towrite
;
1254 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
1257 bip
= &sh
->dev
[dd_idx
].toread
;
1258 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
1259 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
1261 bip
= & (*bip
)->bi_next
;
1263 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
1266 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
1270 bi
->bi_phys_segments
++;
1271 spin_unlock_irq(&conf
->device_lock
);
1272 spin_unlock(&sh
->lock
);
1274 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1275 (unsigned long long)bi
->bi_sector
,
1276 (unsigned long long)sh
->sector
, dd_idx
);
1278 if (conf
->mddev
->bitmap
&& firstwrite
) {
1279 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
1281 sh
->bm_seq
= conf
->seq_flush
+1;
1282 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
1286 /* check if page is covered */
1287 sector_t sector
= sh
->dev
[dd_idx
].sector
;
1288 for (bi
=sh
->dev
[dd_idx
].towrite
;
1289 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
1290 bi
&& bi
->bi_sector
<= sector
;
1291 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
1292 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
1293 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1295 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
1296 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
1301 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
1302 spin_unlock_irq(&conf
->device_lock
);
1303 spin_unlock(&sh
->lock
);
1307 static void end_reshape(raid5_conf_t
*conf
);
1309 static int page_is_zero(struct page
*p
)
1311 char *a
= page_address(p
);
1312 return ((*(u32
*)a
) == 0 &&
1313 memcmp(a
, a
+4, STRIPE_SIZE
-4)==0);
1316 static int stripe_to_pdidx(sector_t stripe
, raid5_conf_t
*conf
, int disks
)
1318 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1320 int chunk_offset
= sector_div(stripe
, sectors_per_chunk
);
1322 raid5_compute_sector(stripe
* (disks
- conf
->max_degraded
)
1323 *sectors_per_chunk
+ chunk_offset
,
1324 disks
, disks
- conf
->max_degraded
,
1325 &dd_idx
, &pd_idx
, conf
);
1331 * handle_stripe - do things to a stripe.
1333 * We lock the stripe and then examine the state of various bits
1334 * to see what needs to be done.
1336 * return some read request which now have data
1337 * return some write requests which are safely on disc
1338 * schedule a read on some buffers
1339 * schedule a write of some buffers
1340 * return confirmation of parity correctness
1342 * Parity calculations are done inside the stripe lock
1343 * buffers are taken off read_list or write_list, and bh_cache buffers
1344 * get BH_Lock set before the stripe lock is released.
1348 static void handle_stripe5(struct stripe_head
*sh
)
1350 raid5_conf_t
*conf
= sh
->raid_conf
;
1351 int disks
= sh
->disks
;
1352 struct bio
*return_bi
= NULL
;
1355 int syncing
, expanding
, expanded
;
1356 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
1357 int non_overwrite
= 0;
1361 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1362 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
),
1365 spin_lock(&sh
->lock
);
1366 clear_bit(STRIPE_HANDLE
, &sh
->state
);
1367 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1369 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
1370 expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
1371 expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
1372 /* Now to look around and see what can be done */
1375 for (i
=disks
; i
--; ) {
1378 clear_bit(R5_Insync
, &dev
->flags
);
1380 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1381 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
1382 /* maybe we can reply to a read */
1383 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
1384 struct bio
*rbi
, *rbi2
;
1385 PRINTK("Return read for disc %d\n", i
);
1386 spin_lock_irq(&conf
->device_lock
);
1389 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1390 wake_up(&conf
->wait_for_overlap
);
1391 spin_unlock_irq(&conf
->device_lock
);
1392 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1393 copy_data(0, rbi
, dev
->page
, dev
->sector
);
1394 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1395 spin_lock_irq(&conf
->device_lock
);
1396 if (--rbi
->bi_phys_segments
== 0) {
1397 rbi
->bi_next
= return_bi
;
1400 spin_unlock_irq(&conf
->device_lock
);
1405 /* now count some things */
1406 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
1407 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
1410 if (dev
->toread
) to_read
++;
1413 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1416 if (dev
->written
) written
++;
1417 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1418 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1419 /* The ReadError flag will just be confusing now */
1420 clear_bit(R5_ReadError
, &dev
->flags
);
1421 clear_bit(R5_ReWrite
, &dev
->flags
);
1423 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1424 || test_bit(R5_ReadError
, &dev
->flags
)) {
1428 set_bit(R5_Insync
, &dev
->flags
);
1431 PRINTK("locked=%d uptodate=%d to_read=%d"
1432 " to_write=%d failed=%d failed_num=%d\n",
1433 locked
, uptodate
, to_read
, to_write
, failed
, failed_num
);
1434 /* check if the array has lost two devices and, if so, some requests might
1437 if (failed
> 1 && to_read
+to_write
+written
) {
1438 for (i
=disks
; i
--; ) {
1441 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1444 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1445 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1446 /* multiple read failures in one stripe */
1447 md_error(conf
->mddev
, rdev
);
1451 spin_lock_irq(&conf
->device_lock
);
1452 /* fail all writes first */
1453 bi
= sh
->dev
[i
].towrite
;
1454 sh
->dev
[i
].towrite
= NULL
;
1455 if (bi
) { to_write
--; bitmap_end
= 1; }
1457 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1458 wake_up(&conf
->wait_for_overlap
);
1460 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1461 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1462 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1463 if (--bi
->bi_phys_segments
== 0) {
1464 md_write_end(conf
->mddev
);
1465 bi
->bi_next
= return_bi
;
1470 /* and fail all 'written' */
1471 bi
= sh
->dev
[i
].written
;
1472 sh
->dev
[i
].written
= NULL
;
1473 if (bi
) bitmap_end
= 1;
1474 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
1475 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1476 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1477 if (--bi
->bi_phys_segments
== 0) {
1478 md_write_end(conf
->mddev
);
1479 bi
->bi_next
= return_bi
;
1485 /* fail any reads if this device is non-operational */
1486 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
1487 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1488 bi
= sh
->dev
[i
].toread
;
1489 sh
->dev
[i
].toread
= NULL
;
1490 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1491 wake_up(&conf
->wait_for_overlap
);
1493 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1494 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1495 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1496 if (--bi
->bi_phys_segments
== 0) {
1497 bi
->bi_next
= return_bi
;
1503 spin_unlock_irq(&conf
->device_lock
);
1505 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1506 STRIPE_SECTORS
, 0, 0);
1509 if (failed
> 1 && syncing
) {
1510 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
1511 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1515 /* might be able to return some write requests if the parity block
1516 * is safe, or on a failed drive
1518 dev
= &sh
->dev
[sh
->pd_idx
];
1520 ( (test_bit(R5_Insync
, &dev
->flags
) && !test_bit(R5_LOCKED
, &dev
->flags
) &&
1521 test_bit(R5_UPTODATE
, &dev
->flags
))
1522 || (failed
== 1 && failed_num
== sh
->pd_idx
))
1524 /* any written block on an uptodate or failed drive can be returned.
1525 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1526 * never LOCKED, so we don't need to test 'failed' directly.
1528 for (i
=disks
; i
--; )
1529 if (sh
->dev
[i
].written
) {
1531 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
1532 test_bit(R5_UPTODATE
, &dev
->flags
) ) {
1533 /* We can return any write requests */
1534 struct bio
*wbi
, *wbi2
;
1536 PRINTK("Return write for disc %d\n", i
);
1537 spin_lock_irq(&conf
->device_lock
);
1539 dev
->written
= NULL
;
1540 while (wbi
&& wbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1541 wbi2
= r5_next_bio(wbi
, dev
->sector
);
1542 if (--wbi
->bi_phys_segments
== 0) {
1543 md_write_end(conf
->mddev
);
1544 wbi
->bi_next
= return_bi
;
1549 if (dev
->towrite
== NULL
)
1551 spin_unlock_irq(&conf
->device_lock
);
1553 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1555 !test_bit(STRIPE_DEGRADED
, &sh
->state
), 0);
1560 /* Now we might consider reading some blocks, either to check/generate
1561 * parity, or to satisfy requests
1562 * or to load a block that is being partially written.
1564 if (to_read
|| non_overwrite
|| (syncing
&& (uptodate
< disks
)) || expanding
) {
1565 for (i
=disks
; i
--;) {
1567 if (!test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1569 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
1572 (failed
&& (sh
->dev
[failed_num
].toread
||
1573 (sh
->dev
[failed_num
].towrite
&& !test_bit(R5_OVERWRITE
, &sh
->dev
[failed_num
].flags
))))
1576 /* we would like to get this block, possibly
1577 * by computing it, but we might not be able to
1579 if (uptodate
== disks
-1) {
1580 PRINTK("Computing block %d\n", i
);
1581 compute_block(sh
, i
);
1583 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
1584 set_bit(R5_LOCKED
, &dev
->flags
);
1585 set_bit(R5_Wantread
, &dev
->flags
);
1587 PRINTK("Reading block %d (sync=%d)\n",
1592 set_bit(STRIPE_HANDLE
, &sh
->state
);
1595 /* now to consider writing and what else, if anything should be read */
1598 for (i
=disks
; i
--;) {
1599 /* would I have to read this buffer for read_modify_write */
1601 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
1602 (!test_bit(R5_LOCKED
, &dev
->flags
)
1604 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1605 if (test_bit(R5_Insync
, &dev
->flags
)
1606 /* && !(!mddev->insync && i == sh->pd_idx) */
1609 else rmw
+= 2*disks
; /* cannot read it */
1611 /* Would I have to read this buffer for reconstruct_write */
1612 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
1613 (!test_bit(R5_LOCKED
, &dev
->flags
)
1615 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1616 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
1617 else rcw
+= 2*disks
;
1620 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1621 (unsigned long long)sh
->sector
, rmw
, rcw
);
1622 set_bit(STRIPE_HANDLE
, &sh
->state
);
1623 if (rmw
< rcw
&& rmw
> 0)
1624 /* prefer read-modify-write, but need to get some data */
1625 for (i
=disks
; i
--;) {
1627 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
1628 !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1629 test_bit(R5_Insync
, &dev
->flags
)) {
1630 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1632 PRINTK("Read_old block %d for r-m-w\n", i
);
1633 set_bit(R5_LOCKED
, &dev
->flags
);
1634 set_bit(R5_Wantread
, &dev
->flags
);
1637 set_bit(STRIPE_DELAYED
, &sh
->state
);
1638 set_bit(STRIPE_HANDLE
, &sh
->state
);
1642 if (rcw
<= rmw
&& rcw
> 0)
1643 /* want reconstruct write, but need to get some data */
1644 for (i
=disks
; i
--;) {
1646 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
1647 !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1648 test_bit(R5_Insync
, &dev
->flags
)) {
1649 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1651 PRINTK("Read_old block %d for Reconstruct\n", i
);
1652 set_bit(R5_LOCKED
, &dev
->flags
);
1653 set_bit(R5_Wantread
, &dev
->flags
);
1656 set_bit(STRIPE_DELAYED
, &sh
->state
);
1657 set_bit(STRIPE_HANDLE
, &sh
->state
);
1661 /* now if nothing is locked, and if we have enough data, we can start a write request */
1662 if (locked
== 0 && (rcw
== 0 ||rmw
== 0) &&
1663 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
1664 PRINTK("Computing parity...\n");
1665 compute_parity5(sh
, rcw
==0 ? RECONSTRUCT_WRITE
: READ_MODIFY_WRITE
);
1666 /* now every locked buffer is ready to be written */
1668 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
1669 PRINTK("Writing block %d\n", i
);
1671 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1672 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
)
1673 || (i
==sh
->pd_idx
&& failed
== 0))
1674 set_bit(STRIPE_INSYNC
, &sh
->state
);
1676 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
1677 atomic_dec(&conf
->preread_active_stripes
);
1678 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
1679 md_wakeup_thread(conf
->mddev
->thread
);
1684 /* maybe we need to check and possibly fix the parity for this stripe
1685 * Any reads will already have been scheduled, so we just see if enough data
1688 if (syncing
&& locked
== 0 &&
1689 !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1690 set_bit(STRIPE_HANDLE
, &sh
->state
);
1692 BUG_ON(uptodate
!= disks
);
1693 compute_parity5(sh
, CHECK_PARITY
);
1695 if (page_is_zero(sh
->dev
[sh
->pd_idx
].page
)) {
1696 /* parity is correct (on disc, not in buffer any more) */
1697 set_bit(STRIPE_INSYNC
, &sh
->state
);
1699 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
1700 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
1701 /* don't try to repair!! */
1702 set_bit(STRIPE_INSYNC
, &sh
->state
);
1704 compute_block(sh
, sh
->pd_idx
);
1709 if (!test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1710 /* either failed parity check, or recovery is happening */
1712 failed_num
= sh
->pd_idx
;
1713 dev
= &sh
->dev
[failed_num
];
1714 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
1715 BUG_ON(uptodate
!= disks
);
1717 set_bit(R5_LOCKED
, &dev
->flags
);
1718 set_bit(R5_Wantwrite
, &dev
->flags
);
1719 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
1721 set_bit(STRIPE_INSYNC
, &sh
->state
);
1724 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1725 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
1726 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1729 /* If the failed drive is just a ReadError, then we might need to progress
1730 * the repair/check process
1732 if (failed
== 1 && ! conf
->mddev
->ro
&&
1733 test_bit(R5_ReadError
, &sh
->dev
[failed_num
].flags
)
1734 && !test_bit(R5_LOCKED
, &sh
->dev
[failed_num
].flags
)
1735 && test_bit(R5_UPTODATE
, &sh
->dev
[failed_num
].flags
)
1737 dev
= &sh
->dev
[failed_num
];
1738 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
1739 set_bit(R5_Wantwrite
, &dev
->flags
);
1740 set_bit(R5_ReWrite
, &dev
->flags
);
1741 set_bit(R5_LOCKED
, &dev
->flags
);
1744 /* let's read it back */
1745 set_bit(R5_Wantread
, &dev
->flags
);
1746 set_bit(R5_LOCKED
, &dev
->flags
);
1751 if (expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
1752 /* Need to write out all blocks after computing parity */
1753 sh
->disks
= conf
->raid_disks
;
1754 sh
->pd_idx
= stripe_to_pdidx(sh
->sector
, conf
, conf
->raid_disks
);
1755 compute_parity5(sh
, RECONSTRUCT_WRITE
);
1756 for (i
= conf
->raid_disks
; i
--;) {
1757 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1759 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1761 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
1762 } else if (expanded
) {
1763 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
1764 atomic_dec(&conf
->reshape_stripes
);
1765 wake_up(&conf
->wait_for_overlap
);
1766 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
1769 if (expanding
&& locked
== 0) {
1770 /* We have read all the blocks in this stripe and now we need to
1771 * copy some of them into a target stripe for expand.
1773 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
1774 for (i
=0; i
< sh
->disks
; i
++)
1775 if (i
!= sh
->pd_idx
) {
1776 int dd_idx
, pd_idx
, j
;
1777 struct stripe_head
*sh2
;
1779 sector_t bn
= compute_blocknr(sh
, i
);
1780 sector_t s
= raid5_compute_sector(bn
, conf
->raid_disks
,
1782 &dd_idx
, &pd_idx
, conf
);
1783 sh2
= get_active_stripe(conf
, s
, conf
->raid_disks
, pd_idx
, 1);
1785 /* so far only the early blocks of this stripe
1786 * have been requested. When later blocks
1787 * get requested, we will try again
1790 if(!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
1791 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
1792 /* must have already done this block */
1793 release_stripe(sh2
);
1796 memcpy(page_address(sh2
->dev
[dd_idx
].page
),
1797 page_address(sh
->dev
[i
].page
),
1799 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
1800 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
1801 for (j
=0; j
<conf
->raid_disks
; j
++)
1802 if (j
!= sh2
->pd_idx
&&
1803 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
1805 if (j
== conf
->raid_disks
) {
1806 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
1807 set_bit(STRIPE_HANDLE
, &sh2
->state
);
1809 release_stripe(sh2
);
1813 spin_unlock(&sh
->lock
);
1815 while ((bi
=return_bi
)) {
1816 int bytes
= bi
->bi_size
;
1818 return_bi
= bi
->bi_next
;
1821 bi
->bi_end_io(bi
, bytes
,
1822 test_bit(BIO_UPTODATE
, &bi
->bi_flags
)
1825 for (i
=disks
; i
-- ;) {
1829 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
1831 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
1836 bi
= &sh
->dev
[i
].req
;
1840 bi
->bi_end_io
= raid5_end_write_request
;
1842 bi
->bi_end_io
= raid5_end_read_request
;
1845 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1846 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
1849 atomic_inc(&rdev
->nr_pending
);
1853 if (syncing
|| expanding
|| expanded
)
1854 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
1856 bi
->bi_bdev
= rdev
->bdev
;
1857 PRINTK("for %llu schedule op %ld on disc %d\n",
1858 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
1859 atomic_inc(&sh
->count
);
1860 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
1861 bi
->bi_flags
= 1 << BIO_UPTODATE
;
1863 bi
->bi_max_vecs
= 1;
1865 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
1866 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1867 bi
->bi_io_vec
[0].bv_offset
= 0;
1868 bi
->bi_size
= STRIPE_SIZE
;
1871 test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1872 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1873 generic_make_request(bi
);
1876 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1877 PRINTK("skip op %ld on disc %d for sector %llu\n",
1878 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
1879 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1880 set_bit(STRIPE_HANDLE
, &sh
->state
);
1885 static void handle_stripe6(struct stripe_head
*sh
, struct page
*tmp_page
)
1887 raid6_conf_t
*conf
= sh
->raid_conf
;
1888 int disks
= conf
->raid_disks
;
1889 struct bio
*return_bi
= NULL
;
1893 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
1894 int non_overwrite
= 0;
1895 int failed_num
[2] = {0, 0};
1896 struct r5dev
*dev
, *pdev
, *qdev
;
1897 int pd_idx
= sh
->pd_idx
;
1898 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1899 int p_failed
, q_failed
;
1901 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1902 (unsigned long long)sh
->sector
, sh
->state
, atomic_read(&sh
->count
),
1905 spin_lock(&sh
->lock
);
1906 clear_bit(STRIPE_HANDLE
, &sh
->state
);
1907 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1909 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
1910 /* Now to look around and see what can be done */
1913 for (i
=disks
; i
--; ) {
1916 clear_bit(R5_Insync
, &dev
->flags
);
1918 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1919 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
1920 /* maybe we can reply to a read */
1921 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
1922 struct bio
*rbi
, *rbi2
;
1923 PRINTK("Return read for disc %d\n", i
);
1924 spin_lock_irq(&conf
->device_lock
);
1927 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1928 wake_up(&conf
->wait_for_overlap
);
1929 spin_unlock_irq(&conf
->device_lock
);
1930 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1931 copy_data(0, rbi
, dev
->page
, dev
->sector
);
1932 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1933 spin_lock_irq(&conf
->device_lock
);
1934 if (--rbi
->bi_phys_segments
== 0) {
1935 rbi
->bi_next
= return_bi
;
1938 spin_unlock_irq(&conf
->device_lock
);
1943 /* now count some things */
1944 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
1945 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
1948 if (dev
->toread
) to_read
++;
1951 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1954 if (dev
->written
) written
++;
1955 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1956 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1957 /* The ReadError flag will just be confusing now */
1958 clear_bit(R5_ReadError
, &dev
->flags
);
1959 clear_bit(R5_ReWrite
, &dev
->flags
);
1961 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1962 || test_bit(R5_ReadError
, &dev
->flags
)) {
1964 failed_num
[failed
] = i
;
1967 set_bit(R5_Insync
, &dev
->flags
);
1970 PRINTK("locked=%d uptodate=%d to_read=%d"
1971 " to_write=%d failed=%d failed_num=%d,%d\n",
1972 locked
, uptodate
, to_read
, to_write
, failed
,
1973 failed_num
[0], failed_num
[1]);
1974 /* check if the array has lost >2 devices and, if so, some requests might
1977 if (failed
> 2 && to_read
+to_write
+written
) {
1978 for (i
=disks
; i
--; ) {
1981 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1984 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1985 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1986 /* multiple read failures in one stripe */
1987 md_error(conf
->mddev
, rdev
);
1991 spin_lock_irq(&conf
->device_lock
);
1992 /* fail all writes first */
1993 bi
= sh
->dev
[i
].towrite
;
1994 sh
->dev
[i
].towrite
= NULL
;
1995 if (bi
) { to_write
--; bitmap_end
= 1; }
1997 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1998 wake_up(&conf
->wait_for_overlap
);
2000 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
2001 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2002 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2003 if (--bi
->bi_phys_segments
== 0) {
2004 md_write_end(conf
->mddev
);
2005 bi
->bi_next
= return_bi
;
2010 /* and fail all 'written' */
2011 bi
= sh
->dev
[i
].written
;
2012 sh
->dev
[i
].written
= NULL
;
2013 if (bi
) bitmap_end
= 1;
2014 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2015 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2016 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2017 if (--bi
->bi_phys_segments
== 0) {
2018 md_write_end(conf
->mddev
);
2019 bi
->bi_next
= return_bi
;
2025 /* fail any reads if this device is non-operational */
2026 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2027 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2028 bi
= sh
->dev
[i
].toread
;
2029 sh
->dev
[i
].toread
= NULL
;
2030 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2031 wake_up(&conf
->wait_for_overlap
);
2033 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
2034 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2035 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2036 if (--bi
->bi_phys_segments
== 0) {
2037 bi
->bi_next
= return_bi
;
2043 spin_unlock_irq(&conf
->device_lock
);
2045 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2046 STRIPE_SECTORS
, 0, 0);
2049 if (failed
> 2 && syncing
) {
2050 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
2051 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2056 * might be able to return some write requests if the parity blocks
2057 * are safe, or on a failed drive
2059 pdev
= &sh
->dev
[pd_idx
];
2060 p_failed
= (failed
>= 1 && failed_num
[0] == pd_idx
)
2061 || (failed
>= 2 && failed_num
[1] == pd_idx
);
2062 qdev
= &sh
->dev
[qd_idx
];
2063 q_failed
= (failed
>= 1 && failed_num
[0] == qd_idx
)
2064 || (failed
>= 2 && failed_num
[1] == qd_idx
);
2067 ( p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
2068 && !test_bit(R5_LOCKED
, &pdev
->flags
)
2069 && test_bit(R5_UPTODATE
, &pdev
->flags
))) ) &&
2070 ( q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
2071 && !test_bit(R5_LOCKED
, &qdev
->flags
)
2072 && test_bit(R5_UPTODATE
, &qdev
->flags
))) ) ) {
2073 /* any written block on an uptodate or failed drive can be
2074 * returned. Note that if we 'wrote' to a failed drive,
2075 * it will be UPTODATE, but never LOCKED, so we don't need
2076 * to test 'failed' directly.
2078 for (i
=disks
; i
--; )
2079 if (sh
->dev
[i
].written
) {
2081 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2082 test_bit(R5_UPTODATE
, &dev
->flags
) ) {
2083 /* We can return any write requests */
2085 struct bio
*wbi
, *wbi2
;
2086 PRINTK("Return write for stripe %llu disc %d\n",
2087 (unsigned long long)sh
->sector
, i
);
2088 spin_lock_irq(&conf
->device_lock
);
2090 dev
->written
= NULL
;
2091 while (wbi
&& wbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
2092 wbi2
= r5_next_bio(wbi
, dev
->sector
);
2093 if (--wbi
->bi_phys_segments
== 0) {
2094 md_write_end(conf
->mddev
);
2095 wbi
->bi_next
= return_bi
;
2100 if (dev
->towrite
== NULL
)
2102 spin_unlock_irq(&conf
->device_lock
);
2104 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2106 !test_bit(STRIPE_DEGRADED
, &sh
->state
), 0);
2111 /* Now we might consider reading some blocks, either to check/generate
2112 * parity, or to satisfy requests
2113 * or to load a block that is being partially written.
2115 if (to_read
|| non_overwrite
|| (to_write
&& failed
) || (syncing
&& (uptodate
< disks
))) {
2116 for (i
=disks
; i
--;) {
2118 if (!test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2120 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2122 (failed
>= 1 && (sh
->dev
[failed_num
[0]].toread
|| to_write
)) ||
2123 (failed
>= 2 && (sh
->dev
[failed_num
[1]].toread
|| to_write
))
2126 /* we would like to get this block, possibly
2127 * by computing it, but we might not be able to
2129 if (uptodate
== disks
-1) {
2130 PRINTK("Computing stripe %llu block %d\n",
2131 (unsigned long long)sh
->sector
, i
);
2132 compute_block_1(sh
, i
, 0);
2134 } else if ( uptodate
== disks
-2 && failed
>= 2 ) {
2135 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2137 for (other
=disks
; other
--;) {
2140 if ( !test_bit(R5_UPTODATE
, &sh
->dev
[other
].flags
) )
2144 PRINTK("Computing stripe %llu blocks %d,%d\n",
2145 (unsigned long long)sh
->sector
, i
, other
);
2146 compute_block_2(sh
, i
, other
);
2148 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2149 set_bit(R5_LOCKED
, &dev
->flags
);
2150 set_bit(R5_Wantread
, &dev
->flags
);
2152 PRINTK("Reading block %d (sync=%d)\n",
2157 set_bit(STRIPE_HANDLE
, &sh
->state
);
2160 /* now to consider writing and what else, if anything should be read */
2162 int rcw
=0, must_compute
=0;
2163 for (i
=disks
; i
--;) {
2165 /* Would I have to read this buffer for reconstruct_write */
2166 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
2167 && i
!= pd_idx
&& i
!= qd_idx
2168 && (!test_bit(R5_LOCKED
, &dev
->flags
)
2170 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
2171 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2173 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i
, dev
->flags
);
2178 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2179 (unsigned long long)sh
->sector
, rcw
, must_compute
);
2180 set_bit(STRIPE_HANDLE
, &sh
->state
);
2183 /* want reconstruct write, but need to get some data */
2184 for (i
=disks
; i
--;) {
2186 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
2187 && !(failed
== 0 && (i
== pd_idx
|| i
== qd_idx
))
2188 && !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2189 test_bit(R5_Insync
, &dev
->flags
)) {
2190 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
2192 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2193 (unsigned long long)sh
->sector
, i
);
2194 set_bit(R5_LOCKED
, &dev
->flags
);
2195 set_bit(R5_Wantread
, &dev
->flags
);
2198 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2199 (unsigned long long)sh
->sector
, i
);
2200 set_bit(STRIPE_DELAYED
, &sh
->state
);
2201 set_bit(STRIPE_HANDLE
, &sh
->state
);
2205 /* now if nothing is locked, and if we have enough data, we can start a write request */
2206 if (locked
== 0 && rcw
== 0 &&
2207 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
2208 if ( must_compute
> 0 ) {
2209 /* We have failed blocks and need to compute them */
2212 case 1: compute_block_1(sh
, failed_num
[0], 0); break;
2213 case 2: compute_block_2(sh
, failed_num
[0], failed_num
[1]); break;
2214 default: BUG(); /* This request should have been failed? */
2218 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh
->sector
);
2219 compute_parity6(sh
, RECONSTRUCT_WRITE
);
2220 /* now every locked buffer is ready to be written */
2222 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
2223 PRINTK("Writing stripe %llu block %d\n",
2224 (unsigned long long)sh
->sector
, i
);
2226 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
2228 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2229 set_bit(STRIPE_INSYNC
, &sh
->state
);
2231 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2232 atomic_dec(&conf
->preread_active_stripes
);
2233 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
2234 md_wakeup_thread(conf
->mddev
->thread
);
2239 /* maybe we need to check and possibly fix the parity for this stripe
2240 * Any reads will already have been scheduled, so we just see if enough data
2243 if (syncing
&& locked
== 0 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
2244 int update_p
= 0, update_q
= 0;
2247 set_bit(STRIPE_HANDLE
, &sh
->state
);
2250 BUG_ON(uptodate
< disks
);
2251 /* Want to check and possibly repair P and Q.
2252 * However there could be one 'failed' device, in which
2253 * case we can only check one of them, possibly using the
2254 * other to generate missing data
2257 /* If !tmp_page, we cannot do the calculations,
2258 * but as we have set STRIPE_HANDLE, we will soon be called
2259 * by stripe_handle with a tmp_page - just wait until then.
2262 if (failed
== q_failed
) {
2263 /* The only possible failed device holds 'Q', so it makes
2264 * sense to check P (If anything else were failed, we would
2265 * have used P to recreate it).
2267 compute_block_1(sh
, pd_idx
, 1);
2268 if (!page_is_zero(sh
->dev
[pd_idx
].page
)) {
2269 compute_block_1(sh
,pd_idx
,0);
2273 if (!q_failed
&& failed
< 2) {
2274 /* q is not failed, and we didn't use it to generate
2275 * anything, so it makes sense to check it
2277 memcpy(page_address(tmp_page
),
2278 page_address(sh
->dev
[qd_idx
].page
),
2280 compute_parity6(sh
, UPDATE_PARITY
);
2281 if (memcmp(page_address(tmp_page
),
2282 page_address(sh
->dev
[qd_idx
].page
),
2284 clear_bit(STRIPE_INSYNC
, &sh
->state
);
2288 if (update_p
|| update_q
) {
2289 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
2290 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
2291 /* don't try to repair!! */
2292 update_p
= update_q
= 0;
2295 /* now write out any block on a failed drive,
2296 * or P or Q if they need it
2300 dev
= &sh
->dev
[failed_num
[1]];
2302 set_bit(R5_LOCKED
, &dev
->flags
);
2303 set_bit(R5_Wantwrite
, &dev
->flags
);
2306 dev
= &sh
->dev
[failed_num
[0]];
2308 set_bit(R5_LOCKED
, &dev
->flags
);
2309 set_bit(R5_Wantwrite
, &dev
->flags
);
2313 dev
= &sh
->dev
[pd_idx
];
2315 set_bit(R5_LOCKED
, &dev
->flags
);
2316 set_bit(R5_Wantwrite
, &dev
->flags
);
2319 dev
= &sh
->dev
[qd_idx
];
2321 set_bit(R5_LOCKED
, &dev
->flags
);
2322 set_bit(R5_Wantwrite
, &dev
->flags
);
2324 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2326 set_bit(STRIPE_INSYNC
, &sh
->state
);
2330 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
2331 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
2332 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2335 /* If the failed drives are just a ReadError, then we might need
2336 * to progress the repair/check process
2338 if (failed
<= 2 && ! conf
->mddev
->ro
)
2339 for (i
=0; i
<failed
;i
++) {
2340 dev
= &sh
->dev
[failed_num
[i
]];
2341 if (test_bit(R5_ReadError
, &dev
->flags
)
2342 && !test_bit(R5_LOCKED
, &dev
->flags
)
2343 && test_bit(R5_UPTODATE
, &dev
->flags
)
2345 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
2346 set_bit(R5_Wantwrite
, &dev
->flags
);
2347 set_bit(R5_ReWrite
, &dev
->flags
);
2348 set_bit(R5_LOCKED
, &dev
->flags
);
2350 /* let's read it back */
2351 set_bit(R5_Wantread
, &dev
->flags
);
2352 set_bit(R5_LOCKED
, &dev
->flags
);
2356 spin_unlock(&sh
->lock
);
2358 while ((bi
=return_bi
)) {
2359 int bytes
= bi
->bi_size
;
2361 return_bi
= bi
->bi_next
;
2364 bi
->bi_end_io(bi
, bytes
,
2365 test_bit(BIO_UPTODATE
, &bi
->bi_flags
)
2368 for (i
=disks
; i
-- ;) {
2372 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
2374 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
2379 bi
= &sh
->dev
[i
].req
;
2383 bi
->bi_end_io
= raid5_end_write_request
;
2385 bi
->bi_end_io
= raid5_end_read_request
;
2388 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2389 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
2392 atomic_inc(&rdev
->nr_pending
);
2397 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
2399 bi
->bi_bdev
= rdev
->bdev
;
2400 PRINTK("for %llu schedule op %ld on disc %d\n",
2401 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
2402 atomic_inc(&sh
->count
);
2403 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
2404 bi
->bi_flags
= 1 << BIO_UPTODATE
;
2406 bi
->bi_max_vecs
= 1;
2408 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
2409 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
2410 bi
->bi_io_vec
[0].bv_offset
= 0;
2411 bi
->bi_size
= STRIPE_SIZE
;
2414 test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
2415 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
2416 generic_make_request(bi
);
2419 set_bit(STRIPE_DEGRADED
, &sh
->state
);
2420 PRINTK("skip op %ld on disc %d for sector %llu\n",
2421 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
2422 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2423 set_bit(STRIPE_HANDLE
, &sh
->state
);
2428 static void handle_stripe(struct stripe_head
*sh
, struct page
*tmp_page
)
2430 if (sh
->raid_conf
->level
== 6)
2431 handle_stripe6(sh
, tmp_page
);
2438 static void raid5_activate_delayed(raid5_conf_t
*conf
)
2440 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
2441 while (!list_empty(&conf
->delayed_list
)) {
2442 struct list_head
*l
= conf
->delayed_list
.next
;
2443 struct stripe_head
*sh
;
2444 sh
= list_entry(l
, struct stripe_head
, lru
);
2446 clear_bit(STRIPE_DELAYED
, &sh
->state
);
2447 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
2448 atomic_inc(&conf
->preread_active_stripes
);
2449 list_add_tail(&sh
->lru
, &conf
->handle_list
);
2454 static void activate_bit_delay(raid5_conf_t
*conf
)
2456 /* device_lock is held */
2457 struct list_head head
;
2458 list_add(&head
, &conf
->bitmap_list
);
2459 list_del_init(&conf
->bitmap_list
);
2460 while (!list_empty(&head
)) {
2461 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
2462 list_del_init(&sh
->lru
);
2463 atomic_inc(&sh
->count
);
2464 __release_stripe(conf
, sh
);
2468 static void unplug_slaves(mddev_t
*mddev
)
2470 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2474 for (i
=0; i
<mddev
->raid_disks
; i
++) {
2475 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2476 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
2477 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
2479 atomic_inc(&rdev
->nr_pending
);
2482 if (r_queue
->unplug_fn
)
2483 r_queue
->unplug_fn(r_queue
);
2485 rdev_dec_pending(rdev
, mddev
);
2492 static void raid5_unplug_device(request_queue_t
*q
)
2494 mddev_t
*mddev
= q
->queuedata
;
2495 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2496 unsigned long flags
;
2498 spin_lock_irqsave(&conf
->device_lock
, flags
);
2500 if (blk_remove_plug(q
)) {
2502 raid5_activate_delayed(conf
);
2504 md_wakeup_thread(mddev
->thread
);
2506 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2508 unplug_slaves(mddev
);
2511 static int raid5_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
2512 sector_t
*error_sector
)
2514 mddev_t
*mddev
= q
->queuedata
;
2515 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2519 for (i
=0; i
<mddev
->raid_disks
&& ret
== 0; i
++) {
2520 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2521 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
2522 struct block_device
*bdev
= rdev
->bdev
;
2523 request_queue_t
*r_queue
= bdev_get_queue(bdev
);
2525 if (!r_queue
->issue_flush_fn
)
2528 atomic_inc(&rdev
->nr_pending
);
2530 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
2532 rdev_dec_pending(rdev
, mddev
);
2541 static int raid5_congested(void *data
, int bits
)
2543 mddev_t
*mddev
= data
;
2544 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2546 /* No difference between reads and writes. Just check
2547 * how busy the stripe_cache is
2549 if (conf
->inactive_blocked
)
2553 if (list_empty_careful(&conf
->inactive_list
))
2559 /* We want read requests to align with chunks where possible,
2560 * but write requests don't need to.
2562 static int raid5_mergeable_bvec(request_queue_t
*q
, struct bio
*bio
, struct bio_vec
*biovec
)
2564 mddev_t
*mddev
= q
->queuedata
;
2565 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
2567 unsigned int chunk_sectors
= mddev
->chunk_size
>> 9;
2568 unsigned int bio_sectors
= bio
->bi_size
>> 9;
2570 if (bio_data_dir(bio
) == WRITE
)
2571 return biovec
->bv_len
; /* always allow writes to be mergeable */
2573 max
= (chunk_sectors
- ((sector
& (chunk_sectors
- 1)) + bio_sectors
)) << 9;
2574 if (max
< 0) max
= 0;
2575 if (max
<= biovec
->bv_len
&& bio_sectors
== 0)
2576 return biovec
->bv_len
;
2582 static int in_chunk_boundary(mddev_t
*mddev
, struct bio
*bio
)
2584 sector_t sector
= bio
->bi_sector
+ get_start_sect(bio
->bi_bdev
);
2585 unsigned int chunk_sectors
= mddev
->chunk_size
>> 9;
2586 unsigned int bio_sectors
= bio
->bi_size
>> 9;
2588 return chunk_sectors
>=
2589 ((sector
& (chunk_sectors
- 1)) + bio_sectors
);
2593 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
2594 * later sampled by raid5d.
2596 static void add_bio_to_retry(struct bio
*bi
,raid5_conf_t
*conf
)
2598 unsigned long flags
;
2600 spin_lock_irqsave(&conf
->device_lock
, flags
);
2602 bi
->bi_next
= conf
->retry_read_aligned_list
;
2603 conf
->retry_read_aligned_list
= bi
;
2605 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2606 md_wakeup_thread(conf
->mddev
->thread
);
2610 static struct bio
*remove_bio_from_retry(raid5_conf_t
*conf
)
2614 bi
= conf
->retry_read_aligned
;
2616 conf
->retry_read_aligned
= NULL
;
2619 bi
= conf
->retry_read_aligned_list
;
2621 conf
->retry_read_aligned
= bi
->bi_next
;
2623 bi
->bi_phys_segments
= 1; /* biased count of active stripes */
2624 bi
->bi_hw_segments
= 0; /* count of processed stripes */
2632 * The "raid5_align_endio" should check if the read succeeded and if it
2633 * did, call bio_endio on the original bio (having bio_put the new bio
2635 * If the read failed..
2637 static int raid5_align_endio(struct bio
*bi
, unsigned int bytes
, int error
)
2639 struct bio
* raid_bi
= bi
->bi_private
;
2642 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2649 mddev
= raid_bi
->bi_bdev
->bd_disk
->queue
->queuedata
;
2650 conf
= mddev_to_conf(mddev
);
2651 rdev
= (void*)raid_bi
->bi_next
;
2652 raid_bi
->bi_next
= NULL
;
2654 rdev_dec_pending(rdev
, conf
->mddev
);
2656 if (!error
&& uptodate
) {
2657 bio_endio(raid_bi
, bytes
, 0);
2658 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
2659 wake_up(&conf
->wait_for_stripe
);
2664 PRINTK("raid5_align_endio : io error...handing IO for a retry\n");
2666 add_bio_to_retry(raid_bi
, conf
);
2670 static int chunk_aligned_read(request_queue_t
*q
, struct bio
* raid_bio
)
2672 mddev_t
*mddev
= q
->queuedata
;
2673 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2674 const unsigned int raid_disks
= conf
->raid_disks
;
2675 const unsigned int data_disks
= raid_disks
- conf
->max_degraded
;
2676 unsigned int dd_idx
, pd_idx
;
2677 struct bio
* align_bi
;
2680 if (!in_chunk_boundary(mddev
, raid_bio
)) {
2681 printk("chunk_aligned_read : non aligned\n");
2685 * use bio_clone to make a copy of the bio
2687 align_bi
= bio_clone(raid_bio
, GFP_NOIO
);
2691 * set bi_end_io to a new function, and set bi_private to the
2694 align_bi
->bi_end_io
= raid5_align_endio
;
2695 align_bi
->bi_private
= raid_bio
;
2699 align_bi
->bi_sector
= raid5_compute_sector(raid_bio
->bi_sector
,
2707 rdev
= rcu_dereference(conf
->disks
[dd_idx
].rdev
);
2708 if (rdev
&& test_bit(In_sync
, &rdev
->flags
)) {
2709 atomic_inc(&rdev
->nr_pending
);
2711 raid_bio
->bi_next
= (void*)rdev
;
2712 align_bi
->bi_bdev
= rdev
->bdev
;
2713 align_bi
->bi_flags
&= ~(1 << BIO_SEG_VALID
);
2714 align_bi
->bi_sector
+= rdev
->data_offset
;
2716 spin_lock_irq(&conf
->device_lock
);
2717 wait_event_lock_irq(conf
->wait_for_stripe
,
2719 conf
->device_lock
, /* nothing */);
2720 atomic_inc(&conf
->active_aligned_reads
);
2721 spin_unlock_irq(&conf
->device_lock
);
2723 generic_make_request(align_bi
);
2733 static int make_request(request_queue_t
*q
, struct bio
* bi
)
2735 mddev_t
*mddev
= q
->queuedata
;
2736 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2737 unsigned int dd_idx
, pd_idx
;
2738 sector_t new_sector
;
2739 sector_t logical_sector
, last_sector
;
2740 struct stripe_head
*sh
;
2741 const int rw
= bio_data_dir(bi
);
2744 if (unlikely(bio_barrier(bi
))) {
2745 bio_endio(bi
, bi
->bi_size
, -EOPNOTSUPP
);
2749 md_write_start(mddev
, bi
);
2751 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
2752 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bi
));
2755 mddev
->reshape_position
== MaxSector
&&
2756 chunk_aligned_read(q
,bi
))
2759 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
2760 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
2762 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
2764 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
2766 int disks
, data_disks
;
2769 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
2770 if (likely(conf
->expand_progress
== MaxSector
))
2771 disks
= conf
->raid_disks
;
2773 /* spinlock is needed as expand_progress may be
2774 * 64bit on a 32bit platform, and so it might be
2775 * possible to see a half-updated value
2776 * Ofcourse expand_progress could change after
2777 * the lock is dropped, so once we get a reference
2778 * to the stripe that we think it is, we will have
2781 spin_lock_irq(&conf
->device_lock
);
2782 disks
= conf
->raid_disks
;
2783 if (logical_sector
>= conf
->expand_progress
)
2784 disks
= conf
->previous_raid_disks
;
2786 if (logical_sector
>= conf
->expand_lo
) {
2787 spin_unlock_irq(&conf
->device_lock
);
2792 spin_unlock_irq(&conf
->device_lock
);
2794 data_disks
= disks
- conf
->max_degraded
;
2796 new_sector
= raid5_compute_sector(logical_sector
, disks
, data_disks
,
2797 &dd_idx
, &pd_idx
, conf
);
2798 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2799 (unsigned long long)new_sector
,
2800 (unsigned long long)logical_sector
);
2802 sh
= get_active_stripe(conf
, new_sector
, disks
, pd_idx
, (bi
->bi_rw
&RWA_MASK
));
2804 if (unlikely(conf
->expand_progress
!= MaxSector
)) {
2805 /* expansion might have moved on while waiting for a
2806 * stripe, so we must do the range check again.
2807 * Expansion could still move past after this
2808 * test, but as we are holding a reference to
2809 * 'sh', we know that if that happens,
2810 * STRIPE_EXPANDING will get set and the expansion
2811 * won't proceed until we finish with the stripe.
2814 spin_lock_irq(&conf
->device_lock
);
2815 if (logical_sector
< conf
->expand_progress
&&
2816 disks
== conf
->previous_raid_disks
)
2817 /* mismatch, need to try again */
2819 spin_unlock_irq(&conf
->device_lock
);
2825 /* FIXME what if we get a false positive because these
2826 * are being updated.
2828 if (logical_sector
>= mddev
->suspend_lo
&&
2829 logical_sector
< mddev
->suspend_hi
) {
2835 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
2836 !add_stripe_bio(sh
, bi
, dd_idx
, (bi
->bi_rw
&RW_MASK
))) {
2837 /* Stripe is busy expanding or
2838 * add failed due to overlap. Flush everything
2841 raid5_unplug_device(mddev
->queue
);
2846 finish_wait(&conf
->wait_for_overlap
, &w
);
2847 handle_stripe(sh
, NULL
);
2850 /* cannot get stripe for read-ahead, just give-up */
2851 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2852 finish_wait(&conf
->wait_for_overlap
, &w
);
2857 spin_lock_irq(&conf
->device_lock
);
2858 remaining
= --bi
->bi_phys_segments
;
2859 spin_unlock_irq(&conf
->device_lock
);
2860 if (remaining
== 0) {
2861 int bytes
= bi
->bi_size
;
2864 md_write_end(mddev
);
2866 bi
->bi_end_io(bi
, bytes
,
2867 test_bit(BIO_UPTODATE
, &bi
->bi_flags
)
2873 static sector_t
reshape_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
)
2875 /* reshaping is quite different to recovery/resync so it is
2876 * handled quite separately ... here.
2878 * On each call to sync_request, we gather one chunk worth of
2879 * destination stripes and flag them as expanding.
2880 * Then we find all the source stripes and request reads.
2881 * As the reads complete, handle_stripe will copy the data
2882 * into the destination stripe and release that stripe.
2884 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
2885 struct stripe_head
*sh
;
2887 sector_t first_sector
, last_sector
;
2892 sector_t writepos
, safepos
, gap
;
2894 if (sector_nr
== 0 &&
2895 conf
->expand_progress
!= 0) {
2896 /* restarting in the middle, skip the initial sectors */
2897 sector_nr
= conf
->expand_progress
;
2898 sector_div(sector_nr
, conf
->raid_disks
-1);
2903 /* we update the metadata when there is more than 3Meg
2904 * in the block range (that is rather arbitrary, should
2905 * probably be time based) or when the data about to be
2906 * copied would over-write the source of the data at
2907 * the front of the range.
2908 * i.e. one new_stripe forward from expand_progress new_maps
2909 * to after where expand_lo old_maps to
2911 writepos
= conf
->expand_progress
+
2912 conf
->chunk_size
/512*(conf
->raid_disks
-1);
2913 sector_div(writepos
, conf
->raid_disks
-1);
2914 safepos
= conf
->expand_lo
;
2915 sector_div(safepos
, conf
->previous_raid_disks
-1);
2916 gap
= conf
->expand_progress
- conf
->expand_lo
;
2918 if (writepos
>= safepos
||
2919 gap
> (conf
->raid_disks
-1)*3000*2 /*3Meg*/) {
2920 /* Cannot proceed until we've updated the superblock... */
2921 wait_event(conf
->wait_for_overlap
,
2922 atomic_read(&conf
->reshape_stripes
)==0);
2923 mddev
->reshape_position
= conf
->expand_progress
;
2924 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
2925 md_wakeup_thread(mddev
->thread
);
2926 wait_event(mddev
->sb_wait
, mddev
->flags
== 0 ||
2927 kthread_should_stop());
2928 spin_lock_irq(&conf
->device_lock
);
2929 conf
->expand_lo
= mddev
->reshape_position
;
2930 spin_unlock_irq(&conf
->device_lock
);
2931 wake_up(&conf
->wait_for_overlap
);
2934 for (i
=0; i
< conf
->chunk_size
/512; i
+= STRIPE_SECTORS
) {
2937 pd_idx
= stripe_to_pdidx(sector_nr
+i
, conf
, conf
->raid_disks
);
2938 sh
= get_active_stripe(conf
, sector_nr
+i
,
2939 conf
->raid_disks
, pd_idx
, 0);
2940 set_bit(STRIPE_EXPANDING
, &sh
->state
);
2941 atomic_inc(&conf
->reshape_stripes
);
2942 /* If any of this stripe is beyond the end of the old
2943 * array, then we need to zero those blocks
2945 for (j
=sh
->disks
; j
--;) {
2947 if (j
== sh
->pd_idx
)
2949 s
= compute_blocknr(sh
, j
);
2950 if (s
< (mddev
->array_size
<<1)) {
2954 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
2955 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
2956 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
2959 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
2960 set_bit(STRIPE_HANDLE
, &sh
->state
);
2964 spin_lock_irq(&conf
->device_lock
);
2965 conf
->expand_progress
= (sector_nr
+ i
)*(conf
->raid_disks
-1);
2966 spin_unlock_irq(&conf
->device_lock
);
2967 /* Ok, those stripe are ready. We can start scheduling
2968 * reads on the source stripes.
2969 * The source stripes are determined by mapping the first and last
2970 * block on the destination stripes.
2972 raid_disks
= conf
->previous_raid_disks
;
2973 data_disks
= raid_disks
- 1;
2975 raid5_compute_sector(sector_nr
*(conf
->raid_disks
-1),
2976 raid_disks
, data_disks
,
2977 &dd_idx
, &pd_idx
, conf
);
2979 raid5_compute_sector((sector_nr
+conf
->chunk_size
/512)
2980 *(conf
->raid_disks
-1) -1,
2981 raid_disks
, data_disks
,
2982 &dd_idx
, &pd_idx
, conf
);
2983 if (last_sector
>= (mddev
->size
<<1))
2984 last_sector
= (mddev
->size
<<1)-1;
2985 while (first_sector
<= last_sector
) {
2986 pd_idx
= stripe_to_pdidx(first_sector
, conf
, conf
->previous_raid_disks
);
2987 sh
= get_active_stripe(conf
, first_sector
,
2988 conf
->previous_raid_disks
, pd_idx
, 0);
2989 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
2990 set_bit(STRIPE_HANDLE
, &sh
->state
);
2992 first_sector
+= STRIPE_SECTORS
;
2994 return conf
->chunk_size
>>9;
2997 /* FIXME go_faster isn't used */
2998 static inline sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
3000 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
3001 struct stripe_head
*sh
;
3003 int raid_disks
= conf
->raid_disks
;
3004 sector_t max_sector
= mddev
->size
<< 1;
3006 int still_degraded
= 0;
3009 if (sector_nr
>= max_sector
) {
3010 /* just being told to finish up .. nothing much to do */
3011 unplug_slaves(mddev
);
3012 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
3017 if (mddev
->curr_resync
< max_sector
) /* aborted */
3018 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
3020 else /* completed sync */
3022 bitmap_close_sync(mddev
->bitmap
);
3027 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
3028 return reshape_request(mddev
, sector_nr
, skipped
);
3030 /* if there is too many failed drives and we are trying
3031 * to resync, then assert that we are finished, because there is
3032 * nothing we can do.
3034 if (mddev
->degraded
>= conf
->max_degraded
&&
3035 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
3036 sector_t rv
= (mddev
->size
<< 1) - sector_nr
;
3040 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
3041 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
3042 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
3043 /* we can skip this block, and probably more */
3044 sync_blocks
/= STRIPE_SECTORS
;
3046 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
3049 pd_idx
= stripe_to_pdidx(sector_nr
, conf
, raid_disks
);
3050 sh
= get_active_stripe(conf
, sector_nr
, raid_disks
, pd_idx
, 1);
3052 sh
= get_active_stripe(conf
, sector_nr
, raid_disks
, pd_idx
, 0);
3053 /* make sure we don't swamp the stripe cache if someone else
3054 * is trying to get access
3056 schedule_timeout_uninterruptible(1);
3058 /* Need to check if array will still be degraded after recovery/resync
3059 * We don't need to check the 'failed' flag as when that gets set,
3062 for (i
=0; i
<mddev
->raid_disks
; i
++)
3063 if (conf
->disks
[i
].rdev
== NULL
)
3066 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
3068 spin_lock(&sh
->lock
);
3069 set_bit(STRIPE_SYNCING
, &sh
->state
);
3070 clear_bit(STRIPE_INSYNC
, &sh
->state
);
3071 spin_unlock(&sh
->lock
);
3073 handle_stripe(sh
, NULL
);
3076 return STRIPE_SECTORS
;
3079 static int retry_aligned_read(raid5_conf_t
*conf
, struct bio
*raid_bio
)
3081 /* We may not be able to submit a whole bio at once as there
3082 * may not be enough stripe_heads available.
3083 * We cannot pre-allocate enough stripe_heads as we may need
3084 * more than exist in the cache (if we allow ever large chunks).
3085 * So we do one stripe head at a time and record in
3086 * ->bi_hw_segments how many have been done.
3088 * We *know* that this entire raid_bio is in one chunk, so
3089 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3091 struct stripe_head
*sh
;
3093 sector_t sector
, logical_sector
, last_sector
;
3098 logical_sector
= raid_bio
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
3099 sector
= raid5_compute_sector( logical_sector
,
3101 conf
->raid_disks
- conf
->max_degraded
,
3105 last_sector
= raid_bio
->bi_sector
+ (raid_bio
->bi_size
>>9);
3107 for (; logical_sector
< last_sector
;
3108 logical_sector
+= STRIPE_SECTORS
, scnt
++) {
3110 if (scnt
< raid_bio
->bi_hw_segments
)
3111 /* already done this stripe */
3114 sh
= get_active_stripe(conf
, sector
, conf
->raid_disks
, pd_idx
, 1);
3117 /* failed to get a stripe - must wait */
3118 raid_bio
->bi_hw_segments
= scnt
;
3119 conf
->retry_read_aligned
= raid_bio
;
3123 set_bit(R5_ReadError
, &sh
->dev
[dd_idx
].flags
);
3124 add_stripe_bio(sh
, raid_bio
, dd_idx
, 0);
3125 handle_stripe(sh
, NULL
);
3129 spin_lock_irq(&conf
->device_lock
);
3130 remaining
= --raid_bio
->bi_phys_segments
;
3131 spin_unlock_irq(&conf
->device_lock
);
3132 if (remaining
== 0) {
3133 int bytes
= raid_bio
->bi_size
;
3135 raid_bio
->bi_size
= 0;
3136 raid_bio
->bi_end_io(raid_bio
, bytes
,
3137 test_bit(BIO_UPTODATE
, &raid_bio
->bi_flags
)
3140 if (atomic_dec_and_test(&conf
->active_aligned_reads
))
3141 wake_up(&conf
->wait_for_stripe
);
3148 * This is our raid5 kernel thread.
3150 * We scan the hash table for stripes which can be handled now.
3151 * During the scan, completed stripes are saved for us by the interrupt
3152 * handler, so that they will not have to wait for our next wakeup.
3154 static void raid5d (mddev_t
*mddev
)
3156 struct stripe_head
*sh
;
3157 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3160 PRINTK("+++ raid5d active\n");
3162 md_check_recovery(mddev
);
3165 spin_lock_irq(&conf
->device_lock
);
3167 struct list_head
*first
;
3170 if (conf
->seq_flush
!= conf
->seq_write
) {
3171 int seq
= conf
->seq_flush
;
3172 spin_unlock_irq(&conf
->device_lock
);
3173 bitmap_unplug(mddev
->bitmap
);
3174 spin_lock_irq(&conf
->device_lock
);
3175 conf
->seq_write
= seq
;
3176 activate_bit_delay(conf
);
3179 if (list_empty(&conf
->handle_list
) &&
3180 atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
&&
3181 !blk_queue_plugged(mddev
->queue
) &&
3182 !list_empty(&conf
->delayed_list
))
3183 raid5_activate_delayed(conf
);
3185 while ((bio
= remove_bio_from_retry(conf
))) {
3187 spin_unlock_irq(&conf
->device_lock
);
3188 ok
= retry_aligned_read(conf
, bio
);
3189 spin_lock_irq(&conf
->device_lock
);
3195 if (list_empty(&conf
->handle_list
))
3198 first
= conf
->handle_list
.next
;
3199 sh
= list_entry(first
, struct stripe_head
, lru
);
3201 list_del_init(first
);
3202 atomic_inc(&sh
->count
);
3203 BUG_ON(atomic_read(&sh
->count
)!= 1);
3204 spin_unlock_irq(&conf
->device_lock
);
3207 handle_stripe(sh
, conf
->spare_page
);
3210 spin_lock_irq(&conf
->device_lock
);
3212 PRINTK("%d stripes handled\n", handled
);
3214 spin_unlock_irq(&conf
->device_lock
);
3216 unplug_slaves(mddev
);
3218 PRINTK("--- raid5d inactive\n");
3222 raid5_show_stripe_cache_size(mddev_t
*mddev
, char *page
)
3224 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3226 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
3232 raid5_store_stripe_cache_size(mddev_t
*mddev
, const char *page
, size_t len
)
3234 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3237 if (len
>= PAGE_SIZE
)
3242 new = simple_strtoul(page
, &end
, 10);
3243 if (!*page
|| (*end
&& *end
!= '\n') )
3245 if (new <= 16 || new > 32768)
3247 while (new < conf
->max_nr_stripes
) {
3248 if (drop_one_stripe(conf
))
3249 conf
->max_nr_stripes
--;
3253 while (new > conf
->max_nr_stripes
) {
3254 if (grow_one_stripe(conf
))
3255 conf
->max_nr_stripes
++;
3261 static struct md_sysfs_entry
3262 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
3263 raid5_show_stripe_cache_size
,
3264 raid5_store_stripe_cache_size
);
3267 stripe_cache_active_show(mddev_t
*mddev
, char *page
)
3269 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3271 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
3276 static struct md_sysfs_entry
3277 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
3279 static struct attribute
*raid5_attrs
[] = {
3280 &raid5_stripecache_size
.attr
,
3281 &raid5_stripecache_active
.attr
,
3284 static struct attribute_group raid5_attrs_group
= {
3286 .attrs
= raid5_attrs
,
3289 static int run(mddev_t
*mddev
)
3292 int raid_disk
, memory
;
3294 struct disk_info
*disk
;
3295 struct list_head
*tmp
;
3296 int working_disks
= 0;
3298 if (mddev
->level
!= 5 && mddev
->level
!= 4 && mddev
->level
!= 6) {
3299 printk(KERN_ERR
"raid5: %s: raid level not set to 4/5/6 (%d)\n",
3300 mdname(mddev
), mddev
->level
);
3304 if (mddev
->reshape_position
!= MaxSector
) {
3305 /* Check that we can continue the reshape.
3306 * Currently only disks can change, it must
3307 * increase, and we must be past the point where
3308 * a stripe over-writes itself
3310 sector_t here_new
, here_old
;
3313 if (mddev
->new_level
!= mddev
->level
||
3314 mddev
->new_layout
!= mddev
->layout
||
3315 mddev
->new_chunk
!= mddev
->chunk_size
) {
3316 printk(KERN_ERR
"raid5: %s: unsupported reshape required - aborting.\n",
3320 if (mddev
->delta_disks
<= 0) {
3321 printk(KERN_ERR
"raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3325 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3326 /* reshape_position must be on a new-stripe boundary, and one
3327 * further up in new geometry must map after here in old geometry.
3329 here_new
= mddev
->reshape_position
;
3330 if (sector_div(here_new
, (mddev
->chunk_size
>>9)*(mddev
->raid_disks
-1))) {
3331 printk(KERN_ERR
"raid5: reshape_position not on a stripe boundary\n");
3334 /* here_new is the stripe we will write to */
3335 here_old
= mddev
->reshape_position
;
3336 sector_div(here_old
, (mddev
->chunk_size
>>9)*(old_disks
-1));
3337 /* here_old is the first stripe that we might need to read from */
3338 if (here_new
>= here_old
) {
3339 /* Reading from the same stripe as writing to - bad */
3340 printk(KERN_ERR
"raid5: reshape_position too early for auto-recovery - aborting.\n");
3343 printk(KERN_INFO
"raid5: reshape will continue\n");
3344 /* OK, we should be able to continue; */
3348 mddev
->private = kzalloc(sizeof (raid5_conf_t
), GFP_KERNEL
);
3349 if ((conf
= mddev
->private) == NULL
)
3351 if (mddev
->reshape_position
== MaxSector
) {
3352 conf
->previous_raid_disks
= conf
->raid_disks
= mddev
->raid_disks
;
3354 conf
->raid_disks
= mddev
->raid_disks
;
3355 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3358 conf
->disks
= kzalloc(conf
->raid_disks
* sizeof(struct disk_info
),
3363 conf
->mddev
= mddev
;
3365 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
3368 if (mddev
->level
== 6) {
3369 conf
->spare_page
= alloc_page(GFP_KERNEL
);
3370 if (!conf
->spare_page
)
3373 spin_lock_init(&conf
->device_lock
);
3374 init_waitqueue_head(&conf
->wait_for_stripe
);
3375 init_waitqueue_head(&conf
->wait_for_overlap
);
3376 INIT_LIST_HEAD(&conf
->handle_list
);
3377 INIT_LIST_HEAD(&conf
->delayed_list
);
3378 INIT_LIST_HEAD(&conf
->bitmap_list
);
3379 INIT_LIST_HEAD(&conf
->inactive_list
);
3380 atomic_set(&conf
->active_stripes
, 0);
3381 atomic_set(&conf
->preread_active_stripes
, 0);
3382 atomic_set(&conf
->active_aligned_reads
, 0);
3384 PRINTK("raid5: run(%s) called.\n", mdname(mddev
));
3386 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3387 raid_disk
= rdev
->raid_disk
;
3388 if (raid_disk
>= conf
->raid_disks
3391 disk
= conf
->disks
+ raid_disk
;
3395 if (test_bit(In_sync
, &rdev
->flags
)) {
3396 char b
[BDEVNAME_SIZE
];
3397 printk(KERN_INFO
"raid5: device %s operational as raid"
3398 " disk %d\n", bdevname(rdev
->bdev
,b
),
3405 * 0 for a fully functional array, 1 or 2 for a degraded array.
3407 mddev
->degraded
= conf
->raid_disks
- working_disks
;
3408 conf
->mddev
= mddev
;
3409 conf
->chunk_size
= mddev
->chunk_size
;
3410 conf
->level
= mddev
->level
;
3411 if (conf
->level
== 6)
3412 conf
->max_degraded
= 2;
3414 conf
->max_degraded
= 1;
3415 conf
->algorithm
= mddev
->layout
;
3416 conf
->max_nr_stripes
= NR_STRIPES
;
3417 conf
->expand_progress
= mddev
->reshape_position
;
3419 /* device size must be a multiple of chunk size */
3420 mddev
->size
&= ~(mddev
->chunk_size
/1024 -1);
3421 mddev
->resync_max_sectors
= mddev
->size
<< 1;
3423 if (conf
->level
== 6 && conf
->raid_disks
< 4) {
3424 printk(KERN_ERR
"raid6: not enough configured devices for %s (%d, minimum 4)\n",
3425 mdname(mddev
), conf
->raid_disks
);
3428 if (!conf
->chunk_size
|| conf
->chunk_size
% 4) {
3429 printk(KERN_ERR
"raid5: invalid chunk size %d for %s\n",
3430 conf
->chunk_size
, mdname(mddev
));
3433 if (conf
->algorithm
> ALGORITHM_RIGHT_SYMMETRIC
) {
3435 "raid5: unsupported parity algorithm %d for %s\n",
3436 conf
->algorithm
, mdname(mddev
));
3439 if (mddev
->degraded
> conf
->max_degraded
) {
3440 printk(KERN_ERR
"raid5: not enough operational devices for %s"
3441 " (%d/%d failed)\n",
3442 mdname(mddev
), mddev
->degraded
, conf
->raid_disks
);
3446 if (mddev
->degraded
> 0 &&
3447 mddev
->recovery_cp
!= MaxSector
) {
3448 if (mddev
->ok_start_degraded
)
3450 "raid5: starting dirty degraded array: %s"
3451 "- data corruption possible.\n",
3455 "raid5: cannot start dirty degraded array for %s\n",
3462 mddev
->thread
= md_register_thread(raid5d
, mddev
, "%s_raid5");
3463 if (!mddev
->thread
) {
3465 "raid5: couldn't allocate thread for %s\n",
3470 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
3471 conf
->raid_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
3472 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
3474 "raid5: couldn't allocate %dkB for buffers\n", memory
);
3475 shrink_stripes(conf
);
3476 md_unregister_thread(mddev
->thread
);
3479 printk(KERN_INFO
"raid5: allocated %dkB for %s\n",
3480 memory
, mdname(mddev
));
3482 if (mddev
->degraded
== 0)
3483 printk("raid5: raid level %d set %s active with %d out of %d"
3484 " devices, algorithm %d\n", conf
->level
, mdname(mddev
),
3485 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
3488 printk(KERN_ALERT
"raid5: raid level %d set %s active with %d"
3489 " out of %d devices, algorithm %d\n", conf
->level
,
3490 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
3491 mddev
->raid_disks
, conf
->algorithm
);
3493 print_raid5_conf(conf
);
3495 if (conf
->expand_progress
!= MaxSector
) {
3496 printk("...ok start reshape thread\n");
3497 conf
->expand_lo
= conf
->expand_progress
;
3498 atomic_set(&conf
->reshape_stripes
, 0);
3499 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3500 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3501 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3502 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3503 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
3507 /* read-ahead size must cover two whole stripes, which is
3508 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3511 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
3512 int stripe
= data_disks
*
3513 (mddev
->chunk_size
/ PAGE_SIZE
);
3514 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
3515 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
3518 /* Ok, everything is just fine now */
3519 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
);
3521 mddev
->queue
->unplug_fn
= raid5_unplug_device
;
3522 mddev
->queue
->issue_flush_fn
= raid5_issue_flush
;
3523 mddev
->queue
->backing_dev_info
.congested_fn
= raid5_congested
;
3524 mddev
->queue
->backing_dev_info
.congested_data
= mddev
;
3526 mddev
->array_size
= mddev
->size
* (conf
->previous_raid_disks
-
3527 conf
->max_degraded
);
3529 blk_queue_merge_bvec(mddev
->queue
, raid5_mergeable_bvec
);
3534 print_raid5_conf(conf
);
3535 safe_put_page(conf
->spare_page
);
3537 kfree(conf
->stripe_hashtbl
);
3540 mddev
->private = NULL
;
3541 printk(KERN_ALERT
"raid5: failed to run raid set %s\n", mdname(mddev
));
3547 static int stop(mddev_t
*mddev
)
3549 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
3551 md_unregister_thread(mddev
->thread
);
3552 mddev
->thread
= NULL
;
3553 shrink_stripes(conf
);
3554 kfree(conf
->stripe_hashtbl
);
3555 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
3556 sysfs_remove_group(&mddev
->kobj
, &raid5_attrs_group
);
3559 mddev
->private = NULL
;
3564 static void print_sh (struct seq_file
*seq
, struct stripe_head
*sh
)
3568 seq_printf(seq
, "sh %llu, pd_idx %d, state %ld.\n",
3569 (unsigned long long)sh
->sector
, sh
->pd_idx
, sh
->state
);
3570 seq_printf(seq
, "sh %llu, count %d.\n",
3571 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
));
3572 seq_printf(seq
, "sh %llu, ", (unsigned long long)sh
->sector
);
3573 for (i
= 0; i
< sh
->disks
; i
++) {
3574 seq_printf(seq
, "(cache%d: %p %ld) ",
3575 i
, sh
->dev
[i
].page
, sh
->dev
[i
].flags
);
3577 seq_printf(seq
, "\n");
3580 static void printall (struct seq_file
*seq
, raid5_conf_t
*conf
)
3582 struct stripe_head
*sh
;
3583 struct hlist_node
*hn
;
3586 spin_lock_irq(&conf
->device_lock
);
3587 for (i
= 0; i
< NR_HASH
; i
++) {
3588 hlist_for_each_entry(sh
, hn
, &conf
->stripe_hashtbl
[i
], hash
) {
3589 if (sh
->raid_conf
!= conf
)
3594 spin_unlock_irq(&conf
->device_lock
);
3598 static void status (struct seq_file
*seq
, mddev_t
*mddev
)
3600 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
3603 seq_printf (seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
, mddev
->chunk_size
>> 10, mddev
->layout
);
3604 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->raid_disks
- mddev
->degraded
);
3605 for (i
= 0; i
< conf
->raid_disks
; i
++)
3606 seq_printf (seq
, "%s",
3607 conf
->disks
[i
].rdev
&&
3608 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
3609 seq_printf (seq
, "]");
3611 seq_printf (seq
, "\n");
3612 printall(seq
, conf
);
3616 static void print_raid5_conf (raid5_conf_t
*conf
)
3619 struct disk_info
*tmp
;
3621 printk("RAID5 conf printout:\n");
3623 printk("(conf==NULL)\n");
3626 printk(" --- rd:%d wd:%d\n", conf
->raid_disks
,
3627 conf
->raid_disks
- conf
->mddev
->degraded
);
3629 for (i
= 0; i
< conf
->raid_disks
; i
++) {
3630 char b
[BDEVNAME_SIZE
];
3631 tmp
= conf
->disks
+ i
;
3633 printk(" disk %d, o:%d, dev:%s\n",
3634 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
3635 bdevname(tmp
->rdev
->bdev
,b
));
3639 static int raid5_spare_active(mddev_t
*mddev
)
3642 raid5_conf_t
*conf
= mddev
->private;
3643 struct disk_info
*tmp
;
3645 for (i
= 0; i
< conf
->raid_disks
; i
++) {
3646 tmp
= conf
->disks
+ i
;
3648 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
3649 && !test_and_set_bit(In_sync
, &tmp
->rdev
->flags
)) {
3650 unsigned long flags
;
3651 spin_lock_irqsave(&conf
->device_lock
, flags
);
3653 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3656 print_raid5_conf(conf
);
3660 static int raid5_remove_disk(mddev_t
*mddev
, int number
)
3662 raid5_conf_t
*conf
= mddev
->private;
3665 struct disk_info
*p
= conf
->disks
+ number
;
3667 print_raid5_conf(conf
);
3670 if (test_bit(In_sync
, &rdev
->flags
) ||
3671 atomic_read(&rdev
->nr_pending
)) {
3677 if (atomic_read(&rdev
->nr_pending
)) {
3678 /* lost the race, try later */
3685 print_raid5_conf(conf
);
3689 static int raid5_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
3691 raid5_conf_t
*conf
= mddev
->private;
3694 struct disk_info
*p
;
3696 if (mddev
->degraded
> conf
->max_degraded
)
3697 /* no point adding a device */
3701 * find the disk ... but prefer rdev->saved_raid_disk
3704 if (rdev
->saved_raid_disk
>= 0 &&
3705 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
3706 disk
= rdev
->saved_raid_disk
;
3709 for ( ; disk
< conf
->raid_disks
; disk
++)
3710 if ((p
=conf
->disks
+ disk
)->rdev
== NULL
) {
3711 clear_bit(In_sync
, &rdev
->flags
);
3712 rdev
->raid_disk
= disk
;
3714 if (rdev
->saved_raid_disk
!= disk
)
3716 rcu_assign_pointer(p
->rdev
, rdev
);
3719 print_raid5_conf(conf
);
3723 static int raid5_resize(mddev_t
*mddev
, sector_t sectors
)
3725 /* no resync is happening, and there is enough space
3726 * on all devices, so we can resize.
3727 * We need to make sure resync covers any new space.
3728 * If the array is shrinking we should possibly wait until
3729 * any io in the removed space completes, but it hardly seems
3732 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3734 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
3735 mddev
->array_size
= (sectors
* (mddev
->raid_disks
-conf
->max_degraded
))>>1;
3736 set_capacity(mddev
->gendisk
, mddev
->array_size
<< 1);
3738 if (sectors
/2 > mddev
->size
&& mddev
->recovery_cp
== MaxSector
) {
3739 mddev
->recovery_cp
= mddev
->size
<< 1;
3740 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3742 mddev
->size
= sectors
/2;
3743 mddev
->resync_max_sectors
= sectors
;
3747 #ifdef CONFIG_MD_RAID5_RESHAPE
3748 static int raid5_check_reshape(mddev_t
*mddev
)
3750 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3753 if (mddev
->delta_disks
< 0 ||
3754 mddev
->new_level
!= mddev
->level
)
3755 return -EINVAL
; /* Cannot shrink array or change level yet */
3756 if (mddev
->delta_disks
== 0)
3757 return 0; /* nothing to do */
3759 /* Can only proceed if there are plenty of stripe_heads.
3760 * We need a minimum of one full stripe,, and for sensible progress
3761 * it is best to have about 4 times that.
3762 * If we require 4 times, then the default 256 4K stripe_heads will
3763 * allow for chunk sizes up to 256K, which is probably OK.
3764 * If the chunk size is greater, user-space should request more
3765 * stripe_heads first.
3767 if ((mddev
->chunk_size
/ STRIPE_SIZE
) * 4 > conf
->max_nr_stripes
||
3768 (mddev
->new_chunk
/ STRIPE_SIZE
) * 4 > conf
->max_nr_stripes
) {
3769 printk(KERN_WARNING
"raid5: reshape: not enough stripes. Needed %lu\n",
3770 (mddev
->chunk_size
/ STRIPE_SIZE
)*4);
3774 err
= resize_stripes(conf
, conf
->raid_disks
+ mddev
->delta_disks
);
3778 /* looks like we might be able to manage this */
3782 static int raid5_start_reshape(mddev_t
*mddev
)
3784 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3786 struct list_head
*rtmp
;
3788 int added_devices
= 0;
3789 unsigned long flags
;
3791 if (mddev
->degraded
||
3792 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3795 ITERATE_RDEV(mddev
, rdev
, rtmp
)
3796 if (rdev
->raid_disk
< 0 &&
3797 !test_bit(Faulty
, &rdev
->flags
))
3800 if (spares
< mddev
->delta_disks
-1)
3801 /* Not enough devices even to make a degraded array
3806 atomic_set(&conf
->reshape_stripes
, 0);
3807 spin_lock_irq(&conf
->device_lock
);
3808 conf
->previous_raid_disks
= conf
->raid_disks
;
3809 conf
->raid_disks
+= mddev
->delta_disks
;
3810 conf
->expand_progress
= 0;
3811 conf
->expand_lo
= 0;
3812 spin_unlock_irq(&conf
->device_lock
);
3814 /* Add some new drives, as many as will fit.
3815 * We know there are enough to make the newly sized array work.
3817 ITERATE_RDEV(mddev
, rdev
, rtmp
)
3818 if (rdev
->raid_disk
< 0 &&
3819 !test_bit(Faulty
, &rdev
->flags
)) {
3820 if (raid5_add_disk(mddev
, rdev
)) {
3822 set_bit(In_sync
, &rdev
->flags
);
3824 rdev
->recovery_offset
= 0;
3825 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3826 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
3831 spin_lock_irqsave(&conf
->device_lock
, flags
);
3832 mddev
->degraded
= (conf
->raid_disks
- conf
->previous_raid_disks
) - added_devices
;
3833 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
3834 mddev
->raid_disks
= conf
->raid_disks
;
3835 mddev
->reshape_position
= 0;
3836 set_bit(MD_CHANGE_DEVS
, &mddev
->flags
);
3838 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3839 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3840 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3841 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3842 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
3844 if (!mddev
->sync_thread
) {
3845 mddev
->recovery
= 0;
3846 spin_lock_irq(&conf
->device_lock
);
3847 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
3848 conf
->expand_progress
= MaxSector
;
3849 spin_unlock_irq(&conf
->device_lock
);
3852 md_wakeup_thread(mddev
->sync_thread
);
3853 md_new_event(mddev
);
3858 static void end_reshape(raid5_conf_t
*conf
)
3860 struct block_device
*bdev
;
3862 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
3863 conf
->mddev
->array_size
= conf
->mddev
->size
* (conf
->raid_disks
-1);
3864 set_capacity(conf
->mddev
->gendisk
, conf
->mddev
->array_size
<< 1);
3865 conf
->mddev
->changed
= 1;
3867 bdev
= bdget_disk(conf
->mddev
->gendisk
, 0);
3869 mutex_lock(&bdev
->bd_inode
->i_mutex
);
3870 i_size_write(bdev
->bd_inode
, (loff_t
)conf
->mddev
->array_size
<< 10);
3871 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
3874 spin_lock_irq(&conf
->device_lock
);
3875 conf
->expand_progress
= MaxSector
;
3876 spin_unlock_irq(&conf
->device_lock
);
3877 conf
->mddev
->reshape_position
= MaxSector
;
3879 /* read-ahead size must cover two whole stripes, which is
3880 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3883 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
3884 int stripe
= data_disks
*
3885 (conf
->mddev
->chunk_size
/ PAGE_SIZE
);
3886 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
3887 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
3892 static void raid5_quiesce(mddev_t
*mddev
, int state
)
3894 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3897 case 2: /* resume for a suspend */
3898 wake_up(&conf
->wait_for_overlap
);
3901 case 1: /* stop all writes */
3902 spin_lock_irq(&conf
->device_lock
);
3904 wait_event_lock_irq(conf
->wait_for_stripe
,
3905 atomic_read(&conf
->active_stripes
) == 0 &&
3906 atomic_read(&conf
->active_aligned_reads
) == 0,
3907 conf
->device_lock
, /* nothing */);
3908 spin_unlock_irq(&conf
->device_lock
);
3911 case 0: /* re-enable writes */
3912 spin_lock_irq(&conf
->device_lock
);
3914 wake_up(&conf
->wait_for_stripe
);
3915 wake_up(&conf
->wait_for_overlap
);
3916 spin_unlock_irq(&conf
->device_lock
);
3921 static struct mdk_personality raid6_personality
=
3925 .owner
= THIS_MODULE
,
3926 .make_request
= make_request
,
3930 .error_handler
= error
,
3931 .hot_add_disk
= raid5_add_disk
,
3932 .hot_remove_disk
= raid5_remove_disk
,
3933 .spare_active
= raid5_spare_active
,
3934 .sync_request
= sync_request
,
3935 .resize
= raid5_resize
,
3936 .quiesce
= raid5_quiesce
,
3938 static struct mdk_personality raid5_personality
=
3942 .owner
= THIS_MODULE
,
3943 .make_request
= make_request
,
3947 .error_handler
= error
,
3948 .hot_add_disk
= raid5_add_disk
,
3949 .hot_remove_disk
= raid5_remove_disk
,
3950 .spare_active
= raid5_spare_active
,
3951 .sync_request
= sync_request
,
3952 .resize
= raid5_resize
,
3953 #ifdef CONFIG_MD_RAID5_RESHAPE
3954 .check_reshape
= raid5_check_reshape
,
3955 .start_reshape
= raid5_start_reshape
,
3957 .quiesce
= raid5_quiesce
,
3960 static struct mdk_personality raid4_personality
=
3964 .owner
= THIS_MODULE
,
3965 .make_request
= make_request
,
3969 .error_handler
= error
,
3970 .hot_add_disk
= raid5_add_disk
,
3971 .hot_remove_disk
= raid5_remove_disk
,
3972 .spare_active
= raid5_spare_active
,
3973 .sync_request
= sync_request
,
3974 .resize
= raid5_resize
,
3975 .quiesce
= raid5_quiesce
,
3978 static int __init
raid5_init(void)
3982 e
= raid6_select_algo();
3985 register_md_personality(&raid6_personality
);
3986 register_md_personality(&raid5_personality
);
3987 register_md_personality(&raid4_personality
);
3991 static void raid5_exit(void)
3993 unregister_md_personality(&raid6_personality
);
3994 unregister_md_personality(&raid5_personality
);
3995 unregister_md_personality(&raid4_personality
);
3998 module_init(raid5_init
);
3999 module_exit(raid5_exit
);
4000 MODULE_LICENSE("GPL");
4001 MODULE_ALIAS("md-personality-4"); /* RAID5 */
4002 MODULE_ALIAS("md-raid5");
4003 MODULE_ALIAS("md-raid4");
4004 MODULE_ALIAS("md-level-5");
4005 MODULE_ALIAS("md-level-4");
4006 MODULE_ALIAS("md-personality-8"); /* RAID6 */
4007 MODULE_ALIAS("md-raid6");
4008 MODULE_ALIAS("md-level-6");
4010 /* This used to be two separate modules, they were: */
4011 MODULE_ALIAS("raid5");
4012 MODULE_ALIAS("raid6");