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 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 956 | static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 957 | { |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 958 | const struct r5pending_data *da = list_entry(a, |
| 959 | struct r5pending_data, sibling); |
| 960 | const struct r5pending_data *db = list_entry(b, |
| 961 | struct r5pending_data, sibling); |
| 962 | if (da->sector > db->sector) |
| 963 | return 1; |
| 964 | if (da->sector < db->sector) |
| 965 | return -1; |
| 966 | return 0; |
| 967 | } |
| 968 | |
| 969 | static void dispatch_defer_bios(struct r5conf *conf, int target, |
| 970 | struct bio_list *list) |
| 971 | { |
| 972 | struct r5pending_data *data; |
| 973 | struct list_head *first, *next = NULL; |
| 974 | int cnt = 0; |
| 975 | |
| 976 | if (conf->pending_data_cnt == 0) |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 977 | return; |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 978 | |
| 979 | list_sort(NULL, &conf->pending_list, cmp_stripe); |
| 980 | |
| 981 | first = conf->pending_list.next; |
| 982 | |
| 983 | /* temporarily move the head */ |
| 984 | if (conf->next_pending_data) |
| 985 | list_move_tail(&conf->pending_list, |
| 986 | &conf->next_pending_data->sibling); |
| 987 | |
| 988 | while (!list_empty(&conf->pending_list)) { |
| 989 | data = list_first_entry(&conf->pending_list, |
| 990 | struct r5pending_data, sibling); |
| 991 | if (&data->sibling == first) |
| 992 | first = data->sibling.next; |
| 993 | next = data->sibling.next; |
| 994 | |
| 995 | bio_list_merge(list, &data->bios); |
| 996 | list_move(&data->sibling, &conf->free_list); |
| 997 | cnt++; |
| 998 | if (cnt >= target) |
| 999 | break; |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1000 | } |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1001 | conf->pending_data_cnt -= cnt; |
| 1002 | BUG_ON(conf->pending_data_cnt < 0 || cnt < target); |
| 1003 | |
| 1004 | if (next != &conf->pending_list) |
| 1005 | conf->next_pending_data = list_entry(next, |
| 1006 | struct r5pending_data, sibling); |
| 1007 | else |
| 1008 | conf->next_pending_data = NULL; |
| 1009 | /* list isn't empty */ |
| 1010 | if (first != &conf->pending_list) |
| 1011 | list_move_tail(&conf->pending_list, first); |
| 1012 | } |
| 1013 | |
| 1014 | static void flush_deferred_bios(struct r5conf *conf) |
| 1015 | { |
| 1016 | struct bio_list tmp = BIO_EMPTY_LIST; |
| 1017 | |
| 1018 | if (conf->pending_data_cnt == 0) |
| 1019 | return; |
| 1020 | |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1021 | spin_lock(&conf->pending_bios_lock); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1022 | dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp); |
| 1023 | BUG_ON(conf->pending_data_cnt != 0); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1024 | spin_unlock(&conf->pending_bios_lock); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1025 | |
| 1026 | dispatch_bio_list(&tmp); |
| 1027 | } |
| 1028 | |
| 1029 | static void defer_issue_bios(struct r5conf *conf, sector_t sector, |
| 1030 | struct bio_list *bios) |
| 1031 | { |
| 1032 | struct bio_list tmp = BIO_EMPTY_LIST; |
| 1033 | struct r5pending_data *ent; |
| 1034 | |
| 1035 | spin_lock(&conf->pending_bios_lock); |
| 1036 | ent = list_first_entry(&conf->free_list, struct r5pending_data, |
| 1037 | sibling); |
| 1038 | list_move_tail(&ent->sibling, &conf->pending_list); |
| 1039 | ent->sector = sector; |
| 1040 | bio_list_init(&ent->bios); |
| 1041 | bio_list_merge(&ent->bios, bios); |
| 1042 | conf->pending_data_cnt++; |
| 1043 | if (conf->pending_data_cnt >= PENDING_IO_MAX) |
| 1044 | dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp); |
| 1045 | |
| 1046 | spin_unlock(&conf->pending_bios_lock); |
| 1047 | |
| 1048 | dispatch_bio_list(&tmp); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1049 | } |
| 1050 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1051 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1052 | raid5_end_read_request(struct bio *bi); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 1053 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 1054 | raid5_end_write_request(struct bio *bi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1055 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 1056 | 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] | 1057 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1058 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1059 | int i, disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1060 | struct stripe_head *head_sh = sh; |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1061 | struct bio_list pending_bios = BIO_EMPTY_LIST; |
| 1062 | bool should_defer; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1063 | |
| 1064 | might_sleep(); |
| 1065 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 1066 | if (log_stripe(sh, s) == 0) |
| 1067 | return; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1068 | |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1069 | should_defer = conf->batch_bio_dispatch && conf->group_cnt; |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 1070 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1071 | for (i = disks; i--; ) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1072 | int op, op_flags = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1073 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1074 | struct bio *bi, *rbi; |
| 1075 | struct md_rdev *rdev, *rrdev = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1076 | |
| 1077 | sh = head_sh; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1078 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1079 | op = REQ_OP_WRITE; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1080 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 1081 | op_flags = REQ_FUA; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1082 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1083 | op = REQ_OP_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1084 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1085 | op = REQ_OP_READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1086 | else if (test_and_clear_bit(R5_WantReplace, |
| 1087 | &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1088 | op = REQ_OP_WRITE; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1089 | replace_only = 1; |
| 1090 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1091 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1092 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1093 | op_flags |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1094 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1095 | again: |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1096 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1097 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1098 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1099 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1100 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1101 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 1102 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 1103 | if (!rdev) { |
| 1104 | rdev = rrdev; |
| 1105 | rrdev = NULL; |
| 1106 | } |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1107 | if (op_is_write(op)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1108 | if (replace_only) |
| 1109 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1110 | if (rdev == rrdev) |
| 1111 | /* We raced and saw duplicates */ |
| 1112 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1113 | } else { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1114 | if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1115 | rdev = rrdev; |
| 1116 | rrdev = NULL; |
| 1117 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1118 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1119 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 1120 | rdev = NULL; |
| 1121 | if (rdev) |
| 1122 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1123 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 1124 | rrdev = NULL; |
| 1125 | if (rrdev) |
| 1126 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1127 | rcu_read_unlock(); |
| 1128 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1129 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1130 | * need to check for writes. We never accept write errors |
| 1131 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1132 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1133 | while (op_is_write(op) && rdev && |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1134 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 1135 | sector_t first_bad; |
| 1136 | int bad_sectors; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1137 | int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1138 | &first_bad, &bad_sectors); |
| 1139 | if (!bad) |
| 1140 | break; |
| 1141 | |
| 1142 | if (bad < 0) { |
| 1143 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 1144 | if (!conf->mddev->external && |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1145 | conf->mddev->sb_flags) { |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1146 | /* It is very unlikely, but we might |
| 1147 | * still need to write out the |
| 1148 | * bad block log - better give it |
| 1149 | * a chance*/ |
| 1150 | md_check_recovery(conf->mddev); |
| 1151 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 1152 | /* |
| 1153 | * Because md_wait_for_blocked_rdev |
| 1154 | * will dec nr_pending, we must |
| 1155 | * increment it first. |
| 1156 | */ |
| 1157 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1158 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 1159 | } else { |
| 1160 | /* Acknowledged bad block - skip the write */ |
| 1161 | rdev_dec_pending(rdev, conf->mddev); |
| 1162 | rdev = NULL; |
| 1163 | } |
| 1164 | } |
| 1165 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1166 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1167 | if (s->syncing || s->expanding || s->expanded |
| 1168 | || s->replacing) |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1169 | md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1170 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 1171 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1172 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1173 | bio_set_dev(bi, rdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1174 | bio_set_op_attrs(bi, op, op_flags); |
| 1175 | bi->bi_end_io = op_is_write(op) |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1176 | ? raid5_end_write_request |
| 1177 | : raid5_end_read_request; |
| 1178 | bi->bi_private = sh; |
| 1179 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1180 | pr_debug("%s: for %llu schedule op %d on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1181 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1182 | bi->bi_opf, i); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1183 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1184 | if (sh != head_sh) |
| 1185 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1186 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1187 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1188 | + rdev->new_data_offset); |
| 1189 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1190 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1191 | + rdev->data_offset); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1192 | if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags)) |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1193 | bi->bi_opf |= REQ_NOMERGE; |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1194 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1195 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1196 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 1197 | |
| 1198 | if (!op_is_write(op) && |
| 1199 | test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 1200 | /* |
| 1201 | * issuing read for a page in journal, this |
| 1202 | * must be preparing for prexor in rmw; read |
| 1203 | * the data into orig_page |
| 1204 | */ |
| 1205 | sh->dev[i].vec.bv_page = sh->dev[i].orig_page; |
| 1206 | else |
| 1207 | sh->dev[i].vec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1208 | bi->bi_vcnt = 1; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1209 | bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf); |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1210 | bi->bi_io_vec[0].bv_offset = sh->dev[i].offset; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1211 | bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf); |
Mariusz Dabrowski | 2cd259a | 2018-04-19 19:28:10 +0200 | [diff] [blame] | 1212 | bi->bi_write_hint = sh->dev[i].write_hint; |
| 1213 | if (!rrdev) |
Eugene Syromiatnikov | f193489 | 2019-09-20 17:58:28 +0200 | [diff] [blame] | 1214 | sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1215 | /* |
| 1216 | * If this is discard request, set bi_vcnt 0. We don't |
| 1217 | * want to confuse SCSI because SCSI will replace payload |
| 1218 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1219 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1220 | bi->bi_vcnt = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1221 | if (rrdev) |
| 1222 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1223 | |
| 1224 | if (conf->mddev->gendisk) |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1225 | trace_block_bio_remap(bi->bi_disk->queue, |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1226 | bi, disk_devt(conf->mddev->gendisk), |
| 1227 | sh->dev[i].sector); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1228 | if (should_defer && op_is_write(op)) |
| 1229 | bio_list_add(&pending_bios, bi); |
| 1230 | else |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 1231 | submit_bio_noacct(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1232 | } |
| 1233 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1234 | if (s->syncing || s->expanding || s->expanded |
| 1235 | || s->replacing) |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1236 | md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf)); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1237 | |
| 1238 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1239 | |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1240 | bio_set_dev(rbi, rrdev->bdev); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1241 | bio_set_op_attrs(rbi, op, op_flags); |
| 1242 | BUG_ON(!op_is_write(op)); |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1243 | rbi->bi_end_io = raid5_end_write_request; |
| 1244 | rbi->bi_private = sh; |
| 1245 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1246 | pr_debug("%s: for %llu schedule op %d on " |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1247 | "replacement disc %d\n", |
| 1248 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1249 | rbi->bi_opf, i); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1250 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1251 | if (sh != head_sh) |
| 1252 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1253 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1254 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1255 | + rrdev->new_data_offset); |
| 1256 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1257 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1258 | + rrdev->data_offset); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1259 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1260 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 1261 | sh->dev[i].rvec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1262 | rbi->bi_vcnt = 1; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1263 | rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf); |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1264 | rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1265 | rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf); |
Mariusz Dabrowski | 2cd259a | 2018-04-19 19:28:10 +0200 | [diff] [blame] | 1266 | rbi->bi_write_hint = sh->dev[i].write_hint; |
Eugene Syromiatnikov | f193489 | 2019-09-20 17:58:28 +0200 | [diff] [blame] | 1267 | sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1268 | /* |
| 1269 | * If this is discard request, set bi_vcnt 0. We don't |
| 1270 | * want to confuse SCSI because SCSI will replace payload |
| 1271 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1272 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1273 | rbi->bi_vcnt = 0; |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1274 | if (conf->mddev->gendisk) |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 1275 | trace_block_bio_remap(rbi->bi_disk->queue, |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1276 | rbi, disk_devt(conf->mddev->gendisk), |
| 1277 | sh->dev[i].sector); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1278 | if (should_defer && op_is_write(op)) |
| 1279 | bio_list_add(&pending_bios, rbi); |
| 1280 | else |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 1281 | submit_bio_noacct(rbi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1282 | } |
| 1283 | if (!rdev && !rrdev) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1284 | if (op_is_write(op)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1285 | set_bit(STRIPE_DEGRADED, &sh->state); |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1286 | pr_debug("skip op %d on disc %d for sector %llu\n", |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1287 | bi->bi_opf, i, (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1288 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1289 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1290 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1291 | |
| 1292 | if (!head_sh->batch_head) |
| 1293 | continue; |
| 1294 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1295 | batch_list); |
| 1296 | if (sh != head_sh) |
| 1297 | goto again; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1298 | } |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 1299 | |
| 1300 | if (should_defer && !bio_list_empty(&pending_bios)) |
| 1301 | defer_issue_bios(conf, head_sh->sector, &pending_bios); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1302 | } |
| 1303 | |
| 1304 | static struct dma_async_tx_descriptor * |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1305 | async_copy_data(int frombio, struct bio *bio, struct page **page, |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1306 | unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1307 | struct stripe_head *sh, int no_skipcopy) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1308 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1309 | struct bio_vec bvl; |
| 1310 | struct bvec_iter iter; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1311 | struct page *bio_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1312 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1313 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1314 | enum async_tx_flags flags = 0; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1315 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1316 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1317 | if (bio->bi_iter.bi_sector >= sector) |
| 1318 | page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1319 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1320 | page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1321 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1322 | if (frombio) |
| 1323 | flags |= ASYNC_TX_FENCE; |
| 1324 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 1325 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1326 | bio_for_each_segment(bvl, bio, iter) { |
| 1327 | int len = bvl.bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1328 | int clen; |
| 1329 | int b_offset = 0; |
| 1330 | |
| 1331 | if (page_offset < 0) { |
| 1332 | b_offset = -page_offset; |
| 1333 | page_offset += b_offset; |
| 1334 | len -= b_offset; |
| 1335 | } |
| 1336 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1337 | if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf)) |
| 1338 | clen = RAID5_STRIPE_SIZE(conf) - page_offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1339 | else |
| 1340 | clen = len; |
| 1341 | |
| 1342 | if (clen > 0) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1343 | b_offset += bvl.bv_offset; |
| 1344 | bio_page = bvl.bv_page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1345 | if (frombio) { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1346 | if (conf->skip_copy && |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1347 | b_offset == 0 && page_offset == 0 && |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1348 | clen == RAID5_STRIPE_SIZE(conf) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1349 | !no_skipcopy) |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1350 | *page = bio_page; |
| 1351 | else |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1352 | tx = async_memcpy(*page, bio_page, page_offset + poff, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1353 | b_offset, clen, &submit); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1354 | } else |
| 1355 | tx = async_memcpy(bio_page, *page, b_offset, |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1356 | page_offset + poff, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1357 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1358 | /* chain the operations */ |
| 1359 | submit.depend_tx = tx; |
| 1360 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1361 | if (clen < len) /* hit end of page */ |
| 1362 | break; |
| 1363 | page_offset += len; |
| 1364 | } |
| 1365 | |
| 1366 | return tx; |
| 1367 | } |
| 1368 | |
| 1369 | static void ops_complete_biofill(void *stripe_head_ref) |
| 1370 | { |
| 1371 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1372 | int i; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1373 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1374 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1375 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1376 | (unsigned long long)sh->sector); |
| 1377 | |
| 1378 | /* clear completed biofills */ |
| 1379 | for (i = sh->disks; i--; ) { |
| 1380 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1381 | |
| 1382 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1383 | /* and check if we need to reply to a read request, |
| 1384 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1385 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1386 | */ |
| 1387 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1388 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1389 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1390 | BUG_ON(!dev->read); |
| 1391 | rbi = dev->read; |
| 1392 | dev->read = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1393 | while (rbi && rbi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1394 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
| 1395 | rbi2 = r5_next_bio(conf, rbi, dev->sector); |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 1396 | bio_endio(rbi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1397 | rbi = rbi2; |
| 1398 | } |
| 1399 | } |
| 1400 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1401 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1402 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1403 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1404 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1405 | } |
| 1406 | |
| 1407 | static void ops_run_biofill(struct stripe_head *sh) |
| 1408 | { |
| 1409 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1410 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1411 | int i; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1412 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1413 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1414 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1415 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1416 | (unsigned long long)sh->sector); |
| 1417 | |
| 1418 | for (i = sh->disks; i--; ) { |
| 1419 | struct r5dev *dev = &sh->dev[i]; |
| 1420 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 1421 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1422 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1423 | dev->read = rbi = dev->toread; |
| 1424 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1425 | spin_unlock_irq(&sh->stripe_lock); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1426 | while (rbi && rbi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1427 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1428 | tx = async_copy_data(0, rbi, &dev->page, |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1429 | dev->offset, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1430 | dev->sector, tx, sh, 0); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1431 | rbi = r5_next_bio(conf, rbi, dev->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1432 | } |
| 1433 | } |
| 1434 | } |
| 1435 | |
| 1436 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1437 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 1438 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1439 | } |
| 1440 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1441 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 1442 | { |
| 1443 | struct r5dev *tgt; |
| 1444 | |
| 1445 | if (target < 0) |
| 1446 | return; |
| 1447 | |
| 1448 | tgt = &sh->dev[target]; |
| 1449 | set_bit(R5_UPTODATE, &tgt->flags); |
| 1450 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1451 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 1452 | } |
| 1453 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1454 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1455 | { |
| 1456 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1457 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1458 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1459 | (unsigned long long)sh->sector); |
| 1460 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1461 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1462 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1463 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1464 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1465 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 1466 | if (sh->check_state == check_state_compute_run) |
| 1467 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1468 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1469 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1470 | } |
| 1471 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1472 | /* return a pointer to the address conversion region of the scribble buffer */ |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 1473 | static struct page **to_addr_page(struct raid5_percpu *percpu, int i) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1474 | { |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 1475 | return percpu->scribble + i * percpu->scribble_obj_size; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1476 | } |
| 1477 | |
| 1478 | /* return a pointer to the address conversion region of the scribble buffer */ |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 1479 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
| 1480 | struct raid5_percpu *percpu, int i) |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1481 | { |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 1482 | return (void *) (to_addr_page(percpu, i) + sh->disks + 2); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1483 | } |
| 1484 | |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1485 | /* |
| 1486 | * Return a pointer to record offset address. |
| 1487 | */ |
| 1488 | static unsigned int * |
| 1489 | to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1490 | { |
| 1491 | return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2); |
| 1492 | } |
| 1493 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1494 | static struct dma_async_tx_descriptor * |
| 1495 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1496 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1497 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1498 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1499 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1500 | int target = sh->ops.target; |
| 1501 | struct r5dev *tgt = &sh->dev[target]; |
| 1502 | struct page *xor_dest = tgt->page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1503 | unsigned int off_dest = tgt->offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1504 | int count = 0; |
| 1505 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1506 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1507 | int i; |
| 1508 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1509 | BUG_ON(sh->batch_head); |
| 1510 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1511 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1512 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1513 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1514 | |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1515 | for (i = disks; i--; ) { |
| 1516 | if (i != target) { |
| 1517 | off_srcs[count] = sh->dev[i].offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1518 | xor_srcs[count++] = sh->dev[i].page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1519 | } |
| 1520 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1521 | |
| 1522 | atomic_inc(&sh->count); |
| 1523 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1524 | 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] | 1525 | ops_complete_compute, sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1526 | if (unlikely(count == 1)) |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1527 | 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] | 1528 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1529 | else |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1530 | 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] | 1531 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1532 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1533 | return tx; |
| 1534 | } |
| 1535 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1536 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 1537 | * @srcs - (struct page *) array of size sh->disks |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1538 | * @offs - (unsigned int) array of offset for each page |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1539 | * @sh - stripe_head to parse |
| 1540 | * |
| 1541 | * Populates srcs in proper layout order for the stripe and returns the |
| 1542 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 1543 | * destination buffer is recorded in srcs[count] and the Q destination |
| 1544 | * is recorded in srcs[count+1]]. |
| 1545 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1546 | static int set_syndrome_sources(struct page **srcs, |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1547 | unsigned int *offs, |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1548 | struct stripe_head *sh, |
| 1549 | int srctype) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1550 | { |
| 1551 | int disks = sh->disks; |
| 1552 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 1553 | int d0_idx = raid6_d0(sh); |
| 1554 | int count; |
| 1555 | int i; |
| 1556 | |
| 1557 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1558 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1559 | |
| 1560 | count = 0; |
| 1561 | i = d0_idx; |
| 1562 | do { |
| 1563 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1564 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1565 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1566 | if (i == sh->qd_idx || i == sh->pd_idx || |
| 1567 | (srctype == SYNDROME_SRC_ALL) || |
| 1568 | (srctype == SYNDROME_SRC_WANT_DRAIN && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1569 | (test_bit(R5_Wantdrain, &dev->flags) || |
| 1570 | test_bit(R5_InJournal, &dev->flags))) || |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1571 | (srctype == SYNDROME_SRC_WRITTEN && |
Song Liu | 0977762 | 2017-03-13 13:44:35 -0700 | [diff] [blame] | 1572 | (dev->written || |
| 1573 | test_bit(R5_InJournal, &dev->flags)))) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1574 | if (test_bit(R5_InJournal, &dev->flags)) |
| 1575 | srcs[slot] = sh->dev[i].orig_page; |
| 1576 | else |
| 1577 | srcs[slot] = sh->dev[i].page; |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1578 | /* |
| 1579 | * For R5_InJournal, PAGE_SIZE must be 4KB and will |
| 1580 | * not shared page. In that case, dev[i].offset |
| 1581 | * is 0. |
| 1582 | */ |
| 1583 | offs[slot] = sh->dev[i].offset; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1584 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1585 | i = raid6_next_disk(i, disks); |
| 1586 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1587 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1588 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1589 | } |
| 1590 | |
| 1591 | static struct dma_async_tx_descriptor * |
| 1592 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1593 | { |
| 1594 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1595 | struct page **blocks = to_addr_page(percpu, 0); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1596 | unsigned int *offs = to_addr_offs(sh, percpu); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1597 | int target; |
| 1598 | int qd_idx = sh->qd_idx; |
| 1599 | struct dma_async_tx_descriptor *tx; |
| 1600 | struct async_submit_ctl submit; |
| 1601 | struct r5dev *tgt; |
| 1602 | struct page *dest; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1603 | unsigned int dest_off; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1604 | int i; |
| 1605 | int count; |
| 1606 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1607 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1608 | if (sh->ops.target < 0) |
| 1609 | target = sh->ops.target2; |
| 1610 | else if (sh->ops.target2 < 0) |
| 1611 | target = sh->ops.target; |
| 1612 | else |
| 1613 | /* we should only have one valid target */ |
| 1614 | BUG(); |
| 1615 | BUG_ON(target < 0); |
| 1616 | pr_debug("%s: stripe %llu block: %d\n", |
| 1617 | __func__, (unsigned long long)sh->sector, target); |
| 1618 | |
| 1619 | tgt = &sh->dev[target]; |
| 1620 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1621 | dest = tgt->page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1622 | dest_off = tgt->offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1623 | |
| 1624 | atomic_inc(&sh->count); |
| 1625 | |
| 1626 | if (target == qd_idx) { |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1627 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1628 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 1629 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1630 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1631 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1632 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1633 | tx = async_gen_syndrome(blocks, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1634 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1635 | } else { |
| 1636 | /* Compute any data- or p-drive using XOR */ |
| 1637 | count = 0; |
| 1638 | for (i = disks; i-- ; ) { |
| 1639 | if (i == target || i == qd_idx) |
| 1640 | continue; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1641 | offs[count] = sh->dev[i].offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1642 | blocks[count++] = sh->dev[i].page; |
| 1643 | } |
| 1644 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1645 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1646 | NULL, ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1647 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1648 | tx = async_xor_offs(dest, dest_off, blocks, offs, count, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1649 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1650 | } |
| 1651 | |
| 1652 | return tx; |
| 1653 | } |
| 1654 | |
| 1655 | static struct dma_async_tx_descriptor * |
| 1656 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1657 | { |
| 1658 | int i, count, disks = sh->disks; |
| 1659 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1660 | int d0_idx = raid6_d0(sh); |
| 1661 | int faila = -1, failb = -1; |
| 1662 | int target = sh->ops.target; |
| 1663 | int target2 = sh->ops.target2; |
| 1664 | struct r5dev *tgt = &sh->dev[target]; |
| 1665 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1666 | struct dma_async_tx_descriptor *tx; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1667 | struct page **blocks = to_addr_page(percpu, 0); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1668 | unsigned int *offs = to_addr_offs(sh, percpu); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1669 | struct async_submit_ctl submit; |
| 1670 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1671 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1672 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1673 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1674 | BUG_ON(target < 0 || target2 < 0); |
| 1675 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1676 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1677 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1678 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1679 | * slot number conversion for 'faila' and 'failb' |
| 1680 | */ |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1681 | for (i = 0; i < disks ; i++) { |
| 1682 | offs[i] = 0; |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1683 | blocks[i] = NULL; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1684 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1685 | count = 0; |
| 1686 | i = d0_idx; |
| 1687 | do { |
| 1688 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1689 | |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1690 | offs[slot] = sh->dev[i].offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1691 | blocks[slot] = sh->dev[i].page; |
| 1692 | |
| 1693 | if (i == target) |
| 1694 | faila = slot; |
| 1695 | if (i == target2) |
| 1696 | failb = slot; |
| 1697 | i = raid6_next_disk(i, disks); |
| 1698 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1699 | |
| 1700 | BUG_ON(faila == failb); |
| 1701 | if (failb < faila) |
| 1702 | swap(faila, failb); |
| 1703 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1704 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1705 | |
| 1706 | atomic_inc(&sh->count); |
| 1707 | |
| 1708 | if (failb == syndrome_disks+1) { |
| 1709 | /* Q disk is one of the missing disks */ |
| 1710 | if (faila == syndrome_disks) { |
| 1711 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1712 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1713 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1714 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1715 | return async_gen_syndrome(blocks, offs, syndrome_disks+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1716 | RAID5_STRIPE_SIZE(sh->raid_conf), |
| 1717 | &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1718 | } else { |
| 1719 | struct page *dest; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1720 | unsigned int dest_off; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1721 | int data_target; |
| 1722 | int qd_idx = sh->qd_idx; |
| 1723 | |
| 1724 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1725 | if (target == qd_idx) |
| 1726 | data_target = target2; |
| 1727 | else |
| 1728 | data_target = target; |
| 1729 | |
| 1730 | count = 0; |
| 1731 | for (i = disks; i-- ; ) { |
| 1732 | if (i == data_target || i == qd_idx) |
| 1733 | continue; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1734 | offs[count] = sh->dev[i].offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1735 | blocks[count++] = sh->dev[i].page; |
| 1736 | } |
| 1737 | dest = sh->dev[data_target].page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1738 | dest_off = sh->dev[data_target].offset; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1739 | init_async_submit(&submit, |
| 1740 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1741 | NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1742 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1743 | tx = async_xor_offs(dest, dest_off, blocks, offs, count, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1744 | RAID5_STRIPE_SIZE(sh->raid_conf), |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1745 | &submit); |
| 1746 | |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1747 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_ALL); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1748 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1749 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1750 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1751 | return async_gen_syndrome(blocks, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1752 | RAID5_STRIPE_SIZE(sh->raid_conf), |
| 1753 | &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1754 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1755 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1756 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1757 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1758 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1759 | if (failb == syndrome_disks) { |
| 1760 | /* We're missing D+P. */ |
| 1761 | return async_raid6_datap_recov(syndrome_disks+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1762 | RAID5_STRIPE_SIZE(sh->raid_conf), |
| 1763 | faila, |
Yufen Yu | 4f86ff5 | 2020-08-20 09:22:11 -0400 | [diff] [blame] | 1764 | blocks, offs, &submit); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1765 | } else { |
| 1766 | /* We're missing D+D. */ |
| 1767 | return async_raid6_2data_recov(syndrome_disks+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1768 | RAID5_STRIPE_SIZE(sh->raid_conf), |
| 1769 | faila, failb, |
Yufen Yu | 4f86ff5 | 2020-08-20 09:22:11 -0400 | [diff] [blame] | 1770 | blocks, offs, &submit); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1771 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1772 | } |
| 1773 | } |
| 1774 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1775 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1776 | { |
| 1777 | struct stripe_head *sh = stripe_head_ref; |
| 1778 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1779 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1780 | (unsigned long long)sh->sector); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1781 | |
| 1782 | if (r5c_is_writeback(sh->raid_conf->log)) |
| 1783 | /* |
| 1784 | * raid5-cache write back uses orig_page during prexor. |
| 1785 | * After prexor, it is time to free orig_page |
| 1786 | */ |
| 1787 | r5c_release_extra_page(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1788 | } |
| 1789 | |
| 1790 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1791 | ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1792 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1793 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1794 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1795 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1796 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1797 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1798 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1799 | |
| 1800 | /* existing parity data subtracted */ |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1801 | unsigned int off_dest = off_srcs[count] = sh->dev[pd_idx].offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1802 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1803 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1804 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1805 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1806 | (unsigned long long)sh->sector); |
| 1807 | |
| 1808 | for (i = disks; i--; ) { |
| 1809 | struct r5dev *dev = &sh->dev[i]; |
| 1810 | /* Only process blocks that are known to be uptodate */ |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1811 | if (test_bit(R5_InJournal, &dev->flags)) { |
| 1812 | /* |
| 1813 | * For this case, PAGE_SIZE must be equal to 4KB and |
| 1814 | * page offset is zero. |
| 1815 | */ |
| 1816 | off_srcs[count] = dev->offset; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1817 | xor_srcs[count++] = dev->orig_page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1818 | } else if (test_bit(R5_Wantdrain, &dev->flags)) { |
| 1819 | off_srcs[count] = dev->offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1820 | xor_srcs[count++] = dev->page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1821 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1822 | } |
| 1823 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1824 | 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] | 1825 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 1826 | 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] | 1827 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1828 | |
| 1829 | return tx; |
| 1830 | } |
| 1831 | |
| 1832 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1833 | ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1834 | struct dma_async_tx_descriptor *tx) |
| 1835 | { |
| 1836 | struct page **blocks = to_addr_page(percpu, 0); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1837 | unsigned int *offs = to_addr_offs(sh, percpu); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1838 | int count; |
| 1839 | struct async_submit_ctl submit; |
| 1840 | |
| 1841 | pr_debug("%s: stripe %llu\n", __func__, |
| 1842 | (unsigned long long)sh->sector); |
| 1843 | |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1844 | count = set_syndrome_sources(blocks, offs, sh, SYNDROME_SRC_WANT_DRAIN); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1845 | |
| 1846 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx, |
| 1847 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 1848 | tx = async_gen_syndrome(blocks, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1849 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1850 | |
| 1851 | return tx; |
| 1852 | } |
| 1853 | |
| 1854 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1855 | 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] | 1856 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1857 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1858 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1859 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1860 | struct stripe_head *head_sh = sh; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1861 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1862 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1863 | (unsigned long long)sh->sector); |
| 1864 | |
| 1865 | for (i = disks; i--; ) { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1866 | struct r5dev *dev; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1867 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1868 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1869 | sh = head_sh; |
| 1870 | if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1871 | struct bio *wbi; |
| 1872 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1873 | again: |
| 1874 | dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1875 | /* |
| 1876 | * clear R5_InJournal, so when rewriting a page in |
| 1877 | * journal, it is not skipped by r5l_log_stripe() |
| 1878 | */ |
| 1879 | clear_bit(R5_InJournal, &dev->flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1880 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1881 | chosen = dev->towrite; |
| 1882 | dev->towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1883 | sh->overwrite_disks = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1884 | BUG_ON(dev->written); |
| 1885 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1886 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1887 | WARN_ON(dev->page != dev->orig_page); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1888 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1889 | while (wbi && wbi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1890 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1891 | if (wbi->bi_opf & REQ_FUA) |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1892 | set_bit(R5_WantFUA, &dev->flags); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1893 | if (wbi->bi_opf & REQ_SYNC) |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1894 | set_bit(R5_SyncIO, &dev->flags); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1895 | if (bio_op(wbi) == REQ_OP_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1896 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1897 | else { |
| 1898 | tx = async_copy_data(1, wbi, &dev->page, |
Yufen Yu | 248728d | 2020-08-20 09:22:07 -0400 | [diff] [blame] | 1899 | dev->offset, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1900 | dev->sector, tx, sh, |
| 1901 | r5c_is_writeback(conf->log)); |
| 1902 | if (dev->page != dev->orig_page && |
| 1903 | !r5c_is_writeback(conf->log)) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1904 | set_bit(R5_SkipCopy, &dev->flags); |
| 1905 | clear_bit(R5_UPTODATE, &dev->flags); |
| 1906 | clear_bit(R5_OVERWRITE, &dev->flags); |
| 1907 | } |
| 1908 | } |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 1909 | wbi = r5_next_bio(conf, wbi, dev->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1910 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1911 | |
| 1912 | if (head_sh->batch_head) { |
| 1913 | sh = list_first_entry(&sh->batch_list, |
| 1914 | struct stripe_head, |
| 1915 | batch_list); |
| 1916 | if (sh == head_sh) |
| 1917 | continue; |
| 1918 | goto again; |
| 1919 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1920 | } |
| 1921 | } |
| 1922 | |
| 1923 | return tx; |
| 1924 | } |
| 1925 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1926 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1927 | { |
| 1928 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1929 | int disks = sh->disks; |
| 1930 | int pd_idx = sh->pd_idx; |
| 1931 | int qd_idx = sh->qd_idx; |
| 1932 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1933 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1934 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1935 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1936 | (unsigned long long)sh->sector); |
| 1937 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1938 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1939 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1940 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1941 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1942 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1943 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1944 | for (i = disks; i--; ) { |
| 1945 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1946 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1947 | if (dev->written || i == pd_idx || i == qd_idx) { |
NeilBrown | 235b600 | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 1948 | if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) { |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1949 | set_bit(R5_UPTODATE, &dev->flags); |
NeilBrown | 235b600 | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 1950 | if (test_bit(STRIPE_EXPAND_READY, &sh->state)) |
| 1951 | set_bit(R5_Expanded, &dev->flags); |
| 1952 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1953 | if (fua) |
| 1954 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1955 | if (sync) |
| 1956 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1957 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1958 | } |
| 1959 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1960 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1961 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1962 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1963 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1964 | else { |
| 1965 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1966 | sh->reconstruct_state = reconstruct_state_result; |
| 1967 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1968 | |
| 1969 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1970 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1974 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1975 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1976 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1977 | int disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1978 | struct page **xor_srcs; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1979 | unsigned int *off_srcs; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1980 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1981 | int count, pd_idx = sh->pd_idx, i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1982 | struct page *xor_dest; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 1983 | unsigned int off_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1984 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1985 | unsigned long flags; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1986 | int j = 0; |
| 1987 | struct stripe_head *head_sh = sh; |
| 1988 | int last_stripe; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1989 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1990 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1991 | (unsigned long long)sh->sector); |
| 1992 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1993 | for (i = 0; i < sh->disks; i++) { |
| 1994 | if (pd_idx == i) |
| 1995 | continue; |
| 1996 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1997 | break; |
| 1998 | } |
| 1999 | if (i >= sh->disks) { |
| 2000 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 2001 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 2002 | ops_complete_reconstruct(sh); |
| 2003 | return; |
| 2004 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2005 | again: |
| 2006 | count = 0; |
| 2007 | xor_srcs = to_addr_page(percpu, j); |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2008 | off_srcs = to_addr_offs(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2009 | /* check if prexor is active which means only process blocks |
| 2010 | * that are part of a read-modify-write (written) |
| 2011 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2012 | if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2013 | prexor = 1; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2014 | off_dest = off_srcs[count] = sh->dev[pd_idx].offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2015 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 2016 | for (i = disks; i--; ) { |
| 2017 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2018 | if (head_sh->dev[i].written || |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2019 | test_bit(R5_InJournal, &head_sh->dev[i].flags)) { |
| 2020 | off_srcs[count] = dev->offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2021 | xor_srcs[count++] = dev->page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2022 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2023 | } |
| 2024 | } else { |
| 2025 | xor_dest = sh->dev[pd_idx].page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2026 | off_dest = sh->dev[pd_idx].offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2027 | for (i = disks; i--; ) { |
| 2028 | struct r5dev *dev = &sh->dev[i]; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2029 | if (i != pd_idx) { |
| 2030 | off_srcs[count] = dev->offset; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2031 | xor_srcs[count++] = dev->page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2032 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2033 | } |
| 2034 | } |
| 2035 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2036 | /* 1/ if we prexor'd then the dest is reused as a source |
| 2037 | * 2/ if we did not prexor then we are redoing the parity |
| 2038 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 2039 | * for the synchronous xor case |
| 2040 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2041 | last_stripe = !head_sh->batch_head || |
| 2042 | list_first_entry(&sh->batch_list, |
| 2043 | struct stripe_head, batch_list) == head_sh; |
| 2044 | if (last_stripe) { |
| 2045 | flags = ASYNC_TX_ACK | |
| 2046 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2047 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2048 | atomic_inc(&head_sh->count); |
| 2049 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh, |
| 2050 | to_addr_conv(sh, percpu, j)); |
| 2051 | } else { |
| 2052 | flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST; |
| 2053 | init_async_submit(&submit, flags, tx, NULL, NULL, |
| 2054 | to_addr_conv(sh, percpu, j)); |
| 2055 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2056 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2057 | if (unlikely(count == 1)) |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2058 | 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] | 2059 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2060 | else |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2061 | 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] | 2062 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2063 | if (!last_stripe) { |
| 2064 | j++; |
| 2065 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 2066 | batch_list); |
| 2067 | goto again; |
| 2068 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2069 | } |
| 2070 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2071 | static void |
| 2072 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 2073 | struct dma_async_tx_descriptor *tx) |
| 2074 | { |
| 2075 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2076 | struct page **blocks; |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2077 | unsigned int *offs; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2078 | int count, i, j = 0; |
| 2079 | struct stripe_head *head_sh = sh; |
| 2080 | int last_stripe; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2081 | int synflags; |
| 2082 | unsigned long txflags; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2083 | |
| 2084 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 2085 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 2086 | for (i = 0; i < sh->disks; i++) { |
| 2087 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 2088 | continue; |
| 2089 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 2090 | break; |
| 2091 | } |
| 2092 | if (i >= sh->disks) { |
| 2093 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 2094 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 2095 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 2096 | ops_complete_reconstruct(sh); |
| 2097 | return; |
| 2098 | } |
| 2099 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2100 | again: |
| 2101 | blocks = to_addr_page(percpu, j); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2102 | offs = to_addr_offs(sh, percpu); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2103 | |
| 2104 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 2105 | synflags = SYNDROME_SRC_WRITTEN; |
| 2106 | txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST; |
| 2107 | } else { |
| 2108 | synflags = SYNDROME_SRC_ALL; |
| 2109 | txflags = ASYNC_TX_ACK; |
| 2110 | } |
| 2111 | |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2112 | count = set_syndrome_sources(blocks, offs, sh, synflags); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2113 | last_stripe = !head_sh->batch_head || |
| 2114 | list_first_entry(&sh->batch_list, |
| 2115 | struct stripe_head, batch_list) == head_sh; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2116 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2117 | if (last_stripe) { |
| 2118 | atomic_inc(&head_sh->count); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2119 | init_async_submit(&submit, txflags, tx, ops_complete_reconstruct, |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2120 | head_sh, to_addr_conv(sh, percpu, j)); |
| 2121 | } else |
| 2122 | init_async_submit(&submit, 0, tx, NULL, NULL, |
| 2123 | to_addr_conv(sh, percpu, j)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2124 | tx = async_gen_syndrome(blocks, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2125 | RAID5_STRIPE_SIZE(sh->raid_conf), &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2126 | if (!last_stripe) { |
| 2127 | j++; |
| 2128 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 2129 | batch_list); |
| 2130 | goto again; |
| 2131 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2132 | } |
| 2133 | |
| 2134 | static void ops_complete_check(void *stripe_head_ref) |
| 2135 | { |
| 2136 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2137 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2138 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2139 | (unsigned long long)sh->sector); |
| 2140 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 2141 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2142 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2143 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2144 | } |
| 2145 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2146 | 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] | 2147 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2148 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2149 | int pd_idx = sh->pd_idx; |
| 2150 | int qd_idx = sh->qd_idx; |
| 2151 | struct page *xor_dest; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2152 | unsigned int off_dest; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2153 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2154 | unsigned int *off_srcs = to_addr_offs(sh, percpu); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2155 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2156 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2157 | int count; |
| 2158 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2159 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2160 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2161 | (unsigned long long)sh->sector); |
| 2162 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2163 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2164 | count = 0; |
| 2165 | xor_dest = sh->dev[pd_idx].page; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2166 | off_dest = sh->dev[pd_idx].offset; |
| 2167 | off_srcs[count] = off_dest; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2168 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2169 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2170 | if (i == pd_idx || i == qd_idx) |
| 2171 | continue; |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2172 | off_srcs[count] = sh->dev[i].offset; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2173 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2174 | } |
| 2175 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2176 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2177 | to_addr_conv(sh, percpu, 0)); |
Yufen Yu | a7c224a | 2020-08-20 09:22:09 -0400 | [diff] [blame] | 2178 | 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] | 2179 | RAID5_STRIPE_SIZE(sh->raid_conf), |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2180 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2181 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2182 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 2183 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 2184 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2185 | } |
| 2186 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2187 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 2188 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2189 | struct page **srcs = to_addr_page(percpu, 0); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2190 | unsigned int *offs = to_addr_offs(sh, percpu); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2191 | struct async_submit_ctl submit; |
| 2192 | int count; |
| 2193 | |
| 2194 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 2195 | (unsigned long long)sh->sector, checkp); |
| 2196 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2197 | BUG_ON(sh->batch_head); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2198 | count = set_syndrome_sources(srcs, offs, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2199 | if (!checkp) |
| 2200 | srcs[count] = NULL; |
| 2201 | |
| 2202 | atomic_inc(&sh->count); |
| 2203 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2204 | sh, to_addr_conv(sh, percpu, 0)); |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2205 | async_syndrome_val(srcs, offs, count+2, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2206 | RAID5_STRIPE_SIZE(sh->raid_conf), |
Yufen Yu | d69454b | 2020-08-20 09:22:10 -0400 | [diff] [blame] | 2207 | &sh->ops.zero_sum_result, percpu->spare_page, 0, &submit); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2208 | } |
| 2209 | |
NeilBrown | 51acbce | 2013-02-28 09:08:34 +1100 | [diff] [blame] | 2210 | 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] | 2211 | { |
| 2212 | int overlap_clear = 0, i, disks = sh->disks; |
| 2213 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2214 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2215 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2216 | struct raid5_percpu *percpu; |
| 2217 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2218 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2219 | cpu = get_cpu(); |
| 2220 | percpu = per_cpu_ptr(conf->percpu, cpu); |
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 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [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 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2279 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2280 | } |
| 2281 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2282 | static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh) |
| 2283 | { |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame^] | 2284 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 2285 | kfree(sh->pages); |
| 2286 | #endif |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2287 | if (sh->ppl_page) |
| 2288 | __free_page(sh->ppl_page); |
| 2289 | kmem_cache_free(sc, sh); |
| 2290 | } |
| 2291 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2292 | 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] | 2293 | int disks, struct r5conf *conf) |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2294 | { |
| 2295 | struct stripe_head *sh; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2296 | int i; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2297 | |
| 2298 | sh = kmem_cache_zalloc(sc, gfp); |
| 2299 | if (sh) { |
| 2300 | spin_lock_init(&sh->stripe_lock); |
| 2301 | spin_lock_init(&sh->batch_lock); |
| 2302 | INIT_LIST_HEAD(&sh->batch_list); |
| 2303 | INIT_LIST_HEAD(&sh->lru); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2304 | INIT_LIST_HEAD(&sh->r5c); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2305 | INIT_LIST_HEAD(&sh->log_list); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2306 | atomic_set(&sh->count, 1); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2307 | sh->raid_conf = conf; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2308 | sh->log_start = MaxSector; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2309 | for (i = 0; i < disks; i++) { |
| 2310 | struct r5dev *dev = &sh->dev[i]; |
| 2311 | |
Ming Lei | 3a83f46 | 2016-11-22 08:57:21 -0700 | [diff] [blame] | 2312 | bio_init(&dev->req, &dev->vec, 1); |
| 2313 | bio_init(&dev->rreq, &dev->rvec, 1); |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2314 | } |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2315 | |
| 2316 | if (raid5_has_ppl(conf)) { |
| 2317 | sh->ppl_page = alloc_page(gfp); |
| 2318 | if (!sh->ppl_page) { |
| 2319 | free_stripe(sc, sh); |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame^] | 2320 | return NULL; |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2321 | } |
| 2322 | } |
Yufen Yu | 046169f | 2020-08-20 09:22:12 -0400 | [diff] [blame^] | 2323 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 2324 | if (init_stripe_shared_pages(sh, conf, disks)) { |
| 2325 | free_stripe(sc, sh); |
| 2326 | return NULL; |
| 2327 | } |
| 2328 | #endif |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2329 | } |
| 2330 | return sh; |
| 2331 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2332 | static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2333 | { |
| 2334 | struct stripe_head *sh; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2335 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2336 | sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2337 | if (!sh) |
| 2338 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 2339 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 2340 | if (grow_buffers(sh, gfp)) { |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2341 | shrink_buffers(sh); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2342 | free_stripe(conf->slab_cache, sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2343 | return 0; |
| 2344 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2345 | sh->hash_lock_index = |
| 2346 | conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2347 | /* we just created an active stripe so... */ |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2348 | atomic_inc(&conf->active_stripes); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2349 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2350 | raid5_release_stripe(sh); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2351 | conf->max_nr_stripes++; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2352 | return 1; |
| 2353 | } |
| 2354 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2355 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2356 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2357 | struct kmem_cache *sc; |
Arnd Bergmann | 53b8d89 | 2018-02-20 14:09:11 +0100 | [diff] [blame] | 2358 | size_t namelen = sizeof(conf->cache_name[0]); |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2359 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2360 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2361 | if (conf->mddev->gendisk) |
Arnd Bergmann | 53b8d89 | 2018-02-20 14:09:11 +0100 | [diff] [blame] | 2362 | snprintf(conf->cache_name[0], namelen, |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2363 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 2364 | else |
Arnd Bergmann | 53b8d89 | 2018-02-20 14:09:11 +0100 | [diff] [blame] | 2365 | snprintf(conf->cache_name[0], namelen, |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2366 | "raid%d-%p", conf->level, conf->mddev); |
Arnd Bergmann | 53b8d89 | 2018-02-20 14:09:11 +0100 | [diff] [blame] | 2367 | snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]); |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2368 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2369 | conf->active_name = 0; |
| 2370 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2372 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2373 | if (!sc) |
| 2374 | return 1; |
| 2375 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2376 | conf->pool_size = devs; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2377 | while (num--) |
| 2378 | if (!grow_one_stripe(conf, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2379 | return 1; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2380 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2381 | return 0; |
| 2382 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2383 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2384 | /** |
Coly Li | 7f8a30e | 2020-04-09 22:17:22 +0800 | [diff] [blame] | 2385 | * scribble_alloc - allocate percpu scribble buffer for required size |
| 2386 | * of the scribble region |
Damien Le Moal | 2aada5b | 2020-07-16 13:54:42 +0900 | [diff] [blame] | 2387 | * @percpu: from for_each_present_cpu() of the caller |
| 2388 | * @num: total number of disks in the array |
| 2389 | * @cnt: scribble objs count for required size of the scribble region |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2390 | * |
Coly Li | 7f8a30e | 2020-04-09 22:17:22 +0800 | [diff] [blame] | 2391 | * The scribble buffer size must be enough to contain: |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2392 | * 1/ a struct page pointer for each device in the array +2 |
| 2393 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 2394 | * (dma_map_page()) or page (page_address()) address. |
| 2395 | * |
| 2396 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 2397 | * calculate over all devices (not just the data blocks), using zeros in place |
| 2398 | * of the P and Q blocks. |
| 2399 | */ |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2400 | static int scribble_alloc(struct raid5_percpu *percpu, |
Coly Li | ba54d4d | 2020-04-09 22:17:21 +0800 | [diff] [blame] | 2401 | int num, int cnt) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2402 | { |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2403 | size_t obj_size = |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2404 | sizeof(struct page *) * (num + 2) + |
| 2405 | sizeof(addr_conv_t) * (num + 2) + |
| 2406 | sizeof(unsigned int) * (num + 2); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2407 | void *scribble; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2408 | |
Coly Li | ba54d4d | 2020-04-09 22:17:21 +0800 | [diff] [blame] | 2409 | /* |
| 2410 | * If here is in raid array suspend context, it is in memalloc noio |
| 2411 | * context as well, there is no potential recursive memory reclaim |
| 2412 | * I/Os with the GFP_KERNEL flag. |
| 2413 | */ |
| 2414 | scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2415 | if (!scribble) |
| 2416 | return -ENOMEM; |
| 2417 | |
| 2418 | kvfree(percpu->scribble); |
| 2419 | |
| 2420 | percpu->scribble = scribble; |
| 2421 | percpu->scribble_obj_size = obj_size; |
| 2422 | return 0; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2423 | } |
| 2424 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2425 | static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors) |
| 2426 | { |
| 2427 | unsigned long cpu; |
| 2428 | int err = 0; |
| 2429 | |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2430 | /* |
| 2431 | * Never shrink. And mddev_suspend() could deadlock if this is called |
| 2432 | * from raid5d. In that case, scribble_disks and scribble_sectors |
| 2433 | * should equal to new_disks and new_sectors |
| 2434 | */ |
| 2435 | if (conf->scribble_disks >= new_disks && |
| 2436 | conf->scribble_sectors >= new_sectors) |
| 2437 | return 0; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2438 | mddev_suspend(conf->mddev); |
| 2439 | get_online_cpus(); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2440 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2441 | for_each_present_cpu(cpu) { |
| 2442 | struct raid5_percpu *percpu; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2443 | |
| 2444 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2445 | err = scribble_alloc(percpu, new_disks, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2446 | new_sectors / RAID5_STRIPE_SECTORS(conf)); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2447 | if (err) |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2448 | break; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2449 | } |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 2450 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2451 | put_online_cpus(); |
| 2452 | mddev_resume(conf->mddev); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2453 | if (!err) { |
| 2454 | conf->scribble_disks = new_disks; |
| 2455 | conf->scribble_sectors = new_sectors; |
| 2456 | } |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2457 | return err; |
| 2458 | } |
| 2459 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2460 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2461 | { |
| 2462 | /* Make all the stripes able to hold 'newsize' devices. |
| 2463 | * New slots in each stripe get 'page' set to a new page. |
| 2464 | * |
| 2465 | * This happens in stages: |
| 2466 | * 1/ create a new kmem_cache and allocate the required number of |
| 2467 | * stripe_heads. |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 2468 | * 2/ gather all the old stripe_heads and transfer the pages across |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2469 | * to the new stripe_heads. This will have the side effect of |
| 2470 | * freezing the array as once all stripe_heads have been collected, |
| 2471 | * no IO will be possible. Old stripe heads are freed once their |
| 2472 | * pages have been transferred over, and the old kmem_cache is |
| 2473 | * freed when all stripes are done. |
| 2474 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
Zhilong Liu | 3560741e | 2017-03-15 16:14:53 +0800 | [diff] [blame] | 2475 | * we simple return a failure status - no need to clean anything up. |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2476 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 2477 | * If this fails, we don't bother trying the shrink the |
| 2478 | * stripe_heads down again, we just leave them as they are. |
| 2479 | * As each stripe_head is processed the new one is released into |
| 2480 | * active service. |
| 2481 | * |
| 2482 | * Once step2 is started, we cannot afford to wait for a write, |
| 2483 | * so we use GFP_NOIO allocations. |
| 2484 | */ |
| 2485 | struct stripe_head *osh, *nsh; |
| 2486 | LIST_HEAD(newstripes); |
| 2487 | struct disk_info *ndisks; |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 2488 | int err = 0; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2489 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2490 | int i; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2491 | int hash, cnt; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2492 | |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 2493 | md_allow_write(conf->mddev); |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 2494 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2495 | /* Step 1 */ |
| 2496 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 2497 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2498 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2499 | if (!sc) |
| 2500 | return -ENOMEM; |
| 2501 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2502 | /* Need to ensure auto-resizing doesn't interfere */ |
| 2503 | mutex_lock(&conf->cache_size_mutex); |
| 2504 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2505 | for (i = conf->max_nr_stripes; i; i--) { |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2506 | nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2507 | if (!nsh) |
| 2508 | break; |
| 2509 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2510 | list_add(&nsh->lru, &newstripes); |
| 2511 | } |
| 2512 | if (i) { |
| 2513 | /* didn't get enough, give up */ |
| 2514 | while (!list_empty(&newstripes)) { |
| 2515 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2516 | list_del(&nsh->lru); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2517 | free_stripe(sc, nsh); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2518 | } |
| 2519 | kmem_cache_destroy(sc); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2520 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2521 | return -ENOMEM; |
| 2522 | } |
| 2523 | /* Step 2 - Must use GFP_NOIO now. |
| 2524 | * OK, we have enough stripes, start collecting inactive |
| 2525 | * stripes and copying them over |
| 2526 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2527 | hash = 0; |
| 2528 | cnt = 0; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2529 | list_for_each_entry(nsh, &newstripes, lru) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2530 | lock_device_hash_lock(conf, hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 2531 | wait_event_cmd(conf->wait_for_stripe, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2532 | !list_empty(conf->inactive_list + hash), |
| 2533 | unlock_device_hash_lock(conf, hash), |
| 2534 | lock_device_hash_lock(conf, hash)); |
| 2535 | osh = get_free_stripe(conf, hash); |
| 2536 | unlock_device_hash_lock(conf, hash); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2537 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2538 | for(i=0; i<conf->pool_size; i++) { |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2539 | nsh->dev[i].page = osh->dev[i].page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2540 | nsh->dev[i].orig_page = osh->dev[i].page; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2541 | nsh->dev[i].offset = osh->dev[i].offset; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2542 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2543 | nsh->hash_lock_index = hash; |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2544 | free_stripe(conf->slab_cache, osh); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2545 | cnt++; |
| 2546 | if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + |
| 2547 | !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { |
| 2548 | hash++; |
| 2549 | cnt = 0; |
| 2550 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2551 | } |
| 2552 | kmem_cache_destroy(conf->slab_cache); |
| 2553 | |
| 2554 | /* Step 3. |
| 2555 | * At this point, we are holding all the stripes so the array |
| 2556 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2557 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2558 | */ |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 2559 | ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2560 | if (ndisks) { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2561 | for (i = 0; i < conf->pool_size; i++) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2562 | ndisks[i] = conf->disks[i]; |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2563 | |
| 2564 | for (i = conf->pool_size; i < newsize; i++) { |
| 2565 | ndisks[i].extra_page = alloc_page(GFP_NOIO); |
| 2566 | if (!ndisks[i].extra_page) |
| 2567 | err = -ENOMEM; |
| 2568 | } |
| 2569 | |
| 2570 | if (err) { |
| 2571 | for (i = conf->pool_size; i < newsize; i++) |
| 2572 | if (ndisks[i].extra_page) |
| 2573 | put_page(ndisks[i].extra_page); |
| 2574 | kfree(ndisks); |
| 2575 | } else { |
| 2576 | kfree(conf->disks); |
| 2577 | conf->disks = ndisks; |
| 2578 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2579 | } else |
| 2580 | err = -ENOMEM; |
| 2581 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2582 | mutex_unlock(&conf->cache_size_mutex); |
Dennis Yang | 583da48 | 2017-03-29 15:46:13 +0800 | [diff] [blame] | 2583 | |
| 2584 | conf->slab_cache = sc; |
| 2585 | conf->active_name = 1-conf->active_name; |
| 2586 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2587 | /* Step 4, return new stripes to service */ |
| 2588 | while(!list_empty(&newstripes)) { |
| 2589 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2590 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2591 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2592 | for (i=conf->raid_disks; i < newsize; i++) |
| 2593 | if (nsh->dev[i].page == NULL) { |
| 2594 | struct page *p = alloc_page(GFP_NOIO); |
| 2595 | nsh->dev[i].page = p; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2596 | nsh->dev[i].orig_page = p; |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 2597 | nsh->dev[i].offset = 0; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2598 | if (!p) |
| 2599 | err = -ENOMEM; |
| 2600 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2601 | raid5_release_stripe(nsh); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2602 | } |
| 2603 | /* critical section pass, GFP_NOIO no longer needed */ |
| 2604 | |
NeilBrown | 6e9eac2 | 2015-05-08 18:19:34 +1000 | [diff] [blame] | 2605 | if (!err) |
| 2606 | conf->pool_size = newsize; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2607 | return err; |
| 2608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2610 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2611 | { |
| 2612 | struct stripe_head *sh; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 2613 | int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2615 | spin_lock_irq(conf->hash_locks + hash); |
| 2616 | sh = get_free_stripe(conf, hash); |
| 2617 | spin_unlock_irq(conf->hash_locks + hash); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2618 | if (!sh) |
| 2619 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2620 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2621 | shrink_buffers(sh); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 2622 | free_stripe(conf->slab_cache, sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2623 | atomic_dec(&conf->active_stripes); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2624 | conf->max_nr_stripes--; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2625 | return 1; |
| 2626 | } |
| 2627 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2628 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2629 | { |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2630 | while (conf->max_nr_stripes && |
| 2631 | drop_one_stripe(conf)) |
| 2632 | ; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2633 | |
Julia Lawall | 644df1a | 2015-09-13 14:15:10 +0200 | [diff] [blame] | 2634 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2635 | conf->slab_cache = NULL; |
| 2636 | } |
| 2637 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2638 | static void raid5_end_read_request(struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2640 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2641 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2642 | int disks = sh->disks, i; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2643 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2644 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2645 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | |
| 2647 | for (i=0 ; i<disks; i++) |
| 2648 | if (bi == &sh->dev[i].req) |
| 2649 | break; |
| 2650 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2651 | pr_debug("end_read_request %llu/%d, count: %d, error %d.\n", |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2652 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2653 | bi->bi_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2654 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2655 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2657 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2658 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2659 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2660 | /* If replacement finished while this request was outstanding, |
| 2661 | * 'replacement' might be NULL already. |
| 2662 | * In that case it moved down to 'rdev'. |
| 2663 | * rdev is not removed until all requests are finished. |
| 2664 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2665 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2666 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2667 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2668 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2669 | if (use_new_offset(conf, sh)) |
| 2670 | s = sh->sector + rdev->new_data_offset; |
| 2671 | else |
| 2672 | s = sh->sector + rdev->data_offset; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2673 | if (!bi->bi_status) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2674 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2675 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2676 | /* Note that this cannot happen on a |
| 2677 | * replacement device. We just fail those on |
| 2678 | * any error |
| 2679 | */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2680 | pr_info_ratelimited( |
| 2681 | "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] | 2682 | mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2683 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2684 | bdevname(rdev->bdev, b)); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2685 | atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2686 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2687 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2688 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2689 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2690 | |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 2691 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 2692 | /* |
| 2693 | * end read for a page in journal, this |
| 2694 | * must be preparing for prexor in rmw |
| 2695 | */ |
| 2696 | set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); |
| 2697 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2698 | if (atomic_read(&rdev->read_errors)) |
| 2699 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2700 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2701 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2702 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2703 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2704 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2705 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
Nigel Croxon | b76b471 | 2019-09-06 09:21:33 -0400 | [diff] [blame] | 2706 | if (!(bi->bi_status == BLK_STS_PROTECTION)) |
| 2707 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2708 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2709 | pr_warn_ratelimited( |
| 2710 | "md/raid:%s: read error on replacement device (sector %llu on %s).\n", |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2711 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2712 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2713 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2714 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 2715 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2716 | pr_warn_ratelimited( |
| 2717 | "md/raid:%s: read error not correctable (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2718 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2719 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2720 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2721 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2722 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2723 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2724 | pr_warn_ratelimited( |
| 2725 | "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2726 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2727 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2728 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2729 | } else if (atomic_read(&rdev->read_errors) |
Nigel Croxon | 0009fad | 2019-08-21 09:27:08 -0400 | [diff] [blame] | 2730 | > conf->max_nr_stripes) { |
| 2731 | if (!test_bit(Faulty, &rdev->flags)) { |
| 2732 | pr_warn("md/raid:%s: %d read_errors > %d stripes\n", |
| 2733 | mdname(conf->mddev), |
| 2734 | atomic_read(&rdev->read_errors), |
| 2735 | conf->max_nr_stripes); |
| 2736 | pr_warn("md/raid:%s: Too many read errors, failing device %s.\n", |
| 2737 | mdname(conf->mddev), bdn); |
| 2738 | } |
| 2739 | } else |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2740 | retry = 1; |
Bian Yu | edfa1f6 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2741 | if (set_bad && test_bit(In_sync, &rdev->flags) |
| 2742 | && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2743 | retry = 1; |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2744 | if (retry) |
Xiao Ni | 143f6e7 | 2019-07-08 10:14:32 +0800 | [diff] [blame] | 2745 | if (sh->qd_idx >= 0 && sh->pd_idx == i) |
| 2746 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 2747 | else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2748 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 2749 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2750 | } else |
| 2751 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2752 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2753 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2754 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2755 | if (!(set_bad |
| 2756 | && test_bit(In_sync, &rdev->flags) |
| 2757 | && rdev_set_badblocks( |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2758 | rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0))) |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2759 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2760 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2761 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2762 | rdev_dec_pending(rdev, conf->mddev); |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2763 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2764 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 2765 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2766 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2767 | } |
| 2768 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2769 | static void raid5_end_write_request(struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2770 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2771 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2772 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2773 | int disks = sh->disks, i; |
Kees Cook | 3f649ab | 2020-06-03 13:09:38 -0700 | [diff] [blame] | 2774 | struct md_rdev *rdev; |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2775 | sector_t first_bad; |
| 2776 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2777 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2778 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2779 | for (i = 0 ; i < disks; i++) { |
| 2780 | if (bi == &sh->dev[i].req) { |
| 2781 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2782 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2783 | } |
| 2784 | if (bi == &sh->dev[i].rreq) { |
| 2785 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2786 | if (rdev) |
| 2787 | replacement = 1; |
| 2788 | else |
| 2789 | /* rdev was removed and 'replacement' |
| 2790 | * replaced it. rdev is not removed |
| 2791 | * until all requests are finished. |
| 2792 | */ |
| 2793 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2794 | break; |
| 2795 | } |
| 2796 | } |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2797 | pr_debug("end_write_request %llu/%d, count %d, error: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2798 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2799 | bi->bi_status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2800 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2801 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2803 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2804 | } |
| 2805 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2806 | if (replacement) { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2807 | if (bi->bi_status) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2808 | md_error(conf->mddev, rdev); |
| 2809 | else if (is_badblock(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2810 | RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2811 | &first_bad, &bad_sectors)) |
| 2812 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 2813 | } else { |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2814 | if (bi->bi_status) { |
NeilBrown | 9f97e4b | 2014-01-16 09:35:38 +1100 | [diff] [blame] | 2815 | set_bit(STRIPE_DEGRADED, &sh->state); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2816 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2817 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2818 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2819 | set_bit(MD_RECOVERY_NEEDED, |
| 2820 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2821 | } else if (is_badblock(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 2822 | RAID5_STRIPE_SECTORS(conf), |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2823 | &first_bad, &bad_sectors)) { |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2824 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2825 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
| 2826 | /* That was a successful write so make |
| 2827 | * sure it looks like we already did |
| 2828 | * a re-write. |
| 2829 | */ |
| 2830 | set_bit(R5_ReWrite, &sh->dev[i].flags); |
| 2831 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2832 | } |
| 2833 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2834 | |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 2835 | if (sh->batch_head && bi->bi_status && !replacement) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2836 | set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state); |
| 2837 | |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2838 | bio_reset(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2839 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 2840 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2841 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2842 | raid5_release_stripe(sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2843 | |
| 2844 | if (sh->batch_head && sh != sh->batch_head) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2845 | raid5_release_stripe(sh->batch_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2846 | } |
| 2847 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 2848 | static void raid5_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2849 | { |
| 2850 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2851 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2852 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2853 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2854 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2855 | spin_lock_irqsave(&conf->device_lock, flags); |
Mariusz Tkaczyk | fb73b35 | 2018-09-04 15:08:30 +0200 | [diff] [blame] | 2856 | |
| 2857 | if (test_bit(In_sync, &rdev->flags) && |
| 2858 | mddev->degraded == conf->max_degraded) { |
| 2859 | /* |
| 2860 | * Don't allow to achieve failed state |
| 2861 | * Don't try to recover this device |
| 2862 | */ |
| 2863 | conf->recovery_disabled = mddev->recovery_disabled; |
| 2864 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2865 | return; |
| 2866 | } |
| 2867 | |
bingjingc | aff69d8 | 2017-11-17 10:57:44 +0800 | [diff] [blame] | 2868 | set_bit(Faulty, &rdev->flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2869 | clear_bit(In_sync, &rdev->flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2870 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2871 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2872 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2873 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2874 | set_bit(Blocked, &rdev->flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2875 | set_mask_bits(&mddev->sb_flags, 0, |
| 2876 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2877 | pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n" |
| 2878 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 2879 | mdname(mddev), |
| 2880 | bdevname(rdev->bdev, b), |
| 2881 | mdname(mddev), |
| 2882 | conf->raid_disks - mddev->degraded); |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 2883 | r5c_update_on_rdev_error(mddev, rdev); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2884 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2885 | |
| 2886 | /* |
| 2887 | * Input: a 'big' sector number, |
| 2888 | * Output: index of the data and parity disk, and the sector # in them. |
| 2889 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2890 | sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
| 2891 | int previous, int *dd_idx, |
| 2892 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2893 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2894 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2895 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2896 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2897 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2898 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2899 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2900 | int algorithm = previous ? conf->prev_algo |
| 2901 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2902 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2903 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2904 | int raid_disks = previous ? conf->previous_raid_disks |
| 2905 | : conf->raid_disks; |
| 2906 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2907 | |
| 2908 | /* First compute the information on this sector */ |
| 2909 | |
| 2910 | /* |
| 2911 | * Compute the chunk number and the sector offset inside the chunk |
| 2912 | */ |
| 2913 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 2914 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2915 | |
| 2916 | /* |
| 2917 | * Compute the stripe number |
| 2918 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2919 | stripe = chunk_number; |
| 2920 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2921 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2922 | /* |
| 2923 | * Select the parity disk based on the user selected algorithm. |
| 2924 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2925 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2926 | switch(conf->level) { |
| 2927 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2928 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2929 | break; |
| 2930 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2931 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2932 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2933 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2934 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2935 | (*dd_idx)++; |
| 2936 | break; |
| 2937 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2938 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2939 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2940 | (*dd_idx)++; |
| 2941 | break; |
| 2942 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2943 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2944 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2945 | break; |
| 2946 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2947 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2948 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2949 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2950 | case ALGORITHM_PARITY_0: |
| 2951 | pd_idx = 0; |
| 2952 | (*dd_idx)++; |
| 2953 | break; |
| 2954 | case ALGORITHM_PARITY_N: |
| 2955 | pd_idx = data_disks; |
| 2956 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2957 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2958 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2959 | } |
| 2960 | break; |
| 2961 | case 6: |
| 2962 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2963 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2964 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2965 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2966 | qd_idx = pd_idx + 1; |
| 2967 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2968 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2969 | qd_idx = 0; |
| 2970 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2971 | (*dd_idx) += 2; /* D D P Q D */ |
| 2972 | break; |
| 2973 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2974 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2975 | qd_idx = pd_idx + 1; |
| 2976 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2977 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2978 | qd_idx = 0; |
| 2979 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2980 | (*dd_idx) += 2; /* D D P Q D */ |
| 2981 | break; |
| 2982 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2983 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2984 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2985 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2986 | break; |
| 2987 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2988 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2989 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2990 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2991 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2992 | |
| 2993 | case ALGORITHM_PARITY_0: |
| 2994 | pd_idx = 0; |
| 2995 | qd_idx = 1; |
| 2996 | (*dd_idx) += 2; |
| 2997 | break; |
| 2998 | case ALGORITHM_PARITY_N: |
| 2999 | pd_idx = data_disks; |
| 3000 | qd_idx = data_disks + 1; |
| 3001 | break; |
| 3002 | |
| 3003 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 3004 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 3005 | * of blocks for computing Q is different. |
| 3006 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3007 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3008 | qd_idx = pd_idx + 1; |
| 3009 | if (pd_idx == raid_disks-1) { |
| 3010 | (*dd_idx)++; /* Q D D D P */ |
| 3011 | qd_idx = 0; |
| 3012 | } else if (*dd_idx >= pd_idx) |
| 3013 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3014 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3015 | break; |
| 3016 | |
| 3017 | case ALGORITHM_ROTATING_N_RESTART: |
| 3018 | /* Same a left_asymmetric, by first stripe is |
| 3019 | * D D D P Q rather than |
| 3020 | * Q D D D P |
| 3021 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3022 | stripe2 += 1; |
| 3023 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3024 | qd_idx = pd_idx + 1; |
| 3025 | if (pd_idx == raid_disks-1) { |
| 3026 | (*dd_idx)++; /* Q D D D P */ |
| 3027 | qd_idx = 0; |
| 3028 | } else if (*dd_idx >= pd_idx) |
| 3029 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3030 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3031 | break; |
| 3032 | |
| 3033 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 3034 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3035 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3036 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 3037 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3038 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3039 | break; |
| 3040 | |
| 3041 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 3042 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3043 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3044 | if (*dd_idx >= pd_idx) |
| 3045 | (*dd_idx)++; |
| 3046 | qd_idx = raid_disks - 1; |
| 3047 | break; |
| 3048 | |
| 3049 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3050 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3051 | if (*dd_idx >= pd_idx) |
| 3052 | (*dd_idx)++; |
| 3053 | qd_idx = raid_disks - 1; |
| 3054 | break; |
| 3055 | |
| 3056 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3057 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3058 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 3059 | qd_idx = raid_disks - 1; |
| 3060 | break; |
| 3061 | |
| 3062 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 3063 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3064 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 3065 | qd_idx = raid_disks - 1; |
| 3066 | break; |
| 3067 | |
| 3068 | case ALGORITHM_PARITY_0_6: |
| 3069 | pd_idx = 0; |
| 3070 | (*dd_idx)++; |
| 3071 | qd_idx = raid_disks - 1; |
| 3072 | break; |
| 3073 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3074 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3075 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3076 | } |
| 3077 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3078 | } |
| 3079 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3080 | if (sh) { |
| 3081 | sh->pd_idx = pd_idx; |
| 3082 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3083 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3084 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3085 | /* |
| 3086 | * Finally, compute the new sector number |
| 3087 | */ |
| 3088 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 3089 | return new_sector; |
| 3090 | } |
| 3091 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 3092 | 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] | 3093 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3094 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 3095 | int raid_disks = sh->disks; |
| 3096 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3097 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 3098 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 3099 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 3100 | int algorithm = previous ? conf->prev_algo |
| 3101 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3102 | sector_t stripe; |
| 3103 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 3104 | sector_t chunk_number; |
| 3105 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3106 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3107 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3108 | |
| 3109 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 3110 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3111 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3112 | if (i == sh->pd_idx) |
| 3113 | return 0; |
| 3114 | switch(conf->level) { |
| 3115 | case 4: break; |
| 3116 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 3117 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3118 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 3119 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 3120 | if (i > sh->pd_idx) |
| 3121 | i--; |
| 3122 | break; |
| 3123 | case ALGORITHM_LEFT_SYMMETRIC: |
| 3124 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 3125 | if (i < sh->pd_idx) |
| 3126 | i += raid_disks; |
| 3127 | i -= (sh->pd_idx + 1); |
| 3128 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3129 | case ALGORITHM_PARITY_0: |
| 3130 | i -= 1; |
| 3131 | break; |
| 3132 | case ALGORITHM_PARITY_N: |
| 3133 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3134 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3135 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3136 | } |
| 3137 | break; |
| 3138 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3139 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3140 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 3141 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3142 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 3143 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3144 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 3145 | case ALGORITHM_ROTATING_N_RESTART: |
| 3146 | if (sh->pd_idx == raid_disks-1) |
| 3147 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3148 | else if (i > sh->pd_idx) |
| 3149 | i -= 2; /* D D P Q D */ |
| 3150 | break; |
| 3151 | case ALGORITHM_LEFT_SYMMETRIC: |
| 3152 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 3153 | if (sh->pd_idx == raid_disks-1) |
| 3154 | i--; /* Q D D D P */ |
| 3155 | else { |
| 3156 | /* D D P Q D */ |
| 3157 | if (i < sh->pd_idx) |
| 3158 | i += raid_disks; |
| 3159 | i -= (sh->pd_idx + 2); |
| 3160 | } |
| 3161 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3162 | case ALGORITHM_PARITY_0: |
| 3163 | i -= 2; |
| 3164 | break; |
| 3165 | case ALGORITHM_PARITY_N: |
| 3166 | break; |
| 3167 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 3168 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3169 | if (sh->pd_idx == 0) |
| 3170 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 3171 | else { |
| 3172 | /* D D Q P D */ |
| 3173 | if (i < sh->pd_idx) |
| 3174 | i += raid_disks; |
| 3175 | i -= (sh->pd_idx + 1); |
| 3176 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3177 | break; |
| 3178 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 3179 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 3180 | if (i > sh->pd_idx) |
| 3181 | i--; |
| 3182 | break; |
| 3183 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 3184 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 3185 | if (i < sh->pd_idx) |
| 3186 | i += data_disks + 1; |
| 3187 | i -= (sh->pd_idx + 1); |
| 3188 | break; |
| 3189 | case ALGORITHM_PARITY_0_6: |
| 3190 | i -= 1; |
| 3191 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3192 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3193 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3194 | } |
| 3195 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3196 | } |
| 3197 | |
| 3198 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 3199 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3200 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3201 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3202 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3203 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 3204 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3205 | pr_warn("md/raid:%s: compute_blocknr: map not correct\n", |
| 3206 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3207 | return 0; |
| 3208 | } |
| 3209 | return r_sector; |
| 3210 | } |
| 3211 | |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3212 | /* |
| 3213 | * There are cases where we want handle_stripe_dirtying() and |
| 3214 | * schedule_reconstruction() to delay towrite to some dev of a stripe. |
| 3215 | * |
| 3216 | * This function checks whether we want to delay the towrite. Specifically, |
| 3217 | * we delay the towrite when: |
| 3218 | * |
| 3219 | * 1. degraded stripe has a non-overwrite to the missing dev, AND this |
| 3220 | * stripe has data in journal (for other devices). |
| 3221 | * |
| 3222 | * In this case, when reading data for the non-overwrite dev, it is |
| 3223 | * necessary to handle complex rmw of write back cache (prexor with |
| 3224 | * orig_page, and xor with page). To keep read path simple, we would |
| 3225 | * like to flush data in journal to RAID disks first, so complex rmw |
| 3226 | * is handled in the write patch (handle_stripe_dirtying). |
| 3227 | * |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3228 | * 2. when journal space is critical (R5C_LOG_CRITICAL=1) |
| 3229 | * |
| 3230 | * It is important to be able to flush all stripes in raid5-cache. |
| 3231 | * Therefore, we need reserve some space on the journal device for |
| 3232 | * these flushes. If flush operation includes pending writes to the |
| 3233 | * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe |
| 3234 | * for the flush out. If we exclude these pending writes from flush |
| 3235 | * operation, we only need (conf->max_degraded + 1) pages per stripe. |
| 3236 | * Therefore, excluding pending writes in these cases enables more |
| 3237 | * efficient use of the journal device. |
| 3238 | * |
| 3239 | * Note: To make sure the stripe makes progress, we only delay |
| 3240 | * towrite for stripes with data already in journal (injournal > 0). |
| 3241 | * When LOG_CRITICAL, stripes with injournal == 0 will be sent to |
| 3242 | * no_space_stripes list. |
| 3243 | * |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 3244 | * 3. during journal failure |
| 3245 | * In journal failure, we try to flush all cached data to raid disks |
| 3246 | * based on data in stripe cache. The array is read-only to upper |
| 3247 | * layers, so we would skip all pending writes. |
| 3248 | * |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3249 | */ |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3250 | static inline bool delay_towrite(struct r5conf *conf, |
| 3251 | struct r5dev *dev, |
| 3252 | struct stripe_head_state *s) |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3253 | { |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3254 | /* case 1 above */ |
| 3255 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 3256 | !test_bit(R5_Insync, &dev->flags) && s->injournal) |
| 3257 | return true; |
| 3258 | /* case 2 above */ |
| 3259 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && |
| 3260 | s->injournal > 0) |
| 3261 | return true; |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 3262 | /* case 3 above */ |
| 3263 | if (s->log_failed && s->injournal) |
| 3264 | return true; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3265 | return false; |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3266 | } |
| 3267 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3268 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3269 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3270 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3271 | { |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3272 | 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] | 3273 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3274 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3275 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3276 | if (rcw) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3277 | /* |
| 3278 | * In some cases, handle_stripe_dirtying initially decided to |
| 3279 | * run rmw and allocates extra page for prexor. However, rcw is |
| 3280 | * cheaper later on. We need to free the extra page now, |
| 3281 | * because we won't be able to do that in ops_complete_prexor(). |
| 3282 | */ |
| 3283 | r5c_release_extra_page(sh); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3284 | |
| 3285 | for (i = disks; i--; ) { |
| 3286 | struct r5dev *dev = &sh->dev[i]; |
| 3287 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3288 | if (dev->towrite && !delay_towrite(conf, dev, s)) { |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3289 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3290 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3291 | if (!expand) |
| 3292 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3293 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3294 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 3295 | set_bit(R5_LOCKED, &dev->flags); |
| 3296 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3297 | } |
| 3298 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 3299 | /* if we are not expanding this is a proper write request, and |
| 3300 | * there will be bios with new data to be drained into the |
| 3301 | * stripe cache |
| 3302 | */ |
| 3303 | if (!expand) { |
| 3304 | if (!s->locked) |
| 3305 | /* False alarm, nothing to do */ |
| 3306 | return; |
| 3307 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 3308 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 3309 | } else |
| 3310 | sh->reconstruct_state = reconstruct_state_run; |
| 3311 | |
| 3312 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
| 3313 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3314 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3315 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3316 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3317 | } else { |
| 3318 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 3319 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3320 | BUG_ON(level == 6 && |
| 3321 | (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) || |
| 3322 | test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags)))); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3323 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3324 | for (i = disks; i--; ) { |
| 3325 | struct r5dev *dev = &sh->dev[i]; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3326 | if (i == pd_idx || i == qd_idx) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3327 | continue; |
| 3328 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3329 | if (dev->towrite && |
| 3330 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3331 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 3332 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3333 | set_bit(R5_LOCKED, &dev->flags); |
| 3334 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3335 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3336 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 3337 | set_bit(R5_LOCKED, &dev->flags); |
| 3338 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3339 | } |
| 3340 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 3341 | if (!s->locked) |
| 3342 | /* False alarm - nothing to do */ |
| 3343 | return; |
| 3344 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
| 3345 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 3346 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 3347 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3348 | } |
| 3349 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3350 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3351 | * are in flight |
| 3352 | */ |
| 3353 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 3354 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3355 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3356 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3357 | if (level == 6) { |
| 3358 | int qd_idx = sh->qd_idx; |
| 3359 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 3360 | |
| 3361 | set_bit(R5_LOCKED, &dev->flags); |
| 3362 | clear_bit(R5_UPTODATE, &dev->flags); |
| 3363 | s->locked++; |
| 3364 | } |
| 3365 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 3366 | if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page && |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 3367 | test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) && |
| 3368 | !test_bit(STRIPE_FULL_WRITE, &sh->state) && |
| 3369 | test_bit(R5_Insync, &sh->dev[pd_idx].flags)) |
| 3370 | set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request); |
| 3371 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3372 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 3373 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3374 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3375 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3376 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3377 | /* |
| 3378 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3379 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3380 | * The bi_next chain must be in order. |
| 3381 | */ |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3382 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, |
| 3383 | int forwrite, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3384 | { |
| 3385 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3386 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3387 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3388 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3389 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3390 | (unsigned long long)bi->bi_iter.bi_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3391 | (unsigned long long)sh->sector); |
| 3392 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3393 | spin_lock_irq(&sh->stripe_lock); |
Mariusz Dabrowski | 2cd259a | 2018-04-19 19:28:10 +0200 | [diff] [blame] | 3394 | sh->dev[dd_idx].write_hint = bi->bi_write_hint; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3395 | /* Don't allow new IO added to stripes in batch list */ |
| 3396 | if (sh->batch_head) |
| 3397 | goto overlap; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3398 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3399 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3400 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3401 | firstwrite = 1; |
| 3402 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3403 | bip = &sh->dev[dd_idx].toread; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3404 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { |
| 3405 | if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3406 | goto overlap; |
| 3407 | bip = & (*bip)->bi_next; |
| 3408 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3409 | if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3410 | goto overlap; |
| 3411 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 3412 | if (forwrite && raid5_has_ppl(conf)) { |
| 3413 | /* |
| 3414 | * With PPL only writes to consecutive data chunks within a |
| 3415 | * stripe are allowed because for a single stripe_head we can |
| 3416 | * only have one PPL entry at a time, which describes one data |
| 3417 | * range. Not really an overlap, but wait_for_overlap can be |
| 3418 | * used to handle this. |
| 3419 | */ |
| 3420 | sector_t sector; |
| 3421 | sector_t first = 0; |
| 3422 | sector_t last = 0; |
| 3423 | int count = 0; |
| 3424 | int i; |
| 3425 | |
| 3426 | for (i = 0; i < sh->disks; i++) { |
| 3427 | if (i != sh->pd_idx && |
| 3428 | (i == dd_idx || sh->dev[i].towrite)) { |
| 3429 | sector = sh->dev[i].sector; |
| 3430 | if (count == 0 || sector < first) |
| 3431 | first = sector; |
| 3432 | if (sector > last) |
| 3433 | last = sector; |
| 3434 | count++; |
| 3435 | } |
| 3436 | } |
| 3437 | |
| 3438 | if (first + conf->chunk_sectors * (count - 1) != last) |
| 3439 | goto overlap; |
| 3440 | } |
| 3441 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3442 | if (!forwrite || previous) |
| 3443 | clear_bit(STRIPE_BATCH_READY, &sh->state); |
| 3444 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 3445 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3446 | if (*bip) |
| 3447 | bi->bi_next = *bip; |
| 3448 | *bip = bi; |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 3449 | bio_inc_remaining(bi); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3450 | md_write_inc(conf->mddev, bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3451 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3452 | if (forwrite) { |
| 3453 | /* check if page is covered */ |
| 3454 | sector_t sector = sh->dev[dd_idx].sector; |
| 3455 | for (bi=sh->dev[dd_idx].towrite; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3456 | sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3457 | bi && bi->bi_iter.bi_sector <= sector; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3458 | bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) { |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 3459 | if (bio_end_sector(bi) >= sector) |
| 3460 | sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3461 | } |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3462 | if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf)) |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3463 | if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags)) |
| 3464 | sh->overwrite_disks++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3465 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3466 | |
| 3467 | 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] | 3468 | (unsigned long long)(*bip)->bi_iter.bi_sector, |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3469 | (unsigned long long)sh->sector, dd_idx); |
| 3470 | |
| 3471 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3472 | /* Cannot hold spinlock over bitmap_startwrite, |
| 3473 | * but must ensure this isn't added to a batch until |
| 3474 | * we have added to the bitmap and set bm_seq. |
| 3475 | * So set STRIPE_BITMAP_PENDING to prevent |
| 3476 | * batching. |
| 3477 | * If multiple add_stripe_bio() calls race here they |
| 3478 | * much all set STRIPE_BITMAP_PENDING. So only the first one |
| 3479 | * to complete "bitmap_startwrite" gets to set |
| 3480 | * STRIPE_BIT_DELAY. This is important as once a stripe |
| 3481 | * is added to a batch, STRIPE_BIT_DELAY cannot be changed |
| 3482 | * any more. |
| 3483 | */ |
| 3484 | set_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3485 | spin_unlock_irq(&sh->stripe_lock); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3486 | md_bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3487 | RAID5_STRIPE_SECTORS(conf), 0); |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3488 | spin_lock_irq(&sh->stripe_lock); |
| 3489 | clear_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3490 | if (!sh->batch_head) { |
| 3491 | sh->bm_seq = conf->seq_flush+1; |
| 3492 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 3493 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3494 | } |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3495 | spin_unlock_irq(&sh->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3496 | |
| 3497 | if (stripe_can_batch(sh)) |
| 3498 | stripe_add_to_batch_list(conf, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3499 | return 1; |
| 3500 | |
| 3501 | overlap: |
| 3502 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3503 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3504 | return 0; |
| 3505 | } |
| 3506 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3507 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3508 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3509 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3510 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3511 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3512 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 3513 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3514 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3515 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3516 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3517 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3518 | raid5_compute_sector(conf, |
| 3519 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 3520 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3521 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3522 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3523 | } |
| 3524 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3525 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3526 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3527 | struct stripe_head_state *s, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3528 | { |
| 3529 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3530 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3531 | for (i = disks; i--; ) { |
| 3532 | struct bio *bi; |
| 3533 | int bitmap_end = 0; |
| 3534 | |
| 3535 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3536 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3537 | rcu_read_lock(); |
| 3538 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 3539 | if (rdev && test_bit(In_sync, &rdev->flags) && |
| 3540 | !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3541 | atomic_inc(&rdev->nr_pending); |
| 3542 | else |
| 3543 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3544 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3545 | if (rdev) { |
| 3546 | if (!rdev_set_badblocks( |
| 3547 | rdev, |
| 3548 | sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3549 | RAID5_STRIPE_SECTORS(conf), 0)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3550 | md_error(conf->mddev, rdev); |
| 3551 | rdev_dec_pending(rdev, conf->mddev); |
| 3552 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3553 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3554 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3555 | /* fail all writes first */ |
| 3556 | bi = sh->dev[i].towrite; |
| 3557 | sh->dev[i].towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3558 | sh->overwrite_disks = 0; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3559 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 1ed850f | 2012-10-11 13:50:13 +1100 | [diff] [blame] | 3560 | if (bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3561 | bitmap_end = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3562 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 3563 | log_stripe_write_finished(sh); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3564 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3565 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3566 | wake_up(&conf->wait_for_overlap); |
| 3567 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3568 | while (bi && bi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3569 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
| 3570 | struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3571 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3572 | md_write_end(conf->mddev); |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 3573 | bio_io_error(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3574 | bi = nextbi; |
| 3575 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3576 | if (bitmap_end) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3577 | md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3578 | RAID5_STRIPE_SECTORS(conf), 0, 0); |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3579 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3580 | /* and fail all 'written' */ |
| 3581 | bi = sh->dev[i].written; |
| 3582 | sh->dev[i].written = NULL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3583 | if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { |
| 3584 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 3585 | sh->dev[i].page = sh->dev[i].orig_page; |
| 3586 | } |
| 3587 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3588 | if (bi) bitmap_end = 1; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3589 | while (bi && bi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3590 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
| 3591 | struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3592 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3593 | md_write_end(conf->mddev); |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 3594 | bio_io_error(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3595 | bi = bi2; |
| 3596 | } |
| 3597 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3598 | /* fail any reads if this device is non-operational and |
| 3599 | * the data has not reached the cache yet. |
| 3600 | */ |
| 3601 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 3602 | s->failed > conf->max_degraded && |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3603 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 3604 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3605 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3606 | bi = sh->dev[i].toread; |
| 3607 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3608 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3609 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3610 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3611 | if (bi) |
| 3612 | s->to_read--; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3613 | while (bi && bi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3614 | sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3615 | struct bio *nextbi = |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3616 | r5_next_bio(conf, bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3617 | |
Guoqing Jiang | 6308d8e | 2017-07-21 16:33:44 +0800 | [diff] [blame] | 3618 | bio_io_error(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3619 | bi = nextbi; |
| 3620 | } |
| 3621 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3622 | if (bitmap_end) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3623 | md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3624 | RAID5_STRIPE_SECTORS(conf), 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3625 | /* If we were in the middle of a write the parity block might |
| 3626 | * still be locked - so just clear all R5_LOCKED flags |
| 3627 | */ |
| 3628 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3629 | } |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3630 | s->to_write = 0; |
| 3631 | s->written = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3632 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3633 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3634 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3635 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3636 | } |
| 3637 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3638 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3639 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3640 | struct stripe_head_state *s) |
| 3641 | { |
| 3642 | int abort = 0; |
| 3643 | int i; |
| 3644 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3645 | BUG_ON(sh->batch_head); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3646 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3647 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 3648 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3649 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3650 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3651 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3652 | * Don't even need to abort as that is handled elsewhere |
| 3653 | * if needed, and not always wanted e.g. if there is a known |
| 3654 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3655 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3656 | * non-sync devices, or abort the recovery |
| 3657 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3658 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 3659 | /* During recovery devices cannot be removed, so |
| 3660 | * locking and refcounting of rdevs is not needed |
| 3661 | */ |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3662 | rcu_read_lock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3663 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3664 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3665 | if (rdev |
| 3666 | && !test_bit(Faulty, &rdev->flags) |
| 3667 | && !test_bit(In_sync, &rdev->flags) |
| 3668 | && !rdev_set_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3669 | RAID5_STRIPE_SECTORS(conf), 0)) |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3670 | abort = 1; |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3671 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3672 | if (rdev |
| 3673 | && !test_bit(Faulty, &rdev->flags) |
| 3674 | && !test_bit(In_sync, &rdev->flags) |
| 3675 | && !rdev_set_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3676 | RAID5_STRIPE_SECTORS(conf), 0)) |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3677 | abort = 1; |
| 3678 | } |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3679 | rcu_read_unlock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3680 | if (abort) |
| 3681 | conf->recovery_disabled = |
| 3682 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3683 | } |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3684 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3685 | } |
| 3686 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3687 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 3688 | { |
| 3689 | struct md_rdev *rdev; |
| 3690 | int rv = 0; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3691 | |
| 3692 | rcu_read_lock(); |
| 3693 | rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3694 | if (rdev |
| 3695 | && !test_bit(Faulty, &rdev->flags) |
| 3696 | && !test_bit(In_sync, &rdev->flags) |
| 3697 | && (rdev->recovery_offset <= sh->sector |
| 3698 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 3699 | rv = 1; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3700 | rcu_read_unlock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3701 | return rv; |
| 3702 | } |
| 3703 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3704 | static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3705 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3706 | { |
| 3707 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3708 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 3709 | &sh->dev[s->failed_num[1]] }; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3710 | int i; |
ChangSyun Peng | 45a4d8f | 2020-07-31 17:50:31 +0800 | [diff] [blame] | 3711 | bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW); |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3712 | |
NeilBrown | a79cfe1 | 2015-02-02 11:37:59 +1100 | [diff] [blame] | 3713 | |
| 3714 | if (test_bit(R5_LOCKED, &dev->flags) || |
| 3715 | test_bit(R5_UPTODATE, &dev->flags)) |
| 3716 | /* No point reading this as we already have it or have |
| 3717 | * decided to get it. |
| 3718 | */ |
| 3719 | return 0; |
| 3720 | |
| 3721 | if (dev->toread || |
| 3722 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags))) |
| 3723 | /* We need this block to directly satisfy a request */ |
| 3724 | return 1; |
| 3725 | |
| 3726 | if (s->syncing || s->expanding || |
| 3727 | (s->replacing && want_replace(sh, disk_idx))) |
| 3728 | /* When syncing, or expanding we read everything. |
| 3729 | * When replacing, we need the replaced block. |
| 3730 | */ |
| 3731 | return 1; |
| 3732 | |
| 3733 | if ((s->failed >= 1 && fdev[0]->toread) || |
| 3734 | (s->failed >= 2 && fdev[1]->toread)) |
| 3735 | /* If we want to read from a failed device, then |
| 3736 | * we need to actually read every other device. |
| 3737 | */ |
| 3738 | return 1; |
| 3739 | |
NeilBrown | a9d5695 | 2015-02-02 11:49:10 +1100 | [diff] [blame] | 3740 | /* Sometimes neither read-modify-write nor reconstruct-write |
| 3741 | * cycles can work. In those cases we read every block we |
| 3742 | * can. Then the parity-update is certain to have enough to |
| 3743 | * work with. |
| 3744 | * This can only be a problem when we need to write something, |
| 3745 | * and some device has failed. If either of those tests |
| 3746 | * fail we need look no further. |
| 3747 | */ |
| 3748 | if (!s->failed || !s->to_write) |
| 3749 | return 0; |
| 3750 | |
| 3751 | if (test_bit(R5_Insync, &dev->flags) && |
| 3752 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3753 | /* Pre-reads at not permitted until after short delay |
| 3754 | * to gather multiple requests. However if this |
Zhilong Liu | 3560741e | 2017-03-15 16:14:53 +0800 | [diff] [blame] | 3755 | * device is no Insync, the block could only be computed |
NeilBrown | a9d5695 | 2015-02-02 11:49:10 +1100 | [diff] [blame] | 3756 | * and there is no need to delay that. |
| 3757 | */ |
| 3758 | return 0; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3759 | |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3760 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3761 | if (fdev[i]->towrite && |
| 3762 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
| 3763 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3764 | /* If we have a partial write to a failed |
| 3765 | * device, then we will need to reconstruct |
| 3766 | * the content of that device, so all other |
| 3767 | * devices must be read. |
| 3768 | */ |
| 3769 | return 1; |
ChangSyun Peng | 45a4d8f | 2020-07-31 17:50:31 +0800 | [diff] [blame] | 3770 | |
| 3771 | if (s->failed >= 2 && |
| 3772 | (fdev[i]->towrite || |
| 3773 | s->failed_num[i] == sh->pd_idx || |
| 3774 | s->failed_num[i] == sh->qd_idx) && |
| 3775 | !test_bit(R5_UPTODATE, &fdev[i]->flags)) |
| 3776 | /* In max degraded raid6, If the failed disk is P, Q, |
| 3777 | * or we want to read the failed disk, we need to do |
| 3778 | * reconstruct-write. |
| 3779 | */ |
| 3780 | force_rcw = true; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3781 | } |
| 3782 | |
ChangSyun Peng | 45a4d8f | 2020-07-31 17:50:31 +0800 | [diff] [blame] | 3783 | /* If we are forced to do a reconstruct-write, because parity |
| 3784 | * cannot be trusted and we are currently recovering it, there |
| 3785 | * is extra need to be careful. |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3786 | * If one of the devices that we would need to read, because |
| 3787 | * it is not being overwritten (and maybe not written at all) |
| 3788 | * is missing/faulty, then we need to read everything we can. |
| 3789 | */ |
ChangSyun Peng | 45a4d8f | 2020-07-31 17:50:31 +0800 | [diff] [blame] | 3790 | if (!force_rcw && |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3791 | sh->sector < sh->raid_conf->mddev->recovery_cp) |
| 3792 | /* reconstruct-write isn't being forced */ |
| 3793 | return 0; |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3794 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | 10d82c5 | 2015-05-08 18:19:33 +1000 | [diff] [blame] | 3795 | if (s->failed_num[i] != sh->pd_idx && |
| 3796 | s->failed_num[i] != sh->qd_idx && |
| 3797 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3798 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3799 | return 1; |
| 3800 | } |
| 3801 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3802 | return 0; |
| 3803 | } |
| 3804 | |
Song Liu | ba02684 | 2017-01-12 17:22:42 -0800 | [diff] [blame] | 3805 | /* fetch_block - checks the given member device to see if its data needs |
| 3806 | * to be read or computed to satisfy a request. |
| 3807 | * |
| 3808 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 3809 | * 0 to tell the loop in handle_stripe_fill to continue |
| 3810 | */ |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3811 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3812 | int disk_idx, int disks) |
| 3813 | { |
| 3814 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 3815 | |
| 3816 | /* is the data in this block needed, and can we get it? */ |
| 3817 | if (need_this_block(sh, s, disk_idx, disks)) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3818 | /* we would like to get this block, possibly by computing it, |
| 3819 | * otherwise read it if the backing disk is insync |
| 3820 | */ |
| 3821 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 3822 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
NeilBrown | b0c783b | 2015-05-08 18:19:32 +1000 | [diff] [blame] | 3823 | BUG_ON(sh->batch_head); |
NeilBrown | 7471fb7 | 2017-04-03 12:11:32 +1000 | [diff] [blame] | 3824 | |
| 3825 | /* |
| 3826 | * In the raid6 case if the only non-uptodate disk is P |
| 3827 | * then we already trusted P to compute the other failed |
| 3828 | * drives. It is safe to compute rather than re-read P. |
| 3829 | * In other cases we only compute blocks from failed |
| 3830 | * devices, otherwise check/repair might fail to detect |
| 3831 | * a real inconsistency. |
| 3832 | */ |
| 3833 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3834 | if ((s->uptodate == disks - 1) && |
NeilBrown | 7471fb7 | 2017-04-03 12:11:32 +1000 | [diff] [blame] | 3835 | ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) || |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3836 | (s->failed && (disk_idx == s->failed_num[0] || |
NeilBrown | 7471fb7 | 2017-04-03 12:11:32 +1000 | [diff] [blame] | 3837 | disk_idx == s->failed_num[1])))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3838 | /* have disk failed, and we're requested to fetch it; |
| 3839 | * do compute it |
| 3840 | */ |
| 3841 | pr_debug("Computing stripe %llu block %d\n", |
| 3842 | (unsigned long long)sh->sector, disk_idx); |
| 3843 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3844 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3845 | set_bit(R5_Wantcompute, &dev->flags); |
| 3846 | sh->ops.target = disk_idx; |
| 3847 | sh->ops.target2 = -1; /* no 2nd target */ |
| 3848 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3849 | /* Careful: from this point on 'uptodate' is in the eye |
| 3850 | * of raid_run_ops which services 'compute' operations |
| 3851 | * before writes. R5_Wantcompute flags a block that will |
| 3852 | * be R5_UPTODATE by the time it is needed for a |
| 3853 | * subsequent operation. |
| 3854 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3855 | s->uptodate++; |
| 3856 | return 1; |
| 3857 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 3858 | /* Computing 2-failure is *very* expensive; only |
| 3859 | * do it if failed >= 2 |
| 3860 | */ |
| 3861 | int other; |
| 3862 | for (other = disks; other--; ) { |
| 3863 | if (other == disk_idx) |
| 3864 | continue; |
| 3865 | if (!test_bit(R5_UPTODATE, |
| 3866 | &sh->dev[other].flags)) |
| 3867 | break; |
| 3868 | } |
| 3869 | BUG_ON(other < 0); |
| 3870 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 3871 | (unsigned long long)sh->sector, |
| 3872 | disk_idx, other); |
| 3873 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3874 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3875 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 3876 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 3877 | sh->ops.target = disk_idx; |
| 3878 | sh->ops.target2 = other; |
| 3879 | s->uptodate += 2; |
| 3880 | s->req_compute = 1; |
| 3881 | return 1; |
| 3882 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 3883 | set_bit(R5_LOCKED, &dev->flags); |
| 3884 | set_bit(R5_Wantread, &dev->flags); |
| 3885 | s->locked++; |
| 3886 | pr_debug("Reading block %d (sync=%d)\n", |
| 3887 | disk_idx, s->syncing); |
| 3888 | } |
| 3889 | } |
| 3890 | |
| 3891 | return 0; |
| 3892 | } |
| 3893 | |
Damien Le Moal | 2aada5b | 2020-07-16 13:54:42 +0900 | [diff] [blame] | 3894 | /* |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3895 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3896 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3897 | static void handle_stripe_fill(struct stripe_head *sh, |
| 3898 | struct stripe_head_state *s, |
| 3899 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3900 | { |
| 3901 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3902 | |
| 3903 | /* look for blocks to read/compute, skip this if a compute |
| 3904 | * is already in flight, or if the stripe contents are in the |
| 3905 | * midst of changing due to a write |
| 3906 | */ |
| 3907 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3908 | !sh->reconstruct_state) { |
| 3909 | |
| 3910 | /* |
| 3911 | * For degraded stripe with data in journal, do not handle |
| 3912 | * read requests yet, instead, flush the stripe to raid |
| 3913 | * disks first, this avoids handling complex rmw of write |
| 3914 | * back cache (prexor with orig_page, and then xor with |
| 3915 | * page) in the read path |
| 3916 | */ |
| 3917 | if (s->injournal && s->failed) { |
| 3918 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
| 3919 | r5c_make_stripe_write_out(sh); |
| 3920 | goto out; |
| 3921 | } |
| 3922 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3923 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3924 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3925 | break; |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3926 | } |
| 3927 | out: |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3928 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3929 | } |
| 3930 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 3931 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 3932 | unsigned long handle_flags); |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3933 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3934 | * any written block on an uptodate or failed drive can be returned. |
| 3935 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 3936 | * never LOCKED, so we don't need to test 'failed' directly. |
| 3937 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3938 | static void handle_stripe_clean_event(struct r5conf *conf, |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3939 | struct stripe_head *sh, int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3940 | { |
| 3941 | int i; |
| 3942 | struct r5dev *dev; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3943 | int discard_pending = 0; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3944 | struct stripe_head *head_sh = sh; |
| 3945 | bool do_endio = false; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3946 | |
| 3947 | for (i = disks; i--; ) |
| 3948 | if (sh->dev[i].written) { |
| 3949 | dev = &sh->dev[i]; |
| 3950 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3951 | (test_bit(R5_UPTODATE, &dev->flags) || |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3952 | test_bit(R5_Discard, &dev->flags) || |
| 3953 | test_bit(R5_SkipCopy, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3954 | /* We can return any write requests */ |
| 3955 | struct bio *wbi, *wbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3956 | pr_debug("Return write for disc %d\n", i); |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 3957 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
| 3958 | clear_bit(R5_UPTODATE, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3959 | if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { |
| 3960 | WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3961 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3962 | do_endio = true; |
| 3963 | |
| 3964 | returnbi: |
| 3965 | dev->page = dev->orig_page; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3966 | wbi = dev->written; |
| 3967 | dev->written = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3968 | while (wbi && wbi->bi_iter.bi_sector < |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3969 | dev->sector + RAID5_STRIPE_SECTORS(conf)) { |
| 3970 | wbi2 = r5_next_bio(conf, wbi, dev->sector); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 3971 | md_write_end(conf->mddev); |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 3972 | bio_endio(wbi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3973 | wbi = wbi2; |
| 3974 | } |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3975 | md_bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 3976 | RAID5_STRIPE_SECTORS(conf), |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 3977 | !test_bit(STRIPE_DEGRADED, &sh->state), |
| 3978 | 0); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3979 | if (head_sh->batch_head) { |
| 3980 | sh = list_first_entry(&sh->batch_list, |
| 3981 | struct stripe_head, |
| 3982 | batch_list); |
| 3983 | if (sh != head_sh) { |
| 3984 | dev = &sh->dev[i]; |
| 3985 | goto returnbi; |
| 3986 | } |
| 3987 | } |
| 3988 | sh = head_sh; |
| 3989 | dev = &sh->dev[i]; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3990 | } else if (test_bit(R5_Discard, &dev->flags)) |
| 3991 | discard_pending = 1; |
| 3992 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 3993 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 3994 | log_stripe_write_finished(sh); |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3995 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3996 | if (!discard_pending && |
| 3997 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3998 | int hash; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3999 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 4000 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
| 4001 | if (sh->qd_idx >= 0) { |
| 4002 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 4003 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); |
| 4004 | } |
| 4005 | /* now that discard is done we can proceed with any sync */ |
| 4006 | clear_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 4007 | /* |
| 4008 | * SCSI discard will change some bio fields and the stripe has |
| 4009 | * no updated data, so remove it from hash list and the stripe |
| 4010 | * will be reinitialized |
| 4011 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4012 | unhash: |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 4013 | hash = sh->hash_lock_index; |
| 4014 | spin_lock_irq(conf->hash_locks + hash); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 4015 | remove_hash(sh); |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 4016 | spin_unlock_irq(conf->hash_locks + hash); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4017 | if (head_sh->batch_head) { |
| 4018 | sh = list_first_entry(&sh->batch_list, |
| 4019 | struct stripe_head, batch_list); |
| 4020 | if (sh != head_sh) |
| 4021 | goto unhash; |
| 4022 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4023 | sh = head_sh; |
| 4024 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4025 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
| 4026 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4027 | |
| 4028 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4029 | |
| 4030 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 4031 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 4032 | md_wakeup_thread(conf->mddev->thread); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4033 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 4034 | if (head_sh->batch_head && do_endio) |
| 4035 | break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4036 | } |
| 4037 | |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 4038 | /* |
| 4039 | * For RMW in write back cache, we need extra page in prexor to store the |
| 4040 | * old data. This page is stored in dev->orig_page. |
| 4041 | * |
| 4042 | * This function checks whether we have data for prexor. The exact logic |
| 4043 | * is: |
| 4044 | * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE) |
| 4045 | */ |
| 4046 | static inline bool uptodate_for_rmw(struct r5dev *dev) |
| 4047 | { |
| 4048 | return (test_bit(R5_UPTODATE, &dev->flags)) && |
| 4049 | (!test_bit(R5_InJournal, &dev->flags) || |
| 4050 | test_bit(R5_OrigPageUPTDODATE, &dev->flags)); |
| 4051 | } |
| 4052 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4053 | static int handle_stripe_dirtying(struct r5conf *conf, |
| 4054 | struct stripe_head *sh, |
| 4055 | struct stripe_head_state *s, |
| 4056 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4057 | { |
| 4058 | int rmw = 0, rcw = 0, i; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 4059 | sector_t recovery_cp = conf->mddev->recovery_cp; |
| 4060 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 4061 | /* Check whether resync is now happening or should start. |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 4062 | * If yes, then the array is dirty (after unclean shutdown or |
| 4063 | * initial creation), so parity in some stripes might be inconsistent. |
| 4064 | * In this case, we need to always do reconstruct-write, to ensure |
| 4065 | * that in case of drive failure or read-error correction, we |
| 4066 | * generate correct data from the parity. |
| 4067 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 4068 | if (conf->rmw_level == PARITY_DISABLE_RMW || |
NeilBrown | 26ac107 | 2015-02-18 11:35:14 +1100 | [diff] [blame] | 4069 | (recovery_cp < MaxSector && sh->sector >= recovery_cp && |
| 4070 | s->failed == 0)) { |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 4071 | /* Calculate the real rcw later - for now make it |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4072 | * look like rcw is cheaper |
| 4073 | */ |
| 4074 | rcw = 1; rmw = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 4075 | pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n", |
| 4076 | conf->rmw_level, (unsigned long long)recovery_cp, |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 4077 | (unsigned long long)sh->sector); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4078 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4079 | /* would I have to read this buffer for read_modify_write */ |
| 4080 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 4081 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 4082 | i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4083 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4084 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 4085 | !(uptodate_for_rmw(dev) || |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4086 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4087 | if (test_bit(R5_Insync, &dev->flags)) |
| 4088 | rmw++; |
| 4089 | else |
| 4090 | rmw += 2*disks; /* cannot read it */ |
| 4091 | } |
| 4092 | /* Would I have to read this buffer for reconstruct_write */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 4093 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 4094 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4095 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4096 | !(test_bit(R5_UPTODATE, &dev->flags) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4097 | test_bit(R5_Wantcompute, &dev->flags))) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 4098 | if (test_bit(R5_Insync, &dev->flags)) |
| 4099 | rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4100 | else |
| 4101 | rcw += 2*disks; |
| 4102 | } |
| 4103 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4104 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 4105 | pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n", |
| 4106 | (unsigned long long)sh->sector, sh->state, rmw, rcw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4107 | set_bit(STRIPE_HANDLE, &sh->state); |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 4108 | 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] | 4109 | /* prefer read-modify-write, but need to get some data */ |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4110 | if (conf->mddev->queue) |
| 4111 | blk_add_trace_msg(conf->mddev->queue, |
| 4112 | "raid5 rmw %llu %d", |
| 4113 | (unsigned long long)sh->sector, rmw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4114 | for (i = disks; i--; ) { |
| 4115 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4116 | if (test_bit(R5_InJournal, &dev->flags) && |
| 4117 | dev->page == dev->orig_page && |
| 4118 | !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) { |
| 4119 | /* alloc page for prexor */ |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4120 | struct page *p = alloc_page(GFP_NOIO); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4121 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4122 | if (p) { |
| 4123 | dev->orig_page = p; |
| 4124 | continue; |
| 4125 | } |
| 4126 | |
| 4127 | /* |
| 4128 | * alloc_page() failed, try use |
| 4129 | * disk_info->extra_page |
| 4130 | */ |
| 4131 | if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE, |
| 4132 | &conf->cache_state)) { |
| 4133 | r5c_use_extra_page(sh); |
| 4134 | break; |
| 4135 | } |
| 4136 | |
| 4137 | /* extra_page in use, add to delayed_list */ |
| 4138 | set_bit(STRIPE_DELAYED, &sh->state); |
| 4139 | s->waiting_extra_page = 1; |
| 4140 | return -EAGAIN; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4141 | } |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4142 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4143 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4144 | for (i = disks; i--; ) { |
| 4145 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 4146 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4147 | i == sh->pd_idx || i == sh->qd_idx || |
| 4148 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4149 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 4150 | !(uptodate_for_rmw(dev) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4151 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4152 | test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 4153 | if (test_bit(STRIPE_PREREAD_ACTIVE, |
| 4154 | &sh->state)) { |
| 4155 | pr_debug("Read_old block %d for r-m-w\n", |
| 4156 | i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4157 | set_bit(R5_LOCKED, &dev->flags); |
| 4158 | set_bit(R5_Wantread, &dev->flags); |
| 4159 | s->locked++; |
Guoqing Jiang | e3914d5 | 2020-07-28 12:01:40 +0200 | [diff] [blame] | 4160 | } else |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4161 | set_bit(STRIPE_DELAYED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4162 | } |
| 4163 | } |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4164 | } |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 4165 | 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] | 4166 | /* want reconstruct write, but need to get some data */ |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4167 | int qread =0; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4168 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4169 | for (i = disks; i--; ) { |
| 4170 | struct r5dev *dev = &sh->dev[i]; |
| 4171 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4172 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4173 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4174 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4175 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 4176 | rcw++; |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 4177 | if (test_bit(R5_Insync, &dev->flags) && |
| 4178 | test_bit(STRIPE_PREREAD_ACTIVE, |
| 4179 | &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4180 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4181 | "%d for Reconstruct\n", i); |
| 4182 | set_bit(R5_LOCKED, &dev->flags); |
| 4183 | set_bit(R5_Wantread, &dev->flags); |
| 4184 | s->locked++; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4185 | qread++; |
Guoqing Jiang | e3914d5 | 2020-07-28 12:01:40 +0200 | [diff] [blame] | 4186 | } else |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4187 | set_bit(STRIPE_DELAYED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4188 | } |
| 4189 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4190 | if (rcw && conf->mddev->queue) |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 4191 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
| 4192 | (unsigned long long)sh->sector, |
| 4193 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4194 | } |
NeilBrown | b1b02fe | 2015-02-02 10:44:29 +1100 | [diff] [blame] | 4195 | |
| 4196 | if (rcw > disks && rmw > disks && |
| 4197 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4198 | set_bit(STRIPE_DELAYED, &sh->state); |
| 4199 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4200 | /* now if nothing is locked, and if we have enough data, |
| 4201 | * we can start a write request |
| 4202 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4203 | /* since handle_stripe can be called at any time we need to handle the |
| 4204 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 4205 | * subsequent call wants to start a write request. raid_run_ops only |
| 4206 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 4207 | * simultaneously. If this is not the case then new writes need to be |
| 4208 | * held off until the compute completes. |
| 4209 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 4210 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 4211 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4212 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 4213 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4214 | return 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4215 | } |
| 4216 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4217 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4218 | struct stripe_head_state *s, int disks) |
| 4219 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4220 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4221 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4222 | BUG_ON(sh->batch_head); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4223 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4224 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4225 | switch (sh->check_state) { |
| 4226 | case check_state_idle: |
| 4227 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4228 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4229 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4230 | sh->check_state = check_state_run; |
| 4231 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4232 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4233 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4234 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 4235 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4236 | dev = &sh->dev[s->failed_num[0]]; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4237 | fallthrough; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4238 | case check_state_compute_result: |
| 4239 | sh->check_state = check_state_idle; |
| 4240 | if (!dev) |
| 4241 | dev = &sh->dev[sh->pd_idx]; |
| 4242 | |
| 4243 | /* check that a write has not made the stripe insync */ |
| 4244 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 4245 | break; |
| 4246 | |
| 4247 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4248 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 4249 | BUG_ON(s->uptodate != disks); |
| 4250 | |
| 4251 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4252 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4253 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4254 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4255 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4256 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4257 | break; |
| 4258 | case check_state_run: |
| 4259 | break; /* we will be called again upon completion */ |
| 4260 | case check_state_check_result: |
| 4261 | sh->check_state = check_state_idle; |
| 4262 | |
| 4263 | /* if a failure occurred during the check operation, leave |
| 4264 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 4265 | */ |
| 4266 | if (s->failed) |
| 4267 | break; |
| 4268 | |
| 4269 | /* handle a successful check operation, if parity is correct |
| 4270 | * we are done. Otherwise update the mismatch count and repair |
| 4271 | * parity if !MD_RECOVERY_CHECK |
| 4272 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 4273 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4274 | /* parity is correct (on disc, |
| 4275 | * not in buffer any more) |
| 4276 | */ |
| 4277 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4278 | else { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4279 | atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4280 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4281 | /* don't try to repair!! */ |
| 4282 | set_bit(STRIPE_INSYNC, &sh->state); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4283 | pr_warn_ratelimited("%s: mismatch sector in range " |
| 4284 | "%llu-%llu\n", mdname(conf->mddev), |
| 4285 | (unsigned long long) sh->sector, |
| 4286 | (unsigned long long) sh->sector + |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4287 | RAID5_STRIPE_SECTORS(conf)); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4288 | } else { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4289 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 4290 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4291 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 4292 | set_bit(R5_Wantcompute, |
| 4293 | &sh->dev[sh->pd_idx].flags); |
| 4294 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 4295 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4296 | s->uptodate++; |
| 4297 | } |
| 4298 | } |
| 4299 | break; |
| 4300 | case check_state_compute_run: |
| 4301 | break; |
| 4302 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4303 | pr_err("%s: unknown check_state: %d sector: %llu\n", |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 4304 | __func__, sh->check_state, |
| 4305 | (unsigned long long) sh->sector); |
| 4306 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4307 | } |
| 4308 | } |
| 4309 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4310 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4311 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4312 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4313 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4314 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 4315 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4316 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4317 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4318 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4319 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4320 | |
| 4321 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4322 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4323 | /* Want to check and possibly repair P and Q. |
| 4324 | * However there could be one 'failed' device, in which |
| 4325 | * case we can only check one of them, possibly using the |
| 4326 | * other to generate missing data |
| 4327 | */ |
| 4328 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4329 | switch (sh->check_state) { |
| 4330 | case check_state_idle: |
| 4331 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4332 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4333 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4334 | * makes sense to check P (If anything else were failed, |
| 4335 | * we would have used P to recreate it). |
| 4336 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4337 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4338 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4339 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4340 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4341 | * anything, so it makes sense to check it |
| 4342 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4343 | if (sh->check_state == check_state_run) |
| 4344 | sh->check_state = check_state_run_pq; |
| 4345 | else |
| 4346 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4347 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4348 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4349 | /* discard potentially stale zero_sum_result */ |
| 4350 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4351 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4352 | if (sh->check_state == check_state_run) { |
| 4353 | /* async_xor_zero_sum destroys the contents of P */ |
| 4354 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 4355 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4356 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4357 | if (sh->check_state >= check_state_run && |
| 4358 | sh->check_state <= check_state_run_pq) { |
| 4359 | /* async_syndrome_zero_sum preserves P and Q, so |
| 4360 | * no need to mark them !uptodate here |
| 4361 | */ |
| 4362 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 4363 | break; |
| 4364 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4365 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4366 | /* we have 2-disk failure */ |
| 4367 | BUG_ON(s->failed != 2); |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 4368 | fallthrough; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4369 | case check_state_compute_result: |
| 4370 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4371 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4372 | /* check that a write has not made the stripe insync */ |
| 4373 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 4374 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4375 | |
| 4376 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4377 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4378 | */ |
Nigel Croxon | b2176a1 | 2019-04-16 09:50:09 -0700 | [diff] [blame] | 4379 | dev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4380 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4381 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4382 | s->locked++; |
| 4383 | set_bit(R5_LOCKED, &dev->flags); |
| 4384 | set_bit(R5_Wantwrite, &dev->flags); |
| 4385 | } |
| 4386 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4387 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4388 | s->locked++; |
| 4389 | set_bit(R5_LOCKED, &dev->flags); |
| 4390 | set_bit(R5_Wantwrite, &dev->flags); |
| 4391 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4392 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4393 | dev = &sh->dev[pd_idx]; |
| 4394 | s->locked++; |
| 4395 | set_bit(R5_LOCKED, &dev->flags); |
| 4396 | set_bit(R5_Wantwrite, &dev->flags); |
| 4397 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4398 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4399 | dev = &sh->dev[qd_idx]; |
| 4400 | s->locked++; |
| 4401 | set_bit(R5_LOCKED, &dev->flags); |
| 4402 | set_bit(R5_Wantwrite, &dev->flags); |
| 4403 | } |
Nigel Croxon | b2176a1 | 2019-04-16 09:50:09 -0700 | [diff] [blame] | 4404 | if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags), |
| 4405 | "%s: disk%td not up to date\n", |
| 4406 | mdname(conf->mddev), |
| 4407 | dev - (struct r5dev *) &sh->dev)) { |
| 4408 | clear_bit(R5_LOCKED, &dev->flags); |
| 4409 | clear_bit(R5_Wantwrite, &dev->flags); |
| 4410 | s->locked--; |
| 4411 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4412 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 4413 | |
| 4414 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4415 | break; |
| 4416 | case check_state_run: |
| 4417 | case check_state_run_q: |
| 4418 | case check_state_run_pq: |
| 4419 | break; /* we will be called again upon completion */ |
| 4420 | case check_state_check_result: |
| 4421 | sh->check_state = check_state_idle; |
| 4422 | |
| 4423 | /* handle a successful check operation, if parity is correct |
| 4424 | * we are done. Otherwise update the mismatch count and repair |
| 4425 | * parity if !MD_RECOVERY_CHECK |
| 4426 | */ |
| 4427 | if (sh->ops.zero_sum_result == 0) { |
Song Liu | a25d8c3 | 2019-04-16 09:34:21 -0700 | [diff] [blame] | 4428 | /* both parities are correct */ |
| 4429 | if (!s->failed) |
| 4430 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4431 | else { |
| 4432 | /* in contrast to the raid5 case we can validate |
| 4433 | * parity, but still have a failure to write |
| 4434 | * back |
| 4435 | */ |
| 4436 | sh->check_state = check_state_compute_result; |
| 4437 | /* Returning at this point means that we may go |
| 4438 | * off and bring p and/or q uptodate again so |
| 4439 | * we make sure to check zero_sum_result again |
| 4440 | * to verify if p or q need writeback |
| 4441 | */ |
| 4442 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4443 | } else { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4444 | atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4445 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4446 | /* don't try to repair!! */ |
| 4447 | set_bit(STRIPE_INSYNC, &sh->state); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4448 | pr_warn_ratelimited("%s: mismatch sector in range " |
| 4449 | "%llu-%llu\n", mdname(conf->mddev), |
| 4450 | (unsigned long long) sh->sector, |
| 4451 | (unsigned long long) sh->sector + |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4452 | RAID5_STRIPE_SECTORS(conf)); |
Nix | e153903 | 2017-05-16 10:13:31 +0100 | [diff] [blame] | 4453 | } else { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4454 | int *target = &sh->ops.target; |
| 4455 | |
| 4456 | sh->ops.target = -1; |
| 4457 | sh->ops.target2 = -1; |
| 4458 | sh->check_state = check_state_compute_run; |
| 4459 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 4460 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 4461 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 4462 | set_bit(R5_Wantcompute, |
| 4463 | &sh->dev[pd_idx].flags); |
| 4464 | *target = pd_idx; |
| 4465 | target = &sh->ops.target2; |
| 4466 | s->uptodate++; |
| 4467 | } |
| 4468 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 4469 | set_bit(R5_Wantcompute, |
| 4470 | &sh->dev[qd_idx].flags); |
| 4471 | *target = qd_idx; |
| 4472 | s->uptodate++; |
| 4473 | } |
| 4474 | } |
| 4475 | } |
| 4476 | break; |
| 4477 | case check_state_compute_run: |
| 4478 | break; |
| 4479 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4480 | pr_warn("%s: unknown check_state: %d sector: %llu\n", |
| 4481 | __func__, sh->check_state, |
| 4482 | (unsigned long long) sh->sector); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4483 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4484 | } |
| 4485 | } |
| 4486 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4487 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4488 | { |
| 4489 | int i; |
| 4490 | |
| 4491 | /* We have read all the blocks in this stripe and now we need to |
| 4492 | * copy some of them into a target stripe for expand. |
| 4493 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4494 | struct dma_async_tx_descriptor *tx = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4495 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4496 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4497 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 4498 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4499 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4500 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4501 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4502 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4503 | sector_t bn = raid5_compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4504 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 4505 | &dd_idx, NULL); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4506 | sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4507 | if (sh2 == NULL) |
| 4508 | /* so far only the early blocks of this stripe |
| 4509 | * have been requested. When later blocks |
| 4510 | * get requested, we will try again |
| 4511 | */ |
| 4512 | continue; |
| 4513 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 4514 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 4515 | /* must have already done this block */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4516 | raid5_release_stripe(sh2); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4517 | continue; |
| 4518 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4519 | |
| 4520 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4521 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4522 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Yufen Yu | 7aba13b | 2020-08-20 09:22:06 -0400 | [diff] [blame] | 4523 | sh->dev[i].page, sh2->dev[dd_idx].offset, |
| 4524 | sh->dev[i].offset, RAID5_STRIPE_SIZE(conf), |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4525 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4526 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4527 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 4528 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 4529 | for (j = 0; j < conf->raid_disks; j++) |
| 4530 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4531 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4532 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 4533 | break; |
| 4534 | if (j == conf->raid_disks) { |
| 4535 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 4536 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 4537 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4538 | raid5_release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4539 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4540 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 4541 | /* done submitting copies, wait for them to complete */ |
NeilBrown | 749586b | 2012-11-20 14:11:15 +1100 | [diff] [blame] | 4542 | async_tx_quiesce(&tx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4543 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4544 | |
| 4545 | /* |
| 4546 | * handle_stripe - do things to a stripe. |
| 4547 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4548 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 4549 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4550 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4551 | * return some read requests which now have data |
| 4552 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4553 | * schedule a read on some buffers |
| 4554 | * schedule a write of some buffers |
| 4555 | * return confirmation of parity correctness |
| 4556 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4557 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4558 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4559 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4560 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4561 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4562 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4563 | struct r5dev *dev; |
| 4564 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4565 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4566 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4567 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4568 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4569 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head; |
| 4570 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4571 | s->failed_num[0] = -1; |
| 4572 | s->failed_num[1] = -1; |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 4573 | s->log_failed = r5l_log_disk_error(conf); |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4574 | |
| 4575 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4576 | rcu_read_lock(); |
| 4577 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4578 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4579 | sector_t first_bad; |
| 4580 | int bad_sectors; |
| 4581 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4582 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4583 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4584 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4585 | 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] | 4586 | i, dev->flags, |
| 4587 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4588 | /* maybe we can reply to a read |
| 4589 | * |
| 4590 | * new wantfill requests are only permitted while |
| 4591 | * ops_complete_biofill is guaranteed to be inactive |
| 4592 | */ |
| 4593 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 4594 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 4595 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4596 | |
| 4597 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4598 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 4599 | s->locked++; |
| 4600 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 4601 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4602 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4603 | s->compute++; |
| 4604 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4605 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4606 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4607 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4608 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4609 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4610 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4611 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4612 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4613 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4614 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4615 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4616 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4617 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4618 | /* Prefer to use the replacement for reads, but only |
| 4619 | * if it is recovered enough and has no bad blocks. |
| 4620 | */ |
| 4621 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 4622 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4623 | rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) && |
| 4624 | !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4625 | &first_bad, &bad_sectors)) |
| 4626 | set_bit(R5_ReadRepl, &dev->flags); |
| 4627 | else { |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4628 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4629 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4630 | else |
| 4631 | clear_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4632 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 4633 | clear_bit(R5_ReadRepl, &dev->flags); |
| 4634 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 4635 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 4636 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4637 | if (rdev) { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4638 | is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4639 | &first_bad, &bad_sectors); |
| 4640 | if (s->blocked_rdev == NULL |
| 4641 | && (test_bit(Blocked, &rdev->flags) |
| 4642 | || is_bad < 0)) { |
| 4643 | if (is_bad < 0) |
| 4644 | set_bit(BlockedBadBlocks, |
| 4645 | &rdev->flags); |
| 4646 | s->blocked_rdev = rdev; |
| 4647 | atomic_inc(&rdev->nr_pending); |
| 4648 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4649 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4650 | clear_bit(R5_Insync, &dev->flags); |
| 4651 | if (!rdev) |
| 4652 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4653 | else if (is_bad) { |
| 4654 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 4655 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 4656 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4657 | /* treat as in-sync, but with a read error |
| 4658 | * which we can now try to correct |
| 4659 | */ |
| 4660 | set_bit(R5_Insync, &dev->flags); |
| 4661 | set_bit(R5_ReadError, &dev->flags); |
| 4662 | } |
| 4663 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4664 | set_bit(R5_Insync, &dev->flags); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 4665 | else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4666 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 4667 | set_bit(R5_Insync, &dev->flags); |
| 4668 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 4669 | test_bit(R5_Expanded, &dev->flags)) |
| 4670 | /* If we've reshaped into here, we assume it is Insync. |
| 4671 | * We will shortly update recovery_offset to make |
| 4672 | * it official. |
| 4673 | */ |
| 4674 | set_bit(R5_Insync, &dev->flags); |
| 4675 | |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4676 | if (test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4677 | /* This flag does not apply to '.replacement' |
| 4678 | * only to .rdev, so make sure to check that*/ |
| 4679 | struct md_rdev *rdev2 = rcu_dereference( |
| 4680 | conf->disks[i].rdev); |
| 4681 | if (rdev2 == rdev) |
| 4682 | clear_bit(R5_Insync, &dev->flags); |
| 4683 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4684 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4685 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4686 | } else |
| 4687 | clear_bit(R5_WriteError, &dev->flags); |
| 4688 | } |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4689 | if (test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4690 | /* This flag does not apply to '.replacement' |
| 4691 | * only to .rdev, so make sure to check that*/ |
| 4692 | struct md_rdev *rdev2 = rcu_dereference( |
| 4693 | conf->disks[i].rdev); |
| 4694 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4695 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4696 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4697 | } else |
| 4698 | clear_bit(R5_MadeGood, &dev->flags); |
| 4699 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4700 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4701 | struct md_rdev *rdev2 = rcu_dereference( |
| 4702 | conf->disks[i].replacement); |
| 4703 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 4704 | s->handle_bad_blocks = 1; |
| 4705 | atomic_inc(&rdev2->nr_pending); |
| 4706 | } else |
| 4707 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 4708 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4709 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4710 | /* The ReadError flag will just be confusing now */ |
| 4711 | clear_bit(R5_ReadError, &dev->flags); |
| 4712 | clear_bit(R5_ReWrite, &dev->flags); |
| 4713 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4714 | if (test_bit(R5_ReadError, &dev->flags)) |
| 4715 | clear_bit(R5_Insync, &dev->flags); |
| 4716 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4717 | if (s->failed < 2) |
| 4718 | s->failed_num[s->failed] = i; |
| 4719 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4720 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 4721 | do_recovery = 1; |
BingJing Chang | d63e2fc | 2018-08-01 17:08:36 +0800 | [diff] [blame] | 4722 | else if (!rdev) { |
| 4723 | rdev = rcu_dereference( |
| 4724 | conf->disks[i].replacement); |
| 4725 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 4726 | do_recovery = 1; |
| 4727 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4728 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4729 | |
| 4730 | if (test_bit(R5_InJournal, &dev->flags)) |
| 4731 | s->injournal++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4732 | if (test_bit(R5_InJournal, &dev->flags) && dev->written) |
| 4733 | s->just_cached++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4734 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4735 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 4736 | /* If there is a failed device being replaced, |
| 4737 | * we must be recovering. |
| 4738 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4739 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4740 | * else we can only be replacing |
| 4741 | * sync and recovery both need to read all devices, and so |
| 4742 | * use the same flag. |
| 4743 | */ |
| 4744 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4745 | sh->sector >= conf->mddev->recovery_cp || |
| 4746 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4747 | s->syncing = 1; |
| 4748 | else |
| 4749 | s->replacing = 1; |
| 4750 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4751 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4752 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4753 | |
Guoqing Jiang | cb9902d | 2020-06-16 11:25:51 +0200 | [diff] [blame] | 4754 | /* |
| 4755 | * Return '1' if this is a member of batch, or '0' if it is a lone stripe or |
| 4756 | * a head which can now be handled. |
| 4757 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4758 | static int clear_batch_ready(struct stripe_head *sh) |
| 4759 | { |
| 4760 | struct stripe_head *tmp; |
| 4761 | if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state)) |
NeilBrown | b15a9db | 2015-05-22 15:20:04 +1000 | [diff] [blame] | 4762 | return (sh->batch_head && sh->batch_head != sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4763 | spin_lock(&sh->stripe_lock); |
| 4764 | if (!sh->batch_head) { |
| 4765 | spin_unlock(&sh->stripe_lock); |
| 4766 | return 0; |
| 4767 | } |
| 4768 | |
| 4769 | /* |
| 4770 | * this stripe could be added to a batch list before we check |
| 4771 | * BATCH_READY, skips it |
| 4772 | */ |
| 4773 | if (sh->batch_head != sh) { |
| 4774 | spin_unlock(&sh->stripe_lock); |
| 4775 | return 1; |
| 4776 | } |
| 4777 | spin_lock(&sh->batch_lock); |
| 4778 | list_for_each_entry(tmp, &sh->batch_list, batch_list) |
| 4779 | clear_bit(STRIPE_BATCH_READY, &tmp->state); |
| 4780 | spin_unlock(&sh->batch_lock); |
| 4781 | spin_unlock(&sh->stripe_lock); |
| 4782 | |
| 4783 | /* |
| 4784 | * BATCH_READY is cleared, no new stripes can be added. |
| 4785 | * batch_list can be accessed without lock |
| 4786 | */ |
| 4787 | return 0; |
| 4788 | } |
| 4789 | |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4790 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 4791 | unsigned long handle_flags) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4792 | { |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4793 | struct stripe_head *sh, *next; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4794 | int i; |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4795 | int do_wakeup = 0; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4796 | |
NeilBrown | bb27051 | 2015-05-08 18:19:40 +1000 | [diff] [blame] | 4797 | list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) { |
| 4798 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4799 | list_del_init(&sh->batch_list); |
| 4800 | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4801 | WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4802 | (1 << STRIPE_SYNCING) | |
| 4803 | (1 << STRIPE_REPLACED) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4804 | (1 << STRIPE_DELAYED) | |
| 4805 | (1 << STRIPE_BIT_DELAY) | |
| 4806 | (1 << STRIPE_FULL_WRITE) | |
| 4807 | (1 << STRIPE_BIOFILL_RUN) | |
| 4808 | (1 << STRIPE_COMPUTE_RUN) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4809 | (1 << STRIPE_DISCARD) | |
| 4810 | (1 << STRIPE_BATCH_READY) | |
| 4811 | (1 << STRIPE_BATCH_ERR) | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4812 | (1 << STRIPE_BITMAP_PENDING)), |
| 4813 | "stripe state: %lx\n", sh->state); |
| 4814 | WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) | |
| 4815 | (1 << STRIPE_REPLACED)), |
| 4816 | "head stripe state: %lx\n", head_sh->state); |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4817 | |
| 4818 | set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS | |
NeilBrown | 550da24 | 2016-03-09 12:58:25 +1100 | [diff] [blame] | 4819 | (1 << STRIPE_PREREAD_ACTIVE) | |
Dennis Yang | 184a09e | 2017-09-06 11:02:35 +0800 | [diff] [blame] | 4820 | (1 << STRIPE_DEGRADED) | |
| 4821 | (1 << STRIPE_ON_UNPLUG_LIST)), |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4822 | head_sh->state & (1 << STRIPE_INSYNC)); |
| 4823 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4824 | sh->check_state = head_sh->check_state; |
| 4825 | sh->reconstruct_state = head_sh->reconstruct_state; |
Amy Chiang | 448ec63 | 2018-05-16 18:59:35 +0800 | [diff] [blame] | 4826 | spin_lock_irq(&sh->stripe_lock); |
| 4827 | sh->batch_head = NULL; |
| 4828 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4829 | for (i = 0; i < sh->disks; i++) { |
| 4830 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 4831 | do_wakeup = 1; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4832 | sh->dev[i].flags = head_sh->dev[i].flags & |
| 4833 | (~((1 << R5_WriteError) | (1 << R5_Overlap))); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4834 | } |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4835 | if (handle_flags == 0 || |
| 4836 | sh->state & handle_flags) |
| 4837 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4838 | raid5_release_stripe(sh); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4839 | } |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4840 | spin_lock_irq(&head_sh->stripe_lock); |
| 4841 | head_sh->batch_head = NULL; |
| 4842 | spin_unlock_irq(&head_sh->stripe_lock); |
| 4843 | for (i = 0; i < head_sh->disks; i++) |
| 4844 | if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags)) |
| 4845 | do_wakeup = 1; |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4846 | if (head_sh->state & handle_flags) |
| 4847 | set_bit(STRIPE_HANDLE, &head_sh->state); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4848 | |
| 4849 | if (do_wakeup) |
| 4850 | wake_up(&head_sh->raid_conf->wait_for_overlap); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4851 | } |
| 4852 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4853 | static void handle_stripe(struct stripe_head *sh) |
| 4854 | { |
| 4855 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4856 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4857 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4858 | int prexor; |
| 4859 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4860 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4861 | |
| 4862 | clear_bit(STRIPE_HANDLE, &sh->state); |
Guoqing Jiang | a377a47 | 2020-06-16 11:25:50 +0200 | [diff] [blame] | 4863 | |
| 4864 | /* |
| 4865 | * handle_stripe should not continue handle the batched stripe, only |
| 4866 | * the head of batch list or lone stripe can continue. Otherwise we |
| 4867 | * could see break_stripe_batch_list warns about the STRIPE_ACTIVE |
| 4868 | * is set for the batched stripe. |
| 4869 | */ |
| 4870 | if (clear_batch_ready(sh)) |
| 4871 | return; |
| 4872 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 4873 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4874 | /* already being handled, ensure it gets handled |
| 4875 | * again when current action finishes */ |
| 4876 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4877 | return; |
| 4878 | } |
| 4879 | |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4880 | if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state)) |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4881 | break_stripe_batch_list(sh, 0); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4882 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4883 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) { |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4884 | spin_lock(&sh->stripe_lock); |
Song Liu | 5ddf044 | 2017-05-11 17:03:44 -0700 | [diff] [blame] | 4885 | /* |
| 4886 | * Cannot process 'sync' concurrently with 'discard'. |
| 4887 | * Flush data in r5cache before 'sync'. |
| 4888 | */ |
| 4889 | if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) && |
| 4890 | !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) && |
| 4891 | !test_bit(STRIPE_DISCARD, &sh->state) && |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4892 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 4893 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4894 | clear_bit(STRIPE_INSYNC, &sh->state); |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4895 | clear_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4896 | } |
| 4897 | spin_unlock(&sh->stripe_lock); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4898 | } |
| 4899 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4900 | |
| 4901 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 4902 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 4903 | (unsigned long long)sh->sector, sh->state, |
| 4904 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 4905 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4906 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4907 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4908 | |
Shaohua Li | b70abcb | 2015-08-13 14:31:58 -0700 | [diff] [blame] | 4909 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) |
| 4910 | goto finish; |
| 4911 | |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 4912 | if (s.handle_bad_blocks || |
| 4913 | test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4914 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4915 | goto finish; |
| 4916 | } |
| 4917 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4918 | if (unlikely(s.blocked_rdev)) { |
| 4919 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4920 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4921 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4922 | goto finish; |
| 4923 | } |
| 4924 | /* There is nothing for the blocked_rdev to block */ |
| 4925 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 4926 | s.blocked_rdev = NULL; |
| 4927 | } |
| 4928 | |
| 4929 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 4930 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 4931 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 4932 | } |
| 4933 | |
| 4934 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 4935 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 4936 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 4937 | s.failed_num[0], s.failed_num[1]); |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 4938 | /* |
| 4939 | * check if the array has lost more than max_degraded devices and, |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4940 | * if so, some requests might need to be failed. |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 4941 | * |
| 4942 | * When journal device failed (log_failed), we will only process |
| 4943 | * the stripe if there is data need write to raid disks |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4944 | */ |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 4945 | if (s.failed > conf->max_degraded || |
| 4946 | (s.log_failed && s.injournal == 0)) { |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4947 | sh->check_state = 0; |
| 4948 | sh->reconstruct_state = 0; |
NeilBrown | 626f209 | 2015-05-22 14:03:10 +1000 | [diff] [blame] | 4949 | break_stripe_batch_list(sh, 0); |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4950 | if (s.to_read+s.to_write+s.written) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 4951 | handle_failed_stripe(conf, sh, &s, disks); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4952 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4953 | handle_failed_sync(conf, sh, &s); |
| 4954 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4955 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4956 | /* Now we check to see if any write operations have recently |
| 4957 | * completed |
| 4958 | */ |
| 4959 | prexor = 0; |
| 4960 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 4961 | prexor = 1; |
| 4962 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 4963 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 4964 | sh->reconstruct_state = reconstruct_state_idle; |
| 4965 | |
| 4966 | /* All the 'written' buffers and the parity block are ready to |
| 4967 | * be written back to disk |
| 4968 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4969 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 4970 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4971 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4972 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 4973 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4974 | for (i = disks; i--; ) { |
| 4975 | struct r5dev *dev = &sh->dev[i]; |
| 4976 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 4977 | (i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4978 | dev->written || test_bit(R5_InJournal, |
| 4979 | &dev->flags))) { |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4980 | pr_debug("Writing block %d\n", i); |
| 4981 | set_bit(R5_Wantwrite, &dev->flags); |
| 4982 | if (prexor) |
| 4983 | continue; |
NeilBrown | 9c4bdf6 | 2014-08-13 09:57:07 +1000 | [diff] [blame] | 4984 | if (s.failed > 1) |
| 4985 | continue; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4986 | if (!test_bit(R5_Insync, &dev->flags) || |
| 4987 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 4988 | s.failed == 0)) |
| 4989 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4990 | } |
| 4991 | } |
| 4992 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4993 | s.dec_preread_active = 1; |
| 4994 | } |
| 4995 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 4996 | /* |
| 4997 | * might be able to return some write requests if the parity blocks |
| 4998 | * are safe, or on a failed drive |
| 4999 | */ |
| 5000 | pdev = &sh->dev[sh->pd_idx]; |
| 5001 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 5002 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 5003 | qdev = &sh->dev[sh->qd_idx]; |
| 5004 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 5005 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 5006 | || conf->level < 6; |
| 5007 | |
| 5008 | if (s.written && |
| 5009 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 5010 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 5011 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 5012 | test_bit(R5_Discard, &pdev->flags))))) && |
| 5013 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 5014 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 5015 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 5016 | test_bit(R5_Discard, &qdev->flags)))))) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5017 | handle_stripe_clean_event(conf, sh, disks); |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 5018 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 5019 | if (s.just_cached) |
NeilBrown | bd83d0a | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5020 | r5c_handle_cached_data_endio(conf, sh, disks); |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 5021 | log_stripe_write_finished(sh); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 5022 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 5023 | /* Now we might consider reading some blocks, either to check/generate |
| 5024 | * parity, or to satisfy requests |
| 5025 | * or to load a block that is being partially written. |
| 5026 | */ |
| 5027 | if (s.to_read || s.non_overwrite |
ChangSyun Peng | a1c6ae3 | 2020-07-31 17:50:17 +0800 | [diff] [blame] | 5028 | || (s.to_write && s.failed) |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 5029 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 5030 | || s.replacing |
| 5031 | || s.expanding) |
| 5032 | handle_stripe_fill(sh, &s, disks); |
| 5033 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5034 | /* |
| 5035 | * When the stripe finishes full journal write cycle (write to journal |
| 5036 | * and raid disk), this is the clean up procedure so it is ready for |
| 5037 | * next operation. |
| 5038 | */ |
| 5039 | r5c_finish_stripe_write_out(conf, sh, &s); |
| 5040 | |
| 5041 | /* |
| 5042 | * Now to consider new write requests, cache write back and what else, |
| 5043 | * if anything should be read. We do not handle new writes when: |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5044 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 5045 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 5046 | * block. |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5047 | * 3/ A r5c cache log write is in flight. |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5048 | */ |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5049 | |
| 5050 | if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) { |
| 5051 | if (!r5c_is_writeback(conf->log)) { |
| 5052 | if (s.to_write) |
| 5053 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 5054 | } else { /* write back cache */ |
| 5055 | int ret = 0; |
| 5056 | |
| 5057 | /* First, try handle writes in caching phase */ |
| 5058 | if (s.to_write) |
| 5059 | ret = r5c_try_caching_write(conf, sh, &s, |
| 5060 | disks); |
| 5061 | /* |
| 5062 | * If caching phase failed: ret == -EAGAIN |
| 5063 | * OR |
| 5064 | * stripe under reclaim: !caching && injournal |
| 5065 | * |
| 5066 | * fall back to handle_stripe_dirtying() |
| 5067 | */ |
| 5068 | if (ret == -EAGAIN || |
| 5069 | /* stripe under reclaim: !caching && injournal */ |
| 5070 | (!test_bit(STRIPE_R5C_CACHING, &sh->state) && |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 5071 | s.injournal > 0)) { |
| 5072 | ret = handle_stripe_dirtying(conf, sh, &s, |
| 5073 | disks); |
| 5074 | if (ret == -EAGAIN) |
| 5075 | goto finish; |
| 5076 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5077 | } |
| 5078 | } |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5079 | |
| 5080 | /* maybe we need to check and possibly fix the parity for this stripe |
| 5081 | * Any reads will already have been scheduled, so we just see if enough |
| 5082 | * data is available. The parity check is held off while parity |
| 5083 | * dependent operations are in flight. |
| 5084 | */ |
| 5085 | if (sh->check_state || |
| 5086 | (s.syncing && s.locked == 0 && |
| 5087 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 5088 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 5089 | if (conf->level == 6) |
| 5090 | handle_parity_checks6(conf, sh, &s, disks); |
| 5091 | else |
| 5092 | handle_parity_checks5(conf, sh, &s, disks); |
| 5093 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5094 | |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 5095 | if ((s.replacing || s.syncing) && s.locked == 0 |
| 5096 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) |
| 5097 | && !test_bit(STRIPE_REPLACED, &sh->state)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5098 | /* Write out to replacement devices where possible */ |
| 5099 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 5100 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 5101 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5102 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 5103 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 5104 | s.locked++; |
| 5105 | } |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 5106 | if (s.replacing) |
| 5107 | set_bit(STRIPE_INSYNC, &sh->state); |
| 5108 | set_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5109 | } |
| 5110 | if ((s.syncing || s.replacing) && s.locked == 0 && |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 5111 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5112 | test_bit(STRIPE_INSYNC, &sh->state)) { |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5113 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5114 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5115 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 5116 | wake_up(&conf->wait_for_overlap); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5117 | } |
| 5118 | |
| 5119 | /* If the failed drives are just a ReadError, then we might need |
| 5120 | * to progress the repair/check process |
| 5121 | */ |
| 5122 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 5123 | for (i = 0; i < s.failed; i++) { |
| 5124 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 5125 | if (test_bit(R5_ReadError, &dev->flags) |
| 5126 | && !test_bit(R5_LOCKED, &dev->flags) |
| 5127 | && test_bit(R5_UPTODATE, &dev->flags) |
| 5128 | ) { |
| 5129 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 5130 | set_bit(R5_Wantwrite, &dev->flags); |
| 5131 | set_bit(R5_ReWrite, &dev->flags); |
Guoqing Jiang | 3a31cf3 | 2020-07-28 12:01:43 +0200 | [diff] [blame] | 5132 | } else |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5133 | /* let's read it back */ |
| 5134 | set_bit(R5_Wantread, &dev->flags); |
Guoqing Jiang | 3a31cf3 | 2020-07-28 12:01:43 +0200 | [diff] [blame] | 5135 | set_bit(R5_LOCKED, &dev->flags); |
| 5136 | s.locked++; |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5137 | } |
| 5138 | } |
| 5139 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5140 | /* Finish reconstruct operations initiated by the expansion process */ |
| 5141 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 5142 | struct stripe_head *sh_src |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5143 | = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5144 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 5145 | /* sh cannot be written until sh_src has been read. |
| 5146 | * so arrange for sh to be delayed a little |
| 5147 | */ |
| 5148 | set_bit(STRIPE_DELAYED, &sh->state); |
| 5149 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5150 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 5151 | &sh_src->state)) |
| 5152 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5153 | raid5_release_stripe(sh_src); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5154 | goto finish; |
| 5155 | } |
| 5156 | if (sh_src) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5157 | raid5_release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5158 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5159 | sh->reconstruct_state = reconstruct_state_idle; |
| 5160 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 5161 | for (i = conf->raid_disks; i--; ) { |
| 5162 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 5163 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 5164 | s.locked++; |
| 5165 | } |
| 5166 | } |
| 5167 | |
| 5168 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 5169 | !sh->reconstruct_state) { |
| 5170 | /* Need to write out all blocks after computing parity */ |
| 5171 | sh->disks = conf->raid_disks; |
| 5172 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 5173 | schedule_reconstruction(sh, &s, 1, 1); |
| 5174 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 5175 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 5176 | atomic_dec(&conf->reshape_stripes); |
| 5177 | wake_up(&conf->wait_for_overlap); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5178 | md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 5179 | } |
| 5180 | |
| 5181 | if (s.expanding && s.locked == 0 && |
| 5182 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 5183 | handle_stripe_expansion(conf, sh); |
| 5184 | |
| 5185 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5186 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 5187 | if (unlikely(s.blocked_rdev)) { |
| 5188 | if (conf->mddev->external) |
| 5189 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 5190 | conf->mddev); |
| 5191 | else |
| 5192 | /* Internal metadata will immediately |
| 5193 | * be written by raid5d, so we don't |
| 5194 | * need to wait here. |
| 5195 | */ |
| 5196 | rdev_dec_pending(s.blocked_rdev, |
| 5197 | conf->mddev); |
| 5198 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 5199 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5200 | if (s.handle_bad_blocks) |
| 5201 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5202 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5203 | struct r5dev *dev = &sh->dev[i]; |
| 5204 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 5205 | /* We own a safe reference to the rdev */ |
| 5206 | rdev = conf->disks[i].rdev; |
| 5207 | if (!rdev_set_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5208 | RAID5_STRIPE_SECTORS(conf), 0)) |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5209 | md_error(conf->mddev, rdev); |
| 5210 | rdev_dec_pending(rdev, conf->mddev); |
| 5211 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 5212 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 5213 | rdev = conf->disks[i].rdev; |
| 5214 | rdev_clear_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5215 | RAID5_STRIPE_SECTORS(conf), 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 5216 | rdev_dec_pending(rdev, conf->mddev); |
| 5217 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5218 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 5219 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5220 | if (!rdev) |
| 5221 | /* rdev have been moved down */ |
| 5222 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5223 | rdev_clear_badblocks(rdev, sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5224 | RAID5_STRIPE_SECTORS(conf), 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 5225 | rdev_dec_pending(rdev, conf->mddev); |
| 5226 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5227 | } |
| 5228 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 5229 | if (s.ops_request) |
| 5230 | raid_run_ops(sh, s.ops_request); |
| 5231 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 5232 | ops_run_io(sh, &s); |
| 5233 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 5234 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5235 | /* 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] | 5236 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5237 | * have actually been submitted. |
| 5238 | */ |
| 5239 | atomic_dec(&conf->preread_active_stripes); |
| 5240 | if (atomic_read(&conf->preread_active_stripes) < |
| 5241 | IO_THRESHOLD) |
| 5242 | md_wakeup_thread(conf->mddev->thread); |
| 5243 | } |
| 5244 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 5245 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5246 | } |
| 5247 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5248 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5249 | { |
| 5250 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 5251 | while (!list_empty(&conf->delayed_list)) { |
| 5252 | struct list_head *l = conf->delayed_list.next; |
| 5253 | struct stripe_head *sh; |
| 5254 | sh = list_entry(l, struct stripe_head, lru); |
| 5255 | list_del_init(l); |
| 5256 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 5257 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5258 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5259 | list_add_tail(&sh->lru, &conf->hold_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5260 | raid5_wakeup_stripe_thread(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5261 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 5262 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5263 | } |
| 5264 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5265 | static void activate_bit_delay(struct r5conf *conf, |
| 5266 | struct list_head *temp_inactive_list) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5267 | { |
| 5268 | /* device_lock is held */ |
| 5269 | struct list_head head; |
| 5270 | list_add(&head, &conf->bitmap_list); |
| 5271 | list_del_init(&conf->bitmap_list); |
| 5272 | while (!list_empty(&head)) { |
| 5273 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5274 | int hash; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5275 | list_del_init(&sh->lru); |
| 5276 | atomic_inc(&sh->count); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5277 | hash = sh->hash_lock_index; |
| 5278 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5279 | } |
| 5280 | } |
| 5281 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5282 | 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] | 5283 | { |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5284 | struct r5conf *conf = mddev->private; |
Christoph Hellwig | 10433d0 | 2017-08-23 19:10:28 +0200 | [diff] [blame] | 5285 | sector_t sector = bio->bi_iter.bi_sector; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5286 | unsigned int chunk_sectors; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 5287 | unsigned int bio_sectors = bio_sectors(bio); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5288 | |
Christoph Hellwig | 10433d0 | 2017-08-23 19:10:28 +0200 | [diff] [blame] | 5289 | WARN_ON_ONCE(bio->bi_partno); |
| 5290 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5291 | chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5292 | return chunk_sectors >= |
| 5293 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 5294 | } |
| 5295 | |
| 5296 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5297 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 5298 | * later sampled by raid5d. |
| 5299 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5300 | 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] | 5301 | { |
| 5302 | unsigned long flags; |
| 5303 | |
| 5304 | spin_lock_irqsave(&conf->device_lock, flags); |
| 5305 | |
| 5306 | bi->bi_next = conf->retry_read_aligned_list; |
| 5307 | conf->retry_read_aligned_list = bi; |
| 5308 | |
| 5309 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 5310 | md_wakeup_thread(conf->mddev->thread); |
| 5311 | } |
| 5312 | |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5313 | static struct bio *remove_bio_from_retry(struct r5conf *conf, |
| 5314 | unsigned int *offset) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5315 | { |
| 5316 | struct bio *bi; |
| 5317 | |
| 5318 | bi = conf->retry_read_aligned; |
| 5319 | if (bi) { |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5320 | *offset = conf->retry_read_offset; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5321 | conf->retry_read_aligned = NULL; |
| 5322 | return bi; |
| 5323 | } |
| 5324 | bi = conf->retry_read_aligned_list; |
| 5325 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5326 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5327 | bi->bi_next = NULL; |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5328 | *offset = 0; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5329 | } |
| 5330 | |
| 5331 | return bi; |
| 5332 | } |
| 5333 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5334 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5335 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 5336 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 5337 | * first). |
| 5338 | * If the read failed.. |
| 5339 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5340 | static void raid5_align_endio(struct bio *bi) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5341 | { |
| 5342 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5343 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5344 | struct r5conf *conf; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5345 | struct md_rdev *rdev; |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 5346 | blk_status_t error = bi->bi_status; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5347 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5348 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5349 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5350 | rdev = (void*)raid_bi->bi_next; |
| 5351 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 5352 | mddev = rdev->mddev; |
| 5353 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5354 | |
| 5355 | rdev_dec_pending(rdev, conf->mddev); |
| 5356 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 5357 | if (!error) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5358 | bio_endio(raid_bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5359 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 5360 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 5361 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5362 | } |
| 5363 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5364 | 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] | 5365 | |
| 5366 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5367 | } |
| 5368 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5369 | 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] | 5370 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5371 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 5372 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5373 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5374 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5375 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5376 | |
| 5377 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5378 | pr_debug("%s: non aligned\n", __func__); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5379 | return 0; |
| 5380 | } |
| 5381 | /* |
Ming Lei | d7a1030 | 2017-02-14 23:29:03 +0800 | [diff] [blame] | 5382 | * use bio_clone_fast to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5383 | */ |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5384 | align_bi = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->bio_set); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5385 | if (!align_bi) |
| 5386 | return 0; |
| 5387 | /* |
| 5388 | * set bi_end_io to a new function, and set bi_private to the |
| 5389 | * original bio. |
| 5390 | */ |
| 5391 | align_bi->bi_end_io = raid5_align_endio; |
| 5392 | align_bi->bi_private = raid_bio; |
| 5393 | /* |
| 5394 | * compute position |
| 5395 | */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5396 | align_bi->bi_iter.bi_sector = |
| 5397 | raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, |
| 5398 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5399 | |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5400 | end_sector = bio_end_sector(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5401 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5402 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 5403 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 5404 | rdev->recovery_offset < end_sector) { |
| 5405 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 5406 | if (rdev && |
| 5407 | (test_bit(Faulty, &rdev->flags) || |
| 5408 | !(test_bit(In_sync, &rdev->flags) || |
| 5409 | rdev->recovery_offset >= end_sector))) |
| 5410 | rdev = NULL; |
| 5411 | } |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 5412 | |
| 5413 | if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) { |
| 5414 | rcu_read_unlock(); |
| 5415 | bio_put(align_bi); |
| 5416 | return 0; |
| 5417 | } |
| 5418 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5419 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5420 | sector_t first_bad; |
| 5421 | int bad_sectors; |
| 5422 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5423 | atomic_inc(&rdev->nr_pending); |
| 5424 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5425 | raid_bio->bi_next = (void*)rdev; |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 5426 | bio_set_dev(align_bi, rdev->bdev); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5427 | |
Kent Overstreet | 7140aaf | 2013-09-25 13:37:01 -0700 | [diff] [blame] | 5428 | if (is_badblock(rdev, align_bi->bi_iter.bi_sector, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5429 | bio_sectors(align_bi), |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5430 | &first_bad, &bad_sectors)) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5431 | bio_put(align_bi); |
| 5432 | rdev_dec_pending(rdev, mddev); |
| 5433 | return 0; |
| 5434 | } |
| 5435 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 5436 | /* No reshape active, so we can trust rdev->data_offset */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5437 | align_bi->bi_iter.bi_sector += rdev->data_offset; |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 5438 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5439 | spin_lock_irq(&conf->device_lock); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 5440 | wait_event_lock_irq(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5441 | conf->quiesce == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 5442 | conf->device_lock); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5443 | atomic_inc(&conf->active_aligned_reads); |
| 5444 | spin_unlock_irq(&conf->device_lock); |
| 5445 | |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5446 | if (mddev->gendisk) |
Christoph Hellwig | 74d4699 | 2017-08-23 19:10:32 +0200 | [diff] [blame] | 5447 | trace_block_bio_remap(align_bi->bi_disk->queue, |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5448 | align_bi, disk_devt(mddev->gendisk), |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5449 | raid_bio->bi_iter.bi_sector); |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 5450 | submit_bio_noacct(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5451 | return 1; |
| 5452 | } else { |
| 5453 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5454 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5455 | return 0; |
| 5456 | } |
| 5457 | } |
| 5458 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5459 | static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio) |
| 5460 | { |
| 5461 | struct bio *split; |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5462 | sector_t sector = raid_bio->bi_iter.bi_sector; |
| 5463 | unsigned chunk_sects = mddev->chunk_sectors; |
| 5464 | unsigned sectors = chunk_sects - (sector & (chunk_sects-1)); |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5465 | |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5466 | if (sectors < bio_sectors(raid_bio)) { |
| 5467 | struct r5conf *conf = mddev->private; |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 5468 | split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split); |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5469 | bio_chain(split, raid_bio); |
Christoph Hellwig | ed00aab | 2020-07-01 10:59:44 +0200 | [diff] [blame] | 5470 | submit_bio_noacct(raid_bio); |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5471 | raid_bio = split; |
| 5472 | } |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5473 | |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 5474 | if (!raid5_read_one_chunk(mddev, raid_bio)) |
| 5475 | return raid_bio; |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5476 | |
| 5477 | return NULL; |
| 5478 | } |
| 5479 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5480 | /* __get_priority_stripe - get the next stripe to process |
| 5481 | * |
| 5482 | * Full stripe writes are allowed to pass preread active stripes up until |
| 5483 | * the bypass_threshold is exceeded. In general the bypass_count |
| 5484 | * increments when the handle_list is handled before the hold_list; however, it |
| 5485 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 5486 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 5487 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 5488 | * handle_list. |
| 5489 | */ |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5490 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5491 | { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5492 | struct stripe_head *sh, *tmp; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5493 | struct list_head *handle_list = NULL; |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5494 | struct r5worker_group *wg; |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 5495 | bool second_try = !r5c_is_writeback(conf->log) && |
| 5496 | !r5l_log_disk_error(conf); |
| 5497 | bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) || |
| 5498 | r5l_log_disk_error(conf); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5499 | |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5500 | again: |
| 5501 | wg = NULL; |
| 5502 | sh = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5503 | if (conf->worker_cnt_per_group == 0) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5504 | handle_list = try_loprio ? &conf->loprio_list : |
| 5505 | &conf->handle_list; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5506 | } else if (group != ANY_GROUP) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5507 | handle_list = try_loprio ? &conf->worker_groups[group].loprio_list : |
| 5508 | &conf->worker_groups[group].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5509 | wg = &conf->worker_groups[group]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5510 | } else { |
| 5511 | int i; |
| 5512 | for (i = 0; i < conf->group_cnt; i++) { |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5513 | handle_list = try_loprio ? &conf->worker_groups[i].loprio_list : |
| 5514 | &conf->worker_groups[i].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5515 | wg = &conf->worker_groups[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5516 | if (!list_empty(handle_list)) |
| 5517 | break; |
| 5518 | } |
| 5519 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5520 | |
| 5521 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 5522 | __func__, |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5523 | list_empty(handle_list) ? "empty" : "busy", |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5524 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 5525 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 5526 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5527 | if (!list_empty(handle_list)) { |
| 5528 | sh = list_entry(handle_list->next, typeof(*sh), lru); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5529 | |
| 5530 | if (list_empty(&conf->hold_list)) |
| 5531 | conf->bypass_count = 0; |
| 5532 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 5533 | if (conf->hold_list.next == conf->last_hold) |
| 5534 | conf->bypass_count++; |
| 5535 | else { |
| 5536 | conf->last_hold = conf->hold_list.next; |
| 5537 | conf->bypass_count -= conf->bypass_threshold; |
| 5538 | if (conf->bypass_count < 0) |
| 5539 | conf->bypass_count = 0; |
| 5540 | } |
| 5541 | } |
| 5542 | } else if (!list_empty(&conf->hold_list) && |
| 5543 | ((conf->bypass_threshold && |
| 5544 | conf->bypass_count > conf->bypass_threshold) || |
| 5545 | atomic_read(&conf->pending_full_writes) == 0)) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5546 | |
| 5547 | list_for_each_entry(tmp, &conf->hold_list, lru) { |
| 5548 | if (conf->worker_cnt_per_group == 0 || |
| 5549 | group == ANY_GROUP || |
| 5550 | !cpu_online(tmp->cpu) || |
| 5551 | cpu_to_group(tmp->cpu) == group) { |
| 5552 | sh = tmp; |
| 5553 | break; |
| 5554 | } |
| 5555 | } |
| 5556 | |
| 5557 | if (sh) { |
| 5558 | conf->bypass_count -= conf->bypass_threshold; |
| 5559 | if (conf->bypass_count < 0) |
| 5560 | conf->bypass_count = 0; |
| 5561 | } |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5562 | wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5563 | } |
| 5564 | |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 5565 | if (!sh) { |
| 5566 | if (second_try) |
| 5567 | return NULL; |
| 5568 | second_try = true; |
| 5569 | try_loprio = !try_loprio; |
| 5570 | goto again; |
| 5571 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5572 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5573 | if (wg) { |
| 5574 | wg->stripes_cnt--; |
| 5575 | sh->group = NULL; |
| 5576 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5577 | list_del_init(&sh->lru); |
Shaohua Li | c7a6d35 | 2014-04-15 09:12:54 +0800 | [diff] [blame] | 5578 | BUG_ON(atomic_inc_return(&sh->count) != 1); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5579 | return sh; |
| 5580 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5581 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5582 | struct raid5_plug_cb { |
| 5583 | struct blk_plug_cb cb; |
| 5584 | struct list_head list; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5585 | struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5586 | }; |
| 5587 | |
| 5588 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 5589 | { |
| 5590 | struct raid5_plug_cb *cb = container_of( |
| 5591 | blk_cb, struct raid5_plug_cb, cb); |
| 5592 | struct stripe_head *sh; |
| 5593 | struct mddev *mddev = cb->cb.data; |
| 5594 | struct r5conf *conf = mddev->private; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5595 | int cnt = 0; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5596 | int hash; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5597 | |
| 5598 | if (cb->list.next && !list_empty(&cb->list)) { |
| 5599 | spin_lock_irq(&conf->device_lock); |
| 5600 | while (!list_empty(&cb->list)) { |
| 5601 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 5602 | list_del_init(&sh->lru); |
| 5603 | /* |
| 5604 | * avoid race release_stripe_plug() sees |
| 5605 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 5606 | * is still in our list |
| 5607 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 5608 | smp_mb__before_atomic(); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5609 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5610 | /* |
| 5611 | * STRIPE_ON_RELEASE_LIST could be set here. In that |
| 5612 | * case, the count is always > 1 here |
| 5613 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5614 | hash = sh->hash_lock_index; |
| 5615 | __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5616 | cnt++; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5617 | } |
| 5618 | spin_unlock_irq(&conf->device_lock); |
| 5619 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5620 | release_inactive_stripe_list(conf, cb->temp_inactive_list, |
| 5621 | NR_STRIPE_HASH_LOCKS); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5622 | if (mddev->queue) |
| 5623 | trace_block_unplug(mddev->queue, cnt, !from_schedule); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5624 | kfree(cb); |
| 5625 | } |
| 5626 | |
| 5627 | static void release_stripe_plug(struct mddev *mddev, |
| 5628 | struct stripe_head *sh) |
| 5629 | { |
| 5630 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 5631 | raid5_unplug, mddev, |
| 5632 | sizeof(struct raid5_plug_cb)); |
| 5633 | struct raid5_plug_cb *cb; |
| 5634 | |
| 5635 | if (!blk_cb) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5636 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5637 | return; |
| 5638 | } |
| 5639 | |
| 5640 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 5641 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5642 | if (cb->list.next == NULL) { |
| 5643 | int i; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5644 | INIT_LIST_HEAD(&cb->list); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5645 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5646 | INIT_LIST_HEAD(cb->temp_inactive_list + i); |
| 5647 | } |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5648 | |
| 5649 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 5650 | list_add_tail(&sh->lru, &cb->list); |
| 5651 | else |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5652 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5653 | } |
| 5654 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5655 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 5656 | { |
| 5657 | struct r5conf *conf = mddev->private; |
| 5658 | sector_t logical_sector, last_sector; |
| 5659 | struct stripe_head *sh; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5660 | int stripe_sectors; |
| 5661 | |
| 5662 | if (mddev->reshape_position != MaxSector) |
| 5663 | /* Skip discard while reshape is happening */ |
| 5664 | return; |
| 5665 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5666 | 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] | 5667 | last_sector = bio_end_sector(bi); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5668 | |
| 5669 | bi->bi_next = NULL; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5670 | |
| 5671 | stripe_sectors = conf->chunk_sectors * |
| 5672 | (conf->raid_disks - conf->max_degraded); |
| 5673 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 5674 | stripe_sectors); |
| 5675 | sector_div(last_sector, stripe_sectors); |
| 5676 | |
| 5677 | logical_sector *= conf->chunk_sectors; |
| 5678 | last_sector *= conf->chunk_sectors; |
| 5679 | |
| 5680 | for (; logical_sector < last_sector; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5681 | logical_sector += RAID5_STRIPE_SECTORS(conf)) { |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5682 | DEFINE_WAIT(w); |
| 5683 | int d; |
| 5684 | again: |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5685 | sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5686 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5687 | TASK_UNINTERRUPTIBLE); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5688 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
| 5689 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5690 | raid5_release_stripe(sh); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5691 | schedule(); |
| 5692 | goto again; |
| 5693 | } |
| 5694 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5695 | spin_lock_irq(&sh->stripe_lock); |
| 5696 | for (d = 0; d < conf->raid_disks; d++) { |
| 5697 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5698 | continue; |
| 5699 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 5700 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 5701 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5702 | raid5_release_stripe(sh); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5703 | schedule(); |
| 5704 | goto again; |
| 5705 | } |
| 5706 | } |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5707 | set_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5708 | finish_wait(&conf->wait_for_overlap, &w); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5709 | sh->overwrite_disks = 0; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5710 | for (d = 0; d < conf->raid_disks; d++) { |
| 5711 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5712 | continue; |
| 5713 | sh->dev[d].towrite = bi; |
| 5714 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5715 | bio_inc_remaining(bi); |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5716 | md_write_inc(mddev, bi); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5717 | sh->overwrite_disks++; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5718 | } |
| 5719 | spin_unlock_irq(&sh->stripe_lock); |
| 5720 | if (conf->mddev->bitmap) { |
| 5721 | for (d = 0; |
| 5722 | d < conf->raid_disks - conf->max_degraded; |
| 5723 | d++) |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 5724 | md_bitmap_startwrite(mddev->bitmap, |
| 5725 | sh->sector, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5726 | RAID5_STRIPE_SECTORS(conf), |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 5727 | 0); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5728 | sh->bm_seq = conf->seq_flush + 1; |
| 5729 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 5730 | } |
| 5731 | |
| 5732 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5733 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 5734 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5735 | atomic_inc(&conf->preread_active_stripes); |
| 5736 | release_stripe_plug(mddev, sh); |
| 5737 | } |
| 5738 | |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5739 | bio_endio(bi); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5740 | } |
| 5741 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5742 | static bool raid5_make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5743 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5744 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5745 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | sector_t new_sector; |
| 5747 | sector_t logical_sector, last_sector; |
| 5748 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5749 | const int rw = bio_data_dir(bi); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5750 | DEFINE_WAIT(w); |
| 5751 | bool do_prepare; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5752 | bool do_flush = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5753 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5754 | if (unlikely(bi->bi_opf & REQ_PREFLUSH)) { |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 5755 | int ret = log_handle_flush_request(conf, bi); |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5756 | |
| 5757 | if (ret == 0) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5758 | return true; |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5759 | if (ret == -ENODEV) { |
David Jeffery | 775d783 | 2019-09-16 13:15:14 -0400 | [diff] [blame] | 5760 | if (md_flush_request(mddev, bi)) |
| 5761 | return true; |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5762 | } |
| 5763 | /* ret == -EAGAIN, fallback */ |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5764 | /* |
| 5765 | * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH, |
| 5766 | * we need to flush journal device |
| 5767 | */ |
| 5768 | do_flush = bi->bi_opf & REQ_PREFLUSH; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 5769 | } |
| 5770 | |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5771 | if (!md_write_start(mddev, bi)) |
| 5772 | return false; |
Eric Mei | 9ffc8f7 | 2015-03-18 23:39:11 -0600 | [diff] [blame] | 5773 | /* |
| 5774 | * If array is degraded, better not do chunk aligned read because |
| 5775 | * later we might have to read it again in order to reconstruct |
| 5776 | * data on failed drives. |
| 5777 | */ |
| 5778 | if (rw == READ && mddev->degraded == 0 && |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5779 | mddev->reshape_position == MaxSector) { |
| 5780 | bi = chunk_aligned_read(mddev, bi); |
| 5781 | if (!bi) |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5782 | return true; |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5783 | } |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 5784 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 5785 | if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) { |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5786 | make_discard_request(mddev, bi); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5787 | md_write_end(mddev); |
| 5788 | return true; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5789 | } |
| 5790 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5791 | 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] | 5792 | last_sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5793 | bi->bi_next = NULL; |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5794 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5795 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 5796 | for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5797 | int previous; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5798 | int seq; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5799 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5800 | do_prepare = false; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5801 | retry: |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5802 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5803 | previous = 0; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5804 | if (do_prepare) |
| 5805 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5806 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5807 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5808 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5809 | * 64bit on a 32bit platform, and so it might be |
| 5810 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 5811 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5812 | * the lock is dropped, so once we get a reference |
| 5813 | * to the stripe that we think it is, we will have |
| 5814 | * to check again. |
| 5815 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5816 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5817 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5818 | ? logical_sector < conf->reshape_progress |
| 5819 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5820 | previous = 1; |
| 5821 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5822 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5823 | ? logical_sector < conf->reshape_safe |
| 5824 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5825 | spin_unlock_irq(&conf->device_lock); |
| 5826 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5827 | do_prepare = true; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5828 | goto retry; |
| 5829 | } |
| 5830 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5831 | spin_unlock_irq(&conf->device_lock); |
| 5832 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5833 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5834 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 5835 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5836 | &dd_idx, NULL); |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5837 | pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n", |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5838 | (unsigned long long)new_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5839 | (unsigned long long)logical_sector); |
| 5840 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5841 | sh = raid5_get_active_stripe(conf, new_sector, previous, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5842 | (bi->bi_opf & REQ_RAHEAD), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5843 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5844 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5845 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5846 | * stripe, so we must do the range check again. |
| 5847 | * Expansion could still move past after this |
| 5848 | * test, but as we are holding a reference to |
| 5849 | * 'sh', we know that if that happens, |
| 5850 | * STRIPE_EXPANDING will get set and the expansion |
| 5851 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5852 | */ |
| 5853 | int must_retry = 0; |
| 5854 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5855 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5856 | ? logical_sector >= conf->reshape_progress |
| 5857 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5858 | /* mismatch, need to try again */ |
| 5859 | must_retry = 1; |
| 5860 | spin_unlock_irq(&conf->device_lock); |
| 5861 | if (must_retry) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5862 | raid5_release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 5863 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5864 | do_prepare = true; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5865 | goto retry; |
| 5866 | } |
| 5867 | } |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5868 | if (read_seqcount_retry(&conf->gen_lock, seq)) { |
| 5869 | /* Might have got the wrong stripe_head |
| 5870 | * by accident |
| 5871 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5872 | raid5_release_stripe(sh); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5873 | goto retry; |
| 5874 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5875 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5876 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5877 | !add_stripe_bio(sh, bi, dd_idx, rw, previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5878 | /* Stripe is busy expanding or |
| 5879 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5880 | * and wait a while |
| 5881 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 5882 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5883 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5884 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5885 | do_prepare = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5886 | goto retry; |
| 5887 | } |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5888 | if (do_flush) { |
| 5889 | set_bit(STRIPE_R5C_PREFLUSH, &sh->state); |
| 5890 | /* we only need flush for one stripe */ |
| 5891 | do_flush = false; |
| 5892 | } |
| 5893 | |
Guoqing Jiang | 1684e97 | 2020-06-16 11:25:52 +0200 | [diff] [blame] | 5894 | set_bit(STRIPE_HANDLE, &sh->state); |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 5895 | clear_bit(STRIPE_DELAYED, &sh->state); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5896 | if ((!sh->batch_head || sh == sh->batch_head) && |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5897 | (bi->bi_opf & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5898 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5899 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5900 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5901 | } else { |
| 5902 | /* cannot get stripe for read-ahead, just give-up */ |
Christoph Hellwig | 4e4cbee | 2017-06-03 09:38:06 +0200 | [diff] [blame] | 5903 | bi->bi_status = BLK_STS_IOERR; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5904 | break; |
| 5905 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5906 | } |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5907 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5908 | |
NeilBrown | 4972805 | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 5909 | if (rw == WRITE) |
| 5910 | md_write_end(mddev); |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 5911 | bio_endio(bi); |
NeilBrown | cc27b0c | 2017-06-05 16:49:39 +1000 | [diff] [blame] | 5912 | return true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5913 | } |
| 5914 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5915 | 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] | 5916 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5917 | 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] | 5918 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5919 | /* reshaping is quite different to recovery/resync so it is |
| 5920 | * handled quite separately ... here. |
| 5921 | * |
| 5922 | * On each call to sync_request, we gather one chunk worth of |
| 5923 | * destination stripes and flag them as expanding. |
| 5924 | * Then we find all the source stripes and request reads. |
| 5925 | * As the reads complete, handle_stripe will copy the data |
| 5926 | * into the destination stripe and release that stripe. |
| 5927 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5928 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | struct stripe_head *sh; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 5930 | struct md_rdev *rdev; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5931 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5932 | int raid_disks = conf->previous_raid_disks; |
| 5933 | int data_disks = raid_disks - conf->max_degraded; |
| 5934 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5935 | int i; |
| 5936 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5937 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5938 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5939 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5940 | struct list_head stripes; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5941 | sector_t retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5942 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5943 | if (sector_nr == 0) { |
| 5944 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5945 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5946 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 5947 | sector_nr = raid5_size(mddev, 0, 0) |
| 5948 | - conf->reshape_progress; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 5949 | } else if (mddev->reshape_backwards && |
| 5950 | conf->reshape_progress == MaxSector) { |
| 5951 | /* shouldn't happen, but just in case, finish up.*/ |
| 5952 | sector_nr = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5953 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5954 | conf->reshape_progress > 0) |
| 5955 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5956 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5957 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 5958 | mddev->curr_resync_completed = sector_nr; |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 5959 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5960 | *skipped = 1; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5961 | retn = sector_nr; |
| 5962 | goto finish; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5963 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5964 | } |
| 5965 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5966 | /* We need to process a full chunk at a time. |
| 5967 | * If old and new chunk sizes differ, we need to process the |
| 5968 | * largest of these |
| 5969 | */ |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5970 | |
| 5971 | reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5972 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5973 | /* We update the metadata at least every 10 seconds, or when |
| 5974 | * the data about to be copied would over-write the source of |
| 5975 | * the data at the front of the range. i.e. one new_stripe |
| 5976 | * along from reshape_progress new_maps to after where |
| 5977 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5978 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5979 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5980 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5981 | readpos = conf->reshape_progress; |
| 5982 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5983 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5984 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5985 | if (mddev->reshape_backwards) { |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 5986 | BUG_ON(writepos < reshape_sectors); |
| 5987 | writepos -= reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5988 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5989 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5990 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5991 | writepos += reshape_sectors; |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 5992 | /* readpos and safepos are worst-case calculations. |
| 5993 | * A negative number is overly pessimistic, and causes |
| 5994 | * obvious problems for unsigned storage. So clip to 0. |
| 5995 | */ |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 5996 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 5997 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5998 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5999 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6000 | /* Having calculated the 'writepos' possibly use it |
| 6001 | * to set 'stripe_addr' which is where we will write to. |
| 6002 | */ |
| 6003 | if (mddev->reshape_backwards) { |
| 6004 | BUG_ON(conf->reshape_progress == 0); |
| 6005 | stripe_addr = writepos; |
| 6006 | BUG_ON((mddev->dev_sectors & |
| 6007 | ~((sector_t)reshape_sectors - 1)) |
| 6008 | - reshape_sectors - stripe_addr |
| 6009 | != sector_nr); |
| 6010 | } else { |
| 6011 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 6012 | stripe_addr = sector_nr; |
| 6013 | } |
| 6014 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6015 | /* 'writepos' is the most advanced device address we might write. |
| 6016 | * 'readpos' is the least advanced device address we might read. |
| 6017 | * 'safepos' is the least address recorded in the metadata as having |
| 6018 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6019 | * If there is a min_offset_diff, these are adjusted either by |
| 6020 | * increasing the safepos/readpos if diff is negative, or |
| 6021 | * increasing writepos if diff is positive. |
| 6022 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6023 | * ensure safety in the face of a crash - that must be done by userspace |
| 6024 | * making a backup of the data. So in that case there is no particular |
| 6025 | * rush to update metadata. |
| 6026 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 6027 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 6028 | * we can be safe in the event of a crash. |
| 6029 | * So we insist on updating metadata if safepos is behind writepos and |
| 6030 | * readpos is beyond writepos. |
| 6031 | * In any case, update the metadata every 10 seconds. |
| 6032 | * Maybe that number should be configurable, but I'm not sure it is |
| 6033 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 6034 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6035 | if (conf->min_offset_diff < 0) { |
| 6036 | safepos += -conf->min_offset_diff; |
| 6037 | readpos += -conf->min_offset_diff; |
| 6038 | } else |
| 6039 | writepos += conf->min_offset_diff; |
| 6040 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6041 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6042 | ? (safepos > writepos && readpos < writepos) |
| 6043 | : (safepos < writepos && readpos > writepos)) || |
| 6044 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6045 | /* Cannot proceed until we've updated the superblock... */ |
| 6046 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6047 | atomic_read(&conf->reshape_stripes)==0 |
| 6048 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 6049 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 6050 | return 0; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6051 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6052 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 6053 | if (!mddev->reshape_backwards) |
| 6054 | /* Can update recovery_offset */ |
| 6055 | rdev_for_each(rdev, mddev) |
| 6056 | if (rdev->raid_disk >= 0 && |
| 6057 | !test_bit(Journal, &rdev->flags) && |
| 6058 | !test_bit(In_sync, &rdev->flags) && |
| 6059 | rdev->recovery_offset < sector_nr) |
| 6060 | rdev->recovery_offset = sector_nr; |
| 6061 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6062 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6063 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6064 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6065 | wait_event(mddev->sb_wait, mddev->sb_flags == 0 || |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6066 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 6067 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6068 | return 0; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6069 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6070 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6071 | spin_unlock_irq(&conf->device_lock); |
| 6072 | wake_up(&conf->wait_for_overlap); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6073 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6074 | } |
| 6075 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 6076 | INIT_LIST_HEAD(&stripes); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6077 | for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6078 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6079 | int skipped_disk = 0; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6080 | sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6081 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 6082 | atomic_inc(&conf->reshape_stripes); |
| 6083 | /* If any of this stripe is beyond the end of the old |
| 6084 | * array, then we need to zero those blocks |
| 6085 | */ |
| 6086 | for (j=sh->disks; j--;) { |
| 6087 | sector_t s; |
| 6088 | if (j == sh->pd_idx) |
| 6089 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6090 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6091 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6092 | continue; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6093 | s = raid5_compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 6094 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6095 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6096 | continue; |
| 6097 | } |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6098 | memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf)); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6099 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 6100 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 6101 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 6102 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6103 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 6104 | set_bit(STRIPE_HANDLE, &sh->state); |
| 6105 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 6106 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6107 | } |
| 6108 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6109 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6110 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6111 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6112 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6113 | spin_unlock_irq(&conf->device_lock); |
| 6114 | /* Ok, those stripe are ready. We can start scheduling |
| 6115 | * reads on the source stripes. |
| 6116 | * The source stripes are determined by mapping the first and last |
| 6117 | * block on the destination stripes. |
| 6118 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6119 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 6120 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6121 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6122 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 6123 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6124 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6125 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6126 | if (last_sector >= mddev->dev_sectors) |
| 6127 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6128 | while (first_sector <= last_sector) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6129 | sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6130 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 6131 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6132 | raid5_release_stripe(sh); |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6133 | first_sector += RAID5_STRIPE_SECTORS(conf); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6134 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 6135 | /* Now that the sources are clearly marked, we can release |
| 6136 | * the destination stripes |
| 6137 | */ |
| 6138 | while (!list_empty(&stripes)) { |
| 6139 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 6140 | list_del_init(&sh->lru); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6141 | raid5_release_stripe(sh); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 6142 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6143 | /* If this takes us to the resync_max point where we have to pause, |
| 6144 | * then we need to write out the superblock. |
| 6145 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 6146 | sector_nr += reshape_sectors; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 6147 | retn = reshape_sectors; |
| 6148 | finish: |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 6149 | if (mddev->curr_resync_completed > mddev->resync_max || |
| 6150 | (sector_nr - mddev->curr_resync_completed) * 2 |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 6151 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6152 | /* Cannot proceed until we've updated the superblock... */ |
| 6153 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6154 | atomic_read(&conf->reshape_stripes) == 0 |
| 6155 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 6156 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 6157 | goto ret; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6158 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 6159 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 6160 | if (!mddev->reshape_backwards) |
| 6161 | /* Can update recovery_offset */ |
| 6162 | rdev_for_each(rdev, mddev) |
| 6163 | if (rdev->raid_disk >= 0 && |
| 6164 | !test_bit(Journal, &rdev->flags) && |
| 6165 | !test_bit(In_sync, &rdev->flags) && |
| 6166 | rdev->recovery_offset < sector_nr) |
| 6167 | rdev->recovery_offset = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 6168 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6169 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6170 | md_wakeup_thread(mddev->thread); |
| 6171 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6172 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6173 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 6174 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 6175 | goto ret; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6176 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6177 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6178 | spin_unlock_irq(&conf->device_lock); |
| 6179 | wake_up(&conf->wait_for_overlap); |
Junxiao Bi | e1a86db | 2020-07-14 16:10:26 -0700 | [diff] [blame] | 6180 | sysfs_notify_dirent_safe(mddev->sysfs_completed); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6181 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 6182 | ret: |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 6183 | return retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6184 | } |
| 6185 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 6186 | static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr, |
| 6187 | int *skipped) |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6188 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6189 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6190 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6191 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 6192 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6193 | int still_degraded = 0; |
| 6194 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6195 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6196 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6197 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 6198 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 6199 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 6200 | end_reshape(conf); |
| 6201 | return 0; |
| 6202 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6203 | |
| 6204 | if (mddev->curr_resync < max_sector) /* aborted */ |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6205 | md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 6206 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6207 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6208 | conf->fullsync = 0; |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6209 | md_bitmap_close_sync(mddev->bitmap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6210 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6211 | return 0; |
| 6212 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 6213 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 6214 | /* Allow raid5_quiesce to complete */ |
| 6215 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 6216 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 6217 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 6218 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6219 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 6220 | /* No need to check resync_max as we never do more than one |
| 6221 | * stripe, and as resync_max will always be on a chunk boundary, |
| 6222 | * if the check in md_do_sync didn't fire, there is no chance |
| 6223 | * of overstepping resync_max here |
| 6224 | */ |
| 6225 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6226 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6227 | * to resync, then assert that we are finished, because there is |
| 6228 | * nothing we can do. |
| 6229 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 6230 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6231 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 6232 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 6233 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6234 | return rv; |
| 6235 | } |
majianpeng | 6f60804 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 6236 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 6237 | !conf->fullsync && |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6238 | !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6239 | sync_blocks >= RAID5_STRIPE_SECTORS(conf)) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6240 | /* we can skip this block, and probably more */ |
Yufen Yu | 83c3e5e | 2020-07-22 23:29:05 -0400 | [diff] [blame] | 6241 | do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf)); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6242 | *skipped = 1; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6243 | /* keep things rounded to whole stripes */ |
| 6244 | return sync_blocks * RAID5_STRIPE_SECTORS(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6245 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6246 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6247 | md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 6248 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6249 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6250 | if (sh == NULL) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6251 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6252 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6253 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6254 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 6255 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6256 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6257 | /* Need to check if array will still be degraded after recovery/resync |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6258 | * Note in case of > 1 drive failures it's possible we're rebuilding |
| 6259 | * one drive while leaving another faulty drive in array. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6260 | */ |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6261 | rcu_read_lock(); |
| 6262 | for (i = 0; i < conf->raid_disks; i++) { |
Mark Rutland | 6aa7de0 | 2017-10-23 14:07:29 -0700 | [diff] [blame] | 6263 | struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev); |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6264 | |
| 6265 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6266 | still_degraded = 1; |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 6267 | } |
| 6268 | rcu_read_unlock(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6269 | |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6270 | md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 6271 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 6272 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Eivind Sarto | 053f5b6 | 2014-06-09 17:06:19 -0700 | [diff] [blame] | 6273 | set_bit(STRIPE_HANDLE, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6274 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6275 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6276 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6277 | return RAID5_STRIPE_SECTORS(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6278 | } |
| 6279 | |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6280 | static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio, |
| 6281 | unsigned int offset) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6282 | { |
| 6283 | /* We may not be able to submit a whole bio at once as there |
| 6284 | * may not be enough stripe_heads available. |
| 6285 | * We cannot pre-allocate enough stripe_heads as we may need |
| 6286 | * more than exist in the cache (if we allow ever large chunks). |
| 6287 | * So we do one stripe head at a time and record in |
| 6288 | * ->bi_hw_segments how many have been done. |
| 6289 | * |
| 6290 | * We *know* that this entire raid_bio is in one chunk, so |
| 6291 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 6292 | */ |
| 6293 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6294 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6295 | sector_t sector, logical_sector, last_sector; |
| 6296 | int scnt = 0; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6297 | int handled = 0; |
| 6298 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 6299 | logical_sector = raid_bio->bi_iter.bi_sector & |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6300 | ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6301 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6302 | 0, &dd_idx, NULL); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 6303 | last_sector = bio_end_sector(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6304 | |
| 6305 | for (; logical_sector < last_sector; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 6306 | logical_sector += RAID5_STRIPE_SECTORS(conf), |
| 6307 | sector += RAID5_STRIPE_SECTORS(conf), |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6308 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6309 | |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6310 | if (scnt < offset) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6311 | /* already done this stripe */ |
| 6312 | continue; |
| 6313 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6314 | sh = raid5_get_active_stripe(conf, sector, 0, 1, 1); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6315 | |
| 6316 | if (!sh) { |
| 6317 | /* failed to get a stripe - must wait */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6318 | conf->retry_read_aligned = raid_bio; |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6319 | conf->retry_read_offset = scnt; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6320 | return handled; |
| 6321 | } |
| 6322 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 6323 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6324 | raid5_release_stripe(sh); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6325 | conf->retry_read_aligned = raid_bio; |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6326 | conf->retry_read_offset = scnt; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6327 | return handled; |
| 6328 | } |
| 6329 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 6330 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6331 | handle_stripe(sh); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6332 | raid5_release_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6333 | handled++; |
| 6334 | } |
NeilBrown | 016c76a | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6335 | |
| 6336 | bio_endio(raid_bio); |
| 6337 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6338 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 6339 | wake_up(&conf->wait_for_quiescent); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6340 | return handled; |
| 6341 | } |
| 6342 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 6343 | static int handle_active_stripes(struct r5conf *conf, int group, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6344 | struct r5worker *worker, |
| 6345 | struct list_head *temp_inactive_list) |
Christoph Hellwig | efcd487 | 2019-04-04 18:56:16 +0200 | [diff] [blame] | 6346 | __releases(&conf->device_lock) |
| 6347 | __acquires(&conf->device_lock) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6348 | { |
| 6349 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6350 | int i, batch_size = 0, hash; |
| 6351 | bool release_inactive = false; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6352 | |
| 6353 | while (batch_size < MAX_STRIPE_BATCH && |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6354 | (sh = __get_priority_stripe(conf, group)) != NULL) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6355 | batch[batch_size++] = sh; |
| 6356 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6357 | if (batch_size == 0) { |
| 6358 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6359 | if (!list_empty(temp_inactive_list + i)) |
| 6360 | break; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6361 | if (i == NR_STRIPE_HASH_LOCKS) { |
| 6362 | spin_unlock_irq(&conf->device_lock); |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 6363 | log_flush_stripe_to_raid(conf); |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6364 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6365 | return batch_size; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6366 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6367 | release_inactive = true; |
| 6368 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6369 | spin_unlock_irq(&conf->device_lock); |
| 6370 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6371 | release_inactive_stripe_list(conf, temp_inactive_list, |
| 6372 | NR_STRIPE_HASH_LOCKS); |
| 6373 | |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6374 | r5l_flush_stripe_to_raid(conf->log); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6375 | if (release_inactive) { |
| 6376 | spin_lock_irq(&conf->device_lock); |
| 6377 | return 0; |
| 6378 | } |
| 6379 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6380 | for (i = 0; i < batch_size; i++) |
| 6381 | handle_stripe(batch[i]); |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 6382 | log_write_stripe_run(conf); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6383 | |
| 6384 | cond_resched(); |
| 6385 | |
| 6386 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6387 | for (i = 0; i < batch_size; i++) { |
| 6388 | hash = batch[i]->hash_lock_index; |
| 6389 | __release_stripe(conf, batch[i], &temp_inactive_list[hash]); |
| 6390 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6391 | return batch_size; |
| 6392 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6393 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6394 | static void raid5_do_work(struct work_struct *work) |
| 6395 | { |
| 6396 | struct r5worker *worker = container_of(work, struct r5worker, work); |
| 6397 | struct r5worker_group *group = worker->group; |
| 6398 | struct r5conf *conf = group->conf; |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 6399 | struct mddev *mddev = conf->mddev; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6400 | int group_id = group - conf->worker_groups; |
| 6401 | int handled; |
| 6402 | struct blk_plug plug; |
| 6403 | |
| 6404 | pr_debug("+++ raid5worker active\n"); |
| 6405 | |
| 6406 | blk_start_plug(&plug); |
| 6407 | handled = 0; |
| 6408 | spin_lock_irq(&conf->device_lock); |
| 6409 | while (1) { |
| 6410 | int batch_size, released; |
| 6411 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6412 | released = release_stripe_list(conf, worker->temp_inactive_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6413 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6414 | batch_size = handle_active_stripes(conf, group_id, worker, |
| 6415 | worker->temp_inactive_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 6416 | worker->working = false; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6417 | if (!batch_size && !released) |
| 6418 | break; |
| 6419 | handled += batch_size; |
NeilBrown | 16d997b | 2017-03-15 14:05:12 +1100 | [diff] [blame] | 6420 | wait_event_lock_irq(mddev->sb_wait, |
| 6421 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags), |
| 6422 | conf->device_lock); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6423 | } |
| 6424 | pr_debug("%d stripes handled\n", handled); |
| 6425 | |
| 6426 | spin_unlock_irq(&conf->device_lock); |
Ofer Heifetz | 7e96d55 | 2017-07-24 09:17:40 +0300 | [diff] [blame] | 6427 | |
Song Liu | 9c72a18e4 | 2017-08-24 09:53:59 -0700 | [diff] [blame] | 6428 | flush_deferred_bios(conf); |
| 6429 | |
| 6430 | r5l_flush_stripe_to_raid(conf->log); |
| 6431 | |
Ofer Heifetz | 7e96d55 | 2017-07-24 09:17:40 +0300 | [diff] [blame] | 6432 | async_tx_issue_pending_all(); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6433 | blk_finish_plug(&plug); |
| 6434 | |
| 6435 | pr_debug("--- raid5worker inactive\n"); |
| 6436 | } |
| 6437 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6438 | /* |
| 6439 | * This is our raid5 kernel thread. |
| 6440 | * |
| 6441 | * We scan the hash table for stripes which can be handled now. |
| 6442 | * During the scan, completed stripes are saved for us by the interrupt |
| 6443 | * handler, so that they will not have to wait for our next wakeup. |
| 6444 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6445 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6446 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6447 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6448 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6449 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6450 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6451 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6452 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6453 | |
| 6454 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6455 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6456 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6457 | handled = 0; |
| 6458 | spin_lock_irq(&conf->device_lock); |
| 6459 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6460 | struct bio *bio; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6461 | int batch_size, released; |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6462 | unsigned int offset; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6463 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6464 | released = release_stripe_list(conf, conf->temp_inactive_list); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6465 | if (released) |
| 6466 | clear_bit(R5_DID_ALLOC, &conf->cache_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6467 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 6468 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 6469 | !list_empty(&conf->bitmap_list)) { |
| 6470 | /* Now is a good time to flush some bitmap updates */ |
| 6471 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 6472 | spin_unlock_irq(&conf->device_lock); |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 6473 | md_bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 6474 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 6475 | conf->seq_write = conf->seq_flush; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6476 | activate_bit_delay(conf, conf->temp_inactive_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6477 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 6478 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6479 | |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6480 | while ((bio = remove_bio_from_retry(conf, &offset))) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6481 | int ok; |
| 6482 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 0472a42 | 2017-03-15 14:05:13 +1100 | [diff] [blame] | 6483 | ok = retry_aligned_read(conf, bio, offset); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6484 | spin_lock_irq(&conf->device_lock); |
| 6485 | if (!ok) |
| 6486 | break; |
| 6487 | handled++; |
| 6488 | } |
| 6489 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6490 | batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, |
| 6491 | conf->temp_inactive_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6492 | if (!batch_size && !released) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6493 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6494 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6495 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6496 | if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) { |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6497 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6498 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6499 | spin_lock_irq(&conf->device_lock); |
| 6500 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6501 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6502 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6503 | |
| 6504 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6505 | if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) && |
| 6506 | mutex_trylock(&conf->cache_size_mutex)) { |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6507 | grow_one_stripe(conf, __GFP_NOWARN); |
| 6508 | /* Set flag even if allocation failed. This helps |
| 6509 | * slow down allocation requests when mem is short |
| 6510 | */ |
| 6511 | set_bit(R5_DID_ALLOC, &conf->cache_state); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6512 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6513 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6514 | |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 6515 | flush_deferred_bios(conf); |
| 6516 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 6517 | r5l_flush_stripe_to_raid(conf->log); |
| 6518 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 6519 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6520 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6521 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6522 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6523 | } |
| 6524 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6525 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6526 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6527 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6528 | struct r5conf *conf; |
| 6529 | int ret = 0; |
| 6530 | spin_lock(&mddev->lock); |
| 6531 | conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6532 | if (conf) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6533 | ret = sprintf(page, "%d\n", conf->min_nr_stripes); |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6534 | spin_unlock(&mddev->lock); |
| 6535 | return ret; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6536 | } |
| 6537 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6538 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6539 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6540 | { |
Alexei Naberezhnov | 483cbbe | 2018-03-27 16:54:16 -0700 | [diff] [blame] | 6541 | int result = 0; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6542 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6543 | |
| 6544 | if (size <= 16 || size > 32768) |
| 6545 | return -EINVAL; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6546 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6547 | conf->min_nr_stripes = size; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6548 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6549 | while (size < conf->max_nr_stripes && |
| 6550 | drop_one_stripe(conf)) |
| 6551 | ; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6552 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6553 | |
Artur Paszkiewicz | 2214c26 | 2017-05-08 11:56:55 +0200 | [diff] [blame] | 6554 | md_allow_write(mddev); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6555 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6556 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6557 | while (size > conf->max_nr_stripes) |
Alexei Naberezhnov | 483cbbe | 2018-03-27 16:54:16 -0700 | [diff] [blame] | 6558 | if (!grow_one_stripe(conf, GFP_KERNEL)) { |
| 6559 | conf->min_nr_stripes = conf->max_nr_stripes; |
| 6560 | result = -ENOMEM; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6561 | break; |
Alexei Naberezhnov | 483cbbe | 2018-03-27 16:54:16 -0700 | [diff] [blame] | 6562 | } |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6563 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6564 | |
Alexei Naberezhnov | 483cbbe | 2018-03-27 16:54:16 -0700 | [diff] [blame] | 6565 | return result; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6566 | } |
| 6567 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 6568 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6569 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6570 | 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] | 6571 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6572 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6573 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6574 | int err; |
| 6575 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6576 | if (len >= PAGE_SIZE) |
| 6577 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6578 | if (kstrtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6579 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6580 | err = mddev_lock(mddev); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6581 | if (err) |
| 6582 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6583 | conf = mddev->private; |
| 6584 | if (!conf) |
| 6585 | err = -ENODEV; |
| 6586 | else |
| 6587 | err = raid5_set_cache_size(mddev, new); |
| 6588 | mddev_unlock(mddev); |
| 6589 | |
| 6590 | return err ?: len; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6591 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6592 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6593 | static struct md_sysfs_entry |
| 6594 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 6595 | raid5_show_stripe_cache_size, |
| 6596 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6597 | |
| 6598 | static ssize_t |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6599 | raid5_show_rmw_level(struct mddev *mddev, char *page) |
| 6600 | { |
| 6601 | struct r5conf *conf = mddev->private; |
| 6602 | if (conf) |
| 6603 | return sprintf(page, "%d\n", conf->rmw_level); |
| 6604 | else |
| 6605 | return 0; |
| 6606 | } |
| 6607 | |
| 6608 | static ssize_t |
| 6609 | raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len) |
| 6610 | { |
| 6611 | struct r5conf *conf = mddev->private; |
| 6612 | unsigned long new; |
| 6613 | |
| 6614 | if (!conf) |
| 6615 | return -ENODEV; |
| 6616 | |
| 6617 | if (len >= PAGE_SIZE) |
| 6618 | return -EINVAL; |
| 6619 | |
| 6620 | if (kstrtoul(page, 10, &new)) |
| 6621 | return -EINVAL; |
| 6622 | |
| 6623 | if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome) |
| 6624 | return -EINVAL; |
| 6625 | |
| 6626 | if (new != PARITY_DISABLE_RMW && |
| 6627 | new != PARITY_ENABLE_RMW && |
| 6628 | new != PARITY_PREFER_RMW) |
| 6629 | return -EINVAL; |
| 6630 | |
| 6631 | conf->rmw_level = new; |
| 6632 | return len; |
| 6633 | } |
| 6634 | |
| 6635 | static struct md_sysfs_entry |
| 6636 | raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR, |
| 6637 | raid5_show_rmw_level, |
| 6638 | raid5_store_rmw_level); |
| 6639 | |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6640 | static ssize_t |
| 6641 | raid5_show_stripe_size(struct mddev *mddev, char *page) |
| 6642 | { |
| 6643 | struct r5conf *conf; |
| 6644 | int ret = 0; |
| 6645 | |
| 6646 | spin_lock(&mddev->lock); |
| 6647 | conf = mddev->private; |
| 6648 | if (conf) |
| 6649 | ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf)); |
| 6650 | spin_unlock(&mddev->lock); |
| 6651 | return ret; |
| 6652 | } |
| 6653 | |
| 6654 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 6655 | static ssize_t |
| 6656 | raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len) |
| 6657 | { |
| 6658 | struct r5conf *conf; |
| 6659 | unsigned long new; |
| 6660 | int err; |
| 6661 | |
| 6662 | if (len >= PAGE_SIZE) |
| 6663 | return -EINVAL; |
| 6664 | if (kstrtoul(page, 10, &new)) |
| 6665 | return -EINVAL; |
| 6666 | |
| 6667 | /* |
| 6668 | * The value should not be bigger than PAGE_SIZE. It requires to |
Yufen Yu | 6af10a3 | 2020-08-20 09:22:05 -0400 | [diff] [blame] | 6669 | * be multiple of DEFAULT_STRIPE_SIZE and the value should be power |
| 6670 | * of two. |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6671 | */ |
Yufen Yu | 6af10a3 | 2020-08-20 09:22:05 -0400 | [diff] [blame] | 6672 | if (new % DEFAULT_STRIPE_SIZE != 0 || |
| 6673 | new > PAGE_SIZE || new == 0 || |
| 6674 | new != roundup_pow_of_two(new)) |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6675 | return -EINVAL; |
| 6676 | |
| 6677 | err = mddev_lock(mddev); |
| 6678 | if (err) |
| 6679 | return err; |
| 6680 | |
| 6681 | conf = mddev->private; |
| 6682 | if (!conf) { |
| 6683 | err = -ENODEV; |
| 6684 | goto out_unlock; |
| 6685 | } |
| 6686 | |
| 6687 | if (new == conf->stripe_size) |
| 6688 | goto out_unlock; |
| 6689 | |
| 6690 | pr_debug("md/raid: change stripe_size from %lu to %lu\n", |
| 6691 | conf->stripe_size, new); |
| 6692 | |
| 6693 | mddev_suspend(mddev); |
| 6694 | conf->stripe_size = new; |
| 6695 | conf->stripe_shift = ilog2(new) - 9; |
| 6696 | conf->stripe_sectors = new >> 9; |
| 6697 | mddev_resume(mddev); |
| 6698 | |
| 6699 | out_unlock: |
| 6700 | mddev_unlock(mddev); |
| 6701 | return err ?: len; |
| 6702 | } |
| 6703 | |
| 6704 | static struct md_sysfs_entry |
| 6705 | raid5_stripe_size = __ATTR(stripe_size, 0644, |
| 6706 | raid5_show_stripe_size, |
| 6707 | raid5_store_stripe_size); |
| 6708 | #else |
| 6709 | static struct md_sysfs_entry |
| 6710 | raid5_stripe_size = __ATTR(stripe_size, 0444, |
| 6711 | raid5_show_stripe_size, |
| 6712 | NULL); |
| 6713 | #endif |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6714 | |
| 6715 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6716 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6717 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6718 | struct r5conf *conf; |
| 6719 | int ret = 0; |
| 6720 | spin_lock(&mddev->lock); |
| 6721 | conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6722 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6723 | ret = sprintf(page, "%d\n", conf->bypass_threshold); |
| 6724 | spin_unlock(&mddev->lock); |
| 6725 | return ret; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6726 | } |
| 6727 | |
| 6728 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6729 | 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] | 6730 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6731 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6732 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6733 | int err; |
| 6734 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6735 | if (len >= PAGE_SIZE) |
| 6736 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6737 | if (kstrtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6738 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6739 | |
| 6740 | err = mddev_lock(mddev); |
| 6741 | if (err) |
| 6742 | return err; |
| 6743 | conf = mddev->private; |
| 6744 | if (!conf) |
| 6745 | err = -ENODEV; |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6746 | else if (new > conf->min_nr_stripes) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6747 | err = -EINVAL; |
| 6748 | else |
| 6749 | conf->bypass_threshold = new; |
| 6750 | mddev_unlock(mddev); |
| 6751 | return err ?: len; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6752 | } |
| 6753 | |
| 6754 | static struct md_sysfs_entry |
| 6755 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 6756 | S_IRUGO | S_IWUSR, |
| 6757 | raid5_show_preread_threshold, |
| 6758 | raid5_store_preread_threshold); |
| 6759 | |
| 6760 | static ssize_t |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6761 | raid5_show_skip_copy(struct mddev *mddev, char *page) |
| 6762 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6763 | struct r5conf *conf; |
| 6764 | int ret = 0; |
| 6765 | spin_lock(&mddev->lock); |
| 6766 | conf = mddev->private; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6767 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6768 | ret = sprintf(page, "%d\n", conf->skip_copy); |
| 6769 | spin_unlock(&mddev->lock); |
| 6770 | return ret; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6771 | } |
| 6772 | |
| 6773 | static ssize_t |
| 6774 | raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) |
| 6775 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6776 | struct r5conf *conf; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6777 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6778 | int err; |
| 6779 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6780 | if (len >= PAGE_SIZE) |
| 6781 | return -EINVAL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6782 | if (kstrtoul(page, 10, &new)) |
| 6783 | return -EINVAL; |
| 6784 | new = !!new; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6785 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6786 | err = mddev_lock(mddev); |
| 6787 | if (err) |
| 6788 | return err; |
| 6789 | conf = mddev->private; |
| 6790 | if (!conf) |
| 6791 | err = -ENODEV; |
| 6792 | else if (new != conf->skip_copy) { |
Christoph Hellwig | 1cb039f | 2020-09-24 08:51:38 +0200 | [diff] [blame] | 6793 | struct request_queue *q = mddev->queue; |
| 6794 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6795 | mddev_suspend(mddev); |
| 6796 | conf->skip_copy = new; |
| 6797 | if (new) |
Christoph Hellwig | 1cb039f | 2020-09-24 08:51:38 +0200 | [diff] [blame] | 6798 | blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6799 | else |
Christoph Hellwig | 1cb039f | 2020-09-24 08:51:38 +0200 | [diff] [blame] | 6800 | blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6801 | mddev_resume(mddev); |
| 6802 | } |
| 6803 | mddev_unlock(mddev); |
| 6804 | return err ?: len; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6805 | } |
| 6806 | |
| 6807 | static struct md_sysfs_entry |
| 6808 | raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, |
| 6809 | raid5_show_skip_copy, |
| 6810 | raid5_store_skip_copy); |
| 6811 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6812 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6813 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6814 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6815 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6816 | if (conf) |
| 6817 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 6818 | else |
| 6819 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6820 | } |
| 6821 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6822 | static struct md_sysfs_entry |
| 6823 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6824 | |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6825 | static ssize_t |
| 6826 | raid5_show_group_thread_cnt(struct mddev *mddev, char *page) |
| 6827 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6828 | struct r5conf *conf; |
| 6829 | int ret = 0; |
| 6830 | spin_lock(&mddev->lock); |
| 6831 | conf = mddev->private; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6832 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6833 | ret = sprintf(page, "%d\n", conf->worker_cnt_per_group); |
| 6834 | spin_unlock(&mddev->lock); |
| 6835 | return ret; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6836 | } |
| 6837 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6838 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 6839 | int *group_cnt, |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6840 | struct r5worker_group **worker_groups); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6841 | static ssize_t |
| 6842 | raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) |
| 6843 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6844 | struct r5conf *conf; |
Shaohua Li | 7d5d7b5 | 2017-09-21 11:03:52 -0700 | [diff] [blame] | 6845 | unsigned int new; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6846 | int err; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6847 | struct r5worker_group *new_groups, *old_groups; |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 6848 | int group_cnt; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6849 | |
| 6850 | if (len >= PAGE_SIZE) |
| 6851 | return -EINVAL; |
Shaohua Li | 7d5d7b5 | 2017-09-21 11:03:52 -0700 | [diff] [blame] | 6852 | if (kstrtouint(page, 10, &new)) |
| 6853 | return -EINVAL; |
| 6854 | /* 8192 should be big enough */ |
| 6855 | if (new > 8192) |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6856 | return -EINVAL; |
| 6857 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6858 | err = mddev_lock(mddev); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6859 | if (err) |
| 6860 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6861 | conf = mddev->private; |
| 6862 | if (!conf) |
| 6863 | err = -ENODEV; |
| 6864 | else if (new != conf->worker_cnt_per_group) { |
| 6865 | mddev_suspend(mddev); |
| 6866 | |
| 6867 | old_groups = conf->worker_groups; |
| 6868 | if (old_groups) |
| 6869 | flush_workqueue(raid5_wq); |
| 6870 | |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 6871 | err = alloc_thread_groups(conf, new, &group_cnt, &new_groups); |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6872 | if (!err) { |
| 6873 | spin_lock_irq(&conf->device_lock); |
| 6874 | conf->group_cnt = group_cnt; |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 6875 | conf->worker_cnt_per_group = new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6876 | conf->worker_groups = new_groups; |
| 6877 | spin_unlock_irq(&conf->device_lock); |
| 6878 | |
| 6879 | if (old_groups) |
| 6880 | kfree(old_groups[0].workers); |
| 6881 | kfree(old_groups); |
| 6882 | } |
| 6883 | mddev_resume(mddev); |
| 6884 | } |
| 6885 | mddev_unlock(mddev); |
| 6886 | |
| 6887 | return err ?: len; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6888 | } |
| 6889 | |
| 6890 | static struct md_sysfs_entry |
| 6891 | raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, |
| 6892 | raid5_show_group_thread_cnt, |
| 6893 | raid5_store_group_thread_cnt); |
| 6894 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6895 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6896 | &raid5_stripecache_size.attr, |
| 6897 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6898 | &raid5_preread_bypass_threshold.attr, |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6899 | &raid5_group_thread_cnt.attr, |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6900 | &raid5_skip_copy.attr, |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6901 | &raid5_rmw_level.attr, |
Yufen Yu | 3b5408b | 2020-07-18 05:29:09 -0400 | [diff] [blame] | 6902 | &raid5_stripe_size.attr, |
Song Liu | 2c7da14 | 2016-11-17 15:24:41 -0800 | [diff] [blame] | 6903 | &r5c_journal_mode.attr, |
Mariusz Dabrowski | a596d08 | 2019-02-18 15:04:09 +0100 | [diff] [blame] | 6904 | &ppl_write_hint.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6905 | NULL, |
| 6906 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6907 | static struct attribute_group raid5_attrs_group = { |
| 6908 | .name = NULL, |
| 6909 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6910 | }; |
| 6911 | |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 6912 | static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt, |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6913 | struct r5worker_group **worker_groups) |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6914 | { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6915 | int i, j, k; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6916 | ssize_t size; |
| 6917 | struct r5worker *workers; |
| 6918 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6919 | if (cnt == 0) { |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6920 | *group_cnt = 0; |
| 6921 | *worker_groups = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6922 | return 0; |
| 6923 | } |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6924 | *group_cnt = num_possible_nodes(); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6925 | size = sizeof(struct r5worker) * cnt; |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 6926 | workers = kcalloc(size, *group_cnt, GFP_NOIO); |
| 6927 | *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group), |
| 6928 | GFP_NOIO); |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6929 | if (!*worker_groups || !workers) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6930 | kfree(workers); |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6931 | kfree(*worker_groups); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6932 | return -ENOMEM; |
| 6933 | } |
| 6934 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6935 | for (i = 0; i < *group_cnt; i++) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6936 | struct r5worker_group *group; |
| 6937 | |
NeilBrown | 0c775d5 | 2013-11-25 11:12:43 +1100 | [diff] [blame] | 6938 | group = &(*worker_groups)[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6939 | INIT_LIST_HEAD(&group->handle_list); |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 6940 | INIT_LIST_HEAD(&group->loprio_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6941 | group->conf = conf; |
| 6942 | group->workers = workers + i * cnt; |
| 6943 | |
| 6944 | for (j = 0; j < cnt; j++) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6945 | struct r5worker *worker = group->workers + j; |
| 6946 | worker->group = group; |
| 6947 | INIT_WORK(&worker->work, raid5_do_work); |
| 6948 | |
| 6949 | for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) |
| 6950 | INIT_LIST_HEAD(worker->temp_inactive_list + k); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6951 | } |
| 6952 | } |
| 6953 | |
| 6954 | return 0; |
| 6955 | } |
| 6956 | |
| 6957 | static void free_thread_groups(struct r5conf *conf) |
| 6958 | { |
| 6959 | if (conf->worker_groups) |
| 6960 | kfree(conf->worker_groups[0].workers); |
| 6961 | kfree(conf->worker_groups); |
| 6962 | conf->worker_groups = NULL; |
| 6963 | } |
| 6964 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6965 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6966 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6967 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6968 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6969 | |
| 6970 | if (!sectors) |
| 6971 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6972 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 6973 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6974 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6975 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 6976 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
| 6977 | sectors &= ~((sector_t)conf->prev_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6978 | return sectors * (raid_disks - conf->max_degraded); |
| 6979 | } |
| 6980 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6981 | static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 6982 | { |
| 6983 | safe_put_page(percpu->spare_page); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6984 | percpu->spare_page = NULL; |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 6985 | kvfree(percpu->scribble); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6986 | percpu->scribble = NULL; |
| 6987 | } |
| 6988 | |
| 6989 | static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 6990 | { |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 6991 | if (conf->level == 6 && !percpu->spare_page) { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6992 | percpu->spare_page = alloc_page(GFP_KERNEL); |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 6993 | if (!percpu->spare_page) |
| 6994 | return -ENOMEM; |
| 6995 | } |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6996 | |
Kent Overstreet | b330e6a | 2019-03-11 23:31:06 -0700 | [diff] [blame] | 6997 | if (scribble_alloc(percpu, |
| 6998 | max(conf->raid_disks, |
| 6999 | conf->previous_raid_disks), |
| 7000 | max(conf->chunk_sectors, |
| 7001 | conf->prev_chunk_sectors) |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 7002 | / RAID5_STRIPE_SECTORS(conf))) { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7003 | free_scratch_buffer(conf, percpu); |
| 7004 | return -ENOMEM; |
| 7005 | } |
| 7006 | |
| 7007 | return 0; |
| 7008 | } |
| 7009 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7010 | static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node) |
| 7011 | { |
| 7012 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
| 7013 | |
| 7014 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); |
| 7015 | return 0; |
| 7016 | } |
| 7017 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7018 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7019 | { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7020 | if (!conf->percpu) |
| 7021 | return; |
| 7022 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7023 | cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7024 | free_percpu(conf->percpu); |
| 7025 | } |
| 7026 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7027 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7028 | { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 7029 | int i; |
| 7030 | |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7031 | log_exit(conf); |
| 7032 | |
Aliaksei Karaliou | 565e045 | 2017-12-23 21:20:31 +0300 | [diff] [blame] | 7033 | unregister_shrinker(&conf->shrinker); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7034 | free_thread_groups(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7035 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7036 | raid5_free_percpu(conf); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 7037 | for (i = 0; i < conf->pool_size; i++) |
| 7038 | if (conf->disks[i].extra_page) |
| 7039 | put_page(conf->disks[i].extra_page); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7040 | kfree(conf->disks); |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 7041 | bioset_exit(&conf->bio_split); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7042 | kfree(conf->stripe_hashtbl); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 7043 | kfree(conf->pending_data); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7044 | kfree(conf); |
| 7045 | } |
| 7046 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7047 | 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] | 7048 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7049 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7050 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 7051 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7052 | if (alloc_scratch_buffer(conf, percpu)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7053 | pr_warn("%s: failed memory allocation for cpu%u\n", |
| 7054 | __func__, cpu); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7055 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7056 | } |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7057 | return 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7058 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7059 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7060 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7061 | { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7062 | int err = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7063 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 7064 | conf->percpu = alloc_percpu(struct raid5_percpu); |
| 7065 | if (!conf->percpu) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7066 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7067 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 7068 | err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 7069 | if (!err) { |
| 7070 | conf->scribble_disks = max(conf->raid_disks, |
| 7071 | conf->previous_raid_disks); |
| 7072 | conf->scribble_sectors = max(conf->chunk_sectors, |
| 7073 | conf->prev_chunk_sectors); |
| 7074 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7075 | return err; |
| 7076 | } |
| 7077 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7078 | static unsigned long raid5_cache_scan(struct shrinker *shrink, |
| 7079 | struct shrink_control *sc) |
| 7080 | { |
| 7081 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 7082 | unsigned long ret = SHRINK_STOP; |
| 7083 | |
| 7084 | if (mutex_trylock(&conf->cache_size_mutex)) { |
| 7085 | ret= 0; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 7086 | while (ret < sc->nr_to_scan && |
| 7087 | conf->max_nr_stripes > conf->min_nr_stripes) { |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 7088 | if (drop_one_stripe(conf) == 0) { |
| 7089 | ret = SHRINK_STOP; |
| 7090 | break; |
| 7091 | } |
| 7092 | ret++; |
| 7093 | } |
| 7094 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7095 | } |
| 7096 | return ret; |
| 7097 | } |
| 7098 | |
| 7099 | static unsigned long raid5_cache_count(struct shrinker *shrink, |
| 7100 | struct shrink_control *sc) |
| 7101 | { |
| 7102 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
| 7103 | |
| 7104 | if (conf->max_nr_stripes < conf->min_nr_stripes) |
| 7105 | /* unlikely, but not impossible */ |
| 7106 | return 0; |
| 7107 | return conf->max_nr_stripes - conf->min_nr_stripes; |
| 7108 | } |
| 7109 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7110 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7111 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7112 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7113 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7114 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7115 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7116 | char pers_name[6]; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7117 | int i; |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 7118 | int group_cnt; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 7119 | struct r5worker_group *new_group; |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 7120 | int ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7122 | if (mddev->new_level != 5 |
| 7123 | && mddev->new_level != 4 |
| 7124 | && mddev->new_level != 6) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7125 | pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
| 7126 | mdname(mddev), mddev->new_level); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7127 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7128 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7129 | if ((mddev->new_level == 5 |
| 7130 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 7131 | (mddev->new_level == 6 |
| 7132 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7133 | pr_warn("md/raid:%s: layout %d not supported\n", |
| 7134 | mdname(mddev), mddev->new_layout); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7135 | return ERR_PTR(-EIO); |
| 7136 | } |
| 7137 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7138 | pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
| 7139 | mdname(mddev), mddev->raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7140 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 7141 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7142 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7143 | if (!mddev->new_chunk_sectors || |
| 7144 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 7145 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7146 | pr_warn("md/raid:%s: invalid chunk size %d\n", |
| 7147 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7148 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 7149 | } |
| 7150 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7151 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7152 | if (conf == NULL) |
| 7153 | goto abort; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 7154 | |
Yufen Yu | e236858 | 2020-07-18 05:29:08 -0400 | [diff] [blame] | 7155 | #if PAGE_SIZE != DEFAULT_STRIPE_SIZE |
| 7156 | conf->stripe_size = DEFAULT_STRIPE_SIZE; |
| 7157 | conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9; |
| 7158 | conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9; |
| 7159 | #endif |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 7160 | INIT_LIST_HEAD(&conf->free_list); |
| 7161 | INIT_LIST_HEAD(&conf->pending_list); |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 7162 | conf->pending_data = kcalloc(PENDING_IO_MAX, |
| 7163 | sizeof(struct r5pending_data), |
| 7164 | GFP_KERNEL); |
Shaohua Li | aaf9f12 | 2017-03-03 22:06:12 -0800 | [diff] [blame] | 7165 | if (!conf->pending_data) |
| 7166 | goto abort; |
| 7167 | for (i = 0; i < PENDING_IO_MAX; i++) |
| 7168 | list_add(&conf->pending_data[i].sibling, &conf->free_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7169 | /* Don't enable multi-threading by default*/ |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 7170 | if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) { |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 7171 | conf->group_cnt = group_cnt; |
Guoqing Jiang | d2c9ad4 | 2019-12-20 15:46:29 +0100 | [diff] [blame] | 7172 | conf->worker_cnt_per_group = 0; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 7173 | conf->worker_groups = new_group; |
| 7174 | } else |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 7175 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7176 | spin_lock_init(&conf->device_lock); |
Ahmed S. Darwish | 0a87b25 | 2020-07-20 17:55:25 +0200 | [diff] [blame] | 7177 | seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 7178 | mutex_init(&conf->cache_size_mutex); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 7179 | init_waitqueue_head(&conf->wait_for_quiescent); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 7180 | init_waitqueue_head(&conf->wait_for_stripe); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7181 | init_waitqueue_head(&conf->wait_for_overlap); |
| 7182 | INIT_LIST_HEAD(&conf->handle_list); |
Shaohua Li | 535ae4e | 2017-02-15 19:37:32 -0800 | [diff] [blame] | 7183 | INIT_LIST_HEAD(&conf->loprio_list); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7184 | INIT_LIST_HEAD(&conf->hold_list); |
| 7185 | INIT_LIST_HEAD(&conf->delayed_list); |
| 7186 | INIT_LIST_HEAD(&conf->bitmap_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 7187 | init_llist_head(&conf->released_stripes); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7188 | atomic_set(&conf->active_stripes, 0); |
| 7189 | atomic_set(&conf->preread_active_stripes, 0); |
| 7190 | atomic_set(&conf->active_aligned_reads, 0); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 7191 | spin_lock_init(&conf->pending_bios_lock); |
| 7192 | conf->batch_bio_dispatch = true; |
| 7193 | rdev_for_each(rdev, mddev) { |
| 7194 | if (test_bit(Journal, &rdev->flags)) |
| 7195 | continue; |
| 7196 | if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 7197 | conf->batch_bio_dispatch = false; |
| 7198 | break; |
| 7199 | } |
| 7200 | } |
| 7201 | |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 7202 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 7203 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7204 | |
| 7205 | conf->raid_disks = mddev->raid_disks; |
| 7206 | if (mddev->reshape_position == MaxSector) |
| 7207 | conf->previous_raid_disks = mddev->raid_disks; |
| 7208 | else |
| 7209 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7210 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7211 | |
Kees Cook | 6396bb2 | 2018-06-12 14:03:40 -0700 | [diff] [blame] | 7212 | conf->disks = kcalloc(max_disks, sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7213 | GFP_KERNEL); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 7214 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7215 | if (!conf->disks) |
| 7216 | goto abort; |
| 7217 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 7218 | for (i = 0; i < max_disks; i++) { |
| 7219 | conf->disks[i].extra_page = alloc_page(GFP_KERNEL); |
| 7220 | if (!conf->disks[i].extra_page) |
| 7221 | goto abort; |
| 7222 | } |
| 7223 | |
Kent Overstreet | afeee51 | 2018-05-20 18:25:52 -0400 | [diff] [blame] | 7224 | ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0); |
| 7225 | if (ret) |
NeilBrown | dd7a8f5 | 2017-04-05 14:05:51 +1000 | [diff] [blame] | 7226 | goto abort; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7227 | conf->mddev = mddev; |
| 7228 | |
| 7229 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 7230 | goto abort; |
| 7231 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7232 | /* We init hash_locks[0] separately to that it can be used |
| 7233 | * as the reference lock in the spin_lock_nest_lock() call |
| 7234 | * in lock_all_device_hash_locks_irq in order to convince |
| 7235 | * lockdep that we know what we are doing. |
| 7236 | */ |
| 7237 | spin_lock_init(conf->hash_locks); |
| 7238 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 7239 | spin_lock_init(conf->hash_locks + i); |
| 7240 | |
| 7241 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 7242 | INIT_LIST_HEAD(conf->inactive_list + i); |
| 7243 | |
| 7244 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 7245 | INIT_LIST_HEAD(conf->temp_inactive_list + i); |
| 7246 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 7247 | atomic_set(&conf->r5c_cached_full_stripes, 0); |
| 7248 | INIT_LIST_HEAD(&conf->r5c_full_stripe_list); |
| 7249 | atomic_set(&conf->r5c_cached_partial_stripes, 0); |
| 7250 | INIT_LIST_HEAD(&conf->r5c_partial_stripe_list); |
Shaohua Li | e33fbb9 | 2017-02-10 16:18:09 -0800 | [diff] [blame] | 7251 | atomic_set(&conf->r5c_flushing_full_stripes, 0); |
| 7252 | atomic_set(&conf->r5c_flushing_partial_stripes, 0); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 7253 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7254 | conf->level = mddev->new_level; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 7255 | conf->chunk_sectors = mddev->new_chunk_sectors; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 7256 | if (raid5_alloc_percpu(conf) != 0) |
| 7257 | goto abort; |
| 7258 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7259 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7260 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7261 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7262 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7263 | if (raid_disk >= max_disks |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7264 | || raid_disk < 0 || test_bit(Journal, &rdev->flags)) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7265 | continue; |
| 7266 | disk = conf->disks + raid_disk; |
| 7267 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7268 | if (test_bit(Replacement, &rdev->flags)) { |
| 7269 | if (disk->replacement) |
| 7270 | goto abort; |
| 7271 | disk->replacement = rdev; |
| 7272 | } else { |
| 7273 | if (disk->rdev) |
| 7274 | goto abort; |
| 7275 | disk->rdev = rdev; |
| 7276 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7277 | |
| 7278 | if (test_bit(In_sync, &rdev->flags)) { |
| 7279 | char b[BDEVNAME_SIZE]; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7280 | pr_info("md/raid:%s: device %s operational as raid disk %d\n", |
| 7281 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 7282 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7283 | /* Cannot rely on bitmap to complete recovery */ |
| 7284 | conf->fullsync = 1; |
| 7285 | } |
| 7286 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7287 | conf->level = mddev->new_level; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7288 | if (conf->level == 6) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7289 | conf->max_degraded = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7290 | if (raid6_call.xor_syndrome) |
| 7291 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 7292 | else |
| 7293 | conf->rmw_level = PARITY_DISABLE_RMW; |
| 7294 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7295 | conf->max_degraded = 1; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 7296 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 7297 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7298 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7299 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7300 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7301 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7302 | conf->prev_algo = mddev->layout; |
NeilBrown | 5cac6bc | 2015-07-17 12:17:50 +1000 | [diff] [blame] | 7303 | } else { |
| 7304 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 7305 | conf->prev_algo = conf->algorithm; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 7306 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7307 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7308 | conf->min_nr_stripes = NR_STRIPES; |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 7309 | if (mddev->reshape_position != MaxSector) { |
| 7310 | int stripes = max_t(int, |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 7311 | ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4, |
| 7312 | ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4); |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 7313 | conf->min_nr_stripes = max(NR_STRIPES, stripes); |
| 7314 | if (conf->min_nr_stripes != NR_STRIPES) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7315 | pr_info("md/raid:%s: force stripe size %d for reshape\n", |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 7316 | mdname(mddev), conf->min_nr_stripes); |
| 7317 | } |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7318 | memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 7319 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7320 | atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7321 | if (grow_stripes(conf, conf->min_nr_stripes)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7322 | pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 7323 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7324 | goto abort; |
| 7325 | } else |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7326 | pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7327 | /* |
| 7328 | * Losing a stripe head costs more than the time to refill it, |
| 7329 | * it reduces the queue depth and so can hurt throughput. |
| 7330 | * So set it rather large, scaled by number of devices. |
| 7331 | */ |
| 7332 | conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4; |
| 7333 | conf->shrinker.scan_objects = raid5_cache_scan; |
| 7334 | conf->shrinker.count_objects = raid5_cache_count; |
| 7335 | conf->shrinker.batch = 128; |
| 7336 | conf->shrinker.flags = 0; |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 7337 | if (register_shrinker(&conf->shrinker)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7338 | pr_warn("md/raid:%s: couldn't register shrinker.\n", |
| 7339 | mdname(mddev)); |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 7340 | goto abort; |
| 7341 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7342 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 7343 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 7344 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7345 | if (!conf->thread) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7346 | pr_warn("md/raid:%s: couldn't allocate thread.\n", |
| 7347 | mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7348 | goto abort; |
| 7349 | } |
| 7350 | |
| 7351 | return conf; |
| 7352 | |
| 7353 | abort: |
| 7354 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7355 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7356 | return ERR_PTR(-EIO); |
| 7357 | } else |
| 7358 | return ERR_PTR(-ENOMEM); |
| 7359 | } |
| 7360 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7361 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 7362 | { |
| 7363 | switch (algo) { |
| 7364 | case ALGORITHM_PARITY_0: |
| 7365 | if (raid_disk < max_degraded) |
| 7366 | return 1; |
| 7367 | break; |
| 7368 | case ALGORITHM_PARITY_N: |
| 7369 | if (raid_disk >= raid_disks - max_degraded) |
| 7370 | return 1; |
| 7371 | break; |
| 7372 | case ALGORITHM_PARITY_0_6: |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 7373 | if (raid_disk == 0 || |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7374 | raid_disk == raid_disks - 1) |
| 7375 | return 1; |
| 7376 | break; |
| 7377 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 7378 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 7379 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 7380 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 7381 | if (raid_disk == raid_disks - 1) |
| 7382 | return 1; |
| 7383 | } |
| 7384 | return 0; |
| 7385 | } |
| 7386 | |
Christoph Hellwig | 16ef510 | 2020-09-24 08:51:33 +0200 | [diff] [blame] | 7387 | static void raid5_set_io_opt(struct r5conf *conf) |
| 7388 | { |
| 7389 | blk_queue_io_opt(conf->mddev->queue, (conf->chunk_sectors << 9) * |
| 7390 | (conf->raid_disks - conf->max_degraded)); |
| 7391 | } |
| 7392 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7393 | static int raid5_run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7394 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7395 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7396 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7397 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7398 | struct md_rdev *rdev; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7399 | struct md_rdev *journal_dev = NULL; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7400 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7401 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7402 | long long min_offset_diff = 0; |
| 7403 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7404 | |
NeilBrown | a415c0f | 2017-06-05 16:05:13 +1000 | [diff] [blame] | 7405 | if (mddev_init_writes_pending(mddev) < 0) |
| 7406 | return -ENOMEM; |
| 7407 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 7408 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7409 | pr_notice("md/raid:%s: not clean -- starting background reconstruction\n", |
| 7410 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7411 | |
| 7412 | rdev_for_each(rdev, mddev) { |
| 7413 | long long diff; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7414 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7415 | if (test_bit(Journal, &rdev->flags)) { |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7416 | journal_dev = rdev; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 7417 | continue; |
| 7418 | } |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7419 | if (rdev->raid_disk < 0) |
| 7420 | continue; |
| 7421 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 7422 | if (first) { |
| 7423 | min_offset_diff = diff; |
| 7424 | first = 0; |
| 7425 | } else if (mddev->reshape_backwards && |
| 7426 | diff < min_offset_diff) |
| 7427 | min_offset_diff = diff; |
| 7428 | else if (!mddev->reshape_backwards && |
| 7429 | diff > min_offset_diff) |
| 7430 | min_offset_diff = diff; |
| 7431 | } |
| 7432 | |
NeilBrown | 230b55f | 2017-10-17 14:24:09 +1100 | [diff] [blame] | 7433 | if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) && |
| 7434 | (mddev->bitmap_info.offset || mddev->bitmap_info.file)) { |
| 7435 | pr_notice("md/raid:%s: array cannot have both journal and bitmap\n", |
| 7436 | mdname(mddev)); |
| 7437 | return -EINVAL; |
| 7438 | } |
| 7439 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7440 | if (mddev->reshape_position != MaxSector) { |
| 7441 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7442 | * Difficulties arise if the stripe we would write to |
| 7443 | * next is at or after the stripe we would read from next. |
| 7444 | * For a reshape that changes the number of devices, this |
| 7445 | * is only possible for a very short time, and mdadm makes |
| 7446 | * sure that time appears to have past before assembling |
| 7447 | * the array. So we fail if that time hasn't passed. |
| 7448 | * For a reshape that keeps the number of devices the same |
| 7449 | * mdadm must be monitoring the reshape can keeping the |
| 7450 | * critical areas read-only and backed up. It will start |
| 7451 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7452 | */ |
| 7453 | sector_t here_new, here_old; |
| 7454 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 7455 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7456 | int chunk_sectors; |
| 7457 | int new_data_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7458 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7459 | if (journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7460 | pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n", |
| 7461 | mdname(mddev)); |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7462 | return -EINVAL; |
| 7463 | } |
| 7464 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7465 | if (mddev->new_level != mddev->level) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7466 | pr_warn("md/raid:%s: unsupported reshape required - aborting.\n", |
| 7467 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7468 | return -EINVAL; |
| 7469 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7470 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 7471 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7472 | * further up in new geometry must map after here in old |
| 7473 | * geometry. |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7474 | * If the chunk sizes are different, then as we perform reshape |
| 7475 | * in units of the largest of the two, reshape_position needs |
| 7476 | * be a multiple of the largest chunk size times new data disks. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7477 | */ |
| 7478 | here_new = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7479 | chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors); |
| 7480 | new_data_disks = mddev->raid_disks - max_degraded; |
| 7481 | if (sector_div(here_new, chunk_sectors * new_data_disks)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7482 | pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n", |
| 7483 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7484 | return -EINVAL; |
| 7485 | } |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7486 | reshape_offset = here_new * chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7487 | /* here_new is the stripe we will write to */ |
| 7488 | here_old = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7489 | sector_div(here_old, chunk_sectors * (old_disks-max_degraded)); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7490 | /* here_old is the first stripe that we might need to read |
| 7491 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7492 | if (mddev->delta_disks == 0) { |
| 7493 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7494 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7495 | * and taking constant backups. |
| 7496 | * mdadm always starts a situation like this in |
| 7497 | * readonly mode so it can take control before |
| 7498 | * allowing any writes. So just check for that. |
| 7499 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7500 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 7501 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 7502 | /* not really in-place - so OK */; |
| 7503 | else if (mddev->ro == 0) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7504 | pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n", |
| 7505 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7506 | return -EINVAL; |
| 7507 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7508 | } else if (mddev->reshape_backwards |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7509 | ? (here_new * chunk_sectors + min_offset_diff <= |
| 7510 | here_old * chunk_sectors) |
| 7511 | : (here_new * chunk_sectors >= |
| 7512 | here_old * chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7513 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7514 | pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n", |
| 7515 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7516 | return -EINVAL; |
| 7517 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7518 | pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7519 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7520 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7521 | BUG_ON(mddev->level != mddev->new_level); |
| 7522 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7523 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7524 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7525 | } |
| 7526 | |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 7527 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags) && |
| 7528 | test_bit(MD_HAS_PPL, &mddev->flags)) { |
| 7529 | pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n", |
| 7530 | mdname(mddev)); |
| 7531 | clear_bit(MD_HAS_PPL, &mddev->flags); |
Pawel Baldysiak | ddc0882 | 2017-08-16 17:13:45 +0200 | [diff] [blame] | 7532 | clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags); |
Artur Paszkiewicz | 3418d03 | 2017-03-09 09:59:59 +0100 | [diff] [blame] | 7533 | } |
| 7534 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7535 | if (mddev->private == NULL) |
| 7536 | conf = setup_conf(mddev); |
| 7537 | else |
| 7538 | conf = mddev->private; |
| 7539 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7540 | if (IS_ERR(conf)) |
| 7541 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 7542 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 7543 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 7544 | if (!journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7545 | pr_warn("md/raid:%s: journal disk is missing, force array readonly\n", |
| 7546 | mdname(mddev)); |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 7547 | mddev->ro = 1; |
| 7548 | set_disk_ro(mddev->gendisk, 1); |
| 7549 | } else if (mddev->recovery_cp == MaxSector) |
| 7550 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
Shaohua Li | 7dde2ad | 2015-10-08 21:54:10 -0700 | [diff] [blame] | 7551 | } |
| 7552 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7553 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7554 | mddev->thread = conf->thread; |
| 7555 | conf->thread = NULL; |
| 7556 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7557 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7558 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 7559 | i++) { |
| 7560 | rdev = conf->disks[i].rdev; |
| 7561 | if (!rdev && conf->disks[i].replacement) { |
| 7562 | /* The replacement is all we have yet */ |
| 7563 | rdev = conf->disks[i].replacement; |
| 7564 | conf->disks[i].replacement = NULL; |
| 7565 | clear_bit(Replacement, &rdev->flags); |
| 7566 | conf->disks[i].rdev = rdev; |
| 7567 | } |
| 7568 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7569 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7570 | if (conf->disks[i].replacement && |
| 7571 | conf->reshape_progress != MaxSector) { |
| 7572 | /* replacements and reshape simply do not mix. */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7573 | pr_warn("md: cannot handle concurrent replacement and reshape.\n"); |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7574 | goto abort; |
| 7575 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 7576 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7577 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 7578 | continue; |
| 7579 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7580 | /* This disc is not fully in-sync. However if it |
| 7581 | * just stored parity (beyond the recovery_offset), |
| 7582 | * when we don't need to be concerned about the |
| 7583 | * array being dirty. |
| 7584 | * When reshape goes 'backwards', we never have |
| 7585 | * partially completed devices, so we only need |
| 7586 | * to worry about reshape going forwards. |
| 7587 | */ |
| 7588 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 7589 | if (mddev->major_version == 0 && |
| 7590 | mddev->minor_version > 90) |
| 7591 | rdev->recovery_offset = reshape_offset; |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7592 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7593 | if (rdev->recovery_offset < reshape_offset) { |
| 7594 | /* We need to check old and new layout */ |
| 7595 | if (!only_parity(rdev->raid_disk, |
| 7596 | conf->algorithm, |
| 7597 | conf->raid_disks, |
| 7598 | conf->max_degraded)) |
| 7599 | continue; |
| 7600 | } |
| 7601 | if (!only_parity(rdev->raid_disk, |
| 7602 | conf->prev_algo, |
| 7603 | conf->previous_raid_disks, |
| 7604 | conf->max_degraded)) |
| 7605 | continue; |
| 7606 | dirty_parity_disks++; |
| 7607 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7608 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7609 | /* |
| 7610 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 7611 | */ |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7612 | mddev->degraded = raid5_calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7613 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 7614 | if (has_failed(conf)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7615 | pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7616 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7617 | goto abort; |
| 7618 | } |
| 7619 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7620 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7621 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7622 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 7623 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7624 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7625 | mddev->recovery_cp != MaxSector) { |
Artur Paszkiewicz | 4536bf9b | 2017-03-09 10:00:01 +0100 | [diff] [blame] | 7626 | if (test_bit(MD_HAS_PPL, &mddev->flags)) |
| 7627 | pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n", |
| 7628 | mdname(mddev)); |
| 7629 | else if (mddev->ok_start_degraded) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7630 | pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n", |
| 7631 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7632 | else { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7633 | pr_crit("md/raid:%s: cannot start dirty degraded array.\n", |
| 7634 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7635 | goto abort; |
| 7636 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7637 | } |
| 7638 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7639 | pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n", |
| 7640 | mdname(mddev), conf->level, |
| 7641 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 7642 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7643 | |
| 7644 | print_raid5_conf(conf); |
| 7645 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7646 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7647 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7648 | atomic_set(&conf->reshape_stripes, 0); |
| 7649 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7650 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7651 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7652 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7653 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7654 | "reshape"); |
Aditya Pakki | e406f12 | 2019-03-04 16:48:54 -0600 | [diff] [blame] | 7655 | if (!mddev->sync_thread) |
| 7656 | goto abort; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7657 | } |
| 7658 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7659 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7660 | if (mddev->to_remove == &raid5_attrs_group) |
| 7661 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7662 | else if (mddev->kobj.sd && |
| 7663 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7664 | pr_warn("raid5: failed to create sysfs attributes for %s\n", |
| 7665 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7666 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 7667 | |
| 7668 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7669 | int chunk_size; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7670 | /* read-ahead size must cover two whole stripes, which |
| 7671 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 7672 | * number of raid devices |
| 7673 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7674 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 7675 | int stripe = data_disks * |
| 7676 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7677 | |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7678 | chunk_size = mddev->chunk_sectors << 9; |
| 7679 | blk_queue_io_min(mddev->queue, chunk_size); |
Christoph Hellwig | 16ef510 | 2020-09-24 08:51:33 +0200 | [diff] [blame] | 7680 | raid5_set_io_opt(conf); |
Kent Overstreet | c78afc6 | 2013-07-11 22:39:53 -0700 | [diff] [blame] | 7681 | mddev->queue->limits.raid_partial_stripes_expensive = 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7682 | /* |
| 7683 | * We can only discard a whole stripe. It doesn't make sense to |
| 7684 | * discard data disk but write parity disk |
| 7685 | */ |
| 7686 | stripe = stripe * PAGE_SIZE; |
NeilBrown | 4ac6875 | 2012-11-19 13:11:26 +1100 | [diff] [blame] | 7687 | /* Round up to power of 2, as discard handling |
| 7688 | * currently assumes that */ |
| 7689 | while ((stripe-1) & stripe) |
| 7690 | stripe = (stripe | (stripe-1)) + 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7691 | mddev->queue->limits.discard_alignment = stripe; |
| 7692 | mddev->queue->limits.discard_granularity = stripe; |
Konstantin Khlebnikov | e8d7c33 | 2016-11-27 19:32:32 +0300 | [diff] [blame] | 7693 | |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7694 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
Christoph Hellwig | 3deff1a | 2017-04-05 19:21:03 +0200 | [diff] [blame] | 7695 | blk_queue_max_write_zeroes_sectors(mddev->queue, 0); |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7696 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7697 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7698 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7699 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7700 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7701 | rdev->new_data_offset << 9); |
| 7702 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7703 | |
Christoph Hellwig | 48920ff | 2017-04-05 19:21:23 +0200 | [diff] [blame] | 7704 | /* |
| 7705 | * zeroing is required, otherwise data |
| 7706 | * could be lost. Consider a scenario: discard a stripe |
| 7707 | * (the stripe could be inconsistent if |
| 7708 | * discard_zeroes_data is 0); write one disk of the |
| 7709 | * stripe (the stripe could be inconsistent again |
| 7710 | * depending on which disks are used to calculate |
| 7711 | * parity); the disk is broken; The stripe data of this |
| 7712 | * disk is lost. |
| 7713 | * |
| 7714 | * We only allow DISCARD if the sysadmin has confirmed that |
| 7715 | * only safe devices are in use by setting a module parameter. |
| 7716 | * A better idea might be to turn DISCARD into WRITE_ZEROES |
| 7717 | * requests, as that is required to be safe. |
| 7718 | */ |
| 7719 | if (devices_handle_discard_safely && |
Jes Sorensen | e7597e6 | 2016-02-16 16:44:24 -0500 | [diff] [blame] | 7720 | mddev->queue->limits.max_discard_sectors >= (stripe >> 9) && |
| 7721 | mddev->queue->limits.discard_granularity >= stripe) |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 7722 | blk_queue_flag_set(QUEUE_FLAG_DISCARD, |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7723 | mddev->queue); |
| 7724 | else |
Bart Van Assche | 8b904b5 | 2018-03-07 17:10:10 -0800 | [diff] [blame] | 7725 | blk_queue_flag_clear(QUEUE_FLAG_DISCARD, |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7726 | mddev->queue); |
Shaohua Li | 1dffddd | 2016-09-08 10:49:06 -0700 | [diff] [blame] | 7727 | |
| 7728 | blk_queue_max_hw_sectors(mddev->queue, UINT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7729 | } |
| 7730 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 7731 | if (log_init(conf, journal_dev, raid5_has_ppl(conf))) |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7732 | goto abort; |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 7733 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7734 | return 0; |
| 7735 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7736 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 7737 | print_raid5_conf(conf); |
| 7738 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7739 | mddev->private = NULL; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7740 | pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7741 | return -EIO; |
| 7742 | } |
| 7743 | |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7744 | static void raid5_free(struct mddev *mddev, void *priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7745 | { |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7746 | struct r5conf *conf = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7747 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7748 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7749 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7750 | } |
| 7751 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7752 | static void raid5_status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7753 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7754 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7755 | int i; |
| 7756 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7757 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7758 | conf->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7759 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
NeilBrown | 5fd1335 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 7760 | rcu_read_lock(); |
| 7761 | for (i = 0; i < conf->raid_disks; i++) { |
| 7762 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
| 7763 | seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 7764 | } |
| 7765 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7766 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7767 | } |
| 7768 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7769 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7770 | { |
| 7771 | int i; |
| 7772 | struct disk_info *tmp; |
| 7773 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7774 | pr_debug("RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7775 | if (!conf) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7776 | pr_debug("(conf==NULL)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7777 | return; |
| 7778 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7779 | pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level, |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7780 | conf->raid_disks, |
| 7781 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7782 | |
| 7783 | for (i = 0; i < conf->raid_disks; i++) { |
| 7784 | char b[BDEVNAME_SIZE]; |
| 7785 | tmp = conf->disks + i; |
| 7786 | if (tmp->rdev) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7787 | pr_debug(" disk %d, o:%d, dev:%s\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7788 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 7789 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7790 | } |
| 7791 | } |
| 7792 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7793 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7794 | { |
| 7795 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7796 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7797 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7798 | int count = 0; |
| 7799 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7800 | |
| 7801 | for (i = 0; i < conf->raid_disks; i++) { |
| 7802 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7803 | if (tmp->replacement |
| 7804 | && tmp->replacement->recovery_offset == MaxSector |
| 7805 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 7806 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 7807 | /* Replacement has just become active. */ |
| 7808 | if (!tmp->rdev |
| 7809 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 7810 | count++; |
| 7811 | if (tmp->rdev) { |
| 7812 | /* Replaced device not technically faulty, |
| 7813 | * but we need to be sure it gets removed |
| 7814 | * and never re-added. |
| 7815 | */ |
| 7816 | set_bit(Faulty, &tmp->rdev->flags); |
| 7817 | sysfs_notify_dirent_safe( |
| 7818 | tmp->rdev->sysfs_state); |
| 7819 | } |
| 7820 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 7821 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7822 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7823 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 7824 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7825 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7826 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7827 | } |
| 7828 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7829 | spin_lock_irqsave(&conf->device_lock, flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7830 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7831 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7832 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7833 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7834 | } |
| 7835 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7836 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7837 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7838 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7839 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7840 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7841 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7842 | struct disk_info *p = conf->disks + number; |
| 7843 | |
| 7844 | print_raid5_conf(conf); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7845 | if (test_bit(Journal, &rdev->flags) && conf->log) { |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7846 | /* |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7847 | * we can't wait pending write here, as this is called in |
| 7848 | * raid5d, wait will deadlock. |
NeilBrown | 84dd97a | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7849 | * neilb: there is no locking about new writes here, |
| 7850 | * so this cannot be safe. |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7851 | */ |
Song Liu | 70d466f | 2017-05-11 15:28:28 -0700 | [diff] [blame] | 7852 | if (atomic_read(&conf->active_stripes) || |
| 7853 | atomic_read(&conf->r5c_cached_full_stripes) || |
| 7854 | atomic_read(&conf->r5c_cached_partial_stripes)) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7855 | return -EBUSY; |
NeilBrown | 84dd97a | 2017-03-15 14:05:14 +1100 | [diff] [blame] | 7856 | } |
Artur Paszkiewicz | ff87573 | 2017-03-09 09:59:58 +0100 | [diff] [blame] | 7857 | log_exit(conf); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7858 | return 0; |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7859 | } |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7860 | if (rdev == p->rdev) |
| 7861 | rdevp = &p->rdev; |
| 7862 | else if (rdev == p->replacement) |
| 7863 | rdevp = &p->replacement; |
| 7864 | else |
| 7865 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7866 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7867 | if (number >= conf->raid_disks && |
| 7868 | conf->reshape_progress == MaxSector) |
| 7869 | clear_bit(In_sync, &rdev->flags); |
| 7870 | |
| 7871 | if (test_bit(In_sync, &rdev->flags) || |
| 7872 | atomic_read(&rdev->nr_pending)) { |
| 7873 | err = -EBUSY; |
| 7874 | goto abort; |
| 7875 | } |
| 7876 | /* Only remove non-faulty devices if recovery |
| 7877 | * isn't possible. |
| 7878 | */ |
| 7879 | if (!test_bit(Faulty, &rdev->flags) && |
| 7880 | mddev->recovery_disabled != conf->recovery_disabled && |
| 7881 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7882 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7883 | number < conf->raid_disks) { |
| 7884 | err = -EBUSY; |
| 7885 | goto abort; |
| 7886 | } |
| 7887 | *rdevp = NULL; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 7888 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
| 7889 | synchronize_rcu(); |
| 7890 | if (atomic_read(&rdev->nr_pending)) { |
| 7891 | /* lost the race, try later */ |
| 7892 | err = -EBUSY; |
| 7893 | *rdevp = rdev; |
| 7894 | } |
| 7895 | } |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 7896 | if (!err) { |
| 7897 | err = log_modify(conf, rdev, false); |
| 7898 | if (err) |
| 7899 | goto abort; |
| 7900 | } |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 7901 | if (p->replacement) { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7902 | /* We must have just cleared 'rdev' */ |
| 7903 | p->rdev = p->replacement; |
| 7904 | clear_bit(Replacement, &p->replacement->flags); |
| 7905 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 7906 | * but will never see neither - if they are careful |
| 7907 | */ |
| 7908 | p->replacement = NULL; |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 7909 | |
| 7910 | if (!err) |
| 7911 | err = log_modify(conf, p->rdev, true); |
Guoqing Jiang | e5bc9c3 | 2017-04-24 15:58:04 +0800 | [diff] [blame] | 7912 | } |
| 7913 | |
| 7914 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7915 | abort: |
| 7916 | |
| 7917 | print_raid5_conf(conf); |
| 7918 | return err; |
| 7919 | } |
| 7920 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7921 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7922 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7923 | struct r5conf *conf = mddev->private; |
Xiao Ni | d9771f5 | 2019-06-14 15:41:05 -0700 | [diff] [blame] | 7924 | int ret, err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7925 | int disk; |
| 7926 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7927 | int first = 0; |
| 7928 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7929 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7930 | if (test_bit(Journal, &rdev->flags)) { |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7931 | if (conf->log) |
| 7932 | return -EBUSY; |
| 7933 | |
| 7934 | rdev->raid_disk = 0; |
| 7935 | /* |
| 7936 | * The array is in readonly mode if journal is missing, so no |
| 7937 | * write requests running. We should be safe |
| 7938 | */ |
Xiao Ni | d9771f5 | 2019-06-14 15:41:05 -0700 | [diff] [blame] | 7939 | ret = log_init(conf, rdev, false); |
| 7940 | if (ret) |
| 7941 | return ret; |
| 7942 | |
| 7943 | ret = r5l_start(conf->log); |
| 7944 | if (ret) |
| 7945 | return ret; |
| 7946 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7947 | return 0; |
| 7948 | } |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 7949 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 7950 | return -EBUSY; |
| 7951 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 7952 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7953 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7954 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7955 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7956 | if (rdev->raid_disk >= 0) |
| 7957 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7958 | |
| 7959 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7960 | * find the disk ... but prefer rdev->saved_raid_disk |
| 7961 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7962 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7963 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7964 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7965 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7966 | first = rdev->saved_raid_disk; |
| 7967 | |
| 7968 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7969 | p = conf->disks + disk; |
| 7970 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7971 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7972 | rdev->raid_disk = disk; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7973 | if (rdev->saved_raid_disk != disk) |
| 7974 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 7975 | rcu_assign_pointer(p->rdev, rdev); |
Artur Paszkiewicz | 6358c23 | 2017-03-09 10:00:02 +0100 | [diff] [blame] | 7976 | |
| 7977 | err = log_modify(conf, rdev, true); |
| 7978 | |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7979 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7980 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7981 | } |
| 7982 | for (disk = first; disk <= last; disk++) { |
| 7983 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7984 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 7985 | p->replacement == NULL) { |
| 7986 | clear_bit(In_sync, &rdev->flags); |
| 7987 | set_bit(Replacement, &rdev->flags); |
| 7988 | rdev->raid_disk = disk; |
| 7989 | err = 0; |
| 7990 | conf->fullsync = 1; |
| 7991 | rcu_assign_pointer(p->replacement, rdev); |
| 7992 | break; |
| 7993 | } |
| 7994 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7995 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7996 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7997 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7998 | } |
| 7999 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8000 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8001 | { |
| 8002 | /* no resync is happening, and there is enough space |
| 8003 | * on all devices, so we can resize. |
| 8004 | * We need to make sure resync covers any new space. |
| 8005 | * If the array is shrinking we should possibly wait until |
| 8006 | * any io in the removed space completes, but it hardly seems |
| 8007 | * worth it. |
| 8008 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 8009 | sector_t newsize; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 8010 | struct r5conf *conf = mddev->private; |
| 8011 | |
Shaohua Li | e254de6 | 2018-08-29 11:05:42 -0700 | [diff] [blame] | 8012 | if (raid5_has_log(conf) || raid5_has_ppl(conf)) |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 8013 | return -EINVAL; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 8014 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 8015 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 8016 | if (mddev->external_size && |
| 8017 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 8018 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 8019 | if (mddev->bitmap) { |
Andy Shevchenko | e64e4018 | 2018-08-01 15:20:50 -0700 | [diff] [blame] | 8020 | int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 8021 | if (ret) |
| 8022 | return ret; |
| 8023 | } |
| 8024 | md_set_array_sectors(mddev, newsize); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 8025 | if (sectors > mddev->dev_sectors && |
| 8026 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8027 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8028 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 8029 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 8030 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 8031 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8032 | return 0; |
| 8033 | } |
| 8034 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8035 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 8036 | { |
| 8037 | /* Can only proceed if there are plenty of stripe_heads. |
| 8038 | * We need a minimum of one full stripe,, and for sensible progress |
| 8039 | * it is best to have about 4 times that. |
| 8040 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 8041 | * allow for chunk sizes up to 256K, which is probably OK. |
| 8042 | * If the chunk size is greater, user-space should request more |
| 8043 | * stripe_heads first. |
| 8044 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8045 | struct r5conf *conf = mddev->private; |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 8046 | if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 8047 | > conf->min_nr_stripes || |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 8048 | ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 8049 | > conf->min_nr_stripes) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 8050 | pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 8051 | mdname(mddev), |
| 8052 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 8053 | / RAID5_STRIPE_SIZE(conf))*4); |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 8054 | return 0; |
| 8055 | } |
| 8056 | return 1; |
| 8057 | } |
| 8058 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8059 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8060 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8061 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8062 | |
Shaohua Li | e254de6 | 2018-08-29 11:05:42 -0700 | [diff] [blame] | 8063 | if (raid5_has_log(conf) || raid5_has_ppl(conf)) |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 8064 | return -EINVAL; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8065 | if (mddev->delta_disks == 0 && |
| 8066 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 8067 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8068 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 8069 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8070 | return -EINVAL; |
NeilBrown | fdcfbbb | 2013-07-04 16:38:16 +1000 | [diff] [blame] | 8071 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8072 | /* We might be able to shrink, but the devices must |
| 8073 | * be made bigger first. |
| 8074 | * For raid6, 4 is the minimum size. |
| 8075 | * Otherwise 2 is the minimum |
| 8076 | */ |
| 8077 | int min = 2; |
| 8078 | if (mddev->level == 6) |
| 8079 | min = 4; |
| 8080 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 8081 | return -EINVAL; |
| 8082 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8083 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 8084 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8085 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8086 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 8087 | if (mddev->new_chunk_sectors > mddev->chunk_sectors || |
| 8088 | mddev->delta_disks > 0) |
| 8089 | if (resize_chunks(conf, |
| 8090 | conf->previous_raid_disks |
| 8091 | + max(0, mddev->delta_disks), |
| 8092 | max(mddev->new_chunk_sectors, |
| 8093 | mddev->chunk_sectors) |
| 8094 | ) < 0) |
| 8095 | return -ENOMEM; |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 8096 | |
| 8097 | if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size) |
| 8098 | return 0; /* never bother to shrink */ |
NeilBrown | e56108d6 | 2012-10-11 14:24:13 +1100 | [diff] [blame] | 8099 | return resize_stripes(conf, (conf->previous_raid_disks |
| 8100 | + mddev->delta_disks)); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8101 | } |
| 8102 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8103 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8104 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8105 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8106 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8107 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 8108 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8109 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 8110 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8111 | return -EBUSY; |
| 8112 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 8113 | if (!check_stripe_cache(mddev)) |
| 8114 | return -ENOSPC; |
| 8115 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 8116 | if (has_failed(conf)) |
| 8117 | return -EINVAL; |
| 8118 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8119 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8120 | if (!test_bit(In_sync, &rdev->flags) |
| 8121 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8122 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8123 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8124 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 8125 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8126 | /* Not enough devices even to make a degraded array |
| 8127 | * of that size |
| 8128 | */ |
| 8129 | return -EINVAL; |
| 8130 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8131 | /* Refuse to reduce size of the array. Any reductions in |
| 8132 | * array size must be through explicit setting of array_size |
| 8133 | * attribute. |
| 8134 | */ |
| 8135 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 8136 | < mddev->array_sectors) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 8137 | pr_warn("md/raid:%s: array size must be reduced before number of disks\n", |
| 8138 | mdname(mddev)); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8139 | return -EINVAL; |
| 8140 | } |
| 8141 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8142 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8143 | spin_lock_irq(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 8144 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8145 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8146 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 8147 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 8148 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8149 | conf->prev_algo = conf->algorithm; |
| 8150 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8151 | conf->generation++; |
| 8152 | /* Code that selects data_offset needs to see the generation update |
| 8153 | * if reshape_progress has been set - so a memory barrier needed. |
| 8154 | */ |
| 8155 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8156 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 8157 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 8158 | else |
| 8159 | conf->reshape_progress = 0; |
| 8160 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 8161 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8162 | spin_unlock_irq(&conf->device_lock); |
| 8163 | |
NeilBrown | 4d77e3b | 2013-08-27 15:57:47 +1000 | [diff] [blame] | 8164 | /* Now make sure any requests that proceeded on the assumption |
| 8165 | * the reshape wasn't running - like Discard or Read - have |
| 8166 | * completed. |
| 8167 | */ |
| 8168 | mddev_suspend(mddev); |
| 8169 | mddev_resume(mddev); |
| 8170 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8171 | /* Add some new drives, as many as will fit. |
| 8172 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 8173 | * Don't add devices if we are reducing the number of |
| 8174 | * devices in the array. This is because it is not possible |
| 8175 | * to correctly record the "partially reconstructed" state of |
| 8176 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8177 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8178 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 8179 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8180 | if (rdev->raid_disk < 0 && |
| 8181 | !test_bit(Faulty, &rdev->flags)) { |
| 8182 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8183 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 8184 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8185 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 8186 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8187 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 8188 | |
Damien Le Moal | 2aada5b | 2020-07-16 13:54:42 +0900 | [diff] [blame] | 8189 | /* Failure here is OK */ |
| 8190 | sysfs_link_rdev(mddev, rdev); |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8191 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8192 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 8193 | && !test_bit(Faulty, &rdev->flags)) { |
| 8194 | /* This is a spare that was manually added */ |
| 8195 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8196 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8197 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 8198 | /* When a reshape changes the number of devices, |
| 8199 | * ->degraded is measured against the larger of the |
| 8200 | * pre and post number of devices. |
| 8201 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8202 | spin_lock_irqsave(&conf->device_lock, flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 8203 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8204 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 8205 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8206 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 8207 | mddev->reshape_position = conf->reshape_progress; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8208 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8209 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8210 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 8211 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 8212 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8213 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 8214 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 8215 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 8216 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8217 | if (!mddev->sync_thread) { |
| 8218 | mddev->recovery = 0; |
| 8219 | spin_lock_irq(&conf->device_lock); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 8220 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8221 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 8222 | mddev->new_chunk_sectors = |
| 8223 | conf->chunk_sectors = conf->prev_chunk_sectors; |
| 8224 | mddev->new_layout = conf->algorithm = conf->prev_algo; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8225 | rdev_for_each(rdev, mddev) |
| 8226 | rdev->new_data_offset = rdev->data_offset; |
| 8227 | smp_wmb(); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 8228 | conf->generation --; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 8229 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 8230 | mddev->reshape_position = MaxSector; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 8231 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8232 | spin_unlock_irq(&conf->device_lock); |
| 8233 | return -EAGAIN; |
| 8234 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 8235 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8236 | md_wakeup_thread(mddev->sync_thread); |
| 8237 | md_new_event(mddev); |
| 8238 | return 0; |
| 8239 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8240 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8241 | /* This is called from the reshape thread and should make any |
| 8242 | * changes needed in 'conf' |
| 8243 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8244 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8245 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8246 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8247 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 8248 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8249 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8250 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8251 | conf->previous_raid_disks = conf->raid_disks; |
Xiao Ni | b5d2771 | 2017-07-05 17:34:04 +0800 | [diff] [blame] | 8252 | md_finish_reshape(conf->mddev); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8253 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 8254 | conf->reshape_progress = MaxSector; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 8255 | conf->mddev->reshape_position = MaxSector; |
NeilBrown | db0505d | 2017-10-17 16:18:36 +1100 | [diff] [blame] | 8256 | rdev_for_each(rdev, conf->mddev) |
| 8257 | if (rdev->raid_disk >= 0 && |
| 8258 | !test_bit(Journal, &rdev->flags) && |
| 8259 | !test_bit(In_sync, &rdev->flags)) |
| 8260 | rdev->recovery_offset = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 8261 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 8262 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8263 | |
Christoph Hellwig | c2e4cd5 | 2020-09-24 08:51:34 +0200 | [diff] [blame] | 8264 | if (conf->mddev->queue) |
Christoph Hellwig | 16ef510 | 2020-09-24 08:51:33 +0200 | [diff] [blame] | 8265 | raid5_set_io_opt(conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8266 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 8267 | } |
| 8268 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8269 | /* This is called from the raid5d thread with mddev_lock held. |
| 8270 | * It makes config changes to the device. |
| 8271 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8272 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8273 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8274 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8275 | |
| 8276 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 8277 | |
BingJing Chang | 8876391 | 2018-02-22 13:34:46 +0800 | [diff] [blame] | 8278 | if (mddev->delta_disks <= 0) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8279 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 8280 | spin_lock_irq(&conf->device_lock); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 8281 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 8282 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8283 | for (d = conf->raid_disks ; |
| 8284 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 8285 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 8286 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 8287 | if (rdev) |
| 8288 | clear_bit(In_sync, &rdev->flags); |
| 8289 | rdev = conf->disks[d].replacement; |
| 8290 | if (rdev) |
| 8291 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 8292 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8293 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8294 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 8295 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 8296 | mddev->reshape_position = MaxSector; |
| 8297 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 8298 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8299 | } |
| 8300 | } |
| 8301 | |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 8302 | static void raid5_quiesce(struct mddev *mddev, int quiesce) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8303 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8304 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8305 | |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 8306 | if (quiesce) { |
| 8307 | /* stop all writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8308 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8309 | /* '2' tells resync/reshape to pause so that all |
| 8310 | * active stripes can drain |
| 8311 | */ |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 8312 | r5c_flush_cache(conf, INT_MAX); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8313 | conf->quiesce = 2; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 8314 | wait_event_cmd(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 8315 | atomic_read(&conf->active_stripes) == 0 && |
| 8316 | atomic_read(&conf->active_aligned_reads) == 0, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8317 | unlock_all_device_hash_locks_irq(conf), |
| 8318 | lock_all_device_hash_locks_irq(conf)); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8319 | conf->quiesce = 1; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8320 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 8321 | /* allow reshape to continue */ |
| 8322 | wake_up(&conf->wait_for_overlap); |
NeilBrown | b03e0cc | 2017-10-19 12:49:15 +1100 | [diff] [blame] | 8323 | } else { |
| 8324 | /* re-enable writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8325 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8326 | conf->quiesce = 0; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 8327 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 8328 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 8329 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8330 | } |
Tomasz Majchrzak | 1532d9e | 2017-12-27 10:31:40 +0100 | [diff] [blame] | 8331 | log_quiesce(conf, quiesce); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8332 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 8333 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8334 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8335 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8336 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 8337 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8338 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8339 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8340 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 8341 | pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 8342 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8343 | return ERR_PTR(-EINVAL); |
| 8344 | } |
| 8345 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 8346 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 8347 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 8348 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8349 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 8350 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 8351 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 8352 | mddev->raid_disks += 1; |
| 8353 | mddev->delta_disks = 1; |
| 8354 | /* make sure it will be not marked as dirty */ |
| 8355 | mddev->recovery_cp = MaxSector; |
| 8356 | |
| 8357 | return setup_conf(mddev); |
| 8358 | } |
| 8359 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8360 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8361 | { |
| 8362 | int chunksect; |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8363 | void *ret; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8364 | |
| 8365 | if (mddev->raid_disks != 2 || |
| 8366 | mddev->degraded > 1) |
| 8367 | return ERR_PTR(-EINVAL); |
| 8368 | |
| 8369 | /* Should check if there are write-behind devices? */ |
| 8370 | |
| 8371 | chunksect = 64*2; /* 64K by default */ |
| 8372 | |
| 8373 | /* The array must be an exact multiple of chunksize */ |
| 8374 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 8375 | chunksect >>= 1; |
| 8376 | |
Yufen Yu | c911c46 | 2020-07-18 05:29:07 -0400 | [diff] [blame] | 8377 | if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private)) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8378 | /* array size does not allow a suitable chunk size */ |
| 8379 | return ERR_PTR(-EINVAL); |
| 8380 | |
| 8381 | mddev->new_level = 5; |
| 8382 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 8383 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8384 | |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8385 | ret = setup_conf(mddev); |
Jes Sorensen | 32cd7cb | 2017-01-06 19:31:35 -0500 | [diff] [blame] | 8386 | if (!IS_ERR(ret)) |
Shaohua Li | 394ed8e | 2017-01-04 16:10:19 -0800 | [diff] [blame] | 8387 | mddev_clear_unsupported_flags(mddev, |
| 8388 | UNSUPPORTED_MDDEV_FLAGS); |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 8389 | return ret; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8390 | } |
| 8391 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8392 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 8393 | { |
| 8394 | int new_layout; |
| 8395 | |
| 8396 | switch (mddev->layout) { |
| 8397 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 8398 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 8399 | break; |
| 8400 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 8401 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 8402 | break; |
| 8403 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 8404 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 8405 | break; |
| 8406 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 8407 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 8408 | break; |
| 8409 | case ALGORITHM_PARITY_0_6: |
| 8410 | new_layout = ALGORITHM_PARITY_0; |
| 8411 | break; |
| 8412 | case ALGORITHM_PARITY_N: |
| 8413 | new_layout = ALGORITHM_PARITY_N; |
| 8414 | break; |
| 8415 | default: |
| 8416 | return ERR_PTR(-EINVAL); |
| 8417 | } |
| 8418 | mddev->new_level = 5; |
| 8419 | mddev->new_layout = new_layout; |
| 8420 | mddev->delta_disks = -1; |
| 8421 | mddev->raid_disks -= 1; |
| 8422 | return setup_conf(mddev); |
| 8423 | } |
| 8424 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8425 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8426 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8427 | /* For a 2-drive array, the layout and chunk size can be changed |
| 8428 | * immediately as not restriping is needed. |
| 8429 | * For larger arrays we record the new value - after validation |
| 8430 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8431 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8432 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8433 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8434 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8435 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8436 | return -EINVAL; |
| 8437 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 8438 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8439 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8440 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8441 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8442 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8443 | /* not factor of array size */ |
| 8444 | return -EINVAL; |
| 8445 | } |
| 8446 | |
| 8447 | /* They look valid */ |
| 8448 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8449 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8450 | /* can make the change immediately */ |
| 8451 | if (mddev->new_layout >= 0) { |
| 8452 | conf->algorithm = mddev->new_layout; |
| 8453 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8454 | } |
| 8455 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8456 | conf->chunk_sectors = new_chunk ; |
| 8457 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8458 | } |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8459 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8460 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8461 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8462 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8463 | } |
| 8464 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8465 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8466 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8467 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8468 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8469 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8470 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8471 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 8472 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8473 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8474 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8475 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8476 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8477 | /* not factor of array size */ |
| 8478 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8479 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8480 | |
| 8481 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8482 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8483 | } |
| 8484 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8485 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8486 | { |
| 8487 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8488 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8489 | * raid1 - if there are two drives. We need to know the chunk size |
| 8490 | * raid4 - trivial - just use a raid4 layout. |
| 8491 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8492 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8493 | if (mddev->level == 0) |
| 8494 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8495 | if (mddev->level == 1) |
| 8496 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 8497 | if (mddev->level == 4) { |
| 8498 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 8499 | mddev->new_level = 5; |
| 8500 | return setup_conf(mddev); |
| 8501 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 8502 | if (mddev->level == 6) |
| 8503 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8504 | |
| 8505 | return ERR_PTR(-EINVAL); |
| 8506 | } |
| 8507 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8508 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8509 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8510 | /* raid4 can take over: |
| 8511 | * raid0 - if there is only one strip zone |
| 8512 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8513 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8514 | if (mddev->level == 0) |
| 8515 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8516 | if (mddev->level == 5 && |
| 8517 | mddev->layout == ALGORITHM_PARITY_N) { |
| 8518 | mddev->new_layout = 0; |
| 8519 | mddev->new_level = 4; |
| 8520 | return setup_conf(mddev); |
| 8521 | } |
| 8522 | return ERR_PTR(-EINVAL); |
| 8523 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8524 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8525 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8526 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8527 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8528 | { |
| 8529 | /* Currently can only take over a raid5. We map the |
| 8530 | * personality to an equivalent raid6 personality |
| 8531 | * with the Q block at the end. |
| 8532 | */ |
| 8533 | int new_layout; |
| 8534 | |
| 8535 | if (mddev->pers != &raid5_personality) |
| 8536 | return ERR_PTR(-EINVAL); |
| 8537 | if (mddev->degraded > 1) |
| 8538 | return ERR_PTR(-EINVAL); |
| 8539 | if (mddev->raid_disks > 253) |
| 8540 | return ERR_PTR(-EINVAL); |
| 8541 | if (mddev->raid_disks < 3) |
| 8542 | return ERR_PTR(-EINVAL); |
| 8543 | |
| 8544 | switch (mddev->layout) { |
| 8545 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 8546 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 8547 | break; |
| 8548 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 8549 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 8550 | break; |
| 8551 | case ALGORITHM_LEFT_SYMMETRIC: |
| 8552 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 8553 | break; |
| 8554 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 8555 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 8556 | break; |
| 8557 | case ALGORITHM_PARITY_0: |
| 8558 | new_layout = ALGORITHM_PARITY_0_6; |
| 8559 | break; |
| 8560 | case ALGORITHM_PARITY_N: |
| 8561 | new_layout = ALGORITHM_PARITY_N; |
| 8562 | break; |
| 8563 | default: |
| 8564 | return ERR_PTR(-EINVAL); |
| 8565 | } |
| 8566 | mddev->new_level = 6; |
| 8567 | mddev->new_layout = new_layout; |
| 8568 | mddev->delta_disks = 1; |
| 8569 | mddev->raid_disks += 1; |
| 8570 | return setup_conf(mddev); |
| 8571 | } |
| 8572 | |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 8573 | static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf) |
| 8574 | { |
| 8575 | struct r5conf *conf; |
| 8576 | int err; |
| 8577 | |
| 8578 | err = mddev_lock(mddev); |
| 8579 | if (err) |
| 8580 | return err; |
| 8581 | conf = mddev->private; |
| 8582 | if (!conf) { |
| 8583 | mddev_unlock(mddev); |
| 8584 | return -ENODEV; |
| 8585 | } |
| 8586 | |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 8587 | if (strncmp(buf, "ppl", 3) == 0) { |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8588 | /* ppl only works with RAID 5 */ |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 8589 | if (!raid5_has_ppl(conf) && conf->level == 5) { |
| 8590 | err = log_init(conf, NULL, true); |
| 8591 | if (!err) { |
| 8592 | err = resize_stripes(conf, conf->pool_size); |
| 8593 | if (err) |
| 8594 | log_exit(conf); |
| 8595 | } |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8596 | } else |
| 8597 | err = -EINVAL; |
| 8598 | } else if (strncmp(buf, "resync", 6) == 0) { |
| 8599 | if (raid5_has_ppl(conf)) { |
| 8600 | mddev_suspend(mddev); |
| 8601 | log_exit(conf); |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8602 | mddev_resume(mddev); |
Artur Paszkiewicz | 845b9e2 | 2017-04-04 13:13:57 +0200 | [diff] [blame] | 8603 | err = resize_stripes(conf, conf->pool_size); |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8604 | } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) && |
| 8605 | r5l_log_disk_error(conf)) { |
| 8606 | bool journal_dev_exists = false; |
| 8607 | struct md_rdev *rdev; |
| 8608 | |
| 8609 | rdev_for_each(rdev, mddev) |
| 8610 | if (test_bit(Journal, &rdev->flags)) { |
| 8611 | journal_dev_exists = true; |
| 8612 | break; |
| 8613 | } |
| 8614 | |
| 8615 | if (!journal_dev_exists) { |
| 8616 | mddev_suspend(mddev); |
| 8617 | clear_bit(MD_HAS_JOURNAL, &mddev->flags); |
| 8618 | mddev_resume(mddev); |
| 8619 | } else /* need remove journal device first */ |
| 8620 | err = -EBUSY; |
| 8621 | } else |
| 8622 | err = -EINVAL; |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 8623 | } else { |
| 8624 | err = -EINVAL; |
| 8625 | } |
| 8626 | |
| 8627 | if (!err) |
| 8628 | md_update_sb(mddev, 1); |
| 8629 | |
| 8630 | mddev_unlock(mddev); |
| 8631 | |
| 8632 | return err; |
| 8633 | } |
| 8634 | |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8635 | static int raid5_start(struct mddev *mddev) |
| 8636 | { |
| 8637 | struct r5conf *conf = mddev->private; |
| 8638 | |
| 8639 | return r5l_start(conf->log); |
| 8640 | } |
| 8641 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8642 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8643 | { |
| 8644 | .name = "raid6", |
| 8645 | .level = 6, |
| 8646 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8647 | .make_request = raid5_make_request, |
| 8648 | .run = raid5_run, |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8649 | .start = raid5_start, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8650 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8651 | .status = raid5_status, |
| 8652 | .error_handler = raid5_error, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8653 | .hot_add_disk = raid5_add_disk, |
| 8654 | .hot_remove_disk= raid5_remove_disk, |
| 8655 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8656 | .sync_request = raid5_sync_request, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8657 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8658 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8659 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 8660 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8661 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8662 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8663 | .takeover = raid6_takeover, |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8664 | .change_consistency_policy = raid5_change_consistency_policy, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8665 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8666 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8667 | { |
| 8668 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8669 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8670 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8671 | .make_request = raid5_make_request, |
| 8672 | .run = raid5_run, |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8673 | .start = raid5_start, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8674 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8675 | .status = raid5_status, |
| 8676 | .error_handler = raid5_error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8677 | .hot_add_disk = raid5_add_disk, |
| 8678 | .hot_remove_disk= raid5_remove_disk, |
| 8679 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8680 | .sync_request = raid5_sync_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8681 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8682 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8683 | .check_reshape = raid5_check_reshape, |
| 8684 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8685 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8686 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8687 | .takeover = raid5_takeover, |
Artur Paszkiewicz | ba903a3 | 2017-03-09 10:00:03 +0100 | [diff] [blame] | 8688 | .change_consistency_policy = raid5_change_consistency_policy, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8689 | }; |
| 8690 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8691 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8692 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8693 | .name = "raid4", |
| 8694 | .level = 4, |
| 8695 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8696 | .make_request = raid5_make_request, |
| 8697 | .run = raid5_run, |
Song Liu | d5d885f | 2017-11-19 22:17:01 -0800 | [diff] [blame] | 8698 | .start = raid5_start, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8699 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8700 | .status = raid5_status, |
| 8701 | .error_handler = raid5_error, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8702 | .hot_add_disk = raid5_add_disk, |
| 8703 | .hot_remove_disk= raid5_remove_disk, |
| 8704 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8705 | .sync_request = raid5_sync_request, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8706 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8707 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 8708 | .check_reshape = raid5_check_reshape, |
| 8709 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8710 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8711 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8712 | .takeover = raid4_takeover, |
Song Liu | 0bb0c10 | 2017-03-27 10:51:33 -0700 | [diff] [blame] | 8713 | .change_consistency_policy = raid5_change_consistency_policy, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8714 | }; |
| 8715 | |
| 8716 | static int __init raid5_init(void) |
| 8717 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8718 | int ret; |
| 8719 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8720 | raid5_wq = alloc_workqueue("raid5wq", |
| 8721 | WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); |
| 8722 | if (!raid5_wq) |
| 8723 | return -ENOMEM; |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8724 | |
| 8725 | ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE, |
| 8726 | "md/raid5:prepare", |
| 8727 | raid456_cpu_up_prepare, |
| 8728 | raid456_cpu_dead); |
| 8729 | if (ret) { |
| 8730 | destroy_workqueue(raid5_wq); |
| 8731 | return ret; |
| 8732 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8733 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8734 | register_md_personality(&raid5_personality); |
| 8735 | register_md_personality(&raid4_personality); |
| 8736 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8737 | } |
| 8738 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8739 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8740 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8741 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8742 | unregister_md_personality(&raid5_personality); |
| 8743 | unregister_md_personality(&raid4_personality); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8744 | cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8745 | destroy_workqueue(raid5_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8746 | } |
| 8747 | |
| 8748 | module_init(raid5_init); |
| 8749 | module_exit(raid5_exit); |
| 8750 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8751 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8752 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 8753 | MODULE_ALIAS("md-raid5"); |
| 8754 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8755 | MODULE_ALIAS("md-level-5"); |
| 8756 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8757 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 8758 | MODULE_ALIAS("md-raid6"); |
| 8759 | MODULE_ALIAS("md-level-6"); |
| 8760 | |
| 8761 | /* This used to be two separate modules, they were: */ |
| 8762 | MODULE_ALIAS("raid5"); |
| 8763 | MODULE_ALIAS("raid6"); |