Thomas Gleixner | af1a889 | 2019-05-20 19:08:12 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * raid5.c : Multiple Devices driver for Linux |
| 4 | * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman |
| 5 | * Copyright (C) 1999, 2000 Ingo Molnar |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6 | * Copyright (C) 2002, 2003 H. Peter Anvin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8 | * RAID-4/5/6 management functions. |
| 9 | * Thanks to Penguin Computing for making the RAID-6 development possible |
| 10 | * by donating a test server! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | */ |
| 12 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 13 | /* |
| 14 | * BITMAP UNPLUGGING: |
| 15 | * |
| 16 | * The sequencing for updating the bitmap reliably is a little |
| 17 | * subtle (and I got it wrong the first time) so it deserves some |
| 18 | * explanation. |
| 19 | * |
| 20 | * We group bitmap updates into batches. Each batch has a number. |
| 21 | * 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] | 22 | * conf->seq_write is the number of the last batch successfully written. |
| 23 | * 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] | 24 | * new additions. |
| 25 | * When we discover that we will need to write to any block in a stripe |
| 26 | * (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] | 27 | * 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] | 28 | * When we are ready to do a write, if that batch hasn't been written yet, |
| 29 | * we plug the array and queue the stripe for later. |
| 30 | * When an unplug happens, we increment bm_flush, thus closing the current |
| 31 | * batch. |
| 32 | * When we notice that bm_flush > bm_write, we write out all pending updates |
| 33 | * to the bitmap, and advance bm_write to where bm_flush was. |
| 34 | * This may occasionally write a bit out twice, but is sure never to |
| 35 | * miss any bits. |
| 36 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 38 | #include <linux/blkdev.h> |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 39 | #include <linux/kthread.h> |
Dan Williams | f701d58 | 2009-03-31 15:09:39 +1100 | [diff] [blame] | 40 | #include <linux/raid/pq.h> |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 41 | #include <linux/async_tx.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 42 | #include <linux/module.h> |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 43 | #include <linux/async.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 44 | #include <linux/seq_file.h> |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 45 | #include <linux/cpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 46 | #include <linux/slab.h> |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 47 | #include <linux/ratelimit.h> |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 48 | #include <linux/nodemask.h> |
Ingo Molnar | 3f07c01 | 2017-02-08 18:51:30 +0100 | [diff] [blame] | 49 | |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 50 | #include <trace/events/block.h> |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 51 | #include <linux/list_sort.h> |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 52 | |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 53 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 54 | #include "raid5.h" |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 55 | #include "raid0.h" |
Mike Snitzer | 935fe09 | 2017-10-10 17:02:41 -0400 | [diff] [blame] | 56 | #include "md-bitmap.h" |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 57 | #include "raid5-log.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 58 | |
Shaohua Li | 394ed8e | 2017-01-04 16:10:19 -0800 | [diff] [blame] | 59 | #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED) |
| 60 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 61 | #define cpu_to_group(cpu) cpu_to_node(cpu) |
| 62 | #define ANY_GROUP NUMA_NO_NODE |
| 63 | |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 64 | static bool devices_handle_discard_safely = false; |
| 65 | module_param(devices_handle_discard_safely, bool, 0644); |
| 66 | MODULE_PARM_DESC(devices_handle_discard_safely, |
| 67 | "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] | 68 | static struct workqueue_struct *raid5_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 69 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 70 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 71 | { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 72 | int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK; |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 73 | return &conf->stripe_hashtbl[hash]; |
| 74 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 76 | static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect) |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 77 | { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 78 | return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static inline void lock_device_hash_lock(struct r5conf *conf, int hash) |
| 82 | { |
| 83 | spin_lock_irq(conf->hash_locks + hash); |
| 84 | spin_lock(&conf->device_lock); |
| 85 | } |
| 86 | |
| 87 | static inline void unlock_device_hash_lock(struct r5conf *conf, int hash) |
| 88 | { |
| 89 | spin_unlock(&conf->device_lock); |
| 90 | spin_unlock_irq(conf->hash_locks + hash); |
| 91 | } |
| 92 | |
| 93 | static inline void lock_all_device_hash_locks_irq(struct r5conf *conf) |
| 94 | { |
| 95 | int i; |
Julia Cartwright | 3d05f3a | 2017-04-28 12:41:02 -0500 | [diff] [blame] | 96 | spin_lock_irq(conf->hash_locks); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 97 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 98 | spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks); |
| 99 | spin_lock(&conf->device_lock); |
| 100 | } |
| 101 | |
| 102 | static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf) |
| 103 | { |
| 104 | int i; |
| 105 | spin_unlock(&conf->device_lock); |
Julia Cartwright | 3d05f3a | 2017-04-28 12:41:02 -0500 | [diff] [blame] | 106 | for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--) |
| 107 | spin_unlock(conf->hash_locks + i); |
| 108 | spin_unlock_irq(conf->hash_locks); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 109 | } |
| 110 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 111 | /* Find first data disk in a raid6 stripe */ |
| 112 | static inline int raid6_d0(struct stripe_head *sh) |
| 113 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 114 | if (sh->ddf_layout) |
| 115 | /* ddf always start from first device */ |
| 116 | return 0; |
| 117 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 118 | if (sh->qd_idx == sh->disks - 1) |
| 119 | return 0; |
| 120 | else |
| 121 | return sh->qd_idx + 1; |
| 122 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 123 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 124 | { |
| 125 | disk++; |
| 126 | return (disk < raid_disks) ? disk : 0; |
| 127 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 128 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 129 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 130 | * We need to map each disk to a 'slot', where the data disks are slot |
| 131 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 132 | * is raid_disks-1. This help does that mapping. |
| 133 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 134 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 135 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 136 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 137 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 138 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 139 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 140 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 141 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 142 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 143 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 144 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 145 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 146 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 147 | return slot; |
| 148 | } |
| 149 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 150 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 152 | static int stripe_operations_active(struct stripe_head *sh) |
| 153 | { |
| 154 | return sh->check_state || sh->reconstruct_state || |
| 155 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 156 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 157 | } |
| 158 | |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 159 | static bool stripe_is_lowprio(struct stripe_head *sh) |
| 160 | { |
| 161 | return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) || |
| 162 | test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) && |
| 163 | !test_bit(STRIPE_R5C_CACHING, &sh->state); |
| 164 | } |
| 165 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 166 | static void raid5_wakeup_stripe_thread(struct stripe_head *sh) |
| 167 | { |
| 168 | struct r5conf *conf = sh->raid_conf; |
| 169 | struct r5worker_group *group; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 170 | int thread_cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 171 | int i, cpu = sh->cpu; |
| 172 | |
| 173 | if (!cpu_online(cpu)) { |
| 174 | cpu = cpumask_any(cpu_online_mask); |
| 175 | sh->cpu = cpu; |
| 176 | } |
| 177 | |
| 178 | if (list_empty(&sh->lru)) { |
| 179 | struct r5worker_group *group; |
| 180 | group = conf->worker_groups + cpu_to_group(cpu); |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 181 | if (stripe_is_lowprio(sh)) |
| 182 | list_add_tail(&sh->lru, &group->loprio_list); |
| 183 | else |
| 184 | list_add_tail(&sh->lru, &group->handle_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 185 | group->stripes_cnt++; |
| 186 | sh->group = group; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | if (conf->worker_cnt_per_group == 0) { |
| 190 | md_wakeup_thread(conf->mddev->thread); |
| 191 | return; |
| 192 | } |
| 193 | |
| 194 | group = conf->worker_groups + cpu_to_group(sh->cpu); |
| 195 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 196 | group->workers[0].working = true; |
| 197 | /* at least one worker should run to avoid race */ |
| 198 | queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work); |
| 199 | |
| 200 | thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1; |
| 201 | /* wakeup more workers */ |
| 202 | for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) { |
| 203 | if (group->workers[i].working == false) { |
| 204 | group->workers[i].working = true; |
| 205 | queue_work_on(sh->cpu, raid5_wq, |
| 206 | &group->workers[i].work); |
| 207 | thread_cnt--; |
| 208 | } |
| 209 | } |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 210 | } |
| 211 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 212 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 213 | struct list_head *temp_inactive_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 215 | int i; |
| 216 | int injournal = 0; /* number of date pages with R5_InJournal */ |
| 217 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 218 | BUG_ON(!list_empty(&sh->lru)); |
| 219 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 220 | |
| 221 | if (r5c_is_writeback(conf->log)) |
| 222 | for (i = sh->disks; i--; ) |
| 223 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 224 | injournal++; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 225 | /* |
Song Liu | 5ddf044 | 2017-05-11 17:03:44 -0700 | [diff] [blame] | 226 | * In the following cases, the stripe cannot be released to cached |
| 227 | * lists. Therefore, we make the stripe write out and set |
| 228 | * STRIPE_HANDLE: |
| 229 | * 1. when quiesce in r5c write back; |
| 230 | * 2. when resync is requested fot the stripe. |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 231 | */ |
Song Liu | 5ddf044 | 2017-05-11 17:03:44 -0700 | [diff] [blame] | 232 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) || |
| 233 | (conf->quiesce && r5c_is_writeback(conf->log) && |
| 234 | !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) { |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 235 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
| 236 | r5c_make_stripe_write_out(sh); |
| 237 | set_bit(STRIPE_HANDLE, &sh->state); |
| 238 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 239 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 240 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 241 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
Jes Sorensen | ad3ab8b | 2015-01-29 12:38:29 -0500 | [diff] [blame] | 242 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 243 | list_add_tail(&sh->lru, &conf->delayed_list); |
Jes Sorensen | ad3ab8b | 2015-01-29 12:38:29 -0500 | [diff] [blame] | 244 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 245 | sh->bm_seq - conf->seq_write > 0) |
| 246 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 247 | else { |
| 248 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 249 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 250 | if (conf->worker_cnt_per_group == 0) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 251 | if (stripe_is_lowprio(sh)) |
| 252 | list_add_tail(&sh->lru, |
| 253 | &conf->loprio_list); |
| 254 | else |
| 255 | list_add_tail(&sh->lru, |
| 256 | &conf->handle_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 257 | } else { |
| 258 | raid5_wakeup_stripe_thread(sh); |
| 259 | return; |
| 260 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 261 | } |
| 262 | md_wakeup_thread(conf->mddev->thread); |
| 263 | } else { |
| 264 | BUG_ON(stripe_operations_active(sh)); |
| 265 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 266 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 267 | < IO_THRESHOLD) |
| 268 | md_wakeup_thread(conf->mddev->thread); |
| 269 | atomic_dec(&conf->active_stripes); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 270 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 271 | if (!r5c_is_writeback(conf->log)) |
| 272 | list_add_tail(&sh->lru, temp_inactive_list); |
| 273 | else { |
| 274 | WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)); |
| 275 | if (injournal == 0) |
| 276 | list_add_tail(&sh->lru, temp_inactive_list); |
| 277 | else if (injournal == conf->raid_disks - conf->max_degraded) { |
| 278 | /* full stripe */ |
| 279 | if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) |
| 280 | atomic_inc(&conf->r5c_cached_full_stripes); |
| 281 | if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) |
| 282 | atomic_dec(&conf->r5c_cached_partial_stripes); |
| 283 | list_add_tail(&sh->lru, &conf->r5c_full_stripe_list); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 284 | r5c_check_cached_full_stripe(conf); |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 285 | } else |
| 286 | /* |
| 287 | * STRIPE_R5C_PARTIAL_STRIPE is set in |
| 288 | * r5c_try_caching_write(). No need to |
| 289 | * set it again. |
| 290 | */ |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 291 | list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 292 | } |
| 293 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 294 | } |
| 295 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 296 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 297 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 298 | struct list_head *temp_inactive_list) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 299 | { |
| 300 | if (atomic_dec_and_test(&sh->count)) |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 301 | do_release_stripe(conf, sh, temp_inactive_list); |
| 302 | } |
| 303 | |
| 304 | /* |
| 305 | * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list |
| 306 | * |
| 307 | * Be careful: Only one task can add/delete stripes from temp_inactive_list at |
| 308 | * given time. Adding stripes only takes device lock, while deleting stripes |
| 309 | * only takes hash lock. |
| 310 | */ |
| 311 | static void release_inactive_stripe_list(struct r5conf *conf, |
| 312 | struct list_head *temp_inactive_list, |
| 313 | int hash) |
| 314 | { |
| 315 | int size; |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 316 | bool do_wakeup = false; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 317 | unsigned long flags; |
| 318 | |
| 319 | if (hash == NR_STRIPE_HASH_LOCKS) { |
| 320 | size = NR_STRIPE_HASH_LOCKS; |
| 321 | hash = NR_STRIPE_HASH_LOCKS - 1; |
| 322 | } else |
| 323 | size = 1; |
| 324 | while (size) { |
| 325 | struct list_head *list = &temp_inactive_list[size - 1]; |
| 326 | |
| 327 | /* |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 328 | * 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] | 329 | * remove stripes from the list |
| 330 | */ |
| 331 | if (!list_empty_careful(list)) { |
| 332 | spin_lock_irqsave(conf->hash_locks + hash, flags); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 333 | if (list_empty(conf->inactive_list + hash) && |
| 334 | !list_empty(list)) |
| 335 | atomic_dec(&conf->empty_inactive_list_nr); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 336 | list_splice_tail_init(list, conf->inactive_list + hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 337 | do_wakeup = true; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 338 | spin_unlock_irqrestore(conf->hash_locks + hash, flags); |
| 339 | } |
| 340 | size--; |
| 341 | hash--; |
| 342 | } |
| 343 | |
| 344 | if (do_wakeup) { |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 345 | wake_up(&conf->wait_for_stripe); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 346 | if (atomic_read(&conf->active_stripes) == 0) |
| 347 | wake_up(&conf->wait_for_quiescent); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 348 | if (conf->retry_read_aligned) |
| 349 | md_wakeup_thread(conf->mddev->thread); |
| 350 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 351 | } |
| 352 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 353 | /* should hold conf->device_lock already */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 354 | static int release_stripe_list(struct r5conf *conf, |
| 355 | struct list_head *temp_inactive_list) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 356 | { |
Byungchul Park | eae8263 | 2017-02-14 16:26:24 +0900 | [diff] [blame] | 357 | struct stripe_head *sh, *t; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 358 | int count = 0; |
| 359 | struct llist_node *head; |
| 360 | |
| 361 | head = llist_del_all(&conf->released_stripes); |
Shaohua Li | d265d9d | 2013-08-28 14:29:05 +0800 | [diff] [blame] | 362 | head = llist_reverse_order(head); |
Byungchul Park | eae8263 | 2017-02-14 16:26:24 +0900 | [diff] [blame] | 363 | llist_for_each_entry_safe(sh, t, head, release_list) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 364 | int hash; |
| 365 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 366 | /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */ |
| 367 | smp_mb(); |
| 368 | clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state); |
| 369 | /* |
| 370 | * Don't worry the bit is set here, because if the bit is set |
| 371 | * again, the count is always > 1. This is true for |
| 372 | * STRIPE_ON_UNPLUG_LIST bit too. |
| 373 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 374 | hash = sh->hash_lock_index; |
| 375 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 376 | count++; |
| 377 | } |
| 378 | |
| 379 | return count; |
| 380 | } |
| 381 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 382 | void raid5_release_stripe(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 383 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 384 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 385 | unsigned long flags; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 386 | struct list_head list; |
| 387 | int hash; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 388 | bool wakeup; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 389 | |
Eivind Sarto | cf170f3 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 390 | /* Avoid release_list until the last reference. |
| 391 | */ |
| 392 | if (atomic_add_unless(&sh->count, -1, 1)) |
| 393 | return; |
| 394 | |
majianpeng | ad4068d | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 395 | if (unlikely(!conf->mddev->thread) || |
| 396 | test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state)) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 397 | goto slow_path; |
| 398 | wakeup = llist_add(&sh->release_list, &conf->released_stripes); |
| 399 | if (wakeup) |
| 400 | md_wakeup_thread(conf->mddev->thread); |
| 401 | return; |
| 402 | slow_path: |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 403 | /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */ |
Anna-Maria Gleixner | 685dbca | 2018-07-03 22:01:36 +0200 | [diff] [blame] | 404 | if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 405 | INIT_LIST_HEAD(&list); |
| 406 | hash = sh->hash_lock_index; |
| 407 | do_release_stripe(conf, sh, &list); |
Anna-Maria Gleixner | 08edaaa | 2018-07-03 22:01:37 +0200 | [diff] [blame] | 408 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 409 | release_inactive_stripe_list(conf, &list, hash); |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 410 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 411 | } |
| 412 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 413 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 414 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 415 | pr_debug("remove_hash(), stripe %llu\n", |
| 416 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 418 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 419 | } |
| 420 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 421 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 422 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 423 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 425 | pr_debug("insert_hash(), stripe %llu\n", |
| 426 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 427 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 428 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 432 | static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | { |
| 434 | struct stripe_head *sh = NULL; |
| 435 | struct list_head *first; |
| 436 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 437 | if (list_empty(conf->inactive_list + hash)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | goto out; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 439 | first = (conf->inactive_list + hash)->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | sh = list_entry(first, struct stripe_head, lru); |
| 441 | list_del_init(first); |
| 442 | remove_hash(sh); |
| 443 | atomic_inc(&conf->active_stripes); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 444 | BUG_ON(hash != sh->hash_lock_index); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 445 | if (list_empty(conf->inactive_list + hash)) |
| 446 | atomic_inc(&conf->empty_inactive_list_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 447 | out: |
| 448 | return sh; |
| 449 | } |
| 450 | |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 451 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 452 | static void free_stripe_pages(struct stripe_head *sh) |
| 453 | { |
| 454 | int i; |
| 455 | struct page *p; |
| 456 | |
| 457 | /* Have not allocate page pool */ |
| 458 | if (!sh->pages) |
| 459 | return; |
| 460 | |
| 461 | for (i = 0; i < sh->nr_pages; i++) { |
| 462 | p = sh->pages[i]; |
| 463 | if (p) |
| 464 | put_page(p); |
| 465 | sh->pages[i] = NULL; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | static int alloc_stripe_pages(struct stripe_head *sh, gfp_t gfp) |
| 470 | { |
| 471 | int i; |
| 472 | struct page *p; |
| 473 | |
| 474 | for (i = 0; i < sh->nr_pages; i++) { |
| 475 | /* The page have allocated. */ |
| 476 | if (sh->pages[i]) |
| 477 | continue; |
| 478 | |
| 479 | p = alloc_page(gfp); |
| 480 | if (!p) { |
| 481 | free_stripe_pages(sh); |
| 482 | return -ENOMEM; |
| 483 | } |
| 484 | sh->pages[i] = p; |
| 485 | } |
| 486 | return 0; |
| 487 | } |
| 488 | |
| 489 | static int |
| 490 | init_stripe_shared_pages(struct stripe_head *sh, struct r5conf *conf, int disks) |
| 491 | { |
| 492 | int nr_pages, cnt; |
| 493 | |
| 494 | if (sh->pages) |
| 495 | return 0; |
| 496 | |
| 497 | /* Each of the sh->dev[i] need one conf->stripe_size */ |
| 498 | cnt = PAGE_SIZE / conf->stripe_size; |
| 499 | nr_pages = (disks + cnt - 1) / cnt; |
| 500 | |
| 501 | sh->pages = kcalloc(nr_pages, sizeof(struct page *), GFP_KERNEL); |
| 502 | if (!sh->pages) |
| 503 | return -ENOMEM; |
| 504 | sh->nr_pages = nr_pages; |
| 505 | sh->stripes_per_page = cnt; |
| 506 | return 0; |
| 507 | } |
| 508 | #endif |
| 509 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 510 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 511 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 512 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 513 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 514 | |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 515 | #if PAGE_SIZE == DEFAULT_STRIPE_SIZE |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 516 | for (i = 0; i < num ; i++) { |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 517 | struct page *p; |
| 518 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 519 | WARN_ON(sh->dev[i].page != sh->dev[i].orig_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 520 | p = sh->dev[i].page; |
| 521 | if (!p) |
| 522 | continue; |
| 523 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 524 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 525 | } |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 526 | #else |
| 527 | for (i = 0; i < num; i++) |
| 528 | sh->dev[i].page = NULL; |
| 529 | free_stripe_pages(sh); /* Free pages */ |
| 530 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 531 | } |
| 532 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 533 | static int grow_buffers(struct stripe_head *sh, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 534 | { |
| 535 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 536 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 537 | |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 538 | #if PAGE_SIZE == DEFAULT_STRIPE_SIZE |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 539 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 540 | struct page *page; |
| 541 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 542 | if (!(page = alloc_page(gfp))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 543 | return 1; |
| 544 | } |
| 545 | sh->dev[i].page = page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 546 | sh->dev[i].orig_page = page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 547 | sh->dev[i].offset = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 548 | } |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 549 | #else |
| 550 | if (alloc_stripe_pages(sh, gfp)) |
| 551 | return -ENOMEM; |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 552 | |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 553 | for (i = 0; i < num; i++) { |
| 554 | sh->dev[i].page = raid5_get_dev_page(sh, i); |
| 555 | sh->dev[i].orig_page = sh->dev[i].page; |
| 556 | sh->dev[i].offset = raid5_get_page_offset(sh, i); |
| 557 | } |
| 558 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 559 | return 0; |
| 560 | } |
| 561 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 562 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 563 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 564 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 565 | 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] | 566 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 567 | struct r5conf *conf = sh->raid_conf; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 568 | int i, seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 569 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 570 | BUG_ON(atomic_read(&sh->count) != 0); |
| 571 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 572 | BUG_ON(stripe_operations_active(sh)); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 573 | BUG_ON(sh->batch_head); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 574 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 575 | pr_debug("init_stripe called, stripe %llu\n", |
Markus Stockhausen | b8e6a15 | 2014-08-23 20:19:27 +1000 | [diff] [blame] | 576 | (unsigned long long)sector); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 577 | retry: |
| 578 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 579 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 580 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 581 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 582 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 583 | sh->state = 0; |
| 584 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 585 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 586 | struct r5dev *dev = &sh->dev[i]; |
| 587 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 588 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 589 | test_bit(R5_LOCKED, &dev->flags)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 590 | pr_err("sector=%llx i=%d %p %p %p %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 591 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 592 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 593 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 594 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 595 | } |
| 596 | dev->flags = 0; |
Guoqing Jiang | 27a4ff8 | 2017-08-10 16:12:17 +0800 | [diff] [blame] | 597 | dev->sector = raid5_compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 598 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 599 | if (read_seqcount_retry(&conf->gen_lock, seq)) |
| 600 | goto retry; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 601 | sh->overwrite_disks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 602 | insert_hash(conf, sh); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 603 | sh->cpu = smp_processor_id(); |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 604 | set_bit(STRIPE_BATCH_READY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 605 | } |
| 606 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 607 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 608 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 609 | { |
| 610 | struct stripe_head *sh; |
| 611 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 612 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 613 | hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 614 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 615 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 616 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 617 | return NULL; |
| 618 | } |
| 619 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 620 | /* |
| 621 | * Need to check if array has failed when deciding whether to: |
| 622 | * - start an array |
| 623 | * - remove non-faulty devices |
| 624 | * - add a spare |
| 625 | * - allow a reshape |
| 626 | * This determination is simple when no reshape is happening. |
| 627 | * However if there is a reshape, we need to carefully check |
| 628 | * both the before and after sections. |
| 629 | * This is because some failed devices may only affect one |
| 630 | * of the two sections, and some non-in_sync devices may |
| 631 | * be insync in the section most affected by failed devices. |
| 632 | */ |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 633 | int raid5_calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 634 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 635 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 636 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 637 | |
| 638 | rcu_read_lock(); |
| 639 | degraded = 0; |
| 640 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 641 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 642 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 643 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 644 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 645 | degraded++; |
| 646 | else if (test_bit(In_sync, &rdev->flags)) |
| 647 | ; |
| 648 | else |
| 649 | /* not in-sync or faulty. |
| 650 | * If the reshape increases the number of devices, |
| 651 | * this is being recovered by the reshape, so |
| 652 | * this 'previous' section is not in_sync. |
| 653 | * If the number of devices is being reduced however, |
| 654 | * the device can only be part of the array if |
| 655 | * we are reverting a reshape, so this section will |
| 656 | * be in-sync. |
| 657 | */ |
| 658 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 659 | degraded++; |
| 660 | } |
| 661 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 662 | if (conf->raid_disks == conf->previous_raid_disks) |
| 663 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 664 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 665 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 666 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 667 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 668 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 669 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 670 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 671 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 672 | else if (test_bit(In_sync, &rdev->flags)) |
| 673 | ; |
| 674 | else |
| 675 | /* not in-sync or faulty. |
| 676 | * If reshape increases the number of devices, this |
| 677 | * section has already been recovered, else it |
| 678 | * almost certainly hasn't. |
| 679 | */ |
| 680 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 681 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 682 | } |
| 683 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 684 | if (degraded2 > degraded) |
| 685 | return degraded2; |
| 686 | return degraded; |
| 687 | } |
| 688 | |
| 689 | static int has_failed(struct r5conf *conf) |
| 690 | { |
| 691 | int degraded; |
| 692 | |
| 693 | if (conf->mddev->reshape_position == MaxSector) |
| 694 | return conf->mddev->degraded > conf->max_degraded; |
| 695 | |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 696 | degraded = raid5_calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 697 | if (degraded > conf->max_degraded) |
| 698 | return 1; |
| 699 | return 0; |
| 700 | } |
| 701 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 702 | struct stripe_head * |
| 703 | raid5_get_active_stripe(struct r5conf *conf, sector_t sector, |
| 704 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 705 | { |
| 706 | struct stripe_head *sh; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 707 | int hash = stripe_hash_locks_hash(conf, sector); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 708 | int inc_empty_inactive_list_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 709 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 710 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 711 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 712 | spin_lock_irq(conf->hash_locks + hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 713 | |
| 714 | do { |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 715 | wait_event_lock_irq(conf->wait_for_quiescent, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 716 | conf->quiesce == 0 || noquiesce, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 717 | *(conf->hash_locks + hash)); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 718 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 719 | if (!sh) { |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 720 | if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 721 | sh = get_free_stripe(conf, hash); |
Shaohua Li | 713bc5c | 2015-05-28 17:33:47 -0700 | [diff] [blame] | 722 | if (!sh && !test_bit(R5_DID_ALLOC, |
| 723 | &conf->cache_state)) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 724 | set_bit(R5_ALLOC_MORE, |
| 725 | &conf->cache_state); |
| 726 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 727 | if (noblock && sh == NULL) |
| 728 | break; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 729 | |
| 730 | r5c_check_stripe_cache_usage(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | if (!sh) { |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 732 | set_bit(R5_INACTIVE_BLOCKED, |
| 733 | &conf->cache_state); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 734 | r5l_wake_reclaim(conf->log, 0); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 735 | wait_event_lock_irq( |
| 736 | conf->wait_for_stripe, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 737 | !list_empty(conf->inactive_list + hash) && |
| 738 | (atomic_read(&conf->active_stripes) |
| 739 | < (conf->max_nr_stripes * 3 / 4) |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 740 | || !test_bit(R5_INACTIVE_BLOCKED, |
| 741 | &conf->cache_state)), |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 742 | *(conf->hash_locks + hash)); |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 743 | clear_bit(R5_INACTIVE_BLOCKED, |
| 744 | &conf->cache_state); |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 745 | } else { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 746 | init_stripe(sh, sector, previous); |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 747 | atomic_inc(&sh->count); |
| 748 | } |
Shaohua Li | e240c18 | 2014-04-09 11:27:42 +0800 | [diff] [blame] | 749 | } else if (!atomic_inc_not_zero(&sh->count)) { |
NeilBrown | 6d183de | 2013-11-28 10:55:27 +1100 | [diff] [blame] | 750 | spin_lock(&conf->device_lock); |
Shaohua Li | e240c18 | 2014-04-09 11:27:42 +0800 | [diff] [blame] | 751 | if (!atomic_read(&sh->count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 752 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 753 | atomic_inc(&conf->active_stripes); |
NeilBrown | 5af9bef | 2014-01-14 15:16:10 +1100 | [diff] [blame] | 754 | BUG_ON(list_empty(&sh->lru) && |
| 755 | !test_bit(STRIPE_EXPANDING, &sh->state)); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 756 | inc_empty_inactive_list_flag = 0; |
| 757 | if (!list_empty(conf->inactive_list + hash)) |
| 758 | inc_empty_inactive_list_flag = 1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 759 | list_del_init(&sh->lru); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 760 | if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) |
| 761 | atomic_inc(&conf->empty_inactive_list_nr); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 762 | if (sh->group) { |
| 763 | sh->group->stripes_cnt--; |
| 764 | sh->group = NULL; |
| 765 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 766 | } |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 767 | atomic_inc(&sh->count); |
NeilBrown | 6d183de | 2013-11-28 10:55:27 +1100 | [diff] [blame] | 768 | spin_unlock(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | } |
| 770 | } while (sh == NULL); |
| 771 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 772 | spin_unlock_irq(conf->hash_locks + hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 773 | return sh; |
| 774 | } |
| 775 | |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 776 | static bool is_full_stripe_write(struct stripe_head *sh) |
| 777 | { |
| 778 | BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded)); |
| 779 | return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded); |
| 780 | } |
| 781 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 782 | static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) |
Christoph Hellwig | 368ecad | 2019-04-04 18:56:15 +0200 | [diff] [blame] | 783 | __acquires(&sh1->stripe_lock) |
| 784 | __acquires(&sh2->stripe_lock) |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 785 | { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 786 | if (sh1 > sh2) { |
Julia Cartwright | 3d05f3a | 2017-04-28 12:41:02 -0500 | [diff] [blame] | 787 | spin_lock_irq(&sh2->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 788 | spin_lock_nested(&sh1->stripe_lock, 1); |
| 789 | } else { |
Julia Cartwright | 3d05f3a | 2017-04-28 12:41:02 -0500 | [diff] [blame] | 790 | spin_lock_irq(&sh1->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 791 | spin_lock_nested(&sh2->stripe_lock, 1); |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) |
Christoph Hellwig | 368ecad | 2019-04-04 18:56:15 +0200 | [diff] [blame] | 796 | __releases(&sh1->stripe_lock) |
| 797 | __releases(&sh2->stripe_lock) |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 798 | { |
| 799 | spin_unlock(&sh1->stripe_lock); |
Julia Cartwright | 3d05f3a | 2017-04-28 12:41:02 -0500 | [diff] [blame] | 800 | spin_unlock_irq(&sh2->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* Only freshly new full stripe normal write stripe can be added to a batch list */ |
| 804 | static bool stripe_can_batch(struct stripe_head *sh) |
| 805 | { |
Shaohua Li | 9c3e333 | 2015-08-13 14:32:02 -0700 | [diff] [blame] | 806 | struct r5conf *conf = sh->raid_conf; |
| 807 | |
Shaohua Li | e254de6 | 2018-08-29 11:05:42 -0700 | [diff] [blame] | 808 | if (raid5_has_log(conf) || raid5_has_ppl(conf)) |
Shaohua Li | 9c3e333 | 2015-08-13 14:32:02 -0700 | [diff] [blame] | 809 | return false; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 810 | return test_bit(STRIPE_BATCH_READY, &sh->state) && |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 811 | !test_bit(STRIPE_BITMAP_PENDING, &sh->state) && |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 812 | is_full_stripe_write(sh); |
| 813 | } |
| 814 | |
| 815 | /* we only do back search */ |
| 816 | static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh) |
| 817 | { |
| 818 | struct stripe_head *head; |
| 819 | sector_t head_sector, tmp_sec; |
| 820 | int hash; |
| 821 | int dd_idx; |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 822 | int inc_empty_inactive_list_flag; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 823 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 824 | /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */ |
| 825 | tmp_sec = sh->sector; |
| 826 | if (!sector_div(tmp_sec, conf->chunk_sectors)) |
| 827 | return; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 828 | head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 829 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 830 | hash = stripe_hash_locks_hash(conf, head_sector); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 831 | spin_lock_irq(conf->hash_locks + hash); |
| 832 | head = __find_stripe(conf, head_sector, conf->generation); |
| 833 | if (head && !atomic_inc_not_zero(&head->count)) { |
| 834 | spin_lock(&conf->device_lock); |
| 835 | if (!atomic_read(&head->count)) { |
| 836 | if (!test_bit(STRIPE_HANDLE, &head->state)) |
| 837 | atomic_inc(&conf->active_stripes); |
| 838 | BUG_ON(list_empty(&head->lru) && |
| 839 | !test_bit(STRIPE_EXPANDING, &head->state)); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 840 | inc_empty_inactive_list_flag = 0; |
| 841 | if (!list_empty(conf->inactive_list + hash)) |
| 842 | inc_empty_inactive_list_flag = 1; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 843 | list_del_init(&head->lru); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 844 | if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) |
| 845 | atomic_inc(&conf->empty_inactive_list_nr); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 846 | if (head->group) { |
| 847 | head->group->stripes_cnt--; |
| 848 | head->group = NULL; |
| 849 | } |
| 850 | } |
| 851 | atomic_inc(&head->count); |
| 852 | spin_unlock(&conf->device_lock); |
| 853 | } |
| 854 | spin_unlock_irq(conf->hash_locks + hash); |
| 855 | |
| 856 | if (!head) |
| 857 | return; |
| 858 | if (!stripe_can_batch(head)) |
| 859 | goto out; |
| 860 | |
| 861 | lock_two_stripes(head, sh); |
| 862 | /* clear_batch_ready clear the flag */ |
| 863 | if (!stripe_can_batch(head) || !stripe_can_batch(sh)) |
| 864 | goto unlock_out; |
| 865 | |
| 866 | if (sh->batch_head) |
| 867 | goto unlock_out; |
| 868 | |
| 869 | dd_idx = 0; |
| 870 | while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) |
| 871 | dd_idx++; |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 872 | 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] | 873 | 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] | 874 | goto unlock_out; |
| 875 | |
| 876 | if (head->batch_head) { |
| 877 | spin_lock(&head->batch_head->batch_lock); |
| 878 | /* This batch list is already running */ |
| 879 | if (!stripe_can_batch(head)) { |
| 880 | spin_unlock(&head->batch_head->batch_lock); |
| 881 | goto unlock_out; |
| 882 | } |
Shaohua Li | 3664847 | 2017-08-25 10:40:02 -0700 | [diff] [blame] | 883 | /* |
| 884 | * We must assign batch_head of this stripe within the |
| 885 | * batch_lock, otherwise clear_batch_ready of batch head |
| 886 | * stripe could clear BATCH_READY bit of this stripe and |
| 887 | * this stripe->batch_head doesn't get assigned, which |
| 888 | * could confuse clear_batch_ready for this stripe |
| 889 | */ |
| 890 | sh->batch_head = head->batch_head; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 891 | |
| 892 | /* |
| 893 | * at this point, head's BATCH_READY could be cleared, but we |
| 894 | * can still add the stripe to batch list |
| 895 | */ |
| 896 | list_add(&sh->batch_list, &head->batch_list); |
| 897 | spin_unlock(&head->batch_head->batch_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 898 | } else { |
| 899 | head->batch_head = head; |
| 900 | sh->batch_head = head->batch_head; |
| 901 | spin_lock(&head->batch_lock); |
| 902 | list_add_tail(&sh->batch_list, &head->batch_list); |
| 903 | spin_unlock(&head->batch_lock); |
| 904 | } |
| 905 | |
| 906 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 907 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 908 | < IO_THRESHOLD) |
| 909 | md_wakeup_thread(conf->mddev->thread); |
| 910 | |
NeilBrown | 2b6b245 | 2015-05-21 15:10:01 +1000 | [diff] [blame] | 911 | if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) { |
| 912 | int seq = sh->bm_seq; |
| 913 | if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) && |
| 914 | sh->batch_head->bm_seq > seq) |
| 915 | seq = sh->batch_head->bm_seq; |
| 916 | set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state); |
| 917 | sh->batch_head->bm_seq = seq; |
| 918 | } |
| 919 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 920 | atomic_inc(&sh->count); |
| 921 | unlock_out: |
| 922 | unlock_two_stripes(head, sh); |
| 923 | out: |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 924 | raid5_release_stripe(head); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 925 | } |
| 926 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 927 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 928 | * in this stripe_head. |
| 929 | */ |
| 930 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 931 | { |
| 932 | sector_t progress = conf->reshape_progress; |
| 933 | /* Need a memory barrier to make sure we see the value |
| 934 | * of conf->generation, or ->data_offset that was set before |
| 935 | * reshape_progress was updated. |
| 936 | */ |
| 937 | smp_rmb(); |
| 938 | if (progress == MaxSector) |
| 939 | return 0; |
| 940 | if (sh->generation == conf->generation - 1) |
| 941 | return 0; |
| 942 | /* We are in a reshape, and this is a new-generation stripe, |
| 943 | * so use new_data_offset. |
| 944 | */ |
| 945 | return 1; |
| 946 | } |
| 947 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 948 | static void dispatch_bio_list(struct bio_list *tmp) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 949 | { |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 950 | struct bio *bio; |
| 951 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 952 | while ((bio = bio_list_pop(tmp))) |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 953 | submit_bio_noacct(bio); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 954 | } |
| 955 | |
Sami Tolvanen | 4f0f586 | 2021-04-08 11:28:34 -0700 | [diff] [blame] | 956 | static int cmp_stripe(void *priv, const struct list_head *a, |
| 957 | const struct list_head *b) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 958 | { |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 959 | const struct r5pending_data *da = list_entry(a, |
| 960 | struct r5pending_data, sibling); |
| 961 | const struct r5pending_data *db = list_entry(b, |
| 962 | struct r5pending_data, sibling); |
| 963 | if (da->sector > db->sector) |
| 964 | return 1; |
| 965 | if (da->sector < db->sector) |
| 966 | return -1; |
| 967 | return 0; |
| 968 | } |
| 969 | |
| 970 | static void dispatch_defer_bios(struct r5conf *conf, int target, |
| 971 | struct bio_list *list) |
| 972 | { |
| 973 | struct r5pending_data *data; |
| 974 | struct list_head *first, *next = NULL; |
| 975 | int cnt = 0; |
| 976 | |
| 977 | if (conf->pending_data_cnt == 0) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 978 | return; |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 979 | |
| 980 | list_sort(NULL, &conf->pending_list, cmp_stripe); |
| 981 | |
| 982 | first = conf->pending_list.next; |
| 983 | |
| 984 | /* temporarily move the head */ |
| 985 | if (conf->next_pending_data) |
| 986 | list_move_tail(&conf->pending_list, |
| 987 | &conf->next_pending_data->sibling); |
| 988 | |
| 989 | while (!list_empty(&conf->pending_list)) { |
| 990 | data = list_first_entry(&conf->pending_list, |
| 991 | struct r5pending_data, sibling); |
| 992 | if (&data->sibling == first) |
| 993 | first = data->sibling.next; |
| 994 | next = data->sibling.next; |
| 995 | |
| 996 | bio_list_merge(list, &data->bios); |
| 997 | list_move(&data->sibling, &conf->free_list); |
| 998 | cnt++; |
| 999 | if (cnt >= target) |
| 1000 | break; |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1001 | } |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1002 | conf->pending_data_cnt -= cnt; |
| 1003 | BUG_ON(conf->pending_data_cnt < 0 || cnt < target); |
| 1004 | |
| 1005 | if (next != &conf->pending_list) |
| 1006 | conf->next_pending_data = list_entry(next, |
| 1007 | struct r5pending_data, sibling); |
| 1008 | else |
| 1009 | conf->next_pending_data = NULL; |
| 1010 | /* list isn't empty */ |
| 1011 | if (first != &conf->pending_list) |
| 1012 | list_move_tail(&conf->pending_list, first); |
| 1013 | } |
| 1014 | |
| 1015 | static void flush_deferred_bios(struct r5conf *conf) |
| 1016 | { |
| 1017 | struct bio_list tmp = BIO_EMPTY_LIST; |
| 1018 | |
| 1019 | if (conf->pending_data_cnt == 0) |
| 1020 | return; |
| 1021 | |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1022 | spin_lock(&conf->pending_bios_lock); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1023 | dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp); |
| 1024 | BUG_ON(conf->pending_data_cnt != 0); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1025 | spin_unlock(&conf->pending_bios_lock); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1026 | |
| 1027 | dispatch_bio_list(&tmp); |
| 1028 | } |
| 1029 | |
| 1030 | static void defer_issue_bios(struct r5conf *conf, sector_t sector, |
| 1031 | struct bio_list *bios) |
| 1032 | { |
| 1033 | struct bio_list tmp = BIO_EMPTY_LIST; |
| 1034 | struct r5pending_data *ent; |
| 1035 | |
| 1036 | spin_lock(&conf->pending_bios_lock); |
| 1037 | ent = list_first_entry(&conf->free_list, struct r5pending_data, |
| 1038 | sibling); |
| 1039 | list_move_tail(&ent->sibling, &conf->pending_list); |
| 1040 | ent->sector = sector; |
| 1041 | bio_list_init(&ent->bios); |
| 1042 | bio_list_merge(&ent->bios, bios); |
| 1043 | conf->pending_data_cnt++; |
| 1044 | if (conf->pending_data_cnt >= PENDING_IO_MAX) |
| 1045 | dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp); |
| 1046 | |
| 1047 | spin_unlock(&conf->pending_bios_lock); |
| 1048 | |
| 1049 | dispatch_bio_list(&tmp); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1050 | } |
| 1051 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1052 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1053 | raid5_end_read_request(struct bio *bi); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1054 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1055 | raid5_end_write_request(struct bio *bi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1056 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 1057 | 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] | 1058 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1059 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1060 | int i, disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1061 | struct stripe_head *head_sh = sh; |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1062 | struct bio_list pending_bios = BIO_EMPTY_LIST; |
| 1063 | bool should_defer; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1064 | |
| 1065 | might_sleep(); |
| 1066 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 1067 | if (log_stripe(sh, s) == 0) |
| 1068 | return; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1069 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1070 | should_defer = conf->batch_bio_dispatch && conf->group_cnt; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1071 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1072 | for (i = disks; i--; ) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1073 | int op, op_flags = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1074 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1075 | struct bio *bi, *rbi; |
| 1076 | struct md_rdev *rdev, *rrdev = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1077 | |
| 1078 | sh = head_sh; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1079 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1080 | op = REQ_OP_WRITE; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1081 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 1082 | op_flags = REQ_FUA; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1083 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1084 | op = REQ_OP_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1085 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1086 | op = REQ_OP_READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1087 | else if (test_and_clear_bit(R5_WantReplace, |
| 1088 | &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1089 | op = REQ_OP_WRITE; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1090 | replace_only = 1; |
| 1091 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1092 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1093 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1094 | op_flags |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1095 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1096 | again: |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1097 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1098 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1099 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1100 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1101 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1102 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 1103 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 1104 | if (!rdev) { |
| 1105 | rdev = rrdev; |
| 1106 | rrdev = NULL; |
| 1107 | } |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1108 | if (op_is_write(op)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1109 | if (replace_only) |
| 1110 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1111 | if (rdev == rrdev) |
| 1112 | /* We raced and saw duplicates */ |
| 1113 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1114 | } else { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1115 | if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1116 | rdev = rrdev; |
| 1117 | rrdev = NULL; |
| 1118 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1119 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1120 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 1121 | rdev = NULL; |
| 1122 | if (rdev) |
| 1123 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1124 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 1125 | rrdev = NULL; |
| 1126 | if (rrdev) |
| 1127 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1128 | rcu_read_unlock(); |
| 1129 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1130 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1131 | * need to check for writes. We never accept write errors |
| 1132 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1133 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1134 | while (op_is_write(op) && rdev && |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1135 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 1136 | sector_t first_bad; |
| 1137 | int bad_sectors; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1138 | int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1139 | &first_bad, &bad_sectors); |
| 1140 | if (!bad) |
| 1141 | break; |
| 1142 | |
| 1143 | if (bad < 0) { |
| 1144 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 1145 | if (!conf->mddev->external && |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1146 | conf->mddev->sb_flags) { |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1147 | /* It is very unlikely, but we might |
| 1148 | * still need to write out the |
| 1149 | * bad block log - better give it |
| 1150 | * a chance*/ |
| 1151 | md_check_recovery(conf->mddev); |
| 1152 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 1153 | /* |
| 1154 | * Because md_wait_for_blocked_rdev |
| 1155 | * will dec nr_pending, we must |
| 1156 | * increment it first. |
| 1157 | */ |
| 1158 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1159 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 1160 | } else { |
| 1161 | /* Acknowledged bad block - skip the write */ |
| 1162 | rdev_dec_pending(rdev, conf->mddev); |
| 1163 | rdev = NULL; |
| 1164 | } |
| 1165 | } |
| 1166 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1167 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1168 | if (s->syncing || s->expanding || s->expanded |
| 1169 | || s->replacing) |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1170 | md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1171 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 1172 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1173 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1174 | bio_set_dev(bi, rdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1175 | bio_set_op_attrs(bi, op, op_flags); |
| 1176 | bi->bi_end_io = op_is_write(op) |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1177 | ? raid5_end_write_request |
| 1178 | : raid5_end_read_request; |
| 1179 | bi->bi_private = sh; |
| 1180 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1181 | pr_debug("%s: for %llu schedule op %d on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1182 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1183 | bi->bi_opf, i); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1184 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1185 | if (sh != head_sh) |
| 1186 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1187 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1188 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1189 | + rdev->new_data_offset); |
| 1190 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1191 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1192 | + rdev->data_offset); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1193 | if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags)) |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1194 | bi->bi_opf |= REQ_NOMERGE; |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1195 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1196 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1197 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 1198 | |
| 1199 | if (!op_is_write(op) && |
| 1200 | test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 1201 | /* |
| 1202 | * issuing read for a page in journal, this |
| 1203 | * must be preparing for prexor in rmw; read |
| 1204 | * the data into orig_page |
| 1205 | */ |
| 1206 | sh->dev[i].vec.bv_page = sh->dev[i].orig_page; |
| 1207 | else |
| 1208 | sh->dev[i].vec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1209 | bi->bi_vcnt = 1; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1210 | bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf); |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1211 | bi->bi_io_vec[0].bv_offset = sh->dev[i].offset; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1212 | bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf); |
Mariusz Dabrowski | 2cd259a | 2018-04-19 19:28:10 +0200 | [diff] [blame] | 1213 | bi->bi_write_hint = sh->dev[i].write_hint; |
| 1214 | if (!rrdev) |
Eugene Syromiatnikov | f193489 | 2019-09-20 17:58:28 +0200 | [diff] [blame] | 1215 | sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1216 | /* |
| 1217 | * If this is discard request, set bi_vcnt 0. We don't |
| 1218 | * want to confuse SCSI because SCSI will replace payload |
| 1219 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1220 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1221 | bi->bi_vcnt = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1222 | if (rrdev) |
| 1223 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1224 | |
| 1225 | if (conf->mddev->gendisk) |
Christoph Hellwig | 1c02fca | 2020-12-03 17:21:38 +0100 | [diff] [blame] | 1226 | trace_block_bio_remap(bi, |
| 1227 | disk_devt(conf->mddev->gendisk), |
| 1228 | sh->dev[i].sector); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1229 | if (should_defer && op_is_write(op)) |
| 1230 | bio_list_add(&pending_bios, bi); |
| 1231 | else |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 1232 | submit_bio_noacct(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1233 | } |
| 1234 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1235 | if (s->syncing || s->expanding || s->expanded |
| 1236 | || s->replacing) |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1237 | md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf)); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1238 | |
| 1239 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1240 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1241 | bio_set_dev(rbi, rrdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1242 | bio_set_op_attrs(rbi, op, op_flags); |
| 1243 | BUG_ON(!op_is_write(op)); |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1244 | rbi->bi_end_io = raid5_end_write_request; |
| 1245 | rbi->bi_private = sh; |
| 1246 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1247 | pr_debug("%s: for %llu schedule op %d on " |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1248 | "replacement disc %d\n", |
| 1249 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1250 | rbi->bi_opf, i); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1251 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1252 | if (sh != head_sh) |
| 1253 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1254 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1255 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1256 | + rrdev->new_data_offset); |
| 1257 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1258 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1259 | + rrdev->data_offset); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1260 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1261 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 1262 | sh->dev[i].rvec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1263 | rbi->bi_vcnt = 1; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1264 | rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf); |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1265 | rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1266 | rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf); |
Mariusz Dabrowski | 2cd259a | 2018-04-19 19:28:10 +0200 | [diff] [blame] | 1267 | rbi->bi_write_hint = sh->dev[i].write_hint; |
Eugene Syromiatnikov | f193489 | 2019-09-20 17:58:28 +0200 | [diff] [blame] | 1268 | sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1269 | /* |
| 1270 | * If this is discard request, set bi_vcnt 0. We don't |
| 1271 | * want to confuse SCSI because SCSI will replace payload |
| 1272 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1273 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1274 | rbi->bi_vcnt = 0; |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1275 | if (conf->mddev->gendisk) |
Christoph Hellwig | 1c02fca | 2020-12-03 17:21:38 +0100 | [diff] [blame] | 1276 | trace_block_bio_remap(rbi, |
| 1277 | disk_devt(conf->mddev->gendisk), |
| 1278 | sh->dev[i].sector); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1279 | if (should_defer && op_is_write(op)) |
| 1280 | bio_list_add(&pending_bios, rbi); |
| 1281 | else |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 1282 | submit_bio_noacct(rbi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1283 | } |
| 1284 | if (!rdev && !rrdev) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1285 | if (op_is_write(op)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1286 | set_bit(STRIPE_DEGRADED, &sh->state); |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1287 | pr_debug("skip op %d on disc %d for sector %llu\n", |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1288 | bi->bi_opf, i, (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1289 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1290 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1291 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1292 | |
| 1293 | if (!head_sh->batch_head) |
| 1294 | continue; |
| 1295 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1296 | batch_list); |
| 1297 | if (sh != head_sh) |
| 1298 | goto again; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1299 | } |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1300 | |
| 1301 | if (should_defer && !bio_list_empty(&pending_bios)) |
| 1302 | defer_issue_bios(conf, head_sh->sector, &pending_bios); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1303 | } |
| 1304 | |
| 1305 | static struct dma_async_tx_descriptor * |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1306 | async_copy_data(int frombio, struct bio *bio, struct page **page, |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1307 | unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1308 | struct stripe_head *sh, int no_skipcopy) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1309 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1310 | struct bio_vec bvl; |
| 1311 | struct bvec_iter iter; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1312 | struct page *bio_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1313 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1314 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1315 | enum async_tx_flags flags = 0; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1316 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1317 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1318 | if (bio->bi_iter.bi_sector >= sector) |
| 1319 | page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1320 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1321 | page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1322 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1323 | if (frombio) |
| 1324 | flags |= ASYNC_TX_FENCE; |
| 1325 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 1326 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1327 | bio_for_each_segment(bvl, bio, iter) { |
| 1328 | int len = bvl.bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1329 | int clen; |
| 1330 | int b_offset = 0; |
| 1331 | |
| 1332 | if (page_offset < 0) { |
| 1333 | b_offset = -page_offset; |
| 1334 | page_offset += b_offset; |
| 1335 | len -= b_offset; |
| 1336 | } |
| 1337 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1338 | if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf)) |
| 1339 | clen = RAID5_STRIPE_SIZE(conf) - page_offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1340 | else |
| 1341 | clen = len; |
| 1342 | |
| 1343 | if (clen > 0) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1344 | b_offset += bvl.bv_offset; |
| 1345 | bio_page = bvl.bv_page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1346 | if (frombio) { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1347 | if (conf->skip_copy && |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1348 | b_offset == 0 && page_offset == 0 && |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1349 | clen == RAID5_STRIPE_SIZE(conf) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1350 | !no_skipcopy) |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1351 | *page = bio_page; |
| 1352 | else |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1353 | tx = async_memcpy(*page, bio_page, page_offset + poff, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1354 | b_offset, clen, &submit); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1355 | } else |
| 1356 | tx = async_memcpy(bio_page, *page, b_offset, |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1357 | page_offset + poff, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1358 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1359 | /* chain the operations */ |
| 1360 | submit.depend_tx = tx; |
| 1361 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1362 | if (clen < len) /* hit end of page */ |
| 1363 | break; |
| 1364 | page_offset += len; |
| 1365 | } |
| 1366 | |
| 1367 | return tx; |
| 1368 | } |
| 1369 | |
| 1370 | static void ops_complete_biofill(void *stripe_head_ref) |
| 1371 | { |
| 1372 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1373 | int i; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1374 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1375 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1376 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1377 | (unsigned long long)sh->sector); |
| 1378 | |
| 1379 | /* clear completed biofills */ |
| 1380 | for (i = sh->disks; i--; ) { |
| 1381 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1382 | |
| 1383 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1384 | /* and check if we need to reply to a read request, |
| 1385 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1386 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1387 | */ |
| 1388 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1389 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1390 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1391 | BUG_ON(!dev->read); |
| 1392 | rbi = dev->read; |
| 1393 | dev->read = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1394 | while (rbi && rbi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1395 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
| 1396 | rbi2 = r5_next_bio(conf, rbi, dev->sector); |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 1397 | bio_endio(rbi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1398 | rbi = rbi2; |
| 1399 | } |
| 1400 | } |
| 1401 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1402 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1403 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1404 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1405 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1406 | } |
| 1407 | |
| 1408 | static void ops_run_biofill(struct stripe_head *sh) |
| 1409 | { |
| 1410 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1411 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1412 | int i; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1413 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1414 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1415 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1416 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1417 | (unsigned long long)sh->sector); |
| 1418 | |
| 1419 | for (i = sh->disks; i--; ) { |
| 1420 | struct r5dev *dev = &sh->dev[i]; |
| 1421 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 1422 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1423 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1424 | dev->read = rbi = dev->toread; |
| 1425 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1426 | spin_unlock_irq(&sh->stripe_lock); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1427 | while (rbi && rbi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1428 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1429 | tx = async_copy_data(0, rbi, &dev->page, |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1430 | dev->offset, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1431 | dev->sector, tx, sh, 0); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1432 | rbi = r5_next_bio(conf, rbi, dev->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1433 | } |
| 1434 | } |
| 1435 | } |
| 1436 | |
| 1437 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1438 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 1439 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1440 | } |
| 1441 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1442 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 1443 | { |
| 1444 | struct r5dev *tgt; |
| 1445 | |
| 1446 | if (target < 0) |
| 1447 | return; |
| 1448 | |
| 1449 | tgt = &sh->dev[target]; |
| 1450 | set_bit(R5_UPTODATE, &tgt->flags); |
| 1451 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1452 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 1453 | } |
| 1454 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1455 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1456 | { |
| 1457 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1458 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1459 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1460 | (unsigned long long)sh->sector); |
| 1461 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1462 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1463 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1464 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1465 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1466 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 1467 | if (sh->check_state == check_state_compute_run) |
| 1468 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1469 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1470 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1471 | } |
| 1472 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1473 | /* return a pointer to the address conversion region of the scribble buffer */ |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 1474 | static struct page **to_addr_page(struct raid5_percpu *percpu, int i) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1475 | { |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 1476 | return percpu->scribble + i * percpu->scribble_obj_size; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1477 | } |
| 1478 | |
| 1479 | /* return a pointer to the address conversion region of the scribble buffer */ |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 1480 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 1481 | struct raid5_percpu *percpu, int i) |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1482 | { |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 1483 | return (void *) (to_addr_page(percpu, i) + sh->disks + 2); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1484 | } |
| 1485 | |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1486 | /* |
| 1487 | * Return a pointer to record offset address. |
| 1488 | */ |
| 1489 | static unsigned int * |
| 1490 | to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1491 | { |
| 1492 | return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2); |
| 1493 | } |
| 1494 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1495 | static struct dma_async_tx_descriptor * |
| 1496 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1497 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1498 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1499 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1500 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1501 | int target = sh->ops.target; |
| 1502 | struct r5dev *tgt = &sh->dev[target]; |
| 1503 | struct page *xor_dest = tgt->page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1504 | unsigned int off_dest = tgt->offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1505 | int count = 0; |
| 1506 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1507 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1508 | int i; |
| 1509 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1510 | BUG_ON(sh->batch_head); |
| 1511 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1512 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1513 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1514 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1515 | |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1516 | for (i = disks; i--; ) { |
| 1517 | if (i != target) { |
| 1518 | off_srcs[count] = sh->dev[i].offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1519 | xor_srcs[count++] = sh->dev[i].page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1520 | } |
| 1521 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1522 | |
| 1523 | atomic_inc(&sh->count); |
| 1524 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1525 | 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] | 1526 | ops_complete_compute, sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1527 | if (unlikely(count == 1)) |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1528 | tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0], |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1529 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1530 | else |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1531 | tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1532 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1533 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1534 | return tx; |
| 1535 | } |
| 1536 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1537 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 1538 | * @srcs - (struct page *) array of size sh->disks |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1539 | * @offs - (unsigned int) array of offset for each page |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1540 | * @sh - stripe_head to parse |
| 1541 | * |
| 1542 | * Populates srcs in proper layout order for the stripe and returns the |
| 1543 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 1544 | * destination buffer is recorded in srcs[count] and the Q destination |
| 1545 | * is recorded in srcs[count+1]]. |
| 1546 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1547 | static int set_syndrome_sources(struct page **srcs, |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1548 | unsigned int *offs, |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1549 | struct stripe_head *sh, |
| 1550 | int srctype) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1551 | { |
| 1552 | int disks = sh->disks; |
| 1553 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 1554 | int d0_idx = raid6_d0(sh); |
| 1555 | int count; |
| 1556 | int i; |
| 1557 | |
| 1558 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1559 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1560 | |
| 1561 | count = 0; |
| 1562 | i = d0_idx; |
| 1563 | do { |
| 1564 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1565 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1566 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1567 | if (i == sh->qd_idx || i == sh->pd_idx || |
| 1568 | (srctype == SYNDROME_SRC_ALL) || |
| 1569 | (srctype == SYNDROME_SRC_WANT_DRAIN && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1570 | (test_bit(R5_Wantdrain, &dev->flags) || |
| 1571 | test_bit(R5_InJournal, &dev->flags))) || |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1572 | (srctype == SYNDROME_SRC_WRITTEN && |
Song Liu | 0977762 | 2017-03-13 13:44:35 -0700 | [diff] [blame] | 1573 | (dev->written || |
| 1574 | test_bit(R5_InJournal, &dev->flags)))) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1575 | if (test_bit(R5_InJournal, &dev->flags)) |
| 1576 | srcs[slot] = sh->dev[i].orig_page; |
| 1577 | else |
| 1578 | srcs[slot] = sh->dev[i].page; |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1579 | /* |
| 1580 | * For R5_InJournal, PAGE_SIZE must be 4KB and will |
| 1581 | * not shared page. In that case, dev[i].offset |
| 1582 | * is 0. |
| 1583 | */ |
| 1584 | offs[slot] = sh->dev[i].offset; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1585 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1586 | i = raid6_next_disk(i, disks); |
| 1587 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1588 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1589 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1590 | } |
| 1591 | |
| 1592 | static struct dma_async_tx_descriptor * |
| 1593 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1594 | { |
| 1595 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1596 | struct page **blocks = to_addr_page(percpu, 0); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1597 | unsigned int *offs = to_addr_offs(sh, percpu); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1598 | int target; |
| 1599 | int qd_idx = sh->qd_idx; |
| 1600 | struct dma_async_tx_descriptor *tx; |
| 1601 | struct async_submit_ctl submit; |
| 1602 | struct r5dev *tgt; |
| 1603 | struct page *dest; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1604 | unsigned int dest_off; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1605 | int i; |
| 1606 | int count; |
| 1607 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1608 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1609 | if (sh->ops.target < 0) |
| 1610 | target = sh->ops.target2; |
| 1611 | else if (sh->ops.target2 < 0) |
| 1612 | target = sh->ops.target; |
| 1613 | else |
| 1614 | /* we should only have one valid target */ |
| 1615 | BUG(); |
| 1616 | BUG_ON(target < 0); |
| 1617 | pr_debug("%s: stripe %llu block: %d\n", |
| 1618 | __func__, (unsigned long long)sh->sector, target); |
| 1619 | |
| 1620 | tgt = &sh->dev[target]; |
| 1621 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1622 | dest = tgt->page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1623 | dest_off = tgt->offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1624 | |
| 1625 | atomic_inc(&sh->count); |
| 1626 | |
| 1627 | if (target == qd_idx) { |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1628 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1629 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 1630 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1631 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1632 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1633 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1634 | tx = async_gen_syndrome(blocks, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1635 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1636 | } else { |
| 1637 | /* Compute any data- or p-drive using XOR */ |
| 1638 | count = 0; |
| 1639 | for (i = disks; i-- ; ) { |
| 1640 | if (i == target || i == qd_idx) |
| 1641 | continue; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1642 | offs[count] = sh->dev[i].offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1643 | blocks[count++] = sh->dev[i].page; |
| 1644 | } |
| 1645 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1646 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1647 | NULL, ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1648 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1649 | tx = async_xor_offs(dest, dest_off, blocks, offs, count, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1650 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1651 | } |
| 1652 | |
| 1653 | return tx; |
| 1654 | } |
| 1655 | |
| 1656 | static struct dma_async_tx_descriptor * |
| 1657 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1658 | { |
| 1659 | int i, count, disks = sh->disks; |
| 1660 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1661 | int d0_idx = raid6_d0(sh); |
| 1662 | int faila = -1, failb = -1; |
| 1663 | int target = sh->ops.target; |
| 1664 | int target2 = sh->ops.target2; |
| 1665 | struct r5dev *tgt = &sh->dev[target]; |
| 1666 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1667 | struct dma_async_tx_descriptor *tx; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1668 | struct page **blocks = to_addr_page(percpu, 0); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1669 | unsigned int *offs = to_addr_offs(sh, percpu); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1670 | struct async_submit_ctl submit; |
| 1671 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1672 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1673 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1674 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1675 | BUG_ON(target < 0 || target2 < 0); |
| 1676 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1677 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1678 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1679 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1680 | * slot number conversion for 'faila' and 'failb' |
| 1681 | */ |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1682 | for (i = 0; i < disks ; i++) { |
| 1683 | offs[i] = 0; |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1684 | blocks[i] = NULL; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1685 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1686 | count = 0; |
| 1687 | i = d0_idx; |
| 1688 | do { |
| 1689 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1690 | |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1691 | offs[slot] = sh->dev[i].offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1692 | blocks[slot] = sh->dev[i].page; |
| 1693 | |
| 1694 | if (i == target) |
| 1695 | faila = slot; |
| 1696 | if (i == target2) |
| 1697 | failb = slot; |
| 1698 | i = raid6_next_disk(i, disks); |
| 1699 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1700 | |
| 1701 | BUG_ON(faila == failb); |
| 1702 | if (failb < faila) |
| 1703 | swap(faila, failb); |
| 1704 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1705 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1706 | |
| 1707 | atomic_inc(&sh->count); |
| 1708 | |
| 1709 | if (failb == syndrome_disks+1) { |
| 1710 | /* Q disk is one of the missing disks */ |
| 1711 | if (faila == syndrome_disks) { |
| 1712 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1713 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1714 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1715 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1716 | return async_gen_syndrome(blocks, offs, syndrome_disks+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1717 | RAID5_STRIPE_SIZE(sh->raid_conf), |
| 1718 | &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1719 | } else { |
| 1720 | struct page *dest; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1721 | unsigned int dest_off; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1722 | int data_target; |
| 1723 | int qd_idx = sh->qd_idx; |
| 1724 | |
| 1725 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1726 | if (target == qd_idx) |
| 1727 | data_target = target2; |
| 1728 | else |
| 1729 | data_target = target; |
| 1730 | |
| 1731 | count = 0; |
| 1732 | for (i = disks; i-- ; ) { |
| 1733 | if (i == data_target || i == qd_idx) |
| 1734 | continue; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1735 | offs[count] = sh->dev[i].offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1736 | blocks[count++] = sh->dev[i].page; |
| 1737 | } |
| 1738 | dest = sh->dev[data_target].page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1739 | dest_off = sh->dev[data_target].offset; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1740 | init_async_submit(&submit, |
| 1741 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1742 | NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1743 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1744 | tx = async_xor_offs(dest, dest_off, blocks, offs, count, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1745 | RAID5_STRIPE_SIZE(sh->raid_conf), |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1746 | &submit); |
| 1747 | |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1748 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1749 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1750 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1751 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1752 | return async_gen_syndrome(blocks, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1753 | RAID5_STRIPE_SIZE(sh->raid_conf), |
| 1754 | &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1755 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1756 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1757 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1758 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1759 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1760 | if (failb == syndrome_disks) { |
| 1761 | /* We're missing D+P. */ |
| 1762 | return async_raid6_datap_recov(syndrome_disks+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1763 | RAID5_STRIPE_SIZE(sh->raid_conf), |
| 1764 | faila, |
Yufen Yu | 4f86ff5 | 2020-08-20 09:22:11 -0400 | [diff] [blame] | 1765 | blocks, offs, &submit); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1766 | } else { |
| 1767 | /* We're missing D+D. */ |
| 1768 | return async_raid6_2data_recov(syndrome_disks+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1769 | RAID5_STRIPE_SIZE(sh->raid_conf), |
| 1770 | faila, failb, |
Yufen Yu | 4f86ff5 | 2020-08-20 09:22:11 -0400 | [diff] [blame] | 1771 | blocks, offs, &submit); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1772 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1773 | } |
| 1774 | } |
| 1775 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1776 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1777 | { |
| 1778 | struct stripe_head *sh = stripe_head_ref; |
| 1779 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1780 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1781 | (unsigned long long)sh->sector); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1782 | |
| 1783 | if (r5c_is_writeback(sh->raid_conf->log)) |
| 1784 | /* |
| 1785 | * raid5-cache write back uses orig_page during prexor. |
| 1786 | * After prexor, it is time to free orig_page |
| 1787 | */ |
| 1788 | r5c_release_extra_page(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1789 | } |
| 1790 | |
| 1791 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1792 | ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1793 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1794 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1795 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1796 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1797 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1798 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1799 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1800 | |
| 1801 | /* existing parity data subtracted */ |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1802 | unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1803 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1804 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1805 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1806 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1807 | (unsigned long long)sh->sector); |
| 1808 | |
| 1809 | for (i = disks; i--; ) { |
| 1810 | struct r5dev *dev = &sh->dev[i]; |
| 1811 | /* Only process blocks that are known to be uptodate */ |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1812 | if (test_bit(R5_InJournal, &dev->flags)) { |
| 1813 | /* |
| 1814 | * For this case, PAGE_SIZE must be equal to 4KB and |
| 1815 | * page offset is zero. |
| 1816 | */ |
| 1817 | off_srcs[count] = dev->offset; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1818 | xor_srcs[count++] = dev->orig_page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1819 | } else if (test_bit(R5_Wantdrain, &dev->flags)) { |
| 1820 | off_srcs[count] = dev->offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1821 | xor_srcs[count++] = dev->page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1822 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1823 | } |
| 1824 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1825 | 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] | 1826 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1827 | tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1828 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1829 | |
| 1830 | return tx; |
| 1831 | } |
| 1832 | |
| 1833 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1834 | ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1835 | struct dma_async_tx_descriptor *tx) |
| 1836 | { |
| 1837 | struct page **blocks = to_addr_page(percpu, 0); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1838 | unsigned int *offs = to_addr_offs(sh, percpu); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1839 | int count; |
| 1840 | struct async_submit_ctl submit; |
| 1841 | |
| 1842 | pr_debug("%s: stripe %llu\n", __func__, |
| 1843 | (unsigned long long)sh->sector); |
| 1844 | |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1845 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1846 | |
| 1847 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx, |
| 1848 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1849 | tx = async_gen_syndrome(blocks, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1850 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1851 | |
| 1852 | return tx; |
| 1853 | } |
| 1854 | |
| 1855 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1856 | 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] | 1857 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1858 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1859 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1860 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1861 | struct stripe_head *head_sh = sh; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1862 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1863 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1864 | (unsigned long long)sh->sector); |
| 1865 | |
| 1866 | for (i = disks; i--; ) { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1867 | struct r5dev *dev; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1868 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1869 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1870 | sh = head_sh; |
| 1871 | if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1872 | struct bio *wbi; |
| 1873 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1874 | again: |
| 1875 | dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1876 | /* |
| 1877 | * clear R5_InJournal, so when rewriting a page in |
| 1878 | * journal, it is not skipped by r5l_log_stripe() |
| 1879 | */ |
| 1880 | clear_bit(R5_InJournal, &dev->flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1881 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1882 | chosen = dev->towrite; |
| 1883 | dev->towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1884 | sh->overwrite_disks = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1885 | BUG_ON(dev->written); |
| 1886 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1887 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1888 | WARN_ON(dev->page != dev->orig_page); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1889 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1890 | while (wbi && wbi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1891 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1892 | if (wbi->bi_opf & REQ_FUA) |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1893 | set_bit(R5_WantFUA, &dev->flags); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1894 | if (wbi->bi_opf & REQ_SYNC) |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1895 | set_bit(R5_SyncIO, &dev->flags); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1896 | if (bio_op(wbi) == REQ_OP_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1897 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1898 | else { |
| 1899 | tx = async_copy_data(1, wbi, &dev->page, |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1900 | dev->offset, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1901 | dev->sector, tx, sh, |
| 1902 | r5c_is_writeback(conf->log)); |
| 1903 | if (dev->page != dev->orig_page && |
| 1904 | !r5c_is_writeback(conf->log)) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1905 | set_bit(R5_SkipCopy, &dev->flags); |
| 1906 | clear_bit(R5_UPTODATE, &dev->flags); |
| 1907 | clear_bit(R5_OVERWRITE, &dev->flags); |
| 1908 | } |
| 1909 | } |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1910 | wbi = r5_next_bio(conf, wbi, dev->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1911 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1912 | |
| 1913 | if (head_sh->batch_head) { |
| 1914 | sh = list_first_entry(&sh->batch_list, |
| 1915 | struct stripe_head, |
| 1916 | batch_list); |
| 1917 | if (sh == head_sh) |
| 1918 | continue; |
| 1919 | goto again; |
| 1920 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1921 | } |
| 1922 | } |
| 1923 | |
| 1924 | return tx; |
| 1925 | } |
| 1926 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1927 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1928 | { |
| 1929 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1930 | int disks = sh->disks; |
| 1931 | int pd_idx = sh->pd_idx; |
| 1932 | int qd_idx = sh->qd_idx; |
| 1933 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1934 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1935 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1936 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1937 | (unsigned long long)sh->sector); |
| 1938 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1939 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1940 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1941 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1942 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1943 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1944 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1945 | for (i = disks; i--; ) { |
| 1946 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1947 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1948 | if (dev->written || i == pd_idx || i == qd_idx) { |
NeilBrown | 235b600 | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 1949 | if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) { |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1950 | set_bit(R5_UPTODATE, &dev->flags); |
NeilBrown | 235b600 | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 1951 | if (test_bit(STRIPE_EXPAND_READY, &sh->state)) |
| 1952 | set_bit(R5_Expanded, &dev->flags); |
| 1953 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1954 | if (fua) |
| 1955 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1956 | if (sync) |
| 1957 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1958 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1959 | } |
| 1960 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1961 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1962 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1963 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1964 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1965 | else { |
| 1966 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1967 | sh->reconstruct_state = reconstruct_state_result; |
| 1968 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1969 | |
| 1970 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1971 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1975 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1976 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1977 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1978 | int disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1979 | struct page **xor_srcs; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1980 | unsigned int *off_srcs; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1981 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1982 | int count, pd_idx = sh->pd_idx, i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1983 | struct page *xor_dest; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1984 | unsigned int off_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1985 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1986 | unsigned long flags; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1987 | int j = 0; |
| 1988 | struct stripe_head *head_sh = sh; |
| 1989 | int last_stripe; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1990 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1991 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1992 | (unsigned long long)sh->sector); |
| 1993 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1994 | for (i = 0; i < sh->disks; i++) { |
| 1995 | if (pd_idx == i) |
| 1996 | continue; |
| 1997 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1998 | break; |
| 1999 | } |
| 2000 | if (i >= sh->disks) { |
| 2001 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 2002 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 2003 | ops_complete_reconstruct(sh); |
| 2004 | return; |
| 2005 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2006 | again: |
| 2007 | count = 0; |
| 2008 | xor_srcs = to_addr_page(percpu, j); |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2009 | off_srcs = to_addr_offs(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2010 | /* check if prexor is active which means only process blocks |
| 2011 | * that are part of a read-modify-write (written) |
| 2012 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2013 | if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2014 | prexor = 1; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2015 | off_dest = off_srcs[count] = sh->dev[pd_idx].offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2016 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 2017 | for (i = disks; i--; ) { |
| 2018 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2019 | if (head_sh->dev[i].written || |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2020 | test_bit(R5_InJournal, &head_sh->dev[i].flags)) { |
| 2021 | off_srcs[count] = dev->offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2022 | xor_srcs[count++] = dev->page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2023 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2024 | } |
| 2025 | } else { |
| 2026 | xor_dest = sh->dev[pd_idx].page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2027 | off_dest = sh->dev[pd_idx].offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2028 | for (i = disks; i--; ) { |
| 2029 | struct r5dev *dev = &sh->dev[i]; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2030 | if (i != pd_idx) { |
| 2031 | off_srcs[count] = dev->offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2032 | xor_srcs[count++] = dev->page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2033 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2034 | } |
| 2035 | } |
| 2036 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2037 | /* 1/ if we prexor'd then the dest is reused as a source |
| 2038 | * 2/ if we did not prexor then we are redoing the parity |
| 2039 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 2040 | * for the synchronous xor case |
| 2041 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2042 | last_stripe = !head_sh->batch_head || |
| 2043 | list_first_entry(&sh->batch_list, |
| 2044 | struct stripe_head, batch_list) == head_sh; |
| 2045 | if (last_stripe) { |
| 2046 | flags = ASYNC_TX_ACK | |
| 2047 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2048 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2049 | atomic_inc(&head_sh->count); |
| 2050 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh, |
| 2051 | to_addr_conv(sh, percpu, j)); |
| 2052 | } else { |
| 2053 | flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST; |
| 2054 | init_async_submit(&submit, flags, tx, NULL, NULL, |
| 2055 | to_addr_conv(sh, percpu, j)); |
| 2056 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2057 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2058 | if (unlikely(count == 1)) |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2059 | tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0], |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2060 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2061 | else |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2062 | tx = async_xor_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2063 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2064 | if (!last_stripe) { |
| 2065 | j++; |
| 2066 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 2067 | batch_list); |
| 2068 | goto again; |
| 2069 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2070 | } |
| 2071 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2072 | static void |
| 2073 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 2074 | struct dma_async_tx_descriptor *tx) |
| 2075 | { |
| 2076 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2077 | struct page **blocks; |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2078 | unsigned int *offs; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2079 | int count, i, j = 0; |
| 2080 | struct stripe_head *head_sh = sh; |
| 2081 | int last_stripe; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2082 | int synflags; |
| 2083 | unsigned long txflags; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2084 | |
| 2085 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 2086 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 2087 | for (i = 0; i < sh->disks; i++) { |
| 2088 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 2089 | continue; |
| 2090 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 2091 | break; |
| 2092 | } |
| 2093 | if (i >= sh->disks) { |
| 2094 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 2095 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 2096 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 2097 | ops_complete_reconstruct(sh); |
| 2098 | return; |
| 2099 | } |
| 2100 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2101 | again: |
| 2102 | blocks = to_addr_page(percpu, j); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2103 | offs = to_addr_offs(sh, percpu); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2104 | |
| 2105 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 2106 | synflags = SYNDROME_SRC_WRITTEN; |
| 2107 | txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST; |
| 2108 | } else { |
| 2109 | synflags = SYNDROME_SRC_ALL; |
| 2110 | txflags = ASYNC_TX_ACK; |
| 2111 | } |
| 2112 | |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2113 | count = set_syndrome_sources(blocks, offs, sh, synflags); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2114 | last_stripe = !head_sh->batch_head || |
| 2115 | list_first_entry(&sh->batch_list, |
| 2116 | struct stripe_head, batch_list) == head_sh; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2117 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2118 | if (last_stripe) { |
| 2119 | atomic_inc(&head_sh->count); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2120 | init_async_submit(&submit, txflags, tx, ops_complete_reconstruct, |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2121 | head_sh, to_addr_conv(sh, percpu, j)); |
| 2122 | } else |
| 2123 | init_async_submit(&submit, 0, tx, NULL, NULL, |
| 2124 | to_addr_conv(sh, percpu, j)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2125 | tx = async_gen_syndrome(blocks, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2126 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2127 | if (!last_stripe) { |
| 2128 | j++; |
| 2129 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 2130 | batch_list); |
| 2131 | goto again; |
| 2132 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2133 | } |
| 2134 | |
| 2135 | static void ops_complete_check(void *stripe_head_ref) |
| 2136 | { |
| 2137 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2138 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2139 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2140 | (unsigned long long)sh->sector); |
| 2141 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2142 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2143 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2144 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2145 | } |
| 2146 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2147 | 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] | 2148 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2149 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2150 | int pd_idx = sh->pd_idx; |
| 2151 | int qd_idx = sh->qd_idx; |
| 2152 | struct page *xor_dest; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2153 | unsigned int off_dest; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2154 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2155 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2156 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2157 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2158 | int count; |
| 2159 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2160 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2161 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2162 | (unsigned long long)sh->sector); |
| 2163 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2164 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2165 | count = 0; |
| 2166 | xor_dest = sh->dev[pd_idx].page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2167 | off_dest = sh->dev[pd_idx].offset; |
| 2168 | off_srcs[count] = off_dest; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2169 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2170 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2171 | if (i == pd_idx || i == qd_idx) |
| 2172 | continue; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2173 | off_srcs[count] = sh->dev[i].offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2174 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2175 | } |
| 2176 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2177 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2178 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2179 | tx = async_xor_val_offs(xor_dest, off_dest, xor_srcs, off_srcs, count, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2180 | RAID5_STRIPE_SIZE(sh->raid_conf), |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2181 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2182 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2183 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2184 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 2185 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2186 | } |
| 2187 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2188 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 2189 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2190 | struct page **srcs = to_addr_page(percpu, 0); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2191 | unsigned int *offs = to_addr_offs(sh, percpu); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2192 | struct async_submit_ctl submit; |
| 2193 | int count; |
| 2194 | |
| 2195 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 2196 | (unsigned long long)sh->sector, checkp); |
| 2197 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2198 | BUG_ON(sh->batch_head); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2199 | count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2200 | if (!checkp) |
| 2201 | srcs[count] = NULL; |
| 2202 | |
| 2203 | atomic_inc(&sh->count); |
| 2204 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2205 | sh, to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2206 | async_syndrome_val(srcs, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2207 | RAID5_STRIPE_SIZE(sh->raid_conf), |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2208 | &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2209 | } |
| 2210 | |
NeilBrown | 51acbce | 2013-02-28 09:08:34 +1100 | [diff] [blame] | 2211 | 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] | 2212 | { |
| 2213 | int overlap_clear = 0, i, disks = sh->disks; |
| 2214 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2215 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2216 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2217 | struct raid5_percpu *percpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2218 | |
Davidlohr Bueso | 770b1d2 | 2021-11-15 17:23:17 -0800 | [diff] [blame] | 2219 | local_lock(&conf->percpu->lock); |
| 2220 | percpu = this_cpu_ptr(conf->percpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 2221 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2222 | ops_run_biofill(sh); |
| 2223 | overlap_clear++; |
| 2224 | } |
| 2225 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 2226 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2227 | if (level < 6) |
| 2228 | tx = ops_run_compute5(sh, percpu); |
| 2229 | else { |
| 2230 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 2231 | tx = ops_run_compute6_1(sh, percpu); |
| 2232 | else |
| 2233 | tx = ops_run_compute6_2(sh, percpu); |
| 2234 | } |
| 2235 | /* terminate the chain if reconstruct is not set to be run */ |
| 2236 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 2237 | async_tx_ack(tx); |
| 2238 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2239 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2240 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) { |
| 2241 | if (level < 6) |
| 2242 | tx = ops_run_prexor5(sh, percpu, tx); |
| 2243 | else |
| 2244 | tx = ops_run_prexor6(sh, percpu, tx); |
| 2245 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2246 | |
Artur Paszkiewicz | ae1713e | 2017-04-04 13:13:58 +0200 | [diff] [blame] | 2247 | if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request)) |
| 2248 | tx = ops_run_partial_parity(sh, percpu, tx); |
| 2249 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2250 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2251 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2252 | overlap_clear++; |
| 2253 | } |
| 2254 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2255 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 2256 | if (level < 6) |
| 2257 | ops_run_reconstruct5(sh, percpu, tx); |
| 2258 | else |
| 2259 | ops_run_reconstruct6(sh, percpu, tx); |
| 2260 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2261 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2262 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 2263 | if (sh->check_state == check_state_run) |
| 2264 | ops_run_check_p(sh, percpu); |
| 2265 | else if (sh->check_state == check_state_run_q) |
| 2266 | ops_run_check_pq(sh, percpu, 0); |
| 2267 | else if (sh->check_state == check_state_run_pq) |
| 2268 | ops_run_check_pq(sh, percpu, 1); |
| 2269 | else |
| 2270 | BUG(); |
| 2271 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2272 | |
Davidlohr Bueso | 770b1d2 | 2021-11-15 17:23:17 -0800 | [diff] [blame] | 2273 | if (overlap_clear && !sh->batch_head) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2274 | for (i = disks; i--; ) { |
| 2275 | struct r5dev *dev = &sh->dev[i]; |
| 2276 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 2277 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 2278 | } |
Davidlohr Bueso | 770b1d2 | 2021-11-15 17:23:17 -0800 | [diff] [blame] | 2279 | } |
| 2280 | local_unlock(&conf->percpu->lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2281 | } |
| 2282 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2283 | static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh) |
| 2284 | { |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 2285 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 2286 | kfree(sh->pages); |
| 2287 | #endif |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2288 | if (sh->ppl_page) |
| 2289 | __free_page(sh->ppl_page); |
| 2290 | kmem_cache_free(sc, sh); |
| 2291 | } |
| 2292 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2293 | static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp, |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2294 | int disks, struct r5conf *conf) |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2295 | { |
| 2296 | struct stripe_head *sh; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2297 | int i; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2298 | |
| 2299 | sh = kmem_cache_zalloc(sc, gfp); |
| 2300 | if (sh) { |
| 2301 | spin_lock_init(&sh->stripe_lock); |
| 2302 | spin_lock_init(&sh->batch_lock); |
| 2303 | INIT_LIST_HEAD(&sh->batch_list); |
| 2304 | INIT_LIST_HEAD(&sh->lru); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2305 | INIT_LIST_HEAD(&sh->r5c); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2306 | INIT_LIST_HEAD(&sh->log_list); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2307 | atomic_set(&sh->count, 1); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2308 | sh->raid_conf = conf; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2309 | sh->log_start = MaxSector; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2310 | for (i = 0; i < disks; i++) { |
| 2311 | struct r5dev *dev = &sh->dev[i]; |
| 2312 | |
Ming Lei | 3a83f46 | 2016-11-22 08:57:21 -0700 | [diff] [blame] | 2313 | bio_init(&dev->req, &dev->vec, 1); |
| 2314 | bio_init(&dev->rreq, &dev->rvec, 1); |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2315 | } |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2316 | |
| 2317 | if (raid5_has_ppl(conf)) { |
| 2318 | sh->ppl_page = alloc_page(gfp); |
| 2319 | if (!sh->ppl_page) { |
| 2320 | free_stripe(sc, sh); |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 2321 | return NULL; |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2322 | } |
| 2323 | } |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame] | 2324 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 2325 | if (init_stripe_shared_pages(sh, conf, disks)) { |
| 2326 | free_stripe(sc, sh); |
| 2327 | return NULL; |
| 2328 | } |
| 2329 | #endif |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2330 | } |
| 2331 | return sh; |
| 2332 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2333 | static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | { |
| 2335 | struct stripe_head *sh; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2336 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2337 | sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2338 | if (!sh) |
| 2339 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 2340 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 2341 | if (grow_buffers(sh, gfp)) { |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2342 | shrink_buffers(sh); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2343 | free_stripe(conf->slab_cache, sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2344 | return 0; |
| 2345 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2346 | sh->hash_lock_index = |
| 2347 | conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2348 | /* we just created an active stripe so... */ |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2349 | atomic_inc(&conf->active_stripes); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2350 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2351 | raid5_release_stripe(sh); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2352 | conf->max_nr_stripes++; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2353 | return 1; |
| 2354 | } |
| 2355 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2356 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2357 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2358 | struct kmem_cache *sc; |
Arnd Bergmann | 53b8d89 | 2018-02-20 14:09:11 +0100 | [diff] [blame] | 2359 | size_t namelen = sizeof(conf->cache_name[0]); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2360 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2361 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2362 | if (conf->mddev->gendisk) |
Arnd Bergmann | 53b8d89 | 2018-02-20 14:09:11 +0100 | [diff] [blame] | 2363 | snprintf(conf->cache_name[0], namelen, |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2364 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 2365 | else |
Arnd Bergmann | 53b8d89 | 2018-02-20 14:09:11 +0100 | [diff] [blame] | 2366 | snprintf(conf->cache_name[0], namelen, |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2367 | "raid%d-%p", conf->level, conf->mddev); |
Arnd Bergmann | 53b8d89 | 2018-02-20 14:09:11 +0100 | [diff] [blame] | 2368 | snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]); |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2369 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2370 | conf->active_name = 0; |
| 2371 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2373 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2374 | if (!sc) |
| 2375 | return 1; |
| 2376 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2377 | conf->pool_size = devs; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2378 | while (num--) |
| 2379 | if (!grow_one_stripe(conf, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2380 | return 1; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2381 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2382 | return 0; |
| 2383 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2384 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2385 | /** |
Coly Li | 7f8a30e | 2020-04-09 22:17:22 +0800 | [diff] [blame] | 2386 | * scribble_alloc - allocate percpu scribble buffer for required size |
| 2387 | * of the scribble region |
Damien Le Moal | 2aada5b | 2020-07-16 13:54:42 +0900 | [diff] [blame] | 2388 | * @percpu: from for_each_present_cpu() of the caller |
| 2389 | * @num: total number of disks in the array |
| 2390 | * @cnt: scribble objs count for required size of the scribble region |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2391 | * |
Coly Li | 7f8a30e | 2020-04-09 22:17:22 +0800 | [diff] [blame] | 2392 | * The scribble buffer size must be enough to contain: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2393 | * 1/ a struct page pointer for each device in the array +2 |
| 2394 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 2395 | * (dma_map_page()) or page (page_address()) address. |
| 2396 | * |
| 2397 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 2398 | * calculate over all devices (not just the data blocks), using zeros in place |
| 2399 | * of the P and Q blocks. |
| 2400 | */ |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2401 | static int scribble_alloc(struct raid5_percpu *percpu, |
Coly Li | ba54d4d | 2020-04-09 22:17:21 +0800 | [diff] [blame] | 2402 | int num, int cnt) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2403 | { |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2404 | size_t obj_size = |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2405 | sizeof(struct page *) * (num + 2) + |
| 2406 | sizeof(addr_conv_t) * (num + 2) + |
| 2407 | sizeof(unsigned int) * (num + 2); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2408 | void *scribble; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2409 | |
Coly Li | ba54d4d | 2020-04-09 22:17:21 +0800 | [diff] [blame] | 2410 | /* |
| 2411 | * If here is in raid array suspend context, it is in memalloc noio |
| 2412 | * context as well, there is no potential recursive memory reclaim |
| 2413 | * I/Os with the GFP_KERNEL flag. |
| 2414 | */ |
| 2415 | scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2416 | if (!scribble) |
| 2417 | return -ENOMEM; |
| 2418 | |
| 2419 | kvfree(percpu->scribble); |
| 2420 | |
| 2421 | percpu->scribble = scribble; |
| 2422 | percpu->scribble_obj_size = obj_size; |
| 2423 | return 0; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2424 | } |
| 2425 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2426 | static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors) |
| 2427 | { |
| 2428 | unsigned long cpu; |
| 2429 | int err = 0; |
| 2430 | |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2431 | /* |
| 2432 | * Never shrink. And mddev_suspend() could deadlock if this is called |
| 2433 | * from raid5d. In that case, scribble_disks and scribble_sectors |
| 2434 | * should equal to new_disks and new_sectors |
| 2435 | */ |
| 2436 | if (conf->scribble_disks >= new_disks && |
| 2437 | conf->scribble_sectors >= new_sectors) |
| 2438 | return 0; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2439 | mddev_suspend(conf->mddev); |
Sebastian Andrzej Siewior | 252034e | 2021-08-03 16:15:58 +0200 | [diff] [blame] | 2440 | cpus_read_lock(); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2441 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2442 | for_each_present_cpu(cpu) { |
| 2443 | struct raid5_percpu *percpu; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2444 | |
| 2445 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2446 | err = scribble_alloc(percpu, new_disks, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2447 | new_sectors / RAID5_STRIPE_SECTORS(conf)); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2448 | if (err) |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2449 | break; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2450 | } |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2451 | |
Sebastian Andrzej Siewior | 252034e | 2021-08-03 16:15:58 +0200 | [diff] [blame] | 2452 | cpus_read_unlock(); |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2453 | mddev_resume(conf->mddev); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2454 | if (!err) { |
| 2455 | conf->scribble_disks = new_disks; |
| 2456 | conf->scribble_sectors = new_sectors; |
| 2457 | } |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2458 | return err; |
| 2459 | } |
| 2460 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2461 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2462 | { |
| 2463 | /* Make all the stripes able to hold 'newsize' devices. |
| 2464 | * New slots in each stripe get 'page' set to a new page. |
| 2465 | * |
| 2466 | * This happens in stages: |
| 2467 | * 1/ create a new kmem_cache and allocate the required number of |
| 2468 | * stripe_heads. |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 2469 | * 2/ gather all the old stripe_heads and transfer the pages across |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2470 | * to the new stripe_heads. This will have the side effect of |
| 2471 | * freezing the array as once all stripe_heads have been collected, |
| 2472 | * no IO will be possible. Old stripe heads are freed once their |
| 2473 | * pages have been transferred over, and the old kmem_cache is |
| 2474 | * freed when all stripes are done. |
| 2475 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
Zhilong Liu | 3560741e | 2017-03-15 16:14:53 +0800 | [diff] [blame] | 2476 | * we simple return a failure status - no need to clean anything up. |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2477 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 2478 | * If this fails, we don't bother trying the shrink the |
| 2479 | * stripe_heads down again, we just leave them as they are. |
| 2480 | * As each stripe_head is processed the new one is released into |
| 2481 | * active service. |
| 2482 | * |
| 2483 | * Once step2 is started, we cannot afford to wait for a write, |
| 2484 | * so we use GFP_NOIO allocations. |
| 2485 | */ |
| 2486 | struct stripe_head *osh, *nsh; |
| 2487 | LIST_HEAD(newstripes); |
| 2488 | struct disk_info *ndisks; |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 2489 | int err = 0; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2490 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2491 | int i; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2492 | int hash, cnt; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2493 | |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 2494 | md_allow_write(conf->mddev); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 2495 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2496 | /* Step 1 */ |
| 2497 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 2498 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2499 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2500 | if (!sc) |
| 2501 | return -ENOMEM; |
| 2502 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2503 | /* Need to ensure auto-resizing doesn't interfere */ |
| 2504 | mutex_lock(&conf->cache_size_mutex); |
| 2505 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2506 | for (i = conf->max_nr_stripes; i; i--) { |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2507 | nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2508 | if (!nsh) |
| 2509 | break; |
| 2510 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2511 | list_add(&nsh->lru, &newstripes); |
| 2512 | } |
| 2513 | if (i) { |
| 2514 | /* didn't get enough, give up */ |
| 2515 | while (!list_empty(&newstripes)) { |
| 2516 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2517 | list_del(&nsh->lru); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2518 | free_stripe(sc, nsh); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2519 | } |
| 2520 | kmem_cache_destroy(sc); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2521 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2522 | return -ENOMEM; |
| 2523 | } |
| 2524 | /* Step 2 - Must use GFP_NOIO now. |
| 2525 | * OK, we have enough stripes, start collecting inactive |
| 2526 | * stripes and copying them over |
| 2527 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2528 | hash = 0; |
| 2529 | cnt = 0; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2530 | list_for_each_entry(nsh, &newstripes, lru) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2531 | lock_device_hash_lock(conf, hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 2532 | wait_event_cmd(conf->wait_for_stripe, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2533 | !list_empty(conf->inactive_list + hash), |
| 2534 | unlock_device_hash_lock(conf, hash), |
| 2535 | lock_device_hash_lock(conf, hash)); |
| 2536 | osh = get_free_stripe(conf, hash); |
| 2537 | unlock_device_hash_lock(conf, hash); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2538 | |
Yufen Yu | f16acaf | 2020-08-20 09:22:13 -0400 | [diff] [blame] | 2539 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 2540 | for (i = 0; i < osh->nr_pages; i++) { |
| 2541 | nsh->pages[i] = osh->pages[i]; |
| 2542 | osh->pages[i] = NULL; |
| 2543 | } |
| 2544 | #endif |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2545 | for(i=0; i<conf->pool_size; i++) { |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2546 | nsh->dev[i].page = osh->dev[i].page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2547 | nsh->dev[i].orig_page = osh->dev[i].page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2548 | nsh->dev[i].offset = osh->dev[i].offset; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2549 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2550 | nsh->hash_lock_index = hash; |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2551 | free_stripe(conf->slab_cache, osh); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2552 | cnt++; |
| 2553 | if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + |
| 2554 | !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { |
| 2555 | hash++; |
| 2556 | cnt = 0; |
| 2557 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2558 | } |
| 2559 | kmem_cache_destroy(conf->slab_cache); |
| 2560 | |
| 2561 | /* Step 3. |
| 2562 | * At this point, we are holding all the stripes so the array |
| 2563 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2564 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2565 | */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2566 | ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2567 | if (ndisks) { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2568 | for (i = 0; i < conf->pool_size; i++) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2569 | ndisks[i] = conf->disks[i]; |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2570 | |
| 2571 | for (i = conf->pool_size; i < newsize; i++) { |
| 2572 | ndisks[i].extra_page = alloc_page(GFP_NOIO); |
| 2573 | if (!ndisks[i].extra_page) |
| 2574 | err = -ENOMEM; |
| 2575 | } |
| 2576 | |
| 2577 | if (err) { |
| 2578 | for (i = conf->pool_size; i < newsize; i++) |
| 2579 | if (ndisks[i].extra_page) |
| 2580 | put_page(ndisks[i].extra_page); |
| 2581 | kfree(ndisks); |
| 2582 | } else { |
| 2583 | kfree(conf->disks); |
| 2584 | conf->disks = ndisks; |
| 2585 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2586 | } else |
| 2587 | err = -ENOMEM; |
| 2588 | |
Dennis Yang | 583da48 | 2017-03-29 15:46:13 +0800 | [diff] [blame] | 2589 | conf->slab_cache = sc; |
| 2590 | conf->active_name = 1-conf->active_name; |
| 2591 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2592 | /* Step 4, return new stripes to service */ |
| 2593 | while(!list_empty(&newstripes)) { |
| 2594 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2595 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2596 | |
Yufen Yu | f16acaf | 2020-08-20 09:22:13 -0400 | [diff] [blame] | 2597 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 2598 | for (i = 0; i < nsh->nr_pages; i++) { |
| 2599 | if (nsh->pages[i]) |
| 2600 | continue; |
| 2601 | nsh->pages[i] = alloc_page(GFP_NOIO); |
| 2602 | if (!nsh->pages[i]) |
| 2603 | err = -ENOMEM; |
| 2604 | } |
| 2605 | |
| 2606 | for (i = conf->raid_disks; i < newsize; i++) { |
| 2607 | if (nsh->dev[i].page) |
| 2608 | continue; |
| 2609 | nsh->dev[i].page = raid5_get_dev_page(nsh, i); |
| 2610 | nsh->dev[i].orig_page = nsh->dev[i].page; |
| 2611 | nsh->dev[i].offset = raid5_get_page_offset(nsh, i); |
| 2612 | } |
| 2613 | #else |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2614 | for (i=conf->raid_disks; i < newsize; i++) |
| 2615 | if (nsh->dev[i].page == NULL) { |
| 2616 | struct page *p = alloc_page(GFP_NOIO); |
| 2617 | nsh->dev[i].page = p; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2618 | nsh->dev[i].orig_page = p; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2619 | nsh->dev[i].offset = 0; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2620 | if (!p) |
| 2621 | err = -ENOMEM; |
| 2622 | } |
Yufen Yu | f16acaf | 2020-08-20 09:22:13 -0400 | [diff] [blame] | 2623 | #endif |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2624 | raid5_release_stripe(nsh); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2625 | } |
| 2626 | /* critical section pass, GFP_NOIO no longer needed */ |
| 2627 | |
NeilBrown | 6e9eac2 | 2015-05-08 18:19:34 +1000 | [diff] [blame] | 2628 | if (!err) |
| 2629 | conf->pool_size = newsize; |
Song Liu | b44c018 | 2020-10-05 09:35:21 -0700 | [diff] [blame] | 2630 | mutex_unlock(&conf->cache_size_mutex); |
| 2631 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2632 | return err; |
| 2633 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2634 | |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2635 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2636 | { |
| 2637 | struct stripe_head *sh; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 2638 | int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2640 | spin_lock_irq(conf->hash_locks + hash); |
| 2641 | sh = get_free_stripe(conf, hash); |
| 2642 | spin_unlock_irq(conf->hash_locks + hash); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2643 | if (!sh) |
| 2644 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2645 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2646 | shrink_buffers(sh); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2647 | free_stripe(conf->slab_cache, sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2648 | atomic_dec(&conf->active_stripes); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2649 | conf->max_nr_stripes--; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2650 | return 1; |
| 2651 | } |
| 2652 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2653 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2654 | { |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2655 | while (conf->max_nr_stripes && |
| 2656 | drop_one_stripe(conf)) |
| 2657 | ; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2658 | |
Julia Lawall | 644df1a | 2015-09-13 14:15:10 +0200 | [diff] [blame] | 2659 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2660 | conf->slab_cache = NULL; |
| 2661 | } |
| 2662 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2663 | static void raid5_end_read_request(struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2665 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2666 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2667 | int disks = sh->disks, i; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2668 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2669 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2670 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2671 | |
| 2672 | for (i=0 ; i<disks; i++) |
| 2673 | if (bi == &sh->dev[i].req) |
| 2674 | break; |
| 2675 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2676 | pr_debug("end_read_request %llu/%d, count: %d, error %d.\n", |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2677 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2678 | bi->bi_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2679 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2680 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2682 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2683 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2684 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2685 | /* If replacement finished while this request was outstanding, |
| 2686 | * 'replacement' might be NULL already. |
| 2687 | * In that case it moved down to 'rdev'. |
| 2688 | * rdev is not removed until all requests are finished. |
| 2689 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2690 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2691 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2692 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2693 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2694 | if (use_new_offset(conf, sh)) |
| 2695 | s = sh->sector + rdev->new_data_offset; |
| 2696 | else |
| 2697 | s = sh->sector + rdev->data_offset; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2698 | if (!bi->bi_status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2699 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2700 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2701 | /* Note that this cannot happen on a |
| 2702 | * replacement device. We just fail those on |
| 2703 | * any error |
| 2704 | */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2705 | pr_info_ratelimited( |
| 2706 | "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n", |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2707 | mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2708 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2709 | bdevname(rdev->bdev, b)); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2710 | atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2711 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2712 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2713 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2714 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2715 | |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 2716 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 2717 | /* |
| 2718 | * end read for a page in journal, this |
| 2719 | * must be preparing for prexor in rmw |
| 2720 | */ |
| 2721 | set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); |
| 2722 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2723 | if (atomic_read(&rdev->read_errors)) |
| 2724 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2725 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2726 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2727 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2728 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2729 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2730 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
Nigel Croxon | b76b471 | 2019-09-06 09:21:33 -0400 | [diff] [blame] | 2731 | if (!(bi->bi_status == BLK_STS_PROTECTION)) |
| 2732 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2733 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2734 | pr_warn_ratelimited( |
| 2735 | "md/raid:%s: read error on replacement device (sector %llu on %s).\n", |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2736 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2737 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2738 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2739 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 2740 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2741 | pr_warn_ratelimited( |
| 2742 | "md/raid:%s: read error not correctable (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2743 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2744 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2745 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2746 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2747 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2748 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2749 | pr_warn_ratelimited( |
| 2750 | "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2751 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2752 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2753 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2754 | } else if (atomic_read(&rdev->read_errors) |
Nigel Croxon | 0009fad | 2019-08-21 09:27:08 -0400 | [diff] [blame] | 2755 | > conf->max_nr_stripes) { |
| 2756 | if (!test_bit(Faulty, &rdev->flags)) { |
| 2757 | pr_warn("md/raid:%s: %d read_errors > %d stripes\n", |
| 2758 | mdname(conf->mddev), |
| 2759 | atomic_read(&rdev->read_errors), |
| 2760 | conf->max_nr_stripes); |
| 2761 | pr_warn("md/raid:%s: Too many read errors, failing device %s.\n", |
| 2762 | mdname(conf->mddev), bdn); |
| 2763 | } |
| 2764 | } else |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2765 | retry = 1; |
Bian Yu | edfa1f6 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2766 | if (set_bad && test_bit(In_sync, &rdev->flags) |
| 2767 | && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2768 | retry = 1; |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2769 | if (retry) |
Xiao Ni | 143f6e7 | 2019-07-08 10:14:32 +0800 | [diff] [blame] | 2770 | if (sh->qd_idx >= 0 && sh->pd_idx == i) |
| 2771 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 2772 | else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2773 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 2774 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2775 | } else |
| 2776 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2777 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2778 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2779 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2780 | if (!(set_bad |
| 2781 | && test_bit(In_sync, &rdev->flags) |
| 2782 | && rdev_set_badblocks( |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2783 | rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0))) |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2784 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2785 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2786 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2787 | rdev_dec_pending(rdev, conf->mddev); |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2788 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2789 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 2790 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2791 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2792 | } |
| 2793 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2794 | static void raid5_end_write_request(struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2795 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2796 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2797 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2798 | int disks = sh->disks, i; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 2799 | struct md_rdev *rdev; |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2800 | sector_t first_bad; |
| 2801 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2802 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2803 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2804 | for (i = 0 ; i < disks; i++) { |
| 2805 | if (bi == &sh->dev[i].req) { |
| 2806 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2807 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2808 | } |
| 2809 | if (bi == &sh->dev[i].rreq) { |
| 2810 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2811 | if (rdev) |
| 2812 | replacement = 1; |
| 2813 | else |
| 2814 | /* rdev was removed and 'replacement' |
| 2815 | * replaced it. rdev is not removed |
| 2816 | * until all requests are finished. |
| 2817 | */ |
| 2818 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2819 | break; |
| 2820 | } |
| 2821 | } |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2822 | pr_debug("end_write_request %llu/%d, count %d, error: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2823 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2824 | bi->bi_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2825 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2826 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2827 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2828 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2829 | } |
| 2830 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2831 | if (replacement) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2832 | if (bi->bi_status) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2833 | md_error(conf->mddev, rdev); |
| 2834 | else if (is_badblock(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2835 | RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2836 | &first_bad, &bad_sectors)) |
| 2837 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 2838 | } else { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2839 | if (bi->bi_status) { |
NeilBrown | 9f97e4b | 2014-01-16 09:35:38 +1100 | [diff] [blame] | 2840 | set_bit(STRIPE_DEGRADED, &sh->state); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2841 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2842 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2843 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2844 | set_bit(MD_RECOVERY_NEEDED, |
| 2845 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2846 | } else if (is_badblock(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2847 | RAID5_STRIPE_SECTORS(conf), |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2848 | &first_bad, &bad_sectors)) { |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2849 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2850 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
| 2851 | /* That was a successful write so make |
| 2852 | * sure it looks like we already did |
| 2853 | * a re-write. |
| 2854 | */ |
| 2855 | set_bit(R5_ReWrite, &sh->dev[i].flags); |
| 2856 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2857 | } |
| 2858 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2859 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2860 | if (sh->batch_head && bi->bi_status && !replacement) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2861 | set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state); |
| 2862 | |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2863 | bio_reset(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2864 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 2865 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2866 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2867 | raid5_release_stripe(sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2868 | |
| 2869 | if (sh->batch_head && sh != sh->batch_head) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2870 | raid5_release_stripe(sh->batch_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2871 | } |
| 2872 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 2873 | static void raid5_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2874 | { |
| 2875 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2876 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2877 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2878 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2879 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2880 | spin_lock_irqsave(&conf->device_lock, flags); |
Mariusz Tkaczyk | fb73b35 | 2018-09-04 15:08:30 +0200 | [diff] [blame] | 2881 | |
| 2882 | if (test_bit(In_sync, &rdev->flags) && |
| 2883 | mddev->degraded == conf->max_degraded) { |
| 2884 | /* |
| 2885 | * Don't allow to achieve failed state |
| 2886 | * Don't try to recover this device |
| 2887 | */ |
| 2888 | conf->recovery_disabled = mddev->recovery_disabled; |
| 2889 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2890 | return; |
| 2891 | } |
| 2892 | |
bingjingc | aff69d8 | 2017-11-17 10:57:44 +0800 | [diff] [blame] | 2893 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2894 | clear_bit(In_sync, &rdev->flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2895 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2896 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2897 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2898 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2899 | set_bit(Blocked, &rdev->flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2900 | set_mask_bits(&mddev->sb_flags, 0, |
| 2901 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2902 | pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n" |
| 2903 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 2904 | mdname(mddev), |
| 2905 | bdevname(rdev->bdev, b), |
| 2906 | mdname(mddev), |
| 2907 | conf->raid_disks - mddev->degraded); |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 2908 | r5c_update_on_rdev_error(mddev, rdev); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2909 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2910 | |
| 2911 | /* |
| 2912 | * Input: a 'big' sector number, |
| 2913 | * Output: index of the data and parity disk, and the sector # in them. |
| 2914 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2915 | sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
| 2916 | int previous, int *dd_idx, |
| 2917 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2918 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2919 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2920 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2921 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2922 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2923 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2925 | int algorithm = previous ? conf->prev_algo |
| 2926 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2927 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2928 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2929 | int raid_disks = previous ? conf->previous_raid_disks |
| 2930 | : conf->raid_disks; |
| 2931 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | |
| 2933 | /* First compute the information on this sector */ |
| 2934 | |
| 2935 | /* |
| 2936 | * Compute the chunk number and the sector offset inside the chunk |
| 2937 | */ |
| 2938 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 2939 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | |
| 2941 | /* |
| 2942 | * Compute the stripe number |
| 2943 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2944 | stripe = chunk_number; |
| 2945 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2946 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2947 | /* |
| 2948 | * Select the parity disk based on the user selected algorithm. |
| 2949 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2950 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2951 | switch(conf->level) { |
| 2952 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2953 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2954 | break; |
| 2955 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2956 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2958 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2959 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2960 | (*dd_idx)++; |
| 2961 | break; |
| 2962 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2963 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2964 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2965 | (*dd_idx)++; |
| 2966 | break; |
| 2967 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2968 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2969 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2970 | break; |
| 2971 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2972 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2973 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2974 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2975 | case ALGORITHM_PARITY_0: |
| 2976 | pd_idx = 0; |
| 2977 | (*dd_idx)++; |
| 2978 | break; |
| 2979 | case ALGORITHM_PARITY_N: |
| 2980 | pd_idx = data_disks; |
| 2981 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2982 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2983 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2984 | } |
| 2985 | break; |
| 2986 | case 6: |
| 2987 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2988 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2989 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2990 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2991 | qd_idx = pd_idx + 1; |
| 2992 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2993 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2994 | qd_idx = 0; |
| 2995 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2996 | (*dd_idx) += 2; /* D D P Q D */ |
| 2997 | break; |
| 2998 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2999 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3000 | qd_idx = pd_idx + 1; |
| 3001 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3002 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3003 | qd_idx = 0; |
| 3004 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3005 | (*dd_idx) += 2; /* D D P Q D */ |
| 3006 | break; |
| 3007 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3008 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3009 | qd_idx = (pd_idx + 1) % raid_disks; |
| 3010 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3011 | break; |
| 3012 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3013 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3014 | qd_idx = (pd_idx + 1) % raid_disks; |
| 3015 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3016 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3017 | |
| 3018 | case ALGORITHM_PARITY_0: |
| 3019 | pd_idx = 0; |
| 3020 | qd_idx = 1; |
| 3021 | (*dd_idx) += 2; |
| 3022 | break; |
| 3023 | case ALGORITHM_PARITY_N: |
| 3024 | pd_idx = data_disks; |
| 3025 | qd_idx = data_disks + 1; |
| 3026 | break; |
| 3027 | |
| 3028 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 3029 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 3030 | * of blocks for computing Q is different. |
| 3031 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3032 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3033 | qd_idx = pd_idx + 1; |
| 3034 | if (pd_idx == raid_disks-1) { |
| 3035 | (*dd_idx)++; /* Q D D D P */ |
| 3036 | qd_idx = 0; |
| 3037 | } else if (*dd_idx >= pd_idx) |
| 3038 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3039 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3040 | break; |
| 3041 | |
| 3042 | case ALGORITHM_ROTATING_N_RESTART: |
| 3043 | /* Same a left_asymmetric, by first stripe is |
| 3044 | * D D D P Q rather than |
| 3045 | * Q D D D P |
| 3046 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3047 | stripe2 += 1; |
| 3048 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3049 | qd_idx = pd_idx + 1; |
| 3050 | if (pd_idx == raid_disks-1) { |
| 3051 | (*dd_idx)++; /* Q D D D P */ |
| 3052 | qd_idx = 0; |
| 3053 | } else if (*dd_idx >= pd_idx) |
| 3054 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3055 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3056 | break; |
| 3057 | |
| 3058 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 3059 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3060 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3061 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 3062 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3063 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3064 | break; |
| 3065 | |
| 3066 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 3067 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3068 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3069 | if (*dd_idx >= pd_idx) |
| 3070 | (*dd_idx)++; |
| 3071 | qd_idx = raid_disks - 1; |
| 3072 | break; |
| 3073 | |
| 3074 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3075 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3076 | if (*dd_idx >= pd_idx) |
| 3077 | (*dd_idx)++; |
| 3078 | qd_idx = raid_disks - 1; |
| 3079 | break; |
| 3080 | |
| 3081 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3082 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3083 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 3084 | qd_idx = raid_disks - 1; |
| 3085 | break; |
| 3086 | |
| 3087 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3088 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3089 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 3090 | qd_idx = raid_disks - 1; |
| 3091 | break; |
| 3092 | |
| 3093 | case ALGORITHM_PARITY_0_6: |
| 3094 | pd_idx = 0; |
| 3095 | (*dd_idx)++; |
| 3096 | qd_idx = raid_disks - 1; |
| 3097 | break; |
| 3098 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3099 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3100 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3101 | } |
| 3102 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3103 | } |
| 3104 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3105 | if (sh) { |
| 3106 | sh->pd_idx = pd_idx; |
| 3107 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3108 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3109 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3110 | /* |
| 3111 | * Finally, compute the new sector number |
| 3112 | */ |
| 3113 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 3114 | return new_sector; |
| 3115 | } |
| 3116 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 3117 | 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] | 3118 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3119 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 3120 | int raid_disks = sh->disks; |
| 3121 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3122 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 3123 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 3124 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 3125 | int algorithm = previous ? conf->prev_algo |
| 3126 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | sector_t stripe; |
| 3128 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 3129 | sector_t chunk_number; |
| 3130 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3131 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3132 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | |
| 3134 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 3135 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3136 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3137 | if (i == sh->pd_idx) |
| 3138 | return 0; |
| 3139 | switch(conf->level) { |
| 3140 | case 4: break; |
| 3141 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 3142 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3143 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 3144 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 3145 | if (i > sh->pd_idx) |
| 3146 | i--; |
| 3147 | break; |
| 3148 | case ALGORITHM_LEFT_SYMMETRIC: |
| 3149 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 3150 | if (i < sh->pd_idx) |
| 3151 | i += raid_disks; |
| 3152 | i -= (sh->pd_idx + 1); |
| 3153 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3154 | case ALGORITHM_PARITY_0: |
| 3155 | i -= 1; |
| 3156 | break; |
| 3157 | case ALGORITHM_PARITY_N: |
| 3158 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3159 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3160 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3161 | } |
| 3162 | break; |
| 3163 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3164 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3165 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 3166 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3167 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 3168 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3169 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 3170 | case ALGORITHM_ROTATING_N_RESTART: |
| 3171 | if (sh->pd_idx == raid_disks-1) |
| 3172 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3173 | else if (i > sh->pd_idx) |
| 3174 | i -= 2; /* D D P Q D */ |
| 3175 | break; |
| 3176 | case ALGORITHM_LEFT_SYMMETRIC: |
| 3177 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 3178 | if (sh->pd_idx == raid_disks-1) |
| 3179 | i--; /* Q D D D P */ |
| 3180 | else { |
| 3181 | /* D D P Q D */ |
| 3182 | if (i < sh->pd_idx) |
| 3183 | i += raid_disks; |
| 3184 | i -= (sh->pd_idx + 2); |
| 3185 | } |
| 3186 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3187 | case ALGORITHM_PARITY_0: |
| 3188 | i -= 2; |
| 3189 | break; |
| 3190 | case ALGORITHM_PARITY_N: |
| 3191 | break; |
| 3192 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 3193 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3194 | if (sh->pd_idx == 0) |
| 3195 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 3196 | else { |
| 3197 | /* D D Q P D */ |
| 3198 | if (i < sh->pd_idx) |
| 3199 | i += raid_disks; |
| 3200 | i -= (sh->pd_idx + 1); |
| 3201 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3202 | break; |
| 3203 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 3204 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 3205 | if (i > sh->pd_idx) |
| 3206 | i--; |
| 3207 | break; |
| 3208 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 3209 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 3210 | if (i < sh->pd_idx) |
| 3211 | i += data_disks + 1; |
| 3212 | i -= (sh->pd_idx + 1); |
| 3213 | break; |
| 3214 | case ALGORITHM_PARITY_0_6: |
| 3215 | i -= 1; |
| 3216 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3217 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3218 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3219 | } |
| 3220 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3221 | } |
| 3222 | |
| 3223 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 3224 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3225 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3226 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3227 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3228 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 3229 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3230 | pr_warn("md/raid:%s: compute_blocknr: map not correct\n", |
| 3231 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3232 | return 0; |
| 3233 | } |
| 3234 | return r_sector; |
| 3235 | } |
| 3236 | |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3237 | /* |
| 3238 | * There are cases where we want handle_stripe_dirtying() and |
| 3239 | * schedule_reconstruction() to delay towrite to some dev of a stripe. |
| 3240 | * |
| 3241 | * This function checks whether we want to delay the towrite. Specifically, |
| 3242 | * we delay the towrite when: |
| 3243 | * |
| 3244 | * 1. degraded stripe has a non-overwrite to the missing dev, AND this |
| 3245 | * stripe has data in journal (for other devices). |
| 3246 | * |
| 3247 | * In this case, when reading data for the non-overwrite dev, it is |
| 3248 | * necessary to handle complex rmw of write back cache (prexor with |
| 3249 | * orig_page, and xor with page). To keep read path simple, we would |
| 3250 | * like to flush data in journal to RAID disks first, so complex rmw |
| 3251 | * is handled in the write patch (handle_stripe_dirtying). |
| 3252 | * |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3253 | * 2. when journal space is critical (R5C_LOG_CRITICAL=1) |
| 3254 | * |
| 3255 | * It is important to be able to flush all stripes in raid5-cache. |
| 3256 | * Therefore, we need reserve some space on the journal device for |
| 3257 | * these flushes. If flush operation includes pending writes to the |
| 3258 | * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe |
| 3259 | * for the flush out. If we exclude these pending writes from flush |
| 3260 | * operation, we only need (conf->max_degraded + 1) pages per stripe. |
| 3261 | * Therefore, excluding pending writes in these cases enables more |
| 3262 | * efficient use of the journal device. |
| 3263 | * |
| 3264 | * Note: To make sure the stripe makes progress, we only delay |
| 3265 | * towrite for stripes with data already in journal (injournal > 0). |
| 3266 | * When LOG_CRITICAL, stripes with injournal == 0 will be sent to |
| 3267 | * no_space_stripes list. |
| 3268 | * |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 3269 | * 3. during journal failure |
| 3270 | * In journal failure, we try to flush all cached data to raid disks |
| 3271 | * based on data in stripe cache. The array is read-only to upper |
| 3272 | * layers, so we would skip all pending writes. |
| 3273 | * |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3274 | */ |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3275 | static inline bool delay_towrite(struct r5conf *conf, |
| 3276 | struct r5dev *dev, |
| 3277 | struct stripe_head_state *s) |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3278 | { |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3279 | /* case 1 above */ |
| 3280 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 3281 | !test_bit(R5_Insync, &dev->flags) && s->injournal) |
| 3282 | return true; |
| 3283 | /* case 2 above */ |
| 3284 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && |
| 3285 | s->injournal > 0) |
| 3286 | return true; |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 3287 | /* case 3 above */ |
| 3288 | if (s->log_failed && s->injournal) |
| 3289 | return true; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3290 | return false; |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3291 | } |
| 3292 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3293 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3294 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3295 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3296 | { |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3297 | 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] | 3298 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3299 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3300 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3301 | if (rcw) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3302 | /* |
| 3303 | * In some cases, handle_stripe_dirtying initially decided to |
| 3304 | * run rmw and allocates extra page for prexor. However, rcw is |
| 3305 | * cheaper later on. We need to free the extra page now, |
| 3306 | * because we won't be able to do that in ops_complete_prexor(). |
| 3307 | */ |
| 3308 | r5c_release_extra_page(sh); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3309 | |
| 3310 | for (i = disks; i--; ) { |
| 3311 | struct r5dev *dev = &sh->dev[i]; |
| 3312 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3313 | if (dev->towrite && !delay_towrite(conf, dev, s)) { |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3314 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3315 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3316 | if (!expand) |
| 3317 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3318 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3319 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 3320 | set_bit(R5_LOCKED, &dev->flags); |
| 3321 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3322 | } |
| 3323 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 3324 | /* if we are not expanding this is a proper write request, and |
| 3325 | * there will be bios with new data to be drained into the |
| 3326 | * stripe cache |
| 3327 | */ |
| 3328 | if (!expand) { |
| 3329 | if (!s->locked) |
| 3330 | /* False alarm, nothing to do */ |
| 3331 | return; |
| 3332 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 3333 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 3334 | } else |
| 3335 | sh->reconstruct_state = reconstruct_state_run; |
| 3336 | |
| 3337 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
| 3338 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3339 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3340 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3341 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3342 | } else { |
| 3343 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 3344 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3345 | BUG_ON(level == 6 && |
| 3346 | (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) || |
| 3347 | test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags)))); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3348 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3349 | for (i = disks; i--; ) { |
| 3350 | struct r5dev *dev = &sh->dev[i]; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3351 | if (i == pd_idx || i == qd_idx) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3352 | continue; |
| 3353 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3354 | if (dev->towrite && |
| 3355 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3356 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 3357 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3358 | set_bit(R5_LOCKED, &dev->flags); |
| 3359 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3360 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3361 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 3362 | set_bit(R5_LOCKED, &dev->flags); |
| 3363 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3364 | } |
| 3365 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 3366 | if (!s->locked) |
| 3367 | /* False alarm - nothing to do */ |
| 3368 | return; |
| 3369 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
| 3370 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 3371 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 3372 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3373 | } |
| 3374 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3375 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3376 | * are in flight |
| 3377 | */ |
| 3378 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 3379 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3380 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3381 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3382 | if (level == 6) { |
| 3383 | int qd_idx = sh->qd_idx; |
| 3384 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 3385 | |
| 3386 | set_bit(R5_LOCKED, &dev->flags); |
| 3387 | clear_bit(R5_UPTODATE, &dev->flags); |
| 3388 | s->locked++; |
| 3389 | } |
| 3390 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 3391 | if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page && |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 3392 | test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) && |
| 3393 | !test_bit(STRIPE_FULL_WRITE, &sh->state) && |
| 3394 | test_bit(R5_Insync, &sh->dev[pd_idx].flags)) |
| 3395 | set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request); |
| 3396 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3397 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 3398 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3399 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3400 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3401 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3402 | /* |
| 3403 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3404 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3405 | * The bi_next chain must be in order. |
| 3406 | */ |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3407 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, |
| 3408 | int forwrite, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3409 | { |
| 3410 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3411 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3412 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3413 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3414 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3415 | (unsigned long long)bi->bi_iter.bi_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3416 | (unsigned long long)sh->sector); |
| 3417 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3418 | spin_lock_irq(&sh->stripe_lock); |
Mariusz Dabrowski | 2cd259a | 2018-04-19 19:28:10 +0200 | [diff] [blame] | 3419 | sh->dev[dd_idx].write_hint = bi->bi_write_hint; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3420 | /* Don't allow new IO added to stripes in batch list */ |
| 3421 | if (sh->batch_head) |
| 3422 | goto overlap; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3423 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3424 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3425 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3426 | firstwrite = 1; |
| 3427 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3428 | bip = &sh->dev[dd_idx].toread; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3429 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { |
| 3430 | if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3431 | goto overlap; |
| 3432 | bip = & (*bip)->bi_next; |
| 3433 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3434 | if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3435 | goto overlap; |
| 3436 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 3437 | if (forwrite && raid5_has_ppl(conf)) { |
| 3438 | /* |
| 3439 | * With PPL only writes to consecutive data chunks within a |
| 3440 | * stripe are allowed because for a single stripe_head we can |
| 3441 | * only have one PPL entry at a time, which describes one data |
| 3442 | * range. Not really an overlap, but wait_for_overlap can be |
| 3443 | * used to handle this. |
| 3444 | */ |
| 3445 | sector_t sector; |
| 3446 | sector_t first = 0; |
| 3447 | sector_t last = 0; |
| 3448 | int count = 0; |
| 3449 | int i; |
| 3450 | |
| 3451 | for (i = 0; i < sh->disks; i++) { |
| 3452 | if (i != sh->pd_idx && |
| 3453 | (i == dd_idx || sh->dev[i].towrite)) { |
| 3454 | sector = sh->dev[i].sector; |
| 3455 | if (count == 0 || sector < first) |
| 3456 | first = sector; |
| 3457 | if (sector > last) |
| 3458 | last = sector; |
| 3459 | count++; |
| 3460 | } |
| 3461 | } |
| 3462 | |
| 3463 | if (first + conf->chunk_sectors * (count - 1) != last) |
| 3464 | goto overlap; |
| 3465 | } |
| 3466 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3467 | if (!forwrite || previous) |
| 3468 | clear_bit(STRIPE_BATCH_READY, &sh->state); |
| 3469 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 3470 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3471 | if (*bip) |
| 3472 | bi->bi_next = *bip; |
| 3473 | *bip = bi; |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 3474 | bio_inc_remaining(bi); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3475 | md_write_inc(conf->mddev, bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3476 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3477 | if (forwrite) { |
| 3478 | /* check if page is covered */ |
| 3479 | sector_t sector = sh->dev[dd_idx].sector; |
| 3480 | for (bi=sh->dev[dd_idx].towrite; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3481 | sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3482 | bi && bi->bi_iter.bi_sector <= sector; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3483 | bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) { |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 3484 | if (bio_end_sector(bi) >= sector) |
| 3485 | sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3486 | } |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3487 | if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf)) |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3488 | if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags)) |
| 3489 | sh->overwrite_disks++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3490 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3491 | |
| 3492 | 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] | 3493 | (unsigned long long)(*bip)->bi_iter.bi_sector, |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3494 | (unsigned long long)sh->sector, dd_idx); |
| 3495 | |
| 3496 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3497 | /* Cannot hold spinlock over bitmap_startwrite, |
| 3498 | * but must ensure this isn't added to a batch until |
| 3499 | * we have added to the bitmap and set bm_seq. |
| 3500 | * So set STRIPE_BITMAP_PENDING to prevent |
| 3501 | * batching. |
| 3502 | * If multiple add_stripe_bio() calls race here they |
| 3503 | * much all set STRIPE_BITMAP_PENDING. So only the first one |
| 3504 | * to complete "bitmap_startwrite" gets to set |
| 3505 | * STRIPE_BIT_DELAY. This is important as once a stripe |
| 3506 | * is added to a batch, STRIPE_BIT_DELAY cannot be changed |
| 3507 | * any more. |
| 3508 | */ |
| 3509 | set_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3510 | spin_unlock_irq(&sh->stripe_lock); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3511 | md_bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3512 | RAID5_STRIPE_SECTORS(conf), 0); |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3513 | spin_lock_irq(&sh->stripe_lock); |
| 3514 | clear_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3515 | if (!sh->batch_head) { |
| 3516 | sh->bm_seq = conf->seq_flush+1; |
| 3517 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 3518 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3519 | } |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3520 | spin_unlock_irq(&sh->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3521 | |
| 3522 | if (stripe_can_batch(sh)) |
| 3523 | stripe_add_to_batch_list(conf, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3524 | return 1; |
| 3525 | |
| 3526 | overlap: |
| 3527 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3528 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3529 | return 0; |
| 3530 | } |
| 3531 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3532 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3533 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3534 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3535 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3536 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3537 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 3538 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3539 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3540 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3541 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3542 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3543 | raid5_compute_sector(conf, |
| 3544 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 3545 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3546 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3547 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3548 | } |
| 3549 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3550 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3551 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3552 | struct stripe_head_state *s, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3553 | { |
| 3554 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3555 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3556 | for (i = disks; i--; ) { |
| 3557 | struct bio *bi; |
| 3558 | int bitmap_end = 0; |
| 3559 | |
| 3560 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3561 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3562 | rcu_read_lock(); |
| 3563 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 3564 | if (rdev && test_bit(In_sync, &rdev->flags) && |
| 3565 | !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3566 | atomic_inc(&rdev->nr_pending); |
| 3567 | else |
| 3568 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3569 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3570 | if (rdev) { |
| 3571 | if (!rdev_set_badblocks( |
| 3572 | rdev, |
| 3573 | sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3574 | RAID5_STRIPE_SECTORS(conf), 0)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3575 | md_error(conf->mddev, rdev); |
| 3576 | rdev_dec_pending(rdev, conf->mddev); |
| 3577 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3578 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3579 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3580 | /* fail all writes first */ |
| 3581 | bi = sh->dev[i].towrite; |
| 3582 | sh->dev[i].towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3583 | sh->overwrite_disks = 0; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3584 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 1ed850f | 2012-10-11 13:50:13 +1100 | [diff] [blame] | 3585 | if (bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3586 | bitmap_end = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3587 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 3588 | log_stripe_write_finished(sh); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3589 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3590 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3591 | wake_up(&conf->wait_for_overlap); |
| 3592 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3593 | while (bi && bi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3594 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
| 3595 | struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3596 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3597 | md_write_end(conf->mddev); |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 3598 | bio_io_error(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3599 | bi = nextbi; |
| 3600 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3601 | if (bitmap_end) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3602 | md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3603 | RAID5_STRIPE_SECTORS(conf), 0, 0); |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3604 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3605 | /* and fail all 'written' */ |
| 3606 | bi = sh->dev[i].written; |
| 3607 | sh->dev[i].written = NULL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3608 | if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { |
| 3609 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 3610 | sh->dev[i].page = sh->dev[i].orig_page; |
| 3611 | } |
| 3612 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3613 | if (bi) bitmap_end = 1; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3614 | while (bi && bi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3615 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
| 3616 | struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3617 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3618 | md_write_end(conf->mddev); |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 3619 | bio_io_error(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3620 | bi = bi2; |
| 3621 | } |
| 3622 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3623 | /* fail any reads if this device is non-operational and |
| 3624 | * the data has not reached the cache yet. |
| 3625 | */ |
| 3626 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 3627 | s->failed > conf->max_degraded && |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3628 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 3629 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3630 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3631 | bi = sh->dev[i].toread; |
| 3632 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3633 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3634 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3635 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3636 | if (bi) |
| 3637 | s->to_read--; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3638 | while (bi && bi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3639 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3640 | struct bio *nextbi = |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3641 | r5_next_bio(conf, bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3642 | |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 3643 | bio_io_error(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3644 | bi = nextbi; |
| 3645 | } |
| 3646 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3647 | if (bitmap_end) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3648 | md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3649 | RAID5_STRIPE_SECTORS(conf), 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3650 | /* If we were in the middle of a write the parity block might |
| 3651 | * still be locked - so just clear all R5_LOCKED flags |
| 3652 | */ |
| 3653 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3654 | } |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3655 | s->to_write = 0; |
| 3656 | s->written = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3657 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3658 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3659 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3660 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3661 | } |
| 3662 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3663 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3664 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3665 | struct stripe_head_state *s) |
| 3666 | { |
| 3667 | int abort = 0; |
| 3668 | int i; |
| 3669 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3670 | BUG_ON(sh->batch_head); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3671 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3672 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 3673 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3674 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3675 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3676 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3677 | * Don't even need to abort as that is handled elsewhere |
| 3678 | * if needed, and not always wanted e.g. if there is a known |
| 3679 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3680 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3681 | * non-sync devices, or abort the recovery |
| 3682 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3683 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 3684 | /* During recovery devices cannot be removed, so |
| 3685 | * locking and refcounting of rdevs is not needed |
| 3686 | */ |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3687 | rcu_read_lock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3688 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3689 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3690 | if (rdev |
| 3691 | && !test_bit(Faulty, &rdev->flags) |
| 3692 | && !test_bit(In_sync, &rdev->flags) |
| 3693 | && !rdev_set_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3694 | RAID5_STRIPE_SECTORS(conf), 0)) |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3695 | abort = 1; |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3696 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3697 | if (rdev |
| 3698 | && !test_bit(Faulty, &rdev->flags) |
| 3699 | && !test_bit(In_sync, &rdev->flags) |
| 3700 | && !rdev_set_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3701 | RAID5_STRIPE_SECTORS(conf), 0)) |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3702 | abort = 1; |
| 3703 | } |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3704 | rcu_read_unlock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3705 | if (abort) |
| 3706 | conf->recovery_disabled = |
| 3707 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3708 | } |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3709 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3710 | } |
| 3711 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3712 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 3713 | { |
| 3714 | struct md_rdev *rdev; |
| 3715 | int rv = 0; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3716 | |
| 3717 | rcu_read_lock(); |
| 3718 | rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3719 | if (rdev |
| 3720 | && !test_bit(Faulty, &rdev->flags) |
| 3721 | && !test_bit(In_sync, &rdev->flags) |
| 3722 | && (rdev->recovery_offset <= sh->sector |
| 3723 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 3724 | rv = 1; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3725 | rcu_read_unlock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3726 | return rv; |
| 3727 | } |
| 3728 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3729 | static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3730 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3731 | { |
| 3732 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3733 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 3734 | &sh->dev[s->failed_num[1]] }; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3735 | int i; |
ChangSyun Peng | 45a4d8f | 2020-07-31 17:50:31 +0800 | [diff] [blame] | 3736 | bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3737 | |
NeilBrown | a79cfe1 | 2015-02-02 11:37:59 +1100 | [diff] [blame] | 3738 | |
| 3739 | if (test_bit(R5_LOCKED, &dev->flags) || |
| 3740 | test_bit(R5_UPTODATE, &dev->flags)) |
| 3741 | /* No point reading this as we already have it or have |
| 3742 | * decided to get it. |
| 3743 | */ |
| 3744 | return 0; |
| 3745 | |
| 3746 | if (dev->toread || |
| 3747 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags))) |
| 3748 | /* We need this block to directly satisfy a request */ |
| 3749 | return 1; |
| 3750 | |
| 3751 | if (s->syncing || s->expanding || |
| 3752 | (s->replacing && want_replace(sh, disk_idx))) |
| 3753 | /* When syncing, or expanding we read everything. |
| 3754 | * When replacing, we need the replaced block. |
| 3755 | */ |
| 3756 | return 1; |
| 3757 | |
| 3758 | if ((s->failed >= 1 && fdev[0]->toread) || |
| 3759 | (s->failed >= 2 && fdev[1]->toread)) |
| 3760 | /* If we want to read from a failed device, then |
| 3761 | * we need to actually read every other device. |
| 3762 | */ |
| 3763 | return 1; |
| 3764 | |
NeilBrown | a9d5695 | 2015-02-02 11:49:10 +1100 | [diff] [blame] | 3765 | /* Sometimes neither read-modify-write nor reconstruct-write |
| 3766 | * cycles can work. In those cases we read every block we |
| 3767 | * can. Then the parity-update is certain to have enough to |
| 3768 | * work with. |
| 3769 | * This can only be a problem when we need to write something, |
| 3770 | * and some device has failed. If either of those tests |
| 3771 | * fail we need look no further. |
| 3772 | */ |
| 3773 | if (!s->failed || !s->to_write) |
| 3774 | return 0; |
| 3775 | |
| 3776 | if (test_bit(R5_Insync, &dev->flags) && |
| 3777 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3778 | /* Pre-reads at not permitted until after short delay |
| 3779 | * to gather multiple requests. However if this |
Zhilong Liu | 3560741e | 2017-03-15 16:14:53 +0800 | [diff] [blame] | 3780 | * device is no Insync, the block could only be computed |
NeilBrown | a9d5695 | 2015-02-02 11:49:10 +1100 | [diff] [blame] | 3781 | * and there is no need to delay that. |
| 3782 | */ |
| 3783 | return 0; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3784 | |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3785 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3786 | if (fdev[i]->towrite && |
| 3787 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
| 3788 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3789 | /* If we have a partial write to a failed |
| 3790 | * device, then we will need to reconstruct |
| 3791 | * the content of that device, so all other |
| 3792 | * devices must be read. |
| 3793 | */ |
| 3794 | return 1; |
ChangSyun Peng | 45a4d8f | 2020-07-31 17:50:31 +0800 | [diff] [blame] | 3795 | |
| 3796 | if (s->failed >= 2 && |
| 3797 | (fdev[i]->towrite || |
| 3798 | s->failed_num[i] == sh->pd_idx || |
| 3799 | s->failed_num[i] == sh->qd_idx) && |
| 3800 | !test_bit(R5_UPTODATE, &fdev[i]->flags)) |
| 3801 | /* In max degraded raid6, If the failed disk is P, Q, |
| 3802 | * or we want to read the failed disk, we need to do |
| 3803 | * reconstruct-write. |
| 3804 | */ |
| 3805 | force_rcw = true; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3806 | } |
| 3807 | |
ChangSyun Peng | 45a4d8f | 2020-07-31 17:50:31 +0800 | [diff] [blame] | 3808 | /* If we are forced to do a reconstruct-write, because parity |
| 3809 | * cannot be trusted and we are currently recovering it, there |
| 3810 | * is extra need to be careful. |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3811 | * If one of the devices that we would need to read, because |
| 3812 | * it is not being overwritten (and maybe not written at all) |
| 3813 | * is missing/faulty, then we need to read everything we can. |
| 3814 | */ |
ChangSyun Peng | 45a4d8f | 2020-07-31 17:50:31 +0800 | [diff] [blame] | 3815 | if (!force_rcw && |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3816 | sh->sector < sh->raid_conf->mddev->recovery_cp) |
| 3817 | /* reconstruct-write isn't being forced */ |
| 3818 | return 0; |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3819 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | 10d82c5 | 2015-05-08 18:19:33 +1000 | [diff] [blame] | 3820 | if (s->failed_num[i] != sh->pd_idx && |
| 3821 | s->failed_num[i] != sh->qd_idx && |
| 3822 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3823 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3824 | return 1; |
| 3825 | } |
| 3826 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3827 | return 0; |
| 3828 | } |
| 3829 | |
Song Liu | ba02684 | 2017-01-12 17:22:42 -0800 | [diff] [blame] | 3830 | /* fetch_block - checks the given member device to see if its data needs |
| 3831 | * to be read or computed to satisfy a request. |
| 3832 | * |
| 3833 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 3834 | * 0 to tell the loop in handle_stripe_fill to continue |
| 3835 | */ |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3836 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3837 | int disk_idx, int disks) |
| 3838 | { |
| 3839 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 3840 | |
| 3841 | /* is the data in this block needed, and can we get it? */ |
| 3842 | if (need_this_block(sh, s, disk_idx, disks)) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3843 | /* we would like to get this block, possibly by computing it, |
| 3844 | * otherwise read it if the backing disk is insync |
| 3845 | */ |
| 3846 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 3847 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
NeilBrown | b0c783b | 2015-05-08 18:19:32 +1000 | [diff] [blame] | 3848 | BUG_ON(sh->batch_head); |
NeilBrown | 7471fb7 | 2017-04-03 12:11:32 +1000 | [diff] [blame] | 3849 | |
| 3850 | /* |
| 3851 | * In the raid6 case if the only non-uptodate disk is P |
| 3852 | * then we already trusted P to compute the other failed |
| 3853 | * drives. It is safe to compute rather than re-read P. |
| 3854 | * In other cases we only compute blocks from failed |
| 3855 | * devices, otherwise check/repair might fail to detect |
| 3856 | * a real inconsistency. |
| 3857 | */ |
| 3858 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3859 | if ((s->uptodate == disks - 1) && |
NeilBrown | 7471fb7 | 2017-04-03 12:11:32 +1000 | [diff] [blame] | 3860 | ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) || |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3861 | (s->failed && (disk_idx == s->failed_num[0] || |
NeilBrown | 7471fb7 | 2017-04-03 12:11:32 +1000 | [diff] [blame] | 3862 | disk_idx == s->failed_num[1])))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3863 | /* have disk failed, and we're requested to fetch it; |
| 3864 | * do compute it |
| 3865 | */ |
| 3866 | pr_debug("Computing stripe %llu block %d\n", |
| 3867 | (unsigned long long)sh->sector, disk_idx); |
| 3868 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3869 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3870 | set_bit(R5_Wantcompute, &dev->flags); |
| 3871 | sh->ops.target = disk_idx; |
| 3872 | sh->ops.target2 = -1; /* no 2nd target */ |
| 3873 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3874 | /* Careful: from this point on 'uptodate' is in the eye |
| 3875 | * of raid_run_ops which services 'compute' operations |
| 3876 | * before writes. R5_Wantcompute flags a block that will |
| 3877 | * be R5_UPTODATE by the time it is needed for a |
| 3878 | * subsequent operation. |
| 3879 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3880 | s->uptodate++; |
| 3881 | return 1; |
| 3882 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 3883 | /* Computing 2-failure is *very* expensive; only |
| 3884 | * do it if failed >= 2 |
| 3885 | */ |
| 3886 | int other; |
| 3887 | for (other = disks; other--; ) { |
| 3888 | if (other == disk_idx) |
| 3889 | continue; |
| 3890 | if (!test_bit(R5_UPTODATE, |
| 3891 | &sh->dev[other].flags)) |
| 3892 | break; |
| 3893 | } |
| 3894 | BUG_ON(other < 0); |
| 3895 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 3896 | (unsigned long long)sh->sector, |
| 3897 | disk_idx, other); |
| 3898 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3899 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3900 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 3901 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 3902 | sh->ops.target = disk_idx; |
| 3903 | sh->ops.target2 = other; |
| 3904 | s->uptodate += 2; |
| 3905 | s->req_compute = 1; |
| 3906 | return 1; |
| 3907 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 3908 | set_bit(R5_LOCKED, &dev->flags); |
| 3909 | set_bit(R5_Wantread, &dev->flags); |
| 3910 | s->locked++; |
| 3911 | pr_debug("Reading block %d (sync=%d)\n", |
| 3912 | disk_idx, s->syncing); |
| 3913 | } |
| 3914 | } |
| 3915 | |
| 3916 | return 0; |
| 3917 | } |
| 3918 | |
Damien Le Moal | 2aada5b | 2020-07-16 13:54:42 +0900 | [diff] [blame] | 3919 | /* |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3920 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3921 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3922 | static void handle_stripe_fill(struct stripe_head *sh, |
| 3923 | struct stripe_head_state *s, |
| 3924 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3925 | { |
| 3926 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3927 | |
| 3928 | /* look for blocks to read/compute, skip this if a compute |
| 3929 | * is already in flight, or if the stripe contents are in the |
| 3930 | * midst of changing due to a write |
| 3931 | */ |
| 3932 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3933 | !sh->reconstruct_state) { |
| 3934 | |
| 3935 | /* |
| 3936 | * For degraded stripe with data in journal, do not handle |
| 3937 | * read requests yet, instead, flush the stripe to raid |
| 3938 | * disks first, this avoids handling complex rmw of write |
| 3939 | * back cache (prexor with orig_page, and then xor with |
| 3940 | * page) in the read path |
| 3941 | */ |
| 3942 | if (s->injournal && s->failed) { |
| 3943 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
| 3944 | r5c_make_stripe_write_out(sh); |
| 3945 | goto out; |
| 3946 | } |
| 3947 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3948 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3949 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3950 | break; |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3951 | } |
| 3952 | out: |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3953 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3954 | } |
| 3955 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 3956 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 3957 | unsigned long handle_flags); |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3958 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3959 | * any written block on an uptodate or failed drive can be returned. |
| 3960 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 3961 | * never LOCKED, so we don't need to test 'failed' directly. |
| 3962 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3963 | static void handle_stripe_clean_event(struct r5conf *conf, |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3964 | struct stripe_head *sh, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3965 | { |
| 3966 | int i; |
| 3967 | struct r5dev *dev; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3968 | int discard_pending = 0; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3969 | struct stripe_head *head_sh = sh; |
| 3970 | bool do_endio = false; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3971 | |
| 3972 | for (i = disks; i--; ) |
| 3973 | if (sh->dev[i].written) { |
| 3974 | dev = &sh->dev[i]; |
| 3975 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3976 | (test_bit(R5_UPTODATE, &dev->flags) || |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3977 | test_bit(R5_Discard, &dev->flags) || |
| 3978 | test_bit(R5_SkipCopy, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3979 | /* We can return any write requests */ |
| 3980 | struct bio *wbi, *wbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3981 | pr_debug("Return write for disc %d\n", i); |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 3982 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
| 3983 | clear_bit(R5_UPTODATE, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3984 | if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { |
| 3985 | WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3986 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3987 | do_endio = true; |
| 3988 | |
| 3989 | returnbi: |
| 3990 | dev->page = dev->orig_page; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3991 | wbi = dev->written; |
| 3992 | dev->written = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3993 | while (wbi && wbi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3994 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
| 3995 | wbi2 = r5_next_bio(conf, wbi, dev->sector); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3996 | md_write_end(conf->mddev); |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 3997 | bio_endio(wbi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3998 | wbi = wbi2; |
| 3999 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4000 | md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4001 | RAID5_STRIPE_SECTORS(conf), |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 4002 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 4003 | 0); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4004 | if (head_sh->batch_head) { |
| 4005 | sh = list_first_entry(&sh->batch_list, |
| 4006 | struct stripe_head, |
| 4007 | batch_list); |
| 4008 | if (sh != head_sh) { |
| 4009 | dev = &sh->dev[i]; |
| 4010 | goto returnbi; |
| 4011 | } |
| 4012 | } |
| 4013 | sh = head_sh; |
| 4014 | dev = &sh->dev[i]; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4015 | } else if (test_bit(R5_Discard, &dev->flags)) |
| 4016 | discard_pending = 1; |
| 4017 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 4018 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 4019 | log_stripe_write_finished(sh); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 4020 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4021 | if (!discard_pending && |
| 4022 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 4023 | int hash; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4024 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 4025 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
| 4026 | if (sh->qd_idx >= 0) { |
| 4027 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 4028 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); |
| 4029 | } |
| 4030 | /* now that discard is done we can proceed with any sync */ |
| 4031 | clear_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 4032 | /* |
| 4033 | * SCSI discard will change some bio fields and the stripe has |
| 4034 | * no updated data, so remove it from hash list and the stripe |
| 4035 | * will be reinitialized |
| 4036 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4037 | unhash: |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 4038 | hash = sh->hash_lock_index; |
| 4039 | spin_lock_irq(conf->hash_locks + hash); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 4040 | remove_hash(sh); |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 4041 | spin_unlock_irq(conf->hash_locks + hash); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4042 | if (head_sh->batch_head) { |
| 4043 | sh = list_first_entry(&sh->batch_list, |
| 4044 | struct stripe_head, batch_list); |
| 4045 | if (sh != head_sh) |
| 4046 | goto unhash; |
| 4047 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4048 | sh = head_sh; |
| 4049 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4050 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
| 4051 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4052 | |
| 4053 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4054 | |
| 4055 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 4056 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 4057 | md_wakeup_thread(conf->mddev->thread); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4058 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 4059 | if (head_sh->batch_head && do_endio) |
| 4060 | break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4061 | } |
| 4062 | |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 4063 | /* |
| 4064 | * For RMW in write back cache, we need extra page in prexor to store the |
| 4065 | * old data. This page is stored in dev->orig_page. |
| 4066 | * |
| 4067 | * This function checks whether we have data for prexor. The exact logic |
| 4068 | * is: |
| 4069 | * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE) |
| 4070 | */ |
| 4071 | static inline bool uptodate_for_rmw(struct r5dev *dev) |
| 4072 | { |
| 4073 | return (test_bit(R5_UPTODATE, &dev->flags)) && |
| 4074 | (!test_bit(R5_InJournal, &dev->flags) || |
| 4075 | test_bit(R5_OrigPageUPTDODATE, &dev->flags)); |
| 4076 | } |
| 4077 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4078 | static int handle_stripe_dirtying(struct r5conf *conf, |
| 4079 | struct stripe_head *sh, |
| 4080 | struct stripe_head_state *s, |
| 4081 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4082 | { |
| 4083 | int rmw = 0, rcw = 0, i; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 4084 | sector_t recovery_cp = conf->mddev->recovery_cp; |
| 4085 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 4086 | /* Check whether resync is now happening or should start. |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 4087 | * If yes, then the array is dirty (after unclean shutdown or |
| 4088 | * initial creation), so parity in some stripes might be inconsistent. |
| 4089 | * In this case, we need to always do reconstruct-write, to ensure |
| 4090 | * that in case of drive failure or read-error correction, we |
| 4091 | * generate correct data from the parity. |
| 4092 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 4093 | if (conf->rmw_level == PARITY_DISABLE_RMW || |
NeilBrown | 26ac107 | 2015-02-18 11:35:14 +1100 | [diff] [blame] | 4094 | (recovery_cp < MaxSector && sh->sector >= recovery_cp && |
| 4095 | s->failed == 0)) { |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 4096 | /* Calculate the real rcw later - for now make it |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4097 | * look like rcw is cheaper |
| 4098 | */ |
| 4099 | rcw = 1; rmw = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 4100 | pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n", |
| 4101 | conf->rmw_level, (unsigned long long)recovery_cp, |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 4102 | (unsigned long long)sh->sector); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4103 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4104 | /* would I have to read this buffer for read_modify_write */ |
| 4105 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 4106 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 4107 | i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4108 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4109 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 4110 | !(uptodate_for_rmw(dev) || |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4111 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4112 | if (test_bit(R5_Insync, &dev->flags)) |
| 4113 | rmw++; |
| 4114 | else |
| 4115 | rmw += 2*disks; /* cannot read it */ |
| 4116 | } |
| 4117 | /* Would I have to read this buffer for reconstruct_write */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 4118 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 4119 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4120 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4121 | !(test_bit(R5_UPTODATE, &dev->flags) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4122 | test_bit(R5_Wantcompute, &dev->flags))) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 4123 | if (test_bit(R5_Insync, &dev->flags)) |
| 4124 | rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4125 | else |
| 4126 | rcw += 2*disks; |
| 4127 | } |
| 4128 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4129 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 4130 | pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n", |
| 4131 | (unsigned long long)sh->sector, sh->state, rmw, rcw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4132 | set_bit(STRIPE_HANDLE, &sh->state); |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 4133 | 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] | 4134 | /* prefer read-modify-write, but need to get some data */ |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4135 | if (conf->mddev->queue) |
| 4136 | blk_add_trace_msg(conf->mddev->queue, |
| 4137 | "raid5 rmw %llu %d", |
| 4138 | (unsigned long long)sh->sector, rmw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4139 | for (i = disks; i--; ) { |
| 4140 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4141 | if (test_bit(R5_InJournal, &dev->flags) && |
| 4142 | dev->page == dev->orig_page && |
| 4143 | !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) { |
| 4144 | /* alloc page for prexor */ |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4145 | struct page *p = alloc_page(GFP_NOIO); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4146 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4147 | if (p) { |
| 4148 | dev->orig_page = p; |
| 4149 | continue; |
| 4150 | } |
| 4151 | |
| 4152 | /* |
| 4153 | * alloc_page() failed, try use |
| 4154 | * disk_info->extra_page |
| 4155 | */ |
| 4156 | if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE, |
| 4157 | &conf->cache_state)) { |
| 4158 | r5c_use_extra_page(sh); |
| 4159 | break; |
| 4160 | } |
| 4161 | |
| 4162 | /* extra_page in use, add to delayed_list */ |
| 4163 | set_bit(STRIPE_DELAYED, &sh->state); |
| 4164 | s->waiting_extra_page = 1; |
| 4165 | return -EAGAIN; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4166 | } |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4167 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4168 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4169 | for (i = disks; i--; ) { |
| 4170 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 4171 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4172 | i == sh->pd_idx || i == sh->qd_idx || |
| 4173 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4174 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 4175 | !(uptodate_for_rmw(dev) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4176 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4177 | test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 4178 | if (test_bit(STRIPE_PREREAD_ACTIVE, |
| 4179 | &sh->state)) { |
| 4180 | pr_debug("Read_old block %d for r-m-w\n", |
| 4181 | i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4182 | set_bit(R5_LOCKED, &dev->flags); |
| 4183 | set_bit(R5_Wantread, &dev->flags); |
| 4184 | s->locked++; |
Guoqing Jiang | e3914d5 | 2020-07-28 12:01:40 +0200 | [diff] [blame] | 4185 | } else |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4186 | set_bit(STRIPE_DELAYED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4187 | } |
| 4188 | } |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4189 | } |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 4190 | 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] | 4191 | /* want reconstruct write, but need to get some data */ |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4192 | int qread =0; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4193 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4194 | for (i = disks; i--; ) { |
| 4195 | struct r5dev *dev = &sh->dev[i]; |
| 4196 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4197 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4198 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4199 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4200 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 4201 | rcw++; |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 4202 | if (test_bit(R5_Insync, &dev->flags) && |
| 4203 | test_bit(STRIPE_PREREAD_ACTIVE, |
| 4204 | &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4205 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4206 | "%d for Reconstruct\n", i); |
| 4207 | set_bit(R5_LOCKED, &dev->flags); |
| 4208 | set_bit(R5_Wantread, &dev->flags); |
| 4209 | s->locked++; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4210 | qread++; |
Guoqing Jiang | e3914d5 | 2020-07-28 12:01:40 +0200 | [diff] [blame] | 4211 | } else |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4212 | set_bit(STRIPE_DELAYED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4213 | } |
| 4214 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4215 | if (rcw && conf->mddev->queue) |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4216 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
| 4217 | (unsigned long long)sh->sector, |
| 4218 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4219 | } |
NeilBrown | b1b02fe | 2015-02-02 10:44:29 +1100 | [diff] [blame] | 4220 | |
| 4221 | if (rcw > disks && rmw > disks && |
| 4222 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4223 | set_bit(STRIPE_DELAYED, &sh->state); |
| 4224 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4225 | /* now if nothing is locked, and if we have enough data, |
| 4226 | * we can start a write request |
| 4227 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4228 | /* since handle_stripe can be called at any time we need to handle the |
| 4229 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 4230 | * subsequent call wants to start a write request. raid_run_ops only |
| 4231 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4232 | * simultaneously. If this is not the case then new writes need to be |
| 4233 | * held off until the compute completes. |
| 4234 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 4235 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 4236 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4237 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 4238 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4239 | return 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4240 | } |
| 4241 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4242 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4243 | struct stripe_head_state *s, int disks) |
| 4244 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4245 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4246 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4247 | BUG_ON(sh->batch_head); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4248 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4249 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4250 | switch (sh->check_state) { |
| 4251 | case check_state_idle: |
| 4252 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4253 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4254 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4255 | sh->check_state = check_state_run; |
| 4256 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4257 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4258 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4259 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4260 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4261 | dev = &sh->dev[s->failed_num[0]]; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4262 | fallthrough; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4263 | case check_state_compute_result: |
| 4264 | sh->check_state = check_state_idle; |
| 4265 | if (!dev) |
| 4266 | dev = &sh->dev[sh->pd_idx]; |
| 4267 | |
| 4268 | /* check that a write has not made the stripe insync */ |
| 4269 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 4270 | break; |
| 4271 | |
| 4272 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4273 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 4274 | BUG_ON(s->uptodate != disks); |
| 4275 | |
| 4276 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4277 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4278 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4279 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4280 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4281 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4282 | break; |
| 4283 | case check_state_run: |
| 4284 | break; /* we will be called again upon completion */ |
| 4285 | case check_state_check_result: |
| 4286 | sh->check_state = check_state_idle; |
| 4287 | |
| 4288 | /* if a failure occurred during the check operation, leave |
| 4289 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 4290 | */ |
| 4291 | if (s->failed) |
| 4292 | break; |
| 4293 | |
| 4294 | /* handle a successful check operation, if parity is correct |
| 4295 | * we are done. Otherwise update the mismatch count and repair |
| 4296 | * parity if !MD_RECOVERY_CHECK |
| 4297 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 4298 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4299 | /* parity is correct (on disc, |
| 4300 | * not in buffer any more) |
| 4301 | */ |
| 4302 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4303 | else { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4304 | atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4305 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4306 | /* don't try to repair!! */ |
| 4307 | set_bit(STRIPE_INSYNC, &sh->state); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4308 | pr_warn_ratelimited("%s: mismatch sector in range " |
| 4309 | "%llu-%llu\n", mdname(conf->mddev), |
| 4310 | (unsigned long long) sh->sector, |
| 4311 | (unsigned long long) sh->sector + |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4312 | RAID5_STRIPE_SECTORS(conf)); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4313 | } else { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4314 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 4315 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4316 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 4317 | set_bit(R5_Wantcompute, |
| 4318 | &sh->dev[sh->pd_idx].flags); |
| 4319 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 4320 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4321 | s->uptodate++; |
| 4322 | } |
| 4323 | } |
| 4324 | break; |
| 4325 | case check_state_compute_run: |
| 4326 | break; |
| 4327 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4328 | pr_err("%s: unknown check_state: %d sector: %llu\n", |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4329 | __func__, sh->check_state, |
| 4330 | (unsigned long long) sh->sector); |
| 4331 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4332 | } |
| 4333 | } |
| 4334 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4335 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4336 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4337 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4338 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4339 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 4340 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4341 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4342 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4343 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4344 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4345 | |
| 4346 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4347 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4348 | /* Want to check and possibly repair P and Q. |
| 4349 | * However there could be one 'failed' device, in which |
| 4350 | * case we can only check one of them, possibly using the |
| 4351 | * other to generate missing data |
| 4352 | */ |
| 4353 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4354 | switch (sh->check_state) { |
| 4355 | case check_state_idle: |
| 4356 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4357 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4358 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4359 | * makes sense to check P (If anything else were failed, |
| 4360 | * we would have used P to recreate it). |
| 4361 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4362 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4363 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4364 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4365 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4366 | * anything, so it makes sense to check it |
| 4367 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4368 | if (sh->check_state == check_state_run) |
| 4369 | sh->check_state = check_state_run_pq; |
| 4370 | else |
| 4371 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4372 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4373 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4374 | /* discard potentially stale zero_sum_result */ |
| 4375 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4376 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4377 | if (sh->check_state == check_state_run) { |
| 4378 | /* async_xor_zero_sum destroys the contents of P */ |
| 4379 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 4380 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4381 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4382 | if (sh->check_state >= check_state_run && |
| 4383 | sh->check_state <= check_state_run_pq) { |
| 4384 | /* async_syndrome_zero_sum preserves P and Q, so |
| 4385 | * no need to mark them !uptodate here |
| 4386 | */ |
| 4387 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 4388 | break; |
| 4389 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4390 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4391 | /* we have 2-disk failure */ |
| 4392 | BUG_ON(s->failed != 2); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4393 | fallthrough; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4394 | case check_state_compute_result: |
| 4395 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4396 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4397 | /* check that a write has not made the stripe insync */ |
| 4398 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 4399 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4400 | |
| 4401 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4402 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4403 | */ |
Nigel Croxon | b2176a1 | 2019-04-16 09:50:09 -0700 | [diff] [blame] | 4404 | dev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4405 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4406 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4407 | s->locked++; |
| 4408 | set_bit(R5_LOCKED, &dev->flags); |
| 4409 | set_bit(R5_Wantwrite, &dev->flags); |
| 4410 | } |
| 4411 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4412 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4413 | s->locked++; |
| 4414 | set_bit(R5_LOCKED, &dev->flags); |
| 4415 | set_bit(R5_Wantwrite, &dev->flags); |
| 4416 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4417 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4418 | dev = &sh->dev[pd_idx]; |
| 4419 | s->locked++; |
| 4420 | set_bit(R5_LOCKED, &dev->flags); |
| 4421 | set_bit(R5_Wantwrite, &dev->flags); |
| 4422 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4423 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4424 | dev = &sh->dev[qd_idx]; |
| 4425 | s->locked++; |
| 4426 | set_bit(R5_LOCKED, &dev->flags); |
| 4427 | set_bit(R5_Wantwrite, &dev->flags); |
| 4428 | } |
Nigel Croxon | b2176a1 | 2019-04-16 09:50:09 -0700 | [diff] [blame] | 4429 | if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags), |
| 4430 | "%s: disk%td not up to date\n", |
| 4431 | mdname(conf->mddev), |
| 4432 | dev - (struct r5dev *) &sh->dev)) { |
| 4433 | clear_bit(R5_LOCKED, &dev->flags); |
| 4434 | clear_bit(R5_Wantwrite, &dev->flags); |
| 4435 | s->locked--; |
| 4436 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4437 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 4438 | |
| 4439 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4440 | break; |
| 4441 | case check_state_run: |
| 4442 | case check_state_run_q: |
| 4443 | case check_state_run_pq: |
| 4444 | break; /* we will be called again upon completion */ |
| 4445 | case check_state_check_result: |
| 4446 | sh->check_state = check_state_idle; |
| 4447 | |
| 4448 | /* handle a successful check operation, if parity is correct |
| 4449 | * we are done. Otherwise update the mismatch count and repair |
| 4450 | * parity if !MD_RECOVERY_CHECK |
| 4451 | */ |
| 4452 | if (sh->ops.zero_sum_result == 0) { |
Song Liu | a25d8c3 | 2019-04-16 09:34:21 -0700 | [diff] [blame] | 4453 | /* both parities are correct */ |
| 4454 | if (!s->failed) |
| 4455 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4456 | else { |
| 4457 | /* in contrast to the raid5 case we can validate |
| 4458 | * parity, but still have a failure to write |
| 4459 | * back |
| 4460 | */ |
| 4461 | sh->check_state = check_state_compute_result; |
| 4462 | /* Returning at this point means that we may go |
| 4463 | * off and bring p and/or q uptodate again so |
| 4464 | * we make sure to check zero_sum_result again |
| 4465 | * to verify if p or q need writeback |
| 4466 | */ |
| 4467 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4468 | } else { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4469 | atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4470 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4471 | /* don't try to repair!! */ |
| 4472 | set_bit(STRIPE_INSYNC, &sh->state); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4473 | pr_warn_ratelimited("%s: mismatch sector in range " |
| 4474 | "%llu-%llu\n", mdname(conf->mddev), |
| 4475 | (unsigned long long) sh->sector, |
| 4476 | (unsigned long long) sh->sector + |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4477 | RAID5_STRIPE_SECTORS(conf)); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4478 | } else { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4479 | int *target = &sh->ops.target; |
| 4480 | |
| 4481 | sh->ops.target = -1; |
| 4482 | sh->ops.target2 = -1; |
| 4483 | sh->check_state = check_state_compute_run; |
| 4484 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 4485 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 4486 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 4487 | set_bit(R5_Wantcompute, |
| 4488 | &sh->dev[pd_idx].flags); |
| 4489 | *target = pd_idx; |
| 4490 | target = &sh->ops.target2; |
| 4491 | s->uptodate++; |
| 4492 | } |
| 4493 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 4494 | set_bit(R5_Wantcompute, |
| 4495 | &sh->dev[qd_idx].flags); |
| 4496 | *target = qd_idx; |
| 4497 | s->uptodate++; |
| 4498 | } |
| 4499 | } |
| 4500 | } |
| 4501 | break; |
| 4502 | case check_state_compute_run: |
| 4503 | break; |
| 4504 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4505 | pr_warn("%s: unknown check_state: %d sector: %llu\n", |
| 4506 | __func__, sh->check_state, |
| 4507 | (unsigned long long) sh->sector); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4508 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4509 | } |
| 4510 | } |
| 4511 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4512 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4513 | { |
| 4514 | int i; |
| 4515 | |
| 4516 | /* We have read all the blocks in this stripe and now we need to |
| 4517 | * copy some of them into a target stripe for expand. |
| 4518 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4519 | struct dma_async_tx_descriptor *tx = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4520 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4521 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4522 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 4523 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4524 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4525 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4526 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4527 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4528 | sector_t bn = raid5_compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4529 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 4530 | &dd_idx, NULL); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4531 | sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4532 | if (sh2 == NULL) |
| 4533 | /* so far only the early blocks of this stripe |
| 4534 | * have been requested. When later blocks |
| 4535 | * get requested, we will try again |
| 4536 | */ |
| 4537 | continue; |
| 4538 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 4539 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 4540 | /* must have already done this block */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4541 | raid5_release_stripe(sh2); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4542 | continue; |
| 4543 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4544 | |
| 4545 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4546 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4547 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 4548 | sh->dev[i].page, sh2->dev[dd_idx].offset, |
| 4549 | sh->dev[i].offset, RAID5_STRIPE_SIZE(conf), |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4550 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4551 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4552 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 4553 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 4554 | for (j = 0; j < conf->raid_disks; j++) |
| 4555 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4556 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4557 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 4558 | break; |
| 4559 | if (j == conf->raid_disks) { |
| 4560 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 4561 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 4562 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4563 | raid5_release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4564 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4565 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 4566 | /* done submitting copies, wait for them to complete */ |
NeilBrown | 749586b | 2012-11-20 14:11:15 +1100 | [diff] [blame] | 4567 | async_tx_quiesce(&tx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4568 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4569 | |
| 4570 | /* |
| 4571 | * handle_stripe - do things to a stripe. |
| 4572 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4573 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 4574 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4575 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4576 | * return some read requests which now have data |
| 4577 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4578 | * schedule a read on some buffers |
| 4579 | * schedule a write of some buffers |
| 4580 | * return confirmation of parity correctness |
| 4581 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4582 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4583 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4584 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4585 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4586 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4587 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4588 | struct r5dev *dev; |
| 4589 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4590 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4591 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4592 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4593 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4594 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head; |
| 4595 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4596 | s->failed_num[0] = -1; |
| 4597 | s->failed_num[1] = -1; |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 4598 | s->log_failed = r5l_log_disk_error(conf); |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4599 | |
| 4600 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4601 | rcu_read_lock(); |
| 4602 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4603 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4604 | sector_t first_bad; |
| 4605 | int bad_sectors; |
| 4606 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4607 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4608 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4609 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4610 | 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] | 4611 | i, dev->flags, |
| 4612 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4613 | /* maybe we can reply to a read |
| 4614 | * |
| 4615 | * new wantfill requests are only permitted while |
| 4616 | * ops_complete_biofill is guaranteed to be inactive |
| 4617 | */ |
| 4618 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 4619 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 4620 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4621 | |
| 4622 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4623 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 4624 | s->locked++; |
| 4625 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 4626 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4627 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4628 | s->compute++; |
| 4629 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4630 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4631 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4632 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4633 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4634 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4635 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4636 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4637 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4638 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4639 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4640 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4641 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4642 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4643 | /* Prefer to use the replacement for reads, but only |
| 4644 | * if it is recovered enough and has no bad blocks. |
| 4645 | */ |
| 4646 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 4647 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4648 | rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) && |
| 4649 | !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4650 | &first_bad, &bad_sectors)) |
| 4651 | set_bit(R5_ReadRepl, &dev->flags); |
| 4652 | else { |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4653 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4654 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4655 | else |
| 4656 | clear_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4657 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 4658 | clear_bit(R5_ReadRepl, &dev->flags); |
| 4659 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 4660 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 4661 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4662 | if (rdev) { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4663 | is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4664 | &first_bad, &bad_sectors); |
| 4665 | if (s->blocked_rdev == NULL |
| 4666 | && (test_bit(Blocked, &rdev->flags) |
| 4667 | || is_bad < 0)) { |
| 4668 | if (is_bad < 0) |
| 4669 | set_bit(BlockedBadBlocks, |
| 4670 | &rdev->flags); |
| 4671 | s->blocked_rdev = rdev; |
| 4672 | atomic_inc(&rdev->nr_pending); |
| 4673 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4674 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4675 | clear_bit(R5_Insync, &dev->flags); |
| 4676 | if (!rdev) |
| 4677 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4678 | else if (is_bad) { |
| 4679 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 4680 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 4681 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4682 | /* treat as in-sync, but with a read error |
| 4683 | * which we can now try to correct |
| 4684 | */ |
| 4685 | set_bit(R5_Insync, &dev->flags); |
| 4686 | set_bit(R5_ReadError, &dev->flags); |
| 4687 | } |
| 4688 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4689 | set_bit(R5_Insync, &dev->flags); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4690 | else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4691 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 4692 | set_bit(R5_Insync, &dev->flags); |
| 4693 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 4694 | test_bit(R5_Expanded, &dev->flags)) |
| 4695 | /* If we've reshaped into here, we assume it is Insync. |
| 4696 | * We will shortly update recovery_offset to make |
| 4697 | * it official. |
| 4698 | */ |
| 4699 | set_bit(R5_Insync, &dev->flags); |
| 4700 | |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4701 | if (test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4702 | /* This flag does not apply to '.replacement' |
| 4703 | * only to .rdev, so make sure to check that*/ |
| 4704 | struct md_rdev *rdev2 = rcu_dereference( |
| 4705 | conf->disks[i].rdev); |
| 4706 | if (rdev2 == rdev) |
| 4707 | clear_bit(R5_Insync, &dev->flags); |
| 4708 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4709 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4710 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4711 | } else |
| 4712 | clear_bit(R5_WriteError, &dev->flags); |
| 4713 | } |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4714 | if (test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4715 | /* This flag does not apply to '.replacement' |
| 4716 | * only to .rdev, so make sure to check that*/ |
| 4717 | struct md_rdev *rdev2 = rcu_dereference( |
| 4718 | conf->disks[i].rdev); |
| 4719 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4720 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4721 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4722 | } else |
| 4723 | clear_bit(R5_MadeGood, &dev->flags); |
| 4724 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4725 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4726 | struct md_rdev *rdev2 = rcu_dereference( |
| 4727 | conf->disks[i].replacement); |
| 4728 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 4729 | s->handle_bad_blocks = 1; |
| 4730 | atomic_inc(&rdev2->nr_pending); |
| 4731 | } else |
| 4732 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 4733 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4734 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4735 | /* The ReadError flag will just be confusing now */ |
| 4736 | clear_bit(R5_ReadError, &dev->flags); |
| 4737 | clear_bit(R5_ReWrite, &dev->flags); |
| 4738 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4739 | if (test_bit(R5_ReadError, &dev->flags)) |
| 4740 | clear_bit(R5_Insync, &dev->flags); |
| 4741 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4742 | if (s->failed < 2) |
| 4743 | s->failed_num[s->failed] = i; |
| 4744 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4745 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 4746 | do_recovery = 1; |
BingJing Chang | d63e2fc | 2018-08-01 17:08:36 +0800 | [diff] [blame] | 4747 | else if (!rdev) { |
| 4748 | rdev = rcu_dereference( |
| 4749 | conf->disks[i].replacement); |
| 4750 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 4751 | do_recovery = 1; |
| 4752 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4753 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4754 | |
| 4755 | if (test_bit(R5_InJournal, &dev->flags)) |
| 4756 | s->injournal++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4757 | if (test_bit(R5_InJournal, &dev->flags) && dev->written) |
| 4758 | s->just_cached++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4759 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4760 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 4761 | /* If there is a failed device being replaced, |
| 4762 | * we must be recovering. |
| 4763 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4764 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4765 | * else we can only be replacing |
| 4766 | * sync and recovery both need to read all devices, and so |
| 4767 | * use the same flag. |
| 4768 | */ |
| 4769 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4770 | sh->sector >= conf->mddev->recovery_cp || |
| 4771 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4772 | s->syncing = 1; |
| 4773 | else |
| 4774 | s->replacing = 1; |
| 4775 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4776 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4777 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4778 | |
Guoqing Jiang | cb9902d | 2020-06-16 11:25:51 +0200 | [diff] [blame] | 4779 | /* |
| 4780 | * Return '1' if this is a member of batch, or '0' if it is a lone stripe or |
| 4781 | * a head which can now be handled. |
| 4782 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4783 | static int clear_batch_ready(struct stripe_head *sh) |
| 4784 | { |
| 4785 | struct stripe_head *tmp; |
| 4786 | if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state)) |
NeilBrown | b15a9db | 2015-05-22 15:20:04 +1000 | [diff] [blame] | 4787 | return (sh->batch_head && sh->batch_head != sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4788 | spin_lock(&sh->stripe_lock); |
| 4789 | if (!sh->batch_head) { |
| 4790 | spin_unlock(&sh->stripe_lock); |
| 4791 | return 0; |
| 4792 | } |
| 4793 | |
| 4794 | /* |
| 4795 | * this stripe could be added to a batch list before we check |
| 4796 | * BATCH_READY, skips it |
| 4797 | */ |
| 4798 | if (sh->batch_head != sh) { |
| 4799 | spin_unlock(&sh->stripe_lock); |
| 4800 | return 1; |
| 4801 | } |
| 4802 | spin_lock(&sh->batch_lock); |
| 4803 | list_for_each_entry(tmp, &sh->batch_list, batch_list) |
| 4804 | clear_bit(STRIPE_BATCH_READY, &tmp->state); |
| 4805 | spin_unlock(&sh->batch_lock); |
| 4806 | spin_unlock(&sh->stripe_lock); |
| 4807 | |
| 4808 | /* |
| 4809 | * BATCH_READY is cleared, no new stripes can be added. |
| 4810 | * batch_list can be accessed without lock |
| 4811 | */ |
| 4812 | return 0; |
| 4813 | } |
| 4814 | |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4815 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 4816 | unsigned long handle_flags) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4817 | { |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4818 | struct stripe_head *sh, *next; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4819 | int i; |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4820 | int do_wakeup = 0; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4821 | |
NeilBrown | bb27051 | 2015-05-08 18:19:40 +1000 | [diff] [blame] | 4822 | list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) { |
| 4823 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4824 | list_del_init(&sh->batch_list); |
| 4825 | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4826 | WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4827 | (1 << STRIPE_SYNCING) | |
| 4828 | (1 << STRIPE_REPLACED) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4829 | (1 << STRIPE_DELAYED) | |
| 4830 | (1 << STRIPE_BIT_DELAY) | |
| 4831 | (1 << STRIPE_FULL_WRITE) | |
| 4832 | (1 << STRIPE_BIOFILL_RUN) | |
| 4833 | (1 << STRIPE_COMPUTE_RUN) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4834 | (1 << STRIPE_DISCARD) | |
| 4835 | (1 << STRIPE_BATCH_READY) | |
| 4836 | (1 << STRIPE_BATCH_ERR) | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4837 | (1 << STRIPE_BITMAP_PENDING)), |
| 4838 | "stripe state: %lx\n", sh->state); |
| 4839 | WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) | |
| 4840 | (1 << STRIPE_REPLACED)), |
| 4841 | "head stripe state: %lx\n", head_sh->state); |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4842 | |
| 4843 | set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS | |
NeilBrown | 550da24 | 2016-03-09 12:58:25 +1100 | [diff] [blame] | 4844 | (1 << STRIPE_PREREAD_ACTIVE) | |
Dennis Yang | 184a09e | 2017-09-06 11:02:35 +0800 | [diff] [blame] | 4845 | (1 << STRIPE_DEGRADED) | |
| 4846 | (1 << STRIPE_ON_UNPLUG_LIST)), |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4847 | head_sh->state & (1 << STRIPE_INSYNC)); |
| 4848 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4849 | sh->check_state = head_sh->check_state; |
| 4850 | sh->reconstruct_state = head_sh->reconstruct_state; |
Amy Chiang | 448ec63 | 2018-05-16 18:59:35 +0800 | [diff] [blame] | 4851 | spin_lock_irq(&sh->stripe_lock); |
| 4852 | sh->batch_head = NULL; |
| 4853 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4854 | for (i = 0; i < sh->disks; i++) { |
| 4855 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 4856 | do_wakeup = 1; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4857 | sh->dev[i].flags = head_sh->dev[i].flags & |
| 4858 | (~((1 << R5_WriteError) | (1 << R5_Overlap))); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4859 | } |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4860 | if (handle_flags == 0 || |
| 4861 | sh->state & handle_flags) |
| 4862 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4863 | raid5_release_stripe(sh); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4864 | } |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4865 | spin_lock_irq(&head_sh->stripe_lock); |
| 4866 | head_sh->batch_head = NULL; |
| 4867 | spin_unlock_irq(&head_sh->stripe_lock); |
| 4868 | for (i = 0; i < head_sh->disks; i++) |
| 4869 | if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags)) |
| 4870 | do_wakeup = 1; |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4871 | if (head_sh->state & handle_flags) |
| 4872 | set_bit(STRIPE_HANDLE, &head_sh->state); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4873 | |
| 4874 | if (do_wakeup) |
| 4875 | wake_up(&head_sh->raid_conf->wait_for_overlap); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4876 | } |
| 4877 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4878 | static void handle_stripe(struct stripe_head *sh) |
| 4879 | { |
| 4880 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4881 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4882 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4883 | int prexor; |
| 4884 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4885 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4886 | |
| 4887 | clear_bit(STRIPE_HANDLE, &sh->state); |
Guoqing Jiang | a377a47 | 2020-06-16 11:25:50 +0200 | [diff] [blame] | 4888 | |
| 4889 | /* |
| 4890 | * handle_stripe should not continue handle the batched stripe, only |
| 4891 | * the head of batch list or lone stripe can continue. Otherwise we |
| 4892 | * could see break_stripe_batch_list warns about the STRIPE_ACTIVE |
| 4893 | * is set for the batched stripe. |
| 4894 | */ |
| 4895 | if (clear_batch_ready(sh)) |
| 4896 | return; |
| 4897 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 4898 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4899 | /* already being handled, ensure it gets handled |
| 4900 | * again when current action finishes */ |
| 4901 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4902 | return; |
| 4903 | } |
| 4904 | |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4905 | if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state)) |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4906 | break_stripe_batch_list(sh, 0); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4907 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4908 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) { |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4909 | spin_lock(&sh->stripe_lock); |
Song Liu | 5ddf044 | 2017-05-11 17:03:44 -0700 | [diff] [blame] | 4910 | /* |
| 4911 | * Cannot process 'sync' concurrently with 'discard'. |
| 4912 | * Flush data in r5cache before 'sync'. |
| 4913 | */ |
| 4914 | if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) && |
| 4915 | !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) && |
| 4916 | !test_bit(STRIPE_DISCARD, &sh->state) && |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4917 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 4918 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4919 | clear_bit(STRIPE_INSYNC, &sh->state); |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4920 | clear_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4921 | } |
| 4922 | spin_unlock(&sh->stripe_lock); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4923 | } |
| 4924 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4925 | |
| 4926 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 4927 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 4928 | (unsigned long long)sh->sector, sh->state, |
| 4929 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 4930 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4931 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4932 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4933 | |
Shaohua Li | b70abcb | 2015-08-13 14:31:58 -0700 | [diff] [blame] | 4934 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) |
| 4935 | goto finish; |
| 4936 | |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 4937 | if (s.handle_bad_blocks || |
| 4938 | test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4939 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4940 | goto finish; |
| 4941 | } |
| 4942 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4943 | if (unlikely(s.blocked_rdev)) { |
| 4944 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4945 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4946 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4947 | goto finish; |
| 4948 | } |
| 4949 | /* There is nothing for the blocked_rdev to block */ |
| 4950 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 4951 | s.blocked_rdev = NULL; |
| 4952 | } |
| 4953 | |
| 4954 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 4955 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 4956 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 4957 | } |
| 4958 | |
| 4959 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 4960 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 4961 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 4962 | s.failed_num[0], s.failed_num[1]); |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 4963 | /* |
| 4964 | * check if the array has lost more than max_degraded devices and, |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4965 | * if so, some requests might need to be failed. |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 4966 | * |
| 4967 | * When journal device failed (log_failed), we will only process |
| 4968 | * the stripe if there is data need write to raid disks |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4969 | */ |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 4970 | if (s.failed > conf->max_degraded || |
| 4971 | (s.log_failed && s.injournal == 0)) { |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4972 | sh->check_state = 0; |
| 4973 | sh->reconstruct_state = 0; |
NeilBrown | 626f209 | 2015-05-22 14:03:10 +1000 | [diff] [blame] | 4974 | break_stripe_batch_list(sh, 0); |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4975 | if (s.to_read+s.to_write+s.written) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 4976 | handle_failed_stripe(conf, sh, &s, disks); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4977 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4978 | handle_failed_sync(conf, sh, &s); |
| 4979 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4980 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4981 | /* Now we check to see if any write operations have recently |
| 4982 | * completed |
| 4983 | */ |
| 4984 | prexor = 0; |
| 4985 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 4986 | prexor = 1; |
| 4987 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 4988 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 4989 | sh->reconstruct_state = reconstruct_state_idle; |
| 4990 | |
| 4991 | /* All the 'written' buffers and the parity block are ready to |
| 4992 | * be written back to disk |
| 4993 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4994 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 4995 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4996 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4997 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 4998 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4999 | for (i = disks; i--; ) { |
| 5000 | struct r5dev *dev = &sh->dev[i]; |
| 5001 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 5002 | (i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 5003 | dev->written || test_bit(R5_InJournal, |
| 5004 | &dev->flags))) { |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5005 | pr_debug("Writing block %d\n", i); |
| 5006 | set_bit(R5_Wantwrite, &dev->flags); |
| 5007 | if (prexor) |
| 5008 | continue; |
NeilBrown | 9c4bdf6 | 2014-08-13 09:57:07 +1000 | [diff] [blame] | 5009 | if (s.failed > 1) |
| 5010 | continue; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5011 | if (!test_bit(R5_Insync, &dev->flags) || |
| 5012 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 5013 | s.failed == 0)) |
| 5014 | set_bit(STRIPE_INSYNC, &sh->state); |
| 5015 | } |
| 5016 | } |
| 5017 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5018 | s.dec_preread_active = 1; |
| 5019 | } |
| 5020 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 5021 | /* |
| 5022 | * might be able to return some write requests if the parity blocks |
| 5023 | * are safe, or on a failed drive |
| 5024 | */ |
| 5025 | pdev = &sh->dev[sh->pd_idx]; |
| 5026 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 5027 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 5028 | qdev = &sh->dev[sh->qd_idx]; |
| 5029 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 5030 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 5031 | || conf->level < 6; |
| 5032 | |
| 5033 | if (s.written && |
| 5034 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 5035 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 5036 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 5037 | test_bit(R5_Discard, &pdev->flags))))) && |
| 5038 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 5039 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 5040 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 5041 | test_bit(R5_Discard, &qdev->flags)))))) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5042 | handle_stripe_clean_event(conf, sh, disks); |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 5043 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 5044 | if (s.just_cached) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5045 | r5c_handle_cached_data_endio(conf, sh, disks); |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 5046 | log_stripe_write_finished(sh); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 5047 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 5048 | /* Now we might consider reading some blocks, either to check/generate |
| 5049 | * parity, or to satisfy requests |
| 5050 | * or to load a block that is being partially written. |
| 5051 | */ |
| 5052 | if (s.to_read || s.non_overwrite |
ChangSyun Peng | a1c6ae3 | 2020-07-31 17:50:17 +0800 | [diff] [blame] | 5053 | || (s.to_write && s.failed) |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 5054 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 5055 | || s.replacing |
| 5056 | || s.expanding) |
| 5057 | handle_stripe_fill(sh, &s, disks); |
| 5058 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5059 | /* |
| 5060 | * When the stripe finishes full journal write cycle (write to journal |
| 5061 | * and raid disk), this is the clean up procedure so it is ready for |
| 5062 | * next operation. |
| 5063 | */ |
| 5064 | r5c_finish_stripe_write_out(conf, sh, &s); |
| 5065 | |
| 5066 | /* |
| 5067 | * Now to consider new write requests, cache write back and what else, |
| 5068 | * if anything should be read. We do not handle new writes when: |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5069 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 5070 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 5071 | * block. |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5072 | * 3/ A r5c cache log write is in flight. |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5073 | */ |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5074 | |
| 5075 | if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) { |
| 5076 | if (!r5c_is_writeback(conf->log)) { |
| 5077 | if (s.to_write) |
| 5078 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 5079 | } else { /* write back cache */ |
| 5080 | int ret = 0; |
| 5081 | |
| 5082 | /* First, try handle writes in caching phase */ |
| 5083 | if (s.to_write) |
| 5084 | ret = r5c_try_caching_write(conf, sh, &s, |
| 5085 | disks); |
| 5086 | /* |
| 5087 | * If caching phase failed: ret == -EAGAIN |
| 5088 | * OR |
| 5089 | * stripe under reclaim: !caching && injournal |
| 5090 | * |
| 5091 | * fall back to handle_stripe_dirtying() |
| 5092 | */ |
| 5093 | if (ret == -EAGAIN || |
| 5094 | /* stripe under reclaim: !caching && injournal */ |
| 5095 | (!test_bit(STRIPE_R5C_CACHING, &sh->state) && |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 5096 | s.injournal > 0)) { |
| 5097 | ret = handle_stripe_dirtying(conf, sh, &s, |
| 5098 | disks); |
| 5099 | if (ret == -EAGAIN) |
| 5100 | goto finish; |
| 5101 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5102 | } |
| 5103 | } |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5104 | |
| 5105 | /* maybe we need to check and possibly fix the parity for this stripe |
| 5106 | * Any reads will already have been scheduled, so we just see if enough |
| 5107 | * data is available. The parity check is held off while parity |
| 5108 | * dependent operations are in flight. |
| 5109 | */ |
| 5110 | if (sh->check_state || |
| 5111 | (s.syncing && s.locked == 0 && |
| 5112 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 5113 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 5114 | if (conf->level == 6) |
| 5115 | handle_parity_checks6(conf, sh, &s, disks); |
| 5116 | else |
| 5117 | handle_parity_checks5(conf, sh, &s, disks); |
| 5118 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5119 | |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 5120 | if ((s.replacing || s.syncing) && s.locked == 0 |
| 5121 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) |
| 5122 | && !test_bit(STRIPE_REPLACED, &sh->state)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5123 | /* Write out to replacement devices where possible */ |
| 5124 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 5125 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 5126 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5127 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 5128 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 5129 | s.locked++; |
| 5130 | } |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 5131 | if (s.replacing) |
| 5132 | set_bit(STRIPE_INSYNC, &sh->state); |
| 5133 | set_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5134 | } |
| 5135 | if ((s.syncing || s.replacing) && s.locked == 0 && |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 5136 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5137 | test_bit(STRIPE_INSYNC, &sh->state)) { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5138 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5139 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5140 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 5141 | wake_up(&conf->wait_for_overlap); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5142 | } |
| 5143 | |
| 5144 | /* If the failed drives are just a ReadError, then we might need |
| 5145 | * to progress the repair/check process |
| 5146 | */ |
| 5147 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 5148 | for (i = 0; i < s.failed; i++) { |
| 5149 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 5150 | if (test_bit(R5_ReadError, &dev->flags) |
| 5151 | && !test_bit(R5_LOCKED, &dev->flags) |
| 5152 | && test_bit(R5_UPTODATE, &dev->flags) |
| 5153 | ) { |
| 5154 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 5155 | set_bit(R5_Wantwrite, &dev->flags); |
| 5156 | set_bit(R5_ReWrite, &dev->flags); |
Guoqing Jiang | 3a31cf3 | 2020-07-28 12:01:43 +0200 | [diff] [blame] | 5157 | } else |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5158 | /* let's read it back */ |
| 5159 | set_bit(R5_Wantread, &dev->flags); |
Guoqing Jiang | 3a31cf3 | 2020-07-28 12:01:43 +0200 | [diff] [blame] | 5160 | set_bit(R5_LOCKED, &dev->flags); |
| 5161 | s.locked++; |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5162 | } |
| 5163 | } |
| 5164 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5165 | /* Finish reconstruct operations initiated by the expansion process */ |
| 5166 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 5167 | struct stripe_head *sh_src |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5168 | = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5169 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 5170 | /* sh cannot be written until sh_src has been read. |
| 5171 | * so arrange for sh to be delayed a little |
| 5172 | */ |
| 5173 | set_bit(STRIPE_DELAYED, &sh->state); |
| 5174 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5175 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 5176 | &sh_src->state)) |
| 5177 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5178 | raid5_release_stripe(sh_src); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5179 | goto finish; |
| 5180 | } |
| 5181 | if (sh_src) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5182 | raid5_release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5183 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5184 | sh->reconstruct_state = reconstruct_state_idle; |
| 5185 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 5186 | for (i = conf->raid_disks; i--; ) { |
| 5187 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 5188 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 5189 | s.locked++; |
| 5190 | } |
| 5191 | } |
| 5192 | |
| 5193 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 5194 | !sh->reconstruct_state) { |
| 5195 | /* Need to write out all blocks after computing parity */ |
| 5196 | sh->disks = conf->raid_disks; |
| 5197 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 5198 | schedule_reconstruction(sh, &s, 1, 1); |
| 5199 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 5200 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 5201 | atomic_dec(&conf->reshape_stripes); |
| 5202 | wake_up(&conf->wait_for_overlap); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5203 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5204 | } |
| 5205 | |
| 5206 | if (s.expanding && s.locked == 0 && |
| 5207 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 5208 | handle_stripe_expansion(conf, sh); |
| 5209 | |
| 5210 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5211 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 5212 | if (unlikely(s.blocked_rdev)) { |
| 5213 | if (conf->mddev->external) |
| 5214 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 5215 | conf->mddev); |
| 5216 | else |
| 5217 | /* Internal metadata will immediately |
| 5218 | * be written by raid5d, so we don't |
| 5219 | * need to wait here. |
| 5220 | */ |
| 5221 | rdev_dec_pending(s.blocked_rdev, |
| 5222 | conf->mddev); |
| 5223 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5224 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5225 | if (s.handle_bad_blocks) |
| 5226 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5227 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5228 | struct r5dev *dev = &sh->dev[i]; |
| 5229 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 5230 | /* We own a safe reference to the rdev */ |
| 5231 | rdev = conf->disks[i].rdev; |
| 5232 | if (!rdev_set_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5233 | RAID5_STRIPE_SECTORS(conf), 0)) |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5234 | md_error(conf->mddev, rdev); |
| 5235 | rdev_dec_pending(rdev, conf->mddev); |
| 5236 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 5237 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 5238 | rdev = conf->disks[i].rdev; |
| 5239 | rdev_clear_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5240 | RAID5_STRIPE_SECTORS(conf), 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 5241 | rdev_dec_pending(rdev, conf->mddev); |
| 5242 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5243 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 5244 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5245 | if (!rdev) |
| 5246 | /* rdev have been moved down */ |
| 5247 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5248 | rdev_clear_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5249 | RAID5_STRIPE_SECTORS(conf), 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5250 | rdev_dec_pending(rdev, conf->mddev); |
| 5251 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5252 | } |
| 5253 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 5254 | if (s.ops_request) |
| 5255 | raid_run_ops(sh, s.ops_request); |
| 5256 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 5257 | ops_run_io(sh, &s); |
| 5258 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 5259 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5260 | /* 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] | 5261 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5262 | * have actually been submitted. |
| 5263 | */ |
| 5264 | atomic_dec(&conf->preread_active_stripes); |
| 5265 | if (atomic_read(&conf->preread_active_stripes) < |
| 5266 | IO_THRESHOLD) |
| 5267 | md_wakeup_thread(conf->mddev->thread); |
| 5268 | } |
| 5269 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 5270 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5271 | } |
| 5272 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5273 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5274 | { |
| 5275 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 5276 | while (!list_empty(&conf->delayed_list)) { |
| 5277 | struct list_head *l = conf->delayed_list.next; |
| 5278 | struct stripe_head *sh; |
| 5279 | sh = list_entry(l, struct stripe_head, lru); |
| 5280 | list_del_init(l); |
| 5281 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 5282 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5283 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5284 | list_add_tail(&sh->lru, &conf->hold_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5285 | raid5_wakeup_stripe_thread(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 5287 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5288 | } |
| 5289 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5290 | static void activate_bit_delay(struct r5conf *conf, |
| 5291 | struct list_head *temp_inactive_list) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5292 | { |
| 5293 | /* device_lock is held */ |
| 5294 | struct list_head head; |
| 5295 | list_add(&head, &conf->bitmap_list); |
| 5296 | list_del_init(&conf->bitmap_list); |
| 5297 | while (!list_empty(&head)) { |
| 5298 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5299 | int hash; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5300 | list_del_init(&sh->lru); |
| 5301 | atomic_inc(&sh->count); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5302 | hash = sh->hash_lock_index; |
| 5303 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5304 | } |
| 5305 | } |
| 5306 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5307 | 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] | 5308 | { |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5309 | struct r5conf *conf = mddev->private; |
Christoph Hellwig | 10433d0 | 2017-08-23 19:10:28 +0200 | [diff] [blame] | 5310 | sector_t sector = bio->bi_iter.bi_sector; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5311 | unsigned int chunk_sectors; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5312 | unsigned int bio_sectors = bio_sectors(bio); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5313 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5314 | chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5315 | return chunk_sectors >= |
| 5316 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 5317 | } |
| 5318 | |
| 5319 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5320 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 5321 | * later sampled by raid5d. |
| 5322 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5323 | 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] | 5324 | { |
| 5325 | unsigned long flags; |
| 5326 | |
| 5327 | spin_lock_irqsave(&conf->device_lock, flags); |
| 5328 | |
| 5329 | bi->bi_next = conf->retry_read_aligned_list; |
| 5330 | conf->retry_read_aligned_list = bi; |
| 5331 | |
| 5332 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5333 | md_wakeup_thread(conf->mddev->thread); |
| 5334 | } |
| 5335 | |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5336 | static struct bio *remove_bio_from_retry(struct r5conf *conf, |
| 5337 | unsigned int *offset) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5338 | { |
| 5339 | struct bio *bi; |
| 5340 | |
| 5341 | bi = conf->retry_read_aligned; |
| 5342 | if (bi) { |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5343 | *offset = conf->retry_read_offset; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5344 | conf->retry_read_aligned = NULL; |
| 5345 | return bi; |
| 5346 | } |
| 5347 | bi = conf->retry_read_aligned_list; |
| 5348 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5349 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5350 | bi->bi_next = NULL; |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5351 | *offset = 0; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5352 | } |
| 5353 | |
| 5354 | return bi; |
| 5355 | } |
| 5356 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5357 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5358 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 5359 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 5360 | * first). |
| 5361 | * If the read failed.. |
| 5362 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5363 | static void raid5_align_endio(struct bio *bi) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5364 | { |
Guoqing Jiang | 1147f58 | 2021-05-25 17:46:19 +0800 | [diff] [blame] | 5365 | struct md_io_acct *md_io_acct = bi->bi_private; |
| 5366 | struct bio *raid_bi = md_io_acct->orig_bio; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5367 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5368 | struct r5conf *conf; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5369 | struct md_rdev *rdev; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 5370 | blk_status_t error = bi->bi_status; |
Guoqing Jiang | 1147f58 | 2021-05-25 17:46:19 +0800 | [diff] [blame] | 5371 | unsigned long start_time = md_io_acct->start_time; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5372 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5373 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5374 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5375 | rdev = (void*)raid_bi->bi_next; |
| 5376 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 5377 | mddev = rdev->mddev; |
| 5378 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5379 | |
| 5380 | rdev_dec_pending(rdev, conf->mddev); |
| 5381 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 5382 | if (!error) { |
Guoqing Jiang | 1147f58 | 2021-05-25 17:46:19 +0800 | [diff] [blame] | 5383 | if (blk_queue_io_stat(raid_bi->bi_bdev->bd_disk->queue)) |
| 5384 | bio_end_io_acct(raid_bi, start_time); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5385 | bio_endio(raid_bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5386 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 5387 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 5388 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5389 | } |
| 5390 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5391 | 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] | 5392 | |
| 5393 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5394 | } |
| 5395 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5396 | 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] | 5397 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5398 | struct r5conf *conf = mddev->private; |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5399 | struct bio *align_bio; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5400 | struct md_rdev *rdev; |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5401 | sector_t sector, end_sector, first_bad; |
| 5402 | int bad_sectors, dd_idx; |
Guoqing Jiang | 1147f58 | 2021-05-25 17:46:19 +0800 | [diff] [blame] | 5403 | struct md_io_acct *md_io_acct; |
Gal Ofri | 97ae272 | 2021-06-07 14:07:03 +0300 | [diff] [blame] | 5404 | bool did_inc; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5405 | |
| 5406 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5407 | pr_debug("%s: non aligned\n", __func__); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5408 | return 0; |
| 5409 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5410 | |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5411 | sector = raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, 0, |
| 5412 | &dd_idx, NULL); |
| 5413 | end_sector = bio_end_sector(raid_bio); |
| 5414 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5415 | rcu_read_lock(); |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5416 | if (r5c_big_stripe_cached(conf, sector)) |
| 5417 | goto out_rcu_unlock; |
| 5418 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5419 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 5420 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 5421 | rdev->recovery_offset < end_sector) { |
| 5422 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5423 | if (!rdev) |
| 5424 | goto out_rcu_unlock; |
| 5425 | if (test_bit(Faulty, &rdev->flags) || |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5426 | !(test_bit(In_sync, &rdev->flags) || |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5427 | rdev->recovery_offset >= end_sector)) |
| 5428 | goto out_rcu_unlock; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5429 | } |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 5430 | |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5431 | atomic_inc(&rdev->nr_pending); |
| 5432 | rcu_read_unlock(); |
| 5433 | |
Guoqing Jiang | c82aa1b | 2021-05-25 17:46:18 +0800 | [diff] [blame] | 5434 | if (is_badblock(rdev, sector, bio_sectors(raid_bio), &first_bad, |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5435 | &bad_sectors)) { |
Guoqing Jiang | c82aa1b | 2021-05-25 17:46:18 +0800 | [diff] [blame] | 5436 | bio_put(raid_bio); |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5437 | rdev_dec_pending(rdev, mddev); |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 5438 | return 0; |
| 5439 | } |
| 5440 | |
Guoqing Jiang | 1147f58 | 2021-05-25 17:46:19 +0800 | [diff] [blame] | 5441 | align_bio = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->io_acct_set); |
| 5442 | md_io_acct = container_of(align_bio, struct md_io_acct, bio_clone); |
| 5443 | raid_bio->bi_next = (void *)rdev; |
| 5444 | if (blk_queue_io_stat(raid_bio->bi_bdev->bd_disk->queue)) |
| 5445 | md_io_acct->start_time = bio_start_io_acct(raid_bio); |
| 5446 | md_io_acct->orig_bio = raid_bio; |
| 5447 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5448 | bio_set_dev(align_bio, rdev->bdev); |
| 5449 | align_bio->bi_end_io = raid5_align_endio; |
Guoqing Jiang | 1147f58 | 2021-05-25 17:46:19 +0800 | [diff] [blame] | 5450 | align_bio->bi_private = md_io_acct; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5451 | align_bio->bi_iter.bi_sector = sector; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5452 | |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5453 | /* No reshape active, so we can trust rdev->data_offset */ |
| 5454 | align_bio->bi_iter.bi_sector += rdev->data_offset; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5455 | |
Gal Ofri | 97ae272 | 2021-06-07 14:07:03 +0300 | [diff] [blame] | 5456 | did_inc = false; |
| 5457 | if (conf->quiesce == 0) { |
| 5458 | atomic_inc(&conf->active_aligned_reads); |
| 5459 | did_inc = true; |
| 5460 | } |
| 5461 | /* need a memory barrier to detect the race with raid5_quiesce() */ |
| 5462 | if (!did_inc || smp_load_acquire(&conf->quiesce) != 0) { |
| 5463 | /* quiesce is in progress, so we need to undo io activation and wait |
| 5464 | * for it to finish |
| 5465 | */ |
| 5466 | if (did_inc && atomic_dec_and_test(&conf->active_aligned_reads)) |
| 5467 | wake_up(&conf->wait_for_quiescent); |
| 5468 | spin_lock_irq(&conf->device_lock); |
| 5469 | wait_event_lock_irq(conf->wait_for_quiescent, conf->quiesce == 0, |
| 5470 | conf->device_lock); |
| 5471 | atomic_inc(&conf->active_aligned_reads); |
| 5472 | spin_unlock_irq(&conf->device_lock); |
| 5473 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5474 | |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5475 | if (mddev->gendisk) |
| 5476 | trace_block_bio_remap(align_bio, disk_devt(mddev->gendisk), |
| 5477 | raid_bio->bi_iter.bi_sector); |
| 5478 | submit_bio_noacct(align_bio); |
| 5479 | return 1; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5480 | |
Christoph Hellwig | e82ed3a | 2021-01-26 15:52:44 +0100 | [diff] [blame] | 5481 | out_rcu_unlock: |
| 5482 | rcu_read_unlock(); |
| 5483 | return 0; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5484 | } |
| 5485 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5486 | static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio) |
| 5487 | { |
| 5488 | struct bio *split; |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5489 | sector_t sector = raid_bio->bi_iter.bi_sector; |
| 5490 | unsigned chunk_sects = mddev->chunk_sectors; |
| 5491 | unsigned sectors = chunk_sects - (sector & (chunk_sects-1)); |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5492 | |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5493 | if (sectors < bio_sectors(raid_bio)) { |
| 5494 | struct r5conf *conf = mddev->private; |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5495 | split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split); |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5496 | bio_chain(split, raid_bio); |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 5497 | submit_bio_noacct(raid_bio); |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5498 | raid_bio = split; |
| 5499 | } |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5500 | |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5501 | if (!raid5_read_one_chunk(mddev, raid_bio)) |
| 5502 | return raid_bio; |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5503 | |
| 5504 | return NULL; |
| 5505 | } |
| 5506 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5507 | /* __get_priority_stripe - get the next stripe to process |
| 5508 | * |
| 5509 | * Full stripe writes are allowed to pass preread active stripes up until |
| 5510 | * the bypass_threshold is exceeded. In general the bypass_count |
| 5511 | * increments when the handle_list is handled before the hold_list; however, it |
| 5512 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 5513 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 5514 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 5515 | * handle_list. |
| 5516 | */ |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5517 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5518 | { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5519 | struct stripe_head *sh, *tmp; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5520 | struct list_head *handle_list = NULL; |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5521 | struct r5worker_group *wg; |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 5522 | bool second_try = !r5c_is_writeback(conf->log) && |
| 5523 | !r5l_log_disk_error(conf); |
| 5524 | bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) || |
| 5525 | r5l_log_disk_error(conf); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5526 | |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5527 | again: |
| 5528 | wg = NULL; |
| 5529 | sh = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5530 | if (conf->worker_cnt_per_group == 0) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5531 | handle_list = try_loprio ? &conf->loprio_list : |
| 5532 | &conf->handle_list; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5533 | } else if (group != ANY_GROUP) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5534 | handle_list = try_loprio ? &conf->worker_groups[group].loprio_list : |
| 5535 | &conf->worker_groups[group].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5536 | wg = &conf->worker_groups[group]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5537 | } else { |
| 5538 | int i; |
| 5539 | for (i = 0; i < conf->group_cnt; i++) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5540 | handle_list = try_loprio ? &conf->worker_groups[i].loprio_list : |
| 5541 | &conf->worker_groups[i].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5542 | wg = &conf->worker_groups[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5543 | if (!list_empty(handle_list)) |
| 5544 | break; |
| 5545 | } |
| 5546 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5547 | |
| 5548 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 5549 | __func__, |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5550 | list_empty(handle_list) ? "empty" : "busy", |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5551 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 5552 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 5553 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5554 | if (!list_empty(handle_list)) { |
| 5555 | sh = list_entry(handle_list->next, typeof(*sh), lru); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5556 | |
| 5557 | if (list_empty(&conf->hold_list)) |
| 5558 | conf->bypass_count = 0; |
| 5559 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 5560 | if (conf->hold_list.next == conf->last_hold) |
| 5561 | conf->bypass_count++; |
| 5562 | else { |
| 5563 | conf->last_hold = conf->hold_list.next; |
| 5564 | conf->bypass_count -= conf->bypass_threshold; |
| 5565 | if (conf->bypass_count < 0) |
| 5566 | conf->bypass_count = 0; |
| 5567 | } |
| 5568 | } |
| 5569 | } else if (!list_empty(&conf->hold_list) && |
| 5570 | ((conf->bypass_threshold && |
| 5571 | conf->bypass_count > conf->bypass_threshold) || |
| 5572 | atomic_read(&conf->pending_full_writes) == 0)) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5573 | |
| 5574 | list_for_each_entry(tmp, &conf->hold_list, lru) { |
| 5575 | if (conf->worker_cnt_per_group == 0 || |
| 5576 | group == ANY_GROUP || |
| 5577 | !cpu_online(tmp->cpu) || |
| 5578 | cpu_to_group(tmp->cpu) == group) { |
| 5579 | sh = tmp; |
| 5580 | break; |
| 5581 | } |
| 5582 | } |
| 5583 | |
| 5584 | if (sh) { |
| 5585 | conf->bypass_count -= conf->bypass_threshold; |
| 5586 | if (conf->bypass_count < 0) |
| 5587 | conf->bypass_count = 0; |
| 5588 | } |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5589 | wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5590 | } |
| 5591 | |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5592 | if (!sh) { |
| 5593 | if (second_try) |
| 5594 | return NULL; |
| 5595 | second_try = true; |
| 5596 | try_loprio = !try_loprio; |
| 5597 | goto again; |
| 5598 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5599 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5600 | if (wg) { |
| 5601 | wg->stripes_cnt--; |
| 5602 | sh->group = NULL; |
| 5603 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5604 | list_del_init(&sh->lru); |
Shaohua Li | c7a6d35 | 2014-04-15 09:12:54 +0800 | [diff] [blame] | 5605 | BUG_ON(atomic_inc_return(&sh->count) != 1); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5606 | return sh; |
| 5607 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5608 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5609 | struct raid5_plug_cb { |
| 5610 | struct blk_plug_cb cb; |
| 5611 | struct list_head list; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5612 | struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5613 | }; |
| 5614 | |
| 5615 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 5616 | { |
| 5617 | struct raid5_plug_cb *cb = container_of( |
| 5618 | blk_cb, struct raid5_plug_cb, cb); |
| 5619 | struct stripe_head *sh; |
| 5620 | struct mddev *mddev = cb->cb.data; |
| 5621 | struct r5conf *conf = mddev->private; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5622 | int cnt = 0; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5623 | int hash; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5624 | |
| 5625 | if (cb->list.next && !list_empty(&cb->list)) { |
| 5626 | spin_lock_irq(&conf->device_lock); |
| 5627 | while (!list_empty(&cb->list)) { |
| 5628 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 5629 | list_del_init(&sh->lru); |
| 5630 | /* |
| 5631 | * avoid race release_stripe_plug() sees |
| 5632 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 5633 | * is still in our list |
| 5634 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 5635 | smp_mb__before_atomic(); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5636 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5637 | /* |
| 5638 | * STRIPE_ON_RELEASE_LIST could be set here. In that |
| 5639 | * case, the count is always > 1 here |
| 5640 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5641 | hash = sh->hash_lock_index; |
| 5642 | __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5643 | cnt++; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5644 | } |
| 5645 | spin_unlock_irq(&conf->device_lock); |
| 5646 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5647 | release_inactive_stripe_list(conf, cb->temp_inactive_list, |
| 5648 | NR_STRIPE_HASH_LOCKS); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5649 | if (mddev->queue) |
| 5650 | trace_block_unplug(mddev->queue, cnt, !from_schedule); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5651 | kfree(cb); |
| 5652 | } |
| 5653 | |
| 5654 | static void release_stripe_plug(struct mddev *mddev, |
| 5655 | struct stripe_head *sh) |
| 5656 | { |
| 5657 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 5658 | raid5_unplug, mddev, |
| 5659 | sizeof(struct raid5_plug_cb)); |
| 5660 | struct raid5_plug_cb *cb; |
| 5661 | |
| 5662 | if (!blk_cb) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5663 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5664 | return; |
| 5665 | } |
| 5666 | |
| 5667 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 5668 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5669 | if (cb->list.next == NULL) { |
| 5670 | int i; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5671 | INIT_LIST_HEAD(&cb->list); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5672 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5673 | INIT_LIST_HEAD(cb->temp_inactive_list + i); |
| 5674 | } |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5675 | |
| 5676 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 5677 | list_add_tail(&sh->lru, &cb->list); |
| 5678 | else |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5679 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5680 | } |
| 5681 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5682 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 5683 | { |
| 5684 | struct r5conf *conf = mddev->private; |
| 5685 | sector_t logical_sector, last_sector; |
| 5686 | struct stripe_head *sh; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5687 | int stripe_sectors; |
| 5688 | |
Vishal Verma | bf2c411 | 2021-12-21 20:06:22 +0000 | [diff] [blame] | 5689 | /* We need to handle this when io_uring supports discard/trim */ |
| 5690 | if (WARN_ON_ONCE(bi->bi_opf & REQ_NOWAIT)) |
| 5691 | return; |
| 5692 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5693 | if (mddev->reshape_position != MaxSector) |
| 5694 | /* Skip discard while reshape is happening */ |
| 5695 | return; |
| 5696 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5697 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); |
Guoqing Jiang | b0f01ec | 2019-09-03 11:41:03 +0200 | [diff] [blame] | 5698 | last_sector = bio_end_sector(bi); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5699 | |
| 5700 | bi->bi_next = NULL; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5701 | |
| 5702 | stripe_sectors = conf->chunk_sectors * |
| 5703 | (conf->raid_disks - conf->max_degraded); |
| 5704 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 5705 | stripe_sectors); |
| 5706 | sector_div(last_sector, stripe_sectors); |
| 5707 | |
| 5708 | logical_sector *= conf->chunk_sectors; |
| 5709 | last_sector *= conf->chunk_sectors; |
| 5710 | |
| 5711 | for (; logical_sector < last_sector; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5712 | logical_sector += RAID5_STRIPE_SECTORS(conf)) { |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5713 | DEFINE_WAIT(w); |
| 5714 | int d; |
| 5715 | again: |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5716 | sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5717 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5718 | TASK_UNINTERRUPTIBLE); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5719 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
| 5720 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5721 | raid5_release_stripe(sh); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5722 | schedule(); |
| 5723 | goto again; |
| 5724 | } |
| 5725 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5726 | spin_lock_irq(&sh->stripe_lock); |
| 5727 | for (d = 0; d < conf->raid_disks; d++) { |
| 5728 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5729 | continue; |
| 5730 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 5731 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 5732 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5733 | raid5_release_stripe(sh); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5734 | schedule(); |
| 5735 | goto again; |
| 5736 | } |
| 5737 | } |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5738 | set_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5739 | finish_wait(&conf->wait_for_overlap, &w); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5740 | sh->overwrite_disks = 0; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5741 | for (d = 0; d < conf->raid_disks; d++) { |
| 5742 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5743 | continue; |
| 5744 | sh->dev[d].towrite = bi; |
| 5745 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5746 | bio_inc_remaining(bi); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5747 | md_write_inc(mddev, bi); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5748 | sh->overwrite_disks++; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5749 | } |
| 5750 | spin_unlock_irq(&sh->stripe_lock); |
| 5751 | if (conf->mddev->bitmap) { |
| 5752 | for (d = 0; |
| 5753 | d < conf->raid_disks - conf->max_degraded; |
| 5754 | d++) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 5755 | md_bitmap_startwrite(mddev->bitmap, |
| 5756 | sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5757 | RAID5_STRIPE_SECTORS(conf), |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 5758 | 0); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5759 | sh->bm_seq = conf->seq_flush + 1; |
| 5760 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 5761 | } |
| 5762 | |
| 5763 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5764 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 5765 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5766 | atomic_inc(&conf->preread_active_stripes); |
| 5767 | release_stripe_plug(mddev, sh); |
| 5768 | } |
| 5769 | |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5770 | bio_endio(bi); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5771 | } |
| 5772 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5773 | static bool raid5_make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5774 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5775 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5776 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5777 | sector_t new_sector; |
| 5778 | sector_t logical_sector, last_sector; |
| 5779 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5780 | const int rw = bio_data_dir(bi); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5781 | DEFINE_WAIT(w); |
| 5782 | bool do_prepare; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5783 | bool do_flush = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5784 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5785 | if (unlikely(bi->bi_opf & REQ_PREFLUSH)) { |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 5786 | int ret = log_handle_flush_request(conf, bi); |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5787 | |
| 5788 | if (ret == 0) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5789 | return true; |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5790 | if (ret == -ENODEV) { |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 5791 | if (md_flush_request(mddev, bi)) |
| 5792 | return true; |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5793 | } |
| 5794 | /* ret == -EAGAIN, fallback */ |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5795 | /* |
| 5796 | * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH, |
| 5797 | * we need to flush journal device |
| 5798 | */ |
| 5799 | do_flush = bi->bi_opf & REQ_PREFLUSH; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 5800 | } |
| 5801 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5802 | if (!md_write_start(mddev, bi)) |
| 5803 | return false; |
Eric Mei | 9ffc8f7 | 2015-03-18 23:39:11 -0600 | [diff] [blame] | 5804 | /* |
| 5805 | * If array is degraded, better not do chunk aligned read because |
| 5806 | * later we might have to read it again in order to reconstruct |
| 5807 | * data on failed drives. |
| 5808 | */ |
| 5809 | if (rw == READ && mddev->degraded == 0 && |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5810 | mddev->reshape_position == MaxSector) { |
| 5811 | bi = chunk_aligned_read(mddev, bi); |
| 5812 | if (!bi) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5813 | return true; |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5814 | } |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 5815 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 5816 | if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) { |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5817 | make_discard_request(mddev, bi); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5818 | md_write_end(mddev); |
| 5819 | return true; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5820 | } |
| 5821 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5822 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5823 | last_sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5824 | bi->bi_next = NULL; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5825 | |
Vishal Verma | bf2c411 | 2021-12-21 20:06:22 +0000 | [diff] [blame] | 5826 | /* Bail out if conflicts with reshape and REQ_NOWAIT is set */ |
| 5827 | if ((bi->bi_opf & REQ_NOWAIT) && |
| 5828 | (conf->reshape_progress != MaxSector) && |
| 5829 | (mddev->reshape_backwards |
| 5830 | ? (logical_sector > conf->reshape_progress && logical_sector <= conf->reshape_safe) |
| 5831 | : (logical_sector >= conf->reshape_safe && logical_sector < conf->reshape_progress))) { |
| 5832 | bio_wouldblock_error(bi); |
| 5833 | if (rw == WRITE) |
| 5834 | md_write_end(mddev); |
| 5835 | return true; |
| 5836 | } |
Guoqing Jiang | 1147f58 | 2021-05-25 17:46:19 +0800 | [diff] [blame] | 5837 | md_account_bio(mddev, &bi); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5838 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5839 | for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5840 | int previous; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5841 | int seq; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5842 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5843 | do_prepare = false; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5844 | retry: |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5845 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5846 | previous = 0; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5847 | if (do_prepare) |
| 5848 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5849 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5850 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5851 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5852 | * 64bit on a 32bit platform, and so it might be |
| 5853 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 5854 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5855 | * the lock is dropped, so once we get a reference |
| 5856 | * to the stripe that we think it is, we will have |
| 5857 | * to check again. |
| 5858 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5859 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5860 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5861 | ? logical_sector < conf->reshape_progress |
| 5862 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5863 | previous = 1; |
| 5864 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5865 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5866 | ? logical_sector < conf->reshape_safe |
| 5867 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5868 | spin_unlock_irq(&conf->device_lock); |
| 5869 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5870 | do_prepare = true; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5871 | goto retry; |
| 5872 | } |
| 5873 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5874 | spin_unlock_irq(&conf->device_lock); |
| 5875 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5876 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5877 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 5878 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5879 | &dd_idx, NULL); |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5880 | pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n", |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5881 | (unsigned long long)new_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5882 | (unsigned long long)logical_sector); |
| 5883 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5884 | sh = raid5_get_active_stripe(conf, new_sector, previous, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5885 | (bi->bi_opf & REQ_RAHEAD), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5887 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5888 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5889 | * stripe, so we must do the range check again. |
| 5890 | * Expansion could still move past after this |
| 5891 | * test, but as we are holding a reference to |
| 5892 | * 'sh', we know that if that happens, |
| 5893 | * STRIPE_EXPANDING will get set and the expansion |
| 5894 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5895 | */ |
| 5896 | int must_retry = 0; |
| 5897 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5898 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5899 | ? logical_sector >= conf->reshape_progress |
| 5900 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5901 | /* mismatch, need to try again */ |
| 5902 | must_retry = 1; |
| 5903 | spin_unlock_irq(&conf->device_lock); |
| 5904 | if (must_retry) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5905 | raid5_release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 5906 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5907 | do_prepare = true; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5908 | goto retry; |
| 5909 | } |
| 5910 | } |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5911 | if (read_seqcount_retry(&conf->gen_lock, seq)) { |
| 5912 | /* Might have got the wrong stripe_head |
| 5913 | * by accident |
| 5914 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5915 | raid5_release_stripe(sh); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5916 | goto retry; |
| 5917 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5918 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5919 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5920 | !add_stripe_bio(sh, bi, dd_idx, rw, previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5921 | /* Stripe is busy expanding or |
| 5922 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | * and wait a while |
| 5924 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 5925 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5926 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5928 | do_prepare = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | goto retry; |
| 5930 | } |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5931 | if (do_flush) { |
| 5932 | set_bit(STRIPE_R5C_PREFLUSH, &sh->state); |
| 5933 | /* we only need flush for one stripe */ |
| 5934 | do_flush = false; |
| 5935 | } |
| 5936 | |
Guoqing Jiang | 1684e97 | 2020-06-16 11:25:52 +0200 | [diff] [blame] | 5937 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 5938 | clear_bit(STRIPE_DELAYED, &sh->state); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5939 | if ((!sh->batch_head || sh == sh->batch_head) && |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5940 | (bi->bi_opf & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5941 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5942 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5943 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5944 | } else { |
| 5945 | /* cannot get stripe for read-ahead, just give-up */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 5946 | bi->bi_status = BLK_STS_IOERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5947 | break; |
| 5948 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5949 | } |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5950 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5951 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5952 | if (rw == WRITE) |
| 5953 | md_write_end(mddev); |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5954 | bio_endio(bi); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5955 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5956 | } |
| 5957 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5958 | 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] | 5959 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5960 | 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] | 5961 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5962 | /* reshaping is quite different to recovery/resync so it is |
| 5963 | * handled quite separately ... here. |
| 5964 | * |
| 5965 | * On each call to sync_request, we gather one chunk worth of |
| 5966 | * destination stripes and flag them as expanding. |
| 5967 | * Then we find all the source stripes and request reads. |
| 5968 | * As the reads complete, handle_stripe will copy the data |
| 5969 | * into the destination stripe and release that stripe. |
| 5970 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5971 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5972 | struct stripe_head *sh; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 5973 | struct md_rdev *rdev; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5974 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5975 | int raid_disks = conf->previous_raid_disks; |
| 5976 | int data_disks = raid_disks - conf->max_degraded; |
| 5977 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5978 | int i; |
| 5979 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5980 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5981 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5982 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5983 | struct list_head stripes; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5984 | sector_t retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5985 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5986 | if (sector_nr == 0) { |
| 5987 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5988 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5989 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 5990 | sector_nr = raid5_size(mddev, 0, 0) |
| 5991 | - conf->reshape_progress; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 5992 | } else if (mddev->reshape_backwards && |
| 5993 | conf->reshape_progress == MaxSector) { |
| 5994 | /* shouldn't happen, but just in case, finish up.*/ |
| 5995 | sector_nr = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5996 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5997 | conf->reshape_progress > 0) |
| 5998 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5999 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6000 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 6001 | mddev->curr_resync_completed = sector_nr; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6002 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6003 | *skipped = 1; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 6004 | retn = sector_nr; |
| 6005 | goto finish; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6006 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6007 | } |
| 6008 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6009 | /* We need to process a full chunk at a time. |
| 6010 | * If old and new chunk sizes differ, we need to process the |
| 6011 | * largest of these |
| 6012 | */ |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 6013 | |
| 6014 | reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6015 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6016 | /* We update the metadata at least every 10 seconds, or when |
| 6017 | * the data about to be copied would over-write the source of |
| 6018 | * the data at the front of the range. i.e. one new_stripe |
| 6019 | * along from reshape_progress new_maps to after where |
| 6020 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6021 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6022 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6023 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6024 | readpos = conf->reshape_progress; |
| 6025 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6026 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6027 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6028 | if (mddev->reshape_backwards) { |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 6029 | BUG_ON(writepos < reshape_sectors); |
| 6030 | writepos -= reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6031 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6032 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6033 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6034 | writepos += reshape_sectors; |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 6035 | /* readpos and safepos are worst-case calculations. |
| 6036 | * A negative number is overly pessimistic, and causes |
| 6037 | * obvious problems for unsigned storage. So clip to 0. |
| 6038 | */ |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 6039 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 6040 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6041 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6042 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6043 | /* Having calculated the 'writepos' possibly use it |
| 6044 | * to set 'stripe_addr' which is where we will write to. |
| 6045 | */ |
| 6046 | if (mddev->reshape_backwards) { |
| 6047 | BUG_ON(conf->reshape_progress == 0); |
| 6048 | stripe_addr = writepos; |
| 6049 | BUG_ON((mddev->dev_sectors & |
| 6050 | ~((sector_t)reshape_sectors - 1)) |
| 6051 | - reshape_sectors - stripe_addr |
| 6052 | != sector_nr); |
| 6053 | } else { |
| 6054 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 6055 | stripe_addr = sector_nr; |
| 6056 | } |
| 6057 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6058 | /* 'writepos' is the most advanced device address we might write. |
| 6059 | * 'readpos' is the least advanced device address we might read. |
| 6060 | * 'safepos' is the least address recorded in the metadata as having |
| 6061 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6062 | * If there is a min_offset_diff, these are adjusted either by |
| 6063 | * increasing the safepos/readpos if diff is negative, or |
| 6064 | * increasing writepos if diff is positive. |
| 6065 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6066 | * ensure safety in the face of a crash - that must be done by userspace |
| 6067 | * making a backup of the data. So in that case there is no particular |
| 6068 | * rush to update metadata. |
| 6069 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 6070 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 6071 | * we can be safe in the event of a crash. |
| 6072 | * So we insist on updating metadata if safepos is behind writepos and |
| 6073 | * readpos is beyond writepos. |
| 6074 | * In any case, update the metadata every 10 seconds. |
| 6075 | * Maybe that number should be configurable, but I'm not sure it is |
| 6076 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 6077 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6078 | if (conf->min_offset_diff < 0) { |
| 6079 | safepos += -conf->min_offset_diff; |
| 6080 | readpos += -conf->min_offset_diff; |
| 6081 | } else |
| 6082 | writepos += conf->min_offset_diff; |
| 6083 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6084 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6085 | ? (safepos > writepos && readpos < writepos) |
| 6086 | : (safepos < writepos && readpos > writepos)) || |
| 6087 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6088 | /* Cannot proceed until we've updated the superblock... */ |
| 6089 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6090 | atomic_read(&conf->reshape_stripes)==0 |
| 6091 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 6092 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 6093 | return 0; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6094 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6095 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 6096 | if (!mddev->reshape_backwards) |
| 6097 | /* Can update recovery_offset */ |
| 6098 | rdev_for_each(rdev, mddev) |
| 6099 | if (rdev->raid_disk >= 0 && |
| 6100 | !test_bit(Journal, &rdev->flags) && |
| 6101 | !test_bit(In_sync, &rdev->flags) && |
| 6102 | rdev->recovery_offset < sector_nr) |
| 6103 | rdev->recovery_offset = sector_nr; |
| 6104 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6105 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6106 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6107 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6108 | wait_event(mddev->sb_wait, mddev->sb_flags == 0 || |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6109 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 6110 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6111 | return 0; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6112 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6113 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6114 | spin_unlock_irq(&conf->device_lock); |
| 6115 | wake_up(&conf->wait_for_overlap); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6116 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6117 | } |
| 6118 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 6119 | INIT_LIST_HEAD(&stripes); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6120 | for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6121 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6122 | int skipped_disk = 0; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6123 | sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6124 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 6125 | atomic_inc(&conf->reshape_stripes); |
| 6126 | /* If any of this stripe is beyond the end of the old |
| 6127 | * array, then we need to zero those blocks |
| 6128 | */ |
| 6129 | for (j=sh->disks; j--;) { |
| 6130 | sector_t s; |
| 6131 | if (j == sh->pd_idx) |
| 6132 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6133 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6134 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6135 | continue; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6136 | s = raid5_compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6137 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6138 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6139 | continue; |
| 6140 | } |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6141 | memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf)); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6142 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 6143 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 6144 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6145 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6146 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 6147 | set_bit(STRIPE_HANDLE, &sh->state); |
| 6148 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 6149 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6150 | } |
| 6151 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6152 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6153 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6154 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6155 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6156 | spin_unlock_irq(&conf->device_lock); |
| 6157 | /* Ok, those stripe are ready. We can start scheduling |
| 6158 | * reads on the source stripes. |
| 6159 | * The source stripes are determined by mapping the first and last |
| 6160 | * block on the destination stripes. |
| 6161 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6162 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6163 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6164 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6165 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 6166 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6167 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6168 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6169 | if (last_sector >= mddev->dev_sectors) |
| 6170 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6171 | while (first_sector <= last_sector) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6172 | sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6173 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 6174 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6175 | raid5_release_stripe(sh); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6176 | first_sector += RAID5_STRIPE_SECTORS(conf); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6177 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 6178 | /* Now that the sources are clearly marked, we can release |
| 6179 | * the destination stripes |
| 6180 | */ |
| 6181 | while (!list_empty(&stripes)) { |
| 6182 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 6183 | list_del_init(&sh->lru); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6184 | raid5_release_stripe(sh); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 6185 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6186 | /* If this takes us to the resync_max point where we have to pause, |
| 6187 | * then we need to write out the superblock. |
| 6188 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6189 | sector_nr += reshape_sectors; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 6190 | retn = reshape_sectors; |
| 6191 | finish: |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 6192 | if (mddev->curr_resync_completed > mddev->resync_max || |
| 6193 | (sector_nr - mddev->curr_resync_completed) * 2 |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 6194 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6195 | /* Cannot proceed until we've updated the superblock... */ |
| 6196 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6197 | atomic_read(&conf->reshape_stripes) == 0 |
| 6198 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 6199 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 6200 | goto ret; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6201 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6202 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 6203 | if (!mddev->reshape_backwards) |
| 6204 | /* Can update recovery_offset */ |
| 6205 | rdev_for_each(rdev, mddev) |
| 6206 | if (rdev->raid_disk >= 0 && |
| 6207 | !test_bit(Journal, &rdev->flags) && |
| 6208 | !test_bit(In_sync, &rdev->flags) && |
| 6209 | rdev->recovery_offset < sector_nr) |
| 6210 | rdev->recovery_offset = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6211 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6212 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6213 | md_wakeup_thread(mddev->thread); |
| 6214 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6215 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6216 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 6217 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6218 | goto ret; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6219 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6220 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6221 | spin_unlock_irq(&conf->device_lock); |
| 6222 | wake_up(&conf->wait_for_overlap); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6223 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6224 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6225 | ret: |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 6226 | return retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6227 | } |
| 6228 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 6229 | static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr, |
| 6230 | int *skipped) |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6231 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6232 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6233 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6234 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 6235 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6236 | int still_degraded = 0; |
| 6237 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6238 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6239 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6240 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6241 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6242 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 6243 | end_reshape(conf); |
| 6244 | return 0; |
| 6245 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6246 | |
| 6247 | if (mddev->curr_resync < max_sector) /* aborted */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6248 | md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 6249 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6250 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6251 | conf->fullsync = 0; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6252 | md_bitmap_close_sync(mddev->bitmap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6253 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | return 0; |
| 6255 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6256 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6257 | /* Allow raid5_quiesce to complete */ |
| 6258 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 6259 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6260 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6261 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6262 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6263 | /* No need to check resync_max as we never do more than one |
| 6264 | * stripe, and as resync_max will always be on a chunk boundary, |
| 6265 | * if the check in md_do_sync didn't fire, there is no chance |
| 6266 | * of overstepping resync_max here |
| 6267 | */ |
| 6268 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6269 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6270 | * to resync, then assert that we are finished, because there is |
| 6271 | * nothing we can do. |
| 6272 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 6273 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6274 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6275 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6276 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6277 | return rv; |
| 6278 | } |
majianpeng | 6f60804 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6279 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 6280 | !conf->fullsync && |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6281 | !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6282 | sync_blocks >= RAID5_STRIPE_SECTORS(conf)) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6283 | /* we can skip this block, and probably more */ |
Yufen Yu | 83c3e5e | 2020-07-22 23:29:05 -0400 | [diff] [blame] | 6284 | do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf)); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6285 | *skipped = 1; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6286 | /* keep things rounded to whole stripes */ |
| 6287 | return sync_blocks * RAID5_STRIPE_SECTORS(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6288 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6289 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6290 | md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 6291 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6292 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6293 | if (sh == NULL) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6294 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6295 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6296 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6297 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 6298 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6299 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6300 | /* Need to check if array will still be degraded after recovery/resync |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6301 | * Note in case of > 1 drive failures it's possible we're rebuilding |
| 6302 | * one drive while leaving another faulty drive in array. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6303 | */ |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6304 | rcu_read_lock(); |
| 6305 | for (i = 0; i < conf->raid_disks; i++) { |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 6306 | struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev); |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6307 | |
| 6308 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6309 | still_degraded = 1; |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6310 | } |
| 6311 | rcu_read_unlock(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6312 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6313 | md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6314 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 6315 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Eivind Sarto | 053f5b6 | 2014-06-09 17:06:19 -0700 | [diff] [blame] | 6316 | set_bit(STRIPE_HANDLE, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6317 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6318 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6319 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6320 | return RAID5_STRIPE_SECTORS(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6321 | } |
| 6322 | |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6323 | static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio, |
| 6324 | unsigned int offset) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6325 | { |
| 6326 | /* We may not be able to submit a whole bio at once as there |
| 6327 | * may not be enough stripe_heads available. |
| 6328 | * We cannot pre-allocate enough stripe_heads as we may need |
| 6329 | * more than exist in the cache (if we allow ever large chunks). |
| 6330 | * So we do one stripe head at a time and record in |
| 6331 | * ->bi_hw_segments how many have been done. |
| 6332 | * |
| 6333 | * We *know* that this entire raid_bio is in one chunk, so |
| 6334 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 6335 | */ |
| 6336 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6337 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6338 | sector_t sector, logical_sector, last_sector; |
| 6339 | int scnt = 0; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6340 | int handled = 0; |
| 6341 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6342 | logical_sector = raid_bio->bi_iter.bi_sector & |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6343 | ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6344 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6345 | 0, &dd_idx, NULL); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 6346 | last_sector = bio_end_sector(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6347 | |
| 6348 | for (; logical_sector < last_sector; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6349 | logical_sector += RAID5_STRIPE_SECTORS(conf), |
| 6350 | sector += RAID5_STRIPE_SECTORS(conf), |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6351 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6352 | |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6353 | if (scnt < offset) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6354 | /* already done this stripe */ |
| 6355 | continue; |
| 6356 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6357 | sh = raid5_get_active_stripe(conf, sector, 0, 1, 1); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6358 | |
| 6359 | if (!sh) { |
| 6360 | /* failed to get a stripe - must wait */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6361 | conf->retry_read_aligned = raid_bio; |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6362 | conf->retry_read_offset = scnt; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6363 | return handled; |
| 6364 | } |
| 6365 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 6366 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6367 | raid5_release_stripe(sh); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6368 | conf->retry_read_aligned = raid_bio; |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6369 | conf->retry_read_offset = scnt; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6370 | return handled; |
| 6371 | } |
| 6372 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 6373 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6374 | handle_stripe(sh); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6375 | raid5_release_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6376 | handled++; |
| 6377 | } |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6378 | |
| 6379 | bio_endio(raid_bio); |
| 6380 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6381 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 6382 | wake_up(&conf->wait_for_quiescent); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6383 | return handled; |
| 6384 | } |
| 6385 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 6386 | static int handle_active_stripes(struct r5conf *conf, int group, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6387 | struct r5worker *worker, |
| 6388 | struct list_head *temp_inactive_list) |
Christoph Hellwig | efcd487 | 2019-04-04 18:56:16 +0200 | [diff] [blame] | 6389 | __releases(&conf->device_lock) |
| 6390 | __acquires(&conf->device_lock) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6391 | { |
| 6392 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6393 | int i, batch_size = 0, hash; |
| 6394 | bool release_inactive = false; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6395 | |
| 6396 | while (batch_size < MAX_STRIPE_BATCH && |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6397 | (sh = __get_priority_stripe(conf, group)) != NULL) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6398 | batch[batch_size++] = sh; |
| 6399 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6400 | if (batch_size == 0) { |
| 6401 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6402 | if (!list_empty(temp_inactive_list + i)) |
| 6403 | break; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6404 | if (i == NR_STRIPE_HASH_LOCKS) { |
| 6405 | spin_unlock_irq(&conf->device_lock); |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 6406 | log_flush_stripe_to_raid(conf); |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6407 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6408 | return batch_size; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6409 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6410 | release_inactive = true; |
| 6411 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6412 | spin_unlock_irq(&conf->device_lock); |
| 6413 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6414 | release_inactive_stripe_list(conf, temp_inactive_list, |
| 6415 | NR_STRIPE_HASH_LOCKS); |
| 6416 | |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6417 | r5l_flush_stripe_to_raid(conf->log); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6418 | if (release_inactive) { |
| 6419 | spin_lock_irq(&conf->device_lock); |
| 6420 | return 0; |
| 6421 | } |
| 6422 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6423 | for (i = 0; i < batch_size; i++) |
| 6424 | handle_stripe(batch[i]); |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 6425 | log_write_stripe_run(conf); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6426 | |
| 6427 | cond_resched(); |
| 6428 | |
| 6429 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6430 | for (i = 0; i < batch_size; i++) { |
| 6431 | hash = batch[i]->hash_lock_index; |
| 6432 | __release_stripe(conf, batch[i], &temp_inactive_list[hash]); |
| 6433 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6434 | return batch_size; |
| 6435 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6436 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6437 | static void raid5_do_work(struct work_struct *work) |
| 6438 | { |
| 6439 | struct r5worker *worker = container_of(work, struct r5worker, work); |
| 6440 | struct r5worker_group *group = worker->group; |
| 6441 | struct r5conf *conf = group->conf; |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 6442 | struct mddev *mddev = conf->mddev; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6443 | int group_id = group - conf->worker_groups; |
| 6444 | int handled; |
| 6445 | struct blk_plug plug; |
| 6446 | |
| 6447 | pr_debug("+++ raid5worker active\n"); |
| 6448 | |
| 6449 | blk_start_plug(&plug); |
| 6450 | handled = 0; |
| 6451 | spin_lock_irq(&conf->device_lock); |
| 6452 | while (1) { |
| 6453 | int batch_size, released; |
| 6454 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6455 | released = release_stripe_list(conf, worker->temp_inactive_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6456 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6457 | batch_size = handle_active_stripes(conf, group_id, worker, |
| 6458 | worker->temp_inactive_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 6459 | worker->working = false; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6460 | if (!batch_size && !released) |
| 6461 | break; |
| 6462 | handled += batch_size; |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 6463 | wait_event_lock_irq(mddev->sb_wait, |
| 6464 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags), |
| 6465 | conf->device_lock); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6466 | } |
| 6467 | pr_debug("%d stripes handled\n", handled); |
| 6468 | |
| 6469 | spin_unlock_irq(&conf->device_lock); |
Ofer Heifetz | 7e96d55 | 2017-07-24 09:17:40 +0300 | [diff] [blame] | 6470 | |
Song Liu | 9c72a18e4 | 2017-08-24 09:53:59 -0700 | [diff] [blame] | 6471 | flush_deferred_bios(conf); |
| 6472 | |
| 6473 | r5l_flush_stripe_to_raid(conf->log); |
| 6474 | |
Ofer Heifetz | 7e96d55 | 2017-07-24 09:17:40 +0300 | [diff] [blame] | 6475 | async_tx_issue_pending_all(); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6476 | blk_finish_plug(&plug); |
| 6477 | |
| 6478 | pr_debug("--- raid5worker inactive\n"); |
| 6479 | } |
| 6480 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6481 | /* |
| 6482 | * This is our raid5 kernel thread. |
| 6483 | * |
| 6484 | * We scan the hash table for stripes which can be handled now. |
| 6485 | * During the scan, completed stripes are saved for us by the interrupt |
| 6486 | * handler, so that they will not have to wait for our next wakeup. |
| 6487 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6488 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6489 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6490 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6491 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6492 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6493 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6494 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6495 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6496 | |
| 6497 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6498 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6499 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6500 | handled = 0; |
| 6501 | spin_lock_irq(&conf->device_lock); |
| 6502 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6503 | struct bio *bio; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6504 | int batch_size, released; |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6505 | unsigned int offset; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6506 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6507 | released = release_stripe_list(conf, conf->temp_inactive_list); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6508 | if (released) |
| 6509 | clear_bit(R5_DID_ALLOC, &conf->cache_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6510 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 6511 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 6512 | !list_empty(&conf->bitmap_list)) { |
| 6513 | /* Now is a good time to flush some bitmap updates */ |
| 6514 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 6515 | spin_unlock_irq(&conf->device_lock); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6516 | md_bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 6517 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 6518 | conf->seq_write = conf->seq_flush; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6519 | activate_bit_delay(conf, conf->temp_inactive_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6520 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 6521 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6522 | |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6523 | while ((bio = remove_bio_from_retry(conf, &offset))) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6524 | int ok; |
| 6525 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6526 | ok = retry_aligned_read(conf, bio, offset); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6527 | spin_lock_irq(&conf->device_lock); |
| 6528 | if (!ok) |
| 6529 | break; |
| 6530 | handled++; |
| 6531 | } |
| 6532 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6533 | batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, |
| 6534 | conf->temp_inactive_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6535 | if (!batch_size && !released) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6536 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6537 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6538 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6539 | if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) { |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6540 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6541 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6542 | spin_lock_irq(&conf->device_lock); |
| 6543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6544 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6545 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6546 | |
| 6547 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6548 | if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) && |
| 6549 | mutex_trylock(&conf->cache_size_mutex)) { |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6550 | grow_one_stripe(conf, __GFP_NOWARN); |
| 6551 | /* Set flag even if allocation failed. This helps |
| 6552 | * slow down allocation requests when mem is short |
| 6553 | */ |
| 6554 | set_bit(R5_DID_ALLOC, &conf->cache_state); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6555 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6556 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6557 | |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 6558 | flush_deferred_bios(conf); |
| 6559 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 6560 | r5l_flush_stripe_to_raid(conf->log); |
| 6561 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 6562 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6563 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6564 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6565 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6566 | } |
| 6567 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6568 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6569 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6570 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6571 | struct r5conf *conf; |
| 6572 | int ret = 0; |
| 6573 | spin_lock(&mddev->lock); |
| 6574 | conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6575 | if (conf) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6576 | ret = sprintf(page, "%d\n", conf->min_nr_stripes); |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6577 | spin_unlock(&mddev->lock); |
| 6578 | return ret; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6579 | } |
| 6580 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6581 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6582 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6583 | { |
Alexei Naberezhnov | 483cbbe | 2018-03-27 16:54:16 -0700 | [diff] [blame] | 6584 | int result = 0; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6585 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6586 | |
| 6587 | if (size <= 16 || size > 32768) |
| 6588 | return -EINVAL; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6589 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6590 | conf->min_nr_stripes = size; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6591 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6592 | while (size < conf->max_nr_stripes && |
| 6593 | drop_one_stripe(conf)) |
| 6594 | ; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6595 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6596 | |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 6597 | md_allow_write(mddev); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6598 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6599 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6600 | while (size > conf->max_nr_stripes) |
Alexei Naberezhnov | 483cbbe | 2018-03-27 16:54:16 -0700 | [diff] [blame] | 6601 | if (!grow_one_stripe(conf, GFP_KERNEL)) { |
| 6602 | conf->min_nr_stripes = conf->max_nr_stripes; |
| 6603 | result = -ENOMEM; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6604 | break; |
Alexei Naberezhnov | 483cbbe | 2018-03-27 16:54:16 -0700 | [diff] [blame] | 6605 | } |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6606 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6607 | |
Alexei Naberezhnov | 483cbbe | 2018-03-27 16:54:16 -0700 | [diff] [blame] | 6608 | return result; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6609 | } |
| 6610 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 6611 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6612 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6613 | 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] | 6614 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6615 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6616 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6617 | int err; |
| 6618 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6619 | if (len >= PAGE_SIZE) |
| 6620 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6621 | if (kstrtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6622 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6623 | err = mddev_lock(mddev); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6624 | if (err) |
| 6625 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6626 | conf = mddev->private; |
| 6627 | if (!conf) |
| 6628 | err = -ENODEV; |
| 6629 | else |
| 6630 | err = raid5_set_cache_size(mddev, new); |
| 6631 | mddev_unlock(mddev); |
| 6632 | |
| 6633 | return err ?: len; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6634 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6635 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6636 | static struct md_sysfs_entry |
| 6637 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 6638 | raid5_show_stripe_cache_size, |
| 6639 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6640 | |
| 6641 | static ssize_t |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6642 | raid5_show_rmw_level(struct mddev *mddev, char *page) |
| 6643 | { |
| 6644 | struct r5conf *conf = mddev->private; |
| 6645 | if (conf) |
| 6646 | return sprintf(page, "%d\n", conf->rmw_level); |
| 6647 | else |
| 6648 | return 0; |
| 6649 | } |
| 6650 | |
| 6651 | static ssize_t |
| 6652 | raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len) |
| 6653 | { |
| 6654 | struct r5conf *conf = mddev->private; |
| 6655 | unsigned long new; |
| 6656 | |
| 6657 | if (!conf) |
| 6658 | return -ENODEV; |
| 6659 | |
| 6660 | if (len >= PAGE_SIZE) |
| 6661 | return -EINVAL; |
| 6662 | |
| 6663 | if (kstrtoul(page, 10, &new)) |
| 6664 | return -EINVAL; |
| 6665 | |
| 6666 | if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome) |
| 6667 | return -EINVAL; |
| 6668 | |
| 6669 | if (new != PARITY_DISABLE_RMW && |
| 6670 | new != PARITY_ENABLE_RMW && |
| 6671 | new != PARITY_PREFER_RMW) |
| 6672 | return -EINVAL; |
| 6673 | |
| 6674 | conf->rmw_level = new; |
| 6675 | return len; |
| 6676 | } |
| 6677 | |
| 6678 | static struct md_sysfs_entry |
| 6679 | raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR, |
| 6680 | raid5_show_rmw_level, |
| 6681 | raid5_store_rmw_level); |
| 6682 | |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6683 | static ssize_t |
| 6684 | raid5_show_stripe_size(struct mddev *mddev, char *page) |
| 6685 | { |
| 6686 | struct r5conf *conf; |
| 6687 | int ret = 0; |
| 6688 | |
| 6689 | spin_lock(&mddev->lock); |
| 6690 | conf = mddev->private; |
| 6691 | if (conf) |
| 6692 | ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf)); |
| 6693 | spin_unlock(&mddev->lock); |
| 6694 | return ret; |
| 6695 | } |
| 6696 | |
| 6697 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 6698 | static ssize_t |
| 6699 | raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len) |
| 6700 | { |
| 6701 | struct r5conf *conf; |
| 6702 | unsigned long new; |
| 6703 | int err; |
Yufen Yu | 3891258 | 2020-08-20 09:22:14 -0400 | [diff] [blame] | 6704 | int size; |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6705 | |
| 6706 | if (len >= PAGE_SIZE) |
| 6707 | return -EINVAL; |
| 6708 | if (kstrtoul(page, 10, &new)) |
| 6709 | return -EINVAL; |
| 6710 | |
| 6711 | /* |
| 6712 | * The value should not be bigger than PAGE_SIZE. It requires to |
Yufen Yu | 6af10a3 | 2020-08-20 09:22:05 -0400 | [diff] [blame] | 6713 | * be multiple of DEFAULT_STRIPE_SIZE and the value should be power |
| 6714 | * of two. |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6715 | */ |
Yufen Yu | 6af10a3 | 2020-08-20 09:22:05 -0400 | [diff] [blame] | 6716 | if (new % DEFAULT_STRIPE_SIZE != 0 || |
| 6717 | new > PAGE_SIZE || new == 0 || |
| 6718 | new != roundup_pow_of_two(new)) |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6719 | return -EINVAL; |
| 6720 | |
| 6721 | err = mddev_lock(mddev); |
| 6722 | if (err) |
| 6723 | return err; |
| 6724 | |
| 6725 | conf = mddev->private; |
| 6726 | if (!conf) { |
| 6727 | err = -ENODEV; |
| 6728 | goto out_unlock; |
| 6729 | } |
| 6730 | |
| 6731 | if (new == conf->stripe_size) |
| 6732 | goto out_unlock; |
| 6733 | |
| 6734 | pr_debug("md/raid: change stripe_size from %lu to %lu\n", |
| 6735 | conf->stripe_size, new); |
| 6736 | |
Yufen Yu | 3891258 | 2020-08-20 09:22:14 -0400 | [diff] [blame] | 6737 | if (mddev->sync_thread || |
| 6738 | test_bit(MD_RECOVERY_RUNNING, &mddev->recovery) || |
| 6739 | mddev->reshape_position != MaxSector || |
| 6740 | mddev->sysfs_active) { |
| 6741 | err = -EBUSY; |
| 6742 | goto out_unlock; |
| 6743 | } |
| 6744 | |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6745 | mddev_suspend(mddev); |
Yufen Yu | 3891258 | 2020-08-20 09:22:14 -0400 | [diff] [blame] | 6746 | mutex_lock(&conf->cache_size_mutex); |
| 6747 | size = conf->max_nr_stripes; |
| 6748 | |
| 6749 | shrink_stripes(conf); |
| 6750 | |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6751 | conf->stripe_size = new; |
| 6752 | conf->stripe_shift = ilog2(new) - 9; |
| 6753 | conf->stripe_sectors = new >> 9; |
Yufen Yu | 3891258 | 2020-08-20 09:22:14 -0400 | [diff] [blame] | 6754 | if (grow_stripes(conf, size)) { |
| 6755 | pr_warn("md/raid:%s: couldn't allocate buffers\n", |
| 6756 | mdname(mddev)); |
| 6757 | err = -ENOMEM; |
| 6758 | } |
| 6759 | mutex_unlock(&conf->cache_size_mutex); |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6760 | mddev_resume(mddev); |
| 6761 | |
| 6762 | out_unlock: |
| 6763 | mddev_unlock(mddev); |
| 6764 | return err ?: len; |
| 6765 | } |
| 6766 | |
| 6767 | static struct md_sysfs_entry |
| 6768 | raid5_stripe_size = __ATTR(stripe_size, 0644, |
| 6769 | raid5_show_stripe_size, |
| 6770 | raid5_store_stripe_size); |
| 6771 | #else |
| 6772 | static struct md_sysfs_entry |
| 6773 | raid5_stripe_size = __ATTR(stripe_size, 0444, |
| 6774 | raid5_show_stripe_size, |
| 6775 | NULL); |
| 6776 | #endif |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6777 | |
| 6778 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6779 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6780 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6781 | struct r5conf *conf; |
| 6782 | int ret = 0; |
| 6783 | spin_lock(&mddev->lock); |
| 6784 | conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6785 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6786 | ret = sprintf(page, "%d\n", conf->bypass_threshold); |
| 6787 | spin_unlock(&mddev->lock); |
| 6788 | return ret; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6789 | } |
| 6790 | |
| 6791 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6792 | 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] | 6793 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6794 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6795 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6796 | int err; |
| 6797 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6798 | if (len >= PAGE_SIZE) |
| 6799 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6800 | if (kstrtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6801 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6802 | |
| 6803 | err = mddev_lock(mddev); |
| 6804 | if (err) |
| 6805 | return err; |
| 6806 | conf = mddev->private; |
| 6807 | if (!conf) |
| 6808 | err = -ENODEV; |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6809 | else if (new > conf->min_nr_stripes) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6810 | err = -EINVAL; |
| 6811 | else |
| 6812 | conf->bypass_threshold = new; |
| 6813 | mddev_unlock(mddev); |
| 6814 | return err ?: len; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6815 | } |
| 6816 | |
| 6817 | static struct md_sysfs_entry |
| 6818 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 6819 | S_IRUGO | S_IWUSR, |
| 6820 | raid5_show_preread_threshold, |
| 6821 | raid5_store_preread_threshold); |
| 6822 | |
| 6823 | static ssize_t |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6824 | raid5_show_skip_copy(struct mddev *mddev, char *page) |
| 6825 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6826 | struct r5conf *conf; |
| 6827 | int ret = 0; |
| 6828 | spin_lock(&mddev->lock); |
| 6829 | conf = mddev->private; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6830 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6831 | ret = sprintf(page, "%d\n", conf->skip_copy); |
| 6832 | spin_unlock(&mddev->lock); |
| 6833 | return ret; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6834 | } |
| 6835 | |
| 6836 | static ssize_t |
| 6837 | raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) |
| 6838 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6839 | struct r5conf *conf; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6840 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6841 | int err; |
| 6842 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6843 | if (len >= PAGE_SIZE) |
| 6844 | return -EINVAL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6845 | if (kstrtoul(page, 10, &new)) |
| 6846 | return -EINVAL; |
| 6847 | new = !!new; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6848 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6849 | err = mddev_lock(mddev); |
| 6850 | if (err) |
| 6851 | return err; |
| 6852 | conf = mddev->private; |
| 6853 | if (!conf) |
| 6854 | err = -ENODEV; |
| 6855 | else if (new != conf->skip_copy) { |
Christoph Hellwig | 1cb039f | 2020-09-24 08:51:38 +0200 | [diff] [blame] | 6856 | struct request_queue *q = mddev->queue; |
| 6857 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6858 | mddev_suspend(mddev); |
| 6859 | conf->skip_copy = new; |
| 6860 | if (new) |
Christoph Hellwig | 1cb039f | 2020-09-24 08:51:38 +0200 | [diff] [blame] | 6861 | blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6862 | else |
Christoph Hellwig | 1cb039f | 2020-09-24 08:51:38 +0200 | [diff] [blame] | 6863 | blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6864 | mddev_resume(mddev); |
| 6865 | } |
| 6866 | mddev_unlock(mddev); |
| 6867 | return err ?: len; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6868 | } |
| 6869 | |
| 6870 | static struct md_sysfs_entry |
| 6871 | raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, |
| 6872 | raid5_show_skip_copy, |
| 6873 | raid5_store_skip_copy); |
| 6874 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6875 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6876 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6877 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6878 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6879 | if (conf) |
| 6880 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 6881 | else |
| 6882 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6883 | } |
| 6884 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6885 | static struct md_sysfs_entry |
| 6886 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6887 | |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6888 | static ssize_t |
| 6889 | raid5_show_group_thread_cnt(struct mddev *mddev, char *page) |
| 6890 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6891 | struct r5conf *conf; |
| 6892 | int ret = 0; |
| 6893 | spin_lock(&mddev->lock); |
| 6894 | conf = mddev->private; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6895 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6896 | ret = sprintf(page, "%d\n", conf->worker_cnt_per_group); |
| 6897 | spin_unlock(&mddev->lock); |
| 6898 | return ret; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6899 | } |
| 6900 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6901 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 6902 | int *group_cnt, |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6903 | struct r5worker_group **worker_groups); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6904 | static ssize_t |
| 6905 | raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) |
| 6906 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6907 | struct r5conf *conf; |
Shaohua Li | 7d5d7b5 | 2017-09-21 11:03:52 -0700 | [diff] [blame] | 6908 | unsigned int new; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6909 | int err; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6910 | struct r5worker_group *new_groups, *old_groups; |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 6911 | int group_cnt; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6912 | |
| 6913 | if (len >= PAGE_SIZE) |
| 6914 | return -EINVAL; |
Shaohua Li | 7d5d7b5 | 2017-09-21 11:03:52 -0700 | [diff] [blame] | 6915 | if (kstrtouint(page, 10, &new)) |
| 6916 | return -EINVAL; |
| 6917 | /* 8192 should be big enough */ |
| 6918 | if (new > 8192) |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6919 | return -EINVAL; |
| 6920 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6921 | err = mddev_lock(mddev); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6922 | if (err) |
| 6923 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6924 | conf = mddev->private; |
| 6925 | if (!conf) |
| 6926 | err = -ENODEV; |
| 6927 | else if (new != conf->worker_cnt_per_group) { |
| 6928 | mddev_suspend(mddev); |
| 6929 | |
| 6930 | old_groups = conf->worker_groups; |
| 6931 | if (old_groups) |
| 6932 | flush_workqueue(raid5_wq); |
| 6933 | |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 6934 | err = alloc_thread_groups(conf, new, &group_cnt, &new_groups); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6935 | if (!err) { |
| 6936 | spin_lock_irq(&conf->device_lock); |
| 6937 | conf->group_cnt = group_cnt; |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 6938 | conf->worker_cnt_per_group = new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6939 | conf->worker_groups = new_groups; |
| 6940 | spin_unlock_irq(&conf->device_lock); |
| 6941 | |
| 6942 | if (old_groups) |
| 6943 | kfree(old_groups[0].workers); |
| 6944 | kfree(old_groups); |
| 6945 | } |
| 6946 | mddev_resume(mddev); |
| 6947 | } |
| 6948 | mddev_unlock(mddev); |
| 6949 | |
| 6950 | return err ?: len; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6951 | } |
| 6952 | |
| 6953 | static struct md_sysfs_entry |
| 6954 | raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, |
| 6955 | raid5_show_group_thread_cnt, |
| 6956 | raid5_store_group_thread_cnt); |
| 6957 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6958 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6959 | &raid5_stripecache_size.attr, |
| 6960 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6961 | &raid5_preread_bypass_threshold.attr, |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6962 | &raid5_group_thread_cnt.attr, |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6963 | &raid5_skip_copy.attr, |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6964 | &raid5_rmw_level.attr, |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6965 | &raid5_stripe_size.attr, |
Song Liu | 2c7da14 | 2016-11-17 15:24:41 -0800 | [diff] [blame] | 6966 | &r5c_journal_mode.attr, |
Mariusz Dabrowski | a596d08 | 2019-02-18 15:04:09 +0100 | [diff] [blame] | 6967 | &ppl_write_hint.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6968 | NULL, |
| 6969 | }; |
Rikard Falkeborn | c32dc04 | 2021-05-29 12:30:49 +0200 | [diff] [blame] | 6970 | static const struct attribute_group raid5_attrs_group = { |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6971 | .name = NULL, |
| 6972 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6973 | }; |
| 6974 | |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 6975 | static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt, |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6976 | struct r5worker_group **worker_groups) |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6977 | { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6978 | int i, j, k; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6979 | ssize_t size; |
| 6980 | struct r5worker *workers; |
| 6981 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6982 | if (cnt == 0) { |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6983 | *group_cnt = 0; |
| 6984 | *worker_groups = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6985 | return 0; |
| 6986 | } |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6987 | *group_cnt = num_possible_nodes(); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6988 | size = sizeof(struct r5worker) * cnt; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 6989 | workers = kcalloc(size, *group_cnt, GFP_NOIO); |
| 6990 | *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group), |
| 6991 | GFP_NOIO); |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6992 | if (!*worker_groups || !workers) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6993 | kfree(workers); |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6994 | kfree(*worker_groups); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6995 | return -ENOMEM; |
| 6996 | } |
| 6997 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6998 | for (i = 0; i < *group_cnt; i++) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6999 | struct r5worker_group *group; |
| 7000 | |
NeilBrown | 0c775d5 | 2013-11-25 11:12:43 +1100 | [diff] [blame] | 7001 | group = &(*worker_groups)[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7002 | INIT_LIST_HEAD(&group->handle_list); |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 7003 | INIT_LIST_HEAD(&group->loprio_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7004 | group->conf = conf; |
| 7005 | group->workers = workers + i * cnt; |
| 7006 | |
| 7007 | for (j = 0; j < cnt; j++) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7008 | struct r5worker *worker = group->workers + j; |
| 7009 | worker->group = group; |
| 7010 | INIT_WORK(&worker->work, raid5_do_work); |
| 7011 | |
| 7012 | for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) |
| 7013 | INIT_LIST_HEAD(worker->temp_inactive_list + k); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7014 | } |
| 7015 | } |
| 7016 | |
| 7017 | return 0; |
| 7018 | } |
| 7019 | |
| 7020 | static void free_thread_groups(struct r5conf *conf) |
| 7021 | { |
| 7022 | if (conf->worker_groups) |
| 7023 | kfree(conf->worker_groups[0].workers); |
| 7024 | kfree(conf->worker_groups); |
| 7025 | conf->worker_groups = NULL; |
| 7026 | } |
| 7027 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7028 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7029 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7030 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7031 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7032 | |
| 7033 | if (!sectors) |
| 7034 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7035 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 7036 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7037 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7038 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7039 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
| 7040 | sectors &= ~((sector_t)conf->prev_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7041 | return sectors * (raid_disks - conf->max_degraded); |
| 7042 | } |
| 7043 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7044 | static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 7045 | { |
| 7046 | safe_put_page(percpu->spare_page); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7047 | percpu->spare_page = NULL; |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 7048 | kvfree(percpu->scribble); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7049 | percpu->scribble = NULL; |
| 7050 | } |
| 7051 | |
| 7052 | static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 7053 | { |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 7054 | if (conf->level == 6 && !percpu->spare_page) { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7055 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 7056 | if (!percpu->spare_page) |
| 7057 | return -ENOMEM; |
| 7058 | } |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7059 | |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 7060 | if (scribble_alloc(percpu, |
| 7061 | max(conf->raid_disks, |
| 7062 | conf->previous_raid_disks), |
| 7063 | max(conf->chunk_sectors, |
| 7064 | conf->prev_chunk_sectors) |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 7065 | / RAID5_STRIPE_SECTORS(conf))) { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7066 | free_scratch_buffer(conf, percpu); |
| 7067 | return -ENOMEM; |
| 7068 | } |
| 7069 | |
Davidlohr Bueso | 770b1d2 | 2021-11-15 17:23:17 -0800 | [diff] [blame] | 7070 | local_lock_init(&percpu->lock); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7071 | return 0; |
| 7072 | } |
| 7073 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7074 | static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node) |
| 7075 | { |
| 7076 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
| 7077 | |
| 7078 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); |
| 7079 | return 0; |
| 7080 | } |
| 7081 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7082 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7083 | { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7084 | if (!conf->percpu) |
| 7085 | return; |
| 7086 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7087 | cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7088 | free_percpu(conf->percpu); |
| 7089 | } |
| 7090 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7091 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7092 | { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 7093 | int i; |
| 7094 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7095 | log_exit(conf); |
| 7096 | |
Aliaksei Karaliou | 565e045 | 2017-12-23 21:20:31 +0300 | [diff] [blame] | 7097 | unregister_shrinker(&conf->shrinker); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7098 | free_thread_groups(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7099 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7100 | raid5_free_percpu(conf); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 7101 | for (i = 0; i < conf->pool_size; i++) |
| 7102 | if (conf->disks[i].extra_page) |
| 7103 | put_page(conf->disks[i].extra_page); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7104 | kfree(conf->disks); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 7105 | bioset_exit(&conf->bio_split); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7106 | kfree(conf->stripe_hashtbl); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 7107 | kfree(conf->pending_data); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7108 | kfree(conf); |
| 7109 | } |
| 7110 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7111 | 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] | 7112 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7113 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7114 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 7115 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7116 | if (alloc_scratch_buffer(conf, percpu)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7117 | pr_warn("%s: failed memory allocation for cpu%u\n", |
| 7118 | __func__, cpu); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7119 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7120 | } |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7121 | return 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7122 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7123 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7124 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7125 | { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7126 | int err = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7127 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7128 | conf->percpu = alloc_percpu(struct raid5_percpu); |
| 7129 | if (!conf->percpu) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7130 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7131 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7132 | err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 7133 | if (!err) { |
| 7134 | conf->scribble_disks = max(conf->raid_disks, |
| 7135 | conf->previous_raid_disks); |
| 7136 | conf->scribble_sectors = max(conf->chunk_sectors, |
| 7137 | conf->prev_chunk_sectors); |
| 7138 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7139 | return err; |
| 7140 | } |
| 7141 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7142 | static unsigned long raid5_cache_scan(struct shrinker *shrink, |
| 7143 | struct shrink_control *sc) |
| 7144 | { |
| 7145 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 7146 | unsigned long ret = SHRINK_STOP; |
| 7147 | |
| 7148 | if (mutex_trylock(&conf->cache_size_mutex)) { |
| 7149 | ret= 0; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 7150 | while (ret < sc->nr_to_scan && |
| 7151 | conf->max_nr_stripes > conf->min_nr_stripes) { |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 7152 | if (drop_one_stripe(conf) == 0) { |
| 7153 | ret = SHRINK_STOP; |
| 7154 | break; |
| 7155 | } |
| 7156 | ret++; |
| 7157 | } |
| 7158 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7159 | } |
| 7160 | return ret; |
| 7161 | } |
| 7162 | |
| 7163 | static unsigned long raid5_cache_count(struct shrinker *shrink, |
| 7164 | struct shrink_control *sc) |
| 7165 | { |
| 7166 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
| 7167 | |
| 7168 | if (conf->max_nr_stripes < conf->min_nr_stripes) |
| 7169 | /* unlikely, but not impossible */ |
| 7170 | return 0; |
| 7171 | return conf->max_nr_stripes - conf->min_nr_stripes; |
| 7172 | } |
| 7173 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7174 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7175 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7176 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7177 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7178 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7179 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7180 | char pers_name[6]; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7181 | int i; |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 7182 | int group_cnt; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 7183 | struct r5worker_group *new_group; |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 7184 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7185 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7186 | if (mddev->new_level != 5 |
| 7187 | && mddev->new_level != 4 |
| 7188 | && mddev->new_level != 6) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7189 | pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
| 7190 | mdname(mddev), mddev->new_level); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7191 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7192 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7193 | if ((mddev->new_level == 5 |
| 7194 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 7195 | (mddev->new_level == 6 |
| 7196 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7197 | pr_warn("md/raid:%s: layout %d not supported\n", |
| 7198 | mdname(mddev), mddev->new_layout); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7199 | return ERR_PTR(-EIO); |
| 7200 | } |
| 7201 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7202 | pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
| 7203 | mdname(mddev), mddev->raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7204 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 7205 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7207 | if (!mddev->new_chunk_sectors || |
| 7208 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 7209 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7210 | pr_warn("md/raid:%s: invalid chunk size %d\n", |
| 7211 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7212 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7213 | } |
| 7214 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7215 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7216 | if (conf == NULL) |
| 7217 | goto abort; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 7218 | |
Yufen Yu | e236858 | 2020-07-18 05:29:08 -0400 | [diff] [blame] | 7219 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 7220 | conf->stripe_size = DEFAULT_STRIPE_SIZE; |
| 7221 | conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9; |
| 7222 | conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9; |
| 7223 | #endif |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 7224 | INIT_LIST_HEAD(&conf->free_list); |
| 7225 | INIT_LIST_HEAD(&conf->pending_list); |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 7226 | conf->pending_data = kcalloc(PENDING_IO_MAX, |
| 7227 | sizeof(struct r5pending_data), |
| 7228 | GFP_KERNEL); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 7229 | if (!conf->pending_data) |
| 7230 | goto abort; |
| 7231 | for (i = 0; i < PENDING_IO_MAX; i++) |
| 7232 | list_add(&conf->pending_data[i].sibling, &conf->free_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7233 | /* Don't enable multi-threading by default*/ |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 7234 | if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) { |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 7235 | conf->group_cnt = group_cnt; |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 7236 | conf->worker_cnt_per_group = 0; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 7237 | conf->worker_groups = new_group; |
| 7238 | } else |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7239 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7240 | spin_lock_init(&conf->device_lock); |
Ahmed S. Darwish | 0a87b25 | 2020-07-20 17:55:25 +0200 | [diff] [blame] | 7241 | seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 7242 | mutex_init(&conf->cache_size_mutex); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 7243 | init_waitqueue_head(&conf->wait_for_quiescent); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 7244 | init_waitqueue_head(&conf->wait_for_stripe); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7245 | init_waitqueue_head(&conf->wait_for_overlap); |
| 7246 | INIT_LIST_HEAD(&conf->handle_list); |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 7247 | INIT_LIST_HEAD(&conf->loprio_list); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7248 | INIT_LIST_HEAD(&conf->hold_list); |
| 7249 | INIT_LIST_HEAD(&conf->delayed_list); |
| 7250 | INIT_LIST_HEAD(&conf->bitmap_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 7251 | init_llist_head(&conf->released_stripes); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7252 | atomic_set(&conf->active_stripes, 0); |
| 7253 | atomic_set(&conf->preread_active_stripes, 0); |
| 7254 | atomic_set(&conf->active_aligned_reads, 0); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 7255 | spin_lock_init(&conf->pending_bios_lock); |
| 7256 | conf->batch_bio_dispatch = true; |
| 7257 | rdev_for_each(rdev, mddev) { |
| 7258 | if (test_bit(Journal, &rdev->flags)) |
| 7259 | continue; |
| 7260 | if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 7261 | conf->batch_bio_dispatch = false; |
| 7262 | break; |
| 7263 | } |
| 7264 | } |
| 7265 | |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7266 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 7267 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7268 | |
| 7269 | conf->raid_disks = mddev->raid_disks; |
| 7270 | if (mddev->reshape_position == MaxSector) |
| 7271 | conf->previous_raid_disks = mddev->raid_disks; |
| 7272 | else |
| 7273 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7274 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7275 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 7276 | conf->disks = kcalloc(max_disks, sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7277 | GFP_KERNEL); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 7278 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7279 | if (!conf->disks) |
| 7280 | goto abort; |
| 7281 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 7282 | for (i = 0; i < max_disks; i++) { |
| 7283 | conf->disks[i].extra_page = alloc_page(GFP_KERNEL); |
| 7284 | if (!conf->disks[i].extra_page) |
| 7285 | goto abort; |
| 7286 | } |
| 7287 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 7288 | ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0); |
| 7289 | if (ret) |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 7290 | goto abort; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7291 | conf->mddev = mddev; |
| 7292 | |
| 7293 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 7294 | goto abort; |
| 7295 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7296 | /* We init hash_locks[0] separately to that it can be used |
| 7297 | * as the reference lock in the spin_lock_nest_lock() call |
| 7298 | * in lock_all_device_hash_locks_irq in order to convince |
| 7299 | * lockdep that we know what we are doing. |
| 7300 | */ |
| 7301 | spin_lock_init(conf->hash_locks); |
| 7302 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 7303 | spin_lock_init(conf->hash_locks + i); |
| 7304 | |
| 7305 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 7306 | INIT_LIST_HEAD(conf->inactive_list + i); |
| 7307 | |
| 7308 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 7309 | INIT_LIST_HEAD(conf->temp_inactive_list + i); |
| 7310 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 7311 | atomic_set(&conf->r5c_cached_full_stripes, 0); |
| 7312 | INIT_LIST_HEAD(&conf->r5c_full_stripe_list); |
| 7313 | atomic_set(&conf->r5c_cached_partial_stripes, 0); |
| 7314 | INIT_LIST_HEAD(&conf->r5c_partial_stripe_list); |
Shaohua Li | e33fbb9 | 2017-02-10 16:18:09 -0800 | [diff] [blame] | 7315 | atomic_set(&conf->r5c_flushing_full_stripes, 0); |
| 7316 | atomic_set(&conf->r5c_flushing_partial_stripes, 0); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 7317 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7318 | conf->level = mddev->new_level; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 7319 | conf->chunk_sectors = mddev->new_chunk_sectors; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7320 | if (raid5_alloc_percpu(conf) != 0) |
| 7321 | goto abort; |
| 7322 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7323 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7324 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7325 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7326 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7327 | if (raid_disk >= max_disks |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7328 | || raid_disk < 0 || test_bit(Journal, &rdev->flags)) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7329 | continue; |
| 7330 | disk = conf->disks + raid_disk; |
| 7331 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7332 | if (test_bit(Replacement, &rdev->flags)) { |
| 7333 | if (disk->replacement) |
| 7334 | goto abort; |
| 7335 | disk->replacement = rdev; |
| 7336 | } else { |
| 7337 | if (disk->rdev) |
| 7338 | goto abort; |
| 7339 | disk->rdev = rdev; |
| 7340 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7341 | |
| 7342 | if (test_bit(In_sync, &rdev->flags)) { |
| 7343 | char b[BDEVNAME_SIZE]; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7344 | pr_info("md/raid:%s: device %s operational as raid disk %d\n", |
| 7345 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 7346 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7347 | /* Cannot rely on bitmap to complete recovery */ |
| 7348 | conf->fullsync = 1; |
| 7349 | } |
| 7350 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7351 | conf->level = mddev->new_level; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7352 | if (conf->level == 6) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7353 | conf->max_degraded = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7354 | if (raid6_call.xor_syndrome) |
| 7355 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 7356 | else |
| 7357 | conf->rmw_level = PARITY_DISABLE_RMW; |
| 7358 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7359 | conf->max_degraded = 1; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7360 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 7361 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7362 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7363 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7364 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7365 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7366 | conf->prev_algo = mddev->layout; |
NeilBrown | 5cac6bc | 2015-07-17 12:17:50 +1000 | [diff] [blame] | 7367 | } else { |
| 7368 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 7369 | conf->prev_algo = conf->algorithm; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7370 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7371 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7372 | conf->min_nr_stripes = NR_STRIPES; |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 7373 | if (mddev->reshape_position != MaxSector) { |
| 7374 | int stripes = max_t(int, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 7375 | ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4, |
| 7376 | ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4); |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 7377 | conf->min_nr_stripes = max(NR_STRIPES, stripes); |
| 7378 | if (conf->min_nr_stripes != NR_STRIPES) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7379 | pr_info("md/raid:%s: force stripe size %d for reshape\n", |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 7380 | mdname(mddev), conf->min_nr_stripes); |
| 7381 | } |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7382 | memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7383 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7384 | atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7385 | if (grow_stripes(conf, conf->min_nr_stripes)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7386 | pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 7387 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7388 | goto abort; |
| 7389 | } else |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7390 | pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7391 | /* |
| 7392 | * Losing a stripe head costs more than the time to refill it, |
| 7393 | * it reduces the queue depth and so can hurt throughput. |
| 7394 | * So set it rather large, scaled by number of devices. |
| 7395 | */ |
| 7396 | conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4; |
| 7397 | conf->shrinker.scan_objects = raid5_cache_scan; |
| 7398 | conf->shrinker.count_objects = raid5_cache_count; |
| 7399 | conf->shrinker.batch = 128; |
| 7400 | conf->shrinker.flags = 0; |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 7401 | if (register_shrinker(&conf->shrinker)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7402 | pr_warn("md/raid:%s: couldn't register shrinker.\n", |
| 7403 | mdname(mddev)); |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 7404 | goto abort; |
| 7405 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7406 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7407 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 7408 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7409 | if (!conf->thread) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7410 | pr_warn("md/raid:%s: couldn't allocate thread.\n", |
| 7411 | mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7412 | goto abort; |
| 7413 | } |
| 7414 | |
| 7415 | return conf; |
| 7416 | |
| 7417 | abort: |
| 7418 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7419 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7420 | return ERR_PTR(-EIO); |
| 7421 | } else |
| 7422 | return ERR_PTR(-ENOMEM); |
| 7423 | } |
| 7424 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7425 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 7426 | { |
| 7427 | switch (algo) { |
| 7428 | case ALGORITHM_PARITY_0: |
| 7429 | if (raid_disk < max_degraded) |
| 7430 | return 1; |
| 7431 | break; |
| 7432 | case ALGORITHM_PARITY_N: |
| 7433 | if (raid_disk >= raid_disks - max_degraded) |
| 7434 | return 1; |
| 7435 | break; |
| 7436 | case ALGORITHM_PARITY_0_6: |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7437 | if (raid_disk == 0 || |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7438 | raid_disk == raid_disks - 1) |
| 7439 | return 1; |
| 7440 | break; |
| 7441 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 7442 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 7443 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 7444 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 7445 | if (raid_disk == raid_disks - 1) |
| 7446 | return 1; |
| 7447 | } |
| 7448 | return 0; |
| 7449 | } |
| 7450 | |
Christoph Hellwig | 16ef510 | 2020-09-24 08:51:33 +0200 | [diff] [blame] | 7451 | static void raid5_set_io_opt(struct r5conf *conf) |
| 7452 | { |
| 7453 | blk_queue_io_opt(conf->mddev->queue, (conf->chunk_sectors << 9) * |
| 7454 | (conf->raid_disks - conf->max_degraded)); |
| 7455 | } |
| 7456 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7457 | static int raid5_run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7458 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7459 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7460 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7461 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7462 | struct md_rdev *rdev; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7463 | struct md_rdev *journal_dev = NULL; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7464 | sector_t reshape_offset = 0; |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7465 | int i, ret = 0; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7466 | long long min_offset_diff = 0; |
| 7467 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7468 | |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7469 | if (acct_bioset_init(mddev)) { |
| 7470 | pr_err("md/raid456:%s: alloc acct bioset failed.\n", mdname(mddev)); |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 7471 | return -ENOMEM; |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7472 | } |
| 7473 | |
| 7474 | if (mddev_init_writes_pending(mddev) < 0) { |
| 7475 | ret = -ENOMEM; |
| 7476 | goto exit_acct_set; |
| 7477 | } |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 7478 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 7479 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7480 | pr_notice("md/raid:%s: not clean -- starting background reconstruction\n", |
| 7481 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7482 | |
| 7483 | rdev_for_each(rdev, mddev) { |
| 7484 | long long diff; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7485 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7486 | if (test_bit(Journal, &rdev->flags)) { |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7487 | journal_dev = rdev; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7488 | continue; |
| 7489 | } |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7490 | if (rdev->raid_disk < 0) |
| 7491 | continue; |
| 7492 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 7493 | if (first) { |
| 7494 | min_offset_diff = diff; |
| 7495 | first = 0; |
| 7496 | } else if (mddev->reshape_backwards && |
| 7497 | diff < min_offset_diff) |
| 7498 | min_offset_diff = diff; |
| 7499 | else if (!mddev->reshape_backwards && |
| 7500 | diff > min_offset_diff) |
| 7501 | min_offset_diff = diff; |
| 7502 | } |
| 7503 | |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 7504 | if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) && |
| 7505 | (mddev->bitmap_info.offset || mddev->bitmap_info.file)) { |
| 7506 | pr_notice("md/raid:%s: array cannot have both journal and bitmap\n", |
| 7507 | mdname(mddev)); |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7508 | ret = -EINVAL; |
| 7509 | goto exit_acct_set; |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 7510 | } |
| 7511 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7512 | if (mddev->reshape_position != MaxSector) { |
| 7513 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7514 | * Difficulties arise if the stripe we would write to |
| 7515 | * next is at or after the stripe we would read from next. |
| 7516 | * For a reshape that changes the number of devices, this |
| 7517 | * is only possible for a very short time, and mdadm makes |
| 7518 | * sure that time appears to have past before assembling |
| 7519 | * the array. So we fail if that time hasn't passed. |
| 7520 | * For a reshape that keeps the number of devices the same |
| 7521 | * mdadm must be monitoring the reshape can keeping the |
| 7522 | * critical areas read-only and backed up. It will start |
| 7523 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7524 | */ |
| 7525 | sector_t here_new, here_old; |
| 7526 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 7527 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7528 | int chunk_sectors; |
| 7529 | int new_data_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7530 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7531 | if (journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7532 | pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n", |
| 7533 | mdname(mddev)); |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7534 | ret = -EINVAL; |
| 7535 | goto exit_acct_set; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7536 | } |
| 7537 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7538 | if (mddev->new_level != mddev->level) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7539 | pr_warn("md/raid:%s: unsupported reshape required - aborting.\n", |
| 7540 | mdname(mddev)); |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7541 | ret = -EINVAL; |
| 7542 | goto exit_acct_set; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7543 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7544 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 7545 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7546 | * further up in new geometry must map after here in old |
| 7547 | * geometry. |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7548 | * If the chunk sizes are different, then as we perform reshape |
| 7549 | * in units of the largest of the two, reshape_position needs |
| 7550 | * be a multiple of the largest chunk size times new data disks. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7551 | */ |
| 7552 | here_new = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7553 | chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors); |
| 7554 | new_data_disks = mddev->raid_disks - max_degraded; |
| 7555 | if (sector_div(here_new, chunk_sectors * new_data_disks)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7556 | pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n", |
| 7557 | mdname(mddev)); |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7558 | ret = -EINVAL; |
| 7559 | goto exit_acct_set; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7560 | } |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7561 | reshape_offset = here_new * chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7562 | /* here_new is the stripe we will write to */ |
| 7563 | here_old = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7564 | sector_div(here_old, chunk_sectors * (old_disks-max_degraded)); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7565 | /* here_old is the first stripe that we might need to read |
| 7566 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7567 | if (mddev->delta_disks == 0) { |
| 7568 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7569 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7570 | * and taking constant backups. |
| 7571 | * mdadm always starts a situation like this in |
| 7572 | * readonly mode so it can take control before |
| 7573 | * allowing any writes. So just check for that. |
| 7574 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7575 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 7576 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 7577 | /* not really in-place - so OK */; |
| 7578 | else if (mddev->ro == 0) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7579 | pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n", |
| 7580 | mdname(mddev)); |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7581 | ret = -EINVAL; |
| 7582 | goto exit_acct_set; |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7583 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7584 | } else if (mddev->reshape_backwards |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7585 | ? (here_new * chunk_sectors + min_offset_diff <= |
| 7586 | here_old * chunk_sectors) |
| 7587 | : (here_new * chunk_sectors >= |
| 7588 | here_old * chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7589 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7590 | pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n", |
| 7591 | mdname(mddev)); |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7592 | ret = -EINVAL; |
| 7593 | goto exit_acct_set; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7594 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7595 | pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7596 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7597 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7598 | BUG_ON(mddev->level != mddev->new_level); |
| 7599 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7600 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7601 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7602 | } |
| 7603 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 7604 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && |
| 7605 | test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 7606 | pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n", |
| 7607 | mdname(mddev)); |
| 7608 | clear_bit(MD_HAS_PPL, &mddev->flags); |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 7609 | clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags); |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 7610 | } |
| 7611 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7612 | if (mddev->private == NULL) |
| 7613 | conf = setup_conf(mddev); |
| 7614 | else |
| 7615 | conf = mddev->private; |
| 7616 | |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7617 | if (IS_ERR(conf)) { |
| 7618 | ret = PTR_ERR(conf); |
| 7619 | goto exit_acct_set; |
| 7620 | } |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 7621 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 7622 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 7623 | if (!journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7624 | pr_warn("md/raid:%s: journal disk is missing, force array readonly\n", |
| 7625 | mdname(mddev)); |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 7626 | mddev->ro = 1; |
| 7627 | set_disk_ro(mddev->gendisk, 1); |
| 7628 | } else if (mddev->recovery_cp == MaxSector) |
| 7629 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
Shaohua Li | 7dde2ad | 2015-10-08 21:54:10 -0700 | [diff] [blame] | 7630 | } |
| 7631 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7632 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7633 | mddev->thread = conf->thread; |
| 7634 | conf->thread = NULL; |
| 7635 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7636 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7637 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 7638 | i++) { |
| 7639 | rdev = conf->disks[i].rdev; |
| 7640 | if (!rdev && conf->disks[i].replacement) { |
| 7641 | /* The replacement is all we have yet */ |
| 7642 | rdev = conf->disks[i].replacement; |
| 7643 | conf->disks[i].replacement = NULL; |
| 7644 | clear_bit(Replacement, &rdev->flags); |
| 7645 | conf->disks[i].rdev = rdev; |
| 7646 | } |
| 7647 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7648 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7649 | if (conf->disks[i].replacement && |
| 7650 | conf->reshape_progress != MaxSector) { |
| 7651 | /* replacements and reshape simply do not mix. */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7652 | pr_warn("md: cannot handle concurrent replacement and reshape.\n"); |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7653 | goto abort; |
| 7654 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 7655 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7656 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 7657 | continue; |
| 7658 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7659 | /* This disc is not fully in-sync. However if it |
| 7660 | * just stored parity (beyond the recovery_offset), |
| 7661 | * when we don't need to be concerned about the |
| 7662 | * array being dirty. |
| 7663 | * When reshape goes 'backwards', we never have |
| 7664 | * partially completed devices, so we only need |
| 7665 | * to worry about reshape going forwards. |
| 7666 | */ |
| 7667 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 7668 | if (mddev->major_version == 0 && |
| 7669 | mddev->minor_version > 90) |
| 7670 | rdev->recovery_offset = reshape_offset; |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7671 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7672 | if (rdev->recovery_offset < reshape_offset) { |
| 7673 | /* We need to check old and new layout */ |
| 7674 | if (!only_parity(rdev->raid_disk, |
| 7675 | conf->algorithm, |
| 7676 | conf->raid_disks, |
| 7677 | conf->max_degraded)) |
| 7678 | continue; |
| 7679 | } |
| 7680 | if (!only_parity(rdev->raid_disk, |
| 7681 | conf->prev_algo, |
| 7682 | conf->previous_raid_disks, |
| 7683 | conf->max_degraded)) |
| 7684 | continue; |
| 7685 | dirty_parity_disks++; |
| 7686 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7687 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7688 | /* |
| 7689 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 7690 | */ |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7691 | mddev->degraded = raid5_calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7692 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 7693 | if (has_failed(conf)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7694 | pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7695 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7696 | goto abort; |
| 7697 | } |
| 7698 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7699 | /* device size must be a multiple of chunk size */ |
Guoqing Jiang | c5eec74 | 2020-12-16 02:26:22 +0100 | [diff] [blame] | 7700 | mddev->dev_sectors &= ~((sector_t)mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7701 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 7702 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7703 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7704 | mddev->recovery_cp != MaxSector) { |
Artur Paszkiewicz | 4536bf9b | 2017-03-09 10:00:01 +0100 | [diff] [blame] | 7705 | if (test_bit(MD_HAS_PPL, &mddev->flags)) |
| 7706 | pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n", |
| 7707 | mdname(mddev)); |
| 7708 | else if (mddev->ok_start_degraded) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7709 | pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n", |
| 7710 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7711 | else { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7712 | pr_crit("md/raid:%s: cannot start dirty degraded array.\n", |
| 7713 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7714 | goto abort; |
| 7715 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7716 | } |
| 7717 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7718 | pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n", |
| 7719 | mdname(mddev), conf->level, |
| 7720 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 7721 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7722 | |
| 7723 | print_raid5_conf(conf); |
| 7724 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7725 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7726 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7727 | atomic_set(&conf->reshape_stripes, 0); |
| 7728 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7729 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7730 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7731 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7732 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7733 | "reshape"); |
Aditya Pakki | e406f12 | 2019-03-04 16:48:54 -0600 | [diff] [blame] | 7734 | if (!mddev->sync_thread) |
| 7735 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7736 | } |
| 7737 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7738 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7739 | if (mddev->to_remove == &raid5_attrs_group) |
| 7740 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7741 | else if (mddev->kobj.sd && |
| 7742 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7743 | pr_warn("raid5: failed to create sysfs attributes for %s\n", |
| 7744 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7745 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 7746 | |
| 7747 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7748 | int chunk_size; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7749 | /* read-ahead size must cover two whole stripes, which |
| 7750 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 7751 | * number of raid devices |
| 7752 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7753 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 7754 | int stripe = data_disks * |
| 7755 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7756 | |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7757 | chunk_size = mddev->chunk_sectors << 9; |
| 7758 | blk_queue_io_min(mddev->queue, chunk_size); |
Christoph Hellwig | 16ef510 | 2020-09-24 08:51:33 +0200 | [diff] [blame] | 7759 | raid5_set_io_opt(conf); |
Kent Overstreet | c78afc6 | 2013-07-11 22:39:53 -0700 | [diff] [blame] | 7760 | mddev->queue->limits.raid_partial_stripes_expensive = 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7761 | /* |
| 7762 | * We can only discard a whole stripe. It doesn't make sense to |
| 7763 | * discard data disk but write parity disk |
| 7764 | */ |
| 7765 | stripe = stripe * PAGE_SIZE; |
Guoqing Jiang | c6efe43 | 2021-10-04 23:34:52 +0800 | [diff] [blame] | 7766 | stripe = roundup_pow_of_two(stripe); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7767 | mddev->queue->limits.discard_alignment = stripe; |
| 7768 | mddev->queue->limits.discard_granularity = stripe; |
Konstantin Khlebnikov | e8d7c33 | 2016-11-27 19:32:32 +0300 | [diff] [blame] | 7769 | |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7770 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
Christoph Hellwig | 3deff1a | 2017-04-05 19:21:03 +0200 | [diff] [blame] | 7771 | blk_queue_max_write_zeroes_sectors(mddev->queue, 0); |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7772 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7773 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7774 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7775 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7776 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7777 | rdev->new_data_offset << 9); |
| 7778 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7779 | |
Christoph Hellwig | 48920ff | 2017-04-05 19:21:23 +0200 | [diff] [blame] | 7780 | /* |
| 7781 | * zeroing is required, otherwise data |
| 7782 | * could be lost. Consider a scenario: discard a stripe |
| 7783 | * (the stripe could be inconsistent if |
| 7784 | * discard_zeroes_data is 0); write one disk of the |
| 7785 | * stripe (the stripe could be inconsistent again |
| 7786 | * depending on which disks are used to calculate |
| 7787 | * parity); the disk is broken; The stripe data of this |
| 7788 | * disk is lost. |
| 7789 | * |
| 7790 | * We only allow DISCARD if the sysadmin has confirmed that |
| 7791 | * only safe devices are in use by setting a module parameter. |
| 7792 | * A better idea might be to turn DISCARD into WRITE_ZEROES |
| 7793 | * requests, as that is required to be safe. |
| 7794 | */ |
| 7795 | if (devices_handle_discard_safely && |
Jes Sorensen | e7597e6 | 2016-02-16 16:44:24 -0500 | [diff] [blame] | 7796 | mddev->queue->limits.max_discard_sectors >= (stripe >> 9) && |
| 7797 | mddev->queue->limits.discard_granularity >= stripe) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 7798 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7799 | mddev->queue); |
| 7800 | else |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 7801 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7802 | mddev->queue); |
Shaohua Li | 1dffddd | 2016-09-08 10:49:06 -0700 | [diff] [blame] | 7803 | |
| 7804 | blk_queue_max_hw_sectors(mddev->queue, UINT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7805 | } |
| 7806 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 7807 | if (log_init(conf, journal_dev, raid5_has_ppl(conf))) |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7808 | goto abort; |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 7809 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7810 | return 0; |
| 7811 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7812 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 7813 | print_raid5_conf(conf); |
| 7814 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7815 | mddev->private = NULL; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7816 | pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev)); |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7817 | ret = -EIO; |
| 7818 | exit_acct_set: |
| 7819 | acct_bioset_exit(mddev); |
| 7820 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7821 | } |
| 7822 | |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7823 | static void raid5_free(struct mddev *mddev, void *priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7824 | { |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7825 | struct r5conf *conf = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7826 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7827 | free_conf(conf); |
Xiao Ni | 0c031fd | 2021-12-10 17:31:15 +0800 | [diff] [blame] | 7828 | acct_bioset_exit(mddev); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7829 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7830 | } |
| 7831 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7832 | static void raid5_status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7833 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7834 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7835 | int i; |
| 7836 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7837 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7838 | conf->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7839 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
NeilBrown | 5fd1335 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 7840 | rcu_read_lock(); |
| 7841 | for (i = 0; i < conf->raid_disks; i++) { |
| 7842 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
| 7843 | seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 7844 | } |
| 7845 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7846 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7847 | } |
| 7848 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7849 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7850 | { |
| 7851 | int i; |
| 7852 | struct disk_info *tmp; |
| 7853 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7854 | pr_debug("RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7855 | if (!conf) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7856 | pr_debug("(conf==NULL)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7857 | return; |
| 7858 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7859 | pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level, |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7860 | conf->raid_disks, |
| 7861 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7862 | |
| 7863 | for (i = 0; i < conf->raid_disks; i++) { |
| 7864 | char b[BDEVNAME_SIZE]; |
| 7865 | tmp = conf->disks + i; |
| 7866 | if (tmp->rdev) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7867 | pr_debug(" disk %d, o:%d, dev:%s\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7868 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 7869 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7870 | } |
| 7871 | } |
| 7872 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7873 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7874 | { |
| 7875 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7876 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7877 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7878 | int count = 0; |
| 7879 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7880 | |
| 7881 | for (i = 0; i < conf->raid_disks; i++) { |
| 7882 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7883 | if (tmp->replacement |
| 7884 | && tmp->replacement->recovery_offset == MaxSector |
| 7885 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 7886 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 7887 | /* Replacement has just become active. */ |
| 7888 | if (!tmp->rdev |
| 7889 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 7890 | count++; |
| 7891 | if (tmp->rdev) { |
| 7892 | /* Replaced device not technically faulty, |
| 7893 | * but we need to be sure it gets removed |
| 7894 | * and never re-added. |
| 7895 | */ |
| 7896 | set_bit(Faulty, &tmp->rdev->flags); |
| 7897 | sysfs_notify_dirent_safe( |
| 7898 | tmp->rdev->sysfs_state); |
| 7899 | } |
| 7900 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 7901 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7902 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7903 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 7904 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7905 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7906 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7907 | } |
| 7908 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7909 | spin_lock_irqsave(&conf->device_lock, flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7910 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7911 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7912 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7913 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7914 | } |
| 7915 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7916 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7917 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7918 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7919 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7920 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7921 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7922 | struct disk_info *p = conf->disks + number; |
| 7923 | |
| 7924 | print_raid5_conf(conf); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7925 | if (test_bit(Journal, &rdev->flags) && conf->log) { |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7926 | /* |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7927 | * we can't wait pending write here, as this is called in |
| 7928 | * raid5d, wait will deadlock. |
NeilBrown | 84dd97a | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7929 | * neilb: there is no locking about new writes here, |
| 7930 | * so this cannot be safe. |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7931 | */ |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 7932 | if (atomic_read(&conf->active_stripes) || |
| 7933 | atomic_read(&conf->r5c_cached_full_stripes) || |
| 7934 | atomic_read(&conf->r5c_cached_partial_stripes)) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7935 | return -EBUSY; |
NeilBrown | 84dd97a | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7936 | } |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7937 | log_exit(conf); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7938 | return 0; |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7939 | } |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7940 | if (rdev == p->rdev) |
| 7941 | rdevp = &p->rdev; |
| 7942 | else if (rdev == p->replacement) |
| 7943 | rdevp = &p->replacement; |
| 7944 | else |
| 7945 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7946 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7947 | if (number >= conf->raid_disks && |
| 7948 | conf->reshape_progress == MaxSector) |
| 7949 | clear_bit(In_sync, &rdev->flags); |
| 7950 | |
| 7951 | if (test_bit(In_sync, &rdev->flags) || |
| 7952 | atomic_read(&rdev->nr_pending)) { |
| 7953 | err = -EBUSY; |
| 7954 | goto abort; |
| 7955 | } |
| 7956 | /* Only remove non-faulty devices if recovery |
| 7957 | * isn't possible. |
| 7958 | */ |
| 7959 | if (!test_bit(Faulty, &rdev->flags) && |
| 7960 | mddev->recovery_disabled != conf->recovery_disabled && |
| 7961 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7962 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7963 | number < conf->raid_disks) { |
| 7964 | err = -EBUSY; |
| 7965 | goto abort; |
| 7966 | } |
| 7967 | *rdevp = NULL; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 7968 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
| 7969 | synchronize_rcu(); |
| 7970 | if (atomic_read(&rdev->nr_pending)) { |
| 7971 | /* lost the race, try later */ |
| 7972 | err = -EBUSY; |
| 7973 | *rdevp = rdev; |
| 7974 | } |
| 7975 | } |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 7976 | if (!err) { |
| 7977 | err = log_modify(conf, rdev, false); |
| 7978 | if (err) |
| 7979 | goto abort; |
| 7980 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 7981 | if (p->replacement) { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7982 | /* We must have just cleared 'rdev' */ |
| 7983 | p->rdev = p->replacement; |
| 7984 | clear_bit(Replacement, &p->replacement->flags); |
| 7985 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 7986 | * but will never see neither - if they are careful |
| 7987 | */ |
| 7988 | p->replacement = NULL; |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 7989 | |
| 7990 | if (!err) |
| 7991 | err = log_modify(conf, p->rdev, true); |
Guoqing Jiang | e5bc9c3 | 2017-04-24 15:58:04 +0800 | [diff] [blame] | 7992 | } |
| 7993 | |
| 7994 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7995 | abort: |
| 7996 | |
| 7997 | print_raid5_conf(conf); |
| 7998 | return err; |
| 7999 | } |
| 8000 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8001 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8002 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8003 | struct r5conf *conf = mddev->private; |
Xiao Ni | d9771f5 | 2019-06-14 15:41:05 -0700 | [diff] [blame] | 8004 | int ret, err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8005 | int disk; |
| 8006 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 8007 | int first = 0; |
| 8008 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8009 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8010 | if (test_bit(Journal, &rdev->flags)) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8011 | if (conf->log) |
| 8012 | return -EBUSY; |
| 8013 | |
| 8014 | rdev->raid_disk = 0; |
| 8015 | /* |
| 8016 | * The array is in readonly mode if journal is missing, so no |
| 8017 | * write requests running. We should be safe |
| 8018 | */ |
Xiao Ni | d9771f5 | 2019-06-14 15:41:05 -0700 | [diff] [blame] | 8019 | ret = log_init(conf, rdev, false); |
| 8020 | if (ret) |
| 8021 | return ret; |
| 8022 | |
| 8023 | ret = r5l_start(conf->log); |
| 8024 | if (ret) |
| 8025 | return ret; |
| 8026 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 8027 | return 0; |
| 8028 | } |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 8029 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 8030 | return -EBUSY; |
| 8031 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 8032 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8033 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 8034 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8035 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 8036 | if (rdev->raid_disk >= 0) |
| 8037 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8038 | |
| 8039 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8040 | * find the disk ... but prefer rdev->saved_raid_disk |
| 8041 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8042 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8043 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 8044 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8045 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 8046 | first = rdev->saved_raid_disk; |
| 8047 | |
| 8048 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8049 | p = conf->disks + disk; |
| 8050 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 8051 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8052 | rdev->raid_disk = disk; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8053 | if (rdev->saved_raid_disk != disk) |
| 8054 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 8055 | rcu_assign_pointer(p->rdev, rdev); |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 8056 | |
| 8057 | err = log_modify(conf, rdev, true); |
| 8058 | |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 8059 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8060 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 8061 | } |
| 8062 | for (disk = first; disk <= last; disk++) { |
| 8063 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 8064 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 8065 | p->replacement == NULL) { |
| 8066 | clear_bit(In_sync, &rdev->flags); |
| 8067 | set_bit(Replacement, &rdev->flags); |
| 8068 | rdev->raid_disk = disk; |
| 8069 | err = 0; |
| 8070 | conf->fullsync = 1; |
| 8071 | rcu_assign_pointer(p->replacement, rdev); |
| 8072 | break; |
| 8073 | } |
| 8074 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 8075 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8076 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 8077 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8078 | } |
| 8079 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8080 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8081 | { |
| 8082 | /* no resync is happening, and there is enough space |
| 8083 | * on all devices, so we can resize. |
| 8084 | * We need to make sure resync covers any new space. |
| 8085 | * If the array is shrinking we should possibly wait until |
| 8086 | * any io in the removed space completes, but it hardly seems |
| 8087 | * worth it. |
| 8088 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 8089 | sector_t newsize; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 8090 | struct r5conf *conf = mddev->private; |
| 8091 | |
Shaohua Li | e254de6 | 2018-08-29 11:05:42 -0700 | [diff] [blame] | 8092 | if (raid5_has_log(conf) || raid5_has_ppl(conf)) |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 8093 | return -EINVAL; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 8094 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 8095 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 8096 | if (mddev->external_size && |
| 8097 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 8098 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 8099 | if (mddev->bitmap) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 8100 | int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 8101 | if (ret) |
| 8102 | return ret; |
| 8103 | } |
| 8104 | md_set_array_sectors(mddev, newsize); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 8105 | if (sectors > mddev->dev_sectors && |
| 8106 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8107 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8108 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8109 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8110 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 8111 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8112 | return 0; |
| 8113 | } |
| 8114 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8115 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 8116 | { |
| 8117 | /* Can only proceed if there are plenty of stripe_heads. |
| 8118 | * We need a minimum of one full stripe,, and for sensible progress |
| 8119 | * it is best to have about 4 times that. |
| 8120 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 8121 | * allow for chunk sizes up to 256K, which is probably OK. |
| 8122 | * If the chunk size is greater, user-space should request more |
| 8123 | * stripe_heads first. |
| 8124 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8125 | struct r5conf *conf = mddev->private; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 8126 | if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 8127 | > conf->min_nr_stripes || |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 8128 | ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 8129 | > conf->min_nr_stripes) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 8130 | pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 8131 | mdname(mddev), |
| 8132 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 8133 | / RAID5_STRIPE_SIZE(conf))*4); |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 8134 | return 0; |
| 8135 | } |
| 8136 | return 1; |
| 8137 | } |
| 8138 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8139 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8140 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8141 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8142 | |
Shaohua Li | e254de6 | 2018-08-29 11:05:42 -0700 | [diff] [blame] | 8143 | if (raid5_has_log(conf) || raid5_has_ppl(conf)) |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 8144 | return -EINVAL; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8145 | if (mddev->delta_disks == 0 && |
| 8146 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 8147 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8148 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 8149 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8150 | return -EINVAL; |
NeilBrown | fdcfbbb | 2013-07-04 16:38:16 +1000 | [diff] [blame] | 8151 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8152 | /* We might be able to shrink, but the devices must |
| 8153 | * be made bigger first. |
| 8154 | * For raid6, 4 is the minimum size. |
| 8155 | * Otherwise 2 is the minimum |
| 8156 | */ |
| 8157 | int min = 2; |
| 8158 | if (mddev->level == 6) |
| 8159 | min = 4; |
| 8160 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 8161 | return -EINVAL; |
| 8162 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8163 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 8164 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8165 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8166 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 8167 | if (mddev->new_chunk_sectors > mddev->chunk_sectors || |
| 8168 | mddev->delta_disks > 0) |
| 8169 | if (resize_chunks(conf, |
| 8170 | conf->previous_raid_disks |
| 8171 | + max(0, mddev->delta_disks), |
| 8172 | max(mddev->new_chunk_sectors, |
| 8173 | mddev->chunk_sectors) |
| 8174 | ) < 0) |
| 8175 | return -ENOMEM; |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 8176 | |
| 8177 | if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size) |
| 8178 | return 0; /* never bother to shrink */ |
NeilBrown | e56108d6 | 2012-10-11 14:24:13 +1100 | [diff] [blame] | 8179 | return resize_stripes(conf, (conf->previous_raid_disks |
| 8180 | + mddev->delta_disks)); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8181 | } |
| 8182 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8183 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8184 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8185 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8186 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8187 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 8188 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8189 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 8190 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8191 | return -EBUSY; |
| 8192 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 8193 | if (!check_stripe_cache(mddev)) |
| 8194 | return -ENOSPC; |
| 8195 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 8196 | if (has_failed(conf)) |
| 8197 | return -EINVAL; |
| 8198 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8199 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8200 | if (!test_bit(In_sync, &rdev->flags) |
| 8201 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8202 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8203 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8204 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 8205 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8206 | /* Not enough devices even to make a degraded array |
| 8207 | * of that size |
| 8208 | */ |
| 8209 | return -EINVAL; |
| 8210 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8211 | /* Refuse to reduce size of the array. Any reductions in |
| 8212 | * array size must be through explicit setting of array_size |
| 8213 | * attribute. |
| 8214 | */ |
| 8215 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 8216 | < mddev->array_sectors) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 8217 | pr_warn("md/raid:%s: array size must be reduced before number of disks\n", |
| 8218 | mdname(mddev)); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8219 | return -EINVAL; |
| 8220 | } |
| 8221 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8222 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8223 | spin_lock_irq(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 8224 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8225 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8226 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 8227 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 8228 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8229 | conf->prev_algo = conf->algorithm; |
| 8230 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8231 | conf->generation++; |
| 8232 | /* Code that selects data_offset needs to see the generation update |
| 8233 | * if reshape_progress has been set - so a memory barrier needed. |
| 8234 | */ |
| 8235 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8236 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 8237 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 8238 | else |
| 8239 | conf->reshape_progress = 0; |
| 8240 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 8241 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8242 | spin_unlock_irq(&conf->device_lock); |
| 8243 | |
NeilBrown | 4d77e3b | 2013-08-27 15:57:47 +1000 | [diff] [blame] | 8244 | /* Now make sure any requests that proceeded on the assumption |
| 8245 | * the reshape wasn't running - like Discard or Read - have |
| 8246 | * completed. |
| 8247 | */ |
| 8248 | mddev_suspend(mddev); |
| 8249 | mddev_resume(mddev); |
| 8250 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8251 | /* Add some new drives, as many as will fit. |
| 8252 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 8253 | * Don't add devices if we are reducing the number of |
| 8254 | * devices in the array. This is because it is not possible |
| 8255 | * to correctly record the "partially reconstructed" state of |
| 8256 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8257 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8258 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8259 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8260 | if (rdev->raid_disk < 0 && |
| 8261 | !test_bit(Faulty, &rdev->flags)) { |
| 8262 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8263 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 8264 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8265 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 8266 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8267 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8268 | |
Damien Le Moal | 2aada5b | 2020-07-16 13:54:42 +0900 | [diff] [blame] | 8269 | /* Failure here is OK */ |
| 8270 | sysfs_link_rdev(mddev, rdev); |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8271 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8272 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 8273 | && !test_bit(Faulty, &rdev->flags)) { |
| 8274 | /* This is a spare that was manually added */ |
| 8275 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8276 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8277 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8278 | /* When a reshape changes the number of devices, |
| 8279 | * ->degraded is measured against the larger of the |
| 8280 | * pre and post number of devices. |
| 8281 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8282 | spin_lock_irqsave(&conf->device_lock, flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 8283 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8284 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 8285 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8286 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 8287 | mddev->reshape_position = conf->reshape_progress; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8288 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8289 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8290 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8291 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8292 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8293 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8294 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 8295 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 8296 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8297 | if (!mddev->sync_thread) { |
| 8298 | mddev->recovery = 0; |
| 8299 | spin_lock_irq(&conf->device_lock); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 8300 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8301 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 8302 | mddev->new_chunk_sectors = |
| 8303 | conf->chunk_sectors = conf->prev_chunk_sectors; |
| 8304 | mddev->new_layout = conf->algorithm = conf->prev_algo; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8305 | rdev_for_each(rdev, mddev) |
| 8306 | rdev->new_data_offset = rdev->data_offset; |
| 8307 | smp_wmb(); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 8308 | conf->generation --; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 8309 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 8310 | mddev->reshape_position = MaxSector; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 8311 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8312 | spin_unlock_irq(&conf->device_lock); |
| 8313 | return -EAGAIN; |
| 8314 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 8315 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8316 | md_wakeup_thread(mddev->sync_thread); |
Guoqing Jiang | 5467948 | 2021-10-04 23:34:53 +0800 | [diff] [blame] | 8317 | md_new_event(); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8318 | return 0; |
| 8319 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8320 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8321 | /* This is called from the reshape thread and should make any |
| 8322 | * changes needed in 'conf' |
| 8323 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8324 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8325 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8326 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8327 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 8328 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8329 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8330 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8331 | conf->previous_raid_disks = conf->raid_disks; |
Xiao Ni | b5d2771 | 2017-07-05 17:34:04 +0800 | [diff] [blame] | 8332 | md_finish_reshape(conf->mddev); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8333 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 8334 | conf->reshape_progress = MaxSector; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 8335 | conf->mddev->reshape_position = MaxSector; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 8336 | rdev_for_each(rdev, conf->mddev) |
| 8337 | if (rdev->raid_disk >= 0 && |
| 8338 | !test_bit(Journal, &rdev->flags) && |
| 8339 | !test_bit(In_sync, &rdev->flags)) |
| 8340 | rdev->recovery_offset = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8341 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 8342 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8343 | |
Christoph Hellwig | c2e4cd5 | 2020-09-24 08:51:34 +0200 | [diff] [blame] | 8344 | if (conf->mddev->queue) |
Christoph Hellwig | 16ef510 | 2020-09-24 08:51:33 +0200 | [diff] [blame] | 8345 | raid5_set_io_opt(conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8346 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8347 | } |
| 8348 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8349 | /* This is called from the raid5d thread with mddev_lock held. |
| 8350 | * It makes config changes to the device. |
| 8351 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8352 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8353 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8354 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8355 | |
| 8356 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8357 | |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 8358 | if (mddev->delta_disks <= 0) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8359 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 8360 | spin_lock_irq(&conf->device_lock); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 8361 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 8362 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8363 | for (d = conf->raid_disks ; |
| 8364 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 8365 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8366 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 8367 | if (rdev) |
| 8368 | clear_bit(In_sync, &rdev->flags); |
| 8369 | rdev = conf->disks[d].replacement; |
| 8370 | if (rdev) |
| 8371 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 8372 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8373 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8374 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 8375 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8376 | mddev->reshape_position = MaxSector; |
| 8377 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8378 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8379 | } |
| 8380 | } |
| 8381 | |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 8382 | static void raid5_quiesce(struct mddev *mddev, int quiesce) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8383 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8384 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8385 | |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 8386 | if (quiesce) { |
| 8387 | /* stop all writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8388 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8389 | /* '2' tells resync/reshape to pause so that all |
| 8390 | * active stripes can drain |
| 8391 | */ |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 8392 | r5c_flush_cache(conf, INT_MAX); |
Gal Ofri | 97ae272 | 2021-06-07 14:07:03 +0300 | [diff] [blame] | 8393 | /* need a memory barrier to make sure read_one_chunk() sees |
| 8394 | * quiesce started and reverts to slow (locked) path. |
| 8395 | */ |
| 8396 | smp_store_release(&conf->quiesce, 2); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 8397 | wait_event_cmd(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 8398 | atomic_read(&conf->active_stripes) == 0 && |
| 8399 | atomic_read(&conf->active_aligned_reads) == 0, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8400 | unlock_all_device_hash_locks_irq(conf), |
| 8401 | lock_all_device_hash_locks_irq(conf)); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8402 | conf->quiesce = 1; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8403 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8404 | /* allow reshape to continue */ |
| 8405 | wake_up(&conf->wait_for_overlap); |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 8406 | } else { |
| 8407 | /* re-enable writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8408 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8409 | conf->quiesce = 0; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 8410 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 8411 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8412 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8413 | } |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 8414 | log_quiesce(conf, quiesce); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8415 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 8416 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8417 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8418 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8419 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 8420 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8421 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8422 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8423 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 8424 | pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 8425 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8426 | return ERR_PTR(-EINVAL); |
| 8427 | } |
| 8428 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8429 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 8430 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 8431 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8432 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8433 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 8434 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 8435 | mddev->raid_disks += 1; |
| 8436 | mddev->delta_disks = 1; |
| 8437 | /* make sure it will be not marked as dirty */ |
| 8438 | mddev->recovery_cp = MaxSector; |
| 8439 | |
| 8440 | return setup_conf(mddev); |
| 8441 | } |
| 8442 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8443 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8444 | { |
| 8445 | int chunksect; |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8446 | void *ret; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8447 | |
| 8448 | if (mddev->raid_disks != 2 || |
| 8449 | mddev->degraded > 1) |
| 8450 | return ERR_PTR(-EINVAL); |
| 8451 | |
| 8452 | /* Should check if there are write-behind devices? */ |
| 8453 | |
| 8454 | chunksect = 64*2; /* 64K by default */ |
| 8455 | |
| 8456 | /* The array must be an exact multiple of chunksize */ |
| 8457 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 8458 | chunksect >>= 1; |
| 8459 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 8460 | if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private)) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8461 | /* array size does not allow a suitable chunk size */ |
| 8462 | return ERR_PTR(-EINVAL); |
| 8463 | |
| 8464 | mddev->new_level = 5; |
| 8465 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 8466 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8467 | |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8468 | ret = setup_conf(mddev); |
Jes Sorensen | 32cd7cb | 2017-01-06 19:31:35 -0500 | [diff] [blame] | 8469 | if (!IS_ERR(ret)) |
Shaohua Li | 394ed8e | 2017-01-04 16:10:19 -0800 | [diff] [blame] | 8470 | mddev_clear_unsupported_flags(mddev, |
| 8471 | UNSUPPORTED_MDDEV_FLAGS); |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8472 | return ret; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8473 | } |
| 8474 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8475 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 8476 | { |
| 8477 | int new_layout; |
| 8478 | |
| 8479 | switch (mddev->layout) { |
| 8480 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 8481 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 8482 | break; |
| 8483 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 8484 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 8485 | break; |
| 8486 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 8487 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 8488 | break; |
| 8489 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 8490 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 8491 | break; |
| 8492 | case ALGORITHM_PARITY_0_6: |
| 8493 | new_layout = ALGORITHM_PARITY_0; |
| 8494 | break; |
| 8495 | case ALGORITHM_PARITY_N: |
| 8496 | new_layout = ALGORITHM_PARITY_N; |
| 8497 | break; |
| 8498 | default: |
| 8499 | return ERR_PTR(-EINVAL); |
| 8500 | } |
| 8501 | mddev->new_level = 5; |
| 8502 | mddev->new_layout = new_layout; |
| 8503 | mddev->delta_disks = -1; |
| 8504 | mddev->raid_disks -= 1; |
| 8505 | return setup_conf(mddev); |
| 8506 | } |
| 8507 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8508 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8509 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8510 | /* For a 2-drive array, the layout and chunk size can be changed |
| 8511 | * immediately as not restriping is needed. |
| 8512 | * For larger arrays we record the new value - after validation |
| 8513 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8514 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8515 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8516 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8517 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8518 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8519 | return -EINVAL; |
| 8520 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 8521 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8522 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8523 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8524 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8525 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8526 | /* not factor of array size */ |
| 8527 | return -EINVAL; |
| 8528 | } |
| 8529 | |
| 8530 | /* They look valid */ |
| 8531 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8532 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8533 | /* can make the change immediately */ |
| 8534 | if (mddev->new_layout >= 0) { |
| 8535 | conf->algorithm = mddev->new_layout; |
| 8536 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8537 | } |
| 8538 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8539 | conf->chunk_sectors = new_chunk ; |
| 8540 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8541 | } |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8542 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8543 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8544 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8545 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8546 | } |
| 8547 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8548 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8549 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8550 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8551 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8552 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8553 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8554 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 8555 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8556 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8557 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8558 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8559 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8560 | /* not factor of array size */ |
| 8561 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8562 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8563 | |
| 8564 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8565 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8566 | } |
| 8567 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8568 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8569 | { |
| 8570 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8571 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8572 | * raid1 - if there are two drives. We need to know the chunk size |
| 8573 | * raid4 - trivial - just use a raid4 layout. |
| 8574 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8575 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8576 | if (mddev->level == 0) |
| 8577 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8578 | if (mddev->level == 1) |
| 8579 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 8580 | if (mddev->level == 4) { |
| 8581 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 8582 | mddev->new_level = 5; |
| 8583 | return setup_conf(mddev); |
| 8584 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 8585 | if (mddev->level == 6) |
| 8586 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8587 | |
| 8588 | return ERR_PTR(-EINVAL); |
| 8589 | } |
| 8590 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8591 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8592 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8593 | /* raid4 can take over: |
| 8594 | * raid0 - if there is only one strip zone |
| 8595 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8596 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8597 | if (mddev->level == 0) |
| 8598 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8599 | if (mddev->level == 5 && |
| 8600 | mddev->layout == ALGORITHM_PARITY_N) { |
| 8601 | mddev->new_layout = 0; |
| 8602 | mddev->new_level = 4; |
| 8603 | return setup_conf(mddev); |
| 8604 | } |
| 8605 | return ERR_PTR(-EINVAL); |
| 8606 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8607 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8608 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8609 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8610 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8611 | { |
| 8612 | /* Currently can only take over a raid5. We map the |
| 8613 | * personality to an equivalent raid6 personality |
| 8614 | * with the Q block at the end. |
| 8615 | */ |
| 8616 | int new_layout; |
| 8617 | |
| 8618 | if (mddev->pers != &raid5_personality) |
| 8619 | return ERR_PTR(-EINVAL); |
| 8620 | if (mddev->degraded > 1) |
| 8621 | return ERR_PTR(-EINVAL); |
| 8622 | if (mddev->raid_disks > 253) |
| 8623 | return ERR_PTR(-EINVAL); |
| 8624 | if (mddev->raid_disks < 3) |
| 8625 | return ERR_PTR(-EINVAL); |
| 8626 | |
| 8627 | switch (mddev->layout) { |
| 8628 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 8629 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 8630 | break; |
| 8631 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 8632 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 8633 | break; |
| 8634 | case ALGORITHM_LEFT_SYMMETRIC: |
| 8635 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 8636 | break; |
| 8637 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 8638 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 8639 | break; |
| 8640 | case ALGORITHM_PARITY_0: |
| 8641 | new_layout = ALGORITHM_PARITY_0_6; |
| 8642 | break; |
| 8643 | case ALGORITHM_PARITY_N: |
| 8644 | new_layout = ALGORITHM_PARITY_N; |
| 8645 | break; |
| 8646 | default: |
| 8647 | return ERR_PTR(-EINVAL); |
| 8648 | } |
| 8649 | mddev->new_level = 6; |
| 8650 | mddev->new_layout = new_layout; |
| 8651 | mddev->delta_disks = 1; |
| 8652 | mddev->raid_disks += 1; |
| 8653 | return setup_conf(mddev); |
| 8654 | } |
| 8655 | |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 8656 | static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf) |
| 8657 | { |
| 8658 | struct r5conf *conf; |
| 8659 | int err; |
| 8660 | |
| 8661 | err = mddev_lock(mddev); |
| 8662 | if (err) |
| 8663 | return err; |
| 8664 | conf = mddev->private; |
| 8665 | if (!conf) { |
| 8666 | mddev_unlock(mddev); |
| 8667 | return -ENODEV; |
| 8668 | } |
| 8669 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 8670 | if (strncmp(buf, "ppl", 3) == 0) { |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8671 | /* ppl only works with RAID 5 */ |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 8672 | if (!raid5_has_ppl(conf) && conf->level == 5) { |
| 8673 | err = log_init(conf, NULL, true); |
| 8674 | if (!err) { |
| 8675 | err = resize_stripes(conf, conf->pool_size); |
| 8676 | if (err) |
| 8677 | log_exit(conf); |
| 8678 | } |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8679 | } else |
| 8680 | err = -EINVAL; |
| 8681 | } else if (strncmp(buf, "resync", 6) == 0) { |
| 8682 | if (raid5_has_ppl(conf)) { |
| 8683 | mddev_suspend(mddev); |
| 8684 | log_exit(conf); |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8685 | mddev_resume(mddev); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 8686 | err = resize_stripes(conf, conf->pool_size); |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8687 | } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) && |
| 8688 | r5l_log_disk_error(conf)) { |
| 8689 | bool journal_dev_exists = false; |
| 8690 | struct md_rdev *rdev; |
| 8691 | |
| 8692 | rdev_for_each(rdev, mddev) |
| 8693 | if (test_bit(Journal, &rdev->flags)) { |
| 8694 | journal_dev_exists = true; |
| 8695 | break; |
| 8696 | } |
| 8697 | |
| 8698 | if (!journal_dev_exists) { |
| 8699 | mddev_suspend(mddev); |
| 8700 | clear_bit(MD_HAS_JOURNAL, &mddev->flags); |
| 8701 | mddev_resume(mddev); |
| 8702 | } else /* need remove journal device first */ |
| 8703 | err = -EBUSY; |
| 8704 | } else |
| 8705 | err = -EINVAL; |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 8706 | } else { |
| 8707 | err = -EINVAL; |
| 8708 | } |
| 8709 | |
| 8710 | if (!err) |
| 8711 | md_update_sb(mddev, 1); |
| 8712 | |
| 8713 | mddev_unlock(mddev); |
| 8714 | |
| 8715 | return err; |
| 8716 | } |
| 8717 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8718 | static int raid5_start(struct mddev *mddev) |
| 8719 | { |
| 8720 | struct r5conf *conf = mddev->private; |
| 8721 | |
| 8722 | return r5l_start(conf->log); |
| 8723 | } |
| 8724 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8725 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8726 | { |
| 8727 | .name = "raid6", |
| 8728 | .level = 6, |
| 8729 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8730 | .make_request = raid5_make_request, |
| 8731 | .run = raid5_run, |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8732 | .start = raid5_start, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8733 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8734 | .status = raid5_status, |
| 8735 | .error_handler = raid5_error, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8736 | .hot_add_disk = raid5_add_disk, |
| 8737 | .hot_remove_disk= raid5_remove_disk, |
| 8738 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8739 | .sync_request = raid5_sync_request, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8740 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8741 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8742 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 8743 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8744 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8745 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8746 | .takeover = raid6_takeover, |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8747 | .change_consistency_policy = raid5_change_consistency_policy, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8748 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8749 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8750 | { |
| 8751 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8752 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8753 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8754 | .make_request = raid5_make_request, |
| 8755 | .run = raid5_run, |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8756 | .start = raid5_start, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8757 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8758 | .status = raid5_status, |
| 8759 | .error_handler = raid5_error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8760 | .hot_add_disk = raid5_add_disk, |
| 8761 | .hot_remove_disk= raid5_remove_disk, |
| 8762 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8763 | .sync_request = raid5_sync_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8764 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8765 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8766 | .check_reshape = raid5_check_reshape, |
| 8767 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8768 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8769 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8770 | .takeover = raid5_takeover, |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 8771 | .change_consistency_policy = raid5_change_consistency_policy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8772 | }; |
| 8773 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8774 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8775 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8776 | .name = "raid4", |
| 8777 | .level = 4, |
| 8778 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8779 | .make_request = raid5_make_request, |
| 8780 | .run = raid5_run, |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8781 | .start = raid5_start, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8782 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8783 | .status = raid5_status, |
| 8784 | .error_handler = raid5_error, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8785 | .hot_add_disk = raid5_add_disk, |
| 8786 | .hot_remove_disk= raid5_remove_disk, |
| 8787 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8788 | .sync_request = raid5_sync_request, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8789 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8790 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 8791 | .check_reshape = raid5_check_reshape, |
| 8792 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8793 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8794 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8795 | .takeover = raid4_takeover, |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8796 | .change_consistency_policy = raid5_change_consistency_policy, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8797 | }; |
| 8798 | |
| 8799 | static int __init raid5_init(void) |
| 8800 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8801 | int ret; |
| 8802 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8803 | raid5_wq = alloc_workqueue("raid5wq", |
| 8804 | WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); |
| 8805 | if (!raid5_wq) |
| 8806 | return -ENOMEM; |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8807 | |
| 8808 | ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE, |
| 8809 | "md/raid5:prepare", |
| 8810 | raid456_cpu_up_prepare, |
| 8811 | raid456_cpu_dead); |
| 8812 | if (ret) { |
| 8813 | destroy_workqueue(raid5_wq); |
| 8814 | return ret; |
| 8815 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8816 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8817 | register_md_personality(&raid5_personality); |
| 8818 | register_md_personality(&raid4_personality); |
| 8819 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8820 | } |
| 8821 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8822 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8823 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8824 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8825 | unregister_md_personality(&raid5_personality); |
| 8826 | unregister_md_personality(&raid4_personality); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8827 | cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8828 | destroy_workqueue(raid5_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8829 | } |
| 8830 | |
| 8831 | module_init(raid5_init); |
| 8832 | module_exit(raid5_exit); |
| 8833 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8834 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8835 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 8836 | MODULE_ALIAS("md-raid5"); |
| 8837 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8838 | MODULE_ALIAS("md-level-5"); |
| 8839 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8840 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 8841 | MODULE_ALIAS("md-raid6"); |
| 8842 | MODULE_ALIAS("md-level-6"); |
| 8843 | |
| 8844 | /* This used to be two separate modules, they were: */ |
| 8845 | MODULE_ALIAS("raid5"); |
| 8846 | MODULE_ALIAS("raid6"); |