Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * raid5.c : Multiple Devices driver for Linux |
| 3 | * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman |
| 4 | * Copyright (C) 1999, 2000 Ingo Molnar |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5 | * Copyright (C) 2002, 2003 H. Peter Anvin |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | * |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7 | * RAID-4/5/6 management functions. |
| 8 | * Thanks to Penguin Computing for making the RAID-6 development possible |
| 9 | * by donating a test server! |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * |
| 11 | * This program is free software; you can redistribute it and/or modify |
| 12 | * it under the terms of the GNU General Public License as published by |
| 13 | * the Free Software Foundation; either version 2, or (at your option) |
| 14 | * any later version. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * (for example /usr/src/linux/COPYING); if not, write to the Free |
| 18 | * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
| 19 | */ |
| 20 | |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 21 | /* |
| 22 | * BITMAP UNPLUGGING: |
| 23 | * |
| 24 | * The sequencing for updating the bitmap reliably is a little |
| 25 | * subtle (and I got it wrong the first time) so it deserves some |
| 26 | * explanation. |
| 27 | * |
| 28 | * We group bitmap updates into batches. Each batch has a number. |
| 29 | * We may write out several batches at once, but that isn't very important. |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 30 | * conf->seq_write is the number of the last batch successfully written. |
| 31 | * conf->seq_flush is the number of the last batch that was closed to |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 32 | * new additions. |
| 33 | * When we discover that we will need to write to any block in a stripe |
| 34 | * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 35 | * the number of the batch it will be in. This is seq_flush+1. |
NeilBrown | ae3c20c | 2006-07-10 04:44:17 -0700 | [diff] [blame] | 36 | * When we are ready to do a write, if that batch hasn't been written yet, |
| 37 | * we plug the array and queue the stripe for later. |
| 38 | * When an unplug happens, we increment bm_flush, thus closing the current |
| 39 | * batch. |
| 40 | * When we notice that bm_flush > bm_write, we write out all pending updates |
| 41 | * to the bitmap, and advance bm_write to where bm_flush was. |
| 42 | * This may occasionally write a bit out twice, but is sure never to |
| 43 | * miss any bits. |
| 44 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 46 | #include <linux/blkdev.h> |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 47 | #include <linux/kthread.h> |
Dan Williams | f701d58 | 2009-03-31 15:09:39 +1100 | [diff] [blame] | 48 | #include <linux/raid/pq.h> |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 49 | #include <linux/async_tx.h> |
Paul Gortmaker | 056075c | 2011-07-03 13:58:33 -0400 | [diff] [blame] | 50 | #include <linux/module.h> |
Dan Williams | 07a3b41 | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 51 | #include <linux/async.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 52 | #include <linux/seq_file.h> |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 53 | #include <linux/cpu.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 54 | #include <linux/slab.h> |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 55 | #include <linux/ratelimit.h> |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 56 | #include <linux/nodemask.h> |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 57 | #include <linux/flex_array.h> |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 58 | #include <trace/events/block.h> |
| 59 | |
NeilBrown | 43b2e5d | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 60 | #include "md.h" |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 61 | #include "raid5.h" |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 62 | #include "raid0.h" |
Christoph Hellwig | ef740c3 | 2009-03-31 14:27:03 +1100 | [diff] [blame] | 63 | #include "bitmap.h" |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 64 | |
Shaohua Li | 394ed8e | 2017-01-04 16:10:19 -0800 | [diff] [blame] | 65 | #define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED) |
| 66 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 67 | #define cpu_to_group(cpu) cpu_to_node(cpu) |
| 68 | #define ANY_GROUP NUMA_NO_NODE |
| 69 | |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 70 | static bool devices_handle_discard_safely = false; |
| 71 | module_param(devices_handle_discard_safely, bool, 0644); |
| 72 | MODULE_PARM_DESC(devices_handle_discard_safely, |
| 73 | "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] | 74 | static struct workqueue_struct *raid5_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 76 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 77 | { |
| 78 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; |
| 79 | return &conf->stripe_hashtbl[hash]; |
| 80 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 82 | static inline int stripe_hash_locks_hash(sector_t sect) |
| 83 | { |
| 84 | return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK; |
| 85 | } |
| 86 | |
| 87 | static inline void lock_device_hash_lock(struct r5conf *conf, int hash) |
| 88 | { |
| 89 | spin_lock_irq(conf->hash_locks + hash); |
| 90 | spin_lock(&conf->device_lock); |
| 91 | } |
| 92 | |
| 93 | static inline void unlock_device_hash_lock(struct r5conf *conf, int hash) |
| 94 | { |
| 95 | spin_unlock(&conf->device_lock); |
| 96 | spin_unlock_irq(conf->hash_locks + hash); |
| 97 | } |
| 98 | |
| 99 | static inline void lock_all_device_hash_locks_irq(struct r5conf *conf) |
| 100 | { |
| 101 | int i; |
| 102 | local_irq_disable(); |
| 103 | spin_lock(conf->hash_locks); |
| 104 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 105 | spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks); |
| 106 | spin_lock(&conf->device_lock); |
| 107 | } |
| 108 | |
| 109 | static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf) |
| 110 | { |
| 111 | int i; |
| 112 | spin_unlock(&conf->device_lock); |
| 113 | for (i = NR_STRIPE_HASH_LOCKS; i; i--) |
| 114 | spin_unlock(conf->hash_locks + i - 1); |
| 115 | local_irq_enable(); |
| 116 | } |
| 117 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 118 | /* Find first data disk in a raid6 stripe */ |
| 119 | static inline int raid6_d0(struct stripe_head *sh) |
| 120 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 121 | if (sh->ddf_layout) |
| 122 | /* ddf always start from first device */ |
| 123 | return 0; |
| 124 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 125 | if (sh->qd_idx == sh->disks - 1) |
| 126 | return 0; |
| 127 | else |
| 128 | return sh->qd_idx + 1; |
| 129 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 130 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 131 | { |
| 132 | disk++; |
| 133 | return (disk < raid_disks) ? disk : 0; |
| 134 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 135 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 136 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 137 | * We need to map each disk to a 'slot', where the data disks are slot |
| 138 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 139 | * is raid_disks-1. This help does that mapping. |
| 140 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 141 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 142 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 143 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 144 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 145 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 146 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 147 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 148 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 149 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 150 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 151 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 152 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 153 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 154 | return slot; |
| 155 | } |
| 156 | |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 157 | static void return_io(struct bio_list *return_bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 158 | { |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 159 | struct bio *bi; |
| 160 | while ((bi = bio_list_pop(return_bi)) != NULL) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 161 | bi->bi_iter.bi_size = 0; |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 162 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 163 | bi, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 164 | bio_endio(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 165 | } |
| 166 | } |
| 167 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 168 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 170 | static int stripe_operations_active(struct stripe_head *sh) |
| 171 | { |
| 172 | return sh->check_state || sh->reconstruct_state || |
| 173 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 174 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 175 | } |
| 176 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 177 | static void raid5_wakeup_stripe_thread(struct stripe_head *sh) |
| 178 | { |
| 179 | struct r5conf *conf = sh->raid_conf; |
| 180 | struct r5worker_group *group; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 181 | int thread_cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 182 | int i, cpu = sh->cpu; |
| 183 | |
| 184 | if (!cpu_online(cpu)) { |
| 185 | cpu = cpumask_any(cpu_online_mask); |
| 186 | sh->cpu = cpu; |
| 187 | } |
| 188 | |
| 189 | if (list_empty(&sh->lru)) { |
| 190 | struct r5worker_group *group; |
| 191 | group = conf->worker_groups + cpu_to_group(cpu); |
| 192 | list_add_tail(&sh->lru, &group->handle_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 193 | group->stripes_cnt++; |
| 194 | sh->group = group; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | if (conf->worker_cnt_per_group == 0) { |
| 198 | md_wakeup_thread(conf->mddev->thread); |
| 199 | return; |
| 200 | } |
| 201 | |
| 202 | group = conf->worker_groups + cpu_to_group(sh->cpu); |
| 203 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 204 | group->workers[0].working = true; |
| 205 | /* at least one worker should run to avoid race */ |
| 206 | queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work); |
| 207 | |
| 208 | thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1; |
| 209 | /* wakeup more workers */ |
| 210 | for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) { |
| 211 | if (group->workers[i].working == false) { |
| 212 | group->workers[i].working = true; |
| 213 | queue_work_on(sh->cpu, raid5_wq, |
| 214 | &group->workers[i].work); |
| 215 | thread_cnt--; |
| 216 | } |
| 217 | } |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 218 | } |
| 219 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 220 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 221 | struct list_head *temp_inactive_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 223 | int i; |
| 224 | int injournal = 0; /* number of date pages with R5_InJournal */ |
| 225 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 226 | BUG_ON(!list_empty(&sh->lru)); |
| 227 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 228 | |
| 229 | if (r5c_is_writeback(conf->log)) |
| 230 | for (i = sh->disks; i--; ) |
| 231 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 232 | injournal++; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 233 | /* |
| 234 | * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with |
| 235 | * data in journal, so they are not released to cached lists |
| 236 | */ |
| 237 | if (conf->quiesce && r5c_is_writeback(conf->log) && |
| 238 | !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) { |
| 239 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
| 240 | r5c_make_stripe_write_out(sh); |
| 241 | set_bit(STRIPE_HANDLE, &sh->state); |
| 242 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 243 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 244 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 245 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
Jes Sorensen | ad3ab8b | 2015-01-29 12:38:29 -0500 | [diff] [blame] | 246 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 247 | list_add_tail(&sh->lru, &conf->delayed_list); |
Jes Sorensen | ad3ab8b | 2015-01-29 12:38:29 -0500 | [diff] [blame] | 248 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 249 | sh->bm_seq - conf->seq_write > 0) |
| 250 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 251 | else { |
| 252 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 253 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 254 | if (conf->worker_cnt_per_group == 0) { |
| 255 | list_add_tail(&sh->lru, &conf->handle_list); |
| 256 | } else { |
| 257 | raid5_wakeup_stripe_thread(sh); |
| 258 | return; |
| 259 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 260 | } |
| 261 | md_wakeup_thread(conf->mddev->thread); |
| 262 | } else { |
| 263 | BUG_ON(stripe_operations_active(sh)); |
| 264 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 265 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 266 | < IO_THRESHOLD) |
| 267 | md_wakeup_thread(conf->mddev->thread); |
| 268 | atomic_dec(&conf->active_stripes); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 269 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 270 | if (!r5c_is_writeback(conf->log)) |
| 271 | list_add_tail(&sh->lru, temp_inactive_list); |
| 272 | else { |
| 273 | WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)); |
| 274 | if (injournal == 0) |
| 275 | list_add_tail(&sh->lru, temp_inactive_list); |
| 276 | else if (injournal == conf->raid_disks - conf->max_degraded) { |
| 277 | /* full stripe */ |
| 278 | if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) |
| 279 | atomic_inc(&conf->r5c_cached_full_stripes); |
| 280 | if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) |
| 281 | atomic_dec(&conf->r5c_cached_partial_stripes); |
| 282 | list_add_tail(&sh->lru, &conf->r5c_full_stripe_list); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 283 | r5c_check_cached_full_stripe(conf); |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 284 | } else |
| 285 | /* |
| 286 | * STRIPE_R5C_PARTIAL_STRIPE is set in |
| 287 | * r5c_try_caching_write(). No need to |
| 288 | * set it again. |
| 289 | */ |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 290 | list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 291 | } |
| 292 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 293 | } |
| 294 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 295 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 296 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 297 | struct list_head *temp_inactive_list) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 298 | { |
| 299 | if (atomic_dec_and_test(&sh->count)) |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 300 | do_release_stripe(conf, sh, temp_inactive_list); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list |
| 305 | * |
| 306 | * Be careful: Only one task can add/delete stripes from temp_inactive_list at |
| 307 | * given time. Adding stripes only takes device lock, while deleting stripes |
| 308 | * only takes hash lock. |
| 309 | */ |
| 310 | static void release_inactive_stripe_list(struct r5conf *conf, |
| 311 | struct list_head *temp_inactive_list, |
| 312 | int hash) |
| 313 | { |
| 314 | int size; |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 315 | bool do_wakeup = false; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 316 | unsigned long flags; |
| 317 | |
| 318 | if (hash == NR_STRIPE_HASH_LOCKS) { |
| 319 | size = NR_STRIPE_HASH_LOCKS; |
| 320 | hash = NR_STRIPE_HASH_LOCKS - 1; |
| 321 | } else |
| 322 | size = 1; |
| 323 | while (size) { |
| 324 | struct list_head *list = &temp_inactive_list[size - 1]; |
| 325 | |
| 326 | /* |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 327 | * 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] | 328 | * remove stripes from the list |
| 329 | */ |
| 330 | if (!list_empty_careful(list)) { |
| 331 | spin_lock_irqsave(conf->hash_locks + hash, flags); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 332 | if (list_empty(conf->inactive_list + hash) && |
| 333 | !list_empty(list)) |
| 334 | atomic_dec(&conf->empty_inactive_list_nr); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 335 | list_splice_tail_init(list, conf->inactive_list + hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 336 | do_wakeup = true; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 337 | spin_unlock_irqrestore(conf->hash_locks + hash, flags); |
| 338 | } |
| 339 | size--; |
| 340 | hash--; |
| 341 | } |
| 342 | |
| 343 | if (do_wakeup) { |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 344 | wake_up(&conf->wait_for_stripe); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 345 | if (atomic_read(&conf->active_stripes) == 0) |
| 346 | wake_up(&conf->wait_for_quiescent); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 347 | if (conf->retry_read_aligned) |
| 348 | md_wakeup_thread(conf->mddev->thread); |
| 349 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 350 | } |
| 351 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 352 | /* should hold conf->device_lock already */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 353 | static int release_stripe_list(struct r5conf *conf, |
| 354 | struct list_head *temp_inactive_list) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 355 | { |
Byungchul Park | eae8263 | 2017-02-14 16:26:24 +0900 | [diff] [blame] | 356 | struct stripe_head *sh, *t; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 357 | int count = 0; |
| 358 | struct llist_node *head; |
| 359 | |
| 360 | head = llist_del_all(&conf->released_stripes); |
Shaohua Li | d265d9d | 2013-08-28 14:29:05 +0800 | [diff] [blame] | 361 | head = llist_reverse_order(head); |
Byungchul Park | eae8263 | 2017-02-14 16:26:24 +0900 | [diff] [blame] | 362 | llist_for_each_entry_safe(sh, t, head, release_list) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 363 | int hash; |
| 364 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 365 | /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */ |
| 366 | smp_mb(); |
| 367 | clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state); |
| 368 | /* |
| 369 | * Don't worry the bit is set here, because if the bit is set |
| 370 | * again, the count is always > 1. This is true for |
| 371 | * STRIPE_ON_UNPLUG_LIST bit too. |
| 372 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 373 | hash = sh->hash_lock_index; |
| 374 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 375 | count++; |
| 376 | } |
| 377 | |
| 378 | return count; |
| 379 | } |
| 380 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 381 | void raid5_release_stripe(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 382 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 383 | struct r5conf *conf = sh->raid_conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | unsigned long flags; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 385 | struct list_head list; |
| 386 | int hash; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 387 | bool wakeup; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 388 | |
Eivind Sarto | cf170f3 | 2014-05-28 13:39:23 +1000 | [diff] [blame] | 389 | /* Avoid release_list until the last reference. |
| 390 | */ |
| 391 | if (atomic_add_unless(&sh->count, -1, 1)) |
| 392 | return; |
| 393 | |
majianpeng | ad4068d | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 394 | if (unlikely(!conf->mddev->thread) || |
| 395 | test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state)) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 396 | goto slow_path; |
| 397 | wakeup = llist_add(&sh->release_list, &conf->released_stripes); |
| 398 | if (wakeup) |
| 399 | md_wakeup_thread(conf->mddev->thread); |
| 400 | return; |
| 401 | slow_path: |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 402 | local_irq_save(flags); |
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 */ |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 404 | if (atomic_dec_and_lock(&sh->count, &conf->device_lock)) { |
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); |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 408 | spin_unlock(&conf->device_lock); |
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 | } |
| 411 | local_irq_restore(flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 412 | } |
| 413 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 414 | static inline void remove_hash(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 416 | pr_debug("remove_hash(), stripe %llu\n", |
| 417 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 418 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 419 | hlist_del_init(&sh->hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 420 | } |
| 421 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 422 | static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 423 | { |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 424 | struct hlist_head *hp = stripe_hash(conf, sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 426 | pr_debug("insert_hash(), stripe %llu\n", |
| 427 | (unsigned long long)sh->sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 428 | |
NeilBrown | fccddba | 2006-01-06 00:20:33 -0800 | [diff] [blame] | 429 | hlist_add_head(&sh->hash, hp); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 430 | } |
| 431 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 432 | /* find an idle stripe, make sure it is unhashed, and return it. */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 433 | static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 434 | { |
| 435 | struct stripe_head *sh = NULL; |
| 436 | struct list_head *first; |
| 437 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 438 | if (list_empty(conf->inactive_list + hash)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 439 | goto out; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 440 | first = (conf->inactive_list + hash)->next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | sh = list_entry(first, struct stripe_head, lru); |
| 442 | list_del_init(first); |
| 443 | remove_hash(sh); |
| 444 | atomic_inc(&conf->active_stripes); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 445 | BUG_ON(hash != sh->hash_lock_index); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 446 | if (list_empty(conf->inactive_list + hash)) |
| 447 | atomic_inc(&conf->empty_inactive_list_nr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | out: |
| 449 | return sh; |
| 450 | } |
| 451 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 452 | static void shrink_buffers(struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 453 | { |
| 454 | struct page *p; |
| 455 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 456 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 458 | for (i = 0; i < num ; i++) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 459 | WARN_ON(sh->dev[i].page != sh->dev[i].orig_page); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | p = sh->dev[i].page; |
| 461 | if (!p) |
| 462 | continue; |
| 463 | sh->dev[i].page = NULL; |
NeilBrown | 2d1f3b5 | 2006-01-06 00:20:31 -0800 | [diff] [blame] | 464 | put_page(p); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | } |
| 466 | } |
| 467 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 468 | static int grow_buffers(struct stripe_head *sh, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | { |
| 470 | int i; |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 471 | int num = sh->raid_conf->pool_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 472 | |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 473 | for (i = 0; i < num; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 474 | struct page *page; |
| 475 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 476 | if (!(page = alloc_page(gfp))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | return 1; |
| 478 | } |
| 479 | sh->dev[i].page = page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 480 | sh->dev[i].orig_page = page; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 481 | } |
| 482 | return 0; |
| 483 | } |
| 484 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 485 | static void raid5_build_block(struct stripe_head *sh, int i, int previous); |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 486 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 487 | struct stripe_head *sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 489 | 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] | 490 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 491 | struct r5conf *conf = sh->raid_conf; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 492 | int i, seq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 493 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 494 | BUG_ON(atomic_read(&sh->count) != 0); |
| 495 | BUG_ON(test_bit(STRIPE_HANDLE, &sh->state)); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 496 | BUG_ON(stripe_operations_active(sh)); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 497 | BUG_ON(sh->batch_head); |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 498 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 499 | pr_debug("init_stripe called, stripe %llu\n", |
Markus Stockhausen | b8e6a15 | 2014-08-23 20:19:27 +1000 | [diff] [blame] | 500 | (unsigned long long)sector); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 501 | retry: |
| 502 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 503 | sh->generation = conf->generation - previous; |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 504 | sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 505 | sh->sector = sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 506 | stripe_set_idx(sector, conf, previous, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 507 | sh->state = 0; |
| 508 | |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 509 | for (i = sh->disks; i--; ) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 510 | struct r5dev *dev = &sh->dev[i]; |
| 511 | |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 512 | if (dev->toread || dev->read || dev->towrite || dev->written || |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 513 | test_bit(R5_LOCKED, &dev->flags)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 514 | pr_err("sector=%llx i=%d %p %p %p %p %d\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 515 | (unsigned long long)sh->sector, i, dev->toread, |
Dan Williams | d84e0f1 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 516 | dev->read, dev->towrite, dev->written, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 517 | test_bit(R5_LOCKED, &dev->flags)); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 518 | WARN_ON(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 519 | } |
| 520 | dev->flags = 0; |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 521 | raid5_build_block(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 522 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 523 | if (read_seqcount_retry(&conf->gen_lock, seq)) |
| 524 | goto retry; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 525 | sh->overwrite_disks = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 526 | insert_hash(conf, sh); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 527 | sh->cpu = smp_processor_id(); |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 528 | set_bit(STRIPE_BATCH_READY, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 529 | } |
| 530 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 531 | static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector, |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 532 | short generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 533 | { |
| 534 | struct stripe_head *sh; |
| 535 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 536 | pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector); |
Sasha Levin | b67bfe0 | 2013-02-27 17:06:00 -0800 | [diff] [blame] | 537 | hlist_for_each_entry(sh, stripe_hash(conf, sector), hash) |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 538 | if (sh->sector == sector && sh->generation == generation) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 539 | return sh; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 540 | pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 541 | return NULL; |
| 542 | } |
| 543 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 544 | /* |
| 545 | * Need to check if array has failed when deciding whether to: |
| 546 | * - start an array |
| 547 | * - remove non-faulty devices |
| 548 | * - add a spare |
| 549 | * - allow a reshape |
| 550 | * This determination is simple when no reshape is happening. |
| 551 | * However if there is a reshape, we need to carefully check |
| 552 | * both the before and after sections. |
| 553 | * This is because some failed devices may only affect one |
| 554 | * of the two sections, and some non-in_sync devices may |
| 555 | * be insync in the section most affected by failed devices. |
| 556 | */ |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 557 | int raid5_calc_degraded(struct r5conf *conf) |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 558 | { |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 559 | int degraded, degraded2; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 560 | int i; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 561 | |
| 562 | rcu_read_lock(); |
| 563 | degraded = 0; |
| 564 | for (i = 0; i < conf->previous_raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 565 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 566 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 567 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 568 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
| 569 | degraded++; |
| 570 | else if (test_bit(In_sync, &rdev->flags)) |
| 571 | ; |
| 572 | else |
| 573 | /* not in-sync or faulty. |
| 574 | * If the reshape increases the number of devices, |
| 575 | * this is being recovered by the reshape, so |
| 576 | * this 'previous' section is not in_sync. |
| 577 | * If the number of devices is being reduced however, |
| 578 | * the device can only be part of the array if |
| 579 | * we are reverting a reshape, so this section will |
| 580 | * be in-sync. |
| 581 | */ |
| 582 | if (conf->raid_disks >= conf->previous_raid_disks) |
| 583 | degraded++; |
| 584 | } |
| 585 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 586 | if (conf->raid_disks == conf->previous_raid_disks) |
| 587 | return degraded; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 588 | rcu_read_lock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 589 | degraded2 = 0; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 590 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 591 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | e5c8647 | 2012-09-19 12:52:30 +1000 | [diff] [blame] | 592 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 593 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 594 | if (!rdev || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 595 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 596 | else if (test_bit(In_sync, &rdev->flags)) |
| 597 | ; |
| 598 | else |
| 599 | /* not in-sync or faulty. |
| 600 | * If reshape increases the number of devices, this |
| 601 | * section has already been recovered, else it |
| 602 | * almost certainly hasn't. |
| 603 | */ |
| 604 | if (conf->raid_disks <= conf->previous_raid_disks) |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 605 | degraded2++; |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 606 | } |
| 607 | rcu_read_unlock(); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 608 | if (degraded2 > degraded) |
| 609 | return degraded2; |
| 610 | return degraded; |
| 611 | } |
| 612 | |
| 613 | static int has_failed(struct r5conf *conf) |
| 614 | { |
| 615 | int degraded; |
| 616 | |
| 617 | if (conf->mddev->reshape_position == MaxSector) |
| 618 | return conf->mddev->degraded > conf->max_degraded; |
| 619 | |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 620 | degraded = raid5_calc_degraded(conf); |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 621 | if (degraded > conf->max_degraded) |
| 622 | return 1; |
| 623 | return 0; |
| 624 | } |
| 625 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 626 | struct stripe_head * |
| 627 | raid5_get_active_stripe(struct r5conf *conf, sector_t sector, |
| 628 | int previous, int noblock, int noquiesce) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 629 | { |
| 630 | struct stripe_head *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 631 | int hash = stripe_hash_locks_hash(sector); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 632 | int inc_empty_inactive_list_flag; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 633 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 634 | pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 635 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 636 | spin_lock_irq(conf->hash_locks + hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 637 | |
| 638 | do { |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 639 | wait_event_lock_irq(conf->wait_for_quiescent, |
NeilBrown | a8c906c | 2009-06-09 14:39:59 +1000 | [diff] [blame] | 640 | conf->quiesce == 0 || noquiesce, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 641 | *(conf->hash_locks + hash)); |
NeilBrown | 86b42c7 | 2009-03-31 15:19:03 +1100 | [diff] [blame] | 642 | sh = __find_stripe(conf, sector, conf->generation - previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 643 | if (!sh) { |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 644 | if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 645 | sh = get_free_stripe(conf, hash); |
Shaohua Li | 713bc5c | 2015-05-28 17:33:47 -0700 | [diff] [blame] | 646 | if (!sh && !test_bit(R5_DID_ALLOC, |
| 647 | &conf->cache_state)) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 648 | set_bit(R5_ALLOC_MORE, |
| 649 | &conf->cache_state); |
| 650 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 651 | if (noblock && sh == NULL) |
| 652 | break; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 653 | |
| 654 | r5c_check_stripe_cache_usage(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 655 | if (!sh) { |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 656 | set_bit(R5_INACTIVE_BLOCKED, |
| 657 | &conf->cache_state); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 658 | r5l_wake_reclaim(conf->log, 0); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 659 | wait_event_lock_irq( |
| 660 | conf->wait_for_stripe, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 661 | !list_empty(conf->inactive_list + hash) && |
| 662 | (atomic_read(&conf->active_stripes) |
| 663 | < (conf->max_nr_stripes * 3 / 4) |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 664 | || !test_bit(R5_INACTIVE_BLOCKED, |
| 665 | &conf->cache_state)), |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 666 | *(conf->hash_locks + hash)); |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 667 | clear_bit(R5_INACTIVE_BLOCKED, |
| 668 | &conf->cache_state); |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 669 | } else { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 670 | init_stripe(sh, sector, previous); |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 671 | atomic_inc(&sh->count); |
| 672 | } |
Shaohua Li | e240c18 | 2014-04-09 11:27:42 +0800 | [diff] [blame] | 673 | } else if (!atomic_inc_not_zero(&sh->count)) { |
NeilBrown | 6d183de | 2013-11-28 10:55:27 +1100 | [diff] [blame] | 674 | spin_lock(&conf->device_lock); |
Shaohua Li | e240c18 | 2014-04-09 11:27:42 +0800 | [diff] [blame] | 675 | if (!atomic_read(&sh->count)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 676 | if (!test_bit(STRIPE_HANDLE, &sh->state)) |
| 677 | atomic_inc(&conf->active_stripes); |
NeilBrown | 5af9bef | 2014-01-14 15:16:10 +1100 | [diff] [blame] | 678 | BUG_ON(list_empty(&sh->lru) && |
| 679 | !test_bit(STRIPE_EXPANDING, &sh->state)); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 680 | inc_empty_inactive_list_flag = 0; |
| 681 | if (!list_empty(conf->inactive_list + hash)) |
| 682 | inc_empty_inactive_list_flag = 1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 683 | list_del_init(&sh->lru); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 684 | if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) |
| 685 | atomic_inc(&conf->empty_inactive_list_nr); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 686 | if (sh->group) { |
| 687 | sh->group->stripes_cnt--; |
| 688 | sh->group = NULL; |
| 689 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 690 | } |
NeilBrown | 7da9d45 | 2014-01-22 11:45:03 +1100 | [diff] [blame] | 691 | atomic_inc(&sh->count); |
NeilBrown | 6d183de | 2013-11-28 10:55:27 +1100 | [diff] [blame] | 692 | spin_unlock(&conf->device_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 693 | } |
| 694 | } while (sh == NULL); |
| 695 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 696 | spin_unlock_irq(conf->hash_locks + hash); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 697 | return sh; |
| 698 | } |
| 699 | |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 700 | static bool is_full_stripe_write(struct stripe_head *sh) |
| 701 | { |
| 702 | BUG_ON(sh->overwrite_disks > (sh->disks - sh->raid_conf->max_degraded)); |
| 703 | return sh->overwrite_disks == (sh->disks - sh->raid_conf->max_degraded); |
| 704 | } |
| 705 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 706 | static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) |
| 707 | { |
| 708 | local_irq_disable(); |
| 709 | if (sh1 > sh2) { |
| 710 | spin_lock(&sh2->stripe_lock); |
| 711 | spin_lock_nested(&sh1->stripe_lock, 1); |
| 712 | } else { |
| 713 | spin_lock(&sh1->stripe_lock); |
| 714 | spin_lock_nested(&sh2->stripe_lock, 1); |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2) |
| 719 | { |
| 720 | spin_unlock(&sh1->stripe_lock); |
| 721 | spin_unlock(&sh2->stripe_lock); |
| 722 | local_irq_enable(); |
| 723 | } |
| 724 | |
| 725 | /* Only freshly new full stripe normal write stripe can be added to a batch list */ |
| 726 | static bool stripe_can_batch(struct stripe_head *sh) |
| 727 | { |
Shaohua Li | 9c3e333 | 2015-08-13 14:32:02 -0700 | [diff] [blame] | 728 | struct r5conf *conf = sh->raid_conf; |
| 729 | |
| 730 | if (conf->log) |
| 731 | return false; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 732 | return test_bit(STRIPE_BATCH_READY, &sh->state) && |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 733 | !test_bit(STRIPE_BITMAP_PENDING, &sh->state) && |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 734 | is_full_stripe_write(sh); |
| 735 | } |
| 736 | |
| 737 | /* we only do back search */ |
| 738 | static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh) |
| 739 | { |
| 740 | struct stripe_head *head; |
| 741 | sector_t head_sector, tmp_sec; |
| 742 | int hash; |
| 743 | int dd_idx; |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 744 | int inc_empty_inactive_list_flag; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 745 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 746 | /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */ |
| 747 | tmp_sec = sh->sector; |
| 748 | if (!sector_div(tmp_sec, conf->chunk_sectors)) |
| 749 | return; |
| 750 | head_sector = sh->sector - STRIPE_SECTORS; |
| 751 | |
| 752 | hash = stripe_hash_locks_hash(head_sector); |
| 753 | spin_lock_irq(conf->hash_locks + hash); |
| 754 | head = __find_stripe(conf, head_sector, conf->generation); |
| 755 | if (head && !atomic_inc_not_zero(&head->count)) { |
| 756 | spin_lock(&conf->device_lock); |
| 757 | if (!atomic_read(&head->count)) { |
| 758 | if (!test_bit(STRIPE_HANDLE, &head->state)) |
| 759 | atomic_inc(&conf->active_stripes); |
| 760 | BUG_ON(list_empty(&head->lru) && |
| 761 | !test_bit(STRIPE_EXPANDING, &head->state)); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 762 | inc_empty_inactive_list_flag = 0; |
| 763 | if (!list_empty(conf->inactive_list + hash)) |
| 764 | inc_empty_inactive_list_flag = 1; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 765 | list_del_init(&head->lru); |
ZhengYuan Liu | ff00d3b | 2016-07-28 14:22:14 +0800 | [diff] [blame] | 766 | if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag) |
| 767 | atomic_inc(&conf->empty_inactive_list_nr); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 768 | if (head->group) { |
| 769 | head->group->stripes_cnt--; |
| 770 | head->group = NULL; |
| 771 | } |
| 772 | } |
| 773 | atomic_inc(&head->count); |
| 774 | spin_unlock(&conf->device_lock); |
| 775 | } |
| 776 | spin_unlock_irq(conf->hash_locks + hash); |
| 777 | |
| 778 | if (!head) |
| 779 | return; |
| 780 | if (!stripe_can_batch(head)) |
| 781 | goto out; |
| 782 | |
| 783 | lock_two_stripes(head, sh); |
| 784 | /* clear_batch_ready clear the flag */ |
| 785 | if (!stripe_can_batch(head) || !stripe_can_batch(sh)) |
| 786 | goto unlock_out; |
| 787 | |
| 788 | if (sh->batch_head) |
| 789 | goto unlock_out; |
| 790 | |
| 791 | dd_idx = 0; |
| 792 | while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx) |
| 793 | dd_idx++; |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 794 | 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] | 795 | 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] | 796 | goto unlock_out; |
| 797 | |
| 798 | if (head->batch_head) { |
| 799 | spin_lock(&head->batch_head->batch_lock); |
| 800 | /* This batch list is already running */ |
| 801 | if (!stripe_can_batch(head)) { |
| 802 | spin_unlock(&head->batch_head->batch_lock); |
| 803 | goto unlock_out; |
| 804 | } |
| 805 | |
| 806 | /* |
| 807 | * at this point, head's BATCH_READY could be cleared, but we |
| 808 | * can still add the stripe to batch list |
| 809 | */ |
| 810 | list_add(&sh->batch_list, &head->batch_list); |
| 811 | spin_unlock(&head->batch_head->batch_lock); |
| 812 | |
| 813 | sh->batch_head = head->batch_head; |
| 814 | } else { |
| 815 | head->batch_head = head; |
| 816 | sh->batch_head = head->batch_head; |
| 817 | spin_lock(&head->batch_lock); |
| 818 | list_add_tail(&sh->batch_list, &head->batch_list); |
| 819 | spin_unlock(&head->batch_lock); |
| 820 | } |
| 821 | |
| 822 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 823 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 824 | < IO_THRESHOLD) |
| 825 | md_wakeup_thread(conf->mddev->thread); |
| 826 | |
NeilBrown | 2b6b245 | 2015-05-21 15:10:01 +1000 | [diff] [blame] | 827 | if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) { |
| 828 | int seq = sh->bm_seq; |
| 829 | if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) && |
| 830 | sh->batch_head->bm_seq > seq) |
| 831 | seq = sh->batch_head->bm_seq; |
| 832 | set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state); |
| 833 | sh->batch_head->bm_seq = seq; |
| 834 | } |
| 835 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 836 | atomic_inc(&sh->count); |
| 837 | unlock_out: |
| 838 | unlock_two_stripes(head, sh); |
| 839 | out: |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 840 | raid5_release_stripe(head); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 841 | } |
| 842 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 843 | /* Determine if 'data_offset' or 'new_data_offset' should be used |
| 844 | * in this stripe_head. |
| 845 | */ |
| 846 | static int use_new_offset(struct r5conf *conf, struct stripe_head *sh) |
| 847 | { |
| 848 | sector_t progress = conf->reshape_progress; |
| 849 | /* Need a memory barrier to make sure we see the value |
| 850 | * of conf->generation, or ->data_offset that was set before |
| 851 | * reshape_progress was updated. |
| 852 | */ |
| 853 | smp_rmb(); |
| 854 | if (progress == MaxSector) |
| 855 | return 0; |
| 856 | if (sh->generation == conf->generation - 1) |
| 857 | return 0; |
| 858 | /* We are in a reshape, and this is a new-generation stripe, |
| 859 | * so use new_data_offset. |
| 860 | */ |
| 861 | return 1; |
| 862 | } |
| 863 | |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 864 | static void flush_deferred_bios(struct r5conf *conf) |
| 865 | { |
| 866 | struct bio_list tmp; |
| 867 | struct bio *bio; |
| 868 | |
| 869 | if (!conf->batch_bio_dispatch || !conf->group_cnt) |
| 870 | return; |
| 871 | |
| 872 | bio_list_init(&tmp); |
| 873 | spin_lock(&conf->pending_bios_lock); |
| 874 | bio_list_merge(&tmp, &conf->pending_bios); |
| 875 | bio_list_init(&conf->pending_bios); |
| 876 | spin_unlock(&conf->pending_bios_lock); |
| 877 | |
| 878 | while ((bio = bio_list_pop(&tmp))) |
| 879 | generic_make_request(bio); |
| 880 | } |
| 881 | |
| 882 | static void defer_bio_issue(struct r5conf *conf, struct bio *bio) |
| 883 | { |
| 884 | /* |
| 885 | * change group_cnt will drain all bios, so this is safe |
| 886 | * |
| 887 | * A read generally means a read-modify-write, which usually means a |
| 888 | * randwrite, so we don't delay it |
| 889 | */ |
| 890 | if (!conf->batch_bio_dispatch || !conf->group_cnt || |
| 891 | bio_op(bio) == REQ_OP_READ) { |
| 892 | generic_make_request(bio); |
| 893 | return; |
| 894 | } |
| 895 | spin_lock(&conf->pending_bios_lock); |
| 896 | bio_list_add(&conf->pending_bios, bio); |
| 897 | spin_unlock(&conf->pending_bios_lock); |
| 898 | md_wakeup_thread(conf->mddev->thread); |
| 899 | } |
| 900 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 901 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 902 | raid5_end_read_request(struct bio *bi); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 903 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 904 | raid5_end_write_request(struct bio *bi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 905 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 906 | 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] | 907 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 908 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 909 | int i, disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 910 | struct stripe_head *head_sh = sh; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 911 | |
| 912 | might_sleep(); |
| 913 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 914 | if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) { |
| 915 | /* writing out phase */ |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 916 | if (s->waiting_extra_page) |
| 917 | return; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 918 | if (r5l_write_stripe(conf->log, sh) == 0) |
| 919 | return; |
| 920 | } else { /* caching phase */ |
| 921 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) { |
| 922 | r5c_cache_data(conf->log, sh, s); |
| 923 | return; |
| 924 | } |
| 925 | } |
| 926 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 927 | for (i = disks; i--; ) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 928 | int op, op_flags = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 929 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 930 | struct bio *bi, *rbi; |
| 931 | struct md_rdev *rdev, *rrdev = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 932 | |
| 933 | sh = head_sh; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 934 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 935 | op = REQ_OP_WRITE; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 936 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
Christoph Hellwig | 70fd761 | 2016-11-01 07:40:10 -0600 | [diff] [blame] | 937 | op_flags = REQ_FUA; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 938 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 939 | op = REQ_OP_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 940 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 941 | op = REQ_OP_READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 942 | else if (test_and_clear_bit(R5_WantReplace, |
| 943 | &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 944 | op = REQ_OP_WRITE; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 945 | replace_only = 1; |
| 946 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 947 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 948 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 949 | op_flags |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 950 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 951 | again: |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 952 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 953 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 954 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 955 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 956 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 957 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 958 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 959 | if (!rdev) { |
| 960 | rdev = rrdev; |
| 961 | rrdev = NULL; |
| 962 | } |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 963 | if (op_is_write(op)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 964 | if (replace_only) |
| 965 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 966 | if (rdev == rrdev) |
| 967 | /* We raced and saw duplicates */ |
| 968 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 969 | } else { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 970 | if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 971 | rdev = rrdev; |
| 972 | rrdev = NULL; |
| 973 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 974 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 975 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 976 | rdev = NULL; |
| 977 | if (rdev) |
| 978 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 979 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 980 | rrdev = NULL; |
| 981 | if (rrdev) |
| 982 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 983 | rcu_read_unlock(); |
| 984 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 985 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 986 | * need to check for writes. We never accept write errors |
| 987 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 988 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 989 | while (op_is_write(op) && rdev && |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 990 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 991 | sector_t first_bad; |
| 992 | int bad_sectors; |
| 993 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 994 | &first_bad, &bad_sectors); |
| 995 | if (!bad) |
| 996 | break; |
| 997 | |
| 998 | if (bad < 0) { |
| 999 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 1000 | if (!conf->mddev->external && |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 1001 | conf->mddev->sb_flags) { |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1002 | /* It is very unlikely, but we might |
| 1003 | * still need to write out the |
| 1004 | * bad block log - better give it |
| 1005 | * a chance*/ |
| 1006 | md_check_recovery(conf->mddev); |
| 1007 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 1008 | /* |
| 1009 | * Because md_wait_for_blocked_rdev |
| 1010 | * will dec nr_pending, we must |
| 1011 | * increment it first. |
| 1012 | */ |
| 1013 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 1014 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 1015 | } else { |
| 1016 | /* Acknowledged bad block - skip the write */ |
| 1017 | rdev_dec_pending(rdev, conf->mddev); |
| 1018 | rdev = NULL; |
| 1019 | } |
| 1020 | } |
| 1021 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1022 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1023 | if (s->syncing || s->expanding || s->expanded |
| 1024 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1025 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 1026 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 1027 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1028 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1029 | bi->bi_bdev = rdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1030 | bio_set_op_attrs(bi, op, op_flags); |
| 1031 | bi->bi_end_io = op_is_write(op) |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1032 | ? raid5_end_write_request |
| 1033 | : raid5_end_read_request; |
| 1034 | bi->bi_private = sh; |
| 1035 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1036 | pr_debug("%s: for %llu schedule op %d on disc %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1037 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1038 | bi->bi_opf, i); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1039 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1040 | if (sh != head_sh) |
| 1041 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1042 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1043 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1044 | + rdev->new_data_offset); |
| 1045 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1046 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1047 | + rdev->data_offset); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1048 | if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags)) |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1049 | bi->bi_opf |= REQ_NOMERGE; |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1050 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1051 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1052 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 1053 | |
| 1054 | if (!op_is_write(op) && |
| 1055 | test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 1056 | /* |
| 1057 | * issuing read for a page in journal, this |
| 1058 | * must be preparing for prexor in rmw; read |
| 1059 | * the data into orig_page |
| 1060 | */ |
| 1061 | sh->dev[i].vec.bv_page = sh->dev[i].orig_page; |
| 1062 | else |
| 1063 | sh->dev[i].vec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1064 | bi->bi_vcnt = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1065 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 1066 | bi->bi_io_vec[0].bv_offset = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1067 | bi->bi_iter.bi_size = STRIPE_SIZE; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1068 | /* |
| 1069 | * If this is discard request, set bi_vcnt 0. We don't |
| 1070 | * want to confuse SCSI because SCSI will replace payload |
| 1071 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1072 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1073 | bi->bi_vcnt = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1074 | if (rrdev) |
| 1075 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1076 | |
| 1077 | if (conf->mddev->gendisk) |
| 1078 | trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), |
| 1079 | bi, disk_devt(conf->mddev->gendisk), |
| 1080 | sh->dev[i].sector); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1081 | defer_bio_issue(conf, bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1082 | } |
| 1083 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1084 | if (s->syncing || s->expanding || s->expanded |
| 1085 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1086 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 1087 | |
| 1088 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1089 | |
| 1090 | rbi->bi_bdev = rrdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1091 | bio_set_op_attrs(rbi, op, op_flags); |
| 1092 | BUG_ON(!op_is_write(op)); |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1093 | rbi->bi_end_io = raid5_end_write_request; |
| 1094 | rbi->bi_private = sh; |
| 1095 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1096 | pr_debug("%s: for %llu schedule op %d on " |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1097 | "replacement disc %d\n", |
| 1098 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1099 | rbi->bi_opf, i); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1100 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1101 | if (sh != head_sh) |
| 1102 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1103 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1104 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1105 | + rrdev->new_data_offset); |
| 1106 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1107 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1108 | + rrdev->data_offset); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1109 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1110 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 1111 | sh->dev[i].rvec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1112 | rbi->bi_vcnt = 1; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1113 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 1114 | rbi->bi_io_vec[0].bv_offset = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1115 | rbi->bi_iter.bi_size = STRIPE_SIZE; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1116 | /* |
| 1117 | * If this is discard request, set bi_vcnt 0. We don't |
| 1118 | * want to confuse SCSI because SCSI will replace payload |
| 1119 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1120 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1121 | rbi->bi_vcnt = 0; |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1122 | if (conf->mddev->gendisk) |
| 1123 | trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), |
| 1124 | rbi, disk_devt(conf->mddev->gendisk), |
| 1125 | sh->dev[i].sector); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 1126 | defer_bio_issue(conf, rbi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1127 | } |
| 1128 | if (!rdev && !rrdev) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1129 | if (op_is_write(op)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1130 | set_bit(STRIPE_DEGRADED, &sh->state); |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1131 | pr_debug("skip op %d on disc %d for sector %llu\n", |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1132 | bi->bi_opf, i, (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1133 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1134 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1135 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1136 | |
| 1137 | if (!head_sh->batch_head) |
| 1138 | continue; |
| 1139 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1140 | batch_list); |
| 1141 | if (sh != head_sh) |
| 1142 | goto again; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | static struct dma_async_tx_descriptor * |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1147 | async_copy_data(int frombio, struct bio *bio, struct page **page, |
| 1148 | sector_t sector, struct dma_async_tx_descriptor *tx, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1149 | struct stripe_head *sh, int no_skipcopy) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1150 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1151 | struct bio_vec bvl; |
| 1152 | struct bvec_iter iter; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1153 | struct page *bio_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1154 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1155 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1156 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1157 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1158 | if (bio->bi_iter.bi_sector >= sector) |
| 1159 | page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1160 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1161 | page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1162 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1163 | if (frombio) |
| 1164 | flags |= ASYNC_TX_FENCE; |
| 1165 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 1166 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1167 | bio_for_each_segment(bvl, bio, iter) { |
| 1168 | int len = bvl.bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1169 | int clen; |
| 1170 | int b_offset = 0; |
| 1171 | |
| 1172 | if (page_offset < 0) { |
| 1173 | b_offset = -page_offset; |
| 1174 | page_offset += b_offset; |
| 1175 | len -= b_offset; |
| 1176 | } |
| 1177 | |
| 1178 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 1179 | clen = STRIPE_SIZE - page_offset; |
| 1180 | else |
| 1181 | clen = len; |
| 1182 | |
| 1183 | if (clen > 0) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1184 | b_offset += bvl.bv_offset; |
| 1185 | bio_page = bvl.bv_page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1186 | if (frombio) { |
| 1187 | if (sh->raid_conf->skip_copy && |
| 1188 | b_offset == 0 && page_offset == 0 && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1189 | clen == STRIPE_SIZE && |
| 1190 | !no_skipcopy) |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1191 | *page = bio_page; |
| 1192 | else |
| 1193 | tx = async_memcpy(*page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1194 | b_offset, clen, &submit); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1195 | } else |
| 1196 | tx = async_memcpy(bio_page, *page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1197 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1198 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1199 | /* chain the operations */ |
| 1200 | submit.depend_tx = tx; |
| 1201 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1202 | if (clen < len) /* hit end of page */ |
| 1203 | break; |
| 1204 | page_offset += len; |
| 1205 | } |
| 1206 | |
| 1207 | return tx; |
| 1208 | } |
| 1209 | |
| 1210 | static void ops_complete_biofill(void *stripe_head_ref) |
| 1211 | { |
| 1212 | struct stripe_head *sh = stripe_head_ref; |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 1213 | struct bio_list return_bi = BIO_EMPTY_LIST; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1214 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1215 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1216 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1217 | (unsigned long long)sh->sector); |
| 1218 | |
| 1219 | /* clear completed biofills */ |
| 1220 | for (i = sh->disks; i--; ) { |
| 1221 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1222 | |
| 1223 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1224 | /* and check if we need to reply to a read request, |
| 1225 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1226 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1227 | */ |
| 1228 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1229 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1230 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1231 | BUG_ON(!dev->read); |
| 1232 | rbi = dev->read; |
| 1233 | dev->read = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1234 | while (rbi && rbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1235 | dev->sector + STRIPE_SECTORS) { |
| 1236 | rbi2 = r5_next_bio(rbi, dev->sector); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 1237 | if (!raid5_dec_bi_active_stripes(rbi)) |
| 1238 | bio_list_add(&return_bi, rbi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1239 | rbi = rbi2; |
| 1240 | } |
| 1241 | } |
| 1242 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1243 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1244 | |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 1245 | return_io(&return_bi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1246 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1247 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1248 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1249 | } |
| 1250 | |
| 1251 | static void ops_run_biofill(struct stripe_head *sh) |
| 1252 | { |
| 1253 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1254 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1255 | int i; |
| 1256 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1257 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1258 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1259 | (unsigned long long)sh->sector); |
| 1260 | |
| 1261 | for (i = sh->disks; i--; ) { |
| 1262 | struct r5dev *dev = &sh->dev[i]; |
| 1263 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 1264 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1265 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1266 | dev->read = rbi = dev->toread; |
| 1267 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1268 | spin_unlock_irq(&sh->stripe_lock); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1269 | while (rbi && rbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1270 | dev->sector + STRIPE_SECTORS) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1271 | tx = async_copy_data(0, rbi, &dev->page, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1272 | dev->sector, tx, sh, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1273 | rbi = r5_next_bio(rbi, dev->sector); |
| 1274 | } |
| 1275 | } |
| 1276 | } |
| 1277 | |
| 1278 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1279 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 1280 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1281 | } |
| 1282 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1283 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 1284 | { |
| 1285 | struct r5dev *tgt; |
| 1286 | |
| 1287 | if (target < 0) |
| 1288 | return; |
| 1289 | |
| 1290 | tgt = &sh->dev[target]; |
| 1291 | set_bit(R5_UPTODATE, &tgt->flags); |
| 1292 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1293 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 1294 | } |
| 1295 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1296 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1297 | { |
| 1298 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1299 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1300 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1301 | (unsigned long long)sh->sector); |
| 1302 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1303 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1304 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1305 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1306 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1307 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 1308 | if (sh->check_state == check_state_compute_run) |
| 1309 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1310 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1311 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1314 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 1315 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1316 | struct raid5_percpu *percpu, int i) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1317 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1318 | void *addr; |
| 1319 | |
| 1320 | addr = flex_array_get(percpu->scribble, i); |
| 1321 | return addr + sizeof(struct page *) * (sh->disks + 2); |
| 1322 | } |
| 1323 | |
| 1324 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 1325 | static struct page **to_addr_page(struct raid5_percpu *percpu, int i) |
| 1326 | { |
| 1327 | void *addr; |
| 1328 | |
| 1329 | addr = flex_array_get(percpu->scribble, i); |
| 1330 | return addr; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1331 | } |
| 1332 | |
| 1333 | static struct dma_async_tx_descriptor * |
| 1334 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1335 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1336 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1337 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1338 | int target = sh->ops.target; |
| 1339 | struct r5dev *tgt = &sh->dev[target]; |
| 1340 | struct page *xor_dest = tgt->page; |
| 1341 | int count = 0; |
| 1342 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1343 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1344 | int i; |
| 1345 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1346 | BUG_ON(sh->batch_head); |
| 1347 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1348 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1349 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1350 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1351 | |
| 1352 | for (i = disks; i--; ) |
| 1353 | if (i != target) |
| 1354 | xor_srcs[count++] = sh->dev[i].page; |
| 1355 | |
| 1356 | atomic_inc(&sh->count); |
| 1357 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1358 | 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] | 1359 | ops_complete_compute, sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1360 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1361 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1362 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1363 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1364 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1365 | return tx; |
| 1366 | } |
| 1367 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1368 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 1369 | * @srcs - (struct page *) array of size sh->disks |
| 1370 | * @sh - stripe_head to parse |
| 1371 | * |
| 1372 | * Populates srcs in proper layout order for the stripe and returns the |
| 1373 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 1374 | * destination buffer is recorded in srcs[count] and the Q destination |
| 1375 | * is recorded in srcs[count+1]]. |
| 1376 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1377 | static int set_syndrome_sources(struct page **srcs, |
| 1378 | struct stripe_head *sh, |
| 1379 | int srctype) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1380 | { |
| 1381 | int disks = sh->disks; |
| 1382 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 1383 | int d0_idx = raid6_d0(sh); |
| 1384 | int count; |
| 1385 | int i; |
| 1386 | |
| 1387 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1388 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1389 | |
| 1390 | count = 0; |
| 1391 | i = d0_idx; |
| 1392 | do { |
| 1393 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1394 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1395 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1396 | if (i == sh->qd_idx || i == sh->pd_idx || |
| 1397 | (srctype == SYNDROME_SRC_ALL) || |
| 1398 | (srctype == SYNDROME_SRC_WANT_DRAIN && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1399 | (test_bit(R5_Wantdrain, &dev->flags) || |
| 1400 | test_bit(R5_InJournal, &dev->flags))) || |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1401 | (srctype == SYNDROME_SRC_WRITTEN && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1402 | dev->written)) { |
| 1403 | if (test_bit(R5_InJournal, &dev->flags)) |
| 1404 | srcs[slot] = sh->dev[i].orig_page; |
| 1405 | else |
| 1406 | srcs[slot] = sh->dev[i].page; |
| 1407 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1408 | i = raid6_next_disk(i, disks); |
| 1409 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1410 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1411 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1412 | } |
| 1413 | |
| 1414 | static struct dma_async_tx_descriptor * |
| 1415 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1416 | { |
| 1417 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1418 | struct page **blocks = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1419 | int target; |
| 1420 | int qd_idx = sh->qd_idx; |
| 1421 | struct dma_async_tx_descriptor *tx; |
| 1422 | struct async_submit_ctl submit; |
| 1423 | struct r5dev *tgt; |
| 1424 | struct page *dest; |
| 1425 | int i; |
| 1426 | int count; |
| 1427 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1428 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1429 | if (sh->ops.target < 0) |
| 1430 | target = sh->ops.target2; |
| 1431 | else if (sh->ops.target2 < 0) |
| 1432 | target = sh->ops.target; |
| 1433 | else |
| 1434 | /* we should only have one valid target */ |
| 1435 | BUG(); |
| 1436 | BUG_ON(target < 0); |
| 1437 | pr_debug("%s: stripe %llu block: %d\n", |
| 1438 | __func__, (unsigned long long)sh->sector, target); |
| 1439 | |
| 1440 | tgt = &sh->dev[target]; |
| 1441 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1442 | dest = tgt->page; |
| 1443 | |
| 1444 | atomic_inc(&sh->count); |
| 1445 | |
| 1446 | if (target == qd_idx) { |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1447 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1448 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 1449 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1450 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1451 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1452 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1453 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 1454 | } else { |
| 1455 | /* Compute any data- or p-drive using XOR */ |
| 1456 | count = 0; |
| 1457 | for (i = disks; i-- ; ) { |
| 1458 | if (i == target || i == qd_idx) |
| 1459 | continue; |
| 1460 | blocks[count++] = sh->dev[i].page; |
| 1461 | } |
| 1462 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1463 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1464 | NULL, ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1465 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1466 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 1467 | } |
| 1468 | |
| 1469 | return tx; |
| 1470 | } |
| 1471 | |
| 1472 | static struct dma_async_tx_descriptor * |
| 1473 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1474 | { |
| 1475 | int i, count, disks = sh->disks; |
| 1476 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1477 | int d0_idx = raid6_d0(sh); |
| 1478 | int faila = -1, failb = -1; |
| 1479 | int target = sh->ops.target; |
| 1480 | int target2 = sh->ops.target2; |
| 1481 | struct r5dev *tgt = &sh->dev[target]; |
| 1482 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1483 | struct dma_async_tx_descriptor *tx; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1484 | struct page **blocks = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1485 | struct async_submit_ctl submit; |
| 1486 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1487 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1488 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1489 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1490 | BUG_ON(target < 0 || target2 < 0); |
| 1491 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1492 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1493 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1494 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1495 | * slot number conversion for 'faila' and 'failb' |
| 1496 | */ |
| 1497 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1498 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1499 | count = 0; |
| 1500 | i = d0_idx; |
| 1501 | do { |
| 1502 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1503 | |
| 1504 | blocks[slot] = sh->dev[i].page; |
| 1505 | |
| 1506 | if (i == target) |
| 1507 | faila = slot; |
| 1508 | if (i == target2) |
| 1509 | failb = slot; |
| 1510 | i = raid6_next_disk(i, disks); |
| 1511 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1512 | |
| 1513 | BUG_ON(faila == failb); |
| 1514 | if (failb < faila) |
| 1515 | swap(faila, failb); |
| 1516 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1517 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1518 | |
| 1519 | atomic_inc(&sh->count); |
| 1520 | |
| 1521 | if (failb == syndrome_disks+1) { |
| 1522 | /* Q disk is one of the missing disks */ |
| 1523 | if (faila == syndrome_disks) { |
| 1524 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1525 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1526 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1527 | to_addr_conv(sh, percpu, 0)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1528 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1529 | STRIPE_SIZE, &submit); |
| 1530 | } else { |
| 1531 | struct page *dest; |
| 1532 | int data_target; |
| 1533 | int qd_idx = sh->qd_idx; |
| 1534 | |
| 1535 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1536 | if (target == qd_idx) |
| 1537 | data_target = target2; |
| 1538 | else |
| 1539 | data_target = target; |
| 1540 | |
| 1541 | count = 0; |
| 1542 | for (i = disks; i-- ; ) { |
| 1543 | if (i == data_target || i == qd_idx) |
| 1544 | continue; |
| 1545 | blocks[count++] = sh->dev[i].page; |
| 1546 | } |
| 1547 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1548 | init_async_submit(&submit, |
| 1549 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1550 | NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1551 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1552 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1553 | &submit); |
| 1554 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1555 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1556 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1557 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1558 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1559 | return async_gen_syndrome(blocks, 0, count+2, |
| 1560 | STRIPE_SIZE, &submit); |
| 1561 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1562 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1563 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1564 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1565 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1566 | if (failb == syndrome_disks) { |
| 1567 | /* We're missing D+P. */ |
| 1568 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1569 | STRIPE_SIZE, faila, |
| 1570 | blocks, &submit); |
| 1571 | } else { |
| 1572 | /* We're missing D+D. */ |
| 1573 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1574 | STRIPE_SIZE, faila, failb, |
| 1575 | blocks, &submit); |
| 1576 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1577 | } |
| 1578 | } |
| 1579 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1580 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1581 | { |
| 1582 | struct stripe_head *sh = stripe_head_ref; |
| 1583 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1584 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1585 | (unsigned long long)sh->sector); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1586 | |
| 1587 | if (r5c_is_writeback(sh->raid_conf->log)) |
| 1588 | /* |
| 1589 | * raid5-cache write back uses orig_page during prexor. |
| 1590 | * After prexor, it is time to free orig_page |
| 1591 | */ |
| 1592 | r5c_release_extra_page(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1596 | ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1597 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1598 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1599 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1600 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1601 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1602 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1603 | |
| 1604 | /* existing parity data subtracted */ |
| 1605 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1606 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1607 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1608 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1609 | (unsigned long long)sh->sector); |
| 1610 | |
| 1611 | for (i = disks; i--; ) { |
| 1612 | struct r5dev *dev = &sh->dev[i]; |
| 1613 | /* Only process blocks that are known to be uptodate */ |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1614 | if (test_bit(R5_InJournal, &dev->flags)) |
| 1615 | xor_srcs[count++] = dev->orig_page; |
| 1616 | else if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1617 | xor_srcs[count++] = dev->page; |
| 1618 | } |
| 1619 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1620 | 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] | 1621 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1622 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1623 | |
| 1624 | return tx; |
| 1625 | } |
| 1626 | |
| 1627 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1628 | ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1629 | struct dma_async_tx_descriptor *tx) |
| 1630 | { |
| 1631 | struct page **blocks = to_addr_page(percpu, 0); |
| 1632 | int count; |
| 1633 | struct async_submit_ctl submit; |
| 1634 | |
| 1635 | pr_debug("%s: stripe %llu\n", __func__, |
| 1636 | (unsigned long long)sh->sector); |
| 1637 | |
| 1638 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN); |
| 1639 | |
| 1640 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx, |
| 1641 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
| 1642 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 1643 | |
| 1644 | return tx; |
| 1645 | } |
| 1646 | |
| 1647 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1648 | 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] | 1649 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1650 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1651 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1652 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1653 | struct stripe_head *head_sh = sh; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1654 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1655 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1656 | (unsigned long long)sh->sector); |
| 1657 | |
| 1658 | for (i = disks; i--; ) { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1659 | struct r5dev *dev; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1660 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1661 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1662 | sh = head_sh; |
| 1663 | if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1664 | struct bio *wbi; |
| 1665 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1666 | again: |
| 1667 | dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1668 | /* |
| 1669 | * clear R5_InJournal, so when rewriting a page in |
| 1670 | * journal, it is not skipped by r5l_log_stripe() |
| 1671 | */ |
| 1672 | clear_bit(R5_InJournal, &dev->flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1673 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1674 | chosen = dev->towrite; |
| 1675 | dev->towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1676 | sh->overwrite_disks = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1677 | BUG_ON(dev->written); |
| 1678 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1679 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1680 | WARN_ON(dev->page != dev->orig_page); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1681 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1682 | while (wbi && wbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1683 | dev->sector + STRIPE_SECTORS) { |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1684 | if (wbi->bi_opf & REQ_FUA) |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1685 | set_bit(R5_WantFUA, &dev->flags); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1686 | if (wbi->bi_opf & REQ_SYNC) |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1687 | set_bit(R5_SyncIO, &dev->flags); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1688 | if (bio_op(wbi) == REQ_OP_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1689 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1690 | else { |
| 1691 | tx = async_copy_data(1, wbi, &dev->page, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1692 | dev->sector, tx, sh, |
| 1693 | r5c_is_writeback(conf->log)); |
| 1694 | if (dev->page != dev->orig_page && |
| 1695 | !r5c_is_writeback(conf->log)) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1696 | set_bit(R5_SkipCopy, &dev->flags); |
| 1697 | clear_bit(R5_UPTODATE, &dev->flags); |
| 1698 | clear_bit(R5_OVERWRITE, &dev->flags); |
| 1699 | } |
| 1700 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1701 | wbi = r5_next_bio(wbi, dev->sector); |
| 1702 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1703 | |
| 1704 | if (head_sh->batch_head) { |
| 1705 | sh = list_first_entry(&sh->batch_list, |
| 1706 | struct stripe_head, |
| 1707 | batch_list); |
| 1708 | if (sh == head_sh) |
| 1709 | continue; |
| 1710 | goto again; |
| 1711 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1712 | } |
| 1713 | } |
| 1714 | |
| 1715 | return tx; |
| 1716 | } |
| 1717 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1718 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1719 | { |
| 1720 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1721 | int disks = sh->disks; |
| 1722 | int pd_idx = sh->pd_idx; |
| 1723 | int qd_idx = sh->qd_idx; |
| 1724 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1725 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1726 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1727 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1728 | (unsigned long long)sh->sector); |
| 1729 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1730 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1731 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1732 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1733 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1734 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1735 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1736 | for (i = disks; i--; ) { |
| 1737 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1738 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1739 | if (dev->written || i == pd_idx || i == qd_idx) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1740 | if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1741 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1742 | if (fua) |
| 1743 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1744 | if (sync) |
| 1745 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1746 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1747 | } |
| 1748 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1749 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1750 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1751 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1752 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1753 | else { |
| 1754 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1755 | sh->reconstruct_state = reconstruct_state_result; |
| 1756 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1757 | |
| 1758 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1759 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1763 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1764 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1765 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1766 | int disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1767 | struct page **xor_srcs; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1768 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1769 | int count, pd_idx = sh->pd_idx, i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1770 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1771 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1772 | unsigned long flags; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1773 | int j = 0; |
| 1774 | struct stripe_head *head_sh = sh; |
| 1775 | int last_stripe; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1776 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1777 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1778 | (unsigned long long)sh->sector); |
| 1779 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1780 | for (i = 0; i < sh->disks; i++) { |
| 1781 | if (pd_idx == i) |
| 1782 | continue; |
| 1783 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1784 | break; |
| 1785 | } |
| 1786 | if (i >= sh->disks) { |
| 1787 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1788 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 1789 | ops_complete_reconstruct(sh); |
| 1790 | return; |
| 1791 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1792 | again: |
| 1793 | count = 0; |
| 1794 | xor_srcs = to_addr_page(percpu, j); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1795 | /* check if prexor is active which means only process blocks |
| 1796 | * that are part of a read-modify-write (written) |
| 1797 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1798 | if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1799 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1800 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1801 | for (i = disks; i--; ) { |
| 1802 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1803 | if (head_sh->dev[i].written || |
| 1804 | test_bit(R5_InJournal, &head_sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1805 | xor_srcs[count++] = dev->page; |
| 1806 | } |
| 1807 | } else { |
| 1808 | xor_dest = sh->dev[pd_idx].page; |
| 1809 | for (i = disks; i--; ) { |
| 1810 | struct r5dev *dev = &sh->dev[i]; |
| 1811 | if (i != pd_idx) |
| 1812 | xor_srcs[count++] = dev->page; |
| 1813 | } |
| 1814 | } |
| 1815 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1816 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1817 | * 2/ if we did not prexor then we are redoing the parity |
| 1818 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1819 | * for the synchronous xor case |
| 1820 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1821 | last_stripe = !head_sh->batch_head || |
| 1822 | list_first_entry(&sh->batch_list, |
| 1823 | struct stripe_head, batch_list) == head_sh; |
| 1824 | if (last_stripe) { |
| 1825 | flags = ASYNC_TX_ACK | |
| 1826 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1827 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1828 | atomic_inc(&head_sh->count); |
| 1829 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh, |
| 1830 | to_addr_conv(sh, percpu, j)); |
| 1831 | } else { |
| 1832 | flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST; |
| 1833 | init_async_submit(&submit, flags, tx, NULL, NULL, |
| 1834 | to_addr_conv(sh, percpu, j)); |
| 1835 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1836 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1837 | if (unlikely(count == 1)) |
| 1838 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1839 | else |
| 1840 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1841 | if (!last_stripe) { |
| 1842 | j++; |
| 1843 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1844 | batch_list); |
| 1845 | goto again; |
| 1846 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1847 | } |
| 1848 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1849 | static void |
| 1850 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1851 | struct dma_async_tx_descriptor *tx) |
| 1852 | { |
| 1853 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1854 | struct page **blocks; |
| 1855 | int count, i, j = 0; |
| 1856 | struct stripe_head *head_sh = sh; |
| 1857 | int last_stripe; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1858 | int synflags; |
| 1859 | unsigned long txflags; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1860 | |
| 1861 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1862 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1863 | for (i = 0; i < sh->disks; i++) { |
| 1864 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 1865 | continue; |
| 1866 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1867 | break; |
| 1868 | } |
| 1869 | if (i >= sh->disks) { |
| 1870 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1871 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 1872 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 1873 | ops_complete_reconstruct(sh); |
| 1874 | return; |
| 1875 | } |
| 1876 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1877 | again: |
| 1878 | blocks = to_addr_page(percpu, j); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1879 | |
| 1880 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1881 | synflags = SYNDROME_SRC_WRITTEN; |
| 1882 | txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST; |
| 1883 | } else { |
| 1884 | synflags = SYNDROME_SRC_ALL; |
| 1885 | txflags = ASYNC_TX_ACK; |
| 1886 | } |
| 1887 | |
| 1888 | count = set_syndrome_sources(blocks, sh, synflags); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1889 | last_stripe = !head_sh->batch_head || |
| 1890 | list_first_entry(&sh->batch_list, |
| 1891 | struct stripe_head, batch_list) == head_sh; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1892 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1893 | if (last_stripe) { |
| 1894 | atomic_inc(&head_sh->count); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1895 | init_async_submit(&submit, txflags, tx, ops_complete_reconstruct, |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1896 | head_sh, to_addr_conv(sh, percpu, j)); |
| 1897 | } else |
| 1898 | init_async_submit(&submit, 0, tx, NULL, NULL, |
| 1899 | to_addr_conv(sh, percpu, j)); |
Shaohua Li | 4876969 | 2015-05-13 09:30:08 -0700 | [diff] [blame] | 1900 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1901 | if (!last_stripe) { |
| 1902 | j++; |
| 1903 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1904 | batch_list); |
| 1905 | goto again; |
| 1906 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1907 | } |
| 1908 | |
| 1909 | static void ops_complete_check(void *stripe_head_ref) |
| 1910 | { |
| 1911 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1912 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1913 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1914 | (unsigned long long)sh->sector); |
| 1915 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1916 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1917 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1918 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1919 | } |
| 1920 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1921 | 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] | 1922 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1923 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1924 | int pd_idx = sh->pd_idx; |
| 1925 | int qd_idx = sh->qd_idx; |
| 1926 | struct page *xor_dest; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1927 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1928 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1929 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1930 | int count; |
| 1931 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1932 | |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1933 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1934 | (unsigned long long)sh->sector); |
| 1935 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1936 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1937 | count = 0; |
| 1938 | xor_dest = sh->dev[pd_idx].page; |
| 1939 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1940 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1941 | if (i == pd_idx || i == qd_idx) |
| 1942 | continue; |
| 1943 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1944 | } |
| 1945 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1946 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1947 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1948 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1949 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1950 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1951 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1952 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1953 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1954 | } |
| 1955 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1956 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1957 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1958 | struct page **srcs = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1959 | struct async_submit_ctl submit; |
| 1960 | int count; |
| 1961 | |
| 1962 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1963 | (unsigned long long)sh->sector, checkp); |
| 1964 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1965 | BUG_ON(sh->batch_head); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1966 | count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1967 | if (!checkp) |
| 1968 | srcs[count] = NULL; |
| 1969 | |
| 1970 | atomic_inc(&sh->count); |
| 1971 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1972 | sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1973 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1974 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1975 | } |
| 1976 | |
NeilBrown | 51acbce | 2013-02-28 09:08:34 +1100 | [diff] [blame] | 1977 | 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] | 1978 | { |
| 1979 | int overlap_clear = 0, i, disks = sh->disks; |
| 1980 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1981 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1982 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1983 | struct raid5_percpu *percpu; |
| 1984 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1985 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1986 | cpu = get_cpu(); |
| 1987 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1988 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1989 | ops_run_biofill(sh); |
| 1990 | overlap_clear++; |
| 1991 | } |
| 1992 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1993 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1994 | if (level < 6) |
| 1995 | tx = ops_run_compute5(sh, percpu); |
| 1996 | else { |
| 1997 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1998 | tx = ops_run_compute6_1(sh, percpu); |
| 1999 | else |
| 2000 | tx = ops_run_compute6_2(sh, percpu); |
| 2001 | } |
| 2002 | /* terminate the chain if reconstruct is not set to be run */ |
| 2003 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 2004 | async_tx_ack(tx); |
| 2005 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2006 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2007 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) { |
| 2008 | if (level < 6) |
| 2009 | tx = ops_run_prexor5(sh, percpu, tx); |
| 2010 | else |
| 2011 | tx = ops_run_prexor6(sh, percpu, tx); |
| 2012 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2013 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2014 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2015 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2016 | overlap_clear++; |
| 2017 | } |
| 2018 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2019 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 2020 | if (level < 6) |
| 2021 | ops_run_reconstruct5(sh, percpu, tx); |
| 2022 | else |
| 2023 | ops_run_reconstruct6(sh, percpu, tx); |
| 2024 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2025 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 2026 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 2027 | if (sh->check_state == check_state_run) |
| 2028 | ops_run_check_p(sh, percpu); |
| 2029 | else if (sh->check_state == check_state_run_q) |
| 2030 | ops_run_check_pq(sh, percpu, 0); |
| 2031 | else if (sh->check_state == check_state_run_pq) |
| 2032 | ops_run_check_pq(sh, percpu, 1); |
| 2033 | else |
| 2034 | BUG(); |
| 2035 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2036 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2037 | if (overlap_clear && !sh->batch_head) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2038 | for (i = disks; i--; ) { |
| 2039 | struct r5dev *dev = &sh->dev[i]; |
| 2040 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 2041 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 2042 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2043 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2044 | } |
| 2045 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2046 | static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp, |
| 2047 | int disks) |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2048 | { |
| 2049 | struct stripe_head *sh; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2050 | int i; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2051 | |
| 2052 | sh = kmem_cache_zalloc(sc, gfp); |
| 2053 | if (sh) { |
| 2054 | spin_lock_init(&sh->stripe_lock); |
| 2055 | spin_lock_init(&sh->batch_lock); |
| 2056 | INIT_LIST_HEAD(&sh->batch_list); |
| 2057 | INIT_LIST_HEAD(&sh->lru); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2058 | INIT_LIST_HEAD(&sh->r5c); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2059 | INIT_LIST_HEAD(&sh->log_list); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2060 | atomic_set(&sh->count, 1); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2061 | sh->log_start = MaxSector; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2062 | for (i = 0; i < disks; i++) { |
| 2063 | struct r5dev *dev = &sh->dev[i]; |
| 2064 | |
Ming Lei | 3a83f46 | 2016-11-22 08:57:21 -0700 | [diff] [blame] | 2065 | bio_init(&dev->req, &dev->vec, 1); |
| 2066 | bio_init(&dev->rreq, &dev->rvec, 1); |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2067 | } |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2068 | } |
| 2069 | return sh; |
| 2070 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2071 | static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2072 | { |
| 2073 | struct stripe_head *sh; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2074 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2075 | sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2076 | if (!sh) |
| 2077 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 2078 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2079 | sh->raid_conf = conf; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2080 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 2081 | if (grow_buffers(sh, gfp)) { |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2082 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2083 | kmem_cache_free(conf->slab_cache, sh); |
| 2084 | return 0; |
| 2085 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2086 | sh->hash_lock_index = |
| 2087 | conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2088 | /* we just created an active stripe so... */ |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2089 | atomic_inc(&conf->active_stripes); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2090 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2091 | raid5_release_stripe(sh); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2092 | conf->max_nr_stripes++; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2093 | return 1; |
| 2094 | } |
| 2095 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2096 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2097 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2098 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2099 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2100 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2101 | if (conf->mddev->gendisk) |
| 2102 | sprintf(conf->cache_name[0], |
| 2103 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 2104 | else |
| 2105 | sprintf(conf->cache_name[0], |
| 2106 | "raid%d-%p", conf->level, conf->mddev); |
| 2107 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 2108 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2109 | conf->active_name = 0; |
| 2110 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2111 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2112 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2113 | if (!sc) |
| 2114 | return 1; |
| 2115 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2116 | conf->pool_size = devs; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2117 | while (num--) |
| 2118 | if (!grow_one_stripe(conf, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2119 | return 1; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2120 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2121 | return 0; |
| 2122 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2123 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2124 | /** |
| 2125 | * scribble_len - return the required size of the scribble region |
| 2126 | * @num - total number of disks in the array |
| 2127 | * |
| 2128 | * The size must be enough to contain: |
| 2129 | * 1/ a struct page pointer for each device in the array +2 |
| 2130 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 2131 | * (dma_map_page()) or page (page_address()) address. |
| 2132 | * |
| 2133 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 2134 | * calculate over all devices (not just the data blocks), using zeros in place |
| 2135 | * of the P and Q blocks. |
| 2136 | */ |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2137 | static struct flex_array *scribble_alloc(int num, int cnt, gfp_t flags) |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2138 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2139 | struct flex_array *ret; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2140 | size_t len; |
| 2141 | |
| 2142 | len = sizeof(struct page *) * (num+2) + sizeof(addr_conv_t) * (num+2); |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2143 | ret = flex_array_alloc(len, cnt, flags); |
| 2144 | if (!ret) |
| 2145 | return NULL; |
| 2146 | /* always prealloc all elements, so no locking is required */ |
| 2147 | if (flex_array_prealloc(ret, 0, cnt, flags)) { |
| 2148 | flex_array_free(ret); |
| 2149 | return NULL; |
| 2150 | } |
| 2151 | return ret; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2152 | } |
| 2153 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2154 | static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors) |
| 2155 | { |
| 2156 | unsigned long cpu; |
| 2157 | int err = 0; |
| 2158 | |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2159 | /* |
| 2160 | * Never shrink. And mddev_suspend() could deadlock if this is called |
| 2161 | * from raid5d. In that case, scribble_disks and scribble_sectors |
| 2162 | * should equal to new_disks and new_sectors |
| 2163 | */ |
| 2164 | if (conf->scribble_disks >= new_disks && |
| 2165 | conf->scribble_sectors >= new_sectors) |
| 2166 | return 0; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2167 | mddev_suspend(conf->mddev); |
| 2168 | get_online_cpus(); |
| 2169 | for_each_present_cpu(cpu) { |
| 2170 | struct raid5_percpu *percpu; |
| 2171 | struct flex_array *scribble; |
| 2172 | |
| 2173 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 2174 | scribble = scribble_alloc(new_disks, |
| 2175 | new_sectors / STRIPE_SECTORS, |
| 2176 | GFP_NOIO); |
| 2177 | |
| 2178 | if (scribble) { |
| 2179 | flex_array_free(percpu->scribble); |
| 2180 | percpu->scribble = scribble; |
| 2181 | } else { |
| 2182 | err = -ENOMEM; |
| 2183 | break; |
| 2184 | } |
| 2185 | } |
| 2186 | put_online_cpus(); |
| 2187 | mddev_resume(conf->mddev); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2188 | if (!err) { |
| 2189 | conf->scribble_disks = new_disks; |
| 2190 | conf->scribble_sectors = new_sectors; |
| 2191 | } |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2192 | return err; |
| 2193 | } |
| 2194 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2195 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2196 | { |
| 2197 | /* Make all the stripes able to hold 'newsize' devices. |
| 2198 | * New slots in each stripe get 'page' set to a new page. |
| 2199 | * |
| 2200 | * This happens in stages: |
| 2201 | * 1/ create a new kmem_cache and allocate the required number of |
| 2202 | * stripe_heads. |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 2203 | * 2/ gather all the old stripe_heads and transfer the pages across |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2204 | * to the new stripe_heads. This will have the side effect of |
| 2205 | * freezing the array as once all stripe_heads have been collected, |
| 2206 | * no IO will be possible. Old stripe heads are freed once their |
| 2207 | * pages have been transferred over, and the old kmem_cache is |
| 2208 | * freed when all stripes are done. |
| 2209 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 2210 | * we simple return a failre status - no need to clean anything up. |
| 2211 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 2212 | * If this fails, we don't bother trying the shrink the |
| 2213 | * stripe_heads down again, we just leave them as they are. |
| 2214 | * As each stripe_head is processed the new one is released into |
| 2215 | * active service. |
| 2216 | * |
| 2217 | * Once step2 is started, we cannot afford to wait for a write, |
| 2218 | * so we use GFP_NOIO allocations. |
| 2219 | */ |
| 2220 | struct stripe_head *osh, *nsh; |
| 2221 | LIST_HEAD(newstripes); |
| 2222 | struct disk_info *ndisks; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 2223 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2224 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2225 | int i; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2226 | int hash, cnt; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2227 | |
| 2228 | if (newsize <= conf->pool_size) |
| 2229 | return 0; /* never bother to shrink */ |
| 2230 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 2231 | err = md_allow_write(conf->mddev); |
| 2232 | if (err) |
| 2233 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 2234 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2235 | /* Step 1 */ |
| 2236 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 2237 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2238 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2239 | if (!sc) |
| 2240 | return -ENOMEM; |
| 2241 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2242 | /* Need to ensure auto-resizing doesn't interfere */ |
| 2243 | mutex_lock(&conf->cache_size_mutex); |
| 2244 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2245 | for (i = conf->max_nr_stripes; i; i--) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2246 | nsh = alloc_stripe(sc, GFP_KERNEL, newsize); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2247 | if (!nsh) |
| 2248 | break; |
| 2249 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2250 | nsh->raid_conf = conf; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2251 | list_add(&nsh->lru, &newstripes); |
| 2252 | } |
| 2253 | if (i) { |
| 2254 | /* didn't get enough, give up */ |
| 2255 | while (!list_empty(&newstripes)) { |
| 2256 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2257 | list_del(&nsh->lru); |
| 2258 | kmem_cache_free(sc, nsh); |
| 2259 | } |
| 2260 | kmem_cache_destroy(sc); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2261 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2262 | return -ENOMEM; |
| 2263 | } |
| 2264 | /* Step 2 - Must use GFP_NOIO now. |
| 2265 | * OK, we have enough stripes, start collecting inactive |
| 2266 | * stripes and copying them over |
| 2267 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2268 | hash = 0; |
| 2269 | cnt = 0; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2270 | list_for_each_entry(nsh, &newstripes, lru) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2271 | lock_device_hash_lock(conf, hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 2272 | wait_event_cmd(conf->wait_for_stripe, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2273 | !list_empty(conf->inactive_list + hash), |
| 2274 | unlock_device_hash_lock(conf, hash), |
| 2275 | lock_device_hash_lock(conf, hash)); |
| 2276 | osh = get_free_stripe(conf, hash); |
| 2277 | unlock_device_hash_lock(conf, hash); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2278 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2279 | for(i=0; i<conf->pool_size; i++) { |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2280 | nsh->dev[i].page = osh->dev[i].page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2281 | nsh->dev[i].orig_page = osh->dev[i].page; |
| 2282 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2283 | nsh->hash_lock_index = hash; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2284 | kmem_cache_free(conf->slab_cache, osh); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2285 | cnt++; |
| 2286 | if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + |
| 2287 | !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { |
| 2288 | hash++; |
| 2289 | cnt = 0; |
| 2290 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2291 | } |
| 2292 | kmem_cache_destroy(conf->slab_cache); |
| 2293 | |
| 2294 | /* Step 3. |
| 2295 | * At this point, we are holding all the stripes so the array |
| 2296 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2297 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2298 | */ |
| 2299 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 2300 | if (ndisks) { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2301 | for (i = 0; i < conf->pool_size; i++) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2302 | ndisks[i] = conf->disks[i]; |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 2303 | |
| 2304 | for (i = conf->pool_size; i < newsize; i++) { |
| 2305 | ndisks[i].extra_page = alloc_page(GFP_NOIO); |
| 2306 | if (!ndisks[i].extra_page) |
| 2307 | err = -ENOMEM; |
| 2308 | } |
| 2309 | |
| 2310 | if (err) { |
| 2311 | for (i = conf->pool_size; i < newsize; i++) |
| 2312 | if (ndisks[i].extra_page) |
| 2313 | put_page(ndisks[i].extra_page); |
| 2314 | kfree(ndisks); |
| 2315 | } else { |
| 2316 | kfree(conf->disks); |
| 2317 | conf->disks = ndisks; |
| 2318 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2319 | } else |
| 2320 | err = -ENOMEM; |
| 2321 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2322 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2323 | /* Step 4, return new stripes to service */ |
| 2324 | while(!list_empty(&newstripes)) { |
| 2325 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2326 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2327 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2328 | for (i=conf->raid_disks; i < newsize; i++) |
| 2329 | if (nsh->dev[i].page == NULL) { |
| 2330 | struct page *p = alloc_page(GFP_NOIO); |
| 2331 | nsh->dev[i].page = p; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2332 | nsh->dev[i].orig_page = p; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2333 | if (!p) |
| 2334 | err = -ENOMEM; |
| 2335 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2336 | raid5_release_stripe(nsh); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2337 | } |
| 2338 | /* critical section pass, GFP_NOIO no longer needed */ |
| 2339 | |
| 2340 | conf->slab_cache = sc; |
| 2341 | conf->active_name = 1-conf->active_name; |
NeilBrown | 6e9eac2 | 2015-05-08 18:19:34 +1000 | [diff] [blame] | 2342 | if (!err) |
| 2343 | conf->pool_size = newsize; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2344 | return err; |
| 2345 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2347 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2348 | { |
| 2349 | struct stripe_head *sh; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 2350 | int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2351 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2352 | spin_lock_irq(conf->hash_locks + hash); |
| 2353 | sh = get_free_stripe(conf, hash); |
| 2354 | spin_unlock_irq(conf->hash_locks + hash); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2355 | if (!sh) |
| 2356 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2357 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2358 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2359 | kmem_cache_free(conf->slab_cache, sh); |
| 2360 | atomic_dec(&conf->active_stripes); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2361 | conf->max_nr_stripes--; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2362 | return 1; |
| 2363 | } |
| 2364 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2365 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2366 | { |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2367 | while (conf->max_nr_stripes && |
| 2368 | drop_one_stripe(conf)) |
| 2369 | ; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2370 | |
Julia Lawall | 644df1a | 2015-09-13 14:15:10 +0200 | [diff] [blame] | 2371 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2372 | conf->slab_cache = NULL; |
| 2373 | } |
| 2374 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2375 | static void raid5_end_read_request(struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2377 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2378 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2379 | int disks = sh->disks, i; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2380 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2381 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2382 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2383 | |
| 2384 | for (i=0 ; i<disks; i++) |
| 2385 | if (bi == &sh->dev[i].req) |
| 2386 | break; |
| 2387 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2388 | pr_debug("end_read_request %llu/%d, count: %d, error %d.\n", |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2389 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2390 | bi->bi_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2391 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2392 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2393 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2394 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2395 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2396 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2397 | /* If replacement finished while this request was outstanding, |
| 2398 | * 'replacement' might be NULL already. |
| 2399 | * In that case it moved down to 'rdev'. |
| 2400 | * rdev is not removed until all requests are finished. |
| 2401 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2402 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2403 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2404 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2405 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2406 | if (use_new_offset(conf, sh)) |
| 2407 | s = sh->sector + rdev->new_data_offset; |
| 2408 | else |
| 2409 | s = sh->sector + rdev->data_offset; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2410 | if (!bi->bi_error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2411 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2412 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2413 | /* Note that this cannot happen on a |
| 2414 | * replacement device. We just fail those on |
| 2415 | * any error |
| 2416 | */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2417 | pr_info_ratelimited( |
| 2418 | "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2419 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2420 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2421 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2422 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2423 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2424 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2425 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2426 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2427 | |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 2428 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 2429 | /* |
| 2430 | * end read for a page in journal, this |
| 2431 | * must be preparing for prexor in rmw |
| 2432 | */ |
| 2433 | set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags); |
| 2434 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2435 | if (atomic_read(&rdev->read_errors)) |
| 2436 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2437 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2438 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2439 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2440 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2441 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2442 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2443 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2444 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2445 | pr_warn_ratelimited( |
| 2446 | "md/raid:%s: read error on replacement device (sector %llu on %s).\n", |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2447 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2448 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2449 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2450 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 2451 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2452 | pr_warn_ratelimited( |
| 2453 | "md/raid:%s: read error not correctable (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2454 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2455 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2456 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2457 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2458 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2459 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2460 | pr_warn_ratelimited( |
| 2461 | "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2462 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2463 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2464 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2465 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2466 | > conf->max_nr_stripes) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2467 | pr_warn("md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2468 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2469 | else |
| 2470 | retry = 1; |
Bian Yu | edfa1f6 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2471 | if (set_bad && test_bit(In_sync, &rdev->flags) |
| 2472 | && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2473 | retry = 1; |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2474 | if (retry) |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2475 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
| 2476 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 2477 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2478 | } else |
| 2479 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2480 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2481 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2482 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2483 | if (!(set_bad |
| 2484 | && test_bit(In_sync, &rdev->flags) |
| 2485 | && rdev_set_badblocks( |
| 2486 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 2487 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2488 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2489 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2490 | rdev_dec_pending(rdev, conf->mddev); |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2491 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2492 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 2493 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2494 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2495 | } |
| 2496 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2497 | static void raid5_end_write_request(struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2498 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2499 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2500 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2501 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2502 | struct md_rdev *uninitialized_var(rdev); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2503 | sector_t first_bad; |
| 2504 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2505 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2506 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2507 | for (i = 0 ; i < disks; i++) { |
| 2508 | if (bi == &sh->dev[i].req) { |
| 2509 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2510 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2511 | } |
| 2512 | if (bi == &sh->dev[i].rreq) { |
| 2513 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2514 | if (rdev) |
| 2515 | replacement = 1; |
| 2516 | else |
| 2517 | /* rdev was removed and 'replacement' |
| 2518 | * replaced it. rdev is not removed |
| 2519 | * until all requests are finished. |
| 2520 | */ |
| 2521 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2522 | break; |
| 2523 | } |
| 2524 | } |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2525 | pr_debug("end_write_request %llu/%d, count %d, error: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2526 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2527 | bi->bi_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2528 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2529 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2530 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2531 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2532 | } |
| 2533 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2534 | if (replacement) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2535 | if (bi->bi_error) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2536 | md_error(conf->mddev, rdev); |
| 2537 | else if (is_badblock(rdev, sh->sector, |
| 2538 | STRIPE_SECTORS, |
| 2539 | &first_bad, &bad_sectors)) |
| 2540 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 2541 | } else { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2542 | if (bi->bi_error) { |
NeilBrown | 9f97e4b | 2014-01-16 09:35:38 +1100 | [diff] [blame] | 2543 | set_bit(STRIPE_DEGRADED, &sh->state); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2544 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2545 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2546 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2547 | set_bit(MD_RECOVERY_NEEDED, |
| 2548 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2549 | } else if (is_badblock(rdev, sh->sector, |
| 2550 | STRIPE_SECTORS, |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2551 | &first_bad, &bad_sectors)) { |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2552 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2553 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
| 2554 | /* That was a successful write so make |
| 2555 | * sure it looks like we already did |
| 2556 | * a re-write. |
| 2557 | */ |
| 2558 | set_bit(R5_ReWrite, &sh->dev[i].flags); |
| 2559 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2560 | } |
| 2561 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2562 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2563 | if (sh->batch_head && bi->bi_error && !replacement) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2564 | set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state); |
| 2565 | |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2566 | bio_reset(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2567 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 2568 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2569 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2570 | raid5_release_stripe(sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2571 | |
| 2572 | if (sh->batch_head && sh != sh->batch_head) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2573 | raid5_release_stripe(sh->batch_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2574 | } |
| 2575 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2576 | static void raid5_build_block(struct stripe_head *sh, int i, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2577 | { |
| 2578 | struct r5dev *dev = &sh->dev[i]; |
| 2579 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2580 | dev->flags = 0; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2581 | dev->sector = raid5_compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2582 | } |
| 2583 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 2584 | static void raid5_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2585 | { |
| 2586 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2587 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2588 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2589 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2590 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2591 | spin_lock_irqsave(&conf->device_lock, flags); |
| 2592 | clear_bit(In_sync, &rdev->flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2593 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2594 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2595 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2596 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2597 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 2598 | set_bit(Faulty, &rdev->flags); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 2599 | set_mask_bits(&mddev->sb_flags, 0, |
| 2600 | BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING)); |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2601 | pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n" |
| 2602 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 2603 | mdname(mddev), |
| 2604 | bdevname(rdev->bdev, b), |
| 2605 | mdname(mddev), |
| 2606 | conf->raid_disks - mddev->degraded); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 2607 | r5c_update_on_rdev_error(mddev); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2609 | |
| 2610 | /* |
| 2611 | * Input: a 'big' sector number, |
| 2612 | * Output: index of the data and parity disk, and the sector # in them. |
| 2613 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2614 | sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
| 2615 | int previous, int *dd_idx, |
| 2616 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2617 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2618 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2619 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2620 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2621 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2622 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2623 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2624 | int algorithm = previous ? conf->prev_algo |
| 2625 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2626 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2627 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2628 | int raid_disks = previous ? conf->previous_raid_disks |
| 2629 | : conf->raid_disks; |
| 2630 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2631 | |
| 2632 | /* First compute the information on this sector */ |
| 2633 | |
| 2634 | /* |
| 2635 | * Compute the chunk number and the sector offset inside the chunk |
| 2636 | */ |
| 2637 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 2638 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2639 | |
| 2640 | /* |
| 2641 | * Compute the stripe number |
| 2642 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2643 | stripe = chunk_number; |
| 2644 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2645 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2646 | /* |
| 2647 | * Select the parity disk based on the user selected algorithm. |
| 2648 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2649 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2650 | switch(conf->level) { |
| 2651 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2652 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2653 | break; |
| 2654 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2655 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2656 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2657 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2658 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2659 | (*dd_idx)++; |
| 2660 | break; |
| 2661 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2662 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2663 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2664 | (*dd_idx)++; |
| 2665 | break; |
| 2666 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2667 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2668 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2669 | break; |
| 2670 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2671 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2672 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2673 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2674 | case ALGORITHM_PARITY_0: |
| 2675 | pd_idx = 0; |
| 2676 | (*dd_idx)++; |
| 2677 | break; |
| 2678 | case ALGORITHM_PARITY_N: |
| 2679 | pd_idx = data_disks; |
| 2680 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2681 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2682 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2683 | } |
| 2684 | break; |
| 2685 | case 6: |
| 2686 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2687 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2688 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2689 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2690 | qd_idx = pd_idx + 1; |
| 2691 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2692 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2693 | qd_idx = 0; |
| 2694 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2695 | (*dd_idx) += 2; /* D D P Q D */ |
| 2696 | break; |
| 2697 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2698 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2699 | qd_idx = pd_idx + 1; |
| 2700 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2701 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2702 | qd_idx = 0; |
| 2703 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2704 | (*dd_idx) += 2; /* D D P Q D */ |
| 2705 | break; |
| 2706 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2707 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2708 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2709 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2710 | break; |
| 2711 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2712 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2713 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2714 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2715 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2716 | |
| 2717 | case ALGORITHM_PARITY_0: |
| 2718 | pd_idx = 0; |
| 2719 | qd_idx = 1; |
| 2720 | (*dd_idx) += 2; |
| 2721 | break; |
| 2722 | case ALGORITHM_PARITY_N: |
| 2723 | pd_idx = data_disks; |
| 2724 | qd_idx = data_disks + 1; |
| 2725 | break; |
| 2726 | |
| 2727 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2728 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2729 | * of blocks for computing Q is different. |
| 2730 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2731 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2732 | qd_idx = pd_idx + 1; |
| 2733 | if (pd_idx == raid_disks-1) { |
| 2734 | (*dd_idx)++; /* Q D D D P */ |
| 2735 | qd_idx = 0; |
| 2736 | } else if (*dd_idx >= pd_idx) |
| 2737 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2738 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2739 | break; |
| 2740 | |
| 2741 | case ALGORITHM_ROTATING_N_RESTART: |
| 2742 | /* Same a left_asymmetric, by first stripe is |
| 2743 | * D D D P Q rather than |
| 2744 | * Q D D D P |
| 2745 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2746 | stripe2 += 1; |
| 2747 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2748 | qd_idx = pd_idx + 1; |
| 2749 | if (pd_idx == raid_disks-1) { |
| 2750 | (*dd_idx)++; /* Q D D D P */ |
| 2751 | qd_idx = 0; |
| 2752 | } else if (*dd_idx >= pd_idx) |
| 2753 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2754 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2755 | break; |
| 2756 | |
| 2757 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2758 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2759 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2760 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2761 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2762 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2763 | break; |
| 2764 | |
| 2765 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2766 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2767 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2768 | if (*dd_idx >= pd_idx) |
| 2769 | (*dd_idx)++; |
| 2770 | qd_idx = raid_disks - 1; |
| 2771 | break; |
| 2772 | |
| 2773 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2774 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2775 | if (*dd_idx >= pd_idx) |
| 2776 | (*dd_idx)++; |
| 2777 | qd_idx = raid_disks - 1; |
| 2778 | break; |
| 2779 | |
| 2780 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2781 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2782 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2783 | qd_idx = raid_disks - 1; |
| 2784 | break; |
| 2785 | |
| 2786 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2787 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2788 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2789 | qd_idx = raid_disks - 1; |
| 2790 | break; |
| 2791 | |
| 2792 | case ALGORITHM_PARITY_0_6: |
| 2793 | pd_idx = 0; |
| 2794 | (*dd_idx)++; |
| 2795 | qd_idx = raid_disks - 1; |
| 2796 | break; |
| 2797 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2798 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2799 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2800 | } |
| 2801 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2802 | } |
| 2803 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2804 | if (sh) { |
| 2805 | sh->pd_idx = pd_idx; |
| 2806 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2807 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2808 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2809 | /* |
| 2810 | * Finally, compute the new sector number |
| 2811 | */ |
| 2812 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2813 | return new_sector; |
| 2814 | } |
| 2815 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2816 | 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] | 2817 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2818 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2819 | int raid_disks = sh->disks; |
| 2820 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2821 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2822 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2823 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2824 | int algorithm = previous ? conf->prev_algo |
| 2825 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2826 | sector_t stripe; |
| 2827 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2828 | sector_t chunk_number; |
| 2829 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2830 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2831 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2832 | |
| 2833 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2834 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2835 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2836 | if (i == sh->pd_idx) |
| 2837 | return 0; |
| 2838 | switch(conf->level) { |
| 2839 | case 4: break; |
| 2840 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2841 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2842 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2843 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2844 | if (i > sh->pd_idx) |
| 2845 | i--; |
| 2846 | break; |
| 2847 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2848 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2849 | if (i < sh->pd_idx) |
| 2850 | i += raid_disks; |
| 2851 | i -= (sh->pd_idx + 1); |
| 2852 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2853 | case ALGORITHM_PARITY_0: |
| 2854 | i -= 1; |
| 2855 | break; |
| 2856 | case ALGORITHM_PARITY_N: |
| 2857 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2858 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2859 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2860 | } |
| 2861 | break; |
| 2862 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2863 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2864 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2865 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2866 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2867 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2868 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2869 | case ALGORITHM_ROTATING_N_RESTART: |
| 2870 | if (sh->pd_idx == raid_disks-1) |
| 2871 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2872 | else if (i > sh->pd_idx) |
| 2873 | i -= 2; /* D D P Q D */ |
| 2874 | break; |
| 2875 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2876 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2877 | if (sh->pd_idx == raid_disks-1) |
| 2878 | i--; /* Q D D D P */ |
| 2879 | else { |
| 2880 | /* D D P Q D */ |
| 2881 | if (i < sh->pd_idx) |
| 2882 | i += raid_disks; |
| 2883 | i -= (sh->pd_idx + 2); |
| 2884 | } |
| 2885 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2886 | case ALGORITHM_PARITY_0: |
| 2887 | i -= 2; |
| 2888 | break; |
| 2889 | case ALGORITHM_PARITY_N: |
| 2890 | break; |
| 2891 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2892 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2893 | if (sh->pd_idx == 0) |
| 2894 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2895 | else { |
| 2896 | /* D D Q P D */ |
| 2897 | if (i < sh->pd_idx) |
| 2898 | i += raid_disks; |
| 2899 | i -= (sh->pd_idx + 1); |
| 2900 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2901 | break; |
| 2902 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2903 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2904 | if (i > sh->pd_idx) |
| 2905 | i--; |
| 2906 | break; |
| 2907 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2908 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2909 | if (i < sh->pd_idx) |
| 2910 | i += data_disks + 1; |
| 2911 | i -= (sh->pd_idx + 1); |
| 2912 | break; |
| 2913 | case ALGORITHM_PARITY_0_6: |
| 2914 | i -= 1; |
| 2915 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2916 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2917 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2918 | } |
| 2919 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2920 | } |
| 2921 | |
| 2922 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2923 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2924 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2925 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2926 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2927 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2928 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2929 | pr_warn("md/raid:%s: compute_blocknr: map not correct\n", |
| 2930 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2931 | return 0; |
| 2932 | } |
| 2933 | return r_sector; |
| 2934 | } |
| 2935 | |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 2936 | /* |
| 2937 | * There are cases where we want handle_stripe_dirtying() and |
| 2938 | * schedule_reconstruction() to delay towrite to some dev of a stripe. |
| 2939 | * |
| 2940 | * This function checks whether we want to delay the towrite. Specifically, |
| 2941 | * we delay the towrite when: |
| 2942 | * |
| 2943 | * 1. degraded stripe has a non-overwrite to the missing dev, AND this |
| 2944 | * stripe has data in journal (for other devices). |
| 2945 | * |
| 2946 | * In this case, when reading data for the non-overwrite dev, it is |
| 2947 | * necessary to handle complex rmw of write back cache (prexor with |
| 2948 | * orig_page, and xor with page). To keep read path simple, we would |
| 2949 | * like to flush data in journal to RAID disks first, so complex rmw |
| 2950 | * is handled in the write patch (handle_stripe_dirtying). |
| 2951 | * |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 2952 | * 2. when journal space is critical (R5C_LOG_CRITICAL=1) |
| 2953 | * |
| 2954 | * It is important to be able to flush all stripes in raid5-cache. |
| 2955 | * Therefore, we need reserve some space on the journal device for |
| 2956 | * these flushes. If flush operation includes pending writes to the |
| 2957 | * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe |
| 2958 | * for the flush out. If we exclude these pending writes from flush |
| 2959 | * operation, we only need (conf->max_degraded + 1) pages per stripe. |
| 2960 | * Therefore, excluding pending writes in these cases enables more |
| 2961 | * efficient use of the journal device. |
| 2962 | * |
| 2963 | * Note: To make sure the stripe makes progress, we only delay |
| 2964 | * towrite for stripes with data already in journal (injournal > 0). |
| 2965 | * When LOG_CRITICAL, stripes with injournal == 0 will be sent to |
| 2966 | * no_space_stripes list. |
| 2967 | * |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 2968 | */ |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 2969 | static inline bool delay_towrite(struct r5conf *conf, |
| 2970 | struct r5dev *dev, |
| 2971 | struct stripe_head_state *s) |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 2972 | { |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 2973 | /* case 1 above */ |
| 2974 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 2975 | !test_bit(R5_Insync, &dev->flags) && s->injournal) |
| 2976 | return true; |
| 2977 | /* case 2 above */ |
| 2978 | if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) && |
| 2979 | s->injournal > 0) |
| 2980 | return true; |
| 2981 | return false; |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 2982 | } |
| 2983 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2984 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2985 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2986 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2987 | { |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2988 | 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] | 2989 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2990 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2991 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2992 | if (rcw) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2993 | /* |
| 2994 | * In some cases, handle_stripe_dirtying initially decided to |
| 2995 | * run rmw and allocates extra page for prexor. However, rcw is |
| 2996 | * cheaper later on. We need to free the extra page now, |
| 2997 | * because we won't be able to do that in ops_complete_prexor(). |
| 2998 | */ |
| 2999 | r5c_release_extra_page(sh); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3000 | |
| 3001 | for (i = disks; i--; ) { |
| 3002 | struct r5dev *dev = &sh->dev[i]; |
| 3003 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3004 | if (dev->towrite && !delay_towrite(conf, dev, s)) { |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3005 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3006 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3007 | if (!expand) |
| 3008 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3009 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3010 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 3011 | set_bit(R5_LOCKED, &dev->flags); |
| 3012 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3013 | } |
| 3014 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 3015 | /* if we are not expanding this is a proper write request, and |
| 3016 | * there will be bios with new data to be drained into the |
| 3017 | * stripe cache |
| 3018 | */ |
| 3019 | if (!expand) { |
| 3020 | if (!s->locked) |
| 3021 | /* False alarm, nothing to do */ |
| 3022 | return; |
| 3023 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 3024 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 3025 | } else |
| 3026 | sh->reconstruct_state = reconstruct_state_run; |
| 3027 | |
| 3028 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
| 3029 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3030 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3031 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3032 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3033 | } else { |
| 3034 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 3035 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3036 | BUG_ON(level == 6 && |
| 3037 | (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) || |
| 3038 | test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags)))); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3039 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3040 | for (i = disks; i--; ) { |
| 3041 | struct r5dev *dev = &sh->dev[i]; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3042 | if (i == pd_idx || i == qd_idx) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3043 | continue; |
| 3044 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3045 | if (dev->towrite && |
| 3046 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 3047 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 3048 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3049 | set_bit(R5_LOCKED, &dev->flags); |
| 3050 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3051 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3052 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 3053 | set_bit(R5_LOCKED, &dev->flags); |
| 3054 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3055 | } |
| 3056 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 3057 | if (!s->locked) |
| 3058 | /* False alarm - nothing to do */ |
| 3059 | return; |
| 3060 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
| 3061 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 3062 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 3063 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3064 | } |
| 3065 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3066 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3067 | * are in flight |
| 3068 | */ |
| 3069 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 3070 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3071 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3072 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3073 | if (level == 6) { |
| 3074 | int qd_idx = sh->qd_idx; |
| 3075 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 3076 | |
| 3077 | set_bit(R5_LOCKED, &dev->flags); |
| 3078 | clear_bit(R5_UPTODATE, &dev->flags); |
| 3079 | s->locked++; |
| 3080 | } |
| 3081 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3082 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272b | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 3083 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 3084 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3085 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3086 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3087 | /* |
| 3088 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 3089 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3090 | * The bi_next chain must be in order. |
| 3091 | */ |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3092 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, |
| 3093 | int forwrite, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3094 | { |
| 3095 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3096 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3097 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3098 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3099 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3100 | (unsigned long long)bi->bi_iter.bi_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3101 | (unsigned long long)sh->sector); |
| 3102 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3103 | /* |
| 3104 | * If several bio share a stripe. The bio bi_phys_segments acts as a |
| 3105 | * reference count to avoid race. The reference count should already be |
| 3106 | * increased before this function is called (for example, in |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 3107 | * raid5_make_request()), so other bio sharing this stripe will not free the |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3108 | * stripe. If a stripe is owned by one stripe, the stripe lock will |
| 3109 | * protect it. |
| 3110 | */ |
| 3111 | spin_lock_irq(&sh->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3112 | /* Don't allow new IO added to stripes in batch list */ |
| 3113 | if (sh->batch_head) |
| 3114 | goto overlap; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3115 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3116 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3117 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3118 | firstwrite = 1; |
| 3119 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3120 | bip = &sh->dev[dd_idx].toread; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3121 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { |
| 3122 | if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3123 | goto overlap; |
| 3124 | bip = & (*bip)->bi_next; |
| 3125 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3126 | if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3127 | goto overlap; |
| 3128 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3129 | if (!forwrite || previous) |
| 3130 | clear_bit(STRIPE_BATCH_READY, &sh->state); |
| 3131 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 3132 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3133 | if (*bip) |
| 3134 | bi->bi_next = *bip; |
| 3135 | *bip = bi; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3136 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3137 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3138 | if (forwrite) { |
| 3139 | /* check if page is covered */ |
| 3140 | sector_t sector = sh->dev[dd_idx].sector; |
| 3141 | for (bi=sh->dev[dd_idx].towrite; |
| 3142 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3143 | bi && bi->bi_iter.bi_sector <= sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3144 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 3145 | if (bio_end_sector(bi) >= sector) |
| 3146 | sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3147 | } |
| 3148 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3149 | if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags)) |
| 3150 | sh->overwrite_disks++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3151 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3152 | |
| 3153 | 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] | 3154 | (unsigned long long)(*bip)->bi_iter.bi_sector, |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3155 | (unsigned long long)sh->sector, dd_idx); |
| 3156 | |
| 3157 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3158 | /* Cannot hold spinlock over bitmap_startwrite, |
| 3159 | * but must ensure this isn't added to a batch until |
| 3160 | * we have added to the bitmap and set bm_seq. |
| 3161 | * So set STRIPE_BITMAP_PENDING to prevent |
| 3162 | * batching. |
| 3163 | * If multiple add_stripe_bio() calls race here they |
| 3164 | * much all set STRIPE_BITMAP_PENDING. So only the first one |
| 3165 | * to complete "bitmap_startwrite" gets to set |
| 3166 | * STRIPE_BIT_DELAY. This is important as once a stripe |
| 3167 | * is added to a batch, STRIPE_BIT_DELAY cannot be changed |
| 3168 | * any more. |
| 3169 | */ |
| 3170 | set_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3171 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3172 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 3173 | STRIPE_SECTORS, 0); |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3174 | spin_lock_irq(&sh->stripe_lock); |
| 3175 | clear_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3176 | if (!sh->batch_head) { |
| 3177 | sh->bm_seq = conf->seq_flush+1; |
| 3178 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 3179 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3180 | } |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3181 | spin_unlock_irq(&sh->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3182 | |
| 3183 | if (stripe_can_batch(sh)) |
| 3184 | stripe_add_to_batch_list(conf, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3185 | return 1; |
| 3186 | |
| 3187 | overlap: |
| 3188 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3189 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3190 | return 0; |
| 3191 | } |
| 3192 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3193 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3194 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3195 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3196 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3197 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3198 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 3199 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3200 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3201 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3202 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3203 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3204 | raid5_compute_sector(conf, |
| 3205 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 3206 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3207 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3208 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3209 | } |
| 3210 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3211 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3212 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3213 | struct stripe_head_state *s, int disks, |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3214 | struct bio_list *return_bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3215 | { |
| 3216 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3217 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3218 | for (i = disks; i--; ) { |
| 3219 | struct bio *bi; |
| 3220 | int bitmap_end = 0; |
| 3221 | |
| 3222 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3223 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3224 | rcu_read_lock(); |
| 3225 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 3226 | if (rdev && test_bit(In_sync, &rdev->flags) && |
| 3227 | !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3228 | atomic_inc(&rdev->nr_pending); |
| 3229 | else |
| 3230 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3231 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3232 | if (rdev) { |
| 3233 | if (!rdev_set_badblocks( |
| 3234 | rdev, |
| 3235 | sh->sector, |
| 3236 | STRIPE_SECTORS, 0)) |
| 3237 | md_error(conf->mddev, rdev); |
| 3238 | rdev_dec_pending(rdev, conf->mddev); |
| 3239 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3240 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3241 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3242 | /* fail all writes first */ |
| 3243 | bi = sh->dev[i].towrite; |
| 3244 | sh->dev[i].towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3245 | sh->overwrite_disks = 0; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3246 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 1ed850f | 2012-10-11 13:50:13 +1100 | [diff] [blame] | 3247 | if (bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3248 | bitmap_end = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3249 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3250 | r5l_stripe_write_finished(sh); |
| 3251 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3252 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3253 | wake_up(&conf->wait_for_overlap); |
| 3254 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3255 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3256 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3257 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3258 | |
| 3259 | bi->bi_error = -EIO; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3260 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3261 | md_write_end(conf->mddev); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3262 | bio_list_add(return_bi, bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3263 | } |
| 3264 | bi = nextbi; |
| 3265 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3266 | if (bitmap_end) |
| 3267 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3268 | STRIPE_SECTORS, 0, 0); |
| 3269 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3270 | /* and fail all 'written' */ |
| 3271 | bi = sh->dev[i].written; |
| 3272 | sh->dev[i].written = NULL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3273 | if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { |
| 3274 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 3275 | sh->dev[i].page = sh->dev[i].orig_page; |
| 3276 | } |
| 3277 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3278 | if (bi) bitmap_end = 1; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3279 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3280 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3281 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3282 | |
| 3283 | bi->bi_error = -EIO; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3284 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3285 | md_write_end(conf->mddev); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3286 | bio_list_add(return_bi, bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3287 | } |
| 3288 | bi = bi2; |
| 3289 | } |
| 3290 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3291 | /* fail any reads if this device is non-operational and |
| 3292 | * the data has not reached the cache yet. |
| 3293 | */ |
| 3294 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 3295 | s->failed > conf->max_degraded && |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3296 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 3297 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3298 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3299 | bi = sh->dev[i].toread; |
| 3300 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3301 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3302 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3303 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3304 | if (bi) |
| 3305 | s->to_read--; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3306 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3307 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3308 | struct bio *nextbi = |
| 3309 | r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3310 | |
| 3311 | bi->bi_error = -EIO; |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3312 | if (!raid5_dec_bi_active_stripes(bi)) |
| 3313 | bio_list_add(return_bi, bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3314 | bi = nextbi; |
| 3315 | } |
| 3316 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3317 | if (bitmap_end) |
| 3318 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3319 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3320 | /* If we were in the middle of a write the parity block might |
| 3321 | * still be locked - so just clear all R5_LOCKED flags |
| 3322 | */ |
| 3323 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3324 | } |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3325 | s->to_write = 0; |
| 3326 | s->written = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3327 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3328 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3329 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3330 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3331 | } |
| 3332 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3333 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3334 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3335 | struct stripe_head_state *s) |
| 3336 | { |
| 3337 | int abort = 0; |
| 3338 | int i; |
| 3339 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3340 | BUG_ON(sh->batch_head); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3341 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3342 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 3343 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3344 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3345 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3346 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3347 | * Don't even need to abort as that is handled elsewhere |
| 3348 | * if needed, and not always wanted e.g. if there is a known |
| 3349 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3350 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3351 | * non-sync devices, or abort the recovery |
| 3352 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3353 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 3354 | /* During recovery devices cannot be removed, so |
| 3355 | * locking and refcounting of rdevs is not needed |
| 3356 | */ |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3357 | rcu_read_lock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3358 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3359 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3360 | if (rdev |
| 3361 | && !test_bit(Faulty, &rdev->flags) |
| 3362 | && !test_bit(In_sync, &rdev->flags) |
| 3363 | && !rdev_set_badblocks(rdev, sh->sector, |
| 3364 | STRIPE_SECTORS, 0)) |
| 3365 | abort = 1; |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3366 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3367 | if (rdev |
| 3368 | && !test_bit(Faulty, &rdev->flags) |
| 3369 | && !test_bit(In_sync, &rdev->flags) |
| 3370 | && !rdev_set_badblocks(rdev, sh->sector, |
| 3371 | STRIPE_SECTORS, 0)) |
| 3372 | abort = 1; |
| 3373 | } |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3374 | rcu_read_unlock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3375 | if (abort) |
| 3376 | conf->recovery_disabled = |
| 3377 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3378 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3379 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3380 | } |
| 3381 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3382 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 3383 | { |
| 3384 | struct md_rdev *rdev; |
| 3385 | int rv = 0; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3386 | |
| 3387 | rcu_read_lock(); |
| 3388 | rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3389 | if (rdev |
| 3390 | && !test_bit(Faulty, &rdev->flags) |
| 3391 | && !test_bit(In_sync, &rdev->flags) |
| 3392 | && (rdev->recovery_offset <= sh->sector |
| 3393 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 3394 | rv = 1; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3395 | rcu_read_unlock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3396 | return rv; |
| 3397 | } |
| 3398 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3399 | static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3400 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3401 | { |
| 3402 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3403 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 3404 | &sh->dev[s->failed_num[1]] }; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3405 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3406 | |
NeilBrown | a79cfe1 | 2015-02-02 11:37:59 +1100 | [diff] [blame] | 3407 | |
| 3408 | if (test_bit(R5_LOCKED, &dev->flags) || |
| 3409 | test_bit(R5_UPTODATE, &dev->flags)) |
| 3410 | /* No point reading this as we already have it or have |
| 3411 | * decided to get it. |
| 3412 | */ |
| 3413 | return 0; |
| 3414 | |
| 3415 | if (dev->toread || |
| 3416 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags))) |
| 3417 | /* We need this block to directly satisfy a request */ |
| 3418 | return 1; |
| 3419 | |
| 3420 | if (s->syncing || s->expanding || |
| 3421 | (s->replacing && want_replace(sh, disk_idx))) |
| 3422 | /* When syncing, or expanding we read everything. |
| 3423 | * When replacing, we need the replaced block. |
| 3424 | */ |
| 3425 | return 1; |
| 3426 | |
| 3427 | if ((s->failed >= 1 && fdev[0]->toread) || |
| 3428 | (s->failed >= 2 && fdev[1]->toread)) |
| 3429 | /* If we want to read from a failed device, then |
| 3430 | * we need to actually read every other device. |
| 3431 | */ |
| 3432 | return 1; |
| 3433 | |
NeilBrown | a9d5695 | 2015-02-02 11:49:10 +1100 | [diff] [blame] | 3434 | /* Sometimes neither read-modify-write nor reconstruct-write |
| 3435 | * cycles can work. In those cases we read every block we |
| 3436 | * can. Then the parity-update is certain to have enough to |
| 3437 | * work with. |
| 3438 | * This can only be a problem when we need to write something, |
| 3439 | * and some device has failed. If either of those tests |
| 3440 | * fail we need look no further. |
| 3441 | */ |
| 3442 | if (!s->failed || !s->to_write) |
| 3443 | return 0; |
| 3444 | |
| 3445 | if (test_bit(R5_Insync, &dev->flags) && |
| 3446 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3447 | /* Pre-reads at not permitted until after short delay |
| 3448 | * to gather multiple requests. However if this |
| 3449 | * device is no Insync, the block could only be be computed |
| 3450 | * and there is no need to delay that. |
| 3451 | */ |
| 3452 | return 0; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3453 | |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3454 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3455 | if (fdev[i]->towrite && |
| 3456 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
| 3457 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3458 | /* If we have a partial write to a failed |
| 3459 | * device, then we will need to reconstruct |
| 3460 | * the content of that device, so all other |
| 3461 | * devices must be read. |
| 3462 | */ |
| 3463 | return 1; |
| 3464 | } |
| 3465 | |
| 3466 | /* If we are forced to do a reconstruct-write, either because |
| 3467 | * the current RAID6 implementation only supports that, or |
| 3468 | * or because parity cannot be trusted and we are currently |
| 3469 | * recovering it, there is extra need to be careful. |
| 3470 | * If one of the devices that we would need to read, because |
| 3471 | * it is not being overwritten (and maybe not written at all) |
| 3472 | * is missing/faulty, then we need to read everything we can. |
| 3473 | */ |
| 3474 | if (sh->raid_conf->level != 6 && |
| 3475 | sh->sector < sh->raid_conf->mddev->recovery_cp) |
| 3476 | /* reconstruct-write isn't being forced */ |
| 3477 | return 0; |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3478 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | 10d82c5 | 2015-05-08 18:19:33 +1000 | [diff] [blame] | 3479 | if (s->failed_num[i] != sh->pd_idx && |
| 3480 | s->failed_num[i] != sh->qd_idx && |
| 3481 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3482 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3483 | return 1; |
| 3484 | } |
| 3485 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3486 | return 0; |
| 3487 | } |
| 3488 | |
Song Liu | ba02684 | 2017-01-12 17:22:42 -0800 | [diff] [blame] | 3489 | /* fetch_block - checks the given member device to see if its data needs |
| 3490 | * to be read or computed to satisfy a request. |
| 3491 | * |
| 3492 | * Returns 1 when no more member devices need to be checked, otherwise returns |
| 3493 | * 0 to tell the loop in handle_stripe_fill to continue |
| 3494 | */ |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3495 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3496 | int disk_idx, int disks) |
| 3497 | { |
| 3498 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 3499 | |
| 3500 | /* is the data in this block needed, and can we get it? */ |
| 3501 | if (need_this_block(sh, s, disk_idx, disks)) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3502 | /* we would like to get this block, possibly by computing it, |
| 3503 | * otherwise read it if the backing disk is insync |
| 3504 | */ |
| 3505 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 3506 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
NeilBrown | b0c783b | 2015-05-08 18:19:32 +1000 | [diff] [blame] | 3507 | BUG_ON(sh->batch_head); |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3508 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3509 | (s->failed && (disk_idx == s->failed_num[0] || |
| 3510 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3511 | /* have disk failed, and we're requested to fetch it; |
| 3512 | * do compute it |
| 3513 | */ |
| 3514 | pr_debug("Computing stripe %llu block %d\n", |
| 3515 | (unsigned long long)sh->sector, disk_idx); |
| 3516 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3517 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3518 | set_bit(R5_Wantcompute, &dev->flags); |
| 3519 | sh->ops.target = disk_idx; |
| 3520 | sh->ops.target2 = -1; /* no 2nd target */ |
| 3521 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3522 | /* Careful: from this point on 'uptodate' is in the eye |
| 3523 | * of raid_run_ops which services 'compute' operations |
| 3524 | * before writes. R5_Wantcompute flags a block that will |
| 3525 | * be R5_UPTODATE by the time it is needed for a |
| 3526 | * subsequent operation. |
| 3527 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3528 | s->uptodate++; |
| 3529 | return 1; |
| 3530 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 3531 | /* Computing 2-failure is *very* expensive; only |
| 3532 | * do it if failed >= 2 |
| 3533 | */ |
| 3534 | int other; |
| 3535 | for (other = disks; other--; ) { |
| 3536 | if (other == disk_idx) |
| 3537 | continue; |
| 3538 | if (!test_bit(R5_UPTODATE, |
| 3539 | &sh->dev[other].flags)) |
| 3540 | break; |
| 3541 | } |
| 3542 | BUG_ON(other < 0); |
| 3543 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 3544 | (unsigned long long)sh->sector, |
| 3545 | disk_idx, other); |
| 3546 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3547 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3548 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 3549 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 3550 | sh->ops.target = disk_idx; |
| 3551 | sh->ops.target2 = other; |
| 3552 | s->uptodate += 2; |
| 3553 | s->req_compute = 1; |
| 3554 | return 1; |
| 3555 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 3556 | set_bit(R5_LOCKED, &dev->flags); |
| 3557 | set_bit(R5_Wantread, &dev->flags); |
| 3558 | s->locked++; |
| 3559 | pr_debug("Reading block %d (sync=%d)\n", |
| 3560 | disk_idx, s->syncing); |
| 3561 | } |
| 3562 | } |
| 3563 | |
| 3564 | return 0; |
| 3565 | } |
| 3566 | |
| 3567 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3568 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3569 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3570 | static void handle_stripe_fill(struct stripe_head *sh, |
| 3571 | struct stripe_head_state *s, |
| 3572 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3573 | { |
| 3574 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3575 | |
| 3576 | /* look for blocks to read/compute, skip this if a compute |
| 3577 | * is already in flight, or if the stripe contents are in the |
| 3578 | * midst of changing due to a write |
| 3579 | */ |
| 3580 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3581 | !sh->reconstruct_state) { |
| 3582 | |
| 3583 | /* |
| 3584 | * For degraded stripe with data in journal, do not handle |
| 3585 | * read requests yet, instead, flush the stripe to raid |
| 3586 | * disks first, this avoids handling complex rmw of write |
| 3587 | * back cache (prexor with orig_page, and then xor with |
| 3588 | * page) in the read path |
| 3589 | */ |
| 3590 | if (s->injournal && s->failed) { |
| 3591 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
| 3592 | r5c_make_stripe_write_out(sh); |
| 3593 | goto out; |
| 3594 | } |
| 3595 | |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3596 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3597 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3598 | break; |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3599 | } |
| 3600 | out: |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3601 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3602 | } |
| 3603 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 3604 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 3605 | unsigned long handle_flags); |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3606 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3607 | * any written block on an uptodate or failed drive can be returned. |
| 3608 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 3609 | * never LOCKED, so we don't need to test 'failed' directly. |
| 3610 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3611 | static void handle_stripe_clean_event(struct r5conf *conf, |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3612 | struct stripe_head *sh, int disks, struct bio_list *return_bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3613 | { |
| 3614 | int i; |
| 3615 | struct r5dev *dev; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3616 | int discard_pending = 0; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3617 | struct stripe_head *head_sh = sh; |
| 3618 | bool do_endio = false; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3619 | |
| 3620 | for (i = disks; i--; ) |
| 3621 | if (sh->dev[i].written) { |
| 3622 | dev = &sh->dev[i]; |
| 3623 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3624 | (test_bit(R5_UPTODATE, &dev->flags) || |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3625 | test_bit(R5_Discard, &dev->flags) || |
| 3626 | test_bit(R5_SkipCopy, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3627 | /* We can return any write requests */ |
| 3628 | struct bio *wbi, *wbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3629 | pr_debug("Return write for disc %d\n", i); |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 3630 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
| 3631 | clear_bit(R5_UPTODATE, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3632 | if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { |
| 3633 | WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3634 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3635 | do_endio = true; |
| 3636 | |
| 3637 | returnbi: |
| 3638 | dev->page = dev->orig_page; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3639 | wbi = dev->written; |
| 3640 | dev->written = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3641 | while (wbi && wbi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3642 | dev->sector + STRIPE_SECTORS) { |
| 3643 | wbi2 = r5_next_bio(wbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3644 | if (!raid5_dec_bi_active_stripes(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3645 | md_write_end(conf->mddev); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3646 | bio_list_add(return_bi, wbi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3647 | } |
| 3648 | wbi = wbi2; |
| 3649 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3650 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3651 | STRIPE_SECTORS, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3652 | !test_bit(STRIPE_DEGRADED, &sh->state), |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3653 | 0); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3654 | if (head_sh->batch_head) { |
| 3655 | sh = list_first_entry(&sh->batch_list, |
| 3656 | struct stripe_head, |
| 3657 | batch_list); |
| 3658 | if (sh != head_sh) { |
| 3659 | dev = &sh->dev[i]; |
| 3660 | goto returnbi; |
| 3661 | } |
| 3662 | } |
| 3663 | sh = head_sh; |
| 3664 | dev = &sh->dev[i]; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3665 | } else if (test_bit(R5_Discard, &dev->flags)) |
| 3666 | discard_pending = 1; |
| 3667 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 3668 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3669 | r5l_stripe_write_finished(sh); |
| 3670 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3671 | if (!discard_pending && |
| 3672 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3673 | int hash; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3674 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 3675 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
| 3676 | if (sh->qd_idx >= 0) { |
| 3677 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 3678 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); |
| 3679 | } |
| 3680 | /* now that discard is done we can proceed with any sync */ |
| 3681 | clear_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 3682 | /* |
| 3683 | * SCSI discard will change some bio fields and the stripe has |
| 3684 | * no updated data, so remove it from hash list and the stripe |
| 3685 | * will be reinitialized |
| 3686 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3687 | unhash: |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3688 | hash = sh->hash_lock_index; |
| 3689 | spin_lock_irq(conf->hash_locks + hash); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 3690 | remove_hash(sh); |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3691 | spin_unlock_irq(conf->hash_locks + hash); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3692 | if (head_sh->batch_head) { |
| 3693 | sh = list_first_entry(&sh->batch_list, |
| 3694 | struct stripe_head, batch_list); |
| 3695 | if (sh != head_sh) |
| 3696 | goto unhash; |
| 3697 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3698 | sh = head_sh; |
| 3699 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3700 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
| 3701 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3702 | |
| 3703 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3704 | |
| 3705 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3706 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3707 | md_wakeup_thread(conf->mddev->thread); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3708 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 3709 | if (head_sh->batch_head && do_endio) |
| 3710 | break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3711 | } |
| 3712 | |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 3713 | /* |
| 3714 | * For RMW in write back cache, we need extra page in prexor to store the |
| 3715 | * old data. This page is stored in dev->orig_page. |
| 3716 | * |
| 3717 | * This function checks whether we have data for prexor. The exact logic |
| 3718 | * is: |
| 3719 | * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE) |
| 3720 | */ |
| 3721 | static inline bool uptodate_for_rmw(struct r5dev *dev) |
| 3722 | { |
| 3723 | return (test_bit(R5_UPTODATE, &dev->flags)) && |
| 3724 | (!test_bit(R5_InJournal, &dev->flags) || |
| 3725 | test_bit(R5_OrigPageUPTDODATE, &dev->flags)); |
| 3726 | } |
| 3727 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3728 | static int handle_stripe_dirtying(struct r5conf *conf, |
| 3729 | struct stripe_head *sh, |
| 3730 | struct stripe_head_state *s, |
| 3731 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3732 | { |
| 3733 | int rmw = 0, rcw = 0, i; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3734 | sector_t recovery_cp = conf->mddev->recovery_cp; |
| 3735 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3736 | /* Check whether resync is now happening or should start. |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3737 | * If yes, then the array is dirty (after unclean shutdown or |
| 3738 | * initial creation), so parity in some stripes might be inconsistent. |
| 3739 | * In this case, we need to always do reconstruct-write, to ensure |
| 3740 | * that in case of drive failure or read-error correction, we |
| 3741 | * generate correct data from the parity. |
| 3742 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3743 | if (conf->rmw_level == PARITY_DISABLE_RMW || |
NeilBrown | 26ac107 | 2015-02-18 11:35:14 +1100 | [diff] [blame] | 3744 | (recovery_cp < MaxSector && sh->sector >= recovery_cp && |
| 3745 | s->failed == 0)) { |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3746 | /* Calculate the real rcw later - for now make it |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3747 | * look like rcw is cheaper |
| 3748 | */ |
| 3749 | rcw = 1; rmw = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3750 | pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n", |
| 3751 | conf->rmw_level, (unsigned long long)recovery_cp, |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3752 | (unsigned long long)sh->sector); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3753 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3754 | /* would I have to read this buffer for read_modify_write */ |
| 3755 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3756 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
Song Liu | 07e8336 | 2017-01-23 17:12:58 -0800 | [diff] [blame] | 3757 | i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3758 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3759 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 3760 | !(uptodate_for_rmw(dev) || |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3761 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3762 | if (test_bit(R5_Insync, &dev->flags)) |
| 3763 | rmw++; |
| 3764 | else |
| 3765 | rmw += 2*disks; /* cannot read it */ |
| 3766 | } |
| 3767 | /* Would I have to read this buffer for reconstruct_write */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3768 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 3769 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3770 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3771 | !(test_bit(R5_UPTODATE, &dev->flags) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3772 | test_bit(R5_Wantcompute, &dev->flags))) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3773 | if (test_bit(R5_Insync, &dev->flags)) |
| 3774 | rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3775 | else |
| 3776 | rcw += 2*disks; |
| 3777 | } |
| 3778 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3779 | |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3780 | pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n", |
| 3781 | (unsigned long long)sh->sector, sh->state, rmw, rcw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3782 | set_bit(STRIPE_HANDLE, &sh->state); |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 3783 | 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] | 3784 | /* prefer read-modify-write, but need to get some data */ |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 3785 | if (conf->mddev->queue) |
| 3786 | blk_add_trace_msg(conf->mddev->queue, |
| 3787 | "raid5 rmw %llu %d", |
| 3788 | (unsigned long long)sh->sector, rmw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3789 | for (i = disks; i--; ) { |
| 3790 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3791 | if (test_bit(R5_InJournal, &dev->flags) && |
| 3792 | dev->page == dev->orig_page && |
| 3793 | !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) { |
| 3794 | /* alloc page for prexor */ |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3795 | struct page *p = alloc_page(GFP_NOIO); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3796 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3797 | if (p) { |
| 3798 | dev->orig_page = p; |
| 3799 | continue; |
| 3800 | } |
| 3801 | |
| 3802 | /* |
| 3803 | * alloc_page() failed, try use |
| 3804 | * disk_info->extra_page |
| 3805 | */ |
| 3806 | if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE, |
| 3807 | &conf->cache_state)) { |
| 3808 | r5c_use_extra_page(sh); |
| 3809 | break; |
| 3810 | } |
| 3811 | |
| 3812 | /* extra_page in use, add to delayed_list */ |
| 3813 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3814 | s->waiting_extra_page = 1; |
| 3815 | return -EAGAIN; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3816 | } |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3817 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3818 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3819 | for (i = disks; i--; ) { |
| 3820 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 39b9958 | 2017-01-24 14:08:23 -0800 | [diff] [blame] | 3821 | if (((dev->towrite && !delay_towrite(conf, dev, s)) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3822 | i == sh->pd_idx || i == sh->qd_idx || |
| 3823 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3824 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 86aa139 | 2017-01-12 17:22:41 -0800 | [diff] [blame] | 3825 | !(uptodate_for_rmw(dev) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3826 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3827 | test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3828 | if (test_bit(STRIPE_PREREAD_ACTIVE, |
| 3829 | &sh->state)) { |
| 3830 | pr_debug("Read_old block %d for r-m-w\n", |
| 3831 | i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3832 | set_bit(R5_LOCKED, &dev->flags); |
| 3833 | set_bit(R5_Wantread, &dev->flags); |
| 3834 | s->locked++; |
| 3835 | } else { |
| 3836 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3837 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3838 | } |
| 3839 | } |
| 3840 | } |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3841 | } |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 3842 | 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] | 3843 | /* want reconstruct write, but need to get some data */ |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3844 | int qread =0; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3845 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3846 | for (i = disks; i--; ) { |
| 3847 | struct r5dev *dev = &sh->dev[i]; |
| 3848 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3849 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3850 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3851 | !(test_bit(R5_UPTODATE, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3852 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 3853 | rcw++; |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3854 | if (test_bit(R5_Insync, &dev->flags) && |
| 3855 | test_bit(STRIPE_PREREAD_ACTIVE, |
| 3856 | &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3857 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3858 | "%d for Reconstruct\n", i); |
| 3859 | set_bit(R5_LOCKED, &dev->flags); |
| 3860 | set_bit(R5_Wantread, &dev->flags); |
| 3861 | s->locked++; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3862 | qread++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3863 | } else { |
| 3864 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3865 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3866 | } |
| 3867 | } |
| 3868 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 3869 | if (rcw && conf->mddev->queue) |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3870 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
| 3871 | (unsigned long long)sh->sector, |
| 3872 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3873 | } |
NeilBrown | b1b02fe | 2015-02-02 10:44:29 +1100 | [diff] [blame] | 3874 | |
| 3875 | if (rcw > disks && rmw > disks && |
| 3876 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3877 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3878 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3879 | /* now if nothing is locked, and if we have enough data, |
| 3880 | * we can start a write request |
| 3881 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3882 | /* since handle_stripe can be called at any time we need to handle the |
| 3883 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3884 | * subsequent call wants to start a write request. raid_run_ops only |
| 3885 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3886 | * simultaneously. If this is not the case then new writes need to be |
| 3887 | * held off until the compute completes. |
| 3888 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3889 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 3890 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3891 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3892 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 3893 | return 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3894 | } |
| 3895 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3896 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3897 | struct stripe_head_state *s, int disks) |
| 3898 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3899 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3900 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3901 | BUG_ON(sh->batch_head); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3902 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3903 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3904 | switch (sh->check_state) { |
| 3905 | case check_state_idle: |
| 3906 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3907 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3908 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3909 | sh->check_state = check_state_run; |
| 3910 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3911 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3912 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3913 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3914 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3915 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3916 | /* fall through */ |
| 3917 | case check_state_compute_result: |
| 3918 | sh->check_state = check_state_idle; |
| 3919 | if (!dev) |
| 3920 | dev = &sh->dev[sh->pd_idx]; |
| 3921 | |
| 3922 | /* check that a write has not made the stripe insync */ |
| 3923 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3924 | break; |
| 3925 | |
| 3926 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3927 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 3928 | BUG_ON(s->uptodate != disks); |
| 3929 | |
| 3930 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3931 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3932 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3933 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3934 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3935 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3936 | break; |
| 3937 | case check_state_run: |
| 3938 | break; /* we will be called again upon completion */ |
| 3939 | case check_state_check_result: |
| 3940 | sh->check_state = check_state_idle; |
| 3941 | |
| 3942 | /* if a failure occurred during the check operation, leave |
| 3943 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 3944 | */ |
| 3945 | if (s->failed) |
| 3946 | break; |
| 3947 | |
| 3948 | /* handle a successful check operation, if parity is correct |
| 3949 | * we are done. Otherwise update the mismatch count and repair |
| 3950 | * parity if !MD_RECOVERY_CHECK |
| 3951 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 3952 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3953 | /* parity is correct (on disc, |
| 3954 | * not in buffer any more) |
| 3955 | */ |
| 3956 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3957 | else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 3958 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3959 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3960 | /* don't try to repair!! */ |
| 3961 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3962 | else { |
| 3963 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3964 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3965 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3966 | set_bit(R5_Wantcompute, |
| 3967 | &sh->dev[sh->pd_idx].flags); |
| 3968 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3969 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3970 | s->uptodate++; |
| 3971 | } |
| 3972 | } |
| 3973 | break; |
| 3974 | case check_state_compute_run: |
| 3975 | break; |
| 3976 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3977 | pr_err("%s: unknown check_state: %d sector: %llu\n", |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3978 | __func__, sh->check_state, |
| 3979 | (unsigned long long) sh->sector); |
| 3980 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3981 | } |
| 3982 | } |
| 3983 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3984 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3985 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3986 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3987 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3988 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3989 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3990 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3991 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3992 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3993 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3994 | |
| 3995 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3996 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3997 | /* Want to check and possibly repair P and Q. |
| 3998 | * However there could be one 'failed' device, in which |
| 3999 | * case we can only check one of them, possibly using the |
| 4000 | * other to generate missing data |
| 4001 | */ |
| 4002 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4003 | switch (sh->check_state) { |
| 4004 | case check_state_idle: |
| 4005 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4006 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4007 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4008 | * makes sense to check P (If anything else were failed, |
| 4009 | * we would have used P to recreate it). |
| 4010 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4011 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4012 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4013 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4014 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4015 | * anything, so it makes sense to check it |
| 4016 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4017 | if (sh->check_state == check_state_run) |
| 4018 | sh->check_state = check_state_run_pq; |
| 4019 | else |
| 4020 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4021 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4022 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4023 | /* discard potentially stale zero_sum_result */ |
| 4024 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4025 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4026 | if (sh->check_state == check_state_run) { |
| 4027 | /* async_xor_zero_sum destroys the contents of P */ |
| 4028 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 4029 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4030 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4031 | if (sh->check_state >= check_state_run && |
| 4032 | sh->check_state <= check_state_run_pq) { |
| 4033 | /* async_syndrome_zero_sum preserves P and Q, so |
| 4034 | * no need to mark them !uptodate here |
| 4035 | */ |
| 4036 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 4037 | break; |
| 4038 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4039 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4040 | /* we have 2-disk failure */ |
| 4041 | BUG_ON(s->failed != 2); |
| 4042 | /* fall through */ |
| 4043 | case check_state_compute_result: |
| 4044 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 4045 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4046 | /* check that a write has not made the stripe insync */ |
| 4047 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 4048 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4049 | |
| 4050 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4051 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4052 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4053 | BUG_ON(s->uptodate < disks - 1); /* We don't need Q to recover */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4054 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4055 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4056 | s->locked++; |
| 4057 | set_bit(R5_LOCKED, &dev->flags); |
| 4058 | set_bit(R5_Wantwrite, &dev->flags); |
| 4059 | } |
| 4060 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 4061 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4062 | s->locked++; |
| 4063 | set_bit(R5_LOCKED, &dev->flags); |
| 4064 | set_bit(R5_Wantwrite, &dev->flags); |
| 4065 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4066 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4067 | dev = &sh->dev[pd_idx]; |
| 4068 | s->locked++; |
| 4069 | set_bit(R5_LOCKED, &dev->flags); |
| 4070 | set_bit(R5_Wantwrite, &dev->flags); |
| 4071 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4072 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4073 | dev = &sh->dev[qd_idx]; |
| 4074 | s->locked++; |
| 4075 | set_bit(R5_LOCKED, &dev->flags); |
| 4076 | set_bit(R5_Wantwrite, &dev->flags); |
| 4077 | } |
| 4078 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 4079 | |
| 4080 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4081 | break; |
| 4082 | case check_state_run: |
| 4083 | case check_state_run_q: |
| 4084 | case check_state_run_pq: |
| 4085 | break; /* we will be called again upon completion */ |
| 4086 | case check_state_check_result: |
| 4087 | sh->check_state = check_state_idle; |
| 4088 | |
| 4089 | /* handle a successful check operation, if parity is correct |
| 4090 | * we are done. Otherwise update the mismatch count and repair |
| 4091 | * parity if !MD_RECOVERY_CHECK |
| 4092 | */ |
| 4093 | if (sh->ops.zero_sum_result == 0) { |
| 4094 | /* both parities are correct */ |
| 4095 | if (!s->failed) |
| 4096 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4097 | else { |
| 4098 | /* in contrast to the raid5 case we can validate |
| 4099 | * parity, but still have a failure to write |
| 4100 | * back |
| 4101 | */ |
| 4102 | sh->check_state = check_state_compute_result; |
| 4103 | /* Returning at this point means that we may go |
| 4104 | * off and bring p and/or q uptodate again so |
| 4105 | * we make sure to check zero_sum_result again |
| 4106 | * to verify if p or q need writeback |
| 4107 | */ |
| 4108 | } |
| 4109 | } else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 4110 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4111 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 4112 | /* don't try to repair!! */ |
| 4113 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4114 | else { |
| 4115 | int *target = &sh->ops.target; |
| 4116 | |
| 4117 | sh->ops.target = -1; |
| 4118 | sh->ops.target2 = -1; |
| 4119 | sh->check_state = check_state_compute_run; |
| 4120 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 4121 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 4122 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 4123 | set_bit(R5_Wantcompute, |
| 4124 | &sh->dev[pd_idx].flags); |
| 4125 | *target = pd_idx; |
| 4126 | target = &sh->ops.target2; |
| 4127 | s->uptodate++; |
| 4128 | } |
| 4129 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 4130 | set_bit(R5_Wantcompute, |
| 4131 | &sh->dev[qd_idx].flags); |
| 4132 | *target = qd_idx; |
| 4133 | s->uptodate++; |
| 4134 | } |
| 4135 | } |
| 4136 | } |
| 4137 | break; |
| 4138 | case check_state_compute_run: |
| 4139 | break; |
| 4140 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 4141 | pr_warn("%s: unknown check_state: %d sector: %llu\n", |
| 4142 | __func__, sh->check_state, |
| 4143 | (unsigned long long) sh->sector); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 4144 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4145 | } |
| 4146 | } |
| 4147 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4148 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4149 | { |
| 4150 | int i; |
| 4151 | |
| 4152 | /* We have read all the blocks in this stripe and now we need to |
| 4153 | * copy some of them into a target stripe for expand. |
| 4154 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4155 | struct dma_async_tx_descriptor *tx = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4156 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4157 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 4158 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 4159 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4160 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4161 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4162 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4163 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4164 | sector_t bn = raid5_compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4165 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 4166 | &dd_idx, NULL); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4167 | sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4168 | if (sh2 == NULL) |
| 4169 | /* so far only the early blocks of this stripe |
| 4170 | * have been requested. When later blocks |
| 4171 | * get requested, we will try again |
| 4172 | */ |
| 4173 | continue; |
| 4174 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 4175 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 4176 | /* must have already done this block */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4177 | raid5_release_stripe(sh2); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4178 | continue; |
| 4179 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4180 | |
| 4181 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4182 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4183 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 4184 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4185 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4186 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4187 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 4188 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 4189 | for (j = 0; j < conf->raid_disks; j++) |
| 4190 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4191 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4192 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 4193 | break; |
| 4194 | if (j == conf->raid_disks) { |
| 4195 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 4196 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 4197 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4198 | raid5_release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4199 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4200 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 4201 | /* done submitting copies, wait for them to complete */ |
NeilBrown | 749586b | 2012-11-20 14:11:15 +1100 | [diff] [blame] | 4202 | async_tx_quiesce(&tx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4203 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4204 | |
| 4205 | /* |
| 4206 | * handle_stripe - do things to a stripe. |
| 4207 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4208 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 4209 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4210 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4211 | * return some read requests which now have data |
| 4212 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4213 | * schedule a read on some buffers |
| 4214 | * schedule a write of some buffers |
| 4215 | * return confirmation of parity correctness |
| 4216 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4217 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4218 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4219 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4220 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4221 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4222 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4223 | struct r5dev *dev; |
| 4224 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4225 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4226 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4227 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4228 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4229 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head; |
| 4230 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4231 | s->failed_num[0] = -1; |
| 4232 | s->failed_num[1] = -1; |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 4233 | s->log_failed = r5l_log_disk_error(conf); |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4234 | |
| 4235 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4236 | rcu_read_lock(); |
| 4237 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4238 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4239 | sector_t first_bad; |
| 4240 | int bad_sectors; |
| 4241 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4242 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4243 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4244 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4245 | 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] | 4246 | i, dev->flags, |
| 4247 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4248 | /* maybe we can reply to a read |
| 4249 | * |
| 4250 | * new wantfill requests are only permitted while |
| 4251 | * ops_complete_biofill is guaranteed to be inactive |
| 4252 | */ |
| 4253 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 4254 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 4255 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4256 | |
| 4257 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4258 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 4259 | s->locked++; |
| 4260 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 4261 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4262 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4263 | s->compute++; |
| 4264 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4265 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4266 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4267 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4268 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4269 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4270 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4271 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4272 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4273 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4274 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4275 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4276 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4277 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4278 | /* Prefer to use the replacement for reads, but only |
| 4279 | * if it is recovered enough and has no bad blocks. |
| 4280 | */ |
| 4281 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 4282 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 4283 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 4284 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 4285 | &first_bad, &bad_sectors)) |
| 4286 | set_bit(R5_ReadRepl, &dev->flags); |
| 4287 | else { |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4288 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4289 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4290 | else |
| 4291 | clear_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4292 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 4293 | clear_bit(R5_ReadRepl, &dev->flags); |
| 4294 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 4295 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 4296 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4297 | if (rdev) { |
| 4298 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 4299 | &first_bad, &bad_sectors); |
| 4300 | if (s->blocked_rdev == NULL |
| 4301 | && (test_bit(Blocked, &rdev->flags) |
| 4302 | || is_bad < 0)) { |
| 4303 | if (is_bad < 0) |
| 4304 | set_bit(BlockedBadBlocks, |
| 4305 | &rdev->flags); |
| 4306 | s->blocked_rdev = rdev; |
| 4307 | atomic_inc(&rdev->nr_pending); |
| 4308 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4309 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4310 | clear_bit(R5_Insync, &dev->flags); |
| 4311 | if (!rdev) |
| 4312 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4313 | else if (is_bad) { |
| 4314 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 4315 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 4316 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4317 | /* treat as in-sync, but with a read error |
| 4318 | * which we can now try to correct |
| 4319 | */ |
| 4320 | set_bit(R5_Insync, &dev->flags); |
| 4321 | set_bit(R5_ReadError, &dev->flags); |
| 4322 | } |
| 4323 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4324 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 4325 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4326 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 4327 | set_bit(R5_Insync, &dev->flags); |
| 4328 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 4329 | test_bit(R5_Expanded, &dev->flags)) |
| 4330 | /* If we've reshaped into here, we assume it is Insync. |
| 4331 | * We will shortly update recovery_offset to make |
| 4332 | * it official. |
| 4333 | */ |
| 4334 | set_bit(R5_Insync, &dev->flags); |
| 4335 | |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4336 | if (test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4337 | /* This flag does not apply to '.replacement' |
| 4338 | * only to .rdev, so make sure to check that*/ |
| 4339 | struct md_rdev *rdev2 = rcu_dereference( |
| 4340 | conf->disks[i].rdev); |
| 4341 | if (rdev2 == rdev) |
| 4342 | clear_bit(R5_Insync, &dev->flags); |
| 4343 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4344 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4345 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4346 | } else |
| 4347 | clear_bit(R5_WriteError, &dev->flags); |
| 4348 | } |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4349 | if (test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4350 | /* This flag does not apply to '.replacement' |
| 4351 | * only to .rdev, so make sure to check that*/ |
| 4352 | struct md_rdev *rdev2 = rcu_dereference( |
| 4353 | conf->disks[i].rdev); |
| 4354 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4355 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4356 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4357 | } else |
| 4358 | clear_bit(R5_MadeGood, &dev->flags); |
| 4359 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4360 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4361 | struct md_rdev *rdev2 = rcu_dereference( |
| 4362 | conf->disks[i].replacement); |
| 4363 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 4364 | s->handle_bad_blocks = 1; |
| 4365 | atomic_inc(&rdev2->nr_pending); |
| 4366 | } else |
| 4367 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 4368 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4369 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4370 | /* The ReadError flag will just be confusing now */ |
| 4371 | clear_bit(R5_ReadError, &dev->flags); |
| 4372 | clear_bit(R5_ReWrite, &dev->flags); |
| 4373 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4374 | if (test_bit(R5_ReadError, &dev->flags)) |
| 4375 | clear_bit(R5_Insync, &dev->flags); |
| 4376 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4377 | if (s->failed < 2) |
| 4378 | s->failed_num[s->failed] = i; |
| 4379 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4380 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 4381 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4382 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4383 | |
| 4384 | if (test_bit(R5_InJournal, &dev->flags)) |
| 4385 | s->injournal++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4386 | if (test_bit(R5_InJournal, &dev->flags) && dev->written) |
| 4387 | s->just_cached++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4388 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4389 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 4390 | /* If there is a failed device being replaced, |
| 4391 | * we must be recovering. |
| 4392 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4393 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4394 | * else we can only be replacing |
| 4395 | * sync and recovery both need to read all devices, and so |
| 4396 | * use the same flag. |
| 4397 | */ |
| 4398 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4399 | sh->sector >= conf->mddev->recovery_cp || |
| 4400 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4401 | s->syncing = 1; |
| 4402 | else |
| 4403 | s->replacing = 1; |
| 4404 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4405 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4406 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4407 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4408 | static int clear_batch_ready(struct stripe_head *sh) |
| 4409 | { |
NeilBrown | b15a9db | 2015-05-22 15:20:04 +1000 | [diff] [blame] | 4410 | /* Return '1' if this is a member of batch, or |
| 4411 | * '0' if it is a lone stripe or a head which can now be |
| 4412 | * handled. |
| 4413 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4414 | struct stripe_head *tmp; |
| 4415 | if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state)) |
NeilBrown | b15a9db | 2015-05-22 15:20:04 +1000 | [diff] [blame] | 4416 | return (sh->batch_head && sh->batch_head != sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4417 | spin_lock(&sh->stripe_lock); |
| 4418 | if (!sh->batch_head) { |
| 4419 | spin_unlock(&sh->stripe_lock); |
| 4420 | return 0; |
| 4421 | } |
| 4422 | |
| 4423 | /* |
| 4424 | * this stripe could be added to a batch list before we check |
| 4425 | * BATCH_READY, skips it |
| 4426 | */ |
| 4427 | if (sh->batch_head != sh) { |
| 4428 | spin_unlock(&sh->stripe_lock); |
| 4429 | return 1; |
| 4430 | } |
| 4431 | spin_lock(&sh->batch_lock); |
| 4432 | list_for_each_entry(tmp, &sh->batch_list, batch_list) |
| 4433 | clear_bit(STRIPE_BATCH_READY, &tmp->state); |
| 4434 | spin_unlock(&sh->batch_lock); |
| 4435 | spin_unlock(&sh->stripe_lock); |
| 4436 | |
| 4437 | /* |
| 4438 | * BATCH_READY is cleared, no new stripes can be added. |
| 4439 | * batch_list can be accessed without lock |
| 4440 | */ |
| 4441 | return 0; |
| 4442 | } |
| 4443 | |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4444 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 4445 | unsigned long handle_flags) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4446 | { |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4447 | struct stripe_head *sh, *next; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4448 | int i; |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4449 | int do_wakeup = 0; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4450 | |
NeilBrown | bb27051 | 2015-05-08 18:19:40 +1000 | [diff] [blame] | 4451 | list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) { |
| 4452 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4453 | list_del_init(&sh->batch_list); |
| 4454 | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4455 | WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4456 | (1 << STRIPE_SYNCING) | |
| 4457 | (1 << STRIPE_REPLACED) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4458 | (1 << STRIPE_DELAYED) | |
| 4459 | (1 << STRIPE_BIT_DELAY) | |
| 4460 | (1 << STRIPE_FULL_WRITE) | |
| 4461 | (1 << STRIPE_BIOFILL_RUN) | |
| 4462 | (1 << STRIPE_COMPUTE_RUN) | |
| 4463 | (1 << STRIPE_OPS_REQ_PENDING) | |
| 4464 | (1 << STRIPE_DISCARD) | |
| 4465 | (1 << STRIPE_BATCH_READY) | |
| 4466 | (1 << STRIPE_BATCH_ERR) | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4467 | (1 << STRIPE_BITMAP_PENDING)), |
| 4468 | "stripe state: %lx\n", sh->state); |
| 4469 | WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) | |
| 4470 | (1 << STRIPE_REPLACED)), |
| 4471 | "head stripe state: %lx\n", head_sh->state); |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4472 | |
| 4473 | set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS | |
NeilBrown | 550da24 | 2016-03-09 12:58:25 +1100 | [diff] [blame] | 4474 | (1 << STRIPE_PREREAD_ACTIVE) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4475 | (1 << STRIPE_DEGRADED)), |
| 4476 | head_sh->state & (1 << STRIPE_INSYNC)); |
| 4477 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4478 | sh->check_state = head_sh->check_state; |
| 4479 | sh->reconstruct_state = head_sh->reconstruct_state; |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4480 | for (i = 0; i < sh->disks; i++) { |
| 4481 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 4482 | do_wakeup = 1; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4483 | sh->dev[i].flags = head_sh->dev[i].flags & |
| 4484 | (~((1 << R5_WriteError) | (1 << R5_Overlap))); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4485 | } |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4486 | spin_lock_irq(&sh->stripe_lock); |
| 4487 | sh->batch_head = NULL; |
| 4488 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4489 | if (handle_flags == 0 || |
| 4490 | sh->state & handle_flags) |
| 4491 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4492 | raid5_release_stripe(sh); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4493 | } |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4494 | spin_lock_irq(&head_sh->stripe_lock); |
| 4495 | head_sh->batch_head = NULL; |
| 4496 | spin_unlock_irq(&head_sh->stripe_lock); |
| 4497 | for (i = 0; i < head_sh->disks; i++) |
| 4498 | if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags)) |
| 4499 | do_wakeup = 1; |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4500 | if (head_sh->state & handle_flags) |
| 4501 | set_bit(STRIPE_HANDLE, &head_sh->state); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4502 | |
| 4503 | if (do_wakeup) |
| 4504 | wake_up(&head_sh->raid_conf->wait_for_overlap); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4505 | } |
| 4506 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4507 | static void handle_stripe(struct stripe_head *sh) |
| 4508 | { |
| 4509 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4510 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4511 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4512 | int prexor; |
| 4513 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4514 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4515 | |
| 4516 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 4517 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4518 | /* already being handled, ensure it gets handled |
| 4519 | * again when current action finishes */ |
| 4520 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4521 | return; |
| 4522 | } |
| 4523 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4524 | if (clear_batch_ready(sh) ) { |
| 4525 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
| 4526 | return; |
| 4527 | } |
| 4528 | |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4529 | if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state)) |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4530 | break_stripe_batch_list(sh, 0); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4531 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4532 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) { |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4533 | spin_lock(&sh->stripe_lock); |
| 4534 | /* Cannot process 'sync' concurrently with 'discard' */ |
| 4535 | if (!test_bit(STRIPE_DISCARD, &sh->state) && |
| 4536 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 4537 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4538 | clear_bit(STRIPE_INSYNC, &sh->state); |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4539 | clear_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4540 | } |
| 4541 | spin_unlock(&sh->stripe_lock); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4542 | } |
| 4543 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4544 | |
| 4545 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 4546 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 4547 | (unsigned long long)sh->sector, sh->state, |
| 4548 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 4549 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4550 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4551 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4552 | |
Shaohua Li | b70abcb | 2015-08-13 14:31:58 -0700 | [diff] [blame] | 4553 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) |
| 4554 | goto finish; |
| 4555 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4556 | if (s.handle_bad_blocks) { |
| 4557 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4558 | goto finish; |
| 4559 | } |
| 4560 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4561 | if (unlikely(s.blocked_rdev)) { |
| 4562 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4563 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4564 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4565 | goto finish; |
| 4566 | } |
| 4567 | /* There is nothing for the blocked_rdev to block */ |
| 4568 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 4569 | s.blocked_rdev = NULL; |
| 4570 | } |
| 4571 | |
| 4572 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 4573 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 4574 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 4575 | } |
| 4576 | |
| 4577 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 4578 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 4579 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 4580 | s.failed_num[0], s.failed_num[1]); |
| 4581 | /* check if the array has lost more than max_degraded devices and, |
| 4582 | * if so, some requests might need to be failed. |
| 4583 | */ |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 4584 | if (s.failed > conf->max_degraded || s.log_failed) { |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4585 | sh->check_state = 0; |
| 4586 | sh->reconstruct_state = 0; |
NeilBrown | 626f209 | 2015-05-22 14:03:10 +1000 | [diff] [blame] | 4587 | break_stripe_batch_list(sh, 0); |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4588 | if (s.to_read+s.to_write+s.written) |
| 4589 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4590 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4591 | handle_failed_sync(conf, sh, &s); |
| 4592 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4593 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4594 | /* Now we check to see if any write operations have recently |
| 4595 | * completed |
| 4596 | */ |
| 4597 | prexor = 0; |
| 4598 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 4599 | prexor = 1; |
| 4600 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 4601 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 4602 | sh->reconstruct_state = reconstruct_state_idle; |
| 4603 | |
| 4604 | /* All the 'written' buffers and the parity block are ready to |
| 4605 | * be written back to disk |
| 4606 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4607 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 4608 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4609 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4610 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 4611 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4612 | for (i = disks; i--; ) { |
| 4613 | struct r5dev *dev = &sh->dev[i]; |
| 4614 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 4615 | (i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4616 | dev->written || test_bit(R5_InJournal, |
| 4617 | &dev->flags))) { |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4618 | pr_debug("Writing block %d\n", i); |
| 4619 | set_bit(R5_Wantwrite, &dev->flags); |
| 4620 | if (prexor) |
| 4621 | continue; |
NeilBrown | 9c4bdf6 | 2014-08-13 09:57:07 +1000 | [diff] [blame] | 4622 | if (s.failed > 1) |
| 4623 | continue; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4624 | if (!test_bit(R5_Insync, &dev->flags) || |
| 4625 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 4626 | s.failed == 0)) |
| 4627 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4628 | } |
| 4629 | } |
| 4630 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4631 | s.dec_preread_active = 1; |
| 4632 | } |
| 4633 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 4634 | /* |
| 4635 | * might be able to return some write requests if the parity blocks |
| 4636 | * are safe, or on a failed drive |
| 4637 | */ |
| 4638 | pdev = &sh->dev[sh->pd_idx]; |
| 4639 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 4640 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 4641 | qdev = &sh->dev[sh->qd_idx]; |
| 4642 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 4643 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 4644 | || conf->level < 6; |
| 4645 | |
| 4646 | if (s.written && |
| 4647 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 4648 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 4649 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 4650 | test_bit(R5_Discard, &pdev->flags))))) && |
| 4651 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 4652 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 4653 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 4654 | test_bit(R5_Discard, &qdev->flags)))))) |
| 4655 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 4656 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4657 | if (s.just_cached) |
| 4658 | r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi); |
| 4659 | r5l_stripe_write_finished(sh); |
| 4660 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 4661 | /* Now we might consider reading some blocks, either to check/generate |
| 4662 | * parity, or to satisfy requests |
| 4663 | * or to load a block that is being partially written. |
| 4664 | */ |
| 4665 | if (s.to_read || s.non_overwrite |
| 4666 | || (conf->level == 6 && s.to_write && s.failed) |
| 4667 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 4668 | || s.replacing |
| 4669 | || s.expanding) |
| 4670 | handle_stripe_fill(sh, &s, disks); |
| 4671 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4672 | /* |
| 4673 | * When the stripe finishes full journal write cycle (write to journal |
| 4674 | * and raid disk), this is the clean up procedure so it is ready for |
| 4675 | * next operation. |
| 4676 | */ |
| 4677 | r5c_finish_stripe_write_out(conf, sh, &s); |
| 4678 | |
| 4679 | /* |
| 4680 | * Now to consider new write requests, cache write back and what else, |
| 4681 | * if anything should be read. We do not handle new writes when: |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4682 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 4683 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 4684 | * block. |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4685 | * 3/ A r5c cache log write is in flight. |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4686 | */ |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4687 | |
| 4688 | if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) { |
| 4689 | if (!r5c_is_writeback(conf->log)) { |
| 4690 | if (s.to_write) |
| 4691 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 4692 | } else { /* write back cache */ |
| 4693 | int ret = 0; |
| 4694 | |
| 4695 | /* First, try handle writes in caching phase */ |
| 4696 | if (s.to_write) |
| 4697 | ret = r5c_try_caching_write(conf, sh, &s, |
| 4698 | disks); |
| 4699 | /* |
| 4700 | * If caching phase failed: ret == -EAGAIN |
| 4701 | * OR |
| 4702 | * stripe under reclaim: !caching && injournal |
| 4703 | * |
| 4704 | * fall back to handle_stripe_dirtying() |
| 4705 | */ |
| 4706 | if (ret == -EAGAIN || |
| 4707 | /* stripe under reclaim: !caching && injournal */ |
| 4708 | (!test_bit(STRIPE_R5C_CACHING, &sh->state) && |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 4709 | s.injournal > 0)) { |
| 4710 | ret = handle_stripe_dirtying(conf, sh, &s, |
| 4711 | disks); |
| 4712 | if (ret == -EAGAIN) |
| 4713 | goto finish; |
| 4714 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4715 | } |
| 4716 | } |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4717 | |
| 4718 | /* maybe we need to check and possibly fix the parity for this stripe |
| 4719 | * Any reads will already have been scheduled, so we just see if enough |
| 4720 | * data is available. The parity check is held off while parity |
| 4721 | * dependent operations are in flight. |
| 4722 | */ |
| 4723 | if (sh->check_state || |
| 4724 | (s.syncing && s.locked == 0 && |
| 4725 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 4726 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 4727 | if (conf->level == 6) |
| 4728 | handle_parity_checks6(conf, sh, &s, disks); |
| 4729 | else |
| 4730 | handle_parity_checks5(conf, sh, &s, disks); |
| 4731 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4732 | |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4733 | if ((s.replacing || s.syncing) && s.locked == 0 |
| 4734 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) |
| 4735 | && !test_bit(STRIPE_REPLACED, &sh->state)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4736 | /* Write out to replacement devices where possible */ |
| 4737 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4738 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 4739 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4740 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 4741 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 4742 | s.locked++; |
| 4743 | } |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4744 | if (s.replacing) |
| 4745 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4746 | set_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4747 | } |
| 4748 | if ((s.syncing || s.replacing) && s.locked == 0 && |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4749 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4750 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4751 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 4752 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4753 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 4754 | wake_up(&conf->wait_for_overlap); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | /* If the failed drives are just a ReadError, then we might need |
| 4758 | * to progress the repair/check process |
| 4759 | */ |
| 4760 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 4761 | for (i = 0; i < s.failed; i++) { |
| 4762 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 4763 | if (test_bit(R5_ReadError, &dev->flags) |
| 4764 | && !test_bit(R5_LOCKED, &dev->flags) |
| 4765 | && test_bit(R5_UPTODATE, &dev->flags) |
| 4766 | ) { |
| 4767 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 4768 | set_bit(R5_Wantwrite, &dev->flags); |
| 4769 | set_bit(R5_ReWrite, &dev->flags); |
| 4770 | set_bit(R5_LOCKED, &dev->flags); |
| 4771 | s.locked++; |
| 4772 | } else { |
| 4773 | /* let's read it back */ |
| 4774 | set_bit(R5_Wantread, &dev->flags); |
| 4775 | set_bit(R5_LOCKED, &dev->flags); |
| 4776 | s.locked++; |
| 4777 | } |
| 4778 | } |
| 4779 | } |
| 4780 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4781 | /* Finish reconstruct operations initiated by the expansion process */ |
| 4782 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 4783 | struct stripe_head *sh_src |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4784 | = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4785 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 4786 | /* sh cannot be written until sh_src has been read. |
| 4787 | * so arrange for sh to be delayed a little |
| 4788 | */ |
| 4789 | set_bit(STRIPE_DELAYED, &sh->state); |
| 4790 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4791 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 4792 | &sh_src->state)) |
| 4793 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4794 | raid5_release_stripe(sh_src); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4795 | goto finish; |
| 4796 | } |
| 4797 | if (sh_src) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4798 | raid5_release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4799 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4800 | sh->reconstruct_state = reconstruct_state_idle; |
| 4801 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 4802 | for (i = conf->raid_disks; i--; ) { |
| 4803 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 4804 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 4805 | s.locked++; |
| 4806 | } |
| 4807 | } |
| 4808 | |
| 4809 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 4810 | !sh->reconstruct_state) { |
| 4811 | /* Need to write out all blocks after computing parity */ |
| 4812 | sh->disks = conf->raid_disks; |
| 4813 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 4814 | schedule_reconstruction(sh, &s, 1, 1); |
| 4815 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 4816 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4817 | atomic_dec(&conf->reshape_stripes); |
| 4818 | wake_up(&conf->wait_for_overlap); |
| 4819 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 4820 | } |
| 4821 | |
| 4822 | if (s.expanding && s.locked == 0 && |
| 4823 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 4824 | handle_stripe_expansion(conf, sh); |
| 4825 | |
| 4826 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4827 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 4828 | if (unlikely(s.blocked_rdev)) { |
| 4829 | if (conf->mddev->external) |
| 4830 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 4831 | conf->mddev); |
| 4832 | else |
| 4833 | /* Internal metadata will immediately |
| 4834 | * be written by raid5d, so we don't |
| 4835 | * need to wait here. |
| 4836 | */ |
| 4837 | rdev_dec_pending(s.blocked_rdev, |
| 4838 | conf->mddev); |
| 4839 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4840 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4841 | if (s.handle_bad_blocks) |
| 4842 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4843 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4844 | struct r5dev *dev = &sh->dev[i]; |
| 4845 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 4846 | /* We own a safe reference to the rdev */ |
| 4847 | rdev = conf->disks[i].rdev; |
| 4848 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 4849 | STRIPE_SECTORS, 0)) |
| 4850 | md_error(conf->mddev, rdev); |
| 4851 | rdev_dec_pending(rdev, conf->mddev); |
| 4852 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4853 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 4854 | rdev = conf->disks[i].rdev; |
| 4855 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4856 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4857 | rdev_dec_pending(rdev, conf->mddev); |
| 4858 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4859 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4860 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4861 | if (!rdev) |
| 4862 | /* rdev have been moved down */ |
| 4863 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4864 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4865 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4866 | rdev_dec_pending(rdev, conf->mddev); |
| 4867 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4868 | } |
| 4869 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4870 | if (s.ops_request) |
| 4871 | raid_run_ops(sh, s.ops_request); |
| 4872 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 4873 | ops_run_io(sh, &s); |
| 4874 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 4875 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4876 | /* 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] | 4877 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4878 | * have actually been submitted. |
| 4879 | */ |
| 4880 | atomic_dec(&conf->preread_active_stripes); |
| 4881 | if (atomic_read(&conf->preread_active_stripes) < |
| 4882 | IO_THRESHOLD) |
| 4883 | md_wakeup_thread(conf->mddev->thread); |
| 4884 | } |
| 4885 | |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 4886 | if (!bio_list_empty(&s.return_bi)) { |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 4887 | if (test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) { |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 4888 | spin_lock_irq(&conf->device_lock); |
| 4889 | bio_list_merge(&conf->return_bi, &s.return_bi); |
| 4890 | spin_unlock_irq(&conf->device_lock); |
| 4891 | md_wakeup_thread(conf->mddev->thread); |
| 4892 | } else |
| 4893 | return_io(&s.return_bi); |
| 4894 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4895 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 4896 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4897 | } |
| 4898 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4899 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4900 | { |
| 4901 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 4902 | while (!list_empty(&conf->delayed_list)) { |
| 4903 | struct list_head *l = conf->delayed_list.next; |
| 4904 | struct stripe_head *sh; |
| 4905 | sh = list_entry(l, struct stripe_head, lru); |
| 4906 | list_del_init(l); |
| 4907 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4908 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4909 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4910 | list_add_tail(&sh->lru, &conf->hold_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4911 | raid5_wakeup_stripe_thread(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4912 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4913 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4914 | } |
| 4915 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4916 | static void activate_bit_delay(struct r5conf *conf, |
| 4917 | struct list_head *temp_inactive_list) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4918 | { |
| 4919 | /* device_lock is held */ |
| 4920 | struct list_head head; |
| 4921 | list_add(&head, &conf->bitmap_list); |
| 4922 | list_del_init(&conf->bitmap_list); |
| 4923 | while (!list_empty(&head)) { |
| 4924 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4925 | int hash; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4926 | list_del_init(&sh->lru); |
| 4927 | atomic_inc(&sh->count); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4928 | hash = sh->hash_lock_index; |
| 4929 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4930 | } |
| 4931 | } |
| 4932 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4933 | static int raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4934 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4935 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4936 | |
| 4937 | /* No difference between reads and writes. Just check |
| 4938 | * how busy the stripe_cache is |
| 4939 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 4940 | |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 4941 | if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4942 | return 1; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 4943 | |
| 4944 | /* Also checks whether there is pressure on r5cache log space */ |
| 4945 | if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) |
| 4946 | return 1; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4947 | if (conf->quiesce) |
| 4948 | return 1; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4949 | if (atomic_read(&conf->empty_inactive_list_nr)) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4950 | return 1; |
| 4951 | |
| 4952 | return 0; |
| 4953 | } |
| 4954 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4955 | 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] | 4956 | { |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 4957 | struct r5conf *conf = mddev->private; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4958 | sector_t sector = bio->bi_iter.bi_sector + get_start_sect(bio->bi_bdev); |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 4959 | unsigned int chunk_sectors; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 4960 | unsigned int bio_sectors = bio_sectors(bio); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4961 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 4962 | chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4963 | return chunk_sectors >= |
| 4964 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 4965 | } |
| 4966 | |
| 4967 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4968 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 4969 | * later sampled by raid5d. |
| 4970 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4971 | 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] | 4972 | { |
| 4973 | unsigned long flags; |
| 4974 | |
| 4975 | spin_lock_irqsave(&conf->device_lock, flags); |
| 4976 | |
| 4977 | bi->bi_next = conf->retry_read_aligned_list; |
| 4978 | conf->retry_read_aligned_list = bi; |
| 4979 | |
| 4980 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 4981 | md_wakeup_thread(conf->mddev->thread); |
| 4982 | } |
| 4983 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4984 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4985 | { |
| 4986 | struct bio *bi; |
| 4987 | |
| 4988 | bi = conf->retry_read_aligned; |
| 4989 | if (bi) { |
| 4990 | conf->retry_read_aligned = NULL; |
| 4991 | return bi; |
| 4992 | } |
| 4993 | bi = conf->retry_read_aligned_list; |
| 4994 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4995 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4996 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4997 | /* |
| 4998 | * this sets the active strip count to 1 and the processed |
| 4999 | * strip count to zero (upper 8 bits) |
| 5000 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5001 | raid5_set_bi_stripes(bi, 1); /* biased count of active stripes */ |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5002 | } |
| 5003 | |
| 5004 | return bi; |
| 5005 | } |
| 5006 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5007 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5008 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 5009 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 5010 | * first). |
| 5011 | * If the read failed.. |
| 5012 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5013 | static void raid5_align_endio(struct bio *bi) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5014 | { |
| 5015 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5016 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5017 | struct r5conf *conf; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5018 | struct md_rdev *rdev; |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 5019 | int error = bi->bi_error; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5020 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5021 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5022 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5023 | rdev = (void*)raid_bi->bi_next; |
| 5024 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 5025 | mddev = rdev->mddev; |
| 5026 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5027 | |
| 5028 | rdev_dec_pending(rdev, conf->mddev); |
| 5029 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 5030 | if (!error) { |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5031 | trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev), |
| 5032 | raid_bi, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5033 | bio_endio(raid_bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5034 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 5035 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 5036 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5037 | } |
| 5038 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5039 | 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] | 5040 | |
| 5041 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5042 | } |
| 5043 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5044 | 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] | 5045 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5046 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 5047 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5048 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 5049 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5050 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5051 | |
| 5052 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5053 | pr_debug("%s: non aligned\n", __func__); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5054 | return 0; |
| 5055 | } |
| 5056 | /* |
Ming Lei | d7a1030 | 2017-02-14 23:29:03 +0800 | [diff] [blame] | 5057 | * 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] | 5058 | */ |
Ming Lei | d7a1030 | 2017-02-14 23:29:03 +0800 | [diff] [blame] | 5059 | 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] | 5060 | if (!align_bi) |
| 5061 | return 0; |
| 5062 | /* |
| 5063 | * set bi_end_io to a new function, and set bi_private to the |
| 5064 | * original bio. |
| 5065 | */ |
| 5066 | align_bi->bi_end_io = raid5_align_endio; |
| 5067 | align_bi->bi_private = raid_bio; |
| 5068 | /* |
| 5069 | * compute position |
| 5070 | */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5071 | align_bi->bi_iter.bi_sector = |
| 5072 | raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, |
| 5073 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5074 | |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5075 | end_sector = bio_end_sector(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5076 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5077 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 5078 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 5079 | rdev->recovery_offset < end_sector) { |
| 5080 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 5081 | if (rdev && |
| 5082 | (test_bit(Faulty, &rdev->flags) || |
| 5083 | !(test_bit(In_sync, &rdev->flags) || |
| 5084 | rdev->recovery_offset >= end_sector))) |
| 5085 | rdev = NULL; |
| 5086 | } |
Song Liu | 03b047f | 2017-01-11 13:39:14 -0800 | [diff] [blame] | 5087 | |
| 5088 | if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) { |
| 5089 | rcu_read_unlock(); |
| 5090 | bio_put(align_bi); |
| 5091 | return 0; |
| 5092 | } |
| 5093 | |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 5094 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5095 | sector_t first_bad; |
| 5096 | int bad_sectors; |
| 5097 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5098 | atomic_inc(&rdev->nr_pending); |
| 5099 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5100 | raid_bio->bi_next = (void*)rdev; |
| 5101 | align_bi->bi_bdev = rdev->bdev; |
Jens Axboe | b7c44ed | 2015-07-24 12:37:59 -0600 | [diff] [blame] | 5102 | bio_clear_flag(align_bi, BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5103 | |
Kent Overstreet | 7140aaf | 2013-09-25 13:37:01 -0700 | [diff] [blame] | 5104 | if (is_badblock(rdev, align_bi->bi_iter.bi_sector, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5105 | bio_sectors(align_bi), |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 5106 | &first_bad, &bad_sectors)) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5107 | bio_put(align_bi); |
| 5108 | rdev_dec_pending(rdev, mddev); |
| 5109 | return 0; |
| 5110 | } |
| 5111 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 5112 | /* No reshape active, so we can trust rdev->data_offset */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5113 | align_bi->bi_iter.bi_sector += rdev->data_offset; |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 5114 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5115 | spin_lock_irq(&conf->device_lock); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 5116 | wait_event_lock_irq(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5117 | conf->quiesce == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 5118 | conf->device_lock); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5119 | atomic_inc(&conf->active_aligned_reads); |
| 5120 | spin_unlock_irq(&conf->device_lock); |
| 5121 | |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5122 | if (mddev->gendisk) |
| 5123 | trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), |
| 5124 | align_bi, disk_devt(mddev->gendisk), |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5125 | raid_bio->bi_iter.bi_sector); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5126 | generic_make_request(align_bi); |
| 5127 | return 1; |
| 5128 | } else { |
| 5129 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5130 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5131 | return 0; |
| 5132 | } |
| 5133 | } |
| 5134 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5135 | static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio) |
| 5136 | { |
| 5137 | struct bio *split; |
| 5138 | |
| 5139 | do { |
| 5140 | sector_t sector = raid_bio->bi_iter.bi_sector; |
| 5141 | unsigned chunk_sects = mddev->chunk_sectors; |
| 5142 | unsigned sectors = chunk_sects - (sector & (chunk_sects-1)); |
| 5143 | |
| 5144 | if (sectors < bio_sectors(raid_bio)) { |
| 5145 | split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set); |
| 5146 | bio_chain(split, raid_bio); |
| 5147 | } else |
| 5148 | split = raid_bio; |
| 5149 | |
| 5150 | if (!raid5_read_one_chunk(mddev, split)) { |
| 5151 | if (split != raid_bio) |
| 5152 | generic_make_request(raid_bio); |
| 5153 | return split; |
| 5154 | } |
| 5155 | } while (split != raid_bio); |
| 5156 | |
| 5157 | return NULL; |
| 5158 | } |
| 5159 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5160 | /* __get_priority_stripe - get the next stripe to process |
| 5161 | * |
| 5162 | * Full stripe writes are allowed to pass preread active stripes up until |
| 5163 | * the bypass_threshold is exceeded. In general the bypass_count |
| 5164 | * increments when the handle_list is handled before the hold_list; however, it |
| 5165 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 5166 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 5167 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 5168 | * handle_list. |
| 5169 | */ |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5170 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5171 | { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5172 | struct stripe_head *sh = NULL, *tmp; |
| 5173 | struct list_head *handle_list = NULL; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5174 | struct r5worker_group *wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5175 | |
| 5176 | if (conf->worker_cnt_per_group == 0) { |
| 5177 | handle_list = &conf->handle_list; |
| 5178 | } else if (group != ANY_GROUP) { |
| 5179 | handle_list = &conf->worker_groups[group].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5180 | wg = &conf->worker_groups[group]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5181 | } else { |
| 5182 | int i; |
| 5183 | for (i = 0; i < conf->group_cnt; i++) { |
| 5184 | handle_list = &conf->worker_groups[i].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5185 | wg = &conf->worker_groups[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5186 | if (!list_empty(handle_list)) |
| 5187 | break; |
| 5188 | } |
| 5189 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5190 | |
| 5191 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 5192 | __func__, |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5193 | list_empty(handle_list) ? "empty" : "busy", |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5194 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 5195 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 5196 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5197 | if (!list_empty(handle_list)) { |
| 5198 | sh = list_entry(handle_list->next, typeof(*sh), lru); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5199 | |
| 5200 | if (list_empty(&conf->hold_list)) |
| 5201 | conf->bypass_count = 0; |
| 5202 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 5203 | if (conf->hold_list.next == conf->last_hold) |
| 5204 | conf->bypass_count++; |
| 5205 | else { |
| 5206 | conf->last_hold = conf->hold_list.next; |
| 5207 | conf->bypass_count -= conf->bypass_threshold; |
| 5208 | if (conf->bypass_count < 0) |
| 5209 | conf->bypass_count = 0; |
| 5210 | } |
| 5211 | } |
| 5212 | } else if (!list_empty(&conf->hold_list) && |
| 5213 | ((conf->bypass_threshold && |
| 5214 | conf->bypass_count > conf->bypass_threshold) || |
| 5215 | atomic_read(&conf->pending_full_writes) == 0)) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5216 | |
| 5217 | list_for_each_entry(tmp, &conf->hold_list, lru) { |
| 5218 | if (conf->worker_cnt_per_group == 0 || |
| 5219 | group == ANY_GROUP || |
| 5220 | !cpu_online(tmp->cpu) || |
| 5221 | cpu_to_group(tmp->cpu) == group) { |
| 5222 | sh = tmp; |
| 5223 | break; |
| 5224 | } |
| 5225 | } |
| 5226 | |
| 5227 | if (sh) { |
| 5228 | conf->bypass_count -= conf->bypass_threshold; |
| 5229 | if (conf->bypass_count < 0) |
| 5230 | conf->bypass_count = 0; |
| 5231 | } |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5232 | wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5233 | } |
| 5234 | |
| 5235 | if (!sh) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5236 | return NULL; |
| 5237 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5238 | if (wg) { |
| 5239 | wg->stripes_cnt--; |
| 5240 | sh->group = NULL; |
| 5241 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5242 | list_del_init(&sh->lru); |
Shaohua Li | c7a6d35 | 2014-04-15 09:12:54 +0800 | [diff] [blame] | 5243 | BUG_ON(atomic_inc_return(&sh->count) != 1); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5244 | return sh; |
| 5245 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5246 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5247 | struct raid5_plug_cb { |
| 5248 | struct blk_plug_cb cb; |
| 5249 | struct list_head list; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5250 | struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5251 | }; |
| 5252 | |
| 5253 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 5254 | { |
| 5255 | struct raid5_plug_cb *cb = container_of( |
| 5256 | blk_cb, struct raid5_plug_cb, cb); |
| 5257 | struct stripe_head *sh; |
| 5258 | struct mddev *mddev = cb->cb.data; |
| 5259 | struct r5conf *conf = mddev->private; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5260 | int cnt = 0; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5261 | int hash; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5262 | |
| 5263 | if (cb->list.next && !list_empty(&cb->list)) { |
| 5264 | spin_lock_irq(&conf->device_lock); |
| 5265 | while (!list_empty(&cb->list)) { |
| 5266 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 5267 | list_del_init(&sh->lru); |
| 5268 | /* |
| 5269 | * avoid race release_stripe_plug() sees |
| 5270 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 5271 | * is still in our list |
| 5272 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 5273 | smp_mb__before_atomic(); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5274 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5275 | /* |
| 5276 | * STRIPE_ON_RELEASE_LIST could be set here. In that |
| 5277 | * case, the count is always > 1 here |
| 5278 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5279 | hash = sh->hash_lock_index; |
| 5280 | __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5281 | cnt++; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5282 | } |
| 5283 | spin_unlock_irq(&conf->device_lock); |
| 5284 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5285 | release_inactive_stripe_list(conf, cb->temp_inactive_list, |
| 5286 | NR_STRIPE_HASH_LOCKS); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5287 | if (mddev->queue) |
| 5288 | trace_block_unplug(mddev->queue, cnt, !from_schedule); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5289 | kfree(cb); |
| 5290 | } |
| 5291 | |
| 5292 | static void release_stripe_plug(struct mddev *mddev, |
| 5293 | struct stripe_head *sh) |
| 5294 | { |
| 5295 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 5296 | raid5_unplug, mddev, |
| 5297 | sizeof(struct raid5_plug_cb)); |
| 5298 | struct raid5_plug_cb *cb; |
| 5299 | |
| 5300 | if (!blk_cb) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5301 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5302 | return; |
| 5303 | } |
| 5304 | |
| 5305 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 5306 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5307 | if (cb->list.next == NULL) { |
| 5308 | int i; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5309 | INIT_LIST_HEAD(&cb->list); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5310 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5311 | INIT_LIST_HEAD(cb->temp_inactive_list + i); |
| 5312 | } |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5313 | |
| 5314 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 5315 | list_add_tail(&sh->lru, &cb->list); |
| 5316 | else |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5317 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5318 | } |
| 5319 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5320 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 5321 | { |
| 5322 | struct r5conf *conf = mddev->private; |
| 5323 | sector_t logical_sector, last_sector; |
| 5324 | struct stripe_head *sh; |
| 5325 | int remaining; |
| 5326 | int stripe_sectors; |
| 5327 | |
| 5328 | if (mddev->reshape_position != MaxSector) |
| 5329 | /* Skip discard while reshape is happening */ |
| 5330 | return; |
| 5331 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5332 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 5333 | last_sector = bi->bi_iter.bi_sector + (bi->bi_iter.bi_size>>9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5334 | |
| 5335 | bi->bi_next = NULL; |
| 5336 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
| 5337 | |
| 5338 | stripe_sectors = conf->chunk_sectors * |
| 5339 | (conf->raid_disks - conf->max_degraded); |
| 5340 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 5341 | stripe_sectors); |
| 5342 | sector_div(last_sector, stripe_sectors); |
| 5343 | |
| 5344 | logical_sector *= conf->chunk_sectors; |
| 5345 | last_sector *= conf->chunk_sectors; |
| 5346 | |
| 5347 | for (; logical_sector < last_sector; |
| 5348 | logical_sector += STRIPE_SECTORS) { |
| 5349 | DEFINE_WAIT(w); |
| 5350 | int d; |
| 5351 | again: |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5352 | sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5353 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5354 | TASK_UNINTERRUPTIBLE); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5355 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
| 5356 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5357 | raid5_release_stripe(sh); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5358 | schedule(); |
| 5359 | goto again; |
| 5360 | } |
| 5361 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5362 | spin_lock_irq(&sh->stripe_lock); |
| 5363 | for (d = 0; d < conf->raid_disks; d++) { |
| 5364 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5365 | continue; |
| 5366 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 5367 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 5368 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5369 | raid5_release_stripe(sh); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5370 | schedule(); |
| 5371 | goto again; |
| 5372 | } |
| 5373 | } |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5374 | set_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5375 | finish_wait(&conf->wait_for_overlap, &w); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5376 | sh->overwrite_disks = 0; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5377 | for (d = 0; d < conf->raid_disks; d++) { |
| 5378 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5379 | continue; |
| 5380 | sh->dev[d].towrite = bi; |
| 5381 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
| 5382 | raid5_inc_bi_active_stripes(bi); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5383 | sh->overwrite_disks++; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5384 | } |
| 5385 | spin_unlock_irq(&sh->stripe_lock); |
| 5386 | if (conf->mddev->bitmap) { |
| 5387 | for (d = 0; |
| 5388 | d < conf->raid_disks - conf->max_degraded; |
| 5389 | d++) |
| 5390 | bitmap_startwrite(mddev->bitmap, |
| 5391 | sh->sector, |
| 5392 | STRIPE_SECTORS, |
| 5393 | 0); |
| 5394 | sh->bm_seq = conf->seq_flush + 1; |
| 5395 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 5396 | } |
| 5397 | |
| 5398 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5399 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 5400 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5401 | atomic_inc(&conf->preread_active_stripes); |
| 5402 | release_stripe_plug(mddev, sh); |
| 5403 | } |
| 5404 | |
| 5405 | remaining = raid5_dec_bi_active_stripes(bi); |
| 5406 | if (remaining == 0) { |
| 5407 | md_write_end(mddev); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5408 | bio_endio(bi); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5409 | } |
| 5410 | } |
| 5411 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5412 | static void raid5_make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5414 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5415 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5416 | sector_t new_sector; |
| 5417 | sector_t logical_sector, last_sector; |
| 5418 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5419 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 5420 | int remaining; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5421 | DEFINE_WAIT(w); |
| 5422 | bool do_prepare; |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5423 | bool do_flush = false; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5424 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5425 | if (unlikely(bi->bi_opf & REQ_PREFLUSH)) { |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5426 | int ret = r5l_handle_flush_request(conf->log, bi); |
| 5427 | |
| 5428 | if (ret == 0) |
| 5429 | return; |
| 5430 | if (ret == -ENODEV) { |
| 5431 | md_flush_request(mddev, bi); |
| 5432 | return; |
| 5433 | } |
| 5434 | /* ret == -EAGAIN, fallback */ |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5435 | /* |
| 5436 | * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH, |
| 5437 | * we need to flush journal device |
| 5438 | */ |
| 5439 | do_flush = bi->bi_opf & REQ_PREFLUSH; |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 5440 | } |
| 5441 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5442 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5443 | |
Eric Mei | 9ffc8f7 | 2015-03-18 23:39:11 -0600 | [diff] [blame] | 5444 | /* |
| 5445 | * If array is degraded, better not do chunk aligned read because |
| 5446 | * later we might have to read it again in order to reconstruct |
| 5447 | * data on failed drives. |
| 5448 | */ |
| 5449 | if (rw == READ && mddev->degraded == 0 && |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5450 | mddev->reshape_position == MaxSector) { |
| 5451 | bi = chunk_aligned_read(mddev, bi); |
| 5452 | if (!bi) |
| 5453 | return; |
| 5454 | } |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 5455 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 5456 | if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) { |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5457 | make_discard_request(mddev, bi); |
| 5458 | return; |
| 5459 | } |
| 5460 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5461 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5462 | last_sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5463 | bi->bi_next = NULL; |
| 5464 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5465 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5466 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5467 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5468 | int previous; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5469 | int seq; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5470 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5471 | do_prepare = false; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5472 | retry: |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5473 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5474 | previous = 0; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5475 | if (do_prepare) |
| 5476 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5477 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5478 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5479 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5480 | * 64bit on a 32bit platform, and so it might be |
| 5481 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 5482 | * Of course reshape_progress could change after |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5483 | * the lock is dropped, so once we get a reference |
| 5484 | * to the stripe that we think it is, we will have |
| 5485 | * to check again. |
| 5486 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5487 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5488 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5489 | ? logical_sector < conf->reshape_progress |
| 5490 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5491 | previous = 1; |
| 5492 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5493 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5494 | ? logical_sector < conf->reshape_safe |
| 5495 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5496 | spin_unlock_irq(&conf->device_lock); |
| 5497 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5498 | do_prepare = true; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5499 | goto retry; |
| 5500 | } |
| 5501 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5502 | spin_unlock_irq(&conf->device_lock); |
| 5503 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5504 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5505 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 5506 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5507 | &dd_idx, NULL); |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5508 | pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n", |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5509 | (unsigned long long)new_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5510 | (unsigned long long)logical_sector); |
| 5511 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5512 | sh = raid5_get_active_stripe(conf, new_sector, previous, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5513 | (bi->bi_opf & REQ_RAHEAD), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5514 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5515 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5516 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f76 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5517 | * stripe, so we must do the range check again. |
| 5518 | * Expansion could still move past after this |
| 5519 | * test, but as we are holding a reference to |
| 5520 | * 'sh', we know that if that happens, |
| 5521 | * STRIPE_EXPANDING will get set and the expansion |
| 5522 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5523 | */ |
| 5524 | int must_retry = 0; |
| 5525 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5526 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5527 | ? logical_sector >= conf->reshape_progress |
| 5528 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5529 | /* mismatch, need to try again */ |
| 5530 | must_retry = 1; |
| 5531 | spin_unlock_irq(&conf->device_lock); |
| 5532 | if (must_retry) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5533 | raid5_release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 5534 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5535 | do_prepare = true; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5536 | goto retry; |
| 5537 | } |
| 5538 | } |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5539 | if (read_seqcount_retry(&conf->gen_lock, seq)) { |
| 5540 | /* Might have got the wrong stripe_head |
| 5541 | * by accident |
| 5542 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5543 | raid5_release_stripe(sh); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5544 | goto retry; |
| 5545 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5546 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 5547 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5548 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5549 | logical_sector < mddev->suspend_hi) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5550 | raid5_release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5551 | /* As the suspend_* range is controlled by |
| 5552 | * userspace, we want an interruptible |
| 5553 | * wait. |
| 5554 | */ |
| 5555 | flush_signals(current); |
| 5556 | prepare_to_wait(&conf->wait_for_overlap, |
| 5557 | &w, TASK_INTERRUPTIBLE); |
| 5558 | if (logical_sector >= mddev->suspend_lo && |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5559 | logical_sector < mddev->suspend_hi) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5560 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5561 | do_prepare = true; |
| 5562 | } |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5563 | goto retry; |
| 5564 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5565 | |
| 5566 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5567 | !add_stripe_bio(sh, bi, dd_idx, rw, previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5568 | /* Stripe is busy expanding or |
| 5569 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5570 | * and wait a while |
| 5571 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 5572 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5573 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5574 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5575 | do_prepare = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5576 | goto retry; |
| 5577 | } |
Song Liu | 3bddb7f | 2016-11-18 16:46:50 -0800 | [diff] [blame] | 5578 | if (do_flush) { |
| 5579 | set_bit(STRIPE_R5C_PREFLUSH, &sh->state); |
| 5580 | /* we only need flush for one stripe */ |
| 5581 | do_flush = false; |
| 5582 | } |
| 5583 | |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 5584 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5585 | clear_bit(STRIPE_DELAYED, &sh->state); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5586 | if ((!sh->batch_head || sh == sh->batch_head) && |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5587 | (bi->bi_opf & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5588 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5589 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5590 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5591 | } else { |
| 5592 | /* cannot get stripe for read-ahead, just give-up */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5593 | bi->bi_error = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5594 | break; |
| 5595 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5596 | } |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5597 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5598 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5599 | remaining = raid5_dec_bi_active_stripes(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 5600 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5601 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5602 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5603 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 5604 | |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5605 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 5606 | bi, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5607 | bio_endio(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5608 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5609 | } |
| 5610 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5611 | 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] | 5612 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5613 | 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] | 5614 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5615 | /* reshaping is quite different to recovery/resync so it is |
| 5616 | * handled quite separately ... here. |
| 5617 | * |
| 5618 | * On each call to sync_request, we gather one chunk worth of |
| 5619 | * destination stripes and flag them as expanding. |
| 5620 | * Then we find all the source stripes and request reads. |
| 5621 | * As the reads complete, handle_stripe will copy the data |
| 5622 | * into the destination stripe and release that stripe. |
| 5623 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5624 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5625 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5626 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5627 | int raid_disks = conf->previous_raid_disks; |
| 5628 | int data_disks = raid_disks - conf->max_degraded; |
| 5629 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5630 | int i; |
| 5631 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5632 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5633 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5634 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5635 | struct list_head stripes; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5636 | sector_t retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5637 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5638 | if (sector_nr == 0) { |
| 5639 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5640 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5641 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 5642 | sector_nr = raid5_size(mddev, 0, 0) |
| 5643 | - conf->reshape_progress; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 5644 | } else if (mddev->reshape_backwards && |
| 5645 | conf->reshape_progress == MaxSector) { |
| 5646 | /* shouldn't happen, but just in case, finish up.*/ |
| 5647 | sector_nr = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5648 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5649 | conf->reshape_progress > 0) |
| 5650 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5651 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5652 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 5653 | mddev->curr_resync_completed = sector_nr; |
| 5654 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5655 | *skipped = 1; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5656 | retn = sector_nr; |
| 5657 | goto finish; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5658 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5659 | } |
| 5660 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5661 | /* We need to process a full chunk at a time. |
| 5662 | * If old and new chunk sizes differ, we need to process the |
| 5663 | * largest of these |
| 5664 | */ |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5665 | |
| 5666 | reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5667 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5668 | /* We update the metadata at least every 10 seconds, or when |
| 5669 | * the data about to be copied would over-write the source of |
| 5670 | * the data at the front of the range. i.e. one new_stripe |
| 5671 | * along from reshape_progress new_maps to after where |
| 5672 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5673 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5674 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5675 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5676 | readpos = conf->reshape_progress; |
| 5677 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5678 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5679 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5680 | if (mddev->reshape_backwards) { |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 5681 | BUG_ON(writepos < reshape_sectors); |
| 5682 | writepos -= reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5683 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5684 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5685 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5686 | writepos += reshape_sectors; |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 5687 | /* readpos and safepos are worst-case calculations. |
| 5688 | * A negative number is overly pessimistic, and causes |
| 5689 | * obvious problems for unsigned storage. So clip to 0. |
| 5690 | */ |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 5691 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 5692 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5693 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5694 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5695 | /* Having calculated the 'writepos' possibly use it |
| 5696 | * to set 'stripe_addr' which is where we will write to. |
| 5697 | */ |
| 5698 | if (mddev->reshape_backwards) { |
| 5699 | BUG_ON(conf->reshape_progress == 0); |
| 5700 | stripe_addr = writepos; |
| 5701 | BUG_ON((mddev->dev_sectors & |
| 5702 | ~((sector_t)reshape_sectors - 1)) |
| 5703 | - reshape_sectors - stripe_addr |
| 5704 | != sector_nr); |
| 5705 | } else { |
| 5706 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 5707 | stripe_addr = sector_nr; |
| 5708 | } |
| 5709 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5710 | /* 'writepos' is the most advanced device address we might write. |
| 5711 | * 'readpos' is the least advanced device address we might read. |
| 5712 | * 'safepos' is the least address recorded in the metadata as having |
| 5713 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5714 | * If there is a min_offset_diff, these are adjusted either by |
| 5715 | * increasing the safepos/readpos if diff is negative, or |
| 5716 | * increasing writepos if diff is positive. |
| 5717 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5718 | * ensure safety in the face of a crash - that must be done by userspace |
| 5719 | * making a backup of the data. So in that case there is no particular |
| 5720 | * rush to update metadata. |
| 5721 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 5722 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 5723 | * we can be safe in the event of a crash. |
| 5724 | * So we insist on updating metadata if safepos is behind writepos and |
| 5725 | * readpos is beyond writepos. |
| 5726 | * In any case, update the metadata every 10 seconds. |
| 5727 | * Maybe that number should be configurable, but I'm not sure it is |
| 5728 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 5729 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5730 | if (conf->min_offset_diff < 0) { |
| 5731 | safepos += -conf->min_offset_diff; |
| 5732 | readpos += -conf->min_offset_diff; |
| 5733 | } else |
| 5734 | writepos += conf->min_offset_diff; |
| 5735 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5736 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5737 | ? (safepos > writepos && readpos < writepos) |
| 5738 | : (safepos < writepos && readpos > writepos)) || |
| 5739 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5740 | /* Cannot proceed until we've updated the superblock... */ |
| 5741 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5742 | atomic_read(&conf->reshape_stripes)==0 |
| 5743 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5744 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 5745 | return 0; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5746 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5747 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5748 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5749 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5750 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5751 | wait_event(mddev->sb_wait, mddev->sb_flags == 0 || |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5752 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5753 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5754 | return 0; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5755 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5756 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5757 | spin_unlock_irq(&conf->device_lock); |
| 5758 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5759 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5760 | } |
| 5761 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5762 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5763 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5764 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5765 | int skipped_disk = 0; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5766 | sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5767 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 5768 | atomic_inc(&conf->reshape_stripes); |
| 5769 | /* If any of this stripe is beyond the end of the old |
| 5770 | * array, then we need to zero those blocks |
| 5771 | */ |
| 5772 | for (j=sh->disks; j--;) { |
| 5773 | sector_t s; |
| 5774 | if (j == sh->pd_idx) |
| 5775 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5776 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5777 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5778 | continue; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5779 | s = raid5_compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5780 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5781 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5782 | continue; |
| 5783 | } |
| 5784 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 5785 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 5786 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 5787 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5788 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5789 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 5790 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5791 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5792 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5793 | } |
| 5794 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5795 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5796 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5797 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5798 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5799 | spin_unlock_irq(&conf->device_lock); |
| 5800 | /* Ok, those stripe are ready. We can start scheduling |
| 5801 | * reads on the source stripes. |
| 5802 | * The source stripes are determined by mapping the first and last |
| 5803 | * block on the destination stripes. |
| 5804 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5805 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5806 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5807 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5808 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 5809 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5810 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5811 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5812 | if (last_sector >= mddev->dev_sectors) |
| 5813 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5814 | while (first_sector <= last_sector) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5815 | sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5816 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 5817 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5818 | raid5_release_stripe(sh); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5819 | first_sector += STRIPE_SECTORS; |
| 5820 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5821 | /* Now that the sources are clearly marked, we can release |
| 5822 | * the destination stripes |
| 5823 | */ |
| 5824 | while (!list_empty(&stripes)) { |
| 5825 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 5826 | list_del_init(&sh->lru); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5827 | raid5_release_stripe(sh); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5828 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5829 | /* If this takes us to the resync_max point where we have to pause, |
| 5830 | * then we need to write out the superblock. |
| 5831 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5832 | sector_nr += reshape_sectors; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5833 | retn = reshape_sectors; |
| 5834 | finish: |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 5835 | if (mddev->curr_resync_completed > mddev->resync_max || |
| 5836 | (sector_nr - mddev->curr_resync_completed) * 2 |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 5837 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5838 | /* Cannot proceed until we've updated the superblock... */ |
| 5839 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5840 | atomic_read(&conf->reshape_stripes) == 0 |
| 5841 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5842 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 5843 | goto ret; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5844 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5845 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5846 | conf->reshape_checkpoint = jiffies; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5847 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5848 | md_wakeup_thread(mddev->thread); |
| 5849 | wait_event(mddev->sb_wait, |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 5850 | !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags) |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5851 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5852 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5853 | goto ret; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5854 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5855 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5856 | spin_unlock_irq(&conf->device_lock); |
| 5857 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5858 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5859 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5860 | ret: |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5861 | return retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5862 | } |
| 5863 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5864 | static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr, |
| 5865 | int *skipped) |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5866 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5867 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5868 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5869 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 5870 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5871 | int still_degraded = 0; |
| 5872 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5873 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5874 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5875 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5876 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5877 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 5878 | end_reshape(conf); |
| 5879 | return 0; |
| 5880 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5881 | |
| 5882 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 5883 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 5884 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5885 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5886 | conf->fullsync = 0; |
| 5887 | bitmap_close_sync(mddev->bitmap); |
| 5888 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5889 | return 0; |
| 5890 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5891 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5892 | /* Allow raid5_quiesce to complete */ |
| 5893 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 5894 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5895 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5896 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5897 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5898 | /* No need to check resync_max as we never do more than one |
| 5899 | * stripe, and as resync_max will always be on a chunk boundary, |
| 5900 | * if the check in md_do_sync didn't fire, there is no chance |
| 5901 | * of overstepping resync_max here |
| 5902 | */ |
| 5903 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5904 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5905 | * to resync, then assert that we are finished, because there is |
| 5906 | * nothing we can do. |
| 5907 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 5908 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5909 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5910 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5911 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5912 | return rv; |
| 5913 | } |
majianpeng | 6f60804 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 5914 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 5915 | !conf->fullsync && |
| 5916 | !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
| 5917 | sync_blocks >= STRIPE_SECTORS) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5918 | /* we can skip this block, and probably more */ |
| 5919 | sync_blocks /= STRIPE_SECTORS; |
| 5920 | *skipped = 1; |
| 5921 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 5922 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | |
Goldwyn Rodrigues | c40f341 | 2015-08-19 08:14:42 +1000 | [diff] [blame] | 5924 | bitmap_cond_end_sync(mddev->bitmap, sector_nr, false); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 5925 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5926 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5927 | if (sh == NULL) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5928 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5929 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5930 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 5932 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5933 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5934 | /* Need to check if array will still be degraded after recovery/resync |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 5935 | * Note in case of > 1 drive failures it's possible we're rebuilding |
| 5936 | * one drive while leaving another faulty drive in array. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5937 | */ |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 5938 | rcu_read_lock(); |
| 5939 | for (i = 0; i < conf->raid_disks; i++) { |
| 5940 | struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev); |
| 5941 | |
| 5942 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5943 | still_degraded = 1; |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 5944 | } |
| 5945 | rcu_read_unlock(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5946 | |
| 5947 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 5948 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 5949 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Eivind Sarto | 053f5b6 | 2014-06-09 17:06:19 -0700 | [diff] [blame] | 5950 | set_bit(STRIPE_HANDLE, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5951 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5952 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5953 | |
| 5954 | return STRIPE_SECTORS; |
| 5955 | } |
| 5956 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5957 | static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5958 | { |
| 5959 | /* We may not be able to submit a whole bio at once as there |
| 5960 | * may not be enough stripe_heads available. |
| 5961 | * We cannot pre-allocate enough stripe_heads as we may need |
| 5962 | * more than exist in the cache (if we allow ever large chunks). |
| 5963 | * So we do one stripe head at a time and record in |
| 5964 | * ->bi_hw_segments how many have been done. |
| 5965 | * |
| 5966 | * We *know* that this entire raid_bio is in one chunk, so |
| 5967 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 5968 | */ |
| 5969 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5970 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5971 | sector_t sector, logical_sector, last_sector; |
| 5972 | int scnt = 0; |
| 5973 | int remaining; |
| 5974 | int handled = 0; |
| 5975 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5976 | logical_sector = raid_bio->bi_iter.bi_sector & |
| 5977 | ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5978 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5979 | 0, &dd_idx, NULL); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5980 | last_sector = bio_end_sector(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5981 | |
| 5982 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5983 | logical_sector += STRIPE_SECTORS, |
| 5984 | sector += STRIPE_SECTORS, |
| 5985 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5986 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5987 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5988 | /* already done this stripe */ |
| 5989 | continue; |
| 5990 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5991 | sh = raid5_get_active_stripe(conf, sector, 0, 1, 1); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5992 | |
| 5993 | if (!sh) { |
| 5994 | /* failed to get a stripe - must wait */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5995 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5996 | conf->retry_read_aligned = raid_bio; |
| 5997 | return handled; |
| 5998 | } |
| 5999 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 6000 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6001 | raid5_release_stripe(sh); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 6002 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 6003 | conf->retry_read_aligned = raid_bio; |
| 6004 | return handled; |
| 6005 | } |
| 6006 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 6007 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6008 | handle_stripe(sh); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 6009 | raid5_release_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6010 | handled++; |
| 6011 | } |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 6012 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 6013 | if (remaining == 0) { |
| 6014 | trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev), |
| 6015 | raid_bio, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 6016 | bio_endio(raid_bio); |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 6017 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6018 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 6019 | wake_up(&conf->wait_for_quiescent); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6020 | return handled; |
| 6021 | } |
| 6022 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 6023 | static int handle_active_stripes(struct r5conf *conf, int group, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6024 | struct r5worker *worker, |
| 6025 | struct list_head *temp_inactive_list) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6026 | { |
| 6027 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6028 | int i, batch_size = 0, hash; |
| 6029 | bool release_inactive = false; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6030 | |
| 6031 | while (batch_size < MAX_STRIPE_BATCH && |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6032 | (sh = __get_priority_stripe(conf, group)) != NULL) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6033 | batch[batch_size++] = sh; |
| 6034 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6035 | if (batch_size == 0) { |
| 6036 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6037 | if (!list_empty(temp_inactive_list + i)) |
| 6038 | break; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6039 | if (i == NR_STRIPE_HASH_LOCKS) { |
| 6040 | spin_unlock_irq(&conf->device_lock); |
| 6041 | r5l_flush_stripe_to_raid(conf->log); |
| 6042 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6043 | return batch_size; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6044 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6045 | release_inactive = true; |
| 6046 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6047 | spin_unlock_irq(&conf->device_lock); |
| 6048 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6049 | release_inactive_stripe_list(conf, temp_inactive_list, |
| 6050 | NR_STRIPE_HASH_LOCKS); |
| 6051 | |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 6052 | r5l_flush_stripe_to_raid(conf->log); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6053 | if (release_inactive) { |
| 6054 | spin_lock_irq(&conf->device_lock); |
| 6055 | return 0; |
| 6056 | } |
| 6057 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6058 | for (i = 0; i < batch_size; i++) |
| 6059 | handle_stripe(batch[i]); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 6060 | r5l_write_stripe_run(conf->log); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6061 | |
| 6062 | cond_resched(); |
| 6063 | |
| 6064 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6065 | for (i = 0; i < batch_size; i++) { |
| 6066 | hash = batch[i]->hash_lock_index; |
| 6067 | __release_stripe(conf, batch[i], &temp_inactive_list[hash]); |
| 6068 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6069 | return batch_size; |
| 6070 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6071 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6072 | static void raid5_do_work(struct work_struct *work) |
| 6073 | { |
| 6074 | struct r5worker *worker = container_of(work, struct r5worker, work); |
| 6075 | struct r5worker_group *group = worker->group; |
| 6076 | struct r5conf *conf = group->conf; |
| 6077 | int group_id = group - conf->worker_groups; |
| 6078 | int handled; |
| 6079 | struct blk_plug plug; |
| 6080 | |
| 6081 | pr_debug("+++ raid5worker active\n"); |
| 6082 | |
| 6083 | blk_start_plug(&plug); |
| 6084 | handled = 0; |
| 6085 | spin_lock_irq(&conf->device_lock); |
| 6086 | while (1) { |
| 6087 | int batch_size, released; |
| 6088 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6089 | released = release_stripe_list(conf, worker->temp_inactive_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6090 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6091 | batch_size = handle_active_stripes(conf, group_id, worker, |
| 6092 | worker->temp_inactive_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 6093 | worker->working = false; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6094 | if (!batch_size && !released) |
| 6095 | break; |
| 6096 | handled += batch_size; |
| 6097 | } |
| 6098 | pr_debug("%d stripes handled\n", handled); |
| 6099 | |
| 6100 | spin_unlock_irq(&conf->device_lock); |
| 6101 | blk_finish_plug(&plug); |
| 6102 | |
| 6103 | pr_debug("--- raid5worker inactive\n"); |
| 6104 | } |
| 6105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6106 | /* |
| 6107 | * This is our raid5 kernel thread. |
| 6108 | * |
| 6109 | * We scan the hash table for stripes which can be handled now. |
| 6110 | * During the scan, completed stripes are saved for us by the interrupt |
| 6111 | * handler, so that they will not have to wait for our next wakeup. |
| 6112 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6113 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6114 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 6115 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6116 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6117 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6118 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6119 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6120 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6121 | |
| 6122 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6123 | |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 6124 | if (!bio_list_empty(&conf->return_bi) && |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6125 | !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 6126 | struct bio_list tmp = BIO_EMPTY_LIST; |
| 6127 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6128 | if (!test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags)) { |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 6129 | bio_list_merge(&tmp, &conf->return_bi); |
| 6130 | bio_list_init(&conf->return_bi); |
| 6131 | } |
| 6132 | spin_unlock_irq(&conf->device_lock); |
| 6133 | return_io(&tmp); |
| 6134 | } |
| 6135 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6136 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6137 | handled = 0; |
| 6138 | spin_lock_irq(&conf->device_lock); |
| 6139 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6140 | struct bio *bio; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6141 | int batch_size, released; |
| 6142 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6143 | released = release_stripe_list(conf, conf->temp_inactive_list); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6144 | if (released) |
| 6145 | clear_bit(R5_DID_ALLOC, &conf->cache_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6146 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 6147 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 6148 | !list_empty(&conf->bitmap_list)) { |
| 6149 | /* Now is a good time to flush some bitmap updates */ |
| 6150 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 6151 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6152 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 6153 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 6154 | conf->seq_write = conf->seq_flush; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6155 | activate_bit_delay(conf, conf->temp_inactive_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6156 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 6157 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 6158 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 6159 | while ((bio = remove_bio_from_retry(conf))) { |
| 6160 | int ok; |
| 6161 | spin_unlock_irq(&conf->device_lock); |
| 6162 | ok = retry_aligned_read(conf, bio); |
| 6163 | spin_lock_irq(&conf->device_lock); |
| 6164 | if (!ok) |
| 6165 | break; |
| 6166 | handled++; |
| 6167 | } |
| 6168 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6169 | batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, |
| 6170 | conf->temp_inactive_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6171 | if (!batch_size && !released) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6172 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6173 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6174 | |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 6175 | if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) { |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6176 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 6177 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 6178 | spin_lock_irq(&conf->device_lock); |
| 6179 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6180 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6181 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6182 | |
| 6183 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6184 | if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) && |
| 6185 | mutex_trylock(&conf->cache_size_mutex)) { |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6186 | grow_one_stripe(conf, __GFP_NOWARN); |
| 6187 | /* Set flag even if allocation failed. This helps |
| 6188 | * slow down allocation requests when mem is short |
| 6189 | */ |
| 6190 | set_bit(R5_DID_ALLOC, &conf->cache_state); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6191 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6192 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6193 | |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 6194 | flush_deferred_bios(conf); |
| 6195 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 6196 | r5l_flush_stripe_to_raid(conf->log); |
| 6197 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 6198 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6199 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6200 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6201 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6202 | } |
| 6203 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6204 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6205 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6206 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6207 | struct r5conf *conf; |
| 6208 | int ret = 0; |
| 6209 | spin_lock(&mddev->lock); |
| 6210 | conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6211 | if (conf) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6212 | ret = sprintf(page, "%d\n", conf->min_nr_stripes); |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6213 | spin_unlock(&mddev->lock); |
| 6214 | return ret; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6215 | } |
| 6216 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6217 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6218 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6219 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6220 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6221 | int err; |
| 6222 | |
| 6223 | if (size <= 16 || size > 32768) |
| 6224 | return -EINVAL; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6225 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6226 | conf->min_nr_stripes = size; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6227 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6228 | while (size < conf->max_nr_stripes && |
| 6229 | drop_one_stripe(conf)) |
| 6230 | ; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6231 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6232 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6233 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6234 | err = md_allow_write(mddev); |
| 6235 | if (err) |
| 6236 | return err; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6237 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6238 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6239 | while (size > conf->max_nr_stripes) |
| 6240 | if (!grow_one_stripe(conf, GFP_KERNEL)) |
| 6241 | break; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6242 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6243 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6244 | return 0; |
| 6245 | } |
| 6246 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 6247 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6248 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6249 | 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] | 6250 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6251 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6252 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6253 | int err; |
| 6254 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6255 | if (len >= PAGE_SIZE) |
| 6256 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6257 | if (kstrtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6258 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6259 | err = mddev_lock(mddev); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6260 | if (err) |
| 6261 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6262 | conf = mddev->private; |
| 6263 | if (!conf) |
| 6264 | err = -ENODEV; |
| 6265 | else |
| 6266 | err = raid5_set_cache_size(mddev, new); |
| 6267 | mddev_unlock(mddev); |
| 6268 | |
| 6269 | return err ?: len; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6270 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6271 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6272 | static struct md_sysfs_entry |
| 6273 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 6274 | raid5_show_stripe_cache_size, |
| 6275 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6276 | |
| 6277 | static ssize_t |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6278 | raid5_show_rmw_level(struct mddev *mddev, char *page) |
| 6279 | { |
| 6280 | struct r5conf *conf = mddev->private; |
| 6281 | if (conf) |
| 6282 | return sprintf(page, "%d\n", conf->rmw_level); |
| 6283 | else |
| 6284 | return 0; |
| 6285 | } |
| 6286 | |
| 6287 | static ssize_t |
| 6288 | raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len) |
| 6289 | { |
| 6290 | struct r5conf *conf = mddev->private; |
| 6291 | unsigned long new; |
| 6292 | |
| 6293 | if (!conf) |
| 6294 | return -ENODEV; |
| 6295 | |
| 6296 | if (len >= PAGE_SIZE) |
| 6297 | return -EINVAL; |
| 6298 | |
| 6299 | if (kstrtoul(page, 10, &new)) |
| 6300 | return -EINVAL; |
| 6301 | |
| 6302 | if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome) |
| 6303 | return -EINVAL; |
| 6304 | |
| 6305 | if (new != PARITY_DISABLE_RMW && |
| 6306 | new != PARITY_ENABLE_RMW && |
| 6307 | new != PARITY_PREFER_RMW) |
| 6308 | return -EINVAL; |
| 6309 | |
| 6310 | conf->rmw_level = new; |
| 6311 | return len; |
| 6312 | } |
| 6313 | |
| 6314 | static struct md_sysfs_entry |
| 6315 | raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR, |
| 6316 | raid5_show_rmw_level, |
| 6317 | raid5_store_rmw_level); |
| 6318 | |
| 6319 | |
| 6320 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6321 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6322 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6323 | struct r5conf *conf; |
| 6324 | int ret = 0; |
| 6325 | spin_lock(&mddev->lock); |
| 6326 | conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6327 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6328 | ret = sprintf(page, "%d\n", conf->bypass_threshold); |
| 6329 | spin_unlock(&mddev->lock); |
| 6330 | return ret; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6331 | } |
| 6332 | |
| 6333 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6334 | 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] | 6335 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6336 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6337 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6338 | int err; |
| 6339 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6340 | if (len >= PAGE_SIZE) |
| 6341 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6342 | if (kstrtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6343 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6344 | |
| 6345 | err = mddev_lock(mddev); |
| 6346 | if (err) |
| 6347 | return err; |
| 6348 | conf = mddev->private; |
| 6349 | if (!conf) |
| 6350 | err = -ENODEV; |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6351 | else if (new > conf->min_nr_stripes) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6352 | err = -EINVAL; |
| 6353 | else |
| 6354 | conf->bypass_threshold = new; |
| 6355 | mddev_unlock(mddev); |
| 6356 | return err ?: len; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6357 | } |
| 6358 | |
| 6359 | static struct md_sysfs_entry |
| 6360 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 6361 | S_IRUGO | S_IWUSR, |
| 6362 | raid5_show_preread_threshold, |
| 6363 | raid5_store_preread_threshold); |
| 6364 | |
| 6365 | static ssize_t |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6366 | raid5_show_skip_copy(struct mddev *mddev, char *page) |
| 6367 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6368 | struct r5conf *conf; |
| 6369 | int ret = 0; |
| 6370 | spin_lock(&mddev->lock); |
| 6371 | conf = mddev->private; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6372 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6373 | ret = sprintf(page, "%d\n", conf->skip_copy); |
| 6374 | spin_unlock(&mddev->lock); |
| 6375 | return ret; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6376 | } |
| 6377 | |
| 6378 | static ssize_t |
| 6379 | raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) |
| 6380 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6381 | struct r5conf *conf; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6382 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6383 | int err; |
| 6384 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6385 | if (len >= PAGE_SIZE) |
| 6386 | return -EINVAL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6387 | if (kstrtoul(page, 10, &new)) |
| 6388 | return -EINVAL; |
| 6389 | new = !!new; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6390 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6391 | err = mddev_lock(mddev); |
| 6392 | if (err) |
| 6393 | return err; |
| 6394 | conf = mddev->private; |
| 6395 | if (!conf) |
| 6396 | err = -ENODEV; |
| 6397 | else if (new != conf->skip_copy) { |
| 6398 | mddev_suspend(mddev); |
| 6399 | conf->skip_copy = new; |
| 6400 | if (new) |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 6401 | mddev->queue->backing_dev_info->capabilities |= |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6402 | BDI_CAP_STABLE_WRITES; |
| 6403 | else |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 6404 | mddev->queue->backing_dev_info->capabilities &= |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6405 | ~BDI_CAP_STABLE_WRITES; |
| 6406 | mddev_resume(mddev); |
| 6407 | } |
| 6408 | mddev_unlock(mddev); |
| 6409 | return err ?: len; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6410 | } |
| 6411 | |
| 6412 | static struct md_sysfs_entry |
| 6413 | raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, |
| 6414 | raid5_show_skip_copy, |
| 6415 | raid5_store_skip_copy); |
| 6416 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6417 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6418 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6419 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6420 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6421 | if (conf) |
| 6422 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 6423 | else |
| 6424 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6425 | } |
| 6426 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6427 | static struct md_sysfs_entry |
| 6428 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6429 | |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6430 | static ssize_t |
| 6431 | raid5_show_group_thread_cnt(struct mddev *mddev, char *page) |
| 6432 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6433 | struct r5conf *conf; |
| 6434 | int ret = 0; |
| 6435 | spin_lock(&mddev->lock); |
| 6436 | conf = mddev->private; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6437 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6438 | ret = sprintf(page, "%d\n", conf->worker_cnt_per_group); |
| 6439 | spin_unlock(&mddev->lock); |
| 6440 | return ret; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6441 | } |
| 6442 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6443 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 6444 | int *group_cnt, |
| 6445 | int *worker_cnt_per_group, |
| 6446 | struct r5worker_group **worker_groups); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6447 | static ssize_t |
| 6448 | raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) |
| 6449 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6450 | struct r5conf *conf; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6451 | unsigned long new; |
| 6452 | int err; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6453 | struct r5worker_group *new_groups, *old_groups; |
| 6454 | int group_cnt, worker_cnt_per_group; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6455 | |
| 6456 | if (len >= PAGE_SIZE) |
| 6457 | return -EINVAL; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6458 | if (kstrtoul(page, 10, &new)) |
| 6459 | return -EINVAL; |
| 6460 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6461 | err = mddev_lock(mddev); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6462 | if (err) |
| 6463 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6464 | conf = mddev->private; |
| 6465 | if (!conf) |
| 6466 | err = -ENODEV; |
| 6467 | else if (new != conf->worker_cnt_per_group) { |
| 6468 | mddev_suspend(mddev); |
| 6469 | |
| 6470 | old_groups = conf->worker_groups; |
| 6471 | if (old_groups) |
| 6472 | flush_workqueue(raid5_wq); |
| 6473 | |
| 6474 | err = alloc_thread_groups(conf, new, |
| 6475 | &group_cnt, &worker_cnt_per_group, |
| 6476 | &new_groups); |
| 6477 | if (!err) { |
| 6478 | spin_lock_irq(&conf->device_lock); |
| 6479 | conf->group_cnt = group_cnt; |
| 6480 | conf->worker_cnt_per_group = worker_cnt_per_group; |
| 6481 | conf->worker_groups = new_groups; |
| 6482 | spin_unlock_irq(&conf->device_lock); |
| 6483 | |
| 6484 | if (old_groups) |
| 6485 | kfree(old_groups[0].workers); |
| 6486 | kfree(old_groups); |
| 6487 | } |
| 6488 | mddev_resume(mddev); |
| 6489 | } |
| 6490 | mddev_unlock(mddev); |
| 6491 | |
| 6492 | return err ?: len; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6493 | } |
| 6494 | |
| 6495 | static struct md_sysfs_entry |
| 6496 | raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, |
| 6497 | raid5_show_group_thread_cnt, |
| 6498 | raid5_store_group_thread_cnt); |
| 6499 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6500 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6501 | &raid5_stripecache_size.attr, |
| 6502 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6503 | &raid5_preread_bypass_threshold.attr, |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6504 | &raid5_group_thread_cnt.attr, |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6505 | &raid5_skip_copy.attr, |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6506 | &raid5_rmw_level.attr, |
Song Liu | 2c7da14 | 2016-11-17 15:24:41 -0800 | [diff] [blame] | 6507 | &r5c_journal_mode.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6508 | NULL, |
| 6509 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6510 | static struct attribute_group raid5_attrs_group = { |
| 6511 | .name = NULL, |
| 6512 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6513 | }; |
| 6514 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6515 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 6516 | int *group_cnt, |
| 6517 | int *worker_cnt_per_group, |
| 6518 | struct r5worker_group **worker_groups) |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6519 | { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6520 | int i, j, k; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6521 | ssize_t size; |
| 6522 | struct r5worker *workers; |
| 6523 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6524 | *worker_cnt_per_group = cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6525 | if (cnt == 0) { |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6526 | *group_cnt = 0; |
| 6527 | *worker_groups = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6528 | return 0; |
| 6529 | } |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6530 | *group_cnt = num_possible_nodes(); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6531 | size = sizeof(struct r5worker) * cnt; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6532 | workers = kzalloc(size * *group_cnt, GFP_NOIO); |
| 6533 | *worker_groups = kzalloc(sizeof(struct r5worker_group) * |
| 6534 | *group_cnt, GFP_NOIO); |
| 6535 | if (!*worker_groups || !workers) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6536 | kfree(workers); |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6537 | kfree(*worker_groups); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6538 | return -ENOMEM; |
| 6539 | } |
| 6540 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6541 | for (i = 0; i < *group_cnt; i++) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6542 | struct r5worker_group *group; |
| 6543 | |
NeilBrown | 0c775d5 | 2013-11-25 11:12:43 +1100 | [diff] [blame] | 6544 | group = &(*worker_groups)[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6545 | INIT_LIST_HEAD(&group->handle_list); |
| 6546 | group->conf = conf; |
| 6547 | group->workers = workers + i * cnt; |
| 6548 | |
| 6549 | for (j = 0; j < cnt; j++) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6550 | struct r5worker *worker = group->workers + j; |
| 6551 | worker->group = group; |
| 6552 | INIT_WORK(&worker->work, raid5_do_work); |
| 6553 | |
| 6554 | for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) |
| 6555 | INIT_LIST_HEAD(worker->temp_inactive_list + k); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6556 | } |
| 6557 | } |
| 6558 | |
| 6559 | return 0; |
| 6560 | } |
| 6561 | |
| 6562 | static void free_thread_groups(struct r5conf *conf) |
| 6563 | { |
| 6564 | if (conf->worker_groups) |
| 6565 | kfree(conf->worker_groups[0].workers); |
| 6566 | kfree(conf->worker_groups); |
| 6567 | conf->worker_groups = NULL; |
| 6568 | } |
| 6569 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6570 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6571 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6572 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6573 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6574 | |
| 6575 | if (!sectors) |
| 6576 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6577 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 6578 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6579 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6580 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 6581 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
| 6582 | sectors &= ~((sector_t)conf->prev_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6583 | return sectors * (raid_disks - conf->max_degraded); |
| 6584 | } |
| 6585 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6586 | static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 6587 | { |
| 6588 | safe_put_page(percpu->spare_page); |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6589 | if (percpu->scribble) |
| 6590 | flex_array_free(percpu->scribble); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6591 | percpu->spare_page = NULL; |
| 6592 | percpu->scribble = NULL; |
| 6593 | } |
| 6594 | |
| 6595 | static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 6596 | { |
| 6597 | if (conf->level == 6 && !percpu->spare_page) |
| 6598 | percpu->spare_page = alloc_page(GFP_KERNEL); |
| 6599 | if (!percpu->scribble) |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6600 | percpu->scribble = scribble_alloc(max(conf->raid_disks, |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 6601 | conf->previous_raid_disks), |
| 6602 | max(conf->chunk_sectors, |
| 6603 | conf->prev_chunk_sectors) |
| 6604 | / STRIPE_SECTORS, |
| 6605 | GFP_KERNEL); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6606 | |
| 6607 | if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) { |
| 6608 | free_scratch_buffer(conf, percpu); |
| 6609 | return -ENOMEM; |
| 6610 | } |
| 6611 | |
| 6612 | return 0; |
| 6613 | } |
| 6614 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6615 | static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node) |
| 6616 | { |
| 6617 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
| 6618 | |
| 6619 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); |
| 6620 | return 0; |
| 6621 | } |
| 6622 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6623 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6624 | { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6625 | if (!conf->percpu) |
| 6626 | return; |
| 6627 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6628 | cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6629 | free_percpu(conf->percpu); |
| 6630 | } |
| 6631 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6632 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6633 | { |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 6634 | int i; |
| 6635 | |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 6636 | if (conf->log) |
| 6637 | r5l_exit_log(conf->log); |
Shaohua Li | 30c8946 | 2016-09-21 09:07:13 -0700 | [diff] [blame] | 6638 | if (conf->shrinker.nr_deferred) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6639 | unregister_shrinker(&conf->shrinker); |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 6640 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6641 | free_thread_groups(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6642 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6643 | raid5_free_percpu(conf); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 6644 | for (i = 0; i < conf->pool_size; i++) |
| 6645 | if (conf->disks[i].extra_page) |
| 6646 | put_page(conf->disks[i].extra_page); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6647 | kfree(conf->disks); |
| 6648 | kfree(conf->stripe_hashtbl); |
| 6649 | kfree(conf); |
| 6650 | } |
| 6651 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6652 | 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] | 6653 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6654 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6655 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 6656 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6657 | if (alloc_scratch_buffer(conf, percpu)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6658 | pr_warn("%s: failed memory allocation for cpu%u\n", |
| 6659 | __func__, cpu); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6660 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6661 | } |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6662 | return 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6663 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6664 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6665 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6666 | { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6667 | int err = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6668 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6669 | conf->percpu = alloc_percpu(struct raid5_percpu); |
| 6670 | if (!conf->percpu) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6671 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6672 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6673 | err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 6674 | if (!err) { |
| 6675 | conf->scribble_disks = max(conf->raid_disks, |
| 6676 | conf->previous_raid_disks); |
| 6677 | conf->scribble_sectors = max(conf->chunk_sectors, |
| 6678 | conf->prev_chunk_sectors); |
| 6679 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6680 | return err; |
| 6681 | } |
| 6682 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6683 | static unsigned long raid5_cache_scan(struct shrinker *shrink, |
| 6684 | struct shrink_control *sc) |
| 6685 | { |
| 6686 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6687 | unsigned long ret = SHRINK_STOP; |
| 6688 | |
| 6689 | if (mutex_trylock(&conf->cache_size_mutex)) { |
| 6690 | ret= 0; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 6691 | while (ret < sc->nr_to_scan && |
| 6692 | conf->max_nr_stripes > conf->min_nr_stripes) { |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6693 | if (drop_one_stripe(conf) == 0) { |
| 6694 | ret = SHRINK_STOP; |
| 6695 | break; |
| 6696 | } |
| 6697 | ret++; |
| 6698 | } |
| 6699 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6700 | } |
| 6701 | return ret; |
| 6702 | } |
| 6703 | |
| 6704 | static unsigned long raid5_cache_count(struct shrinker *shrink, |
| 6705 | struct shrink_control *sc) |
| 6706 | { |
| 6707 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
| 6708 | |
| 6709 | if (conf->max_nr_stripes < conf->min_nr_stripes) |
| 6710 | /* unlikely, but not impossible */ |
| 6711 | return 0; |
| 6712 | return conf->max_nr_stripes - conf->min_nr_stripes; |
| 6713 | } |
| 6714 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6715 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6716 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6717 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6718 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6719 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6720 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6721 | char pers_name[6]; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6722 | int i; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6723 | int group_cnt, worker_cnt_per_group; |
| 6724 | struct r5worker_group *new_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6725 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6726 | if (mddev->new_level != 5 |
| 6727 | && mddev->new_level != 4 |
| 6728 | && mddev->new_level != 6) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6729 | pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
| 6730 | mdname(mddev), mddev->new_level); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6731 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6732 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6733 | if ((mddev->new_level == 5 |
| 6734 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 6735 | (mddev->new_level == 6 |
| 6736 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6737 | pr_warn("md/raid:%s: layout %d not supported\n", |
| 6738 | mdname(mddev), mddev->new_layout); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6739 | return ERR_PTR(-EIO); |
| 6740 | } |
| 6741 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6742 | pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
| 6743 | mdname(mddev), mddev->raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6744 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6745 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6746 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6747 | if (!mddev->new_chunk_sectors || |
| 6748 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 6749 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6750 | pr_warn("md/raid:%s: invalid chunk size %d\n", |
| 6751 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6752 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6753 | } |
| 6754 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6755 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6756 | if (conf == NULL) |
| 6757 | goto abort; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6758 | /* Don't enable multi-threading by default*/ |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6759 | if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group, |
| 6760 | &new_group)) { |
| 6761 | conf->group_cnt = group_cnt; |
| 6762 | conf->worker_cnt_per_group = worker_cnt_per_group; |
| 6763 | conf->worker_groups = new_group; |
| 6764 | } else |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6765 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6766 | spin_lock_init(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 6767 | seqcount_init(&conf->gen_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6768 | mutex_init(&conf->cache_size_mutex); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 6769 | init_waitqueue_head(&conf->wait_for_quiescent); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 6770 | init_waitqueue_head(&conf->wait_for_stripe); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6771 | init_waitqueue_head(&conf->wait_for_overlap); |
| 6772 | INIT_LIST_HEAD(&conf->handle_list); |
| 6773 | INIT_LIST_HEAD(&conf->hold_list); |
| 6774 | INIT_LIST_HEAD(&conf->delayed_list); |
| 6775 | INIT_LIST_HEAD(&conf->bitmap_list); |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 6776 | bio_list_init(&conf->return_bi); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6777 | init_llist_head(&conf->released_stripes); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6778 | atomic_set(&conf->active_stripes, 0); |
| 6779 | atomic_set(&conf->preread_active_stripes, 0); |
| 6780 | atomic_set(&conf->active_aligned_reads, 0); |
Shaohua Li | 765d704 | 2017-01-04 09:33:23 -0800 | [diff] [blame] | 6781 | bio_list_init(&conf->pending_bios); |
| 6782 | spin_lock_init(&conf->pending_bios_lock); |
| 6783 | conf->batch_bio_dispatch = true; |
| 6784 | rdev_for_each(rdev, mddev) { |
| 6785 | if (test_bit(Journal, &rdev->flags)) |
| 6786 | continue; |
| 6787 | if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) { |
| 6788 | conf->batch_bio_dispatch = false; |
| 6789 | break; |
| 6790 | } |
| 6791 | } |
| 6792 | |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6793 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 6794 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6795 | |
| 6796 | conf->raid_disks = mddev->raid_disks; |
| 6797 | if (mddev->reshape_position == MaxSector) |
| 6798 | conf->previous_raid_disks = mddev->raid_disks; |
| 6799 | else |
| 6800 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6801 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6802 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6803 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6804 | GFP_KERNEL); |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 6805 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6806 | if (!conf->disks) |
| 6807 | goto abort; |
| 6808 | |
Song Liu | d7bd398 | 2016-11-23 22:50:39 -0800 | [diff] [blame] | 6809 | for (i = 0; i < max_disks; i++) { |
| 6810 | conf->disks[i].extra_page = alloc_page(GFP_KERNEL); |
| 6811 | if (!conf->disks[i].extra_page) |
| 6812 | goto abort; |
| 6813 | } |
| 6814 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6815 | conf->mddev = mddev; |
| 6816 | |
| 6817 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 6818 | goto abort; |
| 6819 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6820 | /* We init hash_locks[0] separately to that it can be used |
| 6821 | * as the reference lock in the spin_lock_nest_lock() call |
| 6822 | * in lock_all_device_hash_locks_irq in order to convince |
| 6823 | * lockdep that we know what we are doing. |
| 6824 | */ |
| 6825 | spin_lock_init(conf->hash_locks); |
| 6826 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6827 | spin_lock_init(conf->hash_locks + i); |
| 6828 | |
| 6829 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6830 | INIT_LIST_HEAD(conf->inactive_list + i); |
| 6831 | |
| 6832 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6833 | INIT_LIST_HEAD(conf->temp_inactive_list + i); |
| 6834 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 6835 | atomic_set(&conf->r5c_cached_full_stripes, 0); |
| 6836 | INIT_LIST_HEAD(&conf->r5c_full_stripe_list); |
| 6837 | atomic_set(&conf->r5c_cached_partial_stripes, 0); |
| 6838 | INIT_LIST_HEAD(&conf->r5c_partial_stripe_list); |
Shaohua Li | e33fbb9 | 2017-02-10 16:18:09 -0800 | [diff] [blame] | 6839 | atomic_set(&conf->r5c_flushing_full_stripes, 0); |
| 6840 | atomic_set(&conf->r5c_flushing_partial_stripes, 0); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 6841 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6842 | conf->level = mddev->new_level; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6843 | conf->chunk_sectors = mddev->new_chunk_sectors; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6844 | if (raid5_alloc_percpu(conf) != 0) |
| 6845 | goto abort; |
| 6846 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6847 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6848 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6849 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6850 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6851 | if (raid_disk >= max_disks |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 6852 | || raid_disk < 0 || test_bit(Journal, &rdev->flags)) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6853 | continue; |
| 6854 | disk = conf->disks + raid_disk; |
| 6855 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6856 | if (test_bit(Replacement, &rdev->flags)) { |
| 6857 | if (disk->replacement) |
| 6858 | goto abort; |
| 6859 | disk->replacement = rdev; |
| 6860 | } else { |
| 6861 | if (disk->rdev) |
| 6862 | goto abort; |
| 6863 | disk->rdev = rdev; |
| 6864 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6865 | |
| 6866 | if (test_bit(In_sync, &rdev->flags)) { |
| 6867 | char b[BDEVNAME_SIZE]; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6868 | pr_info("md/raid:%s: device %s operational as raid disk %d\n", |
| 6869 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 6870 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6871 | /* Cannot rely on bitmap to complete recovery */ |
| 6872 | conf->fullsync = 1; |
| 6873 | } |
| 6874 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6875 | conf->level = mddev->new_level; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6876 | if (conf->level == 6) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6877 | conf->max_degraded = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6878 | if (raid6_call.xor_syndrome) |
| 6879 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 6880 | else |
| 6881 | conf->rmw_level = PARITY_DISABLE_RMW; |
| 6882 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6883 | conf->max_degraded = 1; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6884 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 6885 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6886 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6887 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 6888 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6889 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 6890 | conf->prev_algo = mddev->layout; |
NeilBrown | 5cac6bc | 2015-07-17 12:17:50 +1000 | [diff] [blame] | 6891 | } else { |
| 6892 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 6893 | conf->prev_algo = conf->algorithm; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 6894 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6895 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6896 | conf->min_nr_stripes = NR_STRIPES; |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 6897 | if (mddev->reshape_position != MaxSector) { |
| 6898 | int stripes = max_t(int, |
| 6899 | ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4, |
| 6900 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4); |
| 6901 | conf->min_nr_stripes = max(NR_STRIPES, stripes); |
| 6902 | if (conf->min_nr_stripes != NR_STRIPES) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6903 | pr_info("md/raid:%s: force stripe size %d for reshape\n", |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 6904 | mdname(mddev), conf->min_nr_stripes); |
| 6905 | } |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6906 | memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6907 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6908 | atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6909 | if (grow_stripes(conf, conf->min_nr_stripes)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6910 | pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 6911 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6912 | goto abort; |
| 6913 | } else |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6914 | pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6915 | /* |
| 6916 | * Losing a stripe head costs more than the time to refill it, |
| 6917 | * it reduces the queue depth and so can hurt throughput. |
| 6918 | * So set it rather large, scaled by number of devices. |
| 6919 | */ |
| 6920 | conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4; |
| 6921 | conf->shrinker.scan_objects = raid5_cache_scan; |
| 6922 | conf->shrinker.count_objects = raid5_cache_count; |
| 6923 | conf->shrinker.batch = 128; |
| 6924 | conf->shrinker.flags = 0; |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 6925 | if (register_shrinker(&conf->shrinker)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6926 | pr_warn("md/raid:%s: couldn't register shrinker.\n", |
| 6927 | mdname(mddev)); |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 6928 | goto abort; |
| 6929 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6930 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6931 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 6932 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6933 | if (!conf->thread) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6934 | pr_warn("md/raid:%s: couldn't allocate thread.\n", |
| 6935 | mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6936 | goto abort; |
| 6937 | } |
| 6938 | |
| 6939 | return conf; |
| 6940 | |
| 6941 | abort: |
| 6942 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6943 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6944 | return ERR_PTR(-EIO); |
| 6945 | } else |
| 6946 | return ERR_PTR(-ENOMEM); |
| 6947 | } |
| 6948 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6949 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 6950 | { |
| 6951 | switch (algo) { |
| 6952 | case ALGORITHM_PARITY_0: |
| 6953 | if (raid_disk < max_degraded) |
| 6954 | return 1; |
| 6955 | break; |
| 6956 | case ALGORITHM_PARITY_N: |
| 6957 | if (raid_disk >= raid_disks - max_degraded) |
| 6958 | return 1; |
| 6959 | break; |
| 6960 | case ALGORITHM_PARITY_0_6: |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6961 | if (raid_disk == 0 || |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6962 | raid_disk == raid_disks - 1) |
| 6963 | return 1; |
| 6964 | break; |
| 6965 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 6966 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 6967 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 6968 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 6969 | if (raid_disk == raid_disks - 1) |
| 6970 | return 1; |
| 6971 | } |
| 6972 | return 0; |
| 6973 | } |
| 6974 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 6975 | static int raid5_run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6976 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6977 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 6978 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6979 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6980 | struct md_rdev *rdev; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 6981 | struct md_rdev *journal_dev = NULL; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6982 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6983 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6984 | long long min_offset_diff = 0; |
| 6985 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6986 | |
Andre Noll | 8c6ac868 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 6987 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6988 | pr_notice("md/raid:%s: not clean -- starting background reconstruction\n", |
| 6989 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6990 | |
| 6991 | rdev_for_each(rdev, mddev) { |
| 6992 | long long diff; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 6993 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 6994 | if (test_bit(Journal, &rdev->flags)) { |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 6995 | journal_dev = rdev; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 6996 | continue; |
| 6997 | } |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6998 | if (rdev->raid_disk < 0) |
| 6999 | continue; |
| 7000 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 7001 | if (first) { |
| 7002 | min_offset_diff = diff; |
| 7003 | first = 0; |
| 7004 | } else if (mddev->reshape_backwards && |
| 7005 | diff < min_offset_diff) |
| 7006 | min_offset_diff = diff; |
| 7007 | else if (!mddev->reshape_backwards && |
| 7008 | diff > min_offset_diff) |
| 7009 | min_offset_diff = diff; |
| 7010 | } |
| 7011 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7012 | if (mddev->reshape_position != MaxSector) { |
| 7013 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7014 | * Difficulties arise if the stripe we would write to |
| 7015 | * next is at or after the stripe we would read from next. |
| 7016 | * For a reshape that changes the number of devices, this |
| 7017 | * is only possible for a very short time, and mdadm makes |
| 7018 | * sure that time appears to have past before assembling |
| 7019 | * the array. So we fail if that time hasn't passed. |
| 7020 | * For a reshape that keeps the number of devices the same |
| 7021 | * mdadm must be monitoring the reshape can keeping the |
| 7022 | * critical areas read-only and backed up. It will start |
| 7023 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7024 | */ |
| 7025 | sector_t here_new, here_old; |
| 7026 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 7027 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7028 | int chunk_sectors; |
| 7029 | int new_data_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7030 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7031 | if (journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7032 | pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n", |
| 7033 | mdname(mddev)); |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7034 | return -EINVAL; |
| 7035 | } |
| 7036 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7037 | if (mddev->new_level != mddev->level) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7038 | pr_warn("md/raid:%s: unsupported reshape required - aborting.\n", |
| 7039 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7040 | return -EINVAL; |
| 7041 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7042 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 7043 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7044 | * further up in new geometry must map after here in old |
| 7045 | * geometry. |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7046 | * If the chunk sizes are different, then as we perform reshape |
| 7047 | * in units of the largest of the two, reshape_position needs |
| 7048 | * be a multiple of the largest chunk size times new data disks. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7049 | */ |
| 7050 | here_new = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7051 | chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors); |
| 7052 | new_data_disks = mddev->raid_disks - max_degraded; |
| 7053 | if (sector_div(here_new, chunk_sectors * new_data_disks)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7054 | pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n", |
| 7055 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7056 | return -EINVAL; |
| 7057 | } |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7058 | reshape_offset = here_new * chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7059 | /* here_new is the stripe we will write to */ |
| 7060 | here_old = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7061 | sector_div(here_old, chunk_sectors * (old_disks-max_degraded)); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7062 | /* here_old is the first stripe that we might need to read |
| 7063 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7064 | if (mddev->delta_disks == 0) { |
| 7065 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7066 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7067 | * and taking constant backups. |
| 7068 | * mdadm always starts a situation like this in |
| 7069 | * readonly mode so it can take control before |
| 7070 | * allowing any writes. So just check for that. |
| 7071 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7072 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 7073 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 7074 | /* not really in-place - so OK */; |
| 7075 | else if (mddev->ro == 0) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7076 | pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n", |
| 7077 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 7078 | return -EINVAL; |
| 7079 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7080 | } else if (mddev->reshape_backwards |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 7081 | ? (here_new * chunk_sectors + min_offset_diff <= |
| 7082 | here_old * chunk_sectors) |
| 7083 | : (here_new * chunk_sectors >= |
| 7084 | here_old * chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7085 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7086 | pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n", |
| 7087 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7088 | return -EINVAL; |
| 7089 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7090 | pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7091 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7092 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7093 | BUG_ON(mddev->level != mddev->new_level); |
| 7094 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7095 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7096 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7097 | } |
| 7098 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7099 | if (mddev->private == NULL) |
| 7100 | conf = setup_conf(mddev); |
| 7101 | else |
| 7102 | conf = mddev->private; |
| 7103 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7104 | if (IS_ERR(conf)) |
| 7105 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 7106 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 7107 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 7108 | if (!journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7109 | pr_warn("md/raid:%s: journal disk is missing, force array readonly\n", |
| 7110 | mdname(mddev)); |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 7111 | mddev->ro = 1; |
| 7112 | set_disk_ro(mddev->gendisk, 1); |
| 7113 | } else if (mddev->recovery_cp == MaxSector) |
| 7114 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
Shaohua Li | 7dde2ad | 2015-10-08 21:54:10 -0700 | [diff] [blame] | 7115 | } |
| 7116 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 7117 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7118 | mddev->thread = conf->thread; |
| 7119 | conf->thread = NULL; |
| 7120 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7121 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7122 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 7123 | i++) { |
| 7124 | rdev = conf->disks[i].rdev; |
| 7125 | if (!rdev && conf->disks[i].replacement) { |
| 7126 | /* The replacement is all we have yet */ |
| 7127 | rdev = conf->disks[i].replacement; |
| 7128 | conf->disks[i].replacement = NULL; |
| 7129 | clear_bit(Replacement, &rdev->flags); |
| 7130 | conf->disks[i].rdev = rdev; |
| 7131 | } |
| 7132 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7133 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7134 | if (conf->disks[i].replacement && |
| 7135 | conf->reshape_progress != MaxSector) { |
| 7136 | /* replacements and reshape simply do not mix. */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7137 | pr_warn("md: cannot handle concurrent replacement and reshape.\n"); |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7138 | goto abort; |
| 7139 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 7140 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7141 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 7142 | continue; |
| 7143 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7144 | /* This disc is not fully in-sync. However if it |
| 7145 | * just stored parity (beyond the recovery_offset), |
| 7146 | * when we don't need to be concerned about the |
| 7147 | * array being dirty. |
| 7148 | * When reshape goes 'backwards', we never have |
| 7149 | * partially completed devices, so we only need |
| 7150 | * to worry about reshape going forwards. |
| 7151 | */ |
| 7152 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 7153 | if (mddev->major_version == 0 && |
| 7154 | mddev->minor_version > 90) |
| 7155 | rdev->recovery_offset = reshape_offset; |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7156 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7157 | if (rdev->recovery_offset < reshape_offset) { |
| 7158 | /* We need to check old and new layout */ |
| 7159 | if (!only_parity(rdev->raid_disk, |
| 7160 | conf->algorithm, |
| 7161 | conf->raid_disks, |
| 7162 | conf->max_degraded)) |
| 7163 | continue; |
| 7164 | } |
| 7165 | if (!only_parity(rdev->raid_disk, |
| 7166 | conf->prev_algo, |
| 7167 | conf->previous_raid_disks, |
| 7168 | conf->max_degraded)) |
| 7169 | continue; |
| 7170 | dirty_parity_disks++; |
| 7171 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7172 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7173 | /* |
| 7174 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 7175 | */ |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7176 | mddev->degraded = raid5_calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7177 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 7178 | if (has_failed(conf)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7179 | pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7180 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7181 | goto abort; |
| 7182 | } |
| 7183 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7184 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7185 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7186 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 7187 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 7188 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7189 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7190 | if (mddev->ok_start_degraded) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7191 | pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n", |
| 7192 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7193 | else { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7194 | pr_crit("md/raid:%s: cannot start dirty degraded array.\n", |
| 7195 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 7196 | goto abort; |
| 7197 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7198 | } |
| 7199 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7200 | pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n", |
| 7201 | mdname(mddev), conf->level, |
| 7202 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 7203 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | |
| 7205 | print_raid5_conf(conf); |
| 7206 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7207 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7208 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7209 | atomic_set(&conf->reshape_stripes, 0); |
| 7210 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7211 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7212 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7213 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7214 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7215 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7216 | } |
| 7217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7218 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7219 | if (mddev->to_remove == &raid5_attrs_group) |
| 7220 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7221 | else if (mddev->kobj.sd && |
| 7222 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7223 | pr_warn("raid5: failed to create sysfs attributes for %s\n", |
| 7224 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7225 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 7226 | |
| 7227 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7228 | int chunk_size; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7229 | bool discard_supported = true; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7230 | /* read-ahead size must cover two whole stripes, which |
| 7231 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 7232 | * number of raid devices |
| 7233 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7234 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 7235 | int stripe = data_disks * |
| 7236 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 7237 | if (mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
| 7238 | mddev->queue->backing_dev_info->ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7239 | |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7240 | chunk_size = mddev->chunk_sectors << 9; |
| 7241 | blk_queue_io_min(mddev->queue, chunk_size); |
| 7242 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 7243 | (conf->raid_disks - conf->max_degraded)); |
Kent Overstreet | c78afc6 | 2013-07-11 22:39:53 -0700 | [diff] [blame] | 7244 | mddev->queue->limits.raid_partial_stripes_expensive = 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7245 | /* |
| 7246 | * We can only discard a whole stripe. It doesn't make sense to |
| 7247 | * discard data disk but write parity disk |
| 7248 | */ |
| 7249 | stripe = stripe * PAGE_SIZE; |
NeilBrown | 4ac6875 | 2012-11-19 13:11:26 +1100 | [diff] [blame] | 7250 | /* Round up to power of 2, as discard handling |
| 7251 | * currently assumes that */ |
| 7252 | while ((stripe-1) & stripe) |
| 7253 | stripe = (stripe | (stripe-1)) + 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7254 | mddev->queue->limits.discard_alignment = stripe; |
| 7255 | mddev->queue->limits.discard_granularity = stripe; |
Konstantin Khlebnikov | e8d7c33 | 2016-11-27 19:32:32 +0300 | [diff] [blame] | 7256 | |
| 7257 | /* |
| 7258 | * We use 16-bit counter of active stripes in bi_phys_segments |
| 7259 | * (minus one for over-loaded initialization) |
| 7260 | */ |
| 7261 | blk_queue_max_hw_sectors(mddev->queue, 0xfffe * STRIPE_SECTORS); |
| 7262 | blk_queue_max_discard_sectors(mddev->queue, |
| 7263 | 0xfffe * STRIPE_SECTORS); |
| 7264 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7265 | /* |
| 7266 | * unaligned part of discard request will be ignored, so can't |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 7267 | * guarantee discard_zeroes_data |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7268 | */ |
| 7269 | mddev->queue->limits.discard_zeroes_data = 0; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7270 | |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7271 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
| 7272 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7273 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7274 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7275 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7276 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7277 | rdev->new_data_offset << 9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7278 | /* |
| 7279 | * discard_zeroes_data is required, otherwise data |
| 7280 | * could be lost. Consider a scenario: discard a stripe |
| 7281 | * (the stripe could be inconsistent if |
| 7282 | * discard_zeroes_data is 0); write one disk of the |
| 7283 | * stripe (the stripe could be inconsistent again |
| 7284 | * depending on which disks are used to calculate |
| 7285 | * parity); the disk is broken; The stripe data of this |
| 7286 | * disk is lost. |
| 7287 | */ |
| 7288 | if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || |
| 7289 | !bdev_get_queue(rdev->bdev)-> |
| 7290 | limits.discard_zeroes_data) |
| 7291 | discard_supported = false; |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 7292 | /* Unfortunately, discard_zeroes_data is not currently |
| 7293 | * a guarantee - just a hint. So we only allow DISCARD |
| 7294 | * if the sysadmin has confirmed that only safe devices |
| 7295 | * are in use by setting a module parameter. |
| 7296 | */ |
| 7297 | if (!devices_handle_discard_safely) { |
| 7298 | if (discard_supported) { |
| 7299 | pr_info("md/raid456: discard support disabled due to uncertainty.\n"); |
| 7300 | pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n"); |
| 7301 | } |
| 7302 | discard_supported = false; |
| 7303 | } |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7304 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7305 | |
| 7306 | if (discard_supported && |
Jes Sorensen | e7597e6 | 2016-02-16 16:44:24 -0500 | [diff] [blame] | 7307 | mddev->queue->limits.max_discard_sectors >= (stripe >> 9) && |
| 7308 | mddev->queue->limits.discard_granularity >= stripe) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7309 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 7310 | mddev->queue); |
| 7311 | else |
| 7312 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, |
| 7313 | mddev->queue); |
Shaohua Li | 1dffddd | 2016-09-08 10:49:06 -0700 | [diff] [blame] | 7314 | |
| 7315 | blk_queue_max_hw_sectors(mddev->queue, UINT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7316 | } |
| 7317 | |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 7318 | if (journal_dev) { |
| 7319 | char b[BDEVNAME_SIZE]; |
| 7320 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7321 | pr_debug("md/raid:%s: using device %s as journal\n", |
| 7322 | mdname(mddev), bdevname(journal_dev->bdev, b)); |
Song Liu | 5aabf7c | 2016-11-17 15:24:44 -0800 | [diff] [blame] | 7323 | if (r5l_init_log(conf, journal_dev)) |
| 7324 | goto abort; |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 7325 | } |
| 7326 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7327 | return 0; |
| 7328 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7329 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 7330 | print_raid5_conf(conf); |
| 7331 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7332 | mddev->private = NULL; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7333 | 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] | 7334 | return -EIO; |
| 7335 | } |
| 7336 | |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7337 | static void raid5_free(struct mddev *mddev, void *priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7338 | { |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7339 | struct r5conf *conf = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7340 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7341 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7342 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7343 | } |
| 7344 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7345 | static void raid5_status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7346 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7347 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7348 | int i; |
| 7349 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7350 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7351 | conf->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7352 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
NeilBrown | 5fd1335 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 7353 | rcu_read_lock(); |
| 7354 | for (i = 0; i < conf->raid_disks; i++) { |
| 7355 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
| 7356 | seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 7357 | } |
| 7358 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7359 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7360 | } |
| 7361 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7362 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7363 | { |
| 7364 | int i; |
| 7365 | struct disk_info *tmp; |
| 7366 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7367 | pr_debug("RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7368 | if (!conf) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7369 | pr_debug("(conf==NULL)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7370 | return; |
| 7371 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7372 | pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level, |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7373 | conf->raid_disks, |
| 7374 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7375 | |
| 7376 | for (i = 0; i < conf->raid_disks; i++) { |
| 7377 | char b[BDEVNAME_SIZE]; |
| 7378 | tmp = conf->disks + i; |
| 7379 | if (tmp->rdev) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7380 | pr_debug(" disk %d, o:%d, dev:%s\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7381 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 7382 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7383 | } |
| 7384 | } |
| 7385 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7386 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7387 | { |
| 7388 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7389 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7390 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7391 | int count = 0; |
| 7392 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7393 | |
| 7394 | for (i = 0; i < conf->raid_disks; i++) { |
| 7395 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7396 | if (tmp->replacement |
| 7397 | && tmp->replacement->recovery_offset == MaxSector |
| 7398 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 7399 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 7400 | /* Replacement has just become active. */ |
| 7401 | if (!tmp->rdev |
| 7402 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 7403 | count++; |
| 7404 | if (tmp->rdev) { |
| 7405 | /* Replaced device not technically faulty, |
| 7406 | * but we need to be sure it gets removed |
| 7407 | * and never re-added. |
| 7408 | */ |
| 7409 | set_bit(Faulty, &tmp->rdev->flags); |
| 7410 | sysfs_notify_dirent_safe( |
| 7411 | tmp->rdev->sysfs_state); |
| 7412 | } |
| 7413 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 7414 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7415 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7416 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 7417 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7418 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7419 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7420 | } |
| 7421 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7422 | spin_lock_irqsave(&conf->device_lock, flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7423 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7424 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7425 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7426 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7427 | } |
| 7428 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7429 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7430 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7431 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7432 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7433 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7434 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7435 | struct disk_info *p = conf->disks + number; |
| 7436 | |
| 7437 | print_raid5_conf(conf); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7438 | if (test_bit(Journal, &rdev->flags) && conf->log) { |
| 7439 | struct r5l_log *log; |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7440 | /* |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7441 | * we can't wait pending write here, as this is called in |
| 7442 | * raid5d, wait will deadlock. |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7443 | */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7444 | if (atomic_read(&mddev->writes_pending)) |
| 7445 | return -EBUSY; |
| 7446 | log = conf->log; |
| 7447 | conf->log = NULL; |
| 7448 | synchronize_rcu(); |
| 7449 | r5l_exit_log(log); |
| 7450 | return 0; |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7451 | } |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7452 | if (rdev == p->rdev) |
| 7453 | rdevp = &p->rdev; |
| 7454 | else if (rdev == p->replacement) |
| 7455 | rdevp = &p->replacement; |
| 7456 | else |
| 7457 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7458 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7459 | if (number >= conf->raid_disks && |
| 7460 | conf->reshape_progress == MaxSector) |
| 7461 | clear_bit(In_sync, &rdev->flags); |
| 7462 | |
| 7463 | if (test_bit(In_sync, &rdev->flags) || |
| 7464 | atomic_read(&rdev->nr_pending)) { |
| 7465 | err = -EBUSY; |
| 7466 | goto abort; |
| 7467 | } |
| 7468 | /* Only remove non-faulty devices if recovery |
| 7469 | * isn't possible. |
| 7470 | */ |
| 7471 | if (!test_bit(Faulty, &rdev->flags) && |
| 7472 | mddev->recovery_disabled != conf->recovery_disabled && |
| 7473 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7474 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7475 | number < conf->raid_disks) { |
| 7476 | err = -EBUSY; |
| 7477 | goto abort; |
| 7478 | } |
| 7479 | *rdevp = NULL; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 7480 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
| 7481 | synchronize_rcu(); |
| 7482 | if (atomic_read(&rdev->nr_pending)) { |
| 7483 | /* lost the race, try later */ |
| 7484 | err = -EBUSY; |
| 7485 | *rdevp = rdev; |
| 7486 | } |
| 7487 | } |
| 7488 | if (p->replacement) { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7489 | /* We must have just cleared 'rdev' */ |
| 7490 | p->rdev = p->replacement; |
| 7491 | clear_bit(Replacement, &p->replacement->flags); |
| 7492 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 7493 | * but will never see neither - if they are careful |
| 7494 | */ |
| 7495 | p->replacement = NULL; |
| 7496 | clear_bit(WantReplacement, &rdev->flags); |
| 7497 | } else |
| 7498 | /* We might have just removed the Replacement as faulty- |
| 7499 | * clear the bit just in case |
| 7500 | */ |
| 7501 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7502 | abort: |
| 7503 | |
| 7504 | print_raid5_conf(conf); |
| 7505 | return err; |
| 7506 | } |
| 7507 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7508 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7509 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7510 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7511 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7512 | int disk; |
| 7513 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7514 | int first = 0; |
| 7515 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7516 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7517 | if (test_bit(Journal, &rdev->flags)) { |
| 7518 | char b[BDEVNAME_SIZE]; |
| 7519 | if (conf->log) |
| 7520 | return -EBUSY; |
| 7521 | |
| 7522 | rdev->raid_disk = 0; |
| 7523 | /* |
| 7524 | * The array is in readonly mode if journal is missing, so no |
| 7525 | * write requests running. We should be safe |
| 7526 | */ |
| 7527 | r5l_init_log(conf, rdev); |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7528 | pr_debug("md/raid:%s: using device %s as journal\n", |
| 7529 | mdname(mddev), bdevname(rdev->bdev, b)); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7530 | return 0; |
| 7531 | } |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 7532 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 7533 | return -EBUSY; |
| 7534 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 7535 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7536 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7537 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7538 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7539 | if (rdev->raid_disk >= 0) |
| 7540 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7541 | |
| 7542 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7543 | * find the disk ... but prefer rdev->saved_raid_disk |
| 7544 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7545 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7546 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7547 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7548 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7549 | first = rdev->saved_raid_disk; |
| 7550 | |
| 7551 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7552 | p = conf->disks + disk; |
| 7553 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7554 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7555 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7556 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7557 | if (rdev->saved_raid_disk != disk) |
| 7558 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 7559 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7560 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7561 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7562 | } |
| 7563 | for (disk = first; disk <= last; disk++) { |
| 7564 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7565 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 7566 | p->replacement == NULL) { |
| 7567 | clear_bit(In_sync, &rdev->flags); |
| 7568 | set_bit(Replacement, &rdev->flags); |
| 7569 | rdev->raid_disk = disk; |
| 7570 | err = 0; |
| 7571 | conf->fullsync = 1; |
| 7572 | rcu_assign_pointer(p->replacement, rdev); |
| 7573 | break; |
| 7574 | } |
| 7575 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7576 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7577 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7578 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7579 | } |
| 7580 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7581 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7582 | { |
| 7583 | /* no resync is happening, and there is enough space |
| 7584 | * on all devices, so we can resize. |
| 7585 | * We need to make sure resync covers any new space. |
| 7586 | * If the array is shrinking we should possibly wait until |
| 7587 | * any io in the removed space completes, but it hardly seems |
| 7588 | * worth it. |
| 7589 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7590 | sector_t newsize; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7591 | struct r5conf *conf = mddev->private; |
| 7592 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7593 | if (conf->log) |
| 7594 | return -EINVAL; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7595 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7596 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 7597 | if (mddev->external_size && |
| 7598 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 7599 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7600 | if (mddev->bitmap) { |
| 7601 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 7602 | if (ret) |
| 7603 | return ret; |
| 7604 | } |
| 7605 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7606 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7607 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 7608 | if (sectors > mddev->dev_sectors && |
| 7609 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7610 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7611 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7612 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7613 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 7614 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7615 | return 0; |
| 7616 | } |
| 7617 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7618 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7619 | { |
| 7620 | /* Can only proceed if there are plenty of stripe_heads. |
| 7621 | * We need a minimum of one full stripe,, and for sensible progress |
| 7622 | * it is best to have about 4 times that. |
| 7623 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 7624 | * allow for chunk sizes up to 256K, which is probably OK. |
| 7625 | * If the chunk size is greater, user-space should request more |
| 7626 | * stripe_heads first. |
| 7627 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7628 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7629 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7630 | > conf->min_nr_stripes || |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7631 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7632 | > conf->min_nr_stripes) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7633 | pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 7634 | mdname(mddev), |
| 7635 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 7636 | / STRIPE_SIZE)*4); |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7637 | return 0; |
| 7638 | } |
| 7639 | return 1; |
| 7640 | } |
| 7641 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7642 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7643 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7644 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7645 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7646 | if (conf->log) |
| 7647 | return -EINVAL; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7648 | if (mddev->delta_disks == 0 && |
| 7649 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7650 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7651 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 7652 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7653 | return -EINVAL; |
NeilBrown | fdcfbbb | 2013-07-04 16:38:16 +1000 | [diff] [blame] | 7654 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7655 | /* We might be able to shrink, but the devices must |
| 7656 | * be made bigger first. |
| 7657 | * For raid6, 4 is the minimum size. |
| 7658 | * Otherwise 2 is the minimum |
| 7659 | */ |
| 7660 | int min = 2; |
| 7661 | if (mddev->level == 6) |
| 7662 | min = 4; |
| 7663 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 7664 | return -EINVAL; |
| 7665 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7666 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7667 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7668 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7669 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 7670 | if (mddev->new_chunk_sectors > mddev->chunk_sectors || |
| 7671 | mddev->delta_disks > 0) |
| 7672 | if (resize_chunks(conf, |
| 7673 | conf->previous_raid_disks |
| 7674 | + max(0, mddev->delta_disks), |
| 7675 | max(mddev->new_chunk_sectors, |
| 7676 | mddev->chunk_sectors) |
| 7677 | ) < 0) |
| 7678 | return -ENOMEM; |
NeilBrown | e56108d6 | 2012-10-11 14:24:13 +1100 | [diff] [blame] | 7679 | return resize_stripes(conf, (conf->previous_raid_disks |
| 7680 | + mddev->delta_disks)); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7681 | } |
| 7682 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7683 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7684 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7685 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7686 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7687 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 7688 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7689 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7690 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7691 | return -EBUSY; |
| 7692 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7693 | if (!check_stripe_cache(mddev)) |
| 7694 | return -ENOSPC; |
| 7695 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 7696 | if (has_failed(conf)) |
| 7697 | return -EINVAL; |
| 7698 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7699 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7700 | if (!test_bit(In_sync, &rdev->flags) |
| 7701 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7702 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7703 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7704 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7705 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7706 | /* Not enough devices even to make a degraded array |
| 7707 | * of that size |
| 7708 | */ |
| 7709 | return -EINVAL; |
| 7710 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7711 | /* Refuse to reduce size of the array. Any reductions in |
| 7712 | * array size must be through explicit setting of array_size |
| 7713 | * attribute. |
| 7714 | */ |
| 7715 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 7716 | < mddev->array_sectors) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7717 | pr_warn("md/raid:%s: array size must be reduced before number of disks\n", |
| 7718 | mdname(mddev)); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7719 | return -EINVAL; |
| 7720 | } |
| 7721 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7722 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7723 | spin_lock_irq(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 7724 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7725 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7726 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7727 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 7728 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7729 | conf->prev_algo = conf->algorithm; |
| 7730 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7731 | conf->generation++; |
| 7732 | /* Code that selects data_offset needs to see the generation update |
| 7733 | * if reshape_progress has been set - so a memory barrier needed. |
| 7734 | */ |
| 7735 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7736 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7737 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 7738 | else |
| 7739 | conf->reshape_progress = 0; |
| 7740 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 7741 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7742 | spin_unlock_irq(&conf->device_lock); |
| 7743 | |
NeilBrown | 4d77e3b | 2013-08-27 15:57:47 +1000 | [diff] [blame] | 7744 | /* Now make sure any requests that proceeded on the assumption |
| 7745 | * the reshape wasn't running - like Discard or Read - have |
| 7746 | * completed. |
| 7747 | */ |
| 7748 | mddev_suspend(mddev); |
| 7749 | mddev_resume(mddev); |
| 7750 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7751 | /* Add some new drives, as many as will fit. |
| 7752 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 7753 | * Don't add devices if we are reducing the number of |
| 7754 | * devices in the array. This is because it is not possible |
| 7755 | * to correctly record the "partially reconstructed" state of |
| 7756 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7757 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7758 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7759 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7760 | if (rdev->raid_disk < 0 && |
| 7761 | !test_bit(Faulty, &rdev->flags)) { |
| 7762 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7763 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 7764 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7765 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 7766 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7767 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7768 | |
| 7769 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7770 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7771 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7772 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 7773 | && !test_bit(Faulty, &rdev->flags)) { |
| 7774 | /* This is a spare that was manually added */ |
| 7775 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7776 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7777 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7778 | /* When a reshape changes the number of devices, |
| 7779 | * ->degraded is measured against the larger of the |
| 7780 | * pre and post number of devices. |
| 7781 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7782 | spin_lock_irqsave(&conf->device_lock, flags); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7783 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7784 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 7785 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7786 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 7787 | mddev->reshape_position = conf->reshape_progress; |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 7788 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7789 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7790 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7791 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 7792 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7793 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7794 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7795 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7796 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7797 | if (!mddev->sync_thread) { |
| 7798 | mddev->recovery = 0; |
| 7799 | spin_lock_irq(&conf->device_lock); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7800 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7801 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7802 | mddev->new_chunk_sectors = |
| 7803 | conf->chunk_sectors = conf->prev_chunk_sectors; |
| 7804 | mddev->new_layout = conf->algorithm = conf->prev_algo; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7805 | rdev_for_each(rdev, mddev) |
| 7806 | rdev->new_data_offset = rdev->data_offset; |
| 7807 | smp_wmb(); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7808 | conf->generation --; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7809 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 7810 | mddev->reshape_position = MaxSector; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7811 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7812 | spin_unlock_irq(&conf->device_lock); |
| 7813 | return -EAGAIN; |
| 7814 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 7815 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7816 | md_wakeup_thread(mddev->sync_thread); |
| 7817 | md_new_event(mddev); |
| 7818 | return 0; |
| 7819 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7820 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7821 | /* This is called from the reshape thread and should make any |
| 7822 | * changes needed in 'conf' |
| 7823 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7824 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7825 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7826 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7827 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7828 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7829 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7830 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7831 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7832 | rdev_for_each(rdev, conf->mddev) |
| 7833 | rdev->data_offset = rdev->new_data_offset; |
| 7834 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7835 | conf->reshape_progress = MaxSector; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 7836 | conf->mddev->reshape_position = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7837 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 7838 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7839 | |
| 7840 | /* read-ahead size must cover two whole stripes, which is |
| 7841 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 7842 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7843 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7844 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7845 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7846 | / PAGE_SIZE); |
Jan Kara | dc3b17c | 2017-02-02 15:56:50 +0100 | [diff] [blame] | 7847 | if (conf->mddev->queue->backing_dev_info->ra_pages < 2 * stripe) |
| 7848 | conf->mddev->queue->backing_dev_info->ra_pages = 2 * stripe; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7849 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7850 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7851 | } |
| 7852 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7853 | /* This is called from the raid5d thread with mddev_lock held. |
| 7854 | * It makes config changes to the device. |
| 7855 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7856 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7857 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7858 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7859 | |
| 7860 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7861 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7862 | if (mddev->delta_disks > 0) { |
| 7863 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
Heinz Mauelshagen | fe67d19 | 2016-05-03 22:15:31 +0200 | [diff] [blame] | 7864 | if (mddev->queue) { |
| 7865 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 7866 | revalidate_disk(mddev->gendisk); |
| 7867 | } |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7868 | } else { |
| 7869 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 7870 | spin_lock_irq(&conf->device_lock); |
Song Liu | 2e38a37 | 2017-01-24 10:45:30 -0800 | [diff] [blame] | 7871 | mddev->degraded = raid5_calc_degraded(conf); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 7872 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7873 | for (d = conf->raid_disks ; |
| 7874 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 7875 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7876 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 7877 | if (rdev) |
| 7878 | clear_bit(In_sync, &rdev->flags); |
| 7879 | rdev = conf->disks[d].replacement; |
| 7880 | if (rdev) |
| 7881 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 7882 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7883 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7884 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7885 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7886 | mddev->reshape_position = MaxSector; |
| 7887 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7888 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7889 | } |
| 7890 | } |
| 7891 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7892 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7893 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7894 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7895 | |
| 7896 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 7897 | case 2: /* resume for a suspend */ |
| 7898 | wake_up(&conf->wait_for_overlap); |
| 7899 | break; |
| 7900 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7901 | case 1: /* stop all writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7902 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7903 | /* '2' tells resync/reshape to pause so that all |
| 7904 | * active stripes can drain |
| 7905 | */ |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 7906 | r5c_flush_cache(conf, INT_MAX); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7907 | conf->quiesce = 2; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 7908 | wait_event_cmd(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 7909 | atomic_read(&conf->active_stripes) == 0 && |
| 7910 | atomic_read(&conf->active_aligned_reads) == 0, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7911 | unlock_all_device_hash_locks_irq(conf), |
| 7912 | lock_all_device_hash_locks_irq(conf)); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7913 | conf->quiesce = 1; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7914 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7915 | /* allow reshape to continue */ |
| 7916 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7917 | break; |
| 7918 | |
| 7919 | case 0: /* re-enable writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7920 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7921 | conf->quiesce = 0; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 7922 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 7923 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7924 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7925 | break; |
| 7926 | } |
Shaohua Li | e6c033f | 2015-10-04 09:20:12 -0700 | [diff] [blame] | 7927 | r5l_quiesce(conf->log, state); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7928 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 7929 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7930 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 7931 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 7932 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 7933 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 7934 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7935 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 7936 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7937 | pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 7938 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7939 | return ERR_PTR(-EINVAL); |
| 7940 | } |
| 7941 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 7942 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 7943 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 7944 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7945 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 7946 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 7947 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 7948 | mddev->raid_disks += 1; |
| 7949 | mddev->delta_disks = 1; |
| 7950 | /* make sure it will be not marked as dirty */ |
| 7951 | mddev->recovery_cp = MaxSector; |
| 7952 | |
| 7953 | return setup_conf(mddev); |
| 7954 | } |
| 7955 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7956 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7957 | { |
| 7958 | int chunksect; |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 7959 | void *ret; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7960 | |
| 7961 | if (mddev->raid_disks != 2 || |
| 7962 | mddev->degraded > 1) |
| 7963 | return ERR_PTR(-EINVAL); |
| 7964 | |
| 7965 | /* Should check if there are write-behind devices? */ |
| 7966 | |
| 7967 | chunksect = 64*2; /* 64K by default */ |
| 7968 | |
| 7969 | /* The array must be an exact multiple of chunksize */ |
| 7970 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 7971 | chunksect >>= 1; |
| 7972 | |
| 7973 | if ((chunksect<<9) < STRIPE_SIZE) |
| 7974 | /* array size does not allow a suitable chunk size */ |
| 7975 | return ERR_PTR(-EINVAL); |
| 7976 | |
| 7977 | mddev->new_level = 5; |
| 7978 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7979 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7980 | |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 7981 | ret = setup_conf(mddev); |
Jes Sorensen | 32cd7cb | 2017-01-06 19:31:35 -0500 | [diff] [blame] | 7982 | if (!IS_ERR(ret)) |
Shaohua Li | 394ed8e | 2017-01-04 16:10:19 -0800 | [diff] [blame] | 7983 | mddev_clear_unsupported_flags(mddev, |
| 7984 | UNSUPPORTED_MDDEV_FLAGS); |
Shaohua Li | 6995f0b | 2016-12-08 15:48:17 -0800 | [diff] [blame] | 7985 | return ret; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7986 | } |
| 7987 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7988 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 7989 | { |
| 7990 | int new_layout; |
| 7991 | |
| 7992 | switch (mddev->layout) { |
| 7993 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 7994 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 7995 | break; |
| 7996 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 7997 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 7998 | break; |
| 7999 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 8000 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 8001 | break; |
| 8002 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 8003 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 8004 | break; |
| 8005 | case ALGORITHM_PARITY_0_6: |
| 8006 | new_layout = ALGORITHM_PARITY_0; |
| 8007 | break; |
| 8008 | case ALGORITHM_PARITY_N: |
| 8009 | new_layout = ALGORITHM_PARITY_N; |
| 8010 | break; |
| 8011 | default: |
| 8012 | return ERR_PTR(-EINVAL); |
| 8013 | } |
| 8014 | mddev->new_level = 5; |
| 8015 | mddev->new_layout = new_layout; |
| 8016 | mddev->delta_disks = -1; |
| 8017 | mddev->raid_disks -= 1; |
| 8018 | return setup_conf(mddev); |
| 8019 | } |
| 8020 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8021 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8022 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8023 | /* For a 2-drive array, the layout and chunk size can be changed |
| 8024 | * immediately as not restriping is needed. |
| 8025 | * For larger arrays we record the new value - after validation |
| 8026 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8027 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 8028 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8029 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8030 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8031 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8032 | return -EINVAL; |
| 8033 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 8034 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8035 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8036 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8037 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8038 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8039 | /* not factor of array size */ |
| 8040 | return -EINVAL; |
| 8041 | } |
| 8042 | |
| 8043 | /* They look valid */ |
| 8044 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8045 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8046 | /* can make the change immediately */ |
| 8047 | if (mddev->new_layout >= 0) { |
| 8048 | conf->algorithm = mddev->new_layout; |
| 8049 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8050 | } |
| 8051 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8052 | conf->chunk_sectors = new_chunk ; |
| 8053 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8054 | } |
Shaohua Li | 2953079 | 2016-12-08 15:48:19 -0800 | [diff] [blame] | 8055 | set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8056 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8057 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8058 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8059 | } |
| 8060 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8061 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8062 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8063 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8064 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8065 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8066 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8067 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 8068 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8069 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8070 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8071 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 8072 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8073 | /* not factor of array size */ |
| 8074 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8075 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 8076 | |
| 8077 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8078 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 8079 | } |
| 8080 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8081 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8082 | { |
| 8083 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8084 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8085 | * raid1 - if there are two drives. We need to know the chunk size |
| 8086 | * raid4 - trivial - just use a raid4 layout. |
| 8087 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8088 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8089 | if (mddev->level == 0) |
| 8090 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8091 | if (mddev->level == 1) |
| 8092 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 8093 | if (mddev->level == 4) { |
| 8094 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 8095 | mddev->new_level = 5; |
| 8096 | return setup_conf(mddev); |
| 8097 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 8098 | if (mddev->level == 6) |
| 8099 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8100 | |
| 8101 | return ERR_PTR(-EINVAL); |
| 8102 | } |
| 8103 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8104 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8105 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8106 | /* raid4 can take over: |
| 8107 | * raid0 - if there is only one strip zone |
| 8108 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8109 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 8110 | if (mddev->level == 0) |
| 8111 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8112 | if (mddev->level == 5 && |
| 8113 | mddev->layout == ALGORITHM_PARITY_N) { |
| 8114 | mddev->new_layout = 0; |
| 8115 | mddev->new_level = 4; |
| 8116 | return setup_conf(mddev); |
| 8117 | } |
| 8118 | return ERR_PTR(-EINVAL); |
| 8119 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8120 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8121 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8122 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 8123 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8124 | { |
| 8125 | /* Currently can only take over a raid5. We map the |
| 8126 | * personality to an equivalent raid6 personality |
| 8127 | * with the Q block at the end. |
| 8128 | */ |
| 8129 | int new_layout; |
| 8130 | |
| 8131 | if (mddev->pers != &raid5_personality) |
| 8132 | return ERR_PTR(-EINVAL); |
| 8133 | if (mddev->degraded > 1) |
| 8134 | return ERR_PTR(-EINVAL); |
| 8135 | if (mddev->raid_disks > 253) |
| 8136 | return ERR_PTR(-EINVAL); |
| 8137 | if (mddev->raid_disks < 3) |
| 8138 | return ERR_PTR(-EINVAL); |
| 8139 | |
| 8140 | switch (mddev->layout) { |
| 8141 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 8142 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 8143 | break; |
| 8144 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 8145 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 8146 | break; |
| 8147 | case ALGORITHM_LEFT_SYMMETRIC: |
| 8148 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 8149 | break; |
| 8150 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 8151 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 8152 | break; |
| 8153 | case ALGORITHM_PARITY_0: |
| 8154 | new_layout = ALGORITHM_PARITY_0_6; |
| 8155 | break; |
| 8156 | case ALGORITHM_PARITY_N: |
| 8157 | new_layout = ALGORITHM_PARITY_N; |
| 8158 | break; |
| 8159 | default: |
| 8160 | return ERR_PTR(-EINVAL); |
| 8161 | } |
| 8162 | mddev->new_level = 6; |
| 8163 | mddev->new_layout = new_layout; |
| 8164 | mddev->delta_disks = 1; |
| 8165 | mddev->raid_disks += 1; |
| 8166 | return setup_conf(mddev); |
| 8167 | } |
| 8168 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8169 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8170 | { |
| 8171 | .name = "raid6", |
| 8172 | .level = 6, |
| 8173 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8174 | .make_request = raid5_make_request, |
| 8175 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8176 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8177 | .status = raid5_status, |
| 8178 | .error_handler = raid5_error, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8179 | .hot_add_disk = raid5_add_disk, |
| 8180 | .hot_remove_disk= raid5_remove_disk, |
| 8181 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8182 | .sync_request = raid5_sync_request, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8183 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8184 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 8185 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 8186 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8187 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8188 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8189 | .takeover = raid6_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8190 | .congested = raid5_congested, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8191 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8192 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8193 | { |
| 8194 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8195 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8196 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8197 | .make_request = raid5_make_request, |
| 8198 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8199 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8200 | .status = raid5_status, |
| 8201 | .error_handler = raid5_error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8202 | .hot_add_disk = raid5_add_disk, |
| 8203 | .hot_remove_disk= raid5_remove_disk, |
| 8204 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8205 | .sync_request = raid5_sync_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8206 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8207 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 8208 | .check_reshape = raid5_check_reshape, |
| 8209 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8210 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 8211 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 8212 | .takeover = raid5_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8213 | .congested = raid5_congested, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8214 | }; |
| 8215 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 8216 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8217 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8218 | .name = "raid4", |
| 8219 | .level = 4, |
| 8220 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8221 | .make_request = raid5_make_request, |
| 8222 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 8223 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8224 | .status = raid5_status, |
| 8225 | .error_handler = raid5_error, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8226 | .hot_add_disk = raid5_add_disk, |
| 8227 | .hot_remove_disk= raid5_remove_disk, |
| 8228 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8229 | .sync_request = raid5_sync_request, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8230 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8231 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 8232 | .check_reshape = raid5_check_reshape, |
| 8233 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8234 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8235 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8236 | .takeover = raid4_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8237 | .congested = raid5_congested, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8238 | }; |
| 8239 | |
| 8240 | static int __init raid5_init(void) |
| 8241 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8242 | int ret; |
| 8243 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8244 | raid5_wq = alloc_workqueue("raid5wq", |
| 8245 | WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); |
| 8246 | if (!raid5_wq) |
| 8247 | return -ENOMEM; |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8248 | |
| 8249 | ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE, |
| 8250 | "md/raid5:prepare", |
| 8251 | raid456_cpu_up_prepare, |
| 8252 | raid456_cpu_dead); |
| 8253 | if (ret) { |
| 8254 | destroy_workqueue(raid5_wq); |
| 8255 | return ret; |
| 8256 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8257 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8258 | register_md_personality(&raid5_personality); |
| 8259 | register_md_personality(&raid4_personality); |
| 8260 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8261 | } |
| 8262 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8263 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8264 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8265 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8266 | unregister_md_personality(&raid5_personality); |
| 8267 | unregister_md_personality(&raid4_personality); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8268 | cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8269 | destroy_workqueue(raid5_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8270 | } |
| 8271 | |
| 8272 | module_init(raid5_init); |
| 8273 | module_exit(raid5_exit); |
| 8274 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8275 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8276 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 8277 | MODULE_ALIAS("md-raid5"); |
| 8278 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8279 | MODULE_ALIAS("md-level-5"); |
| 8280 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8281 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 8282 | MODULE_ALIAS("md-raid6"); |
| 8283 | MODULE_ALIAS("md-level-6"); |
| 8284 | |
| 8285 | /* This used to be two separate modules, they were: */ |
| 8286 | MODULE_ALIAS("raid5"); |
| 8287 | MODULE_ALIAS("raid6"); |