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> |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 56 | #include <linux/nodemask.h> |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 57 | #include <trace/events/block.h> |
| 58 | |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 59 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 60 | #include "raid5.h" |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 61 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 62 | #include "bitmap.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 63 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 64 | #define cpu_to_group(cpu) cpu_to_node(cpu) |
| 65 | #define ANY_GROUP NUMA_NO_NODE |
| 66 | |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 67 | static bool devices_handle_discard_safely = false; |
| 68 | module_param(devices_handle_discard_safely, bool, 0644); |
| 69 | MODULE_PARM_DESC(devices_handle_discard_safely, |
| 70 | "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions"); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 71 | static struct workqueue_struct *raid5_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 72 | /* |
| 73 | * Stripe cache |
| 74 | */ |
| 75 | |
| 76 | #define NR_STRIPES 256 |
| 77 | #define STRIPE_SIZE PAGE_SIZE |
| 78 | #define STRIPE_SHIFT (PAGE_SHIFT - 9) |
| 79 | #define STRIPE_SECTORS (STRIPE_SIZE>>9) |
| 80 | #define IO_THRESHOLD 1 |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 81 | #define BYPASS_THRESHOLD 1 |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 82 | #define NR_HASH (PAGE_SIZE / sizeof(struct hlist_head)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | #define HASH_MASK (NR_HASH - 1) |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 84 | #define MAX_STRIPE_BATCH 8 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 86 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 87 | { |
| 88 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; |
| 89 | return &conf->stripe_hashtbl[hash]; |
| 90 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 92 | static inline int stripe_hash_locks_hash(sector_t sect) |
| 93 | { |
| 94 | return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK; |
| 95 | } |
| 96 | |
| 97 | static inline void lock_device_hash_lock(struct r5conf *conf, int hash) |
| 98 | { |
| 99 | spin_lock_irq(conf->hash_locks + hash); |
| 100 | spin_lock(&conf->device_lock); |
| 101 | } |
| 102 | |
| 103 | static inline void unlock_device_hash_lock(struct r5conf *conf, int hash) |
| 104 | { |
| 105 | spin_unlock(&conf->device_lock); |
| 106 | spin_unlock_irq(conf->hash_locks + hash); |
| 107 | } |
| 108 | |
| 109 | static inline void lock_all_device_hash_locks_irq(struct r5conf *conf) |
| 110 | { |
| 111 | int i; |
| 112 | local_irq_disable(); |
| 113 | spin_lock(conf->hash_locks); |
| 114 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 115 | spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks); |
| 116 | spin_lock(&conf->device_lock); |
| 117 | } |
| 118 | |
| 119 | static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf) |
| 120 | { |
| 121 | int i; |
| 122 | spin_unlock(&conf->device_lock); |
| 123 | for (i = NR_STRIPE_HASH_LOCKS; i; i--) |
| 124 | spin_unlock(conf->hash_locks + i - 1); |
| 125 | local_irq_enable(); |
| 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /* bio's attached to a stripe+device for I/O are linked together in bi_sector |
| 129 | * order without overlap. There may be several bio's per stripe+device, and |
| 130 | * a bio could span several devices. |
| 131 | * When walking this list for a particular stripe+device, we must never proceed |
| 132 | * beyond a bio that extends past this device, as the next bio might no longer |
| 133 | * be valid. |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 134 | * 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] | 135 | * of the current stripe+device |
| 136 | */ |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 137 | static inline struct bio *r5_next_bio(struct bio *bio, sector_t sector) |
| 138 | { |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 139 | int sectors = bio_sectors(bio); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 140 | if (bio->bi_iter.bi_sector + sectors < sector + STRIPE_SECTORS) |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 141 | return bio->bi_next; |
| 142 | else |
| 143 | return NULL; |
| 144 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 146 | /* |
Jens Axboe | 5b99c2f | 2008-08-15 10:56:11 +0200 | [diff] [blame] | 147 | * We maintain a biased count of active stripes in the bottom 16 bits of |
| 148 | * 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] | 149 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 150 | static inline int raid5_bi_processed_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 151 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 152 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 153 | return (atomic_read(segments) >> 16) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 154 | } |
| 155 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 156 | static inline int raid5_dec_bi_active_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 157 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 158 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 159 | return atomic_sub_return(1, segments) & 0xffff; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 160 | } |
| 161 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 162 | static inline void raid5_inc_bi_active_stripes(struct bio *bio) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 163 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 164 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 165 | atomic_inc(segments); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 166 | } |
| 167 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 168 | static inline void raid5_set_bi_processed_stripes(struct bio *bio, |
| 169 | unsigned int cnt) |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 170 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 171 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 172 | int old, new; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 173 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 174 | do { |
| 175 | old = atomic_read(segments); |
| 176 | new = (old & 0xffff) | (cnt << 16); |
| 177 | } while (atomic_cmpxchg(segments, old, new) != old); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 178 | } |
| 179 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 180 | 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] | 181 | { |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 182 | atomic_t *segments = (atomic_t *)&bio->bi_phys_segments; |
| 183 | atomic_set(segments, cnt); |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 184 | } |
| 185 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 186 | /* Find first data disk in a raid6 stripe */ |
| 187 | static inline int raid6_d0(struct stripe_head *sh) |
| 188 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 189 | if (sh->ddf_layout) |
| 190 | /* ddf always start from first device */ |
| 191 | return 0; |
| 192 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 193 | if (sh->qd_idx == sh->disks - 1) |
| 194 | return 0; |
| 195 | else |
| 196 | return sh->qd_idx + 1; |
| 197 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 198 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 199 | { |
| 200 | disk++; |
| 201 | return (disk < raid_disks) ? disk : 0; |
| 202 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 203 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 204 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 205 | * We need to map each disk to a 'slot', where the data disks are slot |
| 206 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 207 | * is raid_disks-1. This help does that mapping. |
| 208 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 209 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 210 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 211 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 212 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 213 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 214 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 215 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 216 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 217 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 218 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 219 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 220 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 221 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 222 | return slot; |
| 223 | } |
| 224 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 225 | static void return_io(struct bio *return_bi) |
| 226 | { |
| 227 | struct bio *bi = return_bi; |
| 228 | while (bi) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 229 | |
| 230 | return_bi = bi->bi_next; |
| 231 | bi->bi_next = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 232 | bi->bi_iter.bi_size = 0; |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 233 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 234 | bi, 0); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 235 | bio_endio(bi, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 236 | bi = return_bi; |
| 237 | } |
| 238 | } |
| 239 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 240 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 241 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 242 | static int stripe_operations_active(struct stripe_head *sh) |
| 243 | { |
| 244 | return sh->check_state || sh->reconstruct_state || |
| 245 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 246 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 247 | } |
| 248 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 249 | static void raid5_wakeup_stripe_thread(struct stripe_head *sh) |
| 250 | { |
| 251 | struct r5conf *conf = sh->raid_conf; |
| 252 | struct r5worker_group *group; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 253 | int thread_cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 254 | int i, cpu = sh->cpu; |
| 255 | |
| 256 | if (!cpu_online(cpu)) { |
| 257 | cpu = cpumask_any(cpu_online_mask); |
| 258 | sh->cpu = cpu; |
| 259 | } |
| 260 | |
| 261 | if (list_empty(&sh->lru)) { |
| 262 | struct r5worker_group *group; |
| 263 | group = conf->worker_groups + cpu_to_group(cpu); |
| 264 | list_add_tail(&sh->lru, &group->handle_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 265 | group->stripes_cnt++; |
| 266 | sh->group = group; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 267 | } |
| 268 | |
| 269 | if (conf->worker_cnt_per_group == 0) { |
| 270 | md_wakeup_thread(conf->mddev->thread); |
| 271 | return; |
| 272 | } |
| 273 | |
| 274 | group = conf->worker_groups + cpu_to_group(sh->cpu); |
| 275 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 276 | group->workers[0].working = true; |
| 277 | /* at least one worker should run to avoid race */ |
| 278 | queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work); |
| 279 | |
| 280 | thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1; |
| 281 | /* wakeup more workers */ |
| 282 | for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) { |
| 283 | if (group->workers[i].working == false) { |
| 284 | group->workers[i].working = true; |
| 285 | queue_work_on(sh->cpu, raid5_wq, |
| 286 | &group->workers[i].work); |
| 287 | thread_cnt--; |
| 288 | } |
| 289 | } |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 290 | } |
| 291 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 292 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 293 | struct list_head *temp_inactive_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | { |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 295 | BUG_ON(!list_empty(&sh->lru)); |
| 296 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
| 297 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 298 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 299 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) { |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 300 | list_add_tail(&sh->lru, &conf->delayed_list); |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 301 | if (atomic_read(&conf->preread_active_stripes) |
| 302 | < IO_THRESHOLD) |
| 303 | md_wakeup_thread(conf->mddev->thread); |
| 304 | } else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 305 | sh->bm_seq - conf->seq_write > 0) |
| 306 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 307 | else { |
| 308 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 309 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 310 | if (conf->worker_cnt_per_group == 0) { |
| 311 | list_add_tail(&sh->lru, &conf->handle_list); |
| 312 | } else { |
| 313 | raid5_wakeup_stripe_thread(sh); |
| 314 | return; |
| 315 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 316 | } |
| 317 | md_wakeup_thread(conf->mddev->thread); |
| 318 | } else { |
| 319 | BUG_ON(stripe_operations_active(sh)); |
| 320 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 321 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 322 | < IO_THRESHOLD) |
| 323 | md_wakeup_thread(conf->mddev->thread); |
| 324 | atomic_dec(&conf->active_stripes); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 325 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) |
| 326 | list_add_tail(&sh->lru, temp_inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 327 | } |
| 328 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 329 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 330 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 331 | struct list_head *temp_inactive_list) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 332 | { |
| 333 | if (atomic_dec_and_test(&sh->count)) |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 334 | do_release_stripe(conf, sh, temp_inactive_list); |
| 335 | } |
| 336 | |
| 337 | /* |
| 338 | * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list |
| 339 | * |
| 340 | * Be careful: Only one task can add/delete stripes from temp_inactive_list at |
| 341 | * given time. Adding stripes only takes device lock, while deleting stripes |
| 342 | * only takes hash lock. |
| 343 | */ |
| 344 | static void release_inactive_stripe_list(struct r5conf *conf, |
| 345 | struct list_head *temp_inactive_list, |
| 346 | int hash) |
| 347 | { |
| 348 | int size; |
| 349 | bool do_wakeup = false; |
| 350 | unsigned long flags; |
| 351 | |
| 352 | if (hash == NR_STRIPE_HASH_LOCKS) { |
| 353 | size = NR_STRIPE_HASH_LOCKS; |
| 354 | hash = NR_STRIPE_HASH_LOCKS - 1; |
| 355 | } else |
| 356 | size = 1; |
| 357 | while (size) { |
| 358 | struct list_head *list = &temp_inactive_list[size - 1]; |
| 359 | |
| 360 | /* |
| 361 | * We don't hold any lock here yet, get_active_stripe() might |
| 362 | * remove stripes from the list |
| 363 | */ |
| 364 | if (!list_empty_careful(list)) { |
| 365 | spin_lock_irqsave(conf->hash_locks + hash, flags); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 366 | if (list_empty(conf->inactive_list + hash) && |
| 367 | !list_empty(list)) |
| 368 | atomic_dec(&conf->empty_inactive_list_nr); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 369 | list_splice_tail_init(list, conf->inactive_list + hash); |
| 370 | do_wakeup = true; |
| 371 | spin_unlock_irqrestore(conf->hash_locks + hash, flags); |
| 372 | } |
| 373 | size--; |
| 374 | hash--; |
| 375 | } |
| 376 | |
| 377 | if (do_wakeup) { |
| 378 | wake_up(&conf->wait_for_stripe); |
| 379 | if (conf->retry_read_aligned) |
| 380 | md_wakeup_thread(conf->mddev->thread); |
| 381 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 382 | } |
| 383 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 384 | /* should hold conf->device_lock already */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 385 | static int release_stripe_list(struct r5conf *conf, |
| 386 | struct list_head *temp_inactive_list) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 387 | { |
| 388 | struct stripe_head *sh; |
| 389 | int count = 0; |
| 390 | struct llist_node *head; |
| 391 | |
| 392 | head = llist_del_all(&conf->released_stripes); |
Shaohua Li | d265d9d | 2013-08-28 14:29:05 +0800 | [diff] [blame] | 393 | head = llist_reverse_order(head); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 394 | while (head) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 395 | int hash; |
| 396 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 397 | sh = llist_entry(head, struct stripe_head, release_list); |
| 398 | head = llist_next(head); |
| 399 | /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */ |
| 400 | smp_mb(); |
| 401 | clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state); |
| 402 | /* |
| 403 | * Don't worry the bit is set here, because if the bit is set |
| 404 | * again, the count is always > 1. This is true for |
| 405 | * STRIPE_ON_UNPLUG_LIST bit too. |
| 406 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 407 | hash = sh->hash_lock_index; |
| 408 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 409 | count++; |
| 410 | } |
| 411 | |
| 412 | return count; |
| 413 | } |
| 414 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | static void release_stripe(struct stripe_head *sh) |
| 416 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 417 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | unsigned long flags; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 419 | struct list_head list; |
| 420 | int hash; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 421 | bool wakeup; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 422 | |
Eivind Sarto | cf170f3 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 423 | /* Avoid release_list until the last reference. |
| 424 | */ |
| 425 | if (atomic_add_unless(&sh->count, -1, 1)) |
| 426 | return; |
| 427 | |
majianpeng | ad4068d | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 428 | if (unlikely(!conf->mddev->thread) || |
| 429 | test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state)) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 430 | goto slow_path; |
| 431 | wakeup = llist_add(&sh->release_list, &conf->released_stripes); |
| 432 | if (wakeup) |
| 433 | md_wakeup_thread(conf->mddev->thread); |
| 434 | return; |
| 435 | slow_path: |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 436 | local_irq_save(flags); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 437 | /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */ |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 438 | if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 439 | INIT_LIST_HEAD(&list); |
| 440 | hash = sh->hash_lock_index; |
| 441 | do_release_stripe(conf, sh, &list); |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 442 | spin_unlock(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 443 | release_inactive_stripe_list(conf, &list, hash); |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 444 | } |
| 445 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 446 | } |
| 447 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 448 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 450 | pr_debug("remove_hash(), stripe %llu\n", |
| 451 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 453 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 454 | } |
| 455 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 456 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 458 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 460 | pr_debug("insert_hash(), stripe %llu\n", |
| 461 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 463 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | |
| 467 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 468 | static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | struct stripe_head *sh = NULL; |
| 471 | struct list_head *first; |
| 472 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 473 | if (list_empty(conf->inactive_list + hash)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | goto out; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 475 | first = (conf->inactive_list + hash)->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 476 | sh = list_entry(first, struct stripe_head, lru); |
| 477 | list_del_init(first); |
| 478 | remove_hash(sh); |
| 479 | atomic_inc(&conf->active_stripes); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 480 | BUG_ON(hash != sh->hash_lock_index); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 481 | if (list_empty(conf->inactive_list + hash)) |
| 482 | atomic_inc(&conf->empty_inactive_list_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | out: |
| 484 | return sh; |
| 485 | } |
| 486 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 487 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | { |
| 489 | struct page *p; |
| 490 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 491 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 492 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 493 | for (i = 0; i < num ; i++) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 494 | WARN_ON(sh->dev[i].page != sh->dev[i].orig_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 495 | p = sh->dev[i].page; |
| 496 | if (!p) |
| 497 | continue; |
| 498 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 499 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 503 | static int grow_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 504 | { |
| 505 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 506 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 508 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 509 | struct page *page; |
| 510 | |
| 511 | if (!(page = alloc_page(GFP_KERNEL))) { |
| 512 | return 1; |
| 513 | } |
| 514 | sh->dev[i].page = page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 515 | sh->dev[i].orig_page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 516 | } |
| 517 | return 0; |
| 518 | } |
| 519 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 520 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 521 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 522 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 523 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 524 | 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] | 525 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 526 | struct r5conf *conf = sh->raid_conf; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 527 | int i, seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 528 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 529 | BUG_ON(atomic_read(&sh->count) != 0); |
| 530 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 531 | BUG_ON(stripe_operations_active(sh)); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 532 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 533 | pr_debug("init_stripe called, stripe %llu\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | (unsigned long long)sh->sector); |
| 535 | |
| 536 | remove_hash(sh); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 537 | retry: |
| 538 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 539 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 540 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 542 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | sh->state = 0; |
| 544 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 545 | |
| 546 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 547 | struct r5dev *dev = &sh->dev[i]; |
| 548 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 549 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 550 | test_bit(R5_LOCKED, &dev->flags)) { |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 551 | 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] | 552 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 553 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 554 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 555 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 556 | } |
| 557 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 558 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 560 | if (read_seqcount_retry(&conf->gen_lock, seq)) |
| 561 | goto retry; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 562 | insert_hash(conf, sh); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 563 | sh->cpu = smp_processor_id(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | } |
| 565 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 566 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 567 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 568 | { |
| 569 | struct stripe_head *sh; |
| 570 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 571 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 572 | hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 573 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 574 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 575 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 576 | return NULL; |
| 577 | } |
| 578 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 579 | /* |
| 580 | * Need to check if array has failed when deciding whether to: |
| 581 | * - start an array |
| 582 | * - remove non-faulty devices |
| 583 | * - add a spare |
| 584 | * - allow a reshape |
| 585 | * This determination is simple when no reshape is happening. |
| 586 | * However if there is a reshape, we need to carefully check |
| 587 | * both the before and after sections. |
| 588 | * This is because some failed devices may only affect one |
| 589 | * of the two sections, and some non-in_sync devices may |
| 590 | * be insync in the section most affected by failed devices. |
| 591 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 592 | static int calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 593 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 594 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 595 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 596 | |
| 597 | rcu_read_lock(); |
| 598 | degraded = 0; |
| 599 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 600 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 601 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 602 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 603 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 604 | degraded++; |
| 605 | else if (test_bit(In_sync, &rdev->flags)) |
| 606 | ; |
| 607 | else |
| 608 | /* not in-sync or faulty. |
| 609 | * If the reshape increases the number of devices, |
| 610 | * this is being recovered by the reshape, so |
| 611 | * this 'previous' section is not in_sync. |
| 612 | * If the number of devices is being reduced however, |
| 613 | * the device can only be part of the array if |
| 614 | * we are reverting a reshape, so this section will |
| 615 | * be in-sync. |
| 616 | */ |
| 617 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 618 | degraded++; |
| 619 | } |
| 620 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 621 | if (conf->raid_disks == conf->previous_raid_disks) |
| 622 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 623 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 624 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 625 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 626 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 627 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 628 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 629 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 630 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 631 | else if (test_bit(In_sync, &rdev->flags)) |
| 632 | ; |
| 633 | else |
| 634 | /* not in-sync or faulty. |
| 635 | * If reshape increases the number of devices, this |
| 636 | * section has already been recovered, else it |
| 637 | * almost certainly hasn't. |
| 638 | */ |
| 639 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 640 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 641 | } |
| 642 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 643 | if (degraded2 > degraded) |
| 644 | return degraded2; |
| 645 | return degraded; |
| 646 | } |
| 647 | |
| 648 | static int has_failed(struct r5conf *conf) |
| 649 | { |
| 650 | int degraded; |
| 651 | |
| 652 | if (conf->mddev->reshape_position == MaxSector) |
| 653 | return conf->mddev->degraded > conf->max_degraded; |
| 654 | |
| 655 | degraded = calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 656 | if (degraded > conf->max_degraded) |
| 657 | return 1; |
| 658 | return 0; |
| 659 | } |
| 660 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 661 | static struct stripe_head * |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 662 | get_active_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 663 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 664 | { |
| 665 | struct stripe_head *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 666 | int hash = stripe_hash_locks_hash(sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 667 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 668 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 669 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 670 | spin_lock_irq(conf->hash_locks + hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | |
| 672 | do { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 673 | wait_event_lock_irq(conf->wait_for_stripe, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 674 | conf->quiesce == 0 || noquiesce, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 675 | *(conf->hash_locks + hash)); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 676 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 677 | if (!sh) { |
| 678 | if (!conf->inactive_blocked) |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 679 | sh = get_free_stripe(conf, hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 680 | if (noblock && sh == NULL) |
| 681 | break; |
| 682 | if (!sh) { |
| 683 | conf->inactive_blocked = 1; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 684 | wait_event_lock_irq( |
| 685 | conf->wait_for_stripe, |
| 686 | !list_empty(conf->inactive_list + hash) && |
| 687 | (atomic_read(&conf->active_stripes) |
| 688 | < (conf->max_nr_stripes * 3 / 4) |
| 689 | || !conf->inactive_blocked), |
| 690 | *(conf->hash_locks + hash)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 691 | conf->inactive_blocked = 0; |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 692 | } else { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 693 | init_stripe(sh, sector, previous); |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 694 | atomic_inc(&sh->count); |
| 695 | } |
Shaohua Li | e240c18 | 2014-04-09 11:27:42 +0800 | [diff] [blame] | 696 | } else if (!atomic_inc_not_zero(&sh->count)) { |
NeilBrown | 6d183de | 2013-11-28 10:55:27 +1100 | [diff] [blame] | 697 | spin_lock(&conf->device_lock); |
Shaohua Li | e240c18 | 2014-04-09 11:27:42 +0800 | [diff] [blame] | 698 | if (!atomic_read(&sh->count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 699 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 700 | atomic_inc(&conf->active_stripes); |
NeilBrown | 5af9bef | 2014-01-14 15:16:10 +1100 | [diff] [blame] | 701 | BUG_ON(list_empty(&sh->lru) && |
| 702 | !test_bit(STRIPE_EXPANDING, &sh->state)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 703 | list_del_init(&sh->lru); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 704 | if (sh->group) { |
| 705 | sh->group->stripes_cnt--; |
| 706 | sh->group = NULL; |
| 707 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 708 | } |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 709 | atomic_inc(&sh->count); |
NeilBrown | 6d183de | 2013-11-28 10:55:27 +1100 | [diff] [blame] | 710 | spin_unlock(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | } |
| 712 | } while (sh == NULL); |
| 713 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 714 | spin_unlock_irq(conf->hash_locks + hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | return sh; |
| 716 | } |
| 717 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 718 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 719 | * in this stripe_head. |
| 720 | */ |
| 721 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 722 | { |
| 723 | sector_t progress = conf->reshape_progress; |
| 724 | /* Need a memory barrier to make sure we see the value |
| 725 | * of conf->generation, or ->data_offset that was set before |
| 726 | * reshape_progress was updated. |
| 727 | */ |
| 728 | smp_rmb(); |
| 729 | if (progress == MaxSector) |
| 730 | return 0; |
| 731 | if (sh->generation == conf->generation - 1) |
| 732 | return 0; |
| 733 | /* We are in a reshape, and this is a new-generation stripe, |
| 734 | * so use new_data_offset. |
| 735 | */ |
| 736 | return 1; |
| 737 | } |
| 738 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 739 | static void |
| 740 | raid5_end_read_request(struct bio *bi, int error); |
| 741 | static void |
| 742 | raid5_end_write_request(struct bio *bi, int error); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 743 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 744 | 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] | 745 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 746 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 747 | int i, disks = sh->disks; |
| 748 | |
| 749 | might_sleep(); |
| 750 | |
| 751 | for (i = disks; i--; ) { |
| 752 | int rw; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 753 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 754 | struct bio *bi, *rbi; |
| 755 | struct md_rdev *rdev, *rrdev = NULL; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 756 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
| 757 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
| 758 | rw = WRITE_FUA; |
| 759 | else |
| 760 | rw = WRITE; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 761 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 762 | rw |= REQ_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 763 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 764 | rw = READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 765 | else if (test_and_clear_bit(R5_WantReplace, |
| 766 | &sh->dev[i].flags)) { |
| 767 | rw = WRITE; |
| 768 | replace_only = 1; |
| 769 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 770 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 771 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
| 772 | rw |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 773 | |
| 774 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 775 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 776 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 777 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 778 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 779 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 780 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 781 | if (!rdev) { |
| 782 | rdev = rrdev; |
| 783 | rrdev = NULL; |
| 784 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 785 | if (rw & WRITE) { |
| 786 | if (replace_only) |
| 787 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 788 | if (rdev == rrdev) |
| 789 | /* We raced and saw duplicates */ |
| 790 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 791 | } else { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 792 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 793 | rdev = rrdev; |
| 794 | rrdev = NULL; |
| 795 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 796 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 797 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 798 | rdev = NULL; |
| 799 | if (rdev) |
| 800 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 801 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 802 | rrdev = NULL; |
| 803 | if (rrdev) |
| 804 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 805 | rcu_read_unlock(); |
| 806 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 807 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 808 | * need to check for writes. We never accept write errors |
| 809 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 810 | */ |
| 811 | while ((rw & WRITE) && rdev && |
| 812 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 813 | sector_t first_bad; |
| 814 | int bad_sectors; |
| 815 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 816 | &first_bad, &bad_sectors); |
| 817 | if (!bad) |
| 818 | break; |
| 819 | |
| 820 | if (bad < 0) { |
| 821 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 822 | if (!conf->mddev->external && |
| 823 | conf->mddev->flags) { |
| 824 | /* It is very unlikely, but we might |
| 825 | * still need to write out the |
| 826 | * bad block log - better give it |
| 827 | * a chance*/ |
| 828 | md_check_recovery(conf->mddev); |
| 829 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 830 | /* |
| 831 | * Because md_wait_for_blocked_rdev |
| 832 | * will dec nr_pending, we must |
| 833 | * increment it first. |
| 834 | */ |
| 835 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 836 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 837 | } else { |
| 838 | /* Acknowledged bad block - skip the write */ |
| 839 | rdev_dec_pending(rdev, conf->mddev); |
| 840 | rdev = NULL; |
| 841 | } |
| 842 | } |
| 843 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 844 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 845 | if (s->syncing || s->expanding || s->expanded |
| 846 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 847 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 848 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 849 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 850 | |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 851 | bio_reset(bi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 852 | bi->bi_bdev = rdev->bdev; |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 853 | bi->bi_rw = rw; |
| 854 | bi->bi_end_io = (rw & WRITE) |
| 855 | ? raid5_end_write_request |
| 856 | : raid5_end_read_request; |
| 857 | bi->bi_private = sh; |
| 858 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 859 | pr_debug("%s: for %llu schedule op %ld on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 860 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 861 | bi->bi_rw, i); |
| 862 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 863 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 864 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 865 | + rdev->new_data_offset); |
| 866 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 867 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 868 | + rdev->data_offset); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 869 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
majianpeng | e59aa23 | 2013-11-14 15:16:19 +1100 | [diff] [blame] | 870 | bi->bi_rw |= REQ_NOMERGE; |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 871 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 872 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 873 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 874 | sh->dev[i].vec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 875 | bi->bi_vcnt = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 876 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 877 | bi->bi_io_vec[0].bv_offset = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 878 | bi->bi_iter.bi_size = STRIPE_SIZE; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 879 | /* |
| 880 | * If this is discard request, set bi_vcnt 0. We don't |
| 881 | * want to confuse SCSI because SCSI will replace payload |
| 882 | */ |
| 883 | if (rw & REQ_DISCARD) |
| 884 | bi->bi_vcnt = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 885 | if (rrdev) |
| 886 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 887 | |
| 888 | if (conf->mddev->gendisk) |
| 889 | trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), |
| 890 | bi, disk_devt(conf->mddev->gendisk), |
| 891 | sh->dev[i].sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 892 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 893 | } |
| 894 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 895 | if (s->syncing || s->expanding || s->expanded |
| 896 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 897 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 898 | |
| 899 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 900 | |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 901 | bio_reset(rbi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 902 | rbi->bi_bdev = rrdev->bdev; |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 903 | rbi->bi_rw = rw; |
| 904 | BUG_ON(!(rw & WRITE)); |
| 905 | rbi->bi_end_io = raid5_end_write_request; |
| 906 | rbi->bi_private = sh; |
| 907 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 908 | pr_debug("%s: for %llu schedule op %ld on " |
| 909 | "replacement disc %d\n", |
| 910 | __func__, (unsigned long long)sh->sector, |
| 911 | rbi->bi_rw, i); |
| 912 | atomic_inc(&sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 913 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 914 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 915 | + rrdev->new_data_offset); |
| 916 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 917 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 918 | + rrdev->data_offset); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 919 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 920 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 921 | sh->dev[i].rvec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 922 | rbi->bi_vcnt = 1; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 923 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 924 | rbi->bi_io_vec[0].bv_offset = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 925 | rbi->bi_iter.bi_size = STRIPE_SIZE; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 926 | /* |
| 927 | * If this is discard request, set bi_vcnt 0. We don't |
| 928 | * want to confuse SCSI because SCSI will replace payload |
| 929 | */ |
| 930 | if (rw & REQ_DISCARD) |
| 931 | rbi->bi_vcnt = 0; |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 932 | if (conf->mddev->gendisk) |
| 933 | trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), |
| 934 | rbi, disk_devt(conf->mddev->gendisk), |
| 935 | sh->dev[i].sector); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 936 | generic_make_request(rbi); |
| 937 | } |
| 938 | if (!rdev && !rrdev) { |
Namhyung Kim | b062962 | 2011-06-14 14:20:19 +1000 | [diff] [blame] | 939 | if (rw & WRITE) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 940 | set_bit(STRIPE_DEGRADED, &sh->state); |
| 941 | pr_debug("skip op %ld on disc %d for sector %llu\n", |
| 942 | bi->bi_rw, i, (unsigned long long)sh->sector); |
| 943 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 944 | set_bit(STRIPE_HANDLE, &sh->state); |
| 945 | } |
| 946 | } |
| 947 | } |
| 948 | |
| 949 | static struct dma_async_tx_descriptor * |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 950 | async_copy_data(int frombio, struct bio *bio, struct page **page, |
| 951 | sector_t sector, struct dma_async_tx_descriptor *tx, |
| 952 | struct stripe_head *sh) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 953 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 954 | struct bio_vec bvl; |
| 955 | struct bvec_iter iter; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 956 | struct page *bio_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 957 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 958 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 959 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 960 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 961 | if (bio->bi_iter.bi_sector >= sector) |
| 962 | page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 963 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 964 | page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 965 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 966 | if (frombio) |
| 967 | flags |= ASYNC_TX_FENCE; |
| 968 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 969 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 970 | bio_for_each_segment(bvl, bio, iter) { |
| 971 | int len = bvl.bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 972 | int clen; |
| 973 | int b_offset = 0; |
| 974 | |
| 975 | if (page_offset < 0) { |
| 976 | b_offset = -page_offset; |
| 977 | page_offset += b_offset; |
| 978 | len -= b_offset; |
| 979 | } |
| 980 | |
| 981 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 982 | clen = STRIPE_SIZE - page_offset; |
| 983 | else |
| 984 | clen = len; |
| 985 | |
| 986 | if (clen > 0) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 987 | b_offset += bvl.bv_offset; |
| 988 | bio_page = bvl.bv_page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 989 | if (frombio) { |
| 990 | if (sh->raid_conf->skip_copy && |
| 991 | b_offset == 0 && page_offset == 0 && |
| 992 | clen == STRIPE_SIZE) |
| 993 | *page = bio_page; |
| 994 | else |
| 995 | tx = async_memcpy(*page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 996 | b_offset, clen, &submit); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 997 | } else |
| 998 | tx = async_memcpy(bio_page, *page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 999 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1000 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1001 | /* chain the operations */ |
| 1002 | submit.depend_tx = tx; |
| 1003 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1004 | if (clen < len) /* hit end of page */ |
| 1005 | break; |
| 1006 | page_offset += len; |
| 1007 | } |
| 1008 | |
| 1009 | return tx; |
| 1010 | } |
| 1011 | |
| 1012 | static void ops_complete_biofill(void *stripe_head_ref) |
| 1013 | { |
| 1014 | struct stripe_head *sh = stripe_head_ref; |
| 1015 | struct bio *return_bi = NULL; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1016 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1017 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1018 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1019 | (unsigned long long)sh->sector); |
| 1020 | |
| 1021 | /* clear completed biofills */ |
| 1022 | for (i = sh->disks; i--; ) { |
| 1023 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1024 | |
| 1025 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1026 | /* and check if we need to reply to a read request, |
| 1027 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1028 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1029 | */ |
| 1030 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1031 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1032 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1033 | BUG_ON(!dev->read); |
| 1034 | rbi = dev->read; |
| 1035 | dev->read = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1036 | while (rbi && rbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1037 | dev->sector + STRIPE_SECTORS) { |
| 1038 | rbi2 = r5_next_bio(rbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1039 | if (!raid5_dec_bi_active_stripes(rbi)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1040 | rbi->bi_next = return_bi; |
| 1041 | return_bi = rbi; |
| 1042 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1043 | rbi = rbi2; |
| 1044 | } |
| 1045 | } |
| 1046 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1047 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1048 | |
| 1049 | return_io(return_bi); |
| 1050 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1051 | set_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1052 | release_stripe(sh); |
| 1053 | } |
| 1054 | |
| 1055 | static void ops_run_biofill(struct stripe_head *sh) |
| 1056 | { |
| 1057 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1058 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1059 | int i; |
| 1060 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1061 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1062 | (unsigned long long)sh->sector); |
| 1063 | |
| 1064 | for (i = sh->disks; i--; ) { |
| 1065 | struct r5dev *dev = &sh->dev[i]; |
| 1066 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 1067 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1068 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1069 | dev->read = rbi = dev->toread; |
| 1070 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1071 | spin_unlock_irq(&sh->stripe_lock); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1072 | while (rbi && rbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1073 | dev->sector + STRIPE_SECTORS) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1074 | tx = async_copy_data(0, rbi, &dev->page, |
| 1075 | dev->sector, tx, sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1076 | rbi = r5_next_bio(rbi, dev->sector); |
| 1077 | } |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1082 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 1083 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1084 | } |
| 1085 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1086 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 1087 | { |
| 1088 | struct r5dev *tgt; |
| 1089 | |
| 1090 | if (target < 0) |
| 1091 | return; |
| 1092 | |
| 1093 | tgt = &sh->dev[target]; |
| 1094 | set_bit(R5_UPTODATE, &tgt->flags); |
| 1095 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1096 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 1097 | } |
| 1098 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1099 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1100 | { |
| 1101 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1102 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1103 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1104 | (unsigned long long)sh->sector); |
| 1105 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1106 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1107 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1108 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1109 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1110 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 1111 | if (sh->check_state == check_state_compute_run) |
| 1112 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1113 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1114 | release_stripe(sh); |
| 1115 | } |
| 1116 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1117 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 1118 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 1119 | struct raid5_percpu *percpu) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1120 | { |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1121 | return percpu->scribble + sizeof(struct page *) * (sh->disks + 2); |
| 1122 | } |
| 1123 | |
| 1124 | static struct dma_async_tx_descriptor * |
| 1125 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1126 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1127 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1128 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1129 | int target = sh->ops.target; |
| 1130 | struct r5dev *tgt = &sh->dev[target]; |
| 1131 | struct page *xor_dest = tgt->page; |
| 1132 | int count = 0; |
| 1133 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1134 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1135 | int i; |
| 1136 | |
| 1137 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1138 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1139 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1140 | |
| 1141 | for (i = disks; i--; ) |
| 1142 | if (i != target) |
| 1143 | xor_srcs[count++] = sh->dev[i].page; |
| 1144 | |
| 1145 | atomic_inc(&sh->count); |
| 1146 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1147 | 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] | 1148 | ops_complete_compute, sh, to_addr_conv(sh, percpu)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1149 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1150 | 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] | 1151 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1152 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1153 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1154 | return tx; |
| 1155 | } |
| 1156 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1157 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 1158 | * @srcs - (struct page *) array of size sh->disks |
| 1159 | * @sh - stripe_head to parse |
| 1160 | * |
| 1161 | * Populates srcs in proper layout order for the stripe and returns the |
| 1162 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 1163 | * destination buffer is recorded in srcs[count] and the Q destination |
| 1164 | * is recorded in srcs[count+1]]. |
| 1165 | */ |
| 1166 | static int set_syndrome_sources(struct page **srcs, struct stripe_head *sh) |
| 1167 | { |
| 1168 | int disks = sh->disks; |
| 1169 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 1170 | int d0_idx = raid6_d0(sh); |
| 1171 | int count; |
| 1172 | int i; |
| 1173 | |
| 1174 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1175 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1176 | |
| 1177 | count = 0; |
| 1178 | i = d0_idx; |
| 1179 | do { |
| 1180 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1181 | |
| 1182 | srcs[slot] = sh->dev[i].page; |
| 1183 | i = raid6_next_disk(i, disks); |
| 1184 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1185 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1186 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1187 | } |
| 1188 | |
| 1189 | static struct dma_async_tx_descriptor * |
| 1190 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1191 | { |
| 1192 | int disks = sh->disks; |
| 1193 | struct page **blocks = percpu->scribble; |
| 1194 | int target; |
| 1195 | int qd_idx = sh->qd_idx; |
| 1196 | struct dma_async_tx_descriptor *tx; |
| 1197 | struct async_submit_ctl submit; |
| 1198 | struct r5dev *tgt; |
| 1199 | struct page *dest; |
| 1200 | int i; |
| 1201 | int count; |
| 1202 | |
| 1203 | if (sh->ops.target < 0) |
| 1204 | target = sh->ops.target2; |
| 1205 | else if (sh->ops.target2 < 0) |
| 1206 | target = sh->ops.target; |
| 1207 | else |
| 1208 | /* we should only have one valid target */ |
| 1209 | BUG(); |
| 1210 | BUG_ON(target < 0); |
| 1211 | pr_debug("%s: stripe %llu block: %d\n", |
| 1212 | __func__, (unsigned long long)sh->sector, target); |
| 1213 | |
| 1214 | tgt = &sh->dev[target]; |
| 1215 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1216 | dest = tgt->page; |
| 1217 | |
| 1218 | atomic_inc(&sh->count); |
| 1219 | |
| 1220 | if (target == qd_idx) { |
| 1221 | count = set_syndrome_sources(blocks, sh); |
| 1222 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 1223 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1224 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1225 | ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1226 | to_addr_conv(sh, percpu)); |
| 1227 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 1228 | } else { |
| 1229 | /* Compute any data- or p-drive using XOR */ |
| 1230 | count = 0; |
| 1231 | for (i = disks; i-- ; ) { |
| 1232 | if (i == target || i == qd_idx) |
| 1233 | continue; |
| 1234 | blocks[count++] = sh->dev[i].page; |
| 1235 | } |
| 1236 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1237 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1238 | NULL, ops_complete_compute, sh, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1239 | to_addr_conv(sh, percpu)); |
| 1240 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 1241 | } |
| 1242 | |
| 1243 | return tx; |
| 1244 | } |
| 1245 | |
| 1246 | static struct dma_async_tx_descriptor * |
| 1247 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1248 | { |
| 1249 | int i, count, disks = sh->disks; |
| 1250 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1251 | int d0_idx = raid6_d0(sh); |
| 1252 | int faila = -1, failb = -1; |
| 1253 | int target = sh->ops.target; |
| 1254 | int target2 = sh->ops.target2; |
| 1255 | struct r5dev *tgt = &sh->dev[target]; |
| 1256 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1257 | struct dma_async_tx_descriptor *tx; |
| 1258 | struct page **blocks = percpu->scribble; |
| 1259 | struct async_submit_ctl submit; |
| 1260 | |
| 1261 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1262 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1263 | BUG_ON(target < 0 || target2 < 0); |
| 1264 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1265 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1266 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1267 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1268 | * slot number conversion for 'faila' and 'failb' |
| 1269 | */ |
| 1270 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1271 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1272 | count = 0; |
| 1273 | i = d0_idx; |
| 1274 | do { |
| 1275 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1276 | |
| 1277 | blocks[slot] = sh->dev[i].page; |
| 1278 | |
| 1279 | if (i == target) |
| 1280 | faila = slot; |
| 1281 | if (i == target2) |
| 1282 | failb = slot; |
| 1283 | i = raid6_next_disk(i, disks); |
| 1284 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1285 | |
| 1286 | BUG_ON(faila == failb); |
| 1287 | if (failb < faila) |
| 1288 | swap(faila, failb); |
| 1289 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1290 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1291 | |
| 1292 | atomic_inc(&sh->count); |
| 1293 | |
| 1294 | if (failb == syndrome_disks+1) { |
| 1295 | /* Q disk is one of the missing disks */ |
| 1296 | if (faila == syndrome_disks) { |
| 1297 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1298 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1299 | ops_complete_compute, sh, |
| 1300 | to_addr_conv(sh, percpu)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1301 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1302 | STRIPE_SIZE, &submit); |
| 1303 | } else { |
| 1304 | struct page *dest; |
| 1305 | int data_target; |
| 1306 | int qd_idx = sh->qd_idx; |
| 1307 | |
| 1308 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1309 | if (target == qd_idx) |
| 1310 | data_target = target2; |
| 1311 | else |
| 1312 | data_target = target; |
| 1313 | |
| 1314 | count = 0; |
| 1315 | for (i = disks; i-- ; ) { |
| 1316 | if (i == data_target || i == qd_idx) |
| 1317 | continue; |
| 1318 | blocks[count++] = sh->dev[i].page; |
| 1319 | } |
| 1320 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1321 | init_async_submit(&submit, |
| 1322 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1323 | NULL, NULL, NULL, |
| 1324 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1325 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1326 | &submit); |
| 1327 | |
| 1328 | count = set_syndrome_sources(blocks, sh); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1329 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1330 | ops_complete_compute, sh, |
| 1331 | to_addr_conv(sh, percpu)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1332 | return async_gen_syndrome(blocks, 0, count+2, |
| 1333 | STRIPE_SIZE, &submit); |
| 1334 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1335 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1336 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1337 | ops_complete_compute, sh, |
| 1338 | to_addr_conv(sh, percpu)); |
| 1339 | if (failb == syndrome_disks) { |
| 1340 | /* We're missing D+P. */ |
| 1341 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1342 | STRIPE_SIZE, faila, |
| 1343 | blocks, &submit); |
| 1344 | } else { |
| 1345 | /* We're missing D+D. */ |
| 1346 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1347 | STRIPE_SIZE, faila, failb, |
| 1348 | blocks, &submit); |
| 1349 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1350 | } |
| 1351 | } |
| 1352 | |
| 1353 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1354 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1355 | { |
| 1356 | struct stripe_head *sh = stripe_head_ref; |
| 1357 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1358 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1359 | (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1360 | } |
| 1361 | |
| 1362 | static struct dma_async_tx_descriptor * |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1363 | ops_run_prexor(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1364 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1365 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1366 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1367 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1368 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1369 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1370 | |
| 1371 | /* existing parity data subtracted */ |
| 1372 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1373 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1374 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1375 | (unsigned long long)sh->sector); |
| 1376 | |
| 1377 | for (i = disks; i--; ) { |
| 1378 | struct r5dev *dev = &sh->dev[i]; |
| 1379 | /* Only process blocks that are known to be uptodate */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1380 | if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1381 | xor_srcs[count++] = dev->page; |
| 1382 | } |
| 1383 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1384 | 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] | 1385 | ops_complete_prexor, sh, to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1386 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1387 | |
| 1388 | return tx; |
| 1389 | } |
| 1390 | |
| 1391 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1392 | 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] | 1393 | { |
| 1394 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1395 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1396 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1397 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1398 | (unsigned long long)sh->sector); |
| 1399 | |
| 1400 | for (i = disks; i--; ) { |
| 1401 | struct r5dev *dev = &sh->dev[i]; |
| 1402 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1403 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1404 | if (test_and_clear_bit(R5_Wantdrain, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1405 | struct bio *wbi; |
| 1406 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1407 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1408 | chosen = dev->towrite; |
| 1409 | dev->towrite = NULL; |
| 1410 | BUG_ON(dev->written); |
| 1411 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1412 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1413 | WARN_ON(dev->page != dev->orig_page); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1414 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1415 | while (wbi && wbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1416 | dev->sector + STRIPE_SECTORS) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1417 | if (wbi->bi_rw & REQ_FUA) |
| 1418 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1419 | if (wbi->bi_rw & REQ_SYNC) |
| 1420 | set_bit(R5_SyncIO, &dev->flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1421 | if (wbi->bi_rw & REQ_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1422 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1423 | else { |
| 1424 | tx = async_copy_data(1, wbi, &dev->page, |
| 1425 | dev->sector, tx, sh); |
| 1426 | if (dev->page != dev->orig_page) { |
| 1427 | set_bit(R5_SkipCopy, &dev->flags); |
| 1428 | clear_bit(R5_UPTODATE, &dev->flags); |
| 1429 | clear_bit(R5_OVERWRITE, &dev->flags); |
| 1430 | } |
| 1431 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1432 | wbi = r5_next_bio(wbi, dev->sector); |
| 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | return tx; |
| 1438 | } |
| 1439 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1440 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1441 | { |
| 1442 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1443 | int disks = sh->disks; |
| 1444 | int pd_idx = sh->pd_idx; |
| 1445 | int qd_idx = sh->qd_idx; |
| 1446 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1447 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1448 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1449 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1450 | (unsigned long long)sh->sector); |
| 1451 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1452 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1453 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1454 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1455 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1456 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1457 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1458 | for (i = disks; i--; ) { |
| 1459 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1460 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1461 | if (dev->written || i == pd_idx || i == qd_idx) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1462 | if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1463 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1464 | if (fua) |
| 1465 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1466 | if (sync) |
| 1467 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1468 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1469 | } |
| 1470 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1471 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1472 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1473 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1474 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1475 | else { |
| 1476 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1477 | sh->reconstruct_state = reconstruct_state_result; |
| 1478 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1479 | |
| 1480 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1481 | release_stripe(sh); |
| 1482 | } |
| 1483 | |
| 1484 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1485 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1486 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1487 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1488 | int disks = sh->disks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1489 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1490 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1491 | int count = 0, pd_idx = sh->pd_idx, i; |
| 1492 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1493 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1494 | unsigned long flags; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1495 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1496 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1497 | (unsigned long long)sh->sector); |
| 1498 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1499 | for (i = 0; i < sh->disks; i++) { |
| 1500 | if (pd_idx == i) |
| 1501 | continue; |
| 1502 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1503 | break; |
| 1504 | } |
| 1505 | if (i >= sh->disks) { |
| 1506 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1507 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 1508 | ops_complete_reconstruct(sh); |
| 1509 | return; |
| 1510 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1511 | /* check if prexor is active which means only process blocks |
| 1512 | * that are part of a read-modify-write (written) |
| 1513 | */ |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1514 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1515 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1516 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1517 | for (i = disks; i--; ) { |
| 1518 | struct r5dev *dev = &sh->dev[i]; |
| 1519 | if (dev->written) |
| 1520 | xor_srcs[count++] = dev->page; |
| 1521 | } |
| 1522 | } else { |
| 1523 | xor_dest = sh->dev[pd_idx].page; |
| 1524 | for (i = disks; i--; ) { |
| 1525 | struct r5dev *dev = &sh->dev[i]; |
| 1526 | if (i != pd_idx) |
| 1527 | xor_srcs[count++] = dev->page; |
| 1528 | } |
| 1529 | } |
| 1530 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1531 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1532 | * 2/ if we did not prexor then we are redoing the parity |
| 1533 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1534 | * for the synchronous xor case |
| 1535 | */ |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 1536 | flags = ASYNC_TX_ACK | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1537 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
| 1538 | |
| 1539 | atomic_inc(&sh->count); |
| 1540 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1541 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, sh, |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1542 | to_addr_conv(sh, percpu)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1543 | if (unlikely(count == 1)) |
| 1544 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1545 | else |
| 1546 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1547 | } |
| 1548 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1549 | static void |
| 1550 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1551 | struct dma_async_tx_descriptor *tx) |
| 1552 | { |
| 1553 | struct async_submit_ctl submit; |
| 1554 | struct page **blocks = percpu->scribble; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1555 | int count, i; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1556 | |
| 1557 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1558 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1559 | for (i = 0; i < sh->disks; i++) { |
| 1560 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 1561 | continue; |
| 1562 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1563 | break; |
| 1564 | } |
| 1565 | if (i >= sh->disks) { |
| 1566 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1567 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 1568 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 1569 | ops_complete_reconstruct(sh); |
| 1570 | return; |
| 1571 | } |
| 1572 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1573 | count = set_syndrome_sources(blocks, sh); |
| 1574 | |
| 1575 | atomic_inc(&sh->count); |
| 1576 | |
| 1577 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_reconstruct, |
| 1578 | sh, to_addr_conv(sh, percpu)); |
| 1579 | async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | static void ops_complete_check(void *stripe_head_ref) |
| 1583 | { |
| 1584 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1585 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1586 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1587 | (unsigned long long)sh->sector); |
| 1588 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1589 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1590 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1591 | release_stripe(sh); |
| 1592 | } |
| 1593 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1594 | 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] | 1595 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1596 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1597 | int pd_idx = sh->pd_idx; |
| 1598 | int qd_idx = sh->qd_idx; |
| 1599 | struct page *xor_dest; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1600 | struct page **xor_srcs = percpu->scribble; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1601 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1602 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1603 | int count; |
| 1604 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1605 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1606 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1607 | (unsigned long long)sh->sector); |
| 1608 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1609 | count = 0; |
| 1610 | xor_dest = sh->dev[pd_idx].page; |
| 1611 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1612 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1613 | if (i == pd_idx || i == qd_idx) |
| 1614 | continue; |
| 1615 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1616 | } |
| 1617 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1618 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
| 1619 | to_addr_conv(sh, percpu)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1620 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1621 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1622 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1623 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1624 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1625 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1626 | } |
| 1627 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1628 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1629 | { |
| 1630 | struct page **srcs = percpu->scribble; |
| 1631 | struct async_submit_ctl submit; |
| 1632 | int count; |
| 1633 | |
| 1634 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1635 | (unsigned long long)sh->sector, checkp); |
| 1636 | |
| 1637 | count = set_syndrome_sources(srcs, sh); |
| 1638 | if (!checkp) |
| 1639 | srcs[count] = NULL; |
| 1640 | |
| 1641 | atomic_inc(&sh->count); |
| 1642 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
| 1643 | sh, to_addr_conv(sh, percpu)); |
| 1644 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1645 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1646 | } |
| 1647 | |
NeilBrown | 51acbce | 2013-02-28 09:08:34 +1100 | [diff] [blame] | 1648 | 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] | 1649 | { |
| 1650 | int overlap_clear = 0, i, disks = sh->disks; |
| 1651 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1652 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1653 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1654 | struct raid5_percpu *percpu; |
| 1655 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1656 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1657 | cpu = get_cpu(); |
| 1658 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1659 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1660 | ops_run_biofill(sh); |
| 1661 | overlap_clear++; |
| 1662 | } |
| 1663 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1664 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1665 | if (level < 6) |
| 1666 | tx = ops_run_compute5(sh, percpu); |
| 1667 | else { |
| 1668 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1669 | tx = ops_run_compute6_1(sh, percpu); |
| 1670 | else |
| 1671 | tx = ops_run_compute6_2(sh, percpu); |
| 1672 | } |
| 1673 | /* terminate the chain if reconstruct is not set to be run */ |
| 1674 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1675 | async_tx_ack(tx); |
| 1676 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1677 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1678 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1679 | tx = ops_run_prexor(sh, percpu, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1680 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1681 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1682 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1683 | overlap_clear++; |
| 1684 | } |
| 1685 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1686 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1687 | if (level < 6) |
| 1688 | ops_run_reconstruct5(sh, percpu, tx); |
| 1689 | else |
| 1690 | ops_run_reconstruct6(sh, percpu, tx); |
| 1691 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1692 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1693 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1694 | if (sh->check_state == check_state_run) |
| 1695 | ops_run_check_p(sh, percpu); |
| 1696 | else if (sh->check_state == check_state_run_q) |
| 1697 | ops_run_check_pq(sh, percpu, 0); |
| 1698 | else if (sh->check_state == check_state_run_pq) |
| 1699 | ops_run_check_pq(sh, percpu, 1); |
| 1700 | else |
| 1701 | BUG(); |
| 1702 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1703 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1704 | if (overlap_clear) |
| 1705 | for (i = disks; i--; ) { |
| 1706 | struct r5dev *dev = &sh->dev[i]; |
| 1707 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1708 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1709 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1710 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1713 | static int grow_one_stripe(struct r5conf *conf, int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1714 | { |
| 1715 | struct stripe_head *sh; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1716 | sh = kmem_cache_zalloc(conf->slab_cache, GFP_KERNEL); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1717 | if (!sh) |
| 1718 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1719 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1720 | sh->raid_conf = conf; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1721 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1722 | spin_lock_init(&sh->stripe_lock); |
| 1723 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1724 | if (grow_buffers(sh)) { |
| 1725 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1726 | kmem_cache_free(conf->slab_cache, sh); |
| 1727 | return 0; |
| 1728 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1729 | sh->hash_lock_index = hash; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1730 | /* we just created an active stripe so... */ |
| 1731 | atomic_set(&sh->count, 1); |
| 1732 | atomic_inc(&conf->active_stripes); |
| 1733 | INIT_LIST_HEAD(&sh->lru); |
| 1734 | release_stripe(sh); |
| 1735 | return 1; |
| 1736 | } |
| 1737 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1738 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1739 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1740 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 1741 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1742 | int hash; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1743 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 1744 | if (conf->mddev->gendisk) |
| 1745 | sprintf(conf->cache_name[0], |
| 1746 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 1747 | else |
| 1748 | sprintf(conf->cache_name[0], |
| 1749 | "raid%d-%p", conf->level, conf->mddev); |
| 1750 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 1751 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1752 | conf->active_name = 0; |
| 1753 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1754 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1755 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1756 | if (!sc) |
| 1757 | return 1; |
| 1758 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1759 | conf->pool_size = devs; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1760 | hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; |
| 1761 | while (num--) { |
| 1762 | if (!grow_one_stripe(conf, hash)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1763 | return 1; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1764 | conf->max_nr_stripes++; |
| 1765 | hash = (hash + 1) % NR_STRIPE_HASH_LOCKS; |
| 1766 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1767 | return 0; |
| 1768 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 1769 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1770 | /** |
| 1771 | * scribble_len - return the required size of the scribble region |
| 1772 | * @num - total number of disks in the array |
| 1773 | * |
| 1774 | * The size must be enough to contain: |
| 1775 | * 1/ a struct page pointer for each device in the array +2 |
| 1776 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 1777 | * (dma_map_page()) or page (page_address()) address. |
| 1778 | * |
| 1779 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 1780 | * calculate over all devices (not just the data blocks), using zeros in place |
| 1781 | * of the P and Q blocks. |
| 1782 | */ |
| 1783 | static size_t scribble_len(int num) |
| 1784 | { |
| 1785 | size_t len; |
| 1786 | |
| 1787 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
| 1788 | |
| 1789 | return len; |
| 1790 | } |
| 1791 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1792 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1793 | { |
| 1794 | /* Make all the stripes able to hold 'newsize' devices. |
| 1795 | * New slots in each stripe get 'page' set to a new page. |
| 1796 | * |
| 1797 | * This happens in stages: |
| 1798 | * 1/ create a new kmem_cache and allocate the required number of |
| 1799 | * stripe_heads. |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 1800 | * 2/ gather all the old stripe_heads and transfer the pages across |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1801 | * to the new stripe_heads. This will have the side effect of |
| 1802 | * freezing the array as once all stripe_heads have been collected, |
| 1803 | * no IO will be possible. Old stripe heads are freed once their |
| 1804 | * pages have been transferred over, and the old kmem_cache is |
| 1805 | * freed when all stripes are done. |
| 1806 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 1807 | * we simple return a failre status - no need to clean anything up. |
| 1808 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 1809 | * If this fails, we don't bother trying the shrink the |
| 1810 | * stripe_heads down again, we just leave them as they are. |
| 1811 | * As each stripe_head is processed the new one is released into |
| 1812 | * active service. |
| 1813 | * |
| 1814 | * Once step2 is started, we cannot afford to wait for a write, |
| 1815 | * so we use GFP_NOIO allocations. |
| 1816 | */ |
| 1817 | struct stripe_head *osh, *nsh; |
| 1818 | LIST_HEAD(newstripes); |
| 1819 | struct disk_info *ndisks; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1820 | unsigned long cpu; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1821 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 1822 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1823 | int i; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1824 | int hash, cnt; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1825 | |
| 1826 | if (newsize <= conf->pool_size) |
| 1827 | return 0; /* never bother to shrink */ |
| 1828 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 1829 | err = md_allow_write(conf->mddev); |
| 1830 | if (err) |
| 1831 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 1832 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1833 | /* Step 1 */ |
| 1834 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 1835 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 1836 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1837 | if (!sc) |
| 1838 | return -ENOMEM; |
| 1839 | |
| 1840 | for (i = conf->max_nr_stripes; i; i--) { |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 1841 | nsh = kmem_cache_zalloc(sc, GFP_KERNEL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1842 | if (!nsh) |
| 1843 | break; |
| 1844 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1845 | nsh->raid_conf = conf; |
NeilBrown | cb13ff6 | 2012-09-24 16:27:20 +1000 | [diff] [blame] | 1846 | spin_lock_init(&nsh->stripe_lock); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1847 | |
| 1848 | list_add(&nsh->lru, &newstripes); |
| 1849 | } |
| 1850 | if (i) { |
| 1851 | /* didn't get enough, give up */ |
| 1852 | while (!list_empty(&newstripes)) { |
| 1853 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1854 | list_del(&nsh->lru); |
| 1855 | kmem_cache_free(sc, nsh); |
| 1856 | } |
| 1857 | kmem_cache_destroy(sc); |
| 1858 | return -ENOMEM; |
| 1859 | } |
| 1860 | /* Step 2 - Must use GFP_NOIO now. |
| 1861 | * OK, we have enough stripes, start collecting inactive |
| 1862 | * stripes and copying them over |
| 1863 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1864 | hash = 0; |
| 1865 | cnt = 0; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1866 | list_for_each_entry(nsh, &newstripes, lru) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1867 | lock_device_hash_lock(conf, hash); |
| 1868 | wait_event_cmd(conf->wait_for_stripe, |
| 1869 | !list_empty(conf->inactive_list + hash), |
| 1870 | unlock_device_hash_lock(conf, hash), |
| 1871 | lock_device_hash_lock(conf, hash)); |
| 1872 | osh = get_free_stripe(conf, hash); |
| 1873 | unlock_device_hash_lock(conf, hash); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1874 | atomic_set(&nsh->count, 1); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1875 | for(i=0; i<conf->pool_size; i++) { |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1876 | nsh->dev[i].page = osh->dev[i].page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1877 | nsh->dev[i].orig_page = osh->dev[i].page; |
| 1878 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1879 | for( ; i<newsize; i++) |
| 1880 | nsh->dev[i].page = NULL; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1881 | nsh->hash_lock_index = hash; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1882 | kmem_cache_free(conf->slab_cache, osh); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1883 | cnt++; |
| 1884 | if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + |
| 1885 | !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { |
| 1886 | hash++; |
| 1887 | cnt = 0; |
| 1888 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1889 | } |
| 1890 | kmem_cache_destroy(conf->slab_cache); |
| 1891 | |
| 1892 | /* Step 3. |
| 1893 | * At this point, we are holding all the stripes so the array |
| 1894 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1895 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1896 | */ |
| 1897 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 1898 | if (ndisks) { |
| 1899 | for (i=0; i<conf->raid_disks; i++) |
| 1900 | ndisks[i] = conf->disks[i]; |
| 1901 | kfree(conf->disks); |
| 1902 | conf->disks = ndisks; |
| 1903 | } else |
| 1904 | err = -ENOMEM; |
| 1905 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1906 | get_online_cpus(); |
| 1907 | conf->scribble_len = scribble_len(newsize); |
| 1908 | for_each_present_cpu(cpu) { |
| 1909 | struct raid5_percpu *percpu; |
| 1910 | void *scribble; |
| 1911 | |
| 1912 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 1913 | scribble = kmalloc(conf->scribble_len, GFP_NOIO); |
| 1914 | |
| 1915 | if (scribble) { |
| 1916 | kfree(percpu->scribble); |
| 1917 | percpu->scribble = scribble; |
| 1918 | } else { |
| 1919 | err = -ENOMEM; |
| 1920 | break; |
| 1921 | } |
| 1922 | } |
| 1923 | put_online_cpus(); |
| 1924 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1925 | /* Step 4, return new stripes to service */ |
| 1926 | while(!list_empty(&newstripes)) { |
| 1927 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 1928 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1929 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1930 | for (i=conf->raid_disks; i < newsize; i++) |
| 1931 | if (nsh->dev[i].page == NULL) { |
| 1932 | struct page *p = alloc_page(GFP_NOIO); |
| 1933 | nsh->dev[i].page = p; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1934 | nsh->dev[i].orig_page = p; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 1935 | if (!p) |
| 1936 | err = -ENOMEM; |
| 1937 | } |
| 1938 | release_stripe(nsh); |
| 1939 | } |
| 1940 | /* critical section pass, GFP_NOIO no longer needed */ |
| 1941 | |
| 1942 | conf->slab_cache = sc; |
| 1943 | conf->active_name = 1-conf->active_name; |
| 1944 | conf->pool_size = newsize; |
| 1945 | return err; |
| 1946 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1947 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1948 | static int drop_one_stripe(struct r5conf *conf, int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1949 | { |
| 1950 | struct stripe_head *sh; |
| 1951 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1952 | spin_lock_irq(conf->hash_locks + hash); |
| 1953 | sh = get_free_stripe(conf, hash); |
| 1954 | spin_unlock_irq(conf->hash_locks + hash); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1955 | if (!sh) |
| 1956 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 1957 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 1958 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1959 | kmem_cache_free(conf->slab_cache, sh); |
| 1960 | atomic_dec(&conf->active_stripes); |
| 1961 | return 1; |
| 1962 | } |
| 1963 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1964 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1965 | { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 1966 | int hash; |
| 1967 | for (hash = 0; hash < NR_STRIPE_HASH_LOCKS; hash++) |
| 1968 | while (drop_one_stripe(conf, hash)) |
| 1969 | ; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 1970 | |
NeilBrown | 29fc7e3 | 2006-02-03 03:03:41 -0800 | [diff] [blame] | 1971 | if (conf->slab_cache) |
| 1972 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1973 | conf->slab_cache = NULL; |
| 1974 | } |
| 1975 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1976 | static void raid5_end_read_request(struct bio * bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1977 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 1978 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1979 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 1980 | int disks = sh->disks, i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1981 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 1982 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1983 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1984 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1985 | |
| 1986 | for (i=0 ; i<disks; i++) |
| 1987 | if (bi == &sh->dev[i].req) |
| 1988 | break; |
| 1989 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 1990 | pr_debug("end_read_request %llu/%d, count: %d, uptodate %d.\n", |
| 1991 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1992 | uptodate); |
| 1993 | if (i == disks) { |
| 1994 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1995 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1996 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 1997 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1998 | /* If replacement finished while this request was outstanding, |
| 1999 | * 'replacement' might be NULL already. |
| 2000 | * In that case it moved down to 'rdev'. |
| 2001 | * rdev is not removed until all requests are finished. |
| 2002 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2003 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2004 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2005 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2006 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2007 | if (use_new_offset(conf, sh)) |
| 2008 | s = sh->sector + rdev->new_data_offset; |
| 2009 | else |
| 2010 | s = sh->sector + rdev->data_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2011 | if (uptodate) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2012 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2013 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2014 | /* Note that this cannot happen on a |
| 2015 | * replacement device. We just fail those on |
| 2016 | * any error |
| 2017 | */ |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2018 | printk_ratelimited( |
| 2019 | KERN_INFO |
| 2020 | "md/raid:%s: read error corrected" |
| 2021 | " (%lu sectors at %llu on %s)\n", |
| 2022 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2023 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2024 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2025 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2026 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2027 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2028 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2029 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2030 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2031 | if (atomic_read(&rdev->read_errors)) |
| 2032 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2033 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2034 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2035 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2036 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2037 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2038 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2039 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2040 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
| 2041 | printk_ratelimited( |
| 2042 | KERN_WARNING |
| 2043 | "md/raid:%s: read error on replacement device " |
| 2044 | "(sector %llu on %s).\n", |
| 2045 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2046 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2047 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2048 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 2049 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2050 | printk_ratelimited( |
| 2051 | KERN_WARNING |
| 2052 | "md/raid:%s: read error not correctable " |
| 2053 | "(sector %llu on %s).\n", |
| 2054 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2055 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2056 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2057 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2058 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2059 | set_bad = 1; |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2060 | printk_ratelimited( |
| 2061 | KERN_WARNING |
| 2062 | "md/raid:%s: read error NOT corrected!! " |
| 2063 | "(sector %llu on %s).\n", |
| 2064 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2065 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2066 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2067 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2068 | > conf->max_nr_stripes) |
NeilBrown | 14f8d26 | 2006-01-06 00:20:14 -0800 | [diff] [blame] | 2069 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2070 | "md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2071 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2072 | else |
| 2073 | retry = 1; |
Bian Yu | edfa1f6 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2074 | if (set_bad && test_bit(In_sync, &rdev->flags) |
| 2075 | && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2076 | retry = 1; |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2077 | if (retry) |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2078 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
| 2079 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 2080 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2081 | } else |
| 2082 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2083 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2084 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2085 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2086 | if (!(set_bad |
| 2087 | && test_bit(In_sync, &rdev->flags) |
| 2088 | && rdev_set_badblocks( |
| 2089 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 2090 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2091 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2092 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2093 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2094 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 2095 | set_bit(STRIPE_HANDLE, &sh->state); |
| 2096 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2097 | } |
| 2098 | |
NeilBrown | d710e13 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 2099 | static void raid5_end_write_request(struct bio *bi, int error) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2101 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2102 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2103 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2104 | struct md_rdev *uninitialized_var(rdev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2105 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2106 | sector_t first_bad; |
| 2107 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2108 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2109 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2110 | for (i = 0 ; i < disks; i++) { |
| 2111 | if (bi == &sh->dev[i].req) { |
| 2112 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2114 | } |
| 2115 | if (bi == &sh->dev[i].rreq) { |
| 2116 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2117 | if (rdev) |
| 2118 | replacement = 1; |
| 2119 | else |
| 2120 | /* rdev was removed and 'replacement' |
| 2121 | * replaced it. rdev is not removed |
| 2122 | * until all requests are finished. |
| 2123 | */ |
| 2124 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2125 | break; |
| 2126 | } |
| 2127 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2128 | pr_debug("end_write_request %llu/%d, count %d, uptodate: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2129 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
| 2130 | uptodate); |
| 2131 | if (i == disks) { |
| 2132 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2133 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2134 | } |
| 2135 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2136 | if (replacement) { |
| 2137 | if (!uptodate) |
| 2138 | md_error(conf->mddev, rdev); |
| 2139 | else if (is_badblock(rdev, sh->sector, |
| 2140 | STRIPE_SECTORS, |
| 2141 | &first_bad, &bad_sectors)) |
| 2142 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 2143 | } else { |
| 2144 | if (!uptodate) { |
NeilBrown | 9f97e4b | 2014-01-16 09:35:38 +1100 | [diff] [blame] | 2145 | set_bit(STRIPE_DEGRADED, &sh->state); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2146 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2147 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2148 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2149 | set_bit(MD_RECOVERY_NEEDED, |
| 2150 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2151 | } else if (is_badblock(rdev, sh->sector, |
| 2152 | STRIPE_SECTORS, |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2153 | &first_bad, &bad_sectors)) { |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2154 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2155 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
| 2156 | /* That was a successful write so make |
| 2157 | * sure it looks like we already did |
| 2158 | * a re-write. |
| 2159 | */ |
| 2160 | set_bit(R5_ReWrite, &sh->dev[i].flags); |
| 2161 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2162 | } |
| 2163 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2164 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2165 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 2166 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2167 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 2168 | release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2169 | } |
| 2170 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2171 | static sector_t compute_blocknr(struct stripe_head *sh, int i, int previous); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2172 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2173 | 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] | 2174 | { |
| 2175 | struct r5dev *dev = &sh->dev[i]; |
| 2176 | |
| 2177 | bio_init(&dev->req); |
| 2178 | dev->req.bi_io_vec = &dev->vec; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2179 | dev->req.bi_max_vecs = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2180 | dev->req.bi_private = sh; |
| 2181 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2182 | bio_init(&dev->rreq); |
| 2183 | dev->rreq.bi_io_vec = &dev->rvec; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2184 | dev->rreq.bi_max_vecs = 1; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2185 | dev->rreq.bi_private = sh; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2186 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2187 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2188 | dev->sector = compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 2191 | static void error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2192 | { |
| 2193 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2194 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2195 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2196 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2197 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2198 | spin_lock_irqsave(&conf->device_lock, flags); |
| 2199 | clear_bit(In_sync, &rdev->flags); |
| 2200 | mddev->degraded = calc_degraded(conf); |
| 2201 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2202 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2203 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2204 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 2205 | set_bit(Faulty, &rdev->flags); |
| 2206 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 2207 | printk(KERN_ALERT |
| 2208 | "md/raid:%s: Disk failure on %s, disabling device.\n" |
| 2209 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 2210 | mdname(mddev), |
| 2211 | bdevname(rdev->bdev, b), |
| 2212 | mdname(mddev), |
| 2213 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2214 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2215 | |
| 2216 | /* |
| 2217 | * Input: a 'big' sector number, |
| 2218 | * Output: index of the data and parity disk, and the sector # in them. |
| 2219 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2220 | static sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2221 | int previous, int *dd_idx, |
| 2222 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2223 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2224 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2225 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2226 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2227 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2228 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2229 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2230 | int algorithm = previous ? conf->prev_algo |
| 2231 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2232 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2233 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2234 | int raid_disks = previous ? conf->previous_raid_disks |
| 2235 | : conf->raid_disks; |
| 2236 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2237 | |
| 2238 | /* First compute the information on this sector */ |
| 2239 | |
| 2240 | /* |
| 2241 | * Compute the chunk number and the sector offset inside the chunk |
| 2242 | */ |
| 2243 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 2244 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2245 | |
| 2246 | /* |
| 2247 | * Compute the stripe number |
| 2248 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2249 | stripe = chunk_number; |
| 2250 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2251 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2252 | /* |
| 2253 | * Select the parity disk based on the user selected algorithm. |
| 2254 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2255 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2256 | switch(conf->level) { |
| 2257 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2258 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2259 | break; |
| 2260 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2261 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2262 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2263 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2264 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2265 | (*dd_idx)++; |
| 2266 | break; |
| 2267 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2268 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2269 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2270 | (*dd_idx)++; |
| 2271 | break; |
| 2272 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2273 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2274 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2275 | break; |
| 2276 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2277 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2278 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2279 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2280 | case ALGORITHM_PARITY_0: |
| 2281 | pd_idx = 0; |
| 2282 | (*dd_idx)++; |
| 2283 | break; |
| 2284 | case ALGORITHM_PARITY_N: |
| 2285 | pd_idx = data_disks; |
| 2286 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2288 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2289 | } |
| 2290 | break; |
| 2291 | case 6: |
| 2292 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2293 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2294 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2295 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2296 | qd_idx = pd_idx + 1; |
| 2297 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2298 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2299 | qd_idx = 0; |
| 2300 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2301 | (*dd_idx) += 2; /* D D P Q D */ |
| 2302 | break; |
| 2303 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2304 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2305 | qd_idx = pd_idx + 1; |
| 2306 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2307 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2308 | qd_idx = 0; |
| 2309 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2310 | (*dd_idx) += 2; /* D D P Q D */ |
| 2311 | break; |
| 2312 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2313 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2314 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2315 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2316 | break; |
| 2317 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2318 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2319 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2320 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2321 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2322 | |
| 2323 | case ALGORITHM_PARITY_0: |
| 2324 | pd_idx = 0; |
| 2325 | qd_idx = 1; |
| 2326 | (*dd_idx) += 2; |
| 2327 | break; |
| 2328 | case ALGORITHM_PARITY_N: |
| 2329 | pd_idx = data_disks; |
| 2330 | qd_idx = data_disks + 1; |
| 2331 | break; |
| 2332 | |
| 2333 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2334 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2335 | * of blocks for computing Q is different. |
| 2336 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2337 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2338 | qd_idx = pd_idx + 1; |
| 2339 | if (pd_idx == raid_disks-1) { |
| 2340 | (*dd_idx)++; /* Q D D D P */ |
| 2341 | qd_idx = 0; |
| 2342 | } else if (*dd_idx >= pd_idx) |
| 2343 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2344 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2345 | break; |
| 2346 | |
| 2347 | case ALGORITHM_ROTATING_N_RESTART: |
| 2348 | /* Same a left_asymmetric, by first stripe is |
| 2349 | * D D D P Q rather than |
| 2350 | * Q D D D P |
| 2351 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2352 | stripe2 += 1; |
| 2353 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2354 | qd_idx = pd_idx + 1; |
| 2355 | if (pd_idx == raid_disks-1) { |
| 2356 | (*dd_idx)++; /* Q D D D P */ |
| 2357 | qd_idx = 0; |
| 2358 | } else if (*dd_idx >= pd_idx) |
| 2359 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2360 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2361 | break; |
| 2362 | |
| 2363 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2364 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2365 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2366 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2367 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2368 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2369 | break; |
| 2370 | |
| 2371 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2372 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2373 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2374 | if (*dd_idx >= pd_idx) |
| 2375 | (*dd_idx)++; |
| 2376 | qd_idx = raid_disks - 1; |
| 2377 | break; |
| 2378 | |
| 2379 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2380 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2381 | if (*dd_idx >= pd_idx) |
| 2382 | (*dd_idx)++; |
| 2383 | qd_idx = raid_disks - 1; |
| 2384 | break; |
| 2385 | |
| 2386 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2387 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2388 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2389 | qd_idx = raid_disks - 1; |
| 2390 | break; |
| 2391 | |
| 2392 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2393 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2394 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2395 | qd_idx = raid_disks - 1; |
| 2396 | break; |
| 2397 | |
| 2398 | case ALGORITHM_PARITY_0_6: |
| 2399 | pd_idx = 0; |
| 2400 | (*dd_idx)++; |
| 2401 | qd_idx = raid_disks - 1; |
| 2402 | break; |
| 2403 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2404 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2405 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2406 | } |
| 2407 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2408 | } |
| 2409 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2410 | if (sh) { |
| 2411 | sh->pd_idx = pd_idx; |
| 2412 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2413 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2414 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2415 | /* |
| 2416 | * Finally, compute the new sector number |
| 2417 | */ |
| 2418 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2419 | return new_sector; |
| 2420 | } |
| 2421 | |
| 2422 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2423 | 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] | 2424 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2425 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2426 | int raid_disks = sh->disks; |
| 2427 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2428 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2429 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2430 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2431 | int algorithm = previous ? conf->prev_algo |
| 2432 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2433 | sector_t stripe; |
| 2434 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2435 | sector_t chunk_number; |
| 2436 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2438 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2439 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2440 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2442 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2443 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2444 | if (i == sh->pd_idx) |
| 2445 | return 0; |
| 2446 | switch(conf->level) { |
| 2447 | case 4: break; |
| 2448 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2449 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2450 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2451 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2452 | if (i > sh->pd_idx) |
| 2453 | i--; |
| 2454 | break; |
| 2455 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2456 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2457 | if (i < sh->pd_idx) |
| 2458 | i += raid_disks; |
| 2459 | i -= (sh->pd_idx + 1); |
| 2460 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2461 | case ALGORITHM_PARITY_0: |
| 2462 | i -= 1; |
| 2463 | break; |
| 2464 | case ALGORITHM_PARITY_N: |
| 2465 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2467 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2468 | } |
| 2469 | break; |
| 2470 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2471 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2472 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2473 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2474 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2475 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2476 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2477 | case ALGORITHM_ROTATING_N_RESTART: |
| 2478 | if (sh->pd_idx == raid_disks-1) |
| 2479 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2480 | else if (i > sh->pd_idx) |
| 2481 | i -= 2; /* D D P Q D */ |
| 2482 | break; |
| 2483 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2484 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2485 | if (sh->pd_idx == raid_disks-1) |
| 2486 | i--; /* Q D D D P */ |
| 2487 | else { |
| 2488 | /* D D P Q D */ |
| 2489 | if (i < sh->pd_idx) |
| 2490 | i += raid_disks; |
| 2491 | i -= (sh->pd_idx + 2); |
| 2492 | } |
| 2493 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2494 | case ALGORITHM_PARITY_0: |
| 2495 | i -= 2; |
| 2496 | break; |
| 2497 | case ALGORITHM_PARITY_N: |
| 2498 | break; |
| 2499 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2500 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2501 | if (sh->pd_idx == 0) |
| 2502 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2503 | else { |
| 2504 | /* D D Q P D */ |
| 2505 | if (i < sh->pd_idx) |
| 2506 | i += raid_disks; |
| 2507 | i -= (sh->pd_idx + 1); |
| 2508 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2509 | break; |
| 2510 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2511 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2512 | if (i > sh->pd_idx) |
| 2513 | i--; |
| 2514 | break; |
| 2515 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2516 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2517 | if (i < sh->pd_idx) |
| 2518 | i += data_disks + 1; |
| 2519 | i -= (sh->pd_idx + 1); |
| 2520 | break; |
| 2521 | case ALGORITHM_PARITY_0_6: |
| 2522 | i -= 1; |
| 2523 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2524 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2525 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2526 | } |
| 2527 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2531 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2533 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2534 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2535 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2536 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2537 | printk(KERN_ERR "md/raid:%s: compute_blocknr: map not correct\n", |
| 2538 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2539 | return 0; |
| 2540 | } |
| 2541 | return r_sector; |
| 2542 | } |
| 2543 | |
| 2544 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2545 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2546 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2547 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2548 | { |
| 2549 | int i, pd_idx = sh->pd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2550 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2551 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2552 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2553 | if (rcw) { |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2554 | |
| 2555 | for (i = disks; i--; ) { |
| 2556 | struct r5dev *dev = &sh->dev[i]; |
| 2557 | |
| 2558 | if (dev->towrite) { |
| 2559 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2560 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2561 | if (!expand) |
| 2562 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2563 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2564 | } |
| 2565 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 2566 | /* if we are not expanding this is a proper write request, and |
| 2567 | * there will be bios with new data to be drained into the |
| 2568 | * stripe cache |
| 2569 | */ |
| 2570 | if (!expand) { |
| 2571 | if (!s->locked) |
| 2572 | /* False alarm, nothing to do */ |
| 2573 | return; |
| 2574 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2575 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2576 | } else |
| 2577 | sh->reconstruct_state = reconstruct_state_run; |
| 2578 | |
| 2579 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
| 2580 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2581 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2582 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2583 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2584 | } else { |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2585 | BUG_ON(level == 6); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2586 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2587 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
| 2588 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2589 | for (i = disks; i--; ) { |
| 2590 | struct r5dev *dev = &sh->dev[i]; |
| 2591 | if (i == pd_idx) |
| 2592 | continue; |
| 2593 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2594 | if (dev->towrite && |
| 2595 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2596 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2597 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2598 | set_bit(R5_LOCKED, &dev->flags); |
| 2599 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2600 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2601 | } |
| 2602 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 2603 | if (!s->locked) |
| 2604 | /* False alarm - nothing to do */ |
| 2605 | return; |
| 2606 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
| 2607 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2608 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2609 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2610 | } |
| 2611 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2612 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2613 | * are in flight |
| 2614 | */ |
| 2615 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2616 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2617 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2618 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2619 | if (level == 6) { |
| 2620 | int qd_idx = sh->qd_idx; |
| 2621 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2622 | |
| 2623 | set_bit(R5_LOCKED, &dev->flags); |
| 2624 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2625 | s->locked++; |
| 2626 | } |
| 2627 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2628 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2629 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2630 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2631 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2632 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2633 | /* |
| 2634 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2635 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | * The bi_next chain must be in order. |
| 2637 | */ |
| 2638 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, int forwrite) |
| 2639 | { |
| 2640 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2641 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2642 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2643 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2644 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2645 | (unsigned long long)bi->bi_iter.bi_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | (unsigned long long)sh->sector); |
| 2647 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2648 | /* |
| 2649 | * If several bio share a stripe. The bio bi_phys_segments acts as a |
| 2650 | * reference count to avoid race. The reference count should already be |
| 2651 | * increased before this function is called (for example, in |
| 2652 | * make_request()), so other bio sharing this stripe will not free the |
| 2653 | * stripe. If a stripe is owned by one stripe, the stripe lock will |
| 2654 | * protect it. |
| 2655 | */ |
| 2656 | spin_lock_irq(&sh->stripe_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2657 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2659 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2660 | firstwrite = 1; |
| 2661 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2662 | bip = &sh->dev[dd_idx].toread; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2663 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { |
| 2664 | if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2665 | goto overlap; |
| 2666 | bip = & (*bip)->bi_next; |
| 2667 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2668 | if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | goto overlap; |
| 2670 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2671 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | if (*bip) |
| 2673 | bi->bi_next = *bip; |
| 2674 | *bip = bi; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2675 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2676 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2677 | if (forwrite) { |
| 2678 | /* check if page is covered */ |
| 2679 | sector_t sector = sh->dev[dd_idx].sector; |
| 2680 | for (bi=sh->dev[dd_idx].towrite; |
| 2681 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2682 | bi && bi->bi_iter.bi_sector <= sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 2684 | if (bio_end_sector(bi) >= sector) |
| 2685 | sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2686 | } |
| 2687 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
| 2688 | set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags); |
| 2689 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2690 | |
| 2691 | pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2692 | (unsigned long long)(*bip)->bi_iter.bi_sector, |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2693 | (unsigned long long)sh->sector, dd_idx); |
NeilBrown | b97390a | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2694 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2695 | |
| 2696 | if (conf->mddev->bitmap && firstwrite) { |
| 2697 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 2698 | STRIPE_SECTORS, 0); |
| 2699 | sh->bm_seq = conf->seq_flush+1; |
| 2700 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 2701 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2702 | return 1; |
| 2703 | |
| 2704 | overlap: |
| 2705 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2706 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | return 0; |
| 2708 | } |
| 2709 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2710 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2711 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2712 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2713 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2714 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2715 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2716 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2717 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2718 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2719 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 2720 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2721 | raid5_compute_sector(conf, |
| 2722 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2723 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2724 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2725 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 2726 | } |
| 2727 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2728 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2729 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2730 | struct stripe_head_state *s, int disks, |
| 2731 | struct bio **return_bi) |
| 2732 | { |
| 2733 | int i; |
| 2734 | for (i = disks; i--; ) { |
| 2735 | struct bio *bi; |
| 2736 | int bitmap_end = 0; |
| 2737 | |
| 2738 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 2739 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2740 | rcu_read_lock(); |
| 2741 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 2742 | if (rdev && test_bit(In_sync, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2743 | atomic_inc(&rdev->nr_pending); |
| 2744 | else |
| 2745 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2746 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2747 | if (rdev) { |
| 2748 | if (!rdev_set_badblocks( |
| 2749 | rdev, |
| 2750 | sh->sector, |
| 2751 | STRIPE_SECTORS, 0)) |
| 2752 | md_error(conf->mddev, rdev); |
| 2753 | rdev_dec_pending(rdev, conf->mddev); |
| 2754 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2755 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2756 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2757 | /* fail all writes first */ |
| 2758 | bi = sh->dev[i].towrite; |
| 2759 | sh->dev[i].towrite = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2760 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 1ed850f | 2012-10-11 13:50:13 +1100 | [diff] [blame] | 2761 | if (bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2762 | bitmap_end = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2763 | |
| 2764 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2765 | wake_up(&conf->wait_for_overlap); |
| 2766 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2767 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2768 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2769 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
| 2770 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2771 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2772 | md_write_end(conf->mddev); |
| 2773 | bi->bi_next = *return_bi; |
| 2774 | *return_bi = bi; |
| 2775 | } |
| 2776 | bi = nextbi; |
| 2777 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2778 | if (bitmap_end) |
| 2779 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2780 | STRIPE_SECTORS, 0, 0); |
| 2781 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2782 | /* and fail all 'written' */ |
| 2783 | bi = sh->dev[i].written; |
| 2784 | sh->dev[i].written = NULL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2785 | if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { |
| 2786 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 2787 | sh->dev[i].page = sh->dev[i].orig_page; |
| 2788 | } |
| 2789 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2790 | if (bi) bitmap_end = 1; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2791 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2792 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2793 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
| 2794 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2795 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2796 | md_write_end(conf->mddev); |
| 2797 | bi->bi_next = *return_bi; |
| 2798 | *return_bi = bi; |
| 2799 | } |
| 2800 | bi = bi2; |
| 2801 | } |
| 2802 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 2803 | /* fail any reads if this device is non-operational and |
| 2804 | * the data has not reached the cache yet. |
| 2805 | */ |
| 2806 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
| 2807 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 2808 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2809 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2810 | bi = sh->dev[i].toread; |
| 2811 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 2812 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2813 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 2814 | wake_up(&conf->wait_for_overlap); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2815 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2816 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 2817 | struct bio *nextbi = |
| 2818 | r5_next_bio(bi, sh->dev[i].sector); |
| 2819 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2820 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2821 | bi->bi_next = *return_bi; |
| 2822 | *return_bi = bi; |
| 2823 | } |
| 2824 | bi = nextbi; |
| 2825 | } |
| 2826 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2827 | if (bitmap_end) |
| 2828 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 2829 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2830 | /* If we were in the middle of a write the parity block might |
| 2831 | * still be locked - so just clear all R5_LOCKED flags |
| 2832 | */ |
| 2833 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2834 | } |
| 2835 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2836 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 2837 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 2838 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2839 | } |
| 2840 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2841 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2842 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2843 | struct stripe_head_state *s) |
| 2844 | { |
| 2845 | int abort = 0; |
| 2846 | int i; |
| 2847 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2848 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 2849 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 2850 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2851 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2852 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2853 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2854 | * Don't even need to abort as that is handled elsewhere |
| 2855 | * if needed, and not always wanted e.g. if there is a known |
| 2856 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2857 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2858 | * non-sync devices, or abort the recovery |
| 2859 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2860 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 2861 | /* During recovery devices cannot be removed, so |
| 2862 | * locking and refcounting of rdevs is not needed |
| 2863 | */ |
| 2864 | for (i = 0; i < conf->raid_disks; i++) { |
| 2865 | struct md_rdev *rdev = conf->disks[i].rdev; |
| 2866 | if (rdev |
| 2867 | && !test_bit(Faulty, &rdev->flags) |
| 2868 | && !test_bit(In_sync, &rdev->flags) |
| 2869 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2870 | STRIPE_SECTORS, 0)) |
| 2871 | abort = 1; |
| 2872 | rdev = conf->disks[i].replacement; |
| 2873 | if (rdev |
| 2874 | && !test_bit(Faulty, &rdev->flags) |
| 2875 | && !test_bit(In_sync, &rdev->flags) |
| 2876 | && !rdev_set_badblocks(rdev, sh->sector, |
| 2877 | STRIPE_SECTORS, 0)) |
| 2878 | abort = 1; |
| 2879 | } |
| 2880 | if (abort) |
| 2881 | conf->recovery_disabled = |
| 2882 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2883 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 2884 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 2885 | } |
| 2886 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2887 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 2888 | { |
| 2889 | struct md_rdev *rdev; |
| 2890 | int rv = 0; |
| 2891 | /* Doing recovery so rcu locking not required */ |
| 2892 | rdev = sh->raid_conf->disks[disk_idx].replacement; |
| 2893 | if (rdev |
| 2894 | && !test_bit(Faulty, &rdev->flags) |
| 2895 | && !test_bit(In_sync, &rdev->flags) |
| 2896 | && (rdev->recovery_offset <= sh->sector |
| 2897 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 2898 | rv = 1; |
| 2899 | |
| 2900 | return rv; |
| 2901 | } |
| 2902 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2903 | /* 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] | 2904 | * to be read or computed to satisfy a request. |
| 2905 | * |
| 2906 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2907 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2908 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2909 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 2910 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2911 | { |
| 2912 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2913 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 2914 | &sh->dev[s->failed_num[1]] }; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2915 | |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2916 | /* is the data in this block needed, and can we get it? */ |
| 2917 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 2918 | !test_bit(R5_UPTODATE, &dev->flags) && |
| 2919 | (dev->toread || |
| 2920 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)) || |
| 2921 | s->syncing || s->expanding || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2922 | (s->replacing && want_replace(sh, disk_idx)) || |
NeilBrown | 5d35e09 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2923 | (s->failed >= 1 && fdev[0]->toread) || |
| 2924 | (s->failed >= 2 && fdev[1]->toread) || |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2925 | (sh->raid_conf->level <= 5 && s->failed && fdev[0]->towrite && |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 2926 | (!test_bit(R5_Insync, &dev->flags) || test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) && |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2927 | !test_bit(R5_OVERWRITE, &fdev[0]->flags)) || |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 2928 | (sh->raid_conf->level == 6 && s->failed && s->to_write && |
NeilBrown | a40687f | 2014-08-13 09:48:45 +1000 | [diff] [blame] | 2929 | s->to_write - s->non_overwrite < sh->raid_conf->raid_disks - 2 && |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 2930 | (!test_bit(R5_Insync, &dev->flags) || test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2931 | /* we would like to get this block, possibly by computing it, |
| 2932 | * otherwise read it if the backing disk is insync |
| 2933 | */ |
| 2934 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 2935 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
| 2936 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 2937 | (s->failed && (disk_idx == s->failed_num[0] || |
| 2938 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2939 | /* have disk failed, and we're requested to fetch it; |
| 2940 | * do compute it |
| 2941 | */ |
| 2942 | pr_debug("Computing stripe %llu block %d\n", |
| 2943 | (unsigned long long)sh->sector, disk_idx); |
| 2944 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2945 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2946 | set_bit(R5_Wantcompute, &dev->flags); |
| 2947 | sh->ops.target = disk_idx; |
| 2948 | sh->ops.target2 = -1; /* no 2nd target */ |
| 2949 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2950 | /* Careful: from this point on 'uptodate' is in the eye |
| 2951 | * of raid_run_ops which services 'compute' operations |
| 2952 | * before writes. R5_Wantcompute flags a block that will |
| 2953 | * be R5_UPTODATE by the time it is needed for a |
| 2954 | * subsequent operation. |
| 2955 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2956 | s->uptodate++; |
| 2957 | return 1; |
| 2958 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 2959 | /* Computing 2-failure is *very* expensive; only |
| 2960 | * do it if failed >= 2 |
| 2961 | */ |
| 2962 | int other; |
| 2963 | for (other = disks; other--; ) { |
| 2964 | if (other == disk_idx) |
| 2965 | continue; |
| 2966 | if (!test_bit(R5_UPTODATE, |
| 2967 | &sh->dev[other].flags)) |
| 2968 | break; |
| 2969 | } |
| 2970 | BUG_ON(other < 0); |
| 2971 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 2972 | (unsigned long long)sh->sector, |
| 2973 | disk_idx, other); |
| 2974 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 2975 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 2976 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 2977 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 2978 | sh->ops.target = disk_idx; |
| 2979 | sh->ops.target2 = other; |
| 2980 | s->uptodate += 2; |
| 2981 | s->req_compute = 1; |
| 2982 | return 1; |
| 2983 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 2984 | set_bit(R5_LOCKED, &dev->flags); |
| 2985 | set_bit(R5_Wantread, &dev->flags); |
| 2986 | s->locked++; |
| 2987 | pr_debug("Reading block %d (sync=%d)\n", |
| 2988 | disk_idx, s->syncing); |
| 2989 | } |
| 2990 | } |
| 2991 | |
| 2992 | return 0; |
| 2993 | } |
| 2994 | |
| 2995 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2996 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2997 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2998 | static void handle_stripe_fill(struct stripe_head *sh, |
| 2999 | struct stripe_head_state *s, |
| 3000 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3001 | { |
| 3002 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3003 | |
| 3004 | /* look for blocks to read/compute, skip this if a compute |
| 3005 | * is already in flight, or if the stripe contents are in the |
| 3006 | * midst of changing due to a write |
| 3007 | */ |
| 3008 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 3009 | !sh->reconstruct_state) |
| 3010 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3011 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3012 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3013 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3014 | } |
| 3015 | |
| 3016 | |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3017 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3018 | * any written block on an uptodate or failed drive can be returned. |
| 3019 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 3020 | * never LOCKED, so we don't need to test 'failed' directly. |
| 3021 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3022 | static void handle_stripe_clean_event(struct r5conf *conf, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3023 | struct stripe_head *sh, int disks, struct bio **return_bi) |
| 3024 | { |
| 3025 | int i; |
| 3026 | struct r5dev *dev; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3027 | int discard_pending = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3028 | |
| 3029 | for (i = disks; i--; ) |
| 3030 | if (sh->dev[i].written) { |
| 3031 | dev = &sh->dev[i]; |
| 3032 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3033 | (test_bit(R5_UPTODATE, &dev->flags) || |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3034 | test_bit(R5_Discard, &dev->flags) || |
| 3035 | test_bit(R5_SkipCopy, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3036 | /* We can return any write requests */ |
| 3037 | struct bio *wbi, *wbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3038 | pr_debug("Return write for disc %d\n", i); |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 3039 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
| 3040 | clear_bit(R5_UPTODATE, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3041 | if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { |
| 3042 | WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); |
| 3043 | dev->page = dev->orig_page; |
| 3044 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3045 | wbi = dev->written; |
| 3046 | dev->written = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3047 | while (wbi && wbi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3048 | dev->sector + STRIPE_SECTORS) { |
| 3049 | wbi2 = r5_next_bio(wbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3050 | if (!raid5_dec_bi_active_stripes(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3051 | md_write_end(conf->mddev); |
| 3052 | wbi->bi_next = *return_bi; |
| 3053 | *return_bi = wbi; |
| 3054 | } |
| 3055 | wbi = wbi2; |
| 3056 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3057 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3058 | STRIPE_SECTORS, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3059 | !test_bit(STRIPE_DEGRADED, &sh->state), |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3060 | 0); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3061 | } else if (test_bit(R5_Discard, &dev->flags)) |
| 3062 | discard_pending = 1; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3063 | WARN_ON(test_bit(R5_SkipCopy, &dev->flags)); |
| 3064 | WARN_ON(dev->page != dev->orig_page); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3065 | } |
| 3066 | if (!discard_pending && |
| 3067 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { |
| 3068 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 3069 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
| 3070 | if (sh->qd_idx >= 0) { |
| 3071 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 3072 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); |
| 3073 | } |
| 3074 | /* now that discard is done we can proceed with any sync */ |
| 3075 | clear_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 3076 | /* |
| 3077 | * SCSI discard will change some bio fields and the stripe has |
| 3078 | * no updated data, so remove it from hash list and the stripe |
| 3079 | * will be reinitialized |
| 3080 | */ |
| 3081 | spin_lock_irq(&conf->device_lock); |
| 3082 | remove_hash(sh); |
| 3083 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3084 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
| 3085 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3086 | |
| 3087 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3088 | |
| 3089 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3090 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3091 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3092 | } |
| 3093 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3094 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3095 | struct stripe_head *sh, |
| 3096 | struct stripe_head_state *s, |
| 3097 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3098 | { |
| 3099 | int rmw = 0, rcw = 0, i; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3100 | sector_t recovery_cp = conf->mddev->recovery_cp; |
| 3101 | |
| 3102 | /* RAID6 requires 'rcw' in current implementation. |
| 3103 | * Otherwise, check whether resync is now happening or should start. |
| 3104 | * If yes, then the array is dirty (after unclean shutdown or |
| 3105 | * initial creation), so parity in some stripes might be inconsistent. |
| 3106 | * In this case, we need to always do reconstruct-write, to ensure |
| 3107 | * that in case of drive failure or read-error correction, we |
| 3108 | * generate correct data from the parity. |
| 3109 | */ |
| 3110 | if (conf->max_degraded == 2 || |
| 3111 | (recovery_cp < MaxSector && sh->sector >= recovery_cp)) { |
| 3112 | /* Calculate the real rcw later - for now make it |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3113 | * look like rcw is cheaper |
| 3114 | */ |
| 3115 | rcw = 1; rmw = 2; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3116 | pr_debug("force RCW max_degraded=%u, recovery_cp=%llu sh->sector=%llu\n", |
| 3117 | conf->max_degraded, (unsigned long long)recovery_cp, |
| 3118 | (unsigned long long)sh->sector); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3119 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3120 | /* would I have to read this buffer for read_modify_write */ |
| 3121 | struct r5dev *dev = &sh->dev[i]; |
| 3122 | if ((dev->towrite || i == sh->pd_idx) && |
| 3123 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3124 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 3125 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3126 | if (test_bit(R5_Insync, &dev->flags)) |
| 3127 | rmw++; |
| 3128 | else |
| 3129 | rmw += 2*disks; /* cannot read it */ |
| 3130 | } |
| 3131 | /* Would I have to read this buffer for reconstruct_write */ |
| 3132 | if (!test_bit(R5_OVERWRITE, &dev->flags) && i != sh->pd_idx && |
| 3133 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3134 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 3135 | test_bit(R5_Wantcompute, &dev->flags))) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3136 | if (test_bit(R5_Insync, &dev->flags)) |
| 3137 | rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3138 | else |
| 3139 | rcw += 2*disks; |
| 3140 | } |
| 3141 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3142 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3143 | (unsigned long long)sh->sector, rmw, rcw); |
| 3144 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3145 | if (rmw < rcw && rmw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3146 | /* prefer read-modify-write, but need to get some data */ |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 3147 | if (conf->mddev->queue) |
| 3148 | blk_add_trace_msg(conf->mddev->queue, |
| 3149 | "raid5 rmw %llu %d", |
| 3150 | (unsigned long long)sh->sector, rmw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3151 | for (i = disks; i--; ) { |
| 3152 | struct r5dev *dev = &sh->dev[i]; |
| 3153 | if ((dev->towrite || i == sh->pd_idx) && |
| 3154 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3155 | !(test_bit(R5_UPTODATE, &dev->flags) || |
| 3156 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3157 | test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3158 | if (test_bit(STRIPE_PREREAD_ACTIVE, |
| 3159 | &sh->state)) { |
| 3160 | pr_debug("Read_old block %d for r-m-w\n", |
| 3161 | i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3162 | set_bit(R5_LOCKED, &dev->flags); |
| 3163 | set_bit(R5_Wantread, &dev->flags); |
| 3164 | s->locked++; |
| 3165 | } else { |
| 3166 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3167 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3168 | } |
| 3169 | } |
| 3170 | } |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3171 | } |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3172 | if (rcw <= rmw && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3173 | /* want reconstruct write, but need to get some data */ |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3174 | int qread =0; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3175 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3176 | for (i = disks; i--; ) { |
| 3177 | struct r5dev *dev = &sh->dev[i]; |
| 3178 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3179 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3180 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3181 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3182 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 3183 | rcw++; |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3184 | if (test_bit(R5_Insync, &dev->flags) && |
| 3185 | test_bit(STRIPE_PREREAD_ACTIVE, |
| 3186 | &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3187 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3188 | "%d for Reconstruct\n", i); |
| 3189 | set_bit(R5_LOCKED, &dev->flags); |
| 3190 | set_bit(R5_Wantread, &dev->flags); |
| 3191 | s->locked++; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3192 | qread++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3193 | } else { |
| 3194 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3195 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3196 | } |
| 3197 | } |
| 3198 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 3199 | if (rcw && conf->mddev->queue) |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3200 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
| 3201 | (unsigned long long)sh->sector, |
| 3202 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3203 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3204 | /* now if nothing is locked, and if we have enough data, |
| 3205 | * we can start a write request |
| 3206 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3207 | /* since handle_stripe can be called at any time we need to handle the |
| 3208 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3209 | * subsequent call wants to start a write request. raid_run_ops only |
| 3210 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3211 | * simultaneously. If this is not the case then new writes need to be |
| 3212 | * held off until the compute completes. |
| 3213 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3214 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 3215 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
| 3216 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3217 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3218 | } |
| 3219 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3220 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3221 | struct stripe_head_state *s, int disks) |
| 3222 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3223 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3224 | |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3225 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3226 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3227 | switch (sh->check_state) { |
| 3228 | case check_state_idle: |
| 3229 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3230 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3231 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3232 | sh->check_state = check_state_run; |
| 3233 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3234 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3235 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3236 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3237 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3238 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3239 | /* fall through */ |
| 3240 | case check_state_compute_result: |
| 3241 | sh->check_state = check_state_idle; |
| 3242 | if (!dev) |
| 3243 | dev = &sh->dev[sh->pd_idx]; |
| 3244 | |
| 3245 | /* check that a write has not made the stripe insync */ |
| 3246 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3247 | break; |
| 3248 | |
| 3249 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3250 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 3251 | BUG_ON(s->uptodate != disks); |
| 3252 | |
| 3253 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3254 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3255 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3256 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3257 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3258 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3259 | break; |
| 3260 | case check_state_run: |
| 3261 | break; /* we will be called again upon completion */ |
| 3262 | case check_state_check_result: |
| 3263 | sh->check_state = check_state_idle; |
| 3264 | |
| 3265 | /* if a failure occurred during the check operation, leave |
| 3266 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 3267 | */ |
| 3268 | if (s->failed) |
| 3269 | break; |
| 3270 | |
| 3271 | /* handle a successful check operation, if parity is correct |
| 3272 | * we are done. Otherwise update the mismatch count and repair |
| 3273 | * parity if !MD_RECOVERY_CHECK |
| 3274 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 3275 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3276 | /* parity is correct (on disc, |
| 3277 | * not in buffer any more) |
| 3278 | */ |
| 3279 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3280 | else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 3281 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3282 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3283 | /* don't try to repair!! */ |
| 3284 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3285 | else { |
| 3286 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3287 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3288 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3289 | set_bit(R5_Wantcompute, |
| 3290 | &sh->dev[sh->pd_idx].flags); |
| 3291 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3292 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3293 | s->uptodate++; |
| 3294 | } |
| 3295 | } |
| 3296 | break; |
| 3297 | case check_state_compute_run: |
| 3298 | break; |
| 3299 | default: |
| 3300 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3301 | __func__, sh->check_state, |
| 3302 | (unsigned long long) sh->sector); |
| 3303 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3304 | } |
| 3305 | } |
| 3306 | |
| 3307 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3308 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3309 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3310 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3311 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3312 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3313 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3314 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3315 | |
| 3316 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3317 | |
| 3318 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3319 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3320 | /* Want to check and possibly repair P and Q. |
| 3321 | * However there could be one 'failed' device, in which |
| 3322 | * case we can only check one of them, possibly using the |
| 3323 | * other to generate missing data |
| 3324 | */ |
| 3325 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3326 | switch (sh->check_state) { |
| 3327 | case check_state_idle: |
| 3328 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3329 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3330 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3331 | * makes sense to check P (If anything else were failed, |
| 3332 | * we would have used P to recreate it). |
| 3333 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3334 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3335 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3336 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3337 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3338 | * anything, so it makes sense to check it |
| 3339 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3340 | if (sh->check_state == check_state_run) |
| 3341 | sh->check_state = check_state_run_pq; |
| 3342 | else |
| 3343 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3344 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3345 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3346 | /* discard potentially stale zero_sum_result */ |
| 3347 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3348 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3349 | if (sh->check_state == check_state_run) { |
| 3350 | /* async_xor_zero_sum destroys the contents of P */ |
| 3351 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 3352 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3353 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3354 | if (sh->check_state >= check_state_run && |
| 3355 | sh->check_state <= check_state_run_pq) { |
| 3356 | /* async_syndrome_zero_sum preserves P and Q, so |
| 3357 | * no need to mark them !uptodate here |
| 3358 | */ |
| 3359 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 3360 | break; |
| 3361 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3362 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3363 | /* we have 2-disk failure */ |
| 3364 | BUG_ON(s->failed != 2); |
| 3365 | /* fall through */ |
| 3366 | case check_state_compute_result: |
| 3367 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3368 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3369 | /* check that a write has not made the stripe insync */ |
| 3370 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3371 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3372 | |
| 3373 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3374 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3375 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3376 | 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] | 3377 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3378 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3379 | s->locked++; |
| 3380 | set_bit(R5_LOCKED, &dev->flags); |
| 3381 | set_bit(R5_Wantwrite, &dev->flags); |
| 3382 | } |
| 3383 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3384 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3385 | s->locked++; |
| 3386 | set_bit(R5_LOCKED, &dev->flags); |
| 3387 | set_bit(R5_Wantwrite, &dev->flags); |
| 3388 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3389 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3390 | dev = &sh->dev[pd_idx]; |
| 3391 | s->locked++; |
| 3392 | set_bit(R5_LOCKED, &dev->flags); |
| 3393 | set_bit(R5_Wantwrite, &dev->flags); |
| 3394 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3395 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3396 | dev = &sh->dev[qd_idx]; |
| 3397 | s->locked++; |
| 3398 | set_bit(R5_LOCKED, &dev->flags); |
| 3399 | set_bit(R5_Wantwrite, &dev->flags); |
| 3400 | } |
| 3401 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3402 | |
| 3403 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3404 | break; |
| 3405 | case check_state_run: |
| 3406 | case check_state_run_q: |
| 3407 | case check_state_run_pq: |
| 3408 | break; /* we will be called again upon completion */ |
| 3409 | case check_state_check_result: |
| 3410 | sh->check_state = check_state_idle; |
| 3411 | |
| 3412 | /* handle a successful check operation, if parity is correct |
| 3413 | * we are done. Otherwise update the mismatch count and repair |
| 3414 | * parity if !MD_RECOVERY_CHECK |
| 3415 | */ |
| 3416 | if (sh->ops.zero_sum_result == 0) { |
| 3417 | /* both parities are correct */ |
| 3418 | if (!s->failed) |
| 3419 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3420 | else { |
| 3421 | /* in contrast to the raid5 case we can validate |
| 3422 | * parity, but still have a failure to write |
| 3423 | * back |
| 3424 | */ |
| 3425 | sh->check_state = check_state_compute_result; |
| 3426 | /* Returning at this point means that we may go |
| 3427 | * off and bring p and/or q uptodate again so |
| 3428 | * we make sure to check zero_sum_result again |
| 3429 | * to verify if p or q need writeback |
| 3430 | */ |
| 3431 | } |
| 3432 | } else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 3433 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3434 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3435 | /* don't try to repair!! */ |
| 3436 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3437 | else { |
| 3438 | int *target = &sh->ops.target; |
| 3439 | |
| 3440 | sh->ops.target = -1; |
| 3441 | sh->ops.target2 = -1; |
| 3442 | sh->check_state = check_state_compute_run; |
| 3443 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3444 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3445 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3446 | set_bit(R5_Wantcompute, |
| 3447 | &sh->dev[pd_idx].flags); |
| 3448 | *target = pd_idx; |
| 3449 | target = &sh->ops.target2; |
| 3450 | s->uptodate++; |
| 3451 | } |
| 3452 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3453 | set_bit(R5_Wantcompute, |
| 3454 | &sh->dev[qd_idx].flags); |
| 3455 | *target = qd_idx; |
| 3456 | s->uptodate++; |
| 3457 | } |
| 3458 | } |
| 3459 | } |
| 3460 | break; |
| 3461 | case check_state_compute_run: |
| 3462 | break; |
| 3463 | default: |
| 3464 | printk(KERN_ERR "%s: unknown check_state: %d sector: %llu\n", |
| 3465 | __func__, sh->check_state, |
| 3466 | (unsigned long long) sh->sector); |
| 3467 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3468 | } |
| 3469 | } |
| 3470 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3471 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3472 | { |
| 3473 | int i; |
| 3474 | |
| 3475 | /* We have read all the blocks in this stripe and now we need to |
| 3476 | * copy some of them into a target stripe for expand. |
| 3477 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3478 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3479 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3480 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3481 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3482 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3483 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3484 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3485 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3486 | sector_t bn = compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3487 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 3488 | &dd_idx, NULL); |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 3489 | sh2 = get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3490 | if (sh2 == NULL) |
| 3491 | /* so far only the early blocks of this stripe |
| 3492 | * have been requested. When later blocks |
| 3493 | * get requested, we will try again |
| 3494 | */ |
| 3495 | continue; |
| 3496 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 3497 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 3498 | /* must have already done this block */ |
| 3499 | release_stripe(sh2); |
| 3500 | continue; |
| 3501 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3502 | |
| 3503 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3504 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3505 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 3506 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 3507 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3508 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3509 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 3510 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 3511 | for (j = 0; j < conf->raid_disks; j++) |
| 3512 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3513 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3514 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 3515 | break; |
| 3516 | if (j == conf->raid_disks) { |
| 3517 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 3518 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 3519 | } |
| 3520 | release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3521 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3522 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 3523 | /* done submitting copies, wait for them to complete */ |
NeilBrown | 749586b | 2012-11-20 14:11:15 +1100 | [diff] [blame] | 3524 | async_tx_quiesce(&tx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3525 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3526 | |
| 3527 | /* |
| 3528 | * handle_stripe - do things to a stripe. |
| 3529 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3530 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 3531 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3532 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3533 | * return some read requests which now have data |
| 3534 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3535 | * schedule a read on some buffers |
| 3536 | * schedule a write of some buffers |
| 3537 | * return confirmation of parity correctness |
| 3538 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3539 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3540 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3541 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3542 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3543 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3544 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3545 | struct r5dev *dev; |
| 3546 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3547 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3548 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3549 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3550 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3551 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3552 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3553 | s->failed_num[0] = -1; |
| 3554 | s->failed_num[1] = -1; |
| 3555 | |
| 3556 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3557 | rcu_read_lock(); |
| 3558 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3559 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3560 | sector_t first_bad; |
| 3561 | int bad_sectors; |
| 3562 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3563 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3564 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3565 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3566 | 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] | 3567 | i, dev->flags, |
| 3568 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 3569 | /* maybe we can reply to a read |
| 3570 | * |
| 3571 | * new wantfill requests are only permitted while |
| 3572 | * ops_complete_biofill is guaranteed to be inactive |
| 3573 | */ |
| 3574 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 3575 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 3576 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3577 | |
| 3578 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3579 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 3580 | s->locked++; |
| 3581 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 3582 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3583 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3584 | s->compute++; |
| 3585 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 3586 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3587 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3588 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3589 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3590 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3591 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3592 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3593 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3594 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3595 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3596 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3597 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3598 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3599 | /* Prefer to use the replacement for reads, but only |
| 3600 | * if it is recovered enough and has no bad blocks. |
| 3601 | */ |
| 3602 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 3603 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 3604 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 3605 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3606 | &first_bad, &bad_sectors)) |
| 3607 | set_bit(R5_ReadRepl, &dev->flags); |
| 3608 | else { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3609 | if (rdev) |
| 3610 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3611 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 3612 | clear_bit(R5_ReadRepl, &dev->flags); |
| 3613 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 3614 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 3615 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3616 | if (rdev) { |
| 3617 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 3618 | &first_bad, &bad_sectors); |
| 3619 | if (s->blocked_rdev == NULL |
| 3620 | && (test_bit(Blocked, &rdev->flags) |
| 3621 | || is_bad < 0)) { |
| 3622 | if (is_bad < 0) |
| 3623 | set_bit(BlockedBadBlocks, |
| 3624 | &rdev->flags); |
| 3625 | s->blocked_rdev = rdev; |
| 3626 | atomic_inc(&rdev->nr_pending); |
| 3627 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3628 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3629 | clear_bit(R5_Insync, &dev->flags); |
| 3630 | if (!rdev) |
| 3631 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3632 | else if (is_bad) { |
| 3633 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3634 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 3635 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3636 | /* treat as in-sync, but with a read error |
| 3637 | * which we can now try to correct |
| 3638 | */ |
| 3639 | set_bit(R5_Insync, &dev->flags); |
| 3640 | set_bit(R5_ReadError, &dev->flags); |
| 3641 | } |
| 3642 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3643 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3644 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3645 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 3646 | set_bit(R5_Insync, &dev->flags); |
| 3647 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 3648 | test_bit(R5_Expanded, &dev->flags)) |
| 3649 | /* If we've reshaped into here, we assume it is Insync. |
| 3650 | * We will shortly update recovery_offset to make |
| 3651 | * it official. |
| 3652 | */ |
| 3653 | set_bit(R5_Insync, &dev->flags); |
| 3654 | |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 3655 | if (test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3656 | /* This flag does not apply to '.replacement' |
| 3657 | * only to .rdev, so make sure to check that*/ |
| 3658 | struct md_rdev *rdev2 = rcu_dereference( |
| 3659 | conf->disks[i].rdev); |
| 3660 | if (rdev2 == rdev) |
| 3661 | clear_bit(R5_Insync, &dev->flags); |
| 3662 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3663 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3664 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3665 | } else |
| 3666 | clear_bit(R5_WriteError, &dev->flags); |
| 3667 | } |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 3668 | if (test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3669 | /* This flag does not apply to '.replacement' |
| 3670 | * only to .rdev, so make sure to check that*/ |
| 3671 | struct md_rdev *rdev2 = rcu_dereference( |
| 3672 | conf->disks[i].rdev); |
| 3673 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3674 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 3675 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 3676 | } else |
| 3677 | clear_bit(R5_MadeGood, &dev->flags); |
| 3678 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3679 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 3680 | struct md_rdev *rdev2 = rcu_dereference( |
| 3681 | conf->disks[i].replacement); |
| 3682 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 3683 | s->handle_bad_blocks = 1; |
| 3684 | atomic_inc(&rdev2->nr_pending); |
| 3685 | } else |
| 3686 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 3687 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3688 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3689 | /* The ReadError flag will just be confusing now */ |
| 3690 | clear_bit(R5_ReadError, &dev->flags); |
| 3691 | clear_bit(R5_ReWrite, &dev->flags); |
| 3692 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3693 | if (test_bit(R5_ReadError, &dev->flags)) |
| 3694 | clear_bit(R5_Insync, &dev->flags); |
| 3695 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3696 | if (s->failed < 2) |
| 3697 | s->failed_num[s->failed] = i; |
| 3698 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3699 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 3700 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 3701 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3702 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3703 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 3704 | /* If there is a failed device being replaced, |
| 3705 | * we must be recovering. |
| 3706 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3707 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3708 | * else we can only be replacing |
| 3709 | * sync and recovery both need to read all devices, and so |
| 3710 | * use the same flag. |
| 3711 | */ |
| 3712 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 3713 | sh->sector >= conf->mddev->recovery_cp || |
| 3714 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3715 | s->syncing = 1; |
| 3716 | else |
| 3717 | s->replacing = 1; |
| 3718 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3719 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3720 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 3721 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3722 | static void handle_stripe(struct stripe_head *sh) |
| 3723 | { |
| 3724 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3725 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3726 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3727 | int prexor; |
| 3728 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3729 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3730 | |
| 3731 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 3732 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3733 | /* already being handled, ensure it gets handled |
| 3734 | * again when current action finishes */ |
| 3735 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3736 | return; |
| 3737 | } |
| 3738 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3739 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3740 | spin_lock(&sh->stripe_lock); |
| 3741 | /* Cannot process 'sync' concurrently with 'discard' */ |
| 3742 | if (!test_bit(STRIPE_DISCARD, &sh->state) && |
| 3743 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 3744 | set_bit(STRIPE_SYNCING, &sh->state); |
| 3745 | clear_bit(STRIPE_INSYNC, &sh->state); |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 3746 | clear_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3747 | } |
| 3748 | spin_unlock(&sh->stripe_lock); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3749 | } |
| 3750 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 3751 | |
| 3752 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 3753 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 3754 | (unsigned long long)sh->sector, sh->state, |
| 3755 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 3756 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 3757 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3758 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3759 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3760 | if (s.handle_bad_blocks) { |
| 3761 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3762 | goto finish; |
| 3763 | } |
| 3764 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3765 | if (unlikely(s.blocked_rdev)) { |
| 3766 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3767 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3768 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3769 | goto finish; |
| 3770 | } |
| 3771 | /* There is nothing for the blocked_rdev to block */ |
| 3772 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 3773 | s.blocked_rdev = NULL; |
| 3774 | } |
| 3775 | |
| 3776 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 3777 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 3778 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 3779 | } |
| 3780 | |
| 3781 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 3782 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 3783 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 3784 | s.failed_num[0], s.failed_num[1]); |
| 3785 | /* check if the array has lost more than max_degraded devices and, |
| 3786 | * if so, some requests might need to be failed. |
| 3787 | */ |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3788 | if (s.failed > conf->max_degraded) { |
| 3789 | sh->check_state = 0; |
| 3790 | sh->reconstruct_state = 0; |
| 3791 | if (s.to_read+s.to_write+s.written) |
| 3792 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3793 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 3794 | handle_failed_sync(conf, sh, &s); |
| 3795 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3796 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3797 | /* Now we check to see if any write operations have recently |
| 3798 | * completed |
| 3799 | */ |
| 3800 | prexor = 0; |
| 3801 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 3802 | prexor = 1; |
| 3803 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 3804 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 3805 | sh->reconstruct_state = reconstruct_state_idle; |
| 3806 | |
| 3807 | /* All the 'written' buffers and the parity block are ready to |
| 3808 | * be written back to disk |
| 3809 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3810 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 3811 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3812 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3813 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 3814 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3815 | for (i = disks; i--; ) { |
| 3816 | struct r5dev *dev = &sh->dev[i]; |
| 3817 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 3818 | (i == sh->pd_idx || i == sh->qd_idx || |
| 3819 | dev->written)) { |
| 3820 | pr_debug("Writing block %d\n", i); |
| 3821 | set_bit(R5_Wantwrite, &dev->flags); |
| 3822 | if (prexor) |
| 3823 | continue; |
NeilBrown | 9c4bdf6 | 2014-08-13 09:57:07 +1000 | [diff] [blame] | 3824 | if (s.failed > 1) |
| 3825 | continue; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3826 | if (!test_bit(R5_Insync, &dev->flags) || |
| 3827 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 3828 | s.failed == 0)) |
| 3829 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3830 | } |
| 3831 | } |
| 3832 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3833 | s.dec_preread_active = 1; |
| 3834 | } |
| 3835 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 3836 | /* |
| 3837 | * might be able to return some write requests if the parity blocks |
| 3838 | * are safe, or on a failed drive |
| 3839 | */ |
| 3840 | pdev = &sh->dev[sh->pd_idx]; |
| 3841 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 3842 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 3843 | qdev = &sh->dev[sh->qd_idx]; |
| 3844 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 3845 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 3846 | || conf->level < 6; |
| 3847 | |
| 3848 | if (s.written && |
| 3849 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 3850 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 3851 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 3852 | test_bit(R5_Discard, &pdev->flags))))) && |
| 3853 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 3854 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 3855 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 3856 | test_bit(R5_Discard, &qdev->flags)))))) |
| 3857 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 3858 | |
| 3859 | /* Now we might consider reading some blocks, either to check/generate |
| 3860 | * parity, or to satisfy requests |
| 3861 | * or to load a block that is being partially written. |
| 3862 | */ |
| 3863 | if (s.to_read || s.non_overwrite |
| 3864 | || (conf->level == 6 && s.to_write && s.failed) |
| 3865 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 3866 | || s.replacing |
| 3867 | || s.expanding) |
| 3868 | handle_stripe_fill(sh, &s, disks); |
| 3869 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3870 | /* Now to consider new write requests and what else, if anything |
| 3871 | * should be read. We do not handle new writes when: |
| 3872 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 3873 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 3874 | * block. |
| 3875 | */ |
| 3876 | if (s.to_write && !sh->reconstruct_state && !sh->check_state) |
| 3877 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 3878 | |
| 3879 | /* maybe we need to check and possibly fix the parity for this stripe |
| 3880 | * Any reads will already have been scheduled, so we just see if enough |
| 3881 | * data is available. The parity check is held off while parity |
| 3882 | * dependent operations are in flight. |
| 3883 | */ |
| 3884 | if (sh->check_state || |
| 3885 | (s.syncing && s.locked == 0 && |
| 3886 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 3887 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 3888 | if (conf->level == 6) |
| 3889 | handle_parity_checks6(conf, sh, &s, disks); |
| 3890 | else |
| 3891 | handle_parity_checks5(conf, sh, &s, disks); |
| 3892 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3893 | |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 3894 | if ((s.replacing || s.syncing) && s.locked == 0 |
| 3895 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) |
| 3896 | && !test_bit(STRIPE_REPLACED, &sh->state)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3897 | /* Write out to replacement devices where possible */ |
| 3898 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 3899 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 3900 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3901 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 3902 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3903 | s.locked++; |
| 3904 | } |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 3905 | if (s.replacing) |
| 3906 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3907 | set_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3908 | } |
| 3909 | if ((s.syncing || s.replacing) && s.locked == 0 && |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 3910 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3911 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3912 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3913 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3914 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 3915 | wake_up(&conf->wait_for_overlap); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3916 | } |
| 3917 | |
| 3918 | /* If the failed drives are just a ReadError, then we might need |
| 3919 | * to progress the repair/check process |
| 3920 | */ |
| 3921 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 3922 | for (i = 0; i < s.failed; i++) { |
| 3923 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 3924 | if (test_bit(R5_ReadError, &dev->flags) |
| 3925 | && !test_bit(R5_LOCKED, &dev->flags) |
| 3926 | && test_bit(R5_UPTODATE, &dev->flags) |
| 3927 | ) { |
| 3928 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 3929 | set_bit(R5_Wantwrite, &dev->flags); |
| 3930 | set_bit(R5_ReWrite, &dev->flags); |
| 3931 | set_bit(R5_LOCKED, &dev->flags); |
| 3932 | s.locked++; |
| 3933 | } else { |
| 3934 | /* let's read it back */ |
| 3935 | set_bit(R5_Wantread, &dev->flags); |
| 3936 | set_bit(R5_LOCKED, &dev->flags); |
| 3937 | s.locked++; |
| 3938 | } |
| 3939 | } |
| 3940 | } |
| 3941 | |
| 3942 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3943 | /* Finish reconstruct operations initiated by the expansion process */ |
| 3944 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 3945 | struct stripe_head *sh_src |
| 3946 | = get_active_stripe(conf, sh->sector, 1, 1, 1); |
| 3947 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 3948 | /* sh cannot be written until sh_src has been read. |
| 3949 | * so arrange for sh to be delayed a little |
| 3950 | */ |
| 3951 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3952 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3953 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 3954 | &sh_src->state)) |
| 3955 | atomic_inc(&conf->preread_active_stripes); |
| 3956 | release_stripe(sh_src); |
| 3957 | goto finish; |
| 3958 | } |
| 3959 | if (sh_src) |
| 3960 | release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3961 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3962 | sh->reconstruct_state = reconstruct_state_idle; |
| 3963 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 3964 | for (i = conf->raid_disks; i--; ) { |
| 3965 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 3966 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 3967 | s.locked++; |
| 3968 | } |
| 3969 | } |
| 3970 | |
| 3971 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 3972 | !sh->reconstruct_state) { |
| 3973 | /* Need to write out all blocks after computing parity */ |
| 3974 | sh->disks = conf->raid_disks; |
| 3975 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 3976 | schedule_reconstruction(sh, &s, 1, 1); |
| 3977 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 3978 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 3979 | atomic_dec(&conf->reshape_stripes); |
| 3980 | wake_up(&conf->wait_for_overlap); |
| 3981 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 3982 | } |
| 3983 | |
| 3984 | if (s.expanding && s.locked == 0 && |
| 3985 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 3986 | handle_stripe_expansion(conf, sh); |
| 3987 | |
| 3988 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 3989 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 3990 | if (unlikely(s.blocked_rdev)) { |
| 3991 | if (conf->mddev->external) |
| 3992 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 3993 | conf->mddev); |
| 3994 | else |
| 3995 | /* Internal metadata will immediately |
| 3996 | * be written by raid5d, so we don't |
| 3997 | * need to wait here. |
| 3998 | */ |
| 3999 | rdev_dec_pending(s.blocked_rdev, |
| 4000 | conf->mddev); |
| 4001 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4002 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4003 | if (s.handle_bad_blocks) |
| 4004 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4005 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4006 | struct r5dev *dev = &sh->dev[i]; |
| 4007 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 4008 | /* We own a safe reference to the rdev */ |
| 4009 | rdev = conf->disks[i].rdev; |
| 4010 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 4011 | STRIPE_SECTORS, 0)) |
| 4012 | md_error(conf->mddev, rdev); |
| 4013 | rdev_dec_pending(rdev, conf->mddev); |
| 4014 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4015 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 4016 | rdev = conf->disks[i].rdev; |
| 4017 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4018 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4019 | rdev_dec_pending(rdev, conf->mddev); |
| 4020 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4021 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4022 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4023 | if (!rdev) |
| 4024 | /* rdev have been moved down */ |
| 4025 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4026 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4027 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4028 | rdev_dec_pending(rdev, conf->mddev); |
| 4029 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4030 | } |
| 4031 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4032 | if (s.ops_request) |
| 4033 | raid_run_ops(sh, s.ops_request); |
| 4034 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 4035 | ops_run_io(sh, &s); |
| 4036 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 4037 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4038 | /* 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] | 4039 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4040 | * have actually been submitted. |
| 4041 | */ |
| 4042 | atomic_dec(&conf->preread_active_stripes); |
| 4043 | if (atomic_read(&conf->preread_active_stripes) < |
| 4044 | IO_THRESHOLD) |
| 4045 | md_wakeup_thread(conf->mddev->thread); |
| 4046 | } |
| 4047 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 4048 | return_io(s.return_bi); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4049 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 4050 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4051 | } |
| 4052 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4053 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4054 | { |
| 4055 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 4056 | while (!list_empty(&conf->delayed_list)) { |
| 4057 | struct list_head *l = conf->delayed_list.next; |
| 4058 | struct stripe_head *sh; |
| 4059 | sh = list_entry(l, struct stripe_head, lru); |
| 4060 | list_del_init(l); |
| 4061 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4062 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4063 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4064 | list_add_tail(&sh->lru, &conf->hold_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4065 | raid5_wakeup_stripe_thread(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4066 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4067 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4068 | } |
| 4069 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4070 | static void activate_bit_delay(struct r5conf *conf, |
| 4071 | struct list_head *temp_inactive_list) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4072 | { |
| 4073 | /* device_lock is held */ |
| 4074 | struct list_head head; |
| 4075 | list_add(&head, &conf->bitmap_list); |
| 4076 | list_del_init(&conf->bitmap_list); |
| 4077 | while (!list_empty(&head)) { |
| 4078 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4079 | int hash; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4080 | list_del_init(&sh->lru); |
| 4081 | atomic_inc(&sh->count); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4082 | hash = sh->hash_lock_index; |
| 4083 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4084 | } |
| 4085 | } |
| 4086 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4087 | int md_raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4088 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4089 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4090 | |
| 4091 | /* No difference between reads and writes. Just check |
| 4092 | * how busy the stripe_cache is |
| 4093 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 4094 | |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4095 | if (conf->inactive_blocked) |
| 4096 | return 1; |
| 4097 | if (conf->quiesce) |
| 4098 | return 1; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4099 | if (atomic_read(&conf->empty_inactive_list_nr)) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4100 | return 1; |
| 4101 | |
| 4102 | return 0; |
| 4103 | } |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 4104 | EXPORT_SYMBOL_GPL(md_raid5_congested); |
| 4105 | |
| 4106 | static int raid5_congested(void *data, int bits) |
| 4107 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4108 | struct mddev *mddev = data; |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 4109 | |
| 4110 | return mddev_congested(mddev, bits) || |
| 4111 | md_raid5_congested(mddev, bits); |
| 4112 | } |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4113 | |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 4114 | /* We want read requests to align with chunks where possible, |
| 4115 | * but write requests don't need to. |
| 4116 | */ |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 4117 | static int raid5_mergeable_bvec(struct request_queue *q, |
| 4118 | struct bvec_merge_data *bvm, |
| 4119 | struct bio_vec *biovec) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 4120 | { |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4121 | struct mddev *mddev = q->queuedata; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 4122 | 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] | 4123 | int max; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4124 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 4125 | unsigned int bio_sectors = bvm->bi_size >> 9; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 4126 | |
Alasdair G Kergon | cc371e6 | 2008-07-03 09:53:43 +0200 | [diff] [blame] | 4127 | if ((bvm->bi_rw & 1) == WRITE) |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 4128 | return biovec->bv_len; /* always allow writes to be mergeable */ |
| 4129 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4130 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 4131 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | 23032a0 | 2006-12-10 02:20:45 -0800 | [diff] [blame] | 4132 | max = (chunk_sectors - ((sector & (chunk_sectors - 1)) + bio_sectors)) << 9; |
| 4133 | if (max < 0) max = 0; |
| 4134 | if (max <= biovec->bv_len && bio_sectors == 0) |
| 4135 | return biovec->bv_len; |
| 4136 | else |
| 4137 | return max; |
| 4138 | } |
| 4139 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4140 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4141 | 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] | 4142 | { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4143 | sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev); |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4144 | unsigned int chunk_sectors = mddev->chunk_sectors; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 4145 | unsigned int bio_sectors = bio_sectors(bio); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4146 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4147 | if (mddev->new_chunk_sectors < mddev->chunk_sectors) |
| 4148 | chunk_sectors = mddev->new_chunk_sectors; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4149 | return chunk_sectors >= |
| 4150 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 4151 | } |
| 4152 | |
| 4153 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4154 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 4155 | * later sampled by raid5d. |
| 4156 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4157 | 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] | 4158 | { |
| 4159 | unsigned long flags; |
| 4160 | |
| 4161 | spin_lock_irqsave(&conf->device_lock, flags); |
| 4162 | |
| 4163 | bi->bi_next = conf->retry_read_aligned_list; |
| 4164 | conf->retry_read_aligned_list = bi; |
| 4165 | |
| 4166 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 4167 | md_wakeup_thread(conf->mddev->thread); |
| 4168 | } |
| 4169 | |
| 4170 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4171 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4172 | { |
| 4173 | struct bio *bi; |
| 4174 | |
| 4175 | bi = conf->retry_read_aligned; |
| 4176 | if (bi) { |
| 4177 | conf->retry_read_aligned = NULL; |
| 4178 | return bi; |
| 4179 | } |
| 4180 | bi = conf->retry_read_aligned_list; |
| 4181 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4182 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4183 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4184 | /* |
| 4185 | * this sets the active strip count to 1 and the processed |
| 4186 | * strip count to zero (upper 8 bits) |
| 4187 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4188 | 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] | 4189 | } |
| 4190 | |
| 4191 | return bi; |
| 4192 | } |
| 4193 | |
| 4194 | |
| 4195 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4196 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 4197 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 4198 | * first). |
| 4199 | * If the read failed.. |
| 4200 | */ |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4201 | static void raid5_align_endio(struct bio *bi, int error) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4202 | { |
| 4203 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4204 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4205 | struct r5conf *conf; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4206 | int uptodate = test_bit(BIO_UPTODATE, &bi->bi_flags); |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4207 | struct md_rdev *rdev; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4208 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4209 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4210 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4211 | rdev = (void*)raid_bi->bi_next; |
| 4212 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 4213 | mddev = rdev->mddev; |
| 4214 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4215 | |
| 4216 | rdev_dec_pending(rdev, conf->mddev); |
| 4217 | |
| 4218 | if (!error && uptodate) { |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 4219 | trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev), |
| 4220 | raid_bi, 0); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4221 | bio_endio(raid_bi, 0); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4222 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 4223 | wake_up(&conf->wait_for_stripe); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4224 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4225 | } |
| 4226 | |
| 4227 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4228 | 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] | 4229 | |
| 4230 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4231 | } |
| 4232 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4233 | static int bio_fits_rdev(struct bio *bi) |
| 4234 | { |
Jens Axboe | 165125e | 2007-07-24 09:28:11 +0200 | [diff] [blame] | 4235 | struct request_queue *q = bdev_get_queue(bi->bi_bdev); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4236 | |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 4237 | if (bio_sectors(bi) > queue_max_sectors(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4238 | return 0; |
| 4239 | blk_recount_segments(q, bi); |
Martin K. Petersen | 8a78362 | 2010-02-26 00:20:39 -0500 | [diff] [blame] | 4240 | if (bi->bi_phys_segments > queue_max_segments(q)) |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4241 | return 0; |
| 4242 | |
| 4243 | if (q->merge_bvec_fn) |
| 4244 | /* it's too hard to apply the merge_bvec_fn at this stage, |
| 4245 | * just just give up |
| 4246 | */ |
| 4247 | return 0; |
| 4248 | |
| 4249 | return 1; |
| 4250 | } |
| 4251 | |
| 4252 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4253 | 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] | 4254 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4255 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 4256 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4257 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4258 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4259 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4260 | |
| 4261 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4262 | pr_debug("chunk_aligned_read : non aligned\n"); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4263 | return 0; |
| 4264 | } |
| 4265 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4266 | * 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] | 4267 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4268 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4269 | if (!align_bi) |
| 4270 | return 0; |
| 4271 | /* |
| 4272 | * set bi_end_io to a new function, and set bi_private to the |
| 4273 | * original bio. |
| 4274 | */ |
| 4275 | align_bi->bi_end_io = raid5_align_endio; |
| 4276 | align_bi->bi_private = raid_bio; |
| 4277 | /* |
| 4278 | * compute position |
| 4279 | */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4280 | align_bi->bi_iter.bi_sector = |
| 4281 | raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, |
| 4282 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4283 | |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 4284 | end_sector = bio_end_sector(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4285 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4286 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 4287 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 4288 | rdev->recovery_offset < end_sector) { |
| 4289 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 4290 | if (rdev && |
| 4291 | (test_bit(Faulty, &rdev->flags) || |
| 4292 | !(test_bit(In_sync, &rdev->flags) || |
| 4293 | rdev->recovery_offset >= end_sector))) |
| 4294 | rdev = NULL; |
| 4295 | } |
| 4296 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4297 | sector_t first_bad; |
| 4298 | int bad_sectors; |
| 4299 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4300 | atomic_inc(&rdev->nr_pending); |
| 4301 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4302 | raid_bio->bi_next = (void*)rdev; |
| 4303 | align_bi->bi_bdev = rdev->bdev; |
NeilBrown | 3fd8371 | 2014-08-23 20:19:26 +1000 | [diff] [blame] | 4304 | __clear_bit(BIO_SEG_VALID, &align_bi->bi_flags); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4305 | |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4306 | if (!bio_fits_rdev(align_bi) || |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4307 | is_badblock(rdev, align_bi->bi_iter.bi_sector, |
| 4308 | bio_sectors(align_bi), |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4309 | &first_bad, &bad_sectors)) { |
| 4310 | /* too big in some way, or has a known bad block */ |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4311 | bio_put(align_bi); |
| 4312 | rdev_dec_pending(rdev, mddev); |
| 4313 | return 0; |
| 4314 | } |
| 4315 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 4316 | /* No reshape active, so we can trust rdev->data_offset */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4317 | align_bi->bi_iter.bi_sector += rdev->data_offset; |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 4318 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4319 | spin_lock_irq(&conf->device_lock); |
| 4320 | wait_event_lock_irq(conf->wait_for_stripe, |
| 4321 | conf->quiesce == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 4322 | conf->device_lock); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4323 | atomic_inc(&conf->active_aligned_reads); |
| 4324 | spin_unlock_irq(&conf->device_lock); |
| 4325 | |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4326 | if (mddev->gendisk) |
| 4327 | trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), |
| 4328 | align_bi, disk_devt(mddev->gendisk), |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4329 | raid_bio->bi_iter.bi_sector); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4330 | generic_make_request(align_bi); |
| 4331 | return 1; |
| 4332 | } else { |
| 4333 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4334 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4335 | return 0; |
| 4336 | } |
| 4337 | } |
| 4338 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4339 | /* __get_priority_stripe - get the next stripe to process |
| 4340 | * |
| 4341 | * Full stripe writes are allowed to pass preread active stripes up until |
| 4342 | * the bypass_threshold is exceeded. In general the bypass_count |
| 4343 | * increments when the handle_list is handled before the hold_list; however, it |
| 4344 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 4345 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 4346 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 4347 | * handle_list. |
| 4348 | */ |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4349 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4350 | { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4351 | struct stripe_head *sh = NULL, *tmp; |
| 4352 | struct list_head *handle_list = NULL; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 4353 | struct r5worker_group *wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4354 | |
| 4355 | if (conf->worker_cnt_per_group == 0) { |
| 4356 | handle_list = &conf->handle_list; |
| 4357 | } else if (group != ANY_GROUP) { |
| 4358 | handle_list = &conf->worker_groups[group].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 4359 | wg = &conf->worker_groups[group]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4360 | } else { |
| 4361 | int i; |
| 4362 | for (i = 0; i < conf->group_cnt; i++) { |
| 4363 | handle_list = &conf->worker_groups[i].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 4364 | wg = &conf->worker_groups[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4365 | if (!list_empty(handle_list)) |
| 4366 | break; |
| 4367 | } |
| 4368 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4369 | |
| 4370 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 4371 | __func__, |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4372 | list_empty(handle_list) ? "empty" : "busy", |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4373 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 4374 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 4375 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4376 | if (!list_empty(handle_list)) { |
| 4377 | sh = list_entry(handle_list->next, typeof(*sh), lru); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4378 | |
| 4379 | if (list_empty(&conf->hold_list)) |
| 4380 | conf->bypass_count = 0; |
| 4381 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 4382 | if (conf->hold_list.next == conf->last_hold) |
| 4383 | conf->bypass_count++; |
| 4384 | else { |
| 4385 | conf->last_hold = conf->hold_list.next; |
| 4386 | conf->bypass_count -= conf->bypass_threshold; |
| 4387 | if (conf->bypass_count < 0) |
| 4388 | conf->bypass_count = 0; |
| 4389 | } |
| 4390 | } |
| 4391 | } else if (!list_empty(&conf->hold_list) && |
| 4392 | ((conf->bypass_threshold && |
| 4393 | conf->bypass_count > conf->bypass_threshold) || |
| 4394 | atomic_read(&conf->pending_full_writes) == 0)) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4395 | |
| 4396 | list_for_each_entry(tmp, &conf->hold_list, lru) { |
| 4397 | if (conf->worker_cnt_per_group == 0 || |
| 4398 | group == ANY_GROUP || |
| 4399 | !cpu_online(tmp->cpu) || |
| 4400 | cpu_to_group(tmp->cpu) == group) { |
| 4401 | sh = tmp; |
| 4402 | break; |
| 4403 | } |
| 4404 | } |
| 4405 | |
| 4406 | if (sh) { |
| 4407 | conf->bypass_count -= conf->bypass_threshold; |
| 4408 | if (conf->bypass_count < 0) |
| 4409 | conf->bypass_count = 0; |
| 4410 | } |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 4411 | wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4412 | } |
| 4413 | |
| 4414 | if (!sh) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4415 | return NULL; |
| 4416 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 4417 | if (wg) { |
| 4418 | wg->stripes_cnt--; |
| 4419 | sh->group = NULL; |
| 4420 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4421 | list_del_init(&sh->lru); |
Shaohua Li | c7a6d35 | 2014-04-15 09:12:54 +0800 | [diff] [blame] | 4422 | BUG_ON(atomic_inc_return(&sh->count) != 1); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4423 | return sh; |
| 4424 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4425 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4426 | struct raid5_plug_cb { |
| 4427 | struct blk_plug_cb cb; |
| 4428 | struct list_head list; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4429 | struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4430 | }; |
| 4431 | |
| 4432 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 4433 | { |
| 4434 | struct raid5_plug_cb *cb = container_of( |
| 4435 | blk_cb, struct raid5_plug_cb, cb); |
| 4436 | struct stripe_head *sh; |
| 4437 | struct mddev *mddev = cb->cb.data; |
| 4438 | struct r5conf *conf = mddev->private; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4439 | int cnt = 0; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4440 | int hash; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4441 | |
| 4442 | if (cb->list.next && !list_empty(&cb->list)) { |
| 4443 | spin_lock_irq(&conf->device_lock); |
| 4444 | while (!list_empty(&cb->list)) { |
| 4445 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 4446 | list_del_init(&sh->lru); |
| 4447 | /* |
| 4448 | * avoid race release_stripe_plug() sees |
| 4449 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 4450 | * is still in our list |
| 4451 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 4452 | smp_mb__before_atomic(); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4453 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 4454 | /* |
| 4455 | * STRIPE_ON_RELEASE_LIST could be set here. In that |
| 4456 | * case, the count is always > 1 here |
| 4457 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4458 | hash = sh->hash_lock_index; |
| 4459 | __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4460 | cnt++; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4461 | } |
| 4462 | spin_unlock_irq(&conf->device_lock); |
| 4463 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4464 | release_inactive_stripe_list(conf, cb->temp_inactive_list, |
| 4465 | NR_STRIPE_HASH_LOCKS); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4466 | if (mddev->queue) |
| 4467 | trace_block_unplug(mddev->queue, cnt, !from_schedule); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4468 | kfree(cb); |
| 4469 | } |
| 4470 | |
| 4471 | static void release_stripe_plug(struct mddev *mddev, |
| 4472 | struct stripe_head *sh) |
| 4473 | { |
| 4474 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 4475 | raid5_unplug, mddev, |
| 4476 | sizeof(struct raid5_plug_cb)); |
| 4477 | struct raid5_plug_cb *cb; |
| 4478 | |
| 4479 | if (!blk_cb) { |
| 4480 | release_stripe(sh); |
| 4481 | return; |
| 4482 | } |
| 4483 | |
| 4484 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 4485 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4486 | if (cb->list.next == NULL) { |
| 4487 | int i; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4488 | INIT_LIST_HEAD(&cb->list); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4489 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 4490 | INIT_LIST_HEAD(cb->temp_inactive_list + i); |
| 4491 | } |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4492 | |
| 4493 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 4494 | list_add_tail(&sh->lru, &cb->list); |
| 4495 | else |
| 4496 | release_stripe(sh); |
| 4497 | } |
| 4498 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4499 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 4500 | { |
| 4501 | struct r5conf *conf = mddev->private; |
| 4502 | sector_t logical_sector, last_sector; |
| 4503 | struct stripe_head *sh; |
| 4504 | int remaining; |
| 4505 | int stripe_sectors; |
| 4506 | |
| 4507 | if (mddev->reshape_position != MaxSector) |
| 4508 | /* Skip discard while reshape is happening */ |
| 4509 | return; |
| 4510 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4511 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 4512 | last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4513 | |
| 4514 | bi->bi_next = NULL; |
| 4515 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
| 4516 | |
| 4517 | stripe_sectors = conf->chunk_sectors * |
| 4518 | (conf->raid_disks - conf->max_degraded); |
| 4519 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 4520 | stripe_sectors); |
| 4521 | sector_div(last_sector, stripe_sectors); |
| 4522 | |
| 4523 | logical_sector *= conf->chunk_sectors; |
| 4524 | last_sector *= conf->chunk_sectors; |
| 4525 | |
| 4526 | for (; logical_sector < last_sector; |
| 4527 | logical_sector += STRIPE_SECTORS) { |
| 4528 | DEFINE_WAIT(w); |
| 4529 | int d; |
| 4530 | again: |
| 4531 | sh = get_active_stripe(conf, logical_sector, 0, 0, 0); |
| 4532 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 4533 | TASK_UNINTERRUPTIBLE); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4534 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
| 4535 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 4536 | release_stripe(sh); |
| 4537 | schedule(); |
| 4538 | goto again; |
| 4539 | } |
| 4540 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4541 | spin_lock_irq(&sh->stripe_lock); |
| 4542 | for (d = 0; d < conf->raid_disks; d++) { |
| 4543 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 4544 | continue; |
| 4545 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 4546 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 4547 | spin_unlock_irq(&sh->stripe_lock); |
| 4548 | release_stripe(sh); |
| 4549 | schedule(); |
| 4550 | goto again; |
| 4551 | } |
| 4552 | } |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4553 | set_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4554 | finish_wait(&conf->wait_for_overlap, &w); |
| 4555 | for (d = 0; d < conf->raid_disks; d++) { |
| 4556 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 4557 | continue; |
| 4558 | sh->dev[d].towrite = bi; |
| 4559 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
| 4560 | raid5_inc_bi_active_stripes(bi); |
| 4561 | } |
| 4562 | spin_unlock_irq(&sh->stripe_lock); |
| 4563 | if (conf->mddev->bitmap) { |
| 4564 | for (d = 0; |
| 4565 | d < conf->raid_disks - conf->max_degraded; |
| 4566 | d++) |
| 4567 | bitmap_startwrite(mddev->bitmap, |
| 4568 | sh->sector, |
| 4569 | STRIPE_SECTORS, |
| 4570 | 0); |
| 4571 | sh->bm_seq = conf->seq_flush + 1; |
| 4572 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 4573 | } |
| 4574 | |
| 4575 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4576 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4577 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4578 | atomic_inc(&conf->preread_active_stripes); |
| 4579 | release_stripe_plug(mddev, sh); |
| 4580 | } |
| 4581 | |
| 4582 | remaining = raid5_dec_bi_active_stripes(bi); |
| 4583 | if (remaining == 0) { |
| 4584 | md_write_end(mddev); |
| 4585 | bio_endio(bi, 0); |
| 4586 | } |
| 4587 | } |
| 4588 | |
Linus Torvalds | b4fdcb0 | 2011-11-04 17:06:58 -0700 | [diff] [blame] | 4589 | static void make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4590 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4591 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4592 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4593 | sector_t new_sector; |
| 4594 | sector_t logical_sector, last_sector; |
| 4595 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 4596 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 4597 | int remaining; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4598 | DEFINE_WAIT(w); |
| 4599 | bool do_prepare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4600 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 4601 | if (unlikely(bi->bi_rw & REQ_FLUSH)) { |
| 4602 | md_flush_request(mddev, bi); |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4603 | return; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 4604 | } |
| 4605 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 4606 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4607 | |
NeilBrown | 802ba06 | 2006-12-13 00:34:13 -0800 | [diff] [blame] | 4608 | if (rw == READ && |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4609 | mddev->reshape_position == MaxSector && |
NeilBrown | 21a52c6 | 2010-04-01 15:02:13 +1100 | [diff] [blame] | 4610 | chunk_aligned_read(mddev,bi)) |
Christoph Hellwig | 5a7bbad | 2011-09-12 12:12:01 +0200 | [diff] [blame] | 4611 | return; |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 4612 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 4613 | if (unlikely(bi->bi_rw & REQ_DISCARD)) { |
| 4614 | make_discard_request(mddev, bi); |
| 4615 | return; |
| 4616 | } |
| 4617 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4618 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 4619 | last_sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4620 | bi->bi_next = NULL; |
| 4621 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 4622 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4623 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4624 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4625 | int previous; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 4626 | int seq; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4627 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4628 | do_prepare = false; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4629 | retry: |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 4630 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4631 | previous = 0; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4632 | if (do_prepare) |
| 4633 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 4634 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4635 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4636 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4637 | * 64bit on a 32bit platform, and so it might be |
| 4638 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 4639 | * Of course reshape_progress could change after |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4640 | * the lock is dropped, so once we get a reference |
| 4641 | * to the stripe that we think it is, we will have |
| 4642 | * to check again. |
| 4643 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4644 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4645 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4646 | ? logical_sector < conf->reshape_progress |
| 4647 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4648 | previous = 1; |
| 4649 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4650 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4651 | ? logical_sector < conf->reshape_safe |
| 4652 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4653 | spin_unlock_irq(&conf->device_lock); |
| 4654 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4655 | do_prepare = true; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 4656 | goto retry; |
| 4657 | } |
| 4658 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4659 | spin_unlock_irq(&conf->device_lock); |
| 4660 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4661 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4662 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 4663 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4664 | &dd_idx, NULL); |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 4665 | pr_debug("raid456: make_request, sector %llu logical %llu\n", |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 4666 | (unsigned long long)new_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4667 | (unsigned long long)logical_sector); |
| 4668 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4669 | sh = get_active_stripe(conf, new_sector, previous, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4670 | (bi->bi_rw&RWA_MASK), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4671 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4672 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4673 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 4674 | * stripe, so we must do the range check again. |
| 4675 | * Expansion could still move past after this |
| 4676 | * test, but as we are holding a reference to |
| 4677 | * 'sh', we know that if that happens, |
| 4678 | * STRIPE_EXPANDING will get set and the expansion |
| 4679 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4680 | */ |
| 4681 | int must_retry = 0; |
| 4682 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4683 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 4684 | ? logical_sector >= conf->reshape_progress |
| 4685 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4686 | /* mismatch, need to try again */ |
| 4687 | must_retry = 1; |
| 4688 | spin_unlock_irq(&conf->device_lock); |
| 4689 | if (must_retry) { |
| 4690 | release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 4691 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4692 | do_prepare = true; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4693 | goto retry; |
| 4694 | } |
| 4695 | } |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 4696 | if (read_seqcount_retry(&conf->gen_lock, seq)) { |
| 4697 | /* Might have got the wrong stripe_head |
| 4698 | * by accident |
| 4699 | */ |
| 4700 | release_stripe(sh); |
| 4701 | goto retry; |
| 4702 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4703 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4704 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4705 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4706 | logical_sector < mddev->suspend_hi) { |
| 4707 | release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4708 | /* As the suspend_* range is controlled by |
| 4709 | * userspace, we want an interruptible |
| 4710 | * wait. |
| 4711 | */ |
| 4712 | flush_signals(current); |
| 4713 | prepare_to_wait(&conf->wait_for_overlap, |
| 4714 | &w, TASK_INTERRUPTIBLE); |
| 4715 | if (logical_sector >= mddev->suspend_lo && |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4716 | logical_sector < mddev->suspend_hi) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 4717 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4718 | do_prepare = true; |
| 4719 | } |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 4720 | goto retry; |
| 4721 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4722 | |
| 4723 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 4724 | !add_stripe_bio(sh, bi, dd_idx, rw)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 4725 | /* Stripe is busy expanding or |
| 4726 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4727 | * and wait a while |
| 4728 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4729 | md_wakeup_thread(mddev->thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4730 | release_stripe(sh); |
| 4731 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4732 | do_prepare = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4733 | goto retry; |
| 4734 | } |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 4735 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4736 | clear_bit(STRIPE_DELAYED, &sh->state); |
NeilBrown | a852d7b | 2012-09-19 12:48:30 +1000 | [diff] [blame] | 4737 | if ((bi->bi_rw & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4738 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4739 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 4740 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4741 | } else { |
| 4742 | /* cannot get stripe for read-ahead, just give-up */ |
| 4743 | clear_bit(BIO_UPTODATE, &bi->bi_flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4744 | break; |
| 4745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4746 | } |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 4747 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 4748 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4749 | remaining = raid5_dec_bi_active_stripes(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 4750 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4751 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4752 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4753 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4754 | |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 4755 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 4756 | bi, 0); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 4757 | bio_endio(bi, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4758 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4759 | } |
| 4760 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4761 | 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] | 4762 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4763 | 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] | 4764 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4765 | /* reshaping is quite different to recovery/resync so it is |
| 4766 | * handled quite separately ... here. |
| 4767 | * |
| 4768 | * On each call to sync_request, we gather one chunk worth of |
| 4769 | * destination stripes and flag them as expanding. |
| 4770 | * Then we find all the source stripes and request reads. |
| 4771 | * As the reads complete, handle_stripe will copy the data |
| 4772 | * into the destination stripe and release that stripe. |
| 4773 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4774 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4775 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 4776 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4777 | int raid_disks = conf->previous_raid_disks; |
| 4778 | int data_disks = raid_disks - conf->max_degraded; |
| 4779 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4780 | int i; |
| 4781 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4782 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4783 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4784 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4785 | struct list_head stripes; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4786 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4787 | if (sector_nr == 0) { |
| 4788 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4789 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4790 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 4791 | sector_nr = raid5_size(mddev, 0, 0) |
| 4792 | - conf->reshape_progress; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4793 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4794 | conf->reshape_progress > 0) |
| 4795 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4796 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4797 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 4798 | mddev->curr_resync_completed = sector_nr; |
| 4799 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4800 | *skipped = 1; |
| 4801 | return sector_nr; |
| 4802 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4803 | } |
| 4804 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4805 | /* We need to process a full chunk at a time. |
| 4806 | * If old and new chunk sizes differ, we need to process the |
| 4807 | * largest of these |
| 4808 | */ |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 4809 | if (mddev->new_chunk_sectors > mddev->chunk_sectors) |
| 4810 | reshape_sectors = mddev->new_chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4811 | else |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 4812 | reshape_sectors = mddev->chunk_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4813 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4814 | /* We update the metadata at least every 10 seconds, or when |
| 4815 | * the data about to be copied would over-write the source of |
| 4816 | * the data at the front of the range. i.e. one new_stripe |
| 4817 | * along from reshape_progress new_maps to after where |
| 4818 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4819 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4820 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4821 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4822 | readpos = conf->reshape_progress; |
| 4823 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4824 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4825 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4826 | if (mddev->reshape_backwards) { |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4827 | writepos -= min_t(sector_t, reshape_sectors, writepos); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4828 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4829 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4830 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4831 | writepos += reshape_sectors; |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 4832 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 4833 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4834 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4835 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4836 | /* Having calculated the 'writepos' possibly use it |
| 4837 | * to set 'stripe_addr' which is where we will write to. |
| 4838 | */ |
| 4839 | if (mddev->reshape_backwards) { |
| 4840 | BUG_ON(conf->reshape_progress == 0); |
| 4841 | stripe_addr = writepos; |
| 4842 | BUG_ON((mddev->dev_sectors & |
| 4843 | ~((sector_t)reshape_sectors - 1)) |
| 4844 | - reshape_sectors - stripe_addr |
| 4845 | != sector_nr); |
| 4846 | } else { |
| 4847 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 4848 | stripe_addr = sector_nr; |
| 4849 | } |
| 4850 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4851 | /* 'writepos' is the most advanced device address we might write. |
| 4852 | * 'readpos' is the least advanced device address we might read. |
| 4853 | * 'safepos' is the least address recorded in the metadata as having |
| 4854 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4855 | * If there is a min_offset_diff, these are adjusted either by |
| 4856 | * increasing the safepos/readpos if diff is negative, or |
| 4857 | * increasing writepos if diff is positive. |
| 4858 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4859 | * ensure safety in the face of a crash - that must be done by userspace |
| 4860 | * making a backup of the data. So in that case there is no particular |
| 4861 | * rush to update metadata. |
| 4862 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 4863 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 4864 | * we can be safe in the event of a crash. |
| 4865 | * So we insist on updating metadata if safepos is behind writepos and |
| 4866 | * readpos is beyond writepos. |
| 4867 | * In any case, update the metadata every 10 seconds. |
| 4868 | * Maybe that number should be configurable, but I'm not sure it is |
| 4869 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 4870 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 4871 | if (conf->min_offset_diff < 0) { |
| 4872 | safepos += -conf->min_offset_diff; |
| 4873 | readpos += -conf->min_offset_diff; |
| 4874 | } else |
| 4875 | writepos += conf->min_offset_diff; |
| 4876 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4877 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4878 | ? (safepos > writepos && readpos < writepos) |
| 4879 | : (safepos < writepos && readpos > writepos)) || |
| 4880 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4881 | /* Cannot proceed until we've updated the superblock... */ |
| 4882 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 4883 | atomic_read(&conf->reshape_stripes)==0 |
| 4884 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 4885 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 4886 | return 0; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4887 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4888 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4889 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4890 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4891 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 4892 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 4893 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 4894 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 4895 | return 0; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4896 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4897 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4898 | spin_unlock_irq(&conf->device_lock); |
| 4899 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4900 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4901 | } |
| 4902 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4903 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4904 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4905 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4906 | int skipped_disk = 0; |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4907 | sh = get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4908 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 4909 | atomic_inc(&conf->reshape_stripes); |
| 4910 | /* If any of this stripe is beyond the end of the old |
| 4911 | * array, then we need to zero those blocks |
| 4912 | */ |
| 4913 | for (j=sh->disks; j--;) { |
| 4914 | sector_t s; |
| 4915 | if (j == sh->pd_idx) |
| 4916 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4917 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4918 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4919 | continue; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 4920 | s = compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 4921 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4922 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4923 | continue; |
| 4924 | } |
| 4925 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 4926 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 4927 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 4928 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 4929 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4930 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4931 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4932 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4933 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4934 | } |
| 4935 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4936 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4937 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4938 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4939 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4940 | spin_unlock_irq(&conf->device_lock); |
| 4941 | /* Ok, those stripe are ready. We can start scheduling |
| 4942 | * reads on the source stripes. |
| 4943 | * The source stripes are determined by mapping the first and last |
| 4944 | * block on the destination stripes. |
| 4945 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4946 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 4947 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4948 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4949 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 4950 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 4951 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4952 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 4953 | if (last_sector >= mddev->dev_sectors) |
| 4954 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4955 | while (first_sector <= last_sector) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 4956 | sh = get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 4957 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4958 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4959 | release_stripe(sh); |
| 4960 | first_sector += STRIPE_SECTORS; |
| 4961 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 4962 | /* Now that the sources are clearly marked, we can release |
| 4963 | * the destination stripes |
| 4964 | */ |
| 4965 | while (!list_empty(&stripes)) { |
| 4966 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 4967 | list_del_init(&sh->lru); |
| 4968 | release_stripe(sh); |
| 4969 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4970 | /* If this takes us to the resync_max point where we have to pause, |
| 4971 | * then we need to write out the superblock. |
| 4972 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4973 | sector_nr += reshape_sectors; |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 4974 | if ((sector_nr - mddev->curr_resync_completed) * 2 |
| 4975 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4976 | /* Cannot proceed until we've updated the superblock... */ |
| 4977 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 4978 | atomic_read(&conf->reshape_stripes) == 0 |
| 4979 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 4980 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 4981 | goto ret; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4982 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 4983 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 4984 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4985 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 4986 | md_wakeup_thread(mddev->thread); |
| 4987 | wait_event(mddev->sb_wait, |
| 4988 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 4989 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 4990 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 4991 | goto ret; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4992 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 4993 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4994 | spin_unlock_irq(&conf->device_lock); |
| 4995 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 4996 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 4997 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 4998 | ret: |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 4999 | return reshape_sectors; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5000 | } |
| 5001 | |
| 5002 | /* FIXME go_faster isn't used */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5003 | 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] | 5004 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5005 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5006 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5007 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 5008 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5009 | int still_degraded = 0; |
| 5010 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5011 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5012 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5013 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5014 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5015 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 5016 | end_reshape(conf); |
| 5017 | return 0; |
| 5018 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5019 | |
| 5020 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 5021 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 5022 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5023 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5024 | conf->fullsync = 0; |
| 5025 | bitmap_close_sync(mddev->bitmap); |
| 5026 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5027 | return 0; |
| 5028 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5029 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5030 | /* Allow raid5_quiesce to complete */ |
| 5031 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 5032 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5033 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5034 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5035 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5036 | /* No need to check resync_max as we never do more than one |
| 5037 | * stripe, and as resync_max will always be on a chunk boundary, |
| 5038 | * if the check in md_do_sync didn't fire, there is no chance |
| 5039 | * of overstepping resync_max here |
| 5040 | */ |
| 5041 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5042 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5043 | * to resync, then assert that we are finished, because there is |
| 5044 | * nothing we can do. |
| 5045 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 5046 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5047 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5048 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5049 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5050 | return rv; |
| 5051 | } |
majianpeng | 6f60804 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 5052 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 5053 | !conf->fullsync && |
| 5054 | !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
| 5055 | sync_blocks >= STRIPE_SECTORS) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5056 | /* we can skip this block, and probably more */ |
| 5057 | sync_blocks /= STRIPE_SECTORS; |
| 5058 | *skipped = 1; |
| 5059 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 5060 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5061 | |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 5062 | bitmap_cond_end_sync(mddev->bitmap, sector_nr); |
| 5063 | |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 5064 | sh = get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5065 | if (sh == NULL) { |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 5066 | sh = get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5067 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5068 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5069 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 5070 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5071 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5072 | /* Need to check if array will still be degraded after recovery/resync |
| 5073 | * We don't need to check the 'failed' flag as when that gets set, |
| 5074 | * recovery aborts. |
| 5075 | */ |
NeilBrown | f001a70 | 2009-06-09 14:30:31 +1000 | [diff] [blame] | 5076 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5077 | if (conf->disks[i].rdev == NULL) |
| 5078 | still_degraded = 1; |
| 5079 | |
| 5080 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 5081 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 5082 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Eivind Sarto | 053f5b6 | 2014-06-09 17:06:19 -0700 | [diff] [blame] | 5083 | set_bit(STRIPE_HANDLE, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5084 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5085 | release_stripe(sh); |
| 5086 | |
| 5087 | return STRIPE_SECTORS; |
| 5088 | } |
| 5089 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5090 | 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] | 5091 | { |
| 5092 | /* We may not be able to submit a whole bio at once as there |
| 5093 | * may not be enough stripe_heads available. |
| 5094 | * We cannot pre-allocate enough stripe_heads as we may need |
| 5095 | * more than exist in the cache (if we allow ever large chunks). |
| 5096 | * So we do one stripe head at a time and record in |
| 5097 | * ->bi_hw_segments how many have been done. |
| 5098 | * |
| 5099 | * We *know* that this entire raid_bio is in one chunk, so |
| 5100 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 5101 | */ |
| 5102 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5103 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5104 | sector_t sector, logical_sector, last_sector; |
| 5105 | int scnt = 0; |
| 5106 | int remaining; |
| 5107 | int handled = 0; |
| 5108 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5109 | logical_sector = raid_bio->bi_iter.bi_sector & |
| 5110 | ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5111 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5112 | 0, &dd_idx, NULL); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5113 | last_sector = bio_end_sector(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5114 | |
| 5115 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5116 | logical_sector += STRIPE_SECTORS, |
| 5117 | sector += STRIPE_SECTORS, |
| 5118 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5119 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5120 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5121 | /* already done this stripe */ |
| 5122 | continue; |
| 5123 | |
hui jiao | 2844dc3 | 2014-06-05 11:34:24 +0800 | [diff] [blame] | 5124 | sh = get_active_stripe(conf, sector, 0, 1, 1); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5125 | |
| 5126 | if (!sh) { |
| 5127 | /* failed to get a stripe - must wait */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5128 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5129 | conf->retry_read_aligned = raid_bio; |
| 5130 | return handled; |
| 5131 | } |
| 5132 | |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5133 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0)) { |
| 5134 | release_stripe(sh); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5135 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5136 | conf->retry_read_aligned = raid_bio; |
| 5137 | return handled; |
| 5138 | } |
| 5139 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 5140 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5141 | handle_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5142 | release_stripe(sh); |
| 5143 | handled++; |
| 5144 | } |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5145 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5146 | if (remaining == 0) { |
| 5147 | trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev), |
| 5148 | raid_bio, 0); |
Neil Brown | 0e13fe23 | 2008-06-28 08:31:20 +1000 | [diff] [blame] | 5149 | bio_endio(raid_bio, 0); |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5150 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5151 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
| 5152 | wake_up(&conf->wait_for_stripe); |
| 5153 | return handled; |
| 5154 | } |
| 5155 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5156 | static int handle_active_stripes(struct r5conf *conf, int group, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5157 | struct r5worker *worker, |
| 5158 | struct list_head *temp_inactive_list) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5159 | { |
| 5160 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5161 | int i, batch_size = 0, hash; |
| 5162 | bool release_inactive = false; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5163 | |
| 5164 | while (batch_size < MAX_STRIPE_BATCH && |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5165 | (sh = __get_priority_stripe(conf, group)) != NULL) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5166 | batch[batch_size++] = sh; |
| 5167 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5168 | if (batch_size == 0) { |
| 5169 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5170 | if (!list_empty(temp_inactive_list + i)) |
| 5171 | break; |
| 5172 | if (i == NR_STRIPE_HASH_LOCKS) |
| 5173 | return batch_size; |
| 5174 | release_inactive = true; |
| 5175 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5176 | spin_unlock_irq(&conf->device_lock); |
| 5177 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5178 | release_inactive_stripe_list(conf, temp_inactive_list, |
| 5179 | NR_STRIPE_HASH_LOCKS); |
| 5180 | |
| 5181 | if (release_inactive) { |
| 5182 | spin_lock_irq(&conf->device_lock); |
| 5183 | return 0; |
| 5184 | } |
| 5185 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5186 | for (i = 0; i < batch_size; i++) |
| 5187 | handle_stripe(batch[i]); |
| 5188 | |
| 5189 | cond_resched(); |
| 5190 | |
| 5191 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5192 | for (i = 0; i < batch_size; i++) { |
| 5193 | hash = batch[i]->hash_lock_index; |
| 5194 | __release_stripe(conf, batch[i], &temp_inactive_list[hash]); |
| 5195 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5196 | return batch_size; |
| 5197 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5198 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5199 | static void raid5_do_work(struct work_struct *work) |
| 5200 | { |
| 5201 | struct r5worker *worker = container_of(work, struct r5worker, work); |
| 5202 | struct r5worker_group *group = worker->group; |
| 5203 | struct r5conf *conf = group->conf; |
| 5204 | int group_id = group - conf->worker_groups; |
| 5205 | int handled; |
| 5206 | struct blk_plug plug; |
| 5207 | |
| 5208 | pr_debug("+++ raid5worker active\n"); |
| 5209 | |
| 5210 | blk_start_plug(&plug); |
| 5211 | handled = 0; |
| 5212 | spin_lock_irq(&conf->device_lock); |
| 5213 | while (1) { |
| 5214 | int batch_size, released; |
| 5215 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5216 | released = release_stripe_list(conf, worker->temp_inactive_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5217 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5218 | batch_size = handle_active_stripes(conf, group_id, worker, |
| 5219 | worker->temp_inactive_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5220 | worker->working = false; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5221 | if (!batch_size && !released) |
| 5222 | break; |
| 5223 | handled += batch_size; |
| 5224 | } |
| 5225 | pr_debug("%d stripes handled\n", handled); |
| 5226 | |
| 5227 | spin_unlock_irq(&conf->device_lock); |
| 5228 | blk_finish_plug(&plug); |
| 5229 | |
| 5230 | pr_debug("--- raid5worker inactive\n"); |
| 5231 | } |
| 5232 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5233 | /* |
| 5234 | * This is our raid5 kernel thread. |
| 5235 | * |
| 5236 | * We scan the hash table for stripes which can be handled now. |
| 5237 | * During the scan, completed stripes are saved for us by the interrupt |
| 5238 | * handler, so that they will not have to wait for our next wakeup. |
| 5239 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 5240 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 5242 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5243 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 5245 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5246 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5247 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5248 | |
| 5249 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5250 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 5251 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5252 | handled = 0; |
| 5253 | spin_lock_irq(&conf->device_lock); |
| 5254 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5255 | struct bio *bio; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5256 | int batch_size, released; |
| 5257 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5258 | released = release_stripe_list(conf, conf->temp_inactive_list); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5259 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 5260 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5261 | !list_empty(&conf->bitmap_list)) { |
| 5262 | /* Now is a good time to flush some bitmap updates */ |
| 5263 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 5264 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5265 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 5266 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5267 | conf->seq_write = conf->seq_flush; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5268 | activate_bit_delay(conf, conf->temp_inactive_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5269 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 5270 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5271 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5272 | while ((bio = remove_bio_from_retry(conf))) { |
| 5273 | int ok; |
| 5274 | spin_unlock_irq(&conf->device_lock); |
| 5275 | ok = retry_aligned_read(conf, bio); |
| 5276 | spin_lock_irq(&conf->device_lock); |
| 5277 | if (!ok) |
| 5278 | break; |
| 5279 | handled++; |
| 5280 | } |
| 5281 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5282 | batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, |
| 5283 | conf->temp_inactive_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5284 | if (!batch_size && !released) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5285 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5286 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5287 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5288 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) { |
| 5289 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 5290 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5291 | spin_lock_irq(&conf->device_lock); |
| 5292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5293 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5294 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5295 | |
| 5296 | spin_unlock_irq(&conf->device_lock); |
| 5297 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 5298 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 5299 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5300 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5301 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5302 | } |
| 5303 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5304 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5305 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5306 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5307 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5308 | if (conf) |
| 5309 | return sprintf(page, "%d\n", conf->max_nr_stripes); |
| 5310 | else |
| 5311 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5312 | } |
| 5313 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5314 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5315 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5316 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5317 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5318 | int err; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5319 | int hash; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5320 | |
| 5321 | if (size <= 16 || size > 32768) |
| 5322 | return -EINVAL; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5323 | hash = (conf->max_nr_stripes - 1) % NR_STRIPE_HASH_LOCKS; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5324 | while (size < conf->max_nr_stripes) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5325 | if (drop_one_stripe(conf, hash)) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5326 | conf->max_nr_stripes--; |
| 5327 | else |
| 5328 | break; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5329 | hash--; |
| 5330 | if (hash < 0) |
| 5331 | hash = NR_STRIPE_HASH_LOCKS - 1; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5332 | } |
| 5333 | err = md_allow_write(mddev); |
| 5334 | if (err) |
| 5335 | return err; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5336 | hash = conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5337 | while (size > conf->max_nr_stripes) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5338 | if (grow_one_stripe(conf, hash)) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5339 | conf->max_nr_stripes++; |
| 5340 | else break; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5341 | hash = (hash + 1) % NR_STRIPE_HASH_LOCKS; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5342 | } |
| 5343 | return 0; |
| 5344 | } |
| 5345 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 5346 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5347 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5348 | 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] | 5349 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5350 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 5351 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5352 | int err; |
| 5353 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5354 | if (len >= PAGE_SIZE) |
| 5355 | return -EINVAL; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5356 | if (!conf) |
| 5357 | return -ENODEV; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5358 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5359 | if (kstrtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5360 | return -EINVAL; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 5361 | err = raid5_set_cache_size(mddev, new); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 5362 | if (err) |
| 5363 | return err; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5364 | return len; |
| 5365 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 5366 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5367 | static struct md_sysfs_entry |
| 5368 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 5369 | raid5_show_stripe_cache_size, |
| 5370 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5371 | |
| 5372 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5373 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5374 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5375 | struct r5conf *conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5376 | if (conf) |
| 5377 | return sprintf(page, "%d\n", conf->bypass_threshold); |
| 5378 | else |
| 5379 | return 0; |
| 5380 | } |
| 5381 | |
| 5382 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5383 | 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] | 5384 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5385 | struct r5conf *conf = mddev->private; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 5386 | unsigned long new; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5387 | if (len >= PAGE_SIZE) |
| 5388 | return -EINVAL; |
| 5389 | if (!conf) |
| 5390 | return -ENODEV; |
| 5391 | |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 5392 | if (kstrtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5393 | return -EINVAL; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 5394 | if (new > conf->max_nr_stripes) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5395 | return -EINVAL; |
| 5396 | conf->bypass_threshold = new; |
| 5397 | return len; |
| 5398 | } |
| 5399 | |
| 5400 | static struct md_sysfs_entry |
| 5401 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 5402 | S_IRUGO | S_IWUSR, |
| 5403 | raid5_show_preread_threshold, |
| 5404 | raid5_store_preread_threshold); |
| 5405 | |
| 5406 | static ssize_t |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 5407 | raid5_show_skip_copy(struct mddev *mddev, char *page) |
| 5408 | { |
| 5409 | struct r5conf *conf = mddev->private; |
| 5410 | if (conf) |
| 5411 | return sprintf(page, "%d\n", conf->skip_copy); |
| 5412 | else |
| 5413 | return 0; |
| 5414 | } |
| 5415 | |
| 5416 | static ssize_t |
| 5417 | raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) |
| 5418 | { |
| 5419 | struct r5conf *conf = mddev->private; |
| 5420 | unsigned long new; |
| 5421 | if (len >= PAGE_SIZE) |
| 5422 | return -EINVAL; |
| 5423 | if (!conf) |
| 5424 | return -ENODEV; |
| 5425 | |
| 5426 | if (kstrtoul(page, 10, &new)) |
| 5427 | return -EINVAL; |
| 5428 | new = !!new; |
| 5429 | if (new == conf->skip_copy) |
| 5430 | return len; |
| 5431 | |
| 5432 | mddev_suspend(mddev); |
| 5433 | conf->skip_copy = new; |
| 5434 | if (new) |
| 5435 | mddev->queue->backing_dev_info.capabilities |= |
| 5436 | BDI_CAP_STABLE_WRITES; |
| 5437 | else |
| 5438 | mddev->queue->backing_dev_info.capabilities &= |
| 5439 | ~BDI_CAP_STABLE_WRITES; |
| 5440 | mddev_resume(mddev); |
| 5441 | return len; |
| 5442 | } |
| 5443 | |
| 5444 | static struct md_sysfs_entry |
| 5445 | raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, |
| 5446 | raid5_show_skip_copy, |
| 5447 | raid5_store_skip_copy); |
| 5448 | |
| 5449 | |
| 5450 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5451 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5452 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5453 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5454 | if (conf) |
| 5455 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 5456 | else |
| 5457 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5458 | } |
| 5459 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 5460 | static struct md_sysfs_entry |
| 5461 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5462 | |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 5463 | static ssize_t |
| 5464 | raid5_show_group_thread_cnt(struct mddev *mddev, char *page) |
| 5465 | { |
| 5466 | struct r5conf *conf = mddev->private; |
| 5467 | if (conf) |
| 5468 | return sprintf(page, "%d\n", conf->worker_cnt_per_group); |
| 5469 | else |
| 5470 | return 0; |
| 5471 | } |
| 5472 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5473 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 5474 | int *group_cnt, |
| 5475 | int *worker_cnt_per_group, |
| 5476 | struct r5worker_group **worker_groups); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 5477 | static ssize_t |
| 5478 | raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) |
| 5479 | { |
| 5480 | struct r5conf *conf = mddev->private; |
| 5481 | unsigned long new; |
| 5482 | int err; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5483 | struct r5worker_group *new_groups, *old_groups; |
| 5484 | int group_cnt, worker_cnt_per_group; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 5485 | |
| 5486 | if (len >= PAGE_SIZE) |
| 5487 | return -EINVAL; |
| 5488 | if (!conf) |
| 5489 | return -ENODEV; |
| 5490 | |
| 5491 | if (kstrtoul(page, 10, &new)) |
| 5492 | return -EINVAL; |
| 5493 | |
| 5494 | if (new == conf->worker_cnt_per_group) |
| 5495 | return len; |
| 5496 | |
| 5497 | mddev_suspend(mddev); |
| 5498 | |
| 5499 | old_groups = conf->worker_groups; |
majianpeng | d206dcf | 2013-11-14 15:16:19 +1100 | [diff] [blame] | 5500 | if (old_groups) |
| 5501 | flush_workqueue(raid5_wq); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 5502 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5503 | err = alloc_thread_groups(conf, new, |
| 5504 | &group_cnt, &worker_cnt_per_group, |
| 5505 | &new_groups); |
| 5506 | if (!err) { |
| 5507 | spin_lock_irq(&conf->device_lock); |
| 5508 | conf->group_cnt = group_cnt; |
| 5509 | conf->worker_cnt_per_group = worker_cnt_per_group; |
| 5510 | conf->worker_groups = new_groups; |
| 5511 | spin_unlock_irq(&conf->device_lock); |
| 5512 | |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 5513 | if (old_groups) |
| 5514 | kfree(old_groups[0].workers); |
| 5515 | kfree(old_groups); |
| 5516 | } |
| 5517 | |
| 5518 | mddev_resume(mddev); |
| 5519 | |
| 5520 | if (err) |
| 5521 | return err; |
| 5522 | return len; |
| 5523 | } |
| 5524 | |
| 5525 | static struct md_sysfs_entry |
| 5526 | raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, |
| 5527 | raid5_show_group_thread_cnt, |
| 5528 | raid5_store_group_thread_cnt); |
| 5529 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 5530 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5531 | &raid5_stripecache_size.attr, |
| 5532 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5533 | &raid5_preread_bypass_threshold.attr, |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 5534 | &raid5_group_thread_cnt.attr, |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 5535 | &raid5_skip_copy.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5536 | NULL, |
| 5537 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 5538 | static struct attribute_group raid5_attrs_group = { |
| 5539 | .name = NULL, |
| 5540 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 5541 | }; |
| 5542 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5543 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 5544 | int *group_cnt, |
| 5545 | int *worker_cnt_per_group, |
| 5546 | struct r5worker_group **worker_groups) |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5547 | { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5548 | int i, j, k; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5549 | ssize_t size; |
| 5550 | struct r5worker *workers; |
| 5551 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5552 | *worker_cnt_per_group = cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5553 | if (cnt == 0) { |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5554 | *group_cnt = 0; |
| 5555 | *worker_groups = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5556 | return 0; |
| 5557 | } |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5558 | *group_cnt = num_possible_nodes(); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5559 | size = sizeof(struct r5worker) * cnt; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5560 | workers = kzalloc(size * *group_cnt, GFP_NOIO); |
| 5561 | *worker_groups = kzalloc(sizeof(struct r5worker_group) * |
| 5562 | *group_cnt, GFP_NOIO); |
| 5563 | if (!*worker_groups || !workers) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5564 | kfree(workers); |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5565 | kfree(*worker_groups); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5566 | return -ENOMEM; |
| 5567 | } |
| 5568 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5569 | for (i = 0; i < *group_cnt; i++) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5570 | struct r5worker_group *group; |
| 5571 | |
NeilBrown | 0c775d5 | 2013-11-25 11:12:43 +1100 | [diff] [blame] | 5572 | group = &(*worker_groups)[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5573 | INIT_LIST_HEAD(&group->handle_list); |
| 5574 | group->conf = conf; |
| 5575 | group->workers = workers + i * cnt; |
| 5576 | |
| 5577 | for (j = 0; j < cnt; j++) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5578 | struct r5worker *worker = group->workers + j; |
| 5579 | worker->group = group; |
| 5580 | INIT_WORK(&worker->work, raid5_do_work); |
| 5581 | |
| 5582 | for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) |
| 5583 | INIT_LIST_HEAD(worker->temp_inactive_list + k); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5584 | } |
| 5585 | } |
| 5586 | |
| 5587 | return 0; |
| 5588 | } |
| 5589 | |
| 5590 | static void free_thread_groups(struct r5conf *conf) |
| 5591 | { |
| 5592 | if (conf->worker_groups) |
| 5593 | kfree(conf->worker_groups[0].workers); |
| 5594 | kfree(conf->worker_groups); |
| 5595 | conf->worker_groups = NULL; |
| 5596 | } |
| 5597 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5598 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5599 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5600 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5601 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5602 | |
| 5603 | if (!sectors) |
| 5604 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5605 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 5606 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5607 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5608 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5609 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5610 | sectors &= ~((sector_t)mddev->new_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 5611 | return sectors * (raid_disks - conf->max_degraded); |
| 5612 | } |
| 5613 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 5614 | static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 5615 | { |
| 5616 | safe_put_page(percpu->spare_page); |
| 5617 | kfree(percpu->scribble); |
| 5618 | percpu->spare_page = NULL; |
| 5619 | percpu->scribble = NULL; |
| 5620 | } |
| 5621 | |
| 5622 | static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 5623 | { |
| 5624 | if (conf->level == 6 && !percpu->spare_page) |
| 5625 | percpu->spare_page = alloc_page(GFP_KERNEL); |
| 5626 | if (!percpu->scribble) |
| 5627 | percpu->scribble = kmalloc(conf->scribble_len, GFP_KERNEL); |
| 5628 | |
| 5629 | if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) { |
| 5630 | free_scratch_buffer(conf, percpu); |
| 5631 | return -ENOMEM; |
| 5632 | } |
| 5633 | |
| 5634 | return 0; |
| 5635 | } |
| 5636 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5637 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5638 | { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5639 | unsigned long cpu; |
| 5640 | |
| 5641 | if (!conf->percpu) |
| 5642 | return; |
| 5643 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5644 | #ifdef CONFIG_HOTPLUG_CPU |
| 5645 | unregister_cpu_notifier(&conf->cpu_notify); |
| 5646 | #endif |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 5647 | |
| 5648 | get_online_cpus(); |
| 5649 | for_each_possible_cpu(cpu) |
| 5650 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5651 | put_online_cpus(); |
| 5652 | |
| 5653 | free_percpu(conf->percpu); |
| 5654 | } |
| 5655 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5656 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5657 | { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5658 | free_thread_groups(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5659 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5660 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5661 | kfree(conf->disks); |
| 5662 | kfree(conf->stripe_hashtbl); |
| 5663 | kfree(conf); |
| 5664 | } |
| 5665 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5666 | #ifdef CONFIG_HOTPLUG_CPU |
| 5667 | static int raid456_cpu_notify(struct notifier_block *nfb, unsigned long action, |
| 5668 | void *hcpu) |
| 5669 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5670 | struct r5conf *conf = container_of(nfb, struct r5conf, cpu_notify); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5671 | long cpu = (long)hcpu; |
| 5672 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 5673 | |
| 5674 | switch (action) { |
| 5675 | case CPU_UP_PREPARE: |
| 5676 | case CPU_UP_PREPARE_FROZEN: |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 5677 | if (alloc_scratch_buffer(conf, percpu)) { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5678 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 5679 | __func__, cpu); |
Akinobu Mita | 55af6bb | 2010-05-26 14:43:35 -0700 | [diff] [blame] | 5680 | return notifier_from_errno(-ENOMEM); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5681 | } |
| 5682 | break; |
| 5683 | case CPU_DEAD: |
| 5684 | case CPU_DEAD_FROZEN: |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 5685 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5686 | break; |
| 5687 | default: |
| 5688 | break; |
| 5689 | } |
| 5690 | return NOTIFY_OK; |
| 5691 | } |
| 5692 | #endif |
| 5693 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5694 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5695 | { |
| 5696 | unsigned long cpu; |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 5697 | int err = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5698 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 5699 | conf->percpu = alloc_percpu(struct raid5_percpu); |
| 5700 | if (!conf->percpu) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5701 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5702 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5703 | #ifdef CONFIG_HOTPLUG_CPU |
| 5704 | conf->cpu_notify.notifier_call = raid456_cpu_notify; |
| 5705 | conf->cpu_notify.priority = 0; |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 5706 | err = register_cpu_notifier(&conf->cpu_notify); |
| 5707 | if (err) |
| 5708 | return err; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5709 | #endif |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 5710 | |
| 5711 | get_online_cpus(); |
| 5712 | for_each_present_cpu(cpu) { |
| 5713 | err = alloc_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); |
| 5714 | if (err) { |
| 5715 | pr_err("%s: failed memory allocation for cpu%ld\n", |
| 5716 | __func__, cpu); |
| 5717 | break; |
| 5718 | } |
| 5719 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5720 | put_online_cpus(); |
| 5721 | |
| 5722 | return err; |
| 5723 | } |
| 5724 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5725 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5726 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5727 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5728 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5729 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5730 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5731 | char pers_name[6]; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5732 | int i; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5733 | int group_cnt, worker_cnt_per_group; |
| 5734 | struct r5worker_group *new_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5735 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5736 | if (mddev->new_level != 5 |
| 5737 | && mddev->new_level != 4 |
| 5738 | && mddev->new_level != 6) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5739 | 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] | 5740 | mdname(mddev), mddev->new_level); |
| 5741 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5742 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5743 | if ((mddev->new_level == 5 |
| 5744 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 5745 | (mddev->new_level == 6 |
| 5746 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5747 | printk(KERN_ERR "md/raid:%s: layout %d not supported\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5748 | mdname(mddev), mddev->new_layout); |
| 5749 | return ERR_PTR(-EIO); |
| 5750 | } |
| 5751 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5752 | 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] | 5753 | mdname(mddev), mddev->raid_disks); |
| 5754 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5755 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5756 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5757 | if (!mddev->new_chunk_sectors || |
| 5758 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 5759 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5760 | printk(KERN_ERR "md/raid:%s: invalid chunk size %d\n", |
| 5761 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5762 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 5763 | } |
| 5764 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5765 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5766 | if (conf == NULL) |
| 5767 | goto abort; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5768 | /* Don't enable multi-threading by default*/ |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 5769 | if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group, |
| 5770 | &new_group)) { |
| 5771 | conf->group_cnt = group_cnt; |
| 5772 | conf->worker_cnt_per_group = worker_cnt_per_group; |
| 5773 | conf->worker_groups = new_group; |
| 5774 | } else |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5775 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 5776 | spin_lock_init(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5777 | seqcount_init(&conf->gen_lock); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 5778 | init_waitqueue_head(&conf->wait_for_stripe); |
| 5779 | init_waitqueue_head(&conf->wait_for_overlap); |
| 5780 | INIT_LIST_HEAD(&conf->handle_list); |
| 5781 | INIT_LIST_HEAD(&conf->hold_list); |
| 5782 | INIT_LIST_HEAD(&conf->delayed_list); |
| 5783 | INIT_LIST_HEAD(&conf->bitmap_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5784 | init_llist_head(&conf->released_stripes); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 5785 | atomic_set(&conf->active_stripes, 0); |
| 5786 | atomic_set(&conf->preread_active_stripes, 0); |
| 5787 | atomic_set(&conf->active_aligned_reads, 0); |
| 5788 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 5789 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5790 | |
| 5791 | conf->raid_disks = mddev->raid_disks; |
| 5792 | if (mddev->reshape_position == MaxSector) |
| 5793 | conf->previous_raid_disks = mddev->raid_disks; |
| 5794 | else |
| 5795 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5796 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
| 5797 | conf->scribble_len = scribble_len(max_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5798 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5799 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5800 | GFP_KERNEL); |
| 5801 | if (!conf->disks) |
| 5802 | goto abort; |
| 5803 | |
| 5804 | conf->mddev = mddev; |
| 5805 | |
| 5806 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 5807 | goto abort; |
| 5808 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5809 | /* We init hash_locks[0] separately to that it can be used |
| 5810 | * as the reference lock in the spin_lock_nest_lock() call |
| 5811 | * in lock_all_device_hash_locks_irq in order to convince |
| 5812 | * lockdep that we know what we are doing. |
| 5813 | */ |
| 5814 | spin_lock_init(conf->hash_locks); |
| 5815 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5816 | spin_lock_init(conf->hash_locks + i); |
| 5817 | |
| 5818 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5819 | INIT_LIST_HEAD(conf->inactive_list + i); |
| 5820 | |
| 5821 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5822 | INIT_LIST_HEAD(conf->temp_inactive_list + i); |
| 5823 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5824 | conf->level = mddev->new_level; |
| 5825 | if (raid5_alloc_percpu(conf) != 0) |
| 5826 | goto abort; |
| 5827 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5828 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5829 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 5830 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5831 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5832 | if (raid_disk >= max_disks |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5833 | || raid_disk < 0) |
| 5834 | continue; |
| 5835 | disk = conf->disks + raid_disk; |
| 5836 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5837 | if (test_bit(Replacement, &rdev->flags)) { |
| 5838 | if (disk->replacement) |
| 5839 | goto abort; |
| 5840 | disk->replacement = rdev; |
| 5841 | } else { |
| 5842 | if (disk->rdev) |
| 5843 | goto abort; |
| 5844 | disk->rdev = rdev; |
| 5845 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5846 | |
| 5847 | if (test_bit(In_sync, &rdev->flags)) { |
| 5848 | char b[BDEVNAME_SIZE]; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5849 | printk(KERN_INFO "md/raid:%s: device %s operational as raid" |
| 5850 | " disk %d\n", |
| 5851 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 5852 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5853 | /* Cannot rely on bitmap to complete recovery */ |
| 5854 | conf->fullsync = 1; |
| 5855 | } |
| 5856 | |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5857 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5858 | conf->level = mddev->new_level; |
| 5859 | if (conf->level == 6) |
| 5860 | conf->max_degraded = 2; |
| 5861 | else |
| 5862 | conf->max_degraded = 1; |
| 5863 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5864 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5865 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5866 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 5867 | conf->prev_algo = mddev->layout; |
| 5868 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5869 | |
| 5870 | memory = conf->max_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 5871 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5872 | atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5873 | if (grow_stripes(conf, NR_STRIPES)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5874 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5875 | "md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 5876 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5877 | goto abort; |
| 5878 | } else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5879 | printk(KERN_INFO "md/raid:%s: allocated %dkB\n", |
| 5880 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5881 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 5882 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 5883 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5884 | if (!conf->thread) { |
| 5885 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5886 | "md/raid:%s: couldn't allocate thread.\n", |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5887 | mdname(mddev)); |
| 5888 | goto abort; |
| 5889 | } |
| 5890 | |
| 5891 | return conf; |
| 5892 | |
| 5893 | abort: |
| 5894 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 5895 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5896 | return ERR_PTR(-EIO); |
| 5897 | } else |
| 5898 | return ERR_PTR(-ENOMEM); |
| 5899 | } |
| 5900 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5901 | |
| 5902 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 5903 | { |
| 5904 | switch (algo) { |
| 5905 | case ALGORITHM_PARITY_0: |
| 5906 | if (raid_disk < max_degraded) |
| 5907 | return 1; |
| 5908 | break; |
| 5909 | case ALGORITHM_PARITY_N: |
| 5910 | if (raid_disk >= raid_disks - max_degraded) |
| 5911 | return 1; |
| 5912 | break; |
| 5913 | case ALGORITHM_PARITY_0_6: |
| 5914 | if (raid_disk == 0 || |
| 5915 | raid_disk == raid_disks - 1) |
| 5916 | return 1; |
| 5917 | break; |
| 5918 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 5919 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 5920 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 5921 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 5922 | if (raid_disk == raid_disks - 1) |
| 5923 | return 1; |
| 5924 | } |
| 5925 | return 0; |
| 5926 | } |
| 5927 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5928 | static int run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5929 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5930 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 5931 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5932 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5933 | struct md_rdev *rdev; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5934 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5935 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5936 | long long min_offset_diff = 0; |
| 5937 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 5938 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5939 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5940 | printk(KERN_NOTICE "md/raid:%s: not clean" |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 5941 | " -- starting background reconstruction\n", |
| 5942 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5943 | |
| 5944 | rdev_for_each(rdev, mddev) { |
| 5945 | long long diff; |
| 5946 | if (rdev->raid_disk < 0) |
| 5947 | continue; |
| 5948 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 5949 | if (first) { |
| 5950 | min_offset_diff = diff; |
| 5951 | first = 0; |
| 5952 | } else if (mddev->reshape_backwards && |
| 5953 | diff < min_offset_diff) |
| 5954 | min_offset_diff = diff; |
| 5955 | else if (!mddev->reshape_backwards && |
| 5956 | diff > min_offset_diff) |
| 5957 | min_offset_diff = diff; |
| 5958 | } |
| 5959 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5960 | if (mddev->reshape_position != MaxSector) { |
| 5961 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5962 | * Difficulties arise if the stripe we would write to |
| 5963 | * next is at or after the stripe we would read from next. |
| 5964 | * For a reshape that changes the number of devices, this |
| 5965 | * is only possible for a very short time, and mdadm makes |
| 5966 | * sure that time appears to have past before assembling |
| 5967 | * the array. So we fail if that time hasn't passed. |
| 5968 | * For a reshape that keeps the number of devices the same |
| 5969 | * mdadm must be monitoring the reshape can keeping the |
| 5970 | * critical areas read-only and backed up. It will start |
| 5971 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5972 | */ |
| 5973 | sector_t here_new, here_old; |
| 5974 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 5975 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5976 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 5977 | if (mddev->new_level != mddev->level) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5978 | printk(KERN_ERR "md/raid:%s: unsupported reshape " |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5979 | "required - aborting.\n", |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5980 | mdname(mddev)); |
| 5981 | return -EINVAL; |
| 5982 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5983 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 5984 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5985 | * further up in new geometry must map after here in old |
| 5986 | * geometry. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5987 | */ |
| 5988 | here_new = mddev->reshape_position; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 5989 | if (sector_div(here_new, mddev->new_chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5990 | (mddev->raid_disks - max_degraded))) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 5991 | printk(KERN_ERR "md/raid:%s: reshape_position not " |
| 5992 | "on a stripe boundary\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5993 | return -EINVAL; |
| 5994 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 5995 | reshape_offset = here_new * mddev->new_chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5996 | /* here_new is the stripe we will write to */ |
| 5997 | here_old = mddev->reshape_position; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 5998 | sector_div(here_old, mddev->chunk_sectors * |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5999 | (old_disks-max_degraded)); |
| 6000 | /* here_old is the first stripe that we might need to read |
| 6001 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 6002 | if (mddev->delta_disks == 0) { |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6003 | if ((here_new * mddev->new_chunk_sectors != |
| 6004 | here_old * mddev->chunk_sectors)) { |
| 6005 | printk(KERN_ERR "md/raid:%s: reshape position is" |
| 6006 | " confused - aborting\n", mdname(mddev)); |
| 6007 | return -EINVAL; |
| 6008 | } |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 6009 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6010 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 6011 | * and taking constant backups. |
| 6012 | * mdadm always starts a situation like this in |
| 6013 | * readonly mode so it can take control before |
| 6014 | * allowing any writes. So just check for that. |
| 6015 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6016 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 6017 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 6018 | /* not really in-place - so OK */; |
| 6019 | else if (mddev->ro == 0) { |
| 6020 | printk(KERN_ERR "md/raid:%s: in-place reshape " |
| 6021 | "must be started in read-only mode " |
| 6022 | "- aborting\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6023 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 6024 | return -EINVAL; |
| 6025 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6026 | } else if (mddev->reshape_backwards |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6027 | ? (here_new * mddev->new_chunk_sectors + min_offset_diff <= |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 6028 | here_old * mddev->chunk_sectors) |
| 6029 | : (here_new * mddev->new_chunk_sectors >= |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6030 | here_old * mddev->chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6031 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6032 | printk(KERN_ERR "md/raid:%s: reshape_position too early for " |
| 6033 | "auto-recovery - aborting.\n", |
| 6034 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6035 | return -EINVAL; |
| 6036 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6037 | printk(KERN_INFO "md/raid:%s: reshape will continue\n", |
| 6038 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6039 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6040 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6041 | BUG_ON(mddev->level != mddev->new_level); |
| 6042 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6043 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6044 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6045 | } |
| 6046 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6047 | if (mddev->private == NULL) |
| 6048 | conf = setup_conf(mddev); |
| 6049 | else |
| 6050 | conf = mddev->private; |
| 6051 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6052 | if (IS_ERR(conf)) |
| 6053 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6054 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6055 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6056 | mddev->thread = conf->thread; |
| 6057 | conf->thread = NULL; |
| 6058 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6060 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 6061 | i++) { |
| 6062 | rdev = conf->disks[i].rdev; |
| 6063 | if (!rdev && conf->disks[i].replacement) { |
| 6064 | /* The replacement is all we have yet */ |
| 6065 | rdev = conf->disks[i].replacement; |
| 6066 | conf->disks[i].replacement = NULL; |
| 6067 | clear_bit(Replacement, &rdev->flags); |
| 6068 | conf->disks[i].rdev = rdev; |
| 6069 | } |
| 6070 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6071 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6072 | if (conf->disks[i].replacement && |
| 6073 | conf->reshape_progress != MaxSector) { |
| 6074 | /* replacements and reshape simply do not mix. */ |
| 6075 | printk(KERN_ERR "md: cannot handle concurrent " |
| 6076 | "replacement and reshape.\n"); |
| 6077 | goto abort; |
| 6078 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 6079 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6080 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 6081 | continue; |
| 6082 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6083 | /* This disc is not fully in-sync. However if it |
| 6084 | * just stored parity (beyond the recovery_offset), |
| 6085 | * when we don't need to be concerned about the |
| 6086 | * array being dirty. |
| 6087 | * When reshape goes 'backwards', we never have |
| 6088 | * partially completed devices, so we only need |
| 6089 | * to worry about reshape going forwards. |
| 6090 | */ |
| 6091 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 6092 | if (mddev->major_version == 0 && |
| 6093 | mddev->minor_version > 90) |
| 6094 | rdev->recovery_offset = reshape_offset; |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 6095 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6096 | if (rdev->recovery_offset < reshape_offset) { |
| 6097 | /* We need to check old and new layout */ |
| 6098 | if (!only_parity(rdev->raid_disk, |
| 6099 | conf->algorithm, |
| 6100 | conf->raid_disks, |
| 6101 | conf->max_degraded)) |
| 6102 | continue; |
| 6103 | } |
| 6104 | if (!only_parity(rdev->raid_disk, |
| 6105 | conf->prev_algo, |
| 6106 | conf->previous_raid_disks, |
| 6107 | conf->max_degraded)) |
| 6108 | continue; |
| 6109 | dirty_parity_disks++; |
| 6110 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6111 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6112 | /* |
| 6113 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 6114 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6115 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6116 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 6117 | if (has_failed(conf)) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6118 | printk(KERN_ERR "md/raid:%s: not enough operational devices" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6119 | " (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 6120 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | goto abort; |
| 6122 | } |
| 6123 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6124 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6125 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6126 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 6127 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6128 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6129 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6130 | if (mddev->ok_start_degraded) |
| 6131 | printk(KERN_WARNING |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6132 | "md/raid:%s: starting dirty degraded array" |
| 6133 | " - data corruption possible.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6134 | mdname(mddev)); |
| 6135 | else { |
| 6136 | printk(KERN_ERR |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6137 | "md/raid:%s: cannot start dirty degraded array.\n", |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6138 | mdname(mddev)); |
| 6139 | goto abort; |
| 6140 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6141 | } |
| 6142 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6143 | if (mddev->degraded == 0) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6144 | printk(KERN_INFO "md/raid:%s: raid level %d active with %d out of %d" |
| 6145 | " devices, algorithm %d\n", mdname(mddev), conf->level, |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 6146 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 6147 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6148 | else |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6149 | printk(KERN_ALERT "md/raid:%s: raid level %d active with %d" |
| 6150 | " out of %d devices, algorithm %d\n", |
| 6151 | mdname(mddev), conf->level, |
| 6152 | mddev->raid_disks - mddev->degraded, |
| 6153 | mddev->raid_disks, mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6154 | |
| 6155 | print_raid5_conf(conf); |
| 6156 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6157 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6158 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6159 | atomic_set(&conf->reshape_stripes, 0); |
| 6160 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6161 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 6162 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 6163 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6164 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6165 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6166 | } |
| 6167 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6168 | |
| 6169 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 6170 | if (mddev->to_remove == &raid5_attrs_group) |
| 6171 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 6172 | else if (mddev->kobj.sd && |
| 6173 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6174 | printk(KERN_WARNING |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 6175 | "raid5: failed to create sysfs attributes for %s\n", |
NeilBrown | 5e55e2f | 2007-03-26 21:32:14 -0800 | [diff] [blame] | 6176 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 6177 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 6178 | |
| 6179 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 6180 | int chunk_size; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 6181 | bool discard_supported = true; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 6182 | /* read-ahead size must cover two whole stripes, which |
| 6183 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 6184 | * number of raid devices |
| 6185 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6186 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 6187 | int stripe = data_disks * |
| 6188 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 6189 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 6190 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 6191 | |
| 6192 | blk_queue_merge_bvec(mddev->queue, raid5_mergeable_bvec); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 6193 | |
| 6194 | mddev->queue->backing_dev_info.congested_data = mddev; |
| 6195 | mddev->queue->backing_dev_info.congested_fn = raid5_congested; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 6196 | |
| 6197 | chunk_size = mddev->chunk_sectors << 9; |
| 6198 | blk_queue_io_min(mddev->queue, chunk_size); |
| 6199 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 6200 | (conf->raid_disks - conf->max_degraded)); |
Kent Overstreet | c78afc6 | 2013-07-11 22:39:53 -0700 | [diff] [blame] | 6201 | mddev->queue->limits.raid_partial_stripes_expensive = 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 6202 | /* |
| 6203 | * We can only discard a whole stripe. It doesn't make sense to |
| 6204 | * discard data disk but write parity disk |
| 6205 | */ |
| 6206 | stripe = stripe * PAGE_SIZE; |
NeilBrown | 4ac6875 | 2012-11-19 13:11:26 +1100 | [diff] [blame] | 6207 | /* Round up to power of 2, as discard handling |
| 6208 | * currently assumes that */ |
| 6209 | while ((stripe-1) & stripe) |
| 6210 | stripe = (stripe | (stripe-1)) + 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 6211 | mddev->queue->limits.discard_alignment = stripe; |
| 6212 | mddev->queue->limits.discard_granularity = stripe; |
| 6213 | /* |
| 6214 | * unaligned part of discard request will be ignored, so can't |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 6215 | * guarantee discard_zeroes_data |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 6216 | */ |
| 6217 | mddev->queue->limits.discard_zeroes_data = 0; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 6218 | |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 6219 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
| 6220 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6221 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 6222 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 6223 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6224 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 6225 | rdev->new_data_offset << 9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 6226 | /* |
| 6227 | * discard_zeroes_data is required, otherwise data |
| 6228 | * could be lost. Consider a scenario: discard a stripe |
| 6229 | * (the stripe could be inconsistent if |
| 6230 | * discard_zeroes_data is 0); write one disk of the |
| 6231 | * stripe (the stripe could be inconsistent again |
| 6232 | * depending on which disks are used to calculate |
| 6233 | * parity); the disk is broken; The stripe data of this |
| 6234 | * disk is lost. |
| 6235 | */ |
| 6236 | if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || |
| 6237 | !bdev_get_queue(rdev->bdev)-> |
| 6238 | limits.discard_zeroes_data) |
| 6239 | discard_supported = false; |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 6240 | /* Unfortunately, discard_zeroes_data is not currently |
| 6241 | * a guarantee - just a hint. So we only allow DISCARD |
| 6242 | * if the sysadmin has confirmed that only safe devices |
| 6243 | * are in use by setting a module parameter. |
| 6244 | */ |
| 6245 | if (!devices_handle_discard_safely) { |
| 6246 | if (discard_supported) { |
| 6247 | pr_info("md/raid456: discard support disabled due to uncertainty.\n"); |
| 6248 | pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n"); |
| 6249 | } |
| 6250 | discard_supported = false; |
| 6251 | } |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6252 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 6253 | |
| 6254 | if (discard_supported && |
| 6255 | mddev->queue->limits.max_discard_sectors >= stripe && |
| 6256 | mddev->queue->limits.discard_granularity >= stripe) |
| 6257 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 6258 | mddev->queue); |
| 6259 | else |
| 6260 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, |
| 6261 | mddev->queue); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6262 | } |
| 6263 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6264 | return 0; |
| 6265 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6266 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 6267 | print_raid5_conf(conf); |
| 6268 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6269 | mddev->private = NULL; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6270 | 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] | 6271 | return -EIO; |
| 6272 | } |
| 6273 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6274 | static int stop(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6275 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6276 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 6278 | md_unregister_thread(&mddev->thread); |
NeilBrown | 11d8a6e | 2010-07-26 11:57:07 +1000 | [diff] [blame] | 6279 | if (mddev->queue) |
| 6280 | mddev->queue->backing_dev_info.congested_fn = NULL; |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6281 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 6282 | mddev->private = NULL; |
| 6283 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6284 | return 0; |
| 6285 | } |
| 6286 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6287 | static void status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6288 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6289 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6290 | int i; |
| 6291 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6292 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
| 6293 | mddev->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 6294 | 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] | 6295 | for (i = 0; i < conf->raid_disks; i++) |
| 6296 | seq_printf (seq, "%s", |
| 6297 | conf->disks[i].rdev && |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6298 | test_bit(In_sync, &conf->disks[i].rdev->flags) ? "U" : "_"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | } |
| 6301 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6302 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6303 | { |
| 6304 | int i; |
| 6305 | struct disk_info *tmp; |
| 6306 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6307 | printk(KERN_DEBUG "RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6308 | if (!conf) { |
| 6309 | printk("(conf==NULL)\n"); |
| 6310 | return; |
| 6311 | } |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6312 | printk(KERN_DEBUG " --- level:%d rd:%d wd:%d\n", conf->level, |
| 6313 | conf->raid_disks, |
| 6314 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6315 | |
| 6316 | for (i = 0; i < conf->raid_disks; i++) { |
| 6317 | char b[BDEVNAME_SIZE]; |
| 6318 | tmp = conf->disks + i; |
| 6319 | if (tmp->rdev) |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6320 | printk(KERN_DEBUG " disk %d, o:%d, dev:%s\n", |
| 6321 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 6322 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6323 | } |
| 6324 | } |
| 6325 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6326 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6327 | { |
| 6328 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6329 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6330 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 6331 | int count = 0; |
| 6332 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6333 | |
| 6334 | for (i = 0; i < conf->raid_disks; i++) { |
| 6335 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6336 | if (tmp->replacement |
| 6337 | && tmp->replacement->recovery_offset == MaxSector |
| 6338 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 6339 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 6340 | /* Replacement has just become active. */ |
| 6341 | if (!tmp->rdev |
| 6342 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 6343 | count++; |
| 6344 | if (tmp->rdev) { |
| 6345 | /* Replaced device not technically faulty, |
| 6346 | * but we need to be sure it gets removed |
| 6347 | * and never re-added. |
| 6348 | */ |
| 6349 | set_bit(Faulty, &tmp->rdev->flags); |
| 6350 | sysfs_notify_dirent_safe( |
| 6351 | tmp->rdev->sysfs_state); |
| 6352 | } |
| 6353 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 6354 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 6355 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6356 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 6357 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 6358 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 6359 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6360 | } |
| 6361 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 6362 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6363 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 6364 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6365 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 6366 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6367 | } |
| 6368 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6369 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6370 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6371 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6372 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 6373 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 6374 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6375 | struct disk_info *p = conf->disks + number; |
| 6376 | |
| 6377 | print_raid5_conf(conf); |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 6378 | if (rdev == p->rdev) |
| 6379 | rdevp = &p->rdev; |
| 6380 | else if (rdev == p->replacement) |
| 6381 | rdevp = &p->replacement; |
| 6382 | else |
| 6383 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6384 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 6385 | if (number >= conf->raid_disks && |
| 6386 | conf->reshape_progress == MaxSector) |
| 6387 | clear_bit(In_sync, &rdev->flags); |
| 6388 | |
| 6389 | if (test_bit(In_sync, &rdev->flags) || |
| 6390 | atomic_read(&rdev->nr_pending)) { |
| 6391 | err = -EBUSY; |
| 6392 | goto abort; |
| 6393 | } |
| 6394 | /* Only remove non-faulty devices if recovery |
| 6395 | * isn't possible. |
| 6396 | */ |
| 6397 | if (!test_bit(Faulty, &rdev->flags) && |
| 6398 | mddev->recovery_disabled != conf->recovery_disabled && |
| 6399 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6400 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 6401 | number < conf->raid_disks) { |
| 6402 | err = -EBUSY; |
| 6403 | goto abort; |
| 6404 | } |
| 6405 | *rdevp = NULL; |
| 6406 | synchronize_rcu(); |
| 6407 | if (atomic_read(&rdev->nr_pending)) { |
| 6408 | /* lost the race, try later */ |
| 6409 | err = -EBUSY; |
| 6410 | *rdevp = rdev; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6411 | } else if (p->replacement) { |
| 6412 | /* We must have just cleared 'rdev' */ |
| 6413 | p->rdev = p->replacement; |
| 6414 | clear_bit(Replacement, &p->replacement->flags); |
| 6415 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 6416 | * but will never see neither - if they are careful |
| 6417 | */ |
| 6418 | p->replacement = NULL; |
| 6419 | clear_bit(WantReplacement, &rdev->flags); |
| 6420 | } else |
| 6421 | /* We might have just removed the Replacement as faulty- |
| 6422 | * clear the bit just in case |
| 6423 | */ |
| 6424 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6425 | abort: |
| 6426 | |
| 6427 | print_raid5_conf(conf); |
| 6428 | return err; |
| 6429 | } |
| 6430 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6431 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6432 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6433 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6434 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6435 | int disk; |
| 6436 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 6437 | int first = 0; |
| 6438 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6439 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 6440 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 6441 | return -EBUSY; |
| 6442 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 6443 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6444 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6445 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 6447 | if (rdev->raid_disk >= 0) |
| 6448 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6449 | |
| 6450 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6451 | * find the disk ... but prefer rdev->saved_raid_disk |
| 6452 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6454 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 6455 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6456 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 6457 | first = rdev->saved_raid_disk; |
| 6458 | |
| 6459 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6460 | p = conf->disks + disk; |
| 6461 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 6462 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6463 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6464 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6465 | if (rdev->saved_raid_disk != disk) |
| 6466 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 6467 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 6468 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6469 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 6470 | } |
| 6471 | for (disk = first; disk <= last; disk++) { |
| 6472 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6473 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 6474 | p->replacement == NULL) { |
| 6475 | clear_bit(In_sync, &rdev->flags); |
| 6476 | set_bit(Replacement, &rdev->flags); |
| 6477 | rdev->raid_disk = disk; |
| 6478 | err = 0; |
| 6479 | conf->fullsync = 1; |
| 6480 | rcu_assign_pointer(p->replacement, rdev); |
| 6481 | break; |
| 6482 | } |
| 6483 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 6484 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6485 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 6486 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6487 | } |
| 6488 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6489 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6490 | { |
| 6491 | /* no resync is happening, and there is enough space |
| 6492 | * on all devices, so we can resize. |
| 6493 | * We need to make sure resync covers any new space. |
| 6494 | * If the array is shrinking we should possibly wait until |
| 6495 | * any io in the removed space completes, but it hardly seems |
| 6496 | * worth it. |
| 6497 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6498 | sector_t newsize; |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6499 | sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6500 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 6501 | if (mddev->external_size && |
| 6502 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6503 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 6504 | if (mddev->bitmap) { |
| 6505 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 6506 | if (ret) |
| 6507 | return ret; |
| 6508 | } |
| 6509 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 6510 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6511 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 6512 | if (sectors > mddev->dev_sectors && |
| 6513 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6514 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6515 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 6516 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6517 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 6518 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6519 | return 0; |
| 6520 | } |
| 6521 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6522 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 6523 | { |
| 6524 | /* Can only proceed if there are plenty of stripe_heads. |
| 6525 | * We need a minimum of one full stripe,, and for sensible progress |
| 6526 | * it is best to have about 4 times that. |
| 6527 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 6528 | * allow for chunk sizes up to 256K, which is probably OK. |
| 6529 | * If the chunk size is greater, user-space should request more |
| 6530 | * stripe_heads first. |
| 6531 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6532 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 6533 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 6534 | > conf->max_nr_stripes || |
| 6535 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
| 6536 | > conf->max_nr_stripes) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6537 | printk(KERN_WARNING "md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 6538 | mdname(mddev), |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 6539 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 6540 | / STRIPE_SIZE)*4); |
| 6541 | return 0; |
| 6542 | } |
| 6543 | return 1; |
| 6544 | } |
| 6545 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6546 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6547 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6548 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6549 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6550 | if (mddev->delta_disks == 0 && |
| 6551 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6552 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6553 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 6554 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6555 | return -EINVAL; |
NeilBrown | fdcfbbb | 2013-07-04 16:38:16 +1000 | [diff] [blame] | 6556 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6557 | /* We might be able to shrink, but the devices must |
| 6558 | * be made bigger first. |
| 6559 | * For raid6, 4 is the minimum size. |
| 6560 | * Otherwise 2 is the minimum |
| 6561 | */ |
| 6562 | int min = 2; |
| 6563 | if (mddev->level == 6) |
| 6564 | min = 4; |
| 6565 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 6566 | return -EINVAL; |
| 6567 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6568 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 6569 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6570 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6571 | |
NeilBrown | e56108d6 | 2012-10-11 14:24:13 +1100 | [diff] [blame] | 6572 | return resize_stripes(conf, (conf->previous_raid_disks |
| 6573 | + mddev->delta_disks)); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6574 | } |
| 6575 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6576 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6577 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6578 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6579 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6580 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 6581 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6582 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6583 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6584 | return -EBUSY; |
| 6585 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 6586 | if (!check_stripe_cache(mddev)) |
| 6587 | return -ENOSPC; |
| 6588 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 6589 | if (has_failed(conf)) |
| 6590 | return -EINVAL; |
| 6591 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6592 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6593 | if (!test_bit(In_sync, &rdev->flags) |
| 6594 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6595 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6596 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6597 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6598 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6599 | /* Not enough devices even to make a degraded array |
| 6600 | * of that size |
| 6601 | */ |
| 6602 | return -EINVAL; |
| 6603 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6604 | /* Refuse to reduce size of the array. Any reductions in |
| 6605 | * array size must be through explicit setting of array_size |
| 6606 | * attribute. |
| 6607 | */ |
| 6608 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 6609 | < mddev->array_sectors) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6610 | printk(KERN_ERR "md/raid:%s: array size must be reduced " |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6611 | "before number of disks\n", mdname(mddev)); |
| 6612 | return -EINVAL; |
| 6613 | } |
| 6614 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6615 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6616 | spin_lock_irq(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 6617 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6618 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6619 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6620 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 6621 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6622 | conf->prev_algo = conf->algorithm; |
| 6623 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6624 | conf->generation++; |
| 6625 | /* Code that selects data_offset needs to see the generation update |
| 6626 | * if reshape_progress has been set - so a memory barrier needed. |
| 6627 | */ |
| 6628 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6629 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6630 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 6631 | else |
| 6632 | conf->reshape_progress = 0; |
| 6633 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 6634 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6635 | spin_unlock_irq(&conf->device_lock); |
| 6636 | |
NeilBrown | 4d77e3b | 2013-08-27 15:57:47 +1000 | [diff] [blame] | 6637 | /* Now make sure any requests that proceeded on the assumption |
| 6638 | * the reshape wasn't running - like Discard or Read - have |
| 6639 | * completed. |
| 6640 | */ |
| 6641 | mddev_suspend(mddev); |
| 6642 | mddev_resume(mddev); |
| 6643 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6644 | /* Add some new drives, as many as will fit. |
| 6645 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 6646 | * Don't add devices if we are reducing the number of |
| 6647 | * devices in the array. This is because it is not possible |
| 6648 | * to correctly record the "partially reconstructed" state of |
| 6649 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6650 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6651 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6652 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6653 | if (rdev->raid_disk < 0 && |
| 6654 | !test_bit(Faulty, &rdev->flags)) { |
| 6655 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6656 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 6657 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6658 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 6659 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6660 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 6661 | |
| 6662 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6663 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6664 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6665 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 6666 | && !test_bit(Faulty, &rdev->flags)) { |
| 6667 | /* This is a spare that was manually added */ |
| 6668 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6669 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6670 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 6671 | /* When a reshape changes the number of devices, |
| 6672 | * ->degraded is measured against the larger of the |
| 6673 | * pre and post number of devices. |
| 6674 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6675 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6676 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6677 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 6678 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 6679 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 6680 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 6681 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6682 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6683 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 6684 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 6685 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 6686 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 6687 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 6688 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6689 | if (!mddev->sync_thread) { |
| 6690 | mddev->recovery = 0; |
| 6691 | spin_lock_irq(&conf->device_lock); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 6692 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6693 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 6694 | mddev->new_chunk_sectors = |
| 6695 | conf->chunk_sectors = conf->prev_chunk_sectors; |
| 6696 | mddev->new_layout = conf->algorithm = conf->prev_algo; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6697 | rdev_for_each(rdev, mddev) |
| 6698 | rdev->new_data_offset = rdev->data_offset; |
| 6699 | smp_wmb(); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 6700 | conf->generation --; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6701 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 6702 | mddev->reshape_position = MaxSector; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 6703 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6704 | spin_unlock_irq(&conf->device_lock); |
| 6705 | return -EAGAIN; |
| 6706 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6707 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6708 | md_wakeup_thread(mddev->sync_thread); |
| 6709 | md_new_event(mddev); |
| 6710 | return 0; |
| 6711 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6712 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6713 | /* This is called from the reshape thread and should make any |
| 6714 | * changes needed in 'conf' |
| 6715 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6716 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6717 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6718 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6719 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6720 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6721 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6722 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6723 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6724 | rdev_for_each(rdev, conf->mddev) |
| 6725 | rdev->data_offset = rdev->new_data_offset; |
| 6726 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6727 | conf->reshape_progress = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6728 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 6729 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6730 | |
| 6731 | /* read-ahead size must cover two whole stripes, which is |
| 6732 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 6733 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 6734 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6735 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6736 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6737 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6738 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 6739 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 6740 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6741 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6742 | } |
| 6743 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6744 | /* This is called from the raid5d thread with mddev_lock held. |
| 6745 | * It makes config changes to the device. |
| 6746 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6747 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6748 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6749 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6750 | |
| 6751 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 6752 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6753 | if (mddev->delta_disks > 0) { |
| 6754 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 6755 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6756 | revalidate_disk(mddev->gendisk); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6757 | } else { |
| 6758 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6759 | spin_lock_irq(&conf->device_lock); |
| 6760 | mddev->degraded = calc_degraded(conf); |
| 6761 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6762 | for (d = conf->raid_disks ; |
| 6763 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 6764 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6765 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 6766 | if (rdev) |
| 6767 | clear_bit(In_sync, &rdev->flags); |
| 6768 | rdev = conf->disks[d].replacement; |
| 6769 | if (rdev) |
| 6770 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 6771 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6772 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6773 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6774 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6775 | mddev->reshape_position = MaxSector; |
| 6776 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6777 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6778 | } |
| 6779 | } |
| 6780 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6781 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6782 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6783 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6784 | |
| 6785 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 6786 | case 2: /* resume for a suspend */ |
| 6787 | wake_up(&conf->wait_for_overlap); |
| 6788 | break; |
| 6789 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6790 | case 1: /* stop all writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6791 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6792 | /* '2' tells resync/reshape to pause so that all |
| 6793 | * active stripes can drain |
| 6794 | */ |
| 6795 | conf->quiesce = 2; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6796 | wait_event_cmd(conf->wait_for_stripe, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6797 | atomic_read(&conf->active_stripes) == 0 && |
| 6798 | atomic_read(&conf->active_aligned_reads) == 0, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6799 | unlock_all_device_hash_locks_irq(conf), |
| 6800 | lock_all_device_hash_locks_irq(conf)); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6801 | conf->quiesce = 1; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6802 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6803 | /* allow reshape to continue */ |
| 6804 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6805 | break; |
| 6806 | |
| 6807 | case 0: /* re-enable writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6808 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6809 | conf->quiesce = 0; |
| 6810 | wake_up(&conf->wait_for_stripe); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 6811 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6812 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6813 | break; |
| 6814 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6815 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 6816 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6817 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6818 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6819 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6820 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 6821 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6822 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6823 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6824 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6825 | printk(KERN_ERR "md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 6826 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6827 | return ERR_PTR(-EINVAL); |
| 6828 | } |
| 6829 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 6830 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 6831 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 6832 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6833 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 6834 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6835 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 6836 | mddev->raid_disks += 1; |
| 6837 | mddev->delta_disks = 1; |
| 6838 | /* make sure it will be not marked as dirty */ |
| 6839 | mddev->recovery_cp = MaxSector; |
| 6840 | |
| 6841 | return setup_conf(mddev); |
| 6842 | } |
| 6843 | |
| 6844 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6845 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6846 | { |
| 6847 | int chunksect; |
| 6848 | |
| 6849 | if (mddev->raid_disks != 2 || |
| 6850 | mddev->degraded > 1) |
| 6851 | return ERR_PTR(-EINVAL); |
| 6852 | |
| 6853 | /* Should check if there are write-behind devices? */ |
| 6854 | |
| 6855 | chunksect = 64*2; /* 64K by default */ |
| 6856 | |
| 6857 | /* The array must be an exact multiple of chunksize */ |
| 6858 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 6859 | chunksect >>= 1; |
| 6860 | |
| 6861 | if ((chunksect<<9) < STRIPE_SIZE) |
| 6862 | /* array size does not allow a suitable chunk size */ |
| 6863 | return ERR_PTR(-EINVAL); |
| 6864 | |
| 6865 | mddev->new_level = 5; |
| 6866 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6867 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6868 | |
| 6869 | return setup_conf(mddev); |
| 6870 | } |
| 6871 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6872 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6873 | { |
| 6874 | int new_layout; |
| 6875 | |
| 6876 | switch (mddev->layout) { |
| 6877 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 6878 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 6879 | break; |
| 6880 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 6881 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 6882 | break; |
| 6883 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 6884 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 6885 | break; |
| 6886 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 6887 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 6888 | break; |
| 6889 | case ALGORITHM_PARITY_0_6: |
| 6890 | new_layout = ALGORITHM_PARITY_0; |
| 6891 | break; |
| 6892 | case ALGORITHM_PARITY_N: |
| 6893 | new_layout = ALGORITHM_PARITY_N; |
| 6894 | break; |
| 6895 | default: |
| 6896 | return ERR_PTR(-EINVAL); |
| 6897 | } |
| 6898 | mddev->new_level = 5; |
| 6899 | mddev->new_layout = new_layout; |
| 6900 | mddev->delta_disks = -1; |
| 6901 | mddev->raid_disks -= 1; |
| 6902 | return setup_conf(mddev); |
| 6903 | } |
| 6904 | |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6905 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6906 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6907 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6908 | /* For a 2-drive array, the layout and chunk size can be changed |
| 6909 | * immediately as not restriping is needed. |
| 6910 | * For larger arrays we record the new value - after validation |
| 6911 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6912 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6913 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6914 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6915 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6916 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6917 | return -EINVAL; |
| 6918 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6919 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6920 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6921 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6922 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6923 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6924 | /* not factor of array size */ |
| 6925 | return -EINVAL; |
| 6926 | } |
| 6927 | |
| 6928 | /* They look valid */ |
| 6929 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6930 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6931 | /* can make the change immediately */ |
| 6932 | if (mddev->new_layout >= 0) { |
| 6933 | conf->algorithm = mddev->new_layout; |
| 6934 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6935 | } |
| 6936 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6937 | conf->chunk_sectors = new_chunk ; |
| 6938 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6939 | } |
| 6940 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 6941 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6942 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6943 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6944 | } |
| 6945 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6946 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6947 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6948 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6949 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6950 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6951 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6952 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 6953 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6954 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6955 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6956 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 6957 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6958 | /* not factor of array size */ |
| 6959 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6960 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6961 | |
| 6962 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 6963 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 6964 | } |
| 6965 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6966 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6967 | { |
| 6968 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6969 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6970 | * raid1 - if there are two drives. We need to know the chunk size |
| 6971 | * raid4 - trivial - just use a raid4 layout. |
| 6972 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6973 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6974 | if (mddev->level == 0) |
| 6975 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6976 | if (mddev->level == 1) |
| 6977 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 6978 | if (mddev->level == 4) { |
| 6979 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 6980 | mddev->new_level = 5; |
| 6981 | return setup_conf(mddev); |
| 6982 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 6983 | if (mddev->level == 6) |
| 6984 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6985 | |
| 6986 | return ERR_PTR(-EINVAL); |
| 6987 | } |
| 6988 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6989 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6990 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6991 | /* raid4 can take over: |
| 6992 | * raid0 - if there is only one strip zone |
| 6993 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6994 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 6995 | if (mddev->level == 0) |
| 6996 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 6997 | if (mddev->level == 5 && |
| 6998 | mddev->layout == ALGORITHM_PARITY_N) { |
| 6999 | mddev->new_layout = 0; |
| 7000 | mddev->new_level = 4; |
| 7001 | return setup_conf(mddev); |
| 7002 | } |
| 7003 | return ERR_PTR(-EINVAL); |
| 7004 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7005 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7006 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7007 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7008 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7009 | { |
| 7010 | /* Currently can only take over a raid5. We map the |
| 7011 | * personality to an equivalent raid6 personality |
| 7012 | * with the Q block at the end. |
| 7013 | */ |
| 7014 | int new_layout; |
| 7015 | |
| 7016 | if (mddev->pers != &raid5_personality) |
| 7017 | return ERR_PTR(-EINVAL); |
| 7018 | if (mddev->degraded > 1) |
| 7019 | return ERR_PTR(-EINVAL); |
| 7020 | if (mddev->raid_disks > 253) |
| 7021 | return ERR_PTR(-EINVAL); |
| 7022 | if (mddev->raid_disks < 3) |
| 7023 | return ERR_PTR(-EINVAL); |
| 7024 | |
| 7025 | switch (mddev->layout) { |
| 7026 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 7027 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 7028 | break; |
| 7029 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 7030 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 7031 | break; |
| 7032 | case ALGORITHM_LEFT_SYMMETRIC: |
| 7033 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 7034 | break; |
| 7035 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 7036 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 7037 | break; |
| 7038 | case ALGORITHM_PARITY_0: |
| 7039 | new_layout = ALGORITHM_PARITY_0_6; |
| 7040 | break; |
| 7041 | case ALGORITHM_PARITY_N: |
| 7042 | new_layout = ALGORITHM_PARITY_N; |
| 7043 | break; |
| 7044 | default: |
| 7045 | return ERR_PTR(-EINVAL); |
| 7046 | } |
| 7047 | mddev->new_level = 6; |
| 7048 | mddev->new_layout = new_layout; |
| 7049 | mddev->delta_disks = 1; |
| 7050 | mddev->raid_disks += 1; |
| 7051 | return setup_conf(mddev); |
| 7052 | } |
| 7053 | |
| 7054 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7055 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7056 | { |
| 7057 | .name = "raid6", |
| 7058 | .level = 6, |
| 7059 | .owner = THIS_MODULE, |
| 7060 | .make_request = make_request, |
| 7061 | .run = run, |
| 7062 | .stop = stop, |
| 7063 | .status = status, |
| 7064 | .error_handler = error, |
| 7065 | .hot_add_disk = raid5_add_disk, |
| 7066 | .hot_remove_disk= raid5_remove_disk, |
| 7067 | .spare_active = raid5_spare_active, |
| 7068 | .sync_request = sync_request, |
| 7069 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7070 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7071 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7072 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7073 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7074 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7075 | .takeover = raid6_takeover, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7076 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7077 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7078 | { |
| 7079 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7080 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7081 | .owner = THIS_MODULE, |
| 7082 | .make_request = make_request, |
| 7083 | .run = run, |
| 7084 | .stop = stop, |
| 7085 | .status = status, |
| 7086 | .error_handler = error, |
| 7087 | .hot_add_disk = raid5_add_disk, |
| 7088 | .hot_remove_disk= raid5_remove_disk, |
| 7089 | .spare_active = raid5_spare_active, |
| 7090 | .sync_request = sync_request, |
| 7091 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7092 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7093 | .check_reshape = raid5_check_reshape, |
| 7094 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7095 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7096 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7097 | .takeover = raid5_takeover, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7098 | }; |
| 7099 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7100 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7101 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7102 | .name = "raid4", |
| 7103 | .level = 4, |
| 7104 | .owner = THIS_MODULE, |
| 7105 | .make_request = make_request, |
| 7106 | .run = run, |
| 7107 | .stop = stop, |
| 7108 | .status = status, |
| 7109 | .error_handler = error, |
| 7110 | .hot_add_disk = raid5_add_disk, |
| 7111 | .hot_remove_disk= raid5_remove_disk, |
| 7112 | .spare_active = raid5_spare_active, |
| 7113 | .sync_request = sync_request, |
| 7114 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7115 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 7116 | .check_reshape = raid5_check_reshape, |
| 7117 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7118 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7119 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 7120 | .takeover = raid4_takeover, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7121 | }; |
| 7122 | |
| 7123 | static int __init raid5_init(void) |
| 7124 | { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7125 | raid5_wq = alloc_workqueue("raid5wq", |
| 7126 | WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); |
| 7127 | if (!raid5_wq) |
| 7128 | return -ENOMEM; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7129 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7130 | register_md_personality(&raid5_personality); |
| 7131 | register_md_personality(&raid4_personality); |
| 7132 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7133 | } |
| 7134 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7135 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7136 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7137 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7138 | unregister_md_personality(&raid5_personality); |
| 7139 | unregister_md_personality(&raid4_personality); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7140 | destroy_workqueue(raid5_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7141 | } |
| 7142 | |
| 7143 | module_init(raid5_init); |
| 7144 | module_exit(raid5_exit); |
| 7145 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 7146 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 7148 | MODULE_ALIAS("md-raid5"); |
| 7149 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7150 | MODULE_ALIAS("md-level-5"); |
| 7151 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7152 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 7153 | MODULE_ALIAS("md-raid6"); |
| 7154 | MODULE_ALIAS("md-level-6"); |
| 7155 | |
| 7156 | /* This used to be two separate modules, they were: */ |
| 7157 | MODULE_ALIAS("raid5"); |
| 7158 | MODULE_ALIAS("raid6"); |