[PATCH] shared mount handling: bind and rbind
[linux-2.6/suspend2-2.6.18.git] / drivers / md / raid6main.c
blob7757869477019ea5a87a186b6b46ced5fcb1fcd7
1 /*
2 * raid6main.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
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-6 management functions. This code is derived from raid5.c.
8 * Last merge from raid5.c bkcvs version 1.79 (kernel 2.6.1).
10 * Thanks to Penguin Computing for making the RAID-6 development possible
11 * by donating a test server!
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2, or (at your option)
16 * any later version.
18 * You should have received a copy of the GNU General Public License
19 * (for example /usr/src/linux/COPYING); if not, write to the Free
20 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include <linux/config.h>
25 #include <linux/module.h>
26 #include <linux/slab.h>
27 #include <linux/highmem.h>
28 #include <linux/bitops.h>
29 #include <asm/atomic.h>
30 #include "raid6.h"
32 #include <linux/raid/bitmap.h>
35 * Stripe cache
38 #define NR_STRIPES 256
39 #define STRIPE_SIZE PAGE_SIZE
40 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
41 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
42 #define IO_THRESHOLD 1
43 #define HASH_PAGES 1
44 #define HASH_PAGES_ORDER 0
45 #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
46 #define HASH_MASK (NR_HASH - 1)
48 #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
50 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
51 * order without overlap. There may be several bio's per stripe+device, and
52 * a bio could span several devices.
53 * When walking this list for a particular stripe+device, we must never proceed
54 * beyond a bio that extends past this device, as the next bio might no longer
55 * be valid.
56 * This macro is used to determine the 'next' bio in the list, given the sector
57 * of the current stripe+device
59 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
61 * The following can be used to debug the driver
63 #define RAID6_DEBUG 0 /* Extremely verbose printk */
64 #define RAID6_PARANOIA 1 /* Check spinlocks */
65 #define RAID6_DUMPSTATE 0 /* Include stripe cache state in /proc/mdstat */
66 #if RAID6_PARANOIA && defined(CONFIG_SMP)
67 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
68 #else
69 # define CHECK_DEVLOCK()
70 #endif
72 #define PRINTK(x...) ((void)(RAID6_DEBUG && printk(KERN_DEBUG x)))
73 #if RAID6_DEBUG
74 #undef inline
75 #undef __inline__
76 #define inline
77 #define __inline__
78 #endif
80 #if !RAID6_USE_EMPTY_ZERO_PAGE
81 /* In .bss so it's zeroed */
82 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
83 #endif
85 static inline int raid6_next_disk(int disk, int raid_disks)
87 disk++;
88 return (disk < raid_disks) ? disk : 0;
91 static void print_raid6_conf (raid6_conf_t *conf);
93 static inline void __release_stripe(raid6_conf_t *conf, struct stripe_head *sh)
95 if (atomic_dec_and_test(&sh->count)) {
96 if (!list_empty(&sh->lru))
97 BUG();
98 if (atomic_read(&conf->active_stripes)==0)
99 BUG();
100 if (test_bit(STRIPE_HANDLE, &sh->state)) {
101 if (test_bit(STRIPE_DELAYED, &sh->state))
102 list_add_tail(&sh->lru, &conf->delayed_list);
103 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
104 conf->seq_write == sh->bm_seq)
105 list_add_tail(&sh->lru, &conf->bitmap_list);
106 else {
107 clear_bit(STRIPE_BIT_DELAY, &sh->state);
108 list_add_tail(&sh->lru, &conf->handle_list);
110 md_wakeup_thread(conf->mddev->thread);
111 } else {
112 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
113 atomic_dec(&conf->preread_active_stripes);
114 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
115 md_wakeup_thread(conf->mddev->thread);
117 list_add_tail(&sh->lru, &conf->inactive_list);
118 atomic_dec(&conf->active_stripes);
119 if (!conf->inactive_blocked ||
120 atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
121 wake_up(&conf->wait_for_stripe);
125 static void release_stripe(struct stripe_head *sh)
127 raid6_conf_t *conf = sh->raid_conf;
128 unsigned long flags;
130 spin_lock_irqsave(&conf->device_lock, flags);
131 __release_stripe(conf, sh);
132 spin_unlock_irqrestore(&conf->device_lock, flags);
135 static void remove_hash(struct stripe_head *sh)
137 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
139 if (sh->hash_pprev) {
140 if (sh->hash_next)
141 sh->hash_next->hash_pprev = sh->hash_pprev;
142 *sh->hash_pprev = sh->hash_next;
143 sh->hash_pprev = NULL;
147 static __inline__ void insert_hash(raid6_conf_t *conf, struct stripe_head *sh)
149 struct stripe_head **shp = &stripe_hash(conf, sh->sector);
151 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
153 CHECK_DEVLOCK();
154 if ((sh->hash_next = *shp) != NULL)
155 (*shp)->hash_pprev = &sh->hash_next;
156 *shp = sh;
157 sh->hash_pprev = shp;
161 /* find an idle stripe, make sure it is unhashed, and return it. */
162 static struct stripe_head *get_free_stripe(raid6_conf_t *conf)
164 struct stripe_head *sh = NULL;
165 struct list_head *first;
167 CHECK_DEVLOCK();
168 if (list_empty(&conf->inactive_list))
169 goto out;
170 first = conf->inactive_list.next;
171 sh = list_entry(first, struct stripe_head, lru);
172 list_del_init(first);
173 remove_hash(sh);
174 atomic_inc(&conf->active_stripes);
175 out:
176 return sh;
179 static void shrink_buffers(struct stripe_head *sh, int num)
181 struct page *p;
182 int i;
184 for (i=0; i<num ; i++) {
185 p = sh->dev[i].page;
186 if (!p)
187 continue;
188 sh->dev[i].page = NULL;
189 page_cache_release(p);
193 static int grow_buffers(struct stripe_head *sh, int num)
195 int i;
197 for (i=0; i<num; i++) {
198 struct page *page;
200 if (!(page = alloc_page(GFP_KERNEL))) {
201 return 1;
203 sh->dev[i].page = page;
205 return 0;
208 static void raid6_build_block (struct stripe_head *sh, int i);
210 static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
212 raid6_conf_t *conf = sh->raid_conf;
213 int disks = conf->raid_disks, i;
215 if (atomic_read(&sh->count) != 0)
216 BUG();
217 if (test_bit(STRIPE_HANDLE, &sh->state))
218 BUG();
220 CHECK_DEVLOCK();
221 PRINTK("init_stripe called, stripe %llu\n",
222 (unsigned long long)sh->sector);
224 remove_hash(sh);
226 sh->sector = sector;
227 sh->pd_idx = pd_idx;
228 sh->state = 0;
230 for (i=disks; i--; ) {
231 struct r5dev *dev = &sh->dev[i];
233 if (dev->toread || dev->towrite || dev->written ||
234 test_bit(R5_LOCKED, &dev->flags)) {
235 PRINTK("sector=%llx i=%d %p %p %p %d\n",
236 (unsigned long long)sh->sector, i, dev->toread,
237 dev->towrite, dev->written,
238 test_bit(R5_LOCKED, &dev->flags));
239 BUG();
241 dev->flags = 0;
242 raid6_build_block(sh, i);
244 insert_hash(conf, sh);
247 static struct stripe_head *__find_stripe(raid6_conf_t *conf, sector_t sector)
249 struct stripe_head *sh;
251 CHECK_DEVLOCK();
252 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
253 for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
254 if (sh->sector == sector)
255 return sh;
256 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
257 return NULL;
260 static void unplug_slaves(mddev_t *mddev);
262 static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector,
263 int pd_idx, int noblock)
265 struct stripe_head *sh;
267 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
269 spin_lock_irq(&conf->device_lock);
271 do {
272 wait_event_lock_irq(conf->wait_for_stripe,
273 conf->quiesce == 0,
274 conf->device_lock, /* nothing */);
275 sh = __find_stripe(conf, sector);
276 if (!sh) {
277 if (!conf->inactive_blocked)
278 sh = get_free_stripe(conf);
279 if (noblock && sh == NULL)
280 break;
281 if (!sh) {
282 conf->inactive_blocked = 1;
283 wait_event_lock_irq(conf->wait_for_stripe,
284 !list_empty(&conf->inactive_list) &&
285 (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
286 || !conf->inactive_blocked),
287 conf->device_lock,
288 unplug_slaves(conf->mddev);
290 conf->inactive_blocked = 0;
291 } else
292 init_stripe(sh, sector, pd_idx);
293 } else {
294 if (atomic_read(&sh->count)) {
295 if (!list_empty(&sh->lru))
296 BUG();
297 } else {
298 if (!test_bit(STRIPE_HANDLE, &sh->state))
299 atomic_inc(&conf->active_stripes);
300 if (list_empty(&sh->lru))
301 BUG();
302 list_del_init(&sh->lru);
305 } while (sh == NULL);
307 if (sh)
308 atomic_inc(&sh->count);
310 spin_unlock_irq(&conf->device_lock);
311 return sh;
314 static int grow_stripes(raid6_conf_t *conf, int num)
316 struct stripe_head *sh;
317 kmem_cache_t *sc;
318 int devs = conf->raid_disks;
320 sprintf(conf->cache_name, "raid6/%s", mdname(conf->mddev));
322 sc = kmem_cache_create(conf->cache_name,
323 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
324 0, 0, NULL, NULL);
325 if (!sc)
326 return 1;
327 conf->slab_cache = sc;
328 while (num--) {
329 sh = kmem_cache_alloc(sc, GFP_KERNEL);
330 if (!sh)
331 return 1;
332 memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
333 sh->raid_conf = conf;
334 spin_lock_init(&sh->lock);
336 if (grow_buffers(sh, conf->raid_disks)) {
337 shrink_buffers(sh, conf->raid_disks);
338 kmem_cache_free(sc, sh);
339 return 1;
341 /* we just created an active stripe so... */
342 atomic_set(&sh->count, 1);
343 atomic_inc(&conf->active_stripes);
344 INIT_LIST_HEAD(&sh->lru);
345 release_stripe(sh);
347 return 0;
350 static void shrink_stripes(raid6_conf_t *conf)
352 struct stripe_head *sh;
354 while (1) {
355 spin_lock_irq(&conf->device_lock);
356 sh = get_free_stripe(conf);
357 spin_unlock_irq(&conf->device_lock);
358 if (!sh)
359 break;
360 if (atomic_read(&sh->count))
361 BUG();
362 shrink_buffers(sh, conf->raid_disks);
363 kmem_cache_free(conf->slab_cache, sh);
364 atomic_dec(&conf->active_stripes);
366 kmem_cache_destroy(conf->slab_cache);
367 conf->slab_cache = NULL;
370 static int raid6_end_read_request (struct bio * bi, unsigned int bytes_done,
371 int error)
373 struct stripe_head *sh = bi->bi_private;
374 raid6_conf_t *conf = sh->raid_conf;
375 int disks = conf->raid_disks, i;
376 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
378 if (bi->bi_size)
379 return 1;
381 for (i=0 ; i<disks; i++)
382 if (bi == &sh->dev[i].req)
383 break;
385 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
386 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
387 uptodate);
388 if (i == disks) {
389 BUG();
390 return 0;
393 if (uptodate) {
394 #if 0
395 struct bio *bio;
396 unsigned long flags;
397 spin_lock_irqsave(&conf->device_lock, flags);
398 /* we can return a buffer if we bypassed the cache or
399 * if the top buffer is not in highmem. If there are
400 * multiple buffers, leave the extra work to
401 * handle_stripe
403 buffer = sh->bh_read[i];
404 if (buffer &&
405 (!PageHighMem(buffer->b_page)
406 || buffer->b_page == bh->b_page )
408 sh->bh_read[i] = buffer->b_reqnext;
409 buffer->b_reqnext = NULL;
410 } else
411 buffer = NULL;
412 spin_unlock_irqrestore(&conf->device_lock, flags);
413 if (sh->bh_page[i]==bh->b_page)
414 set_buffer_uptodate(bh);
415 if (buffer) {
416 if (buffer->b_page != bh->b_page)
417 memcpy(buffer->b_data, bh->b_data, bh->b_size);
418 buffer->b_end_io(buffer, 1);
420 #else
421 set_bit(R5_UPTODATE, &sh->dev[i].flags);
422 #endif
423 } else {
424 md_error(conf->mddev, conf->disks[i].rdev);
425 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
427 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
428 #if 0
429 /* must restore b_page before unlocking buffer... */
430 if (sh->bh_page[i] != bh->b_page) {
431 bh->b_page = sh->bh_page[i];
432 bh->b_data = page_address(bh->b_page);
433 clear_buffer_uptodate(bh);
435 #endif
436 clear_bit(R5_LOCKED, &sh->dev[i].flags);
437 set_bit(STRIPE_HANDLE, &sh->state);
438 release_stripe(sh);
439 return 0;
442 static int raid6_end_write_request (struct bio *bi, unsigned int bytes_done,
443 int error)
445 struct stripe_head *sh = bi->bi_private;
446 raid6_conf_t *conf = sh->raid_conf;
447 int disks = conf->raid_disks, i;
448 unsigned long flags;
449 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
451 if (bi->bi_size)
452 return 1;
454 for (i=0 ; i<disks; i++)
455 if (bi == &sh->dev[i].req)
456 break;
458 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
459 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
460 uptodate);
461 if (i == disks) {
462 BUG();
463 return 0;
466 spin_lock_irqsave(&conf->device_lock, flags);
467 if (!uptodate)
468 md_error(conf->mddev, conf->disks[i].rdev);
470 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
472 clear_bit(R5_LOCKED, &sh->dev[i].flags);
473 set_bit(STRIPE_HANDLE, &sh->state);
474 __release_stripe(conf, sh);
475 spin_unlock_irqrestore(&conf->device_lock, flags);
476 return 0;
480 static sector_t compute_blocknr(struct stripe_head *sh, int i);
482 static void raid6_build_block (struct stripe_head *sh, int i)
484 struct r5dev *dev = &sh->dev[i];
485 int pd_idx = sh->pd_idx;
486 int qd_idx = raid6_next_disk(pd_idx, sh->raid_conf->raid_disks);
488 bio_init(&dev->req);
489 dev->req.bi_io_vec = &dev->vec;
490 dev->req.bi_vcnt++;
491 dev->req.bi_max_vecs++;
492 dev->vec.bv_page = dev->page;
493 dev->vec.bv_len = STRIPE_SIZE;
494 dev->vec.bv_offset = 0;
496 dev->req.bi_sector = sh->sector;
497 dev->req.bi_private = sh;
499 dev->flags = 0;
500 if (i != pd_idx && i != qd_idx)
501 dev->sector = compute_blocknr(sh, i);
504 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
506 char b[BDEVNAME_SIZE];
507 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
508 PRINTK("raid6: error called\n");
510 if (!rdev->faulty) {
511 mddev->sb_dirty = 1;
512 if (rdev->in_sync) {
513 conf->working_disks--;
514 mddev->degraded++;
515 conf->failed_disks++;
516 rdev->in_sync = 0;
518 * if recovery was running, make sure it aborts.
520 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
522 rdev->faulty = 1;
523 printk (KERN_ALERT
524 "raid6: Disk failure on %s, disabling device."
525 " Operation continuing on %d devices\n",
526 bdevname(rdev->bdev,b), conf->working_disks);
531 * Input: a 'big' sector number,
532 * Output: index of the data and parity disk, and the sector # in them.
534 static sector_t raid6_compute_sector(sector_t r_sector, unsigned int raid_disks,
535 unsigned int data_disks, unsigned int * dd_idx,
536 unsigned int * pd_idx, raid6_conf_t *conf)
538 long stripe;
539 unsigned long chunk_number;
540 unsigned int chunk_offset;
541 sector_t new_sector;
542 int sectors_per_chunk = conf->chunk_size >> 9;
544 /* First compute the information on this sector */
547 * Compute the chunk number and the sector offset inside the chunk
549 chunk_offset = sector_div(r_sector, sectors_per_chunk);
550 chunk_number = r_sector;
551 if ( r_sector != chunk_number ) {
552 printk(KERN_CRIT "raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
553 (unsigned long long)r_sector, (unsigned long)chunk_number);
554 BUG();
558 * Compute the stripe number
560 stripe = chunk_number / data_disks;
563 * Compute the data disk and parity disk indexes inside the stripe
565 *dd_idx = chunk_number % data_disks;
568 * Select the parity disk based on the user selected algorithm.
571 /**** FIX THIS ****/
572 switch (conf->algorithm) {
573 case ALGORITHM_LEFT_ASYMMETRIC:
574 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
575 if (*pd_idx == raid_disks-1)
576 (*dd_idx)++; /* Q D D D P */
577 else if (*dd_idx >= *pd_idx)
578 (*dd_idx) += 2; /* D D P Q D */
579 break;
580 case ALGORITHM_RIGHT_ASYMMETRIC:
581 *pd_idx = stripe % raid_disks;
582 if (*pd_idx == raid_disks-1)
583 (*dd_idx)++; /* Q D D D P */
584 else if (*dd_idx >= *pd_idx)
585 (*dd_idx) += 2; /* D D P Q D */
586 break;
587 case ALGORITHM_LEFT_SYMMETRIC:
588 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
589 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
590 break;
591 case ALGORITHM_RIGHT_SYMMETRIC:
592 *pd_idx = stripe % raid_disks;
593 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
594 break;
595 default:
596 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
597 conf->algorithm);
600 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
601 chunk_number, *pd_idx, *dd_idx);
604 * Finally, compute the new sector number
606 new_sector = (sector_t) stripe * sectors_per_chunk + chunk_offset;
607 return new_sector;
611 static sector_t compute_blocknr(struct stripe_head *sh, int i)
613 raid6_conf_t *conf = sh->raid_conf;
614 int raid_disks = conf->raid_disks, data_disks = raid_disks - 2;
615 sector_t new_sector = sh->sector, check;
616 int sectors_per_chunk = conf->chunk_size >> 9;
617 sector_t stripe;
618 int chunk_offset;
619 int chunk_number, dummy1, dummy2, dd_idx = i;
620 sector_t r_sector;
621 int i0 = i;
623 chunk_offset = sector_div(new_sector, sectors_per_chunk);
624 stripe = new_sector;
625 if ( new_sector != stripe ) {
626 printk(KERN_CRIT "raid6: ERROR: new_sector = %llu, stripe = %lu\n",
627 (unsigned long long)new_sector, (unsigned long)stripe);
628 BUG();
631 switch (conf->algorithm) {
632 case ALGORITHM_LEFT_ASYMMETRIC:
633 case ALGORITHM_RIGHT_ASYMMETRIC:
634 if (sh->pd_idx == raid_disks-1)
635 i--; /* Q D D D P */
636 else if (i > sh->pd_idx)
637 i -= 2; /* D D P Q D */
638 break;
639 case ALGORITHM_LEFT_SYMMETRIC:
640 case ALGORITHM_RIGHT_SYMMETRIC:
641 if (sh->pd_idx == raid_disks-1)
642 i--; /* Q D D D P */
643 else {
644 /* D D P Q D */
645 if (i < sh->pd_idx)
646 i += raid_disks;
647 i -= (sh->pd_idx + 2);
649 break;
650 default:
651 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
652 conf->algorithm);
655 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh->pd_idx, i0, i);
657 chunk_number = stripe * data_disks + i;
658 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
660 check = raid6_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
661 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
662 printk(KERN_CRIT "raid6: compute_blocknr: map not correct\n");
663 return 0;
665 return r_sector;
671 * Copy data between a page in the stripe cache, and one or more bion
672 * The page could align with the middle of the bio, or there could be
673 * several bion, each with several bio_vecs, which cover part of the page
674 * Multiple bion are linked together on bi_next. There may be extras
675 * at the end of this list. We ignore them.
677 static void copy_data(int frombio, struct bio *bio,
678 struct page *page,
679 sector_t sector)
681 char *pa = page_address(page);
682 struct bio_vec *bvl;
683 int i;
684 int page_offset;
686 if (bio->bi_sector >= sector)
687 page_offset = (signed)(bio->bi_sector - sector) * 512;
688 else
689 page_offset = (signed)(sector - bio->bi_sector) * -512;
690 bio_for_each_segment(bvl, bio, i) {
691 int len = bio_iovec_idx(bio,i)->bv_len;
692 int clen;
693 int b_offset = 0;
695 if (page_offset < 0) {
696 b_offset = -page_offset;
697 page_offset += b_offset;
698 len -= b_offset;
701 if (len > 0 && page_offset + len > STRIPE_SIZE)
702 clen = STRIPE_SIZE - page_offset;
703 else clen = len;
705 if (clen > 0) {
706 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
707 if (frombio)
708 memcpy(pa+page_offset, ba+b_offset, clen);
709 else
710 memcpy(ba+b_offset, pa+page_offset, clen);
711 __bio_kunmap_atomic(ba, KM_USER0);
713 if (clen < len) /* hit end of page */
714 break;
715 page_offset += len;
719 #define check_xor() do { \
720 if (count == MAX_XOR_BLOCKS) { \
721 xor_block(count, STRIPE_SIZE, ptr); \
722 count = 1; \
724 } while(0)
726 /* Compute P and Q syndromes */
727 static void compute_parity(struct stripe_head *sh, int method)
729 raid6_conf_t *conf = sh->raid_conf;
730 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
731 struct bio *chosen;
732 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
733 void *ptrs[disks];
735 qd_idx = raid6_next_disk(pd_idx, disks);
736 d0_idx = raid6_next_disk(qd_idx, disks);
738 PRINTK("compute_parity, stripe %llu, method %d\n",
739 (unsigned long long)sh->sector, method);
741 switch(method) {
742 case READ_MODIFY_WRITE:
743 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
744 case RECONSTRUCT_WRITE:
745 for (i= disks; i-- ;)
746 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
747 chosen = sh->dev[i].towrite;
748 sh->dev[i].towrite = NULL;
750 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
751 wake_up(&conf->wait_for_overlap);
753 if (sh->dev[i].written) BUG();
754 sh->dev[i].written = chosen;
756 break;
757 case CHECK_PARITY:
758 BUG(); /* Not implemented yet */
761 for (i = disks; i--;)
762 if (sh->dev[i].written) {
763 sector_t sector = sh->dev[i].sector;
764 struct bio *wbi = sh->dev[i].written;
765 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
766 copy_data(1, wbi, sh->dev[i].page, sector);
767 wbi = r5_next_bio(wbi, sector);
770 set_bit(R5_LOCKED, &sh->dev[i].flags);
771 set_bit(R5_UPTODATE, &sh->dev[i].flags);
774 // switch(method) {
775 // case RECONSTRUCT_WRITE:
776 // case CHECK_PARITY:
777 // case UPDATE_PARITY:
778 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
779 /* FIX: Is this ordering of drives even remotely optimal? */
780 count = 0;
781 i = d0_idx;
782 do {
783 ptrs[count++] = page_address(sh->dev[i].page);
784 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
785 printk("block %d/%d not uptodate on parity calc\n", i,count);
786 i = raid6_next_disk(i, disks);
787 } while ( i != d0_idx );
788 // break;
789 // }
791 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
793 switch(method) {
794 case RECONSTRUCT_WRITE:
795 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
796 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
797 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
798 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
799 break;
800 case UPDATE_PARITY:
801 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
802 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
803 break;
807 /* Compute one missing block */
808 static void compute_block_1(struct stripe_head *sh, int dd_idx)
810 raid6_conf_t *conf = sh->raid_conf;
811 int i, count, disks = conf->raid_disks;
812 void *ptr[MAX_XOR_BLOCKS], *p;
813 int pd_idx = sh->pd_idx;
814 int qd_idx = raid6_next_disk(pd_idx, disks);
816 PRINTK("compute_block_1, stripe %llu, idx %d\n",
817 (unsigned long long)sh->sector, dd_idx);
819 if ( dd_idx == qd_idx ) {
820 /* We're actually computing the Q drive */
821 compute_parity(sh, UPDATE_PARITY);
822 } else {
823 ptr[0] = page_address(sh->dev[dd_idx].page);
824 memset(ptr[0], 0, STRIPE_SIZE);
825 count = 1;
826 for (i = disks ; i--; ) {
827 if (i == dd_idx || i == qd_idx)
828 continue;
829 p = page_address(sh->dev[i].page);
830 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
831 ptr[count++] = p;
832 else
833 printk("compute_block() %d, stripe %llu, %d"
834 " not present\n", dd_idx,
835 (unsigned long long)sh->sector, i);
837 check_xor();
839 if (count != 1)
840 xor_block(count, STRIPE_SIZE, ptr);
841 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
845 /* Compute two missing blocks */
846 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
848 raid6_conf_t *conf = sh->raid_conf;
849 int i, count, disks = conf->raid_disks;
850 int pd_idx = sh->pd_idx;
851 int qd_idx = raid6_next_disk(pd_idx, disks);
852 int d0_idx = raid6_next_disk(qd_idx, disks);
853 int faila, failb;
855 /* faila and failb are disk numbers relative to d0_idx */
856 /* pd_idx become disks-2 and qd_idx become disks-1 */
857 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
858 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
860 BUG_ON(faila == failb);
861 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
863 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
864 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
866 if ( failb == disks-1 ) {
867 /* Q disk is one of the missing disks */
868 if ( faila == disks-2 ) {
869 /* Missing P+Q, just recompute */
870 compute_parity(sh, UPDATE_PARITY);
871 return;
872 } else {
873 /* We're missing D+Q; recompute D from P */
874 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1);
875 compute_parity(sh, UPDATE_PARITY); /* Is this necessary? */
876 return;
880 /* We're missing D+P or D+D; build pointer table */
882 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
883 void *ptrs[disks];
885 count = 0;
886 i = d0_idx;
887 do {
888 ptrs[count++] = page_address(sh->dev[i].page);
889 i = raid6_next_disk(i, disks);
890 if (i != dd_idx1 && i != dd_idx2 &&
891 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
892 printk("compute_2 with missing block %d/%d\n", count, i);
893 } while ( i != d0_idx );
895 if ( failb == disks-2 ) {
896 /* We're missing D+P. */
897 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
898 } else {
899 /* We're missing D+D. */
900 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
903 /* Both the above update both missing blocks */
904 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
905 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
911 * Each stripe/dev can have one or more bion attached.
912 * toread/towrite point to the first in a chain.
913 * The bi_next chain must be in order.
915 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
917 struct bio **bip;
918 raid6_conf_t *conf = sh->raid_conf;
919 int firstwrite=0;
921 PRINTK("adding bh b#%llu to stripe s#%llu\n",
922 (unsigned long long)bi->bi_sector,
923 (unsigned long long)sh->sector);
926 spin_lock(&sh->lock);
927 spin_lock_irq(&conf->device_lock);
928 if (forwrite) {
929 bip = &sh->dev[dd_idx].towrite;
930 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
931 firstwrite = 1;
932 } else
933 bip = &sh->dev[dd_idx].toread;
934 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
935 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
936 goto overlap;
937 bip = &(*bip)->bi_next;
939 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
940 goto overlap;
942 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
943 BUG();
944 if (*bip)
945 bi->bi_next = *bip;
946 *bip = bi;
947 bi->bi_phys_segments ++;
948 spin_unlock_irq(&conf->device_lock);
949 spin_unlock(&sh->lock);
951 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
952 (unsigned long long)bi->bi_sector,
953 (unsigned long long)sh->sector, dd_idx);
955 if (conf->mddev->bitmap && firstwrite) {
956 sh->bm_seq = conf->seq_write;
957 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
958 STRIPE_SECTORS, 0);
959 set_bit(STRIPE_BIT_DELAY, &sh->state);
962 if (forwrite) {
963 /* check if page is covered */
964 sector_t sector = sh->dev[dd_idx].sector;
965 for (bi=sh->dev[dd_idx].towrite;
966 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
967 bi && bi->bi_sector <= sector;
968 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
969 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
970 sector = bi->bi_sector + (bi->bi_size>>9);
972 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
973 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
975 return 1;
977 overlap:
978 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
979 spin_unlock_irq(&conf->device_lock);
980 spin_unlock(&sh->lock);
981 return 0;
986 * handle_stripe - do things to a stripe.
988 * We lock the stripe and then examine the state of various bits
989 * to see what needs to be done.
990 * Possible results:
991 * return some read request which now have data
992 * return some write requests which are safely on disc
993 * schedule a read on some buffers
994 * schedule a write of some buffers
995 * return confirmation of parity correctness
997 * Parity calculations are done inside the stripe lock
998 * buffers are taken off read_list or write_list, and bh_cache buffers
999 * get BH_Lock set before the stripe lock is released.
1003 static void handle_stripe(struct stripe_head *sh)
1005 raid6_conf_t *conf = sh->raid_conf;
1006 int disks = conf->raid_disks;
1007 struct bio *return_bi= NULL;
1008 struct bio *bi;
1009 int i;
1010 int syncing;
1011 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1012 int non_overwrite = 0;
1013 int failed_num[2] = {0, 0};
1014 struct r5dev *dev, *pdev, *qdev;
1015 int pd_idx = sh->pd_idx;
1016 int qd_idx = raid6_next_disk(pd_idx, disks);
1017 int p_failed, q_failed;
1019 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1020 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1021 pd_idx, qd_idx);
1023 spin_lock(&sh->lock);
1024 clear_bit(STRIPE_HANDLE, &sh->state);
1025 clear_bit(STRIPE_DELAYED, &sh->state);
1027 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1028 /* Now to look around and see what can be done */
1030 for (i=disks; i--; ) {
1031 mdk_rdev_t *rdev;
1032 dev = &sh->dev[i];
1033 clear_bit(R5_Insync, &dev->flags);
1034 clear_bit(R5_Syncio, &dev->flags);
1036 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1037 i, dev->flags, dev->toread, dev->towrite, dev->written);
1038 /* maybe we can reply to a read */
1039 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1040 struct bio *rbi, *rbi2;
1041 PRINTK("Return read for disc %d\n", i);
1042 spin_lock_irq(&conf->device_lock);
1043 rbi = dev->toread;
1044 dev->toread = NULL;
1045 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1046 wake_up(&conf->wait_for_overlap);
1047 spin_unlock_irq(&conf->device_lock);
1048 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1049 copy_data(0, rbi, dev->page, dev->sector);
1050 rbi2 = r5_next_bio(rbi, dev->sector);
1051 spin_lock_irq(&conf->device_lock);
1052 if (--rbi->bi_phys_segments == 0) {
1053 rbi->bi_next = return_bi;
1054 return_bi = rbi;
1056 spin_unlock_irq(&conf->device_lock);
1057 rbi = rbi2;
1061 /* now count some things */
1062 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1063 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1066 if (dev->toread) to_read++;
1067 if (dev->towrite) {
1068 to_write++;
1069 if (!test_bit(R5_OVERWRITE, &dev->flags))
1070 non_overwrite++;
1072 if (dev->written) written++;
1073 rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
1074 if (!rdev || !rdev->in_sync) {
1075 if ( failed < 2 )
1076 failed_num[failed] = i;
1077 failed++;
1078 } else
1079 set_bit(R5_Insync, &dev->flags);
1081 PRINTK("locked=%d uptodate=%d to_read=%d"
1082 " to_write=%d failed=%d failed_num=%d,%d\n",
1083 locked, uptodate, to_read, to_write, failed,
1084 failed_num[0], failed_num[1]);
1085 /* check if the array has lost >2 devices and, if so, some requests might
1086 * need to be failed
1088 if (failed > 2 && to_read+to_write+written) {
1089 for (i=disks; i--; ) {
1090 int bitmap_end = 0;
1091 spin_lock_irq(&conf->device_lock);
1092 /* fail all writes first */
1093 bi = sh->dev[i].towrite;
1094 sh->dev[i].towrite = NULL;
1095 if (bi) { to_write--; bitmap_end = 1; }
1097 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1098 wake_up(&conf->wait_for_overlap);
1100 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1101 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1102 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1103 if (--bi->bi_phys_segments == 0) {
1104 md_write_end(conf->mddev);
1105 bi->bi_next = return_bi;
1106 return_bi = bi;
1108 bi = nextbi;
1110 /* and fail all 'written' */
1111 bi = sh->dev[i].written;
1112 sh->dev[i].written = NULL;
1113 if (bi) bitmap_end = 1;
1114 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1115 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1116 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1117 if (--bi->bi_phys_segments == 0) {
1118 md_write_end(conf->mddev);
1119 bi->bi_next = return_bi;
1120 return_bi = bi;
1122 bi = bi2;
1125 /* fail any reads if this device is non-operational */
1126 if (!test_bit(R5_Insync, &sh->dev[i].flags)) {
1127 bi = sh->dev[i].toread;
1128 sh->dev[i].toread = NULL;
1129 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1130 wake_up(&conf->wait_for_overlap);
1131 if (bi) to_read--;
1132 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1133 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1134 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1135 if (--bi->bi_phys_segments == 0) {
1136 bi->bi_next = return_bi;
1137 return_bi = bi;
1139 bi = nextbi;
1142 spin_unlock_irq(&conf->device_lock);
1143 if (bitmap_end)
1144 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1145 STRIPE_SECTORS, 0, 0);
1148 if (failed > 2 && syncing) {
1149 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1150 clear_bit(STRIPE_SYNCING, &sh->state);
1151 syncing = 0;
1155 * might be able to return some write requests if the parity blocks
1156 * are safe, or on a failed drive
1158 pdev = &sh->dev[pd_idx];
1159 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
1160 || (failed >= 2 && failed_num[1] == pd_idx);
1161 qdev = &sh->dev[qd_idx];
1162 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
1163 || (failed >= 2 && failed_num[1] == qd_idx);
1165 if ( written &&
1166 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
1167 && !test_bit(R5_LOCKED, &pdev->flags)
1168 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
1169 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
1170 && !test_bit(R5_LOCKED, &qdev->flags)
1171 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
1172 /* any written block on an uptodate or failed drive can be
1173 * returned. Note that if we 'wrote' to a failed drive,
1174 * it will be UPTODATE, but never LOCKED, so we don't need
1175 * to test 'failed' directly.
1177 for (i=disks; i--; )
1178 if (sh->dev[i].written) {
1179 dev = &sh->dev[i];
1180 if (!test_bit(R5_LOCKED, &dev->flags) &&
1181 test_bit(R5_UPTODATE, &dev->flags) ) {
1182 /* We can return any write requests */
1183 int bitmap_end = 0;
1184 struct bio *wbi, *wbi2;
1185 PRINTK("Return write for stripe %llu disc %d\n",
1186 (unsigned long long)sh->sector, i);
1187 spin_lock_irq(&conf->device_lock);
1188 wbi = dev->written;
1189 dev->written = NULL;
1190 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1191 wbi2 = r5_next_bio(wbi, dev->sector);
1192 if (--wbi->bi_phys_segments == 0) {
1193 md_write_end(conf->mddev);
1194 wbi->bi_next = return_bi;
1195 return_bi = wbi;
1197 wbi = wbi2;
1199 if (dev->towrite == NULL)
1200 bitmap_end = 1;
1201 spin_unlock_irq(&conf->device_lock);
1202 if (bitmap_end)
1203 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1204 STRIPE_SECTORS,
1205 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1210 /* Now we might consider reading some blocks, either to check/generate
1211 * parity, or to satisfy requests
1212 * or to load a block that is being partially written.
1214 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
1215 for (i=disks; i--;) {
1216 dev = &sh->dev[i];
1217 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1218 (dev->toread ||
1219 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1220 syncing ||
1221 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
1222 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
1225 /* we would like to get this block, possibly
1226 * by computing it, but we might not be able to
1228 if (uptodate == disks-1) {
1229 PRINTK("Computing stripe %llu block %d\n",
1230 (unsigned long long)sh->sector, i);
1231 compute_block_1(sh, i);
1232 uptodate++;
1233 } else if ( uptodate == disks-2 && failed >= 2 ) {
1234 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1235 int other;
1236 for (other=disks; other--;) {
1237 if ( other == i )
1238 continue;
1239 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
1240 break;
1242 BUG_ON(other < 0);
1243 PRINTK("Computing stripe %llu blocks %d,%d\n",
1244 (unsigned long long)sh->sector, i, other);
1245 compute_block_2(sh, i, other);
1246 uptodate += 2;
1247 } else if (test_bit(R5_Insync, &dev->flags)) {
1248 set_bit(R5_LOCKED, &dev->flags);
1249 set_bit(R5_Wantread, &dev->flags);
1250 #if 0
1251 /* if I am just reading this block and we don't have
1252 a failed drive, or any pending writes then sidestep the cache */
1253 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1254 ! syncing && !failed && !to_write) {
1255 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1256 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1258 #endif
1259 locked++;
1260 PRINTK("Reading block %d (sync=%d)\n",
1261 i, syncing);
1262 if (syncing)
1263 md_sync_acct(conf->disks[i].rdev->bdev,
1264 STRIPE_SECTORS);
1268 set_bit(STRIPE_HANDLE, &sh->state);
1271 /* now to consider writing and what else, if anything should be read */
1272 if (to_write) {
1273 int rcw=0, must_compute=0;
1274 for (i=disks ; i--;) {
1275 dev = &sh->dev[i];
1276 /* Would I have to read this buffer for reconstruct_write */
1277 if (!test_bit(R5_OVERWRITE, &dev->flags)
1278 && i != pd_idx && i != qd_idx
1279 && (!test_bit(R5_LOCKED, &dev->flags)
1280 #if 0
1281 || sh->bh_page[i] != bh->b_page
1282 #endif
1283 ) &&
1284 !test_bit(R5_UPTODATE, &dev->flags)) {
1285 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1286 else {
1287 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
1288 must_compute++;
1292 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1293 (unsigned long long)sh->sector, rcw, must_compute);
1294 set_bit(STRIPE_HANDLE, &sh->state);
1296 if (rcw > 0)
1297 /* want reconstruct write, but need to get some data */
1298 for (i=disks; i--;) {
1299 dev = &sh->dev[i];
1300 if (!test_bit(R5_OVERWRITE, &dev->flags)
1301 && !(failed == 0 && (i == pd_idx || i == qd_idx))
1302 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1303 test_bit(R5_Insync, &dev->flags)) {
1304 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1306 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1307 (unsigned long long)sh->sector, i);
1308 set_bit(R5_LOCKED, &dev->flags);
1309 set_bit(R5_Wantread, &dev->flags);
1310 locked++;
1311 } else {
1312 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1313 (unsigned long long)sh->sector, i);
1314 set_bit(STRIPE_DELAYED, &sh->state);
1315 set_bit(STRIPE_HANDLE, &sh->state);
1319 /* now if nothing is locked, and if we have enough data, we can start a write request */
1320 if (locked == 0 && rcw == 0 &&
1321 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1322 if ( must_compute > 0 ) {
1323 /* We have failed blocks and need to compute them */
1324 switch ( failed ) {
1325 case 0: BUG();
1326 case 1: compute_block_1(sh, failed_num[0]); break;
1327 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
1328 default: BUG(); /* This request should have been failed? */
1332 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
1333 compute_parity(sh, RECONSTRUCT_WRITE);
1334 /* now every locked buffer is ready to be written */
1335 for (i=disks; i--;)
1336 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1337 PRINTK("Writing stripe %llu block %d\n",
1338 (unsigned long long)sh->sector, i);
1339 locked++;
1340 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1341 #if 0 /**** FIX: I don't understand the logic here... ****/
1342 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1343 || ((i==pd_idx || i==qd_idx) && failed == 0)) /* FIX? */
1344 set_bit(STRIPE_INSYNC, &sh->state);
1345 #endif
1347 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1348 atomic_dec(&conf->preread_active_stripes);
1349 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1350 md_wakeup_thread(conf->mddev->thread);
1355 /* maybe we need to check and possibly fix the parity for this stripe
1356 * Any reads will already have been scheduled, so we just see if enough data
1357 * is available
1359 if (syncing && locked == 0 &&
1360 !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 2) {
1361 set_bit(STRIPE_HANDLE, &sh->state);
1362 #if 0 /* RAID-6: Don't support CHECK PARITY yet */
1363 if (failed == 0) {
1364 char *pagea;
1365 if (uptodate != disks)
1366 BUG();
1367 compute_parity(sh, CHECK_PARITY);
1368 uptodate--;
1369 pagea = page_address(sh->dev[pd_idx].page);
1370 if ((*(u32*)pagea) == 0 &&
1371 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1372 /* parity is correct (on disc, not in buffer any more) */
1373 set_bit(STRIPE_INSYNC, &sh->state);
1376 #endif
1377 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1378 int failed_needupdate[2];
1379 struct r5dev *adev, *bdev;
1381 if ( failed < 1 )
1382 failed_num[0] = pd_idx;
1383 if ( failed < 2 )
1384 failed_num[1] = (failed_num[0] == qd_idx) ? pd_idx : qd_idx;
1386 failed_needupdate[0] = !test_bit(R5_UPTODATE, &sh->dev[failed_num[0]].flags);
1387 failed_needupdate[1] = !test_bit(R5_UPTODATE, &sh->dev[failed_num[1]].flags);
1389 PRINTK("sync: failed=%d num=%d,%d fnu=%u%u\n",
1390 failed, failed_num[0], failed_num[1], failed_needupdate[0], failed_needupdate[1]);
1392 #if 0 /* RAID-6: This code seems to require that CHECK_PARITY destroys the uptodateness of the parity */
1393 /* should be able to compute the missing block(s) and write to spare */
1394 if ( failed_needupdate[0] ^ failed_needupdate[1] ) {
1395 if (uptodate+1 != disks)
1396 BUG();
1397 compute_block_1(sh, failed_needupdate[0] ? failed_num[0] : failed_num[1]);
1398 uptodate++;
1399 } else if ( failed_needupdate[0] & failed_needupdate[1] ) {
1400 if (uptodate+2 != disks)
1401 BUG();
1402 compute_block_2(sh, failed_num[0], failed_num[1]);
1403 uptodate += 2;
1405 #else
1406 compute_block_2(sh, failed_num[0], failed_num[1]);
1407 uptodate += failed_needupdate[0] + failed_needupdate[1];
1408 #endif
1410 if (uptodate != disks)
1411 BUG();
1413 PRINTK("Marking for sync stripe %llu blocks %d,%d\n",
1414 (unsigned long long)sh->sector, failed_num[0], failed_num[1]);
1416 /**** FIX: Should we really do both of these unconditionally? ****/
1417 adev = &sh->dev[failed_num[0]];
1418 locked += !test_bit(R5_LOCKED, &adev->flags);
1419 set_bit(R5_LOCKED, &adev->flags);
1420 set_bit(R5_Wantwrite, &adev->flags);
1421 bdev = &sh->dev[failed_num[1]];
1422 locked += !test_bit(R5_LOCKED, &bdev->flags);
1423 set_bit(R5_LOCKED, &bdev->flags);
1424 clear_bit(STRIPE_DEGRADED, &sh->state);
1425 set_bit(R5_Wantwrite, &bdev->flags);
1427 set_bit(STRIPE_INSYNC, &sh->state);
1428 set_bit(R5_Syncio, &adev->flags);
1429 set_bit(R5_Syncio, &bdev->flags);
1432 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1433 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1434 clear_bit(STRIPE_SYNCING, &sh->state);
1437 spin_unlock(&sh->lock);
1439 while ((bi=return_bi)) {
1440 int bytes = bi->bi_size;
1442 return_bi = bi->bi_next;
1443 bi->bi_next = NULL;
1444 bi->bi_size = 0;
1445 bi->bi_end_io(bi, bytes, 0);
1447 for (i=disks; i-- ;) {
1448 int rw;
1449 struct bio *bi;
1450 mdk_rdev_t *rdev;
1451 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1452 rw = 1;
1453 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1454 rw = 0;
1455 else
1456 continue;
1458 bi = &sh->dev[i].req;
1460 bi->bi_rw = rw;
1461 if (rw)
1462 bi->bi_end_io = raid6_end_write_request;
1463 else
1464 bi->bi_end_io = raid6_end_read_request;
1466 rcu_read_lock();
1467 rdev = conf->disks[i].rdev;
1468 if (rdev && rdev->faulty)
1469 rdev = NULL;
1470 if (rdev)
1471 atomic_inc(&rdev->nr_pending);
1472 rcu_read_unlock();
1474 if (rdev) {
1475 if (test_bit(R5_Syncio, &sh->dev[i].flags))
1476 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1478 bi->bi_bdev = rdev->bdev;
1479 PRINTK("for %llu schedule op %ld on disc %d\n",
1480 (unsigned long long)sh->sector, bi->bi_rw, i);
1481 atomic_inc(&sh->count);
1482 bi->bi_sector = sh->sector + rdev->data_offset;
1483 bi->bi_flags = 1 << BIO_UPTODATE;
1484 bi->bi_vcnt = 1;
1485 bi->bi_max_vecs = 1;
1486 bi->bi_idx = 0;
1487 bi->bi_io_vec = &sh->dev[i].vec;
1488 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1489 bi->bi_io_vec[0].bv_offset = 0;
1490 bi->bi_size = STRIPE_SIZE;
1491 bi->bi_next = NULL;
1492 generic_make_request(bi);
1493 } else {
1494 if (rw == 1)
1495 set_bit(STRIPE_DEGRADED, &sh->state);
1496 PRINTK("skip op %ld on disc %d for sector %llu\n",
1497 bi->bi_rw, i, (unsigned long long)sh->sector);
1498 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1499 set_bit(STRIPE_HANDLE, &sh->state);
1504 static inline void raid6_activate_delayed(raid6_conf_t *conf)
1506 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1507 while (!list_empty(&conf->delayed_list)) {
1508 struct list_head *l = conf->delayed_list.next;
1509 struct stripe_head *sh;
1510 sh = list_entry(l, struct stripe_head, lru);
1511 list_del_init(l);
1512 clear_bit(STRIPE_DELAYED, &sh->state);
1513 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1514 atomic_inc(&conf->preread_active_stripes);
1515 list_add_tail(&sh->lru, &conf->handle_list);
1520 static inline void activate_bit_delay(raid6_conf_t *conf)
1522 /* device_lock is held */
1523 struct list_head head;
1524 list_add(&head, &conf->bitmap_list);
1525 list_del_init(&conf->bitmap_list);
1526 while (!list_empty(&head)) {
1527 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1528 list_del_init(&sh->lru);
1529 atomic_inc(&sh->count);
1530 __release_stripe(conf, sh);
1534 static void unplug_slaves(mddev_t *mddev)
1536 raid6_conf_t *conf = mddev_to_conf(mddev);
1537 int i;
1539 rcu_read_lock();
1540 for (i=0; i<mddev->raid_disks; i++) {
1541 mdk_rdev_t *rdev = conf->disks[i].rdev;
1542 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
1543 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1545 atomic_inc(&rdev->nr_pending);
1546 rcu_read_unlock();
1548 if (r_queue->unplug_fn)
1549 r_queue->unplug_fn(r_queue);
1551 rdev_dec_pending(rdev, mddev);
1552 rcu_read_lock();
1555 rcu_read_unlock();
1558 static void raid6_unplug_device(request_queue_t *q)
1560 mddev_t *mddev = q->queuedata;
1561 raid6_conf_t *conf = mddev_to_conf(mddev);
1562 unsigned long flags;
1564 spin_lock_irqsave(&conf->device_lock, flags);
1566 if (blk_remove_plug(q)) {
1567 conf->seq_flush++;
1568 raid6_activate_delayed(conf);
1570 md_wakeup_thread(mddev->thread);
1572 spin_unlock_irqrestore(&conf->device_lock, flags);
1574 unplug_slaves(mddev);
1577 static int raid6_issue_flush(request_queue_t *q, struct gendisk *disk,
1578 sector_t *error_sector)
1580 mddev_t *mddev = q->queuedata;
1581 raid6_conf_t *conf = mddev_to_conf(mddev);
1582 int i, ret = 0;
1584 rcu_read_lock();
1585 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1586 mdk_rdev_t *rdev = conf->disks[i].rdev;
1587 if (rdev && !rdev->faulty) {
1588 struct block_device *bdev = rdev->bdev;
1589 request_queue_t *r_queue = bdev_get_queue(bdev);
1591 if (!r_queue->issue_flush_fn)
1592 ret = -EOPNOTSUPP;
1593 else {
1594 atomic_inc(&rdev->nr_pending);
1595 rcu_read_unlock();
1596 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1597 error_sector);
1598 rdev_dec_pending(rdev, mddev);
1599 rcu_read_lock();
1603 rcu_read_unlock();
1604 return ret;
1607 static inline void raid6_plug_device(raid6_conf_t *conf)
1609 spin_lock_irq(&conf->device_lock);
1610 blk_plug_device(conf->mddev->queue);
1611 spin_unlock_irq(&conf->device_lock);
1614 static int make_request (request_queue_t *q, struct bio * bi)
1616 mddev_t *mddev = q->queuedata;
1617 raid6_conf_t *conf = mddev_to_conf(mddev);
1618 const unsigned int raid_disks = conf->raid_disks;
1619 const unsigned int data_disks = raid_disks - 2;
1620 unsigned int dd_idx, pd_idx;
1621 sector_t new_sector;
1622 sector_t logical_sector, last_sector;
1623 struct stripe_head *sh;
1624 const int rw = bio_data_dir(bi);
1626 if (unlikely(bio_barrier(bi))) {
1627 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1628 return 0;
1631 md_write_start(mddev, bi);
1633 disk_stat_inc(mddev->gendisk, ios[rw]);
1634 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1636 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1637 last_sector = bi->bi_sector + (bi->bi_size>>9);
1639 bi->bi_next = NULL;
1640 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1642 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1643 DEFINE_WAIT(w);
1645 new_sector = raid6_compute_sector(logical_sector,
1646 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1648 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1649 (unsigned long long)new_sector,
1650 (unsigned long long)logical_sector);
1652 retry:
1653 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1654 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1655 if (sh) {
1656 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1657 /* Add failed due to overlap. Flush everything
1658 * and wait a while
1660 raid6_unplug_device(mddev->queue);
1661 release_stripe(sh);
1662 schedule();
1663 goto retry;
1665 finish_wait(&conf->wait_for_overlap, &w);
1666 raid6_plug_device(conf);
1667 handle_stripe(sh);
1668 release_stripe(sh);
1669 } else {
1670 /* cannot get stripe for read-ahead, just give-up */
1671 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1672 finish_wait(&conf->wait_for_overlap, &w);
1673 break;
1677 spin_lock_irq(&conf->device_lock);
1678 if (--bi->bi_phys_segments == 0) {
1679 int bytes = bi->bi_size;
1681 if (rw == WRITE )
1682 md_write_end(mddev);
1683 bi->bi_size = 0;
1684 bi->bi_end_io(bi, bytes, 0);
1686 spin_unlock_irq(&conf->device_lock);
1687 return 0;
1690 /* FIXME go_faster isn't used */
1691 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1693 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1694 struct stripe_head *sh;
1695 int sectors_per_chunk = conf->chunk_size >> 9;
1696 sector_t x;
1697 unsigned long stripe;
1698 int chunk_offset;
1699 int dd_idx, pd_idx;
1700 sector_t first_sector;
1701 int raid_disks = conf->raid_disks;
1702 int data_disks = raid_disks - 2;
1703 sector_t max_sector = mddev->size << 1;
1704 int sync_blocks;
1706 if (sector_nr >= max_sector) {
1707 /* just being told to finish up .. nothing much to do */
1708 unplug_slaves(mddev);
1710 if (mddev->curr_resync < max_sector) /* aborted */
1711 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1712 &sync_blocks, 1);
1713 else /* compelted sync */
1714 conf->fullsync = 0;
1715 bitmap_close_sync(mddev->bitmap);
1717 return 0;
1719 /* if there are 2 or more failed drives and we are trying
1720 * to resync, then assert that we are finished, because there is
1721 * nothing we can do.
1723 if (mddev->degraded >= 2 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1724 sector_t rv = (mddev->size << 1) - sector_nr;
1725 *skipped = 1;
1726 return rv;
1728 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1729 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1730 /* we can skip this block, and probably more */
1731 sync_blocks /= STRIPE_SECTORS;
1732 *skipped = 1;
1733 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1736 x = sector_nr;
1737 chunk_offset = sector_div(x, sectors_per_chunk);
1738 stripe = x;
1739 BUG_ON(x != stripe);
1741 first_sector = raid6_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1742 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1743 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1744 if (sh == NULL) {
1745 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1746 /* make sure we don't swamp the stripe cache if someone else
1747 * is trying to get access
1749 schedule_timeout_uninterruptible(1);
1751 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 0);
1752 spin_lock(&sh->lock);
1753 set_bit(STRIPE_SYNCING, &sh->state);
1754 clear_bit(STRIPE_INSYNC, &sh->state);
1755 spin_unlock(&sh->lock);
1757 handle_stripe(sh);
1758 release_stripe(sh);
1760 return STRIPE_SECTORS;
1764 * This is our raid6 kernel thread.
1766 * We scan the hash table for stripes which can be handled now.
1767 * During the scan, completed stripes are saved for us by the interrupt
1768 * handler, so that they will not have to wait for our next wakeup.
1770 static void raid6d (mddev_t *mddev)
1772 struct stripe_head *sh;
1773 raid6_conf_t *conf = mddev_to_conf(mddev);
1774 int handled;
1776 PRINTK("+++ raid6d active\n");
1778 md_check_recovery(mddev);
1780 handled = 0;
1781 spin_lock_irq(&conf->device_lock);
1782 while (1) {
1783 struct list_head *first;
1785 if (conf->seq_flush - conf->seq_write > 0) {
1786 int seq = conf->seq_flush;
1787 bitmap_unplug(mddev->bitmap);
1788 conf->seq_write = seq;
1789 activate_bit_delay(conf);
1792 if (list_empty(&conf->handle_list) &&
1793 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1794 !blk_queue_plugged(mddev->queue) &&
1795 !list_empty(&conf->delayed_list))
1796 raid6_activate_delayed(conf);
1798 if (list_empty(&conf->handle_list))
1799 break;
1801 first = conf->handle_list.next;
1802 sh = list_entry(first, struct stripe_head, lru);
1804 list_del_init(first);
1805 atomic_inc(&sh->count);
1806 if (atomic_read(&sh->count)!= 1)
1807 BUG();
1808 spin_unlock_irq(&conf->device_lock);
1810 handled++;
1811 handle_stripe(sh);
1812 release_stripe(sh);
1814 spin_lock_irq(&conf->device_lock);
1816 PRINTK("%d stripes handled\n", handled);
1818 spin_unlock_irq(&conf->device_lock);
1820 unplug_slaves(mddev);
1822 PRINTK("--- raid6d inactive\n");
1825 static int run(mddev_t *mddev)
1827 raid6_conf_t *conf;
1828 int raid_disk, memory;
1829 mdk_rdev_t *rdev;
1830 struct disk_info *disk;
1831 struct list_head *tmp;
1833 if (mddev->level != 6) {
1834 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev), mddev->level);
1835 return -EIO;
1838 mddev->private = kmalloc (sizeof (raid6_conf_t)
1839 + mddev->raid_disks * sizeof(struct disk_info),
1840 GFP_KERNEL);
1841 if ((conf = mddev->private) == NULL)
1842 goto abort;
1843 memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1844 conf->mddev = mddev;
1846 if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1847 goto abort;
1848 memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1850 spin_lock_init(&conf->device_lock);
1851 init_waitqueue_head(&conf->wait_for_stripe);
1852 init_waitqueue_head(&conf->wait_for_overlap);
1853 INIT_LIST_HEAD(&conf->handle_list);
1854 INIT_LIST_HEAD(&conf->delayed_list);
1855 INIT_LIST_HEAD(&conf->bitmap_list);
1856 INIT_LIST_HEAD(&conf->inactive_list);
1857 atomic_set(&conf->active_stripes, 0);
1858 atomic_set(&conf->preread_active_stripes, 0);
1860 PRINTK("raid6: run(%s) called.\n", mdname(mddev));
1862 ITERATE_RDEV(mddev,rdev,tmp) {
1863 raid_disk = rdev->raid_disk;
1864 if (raid_disk >= mddev->raid_disks
1865 || raid_disk < 0)
1866 continue;
1867 disk = conf->disks + raid_disk;
1869 disk->rdev = rdev;
1871 if (rdev->in_sync) {
1872 char b[BDEVNAME_SIZE];
1873 printk(KERN_INFO "raid6: device %s operational as raid"
1874 " disk %d\n", bdevname(rdev->bdev,b),
1875 raid_disk);
1876 conf->working_disks++;
1880 conf->raid_disks = mddev->raid_disks;
1883 * 0 for a fully functional array, 1 or 2 for a degraded array.
1885 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1886 conf->mddev = mddev;
1887 conf->chunk_size = mddev->chunk_size;
1888 conf->level = mddev->level;
1889 conf->algorithm = mddev->layout;
1890 conf->max_nr_stripes = NR_STRIPES;
1892 /* device size must be a multiple of chunk size */
1893 mddev->size &= ~(mddev->chunk_size/1024 -1);
1894 mddev->resync_max_sectors = mddev->size << 1;
1896 if (conf->raid_disks < 4) {
1897 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
1898 mdname(mddev), conf->raid_disks);
1899 goto abort;
1901 if (!conf->chunk_size || conf->chunk_size % 4) {
1902 printk(KERN_ERR "raid6: invalid chunk size %d for %s\n",
1903 conf->chunk_size, mdname(mddev));
1904 goto abort;
1906 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1907 printk(KERN_ERR
1908 "raid6: unsupported parity algorithm %d for %s\n",
1909 conf->algorithm, mdname(mddev));
1910 goto abort;
1912 if (mddev->degraded > 2) {
1913 printk(KERN_ERR "raid6: not enough operational devices for %s"
1914 " (%d/%d failed)\n",
1915 mdname(mddev), conf->failed_disks, conf->raid_disks);
1916 goto abort;
1919 #if 0 /* FIX: For now */
1920 if (mddev->degraded > 0 &&
1921 mddev->recovery_cp != MaxSector) {
1922 printk(KERN_ERR "raid6: cannot start dirty degraded array for %s\n", mdname(mddev));
1923 goto abort;
1925 #endif
1928 mddev->thread = md_register_thread(raid6d, mddev, "%s_raid6");
1929 if (!mddev->thread) {
1930 printk(KERN_ERR
1931 "raid6: couldn't allocate thread for %s\n",
1932 mdname(mddev));
1933 goto abort;
1937 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1938 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1939 if (grow_stripes(conf, conf->max_nr_stripes)) {
1940 printk(KERN_ERR
1941 "raid6: couldn't allocate %dkB for buffers\n", memory);
1942 shrink_stripes(conf);
1943 md_unregister_thread(mddev->thread);
1944 goto abort;
1945 } else
1946 printk(KERN_INFO "raid6: allocated %dkB for %s\n",
1947 memory, mdname(mddev));
1949 if (mddev->degraded == 0)
1950 printk(KERN_INFO "raid6: raid level %d set %s active with %d out of %d"
1951 " devices, algorithm %d\n", conf->level, mdname(mddev),
1952 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1953 conf->algorithm);
1954 else
1955 printk(KERN_ALERT "raid6: raid level %d set %s active with %d"
1956 " out of %d devices, algorithm %d\n", conf->level,
1957 mdname(mddev), mddev->raid_disks - mddev->degraded,
1958 mddev->raid_disks, conf->algorithm);
1960 print_raid6_conf(conf);
1962 /* read-ahead size must cover two whole stripes, which is
1963 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
1966 int stripe = (mddev->raid_disks-2) * mddev->chunk_size
1967 / PAGE_CACHE_SIZE;
1968 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1969 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1972 /* Ok, everything is just fine now */
1973 mddev->array_size = mddev->size * (mddev->raid_disks - 2);
1975 if (mddev->bitmap)
1976 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
1978 mddev->queue->unplug_fn = raid6_unplug_device;
1979 mddev->queue->issue_flush_fn = raid6_issue_flush;
1980 return 0;
1981 abort:
1982 if (conf) {
1983 print_raid6_conf(conf);
1984 if (conf->stripe_hashtbl)
1985 free_pages((unsigned long) conf->stripe_hashtbl,
1986 HASH_PAGES_ORDER);
1987 kfree(conf);
1989 mddev->private = NULL;
1990 printk(KERN_ALERT "raid6: failed to run raid set %s\n", mdname(mddev));
1991 return -EIO;
1996 static int stop (mddev_t *mddev)
1998 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2000 md_unregister_thread(mddev->thread);
2001 mddev->thread = NULL;
2002 shrink_stripes(conf);
2003 free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
2004 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2005 kfree(conf);
2006 mddev->private = NULL;
2007 return 0;
2010 #if RAID6_DUMPSTATE
2011 static void print_sh (struct seq_file *seq, struct stripe_head *sh)
2013 int i;
2015 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
2016 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2017 seq_printf(seq, "sh %llu, count %d.\n",
2018 (unsigned long long)sh->sector, atomic_read(&sh->count));
2019 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
2020 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
2021 seq_printf(seq, "(cache%d: %p %ld) ",
2022 i, sh->dev[i].page, sh->dev[i].flags);
2024 seq_printf(seq, "\n");
2027 static void printall (struct seq_file *seq, raid6_conf_t *conf)
2029 struct stripe_head *sh;
2030 int i;
2032 spin_lock_irq(&conf->device_lock);
2033 for (i = 0; i < NR_HASH; i++) {
2034 sh = conf->stripe_hashtbl[i];
2035 for (; sh; sh = sh->hash_next) {
2036 if (sh->raid_conf != conf)
2037 continue;
2038 print_sh(seq, sh);
2041 spin_unlock_irq(&conf->device_lock);
2043 #endif
2045 static void status (struct seq_file *seq, mddev_t *mddev)
2047 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2048 int i;
2050 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2051 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2052 for (i = 0; i < conf->raid_disks; i++)
2053 seq_printf (seq, "%s",
2054 conf->disks[i].rdev &&
2055 conf->disks[i].rdev->in_sync ? "U" : "_");
2056 seq_printf (seq, "]");
2057 #if RAID6_DUMPSTATE
2058 seq_printf (seq, "\n");
2059 printall(seq, conf);
2060 #endif
2063 static void print_raid6_conf (raid6_conf_t *conf)
2065 int i;
2066 struct disk_info *tmp;
2068 printk("RAID6 conf printout:\n");
2069 if (!conf) {
2070 printk("(conf==NULL)\n");
2071 return;
2073 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2074 conf->working_disks, conf->failed_disks);
2076 for (i = 0; i < conf->raid_disks; i++) {
2077 char b[BDEVNAME_SIZE];
2078 tmp = conf->disks + i;
2079 if (tmp->rdev)
2080 printk(" disk %d, o:%d, dev:%s\n",
2081 i, !tmp->rdev->faulty,
2082 bdevname(tmp->rdev->bdev,b));
2086 static int raid6_spare_active(mddev_t *mddev)
2088 int i;
2089 raid6_conf_t *conf = mddev->private;
2090 struct disk_info *tmp;
2092 for (i = 0; i < conf->raid_disks; i++) {
2093 tmp = conf->disks + i;
2094 if (tmp->rdev
2095 && !tmp->rdev->faulty
2096 && !tmp->rdev->in_sync) {
2097 mddev->degraded--;
2098 conf->failed_disks--;
2099 conf->working_disks++;
2100 tmp->rdev->in_sync = 1;
2103 print_raid6_conf(conf);
2104 return 0;
2107 static int raid6_remove_disk(mddev_t *mddev, int number)
2109 raid6_conf_t *conf = mddev->private;
2110 int err = 0;
2111 mdk_rdev_t *rdev;
2112 struct disk_info *p = conf->disks + number;
2114 print_raid6_conf(conf);
2115 rdev = p->rdev;
2116 if (rdev) {
2117 if (rdev->in_sync ||
2118 atomic_read(&rdev->nr_pending)) {
2119 err = -EBUSY;
2120 goto abort;
2122 p->rdev = NULL;
2123 synchronize_rcu();
2124 if (atomic_read(&rdev->nr_pending)) {
2125 /* lost the race, try later */
2126 err = -EBUSY;
2127 p->rdev = rdev;
2131 abort:
2133 print_raid6_conf(conf);
2134 return err;
2137 static int raid6_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2139 raid6_conf_t *conf = mddev->private;
2140 int found = 0;
2141 int disk;
2142 struct disk_info *p;
2144 if (mddev->degraded > 2)
2145 /* no point adding a device */
2146 return 0;
2148 * find the disk ...
2150 for (disk=0; disk < mddev->raid_disks; disk++)
2151 if ((p=conf->disks + disk)->rdev == NULL) {
2152 rdev->in_sync = 0;
2153 rdev->raid_disk = disk;
2154 found = 1;
2155 if (rdev->saved_raid_disk != disk)
2156 conf->fullsync = 1;
2157 p->rdev = rdev;
2158 break;
2160 print_raid6_conf(conf);
2161 return found;
2164 static int raid6_resize(mddev_t *mddev, sector_t sectors)
2166 /* no resync is happening, and there is enough space
2167 * on all devices, so we can resize.
2168 * We need to make sure resync covers any new space.
2169 * If the array is shrinking we should possibly wait until
2170 * any io in the removed space completes, but it hardly seems
2171 * worth it.
2173 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2174 mddev->array_size = (sectors * (mddev->raid_disks-2))>>1;
2175 set_capacity(mddev->gendisk, mddev->array_size << 1);
2176 mddev->changed = 1;
2177 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2178 mddev->recovery_cp = mddev->size << 1;
2179 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2181 mddev->size = sectors /2;
2182 mddev->resync_max_sectors = sectors;
2183 return 0;
2186 static void raid6_quiesce(mddev_t *mddev, int state)
2188 raid6_conf_t *conf = mddev_to_conf(mddev);
2190 switch(state) {
2191 case 1: /* stop all writes */
2192 spin_lock_irq(&conf->device_lock);
2193 conf->quiesce = 1;
2194 wait_event_lock_irq(conf->wait_for_stripe,
2195 atomic_read(&conf->active_stripes) == 0,
2196 conf->device_lock, /* nothing */);
2197 spin_unlock_irq(&conf->device_lock);
2198 break;
2200 case 0: /* re-enable writes */
2201 spin_lock_irq(&conf->device_lock);
2202 conf->quiesce = 0;
2203 wake_up(&conf->wait_for_stripe);
2204 spin_unlock_irq(&conf->device_lock);
2205 break;
2207 if (mddev->thread) {
2208 if (mddev->bitmap)
2209 mddev->thread->timeout = mddev->bitmap->daemon_sleep * HZ;
2210 else
2211 mddev->thread->timeout = MAX_SCHEDULE_TIMEOUT;
2212 md_wakeup_thread(mddev->thread);
2215 static mdk_personality_t raid6_personality=
2217 .name = "raid6",
2218 .owner = THIS_MODULE,
2219 .make_request = make_request,
2220 .run = run,
2221 .stop = stop,
2222 .status = status,
2223 .error_handler = error,
2224 .hot_add_disk = raid6_add_disk,
2225 .hot_remove_disk= raid6_remove_disk,
2226 .spare_active = raid6_spare_active,
2227 .sync_request = sync_request,
2228 .resize = raid6_resize,
2229 .quiesce = raid6_quiesce,
2232 static int __init raid6_init (void)
2234 int e;
2236 e = raid6_select_algo();
2237 if ( e )
2238 return e;
2240 return register_md_personality (RAID6, &raid6_personality);
2243 static void raid6_exit (void)
2245 unregister_md_personality (RAID6);
2248 module_init(raid6_init);
2249 module_exit(raid6_exit);
2250 MODULE_LICENSE("GPL");
2251 MODULE_ALIAS("md-personality-8"); /* RAID6 */