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_one_stripe(raid5_conf_t *conf)
298 struct stripe_head *sh;
299 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
302 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
303 sh->raid_conf = conf;
304 spin_lock_init(&sh->lock);
306 if (grow_buffers(sh, conf->raid_disks)) {
307 shrink_buffers(sh, conf->raid_disks);
308 kmem_cache_free(conf->slab_cache, sh);
311 /* we just created an active stripe so... */
312 atomic_set(&sh->count, 1);
313 atomic_inc(&conf->active_stripes);
314 INIT_LIST_HEAD(&sh->lru);
319 static int grow_stripes(raid5_conf_t *conf, int num)
322 int devs = conf->raid_disks;
324 sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev));
326 sc = kmem_cache_create(conf->cache_name,
327 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
331 conf->slab_cache = sc;
333 if (!grow_one_stripe(conf))
339 static int drop_one_stripe(raid5_conf_t *conf)
341 struct stripe_head *sh;
343 spin_lock_irq(&conf->device_lock);
344 sh = get_free_stripe(conf);
345 spin_unlock_irq(&conf->device_lock);
348 if (atomic_read(&sh->count))
350 shrink_buffers(sh, conf->raid_disks);
351 kmem_cache_free(conf->slab_cache, sh);
352 atomic_dec(&conf->active_stripes);
356 static void shrink_stripes(raid5_conf_t *conf)
358 while (drop_one_stripe(conf))
361 kmem_cache_destroy(conf->slab_cache);
362 conf->slab_cache = NULL;
365 static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
368 struct stripe_head *sh = bi->bi_private;
369 raid5_conf_t *conf = sh->raid_conf;
370 int disks = conf->raid_disks, i;
371 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
376 for (i=0 ; i<disks; i++)
377 if (bi == &sh->dev[i].req)
380 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
381 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
392 spin_lock_irqsave(&conf->device_lock, flags);
393 /* we can return a buffer if we bypassed the cache or
394 * if the top buffer is not in highmem. If there are
395 * multiple buffers, leave the extra work to
398 buffer = sh->bh_read[i];
400 (!PageHighMem(buffer->b_page)
401 || buffer->b_page == bh->b_page )
403 sh->bh_read[i] = buffer->b_reqnext;
404 buffer->b_reqnext = NULL;
407 spin_unlock_irqrestore(&conf->device_lock, flags);
408 if (sh->bh_page[i]==bh->b_page)
409 set_buffer_uptodate(bh);
411 if (buffer->b_page != bh->b_page)
412 memcpy(buffer->b_data, bh->b_data, bh->b_size);
413 buffer->b_end_io(buffer, 1);
416 set_bit(R5_UPTODATE, &sh->dev[i].flags);
418 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
419 printk("R5: read error corrected!!\n");
420 clear_bit(R5_ReadError, &sh->dev[i].flags);
421 clear_bit(R5_ReWrite, &sh->dev[i].flags);
424 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
425 if (conf->mddev->degraded) {
426 printk("R5: read error not correctable.\n");
427 clear_bit(R5_ReadError, &sh->dev[i].flags);
428 clear_bit(R5_ReWrite, &sh->dev[i].flags);
429 md_error(conf->mddev, conf->disks[i].rdev);
430 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
432 printk("R5: read error NOT corrected!!\n");
433 clear_bit(R5_ReadError, &sh->dev[i].flags);
434 clear_bit(R5_ReWrite, &sh->dev[i].flags);
435 md_error(conf->mddev, conf->disks[i].rdev);
437 set_bit(R5_ReadError, &sh->dev[i].flags);
439 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
441 /* must restore b_page before unlocking buffer... */
442 if (sh->bh_page[i] != bh->b_page) {
443 bh->b_page = sh->bh_page[i];
444 bh->b_data = page_address(bh->b_page);
445 clear_buffer_uptodate(bh);
448 clear_bit(R5_LOCKED, &sh->dev[i].flags);
449 set_bit(STRIPE_HANDLE, &sh->state);
454 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
457 struct stripe_head *sh = bi->bi_private;
458 raid5_conf_t *conf = sh->raid_conf;
459 int disks = conf->raid_disks, i;
461 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
466 for (i=0 ; i<disks; i++)
467 if (bi == &sh->dev[i].req)
470 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
471 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
478 spin_lock_irqsave(&conf->device_lock, flags);
480 md_error(conf->mddev, conf->disks[i].rdev);
482 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
484 clear_bit(R5_LOCKED, &sh->dev[i].flags);
485 set_bit(STRIPE_HANDLE, &sh->state);
486 __release_stripe(conf, sh);
487 spin_unlock_irqrestore(&conf->device_lock, flags);
492 static sector_t compute_blocknr(struct stripe_head *sh, int i);
494 static void raid5_build_block (struct stripe_head *sh, int i)
496 struct r5dev *dev = &sh->dev[i];
499 dev->req.bi_io_vec = &dev->vec;
501 dev->req.bi_max_vecs++;
502 dev->vec.bv_page = dev->page;
503 dev->vec.bv_len = STRIPE_SIZE;
504 dev->vec.bv_offset = 0;
506 dev->req.bi_sector = sh->sector;
507 dev->req.bi_private = sh;
511 dev->sector = compute_blocknr(sh, i);
514 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
516 char b[BDEVNAME_SIZE];
517 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
518 PRINTK("raid5: error called\n");
523 conf->working_disks--;
525 conf->failed_disks++;
528 * if recovery was running, make sure it aborts.
530 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
534 "raid5: Disk failure on %s, disabling device."
535 " Operation continuing on %d devices\n",
536 bdevname(rdev->bdev,b), conf->working_disks);
541 * Input: a 'big' sector number,
542 * Output: index of the data and parity disk, and the sector # in them.
544 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
545 unsigned int data_disks, unsigned int * dd_idx,
546 unsigned int * pd_idx, raid5_conf_t *conf)
549 unsigned long chunk_number;
550 unsigned int chunk_offset;
552 int sectors_per_chunk = conf->chunk_size >> 9;
554 /* First compute the information on this sector */
557 * Compute the chunk number and the sector offset inside the chunk
559 chunk_offset = sector_div(r_sector, sectors_per_chunk);
560 chunk_number = r_sector;
561 BUG_ON(r_sector != chunk_number);
564 * Compute the stripe number
566 stripe = chunk_number / data_disks;
569 * Compute the data disk and parity disk indexes inside the stripe
571 *dd_idx = chunk_number % data_disks;
574 * Select the parity disk based on the user selected algorithm.
576 if (conf->level == 4)
577 *pd_idx = data_disks;
578 else switch (conf->algorithm) {
579 case ALGORITHM_LEFT_ASYMMETRIC:
580 *pd_idx = data_disks - stripe % raid_disks;
581 if (*dd_idx >= *pd_idx)
584 case ALGORITHM_RIGHT_ASYMMETRIC:
585 *pd_idx = stripe % raid_disks;
586 if (*dd_idx >= *pd_idx)
589 case ALGORITHM_LEFT_SYMMETRIC:
590 *pd_idx = data_disks - stripe % raid_disks;
591 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
593 case ALGORITHM_RIGHT_SYMMETRIC:
594 *pd_idx = stripe % raid_disks;
595 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
598 printk("raid5: unsupported algorithm %d\n",
603 * Finally, compute the new sector number
605 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
610 static sector_t compute_blocknr(struct stripe_head *sh, int i)
612 raid5_conf_t *conf = sh->raid_conf;
613 int raid_disks = conf->raid_disks, data_disks = raid_disks - 1;
614 sector_t new_sector = sh->sector, check;
615 int sectors_per_chunk = conf->chunk_size >> 9;
618 int chunk_number, dummy1, dummy2, dd_idx = i;
621 chunk_offset = sector_div(new_sector, sectors_per_chunk);
623 BUG_ON(new_sector != stripe);
626 switch (conf->algorithm) {
627 case ALGORITHM_LEFT_ASYMMETRIC:
628 case ALGORITHM_RIGHT_ASYMMETRIC:
632 case ALGORITHM_LEFT_SYMMETRIC:
633 case ALGORITHM_RIGHT_SYMMETRIC:
636 i -= (sh->pd_idx + 1);
639 printk("raid5: unsupported algorithm %d\n",
643 chunk_number = stripe * data_disks + i;
644 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
646 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
647 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
648 printk("compute_blocknr: map not correct\n");
657 * Copy data between a page in the stripe cache, and a bio.
658 * There are no alignment or size guarantees between the page or the
659 * bio except that there is some overlap.
660 * All iovecs in the bio must be considered.
662 static void copy_data(int frombio, struct bio *bio,
666 char *pa = page_address(page);
671 if (bio->bi_sector >= sector)
672 page_offset = (signed)(bio->bi_sector - sector) * 512;
674 page_offset = (signed)(sector - bio->bi_sector) * -512;
675 bio_for_each_segment(bvl, bio, i) {
676 int len = bio_iovec_idx(bio,i)->bv_len;
680 if (page_offset < 0) {
681 b_offset = -page_offset;
682 page_offset += b_offset;
686 if (len > 0 && page_offset + len > STRIPE_SIZE)
687 clen = STRIPE_SIZE - page_offset;
691 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
693 memcpy(pa+page_offset, ba+b_offset, clen);
695 memcpy(ba+b_offset, pa+page_offset, clen);
696 __bio_kunmap_atomic(ba, KM_USER0);
698 if (clen < len) /* hit end of page */
704 #define check_xor() do { \
705 if (count == MAX_XOR_BLOCKS) { \
706 xor_block(count, STRIPE_SIZE, ptr); \
712 static void compute_block(struct stripe_head *sh, int dd_idx)
714 raid5_conf_t *conf = sh->raid_conf;
715 int i, count, disks = conf->raid_disks;
716 void *ptr[MAX_XOR_BLOCKS], *p;
718 PRINTK("compute_block, stripe %llu, idx %d\n",
719 (unsigned long long)sh->sector, dd_idx);
721 ptr[0] = page_address(sh->dev[dd_idx].page);
722 memset(ptr[0], 0, STRIPE_SIZE);
724 for (i = disks ; i--; ) {
727 p = page_address(sh->dev[i].page);
728 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
731 printk("compute_block() %d, stripe %llu, %d"
732 " not present\n", dd_idx,
733 (unsigned long long)sh->sector, i);
738 xor_block(count, STRIPE_SIZE, ptr);
739 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
742 static void compute_parity(struct stripe_head *sh, int method)
744 raid5_conf_t *conf = sh->raid_conf;
745 int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count;
746 void *ptr[MAX_XOR_BLOCKS];
749 PRINTK("compute_parity, stripe %llu, method %d\n",
750 (unsigned long long)sh->sector, method);
753 ptr[0] = page_address(sh->dev[pd_idx].page);
755 case READ_MODIFY_WRITE:
756 if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
758 for (i=disks ; i-- ;) {
761 if (sh->dev[i].towrite &&
762 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
763 ptr[count++] = page_address(sh->dev[i].page);
764 chosen = sh->dev[i].towrite;
765 sh->dev[i].towrite = NULL;
767 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
768 wake_up(&conf->wait_for_overlap);
770 if (sh->dev[i].written) BUG();
771 sh->dev[i].written = chosen;
776 case RECONSTRUCT_WRITE:
777 memset(ptr[0], 0, STRIPE_SIZE);
778 for (i= disks; i-- ;)
779 if (i!=pd_idx && sh->dev[i].towrite) {
780 chosen = sh->dev[i].towrite;
781 sh->dev[i].towrite = NULL;
783 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
784 wake_up(&conf->wait_for_overlap);
786 if (sh->dev[i].written) BUG();
787 sh->dev[i].written = chosen;
794 xor_block(count, STRIPE_SIZE, ptr);
798 for (i = disks; i--;)
799 if (sh->dev[i].written) {
800 sector_t sector = sh->dev[i].sector;
801 struct bio *wbi = sh->dev[i].written;
802 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
803 copy_data(1, wbi, sh->dev[i].page, sector);
804 wbi = r5_next_bio(wbi, sector);
807 set_bit(R5_LOCKED, &sh->dev[i].flags);
808 set_bit(R5_UPTODATE, &sh->dev[i].flags);
812 case RECONSTRUCT_WRITE:
816 ptr[count++] = page_address(sh->dev[i].page);
820 case READ_MODIFY_WRITE:
821 for (i = disks; i--;)
822 if (sh->dev[i].written) {
823 ptr[count++] = page_address(sh->dev[i].page);
828 xor_block(count, STRIPE_SIZE, ptr);
830 if (method != CHECK_PARITY) {
831 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
832 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
834 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
838 * Each stripe/dev can have one or more bion attached.
839 * toread/towrite point to the first in a chain.
840 * The bi_next chain must be in order.
842 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
845 raid5_conf_t *conf = sh->raid_conf;
848 PRINTK("adding bh b#%llu to stripe s#%llu\n",
849 (unsigned long long)bi->bi_sector,
850 (unsigned long long)sh->sector);
853 spin_lock(&sh->lock);
854 spin_lock_irq(&conf->device_lock);
856 bip = &sh->dev[dd_idx].towrite;
857 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
860 bip = &sh->dev[dd_idx].toread;
861 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
862 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
864 bip = & (*bip)->bi_next;
866 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
869 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
874 bi->bi_phys_segments ++;
875 spin_unlock_irq(&conf->device_lock);
876 spin_unlock(&sh->lock);
878 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
879 (unsigned long long)bi->bi_sector,
880 (unsigned long long)sh->sector, dd_idx);
882 if (conf->mddev->bitmap && firstwrite) {
883 sh->bm_seq = conf->seq_write;
884 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
886 set_bit(STRIPE_BIT_DELAY, &sh->state);
890 /* check if page is covered */
891 sector_t sector = sh->dev[dd_idx].sector;
892 for (bi=sh->dev[dd_idx].towrite;
893 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
894 bi && bi->bi_sector <= sector;
895 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
896 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
897 sector = bi->bi_sector + (bi->bi_size>>9);
899 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
900 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
905 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
906 spin_unlock_irq(&conf->device_lock);
907 spin_unlock(&sh->lock);
913 * handle_stripe - do things to a stripe.
915 * We lock the stripe and then examine the state of various bits
916 * to see what needs to be done.
918 * return some read request which now have data
919 * return some write requests which are safely on disc
920 * schedule a read on some buffers
921 * schedule a write of some buffers
922 * return confirmation of parity correctness
924 * Parity calculations are done inside the stripe lock
925 * buffers are taken off read_list or write_list, and bh_cache buffers
926 * get BH_Lock set before the stripe lock is released.
930 static void handle_stripe(struct stripe_head *sh)
932 raid5_conf_t *conf = sh->raid_conf;
933 int disks = conf->raid_disks;
934 struct bio *return_bi= NULL;
938 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
939 int non_overwrite = 0;
943 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
944 (unsigned long long)sh->sector, atomic_read(&sh->count),
947 spin_lock(&sh->lock);
948 clear_bit(STRIPE_HANDLE, &sh->state);
949 clear_bit(STRIPE_DELAYED, &sh->state);
951 syncing = test_bit(STRIPE_SYNCING, &sh->state);
952 /* Now to look around and see what can be done */
954 for (i=disks; i--; ) {
957 clear_bit(R5_Insync, &dev->flags);
958 clear_bit(R5_Syncio, &dev->flags);
960 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
961 i, dev->flags, dev->toread, dev->towrite, dev->written);
962 /* maybe we can reply to a read */
963 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
964 struct bio *rbi, *rbi2;
965 PRINTK("Return read for disc %d\n", i);
966 spin_lock_irq(&conf->device_lock);
969 if (test_and_clear_bit(R5_Overlap, &dev->flags))
970 wake_up(&conf->wait_for_overlap);
971 spin_unlock_irq(&conf->device_lock);
972 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
973 copy_data(0, rbi, dev->page, dev->sector);
974 rbi2 = r5_next_bio(rbi, dev->sector);
975 spin_lock_irq(&conf->device_lock);
976 if (--rbi->bi_phys_segments == 0) {
977 rbi->bi_next = return_bi;
980 spin_unlock_irq(&conf->device_lock);
985 /* now count some things */
986 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
987 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
990 if (dev->toread) to_read++;
993 if (!test_bit(R5_OVERWRITE, &dev->flags))
996 if (dev->written) written++;
997 rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
998 if (!rdev || !rdev->in_sync) {
999 /* The ReadError flag wil just be confusing now */
1000 clear_bit(R5_ReadError, &dev->flags);
1001 clear_bit(R5_ReWrite, &dev->flags);
1003 if (!rdev || !rdev->in_sync
1004 || test_bit(R5_ReadError, &dev->flags)) {
1008 set_bit(R5_Insync, &dev->flags);
1010 PRINTK("locked=%d uptodate=%d to_read=%d"
1011 " to_write=%d failed=%d failed_num=%d\n",
1012 locked, uptodate, to_read, to_write, failed, failed_num);
1013 /* check if the array has lost two devices and, if so, some requests might
1016 if (failed > 1 && to_read+to_write+written) {
1017 for (i=disks; i--; ) {
1020 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1021 mdk_rdev_t *rdev = conf->disks[i].rdev;
1022 if (rdev && rdev->in_sync)
1023 /* multiple read failures in one stripe */
1024 md_error(conf->mddev, rdev);
1027 spin_lock_irq(&conf->device_lock);
1028 /* fail all writes first */
1029 bi = sh->dev[i].towrite;
1030 sh->dev[i].towrite = NULL;
1031 if (bi) { to_write--; bitmap_end = 1; }
1033 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1034 wake_up(&conf->wait_for_overlap);
1036 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1037 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1038 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1039 if (--bi->bi_phys_segments == 0) {
1040 md_write_end(conf->mddev);
1041 bi->bi_next = return_bi;
1046 /* and fail all 'written' */
1047 bi = sh->dev[i].written;
1048 sh->dev[i].written = NULL;
1049 if (bi) bitmap_end = 1;
1050 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1051 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1052 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1053 if (--bi->bi_phys_segments == 0) {
1054 md_write_end(conf->mddev);
1055 bi->bi_next = return_bi;
1061 /* fail any reads if this device is non-operational */
1062 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1063 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1064 bi = sh->dev[i].toread;
1065 sh->dev[i].toread = NULL;
1066 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1067 wake_up(&conf->wait_for_overlap);
1069 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1070 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1071 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1072 if (--bi->bi_phys_segments == 0) {
1073 bi->bi_next = return_bi;
1079 spin_unlock_irq(&conf->device_lock);
1081 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1082 STRIPE_SECTORS, 0, 0);
1085 if (failed > 1 && syncing) {
1086 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1087 clear_bit(STRIPE_SYNCING, &sh->state);
1091 /* might be able to return some write requests if the parity block
1092 * is safe, or on a failed drive
1094 dev = &sh->dev[sh->pd_idx];
1096 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1097 test_bit(R5_UPTODATE, &dev->flags))
1098 || (failed == 1 && failed_num == sh->pd_idx))
1100 /* any written block on an uptodate or failed drive can be returned.
1101 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1102 * never LOCKED, so we don't need to test 'failed' directly.
1104 for (i=disks; i--; )
1105 if (sh->dev[i].written) {
1107 if (!test_bit(R5_LOCKED, &dev->flags) &&
1108 test_bit(R5_UPTODATE, &dev->flags) ) {
1109 /* We can return any write requests */
1110 struct bio *wbi, *wbi2;
1112 PRINTK("Return write for disc %d\n", i);
1113 spin_lock_irq(&conf->device_lock);
1115 dev->written = NULL;
1116 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1117 wbi2 = r5_next_bio(wbi, dev->sector);
1118 if (--wbi->bi_phys_segments == 0) {
1119 md_write_end(conf->mddev);
1120 wbi->bi_next = return_bi;
1125 if (dev->towrite == NULL)
1127 spin_unlock_irq(&conf->device_lock);
1129 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1131 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1136 /* Now we might consider reading some blocks, either to check/generate
1137 * parity, or to satisfy requests
1138 * or to load a block that is being partially written.
1140 if (to_read || non_overwrite || (syncing && (uptodate < disks))) {
1141 for (i=disks; i--;) {
1143 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1145 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1147 (failed && (sh->dev[failed_num].toread ||
1148 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1151 /* we would like to get this block, possibly
1152 * by computing it, but we might not be able to
1154 if (uptodate == disks-1) {
1155 PRINTK("Computing block %d\n", i);
1156 compute_block(sh, i);
1158 } else if (test_bit(R5_Insync, &dev->flags)) {
1159 set_bit(R5_LOCKED, &dev->flags);
1160 set_bit(R5_Wantread, &dev->flags);
1162 /* if I am just reading this block and we don't have
1163 a failed drive, or any pending writes then sidestep the cache */
1164 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1165 ! syncing && !failed && !to_write) {
1166 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1167 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1171 PRINTK("Reading block %d (sync=%d)\n",
1174 md_sync_acct(conf->disks[i].rdev->bdev,
1179 set_bit(STRIPE_HANDLE, &sh->state);
1182 /* now to consider writing and what else, if anything should be read */
1185 for (i=disks ; i--;) {
1186 /* would I have to read this buffer for read_modify_write */
1188 if ((dev->towrite || i == sh->pd_idx) &&
1189 (!test_bit(R5_LOCKED, &dev->flags)
1191 || sh->bh_page[i]!=bh->b_page
1194 !test_bit(R5_UPTODATE, &dev->flags)) {
1195 if (test_bit(R5_Insync, &dev->flags)
1196 /* && !(!mddev->insync && i == sh->pd_idx) */
1199 else rmw += 2*disks; /* cannot read it */
1201 /* Would I have to read this buffer for reconstruct_write */
1202 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1203 (!test_bit(R5_LOCKED, &dev->flags)
1205 || sh->bh_page[i] != bh->b_page
1208 !test_bit(R5_UPTODATE, &dev->flags)) {
1209 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1210 else rcw += 2*disks;
1213 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1214 (unsigned long long)sh->sector, rmw, rcw);
1215 set_bit(STRIPE_HANDLE, &sh->state);
1216 if (rmw < rcw && rmw > 0)
1217 /* prefer read-modify-write, but need to get some data */
1218 for (i=disks; i--;) {
1220 if ((dev->towrite || i == sh->pd_idx) &&
1221 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1222 test_bit(R5_Insync, &dev->flags)) {
1223 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1225 PRINTK("Read_old block %d for r-m-w\n", i);
1226 set_bit(R5_LOCKED, &dev->flags);
1227 set_bit(R5_Wantread, &dev->flags);
1230 set_bit(STRIPE_DELAYED, &sh->state);
1231 set_bit(STRIPE_HANDLE, &sh->state);
1235 if (rcw <= rmw && rcw > 0)
1236 /* want reconstruct write, but need to get some data */
1237 for (i=disks; i--;) {
1239 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1240 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1241 test_bit(R5_Insync, &dev->flags)) {
1242 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1244 PRINTK("Read_old block %d for Reconstruct\n", i);
1245 set_bit(R5_LOCKED, &dev->flags);
1246 set_bit(R5_Wantread, &dev->flags);
1249 set_bit(STRIPE_DELAYED, &sh->state);
1250 set_bit(STRIPE_HANDLE, &sh->state);
1254 /* now if nothing is locked, and if we have enough data, we can start a write request */
1255 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1256 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1257 PRINTK("Computing parity...\n");
1258 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1259 /* now every locked buffer is ready to be written */
1261 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1262 PRINTK("Writing block %d\n", i);
1264 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1265 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1266 || (i==sh->pd_idx && failed == 0))
1267 set_bit(STRIPE_INSYNC, &sh->state);
1269 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1270 atomic_dec(&conf->preread_active_stripes);
1271 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1272 md_wakeup_thread(conf->mddev->thread);
1277 /* maybe we need to check and possibly fix the parity for this stripe
1278 * Any reads will already have been scheduled, so we just see if enough data
1281 if (syncing && locked == 0 &&
1282 !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) {
1283 set_bit(STRIPE_HANDLE, &sh->state);
1286 if (uptodate != disks)
1288 compute_parity(sh, CHECK_PARITY);
1290 pagea = page_address(sh->dev[sh->pd_idx].page);
1291 if ((*(u32*)pagea) == 0 &&
1292 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1293 /* parity is correct (on disc, not in buffer any more) */
1294 set_bit(STRIPE_INSYNC, &sh->state);
1296 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1297 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1298 /* don't try to repair!! */
1299 set_bit(STRIPE_INSYNC, &sh->state);
1302 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1304 failed_num = sh->pd_idx;
1305 /* should be able to compute the missing block and write it to spare */
1306 if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) {
1307 if (uptodate+1 != disks)
1309 compute_block(sh, failed_num);
1312 if (uptodate != disks)
1314 dev = &sh->dev[failed_num];
1315 set_bit(R5_LOCKED, &dev->flags);
1316 set_bit(R5_Wantwrite, &dev->flags);
1317 clear_bit(STRIPE_DEGRADED, &sh->state);
1319 set_bit(STRIPE_INSYNC, &sh->state);
1320 set_bit(R5_Syncio, &dev->flags);
1323 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1324 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1325 clear_bit(STRIPE_SYNCING, &sh->state);
1328 /* If the failed drive is just a ReadError, then we might need to progress
1329 * the repair/check process
1331 if (failed == 1 && test_bit(R5_ReadError, &sh->dev[failed_num].flags)
1332 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1333 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1335 dev = &sh->dev[failed_num];
1336 if (!test_bit(R5_ReWrite, &dev->flags)) {
1337 set_bit(R5_Wantwrite, &dev->flags);
1338 set_bit(R5_ReWrite, &dev->flags);
1339 set_bit(R5_LOCKED, &dev->flags);
1341 /* let's read it back */
1342 set_bit(R5_Wantread, &dev->flags);
1343 set_bit(R5_LOCKED, &dev->flags);
1347 spin_unlock(&sh->lock);
1349 while ((bi=return_bi)) {
1350 int bytes = bi->bi_size;
1352 return_bi = bi->bi_next;
1355 bi->bi_end_io(bi, bytes, 0);
1357 for (i=disks; i-- ;) {
1361 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1363 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1368 bi = &sh->dev[i].req;
1372 bi->bi_end_io = raid5_end_write_request;
1374 bi->bi_end_io = raid5_end_read_request;
1377 rdev = rcu_dereference(conf->disks[i].rdev);
1378 if (rdev && rdev->faulty)
1381 atomic_inc(&rdev->nr_pending);
1385 if (test_bit(R5_Syncio, &sh->dev[i].flags))
1386 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1388 bi->bi_bdev = rdev->bdev;
1389 PRINTK("for %llu schedule op %ld on disc %d\n",
1390 (unsigned long long)sh->sector, bi->bi_rw, i);
1391 atomic_inc(&sh->count);
1392 bi->bi_sector = sh->sector + rdev->data_offset;
1393 bi->bi_flags = 1 << BIO_UPTODATE;
1395 bi->bi_max_vecs = 1;
1397 bi->bi_io_vec = &sh->dev[i].vec;
1398 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1399 bi->bi_io_vec[0].bv_offset = 0;
1400 bi->bi_size = STRIPE_SIZE;
1402 generic_make_request(bi);
1405 set_bit(STRIPE_DEGRADED, &sh->state);
1406 PRINTK("skip op %ld on disc %d for sector %llu\n",
1407 bi->bi_rw, i, (unsigned long long)sh->sector);
1408 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1409 set_bit(STRIPE_HANDLE, &sh->state);
1414 static inline void raid5_activate_delayed(raid5_conf_t *conf)
1416 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1417 while (!list_empty(&conf->delayed_list)) {
1418 struct list_head *l = conf->delayed_list.next;
1419 struct stripe_head *sh;
1420 sh = list_entry(l, struct stripe_head, lru);
1422 clear_bit(STRIPE_DELAYED, &sh->state);
1423 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1424 atomic_inc(&conf->preread_active_stripes);
1425 list_add_tail(&sh->lru, &conf->handle_list);
1430 static inline void activate_bit_delay(raid5_conf_t *conf)
1432 /* device_lock is held */
1433 struct list_head head;
1434 list_add(&head, &conf->bitmap_list);
1435 list_del_init(&conf->bitmap_list);
1436 while (!list_empty(&head)) {
1437 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1438 list_del_init(&sh->lru);
1439 atomic_inc(&sh->count);
1440 __release_stripe(conf, sh);
1444 static void unplug_slaves(mddev_t *mddev)
1446 raid5_conf_t *conf = mddev_to_conf(mddev);
1450 for (i=0; i<mddev->raid_disks; i++) {
1451 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1452 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
1453 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1455 atomic_inc(&rdev->nr_pending);
1458 if (r_queue->unplug_fn)
1459 r_queue->unplug_fn(r_queue);
1461 rdev_dec_pending(rdev, mddev);
1468 static void raid5_unplug_device(request_queue_t *q)
1470 mddev_t *mddev = q->queuedata;
1471 raid5_conf_t *conf = mddev_to_conf(mddev);
1472 unsigned long flags;
1474 spin_lock_irqsave(&conf->device_lock, flags);
1476 if (blk_remove_plug(q)) {
1478 raid5_activate_delayed(conf);
1480 md_wakeup_thread(mddev->thread);
1482 spin_unlock_irqrestore(&conf->device_lock, flags);
1484 unplug_slaves(mddev);
1487 static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
1488 sector_t *error_sector)
1490 mddev_t *mddev = q->queuedata;
1491 raid5_conf_t *conf = mddev_to_conf(mddev);
1495 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1496 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1497 if (rdev && !rdev->faulty) {
1498 struct block_device *bdev = rdev->bdev;
1499 request_queue_t *r_queue = bdev_get_queue(bdev);
1501 if (!r_queue->issue_flush_fn)
1504 atomic_inc(&rdev->nr_pending);
1506 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1508 rdev_dec_pending(rdev, mddev);
1517 static inline void raid5_plug_device(raid5_conf_t *conf)
1519 spin_lock_irq(&conf->device_lock);
1520 blk_plug_device(conf->mddev->queue);
1521 spin_unlock_irq(&conf->device_lock);
1524 static int make_request (request_queue_t *q, struct bio * bi)
1526 mddev_t *mddev = q->queuedata;
1527 raid5_conf_t *conf = mddev_to_conf(mddev);
1528 const unsigned int raid_disks = conf->raid_disks;
1529 const unsigned int data_disks = raid_disks - 1;
1530 unsigned int dd_idx, pd_idx;
1531 sector_t new_sector;
1532 sector_t logical_sector, last_sector;
1533 struct stripe_head *sh;
1534 const int rw = bio_data_dir(bi);
1536 if (unlikely(bio_barrier(bi))) {
1537 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1541 md_write_start(mddev, bi);
1543 disk_stat_inc(mddev->gendisk, ios[rw]);
1544 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1546 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1547 last_sector = bi->bi_sector + (bi->bi_size>>9);
1549 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1551 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1554 new_sector = raid5_compute_sector(logical_sector,
1555 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1557 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1558 (unsigned long long)new_sector,
1559 (unsigned long long)logical_sector);
1562 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1563 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1565 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1566 /* Add failed due to overlap. Flush everything
1569 raid5_unplug_device(mddev->queue);
1574 finish_wait(&conf->wait_for_overlap, &w);
1575 raid5_plug_device(conf);
1580 /* cannot get stripe for read-ahead, just give-up */
1581 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1582 finish_wait(&conf->wait_for_overlap, &w);
1587 spin_lock_irq(&conf->device_lock);
1588 if (--bi->bi_phys_segments == 0) {
1589 int bytes = bi->bi_size;
1591 if ( bio_data_dir(bi) == WRITE )
1592 md_write_end(mddev);
1594 bi->bi_end_io(bi, bytes, 0);
1596 spin_unlock_irq(&conf->device_lock);
1600 /* FIXME go_faster isn't used */
1601 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1603 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1604 struct stripe_head *sh;
1605 int sectors_per_chunk = conf->chunk_size >> 9;
1607 unsigned long stripe;
1610 sector_t first_sector;
1611 int raid_disks = conf->raid_disks;
1612 int data_disks = raid_disks-1;
1613 sector_t max_sector = mddev->size << 1;
1616 if (sector_nr >= max_sector) {
1617 /* just being told to finish up .. nothing much to do */
1618 unplug_slaves(mddev);
1620 if (mddev->curr_resync < max_sector) /* aborted */
1621 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1623 else /* compelted sync */
1625 bitmap_close_sync(mddev->bitmap);
1629 /* if there is 1 or more failed drives and we are trying
1630 * to resync, then assert that we are finished, because there is
1631 * nothing we can do.
1633 if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1634 sector_t rv = (mddev->size << 1) - sector_nr;
1638 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1639 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1640 /* we can skip this block, and probably more */
1641 sync_blocks /= STRIPE_SECTORS;
1643 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1647 chunk_offset = sector_div(x, sectors_per_chunk);
1649 BUG_ON(x != stripe);
1651 first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1652 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1653 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1655 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1656 /* make sure we don't swamp the stripe cache if someone else
1657 * is trying to get access
1659 schedule_timeout_uninterruptible(1);
1661 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
1662 spin_lock(&sh->lock);
1663 set_bit(STRIPE_SYNCING, &sh->state);
1664 clear_bit(STRIPE_INSYNC, &sh->state);
1665 spin_unlock(&sh->lock);
1670 return STRIPE_SECTORS;
1674 * This is our raid5 kernel thread.
1676 * We scan the hash table for stripes which can be handled now.
1677 * During the scan, completed stripes are saved for us by the interrupt
1678 * handler, so that they will not have to wait for our next wakeup.
1680 static void raid5d (mddev_t *mddev)
1682 struct stripe_head *sh;
1683 raid5_conf_t *conf = mddev_to_conf(mddev);
1686 PRINTK("+++ raid5d active\n");
1688 md_check_recovery(mddev);
1691 spin_lock_irq(&conf->device_lock);
1693 struct list_head *first;
1695 if (conf->seq_flush - conf->seq_write > 0) {
1696 int seq = conf->seq_flush;
1697 bitmap_unplug(mddev->bitmap);
1698 conf->seq_write = seq;
1699 activate_bit_delay(conf);
1702 if (list_empty(&conf->handle_list) &&
1703 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1704 !blk_queue_plugged(mddev->queue) &&
1705 !list_empty(&conf->delayed_list))
1706 raid5_activate_delayed(conf);
1708 if (list_empty(&conf->handle_list))
1711 first = conf->handle_list.next;
1712 sh = list_entry(first, struct stripe_head, lru);
1714 list_del_init(first);
1715 atomic_inc(&sh->count);
1716 if (atomic_read(&sh->count)!= 1)
1718 spin_unlock_irq(&conf->device_lock);
1724 spin_lock_irq(&conf->device_lock);
1726 PRINTK("%d stripes handled\n", handled);
1728 spin_unlock_irq(&conf->device_lock);
1730 unplug_slaves(mddev);
1732 PRINTK("--- raid5d inactive\n");
1735 struct raid5_sysfs_entry {
1736 struct attribute attr;
1737 ssize_t (*show)(raid5_conf_t *, char *);
1738 ssize_t (*store)(raid5_conf_t *, const char *, ssize_t);
1742 raid5_show_stripe_cache_size(raid5_conf_t *conf, char *page)
1744 return sprintf(page, "%d\n", conf->max_nr_stripes);
1748 raid5_store_stripe_cache_size(raid5_conf_t *conf, const char *page, ssize_t len)
1752 if (len >= PAGE_SIZE)
1755 new = simple_strtoul(page, &end, 10);
1756 if (!*page || (*end && *end != '\n') )
1758 if (new <= 16 || new > 32768)
1760 while (new < conf->max_nr_stripes) {
1761 if (drop_one_stripe(conf))
1762 conf->max_nr_stripes--;
1766 while (new > conf->max_nr_stripes) {
1767 if (grow_one_stripe(conf))
1768 conf->max_nr_stripes++;
1773 static struct raid5_sysfs_entry raid5_stripecache_size = {
1774 .attr = {.name = "stripe_cache_size", .mode = S_IRUGO | S_IWUSR },
1775 .show = raid5_show_stripe_cache_size,
1776 .store = raid5_store_stripe_cache_size,
1780 raid5_show_stripe_cache_active(raid5_conf_t *conf, char *page)
1782 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
1785 static struct raid5_sysfs_entry raid5_stripecache_active = {
1786 .attr = {.name = "stripe_cache_active", .mode = S_IRUGO},
1787 .show = raid5_show_stripe_cache_active,
1790 static struct attribute *raid5_default_attrs[] = {
1791 &raid5_stripecache_size.attr,
1792 &raid5_stripecache_active.attr,
1797 raid5_attr_show(struct kobject *kobj, struct attribute *attr, char *page)
1799 struct raid5_sysfs_entry *entry = container_of(attr, struct raid5_sysfs_entry, attr);
1800 raid5_conf_t *conf = container_of(kobj, raid5_conf_t, kobj);
1804 return entry->show(conf, page);
1808 raid5_attr_store(struct kobject *kobj, struct attribute *attr,
1809 const char *page, size_t length)
1811 struct raid5_sysfs_entry *entry = container_of(attr, struct raid5_sysfs_entry, attr);
1812 raid5_conf_t *conf = container_of(kobj, raid5_conf_t, kobj);
1816 return entry->store(conf, page, length);
1819 static void raid5_free(struct kobject *ko)
1821 raid5_conf_t *conf = container_of(ko, raid5_conf_t, kobj);
1826 static struct sysfs_ops raid5_sysfs_ops = {
1827 .show = raid5_attr_show,
1828 .store = raid5_attr_store,
1831 static struct kobj_type raid5_ktype = {
1832 .release = raid5_free,
1833 .sysfs_ops = &raid5_sysfs_ops,
1834 .default_attrs = raid5_default_attrs,
1837 static int run(mddev_t *mddev)
1840 int raid_disk, memory;
1842 struct disk_info *disk;
1843 struct list_head *tmp;
1845 if (mddev->level != 5 && mddev->level != 4) {
1846 printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level);
1850 mddev->private = kmalloc (sizeof (raid5_conf_t)
1851 + mddev->raid_disks * sizeof(struct disk_info),
1853 if ((conf = mddev->private) == NULL)
1855 memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1856 conf->mddev = mddev;
1858 if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1860 memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1862 spin_lock_init(&conf->device_lock);
1863 init_waitqueue_head(&conf->wait_for_stripe);
1864 init_waitqueue_head(&conf->wait_for_overlap);
1865 INIT_LIST_HEAD(&conf->handle_list);
1866 INIT_LIST_HEAD(&conf->delayed_list);
1867 INIT_LIST_HEAD(&conf->bitmap_list);
1868 INIT_LIST_HEAD(&conf->inactive_list);
1869 atomic_set(&conf->active_stripes, 0);
1870 atomic_set(&conf->preread_active_stripes, 0);
1872 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
1874 ITERATE_RDEV(mddev,rdev,tmp) {
1875 raid_disk = rdev->raid_disk;
1876 if (raid_disk >= mddev->raid_disks
1879 disk = conf->disks + raid_disk;
1883 if (rdev->in_sync) {
1884 char b[BDEVNAME_SIZE];
1885 printk(KERN_INFO "raid5: device %s operational as raid"
1886 " disk %d\n", bdevname(rdev->bdev,b),
1888 conf->working_disks++;
1892 conf->raid_disks = mddev->raid_disks;
1894 * 0 for a fully functional array, 1 for a degraded array.
1896 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1897 conf->mddev = mddev;
1898 conf->chunk_size = mddev->chunk_size;
1899 conf->level = mddev->level;
1900 conf->algorithm = mddev->layout;
1901 conf->max_nr_stripes = NR_STRIPES;
1903 /* device size must be a multiple of chunk size */
1904 mddev->size &= ~(mddev->chunk_size/1024 -1);
1905 mddev->resync_max_sectors = mddev->size << 1;
1907 if (!conf->chunk_size || conf->chunk_size % 4) {
1908 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
1909 conf->chunk_size, mdname(mddev));
1912 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1914 "raid5: unsupported parity algorithm %d for %s\n",
1915 conf->algorithm, mdname(mddev));
1918 if (mddev->degraded > 1) {
1919 printk(KERN_ERR "raid5: not enough operational devices for %s"
1920 " (%d/%d failed)\n",
1921 mdname(mddev), conf->failed_disks, conf->raid_disks);
1925 if (mddev->degraded == 1 &&
1926 mddev->recovery_cp != MaxSector) {
1928 "raid5: cannot start dirty degraded array for %s\n",
1934 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
1935 if (!mddev->thread) {
1937 "raid5: couldn't allocate thread for %s\n",
1942 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1943 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1944 if (grow_stripes(conf, conf->max_nr_stripes)) {
1946 "raid5: couldn't allocate %dkB for buffers\n", memory);
1947 shrink_stripes(conf);
1948 md_unregister_thread(mddev->thread);
1951 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
1952 memory, mdname(mddev));
1954 if (mddev->degraded == 0)
1955 printk("raid5: raid level %d set %s active with %d out of %d"
1956 " devices, algorithm %d\n", conf->level, mdname(mddev),
1957 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1960 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
1961 " out of %d devices, algorithm %d\n", conf->level,
1962 mdname(mddev), mddev->raid_disks - mddev->degraded,
1963 mddev->raid_disks, conf->algorithm);
1965 print_raid5_conf(conf);
1967 /* read-ahead size must cover two whole stripes, which is
1968 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
1971 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
1973 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1974 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1977 /* Ok, everything is just fine now */
1978 conf->kobj.parent = &mddev->kobj;
1979 strcpy(conf->kobj.name, "raid5");
1980 conf->kobj.ktype = &raid5_ktype;
1981 kobject_register(&conf->kobj);
1984 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1986 mddev->queue->unplug_fn = raid5_unplug_device;
1987 mddev->queue->issue_flush_fn = raid5_issue_flush;
1989 mddev->array_size = mddev->size * (mddev->raid_disks - 1);
1993 print_raid5_conf(conf);
1994 if (conf->stripe_hashtbl)
1995 free_pages((unsigned long) conf->stripe_hashtbl,
1999 mddev->private = NULL;
2000 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
2006 static int stop(mddev_t *mddev)
2008 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2010 md_unregister_thread(mddev->thread);
2011 mddev->thread = NULL;
2012 shrink_stripes(conf);
2013 free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
2014 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2015 kobject_unregister(&conf->kobj);
2016 mddev->private = NULL;
2021 static void print_sh (struct stripe_head *sh)
2025 printk("sh %llu, pd_idx %d, state %ld.\n",
2026 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2027 printk("sh %llu, count %d.\n",
2028 (unsigned long long)sh->sector, atomic_read(&sh->count));
2029 printk("sh %llu, ", (unsigned long long)sh->sector);
2030 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
2031 printk("(cache%d: %p %ld) ",
2032 i, sh->dev[i].page, sh->dev[i].flags);
2037 static void printall (raid5_conf_t *conf)
2039 struct stripe_head *sh;
2042 spin_lock_irq(&conf->device_lock);
2043 for (i = 0; i < NR_HASH; i++) {
2044 sh = conf->stripe_hashtbl[i];
2045 for (; sh; sh = sh->hash_next) {
2046 if (sh->raid_conf != conf)
2051 spin_unlock_irq(&conf->device_lock);
2055 static void status (struct seq_file *seq, mddev_t *mddev)
2057 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2060 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2061 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2062 for (i = 0; i < conf->raid_disks; i++)
2063 seq_printf (seq, "%s",
2064 conf->disks[i].rdev &&
2065 conf->disks[i].rdev->in_sync ? "U" : "_");
2066 seq_printf (seq, "]");
2069 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
2074 static void print_raid5_conf (raid5_conf_t *conf)
2077 struct disk_info *tmp;
2079 printk("RAID5 conf printout:\n");
2081 printk("(conf==NULL)\n");
2084 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2085 conf->working_disks, conf->failed_disks);
2087 for (i = 0; i < conf->raid_disks; i++) {
2088 char b[BDEVNAME_SIZE];
2089 tmp = conf->disks + i;
2091 printk(" disk %d, o:%d, dev:%s\n",
2092 i, !tmp->rdev->faulty,
2093 bdevname(tmp->rdev->bdev,b));
2097 static int raid5_spare_active(mddev_t *mddev)
2100 raid5_conf_t *conf = mddev->private;
2101 struct disk_info *tmp;
2103 for (i = 0; i < conf->raid_disks; i++) {
2104 tmp = conf->disks + i;
2106 && !tmp->rdev->faulty
2107 && !tmp->rdev->in_sync) {
2109 conf->failed_disks--;
2110 conf->working_disks++;
2111 tmp->rdev->in_sync = 1;
2114 print_raid5_conf(conf);
2118 static int raid5_remove_disk(mddev_t *mddev, int number)
2120 raid5_conf_t *conf = mddev->private;
2123 struct disk_info *p = conf->disks + number;
2125 print_raid5_conf(conf);
2128 if (rdev->in_sync ||
2129 atomic_read(&rdev->nr_pending)) {
2135 if (atomic_read(&rdev->nr_pending)) {
2136 /* lost the race, try later */
2143 print_raid5_conf(conf);
2147 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2149 raid5_conf_t *conf = mddev->private;
2152 struct disk_info *p;
2154 if (mddev->degraded > 1)
2155 /* no point adding a device */
2161 for (disk=0; disk < mddev->raid_disks; disk++)
2162 if ((p=conf->disks + disk)->rdev == NULL) {
2164 rdev->raid_disk = disk;
2166 if (rdev->saved_raid_disk != disk)
2168 rcu_assign_pointer(p->rdev, rdev);
2171 print_raid5_conf(conf);
2175 static int raid5_resize(mddev_t *mddev, sector_t sectors)
2177 /* no resync is happening, and there is enough space
2178 * on all devices, so we can resize.
2179 * We need to make sure resync covers any new space.
2180 * If the array is shrinking we should possibly wait until
2181 * any io in the removed space completes, but it hardly seems
2184 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2185 mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
2186 set_capacity(mddev->gendisk, mddev->array_size << 1);
2188 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2189 mddev->recovery_cp = mddev->size << 1;
2190 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2192 mddev->size = sectors /2;
2193 mddev->resync_max_sectors = sectors;
2197 static void raid5_quiesce(mddev_t *mddev, int state)
2199 raid5_conf_t *conf = mddev_to_conf(mddev);
2202 case 1: /* stop all writes */
2203 spin_lock_irq(&conf->device_lock);
2205 wait_event_lock_irq(conf->wait_for_stripe,
2206 atomic_read(&conf->active_stripes) == 0,
2207 conf->device_lock, /* nothing */);
2208 spin_unlock_irq(&conf->device_lock);
2211 case 0: /* re-enable writes */
2212 spin_lock_irq(&conf->device_lock);
2214 wake_up(&conf->wait_for_stripe);
2215 spin_unlock_irq(&conf->device_lock);
2218 if (mddev->thread) {
2220 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2222 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2223 md_wakeup_thread(mddev->thread);
2226 static mdk_personality_t raid5_personality=
2229 .owner = THIS_MODULE,
2230 .make_request = make_request,
2234 .error_handler = error,
2235 .hot_add_disk = raid5_add_disk,
2236 .hot_remove_disk= raid5_remove_disk,
2237 .spare_active = raid5_spare_active,
2238 .sync_request = sync_request,
2239 .resize = raid5_resize,
2240 .quiesce = raid5_quiesce,
2243 static int __init raid5_init (void)
2245 return register_md_personality (RAID5, &raid5_personality);
2248 static void raid5_exit (void)
2250 unregister_md_personality (RAID5);
2253 module_init(raid5_init);
2254 module_exit(raid5_exit);
2255 MODULE_LICENSE("GPL");
2256 MODULE_ALIAS("md-personality-4"); /* RAID5 */