[XFRM]: skb_cow_data() does not set proper owner for new skbs.
[linux-2.6/verdex.git] / drivers / md / raid5.c
blob3cb11ac232fa8ca9ada29fb9cdb99e15cf1ba0f2
1 /*
2 * raid5.c : Multiple Devices driver for Linux
3 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
4 * Copyright (C) 1999, 2000 Ingo Molnar
6 * RAID-5 management functions.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2, or (at your option)
11 * any later version.
13 * You should have received a copy of the GNU General Public License
14 * (for example /usr/src/linux/COPYING); if not, write to the Free
15 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 #include <linux/config.h>
20 #include <linux/module.h>
21 #include <linux/slab.h>
22 #include <linux/raid/raid5.h>
23 #include <linux/highmem.h>
24 #include <linux/bitops.h>
25 #include <asm/atomic.h>
28 * Stripe cache
31 #define NR_STRIPES 256
32 #define STRIPE_SIZE PAGE_SIZE
33 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
34 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
35 #define IO_THRESHOLD 1
36 #define HASH_PAGES 1
37 #define HASH_PAGES_ORDER 0
38 #define NR_HASH (HASH_PAGES * PAGE_SIZE / sizeof(struct stripe_head *))
39 #define HASH_MASK (NR_HASH - 1)
41 #define stripe_hash(conf, sect) ((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK])
43 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
44 * order without overlap. There may be several bio's per stripe+device, and
45 * a bio could span several devices.
46 * When walking this list for a particular stripe+device, we must never proceed
47 * beyond a bio that extends past this device, as the next bio might no longer
48 * be valid.
49 * This macro is used to determine the 'next' bio in the list, given the sector
50 * of the current stripe+device
52 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
54 * The following can be used to debug the driver
56 #define RAID5_DEBUG 0
57 #define RAID5_PARANOIA 1
58 #if RAID5_PARANOIA && defined(CONFIG_SMP)
59 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
60 #else
61 # define CHECK_DEVLOCK()
62 #endif
64 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
65 #if RAID5_DEBUG
66 #define inline
67 #define __inline__
68 #endif
70 static void print_raid5_conf (raid5_conf_t *conf);
72 static inline void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
74 if (atomic_dec_and_test(&sh->count)) {
75 if (!list_empty(&sh->lru))
76 BUG();
77 if (atomic_read(&conf->active_stripes)==0)
78 BUG();
79 if (test_bit(STRIPE_HANDLE, &sh->state)) {
80 if (test_bit(STRIPE_DELAYED, &sh->state))
81 list_add_tail(&sh->lru, &conf->delayed_list);
82 else
83 list_add_tail(&sh->lru, &conf->handle_list);
84 md_wakeup_thread(conf->mddev->thread);
85 } else {
86 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
87 atomic_dec(&conf->preread_active_stripes);
88 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
89 md_wakeup_thread(conf->mddev->thread);
91 list_add_tail(&sh->lru, &conf->inactive_list);
92 atomic_dec(&conf->active_stripes);
93 if (!conf->inactive_blocked ||
94 atomic_read(&conf->active_stripes) < (NR_STRIPES*3/4))
95 wake_up(&conf->wait_for_stripe);
99 static void release_stripe(struct stripe_head *sh)
101 raid5_conf_t *conf = sh->raid_conf;
102 unsigned long flags;
104 spin_lock_irqsave(&conf->device_lock, flags);
105 __release_stripe(conf, sh);
106 spin_unlock_irqrestore(&conf->device_lock, flags);
109 static void remove_hash(struct stripe_head *sh)
111 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
113 if (sh->hash_pprev) {
114 if (sh->hash_next)
115 sh->hash_next->hash_pprev = sh->hash_pprev;
116 *sh->hash_pprev = sh->hash_next;
117 sh->hash_pprev = NULL;
121 static __inline__ void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
123 struct stripe_head **shp = &stripe_hash(conf, sh->sector);
125 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
127 CHECK_DEVLOCK();
128 if ((sh->hash_next = *shp) != NULL)
129 (*shp)->hash_pprev = &sh->hash_next;
130 *shp = sh;
131 sh->hash_pprev = shp;
135 /* find an idle stripe, make sure it is unhashed, and return it. */
136 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
138 struct stripe_head *sh = NULL;
139 struct list_head *first;
141 CHECK_DEVLOCK();
142 if (list_empty(&conf->inactive_list))
143 goto out;
144 first = conf->inactive_list.next;
145 sh = list_entry(first, struct stripe_head, lru);
146 list_del_init(first);
147 remove_hash(sh);
148 atomic_inc(&conf->active_stripes);
149 out:
150 return sh;
153 static void shrink_buffers(struct stripe_head *sh, int num)
155 struct page *p;
156 int i;
158 for (i=0; i<num ; i++) {
159 p = sh->dev[i].page;
160 if (!p)
161 continue;
162 sh->dev[i].page = NULL;
163 page_cache_release(p);
167 static int grow_buffers(struct stripe_head *sh, int num)
169 int i;
171 for (i=0; i<num; i++) {
172 struct page *page;
174 if (!(page = alloc_page(GFP_KERNEL))) {
175 return 1;
177 sh->dev[i].page = page;
179 return 0;
182 static void raid5_build_block (struct stripe_head *sh, int i);
184 static inline void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx)
186 raid5_conf_t *conf = sh->raid_conf;
187 int disks = conf->raid_disks, i;
189 if (atomic_read(&sh->count) != 0)
190 BUG();
191 if (test_bit(STRIPE_HANDLE, &sh->state))
192 BUG();
194 CHECK_DEVLOCK();
195 PRINTK("init_stripe called, stripe %llu\n",
196 (unsigned long long)sh->sector);
198 remove_hash(sh);
200 sh->sector = sector;
201 sh->pd_idx = pd_idx;
202 sh->state = 0;
204 for (i=disks; i--; ) {
205 struct r5dev *dev = &sh->dev[i];
207 if (dev->toread || dev->towrite || dev->written ||
208 test_bit(R5_LOCKED, &dev->flags)) {
209 printk("sector=%llx i=%d %p %p %p %d\n",
210 (unsigned long long)sh->sector, i, dev->toread,
211 dev->towrite, dev->written,
212 test_bit(R5_LOCKED, &dev->flags));
213 BUG();
215 dev->flags = 0;
216 raid5_build_block(sh, i);
218 insert_hash(conf, sh);
221 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector)
223 struct stripe_head *sh;
225 CHECK_DEVLOCK();
226 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
227 for (sh = stripe_hash(conf, sector); sh; sh = sh->hash_next)
228 if (sh->sector == sector)
229 return sh;
230 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
231 return NULL;
234 static void unplug_slaves(mddev_t *mddev);
235 static void raid5_unplug_device(request_queue_t *q);
237 static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector,
238 int pd_idx, int noblock)
240 struct stripe_head *sh;
242 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
244 spin_lock_irq(&conf->device_lock);
246 do {
247 sh = __find_stripe(conf, sector);
248 if (!sh) {
249 if (!conf->inactive_blocked)
250 sh = get_free_stripe(conf);
251 if (noblock && sh == NULL)
252 break;
253 if (!sh) {
254 conf->inactive_blocked = 1;
255 wait_event_lock_irq(conf->wait_for_stripe,
256 !list_empty(&conf->inactive_list) &&
257 (atomic_read(&conf->active_stripes) < (NR_STRIPES *3/4)
258 || !conf->inactive_blocked),
259 conf->device_lock,
260 unplug_slaves(conf->mddev);
262 conf->inactive_blocked = 0;
263 } else
264 init_stripe(sh, sector, pd_idx);
265 } else {
266 if (atomic_read(&sh->count)) {
267 if (!list_empty(&sh->lru))
268 BUG();
269 } else {
270 if (!test_bit(STRIPE_HANDLE, &sh->state))
271 atomic_inc(&conf->active_stripes);
272 if (list_empty(&sh->lru))
273 BUG();
274 list_del_init(&sh->lru);
277 } while (sh == NULL);
279 if (sh)
280 atomic_inc(&sh->count);
282 spin_unlock_irq(&conf->device_lock);
283 return sh;
286 static int grow_stripes(raid5_conf_t *conf, int num)
288 struct stripe_head *sh;
289 kmem_cache_t *sc;
290 int devs = conf->raid_disks;
292 sprintf(conf->cache_name, "raid5/%s", mdname(conf->mddev));
294 sc = kmem_cache_create(conf->cache_name,
295 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
296 0, 0, NULL, NULL);
297 if (!sc)
298 return 1;
299 conf->slab_cache = sc;
300 while (num--) {
301 sh = kmem_cache_alloc(sc, GFP_KERNEL);
302 if (!sh)
303 return 1;
304 memset(sh, 0, sizeof(*sh) + (devs-1)*sizeof(struct r5dev));
305 sh->raid_conf = conf;
306 spin_lock_init(&sh->lock);
308 if (grow_buffers(sh, conf->raid_disks)) {
309 shrink_buffers(sh, conf->raid_disks);
310 kmem_cache_free(sc, sh);
311 return 1;
313 /* we just created an active stripe so... */
314 atomic_set(&sh->count, 1);
315 atomic_inc(&conf->active_stripes);
316 INIT_LIST_HEAD(&sh->lru);
317 release_stripe(sh);
319 return 0;
322 static void shrink_stripes(raid5_conf_t *conf)
324 struct stripe_head *sh;
326 while (1) {
327 spin_lock_irq(&conf->device_lock);
328 sh = get_free_stripe(conf);
329 spin_unlock_irq(&conf->device_lock);
330 if (!sh)
331 break;
332 if (atomic_read(&sh->count))
333 BUG();
334 shrink_buffers(sh, conf->raid_disks);
335 kmem_cache_free(conf->slab_cache, sh);
336 atomic_dec(&conf->active_stripes);
338 kmem_cache_destroy(conf->slab_cache);
339 conf->slab_cache = NULL;
342 static int raid5_end_read_request (struct bio * bi, unsigned int bytes_done,
343 int error)
345 struct stripe_head *sh = bi->bi_private;
346 raid5_conf_t *conf = sh->raid_conf;
347 int disks = conf->raid_disks, i;
348 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
350 if (bi->bi_size)
351 return 1;
353 for (i=0 ; i<disks; i++)
354 if (bi == &sh->dev[i].req)
355 break;
357 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
358 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
359 uptodate);
360 if (i == disks) {
361 BUG();
362 return 0;
365 if (uptodate) {
366 #if 0
367 struct bio *bio;
368 unsigned long flags;
369 spin_lock_irqsave(&conf->device_lock, flags);
370 /* we can return a buffer if we bypassed the cache or
371 * if the top buffer is not in highmem. If there are
372 * multiple buffers, leave the extra work to
373 * handle_stripe
375 buffer = sh->bh_read[i];
376 if (buffer &&
377 (!PageHighMem(buffer->b_page)
378 || buffer->b_page == bh->b_page )
380 sh->bh_read[i] = buffer->b_reqnext;
381 buffer->b_reqnext = NULL;
382 } else
383 buffer = NULL;
384 spin_unlock_irqrestore(&conf->device_lock, flags);
385 if (sh->bh_page[i]==bh->b_page)
386 set_buffer_uptodate(bh);
387 if (buffer) {
388 if (buffer->b_page != bh->b_page)
389 memcpy(buffer->b_data, bh->b_data, bh->b_size);
390 buffer->b_end_io(buffer, 1);
392 #else
393 set_bit(R5_UPTODATE, &sh->dev[i].flags);
394 #endif
395 } else {
396 md_error(conf->mddev, conf->disks[i].rdev);
397 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
399 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
400 #if 0
401 /* must restore b_page before unlocking buffer... */
402 if (sh->bh_page[i] != bh->b_page) {
403 bh->b_page = sh->bh_page[i];
404 bh->b_data = page_address(bh->b_page);
405 clear_buffer_uptodate(bh);
407 #endif
408 clear_bit(R5_LOCKED, &sh->dev[i].flags);
409 set_bit(STRIPE_HANDLE, &sh->state);
410 release_stripe(sh);
411 return 0;
414 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
415 int error)
417 struct stripe_head *sh = bi->bi_private;
418 raid5_conf_t *conf = sh->raid_conf;
419 int disks = conf->raid_disks, i;
420 unsigned long flags;
421 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
423 if (bi->bi_size)
424 return 1;
426 for (i=0 ; i<disks; i++)
427 if (bi == &sh->dev[i].req)
428 break;
430 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
431 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
432 uptodate);
433 if (i == disks) {
434 BUG();
435 return 0;
438 spin_lock_irqsave(&conf->device_lock, flags);
439 if (!uptodate)
440 md_error(conf->mddev, conf->disks[i].rdev);
442 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
444 clear_bit(R5_LOCKED, &sh->dev[i].flags);
445 set_bit(STRIPE_HANDLE, &sh->state);
446 __release_stripe(conf, sh);
447 spin_unlock_irqrestore(&conf->device_lock, flags);
448 return 0;
452 static sector_t compute_blocknr(struct stripe_head *sh, int i);
454 static void raid5_build_block (struct stripe_head *sh, int i)
456 struct r5dev *dev = &sh->dev[i];
458 bio_init(&dev->req);
459 dev->req.bi_io_vec = &dev->vec;
460 dev->req.bi_vcnt++;
461 dev->req.bi_max_vecs++;
462 dev->vec.bv_page = dev->page;
463 dev->vec.bv_len = STRIPE_SIZE;
464 dev->vec.bv_offset = 0;
466 dev->req.bi_sector = sh->sector;
467 dev->req.bi_private = sh;
469 dev->flags = 0;
470 if (i != sh->pd_idx)
471 dev->sector = compute_blocknr(sh, i);
474 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
476 char b[BDEVNAME_SIZE];
477 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
478 PRINTK("raid5: error called\n");
480 if (!rdev->faulty) {
481 mddev->sb_dirty = 1;
482 if (rdev->in_sync) {
483 conf->working_disks--;
484 mddev->degraded++;
485 conf->failed_disks++;
486 rdev->in_sync = 0;
488 * if recovery was running, make sure it aborts.
490 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
492 rdev->faulty = 1;
493 printk (KERN_ALERT
494 "raid5: Disk failure on %s, disabling device."
495 " Operation continuing on %d devices\n",
496 bdevname(rdev->bdev,b), conf->working_disks);
501 * Input: a 'big' sector number,
502 * Output: index of the data and parity disk, and the sector # in them.
504 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
505 unsigned int data_disks, unsigned int * dd_idx,
506 unsigned int * pd_idx, raid5_conf_t *conf)
508 long stripe;
509 unsigned long chunk_number;
510 unsigned int chunk_offset;
511 sector_t new_sector;
512 int sectors_per_chunk = conf->chunk_size >> 9;
514 /* First compute the information on this sector */
517 * Compute the chunk number and the sector offset inside the chunk
519 chunk_offset = sector_div(r_sector, sectors_per_chunk);
520 chunk_number = r_sector;
521 BUG_ON(r_sector != chunk_number);
524 * Compute the stripe number
526 stripe = chunk_number / data_disks;
529 * Compute the data disk and parity disk indexes inside the stripe
531 *dd_idx = chunk_number % data_disks;
534 * Select the parity disk based on the user selected algorithm.
536 if (conf->level == 4)
537 *pd_idx = data_disks;
538 else switch (conf->algorithm) {
539 case ALGORITHM_LEFT_ASYMMETRIC:
540 *pd_idx = data_disks - stripe % raid_disks;
541 if (*dd_idx >= *pd_idx)
542 (*dd_idx)++;
543 break;
544 case ALGORITHM_RIGHT_ASYMMETRIC:
545 *pd_idx = stripe % raid_disks;
546 if (*dd_idx >= *pd_idx)
547 (*dd_idx)++;
548 break;
549 case ALGORITHM_LEFT_SYMMETRIC:
550 *pd_idx = data_disks - stripe % raid_disks;
551 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
552 break;
553 case ALGORITHM_RIGHT_SYMMETRIC:
554 *pd_idx = stripe % raid_disks;
555 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
556 break;
557 default:
558 printk("raid5: unsupported algorithm %d\n",
559 conf->algorithm);
563 * Finally, compute the new sector number
565 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
566 return new_sector;
570 static sector_t compute_blocknr(struct stripe_head *sh, int i)
572 raid5_conf_t *conf = sh->raid_conf;
573 int raid_disks = conf->raid_disks, data_disks = raid_disks - 1;
574 sector_t new_sector = sh->sector, check;
575 int sectors_per_chunk = conf->chunk_size >> 9;
576 sector_t stripe;
577 int chunk_offset;
578 int chunk_number, dummy1, dummy2, dd_idx = i;
579 sector_t r_sector;
581 chunk_offset = sector_div(new_sector, sectors_per_chunk);
582 stripe = new_sector;
583 BUG_ON(new_sector != stripe);
586 switch (conf->algorithm) {
587 case ALGORITHM_LEFT_ASYMMETRIC:
588 case ALGORITHM_RIGHT_ASYMMETRIC:
589 if (i > sh->pd_idx)
590 i--;
591 break;
592 case ALGORITHM_LEFT_SYMMETRIC:
593 case ALGORITHM_RIGHT_SYMMETRIC:
594 if (i < sh->pd_idx)
595 i += raid_disks;
596 i -= (sh->pd_idx + 1);
597 break;
598 default:
599 printk("raid5: unsupported algorithm %d\n",
600 conf->algorithm);
603 chunk_number = stripe * data_disks + i;
604 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
606 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
607 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
608 printk("compute_blocknr: map not correct\n");
609 return 0;
611 return r_sector;
617 * Copy data between a page in the stripe cache, and a bio.
618 * There are no alignment or size guarantees between the page or the
619 * bio except that there is some overlap.
620 * All iovecs in the bio must be considered.
622 static void copy_data(int frombio, struct bio *bio,
623 struct page *page,
624 sector_t sector)
626 char *pa = page_address(page);
627 struct bio_vec *bvl;
628 int i;
629 int page_offset;
631 if (bio->bi_sector >= sector)
632 page_offset = (signed)(bio->bi_sector - sector) * 512;
633 else
634 page_offset = (signed)(sector - bio->bi_sector) * -512;
635 bio_for_each_segment(bvl, bio, i) {
636 int len = bio_iovec_idx(bio,i)->bv_len;
637 int clen;
638 int b_offset = 0;
640 if (page_offset < 0) {
641 b_offset = -page_offset;
642 page_offset += b_offset;
643 len -= b_offset;
646 if (len > 0 && page_offset + len > STRIPE_SIZE)
647 clen = STRIPE_SIZE - page_offset;
648 else clen = len;
650 if (clen > 0) {
651 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
652 if (frombio)
653 memcpy(pa+page_offset, ba+b_offset, clen);
654 else
655 memcpy(ba+b_offset, pa+page_offset, clen);
656 __bio_kunmap_atomic(ba, KM_USER0);
658 if (clen < len) /* hit end of page */
659 break;
660 page_offset += len;
664 #define check_xor() do { \
665 if (count == MAX_XOR_BLOCKS) { \
666 xor_block(count, STRIPE_SIZE, ptr); \
667 count = 1; \
669 } while(0)
672 static void compute_block(struct stripe_head *sh, int dd_idx)
674 raid5_conf_t *conf = sh->raid_conf;
675 int i, count, disks = conf->raid_disks;
676 void *ptr[MAX_XOR_BLOCKS], *p;
678 PRINTK("compute_block, stripe %llu, idx %d\n",
679 (unsigned long long)sh->sector, dd_idx);
681 ptr[0] = page_address(sh->dev[dd_idx].page);
682 memset(ptr[0], 0, STRIPE_SIZE);
683 count = 1;
684 for (i = disks ; i--; ) {
685 if (i == dd_idx)
686 continue;
687 p = page_address(sh->dev[i].page);
688 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
689 ptr[count++] = p;
690 else
691 printk("compute_block() %d, stripe %llu, %d"
692 " not present\n", dd_idx,
693 (unsigned long long)sh->sector, i);
695 check_xor();
697 if (count != 1)
698 xor_block(count, STRIPE_SIZE, ptr);
699 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
702 static void compute_parity(struct stripe_head *sh, int method)
704 raid5_conf_t *conf = sh->raid_conf;
705 int i, pd_idx = sh->pd_idx, disks = conf->raid_disks, count;
706 void *ptr[MAX_XOR_BLOCKS];
707 struct bio *chosen;
709 PRINTK("compute_parity, stripe %llu, method %d\n",
710 (unsigned long long)sh->sector, method);
712 count = 1;
713 ptr[0] = page_address(sh->dev[pd_idx].page);
714 switch(method) {
715 case READ_MODIFY_WRITE:
716 if (!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags))
717 BUG();
718 for (i=disks ; i-- ;) {
719 if (i==pd_idx)
720 continue;
721 if (sh->dev[i].towrite &&
722 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
723 ptr[count++] = page_address(sh->dev[i].page);
724 chosen = sh->dev[i].towrite;
725 sh->dev[i].towrite = NULL;
727 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
728 wake_up(&conf->wait_for_overlap);
730 if (sh->dev[i].written) BUG();
731 sh->dev[i].written = chosen;
732 check_xor();
735 break;
736 case RECONSTRUCT_WRITE:
737 memset(ptr[0], 0, STRIPE_SIZE);
738 for (i= disks; i-- ;)
739 if (i!=pd_idx && sh->dev[i].towrite) {
740 chosen = sh->dev[i].towrite;
741 sh->dev[i].towrite = NULL;
743 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
744 wake_up(&conf->wait_for_overlap);
746 if (sh->dev[i].written) BUG();
747 sh->dev[i].written = chosen;
749 break;
750 case CHECK_PARITY:
751 break;
753 if (count>1) {
754 xor_block(count, STRIPE_SIZE, ptr);
755 count = 1;
758 for (i = disks; i--;)
759 if (sh->dev[i].written) {
760 sector_t sector = sh->dev[i].sector;
761 struct bio *wbi = sh->dev[i].written;
762 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
763 copy_data(1, wbi, sh->dev[i].page, sector);
764 wbi = r5_next_bio(wbi, sector);
767 set_bit(R5_LOCKED, &sh->dev[i].flags);
768 set_bit(R5_UPTODATE, &sh->dev[i].flags);
771 switch(method) {
772 case RECONSTRUCT_WRITE:
773 case CHECK_PARITY:
774 for (i=disks; i--;)
775 if (i != pd_idx) {
776 ptr[count++] = page_address(sh->dev[i].page);
777 check_xor();
779 break;
780 case READ_MODIFY_WRITE:
781 for (i = disks; i--;)
782 if (sh->dev[i].written) {
783 ptr[count++] = page_address(sh->dev[i].page);
784 check_xor();
787 if (count != 1)
788 xor_block(count, STRIPE_SIZE, ptr);
790 if (method != CHECK_PARITY) {
791 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
792 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
793 } else
794 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
798 * Each stripe/dev can have one or more bion attached.
799 * toread/towrite point to the first in a chain.
800 * The bi_next chain must be in order.
802 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
804 struct bio **bip;
805 raid5_conf_t *conf = sh->raid_conf;
807 PRINTK("adding bh b#%llu to stripe s#%llu\n",
808 (unsigned long long)bi->bi_sector,
809 (unsigned long long)sh->sector);
812 spin_lock(&sh->lock);
813 spin_lock_irq(&conf->device_lock);
814 if (forwrite)
815 bip = &sh->dev[dd_idx].towrite;
816 else
817 bip = &sh->dev[dd_idx].toread;
818 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
819 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
820 goto overlap;
821 bip = & (*bip)->bi_next;
823 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
824 goto overlap;
826 if (*bip && bi->bi_next && (*bip) != bi->bi_next)
827 BUG();
828 if (*bip)
829 bi->bi_next = *bip;
830 *bip = bi;
831 bi->bi_phys_segments ++;
832 spin_unlock_irq(&conf->device_lock);
833 spin_unlock(&sh->lock);
835 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
836 (unsigned long long)bi->bi_sector,
837 (unsigned long long)sh->sector, dd_idx);
839 if (forwrite) {
840 /* check if page is covered */
841 sector_t sector = sh->dev[dd_idx].sector;
842 for (bi=sh->dev[dd_idx].towrite;
843 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
844 bi && bi->bi_sector <= sector;
845 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
846 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
847 sector = bi->bi_sector + (bi->bi_size>>9);
849 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
850 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
852 return 1;
854 overlap:
855 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
856 spin_unlock_irq(&conf->device_lock);
857 spin_unlock(&sh->lock);
858 return 0;
863 * handle_stripe - do things to a stripe.
865 * We lock the stripe and then examine the state of various bits
866 * to see what needs to be done.
867 * Possible results:
868 * return some read request which now have data
869 * return some write requests which are safely on disc
870 * schedule a read on some buffers
871 * schedule a write of some buffers
872 * return confirmation of parity correctness
874 * Parity calculations are done inside the stripe lock
875 * buffers are taken off read_list or write_list, and bh_cache buffers
876 * get BH_Lock set before the stripe lock is released.
880 static void handle_stripe(struct stripe_head *sh)
882 raid5_conf_t *conf = sh->raid_conf;
883 int disks = conf->raid_disks;
884 struct bio *return_bi= NULL;
885 struct bio *bi;
886 int i;
887 int syncing;
888 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
889 int non_overwrite = 0;
890 int failed_num=0;
891 struct r5dev *dev;
893 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
894 (unsigned long long)sh->sector, atomic_read(&sh->count),
895 sh->pd_idx);
897 spin_lock(&sh->lock);
898 clear_bit(STRIPE_HANDLE, &sh->state);
899 clear_bit(STRIPE_DELAYED, &sh->state);
901 syncing = test_bit(STRIPE_SYNCING, &sh->state);
902 /* Now to look around and see what can be done */
904 for (i=disks; i--; ) {
905 mdk_rdev_t *rdev;
906 dev = &sh->dev[i];
907 clear_bit(R5_Insync, &dev->flags);
908 clear_bit(R5_Syncio, &dev->flags);
910 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
911 i, dev->flags, dev->toread, dev->towrite, dev->written);
912 /* maybe we can reply to a read */
913 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
914 struct bio *rbi, *rbi2;
915 PRINTK("Return read for disc %d\n", i);
916 spin_lock_irq(&conf->device_lock);
917 rbi = dev->toread;
918 dev->toread = NULL;
919 if (test_and_clear_bit(R5_Overlap, &dev->flags))
920 wake_up(&conf->wait_for_overlap);
921 spin_unlock_irq(&conf->device_lock);
922 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
923 copy_data(0, rbi, dev->page, dev->sector);
924 rbi2 = r5_next_bio(rbi, dev->sector);
925 spin_lock_irq(&conf->device_lock);
926 if (--rbi->bi_phys_segments == 0) {
927 rbi->bi_next = return_bi;
928 return_bi = rbi;
930 spin_unlock_irq(&conf->device_lock);
931 rbi = rbi2;
935 /* now count some things */
936 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
937 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
940 if (dev->toread) to_read++;
941 if (dev->towrite) {
942 to_write++;
943 if (!test_bit(R5_OVERWRITE, &dev->flags))
944 non_overwrite++;
946 if (dev->written) written++;
947 rdev = conf->disks[i].rdev; /* FIXME, should I be looking rdev */
948 if (!rdev || !rdev->in_sync) {
949 failed++;
950 failed_num = i;
951 } else
952 set_bit(R5_Insync, &dev->flags);
954 PRINTK("locked=%d uptodate=%d to_read=%d"
955 " to_write=%d failed=%d failed_num=%d\n",
956 locked, uptodate, to_read, to_write, failed, failed_num);
957 /* check if the array has lost two devices and, if so, some requests might
958 * need to be failed
960 if (failed > 1 && to_read+to_write+written) {
961 spin_lock_irq(&conf->device_lock);
962 for (i=disks; i--; ) {
963 /* fail all writes first */
964 bi = sh->dev[i].towrite;
965 sh->dev[i].towrite = NULL;
966 if (bi) to_write--;
968 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
969 wake_up(&conf->wait_for_overlap);
971 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
972 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
973 clear_bit(BIO_UPTODATE, &bi->bi_flags);
974 if (--bi->bi_phys_segments == 0) {
975 md_write_end(conf->mddev);
976 bi->bi_next = return_bi;
977 return_bi = bi;
979 bi = nextbi;
981 /* and fail all 'written' */
982 bi = sh->dev[i].written;
983 sh->dev[i].written = NULL;
984 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
985 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
986 clear_bit(BIO_UPTODATE, &bi->bi_flags);
987 if (--bi->bi_phys_segments == 0) {
988 md_write_end(conf->mddev);
989 bi->bi_next = return_bi;
990 return_bi = bi;
992 bi = bi2;
995 /* fail any reads if this device is non-operational */
996 if (!test_bit(R5_Insync, &sh->dev[i].flags)) {
997 bi = sh->dev[i].toread;
998 sh->dev[i].toread = NULL;
999 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1000 wake_up(&conf->wait_for_overlap);
1001 if (bi) to_read--;
1002 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1003 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1004 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1005 if (--bi->bi_phys_segments == 0) {
1006 bi->bi_next = return_bi;
1007 return_bi = bi;
1009 bi = nextbi;
1013 spin_unlock_irq(&conf->device_lock);
1015 if (failed > 1 && syncing) {
1016 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1017 clear_bit(STRIPE_SYNCING, &sh->state);
1018 syncing = 0;
1021 /* might be able to return some write requests if the parity block
1022 * is safe, or on a failed drive
1024 dev = &sh->dev[sh->pd_idx];
1025 if ( written &&
1026 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1027 test_bit(R5_UPTODATE, &dev->flags))
1028 || (failed == 1 && failed_num == sh->pd_idx))
1030 /* any written block on an uptodate or failed drive can be returned.
1031 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1032 * never LOCKED, so we don't need to test 'failed' directly.
1034 for (i=disks; i--; )
1035 if (sh->dev[i].written) {
1036 dev = &sh->dev[i];
1037 if (!test_bit(R5_LOCKED, &dev->flags) &&
1038 test_bit(R5_UPTODATE, &dev->flags) ) {
1039 /* We can return any write requests */
1040 struct bio *wbi, *wbi2;
1041 PRINTK("Return write for disc %d\n", i);
1042 spin_lock_irq(&conf->device_lock);
1043 wbi = dev->written;
1044 dev->written = NULL;
1045 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1046 wbi2 = r5_next_bio(wbi, dev->sector);
1047 if (--wbi->bi_phys_segments == 0) {
1048 md_write_end(conf->mddev);
1049 wbi->bi_next = return_bi;
1050 return_bi = wbi;
1052 wbi = wbi2;
1054 spin_unlock_irq(&conf->device_lock);
1059 /* Now we might consider reading some blocks, either to check/generate
1060 * parity, or to satisfy requests
1061 * or to load a block that is being partially written.
1063 if (to_read || non_overwrite || (syncing && (uptodate < disks))) {
1064 for (i=disks; i--;) {
1065 dev = &sh->dev[i];
1066 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1067 (dev->toread ||
1068 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1069 syncing ||
1070 (failed && (sh->dev[failed_num].toread ||
1071 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1074 /* we would like to get this block, possibly
1075 * by computing it, but we might not be able to
1077 if (uptodate == disks-1) {
1078 PRINTK("Computing block %d\n", i);
1079 compute_block(sh, i);
1080 uptodate++;
1081 } else if (test_bit(R5_Insync, &dev->flags)) {
1082 set_bit(R5_LOCKED, &dev->flags);
1083 set_bit(R5_Wantread, &dev->flags);
1084 #if 0
1085 /* if I am just reading this block and we don't have
1086 a failed drive, or any pending writes then sidestep the cache */
1087 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1088 ! syncing && !failed && !to_write) {
1089 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1090 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1092 #endif
1093 locked++;
1094 PRINTK("Reading block %d (sync=%d)\n",
1095 i, syncing);
1096 if (syncing)
1097 md_sync_acct(conf->disks[i].rdev->bdev,
1098 STRIPE_SECTORS);
1102 set_bit(STRIPE_HANDLE, &sh->state);
1105 /* now to consider writing and what else, if anything should be read */
1106 if (to_write) {
1107 int rmw=0, rcw=0;
1108 for (i=disks ; i--;) {
1109 /* would I have to read this buffer for read_modify_write */
1110 dev = &sh->dev[i];
1111 if ((dev->towrite || i == sh->pd_idx) &&
1112 (!test_bit(R5_LOCKED, &dev->flags)
1113 #if 0
1114 || sh->bh_page[i]!=bh->b_page
1115 #endif
1116 ) &&
1117 !test_bit(R5_UPTODATE, &dev->flags)) {
1118 if (test_bit(R5_Insync, &dev->flags)
1119 /* && !(!mddev->insync && i == sh->pd_idx) */
1121 rmw++;
1122 else rmw += 2*disks; /* cannot read it */
1124 /* Would I have to read this buffer for reconstruct_write */
1125 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1126 (!test_bit(R5_LOCKED, &dev->flags)
1127 #if 0
1128 || sh->bh_page[i] != bh->b_page
1129 #endif
1130 ) &&
1131 !test_bit(R5_UPTODATE, &dev->flags)) {
1132 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1133 else rcw += 2*disks;
1136 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1137 (unsigned long long)sh->sector, rmw, rcw);
1138 set_bit(STRIPE_HANDLE, &sh->state);
1139 if (rmw < rcw && rmw > 0)
1140 /* prefer read-modify-write, but need to get some data */
1141 for (i=disks; i--;) {
1142 dev = &sh->dev[i];
1143 if ((dev->towrite || i == sh->pd_idx) &&
1144 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1145 test_bit(R5_Insync, &dev->flags)) {
1146 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1148 PRINTK("Read_old block %d for r-m-w\n", i);
1149 set_bit(R5_LOCKED, &dev->flags);
1150 set_bit(R5_Wantread, &dev->flags);
1151 locked++;
1152 } else {
1153 set_bit(STRIPE_DELAYED, &sh->state);
1154 set_bit(STRIPE_HANDLE, &sh->state);
1158 if (rcw <= rmw && rcw > 0)
1159 /* want reconstruct write, but need to get some data */
1160 for (i=disks; i--;) {
1161 dev = &sh->dev[i];
1162 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1163 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1164 test_bit(R5_Insync, &dev->flags)) {
1165 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1167 PRINTK("Read_old block %d for Reconstruct\n", i);
1168 set_bit(R5_LOCKED, &dev->flags);
1169 set_bit(R5_Wantread, &dev->flags);
1170 locked++;
1171 } else {
1172 set_bit(STRIPE_DELAYED, &sh->state);
1173 set_bit(STRIPE_HANDLE, &sh->state);
1177 /* now if nothing is locked, and if we have enough data, we can start a write request */
1178 if (locked == 0 && (rcw == 0 ||rmw == 0)) {
1179 PRINTK("Computing parity...\n");
1180 compute_parity(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1181 /* now every locked buffer is ready to be written */
1182 for (i=disks; i--;)
1183 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1184 PRINTK("Writing block %d\n", i);
1185 locked++;
1186 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1187 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1188 || (i==sh->pd_idx && failed == 0))
1189 set_bit(STRIPE_INSYNC, &sh->state);
1191 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1192 atomic_dec(&conf->preread_active_stripes);
1193 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1194 md_wakeup_thread(conf->mddev->thread);
1199 /* maybe we need to check and possibly fix the parity for this stripe
1200 * Any reads will already have been scheduled, so we just see if enough data
1201 * is available
1203 if (syncing && locked == 0 &&
1204 !test_bit(STRIPE_INSYNC, &sh->state) && failed <= 1) {
1205 set_bit(STRIPE_HANDLE, &sh->state);
1206 if (failed == 0) {
1207 char *pagea;
1208 if (uptodate != disks)
1209 BUG();
1210 compute_parity(sh, CHECK_PARITY);
1211 uptodate--;
1212 pagea = page_address(sh->dev[sh->pd_idx].page);
1213 if ((*(u32*)pagea) == 0 &&
1214 !memcmp(pagea, pagea+4, STRIPE_SIZE-4)) {
1215 /* parity is correct (on disc, not in buffer any more) */
1216 set_bit(STRIPE_INSYNC, &sh->state);
1219 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1220 if (failed==0)
1221 failed_num = sh->pd_idx;
1222 /* should be able to compute the missing block and write it to spare */
1223 if (!test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)) {
1224 if (uptodate+1 != disks)
1225 BUG();
1226 compute_block(sh, failed_num);
1227 uptodate++;
1229 if (uptodate != disks)
1230 BUG();
1231 dev = &sh->dev[failed_num];
1232 set_bit(R5_LOCKED, &dev->flags);
1233 set_bit(R5_Wantwrite, &dev->flags);
1234 locked++;
1235 set_bit(STRIPE_INSYNC, &sh->state);
1236 set_bit(R5_Syncio, &dev->flags);
1239 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1240 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1241 clear_bit(STRIPE_SYNCING, &sh->state);
1244 spin_unlock(&sh->lock);
1246 while ((bi=return_bi)) {
1247 int bytes = bi->bi_size;
1249 return_bi = bi->bi_next;
1250 bi->bi_next = NULL;
1251 bi->bi_size = 0;
1252 bi->bi_end_io(bi, bytes, 0);
1254 for (i=disks; i-- ;) {
1255 int rw;
1256 struct bio *bi;
1257 mdk_rdev_t *rdev;
1258 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1259 rw = 1;
1260 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1261 rw = 0;
1262 else
1263 continue;
1265 bi = &sh->dev[i].req;
1267 bi->bi_rw = rw;
1268 if (rw)
1269 bi->bi_end_io = raid5_end_write_request;
1270 else
1271 bi->bi_end_io = raid5_end_read_request;
1273 rcu_read_lock();
1274 rdev = conf->disks[i].rdev;
1275 if (rdev && rdev->faulty)
1276 rdev = NULL;
1277 if (rdev)
1278 atomic_inc(&rdev->nr_pending);
1279 rcu_read_unlock();
1281 if (rdev) {
1282 if (test_bit(R5_Syncio, &sh->dev[i].flags))
1283 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1285 bi->bi_bdev = rdev->bdev;
1286 PRINTK("for %llu schedule op %ld on disc %d\n",
1287 (unsigned long long)sh->sector, bi->bi_rw, i);
1288 atomic_inc(&sh->count);
1289 bi->bi_sector = sh->sector + rdev->data_offset;
1290 bi->bi_flags = 1 << BIO_UPTODATE;
1291 bi->bi_vcnt = 1;
1292 bi->bi_max_vecs = 1;
1293 bi->bi_idx = 0;
1294 bi->bi_io_vec = &sh->dev[i].vec;
1295 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1296 bi->bi_io_vec[0].bv_offset = 0;
1297 bi->bi_size = STRIPE_SIZE;
1298 bi->bi_next = NULL;
1299 generic_make_request(bi);
1300 } else {
1301 PRINTK("skip op %ld on disc %d for sector %llu\n",
1302 bi->bi_rw, i, (unsigned long long)sh->sector);
1303 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1304 set_bit(STRIPE_HANDLE, &sh->state);
1309 static inline void raid5_activate_delayed(raid5_conf_t *conf)
1311 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
1312 while (!list_empty(&conf->delayed_list)) {
1313 struct list_head *l = conf->delayed_list.next;
1314 struct stripe_head *sh;
1315 sh = list_entry(l, struct stripe_head, lru);
1316 list_del_init(l);
1317 clear_bit(STRIPE_DELAYED, &sh->state);
1318 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1319 atomic_inc(&conf->preread_active_stripes);
1320 list_add_tail(&sh->lru, &conf->handle_list);
1325 static void unplug_slaves(mddev_t *mddev)
1327 raid5_conf_t *conf = mddev_to_conf(mddev);
1328 int i;
1330 rcu_read_lock();
1331 for (i=0; i<mddev->raid_disks; i++) {
1332 mdk_rdev_t *rdev = conf->disks[i].rdev;
1333 if (rdev && !rdev->faulty && atomic_read(&rdev->nr_pending)) {
1334 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
1336 atomic_inc(&rdev->nr_pending);
1337 rcu_read_unlock();
1339 if (r_queue->unplug_fn)
1340 r_queue->unplug_fn(r_queue);
1342 rdev_dec_pending(rdev, mddev);
1343 rcu_read_lock();
1346 rcu_read_unlock();
1349 static void raid5_unplug_device(request_queue_t *q)
1351 mddev_t *mddev = q->queuedata;
1352 raid5_conf_t *conf = mddev_to_conf(mddev);
1353 unsigned long flags;
1355 spin_lock_irqsave(&conf->device_lock, flags);
1357 if (blk_remove_plug(q))
1358 raid5_activate_delayed(conf);
1359 md_wakeup_thread(mddev->thread);
1361 spin_unlock_irqrestore(&conf->device_lock, flags);
1363 unplug_slaves(mddev);
1366 static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
1367 sector_t *error_sector)
1369 mddev_t *mddev = q->queuedata;
1370 raid5_conf_t *conf = mddev_to_conf(mddev);
1371 int i, ret = 0;
1373 rcu_read_lock();
1374 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
1375 mdk_rdev_t *rdev = conf->disks[i].rdev;
1376 if (rdev && !rdev->faulty) {
1377 struct block_device *bdev = rdev->bdev;
1378 request_queue_t *r_queue = bdev_get_queue(bdev);
1380 if (!r_queue->issue_flush_fn)
1381 ret = -EOPNOTSUPP;
1382 else {
1383 atomic_inc(&rdev->nr_pending);
1384 rcu_read_unlock();
1385 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
1386 error_sector);
1387 rdev_dec_pending(rdev, mddev);
1388 rcu_read_lock();
1392 rcu_read_unlock();
1393 return ret;
1396 static inline void raid5_plug_device(raid5_conf_t *conf)
1398 spin_lock_irq(&conf->device_lock);
1399 blk_plug_device(conf->mddev->queue);
1400 spin_unlock_irq(&conf->device_lock);
1403 static int make_request (request_queue_t *q, struct bio * bi)
1405 mddev_t *mddev = q->queuedata;
1406 raid5_conf_t *conf = mddev_to_conf(mddev);
1407 const unsigned int raid_disks = conf->raid_disks;
1408 const unsigned int data_disks = raid_disks - 1;
1409 unsigned int dd_idx, pd_idx;
1410 sector_t new_sector;
1411 sector_t logical_sector, last_sector;
1412 struct stripe_head *sh;
1414 if (bio_data_dir(bi)==WRITE) {
1415 disk_stat_inc(mddev->gendisk, writes);
1416 disk_stat_add(mddev->gendisk, write_sectors, bio_sectors(bi));
1417 } else {
1418 disk_stat_inc(mddev->gendisk, reads);
1419 disk_stat_add(mddev->gendisk, read_sectors, bio_sectors(bi));
1422 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
1423 last_sector = bi->bi_sector + (bi->bi_size>>9);
1424 bi->bi_next = NULL;
1425 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
1426 if ( bio_data_dir(bi) == WRITE )
1427 md_write_start(mddev);
1428 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
1429 DEFINE_WAIT(w);
1431 new_sector = raid5_compute_sector(logical_sector,
1432 raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1434 PRINTK("raid5: make_request, sector %llu logical %llu\n",
1435 (unsigned long long)new_sector,
1436 (unsigned long long)logical_sector);
1438 retry:
1439 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
1440 sh = get_active_stripe(conf, new_sector, pd_idx, (bi->bi_rw&RWA_MASK));
1441 if (sh) {
1442 if (!add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
1443 /* Add failed due to overlap. Flush everything
1444 * and wait a while
1446 raid5_unplug_device(mddev->queue);
1447 release_stripe(sh);
1448 schedule();
1449 goto retry;
1451 finish_wait(&conf->wait_for_overlap, &w);
1452 raid5_plug_device(conf);
1453 handle_stripe(sh);
1454 release_stripe(sh);
1456 } else {
1457 /* cannot get stripe for read-ahead, just give-up */
1458 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1459 finish_wait(&conf->wait_for_overlap, &w);
1460 break;
1464 spin_lock_irq(&conf->device_lock);
1465 if (--bi->bi_phys_segments == 0) {
1466 int bytes = bi->bi_size;
1468 if ( bio_data_dir(bi) == WRITE )
1469 md_write_end(mddev);
1470 bi->bi_size = 0;
1471 bi->bi_end_io(bi, bytes, 0);
1473 spin_unlock_irq(&conf->device_lock);
1474 return 0;
1477 /* FIXME go_faster isn't used */
1478 static int sync_request (mddev_t *mddev, sector_t sector_nr, int go_faster)
1480 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1481 struct stripe_head *sh;
1482 int sectors_per_chunk = conf->chunk_size >> 9;
1483 sector_t x;
1484 unsigned long stripe;
1485 int chunk_offset;
1486 int dd_idx, pd_idx;
1487 sector_t first_sector;
1488 int raid_disks = conf->raid_disks;
1489 int data_disks = raid_disks-1;
1491 if (sector_nr >= mddev->size <<1) {
1492 /* just being told to finish up .. nothing much to do */
1493 unplug_slaves(mddev);
1494 return 0;
1496 /* if there is 1 or more failed drives and we are trying
1497 * to resync, then assert that we are finished, because there is
1498 * nothing we can do.
1500 if (mddev->degraded >= 1 && test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
1501 int rv = (mddev->size << 1) - sector_nr;
1502 md_done_sync(mddev, rv, 1);
1503 return rv;
1506 x = sector_nr;
1507 chunk_offset = sector_div(x, sectors_per_chunk);
1508 stripe = x;
1509 BUG_ON(x != stripe);
1511 first_sector = raid5_compute_sector((sector_t)stripe*data_disks*sectors_per_chunk
1512 + chunk_offset, raid_disks, data_disks, &dd_idx, &pd_idx, conf);
1513 sh = get_active_stripe(conf, sector_nr, pd_idx, 1);
1514 if (sh == NULL) {
1515 sh = get_active_stripe(conf, sector_nr, pd_idx, 0);
1516 /* make sure we don't swamp the stripe cache if someone else
1517 * is trying to get access
1519 set_current_state(TASK_UNINTERRUPTIBLE);
1520 schedule_timeout(1);
1522 spin_lock(&sh->lock);
1523 set_bit(STRIPE_SYNCING, &sh->state);
1524 clear_bit(STRIPE_INSYNC, &sh->state);
1525 spin_unlock(&sh->lock);
1527 handle_stripe(sh);
1528 release_stripe(sh);
1530 return STRIPE_SECTORS;
1534 * This is our raid5 kernel thread.
1536 * We scan the hash table for stripes which can be handled now.
1537 * During the scan, completed stripes are saved for us by the interrupt
1538 * handler, so that they will not have to wait for our next wakeup.
1540 static void raid5d (mddev_t *mddev)
1542 struct stripe_head *sh;
1543 raid5_conf_t *conf = mddev_to_conf(mddev);
1544 int handled;
1546 PRINTK("+++ raid5d active\n");
1548 md_check_recovery(mddev);
1549 md_handle_safemode(mddev);
1551 handled = 0;
1552 spin_lock_irq(&conf->device_lock);
1553 while (1) {
1554 struct list_head *first;
1556 if (list_empty(&conf->handle_list) &&
1557 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
1558 !blk_queue_plugged(mddev->queue) &&
1559 !list_empty(&conf->delayed_list))
1560 raid5_activate_delayed(conf);
1562 if (list_empty(&conf->handle_list))
1563 break;
1565 first = conf->handle_list.next;
1566 sh = list_entry(first, struct stripe_head, lru);
1568 list_del_init(first);
1569 atomic_inc(&sh->count);
1570 if (atomic_read(&sh->count)!= 1)
1571 BUG();
1572 spin_unlock_irq(&conf->device_lock);
1574 handled++;
1575 handle_stripe(sh);
1576 release_stripe(sh);
1578 spin_lock_irq(&conf->device_lock);
1580 PRINTK("%d stripes handled\n", handled);
1582 spin_unlock_irq(&conf->device_lock);
1584 unplug_slaves(mddev);
1586 PRINTK("--- raid5d inactive\n");
1589 static int run (mddev_t *mddev)
1591 raid5_conf_t *conf;
1592 int raid_disk, memory;
1593 mdk_rdev_t *rdev;
1594 struct disk_info *disk;
1595 struct list_head *tmp;
1597 if (mddev->level != 5 && mddev->level != 4) {
1598 printk("raid5: %s: raid level not set to 4/5 (%d)\n", mdname(mddev), mddev->level);
1599 return -EIO;
1602 mddev->private = kmalloc (sizeof (raid5_conf_t)
1603 + mddev->raid_disks * sizeof(struct disk_info),
1604 GFP_KERNEL);
1605 if ((conf = mddev->private) == NULL)
1606 goto abort;
1607 memset (conf, 0, sizeof (*conf) + mddev->raid_disks * sizeof(struct disk_info) );
1608 conf->mddev = mddev;
1610 if ((conf->stripe_hashtbl = (struct stripe_head **) __get_free_pages(GFP_ATOMIC, HASH_PAGES_ORDER)) == NULL)
1611 goto abort;
1612 memset(conf->stripe_hashtbl, 0, HASH_PAGES * PAGE_SIZE);
1614 spin_lock_init(&conf->device_lock);
1615 init_waitqueue_head(&conf->wait_for_stripe);
1616 init_waitqueue_head(&conf->wait_for_overlap);
1617 INIT_LIST_HEAD(&conf->handle_list);
1618 INIT_LIST_HEAD(&conf->delayed_list);
1619 INIT_LIST_HEAD(&conf->inactive_list);
1620 atomic_set(&conf->active_stripes, 0);
1621 atomic_set(&conf->preread_active_stripes, 0);
1623 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
1625 ITERATE_RDEV(mddev,rdev,tmp) {
1626 raid_disk = rdev->raid_disk;
1627 if (raid_disk >= mddev->raid_disks
1628 || raid_disk < 0)
1629 continue;
1630 disk = conf->disks + raid_disk;
1632 disk->rdev = rdev;
1634 if (rdev->in_sync) {
1635 char b[BDEVNAME_SIZE];
1636 printk(KERN_INFO "raid5: device %s operational as raid"
1637 " disk %d\n", bdevname(rdev->bdev,b),
1638 raid_disk);
1639 conf->working_disks++;
1643 conf->raid_disks = mddev->raid_disks;
1645 * 0 for a fully functional array, 1 for a degraded array.
1647 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
1648 conf->mddev = mddev;
1649 conf->chunk_size = mddev->chunk_size;
1650 conf->level = mddev->level;
1651 conf->algorithm = mddev->layout;
1652 conf->max_nr_stripes = NR_STRIPES;
1654 /* device size must be a multiple of chunk size */
1655 mddev->size &= ~(mddev->chunk_size/1024 -1);
1657 if (!conf->chunk_size || conf->chunk_size % 4) {
1658 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
1659 conf->chunk_size, mdname(mddev));
1660 goto abort;
1662 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
1663 printk(KERN_ERR
1664 "raid5: unsupported parity algorithm %d for %s\n",
1665 conf->algorithm, mdname(mddev));
1666 goto abort;
1668 if (mddev->degraded > 1) {
1669 printk(KERN_ERR "raid5: not enough operational devices for %s"
1670 " (%d/%d failed)\n",
1671 mdname(mddev), conf->failed_disks, conf->raid_disks);
1672 goto abort;
1675 if (mddev->degraded == 1 &&
1676 mddev->recovery_cp != MaxSector) {
1677 printk(KERN_ERR
1678 "raid5: cannot start dirty degraded array for %s\n",
1679 mdname(mddev));
1680 goto abort;
1684 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
1685 if (!mddev->thread) {
1686 printk(KERN_ERR
1687 "raid5: couldn't allocate thread for %s\n",
1688 mdname(mddev));
1689 goto abort;
1692 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
1693 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
1694 if (grow_stripes(conf, conf->max_nr_stripes)) {
1695 printk(KERN_ERR
1696 "raid5: couldn't allocate %dkB for buffers\n", memory);
1697 shrink_stripes(conf);
1698 md_unregister_thread(mddev->thread);
1699 goto abort;
1700 } else
1701 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
1702 memory, mdname(mddev));
1704 if (mddev->degraded == 0)
1705 printk("raid5: raid level %d set %s active with %d out of %d"
1706 " devices, algorithm %d\n", conf->level, mdname(mddev),
1707 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
1708 conf->algorithm);
1709 else
1710 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
1711 " out of %d devices, algorithm %d\n", conf->level,
1712 mdname(mddev), mddev->raid_disks - mddev->degraded,
1713 mddev->raid_disks, conf->algorithm);
1715 print_raid5_conf(conf);
1717 /* read-ahead size must cover two whole stripes, which is
1718 * 2 * (n-1) * chunksize where 'n' is the number of raid devices
1721 int stripe = (mddev->raid_disks-1) * mddev->chunk_size
1722 / PAGE_CACHE_SIZE;
1723 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
1724 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
1727 /* Ok, everything is just fine now */
1729 mddev->queue->unplug_fn = raid5_unplug_device;
1730 mddev->queue->issue_flush_fn = raid5_issue_flush;
1732 mddev->array_size = mddev->size * (mddev->raid_disks - 1);
1733 return 0;
1734 abort:
1735 if (conf) {
1736 print_raid5_conf(conf);
1737 if (conf->stripe_hashtbl)
1738 free_pages((unsigned long) conf->stripe_hashtbl,
1739 HASH_PAGES_ORDER);
1740 kfree(conf);
1742 mddev->private = NULL;
1743 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
1744 return -EIO;
1749 static int stop (mddev_t *mddev)
1751 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1753 md_unregister_thread(mddev->thread);
1754 mddev->thread = NULL;
1755 shrink_stripes(conf);
1756 free_pages((unsigned long) conf->stripe_hashtbl, HASH_PAGES_ORDER);
1757 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
1758 kfree(conf);
1759 mddev->private = NULL;
1760 return 0;
1763 #if RAID5_DEBUG
1764 static void print_sh (struct stripe_head *sh)
1766 int i;
1768 printk("sh %llu, pd_idx %d, state %ld.\n",
1769 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
1770 printk("sh %llu, count %d.\n",
1771 (unsigned long long)sh->sector, atomic_read(&sh->count));
1772 printk("sh %llu, ", (unsigned long long)sh->sector);
1773 for (i = 0; i < sh->raid_conf->raid_disks; i++) {
1774 printk("(cache%d: %p %ld) ",
1775 i, sh->dev[i].page, sh->dev[i].flags);
1777 printk("\n");
1780 static void printall (raid5_conf_t *conf)
1782 struct stripe_head *sh;
1783 int i;
1785 spin_lock_irq(&conf->device_lock);
1786 for (i = 0; i < NR_HASH; i++) {
1787 sh = conf->stripe_hashtbl[i];
1788 for (; sh; sh = sh->hash_next) {
1789 if (sh->raid_conf != conf)
1790 continue;
1791 print_sh(sh);
1794 spin_unlock_irq(&conf->device_lock);
1796 #endif
1798 static void status (struct seq_file *seq, mddev_t *mddev)
1800 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
1801 int i;
1803 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
1804 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
1805 for (i = 0; i < conf->raid_disks; i++)
1806 seq_printf (seq, "%s",
1807 conf->disks[i].rdev &&
1808 conf->disks[i].rdev->in_sync ? "U" : "_");
1809 seq_printf (seq, "]");
1810 #if RAID5_DEBUG
1811 #define D(x) \
1812 seq_printf (seq, "<"#x":%d>", atomic_read(&conf->x))
1813 printall(conf);
1814 #endif
1817 static void print_raid5_conf (raid5_conf_t *conf)
1819 int i;
1820 struct disk_info *tmp;
1822 printk("RAID5 conf printout:\n");
1823 if (!conf) {
1824 printk("(conf==NULL)\n");
1825 return;
1827 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
1828 conf->working_disks, conf->failed_disks);
1830 for (i = 0; i < conf->raid_disks; i++) {
1831 char b[BDEVNAME_SIZE];
1832 tmp = conf->disks + i;
1833 if (tmp->rdev)
1834 printk(" disk %d, o:%d, dev:%s\n",
1835 i, !tmp->rdev->faulty,
1836 bdevname(tmp->rdev->bdev,b));
1840 static int raid5_spare_active(mddev_t *mddev)
1842 int i;
1843 raid5_conf_t *conf = mddev->private;
1844 struct disk_info *tmp;
1846 for (i = 0; i < conf->raid_disks; i++) {
1847 tmp = conf->disks + i;
1848 if (tmp->rdev
1849 && !tmp->rdev->faulty
1850 && !tmp->rdev->in_sync) {
1851 mddev->degraded--;
1852 conf->failed_disks--;
1853 conf->working_disks++;
1854 tmp->rdev->in_sync = 1;
1857 print_raid5_conf(conf);
1858 return 0;
1861 static int raid5_remove_disk(mddev_t *mddev, int number)
1863 raid5_conf_t *conf = mddev->private;
1864 int err = 0;
1865 mdk_rdev_t *rdev;
1866 struct disk_info *p = conf->disks + number;
1868 print_raid5_conf(conf);
1869 rdev = p->rdev;
1870 if (rdev) {
1871 if (rdev->in_sync ||
1872 atomic_read(&rdev->nr_pending)) {
1873 err = -EBUSY;
1874 goto abort;
1876 p->rdev = NULL;
1877 synchronize_rcu();
1878 if (atomic_read(&rdev->nr_pending)) {
1879 /* lost the race, try later */
1880 err = -EBUSY;
1881 p->rdev = rdev;
1884 abort:
1886 print_raid5_conf(conf);
1887 return err;
1890 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
1892 raid5_conf_t *conf = mddev->private;
1893 int found = 0;
1894 int disk;
1895 struct disk_info *p;
1897 if (mddev->degraded > 1)
1898 /* no point adding a device */
1899 return 0;
1902 * find the disk ...
1904 for (disk=0; disk < mddev->raid_disks; disk++)
1905 if ((p=conf->disks + disk)->rdev == NULL) {
1906 rdev->in_sync = 0;
1907 rdev->raid_disk = disk;
1908 found = 1;
1909 p->rdev = rdev;
1910 break;
1912 print_raid5_conf(conf);
1913 return found;
1916 static int raid5_resize(mddev_t *mddev, sector_t sectors)
1918 /* no resync is happening, and there is enough space
1919 * on all devices, so we can resize.
1920 * We need to make sure resync covers any new space.
1921 * If the array is shrinking we should possibly wait until
1922 * any io in the removed space completes, but it hardly seems
1923 * worth it.
1925 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
1926 mddev->array_size = (sectors * (mddev->raid_disks-1))>>1;
1927 set_capacity(mddev->gendisk, mddev->array_size << 1);
1928 mddev->changed = 1;
1929 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
1930 mddev->recovery_cp = mddev->size << 1;
1931 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
1933 mddev->size = sectors /2;
1934 return 0;
1937 static mdk_personality_t raid5_personality=
1939 .name = "raid5",
1940 .owner = THIS_MODULE,
1941 .make_request = make_request,
1942 .run = run,
1943 .stop = stop,
1944 .status = status,
1945 .error_handler = error,
1946 .hot_add_disk = raid5_add_disk,
1947 .hot_remove_disk= raid5_remove_disk,
1948 .spare_active = raid5_spare_active,
1949 .sync_request = sync_request,
1950 .resize = raid5_resize,
1953 static int __init raid5_init (void)
1955 return register_md_personality (RAID5, &raid5_personality);
1958 static void raid5_exit (void)
1960 unregister_md_personality (RAID5);
1963 module_init(raid5_init);
1964 module_exit(raid5_exit);
1965 MODULE_LICENSE("GPL");
1966 MODULE_ALIAS("md-personality-4"); /* RAID5 */