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 | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 65 | #define cpu_to_group(cpu) cpu_to_node(cpu) |
| 66 | #define ANY_GROUP NUMA_NO_NODE |
| 67 | |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 68 | static bool devices_handle_discard_safely = false; |
| 69 | module_param(devices_handle_discard_safely, bool, 0644); |
| 70 | MODULE_PARM_DESC(devices_handle_discard_safely, |
| 71 | "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] | 72 | static struct workqueue_struct *raid5_wq; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 74 | static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect) |
NeilBrown | db298e1 | 2011-10-07 14:23:00 +1100 | [diff] [blame] | 75 | { |
| 76 | int hash = (sect >> STRIPE_SHIFT) & HASH_MASK; |
| 77 | return &conf->stripe_hashtbl[hash]; |
| 78 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 79 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 80 | static inline int stripe_hash_locks_hash(sector_t sect) |
| 81 | { |
| 82 | return (sect >> STRIPE_SHIFT) & STRIPE_HASH_LOCKS_MASK; |
| 83 | } |
| 84 | |
| 85 | static inline void lock_device_hash_lock(struct r5conf *conf, int hash) |
| 86 | { |
| 87 | spin_lock_irq(conf->hash_locks + hash); |
| 88 | spin_lock(&conf->device_lock); |
| 89 | } |
| 90 | |
| 91 | static inline void unlock_device_hash_lock(struct r5conf *conf, int hash) |
| 92 | { |
| 93 | spin_unlock(&conf->device_lock); |
| 94 | spin_unlock_irq(conf->hash_locks + hash); |
| 95 | } |
| 96 | |
| 97 | static inline void lock_all_device_hash_locks_irq(struct r5conf *conf) |
| 98 | { |
| 99 | int i; |
| 100 | local_irq_disable(); |
| 101 | spin_lock(conf->hash_locks); |
| 102 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 103 | spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks); |
| 104 | spin_lock(&conf->device_lock); |
| 105 | } |
| 106 | |
| 107 | static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf) |
| 108 | { |
| 109 | int i; |
| 110 | spin_unlock(&conf->device_lock); |
| 111 | for (i = NR_STRIPE_HASH_LOCKS; i; i--) |
| 112 | spin_unlock(conf->hash_locks + i - 1); |
| 113 | local_irq_enable(); |
| 114 | } |
| 115 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 116 | /* Find first data disk in a raid6 stripe */ |
| 117 | static inline int raid6_d0(struct stripe_head *sh) |
| 118 | { |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 119 | if (sh->ddf_layout) |
| 120 | /* ddf always start from first device */ |
| 121 | return 0; |
| 122 | /* md starts just after Q block */ |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 123 | if (sh->qd_idx == sh->disks - 1) |
| 124 | return 0; |
| 125 | else |
| 126 | return sh->qd_idx + 1; |
| 127 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 128 | static inline int raid6_next_disk(int disk, int raid_disks) |
| 129 | { |
| 130 | disk++; |
| 131 | return (disk < raid_disks) ? disk : 0; |
| 132 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 133 | |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 134 | /* When walking through the disks in a raid5, starting at raid6_d0, |
| 135 | * We need to map each disk to a 'slot', where the data disks are slot |
| 136 | * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk |
| 137 | * is raid_disks-1. This help does that mapping. |
| 138 | */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 139 | static int raid6_idx_to_slot(int idx, struct stripe_head *sh, |
| 140 | int *count, int syndrome_disks) |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 141 | { |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 142 | int slot = *count; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 143 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 144 | if (sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 145 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 146 | if (idx == sh->pd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 147 | return syndrome_disks; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 148 | if (idx == sh->qd_idx) |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 149 | return syndrome_disks + 1; |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 150 | if (!sh->ddf_layout) |
Dan Williams | 6629542 | 2009-10-19 18:09:32 -0700 | [diff] [blame] | 151 | (*count)++; |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 152 | return slot; |
| 153 | } |
| 154 | |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 155 | static void return_io(struct bio_list *return_bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 156 | { |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 157 | struct bio *bi; |
| 158 | while ((bi = bio_list_pop(return_bi)) != NULL) { |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 159 | bi->bi_iter.bi_size = 0; |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 160 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 161 | bi, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 162 | bio_endio(bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 166 | static void print_raid5_conf (struct r5conf *conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 168 | static int stripe_operations_active(struct stripe_head *sh) |
| 169 | { |
| 170 | return sh->check_state || sh->reconstruct_state || |
| 171 | test_bit(STRIPE_BIOFILL_RUN, &sh->state) || |
| 172 | test_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 173 | } |
| 174 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 175 | static void raid5_wakeup_stripe_thread(struct stripe_head *sh) |
| 176 | { |
| 177 | struct r5conf *conf = sh->raid_conf; |
| 178 | struct r5worker_group *group; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 179 | int thread_cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 180 | int i, cpu = sh->cpu; |
| 181 | |
| 182 | if (!cpu_online(cpu)) { |
| 183 | cpu = cpumask_any(cpu_online_mask); |
| 184 | sh->cpu = cpu; |
| 185 | } |
| 186 | |
| 187 | if (list_empty(&sh->lru)) { |
| 188 | struct r5worker_group *group; |
| 189 | group = conf->worker_groups + cpu_to_group(cpu); |
| 190 | list_add_tail(&sh->lru, &group->handle_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 191 | group->stripes_cnt++; |
| 192 | sh->group = group; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | if (conf->worker_cnt_per_group == 0) { |
| 196 | md_wakeup_thread(conf->mddev->thread); |
| 197 | return; |
| 198 | } |
| 199 | |
| 200 | group = conf->worker_groups + cpu_to_group(sh->cpu); |
| 201 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 202 | group->workers[0].working = true; |
| 203 | /* at least one worker should run to avoid race */ |
| 204 | queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work); |
| 205 | |
| 206 | thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1; |
| 207 | /* wakeup more workers */ |
| 208 | for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) { |
| 209 | if (group->workers[i].working == false) { |
| 210 | group->workers[i].working = true; |
| 211 | queue_work_on(sh->cpu, raid5_wq, |
| 212 | &group->workers[i].work); |
| 213 | thread_cnt--; |
| 214 | } |
| 215 | } |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 216 | } |
| 217 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 218 | static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 219 | struct list_head *temp_inactive_list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 220 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 221 | int i; |
| 222 | int injournal = 0; /* number of date pages with R5_InJournal */ |
| 223 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 224 | BUG_ON(!list_empty(&sh->lru)); |
| 225 | BUG_ON(atomic_read(&conf->active_stripes)==0); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 226 | |
| 227 | if (r5c_is_writeback(conf->log)) |
| 228 | for (i = sh->disks; i--; ) |
| 229 | if (test_bit(R5_InJournal, &sh->dev[i].flags)) |
| 230 | injournal++; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 231 | /* |
| 232 | * When quiesce in r5c write back, set STRIPE_HANDLE for stripes with |
| 233 | * data in journal, so they are not released to cached lists |
| 234 | */ |
| 235 | if (conf->quiesce && r5c_is_writeback(conf->log) && |
| 236 | !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0) { |
| 237 | if (test_bit(STRIPE_R5C_CACHING, &sh->state)) |
| 238 | r5c_make_stripe_write_out(sh); |
| 239 | set_bit(STRIPE_HANDLE, &sh->state); |
| 240 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 241 | |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 242 | if (test_bit(STRIPE_HANDLE, &sh->state)) { |
| 243 | if (test_bit(STRIPE_DELAYED, &sh->state) && |
Jes Sorensen | ad3ab8b | 2015-01-29 12:38:29 -0500 | [diff] [blame] | 244 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 245 | list_add_tail(&sh->lru, &conf->delayed_list); |
Jes Sorensen | ad3ab8b | 2015-01-29 12:38:29 -0500 | [diff] [blame] | 246 | else if (test_bit(STRIPE_BIT_DELAY, &sh->state) && |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 247 | sh->bm_seq - conf->seq_write > 0) |
| 248 | list_add_tail(&sh->lru, &conf->bitmap_list); |
| 249 | else { |
| 250 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 251 | clear_bit(STRIPE_BIT_DELAY, &sh->state); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 252 | if (conf->worker_cnt_per_group == 0) { |
| 253 | list_add_tail(&sh->lru, &conf->handle_list); |
| 254 | } else { |
| 255 | raid5_wakeup_stripe_thread(sh); |
| 256 | return; |
| 257 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 258 | } |
| 259 | md_wakeup_thread(conf->mddev->thread); |
| 260 | } else { |
| 261 | BUG_ON(stripe_operations_active(sh)); |
| 262 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 263 | if (atomic_dec_return(&conf->preread_active_stripes) |
| 264 | < IO_THRESHOLD) |
| 265 | md_wakeup_thread(conf->mddev->thread); |
| 266 | atomic_dec(&conf->active_stripes); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 267 | if (!test_bit(STRIPE_EXPANDING, &sh->state)) { |
| 268 | if (!r5c_is_writeback(conf->log)) |
| 269 | list_add_tail(&sh->lru, temp_inactive_list); |
| 270 | else { |
| 271 | WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags)); |
| 272 | if (injournal == 0) |
| 273 | list_add_tail(&sh->lru, temp_inactive_list); |
| 274 | else if (injournal == conf->raid_disks - conf->max_degraded) { |
| 275 | /* full stripe */ |
| 276 | if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state)) |
| 277 | atomic_inc(&conf->r5c_cached_full_stripes); |
| 278 | if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) |
| 279 | atomic_dec(&conf->r5c_cached_partial_stripes); |
| 280 | list_add_tail(&sh->lru, &conf->r5c_full_stripe_list); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 281 | r5c_check_cached_full_stripe(conf); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 282 | } else { |
| 283 | /* partial stripe */ |
| 284 | if (!test_and_set_bit(STRIPE_R5C_PARTIAL_STRIPE, |
| 285 | &sh->state)) |
| 286 | atomic_inc(&conf->r5c_cached_partial_stripes); |
| 287 | list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list); |
| 288 | } |
| 289 | } |
| 290 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |
| 292 | } |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 293 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 294 | static void __release_stripe(struct r5conf *conf, struct stripe_head *sh, |
| 295 | struct list_head *temp_inactive_list) |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 296 | { |
| 297 | if (atomic_dec_and_test(&sh->count)) |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 298 | do_release_stripe(conf, sh, temp_inactive_list); |
| 299 | } |
| 300 | |
| 301 | /* |
| 302 | * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list |
| 303 | * |
| 304 | * Be careful: Only one task can add/delete stripes from temp_inactive_list at |
| 305 | * given time. Adding stripes only takes device lock, while deleting stripes |
| 306 | * only takes hash lock. |
| 307 | */ |
| 308 | static void release_inactive_stripe_list(struct r5conf *conf, |
| 309 | struct list_head *temp_inactive_list, |
| 310 | int hash) |
| 311 | { |
| 312 | int size; |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 313 | bool do_wakeup = false; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 314 | unsigned long flags; |
| 315 | |
| 316 | if (hash == NR_STRIPE_HASH_LOCKS) { |
| 317 | size = NR_STRIPE_HASH_LOCKS; |
| 318 | hash = NR_STRIPE_HASH_LOCKS - 1; |
| 319 | } else |
| 320 | size = 1; |
| 321 | while (size) { |
| 322 | struct list_head *list = &temp_inactive_list[size - 1]; |
| 323 | |
| 324 | /* |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 325 | * 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] | 326 | * remove stripes from the list |
| 327 | */ |
| 328 | if (!list_empty_careful(list)) { |
| 329 | spin_lock_irqsave(conf->hash_locks + hash, flags); |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 330 | if (list_empty(conf->inactive_list + hash) && |
| 331 | !list_empty(list)) |
| 332 | atomic_dec(&conf->empty_inactive_list_nr); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 333 | list_splice_tail_init(list, conf->inactive_list + hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 334 | do_wakeup = true; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 335 | spin_unlock_irqrestore(conf->hash_locks + hash, flags); |
| 336 | } |
| 337 | size--; |
| 338 | hash--; |
| 339 | } |
| 340 | |
| 341 | if (do_wakeup) { |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 342 | wake_up(&conf->wait_for_stripe); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 343 | if (atomic_read(&conf->active_stripes) == 0) |
| 344 | wake_up(&conf->wait_for_quiescent); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 345 | if (conf->retry_read_aligned) |
| 346 | md_wakeup_thread(conf->mddev->thread); |
| 347 | } |
Shaohua Li | 4eb788d | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 348 | } |
| 349 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 350 | /* should hold conf->device_lock already */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 351 | static int release_stripe_list(struct r5conf *conf, |
| 352 | struct list_head *temp_inactive_list) |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 353 | { |
| 354 | struct stripe_head *sh; |
| 355 | int count = 0; |
| 356 | struct llist_node *head; |
| 357 | |
| 358 | head = llist_del_all(&conf->released_stripes); |
Shaohua Li | d265d9d | 2013-08-28 14:29:05 +0800 | [diff] [blame] | 359 | head = llist_reverse_order(head); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 360 | while (head) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 361 | int hash; |
| 362 | |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 363 | sh = llist_entry(head, struct stripe_head, release_list); |
| 364 | head = llist_next(head); |
| 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 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 557 | static int 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 | |
| 620 | degraded = 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 | |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 864 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 865 | raid5_end_read_request(struct bio *bi); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 866 | static void |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 867 | raid5_end_write_request(struct bio *bi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 868 | |
Dan Williams | c4e5ac0 | 2008-06-28 08:31:53 +1000 | [diff] [blame] | 869 | 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] | 870 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 871 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 872 | int i, disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 873 | struct stripe_head *head_sh = sh; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 874 | |
| 875 | might_sleep(); |
| 876 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 877 | if (!test_bit(STRIPE_R5C_CACHING, &sh->state)) { |
| 878 | /* writing out phase */ |
| 879 | if (r5l_write_stripe(conf->log, sh) == 0) |
| 880 | return; |
| 881 | } else { /* caching phase */ |
| 882 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) { |
| 883 | r5c_cache_data(conf->log, sh, s); |
| 884 | return; |
| 885 | } |
| 886 | } |
| 887 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 888 | for (i = disks; i--; ) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 889 | int op, op_flags = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 890 | int replace_only = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 891 | struct bio *bi, *rbi; |
| 892 | struct md_rdev *rdev, *rrdev = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 893 | |
| 894 | sh = head_sh; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 895 | if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 896 | op = REQ_OP_WRITE; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 897 | if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 898 | op_flags = WRITE_FUA; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 899 | if (test_bit(R5_Discard, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 900 | op = REQ_OP_DISCARD; |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 901 | } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 902 | op = REQ_OP_READ; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 903 | else if (test_and_clear_bit(R5_WantReplace, |
| 904 | &sh->dev[i].flags)) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 905 | op = REQ_OP_WRITE; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 906 | replace_only = 1; |
| 907 | } else |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 908 | continue; |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 909 | if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags)) |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 910 | op_flags |= REQ_SYNC; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 911 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 912 | again: |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 913 | bi = &sh->dev[i].req; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 914 | rbi = &sh->dev[i].rreq; /* For writing to replacement */ |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 915 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 916 | rcu_read_lock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 917 | rrdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 918 | smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */ |
| 919 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 920 | if (!rdev) { |
| 921 | rdev = rrdev; |
| 922 | rrdev = NULL; |
| 923 | } |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 924 | if (op_is_write(op)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 925 | if (replace_only) |
| 926 | rdev = NULL; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 927 | if (rdev == rrdev) |
| 928 | /* We raced and saw duplicates */ |
| 929 | rrdev = NULL; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 930 | } else { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 931 | if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 932 | rdev = rrdev; |
| 933 | rrdev = NULL; |
| 934 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 935 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 936 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 937 | rdev = NULL; |
| 938 | if (rdev) |
| 939 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 940 | if (rrdev && test_bit(Faulty, &rrdev->flags)) |
| 941 | rrdev = NULL; |
| 942 | if (rrdev) |
| 943 | atomic_inc(&rrdev->nr_pending); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 944 | rcu_read_unlock(); |
| 945 | |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 946 | /* We have already checked bad blocks for reads. Now |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 947 | * need to check for writes. We never accept write errors |
| 948 | * on the replacement, so we don't to check rrdev. |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 949 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 950 | while (op_is_write(op) && rdev && |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 951 | test_bit(WriteErrorSeen, &rdev->flags)) { |
| 952 | sector_t first_bad; |
| 953 | int bad_sectors; |
| 954 | int bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 955 | &first_bad, &bad_sectors); |
| 956 | if (!bad) |
| 957 | break; |
| 958 | |
| 959 | if (bad < 0) { |
| 960 | set_bit(BlockedBadBlocks, &rdev->flags); |
| 961 | if (!conf->mddev->external && |
| 962 | conf->mddev->flags) { |
| 963 | /* It is very unlikely, but we might |
| 964 | * still need to write out the |
| 965 | * bad block log - better give it |
| 966 | * a chance*/ |
| 967 | md_check_recovery(conf->mddev); |
| 968 | } |
majianpeng | 1850753 | 2012-07-03 12:11:54 +1000 | [diff] [blame] | 969 | /* |
| 970 | * Because md_wait_for_blocked_rdev |
| 971 | * will dec nr_pending, we must |
| 972 | * increment it first. |
| 973 | */ |
| 974 | atomic_inc(&rdev->nr_pending); |
NeilBrown | 73e92e5 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 975 | md_wait_for_blocked_rdev(rdev, conf->mddev); |
| 976 | } else { |
| 977 | /* Acknowledged bad block - skip the write */ |
| 978 | rdev_dec_pending(rdev, conf->mddev); |
| 979 | rdev = NULL; |
| 980 | } |
| 981 | } |
| 982 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 983 | if (rdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 984 | if (s->syncing || s->expanding || s->expanded |
| 985 | || s->replacing) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 986 | md_sync_acct(rdev->bdev, STRIPE_SECTORS); |
| 987 | |
Dan Williams | 2b7497f | 2008-06-28 08:31:52 +1000 | [diff] [blame] | 988 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 989 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 990 | bi->bi_bdev = rdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 991 | bio_set_op_attrs(bi, op, op_flags); |
| 992 | bi->bi_end_io = op_is_write(op) |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 993 | ? raid5_end_write_request |
| 994 | : raid5_end_read_request; |
| 995 | bi->bi_private = sh; |
| 996 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 997 | pr_debug("%s: for %llu schedule op %d on disc %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 998 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 999 | bi->bi_opf, i); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1000 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1001 | if (sh != head_sh) |
| 1002 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1003 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1004 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1005 | + rdev->new_data_offset); |
| 1006 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1007 | bi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1008 | + rdev->data_offset); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1009 | if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags)) |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1010 | bi->bi_opf |= REQ_NOMERGE; |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 1011 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1012 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1013 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 1014 | sh->dev[i].vec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1015 | bi->bi_vcnt = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1016 | bi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 1017 | bi->bi_io_vec[0].bv_offset = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1018 | bi->bi_iter.bi_size = STRIPE_SIZE; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1019 | /* |
| 1020 | * If this is discard request, set bi_vcnt 0. We don't |
| 1021 | * want to confuse SCSI because SCSI will replace payload |
| 1022 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1023 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1024 | bi->bi_vcnt = 0; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1025 | if (rrdev) |
| 1026 | set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1027 | |
| 1028 | if (conf->mddev->gendisk) |
| 1029 | trace_block_bio_remap(bdev_get_queue(bi->bi_bdev), |
| 1030 | bi, disk_devt(conf->mddev->gendisk), |
| 1031 | sh->dev[i].sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1032 | generic_make_request(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1033 | } |
| 1034 | if (rrdev) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1035 | if (s->syncing || s->expanding || s->expanded |
| 1036 | || s->replacing) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1037 | md_sync_acct(rrdev->bdev, STRIPE_SECTORS); |
| 1038 | |
| 1039 | set_bit(STRIPE_IO_STARTED, &sh->state); |
| 1040 | |
| 1041 | rbi->bi_bdev = rrdev->bdev; |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1042 | bio_set_op_attrs(rbi, op, op_flags); |
| 1043 | BUG_ON(!op_is_write(op)); |
Kent Overstreet | 2f6db2a | 2012-09-11 12:26:38 -0700 | [diff] [blame] | 1044 | rbi->bi_end_io = raid5_end_write_request; |
| 1045 | rbi->bi_private = sh; |
| 1046 | |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1047 | pr_debug("%s: for %llu schedule op %d on " |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1048 | "replacement disc %d\n", |
| 1049 | __func__, (unsigned long long)sh->sector, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1050 | rbi->bi_opf, i); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1051 | atomic_inc(&sh->count); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1052 | if (sh != head_sh) |
| 1053 | atomic_inc(&head_sh->count); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1054 | if (use_new_offset(conf, sh)) |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1055 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1056 | + rrdev->new_data_offset); |
| 1057 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1058 | rbi->bi_iter.bi_sector = (sh->sector |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 1059 | + rrdev->data_offset); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1060 | if (test_bit(R5_SkipCopy, &sh->dev[i].flags)) |
| 1061 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 1062 | sh->dev[i].rvec.bv_page = sh->dev[i].page; |
Kent Overstreet | 4997b72 | 2013-05-30 08:44:39 +0200 | [diff] [blame] | 1063 | rbi->bi_vcnt = 1; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1064 | rbi->bi_io_vec[0].bv_len = STRIPE_SIZE; |
| 1065 | rbi->bi_io_vec[0].bv_offset = 0; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1066 | rbi->bi_iter.bi_size = STRIPE_SIZE; |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1067 | /* |
| 1068 | * If this is discard request, set bi_vcnt 0. We don't |
| 1069 | * want to confuse SCSI because SCSI will replace payload |
| 1070 | */ |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1071 | if (op == REQ_OP_DISCARD) |
Shaohua Li | 37c61ff | 2013-10-19 14:50:28 +0800 | [diff] [blame] | 1072 | rbi->bi_vcnt = 0; |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 1073 | if (conf->mddev->gendisk) |
| 1074 | trace_block_bio_remap(bdev_get_queue(rbi->bi_bdev), |
| 1075 | rbi, disk_devt(conf->mddev->gendisk), |
| 1076 | sh->dev[i].sector); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 1077 | generic_make_request(rbi); |
| 1078 | } |
| 1079 | if (!rdev && !rrdev) { |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1080 | if (op_is_write(op)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1081 | set_bit(STRIPE_DEGRADED, &sh->state); |
Mike Christie | 6296b96 | 2016-06-05 14:32:21 -0500 | [diff] [blame] | 1082 | pr_debug("skip op %d on disc %d for sector %llu\n", |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1083 | bi->bi_opf, i, (unsigned long long)sh->sector); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1084 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 1085 | set_bit(STRIPE_HANDLE, &sh->state); |
| 1086 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1087 | |
| 1088 | if (!head_sh->batch_head) |
| 1089 | continue; |
| 1090 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1091 | batch_list); |
| 1092 | if (sh != head_sh) |
| 1093 | goto again; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1094 | } |
| 1095 | } |
| 1096 | |
| 1097 | static struct dma_async_tx_descriptor * |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1098 | async_copy_data(int frombio, struct bio *bio, struct page **page, |
| 1099 | sector_t sector, struct dma_async_tx_descriptor *tx, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1100 | struct stripe_head *sh, int no_skipcopy) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1101 | { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1102 | struct bio_vec bvl; |
| 1103 | struct bvec_iter iter; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1104 | struct page *bio_page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1105 | int page_offset; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1106 | struct async_submit_ctl submit; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1107 | enum async_tx_flags flags = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1108 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1109 | if (bio->bi_iter.bi_sector >= sector) |
| 1110 | page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1111 | else |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1112 | page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1113 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1114 | if (frombio) |
| 1115 | flags |= ASYNC_TX_FENCE; |
| 1116 | init_async_submit(&submit, flags, tx, NULL, NULL, NULL); |
| 1117 | |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1118 | bio_for_each_segment(bvl, bio, iter) { |
| 1119 | int len = bvl.bv_len; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1120 | int clen; |
| 1121 | int b_offset = 0; |
| 1122 | |
| 1123 | if (page_offset < 0) { |
| 1124 | b_offset = -page_offset; |
| 1125 | page_offset += b_offset; |
| 1126 | len -= b_offset; |
| 1127 | } |
| 1128 | |
| 1129 | if (len > 0 && page_offset + len > STRIPE_SIZE) |
| 1130 | clen = STRIPE_SIZE - page_offset; |
| 1131 | else |
| 1132 | clen = len; |
| 1133 | |
| 1134 | if (clen > 0) { |
Kent Overstreet | 7988613 | 2013-11-23 17:19:00 -0800 | [diff] [blame] | 1135 | b_offset += bvl.bv_offset; |
| 1136 | bio_page = bvl.bv_page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1137 | if (frombio) { |
| 1138 | if (sh->raid_conf->skip_copy && |
| 1139 | b_offset == 0 && page_offset == 0 && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1140 | clen == STRIPE_SIZE && |
| 1141 | !no_skipcopy) |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1142 | *page = bio_page; |
| 1143 | else |
| 1144 | tx = async_memcpy(*page, bio_page, page_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1145 | b_offset, clen, &submit); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1146 | } else |
| 1147 | tx = async_memcpy(bio_page, *page, b_offset, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1148 | page_offset, clen, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1149 | } |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1150 | /* chain the operations */ |
| 1151 | submit.depend_tx = tx; |
| 1152 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1153 | if (clen < len) /* hit end of page */ |
| 1154 | break; |
| 1155 | page_offset += len; |
| 1156 | } |
| 1157 | |
| 1158 | return tx; |
| 1159 | } |
| 1160 | |
| 1161 | static void ops_complete_biofill(void *stripe_head_ref) |
| 1162 | { |
| 1163 | struct stripe_head *sh = stripe_head_ref; |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 1164 | struct bio_list return_bi = BIO_EMPTY_LIST; |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1165 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1166 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1167 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1168 | (unsigned long long)sh->sector); |
| 1169 | |
| 1170 | /* clear completed biofills */ |
| 1171 | for (i = sh->disks; i--; ) { |
| 1172 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1173 | |
| 1174 | /* acknowledge completion of a biofill operation */ |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1175 | /* and check if we need to reply to a read request, |
| 1176 | * new R5_Wantfill requests are held off until |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1177 | * !STRIPE_BIOFILL_RUN |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1178 | */ |
| 1179 | if (test_and_clear_bit(R5_Wantfill, &dev->flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1180 | struct bio *rbi, *rbi2; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1181 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1182 | BUG_ON(!dev->read); |
| 1183 | rbi = dev->read; |
| 1184 | dev->read = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1185 | while (rbi && rbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1186 | dev->sector + STRIPE_SECTORS) { |
| 1187 | rbi2 = r5_next_bio(rbi, dev->sector); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 1188 | if (!raid5_dec_bi_active_stripes(rbi)) |
| 1189 | bio_list_add(&return_bi, rbi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1190 | rbi = rbi2; |
| 1191 | } |
| 1192 | } |
| 1193 | } |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1194 | clear_bit(STRIPE_BIOFILL_RUN, &sh->state); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1195 | |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 1196 | return_io(&return_bi); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1197 | |
Dan Williams | e4d8490 | 2007-09-24 10:06:13 -0700 | [diff] [blame] | 1198 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1199 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1200 | } |
| 1201 | |
| 1202 | static void ops_run_biofill(struct stripe_head *sh) |
| 1203 | { |
| 1204 | struct dma_async_tx_descriptor *tx = NULL; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1205 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1206 | int i; |
| 1207 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1208 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1209 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1210 | (unsigned long long)sh->sector); |
| 1211 | |
| 1212 | for (i = sh->disks; i--; ) { |
| 1213 | struct r5dev *dev = &sh->dev[i]; |
| 1214 | if (test_bit(R5_Wantfill, &dev->flags)) { |
| 1215 | struct bio *rbi; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1216 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1217 | dev->read = rbi = dev->toread; |
| 1218 | dev->toread = NULL; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1219 | spin_unlock_irq(&sh->stripe_lock); |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1220 | while (rbi && rbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1221 | dev->sector + STRIPE_SECTORS) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1222 | tx = async_copy_data(0, rbi, &dev->page, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1223 | dev->sector, tx, sh, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1224 | rbi = r5_next_bio(rbi, dev->sector); |
| 1225 | } |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1230 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL); |
| 1231 | async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1232 | } |
| 1233 | |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1234 | static void mark_target_uptodate(struct stripe_head *sh, int target) |
| 1235 | { |
| 1236 | struct r5dev *tgt; |
| 1237 | |
| 1238 | if (target < 0) |
| 1239 | return; |
| 1240 | |
| 1241 | tgt = &sh->dev[target]; |
| 1242 | set_bit(R5_UPTODATE, &tgt->flags); |
| 1243 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1244 | clear_bit(R5_Wantcompute, &tgt->flags); |
| 1245 | } |
| 1246 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1247 | static void ops_complete_compute(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1248 | { |
| 1249 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1250 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1251 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1252 | (unsigned long long)sh->sector); |
| 1253 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1254 | /* mark the computed target(s) as uptodate */ |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1255 | mark_target_uptodate(sh, sh->ops.target); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1256 | mark_target_uptodate(sh, sh->ops.target2); |
Dan Williams | 4e7d2c0 | 2009-08-29 19:13:11 -0700 | [diff] [blame] | 1257 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1258 | clear_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 1259 | if (sh->check_state == check_state_compute_run) |
| 1260 | sh->check_state = check_state_compute_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1261 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1262 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1263 | } |
| 1264 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1265 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 1266 | static addr_conv_t *to_addr_conv(struct stripe_head *sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1267 | struct raid5_percpu *percpu, int i) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1268 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1269 | void *addr; |
| 1270 | |
| 1271 | addr = flex_array_get(percpu->scribble, i); |
| 1272 | return addr + sizeof(struct page *) * (sh->disks + 2); |
| 1273 | } |
| 1274 | |
| 1275 | /* return a pointer to the address conversion region of the scribble buffer */ |
| 1276 | static struct page **to_addr_page(struct raid5_percpu *percpu, int i) |
| 1277 | { |
| 1278 | void *addr; |
| 1279 | |
| 1280 | addr = flex_array_get(percpu->scribble, i); |
| 1281 | return addr; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1282 | } |
| 1283 | |
| 1284 | static struct dma_async_tx_descriptor * |
| 1285 | ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1286 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1287 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1288 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1289 | int target = sh->ops.target; |
| 1290 | struct r5dev *tgt = &sh->dev[target]; |
| 1291 | struct page *xor_dest = tgt->page; |
| 1292 | int count = 0; |
| 1293 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1294 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1295 | int i; |
| 1296 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1297 | BUG_ON(sh->batch_head); |
| 1298 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1299 | pr_debug("%s: stripe %llu block: %d\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1300 | __func__, (unsigned long long)sh->sector, target); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1301 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1302 | |
| 1303 | for (i = disks; i--; ) |
| 1304 | if (i != target) |
| 1305 | xor_srcs[count++] = sh->dev[i].page; |
| 1306 | |
| 1307 | atomic_inc(&sh->count); |
| 1308 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1309 | 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] | 1310 | ops_complete_compute, sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1311 | if (unlikely(count == 1)) |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1312 | 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] | 1313 | else |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1314 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1315 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1316 | return tx; |
| 1317 | } |
| 1318 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1319 | /* set_syndrome_sources - populate source buffers for gen_syndrome |
| 1320 | * @srcs - (struct page *) array of size sh->disks |
| 1321 | * @sh - stripe_head to parse |
| 1322 | * |
| 1323 | * Populates srcs in proper layout order for the stripe and returns the |
| 1324 | * 'count' of sources to be used in a call to async_gen_syndrome. The P |
| 1325 | * destination buffer is recorded in srcs[count] and the Q destination |
| 1326 | * is recorded in srcs[count+1]]. |
| 1327 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1328 | static int set_syndrome_sources(struct page **srcs, |
| 1329 | struct stripe_head *sh, |
| 1330 | int srctype) |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1331 | { |
| 1332 | int disks = sh->disks; |
| 1333 | int syndrome_disks = sh->ddf_layout ? disks : (disks - 2); |
| 1334 | int d0_idx = raid6_d0(sh); |
| 1335 | int count; |
| 1336 | int i; |
| 1337 | |
| 1338 | for (i = 0; i < disks; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1339 | srcs[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1340 | |
| 1341 | count = 0; |
| 1342 | i = d0_idx; |
| 1343 | do { |
| 1344 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1345 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1346 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1347 | if (i == sh->qd_idx || i == sh->pd_idx || |
| 1348 | (srctype == SYNDROME_SRC_ALL) || |
| 1349 | (srctype == SYNDROME_SRC_WANT_DRAIN && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1350 | (test_bit(R5_Wantdrain, &dev->flags) || |
| 1351 | test_bit(R5_InJournal, &dev->flags))) || |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1352 | (srctype == SYNDROME_SRC_WRITTEN && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1353 | dev->written)) { |
| 1354 | if (test_bit(R5_InJournal, &dev->flags)) |
| 1355 | srcs[slot] = sh->dev[i].orig_page; |
| 1356 | else |
| 1357 | srcs[slot] = sh->dev[i].page; |
| 1358 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1359 | i = raid6_next_disk(i, disks); |
| 1360 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1361 | |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1362 | return syndrome_disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1363 | } |
| 1364 | |
| 1365 | static struct dma_async_tx_descriptor * |
| 1366 | ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1367 | { |
| 1368 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1369 | struct page **blocks = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1370 | int target; |
| 1371 | int qd_idx = sh->qd_idx; |
| 1372 | struct dma_async_tx_descriptor *tx; |
| 1373 | struct async_submit_ctl submit; |
| 1374 | struct r5dev *tgt; |
| 1375 | struct page *dest; |
| 1376 | int i; |
| 1377 | int count; |
| 1378 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1379 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1380 | if (sh->ops.target < 0) |
| 1381 | target = sh->ops.target2; |
| 1382 | else if (sh->ops.target2 < 0) |
| 1383 | target = sh->ops.target; |
| 1384 | else |
| 1385 | /* we should only have one valid target */ |
| 1386 | BUG(); |
| 1387 | BUG_ON(target < 0); |
| 1388 | pr_debug("%s: stripe %llu block: %d\n", |
| 1389 | __func__, (unsigned long long)sh->sector, target); |
| 1390 | |
| 1391 | tgt = &sh->dev[target]; |
| 1392 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1393 | dest = tgt->page; |
| 1394 | |
| 1395 | atomic_inc(&sh->count); |
| 1396 | |
| 1397 | if (target == qd_idx) { |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1398 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1399 | blocks[count] = NULL; /* regenerating p is not necessary */ |
| 1400 | BUG_ON(blocks[count+1] != dest); /* q should already be set */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1401 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1402 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1403 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1404 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 1405 | } else { |
| 1406 | /* Compute any data- or p-drive using XOR */ |
| 1407 | count = 0; |
| 1408 | for (i = disks; i-- ; ) { |
| 1409 | if (i == target || i == qd_idx) |
| 1410 | continue; |
| 1411 | blocks[count++] = sh->dev[i].page; |
| 1412 | } |
| 1413 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1414 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1415 | NULL, ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1416 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1417 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, &submit); |
| 1418 | } |
| 1419 | |
| 1420 | return tx; |
| 1421 | } |
| 1422 | |
| 1423 | static struct dma_async_tx_descriptor * |
| 1424 | ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu) |
| 1425 | { |
| 1426 | int i, count, disks = sh->disks; |
| 1427 | int syndrome_disks = sh->ddf_layout ? disks : disks-2; |
| 1428 | int d0_idx = raid6_d0(sh); |
| 1429 | int faila = -1, failb = -1; |
| 1430 | int target = sh->ops.target; |
| 1431 | int target2 = sh->ops.target2; |
| 1432 | struct r5dev *tgt = &sh->dev[target]; |
| 1433 | struct r5dev *tgt2 = &sh->dev[target2]; |
| 1434 | struct dma_async_tx_descriptor *tx; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1435 | struct page **blocks = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1436 | struct async_submit_ctl submit; |
| 1437 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1438 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1439 | pr_debug("%s: stripe %llu block1: %d block2: %d\n", |
| 1440 | __func__, (unsigned long long)sh->sector, target, target2); |
| 1441 | BUG_ON(target < 0 || target2 < 0); |
| 1442 | BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags)); |
| 1443 | BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags)); |
| 1444 | |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1445 | /* we need to open-code set_syndrome_sources to handle the |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1446 | * slot number conversion for 'faila' and 'failb' |
| 1447 | */ |
| 1448 | for (i = 0; i < disks ; i++) |
NeilBrown | 5dd33c9 | 2009-10-16 16:40:25 +1100 | [diff] [blame] | 1449 | blocks[i] = NULL; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1450 | count = 0; |
| 1451 | i = d0_idx; |
| 1452 | do { |
| 1453 | int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks); |
| 1454 | |
| 1455 | blocks[slot] = sh->dev[i].page; |
| 1456 | |
| 1457 | if (i == target) |
| 1458 | faila = slot; |
| 1459 | if (i == target2) |
| 1460 | failb = slot; |
| 1461 | i = raid6_next_disk(i, disks); |
| 1462 | } while (i != d0_idx); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1463 | |
| 1464 | BUG_ON(faila == failb); |
| 1465 | if (failb < faila) |
| 1466 | swap(faila, failb); |
| 1467 | pr_debug("%s: stripe: %llu faila: %d failb: %d\n", |
| 1468 | __func__, (unsigned long long)sh->sector, faila, failb); |
| 1469 | |
| 1470 | atomic_inc(&sh->count); |
| 1471 | |
| 1472 | if (failb == syndrome_disks+1) { |
| 1473 | /* Q disk is one of the missing disks */ |
| 1474 | if (faila == syndrome_disks) { |
| 1475 | /* Missing P+Q, just recompute */ |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1476 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1477 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1478 | to_addr_conv(sh, percpu, 0)); |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 1479 | return async_gen_syndrome(blocks, 0, syndrome_disks+2, |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1480 | STRIPE_SIZE, &submit); |
| 1481 | } else { |
| 1482 | struct page *dest; |
| 1483 | int data_target; |
| 1484 | int qd_idx = sh->qd_idx; |
| 1485 | |
| 1486 | /* Missing D+Q: recompute D from P, then recompute Q */ |
| 1487 | if (target == qd_idx) |
| 1488 | data_target = target2; |
| 1489 | else |
| 1490 | data_target = target; |
| 1491 | |
| 1492 | count = 0; |
| 1493 | for (i = disks; i-- ; ) { |
| 1494 | if (i == data_target || i == qd_idx) |
| 1495 | continue; |
| 1496 | blocks[count++] = sh->dev[i].page; |
| 1497 | } |
| 1498 | dest = sh->dev[data_target].page; |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1499 | init_async_submit(&submit, |
| 1500 | ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, |
| 1501 | NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1502 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1503 | tx = async_xor(dest, blocks, 0, count, STRIPE_SIZE, |
| 1504 | &submit); |
| 1505 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1506 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL); |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1507 | init_async_submit(&submit, ASYNC_TX_FENCE, tx, |
| 1508 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1509 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1510 | return async_gen_syndrome(blocks, 0, count+2, |
| 1511 | STRIPE_SIZE, &submit); |
| 1512 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1513 | } else { |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1514 | init_async_submit(&submit, ASYNC_TX_FENCE, NULL, |
| 1515 | ops_complete_compute, sh, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1516 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | 6c910a7 | 2009-09-16 12:24:54 -0700 | [diff] [blame] | 1517 | if (failb == syndrome_disks) { |
| 1518 | /* We're missing D+P. */ |
| 1519 | return async_raid6_datap_recov(syndrome_disks+2, |
| 1520 | STRIPE_SIZE, faila, |
| 1521 | blocks, &submit); |
| 1522 | } else { |
| 1523 | /* We're missing D+D. */ |
| 1524 | return async_raid6_2data_recov(syndrome_disks+2, |
| 1525 | STRIPE_SIZE, faila, failb, |
| 1526 | blocks, &submit); |
| 1527 | } |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1528 | } |
| 1529 | } |
| 1530 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1531 | static void ops_complete_prexor(void *stripe_head_ref) |
| 1532 | { |
| 1533 | struct stripe_head *sh = stripe_head_ref; |
| 1534 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1535 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1536 | (unsigned long long)sh->sector); |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1537 | |
| 1538 | if (r5c_is_writeback(sh->raid_conf->log)) |
| 1539 | /* |
| 1540 | * raid5-cache write back uses orig_page during prexor. |
| 1541 | * After prexor, it is time to free orig_page |
| 1542 | */ |
| 1543 | r5c_release_extra_page(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1544 | } |
| 1545 | |
| 1546 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1547 | ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1548 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1549 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1550 | int disks = sh->disks; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1551 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1552 | int count = 0, pd_idx = sh->pd_idx, i; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1553 | struct async_submit_ctl submit; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1554 | |
| 1555 | /* existing parity data subtracted */ |
| 1556 | struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1557 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1558 | BUG_ON(sh->batch_head); |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1559 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1560 | (unsigned long long)sh->sector); |
| 1561 | |
| 1562 | for (i = disks; i--; ) { |
| 1563 | struct r5dev *dev = &sh->dev[i]; |
| 1564 | /* Only process blocks that are known to be uptodate */ |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1565 | if (test_bit(R5_InJournal, &dev->flags)) |
| 1566 | xor_srcs[count++] = dev->orig_page; |
| 1567 | else if (test_bit(R5_Wantdrain, &dev->flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1568 | xor_srcs[count++] = dev->page; |
| 1569 | } |
| 1570 | |
Dan Williams | 0403e38 | 2009-09-08 17:42:50 -0700 | [diff] [blame] | 1571 | 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] | 1572 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1573 | tx = async_xor(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1574 | |
| 1575 | return tx; |
| 1576 | } |
| 1577 | |
| 1578 | static struct dma_async_tx_descriptor * |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1579 | ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1580 | struct dma_async_tx_descriptor *tx) |
| 1581 | { |
| 1582 | struct page **blocks = to_addr_page(percpu, 0); |
| 1583 | int count; |
| 1584 | struct async_submit_ctl submit; |
| 1585 | |
| 1586 | pr_debug("%s: stripe %llu\n", __func__, |
| 1587 | (unsigned long long)sh->sector); |
| 1588 | |
| 1589 | count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN); |
| 1590 | |
| 1591 | init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx, |
| 1592 | ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0)); |
| 1593 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
| 1594 | |
| 1595 | return tx; |
| 1596 | } |
| 1597 | |
| 1598 | static struct dma_async_tx_descriptor * |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1599 | 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] | 1600 | { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1601 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1602 | int disks = sh->disks; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1603 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1604 | struct stripe_head *head_sh = sh; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1605 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1606 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1607 | (unsigned long long)sh->sector); |
| 1608 | |
| 1609 | for (i = disks; i--; ) { |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1610 | struct r5dev *dev; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1611 | struct bio *chosen; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1612 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1613 | sh = head_sh; |
| 1614 | if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1615 | struct bio *wbi; |
| 1616 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1617 | again: |
| 1618 | dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1619 | /* |
| 1620 | * clear R5_InJournal, so when rewriting a page in |
| 1621 | * journal, it is not skipped by r5l_log_stripe() |
| 1622 | */ |
| 1623 | clear_bit(R5_InJournal, &dev->flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1624 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1625 | chosen = dev->towrite; |
| 1626 | dev->towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1627 | sh->overwrite_disks = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1628 | BUG_ON(dev->written); |
| 1629 | wbi = dev->written = chosen; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 1630 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1631 | WARN_ON(dev->page != dev->orig_page); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1632 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 1633 | while (wbi && wbi->bi_iter.bi_sector < |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1634 | dev->sector + STRIPE_SECTORS) { |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1635 | if (wbi->bi_opf & REQ_FUA) |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1636 | set_bit(R5_WantFUA, &dev->flags); |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 1637 | if (wbi->bi_opf & REQ_SYNC) |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1638 | set_bit(R5_SyncIO, &dev->flags); |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 1639 | if (bio_op(wbi) == REQ_OP_DISCARD) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1640 | set_bit(R5_Discard, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1641 | else { |
| 1642 | tx = async_copy_data(1, wbi, &dev->page, |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1643 | dev->sector, tx, sh, |
| 1644 | r5c_is_writeback(conf->log)); |
| 1645 | if (dev->page != dev->orig_page && |
| 1646 | !r5c_is_writeback(conf->log)) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1647 | set_bit(R5_SkipCopy, &dev->flags); |
| 1648 | clear_bit(R5_UPTODATE, &dev->flags); |
| 1649 | clear_bit(R5_OVERWRITE, &dev->flags); |
| 1650 | } |
| 1651 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1652 | wbi = r5_next_bio(wbi, dev->sector); |
| 1653 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1654 | |
| 1655 | if (head_sh->batch_head) { |
| 1656 | sh = list_first_entry(&sh->batch_list, |
| 1657 | struct stripe_head, |
| 1658 | batch_list); |
| 1659 | if (sh == head_sh) |
| 1660 | continue; |
| 1661 | goto again; |
| 1662 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1663 | } |
| 1664 | } |
| 1665 | |
| 1666 | return tx; |
| 1667 | } |
| 1668 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1669 | static void ops_complete_reconstruct(void *stripe_head_ref) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1670 | { |
| 1671 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1672 | int disks = sh->disks; |
| 1673 | int pd_idx = sh->pd_idx; |
| 1674 | int qd_idx = sh->qd_idx; |
| 1675 | int i; |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1676 | bool fua = false, sync = false, discard = false; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1677 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1678 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1679 | (unsigned long long)sh->sector); |
| 1680 | |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1681 | for (i = disks; i--; ) { |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1682 | fua |= test_bit(R5_WantFUA, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1683 | sync |= test_bit(R5_SyncIO, &sh->dev[i].flags); |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1684 | discard |= test_bit(R5_Discard, &sh->dev[i].flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1685 | } |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1686 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1687 | for (i = disks; i--; ) { |
| 1688 | struct r5dev *dev = &sh->dev[i]; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1689 | |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1690 | if (dev->written || i == pd_idx || i == qd_idx) { |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 1691 | if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 1692 | set_bit(R5_UPTODATE, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1693 | if (fua) |
| 1694 | set_bit(R5_WantFUA, &dev->flags); |
Shaohua Li | bc0934f | 2012-05-22 13:55:05 +1000 | [diff] [blame] | 1695 | if (sync) |
| 1696 | set_bit(R5_SyncIO, &dev->flags); |
Tejun Heo | e9c7469 | 2010-09-03 11:56:18 +0200 | [diff] [blame] | 1697 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1698 | } |
| 1699 | |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1700 | if (sh->reconstruct_state == reconstruct_state_drain_run) |
| 1701 | sh->reconstruct_state = reconstruct_state_drain_result; |
| 1702 | else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) |
| 1703 | sh->reconstruct_state = reconstruct_state_prexor_drain_result; |
| 1704 | else { |
| 1705 | BUG_ON(sh->reconstruct_state != reconstruct_state_run); |
| 1706 | sh->reconstruct_state = reconstruct_state_result; |
| 1707 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1708 | |
| 1709 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1710 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1711 | } |
| 1712 | |
| 1713 | static void |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1714 | ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1715 | struct dma_async_tx_descriptor *tx) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1716 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1717 | int disks = sh->disks; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1718 | struct page **xor_srcs; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1719 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1720 | int count, pd_idx = sh->pd_idx, i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1721 | struct page *xor_dest; |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1722 | int prexor = 0; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1723 | unsigned long flags; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1724 | int j = 0; |
| 1725 | struct stripe_head *head_sh = sh; |
| 1726 | int last_stripe; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1727 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1728 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1729 | (unsigned long long)sh->sector); |
| 1730 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1731 | for (i = 0; i < sh->disks; i++) { |
| 1732 | if (pd_idx == i) |
| 1733 | continue; |
| 1734 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1735 | break; |
| 1736 | } |
| 1737 | if (i >= sh->disks) { |
| 1738 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1739 | set_bit(R5_Discard, &sh->dev[pd_idx].flags); |
| 1740 | ops_complete_reconstruct(sh); |
| 1741 | return; |
| 1742 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1743 | again: |
| 1744 | count = 0; |
| 1745 | xor_srcs = to_addr_page(percpu, j); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1746 | /* check if prexor is active which means only process blocks |
| 1747 | * that are part of a read-modify-write (written) |
| 1748 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1749 | if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1750 | prexor = 1; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1751 | xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page; |
| 1752 | for (i = disks; i--; ) { |
| 1753 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 1754 | if (head_sh->dev[i].written || |
| 1755 | test_bit(R5_InJournal, &head_sh->dev[i].flags)) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1756 | xor_srcs[count++] = dev->page; |
| 1757 | } |
| 1758 | } else { |
| 1759 | xor_dest = sh->dev[pd_idx].page; |
| 1760 | for (i = disks; i--; ) { |
| 1761 | struct r5dev *dev = &sh->dev[i]; |
| 1762 | if (i != pd_idx) |
| 1763 | xor_srcs[count++] = dev->page; |
| 1764 | } |
| 1765 | } |
| 1766 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1767 | /* 1/ if we prexor'd then the dest is reused as a source |
| 1768 | * 2/ if we did not prexor then we are redoing the parity |
| 1769 | * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST |
| 1770 | * for the synchronous xor case |
| 1771 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1772 | last_stripe = !head_sh->batch_head || |
| 1773 | list_first_entry(&sh->batch_list, |
| 1774 | struct stripe_head, batch_list) == head_sh; |
| 1775 | if (last_stripe) { |
| 1776 | flags = ASYNC_TX_ACK | |
| 1777 | (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1778 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1779 | atomic_inc(&head_sh->count); |
| 1780 | init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh, |
| 1781 | to_addr_conv(sh, percpu, j)); |
| 1782 | } else { |
| 1783 | flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST; |
| 1784 | init_async_submit(&submit, flags, tx, NULL, NULL, |
| 1785 | to_addr_conv(sh, percpu, j)); |
| 1786 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1787 | |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1788 | if (unlikely(count == 1)) |
| 1789 | tx = async_memcpy(xor_dest, xor_srcs[0], 0, 0, STRIPE_SIZE, &submit); |
| 1790 | else |
| 1791 | 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] | 1792 | if (!last_stripe) { |
| 1793 | j++; |
| 1794 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1795 | batch_list); |
| 1796 | goto again; |
| 1797 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1798 | } |
| 1799 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1800 | static void |
| 1801 | ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu, |
| 1802 | struct dma_async_tx_descriptor *tx) |
| 1803 | { |
| 1804 | struct async_submit_ctl submit; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1805 | struct page **blocks; |
| 1806 | int count, i, j = 0; |
| 1807 | struct stripe_head *head_sh = sh; |
| 1808 | int last_stripe; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1809 | int synflags; |
| 1810 | unsigned long txflags; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1811 | |
| 1812 | pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector); |
| 1813 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1814 | for (i = 0; i < sh->disks; i++) { |
| 1815 | if (sh->pd_idx == i || sh->qd_idx == i) |
| 1816 | continue; |
| 1817 | if (!test_bit(R5_Discard, &sh->dev[i].flags)) |
| 1818 | break; |
| 1819 | } |
| 1820 | if (i >= sh->disks) { |
| 1821 | atomic_inc(&sh->count); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 1822 | set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 1823 | set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 1824 | ops_complete_reconstruct(sh); |
| 1825 | return; |
| 1826 | } |
| 1827 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1828 | again: |
| 1829 | blocks = to_addr_page(percpu, j); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1830 | |
| 1831 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) { |
| 1832 | synflags = SYNDROME_SRC_WRITTEN; |
| 1833 | txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST; |
| 1834 | } else { |
| 1835 | synflags = SYNDROME_SRC_ALL; |
| 1836 | txflags = ASYNC_TX_ACK; |
| 1837 | } |
| 1838 | |
| 1839 | count = set_syndrome_sources(blocks, sh, synflags); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1840 | last_stripe = !head_sh->batch_head || |
| 1841 | list_first_entry(&sh->batch_list, |
| 1842 | struct stripe_head, batch_list) == head_sh; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1843 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1844 | if (last_stripe) { |
| 1845 | atomic_inc(&head_sh->count); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1846 | init_async_submit(&submit, txflags, tx, ops_complete_reconstruct, |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1847 | head_sh, to_addr_conv(sh, percpu, j)); |
| 1848 | } else |
| 1849 | init_async_submit(&submit, 0, tx, NULL, NULL, |
| 1850 | to_addr_conv(sh, percpu, j)); |
Shaohua Li | 4876969 | 2015-05-13 09:30:08 -0700 | [diff] [blame] | 1851 | tx = async_gen_syndrome(blocks, 0, count+2, STRIPE_SIZE, &submit); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1852 | if (!last_stripe) { |
| 1853 | j++; |
| 1854 | sh = list_first_entry(&sh->batch_list, struct stripe_head, |
| 1855 | batch_list); |
| 1856 | goto again; |
| 1857 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static void ops_complete_check(void *stripe_head_ref) |
| 1861 | { |
| 1862 | struct stripe_head *sh = stripe_head_ref; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1863 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1864 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1865 | (unsigned long long)sh->sector); |
| 1866 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 1867 | sh->check_state = check_state_check_result; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1868 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 1869 | raid5_release_stripe(sh); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1870 | } |
| 1871 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1872 | 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] | 1873 | { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1874 | int disks = sh->disks; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1875 | int pd_idx = sh->pd_idx; |
| 1876 | int qd_idx = sh->qd_idx; |
| 1877 | struct page *xor_dest; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1878 | struct page **xor_srcs = to_addr_page(percpu, 0); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1879 | struct dma_async_tx_descriptor *tx; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1880 | struct async_submit_ctl submit; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1881 | int count; |
| 1882 | int i; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1883 | |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 1884 | pr_debug("%s: stripe %llu\n", __func__, |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1885 | (unsigned long long)sh->sector); |
| 1886 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1887 | BUG_ON(sh->batch_head); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1888 | count = 0; |
| 1889 | xor_dest = sh->dev[pd_idx].page; |
| 1890 | xor_srcs[count++] = xor_dest; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1891 | for (i = disks; i--; ) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1892 | if (i == pd_idx || i == qd_idx) |
| 1893 | continue; |
| 1894 | xor_srcs[count++] = sh->dev[i].page; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1895 | } |
| 1896 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1897 | init_async_submit(&submit, 0, NULL, NULL, NULL, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1898 | to_addr_conv(sh, percpu, 0)); |
Dan Williams | 099f53c | 2009-04-08 14:28:37 -0700 | [diff] [blame] | 1899 | tx = async_xor_val(xor_dest, xor_srcs, 0, count, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1900 | &sh->ops.zero_sum_result, &submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1901 | |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1902 | atomic_inc(&sh->count); |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 1903 | init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL); |
| 1904 | tx = async_trigger_callback(&submit); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1905 | } |
| 1906 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1907 | static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp) |
| 1908 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1909 | struct page **srcs = to_addr_page(percpu, 0); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1910 | struct async_submit_ctl submit; |
| 1911 | int count; |
| 1912 | |
| 1913 | pr_debug("%s: stripe %llu checkp: %d\n", __func__, |
| 1914 | (unsigned long long)sh->sector, checkp); |
| 1915 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1916 | BUG_ON(sh->batch_head); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1917 | count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1918 | if (!checkp) |
| 1919 | srcs[count] = NULL; |
| 1920 | |
| 1921 | atomic_inc(&sh->count); |
| 1922 | init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check, |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 1923 | sh, to_addr_conv(sh, percpu, 0)); |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1924 | async_syndrome_val(srcs, 0, count+2, STRIPE_SIZE, |
| 1925 | &sh->ops.zero_sum_result, percpu->spare_page, &submit); |
| 1926 | } |
| 1927 | |
NeilBrown | 51acbce | 2013-02-28 09:08:34 +1100 | [diff] [blame] | 1928 | 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] | 1929 | { |
| 1930 | int overlap_clear = 0, i, disks = sh->disks; |
| 1931 | struct dma_async_tx_descriptor *tx = NULL; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 1932 | struct r5conf *conf = sh->raid_conf; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1933 | int level = conf->level; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1934 | struct raid5_percpu *percpu; |
| 1935 | unsigned long cpu; |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1936 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1937 | cpu = get_cpu(); |
| 1938 | percpu = per_cpu_ptr(conf->percpu, cpu); |
Dan Williams | 83de75c | 2008-06-28 08:31:58 +1000 | [diff] [blame] | 1939 | if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) { |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1940 | ops_run_biofill(sh); |
| 1941 | overlap_clear++; |
| 1942 | } |
| 1943 | |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1944 | if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) { |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1945 | if (level < 6) |
| 1946 | tx = ops_run_compute5(sh, percpu); |
| 1947 | else { |
| 1948 | if (sh->ops.target2 < 0 || sh->ops.target < 0) |
| 1949 | tx = ops_run_compute6_1(sh, percpu); |
| 1950 | else |
| 1951 | tx = ops_run_compute6_2(sh, percpu); |
| 1952 | } |
| 1953 | /* terminate the chain if reconstruct is not set to be run */ |
| 1954 | if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) |
Dan Williams | 7b3a871 | 2008-06-28 08:32:09 +1000 | [diff] [blame] | 1955 | async_tx_ack(tx); |
| 1956 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1957 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 1958 | if (test_bit(STRIPE_OP_PREXOR, &ops_request)) { |
| 1959 | if (level < 6) |
| 1960 | tx = ops_run_prexor5(sh, percpu, tx); |
| 1961 | else |
| 1962 | tx = ops_run_prexor6(sh, percpu, tx); |
| 1963 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1964 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 1965 | if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) { |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 1966 | tx = ops_run_biodrain(sh, tx); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1967 | overlap_clear++; |
| 1968 | } |
| 1969 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1970 | if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) { |
| 1971 | if (level < 6) |
| 1972 | ops_run_reconstruct5(sh, percpu, tx); |
| 1973 | else |
| 1974 | ops_run_reconstruct6(sh, percpu, tx); |
| 1975 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1976 | |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 1977 | if (test_bit(STRIPE_OP_CHECK, &ops_request)) { |
| 1978 | if (sh->check_state == check_state_run) |
| 1979 | ops_run_check_p(sh, percpu); |
| 1980 | else if (sh->check_state == check_state_run_q) |
| 1981 | ops_run_check_pq(sh, percpu, 0); |
| 1982 | else if (sh->check_state == check_state_run_pq) |
| 1983 | ops_run_check_pq(sh, percpu, 1); |
| 1984 | else |
| 1985 | BUG(); |
| 1986 | } |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1987 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 1988 | if (overlap_clear && !sh->batch_head) |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1989 | for (i = disks; i--; ) { |
| 1990 | struct r5dev *dev = &sh->dev[i]; |
| 1991 | if (test_and_clear_bit(R5_Overlap, &dev->flags)) |
| 1992 | wake_up(&sh->raid_conf->wait_for_overlap); |
| 1993 | } |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 1994 | put_cpu(); |
Dan Williams | 91c0092 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 1995 | } |
| 1996 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 1997 | static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp, |
| 1998 | int disks) |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 1999 | { |
| 2000 | struct stripe_head *sh; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2001 | int i; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2002 | |
| 2003 | sh = kmem_cache_zalloc(sc, gfp); |
| 2004 | if (sh) { |
| 2005 | spin_lock_init(&sh->stripe_lock); |
| 2006 | spin_lock_init(&sh->batch_lock); |
| 2007 | INIT_LIST_HEAD(&sh->batch_list); |
| 2008 | INIT_LIST_HEAD(&sh->lru); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2009 | INIT_LIST_HEAD(&sh->r5c); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2010 | atomic_set(&sh->count, 1); |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 2011 | sh->log_start = MaxSector; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2012 | for (i = 0; i < disks; i++) { |
| 2013 | struct r5dev *dev = &sh->dev[i]; |
| 2014 | |
| 2015 | bio_init(&dev->req); |
Shaohua Li | 45c91d8 | 2016-08-22 21:14:02 -0700 | [diff] [blame] | 2016 | dev->req.bi_io_vec = &dev->vec; |
| 2017 | dev->req.bi_max_vecs = 1; |
| 2018 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2019 | bio_init(&dev->rreq); |
Shaohua Li | 45c91d8 | 2016-08-22 21:14:02 -0700 | [diff] [blame] | 2020 | dev->rreq.bi_io_vec = &dev->rvec; |
| 2021 | dev->rreq.bi_max_vecs = 1; |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2022 | } |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2023 | } |
| 2024 | return sh; |
| 2025 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2026 | static int grow_one_stripe(struct r5conf *conf, gfp_t gfp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2027 | { |
| 2028 | struct stripe_head *sh; |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2029 | |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2030 | sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2031 | if (!sh) |
| 2032 | return 0; |
Namhyung Kim | 6ce3284 | 2011-07-18 17:38:50 +1000 | [diff] [blame] | 2033 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2034 | sh->raid_conf = conf; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2035 | |
NeilBrown | a9683a7 | 2015-02-25 12:02:51 +1100 | [diff] [blame] | 2036 | if (grow_buffers(sh, gfp)) { |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2037 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2038 | kmem_cache_free(conf->slab_cache, sh); |
| 2039 | return 0; |
| 2040 | } |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2041 | sh->hash_lock_index = |
| 2042 | conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2043 | /* we just created an active stripe so... */ |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2044 | atomic_inc(&conf->active_stripes); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2045 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2046 | raid5_release_stripe(sh); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2047 | conf->max_nr_stripes++; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2048 | return 1; |
| 2049 | } |
| 2050 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2051 | static int grow_stripes(struct r5conf *conf, int num) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2052 | { |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2053 | struct kmem_cache *sc; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 2054 | int devs = max(conf->raid_disks, conf->previous_raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2055 | |
NeilBrown | f4be6b4 | 2010-06-01 19:37:25 +1000 | [diff] [blame] | 2056 | if (conf->mddev->gendisk) |
| 2057 | sprintf(conf->cache_name[0], |
| 2058 | "raid%d-%s", conf->level, mdname(conf->mddev)); |
| 2059 | else |
| 2060 | sprintf(conf->cache_name[0], |
| 2061 | "raid%d-%p", conf->level, conf->mddev); |
| 2062 | sprintf(conf->cache_name[1], "%s-alt", conf->cache_name[0]); |
| 2063 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2064 | conf->active_name = 0; |
| 2065 | sc = kmem_cache_create(conf->cache_name[conf->active_name], |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2066 | sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2067 | 0, 0, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2068 | if (!sc) |
| 2069 | return 1; |
| 2070 | conf->slab_cache = sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2071 | conf->pool_size = devs; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2072 | while (num--) |
| 2073 | if (!grow_one_stripe(conf, GFP_KERNEL)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2074 | return 1; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2075 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2076 | return 0; |
| 2077 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 2078 | |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2079 | /** |
| 2080 | * scribble_len - return the required size of the scribble region |
| 2081 | * @num - total number of disks in the array |
| 2082 | * |
| 2083 | * The size must be enough to contain: |
| 2084 | * 1/ a struct page pointer for each device in the array +2 |
| 2085 | * 2/ room to convert each entry in (1) to its corresponding dma |
| 2086 | * (dma_map_page()) or page (page_address()) address. |
| 2087 | * |
| 2088 | * Note: the +2 is for the destination buffers of the ddf/raid6 case where we |
| 2089 | * calculate over all devices (not just the data blocks), using zeros in place |
| 2090 | * of the P and Q blocks. |
| 2091 | */ |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2092 | 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] | 2093 | { |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 2094 | struct flex_array *ret; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2095 | size_t len; |
| 2096 | |
| 2097 | 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] | 2098 | ret = flex_array_alloc(len, cnt, flags); |
| 2099 | if (!ret) |
| 2100 | return NULL; |
| 2101 | /* always prealloc all elements, so no locking is required */ |
| 2102 | if (flex_array_prealloc(ret, 0, cnt, flags)) { |
| 2103 | flex_array_free(ret); |
| 2104 | return NULL; |
| 2105 | } |
| 2106 | return ret; |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2107 | } |
| 2108 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2109 | static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors) |
| 2110 | { |
| 2111 | unsigned long cpu; |
| 2112 | int err = 0; |
| 2113 | |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2114 | /* |
| 2115 | * Never shrink. And mddev_suspend() could deadlock if this is called |
| 2116 | * from raid5d. In that case, scribble_disks and scribble_sectors |
| 2117 | * should equal to new_disks and new_sectors |
| 2118 | */ |
| 2119 | if (conf->scribble_disks >= new_disks && |
| 2120 | conf->scribble_sectors >= new_sectors) |
| 2121 | return 0; |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2122 | mddev_suspend(conf->mddev); |
| 2123 | get_online_cpus(); |
| 2124 | for_each_present_cpu(cpu) { |
| 2125 | struct raid5_percpu *percpu; |
| 2126 | struct flex_array *scribble; |
| 2127 | |
| 2128 | percpu = per_cpu_ptr(conf->percpu, cpu); |
| 2129 | scribble = scribble_alloc(new_disks, |
| 2130 | new_sectors / STRIPE_SECTORS, |
| 2131 | GFP_NOIO); |
| 2132 | |
| 2133 | if (scribble) { |
| 2134 | flex_array_free(percpu->scribble); |
| 2135 | percpu->scribble = scribble; |
| 2136 | } else { |
| 2137 | err = -ENOMEM; |
| 2138 | break; |
| 2139 | } |
| 2140 | } |
| 2141 | put_online_cpus(); |
| 2142 | mddev_resume(conf->mddev); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 2143 | if (!err) { |
| 2144 | conf->scribble_disks = new_disks; |
| 2145 | conf->scribble_sectors = new_sectors; |
| 2146 | } |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 2147 | return err; |
| 2148 | } |
| 2149 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2150 | static int resize_stripes(struct r5conf *conf, int newsize) |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2151 | { |
| 2152 | /* Make all the stripes able to hold 'newsize' devices. |
| 2153 | * New slots in each stripe get 'page' set to a new page. |
| 2154 | * |
| 2155 | * This happens in stages: |
| 2156 | * 1/ create a new kmem_cache and allocate the required number of |
| 2157 | * stripe_heads. |
Masanari Iida | 83f0d77 | 2012-10-30 00:18:08 +0900 | [diff] [blame] | 2158 | * 2/ gather all the old stripe_heads and transfer the pages across |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2159 | * to the new stripe_heads. This will have the side effect of |
| 2160 | * freezing the array as once all stripe_heads have been collected, |
| 2161 | * no IO will be possible. Old stripe heads are freed once their |
| 2162 | * pages have been transferred over, and the old kmem_cache is |
| 2163 | * freed when all stripes are done. |
| 2164 | * 3/ reallocate conf->disks to be suitable bigger. If this fails, |
| 2165 | * we simple return a failre status - no need to clean anything up. |
| 2166 | * 4/ allocate new pages for the new slots in the new stripe_heads. |
| 2167 | * If this fails, we don't bother trying the shrink the |
| 2168 | * stripe_heads down again, we just leave them as they are. |
| 2169 | * As each stripe_head is processed the new one is released into |
| 2170 | * active service. |
| 2171 | * |
| 2172 | * Once step2 is started, we cannot afford to wait for a write, |
| 2173 | * so we use GFP_NOIO allocations. |
| 2174 | */ |
| 2175 | struct stripe_head *osh, *nsh; |
| 2176 | LIST_HEAD(newstripes); |
| 2177 | struct disk_info *ndisks; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 2178 | int err; |
Christoph Lameter | e18b890 | 2006-12-06 20:33:20 -0800 | [diff] [blame] | 2179 | struct kmem_cache *sc; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2180 | int i; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2181 | int hash, cnt; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2182 | |
| 2183 | if (newsize <= conf->pool_size) |
| 2184 | return 0; /* never bother to shrink */ |
| 2185 | |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 2186 | err = md_allow_write(conf->mddev); |
| 2187 | if (err) |
| 2188 | return err; |
NeilBrown | 2a2275d | 2007-01-26 00:57:11 -0800 | [diff] [blame] | 2189 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2190 | /* Step 1 */ |
| 2191 | sc = kmem_cache_create(conf->cache_name[1-conf->active_name], |
| 2192 | sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev), |
Paul Mundt | 20c2df8 | 2007-07-20 10:11:58 +0900 | [diff] [blame] | 2193 | 0, 0, NULL); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2194 | if (!sc) |
| 2195 | return -ENOMEM; |
| 2196 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2197 | /* Need to ensure auto-resizing doesn't interfere */ |
| 2198 | mutex_lock(&conf->cache_size_mutex); |
| 2199 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2200 | for (i = conf->max_nr_stripes; i; i--) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2201 | nsh = alloc_stripe(sc, GFP_KERNEL, newsize); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2202 | if (!nsh) |
| 2203 | break; |
| 2204 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2205 | nsh->raid_conf = conf; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2206 | list_add(&nsh->lru, &newstripes); |
| 2207 | } |
| 2208 | if (i) { |
| 2209 | /* didn't get enough, give up */ |
| 2210 | while (!list_empty(&newstripes)) { |
| 2211 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2212 | list_del(&nsh->lru); |
| 2213 | kmem_cache_free(sc, nsh); |
| 2214 | } |
| 2215 | kmem_cache_destroy(sc); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2216 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2217 | return -ENOMEM; |
| 2218 | } |
| 2219 | /* Step 2 - Must use GFP_NOIO now. |
| 2220 | * OK, we have enough stripes, start collecting inactive |
| 2221 | * stripes and copying them over |
| 2222 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2223 | hash = 0; |
| 2224 | cnt = 0; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2225 | list_for_each_entry(nsh, &newstripes, lru) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2226 | lock_device_hash_lock(conf, hash); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 2227 | wait_event_cmd(conf->wait_for_stripe, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2228 | !list_empty(conf->inactive_list + hash), |
| 2229 | unlock_device_hash_lock(conf, hash), |
| 2230 | lock_device_hash_lock(conf, hash)); |
| 2231 | osh = get_free_stripe(conf, hash); |
| 2232 | unlock_device_hash_lock(conf, hash); |
NeilBrown | f18c1a3 | 2015-05-08 18:19:04 +1000 | [diff] [blame] | 2233 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2234 | for(i=0; i<conf->pool_size; i++) { |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2235 | nsh->dev[i].page = osh->dev[i].page; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2236 | nsh->dev[i].orig_page = osh->dev[i].page; |
| 2237 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2238 | nsh->hash_lock_index = hash; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2239 | kmem_cache_free(conf->slab_cache, osh); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2240 | cnt++; |
| 2241 | if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS + |
| 2242 | !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) { |
| 2243 | hash++; |
| 2244 | cnt = 0; |
| 2245 | } |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2246 | } |
| 2247 | kmem_cache_destroy(conf->slab_cache); |
| 2248 | |
| 2249 | /* Step 3. |
| 2250 | * At this point, we are holding all the stripes so the array |
| 2251 | * is completely stalled, so now is a good time to resize |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2252 | * conf->disks and the scribble region |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2253 | */ |
| 2254 | ndisks = kzalloc(newsize * sizeof(struct disk_info), GFP_NOIO); |
| 2255 | if (ndisks) { |
| 2256 | for (i=0; i<conf->raid_disks; i++) |
| 2257 | ndisks[i] = conf->disks[i]; |
| 2258 | kfree(conf->disks); |
| 2259 | conf->disks = ndisks; |
| 2260 | } else |
| 2261 | err = -ENOMEM; |
| 2262 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 2263 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2264 | /* Step 4, return new stripes to service */ |
| 2265 | while(!list_empty(&newstripes)) { |
| 2266 | nsh = list_entry(newstripes.next, struct stripe_head, lru); |
| 2267 | list_del_init(&nsh->lru); |
Dan Williams | d6f38f3 | 2009-07-14 11:50:52 -0700 | [diff] [blame] | 2268 | |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2269 | for (i=conf->raid_disks; i < newsize; i++) |
| 2270 | if (nsh->dev[i].page == NULL) { |
| 2271 | struct page *p = alloc_page(GFP_NOIO); |
| 2272 | nsh->dev[i].page = p; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 2273 | nsh->dev[i].orig_page = p; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2274 | if (!p) |
| 2275 | err = -ENOMEM; |
| 2276 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2277 | raid5_release_stripe(nsh); |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2278 | } |
| 2279 | /* critical section pass, GFP_NOIO no longer needed */ |
| 2280 | |
| 2281 | conf->slab_cache = sc; |
| 2282 | conf->active_name = 1-conf->active_name; |
NeilBrown | 6e9eac2 | 2015-05-08 18:19:34 +1000 | [diff] [blame] | 2283 | if (!err) |
| 2284 | conf->pool_size = newsize; |
NeilBrown | ad01c9e | 2006-03-27 01:18:07 -0800 | [diff] [blame] | 2285 | return err; |
| 2286 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2287 | |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2288 | static int drop_one_stripe(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2289 | { |
| 2290 | struct stripe_head *sh; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 2291 | int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2292 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2293 | spin_lock_irq(conf->hash_locks + hash); |
| 2294 | sh = get_free_stripe(conf, hash); |
| 2295 | spin_unlock_irq(conf->hash_locks + hash); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2296 | if (!sh) |
| 2297 | return 0; |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 2298 | BUG_ON(atomic_read(&sh->count)); |
NeilBrown | e4e11e3 | 2010-06-16 16:45:16 +1000 | [diff] [blame] | 2299 | shrink_buffers(sh); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2300 | kmem_cache_free(conf->slab_cache, sh); |
| 2301 | atomic_dec(&conf->active_stripes); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2302 | conf->max_nr_stripes--; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2303 | return 1; |
| 2304 | } |
| 2305 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2306 | static void shrink_stripes(struct r5conf *conf) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2307 | { |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 2308 | while (conf->max_nr_stripes && |
| 2309 | drop_one_stripe(conf)) |
| 2310 | ; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 2311 | |
Julia Lawall | 644df1a | 2015-09-13 14:15:10 +0200 | [diff] [blame] | 2312 | kmem_cache_destroy(conf->slab_cache); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2313 | conf->slab_cache = NULL; |
| 2314 | } |
| 2315 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2316 | static void raid5_end_read_request(struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2317 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2318 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2319 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2320 | int disks = sh->disks, i; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2321 | char b[BDEVNAME_SIZE]; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2322 | struct md_rdev *rdev = NULL; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2323 | sector_t s; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2324 | |
| 2325 | for (i=0 ; i<disks; i++) |
| 2326 | if (bi == &sh->dev[i].req) |
| 2327 | break; |
| 2328 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2329 | pr_debug("end_read_request %llu/%d, count: %d, error %d.\n", |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 2330 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2331 | bi->bi_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2332 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2333 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2334 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2335 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2336 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2337 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2338 | /* If replacement finished while this request was outstanding, |
| 2339 | * 'replacement' might be NULL already. |
| 2340 | * In that case it moved down to 'rdev'. |
| 2341 | * rdev is not removed until all requests are finished. |
| 2342 | */ |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2343 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2344 | if (!rdev) |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2345 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2346 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2347 | if (use_new_offset(conf, sh)) |
| 2348 | s = sh->sector + rdev->new_data_offset; |
| 2349 | else |
| 2350 | s = sh->sector + rdev->data_offset; |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2351 | if (!bi->bi_error) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2352 | set_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2353 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2354 | /* Note that this cannot happen on a |
| 2355 | * replacement device. We just fail those on |
| 2356 | * any error |
| 2357 | */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2358 | pr_info_ratelimited( |
| 2359 | "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] | 2360 | mdname(conf->mddev), STRIPE_SECTORS, |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2361 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2362 | bdevname(rdev->bdev, b)); |
Namhyung Kim | ddd5115 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2363 | atomic_add(STRIPE_SECTORS, &rdev->corrected_errors); |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2364 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2365 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2366 | } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2367 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2368 | |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2369 | if (atomic_read(&rdev->read_errors)) |
| 2370 | atomic_set(&rdev->read_errors, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2371 | } else { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2372 | const char *bdn = bdevname(rdev->bdev, b); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2373 | int retry = 0; |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2374 | int set_bad = 0; |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2375 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2376 | clear_bit(R5_UPTODATE, &sh->dev[i].flags); |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2377 | atomic_inc(&rdev->read_errors); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2378 | if (test_bit(R5_ReadRepl, &sh->dev[i].flags)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2379 | pr_warn_ratelimited( |
| 2380 | "md/raid:%s: read error on replacement device (sector %llu on %s).\n", |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2381 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2382 | (unsigned long long)s, |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2383 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2384 | else if (conf->mddev->degraded >= conf->max_degraded) { |
| 2385 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2386 | pr_warn_ratelimited( |
| 2387 | "md/raid:%s: read error not correctable (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2388 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2389 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2390 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2391 | } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2392 | /* Oh, no!!! */ |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2393 | set_bad = 1; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2394 | pr_warn_ratelimited( |
| 2395 | "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n", |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2396 | mdname(conf->mddev), |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 2397 | (unsigned long long)s, |
Christian Dietrich | 8bda470 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2398 | bdn); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2399 | } else if (atomic_read(&rdev->read_errors) |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2400 | > conf->max_nr_stripes) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2401 | pr_warn("md/raid:%s: Too many read errors, failing device %s.\n", |
NeilBrown | d695043 | 2006-07-10 04:44:20 -0700 | [diff] [blame] | 2402 | mdname(conf->mddev), bdn); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2403 | else |
| 2404 | retry = 1; |
Bian Yu | edfa1f6 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 2405 | if (set_bad && test_bit(In_sync, &rdev->flags) |
| 2406 | && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) |
| 2407 | retry = 1; |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2408 | if (retry) |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 2409 | if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) { |
| 2410 | set_bit(R5_ReadError, &sh->dev[i].flags); |
| 2411 | clear_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
| 2412 | } else |
| 2413 | set_bit(R5_ReadNoMerge, &sh->dev[i].flags); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2414 | else { |
NeilBrown | 4e5314b | 2005-11-08 21:39:22 -0800 | [diff] [blame] | 2415 | clear_bit(R5_ReadError, &sh->dev[i].flags); |
| 2416 | clear_bit(R5_ReWrite, &sh->dev[i].flags); |
majianpeng | 2e8ac303 | 2012-07-03 15:57:02 +1000 | [diff] [blame] | 2417 | if (!(set_bad |
| 2418 | && test_bit(In_sync, &rdev->flags) |
| 2419 | && rdev_set_badblocks( |
| 2420 | rdev, sh->sector, STRIPE_SECTORS, 0))) |
| 2421 | md_error(conf->mddev, rdev); |
NeilBrown | ba22dcb | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 2422 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2423 | } |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 2424 | rdev_dec_pending(rdev, conf->mddev); |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2425 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2426 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
| 2427 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2428 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2429 | } |
| 2430 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2431 | static void raid5_end_write_request(struct bio *bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2432 | { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2433 | struct stripe_head *sh = bi->bi_private; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2434 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 2435 | int disks = sh->disks, i; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2436 | struct md_rdev *uninitialized_var(rdev); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 2437 | sector_t first_bad; |
| 2438 | int bad_sectors; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2439 | int replacement = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2440 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2441 | for (i = 0 ; i < disks; i++) { |
| 2442 | if (bi == &sh->dev[i].req) { |
| 2443 | rdev = conf->disks[i].rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2444 | break; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2445 | } |
| 2446 | if (bi == &sh->dev[i].rreq) { |
| 2447 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2448 | if (rdev) |
| 2449 | replacement = 1; |
| 2450 | else |
| 2451 | /* rdev was removed and 'replacement' |
| 2452 | * replaced it. rdev is not removed |
| 2453 | * until all requests are finished. |
| 2454 | */ |
| 2455 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2456 | break; |
| 2457 | } |
| 2458 | } |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2459 | pr_debug("end_write_request %llu/%d, count %d, error: %d.\n", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2460 | (unsigned long long)sh->sector, i, atomic_read(&sh->count), |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2461 | bi->bi_error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2462 | if (i == disks) { |
Shaohua Li | 5f9d1fd | 2016-08-22 21:14:01 -0700 | [diff] [blame] | 2463 | bio_reset(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2464 | BUG(); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 2465 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2466 | } |
| 2467 | |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2468 | if (replacement) { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2469 | if (bi->bi_error) |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2470 | md_error(conf->mddev, rdev); |
| 2471 | else if (is_badblock(rdev, sh->sector, |
| 2472 | STRIPE_SECTORS, |
| 2473 | &first_bad, &bad_sectors)) |
| 2474 | set_bit(R5_MadeGoodRepl, &sh->dev[i].flags); |
| 2475 | } else { |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2476 | if (bi->bi_error) { |
NeilBrown | 9f97e4b | 2014-01-16 09:35:38 +1100 | [diff] [blame] | 2477 | set_bit(STRIPE_DEGRADED, &sh->state); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2478 | set_bit(WriteErrorSeen, &rdev->flags); |
| 2479 | set_bit(R5_WriteError, &sh->dev[i].flags); |
NeilBrown | 3a6de29 | 2011-12-23 10:17:54 +1100 | [diff] [blame] | 2480 | if (!test_and_set_bit(WantReplacement, &rdev->flags)) |
| 2481 | set_bit(MD_RECOVERY_NEEDED, |
| 2482 | &rdev->mddev->recovery); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2483 | } else if (is_badblock(rdev, sh->sector, |
| 2484 | STRIPE_SECTORS, |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2485 | &first_bad, &bad_sectors)) { |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2486 | set_bit(R5_MadeGood, &sh->dev[i].flags); |
NeilBrown | c0b3297 | 2013-04-24 11:42:42 +1000 | [diff] [blame] | 2487 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) |
| 2488 | /* That was a successful write so make |
| 2489 | * sure it looks like we already did |
| 2490 | * a re-write. |
| 2491 | */ |
| 2492 | set_bit(R5_ReWrite, &sh->dev[i].flags); |
| 2493 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2494 | } |
| 2495 | rdev_dec_pending(rdev, conf->mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2496 | |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 2497 | if (sh->batch_head && bi->bi_error && !replacement) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2498 | set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state); |
| 2499 | |
Shaohua Li | c944555 | 2016-09-08 10:43:58 -0700 | [diff] [blame] | 2500 | bio_reset(bi); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 2501 | if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags)) |
| 2502 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2503 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2504 | raid5_release_stripe(sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2505 | |
| 2506 | if (sh->batch_head && sh != sh->batch_head) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2507 | raid5_release_stripe(sh->batch_head); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2508 | } |
| 2509 | |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2510 | 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] | 2511 | { |
| 2512 | struct r5dev *dev = &sh->dev[i]; |
| 2513 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2514 | dev->flags = 0; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2515 | dev->sector = raid5_compute_blocknr(sh, i, previous); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2516 | } |
| 2517 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 2518 | static void raid5_error(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2519 | { |
| 2520 | char b[BDEVNAME_SIZE]; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2521 | struct r5conf *conf = mddev->private; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2522 | unsigned long flags; |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 2523 | pr_debug("raid456: error called\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2524 | |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 2525 | spin_lock_irqsave(&conf->device_lock, flags); |
| 2526 | clear_bit(In_sync, &rdev->flags); |
| 2527 | mddev->degraded = calc_degraded(conf); |
| 2528 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 2529 | set_bit(MD_RECOVERY_INTR, &mddev->recovery); |
| 2530 | |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 2531 | set_bit(Blocked, &rdev->flags); |
NeilBrown | 6f8d0c7 | 2011-05-11 14:38:44 +1000 | [diff] [blame] | 2532 | set_bit(Faulty, &rdev->flags); |
Guoqing Jiang | 85ad1d1 | 2016-05-03 22:22:13 -0400 | [diff] [blame] | 2533 | set_mask_bits(&mddev->flags, 0, |
| 2534 | BIT(MD_CHANGE_DEVS) | BIT(MD_CHANGE_PENDING)); |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2535 | pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n" |
| 2536 | "md/raid:%s: Operation continuing on %d devices.\n", |
| 2537 | mdname(mddev), |
| 2538 | bdevname(rdev->bdev, b), |
| 2539 | mdname(mddev), |
| 2540 | conf->raid_disks - mddev->degraded); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2541 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2542 | |
| 2543 | /* |
| 2544 | * Input: a 'big' sector number, |
| 2545 | * Output: index of the data and parity disk, and the sector # in them. |
| 2546 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2547 | sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector, |
| 2548 | int previous, int *dd_idx, |
| 2549 | struct stripe_head *sh) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2550 | { |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2551 | sector_t stripe, stripe2; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2552 | sector_t chunk_number; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2553 | unsigned int chunk_offset; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2554 | int pd_idx, qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2555 | int ddf_layout = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2556 | sector_t new_sector; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2557 | int algorithm = previous ? conf->prev_algo |
| 2558 | : conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2559 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2560 | : conf->chunk_sectors; |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2561 | int raid_disks = previous ? conf->previous_raid_disks |
| 2562 | : conf->raid_disks; |
| 2563 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2564 | |
| 2565 | /* First compute the information on this sector */ |
| 2566 | |
| 2567 | /* |
| 2568 | * Compute the chunk number and the sector offset inside the chunk |
| 2569 | */ |
| 2570 | chunk_offset = sector_div(r_sector, sectors_per_chunk); |
| 2571 | chunk_number = r_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2572 | |
| 2573 | /* |
| 2574 | * Compute the stripe number |
| 2575 | */ |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2576 | stripe = chunk_number; |
| 2577 | *dd_idx = sector_div(stripe, data_disks); |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2578 | stripe2 = stripe; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2579 | /* |
| 2580 | * Select the parity disk based on the user selected algorithm. |
| 2581 | */ |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 2582 | pd_idx = qd_idx = -1; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2583 | switch(conf->level) { |
| 2584 | case 4: |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2585 | pd_idx = data_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2586 | break; |
| 2587 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2588 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2589 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2590 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2591 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2592 | (*dd_idx)++; |
| 2593 | break; |
| 2594 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2595 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2596 | if (*dd_idx >= pd_idx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2597 | (*dd_idx)++; |
| 2598 | break; |
| 2599 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2600 | pd_idx = data_disks - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2601 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2602 | break; |
| 2603 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2604 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2605 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2606 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2607 | case ALGORITHM_PARITY_0: |
| 2608 | pd_idx = 0; |
| 2609 | (*dd_idx)++; |
| 2610 | break; |
| 2611 | case ALGORITHM_PARITY_N: |
| 2612 | pd_idx = data_disks; |
| 2613 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2614 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2615 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2616 | } |
| 2617 | break; |
| 2618 | case 6: |
| 2619 | |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2620 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2621 | case ALGORITHM_LEFT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2622 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2623 | qd_idx = pd_idx + 1; |
| 2624 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2625 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2626 | qd_idx = 0; |
| 2627 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2628 | (*dd_idx) += 2; /* D D P Q D */ |
| 2629 | break; |
| 2630 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2631 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2632 | qd_idx = pd_idx + 1; |
| 2633 | if (pd_idx == raid_disks-1) { |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2634 | (*dd_idx)++; /* Q D D D P */ |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2635 | qd_idx = 0; |
| 2636 | } else if (*dd_idx >= pd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2637 | (*dd_idx) += 2; /* D D P Q D */ |
| 2638 | break; |
| 2639 | case ALGORITHM_LEFT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2640 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2641 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2642 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2643 | break; |
| 2644 | case ALGORITHM_RIGHT_SYMMETRIC: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2645 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2646 | qd_idx = (pd_idx + 1) % raid_disks; |
| 2647 | *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2648 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2649 | |
| 2650 | case ALGORITHM_PARITY_0: |
| 2651 | pd_idx = 0; |
| 2652 | qd_idx = 1; |
| 2653 | (*dd_idx) += 2; |
| 2654 | break; |
| 2655 | case ALGORITHM_PARITY_N: |
| 2656 | pd_idx = data_disks; |
| 2657 | qd_idx = data_disks + 1; |
| 2658 | break; |
| 2659 | |
| 2660 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2661 | /* Exactly the same as RIGHT_ASYMMETRIC, but or |
| 2662 | * of blocks for computing Q is different. |
| 2663 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2664 | pd_idx = sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2665 | qd_idx = pd_idx + 1; |
| 2666 | if (pd_idx == raid_disks-1) { |
| 2667 | (*dd_idx)++; /* Q D D D P */ |
| 2668 | qd_idx = 0; |
| 2669 | } else if (*dd_idx >= pd_idx) |
| 2670 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2671 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2672 | break; |
| 2673 | |
| 2674 | case ALGORITHM_ROTATING_N_RESTART: |
| 2675 | /* Same a left_asymmetric, by first stripe is |
| 2676 | * D D D P Q rather than |
| 2677 | * Q D D D P |
| 2678 | */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2679 | stripe2 += 1; |
| 2680 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2681 | qd_idx = pd_idx + 1; |
| 2682 | if (pd_idx == raid_disks-1) { |
| 2683 | (*dd_idx)++; /* Q D D D P */ |
| 2684 | qd_idx = 0; |
| 2685 | } else if (*dd_idx >= pd_idx) |
| 2686 | (*dd_idx) += 2; /* D D P Q D */ |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2687 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2688 | break; |
| 2689 | |
| 2690 | case ALGORITHM_ROTATING_N_CONTINUE: |
| 2691 | /* Same as left_symmetric but Q is before P */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2692 | pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2693 | qd_idx = (pd_idx + raid_disks - 1) % raid_disks; |
| 2694 | *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2695 | ddf_layout = 1; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2696 | break; |
| 2697 | |
| 2698 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2699 | /* RAID5 left_asymmetric, with Q on last device */ |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2700 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2701 | if (*dd_idx >= pd_idx) |
| 2702 | (*dd_idx)++; |
| 2703 | qd_idx = raid_disks - 1; |
| 2704 | break; |
| 2705 | |
| 2706 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2707 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2708 | if (*dd_idx >= pd_idx) |
| 2709 | (*dd_idx)++; |
| 2710 | qd_idx = raid_disks - 1; |
| 2711 | break; |
| 2712 | |
| 2713 | case ALGORITHM_LEFT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2714 | pd_idx = data_disks - sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2715 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2716 | qd_idx = raid_disks - 1; |
| 2717 | break; |
| 2718 | |
| 2719 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
NeilBrown | 6e3b96e | 2010-04-23 07:08:28 +1000 | [diff] [blame] | 2720 | pd_idx = sector_div(stripe2, raid_disks-1); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2721 | *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1); |
| 2722 | qd_idx = raid_disks - 1; |
| 2723 | break; |
| 2724 | |
| 2725 | case ALGORITHM_PARITY_0_6: |
| 2726 | pd_idx = 0; |
| 2727 | (*dd_idx)++; |
| 2728 | qd_idx = raid_disks - 1; |
| 2729 | break; |
| 2730 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2731 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2732 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2733 | } |
| 2734 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2735 | } |
| 2736 | |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2737 | if (sh) { |
| 2738 | sh->pd_idx = pd_idx; |
| 2739 | sh->qd_idx = qd_idx; |
NeilBrown | 67cc2b8 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2740 | sh->ddf_layout = ddf_layout; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2741 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2742 | /* |
| 2743 | * Finally, compute the new sector number |
| 2744 | */ |
| 2745 | new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset; |
| 2746 | return new_sector; |
| 2747 | } |
| 2748 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 2749 | 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] | 2750 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2751 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 2752 | int raid_disks = sh->disks; |
| 2753 | int data_disks = raid_disks - conf->max_degraded; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2754 | sector_t new_sector = sh->sector, check; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 2755 | int sectors_per_chunk = previous ? conf->prev_chunk_sectors |
| 2756 | : conf->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2757 | int algorithm = previous ? conf->prev_algo |
| 2758 | : conf->algorithm; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2759 | sector_t stripe; |
| 2760 | int chunk_offset; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2761 | sector_t chunk_number; |
| 2762 | int dummy1, dd_idx = i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2763 | sector_t r_sector; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2764 | struct stripe_head sh2; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2765 | |
| 2766 | chunk_offset = sector_div(new_sector, sectors_per_chunk); |
| 2767 | stripe = new_sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2768 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2769 | if (i == sh->pd_idx) |
| 2770 | return 0; |
| 2771 | switch(conf->level) { |
| 2772 | case 4: break; |
| 2773 | case 5: |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2774 | switch (algorithm) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2775 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2776 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 2777 | if (i > sh->pd_idx) |
| 2778 | i--; |
| 2779 | break; |
| 2780 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2781 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2782 | if (i < sh->pd_idx) |
| 2783 | i += raid_disks; |
| 2784 | i -= (sh->pd_idx + 1); |
| 2785 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2786 | case ALGORITHM_PARITY_0: |
| 2787 | i -= 1; |
| 2788 | break; |
| 2789 | case ALGORITHM_PARITY_N: |
| 2790 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2791 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2792 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2793 | } |
| 2794 | break; |
| 2795 | case 6: |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2796 | if (i == sh->qd_idx) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2797 | return 0; /* It is the Q disk */ |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 2798 | switch (algorithm) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2799 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 2800 | case ALGORITHM_RIGHT_ASYMMETRIC: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2801 | case ALGORITHM_ROTATING_ZERO_RESTART: |
| 2802 | case ALGORITHM_ROTATING_N_RESTART: |
| 2803 | if (sh->pd_idx == raid_disks-1) |
| 2804 | i--; /* Q D D D P */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2805 | else if (i > sh->pd_idx) |
| 2806 | i -= 2; /* D D P Q D */ |
| 2807 | break; |
| 2808 | case ALGORITHM_LEFT_SYMMETRIC: |
| 2809 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 2810 | if (sh->pd_idx == raid_disks-1) |
| 2811 | i--; /* Q D D D P */ |
| 2812 | else { |
| 2813 | /* D D P Q D */ |
| 2814 | if (i < sh->pd_idx) |
| 2815 | i += raid_disks; |
| 2816 | i -= (sh->pd_idx + 2); |
| 2817 | } |
| 2818 | break; |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2819 | case ALGORITHM_PARITY_0: |
| 2820 | i -= 2; |
| 2821 | break; |
| 2822 | case ALGORITHM_PARITY_N: |
| 2823 | break; |
| 2824 | case ALGORITHM_ROTATING_N_CONTINUE: |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2825 | /* Like left_symmetric, but P is before Q */ |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2826 | if (sh->pd_idx == 0) |
| 2827 | i--; /* P D D D Q */ |
NeilBrown | e4424fe | 2009-10-16 16:27:34 +1100 | [diff] [blame] | 2828 | else { |
| 2829 | /* D D Q P D */ |
| 2830 | if (i < sh->pd_idx) |
| 2831 | i += raid_disks; |
| 2832 | i -= (sh->pd_idx + 1); |
| 2833 | } |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2834 | break; |
| 2835 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 2836 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 2837 | if (i > sh->pd_idx) |
| 2838 | i--; |
| 2839 | break; |
| 2840 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 2841 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 2842 | if (i < sh->pd_idx) |
| 2843 | i += data_disks + 1; |
| 2844 | i -= (sh->pd_idx + 1); |
| 2845 | break; |
| 2846 | case ALGORITHM_PARITY_0_6: |
| 2847 | i -= 1; |
| 2848 | break; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2849 | default: |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2850 | BUG(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2851 | } |
| 2852 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2853 | } |
| 2854 | |
| 2855 | chunk_number = stripe * data_disks + i; |
NeilBrown | 35f2a59 | 2010-04-20 14:13:34 +1000 | [diff] [blame] | 2856 | r_sector = chunk_number * sectors_per_chunk + chunk_offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2857 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2858 | check = raid5_compute_sector(conf, r_sector, |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 2859 | previous, &dummy1, &sh2); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 2860 | if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx |
| 2861 | || sh2.qd_idx != sh->qd_idx) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 2862 | pr_warn("md/raid:%s: compute_blocknr: map not correct\n", |
| 2863 | mdname(conf->mddev)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2864 | return 0; |
| 2865 | } |
| 2866 | return r_sector; |
| 2867 | } |
| 2868 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2869 | static void |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2870 | schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2871 | int rcw, int expand) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2872 | { |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2873 | 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] | 2874 | struct r5conf *conf = sh->raid_conf; |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2875 | int level = conf->level; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2876 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2877 | if (rcw) { |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2878 | /* |
| 2879 | * In some cases, handle_stripe_dirtying initially decided to |
| 2880 | * run rmw and allocates extra page for prexor. However, rcw is |
| 2881 | * cheaper later on. We need to free the extra page now, |
| 2882 | * because we won't be able to do that in ops_complete_prexor(). |
| 2883 | */ |
| 2884 | r5c_release_extra_page(sh); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2885 | |
| 2886 | for (i = disks; i--; ) { |
| 2887 | struct r5dev *dev = &sh->dev[i]; |
| 2888 | |
| 2889 | if (dev->towrite) { |
| 2890 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2891 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2892 | if (!expand) |
| 2893 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2894 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2895 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 2896 | set_bit(R5_LOCKED, &dev->flags); |
| 2897 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2898 | } |
| 2899 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 2900 | /* if we are not expanding this is a proper write request, and |
| 2901 | * there will be bios with new data to be drained into the |
| 2902 | * stripe cache |
| 2903 | */ |
| 2904 | if (!expand) { |
| 2905 | if (!s->locked) |
| 2906 | /* False alarm, nothing to do */ |
| 2907 | return; |
| 2908 | sh->reconstruct_state = reconstruct_state_drain_run; |
| 2909 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2910 | } else |
| 2911 | sh->reconstruct_state = reconstruct_state_run; |
| 2912 | |
| 2913 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
| 2914 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2915 | if (s->locked + conf->max_degraded == disks) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 2916 | if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state)) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2917 | atomic_inc(&conf->pending_full_writes); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2918 | } else { |
| 2919 | BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) || |
| 2920 | test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags))); |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2921 | BUG_ON(level == 6 && |
| 2922 | (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) || |
| 2923 | test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags)))); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2924 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2925 | for (i = disks; i--; ) { |
| 2926 | struct r5dev *dev = &sh->dev[i]; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 2927 | if (i == pd_idx || i == qd_idx) |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2928 | continue; |
| 2929 | |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2930 | if (dev->towrite && |
| 2931 | (test_bit(R5_UPTODATE, &dev->flags) || |
Dan Williams | d8ee072 | 2008-06-28 08:32:06 +1000 | [diff] [blame] | 2932 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 2933 | set_bit(R5_Wantdrain, &dev->flags); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2934 | set_bit(R5_LOCKED, &dev->flags); |
| 2935 | clear_bit(R5_UPTODATE, &dev->flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2936 | s->locked++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 2937 | } else if (test_bit(R5_InJournal, &dev->flags)) { |
| 2938 | set_bit(R5_LOCKED, &dev->flags); |
| 2939 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2940 | } |
| 2941 | } |
NeilBrown | ce7d363 | 2013-03-04 12:37:14 +1100 | [diff] [blame] | 2942 | if (!s->locked) |
| 2943 | /* False alarm - nothing to do */ |
| 2944 | return; |
| 2945 | sh->reconstruct_state = reconstruct_state_prexor_drain_run; |
| 2946 | set_bit(STRIPE_OP_PREXOR, &s->ops_request); |
| 2947 | set_bit(STRIPE_OP_BIODRAIN, &s->ops_request); |
| 2948 | set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2949 | } |
| 2950 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2951 | /* keep the parity disk(s) locked while asynchronous operations |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2952 | * are in flight |
| 2953 | */ |
| 2954 | set_bit(R5_LOCKED, &sh->dev[pd_idx].flags); |
| 2955 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2956 | s->locked++; |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2957 | |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 2958 | if (level == 6) { |
| 2959 | int qd_idx = sh->qd_idx; |
| 2960 | struct r5dev *dev = &sh->dev[qd_idx]; |
| 2961 | |
| 2962 | set_bit(R5_LOCKED, &dev->flags); |
| 2963 | clear_bit(R5_UPTODATE, &dev->flags); |
| 2964 | s->locked++; |
| 2965 | } |
| 2966 | |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2967 | pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n", |
Harvey Harrison | e46b272 | 2008-04-28 02:15:50 -0700 | [diff] [blame] | 2968 | __func__, (unsigned long long)sh->sector, |
Dan Williams | 600aa10 | 2008-06-28 08:32:05 +1000 | [diff] [blame] | 2969 | s->locked, s->ops_request); |
Dan Williams | e33129d | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 2970 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2971 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2972 | /* |
| 2973 | * Each stripe/dev can have one or more bion attached. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 2974 | * toread/towrite point to the first in a chain. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2975 | * The bi_next chain must be in order. |
| 2976 | */ |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2977 | static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx, |
| 2978 | int forwrite, int previous) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2979 | { |
| 2980 | struct bio **bip; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 2981 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 2982 | int firstwrite=0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2983 | |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 2984 | pr_debug("adding bi b#%llu to stripe s#%llu\n", |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 2985 | (unsigned long long)bi->bi_iter.bi_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2986 | (unsigned long long)sh->sector); |
| 2987 | |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 2988 | /* |
| 2989 | * If several bio share a stripe. The bio bi_phys_segments acts as a |
| 2990 | * reference count to avoid race. The reference count should already be |
| 2991 | * increased before this function is called (for example, in |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 2992 | * 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] | 2993 | * stripe. If a stripe is owned by one stripe, the stripe lock will |
| 2994 | * protect it. |
| 2995 | */ |
| 2996 | spin_lock_irq(&sh->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 2997 | /* Don't allow new IO added to stripes in batch list */ |
| 2998 | if (sh->batch_head) |
| 2999 | goto overlap; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3000 | if (forwrite) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3001 | bip = &sh->dev[dd_idx].towrite; |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3002 | if (*bip == NULL) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3003 | firstwrite = 1; |
| 3004 | } else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3005 | bip = &sh->dev[dd_idx].toread; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3006 | while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) { |
| 3007 | if (bio_end_sector(*bip) > bi->bi_iter.bi_sector) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3008 | goto overlap; |
| 3009 | bip = & (*bip)->bi_next; |
| 3010 | } |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3011 | if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3012 | goto overlap; |
| 3013 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3014 | if (!forwrite || previous) |
| 3015 | clear_bit(STRIPE_BATCH_READY, &sh->state); |
| 3016 | |
Eric Sesterhenn | 78bafeb | 2006-04-02 13:31:42 +0200 | [diff] [blame] | 3017 | BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3018 | if (*bip) |
| 3019 | bi->bi_next = *bip; |
| 3020 | *bip = bi; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3021 | raid5_inc_bi_active_stripes(bi); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 3022 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3023 | if (forwrite) { |
| 3024 | /* check if page is covered */ |
| 3025 | sector_t sector = sh->dev[dd_idx].sector; |
| 3026 | for (bi=sh->dev[dd_idx].towrite; |
| 3027 | sector < sh->dev[dd_idx].sector + STRIPE_SECTORS && |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3028 | bi && bi->bi_iter.bi_sector <= sector; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3029 | bi = r5_next_bio(bi, sh->dev[dd_idx].sector)) { |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 3030 | if (bio_end_sector(bi) >= sector) |
| 3031 | sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3032 | } |
| 3033 | if (sector >= sh->dev[dd_idx].sector + STRIPE_SECTORS) |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3034 | if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags)) |
| 3035 | sh->overwrite_disks++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3036 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3037 | |
| 3038 | 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] | 3039 | (unsigned long long)(*bip)->bi_iter.bi_sector, |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3040 | (unsigned long long)sh->sector, dd_idx); |
| 3041 | |
| 3042 | if (conf->mddev->bitmap && firstwrite) { |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3043 | /* Cannot hold spinlock over bitmap_startwrite, |
| 3044 | * but must ensure this isn't added to a batch until |
| 3045 | * we have added to the bitmap and set bm_seq. |
| 3046 | * So set STRIPE_BITMAP_PENDING to prevent |
| 3047 | * batching. |
| 3048 | * If multiple add_stripe_bio() calls race here they |
| 3049 | * much all set STRIPE_BITMAP_PENDING. So only the first one |
| 3050 | * to complete "bitmap_startwrite" gets to set |
| 3051 | * STRIPE_BIT_DELAY. This is important as once a stripe |
| 3052 | * is added to a batch, STRIPE_BIT_DELAY cannot be changed |
| 3053 | * any more. |
| 3054 | */ |
| 3055 | set_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3056 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3057 | bitmap_startwrite(conf->mddev->bitmap, sh->sector, |
| 3058 | STRIPE_SECTORS, 0); |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3059 | spin_lock_irq(&sh->stripe_lock); |
| 3060 | clear_bit(STRIPE_BITMAP_PENDING, &sh->state); |
| 3061 | if (!sh->batch_head) { |
| 3062 | sh->bm_seq = conf->seq_flush+1; |
| 3063 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 3064 | } |
NeilBrown | cbe47ec | 2011-07-26 11:20:35 +1000 | [diff] [blame] | 3065 | } |
NeilBrown | d0852df5 | 2015-05-27 08:43:45 +1000 | [diff] [blame] | 3066 | spin_unlock_irq(&sh->stripe_lock); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3067 | |
| 3068 | if (stripe_can_batch(sh)) |
| 3069 | stripe_add_to_batch_list(conf, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3070 | return 1; |
| 3071 | |
| 3072 | overlap: |
| 3073 | set_bit(R5_Overlap, &sh->dev[dd_idx].flags); |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3074 | spin_unlock_irq(&sh->stripe_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 3075 | return 0; |
| 3076 | } |
| 3077 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3078 | static void end_reshape(struct r5conf *conf); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 3079 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3080 | static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3081 | struct stripe_head *sh) |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3082 | { |
NeilBrown | 784052e | 2009-03-31 15:19:07 +1100 | [diff] [blame] | 3083 | int sectors_per_chunk = |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 3084 | previous ? conf->prev_chunk_sectors : conf->chunk_sectors; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3085 | int dd_idx; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3086 | int chunk_offset = sector_div(stripe, sectors_per_chunk); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3087 | int disks = previous ? conf->previous_raid_disks : conf->raid_disks; |
Coywolf Qi Hunt | 2d2063c | 2006-10-03 01:15:50 -0700 | [diff] [blame] | 3088 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3089 | raid5_compute_sector(conf, |
| 3090 | stripe * (disks - conf->max_degraded) |
NeilBrown | b875e53 | 2006-12-10 02:20:49 -0800 | [diff] [blame] | 3091 | *sectors_per_chunk + chunk_offset, |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3092 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3093 | &dd_idx, sh); |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 3094 | } |
| 3095 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3096 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3097 | handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3098 | struct stripe_head_state *s, int disks, |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3099 | struct bio_list *return_bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3100 | { |
| 3101 | int i; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3102 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3103 | for (i = disks; i--; ) { |
| 3104 | struct bio *bi; |
| 3105 | int bitmap_end = 0; |
| 3106 | |
| 3107 | if (test_bit(R5_ReadError, &sh->dev[i].flags)) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 3108 | struct md_rdev *rdev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3109 | rcu_read_lock(); |
| 3110 | rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | f5b67ae | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 3111 | if (rdev && test_bit(In_sync, &rdev->flags) && |
| 3112 | !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3113 | atomic_inc(&rdev->nr_pending); |
| 3114 | else |
| 3115 | rdev = NULL; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3116 | rcu_read_unlock(); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3117 | if (rdev) { |
| 3118 | if (!rdev_set_badblocks( |
| 3119 | rdev, |
| 3120 | sh->sector, |
| 3121 | STRIPE_SECTORS, 0)) |
| 3122 | md_error(conf->mddev, rdev); |
| 3123 | rdev_dec_pending(rdev, conf->mddev); |
| 3124 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3125 | } |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3126 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3127 | /* fail all writes first */ |
| 3128 | bi = sh->dev[i].towrite; |
| 3129 | sh->dev[i].towrite = NULL; |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3130 | sh->overwrite_disks = 0; |
Shaohua Li | b17459c | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3131 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 1ed850f | 2012-10-11 13:50:13 +1100 | [diff] [blame] | 3132 | if (bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3133 | bitmap_end = 1; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3134 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3135 | r5l_stripe_write_finished(sh); |
| 3136 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3137 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3138 | wake_up(&conf->wait_for_overlap); |
| 3139 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3140 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3141 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3142 | struct bio *nextbi = r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3143 | |
| 3144 | bi->bi_error = -EIO; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3145 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3146 | md_write_end(conf->mddev); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3147 | bio_list_add(return_bi, bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3148 | } |
| 3149 | bi = nextbi; |
| 3150 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3151 | if (bitmap_end) |
| 3152 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3153 | STRIPE_SECTORS, 0, 0); |
| 3154 | bitmap_end = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3155 | /* and fail all 'written' */ |
| 3156 | bi = sh->dev[i].written; |
| 3157 | sh->dev[i].written = NULL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3158 | if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) { |
| 3159 | WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
| 3160 | sh->dev[i].page = sh->dev[i].orig_page; |
| 3161 | } |
| 3162 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3163 | if (bi) bitmap_end = 1; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3164 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3165 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3166 | struct bio *bi2 = r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3167 | |
| 3168 | bi->bi_error = -EIO; |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3169 | if (!raid5_dec_bi_active_stripes(bi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3170 | md_write_end(conf->mddev); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3171 | bio_list_add(return_bi, bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3172 | } |
| 3173 | bi = bi2; |
| 3174 | } |
| 3175 | |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3176 | /* fail any reads if this device is non-operational and |
| 3177 | * the data has not reached the cache yet. |
| 3178 | */ |
| 3179 | if (!test_bit(R5_Wantfill, &sh->dev[i].flags) && |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 3180 | s->failed > conf->max_degraded && |
Dan Williams | b5e98d6 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3181 | (!test_bit(R5_Insync, &sh->dev[i].flags) || |
| 3182 | test_bit(R5_ReadError, &sh->dev[i].flags))) { |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3183 | spin_lock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3184 | bi = sh->dev[i].toread; |
| 3185 | sh->dev[i].toread = NULL; |
NeilBrown | 143c4d0 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3186 | spin_unlock_irq(&sh->stripe_lock); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3187 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 3188 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3189 | if (bi) |
| 3190 | s->to_read--; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3191 | while (bi && bi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3192 | sh->dev[i].sector + STRIPE_SECTORS) { |
| 3193 | struct bio *nextbi = |
| 3194 | r5_next_bio(bi, sh->dev[i].sector); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 3195 | |
| 3196 | bi->bi_error = -EIO; |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3197 | if (!raid5_dec_bi_active_stripes(bi)) |
| 3198 | bio_list_add(return_bi, bi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3199 | bi = nextbi; |
| 3200 | } |
| 3201 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3202 | if (bitmap_end) |
| 3203 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3204 | STRIPE_SECTORS, 0, 0); |
NeilBrown | 8cfa7b0 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3205 | /* If we were in the middle of a write the parity block might |
| 3206 | * still be locked - so just clear all R5_LOCKED flags |
| 3207 | */ |
| 3208 | clear_bit(R5_LOCKED, &sh->dev[i].flags); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3209 | } |
Shaohua Li | ebda780 | 2015-09-18 10:20:13 -0700 | [diff] [blame] | 3210 | s->to_write = 0; |
| 3211 | s->written = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3212 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3213 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3214 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3215 | md_wakeup_thread(conf->mddev->thread); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3216 | } |
| 3217 | |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3218 | static void |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3219 | handle_failed_sync(struct r5conf *conf, struct stripe_head *sh, |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3220 | struct stripe_head_state *s) |
| 3221 | { |
| 3222 | int abort = 0; |
| 3223 | int i; |
| 3224 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3225 | BUG_ON(sh->batch_head); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3226 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3227 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 3228 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3229 | s->syncing = 0; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3230 | s->replacing = 0; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3231 | /* There is nothing more to do for sync/check/repair. |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3232 | * Don't even need to abort as that is handled elsewhere |
| 3233 | * if needed, and not always wanted e.g. if there is a known |
| 3234 | * bad block here. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3235 | * For recover/replace we need to record a bad block on all |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3236 | * non-sync devices, or abort the recovery |
| 3237 | */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3238 | if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) { |
| 3239 | /* During recovery devices cannot be removed, so |
| 3240 | * locking and refcounting of rdevs is not needed |
| 3241 | */ |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3242 | rcu_read_lock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3243 | for (i = 0; i < conf->raid_disks; i++) { |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3244 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3245 | if (rdev |
| 3246 | && !test_bit(Faulty, &rdev->flags) |
| 3247 | && !test_bit(In_sync, &rdev->flags) |
| 3248 | && !rdev_set_badblocks(rdev, sh->sector, |
| 3249 | STRIPE_SECTORS, 0)) |
| 3250 | abort = 1; |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3251 | rdev = rcu_dereference(conf->disks[i].replacement); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3252 | if (rdev |
| 3253 | && !test_bit(Faulty, &rdev->flags) |
| 3254 | && !test_bit(In_sync, &rdev->flags) |
| 3255 | && !rdev_set_badblocks(rdev, sh->sector, |
| 3256 | STRIPE_SECTORS, 0)) |
| 3257 | abort = 1; |
| 3258 | } |
NeilBrown | e50d399 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3259 | rcu_read_unlock(); |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3260 | if (abort) |
| 3261 | conf->recovery_disabled = |
| 3262 | conf->mddev->recovery_disabled; |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3263 | } |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 3264 | md_done_sync(conf->mddev, STRIPE_SECTORS, !abort); |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 3265 | } |
| 3266 | |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3267 | static int want_replace(struct stripe_head *sh, int disk_idx) |
| 3268 | { |
| 3269 | struct md_rdev *rdev; |
| 3270 | int rv = 0; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3271 | |
| 3272 | rcu_read_lock(); |
| 3273 | rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3274 | if (rdev |
| 3275 | && !test_bit(Faulty, &rdev->flags) |
| 3276 | && !test_bit(In_sync, &rdev->flags) |
| 3277 | && (rdev->recovery_offset <= sh->sector |
| 3278 | || rdev->mddev->recovery_cp <= sh->sector)) |
| 3279 | rv = 1; |
NeilBrown | 3f232d6 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 3280 | rcu_read_unlock(); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 3281 | return rv; |
| 3282 | } |
| 3283 | |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3284 | /* fetch_block - checks the given member device to see if its data needs |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3285 | * to be read or computed to satisfy a request. |
| 3286 | * |
| 3287 | * Returns 1 when no more member devices need to be checked, otherwise returns |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3288 | * 0 to tell the loop in handle_stripe_fill to continue |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3289 | */ |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3290 | |
| 3291 | static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3292 | int disk_idx, int disks) |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3293 | { |
| 3294 | struct r5dev *dev = &sh->dev[disk_idx]; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3295 | struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]], |
| 3296 | &sh->dev[s->failed_num[1]] }; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3297 | int i; |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3298 | |
NeilBrown | a79cfe1 | 2015-02-02 11:37:59 +1100 | [diff] [blame] | 3299 | |
| 3300 | if (test_bit(R5_LOCKED, &dev->flags) || |
| 3301 | test_bit(R5_UPTODATE, &dev->flags)) |
| 3302 | /* No point reading this as we already have it or have |
| 3303 | * decided to get it. |
| 3304 | */ |
| 3305 | return 0; |
| 3306 | |
| 3307 | if (dev->toread || |
| 3308 | (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags))) |
| 3309 | /* We need this block to directly satisfy a request */ |
| 3310 | return 1; |
| 3311 | |
| 3312 | if (s->syncing || s->expanding || |
| 3313 | (s->replacing && want_replace(sh, disk_idx))) |
| 3314 | /* When syncing, or expanding we read everything. |
| 3315 | * When replacing, we need the replaced block. |
| 3316 | */ |
| 3317 | return 1; |
| 3318 | |
| 3319 | if ((s->failed >= 1 && fdev[0]->toread) || |
| 3320 | (s->failed >= 2 && fdev[1]->toread)) |
| 3321 | /* If we want to read from a failed device, then |
| 3322 | * we need to actually read every other device. |
| 3323 | */ |
| 3324 | return 1; |
| 3325 | |
NeilBrown | a9d5695 | 2015-02-02 11:49:10 +1100 | [diff] [blame] | 3326 | /* Sometimes neither read-modify-write nor reconstruct-write |
| 3327 | * cycles can work. In those cases we read every block we |
| 3328 | * can. Then the parity-update is certain to have enough to |
| 3329 | * work with. |
| 3330 | * This can only be a problem when we need to write something, |
| 3331 | * and some device has failed. If either of those tests |
| 3332 | * fail we need look no further. |
| 3333 | */ |
| 3334 | if (!s->failed || !s->to_write) |
| 3335 | return 0; |
| 3336 | |
| 3337 | if (test_bit(R5_Insync, &dev->flags) && |
| 3338 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3339 | /* Pre-reads at not permitted until after short delay |
| 3340 | * to gather multiple requests. However if this |
| 3341 | * device is no Insync, the block could only be be computed |
| 3342 | * and there is no need to delay that. |
| 3343 | */ |
| 3344 | return 0; |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3345 | |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3346 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3347 | if (fdev[i]->towrite && |
| 3348 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
| 3349 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3350 | /* If we have a partial write to a failed |
| 3351 | * device, then we will need to reconstruct |
| 3352 | * the content of that device, so all other |
| 3353 | * devices must be read. |
| 3354 | */ |
| 3355 | return 1; |
| 3356 | } |
| 3357 | |
| 3358 | /* If we are forced to do a reconstruct-write, either because |
| 3359 | * the current RAID6 implementation only supports that, or |
| 3360 | * or because parity cannot be trusted and we are currently |
| 3361 | * recovering it, there is extra need to be careful. |
| 3362 | * If one of the devices that we would need to read, because |
| 3363 | * it is not being overwritten (and maybe not written at all) |
| 3364 | * is missing/faulty, then we need to read everything we can. |
| 3365 | */ |
| 3366 | if (sh->raid_conf->level != 6 && |
| 3367 | sh->sector < sh->raid_conf->mddev->recovery_cp) |
| 3368 | /* reconstruct-write isn't being forced */ |
| 3369 | return 0; |
NeilBrown | 36707bb | 2015-09-24 15:25:36 +1000 | [diff] [blame] | 3370 | for (i = 0; i < s->failed && i < 2; i++) { |
NeilBrown | 10d82c5 | 2015-05-08 18:19:33 +1000 | [diff] [blame] | 3371 | if (s->failed_num[i] != sh->pd_idx && |
| 3372 | s->failed_num[i] != sh->qd_idx && |
| 3373 | !test_bit(R5_UPTODATE, &fdev[i]->flags) && |
NeilBrown | ea664c8 | 2015-02-02 14:03:28 +1100 | [diff] [blame] | 3374 | !test_bit(R5_OVERWRITE, &fdev[i]->flags)) |
| 3375 | return 1; |
| 3376 | } |
| 3377 | |
NeilBrown | 2c58f06 | 2015-02-02 11:32:23 +1100 | [diff] [blame] | 3378 | return 0; |
| 3379 | } |
| 3380 | |
| 3381 | static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s, |
| 3382 | int disk_idx, int disks) |
| 3383 | { |
| 3384 | struct r5dev *dev = &sh->dev[disk_idx]; |
| 3385 | |
| 3386 | /* is the data in this block needed, and can we get it? */ |
| 3387 | if (need_this_block(sh, s, disk_idx, disks)) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3388 | /* we would like to get this block, possibly by computing it, |
| 3389 | * otherwise read it if the backing disk is insync |
| 3390 | */ |
| 3391 | BUG_ON(test_bit(R5_Wantcompute, &dev->flags)); |
| 3392 | BUG_ON(test_bit(R5_Wantread, &dev->flags)); |
NeilBrown | b0c783b | 2015-05-08 18:19:32 +1000 | [diff] [blame] | 3393 | BUG_ON(sh->batch_head); |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3394 | if ((s->uptodate == disks - 1) && |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3395 | (s->failed && (disk_idx == s->failed_num[0] || |
| 3396 | disk_idx == s->failed_num[1]))) { |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3397 | /* have disk failed, and we're requested to fetch it; |
| 3398 | * do compute it |
| 3399 | */ |
| 3400 | pr_debug("Computing stripe %llu block %d\n", |
| 3401 | (unsigned long long)sh->sector, disk_idx); |
| 3402 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3403 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3404 | set_bit(R5_Wantcompute, &dev->flags); |
| 3405 | sh->ops.target = disk_idx; |
| 3406 | sh->ops.target2 = -1; /* no 2nd target */ |
| 3407 | s->req_compute = 1; |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3408 | /* Careful: from this point on 'uptodate' is in the eye |
| 3409 | * of raid_run_ops which services 'compute' operations |
| 3410 | * before writes. R5_Wantcompute flags a block that will |
| 3411 | * be R5_UPTODATE by the time it is needed for a |
| 3412 | * subsequent operation. |
| 3413 | */ |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3414 | s->uptodate++; |
| 3415 | return 1; |
| 3416 | } else if (s->uptodate == disks-2 && s->failed >= 2) { |
| 3417 | /* Computing 2-failure is *very* expensive; only |
| 3418 | * do it if failed >= 2 |
| 3419 | */ |
| 3420 | int other; |
| 3421 | for (other = disks; other--; ) { |
| 3422 | if (other == disk_idx) |
| 3423 | continue; |
| 3424 | if (!test_bit(R5_UPTODATE, |
| 3425 | &sh->dev[other].flags)) |
| 3426 | break; |
| 3427 | } |
| 3428 | BUG_ON(other < 0); |
| 3429 | pr_debug("Computing stripe %llu blocks %d,%d\n", |
| 3430 | (unsigned long long)sh->sector, |
| 3431 | disk_idx, other); |
| 3432 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3433 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3434 | set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags); |
| 3435 | set_bit(R5_Wantcompute, &sh->dev[other].flags); |
| 3436 | sh->ops.target = disk_idx; |
| 3437 | sh->ops.target2 = other; |
| 3438 | s->uptodate += 2; |
| 3439 | s->req_compute = 1; |
| 3440 | return 1; |
| 3441 | } else if (test_bit(R5_Insync, &dev->flags)) { |
| 3442 | set_bit(R5_LOCKED, &dev->flags); |
| 3443 | set_bit(R5_Wantread, &dev->flags); |
| 3444 | s->locked++; |
| 3445 | pr_debug("Reading block %d (sync=%d)\n", |
| 3446 | disk_idx, s->syncing); |
| 3447 | } |
| 3448 | } |
| 3449 | |
| 3450 | return 0; |
| 3451 | } |
| 3452 | |
| 3453 | /** |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3454 | * handle_stripe_fill - read or compute data to satisfy pending requests. |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3455 | */ |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3456 | static void handle_stripe_fill(struct stripe_head *sh, |
| 3457 | struct stripe_head_state *s, |
| 3458 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3459 | { |
| 3460 | int i; |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3461 | |
| 3462 | /* look for blocks to read/compute, skip this if a compute |
| 3463 | * is already in flight, or if the stripe contents are in the |
| 3464 | * midst of changing due to a write |
| 3465 | */ |
| 3466 | if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state && |
| 3467 | !sh->reconstruct_state) |
| 3468 | for (i = disks; i--; ) |
NeilBrown | 93b3dbc | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3469 | if (fetch_block(sh, s, i, disks)) |
Yuri Tikhonov | 5599bec | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3470 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3471 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3472 | } |
| 3473 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 3474 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 3475 | unsigned long handle_flags); |
Dan Williams | 1fe797e | 2008-06-28 09:16:30 +1000 | [diff] [blame] | 3476 | /* handle_stripe_clean_event |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3477 | * any written block on an uptodate or failed drive can be returned. |
| 3478 | * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but |
| 3479 | * never LOCKED, so we don't need to test 'failed' directly. |
| 3480 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3481 | static void handle_stripe_clean_event(struct r5conf *conf, |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3482 | struct stripe_head *sh, int disks, struct bio_list *return_bi) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3483 | { |
| 3484 | int i; |
| 3485 | struct r5dev *dev; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3486 | int discard_pending = 0; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3487 | struct stripe_head *head_sh = sh; |
| 3488 | bool do_endio = false; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3489 | |
| 3490 | for (i = disks; i--; ) |
| 3491 | if (sh->dev[i].written) { |
| 3492 | dev = &sh->dev[i]; |
| 3493 | if (!test_bit(R5_LOCKED, &dev->flags) && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 3494 | (test_bit(R5_UPTODATE, &dev->flags) || |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3495 | test_bit(R5_Discard, &dev->flags) || |
| 3496 | test_bit(R5_SkipCopy, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3497 | /* We can return any write requests */ |
| 3498 | struct bio *wbi, *wbi2; |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3499 | pr_debug("Return write for disc %d\n", i); |
NeilBrown | ca64cae | 2012-11-21 16:33:40 +1100 | [diff] [blame] | 3500 | if (test_and_clear_bit(R5_Discard, &dev->flags)) |
| 3501 | clear_bit(R5_UPTODATE, &dev->flags); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3502 | if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) { |
| 3503 | WARN_ON(test_bit(R5_UPTODATE, &dev->flags)); |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 3504 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3505 | do_endio = true; |
| 3506 | |
| 3507 | returnbi: |
| 3508 | dev->page = dev->orig_page; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3509 | wbi = dev->written; |
| 3510 | dev->written = NULL; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 3511 | while (wbi && wbi->bi_iter.bi_sector < |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3512 | dev->sector + STRIPE_SECTORS) { |
| 3513 | wbi2 = r5_next_bio(wbi, dev->sector); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3514 | if (!raid5_dec_bi_active_stripes(wbi)) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3515 | md_write_end(conf->mddev); |
NeilBrown | 34a6f80 | 2015-08-14 12:07:57 +1000 | [diff] [blame] | 3516 | bio_list_add(return_bi, wbi); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3517 | } |
| 3518 | wbi = wbi2; |
| 3519 | } |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3520 | bitmap_endwrite(conf->mddev->bitmap, sh->sector, |
| 3521 | STRIPE_SECTORS, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3522 | !test_bit(STRIPE_DEGRADED, &sh->state), |
Shaohua Li | 7eaf7e8 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 3523 | 0); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3524 | if (head_sh->batch_head) { |
| 3525 | sh = list_first_entry(&sh->batch_list, |
| 3526 | struct stripe_head, |
| 3527 | batch_list); |
| 3528 | if (sh != head_sh) { |
| 3529 | dev = &sh->dev[i]; |
| 3530 | goto returnbi; |
| 3531 | } |
| 3532 | } |
| 3533 | sh = head_sh; |
| 3534 | dev = &sh->dev[i]; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3535 | } else if (test_bit(R5_Discard, &dev->flags)) |
| 3536 | discard_pending = 1; |
| 3537 | } |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 3538 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 3539 | r5l_stripe_write_finished(sh); |
| 3540 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3541 | if (!discard_pending && |
| 3542 | test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) { |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3543 | int hash; |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3544 | clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags); |
| 3545 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
| 3546 | if (sh->qd_idx >= 0) { |
| 3547 | clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags); |
| 3548 | clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags); |
| 3549 | } |
| 3550 | /* now that discard is done we can proceed with any sync */ |
| 3551 | clear_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 3552 | /* |
| 3553 | * SCSI discard will change some bio fields and the stripe has |
| 3554 | * no updated data, so remove it from hash list and the stripe |
| 3555 | * will be reinitialized |
| 3556 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3557 | unhash: |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3558 | hash = sh->hash_lock_index; |
| 3559 | spin_lock_irq(conf->hash_locks + hash); |
Shaohua Li | d47648f | 2013-10-19 14:51:42 +0800 | [diff] [blame] | 3560 | remove_hash(sh); |
Roman Gushchin | b8a9d66 | 2015-10-31 10:53:50 +1100 | [diff] [blame] | 3561 | spin_unlock_irq(conf->hash_locks + hash); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3562 | if (head_sh->batch_head) { |
| 3563 | sh = list_first_entry(&sh->batch_list, |
| 3564 | struct stripe_head, batch_list); |
| 3565 | if (sh != head_sh) |
| 3566 | goto unhash; |
| 3567 | } |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3568 | sh = head_sh; |
| 3569 | |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 3570 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state)) |
| 3571 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3572 | |
| 3573 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 3574 | |
| 3575 | if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state)) |
| 3576 | if (atomic_dec_and_test(&conf->pending_full_writes)) |
| 3577 | md_wakeup_thread(conf->mddev->thread); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3578 | |
NeilBrown | 787b76f | 2015-05-21 12:56:41 +1000 | [diff] [blame] | 3579 | if (head_sh->batch_head && do_endio) |
| 3580 | break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3581 | } |
| 3582 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3583 | static void handle_stripe_dirtying(struct r5conf *conf, |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3584 | struct stripe_head *sh, |
| 3585 | struct stripe_head_state *s, |
| 3586 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3587 | { |
| 3588 | int rmw = 0, rcw = 0, i; |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3589 | sector_t recovery_cp = conf->mddev->recovery_cp; |
| 3590 | |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3591 | /* Check whether resync is now happening or should start. |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3592 | * If yes, then the array is dirty (after unclean shutdown or |
| 3593 | * initial creation), so parity in some stripes might be inconsistent. |
| 3594 | * In this case, we need to always do reconstruct-write, to ensure |
| 3595 | * that in case of drive failure or read-error correction, we |
| 3596 | * generate correct data from the parity. |
| 3597 | */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3598 | if (conf->rmw_level == PARITY_DISABLE_RMW || |
NeilBrown | 26ac107 | 2015-02-18 11:35:14 +1100 | [diff] [blame] | 3599 | (recovery_cp < MaxSector && sh->sector >= recovery_cp && |
| 3600 | s->failed == 0)) { |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3601 | /* Calculate the real rcw later - for now make it |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3602 | * look like rcw is cheaper |
| 3603 | */ |
| 3604 | rcw = 1; rmw = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3605 | pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n", |
| 3606 | conf->rmw_level, (unsigned long long)recovery_cp, |
Alexander Lyakas | a785448 | 2012-10-11 13:50:12 +1100 | [diff] [blame] | 3607 | (unsigned long long)sh->sector); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3608 | } else for (i = disks; i--; ) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3609 | /* would I have to read this buffer for read_modify_write */ |
| 3610 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3611 | if ((dev->towrite || i == sh->pd_idx || i == sh->qd_idx || |
| 3612 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3613 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3614 | !((test_bit(R5_UPTODATE, &dev->flags) && |
| 3615 | (!test_bit(R5_InJournal, &dev->flags) || |
| 3616 | dev->page != dev->orig_page)) || |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3617 | test_bit(R5_Wantcompute, &dev->flags))) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3618 | if (test_bit(R5_Insync, &dev->flags)) |
| 3619 | rmw++; |
| 3620 | else |
| 3621 | rmw += 2*disks; /* cannot read it */ |
| 3622 | } |
| 3623 | /* Would I have to read this buffer for reconstruct_write */ |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 3624 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
| 3625 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3626 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3627 | !(test_bit(R5_UPTODATE, &dev->flags) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3628 | test_bit(R5_InJournal, &dev->flags) || |
| 3629 | test_bit(R5_Wantcompute, &dev->flags))) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3630 | if (test_bit(R5_Insync, &dev->flags)) |
| 3631 | rcw++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3632 | else |
| 3633 | rcw += 2*disks; |
| 3634 | } |
| 3635 | } |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3636 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3637 | pr_debug("for sector %llu, rmw=%d rcw=%d\n", |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3638 | (unsigned long long)sh->sector, rmw, rcw); |
| 3639 | set_bit(STRIPE_HANDLE, &sh->state); |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 3640 | 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] | 3641 | /* prefer read-modify-write, but need to get some data */ |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 3642 | if (conf->mddev->queue) |
| 3643 | blk_add_trace_msg(conf->mddev->queue, |
| 3644 | "raid5 rmw %llu %d", |
| 3645 | (unsigned long long)sh->sector, rmw); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3646 | for (i = disks; i--; ) { |
| 3647 | struct r5dev *dev = &sh->dev[i]; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3648 | if (test_bit(R5_InJournal, &dev->flags) && |
| 3649 | dev->page == dev->orig_page && |
| 3650 | !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) { |
| 3651 | /* alloc page for prexor */ |
| 3652 | dev->orig_page = alloc_page(GFP_NOIO); |
| 3653 | |
| 3654 | /* will handle failure in a later patch*/ |
| 3655 | BUG_ON(!dev->orig_page); |
| 3656 | } |
| 3657 | |
| 3658 | if ((dev->towrite || |
| 3659 | i == sh->pd_idx || i == sh->qd_idx || |
| 3660 | test_bit(R5_InJournal, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3661 | !test_bit(R5_LOCKED, &dev->flags) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3662 | !((test_bit(R5_UPTODATE, &dev->flags) && |
| 3663 | (!test_bit(R5_InJournal, &dev->flags) || |
| 3664 | dev->page != dev->orig_page)) || |
| 3665 | test_bit(R5_Wantcompute, &dev->flags)) && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3666 | test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3667 | if (test_bit(STRIPE_PREREAD_ACTIVE, |
| 3668 | &sh->state)) { |
| 3669 | pr_debug("Read_old block %d for r-m-w\n", |
| 3670 | i); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3671 | set_bit(R5_LOCKED, &dev->flags); |
| 3672 | set_bit(R5_Wantread, &dev->flags); |
| 3673 | s->locked++; |
| 3674 | } else { |
| 3675 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3676 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3677 | } |
| 3678 | } |
| 3679 | } |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3680 | } |
Song Liu | 4125758 | 2016-05-23 17:25:06 -0700 | [diff] [blame] | 3681 | 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] | 3682 | /* want reconstruct write, but need to get some data */ |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3683 | int qread =0; |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3684 | rcw = 0; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3685 | for (i = disks; i--; ) { |
| 3686 | struct r5dev *dev = &sh->dev[i]; |
| 3687 | if (!test_bit(R5_OVERWRITE, &dev->flags) && |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3688 | i != sh->pd_idx && i != sh->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3689 | !test_bit(R5_LOCKED, &dev->flags) && |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3690 | !(test_bit(R5_UPTODATE, &dev->flags) || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3691 | test_bit(R5_InJournal, &dev->flags) || |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3692 | test_bit(R5_Wantcompute, &dev->flags))) { |
| 3693 | rcw++; |
NeilBrown | 67f4554 | 2014-05-28 13:39:22 +1000 | [diff] [blame] | 3694 | if (test_bit(R5_Insync, &dev->flags) && |
| 3695 | test_bit(STRIPE_PREREAD_ACTIVE, |
| 3696 | &sh->state)) { |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3697 | pr_debug("Read_old block " |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3698 | "%d for Reconstruct\n", i); |
| 3699 | set_bit(R5_LOCKED, &dev->flags); |
| 3700 | set_bit(R5_Wantread, &dev->flags); |
| 3701 | s->locked++; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3702 | qread++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3703 | } else { |
| 3704 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3705 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3706 | } |
| 3707 | } |
| 3708 | } |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 3709 | if (rcw && conf->mddev->queue) |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 3710 | blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d", |
| 3711 | (unsigned long long)sh->sector, |
| 3712 | rcw, qread, test_bit(STRIPE_DELAYED, &sh->state)); |
NeilBrown | c8ac180 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 3713 | } |
NeilBrown | b1b02fe | 2015-02-02 10:44:29 +1100 | [diff] [blame] | 3714 | |
| 3715 | if (rcw > disks && rmw > disks && |
| 3716 | !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 3717 | set_bit(STRIPE_DELAYED, &sh->state); |
| 3718 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3719 | /* now if nothing is locked, and if we have enough data, |
| 3720 | * we can start a write request |
| 3721 | */ |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3722 | /* since handle_stripe can be called at any time we need to handle the |
| 3723 | * case where a compute block operation has been submitted and then a |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3724 | * subsequent call wants to start a write request. raid_run_ops only |
| 3725 | * handles the case where compute block and reconstruct are requested |
Dan Williams | f38e121 | 2007-01-02 13:52:30 -0700 | [diff] [blame] | 3726 | * simultaneously. If this is not the case then new writes need to be |
| 3727 | * held off until the compute completes. |
| 3728 | */ |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3729 | if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) && |
| 3730 | (s->locked == 0 && (rcw == 0 || rmw == 0) && |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 3731 | !test_bit(STRIPE_BIT_DELAY, &sh->state))) |
Yuri Tikhonov | c0f7bdd | 2009-08-29 19:13:12 -0700 | [diff] [blame] | 3732 | schedule_reconstruction(sh, s, rcw == 0, 0); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3733 | } |
| 3734 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3735 | static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3736 | struct stripe_head_state *s, int disks) |
| 3737 | { |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3738 | struct r5dev *dev = NULL; |
Dan Williams | e89f896 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3739 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3740 | BUG_ON(sh->batch_head); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3741 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3742 | |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3743 | switch (sh->check_state) { |
| 3744 | case check_state_idle: |
| 3745 | /* start a new check operation if there are no failures */ |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3746 | if (s->failed == 0) { |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3747 | BUG_ON(s->uptodate != disks); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3748 | sh->check_state = check_state_run; |
| 3749 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3750 | clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags); |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3751 | s->uptodate--; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3752 | break; |
Dan Williams | bd2ab67 | 2008-04-10 21:29:27 -0700 | [diff] [blame] | 3753 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3754 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3755 | /* fall through */ |
| 3756 | case check_state_compute_result: |
| 3757 | sh->check_state = check_state_idle; |
| 3758 | if (!dev) |
| 3759 | dev = &sh->dev[sh->pd_idx]; |
| 3760 | |
| 3761 | /* check that a write has not made the stripe insync */ |
| 3762 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3763 | break; |
| 3764 | |
| 3765 | /* either failed parity check, or recovery is happening */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3766 | BUG_ON(!test_bit(R5_UPTODATE, &dev->flags)); |
| 3767 | BUG_ON(s->uptodate != disks); |
| 3768 | |
| 3769 | set_bit(R5_LOCKED, &dev->flags); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3770 | s->locked++; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3771 | set_bit(R5_Wantwrite, &dev->flags); |
Dan Williams | 830ea01 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3772 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3773 | clear_bit(STRIPE_DEGRADED, &sh->state); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3774 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3775 | break; |
| 3776 | case check_state_run: |
| 3777 | break; /* we will be called again upon completion */ |
| 3778 | case check_state_check_result: |
| 3779 | sh->check_state = check_state_idle; |
| 3780 | |
| 3781 | /* if a failure occurred during the check operation, leave |
| 3782 | * STRIPE_INSYNC not set and let the stripe be handled again |
| 3783 | */ |
| 3784 | if (s->failed) |
| 3785 | break; |
| 3786 | |
| 3787 | /* handle a successful check operation, if parity is correct |
| 3788 | * we are done. Otherwise update the mismatch count and repair |
| 3789 | * parity if !MD_RECOVERY_CHECK |
| 3790 | */ |
Dan Williams | ad283ea | 2009-08-29 19:09:26 -0700 | [diff] [blame] | 3791 | if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0) |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3792 | /* parity is correct (on disc, |
| 3793 | * not in buffer any more) |
| 3794 | */ |
| 3795 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3796 | else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 3797 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3798 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3799 | /* don't try to repair!! */ |
| 3800 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3801 | else { |
| 3802 | sh->check_state = check_state_compute_run; |
Dan Williams | 976ea8d | 2008-06-28 08:32:03 +1000 | [diff] [blame] | 3803 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3804 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3805 | set_bit(R5_Wantcompute, |
| 3806 | &sh->dev[sh->pd_idx].flags); |
| 3807 | sh->ops.target = sh->pd_idx; |
Dan Williams | ac6b53b | 2009-07-14 13:40:19 -0700 | [diff] [blame] | 3808 | sh->ops.target2 = -1; |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3809 | s->uptodate++; |
| 3810 | } |
| 3811 | } |
| 3812 | break; |
| 3813 | case check_state_compute_run: |
| 3814 | break; |
| 3815 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3816 | pr_err("%s: unknown check_state: %d sector: %llu\n", |
Dan Williams | ecc65c9 | 2008-06-28 08:31:57 +1000 | [diff] [blame] | 3817 | __func__, sh->check_state, |
| 3818 | (unsigned long long) sh->sector); |
| 3819 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3820 | } |
| 3821 | } |
| 3822 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3823 | static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh, |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3824 | struct stripe_head_state *s, |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3825 | int disks) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3826 | { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3827 | int pd_idx = sh->pd_idx; |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3828 | int qd_idx = sh->qd_idx; |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3829 | struct r5dev *dev; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3830 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3831 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3832 | set_bit(STRIPE_HANDLE, &sh->state); |
| 3833 | |
| 3834 | BUG_ON(s->failed > 2); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3835 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3836 | /* Want to check and possibly repair P and Q. |
| 3837 | * However there could be one 'failed' device, in which |
| 3838 | * case we can only check one of them, possibly using the |
| 3839 | * other to generate missing data |
| 3840 | */ |
| 3841 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3842 | switch (sh->check_state) { |
| 3843 | case check_state_idle: |
| 3844 | /* start a new check operation if there are < 2 failures */ |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3845 | if (s->failed == s->q_failed) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3846 | /* The only possible failed device holds Q, so it |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3847 | * makes sense to check P (If anything else were failed, |
| 3848 | * we would have used P to recreate it). |
| 3849 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3850 | sh->check_state = check_state_run; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3851 | } |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3852 | if (!s->q_failed && s->failed < 2) { |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3853 | /* Q is not failed, and we didn't use it to generate |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3854 | * anything, so it makes sense to check it |
| 3855 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3856 | if (sh->check_state == check_state_run) |
| 3857 | sh->check_state = check_state_run_pq; |
| 3858 | else |
| 3859 | sh->check_state = check_state_run_q; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3860 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3861 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3862 | /* discard potentially stale zero_sum_result */ |
| 3863 | sh->ops.zero_sum_result = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3864 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3865 | if (sh->check_state == check_state_run) { |
| 3866 | /* async_xor_zero_sum destroys the contents of P */ |
| 3867 | clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags); |
| 3868 | s->uptodate--; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3869 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3870 | if (sh->check_state >= check_state_run && |
| 3871 | sh->check_state <= check_state_run_pq) { |
| 3872 | /* async_syndrome_zero_sum preserves P and Q, so |
| 3873 | * no need to mark them !uptodate here |
| 3874 | */ |
| 3875 | set_bit(STRIPE_OP_CHECK, &s->ops_request); |
| 3876 | break; |
| 3877 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3878 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3879 | /* we have 2-disk failure */ |
| 3880 | BUG_ON(s->failed != 2); |
| 3881 | /* fall through */ |
| 3882 | case check_state_compute_result: |
| 3883 | sh->check_state = check_state_idle; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 3884 | |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3885 | /* check that a write has not made the stripe insync */ |
| 3886 | if (test_bit(STRIPE_INSYNC, &sh->state)) |
| 3887 | break; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3888 | |
| 3889 | /* now write out any block on a failed drive, |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3890 | * or P or Q if they were recomputed |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3891 | */ |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3892 | 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] | 3893 | if (s->failed == 2) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3894 | dev = &sh->dev[s->failed_num[1]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3895 | s->locked++; |
| 3896 | set_bit(R5_LOCKED, &dev->flags); |
| 3897 | set_bit(R5_Wantwrite, &dev->flags); |
| 3898 | } |
| 3899 | if (s->failed >= 1) { |
NeilBrown | f2b3b44 | 2011-07-26 11:35:19 +1000 | [diff] [blame] | 3900 | dev = &sh->dev[s->failed_num[0]]; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3901 | s->locked++; |
| 3902 | set_bit(R5_LOCKED, &dev->flags); |
| 3903 | set_bit(R5_Wantwrite, &dev->flags); |
| 3904 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3905 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3906 | dev = &sh->dev[pd_idx]; |
| 3907 | s->locked++; |
| 3908 | set_bit(R5_LOCKED, &dev->flags); |
| 3909 | set_bit(R5_Wantwrite, &dev->flags); |
| 3910 | } |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3911 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3912 | dev = &sh->dev[qd_idx]; |
| 3913 | s->locked++; |
| 3914 | set_bit(R5_LOCKED, &dev->flags); |
| 3915 | set_bit(R5_Wantwrite, &dev->flags); |
| 3916 | } |
| 3917 | clear_bit(STRIPE_DEGRADED, &sh->state); |
| 3918 | |
| 3919 | set_bit(STRIPE_INSYNC, &sh->state); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3920 | break; |
| 3921 | case check_state_run: |
| 3922 | case check_state_run_q: |
| 3923 | case check_state_run_pq: |
| 3924 | break; /* we will be called again upon completion */ |
| 3925 | case check_state_check_result: |
| 3926 | sh->check_state = check_state_idle; |
| 3927 | |
| 3928 | /* handle a successful check operation, if parity is correct |
| 3929 | * we are done. Otherwise update the mismatch count and repair |
| 3930 | * parity if !MD_RECOVERY_CHECK |
| 3931 | */ |
| 3932 | if (sh->ops.zero_sum_result == 0) { |
| 3933 | /* both parities are correct */ |
| 3934 | if (!s->failed) |
| 3935 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3936 | else { |
| 3937 | /* in contrast to the raid5 case we can validate |
| 3938 | * parity, but still have a failure to write |
| 3939 | * back |
| 3940 | */ |
| 3941 | sh->check_state = check_state_compute_result; |
| 3942 | /* Returning at this point means that we may go |
| 3943 | * off and bring p and/or q uptodate again so |
| 3944 | * we make sure to check zero_sum_result again |
| 3945 | * to verify if p or q need writeback |
| 3946 | */ |
| 3947 | } |
| 3948 | } else { |
Jianpeng Ma | 7f7583d | 2012-10-11 14:17:59 +1100 | [diff] [blame] | 3949 | atomic64_add(STRIPE_SECTORS, &conf->mddev->resync_mismatches); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3950 | if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) |
| 3951 | /* don't try to repair!! */ |
| 3952 | set_bit(STRIPE_INSYNC, &sh->state); |
| 3953 | else { |
| 3954 | int *target = &sh->ops.target; |
| 3955 | |
| 3956 | sh->ops.target = -1; |
| 3957 | sh->ops.target2 = -1; |
| 3958 | sh->check_state = check_state_compute_run; |
| 3959 | set_bit(STRIPE_COMPUTE_RUN, &sh->state); |
| 3960 | set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request); |
| 3961 | if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) { |
| 3962 | set_bit(R5_Wantcompute, |
| 3963 | &sh->dev[pd_idx].flags); |
| 3964 | *target = pd_idx; |
| 3965 | target = &sh->ops.target2; |
| 3966 | s->uptodate++; |
| 3967 | } |
| 3968 | if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) { |
| 3969 | set_bit(R5_Wantcompute, |
| 3970 | &sh->dev[qd_idx].flags); |
| 3971 | *target = qd_idx; |
| 3972 | s->uptodate++; |
| 3973 | } |
| 3974 | } |
| 3975 | } |
| 3976 | break; |
| 3977 | case check_state_compute_run: |
| 3978 | break; |
| 3979 | default: |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 3980 | pr_warn("%s: unknown check_state: %d sector: %llu\n", |
| 3981 | __func__, sh->check_state, |
| 3982 | (unsigned long long) sh->sector); |
Dan Williams | d82dfee | 2009-07-14 13:40:57 -0700 | [diff] [blame] | 3983 | BUG(); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3984 | } |
| 3985 | } |
| 3986 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 3987 | static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh) |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3988 | { |
| 3989 | int i; |
| 3990 | |
| 3991 | /* We have read all the blocks in this stripe and now we need to |
| 3992 | * copy some of them into a target stripe for expand. |
| 3993 | */ |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 3994 | struct dma_async_tx_descriptor *tx = NULL; |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 3995 | BUG_ON(sh->batch_head); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 3996 | clear_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 3997 | for (i = 0; i < sh->disks; i++) |
NeilBrown | 34e04e8 | 2009-03-31 15:10:16 +1100 | [diff] [blame] | 3998 | if (i != sh->pd_idx && i != sh->qd_idx) { |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 3999 | int dd_idx, j; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4000 | struct stripe_head *sh2; |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4001 | struct async_submit_ctl submit; |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4002 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4003 | sector_t bn = raid5_compute_blocknr(sh, i, 1); |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 4004 | sector_t s = raid5_compute_sector(conf, bn, 0, |
| 4005 | &dd_idx, NULL); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4006 | sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4007 | if (sh2 == NULL) |
| 4008 | /* so far only the early blocks of this stripe |
| 4009 | * have been requested. When later blocks |
| 4010 | * get requested, we will try again |
| 4011 | */ |
| 4012 | continue; |
| 4013 | if (!test_bit(STRIPE_EXPANDING, &sh2->state) || |
| 4014 | test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) { |
| 4015 | /* must have already done this block */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4016 | raid5_release_stripe(sh2); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4017 | continue; |
| 4018 | } |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4019 | |
| 4020 | /* place all the copies on one channel */ |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4021 | init_async_submit(&submit, 0, tx, NULL, NULL, NULL); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4022 | tx = async_memcpy(sh2->dev[dd_idx].page, |
Dan Williams | 88ba2aa | 2009-04-09 16:16:18 -0700 | [diff] [blame] | 4023 | sh->dev[i].page, 0, 0, STRIPE_SIZE, |
Dan Williams | a08abd8 | 2009-06-03 11:43:59 -0700 | [diff] [blame] | 4024 | &submit); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4025 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4026 | set_bit(R5_Expanded, &sh2->dev[dd_idx].flags); |
| 4027 | set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags); |
| 4028 | for (j = 0; j < conf->raid_disks; j++) |
| 4029 | if (j != sh2->pd_idx && |
NeilBrown | 86c374b | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4030 | j != sh2->qd_idx && |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4031 | !test_bit(R5_Expanded, &sh2->dev[j].flags)) |
| 4032 | break; |
| 4033 | if (j == conf->raid_disks) { |
| 4034 | set_bit(STRIPE_EXPAND_READY, &sh2->state); |
| 4035 | set_bit(STRIPE_HANDLE, &sh2->state); |
| 4036 | } |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4037 | raid5_release_stripe(sh2); |
Dan Williams | f0a50d3 | 2007-01-02 13:52:31 -0700 | [diff] [blame] | 4038 | |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4039 | } |
NeilBrown | a2e0855 | 2007-09-11 15:23:36 -0700 | [diff] [blame] | 4040 | /* done submitting copies, wait for them to complete */ |
NeilBrown | 749586b | 2012-11-20 14:11:15 +1100 | [diff] [blame] | 4041 | async_tx_quiesce(&tx); |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4042 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4043 | |
| 4044 | /* |
| 4045 | * handle_stripe - do things to a stripe. |
| 4046 | * |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4047 | * We lock the stripe by setting STRIPE_ACTIVE and then examine the |
| 4048 | * state of various bits to see what needs to be done. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4049 | * Possible results: |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4050 | * return some read requests which now have data |
| 4051 | * return some write requests which are safely on storage |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4052 | * schedule a read on some buffers |
| 4053 | * schedule a write of some buffers |
| 4054 | * return confirmation of parity correctness |
| 4055 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4056 | */ |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4057 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4058 | static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4059 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4060 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4061 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4062 | struct r5dev *dev; |
| 4063 | int i; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4064 | int do_recovery = 0; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4065 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4066 | memset(s, 0, sizeof(*s)); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4067 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4068 | s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head; |
| 4069 | s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4070 | s->failed_num[0] = -1; |
| 4071 | s->failed_num[1] = -1; |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 4072 | s->log_failed = r5l_log_disk_error(conf); |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4073 | |
| 4074 | /* Now to look around and see what can be done */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4075 | rcu_read_lock(); |
| 4076 | for (i=disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4077 | struct md_rdev *rdev; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4078 | sector_t first_bad; |
| 4079 | int bad_sectors; |
| 4080 | int is_bad = 0; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4081 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4082 | dev = &sh->dev[i]; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4083 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4084 | 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] | 4085 | i, dev->flags, |
| 4086 | dev->toread, dev->towrite, dev->written); |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4087 | /* maybe we can reply to a read |
| 4088 | * |
| 4089 | * new wantfill requests are only permitted while |
| 4090 | * ops_complete_biofill is guaranteed to be inactive |
| 4091 | */ |
| 4092 | if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread && |
| 4093 | !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) |
| 4094 | set_bit(R5_Wantfill, &dev->flags); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4095 | |
| 4096 | /* now count some things */ |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4097 | if (test_bit(R5_LOCKED, &dev->flags)) |
| 4098 | s->locked++; |
| 4099 | if (test_bit(R5_UPTODATE, &dev->flags)) |
| 4100 | s->uptodate++; |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4101 | if (test_bit(R5_Wantcompute, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4102 | s->compute++; |
| 4103 | BUG_ON(s->compute > 2); |
Dan Williams | 2d6e4ec | 2009-09-16 12:11:54 -0700 | [diff] [blame] | 4104 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4105 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4106 | if (test_bit(R5_Wantfill, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4107 | s->to_fill++; |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4108 | else if (dev->toread) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4109 | s->to_read++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4110 | if (dev->towrite) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4111 | s->to_write++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4112 | if (!test_bit(R5_OVERWRITE, &dev->flags)) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4113 | s->non_overwrite++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4114 | } |
Dan Williams | a445685 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4115 | if (dev->written) |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4116 | s->written++; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4117 | /* Prefer to use the replacement for reads, but only |
| 4118 | * if it is recovered enough and has no bad blocks. |
| 4119 | */ |
| 4120 | rdev = rcu_dereference(conf->disks[i].replacement); |
| 4121 | if (rdev && !test_bit(Faulty, &rdev->flags) && |
| 4122 | rdev->recovery_offset >= sh->sector + STRIPE_SECTORS && |
| 4123 | !is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 4124 | &first_bad, &bad_sectors)) |
| 4125 | set_bit(R5_ReadRepl, &dev->flags); |
| 4126 | else { |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4127 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4128 | set_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | e6030cb | 2015-07-17 13:26:23 +1000 | [diff] [blame] | 4129 | else |
| 4130 | clear_bit(R5_NeedReplace, &dev->flags); |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4131 | rdev = rcu_dereference(conf->disks[i].rdev); |
| 4132 | clear_bit(R5_ReadRepl, &dev->flags); |
| 4133 | } |
NeilBrown | 9283d8c | 2011-12-08 16:27:57 +1100 | [diff] [blame] | 4134 | if (rdev && test_bit(Faulty, &rdev->flags)) |
| 4135 | rdev = NULL; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4136 | if (rdev) { |
| 4137 | is_bad = is_badblock(rdev, sh->sector, STRIPE_SECTORS, |
| 4138 | &first_bad, &bad_sectors); |
| 4139 | if (s->blocked_rdev == NULL |
| 4140 | && (test_bit(Blocked, &rdev->flags) |
| 4141 | || is_bad < 0)) { |
| 4142 | if (is_bad < 0) |
| 4143 | set_bit(BlockedBadBlocks, |
| 4144 | &rdev->flags); |
| 4145 | s->blocked_rdev = rdev; |
| 4146 | atomic_inc(&rdev->nr_pending); |
| 4147 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4148 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4149 | clear_bit(R5_Insync, &dev->flags); |
| 4150 | if (!rdev) |
| 4151 | /* Not in-sync */; |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4152 | else if (is_bad) { |
| 4153 | /* also not in-sync */ |
NeilBrown | 18b9837 | 2012-04-01 23:48:38 +1000 | [diff] [blame] | 4154 | if (!test_bit(WriteErrorSeen, &rdev->flags) && |
| 4155 | test_bit(R5_UPTODATE, &dev->flags)) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4156 | /* treat as in-sync, but with a read error |
| 4157 | * which we can now try to correct |
| 4158 | */ |
| 4159 | set_bit(R5_Insync, &dev->flags); |
| 4160 | set_bit(R5_ReadError, &dev->flags); |
| 4161 | } |
| 4162 | } else if (test_bit(In_sync, &rdev->flags)) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4163 | set_bit(R5_Insync, &dev->flags); |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 4164 | else if (sh->sector + STRIPE_SECTORS <= rdev->recovery_offset) |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4165 | /* in sync if before recovery_offset */ |
NeilBrown | 30d7a48 | 2011-12-23 09:57:00 +1100 | [diff] [blame] | 4166 | set_bit(R5_Insync, &dev->flags); |
| 4167 | else if (test_bit(R5_UPTODATE, &dev->flags) && |
| 4168 | test_bit(R5_Expanded, &dev->flags)) |
| 4169 | /* If we've reshaped into here, we assume it is Insync. |
| 4170 | * We will shortly update recovery_offset to make |
| 4171 | * it official. |
| 4172 | */ |
| 4173 | set_bit(R5_Insync, &dev->flags); |
| 4174 | |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4175 | if (test_bit(R5_WriteError, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4176 | /* This flag does not apply to '.replacement' |
| 4177 | * only to .rdev, so make sure to check that*/ |
| 4178 | struct md_rdev *rdev2 = rcu_dereference( |
| 4179 | conf->disks[i].rdev); |
| 4180 | if (rdev2 == rdev) |
| 4181 | clear_bit(R5_Insync, &dev->flags); |
| 4182 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4183 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4184 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4185 | } else |
| 4186 | clear_bit(R5_WriteError, &dev->flags); |
| 4187 | } |
NeilBrown | 1cc03eb | 2014-01-06 13:19:42 +1100 | [diff] [blame] | 4188 | if (test_bit(R5_MadeGood, &dev->flags)) { |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4189 | /* This flag does not apply to '.replacement' |
| 4190 | * only to .rdev, so make sure to check that*/ |
| 4191 | struct md_rdev *rdev2 = rcu_dereference( |
| 4192 | conf->disks[i].rdev); |
| 4193 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4194 | s->handle_bad_blocks = 1; |
NeilBrown | 14a75d3 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4195 | atomic_inc(&rdev2->nr_pending); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4196 | } else |
| 4197 | clear_bit(R5_MadeGood, &dev->flags); |
| 4198 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4199 | if (test_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4200 | struct md_rdev *rdev2 = rcu_dereference( |
| 4201 | conf->disks[i].replacement); |
| 4202 | if (rdev2 && !test_bit(Faulty, &rdev2->flags)) { |
| 4203 | s->handle_bad_blocks = 1; |
| 4204 | atomic_inc(&rdev2->nr_pending); |
| 4205 | } else |
| 4206 | clear_bit(R5_MadeGoodRepl, &dev->flags); |
| 4207 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4208 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4209 | /* The ReadError flag will just be confusing now */ |
| 4210 | clear_bit(R5_ReadError, &dev->flags); |
| 4211 | clear_bit(R5_ReWrite, &dev->flags); |
| 4212 | } |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4213 | if (test_bit(R5_ReadError, &dev->flags)) |
| 4214 | clear_bit(R5_Insync, &dev->flags); |
| 4215 | if (!test_bit(R5_Insync, &dev->flags)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4216 | if (s->failed < 2) |
| 4217 | s->failed_num[s->failed] = i; |
| 4218 | s->failed++; |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4219 | if (rdev && !test_bit(Faulty, &rdev->flags)) |
| 4220 | do_recovery = 1; |
NeilBrown | 415e72d | 2010-06-17 17:25:21 +1000 | [diff] [blame] | 4221 | } |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4222 | |
| 4223 | if (test_bit(R5_InJournal, &dev->flags)) |
| 4224 | s->injournal++; |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4225 | if (test_bit(R5_InJournal, &dev->flags) && dev->written) |
| 4226 | s->just_cached++; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4227 | } |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4228 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
| 4229 | /* If there is a failed device being replaced, |
| 4230 | * we must be recovering. |
| 4231 | * else if we are after recovery_cp, we must be syncing |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4232 | * else if MD_RECOVERY_REQUESTED is set, we also are syncing. |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4233 | * else we can only be replacing |
| 4234 | * sync and recovery both need to read all devices, and so |
| 4235 | * use the same flag. |
| 4236 | */ |
| 4237 | if (do_recovery || |
majianpeng | c6d2e08 | 2012-04-02 01:16:59 +1000 | [diff] [blame] | 4238 | sh->sector >= conf->mddev->recovery_cp || |
| 4239 | test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery))) |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4240 | s->syncing = 1; |
| 4241 | else |
| 4242 | s->replacing = 1; |
| 4243 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4244 | rcu_read_unlock(); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4245 | } |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 4246 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4247 | static int clear_batch_ready(struct stripe_head *sh) |
| 4248 | { |
NeilBrown | b15a9db | 2015-05-22 15:20:04 +1000 | [diff] [blame] | 4249 | /* Return '1' if this is a member of batch, or |
| 4250 | * '0' if it is a lone stripe or a head which can now be |
| 4251 | * handled. |
| 4252 | */ |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4253 | struct stripe_head *tmp; |
| 4254 | if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state)) |
NeilBrown | b15a9db | 2015-05-22 15:20:04 +1000 | [diff] [blame] | 4255 | return (sh->batch_head && sh->batch_head != sh); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4256 | spin_lock(&sh->stripe_lock); |
| 4257 | if (!sh->batch_head) { |
| 4258 | spin_unlock(&sh->stripe_lock); |
| 4259 | return 0; |
| 4260 | } |
| 4261 | |
| 4262 | /* |
| 4263 | * this stripe could be added to a batch list before we check |
| 4264 | * BATCH_READY, skips it |
| 4265 | */ |
| 4266 | if (sh->batch_head != sh) { |
| 4267 | spin_unlock(&sh->stripe_lock); |
| 4268 | return 1; |
| 4269 | } |
| 4270 | spin_lock(&sh->batch_lock); |
| 4271 | list_for_each_entry(tmp, &sh->batch_list, batch_list) |
| 4272 | clear_bit(STRIPE_BATCH_READY, &tmp->state); |
| 4273 | spin_unlock(&sh->batch_lock); |
| 4274 | spin_unlock(&sh->stripe_lock); |
| 4275 | |
| 4276 | /* |
| 4277 | * BATCH_READY is cleared, no new stripes can be added. |
| 4278 | * batch_list can be accessed without lock |
| 4279 | */ |
| 4280 | return 0; |
| 4281 | } |
| 4282 | |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4283 | static void break_stripe_batch_list(struct stripe_head *head_sh, |
| 4284 | unsigned long handle_flags) |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4285 | { |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4286 | struct stripe_head *sh, *next; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4287 | int i; |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4288 | int do_wakeup = 0; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4289 | |
NeilBrown | bb27051 | 2015-05-08 18:19:40 +1000 | [diff] [blame] | 4290 | list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) { |
| 4291 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4292 | list_del_init(&sh->batch_list); |
| 4293 | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4294 | WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4295 | (1 << STRIPE_SYNCING) | |
| 4296 | (1 << STRIPE_REPLACED) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4297 | (1 << STRIPE_DELAYED) | |
| 4298 | (1 << STRIPE_BIT_DELAY) | |
| 4299 | (1 << STRIPE_FULL_WRITE) | |
| 4300 | (1 << STRIPE_BIOFILL_RUN) | |
| 4301 | (1 << STRIPE_COMPUTE_RUN) | |
| 4302 | (1 << STRIPE_OPS_REQ_PENDING) | |
| 4303 | (1 << STRIPE_DISCARD) | |
| 4304 | (1 << STRIPE_BATCH_READY) | |
| 4305 | (1 << STRIPE_BATCH_ERR) | |
Shaohua Li | fb3229d | 2016-03-09 10:08:38 -0800 | [diff] [blame] | 4306 | (1 << STRIPE_BITMAP_PENDING)), |
| 4307 | "stripe state: %lx\n", sh->state); |
| 4308 | WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) | |
| 4309 | (1 << STRIPE_REPLACED)), |
| 4310 | "head stripe state: %lx\n", head_sh->state); |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4311 | |
| 4312 | set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS | |
NeilBrown | 550da24 | 2016-03-09 12:58:25 +1100 | [diff] [blame] | 4313 | (1 << STRIPE_PREREAD_ACTIVE) | |
NeilBrown | 1b956f7 | 2015-05-21 12:40:26 +1000 | [diff] [blame] | 4314 | (1 << STRIPE_DEGRADED)), |
| 4315 | head_sh->state & (1 << STRIPE_INSYNC)); |
| 4316 | |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4317 | sh->check_state = head_sh->check_state; |
| 4318 | sh->reconstruct_state = head_sh->reconstruct_state; |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4319 | for (i = 0; i < sh->disks; i++) { |
| 4320 | if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags)) |
| 4321 | do_wakeup = 1; |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4322 | sh->dev[i].flags = head_sh->dev[i].flags & |
| 4323 | (~((1 << R5_WriteError) | (1 << R5_Overlap))); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4324 | } |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4325 | spin_lock_irq(&sh->stripe_lock); |
| 4326 | sh->batch_head = NULL; |
| 4327 | spin_unlock_irq(&sh->stripe_lock); |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4328 | if (handle_flags == 0 || |
| 4329 | sh->state & handle_flags) |
| 4330 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4331 | raid5_release_stripe(sh); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4332 | } |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4333 | spin_lock_irq(&head_sh->stripe_lock); |
| 4334 | head_sh->batch_head = NULL; |
| 4335 | spin_unlock_irq(&head_sh->stripe_lock); |
| 4336 | for (i = 0; i < head_sh->disks; i++) |
| 4337 | if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags)) |
| 4338 | do_wakeup = 1; |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4339 | if (head_sh->state & handle_flags) |
| 4340 | set_bit(STRIPE_HANDLE, &head_sh->state); |
NeilBrown | fb642b9 | 2015-05-21 12:00:47 +1000 | [diff] [blame] | 4341 | |
| 4342 | if (do_wakeup) |
| 4343 | wake_up(&head_sh->raid_conf->wait_for_overlap); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4344 | } |
| 4345 | |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4346 | static void handle_stripe(struct stripe_head *sh) |
| 4347 | { |
| 4348 | struct stripe_head_state s; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4349 | struct r5conf *conf = sh->raid_conf; |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4350 | int i; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4351 | int prexor; |
| 4352 | int disks = sh->disks; |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4353 | struct r5dev *pdev, *qdev; |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4354 | |
| 4355 | clear_bit(STRIPE_HANDLE, &sh->state); |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 4356 | if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) { |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4357 | /* already being handled, ensure it gets handled |
| 4358 | * again when current action finishes */ |
| 4359 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4360 | return; |
| 4361 | } |
| 4362 | |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4363 | if (clear_batch_ready(sh) ) { |
| 4364 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
| 4365 | return; |
| 4366 | } |
| 4367 | |
NeilBrown | 4e3d62f | 2015-05-21 11:50:16 +1000 | [diff] [blame] | 4368 | if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state)) |
NeilBrown | 3960ce7 | 2015-05-21 12:20:36 +1000 | [diff] [blame] | 4369 | break_stripe_batch_list(sh, 0); |
shli@kernel.org | 72ac733 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 4370 | |
shli@kernel.org | dabc4ec | 2014-12-15 12:57:04 +1100 | [diff] [blame] | 4371 | if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) { |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4372 | spin_lock(&sh->stripe_lock); |
| 4373 | /* Cannot process 'sync' concurrently with 'discard' */ |
| 4374 | if (!test_bit(STRIPE_DISCARD, &sh->state) && |
| 4375 | test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) { |
| 4376 | set_bit(STRIPE_SYNCING, &sh->state); |
| 4377 | clear_bit(STRIPE_INSYNC, &sh->state); |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4378 | clear_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4379 | } |
| 4380 | spin_unlock(&sh->stripe_lock); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4381 | } |
| 4382 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4383 | |
| 4384 | pr_debug("handling stripe %llu, state=%#lx cnt=%d, " |
| 4385 | "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n", |
| 4386 | (unsigned long long)sh->sector, sh->state, |
| 4387 | atomic_read(&sh->count), sh->pd_idx, sh->qd_idx, |
| 4388 | sh->check_state, sh->reconstruct_state); |
NeilBrown | cc94015 | 2011-07-26 11:35:35 +1000 | [diff] [blame] | 4389 | |
NeilBrown | acfe726 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4390 | analyse_stripe(sh, &s); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4391 | |
Shaohua Li | b70abcb2 | 2015-08-13 14:31:58 -0700 | [diff] [blame] | 4392 | if (test_bit(STRIPE_LOG_TRAPPED, &sh->state)) |
| 4393 | goto finish; |
| 4394 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4395 | if (s.handle_bad_blocks) { |
| 4396 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4397 | goto finish; |
| 4398 | } |
| 4399 | |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4400 | if (unlikely(s.blocked_rdev)) { |
| 4401 | if (s.syncing || s.expanding || s.expanded || |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4402 | s.replacing || s.to_write || s.written) { |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4403 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4404 | goto finish; |
| 4405 | } |
| 4406 | /* There is nothing for the blocked_rdev to block */ |
| 4407 | rdev_dec_pending(s.blocked_rdev, conf->mddev); |
| 4408 | s.blocked_rdev = NULL; |
| 4409 | } |
| 4410 | |
| 4411 | if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) { |
| 4412 | set_bit(STRIPE_OP_BIOFILL, &s.ops_request); |
| 4413 | set_bit(STRIPE_BIOFILL_RUN, &sh->state); |
| 4414 | } |
| 4415 | |
| 4416 | pr_debug("locked=%d uptodate=%d to_read=%d" |
| 4417 | " to_write=%d failed=%d failed_num=%d,%d\n", |
| 4418 | s.locked, s.uptodate, s.to_read, s.to_write, s.failed, |
| 4419 | s.failed_num[0], s.failed_num[1]); |
| 4420 | /* check if the array has lost more than max_degraded devices and, |
| 4421 | * if so, some requests might need to be failed. |
| 4422 | */ |
Shaohua Li | 6e74a9c | 2015-10-08 21:54:08 -0700 | [diff] [blame] | 4423 | if (s.failed > conf->max_degraded || s.log_failed) { |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4424 | sh->check_state = 0; |
| 4425 | sh->reconstruct_state = 0; |
NeilBrown | 626f209 | 2015-05-22 14:03:10 +1000 | [diff] [blame] | 4426 | break_stripe_batch_list(sh, 0); |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4427 | if (s.to_read+s.to_write+s.written) |
| 4428 | handle_failed_stripe(conf, sh, &s, disks, &s.return_bi); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4429 | if (s.syncing + s.replacing) |
NeilBrown | 9a3f530 | 2011-11-08 16:22:01 +1100 | [diff] [blame] | 4430 | handle_failed_sync(conf, sh, &s); |
| 4431 | } |
NeilBrown | 474af965fe | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4432 | |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4433 | /* Now we check to see if any write operations have recently |
| 4434 | * completed |
| 4435 | */ |
| 4436 | prexor = 0; |
| 4437 | if (sh->reconstruct_state == reconstruct_state_prexor_drain_result) |
| 4438 | prexor = 1; |
| 4439 | if (sh->reconstruct_state == reconstruct_state_drain_result || |
| 4440 | sh->reconstruct_state == reconstruct_state_prexor_drain_result) { |
| 4441 | sh->reconstruct_state = reconstruct_state_idle; |
| 4442 | |
| 4443 | /* All the 'written' buffers and the parity block are ready to |
| 4444 | * be written back to disk |
| 4445 | */ |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4446 | BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) && |
| 4447 | !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4448 | BUG_ON(sh->qd_idx >= 0 && |
Shaohua Li | 9e444768 | 2012-10-11 13:49:49 +1100 | [diff] [blame] | 4449 | !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) && |
| 4450 | !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags)); |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4451 | for (i = disks; i--; ) { |
| 4452 | struct r5dev *dev = &sh->dev[i]; |
| 4453 | if (test_bit(R5_LOCKED, &dev->flags) && |
| 4454 | (i == sh->pd_idx || i == sh->qd_idx || |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4455 | dev->written || test_bit(R5_InJournal, |
| 4456 | &dev->flags))) { |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4457 | pr_debug("Writing block %d\n", i); |
| 4458 | set_bit(R5_Wantwrite, &dev->flags); |
| 4459 | if (prexor) |
| 4460 | continue; |
NeilBrown | 9c4bdf6 | 2014-08-13 09:57:07 +1000 | [diff] [blame] | 4461 | if (s.failed > 1) |
| 4462 | continue; |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4463 | if (!test_bit(R5_Insync, &dev->flags) || |
| 4464 | ((i == sh->pd_idx || i == sh->qd_idx) && |
| 4465 | s.failed == 0)) |
| 4466 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4467 | } |
| 4468 | } |
| 4469 | if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4470 | s.dec_preread_active = 1; |
| 4471 | } |
| 4472 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 4473 | /* |
| 4474 | * might be able to return some write requests if the parity blocks |
| 4475 | * are safe, or on a failed drive |
| 4476 | */ |
| 4477 | pdev = &sh->dev[sh->pd_idx]; |
| 4478 | s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx) |
| 4479 | || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx); |
| 4480 | qdev = &sh->dev[sh->qd_idx]; |
| 4481 | s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx) |
| 4482 | || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx) |
| 4483 | || conf->level < 6; |
| 4484 | |
| 4485 | if (s.written && |
| 4486 | (s.p_failed || ((test_bit(R5_Insync, &pdev->flags) |
| 4487 | && !test_bit(R5_LOCKED, &pdev->flags) |
| 4488 | && (test_bit(R5_UPTODATE, &pdev->flags) || |
| 4489 | test_bit(R5_Discard, &pdev->flags))))) && |
| 4490 | (s.q_failed || ((test_bit(R5_Insync, &qdev->flags) |
| 4491 | && !test_bit(R5_LOCKED, &qdev->flags) |
| 4492 | && (test_bit(R5_UPTODATE, &qdev->flags) || |
| 4493 | test_bit(R5_Discard, &qdev->flags)))))) |
| 4494 | handle_stripe_clean_event(conf, sh, disks, &s.return_bi); |
| 4495 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 4496 | if (s.just_cached) |
| 4497 | r5c_handle_cached_data_endio(conf, sh, disks, &s.return_bi); |
| 4498 | r5l_stripe_write_finished(sh); |
| 4499 | |
NeilBrown | ef5b7c6 | 2012-11-22 09:13:36 +1100 | [diff] [blame] | 4500 | /* Now we might consider reading some blocks, either to check/generate |
| 4501 | * parity, or to satisfy requests |
| 4502 | * or to load a block that is being partially written. |
| 4503 | */ |
| 4504 | if (s.to_read || s.non_overwrite |
| 4505 | || (conf->level == 6 && s.to_write && s.failed) |
| 4506 | || (s.syncing && (s.uptodate + s.compute < disks)) |
| 4507 | || s.replacing |
| 4508 | || s.expanding) |
| 4509 | handle_stripe_fill(sh, &s, disks); |
| 4510 | |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4511 | /* |
| 4512 | * When the stripe finishes full journal write cycle (write to journal |
| 4513 | * and raid disk), this is the clean up procedure so it is ready for |
| 4514 | * next operation. |
| 4515 | */ |
| 4516 | r5c_finish_stripe_write_out(conf, sh, &s); |
| 4517 | |
| 4518 | /* |
| 4519 | * Now to consider new write requests, cache write back and what else, |
| 4520 | * if anything should be read. We do not handle new writes when: |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4521 | * 1/ A 'write' operation (copy+xor) is already in flight. |
| 4522 | * 2/ A 'check' operation is in flight, as it may clobber the parity |
| 4523 | * block. |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4524 | * 3/ A r5c cache log write is in flight. |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4525 | */ |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 4526 | |
| 4527 | if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) { |
| 4528 | if (!r5c_is_writeback(conf->log)) { |
| 4529 | if (s.to_write) |
| 4530 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 4531 | } else { /* write back cache */ |
| 4532 | int ret = 0; |
| 4533 | |
| 4534 | /* First, try handle writes in caching phase */ |
| 4535 | if (s.to_write) |
| 4536 | ret = r5c_try_caching_write(conf, sh, &s, |
| 4537 | disks); |
| 4538 | /* |
| 4539 | * If caching phase failed: ret == -EAGAIN |
| 4540 | * OR |
| 4541 | * stripe under reclaim: !caching && injournal |
| 4542 | * |
| 4543 | * fall back to handle_stripe_dirtying() |
| 4544 | */ |
| 4545 | if (ret == -EAGAIN || |
| 4546 | /* stripe under reclaim: !caching && injournal */ |
| 4547 | (!test_bit(STRIPE_R5C_CACHING, &sh->state) && |
| 4548 | s.injournal > 0)) |
| 4549 | handle_stripe_dirtying(conf, sh, &s, disks); |
| 4550 | } |
| 4551 | } |
NeilBrown | 8478955 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4552 | |
| 4553 | /* maybe we need to check and possibly fix the parity for this stripe |
| 4554 | * Any reads will already have been scheduled, so we just see if enough |
| 4555 | * data is available. The parity check is held off while parity |
| 4556 | * dependent operations are in flight. |
| 4557 | */ |
| 4558 | if (sh->check_state || |
| 4559 | (s.syncing && s.locked == 0 && |
| 4560 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
| 4561 | !test_bit(STRIPE_INSYNC, &sh->state))) { |
| 4562 | if (conf->level == 6) |
| 4563 | handle_parity_checks6(conf, sh, &s, disks); |
| 4564 | else |
| 4565 | handle_parity_checks5(conf, sh, &s, disks); |
| 4566 | } |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4567 | |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4568 | if ((s.replacing || s.syncing) && s.locked == 0 |
| 4569 | && !test_bit(STRIPE_COMPUTE_RUN, &sh->state) |
| 4570 | && !test_bit(STRIPE_REPLACED, &sh->state)) { |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4571 | /* Write out to replacement devices where possible */ |
| 4572 | for (i = 0; i < conf->raid_disks; i++) |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4573 | if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) { |
| 4574 | WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags)); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4575 | set_bit(R5_WantReplace, &sh->dev[i].flags); |
| 4576 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 4577 | s.locked++; |
| 4578 | } |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4579 | if (s.replacing) |
| 4580 | set_bit(STRIPE_INSYNC, &sh->state); |
| 4581 | set_bit(STRIPE_REPLACED, &sh->state); |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4582 | } |
| 4583 | if ((s.syncing || s.replacing) && s.locked == 0 && |
NeilBrown | f94c0b6 | 2013-07-22 12:57:21 +1000 | [diff] [blame] | 4584 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state) && |
NeilBrown | 9a3e110 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4585 | test_bit(STRIPE_INSYNC, &sh->state)) { |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4586 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 4587 | clear_bit(STRIPE_SYNCING, &sh->state); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 4588 | if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags)) |
| 4589 | wake_up(&conf->wait_for_overlap); |
NeilBrown | c5a3100 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4590 | } |
| 4591 | |
| 4592 | /* If the failed drives are just a ReadError, then we might need |
| 4593 | * to progress the repair/check process |
| 4594 | */ |
| 4595 | if (s.failed <= conf->max_degraded && !conf->mddev->ro) |
| 4596 | for (i = 0; i < s.failed; i++) { |
| 4597 | struct r5dev *dev = &sh->dev[s.failed_num[i]]; |
| 4598 | if (test_bit(R5_ReadError, &dev->flags) |
| 4599 | && !test_bit(R5_LOCKED, &dev->flags) |
| 4600 | && test_bit(R5_UPTODATE, &dev->flags) |
| 4601 | ) { |
| 4602 | if (!test_bit(R5_ReWrite, &dev->flags)) { |
| 4603 | set_bit(R5_Wantwrite, &dev->flags); |
| 4604 | set_bit(R5_ReWrite, &dev->flags); |
| 4605 | set_bit(R5_LOCKED, &dev->flags); |
| 4606 | s.locked++; |
| 4607 | } else { |
| 4608 | /* let's read it back */ |
| 4609 | set_bit(R5_Wantread, &dev->flags); |
| 4610 | set_bit(R5_LOCKED, &dev->flags); |
| 4611 | s.locked++; |
| 4612 | } |
| 4613 | } |
| 4614 | } |
| 4615 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4616 | /* Finish reconstruct operations initiated by the expansion process */ |
| 4617 | if (sh->reconstruct_state == reconstruct_state_result) { |
| 4618 | struct stripe_head *sh_src |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4619 | = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4620 | if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) { |
| 4621 | /* sh cannot be written until sh_src has been read. |
| 4622 | * so arrange for sh to be delayed a little |
| 4623 | */ |
| 4624 | set_bit(STRIPE_DELAYED, &sh->state); |
| 4625 | set_bit(STRIPE_HANDLE, &sh->state); |
| 4626 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, |
| 4627 | &sh_src->state)) |
| 4628 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4629 | raid5_release_stripe(sh_src); |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4630 | goto finish; |
| 4631 | } |
| 4632 | if (sh_src) |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 4633 | raid5_release_stripe(sh_src); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4634 | |
NeilBrown | 3687c06 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 4635 | sh->reconstruct_state = reconstruct_state_idle; |
| 4636 | clear_bit(STRIPE_EXPANDING, &sh->state); |
| 4637 | for (i = conf->raid_disks; i--; ) { |
| 4638 | set_bit(R5_Wantwrite, &sh->dev[i].flags); |
| 4639 | set_bit(R5_LOCKED, &sh->dev[i].flags); |
| 4640 | s.locked++; |
| 4641 | } |
| 4642 | } |
| 4643 | |
| 4644 | if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) && |
| 4645 | !sh->reconstruct_state) { |
| 4646 | /* Need to write out all blocks after computing parity */ |
| 4647 | sh->disks = conf->raid_disks; |
| 4648 | stripe_set_idx(sh->sector, conf, 0, sh); |
| 4649 | schedule_reconstruction(sh, &s, 1, 1); |
| 4650 | } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) { |
| 4651 | clear_bit(STRIPE_EXPAND_READY, &sh->state); |
| 4652 | atomic_dec(&conf->reshape_stripes); |
| 4653 | wake_up(&conf->wait_for_overlap); |
| 4654 | md_done_sync(conf->mddev, STRIPE_SECTORS, 1); |
| 4655 | } |
| 4656 | |
| 4657 | if (s.expanding && s.locked == 0 && |
| 4658 | !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) |
| 4659 | handle_stripe_expansion(conf, sh); |
| 4660 | |
| 4661 | finish: |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4662 | /* wait for this device to become unblocked */ |
NeilBrown | 5f066c6 | 2012-07-03 12:13:29 +1000 | [diff] [blame] | 4663 | if (unlikely(s.blocked_rdev)) { |
| 4664 | if (conf->mddev->external) |
| 4665 | md_wait_for_blocked_rdev(s.blocked_rdev, |
| 4666 | conf->mddev); |
| 4667 | else |
| 4668 | /* Internal metadata will immediately |
| 4669 | * be written by raid5d, so we don't |
| 4670 | * need to wait here. |
| 4671 | */ |
| 4672 | rdev_dec_pending(s.blocked_rdev, |
| 4673 | conf->mddev); |
| 4674 | } |
Dan Williams | 6bfe0b4 | 2008-04-30 00:52:32 -0700 | [diff] [blame] | 4675 | |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4676 | if (s.handle_bad_blocks) |
| 4677 | for (i = disks; i--; ) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4678 | struct md_rdev *rdev; |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4679 | struct r5dev *dev = &sh->dev[i]; |
| 4680 | if (test_and_clear_bit(R5_WriteError, &dev->flags)) { |
| 4681 | /* We own a safe reference to the rdev */ |
| 4682 | rdev = conf->disks[i].rdev; |
| 4683 | if (!rdev_set_badblocks(rdev, sh->sector, |
| 4684 | STRIPE_SECTORS, 0)) |
| 4685 | md_error(conf->mddev, rdev); |
| 4686 | rdev_dec_pending(rdev, conf->mddev); |
| 4687 | } |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4688 | if (test_and_clear_bit(R5_MadeGood, &dev->flags)) { |
| 4689 | rdev = conf->disks[i].rdev; |
| 4690 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4691 | STRIPE_SECTORS, 0); |
NeilBrown | b84db56 | 2011-07-28 11:39:23 +1000 | [diff] [blame] | 4692 | rdev_dec_pending(rdev, conf->mddev); |
| 4693 | } |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4694 | if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) { |
| 4695 | rdev = conf->disks[i].replacement; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4696 | if (!rdev) |
| 4697 | /* rdev have been moved down */ |
| 4698 | rdev = conf->disks[i].rdev; |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4699 | rdev_clear_badblocks(rdev, sh->sector, |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 4700 | STRIPE_SECTORS, 0); |
NeilBrown | 977df36 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 4701 | rdev_dec_pending(rdev, conf->mddev); |
| 4702 | } |
NeilBrown | bc2607f | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4703 | } |
| 4704 | |
Yuri Tikhonov | 6c0069c | 2009-08-29 19:13:13 -0700 | [diff] [blame] | 4705 | if (s.ops_request) |
| 4706 | raid_run_ops(sh, s.ops_request); |
| 4707 | |
Dan Williams | f0e43bc | 2008-06-28 08:31:55 +1000 | [diff] [blame] | 4708 | ops_run_io(sh, &s); |
| 4709 | |
NeilBrown | c5709ef | 2011-07-26 11:35:20 +1000 | [diff] [blame] | 4710 | if (s.dec_preread_active) { |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4711 | /* 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] | 4712 | * is waiting on a flush, it won't continue until the writes |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 4713 | * have actually been submitted. |
| 4714 | */ |
| 4715 | atomic_dec(&conf->preread_active_stripes); |
| 4716 | if (atomic_read(&conf->preread_active_stripes) < |
| 4717 | IO_THRESHOLD) |
| 4718 | md_wakeup_thread(conf->mddev->thread); |
| 4719 | } |
| 4720 | |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 4721 | if (!bio_list_empty(&s.return_bi)) { |
Tomasz Majchrzak | 7adb072 | 2016-10-26 09:20:39 +0200 | [diff] [blame] | 4722 | if (test_bit(MD_CHANGE_PENDING, &conf->mddev->flags)) { |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 4723 | spin_lock_irq(&conf->device_lock); |
| 4724 | bio_list_merge(&conf->return_bi, &s.return_bi); |
| 4725 | spin_unlock_irq(&conf->device_lock); |
| 4726 | md_wakeup_thread(conf->mddev->thread); |
| 4727 | } else |
| 4728 | return_io(&s.return_bi); |
| 4729 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4730 | |
Dan Williams | 257a4b4 | 2011-11-08 16:22:06 +1100 | [diff] [blame] | 4731 | clear_bit_unlock(STRIPE_ACTIVE, &sh->state); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 4732 | } |
| 4733 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4734 | static void raid5_activate_delayed(struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4735 | { |
| 4736 | if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) { |
| 4737 | while (!list_empty(&conf->delayed_list)) { |
| 4738 | struct list_head *l = conf->delayed_list.next; |
| 4739 | struct stripe_head *sh; |
| 4740 | sh = list_entry(l, struct stripe_head, lru); |
| 4741 | list_del_init(l); |
| 4742 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 4743 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 4744 | atomic_inc(&conf->preread_active_stripes); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4745 | list_add_tail(&sh->lru, &conf->hold_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4746 | raid5_wakeup_stripe_thread(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4747 | } |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 4748 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4749 | } |
| 4750 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4751 | static void activate_bit_delay(struct r5conf *conf, |
| 4752 | struct list_head *temp_inactive_list) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4753 | { |
| 4754 | /* device_lock is held */ |
| 4755 | struct list_head head; |
| 4756 | list_add(&head, &conf->bitmap_list); |
| 4757 | list_del_init(&conf->bitmap_list); |
| 4758 | while (!list_empty(&head)) { |
| 4759 | struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4760 | int hash; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4761 | list_del_init(&sh->lru); |
| 4762 | atomic_inc(&sh->count); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4763 | hash = sh->hash_lock_index; |
| 4764 | __release_stripe(conf, sh, &temp_inactive_list[hash]); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 4765 | } |
| 4766 | } |
| 4767 | |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 4768 | static int raid5_congested(struct mddev *mddev, int bits) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4769 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4770 | struct r5conf *conf = mddev->private; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4771 | |
| 4772 | /* No difference between reads and writes. Just check |
| 4773 | * how busy the stripe_cache is |
| 4774 | */ |
NeilBrown | 3fa841d | 2009-09-23 18:10:29 +1000 | [diff] [blame] | 4775 | |
NeilBrown | 5423399 | 2015-02-26 12:21:04 +1100 | [diff] [blame] | 4776 | if (test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4777 | return 1; |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 4778 | |
| 4779 | /* Also checks whether there is pressure on r5cache log space */ |
| 4780 | if (test_bit(R5C_LOG_TIGHT, &conf->cache_state)) |
| 4781 | return 1; |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4782 | if (conf->quiesce) |
| 4783 | return 1; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 4784 | if (atomic_read(&conf->empty_inactive_list_nr)) |
NeilBrown | f022b2f | 2006-10-03 01:15:56 -0700 | [diff] [blame] | 4785 | return 1; |
| 4786 | |
| 4787 | return 0; |
| 4788 | } |
| 4789 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4790 | 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] | 4791 | { |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 4792 | struct r5conf *conf = mddev->private; |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4793 | 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] | 4794 | unsigned int chunk_sectors; |
Kent Overstreet | aa8b57a | 2013-02-05 15:19:29 -0800 | [diff] [blame] | 4795 | unsigned int bio_sectors = bio_sectors(bio); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4796 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 4797 | chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4798 | return chunk_sectors >= |
| 4799 | ((sector & (chunk_sectors - 1)) + bio_sectors); |
| 4800 | } |
| 4801 | |
| 4802 | /* |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4803 | * add bio to the retry LIFO ( in O(1) ... we are in interrupt ) |
| 4804 | * later sampled by raid5d. |
| 4805 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4806 | 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] | 4807 | { |
| 4808 | unsigned long flags; |
| 4809 | |
| 4810 | spin_lock_irqsave(&conf->device_lock, flags); |
| 4811 | |
| 4812 | bi->bi_next = conf->retry_read_aligned_list; |
| 4813 | conf->retry_read_aligned_list = bi; |
| 4814 | |
| 4815 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 4816 | md_wakeup_thread(conf->mddev->thread); |
| 4817 | } |
| 4818 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4819 | static struct bio *remove_bio_from_retry(struct r5conf *conf) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4820 | { |
| 4821 | struct bio *bi; |
| 4822 | |
| 4823 | bi = conf->retry_read_aligned; |
| 4824 | if (bi) { |
| 4825 | conf->retry_read_aligned = NULL; |
| 4826 | return bi; |
| 4827 | } |
| 4828 | bi = conf->retry_read_aligned_list; |
| 4829 | if(bi) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4830 | conf->retry_read_aligned_list = bi->bi_next; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4831 | bi->bi_next = NULL; |
Jens Axboe | 960e739 | 2008-08-15 10:41:18 +0200 | [diff] [blame] | 4832 | /* |
| 4833 | * this sets the active strip count to 1 and the processed |
| 4834 | * strip count to zero (upper 8 bits) |
| 4835 | */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 4836 | 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] | 4837 | } |
| 4838 | |
| 4839 | return bi; |
| 4840 | } |
| 4841 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4842 | /* |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4843 | * The "raid5_align_endio" should check if the read succeeded and if it |
| 4844 | * did, call bio_endio on the original bio (having bio_put the new bio |
| 4845 | * first). |
| 4846 | * If the read failed.. |
| 4847 | */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 4848 | static void raid5_align_endio(struct bio *bi) |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4849 | { |
| 4850 | struct bio* raid_bi = bi->bi_private; |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 4851 | struct mddev *mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4852 | struct r5conf *conf; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4853 | struct md_rdev *rdev; |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 4854 | int error = bi->bi_error; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4855 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4856 | bio_put(bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4857 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4858 | rdev = (void*)raid_bi->bi_next; |
| 4859 | raid_bi->bi_next = NULL; |
NeilBrown | 2b7f222 | 2010-03-25 16:06:03 +1100 | [diff] [blame] | 4860 | mddev = rdev->mddev; |
| 4861 | conf = mddev->private; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4862 | |
| 4863 | rdev_dec_pending(rdev, conf->mddev); |
| 4864 | |
Sasha Levin | 9b81c84 | 2015-08-10 19:05:18 -0400 | [diff] [blame] | 4865 | if (!error) { |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 4866 | trace_block_bio_complete(bdev_get_queue(raid_bi->bi_bdev), |
| 4867 | raid_bi, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 4868 | bio_endio(raid_bi); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4869 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 4870 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 4871 | return; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4872 | } |
| 4873 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 4874 | 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] | 4875 | |
| 4876 | add_bio_to_retry(raid_bi, conf); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4877 | } |
| 4878 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 4879 | 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] | 4880 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 4881 | struct r5conf *conf = mddev->private; |
NeilBrown | 8553fe7ec | 2009-12-14 12:49:47 +1100 | [diff] [blame] | 4882 | int dd_idx; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4883 | struct bio* align_bi; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 4884 | struct md_rdev *rdev; |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4885 | sector_t end_sector; |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4886 | |
| 4887 | if (!in_chunk_boundary(mddev, raid_bio)) { |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 4888 | pr_debug("%s: non aligned\n", __func__); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4889 | return 0; |
| 4890 | } |
| 4891 | /* |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4892 | * use bio_clone_mddev to make a copy of the bio |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4893 | */ |
NeilBrown | a167f66 | 2010-10-26 18:31:13 +1100 | [diff] [blame] | 4894 | align_bi = bio_clone_mddev(raid_bio, GFP_NOIO, mddev); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4895 | if (!align_bi) |
| 4896 | return 0; |
| 4897 | /* |
| 4898 | * set bi_end_io to a new function, and set bi_private to the |
| 4899 | * original bio. |
| 4900 | */ |
| 4901 | align_bi->bi_end_io = raid5_align_endio; |
| 4902 | align_bi->bi_private = raid_bio; |
| 4903 | /* |
| 4904 | * compute position |
| 4905 | */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4906 | align_bi->bi_iter.bi_sector = |
| 4907 | raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector, |
| 4908 | 0, &dd_idx, NULL); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4909 | |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 4910 | end_sector = bio_end_sector(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4911 | rcu_read_lock(); |
NeilBrown | 671488c | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 4912 | rdev = rcu_dereference(conf->disks[dd_idx].replacement); |
| 4913 | if (!rdev || test_bit(Faulty, &rdev->flags) || |
| 4914 | rdev->recovery_offset < end_sector) { |
| 4915 | rdev = rcu_dereference(conf->disks[dd_idx].rdev); |
| 4916 | if (rdev && |
| 4917 | (test_bit(Faulty, &rdev->flags) || |
| 4918 | !(test_bit(In_sync, &rdev->flags) || |
| 4919 | rdev->recovery_offset >= end_sector))) |
| 4920 | rdev = NULL; |
| 4921 | } |
| 4922 | if (rdev) { |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4923 | sector_t first_bad; |
| 4924 | int bad_sectors; |
| 4925 | |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4926 | atomic_inc(&rdev->nr_pending); |
| 4927 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4928 | raid_bio->bi_next = (void*)rdev; |
| 4929 | align_bi->bi_bdev = rdev->bdev; |
Jens Axboe | b7c44ed | 2015-07-24 12:37:59 -0600 | [diff] [blame] | 4930 | bio_clear_flag(align_bi, BIO_SEG_VALID); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4931 | |
Kent Overstreet | 7140aaf | 2013-09-25 13:37:01 -0700 | [diff] [blame] | 4932 | if (is_badblock(rdev, align_bi->bi_iter.bi_sector, |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4933 | bio_sectors(align_bi), |
NeilBrown | 31c176e | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 4934 | &first_bad, &bad_sectors)) { |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 4935 | bio_put(align_bi); |
| 4936 | rdev_dec_pending(rdev, mddev); |
| 4937 | return 0; |
| 4938 | } |
| 4939 | |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 4940 | /* No reshape active, so we can trust rdev->data_offset */ |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4941 | align_bi->bi_iter.bi_sector += rdev->data_offset; |
majianpeng | 6c0544e | 2012-06-12 08:31:10 +0800 | [diff] [blame] | 4942 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4943 | spin_lock_irq(&conf->device_lock); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 4944 | wait_event_lock_irq(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4945 | conf->quiesce == 0, |
Lukas Czerner | eed8c02 | 2012-11-30 11:42:40 +0100 | [diff] [blame] | 4946 | conf->device_lock); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4947 | atomic_inc(&conf->active_aligned_reads); |
| 4948 | spin_unlock_irq(&conf->device_lock); |
| 4949 | |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 4950 | if (mddev->gendisk) |
| 4951 | trace_block_bio_remap(bdev_get_queue(align_bi->bi_bdev), |
| 4952 | align_bi, disk_devt(mddev->gendisk), |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 4953 | raid_bio->bi_iter.bi_sector); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4954 | generic_make_request(align_bi); |
| 4955 | return 1; |
| 4956 | } else { |
| 4957 | rcu_read_unlock(); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 4958 | bio_put(align_bi); |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 4959 | return 0; |
| 4960 | } |
| 4961 | } |
| 4962 | |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 4963 | static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio) |
| 4964 | { |
| 4965 | struct bio *split; |
| 4966 | |
| 4967 | do { |
| 4968 | sector_t sector = raid_bio->bi_iter.bi_sector; |
| 4969 | unsigned chunk_sects = mddev->chunk_sectors; |
| 4970 | unsigned sectors = chunk_sects - (sector & (chunk_sects-1)); |
| 4971 | |
| 4972 | if (sectors < bio_sectors(raid_bio)) { |
| 4973 | split = bio_split(raid_bio, sectors, GFP_NOIO, fs_bio_set); |
| 4974 | bio_chain(split, raid_bio); |
| 4975 | } else |
| 4976 | split = raid_bio; |
| 4977 | |
| 4978 | if (!raid5_read_one_chunk(mddev, split)) { |
| 4979 | if (split != raid_bio) |
| 4980 | generic_make_request(raid_bio); |
| 4981 | return split; |
| 4982 | } |
| 4983 | } while (split != raid_bio); |
| 4984 | |
| 4985 | return NULL; |
| 4986 | } |
| 4987 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4988 | /* __get_priority_stripe - get the next stripe to process |
| 4989 | * |
| 4990 | * Full stripe writes are allowed to pass preread active stripes up until |
| 4991 | * the bypass_threshold is exceeded. In general the bypass_count |
| 4992 | * increments when the handle_list is handled before the hold_list; however, it |
| 4993 | * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a |
| 4994 | * stripe with in flight i/o. The bypass_count will be reset when the |
| 4995 | * head of the hold_list has changed, i.e. the head was promoted to the |
| 4996 | * handle_list. |
| 4997 | */ |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 4998 | static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 4999 | { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5000 | struct stripe_head *sh = NULL, *tmp; |
| 5001 | struct list_head *handle_list = NULL; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5002 | struct r5worker_group *wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5003 | |
| 5004 | if (conf->worker_cnt_per_group == 0) { |
| 5005 | handle_list = &conf->handle_list; |
| 5006 | } else if (group != ANY_GROUP) { |
| 5007 | handle_list = &conf->worker_groups[group].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5008 | wg = &conf->worker_groups[group]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5009 | } else { |
| 5010 | int i; |
| 5011 | for (i = 0; i < conf->group_cnt; i++) { |
| 5012 | handle_list = &conf->worker_groups[i].handle_list; |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5013 | wg = &conf->worker_groups[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5014 | if (!list_empty(handle_list)) |
| 5015 | break; |
| 5016 | } |
| 5017 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5018 | |
| 5019 | pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n", |
| 5020 | __func__, |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5021 | list_empty(handle_list) ? "empty" : "busy", |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5022 | list_empty(&conf->hold_list) ? "empty" : "busy", |
| 5023 | atomic_read(&conf->pending_full_writes), conf->bypass_count); |
| 5024 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5025 | if (!list_empty(handle_list)) { |
| 5026 | sh = list_entry(handle_list->next, typeof(*sh), lru); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5027 | |
| 5028 | if (list_empty(&conf->hold_list)) |
| 5029 | conf->bypass_count = 0; |
| 5030 | else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) { |
| 5031 | if (conf->hold_list.next == conf->last_hold) |
| 5032 | conf->bypass_count++; |
| 5033 | else { |
| 5034 | conf->last_hold = conf->hold_list.next; |
| 5035 | conf->bypass_count -= conf->bypass_threshold; |
| 5036 | if (conf->bypass_count < 0) |
| 5037 | conf->bypass_count = 0; |
| 5038 | } |
| 5039 | } |
| 5040 | } else if (!list_empty(&conf->hold_list) && |
| 5041 | ((conf->bypass_threshold && |
| 5042 | conf->bypass_count > conf->bypass_threshold) || |
| 5043 | atomic_read(&conf->pending_full_writes) == 0)) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5044 | |
| 5045 | list_for_each_entry(tmp, &conf->hold_list, lru) { |
| 5046 | if (conf->worker_cnt_per_group == 0 || |
| 5047 | group == ANY_GROUP || |
| 5048 | !cpu_online(tmp->cpu) || |
| 5049 | cpu_to_group(tmp->cpu) == group) { |
| 5050 | sh = tmp; |
| 5051 | break; |
| 5052 | } |
| 5053 | } |
| 5054 | |
| 5055 | if (sh) { |
| 5056 | conf->bypass_count -= conf->bypass_threshold; |
| 5057 | if (conf->bypass_count < 0) |
| 5058 | conf->bypass_count = 0; |
| 5059 | } |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5060 | wg = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | if (!sh) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5064 | return NULL; |
| 5065 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5066 | if (wg) { |
| 5067 | wg->stripes_cnt--; |
| 5068 | sh->group = NULL; |
| 5069 | } |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5070 | list_del_init(&sh->lru); |
Shaohua Li | c7a6d35 | 2014-04-15 09:12:54 +0800 | [diff] [blame] | 5071 | BUG_ON(atomic_inc_return(&sh->count) != 1); |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 5072 | return sh; |
| 5073 | } |
Raz Ben-Jehuda(caro) | f679623 | 2006-12-10 02:20:46 -0800 | [diff] [blame] | 5074 | |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5075 | struct raid5_plug_cb { |
| 5076 | struct blk_plug_cb cb; |
| 5077 | struct list_head list; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5078 | struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS]; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5079 | }; |
| 5080 | |
| 5081 | static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule) |
| 5082 | { |
| 5083 | struct raid5_plug_cb *cb = container_of( |
| 5084 | blk_cb, struct raid5_plug_cb, cb); |
| 5085 | struct stripe_head *sh; |
| 5086 | struct mddev *mddev = cb->cb.data; |
| 5087 | struct r5conf *conf = mddev->private; |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5088 | int cnt = 0; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5089 | int hash; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5090 | |
| 5091 | if (cb->list.next && !list_empty(&cb->list)) { |
| 5092 | spin_lock_irq(&conf->device_lock); |
| 5093 | while (!list_empty(&cb->list)) { |
| 5094 | sh = list_first_entry(&cb->list, struct stripe_head, lru); |
| 5095 | list_del_init(&sh->lru); |
| 5096 | /* |
| 5097 | * avoid race release_stripe_plug() sees |
| 5098 | * STRIPE_ON_UNPLUG_LIST clear but the stripe |
| 5099 | * is still in our list |
| 5100 | */ |
Peter Zijlstra | 4e857c5 | 2014-03-17 18:06:10 +0100 | [diff] [blame] | 5101 | smp_mb__before_atomic(); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5102 | clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5103 | /* |
| 5104 | * STRIPE_ON_RELEASE_LIST could be set here. In that |
| 5105 | * case, the count is always > 1 here |
| 5106 | */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5107 | hash = sh->hash_lock_index; |
| 5108 | __release_stripe(conf, sh, &cb->temp_inactive_list[hash]); |
NeilBrown | a9add5d | 2012-10-31 11:59:09 +1100 | [diff] [blame] | 5109 | cnt++; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5110 | } |
| 5111 | spin_unlock_irq(&conf->device_lock); |
| 5112 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5113 | release_inactive_stripe_list(conf, cb->temp_inactive_list, |
| 5114 | NR_STRIPE_HASH_LOCKS); |
Jonathan Brassow | e3620a3 | 2013-03-07 16:22:01 -0600 | [diff] [blame] | 5115 | if (mddev->queue) |
| 5116 | trace_block_unplug(mddev->queue, cnt, !from_schedule); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5117 | kfree(cb); |
| 5118 | } |
| 5119 | |
| 5120 | static void release_stripe_plug(struct mddev *mddev, |
| 5121 | struct stripe_head *sh) |
| 5122 | { |
| 5123 | struct blk_plug_cb *blk_cb = blk_check_plugged( |
| 5124 | raid5_unplug, mddev, |
| 5125 | sizeof(struct raid5_plug_cb)); |
| 5126 | struct raid5_plug_cb *cb; |
| 5127 | |
| 5128 | if (!blk_cb) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5129 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5130 | return; |
| 5131 | } |
| 5132 | |
| 5133 | cb = container_of(blk_cb, struct raid5_plug_cb, cb); |
| 5134 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5135 | if (cb->list.next == NULL) { |
| 5136 | int i; |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5137 | INIT_LIST_HEAD(&cb->list); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5138 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5139 | INIT_LIST_HEAD(cb->temp_inactive_list + i); |
| 5140 | } |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5141 | |
| 5142 | if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state)) |
| 5143 | list_add_tail(&sh->lru, &cb->list); |
| 5144 | else |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5145 | raid5_release_stripe(sh); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5146 | } |
| 5147 | |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5148 | static void make_discard_request(struct mddev *mddev, struct bio *bi) |
| 5149 | { |
| 5150 | struct r5conf *conf = mddev->private; |
| 5151 | sector_t logical_sector, last_sector; |
| 5152 | struct stripe_head *sh; |
| 5153 | int remaining; |
| 5154 | int stripe_sectors; |
| 5155 | |
| 5156 | if (mddev->reshape_position != MaxSector) |
| 5157 | /* Skip discard while reshape is happening */ |
| 5158 | return; |
| 5159 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5160 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
| 5161 | 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] | 5162 | |
| 5163 | bi->bi_next = NULL; |
| 5164 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
| 5165 | |
| 5166 | stripe_sectors = conf->chunk_sectors * |
| 5167 | (conf->raid_disks - conf->max_degraded); |
| 5168 | logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector, |
| 5169 | stripe_sectors); |
| 5170 | sector_div(last_sector, stripe_sectors); |
| 5171 | |
| 5172 | logical_sector *= conf->chunk_sectors; |
| 5173 | last_sector *= conf->chunk_sectors; |
| 5174 | |
| 5175 | for (; logical_sector < last_sector; |
| 5176 | logical_sector += STRIPE_SECTORS) { |
| 5177 | DEFINE_WAIT(w); |
| 5178 | int d; |
| 5179 | again: |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5180 | sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5181 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5182 | TASK_UNINTERRUPTIBLE); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5183 | set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
| 5184 | if (test_bit(STRIPE_SYNCING, &sh->state)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5185 | raid5_release_stripe(sh); |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5186 | schedule(); |
| 5187 | goto again; |
| 5188 | } |
| 5189 | clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5190 | spin_lock_irq(&sh->stripe_lock); |
| 5191 | for (d = 0; d < conf->raid_disks; d++) { |
| 5192 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5193 | continue; |
| 5194 | if (sh->dev[d].towrite || sh->dev[d].toread) { |
| 5195 | set_bit(R5_Overlap, &sh->dev[d].flags); |
| 5196 | spin_unlock_irq(&sh->stripe_lock); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5197 | raid5_release_stripe(sh); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5198 | schedule(); |
| 5199 | goto again; |
| 5200 | } |
| 5201 | } |
NeilBrown | f8dfcff | 2013-03-12 12:18:06 +1100 | [diff] [blame] | 5202 | set_bit(STRIPE_DISCARD, &sh->state); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5203 | finish_wait(&conf->wait_for_overlap, &w); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5204 | sh->overwrite_disks = 0; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5205 | for (d = 0; d < conf->raid_disks; d++) { |
| 5206 | if (d == sh->pd_idx || d == sh->qd_idx) |
| 5207 | continue; |
| 5208 | sh->dev[d].towrite = bi; |
| 5209 | set_bit(R5_OVERWRITE, &sh->dev[d].flags); |
| 5210 | raid5_inc_bi_active_stripes(bi); |
shli@kernel.org | 7a87f43 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5211 | sh->overwrite_disks++; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5212 | } |
| 5213 | spin_unlock_irq(&sh->stripe_lock); |
| 5214 | if (conf->mddev->bitmap) { |
| 5215 | for (d = 0; |
| 5216 | d < conf->raid_disks - conf->max_degraded; |
| 5217 | d++) |
| 5218 | bitmap_startwrite(mddev->bitmap, |
| 5219 | sh->sector, |
| 5220 | STRIPE_SECTORS, |
| 5221 | 0); |
| 5222 | sh->bm_seq = conf->seq_flush + 1; |
| 5223 | set_bit(STRIPE_BIT_DELAY, &sh->state); |
| 5224 | } |
| 5225 | |
| 5226 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5227 | clear_bit(STRIPE_DELAYED, &sh->state); |
| 5228 | if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5229 | atomic_inc(&conf->preread_active_stripes); |
| 5230 | release_stripe_plug(mddev, sh); |
| 5231 | } |
| 5232 | |
| 5233 | remaining = raid5_dec_bi_active_stripes(bi); |
| 5234 | if (remaining == 0) { |
| 5235 | md_write_end(mddev); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5236 | bio_endio(bi); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5237 | } |
| 5238 | } |
| 5239 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5240 | static void raid5_make_request(struct mddev *mddev, struct bio * bi) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5241 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5242 | struct r5conf *conf = mddev->private; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5243 | int dd_idx; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5244 | sector_t new_sector; |
| 5245 | sector_t logical_sector, last_sector; |
| 5246 | struct stripe_head *sh; |
Jens Axboe | a362357 | 2005-11-01 09:26:16 +0100 | [diff] [blame] | 5247 | const int rw = bio_data_dir(bi); |
NeilBrown | 4907732 | 2010-03-25 16:20:56 +1100 | [diff] [blame] | 5248 | int remaining; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5249 | DEFINE_WAIT(w); |
| 5250 | bool do_prepare; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5251 | |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5252 | if (unlikely(bi->bi_opf & REQ_PREFLUSH)) { |
Shaohua Li | 828cbe9 | 2015-09-02 13:49:49 -0700 | [diff] [blame] | 5253 | int ret = r5l_handle_flush_request(conf->log, bi); |
| 5254 | |
| 5255 | if (ret == 0) |
| 5256 | return; |
| 5257 | if (ret == -ENODEV) { |
| 5258 | md_flush_request(mddev, bi); |
| 5259 | return; |
| 5260 | } |
| 5261 | /* ret == -EAGAIN, fallback */ |
NeilBrown | e5dcdd8 | 2005-09-09 16:23:41 -0700 | [diff] [blame] | 5262 | } |
| 5263 | |
NeilBrown | 3d310eb | 2005-06-21 17:17:26 -0700 | [diff] [blame] | 5264 | md_write_start(mddev, bi); |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5265 | |
Eric Mei | 9ffc8f7 | 2015-03-18 23:39:11 -0600 | [diff] [blame] | 5266 | /* |
| 5267 | * If array is degraded, better not do chunk aligned read because |
| 5268 | * later we might have to read it again in order to reconstruct |
| 5269 | * data on failed drives. |
| 5270 | */ |
| 5271 | if (rw == READ && mddev->degraded == 0 && |
Song Liu | 2ded370 | 2016-11-17 15:24:38 -0800 | [diff] [blame] | 5272 | !r5c_is_writeback(conf->log) && |
Ming Lin | 7ef6b12 | 2015-05-06 22:51:24 -0700 | [diff] [blame] | 5273 | mddev->reshape_position == MaxSector) { |
| 5274 | bi = chunk_aligned_read(mddev, bi); |
| 5275 | if (!bi) |
| 5276 | return; |
| 5277 | } |
Raz Ben-Jehuda(caro) | 5248861 | 2006-12-10 02:20:48 -0800 | [diff] [blame] | 5278 | |
Mike Christie | 796a5cf | 2016-06-05 14:32:07 -0500 | [diff] [blame] | 5279 | if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) { |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 5280 | make_discard_request(mddev, bi); |
| 5281 | return; |
| 5282 | } |
| 5283 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5284 | logical_sector = bi->bi_iter.bi_sector & ~((sector_t)STRIPE_SECTORS-1); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5285 | last_sector = bio_end_sector(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5286 | bi->bi_next = NULL; |
| 5287 | bi->bi_phys_segments = 1; /* over-loaded to count active stripes */ |
NeilBrown | 06d91a5 | 2005-06-21 17:17:12 -0700 | [diff] [blame] | 5288 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5289 | prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5290 | for (;logical_sector < last_sector; logical_sector += STRIPE_SECTORS) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5291 | int previous; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5292 | int seq; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5293 | |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5294 | do_prepare = false; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5295 | retry: |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5296 | seq = read_seqcount_begin(&conf->gen_lock); |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5297 | previous = 0; |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5298 | if (do_prepare) |
| 5299 | prepare_to_wait(&conf->wait_for_overlap, &w, |
| 5300 | TASK_UNINTERRUPTIBLE); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5301 | if (unlikely(conf->reshape_progress != MaxSector)) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5302 | /* spinlock is needed as reshape_progress may be |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5303 | * 64bit on a 32bit platform, and so it might be |
| 5304 | * possible to see a half-updated value |
Jesper Juhl | aeb878b | 2011-04-10 18:06:17 +0200 | [diff] [blame] | 5305 | * Of course reshape_progress could change after |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5306 | * the lock is dropped, so once we get a reference |
| 5307 | * to the stripe that we think it is, we will have |
| 5308 | * to check again. |
| 5309 | */ |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5310 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5311 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5312 | ? logical_sector < conf->reshape_progress |
| 5313 | : logical_sector >= conf->reshape_progress) { |
NeilBrown | b5663ba | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5314 | previous = 1; |
| 5315 | } else { |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5316 | if (mddev->reshape_backwards |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5317 | ? logical_sector < conf->reshape_safe |
| 5318 | : logical_sector >= conf->reshape_safe) { |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5319 | spin_unlock_irq(&conf->device_lock); |
| 5320 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5321 | do_prepare = true; |
NeilBrown | b578d55 | 2006-03-27 01:18:12 -0800 | [diff] [blame] | 5322 | goto retry; |
| 5323 | } |
| 5324 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5325 | spin_unlock_irq(&conf->device_lock); |
| 5326 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5327 | |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5328 | new_sector = raid5_compute_sector(conf, logical_sector, |
| 5329 | previous, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5330 | &dd_idx, NULL); |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5331 | pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n", |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5332 | (unsigned long long)new_sector, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5333 | (unsigned long long)logical_sector); |
| 5334 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5335 | sh = raid5_get_active_stripe(conf, new_sector, previous, |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5336 | (bi->bi_opf & REQ_RAHEAD), 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5337 | if (sh) { |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5338 | if (unlikely(previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5339 | /* expansion might have moved on while waiting for a |
NeilBrown | df8e7f7 | 2006-03-27 01:18:15 -0800 | [diff] [blame] | 5340 | * stripe, so we must do the range check again. |
| 5341 | * Expansion could still move past after this |
| 5342 | * test, but as we are holding a reference to |
| 5343 | * 'sh', we know that if that happens, |
| 5344 | * STRIPE_EXPANDING will get set and the expansion |
| 5345 | * won't proceed until we finish with the stripe. |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5346 | */ |
| 5347 | int must_retry = 0; |
| 5348 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5349 | if (mddev->reshape_backwards |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 5350 | ? logical_sector >= conf->reshape_progress |
| 5351 | : logical_sector < conf->reshape_progress) |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5352 | /* mismatch, need to try again */ |
| 5353 | must_retry = 1; |
| 5354 | spin_unlock_irq(&conf->device_lock); |
| 5355 | if (must_retry) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5356 | raid5_release_stripe(sh); |
Dan Williams | 7a3ab90 | 2009-06-16 16:00:33 -0700 | [diff] [blame] | 5357 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5358 | do_prepare = true; |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5359 | goto retry; |
| 5360 | } |
| 5361 | } |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5362 | if (read_seqcount_retry(&conf->gen_lock, seq)) { |
| 5363 | /* Might have got the wrong stripe_head |
| 5364 | * by accident |
| 5365 | */ |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5366 | raid5_release_stripe(sh); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 5367 | goto retry; |
| 5368 | } |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5369 | |
Namhyung Kim | ffd96e3 | 2011-07-18 17:38:51 +1000 | [diff] [blame] | 5370 | if (rw == WRITE && |
NeilBrown | a5c308d | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5371 | logical_sector >= mddev->suspend_lo && |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5372 | logical_sector < mddev->suspend_hi) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5373 | raid5_release_stripe(sh); |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5374 | /* As the suspend_* range is controlled by |
| 5375 | * userspace, we want an interruptible |
| 5376 | * wait. |
| 5377 | */ |
| 5378 | flush_signals(current); |
| 5379 | prepare_to_wait(&conf->wait_for_overlap, |
| 5380 | &w, TASK_INTERRUPTIBLE); |
| 5381 | if (logical_sector >= mddev->suspend_lo && |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5382 | logical_sector < mddev->suspend_hi) { |
NeilBrown | e62e58a | 2009-07-01 13:15:35 +1000 | [diff] [blame] | 5383 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5384 | do_prepare = true; |
| 5385 | } |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 5386 | goto retry; |
| 5387 | } |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5388 | |
| 5389 | if (test_bit(STRIPE_EXPANDING, &sh->state) || |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5390 | !add_stripe_bio(sh, bi, dd_idx, rw, previous)) { |
NeilBrown | 7ecaa1e | 2006-03-27 01:18:08 -0800 | [diff] [blame] | 5391 | /* Stripe is busy expanding or |
| 5392 | * add failed due to overlap. Flush everything |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5393 | * and wait a while |
| 5394 | */ |
NeilBrown | 482c083 | 2011-04-18 18:25:42 +1000 | [diff] [blame] | 5395 | md_wakeup_thread(mddev->thread); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5396 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5397 | schedule(); |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5398 | do_prepare = true; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5399 | goto retry; |
| 5400 | } |
NeilBrown | 6ed3003 | 2008-02-06 01:40:00 -0800 | [diff] [blame] | 5401 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5402 | clear_bit(STRIPE_DELAYED, &sh->state); |
shli@kernel.org | 59fc630 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5403 | if ((!sh->batch_head || sh == sh->batch_head) && |
Jens Axboe | 1eff9d3 | 2016-08-05 15:35:16 -0600 | [diff] [blame] | 5404 | (bi->bi_opf & REQ_SYNC) && |
NeilBrown | 729a186 | 2009-12-14 12:49:50 +1100 | [diff] [blame] | 5405 | !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state)) |
| 5406 | atomic_inc(&conf->preread_active_stripes); |
Shaohua Li | 8811b59 | 2012-08-02 08:33:00 +1000 | [diff] [blame] | 5407 | release_stripe_plug(mddev, sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5408 | } else { |
| 5409 | /* cannot get stripe for read-ahead, just give-up */ |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5410 | bi->bi_error = -EIO; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5411 | break; |
| 5412 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5413 | } |
Shaohua Li | 27c0f68 | 2014-04-09 11:25:47 +0800 | [diff] [blame] | 5414 | finish_wait(&conf->wait_for_overlap, &w); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5415 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5416 | remaining = raid5_dec_bi_active_stripes(bi); |
NeilBrown | f634475 | 2006-03-27 01:18:17 -0800 | [diff] [blame] | 5417 | if (remaining == 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5418 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5419 | if ( rw == WRITE ) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5420 | md_write_end(mddev); |
NeilBrown | 6712ecf | 2007-09-27 12:47:43 +0200 | [diff] [blame] | 5421 | |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5422 | trace_block_bio_complete(bdev_get_queue(bi->bi_bdev), |
| 5423 | bi, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5424 | bio_endio(bi); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5425 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5426 | } |
| 5427 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5428 | 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] | 5429 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 5430 | 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] | 5431 | { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5432 | /* reshaping is quite different to recovery/resync so it is |
| 5433 | * handled quite separately ... here. |
| 5434 | * |
| 5435 | * On each call to sync_request, we gather one chunk worth of |
| 5436 | * destination stripes and flag them as expanding. |
| 5437 | * Then we find all the source stripes and request reads. |
| 5438 | * As the reads complete, handle_stripe will copy the data |
| 5439 | * into the destination stripe and release that stripe. |
| 5440 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5441 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5442 | struct stripe_head *sh; |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5443 | sector_t first_sector, last_sector; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5444 | int raid_disks = conf->previous_raid_disks; |
| 5445 | int data_disks = raid_disks - conf->max_degraded; |
| 5446 | int new_data_disks = conf->raid_disks - conf->max_degraded; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5447 | int i; |
| 5448 | int dd_idx; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5449 | sector_t writepos, readpos, safepos; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5450 | sector_t stripe_addr; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5451 | int reshape_sectors; |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5452 | struct list_head stripes; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5453 | sector_t retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5454 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5455 | if (sector_nr == 0) { |
| 5456 | /* If restarting in the middle, skip the initial sectors */ |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5457 | if (mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5458 | conf->reshape_progress < raid5_size(mddev, 0, 0)) { |
| 5459 | sector_nr = raid5_size(mddev, 0, 0) |
| 5460 | - conf->reshape_progress; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 5461 | } else if (mddev->reshape_backwards && |
| 5462 | conf->reshape_progress == MaxSector) { |
| 5463 | /* shouldn't happen, but just in case, finish up.*/ |
| 5464 | sector_nr = MaxSector; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5465 | } else if (!mddev->reshape_backwards && |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5466 | conf->reshape_progress > 0) |
| 5467 | sector_nr = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5468 | sector_div(sector_nr, new_data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5469 | if (sector_nr) { |
NeilBrown | 8dee721 | 2009-11-06 14:59:29 +1100 | [diff] [blame] | 5470 | mddev->curr_resync_completed = sector_nr; |
| 5471 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5472 | *skipped = 1; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5473 | retn = sector_nr; |
| 5474 | goto finish; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5475 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5476 | } |
| 5477 | |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5478 | /* We need to process a full chunk at a time. |
| 5479 | * If old and new chunk sizes differ, we need to process the |
| 5480 | * largest of these |
| 5481 | */ |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 5482 | |
| 5483 | reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5484 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5485 | /* We update the metadata at least every 10 seconds, or when |
| 5486 | * the data about to be copied would over-write the source of |
| 5487 | * the data at the front of the range. i.e. one new_stripe |
| 5488 | * along from reshape_progress new_maps to after where |
| 5489 | * reshape_safe old_maps to |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5490 | */ |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5491 | writepos = conf->reshape_progress; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5492 | sector_div(writepos, new_data_disks); |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5493 | readpos = conf->reshape_progress; |
| 5494 | sector_div(readpos, data_disks); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5495 | safepos = conf->reshape_safe; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5496 | sector_div(safepos, data_disks); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5497 | if (mddev->reshape_backwards) { |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 5498 | BUG_ON(writepos < reshape_sectors); |
| 5499 | writepos -= reshape_sectors; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5500 | readpos += reshape_sectors; |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5501 | safepos += reshape_sectors; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5502 | } else { |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5503 | writepos += reshape_sectors; |
NeilBrown | c74c0d7 | 2015-07-15 17:54:15 +1000 | [diff] [blame] | 5504 | /* readpos and safepos are worst-case calculations. |
| 5505 | * A negative number is overly pessimistic, and causes |
| 5506 | * obvious problems for unsigned storage. So clip to 0. |
| 5507 | */ |
NeilBrown | ed37d83 | 2009-05-27 21:39:05 +1000 | [diff] [blame] | 5508 | readpos -= min_t(sector_t, reshape_sectors, readpos); |
| 5509 | safepos -= min_t(sector_t, reshape_sectors, safepos); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5510 | } |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5511 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5512 | /* Having calculated the 'writepos' possibly use it |
| 5513 | * to set 'stripe_addr' which is where we will write to. |
| 5514 | */ |
| 5515 | if (mddev->reshape_backwards) { |
| 5516 | BUG_ON(conf->reshape_progress == 0); |
| 5517 | stripe_addr = writepos; |
| 5518 | BUG_ON((mddev->dev_sectors & |
| 5519 | ~((sector_t)reshape_sectors - 1)) |
| 5520 | - reshape_sectors - stripe_addr |
| 5521 | != sector_nr); |
| 5522 | } else { |
| 5523 | BUG_ON(writepos != sector_nr + reshape_sectors); |
| 5524 | stripe_addr = sector_nr; |
| 5525 | } |
| 5526 | |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5527 | /* 'writepos' is the most advanced device address we might write. |
| 5528 | * 'readpos' is the least advanced device address we might read. |
| 5529 | * 'safepos' is the least address recorded in the metadata as having |
| 5530 | * been reshaped. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5531 | * If there is a min_offset_diff, these are adjusted either by |
| 5532 | * increasing the safepos/readpos if diff is negative, or |
| 5533 | * increasing writepos if diff is positive. |
| 5534 | * If 'readpos' is then behind 'writepos', there is no way that we can |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5535 | * ensure safety in the face of a crash - that must be done by userspace |
| 5536 | * making a backup of the data. So in that case there is no particular |
| 5537 | * rush to update metadata. |
| 5538 | * Otherwise if 'safepos' is behind 'writepos', then we really need to |
| 5539 | * update the metadata to advance 'safepos' to match 'readpos' so that |
| 5540 | * we can be safe in the event of a crash. |
| 5541 | * So we insist on updating metadata if safepos is behind writepos and |
| 5542 | * readpos is beyond writepos. |
| 5543 | * In any case, update the metadata every 10 seconds. |
| 5544 | * Maybe that number should be configurable, but I'm not sure it is |
| 5545 | * worth it.... maybe it could be a multiple of safemode_delay??? |
| 5546 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 5547 | if (conf->min_offset_diff < 0) { |
| 5548 | safepos += -conf->min_offset_diff; |
| 5549 | readpos += -conf->min_offset_diff; |
| 5550 | } else |
| 5551 | writepos += conf->min_offset_diff; |
| 5552 | |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5553 | if ((mddev->reshape_backwards |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5554 | ? (safepos > writepos && readpos < writepos) |
| 5555 | : (safepos < writepos && readpos > writepos)) || |
| 5556 | time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5557 | /* Cannot proceed until we've updated the superblock... */ |
| 5558 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5559 | atomic_read(&conf->reshape_stripes)==0 |
| 5560 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5561 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 5562 | return 0; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5563 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5564 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5565 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5566 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5567 | md_wakeup_thread(mddev->thread); |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 5568 | wait_event(mddev->sb_wait, mddev->flags == 0 || |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5569 | test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5570 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5571 | return 0; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5572 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5573 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5574 | spin_unlock_irq(&conf->device_lock); |
| 5575 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5576 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5577 | } |
| 5578 | |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5579 | INIT_LIST_HEAD(&stripes); |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5580 | for (i = 0; i < reshape_sectors; i += STRIPE_SECTORS) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5581 | int j; |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5582 | int skipped_disk = 0; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5583 | sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5584 | set_bit(STRIPE_EXPANDING, &sh->state); |
| 5585 | atomic_inc(&conf->reshape_stripes); |
| 5586 | /* If any of this stripe is beyond the end of the old |
| 5587 | * array, then we need to zero those blocks |
| 5588 | */ |
| 5589 | for (j=sh->disks; j--;) { |
| 5590 | sector_t s; |
| 5591 | if (j == sh->pd_idx) |
| 5592 | continue; |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5593 | if (conf->level == 6 && |
NeilBrown | d0dabf7 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5594 | j == sh->qd_idx) |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 5595 | continue; |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5596 | s = raid5_compute_blocknr(sh, j, 0); |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 5597 | if (s < raid5_size(mddev, 0, 0)) { |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5598 | skipped_disk = 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5599 | continue; |
| 5600 | } |
| 5601 | memset(page_address(sh->dev[j].page), 0, STRIPE_SIZE); |
| 5602 | set_bit(R5_Expanded, &sh->dev[j].flags); |
| 5603 | set_bit(R5_UPTODATE, &sh->dev[j].flags); |
| 5604 | } |
NeilBrown | a9f326e | 2009-09-23 18:06:41 +1000 | [diff] [blame] | 5605 | if (!skipped_disk) { |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5606 | set_bit(STRIPE_EXPAND_READY, &sh->state); |
| 5607 | set_bit(STRIPE_HANDLE, &sh->state); |
| 5608 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5609 | list_add(&sh->lru, &stripes); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5610 | } |
| 5611 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 5612 | if (mddev->reshape_backwards) |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5613 | conf->reshape_progress -= reshape_sectors * new_data_disks; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5614 | else |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5615 | conf->reshape_progress += reshape_sectors * new_data_disks; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5616 | spin_unlock_irq(&conf->device_lock); |
| 5617 | /* Ok, those stripe are ready. We can start scheduling |
| 5618 | * reads on the source stripes. |
| 5619 | * The source stripes are determined by mapping the first and last |
| 5620 | * block on the destination stripes. |
| 5621 | */ |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5622 | first_sector = |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 5623 | raid5_compute_sector(conf, stripe_addr*(new_data_disks), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5624 | 1, &dd_idx, NULL); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5625 | last_sector = |
NeilBrown | 0e6e027 | 2009-06-09 16:32:22 +1000 | [diff] [blame] | 5626 | raid5_compute_sector(conf, ((stripe_addr+reshape_sectors) |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 5627 | * new_data_disks - 1), |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5628 | 1, &dd_idx, NULL); |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5629 | if (last_sector >= mddev->dev_sectors) |
| 5630 | last_sector = mddev->dev_sectors - 1; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5631 | while (first_sector <= last_sector) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5632 | sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5633 | set_bit(STRIPE_EXPAND_SOURCE, &sh->state); |
| 5634 | set_bit(STRIPE_HANDLE, &sh->state); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5635 | raid5_release_stripe(sh); |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5636 | first_sector += STRIPE_SECTORS; |
| 5637 | } |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5638 | /* Now that the sources are clearly marked, we can release |
| 5639 | * the destination stripes |
| 5640 | */ |
| 5641 | while (!list_empty(&stripes)) { |
| 5642 | sh = list_entry(stripes.next, struct stripe_head, lru); |
| 5643 | list_del_init(&sh->lru); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5644 | raid5_release_stripe(sh); |
NeilBrown | ab69ae1 | 2009-03-31 15:26:47 +1100 | [diff] [blame] | 5645 | } |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5646 | /* If this takes us to the resync_max point where we have to pause, |
| 5647 | * then we need to write out the superblock. |
| 5648 | */ |
NeilBrown | 7a66138 | 2009-03-31 15:21:40 +1100 | [diff] [blame] | 5649 | sector_nr += reshape_sectors; |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5650 | retn = reshape_sectors; |
| 5651 | finish: |
NeilBrown | c5e19d9 | 2015-07-17 12:06:02 +1000 | [diff] [blame] | 5652 | if (mddev->curr_resync_completed > mddev->resync_max || |
| 5653 | (sector_nr - mddev->curr_resync_completed) * 2 |
NeilBrown | c03f6a1 | 2009-04-17 11:06:30 +1000 | [diff] [blame] | 5654 | >= mddev->resync_max - mddev->curr_resync_completed) { |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5655 | /* Cannot proceed until we've updated the superblock... */ |
| 5656 | wait_event(conf->wait_for_overlap, |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5657 | atomic_read(&conf->reshape_stripes) == 0 |
| 5658 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5659 | if (atomic_read(&conf->reshape_stripes) != 0) |
| 5660 | goto ret; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5661 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 75d3da4 | 2011-01-14 09:14:34 +1100 | [diff] [blame] | 5662 | mddev->curr_resync_completed = sector_nr; |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 5663 | conf->reshape_checkpoint = jiffies; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5664 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 5665 | md_wakeup_thread(mddev->thread); |
| 5666 | wait_event(mddev->sb_wait, |
| 5667 | !test_bit(MD_CHANGE_DEVS, &mddev->flags) |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5668 | || test_bit(MD_RECOVERY_INTR, &mddev->recovery)); |
| 5669 | if (test_bit(MD_RECOVERY_INTR, &mddev->recovery)) |
| 5670 | goto ret; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5671 | spin_lock_irq(&conf->device_lock); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 5672 | conf->reshape_safe = mddev->reshape_position; |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5673 | spin_unlock_irq(&conf->device_lock); |
| 5674 | wake_up(&conf->wait_for_overlap); |
NeilBrown | acb180b | 2009-04-14 16:28:34 +1000 | [diff] [blame] | 5675 | sysfs_notify(&mddev->kobj, NULL, "sync_completed"); |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5676 | } |
NeilBrown | c91abf5 | 2013-11-19 12:02:01 +1100 | [diff] [blame] | 5677 | ret: |
NeilBrown | 9214048 | 2015-07-06 12:28:45 +1000 | [diff] [blame] | 5678 | return retn; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5679 | } |
| 5680 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 5681 | static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr, |
| 5682 | int *skipped) |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5683 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5684 | struct r5conf *conf = mddev->private; |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5685 | struct stripe_head *sh; |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5686 | sector_t max_sector = mddev->dev_sectors; |
NeilBrown | 57dab0b | 2010-10-19 10:03:39 +1100 | [diff] [blame] | 5687 | sector_t sync_blocks; |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5688 | int still_degraded = 0; |
| 5689 | int i; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5690 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5691 | if (sector_nr >= max_sector) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5692 | /* just being told to finish up .. nothing much to do */ |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 5693 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 5694 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) { |
| 5695 | end_reshape(conf); |
| 5696 | return 0; |
| 5697 | } |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5698 | |
| 5699 | if (mddev->curr_resync < max_sector) /* aborted */ |
| 5700 | bitmap_end_sync(mddev->bitmap, mddev->curr_resync, |
| 5701 | &sync_blocks, 1); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5702 | else /* completed sync */ |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5703 | conf->fullsync = 0; |
| 5704 | bitmap_close_sync(mddev->bitmap); |
| 5705 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5706 | return 0; |
| 5707 | } |
NeilBrown | ccfcc3c | 2006-03-27 01:18:09 -0800 | [diff] [blame] | 5708 | |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 5709 | /* Allow raid5_quiesce to complete */ |
| 5710 | wait_event(conf->wait_for_overlap, conf->quiesce != 2); |
| 5711 | |
NeilBrown | 52c0329 | 2006-06-26 00:27:43 -0700 | [diff] [blame] | 5712 | if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) |
| 5713 | return reshape_request(mddev, sector_nr, skipped); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 5714 | |
NeilBrown | c620727 | 2008-02-06 01:39:52 -0800 | [diff] [blame] | 5715 | /* No need to check resync_max as we never do more than one |
| 5716 | * stripe, and as resync_max will always be on a chunk boundary, |
| 5717 | * if the check in md_do_sync didn't fire, there is no chance |
| 5718 | * of overstepping resync_max here |
| 5719 | */ |
| 5720 | |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5721 | /* if there is too many failed drives and we are trying |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5722 | * to resync, then assert that we are finished, because there is |
| 5723 | * nothing we can do. |
| 5724 | */ |
NeilBrown | 3285edf | 2006-06-26 00:27:55 -0700 | [diff] [blame] | 5725 | if (mddev->degraded >= conf->max_degraded && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5726 | test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 5727 | sector_t rv = mddev->dev_sectors - sector_nr; |
NeilBrown | 57afd89 | 2005-06-21 17:17:13 -0700 | [diff] [blame] | 5728 | *skipped = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5729 | return rv; |
| 5730 | } |
majianpeng | 6f60804 | 2013-04-24 11:42:41 +1000 | [diff] [blame] | 5731 | if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) && |
| 5732 | !conf->fullsync && |
| 5733 | !bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) && |
| 5734 | sync_blocks >= STRIPE_SECTORS) { |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5735 | /* we can skip this block, and probably more */ |
| 5736 | sync_blocks /= STRIPE_SECTORS; |
| 5737 | *skipped = 1; |
| 5738 | return sync_blocks * STRIPE_SECTORS; /* keep things rounded to whole stripes */ |
| 5739 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5740 | |
Goldwyn Rodrigues | c40f341 | 2015-08-19 08:14:42 +1000 | [diff] [blame] | 5741 | bitmap_cond_end_sync(mddev->bitmap, sector_nr, false); |
NeilBrown | b47490c | 2008-02-06 01:39:50 -0800 | [diff] [blame] | 5742 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5743 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5744 | if (sh == NULL) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5745 | sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5746 | /* make sure we don't swamp the stripe cache if someone else |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5747 | * is trying to get access |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5748 | */ |
Nishanth Aravamudan | 66c006a | 2005-11-07 01:01:17 -0800 | [diff] [blame] | 5749 | schedule_timeout_uninterruptible(1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5750 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5751 | /* Need to check if array will still be degraded after recovery/resync |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 5752 | * Note in case of > 1 drive failures it's possible we're rebuilding |
| 5753 | * one drive while leaving another faulty drive in array. |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5754 | */ |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 5755 | rcu_read_lock(); |
| 5756 | for (i = 0; i < conf->raid_disks; i++) { |
| 5757 | struct md_rdev *rdev = ACCESS_ONCE(conf->disks[i].rdev); |
| 5758 | |
| 5759 | if (rdev == NULL || test_bit(Faulty, &rdev->flags)) |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5760 | still_degraded = 1; |
Eric Mei | 16d9cfa | 2015-01-06 09:35:02 -0800 | [diff] [blame] | 5761 | } |
| 5762 | rcu_read_unlock(); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 5763 | |
| 5764 | bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded); |
| 5765 | |
NeilBrown | 83206d6 | 2011-07-26 11:19:49 +1000 | [diff] [blame] | 5766 | set_bit(STRIPE_SYNC_REQUESTED, &sh->state); |
Eivind Sarto | 053f5b6 | 2014-06-09 17:06:19 -0700 | [diff] [blame] | 5767 | set_bit(STRIPE_HANDLE, &sh->state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5768 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5769 | raid5_release_stripe(sh); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5770 | |
| 5771 | return STRIPE_SECTORS; |
| 5772 | } |
| 5773 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5774 | 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] | 5775 | { |
| 5776 | /* We may not be able to submit a whole bio at once as there |
| 5777 | * may not be enough stripe_heads available. |
| 5778 | * We cannot pre-allocate enough stripe_heads as we may need |
| 5779 | * more than exist in the cache (if we allow ever large chunks). |
| 5780 | * So we do one stripe head at a time and record in |
| 5781 | * ->bi_hw_segments how many have been done. |
| 5782 | * |
| 5783 | * We *know* that this entire raid_bio is in one chunk, so |
| 5784 | * it will be only one 'dd_idx' and only need one call to raid5_compute_sector. |
| 5785 | */ |
| 5786 | struct stripe_head *sh; |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5787 | int dd_idx; |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5788 | sector_t sector, logical_sector, last_sector; |
| 5789 | int scnt = 0; |
| 5790 | int remaining; |
| 5791 | int handled = 0; |
| 5792 | |
Kent Overstreet | 4f024f3 | 2013-10-11 15:44:27 -0700 | [diff] [blame] | 5793 | logical_sector = raid_bio->bi_iter.bi_sector & |
| 5794 | ~((sector_t)STRIPE_SECTORS-1); |
NeilBrown | 112bf89 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5795 | sector = raid5_compute_sector(conf, logical_sector, |
NeilBrown | 911d4ee | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 5796 | 0, &dd_idx, NULL); |
Kent Overstreet | f73a1c7 | 2012-09-25 15:05:12 -0700 | [diff] [blame] | 5797 | last_sector = bio_end_sector(raid_bio); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5798 | |
| 5799 | for (; logical_sector < last_sector; |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5800 | logical_sector += STRIPE_SECTORS, |
| 5801 | sector += STRIPE_SECTORS, |
| 5802 | scnt++) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5803 | |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5804 | if (scnt < raid5_bi_processed_stripes(raid_bio)) |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5805 | /* already done this stripe */ |
| 5806 | continue; |
| 5807 | |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5808 | sh = raid5_get_active_stripe(conf, sector, 0, 1, 1); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5809 | |
| 5810 | if (!sh) { |
| 5811 | /* failed to get a stripe - must wait */ |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5812 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5813 | conf->retry_read_aligned = raid_bio; |
| 5814 | return handled; |
| 5815 | } |
| 5816 | |
shli@kernel.org | da41ba6 | 2014-12-15 12:57:03 +1100 | [diff] [blame] | 5817 | if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) { |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5818 | raid5_release_stripe(sh); |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5819 | raid5_set_bi_processed_stripes(raid_bio, scnt); |
Neil Brown | 387bb17 | 2007-02-08 14:20:29 -0800 | [diff] [blame] | 5820 | conf->retry_read_aligned = raid_bio; |
| 5821 | return handled; |
| 5822 | } |
| 5823 | |
majianpeng | 3f9e7c1 | 2012-07-31 10:04:21 +1000 | [diff] [blame] | 5824 | set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 5825 | handle_stripe(sh); |
Shaohua Li | 6d036f7 | 2015-08-13 14:31:57 -0700 | [diff] [blame] | 5826 | raid5_release_stripe(sh); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5827 | handled++; |
| 5828 | } |
Shaohua Li | e7836bd6 | 2012-07-19 16:01:31 +1000 | [diff] [blame] | 5829 | remaining = raid5_dec_bi_active_stripes(raid_bio); |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5830 | if (remaining == 0) { |
| 5831 | trace_block_bio_complete(bdev_get_queue(raid_bio->bi_bdev), |
| 5832 | raid_bio, 0); |
Christoph Hellwig | 4246a0b | 2015-07-20 15:29:37 +0200 | [diff] [blame] | 5833 | bio_endio(raid_bio); |
Linus Torvalds | 0a82a8d | 2013-04-18 09:00:26 -0700 | [diff] [blame] | 5834 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5835 | if (atomic_dec_and_test(&conf->active_aligned_reads)) |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 5836 | wake_up(&conf->wait_for_quiescent); |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5837 | return handled; |
| 5838 | } |
| 5839 | |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5840 | static int handle_active_stripes(struct r5conf *conf, int group, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5841 | struct r5worker *worker, |
| 5842 | struct list_head *temp_inactive_list) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5843 | { |
| 5844 | struct stripe_head *batch[MAX_STRIPE_BATCH], *sh; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5845 | int i, batch_size = 0, hash; |
| 5846 | bool release_inactive = false; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5847 | |
| 5848 | while (batch_size < MAX_STRIPE_BATCH && |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5849 | (sh = __get_priority_stripe(conf, group)) != NULL) |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5850 | batch[batch_size++] = sh; |
| 5851 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5852 | if (batch_size == 0) { |
| 5853 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 5854 | if (!list_empty(temp_inactive_list + i)) |
| 5855 | break; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 5856 | if (i == NR_STRIPE_HASH_LOCKS) { |
| 5857 | spin_unlock_irq(&conf->device_lock); |
| 5858 | r5l_flush_stripe_to_raid(conf->log); |
| 5859 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5860 | return batch_size; |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 5861 | } |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5862 | release_inactive = true; |
| 5863 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5864 | spin_unlock_irq(&conf->device_lock); |
| 5865 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5866 | release_inactive_stripe_list(conf, temp_inactive_list, |
| 5867 | NR_STRIPE_HASH_LOCKS); |
| 5868 | |
Shaohua Li | a8c34f9 | 2015-09-02 13:49:46 -0700 | [diff] [blame] | 5869 | r5l_flush_stripe_to_raid(conf->log); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5870 | if (release_inactive) { |
| 5871 | spin_lock_irq(&conf->device_lock); |
| 5872 | return 0; |
| 5873 | } |
| 5874 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5875 | for (i = 0; i < batch_size; i++) |
| 5876 | handle_stripe(batch[i]); |
Shaohua Li | f6bed0e | 2015-08-13 14:31:59 -0700 | [diff] [blame] | 5877 | r5l_write_stripe_run(conf->log); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5878 | |
| 5879 | cond_resched(); |
| 5880 | |
| 5881 | spin_lock_irq(&conf->device_lock); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5882 | for (i = 0; i < batch_size; i++) { |
| 5883 | hash = batch[i]->hash_lock_index; |
| 5884 | __release_stripe(conf, batch[i], &temp_inactive_list[hash]); |
| 5885 | } |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5886 | return batch_size; |
| 5887 | } |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5888 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5889 | static void raid5_do_work(struct work_struct *work) |
| 5890 | { |
| 5891 | struct r5worker *worker = container_of(work, struct r5worker, work); |
| 5892 | struct r5worker_group *group = worker->group; |
| 5893 | struct r5conf *conf = group->conf; |
| 5894 | int group_id = group - conf->worker_groups; |
| 5895 | int handled; |
| 5896 | struct blk_plug plug; |
| 5897 | |
| 5898 | pr_debug("+++ raid5worker active\n"); |
| 5899 | |
| 5900 | blk_start_plug(&plug); |
| 5901 | handled = 0; |
| 5902 | spin_lock_irq(&conf->device_lock); |
| 5903 | while (1) { |
| 5904 | int batch_size, released; |
| 5905 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5906 | released = release_stripe_list(conf, worker->temp_inactive_list); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5907 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5908 | batch_size = handle_active_stripes(conf, group_id, worker, |
| 5909 | worker->temp_inactive_list); |
Shaohua Li | bfc90cb | 2013-08-29 15:40:32 +0800 | [diff] [blame] | 5910 | worker->working = false; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 5911 | if (!batch_size && !released) |
| 5912 | break; |
| 5913 | handled += batch_size; |
| 5914 | } |
| 5915 | pr_debug("%d stripes handled\n", handled); |
| 5916 | |
| 5917 | spin_unlock_irq(&conf->device_lock); |
| 5918 | blk_finish_plug(&plug); |
| 5919 | |
| 5920 | pr_debug("--- raid5worker inactive\n"); |
| 5921 | } |
| 5922 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5923 | /* |
| 5924 | * This is our raid5 kernel thread. |
| 5925 | * |
| 5926 | * We scan the hash table for stripes which can be handled now. |
| 5927 | * During the scan, completed stripes are saved for us by the interrupt |
| 5928 | * handler, so that they will not have to wait for our next wakeup. |
| 5929 | */ |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 5930 | static void raid5d(struct md_thread *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5931 | { |
Shaohua Li | 4ed8731 | 2012-10-11 13:34:00 +1100 | [diff] [blame] | 5932 | struct mddev *mddev = thread->mddev; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 5933 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5934 | int handled; |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 5935 | struct blk_plug plug; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5936 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5937 | pr_debug("+++ raid5d active\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5938 | |
| 5939 | md_check_recovery(mddev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5940 | |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 5941 | if (!bio_list_empty(&conf->return_bi) && |
| 5942 | !test_bit(MD_CHANGE_PENDING, &mddev->flags)) { |
| 5943 | struct bio_list tmp = BIO_EMPTY_LIST; |
| 5944 | spin_lock_irq(&conf->device_lock); |
| 5945 | if (!test_bit(MD_CHANGE_PENDING, &mddev->flags)) { |
| 5946 | bio_list_merge(&tmp, &conf->return_bi); |
| 5947 | bio_list_init(&conf->return_bi); |
| 5948 | } |
| 5949 | spin_unlock_irq(&conf->device_lock); |
| 5950 | return_io(&tmp); |
| 5951 | } |
| 5952 | |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 5953 | blk_start_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5954 | handled = 0; |
| 5955 | spin_lock_irq(&conf->device_lock); |
| 5956 | while (1) { |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5957 | struct bio *bio; |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5958 | int batch_size, released; |
| 5959 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5960 | released = release_stripe_list(conf, conf->temp_inactive_list); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 5961 | if (released) |
| 5962 | clear_bit(R5_DID_ALLOC, &conf->cache_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5963 | |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 5964 | if ( |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5965 | !list_empty(&conf->bitmap_list)) { |
| 5966 | /* Now is a good time to flush some bitmap updates */ |
| 5967 | conf->seq_flush++; |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 5968 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5969 | bitmap_unplug(mddev->bitmap); |
NeilBrown | 700e432 | 2005-11-28 13:44:10 -0800 | [diff] [blame] | 5970 | spin_lock_irq(&conf->device_lock); |
NeilBrown | 7c13edc | 2011-04-18 18:25:43 +1000 | [diff] [blame] | 5971 | conf->seq_write = conf->seq_flush; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5972 | activate_bit_delay(conf, conf->temp_inactive_list); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5973 | } |
NeilBrown | 0021b7b | 2012-07-31 09:08:14 +0200 | [diff] [blame] | 5974 | raid5_activate_delayed(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 5975 | |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 5976 | while ((bio = remove_bio_from_retry(conf))) { |
| 5977 | int ok; |
| 5978 | spin_unlock_irq(&conf->device_lock); |
| 5979 | ok = retry_aligned_read(conf, bio); |
| 5980 | spin_lock_irq(&conf->device_lock); |
| 5981 | if (!ok) |
| 5982 | break; |
| 5983 | handled++; |
| 5984 | } |
| 5985 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 5986 | batch_size = handle_active_stripes(conf, ANY_GROUP, NULL, |
| 5987 | conf->temp_inactive_list); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 5988 | if (!batch_size && !released) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5989 | break; |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5990 | handled += batch_size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5991 | |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5992 | if (mddev->flags & ~(1<<MD_CHANGE_PENDING)) { |
| 5993 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | de393cd | 2011-07-28 11:31:48 +1000 | [diff] [blame] | 5994 | md_check_recovery(mddev); |
Shaohua Li | 46a0640 | 2012-08-02 08:33:15 +1000 | [diff] [blame] | 5995 | spin_lock_irq(&conf->device_lock); |
| 5996 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5997 | } |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 5998 | pr_debug("%d stripes handled\n", handled); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5999 | |
| 6000 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6001 | if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) && |
| 6002 | mutex_trylock(&conf->cache_size_mutex)) { |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6003 | grow_one_stripe(conf, __GFP_NOWARN); |
| 6004 | /* Set flag even if allocation failed. This helps |
| 6005 | * slow down allocation requests when mem is short |
| 6006 | */ |
| 6007 | set_bit(R5_DID_ALLOC, &conf->cache_state); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6008 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6009 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6010 | |
Shaohua Li | 0576b1c | 2015-08-13 14:32:00 -0700 | [diff] [blame] | 6011 | r5l_flush_stripe_to_raid(conf->log); |
| 6012 | |
Dan Williams | c9f21aa | 2008-07-23 12:05:51 -0700 | [diff] [blame] | 6013 | async_tx_issue_pending_all(); |
NeilBrown | e1dfa0a | 2011-04-18 18:25:41 +1000 | [diff] [blame] | 6014 | blk_finish_plug(&plug); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6015 | |
Dan Williams | 45b4233 | 2007-07-09 11:56:43 -0700 | [diff] [blame] | 6016 | pr_debug("--- raid5d inactive\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6017 | } |
| 6018 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6019 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6020 | raid5_show_stripe_cache_size(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6021 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6022 | struct r5conf *conf; |
| 6023 | int ret = 0; |
| 6024 | spin_lock(&mddev->lock); |
| 6025 | conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6026 | if (conf) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6027 | ret = sprintf(page, "%d\n", conf->min_nr_stripes); |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6028 | spin_unlock(&mddev->lock); |
| 6029 | return ret; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6030 | } |
| 6031 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6032 | int |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6033 | raid5_set_cache_size(struct mddev *mddev, int size) |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6034 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6035 | struct r5conf *conf = mddev->private; |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6036 | int err; |
| 6037 | |
| 6038 | if (size <= 16 || size > 32768) |
| 6039 | return -EINVAL; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6040 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6041 | conf->min_nr_stripes = size; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6042 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6043 | while (size < conf->max_nr_stripes && |
| 6044 | drop_one_stripe(conf)) |
| 6045 | ; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6046 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6047 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6048 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6049 | err = md_allow_write(mddev); |
| 6050 | if (err) |
| 6051 | return err; |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6052 | |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6053 | mutex_lock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6054 | while (size > conf->max_nr_stripes) |
| 6055 | if (!grow_one_stripe(conf, GFP_KERNEL)) |
| 6056 | break; |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6057 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | 486f064 | 2015-02-25 12:10:35 +1100 | [diff] [blame] | 6058 | |
NeilBrown | c41d4ac | 2010-06-01 19:37:24 +1000 | [diff] [blame] | 6059 | return 0; |
| 6060 | } |
| 6061 | EXPORT_SYMBOL(raid5_set_cache_size); |
| 6062 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6063 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6064 | 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] | 6065 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6066 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6067 | unsigned long new; |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6068 | int err; |
| 6069 | |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6070 | if (len >= PAGE_SIZE) |
| 6071 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6072 | if (kstrtoul(page, 10, &new)) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6073 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6074 | err = mddev_lock(mddev); |
Dan Williams | b5470dc | 2008-06-27 21:44:04 -0700 | [diff] [blame] | 6075 | if (err) |
| 6076 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6077 | conf = mddev->private; |
| 6078 | if (!conf) |
| 6079 | err = -ENODEV; |
| 6080 | else |
| 6081 | err = raid5_set_cache_size(mddev, new); |
| 6082 | mddev_unlock(mddev); |
| 6083 | |
| 6084 | return err ?: len; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6085 | } |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6086 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6087 | static struct md_sysfs_entry |
| 6088 | raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR, |
| 6089 | raid5_show_stripe_cache_size, |
| 6090 | raid5_store_stripe_cache_size); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6091 | |
| 6092 | static ssize_t |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6093 | raid5_show_rmw_level(struct mddev *mddev, char *page) |
| 6094 | { |
| 6095 | struct r5conf *conf = mddev->private; |
| 6096 | if (conf) |
| 6097 | return sprintf(page, "%d\n", conf->rmw_level); |
| 6098 | else |
| 6099 | return 0; |
| 6100 | } |
| 6101 | |
| 6102 | static ssize_t |
| 6103 | raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len) |
| 6104 | { |
| 6105 | struct r5conf *conf = mddev->private; |
| 6106 | unsigned long new; |
| 6107 | |
| 6108 | if (!conf) |
| 6109 | return -ENODEV; |
| 6110 | |
| 6111 | if (len >= PAGE_SIZE) |
| 6112 | return -EINVAL; |
| 6113 | |
| 6114 | if (kstrtoul(page, 10, &new)) |
| 6115 | return -EINVAL; |
| 6116 | |
| 6117 | if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome) |
| 6118 | return -EINVAL; |
| 6119 | |
| 6120 | if (new != PARITY_DISABLE_RMW && |
| 6121 | new != PARITY_ENABLE_RMW && |
| 6122 | new != PARITY_PREFER_RMW) |
| 6123 | return -EINVAL; |
| 6124 | |
| 6125 | conf->rmw_level = new; |
| 6126 | return len; |
| 6127 | } |
| 6128 | |
| 6129 | static struct md_sysfs_entry |
| 6130 | raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR, |
| 6131 | raid5_show_rmw_level, |
| 6132 | raid5_store_rmw_level); |
| 6133 | |
| 6134 | |
| 6135 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6136 | raid5_show_preread_threshold(struct mddev *mddev, char *page) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6137 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6138 | struct r5conf *conf; |
| 6139 | int ret = 0; |
| 6140 | spin_lock(&mddev->lock); |
| 6141 | conf = mddev->private; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6142 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6143 | ret = sprintf(page, "%d\n", conf->bypass_threshold); |
| 6144 | spin_unlock(&mddev->lock); |
| 6145 | return ret; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6146 | } |
| 6147 | |
| 6148 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6149 | 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] | 6150 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6151 | struct r5conf *conf; |
Dan Williams | 4ef197d8 | 2008-04-28 02:15:54 -0700 | [diff] [blame] | 6152 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6153 | int err; |
| 6154 | |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6155 | if (len >= PAGE_SIZE) |
| 6156 | return -EINVAL; |
Jingoo Han | b29bebd | 2013-06-01 16:15:16 +0900 | [diff] [blame] | 6157 | if (kstrtoul(page, 10, &new)) |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6158 | return -EINVAL; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6159 | |
| 6160 | err = mddev_lock(mddev); |
| 6161 | if (err) |
| 6162 | return err; |
| 6163 | conf = mddev->private; |
| 6164 | if (!conf) |
| 6165 | err = -ENODEV; |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6166 | else if (new > conf->min_nr_stripes) |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6167 | err = -EINVAL; |
| 6168 | else |
| 6169 | conf->bypass_threshold = new; |
| 6170 | mddev_unlock(mddev); |
| 6171 | return err ?: len; |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6172 | } |
| 6173 | |
| 6174 | static struct md_sysfs_entry |
| 6175 | raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold, |
| 6176 | S_IRUGO | S_IWUSR, |
| 6177 | raid5_show_preread_threshold, |
| 6178 | raid5_store_preread_threshold); |
| 6179 | |
| 6180 | static ssize_t |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6181 | raid5_show_skip_copy(struct mddev *mddev, char *page) |
| 6182 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6183 | struct r5conf *conf; |
| 6184 | int ret = 0; |
| 6185 | spin_lock(&mddev->lock); |
| 6186 | conf = mddev->private; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6187 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6188 | ret = sprintf(page, "%d\n", conf->skip_copy); |
| 6189 | spin_unlock(&mddev->lock); |
| 6190 | return ret; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6191 | } |
| 6192 | |
| 6193 | static ssize_t |
| 6194 | raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len) |
| 6195 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6196 | struct r5conf *conf; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6197 | unsigned long new; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6198 | int err; |
| 6199 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6200 | if (len >= PAGE_SIZE) |
| 6201 | return -EINVAL; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6202 | if (kstrtoul(page, 10, &new)) |
| 6203 | return -EINVAL; |
| 6204 | new = !!new; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6205 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6206 | err = mddev_lock(mddev); |
| 6207 | if (err) |
| 6208 | return err; |
| 6209 | conf = mddev->private; |
| 6210 | if (!conf) |
| 6211 | err = -ENODEV; |
| 6212 | else if (new != conf->skip_copy) { |
| 6213 | mddev_suspend(mddev); |
| 6214 | conf->skip_copy = new; |
| 6215 | if (new) |
| 6216 | mddev->queue->backing_dev_info.capabilities |= |
| 6217 | BDI_CAP_STABLE_WRITES; |
| 6218 | else |
| 6219 | mddev->queue->backing_dev_info.capabilities &= |
| 6220 | ~BDI_CAP_STABLE_WRITES; |
| 6221 | mddev_resume(mddev); |
| 6222 | } |
| 6223 | mddev_unlock(mddev); |
| 6224 | return err ?: len; |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6225 | } |
| 6226 | |
| 6227 | static struct md_sysfs_entry |
| 6228 | raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR, |
| 6229 | raid5_show_skip_copy, |
| 6230 | raid5_store_skip_copy); |
| 6231 | |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6232 | static ssize_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6233 | stripe_cache_active_show(struct mddev *mddev, char *page) |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6234 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6235 | struct r5conf *conf = mddev->private; |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6236 | if (conf) |
| 6237 | return sprintf(page, "%d\n", atomic_read(&conf->active_stripes)); |
| 6238 | else |
| 6239 | return 0; |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6240 | } |
| 6241 | |
NeilBrown | 96de1e6 | 2005-11-08 21:39:39 -0800 | [diff] [blame] | 6242 | static struct md_sysfs_entry |
| 6243 | raid5_stripecache_active = __ATTR_RO(stripe_cache_active); |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6244 | |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6245 | static ssize_t |
| 6246 | raid5_show_group_thread_cnt(struct mddev *mddev, char *page) |
| 6247 | { |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6248 | struct r5conf *conf; |
| 6249 | int ret = 0; |
| 6250 | spin_lock(&mddev->lock); |
| 6251 | conf = mddev->private; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6252 | if (conf) |
NeilBrown | 7b1485b | 2014-12-15 12:56:59 +1100 | [diff] [blame] | 6253 | ret = sprintf(page, "%d\n", conf->worker_cnt_per_group); |
| 6254 | spin_unlock(&mddev->lock); |
| 6255 | return ret; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6256 | } |
| 6257 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6258 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 6259 | int *group_cnt, |
| 6260 | int *worker_cnt_per_group, |
| 6261 | struct r5worker_group **worker_groups); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6262 | static ssize_t |
| 6263 | raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len) |
| 6264 | { |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6265 | struct r5conf *conf; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6266 | unsigned long new; |
| 6267 | int err; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6268 | struct r5worker_group *new_groups, *old_groups; |
| 6269 | int group_cnt, worker_cnt_per_group; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6270 | |
| 6271 | if (len >= PAGE_SIZE) |
| 6272 | return -EINVAL; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6273 | if (kstrtoul(page, 10, &new)) |
| 6274 | return -EINVAL; |
| 6275 | |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6276 | err = mddev_lock(mddev); |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6277 | if (err) |
| 6278 | return err; |
NeilBrown | 6791875 | 2014-12-15 12:57:01 +1100 | [diff] [blame] | 6279 | conf = mddev->private; |
| 6280 | if (!conf) |
| 6281 | err = -ENODEV; |
| 6282 | else if (new != conf->worker_cnt_per_group) { |
| 6283 | mddev_suspend(mddev); |
| 6284 | |
| 6285 | old_groups = conf->worker_groups; |
| 6286 | if (old_groups) |
| 6287 | flush_workqueue(raid5_wq); |
| 6288 | |
| 6289 | err = alloc_thread_groups(conf, new, |
| 6290 | &group_cnt, &worker_cnt_per_group, |
| 6291 | &new_groups); |
| 6292 | if (!err) { |
| 6293 | spin_lock_irq(&conf->device_lock); |
| 6294 | conf->group_cnt = group_cnt; |
| 6295 | conf->worker_cnt_per_group = worker_cnt_per_group; |
| 6296 | conf->worker_groups = new_groups; |
| 6297 | spin_unlock_irq(&conf->device_lock); |
| 6298 | |
| 6299 | if (old_groups) |
| 6300 | kfree(old_groups[0].workers); |
| 6301 | kfree(old_groups); |
| 6302 | } |
| 6303 | mddev_resume(mddev); |
| 6304 | } |
| 6305 | mddev_unlock(mddev); |
| 6306 | |
| 6307 | return err ?: len; |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6308 | } |
| 6309 | |
| 6310 | static struct md_sysfs_entry |
| 6311 | raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR, |
| 6312 | raid5_show_group_thread_cnt, |
| 6313 | raid5_store_group_thread_cnt); |
| 6314 | |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6315 | static struct attribute *raid5_attrs[] = { |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6316 | &raid5_stripecache_size.attr, |
| 6317 | &raid5_stripecache_active.attr, |
Dan Williams | 8b3e6cd | 2008-04-28 02:15:53 -0700 | [diff] [blame] | 6318 | &raid5_preread_bypass_threshold.attr, |
Shaohua Li | b721420 | 2013-08-27 17:50:42 +0800 | [diff] [blame] | 6319 | &raid5_group_thread_cnt.attr, |
Shaohua Li | d592a99 | 2014-05-21 17:57:44 +0800 | [diff] [blame] | 6320 | &raid5_skip_copy.attr, |
Markus Stockhausen | d06f191 | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6321 | &raid5_rmw_level.attr, |
Song Liu | 2c7da14 | 2016-11-17 15:24:41 -0800 | [diff] [blame] | 6322 | &r5c_journal_mode.attr, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6323 | NULL, |
| 6324 | }; |
NeilBrown | 007583c | 2005-11-08 21:39:30 -0800 | [diff] [blame] | 6325 | static struct attribute_group raid5_attrs_group = { |
| 6326 | .name = NULL, |
| 6327 | .attrs = raid5_attrs, |
NeilBrown | 3f294f4 | 2005-11-08 21:39:25 -0800 | [diff] [blame] | 6328 | }; |
| 6329 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6330 | static int alloc_thread_groups(struct r5conf *conf, int cnt, |
| 6331 | int *group_cnt, |
| 6332 | int *worker_cnt_per_group, |
| 6333 | struct r5worker_group **worker_groups) |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6334 | { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6335 | int i, j, k; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6336 | ssize_t size; |
| 6337 | struct r5worker *workers; |
| 6338 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6339 | *worker_cnt_per_group = cnt; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6340 | if (cnt == 0) { |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6341 | *group_cnt = 0; |
| 6342 | *worker_groups = NULL; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6343 | return 0; |
| 6344 | } |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6345 | *group_cnt = num_possible_nodes(); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6346 | size = sizeof(struct r5worker) * cnt; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6347 | workers = kzalloc(size * *group_cnt, GFP_NOIO); |
| 6348 | *worker_groups = kzalloc(sizeof(struct r5worker_group) * |
| 6349 | *group_cnt, GFP_NOIO); |
| 6350 | if (!*worker_groups || !workers) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6351 | kfree(workers); |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6352 | kfree(*worker_groups); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6353 | return -ENOMEM; |
| 6354 | } |
| 6355 | |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6356 | for (i = 0; i < *group_cnt; i++) { |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6357 | struct r5worker_group *group; |
| 6358 | |
NeilBrown | 0c775d5 | 2013-11-25 11:12:43 +1100 | [diff] [blame] | 6359 | group = &(*worker_groups)[i]; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6360 | INIT_LIST_HEAD(&group->handle_list); |
| 6361 | group->conf = conf; |
| 6362 | group->workers = workers + i * cnt; |
| 6363 | |
| 6364 | for (j = 0; j < cnt; j++) { |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6365 | struct r5worker *worker = group->workers + j; |
| 6366 | worker->group = group; |
| 6367 | INIT_WORK(&worker->work, raid5_do_work); |
| 6368 | |
| 6369 | for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++) |
| 6370 | INIT_LIST_HEAD(worker->temp_inactive_list + k); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6371 | } |
| 6372 | } |
| 6373 | |
| 6374 | return 0; |
| 6375 | } |
| 6376 | |
| 6377 | static void free_thread_groups(struct r5conf *conf) |
| 6378 | { |
| 6379 | if (conf->worker_groups) |
| 6380 | kfree(conf->worker_groups[0].workers); |
| 6381 | kfree(conf->worker_groups); |
| 6382 | conf->worker_groups = NULL; |
| 6383 | } |
| 6384 | |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6385 | static sector_t |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 6386 | raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks) |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6387 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6388 | struct r5conf *conf = mddev->private; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6389 | |
| 6390 | if (!sectors) |
| 6391 | sectors = mddev->dev_sectors; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6392 | if (!raid_disks) |
NeilBrown | 7ec0547 | 2009-03-31 15:10:36 +1100 | [diff] [blame] | 6393 | /* size is defined by the smallest of previous and new size */ |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6394 | raid_disks = min(conf->raid_disks, conf->previous_raid_disks); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6395 | |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 6396 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
| 6397 | sectors &= ~((sector_t)conf->prev_chunk_sectors - 1); |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 6398 | return sectors * (raid_disks - conf->max_degraded); |
| 6399 | } |
| 6400 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6401 | static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 6402 | { |
| 6403 | safe_put_page(percpu->spare_page); |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6404 | if (percpu->scribble) |
| 6405 | flex_array_free(percpu->scribble); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6406 | percpu->spare_page = NULL; |
| 6407 | percpu->scribble = NULL; |
| 6408 | } |
| 6409 | |
| 6410 | static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu) |
| 6411 | { |
| 6412 | if (conf->level == 6 && !percpu->spare_page) |
| 6413 | percpu->spare_page = alloc_page(GFP_KERNEL); |
| 6414 | if (!percpu->scribble) |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6415 | percpu->scribble = scribble_alloc(max(conf->raid_disks, |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 6416 | conf->previous_raid_disks), |
| 6417 | max(conf->chunk_sectors, |
| 6418 | conf->prev_chunk_sectors) |
| 6419 | / STRIPE_SECTORS, |
| 6420 | GFP_KERNEL); |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6421 | |
| 6422 | if (!percpu->scribble || (conf->level == 6 && !percpu->spare_page)) { |
| 6423 | free_scratch_buffer(conf, percpu); |
| 6424 | return -ENOMEM; |
| 6425 | } |
| 6426 | |
| 6427 | return 0; |
| 6428 | } |
| 6429 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6430 | static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node) |
| 6431 | { |
| 6432 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
| 6433 | |
| 6434 | free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu)); |
| 6435 | return 0; |
| 6436 | } |
| 6437 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6438 | static void raid5_free_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6439 | { |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6440 | if (!conf->percpu) |
| 6441 | return; |
| 6442 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6443 | cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6444 | free_percpu(conf->percpu); |
| 6445 | } |
| 6446 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6447 | static void free_conf(struct r5conf *conf) |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6448 | { |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 6449 | if (conf->log) |
| 6450 | r5l_exit_log(conf->log); |
Shaohua Li | 30c8946 | 2016-09-21 09:07:13 -0700 | [diff] [blame] | 6451 | if (conf->shrinker.nr_deferred) |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6452 | unregister_shrinker(&conf->shrinker); |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 6453 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6454 | free_thread_groups(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6455 | shrink_stripes(conf); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6456 | raid5_free_percpu(conf); |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6457 | kfree(conf->disks); |
| 6458 | kfree(conf->stripe_hashtbl); |
| 6459 | kfree(conf); |
| 6460 | } |
| 6461 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6462 | 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] | 6463 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6464 | struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node); |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6465 | struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu); |
| 6466 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6467 | if (alloc_scratch_buffer(conf, percpu)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6468 | pr_warn("%s: failed memory allocation for cpu%u\n", |
| 6469 | __func__, cpu); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6470 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6471 | } |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6472 | return 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6473 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6474 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6475 | static int raid5_alloc_percpu(struct r5conf *conf) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6476 | { |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6477 | int err = 0; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6478 | |
Oleg Nesterov | 789b5e0 | 2014-02-06 03:42:45 +0530 | [diff] [blame] | 6479 | conf->percpu = alloc_percpu(struct raid5_percpu); |
| 6480 | if (!conf->percpu) |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6481 | return -ENOMEM; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6482 | |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 6483 | err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node); |
Shaohua Li | 27a353c | 2016-02-24 17:38:28 -0800 | [diff] [blame] | 6484 | if (!err) { |
| 6485 | conf->scribble_disks = max(conf->raid_disks, |
| 6486 | conf->previous_raid_disks); |
| 6487 | conf->scribble_sectors = max(conf->chunk_sectors, |
| 6488 | conf->prev_chunk_sectors); |
| 6489 | } |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6490 | return err; |
| 6491 | } |
| 6492 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6493 | static unsigned long raid5_cache_scan(struct shrinker *shrink, |
| 6494 | struct shrink_control *sc) |
| 6495 | { |
| 6496 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6497 | unsigned long ret = SHRINK_STOP; |
| 6498 | |
| 6499 | if (mutex_trylock(&conf->cache_size_mutex)) { |
| 6500 | ret= 0; |
NeilBrown | 49895bc | 2015-08-03 17:09:57 +1000 | [diff] [blame] | 6501 | while (ret < sc->nr_to_scan && |
| 6502 | conf->max_nr_stripes > conf->min_nr_stripes) { |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6503 | if (drop_one_stripe(conf) == 0) { |
| 6504 | ret = SHRINK_STOP; |
| 6505 | break; |
| 6506 | } |
| 6507 | ret++; |
| 6508 | } |
| 6509 | mutex_unlock(&conf->cache_size_mutex); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6510 | } |
| 6511 | return ret; |
| 6512 | } |
| 6513 | |
| 6514 | static unsigned long raid5_cache_count(struct shrinker *shrink, |
| 6515 | struct shrink_control *sc) |
| 6516 | { |
| 6517 | struct r5conf *conf = container_of(shrink, struct r5conf, shrinker); |
| 6518 | |
| 6519 | if (conf->max_nr_stripes < conf->min_nr_stripes) |
| 6520 | /* unlikely, but not impossible */ |
| 6521 | return 0; |
| 6522 | return conf->max_nr_stripes - conf->min_nr_stripes; |
| 6523 | } |
| 6524 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6525 | static struct r5conf *setup_conf(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6526 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6527 | struct r5conf *conf; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6528 | int raid_disk, memory, max_disks; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6529 | struct md_rdev *rdev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6530 | struct disk_info *disk; |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6531 | char pers_name[6]; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6532 | int i; |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6533 | int group_cnt, worker_cnt_per_group; |
| 6534 | struct r5worker_group *new_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6535 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6536 | if (mddev->new_level != 5 |
| 6537 | && mddev->new_level != 4 |
| 6538 | && mddev->new_level != 6) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6539 | pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n", |
| 6540 | mdname(mddev), mddev->new_level); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6541 | return ERR_PTR(-EIO); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6542 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6543 | if ((mddev->new_level == 5 |
| 6544 | && !algorithm_valid_raid5(mddev->new_layout)) || |
| 6545 | (mddev->new_level == 6 |
| 6546 | && !algorithm_valid_raid6(mddev->new_layout))) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6547 | pr_warn("md/raid:%s: layout %d not supported\n", |
| 6548 | mdname(mddev), mddev->new_layout); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6549 | return ERR_PTR(-EIO); |
| 6550 | } |
| 6551 | if (mddev->new_level == 6 && mddev->raid_disks < 4) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6552 | pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n", |
| 6553 | mdname(mddev), mddev->raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6554 | return ERR_PTR(-EINVAL); |
NeilBrown | 99c0fb5 | 2009-03-31 14:39:38 +1100 | [diff] [blame] | 6555 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6556 | |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6557 | if (!mddev->new_chunk_sectors || |
| 6558 | (mddev->new_chunk_sectors << 9) % PAGE_SIZE || |
| 6559 | !is_power_of_2(mddev->new_chunk_sectors)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6560 | pr_warn("md/raid:%s: invalid chunk size %d\n", |
| 6561 | mdname(mddev), mddev->new_chunk_sectors << 9); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6562 | return ERR_PTR(-EINVAL); |
NeilBrown | 4bbf377 | 2008-10-13 11:55:12 +1100 | [diff] [blame] | 6563 | } |
| 6564 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6565 | conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6566 | if (conf == NULL) |
| 6567 | goto abort; |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6568 | /* Don't enable multi-threading by default*/ |
majianpeng | 60aaf93 | 2013-11-14 15:16:20 +1100 | [diff] [blame] | 6569 | if (!alloc_thread_groups(conf, 0, &group_cnt, &worker_cnt_per_group, |
| 6570 | &new_group)) { |
| 6571 | conf->group_cnt = group_cnt; |
| 6572 | conf->worker_cnt_per_group = worker_cnt_per_group; |
| 6573 | conf->worker_groups = new_group; |
| 6574 | } else |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 6575 | goto abort; |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6576 | spin_lock_init(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 6577 | seqcount_init(&conf->gen_lock); |
NeilBrown | 2d5b569 | 2015-07-06 12:49:23 +1000 | [diff] [blame] | 6578 | mutex_init(&conf->cache_size_mutex); |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 6579 | init_waitqueue_head(&conf->wait_for_quiescent); |
Shaohua Li | 6ab2a4b | 2016-02-25 16:24:42 -0800 | [diff] [blame] | 6580 | init_waitqueue_head(&conf->wait_for_stripe); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6581 | init_waitqueue_head(&conf->wait_for_overlap); |
| 6582 | INIT_LIST_HEAD(&conf->handle_list); |
| 6583 | INIT_LIST_HEAD(&conf->hold_list); |
| 6584 | INIT_LIST_HEAD(&conf->delayed_list); |
| 6585 | INIT_LIST_HEAD(&conf->bitmap_list); |
NeilBrown | c3cce6c | 2015-08-14 12:47:33 +1000 | [diff] [blame] | 6586 | bio_list_init(&conf->return_bi); |
Shaohua Li | 773ca82 | 2013-08-27 17:50:39 +0800 | [diff] [blame] | 6587 | init_llist_head(&conf->released_stripes); |
Dan Williams | f5efd45 | 2009-10-16 15:55:38 +1100 | [diff] [blame] | 6588 | atomic_set(&conf->active_stripes, 0); |
| 6589 | atomic_set(&conf->preread_active_stripes, 0); |
| 6590 | atomic_set(&conf->active_aligned_reads, 0); |
| 6591 | conf->bypass_threshold = BYPASS_THRESHOLD; |
NeilBrown | d890fa2 | 2011-10-26 11:54:39 +1100 | [diff] [blame] | 6592 | conf->recovery_disabled = mddev->recovery_disabled - 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6593 | |
| 6594 | conf->raid_disks = mddev->raid_disks; |
| 6595 | if (mddev->reshape_position == MaxSector) |
| 6596 | conf->previous_raid_disks = mddev->raid_disks; |
| 6597 | else |
| 6598 | conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6599 | max_disks = max(conf->raid_disks, conf->previous_raid_disks); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6600 | |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6601 | conf->disks = kzalloc(max_disks * sizeof(struct disk_info), |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6602 | GFP_KERNEL); |
| 6603 | if (!conf->disks) |
| 6604 | goto abort; |
| 6605 | |
| 6606 | conf->mddev = mddev; |
| 6607 | |
| 6608 | if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL) |
| 6609 | goto abort; |
| 6610 | |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6611 | /* We init hash_locks[0] separately to that it can be used |
| 6612 | * as the reference lock in the spin_lock_nest_lock() call |
| 6613 | * in lock_all_device_hash_locks_irq in order to convince |
| 6614 | * lockdep that we know what we are doing. |
| 6615 | */ |
| 6616 | spin_lock_init(conf->hash_locks); |
| 6617 | for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6618 | spin_lock_init(conf->hash_locks + i); |
| 6619 | |
| 6620 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6621 | INIT_LIST_HEAD(conf->inactive_list + i); |
| 6622 | |
| 6623 | for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++) |
| 6624 | INIT_LIST_HEAD(conf->temp_inactive_list + i); |
| 6625 | |
Song Liu | 1e6d690 | 2016-11-17 15:24:39 -0800 | [diff] [blame] | 6626 | atomic_set(&conf->r5c_cached_full_stripes, 0); |
| 6627 | INIT_LIST_HEAD(&conf->r5c_full_stripe_list); |
| 6628 | atomic_set(&conf->r5c_cached_partial_stripes, 0); |
| 6629 | INIT_LIST_HEAD(&conf->r5c_partial_stripe_list); |
| 6630 | |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6631 | conf->level = mddev->new_level; |
shli@kernel.org | 46d5b78 | 2014-12-15 12:57:02 +1100 | [diff] [blame] | 6632 | conf->chunk_sectors = mddev->new_chunk_sectors; |
Dan Williams | 36d1c64 | 2009-07-14 11:48:22 -0700 | [diff] [blame] | 6633 | if (raid5_alloc_percpu(conf) != 0) |
| 6634 | goto abort; |
| 6635 | |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 6636 | pr_debug("raid456: run(%s) called.\n", mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6637 | |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 6638 | rdev_for_each(rdev, mddev) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6639 | raid_disk = rdev->raid_disk; |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6640 | if (raid_disk >= max_disks |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 6641 | || raid_disk < 0 || test_bit(Journal, &rdev->flags)) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6642 | continue; |
| 6643 | disk = conf->disks + raid_disk; |
| 6644 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6645 | if (test_bit(Replacement, &rdev->flags)) { |
| 6646 | if (disk->replacement) |
| 6647 | goto abort; |
| 6648 | disk->replacement = rdev; |
| 6649 | } else { |
| 6650 | if (disk->rdev) |
| 6651 | goto abort; |
| 6652 | disk->rdev = rdev; |
| 6653 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6654 | |
| 6655 | if (test_bit(In_sync, &rdev->flags)) { |
| 6656 | char b[BDEVNAME_SIZE]; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6657 | pr_info("md/raid:%s: device %s operational as raid disk %d\n", |
| 6658 | mdname(mddev), bdevname(rdev->bdev, b), raid_disk); |
Jonathan Brassow | d6b212f | 2011-06-08 18:00:28 -0500 | [diff] [blame] | 6659 | } else if (rdev->saved_raid_disk != raid_disk) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6660 | /* Cannot rely on bitmap to complete recovery */ |
| 6661 | conf->fullsync = 1; |
| 6662 | } |
| 6663 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6664 | conf->level = mddev->new_level; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6665 | if (conf->level == 6) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6666 | conf->max_degraded = 2; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6667 | if (raid6_call.xor_syndrome) |
| 6668 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 6669 | else |
| 6670 | conf->rmw_level = PARITY_DISABLE_RMW; |
| 6671 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6672 | conf->max_degraded = 1; |
Markus Stockhausen | 584acdd | 2014-12-15 12:57:05 +1100 | [diff] [blame] | 6673 | conf->rmw_level = PARITY_ENABLE_RMW; |
| 6674 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6675 | conf->algorithm = mddev->new_layout; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6676 | conf->reshape_progress = mddev->reshape_position; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 6677 | if (conf->reshape_progress != MaxSector) { |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 6678 | conf->prev_chunk_sectors = mddev->chunk_sectors; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 6679 | conf->prev_algo = mddev->layout; |
NeilBrown | 5cac6bc | 2015-07-17 12:17:50 +1000 | [diff] [blame] | 6680 | } else { |
| 6681 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 6682 | conf->prev_algo = conf->algorithm; |
NeilBrown | e183eae | 2009-03-31 15:20:22 +1100 | [diff] [blame] | 6683 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6684 | |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6685 | conf->min_nr_stripes = NR_STRIPES; |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 6686 | if (mddev->reshape_position != MaxSector) { |
| 6687 | int stripes = max_t(int, |
| 6688 | ((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4, |
| 6689 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4); |
| 6690 | conf->min_nr_stripes = max(NR_STRIPES, stripes); |
| 6691 | if (conf->min_nr_stripes != NR_STRIPES) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6692 | pr_info("md/raid:%s: force stripe size %d for reshape\n", |
Shaohua Li | ad5b0f7 | 2016-08-30 10:29:33 -0700 | [diff] [blame] | 6693 | mdname(mddev), conf->min_nr_stripes); |
| 6694 | } |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6695 | memory = conf->min_nr_stripes * (sizeof(struct stripe_head) + |
NeilBrown | 5e5e3e7 | 2009-10-16 16:35:30 +1100 | [diff] [blame] | 6696 | max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024; |
Shaohua Li | 4bda556 | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 6697 | atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6698 | if (grow_stripes(conf, conf->min_nr_stripes)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6699 | pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n", |
| 6700 | mdname(mddev), memory); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6701 | goto abort; |
| 6702 | } else |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6703 | pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory); |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 6704 | /* |
| 6705 | * Losing a stripe head costs more than the time to refill it, |
| 6706 | * it reduces the queue depth and so can hurt throughput. |
| 6707 | * So set it rather large, scaled by number of devices. |
| 6708 | */ |
| 6709 | conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4; |
| 6710 | conf->shrinker.scan_objects = raid5_cache_scan; |
| 6711 | conf->shrinker.count_objects = raid5_cache_count; |
| 6712 | conf->shrinker.batch = 128; |
| 6713 | conf->shrinker.flags = 0; |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 6714 | if (register_shrinker(&conf->shrinker)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6715 | pr_warn("md/raid:%s: couldn't register shrinker.\n", |
| 6716 | mdname(mddev)); |
Chao Yu | 6a0f53f | 2016-09-20 10:33:57 +0800 | [diff] [blame] | 6717 | goto abort; |
| 6718 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6719 | |
NeilBrown | 0232605 | 2012-07-03 15:56:52 +1000 | [diff] [blame] | 6720 | sprintf(pers_name, "raid%d", mddev->new_level); |
| 6721 | conf->thread = md_register_thread(raid5d, mddev, pers_name); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6722 | if (!conf->thread) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6723 | pr_warn("md/raid:%s: couldn't allocate thread.\n", |
| 6724 | mdname(mddev)); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6725 | goto abort; |
| 6726 | } |
| 6727 | |
| 6728 | return conf; |
| 6729 | |
| 6730 | abort: |
| 6731 | if (conf) { |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 6732 | free_conf(conf); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6733 | return ERR_PTR(-EIO); |
| 6734 | } else |
| 6735 | return ERR_PTR(-ENOMEM); |
| 6736 | } |
| 6737 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6738 | static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded) |
| 6739 | { |
| 6740 | switch (algo) { |
| 6741 | case ALGORITHM_PARITY_0: |
| 6742 | if (raid_disk < max_degraded) |
| 6743 | return 1; |
| 6744 | break; |
| 6745 | case ALGORITHM_PARITY_N: |
| 6746 | if (raid_disk >= raid_disks - max_degraded) |
| 6747 | return 1; |
| 6748 | break; |
| 6749 | case ALGORITHM_PARITY_0_6: |
NeilBrown | f72ffdd | 2014-09-30 14:23:59 +1000 | [diff] [blame] | 6750 | if (raid_disk == 0 || |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6751 | raid_disk == raid_disks - 1) |
| 6752 | return 1; |
| 6753 | break; |
| 6754 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 6755 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 6756 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 6757 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 6758 | if (raid_disk == raid_disks - 1) |
| 6759 | return 1; |
| 6760 | } |
| 6761 | return 0; |
| 6762 | } |
| 6763 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 6764 | static int raid5_run(struct mddev *mddev) |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6765 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 6766 | struct r5conf *conf; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 6767 | int working_disks = 0; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6768 | int dirty_parity_disks = 0; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 6769 | struct md_rdev *rdev; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 6770 | struct md_rdev *journal_dev = NULL; |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6771 | sector_t reshape_offset = 0; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6772 | int i; |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6773 | long long min_offset_diff = 0; |
| 6774 | int first = 1; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6775 | |
Andre Noll | 8c6ac86 | 2009-06-18 08:48:06 +1000 | [diff] [blame] | 6776 | if (mddev->recovery_cp != MaxSector) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6777 | pr_notice("md/raid:%s: not clean -- starting background reconstruction\n", |
| 6778 | mdname(mddev)); |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6779 | |
| 6780 | rdev_for_each(rdev, mddev) { |
| 6781 | long long diff; |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 6782 | |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 6783 | if (test_bit(Journal, &rdev->flags)) { |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 6784 | journal_dev = rdev; |
Shaohua Li | f2076e7 | 2015-10-08 21:54:12 -0700 | [diff] [blame] | 6785 | continue; |
| 6786 | } |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6787 | if (rdev->raid_disk < 0) |
| 6788 | continue; |
| 6789 | diff = (rdev->new_data_offset - rdev->data_offset); |
| 6790 | if (first) { |
| 6791 | min_offset_diff = diff; |
| 6792 | first = 0; |
| 6793 | } else if (mddev->reshape_backwards && |
| 6794 | diff < min_offset_diff) |
| 6795 | min_offset_diff = diff; |
| 6796 | else if (!mddev->reshape_backwards && |
| 6797 | diff > min_offset_diff) |
| 6798 | min_offset_diff = diff; |
| 6799 | } |
| 6800 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6801 | if (mddev->reshape_position != MaxSector) { |
| 6802 | /* Check that we can continue the reshape. |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6803 | * Difficulties arise if the stripe we would write to |
| 6804 | * next is at or after the stripe we would read from next. |
| 6805 | * For a reshape that changes the number of devices, this |
| 6806 | * is only possible for a very short time, and mdadm makes |
| 6807 | * sure that time appears to have past before assembling |
| 6808 | * the array. So we fail if that time hasn't passed. |
| 6809 | * For a reshape that keeps the number of devices the same |
| 6810 | * mdadm must be monitoring the reshape can keeping the |
| 6811 | * critical areas read-only and backed up. It will start |
| 6812 | * the array in read-only mode, so we check for that. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6813 | */ |
| 6814 | sector_t here_new, here_old; |
| 6815 | int old_disks; |
Andre Noll | 18b0033 | 2009-03-31 15:00:56 +1100 | [diff] [blame] | 6816 | int max_degraded = (mddev->level == 6 ? 2 : 1); |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 6817 | int chunk_sectors; |
| 6818 | int new_data_disks; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6819 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 6820 | if (journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6821 | pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n", |
| 6822 | mdname(mddev)); |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 6823 | return -EINVAL; |
| 6824 | } |
| 6825 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 6826 | if (mddev->new_level != mddev->level) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6827 | pr_warn("md/raid:%s: unsupported reshape required - aborting.\n", |
| 6828 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6829 | return -EINVAL; |
| 6830 | } |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6831 | old_disks = mddev->raid_disks - mddev->delta_disks; |
| 6832 | /* reshape_position must be on a new-stripe boundary, and one |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6833 | * further up in new geometry must map after here in old |
| 6834 | * geometry. |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 6835 | * If the chunk sizes are different, then as we perform reshape |
| 6836 | * in units of the largest of the two, reshape_position needs |
| 6837 | * be a multiple of the largest chunk size times new data disks. |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6838 | */ |
| 6839 | here_new = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 6840 | chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors); |
| 6841 | new_data_disks = mddev->raid_disks - max_degraded; |
| 6842 | if (sector_div(here_new, chunk_sectors * new_data_disks)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6843 | pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n", |
| 6844 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6845 | return -EINVAL; |
| 6846 | } |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 6847 | reshape_offset = here_new * chunk_sectors; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6848 | /* here_new is the stripe we will write to */ |
| 6849 | here_old = mddev->reshape_position; |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 6850 | sector_div(here_old, chunk_sectors * (old_disks-max_degraded)); |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 6851 | /* here_old is the first stripe that we might need to read |
| 6852 | * from */ |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 6853 | if (mddev->delta_disks == 0) { |
| 6854 | /* We cannot be sure it is safe to start an in-place |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6855 | * reshape. It is only safe if user-space is monitoring |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 6856 | * and taking constant backups. |
| 6857 | * mdadm always starts a situation like this in |
| 6858 | * readonly mode so it can take control before |
| 6859 | * allowing any writes. So just check for that. |
| 6860 | */ |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6861 | if (abs(min_offset_diff) >= mddev->chunk_sectors && |
| 6862 | abs(min_offset_diff) >= mddev->new_chunk_sectors) |
| 6863 | /* not really in-place - so OK */; |
| 6864 | else if (mddev->ro == 0) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6865 | pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n", |
| 6866 | mdname(mddev)); |
NeilBrown | 67ac601 | 2009-08-13 10:06:24 +1000 | [diff] [blame] | 6867 | return -EINVAL; |
| 6868 | } |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 6869 | } else if (mddev->reshape_backwards |
NeilBrown | 05256d9 | 2015-07-15 17:36:21 +1000 | [diff] [blame] | 6870 | ? (here_new * chunk_sectors + min_offset_diff <= |
| 6871 | here_old * chunk_sectors) |
| 6872 | : (here_new * chunk_sectors >= |
| 6873 | here_old * chunk_sectors + (-min_offset_diff))) { |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6874 | /* Reading from the same stripe as writing to - bad */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6875 | pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n", |
| 6876 | mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6877 | return -EINVAL; |
| 6878 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6879 | pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev)); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6880 | /* OK, we should be able to continue; */ |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6881 | } else { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6882 | BUG_ON(mddev->level != mddev->new_level); |
| 6883 | BUG_ON(mddev->layout != mddev->new_layout); |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 6884 | BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6885 | BUG_ON(mddev->delta_disks != 0); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6886 | } |
| 6887 | |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6888 | if (mddev->private == NULL) |
| 6889 | conf = setup_conf(mddev); |
| 6890 | else |
| 6891 | conf = mddev->private; |
| 6892 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6893 | if (IS_ERR(conf)) |
| 6894 | return PTR_ERR(conf); |
NeilBrown | 9ffae0c | 2006-01-06 00:20:32 -0800 | [diff] [blame] | 6895 | |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 6896 | if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) { |
| 6897 | if (!journal_dev) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6898 | pr_warn("md/raid:%s: journal disk is missing, force array readonly\n", |
| 6899 | mdname(mddev)); |
Song Liu | 486b0f7 | 2016-08-19 15:34:01 -0700 | [diff] [blame] | 6900 | mddev->ro = 1; |
| 6901 | set_disk_ro(mddev->gendisk, 1); |
| 6902 | } else if (mddev->recovery_cp == MaxSector) |
| 6903 | set_bit(MD_JOURNAL_CLEAN, &mddev->flags); |
Shaohua Li | 7dde2ad | 2015-10-08 21:54:10 -0700 | [diff] [blame] | 6904 | } |
| 6905 | |
NeilBrown | b5254dd | 2012-05-21 09:27:01 +1000 | [diff] [blame] | 6906 | conf->min_offset_diff = min_offset_diff; |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6907 | mddev->thread = conf->thread; |
| 6908 | conf->thread = NULL; |
| 6909 | mddev->private = conf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6910 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6911 | for (i = 0; i < conf->raid_disks && conf->previous_raid_disks; |
| 6912 | i++) { |
| 6913 | rdev = conf->disks[i].rdev; |
| 6914 | if (!rdev && conf->disks[i].replacement) { |
| 6915 | /* The replacement is all we have yet */ |
| 6916 | rdev = conf->disks[i].replacement; |
| 6917 | conf->disks[i].replacement = NULL; |
| 6918 | clear_bit(Replacement, &rdev->flags); |
| 6919 | conf->disks[i].rdev = rdev; |
| 6920 | } |
| 6921 | if (!rdev) |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6922 | continue; |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6923 | if (conf->disks[i].replacement && |
| 6924 | conf->reshape_progress != MaxSector) { |
| 6925 | /* replacements and reshape simply do not mix. */ |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6926 | pr_warn("md: cannot handle concurrent replacement and reshape.\n"); |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6927 | goto abort; |
| 6928 | } |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 6929 | if (test_bit(In_sync, &rdev->flags)) { |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6930 | working_disks++; |
NeilBrown | 2f11588 | 2010-06-17 17:41:03 +1000 | [diff] [blame] | 6931 | continue; |
| 6932 | } |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6933 | /* This disc is not fully in-sync. However if it |
| 6934 | * just stored parity (beyond the recovery_offset), |
| 6935 | * when we don't need to be concerned about the |
| 6936 | * array being dirty. |
| 6937 | * When reshape goes 'backwards', we never have |
| 6938 | * partially completed devices, so we only need |
| 6939 | * to worry about reshape going forwards. |
| 6940 | */ |
| 6941 | /* Hack because v0.91 doesn't store recovery_offset properly. */ |
| 6942 | if (mddev->major_version == 0 && |
| 6943 | mddev->minor_version > 90) |
| 6944 | rdev->recovery_offset = reshape_offset; |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 6945 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6946 | if (rdev->recovery_offset < reshape_offset) { |
| 6947 | /* We need to check old and new layout */ |
| 6948 | if (!only_parity(rdev->raid_disk, |
| 6949 | conf->algorithm, |
| 6950 | conf->raid_disks, |
| 6951 | conf->max_degraded)) |
| 6952 | continue; |
| 6953 | } |
| 6954 | if (!only_parity(rdev->raid_disk, |
| 6955 | conf->prev_algo, |
| 6956 | conf->previous_raid_disks, |
| 6957 | conf->max_degraded)) |
| 6958 | continue; |
| 6959 | dirty_parity_disks++; |
| 6960 | } |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6961 | |
NeilBrown | 17045f5 | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 6962 | /* |
| 6963 | * 0 for a fully functional array, 1 or 2 for a degraded array. |
| 6964 | */ |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 6965 | mddev->degraded = calc_degraded(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6966 | |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 6967 | if (has_failed(conf)) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6968 | pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n", |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 6969 | mdname(mddev), mddev->degraded, conf->raid_disks); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6970 | goto abort; |
| 6971 | } |
| 6972 | |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6973 | /* device size must be a multiple of chunk size */ |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 6974 | mddev->dev_sectors &= ~(mddev->chunk_sectors - 1); |
NeilBrown | 91adb56 | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 6975 | mddev->resync_max_sectors = mddev->dev_sectors; |
| 6976 | |
NeilBrown | c148ffd | 2009-11-13 17:47:00 +1100 | [diff] [blame] | 6977 | if (mddev->degraded > dirty_parity_disks && |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6978 | mddev->recovery_cp != MaxSector) { |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6979 | if (mddev->ok_start_degraded) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6980 | pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n", |
| 6981 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6982 | else { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6983 | pr_crit("md/raid:%s: cannot start dirty degraded array.\n", |
| 6984 | mdname(mddev)); |
NeilBrown | 6ff8d8ec | 2006-01-06 00:20:15 -0800 | [diff] [blame] | 6985 | goto abort; |
| 6986 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6987 | } |
| 6988 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 6989 | pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n", |
| 6990 | mdname(mddev), conf->level, |
| 6991 | mddev->raid_disks-mddev->degraded, mddev->raid_disks, |
| 6992 | mddev->new_layout); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6993 | |
| 6994 | print_raid5_conf(conf); |
| 6995 | |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6996 | if (conf->reshape_progress != MaxSector) { |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 6997 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 6998 | atomic_set(&conf->reshape_stripes, 0); |
| 6999 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7000 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
| 7001 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7002 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7003 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7004 | "reshape"); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7005 | } |
| 7006 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7007 | /* Ok, everything is just fine now */ |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7008 | if (mddev->to_remove == &raid5_attrs_group) |
| 7009 | mddev->to_remove = NULL; |
NeilBrown | 00bcb4a | 2010-06-01 19:37:23 +1000 | [diff] [blame] | 7010 | else if (mddev->kobj.sd && |
| 7011 | sysfs_create_group(&mddev->kobj, &raid5_attrs_group)) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7012 | pr_warn("raid5: failed to create sysfs attributes for %s\n", |
| 7013 | mdname(mddev)); |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7014 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
| 7015 | |
| 7016 | if (mddev->queue) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7017 | int chunk_size; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7018 | bool discard_supported = true; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7019 | /* read-ahead size must cover two whole stripes, which |
| 7020 | * is 2 * (datadisks) * chunksize where 'n' is the |
| 7021 | * number of raid devices |
| 7022 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7023 | int data_disks = conf->previous_raid_disks - conf->max_degraded; |
| 7024 | int stripe = data_disks * |
| 7025 | ((mddev->chunk_sectors << 9) / PAGE_SIZE); |
| 7026 | if (mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 7027 | mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7028 | |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7029 | chunk_size = mddev->chunk_sectors << 9; |
| 7030 | blk_queue_io_min(mddev->queue, chunk_size); |
| 7031 | blk_queue_io_opt(mddev->queue, chunk_size * |
| 7032 | (conf->raid_disks - conf->max_degraded)); |
Kent Overstreet | c78afc6 | 2013-07-11 22:39:53 -0700 | [diff] [blame] | 7033 | mddev->queue->limits.raid_partial_stripes_expensive = 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7034 | /* |
| 7035 | * We can only discard a whole stripe. It doesn't make sense to |
| 7036 | * discard data disk but write parity disk |
| 7037 | */ |
| 7038 | stripe = stripe * PAGE_SIZE; |
NeilBrown | 4ac6875 | 2012-11-19 13:11:26 +1100 | [diff] [blame] | 7039 | /* Round up to power of 2, as discard handling |
| 7040 | * currently assumes that */ |
| 7041 | while ((stripe-1) & stripe) |
| 7042 | stripe = (stripe | (stripe-1)) + 1; |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7043 | mddev->queue->limits.discard_alignment = stripe; |
| 7044 | mddev->queue->limits.discard_granularity = stripe; |
| 7045 | /* |
| 7046 | * unaligned part of discard request will be ignored, so can't |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 7047 | * guarantee discard_zeroes_data |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7048 | */ |
| 7049 | mddev->queue->limits.discard_zeroes_data = 0; |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7050 | |
H. Peter Anvin | 5026d7a | 2013-06-12 07:37:43 -0700 | [diff] [blame] | 7051 | blk_queue_max_write_same_sectors(mddev->queue, 0); |
| 7052 | |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7053 | rdev_for_each(rdev, mddev) { |
NeilBrown | 9f7c222 | 2010-07-26 12:04:13 +1000 | [diff] [blame] | 7054 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7055 | rdev->data_offset << 9); |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7056 | disk_stack_limits(mddev->gendisk, rdev->bdev, |
| 7057 | rdev->new_data_offset << 9); |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7058 | /* |
| 7059 | * discard_zeroes_data is required, otherwise data |
| 7060 | * could be lost. Consider a scenario: discard a stripe |
| 7061 | * (the stripe could be inconsistent if |
| 7062 | * discard_zeroes_data is 0); write one disk of the |
| 7063 | * stripe (the stripe could be inconsistent again |
| 7064 | * depending on which disks are used to calculate |
| 7065 | * parity); the disk is broken; The stripe data of this |
| 7066 | * disk is lost. |
| 7067 | */ |
| 7068 | if (!blk_queue_discard(bdev_get_queue(rdev->bdev)) || |
| 7069 | !bdev_get_queue(rdev->bdev)-> |
| 7070 | limits.discard_zeroes_data) |
| 7071 | discard_supported = false; |
NeilBrown | 8e0e99b | 2014-10-02 13:45:00 +1000 | [diff] [blame] | 7072 | /* Unfortunately, discard_zeroes_data is not currently |
| 7073 | * a guarantee - just a hint. So we only allow DISCARD |
| 7074 | * if the sysadmin has confirmed that only safe devices |
| 7075 | * are in use by setting a module parameter. |
| 7076 | */ |
| 7077 | if (!devices_handle_discard_safely) { |
| 7078 | if (discard_supported) { |
| 7079 | pr_info("md/raid456: discard support disabled due to uncertainty.\n"); |
| 7080 | pr_info("Set raid456.devices_handle_discard_safely=Y to override.\n"); |
| 7081 | } |
| 7082 | discard_supported = false; |
| 7083 | } |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7084 | } |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7085 | |
| 7086 | if (discard_supported && |
Jes Sorensen | e7597e6 | 2016-02-16 16:44:24 -0500 | [diff] [blame] | 7087 | mddev->queue->limits.max_discard_sectors >= (stripe >> 9) && |
| 7088 | mddev->queue->limits.discard_granularity >= stripe) |
Shaohua Li | 620125f | 2012-10-11 13:49:05 +1100 | [diff] [blame] | 7089 | queue_flag_set_unlocked(QUEUE_FLAG_DISCARD, |
| 7090 | mddev->queue); |
| 7091 | else |
| 7092 | queue_flag_clear_unlocked(QUEUE_FLAG_DISCARD, |
| 7093 | mddev->queue); |
Shaohua Li | 1dffddd | 2016-09-08 10:49:06 -0700 | [diff] [blame] | 7094 | |
| 7095 | blk_queue_max_hw_sectors(mddev->queue, UINT_MAX); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7096 | } |
| 7097 | |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 7098 | if (journal_dev) { |
| 7099 | char b[BDEVNAME_SIZE]; |
| 7100 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7101 | pr_debug("md/raid:%s: using device %s as journal\n", |
| 7102 | mdname(mddev), bdevname(journal_dev->bdev, b)); |
Shaohua Li | 5c7e81c | 2015-08-13 14:32:04 -0700 | [diff] [blame] | 7103 | r5l_init_log(conf, journal_dev); |
| 7104 | } |
| 7105 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7106 | return 0; |
| 7107 | abort: |
NeilBrown | 01f96c0 | 2011-09-21 15:30:20 +1000 | [diff] [blame] | 7108 | md_unregister_thread(&mddev->thread); |
NeilBrown | e4f869d | 2011-10-07 14:22:49 +1100 | [diff] [blame] | 7109 | print_raid5_conf(conf); |
| 7110 | free_conf(conf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7111 | mddev->private = NULL; |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7112 | 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] | 7113 | return -EIO; |
| 7114 | } |
| 7115 | |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7116 | static void raid5_free(struct mddev *mddev, void *priv) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7117 | { |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7118 | struct r5conf *conf = priv; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7119 | |
Dan Williams | 95fc17a | 2009-07-31 12:39:15 +1000 | [diff] [blame] | 7120 | free_conf(conf); |
NeilBrown | a64c876 | 2010-04-14 17:15:37 +1000 | [diff] [blame] | 7121 | mddev->to_remove = &raid5_attrs_group; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7122 | } |
| 7123 | |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7124 | static void raid5_status(struct seq_file *seq, struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7125 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7126 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7127 | int i; |
| 7128 | |
Andre Noll | 9d8f036 | 2009-06-18 08:45:01 +1000 | [diff] [blame] | 7129 | seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level, |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7130 | conf->chunk_sectors / 2, mddev->layout); |
NeilBrown | 02c2de8 | 2006-10-03 01:15:47 -0700 | [diff] [blame] | 7131 | seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded); |
NeilBrown | 5fd1335 | 2016-06-02 16:19:52 +1000 | [diff] [blame] | 7132 | rcu_read_lock(); |
| 7133 | for (i = 0; i < conf->raid_disks; i++) { |
| 7134 | struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev); |
| 7135 | seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_"); |
| 7136 | } |
| 7137 | rcu_read_unlock(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7138 | seq_printf (seq, "]"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7139 | } |
| 7140 | |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7141 | static void print_raid5_conf (struct r5conf *conf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7142 | { |
| 7143 | int i; |
| 7144 | struct disk_info *tmp; |
| 7145 | |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7146 | pr_debug("RAID conf printout:\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7147 | if (!conf) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7148 | pr_debug("(conf==NULL)\n"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7149 | return; |
| 7150 | } |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7151 | pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level, |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7152 | conf->raid_disks, |
| 7153 | conf->raid_disks - conf->mddev->degraded); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7154 | |
| 7155 | for (i = 0; i < conf->raid_disks; i++) { |
| 7156 | char b[BDEVNAME_SIZE]; |
| 7157 | tmp = conf->disks + i; |
| 7158 | if (tmp->rdev) |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7159 | pr_debug(" disk %d, o:%d, dev:%s\n", |
NeilBrown | 0c55e02 | 2010-05-03 14:09:02 +1000 | [diff] [blame] | 7160 | i, !test_bit(Faulty, &tmp->rdev->flags), |
| 7161 | bdevname(tmp->rdev->bdev, b)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7162 | } |
| 7163 | } |
| 7164 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7165 | static int raid5_spare_active(struct mddev *mddev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7166 | { |
| 7167 | int i; |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7168 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7169 | struct disk_info *tmp; |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7170 | int count = 0; |
| 7171 | unsigned long flags; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7172 | |
| 7173 | for (i = 0; i < conf->raid_disks; i++) { |
| 7174 | tmp = conf->disks + i; |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7175 | if (tmp->replacement |
| 7176 | && tmp->replacement->recovery_offset == MaxSector |
| 7177 | && !test_bit(Faulty, &tmp->replacement->flags) |
| 7178 | && !test_and_set_bit(In_sync, &tmp->replacement->flags)) { |
| 7179 | /* Replacement has just become active. */ |
| 7180 | if (!tmp->rdev |
| 7181 | || !test_and_clear_bit(In_sync, &tmp->rdev->flags)) |
| 7182 | count++; |
| 7183 | if (tmp->rdev) { |
| 7184 | /* Replaced device not technically faulty, |
| 7185 | * but we need to be sure it gets removed |
| 7186 | * and never re-added. |
| 7187 | */ |
| 7188 | set_bit(Faulty, &tmp->rdev->flags); |
| 7189 | sysfs_notify_dirent_safe( |
| 7190 | tmp->rdev->sysfs_state); |
| 7191 | } |
| 7192 | sysfs_notify_dirent_safe(tmp->replacement->sysfs_state); |
| 7193 | } else if (tmp->rdev |
NeilBrown | 70fffd0 | 2010-06-16 17:01:25 +1000 | [diff] [blame] | 7194 | && tmp->rdev->recovery_offset == MaxSector |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7195 | && !test_bit(Faulty, &tmp->rdev->flags) |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 7196 | && !test_and_set_bit(In_sync, &tmp->rdev->flags)) { |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7197 | count++; |
Jonathan Brassow | 43c73ca | 2011-01-14 09:14:33 +1100 | [diff] [blame] | 7198 | sysfs_notify_dirent_safe(tmp->rdev->sysfs_state); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7199 | } |
| 7200 | } |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7201 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 7202 | mddev->degraded = calc_degraded(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7203 | spin_unlock_irqrestore(&conf->device_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7204 | print_raid5_conf(conf); |
NeilBrown | 6b96562 | 2010-08-18 11:56:59 +1000 | [diff] [blame] | 7205 | return count; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7206 | } |
| 7207 | |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7208 | static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7209 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7210 | struct r5conf *conf = mddev->private; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7211 | int err = 0; |
NeilBrown | b8321b6 | 2011-12-23 10:17:51 +1100 | [diff] [blame] | 7212 | int number = rdev->raid_disk; |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7213 | struct md_rdev **rdevp; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7214 | struct disk_info *p = conf->disks + number; |
| 7215 | |
| 7216 | print_raid5_conf(conf); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7217 | if (test_bit(Journal, &rdev->flags) && conf->log) { |
| 7218 | struct r5l_log *log; |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7219 | /* |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7220 | * we can't wait pending write here, as this is called in |
| 7221 | * raid5d, wait will deadlock. |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7222 | */ |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7223 | if (atomic_read(&mddev->writes_pending)) |
| 7224 | return -EBUSY; |
| 7225 | log = conf->log; |
| 7226 | conf->log = NULL; |
| 7227 | synchronize_rcu(); |
| 7228 | r5l_exit_log(log); |
| 7229 | return 0; |
Shaohua Li | c2bb624 | 2015-10-08 21:54:07 -0700 | [diff] [blame] | 7230 | } |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7231 | if (rdev == p->rdev) |
| 7232 | rdevp = &p->rdev; |
| 7233 | else if (rdev == p->replacement) |
| 7234 | rdevp = &p->replacement; |
| 7235 | else |
| 7236 | return 0; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7237 | |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7238 | if (number >= conf->raid_disks && |
| 7239 | conf->reshape_progress == MaxSector) |
| 7240 | clear_bit(In_sync, &rdev->flags); |
| 7241 | |
| 7242 | if (test_bit(In_sync, &rdev->flags) || |
| 7243 | atomic_read(&rdev->nr_pending)) { |
| 7244 | err = -EBUSY; |
| 7245 | goto abort; |
| 7246 | } |
| 7247 | /* Only remove non-faulty devices if recovery |
| 7248 | * isn't possible. |
| 7249 | */ |
| 7250 | if (!test_bit(Faulty, &rdev->flags) && |
| 7251 | mddev->recovery_disabled != conf->recovery_disabled && |
| 7252 | !has_failed(conf) && |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7253 | (!p->replacement || p->replacement == rdev) && |
NeilBrown | 657e3e4 | 2011-12-23 10:17:52 +1100 | [diff] [blame] | 7254 | number < conf->raid_disks) { |
| 7255 | err = -EBUSY; |
| 7256 | goto abort; |
| 7257 | } |
| 7258 | *rdevp = NULL; |
NeilBrown | d787be4 | 2016-06-02 16:19:53 +1000 | [diff] [blame] | 7259 | if (!test_bit(RemoveSynchronized, &rdev->flags)) { |
| 7260 | synchronize_rcu(); |
| 7261 | if (atomic_read(&rdev->nr_pending)) { |
| 7262 | /* lost the race, try later */ |
| 7263 | err = -EBUSY; |
| 7264 | *rdevp = rdev; |
| 7265 | } |
| 7266 | } |
| 7267 | if (p->replacement) { |
NeilBrown | dd054fc | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7268 | /* We must have just cleared 'rdev' */ |
| 7269 | p->rdev = p->replacement; |
| 7270 | clear_bit(Replacement, &p->replacement->flags); |
| 7271 | smp_mb(); /* Make sure other CPUs may see both as identical |
| 7272 | * but will never see neither - if they are careful |
| 7273 | */ |
| 7274 | p->replacement = NULL; |
| 7275 | clear_bit(WantReplacement, &rdev->flags); |
| 7276 | } else |
| 7277 | /* We might have just removed the Replacement as faulty- |
| 7278 | * clear the bit just in case |
| 7279 | */ |
| 7280 | clear_bit(WantReplacement, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7281 | abort: |
| 7282 | |
| 7283 | print_raid5_conf(conf); |
| 7284 | return err; |
| 7285 | } |
| 7286 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7287 | static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7288 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7289 | struct r5conf *conf = mddev->private; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7290 | int err = -EEXIST; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7291 | int disk; |
| 7292 | struct disk_info *p; |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7293 | int first = 0; |
| 7294 | int last = conf->raid_disks - 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7295 | |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7296 | if (test_bit(Journal, &rdev->flags)) { |
| 7297 | char b[BDEVNAME_SIZE]; |
| 7298 | if (conf->log) |
| 7299 | return -EBUSY; |
| 7300 | |
| 7301 | rdev->raid_disk = 0; |
| 7302 | /* |
| 7303 | * The array is in readonly mode if journal is missing, so no |
| 7304 | * write requests running. We should be safe |
| 7305 | */ |
| 7306 | r5l_init_log(conf, rdev); |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7307 | pr_debug("md/raid:%s: using device %s as journal\n", |
| 7308 | mdname(mddev), bdevname(rdev->bdev, b)); |
Shaohua Li | f6b6ec5 | 2015-12-21 10:51:02 +1100 | [diff] [blame] | 7309 | return 0; |
| 7310 | } |
NeilBrown | 7f0da59 | 2011-07-28 11:39:22 +1000 | [diff] [blame] | 7311 | if (mddev->recovery_disabled == conf->recovery_disabled) |
| 7312 | return -EBUSY; |
| 7313 | |
NeilBrown | dc10c64 | 2012-03-19 12:46:37 +1100 | [diff] [blame] | 7314 | if (rdev->saved_raid_disk < 0 && has_failed(conf)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7315 | /* no point adding a device */ |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7316 | return -EINVAL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7317 | |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7318 | if (rdev->raid_disk >= 0) |
| 7319 | first = last = rdev->raid_disk; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7320 | |
| 7321 | /* |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7322 | * find the disk ... but prefer rdev->saved_raid_disk |
| 7323 | * if possible. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7324 | */ |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7325 | if (rdev->saved_raid_disk >= 0 && |
Neil Brown | 6c2fce2 | 2008-06-28 08:31:31 +1000 | [diff] [blame] | 7326 | rdev->saved_raid_disk >= first && |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7327 | conf->disks[rdev->saved_raid_disk].rdev == NULL) |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7328 | first = rdev->saved_raid_disk; |
| 7329 | |
| 7330 | for (disk = first; disk <= last; disk++) { |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7331 | p = conf->disks + disk; |
| 7332 | if (p->rdev == NULL) { |
NeilBrown | b2d444d | 2005-11-08 21:39:31 -0800 | [diff] [blame] | 7333 | clear_bit(In_sync, &rdev->flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7334 | rdev->raid_disk = disk; |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7335 | err = 0; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7336 | if (rdev->saved_raid_disk != disk) |
| 7337 | conf->fullsync = 1; |
Suzanne Wood | d6065f7 | 2005-11-08 21:39:27 -0800 | [diff] [blame] | 7338 | rcu_assign_pointer(p->rdev, rdev); |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7339 | goto out; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7340 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7341 | } |
| 7342 | for (disk = first; disk <= last; disk++) { |
| 7343 | p = conf->disks + disk; |
NeilBrown | 7bfec5f | 2011-12-23 10:17:53 +1100 | [diff] [blame] | 7344 | if (test_bit(WantReplacement, &p->rdev->flags) && |
| 7345 | p->replacement == NULL) { |
| 7346 | clear_bit(In_sync, &rdev->flags); |
| 7347 | set_bit(Replacement, &rdev->flags); |
| 7348 | rdev->raid_disk = disk; |
| 7349 | err = 0; |
| 7350 | conf->fullsync = 1; |
| 7351 | rcu_assign_pointer(p->replacement, rdev); |
| 7352 | break; |
| 7353 | } |
| 7354 | } |
NeilBrown | 5cfb22a | 2012-07-03 11:46:53 +1000 | [diff] [blame] | 7355 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7356 | print_raid5_conf(conf); |
Neil Brown | 199050e | 2008-06-28 08:31:33 +1000 | [diff] [blame] | 7357 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7358 | } |
| 7359 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7360 | static int raid5_resize(struct mddev *mddev, sector_t sectors) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7361 | { |
| 7362 | /* no resync is happening, and there is enough space |
| 7363 | * on all devices, so we can resize. |
| 7364 | * We need to make sure resync covers any new space. |
| 7365 | * If the array is shrinking we should possibly wait until |
| 7366 | * any io in the removed space completes, but it hardly seems |
| 7367 | * worth it. |
| 7368 | */ |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7369 | sector_t newsize; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7370 | struct r5conf *conf = mddev->private; |
| 7371 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7372 | if (conf->log) |
| 7373 | return -EINVAL; |
NeilBrown | 3cb5edf | 2015-07-15 17:24:17 +1000 | [diff] [blame] | 7374 | sectors &= ~((sector_t)conf->chunk_sectors - 1); |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7375 | newsize = raid5_size(mddev, sectors, mddev->raid_disks); |
| 7376 | if (mddev->external_size && |
| 7377 | mddev->array_sectors > newsize) |
Dan Williams | b522adc | 2009-03-31 15:00:31 +1100 | [diff] [blame] | 7378 | return -EINVAL; |
NeilBrown | a4a6125 | 2012-05-22 13:55:27 +1000 | [diff] [blame] | 7379 | if (mddev->bitmap) { |
| 7380 | int ret = bitmap_resize(mddev->bitmap, sectors, 0, 0); |
| 7381 | if (ret) |
| 7382 | return ret; |
| 7383 | } |
| 7384 | md_set_array_sectors(mddev, newsize); |
Andre Noll | f233ea5 | 2008-07-21 17:05:22 +1000 | [diff] [blame] | 7385 | set_capacity(mddev->gendisk, mddev->array_sectors); |
NeilBrown | 449aad3 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7386 | revalidate_disk(mddev->gendisk); |
NeilBrown | b098636 | 2011-05-11 15:52:21 +1000 | [diff] [blame] | 7387 | if (sectors > mddev->dev_sectors && |
| 7388 | mddev->recovery_cp > mddev->dev_sectors) { |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7389 | mddev->recovery_cp = mddev->dev_sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7390 | set_bit(MD_RECOVERY_NEEDED, &mddev->recovery); |
| 7391 | } |
Andre Noll | 58c0fed | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 7392 | mddev->dev_sectors = sectors; |
NeilBrown | 4b5c7ae | 2005-07-27 11:43:28 -0700 | [diff] [blame] | 7393 | mddev->resync_max_sectors = sectors; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7394 | return 0; |
| 7395 | } |
| 7396 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7397 | static int check_stripe_cache(struct mddev *mddev) |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7398 | { |
| 7399 | /* Can only proceed if there are plenty of stripe_heads. |
| 7400 | * We need a minimum of one full stripe,, and for sensible progress |
| 7401 | * it is best to have about 4 times that. |
| 7402 | * If we require 4 times, then the default 256 4K stripe_heads will |
| 7403 | * allow for chunk sizes up to 256K, which is probably OK. |
| 7404 | * If the chunk size is greater, user-space should request more |
| 7405 | * stripe_heads first. |
| 7406 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7407 | struct r5conf *conf = mddev->private; |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7408 | if (((mddev->chunk_sectors << 9) / STRIPE_SIZE) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7409 | > conf->min_nr_stripes || |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7410 | ((mddev->new_chunk_sectors << 9) / STRIPE_SIZE) * 4 |
NeilBrown | edbe83a | 2015-02-26 12:47:56 +1100 | [diff] [blame] | 7411 | > conf->min_nr_stripes) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7412 | pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n", |
| 7413 | mdname(mddev), |
| 7414 | ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9) |
| 7415 | / STRIPE_SIZE)*4); |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7416 | return 0; |
| 7417 | } |
| 7418 | return 1; |
| 7419 | } |
| 7420 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7421 | static int check_reshape(struct mddev *mddev) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7422 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7423 | struct r5conf *conf = mddev->private; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7424 | |
Shaohua Li | 713cf5a | 2015-08-13 14:32:03 -0700 | [diff] [blame] | 7425 | if (conf->log) |
| 7426 | return -EINVAL; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7427 | if (mddev->delta_disks == 0 && |
| 7428 | mddev->new_layout == mddev->layout && |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7429 | mddev->new_chunk_sectors == mddev->chunk_sectors) |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7430 | return 0; /* nothing to do */ |
NeilBrown | 674806d | 2010-06-16 17:17:53 +1000 | [diff] [blame] | 7431 | if (has_failed(conf)) |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7432 | return -EINVAL; |
NeilBrown | fdcfbbb | 2013-07-04 16:38:16 +1000 | [diff] [blame] | 7433 | if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) { |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7434 | /* We might be able to shrink, but the devices must |
| 7435 | * be made bigger first. |
| 7436 | * For raid6, 4 is the minimum size. |
| 7437 | * Otherwise 2 is the minimum |
| 7438 | */ |
| 7439 | int min = 2; |
| 7440 | if (mddev->level == 6) |
| 7441 | min = 4; |
| 7442 | if (mddev->raid_disks + mddev->delta_disks < min) |
| 7443 | return -EINVAL; |
| 7444 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7445 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7446 | if (!check_stripe_cache(mddev)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7447 | return -ENOSPC; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7448 | |
NeilBrown | 738a273 | 2015-05-08 18:19:39 +1000 | [diff] [blame] | 7449 | if (mddev->new_chunk_sectors > mddev->chunk_sectors || |
| 7450 | mddev->delta_disks > 0) |
| 7451 | if (resize_chunks(conf, |
| 7452 | conf->previous_raid_disks |
| 7453 | + max(0, mddev->delta_disks), |
| 7454 | max(mddev->new_chunk_sectors, |
| 7455 | mddev->chunk_sectors) |
| 7456 | ) < 0) |
| 7457 | return -ENOMEM; |
NeilBrown | e56108d6 | 2012-10-11 14:24:13 +1100 | [diff] [blame] | 7458 | return resize_stripes(conf, (conf->previous_raid_disks |
| 7459 | + mddev->delta_disks)); |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7460 | } |
| 7461 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7462 | static int raid5_start_reshape(struct mddev *mddev) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7463 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7464 | struct r5conf *conf = mddev->private; |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7465 | struct md_rdev *rdev; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7466 | int spares = 0; |
NeilBrown | c04be0a | 2006-10-03 01:15:53 -0700 | [diff] [blame] | 7467 | unsigned long flags; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7468 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7469 | if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery)) |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7470 | return -EBUSY; |
| 7471 | |
NeilBrown | 01ee22b | 2009-06-18 08:47:20 +1000 | [diff] [blame] | 7472 | if (!check_stripe_cache(mddev)) |
| 7473 | return -ENOSPC; |
| 7474 | |
NeilBrown | 30b6764 | 2012-05-22 13:55:28 +1000 | [diff] [blame] | 7475 | if (has_failed(conf)) |
| 7476 | return -EINVAL; |
| 7477 | |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7478 | rdev_for_each(rdev, mddev) { |
NeilBrown | 469518a | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7479 | if (!test_bit(In_sync, &rdev->flags) |
| 7480 | && !test_bit(Faulty, &rdev->flags)) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7481 | spares++; |
NeilBrown | c6563a8 | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7482 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7483 | |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7484 | if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7485 | /* Not enough devices even to make a degraded array |
| 7486 | * of that size |
| 7487 | */ |
| 7488 | return -EINVAL; |
| 7489 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7490 | /* Refuse to reduce size of the array. Any reductions in |
| 7491 | * array size must be through explicit setting of array_size |
| 7492 | * attribute. |
| 7493 | */ |
| 7494 | if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks) |
| 7495 | < mddev->array_sectors) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7496 | pr_warn("md/raid:%s: array size must be reduced before number of disks\n", |
| 7497 | mdname(mddev)); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7498 | return -EINVAL; |
| 7499 | } |
| 7500 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7501 | atomic_set(&conf->reshape_stripes, 0); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7502 | spin_lock_irq(&conf->device_lock); |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 7503 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7504 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7505 | conf->raid_disks += mddev->delta_disks; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7506 | conf->prev_chunk_sectors = conf->chunk_sectors; |
| 7507 | conf->chunk_sectors = mddev->new_chunk_sectors; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7508 | conf->prev_algo = conf->algorithm; |
| 7509 | conf->algorithm = mddev->new_layout; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7510 | conf->generation++; |
| 7511 | /* Code that selects data_offset needs to see the generation update |
| 7512 | * if reshape_progress has been set - so a memory barrier needed. |
| 7513 | */ |
| 7514 | smp_mb(); |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7515 | if (mddev->reshape_backwards) |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7516 | conf->reshape_progress = raid5_size(mddev, 0, 0); |
| 7517 | else |
| 7518 | conf->reshape_progress = 0; |
| 7519 | conf->reshape_safe = conf->reshape_progress; |
NeilBrown | c46501b | 2013-08-27 15:52:13 +1000 | [diff] [blame] | 7520 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7521 | spin_unlock_irq(&conf->device_lock); |
| 7522 | |
NeilBrown | 4d77e3b | 2013-08-27 15:57:47 +1000 | [diff] [blame] | 7523 | /* Now make sure any requests that proceeded on the assumption |
| 7524 | * the reshape wasn't running - like Discard or Read - have |
| 7525 | * completed. |
| 7526 | */ |
| 7527 | mddev_suspend(mddev); |
| 7528 | mddev_resume(mddev); |
| 7529 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7530 | /* Add some new drives, as many as will fit. |
| 7531 | * We know there are enough to make the newly sized array work. |
NeilBrown | 3424bf6 | 2010-06-17 17:48:26 +1000 | [diff] [blame] | 7532 | * Don't add devices if we are reducing the number of |
| 7533 | * devices in the array. This is because it is not possible |
| 7534 | * to correctly record the "partially reconstructed" state of |
| 7535 | * such devices during the reshape and confusion could result. |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7536 | */ |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7537 | if (mddev->delta_disks >= 0) { |
NeilBrown | dafb20f | 2012-03-19 12:46:39 +1100 | [diff] [blame] | 7538 | rdev_for_each(rdev, mddev) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7539 | if (rdev->raid_disk < 0 && |
| 7540 | !test_bit(Faulty, &rdev->flags)) { |
| 7541 | if (raid5_add_disk(mddev, rdev) == 0) { |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7542 | if (rdev->raid_disk |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 7543 | >= conf->previous_raid_disks) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7544 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 9d4c7d8 | 2012-03-13 11:21:21 +1100 | [diff] [blame] | 7545 | else |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7546 | rdev->recovery_offset = 0; |
Namhyung Kim | 36fad85 | 2011-07-27 11:00:36 +1000 | [diff] [blame] | 7547 | |
| 7548 | if (sysfs_link_rdev(mddev, rdev)) |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7549 | /* Failure here is OK */; |
NeilBrown | 50da084 | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7550 | } |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7551 | } else if (rdev->raid_disk >= conf->previous_raid_disks |
| 7552 | && !test_bit(Faulty, &rdev->flags)) { |
| 7553 | /* This is a spare that was manually added */ |
| 7554 | set_bit(In_sync, &rdev->flags); |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7555 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7556 | |
NeilBrown | 87a8dec | 2011-01-31 11:57:43 +1100 | [diff] [blame] | 7557 | /* When a reshape changes the number of devices, |
| 7558 | * ->degraded is measured against the larger of the |
| 7559 | * pre and post number of devices. |
| 7560 | */ |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7561 | spin_lock_irqsave(&conf->device_lock, flags); |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 7562 | mddev->degraded = calc_degraded(conf); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7563 | spin_unlock_irqrestore(&conf->device_lock, flags); |
| 7564 | } |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7565 | mddev->raid_disks = conf->raid_disks; |
NeilBrown | e516402 | 2009-08-03 10:59:57 +1000 | [diff] [blame] | 7566 | mddev->reshape_position = conf->reshape_progress; |
NeilBrown | 850b2b4 | 2006-10-03 01:15:46 -0700 | [diff] [blame] | 7567 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7568 | |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7569 | clear_bit(MD_RECOVERY_SYNC, &mddev->recovery); |
| 7570 | clear_bit(MD_RECOVERY_CHECK, &mddev->recovery); |
NeilBrown | ea358cd | 2015-06-12 20:05:04 +1000 | [diff] [blame] | 7571 | clear_bit(MD_RECOVERY_DONE, &mddev->recovery); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7572 | set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery); |
| 7573 | set_bit(MD_RECOVERY_RUNNING, &mddev->recovery); |
| 7574 | mddev->sync_thread = md_register_thread(md_do_sync, mddev, |
NeilBrown | 0da3c61 | 2009-09-23 18:09:45 +1000 | [diff] [blame] | 7575 | "reshape"); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7576 | if (!mddev->sync_thread) { |
| 7577 | mddev->recovery = 0; |
| 7578 | spin_lock_irq(&conf->device_lock); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7579 | write_seqcount_begin(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7580 | mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7581 | mddev->new_chunk_sectors = |
| 7582 | conf->chunk_sectors = conf->prev_chunk_sectors; |
| 7583 | mddev->new_layout = conf->algorithm = conf->prev_algo; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7584 | rdev_for_each(rdev, mddev) |
| 7585 | rdev->new_data_offset = rdev->data_offset; |
| 7586 | smp_wmb(); |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7587 | conf->generation --; |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7588 | conf->reshape_progress = MaxSector; |
NeilBrown | 1e3fa9b | 2012-03-13 11:21:18 +1100 | [diff] [blame] | 7589 | mddev->reshape_position = MaxSector; |
NeilBrown | ba8805b | 2013-11-14 15:16:15 +1100 | [diff] [blame] | 7590 | write_seqcount_end(&conf->gen_lock); |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7591 | spin_unlock_irq(&conf->device_lock); |
| 7592 | return -EAGAIN; |
| 7593 | } |
NeilBrown | c8f517c | 2009-03-31 15:28:40 +1100 | [diff] [blame] | 7594 | conf->reshape_checkpoint = jiffies; |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7595 | md_wakeup_thread(mddev->sync_thread); |
| 7596 | md_new_event(mddev); |
| 7597 | return 0; |
| 7598 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7599 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7600 | /* This is called from the reshape thread and should make any |
| 7601 | * changes needed in 'conf' |
| 7602 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7603 | static void end_reshape(struct r5conf *conf) |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7604 | { |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7605 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7606 | if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) { |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7607 | struct md_rdev *rdev; |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7608 | |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7609 | spin_lock_irq(&conf->device_lock); |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7610 | conf->previous_raid_disks = conf->raid_disks; |
NeilBrown | 05616be | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7611 | rdev_for_each(rdev, conf->mddev) |
| 7612 | rdev->data_offset = rdev->new_data_offset; |
| 7613 | smp_wmb(); |
NeilBrown | fef9c61 | 2009-03-31 15:16:46 +1100 | [diff] [blame] | 7614 | conf->reshape_progress = MaxSector; |
NeilBrown | 6cbd814 | 2015-07-24 13:30:32 +1000 | [diff] [blame] | 7615 | conf->mddev->reshape_position = MaxSector; |
NeilBrown | f670557 | 2006-03-27 01:18:11 -0800 | [diff] [blame] | 7616 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | b0f9ec0 | 2009-03-31 15:27:18 +1100 | [diff] [blame] | 7617 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7618 | |
| 7619 | /* read-ahead size must cover two whole stripes, which is |
| 7620 | * 2 * (datadisks) * chunksize where 'n' is the number of raid devices |
| 7621 | */ |
NeilBrown | 4a5add4 | 2010-06-01 19:37:28 +1000 | [diff] [blame] | 7622 | if (conf->mddev->queue) { |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7623 | int data_disks = conf->raid_disks - conf->max_degraded; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7624 | int stripe = data_disks * ((conf->chunk_sectors << 9) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7625 | / PAGE_SIZE); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7626 | if (conf->mddev->queue->backing_dev_info.ra_pages < 2 * stripe) |
| 7627 | conf->mddev->queue->backing_dev_info.ra_pages = 2 * stripe; |
| 7628 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7629 | } |
NeilBrown | 2926955 | 2006-03-27 01:18:10 -0800 | [diff] [blame] | 7630 | } |
| 7631 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7632 | /* This is called from the raid5d thread with mddev_lock held. |
| 7633 | * It makes config changes to the device. |
| 7634 | */ |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7635 | static void raid5_finish_reshape(struct mddev *mddev) |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7636 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7637 | struct r5conf *conf = mddev->private; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7638 | |
| 7639 | if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) { |
| 7640 | |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7641 | if (mddev->delta_disks > 0) { |
| 7642 | md_set_array_sectors(mddev, raid5_size(mddev, 0, 0)); |
Heinz Mauelshagen | fe67d19 | 2016-05-03 22:15:31 +0200 | [diff] [blame] | 7643 | if (mddev->queue) { |
| 7644 | set_capacity(mddev->gendisk, mddev->array_sectors); |
| 7645 | revalidate_disk(mddev->gendisk); |
| 7646 | } |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7647 | } else { |
| 7648 | int d; |
NeilBrown | 908f4fb | 2011-12-23 10:17:50 +1100 | [diff] [blame] | 7649 | spin_lock_irq(&conf->device_lock); |
| 7650 | mddev->degraded = calc_degraded(conf); |
| 7651 | spin_unlock_irq(&conf->device_lock); |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7652 | for (d = conf->raid_disks ; |
| 7653 | d < conf->raid_disks - mddev->delta_disks; |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 7654 | d++) { |
NeilBrown | 3cb0300 | 2011-10-11 16:45:26 +1100 | [diff] [blame] | 7655 | struct md_rdev *rdev = conf->disks[d].rdev; |
NeilBrown | da7613b | 2012-05-22 13:55:33 +1000 | [diff] [blame] | 7656 | if (rdev) |
| 7657 | clear_bit(In_sync, &rdev->flags); |
| 7658 | rdev = conf->disks[d].replacement; |
| 7659 | if (rdev) |
| 7660 | clear_bit(In_sync, &rdev->flags); |
NeilBrown | 1a67dde | 2009-08-13 10:41:49 +1000 | [diff] [blame] | 7661 | } |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7662 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7663 | mddev->layout = conf->algorithm; |
Andre Noll | 09c9e5f | 2009-06-18 08:45:55 +1000 | [diff] [blame] | 7664 | mddev->chunk_sectors = conf->chunk_sectors; |
NeilBrown | ec32a2b | 2009-03-31 15:17:38 +1100 | [diff] [blame] | 7665 | mddev->reshape_position = MaxSector; |
| 7666 | mddev->delta_disks = 0; |
NeilBrown | 2c810cd | 2012-05-21 09:27:00 +1000 | [diff] [blame] | 7667 | mddev->reshape_backwards = 0; |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7668 | } |
| 7669 | } |
| 7670 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7671 | static void raid5_quiesce(struct mddev *mddev, int state) |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7672 | { |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7673 | struct r5conf *conf = mddev->private; |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7674 | |
| 7675 | switch(state) { |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 7676 | case 2: /* resume for a suspend */ |
| 7677 | wake_up(&conf->wait_for_overlap); |
| 7678 | break; |
| 7679 | |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7680 | case 1: /* stop all writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7681 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7682 | /* '2' tells resync/reshape to pause so that all |
| 7683 | * active stripes can drain |
| 7684 | */ |
Song Liu | a39f7af | 2016-11-17 15:24:40 -0800 | [diff] [blame] | 7685 | r5c_flush_cache(conf, INT_MAX); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7686 | conf->quiesce = 2; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 7687 | wait_event_cmd(conf->wait_for_quiescent, |
Raz Ben-Jehuda(caro) | 46031f9 | 2006-12-10 02:20:47 -0800 | [diff] [blame] | 7688 | atomic_read(&conf->active_stripes) == 0 && |
| 7689 | atomic_read(&conf->active_aligned_reads) == 0, |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7690 | unlock_all_device_hash_locks_irq(conf), |
| 7691 | lock_all_device_hash_locks_irq(conf)); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7692 | conf->quiesce = 1; |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7693 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 64bd660 | 2009-08-03 10:59:58 +1000 | [diff] [blame] | 7694 | /* allow reshape to continue */ |
| 7695 | wake_up(&conf->wait_for_overlap); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7696 | break; |
| 7697 | |
| 7698 | case 0: /* re-enable writes */ |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7699 | lock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7700 | conf->quiesce = 0; |
Yuanhan Liu | b1b4648 | 2015-05-08 18:19:06 +1000 | [diff] [blame] | 7701 | wake_up(&conf->wait_for_quiescent); |
NeilBrown | e464eaf | 2006-03-27 01:18:14 -0800 | [diff] [blame] | 7702 | wake_up(&conf->wait_for_overlap); |
Shaohua Li | 566c09c | 2013-11-14 15:16:17 +1100 | [diff] [blame] | 7703 | unlock_all_device_hash_locks_irq(conf); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7704 | break; |
| 7705 | } |
Shaohua Li | e6c033f | 2015-10-04 09:20:12 -0700 | [diff] [blame] | 7706 | r5l_quiesce(conf->log, state); |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7707 | } |
NeilBrown | b15c2e5 | 2006-01-06 00:20:16 -0800 | [diff] [blame] | 7708 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7709 | static void *raid45_takeover_raid0(struct mddev *mddev, int level) |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 7710 | { |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 7711 | struct r0conf *raid0_conf = mddev->private; |
Randy Dunlap | d76c842 | 2011-04-21 09:07:26 -0700 | [diff] [blame] | 7712 | sector_t sectors; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 7713 | |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7714 | /* for raid0 takeover only one zone is supported */ |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 7715 | if (raid0_conf->nr_strip_zones > 1) { |
NeilBrown | cc6167b | 2016-11-02 14:16:50 +1100 | [diff] [blame] | 7716 | pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n", |
| 7717 | mdname(mddev)); |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7718 | return ERR_PTR(-EINVAL); |
| 7719 | } |
| 7720 | |
NeilBrown | e373ab1 | 2011-10-11 16:48:59 +1100 | [diff] [blame] | 7721 | sectors = raid0_conf->strip_zone[0].zone_end; |
| 7722 | sector_div(sectors, raid0_conf->strip_zone[0].nb_dev); |
NeilBrown | 3b71bd9 | 2011-04-20 15:38:18 +1000 | [diff] [blame] | 7723 | mddev->dev_sectors = sectors; |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7724 | mddev->new_level = level; |
Trela Maciej | 54071b3 | 2010-03-08 16:02:42 +1100 | [diff] [blame] | 7725 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 7726 | mddev->new_chunk_sectors = mddev->chunk_sectors; |
| 7727 | mddev->raid_disks += 1; |
| 7728 | mddev->delta_disks = 1; |
| 7729 | /* make sure it will be not marked as dirty */ |
| 7730 | mddev->recovery_cp = MaxSector; |
| 7731 | |
| 7732 | return setup_conf(mddev); |
| 7733 | } |
| 7734 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7735 | static void *raid5_takeover_raid1(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7736 | { |
| 7737 | int chunksect; |
| 7738 | |
| 7739 | if (mddev->raid_disks != 2 || |
| 7740 | mddev->degraded > 1) |
| 7741 | return ERR_PTR(-EINVAL); |
| 7742 | |
| 7743 | /* Should check if there are write-behind devices? */ |
| 7744 | |
| 7745 | chunksect = 64*2; /* 64K by default */ |
| 7746 | |
| 7747 | /* The array must be an exact multiple of chunksize */ |
| 7748 | while (chunksect && (mddev->array_sectors & (chunksect-1))) |
| 7749 | chunksect >>= 1; |
| 7750 | |
| 7751 | if ((chunksect<<9) < STRIPE_SIZE) |
| 7752 | /* array size does not allow a suitable chunk size */ |
| 7753 | return ERR_PTR(-EINVAL); |
| 7754 | |
| 7755 | mddev->new_level = 5; |
| 7756 | mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC; |
Andre Noll | 664e7c4 | 2009-06-18 08:45:27 +1000 | [diff] [blame] | 7757 | mddev->new_chunk_sectors = chunksect; |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7758 | |
| 7759 | return setup_conf(mddev); |
| 7760 | } |
| 7761 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7762 | static void *raid5_takeover_raid6(struct mddev *mddev) |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 7763 | { |
| 7764 | int new_layout; |
| 7765 | |
| 7766 | switch (mddev->layout) { |
| 7767 | case ALGORITHM_LEFT_ASYMMETRIC_6: |
| 7768 | new_layout = ALGORITHM_LEFT_ASYMMETRIC; |
| 7769 | break; |
| 7770 | case ALGORITHM_RIGHT_ASYMMETRIC_6: |
| 7771 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC; |
| 7772 | break; |
| 7773 | case ALGORITHM_LEFT_SYMMETRIC_6: |
| 7774 | new_layout = ALGORITHM_LEFT_SYMMETRIC; |
| 7775 | break; |
| 7776 | case ALGORITHM_RIGHT_SYMMETRIC_6: |
| 7777 | new_layout = ALGORITHM_RIGHT_SYMMETRIC; |
| 7778 | break; |
| 7779 | case ALGORITHM_PARITY_0_6: |
| 7780 | new_layout = ALGORITHM_PARITY_0; |
| 7781 | break; |
| 7782 | case ALGORITHM_PARITY_N: |
| 7783 | new_layout = ALGORITHM_PARITY_N; |
| 7784 | break; |
| 7785 | default: |
| 7786 | return ERR_PTR(-EINVAL); |
| 7787 | } |
| 7788 | mddev->new_level = 5; |
| 7789 | mddev->new_layout = new_layout; |
| 7790 | mddev->delta_disks = -1; |
| 7791 | mddev->raid_disks -= 1; |
| 7792 | return setup_conf(mddev); |
| 7793 | } |
| 7794 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7795 | static int raid5_check_reshape(struct mddev *mddev) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7796 | { |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7797 | /* For a 2-drive array, the layout and chunk size can be changed |
| 7798 | * immediately as not restriping is needed. |
| 7799 | * For larger arrays we record the new value - after validation |
| 7800 | * to be used by a reshape pass. |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7801 | */ |
NeilBrown | d1688a6 | 2011-10-11 16:49:52 +1100 | [diff] [blame] | 7802 | struct r5conf *conf = mddev->private; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7803 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7804 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7805 | if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7806 | return -EINVAL; |
| 7807 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 7808 | if (!is_power_of_2(new_chunk)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7809 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7810 | if (new_chunk < (PAGE_SIZE>>9)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7811 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7812 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7813 | /* not factor of array size */ |
| 7814 | return -EINVAL; |
| 7815 | } |
| 7816 | |
| 7817 | /* They look valid */ |
| 7818 | |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7819 | if (mddev->raid_disks == 2) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7820 | /* can make the change immediately */ |
| 7821 | if (mddev->new_layout >= 0) { |
| 7822 | conf->algorithm = mddev->new_layout; |
| 7823 | mddev->layout = mddev->new_layout; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7824 | } |
| 7825 | if (new_chunk > 0) { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7826 | conf->chunk_sectors = new_chunk ; |
| 7827 | mddev->chunk_sectors = new_chunk; |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7828 | } |
| 7829 | set_bit(MD_CHANGE_DEVS, &mddev->flags); |
| 7830 | md_wakeup_thread(mddev->thread); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7831 | } |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7832 | return check_reshape(mddev); |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7833 | } |
| 7834 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7835 | static int raid6_check_reshape(struct mddev *mddev) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7836 | { |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7837 | int new_chunk = mddev->new_chunk_sectors; |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7838 | |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7839 | if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7840 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7841 | if (new_chunk > 0) { |
Andre Noll | 0ba459d | 2009-06-18 08:46:10 +1000 | [diff] [blame] | 7842 | if (!is_power_of_2(new_chunk)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7843 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7844 | if (new_chunk < (PAGE_SIZE >> 9)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7845 | return -EINVAL; |
NeilBrown | 597a711 | 2009-06-18 08:47:42 +1000 | [diff] [blame] | 7846 | if (mddev->array_sectors & (new_chunk-1)) |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7847 | /* not factor of array size */ |
| 7848 | return -EINVAL; |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7849 | } |
NeilBrown | 88ce493 | 2009-03-31 15:24:23 +1100 | [diff] [blame] | 7850 | |
| 7851 | /* They look valid */ |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7852 | return check_reshape(mddev); |
NeilBrown | b354603 | 2009-03-31 14:56:41 +1100 | [diff] [blame] | 7853 | } |
| 7854 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7855 | static void *raid5_takeover(struct mddev *mddev) |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7856 | { |
| 7857 | /* raid5 can take over: |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7858 | * raid0 - if there is only one strip zone - make it a raid4 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7859 | * raid1 - if there are two drives. We need to know the chunk size |
| 7860 | * raid4 - trivial - just use a raid4 layout. |
| 7861 | * raid6 - Providing it is a *_6 layout |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7862 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7863 | if (mddev->level == 0) |
| 7864 | return raid45_takeover_raid0(mddev, 5); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7865 | if (mddev->level == 1) |
| 7866 | return raid5_takeover_raid1(mddev); |
NeilBrown | e9d4758 | 2009-03-31 14:57:09 +1100 | [diff] [blame] | 7867 | if (mddev->level == 4) { |
| 7868 | mddev->new_layout = ALGORITHM_PARITY_N; |
| 7869 | mddev->new_level = 5; |
| 7870 | return setup_conf(mddev); |
| 7871 | } |
NeilBrown | fc9739c | 2009-03-31 14:57:20 +1100 | [diff] [blame] | 7872 | if (mddev->level == 6) |
| 7873 | return raid5_takeover_raid6(mddev); |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7874 | |
| 7875 | return ERR_PTR(-EINVAL); |
| 7876 | } |
| 7877 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7878 | static void *raid4_takeover(struct mddev *mddev) |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 7879 | { |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7880 | /* raid4 can take over: |
| 7881 | * raid0 - if there is only one strip zone |
| 7882 | * raid5 - if layout is right |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 7883 | */ |
Dan Williams | f1b29bc | 2010-05-01 18:09:05 -0700 | [diff] [blame] | 7884 | if (mddev->level == 0) |
| 7885 | return raid45_takeover_raid0(mddev, 4); |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 7886 | if (mddev->level == 5 && |
| 7887 | mddev->layout == ALGORITHM_PARITY_N) { |
| 7888 | mddev->new_layout = 0; |
| 7889 | mddev->new_level = 4; |
| 7890 | return setup_conf(mddev); |
| 7891 | } |
| 7892 | return ERR_PTR(-EINVAL); |
| 7893 | } |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7894 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7895 | static struct md_personality raid5_personality; |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7896 | |
NeilBrown | fd01b88 | 2011-10-11 16:47:53 +1100 | [diff] [blame] | 7897 | static void *raid6_takeover(struct mddev *mddev) |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7898 | { |
| 7899 | /* Currently can only take over a raid5. We map the |
| 7900 | * personality to an equivalent raid6 personality |
| 7901 | * with the Q block at the end. |
| 7902 | */ |
| 7903 | int new_layout; |
| 7904 | |
| 7905 | if (mddev->pers != &raid5_personality) |
| 7906 | return ERR_PTR(-EINVAL); |
| 7907 | if (mddev->degraded > 1) |
| 7908 | return ERR_PTR(-EINVAL); |
| 7909 | if (mddev->raid_disks > 253) |
| 7910 | return ERR_PTR(-EINVAL); |
| 7911 | if (mddev->raid_disks < 3) |
| 7912 | return ERR_PTR(-EINVAL); |
| 7913 | |
| 7914 | switch (mddev->layout) { |
| 7915 | case ALGORITHM_LEFT_ASYMMETRIC: |
| 7916 | new_layout = ALGORITHM_LEFT_ASYMMETRIC_6; |
| 7917 | break; |
| 7918 | case ALGORITHM_RIGHT_ASYMMETRIC: |
| 7919 | new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6; |
| 7920 | break; |
| 7921 | case ALGORITHM_LEFT_SYMMETRIC: |
| 7922 | new_layout = ALGORITHM_LEFT_SYMMETRIC_6; |
| 7923 | break; |
| 7924 | case ALGORITHM_RIGHT_SYMMETRIC: |
| 7925 | new_layout = ALGORITHM_RIGHT_SYMMETRIC_6; |
| 7926 | break; |
| 7927 | case ALGORITHM_PARITY_0: |
| 7928 | new_layout = ALGORITHM_PARITY_0_6; |
| 7929 | break; |
| 7930 | case ALGORITHM_PARITY_N: |
| 7931 | new_layout = ALGORITHM_PARITY_N; |
| 7932 | break; |
| 7933 | default: |
| 7934 | return ERR_PTR(-EINVAL); |
| 7935 | } |
| 7936 | mddev->new_level = 6; |
| 7937 | mddev->new_layout = new_layout; |
| 7938 | mddev->delta_disks = 1; |
| 7939 | mddev->raid_disks += 1; |
| 7940 | return setup_conf(mddev); |
| 7941 | } |
| 7942 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7943 | static struct md_personality raid6_personality = |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7944 | { |
| 7945 | .name = "raid6", |
| 7946 | .level = 6, |
| 7947 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7948 | .make_request = raid5_make_request, |
| 7949 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7950 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7951 | .status = raid5_status, |
| 7952 | .error_handler = raid5_error, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7953 | .hot_add_disk = raid5_add_disk, |
| 7954 | .hot_remove_disk= raid5_remove_disk, |
| 7955 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7956 | .sync_request = raid5_sync_request, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7957 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7958 | .size = raid5_size, |
NeilBrown | 50ac168 | 2009-06-18 08:47:55 +1000 | [diff] [blame] | 7959 | .check_reshape = raid6_check_reshape, |
NeilBrown | f416885 | 2007-02-28 20:11:53 -0800 | [diff] [blame] | 7960 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7961 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7962 | .quiesce = raid5_quiesce, |
NeilBrown | 245f46c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7963 | .takeover = raid6_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7964 | .congested = raid5_congested, |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 7965 | }; |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7966 | static struct md_personality raid5_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7967 | { |
| 7968 | .name = "raid5", |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7969 | .level = 5, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7970 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7971 | .make_request = raid5_make_request, |
| 7972 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7973 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7974 | .status = raid5_status, |
| 7975 | .error_handler = raid5_error, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7976 | .hot_add_disk = raid5_add_disk, |
| 7977 | .hot_remove_disk= raid5_remove_disk, |
| 7978 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7979 | .sync_request = raid5_sync_request, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7980 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 7981 | .size = raid5_size, |
NeilBrown | 63c70c4 | 2006-03-27 01:18:13 -0800 | [diff] [blame] | 7982 | .check_reshape = raid5_check_reshape, |
| 7983 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 7984 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 7262668 | 2005-09-09 16:23:54 -0700 | [diff] [blame] | 7985 | .quiesce = raid5_quiesce, |
NeilBrown | d562b0c | 2009-03-31 14:39:39 +1100 | [diff] [blame] | 7986 | .takeover = raid5_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 7987 | .congested = raid5_congested, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7988 | }; |
| 7989 | |
NeilBrown | 84fc4b5 | 2011-10-11 16:49:58 +1100 | [diff] [blame] | 7990 | static struct md_personality raid4_personality = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7991 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 7992 | .name = "raid4", |
| 7993 | .level = 4, |
| 7994 | .owner = THIS_MODULE, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7995 | .make_request = raid5_make_request, |
| 7996 | .run = raid5_run, |
NeilBrown | afa0f55 | 2014-12-15 12:56:58 +1100 | [diff] [blame] | 7997 | .free = raid5_free, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 7998 | .status = raid5_status, |
| 7999 | .error_handler = raid5_error, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8000 | .hot_add_disk = raid5_add_disk, |
| 8001 | .hot_remove_disk= raid5_remove_disk, |
| 8002 | .spare_active = raid5_spare_active, |
Shaohua Li | 849674e | 2016-01-20 13:52:20 -0800 | [diff] [blame] | 8003 | .sync_request = raid5_sync_request, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8004 | .resize = raid5_resize, |
Dan Williams | 80c3a6c | 2009-03-17 18:10:40 -0700 | [diff] [blame] | 8005 | .size = raid5_size, |
NeilBrown | 3d37890 | 2007-03-26 21:32:13 -0800 | [diff] [blame] | 8006 | .check_reshape = raid5_check_reshape, |
| 8007 | .start_reshape = raid5_start_reshape, |
NeilBrown | cea9c22 | 2009-03-31 15:15:05 +1100 | [diff] [blame] | 8008 | .finish_reshape = raid5_finish_reshape, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8009 | .quiesce = raid5_quiesce, |
NeilBrown | a78d38a | 2010-03-22 16:53:49 +1100 | [diff] [blame] | 8010 | .takeover = raid4_takeover, |
NeilBrown | 5c675f8 | 2014-12-15 12:56:56 +1100 | [diff] [blame] | 8011 | .congested = raid5_congested, |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8012 | }; |
| 8013 | |
| 8014 | static int __init raid5_init(void) |
| 8015 | { |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8016 | int ret; |
| 8017 | |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8018 | raid5_wq = alloc_workqueue("raid5wq", |
| 8019 | WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0); |
| 8020 | if (!raid5_wq) |
| 8021 | return -ENOMEM; |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8022 | |
| 8023 | ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE, |
| 8024 | "md/raid5:prepare", |
| 8025 | raid456_cpu_up_prepare, |
| 8026 | raid456_cpu_dead); |
| 8027 | if (ret) { |
| 8028 | destroy_workqueue(raid5_wq); |
| 8029 | return ret; |
| 8030 | } |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8031 | register_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8032 | register_md_personality(&raid5_personality); |
| 8033 | register_md_personality(&raid4_personality); |
| 8034 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8035 | } |
| 8036 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8037 | static void raid5_exit(void) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8038 | { |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8039 | unregister_md_personality(&raid6_personality); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8040 | unregister_md_personality(&raid5_personality); |
| 8041 | unregister_md_personality(&raid4_personality); |
Sebastian Andrzej Siewior | 29c6d1b | 2016-08-18 14:57:24 +0200 | [diff] [blame] | 8042 | cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE); |
Shaohua Li | 851c30c | 2013-08-28 14:30:16 +0800 | [diff] [blame] | 8043 | destroy_workqueue(raid5_wq); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8044 | } |
| 8045 | |
| 8046 | module_init(raid5_init); |
| 8047 | module_exit(raid5_exit); |
| 8048 | MODULE_LICENSE("GPL"); |
NeilBrown | 0efb9e6 | 2009-12-14 12:49:58 +1100 | [diff] [blame] | 8049 | MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8050 | MODULE_ALIAS("md-personality-4"); /* RAID5 */ |
NeilBrown | d9d166c | 2006-01-06 00:20:51 -0800 | [diff] [blame] | 8051 | MODULE_ALIAS("md-raid5"); |
| 8052 | MODULE_ALIAS("md-raid4"); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 8053 | MODULE_ALIAS("md-level-5"); |
| 8054 | MODULE_ALIAS("md-level-4"); |
NeilBrown | 16a53ec | 2006-06-26 00:27:38 -0700 | [diff] [blame] | 8055 | MODULE_ALIAS("md-personality-8"); /* RAID6 */ |
| 8056 | MODULE_ALIAS("md-raid6"); |
| 8057 | MODULE_ALIAS("md-level-6"); |
| 8058 | |
| 8059 | /* This used to be two separate modules, they were: */ |
| 8060 | MODULE_ALIAS("raid5"); |
| 8061 | MODULE_ALIAS("raid6"); |