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 atomic_dec(&conf->active_stripes);
97 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
98 list_add_tail(&sh->lru, &conf->inactive_list);
99 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, int disks)
183 raid5_conf_t *conf = sh->raid_conf;
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);
203 for (i = sh->disks; i--; ) {
204 struct r5dev *dev = &sh->dev[i];
206 if (dev->toread || dev->towrite || dev->written ||
207 test_bit(R5_LOCKED, &dev->flags)) {
208 printk("sector=%llx i=%d %p %p %p %d\n",
209 (unsigned long long)sh->sector, i, dev->toread,
210 dev->towrite, dev->written,
211 test_bit(R5_LOCKED, &dev->flags));
215 raid5_build_block(sh, i);
217 insert_hash(conf, sh);
220 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
222 struct stripe_head *sh;
223 struct hlist_node *hn;
226 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
227 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
228 if (sh->sector == sector && sh->disks == disks)
230 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
234 static void unplug_slaves(mddev_t *mddev);
235 static void raid5_unplug_device(request_queue_t *q);
237 static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
238 int pd_idx, int noblock)
240 struct stripe_head *sh;
242 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
244 spin_lock_irq(&conf->device_lock);
247 wait_event_lock_irq(conf->wait_for_stripe,
249 conf->device_lock, /* nothing */);
250 sh = __find_stripe(conf, sector, disks);
252 if (!conf->inactive_blocked)
253 sh = get_free_stripe(conf);
254 if (noblock && sh == NULL)
257 conf->inactive_blocked = 1;
258 wait_event_lock_irq(conf->wait_for_stripe,
259 !list_empty(&conf->inactive_list) &&
260 (atomic_read(&conf->active_stripes)
261 < (conf->max_nr_stripes *3/4)
262 || !conf->inactive_blocked),
264 unplug_slaves(conf->mddev);
266 conf->inactive_blocked = 0;
268 init_stripe(sh, sector, pd_idx, disks);
270 if (atomic_read(&sh->count)) {
271 if (!list_empty(&sh->lru))
274 if (!test_bit(STRIPE_HANDLE, &sh->state))
275 atomic_inc(&conf->active_stripes);
276 if (!list_empty(&sh->lru))
277 list_del_init(&sh->lru);
280 } while (sh == NULL);
283 atomic_inc(&sh->count);
285 spin_unlock_irq(&conf->device_lock);
289 static int grow_one_stripe(raid5_conf_t *conf)
291 struct stripe_head *sh;
292 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
295 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
296 sh->raid_conf = conf;
297 spin_lock_init(&sh->lock);
299 if (grow_buffers(sh, conf->raid_disks)) {
300 shrink_buffers(sh, conf->raid_disks);
301 kmem_cache_free(conf->slab_cache, sh);
304 sh->disks = conf->raid_disks;
305 /* we just created an active stripe so... */
306 atomic_set(&sh->count, 1);
307 atomic_inc(&conf->active_stripes);
308 INIT_LIST_HEAD(&sh->lru);
313 static int grow_stripes(raid5_conf_t *conf, int num)
316 int devs = conf->raid_disks;
318 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
319 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
320 conf->active_name = 0;
321 sc = kmem_cache_create(conf->cache_name[conf->active_name],
322 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
326 conf->slab_cache = sc;
327 conf->pool_size = devs;
329 if (!grow_one_stripe(conf))
335 #ifdef CONFIG_MD_RAID5_RESHAPE
336 static int resize_stripes(raid5_conf_t *conf, int newsize)
338 /* Make all the stripes able to hold 'newsize' devices.
339 * New slots in each stripe get 'page' set to a new page.
341 * This happens in stages:
342 * 1/ create a new kmem_cache and allocate the required number of
344 * 2/ gather all the old stripe_heads and tranfer the pages across
345 * to the new stripe_heads. This will have the side effect of
346 * freezing the array as once all stripe_heads have been collected,
347 * no IO will be possible. Old stripe heads are freed once their
348 * pages have been transferred over, and the old kmem_cache is
349 * freed when all stripes are done.
350 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
351 * we simple return a failre status - no need to clean anything up.
352 * 4/ allocate new pages for the new slots in the new stripe_heads.
353 * If this fails, we don't bother trying the shrink the
354 * stripe_heads down again, we just leave them as they are.
355 * As each stripe_head is processed the new one is released into
358 * Once step2 is started, we cannot afford to wait for a write,
359 * so we use GFP_NOIO allocations.
361 struct stripe_head *osh, *nsh;
362 LIST_HEAD(newstripes);
363 struct disk_info *ndisks;
368 if (newsize <= conf->pool_size)
369 return 0; /* never bother to shrink */
372 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
373 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
378 for (i = conf->max_nr_stripes; i; i--) {
379 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
383 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
385 nsh->raid_conf = conf;
386 spin_lock_init(&nsh->lock);
388 list_add(&nsh->lru, &newstripes);
391 /* didn't get enough, give up */
392 while (!list_empty(&newstripes)) {
393 nsh = list_entry(newstripes.next, struct stripe_head, lru);
395 kmem_cache_free(sc, nsh);
397 kmem_cache_destroy(sc);
400 /* Step 2 - Must use GFP_NOIO now.
401 * OK, we have enough stripes, start collecting inactive
402 * stripes and copying them over
404 list_for_each_entry(nsh, &newstripes, lru) {
405 spin_lock_irq(&conf->device_lock);
406 wait_event_lock_irq(conf->wait_for_stripe,
407 !list_empty(&conf->inactive_list),
409 unplug_slaves(conf->mddev);
411 osh = get_free_stripe(conf);
412 spin_unlock_irq(&conf->device_lock);
413 atomic_set(&nsh->count, 1);
414 for(i=0; i<conf->pool_size; i++)
415 nsh->dev[i].page = osh->dev[i].page;
416 for( ; i<newsize; i++)
417 nsh->dev[i].page = NULL;
418 kmem_cache_free(conf->slab_cache, osh);
420 kmem_cache_destroy(conf->slab_cache);
423 * At this point, we are holding all the stripes so the array
424 * is completely stalled, so now is a good time to resize
427 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
429 for (i=0; i<conf->raid_disks; i++)
430 ndisks[i] = conf->disks[i];
432 conf->disks = ndisks;
436 /* Step 4, return new stripes to service */
437 while(!list_empty(&newstripes)) {
438 nsh = list_entry(newstripes.next, struct stripe_head, lru);
439 list_del_init(&nsh->lru);
440 for (i=conf->raid_disks; i < newsize; i++)
441 if (nsh->dev[i].page == NULL) {
442 struct page *p = alloc_page(GFP_NOIO);
443 nsh->dev[i].page = p;
449 /* critical section pass, GFP_NOIO no longer needed */
451 conf->slab_cache = sc;
452 conf->active_name = 1-conf->active_name;
453 conf->pool_size = newsize;
458 static int drop_one_stripe(raid5_conf_t *conf)
460 struct stripe_head *sh;
462 spin_lock_irq(&conf->device_lock);
463 sh = get_free_stripe(conf);
464 spin_unlock_irq(&conf->device_lock);
467 if (atomic_read(&sh->count))
469 shrink_buffers(sh, conf->pool_size);
470 kmem_cache_free(conf->slab_cache, sh);
471 atomic_dec(&conf->active_stripes);
475 static void shrink_stripes(raid5_conf_t *conf)
477 while (drop_one_stripe(conf))
480 if (conf->slab_cache)
481 kmem_cache_destroy(conf->slab_cache);
482 conf->slab_cache = NULL;
485 static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
488 struct stripe_head *sh = bi->bi_private;
489 raid5_conf_t *conf = sh->raid_conf;
490 int disks = sh->disks, i;
491 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
496 for (i=0 ; i<disks; i++)
497 if (bi == &sh->dev[i].req)
500 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
501 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
512 spin_lock_irqsave(&conf->device_lock, flags);
513 /* we can return a buffer if we bypassed the cache or
514 * if the top buffer is not in highmem. If there are
515 * multiple buffers, leave the extra work to
518 buffer = sh->bh_read[i];
520 (!PageHighMem(buffer->b_page)
521 || buffer->b_page == bh->b_page )
523 sh->bh_read[i] = buffer->b_reqnext;
524 buffer->b_reqnext = NULL;
527 spin_unlock_irqrestore(&conf->device_lock, flags);
528 if (sh->bh_page[i]==bh->b_page)
529 set_buffer_uptodate(bh);
531 if (buffer->b_page != bh->b_page)
532 memcpy(buffer->b_data, bh->b_data, bh->b_size);
533 buffer->b_end_io(buffer, 1);
536 set_bit(R5_UPTODATE, &sh->dev[i].flags);
538 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
539 printk(KERN_INFO "raid5: read error corrected!!\n");
540 clear_bit(R5_ReadError, &sh->dev[i].flags);
541 clear_bit(R5_ReWrite, &sh->dev[i].flags);
543 if (atomic_read(&conf->disks[i].rdev->read_errors))
544 atomic_set(&conf->disks[i].rdev->read_errors, 0);
547 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
548 atomic_inc(&conf->disks[i].rdev->read_errors);
549 if (conf->mddev->degraded)
550 printk(KERN_WARNING "raid5: read error not correctable.\n");
551 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
553 printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
554 else if (atomic_read(&conf->disks[i].rdev->read_errors)
555 > conf->max_nr_stripes)
557 "raid5: Too many read errors, failing device.\n");
561 set_bit(R5_ReadError, &sh->dev[i].flags);
563 clear_bit(R5_ReadError, &sh->dev[i].flags);
564 clear_bit(R5_ReWrite, &sh->dev[i].flags);
565 md_error(conf->mddev, conf->disks[i].rdev);
568 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
570 /* must restore b_page before unlocking buffer... */
571 if (sh->bh_page[i] != bh->b_page) {
572 bh->b_page = sh->bh_page[i];
573 bh->b_data = page_address(bh->b_page);
574 clear_buffer_uptodate(bh);
577 clear_bit(R5_LOCKED, &sh->dev[i].flags);
578 set_bit(STRIPE_HANDLE, &sh->state);
583 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
586 struct stripe_head *sh = bi->bi_private;
587 raid5_conf_t *conf = sh->raid_conf;
588 int disks = sh->disks, i;
590 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
595 for (i=0 ; i<disks; i++)
596 if (bi == &sh->dev[i].req)
599 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
600 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
607 spin_lock_irqsave(&conf->device_lock, flags);
609 md_error(conf->mddev, conf->disks[i].rdev);
611 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
613 clear_bit(R5_LOCKED, &sh->dev[i].flags);
614 set_bit(STRIPE_HANDLE, &sh->state);
615 __release_stripe(conf, sh);
616 spin_unlock_irqrestore(&conf->device_lock, flags);
621 static sector_t compute_blocknr(struct stripe_head *sh, int i);
623 static void raid5_build_block (struct stripe_head *sh, int i)
625 struct r5dev *dev = &sh->dev[i];
628 dev->req.bi_io_vec = &dev->vec;
630 dev->req.bi_max_vecs++;
631 dev->vec.bv_page = dev->page;
632 dev->vec.bv_len = STRIPE_SIZE;
633 dev->vec.bv_offset = 0;
635 dev->req.bi_sector = sh->sector;
636 dev->req.bi_private = sh;
640 dev->sector = compute_blocknr(sh, i);
643 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
645 char b[BDEVNAME_SIZE];
646 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
647 PRINTK("raid5: error called\n");
649 if (!test_bit(Faulty, &rdev->flags)) {
651 if (test_bit(In_sync, &rdev->flags)) {
652 conf->working_disks--;
654 conf->failed_disks++;
655 clear_bit(In_sync, &rdev->flags);
657 * if recovery was running, make sure it aborts.
659 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
661 set_bit(Faulty, &rdev->flags);
663 "raid5: Disk failure on %s, disabling device."
664 " Operation continuing on %d devices\n",
665 bdevname(rdev->bdev,b), conf->working_disks);
670 * Input: a 'big' sector number,
671 * Output: index of the data and parity disk, and the sector # in them.
673 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
674 unsigned int data_disks, unsigned int * dd_idx,
675 unsigned int * pd_idx, raid5_conf_t *conf)
678 unsigned long chunk_number;
679 unsigned int chunk_offset;
681 int sectors_per_chunk = conf->chunk_size >> 9;
683 /* First compute the information on this sector */
686 * Compute the chunk number and the sector offset inside the chunk
688 chunk_offset = sector_div(r_sector, sectors_per_chunk);
689 chunk_number = r_sector;
690 BUG_ON(r_sector != chunk_number);
693 * Compute the stripe number
695 stripe = chunk_number / data_disks;
698 * Compute the data disk and parity disk indexes inside the stripe
700 *dd_idx = chunk_number % data_disks;
703 * Select the parity disk based on the user selected algorithm.
705 if (conf->level == 4)
706 *pd_idx = data_disks;
707 else switch (conf->algorithm) {
708 case ALGORITHM_LEFT_ASYMMETRIC:
709 *pd_idx = data_disks - stripe % raid_disks;
710 if (*dd_idx >= *pd_idx)
713 case ALGORITHM_RIGHT_ASYMMETRIC:
714 *pd_idx = stripe % raid_disks;
715 if (*dd_idx >= *pd_idx)
718 case ALGORITHM_LEFT_SYMMETRIC:
719 *pd_idx = data_disks - stripe % raid_disks;
720 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
722 case ALGORITHM_RIGHT_SYMMETRIC:
723 *pd_idx = stripe % raid_disks;
724 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
727 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
732 * Finally, compute the new sector number
734 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
739 static sector_t compute_blocknr(struct stripe_head *sh, int i)
741 raid5_conf_t *conf = sh->raid_conf;
742 int raid_disks = sh->disks, data_disks = raid_disks - 1;
743 sector_t new_sector = sh->sector, check;
744 int sectors_per_chunk = conf->chunk_size >> 9;
747 int chunk_number, dummy1, dummy2, dd_idx = i;
750 chunk_offset = sector_div(new_sector, sectors_per_chunk);
752 BUG_ON(new_sector != stripe);
755 switch (conf->algorithm) {
756 case ALGORITHM_LEFT_ASYMMETRIC:
757 case ALGORITHM_RIGHT_ASYMMETRIC:
761 case ALGORITHM_LEFT_SYMMETRIC:
762 case ALGORITHM_RIGHT_SYMMETRIC:
765 i -= (sh->pd_idx + 1);
768 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
772 chunk_number = stripe * data_disks + i;
773 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
775 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
776 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
777 printk(KERN_ERR "compute_blocknr: map not correct\n");
786 * Copy data between a page in the stripe cache, and a bio.
787 * There are no alignment or size guarantees between the page or the
788 * bio except that there is some overlap.
789 * All iovecs in the bio must be considered.
791 static void copy_data(int frombio, struct bio *bio,
795 char *pa = page_address(page);
800 if (bio->bi_sector >= sector)
801 page_offset = (signed)(bio->bi_sector - sector) * 512;
803 page_offset = (signed)(sector - bio->bi_sector) * -512;
804 bio_for_each_segment(bvl, bio, i) {
805 int len = bio_iovec_idx(bio,i)->bv_len;
809 if (page_offset < 0) {
810 b_offset = -page_offset;
811 page_offset += b_offset;
815 if (len > 0 && page_offset + len > STRIPE_SIZE)
816 clen = STRIPE_SIZE - page_offset;
820 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
822 memcpy(pa+page_offset, ba+b_offset, clen);
824 memcpy(ba+b_offset, pa+page_offset, clen);
825 __bio_kunmap_atomic(ba, KM_USER0);
827 if (clen < len) /* hit end of page */
833 #define check_xor() do { \
834 if (count == MAX_XOR_BLOCKS) { \
835 xor_block(count, STRIPE_SIZE, ptr); \
841 static void compute_block(struct stripe_head *sh, int dd_idx)
843 int i, count, disks = sh->disks;
844 void *ptr[MAX_XOR_BLOCKS], *p;
846 PRINTK("compute_block, stripe %llu, idx %d\n",
847 (unsigned long long)sh->sector, dd_idx);
849 ptr[0] = page_address(sh->dev[dd_idx].page);
850 memset(ptr[0], 0, STRIPE_SIZE);
852 for (i = disks ; i--; ) {
855 p = page_address(sh->dev[i].page);
856 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
859 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
860 " not present\n", dd_idx,
861 (unsigned long long)sh->sector, i);
866 xor_block(count, STRIPE_SIZE, ptr);
867 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
870 static void compute_parity(struct stripe_head *sh, int method)
872 raid5_conf_t *conf = sh->raid_conf;
873 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
874 void *ptr[MAX_XOR_BLOCKS];
877 PRINTK("compute_parity, stripe %llu, method %d\n",
878 (unsigned long long)sh->sector, method);
881 ptr[0] = page_address(sh->dev[pd_idx].page);
883 case READ_MODIFY_WRITE:
884 if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
886 for (i=disks ; i-- ;) {
889 if (sh->dev[i].towrite &&
890 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
891 ptr[count++] = page_address(sh->dev[i].page);
892 chosen = sh->dev[i].towrite;
893 sh->dev[i].towrite = NULL;
895 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
896 wake_up(&conf->wait_for_overlap);
898 if (sh->dev[i].written) BUG();
899 sh->dev[i].written = chosen;
904 case RECONSTRUCT_WRITE:
905 memset(ptr[0], 0, STRIPE_SIZE);
906 for (i= disks; i-- ;)
907 if (i!=pd_idx && sh->dev[i].towrite) {
908 chosen = sh->dev[i].towrite;
909 sh->dev[i].towrite = NULL;
911 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
912 wake_up(&conf->wait_for_overlap);
914 if (sh->dev[i].written) BUG();
915 sh->dev[i].written = chosen;
922 xor_block(count, STRIPE_SIZE, ptr);
926 for (i = disks; i--;)
927 if (sh->dev[i].written) {
928 sector_t sector = sh->dev[i].sector;
929 struct bio *wbi = sh->dev[i].written;
930 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
931 copy_data(1, wbi, sh->dev[i].page, sector);
932 wbi = r5_next_bio(wbi, sector);
935 set_bit(R5_LOCKED, &sh->dev[i].flags);
936 set_bit(R5_UPTODATE, &sh->dev[i].flags);
940 case RECONSTRUCT_WRITE:
944 ptr[count++] = page_address(sh->dev[i].page);
948 case READ_MODIFY_WRITE:
949 for (i = disks; i--;)
950 if (sh->dev[i].written) {
951 ptr[count++] = page_address(sh->dev[i].page);
956 xor_block(count, STRIPE_SIZE, ptr);
958 if (method != CHECK_PARITY) {
959 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
960 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
962 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
966 * Each stripe/dev can have one or more bion attached.
967 * toread/towrite point to the first in a chain.
968 * The bi_next chain must be in order.
970 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
973 raid5_conf_t *conf = sh->raid_conf;
976 PRINTK("adding bh b#%llu to stripe s#%llu\n",
977 (unsigned long long)bi->bi_sector,
978 (unsigned long long)sh->sector);
981 spin_lock(&sh->lock);
982 spin_lock_irq(&conf->device_lock);
984 bip = &sh->dev[dd_idx].towrite;
985 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
988 bip = &sh->dev[dd_idx].toread;
989 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
990 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
992 bip = & (*bip)->bi_next;
994 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
997 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
1002 bi->bi_phys_segments ++;
1003 spin_unlock_irq(&conf->device_lock);
1004 spin_unlock(&sh->lock);
1006 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1007 (unsigned long long)bi->bi_sector,
1008 (unsigned long long)sh->sector, dd_idx);
1010 if (conf->mddev->bitmap && firstwrite) {
1011 sh->bm_seq = conf->seq_write;
1012 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1014 set_bit(STRIPE_BIT_DELAY, &sh->state);
1018 /* check if page is covered */
1019 sector_t sector = sh->dev[dd_idx].sector;
1020 for (bi=sh->dev[dd_idx].towrite;
1021 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1022 bi && bi->bi_sector <= sector;
1023 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1024 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1025 sector = bi->bi_sector + (bi->bi_size>>9);
1027 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1028 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1033 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1034 spin_unlock_irq(&conf->device_lock);
1035 spin_unlock(&sh->lock);
1039 static void end_reshape(raid5_conf_t *conf);
1041 static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1043 int sectors_per_chunk = conf->chunk_size >> 9;
1044 sector_t x = stripe;
1046 int chunk_offset = sector_div(x, sectors_per_chunk);
1048 raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
1049 + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
1055 * handle_stripe - do things to a stripe.
1057 * We lock the stripe and then examine the state of various bits
1058 * to see what needs to be done.
1060 * return some read request which now have data
1061 * return some write requests which are safely on disc
1062 * schedule a read on some buffers
1063 * schedule a write of some buffers
1064 * return confirmation of parity correctness
1066 * Parity calculations are done inside the stripe lock
1067 * buffers are taken off read_list or write_list, and bh_cache buffers
1068 * get BH_Lock set before the stripe lock is released.
1072 static void handle_stripe(struct stripe_head *sh)
1074 raid5_conf_t *conf = sh->raid_conf;
1075 int disks = sh->disks;
1076 struct bio *return_bi= NULL;
1079 int syncing, expanding, expanded;
1080 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1081 int non_overwrite = 0;
1085 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1086 (unsigned long long)sh->sector, atomic_read(&sh->count),
1089 spin_lock(&sh->lock);
1090 clear_bit(STRIPE_HANDLE, &sh->state);
1091 clear_bit(STRIPE_DELAYED, &sh->state);
1093 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1094 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1095 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
1096 /* Now to look around and see what can be done */
1099 for (i=disks; i--; ) {
1102 clear_bit(R5_Insync, &dev->flags);
1104 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1105 i, dev->flags, dev->toread, dev->towrite, dev->written);
1106 /* maybe we can reply to a read */
1107 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1108 struct bio *rbi, *rbi2;
1109 PRINTK("Return read for disc %d\n", i);
1110 spin_lock_irq(&conf->device_lock);
1113 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1114 wake_up(&conf->wait_for_overlap);
1115 spin_unlock_irq(&conf->device_lock);
1116 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1117 copy_data(0, rbi, dev->page, dev->sector);
1118 rbi2 = r5_next_bio(rbi, dev->sector);
1119 spin_lock_irq(&conf->device_lock);
1120 if (--rbi->bi_phys_segments == 0) {
1121 rbi->bi_next = return_bi;
1124 spin_unlock_irq(&conf->device_lock);
1129 /* now count some things */
1130 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1131 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1134 if (dev->toread) to_read++;
1137 if (!test_bit(R5_OVERWRITE, &dev->flags))
1140 if (dev->written) written++;
1141 rdev = rcu_dereference(conf->disks[i].rdev);
1142 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1143 /* The ReadError flag will just be confusing now */
1144 clear_bit(R5_ReadError, &dev->flags);
1145 clear_bit(R5_ReWrite, &dev->flags);
1147 if (!rdev || !test_bit(In_sync, &rdev->flags)
1148 || test_bit(R5_ReadError, &dev->flags)) {
1152 set_bit(R5_Insync, &dev->flags);
1155 PRINTK("locked=%d uptodate=%d to_read=%d"
1156 " to_write=%d failed=%d failed_num=%d\n",
1157 locked, uptodate, to_read, to_write, failed, failed_num);
1158 /* check if the array has lost two devices and, if so, some requests might
1161 if (failed > 1 && to_read+to_write+written) {
1162 for (i=disks; i--; ) {
1165 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1168 rdev = rcu_dereference(conf->disks[i].rdev);
1169 if (rdev && test_bit(In_sync, &rdev->flags))
1170 /* multiple read failures in one stripe */
1171 md_error(conf->mddev, rdev);
1175 spin_lock_irq(&conf->device_lock);
1176 /* fail all writes first */
1177 bi = sh->dev[i].towrite;
1178 sh->dev[i].towrite = NULL;
1179 if (bi) { to_write--; bitmap_end = 1; }
1181 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1182 wake_up(&conf->wait_for_overlap);
1184 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1185 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1186 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1187 if (--bi->bi_phys_segments == 0) {
1188 md_write_end(conf->mddev);
1189 bi->bi_next = return_bi;
1194 /* and fail all 'written' */
1195 bi = sh->dev[i].written;
1196 sh->dev[i].written = NULL;
1197 if (bi) bitmap_end = 1;
1198 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1199 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1200 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1201 if (--bi->bi_phys_segments == 0) {
1202 md_write_end(conf->mddev);
1203 bi->bi_next = return_bi;
1209 /* fail any reads if this device is non-operational */
1210 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1211 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1212 bi = sh->dev[i].toread;
1213 sh->dev[i].toread = NULL;
1214 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1215 wake_up(&conf->wait_for_overlap);
1217 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1218 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1219 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1220 if (--bi->bi_phys_segments == 0) {
1221 bi->bi_next = return_bi;
1227 spin_unlock_irq(&conf->device_lock);
1229 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1230 STRIPE_SECTORS, 0, 0);
1233 if (failed > 1 && syncing) {
1234 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1235 clear_bit(STRIPE_SYNCING, &sh->state);
1239 /* might be able to return some write requests if the parity block
1240 * is safe, or on a failed drive
1242 dev = &sh->dev[sh->pd_idx];
1244 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1245 test_bit(R5_UPTODATE, &dev->flags))
1246 || (failed == 1 && failed_num == sh->pd_idx))
1248 /* any written block on an uptodate or failed drive can be returned.
1249 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1250 * never LOCKED, so we don't need to test 'failed' directly.
1252 for (i=disks; i--; )
1253 if (sh->dev[i].written) {
1255 if (!test_bit(R5_LOCKED, &dev->flags) &&
1256 test_bit(R5_UPTODATE, &dev->flags) ) {
1257 /* We can return any write requests */
1258 struct bio *wbi, *wbi2;
1260 PRINTK("Return write for disc %d\n", i);
1261 spin_lock_irq(&conf->device_lock);
1263 dev->written = NULL;
1264 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1265 wbi2 = r5_next_bio(wbi, dev->sector);
1266 if (--wbi->bi_phys_segments == 0) {
1267 md_write_end(conf->mddev);
1268 wbi->bi_next = return_bi;
1273 if (dev->towrite == NULL)
1275 spin_unlock_irq(&conf->device_lock);
1277 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1279 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1284 /* Now we might consider reading some blocks, either to check/generate
1285 * parity, or to satisfy requests
1286 * or to load a block that is being partially written.
1288 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
1289 for (i=disks; i--;) {
1291 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1293 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1296 (failed && (sh->dev[failed_num].toread ||
1297 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1300 /* we would like to get this block, possibly
1301 * by computing it, but we might not be able to
1303 if (uptodate == disks-1) {
1304 PRINTK("Computing block %d\n", i);
1305 compute_block(sh, i);
1307 } else if (test_bit(R5_Insync, &dev->flags)) {
1308 set_bit(R5_LOCKED, &dev->flags);
1309 set_bit(R5_Wantread, &dev->flags);
1311 /* if I am just reading this block and we don't have
1312 a failed drive, or any pending writes then sidestep the cache */
1313 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1314 ! syncing && !failed && !to_write) {
1315 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1316 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1320 PRINTK("Reading block %d (sync=%d)\n",
1325 set_bit(STRIPE_HANDLE, &sh->state);
1328 /* now to consider writing and what else, if anything should be read */
1331 for (i=disks ; i--;) {
1332 /* would I have to read this buffer for read_modify_write */
1334 if ((dev->towrite || i == sh->pd_idx) &&
1335 (!test_bit(R5_LOCKED, &dev->flags)
1337 || sh->bh_page[i]!=bh->b_page
1340 !test_bit(R5_UPTODATE, &dev->flags)) {
1341 if (test_bit(R5_Insync, &dev->flags)
1342 /* && !(!mddev->insync && i == sh->pd_idx) */
1345 else rmw += 2*disks; /* cannot read it */
1347 /* Would I have to read this buffer for reconstruct_write */
1348 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1349 (!test_bit(R5_LOCKED, &dev->flags)
1351 || sh->bh_page[i] != bh->b_page
1354 !test_bit(R5_UPTODATE, &dev->flags)) {
1355 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1356 else rcw += 2*disks;
1359 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1360 (unsigned long long)sh->sector, rmw, rcw);
1361 set_bit(STRIPE_HANDLE, &sh->state);
1362 if (rmw < rcw && rmw > 0)
1363 /* prefer read-modify-write, but need to get some data */
1364 for (i=disks; i--;) {
1366 if ((dev->towrite || i == sh->pd_idx) &&
1367 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1368 test_bit(R5_Insync, &dev->flags)) {
1369 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1371 PRINTK("Read_old block %d for r-m-w\n", i);
1372 set_bit(R5_LOCKED, &dev->flags);
1373 set_bit(R5_Wantread, &dev->flags);
1376 set_bit(STRIPE_DELAYED, &sh->state);
1377 set_bit(STRIPE_HANDLE, &sh->state);
1381 if (rcw <= rmw && rcw > 0)
1382 /* want reconstruct write, but need to get some data */
1383 for (i=disks; i--;) {
1385 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1386 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1387 test_bit(R5_Insync, &dev->flags)) {
1388 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1390 PRINTK("Read_old block %d for Reconstruct\n", i);
1391 set_bit(R5_LOCKED, &dev->flags);
1392 set_bit(R5_Wantread, &dev->flags);
1395 set_bit(STRIPE_DELAYED, &sh->state);
1396 set_bit(STRIPE_HANDLE, &sh->state);
1400 /* now if nothing is locked, and if we have enough data, we can start a write request */
1401 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1402 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1403 PRINTK("Computing parity...\n");
1404 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1405 /* now every locked buffer is ready to be written */
1407 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1408 PRINTK("Writing block %d\n", i);
1410 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1411 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1412 || (i==sh->pd_idx && failed == 0))
1413 set_bit(STRIPE_INSYNC, &sh->state);
1415 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1416 atomic_dec(&conf->preread_active_stripes);
1417 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1418 md_wakeup_thread(conf->mddev->thread);
1423 /* maybe we need to check and possibly fix the parity for this stripe
1424 * Any reads will already have been scheduled, so we just see if enough data
1427 if (syncing && locked == 0 &&
1428 !test_bit(STRIPE_INSYNC, &sh->state)) {
1429 set_bit(STRIPE_HANDLE, &sh->state);
1432 if (uptodate != disks)
1434 compute_parity(sh, CHECK_PARITY);
1436 pagea = page_address(sh->dev[sh->pd_idx].page);
1437 if ((*(u32*)pagea) == 0 &&
1438 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1439 /* parity is correct (on disc, not in buffer any more) */
1440 set_bit(STRIPE_INSYNC, &sh->state);
1442 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1443 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1444 /* don't try to repair!! */
1445 set_bit(STRIPE_INSYNC, &sh->state);
1447 compute_block(sh, sh->pd_idx);
1452 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1453 /* either failed parity check, or recovery is happening */
1455 failed_num = sh->pd_idx;
1456 dev = &sh->dev[failed_num];
1457 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1458 BUG_ON(uptodate != disks);
1460 set_bit(R5_LOCKED, &dev->flags);
1461 set_bit(R5_Wantwrite, &dev->flags);
1462 clear_bit(STRIPE_DEGRADED, &sh->state);
1464 set_bit(STRIPE_INSYNC, &sh->state);
1467 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1468 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1469 clear_bit(STRIPE_SYNCING, &sh->state);
1472 /* If the failed drive is just a ReadError, then we might need to progress
1473 * the repair/check process
1475 if (failed == 1 && ! conf->mddev->ro &&
1476 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
1477 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1478 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1480 dev = &sh->dev[failed_num];
1481 if (!test_bit(R5_ReWrite, &dev->flags)) {
1482 set_bit(R5_Wantwrite, &dev->flags);
1483 set_bit(R5_ReWrite, &dev->flags);
1484 set_bit(R5_LOCKED, &dev->flags);
1487 /* let's read it back */
1488 set_bit(R5_Wantread, &dev->flags);
1489 set_bit(R5_LOCKED, &dev->flags);
1494 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1495 /* Need to write out all blocks after computing parity */
1496 sh->disks = conf->raid_disks;
1497 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
1498 compute_parity(sh, RECONSTRUCT_WRITE);
1499 for (i= conf->raid_disks; i--;) {
1500 set_bit(R5_LOCKED, &sh->dev[i].flags);
1502 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1504 clear_bit(STRIPE_EXPANDING, &sh->state);
1505 } else if (expanded) {
1506 clear_bit(STRIPE_EXPAND_READY, &sh->state);
1507 wake_up(&conf->wait_for_overlap);
1508 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1511 if (expanding && locked == 0) {
1512 /* We have read all the blocks in this stripe and now we need to
1513 * copy some of them into a target stripe for expand.
1515 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1516 for (i=0; i< sh->disks; i++)
1517 if (i != sh->pd_idx) {
1518 int dd_idx, pd_idx, j;
1519 struct stripe_head *sh2;
1521 sector_t bn = compute_blocknr(sh, i);
1522 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1524 &dd_idx, &pd_idx, conf);
1525 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1527 /* so far only the early blocks of this stripe
1528 * have been requested. When later blocks
1529 * get requested, we will try again
1532 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1533 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1534 /* must have already done this block */
1535 release_stripe(sh2);
1538 memcpy(page_address(sh2->dev[dd_idx].page),
1539 page_address(sh->dev[i].page),
1541 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1542 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1543 for (j=0; j<conf->raid_disks; j++)
1544 if (j != sh2->pd_idx &&
1545 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1547 if (j == conf->raid_disks) {
1548 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1549 set_bit(STRIPE_HANDLE, &sh2->state);
1551 release_stripe(sh2);
1555 spin_unlock(&sh->lock);
1557 while ((bi=return_bi)) {
1558 int bytes = bi->bi_size;
1560 return_bi = bi->bi_next;
1563 bi->bi_end_io(bi, bytes, 0);
1565 for (i=disks; i-- ;) {
1569 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1571 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1576 bi = &sh->dev[i].req;
1580 bi->bi_end_io = raid5_end_write_request;
1582 bi->bi_end_io = raid5_end_read_request;
1585 rdev = rcu_dereference(conf->disks[i].rdev);
1586 if (rdev && test_bit(Faulty, &rdev->flags))
1589 atomic_inc(&rdev->nr_pending);
1593 if (syncing || expanding || expanded)
1594 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1596 bi->bi_bdev = rdev->bdev;
1597 PRINTK("for %llu schedule op %ld on disc %d\n",
1598 (unsigned long long)sh->sector, bi->bi_rw, i);
1599 atomic_inc(&sh->count);
1600 bi->bi_sector = sh->sector + rdev->data_offset;
1601 bi->bi_flags = 1 << BIO_UPTODATE;
1603 bi->bi_max_vecs = 1;
1605 bi->bi_io_vec = &sh->dev[i].vec;
1606 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1607 bi->bi_io_vec[0].bv_offset = 0;
1608 bi->bi_size = STRIPE_SIZE;
1611 test_bit(R5_ReWrite, &sh->dev[i].flags))
1612 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1613 generic_make_request(bi);
1616 set_bit(STRIPE_DEGRADED, &sh->state);
1617 PRINTK("skip op %ld on disc %d for sector %llu\n",
1618 bi->bi_rw, i, (unsigned long long)sh->sector);
1619 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1620 set_bit(STRIPE_HANDLE, &sh->state);
1625 static void raid5_activate_delayed(raid5_conf_t *conf)
1627 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1628 while (!list_empty(&conf->delayed_list)) {
1629 struct list_head *l = conf->delayed_list.next;
1630 struct stripe_head *sh;
1631 sh = list_entry(l, struct stripe_head, lru);
1633 clear_bit(STRIPE_DELAYED, &sh->state);
1634 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1635 atomic_inc(&conf->preread_active_stripes);
1636 list_add_tail(&sh->lru, &conf->handle_list);
1641 static void activate_bit_delay(raid5_conf_t *conf)
1643 /* device_lock is held */
1644 struct list_head head;
1645 list_add(&head, &conf->bitmap_list);
1646 list_del_init(&conf->bitmap_list);
1647 while (!list_empty(&head)) {
1648 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1649 list_del_init(&sh->lru);
1650 atomic_inc(&sh->count);
1651 __release_stripe(conf, sh);
1655 static void unplug_slaves(mddev_t *mddev)
1657 raid5_conf_t *conf = mddev_to_conf(mddev);
1661 for (i=0; i<mddev->raid_disks; i++) {
1662 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1663 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
1664 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1666 atomic_inc(&rdev->nr_pending);
1669 if (r_queue->unplug_fn)
1670 r_queue->unplug_fn(r_queue);
1672 rdev_dec_pending(rdev, mddev);
1679 static void raid5_unplug_device(request_queue_t *q)
1681 mddev_t *mddev = q->queuedata;
1682 raid5_conf_t *conf = mddev_to_conf(mddev);
1683 unsigned long flags;
1685 spin_lock_irqsave(&conf->device_lock, flags);
1687 if (blk_remove_plug(q)) {
1689 raid5_activate_delayed(conf);
1691 md_wakeup_thread(mddev->thread);
1693 spin_unlock_irqrestore(&conf->device_lock, flags);
1695 unplug_slaves(mddev);
1698 static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
1699 sector_t *error_sector)
1701 mddev_t *mddev = q->queuedata;
1702 raid5_conf_t *conf = mddev_to_conf(mddev);
1706 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1707 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1708 if (rdev && !test_bit(Faulty, &rdev->flags)) {
1709 struct block_device *bdev = rdev->bdev;
1710 request_queue_t *r_queue = bdev_get_queue(bdev);
1712 if (!r_queue->issue_flush_fn)
1715 atomic_inc(&rdev->nr_pending);
1717 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1719 rdev_dec_pending(rdev, mddev);
1728 static inline void raid5_plug_device(raid5_conf_t *conf)
1730 spin_lock_irq(&conf->device_lock);
1731 blk_plug_device(conf->mddev->queue);
1732 spin_unlock_irq(&conf->device_lock);
1735 static int make_request(request_queue_t *q, struct bio * bi)
1737 mddev_t *mddev = q->queuedata;
1738 raid5_conf_t *conf = mddev_to_conf(mddev);
1739 unsigned int dd_idx, pd_idx;
1740 sector_t new_sector;
1741 sector_t logical_sector, last_sector;
1742 struct stripe_head *sh;
1743 const int rw = bio_data_dir(bi);
1745 if (unlikely(bio_barrier(bi))) {
1746 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1750 md_write_start(mddev, bi);
1752 disk_stat_inc(mddev->gendisk, ios[rw]);
1753 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1755 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1756 last_sector = bi->bi_sector + (bi->bi_size>>9);
1758 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1760 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1765 if (likely(conf->expand_progress == MaxSector))
1766 disks = conf->raid_disks;
1768 spin_lock_irq(&conf->device_lock);
1769 disks = conf->raid_disks;
1770 if (logical_sector >= conf->expand_progress)
1771 disks = conf->previous_raid_disks;
1772 spin_unlock_irq(&conf->device_lock);
1774 new_sector = raid5_compute_sector(logical_sector, disks, disks - 1,
1775 &dd_idx, &pd_idx, conf);
1776 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1777 (unsigned long long)new_sector,
1778 (unsigned long long)logical_sector);
1780 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1781 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
1783 if (unlikely(conf->expand_progress != MaxSector)) {
1784 /* expansion might have moved on while waiting for a
1785 * stripe, so we much do the range check again.
1788 spin_lock_irq(&conf->device_lock);
1789 if (logical_sector < conf->expand_progress &&
1790 disks == conf->previous_raid_disks)
1791 /* mismatch, need to try again */
1793 spin_unlock_irq(&conf->device_lock);
1800 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
1801 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1802 /* Stripe is busy expanding or
1803 * add failed due to overlap. Flush everything
1806 raid5_unplug_device(mddev->queue);
1811 finish_wait(&conf->wait_for_overlap, &w);
1812 raid5_plug_device(conf);
1816 /* cannot get stripe for read-ahead, just give-up */
1817 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1818 finish_wait(&conf->wait_for_overlap, &w);
1823 spin_lock_irq(&conf->device_lock);
1824 if (--bi->bi_phys_segments == 0) {
1825 int bytes = bi->bi_size;
1827 if ( bio_data_dir(bi) == WRITE )
1828 md_write_end(mddev);
1830 bi->bi_end_io(bi, bytes, 0);
1832 spin_unlock_irq(&conf->device_lock);
1836 /* FIXME go_faster isn't used */
1837 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1839 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1840 struct stripe_head *sh;
1842 sector_t first_sector, last_sector;
1843 int raid_disks = conf->raid_disks;
1844 int data_disks = raid_disks-1;
1845 sector_t max_sector = mddev->size << 1;
1848 if (sector_nr >= max_sector) {
1849 /* just being told to finish up .. nothing much to do */
1850 unplug_slaves(mddev);
1851 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
1856 if (mddev->curr_resync < max_sector) /* aborted */
1857 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1859 else /* compelted sync */
1861 bitmap_close_sync(mddev->bitmap);
1866 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
1867 /* reshaping is quite different to recovery/resync so it is
1868 * handled quite separately ... here.
1870 * On each call to sync_request, we gather one chunk worth of
1871 * destination stripes and flag them as expanding.
1872 * Then we find all the source stripes and request reads.
1873 * As the reads complete, handle_stripe will copy the data
1874 * into the destination stripe and release that stripe.
1878 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
1881 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
1882 sh = get_active_stripe(conf, sector_nr+i,
1883 conf->raid_disks, pd_idx, 0);
1884 set_bit(STRIPE_EXPANDING, &sh->state);
1885 /* If any of this stripe is beyond the end of the old
1886 * array, then we need to zero those blocks
1888 for (j=sh->disks; j--;) {
1890 if (j == sh->pd_idx)
1892 s = compute_blocknr(sh, j);
1893 if (s < (mddev->array_size<<1)) {
1897 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
1898 set_bit(R5_Expanded, &sh->dev[j].flags);
1899 set_bit(R5_UPTODATE, &sh->dev[j].flags);
1902 set_bit(STRIPE_EXPAND_READY, &sh->state);
1903 set_bit(STRIPE_HANDLE, &sh->state);
1907 spin_lock_irq(&conf->device_lock);
1908 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
1909 spin_unlock_irq(&conf->device_lock);
1910 /* Ok, those stripe are ready. We can start scheduling
1911 * reads on the source stripes.
1912 * The source stripes are determined by mapping the first and last
1913 * block on the destination stripes.
1915 raid_disks = conf->previous_raid_disks;
1916 data_disks = raid_disks - 1;
1918 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
1919 raid_disks, data_disks,
1920 &dd_idx, &pd_idx, conf);
1922 raid5_compute_sector((sector_nr+conf->chunk_size/512)
1923 *(conf->raid_disks-1) -1,
1924 raid_disks, data_disks,
1925 &dd_idx, &pd_idx, conf);
1926 if (last_sector >= (mddev->size<<1))
1927 last_sector = (mddev->size<<1)-1;
1928 while (first_sector <= last_sector) {
1929 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
1930 sh = get_active_stripe(conf, first_sector,
1931 conf->previous_raid_disks, pd_idx, 0);
1932 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1933 set_bit(STRIPE_HANDLE, &sh->state);
1935 first_sector += STRIPE_SECTORS;
1937 return conf->chunk_size>>9;
1939 /* if there is 1 or more failed drives and we are trying
1940 * to resync, then assert that we are finished, because there is
1941 * nothing we can do.
1943 if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1944 sector_t rv = (mddev->size << 1) - sector_nr;
1948 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1949 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
1950 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1951 /* we can skip this block, and probably more */
1952 sync_blocks /= STRIPE_SECTORS;
1954 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1957 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
1958 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
1960 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
1961 /* make sure we don't swamp the stripe cache if someone else
1962 * is trying to get access
1964 schedule_timeout_uninterruptible(1);
1966 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
1967 spin_lock(&sh->lock);
1968 set_bit(STRIPE_SYNCING, &sh->state);
1969 clear_bit(STRIPE_INSYNC, &sh->state);
1970 spin_unlock(&sh->lock);
1975 return STRIPE_SECTORS;
1979 * This is our raid5 kernel thread.
1981 * We scan the hash table for stripes which can be handled now.
1982 * During the scan, completed stripes are saved for us by the interrupt
1983 * handler, so that they will not have to wait for our next wakeup.
1985 static void raid5d (mddev_t *mddev)
1987 struct stripe_head *sh;
1988 raid5_conf_t *conf = mddev_to_conf(mddev);
1991 PRINTK("+++ raid5d active\n");
1993 md_check_recovery(mddev);
1996 spin_lock_irq(&conf->device_lock);
1998 struct list_head *first;
2000 if (conf->seq_flush - conf->seq_write > 0) {
2001 int seq = conf->seq_flush;
2002 spin_unlock_irq(&conf->device_lock);
2003 bitmap_unplug(mddev->bitmap);
2004 spin_lock_irq(&conf->device_lock);
2005 conf->seq_write = seq;
2006 activate_bit_delay(conf);
2009 if (list_empty(&conf->handle_list) &&
2010 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
2011 !blk_queue_plugged(mddev->queue) &&
2012 !list_empty(&conf->delayed_list))
2013 raid5_activate_delayed(conf);
2015 if (list_empty(&conf->handle_list))
2018 first = conf->handle_list.next;
2019 sh = list_entry(first, struct stripe_head, lru);
2021 list_del_init(first);
2022 atomic_inc(&sh->count);
2023 if (atomic_read(&sh->count)!= 1)
2025 spin_unlock_irq(&conf->device_lock);
2031 spin_lock_irq(&conf->device_lock);
2033 PRINTK("%d stripes handled\n", handled);
2035 spin_unlock_irq(&conf->device_lock);
2037 unplug_slaves(mddev);
2039 PRINTK("--- raid5d inactive\n");
2043 raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
2045 raid5_conf_t *conf = mddev_to_conf(mddev);
2047 return sprintf(page, "%d\n", conf->max_nr_stripes);
2053 raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
2055 raid5_conf_t *conf = mddev_to_conf(mddev);
2058 if (len >= PAGE_SIZE)
2063 new = simple_strtoul(page, &end, 10);
2064 if (!*page || (*end && *end != '\n') )
2066 if (new <= 16 || new > 32768)
2068 while (new < conf->max_nr_stripes) {
2069 if (drop_one_stripe(conf))
2070 conf->max_nr_stripes--;
2074 while (new > conf->max_nr_stripes) {
2075 if (grow_one_stripe(conf))
2076 conf->max_nr_stripes++;
2082 static struct md_sysfs_entry
2083 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
2084 raid5_show_stripe_cache_size,
2085 raid5_store_stripe_cache_size);
2088 stripe_cache_active_show(mddev_t *mddev, char *page)
2090 raid5_conf_t *conf = mddev_to_conf(mddev);
2092 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
2097 static struct md_sysfs_entry
2098 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
2100 static struct attribute *raid5_attrs[] = {
2101 &raid5_stripecache_size.attr,
2102 &raid5_stripecache_active.attr,
2105 static struct attribute_group raid5_attrs_group = {
2107 .attrs = raid5_attrs,
2110 static int run(mddev_t *mddev)
2113 int raid_disk, memory;
2115 struct disk_info *disk;
2116 struct list_head *tmp;
2118 if (mddev->level != 5 && mddev->level != 4) {
2119 printk(KERN_ERR "raid5: %s: raid level not set to 4/5 (%d)\n",
2120 mdname(mddev), mddev->level);
2124 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
2125 if ((conf = mddev->private) == NULL)
2127 conf->disks = kzalloc(mddev->raid_disks * sizeof(struct disk_info),
2132 conf->mddev = mddev;
2134 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
2137 spin_lock_init(&conf->device_lock);
2138 init_waitqueue_head(&conf->wait_for_stripe);
2139 init_waitqueue_head(&conf->wait_for_overlap);
2140 INIT_LIST_HEAD(&conf->handle_list);
2141 INIT_LIST_HEAD(&conf->delayed_list);
2142 INIT_LIST_HEAD(&conf->bitmap_list);
2143 INIT_LIST_HEAD(&conf->inactive_list);
2144 atomic_set(&conf->active_stripes, 0);
2145 atomic_set(&conf->preread_active_stripes, 0);
2147 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
2149 ITERATE_RDEV(mddev,rdev,tmp) {
2150 raid_disk = rdev->raid_disk;
2151 if (raid_disk >= mddev->raid_disks
2154 disk = conf->disks + raid_disk;
2158 if (test_bit(In_sync, &rdev->flags)) {
2159 char b[BDEVNAME_SIZE];
2160 printk(KERN_INFO "raid5: device %s operational as raid"
2161 " disk %d\n", bdevname(rdev->bdev,b),
2163 conf->working_disks++;
2167 conf->raid_disks = mddev->raid_disks;
2169 * 0 for a fully functional array, 1 for a degraded array.
2171 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
2172 conf->mddev = mddev;
2173 conf->chunk_size = mddev->chunk_size;
2174 conf->level = mddev->level;
2175 conf->algorithm = mddev->layout;
2176 conf->max_nr_stripes = NR_STRIPES;
2177 conf->expand_progress = MaxSector;
2179 /* device size must be a multiple of chunk size */
2180 mddev->size &= ~(mddev->chunk_size/1024 -1);
2181 mddev->resync_max_sectors = mddev->size << 1;
2183 if (!conf->chunk_size || conf->chunk_size % 4) {
2184 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
2185 conf->chunk_size, mdname(mddev));
2188 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
2190 "raid5: unsupported parity algorithm %d for %s\n",
2191 conf->algorithm, mdname(mddev));
2194 if (mddev->degraded > 1) {
2195 printk(KERN_ERR "raid5: not enough operational devices for %s"
2196 " (%d/%d failed)\n",
2197 mdname(mddev), conf->failed_disks, conf->raid_disks);
2201 if (mddev->degraded == 1 &&
2202 mddev->recovery_cp != MaxSector) {
2203 if (mddev->ok_start_degraded)
2205 "raid5: starting dirty degraded array: %s"
2206 "- data corruption possible.\n",
2210 "raid5: cannot start dirty degraded array for %s\n",
2217 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
2218 if (!mddev->thread) {
2220 "raid5: couldn't allocate thread for %s\n",
2225 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
2226 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
2227 if (grow_stripes(conf, conf->max_nr_stripes)) {
2229 "raid5: couldn't allocate %dkB for buffers\n", memory);
2230 shrink_stripes(conf);
2231 md_unregister_thread(mddev->thread);
2234 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
2235 memory, mdname(mddev));
2237 if (mddev->degraded == 0)
2238 printk("raid5: raid level %d set %s active with %d out of %d"
2239 " devices, algorithm %d\n", conf->level, mdname(mddev),
2240 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
2243 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
2244 " out of %d devices, algorithm %d\n", conf->level,
2245 mdname(mddev), mddev->raid_disks - mddev->degraded,
2246 mddev->raid_disks, conf->algorithm);
2248 print_raid5_conf(conf);
2250 /* read-ahead size must cover two whole stripes, which is
2251 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
2254 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
2256 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
2257 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
2260 /* Ok, everything is just fine now */
2261 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
2263 mddev->queue->unplug_fn = raid5_unplug_device;
2264 mddev->queue->issue_flush_fn = raid5_issue_flush;
2266 mddev->array_size = mddev->size * (mddev->raid_disks - 1);
2270 print_raid5_conf(conf);
2272 kfree(conf->stripe_hashtbl);
2275 mddev->private = NULL;
2276 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
2282 static int stop(mddev_t *mddev)
2284 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2286 md_unregister_thread(mddev->thread);
2287 mddev->thread = NULL;
2288 shrink_stripes(conf);
2289 kfree(conf->stripe_hashtbl);
2290 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2291 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
2294 mddev->private = NULL;
2299 static void print_sh (struct stripe_head *sh)
2303 printk("sh %llu, pd_idx %d, state %ld.\n",
2304 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2305 printk("sh %llu, count %d.\n",
2306 (unsigned long long)sh->sector, atomic_read(&sh->count));
2307 printk("sh %llu, ", (unsigned long long)sh->sector);
2308 for (i = 0; i < sh->disks; i++) {
2309 printk("(cache%d: %p %ld) ",
2310 i, sh->dev[i].page, sh->dev[i].flags);
2315 static void printall (raid5_conf_t *conf)
2317 struct stripe_head *sh;
2318 struct hlist_node *hn;
2321 spin_lock_irq(&conf->device_lock);
2322 for (i = 0; i < NR_HASH; i++) {
2323 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
2324 if (sh->raid_conf != conf)
2329 spin_unlock_irq(&conf->device_lock);
2333 static void status (struct seq_file *seq, mddev_t *mddev)
2335 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2338 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2339 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2340 for (i = 0; i < conf->raid_disks; i++)
2341 seq_printf (seq, "%s",
2342 conf->disks[i].rdev &&
2343 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
2344 seq_printf (seq, "]");
2347 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
2352 static void print_raid5_conf (raid5_conf_t *conf)
2355 struct disk_info *tmp;
2357 printk("RAID5 conf printout:\n");
2359 printk("(conf==NULL)\n");
2362 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2363 conf->working_disks, conf->failed_disks);
2365 for (i = 0; i < conf->raid_disks; i++) {
2366 char b[BDEVNAME_SIZE];
2367 tmp = conf->disks + i;
2369 printk(" disk %d, o:%d, dev:%s\n",
2370 i, !test_bit(Faulty, &tmp->rdev->flags),
2371 bdevname(tmp->rdev->bdev,b));
2375 static int raid5_spare_active(mddev_t *mddev)
2378 raid5_conf_t *conf = mddev->private;
2379 struct disk_info *tmp;
2381 for (i = 0; i < conf->raid_disks; i++) {
2382 tmp = conf->disks + i;
2384 && !test_bit(Faulty, &tmp->rdev->flags)
2385 && !test_bit(In_sync, &tmp->rdev->flags)) {
2387 conf->failed_disks--;
2388 conf->working_disks++;
2389 set_bit(In_sync, &tmp->rdev->flags);
2392 print_raid5_conf(conf);
2396 static int raid5_remove_disk(mddev_t *mddev, int number)
2398 raid5_conf_t *conf = mddev->private;
2401 struct disk_info *p = conf->disks + number;
2403 print_raid5_conf(conf);
2406 if (test_bit(In_sync, &rdev->flags) ||
2407 atomic_read(&rdev->nr_pending)) {
2413 if (atomic_read(&rdev->nr_pending)) {
2414 /* lost the race, try later */
2421 print_raid5_conf(conf);
2425 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2427 raid5_conf_t *conf = mddev->private;
2430 struct disk_info *p;
2432 if (mddev->degraded > 1)
2433 /* no point adding a device */
2439 for (disk=0; disk < mddev->raid_disks; disk++)
2440 if ((p=conf->disks + disk)->rdev == NULL) {
2441 clear_bit(In_sync, &rdev->flags);
2442 rdev->raid_disk = disk;
2444 if (rdev->saved_raid_disk != disk)
2446 rcu_assign_pointer(p->rdev, rdev);
2449 print_raid5_conf(conf);
2453 static int raid5_resize(mddev_t *mddev, sector_t sectors)
2455 /* no resync is happening, and there is enough space
2456 * on all devices, so we can resize.
2457 * We need to make sure resync covers any new space.
2458 * If the array is shrinking we should possibly wait until
2459 * any io in the removed space completes, but it hardly seems
2462 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2463 mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
2464 set_capacity(mddev->gendisk, mddev->array_size << 1);
2466 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2467 mddev->recovery_cp = mddev->size << 1;
2468 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2470 mddev->size = sectors /2;
2471 mddev->resync_max_sectors = sectors;
2475 #ifdef CONFIG_MD_RAID5_RESHAPE
2476 static int raid5_reshape(mddev_t *mddev, int raid_disks)
2478 raid5_conf_t *conf = mddev_to_conf(mddev);
2481 struct list_head *rtmp;
2483 int added_devices = 0;
2485 if (mddev->degraded ||
2486 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
2488 if (conf->raid_disks > raid_disks)
2489 return -EINVAL; /* Cannot shrink array yet */
2490 if (conf->raid_disks == raid_disks)
2491 return 0; /* nothing to do */
2493 /* Can only proceed if there are plenty of stripe_heads.
2494 * We need a minimum of one full stripe,, and for sensible progress
2495 * it is best to have about 4 times that.
2496 * If we require 4 times, then the default 256 4K stripe_heads will
2497 * allow for chunk sizes up to 256K, which is probably OK.
2498 * If the chunk size is greater, user-space should request more
2499 * stripe_heads first.
2501 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
2502 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
2503 (mddev->chunk_size / STRIPE_SIZE)*4);
2507 ITERATE_RDEV(mddev, rdev, rtmp)
2508 if (rdev->raid_disk < 0 &&
2509 !test_bit(Faulty, &rdev->flags))
2511 if (conf->raid_disks + spares < raid_disks-1)
2512 /* Not enough devices even to make a degraded array
2517 err = resize_stripes(conf, raid_disks);
2521 spin_lock_irq(&conf->device_lock);
2522 conf->previous_raid_disks = conf->raid_disks;
2523 mddev->raid_disks = conf->raid_disks = raid_disks;
2524 conf->expand_progress = 0;
2525 spin_unlock_irq(&conf->device_lock);
2527 /* Add some new drives, as many as will fit.
2528 * We know there are enough to make the newly sized array work.
2530 ITERATE_RDEV(mddev, rdev, rtmp)
2531 if (rdev->raid_disk < 0 &&
2532 !test_bit(Faulty, &rdev->flags)) {
2533 if (raid5_add_disk(mddev, rdev)) {
2535 set_bit(In_sync, &rdev->flags);
2536 conf->working_disks++;
2538 sprintf(nm, "rd%d", rdev->raid_disk);
2539 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
2544 mddev->degraded = (raid_disks - conf->previous_raid_disks) - added_devices;
2545 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
2546 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
2547 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
2548 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
2549 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
2551 if (!mddev->sync_thread) {
2552 mddev->recovery = 0;
2553 spin_lock_irq(&conf->device_lock);
2554 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
2555 conf->expand_progress = MaxSector;
2556 spin_unlock_irq(&conf->device_lock);
2559 md_wakeup_thread(mddev->sync_thread);
2560 md_new_event(mddev);
2565 static void end_reshape(raid5_conf_t *conf)
2567 struct block_device *bdev;
2569 conf->mddev->array_size = conf->mddev->size * (conf->mddev->raid_disks-1);
2570 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
2571 conf->mddev->changed = 1;
2573 bdev = bdget_disk(conf->mddev->gendisk, 0);
2575 mutex_lock(&bdev->bd_inode->i_mutex);
2576 i_size_write(bdev->bd_inode, conf->mddev->array_size << 10);
2577 mutex_unlock(&bdev->bd_inode->i_mutex);
2580 spin_lock_irq(&conf->device_lock);
2581 conf->expand_progress = MaxSector;
2582 spin_unlock_irq(&conf->device_lock);
2585 static void raid5_quiesce(mddev_t *mddev, int state)
2587 raid5_conf_t *conf = mddev_to_conf(mddev);
2590 case 1: /* stop all writes */
2591 spin_lock_irq(&conf->device_lock);
2593 wait_event_lock_irq(conf->wait_for_stripe,
2594 atomic_read(&conf->active_stripes) == 0,
2595 conf->device_lock, /* nothing */);
2596 spin_unlock_irq(&conf->device_lock);
2599 case 0: /* re-enable writes */
2600 spin_lock_irq(&conf->device_lock);
2602 wake_up(&conf->wait_for_stripe);
2603 spin_unlock_irq(&conf->device_lock);
2608 static struct mdk_personality raid5_personality =
2612 .owner = THIS_MODULE,
2613 .make_request = make_request,
2617 .error_handler = error,
2618 .hot_add_disk = raid5_add_disk,
2619 .hot_remove_disk= raid5_remove_disk,
2620 .spare_active = raid5_spare_active,
2621 .sync_request = sync_request,
2622 .resize = raid5_resize,
2623 #ifdef CONFIG_MD_RAID5_RESHAPE
2624 .reshape = raid5_reshape,
2626 .quiesce = raid5_quiesce,
2629 static struct mdk_personality raid4_personality =
2633 .owner = THIS_MODULE,
2634 .make_request = make_request,
2638 .error_handler = error,
2639 .hot_add_disk = raid5_add_disk,
2640 .hot_remove_disk= raid5_remove_disk,
2641 .spare_active = raid5_spare_active,
2642 .sync_request = sync_request,
2643 .resize = raid5_resize,
2644 .quiesce = raid5_quiesce,
2647 static int __init raid5_init(void)
2649 register_md_personality(&raid5_personality);
2650 register_md_personality(&raid4_personality);
2654 static void raid5_exit(void)
2656 unregister_md_personality(&raid5_personality);
2657 unregister_md_personality(&raid4_personality);
2660 module_init(raid5_init);
2661 module_exit(raid5_exit);
2662 MODULE_LICENSE("GPL");
2663 MODULE_ALIAS("md-personality-4"); /* RAID5 */
2664 MODULE_ALIAS("md-raid5");
2665 MODULE_ALIAS("md-raid4");
2666 MODULE_ALIAS("md-level-5");
2667 MODULE_ALIAS("md-level-4");