Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5 | * Copyright (C) 2002, 2003 H. Peter Anvin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 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! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 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. |
| 15 | * |
| 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. |
| 19 | */ |
| 20 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 21 | /* |
| 22 | * BITMAP UNPLUGGING: |
| 23 | * |
| 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. |
| 27 | * |
| 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. |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 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 |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 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 |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 35 | * the number of the batch it will be in. This is seq_flush+1. |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 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. |
| 44 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 46 | #include <linux/blkdev.h> |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 47 | #include <linux/kthread.h> |
Dan Williams | f701d58 | 2009-03-31 15:09:39 +1100 | [diff] [blame] | 48 | #include <linux/raid/pq.h> |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 49 | #include <linux/async_tx.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 50 | #include <linux/module.h> |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 51 | #include <linux/async.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include <linux/seq_file.h> |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 53 | #include <linux/cpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 55 | #include <linux/ratelimit.h> |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 56 | #include <trace/events/block.h> |
| 57 | |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 58 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 59 | #include "raid5.h" |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 60 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 61 | #include "bitmap.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 62 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | /* |
| 64 | * Stripe cache |
| 65 | */ |
| 66 | |
| 67 | #define NR_STRIPES 256 |
| 68 | #define STRIPE_SIZE PAGE_SIZE |
| 69 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 70 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 71 | #define IO_THRESHOLD 1 |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 72 | #define BYPASS_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 73 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 74 | #define HASH_MASK (NR_HASH - 1) |
| 75 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 76 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 77 | { |
| 78 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; |
| 79 | return &conf->stripe_hashtbl[hash]; |
| 80 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
| 82 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 83 | * order without overlap. There may be several bio's per stripe+device, and |
| 84 | * a bio could span several devices. |
| 85 | * When walking this list for a particular stripe+device, we must never proceed |
| 86 | * beyond a bio that extends past this device, as the next bio might no longer |
| 87 | * be valid. |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 88 | * This function is used to determine the 'next' bio in the list, given the sector |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | * of the current stripe+device |
| 90 | */ |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 91 | static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector) |
| 92 | { |
| 93 | int sectors = bio->bi_size >> 9; |
| 94 | if (bio->bi_sector + sectors < sector + STRIPE_SECTORS) |
| 95 | return bio->bi_next; |
| 96 | else |
| 97 | return NULL; |
| 98 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 100 | /* |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 101 | * We maintain a biased count of active stripes in the bottom 16 bits of |
| 102 | * bi_phys_segments, and a count of processed stripes in the upper 16 bits |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 103 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 104 | static inline int raid5_bi_processed_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 105 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 106 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 107 | return (atomic_read(segments) >> 16) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 110 | static inline int raid5_dec_bi_active_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 111 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 112 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 113 | return atomic_sub_return(1, segments) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 114 | } |
| 115 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 116 | static inline void raid5_inc_bi_active_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 117 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 118 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 119 | atomic_inc(segments); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 120 | } |
| 121 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 122 | static inline void raid5_set_bi_processed_stripes(struct bio *bio, |
| 123 | unsigned int cnt) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 124 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 125 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 126 | int old, new; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 127 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 128 | do { |
| 129 | old = atomic_read(segments); |
| 130 | new = (old & 0xffff) | (cnt << 16); |
| 131 | } while (atomic_cmpxchg(segments, old, new) != old); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 132 | } |
| 133 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 134 | static inline void raid5_set_bi_stripes(struct bio *bio, unsigned int cnt) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 135 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 136 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 137 | atomic_set(segments, cnt); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 138 | } |
| 139 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 140 | /* Find first data disk in a raid6 stripe */ |
| 141 | static inline int raid6_d0(struct stripe_head *sh) |
| 142 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 143 | if (sh->ddf_layout) |
| 144 | /* ddf always start from first device */ |
| 145 | return 0; |
| 146 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 147 | if (sh->qd_idx == sh->disks - 1) |
| 148 | return 0; |
| 149 | else |
| 150 | return sh->qd_idx + 1; |
| 151 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 152 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 153 | { |
| 154 | disk++; |
| 155 | return (disk < raid_disks) ? disk : 0; |
| 156 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 157 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 158 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 159 | * We need to map each disk to a 'slot', where the data disks are slot |
| 160 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 161 | * is raid_disks-1. This help does that mapping. |
| 162 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 163 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 164 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 165 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 166 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 167 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 168 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 169 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 170 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 171 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 172 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 173 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 174 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 175 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 176 | return slot; |
| 177 | } |
| 178 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 179 | static void return_io(struct bio *return_bi) |
| 180 | { |
| 181 | struct bio *bi = return_bi; |
| 182 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 183 | |
| 184 | return_bi = bi->bi_next; |
| 185 | bi->bi_next = NULL; |
| 186 | bi->bi_size = 0; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 187 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 188 | bi, 0); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 189 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 190 | bi = return_bi; |
| 191 | } |
| 192 | } |
| 193 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 194 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 196 | static int stripe_operations_active(struct stripe_head *sh) |
| 197 | { |
| 198 | return sh->check_state || sh->reconstruct_state || |
| 199 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 200 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 201 | } |
| 202 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 203 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | { |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 205 | BUG_ON(!list_empty(&sh->lru)); |
| 206 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
| 207 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 208 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
| 209 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 210 | list_add_tail(&sh->lru, &conf->delayed_list); |
| 211 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
| 212 | sh->bm_seq - conf->seq_write > 0) |
| 213 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 214 | else { |
| 215 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 216 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
| 217 | list_add_tail(&sh->lru, &conf->handle_list); |
| 218 | } |
| 219 | md_wakeup_thread(conf->mddev->thread); |
| 220 | } else { |
| 221 | BUG_ON(stripe_operations_active(sh)); |
| 222 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 223 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 224 | < IO_THRESHOLD) |
| 225 | md_wakeup_thread(conf->mddev->thread); |
| 226 | atomic_dec(&conf->active_stripes); |
| 227 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 228 | list_add_tail(&sh->lru, &conf->inactive_list); |
| 229 | wake_up(&conf->wait_for_stripe); |
| 230 | if (conf->retry_read_aligned) |
| 231 | md_wakeup_thread(conf->mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | } |
| 233 | } |
| 234 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 235 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 236 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh) |
| 237 | { |
| 238 | if (atomic_dec_and_test(&sh->count)) |
| 239 | do_release_stripe(conf, sh); |
| 240 | } |
| 241 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | static void release_stripe(struct stripe_head *sh) |
| 243 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 244 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 245 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 246 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 247 | local_irq_save(flags); |
| 248 | if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { |
| 249 | do_release_stripe(conf, sh); |
| 250 | spin_unlock(&conf->device_lock); |
| 251 | } |
| 252 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 255 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 256 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 257 | pr_debug("remove_hash(), stripe %llu\n", |
| 258 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 260 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 261 | } |
| 262 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 263 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 265 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 266 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 267 | pr_debug("insert_hash(), stripe %llu\n", |
| 268 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 270 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | |
| 274 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 275 | static struct stripe_head *get_free_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
| 277 | struct stripe_head *sh = NULL; |
| 278 | struct list_head *first; |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | if (list_empty(&conf->inactive_list)) |
| 281 | goto out; |
| 282 | first = conf->inactive_list.next; |
| 283 | sh = list_entry(first, struct stripe_head, lru); |
| 284 | list_del_init(first); |
| 285 | remove_hash(sh); |
| 286 | atomic_inc(&conf->active_stripes); |
| 287 | out: |
| 288 | return sh; |
| 289 | } |
| 290 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 291 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 292 | { |
| 293 | struct page *p; |
| 294 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 295 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 297 | for (i = 0; i < num ; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 298 | p = sh->dev[i].page; |
| 299 | if (!p) |
| 300 | continue; |
| 301 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 302 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 306 | static int grow_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 307 | { |
| 308 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 309 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 311 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | struct page *page; |
| 313 | |
| 314 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 315 | return 1; |
| 316 | } |
| 317 | sh->dev[i].page = page; |
| 318 | } |
| 319 | return 0; |
| 320 | } |
| 321 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 322 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 323 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 324 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 326 | static void init_stripe(struct stripe_head *sh, sector_t sector, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 328 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 329 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 331 | BUG_ON(atomic_read(&sh->count) != 0); |
| 332 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 333 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 334 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 335 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 336 | (unsigned long long)sh->sector); |
| 337 | |
| 338 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 339 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 340 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 341 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 343 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 344 | sh->state = 0; |
| 345 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 346 | |
| 347 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 348 | struct r5dev *dev = &sh->dev[i]; |
| 349 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 350 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 352 | printk(KERN_ERR "sector=%llx i=%d %p %p %p %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 354 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 356 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 357 | } |
| 358 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 359 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 360 | } |
| 361 | insert_hash(conf, sh); |
| 362 | } |
| 363 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 364 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 365 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 366 | { |
| 367 | struct stripe_head *sh; |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 368 | struct hlist_node *hn; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 370 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 371 | hlist_for_each_entry(sh, hn, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 372 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 373 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 374 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 375 | return NULL; |
| 376 | } |
| 377 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 378 | /* |
| 379 | * Need to check if array has failed when deciding whether to: |
| 380 | * - start an array |
| 381 | * - remove non-faulty devices |
| 382 | * - add a spare |
| 383 | * - allow a reshape |
| 384 | * This determination is simple when no reshape is happening. |
| 385 | * However if there is a reshape, we need to carefully check |
| 386 | * both the before and after sections. |
| 387 | * This is because some failed devices may only affect one |
| 388 | * of the two sections, and some non-in_sync devices may |
| 389 | * be insync in the section most affected by failed devices. |
| 390 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 391 | static int calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 392 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 393 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 394 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 395 | |
| 396 | rcu_read_lock(); |
| 397 | degraded = 0; |
| 398 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 399 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 400 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 401 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 402 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 403 | degraded++; |
| 404 | else if (test_bit(In_sync, &rdev->flags)) |
| 405 | ; |
| 406 | else |
| 407 | /* not in-sync or faulty. |
| 408 | * If the reshape increases the number of devices, |
| 409 | * this is being recovered by the reshape, so |
| 410 | * this 'previous' section is not in_sync. |
| 411 | * If the number of devices is being reduced however, |
| 412 | * the device can only be part of the array if |
| 413 | * we are reverting a reshape, so this section will |
| 414 | * be in-sync. |
| 415 | */ |
| 416 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 417 | degraded++; |
| 418 | } |
| 419 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 420 | if (conf->raid_disks == conf->previous_raid_disks) |
| 421 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 422 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 423 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 424 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 425 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 426 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 427 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 428 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 429 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 430 | else if (test_bit(In_sync, &rdev->flags)) |
| 431 | ; |
| 432 | else |
| 433 | /* not in-sync or faulty. |
| 434 | * If reshape increases the number of devices, this |
| 435 | * section has already been recovered, else it |
| 436 | * almost certainly hasn't. |
| 437 | */ |
| 438 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 439 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 440 | } |
| 441 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 442 | if (degraded2 > degraded) |
| 443 | return degraded2; |
| 444 | return degraded; |
| 445 | } |
| 446 | |
| 447 | static int has_failed(struct r5conf *conf) |
| 448 | { |
| 449 | int degraded; |
| 450 | |
| 451 | if (conf->mddev->reshape_position == MaxSector) |
| 452 | return conf->mddev->degraded > conf->max_degraded; |
| 453 | |
| 454 | degraded = calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 455 | if (degraded > conf->max_degraded) |
| 456 | return 1; |
| 457 | return 0; |
| 458 | } |
| 459 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 460 | static struct stripe_head * |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 461 | get_active_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 462 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | { |
| 464 | struct stripe_head *sh; |
| 465 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 466 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
| 468 | spin_lock_irq(&conf->device_lock); |
| 469 | |
| 470 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 471 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 472 | conf->quiesce == 0 || noquiesce, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 473 | conf->device_lock); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 474 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 475 | if (!sh) { |
| 476 | if (!conf->inactive_blocked) |
| 477 | sh = get_free_stripe(conf); |
| 478 | if (noblock && sh == NULL) |
| 479 | break; |
| 480 | if (!sh) { |
| 481 | conf->inactive_blocked = 1; |
| 482 | wait_event_lock_irq(conf->wait_for_stripe, |
| 483 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 484 | (atomic_read(&conf->active_stripes) |
| 485 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 486 | || !conf->inactive_blocked), |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 487 | conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | conf->inactive_blocked = 0; |
| 489 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 490 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 491 | } else { |
| 492 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 493 | BUG_ON(!list_empty(&sh->lru) |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 494 | && !test_bit(STRIPE_EXPANDING, &sh->state) |
| 495 | && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 496 | } else { |
| 497 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 498 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 499 | if (list_empty(&sh->lru) && |
| 500 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 501 | BUG(); |
| 502 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | } while (sh == NULL); |
| 506 | |
| 507 | if (sh) |
| 508 | atomic_inc(&sh->count); |
| 509 | |
| 510 | spin_unlock_irq(&conf->device_lock); |
| 511 | return sh; |
| 512 | } |
| 513 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 514 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 515 | * in this stripe_head. |
| 516 | */ |
| 517 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 518 | { |
| 519 | sector_t progress = conf->reshape_progress; |
| 520 | /* Need a memory barrier to make sure we see the value |
| 521 | * of conf->generation, or ->data_offset that was set before |
| 522 | * reshape_progress was updated. |
| 523 | */ |
| 524 | smp_rmb(); |
| 525 | if (progress == MaxSector) |
| 526 | return 0; |
| 527 | if (sh->generation == conf->generation - 1) |
| 528 | return 0; |
| 529 | /* We are in a reshape, and this is a new-generation stripe, |
| 530 | * so use new_data_offset. |
| 531 | */ |
| 532 | return 1; |
| 533 | } |
| 534 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 535 | static void |
| 536 | raid5_end_read_request(struct bio *bi, int error); |
| 537 | static void |
| 538 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 539 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 540 | static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 541 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 542 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 543 | int i, disks = sh->disks; |
| 544 | |
| 545 | might_sleep(); |
| 546 | |
| 547 | for (i = disks; i--; ) { |
| 548 | int rw; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 549 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 550 | struct bio *bi, *rbi; |
| 551 | struct md_rdev *rdev, *rrdev = NULL; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 552 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 553 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 554 | rw = WRITE_FUA; |
| 555 | else |
| 556 | rw = WRITE; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 557 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 558 | rw |= REQ_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 559 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 560 | rw = READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 561 | else if (test_and_clear_bit(R5_WantReplace, |
| 562 | &sh->dev[i].flags)) { |
| 563 | rw = WRITE; |
| 564 | replace_only = 1; |
| 565 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 566 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 567 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
| 568 | rw |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 569 | |
| 570 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 571 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 572 | |
| 573 | bi->bi_rw = rw; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 574 | rbi->bi_rw = rw; |
| 575 | if (rw & WRITE) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 576 | bi->bi_end_io = raid5_end_write_request; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 577 | rbi->bi_end_io = raid5_end_write_request; |
| 578 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 579 | bi->bi_end_io = raid5_end_read_request; |
| 580 | |
| 581 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 582 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 583 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 584 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 585 | if (!rdev) { |
| 586 | rdev = rrdev; |
| 587 | rrdev = NULL; |
| 588 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 589 | if (rw & WRITE) { |
| 590 | if (replace_only) |
| 591 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 592 | if (rdev == rrdev) |
| 593 | /* We raced and saw duplicates */ |
| 594 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 595 | } else { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 596 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 597 | rdev = rrdev; |
| 598 | rrdev = NULL; |
| 599 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 600 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 601 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 602 | rdev = NULL; |
| 603 | if (rdev) |
| 604 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 605 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 606 | rrdev = NULL; |
| 607 | if (rrdev) |
| 608 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 609 | rcu_read_unlock(); |
| 610 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 611 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 612 | * need to check for writes. We never accept write errors |
| 613 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 614 | */ |
| 615 | while ((rw & WRITE) && rdev && |
| 616 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 617 | sector_t first_bad; |
| 618 | int bad_sectors; |
| 619 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 620 | &first_bad, &bad_sectors); |
| 621 | if (!bad) |
| 622 | break; |
| 623 | |
| 624 | if (bad < 0) { |
| 625 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 626 | if (!conf->mddev->external && |
| 627 | conf->mddev->flags) { |
| 628 | /* It is very unlikely, but we might |
| 629 | * still need to write out the |
| 630 | * bad block log - better give it |
| 631 | * a chance*/ |
| 632 | md_check_recovery(conf->mddev); |
| 633 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 634 | /* |
| 635 | * Because md_wait_for_blocked_rdev |
| 636 | * will dec nr_pending, we must |
| 637 | * increment it first. |
| 638 | */ |
| 639 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 640 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 641 | } else { |
| 642 | /* Acknowledged bad block - skip the write */ |
| 643 | rdev_dec_pending(rdev, conf->mddev); |
| 644 | rdev = NULL; |
| 645 | } |
| 646 | } |
| 647 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 648 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 649 | if (s->syncing || s->expanding || s->expanded |
| 650 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 651 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 652 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 653 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 654 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 655 | bi->bi_bdev = rdev->bdev; |
| 656 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 657 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 658 | bi->bi_rw, i); |
| 659 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 660 | if (use_new_offset(conf, sh)) |
| 661 | bi->bi_sector = (sh->sector |
| 662 | + rdev->new_data_offset); |
| 663 | else |
| 664 | bi->bi_sector = (sh->sector |
| 665 | + rdev->data_offset); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 666 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 667 | bi->bi_rw |= REQ_FLUSH; |
| 668 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 669 | bi->bi_flags = 1 << BIO_UPTODATE; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 670 | bi->bi_idx = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 671 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 672 | bi->bi_io_vec[0].bv_offset = 0; |
| 673 | bi->bi_size = STRIPE_SIZE; |
| 674 | bi->bi_next = NULL; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 675 | if (rrdev) |
| 676 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame^] | 677 | |
| 678 | if (conf->mddev->gendisk) |
| 679 | trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), |
| 680 | bi, disk_devt(conf->mddev->gendisk), |
| 681 | sh->dev[i].sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 682 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 683 | } |
| 684 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 685 | if (s->syncing || s->expanding || s->expanded |
| 686 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 687 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 688 | |
| 689 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 690 | |
| 691 | rbi->bi_bdev = rrdev->bdev; |
| 692 | pr_debug("%s: for %llu schedule op %ld on " |
| 693 | "replacement disc %d\n", |
| 694 | __func__, (unsigned long long)sh->sector, |
| 695 | rbi->bi_rw, i); |
| 696 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 697 | if (use_new_offset(conf, sh)) |
| 698 | rbi->bi_sector = (sh->sector |
| 699 | + rrdev->new_data_offset); |
| 700 | else |
| 701 | rbi->bi_sector = (sh->sector |
| 702 | + rrdev->data_offset); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 703 | rbi->bi_flags = 1 << BIO_UPTODATE; |
| 704 | rbi->bi_idx = 0; |
| 705 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 706 | rbi->bi_io_vec[0].bv_offset = 0; |
| 707 | rbi->bi_size = STRIPE_SIZE; |
| 708 | rbi->bi_next = NULL; |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame^] | 709 | if (conf->mddev->gendisk) |
| 710 | trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), |
| 711 | rbi, disk_devt(conf->mddev->gendisk), |
| 712 | sh->dev[i].sector); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 713 | generic_make_request(rbi); |
| 714 | } |
| 715 | if (!rdev && !rrdev) { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 716 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 717 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 718 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 719 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 720 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 721 | set_bit(STRIPE_HANDLE, &sh->state); |
| 722 | } |
| 723 | } |
| 724 | } |
| 725 | |
| 726 | static struct dma_async_tx_descriptor * |
| 727 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 728 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 729 | { |
| 730 | struct bio_vec *bvl; |
| 731 | struct page *bio_page; |
| 732 | int i; |
| 733 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 734 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 735 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 736 | |
| 737 | if (bio->bi_sector >= sector) |
| 738 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 739 | else |
| 740 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 741 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 742 | if (frombio) |
| 743 | flags |= ASYNC_TX_FENCE; |
| 744 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 745 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 746 | bio_for_each_segment(bvl, bio, i) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 747 | int len = bvl->bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 748 | int clen; |
| 749 | int b_offset = 0; |
| 750 | |
| 751 | if (page_offset < 0) { |
| 752 | b_offset = -page_offset; |
| 753 | page_offset += b_offset; |
| 754 | len -= b_offset; |
| 755 | } |
| 756 | |
| 757 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 758 | clen = STRIPE_SIZE - page_offset; |
| 759 | else |
| 760 | clen = len; |
| 761 | |
| 762 | if (clen > 0) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 763 | b_offset += bvl->bv_offset; |
| 764 | bio_page = bvl->bv_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 765 | if (frombio) |
| 766 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 767 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 768 | else |
| 769 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 770 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 771 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 772 | /* chain the operations */ |
| 773 | submit.depend_tx = tx; |
| 774 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 775 | if (clen < len) /* hit end of page */ |
| 776 | break; |
| 777 | page_offset += len; |
| 778 | } |
| 779 | |
| 780 | return tx; |
| 781 | } |
| 782 | |
| 783 | static void ops_complete_biofill(void *stripe_head_ref) |
| 784 | { |
| 785 | struct stripe_head *sh = stripe_head_ref; |
| 786 | struct bio *return_bi = NULL; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 787 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 788 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 789 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 790 | (unsigned long long)sh->sector); |
| 791 | |
| 792 | /* clear completed biofills */ |
| 793 | for (i = sh->disks; i--; ) { |
| 794 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 795 | |
| 796 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 797 | /* and check if we need to reply to a read request, |
| 798 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 799 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 800 | */ |
| 801 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 802 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 803 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 804 | BUG_ON(!dev->read); |
| 805 | rbi = dev->read; |
| 806 | dev->read = NULL; |
| 807 | while (rbi && rbi->bi_sector < |
| 808 | dev->sector + STRIPE_SECTORS) { |
| 809 | rbi2 = r5_next_bio(rbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 810 | if (!raid5_dec_bi_active_stripes(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 811 | rbi->bi_next = return_bi; |
| 812 | return_bi = rbi; |
| 813 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 814 | rbi = rbi2; |
| 815 | } |
| 816 | } |
| 817 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 818 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 819 | |
| 820 | return_io(return_bi); |
| 821 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 822 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 823 | release_stripe(sh); |
| 824 | } |
| 825 | |
| 826 | static void ops_run_biofill(struct stripe_head *sh) |
| 827 | { |
| 828 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 829 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 830 | int i; |
| 831 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 832 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 833 | (unsigned long long)sh->sector); |
| 834 | |
| 835 | for (i = sh->disks; i--; ) { |
| 836 | struct r5dev *dev = &sh->dev[i]; |
| 837 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 838 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 839 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 840 | dev->read = rbi = dev->toread; |
| 841 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 842 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 843 | while (rbi && rbi->bi_sector < |
| 844 | dev->sector + STRIPE_SECTORS) { |
| 845 | tx = async_copy_data(0, rbi, dev->page, |
| 846 | dev->sector, tx); |
| 847 | rbi = r5_next_bio(rbi, dev->sector); |
| 848 | } |
| 849 | } |
| 850 | } |
| 851 | |
| 852 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 853 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 854 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 855 | } |
| 856 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 857 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 858 | { |
| 859 | struct r5dev *tgt; |
| 860 | |
| 861 | if (target < 0) |
| 862 | return; |
| 863 | |
| 864 | tgt = &sh->dev[target]; |
| 865 | set_bit(R5_UPTODATE, &tgt->flags); |
| 866 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 867 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 868 | } |
| 869 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 870 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 871 | { |
| 872 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 873 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 874 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 875 | (unsigned long long)sh->sector); |
| 876 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 877 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 878 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 879 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 880 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 881 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 882 | if (sh->check_state == check_state_compute_run) |
| 883 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 884 | set_bit(STRIPE_HANDLE, &sh->state); |
| 885 | release_stripe(sh); |
| 886 | } |
| 887 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 888 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 889 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 890 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 891 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 892 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 893 | } |
| 894 | |
| 895 | static struct dma_async_tx_descriptor * |
| 896 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 897 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 898 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 899 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 900 | int target = sh->ops.target; |
| 901 | struct r5dev *tgt = &sh->dev[target]; |
| 902 | struct page *xor_dest = tgt->page; |
| 903 | int count = 0; |
| 904 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 905 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 906 | int i; |
| 907 | |
| 908 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 909 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 910 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 911 | |
| 912 | for (i = disks; i--; ) |
| 913 | if (i != target) |
| 914 | xor_srcs[count++] = sh->dev[i].page; |
| 915 | |
| 916 | atomic_inc(&sh->count); |
| 917 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 918 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 919 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 920 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 921 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 922 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 923 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 924 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 925 | return tx; |
| 926 | } |
| 927 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 928 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 929 | * @srcs - (struct page *) array of size sh->disks |
| 930 | * @sh - stripe_head to parse |
| 931 | * |
| 932 | * Populates srcs in proper layout order for the stripe and returns the |
| 933 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 934 | * destination buffer is recorded in srcs[count] and the Q destination |
| 935 | * is recorded in srcs[count+1]]. |
| 936 | */ |
| 937 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 938 | { |
| 939 | int disks = sh->disks; |
| 940 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 941 | int d0_idx = raid6_d0(sh); |
| 942 | int count; |
| 943 | int i; |
| 944 | |
| 945 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 946 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 947 | |
| 948 | count = 0; |
| 949 | i = d0_idx; |
| 950 | do { |
| 951 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 952 | |
| 953 | srcs[slot] = sh->dev[i].page; |
| 954 | i = raid6_next_disk(i, disks); |
| 955 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 956 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 957 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 958 | } |
| 959 | |
| 960 | static struct dma_async_tx_descriptor * |
| 961 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 962 | { |
| 963 | int disks = sh->disks; |
| 964 | struct page **blocks = percpu->scribble; |
| 965 | int target; |
| 966 | int qd_idx = sh->qd_idx; |
| 967 | struct dma_async_tx_descriptor *tx; |
| 968 | struct async_submit_ctl submit; |
| 969 | struct r5dev *tgt; |
| 970 | struct page *dest; |
| 971 | int i; |
| 972 | int count; |
| 973 | |
| 974 | if (sh->ops.target < 0) |
| 975 | target = sh->ops.target2; |
| 976 | else if (sh->ops.target2 < 0) |
| 977 | target = sh->ops.target; |
| 978 | else |
| 979 | /* we should only have one valid target */ |
| 980 | BUG(); |
| 981 | BUG_ON(target < 0); |
| 982 | pr_debug("%s: stripe %llu block: %d\n", |
| 983 | __func__, (unsigned long long)sh->sector, target); |
| 984 | |
| 985 | tgt = &sh->dev[target]; |
| 986 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 987 | dest = tgt->page; |
| 988 | |
| 989 | atomic_inc(&sh->count); |
| 990 | |
| 991 | if (target == qd_idx) { |
| 992 | count = set_syndrome_sources(blocks, sh); |
| 993 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 994 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 995 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 996 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 997 | to_addr_conv(sh, percpu)); |
| 998 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 999 | } else { |
| 1000 | /* Compute any data- or p-drive using XOR */ |
| 1001 | count = 0; |
| 1002 | for (i = disks; i-- ; ) { |
| 1003 | if (i == target || i == qd_idx) |
| 1004 | continue; |
| 1005 | blocks[count++] = sh->dev[i].page; |
| 1006 | } |
| 1007 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1008 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1009 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1010 | to_addr_conv(sh, percpu)); |
| 1011 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 1012 | } |
| 1013 | |
| 1014 | return tx; |
| 1015 | } |
| 1016 | |
| 1017 | static struct dma_async_tx_descriptor * |
| 1018 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1019 | { |
| 1020 | int i, count, disks = sh->disks; |
| 1021 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1022 | int d0_idx = raid6_d0(sh); |
| 1023 | int faila = -1, failb = -1; |
| 1024 | int target = sh->ops.target; |
| 1025 | int target2 = sh->ops.target2; |
| 1026 | struct r5dev *tgt = &sh->dev[target]; |
| 1027 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1028 | struct dma_async_tx_descriptor *tx; |
| 1029 | struct page **blocks = percpu->scribble; |
| 1030 | struct async_submit_ctl submit; |
| 1031 | |
| 1032 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1033 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1034 | BUG_ON(target < 0 || target2 < 0); |
| 1035 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1036 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1037 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1038 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1039 | * slot number conversion for 'faila' and 'failb' |
| 1040 | */ |
| 1041 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1042 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1043 | count = 0; |
| 1044 | i = d0_idx; |
| 1045 | do { |
| 1046 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1047 | |
| 1048 | blocks[slot] = sh->dev[i].page; |
| 1049 | |
| 1050 | if (i == target) |
| 1051 | faila = slot; |
| 1052 | if (i == target2) |
| 1053 | failb = slot; |
| 1054 | i = raid6_next_disk(i, disks); |
| 1055 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1056 | |
| 1057 | BUG_ON(faila == failb); |
| 1058 | if (failb < faila) |
| 1059 | swap(faila, failb); |
| 1060 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1061 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1062 | |
| 1063 | atomic_inc(&sh->count); |
| 1064 | |
| 1065 | if (failb == syndrome_disks+1) { |
| 1066 | /* Q disk is one of the missing disks */ |
| 1067 | if (faila == syndrome_disks) { |
| 1068 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1069 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1070 | ops_complete_compute, sh, |
| 1071 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1072 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1073 | STRIPE_SIZE, &submit); |
| 1074 | } else { |
| 1075 | struct page *dest; |
| 1076 | int data_target; |
| 1077 | int qd_idx = sh->qd_idx; |
| 1078 | |
| 1079 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1080 | if (target == qd_idx) |
| 1081 | data_target = target2; |
| 1082 | else |
| 1083 | data_target = target; |
| 1084 | |
| 1085 | count = 0; |
| 1086 | for (i = disks; i-- ; ) { |
| 1087 | if (i == data_target || i == qd_idx) |
| 1088 | continue; |
| 1089 | blocks[count++] = sh->dev[i].page; |
| 1090 | } |
| 1091 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1092 | init_async_submit(&submit, |
| 1093 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1094 | NULL, NULL, NULL, |
| 1095 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1096 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1097 | &submit); |
| 1098 | |
| 1099 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1100 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1101 | ops_complete_compute, sh, |
| 1102 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1103 | return async_gen_syndrome(blocks, 0, count+2, |
| 1104 | STRIPE_SIZE, &submit); |
| 1105 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1106 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1107 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1108 | ops_complete_compute, sh, |
| 1109 | to_addr_conv(sh, percpu)); |
| 1110 | if (failb == syndrome_disks) { |
| 1111 | /* We're missing D+P. */ |
| 1112 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1113 | STRIPE_SIZE, faila, |
| 1114 | blocks, &submit); |
| 1115 | } else { |
| 1116 | /* We're missing D+D. */ |
| 1117 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1118 | STRIPE_SIZE, faila, failb, |
| 1119 | blocks, &submit); |
| 1120 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1121 | } |
| 1122 | } |
| 1123 | |
| 1124 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1125 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1126 | { |
| 1127 | struct stripe_head *sh = stripe_head_ref; |
| 1128 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1129 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1130 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1131 | } |
| 1132 | |
| 1133 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1134 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1135 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1136 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1137 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1138 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1139 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1140 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1141 | |
| 1142 | /* existing parity data subtracted */ |
| 1143 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1144 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1145 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1146 | (unsigned long long)sh->sector); |
| 1147 | |
| 1148 | for (i = disks; i--; ) { |
| 1149 | struct r5dev *dev = &sh->dev[i]; |
| 1150 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1151 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1152 | xor_srcs[count++] = dev->page; |
| 1153 | } |
| 1154 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1155 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1156 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1157 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1158 | |
| 1159 | return tx; |
| 1160 | } |
| 1161 | |
| 1162 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1163 | ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1164 | { |
| 1165 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1166 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1167 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1168 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1169 | (unsigned long long)sh->sector); |
| 1170 | |
| 1171 | for (i = disks; i--; ) { |
| 1172 | struct r5dev *dev = &sh->dev[i]; |
| 1173 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1174 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1175 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1176 | struct bio *wbi; |
| 1177 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1178 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1179 | chosen = dev->towrite; |
| 1180 | dev->towrite = NULL; |
| 1181 | BUG_ON(dev->written); |
| 1182 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1183 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1184 | |
| 1185 | while (wbi && wbi->bi_sector < |
| 1186 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1187 | if (wbi->bi_rw & REQ_FUA) |
| 1188 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1189 | if (wbi->bi_rw & REQ_SYNC) |
| 1190 | set_bit(R5_SyncIO, &dev->flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1191 | if (wbi->bi_rw & REQ_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1192 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1193 | else |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1194 | tx = async_copy_data(1, wbi, dev->page, |
| 1195 | dev->sector, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1196 | wbi = r5_next_bio(wbi, dev->sector); |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | |
| 1201 | return tx; |
| 1202 | } |
| 1203 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1204 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1205 | { |
| 1206 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1207 | int disks = sh->disks; |
| 1208 | int pd_idx = sh->pd_idx; |
| 1209 | int qd_idx = sh->qd_idx; |
| 1210 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1211 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1212 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1213 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1214 | (unsigned long long)sh->sector); |
| 1215 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1216 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1217 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1218 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1219 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1220 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1221 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1222 | for (i = disks; i--; ) { |
| 1223 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1224 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1225 | if (dev->written || i == pd_idx || i == qd_idx) { |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1226 | if (!discard) |
| 1227 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1228 | if (fua) |
| 1229 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1230 | if (sync) |
| 1231 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1232 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1235 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1236 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1237 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1238 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1239 | else { |
| 1240 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1241 | sh->reconstruct_state = reconstruct_state_result; |
| 1242 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1243 | |
| 1244 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1245 | release_stripe(sh); |
| 1246 | } |
| 1247 | |
| 1248 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1249 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1250 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1251 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1252 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1253 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1254 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1255 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1256 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1257 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1258 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1259 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1260 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1261 | (unsigned long long)sh->sector); |
| 1262 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1263 | for (i = 0; i < sh->disks; i++) { |
| 1264 | if (pd_idx == i) |
| 1265 | continue; |
| 1266 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1267 | break; |
| 1268 | } |
| 1269 | if (i >= sh->disks) { |
| 1270 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1271 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 1272 | ops_complete_reconstruct(sh); |
| 1273 | return; |
| 1274 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1275 | /* check if prexor is active which means only process blocks |
| 1276 | * that are part of a read-modify-write (written) |
| 1277 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1278 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1279 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1280 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1281 | for (i = disks; i--; ) { |
| 1282 | struct r5dev *dev = &sh->dev[i]; |
| 1283 | if (dev->written) |
| 1284 | xor_srcs[count++] = dev->page; |
| 1285 | } |
| 1286 | } else { |
| 1287 | xor_dest = sh->dev[pd_idx].page; |
| 1288 | for (i = disks; i--; ) { |
| 1289 | struct r5dev *dev = &sh->dev[i]; |
| 1290 | if (i != pd_idx) |
| 1291 | xor_srcs[count++] = dev->page; |
| 1292 | } |
| 1293 | } |
| 1294 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1295 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1296 | * 2/ if we did not prexor then we are redoing the parity |
| 1297 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1298 | * for the synchronous xor case |
| 1299 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1300 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1301 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1302 | |
| 1303 | atomic_inc(&sh->count); |
| 1304 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1305 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1306 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1307 | if (unlikely(count == 1)) |
| 1308 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1309 | else |
| 1310 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1311 | } |
| 1312 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1313 | static void |
| 1314 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1315 | struct dma_async_tx_descriptor *tx) |
| 1316 | { |
| 1317 | struct async_submit_ctl submit; |
| 1318 | struct page **blocks = percpu->scribble; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1319 | int count, i; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1320 | |
| 1321 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1322 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1323 | for (i = 0; i < sh->disks; i++) { |
| 1324 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 1325 | continue; |
| 1326 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1327 | break; |
| 1328 | } |
| 1329 | if (i >= sh->disks) { |
| 1330 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1331 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 1332 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 1333 | ops_complete_reconstruct(sh); |
| 1334 | return; |
| 1335 | } |
| 1336 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1337 | count = set_syndrome_sources(blocks, sh); |
| 1338 | |
| 1339 | atomic_inc(&sh->count); |
| 1340 | |
| 1341 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1342 | sh, to_addr_conv(sh, percpu)); |
| 1343 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1344 | } |
| 1345 | |
| 1346 | static void ops_complete_check(void *stripe_head_ref) |
| 1347 | { |
| 1348 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1349 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1350 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1351 | (unsigned long long)sh->sector); |
| 1352 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1353 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1354 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1355 | release_stripe(sh); |
| 1356 | } |
| 1357 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1358 | static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1359 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1360 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1361 | int pd_idx = sh->pd_idx; |
| 1362 | int qd_idx = sh->qd_idx; |
| 1363 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1364 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1365 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1366 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1367 | int count; |
| 1368 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1369 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1370 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1371 | (unsigned long long)sh->sector); |
| 1372 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1373 | count = 0; |
| 1374 | xor_dest = sh->dev[pd_idx].page; |
| 1375 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1376 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1377 | if (i == pd_idx || i == qd_idx) |
| 1378 | continue; |
| 1379 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1380 | } |
| 1381 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1382 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1383 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1384 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1385 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1386 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1387 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1388 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1389 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1390 | } |
| 1391 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1392 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1393 | { |
| 1394 | struct page **srcs = percpu->scribble; |
| 1395 | struct async_submit_ctl submit; |
| 1396 | int count; |
| 1397 | |
| 1398 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1399 | (unsigned long long)sh->sector, checkp); |
| 1400 | |
| 1401 | count = set_syndrome_sources(srcs, sh); |
| 1402 | if (!checkp) |
| 1403 | srcs[count] = NULL; |
| 1404 | |
| 1405 | atomic_inc(&sh->count); |
| 1406 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1407 | sh, to_addr_conv(sh, percpu)); |
| 1408 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1409 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1410 | } |
| 1411 | |
NeilBrown | 51acbce | 2013-02-28 09:08:34 +1100 | [diff] [blame] | 1412 | static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1413 | { |
| 1414 | int overlap_clear = 0, i, disks = sh->disks; |
| 1415 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1416 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1417 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1418 | struct raid5_percpu *percpu; |
| 1419 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1420 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1421 | cpu = get_cpu(); |
| 1422 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1423 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1424 | ops_run_biofill(sh); |
| 1425 | overlap_clear++; |
| 1426 | } |
| 1427 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1428 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1429 | if (level < 6) |
| 1430 | tx = ops_run_compute5(sh, percpu); |
| 1431 | else { |
| 1432 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1433 | tx = ops_run_compute6_1(sh, percpu); |
| 1434 | else |
| 1435 | tx = ops_run_compute6_2(sh, percpu); |
| 1436 | } |
| 1437 | /* terminate the chain if reconstruct is not set to be run */ |
| 1438 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1439 | async_tx_ack(tx); |
| 1440 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1441 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1442 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1443 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1444 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1445 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1446 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1447 | overlap_clear++; |
| 1448 | } |
| 1449 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1450 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1451 | if (level < 6) |
| 1452 | ops_run_reconstruct5(sh, percpu, tx); |
| 1453 | else |
| 1454 | ops_run_reconstruct6(sh, percpu, tx); |
| 1455 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1456 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1457 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1458 | if (sh->check_state == check_state_run) |
| 1459 | ops_run_check_p(sh, percpu); |
| 1460 | else if (sh->check_state == check_state_run_q) |
| 1461 | ops_run_check_pq(sh, percpu, 0); |
| 1462 | else if (sh->check_state == check_state_run_pq) |
| 1463 | ops_run_check_pq(sh, percpu, 1); |
| 1464 | else |
| 1465 | BUG(); |
| 1466 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1467 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1468 | if (overlap_clear) |
| 1469 | for (i = disks; i--; ) { |
| 1470 | struct r5dev *dev = &sh->dev[i]; |
| 1471 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1472 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1473 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1474 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1475 | } |
| 1476 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1477 | static int grow_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1478 | { |
| 1479 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1480 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1481 | if (!sh) |
| 1482 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1483 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1484 | sh->raid_conf = conf; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1485 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1486 | spin_lock_init(&sh->stripe_lock); |
| 1487 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1488 | if (grow_buffers(sh)) { |
| 1489 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1490 | kmem_cache_free(conf->slab_cache, sh); |
| 1491 | return 0; |
| 1492 | } |
| 1493 | /* we just created an active stripe so... */ |
| 1494 | atomic_set(&sh->count, 1); |
| 1495 | atomic_inc(&conf->active_stripes); |
| 1496 | INIT_LIST_HEAD(&sh->lru); |
| 1497 | release_stripe(sh); |
| 1498 | return 1; |
| 1499 | } |
| 1500 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1501 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1502 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1503 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1504 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1505 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1506 | if (conf->mddev->gendisk) |
| 1507 | sprintf(conf->cache_name[0], |
| 1508 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1509 | else |
| 1510 | sprintf(conf->cache_name[0], |
| 1511 | "raid%d-%p", conf->level, conf->mddev); |
| 1512 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1513 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1514 | conf->active_name = 0; |
| 1515 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1516 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1517 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1518 | if (!sc) |
| 1519 | return 1; |
| 1520 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1521 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1522 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1523 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1524 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1525 | return 0; |
| 1526 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1527 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1528 | /** |
| 1529 | * scribble_len - return the required size of the scribble region |
| 1530 | * @num - total number of disks in the array |
| 1531 | * |
| 1532 | * The size must be enough to contain: |
| 1533 | * 1/ a struct page pointer for each device in the array +2 |
| 1534 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1535 | * (dma_map_page()) or page (page_address()) address. |
| 1536 | * |
| 1537 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1538 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1539 | * of the P and Q blocks. |
| 1540 | */ |
| 1541 | static size_t scribble_len(int num) |
| 1542 | { |
| 1543 | size_t len; |
| 1544 | |
| 1545 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1546 | |
| 1547 | return len; |
| 1548 | } |
| 1549 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1550 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1551 | { |
| 1552 | /* Make all the stripes able to hold 'newsize' devices. |
| 1553 | * New slots in each stripe get 'page' set to a new page. |
| 1554 | * |
| 1555 | * This happens in stages: |
| 1556 | * 1/ create a new kmem_cache and allocate the required number of |
| 1557 | * stripe_heads. |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 1558 | * 2/ gather all the old stripe_heads and transfer the pages across |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1559 | * to the new stripe_heads. This will have the side effect of |
| 1560 | * freezing the array as once all stripe_heads have been collected, |
| 1561 | * no IO will be possible. Old stripe heads are freed once their |
| 1562 | * pages have been transferred over, and the old kmem_cache is |
| 1563 | * freed when all stripes are done. |
| 1564 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1565 | * we simple return a failre status - no need to clean anything up. |
| 1566 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1567 | * If this fails, we don't bother trying the shrink the |
| 1568 | * stripe_heads down again, we just leave them as they are. |
| 1569 | * As each stripe_head is processed the new one is released into |
| 1570 | * active service. |
| 1571 | * |
| 1572 | * Once step2 is started, we cannot afford to wait for a write, |
| 1573 | * so we use GFP_NOIO allocations. |
| 1574 | */ |
| 1575 | struct stripe_head *osh, *nsh; |
| 1576 | LIST_HEAD(newstripes); |
| 1577 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1578 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1579 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1580 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1581 | int i; |
| 1582 | |
| 1583 | if (newsize <= conf->pool_size) |
| 1584 | return 0; /* never bother to shrink */ |
| 1585 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1586 | err = md_allow_write(conf->mddev); |
| 1587 | if (err) |
| 1588 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1589 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1590 | /* Step 1 */ |
| 1591 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1592 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1593 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1594 | if (!sc) |
| 1595 | return -ENOMEM; |
| 1596 | |
| 1597 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1598 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1599 | if (!nsh) |
| 1600 | break; |
| 1601 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1602 | nsh->raid_conf = conf; |
NeilBrown | cb13ff6 | 2012-09-24 16:27:20 +1000 | [diff] [blame] | 1603 | spin_lock_init(&nsh->stripe_lock); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1604 | |
| 1605 | list_add(&nsh->lru, &newstripes); |
| 1606 | } |
| 1607 | if (i) { |
| 1608 | /* didn't get enough, give up */ |
| 1609 | while (!list_empty(&newstripes)) { |
| 1610 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1611 | list_del(&nsh->lru); |
| 1612 | kmem_cache_free(sc, nsh); |
| 1613 | } |
| 1614 | kmem_cache_destroy(sc); |
| 1615 | return -ENOMEM; |
| 1616 | } |
| 1617 | /* Step 2 - Must use GFP_NOIO now. |
| 1618 | * OK, we have enough stripes, start collecting inactive |
| 1619 | * stripes and copying them over |
| 1620 | */ |
| 1621 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1622 | spin_lock_irq(&conf->device_lock); |
| 1623 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1624 | !list_empty(&conf->inactive_list), |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1625 | conf->device_lock); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1626 | osh = get_free_stripe(conf); |
| 1627 | spin_unlock_irq(&conf->device_lock); |
| 1628 | atomic_set(&nsh->count, 1); |
| 1629 | for(i=0; i<conf->pool_size; i++) |
| 1630 | nsh->dev[i].page = osh->dev[i].page; |
| 1631 | for( ; i<newsize; i++) |
| 1632 | nsh->dev[i].page = NULL; |
| 1633 | kmem_cache_free(conf->slab_cache, osh); |
| 1634 | } |
| 1635 | kmem_cache_destroy(conf->slab_cache); |
| 1636 | |
| 1637 | /* Step 3. |
| 1638 | * At this point, we are holding all the stripes so the array |
| 1639 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1640 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1641 | */ |
| 1642 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1643 | if (ndisks) { |
| 1644 | for (i=0; i<conf->raid_disks; i++) |
| 1645 | ndisks[i] = conf->disks[i]; |
| 1646 | kfree(conf->disks); |
| 1647 | conf->disks = ndisks; |
| 1648 | } else |
| 1649 | err = -ENOMEM; |
| 1650 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1651 | get_online_cpus(); |
| 1652 | conf->scribble_len = scribble_len(newsize); |
| 1653 | for_each_present_cpu(cpu) { |
| 1654 | struct raid5_percpu *percpu; |
| 1655 | void *scribble; |
| 1656 | |
| 1657 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1658 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1659 | |
| 1660 | if (scribble) { |
| 1661 | kfree(percpu->scribble); |
| 1662 | percpu->scribble = scribble; |
| 1663 | } else { |
| 1664 | err = -ENOMEM; |
| 1665 | break; |
| 1666 | } |
| 1667 | } |
| 1668 | put_online_cpus(); |
| 1669 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1670 | /* Step 4, return new stripes to service */ |
| 1671 | while(!list_empty(&newstripes)) { |
| 1672 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1673 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1674 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1675 | for (i=conf->raid_disks; i < newsize; i++) |
| 1676 | if (nsh->dev[i].page == NULL) { |
| 1677 | struct page *p = alloc_page(GFP_NOIO); |
| 1678 | nsh->dev[i].page = p; |
| 1679 | if (!p) |
| 1680 | err = -ENOMEM; |
| 1681 | } |
| 1682 | release_stripe(nsh); |
| 1683 | } |
| 1684 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1685 | |
| 1686 | conf->slab_cache = sc; |
| 1687 | conf->active_name = 1-conf->active_name; |
| 1688 | conf->pool_size = newsize; |
| 1689 | return err; |
| 1690 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1691 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1692 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1693 | { |
| 1694 | struct stripe_head *sh; |
| 1695 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1696 | spin_lock_irq(&conf->device_lock); |
| 1697 | sh = get_free_stripe(conf); |
| 1698 | spin_unlock_irq(&conf->device_lock); |
| 1699 | if (!sh) |
| 1700 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1701 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1702 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1703 | kmem_cache_free(conf->slab_cache, sh); |
| 1704 | atomic_dec(&conf->active_stripes); |
| 1705 | return 1; |
| 1706 | } |
| 1707 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1708 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1709 | { |
| 1710 | while (drop_one_stripe(conf)) |
| 1711 | ; |
| 1712 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1713 | if (conf->slab_cache) |
| 1714 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1715 | conf->slab_cache = NULL; |
| 1716 | } |
| 1717 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1718 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1719 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1720 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1721 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1722 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1723 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1724 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1725 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1726 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1727 | |
| 1728 | for (i=0 ; i<disks; i++) |
| 1729 | if (bi == &sh->dev[i].req) |
| 1730 | break; |
| 1731 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1732 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1733 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1734 | uptodate); |
| 1735 | if (i == disks) { |
| 1736 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1737 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1738 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1739 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1740 | /* If replacement finished while this request was outstanding, |
| 1741 | * 'replacement' might be NULL already. |
| 1742 | * In that case it moved down to 'rdev'. |
| 1743 | * rdev is not removed until all requests are finished. |
| 1744 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1745 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1746 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1747 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1748 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1749 | if (use_new_offset(conf, sh)) |
| 1750 | s = sh->sector + rdev->new_data_offset; |
| 1751 | else |
| 1752 | s = sh->sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1753 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1755 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1756 | /* Note that this cannot happen on a |
| 1757 | * replacement device. We just fail those on |
| 1758 | * any error |
| 1759 | */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1760 | printk_ratelimited( |
| 1761 | KERN_INFO |
| 1762 | "md/raid:%s: read error corrected" |
| 1763 | " (%lu sectors at %llu on %s)\n", |
| 1764 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1765 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1766 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1767 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1768 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1769 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1770 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 1771 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 1772 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1773 | if (atomic_read(&rdev->read_errors)) |
| 1774 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1775 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1776 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1777 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1778 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1779 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1780 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1781 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1782 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
| 1783 | printk_ratelimited( |
| 1784 | KERN_WARNING |
| 1785 | "md/raid:%s: read error on replacement device " |
| 1786 | "(sector %llu on %s).\n", |
| 1787 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1788 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1789 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1790 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 1791 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1792 | printk_ratelimited( |
| 1793 | KERN_WARNING |
| 1794 | "md/raid:%s: read error not correctable " |
| 1795 | "(sector %llu on %s).\n", |
| 1796 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1797 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1798 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1799 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1800 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1801 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1802 | printk_ratelimited( |
| 1803 | KERN_WARNING |
| 1804 | "md/raid:%s: read error NOT corrected!! " |
| 1805 | "(sector %llu on %s).\n", |
| 1806 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1807 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1808 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1809 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1810 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1811 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1812 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1813 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1814 | else |
| 1815 | retry = 1; |
| 1816 | if (retry) |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1817 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
| 1818 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1819 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 1820 | } else |
| 1821 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1822 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1823 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1824 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1825 | if (!(set_bad |
| 1826 | && test_bit(In_sync, &rdev->flags) |
| 1827 | && rdev_set_badblocks( |
| 1828 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 1829 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1830 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1831 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1832 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1834 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1835 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | } |
| 1837 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1838 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1839 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1840 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1841 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1842 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1843 | struct md_rdev *uninitialized_var(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1844 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1845 | sector_t first_bad; |
| 1846 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1847 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1848 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1849 | for (i = 0 ; i < disks; i++) { |
| 1850 | if (bi == &sh->dev[i].req) { |
| 1851 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1852 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1853 | } |
| 1854 | if (bi == &sh->dev[i].rreq) { |
| 1855 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1856 | if (rdev) |
| 1857 | replacement = 1; |
| 1858 | else |
| 1859 | /* rdev was removed and 'replacement' |
| 1860 | * replaced it. rdev is not removed |
| 1861 | * until all requests are finished. |
| 1862 | */ |
| 1863 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1864 | break; |
| 1865 | } |
| 1866 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1867 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1868 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1869 | uptodate); |
| 1870 | if (i == disks) { |
| 1871 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1872 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1873 | } |
| 1874 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1875 | if (replacement) { |
| 1876 | if (!uptodate) |
| 1877 | md_error(conf->mddev, rdev); |
| 1878 | else if (is_badblock(rdev, sh->sector, |
| 1879 | STRIPE_SECTORS, |
| 1880 | &first_bad, &bad_sectors)) |
| 1881 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 1882 | } else { |
| 1883 | if (!uptodate) { |
| 1884 | set_bit(WriteErrorSeen, &rdev->flags); |
| 1885 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1886 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1887 | set_bit(MD_RECOVERY_NEEDED, |
| 1888 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1889 | } else if (is_badblock(rdev, sh->sector, |
| 1890 | STRIPE_SECTORS, |
| 1891 | &first_bad, &bad_sectors)) |
| 1892 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
| 1893 | } |
| 1894 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1896 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 1897 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1898 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1899 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1900 | } |
| 1901 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1902 | static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1903 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1904 | static void raid5_build_block(struct stripe_head *sh, int i, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1905 | { |
| 1906 | struct r5dev *dev = &sh->dev[i]; |
| 1907 | |
| 1908 | bio_init(&dev->req); |
| 1909 | dev->req.bi_io_vec = &dev->vec; |
| 1910 | dev->req.bi_vcnt++; |
| 1911 | dev->req.bi_max_vecs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1912 | dev->req.bi_private = sh; |
NeilBrown | 995c427 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1913 | dev->vec.bv_page = dev->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1914 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1915 | bio_init(&dev->rreq); |
| 1916 | dev->rreq.bi_io_vec = &dev->rvec; |
| 1917 | dev->rreq.bi_vcnt++; |
| 1918 | dev->rreq.bi_max_vecs++; |
| 1919 | dev->rreq.bi_private = sh; |
| 1920 | dev->rvec.bv_page = dev->page; |
| 1921 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1922 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1923 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | } |
| 1925 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1926 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1927 | { |
| 1928 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1929 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1930 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1931 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1932 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1933 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1934 | clear_bit(In_sync, &rdev->flags); |
| 1935 | mddev->degraded = calc_degraded(conf); |
| 1936 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1937 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1938 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1939 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1940 | set_bit(Faulty, &rdev->flags); |
| 1941 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1942 | printk(KERN_ALERT |
| 1943 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1944 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1945 | mdname(mddev), |
| 1946 | bdevname(rdev->bdev, b), |
| 1947 | mdname(mddev), |
| 1948 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1949 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1950 | |
| 1951 | /* |
| 1952 | * Input: a 'big' sector number, |
| 1953 | * Output: index of the data and parity disk, and the sector # in them. |
| 1954 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1955 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1956 | int previous, int *dd_idx, |
| 1957 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1959 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1960 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1962 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1963 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1964 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1965 | int algorithm = previous ? conf->prev_algo |
| 1966 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1967 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1968 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1969 | int raid_disks = previous ? conf->previous_raid_disks |
| 1970 | : conf->raid_disks; |
| 1971 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1972 | |
| 1973 | /* First compute the information on this sector */ |
| 1974 | |
| 1975 | /* |
| 1976 | * Compute the chunk number and the sector offset inside the chunk |
| 1977 | */ |
| 1978 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1979 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1980 | |
| 1981 | /* |
| 1982 | * Compute the stripe number |
| 1983 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1984 | stripe = chunk_number; |
| 1985 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1986 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1987 | /* |
| 1988 | * Select the parity disk based on the user selected algorithm. |
| 1989 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1990 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1991 | switch(conf->level) { |
| 1992 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1993 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1994 | break; |
| 1995 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1996 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1998 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1999 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2000 | (*dd_idx)++; |
| 2001 | break; |
| 2002 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2003 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2004 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2005 | (*dd_idx)++; |
| 2006 | break; |
| 2007 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2008 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2009 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2010 | break; |
| 2011 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2012 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2013 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2014 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2015 | case ALGORITHM_PARITY_0: |
| 2016 | pd_idx = 0; |
| 2017 | (*dd_idx)++; |
| 2018 | break; |
| 2019 | case ALGORITHM_PARITY_N: |
| 2020 | pd_idx = data_disks; |
| 2021 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2022 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2023 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2024 | } |
| 2025 | break; |
| 2026 | case 6: |
| 2027 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2028 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2029 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2030 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2031 | qd_idx = pd_idx + 1; |
| 2032 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2033 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2034 | qd_idx = 0; |
| 2035 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2036 | (*dd_idx) += 2; /* D D P Q D */ |
| 2037 | break; |
| 2038 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2039 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2040 | qd_idx = pd_idx + 1; |
| 2041 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2042 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2043 | qd_idx = 0; |
| 2044 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2045 | (*dd_idx) += 2; /* D D P Q D */ |
| 2046 | break; |
| 2047 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2048 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2049 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2050 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2051 | break; |
| 2052 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2053 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2054 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2055 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2056 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2057 | |
| 2058 | case ALGORITHM_PARITY_0: |
| 2059 | pd_idx = 0; |
| 2060 | qd_idx = 1; |
| 2061 | (*dd_idx) += 2; |
| 2062 | break; |
| 2063 | case ALGORITHM_PARITY_N: |
| 2064 | pd_idx = data_disks; |
| 2065 | qd_idx = data_disks + 1; |
| 2066 | break; |
| 2067 | |
| 2068 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2069 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2070 | * of blocks for computing Q is different. |
| 2071 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2072 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2073 | qd_idx = pd_idx + 1; |
| 2074 | if (pd_idx == raid_disks-1) { |
| 2075 | (*dd_idx)++; /* Q D D D P */ |
| 2076 | qd_idx = 0; |
| 2077 | } else if (*dd_idx >= pd_idx) |
| 2078 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2079 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2080 | break; |
| 2081 | |
| 2082 | case ALGORITHM_ROTATING_N_RESTART: |
| 2083 | /* Same a left_asymmetric, by first stripe is |
| 2084 | * D D D P Q rather than |
| 2085 | * Q D D D P |
| 2086 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2087 | stripe2 += 1; |
| 2088 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2089 | qd_idx = pd_idx + 1; |
| 2090 | if (pd_idx == raid_disks-1) { |
| 2091 | (*dd_idx)++; /* Q D D D P */ |
| 2092 | qd_idx = 0; |
| 2093 | } else if (*dd_idx >= pd_idx) |
| 2094 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2095 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2096 | break; |
| 2097 | |
| 2098 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2099 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2100 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2101 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2102 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2103 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2104 | break; |
| 2105 | |
| 2106 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2107 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2108 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2109 | if (*dd_idx >= pd_idx) |
| 2110 | (*dd_idx)++; |
| 2111 | qd_idx = raid_disks - 1; |
| 2112 | break; |
| 2113 | |
| 2114 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2115 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2116 | if (*dd_idx >= pd_idx) |
| 2117 | (*dd_idx)++; |
| 2118 | qd_idx = raid_disks - 1; |
| 2119 | break; |
| 2120 | |
| 2121 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2122 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2123 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2124 | qd_idx = raid_disks - 1; |
| 2125 | break; |
| 2126 | |
| 2127 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2128 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2129 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2130 | qd_idx = raid_disks - 1; |
| 2131 | break; |
| 2132 | |
| 2133 | case ALGORITHM_PARITY_0_6: |
| 2134 | pd_idx = 0; |
| 2135 | (*dd_idx)++; |
| 2136 | qd_idx = raid_disks - 1; |
| 2137 | break; |
| 2138 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2139 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2140 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2141 | } |
| 2142 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2143 | } |
| 2144 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2145 | if (sh) { |
| 2146 | sh->pd_idx = pd_idx; |
| 2147 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2148 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2149 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2150 | /* |
| 2151 | * Finally, compute the new sector number |
| 2152 | */ |
| 2153 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2154 | return new_sector; |
| 2155 | } |
| 2156 | |
| 2157 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2158 | static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2159 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2160 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2161 | int raid_disks = sh->disks; |
| 2162 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2163 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2164 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2165 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2166 | int algorithm = previous ? conf->prev_algo |
| 2167 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2168 | sector_t stripe; |
| 2169 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2170 | sector_t chunk_number; |
| 2171 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2172 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2173 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2174 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2175 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2176 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2177 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2178 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2179 | if (i == sh->pd_idx) |
| 2180 | return 0; |
| 2181 | switch(conf->level) { |
| 2182 | case 4: break; |
| 2183 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2184 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2185 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2186 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2187 | if (i > sh->pd_idx) |
| 2188 | i--; |
| 2189 | break; |
| 2190 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2191 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2192 | if (i < sh->pd_idx) |
| 2193 | i += raid_disks; |
| 2194 | i -= (sh->pd_idx + 1); |
| 2195 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2196 | case ALGORITHM_PARITY_0: |
| 2197 | i -= 1; |
| 2198 | break; |
| 2199 | case ALGORITHM_PARITY_N: |
| 2200 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2202 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2203 | } |
| 2204 | break; |
| 2205 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2206 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2207 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2208 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2209 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2210 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2211 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2212 | case ALGORITHM_ROTATING_N_RESTART: |
| 2213 | if (sh->pd_idx == raid_disks-1) |
| 2214 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2215 | else if (i > sh->pd_idx) |
| 2216 | i -= 2; /* D D P Q D */ |
| 2217 | break; |
| 2218 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2219 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2220 | if (sh->pd_idx == raid_disks-1) |
| 2221 | i--; /* Q D D D P */ |
| 2222 | else { |
| 2223 | /* D D P Q D */ |
| 2224 | if (i < sh->pd_idx) |
| 2225 | i += raid_disks; |
| 2226 | i -= (sh->pd_idx + 2); |
| 2227 | } |
| 2228 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2229 | case ALGORITHM_PARITY_0: |
| 2230 | i -= 2; |
| 2231 | break; |
| 2232 | case ALGORITHM_PARITY_N: |
| 2233 | break; |
| 2234 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2235 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2236 | if (sh->pd_idx == 0) |
| 2237 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2238 | else { |
| 2239 | /* D D Q P D */ |
| 2240 | if (i < sh->pd_idx) |
| 2241 | i += raid_disks; |
| 2242 | i -= (sh->pd_idx + 1); |
| 2243 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2244 | break; |
| 2245 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2246 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2247 | if (i > sh->pd_idx) |
| 2248 | i--; |
| 2249 | break; |
| 2250 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2251 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2252 | if (i < sh->pd_idx) |
| 2253 | i += data_disks + 1; |
| 2254 | i -= (sh->pd_idx + 1); |
| 2255 | break; |
| 2256 | case ALGORITHM_PARITY_0_6: |
| 2257 | i -= 1; |
| 2258 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2259 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2260 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2261 | } |
| 2262 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2263 | } |
| 2264 | |
| 2265 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2266 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2267 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2268 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2269 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2270 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2271 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2272 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2273 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2274 | return 0; |
| 2275 | } |
| 2276 | return r_sector; |
| 2277 | } |
| 2278 | |
| 2279 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2280 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2281 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2282 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2283 | { |
| 2284 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2285 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2286 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2287 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2288 | if (rcw) { |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2289 | |
| 2290 | for (i = disks; i--; ) { |
| 2291 | struct r5dev *dev = &sh->dev[i]; |
| 2292 | |
| 2293 | if (dev->towrite) { |
| 2294 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2295 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2296 | if (!expand) |
| 2297 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2298 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2299 | } |
| 2300 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 2301 | /* if we are not expanding this is a proper write request, and |
| 2302 | * there will be bios with new data to be drained into the |
| 2303 | * stripe cache |
| 2304 | */ |
| 2305 | if (!expand) { |
| 2306 | if (!s->locked) |
| 2307 | /* False alarm, nothing to do */ |
| 2308 | return; |
| 2309 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2310 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2311 | } else |
| 2312 | sh->reconstruct_state = reconstruct_state_run; |
| 2313 | |
| 2314 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
| 2315 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2316 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2317 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2318 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2319 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2320 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2321 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2322 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2323 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2324 | for (i = disks; i--; ) { |
| 2325 | struct r5dev *dev = &sh->dev[i]; |
| 2326 | if (i == pd_idx) |
| 2327 | continue; |
| 2328 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2329 | if (dev->towrite && |
| 2330 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2331 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2332 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2333 | set_bit(R5_LOCKED, &dev->flags); |
| 2334 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2335 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2336 | } |
| 2337 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 2338 | if (!s->locked) |
| 2339 | /* False alarm - nothing to do */ |
| 2340 | return; |
| 2341 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
| 2342 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2343 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2344 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2345 | } |
| 2346 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2347 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2348 | * are in flight |
| 2349 | */ |
| 2350 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2351 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2352 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2353 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2354 | if (level == 6) { |
| 2355 | int qd_idx = sh->qd_idx; |
| 2356 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2357 | |
| 2358 | set_bit(R5_LOCKED, &dev->flags); |
| 2359 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2360 | s->locked++; |
| 2361 | } |
| 2362 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2363 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2364 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2365 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2366 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2367 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | /* |
| 2369 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2370 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | * The bi_next chain must be in order. |
| 2372 | */ |
| 2373 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2374 | { |
| 2375 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2376 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2377 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2378 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2379 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | (unsigned long long)bi->bi_sector, |
| 2381 | (unsigned long long)sh->sector); |
| 2382 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2383 | /* |
| 2384 | * If several bio share a stripe. The bio bi_phys_segments acts as a |
| 2385 | * reference count to avoid race. The reference count should already be |
| 2386 | * increased before this function is called (for example, in |
| 2387 | * make_request()), so other bio sharing this stripe will not free the |
| 2388 | * stripe. If a stripe is owned by one stripe, the stripe lock will |
| 2389 | * protect it. |
| 2390 | */ |
| 2391 | spin_lock_irq(&sh->stripe_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2392 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2394 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2395 | firstwrite = 1; |
| 2396 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2397 | bip = &sh->dev[dd_idx].toread; |
| 2398 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2399 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2400 | goto overlap; |
| 2401 | bip = & (*bip)->bi_next; |
| 2402 | } |
| 2403 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2404 | goto overlap; |
| 2405 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2406 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2407 | if (*bip) |
| 2408 | bi->bi_next = *bip; |
| 2409 | *bip = bi; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2410 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2411 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2412 | if (forwrite) { |
| 2413 | /* check if page is covered */ |
| 2414 | sector_t sector = sh->dev[dd_idx].sector; |
| 2415 | for (bi=sh->dev[dd_idx].towrite; |
| 2416 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2417 | bi && bi->bi_sector <= sector; |
| 2418 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2419 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2420 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2421 | } |
| 2422 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2423 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2424 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2425 | |
| 2426 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 2427 | (unsigned long long)(*bip)->bi_sector, |
| 2428 | (unsigned long long)sh->sector, dd_idx); |
NeilBrown | b97390a | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2429 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2430 | |
| 2431 | if (conf->mddev->bitmap && firstwrite) { |
| 2432 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2433 | STRIPE_SECTORS, 0); |
| 2434 | sh->bm_seq = conf->seq_flush+1; |
| 2435 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2436 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | return 1; |
| 2438 | |
| 2439 | overlap: |
| 2440 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2441 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | return 0; |
| 2443 | } |
| 2444 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2445 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2446 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2447 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2448 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2449 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2450 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2451 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2452 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2453 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2454 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2455 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2456 | raid5_compute_sector(conf, |
| 2457 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2458 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2459 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2460 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2461 | } |
| 2462 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2463 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2464 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2465 | struct stripe_head_state *s, int disks, |
| 2466 | struct bio **return_bi) |
| 2467 | { |
| 2468 | int i; |
| 2469 | for (i = disks; i--; ) { |
| 2470 | struct bio *bi; |
| 2471 | int bitmap_end = 0; |
| 2472 | |
| 2473 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2474 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2475 | rcu_read_lock(); |
| 2476 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2477 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2478 | atomic_inc(&rdev->nr_pending); |
| 2479 | else |
| 2480 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2481 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2482 | if (rdev) { |
| 2483 | if (!rdev_set_badblocks( |
| 2484 | rdev, |
| 2485 | sh->sector, |
| 2486 | STRIPE_SECTORS, 0)) |
| 2487 | md_error(conf->mddev, rdev); |
| 2488 | rdev_dec_pending(rdev, conf->mddev); |
| 2489 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2490 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2491 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2492 | /* fail all writes first */ |
| 2493 | bi = sh->dev[i].towrite; |
| 2494 | sh->dev[i].towrite = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2495 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 1ed850f | 2012-10-11 13:50:13 +1100 | [diff] [blame] | 2496 | if (bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2497 | bitmap_end = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2498 | |
| 2499 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2500 | wake_up(&conf->wait_for_overlap); |
| 2501 | |
| 2502 | while (bi && bi->bi_sector < |
| 2503 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2504 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2505 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2506 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2507 | md_write_end(conf->mddev); |
| 2508 | bi->bi_next = *return_bi; |
| 2509 | *return_bi = bi; |
| 2510 | } |
| 2511 | bi = nextbi; |
| 2512 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2513 | if (bitmap_end) |
| 2514 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2515 | STRIPE_SECTORS, 0, 0); |
| 2516 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2517 | /* and fail all 'written' */ |
| 2518 | bi = sh->dev[i].written; |
| 2519 | sh->dev[i].written = NULL; |
| 2520 | if (bi) bitmap_end = 1; |
| 2521 | while (bi && bi->bi_sector < |
| 2522 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2523 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2524 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2525 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2526 | md_write_end(conf->mddev); |
| 2527 | bi->bi_next = *return_bi; |
| 2528 | *return_bi = bi; |
| 2529 | } |
| 2530 | bi = bi2; |
| 2531 | } |
| 2532 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2533 | /* fail any reads if this device is non-operational and |
| 2534 | * the data has not reached the cache yet. |
| 2535 | */ |
| 2536 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2537 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2538 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2539 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2540 | bi = sh->dev[i].toread; |
| 2541 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2542 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2543 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2544 | wake_up(&conf->wait_for_overlap); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2545 | while (bi && bi->bi_sector < |
| 2546 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2547 | struct bio *nextbi = |
| 2548 | r5_next_bio(bi, sh->dev[i].sector); |
| 2549 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2550 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2551 | bi->bi_next = *return_bi; |
| 2552 | *return_bi = bi; |
| 2553 | } |
| 2554 | bi = nextbi; |
| 2555 | } |
| 2556 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2557 | if (bitmap_end) |
| 2558 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2559 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2560 | /* If we were in the middle of a write the parity block might |
| 2561 | * still be locked - so just clear all R5_LOCKED flags |
| 2562 | */ |
| 2563 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2564 | } |
| 2565 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2566 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2567 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2568 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2569 | } |
| 2570 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2571 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2572 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2573 | struct stripe_head_state *s) |
| 2574 | { |
| 2575 | int abort = 0; |
| 2576 | int i; |
| 2577 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2578 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 2579 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2580 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2581 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2582 | * Don't even need to abort as that is handled elsewhere |
| 2583 | * if needed, and not always wanted e.g. if there is a known |
| 2584 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2585 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2586 | * non-sync devices, or abort the recovery |
| 2587 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2588 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 2589 | /* During recovery devices cannot be removed, so |
| 2590 | * locking and refcounting of rdevs is not needed |
| 2591 | */ |
| 2592 | for (i = 0; i < conf->raid_disks; i++) { |
| 2593 | struct md_rdev *rdev = conf->disks[i].rdev; |
| 2594 | if (rdev |
| 2595 | && !test_bit(Faulty, &rdev->flags) |
| 2596 | && !test_bit(In_sync, &rdev->flags) |
| 2597 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2598 | STRIPE_SECTORS, 0)) |
| 2599 | abort = 1; |
| 2600 | rdev = conf->disks[i].replacement; |
| 2601 | if (rdev |
| 2602 | && !test_bit(Faulty, &rdev->flags) |
| 2603 | && !test_bit(In_sync, &rdev->flags) |
| 2604 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2605 | STRIPE_SECTORS, 0)) |
| 2606 | abort = 1; |
| 2607 | } |
| 2608 | if (abort) |
| 2609 | conf->recovery_disabled = |
| 2610 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2611 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2612 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2613 | } |
| 2614 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2615 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 2616 | { |
| 2617 | struct md_rdev *rdev; |
| 2618 | int rv = 0; |
| 2619 | /* Doing recovery so rcu locking not required */ |
| 2620 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
| 2621 | if (rdev |
| 2622 | && !test_bit(Faulty, &rdev->flags) |
| 2623 | && !test_bit(In_sync, &rdev->flags) |
| 2624 | && (rdev->recovery_offset <= sh->sector |
| 2625 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 2626 | rv = 1; |
| 2627 | |
| 2628 | return rv; |
| 2629 | } |
| 2630 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2631 | /* fetch_block - checks the given member device to see if its data needs |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2632 | * to be read or computed to satisfy a request. |
| 2633 | * |
| 2634 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2635 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2636 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2637 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2638 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2639 | { |
| 2640 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2641 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2642 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2643 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2644 | /* is the data in this block needed, and can we get it? */ |
| 2645 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2646 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2647 | (dev->toread || |
| 2648 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2649 | s->syncing || s->expanding || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2650 | (s->replacing && want_replace(sh, disk_idx)) || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2651 | (s->failed >= 1 && fdev[0]->toread) || |
| 2652 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2653 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
| 2654 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
| 2655 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2656 | /* we would like to get this block, possibly by computing it, |
| 2657 | * otherwise read it if the backing disk is insync |
| 2658 | */ |
| 2659 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2660 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2661 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2662 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2663 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2664 | /* have disk failed, and we're requested to fetch it; |
| 2665 | * do compute it |
| 2666 | */ |
| 2667 | pr_debug("Computing stripe %llu block %d\n", |
| 2668 | (unsigned long long)sh->sector, disk_idx); |
| 2669 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2670 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2671 | set_bit(R5_Wantcompute, &dev->flags); |
| 2672 | sh->ops.target = disk_idx; |
| 2673 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2674 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2675 | /* Careful: from this point on 'uptodate' is in the eye |
| 2676 | * of raid_run_ops which services 'compute' operations |
| 2677 | * before writes. R5_Wantcompute flags a block that will |
| 2678 | * be R5_UPTODATE by the time it is needed for a |
| 2679 | * subsequent operation. |
| 2680 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2681 | s->uptodate++; |
| 2682 | return 1; |
| 2683 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2684 | /* Computing 2-failure is *very* expensive; only |
| 2685 | * do it if failed >= 2 |
| 2686 | */ |
| 2687 | int other; |
| 2688 | for (other = disks; other--; ) { |
| 2689 | if (other == disk_idx) |
| 2690 | continue; |
| 2691 | if (!test_bit(R5_UPTODATE, |
| 2692 | &sh->dev[other].flags)) |
| 2693 | break; |
| 2694 | } |
| 2695 | BUG_ON(other < 0); |
| 2696 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2697 | (unsigned long long)sh->sector, |
| 2698 | disk_idx, other); |
| 2699 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2700 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2701 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2702 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2703 | sh->ops.target = disk_idx; |
| 2704 | sh->ops.target2 = other; |
| 2705 | s->uptodate += 2; |
| 2706 | s->req_compute = 1; |
| 2707 | return 1; |
| 2708 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2709 | set_bit(R5_LOCKED, &dev->flags); |
| 2710 | set_bit(R5_Wantread, &dev->flags); |
| 2711 | s->locked++; |
| 2712 | pr_debug("Reading block %d (sync=%d)\n", |
| 2713 | disk_idx, s->syncing); |
| 2714 | } |
| 2715 | } |
| 2716 | |
| 2717 | return 0; |
| 2718 | } |
| 2719 | |
| 2720 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2721 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2722 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2723 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2724 | struct stripe_head_state *s, |
| 2725 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2726 | { |
| 2727 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2728 | |
| 2729 | /* look for blocks to read/compute, skip this if a compute |
| 2730 | * is already in flight, or if the stripe contents are in the |
| 2731 | * midst of changing due to a write |
| 2732 | */ |
| 2733 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2734 | !sh->reconstruct_state) |
| 2735 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2736 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2737 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2738 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2739 | } |
| 2740 | |
| 2741 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2742 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2743 | * any written block on an uptodate or failed drive can be returned. |
| 2744 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2745 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2746 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2747 | static void handle_stripe_clean_event(struct r5conf *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2748 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2749 | { |
| 2750 | int i; |
| 2751 | struct r5dev *dev; |
| 2752 | |
| 2753 | for (i = disks; i--; ) |
| 2754 | if (sh->dev[i].written) { |
| 2755 | dev = &sh->dev[i]; |
| 2756 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 2757 | (test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 2758 | test_bit(R5_Discard, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2759 | /* We can return any write requests */ |
| 2760 | struct bio *wbi, *wbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2761 | pr_debug("Return write for disc %d\n", i); |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 2762 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
| 2763 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2764 | wbi = dev->written; |
| 2765 | dev->written = NULL; |
| 2766 | while (wbi && wbi->bi_sector < |
| 2767 | dev->sector + STRIPE_SECTORS) { |
| 2768 | wbi2 = r5_next_bio(wbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2769 | if (!raid5_dec_bi_active_stripes(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2770 | md_write_end(conf->mddev); |
| 2771 | wbi->bi_next = *return_bi; |
| 2772 | *return_bi = wbi; |
| 2773 | } |
| 2774 | wbi = wbi2; |
| 2775 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2776 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2777 | STRIPE_SECTORS, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2778 | !test_bit(STRIPE_DEGRADED, &sh->state), |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2779 | 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2780 | } |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 2781 | } else if (test_bit(R5_Discard, &sh->dev[i].flags)) |
| 2782 | clear_bit(R5_Discard, &sh->dev[i].flags); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2783 | |
| 2784 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2785 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2786 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2787 | } |
| 2788 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2789 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2790 | struct stripe_head *sh, |
| 2791 | struct stripe_head_state *s, |
| 2792 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2793 | { |
| 2794 | int rmw = 0, rcw = 0, i; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2795 | sector_t recovery_cp = conf->mddev->recovery_cp; |
| 2796 | |
| 2797 | /* RAID6 requires 'rcw' in current implementation. |
| 2798 | * Otherwise, check whether resync is now happening or should start. |
| 2799 | * If yes, then the array is dirty (after unclean shutdown or |
| 2800 | * initial creation), so parity in some stripes might be inconsistent. |
| 2801 | * In this case, we need to always do reconstruct-write, to ensure |
| 2802 | * that in case of drive failure or read-error correction, we |
| 2803 | * generate correct data from the parity. |
| 2804 | */ |
| 2805 | if (conf->max_degraded == 2 || |
| 2806 | (recovery_cp < MaxSector && sh->sector >= recovery_cp)) { |
| 2807 | /* Calculate the real rcw later - for now make it |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2808 | * look like rcw is cheaper |
| 2809 | */ |
| 2810 | rcw = 1; rmw = 2; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2811 | pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n", |
| 2812 | conf->max_degraded, (unsigned long long)recovery_cp, |
| 2813 | (unsigned long long)sh->sector); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2814 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2815 | /* would I have to read this buffer for read_modify_write */ |
| 2816 | struct r5dev *dev = &sh->dev[i]; |
| 2817 | if ((dev->towrite || i == sh->pd_idx) && |
| 2818 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2819 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2820 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2821 | if (test_bit(R5_Insync, &dev->flags)) |
| 2822 | rmw++; |
| 2823 | else |
| 2824 | rmw += 2*disks; /* cannot read it */ |
| 2825 | } |
| 2826 | /* Would I have to read this buffer for reconstruct_write */ |
| 2827 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2828 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2829 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2830 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2831 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2832 | else |
| 2833 | rcw += 2*disks; |
| 2834 | } |
| 2835 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2836 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2837 | (unsigned long long)sh->sector, rmw, rcw); |
| 2838 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2839 | if (rmw < rcw && rmw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2840 | /* prefer read-modify-write, but need to get some data */ |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame^] | 2841 | if (conf->mddev->queue) |
| 2842 | blk_add_trace_msg(conf->mddev->queue, |
| 2843 | "raid5 rmw %llu %d", |
| 2844 | (unsigned long long)sh->sector, rmw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2845 | for (i = disks; i--; ) { |
| 2846 | struct r5dev *dev = &sh->dev[i]; |
| 2847 | if ((dev->towrite || i == sh->pd_idx) && |
| 2848 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2849 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2850 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2851 | test_bit(R5_Insync, &dev->flags)) { |
| 2852 | if ( |
| 2853 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2854 | pr_debug("Read_old block " |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2855 | "%d for r-m-w\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2856 | set_bit(R5_LOCKED, &dev->flags); |
| 2857 | set_bit(R5_Wantread, &dev->flags); |
| 2858 | s->locked++; |
| 2859 | } else { |
| 2860 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2861 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2862 | } |
| 2863 | } |
| 2864 | } |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2865 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2866 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2867 | /* want reconstruct write, but need to get some data */ |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2868 | int qread =0; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2869 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2870 | for (i = disks; i--; ) { |
| 2871 | struct r5dev *dev = &sh->dev[i]; |
| 2872 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2873 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2874 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2875 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2876 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2877 | rcw++; |
| 2878 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2879 | continue; /* it's a failed drive */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2880 | if ( |
| 2881 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2882 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2883 | "%d for Reconstruct\n", i); |
| 2884 | set_bit(R5_LOCKED, &dev->flags); |
| 2885 | set_bit(R5_Wantread, &dev->flags); |
| 2886 | s->locked++; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2887 | qread++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2888 | } else { |
| 2889 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2890 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2891 | } |
| 2892 | } |
| 2893 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame^] | 2894 | if (rcw && conf->mddev->queue) |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2895 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
| 2896 | (unsigned long long)sh->sector, |
| 2897 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2898 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2899 | /* now if nothing is locked, and if we have enough data, |
| 2900 | * we can start a write request |
| 2901 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2902 | /* since handle_stripe can be called at any time we need to handle the |
| 2903 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2904 | * subsequent call wants to start a write request. raid_run_ops only |
| 2905 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2906 | * simultaneously. If this is not the case then new writes need to be |
| 2907 | * held off until the compute completes. |
| 2908 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2909 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2910 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2911 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2912 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2913 | } |
| 2914 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2915 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2916 | struct stripe_head_state *s, int disks) |
| 2917 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2918 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2919 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2920 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2921 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2922 | switch (sh->check_state) { |
| 2923 | case check_state_idle: |
| 2924 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2925 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2926 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2927 | sh->check_state = check_state_run; |
| 2928 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2929 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2930 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2931 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2932 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2933 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2934 | /* fall through */ |
| 2935 | case check_state_compute_result: |
| 2936 | sh->check_state = check_state_idle; |
| 2937 | if (!dev) |
| 2938 | dev = &sh->dev[sh->pd_idx]; |
| 2939 | |
| 2940 | /* check that a write has not made the stripe insync */ |
| 2941 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2942 | break; |
| 2943 | |
| 2944 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2945 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2946 | BUG_ON(s->uptodate != disks); |
| 2947 | |
| 2948 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2949 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2950 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2951 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2952 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2953 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2954 | break; |
| 2955 | case check_state_run: |
| 2956 | break; /* we will be called again upon completion */ |
| 2957 | case check_state_check_result: |
| 2958 | sh->check_state = check_state_idle; |
| 2959 | |
| 2960 | /* if a failure occurred during the check operation, leave |
| 2961 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2962 | */ |
| 2963 | if (s->failed) |
| 2964 | break; |
| 2965 | |
| 2966 | /* handle a successful check operation, if parity is correct |
| 2967 | * we are done. Otherwise update the mismatch count and repair |
| 2968 | * parity if !MD_RECOVERY_CHECK |
| 2969 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2970 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2971 | /* parity is correct (on disc, |
| 2972 | * not in buffer any more) |
| 2973 | */ |
| 2974 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2975 | else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 2976 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2977 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2978 | /* don't try to repair!! */ |
| 2979 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2980 | else { |
| 2981 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2982 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2983 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2984 | set_bit(R5_Wantcompute, |
| 2985 | &sh->dev[sh->pd_idx].flags); |
| 2986 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2987 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2988 | s->uptodate++; |
| 2989 | } |
| 2990 | } |
| 2991 | break; |
| 2992 | case check_state_compute_run: |
| 2993 | break; |
| 2994 | default: |
| 2995 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 2996 | __func__, sh->check_state, |
| 2997 | (unsigned long long) sh->sector); |
| 2998 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2999 | } |
| 3000 | } |
| 3001 | |
| 3002 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3003 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3004 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3005 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3006 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3007 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3008 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3009 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3010 | |
| 3011 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3012 | |
| 3013 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3014 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3015 | /* Want to check and possibly repair P and Q. |
| 3016 | * However there could be one 'failed' device, in which |
| 3017 | * case we can only check one of them, possibly using the |
| 3018 | * other to generate missing data |
| 3019 | */ |
| 3020 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3021 | switch (sh->check_state) { |
| 3022 | case check_state_idle: |
| 3023 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3024 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3025 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3026 | * makes sense to check P (If anything else were failed, |
| 3027 | * we would have used P to recreate it). |
| 3028 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3029 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3030 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3031 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3032 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3033 | * anything, so it makes sense to check it |
| 3034 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3035 | if (sh->check_state == check_state_run) |
| 3036 | sh->check_state = check_state_run_pq; |
| 3037 | else |
| 3038 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3039 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3040 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3041 | /* discard potentially stale zero_sum_result */ |
| 3042 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3043 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3044 | if (sh->check_state == check_state_run) { |
| 3045 | /* async_xor_zero_sum destroys the contents of P */ |
| 3046 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 3047 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3048 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3049 | if (sh->check_state >= check_state_run && |
| 3050 | sh->check_state <= check_state_run_pq) { |
| 3051 | /* async_syndrome_zero_sum preserves P and Q, so |
| 3052 | * no need to mark them !uptodate here |
| 3053 | */ |
| 3054 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 3055 | break; |
| 3056 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3057 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3058 | /* we have 2-disk failure */ |
| 3059 | BUG_ON(s->failed != 2); |
| 3060 | /* fall through */ |
| 3061 | case check_state_compute_result: |
| 3062 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3063 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3064 | /* check that a write has not made the stripe insync */ |
| 3065 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3066 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3067 | |
| 3068 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3069 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3070 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3071 | BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3072 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3073 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3074 | s->locked++; |
| 3075 | set_bit(R5_LOCKED, &dev->flags); |
| 3076 | set_bit(R5_Wantwrite, &dev->flags); |
| 3077 | } |
| 3078 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3079 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3080 | s->locked++; |
| 3081 | set_bit(R5_LOCKED, &dev->flags); |
| 3082 | set_bit(R5_Wantwrite, &dev->flags); |
| 3083 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3084 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3085 | dev = &sh->dev[pd_idx]; |
| 3086 | s->locked++; |
| 3087 | set_bit(R5_LOCKED, &dev->flags); |
| 3088 | set_bit(R5_Wantwrite, &dev->flags); |
| 3089 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3090 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3091 | dev = &sh->dev[qd_idx]; |
| 3092 | s->locked++; |
| 3093 | set_bit(R5_LOCKED, &dev->flags); |
| 3094 | set_bit(R5_Wantwrite, &dev->flags); |
| 3095 | } |
| 3096 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3097 | |
| 3098 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3099 | break; |
| 3100 | case check_state_run: |
| 3101 | case check_state_run_q: |
| 3102 | case check_state_run_pq: |
| 3103 | break; /* we will be called again upon completion */ |
| 3104 | case check_state_check_result: |
| 3105 | sh->check_state = check_state_idle; |
| 3106 | |
| 3107 | /* handle a successful check operation, if parity is correct |
| 3108 | * we are done. Otherwise update the mismatch count and repair |
| 3109 | * parity if !MD_RECOVERY_CHECK |
| 3110 | */ |
| 3111 | if (sh->ops.zero_sum_result == 0) { |
| 3112 | /* both parities are correct */ |
| 3113 | if (!s->failed) |
| 3114 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3115 | else { |
| 3116 | /* in contrast to the raid5 case we can validate |
| 3117 | * parity, but still have a failure to write |
| 3118 | * back |
| 3119 | */ |
| 3120 | sh->check_state = check_state_compute_result; |
| 3121 | /* Returning at this point means that we may go |
| 3122 | * off and bring p and/or q uptodate again so |
| 3123 | * we make sure to check zero_sum_result again |
| 3124 | * to verify if p or q need writeback |
| 3125 | */ |
| 3126 | } |
| 3127 | } else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 3128 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3129 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3130 | /* don't try to repair!! */ |
| 3131 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3132 | else { |
| 3133 | int *target = &sh->ops.target; |
| 3134 | |
| 3135 | sh->ops.target = -1; |
| 3136 | sh->ops.target2 = -1; |
| 3137 | sh->check_state = check_state_compute_run; |
| 3138 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3139 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3140 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3141 | set_bit(R5_Wantcompute, |
| 3142 | &sh->dev[pd_idx].flags); |
| 3143 | *target = pd_idx; |
| 3144 | target = &sh->ops.target2; |
| 3145 | s->uptodate++; |
| 3146 | } |
| 3147 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3148 | set_bit(R5_Wantcompute, |
| 3149 | &sh->dev[qd_idx].flags); |
| 3150 | *target = qd_idx; |
| 3151 | s->uptodate++; |
| 3152 | } |
| 3153 | } |
| 3154 | } |
| 3155 | break; |
| 3156 | case check_state_compute_run: |
| 3157 | break; |
| 3158 | default: |
| 3159 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3160 | __func__, sh->check_state, |
| 3161 | (unsigned long long) sh->sector); |
| 3162 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3163 | } |
| 3164 | } |
| 3165 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3166 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3167 | { |
| 3168 | int i; |
| 3169 | |
| 3170 | /* We have read all the blocks in this stripe and now we need to |
| 3171 | * copy some of them into a target stripe for expand. |
| 3172 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3173 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3174 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3175 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3176 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3177 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3178 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3179 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3180 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3181 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3182 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3183 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3184 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3185 | if (sh2 == NULL) |
| 3186 | /* so far only the early blocks of this stripe |
| 3187 | * have been requested. When later blocks |
| 3188 | * get requested, we will try again |
| 3189 | */ |
| 3190 | continue; |
| 3191 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3192 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3193 | /* must have already done this block */ |
| 3194 | release_stripe(sh2); |
| 3195 | continue; |
| 3196 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3197 | |
| 3198 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3199 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3200 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3201 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3202 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3203 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3204 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3205 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3206 | for (j = 0; j < conf->raid_disks; j++) |
| 3207 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3208 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3209 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3210 | break; |
| 3211 | if (j == conf->raid_disks) { |
| 3212 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3213 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3214 | } |
| 3215 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3216 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3217 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3218 | /* done submitting copies, wait for them to complete */ |
NeilBrown | 749586b | 2012-11-20 14:11:15 +1100 | [diff] [blame] | 3219 | async_tx_quiesce(&tx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3220 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | |
| 3222 | /* |
| 3223 | * handle_stripe - do things to a stripe. |
| 3224 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3225 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 3226 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3227 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3228 | * return some read requests which now have data |
| 3229 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3230 | * schedule a read on some buffers |
| 3231 | * schedule a write of some buffers |
| 3232 | * return confirmation of parity correctness |
| 3233 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3234 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3235 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3236 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3237 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3238 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3239 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3240 | struct r5dev *dev; |
| 3241 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3242 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3243 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3244 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3245 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3246 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3247 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3248 | s->failed_num[0] = -1; |
| 3249 | s->failed_num[1] = -1; |
| 3250 | |
| 3251 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3252 | rcu_read_lock(); |
| 3253 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3254 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3255 | sector_t first_bad; |
| 3256 | int bad_sectors; |
| 3257 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3258 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3259 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3260 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3261 | pr_debug("check %d: state 0x%lx read %p write %p written %p\n", |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3262 | i, dev->flags, |
| 3263 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3264 | /* maybe we can reply to a read |
| 3265 | * |
| 3266 | * new wantfill requests are only permitted while |
| 3267 | * ops_complete_biofill is guaranteed to be inactive |
| 3268 | */ |
| 3269 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3270 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3271 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3272 | |
| 3273 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3274 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3275 | s->locked++; |
| 3276 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3277 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3278 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3279 | s->compute++; |
| 3280 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3281 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3282 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3283 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3284 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3285 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3286 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3287 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3288 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3289 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3290 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3291 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3292 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3293 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3294 | /* Prefer to use the replacement for reads, but only |
| 3295 | * if it is recovered enough and has no bad blocks. |
| 3296 | */ |
| 3297 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 3298 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 3299 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 3300 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3301 | &first_bad, &bad_sectors)) |
| 3302 | set_bit(R5_ReadRepl, &dev->flags); |
| 3303 | else { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3304 | if (rdev) |
| 3305 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3306 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 3307 | clear_bit(R5_ReadRepl, &dev->flags); |
| 3308 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 3309 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 3310 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3311 | if (rdev) { |
| 3312 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3313 | &first_bad, &bad_sectors); |
| 3314 | if (s->blocked_rdev == NULL |
| 3315 | && (test_bit(Blocked, &rdev->flags) |
| 3316 | || is_bad < 0)) { |
| 3317 | if (is_bad < 0) |
| 3318 | set_bit(BlockedBadBlocks, |
| 3319 | &rdev->flags); |
| 3320 | s->blocked_rdev = rdev; |
| 3321 | atomic_inc(&rdev->nr_pending); |
| 3322 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3323 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3324 | clear_bit(R5_Insync, &dev->flags); |
| 3325 | if (!rdev) |
| 3326 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3327 | else if (is_bad) { |
| 3328 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3329 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 3330 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3331 | /* treat as in-sync, but with a read error |
| 3332 | * which we can now try to correct |
| 3333 | */ |
| 3334 | set_bit(R5_Insync, &dev->flags); |
| 3335 | set_bit(R5_ReadError, &dev->flags); |
| 3336 | } |
| 3337 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3338 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3339 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3340 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3341 | set_bit(R5_Insync, &dev->flags); |
| 3342 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 3343 | test_bit(R5_Expanded, &dev->flags)) |
| 3344 | /* If we've reshaped into here, we assume it is Insync. |
| 3345 | * We will shortly update recovery_offset to make |
| 3346 | * it official. |
| 3347 | */ |
| 3348 | set_bit(R5_Insync, &dev->flags); |
| 3349 | |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3350 | if (rdev && test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3351 | /* This flag does not apply to '.replacement' |
| 3352 | * only to .rdev, so make sure to check that*/ |
| 3353 | struct md_rdev *rdev2 = rcu_dereference( |
| 3354 | conf->disks[i].rdev); |
| 3355 | if (rdev2 == rdev) |
| 3356 | clear_bit(R5_Insync, &dev->flags); |
| 3357 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3358 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3359 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3360 | } else |
| 3361 | clear_bit(R5_WriteError, &dev->flags); |
| 3362 | } |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3363 | if (rdev && test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3364 | /* This flag does not apply to '.replacement' |
| 3365 | * only to .rdev, so make sure to check that*/ |
| 3366 | struct md_rdev *rdev2 = rcu_dereference( |
| 3367 | conf->disks[i].rdev); |
| 3368 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3369 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3370 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3371 | } else |
| 3372 | clear_bit(R5_MadeGood, &dev->flags); |
| 3373 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3374 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3375 | struct md_rdev *rdev2 = rcu_dereference( |
| 3376 | conf->disks[i].replacement); |
| 3377 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 3378 | s->handle_bad_blocks = 1; |
| 3379 | atomic_inc(&rdev2->nr_pending); |
| 3380 | } else |
| 3381 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 3382 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3383 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3384 | /* The ReadError flag will just be confusing now */ |
| 3385 | clear_bit(R5_ReadError, &dev->flags); |
| 3386 | clear_bit(R5_ReWrite, &dev->flags); |
| 3387 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3388 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3389 | clear_bit(R5_Insync, &dev->flags); |
| 3390 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3391 | if (s->failed < 2) |
| 3392 | s->failed_num[s->failed] = i; |
| 3393 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3394 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 3395 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3396 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3397 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3398 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 3399 | /* If there is a failed device being replaced, |
| 3400 | * we must be recovering. |
| 3401 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3402 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3403 | * else we can only be replacing |
| 3404 | * sync and recovery both need to read all devices, and so |
| 3405 | * use the same flag. |
| 3406 | */ |
| 3407 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3408 | sh->sector >= conf->mddev->recovery_cp || |
| 3409 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3410 | s->syncing = 1; |
| 3411 | else |
| 3412 | s->replacing = 1; |
| 3413 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3414 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3415 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3416 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3417 | static void handle_stripe(struct stripe_head *sh) |
| 3418 | { |
| 3419 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3420 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3421 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3422 | int prexor; |
| 3423 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3424 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3425 | |
| 3426 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3427 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3428 | /* already being handled, ensure it gets handled |
| 3429 | * again when current action finishes */ |
| 3430 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3431 | return; |
| 3432 | } |
| 3433 | |
| 3434 | if (test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3435 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3436 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3437 | } |
| 3438 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3439 | |
| 3440 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3441 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3442 | (unsigned long long)sh->sector, sh->state, |
| 3443 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3444 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3445 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3446 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3447 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3448 | if (s.handle_bad_blocks) { |
| 3449 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3450 | goto finish; |
| 3451 | } |
| 3452 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3453 | if (unlikely(s.blocked_rdev)) { |
| 3454 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3455 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3456 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3457 | goto finish; |
| 3458 | } |
| 3459 | /* There is nothing for the blocked_rdev to block */ |
| 3460 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3461 | s.blocked_rdev = NULL; |
| 3462 | } |
| 3463 | |
| 3464 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3465 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3466 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3467 | } |
| 3468 | |
| 3469 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3470 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3471 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3472 | s.failed_num[0], s.failed_num[1]); |
| 3473 | /* check if the array has lost more than max_degraded devices and, |
| 3474 | * if so, some requests might need to be failed. |
| 3475 | */ |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3476 | if (s.failed > conf->max_degraded) { |
| 3477 | sh->check_state = 0; |
| 3478 | sh->reconstruct_state = 0; |
| 3479 | if (s.to_read+s.to_write+s.written) |
| 3480 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3481 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3482 | handle_failed_sync(conf, sh, &s); |
| 3483 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3484 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3485 | /* Now we check to see if any write operations have recently |
| 3486 | * completed |
| 3487 | */ |
| 3488 | prexor = 0; |
| 3489 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3490 | prexor = 1; |
| 3491 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3492 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3493 | sh->reconstruct_state = reconstruct_state_idle; |
| 3494 | |
| 3495 | /* All the 'written' buffers and the parity block are ready to |
| 3496 | * be written back to disk |
| 3497 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3498 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 3499 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3500 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3501 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 3502 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3503 | for (i = disks; i--; ) { |
| 3504 | struct r5dev *dev = &sh->dev[i]; |
| 3505 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3506 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3507 | dev->written)) { |
| 3508 | pr_debug("Writing block %d\n", i); |
| 3509 | set_bit(R5_Wantwrite, &dev->flags); |
| 3510 | if (prexor) |
| 3511 | continue; |
| 3512 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3513 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3514 | s.failed == 0)) |
| 3515 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3516 | } |
| 3517 | } |
| 3518 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3519 | s.dec_preread_active = 1; |
| 3520 | } |
| 3521 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 3522 | /* |
| 3523 | * might be able to return some write requests if the parity blocks |
| 3524 | * are safe, or on a failed drive |
| 3525 | */ |
| 3526 | pdev = &sh->dev[sh->pd_idx]; |
| 3527 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3528 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3529 | qdev = &sh->dev[sh->qd_idx]; |
| 3530 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3531 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3532 | || conf->level < 6; |
| 3533 | |
| 3534 | if (s.written && |
| 3535 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3536 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 3537 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 3538 | test_bit(R5_Discard, &pdev->flags))))) && |
| 3539 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3540 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 3541 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 3542 | test_bit(R5_Discard, &qdev->flags)))))) |
| 3543 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3544 | |
| 3545 | /* Now we might consider reading some blocks, either to check/generate |
| 3546 | * parity, or to satisfy requests |
| 3547 | * or to load a block that is being partially written. |
| 3548 | */ |
| 3549 | if (s.to_read || s.non_overwrite |
| 3550 | || (conf->level == 6 && s.to_write && s.failed) |
| 3551 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 3552 | || s.replacing |
| 3553 | || s.expanding) |
| 3554 | handle_stripe_fill(sh, &s, disks); |
| 3555 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3556 | /* Now to consider new write requests and what else, if anything |
| 3557 | * should be read. We do not handle new writes when: |
| 3558 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3559 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3560 | * block. |
| 3561 | */ |
| 3562 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3563 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3564 | |
| 3565 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3566 | * Any reads will already have been scheduled, so we just see if enough |
| 3567 | * data is available. The parity check is held off while parity |
| 3568 | * dependent operations are in flight. |
| 3569 | */ |
| 3570 | if (sh->check_state || |
| 3571 | (s.syncing && s.locked == 0 && |
| 3572 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3573 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3574 | if (conf->level == 6) |
| 3575 | handle_parity_checks6(conf, sh, &s, disks); |
| 3576 | else |
| 3577 | handle_parity_checks5(conf, sh, &s, disks); |
| 3578 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3579 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3580 | if (s.replacing && s.locked == 0 |
| 3581 | && !test_bit(STRIPE_INSYNC, &sh->state)) { |
| 3582 | /* Write out to replacement devices where possible */ |
| 3583 | for (i = 0; i < conf->raid_disks; i++) |
| 3584 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags) && |
| 3585 | test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 3586 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 3587 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3588 | s.locked++; |
| 3589 | } |
| 3590 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3591 | } |
| 3592 | if ((s.syncing || s.replacing) && s.locked == 0 && |
| 3593 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3594 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3595 | clear_bit(STRIPE_SYNCING, &sh->state); |
| 3596 | } |
| 3597 | |
| 3598 | /* If the failed drives are just a ReadError, then we might need |
| 3599 | * to progress the repair/check process |
| 3600 | */ |
| 3601 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3602 | for (i = 0; i < s.failed; i++) { |
| 3603 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3604 | if (test_bit(R5_ReadError, &dev->flags) |
| 3605 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3606 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3607 | ) { |
| 3608 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3609 | set_bit(R5_Wantwrite, &dev->flags); |
| 3610 | set_bit(R5_ReWrite, &dev->flags); |
| 3611 | set_bit(R5_LOCKED, &dev->flags); |
| 3612 | s.locked++; |
| 3613 | } else { |
| 3614 | /* let's read it back */ |
| 3615 | set_bit(R5_Wantread, &dev->flags); |
| 3616 | set_bit(R5_LOCKED, &dev->flags); |
| 3617 | s.locked++; |
| 3618 | } |
| 3619 | } |
| 3620 | } |
| 3621 | |
| 3622 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3623 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3624 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3625 | struct stripe_head *sh_src |
| 3626 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3627 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3628 | /* sh cannot be written until sh_src has been read. |
| 3629 | * so arrange for sh to be delayed a little |
| 3630 | */ |
| 3631 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3632 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3633 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3634 | &sh_src->state)) |
| 3635 | atomic_inc(&conf->preread_active_stripes); |
| 3636 | release_stripe(sh_src); |
| 3637 | goto finish; |
| 3638 | } |
| 3639 | if (sh_src) |
| 3640 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3641 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3642 | sh->reconstruct_state = reconstruct_state_idle; |
| 3643 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3644 | for (i = conf->raid_disks; i--; ) { |
| 3645 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3646 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3647 | s.locked++; |
| 3648 | } |
| 3649 | } |
| 3650 | |
| 3651 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3652 | !sh->reconstruct_state) { |
| 3653 | /* Need to write out all blocks after computing parity */ |
| 3654 | sh->disks = conf->raid_disks; |
| 3655 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3656 | schedule_reconstruction(sh, &s, 1, 1); |
| 3657 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3658 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3659 | atomic_dec(&conf->reshape_stripes); |
| 3660 | wake_up(&conf->wait_for_overlap); |
| 3661 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3662 | } |
| 3663 | |
| 3664 | if (s.expanding && s.locked == 0 && |
| 3665 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3666 | handle_stripe_expansion(conf, sh); |
| 3667 | |
| 3668 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3669 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 3670 | if (unlikely(s.blocked_rdev)) { |
| 3671 | if (conf->mddev->external) |
| 3672 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 3673 | conf->mddev); |
| 3674 | else |
| 3675 | /* Internal metadata will immediately |
| 3676 | * be written by raid5d, so we don't |
| 3677 | * need to wait here. |
| 3678 | */ |
| 3679 | rdev_dec_pending(s.blocked_rdev, |
| 3680 | conf->mddev); |
| 3681 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3682 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3683 | if (s.handle_bad_blocks) |
| 3684 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3685 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3686 | struct r5dev *dev = &sh->dev[i]; |
| 3687 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 3688 | /* We own a safe reference to the rdev */ |
| 3689 | rdev = conf->disks[i].rdev; |
| 3690 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 3691 | STRIPE_SECTORS, 0)) |
| 3692 | md_error(conf->mddev, rdev); |
| 3693 | rdev_dec_pending(rdev, conf->mddev); |
| 3694 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3695 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 3696 | rdev = conf->disks[i].rdev; |
| 3697 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3698 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3699 | rdev_dec_pending(rdev, conf->mddev); |
| 3700 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3701 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3702 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3703 | if (!rdev) |
| 3704 | /* rdev have been moved down */ |
| 3705 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3706 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3707 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3708 | rdev_dec_pending(rdev, conf->mddev); |
| 3709 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3710 | } |
| 3711 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3712 | if (s.ops_request) |
| 3713 | raid_run_ops(sh, s.ops_request); |
| 3714 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3715 | ops_run_io(sh, &s); |
| 3716 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3717 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3718 | /* We delay this until after ops_run_io so that if make_request |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 3719 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3720 | * have actually been submitted. |
| 3721 | */ |
| 3722 | atomic_dec(&conf->preread_active_stripes); |
| 3723 | if (atomic_read(&conf->preread_active_stripes) < |
| 3724 | IO_THRESHOLD) |
| 3725 | md_wakeup_thread(conf->mddev->thread); |
| 3726 | } |
| 3727 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3728 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3729 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3730 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3731 | } |
| 3732 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3733 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3734 | { |
| 3735 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3736 | while (!list_empty(&conf->delayed_list)) { |
| 3737 | struct list_head *l = conf->delayed_list.next; |
| 3738 | struct stripe_head *sh; |
| 3739 | sh = list_entry(l, struct stripe_head, lru); |
| 3740 | list_del_init(l); |
| 3741 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3742 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3743 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3744 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3745 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3746 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3747 | } |
| 3748 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3749 | static void activate_bit_delay(struct r5conf *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3750 | { |
| 3751 | /* device_lock is held */ |
| 3752 | struct list_head head; |
| 3753 | list_add(&head, &conf->bitmap_list); |
| 3754 | list_del_init(&conf->bitmap_list); |
| 3755 | while (!list_empty(&head)) { |
| 3756 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3757 | list_del_init(&sh->lru); |
| 3758 | atomic_inc(&sh->count); |
| 3759 | __release_stripe(conf, sh); |
| 3760 | } |
| 3761 | } |
| 3762 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3763 | int md_raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3764 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3765 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3766 | |
| 3767 | /* No difference between reads and writes. Just check |
| 3768 | * how busy the stripe_cache is |
| 3769 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3770 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3771 | if (conf->inactive_blocked) |
| 3772 | return 1; |
| 3773 | if (conf->quiesce) |
| 3774 | return 1; |
| 3775 | if (list_empty_careful(&conf->inactive_list)) |
| 3776 | return 1; |
| 3777 | |
| 3778 | return 0; |
| 3779 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3780 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 3781 | |
| 3782 | static int raid5_congested(void *data, int bits) |
| 3783 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3784 | struct mddev *mddev = data; |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3785 | |
| 3786 | return mddev_congested(mddev, bits) || |
| 3787 | md_raid5_congested(mddev, bits); |
| 3788 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3789 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3790 | /* We want read requests to align with chunks where possible, |
| 3791 | * but write requests don't need to. |
| 3792 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3793 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3794 | struct bvec_merge_data *bvm, |
| 3795 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3796 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3797 | struct mddev *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3798 | sector_t sector = bvm->bi_sector + get_start_sect(bvm->bi_bdev); |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3799 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3800 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3801 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3802 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3803 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3804 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3805 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3806 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3807 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3808 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3809 | if (max < 0) max = 0; |
| 3810 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3811 | return biovec->bv_len; |
| 3812 | else |
| 3813 | return max; |
| 3814 | } |
| 3815 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3816 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3817 | static int in_chunk_boundary(struct mddev *mddev, struct bio *bio) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3818 | { |
| 3819 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3820 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3821 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3822 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3823 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3824 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3825 | return chunk_sectors >= |
| 3826 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3827 | } |
| 3828 | |
| 3829 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3830 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3831 | * later sampled by raid5d. |
| 3832 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3833 | static void add_bio_to_retry(struct bio *bi,struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3834 | { |
| 3835 | unsigned long flags; |
| 3836 | |
| 3837 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3838 | |
| 3839 | bi->bi_next = conf->retry_read_aligned_list; |
| 3840 | conf->retry_read_aligned_list = bi; |
| 3841 | |
| 3842 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3843 | md_wakeup_thread(conf->mddev->thread); |
| 3844 | } |
| 3845 | |
| 3846 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3847 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3848 | { |
| 3849 | struct bio *bi; |
| 3850 | |
| 3851 | bi = conf->retry_read_aligned; |
| 3852 | if (bi) { |
| 3853 | conf->retry_read_aligned = NULL; |
| 3854 | return bi; |
| 3855 | } |
| 3856 | bi = conf->retry_read_aligned_list; |
| 3857 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3858 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3859 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3860 | /* |
| 3861 | * this sets the active strip count to 1 and the processed |
| 3862 | * strip count to zero (upper 8 bits) |
| 3863 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3864 | raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3865 | } |
| 3866 | |
| 3867 | return bi; |
| 3868 | } |
| 3869 | |
| 3870 | |
| 3871 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3872 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3873 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3874 | * first). |
| 3875 | * If the read failed.. |
| 3876 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3877 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3878 | { |
| 3879 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3880 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3881 | struct r5conf *conf; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3882 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3883 | struct md_rdev *rdev; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3884 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3885 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3886 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3887 | rdev = (void*)raid_bi->bi_next; |
| 3888 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3889 | mddev = rdev->mddev; |
| 3890 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3891 | |
| 3892 | rdev_dec_pending(rdev, conf->mddev); |
| 3893 | |
| 3894 | if (!error && uptodate) { |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3895 | trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev), |
| 3896 | raid_bi, 0); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3897 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3898 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3899 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3900 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3901 | } |
| 3902 | |
| 3903 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3904 | pr_debug("raid5_align_endio : io error...handing IO for a retry\n"); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3905 | |
| 3906 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3907 | } |
| 3908 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3909 | static int bio_fits_rdev(struct bio *bi) |
| 3910 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3911 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3912 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3913 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3914 | return 0; |
| 3915 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3916 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3917 | return 0; |
| 3918 | |
| 3919 | if (q->merge_bvec_fn) |
| 3920 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3921 | * just just give up |
| 3922 | */ |
| 3923 | return 0; |
| 3924 | |
| 3925 | return 1; |
| 3926 | } |
| 3927 | |
| 3928 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3929 | static int chunk_aligned_read(struct mddev *mddev, struct bio * raid_bio) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3930 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3931 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3932 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3933 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3934 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3935 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3936 | |
| 3937 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3938 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3939 | return 0; |
| 3940 | } |
| 3941 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3942 | * use bio_clone_mddev to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3943 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3944 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3945 | if (!align_bi) |
| 3946 | return 0; |
| 3947 | /* |
| 3948 | * set bi_end_io to a new function, and set bi_private to the |
| 3949 | * original bio. |
| 3950 | */ |
| 3951 | align_bi->bi_end_io = raid5_align_endio; |
| 3952 | align_bi->bi_private = raid_bio; |
| 3953 | /* |
| 3954 | * compute position |
| 3955 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3956 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3957 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3958 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3959 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3960 | end_sector = align_bi->bi_sector + (align_bi->bi_size >> 9); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3961 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3962 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 3963 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 3964 | rdev->recovery_offset < end_sector) { |
| 3965 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3966 | if (rdev && |
| 3967 | (test_bit(Faulty, &rdev->flags) || |
| 3968 | !(test_bit(In_sync, &rdev->flags) || |
| 3969 | rdev->recovery_offset >= end_sector))) |
| 3970 | rdev = NULL; |
| 3971 | } |
| 3972 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3973 | sector_t first_bad; |
| 3974 | int bad_sectors; |
| 3975 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3976 | atomic_inc(&rdev->nr_pending); |
| 3977 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3978 | raid_bio->bi_next = (void*)rdev; |
| 3979 | align_bi->bi_bdev = rdev->bdev; |
| 3980 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3981 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3982 | if (!bio_fits_rdev(align_bi) || |
| 3983 | is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, |
| 3984 | &first_bad, &bad_sectors)) { |
| 3985 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3986 | bio_put(align_bi); |
| 3987 | rdev_dec_pending(rdev, mddev); |
| 3988 | return 0; |
| 3989 | } |
| 3990 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 3991 | /* No reshape active, so we can trust rdev->data_offset */ |
| 3992 | align_bi->bi_sector += rdev->data_offset; |
| 3993 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3994 | spin_lock_irq(&conf->device_lock); |
| 3995 | wait_event_lock_irq(conf->wait_for_stripe, |
| 3996 | conf->quiesce == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 3997 | conf->device_lock); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3998 | atomic_inc(&conf->active_aligned_reads); |
| 3999 | spin_unlock_irq(&conf->device_lock); |
| 4000 | |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame^] | 4001 | if (mddev->gendisk) |
| 4002 | trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), |
| 4003 | align_bi, disk_devt(mddev->gendisk), |
| 4004 | raid_bio->bi_sector); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4005 | generic_make_request(align_bi); |
| 4006 | return 1; |
| 4007 | } else { |
| 4008 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4009 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4010 | return 0; |
| 4011 | } |
| 4012 | } |
| 4013 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4014 | /* __get_priority_stripe - get the next stripe to process |
| 4015 | * |
| 4016 | * Full stripe writes are allowed to pass preread active stripes up until |
| 4017 | * the bypass_threshold is exceeded. In general the bypass_count |
| 4018 | * increments when the handle_list is handled before the hold_list; however, it |
| 4019 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 4020 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 4021 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 4022 | * handle_list. |
| 4023 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4024 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4025 | { |
| 4026 | struct stripe_head *sh; |
| 4027 | |
| 4028 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 4029 | __func__, |
| 4030 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 4031 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 4032 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 4033 | |
| 4034 | if (!list_empty(&conf->handle_list)) { |
| 4035 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 4036 | |
| 4037 | if (list_empty(&conf->hold_list)) |
| 4038 | conf->bypass_count = 0; |
| 4039 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 4040 | if (conf->hold_list.next == conf->last_hold) |
| 4041 | conf->bypass_count++; |
| 4042 | else { |
| 4043 | conf->last_hold = conf->hold_list.next; |
| 4044 | conf->bypass_count -= conf->bypass_threshold; |
| 4045 | if (conf->bypass_count < 0) |
| 4046 | conf->bypass_count = 0; |
| 4047 | } |
| 4048 | } |
| 4049 | } else if (!list_empty(&conf->hold_list) && |
| 4050 | ((conf->bypass_threshold && |
| 4051 | conf->bypass_count > conf->bypass_threshold) || |
| 4052 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 4053 | sh = list_entry(conf->hold_list.next, |
| 4054 | typeof(*sh), lru); |
| 4055 | conf->bypass_count -= conf->bypass_threshold; |
| 4056 | if (conf->bypass_count < 0) |
| 4057 | conf->bypass_count = 0; |
| 4058 | } else |
| 4059 | return NULL; |
| 4060 | |
| 4061 | list_del_init(&sh->lru); |
| 4062 | atomic_inc(&sh->count); |
| 4063 | BUG_ON(atomic_read(&sh->count) != 1); |
| 4064 | return sh; |
| 4065 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4066 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4067 | struct raid5_plug_cb { |
| 4068 | struct blk_plug_cb cb; |
| 4069 | struct list_head list; |
| 4070 | }; |
| 4071 | |
| 4072 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 4073 | { |
| 4074 | struct raid5_plug_cb *cb = container_of( |
| 4075 | blk_cb, struct raid5_plug_cb, cb); |
| 4076 | struct stripe_head *sh; |
| 4077 | struct mddev *mddev = cb->cb.data; |
| 4078 | struct r5conf *conf = mddev->private; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4079 | int cnt = 0; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4080 | |
| 4081 | if (cb->list.next && !list_empty(&cb->list)) { |
| 4082 | spin_lock_irq(&conf->device_lock); |
| 4083 | while (!list_empty(&cb->list)) { |
| 4084 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 4085 | list_del_init(&sh->lru); |
| 4086 | /* |
| 4087 | * avoid race release_stripe_plug() sees |
| 4088 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 4089 | * is still in our list |
| 4090 | */ |
| 4091 | smp_mb__before_clear_bit(); |
| 4092 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
| 4093 | __release_stripe(conf, sh); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4094 | cnt++; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4095 | } |
| 4096 | spin_unlock_irq(&conf->device_lock); |
| 4097 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame^] | 4098 | if (mddev->queue) |
| 4099 | trace_block_unplug(mddev->queue, cnt, !from_schedule); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4100 | kfree(cb); |
| 4101 | } |
| 4102 | |
| 4103 | static void release_stripe_plug(struct mddev *mddev, |
| 4104 | struct stripe_head *sh) |
| 4105 | { |
| 4106 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 4107 | raid5_unplug, mddev, |
| 4108 | sizeof(struct raid5_plug_cb)); |
| 4109 | struct raid5_plug_cb *cb; |
| 4110 | |
| 4111 | if (!blk_cb) { |
| 4112 | release_stripe(sh); |
| 4113 | return; |
| 4114 | } |
| 4115 | |
| 4116 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 4117 | |
| 4118 | if (cb->list.next == NULL) |
| 4119 | INIT_LIST_HEAD(&cb->list); |
| 4120 | |
| 4121 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 4122 | list_add_tail(&sh->lru, &cb->list); |
| 4123 | else |
| 4124 | release_stripe(sh); |
| 4125 | } |
| 4126 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4127 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 4128 | { |
| 4129 | struct r5conf *conf = mddev->private; |
| 4130 | sector_t logical_sector, last_sector; |
| 4131 | struct stripe_head *sh; |
| 4132 | int remaining; |
| 4133 | int stripe_sectors; |
| 4134 | |
| 4135 | if (mddev->reshape_position != MaxSector) |
| 4136 | /* Skip discard while reshape is happening */ |
| 4137 | return; |
| 4138 | |
| 4139 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4140 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4141 | |
| 4142 | bi->bi_next = NULL; |
| 4143 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
| 4144 | |
| 4145 | stripe_sectors = conf->chunk_sectors * |
| 4146 | (conf->raid_disks - conf->max_degraded); |
| 4147 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 4148 | stripe_sectors); |
| 4149 | sector_div(last_sector, stripe_sectors); |
| 4150 | |
| 4151 | logical_sector *= conf->chunk_sectors; |
| 4152 | last_sector *= conf->chunk_sectors; |
| 4153 | |
| 4154 | for (; logical_sector < last_sector; |
| 4155 | logical_sector += STRIPE_SECTORS) { |
| 4156 | DEFINE_WAIT(w); |
| 4157 | int d; |
| 4158 | again: |
| 4159 | sh = get_active_stripe(conf, logical_sector, 0, 0, 0); |
| 4160 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 4161 | TASK_UNINTERRUPTIBLE); |
| 4162 | spin_lock_irq(&sh->stripe_lock); |
| 4163 | for (d = 0; d < conf->raid_disks; d++) { |
| 4164 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 4165 | continue; |
| 4166 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 4167 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 4168 | spin_unlock_irq(&sh->stripe_lock); |
| 4169 | release_stripe(sh); |
| 4170 | schedule(); |
| 4171 | goto again; |
| 4172 | } |
| 4173 | } |
| 4174 | finish_wait(&conf->wait_for_overlap, &w); |
| 4175 | for (d = 0; d < conf->raid_disks; d++) { |
| 4176 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 4177 | continue; |
| 4178 | sh->dev[d].towrite = bi; |
| 4179 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
| 4180 | raid5_inc_bi_active_stripes(bi); |
| 4181 | } |
| 4182 | spin_unlock_irq(&sh->stripe_lock); |
| 4183 | if (conf->mddev->bitmap) { |
| 4184 | for (d = 0; |
| 4185 | d < conf->raid_disks - conf->max_degraded; |
| 4186 | d++) |
| 4187 | bitmap_startwrite(mddev->bitmap, |
| 4188 | sh->sector, |
| 4189 | STRIPE_SECTORS, |
| 4190 | 0); |
| 4191 | sh->bm_seq = conf->seq_flush + 1; |
| 4192 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 4193 | } |
| 4194 | |
| 4195 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4196 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4197 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4198 | atomic_inc(&conf->preread_active_stripes); |
| 4199 | release_stripe_plug(mddev, sh); |
| 4200 | } |
| 4201 | |
| 4202 | remaining = raid5_dec_bi_active_stripes(bi); |
| 4203 | if (remaining == 0) { |
| 4204 | md_write_end(mddev); |
| 4205 | bio_endio(bi, 0); |
| 4206 | } |
| 4207 | } |
| 4208 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 4209 | static void make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4211 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4212 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | sector_t new_sector; |
| 4214 | sector_t logical_sector, last_sector; |
| 4215 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 4216 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 4217 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4218 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4219 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 4220 | md_flush_request(mddev, bi); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4221 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4222 | } |
| 4223 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4224 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4225 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 4226 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4227 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 4228 | chunk_aligned_read(mddev,bi)) |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4229 | return; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4230 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4231 | if (unlikely(bi->bi_rw & REQ_DISCARD)) { |
| 4232 | make_discard_request(mddev, bi); |
| 4233 | return; |
| 4234 | } |
| 4235 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4236 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4237 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4238 | bi->bi_next = NULL; |
| 4239 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4240 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 4242 | DEFINE_WAIT(w); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4243 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4244 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4245 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4246 | previous = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4247 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4248 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4249 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4250 | * 64bit on a 32bit platform, and so it might be |
| 4251 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 4252 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4253 | * the lock is dropped, so once we get a reference |
| 4254 | * to the stripe that we think it is, we will have |
| 4255 | * to check again. |
| 4256 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4257 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4258 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4259 | ? logical_sector < conf->reshape_progress |
| 4260 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4261 | previous = 1; |
| 4262 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4263 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4264 | ? logical_sector < conf->reshape_safe |
| 4265 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4266 | spin_unlock_irq(&conf->device_lock); |
| 4267 | schedule(); |
| 4268 | goto retry; |
| 4269 | } |
| 4270 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4271 | spin_unlock_irq(&conf->device_lock); |
| 4272 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4273 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4274 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4275 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4276 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4277 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4278 | (unsigned long long)new_sector, |
| 4279 | (unsigned long long)logical_sector); |
| 4280 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4281 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4282 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4283 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4284 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4285 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4286 | * stripe, so we must do the range check again. |
| 4287 | * Expansion could still move past after this |
| 4288 | * test, but as we are holding a reference to |
| 4289 | * 'sh', we know that if that happens, |
| 4290 | * STRIPE_EXPANDING will get set and the expansion |
| 4291 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4292 | */ |
| 4293 | int must_retry = 0; |
| 4294 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4295 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4296 | ? logical_sector >= conf->reshape_progress |
| 4297 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4298 | /* mismatch, need to try again */ |
| 4299 | must_retry = 1; |
| 4300 | spin_unlock_irq(&conf->device_lock); |
| 4301 | if (must_retry) { |
| 4302 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 4303 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4304 | goto retry; |
| 4305 | } |
| 4306 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4307 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4308 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4309 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4310 | logical_sector < mddev->suspend_hi) { |
| 4311 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4312 | /* As the suspend_* range is controlled by |
| 4313 | * userspace, we want an interruptible |
| 4314 | * wait. |
| 4315 | */ |
| 4316 | flush_signals(current); |
| 4317 | prepare_to_wait(&conf->wait_for_overlap, |
| 4318 | &w, TASK_INTERRUPTIBLE); |
| 4319 | if (logical_sector >= mddev->suspend_lo && |
| 4320 | logical_sector < mddev->suspend_hi) |
| 4321 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4322 | goto retry; |
| 4323 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4324 | |
| 4325 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4326 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4327 | /* Stripe is busy expanding or |
| 4328 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4329 | * and wait a while |
| 4330 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4331 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4332 | release_stripe(sh); |
| 4333 | schedule(); |
| 4334 | goto retry; |
| 4335 | } |
| 4336 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4337 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4338 | clear_bit(STRIPE_DELAYED, &sh->state); |
NeilBrown | a852d7b | 2012-09-19 12:48:30 +1000 | [diff] [blame] | 4339 | if ((bi->bi_rw & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4340 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4341 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4342 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4343 | } else { |
| 4344 | /* cannot get stripe for read-ahead, just give-up */ |
| 4345 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4346 | finish_wait(&conf->wait_for_overlap, &w); |
| 4347 | break; |
| 4348 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4349 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4350 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4351 | remaining = raid5_dec_bi_active_stripes(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4352 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4353 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4354 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4355 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4356 | |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4357 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 4358 | bi, 0); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4359 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4361 | } |
| 4362 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4363 | static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4364 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4365 | static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4366 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4367 | /* reshaping is quite different to recovery/resync so it is |
| 4368 | * handled quite separately ... here. |
| 4369 | * |
| 4370 | * On each call to sync_request, we gather one chunk worth of |
| 4371 | * destination stripes and flag them as expanding. |
| 4372 | * Then we find all the source stripes and request reads. |
| 4373 | * As the reads complete, handle_stripe will copy the data |
| 4374 | * into the destination stripe and release that stripe. |
| 4375 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4376 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4378 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4379 | int raid_disks = conf->previous_raid_disks; |
| 4380 | int data_disks = raid_disks - conf->max_degraded; |
| 4381 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4382 | int i; |
| 4383 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4384 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4385 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4386 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4387 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4388 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4389 | if (sector_nr == 0) { |
| 4390 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4391 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4392 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4393 | sector_nr = raid5_size(mddev, 0, 0) |
| 4394 | - conf->reshape_progress; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4395 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4396 | conf->reshape_progress > 0) |
| 4397 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4398 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4399 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4400 | mddev->curr_resync_completed = sector_nr; |
| 4401 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4402 | *skipped = 1; |
| 4403 | return sector_nr; |
| 4404 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4405 | } |
| 4406 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4407 | /* We need to process a full chunk at a time. |
| 4408 | * If old and new chunk sizes differ, we need to process the |
| 4409 | * largest of these |
| 4410 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4411 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4412 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4413 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4414 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4415 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4416 | /* We update the metadata at least every 10 seconds, or when |
| 4417 | * the data about to be copied would over-write the source of |
| 4418 | * the data at the front of the range. i.e. one new_stripe |
| 4419 | * along from reshape_progress new_maps to after where |
| 4420 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4421 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4422 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4423 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4424 | readpos = conf->reshape_progress; |
| 4425 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4426 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4427 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4428 | if (mddev->reshape_backwards) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4429 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4430 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4431 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4432 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4433 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4434 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4435 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4436 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4437 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4438 | /* Having calculated the 'writepos' possibly use it |
| 4439 | * to set 'stripe_addr' which is where we will write to. |
| 4440 | */ |
| 4441 | if (mddev->reshape_backwards) { |
| 4442 | BUG_ON(conf->reshape_progress == 0); |
| 4443 | stripe_addr = writepos; |
| 4444 | BUG_ON((mddev->dev_sectors & |
| 4445 | ~((sector_t)reshape_sectors - 1)) |
| 4446 | - reshape_sectors - stripe_addr |
| 4447 | != sector_nr); |
| 4448 | } else { |
| 4449 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 4450 | stripe_addr = sector_nr; |
| 4451 | } |
| 4452 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4453 | /* 'writepos' is the most advanced device address we might write. |
| 4454 | * 'readpos' is the least advanced device address we might read. |
| 4455 | * 'safepos' is the least address recorded in the metadata as having |
| 4456 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4457 | * If there is a min_offset_diff, these are adjusted either by |
| 4458 | * increasing the safepos/readpos if diff is negative, or |
| 4459 | * increasing writepos if diff is positive. |
| 4460 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4461 | * ensure safety in the face of a crash - that must be done by userspace |
| 4462 | * making a backup of the data. So in that case there is no particular |
| 4463 | * rush to update metadata. |
| 4464 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4465 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4466 | * we can be safe in the event of a crash. |
| 4467 | * So we insist on updating metadata if safepos is behind writepos and |
| 4468 | * readpos is beyond writepos. |
| 4469 | * In any case, update the metadata every 10 seconds. |
| 4470 | * Maybe that number should be configurable, but I'm not sure it is |
| 4471 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4472 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4473 | if (conf->min_offset_diff < 0) { |
| 4474 | safepos += -conf->min_offset_diff; |
| 4475 | readpos += -conf->min_offset_diff; |
| 4476 | } else |
| 4477 | writepos += conf->min_offset_diff; |
| 4478 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4479 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4480 | ? (safepos > writepos && readpos < writepos) |
| 4481 | : (safepos < writepos && readpos > writepos)) || |
| 4482 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4483 | /* Cannot proceed until we've updated the superblock... */ |
| 4484 | wait_event(conf->wait_for_overlap, |
| 4485 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4486 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4487 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4488 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4489 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4490 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4491 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4492 | kthread_should_stop()); |
| 4493 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4494 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4495 | spin_unlock_irq(&conf->device_lock); |
| 4496 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4497 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4498 | } |
| 4499 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4500 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4501 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4502 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4503 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4504 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4505 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4506 | atomic_inc(&conf->reshape_stripes); |
| 4507 | /* If any of this stripe is beyond the end of the old |
| 4508 | * array, then we need to zero those blocks |
| 4509 | */ |
| 4510 | for (j=sh->disks; j--;) { |
| 4511 | sector_t s; |
| 4512 | if (j == sh->pd_idx) |
| 4513 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4514 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4515 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4516 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4517 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4518 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4519 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4520 | continue; |
| 4521 | } |
| 4522 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4523 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4524 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4525 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4526 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4527 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4528 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4529 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4530 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4531 | } |
| 4532 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4533 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4534 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4535 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4536 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4537 | spin_unlock_irq(&conf->device_lock); |
| 4538 | /* Ok, those stripe are ready. We can start scheduling |
| 4539 | * reads on the source stripes. |
| 4540 | * The source stripes are determined by mapping the first and last |
| 4541 | * block on the destination stripes. |
| 4542 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4543 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4544 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4545 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4546 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4547 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4548 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4549 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4550 | if (last_sector >= mddev->dev_sectors) |
| 4551 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4552 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4553 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4554 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4555 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4556 | release_stripe(sh); |
| 4557 | first_sector += STRIPE_SECTORS; |
| 4558 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4559 | /* Now that the sources are clearly marked, we can release |
| 4560 | * the destination stripes |
| 4561 | */ |
| 4562 | while (!list_empty(&stripes)) { |
| 4563 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4564 | list_del_init(&sh->lru); |
| 4565 | release_stripe(sh); |
| 4566 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4567 | /* If this takes us to the resync_max point where we have to pause, |
| 4568 | * then we need to write out the superblock. |
| 4569 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4570 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4571 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4572 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4573 | /* Cannot proceed until we've updated the superblock... */ |
| 4574 | wait_event(conf->wait_for_overlap, |
| 4575 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4576 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4577 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4578 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4579 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4580 | md_wakeup_thread(mddev->thread); |
| 4581 | wait_event(mddev->sb_wait, |
| 4582 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4583 | || kthread_should_stop()); |
| 4584 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4585 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4586 | spin_unlock_irq(&conf->device_lock); |
| 4587 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4588 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4589 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4590 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4591 | } |
| 4592 | |
| 4593 | /* FIXME go_faster isn't used */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4594 | static inline sector_t sync_request(struct mddev *mddev, sector_t sector_nr, int *skipped, int go_faster) |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4595 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4596 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4597 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4598 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 4599 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4600 | int still_degraded = 0; |
| 4601 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4602 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4603 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4604 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4605 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4606 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4607 | end_reshape(conf); |
| 4608 | return 0; |
| 4609 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4610 | |
| 4611 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4612 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4613 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4614 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4615 | conf->fullsync = 0; |
| 4616 | bitmap_close_sync(mddev->bitmap); |
| 4617 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4618 | return 0; |
| 4619 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4620 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4621 | /* Allow raid5_quiesce to complete */ |
| 4622 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4623 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4624 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4625 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4626 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4627 | /* No need to check resync_max as we never do more than one |
| 4628 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4629 | * if the check in md_do_sync didn't fire, there is no chance |
| 4630 | * of overstepping resync_max here |
| 4631 | */ |
| 4632 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4633 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4634 | * to resync, then assert that we are finished, because there is |
| 4635 | * nothing we can do. |
| 4636 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4637 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4638 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4639 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4640 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4641 | return rv; |
| 4642 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4643 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4644 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4645 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4646 | /* we can skip this block, and probably more */ |
| 4647 | sync_blocks /= STRIPE_SECTORS; |
| 4648 | *skipped = 1; |
| 4649 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4651 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4652 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4653 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4654 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4655 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4656 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4657 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4658 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4659 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4660 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4661 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4662 | /* Need to check if array will still be degraded after recovery/resync |
| 4663 | * We don't need to check the 'failed' flag as when that gets set, |
| 4664 | * recovery aborts. |
| 4665 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4666 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4667 | if (conf->disks[i].rdev == NULL) |
| 4668 | still_degraded = 1; |
| 4669 | |
| 4670 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4671 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 4672 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4673 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4674 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4675 | release_stripe(sh); |
| 4676 | |
| 4677 | return STRIPE_SECTORS; |
| 4678 | } |
| 4679 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4680 | static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4681 | { |
| 4682 | /* We may not be able to submit a whole bio at once as there |
| 4683 | * may not be enough stripe_heads available. |
| 4684 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4685 | * more than exist in the cache (if we allow ever large chunks). |
| 4686 | * So we do one stripe head at a time and record in |
| 4687 | * ->bi_hw_segments how many have been done. |
| 4688 | * |
| 4689 | * We *know* that this entire raid_bio is in one chunk, so |
| 4690 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4691 | */ |
| 4692 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4693 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4694 | sector_t sector, logical_sector, last_sector; |
| 4695 | int scnt = 0; |
| 4696 | int remaining; |
| 4697 | int handled = 0; |
| 4698 | |
| 4699 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4700 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4701 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4702 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4703 | |
| 4704 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4705 | logical_sector += STRIPE_SECTORS, |
| 4706 | sector += STRIPE_SECTORS, |
| 4707 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4708 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4709 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4710 | /* already done this stripe */ |
| 4711 | continue; |
| 4712 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4713 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4714 | |
| 4715 | if (!sh) { |
| 4716 | /* failed to get a stripe - must wait */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4717 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4718 | conf->retry_read_aligned = raid_bio; |
| 4719 | return handled; |
| 4720 | } |
| 4721 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4722 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4723 | release_stripe(sh); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4724 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4725 | conf->retry_read_aligned = raid_bio; |
| 4726 | return handled; |
| 4727 | } |
| 4728 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 4729 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4730 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4731 | release_stripe(sh); |
| 4732 | handled++; |
| 4733 | } |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4734 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4735 | if (remaining == 0) { |
| 4736 | trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev), |
| 4737 | raid_bio, 0); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4738 | bio_endio(raid_bio, 0); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4739 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4740 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4741 | wake_up(&conf->wait_for_stripe); |
| 4742 | return handled; |
| 4743 | } |
| 4744 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4745 | #define MAX_STRIPE_BATCH 8 |
| 4746 | static int handle_active_stripes(struct r5conf *conf) |
| 4747 | { |
| 4748 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
| 4749 | int i, batch_size = 0; |
| 4750 | |
| 4751 | while (batch_size < MAX_STRIPE_BATCH && |
| 4752 | (sh = __get_priority_stripe(conf)) != NULL) |
| 4753 | batch[batch_size++] = sh; |
| 4754 | |
| 4755 | if (batch_size == 0) |
| 4756 | return batch_size; |
| 4757 | spin_unlock_irq(&conf->device_lock); |
| 4758 | |
| 4759 | for (i = 0; i < batch_size; i++) |
| 4760 | handle_stripe(batch[i]); |
| 4761 | |
| 4762 | cond_resched(); |
| 4763 | |
| 4764 | spin_lock_irq(&conf->device_lock); |
| 4765 | for (i = 0; i < batch_size; i++) |
| 4766 | __release_stripe(conf, batch[i]); |
| 4767 | return batch_size; |
| 4768 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4769 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4770 | /* |
| 4771 | * This is our raid5 kernel thread. |
| 4772 | * |
| 4773 | * We scan the hash table for stripes which can be handled now. |
| 4774 | * During the scan, completed stripes are saved for us by the interrupt |
| 4775 | * handler, so that they will not have to wait for our next wakeup. |
| 4776 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 4777 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4778 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 4779 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4780 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4781 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4782 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4783 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4784 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4785 | |
| 4786 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4787 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4788 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4789 | handled = 0; |
| 4790 | spin_lock_irq(&conf->device_lock); |
| 4791 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4792 | struct bio *bio; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4793 | int batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4794 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 4795 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4796 | !list_empty(&conf->bitmap_list)) { |
| 4797 | /* Now is a good time to flush some bitmap updates */ |
| 4798 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4799 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4800 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4801 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4802 | conf->seq_write = conf->seq_flush; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4803 | activate_bit_delay(conf); |
| 4804 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 4805 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4806 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4807 | while ((bio = remove_bio_from_retry(conf))) { |
| 4808 | int ok; |
| 4809 | spin_unlock_irq(&conf->device_lock); |
| 4810 | ok = retry_aligned_read(conf, bio); |
| 4811 | spin_lock_irq(&conf->device_lock); |
| 4812 | if (!ok) |
| 4813 | break; |
| 4814 | handled++; |
| 4815 | } |
| 4816 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4817 | batch_size = handle_active_stripes(conf); |
| 4818 | if (!batch_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4819 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4820 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4821 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4822 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) { |
| 4823 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 4824 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4825 | spin_lock_irq(&conf->device_lock); |
| 4826 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4827 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4828 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4829 | |
| 4830 | spin_unlock_irq(&conf->device_lock); |
| 4831 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4832 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4833 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4834 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4835 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4836 | } |
| 4837 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4838 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4839 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4840 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4841 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4842 | if (conf) |
| 4843 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4844 | else |
| 4845 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4846 | } |
| 4847 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4848 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4849 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4850 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4851 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4852 | int err; |
| 4853 | |
| 4854 | if (size <= 16 || size > 32768) |
| 4855 | return -EINVAL; |
| 4856 | while (size < conf->max_nr_stripes) { |
| 4857 | if (drop_one_stripe(conf)) |
| 4858 | conf->max_nr_stripes--; |
| 4859 | else |
| 4860 | break; |
| 4861 | } |
| 4862 | err = md_allow_write(mddev); |
| 4863 | if (err) |
| 4864 | return err; |
| 4865 | while (size > conf->max_nr_stripes) { |
| 4866 | if (grow_one_stripe(conf)) |
| 4867 | conf->max_nr_stripes++; |
| 4868 | else break; |
| 4869 | } |
| 4870 | return 0; |
| 4871 | } |
| 4872 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 4873 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4874 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4875 | raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4876 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4877 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4878 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4879 | int err; |
| 4880 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4881 | if (len >= PAGE_SIZE) |
| 4882 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4883 | if (!conf) |
| 4884 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4885 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4886 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4887 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4888 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4889 | if (err) |
| 4890 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4891 | return len; |
| 4892 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4893 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4894 | static struct md_sysfs_entry |
| 4895 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4896 | raid5_show_stripe_cache_size, |
| 4897 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4898 | |
| 4899 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4900 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4901 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4902 | struct r5conf *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4903 | if (conf) |
| 4904 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4905 | else |
| 4906 | return 0; |
| 4907 | } |
| 4908 | |
| 4909 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4910 | raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4911 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4912 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4913 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4914 | if (len >= PAGE_SIZE) |
| 4915 | return -EINVAL; |
| 4916 | if (!conf) |
| 4917 | return -ENODEV; |
| 4918 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4919 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4920 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4921 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4922 | return -EINVAL; |
| 4923 | conf->bypass_threshold = new; |
| 4924 | return len; |
| 4925 | } |
| 4926 | |
| 4927 | static struct md_sysfs_entry |
| 4928 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4929 | S_IRUGO | S_IWUSR, |
| 4930 | raid5_show_preread_threshold, |
| 4931 | raid5_store_preread_threshold); |
| 4932 | |
| 4933 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4934 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4935 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4936 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4937 | if (conf) |
| 4938 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4939 | else |
| 4940 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4941 | } |
| 4942 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4943 | static struct md_sysfs_entry |
| 4944 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4945 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4946 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4947 | &raid5_stripecache_size.attr, |
| 4948 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4949 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4950 | NULL, |
| 4951 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4952 | static struct attribute_group raid5_attrs_group = { |
| 4953 | .name = NULL, |
| 4954 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4955 | }; |
| 4956 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4957 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4958 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4959 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4960 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4961 | |
| 4962 | if (!sectors) |
| 4963 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4964 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4965 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4966 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4967 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4968 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4969 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4970 | return sectors * (raid_disks - conf->max_degraded); |
| 4971 | } |
| 4972 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4973 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4974 | { |
| 4975 | struct raid5_percpu *percpu; |
| 4976 | unsigned long cpu; |
| 4977 | |
| 4978 | if (!conf->percpu) |
| 4979 | return; |
| 4980 | |
| 4981 | get_online_cpus(); |
| 4982 | for_each_possible_cpu(cpu) { |
| 4983 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 4984 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 4985 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4986 | } |
| 4987 | #ifdef CONFIG_HOTPLUG_CPU |
| 4988 | unregister_cpu_notifier(&conf->cpu_notify); |
| 4989 | #endif |
| 4990 | put_online_cpus(); |
| 4991 | |
| 4992 | free_percpu(conf->percpu); |
| 4993 | } |
| 4994 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4995 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4996 | { |
| 4997 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4998 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 4999 | kfree(conf->disks); |
| 5000 | kfree(conf->stripe_hashtbl); |
| 5001 | kfree(conf); |
| 5002 | } |
| 5003 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5004 | #ifdef CONFIG_HOTPLUG_CPU |
| 5005 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 5006 | void *hcpu) |
| 5007 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5008 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5009 | long cpu = (long)hcpu; |
| 5010 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 5011 | |
| 5012 | switch (action) { |
| 5013 | case CPU_UP_PREPARE: |
| 5014 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5015 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5016 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5017 | if (!percpu->scribble) |
| 5018 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 5019 | |
| 5020 | if (!percpu->scribble || |
| 5021 | (conf->level == 6 && !percpu->spare_page)) { |
| 5022 | safe_put_page(percpu->spare_page); |
| 5023 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5024 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 5025 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 5026 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5027 | } |
| 5028 | break; |
| 5029 | case CPU_DEAD: |
| 5030 | case CPU_DEAD_FROZEN: |
| 5031 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5032 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5033 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5034 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5035 | break; |
| 5036 | default: |
| 5037 | break; |
| 5038 | } |
| 5039 | return NOTIFY_OK; |
| 5040 | } |
| 5041 | #endif |
| 5042 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5043 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5044 | { |
| 5045 | unsigned long cpu; |
| 5046 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 5047 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5048 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5049 | int err; |
| 5050 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5051 | allcpus = alloc_percpu(struct raid5_percpu); |
| 5052 | if (!allcpus) |
| 5053 | return -ENOMEM; |
| 5054 | conf->percpu = allcpus; |
| 5055 | |
| 5056 | get_online_cpus(); |
| 5057 | err = 0; |
| 5058 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5059 | if (conf->level == 6) { |
| 5060 | spare_page = alloc_page(GFP_KERNEL); |
| 5061 | if (!spare_page) { |
| 5062 | err = -ENOMEM; |
| 5063 | break; |
| 5064 | } |
| 5065 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 5066 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5067 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5068 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5069 | err = -ENOMEM; |
| 5070 | break; |
| 5071 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5072 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5073 | } |
| 5074 | #ifdef CONFIG_HOTPLUG_CPU |
| 5075 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 5076 | conf->cpu_notify.priority = 0; |
| 5077 | if (err == 0) |
| 5078 | err = register_cpu_notifier(&conf->cpu_notify); |
| 5079 | #endif |
| 5080 | put_online_cpus(); |
| 5081 | |
| 5082 | return err; |
| 5083 | } |
| 5084 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5085 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5086 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5087 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5088 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5089 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5090 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5091 | char pers_name[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5092 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5093 | if (mddev->new_level != 5 |
| 5094 | && mddev->new_level != 4 |
| 5095 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5096 | printk(KERN_ERR "md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5097 | mdname(mddev), mddev->new_level); |
| 5098 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5099 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5100 | if ((mddev->new_level == 5 |
| 5101 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 5102 | (mddev->new_level == 6 |
| 5103 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5104 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5105 | mdname(mddev), mddev->new_layout); |
| 5106 | return ERR_PTR(-EIO); |
| 5107 | } |
| 5108 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5109 | printk(KERN_ERR "md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5110 | mdname(mddev), mddev->raid_disks); |
| 5111 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5112 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5114 | if (!mddev->new_chunk_sectors || |
| 5115 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 5116 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5117 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 5118 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5119 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5120 | } |
| 5121 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5122 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5123 | if (conf == NULL) |
| 5124 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 5125 | spin_lock_init(&conf->device_lock); |
| 5126 | init_waitqueue_head(&conf->wait_for_stripe); |
| 5127 | init_waitqueue_head(&conf->wait_for_overlap); |
| 5128 | INIT_LIST_HEAD(&conf->handle_list); |
| 5129 | INIT_LIST_HEAD(&conf->hold_list); |
| 5130 | INIT_LIST_HEAD(&conf->delayed_list); |
| 5131 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 5132 | INIT_LIST_HEAD(&conf->inactive_list); |
| 5133 | atomic_set(&conf->active_stripes, 0); |
| 5134 | atomic_set(&conf->preread_active_stripes, 0); |
| 5135 | atomic_set(&conf->active_aligned_reads, 0); |
| 5136 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 5137 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5138 | |
| 5139 | conf->raid_disks = mddev->raid_disks; |
| 5140 | if (mddev->reshape_position == MaxSector) |
| 5141 | conf->previous_raid_disks = mddev->raid_disks; |
| 5142 | else |
| 5143 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5144 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 5145 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5146 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5147 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5148 | GFP_KERNEL); |
| 5149 | if (!conf->disks) |
| 5150 | goto abort; |
| 5151 | |
| 5152 | conf->mddev = mddev; |
| 5153 | |
| 5154 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 5155 | goto abort; |
| 5156 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5157 | conf->level = mddev->new_level; |
| 5158 | if (raid5_alloc_percpu(conf) != 0) |
| 5159 | goto abort; |
| 5160 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5161 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5162 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5163 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5164 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5165 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5166 | || raid_disk < 0) |
| 5167 | continue; |
| 5168 | disk = conf->disks + raid_disk; |
| 5169 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5170 | if (test_bit(Replacement, &rdev->flags)) { |
| 5171 | if (disk->replacement) |
| 5172 | goto abort; |
| 5173 | disk->replacement = rdev; |
| 5174 | } else { |
| 5175 | if (disk->rdev) |
| 5176 | goto abort; |
| 5177 | disk->rdev = rdev; |
| 5178 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5179 | |
| 5180 | if (test_bit(In_sync, &rdev->flags)) { |
| 5181 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5182 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 5183 | " disk %d\n", |
| 5184 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 5185 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5186 | /* Cannot rely on bitmap to complete recovery */ |
| 5187 | conf->fullsync = 1; |
| 5188 | } |
| 5189 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5190 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5191 | conf->level = mddev->new_level; |
| 5192 | if (conf->level == 6) |
| 5193 | conf->max_degraded = 2; |
| 5194 | else |
| 5195 | conf->max_degraded = 1; |
| 5196 | conf->algorithm = mddev->new_layout; |
| 5197 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5198 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5199 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5200 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5201 | conf->prev_algo = mddev->layout; |
| 5202 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5203 | |
| 5204 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5205 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5206 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 5207 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5208 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 5209 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5210 | goto abort; |
| 5211 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5212 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 5213 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5214 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5215 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 5216 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5217 | if (!conf->thread) { |
| 5218 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5219 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5220 | mdname(mddev)); |
| 5221 | goto abort; |
| 5222 | } |
| 5223 | |
| 5224 | return conf; |
| 5225 | |
| 5226 | abort: |
| 5227 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5228 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5229 | return ERR_PTR(-EIO); |
| 5230 | } else |
| 5231 | return ERR_PTR(-ENOMEM); |
| 5232 | } |
| 5233 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5234 | |
| 5235 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 5236 | { |
| 5237 | switch (algo) { |
| 5238 | case ALGORITHM_PARITY_0: |
| 5239 | if (raid_disk < max_degraded) |
| 5240 | return 1; |
| 5241 | break; |
| 5242 | case ALGORITHM_PARITY_N: |
| 5243 | if (raid_disk >= raid_disks - max_degraded) |
| 5244 | return 1; |
| 5245 | break; |
| 5246 | case ALGORITHM_PARITY_0_6: |
| 5247 | if (raid_disk == 0 || |
| 5248 | raid_disk == raid_disks - 1) |
| 5249 | return 1; |
| 5250 | break; |
| 5251 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5252 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5253 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5254 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5255 | if (raid_disk == raid_disks - 1) |
| 5256 | return 1; |
| 5257 | } |
| 5258 | return 0; |
| 5259 | } |
| 5260 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5261 | static int run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5262 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5263 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5264 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5265 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5266 | struct md_rdev *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5267 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5268 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5269 | long long min_offset_diff = 0; |
| 5270 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5271 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5272 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5273 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5274 | " -- starting background reconstruction\n", |
| 5275 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5276 | |
| 5277 | rdev_for_each(rdev, mddev) { |
| 5278 | long long diff; |
| 5279 | if (rdev->raid_disk < 0) |
| 5280 | continue; |
| 5281 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 5282 | if (first) { |
| 5283 | min_offset_diff = diff; |
| 5284 | first = 0; |
| 5285 | } else if (mddev->reshape_backwards && |
| 5286 | diff < min_offset_diff) |
| 5287 | min_offset_diff = diff; |
| 5288 | else if (!mddev->reshape_backwards && |
| 5289 | diff > min_offset_diff) |
| 5290 | min_offset_diff = diff; |
| 5291 | } |
| 5292 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5293 | if (mddev->reshape_position != MaxSector) { |
| 5294 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5295 | * Difficulties arise if the stripe we would write to |
| 5296 | * next is at or after the stripe we would read from next. |
| 5297 | * For a reshape that changes the number of devices, this |
| 5298 | * is only possible for a very short time, and mdadm makes |
| 5299 | * sure that time appears to have past before assembling |
| 5300 | * the array. So we fail if that time hasn't passed. |
| 5301 | * For a reshape that keeps the number of devices the same |
| 5302 | * mdadm must be monitoring the reshape can keeping the |
| 5303 | * critical areas read-only and backed up. It will start |
| 5304 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5305 | */ |
| 5306 | sector_t here_new, here_old; |
| 5307 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 5308 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5309 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5310 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5311 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5312 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5313 | mdname(mddev)); |
| 5314 | return -EINVAL; |
| 5315 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5316 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 5317 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5318 | * further up in new geometry must map after here in old |
| 5319 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5320 | */ |
| 5321 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5322 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5323 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5324 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 5325 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5326 | return -EINVAL; |
| 5327 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5328 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5329 | /* here_new is the stripe we will write to */ |
| 5330 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5331 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5332 | (old_disks-max_degraded)); |
| 5333 | /* here_old is the first stripe that we might need to read |
| 5334 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5335 | if (mddev->delta_disks == 0) { |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5336 | if ((here_new * mddev->new_chunk_sectors != |
| 5337 | here_old * mddev->chunk_sectors)) { |
| 5338 | printk(KERN_ERR "md/raid:%s: reshape position is" |
| 5339 | " confused - aborting\n", mdname(mddev)); |
| 5340 | return -EINVAL; |
| 5341 | } |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5342 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5343 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5344 | * and taking constant backups. |
| 5345 | * mdadm always starts a situation like this in |
| 5346 | * readonly mode so it can take control before |
| 5347 | * allowing any writes. So just check for that. |
| 5348 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5349 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 5350 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 5351 | /* not really in-place - so OK */; |
| 5352 | else if (mddev->ro == 0) { |
| 5353 | printk(KERN_ERR "md/raid:%s: in-place reshape " |
| 5354 | "must be started in read-only mode " |
| 5355 | "- aborting\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5356 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5357 | return -EINVAL; |
| 5358 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5359 | } else if (mddev->reshape_backwards |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5360 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5361 | here_old * mddev->chunk_sectors) |
| 5362 | : (here_new * mddev->new_chunk_sectors >= |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5363 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5364 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5365 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 5366 | "auto-recovery - aborting.\n", |
| 5367 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5368 | return -EINVAL; |
| 5369 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5370 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 5371 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5372 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5373 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5374 | BUG_ON(mddev->level != mddev->new_level); |
| 5375 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5376 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5377 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5378 | } |
| 5379 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5380 | if (mddev->private == NULL) |
| 5381 | conf = setup_conf(mddev); |
| 5382 | else |
| 5383 | conf = mddev->private; |
| 5384 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5385 | if (IS_ERR(conf)) |
| 5386 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5387 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5388 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5389 | mddev->thread = conf->thread; |
| 5390 | conf->thread = NULL; |
| 5391 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5392 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5393 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 5394 | i++) { |
| 5395 | rdev = conf->disks[i].rdev; |
| 5396 | if (!rdev && conf->disks[i].replacement) { |
| 5397 | /* The replacement is all we have yet */ |
| 5398 | rdev = conf->disks[i].replacement; |
| 5399 | conf->disks[i].replacement = NULL; |
| 5400 | clear_bit(Replacement, &rdev->flags); |
| 5401 | conf->disks[i].rdev = rdev; |
| 5402 | } |
| 5403 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5404 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5405 | if (conf->disks[i].replacement && |
| 5406 | conf->reshape_progress != MaxSector) { |
| 5407 | /* replacements and reshape simply do not mix. */ |
| 5408 | printk(KERN_ERR "md: cannot handle concurrent " |
| 5409 | "replacement and reshape.\n"); |
| 5410 | goto abort; |
| 5411 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5412 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5413 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5414 | continue; |
| 5415 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5416 | /* This disc is not fully in-sync. However if it |
| 5417 | * just stored parity (beyond the recovery_offset), |
| 5418 | * when we don't need to be concerned about the |
| 5419 | * array being dirty. |
| 5420 | * When reshape goes 'backwards', we never have |
| 5421 | * partially completed devices, so we only need |
| 5422 | * to worry about reshape going forwards. |
| 5423 | */ |
| 5424 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 5425 | if (mddev->major_version == 0 && |
| 5426 | mddev->minor_version > 90) |
| 5427 | rdev->recovery_offset = reshape_offset; |
| 5428 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5429 | if (rdev->recovery_offset < reshape_offset) { |
| 5430 | /* We need to check old and new layout */ |
| 5431 | if (!only_parity(rdev->raid_disk, |
| 5432 | conf->algorithm, |
| 5433 | conf->raid_disks, |
| 5434 | conf->max_degraded)) |
| 5435 | continue; |
| 5436 | } |
| 5437 | if (!only_parity(rdev->raid_disk, |
| 5438 | conf->prev_algo, |
| 5439 | conf->previous_raid_disks, |
| 5440 | conf->max_degraded)) |
| 5441 | continue; |
| 5442 | dirty_parity_disks++; |
| 5443 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5444 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5445 | /* |
| 5446 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 5447 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5448 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5449 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5450 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5451 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5452 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5453 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5454 | goto abort; |
| 5455 | } |
| 5456 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5457 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5458 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5459 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5460 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5461 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5462 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5463 | if (mddev->ok_start_degraded) |
| 5464 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5465 | "md/raid:%s: starting dirty degraded array" |
| 5466 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5467 | mdname(mddev)); |
| 5468 | else { |
| 5469 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5470 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5471 | mdname(mddev)); |
| 5472 | goto abort; |
| 5473 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5474 | } |
| 5475 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5476 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5477 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5478 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5479 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5480 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5481 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5482 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5483 | " out of %d devices, algorithm %d\n", |
| 5484 | mdname(mddev), conf->level, |
| 5485 | mddev->raid_disks - mddev->degraded, |
| 5486 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5487 | |
| 5488 | print_raid5_conf(conf); |
| 5489 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5490 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5491 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5492 | atomic_set(&conf->reshape_stripes, 0); |
| 5493 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5494 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5495 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5496 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5497 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5498 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5499 | } |
| 5500 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5501 | |
| 5502 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5503 | if (mddev->to_remove == &raid5_attrs_group) |
| 5504 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5505 | else if (mddev->kobj.sd && |
| 5506 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5507 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5508 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5509 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5510 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5511 | |
| 5512 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5513 | int chunk_size; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5514 | bool discard_supported = true; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5515 | /* read-ahead size must cover two whole stripes, which |
| 5516 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 5517 | * number of raid devices |
| 5518 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5519 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5520 | int stripe = data_disks * |
| 5521 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 5522 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5523 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5524 | |
| 5525 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5526 | |
| 5527 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5528 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5529 | |
| 5530 | chunk_size = mddev->chunk_sectors << 9; |
| 5531 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5532 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5533 | (conf->raid_disks - conf->max_degraded)); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5534 | /* |
| 5535 | * We can only discard a whole stripe. It doesn't make sense to |
| 5536 | * discard data disk but write parity disk |
| 5537 | */ |
| 5538 | stripe = stripe * PAGE_SIZE; |
NeilBrown | 4ac6875 | 2012-11-19 13:11:26 +1100 | [diff] [blame] | 5539 | /* Round up to power of 2, as discard handling |
| 5540 | * currently assumes that */ |
| 5541 | while ((stripe-1) & stripe) |
| 5542 | stripe = (stripe | (stripe-1)) + 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5543 | mddev->queue->limits.discard_alignment = stripe; |
| 5544 | mddev->queue->limits.discard_granularity = stripe; |
| 5545 | /* |
| 5546 | * unaligned part of discard request will be ignored, so can't |
| 5547 | * guarantee discard_zerors_data |
| 5548 | */ |
| 5549 | mddev->queue->limits.discard_zeroes_data = 0; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5550 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5551 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5552 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5553 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5554 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5555 | rdev->new_data_offset << 9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5556 | /* |
| 5557 | * discard_zeroes_data is required, otherwise data |
| 5558 | * could be lost. Consider a scenario: discard a stripe |
| 5559 | * (the stripe could be inconsistent if |
| 5560 | * discard_zeroes_data is 0); write one disk of the |
| 5561 | * stripe (the stripe could be inconsistent again |
| 5562 | * depending on which disks are used to calculate |
| 5563 | * parity); the disk is broken; The stripe data of this |
| 5564 | * disk is lost. |
| 5565 | */ |
| 5566 | if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || |
| 5567 | !bdev_get_queue(rdev->bdev)-> |
| 5568 | limits.discard_zeroes_data) |
| 5569 | discard_supported = false; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5570 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5571 | |
| 5572 | if (discard_supported && |
| 5573 | mddev->queue->limits.max_discard_sectors >= stripe && |
| 5574 | mddev->queue->limits.discard_granularity >= stripe) |
| 5575 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 5576 | mddev->queue); |
| 5577 | else |
| 5578 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, |
| 5579 | mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5580 | } |
| 5581 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5582 | return 0; |
| 5583 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5584 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 5585 | print_raid5_conf(conf); |
| 5586 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5587 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5588 | printk(KERN_ALERT "md/raid:%s: failed to run raid set.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5589 | return -EIO; |
| 5590 | } |
| 5591 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5592 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5593 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5594 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5596 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5597 | if (mddev->queue) |
| 5598 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5599 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5600 | mddev->private = NULL; |
| 5601 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5602 | return 0; |
| 5603 | } |
| 5604 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5605 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5606 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5607 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | int i; |
| 5609 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5610 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5611 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5612 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5613 | for (i = 0; i < conf->raid_disks; i++) |
| 5614 | seq_printf (seq, "%s", |
| 5615 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5616 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5617 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | } |
| 5619 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5620 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5621 | { |
| 5622 | int i; |
| 5623 | struct disk_info *tmp; |
| 5624 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5625 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5626 | if (!conf) { |
| 5627 | printk("(conf==NULL)\n"); |
| 5628 | return; |
| 5629 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5630 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5631 | conf->raid_disks, |
| 5632 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5633 | |
| 5634 | for (i = 0; i < conf->raid_disks; i++) { |
| 5635 | char b[BDEVNAME_SIZE]; |
| 5636 | tmp = conf->disks + i; |
| 5637 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5638 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5639 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5640 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | } |
| 5642 | } |
| 5643 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5644 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5645 | { |
| 5646 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5647 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5648 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5649 | int count = 0; |
| 5650 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5651 | |
| 5652 | for (i = 0; i < conf->raid_disks; i++) { |
| 5653 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5654 | if (tmp->replacement |
| 5655 | && tmp->replacement->recovery_offset == MaxSector |
| 5656 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 5657 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 5658 | /* Replacement has just become active. */ |
| 5659 | if (!tmp->rdev |
| 5660 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 5661 | count++; |
| 5662 | if (tmp->rdev) { |
| 5663 | /* Replaced device not technically faulty, |
| 5664 | * but we need to be sure it gets removed |
| 5665 | * and never re-added. |
| 5666 | */ |
| 5667 | set_bit(Faulty, &tmp->rdev->flags); |
| 5668 | sysfs_notify_dirent_safe( |
| 5669 | tmp->rdev->sysfs_state); |
| 5670 | } |
| 5671 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 5672 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 5673 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5674 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5675 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5676 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5677 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5678 | } |
| 5679 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5680 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5681 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5682 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5683 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5684 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5685 | } |
| 5686 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5687 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5688 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5689 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5691 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5692 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5693 | struct disk_info *p = conf->disks + number; |
| 5694 | |
| 5695 | print_raid5_conf(conf); |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5696 | if (rdev == p->rdev) |
| 5697 | rdevp = &p->rdev; |
| 5698 | else if (rdev == p->replacement) |
| 5699 | rdevp = &p->replacement; |
| 5700 | else |
| 5701 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5702 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5703 | if (number >= conf->raid_disks && |
| 5704 | conf->reshape_progress == MaxSector) |
| 5705 | clear_bit(In_sync, &rdev->flags); |
| 5706 | |
| 5707 | if (test_bit(In_sync, &rdev->flags) || |
| 5708 | atomic_read(&rdev->nr_pending)) { |
| 5709 | err = -EBUSY; |
| 5710 | goto abort; |
| 5711 | } |
| 5712 | /* Only remove non-faulty devices if recovery |
| 5713 | * isn't possible. |
| 5714 | */ |
| 5715 | if (!test_bit(Faulty, &rdev->flags) && |
| 5716 | mddev->recovery_disabled != conf->recovery_disabled && |
| 5717 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5718 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5719 | number < conf->raid_disks) { |
| 5720 | err = -EBUSY; |
| 5721 | goto abort; |
| 5722 | } |
| 5723 | *rdevp = NULL; |
| 5724 | synchronize_rcu(); |
| 5725 | if (atomic_read(&rdev->nr_pending)) { |
| 5726 | /* lost the race, try later */ |
| 5727 | err = -EBUSY; |
| 5728 | *rdevp = rdev; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5729 | } else if (p->replacement) { |
| 5730 | /* We must have just cleared 'rdev' */ |
| 5731 | p->rdev = p->replacement; |
| 5732 | clear_bit(Replacement, &p->replacement->flags); |
| 5733 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 5734 | * but will never see neither - if they are careful |
| 5735 | */ |
| 5736 | p->replacement = NULL; |
| 5737 | clear_bit(WantReplacement, &rdev->flags); |
| 5738 | } else |
| 5739 | /* We might have just removed the Replacement as faulty- |
| 5740 | * clear the bit just in case |
| 5741 | */ |
| 5742 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | abort: |
| 5744 | |
| 5745 | print_raid5_conf(conf); |
| 5746 | return err; |
| 5747 | } |
| 5748 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5749 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5751 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5752 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | int disk; |
| 5754 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5755 | int first = 0; |
| 5756 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5757 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5758 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 5759 | return -EBUSY; |
| 5760 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 5761 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5762 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5763 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5764 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5765 | if (rdev->raid_disk >= 0) |
| 5766 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5767 | |
| 5768 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5769 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5770 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5771 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5772 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5773 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5774 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5775 | first = rdev->saved_raid_disk; |
| 5776 | |
| 5777 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5778 | p = conf->disks + disk; |
| 5779 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5780 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5781 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5782 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5783 | if (rdev->saved_raid_disk != disk) |
| 5784 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5785 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5786 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5788 | } |
| 5789 | for (disk = first; disk <= last; disk++) { |
| 5790 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5791 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 5792 | p->replacement == NULL) { |
| 5793 | clear_bit(In_sync, &rdev->flags); |
| 5794 | set_bit(Replacement, &rdev->flags); |
| 5795 | rdev->raid_disk = disk; |
| 5796 | err = 0; |
| 5797 | conf->fullsync = 1; |
| 5798 | rcu_assign_pointer(p->replacement, rdev); |
| 5799 | break; |
| 5800 | } |
| 5801 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5802 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5803 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5804 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5805 | } |
| 5806 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5807 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5808 | { |
| 5809 | /* no resync is happening, and there is enough space |
| 5810 | * on all devices, so we can resize. |
| 5811 | * We need to make sure resync covers any new space. |
| 5812 | * If the array is shrinking we should possibly wait until |
| 5813 | * any io in the removed space completes, but it hardly seems |
| 5814 | * worth it. |
| 5815 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5816 | sector_t newsize; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5817 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5818 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 5819 | if (mddev->external_size && |
| 5820 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5821 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5822 | if (mddev->bitmap) { |
| 5823 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 5824 | if (ret) |
| 5825 | return ret; |
| 5826 | } |
| 5827 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5828 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5829 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 5830 | if (sectors > mddev->dev_sectors && |
| 5831 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5832 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5833 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5834 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5835 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5836 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5837 | return 0; |
| 5838 | } |
| 5839 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5840 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5841 | { |
| 5842 | /* Can only proceed if there are plenty of stripe_heads. |
| 5843 | * We need a minimum of one full stripe,, and for sensible progress |
| 5844 | * it is best to have about 4 times that. |
| 5845 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5846 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5847 | * If the chunk size is greater, user-space should request more |
| 5848 | * stripe_heads first. |
| 5849 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5850 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5851 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5852 | > conf->max_nr_stripes || |
| 5853 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5854 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5855 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5856 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5857 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5858 | / STRIPE_SIZE)*4); |
| 5859 | return 0; |
| 5860 | } |
| 5861 | return 1; |
| 5862 | } |
| 5863 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5864 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5865 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5866 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5867 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5868 | if (mddev->delta_disks == 0 && |
| 5869 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5870 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5871 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5872 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5873 | return -EINVAL; |
| 5874 | if (mddev->delta_disks < 0) { |
| 5875 | /* We might be able to shrink, but the devices must |
| 5876 | * be made bigger first. |
| 5877 | * For raid6, 4 is the minimum size. |
| 5878 | * Otherwise 2 is the minimum |
| 5879 | */ |
| 5880 | int min = 2; |
| 5881 | if (mddev->level == 6) |
| 5882 | min = 4; |
| 5883 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5884 | return -EINVAL; |
| 5885 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5886 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5887 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5888 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5889 | |
NeilBrown | e56108d6 | 2012-10-11 14:24:13 +1100 | [diff] [blame] | 5890 | return resize_stripes(conf, (conf->previous_raid_disks |
| 5891 | + mddev->delta_disks)); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5892 | } |
| 5893 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5894 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5895 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5896 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5897 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5898 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5899 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5900 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5901 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5902 | return -EBUSY; |
| 5903 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5904 | if (!check_stripe_cache(mddev)) |
| 5905 | return -ENOSPC; |
| 5906 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 5907 | if (has_failed(conf)) |
| 5908 | return -EINVAL; |
| 5909 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5910 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5911 | if (!test_bit(In_sync, &rdev->flags) |
| 5912 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5913 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5914 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5915 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5916 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5917 | /* Not enough devices even to make a degraded array |
| 5918 | * of that size |
| 5919 | */ |
| 5920 | return -EINVAL; |
| 5921 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5922 | /* Refuse to reduce size of the array. Any reductions in |
| 5923 | * array size must be through explicit setting of array_size |
| 5924 | * attribute. |
| 5925 | */ |
| 5926 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5927 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5928 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5929 | "before number of disks\n", mdname(mddev)); |
| 5930 | return -EINVAL; |
| 5931 | } |
| 5932 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5933 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5934 | spin_lock_irq(&conf->device_lock); |
| 5935 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5936 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5937 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5938 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5939 | conf->prev_algo = conf->algorithm; |
| 5940 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5941 | conf->generation++; |
| 5942 | /* Code that selects data_offset needs to see the generation update |
| 5943 | * if reshape_progress has been set - so a memory barrier needed. |
| 5944 | */ |
| 5945 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5946 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5947 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5948 | else |
| 5949 | conf->reshape_progress = 0; |
| 5950 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5951 | spin_unlock_irq(&conf->device_lock); |
| 5952 | |
| 5953 | /* Add some new drives, as many as will fit. |
| 5954 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 5955 | * Don't add devices if we are reducing the number of |
| 5956 | * devices in the array. This is because it is not possible |
| 5957 | * to correctly record the "partially reconstructed" state of |
| 5958 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5959 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5960 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5961 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5962 | if (rdev->raid_disk < 0 && |
| 5963 | !test_bit(Faulty, &rdev->flags)) { |
| 5964 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5965 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5966 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5967 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5968 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5969 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5970 | |
| 5971 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5972 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5973 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5974 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 5975 | && !test_bit(Faulty, &rdev->flags)) { |
| 5976 | /* This is a spare that was manually added */ |
| 5977 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5978 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5979 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5980 | /* When a reshape changes the number of devices, |
| 5981 | * ->degraded is measured against the larger of the |
| 5982 | * pre and post number of devices. |
| 5983 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5984 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5985 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5986 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5987 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5988 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 5989 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b42 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5990 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5991 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5992 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5993 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5994 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5995 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5996 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5997 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5998 | if (!mddev->sync_thread) { |
| 5999 | mddev->recovery = 0; |
| 6000 | spin_lock_irq(&conf->device_lock); |
| 6001 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6002 | rdev_for_each(rdev, mddev) |
| 6003 | rdev->new_data_offset = rdev->data_offset; |
| 6004 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6005 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 6006 | mddev->reshape_position = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6007 | spin_unlock_irq(&conf->device_lock); |
| 6008 | return -EAGAIN; |
| 6009 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6010 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6011 | md_wakeup_thread(mddev->sync_thread); |
| 6012 | md_new_event(mddev); |
| 6013 | return 0; |
| 6014 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6015 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6016 | /* This is called from the reshape thread and should make any |
| 6017 | * changes needed in 'conf' |
| 6018 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6019 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6020 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6021 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6022 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6023 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6024 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6025 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6026 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6027 | rdev_for_each(rdev, conf->mddev) |
| 6028 | rdev->data_offset = rdev->new_data_offset; |
| 6029 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6030 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6031 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 6032 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6033 | |
| 6034 | /* read-ahead size must cover two whole stripes, which is |
| 6035 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 6036 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 6037 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6038 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6039 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6040 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6041 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 6042 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 6043 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6044 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6045 | } |
| 6046 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6047 | /* This is called from the raid5d thread with mddev_lock held. |
| 6048 | * It makes config changes to the device. |
| 6049 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6050 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6051 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6052 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6053 | |
| 6054 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6055 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6056 | if (mddev->delta_disks > 0) { |
| 6057 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 6058 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6059 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6060 | } else { |
| 6061 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6062 | spin_lock_irq(&conf->device_lock); |
| 6063 | mddev->degraded = calc_degraded(conf); |
| 6064 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6065 | for (d = conf->raid_disks ; |
| 6066 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 6067 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6068 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 6069 | if (rdev) |
| 6070 | clear_bit(In_sync, &rdev->flags); |
| 6071 | rdev = conf->disks[d].replacement; |
| 6072 | if (rdev) |
| 6073 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 6074 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6075 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6076 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6077 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6078 | mddev->reshape_position = MaxSector; |
| 6079 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6080 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6081 | } |
| 6082 | } |
| 6083 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6084 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6085 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6086 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6087 | |
| 6088 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 6089 | case 2: /* resume for a suspend */ |
| 6090 | wake_up(&conf->wait_for_overlap); |
| 6091 | break; |
| 6092 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6093 | case 1: /* stop all writes */ |
| 6094 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6095 | /* '2' tells resync/reshape to pause so that all |
| 6096 | * active stripes can drain |
| 6097 | */ |
| 6098 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6099 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6100 | atomic_read(&conf->active_stripes) == 0 && |
| 6101 | atomic_read(&conf->active_aligned_reads) == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 6102 | conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6103 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6104 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6105 | /* allow reshape to continue */ |
| 6106 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6107 | break; |
| 6108 | |
| 6109 | case 0: /* re-enable writes */ |
| 6110 | spin_lock_irq(&conf->device_lock); |
| 6111 | conf->quiesce = 0; |
| 6112 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 6113 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6114 | spin_unlock_irq(&conf->device_lock); |
| 6115 | break; |
| 6116 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6117 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 6118 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6119 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6120 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6121 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6122 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 6123 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6124 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6125 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6126 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6127 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 6128 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6129 | return ERR_PTR(-EINVAL); |
| 6130 | } |
| 6131 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6132 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 6133 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 6134 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6135 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6136 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6137 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 6138 | mddev->raid_disks += 1; |
| 6139 | mddev->delta_disks = 1; |
| 6140 | /* make sure it will be not marked as dirty */ |
| 6141 | mddev->recovery_cp = MaxSector; |
| 6142 | |
| 6143 | return setup_conf(mddev); |
| 6144 | } |
| 6145 | |
| 6146 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6147 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6148 | { |
| 6149 | int chunksect; |
| 6150 | |
| 6151 | if (mddev->raid_disks != 2 || |
| 6152 | mddev->degraded > 1) |
| 6153 | return ERR_PTR(-EINVAL); |
| 6154 | |
| 6155 | /* Should check if there are write-behind devices? */ |
| 6156 | |
| 6157 | chunksect = 64*2; /* 64K by default */ |
| 6158 | |
| 6159 | /* The array must be an exact multiple of chunksize */ |
| 6160 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 6161 | chunksect >>= 1; |
| 6162 | |
| 6163 | if ((chunksect<<9) < STRIPE_SIZE) |
| 6164 | /* array size does not allow a suitable chunk size */ |
| 6165 | return ERR_PTR(-EINVAL); |
| 6166 | |
| 6167 | mddev->new_level = 5; |
| 6168 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6169 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6170 | |
| 6171 | return setup_conf(mddev); |
| 6172 | } |
| 6173 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6174 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6175 | { |
| 6176 | int new_layout; |
| 6177 | |
| 6178 | switch (mddev->layout) { |
| 6179 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 6180 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 6181 | break; |
| 6182 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 6183 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 6184 | break; |
| 6185 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 6186 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 6187 | break; |
| 6188 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 6189 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 6190 | break; |
| 6191 | case ALGORITHM_PARITY_0_6: |
| 6192 | new_layout = ALGORITHM_PARITY_0; |
| 6193 | break; |
| 6194 | case ALGORITHM_PARITY_N: |
| 6195 | new_layout = ALGORITHM_PARITY_N; |
| 6196 | break; |
| 6197 | default: |
| 6198 | return ERR_PTR(-EINVAL); |
| 6199 | } |
| 6200 | mddev->new_level = 5; |
| 6201 | mddev->new_layout = new_layout; |
| 6202 | mddev->delta_disks = -1; |
| 6203 | mddev->raid_disks -= 1; |
| 6204 | return setup_conf(mddev); |
| 6205 | } |
| 6206 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6207 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6208 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6209 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6210 | /* For a 2-drive array, the layout and chunk size can be changed |
| 6211 | * immediately as not restriping is needed. |
| 6212 | * For larger arrays we record the new value - after validation |
| 6213 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6214 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6215 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6216 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6217 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6218 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6219 | return -EINVAL; |
| 6220 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6221 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6222 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6223 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6224 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6225 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6226 | /* not factor of array size */ |
| 6227 | return -EINVAL; |
| 6228 | } |
| 6229 | |
| 6230 | /* They look valid */ |
| 6231 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6232 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6233 | /* can make the change immediately */ |
| 6234 | if (mddev->new_layout >= 0) { |
| 6235 | conf->algorithm = mddev->new_layout; |
| 6236 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6237 | } |
| 6238 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6239 | conf->chunk_sectors = new_chunk ; |
| 6240 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6241 | } |
| 6242 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 6243 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6244 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6245 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6246 | } |
| 6247 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6248 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6249 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6250 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6251 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6252 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6253 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6254 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6255 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6256 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6257 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6258 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6259 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6260 | /* not factor of array size */ |
| 6261 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6262 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6263 | |
| 6264 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6265 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6266 | } |
| 6267 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6268 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6269 | { |
| 6270 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6271 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6272 | * raid1 - if there are two drives. We need to know the chunk size |
| 6273 | * raid4 - trivial - just use a raid4 layout. |
| 6274 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6275 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6276 | if (mddev->level == 0) |
| 6277 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6278 | if (mddev->level == 1) |
| 6279 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 6280 | if (mddev->level == 4) { |
| 6281 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6282 | mddev->new_level = 5; |
| 6283 | return setup_conf(mddev); |
| 6284 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6285 | if (mddev->level == 6) |
| 6286 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6287 | |
| 6288 | return ERR_PTR(-EINVAL); |
| 6289 | } |
| 6290 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6291 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6292 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6293 | /* raid4 can take over: |
| 6294 | * raid0 - if there is only one strip zone |
| 6295 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6296 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6297 | if (mddev->level == 0) |
| 6298 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6299 | if (mddev->level == 5 && |
| 6300 | mddev->layout == ALGORITHM_PARITY_N) { |
| 6301 | mddev->new_layout = 0; |
| 6302 | mddev->new_level = 4; |
| 6303 | return setup_conf(mddev); |
| 6304 | } |
| 6305 | return ERR_PTR(-EINVAL); |
| 6306 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6307 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6308 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6309 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6310 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6311 | { |
| 6312 | /* Currently can only take over a raid5. We map the |
| 6313 | * personality to an equivalent raid6 personality |
| 6314 | * with the Q block at the end. |
| 6315 | */ |
| 6316 | int new_layout; |
| 6317 | |
| 6318 | if (mddev->pers != &raid5_personality) |
| 6319 | return ERR_PTR(-EINVAL); |
| 6320 | if (mddev->degraded > 1) |
| 6321 | return ERR_PTR(-EINVAL); |
| 6322 | if (mddev->raid_disks > 253) |
| 6323 | return ERR_PTR(-EINVAL); |
| 6324 | if (mddev->raid_disks < 3) |
| 6325 | return ERR_PTR(-EINVAL); |
| 6326 | |
| 6327 | switch (mddev->layout) { |
| 6328 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 6329 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 6330 | break; |
| 6331 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 6332 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 6333 | break; |
| 6334 | case ALGORITHM_LEFT_SYMMETRIC: |
| 6335 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 6336 | break; |
| 6337 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 6338 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 6339 | break; |
| 6340 | case ALGORITHM_PARITY_0: |
| 6341 | new_layout = ALGORITHM_PARITY_0_6; |
| 6342 | break; |
| 6343 | case ALGORITHM_PARITY_N: |
| 6344 | new_layout = ALGORITHM_PARITY_N; |
| 6345 | break; |
| 6346 | default: |
| 6347 | return ERR_PTR(-EINVAL); |
| 6348 | } |
| 6349 | mddev->new_level = 6; |
| 6350 | mddev->new_layout = new_layout; |
| 6351 | mddev->delta_disks = 1; |
| 6352 | mddev->raid_disks += 1; |
| 6353 | return setup_conf(mddev); |
| 6354 | } |
| 6355 | |
| 6356 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6357 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6358 | { |
| 6359 | .name = "raid6", |
| 6360 | .level = 6, |
| 6361 | .owner = THIS_MODULE, |
| 6362 | .make_request = make_request, |
| 6363 | .run = run, |
| 6364 | .stop = stop, |
| 6365 | .status = status, |
| 6366 | .error_handler = error, |
| 6367 | .hot_add_disk = raid5_add_disk, |
| 6368 | .hot_remove_disk= raid5_remove_disk, |
| 6369 | .spare_active = raid5_spare_active, |
| 6370 | .sync_request = sync_request, |
| 6371 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6372 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6373 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6374 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6375 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6376 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6377 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6378 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6379 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6380 | { |
| 6381 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6382 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6383 | .owner = THIS_MODULE, |
| 6384 | .make_request = make_request, |
| 6385 | .run = run, |
| 6386 | .stop = stop, |
| 6387 | .status = status, |
| 6388 | .error_handler = error, |
| 6389 | .hot_add_disk = raid5_add_disk, |
| 6390 | .hot_remove_disk= raid5_remove_disk, |
| 6391 | .spare_active = raid5_spare_active, |
| 6392 | .sync_request = sync_request, |
| 6393 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6394 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6395 | .check_reshape = raid5_check_reshape, |
| 6396 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6397 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6398 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6399 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6400 | }; |
| 6401 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6402 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6404 | .name = "raid4", |
| 6405 | .level = 4, |
| 6406 | .owner = THIS_MODULE, |
| 6407 | .make_request = make_request, |
| 6408 | .run = run, |
| 6409 | .stop = stop, |
| 6410 | .status = status, |
| 6411 | .error_handler = error, |
| 6412 | .hot_add_disk = raid5_add_disk, |
| 6413 | .hot_remove_disk= raid5_remove_disk, |
| 6414 | .spare_active = raid5_spare_active, |
| 6415 | .sync_request = sync_request, |
| 6416 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6417 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 6418 | .check_reshape = raid5_check_reshape, |
| 6419 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6420 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6421 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6422 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6423 | }; |
| 6424 | |
| 6425 | static int __init raid5_init(void) |
| 6426 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6427 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6428 | register_md_personality(&raid5_personality); |
| 6429 | register_md_personality(&raid4_personality); |
| 6430 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6431 | } |
| 6432 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6433 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6434 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6435 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6436 | unregister_md_personality(&raid5_personality); |
| 6437 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6438 | } |
| 6439 | |
| 6440 | module_init(raid5_init); |
| 6441 | module_exit(raid5_exit); |
| 6442 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6443 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6444 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 6445 | MODULE_ALIAS("md-raid5"); |
| 6446 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6447 | MODULE_ALIAS("md-level-5"); |
| 6448 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6449 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 6450 | MODULE_ALIAS("md-raid6"); |
| 6451 | MODULE_ALIAS("md-level-6"); |
| 6452 | |
| 6453 | /* This used to be two separate modules, they were: */ |
| 6454 | MODULE_ALIAS("raid5"); |
| 6455 | MODULE_ALIAS("raid6"); |