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; |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 187 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 188 | bi = return_bi; |
| 189 | } |
| 190 | } |
| 191 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 192 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 193 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 194 | static int stripe_operations_active(struct stripe_head *sh) |
| 195 | { |
| 196 | return sh->check_state || sh->reconstruct_state || |
| 197 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 198 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 199 | } |
| 200 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 201 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 202 | { |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 203 | BUG_ON(!list_empty(&sh->lru)); |
| 204 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
| 205 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 206 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
| 207 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 208 | list_add_tail(&sh->lru, &conf->delayed_list); |
| 209 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
| 210 | sh->bm_seq - conf->seq_write > 0) |
| 211 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 212 | else { |
| 213 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 214 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
| 215 | list_add_tail(&sh->lru, &conf->handle_list); |
| 216 | } |
| 217 | md_wakeup_thread(conf->mddev->thread); |
| 218 | } else { |
| 219 | BUG_ON(stripe_operations_active(sh)); |
| 220 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 221 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 222 | < IO_THRESHOLD) |
| 223 | md_wakeup_thread(conf->mddev->thread); |
| 224 | atomic_dec(&conf->active_stripes); |
| 225 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 226 | list_add_tail(&sh->lru, &conf->inactive_list); |
| 227 | wake_up(&conf->wait_for_stripe); |
| 228 | if (conf->retry_read_aligned) |
| 229 | md_wakeup_thread(conf->mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 230 | } |
| 231 | } |
| 232 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 233 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 234 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh) |
| 235 | { |
| 236 | if (atomic_dec_and_test(&sh->count)) |
| 237 | do_release_stripe(conf, sh); |
| 238 | } |
| 239 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | static void release_stripe(struct stripe_head *sh) |
| 241 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 242 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | unsigned long flags; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 244 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 245 | local_irq_save(flags); |
| 246 | if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { |
| 247 | do_release_stripe(conf, sh); |
| 248 | spin_unlock(&conf->device_lock); |
| 249 | } |
| 250 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 251 | } |
| 252 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 253 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 254 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 255 | pr_debug("remove_hash(), stripe %llu\n", |
| 256 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 257 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 258 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 261 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 262 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 263 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 265 | pr_debug("insert_hash(), stripe %llu\n", |
| 266 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 268 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | |
| 272 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 273 | static struct stripe_head *get_free_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 274 | { |
| 275 | struct stripe_head *sh = NULL; |
| 276 | struct list_head *first; |
| 277 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 278 | if (list_empty(&conf->inactive_list)) |
| 279 | goto out; |
| 280 | first = conf->inactive_list.next; |
| 281 | sh = list_entry(first, struct stripe_head, lru); |
| 282 | list_del_init(first); |
| 283 | remove_hash(sh); |
| 284 | atomic_inc(&conf->active_stripes); |
| 285 | out: |
| 286 | return sh; |
| 287 | } |
| 288 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 289 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 290 | { |
| 291 | struct page *p; |
| 292 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 293 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 295 | for (i = 0; i < num ; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 296 | p = sh->dev[i].page; |
| 297 | if (!p) |
| 298 | continue; |
| 299 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 300 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
| 303 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 304 | static int grow_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 305 | { |
| 306 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 307 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 309 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | struct page *page; |
| 311 | |
| 312 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 313 | return 1; |
| 314 | } |
| 315 | sh->dev[i].page = page; |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 320 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 321 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 322 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 324 | 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] | 325 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 326 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 327 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 328 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 329 | BUG_ON(atomic_read(&sh->count) != 0); |
| 330 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 331 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 332 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 333 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 334 | (unsigned long long)sh->sector); |
| 335 | |
| 336 | remove_hash(sh); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 337 | |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 338 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 339 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 340 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 341 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 342 | sh->state = 0; |
| 343 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 344 | |
| 345 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 346 | struct r5dev *dev = &sh->dev[i]; |
| 347 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 348 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 349 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 350 | 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] | 351 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 352 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 353 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 354 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 355 | } |
| 356 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 357 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 358 | } |
| 359 | insert_hash(conf, sh); |
| 360 | } |
| 361 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 362 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 363 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 364 | { |
| 365 | struct stripe_head *sh; |
| 366 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 367 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 368 | hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 369 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 370 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 371 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | return NULL; |
| 373 | } |
| 374 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 375 | /* |
| 376 | * Need to check if array has failed when deciding whether to: |
| 377 | * - start an array |
| 378 | * - remove non-faulty devices |
| 379 | * - add a spare |
| 380 | * - allow a reshape |
| 381 | * This determination is simple when no reshape is happening. |
| 382 | * However if there is a reshape, we need to carefully check |
| 383 | * both the before and after sections. |
| 384 | * This is because some failed devices may only affect one |
| 385 | * of the two sections, and some non-in_sync devices may |
| 386 | * be insync in the section most affected by failed devices. |
| 387 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 388 | static int calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 389 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 390 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 391 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 392 | |
| 393 | rcu_read_lock(); |
| 394 | degraded = 0; |
| 395 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 396 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 397 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 398 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 399 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 400 | degraded++; |
| 401 | else if (test_bit(In_sync, &rdev->flags)) |
| 402 | ; |
| 403 | else |
| 404 | /* not in-sync or faulty. |
| 405 | * If the reshape increases the number of devices, |
| 406 | * this is being recovered by the reshape, so |
| 407 | * this 'previous' section is not in_sync. |
| 408 | * If the number of devices is being reduced however, |
| 409 | * the device can only be part of the array if |
| 410 | * we are reverting a reshape, so this section will |
| 411 | * be in-sync. |
| 412 | */ |
| 413 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 414 | degraded++; |
| 415 | } |
| 416 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 417 | if (conf->raid_disks == conf->previous_raid_disks) |
| 418 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 419 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 420 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 421 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 422 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 423 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 424 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 425 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 426 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 427 | else if (test_bit(In_sync, &rdev->flags)) |
| 428 | ; |
| 429 | else |
| 430 | /* not in-sync or faulty. |
| 431 | * If reshape increases the number of devices, this |
| 432 | * section has already been recovered, else it |
| 433 | * almost certainly hasn't. |
| 434 | */ |
| 435 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 436 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 437 | } |
| 438 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 439 | if (degraded2 > degraded) |
| 440 | return degraded2; |
| 441 | return degraded; |
| 442 | } |
| 443 | |
| 444 | static int has_failed(struct r5conf *conf) |
| 445 | { |
| 446 | int degraded; |
| 447 | |
| 448 | if (conf->mddev->reshape_position == MaxSector) |
| 449 | return conf->mddev->degraded > conf->max_degraded; |
| 450 | |
| 451 | degraded = calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 452 | if (degraded > conf->max_degraded) |
| 453 | return 1; |
| 454 | return 0; |
| 455 | } |
| 456 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 457 | static struct stripe_head * |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 458 | get_active_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 459 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | { |
| 461 | struct stripe_head *sh; |
| 462 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 463 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | |
| 465 | spin_lock_irq(&conf->device_lock); |
| 466 | |
| 467 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 468 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 469 | conf->quiesce == 0 || noquiesce, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 470 | conf->device_lock); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 471 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | if (!sh) { |
| 473 | if (!conf->inactive_blocked) |
| 474 | sh = get_free_stripe(conf); |
| 475 | if (noblock && sh == NULL) |
| 476 | break; |
| 477 | if (!sh) { |
| 478 | conf->inactive_blocked = 1; |
| 479 | wait_event_lock_irq(conf->wait_for_stripe, |
| 480 | !list_empty(&conf->inactive_list) && |
NeilBrown | 5036805 | 2005-12-12 02:39:17 -0800 | [diff] [blame] | 481 | (atomic_read(&conf->active_stripes) |
| 482 | < (conf->max_nr_stripes *3/4) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | || !conf->inactive_blocked), |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 484 | conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | conf->inactive_blocked = 0; |
| 486 | } else |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 487 | init_stripe(sh, sector, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } else { |
| 489 | if (atomic_read(&sh->count)) { |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 490 | BUG_ON(!list_empty(&sh->lru) |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 491 | && !test_bit(STRIPE_EXPANDING, &sh->state) |
| 492 | && !test_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | } else { |
| 494 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 495 | atomic_inc(&conf->active_stripes); |
NeilBrown | ff4e8d9 | 2006-07-10 04:44:16 -0700 | [diff] [blame] | 496 | if (list_empty(&sh->lru) && |
| 497 | !test_bit(STRIPE_EXPANDING, &sh->state)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 498 | BUG(); |
| 499 | list_del_init(&sh->lru); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | } while (sh == NULL); |
| 503 | |
| 504 | if (sh) |
| 505 | atomic_inc(&sh->count); |
| 506 | |
| 507 | spin_unlock_irq(&conf->device_lock); |
| 508 | return sh; |
| 509 | } |
| 510 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 511 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 512 | * in this stripe_head. |
| 513 | */ |
| 514 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 515 | { |
| 516 | sector_t progress = conf->reshape_progress; |
| 517 | /* Need a memory barrier to make sure we see the value |
| 518 | * of conf->generation, or ->data_offset that was set before |
| 519 | * reshape_progress was updated. |
| 520 | */ |
| 521 | smp_rmb(); |
| 522 | if (progress == MaxSector) |
| 523 | return 0; |
| 524 | if (sh->generation == conf->generation - 1) |
| 525 | return 0; |
| 526 | /* We are in a reshape, and this is a new-generation stripe, |
| 527 | * so use new_data_offset. |
| 528 | */ |
| 529 | return 1; |
| 530 | } |
| 531 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 532 | static void |
| 533 | raid5_end_read_request(struct bio *bi, int error); |
| 534 | static void |
| 535 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 536 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 537 | 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] | 538 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 539 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 540 | int i, disks = sh->disks; |
| 541 | |
| 542 | might_sleep(); |
| 543 | |
| 544 | for (i = disks; i--; ) { |
| 545 | int rw; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 546 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 547 | struct bio *bi, *rbi; |
| 548 | struct md_rdev *rdev, *rrdev = NULL; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 549 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 550 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 551 | rw = WRITE_FUA; |
| 552 | else |
| 553 | rw = WRITE; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 554 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 555 | rw |= REQ_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 556 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 557 | rw = READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 558 | else if (test_and_clear_bit(R5_WantReplace, |
| 559 | &sh->dev[i].flags)) { |
| 560 | rw = WRITE; |
| 561 | replace_only = 1; |
| 562 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 563 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 564 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
| 565 | rw |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 566 | |
| 567 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 568 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 569 | |
| 570 | bi->bi_rw = rw; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 571 | rbi->bi_rw = rw; |
| 572 | if (rw & WRITE) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 573 | bi->bi_end_io = raid5_end_write_request; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 574 | rbi->bi_end_io = raid5_end_write_request; |
| 575 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 576 | bi->bi_end_io = raid5_end_read_request; |
| 577 | |
| 578 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 579 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 580 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 581 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 582 | if (!rdev) { |
| 583 | rdev = rrdev; |
| 584 | rrdev = NULL; |
| 585 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 586 | if (rw & WRITE) { |
| 587 | if (replace_only) |
| 588 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 589 | if (rdev == rrdev) |
| 590 | /* We raced and saw duplicates */ |
| 591 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 592 | } else { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 593 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 594 | rdev = rrdev; |
| 595 | rrdev = NULL; |
| 596 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 597 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 598 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 599 | rdev = NULL; |
| 600 | if (rdev) |
| 601 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 602 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 603 | rrdev = NULL; |
| 604 | if (rrdev) |
| 605 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 606 | rcu_read_unlock(); |
| 607 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 608 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 609 | * need to check for writes. We never accept write errors |
| 610 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 611 | */ |
| 612 | while ((rw & WRITE) && rdev && |
| 613 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 614 | sector_t first_bad; |
| 615 | int bad_sectors; |
| 616 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 617 | &first_bad, &bad_sectors); |
| 618 | if (!bad) |
| 619 | break; |
| 620 | |
| 621 | if (bad < 0) { |
| 622 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 623 | if (!conf->mddev->external && |
| 624 | conf->mddev->flags) { |
| 625 | /* It is very unlikely, but we might |
| 626 | * still need to write out the |
| 627 | * bad block log - better give it |
| 628 | * a chance*/ |
| 629 | md_check_recovery(conf->mddev); |
| 630 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 631 | /* |
| 632 | * Because md_wait_for_blocked_rdev |
| 633 | * will dec nr_pending, we must |
| 634 | * increment it first. |
| 635 | */ |
| 636 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 637 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 638 | } else { |
| 639 | /* Acknowledged bad block - skip the write */ |
| 640 | rdev_dec_pending(rdev, conf->mddev); |
| 641 | rdev = NULL; |
| 642 | } |
| 643 | } |
| 644 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 645 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 646 | if (s->syncing || s->expanding || s->expanded |
| 647 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 648 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 649 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 650 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 651 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 652 | bi->bi_bdev = rdev->bdev; |
| 653 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 654 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 655 | bi->bi_rw, i); |
| 656 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 657 | if (use_new_offset(conf, sh)) |
| 658 | bi->bi_sector = (sh->sector |
| 659 | + rdev->new_data_offset); |
| 660 | else |
| 661 | bi->bi_sector = (sh->sector |
| 662 | + rdev->data_offset); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 663 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 664 | bi->bi_rw |= REQ_FLUSH; |
| 665 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 666 | bi->bi_flags = 1 << BIO_UPTODATE; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 667 | bi->bi_idx = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 668 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 669 | bi->bi_io_vec[0].bv_offset = 0; |
| 670 | bi->bi_size = STRIPE_SIZE; |
| 671 | bi->bi_next = NULL; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 672 | if (rrdev) |
| 673 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 674 | |
| 675 | if (conf->mddev->gendisk) |
| 676 | trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), |
| 677 | bi, disk_devt(conf->mddev->gendisk), |
| 678 | sh->dev[i].sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 679 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 680 | } |
| 681 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 682 | if (s->syncing || s->expanding || s->expanded |
| 683 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 684 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 685 | |
| 686 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 687 | |
| 688 | rbi->bi_bdev = rrdev->bdev; |
| 689 | pr_debug("%s: for %llu schedule op %ld on " |
| 690 | "replacement disc %d\n", |
| 691 | __func__, (unsigned long long)sh->sector, |
| 692 | rbi->bi_rw, i); |
| 693 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 694 | if (use_new_offset(conf, sh)) |
| 695 | rbi->bi_sector = (sh->sector |
| 696 | + rrdev->new_data_offset); |
| 697 | else |
| 698 | rbi->bi_sector = (sh->sector |
| 699 | + rrdev->data_offset); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 700 | rbi->bi_flags = 1 << BIO_UPTODATE; |
| 701 | rbi->bi_idx = 0; |
| 702 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 703 | rbi->bi_io_vec[0].bv_offset = 0; |
| 704 | rbi->bi_size = STRIPE_SIZE; |
| 705 | rbi->bi_next = NULL; |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 706 | if (conf->mddev->gendisk) |
| 707 | trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), |
| 708 | rbi, disk_devt(conf->mddev->gendisk), |
| 709 | sh->dev[i].sector); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 710 | generic_make_request(rbi); |
| 711 | } |
| 712 | if (!rdev && !rrdev) { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 713 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 714 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 715 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 716 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 717 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 718 | set_bit(STRIPE_HANDLE, &sh->state); |
| 719 | } |
| 720 | } |
| 721 | } |
| 722 | |
| 723 | static struct dma_async_tx_descriptor * |
| 724 | async_copy_data(int frombio, struct bio *bio, struct page *page, |
| 725 | sector_t sector, struct dma_async_tx_descriptor *tx) |
| 726 | { |
| 727 | struct bio_vec *bvl; |
| 728 | struct page *bio_page; |
| 729 | int i; |
| 730 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 731 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 732 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 733 | |
| 734 | if (bio->bi_sector >= sector) |
| 735 | page_offset = (signed)(bio->bi_sector - sector) * 512; |
| 736 | else |
| 737 | page_offset = (signed)(sector - bio->bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 738 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 739 | if (frombio) |
| 740 | flags |= ASYNC_TX_FENCE; |
| 741 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 742 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 743 | bio_for_each_segment(bvl, bio, i) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 744 | int len = bvl->bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 745 | int clen; |
| 746 | int b_offset = 0; |
| 747 | |
| 748 | if (page_offset < 0) { |
| 749 | b_offset = -page_offset; |
| 750 | page_offset += b_offset; |
| 751 | len -= b_offset; |
| 752 | } |
| 753 | |
| 754 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 755 | clen = STRIPE_SIZE - page_offset; |
| 756 | else |
| 757 | clen = len; |
| 758 | |
| 759 | if (clen > 0) { |
Namhyung Kim | fcde907 | 2011-06-14 14:23:57 +1000 | [diff] [blame] | 760 | b_offset += bvl->bv_offset; |
| 761 | bio_page = bvl->bv_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 762 | if (frombio) |
| 763 | tx = async_memcpy(page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 764 | b_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 765 | else |
| 766 | tx = async_memcpy(bio_page, page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 767 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 768 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 769 | /* chain the operations */ |
| 770 | submit.depend_tx = tx; |
| 771 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 772 | if (clen < len) /* hit end of page */ |
| 773 | break; |
| 774 | page_offset += len; |
| 775 | } |
| 776 | |
| 777 | return tx; |
| 778 | } |
| 779 | |
| 780 | static void ops_complete_biofill(void *stripe_head_ref) |
| 781 | { |
| 782 | struct stripe_head *sh = stripe_head_ref; |
| 783 | struct bio *return_bi = NULL; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 784 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 785 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 786 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 787 | (unsigned long long)sh->sector); |
| 788 | |
| 789 | /* clear completed biofills */ |
| 790 | for (i = sh->disks; i--; ) { |
| 791 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 792 | |
| 793 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 794 | /* and check if we need to reply to a read request, |
| 795 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 796 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 797 | */ |
| 798 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 799 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 800 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 801 | BUG_ON(!dev->read); |
| 802 | rbi = dev->read; |
| 803 | dev->read = NULL; |
| 804 | while (rbi && rbi->bi_sector < |
| 805 | dev->sector + STRIPE_SECTORS) { |
| 806 | rbi2 = r5_next_bio(rbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 807 | if (!raid5_dec_bi_active_stripes(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 808 | rbi->bi_next = return_bi; |
| 809 | return_bi = rbi; |
| 810 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 811 | rbi = rbi2; |
| 812 | } |
| 813 | } |
| 814 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 815 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 816 | |
| 817 | return_io(return_bi); |
| 818 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 819 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 820 | release_stripe(sh); |
| 821 | } |
| 822 | |
| 823 | static void ops_run_biofill(struct stripe_head *sh) |
| 824 | { |
| 825 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 826 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 827 | int i; |
| 828 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 829 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 830 | (unsigned long long)sh->sector); |
| 831 | |
| 832 | for (i = sh->disks; i--; ) { |
| 833 | struct r5dev *dev = &sh->dev[i]; |
| 834 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 835 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 836 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 837 | dev->read = rbi = dev->toread; |
| 838 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 839 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 840 | while (rbi && rbi->bi_sector < |
| 841 | dev->sector + STRIPE_SECTORS) { |
| 842 | tx = async_copy_data(0, rbi, dev->page, |
| 843 | dev->sector, tx); |
| 844 | rbi = r5_next_bio(rbi, dev->sector); |
| 845 | } |
| 846 | } |
| 847 | } |
| 848 | |
| 849 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 850 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 851 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 852 | } |
| 853 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 854 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 855 | { |
| 856 | struct r5dev *tgt; |
| 857 | |
| 858 | if (target < 0) |
| 859 | return; |
| 860 | |
| 861 | tgt = &sh->dev[target]; |
| 862 | set_bit(R5_UPTODATE, &tgt->flags); |
| 863 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 864 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 865 | } |
| 866 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 867 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 868 | { |
| 869 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 870 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 871 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 872 | (unsigned long long)sh->sector); |
| 873 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 874 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 875 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 876 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 877 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 878 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 879 | if (sh->check_state == check_state_compute_run) |
| 880 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 881 | set_bit(STRIPE_HANDLE, &sh->state); |
| 882 | release_stripe(sh); |
| 883 | } |
| 884 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 885 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 886 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 887 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 888 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 889 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 890 | } |
| 891 | |
| 892 | static struct dma_async_tx_descriptor * |
| 893 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 894 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 895 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 896 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 897 | int target = sh->ops.target; |
| 898 | struct r5dev *tgt = &sh->dev[target]; |
| 899 | struct page *xor_dest = tgt->page; |
| 900 | int count = 0; |
| 901 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 902 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 903 | int i; |
| 904 | |
| 905 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 906 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 907 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 908 | |
| 909 | for (i = disks; i--; ) |
| 910 | if (i != target) |
| 911 | xor_srcs[count++] = sh->dev[i].page; |
| 912 | |
| 913 | atomic_inc(&sh->count); |
| 914 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 915 | 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] | 916 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 917 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 918 | 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] | 919 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 920 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 921 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 922 | return tx; |
| 923 | } |
| 924 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 925 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 926 | * @srcs - (struct page *) array of size sh->disks |
| 927 | * @sh - stripe_head to parse |
| 928 | * |
| 929 | * Populates srcs in proper layout order for the stripe and returns the |
| 930 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 931 | * destination buffer is recorded in srcs[count] and the Q destination |
| 932 | * is recorded in srcs[count+1]]. |
| 933 | */ |
| 934 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 935 | { |
| 936 | int disks = sh->disks; |
| 937 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 938 | int d0_idx = raid6_d0(sh); |
| 939 | int count; |
| 940 | int i; |
| 941 | |
| 942 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 943 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 944 | |
| 945 | count = 0; |
| 946 | i = d0_idx; |
| 947 | do { |
| 948 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 949 | |
| 950 | srcs[slot] = sh->dev[i].page; |
| 951 | i = raid6_next_disk(i, disks); |
| 952 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 953 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 954 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 955 | } |
| 956 | |
| 957 | static struct dma_async_tx_descriptor * |
| 958 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 959 | { |
| 960 | int disks = sh->disks; |
| 961 | struct page **blocks = percpu->scribble; |
| 962 | int target; |
| 963 | int qd_idx = sh->qd_idx; |
| 964 | struct dma_async_tx_descriptor *tx; |
| 965 | struct async_submit_ctl submit; |
| 966 | struct r5dev *tgt; |
| 967 | struct page *dest; |
| 968 | int i; |
| 969 | int count; |
| 970 | |
| 971 | if (sh->ops.target < 0) |
| 972 | target = sh->ops.target2; |
| 973 | else if (sh->ops.target2 < 0) |
| 974 | target = sh->ops.target; |
| 975 | else |
| 976 | /* we should only have one valid target */ |
| 977 | BUG(); |
| 978 | BUG_ON(target < 0); |
| 979 | pr_debug("%s: stripe %llu block: %d\n", |
| 980 | __func__, (unsigned long long)sh->sector, target); |
| 981 | |
| 982 | tgt = &sh->dev[target]; |
| 983 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 984 | dest = tgt->page; |
| 985 | |
| 986 | atomic_inc(&sh->count); |
| 987 | |
| 988 | if (target == qd_idx) { |
| 989 | count = set_syndrome_sources(blocks, sh); |
| 990 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 991 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 992 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 993 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 994 | to_addr_conv(sh, percpu)); |
| 995 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 996 | } else { |
| 997 | /* Compute any data- or p-drive using XOR */ |
| 998 | count = 0; |
| 999 | for (i = disks; i-- ; ) { |
| 1000 | if (i == target || i == qd_idx) |
| 1001 | continue; |
| 1002 | blocks[count++] = sh->dev[i].page; |
| 1003 | } |
| 1004 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1005 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1006 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1007 | to_addr_conv(sh, percpu)); |
| 1008 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 1009 | } |
| 1010 | |
| 1011 | return tx; |
| 1012 | } |
| 1013 | |
| 1014 | static struct dma_async_tx_descriptor * |
| 1015 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1016 | { |
| 1017 | int i, count, disks = sh->disks; |
| 1018 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1019 | int d0_idx = raid6_d0(sh); |
| 1020 | int faila = -1, failb = -1; |
| 1021 | int target = sh->ops.target; |
| 1022 | int target2 = sh->ops.target2; |
| 1023 | struct r5dev *tgt = &sh->dev[target]; |
| 1024 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1025 | struct dma_async_tx_descriptor *tx; |
| 1026 | struct page **blocks = percpu->scribble; |
| 1027 | struct async_submit_ctl submit; |
| 1028 | |
| 1029 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1030 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1031 | BUG_ON(target < 0 || target2 < 0); |
| 1032 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1033 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1034 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1035 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1036 | * slot number conversion for 'faila' and 'failb' |
| 1037 | */ |
| 1038 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1039 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1040 | count = 0; |
| 1041 | i = d0_idx; |
| 1042 | do { |
| 1043 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1044 | |
| 1045 | blocks[slot] = sh->dev[i].page; |
| 1046 | |
| 1047 | if (i == target) |
| 1048 | faila = slot; |
| 1049 | if (i == target2) |
| 1050 | failb = slot; |
| 1051 | i = raid6_next_disk(i, disks); |
| 1052 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1053 | |
| 1054 | BUG_ON(faila == failb); |
| 1055 | if (failb < faila) |
| 1056 | swap(faila, failb); |
| 1057 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1058 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1059 | |
| 1060 | atomic_inc(&sh->count); |
| 1061 | |
| 1062 | if (failb == syndrome_disks+1) { |
| 1063 | /* Q disk is one of the missing disks */ |
| 1064 | if (faila == syndrome_disks) { |
| 1065 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1066 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1067 | ops_complete_compute, sh, |
| 1068 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1069 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1070 | STRIPE_SIZE, &submit); |
| 1071 | } else { |
| 1072 | struct page *dest; |
| 1073 | int data_target; |
| 1074 | int qd_idx = sh->qd_idx; |
| 1075 | |
| 1076 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1077 | if (target == qd_idx) |
| 1078 | data_target = target2; |
| 1079 | else |
| 1080 | data_target = target; |
| 1081 | |
| 1082 | count = 0; |
| 1083 | for (i = disks; i-- ; ) { |
| 1084 | if (i == data_target || i == qd_idx) |
| 1085 | continue; |
| 1086 | blocks[count++] = sh->dev[i].page; |
| 1087 | } |
| 1088 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1089 | init_async_submit(&submit, |
| 1090 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1091 | NULL, NULL, NULL, |
| 1092 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1093 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1094 | &submit); |
| 1095 | |
| 1096 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1097 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1098 | ops_complete_compute, sh, |
| 1099 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1100 | return async_gen_syndrome(blocks, 0, count+2, |
| 1101 | STRIPE_SIZE, &submit); |
| 1102 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1103 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1104 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1105 | ops_complete_compute, sh, |
| 1106 | to_addr_conv(sh, percpu)); |
| 1107 | if (failb == syndrome_disks) { |
| 1108 | /* We're missing D+P. */ |
| 1109 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1110 | STRIPE_SIZE, faila, |
| 1111 | blocks, &submit); |
| 1112 | } else { |
| 1113 | /* We're missing D+D. */ |
| 1114 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1115 | STRIPE_SIZE, faila, failb, |
| 1116 | blocks, &submit); |
| 1117 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1118 | } |
| 1119 | } |
| 1120 | |
| 1121 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1122 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1123 | { |
| 1124 | struct stripe_head *sh = stripe_head_ref; |
| 1125 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1126 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1127 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1128 | } |
| 1129 | |
| 1130 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1131 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1132 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1133 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1134 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1135 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1136 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1137 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1138 | |
| 1139 | /* existing parity data subtracted */ |
| 1140 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1141 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1142 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1143 | (unsigned long long)sh->sector); |
| 1144 | |
| 1145 | for (i = disks; i--; ) { |
| 1146 | struct r5dev *dev = &sh->dev[i]; |
| 1147 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1148 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1149 | xor_srcs[count++] = dev->page; |
| 1150 | } |
| 1151 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1152 | 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] | 1153 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1154 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1155 | |
| 1156 | return tx; |
| 1157 | } |
| 1158 | |
| 1159 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1160 | 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] | 1161 | { |
| 1162 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1163 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1164 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1165 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1166 | (unsigned long long)sh->sector); |
| 1167 | |
| 1168 | for (i = disks; i--; ) { |
| 1169 | struct r5dev *dev = &sh->dev[i]; |
| 1170 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1171 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1172 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1173 | struct bio *wbi; |
| 1174 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1175 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1176 | chosen = dev->towrite; |
| 1177 | dev->towrite = NULL; |
| 1178 | BUG_ON(dev->written); |
| 1179 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1180 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1181 | |
| 1182 | while (wbi && wbi->bi_sector < |
| 1183 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1184 | if (wbi->bi_rw & REQ_FUA) |
| 1185 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1186 | if (wbi->bi_rw & REQ_SYNC) |
| 1187 | set_bit(R5_SyncIO, &dev->flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1188 | if (wbi->bi_rw & REQ_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1189 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1190 | else |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1191 | tx = async_copy_data(1, wbi, dev->page, |
| 1192 | dev->sector, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1193 | wbi = r5_next_bio(wbi, dev->sector); |
| 1194 | } |
| 1195 | } |
| 1196 | } |
| 1197 | |
| 1198 | return tx; |
| 1199 | } |
| 1200 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1201 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1202 | { |
| 1203 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1204 | int disks = sh->disks; |
| 1205 | int pd_idx = sh->pd_idx; |
| 1206 | int qd_idx = sh->qd_idx; |
| 1207 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1208 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1209 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1210 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1211 | (unsigned long long)sh->sector); |
| 1212 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1213 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1214 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1215 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1216 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1217 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1218 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1219 | for (i = disks; i--; ) { |
| 1220 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1221 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1222 | if (dev->written || i == pd_idx || i == qd_idx) { |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1223 | if (!discard) |
| 1224 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1225 | if (fua) |
| 1226 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1227 | if (sync) |
| 1228 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1229 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1230 | } |
| 1231 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1232 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1233 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1234 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1235 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1236 | else { |
| 1237 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1238 | sh->reconstruct_state = reconstruct_state_result; |
| 1239 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1240 | |
| 1241 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1242 | release_stripe(sh); |
| 1243 | } |
| 1244 | |
| 1245 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1246 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1247 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1248 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1249 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1250 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1251 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1252 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1253 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1254 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1255 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1256 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1257 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1258 | (unsigned long long)sh->sector); |
| 1259 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1260 | for (i = 0; i < sh->disks; i++) { |
| 1261 | if (pd_idx == i) |
| 1262 | continue; |
| 1263 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1264 | break; |
| 1265 | } |
| 1266 | if (i >= sh->disks) { |
| 1267 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1268 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 1269 | ops_complete_reconstruct(sh); |
| 1270 | return; |
| 1271 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1272 | /* check if prexor is active which means only process blocks |
| 1273 | * that are part of a read-modify-write (written) |
| 1274 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1275 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1276 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1277 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1278 | for (i = disks; i--; ) { |
| 1279 | struct r5dev *dev = &sh->dev[i]; |
| 1280 | if (dev->written) |
| 1281 | xor_srcs[count++] = dev->page; |
| 1282 | } |
| 1283 | } else { |
| 1284 | xor_dest = sh->dev[pd_idx].page; |
| 1285 | for (i = disks; i--; ) { |
| 1286 | struct r5dev *dev = &sh->dev[i]; |
| 1287 | if (i != pd_idx) |
| 1288 | xor_srcs[count++] = dev->page; |
| 1289 | } |
| 1290 | } |
| 1291 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1292 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1293 | * 2/ if we did not prexor then we are redoing the parity |
| 1294 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1295 | * for the synchronous xor case |
| 1296 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1297 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1298 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1299 | |
| 1300 | atomic_inc(&sh->count); |
| 1301 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1302 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1303 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1304 | if (unlikely(count == 1)) |
| 1305 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1306 | else |
| 1307 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1308 | } |
| 1309 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1310 | static void |
| 1311 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1312 | struct dma_async_tx_descriptor *tx) |
| 1313 | { |
| 1314 | struct async_submit_ctl submit; |
| 1315 | struct page **blocks = percpu->scribble; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1316 | int count, i; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1317 | |
| 1318 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1319 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1320 | for (i = 0; i < sh->disks; i++) { |
| 1321 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 1322 | continue; |
| 1323 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1324 | break; |
| 1325 | } |
| 1326 | if (i >= sh->disks) { |
| 1327 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1328 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 1329 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 1330 | ops_complete_reconstruct(sh); |
| 1331 | return; |
| 1332 | } |
| 1333 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1334 | count = set_syndrome_sources(blocks, sh); |
| 1335 | |
| 1336 | atomic_inc(&sh->count); |
| 1337 | |
| 1338 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1339 | sh, to_addr_conv(sh, percpu)); |
| 1340 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1341 | } |
| 1342 | |
| 1343 | static void ops_complete_check(void *stripe_head_ref) |
| 1344 | { |
| 1345 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1346 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1347 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1348 | (unsigned long long)sh->sector); |
| 1349 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1350 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1351 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1352 | release_stripe(sh); |
| 1353 | } |
| 1354 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1355 | 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] | 1356 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1357 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1358 | int pd_idx = sh->pd_idx; |
| 1359 | int qd_idx = sh->qd_idx; |
| 1360 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1361 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1362 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1363 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1364 | int count; |
| 1365 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1366 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1367 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1368 | (unsigned long long)sh->sector); |
| 1369 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1370 | count = 0; |
| 1371 | xor_dest = sh->dev[pd_idx].page; |
| 1372 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1373 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1374 | if (i == pd_idx || i == qd_idx) |
| 1375 | continue; |
| 1376 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1377 | } |
| 1378 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1379 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1380 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1381 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1382 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1383 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1384 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1385 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1386 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1387 | } |
| 1388 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1389 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1390 | { |
| 1391 | struct page **srcs = percpu->scribble; |
| 1392 | struct async_submit_ctl submit; |
| 1393 | int count; |
| 1394 | |
| 1395 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1396 | (unsigned long long)sh->sector, checkp); |
| 1397 | |
| 1398 | count = set_syndrome_sources(srcs, sh); |
| 1399 | if (!checkp) |
| 1400 | srcs[count] = NULL; |
| 1401 | |
| 1402 | atomic_inc(&sh->count); |
| 1403 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1404 | sh, to_addr_conv(sh, percpu)); |
| 1405 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1406 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1407 | } |
| 1408 | |
NeilBrown | 51acbce | 2013-02-28 09:08:34 +1100 | [diff] [blame] | 1409 | 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] | 1410 | { |
| 1411 | int overlap_clear = 0, i, disks = sh->disks; |
| 1412 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1413 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1414 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1415 | struct raid5_percpu *percpu; |
| 1416 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1417 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1418 | cpu = get_cpu(); |
| 1419 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1420 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1421 | ops_run_biofill(sh); |
| 1422 | overlap_clear++; |
| 1423 | } |
| 1424 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1425 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1426 | if (level < 6) |
| 1427 | tx = ops_run_compute5(sh, percpu); |
| 1428 | else { |
| 1429 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1430 | tx = ops_run_compute6_1(sh, percpu); |
| 1431 | else |
| 1432 | tx = ops_run_compute6_2(sh, percpu); |
| 1433 | } |
| 1434 | /* terminate the chain if reconstruct is not set to be run */ |
| 1435 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1436 | async_tx_ack(tx); |
| 1437 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1438 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1439 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1440 | tx = ops_run_prexor(sh, percpu, tx); |
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_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1443 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1444 | overlap_clear++; |
| 1445 | } |
| 1446 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1447 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1448 | if (level < 6) |
| 1449 | ops_run_reconstruct5(sh, percpu, tx); |
| 1450 | else |
| 1451 | ops_run_reconstruct6(sh, percpu, tx); |
| 1452 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1453 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1454 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1455 | if (sh->check_state == check_state_run) |
| 1456 | ops_run_check_p(sh, percpu); |
| 1457 | else if (sh->check_state == check_state_run_q) |
| 1458 | ops_run_check_pq(sh, percpu, 0); |
| 1459 | else if (sh->check_state == check_state_run_pq) |
| 1460 | ops_run_check_pq(sh, percpu, 1); |
| 1461 | else |
| 1462 | BUG(); |
| 1463 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1464 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1465 | if (overlap_clear) |
| 1466 | for (i = disks; i--; ) { |
| 1467 | struct r5dev *dev = &sh->dev[i]; |
| 1468 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1469 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1470 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1471 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1472 | } |
| 1473 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1474 | static int grow_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1475 | { |
| 1476 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1477 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1478 | if (!sh) |
| 1479 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1480 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1481 | sh->raid_conf = conf; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1482 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1483 | spin_lock_init(&sh->stripe_lock); |
| 1484 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1485 | if (grow_buffers(sh)) { |
| 1486 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1487 | kmem_cache_free(conf->slab_cache, sh); |
| 1488 | return 0; |
| 1489 | } |
| 1490 | /* we just created an active stripe so... */ |
| 1491 | atomic_set(&sh->count, 1); |
| 1492 | atomic_inc(&conf->active_stripes); |
| 1493 | INIT_LIST_HEAD(&sh->lru); |
| 1494 | release_stripe(sh); |
| 1495 | return 1; |
| 1496 | } |
| 1497 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1498 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1499 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1500 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1501 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1502 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1503 | if (conf->mddev->gendisk) |
| 1504 | sprintf(conf->cache_name[0], |
| 1505 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1506 | else |
| 1507 | sprintf(conf->cache_name[0], |
| 1508 | "raid%d-%p", conf->level, conf->mddev); |
| 1509 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1510 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1511 | conf->active_name = 0; |
| 1512 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1513 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1514 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1515 | if (!sc) |
| 1516 | return 1; |
| 1517 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1518 | conf->pool_size = devs; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1519 | while (num--) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1520 | if (!grow_one_stripe(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1521 | return 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1522 | return 0; |
| 1523 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1524 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1525 | /** |
| 1526 | * scribble_len - return the required size of the scribble region |
| 1527 | * @num - total number of disks in the array |
| 1528 | * |
| 1529 | * The size must be enough to contain: |
| 1530 | * 1/ a struct page pointer for each device in the array +2 |
| 1531 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1532 | * (dma_map_page()) or page (page_address()) address. |
| 1533 | * |
| 1534 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1535 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1536 | * of the P and Q blocks. |
| 1537 | */ |
| 1538 | static size_t scribble_len(int num) |
| 1539 | { |
| 1540 | size_t len; |
| 1541 | |
| 1542 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1543 | |
| 1544 | return len; |
| 1545 | } |
| 1546 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1547 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1548 | { |
| 1549 | /* Make all the stripes able to hold 'newsize' devices. |
| 1550 | * New slots in each stripe get 'page' set to a new page. |
| 1551 | * |
| 1552 | * This happens in stages: |
| 1553 | * 1/ create a new kmem_cache and allocate the required number of |
| 1554 | * stripe_heads. |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 1555 | * 2/ gather all the old stripe_heads and transfer the pages across |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1556 | * to the new stripe_heads. This will have the side effect of |
| 1557 | * freezing the array as once all stripe_heads have been collected, |
| 1558 | * no IO will be possible. Old stripe heads are freed once their |
| 1559 | * pages have been transferred over, and the old kmem_cache is |
| 1560 | * freed when all stripes are done. |
| 1561 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1562 | * we simple return a failre status - no need to clean anything up. |
| 1563 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1564 | * If this fails, we don't bother trying the shrink the |
| 1565 | * stripe_heads down again, we just leave them as they are. |
| 1566 | * As each stripe_head is processed the new one is released into |
| 1567 | * active service. |
| 1568 | * |
| 1569 | * Once step2 is started, we cannot afford to wait for a write, |
| 1570 | * so we use GFP_NOIO allocations. |
| 1571 | */ |
| 1572 | struct stripe_head *osh, *nsh; |
| 1573 | LIST_HEAD(newstripes); |
| 1574 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1575 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1576 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1577 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1578 | int i; |
| 1579 | |
| 1580 | if (newsize <= conf->pool_size) |
| 1581 | return 0; /* never bother to shrink */ |
| 1582 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1583 | err = md_allow_write(conf->mddev); |
| 1584 | if (err) |
| 1585 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1586 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1587 | /* Step 1 */ |
| 1588 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1589 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1590 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1591 | if (!sc) |
| 1592 | return -ENOMEM; |
| 1593 | |
| 1594 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1595 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1596 | if (!nsh) |
| 1597 | break; |
| 1598 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1599 | nsh->raid_conf = conf; |
NeilBrown | cb13ff6 | 2012-09-24 16:27:20 +1000 | [diff] [blame] | 1600 | spin_lock_init(&nsh->stripe_lock); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1601 | |
| 1602 | list_add(&nsh->lru, &newstripes); |
| 1603 | } |
| 1604 | if (i) { |
| 1605 | /* didn't get enough, give up */ |
| 1606 | while (!list_empty(&newstripes)) { |
| 1607 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1608 | list_del(&nsh->lru); |
| 1609 | kmem_cache_free(sc, nsh); |
| 1610 | } |
| 1611 | kmem_cache_destroy(sc); |
| 1612 | return -ENOMEM; |
| 1613 | } |
| 1614 | /* Step 2 - Must use GFP_NOIO now. |
| 1615 | * OK, we have enough stripes, start collecting inactive |
| 1616 | * stripes and copying them over |
| 1617 | */ |
| 1618 | list_for_each_entry(nsh, &newstripes, lru) { |
| 1619 | spin_lock_irq(&conf->device_lock); |
| 1620 | wait_event_lock_irq(conf->wait_for_stripe, |
| 1621 | !list_empty(&conf->inactive_list), |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 1622 | conf->device_lock); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1623 | osh = get_free_stripe(conf); |
| 1624 | spin_unlock_irq(&conf->device_lock); |
| 1625 | atomic_set(&nsh->count, 1); |
| 1626 | for(i=0; i<conf->pool_size; i++) |
| 1627 | nsh->dev[i].page = osh->dev[i].page; |
| 1628 | for( ; i<newsize; i++) |
| 1629 | nsh->dev[i].page = NULL; |
| 1630 | kmem_cache_free(conf->slab_cache, osh); |
| 1631 | } |
| 1632 | kmem_cache_destroy(conf->slab_cache); |
| 1633 | |
| 1634 | /* Step 3. |
| 1635 | * At this point, we are holding all the stripes so the array |
| 1636 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1637 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1638 | */ |
| 1639 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1640 | if (ndisks) { |
| 1641 | for (i=0; i<conf->raid_disks; i++) |
| 1642 | ndisks[i] = conf->disks[i]; |
| 1643 | kfree(conf->disks); |
| 1644 | conf->disks = ndisks; |
| 1645 | } else |
| 1646 | err = -ENOMEM; |
| 1647 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1648 | get_online_cpus(); |
| 1649 | conf->scribble_len = scribble_len(newsize); |
| 1650 | for_each_present_cpu(cpu) { |
| 1651 | struct raid5_percpu *percpu; |
| 1652 | void *scribble; |
| 1653 | |
| 1654 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1655 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1656 | |
| 1657 | if (scribble) { |
| 1658 | kfree(percpu->scribble); |
| 1659 | percpu->scribble = scribble; |
| 1660 | } else { |
| 1661 | err = -ENOMEM; |
| 1662 | break; |
| 1663 | } |
| 1664 | } |
| 1665 | put_online_cpus(); |
| 1666 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1667 | /* Step 4, return new stripes to service */ |
| 1668 | while(!list_empty(&newstripes)) { |
| 1669 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1670 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1671 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1672 | for (i=conf->raid_disks; i < newsize; i++) |
| 1673 | if (nsh->dev[i].page == NULL) { |
| 1674 | struct page *p = alloc_page(GFP_NOIO); |
| 1675 | nsh->dev[i].page = p; |
| 1676 | if (!p) |
| 1677 | err = -ENOMEM; |
| 1678 | } |
| 1679 | release_stripe(nsh); |
| 1680 | } |
| 1681 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1682 | |
| 1683 | conf->slab_cache = sc; |
| 1684 | conf->active_name = 1-conf->active_name; |
| 1685 | conf->pool_size = newsize; |
| 1686 | return err; |
| 1687 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1688 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1689 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1690 | { |
| 1691 | struct stripe_head *sh; |
| 1692 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1693 | spin_lock_irq(&conf->device_lock); |
| 1694 | sh = get_free_stripe(conf); |
| 1695 | spin_unlock_irq(&conf->device_lock); |
| 1696 | if (!sh) |
| 1697 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1698 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1699 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1700 | kmem_cache_free(conf->slab_cache, sh); |
| 1701 | atomic_dec(&conf->active_stripes); |
| 1702 | return 1; |
| 1703 | } |
| 1704 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1705 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1706 | { |
| 1707 | while (drop_one_stripe(conf)) |
| 1708 | ; |
| 1709 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1710 | if (conf->slab_cache) |
| 1711 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1712 | conf->slab_cache = NULL; |
| 1713 | } |
| 1714 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1715 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1716 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1717 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1718 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1719 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1720 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1721 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1722 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1723 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1724 | |
| 1725 | for (i=0 ; i<disks; i++) |
| 1726 | if (bi == &sh->dev[i].req) |
| 1727 | break; |
| 1728 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1729 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1730 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1731 | uptodate); |
| 1732 | if (i == disks) { |
| 1733 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1734 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1735 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1736 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1737 | /* If replacement finished while this request was outstanding, |
| 1738 | * 'replacement' might be NULL already. |
| 1739 | * In that case it moved down to 'rdev'. |
| 1740 | * rdev is not removed until all requests are finished. |
| 1741 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1742 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1743 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1744 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1745 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1746 | if (use_new_offset(conf, sh)) |
| 1747 | s = sh->sector + rdev->new_data_offset; |
| 1748 | else |
| 1749 | s = sh->sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1750 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1751 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1752 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1753 | /* Note that this cannot happen on a |
| 1754 | * replacement device. We just fail those on |
| 1755 | * any error |
| 1756 | */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1757 | printk_ratelimited( |
| 1758 | KERN_INFO |
| 1759 | "md/raid:%s: read error corrected" |
| 1760 | " (%lu sectors at %llu on %s)\n", |
| 1761 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1762 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1763 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1764 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1765 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1766 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1767 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 1768 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 1769 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1770 | if (atomic_read(&rdev->read_errors)) |
| 1771 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1772 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1773 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1774 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1775 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1777 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1778 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1779 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
| 1780 | printk_ratelimited( |
| 1781 | KERN_WARNING |
| 1782 | "md/raid:%s: read error on replacement device " |
| 1783 | "(sector %llu on %s).\n", |
| 1784 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1785 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1786 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1787 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 1788 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1789 | printk_ratelimited( |
| 1790 | KERN_WARNING |
| 1791 | "md/raid:%s: read error not correctable " |
| 1792 | "(sector %llu on %s).\n", |
| 1793 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1794 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1795 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1796 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1797 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1798 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1799 | printk_ratelimited( |
| 1800 | KERN_WARNING |
| 1801 | "md/raid:%s: read error NOT corrected!! " |
| 1802 | "(sector %llu on %s).\n", |
| 1803 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1804 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1805 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1806 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1807 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 1808 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1809 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1810 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1811 | else |
| 1812 | retry = 1; |
| 1813 | if (retry) |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1814 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
| 1815 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 1816 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 1817 | } else |
| 1818 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1819 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 1820 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 1821 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 1822 | if (!(set_bad |
| 1823 | && test_bit(In_sync, &rdev->flags) |
| 1824 | && rdev_set_badblocks( |
| 1825 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 1826 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 1827 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1828 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1829 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1830 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1831 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1832 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1833 | } |
| 1834 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 1835 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1836 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1837 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1838 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1839 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1840 | struct md_rdev *uninitialized_var(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1841 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 1842 | sector_t first_bad; |
| 1843 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1844 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1845 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1846 | for (i = 0 ; i < disks; i++) { |
| 1847 | if (bi == &sh->dev[i].req) { |
| 1848 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1849 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1850 | } |
| 1851 | if (bi == &sh->dev[i].rreq) { |
| 1852 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1853 | if (rdev) |
| 1854 | replacement = 1; |
| 1855 | else |
| 1856 | /* rdev was removed and 'replacement' |
| 1857 | * replaced it. rdev is not removed |
| 1858 | * until all requests are finished. |
| 1859 | */ |
| 1860 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1861 | break; |
| 1862 | } |
| 1863 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1864 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1865 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 1866 | uptodate); |
| 1867 | if (i == disks) { |
| 1868 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1869 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1872 | if (replacement) { |
| 1873 | if (!uptodate) |
| 1874 | md_error(conf->mddev, rdev); |
| 1875 | else if (is_badblock(rdev, sh->sector, |
| 1876 | STRIPE_SECTORS, |
| 1877 | &first_bad, &bad_sectors)) |
| 1878 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 1879 | } else { |
| 1880 | if (!uptodate) { |
| 1881 | set_bit(WriteErrorSeen, &rdev->flags); |
| 1882 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 1883 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 1884 | set_bit(MD_RECOVERY_NEEDED, |
| 1885 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1886 | } else if (is_badblock(rdev, sh->sector, |
| 1887 | STRIPE_SECTORS, |
| 1888 | &first_bad, &bad_sectors)) |
| 1889 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
| 1890 | } |
| 1891 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1892 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1893 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 1894 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1895 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 1896 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1897 | } |
| 1898 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1899 | 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] | 1900 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1901 | 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] | 1902 | { |
| 1903 | struct r5dev *dev = &sh->dev[i]; |
| 1904 | |
| 1905 | bio_init(&dev->req); |
| 1906 | dev->req.bi_io_vec = &dev->vec; |
| 1907 | dev->req.bi_vcnt++; |
| 1908 | dev->req.bi_max_vecs++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1909 | dev->req.bi_private = sh; |
NeilBrown | 995c427 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1910 | dev->vec.bv_page = dev->page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1911 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1912 | bio_init(&dev->rreq); |
| 1913 | dev->rreq.bi_io_vec = &dev->rvec; |
| 1914 | dev->rreq.bi_vcnt++; |
| 1915 | dev->rreq.bi_max_vecs++; |
| 1916 | dev->rreq.bi_private = sh; |
| 1917 | dev->rvec.bv_page = dev->page; |
| 1918 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1919 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 1920 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1921 | } |
| 1922 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 1923 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1924 | { |
| 1925 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1926 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1927 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 1928 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1929 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 1930 | spin_lock_irqsave(&conf->device_lock, flags); |
| 1931 | clear_bit(In_sync, &rdev->flags); |
| 1932 | mddev->degraded = calc_degraded(conf); |
| 1933 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 1934 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 1935 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 1936 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 1937 | set_bit(Faulty, &rdev->flags); |
| 1938 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 1939 | printk(KERN_ALERT |
| 1940 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 1941 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 1942 | mdname(mddev), |
| 1943 | bdevname(rdev->bdev, b), |
| 1944 | mdname(mddev), |
| 1945 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
| 1948 | /* |
| 1949 | * Input: a 'big' sector number, |
| 1950 | * Output: index of the data and parity disk, and the sector # in them. |
| 1951 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1952 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1953 | int previous, int *dd_idx, |
| 1954 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1955 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1956 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1957 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1958 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1959 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1960 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1961 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1962 | int algorithm = previous ? conf->prev_algo |
| 1963 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 1964 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 1965 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1966 | int raid_disks = previous ? conf->previous_raid_disks |
| 1967 | : conf->raid_disks; |
| 1968 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1969 | |
| 1970 | /* First compute the information on this sector */ |
| 1971 | |
| 1972 | /* |
| 1973 | * Compute the chunk number and the sector offset inside the chunk |
| 1974 | */ |
| 1975 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 1976 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | |
| 1978 | /* |
| 1979 | * Compute the stripe number |
| 1980 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 1981 | stripe = chunk_number; |
| 1982 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1983 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1984 | /* |
| 1985 | * Select the parity disk based on the user selected algorithm. |
| 1986 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 1987 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1988 | switch(conf->level) { |
| 1989 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1990 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 1991 | break; |
| 1992 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 1993 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1994 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 1995 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1996 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1997 | (*dd_idx)++; |
| 1998 | break; |
| 1999 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2000 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2001 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2002 | (*dd_idx)++; |
| 2003 | break; |
| 2004 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2005 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2006 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2007 | break; |
| 2008 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2009 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2010 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2012 | case ALGORITHM_PARITY_0: |
| 2013 | pd_idx = 0; |
| 2014 | (*dd_idx)++; |
| 2015 | break; |
| 2016 | case ALGORITHM_PARITY_N: |
| 2017 | pd_idx = data_disks; |
| 2018 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2019 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2020 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2021 | } |
| 2022 | break; |
| 2023 | case 6: |
| 2024 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2025 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2026 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2027 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2028 | qd_idx = pd_idx + 1; |
| 2029 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2030 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2031 | qd_idx = 0; |
| 2032 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2033 | (*dd_idx) += 2; /* D D P Q D */ |
| 2034 | break; |
| 2035 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2036 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2037 | qd_idx = pd_idx + 1; |
| 2038 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2039 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2040 | qd_idx = 0; |
| 2041 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2042 | (*dd_idx) += 2; /* D D P Q D */ |
| 2043 | break; |
| 2044 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2045 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2046 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2047 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2048 | break; |
| 2049 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2050 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2051 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2052 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2053 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2054 | |
| 2055 | case ALGORITHM_PARITY_0: |
| 2056 | pd_idx = 0; |
| 2057 | qd_idx = 1; |
| 2058 | (*dd_idx) += 2; |
| 2059 | break; |
| 2060 | case ALGORITHM_PARITY_N: |
| 2061 | pd_idx = data_disks; |
| 2062 | qd_idx = data_disks + 1; |
| 2063 | break; |
| 2064 | |
| 2065 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2066 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2067 | * of blocks for computing Q is different. |
| 2068 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2069 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2070 | qd_idx = pd_idx + 1; |
| 2071 | if (pd_idx == raid_disks-1) { |
| 2072 | (*dd_idx)++; /* Q D D D P */ |
| 2073 | qd_idx = 0; |
| 2074 | } else if (*dd_idx >= pd_idx) |
| 2075 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2076 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2077 | break; |
| 2078 | |
| 2079 | case ALGORITHM_ROTATING_N_RESTART: |
| 2080 | /* Same a left_asymmetric, by first stripe is |
| 2081 | * D D D P Q rather than |
| 2082 | * Q D D D P |
| 2083 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2084 | stripe2 += 1; |
| 2085 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2086 | qd_idx = pd_idx + 1; |
| 2087 | if (pd_idx == raid_disks-1) { |
| 2088 | (*dd_idx)++; /* Q D D D P */ |
| 2089 | qd_idx = 0; |
| 2090 | } else if (*dd_idx >= pd_idx) |
| 2091 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2092 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2093 | break; |
| 2094 | |
| 2095 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2096 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2097 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2098 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2099 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2100 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2101 | break; |
| 2102 | |
| 2103 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2104 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2105 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2106 | if (*dd_idx >= pd_idx) |
| 2107 | (*dd_idx)++; |
| 2108 | qd_idx = raid_disks - 1; |
| 2109 | break; |
| 2110 | |
| 2111 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2112 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2113 | if (*dd_idx >= pd_idx) |
| 2114 | (*dd_idx)++; |
| 2115 | qd_idx = raid_disks - 1; |
| 2116 | break; |
| 2117 | |
| 2118 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2119 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2120 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2121 | qd_idx = raid_disks - 1; |
| 2122 | break; |
| 2123 | |
| 2124 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2125 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2126 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2127 | qd_idx = raid_disks - 1; |
| 2128 | break; |
| 2129 | |
| 2130 | case ALGORITHM_PARITY_0_6: |
| 2131 | pd_idx = 0; |
| 2132 | (*dd_idx)++; |
| 2133 | qd_idx = raid_disks - 1; |
| 2134 | break; |
| 2135 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2136 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2137 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2138 | } |
| 2139 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2140 | } |
| 2141 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2142 | if (sh) { |
| 2143 | sh->pd_idx = pd_idx; |
| 2144 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2145 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2146 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2147 | /* |
| 2148 | * Finally, compute the new sector number |
| 2149 | */ |
| 2150 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2151 | return new_sector; |
| 2152 | } |
| 2153 | |
| 2154 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2155 | 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] | 2156 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2157 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2158 | int raid_disks = sh->disks; |
| 2159 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2160 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2161 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2162 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2163 | int algorithm = previous ? conf->prev_algo |
| 2164 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2165 | sector_t stripe; |
| 2166 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2167 | sector_t chunk_number; |
| 2168 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2170 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2171 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2172 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2173 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2174 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2175 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2176 | if (i == sh->pd_idx) |
| 2177 | return 0; |
| 2178 | switch(conf->level) { |
| 2179 | case 4: break; |
| 2180 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2181 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2182 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2183 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2184 | if (i > sh->pd_idx) |
| 2185 | i--; |
| 2186 | break; |
| 2187 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2188 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2189 | if (i < sh->pd_idx) |
| 2190 | i += raid_disks; |
| 2191 | i -= (sh->pd_idx + 1); |
| 2192 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2193 | case ALGORITHM_PARITY_0: |
| 2194 | i -= 1; |
| 2195 | break; |
| 2196 | case ALGORITHM_PARITY_N: |
| 2197 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2198 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2199 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2200 | } |
| 2201 | break; |
| 2202 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2203 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2204 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2205 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2206 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2207 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2208 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2209 | case ALGORITHM_ROTATING_N_RESTART: |
| 2210 | if (sh->pd_idx == raid_disks-1) |
| 2211 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2212 | else if (i > sh->pd_idx) |
| 2213 | i -= 2; /* D D P Q D */ |
| 2214 | break; |
| 2215 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2216 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2217 | if (sh->pd_idx == raid_disks-1) |
| 2218 | i--; /* Q D D D P */ |
| 2219 | else { |
| 2220 | /* D D P Q D */ |
| 2221 | if (i < sh->pd_idx) |
| 2222 | i += raid_disks; |
| 2223 | i -= (sh->pd_idx + 2); |
| 2224 | } |
| 2225 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2226 | case ALGORITHM_PARITY_0: |
| 2227 | i -= 2; |
| 2228 | break; |
| 2229 | case ALGORITHM_PARITY_N: |
| 2230 | break; |
| 2231 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2232 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2233 | if (sh->pd_idx == 0) |
| 2234 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2235 | else { |
| 2236 | /* D D Q P D */ |
| 2237 | if (i < sh->pd_idx) |
| 2238 | i += raid_disks; |
| 2239 | i -= (sh->pd_idx + 1); |
| 2240 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2241 | break; |
| 2242 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2243 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2244 | if (i > sh->pd_idx) |
| 2245 | i--; |
| 2246 | break; |
| 2247 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2248 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2249 | if (i < sh->pd_idx) |
| 2250 | i += data_disks + 1; |
| 2251 | i -= (sh->pd_idx + 1); |
| 2252 | break; |
| 2253 | case ALGORITHM_PARITY_0_6: |
| 2254 | i -= 1; |
| 2255 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2256 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2257 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2258 | } |
| 2259 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2260 | } |
| 2261 | |
| 2262 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2263 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2264 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2265 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2266 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2267 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2268 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2269 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2270 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2271 | return 0; |
| 2272 | } |
| 2273 | return r_sector; |
| 2274 | } |
| 2275 | |
| 2276 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2277 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2278 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2279 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2280 | { |
| 2281 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2282 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2283 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2284 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2285 | if (rcw) { |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2286 | |
| 2287 | for (i = disks; i--; ) { |
| 2288 | struct r5dev *dev = &sh->dev[i]; |
| 2289 | |
| 2290 | if (dev->towrite) { |
| 2291 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2292 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2293 | if (!expand) |
| 2294 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2295 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2296 | } |
| 2297 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 2298 | /* if we are not expanding this is a proper write request, and |
| 2299 | * there will be bios with new data to be drained into the |
| 2300 | * stripe cache |
| 2301 | */ |
| 2302 | if (!expand) { |
| 2303 | if (!s->locked) |
| 2304 | /* False alarm, nothing to do */ |
| 2305 | return; |
| 2306 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2307 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2308 | } else |
| 2309 | sh->reconstruct_state = reconstruct_state_run; |
| 2310 | |
| 2311 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
| 2312 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2313 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2314 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2315 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2316 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2317 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2318 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2319 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2320 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2321 | for (i = disks; i--; ) { |
| 2322 | struct r5dev *dev = &sh->dev[i]; |
| 2323 | if (i == pd_idx) |
| 2324 | continue; |
| 2325 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2326 | if (dev->towrite && |
| 2327 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2328 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2329 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2330 | set_bit(R5_LOCKED, &dev->flags); |
| 2331 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2332 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2333 | } |
| 2334 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 2335 | if (!s->locked) |
| 2336 | /* False alarm - nothing to do */ |
| 2337 | return; |
| 2338 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
| 2339 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2340 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2341 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2342 | } |
| 2343 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2344 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2345 | * are in flight |
| 2346 | */ |
| 2347 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2348 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2349 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2350 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2351 | if (level == 6) { |
| 2352 | int qd_idx = sh->qd_idx; |
| 2353 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2354 | |
| 2355 | set_bit(R5_LOCKED, &dev->flags); |
| 2356 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2357 | s->locked++; |
| 2358 | } |
| 2359 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2360 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2361 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2362 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2363 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2364 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2365 | /* |
| 2366 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2367 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2368 | * The bi_next chain must be in order. |
| 2369 | */ |
| 2370 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2371 | { |
| 2372 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2373 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2374 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2375 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2376 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2377 | (unsigned long long)bi->bi_sector, |
| 2378 | (unsigned long long)sh->sector); |
| 2379 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2380 | /* |
| 2381 | * If several bio share a stripe. The bio bi_phys_segments acts as a |
| 2382 | * reference count to avoid race. The reference count should already be |
| 2383 | * increased before this function is called (for example, in |
| 2384 | * make_request()), so other bio sharing this stripe will not free the |
| 2385 | * stripe. If a stripe is owned by one stripe, the stripe lock will |
| 2386 | * protect it. |
| 2387 | */ |
| 2388 | spin_lock_irq(&sh->stripe_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2389 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2390 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2391 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2392 | firstwrite = 1; |
| 2393 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2394 | bip = &sh->dev[dd_idx].toread; |
| 2395 | while (*bip && (*bip)->bi_sector < bi->bi_sector) { |
| 2396 | if ((*bip)->bi_sector + ((*bip)->bi_size >> 9) > bi->bi_sector) |
| 2397 | goto overlap; |
| 2398 | bip = & (*bip)->bi_next; |
| 2399 | } |
| 2400 | if (*bip && (*bip)->bi_sector < bi->bi_sector + ((bi->bi_size)>>9)) |
| 2401 | goto overlap; |
| 2402 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2403 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2404 | if (*bip) |
| 2405 | bi->bi_next = *bip; |
| 2406 | *bip = bi; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2407 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2408 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2409 | if (forwrite) { |
| 2410 | /* check if page is covered */ |
| 2411 | sector_t sector = sh->dev[dd_idx].sector; |
| 2412 | for (bi=sh->dev[dd_idx].towrite; |
| 2413 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
| 2414 | bi && bi->bi_sector <= sector; |
| 2415 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
| 2416 | if (bi->bi_sector + (bi->bi_size>>9) >= sector) |
| 2417 | sector = bi->bi_sector + (bi->bi_size>>9); |
| 2418 | } |
| 2419 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2420 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2421 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2422 | |
| 2423 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
| 2424 | (unsigned long long)(*bip)->bi_sector, |
| 2425 | (unsigned long long)sh->sector, dd_idx); |
NeilBrown | b97390a | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2426 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2427 | |
| 2428 | if (conf->mddev->bitmap && firstwrite) { |
| 2429 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2430 | STRIPE_SECTORS, 0); |
| 2431 | sh->bm_seq = conf->seq_flush+1; |
| 2432 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2433 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2434 | return 1; |
| 2435 | |
| 2436 | overlap: |
| 2437 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2438 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | return 0; |
| 2440 | } |
| 2441 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2442 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2443 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2444 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2445 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2446 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2447 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2448 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2449 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2450 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2451 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2452 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2453 | raid5_compute_sector(conf, |
| 2454 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2455 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2456 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2457 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2458 | } |
| 2459 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2460 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2461 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2462 | struct stripe_head_state *s, int disks, |
| 2463 | struct bio **return_bi) |
| 2464 | { |
| 2465 | int i; |
| 2466 | for (i = disks; i--; ) { |
| 2467 | struct bio *bi; |
| 2468 | int bitmap_end = 0; |
| 2469 | |
| 2470 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2471 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2472 | rcu_read_lock(); |
| 2473 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2474 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2475 | atomic_inc(&rdev->nr_pending); |
| 2476 | else |
| 2477 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2478 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2479 | if (rdev) { |
| 2480 | if (!rdev_set_badblocks( |
| 2481 | rdev, |
| 2482 | sh->sector, |
| 2483 | STRIPE_SECTORS, 0)) |
| 2484 | md_error(conf->mddev, rdev); |
| 2485 | rdev_dec_pending(rdev, conf->mddev); |
| 2486 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2487 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2488 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2489 | /* fail all writes first */ |
| 2490 | bi = sh->dev[i].towrite; |
| 2491 | sh->dev[i].towrite = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2492 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 1ed850f | 2012-10-11 13:50:13 +1100 | [diff] [blame] | 2493 | if (bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2494 | bitmap_end = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2495 | |
| 2496 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2497 | wake_up(&conf->wait_for_overlap); |
| 2498 | |
| 2499 | while (bi && bi->bi_sector < |
| 2500 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2501 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2502 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2503 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2504 | md_write_end(conf->mddev); |
| 2505 | bi->bi_next = *return_bi; |
| 2506 | *return_bi = bi; |
| 2507 | } |
| 2508 | bi = nextbi; |
| 2509 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2510 | if (bitmap_end) |
| 2511 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2512 | STRIPE_SECTORS, 0, 0); |
| 2513 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2514 | /* and fail all 'written' */ |
| 2515 | bi = sh->dev[i].written; |
| 2516 | sh->dev[i].written = NULL; |
| 2517 | if (bi) bitmap_end = 1; |
| 2518 | while (bi && bi->bi_sector < |
| 2519 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2520 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2521 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2522 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2523 | md_write_end(conf->mddev); |
| 2524 | bi->bi_next = *return_bi; |
| 2525 | *return_bi = bi; |
| 2526 | } |
| 2527 | bi = bi2; |
| 2528 | } |
| 2529 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2530 | /* fail any reads if this device is non-operational and |
| 2531 | * the data has not reached the cache yet. |
| 2532 | */ |
| 2533 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2534 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2535 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2536 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2537 | bi = sh->dev[i].toread; |
| 2538 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2539 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2540 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2541 | wake_up(&conf->wait_for_overlap); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2542 | while (bi && bi->bi_sector < |
| 2543 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2544 | struct bio *nextbi = |
| 2545 | r5_next_bio(bi, sh->dev[i].sector); |
| 2546 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2547 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2548 | bi->bi_next = *return_bi; |
| 2549 | *return_bi = bi; |
| 2550 | } |
| 2551 | bi = nextbi; |
| 2552 | } |
| 2553 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2554 | if (bitmap_end) |
| 2555 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2556 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2557 | /* If we were in the middle of a write the parity block might |
| 2558 | * still be locked - so just clear all R5_LOCKED flags |
| 2559 | */ |
| 2560 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2561 | } |
| 2562 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2563 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2564 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2565 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2566 | } |
| 2567 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2568 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2569 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2570 | struct stripe_head_state *s) |
| 2571 | { |
| 2572 | int abort = 0; |
| 2573 | int i; |
| 2574 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2575 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 2576 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 2577 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2578 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2579 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2580 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2581 | * Don't even need to abort as that is handled elsewhere |
| 2582 | * if needed, and not always wanted e.g. if there is a known |
| 2583 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2584 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2585 | * non-sync devices, or abort the recovery |
| 2586 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2587 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 2588 | /* During recovery devices cannot be removed, so |
| 2589 | * locking and refcounting of rdevs is not needed |
| 2590 | */ |
| 2591 | for (i = 0; i < conf->raid_disks; i++) { |
| 2592 | struct md_rdev *rdev = conf->disks[i].rdev; |
| 2593 | if (rdev |
| 2594 | && !test_bit(Faulty, &rdev->flags) |
| 2595 | && !test_bit(In_sync, &rdev->flags) |
| 2596 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2597 | STRIPE_SECTORS, 0)) |
| 2598 | abort = 1; |
| 2599 | rdev = conf->disks[i].replacement; |
| 2600 | if (rdev |
| 2601 | && !test_bit(Faulty, &rdev->flags) |
| 2602 | && !test_bit(In_sync, &rdev->flags) |
| 2603 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2604 | STRIPE_SECTORS, 0)) |
| 2605 | abort = 1; |
| 2606 | } |
| 2607 | if (abort) |
| 2608 | conf->recovery_disabled = |
| 2609 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2610 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2611 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2612 | } |
| 2613 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2614 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 2615 | { |
| 2616 | struct md_rdev *rdev; |
| 2617 | int rv = 0; |
| 2618 | /* Doing recovery so rcu locking not required */ |
| 2619 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
| 2620 | if (rdev |
| 2621 | && !test_bit(Faulty, &rdev->flags) |
| 2622 | && !test_bit(In_sync, &rdev->flags) |
| 2623 | && (rdev->recovery_offset <= sh->sector |
| 2624 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 2625 | rv = 1; |
| 2626 | |
| 2627 | return rv; |
| 2628 | } |
| 2629 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2630 | /* 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] | 2631 | * to be read or computed to satisfy a request. |
| 2632 | * |
| 2633 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2634 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2635 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2636 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2637 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2638 | { |
| 2639 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2640 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2641 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2642 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2643 | /* is the data in this block needed, and can we get it? */ |
| 2644 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2645 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2646 | (dev->toread || |
| 2647 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2648 | s->syncing || s->expanding || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2649 | (s->replacing && want_replace(sh, disk_idx)) || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2650 | (s->failed >= 1 && fdev[0]->toread) || |
| 2651 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2652 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
| 2653 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
| 2654 | (sh->raid_conf->level == 6 && s->failed && s->to_write))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2655 | /* we would like to get this block, possibly by computing it, |
| 2656 | * otherwise read it if the backing disk is insync |
| 2657 | */ |
| 2658 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2659 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2660 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2661 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2662 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2663 | /* have disk failed, and we're requested to fetch it; |
| 2664 | * do compute it |
| 2665 | */ |
| 2666 | pr_debug("Computing stripe %llu block %d\n", |
| 2667 | (unsigned long long)sh->sector, disk_idx); |
| 2668 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2669 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2670 | set_bit(R5_Wantcompute, &dev->flags); |
| 2671 | sh->ops.target = disk_idx; |
| 2672 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2673 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2674 | /* Careful: from this point on 'uptodate' is in the eye |
| 2675 | * of raid_run_ops which services 'compute' operations |
| 2676 | * before writes. R5_Wantcompute flags a block that will |
| 2677 | * be R5_UPTODATE by the time it is needed for a |
| 2678 | * subsequent operation. |
| 2679 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2680 | s->uptodate++; |
| 2681 | return 1; |
| 2682 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2683 | /* Computing 2-failure is *very* expensive; only |
| 2684 | * do it if failed >= 2 |
| 2685 | */ |
| 2686 | int other; |
| 2687 | for (other = disks; other--; ) { |
| 2688 | if (other == disk_idx) |
| 2689 | continue; |
| 2690 | if (!test_bit(R5_UPTODATE, |
| 2691 | &sh->dev[other].flags)) |
| 2692 | break; |
| 2693 | } |
| 2694 | BUG_ON(other < 0); |
| 2695 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2696 | (unsigned long long)sh->sector, |
| 2697 | disk_idx, other); |
| 2698 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2699 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2700 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2701 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2702 | sh->ops.target = disk_idx; |
| 2703 | sh->ops.target2 = other; |
| 2704 | s->uptodate += 2; |
| 2705 | s->req_compute = 1; |
| 2706 | return 1; |
| 2707 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2708 | set_bit(R5_LOCKED, &dev->flags); |
| 2709 | set_bit(R5_Wantread, &dev->flags); |
| 2710 | s->locked++; |
| 2711 | pr_debug("Reading block %d (sync=%d)\n", |
| 2712 | disk_idx, s->syncing); |
| 2713 | } |
| 2714 | } |
| 2715 | |
| 2716 | return 0; |
| 2717 | } |
| 2718 | |
| 2719 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2720 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2721 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2722 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2723 | struct stripe_head_state *s, |
| 2724 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2725 | { |
| 2726 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2727 | |
| 2728 | /* look for blocks to read/compute, skip this if a compute |
| 2729 | * is already in flight, or if the stripe contents are in the |
| 2730 | * midst of changing due to a write |
| 2731 | */ |
| 2732 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 2733 | !sh->reconstruct_state) |
| 2734 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2735 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2736 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2737 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2738 | } |
| 2739 | |
| 2740 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2741 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2742 | * any written block on an uptodate or failed drive can be returned. |
| 2743 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 2744 | * never LOCKED, so we don't need to test 'failed' directly. |
| 2745 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2746 | static void handle_stripe_clean_event(struct r5conf *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2747 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 2748 | { |
| 2749 | int i; |
| 2750 | struct r5dev *dev; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 2751 | int discard_pending = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 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); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 2780 | } else if (test_bit(R5_Discard, &dev->flags)) |
| 2781 | discard_pending = 1; |
| 2782 | } |
| 2783 | if (!discard_pending && |
| 2784 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { |
| 2785 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 2786 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
| 2787 | if (sh->qd_idx >= 0) { |
| 2788 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 2789 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); |
| 2790 | } |
| 2791 | /* now that discard is done we can proceed with any sync */ |
| 2792 | clear_bit(STRIPE_DISCARD, &sh->state); |
| 2793 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
| 2794 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2795 | |
| 2796 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2797 | |
| 2798 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2799 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2800 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2801 | } |
| 2802 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2803 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2804 | struct stripe_head *sh, |
| 2805 | struct stripe_head_state *s, |
| 2806 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2807 | { |
| 2808 | int rmw = 0, rcw = 0, i; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2809 | sector_t recovery_cp = conf->mddev->recovery_cp; |
| 2810 | |
| 2811 | /* RAID6 requires 'rcw' in current implementation. |
| 2812 | * Otherwise, check whether resync is now happening or should start. |
| 2813 | * If yes, then the array is dirty (after unclean shutdown or |
| 2814 | * initial creation), so parity in some stripes might be inconsistent. |
| 2815 | * In this case, we need to always do reconstruct-write, to ensure |
| 2816 | * that in case of drive failure or read-error correction, we |
| 2817 | * generate correct data from the parity. |
| 2818 | */ |
| 2819 | if (conf->max_degraded == 2 || |
| 2820 | (recovery_cp < MaxSector && sh->sector >= recovery_cp)) { |
| 2821 | /* Calculate the real rcw later - for now make it |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2822 | * look like rcw is cheaper |
| 2823 | */ |
| 2824 | rcw = 1; rmw = 2; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2825 | pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n", |
| 2826 | conf->max_degraded, (unsigned long long)recovery_cp, |
| 2827 | (unsigned long long)sh->sector); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2828 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2829 | /* would I have to read this buffer for read_modify_write */ |
| 2830 | struct r5dev *dev = &sh->dev[i]; |
| 2831 | if ((dev->towrite || i == sh->pd_idx) && |
| 2832 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2833 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2834 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2835 | if (test_bit(R5_Insync, &dev->flags)) |
| 2836 | rmw++; |
| 2837 | else |
| 2838 | rmw += 2*disks; /* cannot read it */ |
| 2839 | } |
| 2840 | /* Would I have to read this buffer for reconstruct_write */ |
| 2841 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 2842 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2843 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2844 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2845 | if (test_bit(R5_Insync, &dev->flags)) rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2846 | else |
| 2847 | rcw += 2*disks; |
| 2848 | } |
| 2849 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2850 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2851 | (unsigned long long)sh->sector, rmw, rcw); |
| 2852 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2853 | if (rmw < rcw && rmw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2854 | /* prefer read-modify-write, but need to get some data */ |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 2855 | if (conf->mddev->queue) |
| 2856 | blk_add_trace_msg(conf->mddev->queue, |
| 2857 | "raid5 rmw %llu %d", |
| 2858 | (unsigned long long)sh->sector, rmw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2859 | for (i = disks; i--; ) { |
| 2860 | struct r5dev *dev = &sh->dev[i]; |
| 2861 | if ((dev->towrite || i == sh->pd_idx) && |
| 2862 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2863 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 2864 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2865 | test_bit(R5_Insync, &dev->flags)) { |
| 2866 | if ( |
| 2867 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2868 | pr_debug("Read_old block " |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2869 | "%d for r-m-w\n", i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2870 | set_bit(R5_LOCKED, &dev->flags); |
| 2871 | set_bit(R5_Wantread, &dev->flags); |
| 2872 | s->locked++; |
| 2873 | } else { |
| 2874 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2875 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2876 | } |
| 2877 | } |
| 2878 | } |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2879 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2880 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2881 | /* want reconstruct write, but need to get some data */ |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2882 | int qread =0; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2883 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2884 | for (i = disks; i--; ) { |
| 2885 | struct r5dev *dev = &sh->dev[i]; |
| 2886 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2887 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2888 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2889 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2890 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2891 | rcw++; |
| 2892 | if (!test_bit(R5_Insync, &dev->flags)) |
| 2893 | continue; /* it's a failed drive */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2894 | if ( |
| 2895 | test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2896 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2897 | "%d for Reconstruct\n", i); |
| 2898 | set_bit(R5_LOCKED, &dev->flags); |
| 2899 | set_bit(R5_Wantread, &dev->flags); |
| 2900 | s->locked++; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2901 | qread++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2902 | } else { |
| 2903 | set_bit(STRIPE_DELAYED, &sh->state); |
| 2904 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2905 | } |
| 2906 | } |
| 2907 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 2908 | if (rcw && conf->mddev->queue) |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 2909 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
| 2910 | (unsigned long long)sh->sector, |
| 2911 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2912 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2913 | /* now if nothing is locked, and if we have enough data, |
| 2914 | * we can start a write request |
| 2915 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2916 | /* since handle_stripe can be called at any time we need to handle the |
| 2917 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2918 | * subsequent call wants to start a write request. raid_run_ops only |
| 2919 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2920 | * simultaneously. If this is not the case then new writes need to be |
| 2921 | * held off until the compute completes. |
| 2922 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2923 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 2924 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 2925 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2926 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2927 | } |
| 2928 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2929 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2930 | struct stripe_head_state *s, int disks) |
| 2931 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2932 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2933 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2934 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2935 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2936 | switch (sh->check_state) { |
| 2937 | case check_state_idle: |
| 2938 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2939 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2940 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2941 | sh->check_state = check_state_run; |
| 2942 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2943 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2944 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2945 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 2946 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2947 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2948 | /* fall through */ |
| 2949 | case check_state_compute_result: |
| 2950 | sh->check_state = check_state_idle; |
| 2951 | if (!dev) |
| 2952 | dev = &sh->dev[sh->pd_idx]; |
| 2953 | |
| 2954 | /* check that a write has not made the stripe insync */ |
| 2955 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 2956 | break; |
| 2957 | |
| 2958 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2959 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 2960 | BUG_ON(s->uptodate != disks); |
| 2961 | |
| 2962 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2963 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2964 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2965 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2966 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2967 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2968 | break; |
| 2969 | case check_state_run: |
| 2970 | break; /* we will be called again upon completion */ |
| 2971 | case check_state_check_result: |
| 2972 | sh->check_state = check_state_idle; |
| 2973 | |
| 2974 | /* if a failure occurred during the check operation, leave |
| 2975 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 2976 | */ |
| 2977 | if (s->failed) |
| 2978 | break; |
| 2979 | |
| 2980 | /* handle a successful check operation, if parity is correct |
| 2981 | * we are done. Otherwise update the mismatch count and repair |
| 2982 | * parity if !MD_RECOVERY_CHECK |
| 2983 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 2984 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2985 | /* parity is correct (on disc, |
| 2986 | * not in buffer any more) |
| 2987 | */ |
| 2988 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2989 | else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 2990 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2991 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 2992 | /* don't try to repair!! */ |
| 2993 | set_bit(STRIPE_INSYNC, &sh->state); |
| 2994 | else { |
| 2995 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 2996 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2997 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2998 | set_bit(R5_Wantcompute, |
| 2999 | &sh->dev[sh->pd_idx].flags); |
| 3000 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3001 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3002 | s->uptodate++; |
| 3003 | } |
| 3004 | } |
| 3005 | break; |
| 3006 | case check_state_compute_run: |
| 3007 | break; |
| 3008 | default: |
| 3009 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3010 | __func__, sh->check_state, |
| 3011 | (unsigned long long) sh->sector); |
| 3012 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3013 | } |
| 3014 | } |
| 3015 | |
| 3016 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3017 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3018 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3019 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3020 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3021 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3022 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3023 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3024 | |
| 3025 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3026 | |
| 3027 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3028 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3029 | /* Want to check and possibly repair P and Q. |
| 3030 | * However there could be one 'failed' device, in which |
| 3031 | * case we can only check one of them, possibly using the |
| 3032 | * other to generate missing data |
| 3033 | */ |
| 3034 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3035 | switch (sh->check_state) { |
| 3036 | case check_state_idle: |
| 3037 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3038 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3039 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3040 | * makes sense to check P (If anything else were failed, |
| 3041 | * we would have used P to recreate it). |
| 3042 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3043 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3044 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3045 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3046 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3047 | * anything, so it makes sense to check it |
| 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 | else |
| 3052 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3053 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3054 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3055 | /* discard potentially stale zero_sum_result */ |
| 3056 | sh->ops.zero_sum_result = 0; |
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 | if (sh->check_state == check_state_run) { |
| 3059 | /* async_xor_zero_sum destroys the contents of P */ |
| 3060 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 3061 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3062 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3063 | if (sh->check_state >= check_state_run && |
| 3064 | sh->check_state <= check_state_run_pq) { |
| 3065 | /* async_syndrome_zero_sum preserves P and Q, so |
| 3066 | * no need to mark them !uptodate here |
| 3067 | */ |
| 3068 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 3069 | break; |
| 3070 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3071 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3072 | /* we have 2-disk failure */ |
| 3073 | BUG_ON(s->failed != 2); |
| 3074 | /* fall through */ |
| 3075 | case check_state_compute_result: |
| 3076 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3077 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3078 | /* check that a write has not made the stripe insync */ |
| 3079 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3080 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3081 | |
| 3082 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3083 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3084 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3085 | 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] | 3086 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3087 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3088 | s->locked++; |
| 3089 | set_bit(R5_LOCKED, &dev->flags); |
| 3090 | set_bit(R5_Wantwrite, &dev->flags); |
| 3091 | } |
| 3092 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3093 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3094 | s->locked++; |
| 3095 | set_bit(R5_LOCKED, &dev->flags); |
| 3096 | set_bit(R5_Wantwrite, &dev->flags); |
| 3097 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3098 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3099 | dev = &sh->dev[pd_idx]; |
| 3100 | s->locked++; |
| 3101 | set_bit(R5_LOCKED, &dev->flags); |
| 3102 | set_bit(R5_Wantwrite, &dev->flags); |
| 3103 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3104 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3105 | dev = &sh->dev[qd_idx]; |
| 3106 | s->locked++; |
| 3107 | set_bit(R5_LOCKED, &dev->flags); |
| 3108 | set_bit(R5_Wantwrite, &dev->flags); |
| 3109 | } |
| 3110 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3111 | |
| 3112 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3113 | break; |
| 3114 | case check_state_run: |
| 3115 | case check_state_run_q: |
| 3116 | case check_state_run_pq: |
| 3117 | break; /* we will be called again upon completion */ |
| 3118 | case check_state_check_result: |
| 3119 | sh->check_state = check_state_idle; |
| 3120 | |
| 3121 | /* handle a successful check operation, if parity is correct |
| 3122 | * we are done. Otherwise update the mismatch count and repair |
| 3123 | * parity if !MD_RECOVERY_CHECK |
| 3124 | */ |
| 3125 | if (sh->ops.zero_sum_result == 0) { |
| 3126 | /* both parities are correct */ |
| 3127 | if (!s->failed) |
| 3128 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3129 | else { |
| 3130 | /* in contrast to the raid5 case we can validate |
| 3131 | * parity, but still have a failure to write |
| 3132 | * back |
| 3133 | */ |
| 3134 | sh->check_state = check_state_compute_result; |
| 3135 | /* Returning at this point means that we may go |
| 3136 | * off and bring p and/or q uptodate again so |
| 3137 | * we make sure to check zero_sum_result again |
| 3138 | * to verify if p or q need writeback |
| 3139 | */ |
| 3140 | } |
| 3141 | } else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 3142 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3143 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3144 | /* don't try to repair!! */ |
| 3145 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3146 | else { |
| 3147 | int *target = &sh->ops.target; |
| 3148 | |
| 3149 | sh->ops.target = -1; |
| 3150 | sh->ops.target2 = -1; |
| 3151 | sh->check_state = check_state_compute_run; |
| 3152 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3153 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3154 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3155 | set_bit(R5_Wantcompute, |
| 3156 | &sh->dev[pd_idx].flags); |
| 3157 | *target = pd_idx; |
| 3158 | target = &sh->ops.target2; |
| 3159 | s->uptodate++; |
| 3160 | } |
| 3161 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3162 | set_bit(R5_Wantcompute, |
| 3163 | &sh->dev[qd_idx].flags); |
| 3164 | *target = qd_idx; |
| 3165 | s->uptodate++; |
| 3166 | } |
| 3167 | } |
| 3168 | } |
| 3169 | break; |
| 3170 | case check_state_compute_run: |
| 3171 | break; |
| 3172 | default: |
| 3173 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3174 | __func__, sh->check_state, |
| 3175 | (unsigned long long) sh->sector); |
| 3176 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3177 | } |
| 3178 | } |
| 3179 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3180 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3181 | { |
| 3182 | int i; |
| 3183 | |
| 3184 | /* We have read all the blocks in this stripe and now we need to |
| 3185 | * copy some of them into a target stripe for expand. |
| 3186 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3187 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3188 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3189 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3190 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3191 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3192 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3193 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3194 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3195 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3196 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3197 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3198 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3199 | if (sh2 == NULL) |
| 3200 | /* so far only the early blocks of this stripe |
| 3201 | * have been requested. When later blocks |
| 3202 | * get requested, we will try again |
| 3203 | */ |
| 3204 | continue; |
| 3205 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3206 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3207 | /* must have already done this block */ |
| 3208 | release_stripe(sh2); |
| 3209 | continue; |
| 3210 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3211 | |
| 3212 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3213 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3214 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3215 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3216 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3217 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3218 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3219 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3220 | for (j = 0; j < conf->raid_disks; j++) |
| 3221 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3222 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3223 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3224 | break; |
| 3225 | if (j == conf->raid_disks) { |
| 3226 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3227 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3228 | } |
| 3229 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3230 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3231 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3232 | /* done submitting copies, wait for them to complete */ |
NeilBrown | 749586b | 2012-11-20 14:11:15 +1100 | [diff] [blame] | 3233 | async_tx_quiesce(&tx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3234 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3235 | |
| 3236 | /* |
| 3237 | * handle_stripe - do things to a stripe. |
| 3238 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3239 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 3240 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3241 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3242 | * return some read requests which now have data |
| 3243 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3244 | * schedule a read on some buffers |
| 3245 | * schedule a write of some buffers |
| 3246 | * return confirmation of parity correctness |
| 3247 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3248 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3249 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3250 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3251 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3252 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3253 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3254 | struct r5dev *dev; |
| 3255 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3256 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3257 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3258 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3259 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3260 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3261 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3262 | s->failed_num[0] = -1; |
| 3263 | s->failed_num[1] = -1; |
| 3264 | |
| 3265 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3266 | rcu_read_lock(); |
| 3267 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3268 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3269 | sector_t first_bad; |
| 3270 | int bad_sectors; |
| 3271 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3272 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3273 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3274 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3275 | 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] | 3276 | i, dev->flags, |
| 3277 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3278 | /* maybe we can reply to a read |
| 3279 | * |
| 3280 | * new wantfill requests are only permitted while |
| 3281 | * ops_complete_biofill is guaranteed to be inactive |
| 3282 | */ |
| 3283 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3284 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3285 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3286 | |
| 3287 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3288 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3289 | s->locked++; |
| 3290 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3291 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3292 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3293 | s->compute++; |
| 3294 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3295 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3296 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3297 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3298 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3299 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3300 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3301 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3302 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3303 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3304 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3305 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3306 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3307 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3308 | /* Prefer to use the replacement for reads, but only |
| 3309 | * if it is recovered enough and has no bad blocks. |
| 3310 | */ |
| 3311 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 3312 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 3313 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 3314 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3315 | &first_bad, &bad_sectors)) |
| 3316 | set_bit(R5_ReadRepl, &dev->flags); |
| 3317 | else { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3318 | if (rdev) |
| 3319 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3320 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 3321 | clear_bit(R5_ReadRepl, &dev->flags); |
| 3322 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 3323 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 3324 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3325 | if (rdev) { |
| 3326 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3327 | &first_bad, &bad_sectors); |
| 3328 | if (s->blocked_rdev == NULL |
| 3329 | && (test_bit(Blocked, &rdev->flags) |
| 3330 | || is_bad < 0)) { |
| 3331 | if (is_bad < 0) |
| 3332 | set_bit(BlockedBadBlocks, |
| 3333 | &rdev->flags); |
| 3334 | s->blocked_rdev = rdev; |
| 3335 | atomic_inc(&rdev->nr_pending); |
| 3336 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3337 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3338 | clear_bit(R5_Insync, &dev->flags); |
| 3339 | if (!rdev) |
| 3340 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3341 | else if (is_bad) { |
| 3342 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3343 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 3344 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3345 | /* treat as in-sync, but with a read error |
| 3346 | * which we can now try to correct |
| 3347 | */ |
| 3348 | set_bit(R5_Insync, &dev->flags); |
| 3349 | set_bit(R5_ReadError, &dev->flags); |
| 3350 | } |
| 3351 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3352 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3353 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3354 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3355 | set_bit(R5_Insync, &dev->flags); |
| 3356 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 3357 | test_bit(R5_Expanded, &dev->flags)) |
| 3358 | /* If we've reshaped into here, we assume it is Insync. |
| 3359 | * We will shortly update recovery_offset to make |
| 3360 | * it official. |
| 3361 | */ |
| 3362 | set_bit(R5_Insync, &dev->flags); |
| 3363 | |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3364 | if (rdev && test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3365 | /* This flag does not apply to '.replacement' |
| 3366 | * only to .rdev, so make sure to check that*/ |
| 3367 | struct md_rdev *rdev2 = rcu_dereference( |
| 3368 | conf->disks[i].rdev); |
| 3369 | if (rdev2 == rdev) |
| 3370 | clear_bit(R5_Insync, &dev->flags); |
| 3371 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3372 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3373 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3374 | } else |
| 3375 | clear_bit(R5_WriteError, &dev->flags); |
| 3376 | } |
Adam Kwolek | 5d8c71f | 2011-12-09 14:26:11 +1100 | [diff] [blame] | 3377 | if (rdev && test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3378 | /* This flag does not apply to '.replacement' |
| 3379 | * only to .rdev, so make sure to check that*/ |
| 3380 | struct md_rdev *rdev2 = rcu_dereference( |
| 3381 | conf->disks[i].rdev); |
| 3382 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3383 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3384 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3385 | } else |
| 3386 | clear_bit(R5_MadeGood, &dev->flags); |
| 3387 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3388 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3389 | struct md_rdev *rdev2 = rcu_dereference( |
| 3390 | conf->disks[i].replacement); |
| 3391 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 3392 | s->handle_bad_blocks = 1; |
| 3393 | atomic_inc(&rdev2->nr_pending); |
| 3394 | } else |
| 3395 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 3396 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3397 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3398 | /* The ReadError flag will just be confusing now */ |
| 3399 | clear_bit(R5_ReadError, &dev->flags); |
| 3400 | clear_bit(R5_ReWrite, &dev->flags); |
| 3401 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3402 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3403 | clear_bit(R5_Insync, &dev->flags); |
| 3404 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3405 | if (s->failed < 2) |
| 3406 | s->failed_num[s->failed] = i; |
| 3407 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3408 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 3409 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3410 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3411 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3412 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 3413 | /* If there is a failed device being replaced, |
| 3414 | * we must be recovering. |
| 3415 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3416 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3417 | * else we can only be replacing |
| 3418 | * sync and recovery both need to read all devices, and so |
| 3419 | * use the same flag. |
| 3420 | */ |
| 3421 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3422 | sh->sector >= conf->mddev->recovery_cp || |
| 3423 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3424 | s->syncing = 1; |
| 3425 | else |
| 3426 | s->replacing = 1; |
| 3427 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3428 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3429 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3430 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3431 | static void handle_stripe(struct stripe_head *sh) |
| 3432 | { |
| 3433 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3434 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3435 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3436 | int prexor; |
| 3437 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3438 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3439 | |
| 3440 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3441 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3442 | /* already being handled, ensure it gets handled |
| 3443 | * again when current action finishes */ |
| 3444 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3445 | return; |
| 3446 | } |
| 3447 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3448 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3449 | spin_lock(&sh->stripe_lock); |
| 3450 | /* Cannot process 'sync' concurrently with 'discard' */ |
| 3451 | if (!test_bit(STRIPE_DISCARD, &sh->state) && |
| 3452 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3453 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3454 | clear_bit(STRIPE_INSYNC, &sh->state); |
| 3455 | } |
| 3456 | spin_unlock(&sh->stripe_lock); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3457 | } |
| 3458 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3459 | |
| 3460 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3461 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3462 | (unsigned long long)sh->sector, sh->state, |
| 3463 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3464 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3465 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3466 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3467 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3468 | if (s.handle_bad_blocks) { |
| 3469 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3470 | goto finish; |
| 3471 | } |
| 3472 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3473 | if (unlikely(s.blocked_rdev)) { |
| 3474 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3475 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3476 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3477 | goto finish; |
| 3478 | } |
| 3479 | /* There is nothing for the blocked_rdev to block */ |
| 3480 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3481 | s.blocked_rdev = NULL; |
| 3482 | } |
| 3483 | |
| 3484 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3485 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3486 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3487 | } |
| 3488 | |
| 3489 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3490 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3491 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3492 | s.failed_num[0], s.failed_num[1]); |
| 3493 | /* check if the array has lost more than max_degraded devices and, |
| 3494 | * if so, some requests might need to be failed. |
| 3495 | */ |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3496 | if (s.failed > conf->max_degraded) { |
| 3497 | sh->check_state = 0; |
| 3498 | sh->reconstruct_state = 0; |
| 3499 | if (s.to_read+s.to_write+s.written) |
| 3500 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3501 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3502 | handle_failed_sync(conf, sh, &s); |
| 3503 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3504 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3505 | /* Now we check to see if any write operations have recently |
| 3506 | * completed |
| 3507 | */ |
| 3508 | prexor = 0; |
| 3509 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3510 | prexor = 1; |
| 3511 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3512 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3513 | sh->reconstruct_state = reconstruct_state_idle; |
| 3514 | |
| 3515 | /* All the 'written' buffers and the parity block are ready to |
| 3516 | * be written back to disk |
| 3517 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3518 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 3519 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3520 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3521 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 3522 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3523 | for (i = disks; i--; ) { |
| 3524 | struct r5dev *dev = &sh->dev[i]; |
| 3525 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3526 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3527 | dev->written)) { |
| 3528 | pr_debug("Writing block %d\n", i); |
| 3529 | set_bit(R5_Wantwrite, &dev->flags); |
| 3530 | if (prexor) |
| 3531 | continue; |
| 3532 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3533 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3534 | s.failed == 0)) |
| 3535 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3536 | } |
| 3537 | } |
| 3538 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3539 | s.dec_preread_active = 1; |
| 3540 | } |
| 3541 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 3542 | /* |
| 3543 | * might be able to return some write requests if the parity blocks |
| 3544 | * are safe, or on a failed drive |
| 3545 | */ |
| 3546 | pdev = &sh->dev[sh->pd_idx]; |
| 3547 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3548 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3549 | qdev = &sh->dev[sh->qd_idx]; |
| 3550 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3551 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3552 | || conf->level < 6; |
| 3553 | |
| 3554 | if (s.written && |
| 3555 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3556 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 3557 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 3558 | test_bit(R5_Discard, &pdev->flags))))) && |
| 3559 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3560 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 3561 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 3562 | test_bit(R5_Discard, &qdev->flags)))))) |
| 3563 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3564 | |
| 3565 | /* Now we might consider reading some blocks, either to check/generate |
| 3566 | * parity, or to satisfy requests |
| 3567 | * or to load a block that is being partially written. |
| 3568 | */ |
| 3569 | if (s.to_read || s.non_overwrite |
| 3570 | || (conf->level == 6 && s.to_write && s.failed) |
| 3571 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 3572 | || s.replacing |
| 3573 | || s.expanding) |
| 3574 | handle_stripe_fill(sh, &s, disks); |
| 3575 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3576 | /* Now to consider new write requests and what else, if anything |
| 3577 | * should be read. We do not handle new writes when: |
| 3578 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3579 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3580 | * block. |
| 3581 | */ |
| 3582 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3583 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3584 | |
| 3585 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3586 | * Any reads will already have been scheduled, so we just see if enough |
| 3587 | * data is available. The parity check is held off while parity |
| 3588 | * dependent operations are in flight. |
| 3589 | */ |
| 3590 | if (sh->check_state || |
| 3591 | (s.syncing && s.locked == 0 && |
| 3592 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3593 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3594 | if (conf->level == 6) |
| 3595 | handle_parity_checks6(conf, sh, &s, disks); |
| 3596 | else |
| 3597 | handle_parity_checks5(conf, sh, &s, disks); |
| 3598 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3599 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3600 | if (s.replacing && s.locked == 0 |
| 3601 | && !test_bit(STRIPE_INSYNC, &sh->state)) { |
| 3602 | /* Write out to replacement devices where possible */ |
| 3603 | for (i = 0; i < conf->raid_disks; i++) |
| 3604 | if (test_bit(R5_UPTODATE, &sh->dev[i].flags) && |
| 3605 | test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 3606 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 3607 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3608 | s.locked++; |
| 3609 | } |
| 3610 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3611 | } |
| 3612 | if ((s.syncing || s.replacing) && s.locked == 0 && |
| 3613 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3614 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3615 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3616 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 3617 | wake_up(&conf->wait_for_overlap); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3618 | } |
| 3619 | |
| 3620 | /* If the failed drives are just a ReadError, then we might need |
| 3621 | * to progress the repair/check process |
| 3622 | */ |
| 3623 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3624 | for (i = 0; i < s.failed; i++) { |
| 3625 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3626 | if (test_bit(R5_ReadError, &dev->flags) |
| 3627 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3628 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3629 | ) { |
| 3630 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3631 | set_bit(R5_Wantwrite, &dev->flags); |
| 3632 | set_bit(R5_ReWrite, &dev->flags); |
| 3633 | set_bit(R5_LOCKED, &dev->flags); |
| 3634 | s.locked++; |
| 3635 | } else { |
| 3636 | /* let's read it back */ |
| 3637 | set_bit(R5_Wantread, &dev->flags); |
| 3638 | set_bit(R5_LOCKED, &dev->flags); |
| 3639 | s.locked++; |
| 3640 | } |
| 3641 | } |
| 3642 | } |
| 3643 | |
| 3644 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3645 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3646 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3647 | struct stripe_head *sh_src |
| 3648 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3649 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3650 | /* sh cannot be written until sh_src has been read. |
| 3651 | * so arrange for sh to be delayed a little |
| 3652 | */ |
| 3653 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3654 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3655 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3656 | &sh_src->state)) |
| 3657 | atomic_inc(&conf->preread_active_stripes); |
| 3658 | release_stripe(sh_src); |
| 3659 | goto finish; |
| 3660 | } |
| 3661 | if (sh_src) |
| 3662 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3663 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3664 | sh->reconstruct_state = reconstruct_state_idle; |
| 3665 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3666 | for (i = conf->raid_disks; i--; ) { |
| 3667 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3668 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3669 | s.locked++; |
| 3670 | } |
| 3671 | } |
| 3672 | |
| 3673 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3674 | !sh->reconstruct_state) { |
| 3675 | /* Need to write out all blocks after computing parity */ |
| 3676 | sh->disks = conf->raid_disks; |
| 3677 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3678 | schedule_reconstruction(sh, &s, 1, 1); |
| 3679 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3680 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3681 | atomic_dec(&conf->reshape_stripes); |
| 3682 | wake_up(&conf->wait_for_overlap); |
| 3683 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3684 | } |
| 3685 | |
| 3686 | if (s.expanding && s.locked == 0 && |
| 3687 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3688 | handle_stripe_expansion(conf, sh); |
| 3689 | |
| 3690 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3691 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 3692 | if (unlikely(s.blocked_rdev)) { |
| 3693 | if (conf->mddev->external) |
| 3694 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 3695 | conf->mddev); |
| 3696 | else |
| 3697 | /* Internal metadata will immediately |
| 3698 | * be written by raid5d, so we don't |
| 3699 | * need to wait here. |
| 3700 | */ |
| 3701 | rdev_dec_pending(s.blocked_rdev, |
| 3702 | conf->mddev); |
| 3703 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3704 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3705 | if (s.handle_bad_blocks) |
| 3706 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3707 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3708 | struct r5dev *dev = &sh->dev[i]; |
| 3709 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 3710 | /* We own a safe reference to the rdev */ |
| 3711 | rdev = conf->disks[i].rdev; |
| 3712 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 3713 | STRIPE_SECTORS, 0)) |
| 3714 | md_error(conf->mddev, rdev); |
| 3715 | rdev_dec_pending(rdev, conf->mddev); |
| 3716 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3717 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 3718 | rdev = conf->disks[i].rdev; |
| 3719 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3720 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3721 | rdev_dec_pending(rdev, conf->mddev); |
| 3722 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3723 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3724 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3725 | if (!rdev) |
| 3726 | /* rdev have been moved down */ |
| 3727 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3728 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 3729 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3730 | rdev_dec_pending(rdev, conf->mddev); |
| 3731 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3732 | } |
| 3733 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3734 | if (s.ops_request) |
| 3735 | raid_run_ops(sh, s.ops_request); |
| 3736 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 3737 | ops_run_io(sh, &s); |
| 3738 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3739 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3740 | /* 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] | 3741 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 3742 | * have actually been submitted. |
| 3743 | */ |
| 3744 | atomic_dec(&conf->preread_active_stripes); |
| 3745 | if (atomic_read(&conf->preread_active_stripes) < |
| 3746 | IO_THRESHOLD) |
| 3747 | md_wakeup_thread(conf->mddev->thread); |
| 3748 | } |
| 3749 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 3750 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3751 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3752 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3753 | } |
| 3754 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3755 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3756 | { |
| 3757 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 3758 | while (!list_empty(&conf->delayed_list)) { |
| 3759 | struct list_head *l = conf->delayed_list.next; |
| 3760 | struct stripe_head *sh; |
| 3761 | sh = list_entry(l, struct stripe_head, lru); |
| 3762 | list_del_init(l); |
| 3763 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3764 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3765 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3766 | list_add_tail(&sh->lru, &conf->hold_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3767 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 3768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3769 | } |
| 3770 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3771 | static void activate_bit_delay(struct r5conf *conf) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3772 | { |
| 3773 | /* device_lock is held */ |
| 3774 | struct list_head head; |
| 3775 | list_add(&head, &conf->bitmap_list); |
| 3776 | list_del_init(&conf->bitmap_list); |
| 3777 | while (!list_empty(&head)) { |
| 3778 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
| 3779 | list_del_init(&sh->lru); |
| 3780 | atomic_inc(&sh->count); |
| 3781 | __release_stripe(conf, sh); |
| 3782 | } |
| 3783 | } |
| 3784 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3785 | int md_raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3786 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3787 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3788 | |
| 3789 | /* No difference between reads and writes. Just check |
| 3790 | * how busy the stripe_cache is |
| 3791 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 3792 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3793 | if (conf->inactive_blocked) |
| 3794 | return 1; |
| 3795 | if (conf->quiesce) |
| 3796 | return 1; |
| 3797 | if (list_empty_careful(&conf->inactive_list)) |
| 3798 | return 1; |
| 3799 | |
| 3800 | return 0; |
| 3801 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3802 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 3803 | |
| 3804 | static int raid5_congested(void *data, int bits) |
| 3805 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3806 | struct mddev *mddev = data; |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 3807 | |
| 3808 | return mddev_congested(mddev, bits) || |
| 3809 | md_raid5_congested(mddev, bits); |
| 3810 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 3811 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3812 | /* We want read requests to align with chunks where possible, |
| 3813 | * but write requests don't need to. |
| 3814 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3815 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 3816 | struct bvec_merge_data *bvm, |
| 3817 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3818 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3819 | struct mddev *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3820 | 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] | 3821 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3822 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3823 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3824 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 3825 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3826 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 3827 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3828 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3829 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 3830 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 3831 | if (max < 0) max = 0; |
| 3832 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 3833 | return biovec->bv_len; |
| 3834 | else |
| 3835 | return max; |
| 3836 | } |
| 3837 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3838 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3839 | 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] | 3840 | { |
| 3841 | sector_t sector = bio->bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 3842 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3843 | unsigned int bio_sectors = bio->bi_size >> 9; |
| 3844 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 3845 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 3846 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3847 | return chunk_sectors >= |
| 3848 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 3849 | } |
| 3850 | |
| 3851 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3852 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 3853 | * later sampled by raid5d. |
| 3854 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3855 | 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] | 3856 | { |
| 3857 | unsigned long flags; |
| 3858 | |
| 3859 | spin_lock_irqsave(&conf->device_lock, flags); |
| 3860 | |
| 3861 | bi->bi_next = conf->retry_read_aligned_list; |
| 3862 | conf->retry_read_aligned_list = bi; |
| 3863 | |
| 3864 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 3865 | md_wakeup_thread(conf->mddev->thread); |
| 3866 | } |
| 3867 | |
| 3868 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3869 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3870 | { |
| 3871 | struct bio *bi; |
| 3872 | |
| 3873 | bi = conf->retry_read_aligned; |
| 3874 | if (bi) { |
| 3875 | conf->retry_read_aligned = NULL; |
| 3876 | return bi; |
| 3877 | } |
| 3878 | bi = conf->retry_read_aligned_list; |
| 3879 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3880 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3881 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 3882 | /* |
| 3883 | * this sets the active strip count to 1 and the processed |
| 3884 | * strip count to zero (upper 8 bits) |
| 3885 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3886 | 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] | 3887 | } |
| 3888 | |
| 3889 | return bi; |
| 3890 | } |
| 3891 | |
| 3892 | |
| 3893 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3894 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 3895 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 3896 | * first). |
| 3897 | * If the read failed.. |
| 3898 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3899 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3900 | { |
| 3901 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3902 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3903 | struct r5conf *conf; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3904 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3905 | struct md_rdev *rdev; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3906 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3907 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3908 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3909 | rdev = (void*)raid_bi->bi_next; |
| 3910 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 3911 | mddev = rdev->mddev; |
| 3912 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3913 | |
| 3914 | rdev_dec_pending(rdev, conf->mddev); |
| 3915 | |
| 3916 | if (!error && uptodate) { |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3917 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3918 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 3919 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 3920 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3921 | } |
| 3922 | |
| 3923 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3924 | 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] | 3925 | |
| 3926 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3927 | } |
| 3928 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3929 | static int bio_fits_rdev(struct bio *bi) |
| 3930 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 3931 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3932 | |
Martin K. Petersen | ae03bf6 | 2009-05-22 17:17:50 -0400 | [diff] [blame] | 3933 | if ((bi->bi_size>>9) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3934 | return 0; |
| 3935 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 3936 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 3937 | return 0; |
| 3938 | |
| 3939 | if (q->merge_bvec_fn) |
| 3940 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 3941 | * just just give up |
| 3942 | */ |
| 3943 | return 0; |
| 3944 | |
| 3945 | return 1; |
| 3946 | } |
| 3947 | |
| 3948 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 3949 | 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] | 3950 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3951 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 3952 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3953 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3954 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3955 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3956 | |
| 3957 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3958 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3959 | return 0; |
| 3960 | } |
| 3961 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3962 | * 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] | 3963 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 3964 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3965 | if (!align_bi) |
| 3966 | return 0; |
| 3967 | /* |
| 3968 | * set bi_end_io to a new function, and set bi_private to the |
| 3969 | * original bio. |
| 3970 | */ |
| 3971 | align_bi->bi_end_io = raid5_align_endio; |
| 3972 | align_bi->bi_private = raid_bio; |
| 3973 | /* |
| 3974 | * compute position |
| 3975 | */ |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3976 | align_bi->bi_sector = raid5_compute_sector(conf, raid_bio->bi_sector, |
| 3977 | 0, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3978 | &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3979 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3980 | 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] | 3981 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3982 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 3983 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 3984 | rdev->recovery_offset < end_sector) { |
| 3985 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 3986 | if (rdev && |
| 3987 | (test_bit(Faulty, &rdev->flags) || |
| 3988 | !(test_bit(In_sync, &rdev->flags) || |
| 3989 | rdev->recovery_offset >= end_sector))) |
| 3990 | rdev = NULL; |
| 3991 | } |
| 3992 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3993 | sector_t first_bad; |
| 3994 | int bad_sectors; |
| 3995 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 3996 | atomic_inc(&rdev->nr_pending); |
| 3997 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 3998 | raid_bio->bi_next = (void*)rdev; |
| 3999 | align_bi->bi_bdev = rdev->bdev; |
| 4000 | align_bi->bi_flags &= ~(1 << BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4001 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4002 | if (!bio_fits_rdev(align_bi) || |
| 4003 | is_badblock(rdev, align_bi->bi_sector, align_bi->bi_size>>9, |
| 4004 | &first_bad, &bad_sectors)) { |
| 4005 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4006 | bio_put(align_bi); |
| 4007 | rdev_dec_pending(rdev, mddev); |
| 4008 | return 0; |
| 4009 | } |
| 4010 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 4011 | /* No reshape active, so we can trust rdev->data_offset */ |
| 4012 | align_bi->bi_sector += rdev->data_offset; |
| 4013 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4014 | spin_lock_irq(&conf->device_lock); |
| 4015 | wait_event_lock_irq(conf->wait_for_stripe, |
| 4016 | conf->quiesce == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 4017 | conf->device_lock); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4018 | atomic_inc(&conf->active_aligned_reads); |
| 4019 | spin_unlock_irq(&conf->device_lock); |
| 4020 | |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4021 | if (mddev->gendisk) |
| 4022 | trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), |
| 4023 | align_bi, disk_devt(mddev->gendisk), |
| 4024 | raid_bio->bi_sector); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4025 | generic_make_request(align_bi); |
| 4026 | return 1; |
| 4027 | } else { |
| 4028 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4029 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4030 | return 0; |
| 4031 | } |
| 4032 | } |
| 4033 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4034 | /* __get_priority_stripe - get the next stripe to process |
| 4035 | * |
| 4036 | * Full stripe writes are allowed to pass preread active stripes up until |
| 4037 | * the bypass_threshold is exceeded. In general the bypass_count |
| 4038 | * increments when the handle_list is handled before the hold_list; however, it |
| 4039 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 4040 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 4041 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 4042 | * handle_list. |
| 4043 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4044 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4045 | { |
| 4046 | struct stripe_head *sh; |
| 4047 | |
| 4048 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 4049 | __func__, |
| 4050 | list_empty(&conf->handle_list) ? "empty" : "busy", |
| 4051 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 4052 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 4053 | |
| 4054 | if (!list_empty(&conf->handle_list)) { |
| 4055 | sh = list_entry(conf->handle_list.next, typeof(*sh), lru); |
| 4056 | |
| 4057 | if (list_empty(&conf->hold_list)) |
| 4058 | conf->bypass_count = 0; |
| 4059 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 4060 | if (conf->hold_list.next == conf->last_hold) |
| 4061 | conf->bypass_count++; |
| 4062 | else { |
| 4063 | conf->last_hold = conf->hold_list.next; |
| 4064 | conf->bypass_count -= conf->bypass_threshold; |
| 4065 | if (conf->bypass_count < 0) |
| 4066 | conf->bypass_count = 0; |
| 4067 | } |
| 4068 | } |
| 4069 | } else if (!list_empty(&conf->hold_list) && |
| 4070 | ((conf->bypass_threshold && |
| 4071 | conf->bypass_count > conf->bypass_threshold) || |
| 4072 | atomic_read(&conf->pending_full_writes) == 0)) { |
| 4073 | sh = list_entry(conf->hold_list.next, |
| 4074 | typeof(*sh), lru); |
| 4075 | conf->bypass_count -= conf->bypass_threshold; |
| 4076 | if (conf->bypass_count < 0) |
| 4077 | conf->bypass_count = 0; |
| 4078 | } else |
| 4079 | return NULL; |
| 4080 | |
| 4081 | list_del_init(&sh->lru); |
| 4082 | atomic_inc(&sh->count); |
| 4083 | BUG_ON(atomic_read(&sh->count) != 1); |
| 4084 | return sh; |
| 4085 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4086 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4087 | struct raid5_plug_cb { |
| 4088 | struct blk_plug_cb cb; |
| 4089 | struct list_head list; |
| 4090 | }; |
| 4091 | |
| 4092 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 4093 | { |
| 4094 | struct raid5_plug_cb *cb = container_of( |
| 4095 | blk_cb, struct raid5_plug_cb, cb); |
| 4096 | struct stripe_head *sh; |
| 4097 | struct mddev *mddev = cb->cb.data; |
| 4098 | struct r5conf *conf = mddev->private; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4099 | int cnt = 0; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4100 | |
| 4101 | if (cb->list.next && !list_empty(&cb->list)) { |
| 4102 | spin_lock_irq(&conf->device_lock); |
| 4103 | while (!list_empty(&cb->list)) { |
| 4104 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 4105 | list_del_init(&sh->lru); |
| 4106 | /* |
| 4107 | * avoid race release_stripe_plug() sees |
| 4108 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 4109 | * is still in our list |
| 4110 | */ |
| 4111 | smp_mb__before_clear_bit(); |
| 4112 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
| 4113 | __release_stripe(conf, sh); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4114 | cnt++; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4115 | } |
| 4116 | spin_unlock_irq(&conf->device_lock); |
| 4117 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4118 | if (mddev->queue) |
| 4119 | trace_block_unplug(mddev->queue, cnt, !from_schedule); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4120 | kfree(cb); |
| 4121 | } |
| 4122 | |
| 4123 | static void release_stripe_plug(struct mddev *mddev, |
| 4124 | struct stripe_head *sh) |
| 4125 | { |
| 4126 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 4127 | raid5_unplug, mddev, |
| 4128 | sizeof(struct raid5_plug_cb)); |
| 4129 | struct raid5_plug_cb *cb; |
| 4130 | |
| 4131 | if (!blk_cb) { |
| 4132 | release_stripe(sh); |
| 4133 | return; |
| 4134 | } |
| 4135 | |
| 4136 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 4137 | |
| 4138 | if (cb->list.next == NULL) |
| 4139 | INIT_LIST_HEAD(&cb->list); |
| 4140 | |
| 4141 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 4142 | list_add_tail(&sh->lru, &cb->list); |
| 4143 | else |
| 4144 | release_stripe(sh); |
| 4145 | } |
| 4146 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4147 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 4148 | { |
| 4149 | struct r5conf *conf = mddev->private; |
| 4150 | sector_t logical_sector, last_sector; |
| 4151 | struct stripe_head *sh; |
| 4152 | int remaining; |
| 4153 | int stripe_sectors; |
| 4154 | |
| 4155 | if (mddev->reshape_position != MaxSector) |
| 4156 | /* Skip discard while reshape is happening */ |
| 4157 | return; |
| 4158 | |
| 4159 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4160 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4161 | |
| 4162 | bi->bi_next = NULL; |
| 4163 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
| 4164 | |
| 4165 | stripe_sectors = conf->chunk_sectors * |
| 4166 | (conf->raid_disks - conf->max_degraded); |
| 4167 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 4168 | stripe_sectors); |
| 4169 | sector_div(last_sector, stripe_sectors); |
| 4170 | |
| 4171 | logical_sector *= conf->chunk_sectors; |
| 4172 | last_sector *= conf->chunk_sectors; |
| 4173 | |
| 4174 | for (; logical_sector < last_sector; |
| 4175 | logical_sector += STRIPE_SECTORS) { |
| 4176 | DEFINE_WAIT(w); |
| 4177 | int d; |
| 4178 | again: |
| 4179 | sh = get_active_stripe(conf, logical_sector, 0, 0, 0); |
| 4180 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 4181 | TASK_UNINTERRUPTIBLE); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4182 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
| 4183 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 4184 | release_stripe(sh); |
| 4185 | schedule(); |
| 4186 | goto again; |
| 4187 | } |
| 4188 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4189 | spin_lock_irq(&sh->stripe_lock); |
| 4190 | for (d = 0; d < conf->raid_disks; d++) { |
| 4191 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 4192 | continue; |
| 4193 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 4194 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 4195 | spin_unlock_irq(&sh->stripe_lock); |
| 4196 | release_stripe(sh); |
| 4197 | schedule(); |
| 4198 | goto again; |
| 4199 | } |
| 4200 | } |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4201 | set_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4202 | finish_wait(&conf->wait_for_overlap, &w); |
| 4203 | for (d = 0; d < conf->raid_disks; d++) { |
| 4204 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 4205 | continue; |
| 4206 | sh->dev[d].towrite = bi; |
| 4207 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
| 4208 | raid5_inc_bi_active_stripes(bi); |
| 4209 | } |
| 4210 | spin_unlock_irq(&sh->stripe_lock); |
| 4211 | if (conf->mddev->bitmap) { |
| 4212 | for (d = 0; |
| 4213 | d < conf->raid_disks - conf->max_degraded; |
| 4214 | d++) |
| 4215 | bitmap_startwrite(mddev->bitmap, |
| 4216 | sh->sector, |
| 4217 | STRIPE_SECTORS, |
| 4218 | 0); |
| 4219 | sh->bm_seq = conf->seq_flush + 1; |
| 4220 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 4221 | } |
| 4222 | |
| 4223 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4224 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4225 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4226 | atomic_inc(&conf->preread_active_stripes); |
| 4227 | release_stripe_plug(mddev, sh); |
| 4228 | } |
| 4229 | |
| 4230 | remaining = raid5_dec_bi_active_stripes(bi); |
| 4231 | if (remaining == 0) { |
| 4232 | md_write_end(mddev); |
| 4233 | bio_endio(bi, 0); |
| 4234 | } |
| 4235 | } |
| 4236 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 4237 | static void make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4238 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4239 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4240 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4241 | sector_t new_sector; |
| 4242 | sector_t logical_sector, last_sector; |
| 4243 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 4244 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 4245 | int remaining; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4246 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4247 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 4248 | md_flush_request(mddev, bi); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4249 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4250 | } |
| 4251 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4252 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4253 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 4254 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4255 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 4256 | chunk_aligned_read(mddev,bi)) |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4257 | return; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4258 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4259 | if (unlikely(bi->bi_rw & REQ_DISCARD)) { |
| 4260 | make_discard_request(mddev, bi); |
| 4261 | return; |
| 4262 | } |
| 4263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4264 | logical_sector = bi->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4265 | last_sector = bi->bi_sector + (bi->bi_size>>9); |
| 4266 | bi->bi_next = NULL; |
| 4267 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4268 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4269 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
| 4270 | DEFINE_WAIT(w); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4271 | int previous; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4272 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4273 | retry: |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4274 | previous = 0; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4275 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4276 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4277 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4278 | * 64bit on a 32bit platform, and so it might be |
| 4279 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 4280 | * Of course reshape_progress could change after |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4281 | * the lock is dropped, so once we get a reference |
| 4282 | * to the stripe that we think it is, we will have |
| 4283 | * to check again. |
| 4284 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4285 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4286 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4287 | ? logical_sector < conf->reshape_progress |
| 4288 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4289 | previous = 1; |
| 4290 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4291 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4292 | ? logical_sector < conf->reshape_safe |
| 4293 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4294 | spin_unlock_irq(&conf->device_lock); |
| 4295 | schedule(); |
| 4296 | goto retry; |
| 4297 | } |
| 4298 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4299 | spin_unlock_irq(&conf->device_lock); |
| 4300 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4301 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4302 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4303 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4304 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4305 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4306 | (unsigned long long)new_sector, |
| 4307 | (unsigned long long)logical_sector); |
| 4308 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4309 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4310 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4311 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4312 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4313 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4314 | * stripe, so we must do the range check again. |
| 4315 | * Expansion could still move past after this |
| 4316 | * test, but as we are holding a reference to |
| 4317 | * 'sh', we know that if that happens, |
| 4318 | * STRIPE_EXPANDING will get set and the expansion |
| 4319 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4320 | */ |
| 4321 | int must_retry = 0; |
| 4322 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4323 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4324 | ? logical_sector >= conf->reshape_progress |
| 4325 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4326 | /* mismatch, need to try again */ |
| 4327 | must_retry = 1; |
| 4328 | spin_unlock_irq(&conf->device_lock); |
| 4329 | if (must_retry) { |
| 4330 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 4331 | schedule(); |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4332 | goto retry; |
| 4333 | } |
| 4334 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4335 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4336 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4337 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4338 | logical_sector < mddev->suspend_hi) { |
| 4339 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4340 | /* As the suspend_* range is controlled by |
| 4341 | * userspace, we want an interruptible |
| 4342 | * wait. |
| 4343 | */ |
| 4344 | flush_signals(current); |
| 4345 | prepare_to_wait(&conf->wait_for_overlap, |
| 4346 | &w, TASK_INTERRUPTIBLE); |
| 4347 | if (logical_sector >= mddev->suspend_lo && |
| 4348 | logical_sector < mddev->suspend_hi) |
| 4349 | schedule(); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4350 | goto retry; |
| 4351 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4352 | |
| 4353 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4354 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4355 | /* Stripe is busy expanding or |
| 4356 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4357 | * and wait a while |
| 4358 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4359 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4360 | release_stripe(sh); |
| 4361 | schedule(); |
| 4362 | goto retry; |
| 4363 | } |
| 4364 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4365 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4366 | clear_bit(STRIPE_DELAYED, &sh->state); |
NeilBrown | a852d7b | 2012-09-19 12:48:30 +1000 | [diff] [blame] | 4367 | if ((bi->bi_rw & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4368 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4369 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4370 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4371 | } else { |
| 4372 | /* cannot get stripe for read-ahead, just give-up */ |
| 4373 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
| 4374 | finish_wait(&conf->wait_for_overlap, &w); |
| 4375 | break; |
| 4376 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4377 | } |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4378 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4379 | remaining = raid5_dec_bi_active_stripes(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4380 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4381 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4382 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4383 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4384 | |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4385 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4386 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4387 | } |
| 4388 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4389 | 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] | 4390 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4391 | 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] | 4392 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4393 | /* reshaping is quite different to recovery/resync so it is |
| 4394 | * handled quite separately ... here. |
| 4395 | * |
| 4396 | * On each call to sync_request, we gather one chunk worth of |
| 4397 | * destination stripes and flag them as expanding. |
| 4398 | * Then we find all the source stripes and request reads. |
| 4399 | * As the reads complete, handle_stripe will copy the data |
| 4400 | * into the destination stripe and release that stripe. |
| 4401 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4402 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4403 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4404 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4405 | int raid_disks = conf->previous_raid_disks; |
| 4406 | int data_disks = raid_disks - conf->max_degraded; |
| 4407 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4408 | int i; |
| 4409 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4410 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4411 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4412 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4413 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4414 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4415 | if (sector_nr == 0) { |
| 4416 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4417 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4418 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4419 | sector_nr = raid5_size(mddev, 0, 0) |
| 4420 | - conf->reshape_progress; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4421 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4422 | conf->reshape_progress > 0) |
| 4423 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4424 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4425 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4426 | mddev->curr_resync_completed = sector_nr; |
| 4427 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4428 | *skipped = 1; |
| 4429 | return sector_nr; |
| 4430 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4431 | } |
| 4432 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4433 | /* We need to process a full chunk at a time. |
| 4434 | * If old and new chunk sizes differ, we need to process the |
| 4435 | * largest of these |
| 4436 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4437 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4438 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4439 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4440 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4441 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4442 | /* We update the metadata at least every 10 seconds, or when |
| 4443 | * the data about to be copied would over-write the source of |
| 4444 | * the data at the front of the range. i.e. one new_stripe |
| 4445 | * along from reshape_progress new_maps to after where |
| 4446 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4447 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4448 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4449 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4450 | readpos = conf->reshape_progress; |
| 4451 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4452 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4453 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4454 | if (mddev->reshape_backwards) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4455 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4456 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4457 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4458 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4459 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4460 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4461 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4462 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4463 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4464 | /* Having calculated the 'writepos' possibly use it |
| 4465 | * to set 'stripe_addr' which is where we will write to. |
| 4466 | */ |
| 4467 | if (mddev->reshape_backwards) { |
| 4468 | BUG_ON(conf->reshape_progress == 0); |
| 4469 | stripe_addr = writepos; |
| 4470 | BUG_ON((mddev->dev_sectors & |
| 4471 | ~((sector_t)reshape_sectors - 1)) |
| 4472 | - reshape_sectors - stripe_addr |
| 4473 | != sector_nr); |
| 4474 | } else { |
| 4475 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 4476 | stripe_addr = sector_nr; |
| 4477 | } |
| 4478 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4479 | /* 'writepos' is the most advanced device address we might write. |
| 4480 | * 'readpos' is the least advanced device address we might read. |
| 4481 | * 'safepos' is the least address recorded in the metadata as having |
| 4482 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4483 | * If there is a min_offset_diff, these are adjusted either by |
| 4484 | * increasing the safepos/readpos if diff is negative, or |
| 4485 | * increasing writepos if diff is positive. |
| 4486 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4487 | * ensure safety in the face of a crash - that must be done by userspace |
| 4488 | * making a backup of the data. So in that case there is no particular |
| 4489 | * rush to update metadata. |
| 4490 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4491 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4492 | * we can be safe in the event of a crash. |
| 4493 | * So we insist on updating metadata if safepos is behind writepos and |
| 4494 | * readpos is beyond writepos. |
| 4495 | * In any case, update the metadata every 10 seconds. |
| 4496 | * Maybe that number should be configurable, but I'm not sure it is |
| 4497 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4498 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4499 | if (conf->min_offset_diff < 0) { |
| 4500 | safepos += -conf->min_offset_diff; |
| 4501 | readpos += -conf->min_offset_diff; |
| 4502 | } else |
| 4503 | writepos += conf->min_offset_diff; |
| 4504 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4505 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4506 | ? (safepos > writepos && readpos < writepos) |
| 4507 | : (safepos < writepos && readpos > writepos)) || |
| 4508 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4509 | /* Cannot proceed until we've updated the superblock... */ |
| 4510 | wait_event(conf->wait_for_overlap, |
| 4511 | atomic_read(&conf->reshape_stripes)==0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4512 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4513 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4514 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4515 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4516 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4517 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4518 | kthread_should_stop()); |
| 4519 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4520 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4521 | spin_unlock_irq(&conf->device_lock); |
| 4522 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4523 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4524 | } |
| 4525 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4526 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4527 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4528 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4529 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4530 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4531 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4532 | atomic_inc(&conf->reshape_stripes); |
| 4533 | /* If any of this stripe is beyond the end of the old |
| 4534 | * array, then we need to zero those blocks |
| 4535 | */ |
| 4536 | for (j=sh->disks; j--;) { |
| 4537 | sector_t s; |
| 4538 | if (j == sh->pd_idx) |
| 4539 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4540 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4541 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4542 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4543 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4544 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4545 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4546 | continue; |
| 4547 | } |
| 4548 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4549 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4550 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4551 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4552 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4553 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4554 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4555 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4556 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4557 | } |
| 4558 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4559 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4560 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4561 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4562 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4563 | spin_unlock_irq(&conf->device_lock); |
| 4564 | /* Ok, those stripe are ready. We can start scheduling |
| 4565 | * reads on the source stripes. |
| 4566 | * The source stripes are determined by mapping the first and last |
| 4567 | * block on the destination stripes. |
| 4568 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4569 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4570 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4571 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4572 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4573 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4574 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4575 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4576 | if (last_sector >= mddev->dev_sectors) |
| 4577 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4578 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4579 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4580 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4581 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4582 | release_stripe(sh); |
| 4583 | first_sector += STRIPE_SECTORS; |
| 4584 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4585 | /* Now that the sources are clearly marked, we can release |
| 4586 | * the destination stripes |
| 4587 | */ |
| 4588 | while (!list_empty(&stripes)) { |
| 4589 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4590 | list_del_init(&sh->lru); |
| 4591 | release_stripe(sh); |
| 4592 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4593 | /* If this takes us to the resync_max point where we have to pause, |
| 4594 | * then we need to write out the superblock. |
| 4595 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4596 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4597 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4598 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4599 | /* Cannot proceed until we've updated the superblock... */ |
| 4600 | wait_event(conf->wait_for_overlap, |
| 4601 | atomic_read(&conf->reshape_stripes) == 0); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4602 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4603 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4604 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4605 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4606 | md_wakeup_thread(mddev->thread); |
| 4607 | wait_event(mddev->sb_wait, |
| 4608 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
| 4609 | || kthread_should_stop()); |
| 4610 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4611 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4612 | spin_unlock_irq(&conf->device_lock); |
| 4613 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4614 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4615 | } |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4616 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4617 | } |
| 4618 | |
| 4619 | /* FIXME go_faster isn't used */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4620 | 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] | 4621 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4622 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4623 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4624 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 4625 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4626 | int still_degraded = 0; |
| 4627 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4628 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4629 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4630 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 4631 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 4632 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 4633 | end_reshape(conf); |
| 4634 | return 0; |
| 4635 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4636 | |
| 4637 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 4638 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 4639 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4640 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4641 | conf->fullsync = 0; |
| 4642 | bitmap_close_sync(mddev->bitmap); |
| 4643 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4644 | return 0; |
| 4645 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4646 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 4647 | /* Allow raid5_quiesce to complete */ |
| 4648 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 4649 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4650 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 4651 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 4652 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4653 | /* No need to check resync_max as we never do more than one |
| 4654 | * stripe, and as resync_max will always be on a chunk boundary, |
| 4655 | * if the check in md_do_sync didn't fire, there is no chance |
| 4656 | * of overstepping resync_max here |
| 4657 | */ |
| 4658 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4659 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4660 | * to resync, then assert that we are finished, because there is |
| 4661 | * nothing we can do. |
| 4662 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 4663 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4664 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4665 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 4666 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | return rv; |
| 4668 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4669 | if (!bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
NeilBrown | 3855ad9 | 2005-11-08 21:39:38 -0800 | [diff] [blame] | 4670 | !test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4671 | !conf->fullsync && sync_blocks >= STRIPE_SECTORS) { |
| 4672 | /* we can skip this block, and probably more */ |
| 4673 | sync_blocks /= STRIPE_SECTORS; |
| 4674 | *skipped = 1; |
| 4675 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 4676 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4677 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 4678 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 4679 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4680 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4681 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4682 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4683 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4684 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4685 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 4686 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4687 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4688 | /* Need to check if array will still be degraded after recovery/resync |
| 4689 | * We don't need to check the 'failed' flag as when that gets set, |
| 4690 | * recovery aborts. |
| 4691 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 4692 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4693 | if (conf->disks[i].rdev == NULL) |
| 4694 | still_degraded = 1; |
| 4695 | |
| 4696 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 4697 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 4698 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4699 | |
NeilBrown | 1442577 | 2009-10-16 15:55:25 +1100 | [diff] [blame] | 4700 | handle_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4701 | release_stripe(sh); |
| 4702 | |
| 4703 | return STRIPE_SECTORS; |
| 4704 | } |
| 4705 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4706 | 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] | 4707 | { |
| 4708 | /* We may not be able to submit a whole bio at once as there |
| 4709 | * may not be enough stripe_heads available. |
| 4710 | * We cannot pre-allocate enough stripe_heads as we may need |
| 4711 | * more than exist in the cache (if we allow ever large chunks). |
| 4712 | * So we do one stripe head at a time and record in |
| 4713 | * ->bi_hw_segments how many have been done. |
| 4714 | * |
| 4715 | * We *know* that this entire raid_bio is in one chunk, so |
| 4716 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 4717 | */ |
| 4718 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4719 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4720 | sector_t sector, logical_sector, last_sector; |
| 4721 | int scnt = 0; |
| 4722 | int remaining; |
| 4723 | int handled = 0; |
| 4724 | |
| 4725 | logical_sector = raid_bio->bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4726 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4727 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4728 | last_sector = raid_bio->bi_sector + (raid_bio->bi_size>>9); |
| 4729 | |
| 4730 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4731 | logical_sector += STRIPE_SECTORS, |
| 4732 | sector += STRIPE_SECTORS, |
| 4733 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4734 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4735 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4736 | /* already done this stripe */ |
| 4737 | continue; |
| 4738 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4739 | sh = get_active_stripe(conf, sector, 0, 1, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4740 | |
| 4741 | if (!sh) { |
| 4742 | /* failed to get a stripe - must wait */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4743 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4744 | conf->retry_read_aligned = raid_bio; |
| 4745 | return handled; |
| 4746 | } |
| 4747 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4748 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 4749 | release_stripe(sh); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4750 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4751 | conf->retry_read_aligned = raid_bio; |
| 4752 | return handled; |
| 4753 | } |
| 4754 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 4755 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4756 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4757 | release_stripe(sh); |
| 4758 | handled++; |
| 4759 | } |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4760 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
Tejun Heo | 3a366e6 | 2013-01-11 13:06:33 -0800 | [diff] [blame] | 4761 | if (remaining == 0) |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4762 | bio_endio(raid_bio, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4763 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4764 | wake_up(&conf->wait_for_stripe); |
| 4765 | return handled; |
| 4766 | } |
| 4767 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4768 | #define MAX_STRIPE_BATCH 8 |
| 4769 | static int handle_active_stripes(struct r5conf *conf) |
| 4770 | { |
| 4771 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
| 4772 | int i, batch_size = 0; |
| 4773 | |
| 4774 | while (batch_size < MAX_STRIPE_BATCH && |
| 4775 | (sh = __get_priority_stripe(conf)) != NULL) |
| 4776 | batch[batch_size++] = sh; |
| 4777 | |
| 4778 | if (batch_size == 0) |
| 4779 | return batch_size; |
| 4780 | spin_unlock_irq(&conf->device_lock); |
| 4781 | |
| 4782 | for (i = 0; i < batch_size; i++) |
| 4783 | handle_stripe(batch[i]); |
| 4784 | |
| 4785 | cond_resched(); |
| 4786 | |
| 4787 | spin_lock_irq(&conf->device_lock); |
| 4788 | for (i = 0; i < batch_size; i++) |
| 4789 | __release_stripe(conf, batch[i]); |
| 4790 | return batch_size; |
| 4791 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4792 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4793 | /* |
| 4794 | * This is our raid5 kernel thread. |
| 4795 | * |
| 4796 | * We scan the hash table for stripes which can be handled now. |
| 4797 | * During the scan, completed stripes are saved for us by the interrupt |
| 4798 | * handler, so that they will not have to wait for our next wakeup. |
| 4799 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 4800 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4801 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 4802 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4803 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4804 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4805 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4806 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4807 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4808 | |
| 4809 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4810 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4811 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4812 | handled = 0; |
| 4813 | spin_lock_irq(&conf->device_lock); |
| 4814 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4815 | struct bio *bio; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4816 | int batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4817 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 4818 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4819 | !list_empty(&conf->bitmap_list)) { |
| 4820 | /* Now is a good time to flush some bitmap updates */ |
| 4821 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4822 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4823 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 4824 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4825 | conf->seq_write = conf->seq_flush; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4826 | activate_bit_delay(conf); |
| 4827 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 4828 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4829 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4830 | while ((bio = remove_bio_from_retry(conf))) { |
| 4831 | int ok; |
| 4832 | spin_unlock_irq(&conf->device_lock); |
| 4833 | ok = retry_aligned_read(conf, bio); |
| 4834 | spin_lock_irq(&conf->device_lock); |
| 4835 | if (!ok) |
| 4836 | break; |
| 4837 | handled++; |
| 4838 | } |
| 4839 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4840 | batch_size = handle_active_stripes(conf); |
| 4841 | if (!batch_size) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4842 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4843 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4844 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4845 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) { |
| 4846 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 4847 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 4848 | spin_lock_irq(&conf->device_lock); |
| 4849 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4850 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4851 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4852 | |
| 4853 | spin_unlock_irq(&conf->device_lock); |
| 4854 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 4855 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 4856 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4857 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4858 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4859 | } |
| 4860 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4861 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4862 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4863 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4864 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4865 | if (conf) |
| 4866 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 4867 | else |
| 4868 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4869 | } |
| 4870 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4871 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4872 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4873 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4874 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4875 | int err; |
| 4876 | |
| 4877 | if (size <= 16 || size > 32768) |
| 4878 | return -EINVAL; |
| 4879 | while (size < conf->max_nr_stripes) { |
| 4880 | if (drop_one_stripe(conf)) |
| 4881 | conf->max_nr_stripes--; |
| 4882 | else |
| 4883 | break; |
| 4884 | } |
| 4885 | err = md_allow_write(mddev); |
| 4886 | if (err) |
| 4887 | return err; |
| 4888 | while (size > conf->max_nr_stripes) { |
| 4889 | if (grow_one_stripe(conf)) |
| 4890 | conf->max_nr_stripes++; |
| 4891 | else break; |
| 4892 | } |
| 4893 | return 0; |
| 4894 | } |
| 4895 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 4896 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4897 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4898 | 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] | 4899 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4900 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4901 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4902 | int err; |
| 4903 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4904 | if (len >= PAGE_SIZE) |
| 4905 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4906 | if (!conf) |
| 4907 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4908 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4909 | if (strict_strtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4910 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 4911 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 4912 | if (err) |
| 4913 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4914 | return len; |
| 4915 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4916 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4917 | static struct md_sysfs_entry |
| 4918 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 4919 | raid5_show_stripe_cache_size, |
| 4920 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4921 | |
| 4922 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4923 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4924 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4925 | struct r5conf *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4926 | if (conf) |
| 4927 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 4928 | else |
| 4929 | return 0; |
| 4930 | } |
| 4931 | |
| 4932 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4933 | 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] | 4934 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4935 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4936 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4937 | if (len >= PAGE_SIZE) |
| 4938 | return -EINVAL; |
| 4939 | if (!conf) |
| 4940 | return -ENODEV; |
| 4941 | |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4942 | if (strict_strtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4943 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 4944 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4945 | return -EINVAL; |
| 4946 | conf->bypass_threshold = new; |
| 4947 | return len; |
| 4948 | } |
| 4949 | |
| 4950 | static struct md_sysfs_entry |
| 4951 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 4952 | S_IRUGO | S_IWUSR, |
| 4953 | raid5_show_preread_threshold, |
| 4954 | raid5_store_preread_threshold); |
| 4955 | |
| 4956 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4957 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4958 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4959 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4960 | if (conf) |
| 4961 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 4962 | else |
| 4963 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4964 | } |
| 4965 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 4966 | static struct md_sysfs_entry |
| 4967 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4968 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4969 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4970 | &raid5_stripecache_size.attr, |
| 4971 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4972 | &raid5_preread_bypass_threshold.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4973 | NULL, |
| 4974 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 4975 | static struct attribute_group raid5_attrs_group = { |
| 4976 | .name = NULL, |
| 4977 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 4978 | }; |
| 4979 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4980 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4981 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4982 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4983 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4984 | |
| 4985 | if (!sectors) |
| 4986 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4987 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 4988 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 4989 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4990 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4991 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4992 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 4993 | return sectors * (raid_disks - conf->max_degraded); |
| 4994 | } |
| 4995 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4996 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4997 | { |
| 4998 | struct raid5_percpu *percpu; |
| 4999 | unsigned long cpu; |
| 5000 | |
| 5001 | if (!conf->percpu) |
| 5002 | return; |
| 5003 | |
| 5004 | get_online_cpus(); |
| 5005 | for_each_possible_cpu(cpu) { |
| 5006 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 5007 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5008 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5009 | } |
| 5010 | #ifdef CONFIG_HOTPLUG_CPU |
| 5011 | unregister_cpu_notifier(&conf->cpu_notify); |
| 5012 | #endif |
| 5013 | put_online_cpus(); |
| 5014 | |
| 5015 | free_percpu(conf->percpu); |
| 5016 | } |
| 5017 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5018 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5019 | { |
| 5020 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5021 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5022 | kfree(conf->disks); |
| 5023 | kfree(conf->stripe_hashtbl); |
| 5024 | kfree(conf); |
| 5025 | } |
| 5026 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5027 | #ifdef CONFIG_HOTPLUG_CPU |
| 5028 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 5029 | void *hcpu) |
| 5030 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5031 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5032 | long cpu = (long)hcpu; |
| 5033 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 5034 | |
| 5035 | switch (action) { |
| 5036 | case CPU_UP_PREPARE: |
| 5037 | case CPU_UP_PREPARE_FROZEN: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5038 | if (conf->level == 6 && !percpu->spare_page) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5039 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5040 | if (!percpu->scribble) |
| 5041 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 5042 | |
| 5043 | if (!percpu->scribble || |
| 5044 | (conf->level == 6 && !percpu->spare_page)) { |
| 5045 | safe_put_page(percpu->spare_page); |
| 5046 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5047 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 5048 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 5049 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5050 | } |
| 5051 | break; |
| 5052 | case CPU_DEAD: |
| 5053 | case CPU_DEAD_FROZEN: |
| 5054 | safe_put_page(percpu->spare_page); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5055 | kfree(percpu->scribble); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5056 | percpu->spare_page = NULL; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5057 | percpu->scribble = NULL; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5058 | break; |
| 5059 | default: |
| 5060 | break; |
| 5061 | } |
| 5062 | return NOTIFY_OK; |
| 5063 | } |
| 5064 | #endif |
| 5065 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5066 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5067 | { |
| 5068 | unsigned long cpu; |
| 5069 | struct page *spare_page; |
Tejun Heo | a29d8b8 | 2010-02-02 14:39:15 +0900 | [diff] [blame] | 5070 | struct raid5_percpu __percpu *allcpus; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5071 | void *scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5072 | int err; |
| 5073 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5074 | allcpus = alloc_percpu(struct raid5_percpu); |
| 5075 | if (!allcpus) |
| 5076 | return -ENOMEM; |
| 5077 | conf->percpu = allcpus; |
| 5078 | |
| 5079 | get_online_cpus(); |
| 5080 | err = 0; |
| 5081 | for_each_present_cpu(cpu) { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5082 | if (conf->level == 6) { |
| 5083 | spare_page = alloc_page(GFP_KERNEL); |
| 5084 | if (!spare_page) { |
| 5085 | err = -ENOMEM; |
| 5086 | break; |
| 5087 | } |
| 5088 | per_cpu_ptr(conf->percpu, cpu)->spare_page = spare_page; |
| 5089 | } |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5090 | scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5091 | if (!scribble) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5092 | err = -ENOMEM; |
| 5093 | break; |
| 5094 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 5095 | per_cpu_ptr(conf->percpu, cpu)->scribble = scribble; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5096 | } |
| 5097 | #ifdef CONFIG_HOTPLUG_CPU |
| 5098 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 5099 | conf->cpu_notify.priority = 0; |
| 5100 | if (err == 0) |
| 5101 | err = register_cpu_notifier(&conf->cpu_notify); |
| 5102 | #endif |
| 5103 | put_online_cpus(); |
| 5104 | |
| 5105 | return err; |
| 5106 | } |
| 5107 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5108 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5109 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5110 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5111 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5112 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5113 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5114 | char pers_name[6]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5115 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5116 | if (mddev->new_level != 5 |
| 5117 | && mddev->new_level != 4 |
| 5118 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5119 | 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] | 5120 | mdname(mddev), mddev->new_level); |
| 5121 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5122 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5123 | if ((mddev->new_level == 5 |
| 5124 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 5125 | (mddev->new_level == 6 |
| 5126 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5127 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5128 | mdname(mddev), mddev->new_layout); |
| 5129 | return ERR_PTR(-EIO); |
| 5130 | } |
| 5131 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5132 | 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] | 5133 | mdname(mddev), mddev->raid_disks); |
| 5134 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5135 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5136 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5137 | if (!mddev->new_chunk_sectors || |
| 5138 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 5139 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5140 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 5141 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5142 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5143 | } |
| 5144 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5145 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5146 | if (conf == NULL) |
| 5147 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 5148 | spin_lock_init(&conf->device_lock); |
| 5149 | init_waitqueue_head(&conf->wait_for_stripe); |
| 5150 | init_waitqueue_head(&conf->wait_for_overlap); |
| 5151 | INIT_LIST_HEAD(&conf->handle_list); |
| 5152 | INIT_LIST_HEAD(&conf->hold_list); |
| 5153 | INIT_LIST_HEAD(&conf->delayed_list); |
| 5154 | INIT_LIST_HEAD(&conf->bitmap_list); |
| 5155 | INIT_LIST_HEAD(&conf->inactive_list); |
| 5156 | atomic_set(&conf->active_stripes, 0); |
| 5157 | atomic_set(&conf->preread_active_stripes, 0); |
| 5158 | atomic_set(&conf->active_aligned_reads, 0); |
| 5159 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 5160 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5161 | |
| 5162 | conf->raid_disks = mddev->raid_disks; |
| 5163 | if (mddev->reshape_position == MaxSector) |
| 5164 | conf->previous_raid_disks = mddev->raid_disks; |
| 5165 | else |
| 5166 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5167 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 5168 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5169 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5170 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5171 | GFP_KERNEL); |
| 5172 | if (!conf->disks) |
| 5173 | goto abort; |
| 5174 | |
| 5175 | conf->mddev = mddev; |
| 5176 | |
| 5177 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 5178 | goto abort; |
| 5179 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5180 | conf->level = mddev->new_level; |
| 5181 | if (raid5_alloc_percpu(conf) != 0) |
| 5182 | goto abort; |
| 5183 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5184 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5185 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5186 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5187 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5188 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5189 | || raid_disk < 0) |
| 5190 | continue; |
| 5191 | disk = conf->disks + raid_disk; |
| 5192 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5193 | if (test_bit(Replacement, &rdev->flags)) { |
| 5194 | if (disk->replacement) |
| 5195 | goto abort; |
| 5196 | disk->replacement = rdev; |
| 5197 | } else { |
| 5198 | if (disk->rdev) |
| 5199 | goto abort; |
| 5200 | disk->rdev = rdev; |
| 5201 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5202 | |
| 5203 | if (test_bit(In_sync, &rdev->flags)) { |
| 5204 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5205 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 5206 | " disk %d\n", |
| 5207 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 5208 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5209 | /* Cannot rely on bitmap to complete recovery */ |
| 5210 | conf->fullsync = 1; |
| 5211 | } |
| 5212 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5213 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5214 | conf->level = mddev->new_level; |
| 5215 | if (conf->level == 6) |
| 5216 | conf->max_degraded = 2; |
| 5217 | else |
| 5218 | conf->max_degraded = 1; |
| 5219 | conf->algorithm = mddev->new_layout; |
| 5220 | conf->max_nr_stripes = NR_STRIPES; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5221 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5222 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5223 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5224 | conf->prev_algo = mddev->layout; |
| 5225 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5226 | |
| 5227 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5228 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5229 | if (grow_stripes(conf, conf->max_nr_stripes)) { |
| 5230 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5231 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 5232 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5233 | goto abort; |
| 5234 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5235 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 5236 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5237 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5238 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 5239 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5240 | if (!conf->thread) { |
| 5241 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5242 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5243 | mdname(mddev)); |
| 5244 | goto abort; |
| 5245 | } |
| 5246 | |
| 5247 | return conf; |
| 5248 | |
| 5249 | abort: |
| 5250 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5251 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5252 | return ERR_PTR(-EIO); |
| 5253 | } else |
| 5254 | return ERR_PTR(-ENOMEM); |
| 5255 | } |
| 5256 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5257 | |
| 5258 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 5259 | { |
| 5260 | switch (algo) { |
| 5261 | case ALGORITHM_PARITY_0: |
| 5262 | if (raid_disk < max_degraded) |
| 5263 | return 1; |
| 5264 | break; |
| 5265 | case ALGORITHM_PARITY_N: |
| 5266 | if (raid_disk >= raid_disks - max_degraded) |
| 5267 | return 1; |
| 5268 | break; |
| 5269 | case ALGORITHM_PARITY_0_6: |
| 5270 | if (raid_disk == 0 || |
| 5271 | raid_disk == raid_disks - 1) |
| 5272 | return 1; |
| 5273 | break; |
| 5274 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5275 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5276 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5277 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5278 | if (raid_disk == raid_disks - 1) |
| 5279 | return 1; |
| 5280 | } |
| 5281 | return 0; |
| 5282 | } |
| 5283 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5284 | static int run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5285 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5286 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5287 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5288 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5289 | struct md_rdev *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5290 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5291 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5292 | long long min_offset_diff = 0; |
| 5293 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5294 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5295 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5296 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5297 | " -- starting background reconstruction\n", |
| 5298 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5299 | |
| 5300 | rdev_for_each(rdev, mddev) { |
| 5301 | long long diff; |
| 5302 | if (rdev->raid_disk < 0) |
| 5303 | continue; |
| 5304 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 5305 | if (first) { |
| 5306 | min_offset_diff = diff; |
| 5307 | first = 0; |
| 5308 | } else if (mddev->reshape_backwards && |
| 5309 | diff < min_offset_diff) |
| 5310 | min_offset_diff = diff; |
| 5311 | else if (!mddev->reshape_backwards && |
| 5312 | diff > min_offset_diff) |
| 5313 | min_offset_diff = diff; |
| 5314 | } |
| 5315 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5316 | if (mddev->reshape_position != MaxSector) { |
| 5317 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5318 | * Difficulties arise if the stripe we would write to |
| 5319 | * next is at or after the stripe we would read from next. |
| 5320 | * For a reshape that changes the number of devices, this |
| 5321 | * is only possible for a very short time, and mdadm makes |
| 5322 | * sure that time appears to have past before assembling |
| 5323 | * the array. So we fail if that time hasn't passed. |
| 5324 | * For a reshape that keeps the number of devices the same |
| 5325 | * mdadm must be monitoring the reshape can keeping the |
| 5326 | * critical areas read-only and backed up. It will start |
| 5327 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5328 | */ |
| 5329 | sector_t here_new, here_old; |
| 5330 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 5331 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5332 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5333 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5334 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5335 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5336 | mdname(mddev)); |
| 5337 | return -EINVAL; |
| 5338 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5339 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 5340 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5341 | * further up in new geometry must map after here in old |
| 5342 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5343 | */ |
| 5344 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5345 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5346 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5347 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 5348 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5349 | return -EINVAL; |
| 5350 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5351 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5352 | /* here_new is the stripe we will write to */ |
| 5353 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5354 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5355 | (old_disks-max_degraded)); |
| 5356 | /* here_old is the first stripe that we might need to read |
| 5357 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5358 | if (mddev->delta_disks == 0) { |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5359 | if ((here_new * mddev->new_chunk_sectors != |
| 5360 | here_old * mddev->chunk_sectors)) { |
| 5361 | printk(KERN_ERR "md/raid:%s: reshape position is" |
| 5362 | " confused - aborting\n", mdname(mddev)); |
| 5363 | return -EINVAL; |
| 5364 | } |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5365 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5366 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5367 | * and taking constant backups. |
| 5368 | * mdadm always starts a situation like this in |
| 5369 | * readonly mode so it can take control before |
| 5370 | * allowing any writes. So just check for that. |
| 5371 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5372 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 5373 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 5374 | /* not really in-place - so OK */; |
| 5375 | else if (mddev->ro == 0) { |
| 5376 | printk(KERN_ERR "md/raid:%s: in-place reshape " |
| 5377 | "must be started in read-only mode " |
| 5378 | "- aborting\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5379 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5380 | return -EINVAL; |
| 5381 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5382 | } else if (mddev->reshape_backwards |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5383 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 5384 | here_old * mddev->chunk_sectors) |
| 5385 | : (here_new * mddev->new_chunk_sectors >= |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5386 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5387 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5388 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 5389 | "auto-recovery - aborting.\n", |
| 5390 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5391 | return -EINVAL; |
| 5392 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5393 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 5394 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5395 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5396 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5397 | BUG_ON(mddev->level != mddev->new_level); |
| 5398 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5399 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5400 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5401 | } |
| 5402 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5403 | if (mddev->private == NULL) |
| 5404 | conf = setup_conf(mddev); |
| 5405 | else |
| 5406 | conf = mddev->private; |
| 5407 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5408 | if (IS_ERR(conf)) |
| 5409 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 5410 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5411 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5412 | mddev->thread = conf->thread; |
| 5413 | conf->thread = NULL; |
| 5414 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5415 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5416 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 5417 | i++) { |
| 5418 | rdev = conf->disks[i].rdev; |
| 5419 | if (!rdev && conf->disks[i].replacement) { |
| 5420 | /* The replacement is all we have yet */ |
| 5421 | rdev = conf->disks[i].replacement; |
| 5422 | conf->disks[i].replacement = NULL; |
| 5423 | clear_bit(Replacement, &rdev->flags); |
| 5424 | conf->disks[i].rdev = rdev; |
| 5425 | } |
| 5426 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5427 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5428 | if (conf->disks[i].replacement && |
| 5429 | conf->reshape_progress != MaxSector) { |
| 5430 | /* replacements and reshape simply do not mix. */ |
| 5431 | printk(KERN_ERR "md: cannot handle concurrent " |
| 5432 | "replacement and reshape.\n"); |
| 5433 | goto abort; |
| 5434 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5435 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5436 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 5437 | continue; |
| 5438 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5439 | /* This disc is not fully in-sync. However if it |
| 5440 | * just stored parity (beyond the recovery_offset), |
| 5441 | * when we don't need to be concerned about the |
| 5442 | * array being dirty. |
| 5443 | * When reshape goes 'backwards', we never have |
| 5444 | * partially completed devices, so we only need |
| 5445 | * to worry about reshape going forwards. |
| 5446 | */ |
| 5447 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 5448 | if (mddev->major_version == 0 && |
| 5449 | mddev->minor_version > 90) |
| 5450 | rdev->recovery_offset = reshape_offset; |
| 5451 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5452 | if (rdev->recovery_offset < reshape_offset) { |
| 5453 | /* We need to check old and new layout */ |
| 5454 | if (!only_parity(rdev->raid_disk, |
| 5455 | conf->algorithm, |
| 5456 | conf->raid_disks, |
| 5457 | conf->max_degraded)) |
| 5458 | continue; |
| 5459 | } |
| 5460 | if (!only_parity(rdev->raid_disk, |
| 5461 | conf->prev_algo, |
| 5462 | conf->previous_raid_disks, |
| 5463 | conf->max_degraded)) |
| 5464 | continue; |
| 5465 | dirty_parity_disks++; |
| 5466 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5467 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5468 | /* |
| 5469 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 5470 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5471 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5472 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5473 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5474 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5475 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5476 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5477 | goto abort; |
| 5478 | } |
| 5479 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5480 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5481 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5482 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 5483 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5484 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5485 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5486 | if (mddev->ok_start_degraded) |
| 5487 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5488 | "md/raid:%s: starting dirty degraded array" |
| 5489 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5490 | mdname(mddev)); |
| 5491 | else { |
| 5492 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5493 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 5494 | mdname(mddev)); |
| 5495 | goto abort; |
| 5496 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5497 | } |
| 5498 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5499 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5500 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 5501 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5502 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 5503 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5504 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5505 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 5506 | " out of %d devices, algorithm %d\n", |
| 5507 | mdname(mddev), conf->level, |
| 5508 | mddev->raid_disks - mddev->degraded, |
| 5509 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | |
| 5511 | print_raid5_conf(conf); |
| 5512 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5513 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5514 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5515 | atomic_set(&conf->reshape_stripes, 0); |
| 5516 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 5517 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 5518 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 5519 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 5520 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 5521 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5522 | } |
| 5523 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5524 | |
| 5525 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5526 | if (mddev->to_remove == &raid5_attrs_group) |
| 5527 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 5528 | else if (mddev->kobj.sd && |
| 5529 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5530 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5531 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 5532 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5533 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 5534 | |
| 5535 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5536 | int chunk_size; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5537 | bool discard_supported = true; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5538 | /* read-ahead size must cover two whole stripes, which |
| 5539 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 5540 | * number of raid devices |
| 5541 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 5543 | int stripe = data_disks * |
| 5544 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 5545 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 5546 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 5547 | |
| 5548 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5549 | |
| 5550 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 5551 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5552 | |
| 5553 | chunk_size = mddev->chunk_sectors << 9; |
| 5554 | blk_queue_io_min(mddev->queue, chunk_size); |
| 5555 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 5556 | (conf->raid_disks - conf->max_degraded)); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5557 | /* |
| 5558 | * We can only discard a whole stripe. It doesn't make sense to |
| 5559 | * discard data disk but write parity disk |
| 5560 | */ |
| 5561 | stripe = stripe * PAGE_SIZE; |
NeilBrown | 4ac6875 | 2012-11-19 13:11:26 +1100 | [diff] [blame] | 5562 | /* Round up to power of 2, as discard handling |
| 5563 | * currently assumes that */ |
| 5564 | while ((stripe-1) & stripe) |
| 5565 | stripe = (stripe | (stripe-1)) + 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5566 | mddev->queue->limits.discard_alignment = stripe; |
| 5567 | mddev->queue->limits.discard_granularity = stripe; |
| 5568 | /* |
| 5569 | * unaligned part of discard request will be ignored, so can't |
| 5570 | * guarantee discard_zerors_data |
| 5571 | */ |
| 5572 | mddev->queue->limits.discard_zeroes_data = 0; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5573 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5574 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5575 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5576 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5577 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 5578 | rdev->new_data_offset << 9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5579 | /* |
| 5580 | * discard_zeroes_data is required, otherwise data |
| 5581 | * could be lost. Consider a scenario: discard a stripe |
| 5582 | * (the stripe could be inconsistent if |
| 5583 | * discard_zeroes_data is 0); write one disk of the |
| 5584 | * stripe (the stripe could be inconsistent again |
| 5585 | * depending on which disks are used to calculate |
| 5586 | * parity); the disk is broken; The stripe data of this |
| 5587 | * disk is lost. |
| 5588 | */ |
| 5589 | if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || |
| 5590 | !bdev_get_queue(rdev->bdev)-> |
| 5591 | limits.discard_zeroes_data) |
| 5592 | discard_supported = false; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5593 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5594 | |
| 5595 | if (discard_supported && |
| 5596 | mddev->queue->limits.max_discard_sectors >= stripe && |
| 5597 | mddev->queue->limits.discard_granularity >= stripe) |
| 5598 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 5599 | mddev->queue); |
| 5600 | else |
| 5601 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, |
| 5602 | mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | } |
| 5604 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5605 | return 0; |
| 5606 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5607 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 5608 | print_raid5_conf(conf); |
| 5609 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5610 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5611 | 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] | 5612 | return -EIO; |
| 5613 | } |
| 5614 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5615 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5616 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5617 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5618 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 5619 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 5620 | if (mddev->queue) |
| 5621 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5622 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 5623 | mddev->private = NULL; |
| 5624 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | return 0; |
| 5626 | } |
| 5627 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5628 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5629 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5630 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5631 | int i; |
| 5632 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5633 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 5634 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 5635 | 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] | 5636 | for (i = 0; i < conf->raid_disks; i++) |
| 5637 | seq_printf (seq, "%s", |
| 5638 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5639 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5640 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5641 | } |
| 5642 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5643 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5644 | { |
| 5645 | int i; |
| 5646 | struct disk_info *tmp; |
| 5647 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5648 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5649 | if (!conf) { |
| 5650 | printk("(conf==NULL)\n"); |
| 5651 | return; |
| 5652 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5653 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 5654 | conf->raid_disks, |
| 5655 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5656 | |
| 5657 | for (i = 0; i < conf->raid_disks; i++) { |
| 5658 | char b[BDEVNAME_SIZE]; |
| 5659 | tmp = conf->disks + i; |
| 5660 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5661 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 5662 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 5663 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5664 | } |
| 5665 | } |
| 5666 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5667 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5668 | { |
| 5669 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5670 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5671 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5672 | int count = 0; |
| 5673 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5674 | |
| 5675 | for (i = 0; i < conf->raid_disks; i++) { |
| 5676 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5677 | if (tmp->replacement |
| 5678 | && tmp->replacement->recovery_offset == MaxSector |
| 5679 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 5680 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 5681 | /* Replacement has just become active. */ |
| 5682 | if (!tmp->rdev |
| 5683 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 5684 | count++; |
| 5685 | if (tmp->rdev) { |
| 5686 | /* Replaced device not technically faulty, |
| 5687 | * but we need to be sure it gets removed |
| 5688 | * and never re-added. |
| 5689 | */ |
| 5690 | set_bit(Faulty, &tmp->rdev->flags); |
| 5691 | sysfs_notify_dirent_safe( |
| 5692 | tmp->rdev->sysfs_state); |
| 5693 | } |
| 5694 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 5695 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 5696 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5697 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5698 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5699 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 5700 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5701 | } |
| 5702 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5703 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 5704 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5705 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 5707 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5708 | } |
| 5709 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5710 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5711 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5712 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5713 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 5714 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5715 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5716 | struct disk_info *p = conf->disks + number; |
| 5717 | |
| 5718 | print_raid5_conf(conf); |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5719 | if (rdev == p->rdev) |
| 5720 | rdevp = &p->rdev; |
| 5721 | else if (rdev == p->replacement) |
| 5722 | rdevp = &p->replacement; |
| 5723 | else |
| 5724 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5725 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5726 | if (number >= conf->raid_disks && |
| 5727 | conf->reshape_progress == MaxSector) |
| 5728 | clear_bit(In_sync, &rdev->flags); |
| 5729 | |
| 5730 | if (test_bit(In_sync, &rdev->flags) || |
| 5731 | atomic_read(&rdev->nr_pending)) { |
| 5732 | err = -EBUSY; |
| 5733 | goto abort; |
| 5734 | } |
| 5735 | /* Only remove non-faulty devices if recovery |
| 5736 | * isn't possible. |
| 5737 | */ |
| 5738 | if (!test_bit(Faulty, &rdev->flags) && |
| 5739 | mddev->recovery_disabled != conf->recovery_disabled && |
| 5740 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5741 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5742 | number < conf->raid_disks) { |
| 5743 | err = -EBUSY; |
| 5744 | goto abort; |
| 5745 | } |
| 5746 | *rdevp = NULL; |
| 5747 | synchronize_rcu(); |
| 5748 | if (atomic_read(&rdev->nr_pending)) { |
| 5749 | /* lost the race, try later */ |
| 5750 | err = -EBUSY; |
| 5751 | *rdevp = rdev; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5752 | } else if (p->replacement) { |
| 5753 | /* We must have just cleared 'rdev' */ |
| 5754 | p->rdev = p->replacement; |
| 5755 | clear_bit(Replacement, &p->replacement->flags); |
| 5756 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 5757 | * but will never see neither - if they are careful |
| 5758 | */ |
| 5759 | p->replacement = NULL; |
| 5760 | clear_bit(WantReplacement, &rdev->flags); |
| 5761 | } else |
| 5762 | /* We might have just removed the Replacement as faulty- |
| 5763 | * clear the bit just in case |
| 5764 | */ |
| 5765 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5766 | abort: |
| 5767 | |
| 5768 | print_raid5_conf(conf); |
| 5769 | return err; |
| 5770 | } |
| 5771 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5772 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5773 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5774 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5775 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5776 | int disk; |
| 5777 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5778 | int first = 0; |
| 5779 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5780 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5781 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 5782 | return -EBUSY; |
| 5783 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 5784 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5785 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5786 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5787 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5788 | if (rdev->raid_disk >= 0) |
| 5789 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5790 | |
| 5791 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5792 | * find the disk ... but prefer rdev->saved_raid_disk |
| 5793 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5794 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5795 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 5796 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5797 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5798 | first = rdev->saved_raid_disk; |
| 5799 | |
| 5800 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5801 | p = conf->disks + disk; |
| 5802 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 5803 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5804 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5805 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5806 | if (rdev->saved_raid_disk != disk) |
| 5807 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 5808 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5809 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5810 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5811 | } |
| 5812 | for (disk = first; disk <= last; disk++) { |
| 5813 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5814 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 5815 | p->replacement == NULL) { |
| 5816 | clear_bit(In_sync, &rdev->flags); |
| 5817 | set_bit(Replacement, &rdev->flags); |
| 5818 | rdev->raid_disk = disk; |
| 5819 | err = 0; |
| 5820 | conf->fullsync = 1; |
| 5821 | rcu_assign_pointer(p->replacement, rdev); |
| 5822 | break; |
| 5823 | } |
| 5824 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 5825 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5826 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 5827 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5828 | } |
| 5829 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5830 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5831 | { |
| 5832 | /* no resync is happening, and there is enough space |
| 5833 | * on all devices, so we can resize. |
| 5834 | * We need to make sure resync covers any new space. |
| 5835 | * If the array is shrinking we should possibly wait until |
| 5836 | * any io in the removed space completes, but it hardly seems |
| 5837 | * worth it. |
| 5838 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5839 | sector_t newsize; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5840 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5841 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 5842 | if (mddev->external_size && |
| 5843 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5844 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 5845 | if (mddev->bitmap) { |
| 5846 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 5847 | if (ret) |
| 5848 | return ret; |
| 5849 | } |
| 5850 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 5851 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5852 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 5853 | if (sectors > mddev->dev_sectors && |
| 5854 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5855 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5856 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 5857 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5858 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 5859 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5860 | return 0; |
| 5861 | } |
| 5862 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5863 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5864 | { |
| 5865 | /* Can only proceed if there are plenty of stripe_heads. |
| 5866 | * We need a minimum of one full stripe,, and for sensible progress |
| 5867 | * it is best to have about 4 times that. |
| 5868 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 5869 | * allow for chunk sizes up to 256K, which is probably OK. |
| 5870 | * If the chunk size is greater, user-space should request more |
| 5871 | * stripe_heads first. |
| 5872 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5873 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5874 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5875 | > conf->max_nr_stripes || |
| 5876 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 5877 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5878 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 5879 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5880 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 5881 | / STRIPE_SIZE)*4); |
| 5882 | return 0; |
| 5883 | } |
| 5884 | return 1; |
| 5885 | } |
| 5886 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5887 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5888 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5889 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5890 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5891 | if (mddev->delta_disks == 0 && |
| 5892 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5893 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 5894 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 5895 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5896 | return -EINVAL; |
| 5897 | if (mddev->delta_disks < 0) { |
| 5898 | /* We might be able to shrink, but the devices must |
| 5899 | * be made bigger first. |
| 5900 | * For raid6, 4 is the minimum size. |
| 5901 | * Otherwise 2 is the minimum |
| 5902 | */ |
| 5903 | int min = 2; |
| 5904 | if (mddev->level == 6) |
| 5905 | min = 4; |
| 5906 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 5907 | return -EINVAL; |
| 5908 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5909 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5910 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5911 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5912 | |
NeilBrown | e56108d6 | 2012-10-11 14:24:13 +1100 | [diff] [blame] | 5913 | return resize_stripes(conf, (conf->previous_raid_disks |
| 5914 | + mddev->delta_disks)); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5915 | } |
| 5916 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5917 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5918 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5919 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5920 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5921 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 5922 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5923 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5924 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5925 | return -EBUSY; |
| 5926 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 5927 | if (!check_stripe_cache(mddev)) |
| 5928 | return -ENOSPC; |
| 5929 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 5930 | if (has_failed(conf)) |
| 5931 | return -EINVAL; |
| 5932 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5933 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5934 | if (!test_bit(In_sync, &rdev->flags) |
| 5935 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5936 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5937 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5938 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5939 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5940 | /* Not enough devices even to make a degraded array |
| 5941 | * of that size |
| 5942 | */ |
| 5943 | return -EINVAL; |
| 5944 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5945 | /* Refuse to reduce size of the array. Any reductions in |
| 5946 | * array size must be through explicit setting of array_size |
| 5947 | * attribute. |
| 5948 | */ |
| 5949 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 5950 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5951 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5952 | "before number of disks\n", mdname(mddev)); |
| 5953 | return -EINVAL; |
| 5954 | } |
| 5955 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5956 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5957 | spin_lock_irq(&conf->device_lock); |
| 5958 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 5959 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5960 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 5961 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5962 | conf->prev_algo = conf->algorithm; |
| 5963 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5964 | conf->generation++; |
| 5965 | /* Code that selects data_offset needs to see the generation update |
| 5966 | * if reshape_progress has been set - so a memory barrier needed. |
| 5967 | */ |
| 5968 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5969 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5970 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 5971 | else |
| 5972 | conf->reshape_progress = 0; |
| 5973 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5974 | spin_unlock_irq(&conf->device_lock); |
| 5975 | |
| 5976 | /* Add some new drives, as many as will fit. |
| 5977 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 5978 | * Don't add devices if we are reducing the number of |
| 5979 | * devices in the array. This is because it is not possible |
| 5980 | * to correctly record the "partially reconstructed" state of |
| 5981 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5982 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5983 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5984 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5985 | if (rdev->raid_disk < 0 && |
| 5986 | !test_bit(Faulty, &rdev->flags)) { |
| 5987 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5988 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5989 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5990 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 5991 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5992 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5993 | |
| 5994 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5995 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5996 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 5997 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 5998 | && !test_bit(Faulty, &rdev->flags)) { |
| 5999 | /* This is a spare that was manually added */ |
| 6000 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6001 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6002 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6003 | /* When a reshape changes the number of devices, |
| 6004 | * ->degraded is measured against the larger of the |
| 6005 | * pre and post number of devices. |
| 6006 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6007 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6008 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6009 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 6010 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6011 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 6012 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6013 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6014 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6015 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6016 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 6017 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 6018 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6019 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6020 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6021 | if (!mddev->sync_thread) { |
| 6022 | mddev->recovery = 0; |
| 6023 | spin_lock_irq(&conf->device_lock); |
| 6024 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6025 | rdev_for_each(rdev, mddev) |
| 6026 | rdev->new_data_offset = rdev->data_offset; |
| 6027 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6028 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 6029 | mddev->reshape_position = MaxSector; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6030 | spin_unlock_irq(&conf->device_lock); |
| 6031 | return -EAGAIN; |
| 6032 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6033 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6034 | md_wakeup_thread(mddev->sync_thread); |
| 6035 | md_new_event(mddev); |
| 6036 | return 0; |
| 6037 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6038 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6039 | /* This is called from the reshape thread and should make any |
| 6040 | * changes needed in 'conf' |
| 6041 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6042 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6043 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6044 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6045 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6046 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6047 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6048 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6049 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6050 | rdev_for_each(rdev, conf->mddev) |
| 6051 | rdev->data_offset = rdev->new_data_offset; |
| 6052 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6053 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6054 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 6055 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6056 | |
| 6057 | /* read-ahead size must cover two whole stripes, which is |
| 6058 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 6059 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 6060 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6061 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6062 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6063 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6064 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 6065 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 6066 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6067 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6068 | } |
| 6069 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6070 | /* This is called from the raid5d thread with mddev_lock held. |
| 6071 | * It makes config changes to the device. |
| 6072 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6073 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6074 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6075 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6076 | |
| 6077 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6078 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6079 | if (mddev->delta_disks > 0) { |
| 6080 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 6081 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6082 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6083 | } else { |
| 6084 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6085 | spin_lock_irq(&conf->device_lock); |
| 6086 | mddev->degraded = calc_degraded(conf); |
| 6087 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6088 | for (d = conf->raid_disks ; |
| 6089 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 6090 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6091 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 6092 | if (rdev) |
| 6093 | clear_bit(In_sync, &rdev->flags); |
| 6094 | rdev = conf->disks[d].replacement; |
| 6095 | if (rdev) |
| 6096 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 6097 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6098 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6099 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6100 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6101 | mddev->reshape_position = MaxSector; |
| 6102 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6103 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6104 | } |
| 6105 | } |
| 6106 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6107 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6108 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6109 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6110 | |
| 6111 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 6112 | case 2: /* resume for a suspend */ |
| 6113 | wake_up(&conf->wait_for_overlap); |
| 6114 | break; |
| 6115 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6116 | case 1: /* stop all writes */ |
| 6117 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6118 | /* '2' tells resync/reshape to pause so that all |
| 6119 | * active stripes can drain |
| 6120 | */ |
| 6121 | conf->quiesce = 2; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6122 | wait_event_lock_irq(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6123 | atomic_read(&conf->active_stripes) == 0 && |
| 6124 | atomic_read(&conf->active_aligned_reads) == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 6125 | conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6126 | conf->quiesce = 1; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6127 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6128 | /* allow reshape to continue */ |
| 6129 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6130 | break; |
| 6131 | |
| 6132 | case 0: /* re-enable writes */ |
| 6133 | spin_lock_irq(&conf->device_lock); |
| 6134 | conf->quiesce = 0; |
| 6135 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 6136 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6137 | spin_unlock_irq(&conf->device_lock); |
| 6138 | break; |
| 6139 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6140 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 6141 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6142 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6143 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6144 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6145 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 6146 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6147 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6148 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6149 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6150 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 6151 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6152 | return ERR_PTR(-EINVAL); |
| 6153 | } |
| 6154 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6155 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 6156 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 6157 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6158 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6159 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6160 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 6161 | mddev->raid_disks += 1; |
| 6162 | mddev->delta_disks = 1; |
| 6163 | /* make sure it will be not marked as dirty */ |
| 6164 | mddev->recovery_cp = MaxSector; |
| 6165 | |
| 6166 | return setup_conf(mddev); |
| 6167 | } |
| 6168 | |
| 6169 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6170 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6171 | { |
| 6172 | int chunksect; |
| 6173 | |
| 6174 | if (mddev->raid_disks != 2 || |
| 6175 | mddev->degraded > 1) |
| 6176 | return ERR_PTR(-EINVAL); |
| 6177 | |
| 6178 | /* Should check if there are write-behind devices? */ |
| 6179 | |
| 6180 | chunksect = 64*2; /* 64K by default */ |
| 6181 | |
| 6182 | /* The array must be an exact multiple of chunksize */ |
| 6183 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 6184 | chunksect >>= 1; |
| 6185 | |
| 6186 | if ((chunksect<<9) < STRIPE_SIZE) |
| 6187 | /* array size does not allow a suitable chunk size */ |
| 6188 | return ERR_PTR(-EINVAL); |
| 6189 | |
| 6190 | mddev->new_level = 5; |
| 6191 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6192 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6193 | |
| 6194 | return setup_conf(mddev); |
| 6195 | } |
| 6196 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6197 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6198 | { |
| 6199 | int new_layout; |
| 6200 | |
| 6201 | switch (mddev->layout) { |
| 6202 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 6203 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 6204 | break; |
| 6205 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 6206 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 6207 | break; |
| 6208 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 6209 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 6210 | break; |
| 6211 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 6212 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 6213 | break; |
| 6214 | case ALGORITHM_PARITY_0_6: |
| 6215 | new_layout = ALGORITHM_PARITY_0; |
| 6216 | break; |
| 6217 | case ALGORITHM_PARITY_N: |
| 6218 | new_layout = ALGORITHM_PARITY_N; |
| 6219 | break; |
| 6220 | default: |
| 6221 | return ERR_PTR(-EINVAL); |
| 6222 | } |
| 6223 | mddev->new_level = 5; |
| 6224 | mddev->new_layout = new_layout; |
| 6225 | mddev->delta_disks = -1; |
| 6226 | mddev->raid_disks -= 1; |
| 6227 | return setup_conf(mddev); |
| 6228 | } |
| 6229 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6230 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6231 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6232 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6233 | /* For a 2-drive array, the layout and chunk size can be changed |
| 6234 | * immediately as not restriping is needed. |
| 6235 | * For larger arrays we record the new value - after validation |
| 6236 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6237 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6238 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6239 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6240 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6241 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6242 | return -EINVAL; |
| 6243 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6244 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6245 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6246 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6247 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6248 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6249 | /* not factor of array size */ |
| 6250 | return -EINVAL; |
| 6251 | } |
| 6252 | |
| 6253 | /* They look valid */ |
| 6254 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6255 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6256 | /* can make the change immediately */ |
| 6257 | if (mddev->new_layout >= 0) { |
| 6258 | conf->algorithm = mddev->new_layout; |
| 6259 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6260 | } |
| 6261 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6262 | conf->chunk_sectors = new_chunk ; |
| 6263 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6264 | } |
| 6265 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 6266 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6267 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6268 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6269 | } |
| 6270 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6271 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6272 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6273 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6274 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6275 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6276 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6277 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6278 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6279 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6280 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6281 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6282 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6283 | /* not factor of array size */ |
| 6284 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6285 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6286 | |
| 6287 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6288 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6289 | } |
| 6290 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6291 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6292 | { |
| 6293 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6294 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6295 | * raid1 - if there are two drives. We need to know the chunk size |
| 6296 | * raid4 - trivial - just use a raid4 layout. |
| 6297 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6298 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6299 | if (mddev->level == 0) |
| 6300 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6301 | if (mddev->level == 1) |
| 6302 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 6303 | if (mddev->level == 4) { |
| 6304 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6305 | mddev->new_level = 5; |
| 6306 | return setup_conf(mddev); |
| 6307 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6308 | if (mddev->level == 6) |
| 6309 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6310 | |
| 6311 | return ERR_PTR(-EINVAL); |
| 6312 | } |
| 6313 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6314 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6315 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6316 | /* raid4 can take over: |
| 6317 | * raid0 - if there is only one strip zone |
| 6318 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6319 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6320 | if (mddev->level == 0) |
| 6321 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6322 | if (mddev->level == 5 && |
| 6323 | mddev->layout == ALGORITHM_PARITY_N) { |
| 6324 | mddev->new_layout = 0; |
| 6325 | mddev->new_level = 4; |
| 6326 | return setup_conf(mddev); |
| 6327 | } |
| 6328 | return ERR_PTR(-EINVAL); |
| 6329 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6330 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6331 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6332 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6333 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6334 | { |
| 6335 | /* Currently can only take over a raid5. We map the |
| 6336 | * personality to an equivalent raid6 personality |
| 6337 | * with the Q block at the end. |
| 6338 | */ |
| 6339 | int new_layout; |
| 6340 | |
| 6341 | if (mddev->pers != &raid5_personality) |
| 6342 | return ERR_PTR(-EINVAL); |
| 6343 | if (mddev->degraded > 1) |
| 6344 | return ERR_PTR(-EINVAL); |
| 6345 | if (mddev->raid_disks > 253) |
| 6346 | return ERR_PTR(-EINVAL); |
| 6347 | if (mddev->raid_disks < 3) |
| 6348 | return ERR_PTR(-EINVAL); |
| 6349 | |
| 6350 | switch (mddev->layout) { |
| 6351 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 6352 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 6353 | break; |
| 6354 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 6355 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 6356 | break; |
| 6357 | case ALGORITHM_LEFT_SYMMETRIC: |
| 6358 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 6359 | break; |
| 6360 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 6361 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 6362 | break; |
| 6363 | case ALGORITHM_PARITY_0: |
| 6364 | new_layout = ALGORITHM_PARITY_0_6; |
| 6365 | break; |
| 6366 | case ALGORITHM_PARITY_N: |
| 6367 | new_layout = ALGORITHM_PARITY_N; |
| 6368 | break; |
| 6369 | default: |
| 6370 | return ERR_PTR(-EINVAL); |
| 6371 | } |
| 6372 | mddev->new_level = 6; |
| 6373 | mddev->new_layout = new_layout; |
| 6374 | mddev->delta_disks = 1; |
| 6375 | mddev->raid_disks += 1; |
| 6376 | return setup_conf(mddev); |
| 6377 | } |
| 6378 | |
| 6379 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6380 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6381 | { |
| 6382 | .name = "raid6", |
| 6383 | .level = 6, |
| 6384 | .owner = THIS_MODULE, |
| 6385 | .make_request = make_request, |
| 6386 | .run = run, |
| 6387 | .stop = stop, |
| 6388 | .status = status, |
| 6389 | .error_handler = error, |
| 6390 | .hot_add_disk = raid5_add_disk, |
| 6391 | .hot_remove_disk= raid5_remove_disk, |
| 6392 | .spare_active = raid5_spare_active, |
| 6393 | .sync_request = sync_request, |
| 6394 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6395 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6396 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6397 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6398 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6399 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6400 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6401 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6402 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6403 | { |
| 6404 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6405 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | 63c70c4 | 2006-03-27 01:18: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 | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6421 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6422 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6423 | }; |
| 6424 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 6425 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6426 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6427 | .name = "raid4", |
| 6428 | .level = 4, |
| 6429 | .owner = THIS_MODULE, |
| 6430 | .make_request = make_request, |
| 6431 | .run = run, |
| 6432 | .stop = stop, |
| 6433 | .status = status, |
| 6434 | .error_handler = error, |
| 6435 | .hot_add_disk = raid5_add_disk, |
| 6436 | .hot_remove_disk= raid5_remove_disk, |
| 6437 | .spare_active = raid5_spare_active, |
| 6438 | .sync_request = sync_request, |
| 6439 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6440 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 6441 | .check_reshape = raid5_check_reshape, |
| 6442 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6443 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6444 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6445 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6446 | }; |
| 6447 | |
| 6448 | static int __init raid5_init(void) |
| 6449 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6450 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6451 | register_md_personality(&raid5_personality); |
| 6452 | register_md_personality(&raid4_personality); |
| 6453 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6454 | } |
| 6455 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6456 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6458 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6459 | unregister_md_personality(&raid5_personality); |
| 6460 | unregister_md_personality(&raid4_personality); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6461 | } |
| 6462 | |
| 6463 | module_init(raid5_init); |
| 6464 | module_exit(raid5_exit); |
| 6465 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 6466 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 6468 | MODULE_ALIAS("md-raid5"); |
| 6469 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 6470 | MODULE_ALIAS("md-level-5"); |
| 6471 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6472 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 6473 | MODULE_ALIAS("md-raid6"); |
| 6474 | MODULE_ALIAS("md-level-6"); |
| 6475 | |
| 6476 | /* This used to be two separate modules, they were: */ |
| 6477 | MODULE_ALIAS("raid5"); |
| 6478 | MODULE_ALIAS("raid6"); |