Import 2.3.1pre2
[davej-history.git] / drivers / block / raid5.c
blob3c5701b3eae719f7156a4ec2cbb652d0d6f9935e
1 /*****************************************************************************
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
5 * RAID-5 management functions.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
12 * You should have received a copy of the GNU General Public License
13 * (for example /usr/src/linux/COPYING); if not, write to the Free
14 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 #include <linux/module.h>
18 #include <linux/locks.h>
19 #include <linux/malloc.h>
20 #include <linux/md.h>
21 #include <linux/raid5.h>
22 #include <asm/bitops.h>
23 #include <asm/atomic.h>
24 #include <asm/md.h>
26 static struct md_personality raid5_personality;
29 * Stripe cache
31 #define NR_STRIPES 128
32 #define HASH_PAGES 1
33 #define HASH_PAGES_ORDER 0
34 #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
35 #define HASH_MASK (NR_HASH - 1)
36 #define stripe_hash(raid_conf, sect, size) ((raid_conf)->stripe_hashtbl[((sect) / (size >> 9)) & HASH_MASK])
39 * The following can be used to debug the driver
41 #define RAID5_DEBUG 0
43 #if RAID5_DEBUG
44 #define PRINTK(x) do { printk x; } while (0);
45 #else
46 #define PRINTK(x) do { ; } while (0)
47 #endif
49 static inline int stripe_locked(struct stripe_head *sh)
51 return test_bit(STRIPE_LOCKED, &sh->state);
54 static inline int stripe_error(struct stripe_head *sh)
56 return test_bit(STRIPE_ERROR, &sh->state);
60 * Stripes are locked whenever new buffers can't be added to them.
62 static inline void lock_stripe(struct stripe_head *sh)
64 struct raid5_data *raid_conf = sh->raid_conf;
65 if (!test_and_set_bit(STRIPE_LOCKED, &sh->state)) {
66 PRINTK(("locking stripe %lu\n", sh->sector));
67 raid_conf->nr_locked_stripes++;
71 static inline void unlock_stripe(struct stripe_head *sh)
73 struct raid5_data *raid_conf = sh->raid_conf;
74 if (test_and_clear_bit(STRIPE_LOCKED, &sh->state)) {
75 PRINTK(("unlocking stripe %lu\n", sh->sector));
76 raid_conf->nr_locked_stripes--;
77 wake_up(&sh->wait);
81 static inline void finish_stripe(struct stripe_head *sh)
83 struct raid5_data *raid_conf = sh->raid_conf;
84 unlock_stripe(sh);
85 sh->cmd = STRIPE_NONE;
86 sh->phase = PHASE_COMPLETE;
87 raid_conf->nr_pending_stripes--;
88 raid_conf->nr_cached_stripes++;
89 wake_up(&raid_conf->wait_for_stripe);
92 void __wait_on_stripe(struct stripe_head *sh)
94 DECLARE_WAITQUEUE(wait, current);
96 PRINTK(("wait_on_stripe %lu\n", sh->sector));
97 sh->count++;
98 add_wait_queue(&sh->wait, &wait);
99 repeat:
100 current->state = TASK_UNINTERRUPTIBLE;
101 if (stripe_locked(sh)) {
102 schedule();
103 goto repeat;
105 PRINTK(("wait_on_stripe %lu done\n", sh->sector));
106 remove_wait_queue(&sh->wait, &wait);
107 sh->count--;
108 current->state = TASK_RUNNING;
111 static inline void wait_on_stripe(struct stripe_head *sh)
113 if (stripe_locked(sh))
114 __wait_on_stripe(sh);
117 static inline void remove_hash(struct raid5_data *raid_conf, struct stripe_head *sh)
119 PRINTK(("remove_hash(), stripe %lu\n", sh->sector));
121 if (sh->hash_pprev) {
122 if (sh->hash_next)
123 sh->hash_next->hash_pprev = sh->hash_pprev;
124 *sh->hash_pprev = sh->hash_next;
125 sh->hash_pprev = NULL;
126 raid_conf->nr_hashed_stripes--;
130 static inline void insert_hash(struct raid5_data *raid_conf, struct stripe_head *sh)
132 struct stripe_head **shp = &stripe_hash(raid_conf, sh->sector, sh->size);
134 PRINTK(("insert_hash(), stripe %lu, nr_hashed_stripes %d\n", sh->sector, raid_conf->nr_hashed_stripes));
136 if ((sh->hash_next = *shp) != NULL)
137 (*shp)->hash_pprev = &sh->hash_next;
138 *shp = sh;
139 sh->hash_pprev = shp;
140 raid_conf->nr_hashed_stripes++;
143 static struct buffer_head *get_free_buffer(struct stripe_head *sh, int b_size)
145 struct buffer_head *bh;
146 unsigned long flags;
148 save_flags(flags);
149 cli();
150 if ((bh = sh->buffer_pool) == NULL)
151 return NULL;
152 sh->buffer_pool = bh->b_next;
153 bh->b_size = b_size;
154 restore_flags(flags);
155 return bh;
158 static struct buffer_head *get_free_bh(struct stripe_head *sh)
160 struct buffer_head *bh;
161 unsigned long flags;
163 save_flags(flags);
164 cli();
165 if ((bh = sh->bh_pool) == NULL)
166 return NULL;
167 sh->bh_pool = bh->b_next;
168 restore_flags(flags);
169 return bh;
172 static void put_free_buffer(struct stripe_head *sh, struct buffer_head *bh)
174 unsigned long flags;
176 save_flags(flags);
177 cli();
178 bh->b_next = sh->buffer_pool;
179 sh->buffer_pool = bh;
180 restore_flags(flags);
183 static void put_free_bh(struct stripe_head *sh, struct buffer_head *bh)
185 unsigned long flags;
187 save_flags(flags);
188 cli();
189 bh->b_next = sh->bh_pool;
190 sh->bh_pool = bh;
191 restore_flags(flags);
194 static struct stripe_head *get_free_stripe(struct raid5_data *raid_conf)
196 struct stripe_head *sh;
197 unsigned long flags;
199 save_flags(flags);
200 cli();
201 if ((sh = raid_conf->free_sh_list) == NULL) {
202 restore_flags(flags);
203 return NULL;
205 raid_conf->free_sh_list = sh->free_next;
206 raid_conf->nr_free_sh--;
207 if (!raid_conf->nr_free_sh && raid_conf->free_sh_list)
208 printk ("raid5: bug: free_sh_list != NULL, nr_free_sh == 0\n");
209 restore_flags(flags);
210 if (sh->hash_pprev || sh->nr_pending || sh->count)
211 printk("get_free_stripe(): bug\n");
212 return sh;
215 static void put_free_stripe(struct raid5_data *raid_conf, struct stripe_head *sh)
217 unsigned long flags;
219 save_flags(flags);
220 cli();
221 sh->free_next = raid_conf->free_sh_list;
222 raid_conf->free_sh_list = sh;
223 raid_conf->nr_free_sh++;
224 restore_flags(flags);
227 static void shrink_buffers(struct stripe_head *sh, int num)
229 struct buffer_head *bh;
231 while (num--) {
232 if ((bh = get_free_buffer(sh, -1)) == NULL)
233 return;
234 free_page((unsigned long) bh->b_data);
235 kfree(bh);
239 static void shrink_bh(struct stripe_head *sh, int num)
241 struct buffer_head *bh;
243 while (num--) {
244 if ((bh = get_free_bh(sh)) == NULL)
245 return;
246 kfree(bh);
250 static int grow_buffers(struct stripe_head *sh, int num, int b_size, int priority)
252 struct buffer_head *bh;
254 while (num--) {
255 if ((bh = kmalloc(sizeof(struct buffer_head), priority)) == NULL)
256 return 1;
257 memset(bh, 0, sizeof (struct buffer_head));
258 bh->b_data = (char *) __get_free_page(priority);
259 if (!bh->b_data) {
260 kfree(bh);
261 return 1;
263 bh->b_size = b_size;
264 put_free_buffer(sh, bh);
266 return 0;
269 static int grow_bh(struct stripe_head *sh, int num, int priority)
271 struct buffer_head *bh;
273 while (num--) {
274 if ((bh = kmalloc(sizeof(struct buffer_head), priority)) == NULL)
275 return 1;
276 memset(bh, 0, sizeof (struct buffer_head));
277 put_free_bh(sh, bh);
279 return 0;
282 static void raid5_kfree_buffer(struct stripe_head *sh, struct buffer_head *bh)
284 unsigned long flags;
286 save_flags(flags);
287 cli();
288 put_free_buffer(sh, bh);
289 restore_flags(flags);
292 static void raid5_kfree_bh(struct stripe_head *sh, struct buffer_head *bh)
294 unsigned long flags;
296 save_flags(flags);
297 cli();
298 put_free_bh(sh, bh);
299 restore_flags(flags);
302 static void raid5_kfree_old_bh(struct stripe_head *sh, int i)
304 if (!sh->bh_old[i]) {
305 printk("raid5_kfree_old_bh: bug: sector %lu, index %d not present\n", sh->sector, i);
306 return;
308 raid5_kfree_buffer(sh, sh->bh_old[i]);
309 sh->bh_old[i] = NULL;
312 static void raid5_update_old_bh(struct stripe_head *sh, int i)
314 PRINTK(("stripe %lu, idx %d, updating cache copy\n", sh->sector, i));
315 if (!sh->bh_copy[i]) {
316 printk("raid5_update_old_bh: bug: sector %lu, index %d not present\n", sh->sector, i);
317 return;
319 if (sh->bh_old[i])
320 raid5_kfree_old_bh(sh, i);
321 sh->bh_old[i] = sh->bh_copy[i];
322 sh->bh_copy[i] = NULL;
325 static void kfree_stripe(struct stripe_head *sh)
327 struct raid5_data *raid_conf = sh->raid_conf;
328 int disks = raid_conf->raid_disks, j;
330 PRINTK(("kfree_stripe called, stripe %lu\n", sh->sector));
331 if (sh->phase != PHASE_COMPLETE || stripe_locked(sh) || sh->count) {
332 printk("raid5: kfree_stripe(), sector %lu, phase %d, locked %d, count %d\n", sh->sector, sh->phase, stripe_locked(sh), sh->count);
333 return;
335 for (j = 0; j < disks; j++) {
336 if (sh->bh_old[j])
337 raid5_kfree_old_bh(sh, j);
338 if (sh->bh_new[j] || sh->bh_copy[j])
339 printk("raid5: bug: sector %lu, new %p, copy %p\n", sh->sector, sh->bh_new[j], sh->bh_copy[j]);
341 remove_hash(raid_conf, sh);
342 put_free_stripe(raid_conf, sh);
345 static int shrink_stripe_cache(struct raid5_data *raid_conf, int nr)
347 struct stripe_head *sh;
348 int i, count = 0;
350 PRINTK(("shrink_stripe_cache called, %d/%d, clock %d\n", nr, raid_conf->nr_hashed_stripes, raid_conf->clock));
351 for (i = 0; i < NR_HASH; i++) {
352 repeat:
353 sh = raid_conf->stripe_hashtbl[(i + raid_conf->clock) & HASH_MASK];
354 for (; sh; sh = sh->hash_next) {
355 if (sh->phase != PHASE_COMPLETE)
356 continue;
357 if (stripe_locked(sh))
358 continue;
359 if (sh->count)
360 continue;
361 kfree_stripe(sh);
362 if (++count == nr) {
363 PRINTK(("shrink completed, nr_hashed_stripes %d\n", raid_conf->nr_hashed_stripes));
364 raid_conf->clock = (i + raid_conf->clock) & HASH_MASK;
365 return nr;
367 goto repeat;
370 PRINTK(("shrink completed, nr_hashed_stripes %d\n", raid_conf->nr_hashed_stripes));
371 return count;
374 static struct stripe_head *find_stripe(struct raid5_data *raid_conf, unsigned long sector, int size)
376 struct stripe_head *sh;
378 if (raid_conf->buffer_size != size) {
379 PRINTK(("switching size, %d --> %d\n", raid_conf->buffer_size, size));
380 shrink_stripe_cache(raid_conf, raid_conf->max_nr_stripes);
381 raid_conf->buffer_size = size;
384 PRINTK(("find_stripe, sector %lu\n", sector));
385 for (sh = stripe_hash(raid_conf, sector, size); sh; sh = sh->hash_next)
386 if (sh->sector == sector && sh->raid_conf == raid_conf) {
387 if (sh->size == size) {
388 PRINTK(("found stripe %lu\n", sector));
389 return sh;
390 } else {
391 PRINTK(("switching size for %lu, %d --> %d\n", sector, sh->size, size));
392 kfree_stripe(sh);
393 break;
396 PRINTK(("stripe %lu not in cache\n", sector));
397 return NULL;
400 static int grow_stripes(struct raid5_data *raid_conf, int num, int priority)
402 struct stripe_head *sh;
404 while (num--) {
405 if ((sh = kmalloc(sizeof(struct stripe_head), priority)) == NULL)
406 return 1;
407 memset(sh, 0, sizeof(*sh));
408 if (grow_buffers(sh, 2 * raid_conf->raid_disks, PAGE_SIZE, priority)) {
409 shrink_buffers(sh, 2 * raid_conf->raid_disks);
410 kfree(sh);
411 return 1;
413 if (grow_bh(sh, raid_conf->raid_disks, priority)) {
414 shrink_buffers(sh, 2 * raid_conf->raid_disks);
415 shrink_bh(sh, raid_conf->raid_disks);
416 kfree(sh);
417 return 1;
419 put_free_stripe(raid_conf, sh);
420 raid_conf->nr_stripes++;
422 return 0;
425 static void shrink_stripes(struct raid5_data *raid_conf, int num)
427 struct stripe_head *sh;
429 while (num--) {
430 sh = get_free_stripe(raid_conf);
431 if (!sh)
432 break;
433 shrink_buffers(sh, raid_conf->raid_disks * 2);
434 shrink_bh(sh, raid_conf->raid_disks);
435 kfree(sh);
436 raid_conf->nr_stripes--;
440 static struct stripe_head *kmalloc_stripe(struct raid5_data *raid_conf, unsigned long sector, int size)
442 struct stripe_head *sh = NULL, *tmp;
443 struct buffer_head *buffer_pool, *bh_pool;
445 PRINTK(("kmalloc_stripe called\n"));
447 while ((sh = get_free_stripe(raid_conf)) == NULL) {
448 shrink_stripe_cache(raid_conf, raid_conf->max_nr_stripes / 8);
449 if ((sh = get_free_stripe(raid_conf)) != NULL)
450 break;
451 if (!raid_conf->nr_pending_stripes)
452 printk("raid5: bug: nr_free_sh == 0, nr_pending_stripes == 0\n");
453 md_wakeup_thread(raid_conf->thread);
454 PRINTK(("waiting for some stripes to complete\n"));
455 sleep_on(&raid_conf->wait_for_stripe);
459 * The above might have slept, so perhaps another process
460 * already created the stripe for us..
462 if ((tmp = find_stripe(raid_conf, sector, size)) != NULL) {
463 put_free_stripe(raid_conf, sh);
464 wait_on_stripe(tmp);
465 return tmp;
467 if (sh) {
468 buffer_pool = sh->buffer_pool;
469 bh_pool = sh->bh_pool;
470 memset(sh, 0, sizeof(*sh));
471 sh->buffer_pool = buffer_pool;
472 sh->bh_pool = bh_pool;
473 sh->phase = PHASE_COMPLETE;
474 sh->cmd = STRIPE_NONE;
475 sh->raid_conf = raid_conf;
476 sh->sector = sector;
477 sh->size = size;
478 raid_conf->nr_cached_stripes++;
479 insert_hash(raid_conf, sh);
480 } else printk("raid5: bug: kmalloc_stripe() == NULL\n");
481 return sh;
484 static struct stripe_head *get_stripe(struct raid5_data *raid_conf, unsigned long sector, int size)
486 struct stripe_head *sh;
488 PRINTK(("get_stripe, sector %lu\n", sector));
489 sh = find_stripe(raid_conf, sector, size);
490 if (sh)
491 wait_on_stripe(sh);
492 else
493 sh = kmalloc_stripe(raid_conf, sector, size);
494 return sh;
498 static struct buffer_head *raid5_kmalloc_buffer(struct stripe_head *sh, int b_size)
500 struct buffer_head *bh;
502 if ((bh = get_free_buffer(sh, b_size)) == NULL)
503 printk("raid5: bug: raid5_kmalloc_buffer() == NULL\n");
504 return bh;
507 static struct buffer_head *raid5_kmalloc_bh(struct stripe_head *sh)
509 struct buffer_head *bh;
511 if ((bh = get_free_bh(sh)) == NULL)
512 printk("raid5: bug: raid5_kmalloc_bh() == NULL\n");
513 return bh;
516 static inline void raid5_end_buffer_io (struct stripe_head *sh, int i, int uptodate)
518 struct buffer_head *bh = sh->bh_new[i];
520 sh->bh_new[i] = NULL;
521 raid5_kfree_bh(sh, sh->bh_req[i]);
522 sh->bh_req[i] = NULL;
523 bh->b_end_io(bh, uptodate);
524 if (!uptodate)
525 printk(KERN_ALERT "raid5: %s: unrecoverable I/O error for "
526 "block %lu\n", kdevname(bh->b_dev), bh->b_blocknr);
529 static inline void raid5_mark_buffer_uptodate (struct buffer_head *bh, int uptodate)
531 if (uptodate)
532 set_bit(BH_Uptodate, &bh->b_state);
533 else
534 clear_bit(BH_Uptodate, &bh->b_state);
537 static void raid5_end_request (struct buffer_head * bh, int uptodate)
539 struct stripe_head *sh = bh->b_dev_id;
540 struct raid5_data *raid_conf = sh->raid_conf;
541 int disks = raid_conf->raid_disks, i;
542 unsigned long flags;
544 PRINTK(("end_request %lu, nr_pending %d\n", sh->sector, sh->nr_pending));
545 save_flags(flags);
546 cli();
547 raid5_mark_buffer_uptodate(bh, uptodate);
548 --sh->nr_pending;
549 if (!sh->nr_pending) {
550 md_wakeup_thread(raid_conf->thread);
551 atomic_inc(&raid_conf->nr_handle);
553 if (!uptodate)
554 md_error(bh->b_dev, bh->b_rdev);
555 if (raid_conf->failed_disks) {
556 for (i = 0; i < disks; i++) {
557 if (raid_conf->disks[i].operational)
558 continue;
559 if (bh != sh->bh_old[i] && bh != sh->bh_req[i] && bh != sh->bh_copy[i])
560 continue;
561 if (bh->b_rdev != raid_conf->disks[i].dev)
562 continue;
563 set_bit(STRIPE_ERROR, &sh->state);
566 restore_flags(flags);
569 static int raid5_map (struct md_dev *mddev, kdev_t *rdev,
570 unsigned long *rsector, unsigned long size)
572 /* No complex mapping used: the core of the work is done in the
573 * request routine
575 return 0;
578 static void raid5_build_block (struct stripe_head *sh, struct buffer_head *bh, int i)
580 struct raid5_data *raid_conf = sh->raid_conf;
581 struct md_dev *mddev = raid_conf->mddev;
582 int minor = (int) (mddev - md_dev);
583 char *b_data;
584 kdev_t dev = MKDEV(MD_MAJOR, minor);
585 int block = sh->sector / (sh->size >> 9);
587 b_data = ((volatile struct buffer_head *) bh)->b_data;
588 memset (bh, 0, sizeof (struct buffer_head));
589 init_buffer(bh, dev, block, raid5_end_request, sh);
590 ((volatile struct buffer_head *) bh)->b_data = b_data;
592 bh->b_rdev = raid_conf->disks[i].dev;
593 bh->b_rsector = sh->sector;
595 bh->b_state = (1 << BH_Req);
596 bh->b_size = sh->size;
597 bh->b_list = BUF_LOCKED;
600 static int raid5_error (struct md_dev *mddev, kdev_t dev)
602 struct raid5_data *raid_conf = (struct raid5_data *) mddev->private;
603 md_superblock_t *sb = mddev->sb;
604 struct disk_info *disk;
605 int i;
607 PRINTK(("raid5_error called\n"));
608 raid_conf->resync_parity = 0;
609 for (i = 0, disk = raid_conf->disks; i < raid_conf->raid_disks; i++, disk++)
610 if (disk->dev == dev && disk->operational) {
611 disk->operational = 0;
612 sb->disks[disk->number].state |= (1 << MD_FAULTY_DEVICE);
613 sb->disks[disk->number].state &= ~(1 << MD_SYNC_DEVICE);
614 sb->disks[disk->number].state &= ~(1 << MD_ACTIVE_DEVICE);
615 sb->active_disks--;
616 sb->working_disks--;
617 sb->failed_disks++;
618 mddev->sb_dirty = 1;
619 raid_conf->working_disks--;
620 raid_conf->failed_disks++;
621 md_wakeup_thread(raid_conf->thread);
622 printk (KERN_ALERT
623 "RAID5: Disk failure on %s, disabling device."
624 "Operation continuing on %d devices\n",
625 kdevname (dev), raid_conf->working_disks);
627 return 0;
631 * Input: a 'big' sector number,
632 * Output: index of the data and parity disk, and the sector # in them.
634 static inline unsigned long
635 raid5_compute_sector (int r_sector, unsigned int raid_disks, unsigned int data_disks,
636 unsigned int * dd_idx, unsigned int * pd_idx,
637 struct raid5_data *raid_conf)
639 unsigned int stripe;
640 int chunk_number, chunk_offset;
641 unsigned long new_sector;
642 int sectors_per_chunk = raid_conf->chunk_size >> 9;
644 /* First compute the information on this sector */
647 * Compute the chunk number and the sector offset inside the chunk
649 chunk_number = r_sector / sectors_per_chunk;
650 chunk_offset = r_sector % sectors_per_chunk;
653 * Compute the stripe number
655 stripe = chunk_number / data_disks;
658 * Compute the data disk and parity disk indexes inside the stripe
660 *dd_idx = chunk_number % data_disks;
663 * Select the parity disk based on the user selected algorithm.
665 if (raid_conf->level == 4)
666 *pd_idx = data_disks;
667 else switch (raid_conf->algorithm) {
668 case ALGORITHM_LEFT_ASYMMETRIC:
669 *pd_idx = data_disks - stripe % raid_disks;
670 if (*dd_idx >= *pd_idx)
671 (*dd_idx)++;
672 break;
673 case ALGORITHM_RIGHT_ASYMMETRIC:
674 *pd_idx = stripe % raid_disks;
675 if (*dd_idx >= *pd_idx)
676 (*dd_idx)++;
677 break;
678 case ALGORITHM_LEFT_SYMMETRIC:
679 *pd_idx = data_disks - stripe % raid_disks;
680 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
681 break;
682 case ALGORITHM_RIGHT_SYMMETRIC:
683 *pd_idx = stripe % raid_disks;
684 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
685 break;
686 default:
687 printk ("raid5: unsupported algorithm %d\n", raid_conf->algorithm);
691 * Finally, compute the new sector number
693 new_sector = stripe * sectors_per_chunk + chunk_offset;
695 #if 0
696 if ( *dd_idx > data_disks || *pd_idx > data_disks ||
697 chunk_offset + bh->b_size / 512 > sectors_per_chunk )
699 printk ("raid5: bug: dd_idx == %d, pd_idx == %d, chunk_offset == %d\n",
700 *dd_idx, *pd_idx, chunk_offset);
701 #endif
703 return new_sector;
706 static unsigned long compute_blocknr(struct stripe_head *sh, int i)
708 struct raid5_data *raid_conf = sh->raid_conf;
709 int raid_disks = raid_conf->raid_disks, data_disks = raid_disks - 1;
710 unsigned long new_sector = sh->sector, check;
711 int sectors_per_chunk = raid_conf->chunk_size >> 9;
712 unsigned long stripe = new_sector / sectors_per_chunk;
713 int chunk_offset = new_sector % sectors_per_chunk;
714 int chunk_number, dummy1, dummy2, dd_idx = i;
715 unsigned long r_sector, blocknr;
717 switch (raid_conf->algorithm) {
718 case ALGORITHM_LEFT_ASYMMETRIC:
719 case ALGORITHM_RIGHT_ASYMMETRIC:
720 if (i > sh->pd_idx)
721 i--;
722 break;
723 case ALGORITHM_LEFT_SYMMETRIC:
724 case ALGORITHM_RIGHT_SYMMETRIC:
725 if (i < sh->pd_idx)
726 i += raid_disks;
727 i -= (sh->pd_idx + 1);
728 break;
729 default:
730 printk ("raid5: unsupported algorithm %d\n", raid_conf->algorithm);
733 chunk_number = stripe * data_disks + i;
734 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
735 blocknr = r_sector / (sh->size >> 9);
737 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, raid_conf);
738 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
739 printk("compute_blocknr: map not correct\n");
740 return 0;
742 return blocknr;
745 #ifdef HAVE_ARCH_XORBLOCK
746 static void xor_block(struct buffer_head *dest, struct buffer_head *source)
748 __xor_block((char *) dest->b_data, (char *) source->b_data, dest->b_size);
750 #else
751 static void xor_block(struct buffer_head *dest, struct buffer_head *source)
753 long lines = dest->b_size / (sizeof (long)) / 8, i;
754 long *destp = (long *) dest->b_data, *sourcep = (long *) source->b_data;
756 for (i = lines; i > 0; i--) {
757 *(destp + 0) ^= *(sourcep + 0);
758 *(destp + 1) ^= *(sourcep + 1);
759 *(destp + 2) ^= *(sourcep + 2);
760 *(destp + 3) ^= *(sourcep + 3);
761 *(destp + 4) ^= *(sourcep + 4);
762 *(destp + 5) ^= *(sourcep + 5);
763 *(destp + 6) ^= *(sourcep + 6);
764 *(destp + 7) ^= *(sourcep + 7);
765 destp += 8;
766 sourcep += 8;
769 #endif
771 static void compute_block(struct stripe_head *sh, int dd_idx)
773 struct raid5_data *raid_conf = sh->raid_conf;
774 int i, disks = raid_conf->raid_disks;
776 PRINTK(("compute_block, stripe %lu, idx %d\n", sh->sector, dd_idx));
778 if (sh->bh_old[dd_idx] == NULL)
779 sh->bh_old[dd_idx] = raid5_kmalloc_buffer(sh, sh->size);
780 raid5_build_block(sh, sh->bh_old[dd_idx], dd_idx);
782 memset(sh->bh_old[dd_idx]->b_data, 0, sh->size);
783 for (i = 0; i < disks; i++) {
784 if (i == dd_idx)
785 continue;
786 if (sh->bh_old[i]) {
787 xor_block(sh->bh_old[dd_idx], sh->bh_old[i]);
788 continue;
789 } else
790 printk("compute_block() %d, stripe %lu, %d not present\n", dd_idx, sh->sector, i);
792 raid5_mark_buffer_uptodate(sh->bh_old[dd_idx], 1);
795 static void compute_parity(struct stripe_head *sh, int method)
797 struct raid5_data *raid_conf = sh->raid_conf;
798 int i, pd_idx = sh->pd_idx, disks = raid_conf->raid_disks;
800 PRINTK(("compute_parity, stripe %lu, method %d\n", sh->sector, method));
801 for (i = 0; i < disks; i++) {
802 if (i == pd_idx || !sh->bh_new[i])
803 continue;
804 if (!sh->bh_copy[i])
805 sh->bh_copy[i] = raid5_kmalloc_buffer(sh, sh->size);
806 raid5_build_block(sh, sh->bh_copy[i], i);
807 mark_buffer_clean(sh->bh_new[i]);
808 memcpy(sh->bh_copy[i]->b_data, sh->bh_new[i]->b_data, sh->size);
810 if (sh->bh_copy[pd_idx] == NULL)
811 sh->bh_copy[pd_idx] = raid5_kmalloc_buffer(sh, sh->size);
812 raid5_build_block(sh, sh->bh_copy[pd_idx], sh->pd_idx);
814 if (method == RECONSTRUCT_WRITE) {
815 memset(sh->bh_copy[pd_idx]->b_data, 0, sh->size);
816 for (i = 0; i < disks; i++) {
817 if (i == sh->pd_idx)
818 continue;
819 if (sh->bh_new[i]) {
820 xor_block(sh->bh_copy[pd_idx], sh->bh_copy[i]);
821 continue;
823 if (sh->bh_old[i]) {
824 xor_block(sh->bh_copy[pd_idx], sh->bh_old[i]);
825 continue;
828 } else if (method == READ_MODIFY_WRITE) {
829 memcpy(sh->bh_copy[pd_idx]->b_data, sh->bh_old[pd_idx]->b_data, sh->size);
830 for (i = 0; i < disks; i++) {
831 if (i == sh->pd_idx)
832 continue;
833 if (sh->bh_new[i] && sh->bh_old[i]) {
834 xor_block(sh->bh_copy[pd_idx], sh->bh_copy[i]);
835 xor_block(sh->bh_copy[pd_idx], sh->bh_old[i]);
836 continue;
840 raid5_mark_buffer_uptodate(sh->bh_copy[pd_idx], 1);
843 static void add_stripe_bh (struct stripe_head *sh, struct buffer_head *bh, int dd_idx, int rw)
845 struct raid5_data *raid_conf = sh->raid_conf;
846 struct buffer_head *bh_req;
848 if (sh->bh_new[dd_idx]) {
849 printk("raid5: bug: stripe->bh_new[%d], sector %lu exists\n", dd_idx, sh->sector);
850 printk("forcing oops.\n");
851 *(int*)0=0;
854 set_bit(BH_Lock, &bh->b_state);
856 bh_req = raid5_kmalloc_bh(sh);
857 raid5_build_block(sh, bh_req, dd_idx);
858 bh_req->b_data = bh->b_data;
860 if (sh->phase == PHASE_COMPLETE && sh->cmd == STRIPE_NONE) {
861 sh->phase = PHASE_BEGIN;
862 sh->cmd = (rw == READ) ? STRIPE_READ : STRIPE_WRITE;
863 raid_conf->nr_pending_stripes++;
864 atomic_inc(&raid_conf->nr_handle);
866 sh->bh_new[dd_idx] = bh;
867 sh->bh_req[dd_idx] = bh_req;
868 sh->cmd_new[dd_idx] = rw;
869 sh->new[dd_idx] = 1;
872 static void complete_stripe(struct stripe_head *sh)
874 struct raid5_data *raid_conf = sh->raid_conf;
875 int disks = raid_conf->raid_disks;
876 int i, new = 0;
878 PRINTK(("complete_stripe %lu\n", sh->sector));
879 for (i = 0; i < disks; i++) {
880 if (sh->cmd == STRIPE_WRITE && i == sh->pd_idx)
881 raid5_update_old_bh(sh, i);
882 if (sh->bh_new[i]) {
883 if (!sh->new[i]) {
884 #if 0
885 if (sh->cmd == STRIPE_WRITE) {
886 if (memcmp(sh->bh_new[i]->b_data, sh->bh_copy[i]->b_data, sh->size)) {
887 printk("copy differs, %s, sector %lu ",
888 test_bit(BH_Dirty, &sh->bh_new[i]->b_state) ? "dirty" : "clean",
889 sh->sector);
890 } else if (test_bit(BH_Dirty, &sh->bh_new[i]->b_state))
891 printk("sector %lu dirty\n", sh->sector);
893 #endif
894 if (sh->cmd == STRIPE_WRITE)
895 raid5_update_old_bh(sh, i);
896 raid5_end_buffer_io(sh, i, 1);
897 continue;
898 } else
899 new++;
901 if (new && sh->cmd == STRIPE_WRITE)
902 printk("raid5: bug, completed STRIPE_WRITE with new == %d\n", new);
904 if (!new)
905 finish_stripe(sh);
906 else {
907 PRINTK(("stripe %lu, new == %d\n", sh->sector, new));
908 sh->phase = PHASE_BEGIN;
913 * handle_stripe() is our main logic routine. Note that:
915 * 1. lock_stripe() should be used whenever we can't accept additonal
916 * buffers, either during short sleeping in handle_stripe() or
917 * during io operations.
919 * 2. We should be careful to set sh->nr_pending whenever we sleep,
920 * to prevent re-entry of handle_stripe() for the same sh.
922 * 3. raid_conf->failed_disks and disk->operational can be changed
923 * from an interrupt. This complicates things a bit, but it allows
924 * us to stop issuing requests for a failed drive as soon as possible.
926 static void handle_stripe(struct stripe_head *sh)
928 struct raid5_data *raid_conf = sh->raid_conf;
929 struct md_dev *mddev = raid_conf->mddev;
930 int minor = (int) (mddev - md_dev);
931 struct buffer_head *bh;
932 int disks = raid_conf->raid_disks;
933 int i, nr = 0, nr_read = 0, nr_write = 0;
934 int nr_cache = 0, nr_cache_other = 0, nr_cache_overwrite = 0, parity = 0;
935 int nr_failed_other = 0, nr_failed_overwrite = 0, parity_failed = 0;
936 int reading = 0, nr_writing = 0;
937 int method1 = INT_MAX, method2 = INT_MAX;
938 int block;
939 unsigned long flags;
940 int operational[MD_SB_DISKS], failed_disks = raid_conf->failed_disks;
942 PRINTK(("handle_stripe(), stripe %lu\n", sh->sector));
943 if (sh->nr_pending) {
944 printk("handle_stripe(), stripe %lu, io still pending\n", sh->sector);
945 return;
947 if (sh->phase == PHASE_COMPLETE) {
948 printk("handle_stripe(), stripe %lu, already complete\n", sh->sector);
949 return;
952 atomic_dec(&raid_conf->nr_handle);
954 if (test_and_clear_bit(STRIPE_ERROR, &sh->state)) {
955 printk("raid5: restarting stripe %lu\n", sh->sector);
956 sh->phase = PHASE_BEGIN;
959 if ((sh->cmd == STRIPE_WRITE && sh->phase == PHASE_WRITE) ||
960 (sh->cmd == STRIPE_READ && sh->phase == PHASE_READ)) {
962 * Completed
964 complete_stripe(sh);
965 if (sh->phase == PHASE_COMPLETE)
966 return;
969 save_flags(flags);
970 cli();
971 for (i = 0; i < disks; i++) {
972 operational[i] = raid_conf->disks[i].operational;
973 if (i == sh->pd_idx && raid_conf->resync_parity)
974 operational[i] = 0;
976 failed_disks = raid_conf->failed_disks;
977 restore_flags(flags);
979 if (failed_disks > 1) {
980 for (i = 0; i < disks; i++) {
981 if (sh->bh_new[i]) {
982 raid5_end_buffer_io(sh, i, 0);
983 continue;
986 finish_stripe(sh);
987 return;
990 for (i = 0; i < disks; i++) {
991 if (sh->bh_old[i])
992 nr_cache++;
993 if (i == sh->pd_idx) {
994 if (sh->bh_old[i])
995 parity = 1;
996 else if(!operational[i])
997 parity_failed = 1;
998 continue;
1000 if (!sh->bh_new[i]) {
1001 if (sh->bh_old[i])
1002 nr_cache_other++;
1003 else if (!operational[i])
1004 nr_failed_other++;
1005 continue;
1007 sh->new[i] = 0;
1008 nr++;
1009 if (sh->cmd_new[i] == READ)
1010 nr_read++;
1011 if (sh->cmd_new[i] == WRITE)
1012 nr_write++;
1013 if (sh->bh_old[i])
1014 nr_cache_overwrite++;
1015 else if (!operational[i])
1016 nr_failed_overwrite++;
1019 if (nr_write && nr_read)
1020 printk("raid5: bug, nr_write == %d, nr_read == %d, sh->cmd == %d\n", nr_write, nr_read, sh->cmd);
1022 if (nr_write) {
1024 * Attempt to add entries :-)
1026 if (nr_write != disks - 1) {
1027 for (i = 0; i < disks; i++) {
1028 if (i == sh->pd_idx)
1029 continue;
1030 if (sh->bh_new[i])
1031 continue;
1032 block = (int) compute_blocknr(sh, i);
1033 bh = find_buffer(MKDEV(MD_MAJOR, minor), block, sh->size);
1034 if (bh && bh->b_count == 0 && buffer_dirty(bh) && !buffer_locked(bh)) {
1035 PRINTK(("Whee.. sector %lu, index %d (%d) found in the buffer cache!\n", sh->sector, i, block));
1036 add_stripe_bh(sh, bh, i, WRITE);
1037 sh->new[i] = 0;
1038 nr++; nr_write++;
1039 if (sh->bh_old[i]) {
1040 nr_cache_overwrite++;
1041 nr_cache_other--;
1042 } else if (!operational[i]) {
1043 nr_failed_overwrite++;
1044 nr_failed_other--;
1049 PRINTK(("handle_stripe() -- begin writing, stripe %lu\n", sh->sector));
1051 * Writing, need to update parity buffer.
1053 * Compute the number of I/O requests in the "reconstruct
1054 * write" and "read modify write" methods.
1056 if (!nr_failed_other)
1057 method1 = (disks - 1) - (nr_write + nr_cache_other);
1058 if (!nr_failed_overwrite && !parity_failed)
1059 method2 = nr_write - nr_cache_overwrite + (1 - parity);
1061 if (method1 == INT_MAX && method2 == INT_MAX)
1062 printk("raid5: bug: method1 == method2 == INT_MAX\n");
1063 PRINTK(("handle_stripe(), sector %lu, nr_write %d, method1 %d, method2 %d\n", sh->sector, nr_write, method1, method2));
1065 if (!method1 || !method2) {
1066 lock_stripe(sh);
1067 sh->nr_pending++;
1068 sh->phase = PHASE_WRITE;
1069 compute_parity(sh, method1 <= method2 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1070 for (i = 0; i < disks; i++) {
1071 if (!operational[i] && !raid_conf->spare && !raid_conf->resync_parity)
1072 continue;
1073 if (i == sh->pd_idx || sh->bh_new[i])
1074 nr_writing++;
1077 sh->nr_pending = nr_writing;
1078 PRINTK(("handle_stripe() %lu, writing back %d\n", sh->sector, sh->nr_pending));
1080 for (i = 0; i < disks; i++) {
1081 if (!operational[i] && !raid_conf->spare && !raid_conf->resync_parity)
1082 continue;
1083 bh = sh->bh_copy[i];
1084 if (i != sh->pd_idx && ((bh == NULL) ^ (sh->bh_new[i] == NULL)))
1085 printk("raid5: bug: bh == %p, bh_new[%d] == %p\n", bh, i, sh->bh_new[i]);
1086 if (i == sh->pd_idx && !bh)
1087 printk("raid5: bug: bh == NULL, i == pd_idx == %d\n", i);
1088 if (bh) {
1089 bh->b_state |= (1<<BH_Dirty);
1090 PRINTK(("making request for buffer %d\n", i));
1091 clear_bit(BH_Lock, &bh->b_state);
1092 if (!operational[i] && !raid_conf->resync_parity) {
1093 bh->b_rdev = raid_conf->spare->dev;
1094 make_request(MAJOR(raid_conf->spare->dev), WRITE, bh);
1095 } else
1096 make_request(MAJOR(raid_conf->disks[i].dev), WRITE, bh);
1099 return;
1102 lock_stripe(sh);
1103 sh->nr_pending++;
1104 if (method1 < method2) {
1105 sh->write_method = RECONSTRUCT_WRITE;
1106 for (i = 0; i < disks; i++) {
1107 if (i == sh->pd_idx)
1108 continue;
1109 if (sh->bh_new[i] || sh->bh_old[i])
1110 continue;
1111 sh->bh_old[i] = raid5_kmalloc_buffer(sh, sh->size);
1112 raid5_build_block(sh, sh->bh_old[i], i);
1113 reading++;
1115 } else {
1116 sh->write_method = READ_MODIFY_WRITE;
1117 for (i = 0; i < disks; i++) {
1118 if (sh->bh_old[i])
1119 continue;
1120 if (!sh->bh_new[i] && i != sh->pd_idx)
1121 continue;
1122 sh->bh_old[i] = raid5_kmalloc_buffer(sh, sh->size);
1123 raid5_build_block(sh, sh->bh_old[i], i);
1124 reading++;
1127 sh->phase = PHASE_READ_OLD;
1128 sh->nr_pending = reading;
1129 PRINTK(("handle_stripe() %lu, reading %d old buffers\n", sh->sector, sh->nr_pending));
1130 for (i = 0; i < disks; i++) {
1131 if (!sh->bh_old[i])
1132 continue;
1133 if (buffer_uptodate(sh->bh_old[i]))
1134 continue;
1135 clear_bit(BH_Lock, &sh->bh_old[i]->b_state);
1136 make_request(MAJOR(raid_conf->disks[i].dev), READ, sh->bh_old[i]);
1138 } else {
1140 * Reading
1142 method1 = nr_read - nr_cache_overwrite;
1143 lock_stripe(sh);
1144 sh->nr_pending++;
1146 PRINTK(("handle_stripe(), sector %lu, nr_read %d, nr_cache %d, method1 %d\n", sh->sector, nr_read, nr_cache, method1));
1147 if (!method1 || (method1 == 1 && nr_cache == disks - 1)) {
1148 PRINTK(("read %lu completed from cache\n", sh->sector));
1149 for (i = 0; i < disks; i++) {
1150 if (!sh->bh_new[i])
1151 continue;
1152 if (!sh->bh_old[i])
1153 compute_block(sh, i);
1154 memcpy(sh->bh_new[i]->b_data, sh->bh_old[i]->b_data, sh->size);
1156 sh->nr_pending--;
1157 complete_stripe(sh);
1158 return;
1160 if (nr_failed_overwrite) {
1161 sh->phase = PHASE_READ_OLD;
1162 sh->nr_pending = (disks - 1) - nr_cache;
1163 PRINTK(("handle_stripe() %lu, phase READ_OLD, pending %d\n", sh->sector, sh->nr_pending));
1164 for (i = 0; i < disks; i++) {
1165 if (sh->bh_old[i])
1166 continue;
1167 if (!operational[i])
1168 continue;
1169 sh->bh_old[i] = raid5_kmalloc_buffer(sh, sh->size);
1170 raid5_build_block(sh, sh->bh_old[i], i);
1171 clear_bit(BH_Lock, &sh->bh_old[i]->b_state);
1172 make_request(MAJOR(raid_conf->disks[i].dev), READ, sh->bh_old[i]);
1174 } else {
1175 sh->phase = PHASE_READ;
1176 sh->nr_pending = nr_read - nr_cache_overwrite;
1177 PRINTK(("handle_stripe() %lu, phase READ, pending %d\n", sh->sector, sh->nr_pending));
1178 for (i = 0; i < disks; i++) {
1179 if (!sh->bh_new[i])
1180 continue;
1181 if (sh->bh_old[i]) {
1182 memcpy(sh->bh_new[i]->b_data, sh->bh_old[i]->b_data, sh->size);
1183 continue;
1185 make_request(MAJOR(raid_conf->disks[i].dev), READ, sh->bh_req[i]);
1191 static int raid5_make_request (struct md_dev *mddev, int rw, struct buffer_head * bh)
1193 struct raid5_data *raid_conf = (struct raid5_data *) mddev->private;
1194 const unsigned int raid_disks = raid_conf->raid_disks;
1195 const unsigned int data_disks = raid_disks - 1;
1196 unsigned int dd_idx, pd_idx;
1197 unsigned long new_sector;
1199 struct stripe_head *sh;
1201 if (rw == READA) rw = READ;
1202 if (rw == WRITEA) rw = WRITE;
1204 new_sector = raid5_compute_sector(bh->b_rsector, raid_disks, data_disks,
1205 &dd_idx, &pd_idx, raid_conf);
1207 PRINTK(("raid5_make_request, sector %lu\n", new_sector));
1208 repeat:
1209 sh = get_stripe(raid_conf, new_sector, bh->b_size);
1210 if ((rw == READ && sh->cmd == STRIPE_WRITE) || (rw == WRITE && sh->cmd == STRIPE_READ)) {
1211 PRINTK(("raid5: lock contention, rw == %d, sh->cmd == %d\n", rw, sh->cmd));
1212 lock_stripe(sh);
1213 if (!sh->nr_pending)
1214 handle_stripe(sh);
1215 goto repeat;
1217 sh->pd_idx = pd_idx;
1218 if (sh->phase != PHASE_COMPLETE && sh->phase != PHASE_BEGIN)
1219 PRINTK(("stripe %lu catching the bus!\n", sh->sector));
1220 if (sh->bh_new[dd_idx]) {
1221 printk("raid5: bug: stripe->bh_new[%d], sector %lu exists\n", dd_idx, sh->sector);
1222 printk("raid5: bh %p, bh_new %p\n", bh, sh->bh_new[dd_idx]);
1223 lock_stripe(sh);
1224 md_wakeup_thread(raid_conf->thread);
1225 wait_on_stripe(sh);
1226 goto repeat;
1228 add_stripe_bh(sh, bh, dd_idx, rw);
1230 md_wakeup_thread(raid_conf->thread);
1231 return 0;
1234 static void unplug_devices(struct stripe_head *sh)
1236 #if 0
1237 struct raid5_data *raid_conf = sh->raid_conf;
1238 int i;
1240 for (i = 0; i < raid_conf->raid_disks; i++)
1241 unplug_device(blk_dev + MAJOR(raid_conf->disks[i].dev));
1242 #endif
1246 * This is our raid5 kernel thread.
1248 * We scan the hash table for stripes which can be handled now.
1249 * During the scan, completed stripes are saved for us by the interrupt
1250 * handler, so that they will not have to wait for our next wakeup.
1252 static void raid5d (void *data)
1254 struct stripe_head *sh;
1255 struct raid5_data *raid_conf = data;
1256 struct md_dev *mddev = raid_conf->mddev;
1257 int i, handled = 0, unplug = 0;
1258 unsigned long flags;
1260 PRINTK(("+++ raid5d active\n"));
1262 if (mddev->sb_dirty) {
1263 mddev->sb_dirty = 0;
1264 md_update_sb((int) (mddev - md_dev));
1266 for (i = 0; i < NR_HASH; i++) {
1267 repeat:
1268 sh = raid_conf->stripe_hashtbl[i];
1269 for (; sh; sh = sh->hash_next) {
1270 if (sh->raid_conf != raid_conf)
1271 continue;
1272 if (sh->phase == PHASE_COMPLETE)
1273 continue;
1274 if (sh->nr_pending)
1275 continue;
1276 if (sh->sector == raid_conf->next_sector) {
1277 raid_conf->sector_count += (sh->size >> 9);
1278 if (raid_conf->sector_count >= 128)
1279 unplug = 1;
1280 } else
1281 unplug = 1;
1282 if (unplug) {
1283 PRINTK(("unplugging devices, sector == %lu, count == %d\n", sh->sector, raid_conf->sector_count));
1284 unplug_devices(sh);
1285 unplug = 0;
1286 raid_conf->sector_count = 0;
1288 raid_conf->next_sector = sh->sector + (sh->size >> 9);
1289 handled++;
1290 handle_stripe(sh);
1291 goto repeat;
1294 if (raid_conf) {
1295 PRINTK(("%d stripes handled, nr_handle %d\n", handled, atomic_read(&raid_conf->nr_handle)));
1296 save_flags(flags);
1297 cli();
1298 if (!atomic_read(&raid_conf->nr_handle))
1299 clear_bit(THREAD_WAKEUP, &raid_conf->thread->flags);
1301 PRINTK(("--- raid5d inactive\n"));
1304 #if SUPPORT_RECONSTRUCTION
1306 * Private kernel thread for parity reconstruction after an unclean
1307 * shutdown. Reconstruction on spare drives in case of a failed drive
1308 * is done by the generic mdsyncd.
1310 static void raid5syncd (void *data)
1312 struct raid5_data *raid_conf = data;
1313 struct md_dev *mddev = raid_conf->mddev;
1315 if (!raid_conf->resync_parity)
1316 return;
1317 md_do_sync(mddev);
1318 raid_conf->resync_parity = 0;
1320 #endif /* SUPPORT_RECONSTRUCTION */
1322 static int __check_consistency (struct md_dev *mddev, int row)
1324 struct raid5_data *raid_conf = mddev->private;
1325 kdev_t dev;
1326 struct buffer_head *bh[MD_SB_DISKS], tmp;
1327 int i, rc = 0, nr = 0;
1329 if (raid_conf->working_disks != raid_conf->raid_disks)
1330 return 0;
1331 tmp.b_size = 4096;
1332 if ((tmp.b_data = (char *) get_free_page(GFP_KERNEL)) == NULL)
1333 return 0;
1334 memset(bh, 0, MD_SB_DISKS * sizeof(struct buffer_head *));
1335 for (i = 0; i < raid_conf->raid_disks; i++) {
1336 dev = raid_conf->disks[i].dev;
1337 set_blocksize(dev, 4096);
1338 if ((bh[i] = bread(dev, row / 4, 4096)) == NULL)
1339 break;
1340 nr++;
1342 if (nr == raid_conf->raid_disks) {
1343 for (i = 1; i < nr; i++)
1344 xor_block(&tmp, bh[i]);
1345 if (memcmp(tmp.b_data, bh[0]->b_data, 4096))
1346 rc = 1;
1348 for (i = 0; i < raid_conf->raid_disks; i++) {
1349 dev = raid_conf->disks[i].dev;
1350 if (bh[i]) {
1351 bforget(bh[i]);
1352 bh[i] = NULL;
1354 fsync_dev(dev);
1355 invalidate_buffers(dev);
1357 free_page((unsigned long) tmp.b_data);
1358 return rc;
1361 static int check_consistency (struct md_dev *mddev)
1363 int size = mddev->sb->size;
1364 int row;
1366 for (row = 0; row < size; row += size / 8)
1367 if (__check_consistency(mddev, row))
1368 return 1;
1369 return 0;
1372 static int raid5_run (int minor, struct md_dev *mddev)
1374 struct raid5_data *raid_conf;
1375 int i, j, raid_disk, memory;
1376 md_superblock_t *sb = mddev->sb;
1377 md_descriptor_t *descriptor;
1378 struct real_dev *realdev;
1380 MOD_INC_USE_COUNT;
1382 if (sb->level != 5 && sb->level != 4) {
1383 printk("raid5: %s: raid level not set to 4/5 (%d)\n", kdevname(MKDEV(MD_MAJOR, minor)), sb->level);
1384 MOD_DEC_USE_COUNT;
1385 return -EIO;
1388 mddev->private = kmalloc (sizeof (struct raid5_data), GFP_KERNEL);
1389 if ((raid_conf = mddev->private) == NULL)
1390 goto abort;
1391 memset (raid_conf, 0, sizeof (*raid_conf));
1392 raid_conf->mddev = mddev;
1394 if ((raid_conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1395 goto abort;
1396 memset(raid_conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1398 init_waitqueue_head(&raid_conf->wait_for_stripe);
1399 PRINTK(("raid5_run(%d) called.\n", minor));
1401 for (i = 0; i < mddev->nb_dev; i++) {
1402 realdev = &mddev->devices[i];
1403 if (!realdev->sb) {
1404 printk(KERN_ERR "raid5: disabled device %s (couldn't access raid superblock)\n", kdevname(realdev->dev));
1405 continue;
1409 * This is important -- we are using the descriptor on
1410 * the disk only to get a pointer to the descriptor on
1411 * the main superblock, which might be more recent.
1413 descriptor = &sb->disks[realdev->sb->descriptor.number];
1414 if (descriptor->state & (1 << MD_FAULTY_DEVICE)) {
1415 printk(KERN_ERR "raid5: disabled device %s (errors detected)\n", kdevname(realdev->dev));
1416 continue;
1418 if (descriptor->state & (1 << MD_ACTIVE_DEVICE)) {
1419 if (!(descriptor->state & (1 << MD_SYNC_DEVICE))) {
1420 printk(KERN_ERR "raid5: disabled device %s (not in sync)\n", kdevname(realdev->dev));
1421 continue;
1423 raid_disk = descriptor->raid_disk;
1424 if (descriptor->number > sb->nr_disks || raid_disk > sb->raid_disks) {
1425 printk(KERN_ERR "raid5: disabled device %s (inconsistent descriptor)\n", kdevname(realdev->dev));
1426 continue;
1428 if (raid_conf->disks[raid_disk].operational) {
1429 printk(KERN_ERR "raid5: disabled device %s (device %d already operational)\n", kdevname(realdev->dev), raid_disk);
1430 continue;
1432 printk(KERN_INFO "raid5: device %s operational as raid disk %d\n", kdevname(realdev->dev), raid_disk);
1434 raid_conf->disks[raid_disk].number = descriptor->number;
1435 raid_conf->disks[raid_disk].raid_disk = raid_disk;
1436 raid_conf->disks[raid_disk].dev = mddev->devices[i].dev;
1437 raid_conf->disks[raid_disk].operational = 1;
1439 raid_conf->working_disks++;
1440 } else {
1442 * Must be a spare disk ..
1444 printk(KERN_INFO "raid5: spare disk %s\n", kdevname(realdev->dev));
1445 raid_disk = descriptor->raid_disk;
1446 raid_conf->disks[raid_disk].number = descriptor->number;
1447 raid_conf->disks[raid_disk].raid_disk = raid_disk;
1448 raid_conf->disks[raid_disk].dev = mddev->devices [i].dev;
1450 raid_conf->disks[raid_disk].operational = 0;
1451 raid_conf->disks[raid_disk].write_only = 0;
1452 raid_conf->disks[raid_disk].spare = 1;
1455 raid_conf->raid_disks = sb->raid_disks;
1456 raid_conf->failed_disks = raid_conf->raid_disks - raid_conf->working_disks;
1457 raid_conf->mddev = mddev;
1458 raid_conf->chunk_size = sb->chunk_size;
1459 raid_conf->level = sb->level;
1460 raid_conf->algorithm = sb->parity_algorithm;
1461 raid_conf->max_nr_stripes = NR_STRIPES;
1463 if (raid_conf->working_disks != sb->raid_disks && sb->state != (1 << MD_SB_CLEAN)) {
1464 printk(KERN_ALERT "raid5: raid set %s not clean and not all disks are operational -- run ckraid\n", kdevname(MKDEV(MD_MAJOR, minor)));
1465 goto abort;
1467 if (!raid_conf->chunk_size || raid_conf->chunk_size % 4) {
1468 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n", raid_conf->chunk_size, kdevname(MKDEV(MD_MAJOR, minor)));
1469 goto abort;
1471 if (raid_conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1472 printk(KERN_ERR "raid5: unsupported parity algorithm %d for %s\n", raid_conf->algorithm, kdevname(MKDEV(MD_MAJOR, minor)));
1473 goto abort;
1475 if (raid_conf->failed_disks > 1) {
1476 printk(KERN_ERR "raid5: not enough operational devices for %s (%d/%d failed)\n", kdevname(MKDEV(MD_MAJOR, minor)), raid_conf->failed_disks, raid_conf->raid_disks);
1477 goto abort;
1480 if ((sb->state & (1 << MD_SB_CLEAN)) && check_consistency(mddev)) {
1481 printk(KERN_ERR "raid5: detected raid-5 xor inconsistenty -- run ckraid\n");
1482 sb->state |= 1 << MD_SB_ERRORS;
1483 goto abort;
1486 if ((raid_conf->thread = md_register_thread(raid5d, raid_conf)) == NULL) {
1487 printk(KERN_ERR "raid5: couldn't allocate thread for %s\n", kdevname(MKDEV(MD_MAJOR, minor)));
1488 goto abort;
1491 #if SUPPORT_RECONSTRUCTION
1492 if ((raid_conf->resync_thread = md_register_thread(raid5syncd, raid_conf)) == NULL) {
1493 printk(KERN_ERR "raid5: couldn't allocate thread for %s\n", kdevname(MKDEV(MD_MAJOR, minor)));
1494 goto abort;
1496 #endif /* SUPPORT_RECONSTRUCTION */
1498 memory = raid_conf->max_nr_stripes * (sizeof(struct stripe_head) +
1499 raid_conf->raid_disks * (sizeof(struct buffer_head) +
1500 2 * (sizeof(struct buffer_head) + PAGE_SIZE))) / 1024;
1501 if (grow_stripes(raid_conf, raid_conf->max_nr_stripes, GFP_KERNEL)) {
1502 printk(KERN_ERR "raid5: couldn't allocate %dkB for buffers\n", memory);
1503 shrink_stripes(raid_conf, raid_conf->max_nr_stripes);
1504 goto abort;
1505 } else
1506 printk(KERN_INFO "raid5: allocated %dkB for %s\n", memory, kdevname(MKDEV(MD_MAJOR, minor)));
1509 * Regenerate the "device is in sync with the raid set" bit for
1510 * each device.
1512 for (i = 0; i < sb->nr_disks ; i++) {
1513 sb->disks[i].state &= ~(1 << MD_SYNC_DEVICE);
1514 for (j = 0; j < sb->raid_disks; j++) {
1515 if (!raid_conf->disks[j].operational)
1516 continue;
1517 if (sb->disks[i].number == raid_conf->disks[j].number)
1518 sb->disks[i].state |= 1 << MD_SYNC_DEVICE;
1521 sb->active_disks = raid_conf->working_disks;
1523 if (sb->active_disks == sb->raid_disks)
1524 printk("raid5: raid level %d set %s active with %d out of %d devices, algorithm %d\n", raid_conf->level, kdevname(MKDEV(MD_MAJOR, minor)), sb->active_disks, sb->raid_disks, raid_conf->algorithm);
1525 else
1526 printk(KERN_ALERT "raid5: raid level %d set %s active with %d out of %d devices, algorithm %d\n", raid_conf->level, kdevname(MKDEV(MD_MAJOR, minor)), sb->active_disks, sb->raid_disks, raid_conf->algorithm);
1528 if ((sb->state & (1 << MD_SB_CLEAN)) == 0) {
1529 printk("raid5: raid set %s not clean; re-constructing parity\n", kdevname(MKDEV(MD_MAJOR, minor)));
1530 raid_conf->resync_parity = 1;
1531 #if SUPPORT_RECONSTRUCTION
1532 md_wakeup_thread(raid_conf->resync_thread);
1533 #endif /* SUPPORT_RECONSTRUCTION */
1536 /* Ok, everything is just fine now */
1537 return (0);
1538 abort:
1539 if (raid_conf) {
1540 if (raid_conf->stripe_hashtbl)
1541 free_pages((unsigned long) raid_conf->stripe_hashtbl, HASH_PAGES_ORDER);
1542 kfree(raid_conf);
1544 mddev->private = NULL;
1545 printk(KERN_ALERT "raid5: failed to run raid set %s\n", kdevname(MKDEV(MD_MAJOR, minor)));
1546 MOD_DEC_USE_COUNT;
1547 return -EIO;
1550 static int raid5_stop (int minor, struct md_dev *mddev)
1552 struct raid5_data *raid_conf = (struct raid5_data *) mddev->private;
1554 shrink_stripe_cache(raid_conf, raid_conf->max_nr_stripes);
1555 shrink_stripes(raid_conf, raid_conf->max_nr_stripes);
1556 md_unregister_thread(raid_conf->thread);
1557 #if SUPPORT_RECONSTRUCTION
1558 md_unregister_thread(raid_conf->resync_thread);
1559 #endif /* SUPPORT_RECONSTRUCTION */
1560 free_pages((unsigned long) raid_conf->stripe_hashtbl, HASH_PAGES_ORDER);
1561 kfree(raid_conf);
1562 mddev->private = NULL;
1563 MOD_DEC_USE_COUNT;
1564 return 0;
1567 static int raid5_status (char *page, int minor, struct md_dev *mddev)
1569 struct raid5_data *raid_conf = (struct raid5_data *) mddev->private;
1570 md_superblock_t *sb = mddev->sb;
1571 int sz = 0, i;
1573 sz += sprintf (page+sz, " level %d, %dk chunk, algorithm %d", sb->level, sb->chunk_size >> 10, sb->parity_algorithm);
1574 sz += sprintf (page+sz, " [%d/%d] [", raid_conf->raid_disks, raid_conf->working_disks);
1575 for (i = 0; i < raid_conf->raid_disks; i++)
1576 sz += sprintf (page+sz, "%s", raid_conf->disks[i].operational ? "U" : "_");
1577 sz += sprintf (page+sz, "]");
1578 return sz;
1581 static int raid5_mark_spare(struct md_dev *mddev, md_descriptor_t *spare, int state)
1583 int i = 0, failed_disk = -1;
1584 struct raid5_data *raid_conf = mddev->private;
1585 struct disk_info *disk = raid_conf->disks;
1586 unsigned long flags;
1587 md_superblock_t *sb = mddev->sb;
1588 md_descriptor_t *descriptor;
1590 for (i = 0; i < MD_SB_DISKS; i++, disk++) {
1591 if (disk->spare && disk->number == spare->number)
1592 goto found;
1594 return 1;
1595 found:
1596 for (i = 0, disk = raid_conf->disks; i < raid_conf->raid_disks; i++, disk++)
1597 if (!disk->operational)
1598 failed_disk = i;
1599 if (failed_disk == -1)
1600 return 1;
1601 save_flags(flags);
1602 cli();
1603 switch (state) {
1604 case SPARE_WRITE:
1605 disk->operational = 1;
1606 disk->write_only = 1;
1607 raid_conf->spare = disk;
1608 break;
1609 case SPARE_INACTIVE:
1610 disk->operational = 0;
1611 disk->write_only = 0;
1612 raid_conf->spare = NULL;
1613 break;
1614 case SPARE_ACTIVE:
1615 disk->spare = 0;
1616 disk->write_only = 0;
1618 descriptor = &sb->disks[raid_conf->disks[failed_disk].number];
1619 i = spare->raid_disk;
1620 disk->raid_disk = spare->raid_disk = descriptor->raid_disk;
1621 if (disk->raid_disk != failed_disk)
1622 printk("raid5: disk->raid_disk != failed_disk");
1623 descriptor->raid_disk = i;
1625 raid_conf->spare = NULL;
1626 raid_conf->working_disks++;
1627 raid_conf->failed_disks--;
1628 raid_conf->disks[failed_disk] = *disk;
1629 break;
1630 default:
1631 printk("raid5_mark_spare: bug: state == %d\n", state);
1632 restore_flags(flags);
1633 return 1;
1635 restore_flags(flags);
1636 return 0;
1639 static struct md_personality raid5_personality=
1641 "raid5",
1642 raid5_map,
1643 raid5_make_request,
1644 raid5_end_request,
1645 raid5_run,
1646 raid5_stop,
1647 raid5_status,
1648 NULL, /* no ioctls */
1650 raid5_error,
1651 /* raid5_hot_add_disk, */ NULL,
1652 /* raid1_hot_remove_drive */ NULL,
1653 raid5_mark_spare
1656 int raid5_init (void)
1658 return register_md_personality (RAID5, &raid5_personality);
1661 #ifdef MODULE
1662 int init_module (void)
1664 return raid5_init();
1667 void cleanup_module (void)
1669 unregister_md_personality (RAID5);
1671 #endif