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) < (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) < (NR_STRIPES *3/4)
268 || !conf->inactive_blocked),
270 unplug_slaves(conf->mddev);
272 conf->inactive_blocked = 0;
274 init_stripe(sh, sector, pd_idx);
276 if (atomic_read(&sh->count)) {
277 if (!list_empty(&sh->lru))
280 if (!test_bit(STRIPE_HANDLE, &sh->state))
281 atomic_inc(&conf->active_stripes);
282 if (list_empty(&sh->lru))
284 list_del_init(&sh->lru);
287 } while (sh == NULL);
290 atomic_inc(&sh->count);
292 spin_unlock_irq(&conf->device_lock);
296 static int grow_stripes(raid5_conf_t *conf, int num)
298 struct stripe_head *sh;
300 int devs = conf->raid_disks;
302 sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev));
304 sc = kmem_cache_create(conf->cache_name,
305 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
309 conf->slab_cache = sc;
311 sh = kmem_cache_alloc(sc, GFP_KERNEL);
314 memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
315 sh->raid_conf = conf;
316 spin_lock_init(&sh->lock);
318 if (grow_buffers(sh, conf->raid_disks)) {
319 shrink_buffers(sh, conf->raid_disks);
320 kmem_cache_free(sc, sh);
323 /* we just created an active stripe so... */
324 atomic_set(&sh->count, 1);
325 atomic_inc(&conf->active_stripes);
326 INIT_LIST_HEAD(&sh->lru);
332 static void shrink_stripes(raid5_conf_t *conf)
334 struct stripe_head *sh;
337 spin_lock_irq(&conf->device_lock);
338 sh = get_free_stripe(conf);
339 spin_unlock_irq(&conf->device_lock);
342 if (atomic_read(&sh->count))
344 shrink_buffers(sh, conf->raid_disks);
345 kmem_cache_free(conf->slab_cache, sh);
346 atomic_dec(&conf->active_stripes);
348 kmem_cache_destroy(conf->slab_cache);
349 conf->slab_cache = NULL;
352 static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
355 struct stripe_head *sh = bi->bi_private;
356 raid5_conf_t *conf = sh->raid_conf;
357 int disks = conf->raid_disks, i;
358 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
363 for (i=0 ; i<disks; i++)
364 if (bi == &sh->dev[i].req)
367 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
368 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
379 spin_lock_irqsave(&conf->device_lock, flags);
380 /* we can return a buffer if we bypassed the cache or
381 * if the top buffer is not in highmem. If there are
382 * multiple buffers, leave the extra work to
385 buffer = sh->bh_read[i];
387 (!PageHighMem(buffer->b_page)
388 || buffer->b_page == bh->b_page )
390 sh->bh_read[i] = buffer->b_reqnext;
391 buffer->b_reqnext = NULL;
394 spin_unlock_irqrestore(&conf->device_lock, flags);
395 if (sh->bh_page[i]==bh->b_page)
396 set_buffer_uptodate(bh);
398 if (buffer->b_page != bh->b_page)
399 memcpy(buffer->b_data, bh->b_data, bh->b_size);
400 buffer->b_end_io(buffer, 1);
403 set_bit(R5_UPTODATE, &sh->dev[i].flags);
405 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
406 printk("R5: read error corrected!!\n");
407 clear_bit(R5_ReadError, &sh->dev[i].flags);
408 clear_bit(R5_ReWrite, &sh->dev[i].flags);
411 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
412 if (conf->mddev->degraded) {
413 printk("R5: read error not correctable.\n");
414 clear_bit(R5_ReadError, &sh->dev[i].flags);
415 clear_bit(R5_ReWrite, &sh->dev[i].flags);
416 md_error(conf->mddev, conf->disks[i].rdev);
417 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
419 printk("R5: read error NOT corrected!!\n");
420 clear_bit(R5_ReadError, &sh->dev[i].flags);
421 clear_bit(R5_ReWrite, &sh->dev[i].flags);
422 md_error(conf->mddev, conf->disks[i].rdev);
424 set_bit(R5_ReadError, &sh->dev[i].flags);
426 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
428 /* must restore b_page before unlocking buffer... */
429 if (sh->bh_page[i] != bh->b_page) {
430 bh->b_page = sh->bh_page[i];
431 bh->b_data = page_address(bh->b_page);
432 clear_buffer_uptodate(bh);
435 clear_bit(R5_LOCKED, &sh->dev[i].flags);
436 set_bit(STRIPE_HANDLE, &sh->state);
441 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
444 struct stripe_head *sh = bi->bi_private;
445 raid5_conf_t *conf = sh->raid_conf;
446 int disks = conf->raid_disks, i;
448 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
453 for (i=0 ; i<disks; i++)
454 if (bi == &sh->dev[i].req)
457 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
458 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
465 spin_lock_irqsave(&conf->device_lock, flags);
467 md_error(conf->mddev, conf->disks[i].rdev);
469 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
471 clear_bit(R5_LOCKED, &sh->dev[i].flags);
472 set_bit(STRIPE_HANDLE, &sh->state);
473 __release_stripe(conf, sh);
474 spin_unlock_irqrestore(&conf->device_lock, flags);
479 static sector_t compute_blocknr(struct stripe_head *sh, int i);
481 static void raid5_build_block (struct stripe_head *sh, int i)
483 struct r5dev *dev = &sh->dev[i];
486 dev->req.bi_io_vec = &dev->vec;
488 dev->req.bi_max_vecs++;
489 dev->vec.bv_page = dev->page;
490 dev->vec.bv_len = STRIPE_SIZE;
491 dev->vec.bv_offset = 0;
493 dev->req.bi_sector = sh->sector;
494 dev->req.bi_private = sh;
498 dev->sector = compute_blocknr(sh, i);
501 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
503 char b[BDEVNAME_SIZE];
504 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
505 PRINTK("raid5: error called\n");
510 conf->working_disks--;
512 conf->failed_disks++;
515 * if recovery was running, make sure it aborts.
517 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
521 "raid5: Disk failure on %s, disabling device."
522 " Operation continuing on %d devices\n",
523 bdevname(rdev->bdev,b), conf->working_disks);
528 * Input: a 'big' sector number,
529 * Output: index of the data and parity disk, and the sector # in them.
531 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
532 unsigned int data_disks, unsigned int * dd_idx,
533 unsigned int * pd_idx, raid5_conf_t *conf)
536 unsigned long chunk_number;
537 unsigned int chunk_offset;
539 int sectors_per_chunk = conf->chunk_size >> 9;
541 /* First compute the information on this sector */
544 * Compute the chunk number and the sector offset inside the chunk
546 chunk_offset = sector_div(r_sector, sectors_per_chunk);
547 chunk_number = r_sector;
548 BUG_ON(r_sector != chunk_number);
551 * Compute the stripe number
553 stripe = chunk_number / data_disks;
556 * Compute the data disk and parity disk indexes inside the stripe
558 *dd_idx = chunk_number % data_disks;
561 * Select the parity disk based on the user selected algorithm.
563 if (conf->level == 4)
564 *pd_idx = data_disks;
565 else switch (conf->algorithm) {
566 case ALGORITHM_LEFT_ASYMMETRIC:
567 *pd_idx = data_disks - stripe % raid_disks;
568 if (*dd_idx >= *pd_idx)
571 case ALGORITHM_RIGHT_ASYMMETRIC:
572 *pd_idx = stripe % raid_disks;
573 if (*dd_idx >= *pd_idx)
576 case ALGORITHM_LEFT_SYMMETRIC:
577 *pd_idx = data_disks - stripe % raid_disks;
578 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
580 case ALGORITHM_RIGHT_SYMMETRIC:
581 *pd_idx = stripe % raid_disks;
582 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
585 printk("raid5: unsupported algorithm %d\n",
590 * Finally, compute the new sector number
592 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
597 static sector_t compute_blocknr(struct stripe_head *sh, int i)
599 raid5_conf_t *conf = sh->raid_conf;
600 int raid_disks = conf->raid_disks, data_disks = raid_disks - 1;
601 sector_t new_sector = sh->sector, check;
602 int sectors_per_chunk = conf->chunk_size >> 9;
605 int chunk_number, dummy1, dummy2, dd_idx = i;
608 chunk_offset = sector_div(new_sector, sectors_per_chunk);
610 BUG_ON(new_sector != stripe);
613 switch (conf->algorithm) {
614 case ALGORITHM_LEFT_ASYMMETRIC:
615 case ALGORITHM_RIGHT_ASYMMETRIC:
619 case ALGORITHM_LEFT_SYMMETRIC:
620 case ALGORITHM_RIGHT_SYMMETRIC:
623 i -= (sh->pd_idx + 1);
626 printk("raid5: unsupported algorithm %d\n",
630 chunk_number = stripe * data_disks + i;
631 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
633 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
634 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
635 printk("compute_blocknr: map not correct\n");
644 * Copy data between a page in the stripe cache, and a bio.
645 * There are no alignment or size guarantees between the page or the
646 * bio except that there is some overlap.
647 * All iovecs in the bio must be considered.
649 static void copy_data(int frombio, struct bio *bio,
653 char *pa = page_address(page);
658 if (bio->bi_sector >= sector)
659 page_offset = (signed)(bio->bi_sector - sector) * 512;
661 page_offset = (signed)(sector - bio->bi_sector) * -512;
662 bio_for_each_segment(bvl, bio, i) {
663 int len = bio_iovec_idx(bio,i)->bv_len;
667 if (page_offset < 0) {
668 b_offset = -page_offset;
669 page_offset += b_offset;
673 if (len > 0 && page_offset + len > STRIPE_SIZE)
674 clen = STRIPE_SIZE - page_offset;
678 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
680 memcpy(pa+page_offset, ba+b_offset, clen);
682 memcpy(ba+b_offset, pa+page_offset, clen);
683 __bio_kunmap_atomic(ba, KM_USER0);
685 if (clen < len) /* hit end of page */
691 #define check_xor() do { \
692 if (count == MAX_XOR_BLOCKS) { \
693 xor_block(count, STRIPE_SIZE, ptr); \
699 static void compute_block(struct stripe_head *sh, int dd_idx)
701 raid5_conf_t *conf = sh->raid_conf;
702 int i, count, disks = conf->raid_disks;
703 void *ptr[MAX_XOR_BLOCKS], *p;
705 PRINTK("compute_block, stripe %llu, idx %d\n",
706 (unsigned long long)sh->sector, dd_idx);
708 ptr[0] = page_address(sh->dev[dd_idx].page);
709 memset(ptr[0], 0, STRIPE_SIZE);
711 for (i = disks ; i--; ) {
714 p = page_address(sh->dev[i].page);
715 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
718 printk("compute_block() %d, stripe %llu, %d"
719 " not present\n", dd_idx,
720 (unsigned long long)sh->sector, i);
725 xor_block(count, STRIPE_SIZE, ptr);
726 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
729 static void compute_parity(struct stripe_head *sh, int method)
731 raid5_conf_t *conf = sh->raid_conf;
732 int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count;
733 void *ptr[MAX_XOR_BLOCKS];
736 PRINTK("compute_parity, stripe %llu, method %d\n",
737 (unsigned long long)sh->sector, method);
740 ptr[0] = page_address(sh->dev[pd_idx].page);
742 case READ_MODIFY_WRITE:
743 if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
745 for (i=disks ; i-- ;) {
748 if (sh->dev[i].towrite &&
749 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
750 ptr[count++] = page_address(sh->dev[i].page);
751 chosen = sh->dev[i].towrite;
752 sh->dev[i].towrite = NULL;
754 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
755 wake_up(&conf->wait_for_overlap);
757 if (sh->dev[i].written) BUG();
758 sh->dev[i].written = chosen;
763 case RECONSTRUCT_WRITE:
764 memset(ptr[0], 0, STRIPE_SIZE);
765 for (i= disks; i-- ;)
766 if (i!=pd_idx && sh->dev[i].towrite) {
767 chosen = sh->dev[i].towrite;
768 sh->dev[i].towrite = NULL;
770 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
771 wake_up(&conf->wait_for_overlap);
773 if (sh->dev[i].written) BUG();
774 sh->dev[i].written = chosen;
781 xor_block(count, STRIPE_SIZE, ptr);
785 for (i = disks; i--;)
786 if (sh->dev[i].written) {
787 sector_t sector = sh->dev[i].sector;
788 struct bio *wbi = sh->dev[i].written;
789 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
790 copy_data(1, wbi, sh->dev[i].page, sector);
791 wbi = r5_next_bio(wbi, sector);
794 set_bit(R5_LOCKED, &sh->dev[i].flags);
795 set_bit(R5_UPTODATE, &sh->dev[i].flags);
799 case RECONSTRUCT_WRITE:
803 ptr[count++] = page_address(sh->dev[i].page);
807 case READ_MODIFY_WRITE:
808 for (i = disks; i--;)
809 if (sh->dev[i].written) {
810 ptr[count++] = page_address(sh->dev[i].page);
815 xor_block(count, STRIPE_SIZE, ptr);
817 if (method != CHECK_PARITY) {
818 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
819 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
821 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
825 * Each stripe/dev can have one or more bion attached.
826 * toread/towrite point to the first in a chain.
827 * The bi_next chain must be in order.
829 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
832 raid5_conf_t *conf = sh->raid_conf;
835 PRINTK("adding bh b#%llu to stripe s#%llu\n",
836 (unsigned long long)bi->bi_sector,
837 (unsigned long long)sh->sector);
840 spin_lock(&sh->lock);
841 spin_lock_irq(&conf->device_lock);
843 bip = &sh->dev[dd_idx].towrite;
844 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
847 bip = &sh->dev[dd_idx].toread;
848 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
849 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
851 bip = & (*bip)->bi_next;
853 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
856 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
861 bi->bi_phys_segments ++;
862 spin_unlock_irq(&conf->device_lock);
863 spin_unlock(&sh->lock);
865 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
866 (unsigned long long)bi->bi_sector,
867 (unsigned long long)sh->sector, dd_idx);
869 if (conf->mddev->bitmap && firstwrite) {
870 sh->bm_seq = conf->seq_write;
871 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
873 set_bit(STRIPE_BIT_DELAY, &sh->state);
877 /* check if page is covered */
878 sector_t sector = sh->dev[dd_idx].sector;
879 for (bi=sh->dev[dd_idx].towrite;
880 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
881 bi && bi->bi_sector <= sector;
882 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
883 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
884 sector = bi->bi_sector + (bi->bi_size>>9);
886 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
887 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
892 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
893 spin_unlock_irq(&conf->device_lock);
894 spin_unlock(&sh->lock);
900 * handle_stripe - do things to a stripe.
902 * We lock the stripe and then examine the state of various bits
903 * to see what needs to be done.
905 * return some read request which now have data
906 * return some write requests which are safely on disc
907 * schedule a read on some buffers
908 * schedule a write of some buffers
909 * return confirmation of parity correctness
911 * Parity calculations are done inside the stripe lock
912 * buffers are taken off read_list or write_list, and bh_cache buffers
913 * get BH_Lock set before the stripe lock is released.
917 static void handle_stripe(struct stripe_head *sh)
919 raid5_conf_t *conf = sh->raid_conf;
920 int disks = conf->raid_disks;
921 struct bio *return_bi= NULL;
925 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
926 int non_overwrite = 0;
930 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
931 (unsigned long long)sh->sector, atomic_read(&sh->count),
934 spin_lock(&sh->lock);
935 clear_bit(STRIPE_HANDLE, &sh->state);
936 clear_bit(STRIPE_DELAYED, &sh->state);
938 syncing = test_bit(STRIPE_SYNCING, &sh->state);
939 /* Now to look around and see what can be done */
941 for (i=disks; i--; ) {
944 clear_bit(R5_Insync, &dev->flags);
945 clear_bit(R5_Syncio, &dev->flags);
947 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
948 i, dev->flags, dev->toread, dev->towrite, dev->written);
949 /* maybe we can reply to a read */
950 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
951 struct bio *rbi, *rbi2;
952 PRINTK("Return read for disc %d\n", i);
953 spin_lock_irq(&conf->device_lock);
956 if (test_and_clear_bit(R5_Overlap, &dev->flags))
957 wake_up(&conf->wait_for_overlap);
958 spin_unlock_irq(&conf->device_lock);
959 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
960 copy_data(0, rbi, dev->page, dev->sector);
961 rbi2 = r5_next_bio(rbi, dev->sector);
962 spin_lock_irq(&conf->device_lock);
963 if (--rbi->bi_phys_segments == 0) {
964 rbi->bi_next = return_bi;
967 spin_unlock_irq(&conf->device_lock);
972 /* now count some things */
973 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
974 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
977 if (dev->toread) to_read++;
980 if (!test_bit(R5_OVERWRITE, &dev->flags))
983 if (dev->written) written++;
984 rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
985 if (!rdev || !rdev->in_sync) {
986 /* The ReadError flag wil just be confusing now */
987 clear_bit(R5_ReadError, &dev->flags);
988 clear_bit(R5_ReWrite, &dev->flags);
990 if (!rdev || !rdev->in_sync
991 || test_bit(R5_ReadError, &dev->flags)) {
995 set_bit(R5_Insync, &dev->flags);
997 PRINTK("locked=%d uptodate=%d to_read=%d"
998 " to_write=%d failed=%d failed_num=%d\n",
999 locked, uptodate, to_read, to_write, failed, failed_num);
1000 /* check if the array has lost two devices and, if so, some requests might
1003 if (failed > 1 && to_read+to_write+written) {
1004 for (i=disks; i--; ) {
1007 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1008 mdk_rdev_t *rdev = conf->disks[i].rdev;
1009 if (rdev && rdev->in_sync)
1010 /* multiple read failures in one stripe */
1011 md_error(conf->mddev, rdev);
1014 spin_lock_irq(&conf->device_lock);
1015 /* fail all writes first */
1016 bi = sh->dev[i].towrite;
1017 sh->dev[i].towrite = NULL;
1018 if (bi) { to_write--; bitmap_end = 1; }
1020 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1021 wake_up(&conf->wait_for_overlap);
1023 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1024 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1025 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1026 if (--bi->bi_phys_segments == 0) {
1027 md_write_end(conf->mddev);
1028 bi->bi_next = return_bi;
1033 /* and fail all 'written' */
1034 bi = sh->dev[i].written;
1035 sh->dev[i].written = NULL;
1036 if (bi) bitmap_end = 1;
1037 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1038 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1039 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1040 if (--bi->bi_phys_segments == 0) {
1041 md_write_end(conf->mddev);
1042 bi->bi_next = return_bi;
1048 /* fail any reads if this device is non-operational */
1049 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1050 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1051 bi = sh->dev[i].toread;
1052 sh->dev[i].toread = NULL;
1053 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1054 wake_up(&conf->wait_for_overlap);
1056 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1057 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1058 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1059 if (--bi->bi_phys_segments == 0) {
1060 bi->bi_next = return_bi;
1066 spin_unlock_irq(&conf->device_lock);
1068 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1069 STRIPE_SECTORS, 0, 0);
1072 if (failed > 1 && syncing) {
1073 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1074 clear_bit(STRIPE_SYNCING, &sh->state);
1078 /* might be able to return some write requests if the parity block
1079 * is safe, or on a failed drive
1081 dev = &sh->dev[sh->pd_idx];
1083 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1084 test_bit(R5_UPTODATE, &dev->flags))
1085 || (failed == 1 && failed_num == sh->pd_idx))
1087 /* any written block on an uptodate or failed drive can be returned.
1088 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1089 * never LOCKED, so we don't need to test 'failed' directly.
1091 for (i=disks; i--; )
1092 if (sh->dev[i].written) {
1094 if (!test_bit(R5_LOCKED, &dev->flags) &&
1095 test_bit(R5_UPTODATE, &dev->flags) ) {
1096 /* We can return any write requests */
1097 struct bio *wbi, *wbi2;
1099 PRINTK("Return write for disc %d\n", i);
1100 spin_lock_irq(&conf->device_lock);
1102 dev->written = NULL;
1103 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1104 wbi2 = r5_next_bio(wbi, dev->sector);
1105 if (--wbi->bi_phys_segments == 0) {
1106 md_write_end(conf->mddev);
1107 wbi->bi_next = return_bi;
1112 if (dev->towrite == NULL)
1114 spin_unlock_irq(&conf->device_lock);
1116 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1118 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1123 /* Now we might consider reading some blocks, either to check/generate
1124 * parity, or to satisfy requests
1125 * or to load a block that is being partially written.
1127 if (to_read || non_overwrite || (syncing && (uptodate < disks))) {
1128 for (i=disks; i--;) {
1130 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1132 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1134 (failed && (sh->dev[failed_num].toread ||
1135 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1138 /* we would like to get this block, possibly
1139 * by computing it, but we might not be able to
1141 if (uptodate == disks-1) {
1142 PRINTK("Computing block %d\n", i);
1143 compute_block(sh, i);
1145 } else if (test_bit(R5_Insync, &dev->flags)) {
1146 set_bit(R5_LOCKED, &dev->flags);
1147 set_bit(R5_Wantread, &dev->flags);
1149 /* if I am just reading this block and we don't have
1150 a failed drive, or any pending writes then sidestep the cache */
1151 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1152 ! syncing && !failed && !to_write) {
1153 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1154 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1158 PRINTK("Reading block %d (sync=%d)\n",
1161 md_sync_acct(conf->disks[i].rdev->bdev,
1166 set_bit(STRIPE_HANDLE, &sh->state);
1169 /* now to consider writing and what else, if anything should be read */
1172 for (i=disks ; i--;) {
1173 /* would I have to read this buffer for read_modify_write */
1175 if ((dev->towrite || i == sh->pd_idx) &&
1176 (!test_bit(R5_LOCKED, &dev->flags)
1178 || sh->bh_page[i]!=bh->b_page
1181 !test_bit(R5_UPTODATE, &dev->flags)) {
1182 if (test_bit(R5_Insync, &dev->flags)
1183 /* && !(!mddev->insync && i == sh->pd_idx) */
1186 else rmw += 2*disks; /* cannot read it */
1188 /* Would I have to read this buffer for reconstruct_write */
1189 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1190 (!test_bit(R5_LOCKED, &dev->flags)
1192 || sh->bh_page[i] != bh->b_page
1195 !test_bit(R5_UPTODATE, &dev->flags)) {
1196 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1197 else rcw += 2*disks;
1200 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1201 (unsigned long long)sh->sector, rmw, rcw);
1202 set_bit(STRIPE_HANDLE, &sh->state);
1203 if (rmw < rcw && rmw > 0)
1204 /* prefer read-modify-write, but need to get some data */
1205 for (i=disks; i--;) {
1207 if ((dev->towrite || i == sh->pd_idx) &&
1208 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1209 test_bit(R5_Insync, &dev->flags)) {
1210 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1212 PRINTK("Read_old block %d for r-m-w\n", i);
1213 set_bit(R5_LOCKED, &dev->flags);
1214 set_bit(R5_Wantread, &dev->flags);
1217 set_bit(STRIPE_DELAYED, &sh->state);
1218 set_bit(STRIPE_HANDLE, &sh->state);
1222 if (rcw <= rmw && rcw > 0)
1223 /* want reconstruct write, but need to get some data */
1224 for (i=disks; i--;) {
1226 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1227 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1228 test_bit(R5_Insync, &dev->flags)) {
1229 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1231 PRINTK("Read_old block %d for Reconstruct\n", i);
1232 set_bit(R5_LOCKED, &dev->flags);
1233 set_bit(R5_Wantread, &dev->flags);
1236 set_bit(STRIPE_DELAYED, &sh->state);
1237 set_bit(STRIPE_HANDLE, &sh->state);
1241 /* now if nothing is locked, and if we have enough data, we can start a write request */
1242 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1243 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1244 PRINTK("Computing parity...\n");
1245 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1246 /* now every locked buffer is ready to be written */
1248 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1249 PRINTK("Writing block %d\n", i);
1251 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1252 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1253 || (i==sh->pd_idx && failed == 0))
1254 set_bit(STRIPE_INSYNC, &sh->state);
1256 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1257 atomic_dec(&conf->preread_active_stripes);
1258 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1259 md_wakeup_thread(conf->mddev->thread);
1264 /* maybe we need to check and possibly fix the parity for this stripe
1265 * Any reads will already have been scheduled, so we just see if enough data
1268 if (syncing && locked == 0 &&
1269 !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) {
1270 set_bit(STRIPE_HANDLE, &sh->state);
1273 if (uptodate != disks)
1275 compute_parity(sh, CHECK_PARITY);
1277 pagea = page_address(sh->dev[sh->pd_idx].page);
1278 if ((*(u32*)pagea) == 0 &&
1279 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1280 /* parity is correct (on disc, not in buffer any more) */
1281 set_bit(STRIPE_INSYNC, &sh->state);
1284 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1286 failed_num = sh->pd_idx;
1287 /* should be able to compute the missing block and write it to spare */
1288 if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) {
1289 if (uptodate+1 != disks)
1291 compute_block(sh, failed_num);
1294 if (uptodate != disks)
1296 dev = &sh->dev[failed_num];
1297 set_bit(R5_LOCKED, &dev->flags);
1298 set_bit(R5_Wantwrite, &dev->flags);
1299 clear_bit(STRIPE_DEGRADED, &sh->state);
1301 set_bit(STRIPE_INSYNC, &sh->state);
1302 set_bit(R5_Syncio, &dev->flags);
1305 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1306 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1307 clear_bit(STRIPE_SYNCING, &sh->state);
1310 /* If the failed drive is just a ReadError, then we might need to progress
1311 * the repair/check process
1313 if (failed == 1 && test_bit(R5_ReadError, &sh->dev[failed_num].flags)
1314 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1315 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1317 dev = &sh->dev[failed_num];
1318 if (!test_bit(R5_ReWrite, &dev->flags)) {
1319 set_bit(R5_Wantwrite, &dev->flags);
1320 set_bit(R5_ReWrite, &dev->flags);
1321 set_bit(R5_LOCKED, &dev->flags);
1323 /* let's read it back */
1324 set_bit(R5_Wantread, &dev->flags);
1325 set_bit(R5_LOCKED, &dev->flags);
1329 spin_unlock(&sh->lock);
1331 while ((bi=return_bi)) {
1332 int bytes = bi->bi_size;
1334 return_bi = bi->bi_next;
1337 bi->bi_end_io(bi, bytes, 0);
1339 for (i=disks; i-- ;) {
1343 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1345 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1350 bi = &sh->dev[i].req;
1354 bi->bi_end_io = raid5_end_write_request;
1356 bi->bi_end_io = raid5_end_read_request;
1359 rdev = conf->disks[i].rdev;
1360 if (rdev && rdev->faulty)
1363 atomic_inc(&rdev->nr_pending);
1367 if (test_bit(R5_Syncio, &sh->dev[i].flags))
1368 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1370 bi->bi_bdev = rdev->bdev;
1371 PRINTK("for %llu schedule op %ld on disc %d\n",
1372 (unsigned long long)sh->sector, bi->bi_rw, i);
1373 atomic_inc(&sh->count);
1374 bi->bi_sector = sh->sector + rdev->data_offset;
1375 bi->bi_flags = 1 << BIO_UPTODATE;
1377 bi->bi_max_vecs = 1;
1379 bi->bi_io_vec = &sh->dev[i].vec;
1380 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1381 bi->bi_io_vec[0].bv_offset = 0;
1382 bi->bi_size = STRIPE_SIZE;
1384 generic_make_request(bi);
1387 set_bit(STRIPE_DEGRADED, &sh->state);
1388 PRINTK("skip op %ld on disc %d for sector %llu\n",
1389 bi->bi_rw, i, (unsigned long long)sh->sector);
1390 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1391 set_bit(STRIPE_HANDLE, &sh->state);
1396 static inline void raid5_activate_delayed(raid5_conf_t *conf)
1398 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1399 while (!list_empty(&conf->delayed_list)) {
1400 struct list_head *l = conf->delayed_list.next;
1401 struct stripe_head *sh;
1402 sh = list_entry(l, struct stripe_head, lru);
1404 clear_bit(STRIPE_DELAYED, &sh->state);
1405 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1406 atomic_inc(&conf->preread_active_stripes);
1407 list_add_tail(&sh->lru, &conf->handle_list);
1412 static inline void activate_bit_delay(raid5_conf_t *conf)
1414 /* device_lock is held */
1415 struct list_head head;
1416 list_add(&head, &conf->bitmap_list);
1417 list_del_init(&conf->bitmap_list);
1418 while (!list_empty(&head)) {
1419 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1420 list_del_init(&sh->lru);
1421 atomic_inc(&sh->count);
1422 __release_stripe(conf, sh);
1426 static void unplug_slaves(mddev_t *mddev)
1428 raid5_conf_t *conf = mddev_to_conf(mddev);
1432 for (i=0; i<mddev->raid_disks; i++) {
1433 mdk_rdev_t *rdev = conf->disks[i].rdev;
1434 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
1435 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1437 atomic_inc(&rdev->nr_pending);
1440 if (r_queue->unplug_fn)
1441 r_queue->unplug_fn(r_queue);
1443 rdev_dec_pending(rdev, mddev);
1450 static void raid5_unplug_device(request_queue_t *q)
1452 mddev_t *mddev = q->queuedata;
1453 raid5_conf_t *conf = mddev_to_conf(mddev);
1454 unsigned long flags;
1456 spin_lock_irqsave(&conf->device_lock, flags);
1458 if (blk_remove_plug(q)) {
1460 raid5_activate_delayed(conf);
1462 md_wakeup_thread(mddev->thread);
1464 spin_unlock_irqrestore(&conf->device_lock, flags);
1466 unplug_slaves(mddev);
1469 static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
1470 sector_t *error_sector)
1472 mddev_t *mddev = q->queuedata;
1473 raid5_conf_t *conf = mddev_to_conf(mddev);
1477 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1478 mdk_rdev_t *rdev = conf->disks[i].rdev;
1479 if (rdev && !rdev->faulty) {
1480 struct block_device *bdev = rdev->bdev;
1481 request_queue_t *r_queue = bdev_get_queue(bdev);
1483 if (!r_queue->issue_flush_fn)
1486 atomic_inc(&rdev->nr_pending);
1488 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1490 rdev_dec_pending(rdev, mddev);
1499 static inline void raid5_plug_device(raid5_conf_t *conf)
1501 spin_lock_irq(&conf->device_lock);
1502 blk_plug_device(conf->mddev->queue);
1503 spin_unlock_irq(&conf->device_lock);
1506 static int make_request (request_queue_t *q, struct bio * bi)
1508 mddev_t *mddev = q->queuedata;
1509 raid5_conf_t *conf = mddev_to_conf(mddev);
1510 const unsigned int raid_disks = conf->raid_disks;
1511 const unsigned int data_disks = raid_disks - 1;
1512 unsigned int dd_idx, pd_idx;
1513 sector_t new_sector;
1514 sector_t logical_sector, last_sector;
1515 struct stripe_head *sh;
1516 const int rw = bio_data_dir(bi);
1518 if (unlikely(bio_barrier(bi))) {
1519 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1523 md_write_start(mddev, bi);
1525 disk_stat_inc(mddev->gendisk, ios[rw]);
1526 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1528 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1529 last_sector = bi->bi_sector + (bi->bi_size>>9);
1531 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1533 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1536 new_sector = raid5_compute_sector(logical_sector,
1537 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1539 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1540 (unsigned long long)new_sector,
1541 (unsigned long long)logical_sector);
1544 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1545 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1547 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1548 /* Add failed due to overlap. Flush everything
1551 raid5_unplug_device(mddev->queue);
1556 finish_wait(&conf->wait_for_overlap, &w);
1557 raid5_plug_device(conf);
1562 /* cannot get stripe for read-ahead, just give-up */
1563 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1564 finish_wait(&conf->wait_for_overlap, &w);
1569 spin_lock_irq(&conf->device_lock);
1570 if (--bi->bi_phys_segments == 0) {
1571 int bytes = bi->bi_size;
1573 if ( bio_data_dir(bi) == WRITE )
1574 md_write_end(mddev);
1576 bi->bi_end_io(bi, bytes, 0);
1578 spin_unlock_irq(&conf->device_lock);
1582 /* FIXME go_faster isn't used */
1583 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1585 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1586 struct stripe_head *sh;
1587 int sectors_per_chunk = conf->chunk_size >> 9;
1589 unsigned long stripe;
1592 sector_t first_sector;
1593 int raid_disks = conf->raid_disks;
1594 int data_disks = raid_disks-1;
1595 sector_t max_sector = mddev->size << 1;
1598 if (sector_nr >= max_sector) {
1599 /* just being told to finish up .. nothing much to do */
1600 unplug_slaves(mddev);
1602 if (mddev->curr_resync < max_sector) /* aborted */
1603 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1605 else /* compelted sync */
1607 bitmap_close_sync(mddev->bitmap);
1611 /* if there is 1 or more failed drives and we are trying
1612 * to resync, then assert that we are finished, because there is
1613 * nothing we can do.
1615 if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1616 sector_t rv = (mddev->size << 1) - sector_nr;
1620 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1621 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1622 /* we can skip this block, and probably more */
1623 sync_blocks /= STRIPE_SECTORS;
1625 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1629 chunk_offset = sector_div(x, sectors_per_chunk);
1631 BUG_ON(x != stripe);
1633 first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1634 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1635 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1637 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1638 /* make sure we don't swamp the stripe cache if someone else
1639 * is trying to get access
1641 schedule_timeout_uninterruptible(1);
1643 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
1644 spin_lock(&sh->lock);
1645 set_bit(STRIPE_SYNCING, &sh->state);
1646 clear_bit(STRIPE_INSYNC, &sh->state);
1647 spin_unlock(&sh->lock);
1652 return STRIPE_SECTORS;
1656 * This is our raid5 kernel thread.
1658 * We scan the hash table for stripes which can be handled now.
1659 * During the scan, completed stripes are saved for us by the interrupt
1660 * handler, so that they will not have to wait for our next wakeup.
1662 static void raid5d (mddev_t *mddev)
1664 struct stripe_head *sh;
1665 raid5_conf_t *conf = mddev_to_conf(mddev);
1668 PRINTK("+++ raid5d active\n");
1670 md_check_recovery(mddev);
1673 spin_lock_irq(&conf->device_lock);
1675 struct list_head *first;
1677 if (conf->seq_flush - conf->seq_write > 0) {
1678 int seq = conf->seq_flush;
1679 bitmap_unplug(mddev->bitmap);
1680 conf->seq_write = seq;
1681 activate_bit_delay(conf);
1684 if (list_empty(&conf->handle_list) &&
1685 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1686 !blk_queue_plugged(mddev->queue) &&
1687 !list_empty(&conf->delayed_list))
1688 raid5_activate_delayed(conf);
1690 if (list_empty(&conf->handle_list))
1693 first = conf->handle_list.next;
1694 sh = list_entry(first, struct stripe_head, lru);
1696 list_del_init(first);
1697 atomic_inc(&sh->count);
1698 if (atomic_read(&sh->count)!= 1)
1700 spin_unlock_irq(&conf->device_lock);
1706 spin_lock_irq(&conf->device_lock);
1708 PRINTK("%d stripes handled\n", handled);
1710 spin_unlock_irq(&conf->device_lock);
1712 unplug_slaves(mddev);
1714 PRINTK("--- raid5d inactive\n");
1717 static int run(mddev_t *mddev)
1720 int raid_disk, memory;
1722 struct disk_info *disk;
1723 struct list_head *tmp;
1725 if (mddev->level != 5 && mddev->level != 4) {
1726 printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level);
1730 mddev->private = kmalloc (sizeof (raid5_conf_t)
1731 + mddev->raid_disks * sizeof(struct disk_info),
1733 if ((conf = mddev->private) == NULL)
1735 memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1736 conf->mddev = mddev;
1738 if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1740 memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1742 spin_lock_init(&conf->device_lock);
1743 init_waitqueue_head(&conf->wait_for_stripe);
1744 init_waitqueue_head(&conf->wait_for_overlap);
1745 INIT_LIST_HEAD(&conf->handle_list);
1746 INIT_LIST_HEAD(&conf->delayed_list);
1747 INIT_LIST_HEAD(&conf->bitmap_list);
1748 INIT_LIST_HEAD(&conf->inactive_list);
1749 atomic_set(&conf->active_stripes, 0);
1750 atomic_set(&conf->preread_active_stripes, 0);
1752 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
1754 ITERATE_RDEV(mddev,rdev,tmp) {
1755 raid_disk = rdev->raid_disk;
1756 if (raid_disk >= mddev->raid_disks
1759 disk = conf->disks + raid_disk;
1763 if (rdev->in_sync) {
1764 char b[BDEVNAME_SIZE];
1765 printk(KERN_INFO "raid5: device %s operational as raid"
1766 " disk %d\n", bdevname(rdev->bdev,b),
1768 conf->working_disks++;
1772 conf->raid_disks = mddev->raid_disks;
1774 * 0 for a fully functional array, 1 for a degraded array.
1776 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1777 conf->mddev = mddev;
1778 conf->chunk_size = mddev->chunk_size;
1779 conf->level = mddev->level;
1780 conf->algorithm = mddev->layout;
1781 conf->max_nr_stripes = NR_STRIPES;
1783 /* device size must be a multiple of chunk size */
1784 mddev->size &= ~(mddev->chunk_size/1024 -1);
1785 mddev->resync_max_sectors = mddev->size << 1;
1787 if (!conf->chunk_size || conf->chunk_size % 4) {
1788 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
1789 conf->chunk_size, mdname(mddev));
1792 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1794 "raid5: unsupported parity algorithm %d for %s\n",
1795 conf->algorithm, mdname(mddev));
1798 if (mddev->degraded > 1) {
1799 printk(KERN_ERR "raid5: not enough operational devices for %s"
1800 " (%d/%d failed)\n",
1801 mdname(mddev), conf->failed_disks, conf->raid_disks);
1805 if (mddev->degraded == 1 &&
1806 mddev->recovery_cp != MaxSector) {
1808 "raid5: cannot start dirty degraded array for %s\n",
1814 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
1815 if (!mddev->thread) {
1817 "raid5: couldn't allocate thread for %s\n",
1822 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1823 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1824 if (grow_stripes(conf, conf->max_nr_stripes)) {
1826 "raid5: couldn't allocate %dkB for buffers\n", memory);
1827 shrink_stripes(conf);
1828 md_unregister_thread(mddev->thread);
1831 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
1832 memory, mdname(mddev));
1834 if (mddev->degraded == 0)
1835 printk("raid5: raid level %d set %s active with %d out of %d"
1836 " devices, algorithm %d\n", conf->level, mdname(mddev),
1837 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1840 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
1841 " out of %d devices, algorithm %d\n", conf->level,
1842 mdname(mddev), mddev->raid_disks - mddev->degraded,
1843 mddev->raid_disks, conf->algorithm);
1845 print_raid5_conf(conf);
1847 /* read-ahead size must cover two whole stripes, which is
1848 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
1851 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
1853 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1854 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1857 /* Ok, everything is just fine now */
1860 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1862 mddev->queue->unplug_fn = raid5_unplug_device;
1863 mddev->queue->issue_flush_fn = raid5_issue_flush;
1865 mddev->array_size = mddev->size * (mddev->raid_disks - 1);
1869 print_raid5_conf(conf);
1870 if (conf->stripe_hashtbl)
1871 free_pages((unsigned long) conf->stripe_hashtbl,
1875 mddev->private = NULL;
1876 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
1882 static int stop (mddev_t *mddev)
1884 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1886 md_unregister_thread(mddev->thread);
1887 mddev->thread = NULL;
1888 shrink_stripes(conf);
1889 free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
1890 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1892 mddev->private = NULL;
1897 static void print_sh (struct stripe_head *sh)
1901 printk("sh %llu, pd_idx %d, state %ld.\n",
1902 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
1903 printk("sh %llu, count %d.\n",
1904 (unsigned long long)sh->sector, atomic_read(&sh->count));
1905 printk("sh %llu, ", (unsigned long long)sh->sector);
1906 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
1907 printk("(cache%d: %p %ld) ",
1908 i, sh->dev[i].page, sh->dev[i].flags);
1913 static void printall (raid5_conf_t *conf)
1915 struct stripe_head *sh;
1918 spin_lock_irq(&conf->device_lock);
1919 for (i = 0; i < NR_HASH; i++) {
1920 sh = conf->stripe_hashtbl[i];
1921 for (; sh; sh = sh->hash_next) {
1922 if (sh->raid_conf != conf)
1927 spin_unlock_irq(&conf->device_lock);
1931 static void status (struct seq_file *seq, mddev_t *mddev)
1933 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1936 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
1937 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
1938 for (i = 0; i < conf->raid_disks; i++)
1939 seq_printf (seq, "%s",
1940 conf->disks[i].rdev &&
1941 conf->disks[i].rdev->in_sync ? "U" : "_");
1942 seq_printf (seq, "]");
1945 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
1950 static void print_raid5_conf (raid5_conf_t *conf)
1953 struct disk_info *tmp;
1955 printk("RAID5 conf printout:\n");
1957 printk("(conf==NULL)\n");
1960 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
1961 conf->working_disks, conf->failed_disks);
1963 for (i = 0; i < conf->raid_disks; i++) {
1964 char b[BDEVNAME_SIZE];
1965 tmp = conf->disks + i;
1967 printk(" disk %d, o:%d, dev:%s\n",
1968 i, !tmp->rdev->faulty,
1969 bdevname(tmp->rdev->bdev,b));
1973 static int raid5_spare_active(mddev_t *mddev)
1976 raid5_conf_t *conf = mddev->private;
1977 struct disk_info *tmp;
1979 for (i = 0; i < conf->raid_disks; i++) {
1980 tmp = conf->disks + i;
1982 && !tmp->rdev->faulty
1983 && !tmp->rdev->in_sync) {
1985 conf->failed_disks--;
1986 conf->working_disks++;
1987 tmp->rdev->in_sync = 1;
1990 print_raid5_conf(conf);
1994 static int raid5_remove_disk(mddev_t *mddev, int number)
1996 raid5_conf_t *conf = mddev->private;
1999 struct disk_info *p = conf->disks + number;
2001 print_raid5_conf(conf);
2004 if (rdev->in_sync ||
2005 atomic_read(&rdev->nr_pending)) {
2011 if (atomic_read(&rdev->nr_pending)) {
2012 /* lost the race, try later */
2019 print_raid5_conf(conf);
2023 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2025 raid5_conf_t *conf = mddev->private;
2028 struct disk_info *p;
2030 if (mddev->degraded > 1)
2031 /* no point adding a device */
2037 for (disk=0; disk < mddev->raid_disks; disk++)
2038 if ((p=conf->disks + disk)->rdev == NULL) {
2040 rdev->raid_disk = disk;
2042 if (rdev->saved_raid_disk != disk)
2047 print_raid5_conf(conf);
2051 static int raid5_resize(mddev_t *mddev, sector_t sectors)
2053 /* no resync is happening, and there is enough space
2054 * on all devices, so we can resize.
2055 * We need to make sure resync covers any new space.
2056 * If the array is shrinking we should possibly wait until
2057 * any io in the removed space completes, but it hardly seems
2060 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2061 mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
2062 set_capacity(mddev->gendisk, mddev->array_size << 1);
2064 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2065 mddev->recovery_cp = mddev->size << 1;
2066 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2068 mddev->size = sectors /2;
2069 mddev->resync_max_sectors = sectors;
2073 static void raid5_quiesce(mddev_t *mddev, int state)
2075 raid5_conf_t *conf = mddev_to_conf(mddev);
2078 case 1: /* stop all writes */
2079 spin_lock_irq(&conf->device_lock);
2081 wait_event_lock_irq(conf->wait_for_stripe,
2082 atomic_read(&conf->active_stripes) == 0,
2083 conf->device_lock, /* nothing */);
2084 spin_unlock_irq(&conf->device_lock);
2087 case 0: /* re-enable writes */
2088 spin_lock_irq(&conf->device_lock);
2090 wake_up(&conf->wait_for_stripe);
2091 spin_unlock_irq(&conf->device_lock);
2094 if (mddev->thread) {
2096 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2098 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2099 md_wakeup_thread(mddev->thread);
2102 static mdk_personality_t raid5_personality=
2105 .owner = THIS_MODULE,
2106 .make_request = make_request,
2110 .error_handler = error,
2111 .hot_add_disk = raid5_add_disk,
2112 .hot_remove_disk= raid5_remove_disk,
2113 .spare_active = raid5_spare_active,
2114 .sync_request = sync_request,
2115 .resize = raid5_resize,
2116 .quiesce = raid5_quiesce,
2119 static int __init raid5_init (void)
2121 return register_md_personality (RAID5, &raid5_personality);
2124 static void raid5_exit (void)
2126 unregister_md_personality (RAID5);
2129 module_init(raid5_init);
2130 module_exit(raid5_exit);
2131 MODULE_LICENSE("GPL");
2132 MODULE_ALIAS("md-personality-4"); /* RAID5 */