include: replace linux/module.h with "struct module" wherever possible
[linux-2.6/next.git] / drivers / md / raid5.c
blob6981b02141d77c4481c2a3cd77ea8f437e7588cf
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->seq_write is the number of the last batch successfully written.
31 * conf->seq_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 seq_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/blkdev.h>
47 #include <linux/kthread.h>
48 #include <linux/raid/pq.h>
49 #include <linux/async_tx.h>
50 #include <linux/module.h>
51 #include <linux/async.h>
52 #include <linux/seq_file.h>
53 #include <linux/cpu.h>
54 #include <linux/slab.h>
55 #include <linux/ratelimit.h>
56 #include "md.h"
57 #include "raid5.h"
58 #include "raid0.h"
59 #include "bitmap.h"
62 * Stripe cache
65 #define NR_STRIPES 256
66 #define STRIPE_SIZE PAGE_SIZE
67 #define STRIPE_SHIFT (PAGE_SHIFT - 9)
68 #define STRIPE_SECTORS (STRIPE_SIZE>>9)
69 #define IO_THRESHOLD 1
70 #define BYPASS_THRESHOLD 1
71 #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head))
72 #define HASH_MASK (NR_HASH - 1)
74 #define stripe_hash(conf, sect) (&((conf)->stripe_hashtbl[((sect) >> STRIPE_SHIFT) & HASH_MASK]))
76 /* bio's attached to a stripe+device for I/O are linked together in bi_sector
77 * order without overlap. There may be several bio's per stripe+device, and
78 * a bio could span several devices.
79 * When walking this list for a particular stripe+device, we must never proceed
80 * beyond a bio that extends past this device, as the next bio might no longer
81 * be valid.
82 * This macro is used to determine the 'next' bio in the list, given the sector
83 * of the current stripe+device
85 #define r5_next_bio(bio, sect) ( ( (bio)->bi_sector + ((bio)->bi_size>>9) < sect + STRIPE_SECTORS) ? (bio)->bi_next : NULL)
87 * The following can be used to debug the driver
89 #define RAID5_PARANOIA 1
90 #if RAID5_PARANOIA && defined(CONFIG_SMP)
91 # define CHECK_DEVLOCK() assert_spin_locked(&conf->device_lock)
92 #else
93 # define CHECK_DEVLOCK()
94 #endif
96 #ifdef DEBUG
97 #define inline
98 #define __inline__
99 #endif
102 * We maintain a biased count of active stripes in the bottom 16 bits of
103 * bi_phys_segments, and a count of processed stripes in the upper 16 bits
105 static inline int raid5_bi_phys_segments(struct bio *bio)
107 return bio->bi_phys_segments & 0xffff;
110 static inline int raid5_bi_hw_segments(struct bio *bio)
112 return (bio->bi_phys_segments >> 16) & 0xffff;
115 static inline int raid5_dec_bi_phys_segments(struct bio *bio)
117 --bio->bi_phys_segments;
118 return raid5_bi_phys_segments(bio);
121 static inline int raid5_dec_bi_hw_segments(struct bio *bio)
123 unsigned short val = raid5_bi_hw_segments(bio);
125 --val;
126 bio->bi_phys_segments = (val << 16) | raid5_bi_phys_segments(bio);
127 return val;
130 static inline void raid5_set_bi_hw_segments(struct bio *bio, unsigned int cnt)
132 bio->bi_phys_segments = raid5_bi_phys_segments(bio) | (cnt << 16);
135 /* Find first data disk in a raid6 stripe */
136 static inline int raid6_d0(struct stripe_head *sh)
138 if (sh->ddf_layout)
139 /* ddf always start from first device */
140 return 0;
141 /* md starts just after Q block */
142 if (sh->qd_idx == sh->disks - 1)
143 return 0;
144 else
145 return sh->qd_idx + 1;
147 static inline int raid6_next_disk(int disk, int raid_disks)
149 disk++;
150 return (disk < raid_disks) ? disk : 0;
153 /* When walking through the disks in a raid5, starting at raid6_d0,
154 * We need to map each disk to a 'slot', where the data disks are slot
155 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
156 * is raid_disks-1. This help does that mapping.
158 static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
159 int *count, int syndrome_disks)
161 int slot = *count;
163 if (sh->ddf_layout)
164 (*count)++;
165 if (idx == sh->pd_idx)
166 return syndrome_disks;
167 if (idx == sh->qd_idx)
168 return syndrome_disks + 1;
169 if (!sh->ddf_layout)
170 (*count)++;
171 return slot;
174 static void return_io(struct bio *return_bi)
176 struct bio *bi = return_bi;
177 while (bi) {
179 return_bi = bi->bi_next;
180 bi->bi_next = NULL;
181 bi->bi_size = 0;
182 bio_endio(bi, 0);
183 bi = return_bi;
187 static void print_raid5_conf (raid5_conf_t *conf);
189 static int stripe_operations_active(struct stripe_head *sh)
191 return sh->check_state || sh->reconstruct_state ||
192 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
193 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
196 static void __release_stripe(raid5_conf_t *conf, struct stripe_head *sh)
198 if (atomic_dec_and_test(&sh->count)) {
199 BUG_ON(!list_empty(&sh->lru));
200 BUG_ON(atomic_read(&conf->active_stripes)==0);
201 if (test_bit(STRIPE_HANDLE, &sh->state)) {
202 if (test_bit(STRIPE_DELAYED, &sh->state))
203 list_add_tail(&sh->lru, &conf->delayed_list);
204 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
205 sh->bm_seq - conf->seq_write > 0)
206 list_add_tail(&sh->lru, &conf->bitmap_list);
207 else {
208 clear_bit(STRIPE_BIT_DELAY, &sh->state);
209 list_add_tail(&sh->lru, &conf->handle_list);
211 md_wakeup_thread(conf->mddev->thread);
212 } else {
213 BUG_ON(stripe_operations_active(sh));
214 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
215 atomic_dec(&conf->preread_active_stripes);
216 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD)
217 md_wakeup_thread(conf->mddev->thread);
219 atomic_dec(&conf->active_stripes);
220 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
221 list_add_tail(&sh->lru, &conf->inactive_list);
222 wake_up(&conf->wait_for_stripe);
223 if (conf->retry_read_aligned)
224 md_wakeup_thread(conf->mddev->thread);
230 static void release_stripe(struct stripe_head *sh)
232 raid5_conf_t *conf = sh->raid_conf;
233 unsigned long flags;
235 spin_lock_irqsave(&conf->device_lock, flags);
236 __release_stripe(conf, sh);
237 spin_unlock_irqrestore(&conf->device_lock, flags);
240 static inline void remove_hash(struct stripe_head *sh)
242 pr_debug("remove_hash(), stripe %llu\n",
243 (unsigned long long)sh->sector);
245 hlist_del_init(&sh->hash);
248 static inline void insert_hash(raid5_conf_t *conf, struct stripe_head *sh)
250 struct hlist_head *hp = stripe_hash(conf, sh->sector);
252 pr_debug("insert_hash(), stripe %llu\n",
253 (unsigned long long)sh->sector);
255 CHECK_DEVLOCK();
256 hlist_add_head(&sh->hash, hp);
260 /* find an idle stripe, make sure it is unhashed, and return it. */
261 static struct stripe_head *get_free_stripe(raid5_conf_t *conf)
263 struct stripe_head *sh = NULL;
264 struct list_head *first;
266 CHECK_DEVLOCK();
267 if (list_empty(&conf->inactive_list))
268 goto out;
269 first = conf->inactive_list.next;
270 sh = list_entry(first, struct stripe_head, lru);
271 list_del_init(first);
272 remove_hash(sh);
273 atomic_inc(&conf->active_stripes);
274 out:
275 return sh;
278 static void shrink_buffers(struct stripe_head *sh)
280 struct page *p;
281 int i;
282 int num = sh->raid_conf->pool_size;
284 for (i = 0; i < num ; i++) {
285 p = sh->dev[i].page;
286 if (!p)
287 continue;
288 sh->dev[i].page = NULL;
289 put_page(p);
293 static int grow_buffers(struct stripe_head *sh)
295 int i;
296 int num = sh->raid_conf->pool_size;
298 for (i = 0; i < num; i++) {
299 struct page *page;
301 if (!(page = alloc_page(GFP_KERNEL))) {
302 return 1;
304 sh->dev[i].page = page;
306 return 0;
309 static void raid5_build_block(struct stripe_head *sh, int i, int previous);
310 static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
311 struct stripe_head *sh);
313 static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
315 raid5_conf_t *conf = sh->raid_conf;
316 int i;
318 BUG_ON(atomic_read(&sh->count) != 0);
319 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
320 BUG_ON(stripe_operations_active(sh));
322 CHECK_DEVLOCK();
323 pr_debug("init_stripe called, stripe %llu\n",
324 (unsigned long long)sh->sector);
326 remove_hash(sh);
328 sh->generation = conf->generation - previous;
329 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
330 sh->sector = sector;
331 stripe_set_idx(sector, conf, previous, sh);
332 sh->state = 0;
335 for (i = sh->disks; i--; ) {
336 struct r5dev *dev = &sh->dev[i];
338 if (dev->toread || dev->read || dev->towrite || dev->written ||
339 test_bit(R5_LOCKED, &dev->flags)) {
340 printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n",
341 (unsigned long long)sh->sector, i, dev->toread,
342 dev->read, dev->towrite, dev->written,
343 test_bit(R5_LOCKED, &dev->flags));
344 WARN_ON(1);
346 dev->flags = 0;
347 raid5_build_block(sh, i, previous);
349 insert_hash(conf, sh);
352 static struct stripe_head *__find_stripe(raid5_conf_t *conf, sector_t sector,
353 short generation)
355 struct stripe_head *sh;
356 struct hlist_node *hn;
358 CHECK_DEVLOCK();
359 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
360 hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash)
361 if (sh->sector == sector && sh->generation == generation)
362 return sh;
363 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
364 return NULL;
368 * Need to check if array has failed when deciding whether to:
369 * - start an array
370 * - remove non-faulty devices
371 * - add a spare
372 * - allow a reshape
373 * This determination is simple when no reshape is happening.
374 * However if there is a reshape, we need to carefully check
375 * both the before and after sections.
376 * This is because some failed devices may only affect one
377 * of the two sections, and some non-in_sync devices may
378 * be insync in the section most affected by failed devices.
380 static int has_failed(raid5_conf_t *conf)
382 int degraded;
383 int i;
384 if (conf->mddev->reshape_position == MaxSector)
385 return conf->mddev->degraded > conf->max_degraded;
387 rcu_read_lock();
388 degraded = 0;
389 for (i = 0; i < conf->previous_raid_disks; i++) {
390 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
391 if (!rdev || test_bit(Faulty, &rdev->flags))
392 degraded++;
393 else if (test_bit(In_sync, &rdev->flags))
395 else
396 /* not in-sync or faulty.
397 * If the reshape increases the number of devices,
398 * this is being recovered by the reshape, so
399 * this 'previous' section is not in_sync.
400 * If the number of devices is being reduced however,
401 * the device can only be part of the array if
402 * we are reverting a reshape, so this section will
403 * be in-sync.
405 if (conf->raid_disks >= conf->previous_raid_disks)
406 degraded++;
408 rcu_read_unlock();
409 if (degraded > conf->max_degraded)
410 return 1;
411 rcu_read_lock();
412 degraded = 0;
413 for (i = 0; i < conf->raid_disks; i++) {
414 mdk_rdev_t *rdev = rcu_dereference(conf->disks[i].rdev);
415 if (!rdev || test_bit(Faulty, &rdev->flags))
416 degraded++;
417 else if (test_bit(In_sync, &rdev->flags))
419 else
420 /* not in-sync or faulty.
421 * If reshape increases the number of devices, this
422 * section has already been recovered, else it
423 * almost certainly hasn't.
425 if (conf->raid_disks <= conf->previous_raid_disks)
426 degraded++;
428 rcu_read_unlock();
429 if (degraded > conf->max_degraded)
430 return 1;
431 return 0;
434 static struct stripe_head *
435 get_active_stripe(raid5_conf_t *conf, sector_t sector,
436 int previous, int noblock, int noquiesce)
438 struct stripe_head *sh;
440 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
442 spin_lock_irq(&conf->device_lock);
444 do {
445 wait_event_lock_irq(conf->wait_for_stripe,
446 conf->quiesce == 0 || noquiesce,
447 conf->device_lock, /* nothing */);
448 sh = __find_stripe(conf, sector, conf->generation - previous);
449 if (!sh) {
450 if (!conf->inactive_blocked)
451 sh = get_free_stripe(conf);
452 if (noblock && sh == NULL)
453 break;
454 if (!sh) {
455 conf->inactive_blocked = 1;
456 wait_event_lock_irq(conf->wait_for_stripe,
457 !list_empty(&conf->inactive_list) &&
458 (atomic_read(&conf->active_stripes)
459 < (conf->max_nr_stripes *3/4)
460 || !conf->inactive_blocked),
461 conf->device_lock,
463 conf->inactive_blocked = 0;
464 } else
465 init_stripe(sh, sector, previous);
466 } else {
467 if (atomic_read(&sh->count)) {
468 BUG_ON(!list_empty(&sh->lru)
469 && !test_bit(STRIPE_EXPANDING, &sh->state));
470 } else {
471 if (!test_bit(STRIPE_HANDLE, &sh->state))
472 atomic_inc(&conf->active_stripes);
473 if (list_empty(&sh->lru) &&
474 !test_bit(STRIPE_EXPANDING, &sh->state))
475 BUG();
476 list_del_init(&sh->lru);
479 } while (sh == NULL);
481 if (sh)
482 atomic_inc(&sh->count);
484 spin_unlock_irq(&conf->device_lock);
485 return sh;
488 static void
489 raid5_end_read_request(struct bio *bi, int error);
490 static void
491 raid5_end_write_request(struct bio *bi, int error);
493 static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
495 raid5_conf_t *conf = sh->raid_conf;
496 int i, disks = sh->disks;
498 might_sleep();
500 for (i = disks; i--; ) {
501 int rw;
502 struct bio *bi;
503 mdk_rdev_t *rdev;
504 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
505 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
506 rw = WRITE_FUA;
507 else
508 rw = WRITE;
509 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
510 rw = READ;
511 else
512 continue;
514 bi = &sh->dev[i].req;
516 bi->bi_rw = rw;
517 if (rw & WRITE)
518 bi->bi_end_io = raid5_end_write_request;
519 else
520 bi->bi_end_io = raid5_end_read_request;
522 rcu_read_lock();
523 rdev = rcu_dereference(conf->disks[i].rdev);
524 if (rdev && test_bit(Faulty, &rdev->flags))
525 rdev = NULL;
526 if (rdev)
527 atomic_inc(&rdev->nr_pending);
528 rcu_read_unlock();
530 /* We have already checked bad blocks for reads. Now
531 * need to check for writes.
533 while ((rw & WRITE) && rdev &&
534 test_bit(WriteErrorSeen, &rdev->flags)) {
535 sector_t first_bad;
536 int bad_sectors;
537 int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
538 &first_bad, &bad_sectors);
539 if (!bad)
540 break;
542 if (bad < 0) {
543 set_bit(BlockedBadBlocks, &rdev->flags);
544 if (!conf->mddev->external &&
545 conf->mddev->flags) {
546 /* It is very unlikely, but we might
547 * still need to write out the
548 * bad block log - better give it
549 * a chance*/
550 md_check_recovery(conf->mddev);
552 md_wait_for_blocked_rdev(rdev, conf->mddev);
553 } else {
554 /* Acknowledged bad block - skip the write */
555 rdev_dec_pending(rdev, conf->mddev);
556 rdev = NULL;
560 if (rdev) {
561 if (s->syncing || s->expanding || s->expanded)
562 md_sync_acct(rdev->bdev, STRIPE_SECTORS);
564 set_bit(STRIPE_IO_STARTED, &sh->state);
566 bi->bi_bdev = rdev->bdev;
567 pr_debug("%s: for %llu schedule op %ld on disc %d\n",
568 __func__, (unsigned long long)sh->sector,
569 bi->bi_rw, i);
570 atomic_inc(&sh->count);
571 bi->bi_sector = sh->sector + rdev->data_offset;
572 bi->bi_flags = 1 << BIO_UPTODATE;
573 bi->bi_vcnt = 1;
574 bi->bi_max_vecs = 1;
575 bi->bi_idx = 0;
576 bi->bi_io_vec = &sh->dev[i].vec;
577 bi->bi_io_vec[0].bv_len = STRIPE_SIZE;
578 bi->bi_io_vec[0].bv_offset = 0;
579 bi->bi_size = STRIPE_SIZE;
580 bi->bi_next = NULL;
581 generic_make_request(bi);
582 } else {
583 if (rw & WRITE)
584 set_bit(STRIPE_DEGRADED, &sh->state);
585 pr_debug("skip op %ld on disc %d for sector %llu\n",
586 bi->bi_rw, i, (unsigned long long)sh->sector);
587 clear_bit(R5_LOCKED, &sh->dev[i].flags);
588 set_bit(STRIPE_HANDLE, &sh->state);
593 static struct dma_async_tx_descriptor *
594 async_copy_data(int frombio, struct bio *bio, struct page *page,
595 sector_t sector, struct dma_async_tx_descriptor *tx)
597 struct bio_vec *bvl;
598 struct page *bio_page;
599 int i;
600 int page_offset;
601 struct async_submit_ctl submit;
602 enum async_tx_flags flags = 0;
604 if (bio->bi_sector >= sector)
605 page_offset = (signed)(bio->bi_sector - sector) * 512;
606 else
607 page_offset = (signed)(sector - bio->bi_sector) * -512;
609 if (frombio)
610 flags |= ASYNC_TX_FENCE;
611 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
613 bio_for_each_segment(bvl, bio, i) {
614 int len = bvl->bv_len;
615 int clen;
616 int b_offset = 0;
618 if (page_offset < 0) {
619 b_offset = -page_offset;
620 page_offset += b_offset;
621 len -= b_offset;
624 if (len > 0 && page_offset + len > STRIPE_SIZE)
625 clen = STRIPE_SIZE - page_offset;
626 else
627 clen = len;
629 if (clen > 0) {
630 b_offset += bvl->bv_offset;
631 bio_page = bvl->bv_page;
632 if (frombio)
633 tx = async_memcpy(page, bio_page, page_offset,
634 b_offset, clen, &submit);
635 else
636 tx = async_memcpy(bio_page, page, b_offset,
637 page_offset, clen, &submit);
639 /* chain the operations */
640 submit.depend_tx = tx;
642 if (clen < len) /* hit end of page */
643 break;
644 page_offset += len;
647 return tx;
650 static void ops_complete_biofill(void *stripe_head_ref)
652 struct stripe_head *sh = stripe_head_ref;
653 struct bio *return_bi = NULL;
654 raid5_conf_t *conf = sh->raid_conf;
655 int i;
657 pr_debug("%s: stripe %llu\n", __func__,
658 (unsigned long long)sh->sector);
660 /* clear completed biofills */
661 spin_lock_irq(&conf->device_lock);
662 for (i = sh->disks; i--; ) {
663 struct r5dev *dev = &sh->dev[i];
665 /* acknowledge completion of a biofill operation */
666 /* and check if we need to reply to a read request,
667 * new R5_Wantfill requests are held off until
668 * !STRIPE_BIOFILL_RUN
670 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
671 struct bio *rbi, *rbi2;
673 BUG_ON(!dev->read);
674 rbi = dev->read;
675 dev->read = NULL;
676 while (rbi && rbi->bi_sector <
677 dev->sector + STRIPE_SECTORS) {
678 rbi2 = r5_next_bio(rbi, dev->sector);
679 if (!raid5_dec_bi_phys_segments(rbi)) {
680 rbi->bi_next = return_bi;
681 return_bi = rbi;
683 rbi = rbi2;
687 spin_unlock_irq(&conf->device_lock);
688 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
690 return_io(return_bi);
692 set_bit(STRIPE_HANDLE, &sh->state);
693 release_stripe(sh);
696 static void ops_run_biofill(struct stripe_head *sh)
698 struct dma_async_tx_descriptor *tx = NULL;
699 raid5_conf_t *conf = sh->raid_conf;
700 struct async_submit_ctl submit;
701 int i;
703 pr_debug("%s: stripe %llu\n", __func__,
704 (unsigned long long)sh->sector);
706 for (i = sh->disks; i--; ) {
707 struct r5dev *dev = &sh->dev[i];
708 if (test_bit(R5_Wantfill, &dev->flags)) {
709 struct bio *rbi;
710 spin_lock_irq(&conf->device_lock);
711 dev->read = rbi = dev->toread;
712 dev->toread = NULL;
713 spin_unlock_irq(&conf->device_lock);
714 while (rbi && rbi->bi_sector <
715 dev->sector + STRIPE_SECTORS) {
716 tx = async_copy_data(0, rbi, dev->page,
717 dev->sector, tx);
718 rbi = r5_next_bio(rbi, dev->sector);
723 atomic_inc(&sh->count);
724 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
725 async_trigger_callback(&submit);
728 static void mark_target_uptodate(struct stripe_head *sh, int target)
730 struct r5dev *tgt;
732 if (target < 0)
733 return;
735 tgt = &sh->dev[target];
736 set_bit(R5_UPTODATE, &tgt->flags);
737 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
738 clear_bit(R5_Wantcompute, &tgt->flags);
741 static void ops_complete_compute(void *stripe_head_ref)
743 struct stripe_head *sh = stripe_head_ref;
745 pr_debug("%s: stripe %llu\n", __func__,
746 (unsigned long long)sh->sector);
748 /* mark the computed target(s) as uptodate */
749 mark_target_uptodate(sh, sh->ops.target);
750 mark_target_uptodate(sh, sh->ops.target2);
752 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
753 if (sh->check_state == check_state_compute_run)
754 sh->check_state = check_state_compute_result;
755 set_bit(STRIPE_HANDLE, &sh->state);
756 release_stripe(sh);
759 /* return a pointer to the address conversion region of the scribble buffer */
760 static addr_conv_t *to_addr_conv(struct stripe_head *sh,
761 struct raid5_percpu *percpu)
763 return percpu->scribble + sizeof(struct page *) * (sh->disks + 2);
766 static struct dma_async_tx_descriptor *
767 ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
769 int disks = sh->disks;
770 struct page **xor_srcs = percpu->scribble;
771 int target = sh->ops.target;
772 struct r5dev *tgt = &sh->dev[target];
773 struct page *xor_dest = tgt->page;
774 int count = 0;
775 struct dma_async_tx_descriptor *tx;
776 struct async_submit_ctl submit;
777 int i;
779 pr_debug("%s: stripe %llu block: %d\n",
780 __func__, (unsigned long long)sh->sector, target);
781 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
783 for (i = disks; i--; )
784 if (i != target)
785 xor_srcs[count++] = sh->dev[i].page;
787 atomic_inc(&sh->count);
789 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
790 ops_complete_compute, sh, to_addr_conv(sh, percpu));
791 if (unlikely(count == 1))
792 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
793 else
794 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
796 return tx;
799 /* set_syndrome_sources - populate source buffers for gen_syndrome
800 * @srcs - (struct page *) array of size sh->disks
801 * @sh - stripe_head to parse
803 * Populates srcs in proper layout order for the stripe and returns the
804 * 'count' of sources to be used in a call to async_gen_syndrome. The P
805 * destination buffer is recorded in srcs[count] and the Q destination
806 * is recorded in srcs[count+1]].
808 static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh)
810 int disks = sh->disks;
811 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
812 int d0_idx = raid6_d0(sh);
813 int count;
814 int i;
816 for (i = 0; i < disks; i++)
817 srcs[i] = NULL;
819 count = 0;
820 i = d0_idx;
821 do {
822 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
824 srcs[slot] = sh->dev[i].page;
825 i = raid6_next_disk(i, disks);
826 } while (i != d0_idx);
828 return syndrome_disks;
831 static struct dma_async_tx_descriptor *
832 ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
834 int disks = sh->disks;
835 struct page **blocks = percpu->scribble;
836 int target;
837 int qd_idx = sh->qd_idx;
838 struct dma_async_tx_descriptor *tx;
839 struct async_submit_ctl submit;
840 struct r5dev *tgt;
841 struct page *dest;
842 int i;
843 int count;
845 if (sh->ops.target < 0)
846 target = sh->ops.target2;
847 else if (sh->ops.target2 < 0)
848 target = sh->ops.target;
849 else
850 /* we should only have one valid target */
851 BUG();
852 BUG_ON(target < 0);
853 pr_debug("%s: stripe %llu block: %d\n",
854 __func__, (unsigned long long)sh->sector, target);
856 tgt = &sh->dev[target];
857 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
858 dest = tgt->page;
860 atomic_inc(&sh->count);
862 if (target == qd_idx) {
863 count = set_syndrome_sources(blocks, sh);
864 blocks[count] = NULL; /* regenerating p is not necessary */
865 BUG_ON(blocks[count+1] != dest); /* q should already be set */
866 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
867 ops_complete_compute, sh,
868 to_addr_conv(sh, percpu));
869 tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
870 } else {
871 /* Compute any data- or p-drive using XOR */
872 count = 0;
873 for (i = disks; i-- ; ) {
874 if (i == target || i == qd_idx)
875 continue;
876 blocks[count++] = sh->dev[i].page;
879 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
880 NULL, ops_complete_compute, sh,
881 to_addr_conv(sh, percpu));
882 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit);
885 return tx;
888 static struct dma_async_tx_descriptor *
889 ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
891 int i, count, disks = sh->disks;
892 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
893 int d0_idx = raid6_d0(sh);
894 int faila = -1, failb = -1;
895 int target = sh->ops.target;
896 int target2 = sh->ops.target2;
897 struct r5dev *tgt = &sh->dev[target];
898 struct r5dev *tgt2 = &sh->dev[target2];
899 struct dma_async_tx_descriptor *tx;
900 struct page **blocks = percpu->scribble;
901 struct async_submit_ctl submit;
903 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
904 __func__, (unsigned long long)sh->sector, target, target2);
905 BUG_ON(target < 0 || target2 < 0);
906 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
907 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
909 /* we need to open-code set_syndrome_sources to handle the
910 * slot number conversion for 'faila' and 'failb'
912 for (i = 0; i < disks ; i++)
913 blocks[i] = NULL;
914 count = 0;
915 i = d0_idx;
916 do {
917 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
919 blocks[slot] = sh->dev[i].page;
921 if (i == target)
922 faila = slot;
923 if (i == target2)
924 failb = slot;
925 i = raid6_next_disk(i, disks);
926 } while (i != d0_idx);
928 BUG_ON(faila == failb);
929 if (failb < faila)
930 swap(faila, failb);
931 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
932 __func__, (unsigned long long)sh->sector, faila, failb);
934 atomic_inc(&sh->count);
936 if (failb == syndrome_disks+1) {
937 /* Q disk is one of the missing disks */
938 if (faila == syndrome_disks) {
939 /* Missing P+Q, just recompute */
940 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
941 ops_complete_compute, sh,
942 to_addr_conv(sh, percpu));
943 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
944 STRIPE_SIZE, &submit);
945 } else {
946 struct page *dest;
947 int data_target;
948 int qd_idx = sh->qd_idx;
950 /* Missing D+Q: recompute D from P, then recompute Q */
951 if (target == qd_idx)
952 data_target = target2;
953 else
954 data_target = target;
956 count = 0;
957 for (i = disks; i-- ; ) {
958 if (i == data_target || i == qd_idx)
959 continue;
960 blocks[count++] = sh->dev[i].page;
962 dest = sh->dev[data_target].page;
963 init_async_submit(&submit,
964 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
965 NULL, NULL, NULL,
966 to_addr_conv(sh, percpu));
967 tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE,
968 &submit);
970 count = set_syndrome_sources(blocks, sh);
971 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
972 ops_complete_compute, sh,
973 to_addr_conv(sh, percpu));
974 return async_gen_syndrome(blocks, 0, count+2,
975 STRIPE_SIZE, &submit);
977 } else {
978 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
979 ops_complete_compute, sh,
980 to_addr_conv(sh, percpu));
981 if (failb == syndrome_disks) {
982 /* We're missing D+P. */
983 return async_raid6_datap_recov(syndrome_disks+2,
984 STRIPE_SIZE, faila,
985 blocks, &submit);
986 } else {
987 /* We're missing D+D. */
988 return async_raid6_2data_recov(syndrome_disks+2,
989 STRIPE_SIZE, faila, failb,
990 blocks, &submit);
996 static void ops_complete_prexor(void *stripe_head_ref)
998 struct stripe_head *sh = stripe_head_ref;
1000 pr_debug("%s: stripe %llu\n", __func__,
1001 (unsigned long long)sh->sector);
1004 static struct dma_async_tx_descriptor *
1005 ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu,
1006 struct dma_async_tx_descriptor *tx)
1008 int disks = sh->disks;
1009 struct page **xor_srcs = percpu->scribble;
1010 int count = 0, pd_idx = sh->pd_idx, i;
1011 struct async_submit_ctl submit;
1013 /* existing parity data subtracted */
1014 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1016 pr_debug("%s: stripe %llu\n", __func__,
1017 (unsigned long long)sh->sector);
1019 for (i = disks; i--; ) {
1020 struct r5dev *dev = &sh->dev[i];
1021 /* Only process blocks that are known to be uptodate */
1022 if (test_bit(R5_Wantdrain, &dev->flags))
1023 xor_srcs[count++] = dev->page;
1026 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
1027 ops_complete_prexor, sh, to_addr_conv(sh, percpu));
1028 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1030 return tx;
1033 static struct dma_async_tx_descriptor *
1034 ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
1036 int disks = sh->disks;
1037 int i;
1039 pr_debug("%s: stripe %llu\n", __func__,
1040 (unsigned long long)sh->sector);
1042 for (i = disks; i--; ) {
1043 struct r5dev *dev = &sh->dev[i];
1044 struct bio *chosen;
1046 if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) {
1047 struct bio *wbi;
1049 spin_lock_irq(&sh->raid_conf->device_lock);
1050 chosen = dev->towrite;
1051 dev->towrite = NULL;
1052 BUG_ON(dev->written);
1053 wbi = dev->written = chosen;
1054 spin_unlock_irq(&sh->raid_conf->device_lock);
1056 while (wbi && wbi->bi_sector <
1057 dev->sector + STRIPE_SECTORS) {
1058 if (wbi->bi_rw & REQ_FUA)
1059 set_bit(R5_WantFUA, &dev->flags);
1060 tx = async_copy_data(1, wbi, dev->page,
1061 dev->sector, tx);
1062 wbi = r5_next_bio(wbi, dev->sector);
1067 return tx;
1070 static void ops_complete_reconstruct(void *stripe_head_ref)
1072 struct stripe_head *sh = stripe_head_ref;
1073 int disks = sh->disks;
1074 int pd_idx = sh->pd_idx;
1075 int qd_idx = sh->qd_idx;
1076 int i;
1077 bool fua = false;
1079 pr_debug("%s: stripe %llu\n", __func__,
1080 (unsigned long long)sh->sector);
1082 for (i = disks; i--; )
1083 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
1085 for (i = disks; i--; ) {
1086 struct r5dev *dev = &sh->dev[i];
1088 if (dev->written || i == pd_idx || i == qd_idx) {
1089 set_bit(R5_UPTODATE, &dev->flags);
1090 if (fua)
1091 set_bit(R5_WantFUA, &dev->flags);
1095 if (sh->reconstruct_state == reconstruct_state_drain_run)
1096 sh->reconstruct_state = reconstruct_state_drain_result;
1097 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1098 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1099 else {
1100 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1101 sh->reconstruct_state = reconstruct_state_result;
1104 set_bit(STRIPE_HANDLE, &sh->state);
1105 release_stripe(sh);
1108 static void
1109 ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1110 struct dma_async_tx_descriptor *tx)
1112 int disks = sh->disks;
1113 struct page **xor_srcs = percpu->scribble;
1114 struct async_submit_ctl submit;
1115 int count = 0, pd_idx = sh->pd_idx, i;
1116 struct page *xor_dest;
1117 int prexor = 0;
1118 unsigned long flags;
1120 pr_debug("%s: stripe %llu\n", __func__,
1121 (unsigned long long)sh->sector);
1123 /* check if prexor is active which means only process blocks
1124 * that are part of a read-modify-write (written)
1126 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1127 prexor = 1;
1128 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1129 for (i = disks; i--; ) {
1130 struct r5dev *dev = &sh->dev[i];
1131 if (dev->written)
1132 xor_srcs[count++] = dev->page;
1134 } else {
1135 xor_dest = sh->dev[pd_idx].page;
1136 for (i = disks; i--; ) {
1137 struct r5dev *dev = &sh->dev[i];
1138 if (i != pd_idx)
1139 xor_srcs[count++] = dev->page;
1143 /* 1/ if we prexor'd then the dest is reused as a source
1144 * 2/ if we did not prexor then we are redoing the parity
1145 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1146 * for the synchronous xor case
1148 flags = ASYNC_TX_ACK |
1149 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
1151 atomic_inc(&sh->count);
1153 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh,
1154 to_addr_conv(sh, percpu));
1155 if (unlikely(count == 1))
1156 tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit);
1157 else
1158 tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit);
1161 static void
1162 ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1163 struct dma_async_tx_descriptor *tx)
1165 struct async_submit_ctl submit;
1166 struct page **blocks = percpu->scribble;
1167 int count;
1169 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1171 count = set_syndrome_sources(blocks, sh);
1173 atomic_inc(&sh->count);
1175 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct,
1176 sh, to_addr_conv(sh, percpu));
1177 async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit);
1180 static void ops_complete_check(void *stripe_head_ref)
1182 struct stripe_head *sh = stripe_head_ref;
1184 pr_debug("%s: stripe %llu\n", __func__,
1185 (unsigned long long)sh->sector);
1187 sh->check_state = check_state_check_result;
1188 set_bit(STRIPE_HANDLE, &sh->state);
1189 release_stripe(sh);
1192 static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
1194 int disks = sh->disks;
1195 int pd_idx = sh->pd_idx;
1196 int qd_idx = sh->qd_idx;
1197 struct page *xor_dest;
1198 struct page **xor_srcs = percpu->scribble;
1199 struct dma_async_tx_descriptor *tx;
1200 struct async_submit_ctl submit;
1201 int count;
1202 int i;
1204 pr_debug("%s: stripe %llu\n", __func__,
1205 (unsigned long long)sh->sector);
1207 count = 0;
1208 xor_dest = sh->dev[pd_idx].page;
1209 xor_srcs[count++] = xor_dest;
1210 for (i = disks; i--; ) {
1211 if (i == pd_idx || i == qd_idx)
1212 continue;
1213 xor_srcs[count++] = sh->dev[i].page;
1216 init_async_submit(&submit, 0, NULL, NULL, NULL,
1217 to_addr_conv(sh, percpu));
1218 tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE,
1219 &sh->ops.zero_sum_result, &submit);
1221 atomic_inc(&sh->count);
1222 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
1223 tx = async_trigger_callback(&submit);
1226 static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
1228 struct page **srcs = percpu->scribble;
1229 struct async_submit_ctl submit;
1230 int count;
1232 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
1233 (unsigned long long)sh->sector, checkp);
1235 count = set_syndrome_sources(srcs, sh);
1236 if (!checkp)
1237 srcs[count] = NULL;
1239 atomic_inc(&sh->count);
1240 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
1241 sh, to_addr_conv(sh, percpu));
1242 async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE,
1243 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
1246 static void __raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1248 int overlap_clear = 0, i, disks = sh->disks;
1249 struct dma_async_tx_descriptor *tx = NULL;
1250 raid5_conf_t *conf = sh->raid_conf;
1251 int level = conf->level;
1252 struct raid5_percpu *percpu;
1253 unsigned long cpu;
1255 cpu = get_cpu();
1256 percpu = per_cpu_ptr(conf->percpu, cpu);
1257 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
1258 ops_run_biofill(sh);
1259 overlap_clear++;
1262 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
1263 if (level < 6)
1264 tx = ops_run_compute5(sh, percpu);
1265 else {
1266 if (sh->ops.target2 < 0 || sh->ops.target < 0)
1267 tx = ops_run_compute6_1(sh, percpu);
1268 else
1269 tx = ops_run_compute6_2(sh, percpu);
1271 /* terminate the chain if reconstruct is not set to be run */
1272 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
1273 async_tx_ack(tx);
1276 if (test_bit(STRIPE_OP_PREXOR, &ops_request))
1277 tx = ops_run_prexor(sh, percpu, tx);
1279 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
1280 tx = ops_run_biodrain(sh, tx);
1281 overlap_clear++;
1284 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
1285 if (level < 6)
1286 ops_run_reconstruct5(sh, percpu, tx);
1287 else
1288 ops_run_reconstruct6(sh, percpu, tx);
1291 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
1292 if (sh->check_state == check_state_run)
1293 ops_run_check_p(sh, percpu);
1294 else if (sh->check_state == check_state_run_q)
1295 ops_run_check_pq(sh, percpu, 0);
1296 else if (sh->check_state == check_state_run_pq)
1297 ops_run_check_pq(sh, percpu, 1);
1298 else
1299 BUG();
1302 if (overlap_clear)
1303 for (i = disks; i--; ) {
1304 struct r5dev *dev = &sh->dev[i];
1305 if (test_and_clear_bit(R5_Overlap, &dev->flags))
1306 wake_up(&sh->raid_conf->wait_for_overlap);
1308 put_cpu();
1311 #ifdef CONFIG_MULTICORE_RAID456
1312 static void async_run_ops(void *param, async_cookie_t cookie)
1314 struct stripe_head *sh = param;
1315 unsigned long ops_request = sh->ops.request;
1317 clear_bit_unlock(STRIPE_OPS_REQ_PENDING, &sh->state);
1318 wake_up(&sh->ops.wait_for_ops);
1320 __raid_run_ops(sh, ops_request);
1321 release_stripe(sh);
1324 static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
1326 /* since handle_stripe can be called outside of raid5d context
1327 * we need to ensure sh->ops.request is de-staged before another
1328 * request arrives
1330 wait_event(sh->ops.wait_for_ops,
1331 !test_and_set_bit_lock(STRIPE_OPS_REQ_PENDING, &sh->state));
1332 sh->ops.request = ops_request;
1334 atomic_inc(&sh->count);
1335 async_schedule(async_run_ops, sh);
1337 #else
1338 #define raid_run_ops __raid_run_ops
1339 #endif
1341 static int grow_one_stripe(raid5_conf_t *conf)
1343 struct stripe_head *sh;
1344 sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL);
1345 if (!sh)
1346 return 0;
1348 sh->raid_conf = conf;
1349 #ifdef CONFIG_MULTICORE_RAID456
1350 init_waitqueue_head(&sh->ops.wait_for_ops);
1351 #endif
1353 if (grow_buffers(sh)) {
1354 shrink_buffers(sh);
1355 kmem_cache_free(conf->slab_cache, sh);
1356 return 0;
1358 /* we just created an active stripe so... */
1359 atomic_set(&sh->count, 1);
1360 atomic_inc(&conf->active_stripes);
1361 INIT_LIST_HEAD(&sh->lru);
1362 release_stripe(sh);
1363 return 1;
1366 static int grow_stripes(raid5_conf_t *conf, int num)
1368 struct kmem_cache *sc;
1369 int devs = max(conf->raid_disks, conf->previous_raid_disks);
1371 if (conf->mddev->gendisk)
1372 sprintf(conf->cache_name[0],
1373 "raid%d-%s", conf->level, mdname(conf->mddev));
1374 else
1375 sprintf(conf->cache_name[0],
1376 "raid%d-%p", conf->level, conf->mddev);
1377 sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]);
1379 conf->active_name = 0;
1380 sc = kmem_cache_create(conf->cache_name[conf->active_name],
1381 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
1382 0, 0, NULL);
1383 if (!sc)
1384 return 1;
1385 conf->slab_cache = sc;
1386 conf->pool_size = devs;
1387 while (num--)
1388 if (!grow_one_stripe(conf))
1389 return 1;
1390 return 0;
1394 * scribble_len - return the required size of the scribble region
1395 * @num - total number of disks in the array
1397 * The size must be enough to contain:
1398 * 1/ a struct page pointer for each device in the array +2
1399 * 2/ room to convert each entry in (1) to its corresponding dma
1400 * (dma_map_page()) or page (page_address()) address.
1402 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
1403 * calculate over all devices (not just the data blocks), using zeros in place
1404 * of the P and Q blocks.
1406 static size_t scribble_len(int num)
1408 size_t len;
1410 len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2);
1412 return len;
1415 static int resize_stripes(raid5_conf_t *conf, int newsize)
1417 /* Make all the stripes able to hold 'newsize' devices.
1418 * New slots in each stripe get 'page' set to a new page.
1420 * This happens in stages:
1421 * 1/ create a new kmem_cache and allocate the required number of
1422 * stripe_heads.
1423 * 2/ gather all the old stripe_heads and tranfer the pages across
1424 * to the new stripe_heads. This will have the side effect of
1425 * freezing the array as once all stripe_heads have been collected,
1426 * no IO will be possible. Old stripe heads are freed once their
1427 * pages have been transferred over, and the old kmem_cache is
1428 * freed when all stripes are done.
1429 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
1430 * we simple return a failre status - no need to clean anything up.
1431 * 4/ allocate new pages for the new slots in the new stripe_heads.
1432 * If this fails, we don't bother trying the shrink the
1433 * stripe_heads down again, we just leave them as they are.
1434 * As each stripe_head is processed the new one is released into
1435 * active service.
1437 * Once step2 is started, we cannot afford to wait for a write,
1438 * so we use GFP_NOIO allocations.
1440 struct stripe_head *osh, *nsh;
1441 LIST_HEAD(newstripes);
1442 struct disk_info *ndisks;
1443 unsigned long cpu;
1444 int err;
1445 struct kmem_cache *sc;
1446 int i;
1448 if (newsize <= conf->pool_size)
1449 return 0; /* never bother to shrink */
1451 err = md_allow_write(conf->mddev);
1452 if (err)
1453 return err;
1455 /* Step 1 */
1456 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
1457 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
1458 0, 0, NULL);
1459 if (!sc)
1460 return -ENOMEM;
1462 for (i = conf->max_nr_stripes; i; i--) {
1463 nsh = kmem_cache_zalloc(sc, GFP_KERNEL);
1464 if (!nsh)
1465 break;
1467 nsh->raid_conf = conf;
1468 #ifdef CONFIG_MULTICORE_RAID456
1469 init_waitqueue_head(&nsh->ops.wait_for_ops);
1470 #endif
1472 list_add(&nsh->lru, &newstripes);
1474 if (i) {
1475 /* didn't get enough, give up */
1476 while (!list_empty(&newstripes)) {
1477 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1478 list_del(&nsh->lru);
1479 kmem_cache_free(sc, nsh);
1481 kmem_cache_destroy(sc);
1482 return -ENOMEM;
1484 /* Step 2 - Must use GFP_NOIO now.
1485 * OK, we have enough stripes, start collecting inactive
1486 * stripes and copying them over
1488 list_for_each_entry(nsh, &newstripes, lru) {
1489 spin_lock_irq(&conf->device_lock);
1490 wait_event_lock_irq(conf->wait_for_stripe,
1491 !list_empty(&conf->inactive_list),
1492 conf->device_lock,
1494 osh = get_free_stripe(conf);
1495 spin_unlock_irq(&conf->device_lock);
1496 atomic_set(&nsh->count, 1);
1497 for(i=0; i<conf->pool_size; i++)
1498 nsh->dev[i].page = osh->dev[i].page;
1499 for( ; i<newsize; i++)
1500 nsh->dev[i].page = NULL;
1501 kmem_cache_free(conf->slab_cache, osh);
1503 kmem_cache_destroy(conf->slab_cache);
1505 /* Step 3.
1506 * At this point, we are holding all the stripes so the array
1507 * is completely stalled, so now is a good time to resize
1508 * conf->disks and the scribble region
1510 ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO);
1511 if (ndisks) {
1512 for (i=0; i<conf->raid_disks; i++)
1513 ndisks[i] = conf->disks[i];
1514 kfree(conf->disks);
1515 conf->disks = ndisks;
1516 } else
1517 err = -ENOMEM;
1519 get_online_cpus();
1520 conf->scribble_len = scribble_len(newsize);
1521 for_each_present_cpu(cpu) {
1522 struct raid5_percpu *percpu;
1523 void *scribble;
1525 percpu = per_cpu_ptr(conf->percpu, cpu);
1526 scribble = kmalloc(conf->scribble_len, GFP_NOIO);
1528 if (scribble) {
1529 kfree(percpu->scribble);
1530 percpu->scribble = scribble;
1531 } else {
1532 err = -ENOMEM;
1533 break;
1536 put_online_cpus();
1538 /* Step 4, return new stripes to service */
1539 while(!list_empty(&newstripes)) {
1540 nsh = list_entry(newstripes.next, struct stripe_head, lru);
1541 list_del_init(&nsh->lru);
1543 for (i=conf->raid_disks; i < newsize; i++)
1544 if (nsh->dev[i].page == NULL) {
1545 struct page *p = alloc_page(GFP_NOIO);
1546 nsh->dev[i].page = p;
1547 if (!p)
1548 err = -ENOMEM;
1550 release_stripe(nsh);
1552 /* critical section pass, GFP_NOIO no longer needed */
1554 conf->slab_cache = sc;
1555 conf->active_name = 1-conf->active_name;
1556 conf->pool_size = newsize;
1557 return err;
1560 static int drop_one_stripe(raid5_conf_t *conf)
1562 struct stripe_head *sh;
1564 spin_lock_irq(&conf->device_lock);
1565 sh = get_free_stripe(conf);
1566 spin_unlock_irq(&conf->device_lock);
1567 if (!sh)
1568 return 0;
1569 BUG_ON(atomic_read(&sh->count));
1570 shrink_buffers(sh);
1571 kmem_cache_free(conf->slab_cache, sh);
1572 atomic_dec(&conf->active_stripes);
1573 return 1;
1576 static void shrink_stripes(raid5_conf_t *conf)
1578 while (drop_one_stripe(conf))
1581 if (conf->slab_cache)
1582 kmem_cache_destroy(conf->slab_cache);
1583 conf->slab_cache = NULL;
1586 static void raid5_end_read_request(struct bio * bi, int error)
1588 struct stripe_head *sh = bi->bi_private;
1589 raid5_conf_t *conf = sh->raid_conf;
1590 int disks = sh->disks, i;
1591 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1592 char b[BDEVNAME_SIZE];
1593 mdk_rdev_t *rdev;
1596 for (i=0 ; i<disks; i++)
1597 if (bi == &sh->dev[i].req)
1598 break;
1600 pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n",
1601 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1602 uptodate);
1603 if (i == disks) {
1604 BUG();
1605 return;
1608 if (uptodate) {
1609 set_bit(R5_UPTODATE, &sh->dev[i].flags);
1610 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
1611 rdev = conf->disks[i].rdev;
1612 printk_ratelimited(
1613 KERN_INFO
1614 "md/raid:%s: read error corrected"
1615 " (%lu sectors at %llu on %s)\n",
1616 mdname(conf->mddev), STRIPE_SECTORS,
1617 (unsigned long long)(sh->sector
1618 + rdev->data_offset),
1619 bdevname(rdev->bdev, b));
1620 atomic_add(STRIPE_SECTORS, &rdev->corrected_errors);
1621 clear_bit(R5_ReadError, &sh->dev[i].flags);
1622 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1624 if (atomic_read(&conf->disks[i].rdev->read_errors))
1625 atomic_set(&conf->disks[i].rdev->read_errors, 0);
1626 } else {
1627 const char *bdn = bdevname(conf->disks[i].rdev->bdev, b);
1628 int retry = 0;
1629 rdev = conf->disks[i].rdev;
1631 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
1632 atomic_inc(&rdev->read_errors);
1633 if (conf->mddev->degraded >= conf->max_degraded)
1634 printk_ratelimited(
1635 KERN_WARNING
1636 "md/raid:%s: read error not correctable "
1637 "(sector %llu on %s).\n",
1638 mdname(conf->mddev),
1639 (unsigned long long)(sh->sector
1640 + rdev->data_offset),
1641 bdn);
1642 else if (test_bit(R5_ReWrite, &sh->dev[i].flags))
1643 /* Oh, no!!! */
1644 printk_ratelimited(
1645 KERN_WARNING
1646 "md/raid:%s: read error NOT corrected!! "
1647 "(sector %llu on %s).\n",
1648 mdname(conf->mddev),
1649 (unsigned long long)(sh->sector
1650 + rdev->data_offset),
1651 bdn);
1652 else if (atomic_read(&rdev->read_errors)
1653 > conf->max_nr_stripes)
1654 printk(KERN_WARNING
1655 "md/raid:%s: Too many read errors, failing device %s.\n",
1656 mdname(conf->mddev), bdn);
1657 else
1658 retry = 1;
1659 if (retry)
1660 set_bit(R5_ReadError, &sh->dev[i].flags);
1661 else {
1662 clear_bit(R5_ReadError, &sh->dev[i].flags);
1663 clear_bit(R5_ReWrite, &sh->dev[i].flags);
1664 md_error(conf->mddev, rdev);
1667 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1668 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1669 set_bit(STRIPE_HANDLE, &sh->state);
1670 release_stripe(sh);
1673 static void raid5_end_write_request(struct bio *bi, int error)
1675 struct stripe_head *sh = bi->bi_private;
1676 raid5_conf_t *conf = sh->raid_conf;
1677 int disks = sh->disks, i;
1678 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
1679 sector_t first_bad;
1680 int bad_sectors;
1682 for (i=0 ; i<disks; i++)
1683 if (bi == &sh->dev[i].req)
1684 break;
1686 pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n",
1687 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
1688 uptodate);
1689 if (i == disks) {
1690 BUG();
1691 return;
1694 if (!uptodate) {
1695 set_bit(WriteErrorSeen, &conf->disks[i].rdev->flags);
1696 set_bit(R5_WriteError, &sh->dev[i].flags);
1697 } else if (is_badblock(conf->disks[i].rdev, sh->sector, STRIPE_SECTORS,
1698 &first_bad, &bad_sectors))
1699 set_bit(R5_MadeGood, &sh->dev[i].flags);
1701 rdev_dec_pending(conf->disks[i].rdev, conf->mddev);
1703 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1704 set_bit(STRIPE_HANDLE, &sh->state);
1705 release_stripe(sh);
1709 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous);
1711 static void raid5_build_block(struct stripe_head *sh, int i, int previous)
1713 struct r5dev *dev = &sh->dev[i];
1715 bio_init(&dev->req);
1716 dev->req.bi_io_vec = &dev->vec;
1717 dev->req.bi_vcnt++;
1718 dev->req.bi_max_vecs++;
1719 dev->vec.bv_page = dev->page;
1720 dev->vec.bv_len = STRIPE_SIZE;
1721 dev->vec.bv_offset = 0;
1723 dev->req.bi_sector = sh->sector;
1724 dev->req.bi_private = sh;
1726 dev->flags = 0;
1727 dev->sector = compute_blocknr(sh, i, previous);
1730 static void error(mddev_t *mddev, mdk_rdev_t *rdev)
1732 char b[BDEVNAME_SIZE];
1733 raid5_conf_t *conf = mddev->private;
1734 pr_debug("raid456: error called\n");
1736 if (test_and_clear_bit(In_sync, &rdev->flags)) {
1737 unsigned long flags;
1738 spin_lock_irqsave(&conf->device_lock, flags);
1739 mddev->degraded++;
1740 spin_unlock_irqrestore(&conf->device_lock, flags);
1742 * if recovery was running, make sure it aborts.
1744 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
1746 set_bit(Blocked, &rdev->flags);
1747 set_bit(Faulty, &rdev->flags);
1748 set_bit(MD_CHANGE_DEVS, &mddev->flags);
1749 printk(KERN_ALERT
1750 "md/raid:%s: Disk failure on %s, disabling device.\n"
1751 "md/raid:%s: Operation continuing on %d devices.\n",
1752 mdname(mddev),
1753 bdevname(rdev->bdev, b),
1754 mdname(mddev),
1755 conf->raid_disks - mddev->degraded);
1759 * Input: a 'big' sector number,
1760 * Output: index of the data and parity disk, and the sector # in them.
1762 static sector_t raid5_compute_sector(raid5_conf_t *conf, sector_t r_sector,
1763 int previous, int *dd_idx,
1764 struct stripe_head *sh)
1766 sector_t stripe, stripe2;
1767 sector_t chunk_number;
1768 unsigned int chunk_offset;
1769 int pd_idx, qd_idx;
1770 int ddf_layout = 0;
1771 sector_t new_sector;
1772 int algorithm = previous ? conf->prev_algo
1773 : conf->algorithm;
1774 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1775 : conf->chunk_sectors;
1776 int raid_disks = previous ? conf->previous_raid_disks
1777 : conf->raid_disks;
1778 int data_disks = raid_disks - conf->max_degraded;
1780 /* First compute the information on this sector */
1783 * Compute the chunk number and the sector offset inside the chunk
1785 chunk_offset = sector_div(r_sector, sectors_per_chunk);
1786 chunk_number = r_sector;
1789 * Compute the stripe number
1791 stripe = chunk_number;
1792 *dd_idx = sector_div(stripe, data_disks);
1793 stripe2 = stripe;
1795 * Select the parity disk based on the user selected algorithm.
1797 pd_idx = qd_idx = -1;
1798 switch(conf->level) {
1799 case 4:
1800 pd_idx = data_disks;
1801 break;
1802 case 5:
1803 switch (algorithm) {
1804 case ALGORITHM_LEFT_ASYMMETRIC:
1805 pd_idx = data_disks - sector_div(stripe2, raid_disks);
1806 if (*dd_idx >= pd_idx)
1807 (*dd_idx)++;
1808 break;
1809 case ALGORITHM_RIGHT_ASYMMETRIC:
1810 pd_idx = sector_div(stripe2, raid_disks);
1811 if (*dd_idx >= pd_idx)
1812 (*dd_idx)++;
1813 break;
1814 case ALGORITHM_LEFT_SYMMETRIC:
1815 pd_idx = data_disks - sector_div(stripe2, raid_disks);
1816 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1817 break;
1818 case ALGORITHM_RIGHT_SYMMETRIC:
1819 pd_idx = sector_div(stripe2, raid_disks);
1820 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1821 break;
1822 case ALGORITHM_PARITY_0:
1823 pd_idx = 0;
1824 (*dd_idx)++;
1825 break;
1826 case ALGORITHM_PARITY_N:
1827 pd_idx = data_disks;
1828 break;
1829 default:
1830 BUG();
1832 break;
1833 case 6:
1835 switch (algorithm) {
1836 case ALGORITHM_LEFT_ASYMMETRIC:
1837 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
1838 qd_idx = pd_idx + 1;
1839 if (pd_idx == raid_disks-1) {
1840 (*dd_idx)++; /* Q D D D P */
1841 qd_idx = 0;
1842 } else if (*dd_idx >= pd_idx)
1843 (*dd_idx) += 2; /* D D P Q D */
1844 break;
1845 case ALGORITHM_RIGHT_ASYMMETRIC:
1846 pd_idx = sector_div(stripe2, raid_disks);
1847 qd_idx = pd_idx + 1;
1848 if (pd_idx == raid_disks-1) {
1849 (*dd_idx)++; /* Q D D D P */
1850 qd_idx = 0;
1851 } else if (*dd_idx >= pd_idx)
1852 (*dd_idx) += 2; /* D D P Q D */
1853 break;
1854 case ALGORITHM_LEFT_SYMMETRIC:
1855 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
1856 qd_idx = (pd_idx + 1) % raid_disks;
1857 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
1858 break;
1859 case ALGORITHM_RIGHT_SYMMETRIC:
1860 pd_idx = sector_div(stripe2, raid_disks);
1861 qd_idx = (pd_idx + 1) % raid_disks;
1862 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
1863 break;
1865 case ALGORITHM_PARITY_0:
1866 pd_idx = 0;
1867 qd_idx = 1;
1868 (*dd_idx) += 2;
1869 break;
1870 case ALGORITHM_PARITY_N:
1871 pd_idx = data_disks;
1872 qd_idx = data_disks + 1;
1873 break;
1875 case ALGORITHM_ROTATING_ZERO_RESTART:
1876 /* Exactly the same as RIGHT_ASYMMETRIC, but or
1877 * of blocks for computing Q is different.
1879 pd_idx = sector_div(stripe2, raid_disks);
1880 qd_idx = pd_idx + 1;
1881 if (pd_idx == raid_disks-1) {
1882 (*dd_idx)++; /* Q D D D P */
1883 qd_idx = 0;
1884 } else if (*dd_idx >= pd_idx)
1885 (*dd_idx) += 2; /* D D P Q D */
1886 ddf_layout = 1;
1887 break;
1889 case ALGORITHM_ROTATING_N_RESTART:
1890 /* Same a left_asymmetric, by first stripe is
1891 * D D D P Q rather than
1892 * Q D D D P
1894 stripe2 += 1;
1895 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
1896 qd_idx = pd_idx + 1;
1897 if (pd_idx == raid_disks-1) {
1898 (*dd_idx)++; /* Q D D D P */
1899 qd_idx = 0;
1900 } else if (*dd_idx >= pd_idx)
1901 (*dd_idx) += 2; /* D D P Q D */
1902 ddf_layout = 1;
1903 break;
1905 case ALGORITHM_ROTATING_N_CONTINUE:
1906 /* Same as left_symmetric but Q is before P */
1907 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
1908 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
1909 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
1910 ddf_layout = 1;
1911 break;
1913 case ALGORITHM_LEFT_ASYMMETRIC_6:
1914 /* RAID5 left_asymmetric, with Q on last device */
1915 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
1916 if (*dd_idx >= pd_idx)
1917 (*dd_idx)++;
1918 qd_idx = raid_disks - 1;
1919 break;
1921 case ALGORITHM_RIGHT_ASYMMETRIC_6:
1922 pd_idx = sector_div(stripe2, raid_disks-1);
1923 if (*dd_idx >= pd_idx)
1924 (*dd_idx)++;
1925 qd_idx = raid_disks - 1;
1926 break;
1928 case ALGORITHM_LEFT_SYMMETRIC_6:
1929 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
1930 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1931 qd_idx = raid_disks - 1;
1932 break;
1934 case ALGORITHM_RIGHT_SYMMETRIC_6:
1935 pd_idx = sector_div(stripe2, raid_disks-1);
1936 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
1937 qd_idx = raid_disks - 1;
1938 break;
1940 case ALGORITHM_PARITY_0_6:
1941 pd_idx = 0;
1942 (*dd_idx)++;
1943 qd_idx = raid_disks - 1;
1944 break;
1946 default:
1947 BUG();
1949 break;
1952 if (sh) {
1953 sh->pd_idx = pd_idx;
1954 sh->qd_idx = qd_idx;
1955 sh->ddf_layout = ddf_layout;
1958 * Finally, compute the new sector number
1960 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
1961 return new_sector;
1965 static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous)
1967 raid5_conf_t *conf = sh->raid_conf;
1968 int raid_disks = sh->disks;
1969 int data_disks = raid_disks - conf->max_degraded;
1970 sector_t new_sector = sh->sector, check;
1971 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
1972 : conf->chunk_sectors;
1973 int algorithm = previous ? conf->prev_algo
1974 : conf->algorithm;
1975 sector_t stripe;
1976 int chunk_offset;
1977 sector_t chunk_number;
1978 int dummy1, dd_idx = i;
1979 sector_t r_sector;
1980 struct stripe_head sh2;
1983 chunk_offset = sector_div(new_sector, sectors_per_chunk);
1984 stripe = new_sector;
1986 if (i == sh->pd_idx)
1987 return 0;
1988 switch(conf->level) {
1989 case 4: break;
1990 case 5:
1991 switch (algorithm) {
1992 case ALGORITHM_LEFT_ASYMMETRIC:
1993 case ALGORITHM_RIGHT_ASYMMETRIC:
1994 if (i > sh->pd_idx)
1995 i--;
1996 break;
1997 case ALGORITHM_LEFT_SYMMETRIC:
1998 case ALGORITHM_RIGHT_SYMMETRIC:
1999 if (i < sh->pd_idx)
2000 i += raid_disks;
2001 i -= (sh->pd_idx + 1);
2002 break;
2003 case ALGORITHM_PARITY_0:
2004 i -= 1;
2005 break;
2006 case ALGORITHM_PARITY_N:
2007 break;
2008 default:
2009 BUG();
2011 break;
2012 case 6:
2013 if (i == sh->qd_idx)
2014 return 0; /* It is the Q disk */
2015 switch (algorithm) {
2016 case ALGORITHM_LEFT_ASYMMETRIC:
2017 case ALGORITHM_RIGHT_ASYMMETRIC:
2018 case ALGORITHM_ROTATING_ZERO_RESTART:
2019 case ALGORITHM_ROTATING_N_RESTART:
2020 if (sh->pd_idx == raid_disks-1)
2021 i--; /* Q D D D P */
2022 else if (i > sh->pd_idx)
2023 i -= 2; /* D D P Q D */
2024 break;
2025 case ALGORITHM_LEFT_SYMMETRIC:
2026 case ALGORITHM_RIGHT_SYMMETRIC:
2027 if (sh->pd_idx == raid_disks-1)
2028 i--; /* Q D D D P */
2029 else {
2030 /* D D P Q D */
2031 if (i < sh->pd_idx)
2032 i += raid_disks;
2033 i -= (sh->pd_idx + 2);
2035 break;
2036 case ALGORITHM_PARITY_0:
2037 i -= 2;
2038 break;
2039 case ALGORITHM_PARITY_N:
2040 break;
2041 case ALGORITHM_ROTATING_N_CONTINUE:
2042 /* Like left_symmetric, but P is before Q */
2043 if (sh->pd_idx == 0)
2044 i--; /* P D D D Q */
2045 else {
2046 /* D D Q P D */
2047 if (i < sh->pd_idx)
2048 i += raid_disks;
2049 i -= (sh->pd_idx + 1);
2051 break;
2052 case ALGORITHM_LEFT_ASYMMETRIC_6:
2053 case ALGORITHM_RIGHT_ASYMMETRIC_6:
2054 if (i > sh->pd_idx)
2055 i--;
2056 break;
2057 case ALGORITHM_LEFT_SYMMETRIC_6:
2058 case ALGORITHM_RIGHT_SYMMETRIC_6:
2059 if (i < sh->pd_idx)
2060 i += data_disks + 1;
2061 i -= (sh->pd_idx + 1);
2062 break;
2063 case ALGORITHM_PARITY_0_6:
2064 i -= 1;
2065 break;
2066 default:
2067 BUG();
2069 break;
2072 chunk_number = stripe * data_disks + i;
2073 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
2075 check = raid5_compute_sector(conf, r_sector,
2076 previous, &dummy1, &sh2);
2077 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
2078 || sh2.qd_idx != sh->qd_idx) {
2079 printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n",
2080 mdname(conf->mddev));
2081 return 0;
2083 return r_sector;
2087 static void
2088 schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
2089 int rcw, int expand)
2091 int i, pd_idx = sh->pd_idx, disks = sh->disks;
2092 raid5_conf_t *conf = sh->raid_conf;
2093 int level = conf->level;
2095 if (rcw) {
2096 /* if we are not expanding this is a proper write request, and
2097 * there will be bios with new data to be drained into the
2098 * stripe cache
2100 if (!expand) {
2101 sh->reconstruct_state = reconstruct_state_drain_run;
2102 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2103 } else
2104 sh->reconstruct_state = reconstruct_state_run;
2106 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2108 for (i = disks; i--; ) {
2109 struct r5dev *dev = &sh->dev[i];
2111 if (dev->towrite) {
2112 set_bit(R5_LOCKED, &dev->flags);
2113 set_bit(R5_Wantdrain, &dev->flags);
2114 if (!expand)
2115 clear_bit(R5_UPTODATE, &dev->flags);
2116 s->locked++;
2119 if (s->locked + conf->max_degraded == disks)
2120 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
2121 atomic_inc(&conf->pending_full_writes);
2122 } else {
2123 BUG_ON(level == 6);
2124 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
2125 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
2127 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
2128 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
2129 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
2130 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
2132 for (i = disks; i--; ) {
2133 struct r5dev *dev = &sh->dev[i];
2134 if (i == pd_idx)
2135 continue;
2137 if (dev->towrite &&
2138 (test_bit(R5_UPTODATE, &dev->flags) ||
2139 test_bit(R5_Wantcompute, &dev->flags))) {
2140 set_bit(R5_Wantdrain, &dev->flags);
2141 set_bit(R5_LOCKED, &dev->flags);
2142 clear_bit(R5_UPTODATE, &dev->flags);
2143 s->locked++;
2148 /* keep the parity disk(s) locked while asynchronous operations
2149 * are in flight
2151 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
2152 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2153 s->locked++;
2155 if (level == 6) {
2156 int qd_idx = sh->qd_idx;
2157 struct r5dev *dev = &sh->dev[qd_idx];
2159 set_bit(R5_LOCKED, &dev->flags);
2160 clear_bit(R5_UPTODATE, &dev->flags);
2161 s->locked++;
2164 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
2165 __func__, (unsigned long long)sh->sector,
2166 s->locked, s->ops_request);
2170 * Each stripe/dev can have one or more bion attached.
2171 * toread/towrite point to the first in a chain.
2172 * The bi_next chain must be in order.
2174 static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite)
2176 struct bio **bip;
2177 raid5_conf_t *conf = sh->raid_conf;
2178 int firstwrite=0;
2180 pr_debug("adding bi b#%llu to stripe s#%llu\n",
2181 (unsigned long long)bi->bi_sector,
2182 (unsigned long long)sh->sector);
2185 spin_lock_irq(&conf->device_lock);
2186 if (forwrite) {
2187 bip = &sh->dev[dd_idx].towrite;
2188 if (*bip == NULL && sh->dev[dd_idx].written == NULL)
2189 firstwrite = 1;
2190 } else
2191 bip = &sh->dev[dd_idx].toread;
2192 while (*bip && (*bip)->bi_sector < bi->bi_sector) {
2193 if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector)
2194 goto overlap;
2195 bip = & (*bip)->bi_next;
2197 if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9))
2198 goto overlap;
2200 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
2201 if (*bip)
2202 bi->bi_next = *bip;
2203 *bip = bi;
2204 bi->bi_phys_segments++;
2206 if (forwrite) {
2207 /* check if page is covered */
2208 sector_t sector = sh->dev[dd_idx].sector;
2209 for (bi=sh->dev[dd_idx].towrite;
2210 sector < sh->dev[dd_idx].sector + STRIPE_SECTORS &&
2211 bi && bi->bi_sector <= sector;
2212 bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) {
2213 if (bi->bi_sector + (bi->bi_size>>9) >= sector)
2214 sector = bi->bi_sector + (bi->bi_size>>9);
2216 if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS)
2217 set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags);
2219 spin_unlock_irq(&conf->device_lock);
2221 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
2222 (unsigned long long)(*bip)->bi_sector,
2223 (unsigned long long)sh->sector, dd_idx);
2225 if (conf->mddev->bitmap && firstwrite) {
2226 bitmap_startwrite(conf->mddev->bitmap, sh->sector,
2227 STRIPE_SECTORS, 0);
2228 sh->bm_seq = conf->seq_flush+1;
2229 set_bit(STRIPE_BIT_DELAY, &sh->state);
2231 return 1;
2233 overlap:
2234 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
2235 spin_unlock_irq(&conf->device_lock);
2236 return 0;
2239 static void end_reshape(raid5_conf_t *conf);
2241 static void stripe_set_idx(sector_t stripe, raid5_conf_t *conf, int previous,
2242 struct stripe_head *sh)
2244 int sectors_per_chunk =
2245 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
2246 int dd_idx;
2247 int chunk_offset = sector_div(stripe, sectors_per_chunk);
2248 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
2250 raid5_compute_sector(conf,
2251 stripe * (disks - conf->max_degraded)
2252 *sectors_per_chunk + chunk_offset,
2253 previous,
2254 &dd_idx, sh);
2257 static void
2258 handle_failed_stripe(raid5_conf_t *conf, struct stripe_head *sh,
2259 struct stripe_head_state *s, int disks,
2260 struct bio **return_bi)
2262 int i;
2263 for (i = disks; i--; ) {
2264 struct bio *bi;
2265 int bitmap_end = 0;
2267 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
2268 mdk_rdev_t *rdev;
2269 rcu_read_lock();
2270 rdev = rcu_dereference(conf->disks[i].rdev);
2271 if (rdev && test_bit(In_sync, &rdev->flags))
2272 atomic_inc(&rdev->nr_pending);
2273 else
2274 rdev = NULL;
2275 rcu_read_unlock();
2276 if (rdev) {
2277 if (!rdev_set_badblocks(
2278 rdev,
2279 sh->sector,
2280 STRIPE_SECTORS, 0))
2281 md_error(conf->mddev, rdev);
2282 rdev_dec_pending(rdev, conf->mddev);
2285 spin_lock_irq(&conf->device_lock);
2286 /* fail all writes first */
2287 bi = sh->dev[i].towrite;
2288 sh->dev[i].towrite = NULL;
2289 if (bi) {
2290 s->to_write--;
2291 bitmap_end = 1;
2294 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2295 wake_up(&conf->wait_for_overlap);
2297 while (bi && bi->bi_sector <
2298 sh->dev[i].sector + STRIPE_SECTORS) {
2299 struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector);
2300 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2301 if (!raid5_dec_bi_phys_segments(bi)) {
2302 md_write_end(conf->mddev);
2303 bi->bi_next = *return_bi;
2304 *return_bi = bi;
2306 bi = nextbi;
2308 /* and fail all 'written' */
2309 bi = sh->dev[i].written;
2310 sh->dev[i].written = NULL;
2311 if (bi) bitmap_end = 1;
2312 while (bi && bi->bi_sector <
2313 sh->dev[i].sector + STRIPE_SECTORS) {
2314 struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector);
2315 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2316 if (!raid5_dec_bi_phys_segments(bi)) {
2317 md_write_end(conf->mddev);
2318 bi->bi_next = *return_bi;
2319 *return_bi = bi;
2321 bi = bi2;
2324 /* fail any reads if this device is non-operational and
2325 * the data has not reached the cache yet.
2327 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
2328 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
2329 test_bit(R5_ReadError, &sh->dev[i].flags))) {
2330 bi = sh->dev[i].toread;
2331 sh->dev[i].toread = NULL;
2332 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
2333 wake_up(&conf->wait_for_overlap);
2334 if (bi) s->to_read--;
2335 while (bi && bi->bi_sector <
2336 sh->dev[i].sector + STRIPE_SECTORS) {
2337 struct bio *nextbi =
2338 r5_next_bio(bi, sh->dev[i].sector);
2339 clear_bit(BIO_UPTODATE, &bi->bi_flags);
2340 if (!raid5_dec_bi_phys_segments(bi)) {
2341 bi->bi_next = *return_bi;
2342 *return_bi = bi;
2344 bi = nextbi;
2347 spin_unlock_irq(&conf->device_lock);
2348 if (bitmap_end)
2349 bitmap_endwrite(conf->mddev->bitmap, sh->sector,
2350 STRIPE_SECTORS, 0, 0);
2351 /* If we were in the middle of a write the parity block might
2352 * still be locked - so just clear all R5_LOCKED flags
2354 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2357 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2358 if (atomic_dec_and_test(&conf->pending_full_writes))
2359 md_wakeup_thread(conf->mddev->thread);
2362 static void
2363 handle_failed_sync(raid5_conf_t *conf, struct stripe_head *sh,
2364 struct stripe_head_state *s)
2366 int abort = 0;
2367 int i;
2369 md_done_sync(conf->mddev, STRIPE_SECTORS, 0);
2370 clear_bit(STRIPE_SYNCING, &sh->state);
2371 s->syncing = 0;
2372 /* There is nothing more to do for sync/check/repair.
2373 * For recover we need to record a bad block on all
2374 * non-sync devices, or abort the recovery
2376 if (!test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery))
2377 return;
2378 /* During recovery devices cannot be removed, so locking and
2379 * refcounting of rdevs is not needed
2381 for (i = 0; i < conf->raid_disks; i++) {
2382 mdk_rdev_t *rdev = conf->disks[i].rdev;
2383 if (!rdev
2384 || test_bit(Faulty, &rdev->flags)
2385 || test_bit(In_sync, &rdev->flags))
2386 continue;
2387 if (!rdev_set_badblocks(rdev, sh->sector,
2388 STRIPE_SECTORS, 0))
2389 abort = 1;
2391 if (abort) {
2392 conf->recovery_disabled = conf->mddev->recovery_disabled;
2393 set_bit(MD_RECOVERY_INTR, &conf->mddev->recovery);
2397 /* fetch_block - checks the given member device to see if its data needs
2398 * to be read or computed to satisfy a request.
2400 * Returns 1 when no more member devices need to be checked, otherwise returns
2401 * 0 to tell the loop in handle_stripe_fill to continue
2403 static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
2404 int disk_idx, int disks)
2406 struct r5dev *dev = &sh->dev[disk_idx];
2407 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
2408 &sh->dev[s->failed_num[1]] };
2410 /* is the data in this block needed, and can we get it? */
2411 if (!test_bit(R5_LOCKED, &dev->flags) &&
2412 !test_bit(R5_UPTODATE, &dev->flags) &&
2413 (dev->toread ||
2414 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) ||
2415 s->syncing || s->expanding ||
2416 (s->failed >= 1 && fdev[0]->toread) ||
2417 (s->failed >= 2 && fdev[1]->toread) ||
2418 (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite &&
2419 !test_bit(R5_OVERWRITE, &fdev[0]->flags)) ||
2420 (sh->raid_conf->level == 6 && s->failed && s->to_write))) {
2421 /* we would like to get this block, possibly by computing it,
2422 * otherwise read it if the backing disk is insync
2424 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
2425 BUG_ON(test_bit(R5_Wantread, &dev->flags));
2426 if ((s->uptodate == disks - 1) &&
2427 (s->failed && (disk_idx == s->failed_num[0] ||
2428 disk_idx == s->failed_num[1]))) {
2429 /* have disk failed, and we're requested to fetch it;
2430 * do compute it
2432 pr_debug("Computing stripe %llu block %d\n",
2433 (unsigned long long)sh->sector, disk_idx);
2434 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2435 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2436 set_bit(R5_Wantcompute, &dev->flags);
2437 sh->ops.target = disk_idx;
2438 sh->ops.target2 = -1; /* no 2nd target */
2439 s->req_compute = 1;
2440 /* Careful: from this point on 'uptodate' is in the eye
2441 * of raid_run_ops which services 'compute' operations
2442 * before writes. R5_Wantcompute flags a block that will
2443 * be R5_UPTODATE by the time it is needed for a
2444 * subsequent operation.
2446 s->uptodate++;
2447 return 1;
2448 } else if (s->uptodate == disks-2 && s->failed >= 2) {
2449 /* Computing 2-failure is *very* expensive; only
2450 * do it if failed >= 2
2452 int other;
2453 for (other = disks; other--; ) {
2454 if (other == disk_idx)
2455 continue;
2456 if (!test_bit(R5_UPTODATE,
2457 &sh->dev[other].flags))
2458 break;
2460 BUG_ON(other < 0);
2461 pr_debug("Computing stripe %llu blocks %d,%d\n",
2462 (unsigned long long)sh->sector,
2463 disk_idx, other);
2464 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2465 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2466 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
2467 set_bit(R5_Wantcompute, &sh->dev[other].flags);
2468 sh->ops.target = disk_idx;
2469 sh->ops.target2 = other;
2470 s->uptodate += 2;
2471 s->req_compute = 1;
2472 return 1;
2473 } else if (test_bit(R5_Insync, &dev->flags)) {
2474 set_bit(R5_LOCKED, &dev->flags);
2475 set_bit(R5_Wantread, &dev->flags);
2476 s->locked++;
2477 pr_debug("Reading block %d (sync=%d)\n",
2478 disk_idx, s->syncing);
2482 return 0;
2486 * handle_stripe_fill - read or compute data to satisfy pending requests.
2488 static void handle_stripe_fill(struct stripe_head *sh,
2489 struct stripe_head_state *s,
2490 int disks)
2492 int i;
2494 /* look for blocks to read/compute, skip this if a compute
2495 * is already in flight, or if the stripe contents are in the
2496 * midst of changing due to a write
2498 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
2499 !sh->reconstruct_state)
2500 for (i = disks; i--; )
2501 if (fetch_block(sh, s, i, disks))
2502 break;
2503 set_bit(STRIPE_HANDLE, &sh->state);
2507 /* handle_stripe_clean_event
2508 * any written block on an uptodate or failed drive can be returned.
2509 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
2510 * never LOCKED, so we don't need to test 'failed' directly.
2512 static void handle_stripe_clean_event(raid5_conf_t *conf,
2513 struct stripe_head *sh, int disks, struct bio **return_bi)
2515 int i;
2516 struct r5dev *dev;
2518 for (i = disks; i--; )
2519 if (sh->dev[i].written) {
2520 dev = &sh->dev[i];
2521 if (!test_bit(R5_LOCKED, &dev->flags) &&
2522 test_bit(R5_UPTODATE, &dev->flags)) {
2523 /* We can return any write requests */
2524 struct bio *wbi, *wbi2;
2525 int bitmap_end = 0;
2526 pr_debug("Return write for disc %d\n", i);
2527 spin_lock_irq(&conf->device_lock);
2528 wbi = dev->written;
2529 dev->written = NULL;
2530 while (wbi && wbi->bi_sector <
2531 dev->sector + STRIPE_SECTORS) {
2532 wbi2 = r5_next_bio(wbi, dev->sector);
2533 if (!raid5_dec_bi_phys_segments(wbi)) {
2534 md_write_end(conf->mddev);
2535 wbi->bi_next = *return_bi;
2536 *return_bi = wbi;
2538 wbi = wbi2;
2540 if (dev->towrite == NULL)
2541 bitmap_end = 1;
2542 spin_unlock_irq(&conf->device_lock);
2543 if (bitmap_end)
2544 bitmap_endwrite(conf->mddev->bitmap,
2545 sh->sector,
2546 STRIPE_SECTORS,
2547 !test_bit(STRIPE_DEGRADED, &sh->state),
2552 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
2553 if (atomic_dec_and_test(&conf->pending_full_writes))
2554 md_wakeup_thread(conf->mddev->thread);
2557 static void handle_stripe_dirtying(raid5_conf_t *conf,
2558 struct stripe_head *sh,
2559 struct stripe_head_state *s,
2560 int disks)
2562 int rmw = 0, rcw = 0, i;
2563 if (conf->max_degraded == 2) {
2564 /* RAID6 requires 'rcw' in current implementation
2565 * Calculate the real rcw later - for now fake it
2566 * look like rcw is cheaper
2568 rcw = 1; rmw = 2;
2569 } else for (i = disks; i--; ) {
2570 /* would I have to read this buffer for read_modify_write */
2571 struct r5dev *dev = &sh->dev[i];
2572 if ((dev->towrite || i == sh->pd_idx) &&
2573 !test_bit(R5_LOCKED, &dev->flags) &&
2574 !(test_bit(R5_UPTODATE, &dev->flags) ||
2575 test_bit(R5_Wantcompute, &dev->flags))) {
2576 if (test_bit(R5_Insync, &dev->flags))
2577 rmw++;
2578 else
2579 rmw += 2*disks; /* cannot read it */
2581 /* Would I have to read this buffer for reconstruct_write */
2582 if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx &&
2583 !test_bit(R5_LOCKED, &dev->flags) &&
2584 !(test_bit(R5_UPTODATE, &dev->flags) ||
2585 test_bit(R5_Wantcompute, &dev->flags))) {
2586 if (test_bit(R5_Insync, &dev->flags)) rcw++;
2587 else
2588 rcw += 2*disks;
2591 pr_debug("for sector %llu, rmw=%d rcw=%d\n",
2592 (unsigned long long)sh->sector, rmw, rcw);
2593 set_bit(STRIPE_HANDLE, &sh->state);
2594 if (rmw < rcw && rmw > 0)
2595 /* prefer read-modify-write, but need to get some data */
2596 for (i = disks; i--; ) {
2597 struct r5dev *dev = &sh->dev[i];
2598 if ((dev->towrite || i == sh->pd_idx) &&
2599 !test_bit(R5_LOCKED, &dev->flags) &&
2600 !(test_bit(R5_UPTODATE, &dev->flags) ||
2601 test_bit(R5_Wantcompute, &dev->flags)) &&
2602 test_bit(R5_Insync, &dev->flags)) {
2603 if (
2604 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2605 pr_debug("Read_old block "
2606 "%d for r-m-w\n", i);
2607 set_bit(R5_LOCKED, &dev->flags);
2608 set_bit(R5_Wantread, &dev->flags);
2609 s->locked++;
2610 } else {
2611 set_bit(STRIPE_DELAYED, &sh->state);
2612 set_bit(STRIPE_HANDLE, &sh->state);
2616 if (rcw <= rmw && rcw > 0) {
2617 /* want reconstruct write, but need to get some data */
2618 rcw = 0;
2619 for (i = disks; i--; ) {
2620 struct r5dev *dev = &sh->dev[i];
2621 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
2622 i != sh->pd_idx && i != sh->qd_idx &&
2623 !test_bit(R5_LOCKED, &dev->flags) &&
2624 !(test_bit(R5_UPTODATE, &dev->flags) ||
2625 test_bit(R5_Wantcompute, &dev->flags))) {
2626 rcw++;
2627 if (!test_bit(R5_Insync, &dev->flags))
2628 continue; /* it's a failed drive */
2629 if (
2630 test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) {
2631 pr_debug("Read_old block "
2632 "%d for Reconstruct\n", i);
2633 set_bit(R5_LOCKED, &dev->flags);
2634 set_bit(R5_Wantread, &dev->flags);
2635 s->locked++;
2636 } else {
2637 set_bit(STRIPE_DELAYED, &sh->state);
2638 set_bit(STRIPE_HANDLE, &sh->state);
2643 /* now if nothing is locked, and if we have enough data,
2644 * we can start a write request
2646 /* since handle_stripe can be called at any time we need to handle the
2647 * case where a compute block operation has been submitted and then a
2648 * subsequent call wants to start a write request. raid_run_ops only
2649 * handles the case where compute block and reconstruct are requested
2650 * simultaneously. If this is not the case then new writes need to be
2651 * held off until the compute completes.
2653 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
2654 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
2655 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
2656 schedule_reconstruction(sh, s, rcw == 0, 0);
2659 static void handle_parity_checks5(raid5_conf_t *conf, struct stripe_head *sh,
2660 struct stripe_head_state *s, int disks)
2662 struct r5dev *dev = NULL;
2664 set_bit(STRIPE_HANDLE, &sh->state);
2666 switch (sh->check_state) {
2667 case check_state_idle:
2668 /* start a new check operation if there are no failures */
2669 if (s->failed == 0) {
2670 BUG_ON(s->uptodate != disks);
2671 sh->check_state = check_state_run;
2672 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2673 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
2674 s->uptodate--;
2675 break;
2677 dev = &sh->dev[s->failed_num[0]];
2678 /* fall through */
2679 case check_state_compute_result:
2680 sh->check_state = check_state_idle;
2681 if (!dev)
2682 dev = &sh->dev[sh->pd_idx];
2684 /* check that a write has not made the stripe insync */
2685 if (test_bit(STRIPE_INSYNC, &sh->state))
2686 break;
2688 /* either failed parity check, or recovery is happening */
2689 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
2690 BUG_ON(s->uptodate != disks);
2692 set_bit(R5_LOCKED, &dev->flags);
2693 s->locked++;
2694 set_bit(R5_Wantwrite, &dev->flags);
2696 clear_bit(STRIPE_DEGRADED, &sh->state);
2697 set_bit(STRIPE_INSYNC, &sh->state);
2698 break;
2699 case check_state_run:
2700 break; /* we will be called again upon completion */
2701 case check_state_check_result:
2702 sh->check_state = check_state_idle;
2704 /* if a failure occurred during the check operation, leave
2705 * STRIPE_INSYNC not set and let the stripe be handled again
2707 if (s->failed)
2708 break;
2710 /* handle a successful check operation, if parity is correct
2711 * we are done. Otherwise update the mismatch count and repair
2712 * parity if !MD_RECOVERY_CHECK
2714 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
2715 /* parity is correct (on disc,
2716 * not in buffer any more)
2718 set_bit(STRIPE_INSYNC, &sh->state);
2719 else {
2720 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2721 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2722 /* don't try to repair!! */
2723 set_bit(STRIPE_INSYNC, &sh->state);
2724 else {
2725 sh->check_state = check_state_compute_run;
2726 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2727 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2728 set_bit(R5_Wantcompute,
2729 &sh->dev[sh->pd_idx].flags);
2730 sh->ops.target = sh->pd_idx;
2731 sh->ops.target2 = -1;
2732 s->uptodate++;
2735 break;
2736 case check_state_compute_run:
2737 break;
2738 default:
2739 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2740 __func__, sh->check_state,
2741 (unsigned long long) sh->sector);
2742 BUG();
2747 static void handle_parity_checks6(raid5_conf_t *conf, struct stripe_head *sh,
2748 struct stripe_head_state *s,
2749 int disks)
2751 int pd_idx = sh->pd_idx;
2752 int qd_idx = sh->qd_idx;
2753 struct r5dev *dev;
2755 set_bit(STRIPE_HANDLE, &sh->state);
2757 BUG_ON(s->failed > 2);
2759 /* Want to check and possibly repair P and Q.
2760 * However there could be one 'failed' device, in which
2761 * case we can only check one of them, possibly using the
2762 * other to generate missing data
2765 switch (sh->check_state) {
2766 case check_state_idle:
2767 /* start a new check operation if there are < 2 failures */
2768 if (s->failed == s->q_failed) {
2769 /* The only possible failed device holds Q, so it
2770 * makes sense to check P (If anything else were failed,
2771 * we would have used P to recreate it).
2773 sh->check_state = check_state_run;
2775 if (!s->q_failed && s->failed < 2) {
2776 /* Q is not failed, and we didn't use it to generate
2777 * anything, so it makes sense to check it
2779 if (sh->check_state == check_state_run)
2780 sh->check_state = check_state_run_pq;
2781 else
2782 sh->check_state = check_state_run_q;
2785 /* discard potentially stale zero_sum_result */
2786 sh->ops.zero_sum_result = 0;
2788 if (sh->check_state == check_state_run) {
2789 /* async_xor_zero_sum destroys the contents of P */
2790 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
2791 s->uptodate--;
2793 if (sh->check_state >= check_state_run &&
2794 sh->check_state <= check_state_run_pq) {
2795 /* async_syndrome_zero_sum preserves P and Q, so
2796 * no need to mark them !uptodate here
2798 set_bit(STRIPE_OP_CHECK, &s->ops_request);
2799 break;
2802 /* we have 2-disk failure */
2803 BUG_ON(s->failed != 2);
2804 /* fall through */
2805 case check_state_compute_result:
2806 sh->check_state = check_state_idle;
2808 /* check that a write has not made the stripe insync */
2809 if (test_bit(STRIPE_INSYNC, &sh->state))
2810 break;
2812 /* now write out any block on a failed drive,
2813 * or P or Q if they were recomputed
2815 BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */
2816 if (s->failed == 2) {
2817 dev = &sh->dev[s->failed_num[1]];
2818 s->locked++;
2819 set_bit(R5_LOCKED, &dev->flags);
2820 set_bit(R5_Wantwrite, &dev->flags);
2822 if (s->failed >= 1) {
2823 dev = &sh->dev[s->failed_num[0]];
2824 s->locked++;
2825 set_bit(R5_LOCKED, &dev->flags);
2826 set_bit(R5_Wantwrite, &dev->flags);
2828 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2829 dev = &sh->dev[pd_idx];
2830 s->locked++;
2831 set_bit(R5_LOCKED, &dev->flags);
2832 set_bit(R5_Wantwrite, &dev->flags);
2834 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2835 dev = &sh->dev[qd_idx];
2836 s->locked++;
2837 set_bit(R5_LOCKED, &dev->flags);
2838 set_bit(R5_Wantwrite, &dev->flags);
2840 clear_bit(STRIPE_DEGRADED, &sh->state);
2842 set_bit(STRIPE_INSYNC, &sh->state);
2843 break;
2844 case check_state_run:
2845 case check_state_run_q:
2846 case check_state_run_pq:
2847 break; /* we will be called again upon completion */
2848 case check_state_check_result:
2849 sh->check_state = check_state_idle;
2851 /* handle a successful check operation, if parity is correct
2852 * we are done. Otherwise update the mismatch count and repair
2853 * parity if !MD_RECOVERY_CHECK
2855 if (sh->ops.zero_sum_result == 0) {
2856 /* both parities are correct */
2857 if (!s->failed)
2858 set_bit(STRIPE_INSYNC, &sh->state);
2859 else {
2860 /* in contrast to the raid5 case we can validate
2861 * parity, but still have a failure to write
2862 * back
2864 sh->check_state = check_state_compute_result;
2865 /* Returning at this point means that we may go
2866 * off and bring p and/or q uptodate again so
2867 * we make sure to check zero_sum_result again
2868 * to verify if p or q need writeback
2871 } else {
2872 conf->mddev->resync_mismatches += STRIPE_SECTORS;
2873 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery))
2874 /* don't try to repair!! */
2875 set_bit(STRIPE_INSYNC, &sh->state);
2876 else {
2877 int *target = &sh->ops.target;
2879 sh->ops.target = -1;
2880 sh->ops.target2 = -1;
2881 sh->check_state = check_state_compute_run;
2882 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
2883 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
2884 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
2885 set_bit(R5_Wantcompute,
2886 &sh->dev[pd_idx].flags);
2887 *target = pd_idx;
2888 target = &sh->ops.target2;
2889 s->uptodate++;
2891 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
2892 set_bit(R5_Wantcompute,
2893 &sh->dev[qd_idx].flags);
2894 *target = qd_idx;
2895 s->uptodate++;
2899 break;
2900 case check_state_compute_run:
2901 break;
2902 default:
2903 printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n",
2904 __func__, sh->check_state,
2905 (unsigned long long) sh->sector);
2906 BUG();
2910 static void handle_stripe_expansion(raid5_conf_t *conf, struct stripe_head *sh)
2912 int i;
2914 /* We have read all the blocks in this stripe and now we need to
2915 * copy some of them into a target stripe for expand.
2917 struct dma_async_tx_descriptor *tx = NULL;
2918 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2919 for (i = 0; i < sh->disks; i++)
2920 if (i != sh->pd_idx && i != sh->qd_idx) {
2921 int dd_idx, j;
2922 struct stripe_head *sh2;
2923 struct async_submit_ctl submit;
2925 sector_t bn = compute_blocknr(sh, i, 1);
2926 sector_t s = raid5_compute_sector(conf, bn, 0,
2927 &dd_idx, NULL);
2928 sh2 = get_active_stripe(conf, s, 0, 1, 1);
2929 if (sh2 == NULL)
2930 /* so far only the early blocks of this stripe
2931 * have been requested. When later blocks
2932 * get requested, we will try again
2934 continue;
2935 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
2936 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
2937 /* must have already done this block */
2938 release_stripe(sh2);
2939 continue;
2942 /* place all the copies on one channel */
2943 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
2944 tx = async_memcpy(sh2->dev[dd_idx].page,
2945 sh->dev[i].page, 0, 0, STRIPE_SIZE,
2946 &submit);
2948 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
2949 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
2950 for (j = 0; j < conf->raid_disks; j++)
2951 if (j != sh2->pd_idx &&
2952 j != sh2->qd_idx &&
2953 !test_bit(R5_Expanded, &sh2->dev[j].flags))
2954 break;
2955 if (j == conf->raid_disks) {
2956 set_bit(STRIPE_EXPAND_READY, &sh2->state);
2957 set_bit(STRIPE_HANDLE, &sh2->state);
2959 release_stripe(sh2);
2962 /* done submitting copies, wait for them to complete */
2963 if (tx) {
2964 async_tx_ack(tx);
2965 dma_wait_for_async_tx(tx);
2971 * handle_stripe - do things to a stripe.
2973 * We lock the stripe and then examine the state of various bits
2974 * to see what needs to be done.
2975 * Possible results:
2976 * return some read request which now have data
2977 * return some write requests which are safely on disc
2978 * schedule a read on some buffers
2979 * schedule a write of some buffers
2980 * return confirmation of parity correctness
2982 * buffers are taken off read_list or write_list, and bh_cache buffers
2983 * get BH_Lock set before the stripe lock is released.
2987 static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
2989 raid5_conf_t *conf = sh->raid_conf;
2990 int disks = sh->disks;
2991 struct r5dev *dev;
2992 int i;
2994 memset(s, 0, sizeof(*s));
2996 s->syncing = test_bit(STRIPE_SYNCING, &sh->state);
2997 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state);
2998 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state);
2999 s->failed_num[0] = -1;
3000 s->failed_num[1] = -1;
3002 /* Now to look around and see what can be done */
3003 rcu_read_lock();
3004 spin_lock_irq(&conf->device_lock);
3005 for (i=disks; i--; ) {
3006 mdk_rdev_t *rdev;
3007 sector_t first_bad;
3008 int bad_sectors;
3009 int is_bad = 0;
3011 dev = &sh->dev[i];
3013 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
3014 i, dev->flags, dev->toread, dev->towrite, dev->written);
3015 /* maybe we can reply to a read
3017 * new wantfill requests are only permitted while
3018 * ops_complete_biofill is guaranteed to be inactive
3020 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
3021 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
3022 set_bit(R5_Wantfill, &dev->flags);
3024 /* now count some things */
3025 if (test_bit(R5_LOCKED, &dev->flags))
3026 s->locked++;
3027 if (test_bit(R5_UPTODATE, &dev->flags))
3028 s->uptodate++;
3029 if (test_bit(R5_Wantcompute, &dev->flags)) {
3030 s->compute++;
3031 BUG_ON(s->compute > 2);
3034 if (test_bit(R5_Wantfill, &dev->flags))
3035 s->to_fill++;
3036 else if (dev->toread)
3037 s->to_read++;
3038 if (dev->towrite) {
3039 s->to_write++;
3040 if (!test_bit(R5_OVERWRITE, &dev->flags))
3041 s->non_overwrite++;
3043 if (dev->written)
3044 s->written++;
3045 rdev = rcu_dereference(conf->disks[i].rdev);
3046 if (rdev) {
3047 is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS,
3048 &first_bad, &bad_sectors);
3049 if (s->blocked_rdev == NULL
3050 && (test_bit(Blocked, &rdev->flags)
3051 || is_bad < 0)) {
3052 if (is_bad < 0)
3053 set_bit(BlockedBadBlocks,
3054 &rdev->flags);
3055 s->blocked_rdev = rdev;
3056 atomic_inc(&rdev->nr_pending);
3059 clear_bit(R5_Insync, &dev->flags);
3060 if (!rdev)
3061 /* Not in-sync */;
3062 else if (is_bad) {
3063 /* also not in-sync */
3064 if (!test_bit(WriteErrorSeen, &rdev->flags)) {
3065 /* treat as in-sync, but with a read error
3066 * which we can now try to correct
3068 set_bit(R5_Insync, &dev->flags);
3069 set_bit(R5_ReadError, &dev->flags);
3071 } else if (test_bit(In_sync, &rdev->flags))
3072 set_bit(R5_Insync, &dev->flags);
3073 else {
3074 /* in sync if before recovery_offset */
3075 if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset)
3076 set_bit(R5_Insync, &dev->flags);
3078 if (test_bit(R5_WriteError, &dev->flags)) {
3079 clear_bit(R5_Insync, &dev->flags);
3080 if (!test_bit(Faulty, &rdev->flags)) {
3081 s->handle_bad_blocks = 1;
3082 atomic_inc(&rdev->nr_pending);
3083 } else
3084 clear_bit(R5_WriteError, &dev->flags);
3086 if (test_bit(R5_MadeGood, &dev->flags)) {
3087 if (!test_bit(Faulty, &rdev->flags)) {
3088 s->handle_bad_blocks = 1;
3089 atomic_inc(&rdev->nr_pending);
3090 } else
3091 clear_bit(R5_MadeGood, &dev->flags);
3093 if (!test_bit(R5_Insync, &dev->flags)) {
3094 /* The ReadError flag will just be confusing now */
3095 clear_bit(R5_ReadError, &dev->flags);
3096 clear_bit(R5_ReWrite, &dev->flags);
3098 if (test_bit(R5_ReadError, &dev->flags))
3099 clear_bit(R5_Insync, &dev->flags);
3100 if (!test_bit(R5_Insync, &dev->flags)) {
3101 if (s->failed < 2)
3102 s->failed_num[s->failed] = i;
3103 s->failed++;
3106 spin_unlock_irq(&conf->device_lock);
3107 rcu_read_unlock();
3110 static void handle_stripe(struct stripe_head *sh)
3112 struct stripe_head_state s;
3113 raid5_conf_t *conf = sh->raid_conf;
3114 int i;
3115 int prexor;
3116 int disks = sh->disks;
3117 struct r5dev *pdev, *qdev;
3119 clear_bit(STRIPE_HANDLE, &sh->state);
3120 if (test_and_set_bit(STRIPE_ACTIVE, &sh->state)) {
3121 /* already being handled, ensure it gets handled
3122 * again when current action finishes */
3123 set_bit(STRIPE_HANDLE, &sh->state);
3124 return;
3127 if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
3128 set_bit(STRIPE_SYNCING, &sh->state);
3129 clear_bit(STRIPE_INSYNC, &sh->state);
3131 clear_bit(STRIPE_DELAYED, &sh->state);
3133 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
3134 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
3135 (unsigned long long)sh->sector, sh->state,
3136 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
3137 sh->check_state, sh->reconstruct_state);
3139 analyse_stripe(sh, &s);
3141 if (s.handle_bad_blocks) {
3142 set_bit(STRIPE_HANDLE, &sh->state);
3143 goto finish;
3146 if (unlikely(s.blocked_rdev)) {
3147 if (s.syncing || s.expanding || s.expanded ||
3148 s.to_write || s.written) {
3149 set_bit(STRIPE_HANDLE, &sh->state);
3150 goto finish;
3152 /* There is nothing for the blocked_rdev to block */
3153 rdev_dec_pending(s.blocked_rdev, conf->mddev);
3154 s.blocked_rdev = NULL;
3157 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
3158 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
3159 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
3162 pr_debug("locked=%d uptodate=%d to_read=%d"
3163 " to_write=%d failed=%d failed_num=%d,%d\n",
3164 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
3165 s.failed_num[0], s.failed_num[1]);
3166 /* check if the array has lost more than max_degraded devices and,
3167 * if so, some requests might need to be failed.
3169 if (s.failed > conf->max_degraded && s.to_read+s.to_write+s.written)
3170 handle_failed_stripe(conf, sh, &s, disks, &s.return_bi);
3171 if (s.failed > conf->max_degraded && s.syncing)
3172 handle_failed_sync(conf, sh, &s);
3175 * might be able to return some write requests if the parity blocks
3176 * are safe, or on a failed drive
3178 pdev = &sh->dev[sh->pd_idx];
3179 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
3180 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
3181 qdev = &sh->dev[sh->qd_idx];
3182 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
3183 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
3184 || conf->level < 6;
3186 if (s.written &&
3187 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
3188 && !test_bit(R5_LOCKED, &pdev->flags)
3189 && test_bit(R5_UPTODATE, &pdev->flags)))) &&
3190 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
3191 && !test_bit(R5_LOCKED, &qdev->flags)
3192 && test_bit(R5_UPTODATE, &qdev->flags)))))
3193 handle_stripe_clean_event(conf, sh, disks, &s.return_bi);
3195 /* Now we might consider reading some blocks, either to check/generate
3196 * parity, or to satisfy requests
3197 * or to load a block that is being partially written.
3199 if (s.to_read || s.non_overwrite
3200 || (conf->level == 6 && s.to_write && s.failed)
3201 || (s.syncing && (s.uptodate + s.compute < disks)) || s.expanding)
3202 handle_stripe_fill(sh, &s, disks);
3204 /* Now we check to see if any write operations have recently
3205 * completed
3207 prexor = 0;
3208 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
3209 prexor = 1;
3210 if (sh->reconstruct_state == reconstruct_state_drain_result ||
3211 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
3212 sh->reconstruct_state = reconstruct_state_idle;
3214 /* All the 'written' buffers and the parity block are ready to
3215 * be written back to disk
3217 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags));
3218 BUG_ON(sh->qd_idx >= 0 &&
3219 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags));
3220 for (i = disks; i--; ) {
3221 struct r5dev *dev = &sh->dev[i];
3222 if (test_bit(R5_LOCKED, &dev->flags) &&
3223 (i == sh->pd_idx || i == sh->qd_idx ||
3224 dev->written)) {
3225 pr_debug("Writing block %d\n", i);
3226 set_bit(R5_Wantwrite, &dev->flags);
3227 if (prexor)
3228 continue;
3229 if (!test_bit(R5_Insync, &dev->flags) ||
3230 ((i == sh->pd_idx || i == sh->qd_idx) &&
3231 s.failed == 0))
3232 set_bit(STRIPE_INSYNC, &sh->state);
3235 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3236 s.dec_preread_active = 1;
3239 /* Now to consider new write requests and what else, if anything
3240 * should be read. We do not handle new writes when:
3241 * 1/ A 'write' operation (copy+xor) is already in flight.
3242 * 2/ A 'check' operation is in flight, as it may clobber the parity
3243 * block.
3245 if (s.to_write && !sh->reconstruct_state && !sh->check_state)
3246 handle_stripe_dirtying(conf, sh, &s, disks);
3248 /* maybe we need to check and possibly fix the parity for this stripe
3249 * Any reads will already have been scheduled, so we just see if enough
3250 * data is available. The parity check is held off while parity
3251 * dependent operations are in flight.
3253 if (sh->check_state ||
3254 (s.syncing && s.locked == 0 &&
3255 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
3256 !test_bit(STRIPE_INSYNC, &sh->state))) {
3257 if (conf->level == 6)
3258 handle_parity_checks6(conf, sh, &s, disks);
3259 else
3260 handle_parity_checks5(conf, sh, &s, disks);
3263 if (s.syncing && s.locked == 0 && test_bit(STRIPE_INSYNC, &sh->state)) {
3264 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3265 clear_bit(STRIPE_SYNCING, &sh->state);
3268 /* If the failed drives are just a ReadError, then we might need
3269 * to progress the repair/check process
3271 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
3272 for (i = 0; i < s.failed; i++) {
3273 struct r5dev *dev = &sh->dev[s.failed_num[i]];
3274 if (test_bit(R5_ReadError, &dev->flags)
3275 && !test_bit(R5_LOCKED, &dev->flags)
3276 && test_bit(R5_UPTODATE, &dev->flags)
3278 if (!test_bit(R5_ReWrite, &dev->flags)) {
3279 set_bit(R5_Wantwrite, &dev->flags);
3280 set_bit(R5_ReWrite, &dev->flags);
3281 set_bit(R5_LOCKED, &dev->flags);
3282 s.locked++;
3283 } else {
3284 /* let's read it back */
3285 set_bit(R5_Wantread, &dev->flags);
3286 set_bit(R5_LOCKED, &dev->flags);
3287 s.locked++;
3293 /* Finish reconstruct operations initiated by the expansion process */
3294 if (sh->reconstruct_state == reconstruct_state_result) {
3295 struct stripe_head *sh_src
3296 = get_active_stripe(conf, sh->sector, 1, 1, 1);
3297 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
3298 /* sh cannot be written until sh_src has been read.
3299 * so arrange for sh to be delayed a little
3301 set_bit(STRIPE_DELAYED, &sh->state);
3302 set_bit(STRIPE_HANDLE, &sh->state);
3303 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
3304 &sh_src->state))
3305 atomic_inc(&conf->preread_active_stripes);
3306 release_stripe(sh_src);
3307 goto finish;
3309 if (sh_src)
3310 release_stripe(sh_src);
3312 sh->reconstruct_state = reconstruct_state_idle;
3313 clear_bit(STRIPE_EXPANDING, &sh->state);
3314 for (i = conf->raid_disks; i--; ) {
3315 set_bit(R5_Wantwrite, &sh->dev[i].flags);
3316 set_bit(R5_LOCKED, &sh->dev[i].flags);
3317 s.locked++;
3321 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
3322 !sh->reconstruct_state) {
3323 /* Need to write out all blocks after computing parity */
3324 sh->disks = conf->raid_disks;
3325 stripe_set_idx(sh->sector, conf, 0, sh);
3326 schedule_reconstruction(sh, &s, 1, 1);
3327 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
3328 clear_bit(STRIPE_EXPAND_READY, &sh->state);
3329 atomic_dec(&conf->reshape_stripes);
3330 wake_up(&conf->wait_for_overlap);
3331 md_done_sync(conf->mddev, STRIPE_SECTORS, 1);
3334 if (s.expanding && s.locked == 0 &&
3335 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
3336 handle_stripe_expansion(conf, sh);
3338 finish:
3339 /* wait for this device to become unblocked */
3340 if (unlikely(s.blocked_rdev))
3341 md_wait_for_blocked_rdev(s.blocked_rdev, conf->mddev);
3343 if (s.handle_bad_blocks)
3344 for (i = disks; i--; ) {
3345 mdk_rdev_t *rdev;
3346 struct r5dev *dev = &sh->dev[i];
3347 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
3348 /* We own a safe reference to the rdev */
3349 rdev = conf->disks[i].rdev;
3350 if (!rdev_set_badblocks(rdev, sh->sector,
3351 STRIPE_SECTORS, 0))
3352 md_error(conf->mddev, rdev);
3353 rdev_dec_pending(rdev, conf->mddev);
3355 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
3356 rdev = conf->disks[i].rdev;
3357 rdev_clear_badblocks(rdev, sh->sector,
3358 STRIPE_SECTORS);
3359 rdev_dec_pending(rdev, conf->mddev);
3363 if (s.ops_request)
3364 raid_run_ops(sh, s.ops_request);
3366 ops_run_io(sh, &s);
3368 if (s.dec_preread_active) {
3369 /* We delay this until after ops_run_io so that if make_request
3370 * is waiting on a flush, it won't continue until the writes
3371 * have actually been submitted.
3373 atomic_dec(&conf->preread_active_stripes);
3374 if (atomic_read(&conf->preread_active_stripes) <
3375 IO_THRESHOLD)
3376 md_wakeup_thread(conf->mddev->thread);
3379 return_io(s.return_bi);
3381 clear_bit(STRIPE_ACTIVE, &sh->state);
3384 static void raid5_activate_delayed(raid5_conf_t *conf)
3386 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
3387 while (!list_empty(&conf->delayed_list)) {
3388 struct list_head *l = conf->delayed_list.next;
3389 struct stripe_head *sh;
3390 sh = list_entry(l, struct stripe_head, lru);
3391 list_del_init(l);
3392 clear_bit(STRIPE_DELAYED, &sh->state);
3393 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3394 atomic_inc(&conf->preread_active_stripes);
3395 list_add_tail(&sh->lru, &conf->hold_list);
3400 static void activate_bit_delay(raid5_conf_t *conf)
3402 /* device_lock is held */
3403 struct list_head head;
3404 list_add(&head, &conf->bitmap_list);
3405 list_del_init(&conf->bitmap_list);
3406 while (!list_empty(&head)) {
3407 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
3408 list_del_init(&sh->lru);
3409 atomic_inc(&sh->count);
3410 __release_stripe(conf, sh);
3414 int md_raid5_congested(mddev_t *mddev, int bits)
3416 raid5_conf_t *conf = mddev->private;
3418 /* No difference between reads and writes. Just check
3419 * how busy the stripe_cache is
3422 if (conf->inactive_blocked)
3423 return 1;
3424 if (conf->quiesce)
3425 return 1;
3426 if (list_empty_careful(&conf->inactive_list))
3427 return 1;
3429 return 0;
3431 EXPORT_SYMBOL_GPL(md_raid5_congested);
3433 static int raid5_congested(void *data, int bits)
3435 mddev_t *mddev = data;
3437 return mddev_congested(mddev, bits) ||
3438 md_raid5_congested(mddev, bits);
3441 /* We want read requests to align with chunks where possible,
3442 * but write requests don't need to.
3444 static int raid5_mergeable_bvec(struct request_queue *q,
3445 struct bvec_merge_data *bvm,
3446 struct bio_vec *biovec)
3448 mddev_t *mddev = q->queuedata;
3449 sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev);
3450 int max;
3451 unsigned int chunk_sectors = mddev->chunk_sectors;
3452 unsigned int bio_sectors = bvm->bi_size >> 9;
3454 if ((bvm->bi_rw & 1) == WRITE)
3455 return biovec->bv_len; /* always allow writes to be mergeable */
3457 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3458 chunk_sectors = mddev->new_chunk_sectors;
3459 max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9;
3460 if (max < 0) max = 0;
3461 if (max <= biovec->bv_len && bio_sectors == 0)
3462 return biovec->bv_len;
3463 else
3464 return max;
3468 static int in_chunk_boundary(mddev_t *mddev, struct bio *bio)
3470 sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev);
3471 unsigned int chunk_sectors = mddev->chunk_sectors;
3472 unsigned int bio_sectors = bio->bi_size >> 9;
3474 if (mddev->new_chunk_sectors < mddev->chunk_sectors)
3475 chunk_sectors = mddev->new_chunk_sectors;
3476 return chunk_sectors >=
3477 ((sector & (chunk_sectors - 1)) + bio_sectors);
3481 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
3482 * later sampled by raid5d.
3484 static void add_bio_to_retry(struct bio *bi,raid5_conf_t *conf)
3486 unsigned long flags;
3488 spin_lock_irqsave(&conf->device_lock, flags);
3490 bi->bi_next = conf->retry_read_aligned_list;
3491 conf->retry_read_aligned_list = bi;
3493 spin_unlock_irqrestore(&conf->device_lock, flags);
3494 md_wakeup_thread(conf->mddev->thread);
3498 static struct bio *remove_bio_from_retry(raid5_conf_t *conf)
3500 struct bio *bi;
3502 bi = conf->retry_read_aligned;
3503 if (bi) {
3504 conf->retry_read_aligned = NULL;
3505 return bi;
3507 bi = conf->retry_read_aligned_list;
3508 if(bi) {
3509 conf->retry_read_aligned_list = bi->bi_next;
3510 bi->bi_next = NULL;
3512 * this sets the active strip count to 1 and the processed
3513 * strip count to zero (upper 8 bits)
3515 bi->bi_phys_segments = 1; /* biased count of active stripes */
3518 return bi;
3523 * The "raid5_align_endio" should check if the read succeeded and if it
3524 * did, call bio_endio on the original bio (having bio_put the new bio
3525 * first).
3526 * If the read failed..
3528 static void raid5_align_endio(struct bio *bi, int error)
3530 struct bio* raid_bi = bi->bi_private;
3531 mddev_t *mddev;
3532 raid5_conf_t *conf;
3533 int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags);
3534 mdk_rdev_t *rdev;
3536 bio_put(bi);
3538 rdev = (void*)raid_bi->bi_next;
3539 raid_bi->bi_next = NULL;
3540 mddev = rdev->mddev;
3541 conf = mddev->private;
3543 rdev_dec_pending(rdev, conf->mddev);
3545 if (!error && uptodate) {
3546 bio_endio(raid_bi, 0);
3547 if (atomic_dec_and_test(&conf->active_aligned_reads))
3548 wake_up(&conf->wait_for_stripe);
3549 return;
3553 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
3555 add_bio_to_retry(raid_bi, conf);
3558 static int bio_fits_rdev(struct bio *bi)
3560 struct request_queue *q = bdev_get_queue(bi->bi_bdev);
3562 if ((bi->bi_size>>9) > queue_max_sectors(q))
3563 return 0;
3564 blk_recount_segments(q, bi);
3565 if (bi->bi_phys_segments > queue_max_segments(q))
3566 return 0;
3568 if (q->merge_bvec_fn)
3569 /* it's too hard to apply the merge_bvec_fn at this stage,
3570 * just just give up
3572 return 0;
3574 return 1;
3578 static int chunk_aligned_read(mddev_t *mddev, struct bio * raid_bio)
3580 raid5_conf_t *conf = mddev->private;
3581 int dd_idx;
3582 struct bio* align_bi;
3583 mdk_rdev_t *rdev;
3585 if (!in_chunk_boundary(mddev, raid_bio)) {
3586 pr_debug("chunk_aligned_read : non aligned\n");
3587 return 0;
3590 * use bio_clone_mddev to make a copy of the bio
3592 align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev);
3593 if (!align_bi)
3594 return 0;
3596 * set bi_end_io to a new function, and set bi_private to the
3597 * original bio.
3599 align_bi->bi_end_io = raid5_align_endio;
3600 align_bi->bi_private = raid_bio;
3602 * compute position
3604 align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector,
3606 &dd_idx, NULL);
3608 rcu_read_lock();
3609 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
3610 if (rdev && test_bit(In_sync, &rdev->flags)) {
3611 sector_t first_bad;
3612 int bad_sectors;
3614 atomic_inc(&rdev->nr_pending);
3615 rcu_read_unlock();
3616 raid_bio->bi_next = (void*)rdev;
3617 align_bi->bi_bdev = rdev->bdev;
3618 align_bi->bi_flags &= ~(1 << BIO_SEG_VALID);
3619 align_bi->bi_sector += rdev->data_offset;
3621 if (!bio_fits_rdev(align_bi) ||
3622 is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9,
3623 &first_bad, &bad_sectors)) {
3624 /* too big in some way, or has a known bad block */
3625 bio_put(align_bi);
3626 rdev_dec_pending(rdev, mddev);
3627 return 0;
3630 spin_lock_irq(&conf->device_lock);
3631 wait_event_lock_irq(conf->wait_for_stripe,
3632 conf->quiesce == 0,
3633 conf->device_lock, /* nothing */);
3634 atomic_inc(&conf->active_aligned_reads);
3635 spin_unlock_irq(&conf->device_lock);
3637 generic_make_request(align_bi);
3638 return 1;
3639 } else {
3640 rcu_read_unlock();
3641 bio_put(align_bi);
3642 return 0;
3646 /* __get_priority_stripe - get the next stripe to process
3648 * Full stripe writes are allowed to pass preread active stripes up until
3649 * the bypass_threshold is exceeded. In general the bypass_count
3650 * increments when the handle_list is handled before the hold_list; however, it
3651 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
3652 * stripe with in flight i/o. The bypass_count will be reset when the
3653 * head of the hold_list has changed, i.e. the head was promoted to the
3654 * handle_list.
3656 static struct stripe_head *__get_priority_stripe(raid5_conf_t *conf)
3658 struct stripe_head *sh;
3660 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
3661 __func__,
3662 list_empty(&conf->handle_list) ? "empty" : "busy",
3663 list_empty(&conf->hold_list) ? "empty" : "busy",
3664 atomic_read(&conf->pending_full_writes), conf->bypass_count);
3666 if (!list_empty(&conf->handle_list)) {
3667 sh = list_entry(conf->handle_list.next, typeof(*sh), lru);
3669 if (list_empty(&conf->hold_list))
3670 conf->bypass_count = 0;
3671 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
3672 if (conf->hold_list.next == conf->last_hold)
3673 conf->bypass_count++;
3674 else {
3675 conf->last_hold = conf->hold_list.next;
3676 conf->bypass_count -= conf->bypass_threshold;
3677 if (conf->bypass_count < 0)
3678 conf->bypass_count = 0;
3681 } else if (!list_empty(&conf->hold_list) &&
3682 ((conf->bypass_threshold &&
3683 conf->bypass_count > conf->bypass_threshold) ||
3684 atomic_read(&conf->pending_full_writes) == 0)) {
3685 sh = list_entry(conf->hold_list.next,
3686 typeof(*sh), lru);
3687 conf->bypass_count -= conf->bypass_threshold;
3688 if (conf->bypass_count < 0)
3689 conf->bypass_count = 0;
3690 } else
3691 return NULL;
3693 list_del_init(&sh->lru);
3694 atomic_inc(&sh->count);
3695 BUG_ON(atomic_read(&sh->count) != 1);
3696 return sh;
3699 static int make_request(mddev_t *mddev, struct bio * bi)
3701 raid5_conf_t *conf = mddev->private;
3702 int dd_idx;
3703 sector_t new_sector;
3704 sector_t logical_sector, last_sector;
3705 struct stripe_head *sh;
3706 const int rw = bio_data_dir(bi);
3707 int remaining;
3708 int plugged;
3710 if (unlikely(bi->bi_rw & REQ_FLUSH)) {
3711 md_flush_request(mddev, bi);
3712 return 0;
3715 md_write_start(mddev, bi);
3717 if (rw == READ &&
3718 mddev->reshape_position == MaxSector &&
3719 chunk_aligned_read(mddev,bi))
3720 return 0;
3722 logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
3723 last_sector = bi->bi_sector + (bi->bi_size>>9);
3724 bi->bi_next = NULL;
3725 bi->bi_phys_segments = 1; /* over-loaded to count active stripes */
3727 plugged = mddev_check_plugged(mddev);
3728 for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) {
3729 DEFINE_WAIT(w);
3730 int disks, data_disks;
3731 int previous;
3733 retry:
3734 previous = 0;
3735 disks = conf->raid_disks;
3736 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
3737 if (unlikely(conf->reshape_progress != MaxSector)) {
3738 /* spinlock is needed as reshape_progress may be
3739 * 64bit on a 32bit platform, and so it might be
3740 * possible to see a half-updated value
3741 * Of course reshape_progress could change after
3742 * the lock is dropped, so once we get a reference
3743 * to the stripe that we think it is, we will have
3744 * to check again.
3746 spin_lock_irq(&conf->device_lock);
3747 if (mddev->delta_disks < 0
3748 ? logical_sector < conf->reshape_progress
3749 : logical_sector >= conf->reshape_progress) {
3750 disks = conf->previous_raid_disks;
3751 previous = 1;
3752 } else {
3753 if (mddev->delta_disks < 0
3754 ? logical_sector < conf->reshape_safe
3755 : logical_sector >= conf->reshape_safe) {
3756 spin_unlock_irq(&conf->device_lock);
3757 schedule();
3758 goto retry;
3761 spin_unlock_irq(&conf->device_lock);
3763 data_disks = disks - conf->max_degraded;
3765 new_sector = raid5_compute_sector(conf, logical_sector,
3766 previous,
3767 &dd_idx, NULL);
3768 pr_debug("raid456: make_request, sector %llu logical %llu\n",
3769 (unsigned long long)new_sector,
3770 (unsigned long long)logical_sector);
3772 sh = get_active_stripe(conf, new_sector, previous,
3773 (bi->bi_rw&RWA_MASK), 0);
3774 if (sh) {
3775 if (unlikely(previous)) {
3776 /* expansion might have moved on while waiting for a
3777 * stripe, so we must do the range check again.
3778 * Expansion could still move past after this
3779 * test, but as we are holding a reference to
3780 * 'sh', we know that if that happens,
3781 * STRIPE_EXPANDING will get set and the expansion
3782 * won't proceed until we finish with the stripe.
3784 int must_retry = 0;
3785 spin_lock_irq(&conf->device_lock);
3786 if (mddev->delta_disks < 0
3787 ? logical_sector >= conf->reshape_progress
3788 : logical_sector < conf->reshape_progress)
3789 /* mismatch, need to try again */
3790 must_retry = 1;
3791 spin_unlock_irq(&conf->device_lock);
3792 if (must_retry) {
3793 release_stripe(sh);
3794 schedule();
3795 goto retry;
3799 if (rw == WRITE &&
3800 logical_sector >= mddev->suspend_lo &&
3801 logical_sector < mddev->suspend_hi) {
3802 release_stripe(sh);
3803 /* As the suspend_* range is controlled by
3804 * userspace, we want an interruptible
3805 * wait.
3807 flush_signals(current);
3808 prepare_to_wait(&conf->wait_for_overlap,
3809 &w, TASK_INTERRUPTIBLE);
3810 if (logical_sector >= mddev->suspend_lo &&
3811 logical_sector < mddev->suspend_hi)
3812 schedule();
3813 goto retry;
3816 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
3817 !add_stripe_bio(sh, bi, dd_idx, rw)) {
3818 /* Stripe is busy expanding or
3819 * add failed due to overlap. Flush everything
3820 * and wait a while
3822 md_wakeup_thread(mddev->thread);
3823 release_stripe(sh);
3824 schedule();
3825 goto retry;
3827 finish_wait(&conf->wait_for_overlap, &w);
3828 set_bit(STRIPE_HANDLE, &sh->state);
3829 clear_bit(STRIPE_DELAYED, &sh->state);
3830 if ((bi->bi_rw & REQ_SYNC) &&
3831 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3832 atomic_inc(&conf->preread_active_stripes);
3833 release_stripe(sh);
3834 } else {
3835 /* cannot get stripe for read-ahead, just give-up */
3836 clear_bit(BIO_UPTODATE, &bi->bi_flags);
3837 finish_wait(&conf->wait_for_overlap, &w);
3838 break;
3842 if (!plugged)
3843 md_wakeup_thread(mddev->thread);
3845 spin_lock_irq(&conf->device_lock);
3846 remaining = raid5_dec_bi_phys_segments(bi);
3847 spin_unlock_irq(&conf->device_lock);
3848 if (remaining == 0) {
3850 if ( rw == WRITE )
3851 md_write_end(mddev);
3853 bio_endio(bi, 0);
3856 return 0;
3859 static sector_t raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks);
3861 static sector_t reshape_request(mddev_t *mddev, sector_t sector_nr, int *skipped)
3863 /* reshaping is quite different to recovery/resync so it is
3864 * handled quite separately ... here.
3866 * On each call to sync_request, we gather one chunk worth of
3867 * destination stripes and flag them as expanding.
3868 * Then we find all the source stripes and request reads.
3869 * As the reads complete, handle_stripe will copy the data
3870 * into the destination stripe and release that stripe.
3872 raid5_conf_t *conf = mddev->private;
3873 struct stripe_head *sh;
3874 sector_t first_sector, last_sector;
3875 int raid_disks = conf->previous_raid_disks;
3876 int data_disks = raid_disks - conf->max_degraded;
3877 int new_data_disks = conf->raid_disks - conf->max_degraded;
3878 int i;
3879 int dd_idx;
3880 sector_t writepos, readpos, safepos;
3881 sector_t stripe_addr;
3882 int reshape_sectors;
3883 struct list_head stripes;
3885 if (sector_nr == 0) {
3886 /* If restarting in the middle, skip the initial sectors */
3887 if (mddev->delta_disks < 0 &&
3888 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
3889 sector_nr = raid5_size(mddev, 0, 0)
3890 - conf->reshape_progress;
3891 } else if (mddev->delta_disks >= 0 &&
3892 conf->reshape_progress > 0)
3893 sector_nr = conf->reshape_progress;
3894 sector_div(sector_nr, new_data_disks);
3895 if (sector_nr) {
3896 mddev->curr_resync_completed = sector_nr;
3897 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
3898 *skipped = 1;
3899 return sector_nr;
3903 /* We need to process a full chunk at a time.
3904 * If old and new chunk sizes differ, we need to process the
3905 * largest of these
3907 if (mddev->new_chunk_sectors > mddev->chunk_sectors)
3908 reshape_sectors = mddev->new_chunk_sectors;
3909 else
3910 reshape_sectors = mddev->chunk_sectors;
3912 /* we update the metadata when there is more than 3Meg
3913 * in the block range (that is rather arbitrary, should
3914 * probably be time based) or when the data about to be
3915 * copied would over-write the source of the data at
3916 * the front of the range.
3917 * i.e. one new_stripe along from reshape_progress new_maps
3918 * to after where reshape_safe old_maps to
3920 writepos = conf->reshape_progress;
3921 sector_div(writepos, new_data_disks);
3922 readpos = conf->reshape_progress;
3923 sector_div(readpos, data_disks);
3924 safepos = conf->reshape_safe;
3925 sector_div(safepos, data_disks);
3926 if (mddev->delta_disks < 0) {
3927 writepos -= min_t(sector_t, reshape_sectors, writepos);
3928 readpos += reshape_sectors;
3929 safepos += reshape_sectors;
3930 } else {
3931 writepos += reshape_sectors;
3932 readpos -= min_t(sector_t, reshape_sectors, readpos);
3933 safepos -= min_t(sector_t, reshape_sectors, safepos);
3936 /* 'writepos' is the most advanced device address we might write.
3937 * 'readpos' is the least advanced device address we might read.
3938 * 'safepos' is the least address recorded in the metadata as having
3939 * been reshaped.
3940 * If 'readpos' is behind 'writepos', then there is no way that we can
3941 * ensure safety in the face of a crash - that must be done by userspace
3942 * making a backup of the data. So in that case there is no particular
3943 * rush to update metadata.
3944 * Otherwise if 'safepos' is behind 'writepos', then we really need to
3945 * update the metadata to advance 'safepos' to match 'readpos' so that
3946 * we can be safe in the event of a crash.
3947 * So we insist on updating metadata if safepos is behind writepos and
3948 * readpos is beyond writepos.
3949 * In any case, update the metadata every 10 seconds.
3950 * Maybe that number should be configurable, but I'm not sure it is
3951 * worth it.... maybe it could be a multiple of safemode_delay???
3953 if ((mddev->delta_disks < 0
3954 ? (safepos > writepos && readpos < writepos)
3955 : (safepos < writepos && readpos > writepos)) ||
3956 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
3957 /* Cannot proceed until we've updated the superblock... */
3958 wait_event(conf->wait_for_overlap,
3959 atomic_read(&conf->reshape_stripes)==0);
3960 mddev->reshape_position = conf->reshape_progress;
3961 mddev->curr_resync_completed = sector_nr;
3962 conf->reshape_checkpoint = jiffies;
3963 set_bit(MD_CHANGE_DEVS, &mddev->flags);
3964 md_wakeup_thread(mddev->thread);
3965 wait_event(mddev->sb_wait, mddev->flags == 0 ||
3966 kthread_should_stop());
3967 spin_lock_irq(&conf->device_lock);
3968 conf->reshape_safe = mddev->reshape_position;
3969 spin_unlock_irq(&conf->device_lock);
3970 wake_up(&conf->wait_for_overlap);
3971 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
3974 if (mddev->delta_disks < 0) {
3975 BUG_ON(conf->reshape_progress == 0);
3976 stripe_addr = writepos;
3977 BUG_ON((mddev->dev_sectors &
3978 ~((sector_t)reshape_sectors - 1))
3979 - reshape_sectors - stripe_addr
3980 != sector_nr);
3981 } else {
3982 BUG_ON(writepos != sector_nr + reshape_sectors);
3983 stripe_addr = sector_nr;
3985 INIT_LIST_HEAD(&stripes);
3986 for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) {
3987 int j;
3988 int skipped_disk = 0;
3989 sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
3990 set_bit(STRIPE_EXPANDING, &sh->state);
3991 atomic_inc(&conf->reshape_stripes);
3992 /* If any of this stripe is beyond the end of the old
3993 * array, then we need to zero those blocks
3995 for (j=sh->disks; j--;) {
3996 sector_t s;
3997 if (j == sh->pd_idx)
3998 continue;
3999 if (conf->level == 6 &&
4000 j == sh->qd_idx)
4001 continue;
4002 s = compute_blocknr(sh, j, 0);
4003 if (s < raid5_size(mddev, 0, 0)) {
4004 skipped_disk = 1;
4005 continue;
4007 memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE);
4008 set_bit(R5_Expanded, &sh->dev[j].flags);
4009 set_bit(R5_UPTODATE, &sh->dev[j].flags);
4011 if (!skipped_disk) {
4012 set_bit(STRIPE_EXPAND_READY, &sh->state);
4013 set_bit(STRIPE_HANDLE, &sh->state);
4015 list_add(&sh->lru, &stripes);
4017 spin_lock_irq(&conf->device_lock);
4018 if (mddev->delta_disks < 0)
4019 conf->reshape_progress -= reshape_sectors * new_data_disks;
4020 else
4021 conf->reshape_progress += reshape_sectors * new_data_disks;
4022 spin_unlock_irq(&conf->device_lock);
4023 /* Ok, those stripe are ready. We can start scheduling
4024 * reads on the source stripes.
4025 * The source stripes are determined by mapping the first and last
4026 * block on the destination stripes.
4028 first_sector =
4029 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
4030 1, &dd_idx, NULL);
4031 last_sector =
4032 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
4033 * new_data_disks - 1),
4034 1, &dd_idx, NULL);
4035 if (last_sector >= mddev->dev_sectors)
4036 last_sector = mddev->dev_sectors - 1;
4037 while (first_sector <= last_sector) {
4038 sh = get_active_stripe(conf, first_sector, 1, 0, 1);
4039 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4040 set_bit(STRIPE_HANDLE, &sh->state);
4041 release_stripe(sh);
4042 first_sector += STRIPE_SECTORS;
4044 /* Now that the sources are clearly marked, we can release
4045 * the destination stripes
4047 while (!list_empty(&stripes)) {
4048 sh = list_entry(stripes.next, struct stripe_head, lru);
4049 list_del_init(&sh->lru);
4050 release_stripe(sh);
4052 /* If this takes us to the resync_max point where we have to pause,
4053 * then we need to write out the superblock.
4055 sector_nr += reshape_sectors;
4056 if ((sector_nr - mddev->curr_resync_completed) * 2
4057 >= mddev->resync_max - mddev->curr_resync_completed) {
4058 /* Cannot proceed until we've updated the superblock... */
4059 wait_event(conf->wait_for_overlap,
4060 atomic_read(&conf->reshape_stripes) == 0);
4061 mddev->reshape_position = conf->reshape_progress;
4062 mddev->curr_resync_completed = sector_nr;
4063 conf->reshape_checkpoint = jiffies;
4064 set_bit(MD_CHANGE_DEVS, &mddev->flags);
4065 md_wakeup_thread(mddev->thread);
4066 wait_event(mddev->sb_wait,
4067 !test_bit(MD_CHANGE_DEVS, &mddev->flags)
4068 || kthread_should_stop());
4069 spin_lock_irq(&conf->device_lock);
4070 conf->reshape_safe = mddev->reshape_position;
4071 spin_unlock_irq(&conf->device_lock);
4072 wake_up(&conf->wait_for_overlap);
4073 sysfs_notify(&mddev->kobj, NULL, "sync_completed");
4075 return reshape_sectors;
4078 /* FIXME go_faster isn't used */
4079 static inline sector_t sync_request(mddev_t *mddev, sector_t sector_nr, int *skipped, int go_faster)
4081 raid5_conf_t *conf = mddev->private;
4082 struct stripe_head *sh;
4083 sector_t max_sector = mddev->dev_sectors;
4084 sector_t sync_blocks;
4085 int still_degraded = 0;
4086 int i;
4088 if (sector_nr >= max_sector) {
4089 /* just being told to finish up .. nothing much to do */
4091 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
4092 end_reshape(conf);
4093 return 0;
4096 if (mddev->curr_resync < max_sector) /* aborted */
4097 bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
4098 &sync_blocks, 1);
4099 else /* completed sync */
4100 conf->fullsync = 0;
4101 bitmap_close_sync(mddev->bitmap);
4103 return 0;
4106 /* Allow raid5_quiesce to complete */
4107 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
4109 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
4110 return reshape_request(mddev, sector_nr, skipped);
4112 /* No need to check resync_max as we never do more than one
4113 * stripe, and as resync_max will always be on a chunk boundary,
4114 * if the check in md_do_sync didn't fire, there is no chance
4115 * of overstepping resync_max here
4118 /* if there is too many failed drives and we are trying
4119 * to resync, then assert that we are finished, because there is
4120 * nothing we can do.
4122 if (mddev->degraded >= conf->max_degraded &&
4123 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
4124 sector_t rv = mddev->dev_sectors - sector_nr;
4125 *skipped = 1;
4126 return rv;
4128 if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
4129 !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
4130 !conf->fullsync && sync_blocks >= STRIPE_SECTORS) {
4131 /* we can skip this block, and probably more */
4132 sync_blocks /= STRIPE_SECTORS;
4133 *skipped = 1;
4134 return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */
4138 bitmap_cond_end_sync(mddev->bitmap, sector_nr);
4140 sh = get_active_stripe(conf, sector_nr, 0, 1, 0);
4141 if (sh == NULL) {
4142 sh = get_active_stripe(conf, sector_nr, 0, 0, 0);
4143 /* make sure we don't swamp the stripe cache if someone else
4144 * is trying to get access
4146 schedule_timeout_uninterruptible(1);
4148 /* Need to check if array will still be degraded after recovery/resync
4149 * We don't need to check the 'failed' flag as when that gets set,
4150 * recovery aborts.
4152 for (i = 0; i < conf->raid_disks; i++)
4153 if (conf->disks[i].rdev == NULL)
4154 still_degraded = 1;
4156 bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
4158 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
4160 handle_stripe(sh);
4161 release_stripe(sh);
4163 return STRIPE_SECTORS;
4166 static int retry_aligned_read(raid5_conf_t *conf, struct bio *raid_bio)
4168 /* We may not be able to submit a whole bio at once as there
4169 * may not be enough stripe_heads available.
4170 * We cannot pre-allocate enough stripe_heads as we may need
4171 * more than exist in the cache (if we allow ever large chunks).
4172 * So we do one stripe head at a time and record in
4173 * ->bi_hw_segments how many have been done.
4175 * We *know* that this entire raid_bio is in one chunk, so
4176 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
4178 struct stripe_head *sh;
4179 int dd_idx;
4180 sector_t sector, logical_sector, last_sector;
4181 int scnt = 0;
4182 int remaining;
4183 int handled = 0;
4185 logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1);
4186 sector = raid5_compute_sector(conf, logical_sector,
4187 0, &dd_idx, NULL);
4188 last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9);
4190 for (; logical_sector < last_sector;
4191 logical_sector += STRIPE_SECTORS,
4192 sector += STRIPE_SECTORS,
4193 scnt++) {
4195 if (scnt < raid5_bi_hw_segments(raid_bio))
4196 /* already done this stripe */
4197 continue;
4199 sh = get_active_stripe(conf, sector, 0, 1, 0);
4201 if (!sh) {
4202 /* failed to get a stripe - must wait */
4203 raid5_set_bi_hw_segments(raid_bio, scnt);
4204 conf->retry_read_aligned = raid_bio;
4205 return handled;
4208 set_bit(R5_ReadError, &sh->dev[dd_idx].flags);
4209 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) {
4210 release_stripe(sh);
4211 raid5_set_bi_hw_segments(raid_bio, scnt);
4212 conf->retry_read_aligned = raid_bio;
4213 return handled;
4216 handle_stripe(sh);
4217 release_stripe(sh);
4218 handled++;
4220 spin_lock_irq(&conf->device_lock);
4221 remaining = raid5_dec_bi_phys_segments(raid_bio);
4222 spin_unlock_irq(&conf->device_lock);
4223 if (remaining == 0)
4224 bio_endio(raid_bio, 0);
4225 if (atomic_dec_and_test(&conf->active_aligned_reads))
4226 wake_up(&conf->wait_for_stripe);
4227 return handled;
4232 * This is our raid5 kernel thread.
4234 * We scan the hash table for stripes which can be handled now.
4235 * During the scan, completed stripes are saved for us by the interrupt
4236 * handler, so that they will not have to wait for our next wakeup.
4238 static void raid5d(mddev_t *mddev)
4240 struct stripe_head *sh;
4241 raid5_conf_t *conf = mddev->private;
4242 int handled;
4243 struct blk_plug plug;
4245 pr_debug("+++ raid5d active\n");
4247 md_check_recovery(mddev);
4249 blk_start_plug(&plug);
4250 handled = 0;
4251 spin_lock_irq(&conf->device_lock);
4252 while (1) {
4253 struct bio *bio;
4255 if (atomic_read(&mddev->plug_cnt) == 0 &&
4256 !list_empty(&conf->bitmap_list)) {
4257 /* Now is a good time to flush some bitmap updates */
4258 conf->seq_flush++;
4259 spin_unlock_irq(&conf->device_lock);
4260 bitmap_unplug(mddev->bitmap);
4261 spin_lock_irq(&conf->device_lock);
4262 conf->seq_write = conf->seq_flush;
4263 activate_bit_delay(conf);
4265 if (atomic_read(&mddev->plug_cnt) == 0)
4266 raid5_activate_delayed(conf);
4268 while ((bio = remove_bio_from_retry(conf))) {
4269 int ok;
4270 spin_unlock_irq(&conf->device_lock);
4271 ok = retry_aligned_read(conf, bio);
4272 spin_lock_irq(&conf->device_lock);
4273 if (!ok)
4274 break;
4275 handled++;
4278 sh = __get_priority_stripe(conf);
4280 if (!sh)
4281 break;
4282 spin_unlock_irq(&conf->device_lock);
4284 handled++;
4285 handle_stripe(sh);
4286 release_stripe(sh);
4287 cond_resched();
4289 if (mddev->flags & ~(1<<MD_CHANGE_PENDING))
4290 md_check_recovery(mddev);
4292 spin_lock_irq(&conf->device_lock);
4294 pr_debug("%d stripes handled\n", handled);
4296 spin_unlock_irq(&conf->device_lock);
4298 async_tx_issue_pending_all();
4299 blk_finish_plug(&plug);
4301 pr_debug("--- raid5d inactive\n");
4304 static ssize_t
4305 raid5_show_stripe_cache_size(mddev_t *mddev, char *page)
4307 raid5_conf_t *conf = mddev->private;
4308 if (conf)
4309 return sprintf(page, "%d\n", conf->max_nr_stripes);
4310 else
4311 return 0;
4315 raid5_set_cache_size(mddev_t *mddev, int size)
4317 raid5_conf_t *conf = mddev->private;
4318 int err;
4320 if (size <= 16 || size > 32768)
4321 return -EINVAL;
4322 while (size < conf->max_nr_stripes) {
4323 if (drop_one_stripe(conf))
4324 conf->max_nr_stripes--;
4325 else
4326 break;
4328 err = md_allow_write(mddev);
4329 if (err)
4330 return err;
4331 while (size > conf->max_nr_stripes) {
4332 if (grow_one_stripe(conf))
4333 conf->max_nr_stripes++;
4334 else break;
4336 return 0;
4338 EXPORT_SYMBOL(raid5_set_cache_size);
4340 static ssize_t
4341 raid5_store_stripe_cache_size(mddev_t *mddev, const char *page, size_t len)
4343 raid5_conf_t *conf = mddev->private;
4344 unsigned long new;
4345 int err;
4347 if (len >= PAGE_SIZE)
4348 return -EINVAL;
4349 if (!conf)
4350 return -ENODEV;
4352 if (strict_strtoul(page, 10, &new))
4353 return -EINVAL;
4354 err = raid5_set_cache_size(mddev, new);
4355 if (err)
4356 return err;
4357 return len;
4360 static struct md_sysfs_entry
4361 raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
4362 raid5_show_stripe_cache_size,
4363 raid5_store_stripe_cache_size);
4365 static ssize_t
4366 raid5_show_preread_threshold(mddev_t *mddev, char *page)
4368 raid5_conf_t *conf = mddev->private;
4369 if (conf)
4370 return sprintf(page, "%d\n", conf->bypass_threshold);
4371 else
4372 return 0;
4375 static ssize_t
4376 raid5_store_preread_threshold(mddev_t *mddev, const char *page, size_t len)
4378 raid5_conf_t *conf = mddev->private;
4379 unsigned long new;
4380 if (len >= PAGE_SIZE)
4381 return -EINVAL;
4382 if (!conf)
4383 return -ENODEV;
4385 if (strict_strtoul(page, 10, &new))
4386 return -EINVAL;
4387 if (new > conf->max_nr_stripes)
4388 return -EINVAL;
4389 conf->bypass_threshold = new;
4390 return len;
4393 static struct md_sysfs_entry
4394 raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
4395 S_IRUGO | S_IWUSR,
4396 raid5_show_preread_threshold,
4397 raid5_store_preread_threshold);
4399 static ssize_t
4400 stripe_cache_active_show(mddev_t *mddev, char *page)
4402 raid5_conf_t *conf = mddev->private;
4403 if (conf)
4404 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
4405 else
4406 return 0;
4409 static struct md_sysfs_entry
4410 raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
4412 static struct attribute *raid5_attrs[] = {
4413 &raid5_stripecache_size.attr,
4414 &raid5_stripecache_active.attr,
4415 &raid5_preread_bypass_threshold.attr,
4416 NULL,
4418 static struct attribute_group raid5_attrs_group = {
4419 .name = NULL,
4420 .attrs = raid5_attrs,
4423 static sector_t
4424 raid5_size(mddev_t *mddev, sector_t sectors, int raid_disks)
4426 raid5_conf_t *conf = mddev->private;
4428 if (!sectors)
4429 sectors = mddev->dev_sectors;
4430 if (!raid_disks)
4431 /* size is defined by the smallest of previous and new size */
4432 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
4434 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
4435 sectors &= ~((sector_t)mddev->new_chunk_sectors - 1);
4436 return sectors * (raid_disks - conf->max_degraded);
4439 static void raid5_free_percpu(raid5_conf_t *conf)
4441 struct raid5_percpu *percpu;
4442 unsigned long cpu;
4444 if (!conf->percpu)
4445 return;
4447 get_online_cpus();
4448 for_each_possible_cpu(cpu) {
4449 percpu = per_cpu_ptr(conf->percpu, cpu);
4450 safe_put_page(percpu->spare_page);
4451 kfree(percpu->scribble);
4453 #ifdef CONFIG_HOTPLUG_CPU
4454 unregister_cpu_notifier(&conf->cpu_notify);
4455 #endif
4456 put_online_cpus();
4458 free_percpu(conf->percpu);
4461 static void free_conf(raid5_conf_t *conf)
4463 shrink_stripes(conf);
4464 raid5_free_percpu(conf);
4465 kfree(conf->disks);
4466 kfree(conf->stripe_hashtbl);
4467 kfree(conf);
4470 #ifdef CONFIG_HOTPLUG_CPU
4471 static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action,
4472 void *hcpu)
4474 raid5_conf_t *conf = container_of(nfb, raid5_conf_t, cpu_notify);
4475 long cpu = (long)hcpu;
4476 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
4478 switch (action) {
4479 case CPU_UP_PREPARE:
4480 case CPU_UP_PREPARE_FROZEN:
4481 if (conf->level == 6 && !percpu->spare_page)
4482 percpu->spare_page = alloc_page(GFP_KERNEL);
4483 if (!percpu->scribble)
4484 percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4486 if (!percpu->scribble ||
4487 (conf->level == 6 && !percpu->spare_page)) {
4488 safe_put_page(percpu->spare_page);
4489 kfree(percpu->scribble);
4490 pr_err("%s: failed memory allocation for cpu%ld\n",
4491 __func__, cpu);
4492 return notifier_from_errno(-ENOMEM);
4494 break;
4495 case CPU_DEAD:
4496 case CPU_DEAD_FROZEN:
4497 safe_put_page(percpu->spare_page);
4498 kfree(percpu->scribble);
4499 percpu->spare_page = NULL;
4500 percpu->scribble = NULL;
4501 break;
4502 default:
4503 break;
4505 return NOTIFY_OK;
4507 #endif
4509 static int raid5_alloc_percpu(raid5_conf_t *conf)
4511 unsigned long cpu;
4512 struct page *spare_page;
4513 struct raid5_percpu __percpu *allcpus;
4514 void *scribble;
4515 int err;
4517 allcpus = alloc_percpu(struct raid5_percpu);
4518 if (!allcpus)
4519 return -ENOMEM;
4520 conf->percpu = allcpus;
4522 get_online_cpus();
4523 err = 0;
4524 for_each_present_cpu(cpu) {
4525 if (conf->level == 6) {
4526 spare_page = alloc_page(GFP_KERNEL);
4527 if (!spare_page) {
4528 err = -ENOMEM;
4529 break;
4531 per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page;
4533 scribble = kmalloc(conf->scribble_len, GFP_KERNEL);
4534 if (!scribble) {
4535 err = -ENOMEM;
4536 break;
4538 per_cpu_ptr(conf->percpu, cpu)->scribble = scribble;
4540 #ifdef CONFIG_HOTPLUG_CPU
4541 conf->cpu_notify.notifier_call = raid456_cpu_notify;
4542 conf->cpu_notify.priority = 0;
4543 if (err == 0)
4544 err = register_cpu_notifier(&conf->cpu_notify);
4545 #endif
4546 put_online_cpus();
4548 return err;
4551 static raid5_conf_t *setup_conf(mddev_t *mddev)
4553 raid5_conf_t *conf;
4554 int raid_disk, memory, max_disks;
4555 mdk_rdev_t *rdev;
4556 struct disk_info *disk;
4558 if (mddev->new_level != 5
4559 && mddev->new_level != 4
4560 && mddev->new_level != 6) {
4561 printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n",
4562 mdname(mddev), mddev->new_level);
4563 return ERR_PTR(-EIO);
4565 if ((mddev->new_level == 5
4566 && !algorithm_valid_raid5(mddev->new_layout)) ||
4567 (mddev->new_level == 6
4568 && !algorithm_valid_raid6(mddev->new_layout))) {
4569 printk(KERN_ERR "md/raid:%s: layout %d not supported\n",
4570 mdname(mddev), mddev->new_layout);
4571 return ERR_PTR(-EIO);
4573 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
4574 printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n",
4575 mdname(mddev), mddev->raid_disks);
4576 return ERR_PTR(-EINVAL);
4579 if (!mddev->new_chunk_sectors ||
4580 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
4581 !is_power_of_2(mddev->new_chunk_sectors)) {
4582 printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n",
4583 mdname(mddev), mddev->new_chunk_sectors << 9);
4584 return ERR_PTR(-EINVAL);
4587 conf = kzalloc(sizeof(raid5_conf_t), GFP_KERNEL);
4588 if (conf == NULL)
4589 goto abort;
4590 spin_lock_init(&conf->device_lock);
4591 init_waitqueue_head(&conf->wait_for_stripe);
4592 init_waitqueue_head(&conf->wait_for_overlap);
4593 INIT_LIST_HEAD(&conf->handle_list);
4594 INIT_LIST_HEAD(&conf->hold_list);
4595 INIT_LIST_HEAD(&conf->delayed_list);
4596 INIT_LIST_HEAD(&conf->bitmap_list);
4597 INIT_LIST_HEAD(&conf->inactive_list);
4598 atomic_set(&conf->active_stripes, 0);
4599 atomic_set(&conf->preread_active_stripes, 0);
4600 atomic_set(&conf->active_aligned_reads, 0);
4601 conf->bypass_threshold = BYPASS_THRESHOLD;
4603 conf->raid_disks = mddev->raid_disks;
4604 if (mddev->reshape_position == MaxSector)
4605 conf->previous_raid_disks = mddev->raid_disks;
4606 else
4607 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
4608 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
4609 conf->scribble_len = scribble_len(max_disks);
4611 conf->disks = kzalloc(max_disks * sizeof(struct disk_info),
4612 GFP_KERNEL);
4613 if (!conf->disks)
4614 goto abort;
4616 conf->mddev = mddev;
4618 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
4619 goto abort;
4621 conf->level = mddev->new_level;
4622 if (raid5_alloc_percpu(conf) != 0)
4623 goto abort;
4625 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
4627 list_for_each_entry(rdev, &mddev->disks, same_set) {
4628 raid_disk = rdev->raid_disk;
4629 if (raid_disk >= max_disks
4630 || raid_disk < 0)
4631 continue;
4632 disk = conf->disks + raid_disk;
4634 disk->rdev = rdev;
4636 if (test_bit(In_sync, &rdev->flags)) {
4637 char b[BDEVNAME_SIZE];
4638 printk(KERN_INFO "md/raid:%s: device %s operational as raid"
4639 " disk %d\n",
4640 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
4641 } else if (rdev->saved_raid_disk != raid_disk)
4642 /* Cannot rely on bitmap to complete recovery */
4643 conf->fullsync = 1;
4646 conf->chunk_sectors = mddev->new_chunk_sectors;
4647 conf->level = mddev->new_level;
4648 if (conf->level == 6)
4649 conf->max_degraded = 2;
4650 else
4651 conf->max_degraded = 1;
4652 conf->algorithm = mddev->new_layout;
4653 conf->max_nr_stripes = NR_STRIPES;
4654 conf->reshape_progress = mddev->reshape_position;
4655 if (conf->reshape_progress != MaxSector) {
4656 conf->prev_chunk_sectors = mddev->chunk_sectors;
4657 conf->prev_algo = mddev->layout;
4660 memory = conf->max_nr_stripes * (sizeof(struct stripe_head) +
4661 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
4662 if (grow_stripes(conf, conf->max_nr_stripes)) {
4663 printk(KERN_ERR
4664 "md/raid:%s: couldn't allocate %dkB for buffers\n",
4665 mdname(mddev), memory);
4666 goto abort;
4667 } else
4668 printk(KERN_INFO "md/raid:%s: allocated %dkB\n",
4669 mdname(mddev), memory);
4671 conf->thread = md_register_thread(raid5d, mddev, NULL);
4672 if (!conf->thread) {
4673 printk(KERN_ERR
4674 "md/raid:%s: couldn't allocate thread.\n",
4675 mdname(mddev));
4676 goto abort;
4679 return conf;
4681 abort:
4682 if (conf) {
4683 free_conf(conf);
4684 return ERR_PTR(-EIO);
4685 } else
4686 return ERR_PTR(-ENOMEM);
4690 static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
4692 switch (algo) {
4693 case ALGORITHM_PARITY_0:
4694 if (raid_disk < max_degraded)
4695 return 1;
4696 break;
4697 case ALGORITHM_PARITY_N:
4698 if (raid_disk >= raid_disks - max_degraded)
4699 return 1;
4700 break;
4701 case ALGORITHM_PARITY_0_6:
4702 if (raid_disk == 0 ||
4703 raid_disk == raid_disks - 1)
4704 return 1;
4705 break;
4706 case ALGORITHM_LEFT_ASYMMETRIC_6:
4707 case ALGORITHM_RIGHT_ASYMMETRIC_6:
4708 case ALGORITHM_LEFT_SYMMETRIC_6:
4709 case ALGORITHM_RIGHT_SYMMETRIC_6:
4710 if (raid_disk == raid_disks - 1)
4711 return 1;
4713 return 0;
4716 static int run(mddev_t *mddev)
4718 raid5_conf_t *conf;
4719 int working_disks = 0;
4720 int dirty_parity_disks = 0;
4721 mdk_rdev_t *rdev;
4722 sector_t reshape_offset = 0;
4724 if (mddev->recovery_cp != MaxSector)
4725 printk(KERN_NOTICE "md/raid:%s: not clean"
4726 " -- starting background reconstruction\n",
4727 mdname(mddev));
4728 if (mddev->reshape_position != MaxSector) {
4729 /* Check that we can continue the reshape.
4730 * Currently only disks can change, it must
4731 * increase, and we must be past the point where
4732 * a stripe over-writes itself
4734 sector_t here_new, here_old;
4735 int old_disks;
4736 int max_degraded = (mddev->level == 6 ? 2 : 1);
4738 if (mddev->new_level != mddev->level) {
4739 printk(KERN_ERR "md/raid:%s: unsupported reshape "
4740 "required - aborting.\n",
4741 mdname(mddev));
4742 return -EINVAL;
4744 old_disks = mddev->raid_disks - mddev->delta_disks;
4745 /* reshape_position must be on a new-stripe boundary, and one
4746 * further up in new geometry must map after here in old
4747 * geometry.
4749 here_new = mddev->reshape_position;
4750 if (sector_div(here_new, mddev->new_chunk_sectors *
4751 (mddev->raid_disks - max_degraded))) {
4752 printk(KERN_ERR "md/raid:%s: reshape_position not "
4753 "on a stripe boundary\n", mdname(mddev));
4754 return -EINVAL;
4756 reshape_offset = here_new * mddev->new_chunk_sectors;
4757 /* here_new is the stripe we will write to */
4758 here_old = mddev->reshape_position;
4759 sector_div(here_old, mddev->chunk_sectors *
4760 (old_disks-max_degraded));
4761 /* here_old is the first stripe that we might need to read
4762 * from */
4763 if (mddev->delta_disks == 0) {
4764 /* We cannot be sure it is safe to start an in-place
4765 * reshape. It is only safe if user-space if monitoring
4766 * and taking constant backups.
4767 * mdadm always starts a situation like this in
4768 * readonly mode so it can take control before
4769 * allowing any writes. So just check for that.
4771 if ((here_new * mddev->new_chunk_sectors !=
4772 here_old * mddev->chunk_sectors) ||
4773 mddev->ro == 0) {
4774 printk(KERN_ERR "md/raid:%s: in-place reshape must be started"
4775 " in read-only mode - aborting\n",
4776 mdname(mddev));
4777 return -EINVAL;
4779 } else if (mddev->delta_disks < 0
4780 ? (here_new * mddev->new_chunk_sectors <=
4781 here_old * mddev->chunk_sectors)
4782 : (here_new * mddev->new_chunk_sectors >=
4783 here_old * mddev->chunk_sectors)) {
4784 /* Reading from the same stripe as writing to - bad */
4785 printk(KERN_ERR "md/raid:%s: reshape_position too early for "
4786 "auto-recovery - aborting.\n",
4787 mdname(mddev));
4788 return -EINVAL;
4790 printk(KERN_INFO "md/raid:%s: reshape will continue\n",
4791 mdname(mddev));
4792 /* OK, we should be able to continue; */
4793 } else {
4794 BUG_ON(mddev->level != mddev->new_level);
4795 BUG_ON(mddev->layout != mddev->new_layout);
4796 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
4797 BUG_ON(mddev->delta_disks != 0);
4800 if (mddev->private == NULL)
4801 conf = setup_conf(mddev);
4802 else
4803 conf = mddev->private;
4805 if (IS_ERR(conf))
4806 return PTR_ERR(conf);
4808 mddev->thread = conf->thread;
4809 conf->thread = NULL;
4810 mddev->private = conf;
4813 * 0 for a fully functional array, 1 or 2 for a degraded array.
4815 list_for_each_entry(rdev, &mddev->disks, same_set) {
4816 if (rdev->raid_disk < 0)
4817 continue;
4818 if (test_bit(In_sync, &rdev->flags)) {
4819 working_disks++;
4820 continue;
4822 /* This disc is not fully in-sync. However if it
4823 * just stored parity (beyond the recovery_offset),
4824 * when we don't need to be concerned about the
4825 * array being dirty.
4826 * When reshape goes 'backwards', we never have
4827 * partially completed devices, so we only need
4828 * to worry about reshape going forwards.
4830 /* Hack because v0.91 doesn't store recovery_offset properly. */
4831 if (mddev->major_version == 0 &&
4832 mddev->minor_version > 90)
4833 rdev->recovery_offset = reshape_offset;
4835 if (rdev->recovery_offset < reshape_offset) {
4836 /* We need to check old and new layout */
4837 if (!only_parity(rdev->raid_disk,
4838 conf->algorithm,
4839 conf->raid_disks,
4840 conf->max_degraded))
4841 continue;
4843 if (!only_parity(rdev->raid_disk,
4844 conf->prev_algo,
4845 conf->previous_raid_disks,
4846 conf->max_degraded))
4847 continue;
4848 dirty_parity_disks++;
4851 mddev->degraded = (max(conf->raid_disks, conf->previous_raid_disks)
4852 - working_disks);
4854 if (has_failed(conf)) {
4855 printk(KERN_ERR "md/raid:%s: not enough operational devices"
4856 " (%d/%d failed)\n",
4857 mdname(mddev), mddev->degraded, conf->raid_disks);
4858 goto abort;
4861 /* device size must be a multiple of chunk size */
4862 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
4863 mddev->resync_max_sectors = mddev->dev_sectors;
4865 if (mddev->degraded > dirty_parity_disks &&
4866 mddev->recovery_cp != MaxSector) {
4867 if (mddev->ok_start_degraded)
4868 printk(KERN_WARNING
4869 "md/raid:%s: starting dirty degraded array"
4870 " - data corruption possible.\n",
4871 mdname(mddev));
4872 else {
4873 printk(KERN_ERR
4874 "md/raid:%s: cannot start dirty degraded array.\n",
4875 mdname(mddev));
4876 goto abort;
4880 if (mddev->degraded == 0)
4881 printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d"
4882 " devices, algorithm %d\n", mdname(mddev), conf->level,
4883 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
4884 mddev->new_layout);
4885 else
4886 printk(KERN_ALERT "md/raid:%s: raid level %d active with %d"
4887 " out of %d devices, algorithm %d\n",
4888 mdname(mddev), conf->level,
4889 mddev->raid_disks - mddev->degraded,
4890 mddev->raid_disks, mddev->new_layout);
4892 print_raid5_conf(conf);
4894 if (conf->reshape_progress != MaxSector) {
4895 conf->reshape_safe = conf->reshape_progress;
4896 atomic_set(&conf->reshape_stripes, 0);
4897 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
4898 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
4899 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
4900 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
4901 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
4902 "reshape");
4906 /* Ok, everything is just fine now */
4907 if (mddev->to_remove == &raid5_attrs_group)
4908 mddev->to_remove = NULL;
4909 else if (mddev->kobj.sd &&
4910 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
4911 printk(KERN_WARNING
4912 "raid5: failed to create sysfs attributes for %s\n",
4913 mdname(mddev));
4914 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
4916 if (mddev->queue) {
4917 int chunk_size;
4918 /* read-ahead size must cover two whole stripes, which
4919 * is 2 * (datadisks) * chunksize where 'n' is the
4920 * number of raid devices
4922 int data_disks = conf->previous_raid_disks - conf->max_degraded;
4923 int stripe = data_disks *
4924 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
4925 if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
4926 mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
4928 blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec);
4930 mddev->queue->backing_dev_info.congested_data = mddev;
4931 mddev->queue->backing_dev_info.congested_fn = raid5_congested;
4933 chunk_size = mddev->chunk_sectors << 9;
4934 blk_queue_io_min(mddev->queue, chunk_size);
4935 blk_queue_io_opt(mddev->queue, chunk_size *
4936 (conf->raid_disks - conf->max_degraded));
4938 list_for_each_entry(rdev, &mddev->disks, same_set)
4939 disk_stack_limits(mddev->gendisk, rdev->bdev,
4940 rdev->data_offset << 9);
4943 return 0;
4944 abort:
4945 md_unregister_thread(mddev->thread);
4946 mddev->thread = NULL;
4947 if (conf) {
4948 print_raid5_conf(conf);
4949 free_conf(conf);
4951 mddev->private = NULL;
4952 printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev));
4953 return -EIO;
4956 static int stop(mddev_t *mddev)
4958 raid5_conf_t *conf = mddev->private;
4960 md_unregister_thread(mddev->thread);
4961 mddev->thread = NULL;
4962 if (mddev->queue)
4963 mddev->queue->backing_dev_info.congested_fn = NULL;
4964 free_conf(conf);
4965 mddev->private = NULL;
4966 mddev->to_remove = &raid5_attrs_group;
4967 return 0;
4970 #ifdef DEBUG
4971 static void print_sh(struct seq_file *seq, struct stripe_head *sh)
4973 int i;
4975 seq_printf(seq, "sh %llu, pd_idx %d, state %ld.\n",
4976 (unsigned long long)sh->sector, sh->pd_idx, sh->state);
4977 seq_printf(seq, "sh %llu, count %d.\n",
4978 (unsigned long long)sh->sector, atomic_read(&sh->count));
4979 seq_printf(seq, "sh %llu, ", (unsigned long long)sh->sector);
4980 for (i = 0; i < sh->disks; i++) {
4981 seq_printf(seq, "(cache%d: %p %ld) ",
4982 i, sh->dev[i].page, sh->dev[i].flags);
4984 seq_printf(seq, "\n");
4987 static void printall(struct seq_file *seq, raid5_conf_t *conf)
4989 struct stripe_head *sh;
4990 struct hlist_node *hn;
4991 int i;
4993 spin_lock_irq(&conf->device_lock);
4994 for (i = 0; i < NR_HASH; i++) {
4995 hlist_for_each_entry(sh, hn, &conf->stripe_hashtbl[i], hash) {
4996 if (sh->raid_conf != conf)
4997 continue;
4998 print_sh(seq, sh);
5001 spin_unlock_irq(&conf->device_lock);
5003 #endif
5005 static void status(struct seq_file *seq, mddev_t *mddev)
5007 raid5_conf_t *conf = mddev->private;
5008 int i;
5010 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
5011 mddev->chunk_sectors / 2, mddev->layout);
5012 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
5013 for (i = 0; i < conf->raid_disks; i++)
5014 seq_printf (seq, "%s",
5015 conf->disks[i].rdev &&
5016 test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_");
5017 seq_printf (seq, "]");
5018 #ifdef DEBUG
5019 seq_printf (seq, "\n");
5020 printall(seq, conf);
5021 #endif
5024 static void print_raid5_conf (raid5_conf_t *conf)
5026 int i;
5027 struct disk_info *tmp;
5029 printk(KERN_DEBUG "RAID conf printout:\n");
5030 if (!conf) {
5031 printk("(conf==NULL)\n");
5032 return;
5034 printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level,
5035 conf->raid_disks,
5036 conf->raid_disks - conf->mddev->degraded);
5038 for (i = 0; i < conf->raid_disks; i++) {
5039 char b[BDEVNAME_SIZE];
5040 tmp = conf->disks + i;
5041 if (tmp->rdev)
5042 printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n",
5043 i, !test_bit(Faulty, &tmp->rdev->flags),
5044 bdevname(tmp->rdev->bdev, b));
5048 static int raid5_spare_active(mddev_t *mddev)
5050 int i;
5051 raid5_conf_t *conf = mddev->private;
5052 struct disk_info *tmp;
5053 int count = 0;
5054 unsigned long flags;
5056 for (i = 0; i < conf->raid_disks; i++) {
5057 tmp = conf->disks + i;
5058 if (tmp->rdev
5059 && tmp->rdev->recovery_offset == MaxSector
5060 && !test_bit(Faulty, &tmp->rdev->flags)
5061 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
5062 count++;
5063 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
5066 spin_lock_irqsave(&conf->device_lock, flags);
5067 mddev->degraded -= count;
5068 spin_unlock_irqrestore(&conf->device_lock, flags);
5069 print_raid5_conf(conf);
5070 return count;
5073 static int raid5_remove_disk(mddev_t *mddev, int number)
5075 raid5_conf_t *conf = mddev->private;
5076 int err = 0;
5077 mdk_rdev_t *rdev;
5078 struct disk_info *p = conf->disks + number;
5080 print_raid5_conf(conf);
5081 rdev = p->rdev;
5082 if (rdev) {
5083 if (number >= conf->raid_disks &&
5084 conf->reshape_progress == MaxSector)
5085 clear_bit(In_sync, &rdev->flags);
5087 if (test_bit(In_sync, &rdev->flags) ||
5088 atomic_read(&rdev->nr_pending)) {
5089 err = -EBUSY;
5090 goto abort;
5092 /* Only remove non-faulty devices if recovery
5093 * isn't possible.
5095 if (!test_bit(Faulty, &rdev->flags) &&
5096 mddev->recovery_disabled != conf->recovery_disabled &&
5097 !has_failed(conf) &&
5098 number < conf->raid_disks) {
5099 err = -EBUSY;
5100 goto abort;
5102 p->rdev = NULL;
5103 synchronize_rcu();
5104 if (atomic_read(&rdev->nr_pending)) {
5105 /* lost the race, try later */
5106 err = -EBUSY;
5107 p->rdev = rdev;
5110 abort:
5112 print_raid5_conf(conf);
5113 return err;
5116 static int raid5_add_disk(mddev_t *mddev, mdk_rdev_t *rdev)
5118 raid5_conf_t *conf = mddev->private;
5119 int err = -EEXIST;
5120 int disk;
5121 struct disk_info *p;
5122 int first = 0;
5123 int last = conf->raid_disks - 1;
5125 if (mddev->recovery_disabled == conf->recovery_disabled)
5126 return -EBUSY;
5128 if (has_failed(conf))
5129 /* no point adding a device */
5130 return -EINVAL;
5132 if (rdev->raid_disk >= 0)
5133 first = last = rdev->raid_disk;
5136 * find the disk ... but prefer rdev->saved_raid_disk
5137 * if possible.
5139 if (rdev->saved_raid_disk >= 0 &&
5140 rdev->saved_raid_disk >= first &&
5141 conf->disks[rdev->saved_raid_disk].rdev == NULL)
5142 disk = rdev->saved_raid_disk;
5143 else
5144 disk = first;
5145 for ( ; disk <= last ; disk++)
5146 if ((p=conf->disks + disk)->rdev == NULL) {
5147 clear_bit(In_sync, &rdev->flags);
5148 rdev->raid_disk = disk;
5149 err = 0;
5150 if (rdev->saved_raid_disk != disk)
5151 conf->fullsync = 1;
5152 rcu_assign_pointer(p->rdev, rdev);
5153 break;
5155 print_raid5_conf(conf);
5156 return err;
5159 static int raid5_resize(mddev_t *mddev, sector_t sectors)
5161 /* no resync is happening, and there is enough space
5162 * on all devices, so we can resize.
5163 * We need to make sure resync covers any new space.
5164 * If the array is shrinking we should possibly wait until
5165 * any io in the removed space completes, but it hardly seems
5166 * worth it.
5168 sectors &= ~((sector_t)mddev->chunk_sectors - 1);
5169 md_set_array_sectors(mddev, raid5_size(mddev, sectors,
5170 mddev->raid_disks));
5171 if (mddev->array_sectors >
5172 raid5_size(mddev, sectors, mddev->raid_disks))
5173 return -EINVAL;
5174 set_capacity(mddev->gendisk, mddev->array_sectors);
5175 revalidate_disk(mddev->gendisk);
5176 if (sectors > mddev->dev_sectors &&
5177 mddev->recovery_cp > mddev->dev_sectors) {
5178 mddev->recovery_cp = mddev->dev_sectors;
5179 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
5181 mddev->dev_sectors = sectors;
5182 mddev->resync_max_sectors = sectors;
5183 return 0;
5186 static int check_stripe_cache(mddev_t *mddev)
5188 /* Can only proceed if there are plenty of stripe_heads.
5189 * We need a minimum of one full stripe,, and for sensible progress
5190 * it is best to have about 4 times that.
5191 * If we require 4 times, then the default 256 4K stripe_heads will
5192 * allow for chunk sizes up to 256K, which is probably OK.
5193 * If the chunk size is greater, user-space should request more
5194 * stripe_heads first.
5196 raid5_conf_t *conf = mddev->private;
5197 if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4
5198 > conf->max_nr_stripes ||
5199 ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4
5200 > conf->max_nr_stripes) {
5201 printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n",
5202 mdname(mddev),
5203 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
5204 / STRIPE_SIZE)*4);
5205 return 0;
5207 return 1;
5210 static int check_reshape(mddev_t *mddev)
5212 raid5_conf_t *conf = mddev->private;
5214 if (mddev->delta_disks == 0 &&
5215 mddev->new_layout == mddev->layout &&
5216 mddev->new_chunk_sectors == mddev->chunk_sectors)
5217 return 0; /* nothing to do */
5218 if (mddev->bitmap)
5219 /* Cannot grow a bitmap yet */
5220 return -EBUSY;
5221 if (has_failed(conf))
5222 return -EINVAL;
5223 if (mddev->delta_disks < 0) {
5224 /* We might be able to shrink, but the devices must
5225 * be made bigger first.
5226 * For raid6, 4 is the minimum size.
5227 * Otherwise 2 is the minimum
5229 int min = 2;
5230 if (mddev->level == 6)
5231 min = 4;
5232 if (mddev->raid_disks + mddev->delta_disks < min)
5233 return -EINVAL;
5236 if (!check_stripe_cache(mddev))
5237 return -ENOSPC;
5239 return resize_stripes(conf, conf->raid_disks + mddev->delta_disks);
5242 static int raid5_start_reshape(mddev_t *mddev)
5244 raid5_conf_t *conf = mddev->private;
5245 mdk_rdev_t *rdev;
5246 int spares = 0;
5247 unsigned long flags;
5249 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
5250 return -EBUSY;
5252 if (!check_stripe_cache(mddev))
5253 return -ENOSPC;
5255 list_for_each_entry(rdev, &mddev->disks, same_set)
5256 if (!test_bit(In_sync, &rdev->flags)
5257 && !test_bit(Faulty, &rdev->flags))
5258 spares++;
5260 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
5261 /* Not enough devices even to make a degraded array
5262 * of that size
5264 return -EINVAL;
5266 /* Refuse to reduce size of the array. Any reductions in
5267 * array size must be through explicit setting of array_size
5268 * attribute.
5270 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
5271 < mddev->array_sectors) {
5272 printk(KERN_ERR "md/raid:%s: array size must be reduced "
5273 "before number of disks\n", mdname(mddev));
5274 return -EINVAL;
5277 atomic_set(&conf->reshape_stripes, 0);
5278 spin_lock_irq(&conf->device_lock);
5279 conf->previous_raid_disks = conf->raid_disks;
5280 conf->raid_disks += mddev->delta_disks;
5281 conf->prev_chunk_sectors = conf->chunk_sectors;
5282 conf->chunk_sectors = mddev->new_chunk_sectors;
5283 conf->prev_algo = conf->algorithm;
5284 conf->algorithm = mddev->new_layout;
5285 if (mddev->delta_disks < 0)
5286 conf->reshape_progress = raid5_size(mddev, 0, 0);
5287 else
5288 conf->reshape_progress = 0;
5289 conf->reshape_safe = conf->reshape_progress;
5290 conf->generation++;
5291 spin_unlock_irq(&conf->device_lock);
5293 /* Add some new drives, as many as will fit.
5294 * We know there are enough to make the newly sized array work.
5295 * Don't add devices if we are reducing the number of
5296 * devices in the array. This is because it is not possible
5297 * to correctly record the "partially reconstructed" state of
5298 * such devices during the reshape and confusion could result.
5300 if (mddev->delta_disks >= 0) {
5301 int added_devices = 0;
5302 list_for_each_entry(rdev, &mddev->disks, same_set)
5303 if (rdev->raid_disk < 0 &&
5304 !test_bit(Faulty, &rdev->flags)) {
5305 if (raid5_add_disk(mddev, rdev) == 0) {
5306 if (rdev->raid_disk
5307 >= conf->previous_raid_disks) {
5308 set_bit(In_sync, &rdev->flags);
5309 added_devices++;
5310 } else
5311 rdev->recovery_offset = 0;
5313 if (sysfs_link_rdev(mddev, rdev))
5314 /* Failure here is OK */;
5316 } else if (rdev->raid_disk >= conf->previous_raid_disks
5317 && !test_bit(Faulty, &rdev->flags)) {
5318 /* This is a spare that was manually added */
5319 set_bit(In_sync, &rdev->flags);
5320 added_devices++;
5323 /* When a reshape changes the number of devices,
5324 * ->degraded is measured against the larger of the
5325 * pre and post number of devices.
5327 spin_lock_irqsave(&conf->device_lock, flags);
5328 mddev->degraded += (conf->raid_disks - conf->previous_raid_disks)
5329 - added_devices;
5330 spin_unlock_irqrestore(&conf->device_lock, flags);
5332 mddev->raid_disks = conf->raid_disks;
5333 mddev->reshape_position = conf->reshape_progress;
5334 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5336 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
5337 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
5338 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
5339 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
5340 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
5341 "reshape");
5342 if (!mddev->sync_thread) {
5343 mddev->recovery = 0;
5344 spin_lock_irq(&conf->device_lock);
5345 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
5346 conf->reshape_progress = MaxSector;
5347 spin_unlock_irq(&conf->device_lock);
5348 return -EAGAIN;
5350 conf->reshape_checkpoint = jiffies;
5351 md_wakeup_thread(mddev->sync_thread);
5352 md_new_event(mddev);
5353 return 0;
5356 /* This is called from the reshape thread and should make any
5357 * changes needed in 'conf'
5359 static void end_reshape(raid5_conf_t *conf)
5362 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
5364 spin_lock_irq(&conf->device_lock);
5365 conf->previous_raid_disks = conf->raid_disks;
5366 conf->reshape_progress = MaxSector;
5367 spin_unlock_irq(&conf->device_lock);
5368 wake_up(&conf->wait_for_overlap);
5370 /* read-ahead size must cover two whole stripes, which is
5371 * 2 * (datadisks) * chunksize where 'n' is the number of raid devices
5373 if (conf->mddev->queue) {
5374 int data_disks = conf->raid_disks - conf->max_degraded;
5375 int stripe = data_disks * ((conf->chunk_sectors << 9)
5376 / PAGE_SIZE);
5377 if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe)
5378 conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe;
5383 /* This is called from the raid5d thread with mddev_lock held.
5384 * It makes config changes to the device.
5386 static void raid5_finish_reshape(mddev_t *mddev)
5388 raid5_conf_t *conf = mddev->private;
5390 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
5392 if (mddev->delta_disks > 0) {
5393 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
5394 set_capacity(mddev->gendisk, mddev->array_sectors);
5395 revalidate_disk(mddev->gendisk);
5396 } else {
5397 int d;
5398 mddev->degraded = conf->raid_disks;
5399 for (d = 0; d < conf->raid_disks ; d++)
5400 if (conf->disks[d].rdev &&
5401 test_bit(In_sync,
5402 &conf->disks[d].rdev->flags))
5403 mddev->degraded--;
5404 for (d = conf->raid_disks ;
5405 d < conf->raid_disks - mddev->delta_disks;
5406 d++) {
5407 mdk_rdev_t *rdev = conf->disks[d].rdev;
5408 if (rdev && raid5_remove_disk(mddev, d) == 0) {
5409 sysfs_unlink_rdev(mddev, rdev);
5410 rdev->raid_disk = -1;
5414 mddev->layout = conf->algorithm;
5415 mddev->chunk_sectors = conf->chunk_sectors;
5416 mddev->reshape_position = MaxSector;
5417 mddev->delta_disks = 0;
5421 static void raid5_quiesce(mddev_t *mddev, int state)
5423 raid5_conf_t *conf = mddev->private;
5425 switch(state) {
5426 case 2: /* resume for a suspend */
5427 wake_up(&conf->wait_for_overlap);
5428 break;
5430 case 1: /* stop all writes */
5431 spin_lock_irq(&conf->device_lock);
5432 /* '2' tells resync/reshape to pause so that all
5433 * active stripes can drain
5435 conf->quiesce = 2;
5436 wait_event_lock_irq(conf->wait_for_stripe,
5437 atomic_read(&conf->active_stripes) == 0 &&
5438 atomic_read(&conf->active_aligned_reads) == 0,
5439 conf->device_lock, /* nothing */);
5440 conf->quiesce = 1;
5441 spin_unlock_irq(&conf->device_lock);
5442 /* allow reshape to continue */
5443 wake_up(&conf->wait_for_overlap);
5444 break;
5446 case 0: /* re-enable writes */
5447 spin_lock_irq(&conf->device_lock);
5448 conf->quiesce = 0;
5449 wake_up(&conf->wait_for_stripe);
5450 wake_up(&conf->wait_for_overlap);
5451 spin_unlock_irq(&conf->device_lock);
5452 break;
5457 static void *raid45_takeover_raid0(mddev_t *mddev, int level)
5459 struct raid0_private_data *raid0_priv = mddev->private;
5460 sector_t sectors;
5462 /* for raid0 takeover only one zone is supported */
5463 if (raid0_priv->nr_strip_zones > 1) {
5464 printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n",
5465 mdname(mddev));
5466 return ERR_PTR(-EINVAL);
5469 sectors = raid0_priv->strip_zone[0].zone_end;
5470 sector_div(sectors, raid0_priv->strip_zone[0].nb_dev);
5471 mddev->dev_sectors = sectors;
5472 mddev->new_level = level;
5473 mddev->new_layout = ALGORITHM_PARITY_N;
5474 mddev->new_chunk_sectors = mddev->chunk_sectors;
5475 mddev->raid_disks += 1;
5476 mddev->delta_disks = 1;
5477 /* make sure it will be not marked as dirty */
5478 mddev->recovery_cp = MaxSector;
5480 return setup_conf(mddev);
5484 static void *raid5_takeover_raid1(mddev_t *mddev)
5486 int chunksect;
5488 if (mddev->raid_disks != 2 ||
5489 mddev->degraded > 1)
5490 return ERR_PTR(-EINVAL);
5492 /* Should check if there are write-behind devices? */
5494 chunksect = 64*2; /* 64K by default */
5496 /* The array must be an exact multiple of chunksize */
5497 while (chunksect && (mddev->array_sectors & (chunksect-1)))
5498 chunksect >>= 1;
5500 if ((chunksect<<9) < STRIPE_SIZE)
5501 /* array size does not allow a suitable chunk size */
5502 return ERR_PTR(-EINVAL);
5504 mddev->new_level = 5;
5505 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
5506 mddev->new_chunk_sectors = chunksect;
5508 return setup_conf(mddev);
5511 static void *raid5_takeover_raid6(mddev_t *mddev)
5513 int new_layout;
5515 switch (mddev->layout) {
5516 case ALGORITHM_LEFT_ASYMMETRIC_6:
5517 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
5518 break;
5519 case ALGORITHM_RIGHT_ASYMMETRIC_6:
5520 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
5521 break;
5522 case ALGORITHM_LEFT_SYMMETRIC_6:
5523 new_layout = ALGORITHM_LEFT_SYMMETRIC;
5524 break;
5525 case ALGORITHM_RIGHT_SYMMETRIC_6:
5526 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
5527 break;
5528 case ALGORITHM_PARITY_0_6:
5529 new_layout = ALGORITHM_PARITY_0;
5530 break;
5531 case ALGORITHM_PARITY_N:
5532 new_layout = ALGORITHM_PARITY_N;
5533 break;
5534 default:
5535 return ERR_PTR(-EINVAL);
5537 mddev->new_level = 5;
5538 mddev->new_layout = new_layout;
5539 mddev->delta_disks = -1;
5540 mddev->raid_disks -= 1;
5541 return setup_conf(mddev);
5545 static int raid5_check_reshape(mddev_t *mddev)
5547 /* For a 2-drive array, the layout and chunk size can be changed
5548 * immediately as not restriping is needed.
5549 * For larger arrays we record the new value - after validation
5550 * to be used by a reshape pass.
5552 raid5_conf_t *conf = mddev->private;
5553 int new_chunk = mddev->new_chunk_sectors;
5555 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
5556 return -EINVAL;
5557 if (new_chunk > 0) {
5558 if (!is_power_of_2(new_chunk))
5559 return -EINVAL;
5560 if (new_chunk < (PAGE_SIZE>>9))
5561 return -EINVAL;
5562 if (mddev->array_sectors & (new_chunk-1))
5563 /* not factor of array size */
5564 return -EINVAL;
5567 /* They look valid */
5569 if (mddev->raid_disks == 2) {
5570 /* can make the change immediately */
5571 if (mddev->new_layout >= 0) {
5572 conf->algorithm = mddev->new_layout;
5573 mddev->layout = mddev->new_layout;
5575 if (new_chunk > 0) {
5576 conf->chunk_sectors = new_chunk ;
5577 mddev->chunk_sectors = new_chunk;
5579 set_bit(MD_CHANGE_DEVS, &mddev->flags);
5580 md_wakeup_thread(mddev->thread);
5582 return check_reshape(mddev);
5585 static int raid6_check_reshape(mddev_t *mddev)
5587 int new_chunk = mddev->new_chunk_sectors;
5589 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
5590 return -EINVAL;
5591 if (new_chunk > 0) {
5592 if (!is_power_of_2(new_chunk))
5593 return -EINVAL;
5594 if (new_chunk < (PAGE_SIZE >> 9))
5595 return -EINVAL;
5596 if (mddev->array_sectors & (new_chunk-1))
5597 /* not factor of array size */
5598 return -EINVAL;
5601 /* They look valid */
5602 return check_reshape(mddev);
5605 static void *raid5_takeover(mddev_t *mddev)
5607 /* raid5 can take over:
5608 * raid0 - if there is only one strip zone - make it a raid4 layout
5609 * raid1 - if there are two drives. We need to know the chunk size
5610 * raid4 - trivial - just use a raid4 layout.
5611 * raid6 - Providing it is a *_6 layout
5613 if (mddev->level == 0)
5614 return raid45_takeover_raid0(mddev, 5);
5615 if (mddev->level == 1)
5616 return raid5_takeover_raid1(mddev);
5617 if (mddev->level == 4) {
5618 mddev->new_layout = ALGORITHM_PARITY_N;
5619 mddev->new_level = 5;
5620 return setup_conf(mddev);
5622 if (mddev->level == 6)
5623 return raid5_takeover_raid6(mddev);
5625 return ERR_PTR(-EINVAL);
5628 static void *raid4_takeover(mddev_t *mddev)
5630 /* raid4 can take over:
5631 * raid0 - if there is only one strip zone
5632 * raid5 - if layout is right
5634 if (mddev->level == 0)
5635 return raid45_takeover_raid0(mddev, 4);
5636 if (mddev->level == 5 &&
5637 mddev->layout == ALGORITHM_PARITY_N) {
5638 mddev->new_layout = 0;
5639 mddev->new_level = 4;
5640 return setup_conf(mddev);
5642 return ERR_PTR(-EINVAL);
5645 static struct mdk_personality raid5_personality;
5647 static void *raid6_takeover(mddev_t *mddev)
5649 /* Currently can only take over a raid5. We map the
5650 * personality to an equivalent raid6 personality
5651 * with the Q block at the end.
5653 int new_layout;
5655 if (mddev->pers != &raid5_personality)
5656 return ERR_PTR(-EINVAL);
5657 if (mddev->degraded > 1)
5658 return ERR_PTR(-EINVAL);
5659 if (mddev->raid_disks > 253)
5660 return ERR_PTR(-EINVAL);
5661 if (mddev->raid_disks < 3)
5662 return ERR_PTR(-EINVAL);
5664 switch (mddev->layout) {
5665 case ALGORITHM_LEFT_ASYMMETRIC:
5666 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
5667 break;
5668 case ALGORITHM_RIGHT_ASYMMETRIC:
5669 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
5670 break;
5671 case ALGORITHM_LEFT_SYMMETRIC:
5672 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
5673 break;
5674 case ALGORITHM_RIGHT_SYMMETRIC:
5675 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
5676 break;
5677 case ALGORITHM_PARITY_0:
5678 new_layout = ALGORITHM_PARITY_0_6;
5679 break;
5680 case ALGORITHM_PARITY_N:
5681 new_layout = ALGORITHM_PARITY_N;
5682 break;
5683 default:
5684 return ERR_PTR(-EINVAL);
5686 mddev->new_level = 6;
5687 mddev->new_layout = new_layout;
5688 mddev->delta_disks = 1;
5689 mddev->raid_disks += 1;
5690 return setup_conf(mddev);
5694 static struct mdk_personality raid6_personality =
5696 .name = "raid6",
5697 .level = 6,
5698 .owner = THIS_MODULE,
5699 .make_request = make_request,
5700 .run = run,
5701 .stop = stop,
5702 .status = status,
5703 .error_handler = error,
5704 .hot_add_disk = raid5_add_disk,
5705 .hot_remove_disk= raid5_remove_disk,
5706 .spare_active = raid5_spare_active,
5707 .sync_request = sync_request,
5708 .resize = raid5_resize,
5709 .size = raid5_size,
5710 .check_reshape = raid6_check_reshape,
5711 .start_reshape = raid5_start_reshape,
5712 .finish_reshape = raid5_finish_reshape,
5713 .quiesce = raid5_quiesce,
5714 .takeover = raid6_takeover,
5716 static struct mdk_personality raid5_personality =
5718 .name = "raid5",
5719 .level = 5,
5720 .owner = THIS_MODULE,
5721 .make_request = make_request,
5722 .run = run,
5723 .stop = stop,
5724 .status = status,
5725 .error_handler = error,
5726 .hot_add_disk = raid5_add_disk,
5727 .hot_remove_disk= raid5_remove_disk,
5728 .spare_active = raid5_spare_active,
5729 .sync_request = sync_request,
5730 .resize = raid5_resize,
5731 .size = raid5_size,
5732 .check_reshape = raid5_check_reshape,
5733 .start_reshape = raid5_start_reshape,
5734 .finish_reshape = raid5_finish_reshape,
5735 .quiesce = raid5_quiesce,
5736 .takeover = raid5_takeover,
5739 static struct mdk_personality raid4_personality =
5741 .name = "raid4",
5742 .level = 4,
5743 .owner = THIS_MODULE,
5744 .make_request = make_request,
5745 .run = run,
5746 .stop = stop,
5747 .status = status,
5748 .error_handler = error,
5749 .hot_add_disk = raid5_add_disk,
5750 .hot_remove_disk= raid5_remove_disk,
5751 .spare_active = raid5_spare_active,
5752 .sync_request = sync_request,
5753 .resize = raid5_resize,
5754 .size = raid5_size,
5755 .check_reshape = raid5_check_reshape,
5756 .start_reshape = raid5_start_reshape,
5757 .finish_reshape = raid5_finish_reshape,
5758 .quiesce = raid5_quiesce,
5759 .takeover = raid4_takeover,
5762 static int __init raid5_init(void)
5764 register_md_personality(&raid6_personality);
5765 register_md_personality(&raid5_personality);
5766 register_md_personality(&raid4_personality);
5767 return 0;
5770 static void raid5_exit(void)
5772 unregister_md_personality(&raid6_personality);
5773 unregister_md_personality(&raid5_personality);
5774 unregister_md_personality(&raid4_personality);
5777 module_init(raid5_init);
5778 module_exit(raid5_exit);
5779 MODULE_LICENSE("GPL");
5780 MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
5781 MODULE_ALIAS("md-personality-4"); /* RAID5 */
5782 MODULE_ALIAS("md-raid5");
5783 MODULE_ALIAS("md-raid4");
5784 MODULE_ALIAS("md-level-5");
5785 MODULE_ALIAS("md-level-4");
5786 MODULE_ALIAS("md-personality-8"); /* RAID6 */
5787 MODULE_ALIAS("md-raid6");
5788 MODULE_ALIAS("md-level-6");
5790 /* This used to be two separate modules, they were: */
5791 MODULE_ALIAS("raid5");
5792 MODULE_ALIAS("raid6");