[PATCH] md: fix a plug/unplug race in raid5
[linux-2.6/suspend2-2.6.18.git] / drivers / md / raid5.c
blob6ba39408212967341ab370e32f154fae2d1a5753
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
5 * Copyright (C) 2002, 2003 H. Peter Anvin
7 * RAID-4/5/6 management functions.
8 * Thanks to Penguin Computing for making the RAID-6 development possible
9 * by donating a test server!
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2, or (at your option)
14 * any later version.
16 * You should have received a copy of the GNU General Public License
17 * (for example /usr/src/linux/COPYING); if not, write to the Free
18 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22 #include <linux/module.h>
23 #include <linux/slab.h>
24 #include <linux/highmem.h>
25 #include <linux/bitops.h>
26 #include <linux/kthread.h>
27 #include <asm/atomic.h>
28 #include "raid6.h"
30 #include <linux/raid/bitmap.h>
33 * Stripe cache
36 #define NR_STRIPES 256
37 #define STRIPE_SIZE PAGE_SIZE
38 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
39 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
40 #define IO_THRESHOLD 1
41 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
42 #define HASH_MASK (NR_HASH - 1)
44 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
46 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
47 * order without overlap. There may be several bio's per stripe+device, and
48 * a bio could span several devices.
49 * When walking this list for a particular stripe+device, we must never proceed
50 * beyond a bio that extends past this device, as the next bio might no longer
51 * be valid.
52 * This macro is used to determine the 'next' bio in the list, given the sector
53 * of the current stripe+device
55 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
57 * The following can be used to debug the driver
59 #define RAID5_DEBUG 0
60 #define RAID5_PARANOIA 1
61 #if RAID5_PARANOIA && defined(CONFIG_SMP)
62 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
63 #else
64 # define CHECK_DEVLOCK()
65 #endif
67 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
68 #if RAID5_DEBUG
69 #define inline
70 #define __inline__
71 #endif
73 #if !RAID6_USE_EMPTY_ZERO_PAGE
74 /* In .bss so it's zeroed */
75 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
76 #endif
78 static inline int raid6_next_disk(int disk, int raid_disks)
80 disk++;
81 return (disk < raid_disks) ? disk : 0;
83 static void print_raid5_conf (raid5_conf_t *conf);
85 static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
87 if (atomic_dec_and_test(&sh->count)) {
88 BUG_ON(!list_empty(&sh->lru));
89 BUG_ON(atomic_read(&conf->active_stripes)==0);
90 if (test_bit(STRIPE_HANDLE, &sh->state)) {
91 if (test_bit(STRIPE_DELAYED, &sh->state)) {
92 list_add_tail(&sh->lru, &conf->delayed_list);
93 blk_plug_device(conf->mddev->queue);
94 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
95 conf->seq_write == sh->bm_seq) {
96 list_add_tail(&sh->lru, &conf->bitmap_list);
97 blk_plug_device(conf->mddev->queue);
98 } else {
99 clear_bit(STRIPE_BIT_DELAY, &sh->state);
100 list_add_tail(&sh->lru, &conf->handle_list);
102 md_wakeup_thread(conf->mddev->thread);
103 } else {
104 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
105 atomic_dec(&conf->preread_active_stripes);
106 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
107 md_wakeup_thread(conf->mddev->thread);
109 atomic_dec(&conf->active_stripes);
110 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
111 list_add_tail(&sh->lru, &conf->inactive_list);
112 wake_up(&conf->wait_for_stripe);
117 static void release_stripe(struct stripe_head *sh)
119 raid5_conf_t *conf = sh->raid_conf;
120 unsigned long flags;
122 spin_lock_irqsave(&conf->device_lock, flags);
123 __release_stripe(conf, sh);
124 spin_unlock_irqrestore(&conf->device_lock, flags);
127 static inline void remove_hash(struct stripe_head *sh)
129 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
131 hlist_del_init(&sh->hash);
134 static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
136 struct hlist_head *hp = stripe_hash(conf, sh->sector);
138 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
140 CHECK_DEVLOCK();
141 hlist_add_head(&sh->hash, hp);
145 /* find an idle stripe, make sure it is unhashed, and return it. */
146 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
148 struct stripe_head *sh = NULL;
149 struct list_head *first;
151 CHECK_DEVLOCK();
152 if (list_empty(&conf->inactive_list))
153 goto out;
154 first = conf->inactive_list.next;
155 sh = list_entry(first, struct stripe_head, lru);
156 list_del_init(first);
157 remove_hash(sh);
158 atomic_inc(&conf->active_stripes);
159 out:
160 return sh;
163 static void shrink_buffers(struct stripe_head *sh, int num)
165 struct page *p;
166 int i;
168 for (i=0; i<num ; i++) {
169 p = sh->dev[i].page;
170 if (!p)
171 continue;
172 sh->dev[i].page = NULL;
173 put_page(p);
177 static int grow_buffers(struct stripe_head *sh, int num)
179 int i;
181 for (i=0; i<num; i++) {
182 struct page *page;
184 if (!(page = alloc_page(GFP_KERNEL))) {
185 return 1;
187 sh->dev[i].page = page;
189 return 0;
192 static void raid5_build_block (struct stripe_head *sh, int i);
194 static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
196 raid5_conf_t *conf = sh->raid_conf;
197 int i;
199 BUG_ON(atomic_read(&sh->count) != 0);
200 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
202 CHECK_DEVLOCK();
203 PRINTK("init_stripe called, stripe %llu\n",
204 (unsigned long long)sh->sector);
206 remove_hash(sh);
208 sh->sector = sector;
209 sh->pd_idx = pd_idx;
210 sh->state = 0;
212 sh->disks = disks;
214 for (i = sh->disks; i--; ) {
215 struct r5dev *dev = &sh->dev[i];
217 if (dev->toread || dev->towrite || dev->written ||
218 test_bit(R5_LOCKED, &dev->flags)) {
219 printk("sector=%llx i=%d %p %p %p %d\n",
220 (unsigned long long)sh->sector, i, dev->toread,
221 dev->towrite, dev->written,
222 test_bit(R5_LOCKED, &dev->flags));
223 BUG();
225 dev->flags = 0;
226 raid5_build_block(sh, i);
228 insert_hash(conf, sh);
231 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
233 struct stripe_head *sh;
234 struct hlist_node *hn;
236 CHECK_DEVLOCK();
237 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
238 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
239 if (sh->sector == sector && sh->disks == disks)
240 return sh;
241 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
242 return NULL;
245 static void unplug_slaves(mddev_t *mddev);
246 static void raid5_unplug_device(request_queue_t *q);
248 static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
249 int pd_idx, int noblock)
251 struct stripe_head *sh;
253 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
255 spin_lock_irq(&conf->device_lock);
257 do {
258 wait_event_lock_irq(conf->wait_for_stripe,
259 conf->quiesce == 0,
260 conf->device_lock, /* nothing */);
261 sh = __find_stripe(conf, sector, disks);
262 if (!sh) {
263 if (!conf->inactive_blocked)
264 sh = get_free_stripe(conf);
265 if (noblock && sh == NULL)
266 break;
267 if (!sh) {
268 conf->inactive_blocked = 1;
269 wait_event_lock_irq(conf->wait_for_stripe,
270 !list_empty(&conf->inactive_list) &&
271 (atomic_read(&conf->active_stripes)
272 < (conf->max_nr_stripes *3/4)
273 || !conf->inactive_blocked),
274 conf->device_lock,
275 raid5_unplug_device(conf->mddev->queue)
277 conf->inactive_blocked = 0;
278 } else
279 init_stripe(sh, sector, pd_idx, disks);
280 } else {
281 if (atomic_read(&sh->count)) {
282 BUG_ON(!list_empty(&sh->lru));
283 } else {
284 if (!test_bit(STRIPE_HANDLE, &sh->state))
285 atomic_inc(&conf->active_stripes);
286 if (list_empty(&sh->lru) &&
287 !test_bit(STRIPE_EXPANDING, &sh->state))
288 BUG();
289 list_del_init(&sh->lru);
292 } while (sh == NULL);
294 if (sh)
295 atomic_inc(&sh->count);
297 spin_unlock_irq(&conf->device_lock);
298 return sh;
301 static int grow_one_stripe(raid5_conf_t *conf)
303 struct stripe_head *sh;
304 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
305 if (!sh)
306 return 0;
307 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
308 sh->raid_conf = conf;
309 spin_lock_init(&sh->lock);
311 if (grow_buffers(sh, conf->raid_disks)) {
312 shrink_buffers(sh, conf->raid_disks);
313 kmem_cache_free(conf->slab_cache, sh);
314 return 0;
316 sh->disks = conf->raid_disks;
317 /* we just created an active stripe so... */
318 atomic_set(&sh->count, 1);
319 atomic_inc(&conf->active_stripes);
320 INIT_LIST_HEAD(&sh->lru);
321 release_stripe(sh);
322 return 1;
325 static int grow_stripes(raid5_conf_t *conf, int num)
327 kmem_cache_t *sc;
328 int devs = conf->raid_disks;
330 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
331 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
332 conf->active_name = 0;
333 sc = kmem_cache_create(conf->cache_name[conf->active_name],
334 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
335 0, 0, NULL, NULL);
336 if (!sc)
337 return 1;
338 conf->slab_cache = sc;
339 conf->pool_size = devs;
340 while (num--)
341 if (!grow_one_stripe(conf))
342 return 1;
343 return 0;
346 #ifdef CONFIG_MD_RAID5_RESHAPE
347 static int resize_stripes(raid5_conf_t *conf, int newsize)
349 /* Make all the stripes able to hold 'newsize' devices.
350 * New slots in each stripe get 'page' set to a new page.
352 * This happens in stages:
353 * 1/ create a new kmem_cache and allocate the required number of
354 * stripe_heads.
355 * 2/ gather all the old stripe_heads and tranfer the pages across
356 * to the new stripe_heads. This will have the side effect of
357 * freezing the array as once all stripe_heads have been collected,
358 * no IO will be possible. Old stripe heads are freed once their
359 * pages have been transferred over, and the old kmem_cache is
360 * freed when all stripes are done.
361 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
362 * we simple return a failre status - no need to clean anything up.
363 * 4/ allocate new pages for the new slots in the new stripe_heads.
364 * If this fails, we don't bother trying the shrink the
365 * stripe_heads down again, we just leave them as they are.
366 * As each stripe_head is processed the new one is released into
367 * active service.
369 * Once step2 is started, we cannot afford to wait for a write,
370 * so we use GFP_NOIO allocations.
372 struct stripe_head *osh, *nsh;
373 LIST_HEAD(newstripes);
374 struct disk_info *ndisks;
375 int err = 0;
376 kmem_cache_t *sc;
377 int i;
379 if (newsize <= conf->pool_size)
380 return 0; /* never bother to shrink */
382 /* Step 1 */
383 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
384 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
385 0, 0, NULL, NULL);
386 if (!sc)
387 return -ENOMEM;
389 for (i = conf->max_nr_stripes; i; i--) {
390 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
391 if (!nsh)
392 break;
394 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
396 nsh->raid_conf = conf;
397 spin_lock_init(&nsh->lock);
399 list_add(&nsh->lru, &newstripes);
401 if (i) {
402 /* didn't get enough, give up */
403 while (!list_empty(&newstripes)) {
404 nsh = list_entry(newstripes.next, struct stripe_head, lru);
405 list_del(&nsh->lru);
406 kmem_cache_free(sc, nsh);
408 kmem_cache_destroy(sc);
409 return -ENOMEM;
411 /* Step 2 - Must use GFP_NOIO now.
412 * OK, we have enough stripes, start collecting inactive
413 * stripes and copying them over
415 list_for_each_entry(nsh, &newstripes, lru) {
416 spin_lock_irq(&conf->device_lock);
417 wait_event_lock_irq(conf->wait_for_stripe,
418 !list_empty(&conf->inactive_list),
419 conf->device_lock,
420 unplug_slaves(conf->mddev)
422 osh = get_free_stripe(conf);
423 spin_unlock_irq(&conf->device_lock);
424 atomic_set(&nsh->count, 1);
425 for(i=0; i<conf->pool_size; i++)
426 nsh->dev[i].page = osh->dev[i].page;
427 for( ; i<newsize; i++)
428 nsh->dev[i].page = NULL;
429 kmem_cache_free(conf->slab_cache, osh);
431 kmem_cache_destroy(conf->slab_cache);
433 /* Step 3.
434 * At this point, we are holding all the stripes so the array
435 * is completely stalled, so now is a good time to resize
436 * conf->disks.
438 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
439 if (ndisks) {
440 for (i=0; i<conf->raid_disks; i++)
441 ndisks[i] = conf->disks[i];
442 kfree(conf->disks);
443 conf->disks = ndisks;
444 } else
445 err = -ENOMEM;
447 /* Step 4, return new stripes to service */
448 while(!list_empty(&newstripes)) {
449 nsh = list_entry(newstripes.next, struct stripe_head, lru);
450 list_del_init(&nsh->lru);
451 for (i=conf->raid_disks; i < newsize; i++)
452 if (nsh->dev[i].page == NULL) {
453 struct page *p = alloc_page(GFP_NOIO);
454 nsh->dev[i].page = p;
455 if (!p)
456 err = -ENOMEM;
458 release_stripe(nsh);
460 /* critical section pass, GFP_NOIO no longer needed */
462 conf->slab_cache = sc;
463 conf->active_name = 1-conf->active_name;
464 conf->pool_size = newsize;
465 return err;
467 #endif
469 static int drop_one_stripe(raid5_conf_t *conf)
471 struct stripe_head *sh;
473 spin_lock_irq(&conf->device_lock);
474 sh = get_free_stripe(conf);
475 spin_unlock_irq(&conf->device_lock);
476 if (!sh)
477 return 0;
478 BUG_ON(atomic_read(&sh->count));
479 shrink_buffers(sh, conf->pool_size);
480 kmem_cache_free(conf->slab_cache, sh);
481 atomic_dec(&conf->active_stripes);
482 return 1;
485 static void shrink_stripes(raid5_conf_t *conf)
487 while (drop_one_stripe(conf))
490 if (conf->slab_cache)
491 kmem_cache_destroy(conf->slab_cache);
492 conf->slab_cache = NULL;
495 static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
496 int error)
498 struct stripe_head *sh = bi->bi_private;
499 raid5_conf_t *conf = sh->raid_conf;
500 int disks = sh->disks, i;
501 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
503 if (bi->bi_size)
504 return 1;
506 for (i=0 ; i<disks; i++)
507 if (bi == &sh->dev[i].req)
508 break;
510 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
511 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
512 uptodate);
513 if (i == disks) {
514 BUG();
515 return 0;
518 if (uptodate) {
519 #if 0
520 struct bio *bio;
521 unsigned long flags;
522 spin_lock_irqsave(&conf->device_lock, flags);
523 /* we can return a buffer if we bypassed the cache or
524 * if the top buffer is not in highmem. If there are
525 * multiple buffers, leave the extra work to
526 * handle_stripe
528 buffer = sh->bh_read[i];
529 if (buffer &&
530 (!PageHighMem(buffer->b_page)
531 || buffer->b_page == bh->b_page )
533 sh->bh_read[i] = buffer->b_reqnext;
534 buffer->b_reqnext = NULL;
535 } else
536 buffer = NULL;
537 spin_unlock_irqrestore(&conf->device_lock, flags);
538 if (sh->bh_page[i]==bh->b_page)
539 set_buffer_uptodate(bh);
540 if (buffer) {
541 if (buffer->b_page != bh->b_page)
542 memcpy(buffer->b_data, bh->b_data, bh->b_size);
543 buffer->b_end_io(buffer, 1);
545 #else
546 set_bit(R5_UPTODATE, &sh->dev[i].flags);
547 #endif
548 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
549 printk(KERN_INFO "raid5: read error corrected!!\n");
550 clear_bit(R5_ReadError, &sh->dev[i].flags);
551 clear_bit(R5_ReWrite, &sh->dev[i].flags);
553 if (atomic_read(&conf->disks[i].rdev->read_errors))
554 atomic_set(&conf->disks[i].rdev->read_errors, 0);
555 } else {
556 int retry = 0;
557 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
558 atomic_inc(&conf->disks[i].rdev->read_errors);
559 if (conf->mddev->degraded)
560 printk(KERN_WARNING "raid5: read error not correctable.\n");
561 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
562 /* Oh, no!!! */
563 printk(KERN_WARNING "raid5: read error NOT corrected!!\n");
564 else if (atomic_read(&conf->disks[i].rdev->read_errors)
565 > conf->max_nr_stripes)
566 printk(KERN_WARNING
567 "raid5: Too many read errors, failing device.\n");
568 else
569 retry = 1;
570 if (retry)
571 set_bit(R5_ReadError, &sh->dev[i].flags);
572 else {
573 clear_bit(R5_ReadError, &sh->dev[i].flags);
574 clear_bit(R5_ReWrite, &sh->dev[i].flags);
575 md_error(conf->mddev, conf->disks[i].rdev);
578 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
579 #if 0
580 /* must restore b_page before unlocking buffer... */
581 if (sh->bh_page[i] != bh->b_page) {
582 bh->b_page = sh->bh_page[i];
583 bh->b_data = page_address(bh->b_page);
584 clear_buffer_uptodate(bh);
586 #endif
587 clear_bit(R5_LOCKED, &sh->dev[i].flags);
588 set_bit(STRIPE_HANDLE, &sh->state);
589 release_stripe(sh);
590 return 0;
593 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
594 int error)
596 struct stripe_head *sh = bi->bi_private;
597 raid5_conf_t *conf = sh->raid_conf;
598 int disks = sh->disks, i;
599 unsigned long flags;
600 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
602 if (bi->bi_size)
603 return 1;
605 for (i=0 ; i<disks; i++)
606 if (bi == &sh->dev[i].req)
607 break;
609 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
610 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
611 uptodate);
612 if (i == disks) {
613 BUG();
614 return 0;
617 spin_lock_irqsave(&conf->device_lock, flags);
618 if (!uptodate)
619 md_error(conf->mddev, conf->disks[i].rdev);
621 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
623 clear_bit(R5_LOCKED, &sh->dev[i].flags);
624 set_bit(STRIPE_HANDLE, &sh->state);
625 __release_stripe(conf, sh);
626 spin_unlock_irqrestore(&conf->device_lock, flags);
627 return 0;
631 static sector_t compute_blocknr(struct stripe_head *sh, int i);
633 static void raid5_build_block (struct stripe_head *sh, int i)
635 struct r5dev *dev = &sh->dev[i];
637 bio_init(&dev->req);
638 dev->req.bi_io_vec = &dev->vec;
639 dev->req.bi_vcnt++;
640 dev->req.bi_max_vecs++;
641 dev->vec.bv_page = dev->page;
642 dev->vec.bv_len = STRIPE_SIZE;
643 dev->vec.bv_offset = 0;
645 dev->req.bi_sector = sh->sector;
646 dev->req.bi_private = sh;
648 dev->flags = 0;
649 dev->sector = compute_blocknr(sh, i);
652 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
654 char b[BDEVNAME_SIZE];
655 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
656 PRINTK("raid5: error called\n");
658 if (!test_bit(Faulty, &rdev->flags)) {
659 mddev->sb_dirty = 1;
660 if (test_bit(In_sync, &rdev->flags)) {
661 conf->working_disks--;
662 mddev->degraded++;
663 conf->failed_disks++;
664 clear_bit(In_sync, &rdev->flags);
666 * if recovery was running, make sure it aborts.
668 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
670 set_bit(Faulty, &rdev->flags);
671 printk (KERN_ALERT
672 "raid5: Disk failure on %s, disabling device."
673 " Operation continuing on %d devices\n",
674 bdevname(rdev->bdev,b), conf->working_disks);
679 * Input: a 'big' sector number,
680 * Output: index of the data and parity disk, and the sector # in them.
682 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
683 unsigned int data_disks, unsigned int * dd_idx,
684 unsigned int * pd_idx, raid5_conf_t *conf)
686 long stripe;
687 unsigned long chunk_number;
688 unsigned int chunk_offset;
689 sector_t new_sector;
690 int sectors_per_chunk = conf->chunk_size >> 9;
692 /* First compute the information on this sector */
695 * Compute the chunk number and the sector offset inside the chunk
697 chunk_offset = sector_div(r_sector, sectors_per_chunk);
698 chunk_number = r_sector;
699 BUG_ON(r_sector != chunk_number);
702 * Compute the stripe number
704 stripe = chunk_number / data_disks;
707 * Compute the data disk and parity disk indexes inside the stripe
709 *dd_idx = chunk_number % data_disks;
712 * Select the parity disk based on the user selected algorithm.
714 switch(conf->level) {
715 case 4:
716 *pd_idx = data_disks;
717 break;
718 case 5:
719 switch (conf->algorithm) {
720 case ALGORITHM_LEFT_ASYMMETRIC:
721 *pd_idx = data_disks - stripe % raid_disks;
722 if (*dd_idx >= *pd_idx)
723 (*dd_idx)++;
724 break;
725 case ALGORITHM_RIGHT_ASYMMETRIC:
726 *pd_idx = stripe % raid_disks;
727 if (*dd_idx >= *pd_idx)
728 (*dd_idx)++;
729 break;
730 case ALGORITHM_LEFT_SYMMETRIC:
731 *pd_idx = data_disks - stripe % raid_disks;
732 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
733 break;
734 case ALGORITHM_RIGHT_SYMMETRIC:
735 *pd_idx = stripe % raid_disks;
736 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
737 break;
738 default:
739 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
740 conf->algorithm);
742 break;
743 case 6:
745 /**** FIX THIS ****/
746 switch (conf->algorithm) {
747 case ALGORITHM_LEFT_ASYMMETRIC:
748 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
749 if (*pd_idx == raid_disks-1)
750 (*dd_idx)++; /* Q D D D P */
751 else if (*dd_idx >= *pd_idx)
752 (*dd_idx) += 2; /* D D P Q D */
753 break;
754 case ALGORITHM_RIGHT_ASYMMETRIC:
755 *pd_idx = stripe % raid_disks;
756 if (*pd_idx == raid_disks-1)
757 (*dd_idx)++; /* Q D D D P */
758 else if (*dd_idx >= *pd_idx)
759 (*dd_idx) += 2; /* D D P Q D */
760 break;
761 case ALGORITHM_LEFT_SYMMETRIC:
762 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
763 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
764 break;
765 case ALGORITHM_RIGHT_SYMMETRIC:
766 *pd_idx = stripe % raid_disks;
767 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
768 break;
769 default:
770 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
771 conf->algorithm);
773 break;
777 * Finally, compute the new sector number
779 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
780 return new_sector;
784 static sector_t compute_blocknr(struct stripe_head *sh, int i)
786 raid5_conf_t *conf = sh->raid_conf;
787 int raid_disks = sh->disks, data_disks = raid_disks - 1;
788 sector_t new_sector = sh->sector, check;
789 int sectors_per_chunk = conf->chunk_size >> 9;
790 sector_t stripe;
791 int chunk_offset;
792 int chunk_number, dummy1, dummy2, dd_idx = i;
793 sector_t r_sector;
796 chunk_offset = sector_div(new_sector, sectors_per_chunk);
797 stripe = new_sector;
798 BUG_ON(new_sector != stripe);
800 if (i == sh->pd_idx)
801 return 0;
802 switch(conf->level) {
803 case 4: break;
804 case 5:
805 switch (conf->algorithm) {
806 case ALGORITHM_LEFT_ASYMMETRIC:
807 case ALGORITHM_RIGHT_ASYMMETRIC:
808 if (i > sh->pd_idx)
809 i--;
810 break;
811 case ALGORITHM_LEFT_SYMMETRIC:
812 case ALGORITHM_RIGHT_SYMMETRIC:
813 if (i < sh->pd_idx)
814 i += raid_disks;
815 i -= (sh->pd_idx + 1);
816 break;
817 default:
818 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
819 conf->algorithm);
821 break;
822 case 6:
823 data_disks = raid_disks - 2;
824 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
825 return 0; /* It is the Q disk */
826 switch (conf->algorithm) {
827 case ALGORITHM_LEFT_ASYMMETRIC:
828 case ALGORITHM_RIGHT_ASYMMETRIC:
829 if (sh->pd_idx == raid_disks-1)
830 i--; /* Q D D D P */
831 else if (i > sh->pd_idx)
832 i -= 2; /* D D P Q D */
833 break;
834 case ALGORITHM_LEFT_SYMMETRIC:
835 case ALGORITHM_RIGHT_SYMMETRIC:
836 if (sh->pd_idx == raid_disks-1)
837 i--; /* Q D D D P */
838 else {
839 /* D D P Q D */
840 if (i < sh->pd_idx)
841 i += raid_disks;
842 i -= (sh->pd_idx + 2);
844 break;
845 default:
846 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
847 conf->algorithm);
849 break;
852 chunk_number = stripe * data_disks + i;
853 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
855 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
856 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
857 printk(KERN_ERR "compute_blocknr: map not correct\n");
858 return 0;
860 return r_sector;
866 * Copy data between a page in the stripe cache, and one or more bion
867 * The page could align with the middle of the bio, or there could be
868 * several bion, each with several bio_vecs, which cover part of the page
869 * Multiple bion are linked together on bi_next. There may be extras
870 * at the end of this list. We ignore them.
872 static void copy_data(int frombio, struct bio *bio,
873 struct page *page,
874 sector_t sector)
876 char *pa = page_address(page);
877 struct bio_vec *bvl;
878 int i;
879 int page_offset;
881 if (bio->bi_sector >= sector)
882 page_offset = (signed)(bio->bi_sector - sector) * 512;
883 else
884 page_offset = (signed)(sector - bio->bi_sector) * -512;
885 bio_for_each_segment(bvl, bio, i) {
886 int len = bio_iovec_idx(bio,i)->bv_len;
887 int clen;
888 int b_offset = 0;
890 if (page_offset < 0) {
891 b_offset = -page_offset;
892 page_offset += b_offset;
893 len -= b_offset;
896 if (len > 0 && page_offset + len > STRIPE_SIZE)
897 clen = STRIPE_SIZE - page_offset;
898 else clen = len;
900 if (clen > 0) {
901 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
902 if (frombio)
903 memcpy(pa+page_offset, ba+b_offset, clen);
904 else
905 memcpy(ba+b_offset, pa+page_offset, clen);
906 __bio_kunmap_atomic(ba, KM_USER0);
908 if (clen < len) /* hit end of page */
909 break;
910 page_offset += len;
914 #define check_xor() do { \
915 if (count == MAX_XOR_BLOCKS) { \
916 xor_block(count, STRIPE_SIZE, ptr); \
917 count = 1; \
919 } while(0)
922 static void compute_block(struct stripe_head *sh, int dd_idx)
924 int i, count, disks = sh->disks;
925 void *ptr[MAX_XOR_BLOCKS], *p;
927 PRINTK("compute_block, stripe %llu, idx %d\n",
928 (unsigned long long)sh->sector, dd_idx);
930 ptr[0] = page_address(sh->dev[dd_idx].page);
931 memset(ptr[0], 0, STRIPE_SIZE);
932 count = 1;
933 for (i = disks ; i--; ) {
934 if (i == dd_idx)
935 continue;
936 p = page_address(sh->dev[i].page);
937 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
938 ptr[count++] = p;
939 else
940 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
941 " not present\n", dd_idx,
942 (unsigned long long)sh->sector, i);
944 check_xor();
946 if (count != 1)
947 xor_block(count, STRIPE_SIZE, ptr);
948 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
951 static void compute_parity5(struct stripe_head *sh, int method)
953 raid5_conf_t *conf = sh->raid_conf;
954 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
955 void *ptr[MAX_XOR_BLOCKS];
956 struct bio *chosen;
958 PRINTK("compute_parity5, stripe %llu, method %d\n",
959 (unsigned long long)sh->sector, method);
961 count = 1;
962 ptr[0] = page_address(sh->dev[pd_idx].page);
963 switch(method) {
964 case READ_MODIFY_WRITE:
965 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
966 for (i=disks ; i-- ;) {
967 if (i==pd_idx)
968 continue;
969 if (sh->dev[i].towrite &&
970 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
971 ptr[count++] = page_address(sh->dev[i].page);
972 chosen = sh->dev[i].towrite;
973 sh->dev[i].towrite = NULL;
975 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
976 wake_up(&conf->wait_for_overlap);
978 BUG_ON(sh->dev[i].written);
979 sh->dev[i].written = chosen;
980 check_xor();
983 break;
984 case RECONSTRUCT_WRITE:
985 memset(ptr[0], 0, STRIPE_SIZE);
986 for (i= disks; i-- ;)
987 if (i!=pd_idx && sh->dev[i].towrite) {
988 chosen = sh->dev[i].towrite;
989 sh->dev[i].towrite = NULL;
991 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
992 wake_up(&conf->wait_for_overlap);
994 BUG_ON(sh->dev[i].written);
995 sh->dev[i].written = chosen;
997 break;
998 case CHECK_PARITY:
999 break;
1001 if (count>1) {
1002 xor_block(count, STRIPE_SIZE, ptr);
1003 count = 1;
1006 for (i = disks; i--;)
1007 if (sh->dev[i].written) {
1008 sector_t sector = sh->dev[i].sector;
1009 struct bio *wbi = sh->dev[i].written;
1010 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1011 copy_data(1, wbi, sh->dev[i].page, sector);
1012 wbi = r5_next_bio(wbi, sector);
1015 set_bit(R5_LOCKED, &sh->dev[i].flags);
1016 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1019 switch(method) {
1020 case RECONSTRUCT_WRITE:
1021 case CHECK_PARITY:
1022 for (i=disks; i--;)
1023 if (i != pd_idx) {
1024 ptr[count++] = page_address(sh->dev[i].page);
1025 check_xor();
1027 break;
1028 case READ_MODIFY_WRITE:
1029 for (i = disks; i--;)
1030 if (sh->dev[i].written) {
1031 ptr[count++] = page_address(sh->dev[i].page);
1032 check_xor();
1035 if (count != 1)
1036 xor_block(count, STRIPE_SIZE, ptr);
1038 if (method != CHECK_PARITY) {
1039 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1040 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1041 } else
1042 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1045 static void compute_parity6(struct stripe_head *sh, int method)
1047 raid6_conf_t *conf = sh->raid_conf;
1048 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
1049 struct bio *chosen;
1050 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1051 void *ptrs[disks];
1053 qd_idx = raid6_next_disk(pd_idx, disks);
1054 d0_idx = raid6_next_disk(qd_idx, disks);
1056 PRINTK("compute_parity, stripe %llu, method %d\n",
1057 (unsigned long long)sh->sector, method);
1059 switch(method) {
1060 case READ_MODIFY_WRITE:
1061 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1062 case RECONSTRUCT_WRITE:
1063 for (i= disks; i-- ;)
1064 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1065 chosen = sh->dev[i].towrite;
1066 sh->dev[i].towrite = NULL;
1068 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1069 wake_up(&conf->wait_for_overlap);
1071 if (sh->dev[i].written) BUG();
1072 sh->dev[i].written = chosen;
1074 break;
1075 case CHECK_PARITY:
1076 BUG(); /* Not implemented yet */
1079 for (i = disks; i--;)
1080 if (sh->dev[i].written) {
1081 sector_t sector = sh->dev[i].sector;
1082 struct bio *wbi = sh->dev[i].written;
1083 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1084 copy_data(1, wbi, sh->dev[i].page, sector);
1085 wbi = r5_next_bio(wbi, sector);
1088 set_bit(R5_LOCKED, &sh->dev[i].flags);
1089 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1092 // switch(method) {
1093 // case RECONSTRUCT_WRITE:
1094 // case CHECK_PARITY:
1095 // case UPDATE_PARITY:
1096 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1097 /* FIX: Is this ordering of drives even remotely optimal? */
1098 count = 0;
1099 i = d0_idx;
1100 do {
1101 ptrs[count++] = page_address(sh->dev[i].page);
1102 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1103 printk("block %d/%d not uptodate on parity calc\n", i,count);
1104 i = raid6_next_disk(i, disks);
1105 } while ( i != d0_idx );
1106 // break;
1107 // }
1109 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1111 switch(method) {
1112 case RECONSTRUCT_WRITE:
1113 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1114 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1115 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1116 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1117 break;
1118 case UPDATE_PARITY:
1119 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1120 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1121 break;
1126 /* Compute one missing block */
1127 static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1129 raid6_conf_t *conf = sh->raid_conf;
1130 int i, count, disks = conf->raid_disks;
1131 void *ptr[MAX_XOR_BLOCKS], *p;
1132 int pd_idx = sh->pd_idx;
1133 int qd_idx = raid6_next_disk(pd_idx, disks);
1135 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1136 (unsigned long long)sh->sector, dd_idx);
1138 if ( dd_idx == qd_idx ) {
1139 /* We're actually computing the Q drive */
1140 compute_parity6(sh, UPDATE_PARITY);
1141 } else {
1142 ptr[0] = page_address(sh->dev[dd_idx].page);
1143 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
1144 count = 1;
1145 for (i = disks ; i--; ) {
1146 if (i == dd_idx || i == qd_idx)
1147 continue;
1148 p = page_address(sh->dev[i].page);
1149 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1150 ptr[count++] = p;
1151 else
1152 printk("compute_block() %d, stripe %llu, %d"
1153 " not present\n", dd_idx,
1154 (unsigned long long)sh->sector, i);
1156 check_xor();
1158 if (count != 1)
1159 xor_block(count, STRIPE_SIZE, ptr);
1160 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1161 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1165 /* Compute two missing blocks */
1166 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1168 raid6_conf_t *conf = sh->raid_conf;
1169 int i, count, disks = conf->raid_disks;
1170 int pd_idx = sh->pd_idx;
1171 int qd_idx = raid6_next_disk(pd_idx, disks);
1172 int d0_idx = raid6_next_disk(qd_idx, disks);
1173 int faila, failb;
1175 /* faila and failb are disk numbers relative to d0_idx */
1176 /* pd_idx become disks-2 and qd_idx become disks-1 */
1177 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1178 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1180 BUG_ON(faila == failb);
1181 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1183 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1184 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1186 if ( failb == disks-1 ) {
1187 /* Q disk is one of the missing disks */
1188 if ( faila == disks-2 ) {
1189 /* Missing P+Q, just recompute */
1190 compute_parity6(sh, UPDATE_PARITY);
1191 return;
1192 } else {
1193 /* We're missing D+Q; recompute D from P */
1194 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1195 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1196 return;
1200 /* We're missing D+P or D+D; build pointer table */
1202 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1203 void *ptrs[disks];
1205 count = 0;
1206 i = d0_idx;
1207 do {
1208 ptrs[count++] = page_address(sh->dev[i].page);
1209 i = raid6_next_disk(i, disks);
1210 if (i != dd_idx1 && i != dd_idx2 &&
1211 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1212 printk("compute_2 with missing block %d/%d\n", count, i);
1213 } while ( i != d0_idx );
1215 if ( failb == disks-2 ) {
1216 /* We're missing D+P. */
1217 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1218 } else {
1219 /* We're missing D+D. */
1220 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1223 /* Both the above update both missing blocks */
1224 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1225 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1232 * Each stripe/dev can have one or more bion attached.
1233 * toread/towrite point to the first in a chain.
1234 * The bi_next chain must be in order.
1236 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1238 struct bio **bip;
1239 raid5_conf_t *conf = sh->raid_conf;
1240 int firstwrite=0;
1242 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1243 (unsigned long long)bi->bi_sector,
1244 (unsigned long long)sh->sector);
1247 spin_lock(&sh->lock);
1248 spin_lock_irq(&conf->device_lock);
1249 if (forwrite) {
1250 bip = &sh->dev[dd_idx].towrite;
1251 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1252 firstwrite = 1;
1253 } else
1254 bip = &sh->dev[dd_idx].toread;
1255 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1256 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1257 goto overlap;
1258 bip = & (*bip)->bi_next;
1260 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1261 goto overlap;
1263 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
1264 if (*bip)
1265 bi->bi_next = *bip;
1266 *bip = bi;
1267 bi->bi_phys_segments ++;
1268 spin_unlock_irq(&conf->device_lock);
1269 spin_unlock(&sh->lock);
1271 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1272 (unsigned long long)bi->bi_sector,
1273 (unsigned long long)sh->sector, dd_idx);
1275 if (conf->mddev->bitmap && firstwrite) {
1276 sh->bm_seq = conf->seq_write;
1277 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1278 STRIPE_SECTORS, 0);
1279 set_bit(STRIPE_BIT_DELAY, &sh->state);
1282 if (forwrite) {
1283 /* check if page is covered */
1284 sector_t sector = sh->dev[dd_idx].sector;
1285 for (bi=sh->dev[dd_idx].towrite;
1286 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1287 bi && bi->bi_sector <= sector;
1288 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1289 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1290 sector = bi->bi_sector + (bi->bi_size>>9);
1292 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1293 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1295 return 1;
1297 overlap:
1298 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1299 spin_unlock_irq(&conf->device_lock);
1300 spin_unlock(&sh->lock);
1301 return 0;
1304 static void end_reshape(raid5_conf_t *conf);
1306 static int page_is_zero(struct page *p)
1308 char *a = page_address(p);
1309 return ((*(u32*)a) == 0 &&
1310 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1313 static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1315 int sectors_per_chunk = conf->chunk_size >> 9;
1316 sector_t x = stripe;
1317 int pd_idx, dd_idx;
1318 int chunk_offset = sector_div(x, sectors_per_chunk);
1319 stripe = x;
1320 raid5_compute_sector(stripe*(disks-1)*sectors_per_chunk
1321 + chunk_offset, disks, disks-1, &dd_idx, &pd_idx, conf);
1322 return pd_idx;
1327 * handle_stripe - do things to a stripe.
1329 * We lock the stripe and then examine the state of various bits
1330 * to see what needs to be done.
1331 * Possible results:
1332 * return some read request which now have data
1333 * return some write requests which are safely on disc
1334 * schedule a read on some buffers
1335 * schedule a write of some buffers
1336 * return confirmation of parity correctness
1338 * Parity calculations are done inside the stripe lock
1339 * buffers are taken off read_list or write_list, and bh_cache buffers
1340 * get BH_Lock set before the stripe lock is released.
1344 static void handle_stripe5(struct stripe_head *sh)
1346 raid5_conf_t *conf = sh->raid_conf;
1347 int disks = sh->disks;
1348 struct bio *return_bi= NULL;
1349 struct bio *bi;
1350 int i;
1351 int syncing, expanding, expanded;
1352 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1353 int non_overwrite = 0;
1354 int failed_num=0;
1355 struct r5dev *dev;
1357 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1358 (unsigned long long)sh->sector, atomic_read(&sh->count),
1359 sh->pd_idx);
1361 spin_lock(&sh->lock);
1362 clear_bit(STRIPE_HANDLE, &sh->state);
1363 clear_bit(STRIPE_DELAYED, &sh->state);
1365 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1366 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1367 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
1368 /* Now to look around and see what can be done */
1370 rcu_read_lock();
1371 for (i=disks; i--; ) {
1372 mdk_rdev_t *rdev;
1373 dev = &sh->dev[i];
1374 clear_bit(R5_Insync, &dev->flags);
1376 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1377 i, dev->flags, dev->toread, dev->towrite, dev->written);
1378 /* maybe we can reply to a read */
1379 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1380 struct bio *rbi, *rbi2;
1381 PRINTK("Return read for disc %d\n", i);
1382 spin_lock_irq(&conf->device_lock);
1383 rbi = dev->toread;
1384 dev->toread = NULL;
1385 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1386 wake_up(&conf->wait_for_overlap);
1387 spin_unlock_irq(&conf->device_lock);
1388 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1389 copy_data(0, rbi, dev->page, dev->sector);
1390 rbi2 = r5_next_bio(rbi, dev->sector);
1391 spin_lock_irq(&conf->device_lock);
1392 if (--rbi->bi_phys_segments == 0) {
1393 rbi->bi_next = return_bi;
1394 return_bi = rbi;
1396 spin_unlock_irq(&conf->device_lock);
1397 rbi = rbi2;
1401 /* now count some things */
1402 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1403 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1406 if (dev->toread) to_read++;
1407 if (dev->towrite) {
1408 to_write++;
1409 if (!test_bit(R5_OVERWRITE, &dev->flags))
1410 non_overwrite++;
1412 if (dev->written) written++;
1413 rdev = rcu_dereference(conf->disks[i].rdev);
1414 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1415 /* The ReadError flag will just be confusing now */
1416 clear_bit(R5_ReadError, &dev->flags);
1417 clear_bit(R5_ReWrite, &dev->flags);
1419 if (!rdev || !test_bit(In_sync, &rdev->flags)
1420 || test_bit(R5_ReadError, &dev->flags)) {
1421 failed++;
1422 failed_num = i;
1423 } else
1424 set_bit(R5_Insync, &dev->flags);
1426 rcu_read_unlock();
1427 PRINTK("locked=%d uptodate=%d to_read=%d"
1428 " to_write=%d failed=%d failed_num=%d\n",
1429 locked, uptodate, to_read, to_write, failed, failed_num);
1430 /* check if the array has lost two devices and, if so, some requests might
1431 * need to be failed
1433 if (failed > 1 && to_read+to_write+written) {
1434 for (i=disks; i--; ) {
1435 int bitmap_end = 0;
1437 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1438 mdk_rdev_t *rdev;
1439 rcu_read_lock();
1440 rdev = rcu_dereference(conf->disks[i].rdev);
1441 if (rdev && test_bit(In_sync, &rdev->flags))
1442 /* multiple read failures in one stripe */
1443 md_error(conf->mddev, rdev);
1444 rcu_read_unlock();
1447 spin_lock_irq(&conf->device_lock);
1448 /* fail all writes first */
1449 bi = sh->dev[i].towrite;
1450 sh->dev[i].towrite = NULL;
1451 if (bi) { to_write--; bitmap_end = 1; }
1453 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1454 wake_up(&conf->wait_for_overlap);
1456 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1457 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1458 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1459 if (--bi->bi_phys_segments == 0) {
1460 md_write_end(conf->mddev);
1461 bi->bi_next = return_bi;
1462 return_bi = bi;
1464 bi = nextbi;
1466 /* and fail all 'written' */
1467 bi = sh->dev[i].written;
1468 sh->dev[i].written = NULL;
1469 if (bi) bitmap_end = 1;
1470 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1471 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1472 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1473 if (--bi->bi_phys_segments == 0) {
1474 md_write_end(conf->mddev);
1475 bi->bi_next = return_bi;
1476 return_bi = bi;
1478 bi = bi2;
1481 /* fail any reads if this device is non-operational */
1482 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1483 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1484 bi = sh->dev[i].toread;
1485 sh->dev[i].toread = NULL;
1486 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1487 wake_up(&conf->wait_for_overlap);
1488 if (bi) to_read--;
1489 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1490 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1491 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1492 if (--bi->bi_phys_segments == 0) {
1493 bi->bi_next = return_bi;
1494 return_bi = bi;
1496 bi = nextbi;
1499 spin_unlock_irq(&conf->device_lock);
1500 if (bitmap_end)
1501 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1502 STRIPE_SECTORS, 0, 0);
1505 if (failed > 1 && syncing) {
1506 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1507 clear_bit(STRIPE_SYNCING, &sh->state);
1508 syncing = 0;
1511 /* might be able to return some write requests if the parity block
1512 * is safe, or on a failed drive
1514 dev = &sh->dev[sh->pd_idx];
1515 if ( written &&
1516 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1517 test_bit(R5_UPTODATE, &dev->flags))
1518 || (failed == 1 && failed_num == sh->pd_idx))
1520 /* any written block on an uptodate or failed drive can be returned.
1521 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1522 * never LOCKED, so we don't need to test 'failed' directly.
1524 for (i=disks; i--; )
1525 if (sh->dev[i].written) {
1526 dev = &sh->dev[i];
1527 if (!test_bit(R5_LOCKED, &dev->flags) &&
1528 test_bit(R5_UPTODATE, &dev->flags) ) {
1529 /* We can return any write requests */
1530 struct bio *wbi, *wbi2;
1531 int bitmap_end = 0;
1532 PRINTK("Return write for disc %d\n", i);
1533 spin_lock_irq(&conf->device_lock);
1534 wbi = dev->written;
1535 dev->written = NULL;
1536 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1537 wbi2 = r5_next_bio(wbi, dev->sector);
1538 if (--wbi->bi_phys_segments == 0) {
1539 md_write_end(conf->mddev);
1540 wbi->bi_next = return_bi;
1541 return_bi = wbi;
1543 wbi = wbi2;
1545 if (dev->towrite == NULL)
1546 bitmap_end = 1;
1547 spin_unlock_irq(&conf->device_lock);
1548 if (bitmap_end)
1549 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1550 STRIPE_SECTORS,
1551 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1556 /* Now we might consider reading some blocks, either to check/generate
1557 * parity, or to satisfy requests
1558 * or to load a block that is being partially written.
1560 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
1561 for (i=disks; i--;) {
1562 dev = &sh->dev[i];
1563 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1564 (dev->toread ||
1565 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1566 syncing ||
1567 expanding ||
1568 (failed && (sh->dev[failed_num].toread ||
1569 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1572 /* we would like to get this block, possibly
1573 * by computing it, but we might not be able to
1575 if (uptodate == disks-1) {
1576 PRINTK("Computing block %d\n", i);
1577 compute_block(sh, i);
1578 uptodate++;
1579 } else if (test_bit(R5_Insync, &dev->flags)) {
1580 set_bit(R5_LOCKED, &dev->flags);
1581 set_bit(R5_Wantread, &dev->flags);
1582 #if 0
1583 /* if I am just reading this block and we don't have
1584 a failed drive, or any pending writes then sidestep the cache */
1585 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
1586 ! syncing && !failed && !to_write) {
1587 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
1588 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
1590 #endif
1591 locked++;
1592 PRINTK("Reading block %d (sync=%d)\n",
1593 i, syncing);
1597 set_bit(STRIPE_HANDLE, &sh->state);
1600 /* now to consider writing and what else, if anything should be read */
1601 if (to_write) {
1602 int rmw=0, rcw=0;
1603 for (i=disks ; i--;) {
1604 /* would I have to read this buffer for read_modify_write */
1605 dev = &sh->dev[i];
1606 if ((dev->towrite || i == sh->pd_idx) &&
1607 (!test_bit(R5_LOCKED, &dev->flags)
1608 #if 0
1609 || sh->bh_page[i]!=bh->b_page
1610 #endif
1611 ) &&
1612 !test_bit(R5_UPTODATE, &dev->flags)) {
1613 if (test_bit(R5_Insync, &dev->flags)
1614 /* && !(!mddev->insync && i == sh->pd_idx) */
1616 rmw++;
1617 else rmw += 2*disks; /* cannot read it */
1619 /* Would I have to read this buffer for reconstruct_write */
1620 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1621 (!test_bit(R5_LOCKED, &dev->flags)
1622 #if 0
1623 || sh->bh_page[i] != bh->b_page
1624 #endif
1625 ) &&
1626 !test_bit(R5_UPTODATE, &dev->flags)) {
1627 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1628 else rcw += 2*disks;
1631 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1632 (unsigned long long)sh->sector, rmw, rcw);
1633 set_bit(STRIPE_HANDLE, &sh->state);
1634 if (rmw < rcw && rmw > 0)
1635 /* prefer read-modify-write, but need to get some data */
1636 for (i=disks; i--;) {
1637 dev = &sh->dev[i];
1638 if ((dev->towrite || i == sh->pd_idx) &&
1639 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1640 test_bit(R5_Insync, &dev->flags)) {
1641 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1643 PRINTK("Read_old block %d for r-m-w\n", i);
1644 set_bit(R5_LOCKED, &dev->flags);
1645 set_bit(R5_Wantread, &dev->flags);
1646 locked++;
1647 } else {
1648 set_bit(STRIPE_DELAYED, &sh->state);
1649 set_bit(STRIPE_HANDLE, &sh->state);
1653 if (rcw <= rmw && rcw > 0)
1654 /* want reconstruct write, but need to get some data */
1655 for (i=disks; i--;) {
1656 dev = &sh->dev[i];
1657 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1658 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1659 test_bit(R5_Insync, &dev->flags)) {
1660 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1662 PRINTK("Read_old block %d for Reconstruct\n", i);
1663 set_bit(R5_LOCKED, &dev->flags);
1664 set_bit(R5_Wantread, &dev->flags);
1665 locked++;
1666 } else {
1667 set_bit(STRIPE_DELAYED, &sh->state);
1668 set_bit(STRIPE_HANDLE, &sh->state);
1672 /* now if nothing is locked, and if we have enough data, we can start a write request */
1673 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1674 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1675 PRINTK("Computing parity...\n");
1676 compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1677 /* now every locked buffer is ready to be written */
1678 for (i=disks; i--;)
1679 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1680 PRINTK("Writing block %d\n", i);
1681 locked++;
1682 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1683 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1684 || (i==sh->pd_idx && failed == 0))
1685 set_bit(STRIPE_INSYNC, &sh->state);
1687 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1688 atomic_dec(&conf->preread_active_stripes);
1689 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1690 md_wakeup_thread(conf->mddev->thread);
1695 /* maybe we need to check and possibly fix the parity for this stripe
1696 * Any reads will already have been scheduled, so we just see if enough data
1697 * is available
1699 if (syncing && locked == 0 &&
1700 !test_bit(STRIPE_INSYNC, &sh->state)) {
1701 set_bit(STRIPE_HANDLE, &sh->state);
1702 if (failed == 0) {
1703 BUG_ON(uptodate != disks);
1704 compute_parity5(sh, CHECK_PARITY);
1705 uptodate--;
1706 if (page_is_zero(sh->dev[sh->pd_idx].page)) {
1707 /* parity is correct (on disc, not in buffer any more) */
1708 set_bit(STRIPE_INSYNC, &sh->state);
1709 } else {
1710 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1711 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1712 /* don't try to repair!! */
1713 set_bit(STRIPE_INSYNC, &sh->state);
1714 else {
1715 compute_block(sh, sh->pd_idx);
1716 uptodate++;
1720 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1721 /* either failed parity check, or recovery is happening */
1722 if (failed==0)
1723 failed_num = sh->pd_idx;
1724 dev = &sh->dev[failed_num];
1725 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1726 BUG_ON(uptodate != disks);
1728 set_bit(R5_LOCKED, &dev->flags);
1729 set_bit(R5_Wantwrite, &dev->flags);
1730 clear_bit(STRIPE_DEGRADED, &sh->state);
1731 locked++;
1732 set_bit(STRIPE_INSYNC, &sh->state);
1735 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1736 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1737 clear_bit(STRIPE_SYNCING, &sh->state);
1740 /* If the failed drive is just a ReadError, then we might need to progress
1741 * the repair/check process
1743 if (failed == 1 && ! conf->mddev->ro &&
1744 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
1745 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1746 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1748 dev = &sh->dev[failed_num];
1749 if (!test_bit(R5_ReWrite, &dev->flags)) {
1750 set_bit(R5_Wantwrite, &dev->flags);
1751 set_bit(R5_ReWrite, &dev->flags);
1752 set_bit(R5_LOCKED, &dev->flags);
1753 locked++;
1754 } else {
1755 /* let's read it back */
1756 set_bit(R5_Wantread, &dev->flags);
1757 set_bit(R5_LOCKED, &dev->flags);
1758 locked++;
1762 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1763 /* Need to write out all blocks after computing parity */
1764 sh->disks = conf->raid_disks;
1765 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
1766 compute_parity5(sh, RECONSTRUCT_WRITE);
1767 for (i= conf->raid_disks; i--;) {
1768 set_bit(R5_LOCKED, &sh->dev[i].flags);
1769 locked++;
1770 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1772 clear_bit(STRIPE_EXPANDING, &sh->state);
1773 } else if (expanded) {
1774 clear_bit(STRIPE_EXPAND_READY, &sh->state);
1775 atomic_dec(&conf->reshape_stripes);
1776 wake_up(&conf->wait_for_overlap);
1777 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1780 if (expanding && locked == 0) {
1781 /* We have read all the blocks in this stripe and now we need to
1782 * copy some of them into a target stripe for expand.
1784 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1785 for (i=0; i< sh->disks; i++)
1786 if (i != sh->pd_idx) {
1787 int dd_idx, pd_idx, j;
1788 struct stripe_head *sh2;
1790 sector_t bn = compute_blocknr(sh, i);
1791 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1792 conf->raid_disks-1,
1793 &dd_idx, &pd_idx, conf);
1794 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1795 if (sh2 == NULL)
1796 /* so far only the early blocks of this stripe
1797 * have been requested. When later blocks
1798 * get requested, we will try again
1800 continue;
1801 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1802 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1803 /* must have already done this block */
1804 release_stripe(sh2);
1805 continue;
1807 memcpy(page_address(sh2->dev[dd_idx].page),
1808 page_address(sh->dev[i].page),
1809 STRIPE_SIZE);
1810 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1811 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1812 for (j=0; j<conf->raid_disks; j++)
1813 if (j != sh2->pd_idx &&
1814 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1815 break;
1816 if (j == conf->raid_disks) {
1817 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1818 set_bit(STRIPE_HANDLE, &sh2->state);
1820 release_stripe(sh2);
1824 spin_unlock(&sh->lock);
1826 while ((bi=return_bi)) {
1827 int bytes = bi->bi_size;
1829 return_bi = bi->bi_next;
1830 bi->bi_next = NULL;
1831 bi->bi_size = 0;
1832 bi->bi_end_io(bi, bytes, 0);
1834 for (i=disks; i-- ;) {
1835 int rw;
1836 struct bio *bi;
1837 mdk_rdev_t *rdev;
1838 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1839 rw = 1;
1840 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1841 rw = 0;
1842 else
1843 continue;
1845 bi = &sh->dev[i].req;
1847 bi->bi_rw = rw;
1848 if (rw)
1849 bi->bi_end_io = raid5_end_write_request;
1850 else
1851 bi->bi_end_io = raid5_end_read_request;
1853 rcu_read_lock();
1854 rdev = rcu_dereference(conf->disks[i].rdev);
1855 if (rdev && test_bit(Faulty, &rdev->flags))
1856 rdev = NULL;
1857 if (rdev)
1858 atomic_inc(&rdev->nr_pending);
1859 rcu_read_unlock();
1861 if (rdev) {
1862 if (syncing || expanding || expanded)
1863 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1865 bi->bi_bdev = rdev->bdev;
1866 PRINTK("for %llu schedule op %ld on disc %d\n",
1867 (unsigned long long)sh->sector, bi->bi_rw, i);
1868 atomic_inc(&sh->count);
1869 bi->bi_sector = sh->sector + rdev->data_offset;
1870 bi->bi_flags = 1 << BIO_UPTODATE;
1871 bi->bi_vcnt = 1;
1872 bi->bi_max_vecs = 1;
1873 bi->bi_idx = 0;
1874 bi->bi_io_vec = &sh->dev[i].vec;
1875 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1876 bi->bi_io_vec[0].bv_offset = 0;
1877 bi->bi_size = STRIPE_SIZE;
1878 bi->bi_next = NULL;
1879 if (rw == WRITE &&
1880 test_bit(R5_ReWrite, &sh->dev[i].flags))
1881 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1882 generic_make_request(bi);
1883 } else {
1884 if (rw == 1)
1885 set_bit(STRIPE_DEGRADED, &sh->state);
1886 PRINTK("skip op %ld on disc %d for sector %llu\n",
1887 bi->bi_rw, i, (unsigned long long)sh->sector);
1888 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1889 set_bit(STRIPE_HANDLE, &sh->state);
1894 static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
1896 raid6_conf_t *conf = sh->raid_conf;
1897 int disks = conf->raid_disks;
1898 struct bio *return_bi= NULL;
1899 struct bio *bi;
1900 int i;
1901 int syncing;
1902 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1903 int non_overwrite = 0;
1904 int failed_num[2] = {0, 0};
1905 struct r5dev *dev, *pdev, *qdev;
1906 int pd_idx = sh->pd_idx;
1907 int qd_idx = raid6_next_disk(pd_idx, disks);
1908 int p_failed, q_failed;
1910 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1911 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1912 pd_idx, qd_idx);
1914 spin_lock(&sh->lock);
1915 clear_bit(STRIPE_HANDLE, &sh->state);
1916 clear_bit(STRIPE_DELAYED, &sh->state);
1918 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1919 /* Now to look around and see what can be done */
1921 rcu_read_lock();
1922 for (i=disks; i--; ) {
1923 mdk_rdev_t *rdev;
1924 dev = &sh->dev[i];
1925 clear_bit(R5_Insync, &dev->flags);
1927 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1928 i, dev->flags, dev->toread, dev->towrite, dev->written);
1929 /* maybe we can reply to a read */
1930 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1931 struct bio *rbi, *rbi2;
1932 PRINTK("Return read for disc %d\n", i);
1933 spin_lock_irq(&conf->device_lock);
1934 rbi = dev->toread;
1935 dev->toread = NULL;
1936 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1937 wake_up(&conf->wait_for_overlap);
1938 spin_unlock_irq(&conf->device_lock);
1939 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1940 copy_data(0, rbi, dev->page, dev->sector);
1941 rbi2 = r5_next_bio(rbi, dev->sector);
1942 spin_lock_irq(&conf->device_lock);
1943 if (--rbi->bi_phys_segments == 0) {
1944 rbi->bi_next = return_bi;
1945 return_bi = rbi;
1947 spin_unlock_irq(&conf->device_lock);
1948 rbi = rbi2;
1952 /* now count some things */
1953 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1954 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1957 if (dev->toread) to_read++;
1958 if (dev->towrite) {
1959 to_write++;
1960 if (!test_bit(R5_OVERWRITE, &dev->flags))
1961 non_overwrite++;
1963 if (dev->written) written++;
1964 rdev = rcu_dereference(conf->disks[i].rdev);
1965 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1966 /* The ReadError flag will just be confusing now */
1967 clear_bit(R5_ReadError, &dev->flags);
1968 clear_bit(R5_ReWrite, &dev->flags);
1970 if (!rdev || !test_bit(In_sync, &rdev->flags)
1971 || test_bit(R5_ReadError, &dev->flags)) {
1972 if ( failed < 2 )
1973 failed_num[failed] = i;
1974 failed++;
1975 } else
1976 set_bit(R5_Insync, &dev->flags);
1978 rcu_read_unlock();
1979 PRINTK("locked=%d uptodate=%d to_read=%d"
1980 " to_write=%d failed=%d failed_num=%d,%d\n",
1981 locked, uptodate, to_read, to_write, failed,
1982 failed_num[0], failed_num[1]);
1983 /* check if the array has lost >2 devices and, if so, some requests might
1984 * need to be failed
1986 if (failed > 2 && to_read+to_write+written) {
1987 for (i=disks; i--; ) {
1988 int bitmap_end = 0;
1990 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1991 mdk_rdev_t *rdev;
1992 rcu_read_lock();
1993 rdev = rcu_dereference(conf->disks[i].rdev);
1994 if (rdev && test_bit(In_sync, &rdev->flags))
1995 /* multiple read failures in one stripe */
1996 md_error(conf->mddev, rdev);
1997 rcu_read_unlock();
2000 spin_lock_irq(&conf->device_lock);
2001 /* fail all writes first */
2002 bi = sh->dev[i].towrite;
2003 sh->dev[i].towrite = NULL;
2004 if (bi) { to_write--; bitmap_end = 1; }
2006 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2007 wake_up(&conf->wait_for_overlap);
2009 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2010 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2011 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2012 if (--bi->bi_phys_segments == 0) {
2013 md_write_end(conf->mddev);
2014 bi->bi_next = return_bi;
2015 return_bi = bi;
2017 bi = nextbi;
2019 /* and fail all 'written' */
2020 bi = sh->dev[i].written;
2021 sh->dev[i].written = NULL;
2022 if (bi) bitmap_end = 1;
2023 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
2024 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2025 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2026 if (--bi->bi_phys_segments == 0) {
2027 md_write_end(conf->mddev);
2028 bi->bi_next = return_bi;
2029 return_bi = bi;
2031 bi = bi2;
2034 /* fail any reads if this device is non-operational */
2035 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2036 test_bit(R5_ReadError, &sh->dev[i].flags)) {
2037 bi = sh->dev[i].toread;
2038 sh->dev[i].toread = NULL;
2039 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2040 wake_up(&conf->wait_for_overlap);
2041 if (bi) to_read--;
2042 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2043 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2044 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2045 if (--bi->bi_phys_segments == 0) {
2046 bi->bi_next = return_bi;
2047 return_bi = bi;
2049 bi = nextbi;
2052 spin_unlock_irq(&conf->device_lock);
2053 if (bitmap_end)
2054 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2055 STRIPE_SECTORS, 0, 0);
2058 if (failed > 2 && syncing) {
2059 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2060 clear_bit(STRIPE_SYNCING, &sh->state);
2061 syncing = 0;
2065 * might be able to return some write requests if the parity blocks
2066 * are safe, or on a failed drive
2068 pdev = &sh->dev[pd_idx];
2069 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
2070 || (failed >= 2 && failed_num[1] == pd_idx);
2071 qdev = &sh->dev[qd_idx];
2072 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
2073 || (failed >= 2 && failed_num[1] == qd_idx);
2075 if ( written &&
2076 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
2077 && !test_bit(R5_LOCKED, &pdev->flags)
2078 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
2079 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
2080 && !test_bit(R5_LOCKED, &qdev->flags)
2081 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
2082 /* any written block on an uptodate or failed drive can be
2083 * returned. Note that if we 'wrote' to a failed drive,
2084 * it will be UPTODATE, but never LOCKED, so we don't need
2085 * to test 'failed' directly.
2087 for (i=disks; i--; )
2088 if (sh->dev[i].written) {
2089 dev = &sh->dev[i];
2090 if (!test_bit(R5_LOCKED, &dev->flags) &&
2091 test_bit(R5_UPTODATE, &dev->flags) ) {
2092 /* We can return any write requests */
2093 int bitmap_end = 0;
2094 struct bio *wbi, *wbi2;
2095 PRINTK("Return write for stripe %llu disc %d\n",
2096 (unsigned long long)sh->sector, i);
2097 spin_lock_irq(&conf->device_lock);
2098 wbi = dev->written;
2099 dev->written = NULL;
2100 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2101 wbi2 = r5_next_bio(wbi, dev->sector);
2102 if (--wbi->bi_phys_segments == 0) {
2103 md_write_end(conf->mddev);
2104 wbi->bi_next = return_bi;
2105 return_bi = wbi;
2107 wbi = wbi2;
2109 if (dev->towrite == NULL)
2110 bitmap_end = 1;
2111 spin_unlock_irq(&conf->device_lock);
2112 if (bitmap_end)
2113 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2114 STRIPE_SECTORS,
2115 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
2120 /* Now we might consider reading some blocks, either to check/generate
2121 * parity, or to satisfy requests
2122 * or to load a block that is being partially written.
2124 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
2125 for (i=disks; i--;) {
2126 dev = &sh->dev[i];
2127 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2128 (dev->toread ||
2129 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2130 syncing ||
2131 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
2132 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
2135 /* we would like to get this block, possibly
2136 * by computing it, but we might not be able to
2138 if (uptodate == disks-1) {
2139 PRINTK("Computing stripe %llu block %d\n",
2140 (unsigned long long)sh->sector, i);
2141 compute_block_1(sh, i, 0);
2142 uptodate++;
2143 } else if ( uptodate == disks-2 && failed >= 2 ) {
2144 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2145 int other;
2146 for (other=disks; other--;) {
2147 if ( other == i )
2148 continue;
2149 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
2150 break;
2152 BUG_ON(other < 0);
2153 PRINTK("Computing stripe %llu blocks %d,%d\n",
2154 (unsigned long long)sh->sector, i, other);
2155 compute_block_2(sh, i, other);
2156 uptodate += 2;
2157 } else if (test_bit(R5_Insync, &dev->flags)) {
2158 set_bit(R5_LOCKED, &dev->flags);
2159 set_bit(R5_Wantread, &dev->flags);
2160 #if 0
2161 /* if I am just reading this block and we don't have
2162 a failed drive, or any pending writes then sidestep the cache */
2163 if (sh->bh_read[i] && !sh->bh_read[i]->b_reqnext &&
2164 ! syncing && !failed && !to_write) {
2165 sh->bh_cache[i]->b_page = sh->bh_read[i]->b_page;
2166 sh->bh_cache[i]->b_data = sh->bh_read[i]->b_data;
2168 #endif
2169 locked++;
2170 PRINTK("Reading block %d (sync=%d)\n",
2171 i, syncing);
2175 set_bit(STRIPE_HANDLE, &sh->state);
2178 /* now to consider writing and what else, if anything should be read */
2179 if (to_write) {
2180 int rcw=0, must_compute=0;
2181 for (i=disks ; i--;) {
2182 dev = &sh->dev[i];
2183 /* Would I have to read this buffer for reconstruct_write */
2184 if (!test_bit(R5_OVERWRITE, &dev->flags)
2185 && i != pd_idx && i != qd_idx
2186 && (!test_bit(R5_LOCKED, &dev->flags)
2187 #if 0
2188 || sh->bh_page[i] != bh->b_page
2189 #endif
2190 ) &&
2191 !test_bit(R5_UPTODATE, &dev->flags)) {
2192 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2193 else {
2194 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
2195 must_compute++;
2199 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2200 (unsigned long long)sh->sector, rcw, must_compute);
2201 set_bit(STRIPE_HANDLE, &sh->state);
2203 if (rcw > 0)
2204 /* want reconstruct write, but need to get some data */
2205 for (i=disks; i--;) {
2206 dev = &sh->dev[i];
2207 if (!test_bit(R5_OVERWRITE, &dev->flags)
2208 && !(failed == 0 && (i == pd_idx || i == qd_idx))
2209 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2210 test_bit(R5_Insync, &dev->flags)) {
2211 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2213 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2214 (unsigned long long)sh->sector, i);
2215 set_bit(R5_LOCKED, &dev->flags);
2216 set_bit(R5_Wantread, &dev->flags);
2217 locked++;
2218 } else {
2219 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2220 (unsigned long long)sh->sector, i);
2221 set_bit(STRIPE_DELAYED, &sh->state);
2222 set_bit(STRIPE_HANDLE, &sh->state);
2226 /* now if nothing is locked, and if we have enough data, we can start a write request */
2227 if (locked == 0 && rcw == 0 &&
2228 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2229 if ( must_compute > 0 ) {
2230 /* We have failed blocks and need to compute them */
2231 switch ( failed ) {
2232 case 0: BUG();
2233 case 1: compute_block_1(sh, failed_num[0], 0); break;
2234 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
2235 default: BUG(); /* This request should have been failed? */
2239 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
2240 compute_parity6(sh, RECONSTRUCT_WRITE);
2241 /* now every locked buffer is ready to be written */
2242 for (i=disks; i--;)
2243 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2244 PRINTK("Writing stripe %llu block %d\n",
2245 (unsigned long long)sh->sector, i);
2246 locked++;
2247 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2249 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2250 set_bit(STRIPE_INSYNC, &sh->state);
2252 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2253 atomic_dec(&conf->preread_active_stripes);
2254 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
2255 md_wakeup_thread(conf->mddev->thread);
2260 /* maybe we need to check and possibly fix the parity for this stripe
2261 * Any reads will already have been scheduled, so we just see if enough data
2262 * is available
2264 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
2265 int update_p = 0, update_q = 0;
2266 struct r5dev *dev;
2268 set_bit(STRIPE_HANDLE, &sh->state);
2270 BUG_ON(failed>2);
2271 BUG_ON(uptodate < disks);
2272 /* Want to check and possibly repair P and Q.
2273 * However there could be one 'failed' device, in which
2274 * case we can only check one of them, possibly using the
2275 * other to generate missing data
2278 /* If !tmp_page, we cannot do the calculations,
2279 * but as we have set STRIPE_HANDLE, we will soon be called
2280 * by stripe_handle with a tmp_page - just wait until then.
2282 if (tmp_page) {
2283 if (failed == q_failed) {
2284 /* The only possible failed device holds 'Q', so it makes
2285 * sense to check P (If anything else were failed, we would
2286 * have used P to recreate it).
2288 compute_block_1(sh, pd_idx, 1);
2289 if (!page_is_zero(sh->dev[pd_idx].page)) {
2290 compute_block_1(sh,pd_idx,0);
2291 update_p = 1;
2294 if (!q_failed && failed < 2) {
2295 /* q is not failed, and we didn't use it to generate
2296 * anything, so it makes sense to check it
2298 memcpy(page_address(tmp_page),
2299 page_address(sh->dev[qd_idx].page),
2300 STRIPE_SIZE);
2301 compute_parity6(sh, UPDATE_PARITY);
2302 if (memcmp(page_address(tmp_page),
2303 page_address(sh->dev[qd_idx].page),
2304 STRIPE_SIZE)!= 0) {
2305 clear_bit(STRIPE_INSYNC, &sh->state);
2306 update_q = 1;
2309 if (update_p || update_q) {
2310 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2311 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2312 /* don't try to repair!! */
2313 update_p = update_q = 0;
2316 /* now write out any block on a failed drive,
2317 * or P or Q if they need it
2320 if (failed == 2) {
2321 dev = &sh->dev[failed_num[1]];
2322 locked++;
2323 set_bit(R5_LOCKED, &dev->flags);
2324 set_bit(R5_Wantwrite, &dev->flags);
2326 if (failed >= 1) {
2327 dev = &sh->dev[failed_num[0]];
2328 locked++;
2329 set_bit(R5_LOCKED, &dev->flags);
2330 set_bit(R5_Wantwrite, &dev->flags);
2333 if (update_p) {
2334 dev = &sh->dev[pd_idx];
2335 locked ++;
2336 set_bit(R5_LOCKED, &dev->flags);
2337 set_bit(R5_Wantwrite, &dev->flags);
2339 if (update_q) {
2340 dev = &sh->dev[qd_idx];
2341 locked++;
2342 set_bit(R5_LOCKED, &dev->flags);
2343 set_bit(R5_Wantwrite, &dev->flags);
2345 clear_bit(STRIPE_DEGRADED, &sh->state);
2347 set_bit(STRIPE_INSYNC, &sh->state);
2351 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2352 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2353 clear_bit(STRIPE_SYNCING, &sh->state);
2356 /* If the failed drives are just a ReadError, then we might need
2357 * to progress the repair/check process
2359 if (failed <= 2 && ! conf->mddev->ro)
2360 for (i=0; i<failed;i++) {
2361 dev = &sh->dev[failed_num[i]];
2362 if (test_bit(R5_ReadError, &dev->flags)
2363 && !test_bit(R5_LOCKED, &dev->flags)
2364 && test_bit(R5_UPTODATE, &dev->flags)
2366 if (!test_bit(R5_ReWrite, &dev->flags)) {
2367 set_bit(R5_Wantwrite, &dev->flags);
2368 set_bit(R5_ReWrite, &dev->flags);
2369 set_bit(R5_LOCKED, &dev->flags);
2370 } else {
2371 /* let's read it back */
2372 set_bit(R5_Wantread, &dev->flags);
2373 set_bit(R5_LOCKED, &dev->flags);
2377 spin_unlock(&sh->lock);
2379 while ((bi=return_bi)) {
2380 int bytes = bi->bi_size;
2382 return_bi = bi->bi_next;
2383 bi->bi_next = NULL;
2384 bi->bi_size = 0;
2385 bi->bi_end_io(bi, bytes, 0);
2387 for (i=disks; i-- ;) {
2388 int rw;
2389 struct bio *bi;
2390 mdk_rdev_t *rdev;
2391 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
2392 rw = 1;
2393 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
2394 rw = 0;
2395 else
2396 continue;
2398 bi = &sh->dev[i].req;
2400 bi->bi_rw = rw;
2401 if (rw)
2402 bi->bi_end_io = raid5_end_write_request;
2403 else
2404 bi->bi_end_io = raid5_end_read_request;
2406 rcu_read_lock();
2407 rdev = rcu_dereference(conf->disks[i].rdev);
2408 if (rdev && test_bit(Faulty, &rdev->flags))
2409 rdev = NULL;
2410 if (rdev)
2411 atomic_inc(&rdev->nr_pending);
2412 rcu_read_unlock();
2414 if (rdev) {
2415 if (syncing)
2416 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
2418 bi->bi_bdev = rdev->bdev;
2419 PRINTK("for %llu schedule op %ld on disc %d\n",
2420 (unsigned long long)sh->sector, bi->bi_rw, i);
2421 atomic_inc(&sh->count);
2422 bi->bi_sector = sh->sector + rdev->data_offset;
2423 bi->bi_flags = 1 << BIO_UPTODATE;
2424 bi->bi_vcnt = 1;
2425 bi->bi_max_vecs = 1;
2426 bi->bi_idx = 0;
2427 bi->bi_io_vec = &sh->dev[i].vec;
2428 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
2429 bi->bi_io_vec[0].bv_offset = 0;
2430 bi->bi_size = STRIPE_SIZE;
2431 bi->bi_next = NULL;
2432 if (rw == WRITE &&
2433 test_bit(R5_ReWrite, &sh->dev[i].flags))
2434 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2435 generic_make_request(bi);
2436 } else {
2437 if (rw == 1)
2438 set_bit(STRIPE_DEGRADED, &sh->state);
2439 PRINTK("skip op %ld on disc %d for sector %llu\n",
2440 bi->bi_rw, i, (unsigned long long)sh->sector);
2441 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2442 set_bit(STRIPE_HANDLE, &sh->state);
2447 static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2449 if (sh->raid_conf->level == 6)
2450 handle_stripe6(sh, tmp_page);
2451 else
2452 handle_stripe5(sh);
2457 static void raid5_activate_delayed(raid5_conf_t *conf)
2459 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2460 while (!list_empty(&conf->delayed_list)) {
2461 struct list_head *l = conf->delayed_list.next;
2462 struct stripe_head *sh;
2463 sh = list_entry(l, struct stripe_head, lru);
2464 list_del_init(l);
2465 clear_bit(STRIPE_DELAYED, &sh->state);
2466 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2467 atomic_inc(&conf->preread_active_stripes);
2468 list_add_tail(&sh->lru, &conf->handle_list);
2473 static void activate_bit_delay(raid5_conf_t *conf)
2475 /* device_lock is held */
2476 struct list_head head;
2477 list_add(&head, &conf->bitmap_list);
2478 list_del_init(&conf->bitmap_list);
2479 while (!list_empty(&head)) {
2480 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
2481 list_del_init(&sh->lru);
2482 atomic_inc(&sh->count);
2483 __release_stripe(conf, sh);
2487 static void unplug_slaves(mddev_t *mddev)
2489 raid5_conf_t *conf = mddev_to_conf(mddev);
2490 int i;
2492 rcu_read_lock();
2493 for (i=0; i<mddev->raid_disks; i++) {
2494 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
2495 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
2496 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
2498 atomic_inc(&rdev->nr_pending);
2499 rcu_read_unlock();
2501 if (r_queue->unplug_fn)
2502 r_queue->unplug_fn(r_queue);
2504 rdev_dec_pending(rdev, mddev);
2505 rcu_read_lock();
2508 rcu_read_unlock();
2511 static void raid5_unplug_device(request_queue_t *q)
2513 mddev_t *mddev = q->queuedata;
2514 raid5_conf_t *conf = mddev_to_conf(mddev);
2515 unsigned long flags;
2517 spin_lock_irqsave(&conf->device_lock, flags);
2519 if (blk_remove_plug(q)) {
2520 conf->seq_flush++;
2521 raid5_activate_delayed(conf);
2523 md_wakeup_thread(mddev->thread);
2525 spin_unlock_irqrestore(&conf->device_lock, flags);
2527 unplug_slaves(mddev);
2530 static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
2531 sector_t *error_sector)
2533 mddev_t *mddev = q->queuedata;
2534 raid5_conf_t *conf = mddev_to_conf(mddev);
2535 int i, ret = 0;
2537 rcu_read_lock();
2538 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
2539 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
2540 if (rdev && !test_bit(Faulty, &rdev->flags)) {
2541 struct block_device *bdev = rdev->bdev;
2542 request_queue_t *r_queue = bdev_get_queue(bdev);
2544 if (!r_queue->issue_flush_fn)
2545 ret = -EOPNOTSUPP;
2546 else {
2547 atomic_inc(&rdev->nr_pending);
2548 rcu_read_unlock();
2549 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
2550 error_sector);
2551 rdev_dec_pending(rdev, mddev);
2552 rcu_read_lock();
2556 rcu_read_unlock();
2557 return ret;
2560 static int make_request(request_queue_t *q, struct bio * bi)
2562 mddev_t *mddev = q->queuedata;
2563 raid5_conf_t *conf = mddev_to_conf(mddev);
2564 unsigned int dd_idx, pd_idx;
2565 sector_t new_sector;
2566 sector_t logical_sector, last_sector;
2567 struct stripe_head *sh;
2568 const int rw = bio_data_dir(bi);
2569 int remaining;
2571 if (unlikely(bio_barrier(bi))) {
2572 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
2573 return 0;
2576 md_write_start(mddev, bi);
2578 disk_stat_inc(mddev->gendisk, ios[rw]);
2579 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
2581 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
2582 last_sector = bi->bi_sector + (bi->bi_size>>9);
2583 bi->bi_next = NULL;
2584 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
2586 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
2587 DEFINE_WAIT(w);
2588 int disks, data_disks;
2590 retry:
2591 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
2592 if (likely(conf->expand_progress == MaxSector))
2593 disks = conf->raid_disks;
2594 else {
2595 /* spinlock is needed as expand_progress may be
2596 * 64bit on a 32bit platform, and so it might be
2597 * possible to see a half-updated value
2598 * Ofcourse expand_progress could change after
2599 * the lock is dropped, so once we get a reference
2600 * to the stripe that we think it is, we will have
2601 * to check again.
2603 spin_lock_irq(&conf->device_lock);
2604 disks = conf->raid_disks;
2605 if (logical_sector >= conf->expand_progress)
2606 disks = conf->previous_raid_disks;
2607 else {
2608 if (logical_sector >= conf->expand_lo) {
2609 spin_unlock_irq(&conf->device_lock);
2610 schedule();
2611 goto retry;
2614 spin_unlock_irq(&conf->device_lock);
2616 data_disks = disks - conf->max_degraded;
2618 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
2619 &dd_idx, &pd_idx, conf);
2620 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2621 (unsigned long long)new_sector,
2622 (unsigned long long)logical_sector);
2624 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
2625 if (sh) {
2626 if (unlikely(conf->expand_progress != MaxSector)) {
2627 /* expansion might have moved on while waiting for a
2628 * stripe, so we must do the range check again.
2629 * Expansion could still move past after this
2630 * test, but as we are holding a reference to
2631 * 'sh', we know that if that happens,
2632 * STRIPE_EXPANDING will get set and the expansion
2633 * won't proceed until we finish with the stripe.
2635 int must_retry = 0;
2636 spin_lock_irq(&conf->device_lock);
2637 if (logical_sector < conf->expand_progress &&
2638 disks == conf->previous_raid_disks)
2639 /* mismatch, need to try again */
2640 must_retry = 1;
2641 spin_unlock_irq(&conf->device_lock);
2642 if (must_retry) {
2643 release_stripe(sh);
2644 goto retry;
2647 /* FIXME what if we get a false positive because these
2648 * are being updated.
2650 if (logical_sector >= mddev->suspend_lo &&
2651 logical_sector < mddev->suspend_hi) {
2652 release_stripe(sh);
2653 schedule();
2654 goto retry;
2657 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
2658 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
2659 /* Stripe is busy expanding or
2660 * add failed due to overlap. Flush everything
2661 * and wait a while
2663 raid5_unplug_device(mddev->queue);
2664 release_stripe(sh);
2665 schedule();
2666 goto retry;
2668 finish_wait(&conf->wait_for_overlap, &w);
2669 handle_stripe(sh, NULL);
2670 release_stripe(sh);
2671 } else {
2672 /* cannot get stripe for read-ahead, just give-up */
2673 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2674 finish_wait(&conf->wait_for_overlap, &w);
2675 break;
2679 spin_lock_irq(&conf->device_lock);
2680 remaining = --bi->bi_phys_segments;
2681 spin_unlock_irq(&conf->device_lock);
2682 if (remaining == 0) {
2683 int bytes = bi->bi_size;
2685 if ( rw == WRITE )
2686 md_write_end(mddev);
2687 bi->bi_size = 0;
2688 bi->bi_end_io(bi, bytes, 0);
2690 return 0;
2693 static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
2695 /* reshaping is quite different to recovery/resync so it is
2696 * handled quite separately ... here.
2698 * On each call to sync_request, we gather one chunk worth of
2699 * destination stripes and flag them as expanding.
2700 * Then we find all the source stripes and request reads.
2701 * As the reads complete, handle_stripe will copy the data
2702 * into the destination stripe and release that stripe.
2704 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2705 struct stripe_head *sh;
2706 int pd_idx;
2707 sector_t first_sector, last_sector;
2708 int raid_disks;
2709 int data_disks;
2710 int i;
2711 int dd_idx;
2712 sector_t writepos, safepos, gap;
2714 if (sector_nr == 0 &&
2715 conf->expand_progress != 0) {
2716 /* restarting in the middle, skip the initial sectors */
2717 sector_nr = conf->expand_progress;
2718 sector_div(sector_nr, conf->raid_disks-1);
2719 *skipped = 1;
2720 return sector_nr;
2723 /* we update the metadata when there is more than 3Meg
2724 * in the block range (that is rather arbitrary, should
2725 * probably be time based) or when the data about to be
2726 * copied would over-write the source of the data at
2727 * the front of the range.
2728 * i.e. one new_stripe forward from expand_progress new_maps
2729 * to after where expand_lo old_maps to
2731 writepos = conf->expand_progress +
2732 conf->chunk_size/512*(conf->raid_disks-1);
2733 sector_div(writepos, conf->raid_disks-1);
2734 safepos = conf->expand_lo;
2735 sector_div(safepos, conf->previous_raid_disks-1);
2736 gap = conf->expand_progress - conf->expand_lo;
2738 if (writepos >= safepos ||
2739 gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
2740 /* Cannot proceed until we've updated the superblock... */
2741 wait_event(conf->wait_for_overlap,
2742 atomic_read(&conf->reshape_stripes)==0);
2743 mddev->reshape_position = conf->expand_progress;
2744 mddev->sb_dirty = 1;
2745 md_wakeup_thread(mddev->thread);
2746 wait_event(mddev->sb_wait, mddev->sb_dirty == 0 ||
2747 kthread_should_stop());
2748 spin_lock_irq(&conf->device_lock);
2749 conf->expand_lo = mddev->reshape_position;
2750 spin_unlock_irq(&conf->device_lock);
2751 wake_up(&conf->wait_for_overlap);
2754 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
2755 int j;
2756 int skipped = 0;
2757 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
2758 sh = get_active_stripe(conf, sector_nr+i,
2759 conf->raid_disks, pd_idx, 0);
2760 set_bit(STRIPE_EXPANDING, &sh->state);
2761 atomic_inc(&conf->reshape_stripes);
2762 /* If any of this stripe is beyond the end of the old
2763 * array, then we need to zero those blocks
2765 for (j=sh->disks; j--;) {
2766 sector_t s;
2767 if (j == sh->pd_idx)
2768 continue;
2769 s = compute_blocknr(sh, j);
2770 if (s < (mddev->array_size<<1)) {
2771 skipped = 1;
2772 continue;
2774 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
2775 set_bit(R5_Expanded, &sh->dev[j].flags);
2776 set_bit(R5_UPTODATE, &sh->dev[j].flags);
2778 if (!skipped) {
2779 set_bit(STRIPE_EXPAND_READY, &sh->state);
2780 set_bit(STRIPE_HANDLE, &sh->state);
2782 release_stripe(sh);
2784 spin_lock_irq(&conf->device_lock);
2785 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
2786 spin_unlock_irq(&conf->device_lock);
2787 /* Ok, those stripe are ready. We can start scheduling
2788 * reads on the source stripes.
2789 * The source stripes are determined by mapping the first and last
2790 * block on the destination stripes.
2792 raid_disks = conf->previous_raid_disks;
2793 data_disks = raid_disks - 1;
2794 first_sector =
2795 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
2796 raid_disks, data_disks,
2797 &dd_idx, &pd_idx, conf);
2798 last_sector =
2799 raid5_compute_sector((sector_nr+conf->chunk_size/512)
2800 *(conf->raid_disks-1) -1,
2801 raid_disks, data_disks,
2802 &dd_idx, &pd_idx, conf);
2803 if (last_sector >= (mddev->size<<1))
2804 last_sector = (mddev->size<<1)-1;
2805 while (first_sector <= last_sector) {
2806 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
2807 sh = get_active_stripe(conf, first_sector,
2808 conf->previous_raid_disks, pd_idx, 0);
2809 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2810 set_bit(STRIPE_HANDLE, &sh->state);
2811 release_stripe(sh);
2812 first_sector += STRIPE_SECTORS;
2814 return conf->chunk_size>>9;
2817 /* FIXME go_faster isn't used */
2818 static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
2820 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2821 struct stripe_head *sh;
2822 int pd_idx;
2823 int raid_disks = conf->raid_disks;
2824 sector_t max_sector = mddev->size << 1;
2825 int sync_blocks;
2826 int still_degraded = 0;
2827 int i;
2829 if (sector_nr >= max_sector) {
2830 /* just being told to finish up .. nothing much to do */
2831 unplug_slaves(mddev);
2832 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
2833 end_reshape(conf);
2834 return 0;
2837 if (mddev->curr_resync < max_sector) /* aborted */
2838 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
2839 &sync_blocks, 1);
2840 else /* completed sync */
2841 conf->fullsync = 0;
2842 bitmap_close_sync(mddev->bitmap);
2844 return 0;
2847 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
2848 return reshape_request(mddev, sector_nr, skipped);
2850 /* if there is too many failed drives and we are trying
2851 * to resync, then assert that we are finished, because there is
2852 * nothing we can do.
2854 if (mddev->degraded >= conf->max_degraded &&
2855 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
2856 sector_t rv = (mddev->size << 1) - sector_nr;
2857 *skipped = 1;
2858 return rv;
2860 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
2861 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
2862 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
2863 /* we can skip this block, and probably more */
2864 sync_blocks /= STRIPE_SECTORS;
2865 *skipped = 1;
2866 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
2869 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
2870 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
2871 if (sh == NULL) {
2872 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
2873 /* make sure we don't swamp the stripe cache if someone else
2874 * is trying to get access
2876 schedule_timeout_uninterruptible(1);
2878 /* Need to check if array will still be degraded after recovery/resync
2879 * We don't need to check the 'failed' flag as when that gets set,
2880 * recovery aborts.
2882 for (i=0; i<mddev->raid_disks; i++)
2883 if (conf->disks[i].rdev == NULL)
2884 still_degraded = 1;
2886 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
2888 spin_lock(&sh->lock);
2889 set_bit(STRIPE_SYNCING, &sh->state);
2890 clear_bit(STRIPE_INSYNC, &sh->state);
2891 spin_unlock(&sh->lock);
2893 handle_stripe(sh, NULL);
2894 release_stripe(sh);
2896 return STRIPE_SECTORS;
2900 * This is our raid5 kernel thread.
2902 * We scan the hash table for stripes which can be handled now.
2903 * During the scan, completed stripes are saved for us by the interrupt
2904 * handler, so that they will not have to wait for our next wakeup.
2906 static void raid5d (mddev_t *mddev)
2908 struct stripe_head *sh;
2909 raid5_conf_t *conf = mddev_to_conf(mddev);
2910 int handled;
2912 PRINTK("+++ raid5d active\n");
2914 md_check_recovery(mddev);
2916 handled = 0;
2917 spin_lock_irq(&conf->device_lock);
2918 while (1) {
2919 struct list_head *first;
2921 if (conf->seq_flush - conf->seq_write > 0) {
2922 int seq = conf->seq_flush;
2923 spin_unlock_irq(&conf->device_lock);
2924 bitmap_unplug(mddev->bitmap);
2925 spin_lock_irq(&conf->device_lock);
2926 conf->seq_write = seq;
2927 activate_bit_delay(conf);
2930 if (list_empty(&conf->handle_list) &&
2931 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
2932 !blk_queue_plugged(mddev->queue) &&
2933 !list_empty(&conf->delayed_list))
2934 raid5_activate_delayed(conf);
2936 if (list_empty(&conf->handle_list))
2937 break;
2939 first = conf->handle_list.next;
2940 sh = list_entry(first, struct stripe_head, lru);
2942 list_del_init(first);
2943 atomic_inc(&sh->count);
2944 BUG_ON(atomic_read(&sh->count)!= 1);
2945 spin_unlock_irq(&conf->device_lock);
2947 handled++;
2948 handle_stripe(sh, conf->spare_page);
2949 release_stripe(sh);
2951 spin_lock_irq(&conf->device_lock);
2953 PRINTK("%d stripes handled\n", handled);
2955 spin_unlock_irq(&conf->device_lock);
2957 unplug_slaves(mddev);
2959 PRINTK("--- raid5d inactive\n");
2962 static ssize_t
2963 raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
2965 raid5_conf_t *conf = mddev_to_conf(mddev);
2966 if (conf)
2967 return sprintf(page, "%d\n", conf->max_nr_stripes);
2968 else
2969 return 0;
2972 static ssize_t
2973 raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
2975 raid5_conf_t *conf = mddev_to_conf(mddev);
2976 char *end;
2977 int new;
2978 if (len >= PAGE_SIZE)
2979 return -EINVAL;
2980 if (!conf)
2981 return -ENODEV;
2983 new = simple_strtoul(page, &end, 10);
2984 if (!*page || (*end && *end != '\n') )
2985 return -EINVAL;
2986 if (new <= 16 || new > 32768)
2987 return -EINVAL;
2988 while (new < conf->max_nr_stripes) {
2989 if (drop_one_stripe(conf))
2990 conf->max_nr_stripes--;
2991 else
2992 break;
2994 while (new > conf->max_nr_stripes) {
2995 if (grow_one_stripe(conf))
2996 conf->max_nr_stripes++;
2997 else break;
2999 return len;
3002 static struct md_sysfs_entry
3003 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3004 raid5_show_stripe_cache_size,
3005 raid5_store_stripe_cache_size);
3007 static ssize_t
3008 stripe_cache_active_show(mddev_t *mddev, char *page)
3010 raid5_conf_t *conf = mddev_to_conf(mddev);
3011 if (conf)
3012 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3013 else
3014 return 0;
3017 static struct md_sysfs_entry
3018 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
3020 static struct attribute *raid5_attrs[] = {
3021 &raid5_stripecache_size.attr,
3022 &raid5_stripecache_active.attr,
3023 NULL,
3025 static struct attribute_group raid5_attrs_group = {
3026 .name = NULL,
3027 .attrs = raid5_attrs,
3030 static int run(mddev_t *mddev)
3032 raid5_conf_t *conf;
3033 int raid_disk, memory;
3034 mdk_rdev_t *rdev;
3035 struct disk_info *disk;
3036 struct list_head *tmp;
3038 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3039 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
3040 mdname(mddev), mddev->level);
3041 return -EIO;
3044 if (mddev->reshape_position != MaxSector) {
3045 /* Check that we can continue the reshape.
3046 * Currently only disks can change, it must
3047 * increase, and we must be past the point where
3048 * a stripe over-writes itself
3050 sector_t here_new, here_old;
3051 int old_disks;
3053 if (mddev->new_level != mddev->level ||
3054 mddev->new_layout != mddev->layout ||
3055 mddev->new_chunk != mddev->chunk_size) {
3056 printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
3057 mdname(mddev));
3058 return -EINVAL;
3060 if (mddev->delta_disks <= 0) {
3061 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3062 mdname(mddev));
3063 return -EINVAL;
3065 old_disks = mddev->raid_disks - mddev->delta_disks;
3066 /* reshape_position must be on a new-stripe boundary, and one
3067 * further up in new geometry must map after here in old geometry.
3069 here_new = mddev->reshape_position;
3070 if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
3071 printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
3072 return -EINVAL;
3074 /* here_new is the stripe we will write to */
3075 here_old = mddev->reshape_position;
3076 sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
3077 /* here_old is the first stripe that we might need to read from */
3078 if (here_new >= here_old) {
3079 /* Reading from the same stripe as writing to - bad */
3080 printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
3081 return -EINVAL;
3083 printk(KERN_INFO "raid5: reshape will continue\n");
3084 /* OK, we should be able to continue; */
3088 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
3089 if ((conf = mddev->private) == NULL)
3090 goto abort;
3091 if (mddev->reshape_position == MaxSector) {
3092 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
3093 } else {
3094 conf->raid_disks = mddev->raid_disks;
3095 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
3098 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
3099 GFP_KERNEL);
3100 if (!conf->disks)
3101 goto abort;
3103 conf->mddev = mddev;
3105 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
3106 goto abort;
3108 if (mddev->level == 6) {
3109 conf->spare_page = alloc_page(GFP_KERNEL);
3110 if (!conf->spare_page)
3111 goto abort;
3113 spin_lock_init(&conf->device_lock);
3114 init_waitqueue_head(&conf->wait_for_stripe);
3115 init_waitqueue_head(&conf->wait_for_overlap);
3116 INIT_LIST_HEAD(&conf->handle_list);
3117 INIT_LIST_HEAD(&conf->delayed_list);
3118 INIT_LIST_HEAD(&conf->bitmap_list);
3119 INIT_LIST_HEAD(&conf->inactive_list);
3120 atomic_set(&conf->active_stripes, 0);
3121 atomic_set(&conf->preread_active_stripes, 0);
3123 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
3125 ITERATE_RDEV(mddev,rdev,tmp) {
3126 raid_disk = rdev->raid_disk;
3127 if (raid_disk >= conf->raid_disks
3128 || raid_disk < 0)
3129 continue;
3130 disk = conf->disks + raid_disk;
3132 disk->rdev = rdev;
3134 if (test_bit(In_sync, &rdev->flags)) {
3135 char b[BDEVNAME_SIZE];
3136 printk(KERN_INFO "raid5: device %s operational as raid"
3137 " disk %d\n", bdevname(rdev->bdev,b),
3138 raid_disk);
3139 conf->working_disks++;
3144 * 0 for a fully functional array, 1 or 2 for a degraded array.
3146 mddev->degraded = conf->failed_disks = conf->raid_disks - conf->working_disks;
3147 conf->mddev = mddev;
3148 conf->chunk_size = mddev->chunk_size;
3149 conf->level = mddev->level;
3150 if (conf->level == 6)
3151 conf->max_degraded = 2;
3152 else
3153 conf->max_degraded = 1;
3154 conf->algorithm = mddev->layout;
3155 conf->max_nr_stripes = NR_STRIPES;
3156 conf->expand_progress = mddev->reshape_position;
3158 /* device size must be a multiple of chunk size */
3159 mddev->size &= ~(mddev->chunk_size/1024 -1);
3160 mddev->resync_max_sectors = mddev->size << 1;
3162 if (conf->level == 6 && conf->raid_disks < 4) {
3163 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
3164 mdname(mddev), conf->raid_disks);
3165 goto abort;
3167 if (!conf->chunk_size || conf->chunk_size % 4) {
3168 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
3169 conf->chunk_size, mdname(mddev));
3170 goto abort;
3172 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
3173 printk(KERN_ERR
3174 "raid5: unsupported parity algorithm %d for %s\n",
3175 conf->algorithm, mdname(mddev));
3176 goto abort;
3178 if (mddev->degraded > conf->max_degraded) {
3179 printk(KERN_ERR "raid5: not enough operational devices for %s"
3180 " (%d/%d failed)\n",
3181 mdname(mddev), conf->failed_disks, conf->raid_disks);
3182 goto abort;
3185 if (mddev->degraded > 0 &&
3186 mddev->recovery_cp != MaxSector) {
3187 if (mddev->ok_start_degraded)
3188 printk(KERN_WARNING
3189 "raid5: starting dirty degraded array: %s"
3190 "- data corruption possible.\n",
3191 mdname(mddev));
3192 else {
3193 printk(KERN_ERR
3194 "raid5: cannot start dirty degraded array for %s\n",
3195 mdname(mddev));
3196 goto abort;
3201 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
3202 if (!mddev->thread) {
3203 printk(KERN_ERR
3204 "raid5: couldn't allocate thread for %s\n",
3205 mdname(mddev));
3206 goto abort;
3209 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
3210 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
3211 if (grow_stripes(conf, conf->max_nr_stripes)) {
3212 printk(KERN_ERR
3213 "raid5: couldn't allocate %dkB for buffers\n", memory);
3214 shrink_stripes(conf);
3215 md_unregister_thread(mddev->thread);
3216 goto abort;
3217 } else
3218 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
3219 memory, mdname(mddev));
3221 if (mddev->degraded == 0)
3222 printk("raid5: raid level %d set %s active with %d out of %d"
3223 " devices, algorithm %d\n", conf->level, mdname(mddev),
3224 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
3225 conf->algorithm);
3226 else
3227 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
3228 " out of %d devices, algorithm %d\n", conf->level,
3229 mdname(mddev), mddev->raid_disks - mddev->degraded,
3230 mddev->raid_disks, conf->algorithm);
3232 print_raid5_conf(conf);
3234 if (conf->expand_progress != MaxSector) {
3235 printk("...ok start reshape thread\n");
3236 conf->expand_lo = conf->expand_progress;
3237 atomic_set(&conf->reshape_stripes, 0);
3238 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3239 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3240 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3241 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3242 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3243 "%s_reshape");
3246 /* read-ahead size must cover two whole stripes, which is
3247 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3250 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3251 int stripe = data_disks *
3252 (mddev->chunk_size / PAGE_SIZE);
3253 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3254 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3257 /* Ok, everything is just fine now */
3258 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
3260 mddev->queue->unplug_fn = raid5_unplug_device;
3261 mddev->queue->issue_flush_fn = raid5_issue_flush;
3262 mddev->array_size = mddev->size * (conf->previous_raid_disks -
3263 conf->max_degraded);
3265 return 0;
3266 abort:
3267 if (conf) {
3268 print_raid5_conf(conf);
3269 safe_put_page(conf->spare_page);
3270 kfree(conf->disks);
3271 kfree(conf->stripe_hashtbl);
3272 kfree(conf);
3274 mddev->private = NULL;
3275 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
3276 return -EIO;
3281 static int stop(mddev_t *mddev)
3283 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3285 md_unregister_thread(mddev->thread);
3286 mddev->thread = NULL;
3287 shrink_stripes(conf);
3288 kfree(conf->stripe_hashtbl);
3289 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3290 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
3291 kfree(conf->disks);
3292 kfree(conf);
3293 mddev->private = NULL;
3294 return 0;
3297 #if RAID5_DEBUG
3298 static void print_sh (struct seq_file *seq, struct stripe_head *sh)
3300 int i;
3302 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
3303 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
3304 seq_printf(seq, "sh %llu, count %d.\n",
3305 (unsigned long long)sh->sector, atomic_read(&sh->count));
3306 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
3307 for (i = 0; i < sh->disks; i++) {
3308 seq_printf(seq, "(cache%d: %p %ld) ",
3309 i, sh->dev[i].page, sh->dev[i].flags);
3311 seq_printf(seq, "\n");
3314 static void printall (struct seq_file *seq, raid5_conf_t *conf)
3316 struct stripe_head *sh;
3317 struct hlist_node *hn;
3318 int i;
3320 spin_lock_irq(&conf->device_lock);
3321 for (i = 0; i < NR_HASH; i++) {
3322 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
3323 if (sh->raid_conf != conf)
3324 continue;
3325 print_sh(seq, sh);
3328 spin_unlock_irq(&conf->device_lock);
3330 #endif
3332 static void status (struct seq_file *seq, mddev_t *mddev)
3334 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3335 int i;
3337 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
3338 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->working_disks);
3339 for (i = 0; i < conf->raid_disks; i++)
3340 seq_printf (seq, "%s",
3341 conf->disks[i].rdev &&
3342 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
3343 seq_printf (seq, "]");
3344 #if RAID5_DEBUG
3345 seq_printf (seq, "\n");
3346 printall(seq, conf);
3347 #endif
3350 static void print_raid5_conf (raid5_conf_t *conf)
3352 int i;
3353 struct disk_info *tmp;
3355 printk("RAID5 conf printout:\n");
3356 if (!conf) {
3357 printk("(conf==NULL)\n");
3358 return;
3360 printk(" --- rd:%d wd:%d fd:%d\n", conf->raid_disks,
3361 conf->working_disks, conf->failed_disks);
3363 for (i = 0; i < conf->raid_disks; i++) {
3364 char b[BDEVNAME_SIZE];
3365 tmp = conf->disks + i;
3366 if (tmp->rdev)
3367 printk(" disk %d, o:%d, dev:%s\n",
3368 i, !test_bit(Faulty, &tmp->rdev->flags),
3369 bdevname(tmp->rdev->bdev,b));
3373 static int raid5_spare_active(mddev_t *mddev)
3375 int i;
3376 raid5_conf_t *conf = mddev->private;
3377 struct disk_info *tmp;
3379 for (i = 0; i < conf->raid_disks; i++) {
3380 tmp = conf->disks + i;
3381 if (tmp->rdev
3382 && !test_bit(Faulty, &tmp->rdev->flags)
3383 && !test_bit(In_sync, &tmp->rdev->flags)) {
3384 mddev->degraded--;
3385 conf->failed_disks--;
3386 conf->working_disks++;
3387 set_bit(In_sync, &tmp->rdev->flags);
3390 print_raid5_conf(conf);
3391 return 0;
3394 static int raid5_remove_disk(mddev_t *mddev, int number)
3396 raid5_conf_t *conf = mddev->private;
3397 int err = 0;
3398 mdk_rdev_t *rdev;
3399 struct disk_info *p = conf->disks + number;
3401 print_raid5_conf(conf);
3402 rdev = p->rdev;
3403 if (rdev) {
3404 if (test_bit(In_sync, &rdev->flags) ||
3405 atomic_read(&rdev->nr_pending)) {
3406 err = -EBUSY;
3407 goto abort;
3409 p->rdev = NULL;
3410 synchronize_rcu();
3411 if (atomic_read(&rdev->nr_pending)) {
3412 /* lost the race, try later */
3413 err = -EBUSY;
3414 p->rdev = rdev;
3417 abort:
3419 print_raid5_conf(conf);
3420 return err;
3423 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
3425 raid5_conf_t *conf = mddev->private;
3426 int found = 0;
3427 int disk;
3428 struct disk_info *p;
3430 if (mddev->degraded > conf->max_degraded)
3431 /* no point adding a device */
3432 return 0;
3435 * find the disk ... but prefer rdev->saved_raid_disk
3436 * if possible.
3438 if (rdev->saved_raid_disk >= 0 &&
3439 conf->disks[rdev->saved_raid_disk].rdev == NULL)
3440 disk = rdev->saved_raid_disk;
3441 else
3442 disk = 0;
3443 for ( ; disk < conf->raid_disks; disk++)
3444 if ((p=conf->disks + disk)->rdev == NULL) {
3445 clear_bit(In_sync, &rdev->flags);
3446 rdev->raid_disk = disk;
3447 found = 1;
3448 if (rdev->saved_raid_disk != disk)
3449 conf->fullsync = 1;
3450 rcu_assign_pointer(p->rdev, rdev);
3451 break;
3453 print_raid5_conf(conf);
3454 return found;
3457 static int raid5_resize(mddev_t *mddev, sector_t sectors)
3459 /* no resync is happening, and there is enough space
3460 * on all devices, so we can resize.
3461 * We need to make sure resync covers any new space.
3462 * If the array is shrinking we should possibly wait until
3463 * any io in the removed space completes, but it hardly seems
3464 * worth it.
3466 raid5_conf_t *conf = mddev_to_conf(mddev);
3468 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
3469 mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
3470 set_capacity(mddev->gendisk, mddev->array_size << 1);
3471 mddev->changed = 1;
3472 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
3473 mddev->recovery_cp = mddev->size << 1;
3474 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3476 mddev->size = sectors /2;
3477 mddev->resync_max_sectors = sectors;
3478 return 0;
3481 #ifdef CONFIG_MD_RAID5_RESHAPE
3482 static int raid5_check_reshape(mddev_t *mddev)
3484 raid5_conf_t *conf = mddev_to_conf(mddev);
3485 int err;
3487 if (mddev->delta_disks < 0 ||
3488 mddev->new_level != mddev->level)
3489 return -EINVAL; /* Cannot shrink array or change level yet */
3490 if (mddev->delta_disks == 0)
3491 return 0; /* nothing to do */
3493 /* Can only proceed if there are plenty of stripe_heads.
3494 * We need a minimum of one full stripe,, and for sensible progress
3495 * it is best to have about 4 times that.
3496 * If we require 4 times, then the default 256 4K stripe_heads will
3497 * allow for chunk sizes up to 256K, which is probably OK.
3498 * If the chunk size is greater, user-space should request more
3499 * stripe_heads first.
3501 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
3502 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
3503 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
3504 (mddev->chunk_size / STRIPE_SIZE)*4);
3505 return -ENOSPC;
3508 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
3509 if (err)
3510 return err;
3512 /* looks like we might be able to manage this */
3513 return 0;
3516 static int raid5_start_reshape(mddev_t *mddev)
3518 raid5_conf_t *conf = mddev_to_conf(mddev);
3519 mdk_rdev_t *rdev;
3520 struct list_head *rtmp;
3521 int spares = 0;
3522 int added_devices = 0;
3524 if (mddev->degraded ||
3525 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3526 return -EBUSY;
3528 ITERATE_RDEV(mddev, rdev, rtmp)
3529 if (rdev->raid_disk < 0 &&
3530 !test_bit(Faulty, &rdev->flags))
3531 spares++;
3533 if (spares < mddev->delta_disks-1)
3534 /* Not enough devices even to make a degraded array
3535 * of that size
3537 return -EINVAL;
3539 atomic_set(&conf->reshape_stripes, 0);
3540 spin_lock_irq(&conf->device_lock);
3541 conf->previous_raid_disks = conf->raid_disks;
3542 conf->raid_disks += mddev->delta_disks;
3543 conf->expand_progress = 0;
3544 conf->expand_lo = 0;
3545 spin_unlock_irq(&conf->device_lock);
3547 /* Add some new drives, as many as will fit.
3548 * We know there are enough to make the newly sized array work.
3550 ITERATE_RDEV(mddev, rdev, rtmp)
3551 if (rdev->raid_disk < 0 &&
3552 !test_bit(Faulty, &rdev->flags)) {
3553 if (raid5_add_disk(mddev, rdev)) {
3554 char nm[20];
3555 set_bit(In_sync, &rdev->flags);
3556 conf->working_disks++;
3557 added_devices++;
3558 rdev->recovery_offset = 0;
3559 sprintf(nm, "rd%d", rdev->raid_disk);
3560 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3561 } else
3562 break;
3565 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
3566 mddev->raid_disks = conf->raid_disks;
3567 mddev->reshape_position = 0;
3568 mddev->sb_dirty = 1;
3570 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3571 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3572 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3573 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3574 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3575 "%s_reshape");
3576 if (!mddev->sync_thread) {
3577 mddev->recovery = 0;
3578 spin_lock_irq(&conf->device_lock);
3579 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
3580 conf->expand_progress = MaxSector;
3581 spin_unlock_irq(&conf->device_lock);
3582 return -EAGAIN;
3584 md_wakeup_thread(mddev->sync_thread);
3585 md_new_event(mddev);
3586 return 0;
3588 #endif
3590 static void end_reshape(raid5_conf_t *conf)
3592 struct block_device *bdev;
3594 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
3595 conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
3596 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
3597 conf->mddev->changed = 1;
3599 bdev = bdget_disk(conf->mddev->gendisk, 0);
3600 if (bdev) {
3601 mutex_lock(&bdev->bd_inode->i_mutex);
3602 i_size_write(bdev->bd_inode, conf->mddev->array_size << 10);
3603 mutex_unlock(&bdev->bd_inode->i_mutex);
3604 bdput(bdev);
3606 spin_lock_irq(&conf->device_lock);
3607 conf->expand_progress = MaxSector;
3608 spin_unlock_irq(&conf->device_lock);
3609 conf->mddev->reshape_position = MaxSector;
3611 /* read-ahead size must cover two whole stripes, which is
3612 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3615 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3616 int stripe = data_disks *
3617 (conf->mddev->chunk_size / PAGE_SIZE);
3618 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3619 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3624 static void raid5_quiesce(mddev_t *mddev, int state)
3626 raid5_conf_t *conf = mddev_to_conf(mddev);
3628 switch(state) {
3629 case 2: /* resume for a suspend */
3630 wake_up(&conf->wait_for_overlap);
3631 break;
3633 case 1: /* stop all writes */
3634 spin_lock_irq(&conf->device_lock);
3635 conf->quiesce = 1;
3636 wait_event_lock_irq(conf->wait_for_stripe,
3637 atomic_read(&conf->active_stripes) == 0,
3638 conf->device_lock, /* nothing */);
3639 spin_unlock_irq(&conf->device_lock);
3640 break;
3642 case 0: /* re-enable writes */
3643 spin_lock_irq(&conf->device_lock);
3644 conf->quiesce = 0;
3645 wake_up(&conf->wait_for_stripe);
3646 wake_up(&conf->wait_for_overlap);
3647 spin_unlock_irq(&conf->device_lock);
3648 break;
3652 static struct mdk_personality raid6_personality =
3654 .name = "raid6",
3655 .level = 6,
3656 .owner = THIS_MODULE,
3657 .make_request = make_request,
3658 .run = run,
3659 .stop = stop,
3660 .status = status,
3661 .error_handler = error,
3662 .hot_add_disk = raid5_add_disk,
3663 .hot_remove_disk= raid5_remove_disk,
3664 .spare_active = raid5_spare_active,
3665 .sync_request = sync_request,
3666 .resize = raid5_resize,
3667 .quiesce = raid5_quiesce,
3669 static struct mdk_personality raid5_personality =
3671 .name = "raid5",
3672 .level = 5,
3673 .owner = THIS_MODULE,
3674 .make_request = make_request,
3675 .run = run,
3676 .stop = stop,
3677 .status = status,
3678 .error_handler = error,
3679 .hot_add_disk = raid5_add_disk,
3680 .hot_remove_disk= raid5_remove_disk,
3681 .spare_active = raid5_spare_active,
3682 .sync_request = sync_request,
3683 .resize = raid5_resize,
3684 #ifdef CONFIG_MD_RAID5_RESHAPE
3685 .check_reshape = raid5_check_reshape,
3686 .start_reshape = raid5_start_reshape,
3687 #endif
3688 .quiesce = raid5_quiesce,
3691 static struct mdk_personality raid4_personality =
3693 .name = "raid4",
3694 .level = 4,
3695 .owner = THIS_MODULE,
3696 .make_request = make_request,
3697 .run = run,
3698 .stop = stop,
3699 .status = status,
3700 .error_handler = error,
3701 .hot_add_disk = raid5_add_disk,
3702 .hot_remove_disk= raid5_remove_disk,
3703 .spare_active = raid5_spare_active,
3704 .sync_request = sync_request,
3705 .resize = raid5_resize,
3706 .quiesce = raid5_quiesce,
3709 static int __init raid5_init(void)
3711 int e;
3713 e = raid6_select_algo();
3714 if ( e )
3715 return e;
3716 register_md_personality(&raid6_personality);
3717 register_md_personality(&raid5_personality);
3718 register_md_personality(&raid4_personality);
3719 return 0;
3722 static void raid5_exit(void)
3724 unregister_md_personality(&raid6_personality);
3725 unregister_md_personality(&raid5_personality);
3726 unregister_md_personality(&raid4_personality);
3729 module_init(raid5_init);
3730 module_exit(raid5_exit);
3731 MODULE_LICENSE("GPL");
3732 MODULE_ALIAS("md-personality-4"); /* RAID5 */
3733 MODULE_ALIAS("md-raid5");
3734 MODULE_ALIAS("md-raid4");
3735 MODULE_ALIAS("md-level-5");
3736 MODULE_ALIAS("md-level-4");
3737 MODULE_ALIAS("md-personality-8"); /* RAID6 */
3738 MODULE_ALIAS("md-raid6");
3739 MODULE_ALIAS("md-level-6");
3741 /* This used to be two separate modules, they were: */
3742 MODULE_ALIAS("raid5");
3743 MODULE_ALIAS("raid6");