[PATCH] md: remove unnecessary printk when raid5 gets an unaligned read.
[linux-2.6/verdex.git] / drivers / md / raid5.c
blob467c16982d02e541bc47f4e32c0c90939fb68d52
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 * BITMAP UNPLUGGING:
24 * The sequencing for updating the bitmap reliably is a little
25 * subtle (and I got it wrong the first time) so it deserves some
26 * explanation.
28 * We group bitmap updates into batches. Each batch has a number.
29 * We may write out several batches at once, but that isn't very important.
30 * conf->bm_write is the number of the last batch successfully written.
31 * conf->bm_flush is the number of the last batch that was closed to
32 * new additions.
33 * When we discover that we will need to write to any block in a stripe
34 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
35 * the number of the batch it will be in. This is bm_flush+1.
36 * When we are ready to do a write, if that batch hasn't been written yet,
37 * we plug the array and queue the stripe for later.
38 * When an unplug happens, we increment bm_flush, thus closing the current
39 * batch.
40 * When we notice that bm_flush > bm_write, we write out all pending updates
41 * to the bitmap, and advance bm_write to where bm_flush was.
42 * This may occasionally write a bit out twice, but is sure never to
43 * miss any bits.
46 #include <linux/module.h>
47 #include <linux/slab.h>
48 #include <linux/highmem.h>
49 #include <linux/bitops.h>
50 #include <linux/kthread.h>
51 #include <asm/atomic.h>
52 #include "raid6.h"
54 #include <linux/raid/bitmap.h>
57 * Stripe cache
60 #define NR_STRIPES 256
61 #define STRIPE_SIZE PAGE_SIZE
62 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
63 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
64 #define IO_THRESHOLD 1
65 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
66 #define HASH_MASK (NR_HASH - 1)
68 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
70 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
71 * order without overlap. There may be several bio's per stripe+device, and
72 * a bio could span several devices.
73 * When walking this list for a particular stripe+device, we must never proceed
74 * beyond a bio that extends past this device, as the next bio might no longer
75 * be valid.
76 * This macro is used to determine the 'next' bio in the list, given the sector
77 * of the current stripe+device
79 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
81 * The following can be used to debug the driver
83 #define RAID5_DEBUG 0
84 #define RAID5_PARANOIA 1
85 #if RAID5_PARANOIA && defined(CONFIG_SMP)
86 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
87 #else
88 # define CHECK_DEVLOCK()
89 #endif
91 #define PRINTK(x...) ((void)(RAID5_DEBUG && printk(x)))
92 #if RAID5_DEBUG
93 #define inline
94 #define __inline__
95 #endif
97 #if !RAID6_USE_EMPTY_ZERO_PAGE
98 /* In .bss so it's zeroed */
99 const char raid6_empty_zero_page[PAGE_SIZE] __attribute__((aligned(256)));
100 #endif
102 static inline int raid6_next_disk(int disk, int raid_disks)
104 disk++;
105 return (disk < raid_disks) ? disk : 0;
107 static void print_raid5_conf (raid5_conf_t *conf);
109 static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
111 if (atomic_dec_and_test(&sh->count)) {
112 BUG_ON(!list_empty(&sh->lru));
113 BUG_ON(atomic_read(&conf->active_stripes)==0);
114 if (test_bit(STRIPE_HANDLE, &sh->state)) {
115 if (test_bit(STRIPE_DELAYED, &sh->state)) {
116 list_add_tail(&sh->lru, &conf->delayed_list);
117 blk_plug_device(conf->mddev->queue);
118 } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
119 sh->bm_seq - conf->seq_write > 0) {
120 list_add_tail(&sh->lru, &conf->bitmap_list);
121 blk_plug_device(conf->mddev->queue);
122 } else {
123 clear_bit(STRIPE_BIT_DELAY, &sh->state);
124 list_add_tail(&sh->lru, &conf->handle_list);
126 md_wakeup_thread(conf->mddev->thread);
127 } else {
128 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
129 atomic_dec(&conf->preread_active_stripes);
130 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
131 md_wakeup_thread(conf->mddev->thread);
133 atomic_dec(&conf->active_stripes);
134 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
135 list_add_tail(&sh->lru, &conf->inactive_list);
136 wake_up(&conf->wait_for_stripe);
137 if (conf->retry_read_aligned)
138 md_wakeup_thread(conf->mddev->thread);
143 static void release_stripe(struct stripe_head *sh)
145 raid5_conf_t *conf = sh->raid_conf;
146 unsigned long flags;
148 spin_lock_irqsave(&conf->device_lock, flags);
149 __release_stripe(conf, sh);
150 spin_unlock_irqrestore(&conf->device_lock, flags);
153 static inline void remove_hash(struct stripe_head *sh)
155 PRINTK("remove_hash(), stripe %llu\n", (unsigned long long)sh->sector);
157 hlist_del_init(&sh->hash);
160 static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
162 struct hlist_head *hp = stripe_hash(conf, sh->sector);
164 PRINTK("insert_hash(), stripe %llu\n", (unsigned long long)sh->sector);
166 CHECK_DEVLOCK();
167 hlist_add_head(&sh->hash, hp);
171 /* find an idle stripe, make sure it is unhashed, and return it. */
172 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
174 struct stripe_head *sh = NULL;
175 struct list_head *first;
177 CHECK_DEVLOCK();
178 if (list_empty(&conf->inactive_list))
179 goto out;
180 first = conf->inactive_list.next;
181 sh = list_entry(first, struct stripe_head, lru);
182 list_del_init(first);
183 remove_hash(sh);
184 atomic_inc(&conf->active_stripes);
185 out:
186 return sh;
189 static void shrink_buffers(struct stripe_head *sh, int num)
191 struct page *p;
192 int i;
194 for (i=0; i<num ; i++) {
195 p = sh->dev[i].page;
196 if (!p)
197 continue;
198 sh->dev[i].page = NULL;
199 put_page(p);
203 static int grow_buffers(struct stripe_head *sh, int num)
205 int i;
207 for (i=0; i<num; i++) {
208 struct page *page;
210 if (!(page = alloc_page(GFP_KERNEL))) {
211 return 1;
213 sh->dev[i].page = page;
215 return 0;
218 static void raid5_build_block (struct stripe_head *sh, int i);
220 static void init_stripe(struct stripe_head *sh, sector_t sector, int pd_idx, int disks)
222 raid5_conf_t *conf = sh->raid_conf;
223 int i;
225 BUG_ON(atomic_read(&sh->count) != 0);
226 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
228 CHECK_DEVLOCK();
229 PRINTK("init_stripe called, stripe %llu\n",
230 (unsigned long long)sh->sector);
232 remove_hash(sh);
234 sh->sector = sector;
235 sh->pd_idx = pd_idx;
236 sh->state = 0;
238 sh->disks = disks;
240 for (i = sh->disks; i--; ) {
241 struct r5dev *dev = &sh->dev[i];
243 if (dev->toread || dev->towrite || dev->written ||
244 test_bit(R5_LOCKED, &dev->flags)) {
245 printk("sector=%llx i=%d %p %p %p %d\n",
246 (unsigned long long)sh->sector, i, dev->toread,
247 dev->towrite, dev->written,
248 test_bit(R5_LOCKED, &dev->flags));
249 BUG();
251 dev->flags = 0;
252 raid5_build_block(sh, i);
254 insert_hash(conf, sh);
257 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector, int disks)
259 struct stripe_head *sh;
260 struct hlist_node *hn;
262 CHECK_DEVLOCK();
263 PRINTK("__find_stripe, sector %llu\n", (unsigned long long)sector);
264 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
265 if (sh->sector == sector && sh->disks == disks)
266 return sh;
267 PRINTK("__stripe %llu not in cache\n", (unsigned long long)sector);
268 return NULL;
271 static void unplug_slaves(mddev_t *mddev);
272 static void raid5_unplug_device(request_queue_t *q);
274 static struct stripe_head *get_active_stripe(raid5_conf_t *conf, sector_t sector, int disks,
275 int pd_idx, int noblock)
277 struct stripe_head *sh;
279 PRINTK("get_stripe, sector %llu\n", (unsigned long long)sector);
281 spin_lock_irq(&conf->device_lock);
283 do {
284 wait_event_lock_irq(conf->wait_for_stripe,
285 conf->quiesce == 0,
286 conf->device_lock, /* nothing */);
287 sh = __find_stripe(conf, sector, disks);
288 if (!sh) {
289 if (!conf->inactive_blocked)
290 sh = get_free_stripe(conf);
291 if (noblock && sh == NULL)
292 break;
293 if (!sh) {
294 conf->inactive_blocked = 1;
295 wait_event_lock_irq(conf->wait_for_stripe,
296 !list_empty(&conf->inactive_list) &&
297 (atomic_read(&conf->active_stripes)
298 < (conf->max_nr_stripes *3/4)
299 || !conf->inactive_blocked),
300 conf->device_lock,
301 raid5_unplug_device(conf->mddev->queue)
303 conf->inactive_blocked = 0;
304 } else
305 init_stripe(sh, sector, pd_idx, disks);
306 } else {
307 if (atomic_read(&sh->count)) {
308 BUG_ON(!list_empty(&sh->lru));
309 } else {
310 if (!test_bit(STRIPE_HANDLE, &sh->state))
311 atomic_inc(&conf->active_stripes);
312 if (list_empty(&sh->lru) &&
313 !test_bit(STRIPE_EXPANDING, &sh->state))
314 BUG();
315 list_del_init(&sh->lru);
318 } while (sh == NULL);
320 if (sh)
321 atomic_inc(&sh->count);
323 spin_unlock_irq(&conf->device_lock);
324 return sh;
327 static int grow_one_stripe(raid5_conf_t *conf)
329 struct stripe_head *sh;
330 sh = kmem_cache_alloc(conf->slab_cache, GFP_KERNEL);
331 if (!sh)
332 return 0;
333 memset(sh, 0, sizeof(*sh) + (conf->raid_disks-1)*sizeof(struct r5dev));
334 sh->raid_conf = conf;
335 spin_lock_init(&sh->lock);
337 if (grow_buffers(sh, conf->raid_disks)) {
338 shrink_buffers(sh, conf->raid_disks);
339 kmem_cache_free(conf->slab_cache, sh);
340 return 0;
342 sh->disks = conf->raid_disks;
343 /* we just created an active stripe so... */
344 atomic_set(&sh->count, 1);
345 atomic_inc(&conf->active_stripes);
346 INIT_LIST_HEAD(&sh->lru);
347 release_stripe(sh);
348 return 1;
351 static int grow_stripes(raid5_conf_t *conf, int num)
353 struct kmem_cache *sc;
354 int devs = conf->raid_disks;
356 sprintf(conf->cache_name[0], "raid5/%s", mdname(conf->mddev));
357 sprintf(conf->cache_name[1], "raid5/%s-alt", mdname(conf->mddev));
358 conf->active_name = 0;
359 sc = kmem_cache_create(conf->cache_name[conf->active_name],
360 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
361 0, 0, NULL, NULL);
362 if (!sc)
363 return 1;
364 conf->slab_cache = sc;
365 conf->pool_size = devs;
366 while (num--)
367 if (!grow_one_stripe(conf))
368 return 1;
369 return 0;
372 #ifdef CONFIG_MD_RAID5_RESHAPE
373 static int resize_stripes(raid5_conf_t *conf, int newsize)
375 /* Make all the stripes able to hold 'newsize' devices.
376 * New slots in each stripe get 'page' set to a new page.
378 * This happens in stages:
379 * 1/ create a new kmem_cache and allocate the required number of
380 * stripe_heads.
381 * 2/ gather all the old stripe_heads and tranfer the pages across
382 * to the new stripe_heads. This will have the side effect of
383 * freezing the array as once all stripe_heads have been collected,
384 * no IO will be possible. Old stripe heads are freed once their
385 * pages have been transferred over, and the old kmem_cache is
386 * freed when all stripes are done.
387 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
388 * we simple return a failre status - no need to clean anything up.
389 * 4/ allocate new pages for the new slots in the new stripe_heads.
390 * If this fails, we don't bother trying the shrink the
391 * stripe_heads down again, we just leave them as they are.
392 * As each stripe_head is processed the new one is released into
393 * active service.
395 * Once step2 is started, we cannot afford to wait for a write,
396 * so we use GFP_NOIO allocations.
398 struct stripe_head *osh, *nsh;
399 LIST_HEAD(newstripes);
400 struct disk_info *ndisks;
401 int err = 0;
402 struct kmem_cache *sc;
403 int i;
405 if (newsize <= conf->pool_size)
406 return 0; /* never bother to shrink */
408 md_allow_write(conf->mddev);
410 /* Step 1 */
411 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
412 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
413 0, 0, NULL, NULL);
414 if (!sc)
415 return -ENOMEM;
417 for (i = conf->max_nr_stripes; i; i--) {
418 nsh = kmem_cache_alloc(sc, GFP_KERNEL);
419 if (!nsh)
420 break;
422 memset(nsh, 0, sizeof(*nsh) + (newsize-1)*sizeof(struct r5dev));
424 nsh->raid_conf = conf;
425 spin_lock_init(&nsh->lock);
427 list_add(&nsh->lru, &newstripes);
429 if (i) {
430 /* didn't get enough, give up */
431 while (!list_empty(&newstripes)) {
432 nsh = list_entry(newstripes.next, struct stripe_head, lru);
433 list_del(&nsh->lru);
434 kmem_cache_free(sc, nsh);
436 kmem_cache_destroy(sc);
437 return -ENOMEM;
439 /* Step 2 - Must use GFP_NOIO now.
440 * OK, we have enough stripes, start collecting inactive
441 * stripes and copying them over
443 list_for_each_entry(nsh, &newstripes, lru) {
444 spin_lock_irq(&conf->device_lock);
445 wait_event_lock_irq(conf->wait_for_stripe,
446 !list_empty(&conf->inactive_list),
447 conf->device_lock,
448 unplug_slaves(conf->mddev)
450 osh = get_free_stripe(conf);
451 spin_unlock_irq(&conf->device_lock);
452 atomic_set(&nsh->count, 1);
453 for(i=0; i<conf->pool_size; i++)
454 nsh->dev[i].page = osh->dev[i].page;
455 for( ; i<newsize; i++)
456 nsh->dev[i].page = NULL;
457 kmem_cache_free(conf->slab_cache, osh);
459 kmem_cache_destroy(conf->slab_cache);
461 /* Step 3.
462 * At this point, we are holding all the stripes so the array
463 * is completely stalled, so now is a good time to resize
464 * conf->disks.
466 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
467 if (ndisks) {
468 for (i=0; i<conf->raid_disks; i++)
469 ndisks[i] = conf->disks[i];
470 kfree(conf->disks);
471 conf->disks = ndisks;
472 } else
473 err = -ENOMEM;
475 /* Step 4, return new stripes to service */
476 while(!list_empty(&newstripes)) {
477 nsh = list_entry(newstripes.next, struct stripe_head, lru);
478 list_del_init(&nsh->lru);
479 for (i=conf->raid_disks; i < newsize; i++)
480 if (nsh->dev[i].page == NULL) {
481 struct page *p = alloc_page(GFP_NOIO);
482 nsh->dev[i].page = p;
483 if (!p)
484 err = -ENOMEM;
486 release_stripe(nsh);
488 /* critical section pass, GFP_NOIO no longer needed */
490 conf->slab_cache = sc;
491 conf->active_name = 1-conf->active_name;
492 conf->pool_size = newsize;
493 return err;
495 #endif
497 static int drop_one_stripe(raid5_conf_t *conf)
499 struct stripe_head *sh;
501 spin_lock_irq(&conf->device_lock);
502 sh = get_free_stripe(conf);
503 spin_unlock_irq(&conf->device_lock);
504 if (!sh)
505 return 0;
506 BUG_ON(atomic_read(&sh->count));
507 shrink_buffers(sh, conf->pool_size);
508 kmem_cache_free(conf->slab_cache, sh);
509 atomic_dec(&conf->active_stripes);
510 return 1;
513 static void shrink_stripes(raid5_conf_t *conf)
515 while (drop_one_stripe(conf))
518 if (conf->slab_cache)
519 kmem_cache_destroy(conf->slab_cache);
520 conf->slab_cache = NULL;
523 static int raid5_end_read_request(struct bio * bi, unsigned int bytes_done,
524 int error)
526 struct stripe_head *sh = bi->bi_private;
527 raid5_conf_t *conf = sh->raid_conf;
528 int disks = sh->disks, i;
529 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
530 char b[BDEVNAME_SIZE];
531 mdk_rdev_t *rdev;
533 if (bi->bi_size)
534 return 1;
536 for (i=0 ; i<disks; i++)
537 if (bi == &sh->dev[i].req)
538 break;
540 PRINTK("end_read_request %llu/%d, count: %d, uptodate %d.\n",
541 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
542 uptodate);
543 if (i == disks) {
544 BUG();
545 return 0;
548 if (uptodate) {
549 set_bit(R5_UPTODATE, &sh->dev[i].flags);
550 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
551 rdev = conf->disks[i].rdev;
552 printk(KERN_INFO "raid5:%s: read error corrected (%lu sectors at %llu on %s)\n",
553 mdname(conf->mddev), STRIPE_SECTORS,
554 (unsigned long long)sh->sector + rdev->data_offset,
555 bdevname(rdev->bdev, b));
556 clear_bit(R5_ReadError, &sh->dev[i].flags);
557 clear_bit(R5_ReWrite, &sh->dev[i].flags);
559 if (atomic_read(&conf->disks[i].rdev->read_errors))
560 atomic_set(&conf->disks[i].rdev->read_errors, 0);
561 } else {
562 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
563 int retry = 0;
564 rdev = conf->disks[i].rdev;
566 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
567 atomic_inc(&rdev->read_errors);
568 if (conf->mddev->degraded)
569 printk(KERN_WARNING "raid5:%s: read error not correctable (sector %llu on %s).\n",
570 mdname(conf->mddev),
571 (unsigned long long)sh->sector + rdev->data_offset,
572 bdn);
573 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
574 /* Oh, no!!! */
575 printk(KERN_WARNING "raid5:%s: read error NOT corrected!! (sector %llu on %s).\n",
576 mdname(conf->mddev),
577 (unsigned long long)sh->sector + rdev->data_offset,
578 bdn);
579 else if (atomic_read(&rdev->read_errors)
580 > conf->max_nr_stripes)
581 printk(KERN_WARNING
582 "raid5:%s: Too many read errors, failing device %s.\n",
583 mdname(conf->mddev), bdn);
584 else
585 retry = 1;
586 if (retry)
587 set_bit(R5_ReadError, &sh->dev[i].flags);
588 else {
589 clear_bit(R5_ReadError, &sh->dev[i].flags);
590 clear_bit(R5_ReWrite, &sh->dev[i].flags);
591 md_error(conf->mddev, rdev);
594 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
595 clear_bit(R5_LOCKED, &sh->dev[i].flags);
596 set_bit(STRIPE_HANDLE, &sh->state);
597 release_stripe(sh);
598 return 0;
601 static int raid5_end_write_request (struct bio *bi, unsigned int bytes_done,
602 int error)
604 struct stripe_head *sh = bi->bi_private;
605 raid5_conf_t *conf = sh->raid_conf;
606 int disks = sh->disks, i;
607 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
609 if (bi->bi_size)
610 return 1;
612 for (i=0 ; i<disks; i++)
613 if (bi == &sh->dev[i].req)
614 break;
616 PRINTK("end_write_request %llu/%d, count %d, uptodate: %d.\n",
617 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
618 uptodate);
619 if (i == disks) {
620 BUG();
621 return 0;
624 if (!uptodate)
625 md_error(conf->mddev, conf->disks[i].rdev);
627 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
629 clear_bit(R5_LOCKED, &sh->dev[i].flags);
630 set_bit(STRIPE_HANDLE, &sh->state);
631 release_stripe(sh);
632 return 0;
636 static sector_t compute_blocknr(struct stripe_head *sh, int i);
638 static void raid5_build_block (struct stripe_head *sh, int i)
640 struct r5dev *dev = &sh->dev[i];
642 bio_init(&dev->req);
643 dev->req.bi_io_vec = &dev->vec;
644 dev->req.bi_vcnt++;
645 dev->req.bi_max_vecs++;
646 dev->vec.bv_page = dev->page;
647 dev->vec.bv_len = STRIPE_SIZE;
648 dev->vec.bv_offset = 0;
650 dev->req.bi_sector = sh->sector;
651 dev->req.bi_private = sh;
653 dev->flags = 0;
654 dev->sector = compute_blocknr(sh, i);
657 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
659 char b[BDEVNAME_SIZE];
660 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
661 PRINTK("raid5: error called\n");
663 if (!test_bit(Faulty, &rdev->flags)) {
664 set_bit(MD_CHANGE_DEVS, &mddev->flags);
665 if (test_and_clear_bit(In_sync, &rdev->flags)) {
666 unsigned long flags;
667 spin_lock_irqsave(&conf->device_lock, flags);
668 mddev->degraded++;
669 spin_unlock_irqrestore(&conf->device_lock, flags);
671 * if recovery was running, make sure it aborts.
673 set_bit(MD_RECOVERY_ERR, &mddev->recovery);
675 set_bit(Faulty, &rdev->flags);
676 printk (KERN_ALERT
677 "raid5: Disk failure on %s, disabling device."
678 " Operation continuing on %d devices\n",
679 bdevname(rdev->bdev,b), conf->raid_disks - mddev->degraded);
684 * Input: a 'big' sector number,
685 * Output: index of the data and parity disk, and the sector # in them.
687 static sector_t raid5_compute_sector(sector_t r_sector, unsigned int raid_disks,
688 unsigned int data_disks, unsigned int * dd_idx,
689 unsigned int * pd_idx, raid5_conf_t *conf)
691 long stripe;
692 unsigned long chunk_number;
693 unsigned int chunk_offset;
694 sector_t new_sector;
695 int sectors_per_chunk = conf->chunk_size >> 9;
697 /* First compute the information on this sector */
700 * Compute the chunk number and the sector offset inside the chunk
702 chunk_offset = sector_div(r_sector, sectors_per_chunk);
703 chunk_number = r_sector;
704 BUG_ON(r_sector != chunk_number);
707 * Compute the stripe number
709 stripe = chunk_number / data_disks;
712 * Compute the data disk and parity disk indexes inside the stripe
714 *dd_idx = chunk_number % data_disks;
717 * Select the parity disk based on the user selected algorithm.
719 switch(conf->level) {
720 case 4:
721 *pd_idx = data_disks;
722 break;
723 case 5:
724 switch (conf->algorithm) {
725 case ALGORITHM_LEFT_ASYMMETRIC:
726 *pd_idx = data_disks - stripe % raid_disks;
727 if (*dd_idx >= *pd_idx)
728 (*dd_idx)++;
729 break;
730 case ALGORITHM_RIGHT_ASYMMETRIC:
731 *pd_idx = stripe % raid_disks;
732 if (*dd_idx >= *pd_idx)
733 (*dd_idx)++;
734 break;
735 case ALGORITHM_LEFT_SYMMETRIC:
736 *pd_idx = data_disks - stripe % raid_disks;
737 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
738 break;
739 case ALGORITHM_RIGHT_SYMMETRIC:
740 *pd_idx = stripe % raid_disks;
741 *dd_idx = (*pd_idx + 1 + *dd_idx) % raid_disks;
742 break;
743 default:
744 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
745 conf->algorithm);
747 break;
748 case 6:
750 /**** FIX THIS ****/
751 switch (conf->algorithm) {
752 case ALGORITHM_LEFT_ASYMMETRIC:
753 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
754 if (*pd_idx == raid_disks-1)
755 (*dd_idx)++; /* Q D D D P */
756 else if (*dd_idx >= *pd_idx)
757 (*dd_idx) += 2; /* D D P Q D */
758 break;
759 case ALGORITHM_RIGHT_ASYMMETRIC:
760 *pd_idx = stripe % raid_disks;
761 if (*pd_idx == raid_disks-1)
762 (*dd_idx)++; /* Q D D D P */
763 else if (*dd_idx >= *pd_idx)
764 (*dd_idx) += 2; /* D D P Q D */
765 break;
766 case ALGORITHM_LEFT_SYMMETRIC:
767 *pd_idx = raid_disks - 1 - (stripe % raid_disks);
768 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
769 break;
770 case ALGORITHM_RIGHT_SYMMETRIC:
771 *pd_idx = stripe % raid_disks;
772 *dd_idx = (*pd_idx + 2 + *dd_idx) % raid_disks;
773 break;
774 default:
775 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
776 conf->algorithm);
778 break;
782 * Finally, compute the new sector number
784 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
785 return new_sector;
789 static sector_t compute_blocknr(struct stripe_head *sh, int i)
791 raid5_conf_t *conf = sh->raid_conf;
792 int raid_disks = sh->disks;
793 int data_disks = raid_disks - conf->max_degraded;
794 sector_t new_sector = sh->sector, check;
795 int sectors_per_chunk = conf->chunk_size >> 9;
796 sector_t stripe;
797 int chunk_offset;
798 int chunk_number, dummy1, dummy2, dd_idx = i;
799 sector_t r_sector;
802 chunk_offset = sector_div(new_sector, sectors_per_chunk);
803 stripe = new_sector;
804 BUG_ON(new_sector != stripe);
806 if (i == sh->pd_idx)
807 return 0;
808 switch(conf->level) {
809 case 4: break;
810 case 5:
811 switch (conf->algorithm) {
812 case ALGORITHM_LEFT_ASYMMETRIC:
813 case ALGORITHM_RIGHT_ASYMMETRIC:
814 if (i > sh->pd_idx)
815 i--;
816 break;
817 case ALGORITHM_LEFT_SYMMETRIC:
818 case ALGORITHM_RIGHT_SYMMETRIC:
819 if (i < sh->pd_idx)
820 i += raid_disks;
821 i -= (sh->pd_idx + 1);
822 break;
823 default:
824 printk(KERN_ERR "raid5: unsupported algorithm %d\n",
825 conf->algorithm);
827 break;
828 case 6:
829 if (i == raid6_next_disk(sh->pd_idx, raid_disks))
830 return 0; /* It is the Q disk */
831 switch (conf->algorithm) {
832 case ALGORITHM_LEFT_ASYMMETRIC:
833 case ALGORITHM_RIGHT_ASYMMETRIC:
834 if (sh->pd_idx == raid_disks-1)
835 i--; /* Q D D D P */
836 else if (i > sh->pd_idx)
837 i -= 2; /* D D P Q D */
838 break;
839 case ALGORITHM_LEFT_SYMMETRIC:
840 case ALGORITHM_RIGHT_SYMMETRIC:
841 if (sh->pd_idx == raid_disks-1)
842 i--; /* Q D D D P */
843 else {
844 /* D D P Q D */
845 if (i < sh->pd_idx)
846 i += raid_disks;
847 i -= (sh->pd_idx + 2);
849 break;
850 default:
851 printk (KERN_CRIT "raid6: unsupported algorithm %d\n",
852 conf->algorithm);
854 break;
857 chunk_number = stripe * data_disks + i;
858 r_sector = (sector_t)chunk_number * sectors_per_chunk + chunk_offset;
860 check = raid5_compute_sector (r_sector, raid_disks, data_disks, &dummy1, &dummy2, conf);
861 if (check != sh->sector || dummy1 != dd_idx || dummy2 != sh->pd_idx) {
862 printk(KERN_ERR "compute_blocknr: map not correct\n");
863 return 0;
865 return r_sector;
871 * Copy data between a page in the stripe cache, and one or more bion
872 * The page could align with the middle of the bio, or there could be
873 * several bion, each with several bio_vecs, which cover part of the page
874 * Multiple bion are linked together on bi_next. There may be extras
875 * at the end of this list. We ignore them.
877 static void copy_data(int frombio, struct bio *bio,
878 struct page *page,
879 sector_t sector)
881 char *pa = page_address(page);
882 struct bio_vec *bvl;
883 int i;
884 int page_offset;
886 if (bio->bi_sector >= sector)
887 page_offset = (signed)(bio->bi_sector - sector) * 512;
888 else
889 page_offset = (signed)(sector - bio->bi_sector) * -512;
890 bio_for_each_segment(bvl, bio, i) {
891 int len = bio_iovec_idx(bio,i)->bv_len;
892 int clen;
893 int b_offset = 0;
895 if (page_offset < 0) {
896 b_offset = -page_offset;
897 page_offset += b_offset;
898 len -= b_offset;
901 if (len > 0 && page_offset + len > STRIPE_SIZE)
902 clen = STRIPE_SIZE - page_offset;
903 else clen = len;
905 if (clen > 0) {
906 char *ba = __bio_kmap_atomic(bio, i, KM_USER0);
907 if (frombio)
908 memcpy(pa+page_offset, ba+b_offset, clen);
909 else
910 memcpy(ba+b_offset, pa+page_offset, clen);
911 __bio_kunmap_atomic(ba, KM_USER0);
913 if (clen < len) /* hit end of page */
914 break;
915 page_offset += len;
919 #define check_xor() do { \
920 if (count == MAX_XOR_BLOCKS) { \
921 xor_block(count, STRIPE_SIZE, ptr); \
922 count = 1; \
924 } while(0)
927 static void compute_block(struct stripe_head *sh, int dd_idx)
929 int i, count, disks = sh->disks;
930 void *ptr[MAX_XOR_BLOCKS], *p;
932 PRINTK("compute_block, stripe %llu, idx %d\n",
933 (unsigned long long)sh->sector, dd_idx);
935 ptr[0] = page_address(sh->dev[dd_idx].page);
936 memset(ptr[0], 0, STRIPE_SIZE);
937 count = 1;
938 for (i = disks ; i--; ) {
939 if (i == dd_idx)
940 continue;
941 p = page_address(sh->dev[i].page);
942 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
943 ptr[count++] = p;
944 else
945 printk(KERN_ERR "compute_block() %d, stripe %llu, %d"
946 " not present\n", dd_idx,
947 (unsigned long long)sh->sector, i);
949 check_xor();
951 if (count != 1)
952 xor_block(count, STRIPE_SIZE, ptr);
953 set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
956 static void compute_parity5(struct stripe_head *sh, int method)
958 raid5_conf_t *conf = sh->raid_conf;
959 int i, pd_idx = sh->pd_idx, disks = sh->disks, count;
960 void *ptr[MAX_XOR_BLOCKS];
961 struct bio *chosen;
963 PRINTK("compute_parity5, stripe %llu, method %d\n",
964 (unsigned long long)sh->sector, method);
966 count = 1;
967 ptr[0] = page_address(sh->dev[pd_idx].page);
968 switch(method) {
969 case READ_MODIFY_WRITE:
970 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags));
971 for (i=disks ; i-- ;) {
972 if (i==pd_idx)
973 continue;
974 if (sh->dev[i].towrite &&
975 test_bit(R5_UPTODATE, &sh->dev[i].flags)) {
976 ptr[count++] = page_address(sh->dev[i].page);
977 chosen = sh->dev[i].towrite;
978 sh->dev[i].towrite = NULL;
980 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
981 wake_up(&conf->wait_for_overlap);
983 BUG_ON(sh->dev[i].written);
984 sh->dev[i].written = chosen;
985 check_xor();
988 break;
989 case RECONSTRUCT_WRITE:
990 memset(ptr[0], 0, STRIPE_SIZE);
991 for (i= disks; i-- ;)
992 if (i!=pd_idx && sh->dev[i].towrite) {
993 chosen = sh->dev[i].towrite;
994 sh->dev[i].towrite = NULL;
996 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
997 wake_up(&conf->wait_for_overlap);
999 BUG_ON(sh->dev[i].written);
1000 sh->dev[i].written = chosen;
1002 break;
1003 case CHECK_PARITY:
1004 break;
1006 if (count>1) {
1007 xor_block(count, STRIPE_SIZE, ptr);
1008 count = 1;
1011 for (i = disks; i--;)
1012 if (sh->dev[i].written) {
1013 sector_t sector = sh->dev[i].sector;
1014 struct bio *wbi = sh->dev[i].written;
1015 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1016 copy_data(1, wbi, sh->dev[i].page, sector);
1017 wbi = r5_next_bio(wbi, sector);
1020 set_bit(R5_LOCKED, &sh->dev[i].flags);
1021 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1024 switch(method) {
1025 case RECONSTRUCT_WRITE:
1026 case CHECK_PARITY:
1027 for (i=disks; i--;)
1028 if (i != pd_idx) {
1029 ptr[count++] = page_address(sh->dev[i].page);
1030 check_xor();
1032 break;
1033 case READ_MODIFY_WRITE:
1034 for (i = disks; i--;)
1035 if (sh->dev[i].written) {
1036 ptr[count++] = page_address(sh->dev[i].page);
1037 check_xor();
1040 if (count != 1)
1041 xor_block(count, STRIPE_SIZE, ptr);
1043 if (method != CHECK_PARITY) {
1044 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1045 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1046 } else
1047 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1050 static void compute_parity6(struct stripe_head *sh, int method)
1052 raid6_conf_t *conf = sh->raid_conf;
1053 int i, pd_idx = sh->pd_idx, qd_idx, d0_idx, disks = conf->raid_disks, count;
1054 struct bio *chosen;
1055 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1056 void *ptrs[disks];
1058 qd_idx = raid6_next_disk(pd_idx, disks);
1059 d0_idx = raid6_next_disk(qd_idx, disks);
1061 PRINTK("compute_parity, stripe %llu, method %d\n",
1062 (unsigned long long)sh->sector, method);
1064 switch(method) {
1065 case READ_MODIFY_WRITE:
1066 BUG(); /* READ_MODIFY_WRITE N/A for RAID-6 */
1067 case RECONSTRUCT_WRITE:
1068 for (i= disks; i-- ;)
1069 if ( i != pd_idx && i != qd_idx && sh->dev[i].towrite ) {
1070 chosen = sh->dev[i].towrite;
1071 sh->dev[i].towrite = NULL;
1073 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1074 wake_up(&conf->wait_for_overlap);
1076 BUG_ON(sh->dev[i].written);
1077 sh->dev[i].written = chosen;
1079 break;
1080 case CHECK_PARITY:
1081 BUG(); /* Not implemented yet */
1084 for (i = disks; i--;)
1085 if (sh->dev[i].written) {
1086 sector_t sector = sh->dev[i].sector;
1087 struct bio *wbi = sh->dev[i].written;
1088 while (wbi && wbi->bi_sector < sector + STRIPE_SECTORS) {
1089 copy_data(1, wbi, sh->dev[i].page, sector);
1090 wbi = r5_next_bio(wbi, sector);
1093 set_bit(R5_LOCKED, &sh->dev[i].flags);
1094 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1097 // switch(method) {
1098 // case RECONSTRUCT_WRITE:
1099 // case CHECK_PARITY:
1100 // case UPDATE_PARITY:
1101 /* Note that unlike RAID-5, the ordering of the disks matters greatly. */
1102 /* FIX: Is this ordering of drives even remotely optimal? */
1103 count = 0;
1104 i = d0_idx;
1105 do {
1106 ptrs[count++] = page_address(sh->dev[i].page);
1107 if (count <= disks-2 && !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1108 printk("block %d/%d not uptodate on parity calc\n", i,count);
1109 i = raid6_next_disk(i, disks);
1110 } while ( i != d0_idx );
1111 // break;
1112 // }
1114 raid6_call.gen_syndrome(disks, STRIPE_SIZE, ptrs);
1116 switch(method) {
1117 case RECONSTRUCT_WRITE:
1118 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1119 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1120 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
1121 set_bit(R5_LOCKED, &sh->dev[qd_idx].flags);
1122 break;
1123 case UPDATE_PARITY:
1124 set_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
1125 set_bit(R5_UPTODATE, &sh->dev[qd_idx].flags);
1126 break;
1131 /* Compute one missing block */
1132 static void compute_block_1(struct stripe_head *sh, int dd_idx, int nozero)
1134 raid6_conf_t *conf = sh->raid_conf;
1135 int i, count, disks = conf->raid_disks;
1136 void *ptr[MAX_XOR_BLOCKS], *p;
1137 int pd_idx = sh->pd_idx;
1138 int qd_idx = raid6_next_disk(pd_idx, disks);
1140 PRINTK("compute_block_1, stripe %llu, idx %d\n",
1141 (unsigned long long)sh->sector, dd_idx);
1143 if ( dd_idx == qd_idx ) {
1144 /* We're actually computing the Q drive */
1145 compute_parity6(sh, UPDATE_PARITY);
1146 } else {
1147 ptr[0] = page_address(sh->dev[dd_idx].page);
1148 if (!nozero) memset(ptr[0], 0, STRIPE_SIZE);
1149 count = 1;
1150 for (i = disks ; i--; ) {
1151 if (i == dd_idx || i == qd_idx)
1152 continue;
1153 p = page_address(sh->dev[i].page);
1154 if (test_bit(R5_UPTODATE, &sh->dev[i].flags))
1155 ptr[count++] = p;
1156 else
1157 printk("compute_block() %d, stripe %llu, %d"
1158 " not present\n", dd_idx,
1159 (unsigned long long)sh->sector, i);
1161 check_xor();
1163 if (count != 1)
1164 xor_block(count, STRIPE_SIZE, ptr);
1165 if (!nozero) set_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1166 else clear_bit(R5_UPTODATE, &sh->dev[dd_idx].flags);
1170 /* Compute two missing blocks */
1171 static void compute_block_2(struct stripe_head *sh, int dd_idx1, int dd_idx2)
1173 raid6_conf_t *conf = sh->raid_conf;
1174 int i, count, disks = conf->raid_disks;
1175 int pd_idx = sh->pd_idx;
1176 int qd_idx = raid6_next_disk(pd_idx, disks);
1177 int d0_idx = raid6_next_disk(qd_idx, disks);
1178 int faila, failb;
1180 /* faila and failb are disk numbers relative to d0_idx */
1181 /* pd_idx become disks-2 and qd_idx become disks-1 */
1182 faila = (dd_idx1 < d0_idx) ? dd_idx1+(disks-d0_idx) : dd_idx1-d0_idx;
1183 failb = (dd_idx2 < d0_idx) ? dd_idx2+(disks-d0_idx) : dd_idx2-d0_idx;
1185 BUG_ON(faila == failb);
1186 if ( failb < faila ) { int tmp = faila; faila = failb; failb = tmp; }
1188 PRINTK("compute_block_2, stripe %llu, idx %d,%d (%d,%d)\n",
1189 (unsigned long long)sh->sector, dd_idx1, dd_idx2, faila, failb);
1191 if ( failb == disks-1 ) {
1192 /* Q disk is one of the missing disks */
1193 if ( faila == disks-2 ) {
1194 /* Missing P+Q, just recompute */
1195 compute_parity6(sh, UPDATE_PARITY);
1196 return;
1197 } else {
1198 /* We're missing D+Q; recompute D from P */
1199 compute_block_1(sh, (dd_idx1 == qd_idx) ? dd_idx2 : dd_idx1, 0);
1200 compute_parity6(sh, UPDATE_PARITY); /* Is this necessary? */
1201 return;
1205 /* We're missing D+P or D+D; build pointer table */
1207 /**** FIX THIS: This could be very bad if disks is close to 256 ****/
1208 void *ptrs[disks];
1210 count = 0;
1211 i = d0_idx;
1212 do {
1213 ptrs[count++] = page_address(sh->dev[i].page);
1214 i = raid6_next_disk(i, disks);
1215 if (i != dd_idx1 && i != dd_idx2 &&
1216 !test_bit(R5_UPTODATE, &sh->dev[i].flags))
1217 printk("compute_2 with missing block %d/%d\n", count, i);
1218 } while ( i != d0_idx );
1220 if ( failb == disks-2 ) {
1221 /* We're missing D+P. */
1222 raid6_datap_recov(disks, STRIPE_SIZE, faila, ptrs);
1223 } else {
1224 /* We're missing D+D. */
1225 raid6_2data_recov(disks, STRIPE_SIZE, faila, failb, ptrs);
1228 /* Both the above update both missing blocks */
1229 set_bit(R5_UPTODATE, &sh->dev[dd_idx1].flags);
1230 set_bit(R5_UPTODATE, &sh->dev[dd_idx2].flags);
1237 * Each stripe/dev can have one or more bion attached.
1238 * toread/towrite point to the first in a chain.
1239 * The bi_next chain must be in order.
1241 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
1243 struct bio **bip;
1244 raid5_conf_t *conf = sh->raid_conf;
1245 int firstwrite=0;
1247 PRINTK("adding bh b#%llu to stripe s#%llu\n",
1248 (unsigned long long)bi->bi_sector,
1249 (unsigned long long)sh->sector);
1252 spin_lock(&sh->lock);
1253 spin_lock_irq(&conf->device_lock);
1254 if (forwrite) {
1255 bip = &sh->dev[dd_idx].towrite;
1256 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
1257 firstwrite = 1;
1258 } else
1259 bip = &sh->dev[dd_idx].toread;
1260 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
1261 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
1262 goto overlap;
1263 bip = & (*bip)->bi_next;
1265 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
1266 goto overlap;
1268 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
1269 if (*bip)
1270 bi->bi_next = *bip;
1271 *bip = bi;
1272 bi->bi_phys_segments ++;
1273 spin_unlock_irq(&conf->device_lock);
1274 spin_unlock(&sh->lock);
1276 PRINTK("added bi b#%llu to stripe s#%llu, disk %d.\n",
1277 (unsigned long long)bi->bi_sector,
1278 (unsigned long long)sh->sector, dd_idx);
1280 if (conf->mddev->bitmap && firstwrite) {
1281 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
1282 STRIPE_SECTORS, 0);
1283 sh->bm_seq = conf->seq_flush+1;
1284 set_bit(STRIPE_BIT_DELAY, &sh->state);
1287 if (forwrite) {
1288 /* check if page is covered */
1289 sector_t sector = sh->dev[dd_idx].sector;
1290 for (bi=sh->dev[dd_idx].towrite;
1291 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
1292 bi && bi->bi_sector <= sector;
1293 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
1294 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
1295 sector = bi->bi_sector + (bi->bi_size>>9);
1297 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
1298 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
1300 return 1;
1302 overlap:
1303 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
1304 spin_unlock_irq(&conf->device_lock);
1305 spin_unlock(&sh->lock);
1306 return 0;
1309 static void end_reshape(raid5_conf_t *conf);
1311 static int page_is_zero(struct page *p)
1313 char *a = page_address(p);
1314 return ((*(u32*)a) == 0 &&
1315 memcmp(a, a+4, STRIPE_SIZE-4)==0);
1318 static int stripe_to_pdidx(sector_t stripe, raid5_conf_t *conf, int disks)
1320 int sectors_per_chunk = conf->chunk_size >> 9;
1321 int pd_idx, dd_idx;
1322 int chunk_offset = sector_div(stripe, sectors_per_chunk);
1324 raid5_compute_sector(stripe * (disks - conf->max_degraded)
1325 *sectors_per_chunk + chunk_offset,
1326 disks, disks - conf->max_degraded,
1327 &dd_idx, &pd_idx, conf);
1328 return pd_idx;
1333 * handle_stripe - do things to a stripe.
1335 * We lock the stripe and then examine the state of various bits
1336 * to see what needs to be done.
1337 * Possible results:
1338 * return some read request which now have data
1339 * return some write requests which are safely on disc
1340 * schedule a read on some buffers
1341 * schedule a write of some buffers
1342 * return confirmation of parity correctness
1344 * Parity calculations are done inside the stripe lock
1345 * buffers are taken off read_list or write_list, and bh_cache buffers
1346 * get BH_Lock set before the stripe lock is released.
1350 static void handle_stripe5(struct stripe_head *sh)
1352 raid5_conf_t *conf = sh->raid_conf;
1353 int disks = sh->disks;
1354 struct bio *return_bi= NULL;
1355 struct bio *bi;
1356 int i;
1357 int syncing, expanding, expanded;
1358 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1359 int non_overwrite = 0;
1360 int failed_num=0;
1361 struct r5dev *dev;
1363 PRINTK("handling stripe %llu, cnt=%d, pd_idx=%d\n",
1364 (unsigned long long)sh->sector, atomic_read(&sh->count),
1365 sh->pd_idx);
1367 spin_lock(&sh->lock);
1368 clear_bit(STRIPE_HANDLE, &sh->state);
1369 clear_bit(STRIPE_DELAYED, &sh->state);
1371 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1372 expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1373 expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
1374 /* Now to look around and see what can be done */
1376 rcu_read_lock();
1377 for (i=disks; i--; ) {
1378 mdk_rdev_t *rdev;
1379 dev = &sh->dev[i];
1380 clear_bit(R5_Insync, &dev->flags);
1382 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1383 i, dev->flags, dev->toread, dev->towrite, dev->written);
1384 /* maybe we can reply to a read */
1385 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1386 struct bio *rbi, *rbi2;
1387 PRINTK("Return read for disc %d\n", i);
1388 spin_lock_irq(&conf->device_lock);
1389 rbi = dev->toread;
1390 dev->toread = NULL;
1391 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1392 wake_up(&conf->wait_for_overlap);
1393 spin_unlock_irq(&conf->device_lock);
1394 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1395 copy_data(0, rbi, dev->page, dev->sector);
1396 rbi2 = r5_next_bio(rbi, dev->sector);
1397 spin_lock_irq(&conf->device_lock);
1398 if (--rbi->bi_phys_segments == 0) {
1399 rbi->bi_next = return_bi;
1400 return_bi = rbi;
1402 spin_unlock_irq(&conf->device_lock);
1403 rbi = rbi2;
1407 /* now count some things */
1408 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1409 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1412 if (dev->toread) to_read++;
1413 if (dev->towrite) {
1414 to_write++;
1415 if (!test_bit(R5_OVERWRITE, &dev->flags))
1416 non_overwrite++;
1418 if (dev->written) written++;
1419 rdev = rcu_dereference(conf->disks[i].rdev);
1420 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1421 /* The ReadError flag will just be confusing now */
1422 clear_bit(R5_ReadError, &dev->flags);
1423 clear_bit(R5_ReWrite, &dev->flags);
1425 if (!rdev || !test_bit(In_sync, &rdev->flags)
1426 || test_bit(R5_ReadError, &dev->flags)) {
1427 failed++;
1428 failed_num = i;
1429 } else
1430 set_bit(R5_Insync, &dev->flags);
1432 rcu_read_unlock();
1433 PRINTK("locked=%d uptodate=%d to_read=%d"
1434 " to_write=%d failed=%d failed_num=%d\n",
1435 locked, uptodate, to_read, to_write, failed, failed_num);
1436 /* check if the array has lost two devices and, if so, some requests might
1437 * need to be failed
1439 if (failed > 1 && to_read+to_write+written) {
1440 for (i=disks; i--; ) {
1441 int bitmap_end = 0;
1443 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1444 mdk_rdev_t *rdev;
1445 rcu_read_lock();
1446 rdev = rcu_dereference(conf->disks[i].rdev);
1447 if (rdev && test_bit(In_sync, &rdev->flags))
1448 /* multiple read failures in one stripe */
1449 md_error(conf->mddev, rdev);
1450 rcu_read_unlock();
1453 spin_lock_irq(&conf->device_lock);
1454 /* fail all writes first */
1455 bi = sh->dev[i].towrite;
1456 sh->dev[i].towrite = NULL;
1457 if (bi) { to_write--; bitmap_end = 1; }
1459 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1460 wake_up(&conf->wait_for_overlap);
1462 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1463 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1464 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1465 if (--bi->bi_phys_segments == 0) {
1466 md_write_end(conf->mddev);
1467 bi->bi_next = return_bi;
1468 return_bi = bi;
1470 bi = nextbi;
1472 /* and fail all 'written' */
1473 bi = sh->dev[i].written;
1474 sh->dev[i].written = NULL;
1475 if (bi) bitmap_end = 1;
1476 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
1477 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
1478 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1479 if (--bi->bi_phys_segments == 0) {
1480 md_write_end(conf->mddev);
1481 bi->bi_next = return_bi;
1482 return_bi = bi;
1484 bi = bi2;
1487 /* fail any reads if this device is non-operational */
1488 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
1489 test_bit(R5_ReadError, &sh->dev[i].flags)) {
1490 bi = sh->dev[i].toread;
1491 sh->dev[i].toread = NULL;
1492 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
1493 wake_up(&conf->wait_for_overlap);
1494 if (bi) to_read--;
1495 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
1496 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
1497 clear_bit(BIO_UPTODATE, &bi->bi_flags);
1498 if (--bi->bi_phys_segments == 0) {
1499 bi->bi_next = return_bi;
1500 return_bi = bi;
1502 bi = nextbi;
1505 spin_unlock_irq(&conf->device_lock);
1506 if (bitmap_end)
1507 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1508 STRIPE_SECTORS, 0, 0);
1511 if (failed > 1 && syncing) {
1512 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
1513 clear_bit(STRIPE_SYNCING, &sh->state);
1514 syncing = 0;
1517 /* might be able to return some write requests if the parity block
1518 * is safe, or on a failed drive
1520 dev = &sh->dev[sh->pd_idx];
1521 if ( written &&
1522 ( (test_bit(R5_Insync, &dev->flags) && !test_bit(R5_LOCKED, &dev->flags) &&
1523 test_bit(R5_UPTODATE, &dev->flags))
1524 || (failed == 1 && failed_num == sh->pd_idx))
1526 /* any written block on an uptodate or failed drive can be returned.
1527 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
1528 * never LOCKED, so we don't need to test 'failed' directly.
1530 for (i=disks; i--; )
1531 if (sh->dev[i].written) {
1532 dev = &sh->dev[i];
1533 if (!test_bit(R5_LOCKED, &dev->flags) &&
1534 test_bit(R5_UPTODATE, &dev->flags) ) {
1535 /* We can return any write requests */
1536 struct bio *wbi, *wbi2;
1537 int bitmap_end = 0;
1538 PRINTK("Return write for disc %d\n", i);
1539 spin_lock_irq(&conf->device_lock);
1540 wbi = dev->written;
1541 dev->written = NULL;
1542 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1543 wbi2 = r5_next_bio(wbi, dev->sector);
1544 if (--wbi->bi_phys_segments == 0) {
1545 md_write_end(conf->mddev);
1546 wbi->bi_next = return_bi;
1547 return_bi = wbi;
1549 wbi = wbi2;
1551 if (dev->towrite == NULL)
1552 bitmap_end = 1;
1553 spin_unlock_irq(&conf->device_lock);
1554 if (bitmap_end)
1555 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
1556 STRIPE_SECTORS,
1557 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
1562 /* Now we might consider reading some blocks, either to check/generate
1563 * parity, or to satisfy requests
1564 * or to load a block that is being partially written.
1566 if (to_read || non_overwrite || (syncing && (uptodate < disks)) || expanding) {
1567 for (i=disks; i--;) {
1568 dev = &sh->dev[i];
1569 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1570 (dev->toread ||
1571 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
1572 syncing ||
1573 expanding ||
1574 (failed && (sh->dev[failed_num].toread ||
1575 (sh->dev[failed_num].towrite && !test_bit(R5_OVERWRITE, &sh->dev[failed_num].flags))))
1578 /* we would like to get this block, possibly
1579 * by computing it, but we might not be able to
1581 if (uptodate == disks-1) {
1582 PRINTK("Computing block %d\n", i);
1583 compute_block(sh, i);
1584 uptodate++;
1585 } else if (test_bit(R5_Insync, &dev->flags)) {
1586 set_bit(R5_LOCKED, &dev->flags);
1587 set_bit(R5_Wantread, &dev->flags);
1588 locked++;
1589 PRINTK("Reading block %d (sync=%d)\n",
1590 i, syncing);
1594 set_bit(STRIPE_HANDLE, &sh->state);
1597 /* now to consider writing and what else, if anything should be read */
1598 if (to_write) {
1599 int rmw=0, rcw=0;
1600 for (i=disks ; i--;) {
1601 /* would I have to read this buffer for read_modify_write */
1602 dev = &sh->dev[i];
1603 if ((dev->towrite || i == sh->pd_idx) &&
1604 (!test_bit(R5_LOCKED, &dev->flags)
1605 ) &&
1606 !test_bit(R5_UPTODATE, &dev->flags)) {
1607 if (test_bit(R5_Insync, &dev->flags)
1608 /* && !(!mddev->insync && i == sh->pd_idx) */
1610 rmw++;
1611 else rmw += 2*disks; /* cannot read it */
1613 /* Would I have to read this buffer for reconstruct_write */
1614 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1615 (!test_bit(R5_LOCKED, &dev->flags)
1616 ) &&
1617 !test_bit(R5_UPTODATE, &dev->flags)) {
1618 if (test_bit(R5_Insync, &dev->flags)) rcw++;
1619 else rcw += 2*disks;
1622 PRINTK("for sector %llu, rmw=%d rcw=%d\n",
1623 (unsigned long long)sh->sector, rmw, rcw);
1624 set_bit(STRIPE_HANDLE, &sh->state);
1625 if (rmw < rcw && rmw > 0)
1626 /* prefer read-modify-write, but need to get some data */
1627 for (i=disks; i--;) {
1628 dev = &sh->dev[i];
1629 if ((dev->towrite || i == sh->pd_idx) &&
1630 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1631 test_bit(R5_Insync, &dev->flags)) {
1632 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1634 PRINTK("Read_old block %d for r-m-w\n", i);
1635 set_bit(R5_LOCKED, &dev->flags);
1636 set_bit(R5_Wantread, &dev->flags);
1637 locked++;
1638 } else {
1639 set_bit(STRIPE_DELAYED, &sh->state);
1640 set_bit(STRIPE_HANDLE, &sh->state);
1644 if (rcw <= rmw && rcw > 0)
1645 /* want reconstruct write, but need to get some data */
1646 for (i=disks; i--;) {
1647 dev = &sh->dev[i];
1648 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
1649 !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
1650 test_bit(R5_Insync, &dev->flags)) {
1651 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
1653 PRINTK("Read_old block %d for Reconstruct\n", i);
1654 set_bit(R5_LOCKED, &dev->flags);
1655 set_bit(R5_Wantread, &dev->flags);
1656 locked++;
1657 } else {
1658 set_bit(STRIPE_DELAYED, &sh->state);
1659 set_bit(STRIPE_HANDLE, &sh->state);
1663 /* now if nothing is locked, and if we have enough data, we can start a write request */
1664 if (locked == 0 && (rcw == 0 ||rmw == 0) &&
1665 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
1666 PRINTK("Computing parity...\n");
1667 compute_parity5(sh, rcw==0 ? RECONSTRUCT_WRITE : READ_MODIFY_WRITE);
1668 /* now every locked buffer is ready to be written */
1669 for (i=disks; i--;)
1670 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
1671 PRINTK("Writing block %d\n", i);
1672 locked++;
1673 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1674 if (!test_bit(R5_Insync, &sh->dev[i].flags)
1675 || (i==sh->pd_idx && failed == 0))
1676 set_bit(STRIPE_INSYNC, &sh->state);
1678 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
1679 atomic_dec(&conf->preread_active_stripes);
1680 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
1681 md_wakeup_thread(conf->mddev->thread);
1686 /* maybe we need to check and possibly fix the parity for this stripe
1687 * Any reads will already have been scheduled, so we just see if enough data
1688 * is available
1690 if (syncing && locked == 0 &&
1691 !test_bit(STRIPE_INSYNC, &sh->state)) {
1692 set_bit(STRIPE_HANDLE, &sh->state);
1693 if (failed == 0) {
1694 BUG_ON(uptodate != disks);
1695 compute_parity5(sh, CHECK_PARITY);
1696 uptodate--;
1697 if (page_is_zero(sh->dev[sh->pd_idx].page)) {
1698 /* parity is correct (on disc, not in buffer any more) */
1699 set_bit(STRIPE_INSYNC, &sh->state);
1700 } else {
1701 conf->mddev->resync_mismatches += STRIPE_SECTORS;
1702 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
1703 /* don't try to repair!! */
1704 set_bit(STRIPE_INSYNC, &sh->state);
1705 else {
1706 compute_block(sh, sh->pd_idx);
1707 uptodate++;
1711 if (!test_bit(STRIPE_INSYNC, &sh->state)) {
1712 /* either failed parity check, or recovery is happening */
1713 if (failed==0)
1714 failed_num = sh->pd_idx;
1715 dev = &sh->dev[failed_num];
1716 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
1717 BUG_ON(uptodate != disks);
1719 set_bit(R5_LOCKED, &dev->flags);
1720 set_bit(R5_Wantwrite, &dev->flags);
1721 clear_bit(STRIPE_DEGRADED, &sh->state);
1722 locked++;
1723 set_bit(STRIPE_INSYNC, &sh->state);
1726 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
1727 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
1728 clear_bit(STRIPE_SYNCING, &sh->state);
1731 /* If the failed drive is just a ReadError, then we might need to progress
1732 * the repair/check process
1734 if (failed == 1 && ! conf->mddev->ro &&
1735 test_bit(R5_ReadError, &sh->dev[failed_num].flags)
1736 && !test_bit(R5_LOCKED, &sh->dev[failed_num].flags)
1737 && test_bit(R5_UPTODATE, &sh->dev[failed_num].flags)
1739 dev = &sh->dev[failed_num];
1740 if (!test_bit(R5_ReWrite, &dev->flags)) {
1741 set_bit(R5_Wantwrite, &dev->flags);
1742 set_bit(R5_ReWrite, &dev->flags);
1743 set_bit(R5_LOCKED, &dev->flags);
1744 locked++;
1745 } else {
1746 /* let's read it back */
1747 set_bit(R5_Wantread, &dev->flags);
1748 set_bit(R5_LOCKED, &dev->flags);
1749 locked++;
1753 if (expanded && test_bit(STRIPE_EXPANDING, &sh->state)) {
1754 /* Need to write out all blocks after computing parity */
1755 sh->disks = conf->raid_disks;
1756 sh->pd_idx = stripe_to_pdidx(sh->sector, conf, conf->raid_disks);
1757 compute_parity5(sh, RECONSTRUCT_WRITE);
1758 for (i= conf->raid_disks; i--;) {
1759 set_bit(R5_LOCKED, &sh->dev[i].flags);
1760 locked++;
1761 set_bit(R5_Wantwrite, &sh->dev[i].flags);
1763 clear_bit(STRIPE_EXPANDING, &sh->state);
1764 } else if (expanded) {
1765 clear_bit(STRIPE_EXPAND_READY, &sh->state);
1766 atomic_dec(&conf->reshape_stripes);
1767 wake_up(&conf->wait_for_overlap);
1768 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
1771 if (expanding && locked == 0) {
1772 /* We have read all the blocks in this stripe and now we need to
1773 * copy some of them into a target stripe for expand.
1775 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
1776 for (i=0; i< sh->disks; i++)
1777 if (i != sh->pd_idx) {
1778 int dd_idx, pd_idx, j;
1779 struct stripe_head *sh2;
1781 sector_t bn = compute_blocknr(sh, i);
1782 sector_t s = raid5_compute_sector(bn, conf->raid_disks,
1783 conf->raid_disks-1,
1784 &dd_idx, &pd_idx, conf);
1785 sh2 = get_active_stripe(conf, s, conf->raid_disks, pd_idx, 1);
1786 if (sh2 == NULL)
1787 /* so far only the early blocks of this stripe
1788 * have been requested. When later blocks
1789 * get requested, we will try again
1791 continue;
1792 if(!test_bit(STRIPE_EXPANDING, &sh2->state) ||
1793 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
1794 /* must have already done this block */
1795 release_stripe(sh2);
1796 continue;
1798 memcpy(page_address(sh2->dev[dd_idx].page),
1799 page_address(sh->dev[i].page),
1800 STRIPE_SIZE);
1801 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
1802 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
1803 for (j=0; j<conf->raid_disks; j++)
1804 if (j != sh2->pd_idx &&
1805 !test_bit(R5_Expanded, &sh2->dev[j].flags))
1806 break;
1807 if (j == conf->raid_disks) {
1808 set_bit(STRIPE_EXPAND_READY, &sh2->state);
1809 set_bit(STRIPE_HANDLE, &sh2->state);
1811 release_stripe(sh2);
1815 spin_unlock(&sh->lock);
1817 while ((bi=return_bi)) {
1818 int bytes = bi->bi_size;
1820 return_bi = bi->bi_next;
1821 bi->bi_next = NULL;
1822 bi->bi_size = 0;
1823 bi->bi_end_io(bi, bytes,
1824 test_bit(BIO_UPTODATE, &bi->bi_flags)
1825 ? 0 : -EIO);
1827 for (i=disks; i-- ;) {
1828 int rw;
1829 struct bio *bi;
1830 mdk_rdev_t *rdev;
1831 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
1832 rw = WRITE;
1833 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
1834 rw = READ;
1835 else
1836 continue;
1838 bi = &sh->dev[i].req;
1840 bi->bi_rw = rw;
1841 if (rw == WRITE)
1842 bi->bi_end_io = raid5_end_write_request;
1843 else
1844 bi->bi_end_io = raid5_end_read_request;
1846 rcu_read_lock();
1847 rdev = rcu_dereference(conf->disks[i].rdev);
1848 if (rdev && test_bit(Faulty, &rdev->flags))
1849 rdev = NULL;
1850 if (rdev)
1851 atomic_inc(&rdev->nr_pending);
1852 rcu_read_unlock();
1854 if (rdev) {
1855 if (syncing || expanding || expanded)
1856 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
1858 bi->bi_bdev = rdev->bdev;
1859 PRINTK("for %llu schedule op %ld on disc %d\n",
1860 (unsigned long long)sh->sector, bi->bi_rw, i);
1861 atomic_inc(&sh->count);
1862 bi->bi_sector = sh->sector + rdev->data_offset;
1863 bi->bi_flags = 1 << BIO_UPTODATE;
1864 bi->bi_vcnt = 1;
1865 bi->bi_max_vecs = 1;
1866 bi->bi_idx = 0;
1867 bi->bi_io_vec = &sh->dev[i].vec;
1868 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
1869 bi->bi_io_vec[0].bv_offset = 0;
1870 bi->bi_size = STRIPE_SIZE;
1871 bi->bi_next = NULL;
1872 if (rw == WRITE &&
1873 test_bit(R5_ReWrite, &sh->dev[i].flags))
1874 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1875 generic_make_request(bi);
1876 } else {
1877 if (rw == WRITE)
1878 set_bit(STRIPE_DEGRADED, &sh->state);
1879 PRINTK("skip op %ld on disc %d for sector %llu\n",
1880 bi->bi_rw, i, (unsigned long long)sh->sector);
1881 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1882 set_bit(STRIPE_HANDLE, &sh->state);
1887 static void handle_stripe6(struct stripe_head *sh, struct page *tmp_page)
1889 raid6_conf_t *conf = sh->raid_conf;
1890 int disks = conf->raid_disks;
1891 struct bio *return_bi= NULL;
1892 struct bio *bi;
1893 int i;
1894 int syncing;
1895 int locked=0, uptodate=0, to_read=0, to_write=0, failed=0, written=0;
1896 int non_overwrite = 0;
1897 int failed_num[2] = {0, 0};
1898 struct r5dev *dev, *pdev, *qdev;
1899 int pd_idx = sh->pd_idx;
1900 int qd_idx = raid6_next_disk(pd_idx, disks);
1901 int p_failed, q_failed;
1903 PRINTK("handling stripe %llu, state=%#lx cnt=%d, pd_idx=%d, qd_idx=%d\n",
1904 (unsigned long long)sh->sector, sh->state, atomic_read(&sh->count),
1905 pd_idx, qd_idx);
1907 spin_lock(&sh->lock);
1908 clear_bit(STRIPE_HANDLE, &sh->state);
1909 clear_bit(STRIPE_DELAYED, &sh->state);
1911 syncing = test_bit(STRIPE_SYNCING, &sh->state);
1912 /* Now to look around and see what can be done */
1914 rcu_read_lock();
1915 for (i=disks; i--; ) {
1916 mdk_rdev_t *rdev;
1917 dev = &sh->dev[i];
1918 clear_bit(R5_Insync, &dev->flags);
1920 PRINTK("check %d: state 0x%lx read %p write %p written %p\n",
1921 i, dev->flags, dev->toread, dev->towrite, dev->written);
1922 /* maybe we can reply to a read */
1923 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread) {
1924 struct bio *rbi, *rbi2;
1925 PRINTK("Return read for disc %d\n", i);
1926 spin_lock_irq(&conf->device_lock);
1927 rbi = dev->toread;
1928 dev->toread = NULL;
1929 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1930 wake_up(&conf->wait_for_overlap);
1931 spin_unlock_irq(&conf->device_lock);
1932 while (rbi && rbi->bi_sector < dev->sector + STRIPE_SECTORS) {
1933 copy_data(0, rbi, dev->page, dev->sector);
1934 rbi2 = r5_next_bio(rbi, dev->sector);
1935 spin_lock_irq(&conf->device_lock);
1936 if (--rbi->bi_phys_segments == 0) {
1937 rbi->bi_next = return_bi;
1938 return_bi = rbi;
1940 spin_unlock_irq(&conf->device_lock);
1941 rbi = rbi2;
1945 /* now count some things */
1946 if (test_bit(R5_LOCKED, &dev->flags)) locked++;
1947 if (test_bit(R5_UPTODATE, &dev->flags)) uptodate++;
1950 if (dev->toread) to_read++;
1951 if (dev->towrite) {
1952 to_write++;
1953 if (!test_bit(R5_OVERWRITE, &dev->flags))
1954 non_overwrite++;
1956 if (dev->written) written++;
1957 rdev = rcu_dereference(conf->disks[i].rdev);
1958 if (!rdev || !test_bit(In_sync, &rdev->flags)) {
1959 /* The ReadError flag will just be confusing now */
1960 clear_bit(R5_ReadError, &dev->flags);
1961 clear_bit(R5_ReWrite, &dev->flags);
1963 if (!rdev || !test_bit(In_sync, &rdev->flags)
1964 || test_bit(R5_ReadError, &dev->flags)) {
1965 if ( failed < 2 )
1966 failed_num[failed] = i;
1967 failed++;
1968 } else
1969 set_bit(R5_Insync, &dev->flags);
1971 rcu_read_unlock();
1972 PRINTK("locked=%d uptodate=%d to_read=%d"
1973 " to_write=%d failed=%d failed_num=%d,%d\n",
1974 locked, uptodate, to_read, to_write, failed,
1975 failed_num[0], failed_num[1]);
1976 /* check if the array has lost >2 devices and, if so, some requests might
1977 * need to be failed
1979 if (failed > 2 && to_read+to_write+written) {
1980 for (i=disks; i--; ) {
1981 int bitmap_end = 0;
1983 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1984 mdk_rdev_t *rdev;
1985 rcu_read_lock();
1986 rdev = rcu_dereference(conf->disks[i].rdev);
1987 if (rdev && test_bit(In_sync, &rdev->flags))
1988 /* multiple read failures in one stripe */
1989 md_error(conf->mddev, rdev);
1990 rcu_read_unlock();
1993 spin_lock_irq(&conf->device_lock);
1994 /* fail all writes first */
1995 bi = sh->dev[i].towrite;
1996 sh->dev[i].towrite = NULL;
1997 if (bi) { to_write--; bitmap_end = 1; }
1999 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2000 wake_up(&conf->wait_for_overlap);
2002 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2003 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2004 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2005 if (--bi->bi_phys_segments == 0) {
2006 md_write_end(conf->mddev);
2007 bi->bi_next = return_bi;
2008 return_bi = bi;
2010 bi = nextbi;
2012 /* and fail all 'written' */
2013 bi = sh->dev[i].written;
2014 sh->dev[i].written = NULL;
2015 if (bi) bitmap_end = 1;
2016 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS) {
2017 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2018 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2019 if (--bi->bi_phys_segments == 0) {
2020 md_write_end(conf->mddev);
2021 bi->bi_next = return_bi;
2022 return_bi = bi;
2024 bi = bi2;
2027 /* fail any reads if this device is non-operational */
2028 if (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2029 test_bit(R5_ReadError, &sh->dev[i].flags)) {
2030 bi = sh->dev[i].toread;
2031 sh->dev[i].toread = NULL;
2032 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2033 wake_up(&conf->wait_for_overlap);
2034 if (bi) to_read--;
2035 while (bi && bi->bi_sector < sh->dev[i].sector + STRIPE_SECTORS){
2036 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2037 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2038 if (--bi->bi_phys_segments == 0) {
2039 bi->bi_next = return_bi;
2040 return_bi = bi;
2042 bi = nextbi;
2045 spin_unlock_irq(&conf->device_lock);
2046 if (bitmap_end)
2047 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2048 STRIPE_SECTORS, 0, 0);
2051 if (failed > 2 && syncing) {
2052 md_done_sync(conf->mddev, STRIPE_SECTORS,0);
2053 clear_bit(STRIPE_SYNCING, &sh->state);
2054 syncing = 0;
2058 * might be able to return some write requests if the parity blocks
2059 * are safe, or on a failed drive
2061 pdev = &sh->dev[pd_idx];
2062 p_failed = (failed >= 1 && failed_num[0] == pd_idx)
2063 || (failed >= 2 && failed_num[1] == pd_idx);
2064 qdev = &sh->dev[qd_idx];
2065 q_failed = (failed >= 1 && failed_num[0] == qd_idx)
2066 || (failed >= 2 && failed_num[1] == qd_idx);
2068 if ( written &&
2069 ( p_failed || ((test_bit(R5_Insync, &pdev->flags)
2070 && !test_bit(R5_LOCKED, &pdev->flags)
2071 && test_bit(R5_UPTODATE, &pdev->flags))) ) &&
2072 ( q_failed || ((test_bit(R5_Insync, &qdev->flags)
2073 && !test_bit(R5_LOCKED, &qdev->flags)
2074 && test_bit(R5_UPTODATE, &qdev->flags))) ) ) {
2075 /* any written block on an uptodate or failed drive can be
2076 * returned. Note that if we 'wrote' to a failed drive,
2077 * it will be UPTODATE, but never LOCKED, so we don't need
2078 * to test 'failed' directly.
2080 for (i=disks; i--; )
2081 if (sh->dev[i].written) {
2082 dev = &sh->dev[i];
2083 if (!test_bit(R5_LOCKED, &dev->flags) &&
2084 test_bit(R5_UPTODATE, &dev->flags) ) {
2085 /* We can return any write requests */
2086 int bitmap_end = 0;
2087 struct bio *wbi, *wbi2;
2088 PRINTK("Return write for stripe %llu disc %d\n",
2089 (unsigned long long)sh->sector, i);
2090 spin_lock_irq(&conf->device_lock);
2091 wbi = dev->written;
2092 dev->written = NULL;
2093 while (wbi && wbi->bi_sector < dev->sector + STRIPE_SECTORS) {
2094 wbi2 = r5_next_bio(wbi, dev->sector);
2095 if (--wbi->bi_phys_segments == 0) {
2096 md_write_end(conf->mddev);
2097 wbi->bi_next = return_bi;
2098 return_bi = wbi;
2100 wbi = wbi2;
2102 if (dev->towrite == NULL)
2103 bitmap_end = 1;
2104 spin_unlock_irq(&conf->device_lock);
2105 if (bitmap_end)
2106 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2107 STRIPE_SECTORS,
2108 !test_bit(STRIPE_DEGRADED, &sh->state), 0);
2113 /* Now we might consider reading some blocks, either to check/generate
2114 * parity, or to satisfy requests
2115 * or to load a block that is being partially written.
2117 if (to_read || non_overwrite || (to_write && failed) || (syncing && (uptodate < disks))) {
2118 for (i=disks; i--;) {
2119 dev = &sh->dev[i];
2120 if (!test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2121 (dev->toread ||
2122 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2123 syncing ||
2124 (failed >= 1 && (sh->dev[failed_num[0]].toread || to_write)) ||
2125 (failed >= 2 && (sh->dev[failed_num[1]].toread || to_write))
2128 /* we would like to get this block, possibly
2129 * by computing it, but we might not be able to
2131 if (uptodate == disks-1) {
2132 PRINTK("Computing stripe %llu block %d\n",
2133 (unsigned long long)sh->sector, i);
2134 compute_block_1(sh, i, 0);
2135 uptodate++;
2136 } else if ( uptodate == disks-2 && failed >= 2 ) {
2137 /* Computing 2-failure is *very* expensive; only do it if failed >= 2 */
2138 int other;
2139 for (other=disks; other--;) {
2140 if ( other == i )
2141 continue;
2142 if ( !test_bit(R5_UPTODATE, &sh->dev[other].flags) )
2143 break;
2145 BUG_ON(other < 0);
2146 PRINTK("Computing stripe %llu blocks %d,%d\n",
2147 (unsigned long long)sh->sector, i, other);
2148 compute_block_2(sh, i, other);
2149 uptodate += 2;
2150 } else if (test_bit(R5_Insync, &dev->flags)) {
2151 set_bit(R5_LOCKED, &dev->flags);
2152 set_bit(R5_Wantread, &dev->flags);
2153 locked++;
2154 PRINTK("Reading block %d (sync=%d)\n",
2155 i, syncing);
2159 set_bit(STRIPE_HANDLE, &sh->state);
2162 /* now to consider writing and what else, if anything should be read */
2163 if (to_write) {
2164 int rcw=0, must_compute=0;
2165 for (i=disks ; i--;) {
2166 dev = &sh->dev[i];
2167 /* Would I have to read this buffer for reconstruct_write */
2168 if (!test_bit(R5_OVERWRITE, &dev->flags)
2169 && i != pd_idx && i != qd_idx
2170 && (!test_bit(R5_LOCKED, &dev->flags)
2171 ) &&
2172 !test_bit(R5_UPTODATE, &dev->flags)) {
2173 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2174 else {
2175 PRINTK("raid6: must_compute: disk %d flags=%#lx\n", i, dev->flags);
2176 must_compute++;
2180 PRINTK("for sector %llu, rcw=%d, must_compute=%d\n",
2181 (unsigned long long)sh->sector, rcw, must_compute);
2182 set_bit(STRIPE_HANDLE, &sh->state);
2184 if (rcw > 0)
2185 /* want reconstruct write, but need to get some data */
2186 for (i=disks; i--;) {
2187 dev = &sh->dev[i];
2188 if (!test_bit(R5_OVERWRITE, &dev->flags)
2189 && !(failed == 0 && (i == pd_idx || i == qd_idx))
2190 && !test_bit(R5_LOCKED, &dev->flags) && !test_bit(R5_UPTODATE, &dev->flags) &&
2191 test_bit(R5_Insync, &dev->flags)) {
2192 if (test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2194 PRINTK("Read_old stripe %llu block %d for Reconstruct\n",
2195 (unsigned long long)sh->sector, i);
2196 set_bit(R5_LOCKED, &dev->flags);
2197 set_bit(R5_Wantread, &dev->flags);
2198 locked++;
2199 } else {
2200 PRINTK("Request delayed stripe %llu block %d for Reconstruct\n",
2201 (unsigned long long)sh->sector, i);
2202 set_bit(STRIPE_DELAYED, &sh->state);
2203 set_bit(STRIPE_HANDLE, &sh->state);
2207 /* now if nothing is locked, and if we have enough data, we can start a write request */
2208 if (locked == 0 && rcw == 0 &&
2209 !test_bit(STRIPE_BIT_DELAY, &sh->state)) {
2210 if ( must_compute > 0 ) {
2211 /* We have failed blocks and need to compute them */
2212 switch ( failed ) {
2213 case 0: BUG();
2214 case 1: compute_block_1(sh, failed_num[0], 0); break;
2215 case 2: compute_block_2(sh, failed_num[0], failed_num[1]); break;
2216 default: BUG(); /* This request should have been failed? */
2220 PRINTK("Computing parity for stripe %llu\n", (unsigned long long)sh->sector);
2221 compute_parity6(sh, RECONSTRUCT_WRITE);
2222 /* now every locked buffer is ready to be written */
2223 for (i=disks; i--;)
2224 if (test_bit(R5_LOCKED, &sh->dev[i].flags)) {
2225 PRINTK("Writing stripe %llu block %d\n",
2226 (unsigned long long)sh->sector, i);
2227 locked++;
2228 set_bit(R5_Wantwrite, &sh->dev[i].flags);
2230 /* after a RECONSTRUCT_WRITE, the stripe MUST be in-sync */
2231 set_bit(STRIPE_INSYNC, &sh->state);
2233 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2234 atomic_dec(&conf->preread_active_stripes);
2235 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
2236 md_wakeup_thread(conf->mddev->thread);
2241 /* maybe we need to check and possibly fix the parity for this stripe
2242 * Any reads will already have been scheduled, so we just see if enough data
2243 * is available
2245 if (syncing && locked == 0 && !test_bit(STRIPE_INSYNC, &sh->state)) {
2246 int update_p = 0, update_q = 0;
2247 struct r5dev *dev;
2249 set_bit(STRIPE_HANDLE, &sh->state);
2251 BUG_ON(failed>2);
2252 BUG_ON(uptodate < disks);
2253 /* Want to check and possibly repair P and Q.
2254 * However there could be one 'failed' device, in which
2255 * case we can only check one of them, possibly using the
2256 * other to generate missing data
2259 /* If !tmp_page, we cannot do the calculations,
2260 * but as we have set STRIPE_HANDLE, we will soon be called
2261 * by stripe_handle with a tmp_page - just wait until then.
2263 if (tmp_page) {
2264 if (failed == q_failed) {
2265 /* The only possible failed device holds 'Q', so it makes
2266 * sense to check P (If anything else were failed, we would
2267 * have used P to recreate it).
2269 compute_block_1(sh, pd_idx, 1);
2270 if (!page_is_zero(sh->dev[pd_idx].page)) {
2271 compute_block_1(sh,pd_idx,0);
2272 update_p = 1;
2275 if (!q_failed && failed < 2) {
2276 /* q is not failed, and we didn't use it to generate
2277 * anything, so it makes sense to check it
2279 memcpy(page_address(tmp_page),
2280 page_address(sh->dev[qd_idx].page),
2281 STRIPE_SIZE);
2282 compute_parity6(sh, UPDATE_PARITY);
2283 if (memcmp(page_address(tmp_page),
2284 page_address(sh->dev[qd_idx].page),
2285 STRIPE_SIZE)!= 0) {
2286 clear_bit(STRIPE_INSYNC, &sh->state);
2287 update_q = 1;
2290 if (update_p || update_q) {
2291 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2292 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2293 /* don't try to repair!! */
2294 update_p = update_q = 0;
2297 /* now write out any block on a failed drive,
2298 * or P or Q if they need it
2301 if (failed == 2) {
2302 dev = &sh->dev[failed_num[1]];
2303 locked++;
2304 set_bit(R5_LOCKED, &dev->flags);
2305 set_bit(R5_Wantwrite, &dev->flags);
2307 if (failed >= 1) {
2308 dev = &sh->dev[failed_num[0]];
2309 locked++;
2310 set_bit(R5_LOCKED, &dev->flags);
2311 set_bit(R5_Wantwrite, &dev->flags);
2314 if (update_p) {
2315 dev = &sh->dev[pd_idx];
2316 locked ++;
2317 set_bit(R5_LOCKED, &dev->flags);
2318 set_bit(R5_Wantwrite, &dev->flags);
2320 if (update_q) {
2321 dev = &sh->dev[qd_idx];
2322 locked++;
2323 set_bit(R5_LOCKED, &dev->flags);
2324 set_bit(R5_Wantwrite, &dev->flags);
2326 clear_bit(STRIPE_DEGRADED, &sh->state);
2328 set_bit(STRIPE_INSYNC, &sh->state);
2332 if (syncing && locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
2333 md_done_sync(conf->mddev, STRIPE_SECTORS,1);
2334 clear_bit(STRIPE_SYNCING, &sh->state);
2337 /* If the failed drives are just a ReadError, then we might need
2338 * to progress the repair/check process
2340 if (failed <= 2 && ! conf->mddev->ro)
2341 for (i=0; i<failed;i++) {
2342 dev = &sh->dev[failed_num[i]];
2343 if (test_bit(R5_ReadError, &dev->flags)
2344 && !test_bit(R5_LOCKED, &dev->flags)
2345 && test_bit(R5_UPTODATE, &dev->flags)
2347 if (!test_bit(R5_ReWrite, &dev->flags)) {
2348 set_bit(R5_Wantwrite, &dev->flags);
2349 set_bit(R5_ReWrite, &dev->flags);
2350 set_bit(R5_LOCKED, &dev->flags);
2351 } else {
2352 /* let's read it back */
2353 set_bit(R5_Wantread, &dev->flags);
2354 set_bit(R5_LOCKED, &dev->flags);
2358 spin_unlock(&sh->lock);
2360 while ((bi=return_bi)) {
2361 int bytes = bi->bi_size;
2363 return_bi = bi->bi_next;
2364 bi->bi_next = NULL;
2365 bi->bi_size = 0;
2366 bi->bi_end_io(bi, bytes,
2367 test_bit(BIO_UPTODATE, &bi->bi_flags)
2368 ? 0 : -EIO);
2370 for (i=disks; i-- ;) {
2371 int rw;
2372 struct bio *bi;
2373 mdk_rdev_t *rdev;
2374 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags))
2375 rw = WRITE;
2376 else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
2377 rw = READ;
2378 else
2379 continue;
2381 bi = &sh->dev[i].req;
2383 bi->bi_rw = rw;
2384 if (rw == WRITE)
2385 bi->bi_end_io = raid5_end_write_request;
2386 else
2387 bi->bi_end_io = raid5_end_read_request;
2389 rcu_read_lock();
2390 rdev = rcu_dereference(conf->disks[i].rdev);
2391 if (rdev && test_bit(Faulty, &rdev->flags))
2392 rdev = NULL;
2393 if (rdev)
2394 atomic_inc(&rdev->nr_pending);
2395 rcu_read_unlock();
2397 if (rdev) {
2398 if (syncing)
2399 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
2401 bi->bi_bdev = rdev->bdev;
2402 PRINTK("for %llu schedule op %ld on disc %d\n",
2403 (unsigned long long)sh->sector, bi->bi_rw, i);
2404 atomic_inc(&sh->count);
2405 bi->bi_sector = sh->sector + rdev->data_offset;
2406 bi->bi_flags = 1 << BIO_UPTODATE;
2407 bi->bi_vcnt = 1;
2408 bi->bi_max_vecs = 1;
2409 bi->bi_idx = 0;
2410 bi->bi_io_vec = &sh->dev[i].vec;
2411 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
2412 bi->bi_io_vec[0].bv_offset = 0;
2413 bi->bi_size = STRIPE_SIZE;
2414 bi->bi_next = NULL;
2415 if (rw == WRITE &&
2416 test_bit(R5_ReWrite, &sh->dev[i].flags))
2417 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
2418 generic_make_request(bi);
2419 } else {
2420 if (rw == WRITE)
2421 set_bit(STRIPE_DEGRADED, &sh->state);
2422 PRINTK("skip op %ld on disc %d for sector %llu\n",
2423 bi->bi_rw, i, (unsigned long long)sh->sector);
2424 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2425 set_bit(STRIPE_HANDLE, &sh->state);
2430 static void handle_stripe(struct stripe_head *sh, struct page *tmp_page)
2432 if (sh->raid_conf->level == 6)
2433 handle_stripe6(sh, tmp_page);
2434 else
2435 handle_stripe5(sh);
2440 static void raid5_activate_delayed(raid5_conf_t *conf)
2442 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
2443 while (!list_empty(&conf->delayed_list)) {
2444 struct list_head *l = conf->delayed_list.next;
2445 struct stripe_head *sh;
2446 sh = list_entry(l, struct stripe_head, lru);
2447 list_del_init(l);
2448 clear_bit(STRIPE_DELAYED, &sh->state);
2449 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
2450 atomic_inc(&conf->preread_active_stripes);
2451 list_add_tail(&sh->lru, &conf->handle_list);
2456 static void activate_bit_delay(raid5_conf_t *conf)
2458 /* device_lock is held */
2459 struct list_head head;
2460 list_add(&head, &conf->bitmap_list);
2461 list_del_init(&conf->bitmap_list);
2462 while (!list_empty(&head)) {
2463 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
2464 list_del_init(&sh->lru);
2465 atomic_inc(&sh->count);
2466 __release_stripe(conf, sh);
2470 static void unplug_slaves(mddev_t *mddev)
2472 raid5_conf_t *conf = mddev_to_conf(mddev);
2473 int i;
2475 rcu_read_lock();
2476 for (i=0; i<mddev->raid_disks; i++) {
2477 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
2478 if (rdev && !test_bit(Faulty, &rdev->flags) && atomic_read(&rdev->nr_pending)) {
2479 request_queue_t *r_queue = bdev_get_queue(rdev->bdev);
2481 atomic_inc(&rdev->nr_pending);
2482 rcu_read_unlock();
2484 if (r_queue->unplug_fn)
2485 r_queue->unplug_fn(r_queue);
2487 rdev_dec_pending(rdev, mddev);
2488 rcu_read_lock();
2491 rcu_read_unlock();
2494 static void raid5_unplug_device(request_queue_t *q)
2496 mddev_t *mddev = q->queuedata;
2497 raid5_conf_t *conf = mddev_to_conf(mddev);
2498 unsigned long flags;
2500 spin_lock_irqsave(&conf->device_lock, flags);
2502 if (blk_remove_plug(q)) {
2503 conf->seq_flush++;
2504 raid5_activate_delayed(conf);
2506 md_wakeup_thread(mddev->thread);
2508 spin_unlock_irqrestore(&conf->device_lock, flags);
2510 unplug_slaves(mddev);
2513 static int raid5_issue_flush(request_queue_t *q, struct gendisk *disk,
2514 sector_t *error_sector)
2516 mddev_t *mddev = q->queuedata;
2517 raid5_conf_t *conf = mddev_to_conf(mddev);
2518 int i, ret = 0;
2520 rcu_read_lock();
2521 for (i=0; i<mddev->raid_disks && ret == 0; i++) {
2522 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
2523 if (rdev && !test_bit(Faulty, &rdev->flags)) {
2524 struct block_device *bdev = rdev->bdev;
2525 request_queue_t *r_queue = bdev_get_queue(bdev);
2527 if (!r_queue->issue_flush_fn)
2528 ret = -EOPNOTSUPP;
2529 else {
2530 atomic_inc(&rdev->nr_pending);
2531 rcu_read_unlock();
2532 ret = r_queue->issue_flush_fn(r_queue, bdev->bd_disk,
2533 error_sector);
2534 rdev_dec_pending(rdev, mddev);
2535 rcu_read_lock();
2539 rcu_read_unlock();
2540 return ret;
2543 static int raid5_congested(void *data, int bits)
2545 mddev_t *mddev = data;
2546 raid5_conf_t *conf = mddev_to_conf(mddev);
2548 /* No difference between reads and writes. Just check
2549 * how busy the stripe_cache is
2551 if (conf->inactive_blocked)
2552 return 1;
2553 if (conf->quiesce)
2554 return 1;
2555 if (list_empty_careful(&conf->inactive_list))
2556 return 1;
2558 return 0;
2561 /* We want read requests to align with chunks where possible,
2562 * but write requests don't need to.
2564 static int raid5_mergeable_bvec(request_queue_t *q, struct bio *bio, struct bio_vec *biovec)
2566 mddev_t *mddev = q->queuedata;
2567 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2568 int max;
2569 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2570 unsigned int bio_sectors = bio->bi_size >> 9;
2572 if (bio_data_dir(bio) == WRITE)
2573 return biovec->bv_len; /* always allow writes to be mergeable */
2575 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
2576 if (max < 0) max = 0;
2577 if (max <= biovec->bv_len && bio_sectors == 0)
2578 return biovec->bv_len;
2579 else
2580 return max;
2584 static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
2586 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
2587 unsigned int chunk_sectors = mddev->chunk_size >> 9;
2588 unsigned int bio_sectors = bio->bi_size >> 9;
2590 return chunk_sectors >=
2591 ((sector & (chunk_sectors - 1)) + bio_sectors);
2595 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
2596 * later sampled by raid5d.
2598 static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
2600 unsigned long flags;
2602 spin_lock_irqsave(&conf->device_lock, flags);
2604 bi->bi_next = conf->retry_read_aligned_list;
2605 conf->retry_read_aligned_list = bi;
2607 spin_unlock_irqrestore(&conf->device_lock, flags);
2608 md_wakeup_thread(conf->mddev->thread);
2612 static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
2614 struct bio *bi;
2616 bi = conf->retry_read_aligned;
2617 if (bi) {
2618 conf->retry_read_aligned = NULL;
2619 return bi;
2621 bi = conf->retry_read_aligned_list;
2622 if(bi) {
2623 conf->retry_read_aligned = bi->bi_next;
2624 bi->bi_next = NULL;
2625 bi->bi_phys_segments = 1; /* biased count of active stripes */
2626 bi->bi_hw_segments = 0; /* count of processed stripes */
2629 return bi;
2634 * The "raid5_align_endio" should check if the read succeeded and if it
2635 * did, call bio_endio on the original bio (having bio_put the new bio
2636 * first).
2637 * If the read failed..
2639 static int raid5_align_endio(struct bio *bi, unsigned int bytes, int error)
2641 struct bio* raid_bi = bi->bi_private;
2642 mddev_t *mddev;
2643 raid5_conf_t *conf;
2644 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
2645 mdk_rdev_t *rdev;
2647 if (bi->bi_size)
2648 return 1;
2649 bio_put(bi);
2651 mddev = raid_bi->bi_bdev->bd_disk->queue->queuedata;
2652 conf = mddev_to_conf(mddev);
2653 rdev = (void*)raid_bi->bi_next;
2654 raid_bi->bi_next = NULL;
2656 rdev_dec_pending(rdev, conf->mddev);
2658 if (!error && uptodate) {
2659 bio_endio(raid_bi, bytes, 0);
2660 if (atomic_dec_and_test(&conf->active_aligned_reads))
2661 wake_up(&conf->wait_for_stripe);
2662 return 0;
2666 PRINTK("raid5_align_endio : io error...handing IO for a retry\n");
2668 add_bio_to_retry(raid_bi, conf);
2669 return 0;
2672 static int chunk_aligned_read(request_queue_t *q, struct bio * raid_bio)
2674 mddev_t *mddev = q->queuedata;
2675 raid5_conf_t *conf = mddev_to_conf(mddev);
2676 const unsigned int raid_disks = conf->raid_disks;
2677 const unsigned int data_disks = raid_disks - conf->max_degraded;
2678 unsigned int dd_idx, pd_idx;
2679 struct bio* align_bi;
2680 mdk_rdev_t *rdev;
2682 if (!in_chunk_boundary(mddev, raid_bio)) {
2683 PRINTK("chunk_aligned_read : non aligned\n");
2684 return 0;
2687 * use bio_clone to make a copy of the bio
2689 align_bi = bio_clone(raid_bio, GFP_NOIO);
2690 if (!align_bi)
2691 return 0;
2693 * set bi_end_io to a new function, and set bi_private to the
2694 * original bio.
2696 align_bi->bi_end_io = raid5_align_endio;
2697 align_bi->bi_private = raid_bio;
2699 * compute position
2701 align_bi->bi_sector = raid5_compute_sector(raid_bio->bi_sector,
2702 raid_disks,
2703 data_disks,
2704 &dd_idx,
2705 &pd_idx,
2706 conf);
2708 rcu_read_lock();
2709 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
2710 if (rdev && test_bit(In_sync, &rdev->flags)) {
2711 atomic_inc(&rdev->nr_pending);
2712 rcu_read_unlock();
2713 raid_bio->bi_next = (void*)rdev;
2714 align_bi->bi_bdev = rdev->bdev;
2715 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
2716 align_bi->bi_sector += rdev->data_offset;
2718 spin_lock_irq(&conf->device_lock);
2719 wait_event_lock_irq(conf->wait_for_stripe,
2720 conf->quiesce == 0,
2721 conf->device_lock, /* nothing */);
2722 atomic_inc(&conf->active_aligned_reads);
2723 spin_unlock_irq(&conf->device_lock);
2725 generic_make_request(align_bi);
2726 return 1;
2727 } else {
2728 rcu_read_unlock();
2729 bio_put(align_bi);
2730 return 0;
2735 static int make_request(request_queue_t *q, struct bio * bi)
2737 mddev_t *mddev = q->queuedata;
2738 raid5_conf_t *conf = mddev_to_conf(mddev);
2739 unsigned int dd_idx, pd_idx;
2740 sector_t new_sector;
2741 sector_t logical_sector, last_sector;
2742 struct stripe_head *sh;
2743 const int rw = bio_data_dir(bi);
2744 int remaining;
2746 if (unlikely(bio_barrier(bi))) {
2747 bio_endio(bi, bi->bi_size, -EOPNOTSUPP);
2748 return 0;
2751 md_write_start(mddev, bi);
2753 disk_stat_inc(mddev->gendisk, ios[rw]);
2754 disk_stat_add(mddev->gendisk, sectors[rw], bio_sectors(bi));
2756 if (rw == READ &&
2757 mddev->reshape_position == MaxSector &&
2758 chunk_aligned_read(q,bi))
2759 return 0;
2761 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
2762 last_sector = bi->bi_sector + (bi->bi_size>>9);
2763 bi->bi_next = NULL;
2764 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
2766 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
2767 DEFINE_WAIT(w);
2768 int disks, data_disks;
2770 retry:
2771 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
2772 if (likely(conf->expand_progress == MaxSector))
2773 disks = conf->raid_disks;
2774 else {
2775 /* spinlock is needed as expand_progress may be
2776 * 64bit on a 32bit platform, and so it might be
2777 * possible to see a half-updated value
2778 * Ofcourse expand_progress could change after
2779 * the lock is dropped, so once we get a reference
2780 * to the stripe that we think it is, we will have
2781 * to check again.
2783 spin_lock_irq(&conf->device_lock);
2784 disks = conf->raid_disks;
2785 if (logical_sector >= conf->expand_progress)
2786 disks = conf->previous_raid_disks;
2787 else {
2788 if (logical_sector >= conf->expand_lo) {
2789 spin_unlock_irq(&conf->device_lock);
2790 schedule();
2791 goto retry;
2794 spin_unlock_irq(&conf->device_lock);
2796 data_disks = disks - conf->max_degraded;
2798 new_sector = raid5_compute_sector(logical_sector, disks, data_disks,
2799 &dd_idx, &pd_idx, conf);
2800 PRINTK("raid5: make_request, sector %llu logical %llu\n",
2801 (unsigned long long)new_sector,
2802 (unsigned long long)logical_sector);
2804 sh = get_active_stripe(conf, new_sector, disks, pd_idx, (bi->bi_rw&RWA_MASK));
2805 if (sh) {
2806 if (unlikely(conf->expand_progress != MaxSector)) {
2807 /* expansion might have moved on while waiting for a
2808 * stripe, so we must do the range check again.
2809 * Expansion could still move past after this
2810 * test, but as we are holding a reference to
2811 * 'sh', we know that if that happens,
2812 * STRIPE_EXPANDING will get set and the expansion
2813 * won't proceed until we finish with the stripe.
2815 int must_retry = 0;
2816 spin_lock_irq(&conf->device_lock);
2817 if (logical_sector < conf->expand_progress &&
2818 disks == conf->previous_raid_disks)
2819 /* mismatch, need to try again */
2820 must_retry = 1;
2821 spin_unlock_irq(&conf->device_lock);
2822 if (must_retry) {
2823 release_stripe(sh);
2824 goto retry;
2827 /* FIXME what if we get a false positive because these
2828 * are being updated.
2830 if (logical_sector >= mddev->suspend_lo &&
2831 logical_sector < mddev->suspend_hi) {
2832 release_stripe(sh);
2833 schedule();
2834 goto retry;
2837 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
2838 !add_stripe_bio(sh, bi, dd_idx, (bi->bi_rw&RW_MASK))) {
2839 /* Stripe is busy expanding or
2840 * add failed due to overlap. Flush everything
2841 * and wait a while
2843 raid5_unplug_device(mddev->queue);
2844 release_stripe(sh);
2845 schedule();
2846 goto retry;
2848 finish_wait(&conf->wait_for_overlap, &w);
2849 handle_stripe(sh, NULL);
2850 release_stripe(sh);
2851 } else {
2852 /* cannot get stripe for read-ahead, just give-up */
2853 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2854 finish_wait(&conf->wait_for_overlap, &w);
2855 break;
2859 spin_lock_irq(&conf->device_lock);
2860 remaining = --bi->bi_phys_segments;
2861 spin_unlock_irq(&conf->device_lock);
2862 if (remaining == 0) {
2863 int bytes = bi->bi_size;
2865 if ( rw == WRITE )
2866 md_write_end(mddev);
2867 bi->bi_size = 0;
2868 bi->bi_end_io(bi, bytes,
2869 test_bit(BIO_UPTODATE, &bi->bi_flags)
2870 ? 0 : -EIO);
2872 return 0;
2875 static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
2877 /* reshaping is quite different to recovery/resync so it is
2878 * handled quite separately ... here.
2880 * On each call to sync_request, we gather one chunk worth of
2881 * destination stripes and flag them as expanding.
2882 * Then we find all the source stripes and request reads.
2883 * As the reads complete, handle_stripe will copy the data
2884 * into the destination stripe and release that stripe.
2886 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
2887 struct stripe_head *sh;
2888 int pd_idx;
2889 sector_t first_sector, last_sector;
2890 int raid_disks;
2891 int data_disks;
2892 int i;
2893 int dd_idx;
2894 sector_t writepos, safepos, gap;
2896 if (sector_nr == 0 &&
2897 conf->expand_progress != 0) {
2898 /* restarting in the middle, skip the initial sectors */
2899 sector_nr = conf->expand_progress;
2900 sector_div(sector_nr, conf->raid_disks-1);
2901 *skipped = 1;
2902 return sector_nr;
2905 /* we update the metadata when there is more than 3Meg
2906 * in the block range (that is rather arbitrary, should
2907 * probably be time based) or when the data about to be
2908 * copied would over-write the source of the data at
2909 * the front of the range.
2910 * i.e. one new_stripe forward from expand_progress new_maps
2911 * to after where expand_lo old_maps to
2913 writepos = conf->expand_progress +
2914 conf->chunk_size/512*(conf->raid_disks-1);
2915 sector_div(writepos, conf->raid_disks-1);
2916 safepos = conf->expand_lo;
2917 sector_div(safepos, conf->previous_raid_disks-1);
2918 gap = conf->expand_progress - conf->expand_lo;
2920 if (writepos >= safepos ||
2921 gap > (conf->raid_disks-1)*3000*2 /*3Meg*/) {
2922 /* Cannot proceed until we've updated the superblock... */
2923 wait_event(conf->wait_for_overlap,
2924 atomic_read(&conf->reshape_stripes)==0);
2925 mddev->reshape_position = conf->expand_progress;
2926 set_bit(MD_CHANGE_DEVS, &mddev->flags);
2927 md_wakeup_thread(mddev->thread);
2928 wait_event(mddev->sb_wait, mddev->flags == 0 ||
2929 kthread_should_stop());
2930 spin_lock_irq(&conf->device_lock);
2931 conf->expand_lo = mddev->reshape_position;
2932 spin_unlock_irq(&conf->device_lock);
2933 wake_up(&conf->wait_for_overlap);
2936 for (i=0; i < conf->chunk_size/512; i+= STRIPE_SECTORS) {
2937 int j;
2938 int skipped = 0;
2939 pd_idx = stripe_to_pdidx(sector_nr+i, conf, conf->raid_disks);
2940 sh = get_active_stripe(conf, sector_nr+i,
2941 conf->raid_disks, pd_idx, 0);
2942 set_bit(STRIPE_EXPANDING, &sh->state);
2943 atomic_inc(&conf->reshape_stripes);
2944 /* If any of this stripe is beyond the end of the old
2945 * array, then we need to zero those blocks
2947 for (j=sh->disks; j--;) {
2948 sector_t s;
2949 if (j == sh->pd_idx)
2950 continue;
2951 s = compute_blocknr(sh, j);
2952 if (s < (mddev->array_size<<1)) {
2953 skipped = 1;
2954 continue;
2956 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
2957 set_bit(R5_Expanded, &sh->dev[j].flags);
2958 set_bit(R5_UPTODATE, &sh->dev[j].flags);
2960 if (!skipped) {
2961 set_bit(STRIPE_EXPAND_READY, &sh->state);
2962 set_bit(STRIPE_HANDLE, &sh->state);
2964 release_stripe(sh);
2966 spin_lock_irq(&conf->device_lock);
2967 conf->expand_progress = (sector_nr + i)*(conf->raid_disks-1);
2968 spin_unlock_irq(&conf->device_lock);
2969 /* Ok, those stripe are ready. We can start scheduling
2970 * reads on the source stripes.
2971 * The source stripes are determined by mapping the first and last
2972 * block on the destination stripes.
2974 raid_disks = conf->previous_raid_disks;
2975 data_disks = raid_disks - 1;
2976 first_sector =
2977 raid5_compute_sector(sector_nr*(conf->raid_disks-1),
2978 raid_disks, data_disks,
2979 &dd_idx, &pd_idx, conf);
2980 last_sector =
2981 raid5_compute_sector((sector_nr+conf->chunk_size/512)
2982 *(conf->raid_disks-1) -1,
2983 raid_disks, data_disks,
2984 &dd_idx, &pd_idx, conf);
2985 if (last_sector >= (mddev->size<<1))
2986 last_sector = (mddev->size<<1)-1;
2987 while (first_sector <= last_sector) {
2988 pd_idx = stripe_to_pdidx(first_sector, conf, conf->previous_raid_disks);
2989 sh = get_active_stripe(conf, first_sector,
2990 conf->previous_raid_disks, pd_idx, 0);
2991 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2992 set_bit(STRIPE_HANDLE, &sh->state);
2993 release_stripe(sh);
2994 first_sector += STRIPE_SECTORS;
2996 return conf->chunk_size>>9;
2999 /* FIXME go_faster isn't used */
3000 static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
3002 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3003 struct stripe_head *sh;
3004 int pd_idx;
3005 int raid_disks = conf->raid_disks;
3006 sector_t max_sector = mddev->size << 1;
3007 int sync_blocks;
3008 int still_degraded = 0;
3009 int i;
3011 if (sector_nr >= max_sector) {
3012 /* just being told to finish up .. nothing much to do */
3013 unplug_slaves(mddev);
3014 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
3015 end_reshape(conf);
3016 return 0;
3019 if (mddev->curr_resync < max_sector) /* aborted */
3020 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
3021 &sync_blocks, 1);
3022 else /* completed sync */
3023 conf->fullsync = 0;
3024 bitmap_close_sync(mddev->bitmap);
3026 return 0;
3029 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
3030 return reshape_request(mddev, sector_nr, skipped);
3032 /* if there is too many failed drives and we are trying
3033 * to resync, then assert that we are finished, because there is
3034 * nothing we can do.
3036 if (mddev->degraded >= conf->max_degraded &&
3037 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
3038 sector_t rv = (mddev->size << 1) - sector_nr;
3039 *skipped = 1;
3040 return rv;
3042 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
3043 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
3044 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
3045 /* we can skip this block, and probably more */
3046 sync_blocks /= STRIPE_SECTORS;
3047 *skipped = 1;
3048 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
3051 pd_idx = stripe_to_pdidx(sector_nr, conf, raid_disks);
3052 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 1);
3053 if (sh == NULL) {
3054 sh = get_active_stripe(conf, sector_nr, raid_disks, pd_idx, 0);
3055 /* make sure we don't swamp the stripe cache if someone else
3056 * is trying to get access
3058 schedule_timeout_uninterruptible(1);
3060 /* Need to check if array will still be degraded after recovery/resync
3061 * We don't need to check the 'failed' flag as when that gets set,
3062 * recovery aborts.
3064 for (i=0; i<mddev->raid_disks; i++)
3065 if (conf->disks[i].rdev == NULL)
3066 still_degraded = 1;
3068 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
3070 spin_lock(&sh->lock);
3071 set_bit(STRIPE_SYNCING, &sh->state);
3072 clear_bit(STRIPE_INSYNC, &sh->state);
3073 spin_unlock(&sh->lock);
3075 handle_stripe(sh, NULL);
3076 release_stripe(sh);
3078 return STRIPE_SECTORS;
3081 static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
3083 /* We may not be able to submit a whole bio at once as there
3084 * may not be enough stripe_heads available.
3085 * We cannot pre-allocate enough stripe_heads as we may need
3086 * more than exist in the cache (if we allow ever large chunks).
3087 * So we do one stripe head at a time and record in
3088 * ->bi_hw_segments how many have been done.
3090 * We *know* that this entire raid_bio is in one chunk, so
3091 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
3093 struct stripe_head *sh;
3094 int dd_idx, pd_idx;
3095 sector_t sector, logical_sector, last_sector;
3096 int scnt = 0;
3097 int remaining;
3098 int handled = 0;
3100 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3101 sector = raid5_compute_sector( logical_sector,
3102 conf->raid_disks,
3103 conf->raid_disks - conf->max_degraded,
3104 &dd_idx,
3105 &pd_idx,
3106 conf);
3107 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
3109 for (; logical_sector < last_sector;
3110 logical_sector += STRIPE_SECTORS, scnt++) {
3112 if (scnt < raid_bio->bi_hw_segments)
3113 /* already done this stripe */
3114 continue;
3116 sh = get_active_stripe(conf, sector, conf->raid_disks, pd_idx, 1);
3118 if (!sh) {
3119 /* failed to get a stripe - must wait */
3120 raid_bio->bi_hw_segments = scnt;
3121 conf->retry_read_aligned = raid_bio;
3122 return handled;
3125 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
3126 add_stripe_bio(sh, raid_bio, dd_idx, 0);
3127 handle_stripe(sh, NULL);
3128 release_stripe(sh);
3129 handled++;
3131 spin_lock_irq(&conf->device_lock);
3132 remaining = --raid_bio->bi_phys_segments;
3133 spin_unlock_irq(&conf->device_lock);
3134 if (remaining == 0) {
3135 int bytes = raid_bio->bi_size;
3137 raid_bio->bi_size = 0;
3138 raid_bio->bi_end_io(raid_bio, bytes,
3139 test_bit(BIO_UPTODATE, &raid_bio->bi_flags)
3140 ? 0 : -EIO);
3142 if (atomic_dec_and_test(&conf->active_aligned_reads))
3143 wake_up(&conf->wait_for_stripe);
3144 return handled;
3150 * This is our raid5 kernel thread.
3152 * We scan the hash table for stripes which can be handled now.
3153 * During the scan, completed stripes are saved for us by the interrupt
3154 * handler, so that they will not have to wait for our next wakeup.
3156 static void raid5d (mddev_t *mddev)
3158 struct stripe_head *sh;
3159 raid5_conf_t *conf = mddev_to_conf(mddev);
3160 int handled;
3162 PRINTK("+++ raid5d active\n");
3164 md_check_recovery(mddev);
3166 handled = 0;
3167 spin_lock_irq(&conf->device_lock);
3168 while (1) {
3169 struct list_head *first;
3170 struct bio *bio;
3172 if (conf->seq_flush != conf->seq_write) {
3173 int seq = conf->seq_flush;
3174 spin_unlock_irq(&conf->device_lock);
3175 bitmap_unplug(mddev->bitmap);
3176 spin_lock_irq(&conf->device_lock);
3177 conf->seq_write = seq;
3178 activate_bit_delay(conf);
3181 if (list_empty(&conf->handle_list) &&
3182 atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD &&
3183 !blk_queue_plugged(mddev->queue) &&
3184 !list_empty(&conf->delayed_list))
3185 raid5_activate_delayed(conf);
3187 while ((bio = remove_bio_from_retry(conf))) {
3188 int ok;
3189 spin_unlock_irq(&conf->device_lock);
3190 ok = retry_aligned_read(conf, bio);
3191 spin_lock_irq(&conf->device_lock);
3192 if (!ok)
3193 break;
3194 handled++;
3197 if (list_empty(&conf->handle_list))
3198 break;
3200 first = conf->handle_list.next;
3201 sh = list_entry(first, struct stripe_head, lru);
3203 list_del_init(first);
3204 atomic_inc(&sh->count);
3205 BUG_ON(atomic_read(&sh->count)!= 1);
3206 spin_unlock_irq(&conf->device_lock);
3208 handled++;
3209 handle_stripe(sh, conf->spare_page);
3210 release_stripe(sh);
3212 spin_lock_irq(&conf->device_lock);
3214 PRINTK("%d stripes handled\n", handled);
3216 spin_unlock_irq(&conf->device_lock);
3218 unplug_slaves(mddev);
3220 PRINTK("--- raid5d inactive\n");
3223 static ssize_t
3224 raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
3226 raid5_conf_t *conf = mddev_to_conf(mddev);
3227 if (conf)
3228 return sprintf(page, "%d\n", conf->max_nr_stripes);
3229 else
3230 return 0;
3233 static ssize_t
3234 raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
3236 raid5_conf_t *conf = mddev_to_conf(mddev);
3237 char *end;
3238 int new;
3239 if (len >= PAGE_SIZE)
3240 return -EINVAL;
3241 if (!conf)
3242 return -ENODEV;
3244 new = simple_strtoul(page, &end, 10);
3245 if (!*page || (*end && *end != '\n') )
3246 return -EINVAL;
3247 if (new <= 16 || new > 32768)
3248 return -EINVAL;
3249 while (new < conf->max_nr_stripes) {
3250 if (drop_one_stripe(conf))
3251 conf->max_nr_stripes--;
3252 else
3253 break;
3255 md_allow_write(mddev);
3256 while (new > conf->max_nr_stripes) {
3257 if (grow_one_stripe(conf))
3258 conf->max_nr_stripes++;
3259 else break;
3261 return len;
3264 static struct md_sysfs_entry
3265 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
3266 raid5_show_stripe_cache_size,
3267 raid5_store_stripe_cache_size);
3269 static ssize_t
3270 stripe_cache_active_show(mddev_t *mddev, char *page)
3272 raid5_conf_t *conf = mddev_to_conf(mddev);
3273 if (conf)
3274 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
3275 else
3276 return 0;
3279 static struct md_sysfs_entry
3280 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
3282 static struct attribute *raid5_attrs[] = {
3283 &raid5_stripecache_size.attr,
3284 &raid5_stripecache_active.attr,
3285 NULL,
3287 static struct attribute_group raid5_attrs_group = {
3288 .name = NULL,
3289 .attrs = raid5_attrs,
3292 static int run(mddev_t *mddev)
3294 raid5_conf_t *conf;
3295 int raid_disk, memory;
3296 mdk_rdev_t *rdev;
3297 struct disk_info *disk;
3298 struct list_head *tmp;
3299 int working_disks = 0;
3301 if (mddev->level != 5 && mddev->level != 4 && mddev->level != 6) {
3302 printk(KERN_ERR "raid5: %s: raid level not set to 4/5/6 (%d)\n",
3303 mdname(mddev), mddev->level);
3304 return -EIO;
3307 if (mddev->reshape_position != MaxSector) {
3308 /* Check that we can continue the reshape.
3309 * Currently only disks can change, it must
3310 * increase, and we must be past the point where
3311 * a stripe over-writes itself
3313 sector_t here_new, here_old;
3314 int old_disks;
3316 if (mddev->new_level != mddev->level ||
3317 mddev->new_layout != mddev->layout ||
3318 mddev->new_chunk != mddev->chunk_size) {
3319 printk(KERN_ERR "raid5: %s: unsupported reshape required - aborting.\n",
3320 mdname(mddev));
3321 return -EINVAL;
3323 if (mddev->delta_disks <= 0) {
3324 printk(KERN_ERR "raid5: %s: unsupported reshape (reduce disks) required - aborting.\n",
3325 mdname(mddev));
3326 return -EINVAL;
3328 old_disks = mddev->raid_disks - mddev->delta_disks;
3329 /* reshape_position must be on a new-stripe boundary, and one
3330 * further up in new geometry must map after here in old geometry.
3332 here_new = mddev->reshape_position;
3333 if (sector_div(here_new, (mddev->chunk_size>>9)*(mddev->raid_disks-1))) {
3334 printk(KERN_ERR "raid5: reshape_position not on a stripe boundary\n");
3335 return -EINVAL;
3337 /* here_new is the stripe we will write to */
3338 here_old = mddev->reshape_position;
3339 sector_div(here_old, (mddev->chunk_size>>9)*(old_disks-1));
3340 /* here_old is the first stripe that we might need to read from */
3341 if (here_new >= here_old) {
3342 /* Reading from the same stripe as writing to - bad */
3343 printk(KERN_ERR "raid5: reshape_position too early for auto-recovery - aborting.\n");
3344 return -EINVAL;
3346 printk(KERN_INFO "raid5: reshape will continue\n");
3347 /* OK, we should be able to continue; */
3351 mddev->private = kzalloc(sizeof (raid5_conf_t), GFP_KERNEL);
3352 if ((conf = mddev->private) == NULL)
3353 goto abort;
3354 if (mddev->reshape_position == MaxSector) {
3355 conf->previous_raid_disks = conf->raid_disks = mddev->raid_disks;
3356 } else {
3357 conf->raid_disks = mddev->raid_disks;
3358 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
3361 conf->disks = kzalloc(conf->raid_disks * sizeof(struct disk_info),
3362 GFP_KERNEL);
3363 if (!conf->disks)
3364 goto abort;
3366 conf->mddev = mddev;
3368 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
3369 goto abort;
3371 if (mddev->level == 6) {
3372 conf->spare_page = alloc_page(GFP_KERNEL);
3373 if (!conf->spare_page)
3374 goto abort;
3376 spin_lock_init(&conf->device_lock);
3377 init_waitqueue_head(&conf->wait_for_stripe);
3378 init_waitqueue_head(&conf->wait_for_overlap);
3379 INIT_LIST_HEAD(&conf->handle_list);
3380 INIT_LIST_HEAD(&conf->delayed_list);
3381 INIT_LIST_HEAD(&conf->bitmap_list);
3382 INIT_LIST_HEAD(&conf->inactive_list);
3383 atomic_set(&conf->active_stripes, 0);
3384 atomic_set(&conf->preread_active_stripes, 0);
3385 atomic_set(&conf->active_aligned_reads, 0);
3387 PRINTK("raid5: run(%s) called.\n", mdname(mddev));
3389 ITERATE_RDEV(mddev,rdev,tmp) {
3390 raid_disk = rdev->raid_disk;
3391 if (raid_disk >= conf->raid_disks
3392 || raid_disk < 0)
3393 continue;
3394 disk = conf->disks + raid_disk;
3396 disk->rdev = rdev;
3398 if (test_bit(In_sync, &rdev->flags)) {
3399 char b[BDEVNAME_SIZE];
3400 printk(KERN_INFO "raid5: device %s operational as raid"
3401 " disk %d\n", bdevname(rdev->bdev,b),
3402 raid_disk);
3403 working_disks++;
3408 * 0 for a fully functional array, 1 or 2 for a degraded array.
3410 mddev->degraded = conf->raid_disks - working_disks;
3411 conf->mddev = mddev;
3412 conf->chunk_size = mddev->chunk_size;
3413 conf->level = mddev->level;
3414 if (conf->level == 6)
3415 conf->max_degraded = 2;
3416 else
3417 conf->max_degraded = 1;
3418 conf->algorithm = mddev->layout;
3419 conf->max_nr_stripes = NR_STRIPES;
3420 conf->expand_progress = mddev->reshape_position;
3422 /* device size must be a multiple of chunk size */
3423 mddev->size &= ~(mddev->chunk_size/1024 -1);
3424 mddev->resync_max_sectors = mddev->size << 1;
3426 if (conf->level == 6 && conf->raid_disks < 4) {
3427 printk(KERN_ERR "raid6: not enough configured devices for %s (%d, minimum 4)\n",
3428 mdname(mddev), conf->raid_disks);
3429 goto abort;
3431 if (!conf->chunk_size || conf->chunk_size % 4) {
3432 printk(KERN_ERR "raid5: invalid chunk size %d for %s\n",
3433 conf->chunk_size, mdname(mddev));
3434 goto abort;
3436 if (conf->algorithm > ALGORITHM_RIGHT_SYMMETRIC) {
3437 printk(KERN_ERR
3438 "raid5: unsupported parity algorithm %d for %s\n",
3439 conf->algorithm, mdname(mddev));
3440 goto abort;
3442 if (mddev->degraded > conf->max_degraded) {
3443 printk(KERN_ERR "raid5: not enough operational devices for %s"
3444 " (%d/%d failed)\n",
3445 mdname(mddev), mddev->degraded, conf->raid_disks);
3446 goto abort;
3449 if (mddev->degraded > 0 &&
3450 mddev->recovery_cp != MaxSector) {
3451 if (mddev->ok_start_degraded)
3452 printk(KERN_WARNING
3453 "raid5: starting dirty degraded array: %s"
3454 "- data corruption possible.\n",
3455 mdname(mddev));
3456 else {
3457 printk(KERN_ERR
3458 "raid5: cannot start dirty degraded array for %s\n",
3459 mdname(mddev));
3460 goto abort;
3465 mddev->thread = md_register_thread(raid5d, mddev, "%s_raid5");
3466 if (!mddev->thread) {
3467 printk(KERN_ERR
3468 "raid5: couldn't allocate thread for %s\n",
3469 mdname(mddev));
3470 goto abort;
3473 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
3474 conf->raid_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
3475 if (grow_stripes(conf, conf->max_nr_stripes)) {
3476 printk(KERN_ERR
3477 "raid5: couldn't allocate %dkB for buffers\n", memory);
3478 shrink_stripes(conf);
3479 md_unregister_thread(mddev->thread);
3480 goto abort;
3481 } else
3482 printk(KERN_INFO "raid5: allocated %dkB for %s\n",
3483 memory, mdname(mddev));
3485 if (mddev->degraded == 0)
3486 printk("raid5: raid level %d set %s active with %d out of %d"
3487 " devices, algorithm %d\n", conf->level, mdname(mddev),
3488 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
3489 conf->algorithm);
3490 else
3491 printk(KERN_ALERT "raid5: raid level %d set %s active with %d"
3492 " out of %d devices, algorithm %d\n", conf->level,
3493 mdname(mddev), mddev->raid_disks - mddev->degraded,
3494 mddev->raid_disks, conf->algorithm);
3496 print_raid5_conf(conf);
3498 if (conf->expand_progress != MaxSector) {
3499 printk("...ok start reshape thread\n");
3500 conf->expand_lo = conf->expand_progress;
3501 atomic_set(&conf->reshape_stripes, 0);
3502 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3503 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3504 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3505 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3506 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3507 "%s_reshape");
3510 /* read-ahead size must cover two whole stripes, which is
3511 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3514 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3515 int stripe = data_disks *
3516 (mddev->chunk_size / PAGE_SIZE);
3517 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3518 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3521 /* Ok, everything is just fine now */
3522 sysfs_create_group(&mddev->kobj, &raid5_attrs_group);
3524 mddev->queue->unplug_fn = raid5_unplug_device;
3525 mddev->queue->issue_flush_fn = raid5_issue_flush;
3526 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
3527 mddev->queue->backing_dev_info.congested_data = mddev;
3529 mddev->array_size = mddev->size * (conf->previous_raid_disks -
3530 conf->max_degraded);
3532 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
3534 return 0;
3535 abort:
3536 if (conf) {
3537 print_raid5_conf(conf);
3538 safe_put_page(conf->spare_page);
3539 kfree(conf->disks);
3540 kfree(conf->stripe_hashtbl);
3541 kfree(conf);
3543 mddev->private = NULL;
3544 printk(KERN_ALERT "raid5: failed to run raid set %s\n", mdname(mddev));
3545 return -EIO;
3550 static int stop(mddev_t *mddev)
3552 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3554 md_unregister_thread(mddev->thread);
3555 mddev->thread = NULL;
3556 shrink_stripes(conf);
3557 kfree(conf->stripe_hashtbl);
3558 blk_sync_queue(mddev->queue); /* the unplug fn references 'conf'*/
3559 sysfs_remove_group(&mddev->kobj, &raid5_attrs_group);
3560 kfree(conf->disks);
3561 kfree(conf);
3562 mddev->private = NULL;
3563 return 0;
3566 #if RAID5_DEBUG
3567 static void print_sh (struct seq_file *seq, struct stripe_head *sh)
3569 int i;
3571 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
3572 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
3573 seq_printf(seq, "sh %llu, count %d.\n",
3574 (unsigned long long)sh->sector, atomic_read(&sh->count));
3575 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
3576 for (i = 0; i < sh->disks; i++) {
3577 seq_printf(seq, "(cache%d: %p %ld) ",
3578 i, sh->dev[i].page, sh->dev[i].flags);
3580 seq_printf(seq, "\n");
3583 static void printall (struct seq_file *seq, raid5_conf_t *conf)
3585 struct stripe_head *sh;
3586 struct hlist_node *hn;
3587 int i;
3589 spin_lock_irq(&conf->device_lock);
3590 for (i = 0; i < NR_HASH; i++) {
3591 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
3592 if (sh->raid_conf != conf)
3593 continue;
3594 print_sh(seq, sh);
3597 spin_unlock_irq(&conf->device_lock);
3599 #endif
3601 static void status (struct seq_file *seq, mddev_t *mddev)
3603 raid5_conf_t *conf = (raid5_conf_t *) mddev->private;
3604 int i;
3606 seq_printf (seq, " level %d, %dk chunk, algorithm %d", mddev->level, mddev->chunk_size >> 10, mddev->layout);
3607 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
3608 for (i = 0; i < conf->raid_disks; i++)
3609 seq_printf (seq, "%s",
3610 conf->disks[i].rdev &&
3611 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
3612 seq_printf (seq, "]");
3613 #if RAID5_DEBUG
3614 seq_printf (seq, "\n");
3615 printall(seq, conf);
3616 #endif
3619 static void print_raid5_conf (raid5_conf_t *conf)
3621 int i;
3622 struct disk_info *tmp;
3624 printk("RAID5 conf printout:\n");
3625 if (!conf) {
3626 printk("(conf==NULL)\n");
3627 return;
3629 printk(" --- rd:%d wd:%d\n", conf->raid_disks,
3630 conf->raid_disks - conf->mddev->degraded);
3632 for (i = 0; i < conf->raid_disks; i++) {
3633 char b[BDEVNAME_SIZE];
3634 tmp = conf->disks + i;
3635 if (tmp->rdev)
3636 printk(" disk %d, o:%d, dev:%s\n",
3637 i, !test_bit(Faulty, &tmp->rdev->flags),
3638 bdevname(tmp->rdev->bdev,b));
3642 static int raid5_spare_active(mddev_t *mddev)
3644 int i;
3645 raid5_conf_t *conf = mddev->private;
3646 struct disk_info *tmp;
3648 for (i = 0; i < conf->raid_disks; i++) {
3649 tmp = conf->disks + i;
3650 if (tmp->rdev
3651 && !test_bit(Faulty, &tmp->rdev->flags)
3652 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
3653 unsigned long flags;
3654 spin_lock_irqsave(&conf->device_lock, flags);
3655 mddev->degraded--;
3656 spin_unlock_irqrestore(&conf->device_lock, flags);
3659 print_raid5_conf(conf);
3660 return 0;
3663 static int raid5_remove_disk(mddev_t *mddev, int number)
3665 raid5_conf_t *conf = mddev->private;
3666 int err = 0;
3667 mdk_rdev_t *rdev;
3668 struct disk_info *p = conf->disks + number;
3670 print_raid5_conf(conf);
3671 rdev = p->rdev;
3672 if (rdev) {
3673 if (test_bit(In_sync, &rdev->flags) ||
3674 atomic_read(&rdev->nr_pending)) {
3675 err = -EBUSY;
3676 goto abort;
3678 p->rdev = NULL;
3679 synchronize_rcu();
3680 if (atomic_read(&rdev->nr_pending)) {
3681 /* lost the race, try later */
3682 err = -EBUSY;
3683 p->rdev = rdev;
3686 abort:
3688 print_raid5_conf(conf);
3689 return err;
3692 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
3694 raid5_conf_t *conf = mddev->private;
3695 int found = 0;
3696 int disk;
3697 struct disk_info *p;
3699 if (mddev->degraded > conf->max_degraded)
3700 /* no point adding a device */
3701 return 0;
3704 * find the disk ... but prefer rdev->saved_raid_disk
3705 * if possible.
3707 if (rdev->saved_raid_disk >= 0 &&
3708 conf->disks[rdev->saved_raid_disk].rdev == NULL)
3709 disk = rdev->saved_raid_disk;
3710 else
3711 disk = 0;
3712 for ( ; disk < conf->raid_disks; disk++)
3713 if ((p=conf->disks + disk)->rdev == NULL) {
3714 clear_bit(In_sync, &rdev->flags);
3715 rdev->raid_disk = disk;
3716 found = 1;
3717 if (rdev->saved_raid_disk != disk)
3718 conf->fullsync = 1;
3719 rcu_assign_pointer(p->rdev, rdev);
3720 break;
3722 print_raid5_conf(conf);
3723 return found;
3726 static int raid5_resize(mddev_t *mddev, sector_t sectors)
3728 /* no resync is happening, and there is enough space
3729 * on all devices, so we can resize.
3730 * We need to make sure resync covers any new space.
3731 * If the array is shrinking we should possibly wait until
3732 * any io in the removed space completes, but it hardly seems
3733 * worth it.
3735 raid5_conf_t *conf = mddev_to_conf(mddev);
3737 sectors &= ~((sector_t)mddev->chunk_size/512 - 1);
3738 mddev->array_size = (sectors * (mddev->raid_disks-conf->max_degraded))>>1;
3739 set_capacity(mddev->gendisk, mddev->array_size << 1);
3740 mddev->changed = 1;
3741 if (sectors/2 > mddev->size && mddev->recovery_cp == MaxSector) {
3742 mddev->recovery_cp = mddev->size << 1;
3743 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
3745 mddev->size = sectors /2;
3746 mddev->resync_max_sectors = sectors;
3747 return 0;
3750 #ifdef CONFIG_MD_RAID5_RESHAPE
3751 static int raid5_check_reshape(mddev_t *mddev)
3753 raid5_conf_t *conf = mddev_to_conf(mddev);
3754 int err;
3756 if (mddev->delta_disks < 0 ||
3757 mddev->new_level != mddev->level)
3758 return -EINVAL; /* Cannot shrink array or change level yet */
3759 if (mddev->delta_disks == 0)
3760 return 0; /* nothing to do */
3762 /* Can only proceed if there are plenty of stripe_heads.
3763 * We need a minimum of one full stripe,, and for sensible progress
3764 * it is best to have about 4 times that.
3765 * If we require 4 times, then the default 256 4K stripe_heads will
3766 * allow for chunk sizes up to 256K, which is probably OK.
3767 * If the chunk size is greater, user-space should request more
3768 * stripe_heads first.
3770 if ((mddev->chunk_size / STRIPE_SIZE) * 4 > conf->max_nr_stripes ||
3771 (mddev->new_chunk / STRIPE_SIZE) * 4 > conf->max_nr_stripes) {
3772 printk(KERN_WARNING "raid5: reshape: not enough stripes. Needed %lu\n",
3773 (mddev->chunk_size / STRIPE_SIZE)*4);
3774 return -ENOSPC;
3777 err = resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
3778 if (err)
3779 return err;
3781 /* looks like we might be able to manage this */
3782 return 0;
3785 static int raid5_start_reshape(mddev_t *mddev)
3787 raid5_conf_t *conf = mddev_to_conf(mddev);
3788 mdk_rdev_t *rdev;
3789 struct list_head *rtmp;
3790 int spares = 0;
3791 int added_devices = 0;
3792 unsigned long flags;
3794 if (mddev->degraded ||
3795 test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
3796 return -EBUSY;
3798 ITERATE_RDEV(mddev, rdev, rtmp)
3799 if (rdev->raid_disk < 0 &&
3800 !test_bit(Faulty, &rdev->flags))
3801 spares++;
3803 if (spares < mddev->delta_disks-1)
3804 /* Not enough devices even to make a degraded array
3805 * of that size
3807 return -EINVAL;
3809 atomic_set(&conf->reshape_stripes, 0);
3810 spin_lock_irq(&conf->device_lock);
3811 conf->previous_raid_disks = conf->raid_disks;
3812 conf->raid_disks += mddev->delta_disks;
3813 conf->expand_progress = 0;
3814 conf->expand_lo = 0;
3815 spin_unlock_irq(&conf->device_lock);
3817 /* Add some new drives, as many as will fit.
3818 * We know there are enough to make the newly sized array work.
3820 ITERATE_RDEV(mddev, rdev, rtmp)
3821 if (rdev->raid_disk < 0 &&
3822 !test_bit(Faulty, &rdev->flags)) {
3823 if (raid5_add_disk(mddev, rdev)) {
3824 char nm[20];
3825 set_bit(In_sync, &rdev->flags);
3826 added_devices++;
3827 rdev->recovery_offset = 0;
3828 sprintf(nm, "rd%d", rdev->raid_disk);
3829 sysfs_create_link(&mddev->kobj, &rdev->kobj, nm);
3830 } else
3831 break;
3834 spin_lock_irqsave(&conf->device_lock, flags);
3835 mddev->degraded = (conf->raid_disks - conf->previous_raid_disks) - added_devices;
3836 spin_unlock_irqrestore(&conf->device_lock, flags);
3837 mddev->raid_disks = conf->raid_disks;
3838 mddev->reshape_position = 0;
3839 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3841 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
3842 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
3843 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
3844 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
3845 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
3846 "%s_reshape");
3847 if (!mddev->sync_thread) {
3848 mddev->recovery = 0;
3849 spin_lock_irq(&conf->device_lock);
3850 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
3851 conf->expand_progress = MaxSector;
3852 spin_unlock_irq(&conf->device_lock);
3853 return -EAGAIN;
3855 md_wakeup_thread(mddev->sync_thread);
3856 md_new_event(mddev);
3857 return 0;
3859 #endif
3861 static void end_reshape(raid5_conf_t *conf)
3863 struct block_device *bdev;
3865 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
3866 conf->mddev->array_size = conf->mddev->size * (conf->raid_disks-1);
3867 set_capacity(conf->mddev->gendisk, conf->mddev->array_size << 1);
3868 conf->mddev->changed = 1;
3870 bdev = bdget_disk(conf->mddev->gendisk, 0);
3871 if (bdev) {
3872 mutex_lock(&bdev->bd_inode->i_mutex);
3873 i_size_write(bdev->bd_inode, (loff_t)conf->mddev->array_size << 10);
3874 mutex_unlock(&bdev->bd_inode->i_mutex);
3875 bdput(bdev);
3877 spin_lock_irq(&conf->device_lock);
3878 conf->expand_progress = MaxSector;
3879 spin_unlock_irq(&conf->device_lock);
3880 conf->mddev->reshape_position = MaxSector;
3882 /* read-ahead size must cover two whole stripes, which is
3883 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
3886 int data_disks = conf->previous_raid_disks - conf->max_degraded;
3887 int stripe = data_disks *
3888 (conf->mddev->chunk_size / PAGE_SIZE);
3889 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
3890 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
3895 static void raid5_quiesce(mddev_t *mddev, int state)
3897 raid5_conf_t *conf = mddev_to_conf(mddev);
3899 switch(state) {
3900 case 2: /* resume for a suspend */
3901 wake_up(&conf->wait_for_overlap);
3902 break;
3904 case 1: /* stop all writes */
3905 spin_lock_irq(&conf->device_lock);
3906 conf->quiesce = 1;
3907 wait_event_lock_irq(conf->wait_for_stripe,
3908 atomic_read(&conf->active_stripes) == 0 &&
3909 atomic_read(&conf->active_aligned_reads) == 0,
3910 conf->device_lock, /* nothing */);
3911 spin_unlock_irq(&conf->device_lock);
3912 break;
3914 case 0: /* re-enable writes */
3915 spin_lock_irq(&conf->device_lock);
3916 conf->quiesce = 0;
3917 wake_up(&conf->wait_for_stripe);
3918 wake_up(&conf->wait_for_overlap);
3919 spin_unlock_irq(&conf->device_lock);
3920 break;
3924 static struct mdk_personality raid6_personality =
3926 .name = "raid6",
3927 .level = 6,
3928 .owner = THIS_MODULE,
3929 .make_request = make_request,
3930 .run = run,
3931 .stop = stop,
3932 .status = status,
3933 .error_handler = error,
3934 .hot_add_disk = raid5_add_disk,
3935 .hot_remove_disk= raid5_remove_disk,
3936 .spare_active = raid5_spare_active,
3937 .sync_request = sync_request,
3938 .resize = raid5_resize,
3939 .quiesce = raid5_quiesce,
3941 static struct mdk_personality raid5_personality =
3943 .name = "raid5",
3944 .level = 5,
3945 .owner = THIS_MODULE,
3946 .make_request = make_request,
3947 .run = run,
3948 .stop = stop,
3949 .status = status,
3950 .error_handler = error,
3951 .hot_add_disk = raid5_add_disk,
3952 .hot_remove_disk= raid5_remove_disk,
3953 .spare_active = raid5_spare_active,
3954 .sync_request = sync_request,
3955 .resize = raid5_resize,
3956 #ifdef CONFIG_MD_RAID5_RESHAPE
3957 .check_reshape = raid5_check_reshape,
3958 .start_reshape = raid5_start_reshape,
3959 #endif
3960 .quiesce = raid5_quiesce,
3963 static struct mdk_personality raid4_personality =
3965 .name = "raid4",
3966 .level = 4,
3967 .owner = THIS_MODULE,
3968 .make_request = make_request,
3969 .run = run,
3970 .stop = stop,
3971 .status = status,
3972 .error_handler = error,
3973 .hot_add_disk = raid5_add_disk,
3974 .hot_remove_disk= raid5_remove_disk,
3975 .spare_active = raid5_spare_active,
3976 .sync_request = sync_request,
3977 .resize = raid5_resize,
3978 .quiesce = raid5_quiesce,
3981 static int __init raid5_init(void)
3983 int e;
3985 e = raid6_select_algo();
3986 if ( e )
3987 return e;
3988 register_md_personality(&raid6_personality);
3989 register_md_personality(&raid5_personality);
3990 register_md_personality(&raid4_personality);
3991 return 0;
3994 static void raid5_exit(void)
3996 unregister_md_personality(&raid6_personality);
3997 unregister_md_personality(&raid5_personality);
3998 unregister_md_personality(&raid4_personality);
4001 module_init(raid5_init);
4002 module_exit(raid5_exit);
4003 MODULE_LICENSE("GPL");
4004 MODULE_ALIAS("md-personality-4"); /* RAID5 */
4005 MODULE_ALIAS("md-raid5");
4006 MODULE_ALIAS("md-raid4");
4007 MODULE_ALIAS("md-level-5");
4008 MODULE_ALIAS("md-level-4");
4009 MODULE_ALIAS("md-personality-8"); /* RAID6 */
4010 MODULE_ALIAS("md-raid6");
4011 MODULE_ALIAS("md-level-6");
4013 /* This used to be two separate modules, they were: */
4014 MODULE_ALIAS("raid5");
4015 MODULE_ALIAS("raid6");