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> |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 57 | #include <linux/flex_array.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 58 | #include <linux/sched/signal.h> |
| 59 | |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 60 | #include <trace/events/block.h> |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 61 | #include <linux/list_sort.h> |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 62 | |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 63 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 64 | #include "raid5.h" |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 65 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 66 | #include "bitmap.h" |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 67 | #include "raid5-log.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 68 | |
Shaohua Li | 394ed8e | 2017-01-04 16:10:19 -0800 | [diff] [blame] | 69 | #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED) |
| 70 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 71 | #define cpu_to_group(cpu) cpu_to_node(cpu) |
| 72 | #define ANY_GROUP NUMA_NO_NODE |
| 73 | |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 74 | static bool devices_handle_discard_safely = false; |
| 75 | module_param(devices_handle_discard_safely, bool, 0644); |
| 76 | MODULE_PARM_DESC(devices_handle_discard_safely, |
| 77 | "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] | 78 | static struct workqueue_struct *raid5_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 80 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 81 | { |
| 82 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; |
| 83 | return &conf->stripe_hashtbl[hash]; |
| 84 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 86 | static inline int stripe_hash_locks_hash(sector_t sect) |
| 87 | { |
| 88 | return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK; |
| 89 | } |
| 90 | |
| 91 | static inline void lock_device_hash_lock(struct r5conf *conf, int hash) |
| 92 | { |
| 93 | spin_lock_irq(conf->hash_locks + hash); |
| 94 | spin_lock(&conf->device_lock); |
| 95 | } |
| 96 | |
| 97 | static inline void unlock_device_hash_lock(struct r5conf *conf, int hash) |
| 98 | { |
| 99 | spin_unlock(&conf->device_lock); |
| 100 | spin_unlock_irq(conf->hash_locks + hash); |
| 101 | } |
| 102 | |
| 103 | static inline void lock_all_device_hash_locks_irq(struct r5conf *conf) |
| 104 | { |
| 105 | int i; |
| 106 | local_irq_disable(); |
| 107 | spin_lock(conf->hash_locks); |
| 108 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 109 | spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks); |
| 110 | spin_lock(&conf->device_lock); |
| 111 | } |
| 112 | |
| 113 | static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf) |
| 114 | { |
| 115 | int i; |
| 116 | spin_unlock(&conf->device_lock); |
| 117 | for (i = NR_STRIPE_HASH_LOCKS; i; i--) |
| 118 | spin_unlock(conf->hash_locks + i - 1); |
| 119 | local_irq_enable(); |
| 120 | } |
| 121 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 122 | /* Find first data disk in a raid6 stripe */ |
| 123 | static inline int raid6_d0(struct stripe_head *sh) |
| 124 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 125 | if (sh->ddf_layout) |
| 126 | /* ddf always start from first device */ |
| 127 | return 0; |
| 128 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 129 | if (sh->qd_idx == sh->disks - 1) |
| 130 | return 0; |
| 131 | else |
| 132 | return sh->qd_idx + 1; |
| 133 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 134 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 135 | { |
| 136 | disk++; |
| 137 | return (disk < raid_disks) ? disk : 0; |
| 138 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 139 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 140 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 141 | * We need to map each disk to a 'slot', where the data disks are slot |
| 142 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 143 | * is raid_disks-1. This help does that mapping. |
| 144 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 145 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 146 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 147 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 148 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 149 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 150 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 151 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 152 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 153 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 154 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 155 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 156 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 157 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 158 | return slot; |
| 159 | } |
| 160 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 161 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 162 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 163 | static int stripe_operations_active(struct stripe_head *sh) |
| 164 | { |
| 165 | return sh->check_state || sh->reconstruct_state || |
| 166 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 167 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 168 | } |
| 169 | |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 170 | static bool stripe_is_lowprio(struct stripe_head *sh) |
| 171 | { |
| 172 | return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) || |
| 173 | test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) && |
| 174 | !test_bit(STRIPE_R5C_CACHING, &sh->state); |
| 175 | } |
| 176 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 177 | static void raid5_wakeup_stripe_thread(struct stripe_head *sh) |
| 178 | { |
| 179 | struct r5conf *conf = sh->raid_conf; |
| 180 | struct r5worker_group *group; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 181 | int thread_cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 182 | int i, cpu = sh->cpu; |
| 183 | |
| 184 | if (!cpu_online(cpu)) { |
| 185 | cpu = cpumask_any(cpu_online_mask); |
| 186 | sh->cpu = cpu; |
| 187 | } |
| 188 | |
| 189 | if (list_empty(&sh->lru)) { |
| 190 | struct r5worker_group *group; |
| 191 | group = conf->worker_groups + cpu_to_group(cpu); |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 192 | if (stripe_is_lowprio(sh)) |
| 193 | list_add_tail(&sh->lru, &group->loprio_list); |
| 194 | else |
| 195 | list_add_tail(&sh->lru, &group->handle_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 196 | group->stripes_cnt++; |
| 197 | sh->group = group; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | if (conf->worker_cnt_per_group == 0) { |
| 201 | md_wakeup_thread(conf->mddev->thread); |
| 202 | return; |
| 203 | } |
| 204 | |
| 205 | group = conf->worker_groups + cpu_to_group(sh->cpu); |
| 206 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 207 | group->workers[0].working = true; |
| 208 | /* at least one worker should run to avoid race */ |
| 209 | queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work); |
| 210 | |
| 211 | thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1; |
| 212 | /* wakeup more workers */ |
| 213 | for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) { |
| 214 | if (group->workers[i].working == false) { |
| 215 | group->workers[i].working = true; |
| 216 | queue_work_on(sh->cpu, raid5_wq, |
| 217 | &group->workers[i].work); |
| 218 | thread_cnt--; |
| 219 | } |
| 220 | } |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 221 | } |
| 222 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 223 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 224 | struct list_head *temp_inactive_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 226 | int i; |
| 227 | int injournal = 0; /* number of date pages with R5_InJournal */ |
| 228 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 229 | BUG_ON(!list_empty(&sh->lru)); |
| 230 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 231 | |
| 232 | if (r5c_is_writeback(conf->log)) |
| 233 | for (i = sh->disks; i--; ) |
| 234 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 235 | injournal++; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 236 | /* |
| 237 | * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with |
| 238 | * data in journal, so they are not released to cached lists |
| 239 | */ |
| 240 | if (conf->quiesce && r5c_is_writeback(conf->log) && |
| 241 | !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) { |
| 242 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
| 243 | r5c_make_stripe_write_out(sh); |
| 244 | set_bit(STRIPE_HANDLE, &sh->state); |
| 245 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 246 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 247 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 248 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
Jes Sorensen | ad3ab8b | 2015-01-29 12:38:29 -0500 | [diff] [blame] | 249 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 250 | list_add_tail(&sh->lru, &conf->delayed_list); |
Jes Sorensen | ad3ab8b | 2015-01-29 12:38:29 -0500 | [diff] [blame] | 251 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 252 | sh->bm_seq - conf->seq_write > 0) |
| 253 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 254 | else { |
| 255 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 256 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 257 | if (conf->worker_cnt_per_group == 0) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 258 | if (stripe_is_lowprio(sh)) |
| 259 | list_add_tail(&sh->lru, |
| 260 | &conf->loprio_list); |
| 261 | else |
| 262 | list_add_tail(&sh->lru, |
| 263 | &conf->handle_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 264 | } else { |
| 265 | raid5_wakeup_stripe_thread(sh); |
| 266 | return; |
| 267 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 268 | } |
| 269 | md_wakeup_thread(conf->mddev->thread); |
| 270 | } else { |
| 271 | BUG_ON(stripe_operations_active(sh)); |
| 272 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 273 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 274 | < IO_THRESHOLD) |
| 275 | md_wakeup_thread(conf->mddev->thread); |
| 276 | atomic_dec(&conf->active_stripes); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 277 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 278 | if (!r5c_is_writeback(conf->log)) |
| 279 | list_add_tail(&sh->lru, temp_inactive_list); |
| 280 | else { |
| 281 | WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)); |
| 282 | if (injournal == 0) |
| 283 | list_add_tail(&sh->lru, temp_inactive_list); |
| 284 | else if (injournal == conf->raid_disks - conf->max_degraded) { |
| 285 | /* full stripe */ |
| 286 | if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) |
| 287 | atomic_inc(&conf->r5c_cached_full_stripes); |
| 288 | if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) |
| 289 | atomic_dec(&conf->r5c_cached_partial_stripes); |
| 290 | list_add_tail(&sh->lru, &conf->r5c_full_stripe_list); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 291 | r5c_check_cached_full_stripe(conf); |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 292 | } else |
| 293 | /* |
| 294 | * STRIPE_R5C_PARTIAL_STRIPE is set in |
| 295 | * r5c_try_caching_write(). No need to |
| 296 | * set it again. |
| 297 | */ |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 298 | list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 299 | } |
| 300 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 301 | } |
| 302 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 303 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 304 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 305 | struct list_head *temp_inactive_list) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 306 | { |
| 307 | if (atomic_dec_and_test(&sh->count)) |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 308 | do_release_stripe(conf, sh, temp_inactive_list); |
| 309 | } |
| 310 | |
| 311 | /* |
| 312 | * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list |
| 313 | * |
| 314 | * Be careful: Only one task can add/delete stripes from temp_inactive_list at |
| 315 | * given time. Adding stripes only takes device lock, while deleting stripes |
| 316 | * only takes hash lock. |
| 317 | */ |
| 318 | static void release_inactive_stripe_list(struct r5conf *conf, |
| 319 | struct list_head *temp_inactive_list, |
| 320 | int hash) |
| 321 | { |
| 322 | int size; |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 323 | bool do_wakeup = false; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 324 | unsigned long flags; |
| 325 | |
| 326 | if (hash == NR_STRIPE_HASH_LOCKS) { |
| 327 | size = NR_STRIPE_HASH_LOCKS; |
| 328 | hash = NR_STRIPE_HASH_LOCKS - 1; |
| 329 | } else |
| 330 | size = 1; |
| 331 | while (size) { |
| 332 | struct list_head *list = &temp_inactive_list[size - 1]; |
| 333 | |
| 334 | /* |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 335 | * We don't hold any lock here yet, raid5_get_active_stripe() might |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 336 | * remove stripes from the list |
| 337 | */ |
| 338 | if (!list_empty_careful(list)) { |
| 339 | spin_lock_irqsave(conf->hash_locks + hash, flags); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 340 | if (list_empty(conf->inactive_list + hash) && |
| 341 | !list_empty(list)) |
| 342 | atomic_dec(&conf->empty_inactive_list_nr); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 343 | list_splice_tail_init(list, conf->inactive_list + hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 344 | do_wakeup = true; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 345 | spin_unlock_irqrestore(conf->hash_locks + hash, flags); |
| 346 | } |
| 347 | size--; |
| 348 | hash--; |
| 349 | } |
| 350 | |
| 351 | if (do_wakeup) { |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 352 | wake_up(&conf->wait_for_stripe); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 353 | if (atomic_read(&conf->active_stripes) == 0) |
| 354 | wake_up(&conf->wait_for_quiescent); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 355 | if (conf->retry_read_aligned) |
| 356 | md_wakeup_thread(conf->mddev->thread); |
| 357 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 358 | } |
| 359 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 360 | /* should hold conf->device_lock already */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 361 | static int release_stripe_list(struct r5conf *conf, |
| 362 | struct list_head *temp_inactive_list) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 363 | { |
Byungchul Park | eae8263 | 2017-02-14 16:26:24 +0900 | [diff] [blame] | 364 | struct stripe_head *sh, *t; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 365 | int count = 0; |
| 366 | struct llist_node *head; |
| 367 | |
| 368 | head = llist_del_all(&conf->released_stripes); |
Shaohua Li | d265d9d | 2013-08-28 14:29:05 +0800 | [diff] [blame] | 369 | head = llist_reverse_order(head); |
Byungchul Park | eae8263 | 2017-02-14 16:26:24 +0900 | [diff] [blame] | 370 | llist_for_each_entry_safe(sh, t, head, release_list) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 371 | int hash; |
| 372 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 373 | /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */ |
| 374 | smp_mb(); |
| 375 | clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state); |
| 376 | /* |
| 377 | * Don't worry the bit is set here, because if the bit is set |
| 378 | * again, the count is always > 1. This is true for |
| 379 | * STRIPE_ON_UNPLUG_LIST bit too. |
| 380 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 381 | hash = sh->hash_lock_index; |
| 382 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 383 | count++; |
| 384 | } |
| 385 | |
| 386 | return count; |
| 387 | } |
| 388 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 389 | void raid5_release_stripe(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 391 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | unsigned long flags; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 393 | struct list_head list; |
| 394 | int hash; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 395 | bool wakeup; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 396 | |
Eivind Sarto | cf170f3 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 397 | /* Avoid release_list until the last reference. |
| 398 | */ |
| 399 | if (atomic_add_unless(&sh->count, -1, 1)) |
| 400 | return; |
| 401 | |
majianpeng | ad4068d | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 402 | if (unlikely(!conf->mddev->thread) || |
| 403 | test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state)) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 404 | goto slow_path; |
| 405 | wakeup = llist_add(&sh->release_list, &conf->released_stripes); |
| 406 | if (wakeup) |
| 407 | md_wakeup_thread(conf->mddev->thread); |
| 408 | return; |
| 409 | slow_path: |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 410 | local_irq_save(flags); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 411 | /* 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] | 412 | if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 413 | INIT_LIST_HEAD(&list); |
| 414 | hash = sh->hash_lock_index; |
| 415 | do_release_stripe(conf, sh, &list); |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 416 | spin_unlock(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 417 | release_inactive_stripe_list(conf, &list, hash); |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 418 | } |
| 419 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 422 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 424 | pr_debug("remove_hash(), stripe %llu\n", |
| 425 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 426 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 427 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | } |
| 429 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 430 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 432 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 434 | pr_debug("insert_hash(), stripe %llu\n", |
| 435 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 436 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 437 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | } |
| 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 441 | static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | { |
| 443 | struct stripe_head *sh = NULL; |
| 444 | struct list_head *first; |
| 445 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 446 | if (list_empty(conf->inactive_list + hash)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | goto out; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 448 | first = (conf->inactive_list + hash)->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | sh = list_entry(first, struct stripe_head, lru); |
| 450 | list_del_init(first); |
| 451 | remove_hash(sh); |
| 452 | atomic_inc(&conf->active_stripes); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 453 | BUG_ON(hash != sh->hash_lock_index); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 454 | if (list_empty(conf->inactive_list + hash)) |
| 455 | atomic_inc(&conf->empty_inactive_list_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 456 | out: |
| 457 | return sh; |
| 458 | } |
| 459 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 460 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 461 | { |
| 462 | struct page *p; |
| 463 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 464 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 466 | for (i = 0; i < num ; i++) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 467 | WARN_ON(sh->dev[i].page != sh->dev[i].orig_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 468 | p = sh->dev[i].page; |
| 469 | if (!p) |
| 470 | continue; |
| 471 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 472 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 473 | } |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 474 | |
| 475 | if (sh->ppl_page) { |
| 476 | put_page(sh->ppl_page); |
| 477 | sh->ppl_page = NULL; |
| 478 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | } |
| 480 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 481 | static int grow_buffers(struct stripe_head *sh, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 482 | { |
| 483 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 484 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 485 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 486 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | struct page *page; |
| 488 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 489 | if (!(page = alloc_page(gfp))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 490 | return 1; |
| 491 | } |
| 492 | sh->dev[i].page = page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 493 | sh->dev[i].orig_page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 494 | } |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 495 | |
| 496 | if (raid5_has_ppl(sh->raid_conf)) { |
| 497 | sh->ppl_page = alloc_page(gfp); |
| 498 | if (!sh->ppl_page) |
| 499 | return 1; |
| 500 | } |
| 501 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 502 | return 0; |
| 503 | } |
| 504 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 505 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 506 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 507 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 508 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 509 | 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] | 510 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 511 | struct r5conf *conf = sh->raid_conf; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 512 | int i, seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 514 | BUG_ON(atomic_read(&sh->count) != 0); |
| 515 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 516 | BUG_ON(stripe_operations_active(sh)); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 517 | BUG_ON(sh->batch_head); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 518 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 519 | pr_debug("init_stripe called, stripe %llu\n", |
Markus Stockhausen | b8e6a15 | 2014-08-23 20:19:27 +1000 | [diff] [blame] | 520 | (unsigned long long)sector); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 521 | retry: |
| 522 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 523 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 524 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 526 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 527 | sh->state = 0; |
| 528 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 529 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 530 | struct r5dev *dev = &sh->dev[i]; |
| 531 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 532 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | test_bit(R5_LOCKED, &dev->flags)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 534 | pr_err("sector=%llx i=%d %p %p %p %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 535 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 536 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 538 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | } |
| 540 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 541 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 542 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 543 | if (read_seqcount_retry(&conf->gen_lock, seq)) |
| 544 | goto retry; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 545 | sh->overwrite_disks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 546 | insert_hash(conf, sh); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 547 | sh->cpu = smp_processor_id(); |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 548 | set_bit(STRIPE_BATCH_READY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 549 | } |
| 550 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 551 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 552 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 553 | { |
| 554 | struct stripe_head *sh; |
| 555 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 556 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 557 | hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 558 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 560 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 561 | return NULL; |
| 562 | } |
| 563 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 564 | /* |
| 565 | * Need to check if array has failed when deciding whether to: |
| 566 | * - start an array |
| 567 | * - remove non-faulty devices |
| 568 | * - add a spare |
| 569 | * - allow a reshape |
| 570 | * This determination is simple when no reshape is happening. |
| 571 | * However if there is a reshape, we need to carefully check |
| 572 | * both the before and after sections. |
| 573 | * This is because some failed devices may only affect one |
| 574 | * of the two sections, and some non-in_sync devices may |
| 575 | * be insync in the section most affected by failed devices. |
| 576 | */ |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 577 | int raid5_calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 578 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 579 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 580 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 581 | |
| 582 | rcu_read_lock(); |
| 583 | degraded = 0; |
| 584 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 585 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 586 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 587 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 588 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 589 | degraded++; |
| 590 | else if (test_bit(In_sync, &rdev->flags)) |
| 591 | ; |
| 592 | else |
| 593 | /* not in-sync or faulty. |
| 594 | * If the reshape increases the number of devices, |
| 595 | * this is being recovered by the reshape, so |
| 596 | * this 'previous' section is not in_sync. |
| 597 | * If the number of devices is being reduced however, |
| 598 | * the device can only be part of the array if |
| 599 | * we are reverting a reshape, so this section will |
| 600 | * be in-sync. |
| 601 | */ |
| 602 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 603 | degraded++; |
| 604 | } |
| 605 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 606 | if (conf->raid_disks == conf->previous_raid_disks) |
| 607 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 608 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 609 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 610 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 611 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 612 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 613 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 614 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 615 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 616 | else if (test_bit(In_sync, &rdev->flags)) |
| 617 | ; |
| 618 | else |
| 619 | /* not in-sync or faulty. |
| 620 | * If reshape increases the number of devices, this |
| 621 | * section has already been recovered, else it |
| 622 | * almost certainly hasn't. |
| 623 | */ |
| 624 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 625 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 626 | } |
| 627 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 628 | if (degraded2 > degraded) |
| 629 | return degraded2; |
| 630 | return degraded; |
| 631 | } |
| 632 | |
| 633 | static int has_failed(struct r5conf *conf) |
| 634 | { |
| 635 | int degraded; |
| 636 | |
| 637 | if (conf->mddev->reshape_position == MaxSector) |
| 638 | return conf->mddev->degraded > conf->max_degraded; |
| 639 | |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 640 | degraded = raid5_calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 641 | if (degraded > conf->max_degraded) |
| 642 | return 1; |
| 643 | return 0; |
| 644 | } |
| 645 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 646 | struct stripe_head * |
| 647 | raid5_get_active_stripe(struct r5conf *conf, sector_t sector, |
| 648 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 649 | { |
| 650 | struct stripe_head *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 651 | int hash = stripe_hash_locks_hash(sector); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 652 | int inc_empty_inactive_list_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 653 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 654 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 656 | spin_lock_irq(conf->hash_locks + hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 657 | |
| 658 | do { |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 659 | wait_event_lock_irq(conf->wait_for_quiescent, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 660 | conf->quiesce == 0 || noquiesce, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 661 | *(conf->hash_locks + hash)); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 662 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 663 | if (!sh) { |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 664 | if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 665 | sh = get_free_stripe(conf, hash); |
Shaohua Li | 713bc5c | 2015-05-28 17:33:47 -0700 | [diff] [blame] | 666 | if (!sh && !test_bit(R5_DID_ALLOC, |
| 667 | &conf->cache_state)) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 668 | set_bit(R5_ALLOC_MORE, |
| 669 | &conf->cache_state); |
| 670 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 671 | if (noblock && sh == NULL) |
| 672 | break; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 673 | |
| 674 | r5c_check_stripe_cache_usage(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 675 | if (!sh) { |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 676 | set_bit(R5_INACTIVE_BLOCKED, |
| 677 | &conf->cache_state); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 678 | r5l_wake_reclaim(conf->log, 0); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 679 | wait_event_lock_irq( |
| 680 | conf->wait_for_stripe, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 681 | !list_empty(conf->inactive_list + hash) && |
| 682 | (atomic_read(&conf->active_stripes) |
| 683 | < (conf->max_nr_stripes * 3 / 4) |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 684 | || !test_bit(R5_INACTIVE_BLOCKED, |
| 685 | &conf->cache_state)), |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 686 | *(conf->hash_locks + hash)); |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 687 | clear_bit(R5_INACTIVE_BLOCKED, |
| 688 | &conf->cache_state); |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 689 | } else { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 690 | init_stripe(sh, sector, previous); |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 691 | atomic_inc(&sh->count); |
| 692 | } |
Shaohua Li | e240c18 | 2014-04-09 11:27:42 +0800 | [diff] [blame] | 693 | } else if (!atomic_inc_not_zero(&sh->count)) { |
NeilBrown | 6d183de | 2013-11-28 10:55:27 +1100 | [diff] [blame] | 694 | spin_lock(&conf->device_lock); |
Shaohua Li | e240c18 | 2014-04-09 11:27:42 +0800 | [diff] [blame] | 695 | if (!atomic_read(&sh->count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 696 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 697 | atomic_inc(&conf->active_stripes); |
NeilBrown | 5af9bef | 2014-01-14 15:16:10 +1100 | [diff] [blame] | 698 | BUG_ON(list_empty(&sh->lru) && |
| 699 | !test_bit(STRIPE_EXPANDING, &sh->state)); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 700 | inc_empty_inactive_list_flag = 0; |
| 701 | if (!list_empty(conf->inactive_list + hash)) |
| 702 | inc_empty_inactive_list_flag = 1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 703 | list_del_init(&sh->lru); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 704 | if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) |
| 705 | atomic_inc(&conf->empty_inactive_list_nr); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 706 | if (sh->group) { |
| 707 | sh->group->stripes_cnt--; |
| 708 | sh->group = NULL; |
| 709 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 710 | } |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 711 | atomic_inc(&sh->count); |
NeilBrown | 6d183de | 2013-11-28 10:55:27 +1100 | [diff] [blame] | 712 | spin_unlock(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | } |
| 714 | } while (sh == NULL); |
| 715 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 716 | spin_unlock_irq(conf->hash_locks + hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 717 | return sh; |
| 718 | } |
| 719 | |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 720 | static bool is_full_stripe_write(struct stripe_head *sh) |
| 721 | { |
| 722 | BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded)); |
| 723 | return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded); |
| 724 | } |
| 725 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 726 | static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) |
| 727 | { |
| 728 | local_irq_disable(); |
| 729 | if (sh1 > sh2) { |
| 730 | spin_lock(&sh2->stripe_lock); |
| 731 | spin_lock_nested(&sh1->stripe_lock, 1); |
| 732 | } else { |
| 733 | spin_lock(&sh1->stripe_lock); |
| 734 | spin_lock_nested(&sh2->stripe_lock, 1); |
| 735 | } |
| 736 | } |
| 737 | |
| 738 | static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) |
| 739 | { |
| 740 | spin_unlock(&sh1->stripe_lock); |
| 741 | spin_unlock(&sh2->stripe_lock); |
| 742 | local_irq_enable(); |
| 743 | } |
| 744 | |
| 745 | /* Only freshly new full stripe normal write stripe can be added to a batch list */ |
| 746 | static bool stripe_can_batch(struct stripe_head *sh) |
| 747 | { |
Shaohua Li | 9c3e333 | 2015-08-13 14:32:02 -0700 | [diff] [blame] | 748 | struct r5conf *conf = sh->raid_conf; |
| 749 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 750 | if (conf->log || raid5_has_ppl(conf)) |
Shaohua Li | 9c3e333 | 2015-08-13 14:32:02 -0700 | [diff] [blame] | 751 | return false; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 752 | return test_bit(STRIPE_BATCH_READY, &sh->state) && |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 753 | !test_bit(STRIPE_BITMAP_PENDING, &sh->state) && |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 754 | is_full_stripe_write(sh); |
| 755 | } |
| 756 | |
| 757 | /* we only do back search */ |
| 758 | static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh) |
| 759 | { |
| 760 | struct stripe_head *head; |
| 761 | sector_t head_sector, tmp_sec; |
| 762 | int hash; |
| 763 | int dd_idx; |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 764 | int inc_empty_inactive_list_flag; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 765 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 766 | /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */ |
| 767 | tmp_sec = sh->sector; |
| 768 | if (!sector_div(tmp_sec, conf->chunk_sectors)) |
| 769 | return; |
| 770 | head_sector = sh->sector - STRIPE_SECTORS; |
| 771 | |
| 772 | hash = stripe_hash_locks_hash(head_sector); |
| 773 | spin_lock_irq(conf->hash_locks + hash); |
| 774 | head = __find_stripe(conf, head_sector, conf->generation); |
| 775 | if (head && !atomic_inc_not_zero(&head->count)) { |
| 776 | spin_lock(&conf->device_lock); |
| 777 | if (!atomic_read(&head->count)) { |
| 778 | if (!test_bit(STRIPE_HANDLE, &head->state)) |
| 779 | atomic_inc(&conf->active_stripes); |
| 780 | BUG_ON(list_empty(&head->lru) && |
| 781 | !test_bit(STRIPE_EXPANDING, &head->state)); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 782 | inc_empty_inactive_list_flag = 0; |
| 783 | if (!list_empty(conf->inactive_list + hash)) |
| 784 | inc_empty_inactive_list_flag = 1; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 785 | list_del_init(&head->lru); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 786 | if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) |
| 787 | atomic_inc(&conf->empty_inactive_list_nr); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 788 | if (head->group) { |
| 789 | head->group->stripes_cnt--; |
| 790 | head->group = NULL; |
| 791 | } |
| 792 | } |
| 793 | atomic_inc(&head->count); |
| 794 | spin_unlock(&conf->device_lock); |
| 795 | } |
| 796 | spin_unlock_irq(conf->hash_locks + hash); |
| 797 | |
| 798 | if (!head) |
| 799 | return; |
| 800 | if (!stripe_can_batch(head)) |
| 801 | goto out; |
| 802 | |
| 803 | lock_two_stripes(head, sh); |
| 804 | /* clear_batch_ready clear the flag */ |
| 805 | if (!stripe_can_batch(head) || !stripe_can_batch(sh)) |
| 806 | goto unlock_out; |
| 807 | |
| 808 | if (sh->batch_head) |
| 809 | goto unlock_out; |
| 810 | |
| 811 | dd_idx = 0; |
| 812 | while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) |
| 813 | dd_idx++; |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 814 | if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf || |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 815 | bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite)) |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 816 | goto unlock_out; |
| 817 | |
| 818 | if (head->batch_head) { |
| 819 | spin_lock(&head->batch_head->batch_lock); |
| 820 | /* This batch list is already running */ |
| 821 | if (!stripe_can_batch(head)) { |
| 822 | spin_unlock(&head->batch_head->batch_lock); |
| 823 | goto unlock_out; |
| 824 | } |
| 825 | |
| 826 | /* |
| 827 | * at this point, head's BATCH_READY could be cleared, but we |
| 828 | * can still add the stripe to batch list |
| 829 | */ |
| 830 | list_add(&sh->batch_list, &head->batch_list); |
| 831 | spin_unlock(&head->batch_head->batch_lock); |
| 832 | |
| 833 | sh->batch_head = head->batch_head; |
| 834 | } else { |
| 835 | head->batch_head = head; |
| 836 | sh->batch_head = head->batch_head; |
| 837 | spin_lock(&head->batch_lock); |
| 838 | list_add_tail(&sh->batch_list, &head->batch_list); |
| 839 | spin_unlock(&head->batch_lock); |
| 840 | } |
| 841 | |
| 842 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 843 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 844 | < IO_THRESHOLD) |
| 845 | md_wakeup_thread(conf->mddev->thread); |
| 846 | |
NeilBrown | 2b6b245 | 2015-05-21 15:10:01 +1000 | [diff] [blame] | 847 | if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) { |
| 848 | int seq = sh->bm_seq; |
| 849 | if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) && |
| 850 | sh->batch_head->bm_seq > seq) |
| 851 | seq = sh->batch_head->bm_seq; |
| 852 | set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state); |
| 853 | sh->batch_head->bm_seq = seq; |
| 854 | } |
| 855 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 856 | atomic_inc(&sh->count); |
| 857 | unlock_out: |
| 858 | unlock_two_stripes(head, sh); |
| 859 | out: |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 860 | raid5_release_stripe(head); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 861 | } |
| 862 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 863 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 864 | * in this stripe_head. |
| 865 | */ |
| 866 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 867 | { |
| 868 | sector_t progress = conf->reshape_progress; |
| 869 | /* Need a memory barrier to make sure we see the value |
| 870 | * of conf->generation, or ->data_offset that was set before |
| 871 | * reshape_progress was updated. |
| 872 | */ |
| 873 | smp_rmb(); |
| 874 | if (progress == MaxSector) |
| 875 | return 0; |
| 876 | if (sh->generation == conf->generation - 1) |
| 877 | return 0; |
| 878 | /* We are in a reshape, and this is a new-generation stripe, |
| 879 | * so use new_data_offset. |
| 880 | */ |
| 881 | return 1; |
| 882 | } |
| 883 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 884 | static void dispatch_bio_list(struct bio_list *tmp) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 885 | { |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 886 | struct bio *bio; |
| 887 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 888 | while ((bio = bio_list_pop(tmp))) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 889 | generic_make_request(bio); |
| 890 | } |
| 891 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 892 | static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 893 | { |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 894 | const struct r5pending_data *da = list_entry(a, |
| 895 | struct r5pending_data, sibling); |
| 896 | const struct r5pending_data *db = list_entry(b, |
| 897 | struct r5pending_data, sibling); |
| 898 | if (da->sector > db->sector) |
| 899 | return 1; |
| 900 | if (da->sector < db->sector) |
| 901 | return -1; |
| 902 | return 0; |
| 903 | } |
| 904 | |
| 905 | static void dispatch_defer_bios(struct r5conf *conf, int target, |
| 906 | struct bio_list *list) |
| 907 | { |
| 908 | struct r5pending_data *data; |
| 909 | struct list_head *first, *next = NULL; |
| 910 | int cnt = 0; |
| 911 | |
| 912 | if (conf->pending_data_cnt == 0) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 913 | return; |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 914 | |
| 915 | list_sort(NULL, &conf->pending_list, cmp_stripe); |
| 916 | |
| 917 | first = conf->pending_list.next; |
| 918 | |
| 919 | /* temporarily move the head */ |
| 920 | if (conf->next_pending_data) |
| 921 | list_move_tail(&conf->pending_list, |
| 922 | &conf->next_pending_data->sibling); |
| 923 | |
| 924 | while (!list_empty(&conf->pending_list)) { |
| 925 | data = list_first_entry(&conf->pending_list, |
| 926 | struct r5pending_data, sibling); |
| 927 | if (&data->sibling == first) |
| 928 | first = data->sibling.next; |
| 929 | next = data->sibling.next; |
| 930 | |
| 931 | bio_list_merge(list, &data->bios); |
| 932 | list_move(&data->sibling, &conf->free_list); |
| 933 | cnt++; |
| 934 | if (cnt >= target) |
| 935 | break; |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 936 | } |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 937 | conf->pending_data_cnt -= cnt; |
| 938 | BUG_ON(conf->pending_data_cnt < 0 || cnt < target); |
| 939 | |
| 940 | if (next != &conf->pending_list) |
| 941 | conf->next_pending_data = list_entry(next, |
| 942 | struct r5pending_data, sibling); |
| 943 | else |
| 944 | conf->next_pending_data = NULL; |
| 945 | /* list isn't empty */ |
| 946 | if (first != &conf->pending_list) |
| 947 | list_move_tail(&conf->pending_list, first); |
| 948 | } |
| 949 | |
| 950 | static void flush_deferred_bios(struct r5conf *conf) |
| 951 | { |
| 952 | struct bio_list tmp = BIO_EMPTY_LIST; |
| 953 | |
| 954 | if (conf->pending_data_cnt == 0) |
| 955 | return; |
| 956 | |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 957 | spin_lock(&conf->pending_bios_lock); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 958 | dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp); |
| 959 | BUG_ON(conf->pending_data_cnt != 0); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 960 | spin_unlock(&conf->pending_bios_lock); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 961 | |
| 962 | dispatch_bio_list(&tmp); |
| 963 | } |
| 964 | |
| 965 | static void defer_issue_bios(struct r5conf *conf, sector_t sector, |
| 966 | struct bio_list *bios) |
| 967 | { |
| 968 | struct bio_list tmp = BIO_EMPTY_LIST; |
| 969 | struct r5pending_data *ent; |
| 970 | |
| 971 | spin_lock(&conf->pending_bios_lock); |
| 972 | ent = list_first_entry(&conf->free_list, struct r5pending_data, |
| 973 | sibling); |
| 974 | list_move_tail(&ent->sibling, &conf->pending_list); |
| 975 | ent->sector = sector; |
| 976 | bio_list_init(&ent->bios); |
| 977 | bio_list_merge(&ent->bios, bios); |
| 978 | conf->pending_data_cnt++; |
| 979 | if (conf->pending_data_cnt >= PENDING_IO_MAX) |
| 980 | dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp); |
| 981 | |
| 982 | spin_unlock(&conf->pending_bios_lock); |
| 983 | |
| 984 | dispatch_bio_list(&tmp); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 985 | } |
| 986 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 987 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 988 | raid5_end_read_request(struct bio *bi); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 989 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 990 | raid5_end_write_request(struct bio *bi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 991 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 992 | 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] | 993 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 994 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 995 | int i, disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 996 | struct stripe_head *head_sh = sh; |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 997 | struct bio_list pending_bios = BIO_EMPTY_LIST; |
| 998 | bool should_defer; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 999 | |
| 1000 | might_sleep(); |
| 1001 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 1002 | if (log_stripe(sh, s) == 0) |
| 1003 | return; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1004 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1005 | should_defer = conf->batch_bio_dispatch && conf->group_cnt; |
| 1006 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1007 | for (i = disks; i--; ) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1008 | int op, op_flags = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1009 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1010 | struct bio *bi, *rbi; |
| 1011 | struct md_rdev *rdev, *rrdev = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1012 | |
| 1013 | sh = head_sh; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1014 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1015 | op = REQ_OP_WRITE; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1016 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 1017 | op_flags = REQ_FUA; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1018 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1019 | op = REQ_OP_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1020 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1021 | op = REQ_OP_READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1022 | else if (test_and_clear_bit(R5_WantReplace, |
| 1023 | &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1024 | op = REQ_OP_WRITE; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1025 | replace_only = 1; |
| 1026 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1027 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1028 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1029 | op_flags |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1030 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1031 | again: |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1032 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1033 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1034 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1035 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1036 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1037 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 1038 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 1039 | if (!rdev) { |
| 1040 | rdev = rrdev; |
| 1041 | rrdev = NULL; |
| 1042 | } |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1043 | if (op_is_write(op)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1044 | if (replace_only) |
| 1045 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1046 | if (rdev == rrdev) |
| 1047 | /* We raced and saw duplicates */ |
| 1048 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1049 | } else { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1050 | if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1051 | rdev = rrdev; |
| 1052 | rrdev = NULL; |
| 1053 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1054 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1055 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 1056 | rdev = NULL; |
| 1057 | if (rdev) |
| 1058 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1059 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 1060 | rrdev = NULL; |
| 1061 | if (rrdev) |
| 1062 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1063 | rcu_read_unlock(); |
| 1064 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1065 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1066 | * need to check for writes. We never accept write errors |
| 1067 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1068 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1069 | while (op_is_write(op) && rdev && |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1070 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 1071 | sector_t first_bad; |
| 1072 | int bad_sectors; |
| 1073 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 1074 | &first_bad, &bad_sectors); |
| 1075 | if (!bad) |
| 1076 | break; |
| 1077 | |
| 1078 | if (bad < 0) { |
| 1079 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 1080 | if (!conf->mddev->external && |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1081 | conf->mddev->sb_flags) { |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1082 | /* It is very unlikely, but we might |
| 1083 | * still need to write out the |
| 1084 | * bad block log - better give it |
| 1085 | * a chance*/ |
| 1086 | md_check_recovery(conf->mddev); |
| 1087 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 1088 | /* |
| 1089 | * Because md_wait_for_blocked_rdev |
| 1090 | * will dec nr_pending, we must |
| 1091 | * increment it first. |
| 1092 | */ |
| 1093 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1094 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 1095 | } else { |
| 1096 | /* Acknowledged bad block - skip the write */ |
| 1097 | rdev_dec_pending(rdev, conf->mddev); |
| 1098 | rdev = NULL; |
| 1099 | } |
| 1100 | } |
| 1101 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1102 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1103 | if (s->syncing || s->expanding || s->expanded |
| 1104 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1105 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 1106 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 1107 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1108 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1109 | bi->bi_bdev = rdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1110 | bio_set_op_attrs(bi, op, op_flags); |
| 1111 | bi->bi_end_io = op_is_write(op) |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1112 | ? raid5_end_write_request |
| 1113 | : raid5_end_read_request; |
| 1114 | bi->bi_private = sh; |
| 1115 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1116 | pr_debug("%s: for %llu schedule op %d on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1117 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1118 | bi->bi_opf, i); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1119 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1120 | if (sh != head_sh) |
| 1121 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1122 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1123 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1124 | + rdev->new_data_offset); |
| 1125 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1126 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1127 | + rdev->data_offset); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1128 | if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags)) |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1129 | bi->bi_opf |= REQ_NOMERGE; |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1130 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1131 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1132 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 1133 | |
| 1134 | if (!op_is_write(op) && |
| 1135 | test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 1136 | /* |
| 1137 | * issuing read for a page in journal, this |
| 1138 | * must be preparing for prexor in rmw; read |
| 1139 | * the data into orig_page |
| 1140 | */ |
| 1141 | sh->dev[i].vec.bv_page = sh->dev[i].orig_page; |
| 1142 | else |
| 1143 | sh->dev[i].vec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1144 | bi->bi_vcnt = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1145 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 1146 | bi->bi_io_vec[0].bv_offset = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1147 | bi->bi_iter.bi_size = STRIPE_SIZE; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1148 | /* |
| 1149 | * If this is discard request, set bi_vcnt 0. We don't |
| 1150 | * want to confuse SCSI because SCSI will replace payload |
| 1151 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1152 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1153 | bi->bi_vcnt = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1154 | if (rrdev) |
| 1155 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1156 | |
| 1157 | if (conf->mddev->gendisk) |
| 1158 | trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), |
| 1159 | bi, disk_devt(conf->mddev->gendisk), |
| 1160 | sh->dev[i].sector); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1161 | if (should_defer && op_is_write(op)) |
| 1162 | bio_list_add(&pending_bios, bi); |
| 1163 | else |
| 1164 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1165 | } |
| 1166 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1167 | if (s->syncing || s->expanding || s->expanded |
| 1168 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1169 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 1170 | |
| 1171 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1172 | |
| 1173 | rbi->bi_bdev = rrdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1174 | bio_set_op_attrs(rbi, op, op_flags); |
| 1175 | BUG_ON(!op_is_write(op)); |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1176 | rbi->bi_end_io = raid5_end_write_request; |
| 1177 | rbi->bi_private = sh; |
| 1178 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1179 | pr_debug("%s: for %llu schedule op %d on " |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1180 | "replacement disc %d\n", |
| 1181 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1182 | rbi->bi_opf, i); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1183 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1184 | if (sh != head_sh) |
| 1185 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1186 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1187 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1188 | + rrdev->new_data_offset); |
| 1189 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1190 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1191 | + rrdev->data_offset); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1192 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1193 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 1194 | sh->dev[i].rvec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1195 | rbi->bi_vcnt = 1; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1196 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 1197 | rbi->bi_io_vec[0].bv_offset = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1198 | rbi->bi_iter.bi_size = STRIPE_SIZE; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1199 | /* |
| 1200 | * If this is discard request, set bi_vcnt 0. We don't |
| 1201 | * want to confuse SCSI because SCSI will replace payload |
| 1202 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1203 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1204 | rbi->bi_vcnt = 0; |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1205 | if (conf->mddev->gendisk) |
| 1206 | trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), |
| 1207 | rbi, disk_devt(conf->mddev->gendisk), |
| 1208 | sh->dev[i].sector); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1209 | if (should_defer && op_is_write(op)) |
| 1210 | bio_list_add(&pending_bios, rbi); |
| 1211 | else |
| 1212 | generic_make_request(rbi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1213 | } |
| 1214 | if (!rdev && !rrdev) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1215 | if (op_is_write(op)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1216 | set_bit(STRIPE_DEGRADED, &sh->state); |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1217 | pr_debug("skip op %d on disc %d for sector %llu\n", |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1218 | bi->bi_opf, i, (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1219 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1220 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1221 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1222 | |
| 1223 | if (!head_sh->batch_head) |
| 1224 | continue; |
| 1225 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1226 | batch_list); |
| 1227 | if (sh != head_sh) |
| 1228 | goto again; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1229 | } |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1230 | |
| 1231 | if (should_defer && !bio_list_empty(&pending_bios)) |
| 1232 | defer_issue_bios(conf, head_sh->sector, &pending_bios); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | static struct dma_async_tx_descriptor * |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1236 | async_copy_data(int frombio, struct bio *bio, struct page **page, |
| 1237 | sector_t sector, struct dma_async_tx_descriptor *tx, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1238 | struct stripe_head *sh, int no_skipcopy) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1239 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1240 | struct bio_vec bvl; |
| 1241 | struct bvec_iter iter; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1242 | struct page *bio_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1243 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1244 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1245 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1246 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1247 | if (bio->bi_iter.bi_sector >= sector) |
| 1248 | page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1249 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1250 | page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1251 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1252 | if (frombio) |
| 1253 | flags |= ASYNC_TX_FENCE; |
| 1254 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 1255 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1256 | bio_for_each_segment(bvl, bio, iter) { |
| 1257 | int len = bvl.bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1258 | int clen; |
| 1259 | int b_offset = 0; |
| 1260 | |
| 1261 | if (page_offset < 0) { |
| 1262 | b_offset = -page_offset; |
| 1263 | page_offset += b_offset; |
| 1264 | len -= b_offset; |
| 1265 | } |
| 1266 | |
| 1267 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 1268 | clen = STRIPE_SIZE - page_offset; |
| 1269 | else |
| 1270 | clen = len; |
| 1271 | |
| 1272 | if (clen > 0) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1273 | b_offset += bvl.bv_offset; |
| 1274 | bio_page = bvl.bv_page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1275 | if (frombio) { |
| 1276 | if (sh->raid_conf->skip_copy && |
| 1277 | b_offset == 0 && page_offset == 0 && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1278 | clen == STRIPE_SIZE && |
| 1279 | !no_skipcopy) |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1280 | *page = bio_page; |
| 1281 | else |
| 1282 | tx = async_memcpy(*page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1283 | b_offset, clen, &submit); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1284 | } else |
| 1285 | tx = async_memcpy(bio_page, *page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1286 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1287 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1288 | /* chain the operations */ |
| 1289 | submit.depend_tx = tx; |
| 1290 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1291 | if (clen < len) /* hit end of page */ |
| 1292 | break; |
| 1293 | page_offset += len; |
| 1294 | } |
| 1295 | |
| 1296 | return tx; |
| 1297 | } |
| 1298 | |
| 1299 | static void ops_complete_biofill(void *stripe_head_ref) |
| 1300 | { |
| 1301 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1302 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1303 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1304 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1305 | (unsigned long long)sh->sector); |
| 1306 | |
| 1307 | /* clear completed biofills */ |
| 1308 | for (i = sh->disks; i--; ) { |
| 1309 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1310 | |
| 1311 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1312 | /* and check if we need to reply to a read request, |
| 1313 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1314 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1315 | */ |
| 1316 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1317 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1318 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1319 | BUG_ON(!dev->read); |
| 1320 | rbi = dev->read; |
| 1321 | dev->read = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1322 | while (rbi && rbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1323 | dev->sector + STRIPE_SECTORS) { |
| 1324 | rbi2 = r5_next_bio(rbi, dev->sector); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 1325 | if (!raid5_dec_bi_active_stripes(rbi)) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 1326 | bio_endio(rbi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1327 | rbi = rbi2; |
| 1328 | } |
| 1329 | } |
| 1330 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1331 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1332 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1333 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1334 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1335 | } |
| 1336 | |
| 1337 | static void ops_run_biofill(struct stripe_head *sh) |
| 1338 | { |
| 1339 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1340 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1341 | int i; |
| 1342 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1343 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1344 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1345 | (unsigned long long)sh->sector); |
| 1346 | |
| 1347 | for (i = sh->disks; i--; ) { |
| 1348 | struct r5dev *dev = &sh->dev[i]; |
| 1349 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 1350 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1351 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1352 | dev->read = rbi = dev->toread; |
| 1353 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1354 | spin_unlock_irq(&sh->stripe_lock); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1355 | while (rbi && rbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1356 | dev->sector + STRIPE_SECTORS) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1357 | tx = async_copy_data(0, rbi, &dev->page, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1358 | dev->sector, tx, sh, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1359 | rbi = r5_next_bio(rbi, dev->sector); |
| 1360 | } |
| 1361 | } |
| 1362 | } |
| 1363 | |
| 1364 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1365 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 1366 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1367 | } |
| 1368 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1369 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 1370 | { |
| 1371 | struct r5dev *tgt; |
| 1372 | |
| 1373 | if (target < 0) |
| 1374 | return; |
| 1375 | |
| 1376 | tgt = &sh->dev[target]; |
| 1377 | set_bit(R5_UPTODATE, &tgt->flags); |
| 1378 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1379 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 1380 | } |
| 1381 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1382 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1383 | { |
| 1384 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1385 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1386 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1387 | (unsigned long long)sh->sector); |
| 1388 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1389 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1390 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1391 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1392 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1393 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 1394 | if (sh->check_state == check_state_compute_run) |
| 1395 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1396 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1397 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1398 | } |
| 1399 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1400 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 1401 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1402 | struct raid5_percpu *percpu, int i) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1403 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1404 | void *addr; |
| 1405 | |
| 1406 | addr = flex_array_get(percpu->scribble, i); |
| 1407 | return addr + sizeof(struct page *) * (sh->disks + 2); |
| 1408 | } |
| 1409 | |
| 1410 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 1411 | static struct page **to_addr_page(struct raid5_percpu *percpu, int i) |
| 1412 | { |
| 1413 | void *addr; |
| 1414 | |
| 1415 | addr = flex_array_get(percpu->scribble, i); |
| 1416 | return addr; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1417 | } |
| 1418 | |
| 1419 | static struct dma_async_tx_descriptor * |
| 1420 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1421 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1422 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1423 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1424 | int target = sh->ops.target; |
| 1425 | struct r5dev *tgt = &sh->dev[target]; |
| 1426 | struct page *xor_dest = tgt->page; |
| 1427 | int count = 0; |
| 1428 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1429 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1430 | int i; |
| 1431 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1432 | BUG_ON(sh->batch_head); |
| 1433 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1434 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1435 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1436 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1437 | |
| 1438 | for (i = disks; i--; ) |
| 1439 | if (i != target) |
| 1440 | xor_srcs[count++] = sh->dev[i].page; |
| 1441 | |
| 1442 | atomic_inc(&sh->count); |
| 1443 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1444 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1445 | ops_complete_compute, sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1446 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1447 | 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] | 1448 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1449 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1450 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1451 | return tx; |
| 1452 | } |
| 1453 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1454 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 1455 | * @srcs - (struct page *) array of size sh->disks |
| 1456 | * @sh - stripe_head to parse |
| 1457 | * |
| 1458 | * Populates srcs in proper layout order for the stripe and returns the |
| 1459 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 1460 | * destination buffer is recorded in srcs[count] and the Q destination |
| 1461 | * is recorded in srcs[count+1]]. |
| 1462 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1463 | static int set_syndrome_sources(struct page **srcs, |
| 1464 | struct stripe_head *sh, |
| 1465 | int srctype) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1466 | { |
| 1467 | int disks = sh->disks; |
| 1468 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 1469 | int d0_idx = raid6_d0(sh); |
| 1470 | int count; |
| 1471 | int i; |
| 1472 | |
| 1473 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1474 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1475 | |
| 1476 | count = 0; |
| 1477 | i = d0_idx; |
| 1478 | do { |
| 1479 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1480 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1481 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1482 | if (i == sh->qd_idx || i == sh->pd_idx || |
| 1483 | (srctype == SYNDROME_SRC_ALL) || |
| 1484 | (srctype == SYNDROME_SRC_WANT_DRAIN && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1485 | (test_bit(R5_Wantdrain, &dev->flags) || |
| 1486 | test_bit(R5_InJournal, &dev->flags))) || |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1487 | (srctype == SYNDROME_SRC_WRITTEN && |
Song Liu | 0977762 | 2017-03-13 13:44:35 -0700 | [diff] [blame] | 1488 | (dev->written || |
| 1489 | test_bit(R5_InJournal, &dev->flags)))) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1490 | if (test_bit(R5_InJournal, &dev->flags)) |
| 1491 | srcs[slot] = sh->dev[i].orig_page; |
| 1492 | else |
| 1493 | srcs[slot] = sh->dev[i].page; |
| 1494 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1495 | i = raid6_next_disk(i, disks); |
| 1496 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1497 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1498 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | static struct dma_async_tx_descriptor * |
| 1502 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1503 | { |
| 1504 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1505 | struct page **blocks = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1506 | int target; |
| 1507 | int qd_idx = sh->qd_idx; |
| 1508 | struct dma_async_tx_descriptor *tx; |
| 1509 | struct async_submit_ctl submit; |
| 1510 | struct r5dev *tgt; |
| 1511 | struct page *dest; |
| 1512 | int i; |
| 1513 | int count; |
| 1514 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1515 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1516 | if (sh->ops.target < 0) |
| 1517 | target = sh->ops.target2; |
| 1518 | else if (sh->ops.target2 < 0) |
| 1519 | target = sh->ops.target; |
| 1520 | else |
| 1521 | /* we should only have one valid target */ |
| 1522 | BUG(); |
| 1523 | BUG_ON(target < 0); |
| 1524 | pr_debug("%s: stripe %llu block: %d\n", |
| 1525 | __func__, (unsigned long long)sh->sector, target); |
| 1526 | |
| 1527 | tgt = &sh->dev[target]; |
| 1528 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1529 | dest = tgt->page; |
| 1530 | |
| 1531 | atomic_inc(&sh->count); |
| 1532 | |
| 1533 | if (target == qd_idx) { |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1534 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1535 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 1536 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1537 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1538 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1539 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1540 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 1541 | } else { |
| 1542 | /* Compute any data- or p-drive using XOR */ |
| 1543 | count = 0; |
| 1544 | for (i = disks; i-- ; ) { |
| 1545 | if (i == target || i == qd_idx) |
| 1546 | continue; |
| 1547 | blocks[count++] = sh->dev[i].page; |
| 1548 | } |
| 1549 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1550 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1551 | NULL, ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1552 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1553 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 1554 | } |
| 1555 | |
| 1556 | return tx; |
| 1557 | } |
| 1558 | |
| 1559 | static struct dma_async_tx_descriptor * |
| 1560 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1561 | { |
| 1562 | int i, count, disks = sh->disks; |
| 1563 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1564 | int d0_idx = raid6_d0(sh); |
| 1565 | int faila = -1, failb = -1; |
| 1566 | int target = sh->ops.target; |
| 1567 | int target2 = sh->ops.target2; |
| 1568 | struct r5dev *tgt = &sh->dev[target]; |
| 1569 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1570 | struct dma_async_tx_descriptor *tx; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1571 | struct page **blocks = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1572 | struct async_submit_ctl submit; |
| 1573 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1574 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1575 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1576 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1577 | BUG_ON(target < 0 || target2 < 0); |
| 1578 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1579 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1580 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1581 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1582 | * slot number conversion for 'faila' and 'failb' |
| 1583 | */ |
| 1584 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1585 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1586 | count = 0; |
| 1587 | i = d0_idx; |
| 1588 | do { |
| 1589 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1590 | |
| 1591 | blocks[slot] = sh->dev[i].page; |
| 1592 | |
| 1593 | if (i == target) |
| 1594 | faila = slot; |
| 1595 | if (i == target2) |
| 1596 | failb = slot; |
| 1597 | i = raid6_next_disk(i, disks); |
| 1598 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1599 | |
| 1600 | BUG_ON(faila == failb); |
| 1601 | if (failb < faila) |
| 1602 | swap(faila, failb); |
| 1603 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1604 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1605 | |
| 1606 | atomic_inc(&sh->count); |
| 1607 | |
| 1608 | if (failb == syndrome_disks+1) { |
| 1609 | /* Q disk is one of the missing disks */ |
| 1610 | if (faila == syndrome_disks) { |
| 1611 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1612 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1613 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1614 | to_addr_conv(sh, percpu, 0)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1615 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1616 | STRIPE_SIZE, &submit); |
| 1617 | } else { |
| 1618 | struct page *dest; |
| 1619 | int data_target; |
| 1620 | int qd_idx = sh->qd_idx; |
| 1621 | |
| 1622 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1623 | if (target == qd_idx) |
| 1624 | data_target = target2; |
| 1625 | else |
| 1626 | data_target = target; |
| 1627 | |
| 1628 | count = 0; |
| 1629 | for (i = disks; i-- ; ) { |
| 1630 | if (i == data_target || i == qd_idx) |
| 1631 | continue; |
| 1632 | blocks[count++] = sh->dev[i].page; |
| 1633 | } |
| 1634 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1635 | init_async_submit(&submit, |
| 1636 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1637 | NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1638 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1639 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1640 | &submit); |
| 1641 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1642 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1643 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1644 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1645 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1646 | return async_gen_syndrome(blocks, 0, count+2, |
| 1647 | STRIPE_SIZE, &submit); |
| 1648 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1649 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1650 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1651 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1652 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1653 | if (failb == syndrome_disks) { |
| 1654 | /* We're missing D+P. */ |
| 1655 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1656 | STRIPE_SIZE, faila, |
| 1657 | blocks, &submit); |
| 1658 | } else { |
| 1659 | /* We're missing D+D. */ |
| 1660 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1661 | STRIPE_SIZE, faila, failb, |
| 1662 | blocks, &submit); |
| 1663 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1664 | } |
| 1665 | } |
| 1666 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1667 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1668 | { |
| 1669 | struct stripe_head *sh = stripe_head_ref; |
| 1670 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1671 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1672 | (unsigned long long)sh->sector); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1673 | |
| 1674 | if (r5c_is_writeback(sh->raid_conf->log)) |
| 1675 | /* |
| 1676 | * raid5-cache write back uses orig_page during prexor. |
| 1677 | * After prexor, it is time to free orig_page |
| 1678 | */ |
| 1679 | r5c_release_extra_page(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1680 | } |
| 1681 | |
| 1682 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1683 | ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1684 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1685 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1686 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1687 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1688 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1689 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1690 | |
| 1691 | /* existing parity data subtracted */ |
| 1692 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1693 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1694 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1695 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1696 | (unsigned long long)sh->sector); |
| 1697 | |
| 1698 | for (i = disks; i--; ) { |
| 1699 | struct r5dev *dev = &sh->dev[i]; |
| 1700 | /* Only process blocks that are known to be uptodate */ |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1701 | if (test_bit(R5_InJournal, &dev->flags)) |
| 1702 | xor_srcs[count++] = dev->orig_page; |
| 1703 | else if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1704 | xor_srcs[count++] = dev->page; |
| 1705 | } |
| 1706 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1707 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1708 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1709 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1710 | |
| 1711 | return tx; |
| 1712 | } |
| 1713 | |
| 1714 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1715 | ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1716 | struct dma_async_tx_descriptor *tx) |
| 1717 | { |
| 1718 | struct page **blocks = to_addr_page(percpu, 0); |
| 1719 | int count; |
| 1720 | struct async_submit_ctl submit; |
| 1721 | |
| 1722 | pr_debug("%s: stripe %llu\n", __func__, |
| 1723 | (unsigned long long)sh->sector); |
| 1724 | |
| 1725 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN); |
| 1726 | |
| 1727 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx, |
| 1728 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
| 1729 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 1730 | |
| 1731 | return tx; |
| 1732 | } |
| 1733 | |
| 1734 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1735 | 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] | 1736 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1737 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1738 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1739 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1740 | struct stripe_head *head_sh = sh; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1741 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1742 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1743 | (unsigned long long)sh->sector); |
| 1744 | |
| 1745 | for (i = disks; i--; ) { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1746 | struct r5dev *dev; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1747 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1748 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1749 | sh = head_sh; |
| 1750 | if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1751 | struct bio *wbi; |
| 1752 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1753 | again: |
| 1754 | dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1755 | /* |
| 1756 | * clear R5_InJournal, so when rewriting a page in |
| 1757 | * journal, it is not skipped by r5l_log_stripe() |
| 1758 | */ |
| 1759 | clear_bit(R5_InJournal, &dev->flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1760 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1761 | chosen = dev->towrite; |
| 1762 | dev->towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1763 | sh->overwrite_disks = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1764 | BUG_ON(dev->written); |
| 1765 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1766 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1767 | WARN_ON(dev->page != dev->orig_page); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1768 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1769 | while (wbi && wbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1770 | dev->sector + STRIPE_SECTORS) { |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1771 | if (wbi->bi_opf & REQ_FUA) |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1772 | set_bit(R5_WantFUA, &dev->flags); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1773 | if (wbi->bi_opf & REQ_SYNC) |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1774 | set_bit(R5_SyncIO, &dev->flags); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1775 | if (bio_op(wbi) == REQ_OP_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1776 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1777 | else { |
| 1778 | tx = async_copy_data(1, wbi, &dev->page, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1779 | dev->sector, tx, sh, |
| 1780 | r5c_is_writeback(conf->log)); |
| 1781 | if (dev->page != dev->orig_page && |
| 1782 | !r5c_is_writeback(conf->log)) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1783 | set_bit(R5_SkipCopy, &dev->flags); |
| 1784 | clear_bit(R5_UPTODATE, &dev->flags); |
| 1785 | clear_bit(R5_OVERWRITE, &dev->flags); |
| 1786 | } |
| 1787 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1788 | wbi = r5_next_bio(wbi, dev->sector); |
| 1789 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1790 | |
| 1791 | if (head_sh->batch_head) { |
| 1792 | sh = list_first_entry(&sh->batch_list, |
| 1793 | struct stripe_head, |
| 1794 | batch_list); |
| 1795 | if (sh == head_sh) |
| 1796 | continue; |
| 1797 | goto again; |
| 1798 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1799 | } |
| 1800 | } |
| 1801 | |
| 1802 | return tx; |
| 1803 | } |
| 1804 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1805 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1806 | { |
| 1807 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1808 | int disks = sh->disks; |
| 1809 | int pd_idx = sh->pd_idx; |
| 1810 | int qd_idx = sh->qd_idx; |
| 1811 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1812 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1813 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1814 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1815 | (unsigned long long)sh->sector); |
| 1816 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1817 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1818 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1819 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1820 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1821 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1822 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1823 | for (i = disks; i--; ) { |
| 1824 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1825 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1826 | if (dev->written || i == pd_idx || i == qd_idx) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1827 | if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1828 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1829 | if (fua) |
| 1830 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1831 | if (sync) |
| 1832 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1833 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1836 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1837 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1838 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1839 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1840 | else { |
| 1841 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1842 | sh->reconstruct_state = reconstruct_state_result; |
| 1843 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1844 | |
| 1845 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1846 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1850 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1851 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1852 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1853 | int disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1854 | struct page **xor_srcs; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1855 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1856 | int count, pd_idx = sh->pd_idx, i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1857 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1858 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1859 | unsigned long flags; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1860 | int j = 0; |
| 1861 | struct stripe_head *head_sh = sh; |
| 1862 | int last_stripe; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1863 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1864 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1865 | (unsigned long long)sh->sector); |
| 1866 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1867 | for (i = 0; i < sh->disks; i++) { |
| 1868 | if (pd_idx == i) |
| 1869 | continue; |
| 1870 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1871 | break; |
| 1872 | } |
| 1873 | if (i >= sh->disks) { |
| 1874 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1875 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 1876 | ops_complete_reconstruct(sh); |
| 1877 | return; |
| 1878 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1879 | again: |
| 1880 | count = 0; |
| 1881 | xor_srcs = to_addr_page(percpu, j); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1882 | /* check if prexor is active which means only process blocks |
| 1883 | * that are part of a read-modify-write (written) |
| 1884 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1885 | if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1886 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1887 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1888 | for (i = disks; i--; ) { |
| 1889 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1890 | if (head_sh->dev[i].written || |
| 1891 | test_bit(R5_InJournal, &head_sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1892 | xor_srcs[count++] = dev->page; |
| 1893 | } |
| 1894 | } else { |
| 1895 | xor_dest = sh->dev[pd_idx].page; |
| 1896 | for (i = disks; i--; ) { |
| 1897 | struct r5dev *dev = &sh->dev[i]; |
| 1898 | if (i != pd_idx) |
| 1899 | xor_srcs[count++] = dev->page; |
| 1900 | } |
| 1901 | } |
| 1902 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1903 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1904 | * 2/ if we did not prexor then we are redoing the parity |
| 1905 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1906 | * for the synchronous xor case |
| 1907 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1908 | last_stripe = !head_sh->batch_head || |
| 1909 | list_first_entry(&sh->batch_list, |
| 1910 | struct stripe_head, batch_list) == head_sh; |
| 1911 | if (last_stripe) { |
| 1912 | flags = ASYNC_TX_ACK | |
| 1913 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1914 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1915 | atomic_inc(&head_sh->count); |
| 1916 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh, |
| 1917 | to_addr_conv(sh, percpu, j)); |
| 1918 | } else { |
| 1919 | flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST; |
| 1920 | init_async_submit(&submit, flags, tx, NULL, NULL, |
| 1921 | to_addr_conv(sh, percpu, j)); |
| 1922 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1923 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1924 | if (unlikely(count == 1)) |
| 1925 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1926 | else |
| 1927 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1928 | if (!last_stripe) { |
| 1929 | j++; |
| 1930 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1931 | batch_list); |
| 1932 | goto again; |
| 1933 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1934 | } |
| 1935 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1936 | static void |
| 1937 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1938 | struct dma_async_tx_descriptor *tx) |
| 1939 | { |
| 1940 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1941 | struct page **blocks; |
| 1942 | int count, i, j = 0; |
| 1943 | struct stripe_head *head_sh = sh; |
| 1944 | int last_stripe; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1945 | int synflags; |
| 1946 | unsigned long txflags; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1947 | |
| 1948 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1949 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1950 | for (i = 0; i < sh->disks; i++) { |
| 1951 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 1952 | continue; |
| 1953 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1954 | break; |
| 1955 | } |
| 1956 | if (i >= sh->disks) { |
| 1957 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1958 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 1959 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 1960 | ops_complete_reconstruct(sh); |
| 1961 | return; |
| 1962 | } |
| 1963 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1964 | again: |
| 1965 | blocks = to_addr_page(percpu, j); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1966 | |
| 1967 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1968 | synflags = SYNDROME_SRC_WRITTEN; |
| 1969 | txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST; |
| 1970 | } else { |
| 1971 | synflags = SYNDROME_SRC_ALL; |
| 1972 | txflags = ASYNC_TX_ACK; |
| 1973 | } |
| 1974 | |
| 1975 | count = set_syndrome_sources(blocks, sh, synflags); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1976 | last_stripe = !head_sh->batch_head || |
| 1977 | list_first_entry(&sh->batch_list, |
| 1978 | struct stripe_head, batch_list) == head_sh; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1979 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1980 | if (last_stripe) { |
| 1981 | atomic_inc(&head_sh->count); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1982 | init_async_submit(&submit, txflags, tx, ops_complete_reconstruct, |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1983 | head_sh, to_addr_conv(sh, percpu, j)); |
| 1984 | } else |
| 1985 | init_async_submit(&submit, 0, tx, NULL, NULL, |
| 1986 | to_addr_conv(sh, percpu, j)); |
Shaohua Li | 4876969 | 2015-05-13 09:30:08 -0700 | [diff] [blame] | 1987 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1988 | if (!last_stripe) { |
| 1989 | j++; |
| 1990 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1991 | batch_list); |
| 1992 | goto again; |
| 1993 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1994 | } |
| 1995 | |
| 1996 | static void ops_complete_check(void *stripe_head_ref) |
| 1997 | { |
| 1998 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1999 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2000 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2001 | (unsigned long long)sh->sector); |
| 2002 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2003 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2004 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2005 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2006 | } |
| 2007 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2008 | 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] | 2009 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2010 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2011 | int pd_idx = sh->pd_idx; |
| 2012 | int qd_idx = sh->qd_idx; |
| 2013 | struct page *xor_dest; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2014 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2015 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2016 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2017 | int count; |
| 2018 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2019 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2020 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2021 | (unsigned long long)sh->sector); |
| 2022 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2023 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2024 | count = 0; |
| 2025 | xor_dest = sh->dev[pd_idx].page; |
| 2026 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2027 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2028 | if (i == pd_idx || i == qd_idx) |
| 2029 | continue; |
| 2030 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2031 | } |
| 2032 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2033 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2034 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 2035 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2036 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2037 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2038 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2039 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 2040 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2041 | } |
| 2042 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2043 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 2044 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2045 | struct page **srcs = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2046 | struct async_submit_ctl submit; |
| 2047 | int count; |
| 2048 | |
| 2049 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 2050 | (unsigned long long)sh->sector, checkp); |
| 2051 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2052 | BUG_ON(sh->batch_head); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2053 | count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2054 | if (!checkp) |
| 2055 | srcs[count] = NULL; |
| 2056 | |
| 2057 | atomic_inc(&sh->count); |
| 2058 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2059 | sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2060 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 2061 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 2062 | } |
| 2063 | |
NeilBrown | 51acbce | 2013-02-28 09:08:34 +1100 | [diff] [blame] | 2064 | 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] | 2065 | { |
| 2066 | int overlap_clear = 0, i, disks = sh->disks; |
| 2067 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2068 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2069 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2070 | struct raid5_percpu *percpu; |
| 2071 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2072 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2073 | cpu = get_cpu(); |
| 2074 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2075 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2076 | ops_run_biofill(sh); |
| 2077 | overlap_clear++; |
| 2078 | } |
| 2079 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 2080 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2081 | if (level < 6) |
| 2082 | tx = ops_run_compute5(sh, percpu); |
| 2083 | else { |
| 2084 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 2085 | tx = ops_run_compute6_1(sh, percpu); |
| 2086 | else |
| 2087 | tx = ops_run_compute6_2(sh, percpu); |
| 2088 | } |
| 2089 | /* terminate the chain if reconstruct is not set to be run */ |
| 2090 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 2091 | async_tx_ack(tx); |
| 2092 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2093 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 2094 | if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request)) |
| 2095 | tx = ops_run_partial_parity(sh, percpu, tx); |
| 2096 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2097 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) { |
| 2098 | if (level < 6) |
| 2099 | tx = ops_run_prexor5(sh, percpu, tx); |
| 2100 | else |
| 2101 | tx = ops_run_prexor6(sh, percpu, tx); |
| 2102 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2103 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2104 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2105 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2106 | overlap_clear++; |
| 2107 | } |
| 2108 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2109 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 2110 | if (level < 6) |
| 2111 | ops_run_reconstruct5(sh, percpu, tx); |
| 2112 | else |
| 2113 | ops_run_reconstruct6(sh, percpu, tx); |
| 2114 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2115 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2116 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 2117 | if (sh->check_state == check_state_run) |
| 2118 | ops_run_check_p(sh, percpu); |
| 2119 | else if (sh->check_state == check_state_run_q) |
| 2120 | ops_run_check_pq(sh, percpu, 0); |
| 2121 | else if (sh->check_state == check_state_run_pq) |
| 2122 | ops_run_check_pq(sh, percpu, 1); |
| 2123 | else |
| 2124 | BUG(); |
| 2125 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2126 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2127 | if (overlap_clear && !sh->batch_head) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2128 | for (i = disks; i--; ) { |
| 2129 | struct r5dev *dev = &sh->dev[i]; |
| 2130 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 2131 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 2132 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2133 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2134 | } |
| 2135 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2136 | static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp, |
| 2137 | int disks) |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2138 | { |
| 2139 | struct stripe_head *sh; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2140 | int i; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2141 | |
| 2142 | sh = kmem_cache_zalloc(sc, gfp); |
| 2143 | if (sh) { |
| 2144 | spin_lock_init(&sh->stripe_lock); |
| 2145 | spin_lock_init(&sh->batch_lock); |
| 2146 | INIT_LIST_HEAD(&sh->batch_list); |
| 2147 | INIT_LIST_HEAD(&sh->lru); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2148 | INIT_LIST_HEAD(&sh->r5c); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2149 | INIT_LIST_HEAD(&sh->log_list); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2150 | atomic_set(&sh->count, 1); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2151 | sh->log_start = MaxSector; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2152 | for (i = 0; i < disks; i++) { |
| 2153 | struct r5dev *dev = &sh->dev[i]; |
| 2154 | |
Ming Lei | 3a83f46 | 2016-11-22 08:57:21 -0700 | [diff] [blame] | 2155 | bio_init(&dev->req, &dev->vec, 1); |
| 2156 | bio_init(&dev->rreq, &dev->rvec, 1); |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2157 | } |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2158 | } |
| 2159 | return sh; |
| 2160 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2161 | static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2162 | { |
| 2163 | struct stripe_head *sh; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2164 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2165 | sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2166 | if (!sh) |
| 2167 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 2168 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2169 | sh->raid_conf = conf; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2170 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 2171 | if (grow_buffers(sh, gfp)) { |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2172 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2173 | kmem_cache_free(conf->slab_cache, sh); |
| 2174 | return 0; |
| 2175 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2176 | sh->hash_lock_index = |
| 2177 | conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2178 | /* we just created an active stripe so... */ |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2179 | atomic_inc(&conf->active_stripes); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2180 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2181 | raid5_release_stripe(sh); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2182 | conf->max_nr_stripes++; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2183 | return 1; |
| 2184 | } |
| 2185 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2186 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2187 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2188 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2189 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2190 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2191 | if (conf->mddev->gendisk) |
| 2192 | sprintf(conf->cache_name[0], |
| 2193 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 2194 | else |
| 2195 | sprintf(conf->cache_name[0], |
| 2196 | "raid%d-%p", conf->level, conf->mddev); |
| 2197 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 2198 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2199 | conf->active_name = 0; |
| 2200 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2201 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2202 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2203 | if (!sc) |
| 2204 | return 1; |
| 2205 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2206 | conf->pool_size = devs; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2207 | while (num--) |
| 2208 | if (!grow_one_stripe(conf, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2209 | return 1; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2211 | return 0; |
| 2212 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2213 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2214 | /** |
| 2215 | * scribble_len - return the required size of the scribble region |
| 2216 | * @num - total number of disks in the array |
| 2217 | * |
| 2218 | * The size must be enough to contain: |
| 2219 | * 1/ a struct page pointer for each device in the array +2 |
| 2220 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 2221 | * (dma_map_page()) or page (page_address()) address. |
| 2222 | * |
| 2223 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 2224 | * calculate over all devices (not just the data blocks), using zeros in place |
| 2225 | * of the P and Q blocks. |
| 2226 | */ |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2227 | static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2228 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2229 | struct flex_array *ret; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2230 | size_t len; |
| 2231 | |
| 2232 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2233 | ret = flex_array_alloc(len, cnt, flags); |
| 2234 | if (!ret) |
| 2235 | return NULL; |
| 2236 | /* always prealloc all elements, so no locking is required */ |
| 2237 | if (flex_array_prealloc(ret, 0, cnt, flags)) { |
| 2238 | flex_array_free(ret); |
| 2239 | return NULL; |
| 2240 | } |
| 2241 | return ret; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2242 | } |
| 2243 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2244 | static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors) |
| 2245 | { |
| 2246 | unsigned long cpu; |
| 2247 | int err = 0; |
| 2248 | |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2249 | /* |
| 2250 | * Never shrink. And mddev_suspend() could deadlock if this is called |
| 2251 | * from raid5d. In that case, scribble_disks and scribble_sectors |
| 2252 | * should equal to new_disks and new_sectors |
| 2253 | */ |
| 2254 | if (conf->scribble_disks >= new_disks && |
| 2255 | conf->scribble_sectors >= new_sectors) |
| 2256 | return 0; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2257 | mddev_suspend(conf->mddev); |
| 2258 | get_online_cpus(); |
| 2259 | for_each_present_cpu(cpu) { |
| 2260 | struct raid5_percpu *percpu; |
| 2261 | struct flex_array *scribble; |
| 2262 | |
| 2263 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 2264 | scribble = scribble_alloc(new_disks, |
| 2265 | new_sectors / STRIPE_SECTORS, |
| 2266 | GFP_NOIO); |
| 2267 | |
| 2268 | if (scribble) { |
| 2269 | flex_array_free(percpu->scribble); |
| 2270 | percpu->scribble = scribble; |
| 2271 | } else { |
| 2272 | err = -ENOMEM; |
| 2273 | break; |
| 2274 | } |
| 2275 | } |
| 2276 | put_online_cpus(); |
| 2277 | mddev_resume(conf->mddev); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2278 | if (!err) { |
| 2279 | conf->scribble_disks = new_disks; |
| 2280 | conf->scribble_sectors = new_sectors; |
| 2281 | } |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2282 | return err; |
| 2283 | } |
| 2284 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2285 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2286 | { |
| 2287 | /* Make all the stripes able to hold 'newsize' devices. |
| 2288 | * New slots in each stripe get 'page' set to a new page. |
| 2289 | * |
| 2290 | * This happens in stages: |
| 2291 | * 1/ create a new kmem_cache and allocate the required number of |
| 2292 | * stripe_heads. |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 2293 | * 2/ gather all the old stripe_heads and transfer the pages across |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2294 | * to the new stripe_heads. This will have the side effect of |
| 2295 | * freezing the array as once all stripe_heads have been collected, |
| 2296 | * no IO will be possible. Old stripe heads are freed once their |
| 2297 | * pages have been transferred over, and the old kmem_cache is |
| 2298 | * freed when all stripes are done. |
| 2299 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 2300 | * we simple return a failre status - no need to clean anything up. |
| 2301 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 2302 | * If this fails, we don't bother trying the shrink the |
| 2303 | * stripe_heads down again, we just leave them as they are. |
| 2304 | * As each stripe_head is processed the new one is released into |
| 2305 | * active service. |
| 2306 | * |
| 2307 | * Once step2 is started, we cannot afford to wait for a write, |
| 2308 | * so we use GFP_NOIO allocations. |
| 2309 | */ |
| 2310 | struct stripe_head *osh, *nsh; |
| 2311 | LIST_HEAD(newstripes); |
| 2312 | struct disk_info *ndisks; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 2313 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2314 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2315 | int i; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2316 | int hash, cnt; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2317 | |
| 2318 | if (newsize <= conf->pool_size) |
| 2319 | return 0; /* never bother to shrink */ |
| 2320 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 2321 | err = md_allow_write(conf->mddev); |
| 2322 | if (err) |
| 2323 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 2324 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2325 | /* Step 1 */ |
| 2326 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 2327 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2328 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2329 | if (!sc) |
| 2330 | return -ENOMEM; |
| 2331 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2332 | /* Need to ensure auto-resizing doesn't interfere */ |
| 2333 | mutex_lock(&conf->cache_size_mutex); |
| 2334 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2335 | for (i = conf->max_nr_stripes; i; i--) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2336 | nsh = alloc_stripe(sc, GFP_KERNEL, newsize); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2337 | if (!nsh) |
| 2338 | break; |
| 2339 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2340 | nsh->raid_conf = conf; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2341 | list_add(&nsh->lru, &newstripes); |
| 2342 | } |
| 2343 | if (i) { |
| 2344 | /* didn't get enough, give up */ |
| 2345 | while (!list_empty(&newstripes)) { |
| 2346 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2347 | list_del(&nsh->lru); |
| 2348 | kmem_cache_free(sc, nsh); |
| 2349 | } |
| 2350 | kmem_cache_destroy(sc); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2351 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2352 | return -ENOMEM; |
| 2353 | } |
| 2354 | /* Step 2 - Must use GFP_NOIO now. |
| 2355 | * OK, we have enough stripes, start collecting inactive |
| 2356 | * stripes and copying them over |
| 2357 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2358 | hash = 0; |
| 2359 | cnt = 0; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2360 | list_for_each_entry(nsh, &newstripes, lru) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2361 | lock_device_hash_lock(conf, hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 2362 | wait_event_cmd(conf->wait_for_stripe, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2363 | !list_empty(conf->inactive_list + hash), |
| 2364 | unlock_device_hash_lock(conf, hash), |
| 2365 | lock_device_hash_lock(conf, hash)); |
| 2366 | osh = get_free_stripe(conf, hash); |
| 2367 | unlock_device_hash_lock(conf, hash); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2368 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2369 | for(i=0; i<conf->pool_size; i++) { |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2370 | nsh->dev[i].page = osh->dev[i].page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2371 | nsh->dev[i].orig_page = osh->dev[i].page; |
| 2372 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2373 | nsh->hash_lock_index = hash; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2374 | kmem_cache_free(conf->slab_cache, osh); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2375 | cnt++; |
| 2376 | if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + |
| 2377 | !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { |
| 2378 | hash++; |
| 2379 | cnt = 0; |
| 2380 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2381 | } |
| 2382 | kmem_cache_destroy(conf->slab_cache); |
| 2383 | |
| 2384 | /* Step 3. |
| 2385 | * At this point, we are holding all the stripes so the array |
| 2386 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2387 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2388 | */ |
| 2389 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 2390 | if (ndisks) { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2391 | for (i = 0; i < conf->pool_size; i++) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2392 | ndisks[i] = conf->disks[i]; |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2393 | |
| 2394 | for (i = conf->pool_size; i < newsize; i++) { |
| 2395 | ndisks[i].extra_page = alloc_page(GFP_NOIO); |
| 2396 | if (!ndisks[i].extra_page) |
| 2397 | err = -ENOMEM; |
| 2398 | } |
| 2399 | |
| 2400 | if (err) { |
| 2401 | for (i = conf->pool_size; i < newsize; i++) |
| 2402 | if (ndisks[i].extra_page) |
| 2403 | put_page(ndisks[i].extra_page); |
| 2404 | kfree(ndisks); |
| 2405 | } else { |
| 2406 | kfree(conf->disks); |
| 2407 | conf->disks = ndisks; |
| 2408 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2409 | } else |
| 2410 | err = -ENOMEM; |
| 2411 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2412 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2413 | /* Step 4, return new stripes to service */ |
| 2414 | while(!list_empty(&newstripes)) { |
| 2415 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2416 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2417 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2418 | for (i=conf->raid_disks; i < newsize; i++) |
| 2419 | if (nsh->dev[i].page == NULL) { |
| 2420 | struct page *p = alloc_page(GFP_NOIO); |
| 2421 | nsh->dev[i].page = p; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2422 | nsh->dev[i].orig_page = p; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2423 | if (!p) |
| 2424 | err = -ENOMEM; |
| 2425 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2426 | raid5_release_stripe(nsh); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2427 | } |
| 2428 | /* critical section pass, GFP_NOIO no longer needed */ |
| 2429 | |
| 2430 | conf->slab_cache = sc; |
| 2431 | conf->active_name = 1-conf->active_name; |
NeilBrown | 6e9eac2 | 2015-05-08 18:19:34 +1000 | [diff] [blame] | 2432 | if (!err) |
| 2433 | conf->pool_size = newsize; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2434 | return err; |
| 2435 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2436 | |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2437 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2438 | { |
| 2439 | struct stripe_head *sh; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 2440 | int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2441 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2442 | spin_lock_irq(conf->hash_locks + hash); |
| 2443 | sh = get_free_stripe(conf, hash); |
| 2444 | spin_unlock_irq(conf->hash_locks + hash); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2445 | if (!sh) |
| 2446 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2447 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2448 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2449 | kmem_cache_free(conf->slab_cache, sh); |
| 2450 | atomic_dec(&conf->active_stripes); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2451 | conf->max_nr_stripes--; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2452 | return 1; |
| 2453 | } |
| 2454 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2455 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2456 | { |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2457 | while (conf->max_nr_stripes && |
| 2458 | drop_one_stripe(conf)) |
| 2459 | ; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2460 | |
Julia Lawall | 644df1a | 2015-09-13 14:15:10 +0200 | [diff] [blame] | 2461 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | conf->slab_cache = NULL; |
| 2463 | } |
| 2464 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2465 | static void raid5_end_read_request(struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2467 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2468 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2469 | int disks = sh->disks, i; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2470 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2471 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2472 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2473 | |
| 2474 | for (i=0 ; i<disks; i++) |
| 2475 | if (bi == &sh->dev[i].req) |
| 2476 | break; |
| 2477 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2478 | pr_debug("end_read_request %llu/%d, count: %d, error %d.\n", |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2479 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2480 | bi->bi_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2481 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2482 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2483 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2484 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2485 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2486 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2487 | /* If replacement finished while this request was outstanding, |
| 2488 | * 'replacement' might be NULL already. |
| 2489 | * In that case it moved down to 'rdev'. |
| 2490 | * rdev is not removed until all requests are finished. |
| 2491 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2492 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2493 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2494 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2496 | if (use_new_offset(conf, sh)) |
| 2497 | s = sh->sector + rdev->new_data_offset; |
| 2498 | else |
| 2499 | s = sh->sector + rdev->data_offset; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2500 | if (!bi->bi_error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2501 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2502 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2503 | /* Note that this cannot happen on a |
| 2504 | * replacement device. We just fail those on |
| 2505 | * any error |
| 2506 | */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2507 | pr_info_ratelimited( |
| 2508 | "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2509 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2510 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2511 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2512 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2513 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2514 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2515 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2516 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2517 | |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 2518 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 2519 | /* |
| 2520 | * end read for a page in journal, this |
| 2521 | * must be preparing for prexor in rmw |
| 2522 | */ |
| 2523 | set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); |
| 2524 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2525 | if (atomic_read(&rdev->read_errors)) |
| 2526 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2527 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2528 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2529 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2530 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2531 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2533 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2534 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2535 | pr_warn_ratelimited( |
| 2536 | "md/raid:%s: read error on replacement device (sector %llu on %s).\n", |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2537 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2538 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2539 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2540 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 2541 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2542 | pr_warn_ratelimited( |
| 2543 | "md/raid:%s: read error not correctable (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2544 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2545 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2546 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2547 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2548 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2549 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2550 | pr_warn_ratelimited( |
| 2551 | "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2552 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2553 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2554 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2555 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2556 | > conf->max_nr_stripes) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2557 | pr_warn("md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2558 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2559 | else |
| 2560 | retry = 1; |
Bian Yu | edfa1f6 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2561 | if (set_bad && test_bit(In_sync, &rdev->flags) |
| 2562 | && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2563 | retry = 1; |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2564 | if (retry) |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2565 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
| 2566 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 2567 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2568 | } else |
| 2569 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2570 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2571 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2572 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2573 | if (!(set_bad |
| 2574 | && test_bit(In_sync, &rdev->flags) |
| 2575 | && rdev_set_badblocks( |
| 2576 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 2577 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2578 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2580 | rdev_dec_pending(rdev, conf->mddev); |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2581 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 2583 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2584 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | } |
| 2586 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2587 | static void raid5_end_write_request(struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2588 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2589 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2590 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2591 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2592 | struct md_rdev *uninitialized_var(rdev); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2593 | sector_t first_bad; |
| 2594 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2595 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2596 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2597 | for (i = 0 ; i < disks; i++) { |
| 2598 | if (bi == &sh->dev[i].req) { |
| 2599 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2600 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2601 | } |
| 2602 | if (bi == &sh->dev[i].rreq) { |
| 2603 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2604 | if (rdev) |
| 2605 | replacement = 1; |
| 2606 | else |
| 2607 | /* rdev was removed and 'replacement' |
| 2608 | * replaced it. rdev is not removed |
| 2609 | * until all requests are finished. |
| 2610 | */ |
| 2611 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2612 | break; |
| 2613 | } |
| 2614 | } |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2615 | pr_debug("end_write_request %llu/%d, count %d, error: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2616 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2617 | bi->bi_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2618 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2619 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2621 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2622 | } |
| 2623 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2624 | if (replacement) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2625 | if (bi->bi_error) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2626 | md_error(conf->mddev, rdev); |
| 2627 | else if (is_badblock(rdev, sh->sector, |
| 2628 | STRIPE_SECTORS, |
| 2629 | &first_bad, &bad_sectors)) |
| 2630 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 2631 | } else { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2632 | if (bi->bi_error) { |
NeilBrown | 9f97e4b | 2014-01-16 09:35:38 +1100 | [diff] [blame] | 2633 | set_bit(STRIPE_DEGRADED, &sh->state); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2634 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2635 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2636 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2637 | set_bit(MD_RECOVERY_NEEDED, |
| 2638 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2639 | } else if (is_badblock(rdev, sh->sector, |
| 2640 | STRIPE_SECTORS, |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2641 | &first_bad, &bad_sectors)) { |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2642 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2643 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
| 2644 | /* That was a successful write so make |
| 2645 | * sure it looks like we already did |
| 2646 | * a re-write. |
| 2647 | */ |
| 2648 | set_bit(R5_ReWrite, &sh->dev[i].flags); |
| 2649 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2650 | } |
| 2651 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2652 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2653 | if (sh->batch_head && bi->bi_error && !replacement) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2654 | set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state); |
| 2655 | |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2656 | bio_reset(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2657 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 2658 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2660 | raid5_release_stripe(sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2661 | |
| 2662 | if (sh->batch_head && sh != sh->batch_head) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2663 | raid5_release_stripe(sh->batch_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | } |
| 2665 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2666 | 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] | 2667 | { |
| 2668 | struct r5dev *dev = &sh->dev[i]; |
| 2669 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2670 | dev->flags = 0; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2671 | dev->sector = raid5_compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2672 | } |
| 2673 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 2674 | static void raid5_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2675 | { |
| 2676 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2677 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2678 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2679 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2680 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2681 | spin_lock_irqsave(&conf->device_lock, flags); |
| 2682 | clear_bit(In_sync, &rdev->flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2683 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2684 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2685 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2686 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2687 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 2688 | set_bit(Faulty, &rdev->flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2689 | set_mask_bits(&mddev->sb_flags, 0, |
| 2690 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2691 | pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n" |
| 2692 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 2693 | mdname(mddev), |
| 2694 | bdevname(rdev->bdev, b), |
| 2695 | mdname(mddev), |
| 2696 | conf->raid_disks - mddev->degraded); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2697 | r5c_update_on_rdev_error(mddev); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2698 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | |
| 2700 | /* |
| 2701 | * Input: a 'big' sector number, |
| 2702 | * Output: index of the data and parity disk, and the sector # in them. |
| 2703 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2704 | sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
| 2705 | int previous, int *dd_idx, |
| 2706 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2707 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2708 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2709 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2710 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2711 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2712 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2713 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2714 | int algorithm = previous ? conf->prev_algo |
| 2715 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2716 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2717 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2718 | int raid_disks = previous ? conf->previous_raid_disks |
| 2719 | : conf->raid_disks; |
| 2720 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2721 | |
| 2722 | /* First compute the information on this sector */ |
| 2723 | |
| 2724 | /* |
| 2725 | * Compute the chunk number and the sector offset inside the chunk |
| 2726 | */ |
| 2727 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 2728 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2729 | |
| 2730 | /* |
| 2731 | * Compute the stripe number |
| 2732 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2733 | stripe = chunk_number; |
| 2734 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2735 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2736 | /* |
| 2737 | * Select the parity disk based on the user selected algorithm. |
| 2738 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2739 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2740 | switch(conf->level) { |
| 2741 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2742 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2743 | break; |
| 2744 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2745 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2746 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2747 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2748 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2749 | (*dd_idx)++; |
| 2750 | break; |
| 2751 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2752 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2753 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | (*dd_idx)++; |
| 2755 | break; |
| 2756 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2757 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2758 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | break; |
| 2760 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2761 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2762 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2764 | case ALGORITHM_PARITY_0: |
| 2765 | pd_idx = 0; |
| 2766 | (*dd_idx)++; |
| 2767 | break; |
| 2768 | case ALGORITHM_PARITY_N: |
| 2769 | pd_idx = data_disks; |
| 2770 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2771 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2772 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2773 | } |
| 2774 | break; |
| 2775 | case 6: |
| 2776 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2777 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2778 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2779 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2780 | qd_idx = pd_idx + 1; |
| 2781 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2782 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2783 | qd_idx = 0; |
| 2784 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2785 | (*dd_idx) += 2; /* D D P Q D */ |
| 2786 | break; |
| 2787 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2788 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2789 | qd_idx = pd_idx + 1; |
| 2790 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2791 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2792 | qd_idx = 0; |
| 2793 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2794 | (*dd_idx) += 2; /* D D P Q D */ |
| 2795 | break; |
| 2796 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2797 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2798 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2799 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2800 | break; |
| 2801 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2802 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2803 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2804 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2805 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2806 | |
| 2807 | case ALGORITHM_PARITY_0: |
| 2808 | pd_idx = 0; |
| 2809 | qd_idx = 1; |
| 2810 | (*dd_idx) += 2; |
| 2811 | break; |
| 2812 | case ALGORITHM_PARITY_N: |
| 2813 | pd_idx = data_disks; |
| 2814 | qd_idx = data_disks + 1; |
| 2815 | break; |
| 2816 | |
| 2817 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2818 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2819 | * of blocks for computing Q is different. |
| 2820 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2821 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2822 | qd_idx = pd_idx + 1; |
| 2823 | if (pd_idx == raid_disks-1) { |
| 2824 | (*dd_idx)++; /* Q D D D P */ |
| 2825 | qd_idx = 0; |
| 2826 | } else if (*dd_idx >= pd_idx) |
| 2827 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2828 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2829 | break; |
| 2830 | |
| 2831 | case ALGORITHM_ROTATING_N_RESTART: |
| 2832 | /* Same a left_asymmetric, by first stripe is |
| 2833 | * D D D P Q rather than |
| 2834 | * Q D D D P |
| 2835 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2836 | stripe2 += 1; |
| 2837 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2838 | qd_idx = pd_idx + 1; |
| 2839 | if (pd_idx == raid_disks-1) { |
| 2840 | (*dd_idx)++; /* Q D D D P */ |
| 2841 | qd_idx = 0; |
| 2842 | } else if (*dd_idx >= pd_idx) |
| 2843 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2844 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2845 | break; |
| 2846 | |
| 2847 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2848 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2849 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2850 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2851 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2852 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2853 | break; |
| 2854 | |
| 2855 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2856 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2857 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2858 | if (*dd_idx >= pd_idx) |
| 2859 | (*dd_idx)++; |
| 2860 | qd_idx = raid_disks - 1; |
| 2861 | break; |
| 2862 | |
| 2863 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2864 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2865 | if (*dd_idx >= pd_idx) |
| 2866 | (*dd_idx)++; |
| 2867 | qd_idx = raid_disks - 1; |
| 2868 | break; |
| 2869 | |
| 2870 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2871 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2872 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2873 | qd_idx = raid_disks - 1; |
| 2874 | break; |
| 2875 | |
| 2876 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2877 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2878 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2879 | qd_idx = raid_disks - 1; |
| 2880 | break; |
| 2881 | |
| 2882 | case ALGORITHM_PARITY_0_6: |
| 2883 | pd_idx = 0; |
| 2884 | (*dd_idx)++; |
| 2885 | qd_idx = raid_disks - 1; |
| 2886 | break; |
| 2887 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2888 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2889 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2890 | } |
| 2891 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2892 | } |
| 2893 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2894 | if (sh) { |
| 2895 | sh->pd_idx = pd_idx; |
| 2896 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2897 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2898 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | /* |
| 2900 | * Finally, compute the new sector number |
| 2901 | */ |
| 2902 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2903 | return new_sector; |
| 2904 | } |
| 2905 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2906 | sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2908 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2909 | int raid_disks = sh->disks; |
| 2910 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2911 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2912 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2913 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2914 | int algorithm = previous ? conf->prev_algo |
| 2915 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2916 | sector_t stripe; |
| 2917 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2918 | sector_t chunk_number; |
| 2919 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2921 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | |
| 2923 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2924 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2925 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2926 | if (i == sh->pd_idx) |
| 2927 | return 0; |
| 2928 | switch(conf->level) { |
| 2929 | case 4: break; |
| 2930 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2931 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2933 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2934 | if (i > sh->pd_idx) |
| 2935 | i--; |
| 2936 | break; |
| 2937 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2938 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2939 | if (i < sh->pd_idx) |
| 2940 | i += raid_disks; |
| 2941 | i -= (sh->pd_idx + 1); |
| 2942 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2943 | case ALGORITHM_PARITY_0: |
| 2944 | i -= 1; |
| 2945 | break; |
| 2946 | case ALGORITHM_PARITY_N: |
| 2947 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2948 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2949 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2950 | } |
| 2951 | break; |
| 2952 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2953 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2954 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2955 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2956 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2957 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2958 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2959 | case ALGORITHM_ROTATING_N_RESTART: |
| 2960 | if (sh->pd_idx == raid_disks-1) |
| 2961 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2962 | else if (i > sh->pd_idx) |
| 2963 | i -= 2; /* D D P Q D */ |
| 2964 | break; |
| 2965 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2966 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2967 | if (sh->pd_idx == raid_disks-1) |
| 2968 | i--; /* Q D D D P */ |
| 2969 | else { |
| 2970 | /* D D P Q D */ |
| 2971 | if (i < sh->pd_idx) |
| 2972 | i += raid_disks; |
| 2973 | i -= (sh->pd_idx + 2); |
| 2974 | } |
| 2975 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2976 | case ALGORITHM_PARITY_0: |
| 2977 | i -= 2; |
| 2978 | break; |
| 2979 | case ALGORITHM_PARITY_N: |
| 2980 | break; |
| 2981 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2982 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2983 | if (sh->pd_idx == 0) |
| 2984 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2985 | else { |
| 2986 | /* D D Q P D */ |
| 2987 | if (i < sh->pd_idx) |
| 2988 | i += raid_disks; |
| 2989 | i -= (sh->pd_idx + 1); |
| 2990 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2991 | break; |
| 2992 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2993 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2994 | if (i > sh->pd_idx) |
| 2995 | i--; |
| 2996 | break; |
| 2997 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2998 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2999 | if (i < sh->pd_idx) |
| 3000 | i += data_disks + 1; |
| 3001 | i -= (sh->pd_idx + 1); |
| 3002 | break; |
| 3003 | case ALGORITHM_PARITY_0_6: |
| 3004 | i -= 1; |
| 3005 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3006 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3007 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3008 | } |
| 3009 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3010 | } |
| 3011 | |
| 3012 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 3013 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3014 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3015 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3016 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3017 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 3018 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3019 | pr_warn("md/raid:%s: compute_blocknr: map not correct\n", |
| 3020 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3021 | return 0; |
| 3022 | } |
| 3023 | return r_sector; |
| 3024 | } |
| 3025 | |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3026 | /* |
| 3027 | * There are cases where we want handle_stripe_dirtying() and |
| 3028 | * schedule_reconstruction() to delay towrite to some dev of a stripe. |
| 3029 | * |
| 3030 | * This function checks whether we want to delay the towrite. Specifically, |
| 3031 | * we delay the towrite when: |
| 3032 | * |
| 3033 | * 1. degraded stripe has a non-overwrite to the missing dev, AND this |
| 3034 | * stripe has data in journal (for other devices). |
| 3035 | * |
| 3036 | * In this case, when reading data for the non-overwrite dev, it is |
| 3037 | * necessary to handle complex rmw of write back cache (prexor with |
| 3038 | * orig_page, and xor with page). To keep read path simple, we would |
| 3039 | * like to flush data in journal to RAID disks first, so complex rmw |
| 3040 | * is handled in the write patch (handle_stripe_dirtying). |
| 3041 | * |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3042 | * 2. when journal space is critical (R5C_LOG_CRITICAL=1) |
| 3043 | * |
| 3044 | * It is important to be able to flush all stripes in raid5-cache. |
| 3045 | * Therefore, we need reserve some space on the journal device for |
| 3046 | * these flushes. If flush operation includes pending writes to the |
| 3047 | * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe |
| 3048 | * for the flush out. If we exclude these pending writes from flush |
| 3049 | * operation, we only need (conf->max_degraded + 1) pages per stripe. |
| 3050 | * Therefore, excluding pending writes in these cases enables more |
| 3051 | * efficient use of the journal device. |
| 3052 | * |
| 3053 | * Note: To make sure the stripe makes progress, we only delay |
| 3054 | * towrite for stripes with data already in journal (injournal > 0). |
| 3055 | * When LOG_CRITICAL, stripes with injournal == 0 will be sent to |
| 3056 | * no_space_stripes list. |
| 3057 | * |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3058 | */ |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3059 | static inline bool delay_towrite(struct r5conf *conf, |
| 3060 | struct r5dev *dev, |
| 3061 | struct stripe_head_state *s) |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3062 | { |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3063 | /* case 1 above */ |
| 3064 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 3065 | !test_bit(R5_Insync, &dev->flags) && s->injournal) |
| 3066 | return true; |
| 3067 | /* case 2 above */ |
| 3068 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && |
| 3069 | s->injournal > 0) |
| 3070 | return true; |
| 3071 | return false; |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3072 | } |
| 3073 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3074 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3075 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3076 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3077 | { |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3078 | int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3079 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3080 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3081 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3082 | if (rcw) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3083 | /* |
| 3084 | * In some cases, handle_stripe_dirtying initially decided to |
| 3085 | * run rmw and allocates extra page for prexor. However, rcw is |
| 3086 | * cheaper later on. We need to free the extra page now, |
| 3087 | * because we won't be able to do that in ops_complete_prexor(). |
| 3088 | */ |
| 3089 | r5c_release_extra_page(sh); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3090 | |
| 3091 | for (i = disks; i--; ) { |
| 3092 | struct r5dev *dev = &sh->dev[i]; |
| 3093 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3094 | if (dev->towrite && !delay_towrite(conf, dev, s)) { |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3095 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3096 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3097 | if (!expand) |
| 3098 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3099 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3100 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 3101 | set_bit(R5_LOCKED, &dev->flags); |
| 3102 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3103 | } |
| 3104 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 3105 | /* if we are not expanding this is a proper write request, and |
| 3106 | * there will be bios with new data to be drained into the |
| 3107 | * stripe cache |
| 3108 | */ |
| 3109 | if (!expand) { |
| 3110 | if (!s->locked) |
| 3111 | /* False alarm, nothing to do */ |
| 3112 | return; |
| 3113 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 3114 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 3115 | } else |
| 3116 | sh->reconstruct_state = reconstruct_state_run; |
| 3117 | |
| 3118 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
| 3119 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3120 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3121 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3122 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3123 | } else { |
| 3124 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 3125 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3126 | BUG_ON(level == 6 && |
| 3127 | (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) || |
| 3128 | test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags)))); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3129 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3130 | for (i = disks; i--; ) { |
| 3131 | struct r5dev *dev = &sh->dev[i]; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3132 | if (i == pd_idx || i == qd_idx) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3133 | continue; |
| 3134 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3135 | if (dev->towrite && |
| 3136 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3137 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 3138 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3139 | set_bit(R5_LOCKED, &dev->flags); |
| 3140 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3141 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3142 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 3143 | set_bit(R5_LOCKED, &dev->flags); |
| 3144 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3145 | } |
| 3146 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 3147 | if (!s->locked) |
| 3148 | /* False alarm - nothing to do */ |
| 3149 | return; |
| 3150 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
| 3151 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 3152 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 3153 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3154 | } |
| 3155 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3156 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3157 | * are in flight |
| 3158 | */ |
| 3159 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 3160 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3161 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3162 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3163 | if (level == 6) { |
| 3164 | int qd_idx = sh->qd_idx; |
| 3165 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 3166 | |
| 3167 | set_bit(R5_LOCKED, &dev->flags); |
| 3168 | clear_bit(R5_UPTODATE, &dev->flags); |
| 3169 | s->locked++; |
| 3170 | } |
| 3171 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 3172 | if (raid5_has_ppl(sh->raid_conf) && |
| 3173 | test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) && |
| 3174 | !test_bit(STRIPE_FULL_WRITE, &sh->state) && |
| 3175 | test_bit(R5_Insync, &sh->dev[pd_idx].flags)) |
| 3176 | set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request); |
| 3177 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3178 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 3179 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3180 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3181 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3182 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3183 | /* |
| 3184 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3185 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3186 | * The bi_next chain must be in order. |
| 3187 | */ |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3188 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, |
| 3189 | int forwrite, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | { |
| 3191 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3192 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3193 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3194 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3195 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3196 | (unsigned long long)bi->bi_iter.bi_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3197 | (unsigned long long)sh->sector); |
| 3198 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3199 | /* |
| 3200 | * If several bio share a stripe. The bio bi_phys_segments acts as a |
| 3201 | * reference count to avoid race. The reference count should already be |
| 3202 | * increased before this function is called (for example, in |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 3203 | * raid5_make_request()), so other bio sharing this stripe will not free the |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3204 | * stripe. If a stripe is owned by one stripe, the stripe lock will |
| 3205 | * protect it. |
| 3206 | */ |
| 3207 | spin_lock_irq(&sh->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3208 | /* Don't allow new IO added to stripes in batch list */ |
| 3209 | if (sh->batch_head) |
| 3210 | goto overlap; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3211 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3212 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3213 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3214 | firstwrite = 1; |
| 3215 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3216 | bip = &sh->dev[dd_idx].toread; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3217 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { |
| 3218 | if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3219 | goto overlap; |
| 3220 | bip = & (*bip)->bi_next; |
| 3221 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3222 | if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3223 | goto overlap; |
| 3224 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 3225 | if (forwrite && raid5_has_ppl(conf)) { |
| 3226 | /* |
| 3227 | * With PPL only writes to consecutive data chunks within a |
| 3228 | * stripe are allowed because for a single stripe_head we can |
| 3229 | * only have one PPL entry at a time, which describes one data |
| 3230 | * range. Not really an overlap, but wait_for_overlap can be |
| 3231 | * used to handle this. |
| 3232 | */ |
| 3233 | sector_t sector; |
| 3234 | sector_t first = 0; |
| 3235 | sector_t last = 0; |
| 3236 | int count = 0; |
| 3237 | int i; |
| 3238 | |
| 3239 | for (i = 0; i < sh->disks; i++) { |
| 3240 | if (i != sh->pd_idx && |
| 3241 | (i == dd_idx || sh->dev[i].towrite)) { |
| 3242 | sector = sh->dev[i].sector; |
| 3243 | if (count == 0 || sector < first) |
| 3244 | first = sector; |
| 3245 | if (sector > last) |
| 3246 | last = sector; |
| 3247 | count++; |
| 3248 | } |
| 3249 | } |
| 3250 | |
| 3251 | if (first + conf->chunk_sectors * (count - 1) != last) |
| 3252 | goto overlap; |
| 3253 | } |
| 3254 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3255 | if (!forwrite || previous) |
| 3256 | clear_bit(STRIPE_BATCH_READY, &sh->state); |
| 3257 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 3258 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3259 | if (*bip) |
| 3260 | bi->bi_next = *bip; |
| 3261 | *bip = bi; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3262 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3263 | md_write_inc(conf->mddev, bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3264 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3265 | if (forwrite) { |
| 3266 | /* check if page is covered */ |
| 3267 | sector_t sector = sh->dev[dd_idx].sector; |
| 3268 | for (bi=sh->dev[dd_idx].towrite; |
| 3269 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3270 | bi && bi->bi_iter.bi_sector <= sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3271 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 3272 | if (bio_end_sector(bi) >= sector) |
| 3273 | sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3274 | } |
| 3275 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3276 | if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags)) |
| 3277 | sh->overwrite_disks++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3278 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3279 | |
| 3280 | 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] | 3281 | (unsigned long long)(*bip)->bi_iter.bi_sector, |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3282 | (unsigned long long)sh->sector, dd_idx); |
| 3283 | |
| 3284 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3285 | /* Cannot hold spinlock over bitmap_startwrite, |
| 3286 | * but must ensure this isn't added to a batch until |
| 3287 | * we have added to the bitmap and set bm_seq. |
| 3288 | * So set STRIPE_BITMAP_PENDING to prevent |
| 3289 | * batching. |
| 3290 | * If multiple add_stripe_bio() calls race here they |
| 3291 | * much all set STRIPE_BITMAP_PENDING. So only the first one |
| 3292 | * to complete "bitmap_startwrite" gets to set |
| 3293 | * STRIPE_BIT_DELAY. This is important as once a stripe |
| 3294 | * is added to a batch, STRIPE_BIT_DELAY cannot be changed |
| 3295 | * any more. |
| 3296 | */ |
| 3297 | set_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3298 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3299 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 3300 | STRIPE_SECTORS, 0); |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3301 | spin_lock_irq(&sh->stripe_lock); |
| 3302 | clear_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3303 | if (!sh->batch_head) { |
| 3304 | sh->bm_seq = conf->seq_flush+1; |
| 3305 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 3306 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3307 | } |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3308 | spin_unlock_irq(&sh->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3309 | |
| 3310 | if (stripe_can_batch(sh)) |
| 3311 | stripe_add_to_batch_list(conf, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3312 | return 1; |
| 3313 | |
| 3314 | overlap: |
| 3315 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3316 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3317 | return 0; |
| 3318 | } |
| 3319 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3320 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3321 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3322 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3323 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3324 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3325 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 3326 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3327 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3328 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3329 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3330 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3331 | raid5_compute_sector(conf, |
| 3332 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 3333 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3334 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3335 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3336 | } |
| 3337 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3338 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3339 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 3340 | struct stripe_head_state *s, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3341 | { |
| 3342 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3343 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3344 | for (i = disks; i--; ) { |
| 3345 | struct bio *bi; |
| 3346 | int bitmap_end = 0; |
| 3347 | |
| 3348 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3349 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3350 | rcu_read_lock(); |
| 3351 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 3352 | if (rdev && test_bit(In_sync, &rdev->flags) && |
| 3353 | !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3354 | atomic_inc(&rdev->nr_pending); |
| 3355 | else |
| 3356 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3357 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3358 | if (rdev) { |
| 3359 | if (!rdev_set_badblocks( |
| 3360 | rdev, |
| 3361 | sh->sector, |
| 3362 | STRIPE_SECTORS, 0)) |
| 3363 | md_error(conf->mddev, rdev); |
| 3364 | rdev_dec_pending(rdev, conf->mddev); |
| 3365 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3366 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3367 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3368 | /* fail all writes first */ |
| 3369 | bi = sh->dev[i].towrite; |
| 3370 | sh->dev[i].towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3371 | sh->overwrite_disks = 0; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3372 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 1ed850f | 2012-10-11 13:50:13 +1100 | [diff] [blame] | 3373 | if (bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3374 | bitmap_end = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3375 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 3376 | log_stripe_write_finished(sh); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3377 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3378 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3379 | wake_up(&conf->wait_for_overlap); |
| 3380 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3381 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3382 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3383 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3384 | |
| 3385 | bi->bi_error = -EIO; |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3386 | md_write_end(conf->mddev); |
| 3387 | if (!raid5_dec_bi_active_stripes(bi)) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 3388 | bio_endio(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3389 | bi = nextbi; |
| 3390 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3391 | if (bitmap_end) |
| 3392 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3393 | STRIPE_SECTORS, 0, 0); |
| 3394 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3395 | /* and fail all 'written' */ |
| 3396 | bi = sh->dev[i].written; |
| 3397 | sh->dev[i].written = NULL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3398 | if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { |
| 3399 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 3400 | sh->dev[i].page = sh->dev[i].orig_page; |
| 3401 | } |
| 3402 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3403 | if (bi) bitmap_end = 1; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3404 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3405 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3406 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3407 | |
| 3408 | bi->bi_error = -EIO; |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3409 | md_write_end(conf->mddev); |
| 3410 | if (!raid5_dec_bi_active_stripes(bi)) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 3411 | bio_endio(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3412 | bi = bi2; |
| 3413 | } |
| 3414 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3415 | /* fail any reads if this device is non-operational and |
| 3416 | * the data has not reached the cache yet. |
| 3417 | */ |
| 3418 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 3419 | s->failed > conf->max_degraded && |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3420 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 3421 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3422 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3423 | bi = sh->dev[i].toread; |
| 3424 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3425 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3426 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3427 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3428 | if (bi) |
| 3429 | s->to_read--; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3430 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3431 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3432 | struct bio *nextbi = |
| 3433 | r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3434 | |
| 3435 | bi->bi_error = -EIO; |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3436 | if (!raid5_dec_bi_active_stripes(bi)) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 3437 | bio_endio(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3438 | bi = nextbi; |
| 3439 | } |
| 3440 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3441 | if (bitmap_end) |
| 3442 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3443 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3444 | /* If we were in the middle of a write the parity block might |
| 3445 | * still be locked - so just clear all R5_LOCKED flags |
| 3446 | */ |
| 3447 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3448 | } |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3449 | s->to_write = 0; |
| 3450 | s->written = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3451 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3452 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3453 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3454 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3455 | } |
| 3456 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3457 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3458 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3459 | struct stripe_head_state *s) |
| 3460 | { |
| 3461 | int abort = 0; |
| 3462 | int i; |
| 3463 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3464 | BUG_ON(sh->batch_head); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3465 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3466 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 3467 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3468 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3469 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3470 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3471 | * Don't even need to abort as that is handled elsewhere |
| 3472 | * if needed, and not always wanted e.g. if there is a known |
| 3473 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3474 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3475 | * non-sync devices, or abort the recovery |
| 3476 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3477 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 3478 | /* During recovery devices cannot be removed, so |
| 3479 | * locking and refcounting of rdevs is not needed |
| 3480 | */ |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3481 | rcu_read_lock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3482 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3483 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3484 | if (rdev |
| 3485 | && !test_bit(Faulty, &rdev->flags) |
| 3486 | && !test_bit(In_sync, &rdev->flags) |
| 3487 | && !rdev_set_badblocks(rdev, sh->sector, |
| 3488 | STRIPE_SECTORS, 0)) |
| 3489 | abort = 1; |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3490 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3491 | if (rdev |
| 3492 | && !test_bit(Faulty, &rdev->flags) |
| 3493 | && !test_bit(In_sync, &rdev->flags) |
| 3494 | && !rdev_set_badblocks(rdev, sh->sector, |
| 3495 | STRIPE_SECTORS, 0)) |
| 3496 | abort = 1; |
| 3497 | } |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3498 | rcu_read_unlock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3499 | if (abort) |
| 3500 | conf->recovery_disabled = |
| 3501 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3502 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3503 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3504 | } |
| 3505 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3506 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 3507 | { |
| 3508 | struct md_rdev *rdev; |
| 3509 | int rv = 0; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3510 | |
| 3511 | rcu_read_lock(); |
| 3512 | rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3513 | if (rdev |
| 3514 | && !test_bit(Faulty, &rdev->flags) |
| 3515 | && !test_bit(In_sync, &rdev->flags) |
| 3516 | && (rdev->recovery_offset <= sh->sector |
| 3517 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 3518 | rv = 1; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3519 | rcu_read_unlock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3520 | return rv; |
| 3521 | } |
| 3522 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3523 | static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3524 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3525 | { |
| 3526 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3527 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 3528 | &sh->dev[s->failed_num[1]] }; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3529 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3530 | |
NeilBrown | a79cfe1 | 2015-02-02 11:37:59 +1100 | [diff] [blame] | 3531 | |
| 3532 | if (test_bit(R5_LOCKED, &dev->flags) || |
| 3533 | test_bit(R5_UPTODATE, &dev->flags)) |
| 3534 | /* No point reading this as we already have it or have |
| 3535 | * decided to get it. |
| 3536 | */ |
| 3537 | return 0; |
| 3538 | |
| 3539 | if (dev->toread || |
| 3540 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags))) |
| 3541 | /* We need this block to directly satisfy a request */ |
| 3542 | return 1; |
| 3543 | |
| 3544 | if (s->syncing || s->expanding || |
| 3545 | (s->replacing && want_replace(sh, disk_idx))) |
| 3546 | /* When syncing, or expanding we read everything. |
| 3547 | * When replacing, we need the replaced block. |
| 3548 | */ |
| 3549 | return 1; |
| 3550 | |
| 3551 | if ((s->failed >= 1 && fdev[0]->toread) || |
| 3552 | (s->failed >= 2 && fdev[1]->toread)) |
| 3553 | /* If we want to read from a failed device, then |
| 3554 | * we need to actually read every other device. |
| 3555 | */ |
| 3556 | return 1; |
| 3557 | |
NeilBrown | a9d5695 | 2015-02-02 11:49:10 +1100 | [diff] [blame] | 3558 | /* Sometimes neither read-modify-write nor reconstruct-write |
| 3559 | * cycles can work. In those cases we read every block we |
| 3560 | * can. Then the parity-update is certain to have enough to |
| 3561 | * work with. |
| 3562 | * This can only be a problem when we need to write something, |
| 3563 | * and some device has failed. If either of those tests |
| 3564 | * fail we need look no further. |
| 3565 | */ |
| 3566 | if (!s->failed || !s->to_write) |
| 3567 | return 0; |
| 3568 | |
| 3569 | if (test_bit(R5_Insync, &dev->flags) && |
| 3570 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3571 | /* Pre-reads at not permitted until after short delay |
| 3572 | * to gather multiple requests. However if this |
| 3573 | * device is no Insync, the block could only be be computed |
| 3574 | * and there is no need to delay that. |
| 3575 | */ |
| 3576 | return 0; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3577 | |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3578 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3579 | if (fdev[i]->towrite && |
| 3580 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
| 3581 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3582 | /* If we have a partial write to a failed |
| 3583 | * device, then we will need to reconstruct |
| 3584 | * the content of that device, so all other |
| 3585 | * devices must be read. |
| 3586 | */ |
| 3587 | return 1; |
| 3588 | } |
| 3589 | |
| 3590 | /* If we are forced to do a reconstruct-write, either because |
| 3591 | * the current RAID6 implementation only supports that, or |
| 3592 | * or because parity cannot be trusted and we are currently |
| 3593 | * recovering it, there is extra need to be careful. |
| 3594 | * If one of the devices that we would need to read, because |
| 3595 | * it is not being overwritten (and maybe not written at all) |
| 3596 | * is missing/faulty, then we need to read everything we can. |
| 3597 | */ |
| 3598 | if (sh->raid_conf->level != 6 && |
| 3599 | sh->sector < sh->raid_conf->mddev->recovery_cp) |
| 3600 | /* reconstruct-write isn't being forced */ |
| 3601 | return 0; |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3602 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | 10d82c5 | 2015-05-08 18:19:33 +1000 | [diff] [blame] | 3603 | if (s->failed_num[i] != sh->pd_idx && |
| 3604 | s->failed_num[i] != sh->qd_idx && |
| 3605 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3606 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3607 | return 1; |
| 3608 | } |
| 3609 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3610 | return 0; |
| 3611 | } |
| 3612 | |
Song Liu | ba02684 | 2017-01-12 17:22:42 -0800 | [diff] [blame] | 3613 | /* fetch_block - checks the given member device to see if its data needs |
| 3614 | * to be read or computed to satisfy a request. |
| 3615 | * |
| 3616 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 3617 | * 0 to tell the loop in handle_stripe_fill to continue |
| 3618 | */ |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3619 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3620 | int disk_idx, int disks) |
| 3621 | { |
| 3622 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 3623 | |
| 3624 | /* is the data in this block needed, and can we get it? */ |
| 3625 | if (need_this_block(sh, s, disk_idx, disks)) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3626 | /* we would like to get this block, possibly by computing it, |
| 3627 | * otherwise read it if the backing disk is insync |
| 3628 | */ |
| 3629 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 3630 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
NeilBrown | b0c783b | 2015-05-08 18:19:32 +1000 | [diff] [blame] | 3631 | BUG_ON(sh->batch_head); |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3632 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3633 | (s->failed && (disk_idx == s->failed_num[0] || |
| 3634 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3635 | /* have disk failed, and we're requested to fetch it; |
| 3636 | * do compute it |
| 3637 | */ |
| 3638 | pr_debug("Computing stripe %llu block %d\n", |
| 3639 | (unsigned long long)sh->sector, disk_idx); |
| 3640 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3641 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3642 | set_bit(R5_Wantcompute, &dev->flags); |
| 3643 | sh->ops.target = disk_idx; |
| 3644 | sh->ops.target2 = -1; /* no 2nd target */ |
| 3645 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3646 | /* Careful: from this point on 'uptodate' is in the eye |
| 3647 | * of raid_run_ops which services 'compute' operations |
| 3648 | * before writes. R5_Wantcompute flags a block that will |
| 3649 | * be R5_UPTODATE by the time it is needed for a |
| 3650 | * subsequent operation. |
| 3651 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3652 | s->uptodate++; |
| 3653 | return 1; |
| 3654 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 3655 | /* Computing 2-failure is *very* expensive; only |
| 3656 | * do it if failed >= 2 |
| 3657 | */ |
| 3658 | int other; |
| 3659 | for (other = disks; other--; ) { |
| 3660 | if (other == disk_idx) |
| 3661 | continue; |
| 3662 | if (!test_bit(R5_UPTODATE, |
| 3663 | &sh->dev[other].flags)) |
| 3664 | break; |
| 3665 | } |
| 3666 | BUG_ON(other < 0); |
| 3667 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 3668 | (unsigned long long)sh->sector, |
| 3669 | disk_idx, other); |
| 3670 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3671 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3672 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 3673 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 3674 | sh->ops.target = disk_idx; |
| 3675 | sh->ops.target2 = other; |
| 3676 | s->uptodate += 2; |
| 3677 | s->req_compute = 1; |
| 3678 | return 1; |
| 3679 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 3680 | set_bit(R5_LOCKED, &dev->flags); |
| 3681 | set_bit(R5_Wantread, &dev->flags); |
| 3682 | s->locked++; |
| 3683 | pr_debug("Reading block %d (sync=%d)\n", |
| 3684 | disk_idx, s->syncing); |
| 3685 | } |
| 3686 | } |
| 3687 | |
| 3688 | return 0; |
| 3689 | } |
| 3690 | |
| 3691 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3692 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3693 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3694 | static void handle_stripe_fill(struct stripe_head *sh, |
| 3695 | struct stripe_head_state *s, |
| 3696 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3697 | { |
| 3698 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3699 | |
| 3700 | /* look for blocks to read/compute, skip this if a compute |
| 3701 | * is already in flight, or if the stripe contents are in the |
| 3702 | * midst of changing due to a write |
| 3703 | */ |
| 3704 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3705 | !sh->reconstruct_state) { |
| 3706 | |
| 3707 | /* |
| 3708 | * For degraded stripe with data in journal, do not handle |
| 3709 | * read requests yet, instead, flush the stripe to raid |
| 3710 | * disks first, this avoids handling complex rmw of write |
| 3711 | * back cache (prexor with orig_page, and then xor with |
| 3712 | * page) in the read path |
| 3713 | */ |
| 3714 | if (s->injournal && s->failed) { |
| 3715 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
| 3716 | r5c_make_stripe_write_out(sh); |
| 3717 | goto out; |
| 3718 | } |
| 3719 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3720 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3721 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3722 | break; |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3723 | } |
| 3724 | out: |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3725 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3726 | } |
| 3727 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 3728 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 3729 | unsigned long handle_flags); |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3730 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3731 | * any written block on an uptodate or failed drive can be returned. |
| 3732 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 3733 | * never LOCKED, so we don't need to test 'failed' directly. |
| 3734 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3735 | static void handle_stripe_clean_event(struct r5conf *conf, |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 3736 | struct stripe_head *sh, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3737 | { |
| 3738 | int i; |
| 3739 | struct r5dev *dev; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3740 | int discard_pending = 0; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3741 | struct stripe_head *head_sh = sh; |
| 3742 | bool do_endio = false; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3743 | |
| 3744 | for (i = disks; i--; ) |
| 3745 | if (sh->dev[i].written) { |
| 3746 | dev = &sh->dev[i]; |
| 3747 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3748 | (test_bit(R5_UPTODATE, &dev->flags) || |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3749 | test_bit(R5_Discard, &dev->flags) || |
| 3750 | test_bit(R5_SkipCopy, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3751 | /* We can return any write requests */ |
| 3752 | struct bio *wbi, *wbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3753 | pr_debug("Return write for disc %d\n", i); |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 3754 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
| 3755 | clear_bit(R5_UPTODATE, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3756 | if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { |
| 3757 | WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3758 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3759 | do_endio = true; |
| 3760 | |
| 3761 | returnbi: |
| 3762 | dev->page = dev->orig_page; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3763 | wbi = dev->written; |
| 3764 | dev->written = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3765 | while (wbi && wbi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3766 | dev->sector + STRIPE_SECTORS) { |
| 3767 | wbi2 = r5_next_bio(wbi, dev->sector); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3768 | md_write_end(conf->mddev); |
| 3769 | if (!raid5_dec_bi_active_stripes(wbi)) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 3770 | bio_endio(wbi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3771 | wbi = wbi2; |
| 3772 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3773 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3774 | STRIPE_SECTORS, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3775 | !test_bit(STRIPE_DEGRADED, &sh->state), |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3776 | 0); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3777 | if (head_sh->batch_head) { |
| 3778 | sh = list_first_entry(&sh->batch_list, |
| 3779 | struct stripe_head, |
| 3780 | batch_list); |
| 3781 | if (sh != head_sh) { |
| 3782 | dev = &sh->dev[i]; |
| 3783 | goto returnbi; |
| 3784 | } |
| 3785 | } |
| 3786 | sh = head_sh; |
| 3787 | dev = &sh->dev[i]; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3788 | } else if (test_bit(R5_Discard, &dev->flags)) |
| 3789 | discard_pending = 1; |
| 3790 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 3791 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 3792 | log_stripe_write_finished(sh); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3793 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3794 | if (!discard_pending && |
| 3795 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3796 | int hash; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3797 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 3798 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
| 3799 | if (sh->qd_idx >= 0) { |
| 3800 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 3801 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); |
| 3802 | } |
| 3803 | /* now that discard is done we can proceed with any sync */ |
| 3804 | clear_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 3805 | /* |
| 3806 | * SCSI discard will change some bio fields and the stripe has |
| 3807 | * no updated data, so remove it from hash list and the stripe |
| 3808 | * will be reinitialized |
| 3809 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3810 | unhash: |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3811 | hash = sh->hash_lock_index; |
| 3812 | spin_lock_irq(conf->hash_locks + hash); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 3813 | remove_hash(sh); |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3814 | spin_unlock_irq(conf->hash_locks + hash); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3815 | if (head_sh->batch_head) { |
| 3816 | sh = list_first_entry(&sh->batch_list, |
| 3817 | struct stripe_head, batch_list); |
| 3818 | if (sh != head_sh) |
| 3819 | goto unhash; |
| 3820 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3821 | sh = head_sh; |
| 3822 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3823 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
| 3824 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3825 | |
| 3826 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3827 | |
| 3828 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3829 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3830 | md_wakeup_thread(conf->mddev->thread); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3831 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 3832 | if (head_sh->batch_head && do_endio) |
| 3833 | break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3834 | } |
| 3835 | |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 3836 | /* |
| 3837 | * For RMW in write back cache, we need extra page in prexor to store the |
| 3838 | * old data. This page is stored in dev->orig_page. |
| 3839 | * |
| 3840 | * This function checks whether we have data for prexor. The exact logic |
| 3841 | * is: |
| 3842 | * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE) |
| 3843 | */ |
| 3844 | static inline bool uptodate_for_rmw(struct r5dev *dev) |
| 3845 | { |
| 3846 | return (test_bit(R5_UPTODATE, &dev->flags)) && |
| 3847 | (!test_bit(R5_InJournal, &dev->flags) || |
| 3848 | test_bit(R5_OrigPageUPTDODATE, &dev->flags)); |
| 3849 | } |
| 3850 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3851 | static int handle_stripe_dirtying(struct r5conf *conf, |
| 3852 | struct stripe_head *sh, |
| 3853 | struct stripe_head_state *s, |
| 3854 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3855 | { |
| 3856 | int rmw = 0, rcw = 0, i; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3857 | sector_t recovery_cp = conf->mddev->recovery_cp; |
| 3858 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3859 | /* Check whether resync is now happening or should start. |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3860 | * If yes, then the array is dirty (after unclean shutdown or |
| 3861 | * initial creation), so parity in some stripes might be inconsistent. |
| 3862 | * In this case, we need to always do reconstruct-write, to ensure |
| 3863 | * that in case of drive failure or read-error correction, we |
| 3864 | * generate correct data from the parity. |
| 3865 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3866 | if (conf->rmw_level == PARITY_DISABLE_RMW || |
NeilBrown | 26ac107 | 2015-02-18 11:35:14 +1100 | [diff] [blame] | 3867 | (recovery_cp < MaxSector && sh->sector >= recovery_cp && |
| 3868 | s->failed == 0)) { |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3869 | /* Calculate the real rcw later - for now make it |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3870 | * look like rcw is cheaper |
| 3871 | */ |
| 3872 | rcw = 1; rmw = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3873 | pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n", |
| 3874 | conf->rmw_level, (unsigned long long)recovery_cp, |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3875 | (unsigned long long)sh->sector); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3876 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3877 | /* would I have to read this buffer for read_modify_write */ |
| 3878 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3879 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3880 | i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3881 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3882 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 3883 | !(uptodate_for_rmw(dev) || |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3884 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3885 | if (test_bit(R5_Insync, &dev->flags)) |
| 3886 | rmw++; |
| 3887 | else |
| 3888 | rmw += 2*disks; /* cannot read it */ |
| 3889 | } |
| 3890 | /* Would I have to read this buffer for reconstruct_write */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3891 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 3892 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3893 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3894 | !(test_bit(R5_UPTODATE, &dev->flags) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3895 | test_bit(R5_Wantcompute, &dev->flags))) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3896 | if (test_bit(R5_Insync, &dev->flags)) |
| 3897 | rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3898 | else |
| 3899 | rcw += 2*disks; |
| 3900 | } |
| 3901 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3902 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3903 | pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n", |
| 3904 | (unsigned long long)sh->sector, sh->state, rmw, rcw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3905 | set_bit(STRIPE_HANDLE, &sh->state); |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 3906 | if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3907 | /* prefer read-modify-write, but need to get some data */ |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 3908 | if (conf->mddev->queue) |
| 3909 | blk_add_trace_msg(conf->mddev->queue, |
| 3910 | "raid5 rmw %llu %d", |
| 3911 | (unsigned long long)sh->sector, rmw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3912 | for (i = disks; i--; ) { |
| 3913 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3914 | if (test_bit(R5_InJournal, &dev->flags) && |
| 3915 | dev->page == dev->orig_page && |
| 3916 | !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) { |
| 3917 | /* alloc page for prexor */ |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3918 | struct page *p = alloc_page(GFP_NOIO); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3919 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3920 | if (p) { |
| 3921 | dev->orig_page = p; |
| 3922 | continue; |
| 3923 | } |
| 3924 | |
| 3925 | /* |
| 3926 | * alloc_page() failed, try use |
| 3927 | * disk_info->extra_page |
| 3928 | */ |
| 3929 | if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE, |
| 3930 | &conf->cache_state)) { |
| 3931 | r5c_use_extra_page(sh); |
| 3932 | break; |
| 3933 | } |
| 3934 | |
| 3935 | /* extra_page in use, add to delayed_list */ |
| 3936 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3937 | s->waiting_extra_page = 1; |
| 3938 | return -EAGAIN; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3939 | } |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3940 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3941 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3942 | for (i = disks; i--; ) { |
| 3943 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3944 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3945 | i == sh->pd_idx || i == sh->qd_idx || |
| 3946 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3947 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 3948 | !(uptodate_for_rmw(dev) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3949 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3950 | test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3951 | if (test_bit(STRIPE_PREREAD_ACTIVE, |
| 3952 | &sh->state)) { |
| 3953 | pr_debug("Read_old block %d for r-m-w\n", |
| 3954 | i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3955 | set_bit(R5_LOCKED, &dev->flags); |
| 3956 | set_bit(R5_Wantread, &dev->flags); |
| 3957 | s->locked++; |
| 3958 | } else { |
| 3959 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3960 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3961 | } |
| 3962 | } |
| 3963 | } |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3964 | } |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 3965 | if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3966 | /* want reconstruct write, but need to get some data */ |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3967 | int qread =0; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3968 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3969 | for (i = disks; i--; ) { |
| 3970 | struct r5dev *dev = &sh->dev[i]; |
| 3971 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3972 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3973 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3974 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3975 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 3976 | rcw++; |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3977 | if (test_bit(R5_Insync, &dev->flags) && |
| 3978 | test_bit(STRIPE_PREREAD_ACTIVE, |
| 3979 | &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3980 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3981 | "%d for Reconstruct\n", i); |
| 3982 | set_bit(R5_LOCKED, &dev->flags); |
| 3983 | set_bit(R5_Wantread, &dev->flags); |
| 3984 | s->locked++; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3985 | qread++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3986 | } else { |
| 3987 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3988 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3989 | } |
| 3990 | } |
| 3991 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 3992 | if (rcw && conf->mddev->queue) |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3993 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
| 3994 | (unsigned long long)sh->sector, |
| 3995 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3996 | } |
NeilBrown | b1b02fe | 2015-02-02 10:44:29 +1100 | [diff] [blame] | 3997 | |
| 3998 | if (rcw > disks && rmw > disks && |
| 3999 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4000 | set_bit(STRIPE_DELAYED, &sh->state); |
| 4001 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4002 | /* now if nothing is locked, and if we have enough data, |
| 4003 | * we can start a write request |
| 4004 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4005 | /* since handle_stripe can be called at any time we need to handle the |
| 4006 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 4007 | * subsequent call wants to start a write request. raid_run_ops only |
| 4008 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4009 | * simultaneously. If this is not the case then new writes need to be |
| 4010 | * held off until the compute completes. |
| 4011 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 4012 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 4013 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4014 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 4015 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4016 | return 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4017 | } |
| 4018 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4019 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4020 | struct stripe_head_state *s, int disks) |
| 4021 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4022 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4023 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4024 | BUG_ON(sh->batch_head); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4025 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4026 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4027 | switch (sh->check_state) { |
| 4028 | case check_state_idle: |
| 4029 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4030 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4031 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4032 | sh->check_state = check_state_run; |
| 4033 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4034 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4035 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4036 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4037 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4038 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4039 | /* fall through */ |
| 4040 | case check_state_compute_result: |
| 4041 | sh->check_state = check_state_idle; |
| 4042 | if (!dev) |
| 4043 | dev = &sh->dev[sh->pd_idx]; |
| 4044 | |
| 4045 | /* check that a write has not made the stripe insync */ |
| 4046 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 4047 | break; |
| 4048 | |
| 4049 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4050 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 4051 | BUG_ON(s->uptodate != disks); |
| 4052 | |
| 4053 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4054 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4055 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4056 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4057 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4058 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4059 | break; |
| 4060 | case check_state_run: |
| 4061 | break; /* we will be called again upon completion */ |
| 4062 | case check_state_check_result: |
| 4063 | sh->check_state = check_state_idle; |
| 4064 | |
| 4065 | /* if a failure occurred during the check operation, leave |
| 4066 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 4067 | */ |
| 4068 | if (s->failed) |
| 4069 | break; |
| 4070 | |
| 4071 | /* handle a successful check operation, if parity is correct |
| 4072 | * we are done. Otherwise update the mismatch count and repair |
| 4073 | * parity if !MD_RECOVERY_CHECK |
| 4074 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 4075 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4076 | /* parity is correct (on disc, |
| 4077 | * not in buffer any more) |
| 4078 | */ |
| 4079 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4080 | else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4081 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4082 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 4083 | /* don't try to repair!! */ |
| 4084 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4085 | else { |
| 4086 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 4087 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4088 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 4089 | set_bit(R5_Wantcompute, |
| 4090 | &sh->dev[sh->pd_idx].flags); |
| 4091 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 4092 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4093 | s->uptodate++; |
| 4094 | } |
| 4095 | } |
| 4096 | break; |
| 4097 | case check_state_compute_run: |
| 4098 | break; |
| 4099 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4100 | pr_err("%s: unknown check_state: %d sector: %llu\n", |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4101 | __func__, sh->check_state, |
| 4102 | (unsigned long long) sh->sector); |
| 4103 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4104 | } |
| 4105 | } |
| 4106 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4107 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4108 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4109 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4110 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4111 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 4112 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4113 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4114 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4115 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4116 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4117 | |
| 4118 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4119 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4120 | /* Want to check and possibly repair P and Q. |
| 4121 | * However there could be one 'failed' device, in which |
| 4122 | * case we can only check one of them, possibly using the |
| 4123 | * other to generate missing data |
| 4124 | */ |
| 4125 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4126 | switch (sh->check_state) { |
| 4127 | case check_state_idle: |
| 4128 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4129 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4130 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4131 | * makes sense to check P (If anything else were failed, |
| 4132 | * we would have used P to recreate it). |
| 4133 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4134 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4135 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4136 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4137 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4138 | * anything, so it makes sense to check it |
| 4139 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4140 | if (sh->check_state == check_state_run) |
| 4141 | sh->check_state = check_state_run_pq; |
| 4142 | else |
| 4143 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4144 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4145 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4146 | /* discard potentially stale zero_sum_result */ |
| 4147 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4148 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4149 | if (sh->check_state == check_state_run) { |
| 4150 | /* async_xor_zero_sum destroys the contents of P */ |
| 4151 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 4152 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4153 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4154 | if (sh->check_state >= check_state_run && |
| 4155 | sh->check_state <= check_state_run_pq) { |
| 4156 | /* async_syndrome_zero_sum preserves P and Q, so |
| 4157 | * no need to mark them !uptodate here |
| 4158 | */ |
| 4159 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 4160 | break; |
| 4161 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4162 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4163 | /* we have 2-disk failure */ |
| 4164 | BUG_ON(s->failed != 2); |
| 4165 | /* fall through */ |
| 4166 | case check_state_compute_result: |
| 4167 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4168 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4169 | /* check that a write has not made the stripe insync */ |
| 4170 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 4171 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4172 | |
| 4173 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4174 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4175 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4176 | 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] | 4177 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4178 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4179 | s->locked++; |
| 4180 | set_bit(R5_LOCKED, &dev->flags); |
| 4181 | set_bit(R5_Wantwrite, &dev->flags); |
| 4182 | } |
| 4183 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4184 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4185 | s->locked++; |
| 4186 | set_bit(R5_LOCKED, &dev->flags); |
| 4187 | set_bit(R5_Wantwrite, &dev->flags); |
| 4188 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4189 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4190 | dev = &sh->dev[pd_idx]; |
| 4191 | s->locked++; |
| 4192 | set_bit(R5_LOCKED, &dev->flags); |
| 4193 | set_bit(R5_Wantwrite, &dev->flags); |
| 4194 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4195 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4196 | dev = &sh->dev[qd_idx]; |
| 4197 | s->locked++; |
| 4198 | set_bit(R5_LOCKED, &dev->flags); |
| 4199 | set_bit(R5_Wantwrite, &dev->flags); |
| 4200 | } |
| 4201 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 4202 | |
| 4203 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4204 | break; |
| 4205 | case check_state_run: |
| 4206 | case check_state_run_q: |
| 4207 | case check_state_run_pq: |
| 4208 | break; /* we will be called again upon completion */ |
| 4209 | case check_state_check_result: |
| 4210 | sh->check_state = check_state_idle; |
| 4211 | |
| 4212 | /* handle a successful check operation, if parity is correct |
| 4213 | * we are done. Otherwise update the mismatch count and repair |
| 4214 | * parity if !MD_RECOVERY_CHECK |
| 4215 | */ |
| 4216 | if (sh->ops.zero_sum_result == 0) { |
| 4217 | /* both parities are correct */ |
| 4218 | if (!s->failed) |
| 4219 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4220 | else { |
| 4221 | /* in contrast to the raid5 case we can validate |
| 4222 | * parity, but still have a failure to write |
| 4223 | * back |
| 4224 | */ |
| 4225 | sh->check_state = check_state_compute_result; |
| 4226 | /* Returning at this point means that we may go |
| 4227 | * off and bring p and/or q uptodate again so |
| 4228 | * we make sure to check zero_sum_result again |
| 4229 | * to verify if p or q need writeback |
| 4230 | */ |
| 4231 | } |
| 4232 | } else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4233 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4234 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 4235 | /* don't try to repair!! */ |
| 4236 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4237 | else { |
| 4238 | int *target = &sh->ops.target; |
| 4239 | |
| 4240 | sh->ops.target = -1; |
| 4241 | sh->ops.target2 = -1; |
| 4242 | sh->check_state = check_state_compute_run; |
| 4243 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 4244 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 4245 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 4246 | set_bit(R5_Wantcompute, |
| 4247 | &sh->dev[pd_idx].flags); |
| 4248 | *target = pd_idx; |
| 4249 | target = &sh->ops.target2; |
| 4250 | s->uptodate++; |
| 4251 | } |
| 4252 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 4253 | set_bit(R5_Wantcompute, |
| 4254 | &sh->dev[qd_idx].flags); |
| 4255 | *target = qd_idx; |
| 4256 | s->uptodate++; |
| 4257 | } |
| 4258 | } |
| 4259 | } |
| 4260 | break; |
| 4261 | case check_state_compute_run: |
| 4262 | break; |
| 4263 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4264 | pr_warn("%s: unknown check_state: %d sector: %llu\n", |
| 4265 | __func__, sh->check_state, |
| 4266 | (unsigned long long) sh->sector); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4267 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4268 | } |
| 4269 | } |
| 4270 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4271 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4272 | { |
| 4273 | int i; |
| 4274 | |
| 4275 | /* We have read all the blocks in this stripe and now we need to |
| 4276 | * copy some of them into a target stripe for expand. |
| 4277 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4278 | struct dma_async_tx_descriptor *tx = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4279 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4280 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4281 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 4282 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4283 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4284 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4285 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4286 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4287 | sector_t bn = raid5_compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4288 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 4289 | &dd_idx, NULL); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4290 | sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4291 | if (sh2 == NULL) |
| 4292 | /* so far only the early blocks of this stripe |
| 4293 | * have been requested. When later blocks |
| 4294 | * get requested, we will try again |
| 4295 | */ |
| 4296 | continue; |
| 4297 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 4298 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 4299 | /* must have already done this block */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4300 | raid5_release_stripe(sh2); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4301 | continue; |
| 4302 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4303 | |
| 4304 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4305 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4306 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 4307 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4308 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4309 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4310 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 4311 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 4312 | for (j = 0; j < conf->raid_disks; j++) |
| 4313 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4314 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4315 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 4316 | break; |
| 4317 | if (j == conf->raid_disks) { |
| 4318 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 4319 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 4320 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4321 | raid5_release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4322 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4323 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 4324 | /* done submitting copies, wait for them to complete */ |
NeilBrown | 749586b | 2012-11-20 14:11:15 +1100 | [diff] [blame] | 4325 | async_tx_quiesce(&tx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4326 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4327 | |
| 4328 | /* |
| 4329 | * handle_stripe - do things to a stripe. |
| 4330 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4331 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 4332 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4333 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4334 | * return some read requests which now have data |
| 4335 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4336 | * schedule a read on some buffers |
| 4337 | * schedule a write of some buffers |
| 4338 | * return confirmation of parity correctness |
| 4339 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4340 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4341 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4342 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4343 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4344 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4345 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4346 | struct r5dev *dev; |
| 4347 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4348 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4349 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4350 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4351 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4352 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head; |
| 4353 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4354 | s->failed_num[0] = -1; |
| 4355 | s->failed_num[1] = -1; |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 4356 | s->log_failed = r5l_log_disk_error(conf); |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4357 | |
| 4358 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4359 | rcu_read_lock(); |
| 4360 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4361 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4362 | sector_t first_bad; |
| 4363 | int bad_sectors; |
| 4364 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4365 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4366 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4367 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4368 | 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] | 4369 | i, dev->flags, |
| 4370 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4371 | /* maybe we can reply to a read |
| 4372 | * |
| 4373 | * new wantfill requests are only permitted while |
| 4374 | * ops_complete_biofill is guaranteed to be inactive |
| 4375 | */ |
| 4376 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 4377 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 4378 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4379 | |
| 4380 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4381 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 4382 | s->locked++; |
| 4383 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 4384 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4385 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4386 | s->compute++; |
| 4387 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4388 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4389 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4390 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4391 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4392 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4393 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4394 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4395 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4396 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4397 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4398 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4399 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4400 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4401 | /* Prefer to use the replacement for reads, but only |
| 4402 | * if it is recovered enough and has no bad blocks. |
| 4403 | */ |
| 4404 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 4405 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 4406 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 4407 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 4408 | &first_bad, &bad_sectors)) |
| 4409 | set_bit(R5_ReadRepl, &dev->flags); |
| 4410 | else { |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4411 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4412 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4413 | else |
| 4414 | clear_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4415 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 4416 | clear_bit(R5_ReadRepl, &dev->flags); |
| 4417 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 4418 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 4419 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4420 | if (rdev) { |
| 4421 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 4422 | &first_bad, &bad_sectors); |
| 4423 | if (s->blocked_rdev == NULL |
| 4424 | && (test_bit(Blocked, &rdev->flags) |
| 4425 | || is_bad < 0)) { |
| 4426 | if (is_bad < 0) |
| 4427 | set_bit(BlockedBadBlocks, |
| 4428 | &rdev->flags); |
| 4429 | s->blocked_rdev = rdev; |
| 4430 | atomic_inc(&rdev->nr_pending); |
| 4431 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4432 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4433 | clear_bit(R5_Insync, &dev->flags); |
| 4434 | if (!rdev) |
| 4435 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4436 | else if (is_bad) { |
| 4437 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 4438 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 4439 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4440 | /* treat as in-sync, but with a read error |
| 4441 | * which we can now try to correct |
| 4442 | */ |
| 4443 | set_bit(R5_Insync, &dev->flags); |
| 4444 | set_bit(R5_ReadError, &dev->flags); |
| 4445 | } |
| 4446 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4447 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 4448 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4449 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 4450 | set_bit(R5_Insync, &dev->flags); |
| 4451 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 4452 | test_bit(R5_Expanded, &dev->flags)) |
| 4453 | /* If we've reshaped into here, we assume it is Insync. |
| 4454 | * We will shortly update recovery_offset to make |
| 4455 | * it official. |
| 4456 | */ |
| 4457 | set_bit(R5_Insync, &dev->flags); |
| 4458 | |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4459 | if (test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4460 | /* This flag does not apply to '.replacement' |
| 4461 | * only to .rdev, so make sure to check that*/ |
| 4462 | struct md_rdev *rdev2 = rcu_dereference( |
| 4463 | conf->disks[i].rdev); |
| 4464 | if (rdev2 == rdev) |
| 4465 | clear_bit(R5_Insync, &dev->flags); |
| 4466 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4467 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4468 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4469 | } else |
| 4470 | clear_bit(R5_WriteError, &dev->flags); |
| 4471 | } |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4472 | if (test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4473 | /* This flag does not apply to '.replacement' |
| 4474 | * only to .rdev, so make sure to check that*/ |
| 4475 | struct md_rdev *rdev2 = rcu_dereference( |
| 4476 | conf->disks[i].rdev); |
| 4477 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4478 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4479 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4480 | } else |
| 4481 | clear_bit(R5_MadeGood, &dev->flags); |
| 4482 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4483 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4484 | struct md_rdev *rdev2 = rcu_dereference( |
| 4485 | conf->disks[i].replacement); |
| 4486 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 4487 | s->handle_bad_blocks = 1; |
| 4488 | atomic_inc(&rdev2->nr_pending); |
| 4489 | } else |
| 4490 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 4491 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4492 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4493 | /* The ReadError flag will just be confusing now */ |
| 4494 | clear_bit(R5_ReadError, &dev->flags); |
| 4495 | clear_bit(R5_ReWrite, &dev->flags); |
| 4496 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4497 | if (test_bit(R5_ReadError, &dev->flags)) |
| 4498 | clear_bit(R5_Insync, &dev->flags); |
| 4499 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4500 | if (s->failed < 2) |
| 4501 | s->failed_num[s->failed] = i; |
| 4502 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4503 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 4504 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4505 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4506 | |
| 4507 | if (test_bit(R5_InJournal, &dev->flags)) |
| 4508 | s->injournal++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4509 | if (test_bit(R5_InJournal, &dev->flags) && dev->written) |
| 4510 | s->just_cached++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4511 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4512 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 4513 | /* If there is a failed device being replaced, |
| 4514 | * we must be recovering. |
| 4515 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4516 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4517 | * else we can only be replacing |
| 4518 | * sync and recovery both need to read all devices, and so |
| 4519 | * use the same flag. |
| 4520 | */ |
| 4521 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4522 | sh->sector >= conf->mddev->recovery_cp || |
| 4523 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4524 | s->syncing = 1; |
| 4525 | else |
| 4526 | s->replacing = 1; |
| 4527 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4528 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4529 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4530 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4531 | static int clear_batch_ready(struct stripe_head *sh) |
| 4532 | { |
NeilBrown | b15a9db | 2015-05-22 15:20:04 +1000 | [diff] [blame] | 4533 | /* Return '1' if this is a member of batch, or |
| 4534 | * '0' if it is a lone stripe or a head which can now be |
| 4535 | * handled. |
| 4536 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4537 | struct stripe_head *tmp; |
| 4538 | if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state)) |
NeilBrown | b15a9db | 2015-05-22 15:20:04 +1000 | [diff] [blame] | 4539 | return (sh->batch_head && sh->batch_head != sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4540 | spin_lock(&sh->stripe_lock); |
| 4541 | if (!sh->batch_head) { |
| 4542 | spin_unlock(&sh->stripe_lock); |
| 4543 | return 0; |
| 4544 | } |
| 4545 | |
| 4546 | /* |
| 4547 | * this stripe could be added to a batch list before we check |
| 4548 | * BATCH_READY, skips it |
| 4549 | */ |
| 4550 | if (sh->batch_head != sh) { |
| 4551 | spin_unlock(&sh->stripe_lock); |
| 4552 | return 1; |
| 4553 | } |
| 4554 | spin_lock(&sh->batch_lock); |
| 4555 | list_for_each_entry(tmp, &sh->batch_list, batch_list) |
| 4556 | clear_bit(STRIPE_BATCH_READY, &tmp->state); |
| 4557 | spin_unlock(&sh->batch_lock); |
| 4558 | spin_unlock(&sh->stripe_lock); |
| 4559 | |
| 4560 | /* |
| 4561 | * BATCH_READY is cleared, no new stripes can be added. |
| 4562 | * batch_list can be accessed without lock |
| 4563 | */ |
| 4564 | return 0; |
| 4565 | } |
| 4566 | |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4567 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 4568 | unsigned long handle_flags) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4569 | { |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4570 | struct stripe_head *sh, *next; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4571 | int i; |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4572 | int do_wakeup = 0; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4573 | |
NeilBrown | bb27051 | 2015-05-08 18:19:40 +1000 | [diff] [blame] | 4574 | list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) { |
| 4575 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4576 | list_del_init(&sh->batch_list); |
| 4577 | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4578 | WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4579 | (1 << STRIPE_SYNCING) | |
| 4580 | (1 << STRIPE_REPLACED) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4581 | (1 << STRIPE_DELAYED) | |
| 4582 | (1 << STRIPE_BIT_DELAY) | |
| 4583 | (1 << STRIPE_FULL_WRITE) | |
| 4584 | (1 << STRIPE_BIOFILL_RUN) | |
| 4585 | (1 << STRIPE_COMPUTE_RUN) | |
| 4586 | (1 << STRIPE_OPS_REQ_PENDING) | |
| 4587 | (1 << STRIPE_DISCARD) | |
| 4588 | (1 << STRIPE_BATCH_READY) | |
| 4589 | (1 << STRIPE_BATCH_ERR) | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4590 | (1 << STRIPE_BITMAP_PENDING)), |
| 4591 | "stripe state: %lx\n", sh->state); |
| 4592 | WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) | |
| 4593 | (1 << STRIPE_REPLACED)), |
| 4594 | "head stripe state: %lx\n", head_sh->state); |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4595 | |
| 4596 | set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS | |
NeilBrown | 550da24 | 2016-03-09 12:58:25 +1100 | [diff] [blame] | 4597 | (1 << STRIPE_PREREAD_ACTIVE) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4598 | (1 << STRIPE_DEGRADED)), |
| 4599 | head_sh->state & (1 << STRIPE_INSYNC)); |
| 4600 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4601 | sh->check_state = head_sh->check_state; |
| 4602 | sh->reconstruct_state = head_sh->reconstruct_state; |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4603 | for (i = 0; i < sh->disks; i++) { |
| 4604 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 4605 | do_wakeup = 1; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4606 | sh->dev[i].flags = head_sh->dev[i].flags & |
| 4607 | (~((1 << R5_WriteError) | (1 << R5_Overlap))); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4608 | } |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4609 | spin_lock_irq(&sh->stripe_lock); |
| 4610 | sh->batch_head = NULL; |
| 4611 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4612 | if (handle_flags == 0 || |
| 4613 | sh->state & handle_flags) |
| 4614 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4615 | raid5_release_stripe(sh); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4616 | } |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4617 | spin_lock_irq(&head_sh->stripe_lock); |
| 4618 | head_sh->batch_head = NULL; |
| 4619 | spin_unlock_irq(&head_sh->stripe_lock); |
| 4620 | for (i = 0; i < head_sh->disks; i++) |
| 4621 | if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags)) |
| 4622 | do_wakeup = 1; |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4623 | if (head_sh->state & handle_flags) |
| 4624 | set_bit(STRIPE_HANDLE, &head_sh->state); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4625 | |
| 4626 | if (do_wakeup) |
| 4627 | wake_up(&head_sh->raid_conf->wait_for_overlap); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4628 | } |
| 4629 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4630 | static void handle_stripe(struct stripe_head *sh) |
| 4631 | { |
| 4632 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4633 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4634 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4635 | int prexor; |
| 4636 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4637 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4638 | |
| 4639 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 4640 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4641 | /* already being handled, ensure it gets handled |
| 4642 | * again when current action finishes */ |
| 4643 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4644 | return; |
| 4645 | } |
| 4646 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4647 | if (clear_batch_ready(sh) ) { |
| 4648 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
| 4649 | return; |
| 4650 | } |
| 4651 | |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4652 | if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state)) |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4653 | break_stripe_batch_list(sh, 0); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4654 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4655 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) { |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4656 | spin_lock(&sh->stripe_lock); |
| 4657 | /* Cannot process 'sync' concurrently with 'discard' */ |
| 4658 | if (!test_bit(STRIPE_DISCARD, &sh->state) && |
| 4659 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 4660 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4661 | clear_bit(STRIPE_INSYNC, &sh->state); |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4662 | clear_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4663 | } |
| 4664 | spin_unlock(&sh->stripe_lock); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4665 | } |
| 4666 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4667 | |
| 4668 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 4669 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 4670 | (unsigned long long)sh->sector, sh->state, |
| 4671 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 4672 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4673 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4674 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4675 | |
Shaohua Li | b70abcb | 2015-08-13 14:31:58 -0700 | [diff] [blame] | 4676 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) |
| 4677 | goto finish; |
| 4678 | |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 4679 | if (s.handle_bad_blocks || |
| 4680 | test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4681 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4682 | goto finish; |
| 4683 | } |
| 4684 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4685 | if (unlikely(s.blocked_rdev)) { |
| 4686 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4687 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4688 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4689 | goto finish; |
| 4690 | } |
| 4691 | /* There is nothing for the blocked_rdev to block */ |
| 4692 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 4693 | s.blocked_rdev = NULL; |
| 4694 | } |
| 4695 | |
| 4696 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 4697 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 4698 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 4699 | } |
| 4700 | |
| 4701 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 4702 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 4703 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 4704 | s.failed_num[0], s.failed_num[1]); |
| 4705 | /* check if the array has lost more than max_degraded devices and, |
| 4706 | * if so, some requests might need to be failed. |
| 4707 | */ |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 4708 | if (s.failed > conf->max_degraded || s.log_failed) { |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4709 | sh->check_state = 0; |
| 4710 | sh->reconstruct_state = 0; |
NeilBrown | 626f209 | 2015-05-22 14:03:10 +1000 | [diff] [blame] | 4711 | break_stripe_batch_list(sh, 0); |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4712 | if (s.to_read+s.to_write+s.written) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 4713 | handle_failed_stripe(conf, sh, &s, disks); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4714 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4715 | handle_failed_sync(conf, sh, &s); |
| 4716 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4717 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4718 | /* Now we check to see if any write operations have recently |
| 4719 | * completed |
| 4720 | */ |
| 4721 | prexor = 0; |
| 4722 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 4723 | prexor = 1; |
| 4724 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 4725 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 4726 | sh->reconstruct_state = reconstruct_state_idle; |
| 4727 | |
| 4728 | /* All the 'written' buffers and the parity block are ready to |
| 4729 | * be written back to disk |
| 4730 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4731 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 4732 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4733 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4734 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 4735 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4736 | for (i = disks; i--; ) { |
| 4737 | struct r5dev *dev = &sh->dev[i]; |
| 4738 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 4739 | (i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4740 | dev->written || test_bit(R5_InJournal, |
| 4741 | &dev->flags))) { |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4742 | pr_debug("Writing block %d\n", i); |
| 4743 | set_bit(R5_Wantwrite, &dev->flags); |
| 4744 | if (prexor) |
| 4745 | continue; |
NeilBrown | 9c4bdf6 | 2014-08-13 09:57:07 +1000 | [diff] [blame] | 4746 | if (s.failed > 1) |
| 4747 | continue; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4748 | if (!test_bit(R5_Insync, &dev->flags) || |
| 4749 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 4750 | s.failed == 0)) |
| 4751 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4752 | } |
| 4753 | } |
| 4754 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4755 | s.dec_preread_active = 1; |
| 4756 | } |
| 4757 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 4758 | /* |
| 4759 | * might be able to return some write requests if the parity blocks |
| 4760 | * are safe, or on a failed drive |
| 4761 | */ |
| 4762 | pdev = &sh->dev[sh->pd_idx]; |
| 4763 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 4764 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 4765 | qdev = &sh->dev[sh->qd_idx]; |
| 4766 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 4767 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 4768 | || conf->level < 6; |
| 4769 | |
| 4770 | if (s.written && |
| 4771 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 4772 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 4773 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 4774 | test_bit(R5_Discard, &pdev->flags))))) && |
| 4775 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 4776 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 4777 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 4778 | test_bit(R5_Discard, &qdev->flags)))))) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 4779 | handle_stripe_clean_event(conf, sh, disks); |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 4780 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4781 | if (s.just_cached) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame^] | 4782 | r5c_handle_cached_data_endio(conf, sh, disks); |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 4783 | log_stripe_write_finished(sh); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4784 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 4785 | /* Now we might consider reading some blocks, either to check/generate |
| 4786 | * parity, or to satisfy requests |
| 4787 | * or to load a block that is being partially written. |
| 4788 | */ |
| 4789 | if (s.to_read || s.non_overwrite |
| 4790 | || (conf->level == 6 && s.to_write && s.failed) |
| 4791 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 4792 | || s.replacing |
| 4793 | || s.expanding) |
| 4794 | handle_stripe_fill(sh, &s, disks); |
| 4795 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4796 | /* |
| 4797 | * When the stripe finishes full journal write cycle (write to journal |
| 4798 | * and raid disk), this is the clean up procedure so it is ready for |
| 4799 | * next operation. |
| 4800 | */ |
| 4801 | r5c_finish_stripe_write_out(conf, sh, &s); |
| 4802 | |
| 4803 | /* |
| 4804 | * Now to consider new write requests, cache write back and what else, |
| 4805 | * if anything should be read. We do not handle new writes when: |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4806 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 4807 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 4808 | * block. |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4809 | * 3/ A r5c cache log write is in flight. |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4810 | */ |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4811 | |
| 4812 | if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) { |
| 4813 | if (!r5c_is_writeback(conf->log)) { |
| 4814 | if (s.to_write) |
| 4815 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 4816 | } else { /* write back cache */ |
| 4817 | int ret = 0; |
| 4818 | |
| 4819 | /* First, try handle writes in caching phase */ |
| 4820 | if (s.to_write) |
| 4821 | ret = r5c_try_caching_write(conf, sh, &s, |
| 4822 | disks); |
| 4823 | /* |
| 4824 | * If caching phase failed: ret == -EAGAIN |
| 4825 | * OR |
| 4826 | * stripe under reclaim: !caching && injournal |
| 4827 | * |
| 4828 | * fall back to handle_stripe_dirtying() |
| 4829 | */ |
| 4830 | if (ret == -EAGAIN || |
| 4831 | /* stripe under reclaim: !caching && injournal */ |
| 4832 | (!test_bit(STRIPE_R5C_CACHING, &sh->state) && |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4833 | s.injournal > 0)) { |
| 4834 | ret = handle_stripe_dirtying(conf, sh, &s, |
| 4835 | disks); |
| 4836 | if (ret == -EAGAIN) |
| 4837 | goto finish; |
| 4838 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4839 | } |
| 4840 | } |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4841 | |
| 4842 | /* maybe we need to check and possibly fix the parity for this stripe |
| 4843 | * Any reads will already have been scheduled, so we just see if enough |
| 4844 | * data is available. The parity check is held off while parity |
| 4845 | * dependent operations are in flight. |
| 4846 | */ |
| 4847 | if (sh->check_state || |
| 4848 | (s.syncing && s.locked == 0 && |
| 4849 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 4850 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 4851 | if (conf->level == 6) |
| 4852 | handle_parity_checks6(conf, sh, &s, disks); |
| 4853 | else |
| 4854 | handle_parity_checks5(conf, sh, &s, disks); |
| 4855 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4856 | |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4857 | if ((s.replacing || s.syncing) && s.locked == 0 |
| 4858 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) |
| 4859 | && !test_bit(STRIPE_REPLACED, &sh->state)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4860 | /* Write out to replacement devices where possible */ |
| 4861 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4862 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 4863 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4864 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 4865 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 4866 | s.locked++; |
| 4867 | } |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4868 | if (s.replacing) |
| 4869 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4870 | set_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4871 | } |
| 4872 | if ((s.syncing || s.replacing) && s.locked == 0 && |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4873 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4874 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4875 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 4876 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4877 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 4878 | wake_up(&conf->wait_for_overlap); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4879 | } |
| 4880 | |
| 4881 | /* If the failed drives are just a ReadError, then we might need |
| 4882 | * to progress the repair/check process |
| 4883 | */ |
| 4884 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 4885 | for (i = 0; i < s.failed; i++) { |
| 4886 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 4887 | if (test_bit(R5_ReadError, &dev->flags) |
| 4888 | && !test_bit(R5_LOCKED, &dev->flags) |
| 4889 | && test_bit(R5_UPTODATE, &dev->flags) |
| 4890 | ) { |
| 4891 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 4892 | set_bit(R5_Wantwrite, &dev->flags); |
| 4893 | set_bit(R5_ReWrite, &dev->flags); |
| 4894 | set_bit(R5_LOCKED, &dev->flags); |
| 4895 | s.locked++; |
| 4896 | } else { |
| 4897 | /* let's read it back */ |
| 4898 | set_bit(R5_Wantread, &dev->flags); |
| 4899 | set_bit(R5_LOCKED, &dev->flags); |
| 4900 | s.locked++; |
| 4901 | } |
| 4902 | } |
| 4903 | } |
| 4904 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4905 | /* Finish reconstruct operations initiated by the expansion process */ |
| 4906 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 4907 | struct stripe_head *sh_src |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4908 | = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4909 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 4910 | /* sh cannot be written until sh_src has been read. |
| 4911 | * so arrange for sh to be delayed a little |
| 4912 | */ |
| 4913 | set_bit(STRIPE_DELAYED, &sh->state); |
| 4914 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4915 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 4916 | &sh_src->state)) |
| 4917 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4918 | raid5_release_stripe(sh_src); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4919 | goto finish; |
| 4920 | } |
| 4921 | if (sh_src) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4922 | raid5_release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4923 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4924 | sh->reconstruct_state = reconstruct_state_idle; |
| 4925 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 4926 | for (i = conf->raid_disks; i--; ) { |
| 4927 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 4928 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 4929 | s.locked++; |
| 4930 | } |
| 4931 | } |
| 4932 | |
| 4933 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 4934 | !sh->reconstruct_state) { |
| 4935 | /* Need to write out all blocks after computing parity */ |
| 4936 | sh->disks = conf->raid_disks; |
| 4937 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 4938 | schedule_reconstruction(sh, &s, 1, 1); |
| 4939 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 4940 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4941 | atomic_dec(&conf->reshape_stripes); |
| 4942 | wake_up(&conf->wait_for_overlap); |
| 4943 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 4944 | } |
| 4945 | |
| 4946 | if (s.expanding && s.locked == 0 && |
| 4947 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 4948 | handle_stripe_expansion(conf, sh); |
| 4949 | |
| 4950 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4951 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 4952 | if (unlikely(s.blocked_rdev)) { |
| 4953 | if (conf->mddev->external) |
| 4954 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 4955 | conf->mddev); |
| 4956 | else |
| 4957 | /* Internal metadata will immediately |
| 4958 | * be written by raid5d, so we don't |
| 4959 | * need to wait here. |
| 4960 | */ |
| 4961 | rdev_dec_pending(s.blocked_rdev, |
| 4962 | conf->mddev); |
| 4963 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4964 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4965 | if (s.handle_bad_blocks) |
| 4966 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4967 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4968 | struct r5dev *dev = &sh->dev[i]; |
| 4969 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 4970 | /* We own a safe reference to the rdev */ |
| 4971 | rdev = conf->disks[i].rdev; |
| 4972 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 4973 | STRIPE_SECTORS, 0)) |
| 4974 | md_error(conf->mddev, rdev); |
| 4975 | rdev_dec_pending(rdev, conf->mddev); |
| 4976 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4977 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 4978 | rdev = conf->disks[i].rdev; |
| 4979 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4980 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4981 | rdev_dec_pending(rdev, conf->mddev); |
| 4982 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4983 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4984 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4985 | if (!rdev) |
| 4986 | /* rdev have been moved down */ |
| 4987 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4988 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4989 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4990 | rdev_dec_pending(rdev, conf->mddev); |
| 4991 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4992 | } |
| 4993 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4994 | if (s.ops_request) |
| 4995 | raid_run_ops(sh, s.ops_request); |
| 4996 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 4997 | ops_run_io(sh, &s); |
| 4998 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 4999 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5000 | /* 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] | 5001 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5002 | * have actually been submitted. |
| 5003 | */ |
| 5004 | atomic_dec(&conf->preread_active_stripes); |
| 5005 | if (atomic_read(&conf->preread_active_stripes) < |
| 5006 | IO_THRESHOLD) |
| 5007 | md_wakeup_thread(conf->mddev->thread); |
| 5008 | } |
| 5009 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 5010 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5011 | } |
| 5012 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5013 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5014 | { |
| 5015 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 5016 | while (!list_empty(&conf->delayed_list)) { |
| 5017 | struct list_head *l = conf->delayed_list.next; |
| 5018 | struct stripe_head *sh; |
| 5019 | sh = list_entry(l, struct stripe_head, lru); |
| 5020 | list_del_init(l); |
| 5021 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 5022 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5023 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5024 | list_add_tail(&sh->lru, &conf->hold_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5025 | raid5_wakeup_stripe_thread(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5026 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 5027 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5028 | } |
| 5029 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5030 | static void activate_bit_delay(struct r5conf *conf, |
| 5031 | struct list_head *temp_inactive_list) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5032 | { |
| 5033 | /* device_lock is held */ |
| 5034 | struct list_head head; |
| 5035 | list_add(&head, &conf->bitmap_list); |
| 5036 | list_del_init(&conf->bitmap_list); |
| 5037 | while (!list_empty(&head)) { |
| 5038 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5039 | int hash; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5040 | list_del_init(&sh->lru); |
| 5041 | atomic_inc(&sh->count); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5042 | hash = sh->hash_lock_index; |
| 5043 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5044 | } |
| 5045 | } |
| 5046 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 5047 | static int raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5048 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5049 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5050 | |
| 5051 | /* No difference between reads and writes. Just check |
| 5052 | * how busy the stripe_cache is |
| 5053 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 5054 | |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 5055 | if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5056 | return 1; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 5057 | |
| 5058 | /* Also checks whether there is pressure on r5cache log space */ |
| 5059 | if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) |
| 5060 | return 1; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5061 | if (conf->quiesce) |
| 5062 | return 1; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5063 | if (atomic_read(&conf->empty_inactive_list_nr)) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 5064 | return 1; |
| 5065 | |
| 5066 | return 0; |
| 5067 | } |
| 5068 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5069 | 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] | 5070 | { |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5071 | struct r5conf *conf = mddev->private; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5072 | sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev); |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5073 | unsigned int chunk_sectors; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5074 | unsigned int bio_sectors = bio_sectors(bio); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5075 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5076 | chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5077 | return chunk_sectors >= |
| 5078 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 5079 | } |
| 5080 | |
| 5081 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5082 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 5083 | * later sampled by raid5d. |
| 5084 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5085 | 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] | 5086 | { |
| 5087 | unsigned long flags; |
| 5088 | |
| 5089 | spin_lock_irqsave(&conf->device_lock, flags); |
| 5090 | |
| 5091 | bi->bi_next = conf->retry_read_aligned_list; |
| 5092 | conf->retry_read_aligned_list = bi; |
| 5093 | |
| 5094 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5095 | md_wakeup_thread(conf->mddev->thread); |
| 5096 | } |
| 5097 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5098 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5099 | { |
| 5100 | struct bio *bi; |
| 5101 | |
| 5102 | bi = conf->retry_read_aligned; |
| 5103 | if (bi) { |
| 5104 | conf->retry_read_aligned = NULL; |
| 5105 | return bi; |
| 5106 | } |
| 5107 | bi = conf->retry_read_aligned_list; |
| 5108 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5109 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5110 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 5111 | /* |
| 5112 | * this sets the active strip count to 1 and the processed |
| 5113 | * strip count to zero (upper 8 bits) |
| 5114 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5115 | 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] | 5116 | } |
| 5117 | |
| 5118 | return bi; |
| 5119 | } |
| 5120 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5121 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5122 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 5123 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 5124 | * first). |
| 5125 | * If the read failed.. |
| 5126 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5127 | static void raid5_align_endio(struct bio *bi) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5128 | { |
| 5129 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5130 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5131 | struct r5conf *conf; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5132 | struct md_rdev *rdev; |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 5133 | int error = bi->bi_error; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5134 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5135 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5136 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5137 | rdev = (void*)raid_bi->bi_next; |
| 5138 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 5139 | mddev = rdev->mddev; |
| 5140 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5141 | |
| 5142 | rdev_dec_pending(rdev, conf->mddev); |
| 5143 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 5144 | if (!error) { |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5145 | trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev), |
| 5146 | raid_bi, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5147 | bio_endio(raid_bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5148 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 5149 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 5150 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5151 | } |
| 5152 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5153 | 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] | 5154 | |
| 5155 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5156 | } |
| 5157 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5158 | static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5159 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5160 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 5161 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5162 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5163 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5164 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5165 | |
| 5166 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5167 | pr_debug("%s: non aligned\n", __func__); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5168 | return 0; |
| 5169 | } |
| 5170 | /* |
Ming Lei | d7a1030 | 2017-02-14 23:29:03 +0800 | [diff] [blame] | 5171 | * use bio_clone_fast to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5172 | */ |
Ming Lei | d7a1030 | 2017-02-14 23:29:03 +0800 | [diff] [blame] | 5173 | align_bi = bio_clone_fast(raid_bio, GFP_NOIO, mddev->bio_set); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5174 | if (!align_bi) |
| 5175 | return 0; |
| 5176 | /* |
| 5177 | * set bi_end_io to a new function, and set bi_private to the |
| 5178 | * original bio. |
| 5179 | */ |
| 5180 | align_bi->bi_end_io = raid5_align_endio; |
| 5181 | align_bi->bi_private = raid_bio; |
| 5182 | /* |
| 5183 | * compute position |
| 5184 | */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5185 | align_bi->bi_iter.bi_sector = |
| 5186 | raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, |
| 5187 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5188 | |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5189 | end_sector = bio_end_sector(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5190 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5191 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 5192 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 5193 | rdev->recovery_offset < end_sector) { |
| 5194 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 5195 | if (rdev && |
| 5196 | (test_bit(Faulty, &rdev->flags) || |
| 5197 | !(test_bit(In_sync, &rdev->flags) || |
| 5198 | rdev->recovery_offset >= end_sector))) |
| 5199 | rdev = NULL; |
| 5200 | } |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 5201 | |
| 5202 | if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) { |
| 5203 | rcu_read_unlock(); |
| 5204 | bio_put(align_bi); |
| 5205 | return 0; |
| 5206 | } |
| 5207 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5208 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5209 | sector_t first_bad; |
| 5210 | int bad_sectors; |
| 5211 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5212 | atomic_inc(&rdev->nr_pending); |
| 5213 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5214 | raid_bio->bi_next = (void*)rdev; |
| 5215 | align_bi->bi_bdev = rdev->bdev; |
Jens Axboe | b7c44ed | 2015-07-24 12:37:59 -0600 | [diff] [blame] | 5216 | bio_clear_flag(align_bi, BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5217 | |
Kent Overstreet | 7140aaf | 2013-09-25 13:37:01 -0700 | [diff] [blame] | 5218 | if (is_badblock(rdev, align_bi->bi_iter.bi_sector, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5219 | bio_sectors(align_bi), |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5220 | &first_bad, &bad_sectors)) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5221 | bio_put(align_bi); |
| 5222 | rdev_dec_pending(rdev, mddev); |
| 5223 | return 0; |
| 5224 | } |
| 5225 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 5226 | /* No reshape active, so we can trust rdev->data_offset */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5227 | align_bi->bi_iter.bi_sector += rdev->data_offset; |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 5228 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5229 | spin_lock_irq(&conf->device_lock); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 5230 | wait_event_lock_irq(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5231 | conf->quiesce == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 5232 | conf->device_lock); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5233 | atomic_inc(&conf->active_aligned_reads); |
| 5234 | spin_unlock_irq(&conf->device_lock); |
| 5235 | |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5236 | if (mddev->gendisk) |
| 5237 | trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), |
| 5238 | align_bi, disk_devt(mddev->gendisk), |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5239 | raid_bio->bi_iter.bi_sector); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5240 | generic_make_request(align_bi); |
| 5241 | return 1; |
| 5242 | } else { |
| 5243 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5244 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5245 | return 0; |
| 5246 | } |
| 5247 | } |
| 5248 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5249 | static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio) |
| 5250 | { |
| 5251 | struct bio *split; |
| 5252 | |
| 5253 | do { |
| 5254 | sector_t sector = raid_bio->bi_iter.bi_sector; |
| 5255 | unsigned chunk_sects = mddev->chunk_sectors; |
| 5256 | unsigned sectors = chunk_sects - (sector & (chunk_sects-1)); |
| 5257 | |
| 5258 | if (sectors < bio_sectors(raid_bio)) { |
| 5259 | split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set); |
| 5260 | bio_chain(split, raid_bio); |
| 5261 | } else |
| 5262 | split = raid_bio; |
| 5263 | |
| 5264 | if (!raid5_read_one_chunk(mddev, split)) { |
| 5265 | if (split != raid_bio) |
| 5266 | generic_make_request(raid_bio); |
| 5267 | return split; |
| 5268 | } |
| 5269 | } while (split != raid_bio); |
| 5270 | |
| 5271 | return NULL; |
| 5272 | } |
| 5273 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5274 | /* __get_priority_stripe - get the next stripe to process |
| 5275 | * |
| 5276 | * Full stripe writes are allowed to pass preread active stripes up until |
| 5277 | * the bypass_threshold is exceeded. In general the bypass_count |
| 5278 | * increments when the handle_list is handled before the hold_list; however, it |
| 5279 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 5280 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 5281 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 5282 | * handle_list. |
| 5283 | */ |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5284 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5285 | { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5286 | struct stripe_head *sh, *tmp; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5287 | struct list_head *handle_list = NULL; |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5288 | struct r5worker_group *wg; |
| 5289 | bool second_try = !r5c_is_writeback(conf->log); |
| 5290 | bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5291 | |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5292 | again: |
| 5293 | wg = NULL; |
| 5294 | sh = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5295 | if (conf->worker_cnt_per_group == 0) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5296 | handle_list = try_loprio ? &conf->loprio_list : |
| 5297 | &conf->handle_list; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5298 | } else if (group != ANY_GROUP) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5299 | handle_list = try_loprio ? &conf->worker_groups[group].loprio_list : |
| 5300 | &conf->worker_groups[group].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5301 | wg = &conf->worker_groups[group]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5302 | } else { |
| 5303 | int i; |
| 5304 | for (i = 0; i < conf->group_cnt; i++) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5305 | handle_list = try_loprio ? &conf->worker_groups[i].loprio_list : |
| 5306 | &conf->worker_groups[i].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5307 | wg = &conf->worker_groups[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5308 | if (!list_empty(handle_list)) |
| 5309 | break; |
| 5310 | } |
| 5311 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5312 | |
| 5313 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 5314 | __func__, |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5315 | list_empty(handle_list) ? "empty" : "busy", |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5316 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 5317 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 5318 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5319 | if (!list_empty(handle_list)) { |
| 5320 | sh = list_entry(handle_list->next, typeof(*sh), lru); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5321 | |
| 5322 | if (list_empty(&conf->hold_list)) |
| 5323 | conf->bypass_count = 0; |
| 5324 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 5325 | if (conf->hold_list.next == conf->last_hold) |
| 5326 | conf->bypass_count++; |
| 5327 | else { |
| 5328 | conf->last_hold = conf->hold_list.next; |
| 5329 | conf->bypass_count -= conf->bypass_threshold; |
| 5330 | if (conf->bypass_count < 0) |
| 5331 | conf->bypass_count = 0; |
| 5332 | } |
| 5333 | } |
| 5334 | } else if (!list_empty(&conf->hold_list) && |
| 5335 | ((conf->bypass_threshold && |
| 5336 | conf->bypass_count > conf->bypass_threshold) || |
| 5337 | atomic_read(&conf->pending_full_writes) == 0)) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5338 | |
| 5339 | list_for_each_entry(tmp, &conf->hold_list, lru) { |
| 5340 | if (conf->worker_cnt_per_group == 0 || |
| 5341 | group == ANY_GROUP || |
| 5342 | !cpu_online(tmp->cpu) || |
| 5343 | cpu_to_group(tmp->cpu) == group) { |
| 5344 | sh = tmp; |
| 5345 | break; |
| 5346 | } |
| 5347 | } |
| 5348 | |
| 5349 | if (sh) { |
| 5350 | conf->bypass_count -= conf->bypass_threshold; |
| 5351 | if (conf->bypass_count < 0) |
| 5352 | conf->bypass_count = 0; |
| 5353 | } |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5354 | wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5355 | } |
| 5356 | |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5357 | if (!sh) { |
| 5358 | if (second_try) |
| 5359 | return NULL; |
| 5360 | second_try = true; |
| 5361 | try_loprio = !try_loprio; |
| 5362 | goto again; |
| 5363 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5364 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5365 | if (wg) { |
| 5366 | wg->stripes_cnt--; |
| 5367 | sh->group = NULL; |
| 5368 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5369 | list_del_init(&sh->lru); |
Shaohua Li | c7a6d35 | 2014-04-15 09:12:54 +0800 | [diff] [blame] | 5370 | BUG_ON(atomic_inc_return(&sh->count) != 1); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5371 | return sh; |
| 5372 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5373 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5374 | struct raid5_plug_cb { |
| 5375 | struct blk_plug_cb cb; |
| 5376 | struct list_head list; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5377 | struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5378 | }; |
| 5379 | |
| 5380 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 5381 | { |
| 5382 | struct raid5_plug_cb *cb = container_of( |
| 5383 | blk_cb, struct raid5_plug_cb, cb); |
| 5384 | struct stripe_head *sh; |
| 5385 | struct mddev *mddev = cb->cb.data; |
| 5386 | struct r5conf *conf = mddev->private; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5387 | int cnt = 0; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5388 | int hash; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5389 | |
| 5390 | if (cb->list.next && !list_empty(&cb->list)) { |
| 5391 | spin_lock_irq(&conf->device_lock); |
| 5392 | while (!list_empty(&cb->list)) { |
| 5393 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 5394 | list_del_init(&sh->lru); |
| 5395 | /* |
| 5396 | * avoid race release_stripe_plug() sees |
| 5397 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 5398 | * is still in our list |
| 5399 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 5400 | smp_mb__before_atomic(); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5401 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5402 | /* |
| 5403 | * STRIPE_ON_RELEASE_LIST could be set here. In that |
| 5404 | * case, the count is always > 1 here |
| 5405 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5406 | hash = sh->hash_lock_index; |
| 5407 | __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5408 | cnt++; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5409 | } |
| 5410 | spin_unlock_irq(&conf->device_lock); |
| 5411 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5412 | release_inactive_stripe_list(conf, cb->temp_inactive_list, |
| 5413 | NR_STRIPE_HASH_LOCKS); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5414 | if (mddev->queue) |
| 5415 | trace_block_unplug(mddev->queue, cnt, !from_schedule); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5416 | kfree(cb); |
| 5417 | } |
| 5418 | |
| 5419 | static void release_stripe_plug(struct mddev *mddev, |
| 5420 | struct stripe_head *sh) |
| 5421 | { |
| 5422 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 5423 | raid5_unplug, mddev, |
| 5424 | sizeof(struct raid5_plug_cb)); |
| 5425 | struct raid5_plug_cb *cb; |
| 5426 | |
| 5427 | if (!blk_cb) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5428 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5429 | return; |
| 5430 | } |
| 5431 | |
| 5432 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 5433 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5434 | if (cb->list.next == NULL) { |
| 5435 | int i; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5436 | INIT_LIST_HEAD(&cb->list); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5437 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5438 | INIT_LIST_HEAD(cb->temp_inactive_list + i); |
| 5439 | } |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5440 | |
| 5441 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 5442 | list_add_tail(&sh->lru, &cb->list); |
| 5443 | else |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5444 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5445 | } |
| 5446 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5447 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 5448 | { |
| 5449 | struct r5conf *conf = mddev->private; |
| 5450 | sector_t logical_sector, last_sector; |
| 5451 | struct stripe_head *sh; |
| 5452 | int remaining; |
| 5453 | int stripe_sectors; |
| 5454 | |
| 5455 | if (mddev->reshape_position != MaxSector) |
| 5456 | /* Skip discard while reshape is happening */ |
| 5457 | return; |
| 5458 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5459 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 5460 | 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] | 5461 | |
| 5462 | bi->bi_next = NULL; |
| 5463 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5464 | md_write_start(mddev, bi); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5465 | |
| 5466 | stripe_sectors = conf->chunk_sectors * |
| 5467 | (conf->raid_disks - conf->max_degraded); |
| 5468 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 5469 | stripe_sectors); |
| 5470 | sector_div(last_sector, stripe_sectors); |
| 5471 | |
| 5472 | logical_sector *= conf->chunk_sectors; |
| 5473 | last_sector *= conf->chunk_sectors; |
| 5474 | |
| 5475 | for (; logical_sector < last_sector; |
| 5476 | logical_sector += STRIPE_SECTORS) { |
| 5477 | DEFINE_WAIT(w); |
| 5478 | int d; |
| 5479 | again: |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5480 | sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5481 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5482 | TASK_UNINTERRUPTIBLE); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5483 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
| 5484 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5485 | raid5_release_stripe(sh); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5486 | schedule(); |
| 5487 | goto again; |
| 5488 | } |
| 5489 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5490 | spin_lock_irq(&sh->stripe_lock); |
| 5491 | for (d = 0; d < conf->raid_disks; d++) { |
| 5492 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5493 | continue; |
| 5494 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 5495 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 5496 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5497 | raid5_release_stripe(sh); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5498 | schedule(); |
| 5499 | goto again; |
| 5500 | } |
| 5501 | } |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5502 | set_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5503 | finish_wait(&conf->wait_for_overlap, &w); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5504 | sh->overwrite_disks = 0; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5505 | for (d = 0; d < conf->raid_disks; d++) { |
| 5506 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5507 | continue; |
| 5508 | sh->dev[d].towrite = bi; |
| 5509 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
| 5510 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5511 | md_write_inc(mddev, bi); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5512 | sh->overwrite_disks++; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5513 | } |
| 5514 | spin_unlock_irq(&sh->stripe_lock); |
| 5515 | if (conf->mddev->bitmap) { |
| 5516 | for (d = 0; |
| 5517 | d < conf->raid_disks - conf->max_degraded; |
| 5518 | d++) |
| 5519 | bitmap_startwrite(mddev->bitmap, |
| 5520 | sh->sector, |
| 5521 | STRIPE_SECTORS, |
| 5522 | 0); |
| 5523 | sh->bm_seq = conf->seq_flush + 1; |
| 5524 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 5525 | } |
| 5526 | |
| 5527 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5528 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 5529 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5530 | atomic_inc(&conf->preread_active_stripes); |
| 5531 | release_stripe_plug(mddev, sh); |
| 5532 | } |
| 5533 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5534 | md_write_end(mddev); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5535 | remaining = raid5_dec_bi_active_stripes(bi); |
| 5536 | if (remaining == 0) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5537 | bio_endio(bi); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5538 | } |
| 5539 | } |
| 5540 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5541 | static void raid5_make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5542 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5543 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5544 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5545 | sector_t new_sector; |
| 5546 | sector_t logical_sector, last_sector; |
| 5547 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5548 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 5549 | int remaining; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5550 | DEFINE_WAIT(w); |
| 5551 | bool do_prepare; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5552 | bool do_flush = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5553 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5554 | if (unlikely(bi->bi_opf & REQ_PREFLUSH)) { |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5555 | int ret = r5l_handle_flush_request(conf->log, bi); |
| 5556 | |
| 5557 | if (ret == 0) |
| 5558 | return; |
| 5559 | if (ret == -ENODEV) { |
| 5560 | md_flush_request(mddev, bi); |
| 5561 | return; |
| 5562 | } |
| 5563 | /* ret == -EAGAIN, fallback */ |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5564 | /* |
| 5565 | * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH, |
| 5566 | * we need to flush journal device |
| 5567 | */ |
| 5568 | do_flush = bi->bi_opf & REQ_PREFLUSH; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 5569 | } |
| 5570 | |
Eric Mei | 9ffc8f7 | 2015-03-18 23:39:11 -0600 | [diff] [blame] | 5571 | /* |
| 5572 | * If array is degraded, better not do chunk aligned read because |
| 5573 | * later we might have to read it again in order to reconstruct |
| 5574 | * data on failed drives. |
| 5575 | */ |
| 5576 | if (rw == READ && mddev->degraded == 0 && |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5577 | mddev->reshape_position == MaxSector) { |
| 5578 | bi = chunk_aligned_read(mddev, bi); |
| 5579 | if (!bi) |
| 5580 | return; |
| 5581 | } |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 5582 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 5583 | if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) { |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5584 | make_discard_request(mddev, bi); |
| 5585 | return; |
| 5586 | } |
| 5587 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5588 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5589 | last_sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5590 | bi->bi_next = NULL; |
| 5591 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5592 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5593 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5594 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5595 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5596 | int previous; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5597 | int seq; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5598 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5599 | do_prepare = false; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5600 | retry: |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5601 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5602 | previous = 0; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5603 | if (do_prepare) |
| 5604 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5605 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5606 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5607 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5608 | * 64bit on a 32bit platform, and so it might be |
| 5609 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 5610 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5611 | * the lock is dropped, so once we get a reference |
| 5612 | * to the stripe that we think it is, we will have |
| 5613 | * to check again. |
| 5614 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5615 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5616 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5617 | ? logical_sector < conf->reshape_progress |
| 5618 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5619 | previous = 1; |
| 5620 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5621 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5622 | ? logical_sector < conf->reshape_safe |
| 5623 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5624 | spin_unlock_irq(&conf->device_lock); |
| 5625 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5626 | do_prepare = true; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5627 | goto retry; |
| 5628 | } |
| 5629 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5630 | spin_unlock_irq(&conf->device_lock); |
| 5631 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5632 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5633 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 5634 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5635 | &dd_idx, NULL); |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5636 | pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n", |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5637 | (unsigned long long)new_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5638 | (unsigned long long)logical_sector); |
| 5639 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5640 | sh = raid5_get_active_stripe(conf, new_sector, previous, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5641 | (bi->bi_opf & REQ_RAHEAD), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5642 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5643 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5644 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5645 | * stripe, so we must do the range check again. |
| 5646 | * Expansion could still move past after this |
| 5647 | * test, but as we are holding a reference to |
| 5648 | * 'sh', we know that if that happens, |
| 5649 | * STRIPE_EXPANDING will get set and the expansion |
| 5650 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5651 | */ |
| 5652 | int must_retry = 0; |
| 5653 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5654 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5655 | ? logical_sector >= conf->reshape_progress |
| 5656 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5657 | /* mismatch, need to try again */ |
| 5658 | must_retry = 1; |
| 5659 | spin_unlock_irq(&conf->device_lock); |
| 5660 | if (must_retry) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5661 | raid5_release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 5662 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5663 | do_prepare = true; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5664 | goto retry; |
| 5665 | } |
| 5666 | } |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5667 | if (read_seqcount_retry(&conf->gen_lock, seq)) { |
| 5668 | /* Might have got the wrong stripe_head |
| 5669 | * by accident |
| 5670 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5671 | raid5_release_stripe(sh); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5672 | goto retry; |
| 5673 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5674 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 5675 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5676 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5677 | logical_sector < mddev->suspend_hi) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5678 | raid5_release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5679 | /* As the suspend_* range is controlled by |
| 5680 | * userspace, we want an interruptible |
| 5681 | * wait. |
| 5682 | */ |
| 5683 | flush_signals(current); |
| 5684 | prepare_to_wait(&conf->wait_for_overlap, |
| 5685 | &w, TASK_INTERRUPTIBLE); |
| 5686 | if (logical_sector >= mddev->suspend_lo && |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5687 | logical_sector < mddev->suspend_hi) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5688 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5689 | do_prepare = true; |
| 5690 | } |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5691 | goto retry; |
| 5692 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5693 | |
| 5694 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5695 | !add_stripe_bio(sh, bi, dd_idx, rw, previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5696 | /* Stripe is busy expanding or |
| 5697 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5698 | * and wait a while |
| 5699 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 5700 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5701 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5702 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5703 | do_prepare = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5704 | goto retry; |
| 5705 | } |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5706 | if (do_flush) { |
| 5707 | set_bit(STRIPE_R5C_PREFLUSH, &sh->state); |
| 5708 | /* we only need flush for one stripe */ |
| 5709 | do_flush = false; |
| 5710 | } |
| 5711 | |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 5712 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5713 | clear_bit(STRIPE_DELAYED, &sh->state); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5714 | if ((!sh->batch_head || sh == sh->batch_head) && |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5715 | (bi->bi_opf & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5716 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5717 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5718 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5719 | } else { |
| 5720 | /* cannot get stripe for read-ahead, just give-up */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5721 | bi->bi_error = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | break; |
| 5723 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5724 | } |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5725 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5726 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5727 | if (rw == WRITE) |
| 5728 | md_write_end(mddev); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5729 | remaining = raid5_dec_bi_active_stripes(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 5730 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5731 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 5732 | |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5733 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 5734 | bi, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5735 | bio_endio(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5736 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5737 | } |
| 5738 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5739 | 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] | 5740 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5741 | 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] | 5742 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5743 | /* reshaping is quite different to recovery/resync so it is |
| 5744 | * handled quite separately ... here. |
| 5745 | * |
| 5746 | * On each call to sync_request, we gather one chunk worth of |
| 5747 | * destination stripes and flag them as expanding. |
| 5748 | * Then we find all the source stripes and request reads. |
| 5749 | * As the reads complete, handle_stripe will copy the data |
| 5750 | * into the destination stripe and release that stripe. |
| 5751 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5752 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5754 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5755 | int raid_disks = conf->previous_raid_disks; |
| 5756 | int data_disks = raid_disks - conf->max_degraded; |
| 5757 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5758 | int i; |
| 5759 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5760 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5761 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5762 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5763 | struct list_head stripes; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5764 | sector_t retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5765 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5766 | if (sector_nr == 0) { |
| 5767 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5768 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5769 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 5770 | sector_nr = raid5_size(mddev, 0, 0) |
| 5771 | - conf->reshape_progress; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 5772 | } else if (mddev->reshape_backwards && |
| 5773 | conf->reshape_progress == MaxSector) { |
| 5774 | /* shouldn't happen, but just in case, finish up.*/ |
| 5775 | sector_nr = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5776 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5777 | conf->reshape_progress > 0) |
| 5778 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5779 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5780 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 5781 | mddev->curr_resync_completed = sector_nr; |
| 5782 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5783 | *skipped = 1; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5784 | retn = sector_nr; |
| 5785 | goto finish; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5786 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5787 | } |
| 5788 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5789 | /* We need to process a full chunk at a time. |
| 5790 | * If old and new chunk sizes differ, we need to process the |
| 5791 | * largest of these |
| 5792 | */ |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5793 | |
| 5794 | reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5795 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5796 | /* We update the metadata at least every 10 seconds, or when |
| 5797 | * the data about to be copied would over-write the source of |
| 5798 | * the data at the front of the range. i.e. one new_stripe |
| 5799 | * along from reshape_progress new_maps to after where |
| 5800 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5801 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5802 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5803 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5804 | readpos = conf->reshape_progress; |
| 5805 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5806 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5807 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5808 | if (mddev->reshape_backwards) { |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 5809 | BUG_ON(writepos < reshape_sectors); |
| 5810 | writepos -= reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5811 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5812 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5813 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5814 | writepos += reshape_sectors; |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 5815 | /* readpos and safepos are worst-case calculations. |
| 5816 | * A negative number is overly pessimistic, and causes |
| 5817 | * obvious problems for unsigned storage. So clip to 0. |
| 5818 | */ |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 5819 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 5820 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5821 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5822 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5823 | /* Having calculated the 'writepos' possibly use it |
| 5824 | * to set 'stripe_addr' which is where we will write to. |
| 5825 | */ |
| 5826 | if (mddev->reshape_backwards) { |
| 5827 | BUG_ON(conf->reshape_progress == 0); |
| 5828 | stripe_addr = writepos; |
| 5829 | BUG_ON((mddev->dev_sectors & |
| 5830 | ~((sector_t)reshape_sectors - 1)) |
| 5831 | - reshape_sectors - stripe_addr |
| 5832 | != sector_nr); |
| 5833 | } else { |
| 5834 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 5835 | stripe_addr = sector_nr; |
| 5836 | } |
| 5837 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5838 | /* 'writepos' is the most advanced device address we might write. |
| 5839 | * 'readpos' is the least advanced device address we might read. |
| 5840 | * 'safepos' is the least address recorded in the metadata as having |
| 5841 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5842 | * If there is a min_offset_diff, these are adjusted either by |
| 5843 | * increasing the safepos/readpos if diff is negative, or |
| 5844 | * increasing writepos if diff is positive. |
| 5845 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5846 | * ensure safety in the face of a crash - that must be done by userspace |
| 5847 | * making a backup of the data. So in that case there is no particular |
| 5848 | * rush to update metadata. |
| 5849 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 5850 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 5851 | * we can be safe in the event of a crash. |
| 5852 | * So we insist on updating metadata if safepos is behind writepos and |
| 5853 | * readpos is beyond writepos. |
| 5854 | * In any case, update the metadata every 10 seconds. |
| 5855 | * Maybe that number should be configurable, but I'm not sure it is |
| 5856 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 5857 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5858 | if (conf->min_offset_diff < 0) { |
| 5859 | safepos += -conf->min_offset_diff; |
| 5860 | readpos += -conf->min_offset_diff; |
| 5861 | } else |
| 5862 | writepos += conf->min_offset_diff; |
| 5863 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5864 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5865 | ? (safepos > writepos && readpos < writepos) |
| 5866 | : (safepos < writepos && readpos > writepos)) || |
| 5867 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5868 | /* Cannot proceed until we've updated the superblock... */ |
| 5869 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5870 | atomic_read(&conf->reshape_stripes)==0 |
| 5871 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5872 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 5873 | return 0; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5874 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5875 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5876 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5877 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5878 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5879 | wait_event(mddev->sb_wait, mddev->sb_flags == 0 || |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5880 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5881 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5882 | return 0; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5883 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5884 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5885 | spin_unlock_irq(&conf->device_lock); |
| 5886 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5887 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5888 | } |
| 5889 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5890 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5891 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5892 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5893 | int skipped_disk = 0; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5894 | sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5895 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 5896 | atomic_inc(&conf->reshape_stripes); |
| 5897 | /* If any of this stripe is beyond the end of the old |
| 5898 | * array, then we need to zero those blocks |
| 5899 | */ |
| 5900 | for (j=sh->disks; j--;) { |
| 5901 | sector_t s; |
| 5902 | if (j == sh->pd_idx) |
| 5903 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5904 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5905 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5906 | continue; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5907 | s = raid5_compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5908 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5909 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5910 | continue; |
| 5911 | } |
| 5912 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 5913 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 5914 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 5915 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5916 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5917 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 5918 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5919 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5920 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5921 | } |
| 5922 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5923 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5924 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5925 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5926 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5927 | spin_unlock_irq(&conf->device_lock); |
| 5928 | /* Ok, those stripe are ready. We can start scheduling |
| 5929 | * reads on the source stripes. |
| 5930 | * The source stripes are determined by mapping the first and last |
| 5931 | * block on the destination stripes. |
| 5932 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5933 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5934 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5935 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5936 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 5937 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5938 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5939 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5940 | if (last_sector >= mddev->dev_sectors) |
| 5941 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5942 | while (first_sector <= last_sector) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5943 | sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5944 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 5945 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5946 | raid5_release_stripe(sh); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5947 | first_sector += STRIPE_SECTORS; |
| 5948 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5949 | /* Now that the sources are clearly marked, we can release |
| 5950 | * the destination stripes |
| 5951 | */ |
| 5952 | while (!list_empty(&stripes)) { |
| 5953 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 5954 | list_del_init(&sh->lru); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5955 | raid5_release_stripe(sh); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5956 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5957 | /* If this takes us to the resync_max point where we have to pause, |
| 5958 | * then we need to write out the superblock. |
| 5959 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5960 | sector_nr += reshape_sectors; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5961 | retn = reshape_sectors; |
| 5962 | finish: |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 5963 | if (mddev->curr_resync_completed > mddev->resync_max || |
| 5964 | (sector_nr - mddev->curr_resync_completed) * 2 |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 5965 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5966 | /* Cannot proceed until we've updated the superblock... */ |
| 5967 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5968 | atomic_read(&conf->reshape_stripes) == 0 |
| 5969 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5970 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 5971 | goto ret; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5972 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5973 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5974 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5975 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5976 | md_wakeup_thread(mddev->thread); |
| 5977 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5978 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5979 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5980 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5981 | goto ret; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5982 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5983 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5984 | spin_unlock_irq(&conf->device_lock); |
| 5985 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5986 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5987 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5988 | ret: |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5989 | return retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5990 | } |
| 5991 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5992 | static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr, |
| 5993 | int *skipped) |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5994 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5995 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5996 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5997 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 5998 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5999 | int still_degraded = 0; |
| 6000 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6001 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6002 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6003 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6004 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6005 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 6006 | end_reshape(conf); |
| 6007 | return 0; |
| 6008 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6009 | |
| 6010 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 6011 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 6012 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6013 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6014 | conf->fullsync = 0; |
| 6015 | bitmap_close_sync(mddev->bitmap); |
| 6016 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | return 0; |
| 6018 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6019 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6020 | /* Allow raid5_quiesce to complete */ |
| 6021 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 6022 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6023 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6024 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6025 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6026 | /* No need to check resync_max as we never do more than one |
| 6027 | * stripe, and as resync_max will always be on a chunk boundary, |
| 6028 | * if the check in md_do_sync didn't fire, there is no chance |
| 6029 | * of overstepping resync_max here |
| 6030 | */ |
| 6031 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6032 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6033 | * to resync, then assert that we are finished, because there is |
| 6034 | * nothing we can do. |
| 6035 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 6036 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6037 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6038 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6039 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6040 | return rv; |
| 6041 | } |
majianpeng | 6f60804 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6042 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 6043 | !conf->fullsync && |
| 6044 | !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
| 6045 | sync_blocks >= STRIPE_SECTORS) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6046 | /* we can skip this block, and probably more */ |
| 6047 | sync_blocks /= STRIPE_SECTORS; |
| 6048 | *skipped = 1; |
| 6049 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 6050 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6051 | |
Goldwyn Rodrigues | c40f341 | 2015-08-19 08:14:42 +1000 | [diff] [blame] | 6052 | bitmap_cond_end_sync(mddev->bitmap, sector_nr, false); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 6053 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6054 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6055 | if (sh == NULL) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6056 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6057 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6058 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6059 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 6060 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6061 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6062 | /* Need to check if array will still be degraded after recovery/resync |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6063 | * Note in case of > 1 drive failures it's possible we're rebuilding |
| 6064 | * one drive while leaving another faulty drive in array. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6065 | */ |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6066 | rcu_read_lock(); |
| 6067 | for (i = 0; i < conf->raid_disks; i++) { |
| 6068 | struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev); |
| 6069 | |
| 6070 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6071 | still_degraded = 1; |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6072 | } |
| 6073 | rcu_read_unlock(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6074 | |
| 6075 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 6076 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 6077 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Eivind Sarto | 053f5b6 | 2014-06-09 17:06:19 -0700 | [diff] [blame] | 6078 | set_bit(STRIPE_HANDLE, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6079 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6080 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6081 | |
| 6082 | return STRIPE_SECTORS; |
| 6083 | } |
| 6084 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6085 | 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] | 6086 | { |
| 6087 | /* We may not be able to submit a whole bio at once as there |
| 6088 | * may not be enough stripe_heads available. |
| 6089 | * We cannot pre-allocate enough stripe_heads as we may need |
| 6090 | * more than exist in the cache (if we allow ever large chunks). |
| 6091 | * So we do one stripe head at a time and record in |
| 6092 | * ->bi_hw_segments how many have been done. |
| 6093 | * |
| 6094 | * We *know* that this entire raid_bio is in one chunk, so |
| 6095 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 6096 | */ |
| 6097 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6098 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6099 | sector_t sector, logical_sector, last_sector; |
| 6100 | int scnt = 0; |
| 6101 | int remaining; |
| 6102 | int handled = 0; |
| 6103 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6104 | logical_sector = raid_bio->bi_iter.bi_sector & |
| 6105 | ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6106 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6107 | 0, &dd_idx, NULL); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 6108 | last_sector = bio_end_sector(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6109 | |
| 6110 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6111 | logical_sector += STRIPE_SECTORS, |
| 6112 | sector += STRIPE_SECTORS, |
| 6113 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6114 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 6115 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6116 | /* already done this stripe */ |
| 6117 | continue; |
| 6118 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6119 | sh = raid5_get_active_stripe(conf, sector, 0, 1, 1); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6120 | |
| 6121 | if (!sh) { |
| 6122 | /* failed to get a stripe - must wait */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 6123 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6124 | conf->retry_read_aligned = raid_bio; |
| 6125 | return handled; |
| 6126 | } |
| 6127 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 6128 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6129 | raid5_release_stripe(sh); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 6130 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6131 | conf->retry_read_aligned = raid_bio; |
| 6132 | return handled; |
| 6133 | } |
| 6134 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 6135 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6136 | handle_stripe(sh); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6137 | raid5_release_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6138 | handled++; |
| 6139 | } |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 6140 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 6141 | if (remaining == 0) { |
| 6142 | trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev), |
| 6143 | raid_bio, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6144 | bio_endio(raid_bio); |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 6145 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6146 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 6147 | wake_up(&conf->wait_for_quiescent); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6148 | return handled; |
| 6149 | } |
| 6150 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 6151 | static int handle_active_stripes(struct r5conf *conf, int group, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6152 | struct r5worker *worker, |
| 6153 | struct list_head *temp_inactive_list) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6154 | { |
| 6155 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6156 | int i, batch_size = 0, hash; |
| 6157 | bool release_inactive = false; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6158 | |
| 6159 | while (batch_size < MAX_STRIPE_BATCH && |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6160 | (sh = __get_priority_stripe(conf, group)) != NULL) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6161 | batch[batch_size++] = sh; |
| 6162 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6163 | if (batch_size == 0) { |
| 6164 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6165 | if (!list_empty(temp_inactive_list + i)) |
| 6166 | break; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6167 | if (i == NR_STRIPE_HASH_LOCKS) { |
| 6168 | spin_unlock_irq(&conf->device_lock); |
| 6169 | r5l_flush_stripe_to_raid(conf->log); |
| 6170 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6171 | return batch_size; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6172 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6173 | release_inactive = true; |
| 6174 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6175 | spin_unlock_irq(&conf->device_lock); |
| 6176 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6177 | release_inactive_stripe_list(conf, temp_inactive_list, |
| 6178 | NR_STRIPE_HASH_LOCKS); |
| 6179 | |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6180 | r5l_flush_stripe_to_raid(conf->log); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6181 | if (release_inactive) { |
| 6182 | spin_lock_irq(&conf->device_lock); |
| 6183 | return 0; |
| 6184 | } |
| 6185 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6186 | for (i = 0; i < batch_size; i++) |
| 6187 | handle_stripe(batch[i]); |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 6188 | log_write_stripe_run(conf); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6189 | |
| 6190 | cond_resched(); |
| 6191 | |
| 6192 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6193 | for (i = 0; i < batch_size; i++) { |
| 6194 | hash = batch[i]->hash_lock_index; |
| 6195 | __release_stripe(conf, batch[i], &temp_inactive_list[hash]); |
| 6196 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6197 | return batch_size; |
| 6198 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6199 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6200 | static void raid5_do_work(struct work_struct *work) |
| 6201 | { |
| 6202 | struct r5worker *worker = container_of(work, struct r5worker, work); |
| 6203 | struct r5worker_group *group = worker->group; |
| 6204 | struct r5conf *conf = group->conf; |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 6205 | struct mddev *mddev = conf->mddev; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6206 | int group_id = group - conf->worker_groups; |
| 6207 | int handled; |
| 6208 | struct blk_plug plug; |
| 6209 | |
| 6210 | pr_debug("+++ raid5worker active\n"); |
| 6211 | |
| 6212 | blk_start_plug(&plug); |
| 6213 | handled = 0; |
| 6214 | spin_lock_irq(&conf->device_lock); |
| 6215 | while (1) { |
| 6216 | int batch_size, released; |
| 6217 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6218 | released = release_stripe_list(conf, worker->temp_inactive_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6219 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6220 | batch_size = handle_active_stripes(conf, group_id, worker, |
| 6221 | worker->temp_inactive_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 6222 | worker->working = false; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6223 | if (!batch_size && !released) |
| 6224 | break; |
| 6225 | handled += batch_size; |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 6226 | wait_event_lock_irq(mddev->sb_wait, |
| 6227 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags), |
| 6228 | conf->device_lock); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6229 | } |
| 6230 | pr_debug("%d stripes handled\n", handled); |
| 6231 | |
| 6232 | spin_unlock_irq(&conf->device_lock); |
| 6233 | blk_finish_plug(&plug); |
| 6234 | |
| 6235 | pr_debug("--- raid5worker inactive\n"); |
| 6236 | } |
| 6237 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | /* |
| 6239 | * This is our raid5 kernel thread. |
| 6240 | * |
| 6241 | * We scan the hash table for stripes which can be handled now. |
| 6242 | * During the scan, completed stripes are saved for us by the interrupt |
| 6243 | * handler, so that they will not have to wait for our next wakeup. |
| 6244 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6245 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6247 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6248 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6249 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6250 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6251 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6252 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6253 | |
| 6254 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6255 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6256 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6257 | handled = 0; |
| 6258 | spin_lock_irq(&conf->device_lock); |
| 6259 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6260 | struct bio *bio; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6261 | int batch_size, released; |
| 6262 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6263 | released = release_stripe_list(conf, conf->temp_inactive_list); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6264 | if (released) |
| 6265 | clear_bit(R5_DID_ALLOC, &conf->cache_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6266 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 6267 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 6268 | !list_empty(&conf->bitmap_list)) { |
| 6269 | /* Now is a good time to flush some bitmap updates */ |
| 6270 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 6271 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6272 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 6273 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 6274 | conf->seq_write = conf->seq_flush; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6275 | activate_bit_delay(conf, conf->temp_inactive_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6276 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 6277 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6278 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6279 | while ((bio = remove_bio_from_retry(conf))) { |
| 6280 | int ok; |
| 6281 | spin_unlock_irq(&conf->device_lock); |
| 6282 | ok = retry_aligned_read(conf, bio); |
| 6283 | spin_lock_irq(&conf->device_lock); |
| 6284 | if (!ok) |
| 6285 | break; |
| 6286 | handled++; |
| 6287 | } |
| 6288 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6289 | batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, |
| 6290 | conf->temp_inactive_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6291 | if (!batch_size && !released) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6292 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6293 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6294 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6295 | if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) { |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6296 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6297 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6298 | spin_lock_irq(&conf->device_lock); |
| 6299 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6300 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6301 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6302 | |
| 6303 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6304 | if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) && |
| 6305 | mutex_trylock(&conf->cache_size_mutex)) { |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6306 | grow_one_stripe(conf, __GFP_NOWARN); |
| 6307 | /* Set flag even if allocation failed. This helps |
| 6308 | * slow down allocation requests when mem is short |
| 6309 | */ |
| 6310 | set_bit(R5_DID_ALLOC, &conf->cache_state); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6311 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6312 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6313 | |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 6314 | flush_deferred_bios(conf); |
| 6315 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 6316 | r5l_flush_stripe_to_raid(conf->log); |
| 6317 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 6318 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6319 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6320 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6321 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6322 | } |
| 6323 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6324 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6325 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6326 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6327 | struct r5conf *conf; |
| 6328 | int ret = 0; |
| 6329 | spin_lock(&mddev->lock); |
| 6330 | conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6331 | if (conf) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6332 | ret = sprintf(page, "%d\n", conf->min_nr_stripes); |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6333 | spin_unlock(&mddev->lock); |
| 6334 | return ret; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6335 | } |
| 6336 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6337 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6338 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6339 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6340 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6341 | int err; |
| 6342 | |
| 6343 | if (size <= 16 || size > 32768) |
| 6344 | return -EINVAL; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6345 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6346 | conf->min_nr_stripes = size; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6347 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6348 | while (size < conf->max_nr_stripes && |
| 6349 | drop_one_stripe(conf)) |
| 6350 | ; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6351 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6352 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6353 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6354 | err = md_allow_write(mddev); |
| 6355 | if (err) |
| 6356 | return err; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6357 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6358 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6359 | while (size > conf->max_nr_stripes) |
| 6360 | if (!grow_one_stripe(conf, GFP_KERNEL)) |
| 6361 | break; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6362 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6363 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6364 | return 0; |
| 6365 | } |
| 6366 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 6367 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6368 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6369 | 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] | 6370 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6371 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6372 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6373 | int err; |
| 6374 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6375 | if (len >= PAGE_SIZE) |
| 6376 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6377 | if (kstrtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6378 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6379 | err = mddev_lock(mddev); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6380 | if (err) |
| 6381 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6382 | conf = mddev->private; |
| 6383 | if (!conf) |
| 6384 | err = -ENODEV; |
| 6385 | else |
| 6386 | err = raid5_set_cache_size(mddev, new); |
| 6387 | mddev_unlock(mddev); |
| 6388 | |
| 6389 | return err ?: len; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6390 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6391 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6392 | static struct md_sysfs_entry |
| 6393 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 6394 | raid5_show_stripe_cache_size, |
| 6395 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6396 | |
| 6397 | static ssize_t |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6398 | raid5_show_rmw_level(struct mddev *mddev, char *page) |
| 6399 | { |
| 6400 | struct r5conf *conf = mddev->private; |
| 6401 | if (conf) |
| 6402 | return sprintf(page, "%d\n", conf->rmw_level); |
| 6403 | else |
| 6404 | return 0; |
| 6405 | } |
| 6406 | |
| 6407 | static ssize_t |
| 6408 | raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len) |
| 6409 | { |
| 6410 | struct r5conf *conf = mddev->private; |
| 6411 | unsigned long new; |
| 6412 | |
| 6413 | if (!conf) |
| 6414 | return -ENODEV; |
| 6415 | |
| 6416 | if (len >= PAGE_SIZE) |
| 6417 | return -EINVAL; |
| 6418 | |
| 6419 | if (kstrtoul(page, 10, &new)) |
| 6420 | return -EINVAL; |
| 6421 | |
| 6422 | if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome) |
| 6423 | return -EINVAL; |
| 6424 | |
| 6425 | if (new != PARITY_DISABLE_RMW && |
| 6426 | new != PARITY_ENABLE_RMW && |
| 6427 | new != PARITY_PREFER_RMW) |
| 6428 | return -EINVAL; |
| 6429 | |
| 6430 | conf->rmw_level = new; |
| 6431 | return len; |
| 6432 | } |
| 6433 | |
| 6434 | static struct md_sysfs_entry |
| 6435 | raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR, |
| 6436 | raid5_show_rmw_level, |
| 6437 | raid5_store_rmw_level); |
| 6438 | |
| 6439 | |
| 6440 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6441 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6442 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6443 | struct r5conf *conf; |
| 6444 | int ret = 0; |
| 6445 | spin_lock(&mddev->lock); |
| 6446 | conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6447 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6448 | ret = sprintf(page, "%d\n", conf->bypass_threshold); |
| 6449 | spin_unlock(&mddev->lock); |
| 6450 | return ret; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6451 | } |
| 6452 | |
| 6453 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6454 | 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] | 6455 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6456 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6457 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6458 | int err; |
| 6459 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6460 | if (len >= PAGE_SIZE) |
| 6461 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6462 | if (kstrtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6463 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6464 | |
| 6465 | err = mddev_lock(mddev); |
| 6466 | if (err) |
| 6467 | return err; |
| 6468 | conf = mddev->private; |
| 6469 | if (!conf) |
| 6470 | err = -ENODEV; |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6471 | else if (new > conf->min_nr_stripes) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6472 | err = -EINVAL; |
| 6473 | else |
| 6474 | conf->bypass_threshold = new; |
| 6475 | mddev_unlock(mddev); |
| 6476 | return err ?: len; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6477 | } |
| 6478 | |
| 6479 | static struct md_sysfs_entry |
| 6480 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 6481 | S_IRUGO | S_IWUSR, |
| 6482 | raid5_show_preread_threshold, |
| 6483 | raid5_store_preread_threshold); |
| 6484 | |
| 6485 | static ssize_t |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6486 | raid5_show_skip_copy(struct mddev *mddev, char *page) |
| 6487 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6488 | struct r5conf *conf; |
| 6489 | int ret = 0; |
| 6490 | spin_lock(&mddev->lock); |
| 6491 | conf = mddev->private; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6492 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6493 | ret = sprintf(page, "%d\n", conf->skip_copy); |
| 6494 | spin_unlock(&mddev->lock); |
| 6495 | return ret; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6496 | } |
| 6497 | |
| 6498 | static ssize_t |
| 6499 | raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) |
| 6500 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6501 | struct r5conf *conf; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6502 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6503 | int err; |
| 6504 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6505 | if (len >= PAGE_SIZE) |
| 6506 | return -EINVAL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6507 | if (kstrtoul(page, 10, &new)) |
| 6508 | return -EINVAL; |
| 6509 | new = !!new; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6510 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6511 | err = mddev_lock(mddev); |
| 6512 | if (err) |
| 6513 | return err; |
| 6514 | conf = mddev->private; |
| 6515 | if (!conf) |
| 6516 | err = -ENODEV; |
| 6517 | else if (new != conf->skip_copy) { |
| 6518 | mddev_suspend(mddev); |
| 6519 | conf->skip_copy = new; |
| 6520 | if (new) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 6521 | mddev->queue->backing_dev_info->capabilities |= |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6522 | BDI_CAP_STABLE_WRITES; |
| 6523 | else |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 6524 | mddev->queue->backing_dev_info->capabilities &= |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6525 | ~BDI_CAP_STABLE_WRITES; |
| 6526 | mddev_resume(mddev); |
| 6527 | } |
| 6528 | mddev_unlock(mddev); |
| 6529 | return err ?: len; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6530 | } |
| 6531 | |
| 6532 | static struct md_sysfs_entry |
| 6533 | raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, |
| 6534 | raid5_show_skip_copy, |
| 6535 | raid5_store_skip_copy); |
| 6536 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6537 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6538 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6539 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6540 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6541 | if (conf) |
| 6542 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 6543 | else |
| 6544 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6545 | } |
| 6546 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6547 | static struct md_sysfs_entry |
| 6548 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6549 | |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6550 | static ssize_t |
| 6551 | raid5_show_group_thread_cnt(struct mddev *mddev, char *page) |
| 6552 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6553 | struct r5conf *conf; |
| 6554 | int ret = 0; |
| 6555 | spin_lock(&mddev->lock); |
| 6556 | conf = mddev->private; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6557 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6558 | ret = sprintf(page, "%d\n", conf->worker_cnt_per_group); |
| 6559 | spin_unlock(&mddev->lock); |
| 6560 | return ret; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6561 | } |
| 6562 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6563 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 6564 | int *group_cnt, |
| 6565 | int *worker_cnt_per_group, |
| 6566 | struct r5worker_group **worker_groups); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6567 | static ssize_t |
| 6568 | raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) |
| 6569 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6570 | struct r5conf *conf; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6571 | unsigned long new; |
| 6572 | int err; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6573 | struct r5worker_group *new_groups, *old_groups; |
| 6574 | int group_cnt, worker_cnt_per_group; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6575 | |
| 6576 | if (len >= PAGE_SIZE) |
| 6577 | return -EINVAL; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6578 | if (kstrtoul(page, 10, &new)) |
| 6579 | return -EINVAL; |
| 6580 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6581 | err = mddev_lock(mddev); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6582 | if (err) |
| 6583 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6584 | conf = mddev->private; |
| 6585 | if (!conf) |
| 6586 | err = -ENODEV; |
| 6587 | else if (new != conf->worker_cnt_per_group) { |
| 6588 | mddev_suspend(mddev); |
| 6589 | |
| 6590 | old_groups = conf->worker_groups; |
| 6591 | if (old_groups) |
| 6592 | flush_workqueue(raid5_wq); |
| 6593 | |
| 6594 | err = alloc_thread_groups(conf, new, |
| 6595 | &group_cnt, &worker_cnt_per_group, |
| 6596 | &new_groups); |
| 6597 | if (!err) { |
| 6598 | spin_lock_irq(&conf->device_lock); |
| 6599 | conf->group_cnt = group_cnt; |
| 6600 | conf->worker_cnt_per_group = worker_cnt_per_group; |
| 6601 | conf->worker_groups = new_groups; |
| 6602 | spin_unlock_irq(&conf->device_lock); |
| 6603 | |
| 6604 | if (old_groups) |
| 6605 | kfree(old_groups[0].workers); |
| 6606 | kfree(old_groups); |
| 6607 | } |
| 6608 | mddev_resume(mddev); |
| 6609 | } |
| 6610 | mddev_unlock(mddev); |
| 6611 | |
| 6612 | return err ?: len; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6613 | } |
| 6614 | |
| 6615 | static struct md_sysfs_entry |
| 6616 | raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, |
| 6617 | raid5_show_group_thread_cnt, |
| 6618 | raid5_store_group_thread_cnt); |
| 6619 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6620 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6621 | &raid5_stripecache_size.attr, |
| 6622 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6623 | &raid5_preread_bypass_threshold.attr, |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6624 | &raid5_group_thread_cnt.attr, |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6625 | &raid5_skip_copy.attr, |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6626 | &raid5_rmw_level.attr, |
Song Liu | 2c7da14 | 2016-11-17 15:24:41 -0800 | [diff] [blame] | 6627 | &r5c_journal_mode.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6628 | NULL, |
| 6629 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6630 | static struct attribute_group raid5_attrs_group = { |
| 6631 | .name = NULL, |
| 6632 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6633 | }; |
| 6634 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6635 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 6636 | int *group_cnt, |
| 6637 | int *worker_cnt_per_group, |
| 6638 | struct r5worker_group **worker_groups) |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6639 | { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6640 | int i, j, k; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6641 | ssize_t size; |
| 6642 | struct r5worker *workers; |
| 6643 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6644 | *worker_cnt_per_group = cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6645 | if (cnt == 0) { |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6646 | *group_cnt = 0; |
| 6647 | *worker_groups = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6648 | return 0; |
| 6649 | } |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6650 | *group_cnt = num_possible_nodes(); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6651 | size = sizeof(struct r5worker) * cnt; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6652 | workers = kzalloc(size * *group_cnt, GFP_NOIO); |
| 6653 | *worker_groups = kzalloc(sizeof(struct r5worker_group) * |
| 6654 | *group_cnt, GFP_NOIO); |
| 6655 | if (!*worker_groups || !workers) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6656 | kfree(workers); |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6657 | kfree(*worker_groups); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6658 | return -ENOMEM; |
| 6659 | } |
| 6660 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6661 | for (i = 0; i < *group_cnt; i++) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6662 | struct r5worker_group *group; |
| 6663 | |
NeilBrown | 0c775d5 | 2013-11-25 11:12:43 +1100 | [diff] [blame] | 6664 | group = &(*worker_groups)[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6665 | INIT_LIST_HEAD(&group->handle_list); |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 6666 | INIT_LIST_HEAD(&group->loprio_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6667 | group->conf = conf; |
| 6668 | group->workers = workers + i * cnt; |
| 6669 | |
| 6670 | for (j = 0; j < cnt; j++) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6671 | struct r5worker *worker = group->workers + j; |
| 6672 | worker->group = group; |
| 6673 | INIT_WORK(&worker->work, raid5_do_work); |
| 6674 | |
| 6675 | for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) |
| 6676 | INIT_LIST_HEAD(worker->temp_inactive_list + k); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6677 | } |
| 6678 | } |
| 6679 | |
| 6680 | return 0; |
| 6681 | } |
| 6682 | |
| 6683 | static void free_thread_groups(struct r5conf *conf) |
| 6684 | { |
| 6685 | if (conf->worker_groups) |
| 6686 | kfree(conf->worker_groups[0].workers); |
| 6687 | kfree(conf->worker_groups); |
| 6688 | conf->worker_groups = NULL; |
| 6689 | } |
| 6690 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6691 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6692 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6693 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6694 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6695 | |
| 6696 | if (!sectors) |
| 6697 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6698 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 6699 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6700 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6701 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 6702 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
| 6703 | sectors &= ~((sector_t)conf->prev_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6704 | return sectors * (raid_disks - conf->max_degraded); |
| 6705 | } |
| 6706 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6707 | static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 6708 | { |
| 6709 | safe_put_page(percpu->spare_page); |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6710 | if (percpu->scribble) |
| 6711 | flex_array_free(percpu->scribble); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6712 | percpu->spare_page = NULL; |
| 6713 | percpu->scribble = NULL; |
| 6714 | } |
| 6715 | |
| 6716 | static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 6717 | { |
| 6718 | if (conf->level == 6 && !percpu->spare_page) |
| 6719 | percpu->spare_page = alloc_page(GFP_KERNEL); |
| 6720 | if (!percpu->scribble) |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6721 | percpu->scribble = scribble_alloc(max(conf->raid_disks, |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 6722 | conf->previous_raid_disks), |
| 6723 | max(conf->chunk_sectors, |
| 6724 | conf->prev_chunk_sectors) |
| 6725 | / STRIPE_SECTORS, |
| 6726 | GFP_KERNEL); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6727 | |
| 6728 | if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) { |
| 6729 | free_scratch_buffer(conf, percpu); |
| 6730 | return -ENOMEM; |
| 6731 | } |
| 6732 | |
| 6733 | return 0; |
| 6734 | } |
| 6735 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6736 | static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node) |
| 6737 | { |
| 6738 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
| 6739 | |
| 6740 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); |
| 6741 | return 0; |
| 6742 | } |
| 6743 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6744 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6745 | { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6746 | if (!conf->percpu) |
| 6747 | return; |
| 6748 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6749 | cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6750 | free_percpu(conf->percpu); |
| 6751 | } |
| 6752 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6753 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6754 | { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 6755 | int i; |
| 6756 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 6757 | log_exit(conf); |
| 6758 | |
Shaohua Li | 30c8946 | 2016-09-21 09:07:13 -0700 | [diff] [blame] | 6759 | if (conf->shrinker.nr_deferred) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6760 | unregister_shrinker(&conf->shrinker); |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 6761 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6762 | free_thread_groups(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6763 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6764 | raid5_free_percpu(conf); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 6765 | for (i = 0; i < conf->pool_size; i++) |
| 6766 | if (conf->disks[i].extra_page) |
| 6767 | put_page(conf->disks[i].extra_page); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6768 | kfree(conf->disks); |
| 6769 | kfree(conf->stripe_hashtbl); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 6770 | kfree(conf->pending_data); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6771 | kfree(conf); |
| 6772 | } |
| 6773 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6774 | static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6775 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6776 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6777 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 6778 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6779 | if (alloc_scratch_buffer(conf, percpu)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6780 | pr_warn("%s: failed memory allocation for cpu%u\n", |
| 6781 | __func__, cpu); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6782 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6783 | } |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6784 | return 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6785 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6786 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6787 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6788 | { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6789 | int err = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6790 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6791 | conf->percpu = alloc_percpu(struct raid5_percpu); |
| 6792 | if (!conf->percpu) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6793 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6794 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6795 | err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 6796 | if (!err) { |
| 6797 | conf->scribble_disks = max(conf->raid_disks, |
| 6798 | conf->previous_raid_disks); |
| 6799 | conf->scribble_sectors = max(conf->chunk_sectors, |
| 6800 | conf->prev_chunk_sectors); |
| 6801 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6802 | return err; |
| 6803 | } |
| 6804 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6805 | static unsigned long raid5_cache_scan(struct shrinker *shrink, |
| 6806 | struct shrink_control *sc) |
| 6807 | { |
| 6808 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6809 | unsigned long ret = SHRINK_STOP; |
| 6810 | |
| 6811 | if (mutex_trylock(&conf->cache_size_mutex)) { |
| 6812 | ret= 0; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 6813 | while (ret < sc->nr_to_scan && |
| 6814 | conf->max_nr_stripes > conf->min_nr_stripes) { |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6815 | if (drop_one_stripe(conf) == 0) { |
| 6816 | ret = SHRINK_STOP; |
| 6817 | break; |
| 6818 | } |
| 6819 | ret++; |
| 6820 | } |
| 6821 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6822 | } |
| 6823 | return ret; |
| 6824 | } |
| 6825 | |
| 6826 | static unsigned long raid5_cache_count(struct shrinker *shrink, |
| 6827 | struct shrink_control *sc) |
| 6828 | { |
| 6829 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
| 6830 | |
| 6831 | if (conf->max_nr_stripes < conf->min_nr_stripes) |
| 6832 | /* unlikely, but not impossible */ |
| 6833 | return 0; |
| 6834 | return conf->max_nr_stripes - conf->min_nr_stripes; |
| 6835 | } |
| 6836 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6837 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6838 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6839 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6840 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6841 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6842 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6843 | char pers_name[6]; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6844 | int i; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6845 | int group_cnt, worker_cnt_per_group; |
| 6846 | struct r5worker_group *new_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6847 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6848 | if (mddev->new_level != 5 |
| 6849 | && mddev->new_level != 4 |
| 6850 | && mddev->new_level != 6) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6851 | pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
| 6852 | mdname(mddev), mddev->new_level); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6853 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6854 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6855 | if ((mddev->new_level == 5 |
| 6856 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 6857 | (mddev->new_level == 6 |
| 6858 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6859 | pr_warn("md/raid:%s: layout %d not supported\n", |
| 6860 | mdname(mddev), mddev->new_layout); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6861 | return ERR_PTR(-EIO); |
| 6862 | } |
| 6863 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6864 | pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
| 6865 | mdname(mddev), mddev->raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6866 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6867 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6868 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6869 | if (!mddev->new_chunk_sectors || |
| 6870 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 6871 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6872 | pr_warn("md/raid:%s: invalid chunk size %d\n", |
| 6873 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6874 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6875 | } |
| 6876 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6877 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6878 | if (conf == NULL) |
| 6879 | goto abort; |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 6880 | INIT_LIST_HEAD(&conf->free_list); |
| 6881 | INIT_LIST_HEAD(&conf->pending_list); |
| 6882 | conf->pending_data = kzalloc(sizeof(struct r5pending_data) * |
| 6883 | PENDING_IO_MAX, GFP_KERNEL); |
| 6884 | if (!conf->pending_data) |
| 6885 | goto abort; |
| 6886 | for (i = 0; i < PENDING_IO_MAX; i++) |
| 6887 | list_add(&conf->pending_data[i].sibling, &conf->free_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6888 | /* Don't enable multi-threading by default*/ |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6889 | if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group, |
| 6890 | &new_group)) { |
| 6891 | conf->group_cnt = group_cnt; |
| 6892 | conf->worker_cnt_per_group = worker_cnt_per_group; |
| 6893 | conf->worker_groups = new_group; |
| 6894 | } else |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6895 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6896 | spin_lock_init(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 6897 | seqcount_init(&conf->gen_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6898 | mutex_init(&conf->cache_size_mutex); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 6899 | init_waitqueue_head(&conf->wait_for_quiescent); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 6900 | init_waitqueue_head(&conf->wait_for_stripe); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6901 | init_waitqueue_head(&conf->wait_for_overlap); |
| 6902 | INIT_LIST_HEAD(&conf->handle_list); |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 6903 | INIT_LIST_HEAD(&conf->loprio_list); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6904 | INIT_LIST_HEAD(&conf->hold_list); |
| 6905 | INIT_LIST_HEAD(&conf->delayed_list); |
| 6906 | INIT_LIST_HEAD(&conf->bitmap_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6907 | init_llist_head(&conf->released_stripes); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6908 | atomic_set(&conf->active_stripes, 0); |
| 6909 | atomic_set(&conf->preread_active_stripes, 0); |
| 6910 | atomic_set(&conf->active_aligned_reads, 0); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 6911 | spin_lock_init(&conf->pending_bios_lock); |
| 6912 | conf->batch_bio_dispatch = true; |
| 6913 | rdev_for_each(rdev, mddev) { |
| 6914 | if (test_bit(Journal, &rdev->flags)) |
| 6915 | continue; |
| 6916 | if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 6917 | conf->batch_bio_dispatch = false; |
| 6918 | break; |
| 6919 | } |
| 6920 | } |
| 6921 | |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6922 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 6923 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6924 | |
| 6925 | conf->raid_disks = mddev->raid_disks; |
| 6926 | if (mddev->reshape_position == MaxSector) |
| 6927 | conf->previous_raid_disks = mddev->raid_disks; |
| 6928 | else |
| 6929 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6930 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6931 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6932 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6933 | GFP_KERNEL); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 6934 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6935 | if (!conf->disks) |
| 6936 | goto abort; |
| 6937 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 6938 | for (i = 0; i < max_disks; i++) { |
| 6939 | conf->disks[i].extra_page = alloc_page(GFP_KERNEL); |
| 6940 | if (!conf->disks[i].extra_page) |
| 6941 | goto abort; |
| 6942 | } |
| 6943 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6944 | conf->mddev = mddev; |
| 6945 | |
| 6946 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 6947 | goto abort; |
| 6948 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6949 | /* We init hash_locks[0] separately to that it can be used |
| 6950 | * as the reference lock in the spin_lock_nest_lock() call |
| 6951 | * in lock_all_device_hash_locks_irq in order to convince |
| 6952 | * lockdep that we know what we are doing. |
| 6953 | */ |
| 6954 | spin_lock_init(conf->hash_locks); |
| 6955 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6956 | spin_lock_init(conf->hash_locks + i); |
| 6957 | |
| 6958 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6959 | INIT_LIST_HEAD(conf->inactive_list + i); |
| 6960 | |
| 6961 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6962 | INIT_LIST_HEAD(conf->temp_inactive_list + i); |
| 6963 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 6964 | atomic_set(&conf->r5c_cached_full_stripes, 0); |
| 6965 | INIT_LIST_HEAD(&conf->r5c_full_stripe_list); |
| 6966 | atomic_set(&conf->r5c_cached_partial_stripes, 0); |
| 6967 | INIT_LIST_HEAD(&conf->r5c_partial_stripe_list); |
Shaohua Li | e33fbb9 | 2017-02-10 16:18:09 -0800 | [diff] [blame] | 6968 | atomic_set(&conf->r5c_flushing_full_stripes, 0); |
| 6969 | atomic_set(&conf->r5c_flushing_partial_stripes, 0); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 6970 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6971 | conf->level = mddev->new_level; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6972 | conf->chunk_sectors = mddev->new_chunk_sectors; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6973 | if (raid5_alloc_percpu(conf) != 0) |
| 6974 | goto abort; |
| 6975 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6976 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6977 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6978 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6979 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6980 | if (raid_disk >= max_disks |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 6981 | || raid_disk < 0 || test_bit(Journal, &rdev->flags)) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6982 | continue; |
| 6983 | disk = conf->disks + raid_disk; |
| 6984 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6985 | if (test_bit(Replacement, &rdev->flags)) { |
| 6986 | if (disk->replacement) |
| 6987 | goto abort; |
| 6988 | disk->replacement = rdev; |
| 6989 | } else { |
| 6990 | if (disk->rdev) |
| 6991 | goto abort; |
| 6992 | disk->rdev = rdev; |
| 6993 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6994 | |
| 6995 | if (test_bit(In_sync, &rdev->flags)) { |
| 6996 | char b[BDEVNAME_SIZE]; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6997 | pr_info("md/raid:%s: device %s operational as raid disk %d\n", |
| 6998 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 6999 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7000 | /* Cannot rely on bitmap to complete recovery */ |
| 7001 | conf->fullsync = 1; |
| 7002 | } |
| 7003 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7004 | conf->level = mddev->new_level; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7005 | if (conf->level == 6) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7006 | conf->max_degraded = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7007 | if (raid6_call.xor_syndrome) |
| 7008 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 7009 | else |
| 7010 | conf->rmw_level = PARITY_DISABLE_RMW; |
| 7011 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7012 | conf->max_degraded = 1; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7013 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 7014 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7015 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7016 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7017 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7018 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7019 | conf->prev_algo = mddev->layout; |
NeilBrown | 5cac6bc | 2015-07-17 12:17:50 +1000 | [diff] [blame] | 7020 | } else { |
| 7021 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 7022 | conf->prev_algo = conf->algorithm; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7023 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7024 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7025 | conf->min_nr_stripes = NR_STRIPES; |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 7026 | if (mddev->reshape_position != MaxSector) { |
| 7027 | int stripes = max_t(int, |
| 7028 | ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4, |
| 7029 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4); |
| 7030 | conf->min_nr_stripes = max(NR_STRIPES, stripes); |
| 7031 | if (conf->min_nr_stripes != NR_STRIPES) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7032 | pr_info("md/raid:%s: force stripe size %d for reshape\n", |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 7033 | mdname(mddev), conf->min_nr_stripes); |
| 7034 | } |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7035 | memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7036 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7037 | atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7038 | if (grow_stripes(conf, conf->min_nr_stripes)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7039 | pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 7040 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7041 | goto abort; |
| 7042 | } else |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7043 | pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7044 | /* |
| 7045 | * Losing a stripe head costs more than the time to refill it, |
| 7046 | * it reduces the queue depth and so can hurt throughput. |
| 7047 | * So set it rather large, scaled by number of devices. |
| 7048 | */ |
| 7049 | conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4; |
| 7050 | conf->shrinker.scan_objects = raid5_cache_scan; |
| 7051 | conf->shrinker.count_objects = raid5_cache_count; |
| 7052 | conf->shrinker.batch = 128; |
| 7053 | conf->shrinker.flags = 0; |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 7054 | if (register_shrinker(&conf->shrinker)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7055 | pr_warn("md/raid:%s: couldn't register shrinker.\n", |
| 7056 | mdname(mddev)); |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 7057 | goto abort; |
| 7058 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7059 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7060 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 7061 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7062 | if (!conf->thread) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7063 | pr_warn("md/raid:%s: couldn't allocate thread.\n", |
| 7064 | mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7065 | goto abort; |
| 7066 | } |
| 7067 | |
| 7068 | return conf; |
| 7069 | |
| 7070 | abort: |
| 7071 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7072 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7073 | return ERR_PTR(-EIO); |
| 7074 | } else |
| 7075 | return ERR_PTR(-ENOMEM); |
| 7076 | } |
| 7077 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7078 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 7079 | { |
| 7080 | switch (algo) { |
| 7081 | case ALGORITHM_PARITY_0: |
| 7082 | if (raid_disk < max_degraded) |
| 7083 | return 1; |
| 7084 | break; |
| 7085 | case ALGORITHM_PARITY_N: |
| 7086 | if (raid_disk >= raid_disks - max_degraded) |
| 7087 | return 1; |
| 7088 | break; |
| 7089 | case ALGORITHM_PARITY_0_6: |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7090 | if (raid_disk == 0 || |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7091 | raid_disk == raid_disks - 1) |
| 7092 | return 1; |
| 7093 | break; |
| 7094 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 7095 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 7096 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 7097 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 7098 | if (raid_disk == raid_disks - 1) |
| 7099 | return 1; |
| 7100 | } |
| 7101 | return 0; |
| 7102 | } |
| 7103 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7104 | static int raid5_run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7105 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7106 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7107 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7108 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7109 | struct md_rdev *rdev; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7110 | struct md_rdev *journal_dev = NULL; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7111 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7112 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7113 | long long min_offset_diff = 0; |
| 7114 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7115 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 7116 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7117 | pr_notice("md/raid:%s: not clean -- starting background reconstruction\n", |
| 7118 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7119 | |
| 7120 | rdev_for_each(rdev, mddev) { |
| 7121 | long long diff; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7122 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7123 | if (test_bit(Journal, &rdev->flags)) { |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7124 | journal_dev = rdev; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7125 | continue; |
| 7126 | } |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7127 | if (rdev->raid_disk < 0) |
| 7128 | continue; |
| 7129 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 7130 | if (first) { |
| 7131 | min_offset_diff = diff; |
| 7132 | first = 0; |
| 7133 | } else if (mddev->reshape_backwards && |
| 7134 | diff < min_offset_diff) |
| 7135 | min_offset_diff = diff; |
| 7136 | else if (!mddev->reshape_backwards && |
| 7137 | diff > min_offset_diff) |
| 7138 | min_offset_diff = diff; |
| 7139 | } |
| 7140 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7141 | if (mddev->reshape_position != MaxSector) { |
| 7142 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7143 | * Difficulties arise if the stripe we would write to |
| 7144 | * next is at or after the stripe we would read from next. |
| 7145 | * For a reshape that changes the number of devices, this |
| 7146 | * is only possible for a very short time, and mdadm makes |
| 7147 | * sure that time appears to have past before assembling |
| 7148 | * the array. So we fail if that time hasn't passed. |
| 7149 | * For a reshape that keeps the number of devices the same |
| 7150 | * mdadm must be monitoring the reshape can keeping the |
| 7151 | * critical areas read-only and backed up. It will start |
| 7152 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7153 | */ |
| 7154 | sector_t here_new, here_old; |
| 7155 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 7156 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7157 | int chunk_sectors; |
| 7158 | int new_data_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7159 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7160 | if (journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7161 | pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n", |
| 7162 | mdname(mddev)); |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7163 | return -EINVAL; |
| 7164 | } |
| 7165 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7166 | if (mddev->new_level != mddev->level) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7167 | pr_warn("md/raid:%s: unsupported reshape required - aborting.\n", |
| 7168 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7169 | return -EINVAL; |
| 7170 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7171 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 7172 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7173 | * further up in new geometry must map after here in old |
| 7174 | * geometry. |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7175 | * If the chunk sizes are different, then as we perform reshape |
| 7176 | * in units of the largest of the two, reshape_position needs |
| 7177 | * be a multiple of the largest chunk size times new data disks. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7178 | */ |
| 7179 | here_new = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7180 | chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors); |
| 7181 | new_data_disks = mddev->raid_disks - max_degraded; |
| 7182 | if (sector_div(here_new, chunk_sectors * new_data_disks)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7183 | pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n", |
| 7184 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7185 | return -EINVAL; |
| 7186 | } |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7187 | reshape_offset = here_new * chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7188 | /* here_new is the stripe we will write to */ |
| 7189 | here_old = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7190 | sector_div(here_old, chunk_sectors * (old_disks-max_degraded)); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7191 | /* here_old is the first stripe that we might need to read |
| 7192 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7193 | if (mddev->delta_disks == 0) { |
| 7194 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7195 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7196 | * and taking constant backups. |
| 7197 | * mdadm always starts a situation like this in |
| 7198 | * readonly mode so it can take control before |
| 7199 | * allowing any writes. So just check for that. |
| 7200 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7201 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 7202 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 7203 | /* not really in-place - so OK */; |
| 7204 | else if (mddev->ro == 0) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7205 | pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n", |
| 7206 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7207 | return -EINVAL; |
| 7208 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7209 | } else if (mddev->reshape_backwards |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7210 | ? (here_new * chunk_sectors + min_offset_diff <= |
| 7211 | here_old * chunk_sectors) |
| 7212 | : (here_new * chunk_sectors >= |
| 7213 | here_old * chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7214 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7215 | pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n", |
| 7216 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7217 | return -EINVAL; |
| 7218 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7219 | pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7220 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7221 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7222 | BUG_ON(mddev->level != mddev->new_level); |
| 7223 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7224 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7225 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7226 | } |
| 7227 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 7228 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && |
| 7229 | test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 7230 | pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n", |
| 7231 | mdname(mddev)); |
| 7232 | clear_bit(MD_HAS_PPL, &mddev->flags); |
| 7233 | } |
| 7234 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7235 | if (mddev->private == NULL) |
| 7236 | conf = setup_conf(mddev); |
| 7237 | else |
| 7238 | conf = mddev->private; |
| 7239 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7240 | if (IS_ERR(conf)) |
| 7241 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 7242 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 7243 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 7244 | if (!journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7245 | pr_warn("md/raid:%s: journal disk is missing, force array readonly\n", |
| 7246 | mdname(mddev)); |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 7247 | mddev->ro = 1; |
| 7248 | set_disk_ro(mddev->gendisk, 1); |
| 7249 | } else if (mddev->recovery_cp == MaxSector) |
| 7250 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
Shaohua Li | 7dde2ad | 2015-10-08 21:54:10 -0700 | [diff] [blame] | 7251 | } |
| 7252 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7253 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7254 | mddev->thread = conf->thread; |
| 7255 | conf->thread = NULL; |
| 7256 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7257 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7258 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 7259 | i++) { |
| 7260 | rdev = conf->disks[i].rdev; |
| 7261 | if (!rdev && conf->disks[i].replacement) { |
| 7262 | /* The replacement is all we have yet */ |
| 7263 | rdev = conf->disks[i].replacement; |
| 7264 | conf->disks[i].replacement = NULL; |
| 7265 | clear_bit(Replacement, &rdev->flags); |
| 7266 | conf->disks[i].rdev = rdev; |
| 7267 | } |
| 7268 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7269 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7270 | if (conf->disks[i].replacement && |
| 7271 | conf->reshape_progress != MaxSector) { |
| 7272 | /* replacements and reshape simply do not mix. */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7273 | pr_warn("md: cannot handle concurrent replacement and reshape.\n"); |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7274 | goto abort; |
| 7275 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 7276 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7277 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 7278 | continue; |
| 7279 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7280 | /* This disc is not fully in-sync. However if it |
| 7281 | * just stored parity (beyond the recovery_offset), |
| 7282 | * when we don't need to be concerned about the |
| 7283 | * array being dirty. |
| 7284 | * When reshape goes 'backwards', we never have |
| 7285 | * partially completed devices, so we only need |
| 7286 | * to worry about reshape going forwards. |
| 7287 | */ |
| 7288 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 7289 | if (mddev->major_version == 0 && |
| 7290 | mddev->minor_version > 90) |
| 7291 | rdev->recovery_offset = reshape_offset; |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7292 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7293 | if (rdev->recovery_offset < reshape_offset) { |
| 7294 | /* We need to check old and new layout */ |
| 7295 | if (!only_parity(rdev->raid_disk, |
| 7296 | conf->algorithm, |
| 7297 | conf->raid_disks, |
| 7298 | conf->max_degraded)) |
| 7299 | continue; |
| 7300 | } |
| 7301 | if (!only_parity(rdev->raid_disk, |
| 7302 | conf->prev_algo, |
| 7303 | conf->previous_raid_disks, |
| 7304 | conf->max_degraded)) |
| 7305 | continue; |
| 7306 | dirty_parity_disks++; |
| 7307 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7308 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7309 | /* |
| 7310 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 7311 | */ |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7312 | mddev->degraded = raid5_calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7313 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 7314 | if (has_failed(conf)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7315 | pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7316 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7317 | goto abort; |
| 7318 | } |
| 7319 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7320 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7321 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7322 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 7323 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7324 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7325 | mddev->recovery_cp != MaxSector) { |
Artur Paszkiewicz | 4536bf9b | 2017-03-09 10:00:01 +0100 | [diff] [blame] | 7326 | if (test_bit(MD_HAS_PPL, &mddev->flags)) |
| 7327 | pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n", |
| 7328 | mdname(mddev)); |
| 7329 | else if (mddev->ok_start_degraded) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7330 | pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n", |
| 7331 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7332 | else { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7333 | pr_crit("md/raid:%s: cannot start dirty degraded array.\n", |
| 7334 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7335 | goto abort; |
| 7336 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7337 | } |
| 7338 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7339 | pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n", |
| 7340 | mdname(mddev), conf->level, |
| 7341 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 7342 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | |
| 7344 | print_raid5_conf(conf); |
| 7345 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7346 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7347 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7348 | atomic_set(&conf->reshape_stripes, 0); |
| 7349 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7350 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7351 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7352 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7353 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7354 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7355 | } |
| 7356 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7357 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7358 | if (mddev->to_remove == &raid5_attrs_group) |
| 7359 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7360 | else if (mddev->kobj.sd && |
| 7361 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7362 | pr_warn("raid5: failed to create sysfs attributes for %s\n", |
| 7363 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7364 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 7365 | |
| 7366 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7367 | int chunk_size; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7368 | bool discard_supported = true; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7369 | /* read-ahead size must cover two whole stripes, which |
| 7370 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 7371 | * number of raid devices |
| 7372 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7373 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 7374 | int stripe = data_disks * |
| 7375 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 7376 | if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
| 7377 | mddev->queue->backing_dev_info->ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7378 | |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7379 | chunk_size = mddev->chunk_sectors << 9; |
| 7380 | blk_queue_io_min(mddev->queue, chunk_size); |
| 7381 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 7382 | (conf->raid_disks - conf->max_degraded)); |
Kent Overstreet | c78afc6 | 2013-07-11 22:39:53 -0700 | [diff] [blame] | 7383 | mddev->queue->limits.raid_partial_stripes_expensive = 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7384 | /* |
| 7385 | * We can only discard a whole stripe. It doesn't make sense to |
| 7386 | * discard data disk but write parity disk |
| 7387 | */ |
| 7388 | stripe = stripe * PAGE_SIZE; |
NeilBrown | 4ac6875 | 2012-11-19 13:11:26 +1100 | [diff] [blame] | 7389 | /* Round up to power of 2, as discard handling |
| 7390 | * currently assumes that */ |
| 7391 | while ((stripe-1) & stripe) |
| 7392 | stripe = (stripe | (stripe-1)) + 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7393 | mddev->queue->limits.discard_alignment = stripe; |
| 7394 | mddev->queue->limits.discard_granularity = stripe; |
Konstantin Khlebnikov | e8d7c33 | 2016-11-27 19:32:32 +0300 | [diff] [blame] | 7395 | |
| 7396 | /* |
| 7397 | * We use 16-bit counter of active stripes in bi_phys_segments |
| 7398 | * (minus one for over-loaded initialization) |
| 7399 | */ |
| 7400 | blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS); |
| 7401 | blk_queue_max_discard_sectors(mddev->queue, |
| 7402 | 0xfffe * STRIPE_SECTORS); |
| 7403 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7404 | /* |
| 7405 | * unaligned part of discard request will be ignored, so can't |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 7406 | * guarantee discard_zeroes_data |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7407 | */ |
| 7408 | mddev->queue->limits.discard_zeroes_data = 0; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7409 | |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7410 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
| 7411 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7412 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7413 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7414 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7415 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7416 | rdev->new_data_offset << 9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7417 | /* |
| 7418 | * discard_zeroes_data is required, otherwise data |
| 7419 | * could be lost. Consider a scenario: discard a stripe |
| 7420 | * (the stripe could be inconsistent if |
| 7421 | * discard_zeroes_data is 0); write one disk of the |
| 7422 | * stripe (the stripe could be inconsistent again |
| 7423 | * depending on which disks are used to calculate |
| 7424 | * parity); the disk is broken; The stripe data of this |
| 7425 | * disk is lost. |
| 7426 | */ |
| 7427 | if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || |
| 7428 | !bdev_get_queue(rdev->bdev)-> |
| 7429 | limits.discard_zeroes_data) |
| 7430 | discard_supported = false; |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 7431 | /* Unfortunately, discard_zeroes_data is not currently |
| 7432 | * a guarantee - just a hint. So we only allow DISCARD |
| 7433 | * if the sysadmin has confirmed that only safe devices |
| 7434 | * are in use by setting a module parameter. |
| 7435 | */ |
| 7436 | if (!devices_handle_discard_safely) { |
| 7437 | if (discard_supported) { |
| 7438 | pr_info("md/raid456: discard support disabled due to uncertainty.\n"); |
| 7439 | pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n"); |
| 7440 | } |
| 7441 | discard_supported = false; |
| 7442 | } |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7443 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7444 | |
| 7445 | if (discard_supported && |
Jes Sorensen | e7597e6 | 2016-02-16 16:44:24 -0500 | [diff] [blame] | 7446 | mddev->queue->limits.max_discard_sectors >= (stripe >> 9) && |
| 7447 | mddev->queue->limits.discard_granularity >= stripe) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7448 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 7449 | mddev->queue); |
| 7450 | else |
| 7451 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, |
| 7452 | mddev->queue); |
Shaohua Li | 1dffddd | 2016-09-08 10:49:06 -0700 | [diff] [blame] | 7453 | |
| 7454 | blk_queue_max_hw_sectors(mddev->queue, UINT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7455 | } |
| 7456 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7457 | if (log_init(conf, journal_dev)) |
| 7458 | goto abort; |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 7459 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7460 | return 0; |
| 7461 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7462 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 7463 | print_raid5_conf(conf); |
| 7464 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7465 | mddev->private = NULL; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7466 | pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7467 | return -EIO; |
| 7468 | } |
| 7469 | |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7470 | static void raid5_free(struct mddev *mddev, void *priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7471 | { |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7472 | struct r5conf *conf = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7473 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7474 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7475 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7476 | } |
| 7477 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7478 | static void raid5_status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7479 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7480 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7481 | int i; |
| 7482 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7483 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7484 | conf->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7485 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
NeilBrown | 5fd1335 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 7486 | rcu_read_lock(); |
| 7487 | for (i = 0; i < conf->raid_disks; i++) { |
| 7488 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
| 7489 | seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 7490 | } |
| 7491 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7492 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7493 | } |
| 7494 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7495 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7496 | { |
| 7497 | int i; |
| 7498 | struct disk_info *tmp; |
| 7499 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7500 | pr_debug("RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7501 | if (!conf) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7502 | pr_debug("(conf==NULL)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7503 | return; |
| 7504 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7505 | pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level, |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7506 | conf->raid_disks, |
| 7507 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7508 | |
| 7509 | for (i = 0; i < conf->raid_disks; i++) { |
| 7510 | char b[BDEVNAME_SIZE]; |
| 7511 | tmp = conf->disks + i; |
| 7512 | if (tmp->rdev) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7513 | pr_debug(" disk %d, o:%d, dev:%s\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7514 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 7515 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7516 | } |
| 7517 | } |
| 7518 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7519 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7520 | { |
| 7521 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7522 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7523 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7524 | int count = 0; |
| 7525 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7526 | |
| 7527 | for (i = 0; i < conf->raid_disks; i++) { |
| 7528 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7529 | if (tmp->replacement |
| 7530 | && tmp->replacement->recovery_offset == MaxSector |
| 7531 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 7532 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 7533 | /* Replacement has just become active. */ |
| 7534 | if (!tmp->rdev |
| 7535 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 7536 | count++; |
| 7537 | if (tmp->rdev) { |
| 7538 | /* Replaced device not technically faulty, |
| 7539 | * but we need to be sure it gets removed |
| 7540 | * and never re-added. |
| 7541 | */ |
| 7542 | set_bit(Faulty, &tmp->rdev->flags); |
| 7543 | sysfs_notify_dirent_safe( |
| 7544 | tmp->rdev->sysfs_state); |
| 7545 | } |
| 7546 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 7547 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7548 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7549 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 7550 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7551 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7552 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7553 | } |
| 7554 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7555 | spin_lock_irqsave(&conf->device_lock, flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7556 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7557 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7558 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7559 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7560 | } |
| 7561 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7562 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7563 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7564 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7565 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7566 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7567 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7568 | struct disk_info *p = conf->disks + number; |
| 7569 | |
| 7570 | print_raid5_conf(conf); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7571 | if (test_bit(Journal, &rdev->flags) && conf->log) { |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7572 | /* |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7573 | * we can't wait pending write here, as this is called in |
| 7574 | * raid5d, wait will deadlock. |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7575 | */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7576 | if (atomic_read(&mddev->writes_pending)) |
| 7577 | return -EBUSY; |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7578 | log_exit(conf); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7579 | return 0; |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7580 | } |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7581 | if (rdev == p->rdev) |
| 7582 | rdevp = &p->rdev; |
| 7583 | else if (rdev == p->replacement) |
| 7584 | rdevp = &p->replacement; |
| 7585 | else |
| 7586 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7587 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7588 | if (number >= conf->raid_disks && |
| 7589 | conf->reshape_progress == MaxSector) |
| 7590 | clear_bit(In_sync, &rdev->flags); |
| 7591 | |
| 7592 | if (test_bit(In_sync, &rdev->flags) || |
| 7593 | atomic_read(&rdev->nr_pending)) { |
| 7594 | err = -EBUSY; |
| 7595 | goto abort; |
| 7596 | } |
| 7597 | /* Only remove non-faulty devices if recovery |
| 7598 | * isn't possible. |
| 7599 | */ |
| 7600 | if (!test_bit(Faulty, &rdev->flags) && |
| 7601 | mddev->recovery_disabled != conf->recovery_disabled && |
| 7602 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7603 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7604 | number < conf->raid_disks) { |
| 7605 | err = -EBUSY; |
| 7606 | goto abort; |
| 7607 | } |
| 7608 | *rdevp = NULL; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 7609 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
| 7610 | synchronize_rcu(); |
| 7611 | if (atomic_read(&rdev->nr_pending)) { |
| 7612 | /* lost the race, try later */ |
| 7613 | err = -EBUSY; |
| 7614 | *rdevp = rdev; |
| 7615 | } |
| 7616 | } |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 7617 | if (!err) { |
| 7618 | err = log_modify(conf, rdev, false); |
| 7619 | if (err) |
| 7620 | goto abort; |
| 7621 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 7622 | if (p->replacement) { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7623 | /* We must have just cleared 'rdev' */ |
| 7624 | p->rdev = p->replacement; |
| 7625 | clear_bit(Replacement, &p->replacement->flags); |
| 7626 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 7627 | * but will never see neither - if they are careful |
| 7628 | */ |
| 7629 | p->replacement = NULL; |
| 7630 | clear_bit(WantReplacement, &rdev->flags); |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 7631 | |
| 7632 | if (!err) |
| 7633 | err = log_modify(conf, p->rdev, true); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7634 | } else |
| 7635 | /* We might have just removed the Replacement as faulty- |
| 7636 | * clear the bit just in case |
| 7637 | */ |
| 7638 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7639 | abort: |
| 7640 | |
| 7641 | print_raid5_conf(conf); |
| 7642 | return err; |
| 7643 | } |
| 7644 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7645 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7646 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7647 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7648 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7649 | int disk; |
| 7650 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7651 | int first = 0; |
| 7652 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7653 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7654 | if (test_bit(Journal, &rdev->flags)) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7655 | if (conf->log) |
| 7656 | return -EBUSY; |
| 7657 | |
| 7658 | rdev->raid_disk = 0; |
| 7659 | /* |
| 7660 | * The array is in readonly mode if journal is missing, so no |
| 7661 | * write requests running. We should be safe |
| 7662 | */ |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7663 | log_init(conf, rdev); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7664 | return 0; |
| 7665 | } |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 7666 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 7667 | return -EBUSY; |
| 7668 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 7669 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7670 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7671 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7672 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7673 | if (rdev->raid_disk >= 0) |
| 7674 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7675 | |
| 7676 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7677 | * find the disk ... but prefer rdev->saved_raid_disk |
| 7678 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7679 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7680 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7681 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7682 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7683 | first = rdev->saved_raid_disk; |
| 7684 | |
| 7685 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7686 | p = conf->disks + disk; |
| 7687 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7688 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7689 | rdev->raid_disk = disk; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7690 | if (rdev->saved_raid_disk != disk) |
| 7691 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 7692 | rcu_assign_pointer(p->rdev, rdev); |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 7693 | |
| 7694 | err = log_modify(conf, rdev, true); |
| 7695 | |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7696 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7697 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7698 | } |
| 7699 | for (disk = first; disk <= last; disk++) { |
| 7700 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7701 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 7702 | p->replacement == NULL) { |
| 7703 | clear_bit(In_sync, &rdev->flags); |
| 7704 | set_bit(Replacement, &rdev->flags); |
| 7705 | rdev->raid_disk = disk; |
| 7706 | err = 0; |
| 7707 | conf->fullsync = 1; |
| 7708 | rcu_assign_pointer(p->replacement, rdev); |
| 7709 | break; |
| 7710 | } |
| 7711 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7712 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7713 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7714 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7715 | } |
| 7716 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7717 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7718 | { |
| 7719 | /* no resync is happening, and there is enough space |
| 7720 | * on all devices, so we can resize. |
| 7721 | * We need to make sure resync covers any new space. |
| 7722 | * If the array is shrinking we should possibly wait until |
| 7723 | * any io in the removed space completes, but it hardly seems |
| 7724 | * worth it. |
| 7725 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7726 | sector_t newsize; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7727 | struct r5conf *conf = mddev->private; |
| 7728 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 7729 | if (conf->log || raid5_has_ppl(conf)) |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7730 | return -EINVAL; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7731 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7732 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 7733 | if (mddev->external_size && |
| 7734 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 7735 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7736 | if (mddev->bitmap) { |
| 7737 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 7738 | if (ret) |
| 7739 | return ret; |
| 7740 | } |
| 7741 | md_set_array_sectors(mddev, newsize); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 7742 | if (sectors > mddev->dev_sectors && |
| 7743 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7744 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7745 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7746 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7747 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 7748 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7749 | return 0; |
| 7750 | } |
| 7751 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7752 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7753 | { |
| 7754 | /* Can only proceed if there are plenty of stripe_heads. |
| 7755 | * We need a minimum of one full stripe,, and for sensible progress |
| 7756 | * it is best to have about 4 times that. |
| 7757 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 7758 | * allow for chunk sizes up to 256K, which is probably OK. |
| 7759 | * If the chunk size is greater, user-space should request more |
| 7760 | * stripe_heads first. |
| 7761 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7762 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7763 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7764 | > conf->min_nr_stripes || |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7765 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7766 | > conf->min_nr_stripes) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7767 | pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 7768 | mdname(mddev), |
| 7769 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 7770 | / STRIPE_SIZE)*4); |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7771 | return 0; |
| 7772 | } |
| 7773 | return 1; |
| 7774 | } |
| 7775 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7776 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7777 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7778 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7779 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 7780 | if (conf->log || raid5_has_ppl(conf)) |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7781 | return -EINVAL; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7782 | if (mddev->delta_disks == 0 && |
| 7783 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7784 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7785 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 7786 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7787 | return -EINVAL; |
NeilBrown | fdcfbbb | 2013-07-04 16:38:16 +1000 | [diff] [blame] | 7788 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7789 | /* We might be able to shrink, but the devices must |
| 7790 | * be made bigger first. |
| 7791 | * For raid6, 4 is the minimum size. |
| 7792 | * Otherwise 2 is the minimum |
| 7793 | */ |
| 7794 | int min = 2; |
| 7795 | if (mddev->level == 6) |
| 7796 | min = 4; |
| 7797 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 7798 | return -EINVAL; |
| 7799 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7800 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7801 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7802 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7803 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 7804 | if (mddev->new_chunk_sectors > mddev->chunk_sectors || |
| 7805 | mddev->delta_disks > 0) |
| 7806 | if (resize_chunks(conf, |
| 7807 | conf->previous_raid_disks |
| 7808 | + max(0, mddev->delta_disks), |
| 7809 | max(mddev->new_chunk_sectors, |
| 7810 | mddev->chunk_sectors) |
| 7811 | ) < 0) |
| 7812 | return -ENOMEM; |
NeilBrown | e56108d6 | 2012-10-11 14:24:13 +1100 | [diff] [blame] | 7813 | return resize_stripes(conf, (conf->previous_raid_disks |
| 7814 | + mddev->delta_disks)); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7815 | } |
| 7816 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7817 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7818 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7819 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7820 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7821 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 7822 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7823 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7824 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7825 | return -EBUSY; |
| 7826 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7827 | if (!check_stripe_cache(mddev)) |
| 7828 | return -ENOSPC; |
| 7829 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 7830 | if (has_failed(conf)) |
| 7831 | return -EINVAL; |
| 7832 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7833 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7834 | if (!test_bit(In_sync, &rdev->flags) |
| 7835 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7836 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7837 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7838 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7839 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7840 | /* Not enough devices even to make a degraded array |
| 7841 | * of that size |
| 7842 | */ |
| 7843 | return -EINVAL; |
| 7844 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7845 | /* Refuse to reduce size of the array. Any reductions in |
| 7846 | * array size must be through explicit setting of array_size |
| 7847 | * attribute. |
| 7848 | */ |
| 7849 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 7850 | < mddev->array_sectors) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7851 | pr_warn("md/raid:%s: array size must be reduced before number of disks\n", |
| 7852 | mdname(mddev)); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7853 | return -EINVAL; |
| 7854 | } |
| 7855 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7856 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7857 | spin_lock_irq(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 7858 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7859 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7860 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7861 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 7862 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7863 | conf->prev_algo = conf->algorithm; |
| 7864 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7865 | conf->generation++; |
| 7866 | /* Code that selects data_offset needs to see the generation update |
| 7867 | * if reshape_progress has been set - so a memory barrier needed. |
| 7868 | */ |
| 7869 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7870 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7871 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 7872 | else |
| 7873 | conf->reshape_progress = 0; |
| 7874 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 7875 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7876 | spin_unlock_irq(&conf->device_lock); |
| 7877 | |
NeilBrown | 4d77e3b | 2013-08-27 15:57:47 +1000 | [diff] [blame] | 7878 | /* Now make sure any requests that proceeded on the assumption |
| 7879 | * the reshape wasn't running - like Discard or Read - have |
| 7880 | * completed. |
| 7881 | */ |
| 7882 | mddev_suspend(mddev); |
| 7883 | mddev_resume(mddev); |
| 7884 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7885 | /* Add some new drives, as many as will fit. |
| 7886 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 7887 | * Don't add devices if we are reducing the number of |
| 7888 | * devices in the array. This is because it is not possible |
| 7889 | * to correctly record the "partially reconstructed" state of |
| 7890 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7891 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7892 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7893 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7894 | if (rdev->raid_disk < 0 && |
| 7895 | !test_bit(Faulty, &rdev->flags)) { |
| 7896 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7897 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 7898 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7899 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 7900 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7901 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7902 | |
| 7903 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7904 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7905 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7906 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 7907 | && !test_bit(Faulty, &rdev->flags)) { |
| 7908 | /* This is a spare that was manually added */ |
| 7909 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7910 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7911 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7912 | /* When a reshape changes the number of devices, |
| 7913 | * ->degraded is measured against the larger of the |
| 7914 | * pre and post number of devices. |
| 7915 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7916 | spin_lock_irqsave(&conf->device_lock, flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7917 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7918 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 7919 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7920 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 7921 | mddev->reshape_position = conf->reshape_progress; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7922 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7923 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7924 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7925 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 7926 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7927 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7928 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7929 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7930 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7931 | if (!mddev->sync_thread) { |
| 7932 | mddev->recovery = 0; |
| 7933 | spin_lock_irq(&conf->device_lock); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7934 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7935 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7936 | mddev->new_chunk_sectors = |
| 7937 | conf->chunk_sectors = conf->prev_chunk_sectors; |
| 7938 | mddev->new_layout = conf->algorithm = conf->prev_algo; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7939 | rdev_for_each(rdev, mddev) |
| 7940 | rdev->new_data_offset = rdev->data_offset; |
| 7941 | smp_wmb(); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7942 | conf->generation --; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7943 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 7944 | mddev->reshape_position = MaxSector; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7945 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7946 | spin_unlock_irq(&conf->device_lock); |
| 7947 | return -EAGAIN; |
| 7948 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 7949 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7950 | md_wakeup_thread(mddev->sync_thread); |
| 7951 | md_new_event(mddev); |
| 7952 | return 0; |
| 7953 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7954 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7955 | /* This is called from the reshape thread and should make any |
| 7956 | * changes needed in 'conf' |
| 7957 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7958 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7959 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7960 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7961 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7962 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7963 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7964 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7965 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7966 | rdev_for_each(rdev, conf->mddev) |
| 7967 | rdev->data_offset = rdev->new_data_offset; |
| 7968 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7969 | conf->reshape_progress = MaxSector; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 7970 | conf->mddev->reshape_position = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7971 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 7972 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7973 | |
| 7974 | /* read-ahead size must cover two whole stripes, which is |
| 7975 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 7976 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7977 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7978 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7979 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7980 | / PAGE_SIZE); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 7981 | if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
| 7982 | conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7983 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7984 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7985 | } |
| 7986 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7987 | /* This is called from the raid5d thread with mddev_lock held. |
| 7988 | * It makes config changes to the device. |
| 7989 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7990 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7991 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7992 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7993 | |
| 7994 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7995 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7996 | if (mddev->delta_disks > 0) { |
| 7997 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
Heinz Mauelshagen | fe67d19 | 2016-05-03 22:15:31 +0200 | [diff] [blame] | 7998 | if (mddev->queue) { |
| 7999 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 8000 | revalidate_disk(mddev->gendisk); |
| 8001 | } |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8002 | } else { |
| 8003 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 8004 | spin_lock_irq(&conf->device_lock); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 8005 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 8006 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8007 | for (d = conf->raid_disks ; |
| 8008 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 8009 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8010 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 8011 | if (rdev) |
| 8012 | clear_bit(In_sync, &rdev->flags); |
| 8013 | rdev = conf->disks[d].replacement; |
| 8014 | if (rdev) |
| 8015 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 8016 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8017 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8018 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 8019 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8020 | mddev->reshape_position = MaxSector; |
| 8021 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8022 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8023 | } |
| 8024 | } |
| 8025 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8026 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8027 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8028 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8029 | |
| 8030 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 8031 | case 2: /* resume for a suspend */ |
| 8032 | wake_up(&conf->wait_for_overlap); |
| 8033 | break; |
| 8034 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8035 | case 1: /* stop all writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8036 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8037 | /* '2' tells resync/reshape to pause so that all |
| 8038 | * active stripes can drain |
| 8039 | */ |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 8040 | r5c_flush_cache(conf, INT_MAX); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8041 | conf->quiesce = 2; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 8042 | wait_event_cmd(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 8043 | atomic_read(&conf->active_stripes) == 0 && |
| 8044 | atomic_read(&conf->active_aligned_reads) == 0, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8045 | unlock_all_device_hash_locks_irq(conf), |
| 8046 | lock_all_device_hash_locks_irq(conf)); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8047 | conf->quiesce = 1; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8048 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8049 | /* allow reshape to continue */ |
| 8050 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8051 | break; |
| 8052 | |
| 8053 | case 0: /* re-enable writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8054 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8055 | conf->quiesce = 0; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 8056 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 8057 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8058 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8059 | break; |
| 8060 | } |
Shaohua Li | e6c033f | 2015-10-04 09:20:12 -0700 | [diff] [blame] | 8061 | r5l_quiesce(conf->log, state); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8062 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 8063 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8064 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8065 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8066 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 8067 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8068 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8069 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8070 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 8071 | pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 8072 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8073 | return ERR_PTR(-EINVAL); |
| 8074 | } |
| 8075 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8076 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 8077 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 8078 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8079 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8080 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 8081 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 8082 | mddev->raid_disks += 1; |
| 8083 | mddev->delta_disks = 1; |
| 8084 | /* make sure it will be not marked as dirty */ |
| 8085 | mddev->recovery_cp = MaxSector; |
| 8086 | |
| 8087 | return setup_conf(mddev); |
| 8088 | } |
| 8089 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8090 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8091 | { |
| 8092 | int chunksect; |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8093 | void *ret; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8094 | |
| 8095 | if (mddev->raid_disks != 2 || |
| 8096 | mddev->degraded > 1) |
| 8097 | return ERR_PTR(-EINVAL); |
| 8098 | |
| 8099 | /* Should check if there are write-behind devices? */ |
| 8100 | |
| 8101 | chunksect = 64*2; /* 64K by default */ |
| 8102 | |
| 8103 | /* The array must be an exact multiple of chunksize */ |
| 8104 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 8105 | chunksect >>= 1; |
| 8106 | |
| 8107 | if ((chunksect<<9) < STRIPE_SIZE) |
| 8108 | /* array size does not allow a suitable chunk size */ |
| 8109 | return ERR_PTR(-EINVAL); |
| 8110 | |
| 8111 | mddev->new_level = 5; |
| 8112 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 8113 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8114 | |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8115 | ret = setup_conf(mddev); |
Jes Sorensen | 32cd7cb | 2017-01-06 19:31:35 -0500 | [diff] [blame] | 8116 | if (!IS_ERR(ret)) |
Shaohua Li | 394ed8e | 2017-01-04 16:10:19 -0800 | [diff] [blame] | 8117 | mddev_clear_unsupported_flags(mddev, |
| 8118 | UNSUPPORTED_MDDEV_FLAGS); |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8119 | return ret; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8120 | } |
| 8121 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8122 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 8123 | { |
| 8124 | int new_layout; |
| 8125 | |
| 8126 | switch (mddev->layout) { |
| 8127 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 8128 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 8129 | break; |
| 8130 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 8131 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 8132 | break; |
| 8133 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 8134 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 8135 | break; |
| 8136 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 8137 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 8138 | break; |
| 8139 | case ALGORITHM_PARITY_0_6: |
| 8140 | new_layout = ALGORITHM_PARITY_0; |
| 8141 | break; |
| 8142 | case ALGORITHM_PARITY_N: |
| 8143 | new_layout = ALGORITHM_PARITY_N; |
| 8144 | break; |
| 8145 | default: |
| 8146 | return ERR_PTR(-EINVAL); |
| 8147 | } |
| 8148 | mddev->new_level = 5; |
| 8149 | mddev->new_layout = new_layout; |
| 8150 | mddev->delta_disks = -1; |
| 8151 | mddev->raid_disks -= 1; |
| 8152 | return setup_conf(mddev); |
| 8153 | } |
| 8154 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8155 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8156 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8157 | /* For a 2-drive array, the layout and chunk size can be changed |
| 8158 | * immediately as not restriping is needed. |
| 8159 | * For larger arrays we record the new value - after validation |
| 8160 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8161 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8162 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8163 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8164 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8165 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8166 | return -EINVAL; |
| 8167 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 8168 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8169 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8170 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8171 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8172 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8173 | /* not factor of array size */ |
| 8174 | return -EINVAL; |
| 8175 | } |
| 8176 | |
| 8177 | /* They look valid */ |
| 8178 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8179 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8180 | /* can make the change immediately */ |
| 8181 | if (mddev->new_layout >= 0) { |
| 8182 | conf->algorithm = mddev->new_layout; |
| 8183 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8184 | } |
| 8185 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8186 | conf->chunk_sectors = new_chunk ; |
| 8187 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8188 | } |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8189 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8190 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8191 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8192 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8193 | } |
| 8194 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8195 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8196 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8197 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8198 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8199 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8200 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8201 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 8202 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8203 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8204 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8205 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8206 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8207 | /* not factor of array size */ |
| 8208 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8209 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8210 | |
| 8211 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8212 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8213 | } |
| 8214 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8215 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8216 | { |
| 8217 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8218 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8219 | * raid1 - if there are two drives. We need to know the chunk size |
| 8220 | * raid4 - trivial - just use a raid4 layout. |
| 8221 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8222 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8223 | if (mddev->level == 0) |
| 8224 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8225 | if (mddev->level == 1) |
| 8226 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 8227 | if (mddev->level == 4) { |
| 8228 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 8229 | mddev->new_level = 5; |
| 8230 | return setup_conf(mddev); |
| 8231 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 8232 | if (mddev->level == 6) |
| 8233 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8234 | |
| 8235 | return ERR_PTR(-EINVAL); |
| 8236 | } |
| 8237 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8238 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8239 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8240 | /* raid4 can take over: |
| 8241 | * raid0 - if there is only one strip zone |
| 8242 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8243 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8244 | if (mddev->level == 0) |
| 8245 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8246 | if (mddev->level == 5 && |
| 8247 | mddev->layout == ALGORITHM_PARITY_N) { |
| 8248 | mddev->new_layout = 0; |
| 8249 | mddev->new_level = 4; |
| 8250 | return setup_conf(mddev); |
| 8251 | } |
| 8252 | return ERR_PTR(-EINVAL); |
| 8253 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8254 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8255 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8256 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8257 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8258 | { |
| 8259 | /* Currently can only take over a raid5. We map the |
| 8260 | * personality to an equivalent raid6 personality |
| 8261 | * with the Q block at the end. |
| 8262 | */ |
| 8263 | int new_layout; |
| 8264 | |
| 8265 | if (mddev->pers != &raid5_personality) |
| 8266 | return ERR_PTR(-EINVAL); |
| 8267 | if (mddev->degraded > 1) |
| 8268 | return ERR_PTR(-EINVAL); |
| 8269 | if (mddev->raid_disks > 253) |
| 8270 | return ERR_PTR(-EINVAL); |
| 8271 | if (mddev->raid_disks < 3) |
| 8272 | return ERR_PTR(-EINVAL); |
| 8273 | |
| 8274 | switch (mddev->layout) { |
| 8275 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 8276 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 8277 | break; |
| 8278 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 8279 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 8280 | break; |
| 8281 | case ALGORITHM_LEFT_SYMMETRIC: |
| 8282 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 8283 | break; |
| 8284 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 8285 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 8286 | break; |
| 8287 | case ALGORITHM_PARITY_0: |
| 8288 | new_layout = ALGORITHM_PARITY_0_6; |
| 8289 | break; |
| 8290 | case ALGORITHM_PARITY_N: |
| 8291 | new_layout = ALGORITHM_PARITY_N; |
| 8292 | break; |
| 8293 | default: |
| 8294 | return ERR_PTR(-EINVAL); |
| 8295 | } |
| 8296 | mddev->new_level = 6; |
| 8297 | mddev->new_layout = new_layout; |
| 8298 | mddev->delta_disks = 1; |
| 8299 | mddev->raid_disks += 1; |
| 8300 | return setup_conf(mddev); |
| 8301 | } |
| 8302 | |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 8303 | static void raid5_reset_stripe_cache(struct mddev *mddev) |
| 8304 | { |
| 8305 | struct r5conf *conf = mddev->private; |
| 8306 | |
| 8307 | mutex_lock(&conf->cache_size_mutex); |
| 8308 | while (conf->max_nr_stripes && |
| 8309 | drop_one_stripe(conf)) |
| 8310 | ; |
| 8311 | while (conf->min_nr_stripes > conf->max_nr_stripes && |
| 8312 | grow_one_stripe(conf, GFP_KERNEL)) |
| 8313 | ; |
| 8314 | mutex_unlock(&conf->cache_size_mutex); |
| 8315 | } |
| 8316 | |
| 8317 | static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf) |
| 8318 | { |
| 8319 | struct r5conf *conf; |
| 8320 | int err; |
| 8321 | |
| 8322 | err = mddev_lock(mddev); |
| 8323 | if (err) |
| 8324 | return err; |
| 8325 | conf = mddev->private; |
| 8326 | if (!conf) { |
| 8327 | mddev_unlock(mddev); |
| 8328 | return -ENODEV; |
| 8329 | } |
| 8330 | |
| 8331 | if (strncmp(buf, "ppl", 3) == 0 && !raid5_has_ppl(conf)) { |
| 8332 | mddev_suspend(mddev); |
| 8333 | set_bit(MD_HAS_PPL, &mddev->flags); |
| 8334 | err = log_init(conf, NULL); |
| 8335 | if (!err) |
| 8336 | raid5_reset_stripe_cache(mddev); |
| 8337 | mddev_resume(mddev); |
| 8338 | } else if (strncmp(buf, "resync", 6) == 0 && raid5_has_ppl(conf)) { |
| 8339 | mddev_suspend(mddev); |
| 8340 | log_exit(conf); |
| 8341 | raid5_reset_stripe_cache(mddev); |
| 8342 | mddev_resume(mddev); |
| 8343 | } else { |
| 8344 | err = -EINVAL; |
| 8345 | } |
| 8346 | |
| 8347 | if (!err) |
| 8348 | md_update_sb(mddev, 1); |
| 8349 | |
| 8350 | mddev_unlock(mddev); |
| 8351 | |
| 8352 | return err; |
| 8353 | } |
| 8354 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8355 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8356 | { |
| 8357 | .name = "raid6", |
| 8358 | .level = 6, |
| 8359 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8360 | .make_request = raid5_make_request, |
| 8361 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8362 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8363 | .status = raid5_status, |
| 8364 | .error_handler = raid5_error, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8365 | .hot_add_disk = raid5_add_disk, |
| 8366 | .hot_remove_disk= raid5_remove_disk, |
| 8367 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8368 | .sync_request = raid5_sync_request, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8369 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8370 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8371 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 8372 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8373 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8374 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8375 | .takeover = raid6_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8376 | .congested = raid5_congested, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8377 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8378 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8379 | { |
| 8380 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8381 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8382 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8383 | .make_request = raid5_make_request, |
| 8384 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8385 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8386 | .status = raid5_status, |
| 8387 | .error_handler = raid5_error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8388 | .hot_add_disk = raid5_add_disk, |
| 8389 | .hot_remove_disk= raid5_remove_disk, |
| 8390 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8391 | .sync_request = raid5_sync_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8392 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8393 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8394 | .check_reshape = raid5_check_reshape, |
| 8395 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8396 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8397 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8398 | .takeover = raid5_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8399 | .congested = raid5_congested, |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 8400 | .change_consistency_policy = raid5_change_consistency_policy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8401 | }; |
| 8402 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8403 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8404 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8405 | .name = "raid4", |
| 8406 | .level = 4, |
| 8407 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8408 | .make_request = raid5_make_request, |
| 8409 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8410 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8411 | .status = raid5_status, |
| 8412 | .error_handler = raid5_error, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8413 | .hot_add_disk = raid5_add_disk, |
| 8414 | .hot_remove_disk= raid5_remove_disk, |
| 8415 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8416 | .sync_request = raid5_sync_request, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8417 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8418 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 8419 | .check_reshape = raid5_check_reshape, |
| 8420 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8421 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8422 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8423 | .takeover = raid4_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8424 | .congested = raid5_congested, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8425 | }; |
| 8426 | |
| 8427 | static int __init raid5_init(void) |
| 8428 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8429 | int ret; |
| 8430 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8431 | raid5_wq = alloc_workqueue("raid5wq", |
| 8432 | WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); |
| 8433 | if (!raid5_wq) |
| 8434 | return -ENOMEM; |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8435 | |
| 8436 | ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE, |
| 8437 | "md/raid5:prepare", |
| 8438 | raid456_cpu_up_prepare, |
| 8439 | raid456_cpu_dead); |
| 8440 | if (ret) { |
| 8441 | destroy_workqueue(raid5_wq); |
| 8442 | return ret; |
| 8443 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8444 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8445 | register_md_personality(&raid5_personality); |
| 8446 | register_md_personality(&raid4_personality); |
| 8447 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8448 | } |
| 8449 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8450 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8451 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8452 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8453 | unregister_md_personality(&raid5_personality); |
| 8454 | unregister_md_personality(&raid4_personality); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8455 | cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8456 | destroy_workqueue(raid5_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8457 | } |
| 8458 | |
| 8459 | module_init(raid5_init); |
| 8460 | module_exit(raid5_exit); |
| 8461 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8462 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8463 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 8464 | MODULE_ALIAS("md-raid5"); |
| 8465 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8466 | MODULE_ALIAS("md-level-5"); |
| 8467 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8468 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 8469 | MODULE_ALIAS("md-raid6"); |
| 8470 | MODULE_ALIAS("md-level-6"); |
| 8471 | |
| 8472 | /* This used to be two separate modules, they were: */ |
| 8473 | MODULE_ALIAS("raid5"); |
| 8474 | MODULE_ALIAS("raid6"); |