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.
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/highmem.h>
25 #include <linux/bitops.h>
26 #include <linux/kthread.h>
27 #include <asm/atomic.h>
30 #include <linux/raid/bitmap.h>
36 #define NR_STRIPES 256
37 #define STRIPE_SIZE PAGE_SIZE
38 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
39 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
40 #define IO_THRESHOLD 1
41 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
42 #define HASH_MASK (NR_HASH - 1)
44 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
46 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
47 * order without overlap. There may be several bio's per stripe+device, and
48 * a bio could span several devices.
49 * When walking this list for a particular stripe+device, we must never proceed
50 * beyond a bio that extends past this device, as the next bio might no longer
52 * This macro is used to determine the 'next' bio in the list, given the sector
53 * of the current stripe+device
55 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
57 * The following can be used to debug the driver
60 #define RAID5_PARANOIA 1
61 #if RAID5_PARANOIA && defined(CONFIG_SMP)
62 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
64 # define CHECK_DEVLOCK()
67 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
73 #if !RAID6_USE_EMPTY_ZERO_PAGE
74 /* In .bss so it's zeroed */
75 const char raid6_empty_zero_page
[PAGE_SIZE
] __attribute__((aligned(256)));
78 static inline int raid6_next_disk(int disk
, int raid_disks
)
81 return (disk
< raid_disks
) ? disk
: 0;
83 static void print_raid5_conf (raid5_conf_t
*conf
);
85 static void __release_stripe(raid5_conf_t
*conf
, struct stripe_head
*sh
)
87 if (atomic_dec_and_test(&sh
->count
)) {
88 BUG_ON(!list_empty(&sh
->lru
));
89 BUG_ON(atomic_read(&conf
->active_stripes
)==0);
90 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
91 if (test_bit(STRIPE_DELAYED
, &sh
->state
)) {
92 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
93 blk_plug_device(conf
->mddev
->queue
);
94 } else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
95 conf
->seq_write
== sh
->bm_seq
) {
96 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
97 blk_plug_device(conf
->mddev
->queue
);
99 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
100 list_add_tail(&sh
->lru
, &conf
->handle_list
);
102 md_wakeup_thread(conf
->mddev
->thread
);
104 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
105 atomic_dec(&conf
->preread_active_stripes
);
106 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
107 md_wakeup_thread(conf
->mddev
->thread
);
109 atomic_dec(&conf
->active_stripes
);
110 if (!test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
111 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
112 wake_up(&conf
->wait_for_stripe
);
117 static void release_stripe(struct stripe_head
*sh
)
119 raid5_conf_t
*conf
= sh
->raid_conf
;
122 spin_lock_irqsave(&conf
->device_lock
, flags
);
123 __release_stripe(conf
, sh
);
124 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
127 static inline void remove_hash(struct stripe_head
*sh
)
129 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
131 hlist_del_init(&sh
->hash
);
134 static inline void insert_hash(raid5_conf_t
*conf
, struct stripe_head
*sh
)
136 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
138 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
141 hlist_add_head(&sh
->hash
, hp
);
145 /* find an idle stripe, make sure it is unhashed, and return it. */
146 static struct stripe_head
*get_free_stripe(raid5_conf_t
*conf
)
148 struct stripe_head
*sh
= NULL
;
149 struct list_head
*first
;
152 if (list_empty(&conf
->inactive_list
))
154 first
= conf
->inactive_list
.next
;
155 sh
= list_entry(first
, struct stripe_head
, lru
);
156 list_del_init(first
);
158 atomic_inc(&conf
->active_stripes
);
163 static void shrink_buffers(struct stripe_head
*sh
, int num
)
168 for (i
=0; i
<num
; i
++) {
172 sh
->dev
[i
].page
= NULL
;
177 static int grow_buffers(struct stripe_head
*sh
, int num
)
181 for (i
=0; i
<num
; i
++) {
184 if (!(page
= alloc_page(GFP_KERNEL
))) {
187 sh
->dev
[i
].page
= page
;
192 static void raid5_build_block (struct stripe_head
*sh
, int i
);
194 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int pd_idx
, int disks
)
196 raid5_conf_t
*conf
= sh
->raid_conf
;
199 BUG_ON(atomic_read(&sh
->count
) != 0);
200 BUG_ON(test_bit(STRIPE_HANDLE
, &sh
->state
));
203 PRINTK("init_stripe called, stripe %llu\n",
204 (unsigned long long)sh
->sector
);
214 for (i
= sh
->disks
; i
--; ) {
215 struct r5dev
*dev
= &sh
->dev
[i
];
217 if (dev
->toread
|| dev
->towrite
|| dev
->written
||
218 test_bit(R5_LOCKED
, &dev
->flags
)) {
219 printk("sector=%llx i=%d %p %p %p %d\n",
220 (unsigned long long)sh
->sector
, i
, dev
->toread
,
221 dev
->towrite
, dev
->written
,
222 test_bit(R5_LOCKED
, &dev
->flags
));
226 raid5_build_block(sh
, i
);
228 insert_hash(conf
, sh
);
231 static struct stripe_head
*__find_stripe(raid5_conf_t
*conf
, sector_t sector
, int disks
)
233 struct stripe_head
*sh
;
234 struct hlist_node
*hn
;
237 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector
);
238 hlist_for_each_entry(sh
, hn
, stripe_hash(conf
, sector
), hash
)
239 if (sh
->sector
== sector
&& sh
->disks
== disks
)
241 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector
);
245 static void unplug_slaves(mddev_t
*mddev
);
246 static void raid5_unplug_device(request_queue_t
*q
);
248 static struct stripe_head
*get_active_stripe(raid5_conf_t
*conf
, sector_t sector
, int disks
,
249 int pd_idx
, int noblock
)
251 struct stripe_head
*sh
;
253 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector
);
255 spin_lock_irq(&conf
->device_lock
);
258 wait_event_lock_irq(conf
->wait_for_stripe
,
260 conf
->device_lock
, /* nothing */);
261 sh
= __find_stripe(conf
, sector
, disks
);
263 if (!conf
->inactive_blocked
)
264 sh
= get_free_stripe(conf
);
265 if (noblock
&& sh
== NULL
)
268 conf
->inactive_blocked
= 1;
269 wait_event_lock_irq(conf
->wait_for_stripe
,
270 !list_empty(&conf
->inactive_list
) &&
271 (atomic_read(&conf
->active_stripes
)
272 < (conf
->max_nr_stripes
*3/4)
273 || !conf
->inactive_blocked
),
275 raid5_unplug_device(conf
->mddev
->queue
)
277 conf
->inactive_blocked
= 0;
279 init_stripe(sh
, sector
, pd_idx
, disks
);
281 if (atomic_read(&sh
->count
)) {
282 BUG_ON(!list_empty(&sh
->lru
));
284 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
285 atomic_inc(&conf
->active_stripes
);
286 if (list_empty(&sh
->lru
) &&
287 !test_bit(STRIPE_EXPANDING
, &sh
->state
))
289 list_del_init(&sh
->lru
);
292 } while (sh
== NULL
);
295 atomic_inc(&sh
->count
);
297 spin_unlock_irq(&conf
->device_lock
);
301 static int grow_one_stripe(raid5_conf_t
*conf
)
303 struct stripe_head
*sh
;
304 sh
= kmem_cache_alloc(conf
->slab_cache
, GFP_KERNEL
);
307 memset(sh
, 0, sizeof(*sh
) + (conf
->raid_disks
-1)*sizeof(struct r5dev
));
308 sh
->raid_conf
= conf
;
309 spin_lock_init(&sh
->lock
);
311 if (grow_buffers(sh
, conf
->raid_disks
)) {
312 shrink_buffers(sh
, conf
->raid_disks
);
313 kmem_cache_free(conf
->slab_cache
, sh
);
316 sh
->disks
= conf
->raid_disks
;
317 /* we just created an active stripe so... */
318 atomic_set(&sh
->count
, 1);
319 atomic_inc(&conf
->active_stripes
);
320 INIT_LIST_HEAD(&sh
->lru
);
325 static int grow_stripes(raid5_conf_t
*conf
, int num
)
328 int devs
= conf
->raid_disks
;
330 sprintf(conf
->cache_name
[0], "raid5/%s", mdname(conf
->mddev
));
331 sprintf(conf
->cache_name
[1], "raid5/%s-alt", mdname(conf
->mddev
));
332 conf
->active_name
= 0;
333 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
334 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
338 conf
->slab_cache
= sc
;
339 conf
->pool_size
= devs
;
341 if (!grow_one_stripe(conf
))
346 #ifdef CONFIG_MD_RAID5_RESHAPE
347 static int resize_stripes(raid5_conf_t
*conf
, int newsize
)
349 /* Make all the stripes able to hold 'newsize' devices.
350 * New slots in each stripe get 'page' set to a new page.
352 * This happens in stages:
353 * 1/ create a new kmem_cache and allocate the required number of
355 * 2/ gather all the old stripe_heads and tranfer the pages across
356 * to the new stripe_heads. This will have the side effect of
357 * freezing the array as once all stripe_heads have been collected,
358 * no IO will be possible. Old stripe heads are freed once their
359 * pages have been transferred over, and the old kmem_cache is
360 * freed when all stripes are done.
361 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
362 * we simple return a failre status - no need to clean anything up.
363 * 4/ allocate new pages for the new slots in the new stripe_heads.
364 * If this fails, we don't bother trying the shrink the
365 * stripe_heads down again, we just leave them as they are.
366 * As each stripe_head is processed the new one is released into
369 * Once step2 is started, we cannot afford to wait for a write,
370 * so we use GFP_NOIO allocations.
372 struct stripe_head
*osh
, *nsh
;
373 LIST_HEAD(newstripes
);
374 struct disk_info
*ndisks
;
379 if (newsize
<= conf
->pool_size
)
380 return 0; /* never bother to shrink */
383 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
384 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
389 for (i
= conf
->max_nr_stripes
; i
; i
--) {
390 nsh
= kmem_cache_alloc(sc
, GFP_KERNEL
);
394 memset(nsh
, 0, sizeof(*nsh
) + (newsize
-1)*sizeof(struct r5dev
));
396 nsh
->raid_conf
= conf
;
397 spin_lock_init(&nsh
->lock
);
399 list_add(&nsh
->lru
, &newstripes
);
402 /* didn't get enough, give up */
403 while (!list_empty(&newstripes
)) {
404 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
406 kmem_cache_free(sc
, nsh
);
408 kmem_cache_destroy(sc
);
411 /* Step 2 - Must use GFP_NOIO now.
412 * OK, we have enough stripes, start collecting inactive
413 * stripes and copying them over
415 list_for_each_entry(nsh
, &newstripes
, lru
) {
416 spin_lock_irq(&conf
->device_lock
);
417 wait_event_lock_irq(conf
->wait_for_stripe
,
418 !list_empty(&conf
->inactive_list
),
420 unplug_slaves(conf
->mddev
)
422 osh
= get_free_stripe(conf
);
423 spin_unlock_irq(&conf
->device_lock
);
424 atomic_set(&nsh
->count
, 1);
425 for(i
=0; i
<conf
->pool_size
; i
++)
426 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
427 for( ; i
<newsize
; i
++)
428 nsh
->dev
[i
].page
= NULL
;
429 kmem_cache_free(conf
->slab_cache
, osh
);
431 kmem_cache_destroy(conf
->slab_cache
);
434 * At this point, we are holding all the stripes so the array
435 * is completely stalled, so now is a good time to resize
438 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
440 for (i
=0; i
<conf
->raid_disks
; i
++)
441 ndisks
[i
] = conf
->disks
[i
];
443 conf
->disks
= ndisks
;
447 /* Step 4, return new stripes to service */
448 while(!list_empty(&newstripes
)) {
449 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
450 list_del_init(&nsh
->lru
);
451 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
452 if (nsh
->dev
[i
].page
== NULL
) {
453 struct page
*p
= alloc_page(GFP_NOIO
);
454 nsh
->dev
[i
].page
= p
;
460 /* critical section pass, GFP_NOIO no longer needed */
462 conf
->slab_cache
= sc
;
463 conf
->active_name
= 1-conf
->active_name
;
464 conf
->pool_size
= newsize
;
469 static int drop_one_stripe(raid5_conf_t
*conf
)
471 struct stripe_head
*sh
;
473 spin_lock_irq(&conf
->device_lock
);
474 sh
= get_free_stripe(conf
);
475 spin_unlock_irq(&conf
->device_lock
);
478 BUG_ON(atomic_read(&sh
->count
));
479 shrink_buffers(sh
, conf
->pool_size
);
480 kmem_cache_free(conf
->slab_cache
, sh
);
481 atomic_dec(&conf
->active_stripes
);
485 static void shrink_stripes(raid5_conf_t
*conf
)
487 while (drop_one_stripe(conf
))
490 if (conf
->slab_cache
)
491 kmem_cache_destroy(conf
->slab_cache
);
492 conf
->slab_cache
= NULL
;
495 static int raid5_end_read_request(struct bio
* bi
, unsigned int bytes_done
,
498 struct stripe_head
*sh
= bi
->bi_private
;
499 raid5_conf_t
*conf
= sh
->raid_conf
;
500 int disks
= sh
->disks
, i
;
501 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
506 for (i
=0 ; i
<disks
; i
++)
507 if (bi
== &sh
->dev
[i
].req
)
510 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
511 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
522 spin_lock_irqsave(&conf
->device_lock
, flags
);
523 /* we can return a buffer if we bypassed the cache or
524 * if the top buffer is not in highmem. If there are
525 * multiple buffers, leave the extra work to
528 buffer
= sh
->bh_read
[i
];
530 (!PageHighMem(buffer
->b_page
)
531 || buffer
->b_page
== bh
->b_page
)
533 sh
->bh_read
[i
] = buffer
->b_reqnext
;
534 buffer
->b_reqnext
= NULL
;
537 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
538 if (sh
->bh_page
[i
]==bh
->b_page
)
539 set_buffer_uptodate(bh
);
541 if (buffer
->b_page
!= bh
->b_page
)
542 memcpy(buffer
->b_data
, bh
->b_data
, bh
->b_size
);
543 buffer
->b_end_io(buffer
, 1);
546 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
548 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
549 printk(KERN_INFO
"raid5: read error corrected!!\n");
550 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
551 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
553 if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
))
554 atomic_set(&conf
->disks
[i
].rdev
->read_errors
, 0);
557 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
558 atomic_inc(&conf
->disks
[i
].rdev
->read_errors
);
559 if (conf
->mddev
->degraded
)
560 printk(KERN_WARNING
"raid5: read error not correctable.\n");
561 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
563 printk(KERN_WARNING
"raid5: read error NOT corrected!!\n");
564 else if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
)
565 > conf
->max_nr_stripes
)
567 "raid5: Too many read errors, failing device.\n");
571 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
573 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
574 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
575 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
578 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
580 /* must restore b_page before unlocking buffer... */
581 if (sh
->bh_page
[i
] != bh
->b_page
) {
582 bh
->b_page
= sh
->bh_page
[i
];
583 bh
->b_data
= page_address(bh
->b_page
);
584 clear_buffer_uptodate(bh
);
587 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
588 set_bit(STRIPE_HANDLE
, &sh
->state
);
593 static int raid5_end_write_request (struct bio
*bi
, unsigned int bytes_done
,
596 struct stripe_head
*sh
= bi
->bi_private
;
597 raid5_conf_t
*conf
= sh
->raid_conf
;
598 int disks
= sh
->disks
, i
;
600 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
605 for (i
=0 ; i
<disks
; i
++)
606 if (bi
== &sh
->dev
[i
].req
)
609 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
610 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
617 spin_lock_irqsave(&conf
->device_lock
, flags
);
619 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
621 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
623 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
624 set_bit(STRIPE_HANDLE
, &sh
->state
);
625 __release_stripe(conf
, sh
);
626 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
631 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
);
633 static void raid5_build_block (struct stripe_head
*sh
, int i
)
635 struct r5dev
*dev
= &sh
->dev
[i
];
638 dev
->req
.bi_io_vec
= &dev
->vec
;
640 dev
->req
.bi_max_vecs
++;
641 dev
->vec
.bv_page
= dev
->page
;
642 dev
->vec
.bv_len
= STRIPE_SIZE
;
643 dev
->vec
.bv_offset
= 0;
645 dev
->req
.bi_sector
= sh
->sector
;
646 dev
->req
.bi_private
= sh
;
649 dev
->sector
= compute_blocknr(sh
, i
);
652 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
654 char b
[BDEVNAME_SIZE
];
655 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
656 PRINTK("raid5: error called\n");
658 if (!test_bit(Faulty
, &rdev
->flags
)) {
660 if (test_bit(In_sync
, &rdev
->flags
)) {
661 conf
->working_disks
--;
663 conf
->failed_disks
++;
664 clear_bit(In_sync
, &rdev
->flags
);
666 * if recovery was running, make sure it aborts.
668 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
670 set_bit(Faulty
, &rdev
->flags
);
672 "raid5: Disk failure on %s, disabling device."
673 " Operation continuing on %d devices\n",
674 bdevname(rdev
->bdev
,b
), conf
->working_disks
);
679 * Input: a 'big' sector number,
680 * Output: index of the data and parity disk, and the sector # in them.
682 static sector_t
raid5_compute_sector(sector_t r_sector
, unsigned int raid_disks
,
683 unsigned int data_disks
, unsigned int * dd_idx
,
684 unsigned int * pd_idx
, raid5_conf_t
*conf
)
687 unsigned long chunk_number
;
688 unsigned int chunk_offset
;
690 int sectors_per_chunk
= conf
->chunk_size
>> 9;
692 /* First compute the information on this sector */
695 * Compute the chunk number and the sector offset inside the chunk
697 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
698 chunk_number
= r_sector
;
699 BUG_ON(r_sector
!= chunk_number
);
702 * Compute the stripe number
704 stripe
= chunk_number
/ data_disks
;
707 * Compute the data disk and parity disk indexes inside the stripe
709 *dd_idx
= chunk_number
% data_disks
;
712 * Select the parity disk based on the user selected algorithm.
714 switch(conf
->level
) {
716 *pd_idx
= data_disks
;
719 switch (conf
->algorithm
) {
720 case ALGORITHM_LEFT_ASYMMETRIC
:
721 *pd_idx
= data_disks
- stripe
% raid_disks
;
722 if (*dd_idx
>= *pd_idx
)
725 case ALGORITHM_RIGHT_ASYMMETRIC
:
726 *pd_idx
= stripe
% raid_disks
;
727 if (*dd_idx
>= *pd_idx
)
730 case ALGORITHM_LEFT_SYMMETRIC
:
731 *pd_idx
= data_disks
- stripe
% raid_disks
;
732 *dd_idx
= (*pd_idx
+ 1 + *dd_idx
) % raid_disks
;
734 case ALGORITHM_RIGHT_SYMMETRIC
:
735 *pd_idx
= stripe
% raid_disks
;
736 *dd_idx
= (*pd_idx
+ 1 + *dd_idx
) % raid_disks
;
739 printk(KERN_ERR
"raid5: unsupported algorithm %d\n",
746 switch (conf
->algorithm
) {
747 case ALGORITHM_LEFT_ASYMMETRIC
:
748 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
749 if (*pd_idx
== raid_disks
-1)
750 (*dd_idx
)++; /* Q D D D P */
751 else if (*dd_idx
>= *pd_idx
)
752 (*dd_idx
) += 2; /* D D P Q D */
754 case ALGORITHM_RIGHT_ASYMMETRIC
:
755 *pd_idx
= stripe
% raid_disks
;
756 if (*pd_idx
== raid_disks
-1)
757 (*dd_idx
)++; /* Q D D D P */
758 else if (*dd_idx
>= *pd_idx
)
759 (*dd_idx
) += 2; /* D D P Q D */
761 case ALGORITHM_LEFT_SYMMETRIC
:
762 *pd_idx
= raid_disks
- 1 - (stripe
% raid_disks
);
763 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
765 case ALGORITHM_RIGHT_SYMMETRIC
:
766 *pd_idx
= stripe
% raid_disks
;
767 *dd_idx
= (*pd_idx
+ 2 + *dd_idx
) % raid_disks
;
770 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
777 * Finally, compute the new sector number
779 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
784 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
)
786 raid5_conf_t
*conf
= sh
->raid_conf
;
787 int raid_disks
= sh
->disks
, data_disks
= raid_disks
- 1;
788 sector_t new_sector
= sh
->sector
, check
;
789 int sectors_per_chunk
= conf
->chunk_size
>> 9;
792 int chunk_number
, dummy1
, dummy2
, dd_idx
= i
;
796 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
798 BUG_ON(new_sector
!= stripe
);
802 switch(conf
->level
) {
805 switch (conf
->algorithm
) {
806 case ALGORITHM_LEFT_ASYMMETRIC
:
807 case ALGORITHM_RIGHT_ASYMMETRIC
:
811 case ALGORITHM_LEFT_SYMMETRIC
:
812 case ALGORITHM_RIGHT_SYMMETRIC
:
815 i
-= (sh
->pd_idx
+ 1);
818 printk(KERN_ERR
"raid5: unsupported algorithm %d\n",
823 data_disks
= raid_disks
- 2;
824 if (i
== raid6_next_disk(sh
->pd_idx
, raid_disks
))
825 return 0; /* It is the Q disk */
826 switch (conf
->algorithm
) {
827 case ALGORITHM_LEFT_ASYMMETRIC
:
828 case ALGORITHM_RIGHT_ASYMMETRIC
:
829 if (sh
->pd_idx
== raid_disks
-1)
831 else if (i
> sh
->pd_idx
)
832 i
-= 2; /* D D P Q D */
834 case ALGORITHM_LEFT_SYMMETRIC
:
835 case ALGORITHM_RIGHT_SYMMETRIC
:
836 if (sh
->pd_idx
== raid_disks
-1)
842 i
-= (sh
->pd_idx
+ 2);
846 printk (KERN_CRIT
"raid6: unsupported algorithm %d\n",
852 chunk_number
= stripe
* data_disks
+ i
;
853 r_sector
= (sector_t
)chunk_number
* sectors_per_chunk
+ chunk_offset
;
855 check
= raid5_compute_sector (r_sector
, raid_disks
, data_disks
, &dummy1
, &dummy2
, conf
);
856 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| dummy2
!= sh
->pd_idx
) {
857 printk(KERN_ERR
"compute_blocknr: map not correct\n");
866 * Copy data between a page in the stripe cache, and one or more bion
867 * The page could align with the middle of the bio, or there could be
868 * several bion, each with several bio_vecs, which cover part of the page
869 * Multiple bion are linked together on bi_next. There may be extras
870 * at the end of this list. We ignore them.
872 static void copy_data(int frombio
, struct bio
*bio
,
876 char *pa
= page_address(page
);
881 if (bio
->bi_sector
>= sector
)
882 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
884 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
885 bio_for_each_segment(bvl
, bio
, i
) {
886 int len
= bio_iovec_idx(bio
,i
)->bv_len
;
890 if (page_offset
< 0) {
891 b_offset
= -page_offset
;
892 page_offset
+= b_offset
;
896 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
897 clen
= STRIPE_SIZE
- page_offset
;
901 char *ba
= __bio_kmap_atomic(bio
, i
, KM_USER0
);
903 memcpy(pa
+page_offset
, ba
+b_offset
, clen
);
905 memcpy(ba
+b_offset
, pa
+page_offset
, clen
);
906 __bio_kunmap_atomic(ba
, KM_USER0
);
908 if (clen
< len
) /* hit end of page */
914 #define check_xor() do { \
915 if (count == MAX_XOR_BLOCKS) { \
916 xor_block(count, STRIPE_SIZE, ptr); \
922 static void compute_block(struct stripe_head
*sh
, int dd_idx
)
924 int i
, count
, disks
= sh
->disks
;
925 void *ptr
[MAX_XOR_BLOCKS
], *p
;
927 PRINTK("compute_block, stripe %llu, idx %d\n",
928 (unsigned long long)sh
->sector
, dd_idx
);
930 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
931 memset(ptr
[0], 0, STRIPE_SIZE
);
933 for (i
= disks
; i
--; ) {
936 p
= page_address(sh
->dev
[i
].page
);
937 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
940 printk(KERN_ERR
"compute_block() %d, stripe %llu, %d"
941 " not present\n", dd_idx
,
942 (unsigned long long)sh
->sector
, i
);
947 xor_block(count
, STRIPE_SIZE
, ptr
);
948 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
951 static void compute_parity5(struct stripe_head
*sh
, int method
)
953 raid5_conf_t
*conf
= sh
->raid_conf
;
954 int i
, pd_idx
= sh
->pd_idx
, disks
= sh
->disks
, count
;
955 void *ptr
[MAX_XOR_BLOCKS
];
958 PRINTK("compute_parity5, stripe %llu, method %d\n",
959 (unsigned long long)sh
->sector
, method
);
962 ptr
[0] = page_address(sh
->dev
[pd_idx
].page
);
964 case READ_MODIFY_WRITE
:
965 BUG_ON(!test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
));
966 for (i
=disks
; i
-- ;) {
969 if (sh
->dev
[i
].towrite
&&
970 test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
)) {
971 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
972 chosen
= sh
->dev
[i
].towrite
;
973 sh
->dev
[i
].towrite
= NULL
;
975 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
976 wake_up(&conf
->wait_for_overlap
);
978 BUG_ON(sh
->dev
[i
].written
);
979 sh
->dev
[i
].written
= chosen
;
984 case RECONSTRUCT_WRITE
:
985 memset(ptr
[0], 0, STRIPE_SIZE
);
986 for (i
= disks
; i
-- ;)
987 if (i
!=pd_idx
&& sh
->dev
[i
].towrite
) {
988 chosen
= sh
->dev
[i
].towrite
;
989 sh
->dev
[i
].towrite
= NULL
;
991 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
992 wake_up(&conf
->wait_for_overlap
);
994 BUG_ON(sh
->dev
[i
].written
);
995 sh
->dev
[i
].written
= chosen
;
1002 xor_block(count
, STRIPE_SIZE
, ptr
);
1006 for (i
= disks
; i
--;)
1007 if (sh
->dev
[i
].written
) {
1008 sector_t sector
= sh
->dev
[i
].sector
;
1009 struct bio
*wbi
= sh
->dev
[i
].written
;
1010 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
1011 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
1012 wbi
= r5_next_bio(wbi
, sector
);
1015 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1016 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1020 case RECONSTRUCT_WRITE
:
1024 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
1028 case READ_MODIFY_WRITE
:
1029 for (i
= disks
; i
--;)
1030 if (sh
->dev
[i
].written
) {
1031 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
1036 xor_block(count
, STRIPE_SIZE
, ptr
);
1038 if (method
!= CHECK_PARITY
) {
1039 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1040 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
1042 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1045 static void compute_parity6(struct stripe_head
*sh
, int method
)
1047 raid6_conf_t
*conf
= sh
->raid_conf
;
1048 int i
, pd_idx
= sh
->pd_idx
, qd_idx
, d0_idx
, disks
= conf
->raid_disks
, count
;
1050 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1053 qd_idx
= raid6_next_disk(pd_idx
, disks
);
1054 d0_idx
= raid6_next_disk(qd_idx
, disks
);
1056 PRINTK("compute_parity, stripe %llu, method %d\n",
1057 (unsigned long long)sh
->sector
, method
);
1060 case READ_MODIFY_WRITE
:
1061 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1062 case RECONSTRUCT_WRITE
:
1063 for (i
= disks
; i
-- ;)
1064 if ( i
!= pd_idx
&& i
!= qd_idx
&& sh
->dev
[i
].towrite
) {
1065 chosen
= sh
->dev
[i
].towrite
;
1066 sh
->dev
[i
].towrite
= NULL
;
1068 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1069 wake_up(&conf
->wait_for_overlap
);
1071 if (sh
->dev
[i
].written
) BUG();
1072 sh
->dev
[i
].written
= chosen
;
1076 BUG(); /* Not implemented yet */
1079 for (i
= disks
; i
--;)
1080 if (sh
->dev
[i
].written
) {
1081 sector_t sector
= sh
->dev
[i
].sector
;
1082 struct bio
*wbi
= sh
->dev
[i
].written
;
1083 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
1084 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
1085 wbi
= r5_next_bio(wbi
, sector
);
1088 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1089 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
1093 // case RECONSTRUCT_WRITE:
1094 // case CHECK_PARITY:
1095 // case UPDATE_PARITY:
1096 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1097 /* FIX: Is this ordering of drives even remotely optimal? */
1101 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
1102 if (count
<= disks
-2 && !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
1103 printk("block %d/%d not uptodate on parity calc\n", i
,count
);
1104 i
= raid6_next_disk(i
, disks
);
1105 } while ( i
!= d0_idx
);
1109 raid6_call
.gen_syndrome(disks
, STRIPE_SIZE
, ptrs
);
1112 case RECONSTRUCT_WRITE
:
1113 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1114 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
1115 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
1116 set_bit(R5_LOCKED
, &sh
->dev
[qd_idx
].flags
);
1119 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
1120 set_bit(R5_UPTODATE
, &sh
->dev
[qd_idx
].flags
);
1126 /* Compute one missing block */
1127 static void compute_block_1(struct stripe_head
*sh
, int dd_idx
, int nozero
)
1129 raid6_conf_t
*conf
= sh
->raid_conf
;
1130 int i
, count
, disks
= conf
->raid_disks
;
1131 void *ptr
[MAX_XOR_BLOCKS
], *p
;
1132 int pd_idx
= sh
->pd_idx
;
1133 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1135 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1136 (unsigned long long)sh
->sector
, dd_idx
);
1138 if ( dd_idx
== qd_idx
) {
1139 /* We're actually computing the Q drive */
1140 compute_parity6(sh
, UPDATE_PARITY
);
1142 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
1143 if (!nozero
) memset(ptr
[0], 0, STRIPE_SIZE
);
1145 for (i
= disks
; i
--; ) {
1146 if (i
== dd_idx
|| i
== qd_idx
)
1148 p
= page_address(sh
->dev
[i
].page
);
1149 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
1152 printk("compute_block() %d, stripe %llu, %d"
1153 " not present\n", dd_idx
,
1154 (unsigned long long)sh
->sector
, i
);
1159 xor_block(count
, STRIPE_SIZE
, ptr
);
1160 if (!nozero
) set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
1161 else clear_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
1165 /* Compute two missing blocks */
1166 static void compute_block_2(struct stripe_head
*sh
, int dd_idx1
, int dd_idx2
)
1168 raid6_conf_t
*conf
= sh
->raid_conf
;
1169 int i
, count
, disks
= conf
->raid_disks
;
1170 int pd_idx
= sh
->pd_idx
;
1171 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1172 int d0_idx
= raid6_next_disk(qd_idx
, disks
);
1175 /* faila and failb are disk numbers relative to d0_idx */
1176 /* pd_idx become disks-2 and qd_idx become disks-1 */
1177 faila
= (dd_idx1
< d0_idx
) ? dd_idx1
+(disks
-d0_idx
) : dd_idx1
-d0_idx
;
1178 failb
= (dd_idx2
< d0_idx
) ? dd_idx2
+(disks
-d0_idx
) : dd_idx2
-d0_idx
;
1180 BUG_ON(faila
== failb
);
1181 if ( failb
< faila
) { int tmp
= faila
; faila
= failb
; failb
= tmp
; }
1183 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1184 (unsigned long long)sh
->sector
, dd_idx1
, dd_idx2
, faila
, failb
);
1186 if ( failb
== disks
-1 ) {
1187 /* Q disk is one of the missing disks */
1188 if ( faila
== disks
-2 ) {
1189 /* Missing P+Q, just recompute */
1190 compute_parity6(sh
, UPDATE_PARITY
);
1193 /* We're missing D+Q; recompute D from P */
1194 compute_block_1(sh
, (dd_idx1
== qd_idx
) ? dd_idx2
: dd_idx1
, 0);
1195 compute_parity6(sh
, UPDATE_PARITY
); /* Is this necessary? */
1200 /* We're missing D+P or D+D; build pointer table */
1202 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1208 ptrs
[count
++] = page_address(sh
->dev
[i
].page
);
1209 i
= raid6_next_disk(i
, disks
);
1210 if (i
!= dd_idx1
&& i
!= dd_idx2
&&
1211 !test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
1212 printk("compute_2 with missing block %d/%d\n", count
, i
);
1213 } while ( i
!= d0_idx
);
1215 if ( failb
== disks
-2 ) {
1216 /* We're missing D+P. */
1217 raid6_datap_recov(disks
, STRIPE_SIZE
, faila
, ptrs
);
1219 /* We're missing D+D. */
1220 raid6_2data_recov(disks
, STRIPE_SIZE
, faila
, failb
, ptrs
);
1223 /* Both the above update both missing blocks */
1224 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx1
].flags
);
1225 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx2
].flags
);
1232 * Each stripe/dev can have one or more bion attached.
1233 * toread/towrite point to the first in a chain.
1234 * The bi_next chain must be in order.
1236 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
1239 raid5_conf_t
*conf
= sh
->raid_conf
;
1242 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1243 (unsigned long long)bi
->bi_sector
,
1244 (unsigned long long)sh
->sector
);
1247 spin_lock(&sh
->lock
);
1248 spin_lock_irq(&conf
->device_lock
);
1250 bip
= &sh
->dev
[dd_idx
].towrite
;
1251 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
1254 bip
= &sh
->dev
[dd_idx
].toread
;
1255 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
1256 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
1258 bip
= & (*bip
)->bi_next
;
1260 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
1263 BUG_ON(*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
);
1267 bi
->bi_phys_segments
++;
1268 spin_unlock_irq(&conf
->device_lock
);
1269 spin_unlock(&sh
->lock
);
1271 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1272 (unsigned long long)bi
->bi_sector
,
1273 (unsigned long long)sh
->sector
, dd_idx
);
1275 if (conf
->mddev
->bitmap
&& firstwrite
) {
1276 sh
->bm_seq
= conf
->seq_write
;
1277 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
1279 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
1283 /* check if page is covered */
1284 sector_t sector
= sh
->dev
[dd_idx
].sector
;
1285 for (bi
=sh
->dev
[dd_idx
].towrite
;
1286 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
1287 bi
&& bi
->bi_sector
<= sector
;
1288 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
1289 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
1290 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1292 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
1293 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
1298 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
1299 spin_unlock_irq(&conf
->device_lock
);
1300 spin_unlock(&sh
->lock
);
1304 static void end_reshape(raid5_conf_t
*conf
);
1306 static int page_is_zero(struct page
*p
)
1308 char *a
= page_address(p
);
1309 return ((*(u32
*)a
) == 0 &&
1310 memcmp(a
, a
+4, STRIPE_SIZE
-4)==0);
1313 static int stripe_to_pdidx(sector_t stripe
, raid5_conf_t
*conf
, int disks
)
1315 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1316 sector_t x
= stripe
;
1318 int chunk_offset
= sector_div(x
, sectors_per_chunk
);
1320 raid5_compute_sector(stripe
*(disks
-1)*sectors_per_chunk
1321 + chunk_offset
, disks
, disks
-1, &dd_idx
, &pd_idx
, conf
);
1327 * handle_stripe - do things to a stripe.
1329 * We lock the stripe and then examine the state of various bits
1330 * to see what needs to be done.
1332 * return some read request which now have data
1333 * return some write requests which are safely on disc
1334 * schedule a read on some buffers
1335 * schedule a write of some buffers
1336 * return confirmation of parity correctness
1338 * Parity calculations are done inside the stripe lock
1339 * buffers are taken off read_list or write_list, and bh_cache buffers
1340 * get BH_Lock set before the stripe lock is released.
1344 static void handle_stripe5(struct stripe_head
*sh
)
1346 raid5_conf_t
*conf
= sh
->raid_conf
;
1347 int disks
= sh
->disks
;
1348 struct bio
*return_bi
= NULL
;
1351 int syncing
, expanding
, expanded
;
1352 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
1353 int non_overwrite
= 0;
1357 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1358 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
),
1361 spin_lock(&sh
->lock
);
1362 clear_bit(STRIPE_HANDLE
, &sh
->state
);
1363 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1365 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
1366 expanding
= test_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
1367 expanded
= test_bit(STRIPE_EXPAND_READY
, &sh
->state
);
1368 /* Now to look around and see what can be done */
1371 for (i
=disks
; i
--; ) {
1374 clear_bit(R5_Insync
, &dev
->flags
);
1376 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1377 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
1378 /* maybe we can reply to a read */
1379 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
1380 struct bio
*rbi
, *rbi2
;
1381 PRINTK("Return read for disc %d\n", i
);
1382 spin_lock_irq(&conf
->device_lock
);
1385 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1386 wake_up(&conf
->wait_for_overlap
);
1387 spin_unlock_irq(&conf
->device_lock
);
1388 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1389 copy_data(0, rbi
, dev
->page
, dev
->sector
);
1390 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1391 spin_lock_irq(&conf
->device_lock
);
1392 if (--rbi
->bi_phys_segments
== 0) {
1393 rbi
->bi_next
= return_bi
;
1396 spin_unlock_irq(&conf
->device_lock
);
1401 /* now count some things */
1402 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
1403 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
1406 if (dev
->toread
) to_read
++;
1409 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1412 if (dev
->written
) written
++;
1413 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1414 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1415 /* The ReadError flag will just be confusing now */
1416 clear_bit(R5_ReadError
, &dev
->flags
);
1417 clear_bit(R5_ReWrite
, &dev
->flags
);
1419 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1420 || test_bit(R5_ReadError
, &dev
->flags
)) {
1424 set_bit(R5_Insync
, &dev
->flags
);
1427 PRINTK("locked=%d uptodate=%d to_read=%d"
1428 " to_write=%d failed=%d failed_num=%d\n",
1429 locked
, uptodate
, to_read
, to_write
, failed
, failed_num
);
1430 /* check if the array has lost two devices and, if so, some requests might
1433 if (failed
> 1 && to_read
+to_write
+written
) {
1434 for (i
=disks
; i
--; ) {
1437 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1440 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1441 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1442 /* multiple read failures in one stripe */
1443 md_error(conf
->mddev
, rdev
);
1447 spin_lock_irq(&conf
->device_lock
);
1448 /* fail all writes first */
1449 bi
= sh
->dev
[i
].towrite
;
1450 sh
->dev
[i
].towrite
= NULL
;
1451 if (bi
) { to_write
--; bitmap_end
= 1; }
1453 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1454 wake_up(&conf
->wait_for_overlap
);
1456 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1457 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1458 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1459 if (--bi
->bi_phys_segments
== 0) {
1460 md_write_end(conf
->mddev
);
1461 bi
->bi_next
= return_bi
;
1466 /* and fail all 'written' */
1467 bi
= sh
->dev
[i
].written
;
1468 sh
->dev
[i
].written
= NULL
;
1469 if (bi
) bitmap_end
= 1;
1470 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
1471 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1472 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1473 if (--bi
->bi_phys_segments
== 0) {
1474 md_write_end(conf
->mddev
);
1475 bi
->bi_next
= return_bi
;
1481 /* fail any reads if this device is non-operational */
1482 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
1483 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1484 bi
= sh
->dev
[i
].toread
;
1485 sh
->dev
[i
].toread
= NULL
;
1486 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1487 wake_up(&conf
->wait_for_overlap
);
1489 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1490 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1491 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1492 if (--bi
->bi_phys_segments
== 0) {
1493 bi
->bi_next
= return_bi
;
1499 spin_unlock_irq(&conf
->device_lock
);
1501 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1502 STRIPE_SECTORS
, 0, 0);
1505 if (failed
> 1 && syncing
) {
1506 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
1507 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1511 /* might be able to return some write requests if the parity block
1512 * is safe, or on a failed drive
1514 dev
= &sh
->dev
[sh
->pd_idx
];
1516 ( (test_bit(R5_Insync
, &dev
->flags
) && !test_bit(R5_LOCKED
, &dev
->flags
) &&
1517 test_bit(R5_UPTODATE
, &dev
->flags
))
1518 || (failed
== 1 && failed_num
== sh
->pd_idx
))
1520 /* any written block on an uptodate or failed drive can be returned.
1521 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1522 * never LOCKED, so we don't need to test 'failed' directly.
1524 for (i
=disks
; i
--; )
1525 if (sh
->dev
[i
].written
) {
1527 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
1528 test_bit(R5_UPTODATE
, &dev
->flags
) ) {
1529 /* We can return any write requests */
1530 struct bio
*wbi
, *wbi2
;
1532 PRINTK("Return write for disc %d\n", i
);
1533 spin_lock_irq(&conf
->device_lock
);
1535 dev
->written
= NULL
;
1536 while (wbi
&& wbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1537 wbi2
= r5_next_bio(wbi
, dev
->sector
);
1538 if (--wbi
->bi_phys_segments
== 0) {
1539 md_write_end(conf
->mddev
);
1540 wbi
->bi_next
= return_bi
;
1545 if (dev
->towrite
== NULL
)
1547 spin_unlock_irq(&conf
->device_lock
);
1549 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1551 !test_bit(STRIPE_DEGRADED
, &sh
->state
), 0);
1556 /* Now we might consider reading some blocks, either to check/generate
1557 * parity, or to satisfy requests
1558 * or to load a block that is being partially written.
1560 if (to_read
|| non_overwrite
|| (syncing
&& (uptodate
< disks
)) || expanding
) {
1561 for (i
=disks
; i
--;) {
1563 if (!test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1565 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
1568 (failed
&& (sh
->dev
[failed_num
].toread
||
1569 (sh
->dev
[failed_num
].towrite
&& !test_bit(R5_OVERWRITE
, &sh
->dev
[failed_num
].flags
))))
1572 /* we would like to get this block, possibly
1573 * by computing it, but we might not be able to
1575 if (uptodate
== disks
-1) {
1576 PRINTK("Computing block %d\n", i
);
1577 compute_block(sh
, i
);
1579 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
1580 set_bit(R5_LOCKED
, &dev
->flags
);
1581 set_bit(R5_Wantread
, &dev
->flags
);
1583 /* if I am just reading this block and we don't have
1584 a failed drive, or any pending writes then sidestep the cache */
1585 if (sh
->bh_read
[i
] && !sh
->bh_read
[i
]->b_reqnext
&&
1586 ! syncing
&& !failed
&& !to_write
) {
1587 sh
->bh_cache
[i
]->b_page
= sh
->bh_read
[i
]->b_page
;
1588 sh
->bh_cache
[i
]->b_data
= sh
->bh_read
[i
]->b_data
;
1592 PRINTK("Reading block %d (sync=%d)\n",
1597 set_bit(STRIPE_HANDLE
, &sh
->state
);
1600 /* now to consider writing and what else, if anything should be read */
1603 for (i
=disks
; i
--;) {
1604 /* would I have to read this buffer for read_modify_write */
1606 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
1607 (!test_bit(R5_LOCKED
, &dev
->flags
)
1609 || sh
->bh_page
[i
]!=bh
->b_page
1612 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1613 if (test_bit(R5_Insync
, &dev
->flags
)
1614 /* && !(!mddev->insync && i == sh->pd_idx) */
1617 else rmw
+= 2*disks
; /* cannot read it */
1619 /* Would I have to read this buffer for reconstruct_write */
1620 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
1621 (!test_bit(R5_LOCKED
, &dev
->flags
)
1623 || sh
->bh_page
[i
] != bh
->b_page
1626 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1627 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
1628 else rcw
+= 2*disks
;
1631 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1632 (unsigned long long)sh
->sector
, rmw
, rcw
);
1633 set_bit(STRIPE_HANDLE
, &sh
->state
);
1634 if (rmw
< rcw
&& rmw
> 0)
1635 /* prefer read-modify-write, but need to get some data */
1636 for (i
=disks
; i
--;) {
1638 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
1639 !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1640 test_bit(R5_Insync
, &dev
->flags
)) {
1641 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1643 PRINTK("Read_old block %d for r-m-w\n", i
);
1644 set_bit(R5_LOCKED
, &dev
->flags
);
1645 set_bit(R5_Wantread
, &dev
->flags
);
1648 set_bit(STRIPE_DELAYED
, &sh
->state
);
1649 set_bit(STRIPE_HANDLE
, &sh
->state
);
1653 if (rcw
<= rmw
&& rcw
> 0)
1654 /* want reconstruct write, but need to get some data */
1655 for (i
=disks
; i
--;) {
1657 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
1658 !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1659 test_bit(R5_Insync
, &dev
->flags
)) {
1660 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1662 PRINTK("Read_old block %d for Reconstruct\n", i
);
1663 set_bit(R5_LOCKED
, &dev
->flags
);
1664 set_bit(R5_Wantread
, &dev
->flags
);
1667 set_bit(STRIPE_DELAYED
, &sh
->state
);
1668 set_bit(STRIPE_HANDLE
, &sh
->state
);
1672 /* now if nothing is locked, and if we have enough data, we can start a write request */
1673 if (locked
== 0 && (rcw
== 0 ||rmw
== 0) &&
1674 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
1675 PRINTK("Computing parity...\n");
1676 compute_parity5(sh
, rcw
==0 ? RECONSTRUCT_WRITE
: READ_MODIFY_WRITE
);
1677 /* now every locked buffer is ready to be written */
1679 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
1680 PRINTK("Writing block %d\n", i
);
1682 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1683 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
)
1684 || (i
==sh
->pd_idx
&& failed
== 0))
1685 set_bit(STRIPE_INSYNC
, &sh
->state
);
1687 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
1688 atomic_dec(&conf
->preread_active_stripes
);
1689 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
1690 md_wakeup_thread(conf
->mddev
->thread
);
1695 /* maybe we need to check and possibly fix the parity for this stripe
1696 * Any reads will already have been scheduled, so we just see if enough data
1699 if (syncing
&& locked
== 0 &&
1700 !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1701 set_bit(STRIPE_HANDLE
, &sh
->state
);
1703 BUG_ON(uptodate
!= disks
);
1704 compute_parity5(sh
, CHECK_PARITY
);
1706 if (page_is_zero(sh
->dev
[sh
->pd_idx
].page
)) {
1707 /* parity is correct (on disc, not in buffer any more) */
1708 set_bit(STRIPE_INSYNC
, &sh
->state
);
1710 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
1711 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
1712 /* don't try to repair!! */
1713 set_bit(STRIPE_INSYNC
, &sh
->state
);
1715 compute_block(sh
, sh
->pd_idx
);
1720 if (!test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1721 /* either failed parity check, or recovery is happening */
1723 failed_num
= sh
->pd_idx
;
1724 dev
= &sh
->dev
[failed_num
];
1725 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
1726 BUG_ON(uptodate
!= disks
);
1728 set_bit(R5_LOCKED
, &dev
->flags
);
1729 set_bit(R5_Wantwrite
, &dev
->flags
);
1730 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
1732 set_bit(STRIPE_INSYNC
, &sh
->state
);
1735 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1736 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
1737 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1740 /* If the failed drive is just a ReadError, then we might need to progress
1741 * the repair/check process
1743 if (failed
== 1 && ! conf
->mddev
->ro
&&
1744 test_bit(R5_ReadError
, &sh
->dev
[failed_num
].flags
)
1745 && !test_bit(R5_LOCKED
, &sh
->dev
[failed_num
].flags
)
1746 && test_bit(R5_UPTODATE
, &sh
->dev
[failed_num
].flags
)
1748 dev
= &sh
->dev
[failed_num
];
1749 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
1750 set_bit(R5_Wantwrite
, &dev
->flags
);
1751 set_bit(R5_ReWrite
, &dev
->flags
);
1752 set_bit(R5_LOCKED
, &dev
->flags
);
1755 /* let's read it back */
1756 set_bit(R5_Wantread
, &dev
->flags
);
1757 set_bit(R5_LOCKED
, &dev
->flags
);
1762 if (expanded
&& test_bit(STRIPE_EXPANDING
, &sh
->state
)) {
1763 /* Need to write out all blocks after computing parity */
1764 sh
->disks
= conf
->raid_disks
;
1765 sh
->pd_idx
= stripe_to_pdidx(sh
->sector
, conf
, conf
->raid_disks
);
1766 compute_parity5(sh
, RECONSTRUCT_WRITE
);
1767 for (i
= conf
->raid_disks
; i
--;) {
1768 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1770 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1772 clear_bit(STRIPE_EXPANDING
, &sh
->state
);
1773 } else if (expanded
) {
1774 clear_bit(STRIPE_EXPAND_READY
, &sh
->state
);
1775 atomic_dec(&conf
->reshape_stripes
);
1776 wake_up(&conf
->wait_for_overlap
);
1777 md_done_sync(conf
->mddev
, STRIPE_SECTORS
, 1);
1780 if (expanding
&& locked
== 0) {
1781 /* We have read all the blocks in this stripe and now we need to
1782 * copy some of them into a target stripe for expand.
1784 clear_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
1785 for (i
=0; i
< sh
->disks
; i
++)
1786 if (i
!= sh
->pd_idx
) {
1787 int dd_idx
, pd_idx
, j
;
1788 struct stripe_head
*sh2
;
1790 sector_t bn
= compute_blocknr(sh
, i
);
1791 sector_t s
= raid5_compute_sector(bn
, conf
->raid_disks
,
1793 &dd_idx
, &pd_idx
, conf
);
1794 sh2
= get_active_stripe(conf
, s
, conf
->raid_disks
, pd_idx
, 1);
1796 /* so far only the early blocks of this stripe
1797 * have been requested. When later blocks
1798 * get requested, we will try again
1801 if(!test_bit(STRIPE_EXPANDING
, &sh2
->state
) ||
1802 test_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
)) {
1803 /* must have already done this block */
1804 release_stripe(sh2
);
1807 memcpy(page_address(sh2
->dev
[dd_idx
].page
),
1808 page_address(sh
->dev
[i
].page
),
1810 set_bit(R5_Expanded
, &sh2
->dev
[dd_idx
].flags
);
1811 set_bit(R5_UPTODATE
, &sh2
->dev
[dd_idx
].flags
);
1812 for (j
=0; j
<conf
->raid_disks
; j
++)
1813 if (j
!= sh2
->pd_idx
&&
1814 !test_bit(R5_Expanded
, &sh2
->dev
[j
].flags
))
1816 if (j
== conf
->raid_disks
) {
1817 set_bit(STRIPE_EXPAND_READY
, &sh2
->state
);
1818 set_bit(STRIPE_HANDLE
, &sh2
->state
);
1820 release_stripe(sh2
);
1824 spin_unlock(&sh
->lock
);
1826 while ((bi
=return_bi
)) {
1827 int bytes
= bi
->bi_size
;
1829 return_bi
= bi
->bi_next
;
1832 bi
->bi_end_io(bi
, bytes
, 0);
1834 for (i
=disks
; i
-- ;) {
1838 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
1840 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
1845 bi
= &sh
->dev
[i
].req
;
1849 bi
->bi_end_io
= raid5_end_write_request
;
1851 bi
->bi_end_io
= raid5_end_read_request
;
1854 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1855 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
1858 atomic_inc(&rdev
->nr_pending
);
1862 if (syncing
|| expanding
|| expanded
)
1863 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
1865 bi
->bi_bdev
= rdev
->bdev
;
1866 PRINTK("for %llu schedule op %ld on disc %d\n",
1867 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
1868 atomic_inc(&sh
->count
);
1869 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
1870 bi
->bi_flags
= 1 << BIO_UPTODATE
;
1872 bi
->bi_max_vecs
= 1;
1874 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
1875 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1876 bi
->bi_io_vec
[0].bv_offset
= 0;
1877 bi
->bi_size
= STRIPE_SIZE
;
1880 test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1881 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1882 generic_make_request(bi
);
1885 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1886 PRINTK("skip op %ld on disc %d for sector %llu\n",
1887 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
1888 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1889 set_bit(STRIPE_HANDLE
, &sh
->state
);
1894 static void handle_stripe6(struct stripe_head
*sh
, struct page
*tmp_page
)
1896 raid6_conf_t
*conf
= sh
->raid_conf
;
1897 int disks
= conf
->raid_disks
;
1898 struct bio
*return_bi
= NULL
;
1902 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
1903 int non_overwrite
= 0;
1904 int failed_num
[2] = {0, 0};
1905 struct r5dev
*dev
, *pdev
, *qdev
;
1906 int pd_idx
= sh
->pd_idx
;
1907 int qd_idx
= raid6_next_disk(pd_idx
, disks
);
1908 int p_failed
, q_failed
;
1910 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1911 (unsigned long long)sh
->sector
, sh
->state
, atomic_read(&sh
->count
),
1914 spin_lock(&sh
->lock
);
1915 clear_bit(STRIPE_HANDLE
, &sh
->state
);
1916 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1918 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
1919 /* Now to look around and see what can be done */
1922 for (i
=disks
; i
--; ) {
1925 clear_bit(R5_Insync
, &dev
->flags
);
1927 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1928 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
1929 /* maybe we can reply to a read */
1930 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
1931 struct bio
*rbi
, *rbi2
;
1932 PRINTK("Return read for disc %d\n", i
);
1933 spin_lock_irq(&conf
->device_lock
);
1936 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1937 wake_up(&conf
->wait_for_overlap
);
1938 spin_unlock_irq(&conf
->device_lock
);
1939 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1940 copy_data(0, rbi
, dev
->page
, dev
->sector
);
1941 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1942 spin_lock_irq(&conf
->device_lock
);
1943 if (--rbi
->bi_phys_segments
== 0) {
1944 rbi
->bi_next
= return_bi
;
1947 spin_unlock_irq(&conf
->device_lock
);
1952 /* now count some things */
1953 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
1954 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
1957 if (dev
->toread
) to_read
++;
1960 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1963 if (dev
->written
) written
++;
1964 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1965 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1966 /* The ReadError flag will just be confusing now */
1967 clear_bit(R5_ReadError
, &dev
->flags
);
1968 clear_bit(R5_ReWrite
, &dev
->flags
);
1970 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1971 || test_bit(R5_ReadError
, &dev
->flags
)) {
1973 failed_num
[failed
] = i
;
1976 set_bit(R5_Insync
, &dev
->flags
);
1979 PRINTK("locked=%d uptodate=%d to_read=%d"
1980 " to_write=%d failed=%d failed_num=%d,%d\n",
1981 locked
, uptodate
, to_read
, to_write
, failed
,
1982 failed_num
[0], failed_num
[1]);
1983 /* check if the array has lost >2 devices and, if so, some requests might
1986 if (failed
> 2 && to_read
+to_write
+written
) {
1987 for (i
=disks
; i
--; ) {
1990 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1993 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1994 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1995 /* multiple read failures in one stripe */
1996 md_error(conf
->mddev
, rdev
);
2000 spin_lock_irq(&conf
->device_lock
);
2001 /* fail all writes first */
2002 bi
= sh
->dev
[i
].towrite
;
2003 sh
->dev
[i
].towrite
= NULL
;
2004 if (bi
) { to_write
--; bitmap_end
= 1; }
2006 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2007 wake_up(&conf
->wait_for_overlap
);
2009 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
2010 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2011 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2012 if (--bi
->bi_phys_segments
== 0) {
2013 md_write_end(conf
->mddev
);
2014 bi
->bi_next
= return_bi
;
2019 /* and fail all 'written' */
2020 bi
= sh
->dev
[i
].written
;
2021 sh
->dev
[i
].written
= NULL
;
2022 if (bi
) bitmap_end
= 1;
2023 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
2024 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2025 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2026 if (--bi
->bi_phys_segments
== 0) {
2027 md_write_end(conf
->mddev
);
2028 bi
->bi_next
= return_bi
;
2034 /* fail any reads if this device is non-operational */
2035 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
2036 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
2037 bi
= sh
->dev
[i
].toread
;
2038 sh
->dev
[i
].toread
= NULL
;
2039 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
2040 wake_up(&conf
->wait_for_overlap
);
2042 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
2043 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
2044 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2045 if (--bi
->bi_phys_segments
== 0) {
2046 bi
->bi_next
= return_bi
;
2052 spin_unlock_irq(&conf
->device_lock
);
2054 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2055 STRIPE_SECTORS
, 0, 0);
2058 if (failed
> 2 && syncing
) {
2059 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
2060 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2065 * might be able to return some write requests if the parity blocks
2066 * are safe, or on a failed drive
2068 pdev
= &sh
->dev
[pd_idx
];
2069 p_failed
= (failed
>= 1 && failed_num
[0] == pd_idx
)
2070 || (failed
>= 2 && failed_num
[1] == pd_idx
);
2071 qdev
= &sh
->dev
[qd_idx
];
2072 q_failed
= (failed
>= 1 && failed_num
[0] == qd_idx
)
2073 || (failed
>= 2 && failed_num
[1] == qd_idx
);
2076 ( p_failed
|| ((test_bit(R5_Insync
, &pdev
->flags
)
2077 && !test_bit(R5_LOCKED
, &pdev
->flags
)
2078 && test_bit(R5_UPTODATE
, &pdev
->flags
))) ) &&
2079 ( q_failed
|| ((test_bit(R5_Insync
, &qdev
->flags
)
2080 && !test_bit(R5_LOCKED
, &qdev
->flags
)
2081 && test_bit(R5_UPTODATE
, &qdev
->flags
))) ) ) {
2082 /* any written block on an uptodate or failed drive can be
2083 * returned. Note that if we 'wrote' to a failed drive,
2084 * it will be UPTODATE, but never LOCKED, so we don't need
2085 * to test 'failed' directly.
2087 for (i
=disks
; i
--; )
2088 if (sh
->dev
[i
].written
) {
2090 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
2091 test_bit(R5_UPTODATE
, &dev
->flags
) ) {
2092 /* We can return any write requests */
2094 struct bio
*wbi
, *wbi2
;
2095 PRINTK("Return write for stripe %llu disc %d\n",
2096 (unsigned long long)sh
->sector
, i
);
2097 spin_lock_irq(&conf
->device_lock
);
2099 dev
->written
= NULL
;
2100 while (wbi
&& wbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
2101 wbi2
= r5_next_bio(wbi
, dev
->sector
);
2102 if (--wbi
->bi_phys_segments
== 0) {
2103 md_write_end(conf
->mddev
);
2104 wbi
->bi_next
= return_bi
;
2109 if (dev
->towrite
== NULL
)
2111 spin_unlock_irq(&conf
->device_lock
);
2113 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
2115 !test_bit(STRIPE_DEGRADED
, &sh
->state
), 0);
2120 /* Now we might consider reading some blocks, either to check/generate
2121 * parity, or to satisfy requests
2122 * or to load a block that is being partially written.
2124 if (to_read
|| non_overwrite
|| (to_write
&& failed
) || (syncing
&& (uptodate
< disks
))) {
2125 for (i
=disks
; i
--;) {
2127 if (!test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2129 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
2131 (failed
>= 1 && (sh
->dev
[failed_num
[0]].toread
|| to_write
)) ||
2132 (failed
>= 2 && (sh
->dev
[failed_num
[1]].toread
|| to_write
))
2135 /* we would like to get this block, possibly
2136 * by computing it, but we might not be able to
2138 if (uptodate
== disks
-1) {
2139 PRINTK("Computing stripe %llu block %d\n",
2140 (unsigned long long)sh
->sector
, i
);
2141 compute_block_1(sh
, i
, 0);
2143 } else if ( uptodate
== disks
-2 && failed
>= 2 ) {
2144 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2146 for (other
=disks
; other
--;) {
2149 if ( !test_bit(R5_UPTODATE
, &sh
->dev
[other
].flags
) )
2153 PRINTK("Computing stripe %llu blocks %d,%d\n",
2154 (unsigned long long)sh
->sector
, i
, other
);
2155 compute_block_2(sh
, i
, other
);
2157 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
2158 set_bit(R5_LOCKED
, &dev
->flags
);
2159 set_bit(R5_Wantread
, &dev
->flags
);
2161 /* if I am just reading this block and we don't have
2162 a failed drive, or any pending writes then sidestep the cache */
2163 if (sh
->bh_read
[i
] && !sh
->bh_read
[i
]->b_reqnext
&&
2164 ! syncing
&& !failed
&& !to_write
) {
2165 sh
->bh_cache
[i
]->b_page
= sh
->bh_read
[i
]->b_page
;
2166 sh
->bh_cache
[i
]->b_data
= sh
->bh_read
[i
]->b_data
;
2170 PRINTK("Reading block %d (sync=%d)\n",
2175 set_bit(STRIPE_HANDLE
, &sh
->state
);
2178 /* now to consider writing and what else, if anything should be read */
2180 int rcw
=0, must_compute
=0;
2181 for (i
=disks
; i
--;) {
2183 /* Would I have to read this buffer for reconstruct_write */
2184 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
2185 && i
!= pd_idx
&& i
!= qd_idx
2186 && (!test_bit(R5_LOCKED
, &dev
->flags
)
2188 || sh
->bh_page
[i
] != bh
->b_page
2191 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
2192 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
2194 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i
, dev
->flags
);
2199 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2200 (unsigned long long)sh
->sector
, rcw
, must_compute
);
2201 set_bit(STRIPE_HANDLE
, &sh
->state
);
2204 /* want reconstruct write, but need to get some data */
2205 for (i
=disks
; i
--;) {
2207 if (!test_bit(R5_OVERWRITE
, &dev
->flags
)
2208 && !(failed
== 0 && (i
== pd_idx
|| i
== qd_idx
))
2209 && !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
2210 test_bit(R5_Insync
, &dev
->flags
)) {
2211 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
2213 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2214 (unsigned long long)sh
->sector
, i
);
2215 set_bit(R5_LOCKED
, &dev
->flags
);
2216 set_bit(R5_Wantread
, &dev
->flags
);
2219 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2220 (unsigned long long)sh
->sector
, i
);
2221 set_bit(STRIPE_DELAYED
, &sh
->state
);
2222 set_bit(STRIPE_HANDLE
, &sh
->state
);
2226 /* now if nothing is locked, and if we have enough data, we can start a write request */
2227 if (locked
== 0 && rcw
== 0 &&
2228 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
2229 if ( must_compute
> 0 ) {
2230 /* We have failed blocks and need to compute them */
2233 case 1: compute_block_1(sh
, failed_num
[0], 0); break;
2234 case 2: compute_block_2(sh
, failed_num
[0], failed_num
[1]); break;
2235 default: BUG(); /* This request should have been failed? */
2239 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh
->sector
);
2240 compute_parity6(sh
, RECONSTRUCT_WRITE
);
2241 /* now every locked buffer is ready to be written */
2243 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
2244 PRINTK("Writing stripe %llu block %d\n",
2245 (unsigned long long)sh
->sector
, i
);
2247 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
2249 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2250 set_bit(STRIPE_INSYNC
, &sh
->state
);
2252 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
2253 atomic_dec(&conf
->preread_active_stripes
);
2254 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
2255 md_wakeup_thread(conf
->mddev
->thread
);
2260 /* maybe we need to check and possibly fix the parity for this stripe
2261 * Any reads will already have been scheduled, so we just see if enough data
2264 if (syncing
&& locked
== 0 && !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
2265 int update_p
= 0, update_q
= 0;
2268 set_bit(STRIPE_HANDLE
, &sh
->state
);
2271 BUG_ON(uptodate
< disks
);
2272 /* Want to check and possibly repair P and Q.
2273 * However there could be one 'failed' device, in which
2274 * case we can only check one of them, possibly using the
2275 * other to generate missing data
2278 /* If !tmp_page, we cannot do the calculations,
2279 * but as we have set STRIPE_HANDLE, we will soon be called
2280 * by stripe_handle with a tmp_page - just wait until then.
2283 if (failed
== q_failed
) {
2284 /* The only possible failed device holds 'Q', so it makes
2285 * sense to check P (If anything else were failed, we would
2286 * have used P to recreate it).
2288 compute_block_1(sh
, pd_idx
, 1);
2289 if (!page_is_zero(sh
->dev
[pd_idx
].page
)) {
2290 compute_block_1(sh
,pd_idx
,0);
2294 if (!q_failed
&& failed
< 2) {
2295 /* q is not failed, and we didn't use it to generate
2296 * anything, so it makes sense to check it
2298 memcpy(page_address(tmp_page
),
2299 page_address(sh
->dev
[qd_idx
].page
),
2301 compute_parity6(sh
, UPDATE_PARITY
);
2302 if (memcmp(page_address(tmp_page
),
2303 page_address(sh
->dev
[qd_idx
].page
),
2305 clear_bit(STRIPE_INSYNC
, &sh
->state
);
2309 if (update_p
|| update_q
) {
2310 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
2311 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
2312 /* don't try to repair!! */
2313 update_p
= update_q
= 0;
2316 /* now write out any block on a failed drive,
2317 * or P or Q if they need it
2321 dev
= &sh
->dev
[failed_num
[1]];
2323 set_bit(R5_LOCKED
, &dev
->flags
);
2324 set_bit(R5_Wantwrite
, &dev
->flags
);
2327 dev
= &sh
->dev
[failed_num
[0]];
2329 set_bit(R5_LOCKED
, &dev
->flags
);
2330 set_bit(R5_Wantwrite
, &dev
->flags
);
2334 dev
= &sh
->dev
[pd_idx
];
2336 set_bit(R5_LOCKED
, &dev
->flags
);
2337 set_bit(R5_Wantwrite
, &dev
->flags
);
2340 dev
= &sh
->dev
[qd_idx
];
2342 set_bit(R5_LOCKED
, &dev
->flags
);
2343 set_bit(R5_Wantwrite
, &dev
->flags
);
2345 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
2347 set_bit(STRIPE_INSYNC
, &sh
->state
);
2351 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
2352 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
2353 clear_bit(STRIPE_SYNCING
, &sh
->state
);
2356 /* If the failed drives are just a ReadError, then we might need
2357 * to progress the repair/check process
2359 if (failed
<= 2 && ! conf
->mddev
->ro
)
2360 for (i
=0; i
<failed
;i
++) {
2361 dev
= &sh
->dev
[failed_num
[i
]];
2362 if (test_bit(R5_ReadError
, &dev
->flags
)
2363 && !test_bit(R5_LOCKED
, &dev
->flags
)
2364 && test_bit(R5_UPTODATE
, &dev
->flags
)
2366 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
2367 set_bit(R5_Wantwrite
, &dev
->flags
);
2368 set_bit(R5_ReWrite
, &dev
->flags
);
2369 set_bit(R5_LOCKED
, &dev
->flags
);
2371 /* let's read it back */
2372 set_bit(R5_Wantread
, &dev
->flags
);
2373 set_bit(R5_LOCKED
, &dev
->flags
);
2377 spin_unlock(&sh
->lock
);
2379 while ((bi
=return_bi
)) {
2380 int bytes
= bi
->bi_size
;
2382 return_bi
= bi
->bi_next
;
2385 bi
->bi_end_io(bi
, bytes
, 0);
2387 for (i
=disks
; i
-- ;) {
2391 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
2393 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
2398 bi
= &sh
->dev
[i
].req
;
2402 bi
->bi_end_io
= raid5_end_write_request
;
2404 bi
->bi_end_io
= raid5_end_read_request
;
2407 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2408 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
2411 atomic_inc(&rdev
->nr_pending
);
2416 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
2418 bi
->bi_bdev
= rdev
->bdev
;
2419 PRINTK("for %llu schedule op %ld on disc %d\n",
2420 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
2421 atomic_inc(&sh
->count
);
2422 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
2423 bi
->bi_flags
= 1 << BIO_UPTODATE
;
2425 bi
->bi_max_vecs
= 1;
2427 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
2428 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
2429 bi
->bi_io_vec
[0].bv_offset
= 0;
2430 bi
->bi_size
= STRIPE_SIZE
;
2433 test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
2434 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
2435 generic_make_request(bi
);
2438 set_bit(STRIPE_DEGRADED
, &sh
->state
);
2439 PRINTK("skip op %ld on disc %d for sector %llu\n",
2440 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
2441 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
2442 set_bit(STRIPE_HANDLE
, &sh
->state
);
2447 static void handle_stripe(struct stripe_head
*sh
, struct page
*tmp_page
)
2449 if (sh
->raid_conf
->level
== 6)
2450 handle_stripe6(sh
, tmp_page
);
2457 static void raid5_activate_delayed(raid5_conf_t
*conf
)
2459 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
2460 while (!list_empty(&conf
->delayed_list
)) {
2461 struct list_head
*l
= conf
->delayed_list
.next
;
2462 struct stripe_head
*sh
;
2463 sh
= list_entry(l
, struct stripe_head
, lru
);
2465 clear_bit(STRIPE_DELAYED
, &sh
->state
);
2466 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
2467 atomic_inc(&conf
->preread_active_stripes
);
2468 list_add_tail(&sh
->lru
, &conf
->handle_list
);
2473 static void activate_bit_delay(raid5_conf_t
*conf
)
2475 /* device_lock is held */
2476 struct list_head head
;
2477 list_add(&head
, &conf
->bitmap_list
);
2478 list_del_init(&conf
->bitmap_list
);
2479 while (!list_empty(&head
)) {
2480 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
2481 list_del_init(&sh
->lru
);
2482 atomic_inc(&sh
->count
);
2483 __release_stripe(conf
, sh
);
2487 static void unplug_slaves(mddev_t
*mddev
)
2489 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2493 for (i
=0; i
<mddev
->raid_disks
; i
++) {
2494 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2495 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
2496 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
2498 atomic_inc(&rdev
->nr_pending
);
2501 if (r_queue
->unplug_fn
)
2502 r_queue
->unplug_fn(r_queue
);
2504 rdev_dec_pending(rdev
, mddev
);
2511 static void raid5_unplug_device(request_queue_t
*q
)
2513 mddev_t
*mddev
= q
->queuedata
;
2514 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2515 unsigned long flags
;
2517 spin_lock_irqsave(&conf
->device_lock
, flags
);
2519 if (blk_remove_plug(q
)) {
2521 raid5_activate_delayed(conf
);
2523 md_wakeup_thread(mddev
->thread
);
2525 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
2527 unplug_slaves(mddev
);
2530 static int raid5_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
2531 sector_t
*error_sector
)
2533 mddev_t
*mddev
= q
->queuedata
;
2534 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2538 for (i
=0; i
<mddev
->raid_disks
&& ret
== 0; i
++) {
2539 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
2540 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
2541 struct block_device
*bdev
= rdev
->bdev
;
2542 request_queue_t
*r_queue
= bdev_get_queue(bdev
);
2544 if (!r_queue
->issue_flush_fn
)
2547 atomic_inc(&rdev
->nr_pending
);
2549 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
2551 rdev_dec_pending(rdev
, mddev
);
2560 static int make_request(request_queue_t
*q
, struct bio
* bi
)
2562 mddev_t
*mddev
= q
->queuedata
;
2563 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2564 unsigned int dd_idx
, pd_idx
;
2565 sector_t new_sector
;
2566 sector_t logical_sector
, last_sector
;
2567 struct stripe_head
*sh
;
2568 const int rw
= bio_data_dir(bi
);
2571 if (unlikely(bio_barrier(bi
))) {
2572 bio_endio(bi
, bi
->bi_size
, -EOPNOTSUPP
);
2576 md_write_start(mddev
, bi
);
2578 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
2579 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bi
));
2581 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
2582 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
2584 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
2586 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
2588 int disks
, data_disks
;
2591 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
2592 if (likely(conf
->expand_progress
== MaxSector
))
2593 disks
= conf
->raid_disks
;
2595 /* spinlock is needed as expand_progress may be
2596 * 64bit on a 32bit platform, and so it might be
2597 * possible to see a half-updated value
2598 * Ofcourse expand_progress could change after
2599 * the lock is dropped, so once we get a reference
2600 * to the stripe that we think it is, we will have
2603 spin_lock_irq(&conf
->device_lock
);
2604 disks
= conf
->raid_disks
;
2605 if (logical_sector
>= conf
->expand_progress
)
2606 disks
= conf
->previous_raid_disks
;
2608 if (logical_sector
>= conf
->expand_lo
) {
2609 spin_unlock_irq(&conf
->device_lock
);
2614 spin_unlock_irq(&conf
->device_lock
);
2616 data_disks
= disks
- conf
->max_degraded
;
2618 new_sector
= raid5_compute_sector(logical_sector
, disks
, data_disks
,
2619 &dd_idx
, &pd_idx
, conf
);
2620 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2621 (unsigned long long)new_sector
,
2622 (unsigned long long)logical_sector
);
2624 sh
= get_active_stripe(conf
, new_sector
, disks
, pd_idx
, (bi
->bi_rw
&RWA_MASK
));
2626 if (unlikely(conf
->expand_progress
!= MaxSector
)) {
2627 /* expansion might have moved on while waiting for a
2628 * stripe, so we must do the range check again.
2629 * Expansion could still move past after this
2630 * test, but as we are holding a reference to
2631 * 'sh', we know that if that happens,
2632 * STRIPE_EXPANDING will get set and the expansion
2633 * won't proceed until we finish with the stripe.
2636 spin_lock_irq(&conf
->device_lock
);
2637 if (logical_sector
< conf
->expand_progress
&&
2638 disks
== conf
->previous_raid_disks
)
2639 /* mismatch, need to try again */
2641 spin_unlock_irq(&conf
->device_lock
);
2647 /* FIXME what if we get a false positive because these
2648 * are being updated.
2650 if (logical_sector
>= mddev
->suspend_lo
&&
2651 logical_sector
< mddev
->suspend_hi
) {
2657 if (test_bit(STRIPE_EXPANDING
, &sh
->state
) ||
2658 !add_stripe_bio(sh
, bi
, dd_idx
, (bi
->bi_rw
&RW_MASK
))) {
2659 /* Stripe is busy expanding or
2660 * add failed due to overlap. Flush everything
2663 raid5_unplug_device(mddev
->queue
);
2668 finish_wait(&conf
->wait_for_overlap
, &w
);
2669 handle_stripe(sh
, NULL
);
2672 /* cannot get stripe for read-ahead, just give-up */
2673 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
2674 finish_wait(&conf
->wait_for_overlap
, &w
);
2679 spin_lock_irq(&conf
->device_lock
);
2680 remaining
= --bi
->bi_phys_segments
;
2681 spin_unlock_irq(&conf
->device_lock
);
2682 if (remaining
== 0) {
2683 int bytes
= bi
->bi_size
;
2686 md_write_end(mddev
);
2688 bi
->bi_end_io(bi
, bytes
, 0);
2693 static sector_t
reshape_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
)
2695 /* reshaping is quite different to recovery/resync so it is
2696 * handled quite separately ... here.
2698 * On each call to sync_request, we gather one chunk worth of
2699 * destination stripes and flag them as expanding.
2700 * Then we find all the source stripes and request reads.
2701 * As the reads complete, handle_stripe will copy the data
2702 * into the destination stripe and release that stripe.
2704 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
2705 struct stripe_head
*sh
;
2707 sector_t first_sector
, last_sector
;
2712 sector_t writepos
, safepos
, gap
;
2714 if (sector_nr
== 0 &&
2715 conf
->expand_progress
!= 0) {
2716 /* restarting in the middle, skip the initial sectors */
2717 sector_nr
= conf
->expand_progress
;
2718 sector_div(sector_nr
, conf
->raid_disks
-1);
2723 /* we update the metadata when there is more than 3Meg
2724 * in the block range (that is rather arbitrary, should
2725 * probably be time based) or when the data about to be
2726 * copied would over-write the source of the data at
2727 * the front of the range.
2728 * i.e. one new_stripe forward from expand_progress new_maps
2729 * to after where expand_lo old_maps to
2731 writepos
= conf
->expand_progress
+
2732 conf
->chunk_size
/512*(conf
->raid_disks
-1);
2733 sector_div(writepos
, conf
->raid_disks
-1);
2734 safepos
= conf
->expand_lo
;
2735 sector_div(safepos
, conf
->previous_raid_disks
-1);
2736 gap
= conf
->expand_progress
- conf
->expand_lo
;
2738 if (writepos
>= safepos
||
2739 gap
> (conf
->raid_disks
-1)*3000*2 /*3Meg*/) {
2740 /* Cannot proceed until we've updated the superblock... */
2741 wait_event(conf
->wait_for_overlap
,
2742 atomic_read(&conf
->reshape_stripes
)==0);
2743 mddev
->reshape_position
= conf
->expand_progress
;
2744 mddev
->sb_dirty
= 1;
2745 md_wakeup_thread(mddev
->thread
);
2746 wait_event(mddev
->sb_wait
, mddev
->sb_dirty
== 0 ||
2747 kthread_should_stop());
2748 spin_lock_irq(&conf
->device_lock
);
2749 conf
->expand_lo
= mddev
->reshape_position
;
2750 spin_unlock_irq(&conf
->device_lock
);
2751 wake_up(&conf
->wait_for_overlap
);
2754 for (i
=0; i
< conf
->chunk_size
/512; i
+= STRIPE_SECTORS
) {
2757 pd_idx
= stripe_to_pdidx(sector_nr
+i
, conf
, conf
->raid_disks
);
2758 sh
= get_active_stripe(conf
, sector_nr
+i
,
2759 conf
->raid_disks
, pd_idx
, 0);
2760 set_bit(STRIPE_EXPANDING
, &sh
->state
);
2761 atomic_inc(&conf
->reshape_stripes
);
2762 /* If any of this stripe is beyond the end of the old
2763 * array, then we need to zero those blocks
2765 for (j
=sh
->disks
; j
--;) {
2767 if (j
== sh
->pd_idx
)
2769 s
= compute_blocknr(sh
, j
);
2770 if (s
< (mddev
->array_size
<<1)) {
2774 memset(page_address(sh
->dev
[j
].page
), 0, STRIPE_SIZE
);
2775 set_bit(R5_Expanded
, &sh
->dev
[j
].flags
);
2776 set_bit(R5_UPTODATE
, &sh
->dev
[j
].flags
);
2779 set_bit(STRIPE_EXPAND_READY
, &sh
->state
);
2780 set_bit(STRIPE_HANDLE
, &sh
->state
);
2784 spin_lock_irq(&conf
->device_lock
);
2785 conf
->expand_progress
= (sector_nr
+ i
)*(conf
->raid_disks
-1);
2786 spin_unlock_irq(&conf
->device_lock
);
2787 /* Ok, those stripe are ready. We can start scheduling
2788 * reads on the source stripes.
2789 * The source stripes are determined by mapping the first and last
2790 * block on the destination stripes.
2792 raid_disks
= conf
->previous_raid_disks
;
2793 data_disks
= raid_disks
- 1;
2795 raid5_compute_sector(sector_nr
*(conf
->raid_disks
-1),
2796 raid_disks
, data_disks
,
2797 &dd_idx
, &pd_idx
, conf
);
2799 raid5_compute_sector((sector_nr
+conf
->chunk_size
/512)
2800 *(conf
->raid_disks
-1) -1,
2801 raid_disks
, data_disks
,
2802 &dd_idx
, &pd_idx
, conf
);
2803 if (last_sector
>= (mddev
->size
<<1))
2804 last_sector
= (mddev
->size
<<1)-1;
2805 while (first_sector
<= last_sector
) {
2806 pd_idx
= stripe_to_pdidx(first_sector
, conf
, conf
->previous_raid_disks
);
2807 sh
= get_active_stripe(conf
, first_sector
,
2808 conf
->previous_raid_disks
, pd_idx
, 0);
2809 set_bit(STRIPE_EXPAND_SOURCE
, &sh
->state
);
2810 set_bit(STRIPE_HANDLE
, &sh
->state
);
2812 first_sector
+= STRIPE_SECTORS
;
2814 return conf
->chunk_size
>>9;
2817 /* FIXME go_faster isn't used */
2818 static inline sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
2820 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
2821 struct stripe_head
*sh
;
2823 int raid_disks
= conf
->raid_disks
;
2824 sector_t max_sector
= mddev
->size
<< 1;
2826 int still_degraded
= 0;
2829 if (sector_nr
>= max_sector
) {
2830 /* just being told to finish up .. nothing much to do */
2831 unplug_slaves(mddev
);
2832 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
)) {
2837 if (mddev
->curr_resync
< max_sector
) /* aborted */
2838 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
2840 else /* completed sync */
2842 bitmap_close_sync(mddev
->bitmap
);
2847 if (test_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
))
2848 return reshape_request(mddev
, sector_nr
, skipped
);
2850 /* if there is too many failed drives and we are trying
2851 * to resync, then assert that we are finished, because there is
2852 * nothing we can do.
2854 if (mddev
->degraded
>= conf
->max_degraded
&&
2855 test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
2856 sector_t rv
= (mddev
->size
<< 1) - sector_nr
;
2860 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
2861 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
2862 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
2863 /* we can skip this block, and probably more */
2864 sync_blocks
/= STRIPE_SECTORS
;
2866 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
2869 pd_idx
= stripe_to_pdidx(sector_nr
, conf
, raid_disks
);
2870 sh
= get_active_stripe(conf
, sector_nr
, raid_disks
, pd_idx
, 1);
2872 sh
= get_active_stripe(conf
, sector_nr
, raid_disks
, pd_idx
, 0);
2873 /* make sure we don't swamp the stripe cache if someone else
2874 * is trying to get access
2876 schedule_timeout_uninterruptible(1);
2878 /* Need to check if array will still be degraded after recovery/resync
2879 * We don't need to check the 'failed' flag as when that gets set,
2882 for (i
=0; i
<mddev
->raid_disks
; i
++)
2883 if (conf
->disks
[i
].rdev
== NULL
)
2886 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, still_degraded
);
2888 spin_lock(&sh
->lock
);
2889 set_bit(STRIPE_SYNCING
, &sh
->state
);
2890 clear_bit(STRIPE_INSYNC
, &sh
->state
);
2891 spin_unlock(&sh
->lock
);
2893 handle_stripe(sh
, NULL
);
2896 return STRIPE_SECTORS
;
2900 * This is our raid5 kernel thread.
2902 * We scan the hash table for stripes which can be handled now.
2903 * During the scan, completed stripes are saved for us by the interrupt
2904 * handler, so that they will not have to wait for our next wakeup.
2906 static void raid5d (mddev_t
*mddev
)
2908 struct stripe_head
*sh
;
2909 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2912 PRINTK("+++ raid5d active\n");
2914 md_check_recovery(mddev
);
2917 spin_lock_irq(&conf
->device_lock
);
2919 struct list_head
*first
;
2921 if (conf
->seq_flush
- conf
->seq_write
> 0) {
2922 int seq
= conf
->seq_flush
;
2923 spin_unlock_irq(&conf
->device_lock
);
2924 bitmap_unplug(mddev
->bitmap
);
2925 spin_lock_irq(&conf
->device_lock
);
2926 conf
->seq_write
= seq
;
2927 activate_bit_delay(conf
);
2930 if (list_empty(&conf
->handle_list
) &&
2931 atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
&&
2932 !blk_queue_plugged(mddev
->queue
) &&
2933 !list_empty(&conf
->delayed_list
))
2934 raid5_activate_delayed(conf
);
2936 if (list_empty(&conf
->handle_list
))
2939 first
= conf
->handle_list
.next
;
2940 sh
= list_entry(first
, struct stripe_head
, lru
);
2942 list_del_init(first
);
2943 atomic_inc(&sh
->count
);
2944 BUG_ON(atomic_read(&sh
->count
)!= 1);
2945 spin_unlock_irq(&conf
->device_lock
);
2948 handle_stripe(sh
, conf
->spare_page
);
2951 spin_lock_irq(&conf
->device_lock
);
2953 PRINTK("%d stripes handled\n", handled
);
2955 spin_unlock_irq(&conf
->device_lock
);
2957 unplug_slaves(mddev
);
2959 PRINTK("--- raid5d inactive\n");
2963 raid5_show_stripe_cache_size(mddev_t
*mddev
, char *page
)
2965 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2967 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
2973 raid5_store_stripe_cache_size(mddev_t
*mddev
, const char *page
, size_t len
)
2975 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2978 if (len
>= PAGE_SIZE
)
2983 new = simple_strtoul(page
, &end
, 10);
2984 if (!*page
|| (*end
&& *end
!= '\n') )
2986 if (new <= 16 || new > 32768)
2988 while (new < conf
->max_nr_stripes
) {
2989 if (drop_one_stripe(conf
))
2990 conf
->max_nr_stripes
--;
2994 while (new > conf
->max_nr_stripes
) {
2995 if (grow_one_stripe(conf
))
2996 conf
->max_nr_stripes
++;
3002 static struct md_sysfs_entry
3003 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
3004 raid5_show_stripe_cache_size
,
3005 raid5_store_stripe_cache_size
);
3008 stripe_cache_active_show(mddev_t
*mddev
, char *page
)
3010 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3012 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
3017 static struct md_sysfs_entry
3018 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
3020 static struct attribute
*raid5_attrs
[] = {
3021 &raid5_stripecache_size
.attr
,
3022 &raid5_stripecache_active
.attr
,
3025 static struct attribute_group raid5_attrs_group
= {
3027 .attrs
= raid5_attrs
,
3030 static int run(mddev_t
*mddev
)
3033 int raid_disk
, memory
;
3035 struct disk_info
*disk
;
3036 struct list_head
*tmp
;
3038 if (mddev
->level
!= 5 && mddev
->level
!= 4 && mddev
->level
!= 6) {
3039 printk(KERN_ERR
"raid5: %s: raid level not set to 4/5/6 (%d)\n",
3040 mdname(mddev
), mddev
->level
);
3044 if (mddev
->reshape_position
!= MaxSector
) {
3045 /* Check that we can continue the reshape.
3046 * Currently only disks can change, it must
3047 * increase, and we must be past the point where
3048 * a stripe over-writes itself
3050 sector_t here_new
, here_old
;
3053 if (mddev
->new_level
!= mddev
->level
||
3054 mddev
->new_layout
!= mddev
->layout
||
3055 mddev
->new_chunk
!= mddev
->chunk_size
) {
3056 printk(KERN_ERR
"raid5: %s: unsupported reshape required - aborting.\n",
3060 if (mddev
->delta_disks
<= 0) {
3061 printk(KERN_ERR
"raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3065 old_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3066 /* reshape_position must be on a new-stripe boundary, and one
3067 * further up in new geometry must map after here in old geometry.
3069 here_new
= mddev
->reshape_position
;
3070 if (sector_div(here_new
, (mddev
->chunk_size
>>9)*(mddev
->raid_disks
-1))) {
3071 printk(KERN_ERR
"raid5: reshape_position not on a stripe boundary\n");
3074 /* here_new is the stripe we will write to */
3075 here_old
= mddev
->reshape_position
;
3076 sector_div(here_old
, (mddev
->chunk_size
>>9)*(old_disks
-1));
3077 /* here_old is the first stripe that we might need to read from */
3078 if (here_new
>= here_old
) {
3079 /* Reading from the same stripe as writing to - bad */
3080 printk(KERN_ERR
"raid5: reshape_position too early for auto-recovery - aborting.\n");
3083 printk(KERN_INFO
"raid5: reshape will continue\n");
3084 /* OK, we should be able to continue; */
3088 mddev
->private = kzalloc(sizeof (raid5_conf_t
), GFP_KERNEL
);
3089 if ((conf
= mddev
->private) == NULL
)
3091 if (mddev
->reshape_position
== MaxSector
) {
3092 conf
->previous_raid_disks
= conf
->raid_disks
= mddev
->raid_disks
;
3094 conf
->raid_disks
= mddev
->raid_disks
;
3095 conf
->previous_raid_disks
= mddev
->raid_disks
- mddev
->delta_disks
;
3098 conf
->disks
= kzalloc(conf
->raid_disks
* sizeof(struct disk_info
),
3103 conf
->mddev
= mddev
;
3105 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
3108 if (mddev
->level
== 6) {
3109 conf
->spare_page
= alloc_page(GFP_KERNEL
);
3110 if (!conf
->spare_page
)
3113 spin_lock_init(&conf
->device_lock
);
3114 init_waitqueue_head(&conf
->wait_for_stripe
);
3115 init_waitqueue_head(&conf
->wait_for_overlap
);
3116 INIT_LIST_HEAD(&conf
->handle_list
);
3117 INIT_LIST_HEAD(&conf
->delayed_list
);
3118 INIT_LIST_HEAD(&conf
->bitmap_list
);
3119 INIT_LIST_HEAD(&conf
->inactive_list
);
3120 atomic_set(&conf
->active_stripes
, 0);
3121 atomic_set(&conf
->preread_active_stripes
, 0);
3123 PRINTK("raid5: run(%s) called.\n", mdname(mddev
));
3125 ITERATE_RDEV(mddev
,rdev
,tmp
) {
3126 raid_disk
= rdev
->raid_disk
;
3127 if (raid_disk
>= conf
->raid_disks
3130 disk
= conf
->disks
+ raid_disk
;
3134 if (test_bit(In_sync
, &rdev
->flags
)) {
3135 char b
[BDEVNAME_SIZE
];
3136 printk(KERN_INFO
"raid5: device %s operational as raid"
3137 " disk %d\n", bdevname(rdev
->bdev
,b
),
3139 conf
->working_disks
++;
3144 * 0 for a fully functional array, 1 or 2 for a degraded array.
3146 mddev
->degraded
= conf
->failed_disks
= conf
->raid_disks
- conf
->working_disks
;
3147 conf
->mddev
= mddev
;
3148 conf
->chunk_size
= mddev
->chunk_size
;
3149 conf
->level
= mddev
->level
;
3150 if (conf
->level
== 6)
3151 conf
->max_degraded
= 2;
3153 conf
->max_degraded
= 1;
3154 conf
->algorithm
= mddev
->layout
;
3155 conf
->max_nr_stripes
= NR_STRIPES
;
3156 conf
->expand_progress
= mddev
->reshape_position
;
3158 /* device size must be a multiple of chunk size */
3159 mddev
->size
&= ~(mddev
->chunk_size
/1024 -1);
3160 mddev
->resync_max_sectors
= mddev
->size
<< 1;
3162 if (conf
->level
== 6 && conf
->raid_disks
< 4) {
3163 printk(KERN_ERR
"raid6: not enough configured devices for %s (%d, minimum 4)\n",
3164 mdname(mddev
), conf
->raid_disks
);
3167 if (!conf
->chunk_size
|| conf
->chunk_size
% 4) {
3168 printk(KERN_ERR
"raid5: invalid chunk size %d for %s\n",
3169 conf
->chunk_size
, mdname(mddev
));
3172 if (conf
->algorithm
> ALGORITHM_RIGHT_SYMMETRIC
) {
3174 "raid5: unsupported parity algorithm %d for %s\n",
3175 conf
->algorithm
, mdname(mddev
));
3178 if (mddev
->degraded
> conf
->max_degraded
) {
3179 printk(KERN_ERR
"raid5: not enough operational devices for %s"
3180 " (%d/%d failed)\n",
3181 mdname(mddev
), conf
->failed_disks
, conf
->raid_disks
);
3185 if (mddev
->degraded
> 0 &&
3186 mddev
->recovery_cp
!= MaxSector
) {
3187 if (mddev
->ok_start_degraded
)
3189 "raid5: starting dirty degraded array: %s"
3190 "- data corruption possible.\n",
3194 "raid5: cannot start dirty degraded array for %s\n",
3201 mddev
->thread
= md_register_thread(raid5d
, mddev
, "%s_raid5");
3202 if (!mddev
->thread
) {
3204 "raid5: couldn't allocate thread for %s\n",
3209 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
3210 conf
->raid_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
3211 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
3213 "raid5: couldn't allocate %dkB for buffers\n", memory
);
3214 shrink_stripes(conf
);
3215 md_unregister_thread(mddev
->thread
);
3218 printk(KERN_INFO
"raid5: allocated %dkB for %s\n",
3219 memory
, mdname(mddev
));
3221 if (mddev
->degraded
== 0)
3222 printk("raid5: raid level %d set %s active with %d out of %d"
3223 " devices, algorithm %d\n", conf
->level
, mdname(mddev
),
3224 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
3227 printk(KERN_ALERT
"raid5: raid level %d set %s active with %d"
3228 " out of %d devices, algorithm %d\n", conf
->level
,
3229 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
3230 mddev
->raid_disks
, conf
->algorithm
);
3232 print_raid5_conf(conf
);
3234 if (conf
->expand_progress
!= MaxSector
) {
3235 printk("...ok start reshape thread\n");
3236 conf
->expand_lo
= conf
->expand_progress
;
3237 atomic_set(&conf
->reshape_stripes
, 0);
3238 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3239 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3240 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3241 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3242 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
3246 /* read-ahead size must cover two whole stripes, which is
3247 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3250 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
3251 int stripe
= data_disks
*
3252 (mddev
->chunk_size
/ PAGE_SIZE
);
3253 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
3254 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
3257 /* Ok, everything is just fine now */
3258 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
);
3260 mddev
->queue
->unplug_fn
= raid5_unplug_device
;
3261 mddev
->queue
->issue_flush_fn
= raid5_issue_flush
;
3262 mddev
->array_size
= mddev
->size
* (conf
->previous_raid_disks
-
3263 conf
->max_degraded
);
3268 print_raid5_conf(conf
);
3269 safe_put_page(conf
->spare_page
);
3271 kfree(conf
->stripe_hashtbl
);
3274 mddev
->private = NULL
;
3275 printk(KERN_ALERT
"raid5: failed to run raid set %s\n", mdname(mddev
));
3281 static int stop(mddev_t
*mddev
)
3283 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
3285 md_unregister_thread(mddev
->thread
);
3286 mddev
->thread
= NULL
;
3287 shrink_stripes(conf
);
3288 kfree(conf
->stripe_hashtbl
);
3289 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
3290 sysfs_remove_group(&mddev
->kobj
, &raid5_attrs_group
);
3293 mddev
->private = NULL
;
3298 static void print_sh (struct seq_file
*seq
, struct stripe_head
*sh
)
3302 seq_printf(seq
, "sh %llu, pd_idx %d, state %ld.\n",
3303 (unsigned long long)sh
->sector
, sh
->pd_idx
, sh
->state
);
3304 seq_printf(seq
, "sh %llu, count %d.\n",
3305 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
));
3306 seq_printf(seq
, "sh %llu, ", (unsigned long long)sh
->sector
);
3307 for (i
= 0; i
< sh
->disks
; i
++) {
3308 seq_printf(seq
, "(cache%d: %p %ld) ",
3309 i
, sh
->dev
[i
].page
, sh
->dev
[i
].flags
);
3311 seq_printf(seq
, "\n");
3314 static void printall (struct seq_file
*seq
, raid5_conf_t
*conf
)
3316 struct stripe_head
*sh
;
3317 struct hlist_node
*hn
;
3320 spin_lock_irq(&conf
->device_lock
);
3321 for (i
= 0; i
< NR_HASH
; i
++) {
3322 hlist_for_each_entry(sh
, hn
, &conf
->stripe_hashtbl
[i
], hash
) {
3323 if (sh
->raid_conf
!= conf
)
3328 spin_unlock_irq(&conf
->device_lock
);
3332 static void status (struct seq_file
*seq
, mddev_t
*mddev
)
3334 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
3337 seq_printf (seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
, mddev
->chunk_size
>> 10, mddev
->layout
);
3338 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->working_disks
);
3339 for (i
= 0; i
< conf
->raid_disks
; i
++)
3340 seq_printf (seq
, "%s",
3341 conf
->disks
[i
].rdev
&&
3342 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
3343 seq_printf (seq
, "]");
3345 seq_printf (seq
, "\n");
3346 printall(seq
, conf
);
3350 static void print_raid5_conf (raid5_conf_t
*conf
)
3353 struct disk_info
*tmp
;
3355 printk("RAID5 conf printout:\n");
3357 printk("(conf==NULL)\n");
3360 printk(" --- rd:%d wd:%d fd:%d\n", conf
->raid_disks
,
3361 conf
->working_disks
, conf
->failed_disks
);
3363 for (i
= 0; i
< conf
->raid_disks
; i
++) {
3364 char b
[BDEVNAME_SIZE
];
3365 tmp
= conf
->disks
+ i
;
3367 printk(" disk %d, o:%d, dev:%s\n",
3368 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
3369 bdevname(tmp
->rdev
->bdev
,b
));
3373 static int raid5_spare_active(mddev_t
*mddev
)
3376 raid5_conf_t
*conf
= mddev
->private;
3377 struct disk_info
*tmp
;
3379 for (i
= 0; i
< conf
->raid_disks
; i
++) {
3380 tmp
= conf
->disks
+ i
;
3382 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
3383 && !test_bit(In_sync
, &tmp
->rdev
->flags
)) {
3385 conf
->failed_disks
--;
3386 conf
->working_disks
++;
3387 set_bit(In_sync
, &tmp
->rdev
->flags
);
3390 print_raid5_conf(conf
);
3394 static int raid5_remove_disk(mddev_t
*mddev
, int number
)
3396 raid5_conf_t
*conf
= mddev
->private;
3399 struct disk_info
*p
= conf
->disks
+ number
;
3401 print_raid5_conf(conf
);
3404 if (test_bit(In_sync
, &rdev
->flags
) ||
3405 atomic_read(&rdev
->nr_pending
)) {
3411 if (atomic_read(&rdev
->nr_pending
)) {
3412 /* lost the race, try later */
3419 print_raid5_conf(conf
);
3423 static int raid5_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
3425 raid5_conf_t
*conf
= mddev
->private;
3428 struct disk_info
*p
;
3430 if (mddev
->degraded
> conf
->max_degraded
)
3431 /* no point adding a device */
3435 * find the disk ... but prefer rdev->saved_raid_disk
3438 if (rdev
->saved_raid_disk
>= 0 &&
3439 conf
->disks
[rdev
->saved_raid_disk
].rdev
== NULL
)
3440 disk
= rdev
->saved_raid_disk
;
3443 for ( ; disk
< conf
->raid_disks
; disk
++)
3444 if ((p
=conf
->disks
+ disk
)->rdev
== NULL
) {
3445 clear_bit(In_sync
, &rdev
->flags
);
3446 rdev
->raid_disk
= disk
;
3448 if (rdev
->saved_raid_disk
!= disk
)
3450 rcu_assign_pointer(p
->rdev
, rdev
);
3453 print_raid5_conf(conf
);
3457 static int raid5_resize(mddev_t
*mddev
, sector_t sectors
)
3459 /* no resync is happening, and there is enough space
3460 * on all devices, so we can resize.
3461 * We need to make sure resync covers any new space.
3462 * If the array is shrinking we should possibly wait until
3463 * any io in the removed space completes, but it hardly seems
3466 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3468 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
3469 mddev
->array_size
= (sectors
* (mddev
->raid_disks
-conf
->max_degraded
))>>1;
3470 set_capacity(mddev
->gendisk
, mddev
->array_size
<< 1);
3472 if (sectors
/2 > mddev
->size
&& mddev
->recovery_cp
== MaxSector
) {
3473 mddev
->recovery_cp
= mddev
->size
<< 1;
3474 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
3476 mddev
->size
= sectors
/2;
3477 mddev
->resync_max_sectors
= sectors
;
3481 #ifdef CONFIG_MD_RAID5_RESHAPE
3482 static int raid5_check_reshape(mddev_t
*mddev
)
3484 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3487 if (mddev
->delta_disks
< 0 ||
3488 mddev
->new_level
!= mddev
->level
)
3489 return -EINVAL
; /* Cannot shrink array or change level yet */
3490 if (mddev
->delta_disks
== 0)
3491 return 0; /* nothing to do */
3493 /* Can only proceed if there are plenty of stripe_heads.
3494 * We need a minimum of one full stripe,, and for sensible progress
3495 * it is best to have about 4 times that.
3496 * If we require 4 times, then the default 256 4K stripe_heads will
3497 * allow for chunk sizes up to 256K, which is probably OK.
3498 * If the chunk size is greater, user-space should request more
3499 * stripe_heads first.
3501 if ((mddev
->chunk_size
/ STRIPE_SIZE
) * 4 > conf
->max_nr_stripes
||
3502 (mddev
->new_chunk
/ STRIPE_SIZE
) * 4 > conf
->max_nr_stripes
) {
3503 printk(KERN_WARNING
"raid5: reshape: not enough stripes. Needed %lu\n",
3504 (mddev
->chunk_size
/ STRIPE_SIZE
)*4);
3508 err
= resize_stripes(conf
, conf
->raid_disks
+ mddev
->delta_disks
);
3512 /* looks like we might be able to manage this */
3516 static int raid5_start_reshape(mddev_t
*mddev
)
3518 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3520 struct list_head
*rtmp
;
3522 int added_devices
= 0;
3524 if (mddev
->degraded
||
3525 test_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
))
3528 ITERATE_RDEV(mddev
, rdev
, rtmp
)
3529 if (rdev
->raid_disk
< 0 &&
3530 !test_bit(Faulty
, &rdev
->flags
))
3533 if (spares
< mddev
->delta_disks
-1)
3534 /* Not enough devices even to make a degraded array
3539 atomic_set(&conf
->reshape_stripes
, 0);
3540 spin_lock_irq(&conf
->device_lock
);
3541 conf
->previous_raid_disks
= conf
->raid_disks
;
3542 conf
->raid_disks
+= mddev
->delta_disks
;
3543 conf
->expand_progress
= 0;
3544 conf
->expand_lo
= 0;
3545 spin_unlock_irq(&conf
->device_lock
);
3547 /* Add some new drives, as many as will fit.
3548 * We know there are enough to make the newly sized array work.
3550 ITERATE_RDEV(mddev
, rdev
, rtmp
)
3551 if (rdev
->raid_disk
< 0 &&
3552 !test_bit(Faulty
, &rdev
->flags
)) {
3553 if (raid5_add_disk(mddev
, rdev
)) {
3555 set_bit(In_sync
, &rdev
->flags
);
3556 conf
->working_disks
++;
3558 rdev
->recovery_offset
= 0;
3559 sprintf(nm
, "rd%d", rdev
->raid_disk
);
3560 sysfs_create_link(&mddev
->kobj
, &rdev
->kobj
, nm
);
3565 mddev
->degraded
= (conf
->raid_disks
- conf
->previous_raid_disks
) - added_devices
;
3566 mddev
->raid_disks
= conf
->raid_disks
;
3567 mddev
->reshape_position
= 0;
3568 mddev
->sb_dirty
= 1;
3570 clear_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
);
3571 clear_bit(MD_RECOVERY_CHECK
, &mddev
->recovery
);
3572 set_bit(MD_RECOVERY_RESHAPE
, &mddev
->recovery
);
3573 set_bit(MD_RECOVERY_RUNNING
, &mddev
->recovery
);
3574 mddev
->sync_thread
= md_register_thread(md_do_sync
, mddev
,
3576 if (!mddev
->sync_thread
) {
3577 mddev
->recovery
= 0;
3578 spin_lock_irq(&conf
->device_lock
);
3579 mddev
->raid_disks
= conf
->raid_disks
= conf
->previous_raid_disks
;
3580 conf
->expand_progress
= MaxSector
;
3581 spin_unlock_irq(&conf
->device_lock
);
3584 md_wakeup_thread(mddev
->sync_thread
);
3585 md_new_event(mddev
);
3590 static void end_reshape(raid5_conf_t
*conf
)
3592 struct block_device
*bdev
;
3594 if (!test_bit(MD_RECOVERY_INTR
, &conf
->mddev
->recovery
)) {
3595 conf
->mddev
->array_size
= conf
->mddev
->size
* (conf
->raid_disks
-1);
3596 set_capacity(conf
->mddev
->gendisk
, conf
->mddev
->array_size
<< 1);
3597 conf
->mddev
->changed
= 1;
3599 bdev
= bdget_disk(conf
->mddev
->gendisk
, 0);
3601 mutex_lock(&bdev
->bd_inode
->i_mutex
);
3602 i_size_write(bdev
->bd_inode
, conf
->mddev
->array_size
<< 10);
3603 mutex_unlock(&bdev
->bd_inode
->i_mutex
);
3606 spin_lock_irq(&conf
->device_lock
);
3607 conf
->expand_progress
= MaxSector
;
3608 spin_unlock_irq(&conf
->device_lock
);
3609 conf
->mddev
->reshape_position
= MaxSector
;
3611 /* read-ahead size must cover two whole stripes, which is
3612 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3615 int data_disks
= conf
->previous_raid_disks
- conf
->max_degraded
;
3616 int stripe
= data_disks
*
3617 (conf
->mddev
->chunk_size
/ PAGE_SIZE
);
3618 if (conf
->mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
3619 conf
->mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
3624 static void raid5_quiesce(mddev_t
*mddev
, int state
)
3626 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
3629 case 2: /* resume for a suspend */
3630 wake_up(&conf
->wait_for_overlap
);
3633 case 1: /* stop all writes */
3634 spin_lock_irq(&conf
->device_lock
);
3636 wait_event_lock_irq(conf
->wait_for_stripe
,
3637 atomic_read(&conf
->active_stripes
) == 0,
3638 conf
->device_lock
, /* nothing */);
3639 spin_unlock_irq(&conf
->device_lock
);
3642 case 0: /* re-enable writes */
3643 spin_lock_irq(&conf
->device_lock
);
3645 wake_up(&conf
->wait_for_stripe
);
3646 wake_up(&conf
->wait_for_overlap
);
3647 spin_unlock_irq(&conf
->device_lock
);
3652 static struct mdk_personality raid6_personality
=
3656 .owner
= THIS_MODULE
,
3657 .make_request
= make_request
,
3661 .error_handler
= error
,
3662 .hot_add_disk
= raid5_add_disk
,
3663 .hot_remove_disk
= raid5_remove_disk
,
3664 .spare_active
= raid5_spare_active
,
3665 .sync_request
= sync_request
,
3666 .resize
= raid5_resize
,
3667 .quiesce
= raid5_quiesce
,
3669 static struct mdk_personality raid5_personality
=
3673 .owner
= THIS_MODULE
,
3674 .make_request
= make_request
,
3678 .error_handler
= error
,
3679 .hot_add_disk
= raid5_add_disk
,
3680 .hot_remove_disk
= raid5_remove_disk
,
3681 .spare_active
= raid5_spare_active
,
3682 .sync_request
= sync_request
,
3683 .resize
= raid5_resize
,
3684 #ifdef CONFIG_MD_RAID5_RESHAPE
3685 .check_reshape
= raid5_check_reshape
,
3686 .start_reshape
= raid5_start_reshape
,
3688 .quiesce
= raid5_quiesce
,
3691 static struct mdk_personality raid4_personality
=
3695 .owner
= THIS_MODULE
,
3696 .make_request
= make_request
,
3700 .error_handler
= error
,
3701 .hot_add_disk
= raid5_add_disk
,
3702 .hot_remove_disk
= raid5_remove_disk
,
3703 .spare_active
= raid5_spare_active
,
3704 .sync_request
= sync_request
,
3705 .resize
= raid5_resize
,
3706 .quiesce
= raid5_quiesce
,
3709 static int __init
raid5_init(void)
3713 e
= raid6_select_algo();
3716 register_md_personality(&raid6_personality
);
3717 register_md_personality(&raid5_personality
);
3718 register_md_personality(&raid4_personality
);
3722 static void raid5_exit(void)
3724 unregister_md_personality(&raid6_personality
);
3725 unregister_md_personality(&raid5_personality
);
3726 unregister_md_personality(&raid4_personality
);
3729 module_init(raid5_init
);
3730 module_exit(raid5_exit
);
3731 MODULE_LICENSE("GPL");
3732 MODULE_ALIAS("md-personality-4"); /* RAID5 */
3733 MODULE_ALIAS("md-raid5");
3734 MODULE_ALIAS("md-raid4");
3735 MODULE_ALIAS("md-level-5");
3736 MODULE_ALIAS("md-level-4");
3737 MODULE_ALIAS("md-personality-8"); /* RAID6 */
3738 MODULE_ALIAS("md-raid6");
3739 MODULE_ALIAS("md-level-6");
3741 /* This used to be two separate modules, they were: */
3742 MODULE_ALIAS("raid5");
3743 MODULE_ALIAS("raid6");