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
39 #define HASH_PAGES_ORDER 0
40 #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
41 #define HASH_MASK (NR_HASH - 1)
43 #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
45 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
46 * order without overlap. There may be several bio's per stripe+device, and
47 * a bio could span several devices.
48 * When walking this list for a particular stripe+device, we must never proceed
49 * beyond a bio that extends past this device, as the next bio might no longer
51 * This macro is used to determine the 'next' bio in the list, given the sector
52 * of the current stripe+device
54 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
56 * The following can be used to debug the driver
59 #define RAID5_PARANOIA 1
60 #if RAID5_PARANOIA && defined(CONFIG_SMP)
61 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
63 # define CHECK_DEVLOCK()
66 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
72 static void print_raid5_conf (raid5_conf_t
*conf
);
74 static inline void __release_stripe(raid5_conf_t
*conf
, struct stripe_head
*sh
)
76 if (atomic_dec_and_test(&sh
->count
)) {
77 if (!list_empty(&sh
->lru
))
79 if (atomic_read(&conf
->active_stripes
)==0)
81 if (test_bit(STRIPE_HANDLE
, &sh
->state
)) {
82 if (test_bit(STRIPE_DELAYED
, &sh
->state
))
83 list_add_tail(&sh
->lru
, &conf
->delayed_list
);
84 else if (test_bit(STRIPE_BIT_DELAY
, &sh
->state
) &&
85 conf
->seq_write
== sh
->bm_seq
)
86 list_add_tail(&sh
->lru
, &conf
->bitmap_list
);
88 clear_bit(STRIPE_BIT_DELAY
, &sh
->state
);
89 list_add_tail(&sh
->lru
, &conf
->handle_list
);
91 md_wakeup_thread(conf
->mddev
->thread
);
93 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
94 atomic_dec(&conf
->preread_active_stripes
);
95 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
96 md_wakeup_thread(conf
->mddev
->thread
);
98 list_add_tail(&sh
->lru
, &conf
->inactive_list
);
99 atomic_dec(&conf
->active_stripes
);
100 if (!conf
->inactive_blocked
||
101 atomic_read(&conf
->active_stripes
) < (conf
->max_nr_stripes
*3/4))
102 wake_up(&conf
->wait_for_stripe
);
106 static void release_stripe(struct stripe_head
*sh
)
108 raid5_conf_t
*conf
= sh
->raid_conf
;
111 spin_lock_irqsave(&conf
->device_lock
, flags
);
112 __release_stripe(conf
, sh
);
113 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
116 static void remove_hash(struct stripe_head
*sh
)
118 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
120 if (sh
->hash_pprev
) {
122 sh
->hash_next
->hash_pprev
= sh
->hash_pprev
;
123 *sh
->hash_pprev
= sh
->hash_next
;
124 sh
->hash_pprev
= NULL
;
128 static __inline__
void insert_hash(raid5_conf_t
*conf
, struct stripe_head
*sh
)
130 struct stripe_head
**shp
= &stripe_hash(conf
, sh
->sector
);
132 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh
->sector
);
135 if ((sh
->hash_next
= *shp
) != NULL
)
136 (*shp
)->hash_pprev
= &sh
->hash_next
;
138 sh
->hash_pprev
= shp
;
142 /* find an idle stripe, make sure it is unhashed, and return it. */
143 static struct stripe_head
*get_free_stripe(raid5_conf_t
*conf
)
145 struct stripe_head
*sh
= NULL
;
146 struct list_head
*first
;
149 if (list_empty(&conf
->inactive_list
))
151 first
= conf
->inactive_list
.next
;
152 sh
= list_entry(first
, struct stripe_head
, lru
);
153 list_del_init(first
);
155 atomic_inc(&conf
->active_stripes
);
160 static void shrink_buffers(struct stripe_head
*sh
, int num
)
165 for (i
=0; i
<num
; i
++) {
169 sh
->dev
[i
].page
= NULL
;
170 page_cache_release(p
);
174 static int grow_buffers(struct stripe_head
*sh
, int num
)
178 for (i
=0; i
<num
; i
++) {
181 if (!(page
= alloc_page(GFP_KERNEL
))) {
184 sh
->dev
[i
].page
= page
;
189 static void raid5_build_block (struct stripe_head
*sh
, int i
);
191 static inline void init_stripe(struct stripe_head
*sh
, sector_t sector
, int pd_idx
)
193 raid5_conf_t
*conf
= sh
->raid_conf
;
194 int disks
= conf
->raid_disks
, i
;
196 if (atomic_read(&sh
->count
) != 0)
198 if (test_bit(STRIPE_HANDLE
, &sh
->state
))
202 PRINTK("init_stripe called, stripe %llu\n",
203 (unsigned long long)sh
->sector
);
211 for (i
=disks
; i
--; ) {
212 struct r5dev
*dev
= &sh
->dev
[i
];
214 if (dev
->toread
|| dev
->towrite
|| dev
->written
||
215 test_bit(R5_LOCKED
, &dev
->flags
)) {
216 printk("sector=%llx i=%d %p %p %p %d\n",
217 (unsigned long long)sh
->sector
, i
, dev
->toread
,
218 dev
->towrite
, dev
->written
,
219 test_bit(R5_LOCKED
, &dev
->flags
));
223 raid5_build_block(sh
, i
);
225 insert_hash(conf
, sh
);
228 static struct stripe_head
*__find_stripe(raid5_conf_t
*conf
, sector_t sector
)
230 struct stripe_head
*sh
;
233 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector
);
234 for (sh
= stripe_hash(conf
, sector
); sh
; sh
= sh
->hash_next
)
235 if (sh
->sector
== sector
)
237 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector
);
241 static void unplug_slaves(mddev_t
*mddev
);
242 static void raid5_unplug_device(request_queue_t
*q
);
244 static struct stripe_head
*get_active_stripe(raid5_conf_t
*conf
, sector_t sector
,
245 int pd_idx
, int noblock
)
247 struct stripe_head
*sh
;
249 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector
);
251 spin_lock_irq(&conf
->device_lock
);
254 wait_event_lock_irq(conf
->wait_for_stripe
,
256 conf
->device_lock
, /* nothing */);
257 sh
= __find_stripe(conf
, sector
);
259 if (!conf
->inactive_blocked
)
260 sh
= get_free_stripe(conf
);
261 if (noblock
&& sh
== NULL
)
264 conf
->inactive_blocked
= 1;
265 wait_event_lock_irq(conf
->wait_for_stripe
,
266 !list_empty(&conf
->inactive_list
) &&
267 (atomic_read(&conf
->active_stripes
)
268 < (conf
->max_nr_stripes
*3/4)
269 || !conf
->inactive_blocked
),
271 unplug_slaves(conf
->mddev
);
273 conf
->inactive_blocked
= 0;
275 init_stripe(sh
, sector
, pd_idx
);
277 if (atomic_read(&sh
->count
)) {
278 if (!list_empty(&sh
->lru
))
281 if (!test_bit(STRIPE_HANDLE
, &sh
->state
))
282 atomic_inc(&conf
->active_stripes
);
283 if (list_empty(&sh
->lru
))
285 list_del_init(&sh
->lru
);
288 } while (sh
== NULL
);
291 atomic_inc(&sh
->count
);
293 spin_unlock_irq(&conf
->device_lock
);
297 static int grow_one_stripe(raid5_conf_t
*conf
)
299 struct stripe_head
*sh
;
300 sh
= kmem_cache_alloc(conf
->slab_cache
, GFP_KERNEL
);
303 memset(sh
, 0, sizeof(*sh
) + (conf
->raid_disks
-1)*sizeof(struct r5dev
));
304 sh
->raid_conf
= conf
;
305 spin_lock_init(&sh
->lock
);
307 if (grow_buffers(sh
, conf
->raid_disks
)) {
308 shrink_buffers(sh
, conf
->raid_disks
);
309 kmem_cache_free(conf
->slab_cache
, sh
);
312 /* we just created an active stripe so... */
313 atomic_set(&sh
->count
, 1);
314 atomic_inc(&conf
->active_stripes
);
315 INIT_LIST_HEAD(&sh
->lru
);
320 static int grow_stripes(raid5_conf_t
*conf
, int num
)
323 int devs
= conf
->raid_disks
;
325 sprintf(conf
->cache_name
, "raid5/%s", mdname(conf
->mddev
));
327 sc
= kmem_cache_create(conf
->cache_name
,
328 sizeof(struct stripe_head
)+(devs
-1)*sizeof(struct r5dev
),
332 conf
->slab_cache
= sc
;
334 if (!grow_one_stripe(conf
))
340 static int drop_one_stripe(raid5_conf_t
*conf
)
342 struct stripe_head
*sh
;
344 spin_lock_irq(&conf
->device_lock
);
345 sh
= get_free_stripe(conf
);
346 spin_unlock_irq(&conf
->device_lock
);
349 if (atomic_read(&sh
->count
))
351 shrink_buffers(sh
, conf
->raid_disks
);
352 kmem_cache_free(conf
->slab_cache
, sh
);
353 atomic_dec(&conf
->active_stripes
);
357 static void shrink_stripes(raid5_conf_t
*conf
)
359 while (drop_one_stripe(conf
))
362 kmem_cache_destroy(conf
->slab_cache
);
363 conf
->slab_cache
= NULL
;
366 static int raid5_end_read_request(struct bio
* bi
, unsigned int bytes_done
,
369 struct stripe_head
*sh
= bi
->bi_private
;
370 raid5_conf_t
*conf
= sh
->raid_conf
;
371 int disks
= conf
->raid_disks
, i
;
372 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
377 for (i
=0 ; i
<disks
; i
++)
378 if (bi
== &sh
->dev
[i
].req
)
381 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
382 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
393 spin_lock_irqsave(&conf
->device_lock
, flags
);
394 /* we can return a buffer if we bypassed the cache or
395 * if the top buffer is not in highmem. If there are
396 * multiple buffers, leave the extra work to
399 buffer
= sh
->bh_read
[i
];
401 (!PageHighMem(buffer
->b_page
)
402 || buffer
->b_page
== bh
->b_page
)
404 sh
->bh_read
[i
] = buffer
->b_reqnext
;
405 buffer
->b_reqnext
= NULL
;
408 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
409 if (sh
->bh_page
[i
]==bh
->b_page
)
410 set_buffer_uptodate(bh
);
412 if (buffer
->b_page
!= bh
->b_page
)
413 memcpy(buffer
->b_data
, bh
->b_data
, bh
->b_size
);
414 buffer
->b_end_io(buffer
, 1);
417 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
419 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
420 printk("R5: read error corrected!!\n");
421 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
422 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
424 if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
))
425 atomic_set(&conf
->disks
[i
].rdev
->read_errors
, 0);
428 clear_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
429 atomic_inc(&conf
->disks
[i
].rdev
->read_errors
);
430 if (conf
->mddev
->degraded
)
431 printk("R5: read error not correctable.\n");
432 else if (test_bit(R5_ReWrite
, &sh
->dev
[i
].flags
))
434 printk("R5: read error NOT corrected!!\n");
435 else if (atomic_read(&conf
->disks
[i
].rdev
->read_errors
)
436 > conf
->max_nr_stripes
)
437 printk("raid5: Too many read errors, failing device.\n");
441 set_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
443 clear_bit(R5_ReadError
, &sh
->dev
[i
].flags
);
444 clear_bit(R5_ReWrite
, &sh
->dev
[i
].flags
);
445 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
448 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
450 /* must restore b_page before unlocking buffer... */
451 if (sh
->bh_page
[i
] != bh
->b_page
) {
452 bh
->b_page
= sh
->bh_page
[i
];
453 bh
->b_data
= page_address(bh
->b_page
);
454 clear_buffer_uptodate(bh
);
457 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
458 set_bit(STRIPE_HANDLE
, &sh
->state
);
463 static int raid5_end_write_request (struct bio
*bi
, unsigned int bytes_done
,
466 struct stripe_head
*sh
= bi
->bi_private
;
467 raid5_conf_t
*conf
= sh
->raid_conf
;
468 int disks
= conf
->raid_disks
, i
;
470 int uptodate
= test_bit(BIO_UPTODATE
, &bi
->bi_flags
);
475 for (i
=0 ; i
<disks
; i
++)
476 if (bi
== &sh
->dev
[i
].req
)
479 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
480 (unsigned long long)sh
->sector
, i
, atomic_read(&sh
->count
),
487 spin_lock_irqsave(&conf
->device_lock
, flags
);
489 md_error(conf
->mddev
, conf
->disks
[i
].rdev
);
491 rdev_dec_pending(conf
->disks
[i
].rdev
, conf
->mddev
);
493 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
494 set_bit(STRIPE_HANDLE
, &sh
->state
);
495 __release_stripe(conf
, sh
);
496 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
501 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
);
503 static void raid5_build_block (struct stripe_head
*sh
, int i
)
505 struct r5dev
*dev
= &sh
->dev
[i
];
508 dev
->req
.bi_io_vec
= &dev
->vec
;
510 dev
->req
.bi_max_vecs
++;
511 dev
->vec
.bv_page
= dev
->page
;
512 dev
->vec
.bv_len
= STRIPE_SIZE
;
513 dev
->vec
.bv_offset
= 0;
515 dev
->req
.bi_sector
= sh
->sector
;
516 dev
->req
.bi_private
= sh
;
520 dev
->sector
= compute_blocknr(sh
, i
);
523 static void error(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
525 char b
[BDEVNAME_SIZE
];
526 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
527 PRINTK("raid5: error called\n");
529 if (!test_bit(Faulty
, &rdev
->flags
)) {
531 if (test_bit(In_sync
, &rdev
->flags
)) {
532 conf
->working_disks
--;
534 conf
->failed_disks
++;
535 clear_bit(In_sync
, &rdev
->flags
);
537 * if recovery was running, make sure it aborts.
539 set_bit(MD_RECOVERY_ERR
, &mddev
->recovery
);
541 set_bit(Faulty
, &rdev
->flags
);
543 "raid5: Disk failure on %s, disabling device."
544 " Operation continuing on %d devices\n",
545 bdevname(rdev
->bdev
,b
), conf
->working_disks
);
550 * Input: a 'big' sector number,
551 * Output: index of the data and parity disk, and the sector # in them.
553 static sector_t
raid5_compute_sector(sector_t r_sector
, unsigned int raid_disks
,
554 unsigned int data_disks
, unsigned int * dd_idx
,
555 unsigned int * pd_idx
, raid5_conf_t
*conf
)
558 unsigned long chunk_number
;
559 unsigned int chunk_offset
;
561 int sectors_per_chunk
= conf
->chunk_size
>> 9;
563 /* First compute the information on this sector */
566 * Compute the chunk number and the sector offset inside the chunk
568 chunk_offset
= sector_div(r_sector
, sectors_per_chunk
);
569 chunk_number
= r_sector
;
570 BUG_ON(r_sector
!= chunk_number
);
573 * Compute the stripe number
575 stripe
= chunk_number
/ data_disks
;
578 * Compute the data disk and parity disk indexes inside the stripe
580 *dd_idx
= chunk_number
% data_disks
;
583 * Select the parity disk based on the user selected algorithm.
585 if (conf
->level
== 4)
586 *pd_idx
= data_disks
;
587 else switch (conf
->algorithm
) {
588 case ALGORITHM_LEFT_ASYMMETRIC
:
589 *pd_idx
= data_disks
- stripe
% raid_disks
;
590 if (*dd_idx
>= *pd_idx
)
593 case ALGORITHM_RIGHT_ASYMMETRIC
:
594 *pd_idx
= stripe
% raid_disks
;
595 if (*dd_idx
>= *pd_idx
)
598 case ALGORITHM_LEFT_SYMMETRIC
:
599 *pd_idx
= data_disks
- stripe
% raid_disks
;
600 *dd_idx
= (*pd_idx
+ 1 + *dd_idx
) % raid_disks
;
602 case ALGORITHM_RIGHT_SYMMETRIC
:
603 *pd_idx
= stripe
% raid_disks
;
604 *dd_idx
= (*pd_idx
+ 1 + *dd_idx
) % raid_disks
;
607 printk("raid5: unsupported algorithm %d\n",
612 * Finally, compute the new sector number
614 new_sector
= (sector_t
)stripe
* sectors_per_chunk
+ chunk_offset
;
619 static sector_t
compute_blocknr(struct stripe_head
*sh
, int i
)
621 raid5_conf_t
*conf
= sh
->raid_conf
;
622 int raid_disks
= conf
->raid_disks
, data_disks
= raid_disks
- 1;
623 sector_t new_sector
= sh
->sector
, check
;
624 int sectors_per_chunk
= conf
->chunk_size
>> 9;
627 int chunk_number
, dummy1
, dummy2
, dd_idx
= i
;
630 chunk_offset
= sector_div(new_sector
, sectors_per_chunk
);
632 BUG_ON(new_sector
!= stripe
);
635 switch (conf
->algorithm
) {
636 case ALGORITHM_LEFT_ASYMMETRIC
:
637 case ALGORITHM_RIGHT_ASYMMETRIC
:
641 case ALGORITHM_LEFT_SYMMETRIC
:
642 case ALGORITHM_RIGHT_SYMMETRIC
:
645 i
-= (sh
->pd_idx
+ 1);
648 printk("raid5: unsupported algorithm %d\n",
652 chunk_number
= stripe
* data_disks
+ i
;
653 r_sector
= (sector_t
)chunk_number
* sectors_per_chunk
+ chunk_offset
;
655 check
= raid5_compute_sector (r_sector
, raid_disks
, data_disks
, &dummy1
, &dummy2
, conf
);
656 if (check
!= sh
->sector
|| dummy1
!= dd_idx
|| dummy2
!= sh
->pd_idx
) {
657 printk("compute_blocknr: map not correct\n");
666 * Copy data between a page in the stripe cache, and a bio.
667 * There are no alignment or size guarantees between the page or the
668 * bio except that there is some overlap.
669 * All iovecs in the bio must be considered.
671 static void copy_data(int frombio
, struct bio
*bio
,
675 char *pa
= page_address(page
);
680 if (bio
->bi_sector
>= sector
)
681 page_offset
= (signed)(bio
->bi_sector
- sector
) * 512;
683 page_offset
= (signed)(sector
- bio
->bi_sector
) * -512;
684 bio_for_each_segment(bvl
, bio
, i
) {
685 int len
= bio_iovec_idx(bio
,i
)->bv_len
;
689 if (page_offset
< 0) {
690 b_offset
= -page_offset
;
691 page_offset
+= b_offset
;
695 if (len
> 0 && page_offset
+ len
> STRIPE_SIZE
)
696 clen
= STRIPE_SIZE
- page_offset
;
700 char *ba
= __bio_kmap_atomic(bio
, i
, KM_USER0
);
702 memcpy(pa
+page_offset
, ba
+b_offset
, clen
);
704 memcpy(ba
+b_offset
, pa
+page_offset
, clen
);
705 __bio_kunmap_atomic(ba
, KM_USER0
);
707 if (clen
< len
) /* hit end of page */
713 #define check_xor() do { \
714 if (count == MAX_XOR_BLOCKS) { \
715 xor_block(count, STRIPE_SIZE, ptr); \
721 static void compute_block(struct stripe_head
*sh
, int dd_idx
)
723 raid5_conf_t
*conf
= sh
->raid_conf
;
724 int i
, count
, disks
= conf
->raid_disks
;
725 void *ptr
[MAX_XOR_BLOCKS
], *p
;
727 PRINTK("compute_block, stripe %llu, idx %d\n",
728 (unsigned long long)sh
->sector
, dd_idx
);
730 ptr
[0] = page_address(sh
->dev
[dd_idx
].page
);
731 memset(ptr
[0], 0, STRIPE_SIZE
);
733 for (i
= disks
; i
--; ) {
736 p
= page_address(sh
->dev
[i
].page
);
737 if (test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
))
740 printk("compute_block() %d, stripe %llu, %d"
741 " not present\n", dd_idx
,
742 (unsigned long long)sh
->sector
, i
);
747 xor_block(count
, STRIPE_SIZE
, ptr
);
748 set_bit(R5_UPTODATE
, &sh
->dev
[dd_idx
].flags
);
751 static void compute_parity(struct stripe_head
*sh
, int method
)
753 raid5_conf_t
*conf
= sh
->raid_conf
;
754 int i
, pd_idx
= sh
->pd_idx
, disks
= conf
->raid_disks
, count
;
755 void *ptr
[MAX_XOR_BLOCKS
];
758 PRINTK("compute_parity, stripe %llu, method %d\n",
759 (unsigned long long)sh
->sector
, method
);
762 ptr
[0] = page_address(sh
->dev
[pd_idx
].page
);
764 case READ_MODIFY_WRITE
:
765 if (!test_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
))
767 for (i
=disks
; i
-- ;) {
770 if (sh
->dev
[i
].towrite
&&
771 test_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
)) {
772 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
773 chosen
= sh
->dev
[i
].towrite
;
774 sh
->dev
[i
].towrite
= NULL
;
776 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
777 wake_up(&conf
->wait_for_overlap
);
779 if (sh
->dev
[i
].written
) BUG();
780 sh
->dev
[i
].written
= chosen
;
785 case RECONSTRUCT_WRITE
:
786 memset(ptr
[0], 0, STRIPE_SIZE
);
787 for (i
= disks
; i
-- ;)
788 if (i
!=pd_idx
&& sh
->dev
[i
].towrite
) {
789 chosen
= sh
->dev
[i
].towrite
;
790 sh
->dev
[i
].towrite
= NULL
;
792 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
793 wake_up(&conf
->wait_for_overlap
);
795 if (sh
->dev
[i
].written
) BUG();
796 sh
->dev
[i
].written
= chosen
;
803 xor_block(count
, STRIPE_SIZE
, ptr
);
807 for (i
= disks
; i
--;)
808 if (sh
->dev
[i
].written
) {
809 sector_t sector
= sh
->dev
[i
].sector
;
810 struct bio
*wbi
= sh
->dev
[i
].written
;
811 while (wbi
&& wbi
->bi_sector
< sector
+ STRIPE_SECTORS
) {
812 copy_data(1, wbi
, sh
->dev
[i
].page
, sector
);
813 wbi
= r5_next_bio(wbi
, sector
);
816 set_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
817 set_bit(R5_UPTODATE
, &sh
->dev
[i
].flags
);
821 case RECONSTRUCT_WRITE
:
825 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
829 case READ_MODIFY_WRITE
:
830 for (i
= disks
; i
--;)
831 if (sh
->dev
[i
].written
) {
832 ptr
[count
++] = page_address(sh
->dev
[i
].page
);
837 xor_block(count
, STRIPE_SIZE
, ptr
);
839 if (method
!= CHECK_PARITY
) {
840 set_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
841 set_bit(R5_LOCKED
, &sh
->dev
[pd_idx
].flags
);
843 clear_bit(R5_UPTODATE
, &sh
->dev
[pd_idx
].flags
);
847 * Each stripe/dev can have one or more bion attached.
848 * toread/towrite point to the first in a chain.
849 * The bi_next chain must be in order.
851 static int add_stripe_bio(struct stripe_head
*sh
, struct bio
*bi
, int dd_idx
, int forwrite
)
854 raid5_conf_t
*conf
= sh
->raid_conf
;
857 PRINTK("adding bh b#%llu to stripe s#%llu\n",
858 (unsigned long long)bi
->bi_sector
,
859 (unsigned long long)sh
->sector
);
862 spin_lock(&sh
->lock
);
863 spin_lock_irq(&conf
->device_lock
);
865 bip
= &sh
->dev
[dd_idx
].towrite
;
866 if (*bip
== NULL
&& sh
->dev
[dd_idx
].written
== NULL
)
869 bip
= &sh
->dev
[dd_idx
].toread
;
870 while (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
) {
871 if ((*bip
)->bi_sector
+ ((*bip
)->bi_size
>> 9) > bi
->bi_sector
)
873 bip
= & (*bip
)->bi_next
;
875 if (*bip
&& (*bip
)->bi_sector
< bi
->bi_sector
+ ((bi
->bi_size
)>>9))
878 if (*bip
&& bi
->bi_next
&& (*bip
) != bi
->bi_next
)
883 bi
->bi_phys_segments
++;
884 spin_unlock_irq(&conf
->device_lock
);
885 spin_unlock(&sh
->lock
);
887 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
888 (unsigned long long)bi
->bi_sector
,
889 (unsigned long long)sh
->sector
, dd_idx
);
891 if (conf
->mddev
->bitmap
&& firstwrite
) {
892 sh
->bm_seq
= conf
->seq_write
;
893 bitmap_startwrite(conf
->mddev
->bitmap
, sh
->sector
,
895 set_bit(STRIPE_BIT_DELAY
, &sh
->state
);
899 /* check if page is covered */
900 sector_t sector
= sh
->dev
[dd_idx
].sector
;
901 for (bi
=sh
->dev
[dd_idx
].towrite
;
902 sector
< sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
&&
903 bi
&& bi
->bi_sector
<= sector
;
904 bi
= r5_next_bio(bi
, sh
->dev
[dd_idx
].sector
)) {
905 if (bi
->bi_sector
+ (bi
->bi_size
>>9) >= sector
)
906 sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
908 if (sector
>= sh
->dev
[dd_idx
].sector
+ STRIPE_SECTORS
)
909 set_bit(R5_OVERWRITE
, &sh
->dev
[dd_idx
].flags
);
914 set_bit(R5_Overlap
, &sh
->dev
[dd_idx
].flags
);
915 spin_unlock_irq(&conf
->device_lock
);
916 spin_unlock(&sh
->lock
);
922 * handle_stripe - do things to a stripe.
924 * We lock the stripe and then examine the state of various bits
925 * to see what needs to be done.
927 * return some read request which now have data
928 * return some write requests which are safely on disc
929 * schedule a read on some buffers
930 * schedule a write of some buffers
931 * return confirmation of parity correctness
933 * Parity calculations are done inside the stripe lock
934 * buffers are taken off read_list or write_list, and bh_cache buffers
935 * get BH_Lock set before the stripe lock is released.
939 static void handle_stripe(struct stripe_head
*sh
)
941 raid5_conf_t
*conf
= sh
->raid_conf
;
942 int disks
= conf
->raid_disks
;
943 struct bio
*return_bi
= NULL
;
947 int locked
=0, uptodate
=0, to_read
=0, to_write
=0, failed
=0, written
=0;
948 int non_overwrite
= 0;
952 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
953 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
),
956 spin_lock(&sh
->lock
);
957 clear_bit(STRIPE_HANDLE
, &sh
->state
);
958 clear_bit(STRIPE_DELAYED
, &sh
->state
);
960 syncing
= test_bit(STRIPE_SYNCING
, &sh
->state
);
961 /* Now to look around and see what can be done */
963 for (i
=disks
; i
--; ) {
966 clear_bit(R5_Insync
, &dev
->flags
);
967 clear_bit(R5_Syncio
, &dev
->flags
);
969 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
970 i
, dev
->flags
, dev
->toread
, dev
->towrite
, dev
->written
);
971 /* maybe we can reply to a read */
972 if (test_bit(R5_UPTODATE
, &dev
->flags
) && dev
->toread
) {
973 struct bio
*rbi
, *rbi2
;
974 PRINTK("Return read for disc %d\n", i
);
975 spin_lock_irq(&conf
->device_lock
);
978 if (test_and_clear_bit(R5_Overlap
, &dev
->flags
))
979 wake_up(&conf
->wait_for_overlap
);
980 spin_unlock_irq(&conf
->device_lock
);
981 while (rbi
&& rbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
982 copy_data(0, rbi
, dev
->page
, dev
->sector
);
983 rbi2
= r5_next_bio(rbi
, dev
->sector
);
984 spin_lock_irq(&conf
->device_lock
);
985 if (--rbi
->bi_phys_segments
== 0) {
986 rbi
->bi_next
= return_bi
;
989 spin_unlock_irq(&conf
->device_lock
);
994 /* now count some things */
995 if (test_bit(R5_LOCKED
, &dev
->flags
)) locked
++;
996 if (test_bit(R5_UPTODATE
, &dev
->flags
)) uptodate
++;
999 if (dev
->toread
) to_read
++;
1002 if (!test_bit(R5_OVERWRITE
, &dev
->flags
))
1005 if (dev
->written
) written
++;
1006 rdev
= conf
->disks
[i
].rdev
; /* FIXME, should I be looking rdev */
1007 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)) {
1008 /* The ReadError flag wil just be confusing now */
1009 clear_bit(R5_ReadError
, &dev
->flags
);
1010 clear_bit(R5_ReWrite
, &dev
->flags
);
1012 if (!rdev
|| !test_bit(In_sync
, &rdev
->flags
)
1013 || test_bit(R5_ReadError
, &dev
->flags
)) {
1017 set_bit(R5_Insync
, &dev
->flags
);
1019 PRINTK("locked=%d uptodate=%d to_read=%d"
1020 " to_write=%d failed=%d failed_num=%d\n",
1021 locked
, uptodate
, to_read
, to_write
, failed
, failed_num
);
1022 /* check if the array has lost two devices and, if so, some requests might
1025 if (failed
> 1 && to_read
+to_write
+written
) {
1026 for (i
=disks
; i
--; ) {
1029 if (test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1030 mdk_rdev_t
*rdev
= conf
->disks
[i
].rdev
;
1031 if (rdev
&& test_bit(In_sync
, &rdev
->flags
))
1032 /* multiple read failures in one stripe */
1033 md_error(conf
->mddev
, rdev
);
1036 spin_lock_irq(&conf
->device_lock
);
1037 /* fail all writes first */
1038 bi
= sh
->dev
[i
].towrite
;
1039 sh
->dev
[i
].towrite
= NULL
;
1040 if (bi
) { to_write
--; bitmap_end
= 1; }
1042 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1043 wake_up(&conf
->wait_for_overlap
);
1045 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1046 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1047 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1048 if (--bi
->bi_phys_segments
== 0) {
1049 md_write_end(conf
->mddev
);
1050 bi
->bi_next
= return_bi
;
1055 /* and fail all 'written' */
1056 bi
= sh
->dev
[i
].written
;
1057 sh
->dev
[i
].written
= NULL
;
1058 if (bi
) bitmap_end
= 1;
1059 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
) {
1060 struct bio
*bi2
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1061 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1062 if (--bi
->bi_phys_segments
== 0) {
1063 md_write_end(conf
->mddev
);
1064 bi
->bi_next
= return_bi
;
1070 /* fail any reads if this device is non-operational */
1071 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
) ||
1072 test_bit(R5_ReadError
, &sh
->dev
[i
].flags
)) {
1073 bi
= sh
->dev
[i
].toread
;
1074 sh
->dev
[i
].toread
= NULL
;
1075 if (test_and_clear_bit(R5_Overlap
, &sh
->dev
[i
].flags
))
1076 wake_up(&conf
->wait_for_overlap
);
1078 while (bi
&& bi
->bi_sector
< sh
->dev
[i
].sector
+ STRIPE_SECTORS
){
1079 struct bio
*nextbi
= r5_next_bio(bi
, sh
->dev
[i
].sector
);
1080 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1081 if (--bi
->bi_phys_segments
== 0) {
1082 bi
->bi_next
= return_bi
;
1088 spin_unlock_irq(&conf
->device_lock
);
1090 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1091 STRIPE_SECTORS
, 0, 0);
1094 if (failed
> 1 && syncing
) {
1095 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,0);
1096 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1100 /* might be able to return some write requests if the parity block
1101 * is safe, or on a failed drive
1103 dev
= &sh
->dev
[sh
->pd_idx
];
1105 ( (test_bit(R5_Insync
, &dev
->flags
) && !test_bit(R5_LOCKED
, &dev
->flags
) &&
1106 test_bit(R5_UPTODATE
, &dev
->flags
))
1107 || (failed
== 1 && failed_num
== sh
->pd_idx
))
1109 /* any written block on an uptodate or failed drive can be returned.
1110 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1111 * never LOCKED, so we don't need to test 'failed' directly.
1113 for (i
=disks
; i
--; )
1114 if (sh
->dev
[i
].written
) {
1116 if (!test_bit(R5_LOCKED
, &dev
->flags
) &&
1117 test_bit(R5_UPTODATE
, &dev
->flags
) ) {
1118 /* We can return any write requests */
1119 struct bio
*wbi
, *wbi2
;
1121 PRINTK("Return write for disc %d\n", i
);
1122 spin_lock_irq(&conf
->device_lock
);
1124 dev
->written
= NULL
;
1125 while (wbi
&& wbi
->bi_sector
< dev
->sector
+ STRIPE_SECTORS
) {
1126 wbi2
= r5_next_bio(wbi
, dev
->sector
);
1127 if (--wbi
->bi_phys_segments
== 0) {
1128 md_write_end(conf
->mddev
);
1129 wbi
->bi_next
= return_bi
;
1134 if (dev
->towrite
== NULL
)
1136 spin_unlock_irq(&conf
->device_lock
);
1138 bitmap_endwrite(conf
->mddev
->bitmap
, sh
->sector
,
1140 !test_bit(STRIPE_DEGRADED
, &sh
->state
), 0);
1145 /* Now we might consider reading some blocks, either to check/generate
1146 * parity, or to satisfy requests
1147 * or to load a block that is being partially written.
1149 if (to_read
|| non_overwrite
|| (syncing
&& (uptodate
< disks
))) {
1150 for (i
=disks
; i
--;) {
1152 if (!test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1154 (dev
->towrite
&& !test_bit(R5_OVERWRITE
, &dev
->flags
)) ||
1156 (failed
&& (sh
->dev
[failed_num
].toread
||
1157 (sh
->dev
[failed_num
].towrite
&& !test_bit(R5_OVERWRITE
, &sh
->dev
[failed_num
].flags
))))
1160 /* we would like to get this block, possibly
1161 * by computing it, but we might not be able to
1163 if (uptodate
== disks
-1) {
1164 PRINTK("Computing block %d\n", i
);
1165 compute_block(sh
, i
);
1167 } else if (test_bit(R5_Insync
, &dev
->flags
)) {
1168 set_bit(R5_LOCKED
, &dev
->flags
);
1169 set_bit(R5_Wantread
, &dev
->flags
);
1171 /* if I am just reading this block and we don't have
1172 a failed drive, or any pending writes then sidestep the cache */
1173 if (sh
->bh_read
[i
] && !sh
->bh_read
[i
]->b_reqnext
&&
1174 ! syncing
&& !failed
&& !to_write
) {
1175 sh
->bh_cache
[i
]->b_page
= sh
->bh_read
[i
]->b_page
;
1176 sh
->bh_cache
[i
]->b_data
= sh
->bh_read
[i
]->b_data
;
1180 PRINTK("Reading block %d (sync=%d)\n",
1183 md_sync_acct(conf
->disks
[i
].rdev
->bdev
,
1188 set_bit(STRIPE_HANDLE
, &sh
->state
);
1191 /* now to consider writing and what else, if anything should be read */
1194 for (i
=disks
; i
--;) {
1195 /* would I have to read this buffer for read_modify_write */
1197 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
1198 (!test_bit(R5_LOCKED
, &dev
->flags
)
1200 || sh
->bh_page
[i
]!=bh
->b_page
1203 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1204 if (test_bit(R5_Insync
, &dev
->flags
)
1205 /* && !(!mddev->insync && i == sh->pd_idx) */
1208 else rmw
+= 2*disks
; /* cannot read it */
1210 /* Would I have to read this buffer for reconstruct_write */
1211 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
1212 (!test_bit(R5_LOCKED
, &dev
->flags
)
1214 || sh
->bh_page
[i
] != bh
->b_page
1217 !test_bit(R5_UPTODATE
, &dev
->flags
)) {
1218 if (test_bit(R5_Insync
, &dev
->flags
)) rcw
++;
1219 else rcw
+= 2*disks
;
1222 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1223 (unsigned long long)sh
->sector
, rmw
, rcw
);
1224 set_bit(STRIPE_HANDLE
, &sh
->state
);
1225 if (rmw
< rcw
&& rmw
> 0)
1226 /* prefer read-modify-write, but need to get some data */
1227 for (i
=disks
; i
--;) {
1229 if ((dev
->towrite
|| i
== sh
->pd_idx
) &&
1230 !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1231 test_bit(R5_Insync
, &dev
->flags
)) {
1232 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1234 PRINTK("Read_old block %d for r-m-w\n", i
);
1235 set_bit(R5_LOCKED
, &dev
->flags
);
1236 set_bit(R5_Wantread
, &dev
->flags
);
1239 set_bit(STRIPE_DELAYED
, &sh
->state
);
1240 set_bit(STRIPE_HANDLE
, &sh
->state
);
1244 if (rcw
<= rmw
&& rcw
> 0)
1245 /* want reconstruct write, but need to get some data */
1246 for (i
=disks
; i
--;) {
1248 if (!test_bit(R5_OVERWRITE
, &dev
->flags
) && i
!= sh
->pd_idx
&&
1249 !test_bit(R5_LOCKED
, &dev
->flags
) && !test_bit(R5_UPTODATE
, &dev
->flags
) &&
1250 test_bit(R5_Insync
, &dev
->flags
)) {
1251 if (test_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1253 PRINTK("Read_old block %d for Reconstruct\n", i
);
1254 set_bit(R5_LOCKED
, &dev
->flags
);
1255 set_bit(R5_Wantread
, &dev
->flags
);
1258 set_bit(STRIPE_DELAYED
, &sh
->state
);
1259 set_bit(STRIPE_HANDLE
, &sh
->state
);
1263 /* now if nothing is locked, and if we have enough data, we can start a write request */
1264 if (locked
== 0 && (rcw
== 0 ||rmw
== 0) &&
1265 !test_bit(STRIPE_BIT_DELAY
, &sh
->state
)) {
1266 PRINTK("Computing parity...\n");
1267 compute_parity(sh
, rcw
==0 ? RECONSTRUCT_WRITE
: READ_MODIFY_WRITE
);
1268 /* now every locked buffer is ready to be written */
1270 if (test_bit(R5_LOCKED
, &sh
->dev
[i
].flags
)) {
1271 PRINTK("Writing block %d\n", i
);
1273 set_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
);
1274 if (!test_bit(R5_Insync
, &sh
->dev
[i
].flags
)
1275 || (i
==sh
->pd_idx
&& failed
== 0))
1276 set_bit(STRIPE_INSYNC
, &sh
->state
);
1278 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
)) {
1279 atomic_dec(&conf
->preread_active_stripes
);
1280 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
)
1281 md_wakeup_thread(conf
->mddev
->thread
);
1286 /* maybe we need to check and possibly fix the parity for this stripe
1287 * Any reads will already have been scheduled, so we just see if enough data
1290 if (syncing
&& locked
== 0 &&
1291 !test_bit(STRIPE_INSYNC
, &sh
->state
) && failed
<= 1) {
1292 set_bit(STRIPE_HANDLE
, &sh
->state
);
1295 if (uptodate
!= disks
)
1297 compute_parity(sh
, CHECK_PARITY
);
1299 pagea
= page_address(sh
->dev
[sh
->pd_idx
].page
);
1300 if ((*(u32
*)pagea
) == 0 &&
1301 !memcmp(pagea
, pagea
+4, STRIPE_SIZE
-4)) {
1302 /* parity is correct (on disc, not in buffer any more) */
1303 set_bit(STRIPE_INSYNC
, &sh
->state
);
1305 conf
->mddev
->resync_mismatches
+= STRIPE_SECTORS
;
1306 if (test_bit(MD_RECOVERY_CHECK
, &conf
->mddev
->recovery
))
1307 /* don't try to repair!! */
1308 set_bit(STRIPE_INSYNC
, &sh
->state
);
1311 if (!test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1313 failed_num
= sh
->pd_idx
;
1314 /* should be able to compute the missing block and write it to spare */
1315 if (!test_bit(R5_UPTODATE
, &sh
->dev
[failed_num
].flags
)) {
1316 if (uptodate
+1 != disks
)
1318 compute_block(sh
, failed_num
);
1321 if (uptodate
!= disks
)
1323 dev
= &sh
->dev
[failed_num
];
1324 set_bit(R5_LOCKED
, &dev
->flags
);
1325 set_bit(R5_Wantwrite
, &dev
->flags
);
1326 clear_bit(STRIPE_DEGRADED
, &sh
->state
);
1328 set_bit(STRIPE_INSYNC
, &sh
->state
);
1329 set_bit(R5_Syncio
, &dev
->flags
);
1332 if (syncing
&& locked
== 0 && test_bit(STRIPE_INSYNC
, &sh
->state
)) {
1333 md_done_sync(conf
->mddev
, STRIPE_SECTORS
,1);
1334 clear_bit(STRIPE_SYNCING
, &sh
->state
);
1337 /* If the failed drive is just a ReadError, then we might need to progress
1338 * the repair/check process
1340 if (failed
== 1 && ! conf
->mddev
->ro
&&
1341 test_bit(R5_ReadError
, &sh
->dev
[failed_num
].flags
)
1342 && !test_bit(R5_LOCKED
, &sh
->dev
[failed_num
].flags
)
1343 && test_bit(R5_UPTODATE
, &sh
->dev
[failed_num
].flags
)
1345 dev
= &sh
->dev
[failed_num
];
1346 if (!test_bit(R5_ReWrite
, &dev
->flags
)) {
1347 set_bit(R5_Wantwrite
, &dev
->flags
);
1348 set_bit(R5_ReWrite
, &dev
->flags
);
1349 set_bit(R5_LOCKED
, &dev
->flags
);
1351 /* let's read it back */
1352 set_bit(R5_Wantread
, &dev
->flags
);
1353 set_bit(R5_LOCKED
, &dev
->flags
);
1357 spin_unlock(&sh
->lock
);
1359 while ((bi
=return_bi
)) {
1360 int bytes
= bi
->bi_size
;
1362 return_bi
= bi
->bi_next
;
1365 bi
->bi_end_io(bi
, bytes
, 0);
1367 for (i
=disks
; i
-- ;) {
1371 if (test_and_clear_bit(R5_Wantwrite
, &sh
->dev
[i
].flags
))
1373 else if (test_and_clear_bit(R5_Wantread
, &sh
->dev
[i
].flags
))
1378 bi
= &sh
->dev
[i
].req
;
1382 bi
->bi_end_io
= raid5_end_write_request
;
1384 bi
->bi_end_io
= raid5_end_read_request
;
1387 rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1388 if (rdev
&& test_bit(Faulty
, &rdev
->flags
))
1391 atomic_inc(&rdev
->nr_pending
);
1395 if (test_bit(R5_Syncio
, &sh
->dev
[i
].flags
))
1396 md_sync_acct(rdev
->bdev
, STRIPE_SECTORS
);
1398 bi
->bi_bdev
= rdev
->bdev
;
1399 PRINTK("for %llu schedule op %ld on disc %d\n",
1400 (unsigned long long)sh
->sector
, bi
->bi_rw
, i
);
1401 atomic_inc(&sh
->count
);
1402 bi
->bi_sector
= sh
->sector
+ rdev
->data_offset
;
1403 bi
->bi_flags
= 1 << BIO_UPTODATE
;
1405 bi
->bi_max_vecs
= 1;
1407 bi
->bi_io_vec
= &sh
->dev
[i
].vec
;
1408 bi
->bi_io_vec
[0].bv_len
= STRIPE_SIZE
;
1409 bi
->bi_io_vec
[0].bv_offset
= 0;
1410 bi
->bi_size
= STRIPE_SIZE
;
1412 generic_make_request(bi
);
1415 set_bit(STRIPE_DEGRADED
, &sh
->state
);
1416 PRINTK("skip op %ld on disc %d for sector %llu\n",
1417 bi
->bi_rw
, i
, (unsigned long long)sh
->sector
);
1418 clear_bit(R5_LOCKED
, &sh
->dev
[i
].flags
);
1419 set_bit(STRIPE_HANDLE
, &sh
->state
);
1424 static inline void raid5_activate_delayed(raid5_conf_t
*conf
)
1426 if (atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
) {
1427 while (!list_empty(&conf
->delayed_list
)) {
1428 struct list_head
*l
= conf
->delayed_list
.next
;
1429 struct stripe_head
*sh
;
1430 sh
= list_entry(l
, struct stripe_head
, lru
);
1432 clear_bit(STRIPE_DELAYED
, &sh
->state
);
1433 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE
, &sh
->state
))
1434 atomic_inc(&conf
->preread_active_stripes
);
1435 list_add_tail(&sh
->lru
, &conf
->handle_list
);
1440 static inline void activate_bit_delay(raid5_conf_t
*conf
)
1442 /* device_lock is held */
1443 struct list_head head
;
1444 list_add(&head
, &conf
->bitmap_list
);
1445 list_del_init(&conf
->bitmap_list
);
1446 while (!list_empty(&head
)) {
1447 struct stripe_head
*sh
= list_entry(head
.next
, struct stripe_head
, lru
);
1448 list_del_init(&sh
->lru
);
1449 atomic_inc(&sh
->count
);
1450 __release_stripe(conf
, sh
);
1454 static void unplug_slaves(mddev_t
*mddev
)
1456 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1460 for (i
=0; i
<mddev
->raid_disks
; i
++) {
1461 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1462 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
) && atomic_read(&rdev
->nr_pending
)) {
1463 request_queue_t
*r_queue
= bdev_get_queue(rdev
->bdev
);
1465 atomic_inc(&rdev
->nr_pending
);
1468 if (r_queue
->unplug_fn
)
1469 r_queue
->unplug_fn(r_queue
);
1471 rdev_dec_pending(rdev
, mddev
);
1478 static void raid5_unplug_device(request_queue_t
*q
)
1480 mddev_t
*mddev
= q
->queuedata
;
1481 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1482 unsigned long flags
;
1484 spin_lock_irqsave(&conf
->device_lock
, flags
);
1486 if (blk_remove_plug(q
)) {
1488 raid5_activate_delayed(conf
);
1490 md_wakeup_thread(mddev
->thread
);
1492 spin_unlock_irqrestore(&conf
->device_lock
, flags
);
1494 unplug_slaves(mddev
);
1497 static int raid5_issue_flush(request_queue_t
*q
, struct gendisk
*disk
,
1498 sector_t
*error_sector
)
1500 mddev_t
*mddev
= q
->queuedata
;
1501 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1505 for (i
=0; i
<mddev
->raid_disks
&& ret
== 0; i
++) {
1506 mdk_rdev_t
*rdev
= rcu_dereference(conf
->disks
[i
].rdev
);
1507 if (rdev
&& !test_bit(Faulty
, &rdev
->flags
)) {
1508 struct block_device
*bdev
= rdev
->bdev
;
1509 request_queue_t
*r_queue
= bdev_get_queue(bdev
);
1511 if (!r_queue
->issue_flush_fn
)
1514 atomic_inc(&rdev
->nr_pending
);
1516 ret
= r_queue
->issue_flush_fn(r_queue
, bdev
->bd_disk
,
1518 rdev_dec_pending(rdev
, mddev
);
1527 static inline void raid5_plug_device(raid5_conf_t
*conf
)
1529 spin_lock_irq(&conf
->device_lock
);
1530 blk_plug_device(conf
->mddev
->queue
);
1531 spin_unlock_irq(&conf
->device_lock
);
1534 static int make_request (request_queue_t
*q
, struct bio
* bi
)
1536 mddev_t
*mddev
= q
->queuedata
;
1537 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1538 const unsigned int raid_disks
= conf
->raid_disks
;
1539 const unsigned int data_disks
= raid_disks
- 1;
1540 unsigned int dd_idx
, pd_idx
;
1541 sector_t new_sector
;
1542 sector_t logical_sector
, last_sector
;
1543 struct stripe_head
*sh
;
1544 const int rw
= bio_data_dir(bi
);
1546 if (unlikely(bio_barrier(bi
))) {
1547 bio_endio(bi
, bi
->bi_size
, -EOPNOTSUPP
);
1551 md_write_start(mddev
, bi
);
1553 disk_stat_inc(mddev
->gendisk
, ios
[rw
]);
1554 disk_stat_add(mddev
->gendisk
, sectors
[rw
], bio_sectors(bi
));
1556 logical_sector
= bi
->bi_sector
& ~((sector_t
)STRIPE_SECTORS
-1);
1557 last_sector
= bi
->bi_sector
+ (bi
->bi_size
>>9);
1559 bi
->bi_phys_segments
= 1; /* over-loaded to count active stripes */
1561 for (;logical_sector
< last_sector
; logical_sector
+= STRIPE_SECTORS
) {
1564 new_sector
= raid5_compute_sector(logical_sector
,
1565 raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1567 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1568 (unsigned long long)new_sector
,
1569 (unsigned long long)logical_sector
);
1572 prepare_to_wait(&conf
->wait_for_overlap
, &w
, TASK_UNINTERRUPTIBLE
);
1573 sh
= get_active_stripe(conf
, new_sector
, pd_idx
, (bi
->bi_rw
&RWA_MASK
));
1575 if (!add_stripe_bio(sh
, bi
, dd_idx
, (bi
->bi_rw
&RW_MASK
))) {
1576 /* Add failed due to overlap. Flush everything
1579 raid5_unplug_device(mddev
->queue
);
1584 finish_wait(&conf
->wait_for_overlap
, &w
);
1585 raid5_plug_device(conf
);
1590 /* cannot get stripe for read-ahead, just give-up */
1591 clear_bit(BIO_UPTODATE
, &bi
->bi_flags
);
1592 finish_wait(&conf
->wait_for_overlap
, &w
);
1597 spin_lock_irq(&conf
->device_lock
);
1598 if (--bi
->bi_phys_segments
== 0) {
1599 int bytes
= bi
->bi_size
;
1601 if ( bio_data_dir(bi
) == WRITE
)
1602 md_write_end(mddev
);
1604 bi
->bi_end_io(bi
, bytes
, 0);
1606 spin_unlock_irq(&conf
->device_lock
);
1610 /* FIXME go_faster isn't used */
1611 static sector_t
sync_request(mddev_t
*mddev
, sector_t sector_nr
, int *skipped
, int go_faster
)
1613 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
1614 struct stripe_head
*sh
;
1615 int sectors_per_chunk
= conf
->chunk_size
>> 9;
1617 unsigned long stripe
;
1620 sector_t first_sector
;
1621 int raid_disks
= conf
->raid_disks
;
1622 int data_disks
= raid_disks
-1;
1623 sector_t max_sector
= mddev
->size
<< 1;
1626 if (sector_nr
>= max_sector
) {
1627 /* just being told to finish up .. nothing much to do */
1628 unplug_slaves(mddev
);
1630 if (mddev
->curr_resync
< max_sector
) /* aborted */
1631 bitmap_end_sync(mddev
->bitmap
, mddev
->curr_resync
,
1633 else /* compelted sync */
1635 bitmap_close_sync(mddev
->bitmap
);
1639 /* if there is 1 or more failed drives and we are trying
1640 * to resync, then assert that we are finished, because there is
1641 * nothing we can do.
1643 if (mddev
->degraded
>= 1 && test_bit(MD_RECOVERY_SYNC
, &mddev
->recovery
)) {
1644 sector_t rv
= (mddev
->size
<< 1) - sector_nr
;
1648 if (!bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 1) &&
1649 !test_bit(MD_RECOVERY_REQUESTED
, &mddev
->recovery
) &&
1650 !conf
->fullsync
&& sync_blocks
>= STRIPE_SECTORS
) {
1651 /* we can skip this block, and probably more */
1652 sync_blocks
/= STRIPE_SECTORS
;
1654 return sync_blocks
* STRIPE_SECTORS
; /* keep things rounded to whole stripes */
1658 chunk_offset
= sector_div(x
, sectors_per_chunk
);
1660 BUG_ON(x
!= stripe
);
1662 first_sector
= raid5_compute_sector((sector_t
)stripe
*data_disks
*sectors_per_chunk
1663 + chunk_offset
, raid_disks
, data_disks
, &dd_idx
, &pd_idx
, conf
);
1664 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 1);
1666 sh
= get_active_stripe(conf
, sector_nr
, pd_idx
, 0);
1667 /* make sure we don't swamp the stripe cache if someone else
1668 * is trying to get access
1670 schedule_timeout_uninterruptible(1);
1672 bitmap_start_sync(mddev
->bitmap
, sector_nr
, &sync_blocks
, 0);
1673 spin_lock(&sh
->lock
);
1674 set_bit(STRIPE_SYNCING
, &sh
->state
);
1675 clear_bit(STRIPE_INSYNC
, &sh
->state
);
1676 spin_unlock(&sh
->lock
);
1681 return STRIPE_SECTORS
;
1685 * This is our raid5 kernel thread.
1687 * We scan the hash table for stripes which can be handled now.
1688 * During the scan, completed stripes are saved for us by the interrupt
1689 * handler, so that they will not have to wait for our next wakeup.
1691 static void raid5d (mddev_t
*mddev
)
1693 struct stripe_head
*sh
;
1694 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1697 PRINTK("+++ raid5d active\n");
1699 md_check_recovery(mddev
);
1702 spin_lock_irq(&conf
->device_lock
);
1704 struct list_head
*first
;
1706 if (conf
->seq_flush
- conf
->seq_write
> 0) {
1707 int seq
= conf
->seq_flush
;
1708 spin_unlock_irq(&conf
->device_lock
);
1709 bitmap_unplug(mddev
->bitmap
);
1710 spin_lock_irq(&conf
->device_lock
);
1711 conf
->seq_write
= seq
;
1712 activate_bit_delay(conf
);
1715 if (list_empty(&conf
->handle_list
) &&
1716 atomic_read(&conf
->preread_active_stripes
) < IO_THRESHOLD
&&
1717 !blk_queue_plugged(mddev
->queue
) &&
1718 !list_empty(&conf
->delayed_list
))
1719 raid5_activate_delayed(conf
);
1721 if (list_empty(&conf
->handle_list
))
1724 first
= conf
->handle_list
.next
;
1725 sh
= list_entry(first
, struct stripe_head
, lru
);
1727 list_del_init(first
);
1728 atomic_inc(&sh
->count
);
1729 if (atomic_read(&sh
->count
)!= 1)
1731 spin_unlock_irq(&conf
->device_lock
);
1737 spin_lock_irq(&conf
->device_lock
);
1739 PRINTK("%d stripes handled\n", handled
);
1741 spin_unlock_irq(&conf
->device_lock
);
1743 unplug_slaves(mddev
);
1745 PRINTK("--- raid5d inactive\n");
1749 raid5_show_stripe_cache_size(mddev_t
*mddev
, char *page
)
1751 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1753 return sprintf(page
, "%d\n", conf
->max_nr_stripes
);
1759 raid5_store_stripe_cache_size(mddev_t
*mddev
, const char *page
, size_t len
)
1761 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1764 if (len
>= PAGE_SIZE
)
1769 new = simple_strtoul(page
, &end
, 10);
1770 if (!*page
|| (*end
&& *end
!= '\n') )
1772 if (new <= 16 || new > 32768)
1774 while (new < conf
->max_nr_stripes
) {
1775 if (drop_one_stripe(conf
))
1776 conf
->max_nr_stripes
--;
1780 while (new > conf
->max_nr_stripes
) {
1781 if (grow_one_stripe(conf
))
1782 conf
->max_nr_stripes
++;
1788 static struct md_sysfs_entry
1789 raid5_stripecache_size
= __ATTR(stripe_cache_size
, S_IRUGO
| S_IWUSR
,
1790 raid5_show_stripe_cache_size
,
1791 raid5_store_stripe_cache_size
);
1794 stripe_cache_active_show(mddev_t
*mddev
, char *page
)
1796 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
1798 return sprintf(page
, "%d\n", atomic_read(&conf
->active_stripes
));
1803 static struct md_sysfs_entry
1804 raid5_stripecache_active
= __ATTR_RO(stripe_cache_active
);
1806 static struct attribute
*raid5_attrs
[] = {
1807 &raid5_stripecache_size
.attr
,
1808 &raid5_stripecache_active
.attr
,
1811 static struct attribute_group raid5_attrs_group
= {
1813 .attrs
= raid5_attrs
,
1816 static int run(mddev_t
*mddev
)
1819 int raid_disk
, memory
;
1821 struct disk_info
*disk
;
1822 struct list_head
*tmp
;
1824 if (mddev
->level
!= 5 && mddev
->level
!= 4) {
1825 printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev
), mddev
->level
);
1829 mddev
->private = kmalloc (sizeof (raid5_conf_t
)
1830 + mddev
->raid_disks
* sizeof(struct disk_info
),
1832 if ((conf
= mddev
->private) == NULL
)
1834 memset (conf
, 0, sizeof (*conf
) + mddev
->raid_disks
* sizeof(struct disk_info
) );
1835 conf
->mddev
= mddev
;
1837 if ((conf
->stripe_hashtbl
= (struct stripe_head
**) __get_free_pages(GFP_ATOMIC
, HASH_PAGES_ORDER
)) == NULL
)
1839 memset(conf
->stripe_hashtbl
, 0, HASH_PAGES
* PAGE_SIZE
);
1841 spin_lock_init(&conf
->device_lock
);
1842 init_waitqueue_head(&conf
->wait_for_stripe
);
1843 init_waitqueue_head(&conf
->wait_for_overlap
);
1844 INIT_LIST_HEAD(&conf
->handle_list
);
1845 INIT_LIST_HEAD(&conf
->delayed_list
);
1846 INIT_LIST_HEAD(&conf
->bitmap_list
);
1847 INIT_LIST_HEAD(&conf
->inactive_list
);
1848 atomic_set(&conf
->active_stripes
, 0);
1849 atomic_set(&conf
->preread_active_stripes
, 0);
1851 PRINTK("raid5: run(%s) called.\n", mdname(mddev
));
1853 ITERATE_RDEV(mddev
,rdev
,tmp
) {
1854 raid_disk
= rdev
->raid_disk
;
1855 if (raid_disk
>= mddev
->raid_disks
1858 disk
= conf
->disks
+ raid_disk
;
1862 if (test_bit(In_sync
, &rdev
->flags
)) {
1863 char b
[BDEVNAME_SIZE
];
1864 printk(KERN_INFO
"raid5: device %s operational as raid"
1865 " disk %d\n", bdevname(rdev
->bdev
,b
),
1867 conf
->working_disks
++;
1871 conf
->raid_disks
= mddev
->raid_disks
;
1873 * 0 for a fully functional array, 1 for a degraded array.
1875 mddev
->degraded
= conf
->failed_disks
= conf
->raid_disks
- conf
->working_disks
;
1876 conf
->mddev
= mddev
;
1877 conf
->chunk_size
= mddev
->chunk_size
;
1878 conf
->level
= mddev
->level
;
1879 conf
->algorithm
= mddev
->layout
;
1880 conf
->max_nr_stripes
= NR_STRIPES
;
1882 /* device size must be a multiple of chunk size */
1883 mddev
->size
&= ~(mddev
->chunk_size
/1024 -1);
1884 mddev
->resync_max_sectors
= mddev
->size
<< 1;
1886 if (!conf
->chunk_size
|| conf
->chunk_size
% 4) {
1887 printk(KERN_ERR
"raid5: invalid chunk size %d for %s\n",
1888 conf
->chunk_size
, mdname(mddev
));
1891 if (conf
->algorithm
> ALGORITHM_RIGHT_SYMMETRIC
) {
1893 "raid5: unsupported parity algorithm %d for %s\n",
1894 conf
->algorithm
, mdname(mddev
));
1897 if (mddev
->degraded
> 1) {
1898 printk(KERN_ERR
"raid5: not enough operational devices for %s"
1899 " (%d/%d failed)\n",
1900 mdname(mddev
), conf
->failed_disks
, conf
->raid_disks
);
1904 if (mddev
->degraded
== 1 &&
1905 mddev
->recovery_cp
!= MaxSector
) {
1907 "raid5: cannot start dirty degraded array for %s\n",
1913 mddev
->thread
= md_register_thread(raid5d
, mddev
, "%s_raid5");
1914 if (!mddev
->thread
) {
1916 "raid5: couldn't allocate thread for %s\n",
1921 memory
= conf
->max_nr_stripes
* (sizeof(struct stripe_head
) +
1922 conf
->raid_disks
* ((sizeof(struct bio
) + PAGE_SIZE
))) / 1024;
1923 if (grow_stripes(conf
, conf
->max_nr_stripes
)) {
1925 "raid5: couldn't allocate %dkB for buffers\n", memory
);
1926 shrink_stripes(conf
);
1927 md_unregister_thread(mddev
->thread
);
1930 printk(KERN_INFO
"raid5: allocated %dkB for %s\n",
1931 memory
, mdname(mddev
));
1933 if (mddev
->degraded
== 0)
1934 printk("raid5: raid level %d set %s active with %d out of %d"
1935 " devices, algorithm %d\n", conf
->level
, mdname(mddev
),
1936 mddev
->raid_disks
-mddev
->degraded
, mddev
->raid_disks
,
1939 printk(KERN_ALERT
"raid5: raid level %d set %s active with %d"
1940 " out of %d devices, algorithm %d\n", conf
->level
,
1941 mdname(mddev
), mddev
->raid_disks
- mddev
->degraded
,
1942 mddev
->raid_disks
, conf
->algorithm
);
1944 print_raid5_conf(conf
);
1946 /* read-ahead size must cover two whole stripes, which is
1947 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
1950 int stripe
= (mddev
->raid_disks
-1) * mddev
->chunk_size
1952 if (mddev
->queue
->backing_dev_info
.ra_pages
< 2 * stripe
)
1953 mddev
->queue
->backing_dev_info
.ra_pages
= 2 * stripe
;
1956 /* Ok, everything is just fine now */
1957 sysfs_create_group(&mddev
->kobj
, &raid5_attrs_group
);
1960 mddev
->thread
->timeout
= mddev
->bitmap
->daemon_sleep
* HZ
;
1962 mddev
->queue
->unplug_fn
= raid5_unplug_device
;
1963 mddev
->queue
->issue_flush_fn
= raid5_issue_flush
;
1965 mddev
->array_size
= mddev
->size
* (mddev
->raid_disks
- 1);
1969 print_raid5_conf(conf
);
1970 if (conf
->stripe_hashtbl
)
1971 free_pages((unsigned long) conf
->stripe_hashtbl
,
1975 mddev
->private = NULL
;
1976 printk(KERN_ALERT
"raid5: failed to run raid set %s\n", mdname(mddev
));
1982 static int stop(mddev_t
*mddev
)
1984 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
1986 md_unregister_thread(mddev
->thread
);
1987 mddev
->thread
= NULL
;
1988 shrink_stripes(conf
);
1989 free_pages((unsigned long) conf
->stripe_hashtbl
, HASH_PAGES_ORDER
);
1990 blk_sync_queue(mddev
->queue
); /* the unplug fn references 'conf'*/
1991 sysfs_remove_group(&mddev
->kobj
, &raid5_attrs_group
);
1993 mddev
->private = NULL
;
1998 static void print_sh (struct stripe_head
*sh
)
2002 printk("sh %llu, pd_idx %d, state %ld.\n",
2003 (unsigned long long)sh
->sector
, sh
->pd_idx
, sh
->state
);
2004 printk("sh %llu, count %d.\n",
2005 (unsigned long long)sh
->sector
, atomic_read(&sh
->count
));
2006 printk("sh %llu, ", (unsigned long long)sh
->sector
);
2007 for (i
= 0; i
< sh
->raid_conf
->raid_disks
; i
++) {
2008 printk("(cache%d: %p %ld) ",
2009 i
, sh
->dev
[i
].page
, sh
->dev
[i
].flags
);
2014 static void printall (raid5_conf_t
*conf
)
2016 struct stripe_head
*sh
;
2019 spin_lock_irq(&conf
->device_lock
);
2020 for (i
= 0; i
< NR_HASH
; i
++) {
2021 sh
= conf
->stripe_hashtbl
[i
];
2022 for (; sh
; sh
= sh
->hash_next
) {
2023 if (sh
->raid_conf
!= conf
)
2028 spin_unlock_irq(&conf
->device_lock
);
2032 static void status (struct seq_file
*seq
, mddev_t
*mddev
)
2034 raid5_conf_t
*conf
= (raid5_conf_t
*) mddev
->private;
2037 seq_printf (seq
, " level %d, %dk chunk, algorithm %d", mddev
->level
, mddev
->chunk_size
>> 10, mddev
->layout
);
2038 seq_printf (seq
, " [%d/%d] [", conf
->raid_disks
, conf
->working_disks
);
2039 for (i
= 0; i
< conf
->raid_disks
; i
++)
2040 seq_printf (seq
, "%s",
2041 conf
->disks
[i
].rdev
&&
2042 test_bit(In_sync
, &conf
->disks
[i
].rdev
->flags
) ? "U" : "_");
2043 seq_printf (seq
, "]");
2046 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
2051 static void print_raid5_conf (raid5_conf_t
*conf
)
2054 struct disk_info
*tmp
;
2056 printk("RAID5 conf printout:\n");
2058 printk("(conf==NULL)\n");
2061 printk(" --- rd:%d wd:%d fd:%d\n", conf
->raid_disks
,
2062 conf
->working_disks
, conf
->failed_disks
);
2064 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2065 char b
[BDEVNAME_SIZE
];
2066 tmp
= conf
->disks
+ i
;
2068 printk(" disk %d, o:%d, dev:%s\n",
2069 i
, !test_bit(Faulty
, &tmp
->rdev
->flags
),
2070 bdevname(tmp
->rdev
->bdev
,b
));
2074 static int raid5_spare_active(mddev_t
*mddev
)
2077 raid5_conf_t
*conf
= mddev
->private;
2078 struct disk_info
*tmp
;
2080 for (i
= 0; i
< conf
->raid_disks
; i
++) {
2081 tmp
= conf
->disks
+ i
;
2083 && !test_bit(Faulty
, &tmp
->rdev
->flags
)
2084 && !test_bit(In_sync
, &tmp
->rdev
->flags
)) {
2086 conf
->failed_disks
--;
2087 conf
->working_disks
++;
2088 set_bit(In_sync
, &tmp
->rdev
->flags
);
2091 print_raid5_conf(conf
);
2095 static int raid5_remove_disk(mddev_t
*mddev
, int number
)
2097 raid5_conf_t
*conf
= mddev
->private;
2100 struct disk_info
*p
= conf
->disks
+ number
;
2102 print_raid5_conf(conf
);
2105 if (test_bit(In_sync
, &rdev
->flags
) ||
2106 atomic_read(&rdev
->nr_pending
)) {
2112 if (atomic_read(&rdev
->nr_pending
)) {
2113 /* lost the race, try later */
2120 print_raid5_conf(conf
);
2124 static int raid5_add_disk(mddev_t
*mddev
, mdk_rdev_t
*rdev
)
2126 raid5_conf_t
*conf
= mddev
->private;
2129 struct disk_info
*p
;
2131 if (mddev
->degraded
> 1)
2132 /* no point adding a device */
2138 for (disk
=0; disk
< mddev
->raid_disks
; disk
++)
2139 if ((p
=conf
->disks
+ disk
)->rdev
== NULL
) {
2140 clear_bit(In_sync
, &rdev
->flags
);
2141 rdev
->raid_disk
= disk
;
2143 if (rdev
->saved_raid_disk
!= disk
)
2145 rcu_assign_pointer(p
->rdev
, rdev
);
2148 print_raid5_conf(conf
);
2152 static int raid5_resize(mddev_t
*mddev
, sector_t sectors
)
2154 /* no resync is happening, and there is enough space
2155 * on all devices, so we can resize.
2156 * We need to make sure resync covers any new space.
2157 * If the array is shrinking we should possibly wait until
2158 * any io in the removed space completes, but it hardly seems
2161 sectors
&= ~((sector_t
)mddev
->chunk_size
/512 - 1);
2162 mddev
->array_size
= (sectors
* (mddev
->raid_disks
-1))>>1;
2163 set_capacity(mddev
->gendisk
, mddev
->array_size
<< 1);
2165 if (sectors
/2 > mddev
->size
&& mddev
->recovery_cp
== MaxSector
) {
2166 mddev
->recovery_cp
= mddev
->size
<< 1;
2167 set_bit(MD_RECOVERY_NEEDED
, &mddev
->recovery
);
2169 mddev
->size
= sectors
/2;
2170 mddev
->resync_max_sectors
= sectors
;
2174 static void raid5_quiesce(mddev_t
*mddev
, int state
)
2176 raid5_conf_t
*conf
= mddev_to_conf(mddev
);
2179 case 1: /* stop all writes */
2180 spin_lock_irq(&conf
->device_lock
);
2182 wait_event_lock_irq(conf
->wait_for_stripe
,
2183 atomic_read(&conf
->active_stripes
) == 0,
2184 conf
->device_lock
, /* nothing */);
2185 spin_unlock_irq(&conf
->device_lock
);
2188 case 0: /* re-enable writes */
2189 spin_lock_irq(&conf
->device_lock
);
2191 wake_up(&conf
->wait_for_stripe
);
2192 spin_unlock_irq(&conf
->device_lock
);
2195 if (mddev
->thread
) {
2197 mddev
->thread
->timeout
= mddev
->bitmap
->daemon_sleep
* HZ
;
2199 mddev
->thread
->timeout
= MAX_SCHEDULE_TIMEOUT
;
2200 md_wakeup_thread(mddev
->thread
);
2203 static mdk_personality_t raid5_personality
=
2206 .owner
= THIS_MODULE
,
2207 .make_request
= make_request
,
2211 .error_handler
= error
,
2212 .hot_add_disk
= raid5_add_disk
,
2213 .hot_remove_disk
= raid5_remove_disk
,
2214 .spare_active
= raid5_spare_active
,
2215 .sync_request
= sync_request
,
2216 .resize
= raid5_resize
,
2217 .quiesce
= raid5_quiesce
,
2220 static int __init
raid5_init (void)
2222 return register_md_personality (RAID5
, &raid5_personality
);
2225 static void raid5_exit (void)
2227 unregister_md_personality (RAID5
);
2230 module_init(raid5_init
);
2231 module_exit(raid5_exit
);
2232 MODULE_LICENSE("GPL");
2233 MODULE_ALIAS("md-personality-4"); /* RAID5 */