[PATCH] USB: Remove LINUX_VERSION_CODE check in pwc/pwc-ctrl.c
[linux-2.6.22.y-op.git] / drivers / md / raid6main.c
blobf618a53b98bee235cca1160e4d08b16bd955ff0c
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 NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
44 #define HASH_MASK (NR_HASH - 1)
46 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
48 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
49 * order without overlap. There may be several bio's per stripe+device, and
50 * a bio could span several devices.
51 * When walking this list for a particular stripe+device, we must never proceed
52 * beyond a bio that extends past this device, as the next bio might no longer
53 * be valid.
54 * This macro is used to determine the 'next' bio in the list, given the sector
55 * of the current stripe+device
57 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
59 * The following can be used to debug the driver
61 #define RAID6_DEBUG 0 /* Extremely verbose printk */
62 #define RAID6_PARANOIA 1 /* Check spinlocks */
63 #define RAID6_DUMPSTATE 0 /* Include stripe cache state in /proc/mdstat */
64 #if RAID6_PARANOIA && defined(CONFIG_SMP)
65 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
66 #else
67 # define CHECK_DEVLOCK()
68 #endif
70 #define PRINTK(x...) ((void)(RAID6_DEBUG && printk(KERN_DEBUG x)))
71 #if RAID6_DEBUG
72 #undef inline
73 #undef __inline__
74 #define inline
75 #define __inline__
76 #endif
78 #if !RAID6_USE_EMPTY_ZERO_PAGE
79 /* In .bss so it's zeroed */
80 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
81 #endif
83 static inline int raid6_next_disk(int disk, int raid_disks)
85 disk++;
86 return (disk < raid_disks) ? disk : 0;
89 static void print_raid6_conf (raid6_conf_t *conf);
91 static void __release_stripe(raid6_conf_t *conf, struct stripe_head *sh)
93 if (atomic_dec_and_test(&sh->count)) {
94 if (!list_empty(&sh->lru))
95 BUG();
96 if (atomic_read(&conf->active_stripes)==0)
97 BUG();
98 if (test_bit(STRIPE_HANDLE, &sh->state)) {
99 if (test_bit(STRIPE_DELAYED, &sh->state))
100 list_add_tail(&sh->lru, &conf->delayed_list);
101 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
102 conf->seq_write == sh->bm_seq)
103 list_add_tail(&sh->lru, &conf->bitmap_list);
104 else {
105 clear_bit(STRIPE_BIT_DELAY, &sh->state);
106 list_add_tail(&sh->lru, &conf->handle_list);
108 md_wakeup_thread(conf->mddev->thread);
109 } else {
110 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
111 atomic_dec(&conf->preread_active_stripes);
112 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
113 md_wakeup_thread(conf->mddev->thread);
115 list_add_tail(&sh->lru, &conf->inactive_list);
116 atomic_dec(&conf->active_stripes);
117 if (!conf->inactive_blocked ||
118 atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
119 wake_up(&conf->wait_for_stripe);
123 static void release_stripe(struct stripe_head *sh)
125 raid6_conf_t *conf = sh->raid_conf;
126 unsigned long flags;
128 spin_lock_irqsave(&conf->device_lock, flags);
129 __release_stripe(conf, sh);
130 spin_unlock_irqrestore(&conf->device_lock, flags);
133 static inline void remove_hash(struct stripe_head *sh)
135 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
137 hlist_del_init(&sh->hash);
140 static inline void insert_hash(raid6_conf_t *conf, struct stripe_head *sh)
142 struct hlist_head *hp = stripe_hash(conf, sh->sector);
144 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
146 CHECK_DEVLOCK();
147 hlist_add_head(&sh->hash, hp);
151 /* find an idle stripe, make sure it is unhashed, and return it. */
152 static struct stripe_head *get_free_stripe(raid6_conf_t *conf)
154 struct stripe_head *sh = NULL;
155 struct list_head *first;
157 CHECK_DEVLOCK();
158 if (list_empty(&conf->inactive_list))
159 goto out;
160 first = conf->inactive_list.next;
161 sh = list_entry(first, struct stripe_head, lru);
162 list_del_init(first);
163 remove_hash(sh);
164 atomic_inc(&conf->active_stripes);
165 out:
166 return sh;
169 static void shrink_buffers(struct stripe_head *sh, int num)
171 struct page *p;
172 int i;
174 for (i=0; i<num ; i++) {
175 p = sh->dev[i].page;
176 if (!p)
177 continue;
178 sh->dev[i].page = NULL;
179 put_page(p);
183 static int grow_buffers(struct stripe_head *sh, int num)
185 int i;
187 for (i=0; i<num; i++) {
188 struct page *page;
190 if (!(page = alloc_page(GFP_KERNEL))) {
191 return 1;
193 sh->dev[i].page = page;
195 return 0;
198 static void raid6_build_block (struct stripe_head *sh, int i);
200 static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
202 raid6_conf_t *conf = sh->raid_conf;
203 int disks = conf->raid_disks, i;
205 if (atomic_read(&sh->count) != 0)
206 BUG();
207 if (test_bit(STRIPE_HANDLE, &sh->state))
208 BUG();
210 CHECK_DEVLOCK();
211 PRINTK("init_stripe called, stripe %llu\n",
212 (unsigned long long)sh->sector);
214 remove_hash(sh);
216 sh->sector = sector;
217 sh->pd_idx = pd_idx;
218 sh->state = 0;
220 for (i=disks; i--; ) {
221 struct r5dev *dev = &sh->dev[i];
223 if (dev->toread || dev->towrite || dev->written ||
224 test_bit(R5_LOCKED, &dev->flags)) {
225 PRINTK("sector=%llx i=%d %p %p %p %d\n",
226 (unsigned long long)sh->sector, i, dev->toread,
227 dev->towrite, dev->written,
228 test_bit(R5_LOCKED, &dev->flags));
229 BUG();
231 dev->flags = 0;
232 raid6_build_block(sh, i);
234 insert_hash(conf, sh);
237 static struct stripe_head *__find_stripe(raid6_conf_t *conf, sector_t sector)
239 struct stripe_head *sh;
240 struct hlist_node *hn;
242 CHECK_DEVLOCK();
243 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
244 hlist_for_each_entry (sh, hn, stripe_hash(conf, sector), hash)
245 if (sh->sector == sector)
246 return sh;
247 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
248 return NULL;
251 static void unplug_slaves(mddev_t *mddev);
253 static struct stripe_head *get_active_stripe(raid6_conf_t *conf, sector_t sector,
254 int pd_idx, int noblock)
256 struct stripe_head *sh;
258 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
260 spin_lock_irq(&conf->device_lock);
262 do {
263 wait_event_lock_irq(conf->wait_for_stripe,
264 conf->quiesce == 0,
265 conf->device_lock, /* nothing */);
266 sh = __find_stripe(conf, sector);
267 if (!sh) {
268 if (!conf->inactive_blocked)
269 sh = get_free_stripe(conf);
270 if (noblock && sh == NULL)
271 break;
272 if (!sh) {
273 conf->inactive_blocked = 1;
274 wait_event_lock_irq(conf->wait_for_stripe,
275 !list_empty(&conf->inactive_list) &&
276 (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
277 || !conf->inactive_blocked),
278 conf->device_lock,
279 unplug_slaves(conf->mddev);
281 conf->inactive_blocked = 0;
282 } else
283 init_stripe(sh, sector, pd_idx);
284 } else {
285 if (atomic_read(&sh->count)) {
286 if (!list_empty(&sh->lru))
287 BUG();
288 } else {
289 if (!test_bit(STRIPE_HANDLE, &sh->state))
290 atomic_inc(&conf->active_stripes);
291 if (list_empty(&sh->lru))
292 BUG();
293 list_del_init(&sh->lru);
296 } while (sh == NULL);
298 if (sh)
299 atomic_inc(&sh->count);
301 spin_unlock_irq(&conf->device_lock);
302 return sh;
305 static int grow_stripes(raid6_conf_t *conf, int num)
307 struct stripe_head *sh;
308 kmem_cache_t *sc;
309 int devs = conf->raid_disks;
311 sprintf(conf->cache_name, "raid6/%s", mdname(conf->mddev));
313 sc = kmem_cache_create(conf->cache_name,
314 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
315 0, 0, NULL, NULL);
316 if (!sc)
317 return 1;
318 conf->slab_cache = sc;
319 while (num--) {
320 sh = kmem_cache_alloc(sc, GFP_KERNEL);
321 if (!sh)
322 return 1;
323 memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
324 sh->raid_conf = conf;
325 spin_lock_init(&sh->lock);
327 if (grow_buffers(sh, conf->raid_disks)) {
328 shrink_buffers(sh, conf->raid_disks);
329 kmem_cache_free(sc, sh);
330 return 1;
332 /* we just created an active stripe so... */
333 atomic_set(&sh->count, 1);
334 atomic_inc(&conf->active_stripes);
335 INIT_LIST_HEAD(&sh->lru);
336 release_stripe(sh);
338 return 0;
341 static void shrink_stripes(raid6_conf_t *conf)
343 struct stripe_head *sh;
345 while (1) {
346 spin_lock_irq(&conf->device_lock);
347 sh = get_free_stripe(conf);
348 spin_unlock_irq(&conf->device_lock);
349 if (!sh)
350 break;
351 if (atomic_read(&sh->count))
352 BUG();
353 shrink_buffers(sh, conf->raid_disks);
354 kmem_cache_free(conf->slab_cache, sh);
355 atomic_dec(&conf->active_stripes);
357 kmem_cache_destroy(conf->slab_cache);
358 conf->slab_cache = NULL;
361 static int raid6_end_read_request(struct bio * bi, unsigned int bytes_done,
362 int error)
364 struct stripe_head *sh = bi->bi_private;
365 raid6_conf_t *conf = sh->raid_conf;
366 int disks = conf->raid_disks, i;
367 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
369 if (bi->bi_size)
370 return 1;
372 for (i=0 ; i<disks; i++)
373 if (bi == &sh->dev[i].req)
374 break;
376 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
377 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
378 uptodate);
379 if (i == disks) {
380 BUG();
381 return 0;
384 if (uptodate) {
385 #if 0
386 struct bio *bio;
387 unsigned long flags;
388 spin_lock_irqsave(&conf->device_lock, flags);
389 /* we can return a buffer if we bypassed the cache or
390 * if the top buffer is not in highmem. If there are
391 * multiple buffers, leave the extra work to
392 * handle_stripe
394 buffer = sh->bh_read[i];
395 if (buffer &&
396 (!PageHighMem(buffer->b_page)
397 || buffer->b_page == bh->b_page )
399 sh->bh_read[i] = buffer->b_reqnext;
400 buffer->b_reqnext = NULL;
401 } else
402 buffer = NULL;
403 spin_unlock_irqrestore(&conf->device_lock, flags);
404 if (sh->bh_page[i]==bh->b_page)
405 set_buffer_uptodate(bh);
406 if (buffer) {
407 if (buffer->b_page != bh->b_page)
408 memcpy(buffer->b_data, bh->b_data, bh->b_size);
409 buffer->b_end_io(buffer, 1);
411 #else
412 set_bit(R5_UPTODATE, &sh->dev[i].flags);
413 #endif
414 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
415 printk(KERN_INFO "raid6: read error corrected!!\n");
416 clear_bit(R5_ReadError, &sh->dev[i].flags);
417 clear_bit(R5_ReWrite, &sh->dev[i].flags);
419 if (atomic_read(&conf->disks[i].rdev->read_errors))
420 atomic_set(&conf->disks[i].rdev->read_errors, 0);
421 } else {
422 int retry = 0;
423 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
424 atomic_inc(&conf->disks[i].rdev->read_errors);
425 if (conf->mddev->degraded)
426 printk(KERN_WARNING "raid6: read error not correctable.\n");
427 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
428 /* Oh, no!!! */
429 printk(KERN_WARNING "raid6: read error NOT corrected!!\n");
430 else if (atomic_read(&conf->disks[i].rdev->read_errors)
431 > conf->max_nr_stripes)
432 printk(KERN_WARNING
433 "raid6: Too many read errors, failing device.\n");
434 else
435 retry = 1;
436 if (retry)
437 set_bit(R5_ReadError, &sh->dev[i].flags);
438 else {
439 clear_bit(R5_ReadError, &sh->dev[i].flags);
440 clear_bit(R5_ReWrite, &sh->dev[i].flags);
441 md_error(conf->mddev, conf->disks[i].rdev);
444 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
445 #if 0
446 /* must restore b_page before unlocking buffer... */
447 if (sh->bh_page[i] != bh->b_page) {
448 bh->b_page = sh->bh_page[i];
449 bh->b_data = page_address(bh->b_page);
450 clear_buffer_uptodate(bh);
452 #endif
453 clear_bit(R5_LOCKED, &sh->dev[i].flags);
454 set_bit(STRIPE_HANDLE, &sh->state);
455 release_stripe(sh);
456 return 0;
459 static int raid6_end_write_request (struct bio *bi, unsigned int bytes_done,
460 int error)
462 struct stripe_head *sh = bi->bi_private;
463 raid6_conf_t *conf = sh->raid_conf;
464 int disks = conf->raid_disks, i;
465 unsigned long flags;
466 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
468 if (bi->bi_size)
469 return 1;
471 for (i=0 ; i<disks; i++)
472 if (bi == &sh->dev[i].req)
473 break;
475 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
476 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
477 uptodate);
478 if (i == disks) {
479 BUG();
480 return 0;
483 spin_lock_irqsave(&conf->device_lock, flags);
484 if (!uptodate)
485 md_error(conf->mddev, conf->disks[i].rdev);
487 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
489 clear_bit(R5_LOCKED, &sh->dev[i].flags);
490 set_bit(STRIPE_HANDLE, &sh->state);
491 __release_stripe(conf, sh);
492 spin_unlock_irqrestore(&conf->device_lock, flags);
493 return 0;
497 static sector_t compute_blocknr(struct stripe_head *sh, int i);
499 static void raid6_build_block (struct stripe_head *sh, int i)
501 struct r5dev *dev = &sh->dev[i];
502 int pd_idx = sh->pd_idx;
503 int qd_idx = raid6_next_disk(pd_idx, sh->raid_conf->raid_disks);
505 bio_init(&dev->req);
506 dev->req.bi_io_vec = &dev->vec;
507 dev->req.bi_vcnt++;
508 dev->req.bi_max_vecs++;
509 dev->vec.bv_page = dev->page;
510 dev->vec.bv_len = STRIPE_SIZE;
511 dev->vec.bv_offset = 0;
513 dev->req.bi_sector = sh->sector;
514 dev->req.bi_private = sh;
516 dev->flags = 0;
517 if (i != pd_idx && i != qd_idx)
518 dev->sector = compute_blocknr(sh, i);
521 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
523 char b[BDEVNAME_SIZE];
524 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
525 PRINTK("raid6: error called\n");
527 if (!test_bit(Faulty, &rdev->flags)) {
528 mddev->sb_dirty = 1;
529 if (test_bit(In_sync, &rdev->flags)) {
530 conf->working_disks--;
531 mddev->degraded++;
532 conf->failed_disks++;
533 clear_bit(In_sync, &rdev->flags);
535 * if recovery was running, make sure it aborts.
537 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
539 set_bit(Faulty, &rdev->flags);
540 printk (KERN_ALERT
541 "raid6: Disk failure on %s, disabling device."
542 " Operation continuing on %d devices\n",
543 bdevname(rdev->bdev,b), conf->working_disks);
548 * Input: a 'big' sector number,
549 * Output: index of the data and parity disk, and the sector # in them.
551 static sector_t raid6_compute_sector(sector_t r_sector, unsigned int raid_disks,
552 unsigned int data_disks, unsigned int * dd_idx,
553 unsigned int * pd_idx, raid6_conf_t *conf)
555 long stripe;
556 unsigned long chunk_number;
557 unsigned int chunk_offset;
558 sector_t new_sector;
559 int sectors_per_chunk = conf->chunk_size >> 9;
561 /* First compute the information on this sector */
564 * Compute the chunk number and the sector offset inside the chunk
566 chunk_offset = sector_div(r_sector, sectors_per_chunk);
567 chunk_number = r_sector;
568 if ( r_sector != chunk_number ) {
569 printk(KERN_CRIT "raid6: ERROR: r_sector = %llu, chunk_number = %lu\n",
570 (unsigned long long)r_sector, (unsigned long)chunk_number);
571 BUG();
575 * Compute the stripe number
577 stripe = chunk_number / data_disks;
580 * Compute the data disk and parity disk indexes inside the stripe
582 *dd_idx = chunk_number % data_disks;
585 * Select the parity disk based on the user selected algorithm.
588 /**** FIX THIS ****/
589 switch (conf->algorithm) {
590 case ALGORITHM_LEFT_ASYMMETRIC:
591 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
592 if (*pd_idx == raid_disks-1)
593 (*dd_idx)++; /* Q D D D P */
594 else if (*dd_idx >= *pd_idx)
595 (*dd_idx) += 2; /* D D P Q D */
596 break;
597 case ALGORITHM_RIGHT_ASYMMETRIC:
598 *pd_idx = stripe % raid_disks;
599 if (*pd_idx == raid_disks-1)
600 (*dd_idx)++; /* Q D D D P */
601 else if (*dd_idx >= *pd_idx)
602 (*dd_idx) += 2; /* D D P Q D */
603 break;
604 case ALGORITHM_LEFT_SYMMETRIC:
605 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
606 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
607 break;
608 case ALGORITHM_RIGHT_SYMMETRIC:
609 *pd_idx = stripe % raid_disks;
610 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
611 break;
612 default:
613 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
614 conf->algorithm);
617 PRINTK("raid6: chunk_number = %lu, pd_idx = %u, dd_idx = %u\n",
618 chunk_number, *pd_idx, *dd_idx);
621 * Finally, compute the new sector number
623 new_sector = (sector_t) stripe * sectors_per_chunk + chunk_offset;
624 return new_sector;
628 static sector_t compute_blocknr(struct stripe_head *sh, int i)
630 raid6_conf_t *conf = sh->raid_conf;
631 int raid_disks = conf->raid_disks, data_disks = raid_disks - 2;
632 sector_t new_sector = sh->sector, check;
633 int sectors_per_chunk = conf->chunk_size >> 9;
634 sector_t stripe;
635 int chunk_offset;
636 int chunk_number, dummy1, dummy2, dd_idx = i;
637 sector_t r_sector;
638 int i0 = i;
640 chunk_offset = sector_div(new_sector, sectors_per_chunk);
641 stripe = new_sector;
642 if ( new_sector != stripe ) {
643 printk(KERN_CRIT "raid6: ERROR: new_sector = %llu, stripe = %lu\n",
644 (unsigned long long)new_sector, (unsigned long)stripe);
645 BUG();
648 switch (conf->algorithm) {
649 case ALGORITHM_LEFT_ASYMMETRIC:
650 case ALGORITHM_RIGHT_ASYMMETRIC:
651 if (sh->pd_idx == raid_disks-1)
652 i--; /* Q D D D P */
653 else if (i > sh->pd_idx)
654 i -= 2; /* D D P Q D */
655 break;
656 case ALGORITHM_LEFT_SYMMETRIC:
657 case ALGORITHM_RIGHT_SYMMETRIC:
658 if (sh->pd_idx == raid_disks-1)
659 i--; /* Q D D D P */
660 else {
661 /* D D P Q D */
662 if (i < sh->pd_idx)
663 i += raid_disks;
664 i -= (sh->pd_idx + 2);
666 break;
667 default:
668 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
669 conf->algorithm);
672 PRINTK("raid6: compute_blocknr: pd_idx = %u, i0 = %u, i = %u\n", sh->pd_idx, i0, i);
674 chunk_number = stripe * data_disks + i;
675 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
677 check = raid6_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
678 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
679 printk(KERN_CRIT "raid6: compute_blocknr: map not correct\n");
680 return 0;
682 return r_sector;
688 * Copy data between a page in the stripe cache, and one or more bion
689 * The page could align with the middle of the bio, or there could be
690 * several bion, each with several bio_vecs, which cover part of the page
691 * Multiple bion are linked together on bi_next. There may be extras
692 * at the end of this list. We ignore them.
694 static void copy_data(int frombio, struct bio *bio,
695 struct page *page,
696 sector_t sector)
698 char *pa = page_address(page);
699 struct bio_vec *bvl;
700 int i;
701 int page_offset;
703 if (bio->bi_sector >= sector)
704 page_offset = (signed)(bio->bi_sector - sector) * 512;
705 else
706 page_offset = (signed)(sector - bio->bi_sector) * -512;
707 bio_for_each_segment(bvl, bio, i) {
708 int len = bio_iovec_idx(bio,i)->bv_len;
709 int clen;
710 int b_offset = 0;
712 if (page_offset < 0) {
713 b_offset = -page_offset;
714 page_offset += b_offset;
715 len -= b_offset;
718 if (len > 0 && page_offset + len > STRIPE_SIZE)
719 clen = STRIPE_SIZE - page_offset;
720 else clen = len;
722 if (clen > 0) {
723 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
724 if (frombio)
725 memcpy(pa+page_offset, ba+b_offset, clen);
726 else
727 memcpy(ba+b_offset, pa+page_offset, clen);
728 __bio_kunmap_atomic(ba, KM_USER0);
730 if (clen < len) /* hit end of page */
731 break;
732 page_offset += len;
736 #define check_xor() do { \
737 if (count == MAX_XOR_BLOCKS) { \
738 xor_block(count, STRIPE_SIZE, ptr); \
739 count = 1; \
741 } while(0)
743 /* Compute P and Q syndromes */
744 static void compute_parity(struct stripe_head *sh, int method)
746 raid6_conf_t *conf = sh->raid_conf;
747 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
748 struct bio *chosen;
749 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
750 void *ptrs[disks];
752 qd_idx = raid6_next_disk(pd_idx, disks);
753 d0_idx = raid6_next_disk(qd_idx, disks);
755 PRINTK("compute_parity, stripe %llu, method %d\n",
756 (unsigned long long)sh->sector, method);
758 switch(method) {
759 case READ_MODIFY_WRITE:
760 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
761 case RECONSTRUCT_WRITE:
762 for (i= disks; i-- ;)
763 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
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;
773 break;
774 case CHECK_PARITY:
775 BUG(); /* Not implemented yet */
778 for (i = disks; i--;)
779 if (sh->dev[i].written) {
780 sector_t sector = sh->dev[i].sector;
781 struct bio *wbi = sh->dev[i].written;
782 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
783 copy_data(1, wbi, sh->dev[i].page, sector);
784 wbi = r5_next_bio(wbi, sector);
787 set_bit(R5_LOCKED, &sh->dev[i].flags);
788 set_bit(R5_UPTODATE, &sh->dev[i].flags);
791 // switch(method) {
792 // case RECONSTRUCT_WRITE:
793 // case CHECK_PARITY:
794 // case UPDATE_PARITY:
795 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
796 /* FIX: Is this ordering of drives even remotely optimal? */
797 count = 0;
798 i = d0_idx;
799 do {
800 ptrs[count++] = page_address(sh->dev[i].page);
801 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
802 printk("block %d/%d not uptodate on parity calc\n", i,count);
803 i = raid6_next_disk(i, disks);
804 } while ( i != d0_idx );
805 // break;
806 // }
808 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
810 switch(method) {
811 case RECONSTRUCT_WRITE:
812 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
813 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
814 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
815 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
816 break;
817 case UPDATE_PARITY:
818 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
819 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
820 break;
824 /* Compute one missing block */
825 static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
827 raid6_conf_t *conf = sh->raid_conf;
828 int i, count, disks = conf->raid_disks;
829 void *ptr[MAX_XOR_BLOCKS], *p;
830 int pd_idx = sh->pd_idx;
831 int qd_idx = raid6_next_disk(pd_idx, disks);
833 PRINTK("compute_block_1, stripe %llu, idx %d\n",
834 (unsigned long long)sh->sector, dd_idx);
836 if ( dd_idx == qd_idx ) {
837 /* We're actually computing the Q drive */
838 compute_parity(sh, UPDATE_PARITY);
839 } else {
840 ptr[0] = page_address(sh->dev[dd_idx].page);
841 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
842 count = 1;
843 for (i = disks ; i--; ) {
844 if (i == dd_idx || i == qd_idx)
845 continue;
846 p = page_address(sh->dev[i].page);
847 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
848 ptr[count++] = p;
849 else
850 printk("compute_block() %d, stripe %llu, %d"
851 " not present\n", dd_idx,
852 (unsigned long long)sh->sector, i);
854 check_xor();
856 if (count != 1)
857 xor_block(count, STRIPE_SIZE, ptr);
858 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
859 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
863 /* Compute two missing blocks */
864 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
866 raid6_conf_t *conf = sh->raid_conf;
867 int i, count, disks = conf->raid_disks;
868 int pd_idx = sh->pd_idx;
869 int qd_idx = raid6_next_disk(pd_idx, disks);
870 int d0_idx = raid6_next_disk(qd_idx, disks);
871 int faila, failb;
873 /* faila and failb are disk numbers relative to d0_idx */
874 /* pd_idx become disks-2 and qd_idx become disks-1 */
875 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
876 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
878 BUG_ON(faila == failb);
879 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
881 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
882 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
884 if ( failb == disks-1 ) {
885 /* Q disk is one of the missing disks */
886 if ( faila == disks-2 ) {
887 /* Missing P+Q, just recompute */
888 compute_parity(sh, UPDATE_PARITY);
889 return;
890 } else {
891 /* We're missing D+Q; recompute D from P */
892 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
893 compute_parity(sh, UPDATE_PARITY); /* Is this necessary? */
894 return;
898 /* We're missing D+P or D+D; build pointer table */
900 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
901 void *ptrs[disks];
903 count = 0;
904 i = d0_idx;
905 do {
906 ptrs[count++] = page_address(sh->dev[i].page);
907 i = raid6_next_disk(i, disks);
908 if (i != dd_idx1 && i != dd_idx2 &&
909 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
910 printk("compute_2 with missing block %d/%d\n", count, i);
911 } while ( i != d0_idx );
913 if ( failb == disks-2 ) {
914 /* We're missing D+P. */
915 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
916 } else {
917 /* We're missing D+D. */
918 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
921 /* Both the above update both missing blocks */
922 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
923 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
929 * Each stripe/dev can have one or more bion attached.
930 * toread/towrite point to the first in a chain.
931 * The bi_next chain must be in order.
933 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
935 struct bio **bip;
936 raid6_conf_t *conf = sh->raid_conf;
937 int firstwrite=0;
939 PRINTK("adding bh b#%llu to stripe s#%llu\n",
940 (unsigned long long)bi->bi_sector,
941 (unsigned long long)sh->sector);
944 spin_lock(&sh->lock);
945 spin_lock_irq(&conf->device_lock);
946 if (forwrite) {
947 bip = &sh->dev[dd_idx].towrite;
948 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
949 firstwrite = 1;
950 } else
951 bip = &sh->dev[dd_idx].toread;
952 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
953 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
954 goto overlap;
955 bip = &(*bip)->bi_next;
957 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
958 goto overlap;
960 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
961 BUG();
962 if (*bip)
963 bi->bi_next = *bip;
964 *bip = bi;
965 bi->bi_phys_segments ++;
966 spin_unlock_irq(&conf->device_lock);
967 spin_unlock(&sh->lock);
969 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
970 (unsigned long long)bi->bi_sector,
971 (unsigned long long)sh->sector, dd_idx);
973 if (conf->mddev->bitmap && firstwrite) {
974 sh->bm_seq = conf->seq_write;
975 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
976 STRIPE_SECTORS, 0);
977 set_bit(STRIPE_BIT_DELAY, &sh->state);
980 if (forwrite) {
981 /* check if page is covered */
982 sector_t sector = sh->dev[dd_idx].sector;
983 for (bi=sh->dev[dd_idx].towrite;
984 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
985 bi && bi->bi_sector <= sector;
986 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
987 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
988 sector = bi->bi_sector + (bi->bi_size>>9);
990 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
991 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
993 return 1;
995 overlap:
996 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
997 spin_unlock_irq(&conf->device_lock);
998 spin_unlock(&sh->lock);
999 return 0;
1003 static int page_is_zero(struct page *p)
1005 char *a = page_address(p);
1006 return ((*(u32*)a) == 0 &&
1007 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1010 * handle_stripe - do things to a stripe.
1012 * We lock the stripe and then examine the state of various bits
1013 * to see what needs to be done.
1014 * Possible results:
1015 * return some read request which now have data
1016 * return some write requests which are safely on disc
1017 * schedule a read on some buffers
1018 * schedule a write of some buffers
1019 * return confirmation of parity correctness
1021 * Parity calculations are done inside the stripe lock
1022 * buffers are taken off read_list or write_list, and bh_cache buffers
1023 * get BH_Lock set before the stripe lock is released.
1027 static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
1029 raid6_conf_t *conf = sh->raid_conf;
1030 int disks = conf->raid_disks;
1031 struct bio *return_bi= NULL;
1032 struct bio *bi;
1033 int i;
1034 int syncing;
1035 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1036 int non_overwrite = 0;
1037 int failed_num[2] = {0, 0};
1038 struct r5dev *dev, *pdev, *qdev;
1039 int pd_idx = sh->pd_idx;
1040 int qd_idx = raid6_next_disk(pd_idx, disks);
1041 int p_failed, q_failed;
1043 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1044 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1045 pd_idx, qd_idx);
1047 spin_lock(&sh->lock);
1048 clear_bit(STRIPE_HANDLE, &sh->state);
1049 clear_bit(STRIPE_DELAYED, &sh->state);
1051 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1052 /* Now to look around and see what can be done */
1054 rcu_read_lock();
1055 for (i=disks; i--; ) {
1056 mdk_rdev_t *rdev;
1057 dev = &sh->dev[i];
1058 clear_bit(R5_Insync, &dev->flags);
1060 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1061 i, dev->flags, dev->toread, dev->towrite, dev->written);
1062 /* maybe we can reply to a read */
1063 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1064 struct bio *rbi, *rbi2;
1065 PRINTK("Return read for disc %d\n", i);
1066 spin_lock_irq(&conf->device_lock);
1067 rbi = dev->toread;
1068 dev->toread = NULL;
1069 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1070 wake_up(&conf->wait_for_overlap);
1071 spin_unlock_irq(&conf->device_lock);
1072 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1073 copy_data(0, rbi, dev->page, dev->sector);
1074 rbi2 = r5_next_bio(rbi, dev->sector);
1075 spin_lock_irq(&conf->device_lock);
1076 if (--rbi->bi_phys_segments == 0) {
1077 rbi->bi_next = return_bi;
1078 return_bi = rbi;
1080 spin_unlock_irq(&conf->device_lock);
1081 rbi = rbi2;
1085 /* now count some things */
1086 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1087 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1090 if (dev->toread) to_read++;
1091 if (dev->towrite) {
1092 to_write++;
1093 if (!test_bit(R5_OVERWRITE, &dev->flags))
1094 non_overwrite++;
1096 if (dev->written) written++;
1097 rdev = rcu_dereference(conf->disks[i].rdev);
1098 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1099 /* The ReadError flag will just be confusing now */
1100 clear_bit(R5_ReadError, &dev->flags);
1101 clear_bit(R5_ReWrite, &dev->flags);
1103 if (!rdev || !test_bit(In_sync, &rdev->flags)
1104 || test_bit(R5_ReadError, &dev->flags)) {
1105 if ( failed < 2 )
1106 failed_num[failed] = i;
1107 failed++;
1108 } else
1109 set_bit(R5_Insync, &dev->flags);
1111 rcu_read_unlock();
1112 PRINTK("locked=%d uptodate=%d to_read=%d"
1113 " to_write=%d failed=%d failed_num=%d,%d\n",
1114 locked, uptodate, to_read, to_write, failed,
1115 failed_num[0], failed_num[1]);
1116 /* check if the array has lost >2 devices and, if so, some requests might
1117 * need to be failed
1119 if (failed > 2 && to_read+to_write+written) {
1120 for (i=disks; i--; ) {
1121 int bitmap_end = 0;
1123 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1124 mdk_rdev_t *rdev;
1125 rcu_read_lock();
1126 rdev = rcu_dereference(conf->disks[i].rdev);
1127 if (rdev && test_bit(In_sync, &rdev->flags))
1128 /* multiple read failures in one stripe */
1129 md_error(conf->mddev, rdev);
1130 rcu_read_unlock();
1133 spin_lock_irq(&conf->device_lock);
1134 /* fail all writes first */
1135 bi = sh->dev[i].towrite;
1136 sh->dev[i].towrite = NULL;
1137 if (bi) { to_write--; bitmap_end = 1; }
1139 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1140 wake_up(&conf->wait_for_overlap);
1142 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1143 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1144 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1145 if (--bi->bi_phys_segments == 0) {
1146 md_write_end(conf->mddev);
1147 bi->bi_next = return_bi;
1148 return_bi = bi;
1150 bi = nextbi;
1152 /* and fail all 'written' */
1153 bi = sh->dev[i].written;
1154 sh->dev[i].written = NULL;
1155 if (bi) bitmap_end = 1;
1156 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1157 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1158 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1159 if (--bi->bi_phys_segments == 0) {
1160 md_write_end(conf->mddev);
1161 bi->bi_next = return_bi;
1162 return_bi = bi;
1164 bi = bi2;
1167 /* fail any reads if this device is non-operational */
1168 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1169 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1170 bi = sh->dev[i].toread;
1171 sh->dev[i].toread = NULL;
1172 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1173 wake_up(&conf->wait_for_overlap);
1174 if (bi) to_read--;
1175 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1176 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1177 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1178 if (--bi->bi_phys_segments == 0) {
1179 bi->bi_next = return_bi;
1180 return_bi = bi;
1182 bi = nextbi;
1185 spin_unlock_irq(&conf->device_lock);
1186 if (bitmap_end)
1187 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1188 STRIPE_SECTORS, 0, 0);
1191 if (failed > 2 && syncing) {
1192 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1193 clear_bit(STRIPE_SYNCING, &sh->state);
1194 syncing = 0;
1198 * might be able to return some write requests if the parity blocks
1199 * are safe, or on a failed drive
1201 pdev = &sh->dev[pd_idx];
1202 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
1203 || (failed >= 2 && failed_num[1] == pd_idx);
1204 qdev = &sh->dev[qd_idx];
1205 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
1206 || (failed >= 2 && failed_num[1] == qd_idx);
1208 if ( written &&
1209 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
1210 && !test_bit(R5_LOCKED, &pdev->flags)
1211 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
1212 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
1213 && !test_bit(R5_LOCKED, &qdev->flags)
1214 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
1215 /* any written block on an uptodate or failed drive can be
1216 * returned. Note that if we 'wrote' to a failed drive,
1217 * it will be UPTODATE, but never LOCKED, so we don't need
1218 * to test 'failed' directly.
1220 for (i=disks; i--; )
1221 if (sh->dev[i].written) {
1222 dev = &sh->dev[i];
1223 if (!test_bit(R5_LOCKED, &dev->flags) &&
1224 test_bit(R5_UPTODATE, &dev->flags) ) {
1225 /* We can return any write requests */
1226 int bitmap_end = 0;
1227 struct bio *wbi, *wbi2;
1228 PRINTK("Return write for stripe %llu disc %d\n",
1229 (unsigned long long)sh->sector, i);
1230 spin_lock_irq(&conf->device_lock);
1231 wbi = dev->written;
1232 dev->written = NULL;
1233 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1234 wbi2 = r5_next_bio(wbi, dev->sector);
1235 if (--wbi->bi_phys_segments == 0) {
1236 md_write_end(conf->mddev);
1237 wbi->bi_next = return_bi;
1238 return_bi = wbi;
1240 wbi = wbi2;
1242 if (dev->towrite == NULL)
1243 bitmap_end = 1;
1244 spin_unlock_irq(&conf->device_lock);
1245 if (bitmap_end)
1246 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1247 STRIPE_SECTORS,
1248 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1253 /* Now we might consider reading some blocks, either to check/generate
1254 * parity, or to satisfy requests
1255 * or to load a block that is being partially written.
1257 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
1258 for (i=disks; i--;) {
1259 dev = &sh->dev[i];
1260 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1261 (dev->toread ||
1262 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1263 syncing ||
1264 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
1265 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
1268 /* we would like to get this block, possibly
1269 * by computing it, but we might not be able to
1271 if (uptodate == disks-1) {
1272 PRINTK("Computing stripe %llu block %d\n",
1273 (unsigned long long)sh->sector, i);
1274 compute_block_1(sh, i, 0);
1275 uptodate++;
1276 } else if ( uptodate == disks-2 && failed >= 2 ) {
1277 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
1278 int other;
1279 for (other=disks; other--;) {
1280 if ( other == i )
1281 continue;
1282 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
1283 break;
1285 BUG_ON(other < 0);
1286 PRINTK("Computing stripe %llu blocks %d,%d\n",
1287 (unsigned long long)sh->sector, i, other);
1288 compute_block_2(sh, i, other);
1289 uptodate += 2;
1290 } else if (test_bit(R5_Insync, &dev->flags)) {
1291 set_bit(R5_LOCKED, &dev->flags);
1292 set_bit(R5_Wantread, &dev->flags);
1293 #if 0
1294 /* if I am just reading this block and we don't have
1295 a failed drive, or any pending writes then sidestep the cache */
1296 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1297 ! syncing && !failed && !to_write) {
1298 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1299 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1301 #endif
1302 locked++;
1303 PRINTK("Reading block %d (sync=%d)\n",
1304 i, syncing);
1308 set_bit(STRIPE_HANDLE, &sh->state);
1311 /* now to consider writing and what else, if anything should be read */
1312 if (to_write) {
1313 int rcw=0, must_compute=0;
1314 for (i=disks ; i--;) {
1315 dev = &sh->dev[i];
1316 /* Would I have to read this buffer for reconstruct_write */
1317 if (!test_bit(R5_OVERWRITE, &dev->flags)
1318 && i != pd_idx && i != qd_idx
1319 && (!test_bit(R5_LOCKED, &dev->flags)
1320 #if 0
1321 || sh->bh_page[i] != bh->b_page
1322 #endif
1323 ) &&
1324 !test_bit(R5_UPTODATE, &dev->flags)) {
1325 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1326 else {
1327 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
1328 must_compute++;
1332 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
1333 (unsigned long long)sh->sector, rcw, must_compute);
1334 set_bit(STRIPE_HANDLE, &sh->state);
1336 if (rcw > 0)
1337 /* want reconstruct write, but need to get some data */
1338 for (i=disks; i--;) {
1339 dev = &sh->dev[i];
1340 if (!test_bit(R5_OVERWRITE, &dev->flags)
1341 && !(failed == 0 && (i == pd_idx || i == qd_idx))
1342 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1343 test_bit(R5_Insync, &dev->flags)) {
1344 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1346 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
1347 (unsigned long long)sh->sector, i);
1348 set_bit(R5_LOCKED, &dev->flags);
1349 set_bit(R5_Wantread, &dev->flags);
1350 locked++;
1351 } else {
1352 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
1353 (unsigned long long)sh->sector, i);
1354 set_bit(STRIPE_DELAYED, &sh->state);
1355 set_bit(STRIPE_HANDLE, &sh->state);
1359 /* now if nothing is locked, and if we have enough data, we can start a write request */
1360 if (locked == 0 && rcw == 0 &&
1361 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1362 if ( must_compute > 0 ) {
1363 /* We have failed blocks and need to compute them */
1364 switch ( failed ) {
1365 case 0: BUG();
1366 case 1: compute_block_1(sh, failed_num[0], 0); break;
1367 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
1368 default: BUG(); /* This request should have been failed? */
1372 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
1373 compute_parity(sh, RECONSTRUCT_WRITE);
1374 /* now every locked buffer is ready to be written */
1375 for (i=disks; i--;)
1376 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1377 PRINTK("Writing stripe %llu block %d\n",
1378 (unsigned long long)sh->sector, i);
1379 locked++;
1380 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1382 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
1383 set_bit(STRIPE_INSYNC, &sh->state);
1385 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1386 atomic_dec(&conf->preread_active_stripes);
1387 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1388 md_wakeup_thread(conf->mddev->thread);
1393 /* maybe we need to check and possibly fix the parity for this stripe
1394 * Any reads will already have been scheduled, so we just see if enough data
1395 * is available
1397 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
1398 int update_p = 0, update_q = 0;
1399 struct r5dev *dev;
1401 set_bit(STRIPE_HANDLE, &sh->state);
1403 BUG_ON(failed>2);
1404 BUG_ON(uptodate < disks);
1405 /* Want to check and possibly repair P and Q.
1406 * However there could be one 'failed' device, in which
1407 * case we can only check one of them, possibly using the
1408 * other to generate missing data
1411 /* If !tmp_page, we cannot do the calculations,
1412 * but as we have set STRIPE_HANDLE, we will soon be called
1413 * by stripe_handle with a tmp_page - just wait until then.
1415 if (tmp_page) {
1416 if (failed == q_failed) {
1417 /* The only possible failed device holds 'Q', so it makes
1418 * sense to check P (If anything else were failed, we would
1419 * have used P to recreate it).
1421 compute_block_1(sh, pd_idx, 1);
1422 if (!page_is_zero(sh->dev[pd_idx].page)) {
1423 compute_block_1(sh,pd_idx,0);
1424 update_p = 1;
1427 if (!q_failed && failed < 2) {
1428 /* q is not failed, and we didn't use it to generate
1429 * anything, so it makes sense to check it
1431 memcpy(page_address(tmp_page),
1432 page_address(sh->dev[qd_idx].page),
1433 STRIPE_SIZE);
1434 compute_parity(sh, UPDATE_PARITY);
1435 if (memcmp(page_address(tmp_page),
1436 page_address(sh->dev[qd_idx].page),
1437 STRIPE_SIZE)!= 0) {
1438 clear_bit(STRIPE_INSYNC, &sh->state);
1439 update_q = 1;
1442 if (update_p || update_q) {
1443 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1444 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1445 /* don't try to repair!! */
1446 update_p = update_q = 0;
1449 /* now write out any block on a failed drive,
1450 * or P or Q if they need it
1453 if (failed == 2) {
1454 dev = &sh->dev[failed_num[1]];
1455 locked++;
1456 set_bit(R5_LOCKED, &dev->flags);
1457 set_bit(R5_Wantwrite, &dev->flags);
1459 if (failed >= 1) {
1460 dev = &sh->dev[failed_num[0]];
1461 locked++;
1462 set_bit(R5_LOCKED, &dev->flags);
1463 set_bit(R5_Wantwrite, &dev->flags);
1466 if (update_p) {
1467 dev = &sh->dev[pd_idx];
1468 locked ++;
1469 set_bit(R5_LOCKED, &dev->flags);
1470 set_bit(R5_Wantwrite, &dev->flags);
1472 if (update_q) {
1473 dev = &sh->dev[qd_idx];
1474 locked++;
1475 set_bit(R5_LOCKED, &dev->flags);
1476 set_bit(R5_Wantwrite, &dev->flags);
1478 clear_bit(STRIPE_DEGRADED, &sh->state);
1480 set_bit(STRIPE_INSYNC, &sh->state);
1484 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1485 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1486 clear_bit(STRIPE_SYNCING, &sh->state);
1489 /* If the failed drives are just a ReadError, then we might need
1490 * to progress the repair/check process
1492 if (failed <= 2 && ! conf->mddev->ro)
1493 for (i=0; i<failed;i++) {
1494 dev = &sh->dev[failed_num[i]];
1495 if (test_bit(R5_ReadError, &dev->flags)
1496 && !test_bit(R5_LOCKED, &dev->flags)
1497 && test_bit(R5_UPTODATE, &dev->flags)
1499 if (!test_bit(R5_ReWrite, &dev->flags)) {
1500 set_bit(R5_Wantwrite, &dev->flags);
1501 set_bit(R5_ReWrite, &dev->flags);
1502 set_bit(R5_LOCKED, &dev->flags);
1503 } else {
1504 /* let's read it back */
1505 set_bit(R5_Wantread, &dev->flags);
1506 set_bit(R5_LOCKED, &dev->flags);
1510 spin_unlock(&sh->lock);
1512 while ((bi=return_bi)) {
1513 int bytes = bi->bi_size;
1515 return_bi = bi->bi_next;
1516 bi->bi_next = NULL;
1517 bi->bi_size = 0;
1518 bi->bi_end_io(bi, bytes, 0);
1520 for (i=disks; i-- ;) {
1521 int rw;
1522 struct bio *bi;
1523 mdk_rdev_t *rdev;
1524 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1525 rw = 1;
1526 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1527 rw = 0;
1528 else
1529 continue;
1531 bi = &sh->dev[i].req;
1533 bi->bi_rw = rw;
1534 if (rw)
1535 bi->bi_end_io = raid6_end_write_request;
1536 else
1537 bi->bi_end_io = raid6_end_read_request;
1539 rcu_read_lock();
1540 rdev = rcu_dereference(conf->disks[i].rdev);
1541 if (rdev && test_bit(Faulty, &rdev->flags))
1542 rdev = NULL;
1543 if (rdev)
1544 atomic_inc(&rdev->nr_pending);
1545 rcu_read_unlock();
1547 if (rdev) {
1548 if (syncing)
1549 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1551 bi->bi_bdev = rdev->bdev;
1552 PRINTK("for %llu schedule op %ld on disc %d\n",
1553 (unsigned long long)sh->sector, bi->bi_rw, i);
1554 atomic_inc(&sh->count);
1555 bi->bi_sector = sh->sector + rdev->data_offset;
1556 bi->bi_flags = 1 << BIO_UPTODATE;
1557 bi->bi_vcnt = 1;
1558 bi->bi_max_vecs = 1;
1559 bi->bi_idx = 0;
1560 bi->bi_io_vec = &sh->dev[i].vec;
1561 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1562 bi->bi_io_vec[0].bv_offset = 0;
1563 bi->bi_size = STRIPE_SIZE;
1564 bi->bi_next = NULL;
1565 if (rw == WRITE &&
1566 test_bit(R5_ReWrite, &sh->dev[i].flags))
1567 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1568 generic_make_request(bi);
1569 } else {
1570 if (rw == 1)
1571 set_bit(STRIPE_DEGRADED, &sh->state);
1572 PRINTK("skip op %ld on disc %d for sector %llu\n",
1573 bi->bi_rw, i, (unsigned long long)sh->sector);
1574 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1575 set_bit(STRIPE_HANDLE, &sh->state);
1580 static void raid6_activate_delayed(raid6_conf_t *conf)
1582 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1583 while (!list_empty(&conf->delayed_list)) {
1584 struct list_head *l = conf->delayed_list.next;
1585 struct stripe_head *sh;
1586 sh = list_entry(l, struct stripe_head, lru);
1587 list_del_init(l);
1588 clear_bit(STRIPE_DELAYED, &sh->state);
1589 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1590 atomic_inc(&conf->preread_active_stripes);
1591 list_add_tail(&sh->lru, &conf->handle_list);
1596 static void activate_bit_delay(raid6_conf_t *conf)
1598 /* device_lock is held */
1599 struct list_head head;
1600 list_add(&head, &conf->bitmap_list);
1601 list_del_init(&conf->bitmap_list);
1602 while (!list_empty(&head)) {
1603 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
1604 list_del_init(&sh->lru);
1605 atomic_inc(&sh->count);
1606 __release_stripe(conf, sh);
1610 static void unplug_slaves(mddev_t *mddev)
1612 raid6_conf_t *conf = mddev_to_conf(mddev);
1613 int i;
1615 rcu_read_lock();
1616 for (i=0; i<mddev->raid_disks; i++) {
1617 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1618 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
1619 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1621 atomic_inc(&rdev->nr_pending);
1622 rcu_read_unlock();
1624 if (r_queue->unplug_fn)
1625 r_queue->unplug_fn(r_queue);
1627 rdev_dec_pending(rdev, mddev);
1628 rcu_read_lock();
1631 rcu_read_unlock();
1634 static void raid6_unplug_device(request_queue_t *q)
1636 mddev_t *mddev = q->queuedata;
1637 raid6_conf_t *conf = mddev_to_conf(mddev);
1638 unsigned long flags;
1640 spin_lock_irqsave(&conf->device_lock, flags);
1642 if (blk_remove_plug(q)) {
1643 conf->seq_flush++;
1644 raid6_activate_delayed(conf);
1646 md_wakeup_thread(mddev->thread);
1648 spin_unlock_irqrestore(&conf->device_lock, flags);
1650 unplug_slaves(mddev);
1653 static int raid6_issue_flush(request_queue_t *q, struct gendisk *disk,
1654 sector_t *error_sector)
1656 mddev_t *mddev = q->queuedata;
1657 raid6_conf_t *conf = mddev_to_conf(mddev);
1658 int i, ret = 0;
1660 rcu_read_lock();
1661 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1662 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
1663 if (rdev && !test_bit(Faulty, &rdev->flags)) {
1664 struct block_device *bdev = rdev->bdev;
1665 request_queue_t *r_queue = bdev_get_queue(bdev);
1667 if (!r_queue->issue_flush_fn)
1668 ret = -EOPNOTSUPP;
1669 else {
1670 atomic_inc(&rdev->nr_pending);
1671 rcu_read_unlock();
1672 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1673 error_sector);
1674 rdev_dec_pending(rdev, mddev);
1675 rcu_read_lock();
1679 rcu_read_unlock();
1680 return ret;
1683 static inline void raid6_plug_device(raid6_conf_t *conf)
1685 spin_lock_irq(&conf->device_lock);
1686 blk_plug_device(conf->mddev->queue);
1687 spin_unlock_irq(&conf->device_lock);
1690 static int make_request (request_queue_t *q, struct bio * bi)
1692 mddev_t *mddev = q->queuedata;
1693 raid6_conf_t *conf = mddev_to_conf(mddev);
1694 const unsigned int raid_disks = conf->raid_disks;
1695 const unsigned int data_disks = raid_disks - 2;
1696 unsigned int dd_idx, pd_idx;
1697 sector_t new_sector;
1698 sector_t logical_sector, last_sector;
1699 struct stripe_head *sh;
1700 const int rw = bio_data_dir(bi);
1702 if (unlikely(bio_barrier(bi))) {
1703 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
1704 return 0;
1707 md_write_start(mddev, bi);
1709 disk_stat_inc(mddev->gendisk, ios[rw]);
1710 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
1712 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1713 last_sector = bi->bi_sector + (bi->bi_size>>9);
1715 bi->bi_next = NULL;
1716 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1718 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1719 DEFINE_WAIT(w);
1721 new_sector = raid6_compute_sector(logical_sector,
1722 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1724 PRINTK("raid6: make_request, sector %llu logical %llu\n",
1725 (unsigned long long)new_sector,
1726 (unsigned long long)logical_sector);
1728 retry:
1729 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1730 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1731 if (sh) {
1732 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1733 /* Add failed due to overlap. Flush everything
1734 * and wait a while
1736 raid6_unplug_device(mddev->queue);
1737 release_stripe(sh);
1738 schedule();
1739 goto retry;
1741 finish_wait(&conf->wait_for_overlap, &w);
1742 raid6_plug_device(conf);
1743 handle_stripe(sh, NULL);
1744 release_stripe(sh);
1745 } else {
1746 /* cannot get stripe for read-ahead, just give-up */
1747 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1748 finish_wait(&conf->wait_for_overlap, &w);
1749 break;
1753 spin_lock_irq(&conf->device_lock);
1754 if (--bi->bi_phys_segments == 0) {
1755 int bytes = bi->bi_size;
1757 if (rw == WRITE )
1758 md_write_end(mddev);
1759 bi->bi_size = 0;
1760 bi->bi_end_io(bi, bytes, 0);
1762 spin_unlock_irq(&conf->device_lock);
1763 return 0;
1766 /* FIXME go_faster isn't used */
1767 static sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
1769 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
1770 struct stripe_head *sh;
1771 int sectors_per_chunk = conf->chunk_size >> 9;
1772 sector_t x;
1773 unsigned long stripe;
1774 int chunk_offset;
1775 int dd_idx, pd_idx;
1776 sector_t first_sector;
1777 int raid_disks = conf->raid_disks;
1778 int data_disks = raid_disks - 2;
1779 sector_t max_sector = mddev->size << 1;
1780 int sync_blocks;
1781 int still_degraded = 0;
1782 int i;
1784 if (sector_nr >= max_sector) {
1785 /* just being told to finish up .. nothing much to do */
1786 unplug_slaves(mddev);
1788 if (mddev->curr_resync < max_sector) /* aborted */
1789 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
1790 &sync_blocks, 1);
1791 else /* completed sync */
1792 conf->fullsync = 0;
1793 bitmap_close_sync(mddev->bitmap);
1795 return 0;
1797 /* if there are 2 or more failed drives and we are trying
1798 * to resync, then assert that we are finished, because there is
1799 * nothing we can do.
1801 if (mddev->degraded >= 2 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1802 sector_t rv = (mddev->size << 1) - sector_nr;
1803 *skipped = 1;
1804 return rv;
1806 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
1807 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
1808 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
1809 /* we can skip this block, and probably more */
1810 sync_blocks /= STRIPE_SECTORS;
1811 *skipped = 1;
1812 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
1815 x = sector_nr;
1816 chunk_offset = sector_div(x, sectors_per_chunk);
1817 stripe = x;
1818 BUG_ON(x != stripe);
1820 first_sector = raid6_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1821 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1822 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1823 if (sh == NULL) {
1824 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1825 /* make sure we don't swamp the stripe cache if someone else
1826 * is trying to get access
1828 schedule_timeout_uninterruptible(1);
1830 /* Need to check if array will still be degraded after recovery/resync
1831 * We don't need to check the 'failed' flag as when that gets set,
1832 * recovery aborts.
1834 for (i=0; i<mddev->raid_disks; i++)
1835 if (conf->disks[i].rdev == NULL)
1836 still_degraded = 1;
1838 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
1840 spin_lock(&sh->lock);
1841 set_bit(STRIPE_SYNCING, &sh->state);
1842 clear_bit(STRIPE_INSYNC, &sh->state);
1843 spin_unlock(&sh->lock);
1845 handle_stripe(sh, NULL);
1846 release_stripe(sh);
1848 return STRIPE_SECTORS;
1852 * This is our raid6 kernel thread.
1854 * We scan the hash table for stripes which can be handled now.
1855 * During the scan, completed stripes are saved for us by the interrupt
1856 * handler, so that they will not have to wait for our next wakeup.
1858 static void raid6d (mddev_t *mddev)
1860 struct stripe_head *sh;
1861 raid6_conf_t *conf = mddev_to_conf(mddev);
1862 int handled;
1864 PRINTK("+++ raid6d active\n");
1866 md_check_recovery(mddev);
1868 handled = 0;
1869 spin_lock_irq(&conf->device_lock);
1870 while (1) {
1871 struct list_head *first;
1873 if (conf->seq_flush - conf->seq_write > 0) {
1874 int seq = conf->seq_flush;
1875 spin_unlock_irq(&conf->device_lock);
1876 bitmap_unplug(mddev->bitmap);
1877 spin_lock_irq(&conf->device_lock);
1878 conf->seq_write = seq;
1879 activate_bit_delay(conf);
1882 if (list_empty(&conf->handle_list) &&
1883 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1884 !blk_queue_plugged(mddev->queue) &&
1885 !list_empty(&conf->delayed_list))
1886 raid6_activate_delayed(conf);
1888 if (list_empty(&conf->handle_list))
1889 break;
1891 first = conf->handle_list.next;
1892 sh = list_entry(first, struct stripe_head, lru);
1894 list_del_init(first);
1895 atomic_inc(&sh->count);
1896 if (atomic_read(&sh->count)!= 1)
1897 BUG();
1898 spin_unlock_irq(&conf->device_lock);
1900 handled++;
1901 handle_stripe(sh, conf->spare_page);
1902 release_stripe(sh);
1904 spin_lock_irq(&conf->device_lock);
1906 PRINTK("%d stripes handled\n", handled);
1908 spin_unlock_irq(&conf->device_lock);
1910 unplug_slaves(mddev);
1912 PRINTK("--- raid6d inactive\n");
1915 static int run(mddev_t *mddev)
1917 raid6_conf_t *conf;
1918 int raid_disk, memory;
1919 mdk_rdev_t *rdev;
1920 struct disk_info *disk;
1921 struct list_head *tmp;
1923 if (mddev->level != 6) {
1924 PRINTK("raid6: %s: raid level not set to 6 (%d)\n", mdname(mddev), mddev->level);
1925 return -EIO;
1928 mddev->private = kzalloc(sizeof (raid6_conf_t)
1929 + mddev->raid_disks * sizeof(struct disk_info),
1930 GFP_KERNEL);
1931 if ((conf = mddev->private) == NULL)
1932 goto abort;
1933 conf->mddev = mddev;
1935 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
1936 goto abort;
1938 conf->spare_page = alloc_page(GFP_KERNEL);
1939 if (!conf->spare_page)
1940 goto abort;
1942 spin_lock_init(&conf->device_lock);
1943 init_waitqueue_head(&conf->wait_for_stripe);
1944 init_waitqueue_head(&conf->wait_for_overlap);
1945 INIT_LIST_HEAD(&conf->handle_list);
1946 INIT_LIST_HEAD(&conf->delayed_list);
1947 INIT_LIST_HEAD(&conf->bitmap_list);
1948 INIT_LIST_HEAD(&conf->inactive_list);
1949 atomic_set(&conf->active_stripes, 0);
1950 atomic_set(&conf->preread_active_stripes, 0);
1952 PRINTK("raid6: run(%s) called.\n", mdname(mddev));
1954 ITERATE_RDEV(mddev,rdev,tmp) {
1955 raid_disk = rdev->raid_disk;
1956 if (raid_disk >= mddev->raid_disks
1957 || raid_disk < 0)
1958 continue;
1959 disk = conf->disks + raid_disk;
1961 disk->rdev = rdev;
1963 if (test_bit(In_sync, &rdev->flags)) {
1964 char b[BDEVNAME_SIZE];
1965 printk(KERN_INFO "raid6: device %s operational as raid"
1966 " disk %d\n", bdevname(rdev->bdev,b),
1967 raid_disk);
1968 conf->working_disks++;
1972 conf->raid_disks = mddev->raid_disks;
1975 * 0 for a fully functional array, 1 or 2 for a degraded array.
1977 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1978 conf->mddev = mddev;
1979 conf->chunk_size = mddev->chunk_size;
1980 conf->level = mddev->level;
1981 conf->algorithm = mddev->layout;
1982 conf->max_nr_stripes = NR_STRIPES;
1984 /* device size must be a multiple of chunk size */
1985 mddev->size &= ~(mddev->chunk_size/1024 -1);
1986 mddev->resync_max_sectors = mddev->size << 1;
1988 if (conf->raid_disks < 4) {
1989 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
1990 mdname(mddev), conf->raid_disks);
1991 goto abort;
1993 if (!conf->chunk_size || conf->chunk_size % 4) {
1994 printk(KERN_ERR "raid6: invalid chunk size %d for %s\n",
1995 conf->chunk_size, mdname(mddev));
1996 goto abort;
1998 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1999 printk(KERN_ERR
2000 "raid6: unsupported parity algorithm %d for %s\n",
2001 conf->algorithm, mdname(mddev));
2002 goto abort;
2004 if (mddev->degraded > 2) {
2005 printk(KERN_ERR "raid6: not enough operational devices for %s"
2006 " (%d/%d failed)\n",
2007 mdname(mddev), conf->failed_disks, conf->raid_disks);
2008 goto abort;
2011 if (mddev->degraded > 0 &&
2012 mddev->recovery_cp != MaxSector) {
2013 if (mddev->ok_start_degraded)
2014 printk(KERN_WARNING "raid6: starting dirty degraded array:%s"
2015 "- data corruption possible.\n",
2016 mdname(mddev));
2017 else {
2018 printk(KERN_ERR "raid6: cannot start dirty degraded array"
2019 " for %s\n", mdname(mddev));
2020 goto abort;
2025 mddev->thread = md_register_thread(raid6d, mddev, "%s_raid6");
2026 if (!mddev->thread) {
2027 printk(KERN_ERR
2028 "raid6: couldn't allocate thread for %s\n",
2029 mdname(mddev));
2030 goto abort;
2034 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
2035 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
2036 if (grow_stripes(conf, conf->max_nr_stripes)) {
2037 printk(KERN_ERR
2038 "raid6: couldn't allocate %dkB for buffers\n", memory);
2039 shrink_stripes(conf);
2040 md_unregister_thread(mddev->thread);
2041 goto abort;
2042 } else
2043 printk(KERN_INFO "raid6: allocated %dkB for %s\n",
2044 memory, mdname(mddev));
2046 if (mddev->degraded == 0)
2047 printk(KERN_INFO "raid6: raid level %d set %s active with %d out of %d"
2048 " devices, algorithm %d\n", conf->level, mdname(mddev),
2049 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
2050 conf->algorithm);
2051 else
2052 printk(KERN_ALERT "raid6: raid level %d set %s active with %d"
2053 " out of %d devices, algorithm %d\n", conf->level,
2054 mdname(mddev), mddev->raid_disks - mddev->degraded,
2055 mddev->raid_disks, conf->algorithm);
2057 print_raid6_conf(conf);
2059 /* read-ahead size must cover two whole stripes, which is
2060 * 2 * (n-2) * chunksize where 'n' is the number of raid devices
2063 int stripe = (mddev->raid_disks-2) * mddev->chunk_size
2064 / PAGE_SIZE;
2065 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
2066 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
2069 /* Ok, everything is just fine now */
2070 mddev->array_size = mddev->size * (mddev->raid_disks - 2);
2072 mddev->queue->unplug_fn = raid6_unplug_device;
2073 mddev->queue->issue_flush_fn = raid6_issue_flush;
2074 return 0;
2075 abort:
2076 if (conf) {
2077 print_raid6_conf(conf);
2078 safe_put_page(conf->spare_page);
2079 kfree(conf->stripe_hashtbl);
2080 kfree(conf);
2082 mddev->private = NULL;
2083 printk(KERN_ALERT "raid6: failed to run raid set %s\n", mdname(mddev));
2084 return -EIO;
2089 static int stop (mddev_t *mddev)
2091 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2093 md_unregister_thread(mddev->thread);
2094 mddev->thread = NULL;
2095 shrink_stripes(conf);
2096 kfree(conf->stripe_hashtbl);
2097 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
2098 kfree(conf);
2099 mddev->private = NULL;
2100 return 0;
2103 #if RAID6_DUMPSTATE
2104 static void print_sh (struct seq_file *seq, struct stripe_head *sh)
2106 int i;
2108 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
2109 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
2110 seq_printf(seq, "sh %llu, count %d.\n",
2111 (unsigned long long)sh->sector, atomic_read(&sh->count));
2112 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
2113 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
2114 seq_printf(seq, "(cache%d: %p %ld) ",
2115 i, sh->dev[i].page, sh->dev[i].flags);
2117 seq_printf(seq, "\n");
2120 static void printall (struct seq_file *seq, raid6_conf_t *conf)
2122 struct stripe_head *sh;
2123 struct hlist_node *hn;
2124 int i;
2126 spin_lock_irq(&conf->device_lock);
2127 for (i = 0; i < NR_HASH; i++) {
2128 sh = conf->stripe_hashtbl[i];
2129 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
2130 if (sh->raid_conf != conf)
2131 continue;
2132 print_sh(seq, sh);
2135 spin_unlock_irq(&conf->device_lock);
2137 #endif
2139 static void status (struct seq_file *seq, mddev_t *mddev)
2141 raid6_conf_t *conf = (raid6_conf_t *) mddev->private;
2142 int i;
2144 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
2145 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
2146 for (i = 0; i < conf->raid_disks; i++)
2147 seq_printf (seq, "%s",
2148 conf->disks[i].rdev &&
2149 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
2150 seq_printf (seq, "]");
2151 #if RAID6_DUMPSTATE
2152 seq_printf (seq, "\n");
2153 printall(seq, conf);
2154 #endif
2157 static void print_raid6_conf (raid6_conf_t *conf)
2159 int i;
2160 struct disk_info *tmp;
2162 printk("RAID6 conf printout:\n");
2163 if (!conf) {
2164 printk("(conf==NULL)\n");
2165 return;
2167 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
2168 conf->working_disks, conf->failed_disks);
2170 for (i = 0; i < conf->raid_disks; i++) {
2171 char b[BDEVNAME_SIZE];
2172 tmp = conf->disks + i;
2173 if (tmp->rdev)
2174 printk(" disk %d, o:%d, dev:%s\n",
2175 i, !test_bit(Faulty, &tmp->rdev->flags),
2176 bdevname(tmp->rdev->bdev,b));
2180 static int raid6_spare_active(mddev_t *mddev)
2182 int i;
2183 raid6_conf_t *conf = mddev->private;
2184 struct disk_info *tmp;
2186 for (i = 0; i < conf->raid_disks; i++) {
2187 tmp = conf->disks + i;
2188 if (tmp->rdev
2189 && !test_bit(Faulty, &tmp->rdev->flags)
2190 && !test_bit(In_sync, &tmp->rdev->flags)) {
2191 mddev->degraded--;
2192 conf->failed_disks--;
2193 conf->working_disks++;
2194 set_bit(In_sync, &tmp->rdev->flags);
2197 print_raid6_conf(conf);
2198 return 0;
2201 static int raid6_remove_disk(mddev_t *mddev, int number)
2203 raid6_conf_t *conf = mddev->private;
2204 int err = 0;
2205 mdk_rdev_t *rdev;
2206 struct disk_info *p = conf->disks + number;
2208 print_raid6_conf(conf);
2209 rdev = p->rdev;
2210 if (rdev) {
2211 if (test_bit(In_sync, &rdev->flags) ||
2212 atomic_read(&rdev->nr_pending)) {
2213 err = -EBUSY;
2214 goto abort;
2216 p->rdev = NULL;
2217 synchronize_rcu();
2218 if (atomic_read(&rdev->nr_pending)) {
2219 /* lost the race, try later */
2220 err = -EBUSY;
2221 p->rdev = rdev;
2225 abort:
2227 print_raid6_conf(conf);
2228 return err;
2231 static int raid6_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
2233 raid6_conf_t *conf = mddev->private;
2234 int found = 0;
2235 int disk;
2236 struct disk_info *p;
2238 if (mddev->degraded > 2)
2239 /* no point adding a device */
2240 return 0;
2242 * find the disk ... but prefer rdev->saved_raid_disk
2243 * if possible.
2245 if (rdev->saved_raid_disk >= 0 &&
2246 conf->disks[rdev->saved_raid_disk].rdev == NULL)
2247 disk = rdev->saved_raid_disk;
2248 else
2249 disk = 0;
2250 for ( ; disk < mddev->raid_disks; disk++)
2251 if ((p=conf->disks + disk)->rdev == NULL) {
2252 clear_bit(In_sync, &rdev->flags);
2253 rdev->raid_disk = disk;
2254 found = 1;
2255 if (rdev->saved_raid_disk != disk)
2256 conf->fullsync = 1;
2257 rcu_assign_pointer(p->rdev, rdev);
2258 break;
2260 print_raid6_conf(conf);
2261 return found;
2264 static int raid6_resize(mddev_t *mddev, sector_t sectors)
2266 /* no resync is happening, and there is enough space
2267 * on all devices, so we can resize.
2268 * We need to make sure resync covers any new space.
2269 * If the array is shrinking we should possibly wait until
2270 * any io in the removed space completes, but it hardly seems
2271 * worth it.
2273 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
2274 mddev->array_size = (sectors * (mddev->raid_disks-2))>>1;
2275 set_capacity(mddev->gendisk, mddev->array_size << 1);
2276 mddev->changed = 1;
2277 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
2278 mddev->recovery_cp = mddev->size << 1;
2279 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
2281 mddev->size = sectors /2;
2282 mddev->resync_max_sectors = sectors;
2283 return 0;
2286 static void raid6_quiesce(mddev_t *mddev, int state)
2288 raid6_conf_t *conf = mddev_to_conf(mddev);
2290 switch(state) {
2291 case 1: /* stop all writes */
2292 spin_lock_irq(&conf->device_lock);
2293 conf->quiesce = 1;
2294 wait_event_lock_irq(conf->wait_for_stripe,
2295 atomic_read(&conf->active_stripes) == 0,
2296 conf->device_lock, /* nothing */);
2297 spin_unlock_irq(&conf->device_lock);
2298 break;
2300 case 0: /* re-enable writes */
2301 spin_lock_irq(&conf->device_lock);
2302 conf->quiesce = 0;
2303 wake_up(&conf->wait_for_stripe);
2304 spin_unlock_irq(&conf->device_lock);
2305 break;
2309 static struct mdk_personality raid6_personality =
2311 .name = "raid6",
2312 .level = 6,
2313 .owner = THIS_MODULE,
2314 .make_request = make_request,
2315 .run = run,
2316 .stop = stop,
2317 .status = status,
2318 .error_handler = error,
2319 .hot_add_disk = raid6_add_disk,
2320 .hot_remove_disk= raid6_remove_disk,
2321 .spare_active = raid6_spare_active,
2322 .sync_request = sync_request,
2323 .resize = raid6_resize,
2324 .quiesce = raid6_quiesce,
2327 static int __init raid6_init(void)
2329 int e;
2331 e = raid6_select_algo();
2332 if ( e )
2333 return e;
2335 return register_md_personality(&raid6_personality);
2338 static void raid6_exit (void)
2340 unregister_md_personality(&raid6_personality);
2343 module_init(raid6_init);
2344 module_exit(raid6_exit);
2345 MODULE_LICENSE("GPL");
2346 MODULE_ALIAS("md-personality-8"); /* RAID6 */
2347 MODULE_ALIAS("md-raid6");
2348 MODULE_ALIAS("md-level-6");