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
6 * RAID-5 management functions.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
13 * You should have received a copy of the GNU General Public License
14 * (for example /usr/src/linux/COPYING); if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/raid/raid5.h>
23 #include <linux/highmem.h>
24 #include <linux/bitops.h>
25 #include <asm/atomic.h>
27 #include <linux/raid/bitmap.h>
33 #define NR_STRIPES 256
34 #define STRIPE_SIZE PAGE_SIZE
35 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
36 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
37 #define IO_THRESHOLD 1
38 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
39 #define HASH_MASK (NR_HASH - 1)
41 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
43 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
44 * order without overlap. There may be several bio's per stripe+device, and
45 * a bio could span several devices.
46 * When walking this list for a particular stripe+device, we must never proceed
47 * beyond a bio that extends past this device, as the next bio might no longer
49 * This macro is used to determine the 'next' bio in the list, given the sector
50 * of the current stripe+device
52 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
54 * The following can be used to debug the driver
57 #define RAID5_PARANOIA 1
58 #if RAID5_PARANOIA && defined(CONFIG_SMP)
59 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
61 # define CHECK_DEVLOCK()
64 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
70 static void print_raid5_conf (raid5_conf_t
*conf
);
72 static void __release_stripe(raid5_conf_t
*conf
, struct stripe_head
*sh
)
74 if (atomic_dec_and_test(&sh
->count
)) {
75 if (!list_empty(&sh
->lru
))
77 if (atomic_read(&conf
->active_stripes
)==0)
79 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
80 if (test_bit(STRIPE_DELAYED
, &sh
->state
))
81 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
82 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
83 conf
->seq_write
== sh
->bm_seq
)
84 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
86 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
87 list_add_tail(&sh
->lru
, &conf
->handle_list
);
89 md_wakeup_thread(conf
->mddev
->thread
);
91 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
92 atomic_dec(&conf
->preread_active_stripes
);
93 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
94 md_wakeup_thread(conf
->mddev
->thread
);
96 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
97 atomic_dec(&conf
->active_stripes
);
98 if (!conf
->inactive_blocked
||
99 atomic_read(&conf
->active_stripes
) < (conf
->max_nr_stripes
*3/4))
100 wake_up(&conf
->wait_for_stripe
);
104 static void release_stripe(struct stripe_head
*sh
)
106 raid5_conf_t
*conf
= sh
->raid_conf
;
109 spin_lock_irqsave(&conf
->device_lock
, flags
);
110 __release_stripe(conf
, sh
);
111 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
114 static inline void remove_hash(struct stripe_head
*sh
)
116 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
118 hlist_del_init(&sh
->hash
);
121 static void insert_hash(raid5_conf_t
*conf
, struct stripe_head
*sh
)
123 struct hlist_head
*hp
= stripe_hash(conf
, sh
->sector
);
125 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
128 hlist_add_head(&sh
->hash
, hp
);
132 /* find an idle stripe, make sure it is unhashed, and return it. */
133 static struct stripe_head
*get_free_stripe(raid5_conf_t
*conf
)
135 struct stripe_head
*sh
= NULL
;
136 struct list_head
*first
;
139 if (list_empty(&conf
->inactive_list
))
141 first
= conf
->inactive_list
.next
;
142 sh
= list_entry(first
, struct stripe_head
, lru
);
143 list_del_init(first
);
145 atomic_inc(&conf
->active_stripes
);
150 static void shrink_buffers(struct stripe_head
*sh
, int num
)
155 for (i
=0; i
<num
; i
++) {
159 sh
->dev
[i
].page
= NULL
;
164 static int grow_buffers(struct stripe_head
*sh
, int num
)
168 for (i
=0; i
<num
; i
++) {
171 if (!(page
= alloc_page(GFP_KERNEL
))) {
174 sh
->dev
[i
].page
= page
;
179 static void raid5_build_block (struct stripe_head
*sh
, int i
);
181 static void init_stripe(struct stripe_head
*sh
, sector_t sector
, int pd_idx
)
183 raid5_conf_t
*conf
= sh
->raid_conf
;
184 int disks
= conf
->raid_disks
, i
;
186 if (atomic_read(&sh
->count
) != 0)
188 if (test_bit(STRIPE_HANDLE
, &sh
->state
))
192 PRINTK("init_stripe called, stripe %llu\n",
193 (unsigned long long)sh
->sector
);
201 for (i
=disks
; i
--; ) {
202 struct r5dev
*dev
= &sh
->dev
[i
];
204 if (dev
->toread
|| dev
->towrite
|| dev
->written
||
205 test_bit(R5_LOCKED
, &dev
->flags
)) {
206 printk("sector=%llx i=%d %p %p %p %d\n",
207 (unsigned long long)sh
->sector
, i
, dev
->toread
,
208 dev
->towrite
, dev
->written
,
209 test_bit(R5_LOCKED
, &dev
->flags
));
213 raid5_build_block(sh
, i
);
215 insert_hash(conf
, sh
);
218 static struct stripe_head
*__find_stripe(raid5_conf_t
*conf
, sector_t sector
)
220 struct stripe_head
*sh
;
221 struct hlist_node
*hn
;
224 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector
);
225 hlist_for_each_entry(sh
, hn
, stripe_hash(conf
, sector
), hash
)
226 if (sh
->sector
== sector
)
228 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector
);
232 static void unplug_slaves(mddev_t
*mddev
);
233 static void raid5_unplug_device(request_queue_t
*q
);
235 static struct stripe_head
*get_active_stripe(raid5_conf_t
*conf
, sector_t sector
,
236 int pd_idx
, int noblock
)
238 struct stripe_head
*sh
;
240 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector
);
242 spin_lock_irq(&conf
->device_lock
);
245 wait_event_lock_irq(conf
->wait_for_stripe
,
247 conf
->device_lock
, /* nothing */);
248 sh
= __find_stripe(conf
, sector
);
250 if (!conf
->inactive_blocked
)
251 sh
= get_free_stripe(conf
);
252 if (noblock
&& sh
== NULL
)
255 conf
->inactive_blocked
= 1;
256 wait_event_lock_irq(conf
->wait_for_stripe
,
257 !list_empty(&conf
->inactive_list
) &&
258 (atomic_read(&conf
->active_stripes
)
259 < (conf
->max_nr_stripes
*3/4)
260 || !conf
->inactive_blocked
),
262 unplug_slaves(conf
->mddev
);
264 conf
->inactive_blocked
= 0;
266 init_stripe(sh
, sector
, pd_idx
);
268 if (atomic_read(&sh
->count
)) {
269 if (!list_empty(&sh
->lru
))
272 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
273 atomic_inc(&conf
->active_stripes
);
274 if (list_empty(&sh
->lru
))
276 list_del_init(&sh
->lru
);
279 } while (sh
== NULL
);
282 atomic_inc(&sh
->count
);
284 spin_unlock_irq(&conf
->device_lock
);
288 static int grow_one_stripe(raid5_conf_t
*conf
)
290 struct stripe_head
*sh
;
291 sh
= kmem_cache_alloc(conf
->slab_cache
, GFP_KERNEL
);
294 memset(sh
, 0, sizeof(*sh
) + (conf
->raid_disks
-1)*sizeof(struct r5dev
));
295 sh
->raid_conf
= conf
;
296 spin_lock_init(&sh
->lock
);
298 if (grow_buffers(sh
, conf
->raid_disks
)) {
299 shrink_buffers(sh
, conf
->raid_disks
);
300 kmem_cache_free(conf
->slab_cache
, sh
);
303 /* we just created an active stripe so... */
304 atomic_set(&sh
->count
, 1);
305 atomic_inc(&conf
->active_stripes
);
306 INIT_LIST_HEAD(&sh
->lru
);
311 static int grow_stripes(raid5_conf_t
*conf
, int num
)
314 int devs
= conf
->raid_disks
;
316 sprintf(conf
->cache_name
[0], "raid5/%s", mdname(conf
->mddev
));
317 sprintf(conf
->cache_name
[1], "raid5/%s-alt", mdname(conf
->mddev
));
318 conf
->active_name
= 0;
319 sc
= kmem_cache_create(conf
->cache_name
[conf
->active_name
],
320 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
324 conf
->slab_cache
= sc
;
325 conf
->pool_size
= devs
;
327 if (!grow_one_stripe(conf
))
332 static int resize_stripes(raid5_conf_t
*conf
, int newsize
)
334 /* Make all the stripes able to hold 'newsize' devices.
335 * New slots in each stripe get 'page' set to a new page.
337 * This happens in stages:
338 * 1/ create a new kmem_cache and allocate the required number of
340 * 2/ gather all the old stripe_heads and tranfer the pages across
341 * to the new stripe_heads. This will have the side effect of
342 * freezing the array as once all stripe_heads have been collected,
343 * no IO will be possible. Old stripe heads are freed once their
344 * pages have been transferred over, and the old kmem_cache is
345 * freed when all stripes are done.
346 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
347 * we simple return a failre status - no need to clean anything up.
348 * 4/ allocate new pages for the new slots in the new stripe_heads.
349 * If this fails, we don't bother trying the shrink the
350 * stripe_heads down again, we just leave them as they are.
351 * As each stripe_head is processed the new one is released into
354 * Once step2 is started, we cannot afford to wait for a write,
355 * so we use GFP_NOIO allocations.
357 struct stripe_head
*osh
, *nsh
;
358 LIST_HEAD(newstripes
);
359 struct disk_info
*ndisks
;
364 if (newsize
<= conf
->pool_size
)
365 return 0; /* never bother to shrink */
368 sc
= kmem_cache_create(conf
->cache_name
[1-conf
->active_name
],
369 sizeof(struct stripe_head
)+(newsize
-1)*sizeof(struct r5dev
),
374 for (i
= conf
->max_nr_stripes
; i
; i
--) {
375 nsh
= kmem_cache_alloc(sc
, GFP_KERNEL
);
379 memset(nsh
, 0, sizeof(*nsh
) + (newsize
-1)*sizeof(struct r5dev
));
381 nsh
->raid_conf
= conf
;
382 spin_lock_init(&nsh
->lock
);
384 list_add(&nsh
->lru
, &newstripes
);
387 /* didn't get enough, give up */
388 while (!list_empty(&newstripes
)) {
389 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
391 kmem_cache_free(sc
, nsh
);
393 kmem_cache_destroy(sc
);
396 /* Step 2 - Must use GFP_NOIO now.
397 * OK, we have enough stripes, start collecting inactive
398 * stripes and copying them over
400 list_for_each_entry(nsh
, &newstripes
, lru
) {
401 spin_lock_irq(&conf
->device_lock
);
402 wait_event_lock_irq(conf
->wait_for_stripe
,
403 !list_empty(&conf
->inactive_list
),
405 unplug_slaves(conf
->mddev
);
407 osh
= get_free_stripe(conf
);
408 spin_unlock_irq(&conf
->device_lock
);
409 atomic_set(&nsh
->count
, 1);
410 for(i
=0; i
<conf
->pool_size
; i
++)
411 nsh
->dev
[i
].page
= osh
->dev
[i
].page
;
412 for( ; i
<newsize
; i
++)
413 nsh
->dev
[i
].page
= NULL
;
414 kmem_cache_free(conf
->slab_cache
, osh
);
416 kmem_cache_destroy(conf
->slab_cache
);
419 * At this point, we are holding all the stripes so the array
420 * is completely stalled, so now is a good time to resize
423 ndisks
= kzalloc(newsize
* sizeof(struct disk_info
), GFP_NOIO
);
425 for (i
=0; i
<conf
->raid_disks
; i
++)
426 ndisks
[i
] = conf
->disks
[i
];
428 conf
->disks
= ndisks
;
432 /* Step 4, return new stripes to service */
433 while(!list_empty(&newstripes
)) {
434 nsh
= list_entry(newstripes
.next
, struct stripe_head
, lru
);
435 list_del_init(&nsh
->lru
);
436 for (i
=conf
->raid_disks
; i
< newsize
; i
++)
437 if (nsh
->dev
[i
].page
== NULL
) {
438 struct page
*p
= alloc_page(GFP_NOIO
);
439 nsh
->dev
[i
].page
= p
;
445 /* critical section pass, GFP_NOIO no longer needed */
447 conf
->slab_cache
= sc
;
448 conf
->active_name
= 1-conf
->active_name
;
449 conf
->pool_size
= newsize
;
454 static int drop_one_stripe(raid5_conf_t
*conf
)
456 struct stripe_head
*sh
;
458 spin_lock_irq(&conf
->device_lock
);
459 sh
= get_free_stripe(conf
);
460 spin_unlock_irq(&conf
->device_lock
);
463 if (atomic_read(&sh
->count
))
465 shrink_buffers(sh
, conf
->pool_size
);
466 kmem_cache_free(conf
->slab_cache
, sh
);
467 atomic_dec(&conf
->active_stripes
);
471 static void shrink_stripes(raid5_conf_t
*conf
)
473 while (drop_one_stripe(conf
))
476 if (conf
->slab_cache
)
477 kmem_cache_destroy(conf
->slab_cache
);
478 conf
->slab_cache
= NULL
;
481 static int raid5_end_read_request(struct bio
* bi
, unsigned int bytes_done
,
484 struct stripe_head
*sh
= bi
->bi_private
;
485 raid5_conf_t
*conf
= sh
->raid_conf
;
486 int disks
= conf
->raid_disks
, i
;
487 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
492 for (i
=0 ; i
<disks
; i
++)
493 if (bi
== &sh
->dev
[i
].req
)
496 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
497 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
508 spin_lock_irqsave(&conf
->device_lock
, flags
);
509 /* we can return a buffer if we bypassed the cache or
510 * if the top buffer is not in highmem. If there are
511 * multiple buffers, leave the extra work to
514 buffer
= sh
->bh_read
[i
];
516 (!PageHighMem(buffer
->b_page
)
517 || buffer
->b_page
== bh
->b_page
)
519 sh
->bh_read
[i
] = buffer
->b_reqnext
;
520 buffer
->b_reqnext
= NULL
;
523 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
524 if (sh
->bh_page
[i
]==bh
->b_page
)
525 set_buffer_uptodate(bh
);
527 if (buffer
->b_page
!= bh
->b_page
)
528 memcpy(buffer
->b_data
, bh
->b_data
, bh
->b_size
);
529 buffer
->b_end_io(buffer
, 1);
532 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
534 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
535 printk(KERN_INFO
"raid5: read error corrected!!\n");
536 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
537 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
539 if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
))
540 atomic_set(&conf
->disks
[i
].rdev
->read_errors
, 0);
543 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
544 atomic_inc(&conf
->disks
[i
].rdev
->read_errors
);
545 if (conf
->mddev
->degraded
)
546 printk(KERN_WARNING
"raid5: read error not correctable.\n");
547 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
549 printk(KERN_WARNING
"raid5: read error NOT corrected!!\n");
550 else if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
)
551 > conf
->max_nr_stripes
)
553 "raid5: Too many read errors, failing device.\n");
557 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
559 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
560 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
561 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
564 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
566 /* must restore b_page before unlocking buffer... */
567 if (sh
->bh_page
[i
] != bh
->b_page
) {
568 bh
->b_page
= sh
->bh_page
[i
];
569 bh
->b_data
= page_address(bh
->b_page
);
570 clear_buffer_uptodate(bh
);
573 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
574 set_bit(STRIPE_HANDLE
, &sh
->state
);
579 static int raid5_end_write_request (struct bio
*bi
, unsigned int bytes_done
,
582 struct stripe_head
*sh
= bi
->bi_private
;
583 raid5_conf_t
*conf
= sh
->raid_conf
;
584 int disks
= conf
->raid_disks
, i
;
586 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
591 for (i
=0 ; i
<disks
; i
++)
592 if (bi
== &sh
->dev
[i
].req
)
595 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
596 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
603 spin_lock_irqsave(&conf
->device_lock
, flags
);
605 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
607 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
609 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
610 set_bit(STRIPE_HANDLE
, &sh
->state
);
611 __release_stripe(conf
, sh
);
612 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
617 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
);
619 static void raid5_build_block (struct stripe_head
*sh
, int i
)
621 struct r5dev
*dev
= &sh
->dev
[i
];
624 dev
->req
.bi_io_vec
= &dev
->vec
;
626 dev
->req
.bi_max_vecs
++;
627 dev
->vec
.bv_page
= dev
->page
;
628 dev
->vec
.bv_len
= STRIPE_SIZE
;
629 dev
->vec
.bv_offset
= 0;
631 dev
->req
.bi_sector
= sh
->sector
;
632 dev
->req
.bi_private
= sh
;
636 dev
->sector
= compute_blocknr(sh
, i
);
639 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
641 char b
[BDEVNAME_SIZE
];
642 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
643 PRINTK("raid5: error called\n");
645 if (!test_bit(Faulty
, &rdev
->flags
)) {
647 if (test_bit(In_sync
, &rdev
->flags
)) {
648 conf
->working_disks
--;
650 conf
->failed_disks
++;
651 clear_bit(In_sync
, &rdev
->flags
);
653 * if recovery was running, make sure it aborts.
655 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
657 set_bit(Faulty
, &rdev
->flags
);
659 "raid5: Disk failure on %s, disabling device."
660 " Operation continuing on %d devices\n",
661 bdevname(rdev
->bdev
,b
), conf
->working_disks
);
666 * Input: a 'big' sector number,
667 * Output: index of the data and parity disk, and the sector # in them.
669 static sector_t
raid5_compute_sector(sector_t r_sector
, unsigned int raid_disks
,
670 unsigned int data_disks
, unsigned int * dd_idx
,
671 unsigned int * pd_idx
, raid5_conf_t
*conf
)
674 unsigned long chunk_number
;
675 unsigned int chunk_offset
;
677 int sectors_per_chunk
= conf
->chunk_size
>> 9;
679 /* First compute the information on this sector */
682 * Compute the chunk number and the sector offset inside the chunk
684 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
685 chunk_number
= r_sector
;
686 BUG_ON(r_sector
!= chunk_number
);
689 * Compute the stripe number
691 stripe
= chunk_number
/ data_disks
;
694 * Compute the data disk and parity disk indexes inside the stripe
696 *dd_idx
= chunk_number
% data_disks
;
699 * Select the parity disk based on the user selected algorithm.
701 if (conf
->level
== 4)
702 *pd_idx
= data_disks
;
703 else switch (conf
->algorithm
) {
704 case ALGORITHM_LEFT_ASYMMETRIC
:
705 *pd_idx
= data_disks
- stripe
% raid_disks
;
706 if (*dd_idx
>= *pd_idx
)
709 case ALGORITHM_RIGHT_ASYMMETRIC
:
710 *pd_idx
= stripe
% raid_disks
;
711 if (*dd_idx
>= *pd_idx
)
714 case ALGORITHM_LEFT_SYMMETRIC
:
715 *pd_idx
= data_disks
- stripe
% raid_disks
;
716 *dd_idx
= (*pd_idx
+ 1 + *dd_idx
) % raid_disks
;
718 case ALGORITHM_RIGHT_SYMMETRIC
:
719 *pd_idx
= stripe
% raid_disks
;
720 *dd_idx
= (*pd_idx
+ 1 + *dd_idx
) % raid_disks
;
723 printk(KERN_ERR
"raid5: unsupported algorithm %d\n",
728 * Finally, compute the new sector number
730 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
735 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
)
737 raid5_conf_t
*conf
= sh
->raid_conf
;
738 int raid_disks
= conf
->raid_disks
, data_disks
= raid_disks
- 1;
739 sector_t new_sector
= sh
->sector
, check
;
740 int sectors_per_chunk
= conf
->chunk_size
>> 9;
743 int chunk_number
, dummy1
, dummy2
, dd_idx
= i
;
746 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
748 BUG_ON(new_sector
!= stripe
);
751 switch (conf
->algorithm
) {
752 case ALGORITHM_LEFT_ASYMMETRIC
:
753 case ALGORITHM_RIGHT_ASYMMETRIC
:
757 case ALGORITHM_LEFT_SYMMETRIC
:
758 case ALGORITHM_RIGHT_SYMMETRIC
:
761 i
-= (sh
->pd_idx
+ 1);
764 printk(KERN_ERR
"raid5: unsupported algorithm %d\n",
768 chunk_number
= stripe
* data_disks
+ i
;
769 r_sector
= (sector_t
)chunk_number
* sectors_per_chunk
+ chunk_offset
;
771 check
= raid5_compute_sector (r_sector
, raid_disks
, data_disks
, &dummy1
, &dummy2
, conf
);
772 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| dummy2
!= sh
->pd_idx
) {
773 printk(KERN_ERR
"compute_blocknr: map not correct\n");
782 * Copy data between a page in the stripe cache, and a bio.
783 * There are no alignment or size guarantees between the page or the
784 * bio except that there is some overlap.
785 * All iovecs in the bio must be considered.
787 static void copy_data(int frombio
, struct bio
*bio
,
791 char *pa
= page_address(page
);
796 if (bio
->bi_sector
>= sector
)
797 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
799 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
800 bio_for_each_segment(bvl
, bio
, i
) {
801 int len
= bio_iovec_idx(bio
,i
)->bv_len
;
805 if (page_offset
< 0) {
806 b_offset
= -page_offset
;
807 page_offset
+= b_offset
;
811 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
812 clen
= STRIPE_SIZE
- page_offset
;
816 char *ba
= __bio_kmap_atomic(bio
, i
, KM_USER0
);
818 memcpy(pa
+page_offset
, ba
+b_offset
, clen
);
820 memcpy(ba
+b_offset
, pa
+page_offset
, clen
);
821 __bio_kunmap_atomic(ba
, KM_USER0
);
823 if (clen
< len
) /* hit end of page */
829 #define check_xor() do { \
830 if (count == MAX_XOR_BLOCKS) { \
831 xor_block(count, STRIPE_SIZE, ptr); \
837 static void compute_block(struct stripe_head
*sh
, int dd_idx
)
839 raid5_conf_t
*conf
= sh
->raid_conf
;
840 int i
, count
, disks
= conf
->raid_disks
;
841 void *ptr
[MAX_XOR_BLOCKS
], *p
;
843 PRINTK("compute_block, stripe %llu, idx %d\n",
844 (unsigned long long)sh
->sector
, dd_idx
);
846 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
847 memset(ptr
[0], 0, STRIPE_SIZE
);
849 for (i
= disks
; i
--; ) {
852 p
= page_address(sh
->dev
[i
].page
);
853 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
856 printk(KERN_ERR
"compute_block() %d, stripe %llu, %d"
857 " not present\n", dd_idx
,
858 (unsigned long long)sh
->sector
, i
);
863 xor_block(count
, STRIPE_SIZE
, ptr
);
864 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
867 static void compute_parity(struct stripe_head
*sh
, int method
)
869 raid5_conf_t
*conf
= sh
->raid_conf
;
870 int i
, pd_idx
= sh
->pd_idx
, disks
= conf
->raid_disks
, count
;
871 void *ptr
[MAX_XOR_BLOCKS
];
874 PRINTK("compute_parity, stripe %llu, method %d\n",
875 (unsigned long long)sh
->sector
, method
);
878 ptr
[0] = page_address(sh
->dev
[pd_idx
].page
);
880 case READ_MODIFY_WRITE
:
881 if (!test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
))
883 for (i
=disks
; i
-- ;) {
886 if (sh
->dev
[i
].towrite
&&
887 test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
)) {
888 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
889 chosen
= sh
->dev
[i
].towrite
;
890 sh
->dev
[i
].towrite
= NULL
;
892 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
893 wake_up(&conf
->wait_for_overlap
);
895 if (sh
->dev
[i
].written
) BUG();
896 sh
->dev
[i
].written
= chosen
;
901 case RECONSTRUCT_WRITE
:
902 memset(ptr
[0], 0, STRIPE_SIZE
);
903 for (i
= disks
; i
-- ;)
904 if (i
!=pd_idx
&& sh
->dev
[i
].towrite
) {
905 chosen
= sh
->dev
[i
].towrite
;
906 sh
->dev
[i
].towrite
= NULL
;
908 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
909 wake_up(&conf
->wait_for_overlap
);
911 if (sh
->dev
[i
].written
) BUG();
912 sh
->dev
[i
].written
= chosen
;
919 xor_block(count
, STRIPE_SIZE
, ptr
);
923 for (i
= disks
; i
--;)
924 if (sh
->dev
[i
].written
) {
925 sector_t sector
= sh
->dev
[i
].sector
;
926 struct bio
*wbi
= sh
->dev
[i
].written
;
927 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
928 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
929 wbi
= r5_next_bio(wbi
, sector
);
932 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
933 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
937 case RECONSTRUCT_WRITE
:
941 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
945 case READ_MODIFY_WRITE
:
946 for (i
= disks
; i
--;)
947 if (sh
->dev
[i
].written
) {
948 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
953 xor_block(count
, STRIPE_SIZE
, ptr
);
955 if (method
!= CHECK_PARITY
) {
956 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
957 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
959 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
963 * Each stripe/dev can have one or more bion attached.
964 * toread/towrite point to the first in a chain.
965 * The bi_next chain must be in order.
967 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
970 raid5_conf_t
*conf
= sh
->raid_conf
;
973 PRINTK("adding bh b#%llu to stripe s#%llu\n",
974 (unsigned long long)bi
->bi_sector
,
975 (unsigned long long)sh
->sector
);
978 spin_lock(&sh
->lock
);
979 spin_lock_irq(&conf
->device_lock
);
981 bip
= &sh
->dev
[dd_idx
].towrite
;
982 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
985 bip
= &sh
->dev
[dd_idx
].toread
;
986 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
987 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
989 bip
= & (*bip
)->bi_next
;
991 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
994 if (*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
)
999 bi
->bi_phys_segments
++;
1000 spin_unlock_irq(&conf
->device_lock
);
1001 spin_unlock(&sh
->lock
);
1003 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1004 (unsigned long long)bi
->bi_sector
,
1005 (unsigned long long)sh
->sector
, dd_idx
);
1007 if (conf
->mddev
->bitmap
&& firstwrite
) {
1008 sh
->bm_seq
= conf
->seq_write
;
1009 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
1011 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
1015 /* check if page is covered */
1016 sector_t sector
= sh
->dev
[dd_idx
].sector
;
1017 for (bi
=sh
->dev
[dd_idx
].towrite
;
1018 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
1019 bi
&& bi
->bi_sector
<= sector
;
1020 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
1021 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
1022 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1024 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
1025 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
1030 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
1031 spin_unlock_irq(&conf
->device_lock
);
1032 spin_unlock(&sh
->lock
);
1038 * handle_stripe - do things to a stripe.
1040 * We lock the stripe and then examine the state of various bits
1041 * to see what needs to be done.
1043 * return some read request which now have data
1044 * return some write requests which are safely on disc
1045 * schedule a read on some buffers
1046 * schedule a write of some buffers
1047 * return confirmation of parity correctness
1049 * Parity calculations are done inside the stripe lock
1050 * buffers are taken off read_list or write_list, and bh_cache buffers
1051 * get BH_Lock set before the stripe lock is released.
1055 static void handle_stripe(struct stripe_head
*sh
)
1057 raid5_conf_t
*conf
= sh
->raid_conf
;
1058 int disks
= conf
->raid_disks
;
1059 struct bio
*return_bi
= NULL
;
1063 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
1064 int non_overwrite
= 0;
1068 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1069 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
),
1072 spin_lock(&sh
->lock
);
1073 clear_bit(STRIPE_HANDLE
, &sh
->state
);
1074 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1076 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
1077 /* Now to look around and see what can be done */
1080 for (i
=disks
; i
--; ) {
1083 clear_bit(R5_Insync
, &dev
->flags
);
1085 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1086 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
1087 /* maybe we can reply to a read */
1088 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
1089 struct bio
*rbi
, *rbi2
;
1090 PRINTK("Return read for disc %d\n", i
);
1091 spin_lock_irq(&conf
->device_lock
);
1094 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
1095 wake_up(&conf
->wait_for_overlap
);
1096 spin_unlock_irq(&conf
->device_lock
);
1097 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1098 copy_data(0, rbi
, dev
->page
, dev
->sector
);
1099 rbi2
= r5_next_bio(rbi
, dev
->sector
);
1100 spin_lock_irq(&conf
->device_lock
);
1101 if (--rbi
->bi_phys_segments
== 0) {
1102 rbi
->bi_next
= return_bi
;
1105 spin_unlock_irq(&conf
->device_lock
);
1110 /* now count some things */
1111 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
1112 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
1115 if (dev
->toread
) to_read
++;
1118 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1121 if (dev
->written
) written
++;
1122 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1123 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1124 /* The ReadError flag will just be confusing now */
1125 clear_bit(R5_ReadError
, &dev
->flags
);
1126 clear_bit(R5_ReWrite
, &dev
->flags
);
1128 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1129 || test_bit(R5_ReadError
, &dev
->flags
)) {
1133 set_bit(R5_Insync
, &dev
->flags
);
1136 PRINTK("locked=%d uptodate=%d to_read=%d"
1137 " to_write=%d failed=%d failed_num=%d\n",
1138 locked
, uptodate
, to_read
, to_write
, failed
, failed_num
);
1139 /* check if the array has lost two devices and, if so, some requests might
1142 if (failed
> 1 && to_read
+to_write
+written
) {
1143 for (i
=disks
; i
--; ) {
1146 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1149 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1150 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1151 /* multiple read failures in one stripe */
1152 md_error(conf
->mddev
, rdev
);
1156 spin_lock_irq(&conf
->device_lock
);
1157 /* fail all writes first */
1158 bi
= sh
->dev
[i
].towrite
;
1159 sh
->dev
[i
].towrite
= NULL
;
1160 if (bi
) { to_write
--; bitmap_end
= 1; }
1162 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1163 wake_up(&conf
->wait_for_overlap
);
1165 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1166 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1167 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1168 if (--bi
->bi_phys_segments
== 0) {
1169 md_write_end(conf
->mddev
);
1170 bi
->bi_next
= return_bi
;
1175 /* and fail all 'written' */
1176 bi
= sh
->dev
[i
].written
;
1177 sh
->dev
[i
].written
= NULL
;
1178 if (bi
) bitmap_end
= 1;
1179 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
1180 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1181 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1182 if (--bi
->bi_phys_segments
== 0) {
1183 md_write_end(conf
->mddev
);
1184 bi
->bi_next
= return_bi
;
1190 /* fail any reads if this device is non-operational */
1191 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
1192 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1193 bi
= sh
->dev
[i
].toread
;
1194 sh
->dev
[i
].toread
= NULL
;
1195 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1196 wake_up(&conf
->wait_for_overlap
);
1198 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1199 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1200 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1201 if (--bi
->bi_phys_segments
== 0) {
1202 bi
->bi_next
= return_bi
;
1208 spin_unlock_irq(&conf
->device_lock
);
1210 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1211 STRIPE_SECTORS
, 0, 0);
1214 if (failed
> 1 && syncing
) {
1215 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
1216 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1220 /* might be able to return some write requests if the parity block
1221 * is safe, or on a failed drive
1223 dev
= &sh
->dev
[sh
->pd_idx
];
1225 ( (test_bit(R5_Insync
, &dev
->flags
) && !test_bit(R5_LOCKED
, &dev
->flags
) &&
1226 test_bit(R5_UPTODATE
, &dev
->flags
))
1227 || (failed
== 1 && failed_num
== sh
->pd_idx
))
1229 /* any written block on an uptodate or failed drive can be returned.
1230 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1231 * never LOCKED, so we don't need to test 'failed' directly.
1233 for (i
=disks
; i
--; )
1234 if (sh
->dev
[i
].written
) {
1236 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
1237 test_bit(R5_UPTODATE
, &dev
->flags
) ) {
1238 /* We can return any write requests */
1239 struct bio
*wbi
, *wbi2
;
1241 PRINTK("Return write for disc %d\n", i
);
1242 spin_lock_irq(&conf
->device_lock
);
1244 dev
->written
= NULL
;
1245 while (wbi
&& wbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1246 wbi2
= r5_next_bio(wbi
, dev
->sector
);
1247 if (--wbi
->bi_phys_segments
== 0) {
1248 md_write_end(conf
->mddev
);
1249 wbi
->bi_next
= return_bi
;
1254 if (dev
->towrite
== NULL
)
1256 spin_unlock_irq(&conf
->device_lock
);
1258 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1260 !test_bit(STRIPE_DEGRADED
, &sh
->state
), 0);
1265 /* Now we might consider reading some blocks, either to check/generate
1266 * parity, or to satisfy requests
1267 * or to load a block that is being partially written.
1269 if (to_read
|| non_overwrite
|| (syncing
&& (uptodate
< disks
))) {
1270 for (i
=disks
; i
--;) {
1272 if (!test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1274 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
1276 (failed
&& (sh
->dev
[failed_num
].toread
||
1277 (sh
->dev
[failed_num
].towrite
&& !test_bit(R5_OVERWRITE
, &sh
->dev
[failed_num
].flags
))))
1280 /* we would like to get this block, possibly
1281 * by computing it, but we might not be able to
1283 if (uptodate
== disks
-1) {
1284 PRINTK("Computing block %d\n", i
);
1285 compute_block(sh
, i
);
1287 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
1288 set_bit(R5_LOCKED
, &dev
->flags
);
1289 set_bit(R5_Wantread
, &dev
->flags
);
1291 /* if I am just reading this block and we don't have
1292 a failed drive, or any pending writes then sidestep the cache */
1293 if (sh
->bh_read
[i
] && !sh
->bh_read
[i
]->b_reqnext
&&
1294 ! syncing
&& !failed
&& !to_write
) {
1295 sh
->bh_cache
[i
]->b_page
= sh
->bh_read
[i
]->b_page
;
1296 sh
->bh_cache
[i
]->b_data
= sh
->bh_read
[i
]->b_data
;
1300 PRINTK("Reading block %d (sync=%d)\n",
1305 set_bit(STRIPE_HANDLE
, &sh
->state
);
1308 /* now to consider writing and what else, if anything should be read */
1311 for (i
=disks
; i
--;) {
1312 /* would I have to read this buffer for read_modify_write */
1314 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
1315 (!test_bit(R5_LOCKED
, &dev
->flags
)
1317 || sh
->bh_page
[i
]!=bh
->b_page
1320 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1321 if (test_bit(R5_Insync
, &dev
->flags
)
1322 /* && !(!mddev->insync && i == sh->pd_idx) */
1325 else rmw
+= 2*disks
; /* cannot read it */
1327 /* Would I have to read this buffer for reconstruct_write */
1328 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
1329 (!test_bit(R5_LOCKED
, &dev
->flags
)
1331 || sh
->bh_page
[i
] != bh
->b_page
1334 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1335 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
1336 else rcw
+= 2*disks
;
1339 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1340 (unsigned long long)sh
->sector
, rmw
, rcw
);
1341 set_bit(STRIPE_HANDLE
, &sh
->state
);
1342 if (rmw
< rcw
&& rmw
> 0)
1343 /* prefer read-modify-write, but need to get some data */
1344 for (i
=disks
; i
--;) {
1346 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
1347 !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1348 test_bit(R5_Insync
, &dev
->flags
)) {
1349 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1351 PRINTK("Read_old block %d for r-m-w\n", i
);
1352 set_bit(R5_LOCKED
, &dev
->flags
);
1353 set_bit(R5_Wantread
, &dev
->flags
);
1356 set_bit(STRIPE_DELAYED
, &sh
->state
);
1357 set_bit(STRIPE_HANDLE
, &sh
->state
);
1361 if (rcw
<= rmw
&& rcw
> 0)
1362 /* want reconstruct write, but need to get some data */
1363 for (i
=disks
; i
--;) {
1365 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
1366 !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1367 test_bit(R5_Insync
, &dev
->flags
)) {
1368 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1370 PRINTK("Read_old block %d for Reconstruct\n", i
);
1371 set_bit(R5_LOCKED
, &dev
->flags
);
1372 set_bit(R5_Wantread
, &dev
->flags
);
1375 set_bit(STRIPE_DELAYED
, &sh
->state
);
1376 set_bit(STRIPE_HANDLE
, &sh
->state
);
1380 /* now if nothing is locked, and if we have enough data, we can start a write request */
1381 if (locked
== 0 && (rcw
== 0 ||rmw
== 0) &&
1382 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
1383 PRINTK("Computing parity...\n");
1384 compute_parity(sh
, rcw
==0 ? RECONSTRUCT_WRITE
: READ_MODIFY_WRITE
);
1385 /* now every locked buffer is ready to be written */
1387 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
1388 PRINTK("Writing block %d\n", i
);
1390 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1391 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
)
1392 || (i
==sh
->pd_idx
&& failed
== 0))
1393 set_bit(STRIPE_INSYNC
, &sh
->state
);
1395 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
1396 atomic_dec(&conf
->preread_active_stripes
);
1397 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
1398 md_wakeup_thread(conf
->mddev
->thread
);
1403 /* maybe we need to check and possibly fix the parity for this stripe
1404 * Any reads will already have been scheduled, so we just see if enough data
1407 if (syncing
&& locked
== 0 &&
1408 !test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1409 set_bit(STRIPE_HANDLE
, &sh
->state
);
1412 if (uptodate
!= disks
)
1414 compute_parity(sh
, CHECK_PARITY
);
1416 pagea
= page_address(sh
->dev
[sh
->pd_idx
].page
);
1417 if ((*(u32
*)pagea
) == 0 &&
1418 !memcmp(pagea
, pagea
+4, STRIPE_SIZE
-4)) {
1419 /* parity is correct (on disc, not in buffer any more) */
1420 set_bit(STRIPE_INSYNC
, &sh
->state
);
1422 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
1423 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
1424 /* don't try to repair!! */
1425 set_bit(STRIPE_INSYNC
, &sh
->state
);
1427 compute_block(sh
, sh
->pd_idx
);
1432 if (!test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1433 /* either failed parity check, or recovery is happening */
1435 failed_num
= sh
->pd_idx
;
1436 dev
= &sh
->dev
[failed_num
];
1437 BUG_ON(!test_bit(R5_UPTODATE
, &dev
->flags
));
1438 BUG_ON(uptodate
!= disks
);
1440 set_bit(R5_LOCKED
, &dev
->flags
);
1441 set_bit(R5_Wantwrite
, &dev
->flags
);
1442 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
1444 set_bit(STRIPE_INSYNC
, &sh
->state
);
1447 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1448 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
1449 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1452 /* If the failed drive is just a ReadError, then we might need to progress
1453 * the repair/check process
1455 if (failed
== 1 && ! conf
->mddev
->ro
&&
1456 test_bit(R5_ReadError
, &sh
->dev
[failed_num
].flags
)
1457 && !test_bit(R5_LOCKED
, &sh
->dev
[failed_num
].flags
)
1458 && test_bit(R5_UPTODATE
, &sh
->dev
[failed_num
].flags
)
1460 dev
= &sh
->dev
[failed_num
];
1461 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
1462 set_bit(R5_Wantwrite
, &dev
->flags
);
1463 set_bit(R5_ReWrite
, &dev
->flags
);
1464 set_bit(R5_LOCKED
, &dev
->flags
);
1466 /* let's read it back */
1467 set_bit(R5_Wantread
, &dev
->flags
);
1468 set_bit(R5_LOCKED
, &dev
->flags
);
1472 spin_unlock(&sh
->lock
);
1474 while ((bi
=return_bi
)) {
1475 int bytes
= bi
->bi_size
;
1477 return_bi
= bi
->bi_next
;
1480 bi
->bi_end_io(bi
, bytes
, 0);
1482 for (i
=disks
; i
-- ;) {
1486 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
1488 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
1493 bi
= &sh
->dev
[i
].req
;
1497 bi
->bi_end_io
= raid5_end_write_request
;
1499 bi
->bi_end_io
= raid5_end_read_request
;
1502 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1503 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
1506 atomic_inc(&rdev
->nr_pending
);
1511 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
1513 bi
->bi_bdev
= rdev
->bdev
;
1514 PRINTK("for %llu schedule op %ld on disc %d\n",
1515 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
1516 atomic_inc(&sh
->count
);
1517 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
1518 bi
->bi_flags
= 1 << BIO_UPTODATE
;
1520 bi
->bi_max_vecs
= 1;
1522 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
1523 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1524 bi
->bi_io_vec
[0].bv_offset
= 0;
1525 bi
->bi_size
= STRIPE_SIZE
;
1528 test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
1529 atomic_add(STRIPE_SECTORS
, &rdev
->corrected_errors
);
1530 generic_make_request(bi
);
1533 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1534 PRINTK("skip op %ld on disc %d for sector %llu\n",
1535 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
1536 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1537 set_bit(STRIPE_HANDLE
, &sh
->state
);
1542 static void raid5_activate_delayed(raid5_conf_t
*conf
)
1544 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
1545 while (!list_empty(&conf
->delayed_list
)) {
1546 struct list_head
*l
= conf
->delayed_list
.next
;
1547 struct stripe_head
*sh
;
1548 sh
= list_entry(l
, struct stripe_head
, lru
);
1550 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1551 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1552 atomic_inc(&conf
->preread_active_stripes
);
1553 list_add_tail(&sh
->lru
, &conf
->handle_list
);
1558 static void activate_bit_delay(raid5_conf_t
*conf
)
1560 /* device_lock is held */
1561 struct list_head head
;
1562 list_add(&head
, &conf
->bitmap_list
);
1563 list_del_init(&conf
->bitmap_list
);
1564 while (!list_empty(&head
)) {
1565 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
1566 list_del_init(&sh
->lru
);
1567 atomic_inc(&sh
->count
);
1568 __release_stripe(conf
, sh
);
1572 static void unplug_slaves(mddev_t
*mddev
)
1574 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1578 for (i
=0; i
<mddev
->raid_disks
; i
++) {
1579 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1580 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
1581 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
1583 atomic_inc(&rdev
->nr_pending
);
1586 if (r_queue
->unplug_fn
)
1587 r_queue
->unplug_fn(r_queue
);
1589 rdev_dec_pending(rdev
, mddev
);
1596 static void raid5_unplug_device(request_queue_t
*q
)
1598 mddev_t
*mddev
= q
->queuedata
;
1599 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1600 unsigned long flags
;
1602 spin_lock_irqsave(&conf
->device_lock
, flags
);
1604 if (blk_remove_plug(q
)) {
1606 raid5_activate_delayed(conf
);
1608 md_wakeup_thread(mddev
->thread
);
1610 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1612 unplug_slaves(mddev
);
1615 static int raid5_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
1616 sector_t
*error_sector
)
1618 mddev_t
*mddev
= q
->queuedata
;
1619 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1623 for (i
=0; i
<mddev
->raid_disks
&& ret
== 0; i
++) {
1624 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1625 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
1626 struct block_device
*bdev
= rdev
->bdev
;
1627 request_queue_t
*r_queue
= bdev_get_queue(bdev
);
1629 if (!r_queue
->issue_flush_fn
)
1632 atomic_inc(&rdev
->nr_pending
);
1634 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
1636 rdev_dec_pending(rdev
, mddev
);
1645 static inline void raid5_plug_device(raid5_conf_t
*conf
)
1647 spin_lock_irq(&conf
->device_lock
);
1648 blk_plug_device(conf
->mddev
->queue
);
1649 spin_unlock_irq(&conf
->device_lock
);
1652 static int make_request (request_queue_t
*q
, struct bio
* bi
)
1654 mddev_t
*mddev
= q
->queuedata
;
1655 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1656 const unsigned int raid_disks
= conf
->raid_disks
;
1657 const unsigned int data_disks
= raid_disks
- 1;
1658 unsigned int dd_idx
, pd_idx
;
1659 sector_t new_sector
;
1660 sector_t logical_sector
, last_sector
;
1661 struct stripe_head
*sh
;
1662 const int rw
= bio_data_dir(bi
);
1664 if (unlikely(bio_barrier(bi
))) {
1665 bio_endio(bi
, bi
->bi_size
, -EOPNOTSUPP
);
1669 md_write_start(mddev
, bi
);
1671 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
1672 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bi
));
1674 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
1675 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1677 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
1679 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
1682 new_sector
= raid5_compute_sector(logical_sector
,
1683 raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1685 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1686 (unsigned long long)new_sector
,
1687 (unsigned long long)logical_sector
);
1690 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
1691 sh
= get_active_stripe(conf
, new_sector
, pd_idx
, (bi
->bi_rw
&RWA_MASK
));
1693 if (!add_stripe_bio(sh
, bi
, dd_idx
, (bi
->bi_rw
&RW_MASK
))) {
1694 /* Add failed due to overlap. Flush everything
1697 raid5_unplug_device(mddev
->queue
);
1702 finish_wait(&conf
->wait_for_overlap
, &w
);
1703 raid5_plug_device(conf
);
1708 /* cannot get stripe for read-ahead, just give-up */
1709 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1710 finish_wait(&conf
->wait_for_overlap
, &w
);
1715 spin_lock_irq(&conf
->device_lock
);
1716 if (--bi
->bi_phys_segments
== 0) {
1717 int bytes
= bi
->bi_size
;
1719 if ( bio_data_dir(bi
) == WRITE
)
1720 md_write_end(mddev
);
1722 bi
->bi_end_io(bi
, bytes
, 0);
1724 spin_unlock_irq(&conf
->device_lock
);
1728 /* FIXME go_faster isn't used */
1729 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1731 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
1732 struct stripe_head
*sh
;
1733 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1735 unsigned long stripe
;
1738 sector_t first_sector
;
1739 int raid_disks
= conf
->raid_disks
;
1740 int data_disks
= raid_disks
-1;
1741 sector_t max_sector
= mddev
->size
<< 1;
1744 if (sector_nr
>= max_sector
) {
1745 /* just being told to finish up .. nothing much to do */
1746 unplug_slaves(mddev
);
1748 if (mddev
->curr_resync
< max_sector
) /* aborted */
1749 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1751 else /* compelted sync */
1753 bitmap_close_sync(mddev
->bitmap
);
1757 /* if there is 1 or more failed drives and we are trying
1758 * to resync, then assert that we are finished, because there is
1759 * nothing we can do.
1761 if (mddev
->degraded
>= 1 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1762 sector_t rv
= (mddev
->size
<< 1) - sector_nr
;
1766 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
1767 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
1768 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
1769 /* we can skip this block, and probably more */
1770 sync_blocks
/= STRIPE_SECTORS
;
1772 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
1776 chunk_offset
= sector_div(x
, sectors_per_chunk
);
1778 BUG_ON(x
!= stripe
);
1780 first_sector
= raid5_compute_sector((sector_t
)stripe
*data_disks
*sectors_per_chunk
1781 + chunk_offset
, raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1782 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 1);
1784 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 0);
1785 /* make sure we don't swamp the stripe cache if someone else
1786 * is trying to get access
1788 schedule_timeout_uninterruptible(1);
1790 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 0);
1791 spin_lock(&sh
->lock
);
1792 set_bit(STRIPE_SYNCING
, &sh
->state
);
1793 clear_bit(STRIPE_INSYNC
, &sh
->state
);
1794 spin_unlock(&sh
->lock
);
1799 return STRIPE_SECTORS
;
1803 * This is our raid5 kernel thread.
1805 * We scan the hash table for stripes which can be handled now.
1806 * During the scan, completed stripes are saved for us by the interrupt
1807 * handler, so that they will not have to wait for our next wakeup.
1809 static void raid5d (mddev_t
*mddev
)
1811 struct stripe_head
*sh
;
1812 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1815 PRINTK("+++ raid5d active\n");
1817 md_check_recovery(mddev
);
1820 spin_lock_irq(&conf
->device_lock
);
1822 struct list_head
*first
;
1824 if (conf
->seq_flush
- conf
->seq_write
> 0) {
1825 int seq
= conf
->seq_flush
;
1826 spin_unlock_irq(&conf
->device_lock
);
1827 bitmap_unplug(mddev
->bitmap
);
1828 spin_lock_irq(&conf
->device_lock
);
1829 conf
->seq_write
= seq
;
1830 activate_bit_delay(conf
);
1833 if (list_empty(&conf
->handle_list
) &&
1834 atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
&&
1835 !blk_queue_plugged(mddev
->queue
) &&
1836 !list_empty(&conf
->delayed_list
))
1837 raid5_activate_delayed(conf
);
1839 if (list_empty(&conf
->handle_list
))
1842 first
= conf
->handle_list
.next
;
1843 sh
= list_entry(first
, struct stripe_head
, lru
);
1845 list_del_init(first
);
1846 atomic_inc(&sh
->count
);
1847 if (atomic_read(&sh
->count
)!= 1)
1849 spin_unlock_irq(&conf
->device_lock
);
1855 spin_lock_irq(&conf
->device_lock
);
1857 PRINTK("%d stripes handled\n", handled
);
1859 spin_unlock_irq(&conf
->device_lock
);
1861 unplug_slaves(mddev
);
1863 PRINTK("--- raid5d inactive\n");
1867 raid5_show_stripe_cache_size(mddev_t
*mddev
, char *page
)
1869 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1871 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
1877 raid5_store_stripe_cache_size(mddev_t
*mddev
, const char *page
, size_t len
)
1879 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1882 if (len
>= PAGE_SIZE
)
1887 new = simple_strtoul(page
, &end
, 10);
1888 if (!*page
|| (*end
&& *end
!= '\n') )
1890 if (new <= 16 || new > 32768)
1892 while (new < conf
->max_nr_stripes
) {
1893 if (drop_one_stripe(conf
))
1894 conf
->max_nr_stripes
--;
1898 while (new > conf
->max_nr_stripes
) {
1899 if (grow_one_stripe(conf
))
1900 conf
->max_nr_stripes
++;
1906 static struct md_sysfs_entry
1907 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
1908 raid5_show_stripe_cache_size
,
1909 raid5_store_stripe_cache_size
);
1912 stripe_cache_active_show(mddev_t
*mddev
, char *page
)
1914 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1916 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
1921 static struct md_sysfs_entry
1922 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
1924 static struct attribute
*raid5_attrs
[] = {
1925 &raid5_stripecache_size
.attr
,
1926 &raid5_stripecache_active
.attr
,
1929 static struct attribute_group raid5_attrs_group
= {
1931 .attrs
= raid5_attrs
,
1934 static int run(mddev_t
*mddev
)
1937 int raid_disk
, memory
;
1939 struct disk_info
*disk
;
1940 struct list_head
*tmp
;
1942 if (mddev
->level
!= 5 && mddev
->level
!= 4) {
1943 printk(KERN_ERR
"raid5: %s: raid level not set to 4/5 (%d)\n",
1944 mdname(mddev
), mddev
->level
);
1948 mddev
->private = kzalloc(sizeof (raid5_conf_t
), GFP_KERNEL
);
1949 if ((conf
= mddev
->private) == NULL
)
1951 conf
->disks
= kzalloc(mddev
->raid_disks
* sizeof(struct disk_info
),
1956 conf
->mddev
= mddev
;
1958 if ((conf
->stripe_hashtbl
= kzalloc(PAGE_SIZE
, GFP_KERNEL
)) == NULL
)
1961 spin_lock_init(&conf
->device_lock
);
1962 init_waitqueue_head(&conf
->wait_for_stripe
);
1963 init_waitqueue_head(&conf
->wait_for_overlap
);
1964 INIT_LIST_HEAD(&conf
->handle_list
);
1965 INIT_LIST_HEAD(&conf
->delayed_list
);
1966 INIT_LIST_HEAD(&conf
->bitmap_list
);
1967 INIT_LIST_HEAD(&conf
->inactive_list
);
1968 atomic_set(&conf
->active_stripes
, 0);
1969 atomic_set(&conf
->preread_active_stripes
, 0);
1971 PRINTK("raid5: run(%s) called.\n", mdname(mddev
));
1973 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1974 raid_disk
= rdev
->raid_disk
;
1975 if (raid_disk
>= mddev
->raid_disks
1978 disk
= conf
->disks
+ raid_disk
;
1982 if (test_bit(In_sync
, &rdev
->flags
)) {
1983 char b
[BDEVNAME_SIZE
];
1984 printk(KERN_INFO
"raid5: device %s operational as raid"
1985 " disk %d\n", bdevname(rdev
->bdev
,b
),
1987 conf
->working_disks
++;
1991 conf
->raid_disks
= mddev
->raid_disks
;
1993 * 0 for a fully functional array, 1 for a degraded array.
1995 mddev
->degraded
= conf
->failed_disks
= conf
->raid_disks
- conf
->working_disks
;
1996 conf
->mddev
= mddev
;
1997 conf
->chunk_size
= mddev
->chunk_size
;
1998 conf
->level
= mddev
->level
;
1999 conf
->algorithm
= mddev
->layout
;
2000 conf
->max_nr_stripes
= NR_STRIPES
;
2002 /* device size must be a multiple of chunk size */
2003 mddev
->size
&= ~(mddev
->chunk_size
/1024 -1);
2004 mddev
->resync_max_sectors
= mddev
->size
<< 1;
2006 if (!conf
->chunk_size
|| conf
->chunk_size
% 4) {
2007 printk(KERN_ERR
"raid5: invalid chunk size %d for %s\n",
2008 conf
->chunk_size
, mdname(mddev
));
2011 if (conf
->algorithm
> ALGORITHM_RIGHT_SYMMETRIC
) {
2013 "raid5: unsupported parity algorithm %d for %s\n",
2014 conf
->algorithm
, mdname(mddev
));
2017 if (mddev
->degraded
> 1) {
2018 printk(KERN_ERR
"raid5: not enough operational devices for %s"
2019 " (%d/%d failed)\n",
2020 mdname(mddev
), conf
->failed_disks
, conf
->raid_disks
);
2024 if (mddev
->degraded
== 1 &&
2025 mddev
->recovery_cp
!= MaxSector
) {
2026 if (mddev
->ok_start_degraded
)
2028 "raid5: starting dirty degraded array: %s"
2029 "- data corruption possible.\n",
2033 "raid5: cannot start dirty degraded array for %s\n",
2040 mddev
->thread
= md_register_thread(raid5d
, mddev
, "%s_raid5");
2041 if (!mddev
->thread
) {
2043 "raid5: couldn't allocate thread for %s\n",
2048 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
2049 conf
->raid_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
2050 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
2052 "raid5: couldn't allocate %dkB for buffers\n", memory
);
2053 shrink_stripes(conf
);
2054 md_unregister_thread(mddev
->thread
);
2057 printk(KERN_INFO
"raid5: allocated %dkB for %s\n",
2058 memory
, mdname(mddev
));
2060 if (mddev
->degraded
== 0)
2061 printk("raid5: raid level %d set %s active with %d out of %d"
2062 " devices, algorithm %d\n", conf
->level
, mdname(mddev
),
2063 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
2066 printk(KERN_ALERT
"raid5: raid level %d set %s active with %d"
2067 " out of %d devices, algorithm %d\n", conf
->level
,
2068 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
2069 mddev
->raid_disks
, conf
->algorithm
);
2071 print_raid5_conf(conf
);
2073 /* read-ahead size must cover two whole stripes, which is
2074 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
2077 int stripe
= (mddev
->raid_disks
-1) * mddev
->chunk_size
2079 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
2080 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
2083 /* Ok, everything is just fine now */
2084 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
);
2086 mddev
->queue
->unplug_fn
= raid5_unplug_device
;
2087 mddev
->queue
->issue_flush_fn
= raid5_issue_flush
;
2089 mddev
->array_size
= mddev
->size
* (mddev
->raid_disks
- 1);
2093 print_raid5_conf(conf
);
2095 kfree(conf
->stripe_hashtbl
);
2098 mddev
->private = NULL
;
2099 printk(KERN_ALERT
"raid5: failed to run raid set %s\n", mdname(mddev
));
2105 static int stop(mddev_t
*mddev
)
2107 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
2109 md_unregister_thread(mddev
->thread
);
2110 mddev
->thread
= NULL
;
2111 shrink_stripes(conf
);
2112 kfree(conf
->stripe_hashtbl
);
2113 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
2114 sysfs_remove_group(&mddev
->kobj
, &raid5_attrs_group
);
2117 mddev
->private = NULL
;
2122 static void print_sh (struct stripe_head
*sh
)
2126 printk("sh %llu, pd_idx %d, state %ld.\n",
2127 (unsigned long long)sh
->sector
, sh
->pd_idx
, sh
->state
);
2128 printk("sh %llu, count %d.\n",
2129 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
));
2130 printk("sh %llu, ", (unsigned long long)sh
->sector
);
2131 for (i
= 0; i
< sh
->raid_conf
->raid_disks
; i
++) {
2132 printk("(cache%d: %p %ld) ",
2133 i
, sh
->dev
[i
].page
, sh
->dev
[i
].flags
);
2138 static void printall (raid5_conf_t
*conf
)
2140 struct stripe_head
*sh
;
2141 struct hlist_node
*hn
;
2144 spin_lock_irq(&conf
->device_lock
);
2145 for (i
= 0; i
< NR_HASH
; i
++) {
2146 hlist_for_each_entry(sh
, hn
, &conf
->stripe_hashtbl
[i
], hash
) {
2147 if (sh
->raid_conf
!= conf
)
2152 spin_unlock_irq(&conf
->device_lock
);
2156 static void status (struct seq_file
*seq
, mddev_t
*mddev
)
2158 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
2161 seq_printf (seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
, mddev
->chunk_size
>> 10, mddev
->layout
);
2162 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->working_disks
);
2163 for (i
= 0; i
< conf
->raid_disks
; i
++)
2164 seq_printf (seq
, "%s",
2165 conf
->disks
[i
].rdev
&&
2166 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
2167 seq_printf (seq
, "]");
2170 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
2175 static void print_raid5_conf (raid5_conf_t
*conf
)
2178 struct disk_info
*tmp
;
2180 printk("RAID5 conf printout:\n");
2182 printk("(conf==NULL)\n");
2185 printk(" --- rd:%d wd:%d fd:%d\n", conf
->raid_disks
,
2186 conf
->working_disks
, conf
->failed_disks
);
2188 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2189 char b
[BDEVNAME_SIZE
];
2190 tmp
= conf
->disks
+ i
;
2192 printk(" disk %d, o:%d, dev:%s\n",
2193 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
2194 bdevname(tmp
->rdev
->bdev
,b
));
2198 static int raid5_spare_active(mddev_t
*mddev
)
2201 raid5_conf_t
*conf
= mddev
->private;
2202 struct disk_info
*tmp
;
2204 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2205 tmp
= conf
->disks
+ i
;
2207 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
2208 && !test_bit(In_sync
, &tmp
->rdev
->flags
)) {
2210 conf
->failed_disks
--;
2211 conf
->working_disks
++;
2212 set_bit(In_sync
, &tmp
->rdev
->flags
);
2215 print_raid5_conf(conf
);
2219 static int raid5_remove_disk(mddev_t
*mddev
, int number
)
2221 raid5_conf_t
*conf
= mddev
->private;
2224 struct disk_info
*p
= conf
->disks
+ number
;
2226 print_raid5_conf(conf
);
2229 if (test_bit(In_sync
, &rdev
->flags
) ||
2230 atomic_read(&rdev
->nr_pending
)) {
2236 if (atomic_read(&rdev
->nr_pending
)) {
2237 /* lost the race, try later */
2244 print_raid5_conf(conf
);
2248 static int raid5_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
2250 raid5_conf_t
*conf
= mddev
->private;
2253 struct disk_info
*p
;
2255 if (mddev
->degraded
> 1)
2256 /* no point adding a device */
2262 for (disk
=0; disk
< mddev
->raid_disks
; disk
++)
2263 if ((p
=conf
->disks
+ disk
)->rdev
== NULL
) {
2264 clear_bit(In_sync
, &rdev
->flags
);
2265 rdev
->raid_disk
= disk
;
2267 if (rdev
->saved_raid_disk
!= disk
)
2269 rcu_assign_pointer(p
->rdev
, rdev
);
2272 print_raid5_conf(conf
);
2276 static int raid5_resize(mddev_t
*mddev
, sector_t sectors
)
2278 /* no resync is happening, and there is enough space
2279 * on all devices, so we can resize.
2280 * We need to make sure resync covers any new space.
2281 * If the array is shrinking we should possibly wait until
2282 * any io in the removed space completes, but it hardly seems
2285 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
2286 mddev
->array_size
= (sectors
* (mddev
->raid_disks
-1))>>1;
2287 set_capacity(mddev
->gendisk
, mddev
->array_size
<< 1);
2289 if (sectors
/2 > mddev
->size
&& mddev
->recovery_cp
== MaxSector
) {
2290 mddev
->recovery_cp
= mddev
->size
<< 1;
2291 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2293 mddev
->size
= sectors
/2;
2294 mddev
->resync_max_sectors
= sectors
;
2298 static void raid5_quiesce(mddev_t
*mddev
, int state
)
2300 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2303 case 1: /* stop all writes */
2304 spin_lock_irq(&conf
->device_lock
);
2306 wait_event_lock_irq(conf
->wait_for_stripe
,
2307 atomic_read(&conf
->active_stripes
) == 0,
2308 conf
->device_lock
, /* nothing */);
2309 spin_unlock_irq(&conf
->device_lock
);
2312 case 0: /* re-enable writes */
2313 spin_lock_irq(&conf
->device_lock
);
2315 wake_up(&conf
->wait_for_stripe
);
2316 spin_unlock_irq(&conf
->device_lock
);
2321 static struct mdk_personality raid5_personality
=
2325 .owner
= THIS_MODULE
,
2326 .make_request
= make_request
,
2330 .error_handler
= error
,
2331 .hot_add_disk
= raid5_add_disk
,
2332 .hot_remove_disk
= raid5_remove_disk
,
2333 .spare_active
= raid5_spare_active
,
2334 .sync_request
= sync_request
,
2335 .resize
= raid5_resize
,
2336 .quiesce
= raid5_quiesce
,
2339 static struct mdk_personality raid4_personality
=
2343 .owner
= THIS_MODULE
,
2344 .make_request
= make_request
,
2348 .error_handler
= error
,
2349 .hot_add_disk
= raid5_add_disk
,
2350 .hot_remove_disk
= raid5_remove_disk
,
2351 .spare_active
= raid5_spare_active
,
2352 .sync_request
= sync_request
,
2353 .resize
= raid5_resize
,
2354 .quiesce
= raid5_quiesce
,
2357 static int __init
raid5_init(void)
2359 register_md_personality(&raid5_personality
);
2360 register_md_personality(&raid4_personality
);
2364 static void raid5_exit(void)
2366 unregister_md_personality(&raid5_personality
);
2367 unregister_md_personality(&raid4_personality
);
2370 module_init(raid5_init
);
2371 module_exit(raid5_exit
);
2372 MODULE_LICENSE("GPL");
2373 MODULE_ALIAS("md-personality-4"); /* RAID5 */
2374 MODULE_ALIAS("md-raid5");
2375 MODULE_ALIAS("md-raid4");
2376 MODULE_ALIAS("md-level-5");
2377 MODULE_ALIAS("md-level-4");