blob: 7988d373f18be85d557f07b1d90d5de0bc94dcce [file] [log] [blame]
Thomas Gleixneraf1a8892019-05-20 19:08:12 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * raid5.c : Multiple Devices driver for Linux
4 * Copyright (C) 1996, 1997 Ingo Molnar, Miguel de Icaza, Gadi Oxman
5 * Copyright (C) 1999, 2000 Ingo Molnar
NeilBrown16a53ec2006-06-26 00:27:38 -07006 * Copyright (C) 2002, 2003 H. Peter Anvin
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
NeilBrown16a53ec2006-06-26 00:27:38 -07008 * RAID-4/5/6 management functions.
9 * Thanks to Penguin Computing for making the RAID-6 development possible
10 * by donating a test server!
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 */
12
NeilBrownae3c20c2006-07-10 04:44:17 -070013/*
14 * BITMAP UNPLUGGING:
15 *
16 * The sequencing for updating the bitmap reliably is a little
17 * subtle (and I got it wrong the first time) so it deserves some
18 * explanation.
19 *
20 * We group bitmap updates into batches. Each batch has a number.
21 * We may write out several batches at once, but that isn't very important.
NeilBrown7c13edc2011-04-18 18:25:43 +100022 * conf->seq_write is the number of the last batch successfully written.
23 * conf->seq_flush is the number of the last batch that was closed to
NeilBrownae3c20c2006-07-10 04:44:17 -070024 * new additions.
25 * When we discover that we will need to write to any block in a stripe
26 * (in add_stripe_bio) we update the in-memory bitmap and record in sh->bm_seq
NeilBrown7c13edc2011-04-18 18:25:43 +100027 * the number of the batch it will be in. This is seq_flush+1.
NeilBrownae3c20c2006-07-10 04:44:17 -070028 * When we are ready to do a write, if that batch hasn't been written yet,
29 * we plug the array and queue the stripe for later.
30 * When an unplug happens, we increment bm_flush, thus closing the current
31 * batch.
32 * When we notice that bm_flush > bm_write, we write out all pending updates
33 * to the bitmap, and advance bm_write to where bm_flush was.
34 * This may occasionally write a bit out twice, but is sure never to
35 * miss any bits.
36 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
NeilBrownbff61972009-03-31 14:33:13 +110038#include <linux/blkdev.h>
NeilBrownf6705572006-03-27 01:18:11 -080039#include <linux/kthread.h>
Dan Williamsf701d582009-03-31 15:09:39 +110040#include <linux/raid/pq.h>
Dan Williams91c00922007-01-02 13:52:30 -070041#include <linux/async_tx.h>
Paul Gortmaker056075c2011-07-03 13:58:33 -040042#include <linux/module.h>
Dan Williams07a3b412009-08-29 19:13:13 -070043#include <linux/async.h>
NeilBrownbff61972009-03-31 14:33:13 +110044#include <linux/seq_file.h>
Dan Williams36d1c642009-07-14 11:48:22 -070045#include <linux/cpu.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090046#include <linux/slab.h>
Christian Dietrich8bda4702011-07-27 11:00:36 +100047#include <linux/ratelimit.h>
Shaohua Li851c30c2013-08-28 14:30:16 +080048#include <linux/nodemask.h>
Ingo Molnar3f07c012017-02-08 18:51:30 +010049
NeilBrowna9add5d2012-10-31 11:59:09 +110050#include <trace/events/block.h>
Shaohua Liaaf9f122017-03-03 22:06:12 -080051#include <linux/list_sort.h>
NeilBrowna9add5d2012-10-31 11:59:09 +110052
NeilBrown43b2e5d2009-03-31 14:33:13 +110053#include "md.h"
NeilBrownbff61972009-03-31 14:33:13 +110054#include "raid5.h"
Trela Maciej54071b32010-03-08 16:02:42 +110055#include "raid0.h"
Mike Snitzer935fe092017-10-10 17:02:41 -040056#include "md-bitmap.h"
Artur Paszkiewiczff875732017-03-09 09:59:58 +010057#include "raid5-log.h"
NeilBrown72626682005-09-09 16:23:54 -070058
Shaohua Li394ed8e2017-01-04 16:10:19 -080059#define UNSUPPORTED_MDDEV_FLAGS (1L << MD_FAILFAST_SUPPORTED)
60
Shaohua Li851c30c2013-08-28 14:30:16 +080061#define cpu_to_group(cpu) cpu_to_node(cpu)
62#define ANY_GROUP NUMA_NO_NODE
63
NeilBrown8e0e99b2014-10-02 13:45:00 +100064static bool devices_handle_discard_safely = false;
65module_param(devices_handle_discard_safely, bool, 0644);
66MODULE_PARM_DESC(devices_handle_discard_safely,
67 "Set to Y if all devices in each array reliably return zeroes on reads from discarded regions");
Shaohua Li851c30c2013-08-28 14:30:16 +080068static struct workqueue_struct *raid5_wq;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
NeilBrownd1688a62011-10-11 16:49:52 +110070static inline struct hlist_head *stripe_hash(struct r5conf *conf, sector_t sect)
NeilBrowndb298e12011-10-07 14:23:00 +110071{
Yufen Yuc911c462020-07-18 05:29:07 -040072 int hash = (sect >> RAID5_STRIPE_SHIFT(conf)) & HASH_MASK;
NeilBrowndb298e12011-10-07 14:23:00 +110073 return &conf->stripe_hashtbl[hash];
74}
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
Yufen Yuc911c462020-07-18 05:29:07 -040076static inline int stripe_hash_locks_hash(struct r5conf *conf, sector_t sect)
Shaohua Li566c09c2013-11-14 15:16:17 +110077{
Yufen Yuc911c462020-07-18 05:29:07 -040078 return (sect >> RAID5_STRIPE_SHIFT(conf)) & STRIPE_HASH_LOCKS_MASK;
Shaohua Li566c09c2013-11-14 15:16:17 +110079}
80
81static inline void lock_device_hash_lock(struct r5conf *conf, int hash)
82{
83 spin_lock_irq(conf->hash_locks + hash);
84 spin_lock(&conf->device_lock);
85}
86
87static inline void unlock_device_hash_lock(struct r5conf *conf, int hash)
88{
89 spin_unlock(&conf->device_lock);
90 spin_unlock_irq(conf->hash_locks + hash);
91}
92
93static inline void lock_all_device_hash_locks_irq(struct r5conf *conf)
94{
95 int i;
Julia Cartwright3d05f3a2017-04-28 12:41:02 -050096 spin_lock_irq(conf->hash_locks);
Shaohua Li566c09c2013-11-14 15:16:17 +110097 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
98 spin_lock_nest_lock(conf->hash_locks + i, conf->hash_locks);
99 spin_lock(&conf->device_lock);
100}
101
102static inline void unlock_all_device_hash_locks_irq(struct r5conf *conf)
103{
104 int i;
105 spin_unlock(&conf->device_lock);
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500106 for (i = NR_STRIPE_HASH_LOCKS - 1; i; i--)
107 spin_unlock(conf->hash_locks + i);
108 spin_unlock_irq(conf->hash_locks);
Shaohua Li566c09c2013-11-14 15:16:17 +1100109}
110
NeilBrownd0dabf72009-03-31 14:39:38 +1100111/* Find first data disk in a raid6 stripe */
112static inline int raid6_d0(struct stripe_head *sh)
113{
NeilBrown67cc2b82009-03-31 14:39:38 +1100114 if (sh->ddf_layout)
115 /* ddf always start from first device */
116 return 0;
117 /* md starts just after Q block */
NeilBrownd0dabf72009-03-31 14:39:38 +1100118 if (sh->qd_idx == sh->disks - 1)
119 return 0;
120 else
121 return sh->qd_idx + 1;
122}
NeilBrown16a53ec2006-06-26 00:27:38 -0700123static inline int raid6_next_disk(int disk, int raid_disks)
124{
125 disk++;
126 return (disk < raid_disks) ? disk : 0;
127}
Dan Williamsa4456852007-07-09 11:56:43 -0700128
NeilBrownd0dabf72009-03-31 14:39:38 +1100129/* When walking through the disks in a raid5, starting at raid6_d0,
130 * We need to map each disk to a 'slot', where the data disks are slot
131 * 0 .. raid_disks-3, the parity disk is raid_disks-2 and the Q disk
132 * is raid_disks-1. This help does that mapping.
133 */
NeilBrown67cc2b82009-03-31 14:39:38 +1100134static int raid6_idx_to_slot(int idx, struct stripe_head *sh,
135 int *count, int syndrome_disks)
NeilBrownd0dabf72009-03-31 14:39:38 +1100136{
Dan Williams66295422009-10-19 18:09:32 -0700137 int slot = *count;
NeilBrown67cc2b82009-03-31 14:39:38 +1100138
NeilBrowne4424fe2009-10-16 16:27:34 +1100139 if (sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700140 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100141 if (idx == sh->pd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100142 return syndrome_disks;
NeilBrownd0dabf72009-03-31 14:39:38 +1100143 if (idx == sh->qd_idx)
NeilBrown67cc2b82009-03-31 14:39:38 +1100144 return syndrome_disks + 1;
NeilBrowne4424fe2009-10-16 16:27:34 +1100145 if (!sh->ddf_layout)
Dan Williams66295422009-10-19 18:09:32 -0700146 (*count)++;
NeilBrownd0dabf72009-03-31 14:39:38 +1100147 return slot;
148}
149
NeilBrownd1688a62011-10-11 16:49:52 +1100150static void print_raid5_conf (struct r5conf *conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151
Dan Williams600aa102008-06-28 08:32:05 +1000152static int stripe_operations_active(struct stripe_head *sh)
153{
154 return sh->check_state || sh->reconstruct_state ||
155 test_bit(STRIPE_BIOFILL_RUN, &sh->state) ||
156 test_bit(STRIPE_COMPUTE_RUN, &sh->state);
157}
158
Shaohua Li535ae4e2017-02-15 19:37:32 -0800159static bool stripe_is_lowprio(struct stripe_head *sh)
160{
161 return (test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) ||
162 test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state)) &&
163 !test_bit(STRIPE_R5C_CACHING, &sh->state);
164}
165
Shaohua Li851c30c2013-08-28 14:30:16 +0800166static void raid5_wakeup_stripe_thread(struct stripe_head *sh)
167{
168 struct r5conf *conf = sh->raid_conf;
169 struct r5worker_group *group;
Shaohua Libfc90cb2013-08-29 15:40:32 +0800170 int thread_cnt;
Shaohua Li851c30c2013-08-28 14:30:16 +0800171 int i, cpu = sh->cpu;
172
173 if (!cpu_online(cpu)) {
174 cpu = cpumask_any(cpu_online_mask);
175 sh->cpu = cpu;
176 }
177
178 if (list_empty(&sh->lru)) {
179 struct r5worker_group *group;
180 group = conf->worker_groups + cpu_to_group(cpu);
Shaohua Li535ae4e2017-02-15 19:37:32 -0800181 if (stripe_is_lowprio(sh))
182 list_add_tail(&sh->lru, &group->loprio_list);
183 else
184 list_add_tail(&sh->lru, &group->handle_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800185 group->stripes_cnt++;
186 sh->group = group;
Shaohua Li851c30c2013-08-28 14:30:16 +0800187 }
188
189 if (conf->worker_cnt_per_group == 0) {
190 md_wakeup_thread(conf->mddev->thread);
191 return;
192 }
193
194 group = conf->worker_groups + cpu_to_group(sh->cpu);
195
Shaohua Libfc90cb2013-08-29 15:40:32 +0800196 group->workers[0].working = true;
197 /* at least one worker should run to avoid race */
198 queue_work_on(sh->cpu, raid5_wq, &group->workers[0].work);
199
200 thread_cnt = group->stripes_cnt / MAX_STRIPE_BATCH - 1;
201 /* wakeup more workers */
202 for (i = 1; i < conf->worker_cnt_per_group && thread_cnt > 0; i++) {
203 if (group->workers[i].working == false) {
204 group->workers[i].working = true;
205 queue_work_on(sh->cpu, raid5_wq,
206 &group->workers[i].work);
207 thread_cnt--;
208 }
209 }
Shaohua Li851c30c2013-08-28 14:30:16 +0800210}
211
Shaohua Li566c09c2013-11-14 15:16:17 +1100212static void do_release_stripe(struct r5conf *conf, struct stripe_head *sh,
213 struct list_head *temp_inactive_list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
Song Liu1e6d6902016-11-17 15:24:39 -0800215 int i;
216 int injournal = 0; /* number of date pages with R5_InJournal */
217
Shaohua Li4eb788d2012-07-19 16:01:31 +1000218 BUG_ON(!list_empty(&sh->lru));
219 BUG_ON(atomic_read(&conf->active_stripes)==0);
Song Liu1e6d6902016-11-17 15:24:39 -0800220
221 if (r5c_is_writeback(conf->log))
222 for (i = sh->disks; i--; )
223 if (test_bit(R5_InJournal, &sh->dev[i].flags))
224 injournal++;
Song Liua39f7af2016-11-17 15:24:40 -0800225 /*
Song Liu5ddf0442017-05-11 17:03:44 -0700226 * In the following cases, the stripe cannot be released to cached
227 * lists. Therefore, we make the stripe write out and set
228 * STRIPE_HANDLE:
229 * 1. when quiesce in r5c write back;
230 * 2. when resync is requested fot the stripe.
Song Liua39f7af2016-11-17 15:24:40 -0800231 */
Song Liu5ddf0442017-05-11 17:03:44 -0700232 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) ||
233 (conf->quiesce && r5c_is_writeback(conf->log) &&
234 !test_bit(STRIPE_HANDLE, &sh->state) && injournal != 0)) {
Song Liua39f7af2016-11-17 15:24:40 -0800235 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
236 r5c_make_stripe_write_out(sh);
237 set_bit(STRIPE_HANDLE, &sh->state);
238 }
Song Liu1e6d6902016-11-17 15:24:39 -0800239
Shaohua Li4eb788d2012-07-19 16:01:31 +1000240 if (test_bit(STRIPE_HANDLE, &sh->state)) {
241 if (test_bit(STRIPE_DELAYED, &sh->state) &&
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500242 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
Shaohua Li4eb788d2012-07-19 16:01:31 +1000243 list_add_tail(&sh->lru, &conf->delayed_list);
Jes Sorensenad3ab8b2015-01-29 12:38:29 -0500244 else if (test_bit(STRIPE_BIT_DELAY, &sh->state) &&
Shaohua Li4eb788d2012-07-19 16:01:31 +1000245 sh->bm_seq - conf->seq_write > 0)
246 list_add_tail(&sh->lru, &conf->bitmap_list);
247 else {
248 clear_bit(STRIPE_DELAYED, &sh->state);
249 clear_bit(STRIPE_BIT_DELAY, &sh->state);
Shaohua Li851c30c2013-08-28 14:30:16 +0800250 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -0800251 if (stripe_is_lowprio(sh))
252 list_add_tail(&sh->lru,
253 &conf->loprio_list);
254 else
255 list_add_tail(&sh->lru,
256 &conf->handle_list);
Shaohua Li851c30c2013-08-28 14:30:16 +0800257 } else {
258 raid5_wakeup_stripe_thread(sh);
259 return;
260 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000261 }
262 md_wakeup_thread(conf->mddev->thread);
263 } else {
264 BUG_ON(stripe_operations_active(sh));
265 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
266 if (atomic_dec_return(&conf->preread_active_stripes)
267 < IO_THRESHOLD)
268 md_wakeup_thread(conf->mddev->thread);
269 atomic_dec(&conf->active_stripes);
Song Liu1e6d6902016-11-17 15:24:39 -0800270 if (!test_bit(STRIPE_EXPANDING, &sh->state)) {
271 if (!r5c_is_writeback(conf->log))
272 list_add_tail(&sh->lru, temp_inactive_list);
273 else {
274 WARN_ON(test_bit(R5_InJournal, &sh->dev[sh->pd_idx].flags));
275 if (injournal == 0)
276 list_add_tail(&sh->lru, temp_inactive_list);
277 else if (injournal == conf->raid_disks - conf->max_degraded) {
278 /* full stripe */
279 if (!test_and_set_bit(STRIPE_R5C_FULL_STRIPE, &sh->state))
280 atomic_inc(&conf->r5c_cached_full_stripes);
281 if (test_and_clear_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state))
282 atomic_dec(&conf->r5c_cached_partial_stripes);
283 list_add_tail(&sh->lru, &conf->r5c_full_stripe_list);
Song Liua39f7af2016-11-17 15:24:40 -0800284 r5c_check_cached_full_stripe(conf);
Song Liu03b047f2017-01-11 13:39:14 -0800285 } else
286 /*
287 * STRIPE_R5C_PARTIAL_STRIPE is set in
288 * r5c_try_caching_write(). No need to
289 * set it again.
290 */
Song Liu1e6d6902016-11-17 15:24:39 -0800291 list_add_tail(&sh->lru, &conf->r5c_partial_stripe_list);
Song Liu1e6d6902016-11-17 15:24:39 -0800292 }
293 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 }
295}
NeilBrownd0dabf72009-03-31 14:39:38 +1100296
Shaohua Li566c09c2013-11-14 15:16:17 +1100297static void __release_stripe(struct r5conf *conf, struct stripe_head *sh,
298 struct list_head *temp_inactive_list)
Shaohua Li4eb788d2012-07-19 16:01:31 +1000299{
300 if (atomic_dec_and_test(&sh->count))
Shaohua Li566c09c2013-11-14 15:16:17 +1100301 do_release_stripe(conf, sh, temp_inactive_list);
302}
303
304/*
305 * @hash could be NR_STRIPE_HASH_LOCKS, then we have a list of inactive_list
306 *
307 * Be careful: Only one task can add/delete stripes from temp_inactive_list at
308 * given time. Adding stripes only takes device lock, while deleting stripes
309 * only takes hash lock.
310 */
311static void release_inactive_stripe_list(struct r5conf *conf,
312 struct list_head *temp_inactive_list,
313 int hash)
314{
315 int size;
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800316 bool do_wakeup = false;
Shaohua Li566c09c2013-11-14 15:16:17 +1100317 unsigned long flags;
318
319 if (hash == NR_STRIPE_HASH_LOCKS) {
320 size = NR_STRIPE_HASH_LOCKS;
321 hash = NR_STRIPE_HASH_LOCKS - 1;
322 } else
323 size = 1;
324 while (size) {
325 struct list_head *list = &temp_inactive_list[size - 1];
326
327 /*
Shaohua Li6d036f72015-08-13 14:31:57 -0700328 * We don't hold any lock here yet, raid5_get_active_stripe() might
Shaohua Li566c09c2013-11-14 15:16:17 +1100329 * remove stripes from the list
330 */
331 if (!list_empty_careful(list)) {
332 spin_lock_irqsave(conf->hash_locks + hash, flags);
Shaohua Li4bda5562013-11-14 15:16:17 +1100333 if (list_empty(conf->inactive_list + hash) &&
334 !list_empty(list))
335 atomic_dec(&conf->empty_inactive_list_nr);
Shaohua Li566c09c2013-11-14 15:16:17 +1100336 list_splice_tail_init(list, conf->inactive_list + hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800337 do_wakeup = true;
Shaohua Li566c09c2013-11-14 15:16:17 +1100338 spin_unlock_irqrestore(conf->hash_locks + hash, flags);
339 }
340 size--;
341 hash--;
342 }
343
344 if (do_wakeup) {
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800345 wake_up(&conf->wait_for_stripe);
Yuanhan Liub1b46482015-05-08 18:19:06 +1000346 if (atomic_read(&conf->active_stripes) == 0)
347 wake_up(&conf->wait_for_quiescent);
Shaohua Li566c09c2013-11-14 15:16:17 +1100348 if (conf->retry_read_aligned)
349 md_wakeup_thread(conf->mddev->thread);
350 }
Shaohua Li4eb788d2012-07-19 16:01:31 +1000351}
352
Shaohua Li773ca822013-08-27 17:50:39 +0800353/* should hold conf->device_lock already */
Shaohua Li566c09c2013-11-14 15:16:17 +1100354static int release_stripe_list(struct r5conf *conf,
355 struct list_head *temp_inactive_list)
Shaohua Li773ca822013-08-27 17:50:39 +0800356{
Byungchul Parkeae82632017-02-14 16:26:24 +0900357 struct stripe_head *sh, *t;
Shaohua Li773ca822013-08-27 17:50:39 +0800358 int count = 0;
359 struct llist_node *head;
360
361 head = llist_del_all(&conf->released_stripes);
Shaohua Lid265d9d2013-08-28 14:29:05 +0800362 head = llist_reverse_order(head);
Byungchul Parkeae82632017-02-14 16:26:24 +0900363 llist_for_each_entry_safe(sh, t, head, release_list) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100364 int hash;
365
Shaohua Li773ca822013-08-27 17:50:39 +0800366 /* sh could be readded after STRIPE_ON_RELEASE_LIST is cleard */
367 smp_mb();
368 clear_bit(STRIPE_ON_RELEASE_LIST, &sh->state);
369 /*
370 * Don't worry the bit is set here, because if the bit is set
371 * again, the count is always > 1. This is true for
372 * STRIPE_ON_UNPLUG_LIST bit too.
373 */
Shaohua Li566c09c2013-11-14 15:16:17 +1100374 hash = sh->hash_lock_index;
375 __release_stripe(conf, sh, &temp_inactive_list[hash]);
Shaohua Li773ca822013-08-27 17:50:39 +0800376 count++;
377 }
378
379 return count;
380}
381
Shaohua Li6d036f72015-08-13 14:31:57 -0700382void raid5_release_stripe(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383{
NeilBrownd1688a62011-10-11 16:49:52 +1100384 struct r5conf *conf = sh->raid_conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 unsigned long flags;
Shaohua Li566c09c2013-11-14 15:16:17 +1100386 struct list_head list;
387 int hash;
Shaohua Li773ca822013-08-27 17:50:39 +0800388 bool wakeup;
NeilBrown16a53ec2006-06-26 00:27:38 -0700389
Eivind Sartocf170f32014-05-28 13:39:23 +1000390 /* Avoid release_list until the last reference.
391 */
392 if (atomic_add_unless(&sh->count, -1, 1))
393 return;
394
majianpengad4068d2013-11-14 15:16:15 +1100395 if (unlikely(!conf->mddev->thread) ||
396 test_and_set_bit(STRIPE_ON_RELEASE_LIST, &sh->state))
Shaohua Li773ca822013-08-27 17:50:39 +0800397 goto slow_path;
398 wakeup = llist_add(&sh->release_list, &conf->released_stripes);
399 if (wakeup)
400 md_wakeup_thread(conf->mddev->thread);
401 return;
402slow_path:
Shaohua Li773ca822013-08-27 17:50:39 +0800403 /* we are ok here if STRIPE_ON_RELEASE_LIST is set or not */
Anna-Maria Gleixner685dbca2018-07-03 22:01:36 +0200404 if (atomic_dec_and_lock_irqsave(&sh->count, &conf->device_lock, flags)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100405 INIT_LIST_HEAD(&list);
406 hash = sh->hash_lock_index;
407 do_release_stripe(conf, sh, &list);
Anna-Maria Gleixner08edaaa2018-07-03 22:01:37 +0200408 spin_unlock_irqrestore(&conf->device_lock, flags);
Shaohua Li566c09c2013-11-14 15:16:17 +1100409 release_inactive_stripe_list(conf, &list, hash);
Shaohua Li4eb788d2012-07-19 16:01:31 +1000410 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411}
412
NeilBrownfccddba2006-01-06 00:20:33 -0800413static inline void remove_hash(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414{
Dan Williams45b42332007-07-09 11:56:43 -0700415 pr_debug("remove_hash(), stripe %llu\n",
416 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
NeilBrownfccddba2006-01-06 00:20:33 -0800418 hlist_del_init(&sh->hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
NeilBrownd1688a62011-10-11 16:49:52 +1100421static inline void insert_hash(struct r5conf *conf, struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422{
NeilBrownfccddba2006-01-06 00:20:33 -0800423 struct hlist_head *hp = stripe_hash(conf, sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Dan Williams45b42332007-07-09 11:56:43 -0700425 pr_debug("insert_hash(), stripe %llu\n",
426 (unsigned long long)sh->sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
NeilBrownfccddba2006-01-06 00:20:33 -0800428 hlist_add_head(&sh->hash, hp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431/* find an idle stripe, make sure it is unhashed, and return it. */
Shaohua Li566c09c2013-11-14 15:16:17 +1100432static struct stripe_head *get_free_stripe(struct r5conf *conf, int hash)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433{
434 struct stripe_head *sh = NULL;
435 struct list_head *first;
436
Shaohua Li566c09c2013-11-14 15:16:17 +1100437 if (list_empty(conf->inactive_list + hash))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 goto out;
Shaohua Li566c09c2013-11-14 15:16:17 +1100439 first = (conf->inactive_list + hash)->next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 sh = list_entry(first, struct stripe_head, lru);
441 list_del_init(first);
442 remove_hash(sh);
443 atomic_inc(&conf->active_stripes);
Shaohua Li566c09c2013-11-14 15:16:17 +1100444 BUG_ON(hash != sh->hash_lock_index);
Shaohua Li4bda5562013-11-14 15:16:17 +1100445 if (list_empty(conf->inactive_list + hash))
446 atomic_inc(&conf->empty_inactive_list_nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447out:
448 return sh;
449}
450
NeilBrowne4e11e32010-06-16 16:45:16 +1000451static void shrink_buffers(struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
453 struct page *p;
454 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000455 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
NeilBrowne4e11e32010-06-16 16:45:16 +1000457 for (i = 0; i < num ; i++) {
Shaohua Lid592a992014-05-21 17:57:44 +0800458 WARN_ON(sh->dev[i].page != sh->dev[i].orig_page);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 p = sh->dev[i].page;
460 if (!p)
461 continue;
462 sh->dev[i].page = NULL;
NeilBrown2d1f3b52006-01-06 00:20:31 -0800463 put_page(p);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465}
466
NeilBrowna9683a72015-02-25 12:02:51 +1100467static int grow_buffers(struct stripe_head *sh, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468{
469 int i;
NeilBrowne4e11e32010-06-16 16:45:16 +1000470 int num = sh->raid_conf->pool_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471
NeilBrowne4e11e32010-06-16 16:45:16 +1000472 for (i = 0; i < num; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 struct page *page;
474
NeilBrowna9683a72015-02-25 12:02:51 +1100475 if (!(page = alloc_page(gfp))) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 return 1;
477 }
478 sh->dev[i].page = page;
Shaohua Lid592a992014-05-21 17:57:44 +0800479 sh->dev[i].orig_page = page;
Yufen Yu7aba13b2020-08-20 09:22:06 -0400480 sh->dev[i].offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 }
Artur Paszkiewicz3418d032017-03-09 09:59:59 +0100482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 return 0;
484}
485
NeilBrownd1688a62011-10-11 16:49:52 +1100486static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +1100487 struct stripe_head *sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488
NeilBrownb5663ba2009-03-31 14:39:38 +1100489static void init_stripe(struct stripe_head *sh, sector_t sector, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490{
NeilBrownd1688a62011-10-11 16:49:52 +1100491 struct r5conf *conf = sh->raid_conf;
Shaohua Li566c09c2013-11-14 15:16:17 +1100492 int i, seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +0200494 BUG_ON(atomic_read(&sh->count) != 0);
495 BUG_ON(test_bit(STRIPE_HANDLE, &sh->state));
Dan Williams600aa102008-06-28 08:32:05 +1000496 BUG_ON(stripe_operations_active(sh));
shli@kernel.org59fc6302014-12-15 12:57:03 +1100497 BUG_ON(sh->batch_head);
Dan Williamsd84e0f12007-01-02 13:52:30 -0700498
Dan Williams45b42332007-07-09 11:56:43 -0700499 pr_debug("init_stripe called, stripe %llu\n",
Markus Stockhausenb8e6a152014-08-23 20:19:27 +1000500 (unsigned long long)sector);
Shaohua Li566c09c2013-11-14 15:16:17 +1100501retry:
502 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrown86b42c72009-03-31 15:19:03 +1100503 sh->generation = conf->generation - previous;
NeilBrownb5663ba2009-03-31 14:39:38 +1100504 sh->disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 sh->sector = sector;
NeilBrown911d4ee2009-03-31 14:39:38 +1100506 stripe_set_idx(sector, conf, previous, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 sh->state = 0;
508
NeilBrown7ecaa1e2006-03-27 01:18:08 -0800509 for (i = sh->disks; i--; ) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 struct r5dev *dev = &sh->dev[i];
511
Dan Williamsd84e0f12007-01-02 13:52:30 -0700512 if (dev->toread || dev->read || dev->towrite || dev->written ||
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 test_bit(R5_LOCKED, &dev->flags)) {
NeilBrowncc6167b2016-11-02 14:16:50 +1100514 pr_err("sector=%llx i=%d %p %p %p %p %d\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 (unsigned long long)sh->sector, i, dev->toread,
Dan Williamsd84e0f12007-01-02 13:52:30 -0700516 dev->read, dev->towrite, dev->written,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 test_bit(R5_LOCKED, &dev->flags));
NeilBrown8cfa7b02011-07-27 11:00:36 +1000518 WARN_ON(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520 dev->flags = 0;
Guoqing Jiang27a4ff82017-08-10 16:12:17 +0800521 dev->sector = raid5_compute_blocknr(sh, i, previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 }
Shaohua Li566c09c2013-11-14 15:16:17 +1100523 if (read_seqcount_retry(&conf->gen_lock, seq))
524 goto retry;
shli@kernel.org7a87f432014-12-15 12:57:03 +1100525 sh->overwrite_disks = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 insert_hash(conf, sh);
Shaohua Li851c30c2013-08-28 14:30:16 +0800527 sh->cpu = smp_processor_id();
shli@kernel.orgda41ba62014-12-15 12:57:03 +1100528 set_bit(STRIPE_BATCH_READY, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529}
530
NeilBrownd1688a62011-10-11 16:49:52 +1100531static struct stripe_head *__find_stripe(struct r5conf *conf, sector_t sector,
NeilBrown86b42c72009-03-31 15:19:03 +1100532 short generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533{
534 struct stripe_head *sh;
535
Dan Williams45b42332007-07-09 11:56:43 -0700536 pr_debug("__find_stripe, sector %llu\n", (unsigned long long)sector);
Sasha Levinb67bfe02013-02-27 17:06:00 -0800537 hlist_for_each_entry(sh, stripe_hash(conf, sector), hash)
NeilBrown86b42c72009-03-31 15:19:03 +1100538 if (sh->sector == sector && sh->generation == generation)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 return sh;
Dan Williams45b42332007-07-09 11:56:43 -0700540 pr_debug("__stripe %llu not in cache\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 return NULL;
542}
543
NeilBrown674806d2010-06-16 17:17:53 +1000544/*
545 * Need to check if array has failed when deciding whether to:
546 * - start an array
547 * - remove non-faulty devices
548 * - add a spare
549 * - allow a reshape
550 * This determination is simple when no reshape is happening.
551 * However if there is a reshape, we need to carefully check
552 * both the before and after sections.
553 * This is because some failed devices may only affect one
554 * of the two sections, and some non-in_sync devices may
555 * be insync in the section most affected by failed devices.
556 */
Song Liu2e38a372017-01-24 10:45:30 -0800557int raid5_calc_degraded(struct r5conf *conf)
NeilBrown674806d2010-06-16 17:17:53 +1000558{
NeilBrown908f4fb2011-12-23 10:17:50 +1100559 int degraded, degraded2;
NeilBrown674806d2010-06-16 17:17:53 +1000560 int i;
NeilBrown674806d2010-06-16 17:17:53 +1000561
562 rcu_read_lock();
563 degraded = 0;
564 for (i = 0; i < conf->previous_raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100565 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000566 if (rdev && test_bit(Faulty, &rdev->flags))
567 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000568 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();
NeilBrown908f4fb2011-12-23 10:17:50 +1100586 if (conf->raid_disks == conf->previous_raid_disks)
587 return degraded;
NeilBrown674806d2010-06-16 17:17:53 +1000588 rcu_read_lock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100589 degraded2 = 0;
NeilBrown674806d2010-06-16 17:17:53 +1000590 for (i = 0; i < conf->raid_disks; i++) {
NeilBrown3cb03002011-10-11 16:45:26 +1100591 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrowne5c86472012-09-19 12:52:30 +1000592 if (rdev && test_bit(Faulty, &rdev->flags))
593 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown674806d2010-06-16 17:17:53 +1000594 if (!rdev || test_bit(Faulty, &rdev->flags))
NeilBrown908f4fb2011-12-23 10:17:50 +1100595 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000596 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)
NeilBrown908f4fb2011-12-23 10:17:50 +1100605 degraded2++;
NeilBrown674806d2010-06-16 17:17:53 +1000606 }
607 rcu_read_unlock();
NeilBrown908f4fb2011-12-23 10:17:50 +1100608 if (degraded2 > degraded)
609 return degraded2;
610 return degraded;
611}
612
613static int has_failed(struct r5conf *conf)
614{
615 int degraded;
616
617 if (conf->mddev->reshape_position == MaxSector)
618 return conf->mddev->degraded > conf->max_degraded;
619
Song Liu2e38a372017-01-24 10:45:30 -0800620 degraded = raid5_calc_degraded(conf);
NeilBrown674806d2010-06-16 17:17:53 +1000621 if (degraded > conf->max_degraded)
622 return 1;
623 return 0;
624}
625
Shaohua Li6d036f72015-08-13 14:31:57 -0700626struct stripe_head *
627raid5_get_active_stripe(struct r5conf *conf, sector_t sector,
628 int previous, int noblock, int noquiesce)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
630 struct stripe_head *sh;
Yufen Yuc911c462020-07-18 05:29:07 -0400631 int hash = stripe_hash_locks_hash(conf, sector);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800632 int inc_empty_inactive_list_flag;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633
Dan Williams45b42332007-07-09 11:56:43 -0700634 pr_debug("get_stripe, sector %llu\n", (unsigned long long)sector);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
Shaohua Li566c09c2013-11-14 15:16:17 +1100636 spin_lock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 do {
Yuanhan Liub1b46482015-05-08 18:19:06 +1000639 wait_event_lock_irq(conf->wait_for_quiescent,
NeilBrowna8c906c2009-06-09 14:39:59 +1000640 conf->quiesce == 0 || noquiesce,
Shaohua Li566c09c2013-11-14 15:16:17 +1100641 *(conf->hash_locks + hash));
NeilBrown86b42c72009-03-31 15:19:03 +1100642 sh = __find_stripe(conf, sector, conf->generation - previous);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 if (!sh) {
NeilBrownedbe83a2015-02-26 12:47:56 +1100644 if (!test_bit(R5_INACTIVE_BLOCKED, &conf->cache_state)) {
Shaohua Li566c09c2013-11-14 15:16:17 +1100645 sh = get_free_stripe(conf, hash);
Shaohua Li713bc5c2015-05-28 17:33:47 -0700646 if (!sh && !test_bit(R5_DID_ALLOC,
647 &conf->cache_state))
NeilBrownedbe83a2015-02-26 12:47:56 +1100648 set_bit(R5_ALLOC_MORE,
649 &conf->cache_state);
650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 if (noblock && sh == NULL)
652 break;
Song Liua39f7af2016-11-17 15:24:40 -0800653
654 r5c_check_stripe_cache_usage(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 if (!sh) {
NeilBrown54233992015-02-26 12:21:04 +1100656 set_bit(R5_INACTIVE_BLOCKED,
657 &conf->cache_state);
Song Liua39f7af2016-11-17 15:24:40 -0800658 r5l_wake_reclaim(conf->log, 0);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800659 wait_event_lock_irq(
660 conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +1100661 !list_empty(conf->inactive_list + hash) &&
662 (atomic_read(&conf->active_stripes)
663 < (conf->max_nr_stripes * 3 / 4)
NeilBrown54233992015-02-26 12:21:04 +1100664 || !test_bit(R5_INACTIVE_BLOCKED,
665 &conf->cache_state)),
Shaohua Li6ab2a4b2016-02-25 16:24:42 -0800666 *(conf->hash_locks + hash));
NeilBrown54233992015-02-26 12:21:04 +1100667 clear_bit(R5_INACTIVE_BLOCKED,
668 &conf->cache_state);
NeilBrown7da9d452014-01-22 11:45:03 +1100669 } else {
NeilBrownb5663ba2009-03-31 14:39:38 +1100670 init_stripe(sh, sector, previous);
NeilBrown7da9d452014-01-22 11:45:03 +1100671 atomic_inc(&sh->count);
672 }
Shaohua Lie240c182014-04-09 11:27:42 +0800673 } else if (!atomic_inc_not_zero(&sh->count)) {
NeilBrown6d183de2013-11-28 10:55:27 +1100674 spin_lock(&conf->device_lock);
Shaohua Lie240c182014-04-09 11:27:42 +0800675 if (!atomic_read(&sh->count)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!test_bit(STRIPE_HANDLE, &sh->state))
677 atomic_inc(&conf->active_stripes);
NeilBrown5af9bef2014-01-14 15:16:10 +1100678 BUG_ON(list_empty(&sh->lru) &&
679 !test_bit(STRIPE_EXPANDING, &sh->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800680 inc_empty_inactive_list_flag = 0;
681 if (!list_empty(conf->inactive_list + hash))
682 inc_empty_inactive_list_flag = 1;
NeilBrown16a53ec2006-06-26 00:27:38 -0700683 list_del_init(&sh->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800684 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
685 atomic_inc(&conf->empty_inactive_list_nr);
Shaohua Libfc90cb2013-08-29 15:40:32 +0800686 if (sh->group) {
687 sh->group->stripes_cnt--;
688 sh->group = NULL;
689 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 }
NeilBrown7da9d452014-01-22 11:45:03 +1100691 atomic_inc(&sh->count);
NeilBrown6d183de2013-11-28 10:55:27 +1100692 spin_unlock(&conf->device_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 }
694 } while (sh == NULL);
695
Shaohua Li566c09c2013-11-14 15:16:17 +1100696 spin_unlock_irq(conf->hash_locks + hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 return sh;
698}
699
shli@kernel.org7a87f432014-12-15 12:57:03 +1100700static 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.org59fc6302014-12-15 12:57:03 +1100706static void lock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
Christoph Hellwig368ecad2019-04-04 18:56:15 +0200707 __acquires(&sh1->stripe_lock)
708 __acquires(&sh2->stripe_lock)
shli@kernel.org59fc6302014-12-15 12:57:03 +1100709{
shli@kernel.org59fc6302014-12-15 12:57:03 +1100710 if (sh1 > sh2) {
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500711 spin_lock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100712 spin_lock_nested(&sh1->stripe_lock, 1);
713 } else {
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500714 spin_lock_irq(&sh1->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100715 spin_lock_nested(&sh2->stripe_lock, 1);
716 }
717}
718
719static void unlock_two_stripes(struct stripe_head *sh1, struct stripe_head *sh2)
Christoph Hellwig368ecad2019-04-04 18:56:15 +0200720 __releases(&sh1->stripe_lock)
721 __releases(&sh2->stripe_lock)
shli@kernel.org59fc6302014-12-15 12:57:03 +1100722{
723 spin_unlock(&sh1->stripe_lock);
Julia Cartwright3d05f3a2017-04-28 12:41:02 -0500724 spin_unlock_irq(&sh2->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100725}
726
727/* Only freshly new full stripe normal write stripe can be added to a batch list */
728static bool stripe_can_batch(struct stripe_head *sh)
729{
Shaohua Li9c3e3332015-08-13 14:32:02 -0700730 struct r5conf *conf = sh->raid_conf;
731
Shaohua Lie254de62018-08-29 11:05:42 -0700732 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li9c3e3332015-08-13 14:32:02 -0700733 return false;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100734 return test_bit(STRIPE_BATCH_READY, &sh->state) &&
NeilBrownd0852df52015-05-27 08:43:45 +1000735 !test_bit(STRIPE_BITMAP_PENDING, &sh->state) &&
shli@kernel.org59fc6302014-12-15 12:57:03 +1100736 is_full_stripe_write(sh);
737}
738
739/* we only do back search */
740static void stripe_add_to_batch_list(struct r5conf *conf, struct stripe_head *sh)
741{
742 struct stripe_head *head;
743 sector_t head_sector, tmp_sec;
744 int hash;
745 int dd_idx;
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800746 int inc_empty_inactive_list_flag;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100747
shli@kernel.org59fc6302014-12-15 12:57:03 +1100748 /* Don't cross chunks, so stripe pd_idx/qd_idx is the same */
749 tmp_sec = sh->sector;
750 if (!sector_div(tmp_sec, conf->chunk_sectors))
751 return;
Yufen Yuc911c462020-07-18 05:29:07 -0400752 head_sector = sh->sector - RAID5_STRIPE_SECTORS(conf);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100753
Yufen Yuc911c462020-07-18 05:29:07 -0400754 hash = stripe_hash_locks_hash(conf, head_sector);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100755 spin_lock_irq(conf->hash_locks + hash);
756 head = __find_stripe(conf, head_sector, conf->generation);
757 if (head && !atomic_inc_not_zero(&head->count)) {
758 spin_lock(&conf->device_lock);
759 if (!atomic_read(&head->count)) {
760 if (!test_bit(STRIPE_HANDLE, &head->state))
761 atomic_inc(&conf->active_stripes);
762 BUG_ON(list_empty(&head->lru) &&
763 !test_bit(STRIPE_EXPANDING, &head->state));
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800764 inc_empty_inactive_list_flag = 0;
765 if (!list_empty(conf->inactive_list + hash))
766 inc_empty_inactive_list_flag = 1;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100767 list_del_init(&head->lru);
ZhengYuan Liuff00d3b2016-07-28 14:22:14 +0800768 if (list_empty(conf->inactive_list + hash) && inc_empty_inactive_list_flag)
769 atomic_inc(&conf->empty_inactive_list_nr);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100770 if (head->group) {
771 head->group->stripes_cnt--;
772 head->group = NULL;
773 }
774 }
775 atomic_inc(&head->count);
776 spin_unlock(&conf->device_lock);
777 }
778 spin_unlock_irq(conf->hash_locks + hash);
779
780 if (!head)
781 return;
782 if (!stripe_can_batch(head))
783 goto out;
784
785 lock_two_stripes(head, sh);
786 /* clear_batch_ready clear the flag */
787 if (!stripe_can_batch(head) || !stripe_can_batch(sh))
788 goto unlock_out;
789
790 if (sh->batch_head)
791 goto unlock_out;
792
793 dd_idx = 0;
794 while (dd_idx == sh->pd_idx || dd_idx == sh->qd_idx)
795 dd_idx++;
Jens Axboe1eff9d32016-08-05 15:35:16 -0600796 if (head->dev[dd_idx].towrite->bi_opf != sh->dev[dd_idx].towrite->bi_opf ||
Mike Christie796a5cf2016-06-05 14:32:07 -0500797 bio_op(head->dev[dd_idx].towrite) != bio_op(sh->dev[dd_idx].towrite))
shli@kernel.org59fc6302014-12-15 12:57:03 +1100798 goto unlock_out;
799
800 if (head->batch_head) {
801 spin_lock(&head->batch_head->batch_lock);
802 /* This batch list is already running */
803 if (!stripe_can_batch(head)) {
804 spin_unlock(&head->batch_head->batch_lock);
805 goto unlock_out;
806 }
Shaohua Li36648472017-08-25 10:40:02 -0700807 /*
808 * We must assign batch_head of this stripe within the
809 * batch_lock, otherwise clear_batch_ready of batch head
810 * stripe could clear BATCH_READY bit of this stripe and
811 * this stripe->batch_head doesn't get assigned, which
812 * could confuse clear_batch_ready for this stripe
813 */
814 sh->batch_head = head->batch_head;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100815
816 /*
817 * at this point, head's BATCH_READY could be cleared, but we
818 * can still add the stripe to batch list
819 */
820 list_add(&sh->batch_list, &head->batch_list);
821 spin_unlock(&head->batch_head->batch_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100822 } else {
823 head->batch_head = head;
824 sh->batch_head = head->batch_head;
825 spin_lock(&head->batch_lock);
826 list_add_tail(&sh->batch_list, &head->batch_list);
827 spin_unlock(&head->batch_lock);
828 }
829
830 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
831 if (atomic_dec_return(&conf->preread_active_stripes)
832 < IO_THRESHOLD)
833 md_wakeup_thread(conf->mddev->thread);
834
NeilBrown2b6b2452015-05-21 15:10:01 +1000835 if (test_and_clear_bit(STRIPE_BIT_DELAY, &sh->state)) {
836 int seq = sh->bm_seq;
837 if (test_bit(STRIPE_BIT_DELAY, &sh->batch_head->state) &&
838 sh->batch_head->bm_seq > seq)
839 seq = sh->batch_head->bm_seq;
840 set_bit(STRIPE_BIT_DELAY, &sh->batch_head->state);
841 sh->batch_head->bm_seq = seq;
842 }
843
shli@kernel.org59fc6302014-12-15 12:57:03 +1100844 atomic_inc(&sh->count);
845unlock_out:
846 unlock_two_stripes(head, sh);
847out:
Shaohua Li6d036f72015-08-13 14:31:57 -0700848 raid5_release_stripe(head);
shli@kernel.org59fc6302014-12-15 12:57:03 +1100849}
850
NeilBrown05616be2012-05-21 09:27:00 +1000851/* Determine if 'data_offset' or 'new_data_offset' should be used
852 * in this stripe_head.
853 */
854static int use_new_offset(struct r5conf *conf, struct stripe_head *sh)
855{
856 sector_t progress = conf->reshape_progress;
857 /* Need a memory barrier to make sure we see the value
858 * of conf->generation, or ->data_offset that was set before
859 * reshape_progress was updated.
860 */
861 smp_rmb();
862 if (progress == MaxSector)
863 return 0;
864 if (sh->generation == conf->generation - 1)
865 return 0;
866 /* We are in a reshape, and this is a new-generation stripe,
867 * so use new_data_offset.
868 */
869 return 1;
870}
871
Shaohua Liaaf9f122017-03-03 22:06:12 -0800872static void dispatch_bio_list(struct bio_list *tmp)
Shaohua Li765d7042017-01-04 09:33:23 -0800873{
Shaohua Li765d7042017-01-04 09:33:23 -0800874 struct bio *bio;
875
Shaohua Liaaf9f122017-03-03 22:06:12 -0800876 while ((bio = bio_list_pop(tmp)))
Christoph Hellwiged00aab2020-07-01 10:59:44 +0200877 submit_bio_noacct(bio);
Shaohua Li765d7042017-01-04 09:33:23 -0800878}
879
Shaohua Liaaf9f122017-03-03 22:06:12 -0800880static int cmp_stripe(void *priv, struct list_head *a, struct list_head *b)
Shaohua Li765d7042017-01-04 09:33:23 -0800881{
Shaohua Liaaf9f122017-03-03 22:06:12 -0800882 const struct r5pending_data *da = list_entry(a,
883 struct r5pending_data, sibling);
884 const struct r5pending_data *db = list_entry(b,
885 struct r5pending_data, sibling);
886 if (da->sector > db->sector)
887 return 1;
888 if (da->sector < db->sector)
889 return -1;
890 return 0;
891}
892
893static void dispatch_defer_bios(struct r5conf *conf, int target,
894 struct bio_list *list)
895{
896 struct r5pending_data *data;
897 struct list_head *first, *next = NULL;
898 int cnt = 0;
899
900 if (conf->pending_data_cnt == 0)
Shaohua Li765d7042017-01-04 09:33:23 -0800901 return;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800902
903 list_sort(NULL, &conf->pending_list, cmp_stripe);
904
905 first = conf->pending_list.next;
906
907 /* temporarily move the head */
908 if (conf->next_pending_data)
909 list_move_tail(&conf->pending_list,
910 &conf->next_pending_data->sibling);
911
912 while (!list_empty(&conf->pending_list)) {
913 data = list_first_entry(&conf->pending_list,
914 struct r5pending_data, sibling);
915 if (&data->sibling == first)
916 first = data->sibling.next;
917 next = data->sibling.next;
918
919 bio_list_merge(list, &data->bios);
920 list_move(&data->sibling, &conf->free_list);
921 cnt++;
922 if (cnt >= target)
923 break;
Shaohua Li765d7042017-01-04 09:33:23 -0800924 }
Shaohua Liaaf9f122017-03-03 22:06:12 -0800925 conf->pending_data_cnt -= cnt;
926 BUG_ON(conf->pending_data_cnt < 0 || cnt < target);
927
928 if (next != &conf->pending_list)
929 conf->next_pending_data = list_entry(next,
930 struct r5pending_data, sibling);
931 else
932 conf->next_pending_data = NULL;
933 /* list isn't empty */
934 if (first != &conf->pending_list)
935 list_move_tail(&conf->pending_list, first);
936}
937
938static void flush_deferred_bios(struct r5conf *conf)
939{
940 struct bio_list tmp = BIO_EMPTY_LIST;
941
942 if (conf->pending_data_cnt == 0)
943 return;
944
Shaohua Li765d7042017-01-04 09:33:23 -0800945 spin_lock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800946 dispatch_defer_bios(conf, conf->pending_data_cnt, &tmp);
947 BUG_ON(conf->pending_data_cnt != 0);
Shaohua Li765d7042017-01-04 09:33:23 -0800948 spin_unlock(&conf->pending_bios_lock);
Shaohua Liaaf9f122017-03-03 22:06:12 -0800949
950 dispatch_bio_list(&tmp);
951}
952
953static void defer_issue_bios(struct r5conf *conf, sector_t sector,
954 struct bio_list *bios)
955{
956 struct bio_list tmp = BIO_EMPTY_LIST;
957 struct r5pending_data *ent;
958
959 spin_lock(&conf->pending_bios_lock);
960 ent = list_first_entry(&conf->free_list, struct r5pending_data,
961 sibling);
962 list_move_tail(&ent->sibling, &conf->pending_list);
963 ent->sector = sector;
964 bio_list_init(&ent->bios);
965 bio_list_merge(&ent->bios, bios);
966 conf->pending_data_cnt++;
967 if (conf->pending_data_cnt >= PENDING_IO_MAX)
968 dispatch_defer_bios(conf, PENDING_IO_ONE_FLUSH, &tmp);
969
970 spin_unlock(&conf->pending_bios_lock);
971
972 dispatch_bio_list(&tmp);
Shaohua Li765d7042017-01-04 09:33:23 -0800973}
974
NeilBrown6712ecf2007-09-27 12:47:43 +0200975static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200976raid5_end_read_request(struct bio *bi);
NeilBrown6712ecf2007-09-27 12:47:43 +0200977static void
Christoph Hellwig4246a0b2015-07-20 15:29:37 +0200978raid5_end_write_request(struct bio *bi);
Dan Williams91c00922007-01-02 13:52:30 -0700979
Dan Williamsc4e5ac02008-06-28 08:31:53 +1000980static void ops_run_io(struct stripe_head *sh, struct stripe_head_state *s)
Dan Williams91c00922007-01-02 13:52:30 -0700981{
NeilBrownd1688a62011-10-11 16:49:52 +1100982 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -0700983 int i, disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +1100984 struct stripe_head *head_sh = sh;
Shaohua Liaaf9f122017-03-03 22:06:12 -0800985 struct bio_list pending_bios = BIO_EMPTY_LIST;
986 bool should_defer;
Dan Williams91c00922007-01-02 13:52:30 -0700987
988 might_sleep();
989
Artur Paszkiewiczff875732017-03-09 09:59:58 +0100990 if (log_stripe(sh, s) == 0)
991 return;
Song Liu1e6d6902016-11-17 15:24:39 -0800992
Shaohua Liaaf9f122017-03-03 22:06:12 -0800993 should_defer = conf->batch_bio_dispatch && conf->group_cnt;
Shaohua Lif6bed0e2015-08-13 14:31:59 -0700994
Dan Williams91c00922007-01-02 13:52:30 -0700995 for (i = disks; i--; ) {
Mike Christie796a5cf2016-06-05 14:32:07 -0500996 int op, op_flags = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +1100997 int replace_only = 0;
NeilBrown977df362011-12-23 10:17:53 +1100998 struct bio *bi, *rbi;
999 struct md_rdev *rdev, *rrdev = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001000
1001 sh = head_sh;
Tejun Heoe9c74692010-09-03 11:56:18 +02001002 if (test_and_clear_bit(R5_Wantwrite, &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001003 op = REQ_OP_WRITE;
Tejun Heoe9c74692010-09-03 11:56:18 +02001004 if (test_and_clear_bit(R5_WantFUA, &sh->dev[i].flags))
Christoph Hellwig70fd7612016-11-01 07:40:10 -06001005 op_flags = REQ_FUA;
Shaohua Li9e4447682012-10-11 13:49:49 +11001006 if (test_bit(R5_Discard, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001007 op = REQ_OP_DISCARD;
Tejun Heoe9c74692010-09-03 11:56:18 +02001008 } else if (test_and_clear_bit(R5_Wantread, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001009 op = REQ_OP_READ;
NeilBrown9a3e1102011-12-23 10:17:53 +11001010 else if (test_and_clear_bit(R5_WantReplace,
1011 &sh->dev[i].flags)) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001012 op = REQ_OP_WRITE;
NeilBrown9a3e1102011-12-23 10:17:53 +11001013 replace_only = 1;
1014 } else
Dan Williams91c00922007-01-02 13:52:30 -07001015 continue;
Shaohua Libc0934f2012-05-22 13:55:05 +10001016 if (test_and_clear_bit(R5_SyncIO, &sh->dev[i].flags))
Mike Christie796a5cf2016-06-05 14:32:07 -05001017 op_flags |= REQ_SYNC;
Dan Williams91c00922007-01-02 13:52:30 -07001018
shli@kernel.org59fc6302014-12-15 12:57:03 +11001019again:
Dan Williams91c00922007-01-02 13:52:30 -07001020 bi = &sh->dev[i].req;
NeilBrown977df362011-12-23 10:17:53 +11001021 rbi = &sh->dev[i].rreq; /* For writing to replacement */
Dan Williams91c00922007-01-02 13:52:30 -07001022
Dan Williams91c00922007-01-02 13:52:30 -07001023 rcu_read_lock();
NeilBrown9a3e1102011-12-23 10:17:53 +11001024 rrdev = rcu_dereference(conf->disks[i].replacement);
NeilBrowndd054fc2011-12-23 10:17:53 +11001025 smp_mb(); /* Ensure that if rrdev is NULL, rdev won't be */
1026 rdev = rcu_dereference(conf->disks[i].rdev);
1027 if (!rdev) {
1028 rdev = rrdev;
1029 rrdev = NULL;
1030 }
Mike Christie796a5cf2016-06-05 14:32:07 -05001031 if (op_is_write(op)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001032 if (replace_only)
1033 rdev = NULL;
NeilBrowndd054fc2011-12-23 10:17:53 +11001034 if (rdev == rrdev)
1035 /* We raced and saw duplicates */
1036 rrdev = NULL;
NeilBrown9a3e1102011-12-23 10:17:53 +11001037 } else {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001038 if (test_bit(R5_ReadRepl, &head_sh->dev[i].flags) && rrdev)
NeilBrown9a3e1102011-12-23 10:17:53 +11001039 rdev = rrdev;
1040 rrdev = NULL;
1041 }
NeilBrown977df362011-12-23 10:17:53 +11001042
Dan Williams91c00922007-01-02 13:52:30 -07001043 if (rdev && test_bit(Faulty, &rdev->flags))
1044 rdev = NULL;
1045 if (rdev)
1046 atomic_inc(&rdev->nr_pending);
NeilBrown977df362011-12-23 10:17:53 +11001047 if (rrdev && test_bit(Faulty, &rrdev->flags))
1048 rrdev = NULL;
1049 if (rrdev)
1050 atomic_inc(&rrdev->nr_pending);
Dan Williams91c00922007-01-02 13:52:30 -07001051 rcu_read_unlock();
1052
NeilBrown73e92e52011-07-28 11:39:22 +10001053 /* We have already checked bad blocks for reads. Now
NeilBrown977df362011-12-23 10:17:53 +11001054 * need to check for writes. We never accept write errors
1055 * on the replacement, so we don't to check rrdev.
NeilBrown73e92e52011-07-28 11:39:22 +10001056 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001057 while (op_is_write(op) && rdev &&
NeilBrown73e92e52011-07-28 11:39:22 +10001058 test_bit(WriteErrorSeen, &rdev->flags)) {
1059 sector_t first_bad;
1060 int bad_sectors;
Yufen Yuc911c462020-07-18 05:29:07 -04001061 int bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown73e92e52011-07-28 11:39:22 +10001062 &first_bad, &bad_sectors);
1063 if (!bad)
1064 break;
1065
1066 if (bad < 0) {
1067 set_bit(BlockedBadBlocks, &rdev->flags);
1068 if (!conf->mddev->external &&
Shaohua Li29530792016-12-08 15:48:19 -08001069 conf->mddev->sb_flags) {
NeilBrown73e92e52011-07-28 11:39:22 +10001070 /* It is very unlikely, but we might
1071 * still need to write out the
1072 * bad block log - better give it
1073 * a chance*/
1074 md_check_recovery(conf->mddev);
1075 }
majianpeng18507532012-07-03 12:11:54 +10001076 /*
1077 * Because md_wait_for_blocked_rdev
1078 * will dec nr_pending, we must
1079 * increment it first.
1080 */
1081 atomic_inc(&rdev->nr_pending);
NeilBrown73e92e52011-07-28 11:39:22 +10001082 md_wait_for_blocked_rdev(rdev, conf->mddev);
1083 } else {
1084 /* Acknowledged bad block - skip the write */
1085 rdev_dec_pending(rdev, conf->mddev);
1086 rdev = NULL;
1087 }
1088 }
1089
Dan Williams91c00922007-01-02 13:52:30 -07001090 if (rdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001091 if (s->syncing || s->expanding || s->expanded
1092 || s->replacing)
Yufen Yuc911c462020-07-18 05:29:07 -04001093 md_sync_acct(rdev->bdev, RAID5_STRIPE_SECTORS(conf));
Dan Williams91c00922007-01-02 13:52:30 -07001094
Dan Williams2b7497f2008-06-28 08:31:52 +10001095 set_bit(STRIPE_IO_STARTED, &sh->state);
1096
Christoph Hellwig74d46992017-08-23 19:10:32 +02001097 bio_set_dev(bi, rdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001098 bio_set_op_attrs(bi, op, op_flags);
1099 bi->bi_end_io = op_is_write(op)
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001100 ? raid5_end_write_request
1101 : raid5_end_read_request;
1102 bi->bi_private = sh;
1103
Mike Christie6296b962016-06-05 14:32:21 -05001104 pr_debug("%s: for %llu schedule op %d on disc %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001105 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001106 bi->bi_opf, i);
Dan Williams91c00922007-01-02 13:52:30 -07001107 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001108 if (sh != head_sh)
1109 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001110 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001111 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001112 + rdev->new_data_offset);
1113 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001114 bi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001115 + rdev->data_offset);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001116 if (test_bit(R5_ReadNoMerge, &head_sh->dev[i].flags))
Jens Axboe1eff9d32016-08-05 15:35:16 -06001117 bi->bi_opf |= REQ_NOMERGE;
majianpeng3f9e7c12012-07-31 10:04:21 +10001118
Shaohua Lid592a992014-05-21 17:57:44 +08001119 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1120 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
Song Liu86aa1392017-01-12 17:22:41 -08001121
1122 if (!op_is_write(op) &&
1123 test_bit(R5_InJournal, &sh->dev[i].flags))
1124 /*
1125 * issuing read for a page in journal, this
1126 * must be preparing for prexor in rmw; read
1127 * the data into orig_page
1128 */
1129 sh->dev[i].vec.bv_page = sh->dev[i].orig_page;
1130 else
1131 sh->dev[i].vec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001132 bi->bi_vcnt = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04001133 bi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
Yufen Yu7aba13b2020-08-20 09:22:06 -04001134 bi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
Yufen Yuc911c462020-07-18 05:29:07 -04001135 bi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02001136 bi->bi_write_hint = sh->dev[i].write_hint;
1137 if (!rrdev)
Eugene Syromiatnikovf1934892019-09-20 17:58:28 +02001138 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001139 /*
1140 * If this is discard request, set bi_vcnt 0. We don't
1141 * want to confuse SCSI because SCSI will replace payload
1142 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001143 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001144 bi->bi_vcnt = 0;
NeilBrown977df362011-12-23 10:17:53 +11001145 if (rrdev)
1146 set_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001147
1148 if (conf->mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001149 trace_block_bio_remap(bi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001150 bi, disk_devt(conf->mddev->gendisk),
1151 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001152 if (should_defer && op_is_write(op))
1153 bio_list_add(&pending_bios, bi);
1154 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001155 submit_bio_noacct(bi);
NeilBrown977df362011-12-23 10:17:53 +11001156 }
1157 if (rrdev) {
NeilBrown9a3e1102011-12-23 10:17:53 +11001158 if (s->syncing || s->expanding || s->expanded
1159 || s->replacing)
Yufen Yuc911c462020-07-18 05:29:07 -04001160 md_sync_acct(rrdev->bdev, RAID5_STRIPE_SECTORS(conf));
NeilBrown977df362011-12-23 10:17:53 +11001161
1162 set_bit(STRIPE_IO_STARTED, &sh->state);
1163
Christoph Hellwig74d46992017-08-23 19:10:32 +02001164 bio_set_dev(rbi, rrdev->bdev);
Mike Christie796a5cf2016-06-05 14:32:07 -05001165 bio_set_op_attrs(rbi, op, op_flags);
1166 BUG_ON(!op_is_write(op));
Kent Overstreet2f6db2a2012-09-11 12:26:38 -07001167 rbi->bi_end_io = raid5_end_write_request;
1168 rbi->bi_private = sh;
1169
Mike Christie6296b962016-06-05 14:32:21 -05001170 pr_debug("%s: for %llu schedule op %d on "
NeilBrown977df362011-12-23 10:17:53 +11001171 "replacement disc %d\n",
1172 __func__, (unsigned long long)sh->sector,
Jens Axboe1eff9d32016-08-05 15:35:16 -06001173 rbi->bi_opf, i);
NeilBrown977df362011-12-23 10:17:53 +11001174 atomic_inc(&sh->count);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001175 if (sh != head_sh)
1176 atomic_inc(&head_sh->count);
NeilBrown05616be2012-05-21 09:27:00 +10001177 if (use_new_offset(conf, sh))
Kent Overstreet4f024f32013-10-11 15:44:27 -07001178 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001179 + rrdev->new_data_offset);
1180 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001181 rbi->bi_iter.bi_sector = (sh->sector
NeilBrown05616be2012-05-21 09:27:00 +10001182 + rrdev->data_offset);
Shaohua Lid592a992014-05-21 17:57:44 +08001183 if (test_bit(R5_SkipCopy, &sh->dev[i].flags))
1184 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
1185 sh->dev[i].rvec.bv_page = sh->dev[i].page;
Kent Overstreet4997b722013-05-30 08:44:39 +02001186 rbi->bi_vcnt = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04001187 rbi->bi_io_vec[0].bv_len = RAID5_STRIPE_SIZE(conf);
Yufen Yu7aba13b2020-08-20 09:22:06 -04001188 rbi->bi_io_vec[0].bv_offset = sh->dev[i].offset;
Yufen Yuc911c462020-07-18 05:29:07 -04001189 rbi->bi_iter.bi_size = RAID5_STRIPE_SIZE(conf);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02001190 rbi->bi_write_hint = sh->dev[i].write_hint;
Eugene Syromiatnikovf1934892019-09-20 17:58:28 +02001191 sh->dev[i].write_hint = RWH_WRITE_LIFE_NOT_SET;
Shaohua Li37c61ff2013-10-19 14:50:28 +08001192 /*
1193 * If this is discard request, set bi_vcnt 0. We don't
1194 * want to confuse SCSI because SCSI will replace payload
1195 */
Mike Christie796a5cf2016-06-05 14:32:07 -05001196 if (op == REQ_OP_DISCARD)
Shaohua Li37c61ff2013-10-19 14:50:28 +08001197 rbi->bi_vcnt = 0;
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001198 if (conf->mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02001199 trace_block_bio_remap(rbi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06001200 rbi, disk_devt(conf->mddev->gendisk),
1201 sh->dev[i].sector);
Shaohua Liaaf9f122017-03-03 22:06:12 -08001202 if (should_defer && op_is_write(op))
1203 bio_list_add(&pending_bios, rbi);
1204 else
Christoph Hellwiged00aab2020-07-01 10:59:44 +02001205 submit_bio_noacct(rbi);
NeilBrown977df362011-12-23 10:17:53 +11001206 }
1207 if (!rdev && !rrdev) {
Mike Christie796a5cf2016-06-05 14:32:07 -05001208 if (op_is_write(op))
Dan Williams91c00922007-01-02 13:52:30 -07001209 set_bit(STRIPE_DEGRADED, &sh->state);
Mike Christie6296b962016-06-05 14:32:21 -05001210 pr_debug("skip op %d on disc %d for sector %llu\n",
Jens Axboe1eff9d32016-08-05 15:35:16 -06001211 bi->bi_opf, i, (unsigned long long)sh->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001212 clear_bit(R5_LOCKED, &sh->dev[i].flags);
1213 set_bit(STRIPE_HANDLE, &sh->state);
1214 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001215
1216 if (!head_sh->batch_head)
1217 continue;
1218 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1219 batch_list);
1220 if (sh != head_sh)
1221 goto again;
Dan Williams91c00922007-01-02 13:52:30 -07001222 }
Shaohua Liaaf9f122017-03-03 22:06:12 -08001223
1224 if (should_defer && !bio_list_empty(&pending_bios))
1225 defer_issue_bios(conf, head_sh->sector, &pending_bios);
Dan Williams91c00922007-01-02 13:52:30 -07001226}
1227
1228static struct dma_async_tx_descriptor *
Shaohua Lid592a992014-05-21 17:57:44 +08001229async_copy_data(int frombio, struct bio *bio, struct page **page,
Yufen Yu248728d2020-08-20 09:22:07 -04001230 unsigned int poff, sector_t sector, struct dma_async_tx_descriptor *tx,
Song Liu1e6d6902016-11-17 15:24:39 -08001231 struct stripe_head *sh, int no_skipcopy)
Dan Williams91c00922007-01-02 13:52:30 -07001232{
Kent Overstreet79886132013-11-23 17:19:00 -08001233 struct bio_vec bvl;
1234 struct bvec_iter iter;
Dan Williams91c00922007-01-02 13:52:30 -07001235 struct page *bio_page;
Dan Williams91c00922007-01-02 13:52:30 -07001236 int page_offset;
Dan Williamsa08abd82009-06-03 11:43:59 -07001237 struct async_submit_ctl submit;
Dan Williams0403e382009-09-08 17:42:50 -07001238 enum async_tx_flags flags = 0;
Yufen Yuc911c462020-07-18 05:29:07 -04001239 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001240
Kent Overstreet4f024f32013-10-11 15:44:27 -07001241 if (bio->bi_iter.bi_sector >= sector)
1242 page_offset = (signed)(bio->bi_iter.bi_sector - sector) * 512;
Dan Williams91c00922007-01-02 13:52:30 -07001243 else
Kent Overstreet4f024f32013-10-11 15:44:27 -07001244 page_offset = (signed)(sector - bio->bi_iter.bi_sector) * -512;
Dan Williamsa08abd82009-06-03 11:43:59 -07001245
Dan Williams0403e382009-09-08 17:42:50 -07001246 if (frombio)
1247 flags |= ASYNC_TX_FENCE;
1248 init_async_submit(&submit, flags, tx, NULL, NULL, NULL);
1249
Kent Overstreet79886132013-11-23 17:19:00 -08001250 bio_for_each_segment(bvl, bio, iter) {
1251 int len = bvl.bv_len;
Dan Williams91c00922007-01-02 13:52:30 -07001252 int clen;
1253 int b_offset = 0;
1254
1255 if (page_offset < 0) {
1256 b_offset = -page_offset;
1257 page_offset += b_offset;
1258 len -= b_offset;
1259 }
1260
Yufen Yuc911c462020-07-18 05:29:07 -04001261 if (len > 0 && page_offset + len > RAID5_STRIPE_SIZE(conf))
1262 clen = RAID5_STRIPE_SIZE(conf) - page_offset;
Dan Williams91c00922007-01-02 13:52:30 -07001263 else
1264 clen = len;
1265
1266 if (clen > 0) {
Kent Overstreet79886132013-11-23 17:19:00 -08001267 b_offset += bvl.bv_offset;
1268 bio_page = bvl.bv_page;
Shaohua Lid592a992014-05-21 17:57:44 +08001269 if (frombio) {
Yufen Yuc911c462020-07-18 05:29:07 -04001270 if (conf->skip_copy &&
Shaohua Lid592a992014-05-21 17:57:44 +08001271 b_offset == 0 && page_offset == 0 &&
Yufen Yuc911c462020-07-18 05:29:07 -04001272 clen == RAID5_STRIPE_SIZE(conf) &&
Song Liu1e6d6902016-11-17 15:24:39 -08001273 !no_skipcopy)
Shaohua Lid592a992014-05-21 17:57:44 +08001274 *page = bio_page;
1275 else
Yufen Yu248728d2020-08-20 09:22:07 -04001276 tx = async_memcpy(*page, bio_page, page_offset + poff,
Dan Williamsa08abd82009-06-03 11:43:59 -07001277 b_offset, clen, &submit);
Shaohua Lid592a992014-05-21 17:57:44 +08001278 } else
1279 tx = async_memcpy(bio_page, *page, b_offset,
Yufen Yu248728d2020-08-20 09:22:07 -04001280 page_offset + poff, clen, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001281 }
Dan Williamsa08abd82009-06-03 11:43:59 -07001282 /* chain the operations */
1283 submit.depend_tx = tx;
1284
Dan Williams91c00922007-01-02 13:52:30 -07001285 if (clen < len) /* hit end of page */
1286 break;
1287 page_offset += len;
1288 }
1289
1290 return tx;
1291}
1292
1293static void ops_complete_biofill(void *stripe_head_ref)
1294{
1295 struct stripe_head *sh = stripe_head_ref;
Dan Williamse4d84902007-09-24 10:06:13 -07001296 int i;
Yufen Yuc911c462020-07-18 05:29:07 -04001297 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001298
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001299 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001300 (unsigned long long)sh->sector);
1301
1302 /* clear completed biofills */
1303 for (i = sh->disks; i--; ) {
1304 struct r5dev *dev = &sh->dev[i];
Dan Williams91c00922007-01-02 13:52:30 -07001305
1306 /* acknowledge completion of a biofill operation */
Dan Williamse4d84902007-09-24 10:06:13 -07001307 /* and check if we need to reply to a read request,
1308 * new R5_Wantfill requests are held off until
Dan Williams83de75c2008-06-28 08:31:58 +10001309 * !STRIPE_BIOFILL_RUN
Dan Williamse4d84902007-09-24 10:06:13 -07001310 */
1311 if (test_and_clear_bit(R5_Wantfill, &dev->flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001312 struct bio *rbi, *rbi2;
Dan Williams91c00922007-01-02 13:52:30 -07001313
Dan Williams91c00922007-01-02 13:52:30 -07001314 BUG_ON(!dev->read);
1315 rbi = dev->read;
1316 dev->read = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07001317 while (rbi && rbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001318 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
1319 rbi2 = r5_next_bio(conf, rbi, dev->sector);
NeilBrown016c76a2017-03-15 14:05:13 +11001320 bio_endio(rbi);
Dan Williams91c00922007-01-02 13:52:30 -07001321 rbi = rbi2;
1322 }
1323 }
1324 }
Dan Williams83de75c2008-06-28 08:31:58 +10001325 clear_bit(STRIPE_BIOFILL_RUN, &sh->state);
Dan Williams91c00922007-01-02 13:52:30 -07001326
Dan Williamse4d84902007-09-24 10:06:13 -07001327 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001328 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001329}
1330
1331static void ops_run_biofill(struct stripe_head *sh)
1332{
1333 struct dma_async_tx_descriptor *tx = NULL;
Dan Williamsa08abd82009-06-03 11:43:59 -07001334 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001335 int i;
Yufen Yuc911c462020-07-18 05:29:07 -04001336 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001337
shli@kernel.org59fc6302014-12-15 12:57:03 +11001338 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001339 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001340 (unsigned long long)sh->sector);
1341
1342 for (i = sh->disks; i--; ) {
1343 struct r5dev *dev = &sh->dev[i];
1344 if (test_bit(R5_Wantfill, &dev->flags)) {
1345 struct bio *rbi;
Shaohua Lib17459c2012-07-19 16:01:31 +10001346 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001347 dev->read = rbi = dev->toread;
1348 dev->toread = NULL;
Shaohua Lib17459c2012-07-19 16:01:31 +10001349 spin_unlock_irq(&sh->stripe_lock);
Kent Overstreet4f024f32013-10-11 15:44:27 -07001350 while (rbi && rbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001351 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001352 tx = async_copy_data(0, rbi, &dev->page,
Yufen Yu248728d2020-08-20 09:22:07 -04001353 dev->offset,
Song Liu1e6d6902016-11-17 15:24:39 -08001354 dev->sector, tx, sh, 0);
Yufen Yuc911c462020-07-18 05:29:07 -04001355 rbi = r5_next_bio(conf, rbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001356 }
1357 }
1358 }
1359
1360 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07001361 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_biofill, sh, NULL);
1362 async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07001363}
1364
Dan Williams4e7d2c02009-08-29 19:13:11 -07001365static void mark_target_uptodate(struct stripe_head *sh, int target)
1366{
1367 struct r5dev *tgt;
1368
1369 if (target < 0)
1370 return;
1371
1372 tgt = &sh->dev[target];
1373 set_bit(R5_UPTODATE, &tgt->flags);
1374 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1375 clear_bit(R5_Wantcompute, &tgt->flags);
1376}
1377
Dan Williamsac6b53b2009-07-14 13:40:19 -07001378static void ops_complete_compute(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001379{
1380 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07001381
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001382 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001383 (unsigned long long)sh->sector);
1384
Dan Williamsac6b53b2009-07-14 13:40:19 -07001385 /* mark the computed target(s) as uptodate */
Dan Williams4e7d2c02009-08-29 19:13:11 -07001386 mark_target_uptodate(sh, sh->ops.target);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001387 mark_target_uptodate(sh, sh->ops.target2);
Dan Williams4e7d2c02009-08-29 19:13:11 -07001388
Dan Williamsecc65c92008-06-28 08:31:57 +10001389 clear_bit(STRIPE_COMPUTE_RUN, &sh->state);
1390 if (sh->check_state == check_state_compute_run)
1391 sh->check_state = check_state_compute_result;
Dan Williams91c00922007-01-02 13:52:30 -07001392 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001393 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001394}
1395
Dan Williamsd6f38f32009-07-14 11:50:52 -07001396/* return a pointer to the address conversion region of the scribble buffer */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001397static struct page **to_addr_page(struct raid5_percpu *percpu, int i)
Dan Williams91c00922007-01-02 13:52:30 -07001398{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001399 return percpu->scribble + i * percpu->scribble_obj_size;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001400}
1401
1402/* return a pointer to the address conversion region of the scribble buffer */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001403static addr_conv_t *to_addr_conv(struct stripe_head *sh,
1404 struct raid5_percpu *percpu, int i)
shli@kernel.org46d5b782014-12-15 12:57:02 +11001405{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07001406 return (void *) (to_addr_page(percpu, i) + sh->disks + 2);
Dan Williamsd6f38f32009-07-14 11:50:52 -07001407}
1408
Yufen Yu7aba13b2020-08-20 09:22:06 -04001409/*
1410 * Return a pointer to record offset address.
1411 */
1412static unsigned int *
1413to_addr_offs(struct stripe_head *sh, struct raid5_percpu *percpu)
1414{
1415 return (unsigned int *) (to_addr_conv(sh, percpu, 0) + sh->disks + 2);
1416}
1417
Dan Williamsd6f38f32009-07-14 11:50:52 -07001418static struct dma_async_tx_descriptor *
1419ops_run_compute5(struct stripe_head *sh, struct raid5_percpu *percpu)
1420{
Dan Williams91c00922007-01-02 13:52:30 -07001421 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001422 struct page **xor_srcs = to_addr_page(percpu, 0);
Yufen Yu7aba13b2020-08-20 09:22:06 -04001423 unsigned int *off_srcs = to_addr_offs(sh, percpu);
Dan Williams91c00922007-01-02 13:52:30 -07001424 int target = sh->ops.target;
1425 struct r5dev *tgt = &sh->dev[target];
1426 struct page *xor_dest = tgt->page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001427 unsigned int off_dest = tgt->offset;
Dan Williams91c00922007-01-02 13:52:30 -07001428 int count = 0;
1429 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07001430 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001431 int i;
1432
shli@kernel.org59fc6302014-12-15 12:57:03 +11001433 BUG_ON(sh->batch_head);
1434
Dan Williams91c00922007-01-02 13:52:30 -07001435 pr_debug("%s: stripe %llu block: %d\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001436 __func__, (unsigned long long)sh->sector, target);
Dan Williams91c00922007-01-02 13:52:30 -07001437 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1438
Yufen Yu7aba13b2020-08-20 09:22:06 -04001439 for (i = disks; i--; ) {
1440 if (i != target) {
1441 off_srcs[count] = sh->dev[i].offset;
Dan Williams91c00922007-01-02 13:52:30 -07001442 xor_srcs[count++] = sh->dev[i].page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001443 }
1444 }
Dan Williams91c00922007-01-02 13:52:30 -07001445
1446 atomic_inc(&sh->count);
1447
Dan Williams0403e382009-09-08 17:42:50 -07001448 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001449 ops_complete_compute, sh, to_addr_conv(sh, percpu, 0));
Dan Williams91c00922007-01-02 13:52:30 -07001450 if (unlikely(count == 1))
Yufen Yu7aba13b2020-08-20 09:22:06 -04001451 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
Yufen Yuc911c462020-07-18 05:29:07 -04001452 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001453 else
Yufen Yuc911c462020-07-18 05:29:07 -04001454 tx = async_xor(xor_dest, xor_srcs, 0, count,
1455 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001456
Dan Williams91c00922007-01-02 13:52:30 -07001457 return tx;
1458}
1459
Dan Williamsac6b53b2009-07-14 13:40:19 -07001460/* set_syndrome_sources - populate source buffers for gen_syndrome
1461 * @srcs - (struct page *) array of size sh->disks
1462 * @sh - stripe_head to parse
1463 *
1464 * Populates srcs in proper layout order for the stripe and returns the
1465 * 'count' of sources to be used in a call to async_gen_syndrome. The P
1466 * destination buffer is recorded in srcs[count] and the Q destination
1467 * is recorded in srcs[count+1]].
1468 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001469static int set_syndrome_sources(struct page **srcs,
1470 struct stripe_head *sh,
1471 int srctype)
Dan Williamsac6b53b2009-07-14 13:40:19 -07001472{
1473 int disks = sh->disks;
1474 int syndrome_disks = sh->ddf_layout ? disks : (disks - 2);
1475 int d0_idx = raid6_d0(sh);
1476 int count;
1477 int i;
1478
1479 for (i = 0; i < disks; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001480 srcs[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001481
1482 count = 0;
1483 i = d0_idx;
1484 do {
1485 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001486 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001487
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001488 if (i == sh->qd_idx || i == sh->pd_idx ||
1489 (srctype == SYNDROME_SRC_ALL) ||
1490 (srctype == SYNDROME_SRC_WANT_DRAIN &&
Song Liu1e6d6902016-11-17 15:24:39 -08001491 (test_bit(R5_Wantdrain, &dev->flags) ||
1492 test_bit(R5_InJournal, &dev->flags))) ||
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001493 (srctype == SYNDROME_SRC_WRITTEN &&
Song Liu09777622017-03-13 13:44:35 -07001494 (dev->written ||
1495 test_bit(R5_InJournal, &dev->flags)))) {
Song Liu1e6d6902016-11-17 15:24:39 -08001496 if (test_bit(R5_InJournal, &dev->flags))
1497 srcs[slot] = sh->dev[i].orig_page;
1498 else
1499 srcs[slot] = sh->dev[i].page;
1500 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001501 i = raid6_next_disk(i, disks);
1502 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001503
NeilBrowne4424fe2009-10-16 16:27:34 +11001504 return syndrome_disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001505}
1506
1507static struct dma_async_tx_descriptor *
1508ops_run_compute6_1(struct stripe_head *sh, struct raid5_percpu *percpu)
1509{
1510 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001511 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001512 int target;
1513 int qd_idx = sh->qd_idx;
1514 struct dma_async_tx_descriptor *tx;
1515 struct async_submit_ctl submit;
1516 struct r5dev *tgt;
1517 struct page *dest;
1518 int i;
1519 int count;
1520
shli@kernel.org59fc6302014-12-15 12:57:03 +11001521 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001522 if (sh->ops.target < 0)
1523 target = sh->ops.target2;
1524 else if (sh->ops.target2 < 0)
1525 target = sh->ops.target;
1526 else
1527 /* we should only have one valid target */
1528 BUG();
1529 BUG_ON(target < 0);
1530 pr_debug("%s: stripe %llu block: %d\n",
1531 __func__, (unsigned long long)sh->sector, target);
1532
1533 tgt = &sh->dev[target];
1534 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1535 dest = tgt->page;
1536
1537 atomic_inc(&sh->count);
1538
1539 if (target == qd_idx) {
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001540 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001541 blocks[count] = NULL; /* regenerating p is not necessary */
1542 BUG_ON(blocks[count+1] != dest); /* q should already be set */
Dan Williams0403e382009-09-08 17:42:50 -07001543 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1544 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001545 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001546 tx = async_gen_syndrome(blocks, 0, count+2,
1547 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001548 } else {
1549 /* Compute any data- or p-drive using XOR */
1550 count = 0;
1551 for (i = disks; i-- ; ) {
1552 if (i == target || i == qd_idx)
1553 continue;
1554 blocks[count++] = sh->dev[i].page;
1555 }
1556
Dan Williams0403e382009-09-08 17:42:50 -07001557 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1558 NULL, ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001559 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001560 tx = async_xor(dest, blocks, 0, count,
1561 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001562 }
1563
1564 return tx;
1565}
1566
1567static struct dma_async_tx_descriptor *
1568ops_run_compute6_2(struct stripe_head *sh, struct raid5_percpu *percpu)
1569{
1570 int i, count, disks = sh->disks;
1571 int syndrome_disks = sh->ddf_layout ? disks : disks-2;
1572 int d0_idx = raid6_d0(sh);
1573 int faila = -1, failb = -1;
1574 int target = sh->ops.target;
1575 int target2 = sh->ops.target2;
1576 struct r5dev *tgt = &sh->dev[target];
1577 struct r5dev *tgt2 = &sh->dev[target2];
1578 struct dma_async_tx_descriptor *tx;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001579 struct page **blocks = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001580 struct async_submit_ctl submit;
1581
shli@kernel.org59fc6302014-12-15 12:57:03 +11001582 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001583 pr_debug("%s: stripe %llu block1: %d block2: %d\n",
1584 __func__, (unsigned long long)sh->sector, target, target2);
1585 BUG_ON(target < 0 || target2 < 0);
1586 BUG_ON(!test_bit(R5_Wantcompute, &tgt->flags));
1587 BUG_ON(!test_bit(R5_Wantcompute, &tgt2->flags));
1588
Dan Williams6c910a72009-09-16 12:24:54 -07001589 /* we need to open-code set_syndrome_sources to handle the
Dan Williamsac6b53b2009-07-14 13:40:19 -07001590 * slot number conversion for 'faila' and 'failb'
1591 */
1592 for (i = 0; i < disks ; i++)
NeilBrown5dd33c92009-10-16 16:40:25 +11001593 blocks[i] = NULL;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001594 count = 0;
1595 i = d0_idx;
1596 do {
1597 int slot = raid6_idx_to_slot(i, sh, &count, syndrome_disks);
1598
1599 blocks[slot] = sh->dev[i].page;
1600
1601 if (i == target)
1602 faila = slot;
1603 if (i == target2)
1604 failb = slot;
1605 i = raid6_next_disk(i, disks);
1606 } while (i != d0_idx);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001607
1608 BUG_ON(faila == failb);
1609 if (failb < faila)
1610 swap(faila, failb);
1611 pr_debug("%s: stripe: %llu faila: %d failb: %d\n",
1612 __func__, (unsigned long long)sh->sector, faila, failb);
1613
1614 atomic_inc(&sh->count);
1615
1616 if (failb == syndrome_disks+1) {
1617 /* Q disk is one of the missing disks */
1618 if (faila == syndrome_disks) {
1619 /* Missing P+Q, just recompute */
Dan Williams0403e382009-09-08 17:42:50 -07001620 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1621 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001622 to_addr_conv(sh, percpu, 0));
NeilBrowne4424fe2009-10-16 16:27:34 +11001623 return async_gen_syndrome(blocks, 0, syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001624 RAID5_STRIPE_SIZE(sh->raid_conf),
1625 &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001626 } else {
1627 struct page *dest;
1628 int data_target;
1629 int qd_idx = sh->qd_idx;
1630
1631 /* Missing D+Q: recompute D from P, then recompute Q */
1632 if (target == qd_idx)
1633 data_target = target2;
1634 else
1635 data_target = target;
1636
1637 count = 0;
1638 for (i = disks; i-- ; ) {
1639 if (i == data_target || i == qd_idx)
1640 continue;
1641 blocks[count++] = sh->dev[i].page;
1642 }
1643 dest = sh->dev[data_target].page;
Dan Williams0403e382009-09-08 17:42:50 -07001644 init_async_submit(&submit,
1645 ASYNC_TX_FENCE|ASYNC_TX_XOR_ZERO_DST,
1646 NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001647 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001648 tx = async_xor(dest, blocks, 0, count,
1649 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsac6b53b2009-07-14 13:40:19 -07001650 &submit);
1651
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001652 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_ALL);
Dan Williams0403e382009-09-08 17:42:50 -07001653 init_async_submit(&submit, ASYNC_TX_FENCE, tx,
1654 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001655 to_addr_conv(sh, percpu, 0));
Dan Williamsac6b53b2009-07-14 13:40:19 -07001656 return async_gen_syndrome(blocks, 0, count+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001657 RAID5_STRIPE_SIZE(sh->raid_conf),
1658 &submit);
Dan Williamsac6b53b2009-07-14 13:40:19 -07001659 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001660 } else {
Dan Williams6c910a72009-09-16 12:24:54 -07001661 init_async_submit(&submit, ASYNC_TX_FENCE, NULL,
1662 ops_complete_compute, sh,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001663 to_addr_conv(sh, percpu, 0));
Dan Williams6c910a72009-09-16 12:24:54 -07001664 if (failb == syndrome_disks) {
1665 /* We're missing D+P. */
1666 return async_raid6_datap_recov(syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001667 RAID5_STRIPE_SIZE(sh->raid_conf),
1668 faila,
1669 blocks, &submit);
Dan Williams6c910a72009-09-16 12:24:54 -07001670 } else {
1671 /* We're missing D+D. */
1672 return async_raid6_2data_recov(syndrome_disks+2,
Yufen Yuc911c462020-07-18 05:29:07 -04001673 RAID5_STRIPE_SIZE(sh->raid_conf),
1674 faila, failb,
1675 blocks, &submit);
Dan Williams6c910a72009-09-16 12:24:54 -07001676 }
Dan Williamsac6b53b2009-07-14 13:40:19 -07001677 }
1678}
1679
Dan Williams91c00922007-01-02 13:52:30 -07001680static void ops_complete_prexor(void *stripe_head_ref)
1681{
1682 struct stripe_head *sh = stripe_head_ref;
1683
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001684 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001685 (unsigned long long)sh->sector);
Song Liu1e6d6902016-11-17 15:24:39 -08001686
1687 if (r5c_is_writeback(sh->raid_conf->log))
1688 /*
1689 * raid5-cache write back uses orig_page during prexor.
1690 * After prexor, it is time to free orig_page
1691 */
1692 r5c_release_extra_page(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001693}
1694
1695static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001696ops_run_prexor5(struct stripe_head *sh, struct raid5_percpu *percpu,
1697 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001698{
Dan Williams91c00922007-01-02 13:52:30 -07001699 int disks = sh->disks;
shli@kernel.org46d5b782014-12-15 12:57:02 +11001700 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07001701 int count = 0, pd_idx = sh->pd_idx, i;
Dan Williamsa08abd82009-06-03 11:43:59 -07001702 struct async_submit_ctl submit;
Dan Williams91c00922007-01-02 13:52:30 -07001703
1704 /* existing parity data subtracted */
1705 struct page *xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1706
shli@kernel.org59fc6302014-12-15 12:57:03 +11001707 BUG_ON(sh->batch_head);
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001708 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001709 (unsigned long long)sh->sector);
1710
1711 for (i = disks; i--; ) {
1712 struct r5dev *dev = &sh->dev[i];
1713 /* Only process blocks that are known to be uptodate */
Song Liu1e6d6902016-11-17 15:24:39 -08001714 if (test_bit(R5_InJournal, &dev->flags))
1715 xor_srcs[count++] = dev->orig_page;
1716 else if (test_bit(R5_Wantdrain, &dev->flags))
Dan Williams91c00922007-01-02 13:52:30 -07001717 xor_srcs[count++] = dev->page;
1718 }
1719
Dan Williams0403e382009-09-08 17:42:50 -07001720 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_XOR_DROP_DST, tx,
shli@kernel.org46d5b782014-12-15 12:57:02 +11001721 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001722 tx = async_xor(xor_dest, xor_srcs, 0, count,
1723 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williams91c00922007-01-02 13:52:30 -07001724
1725 return tx;
1726}
1727
1728static struct dma_async_tx_descriptor *
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001729ops_run_prexor6(struct stripe_head *sh, struct raid5_percpu *percpu,
1730 struct dma_async_tx_descriptor *tx)
1731{
1732 struct page **blocks = to_addr_page(percpu, 0);
1733 int count;
1734 struct async_submit_ctl submit;
1735
1736 pr_debug("%s: stripe %llu\n", __func__,
1737 (unsigned long long)sh->sector);
1738
1739 count = set_syndrome_sources(blocks, sh, SYNDROME_SRC_WANT_DRAIN);
1740
1741 init_async_submit(&submit, ASYNC_TX_FENCE|ASYNC_TX_PQ_XOR_DST, tx,
1742 ops_complete_prexor, sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04001743 tx = async_gen_syndrome(blocks, 0, count+2,
1744 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001745
1746 return tx;
1747}
1748
1749static struct dma_async_tx_descriptor *
Dan Williamsd8ee0722008-06-28 08:32:06 +10001750ops_run_biodrain(struct stripe_head *sh, struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001751{
Song Liu1e6d6902016-11-17 15:24:39 -08001752 struct r5conf *conf = sh->raid_conf;
Dan Williams91c00922007-01-02 13:52:30 -07001753 int disks = sh->disks;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001754 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001755 struct stripe_head *head_sh = sh;
Dan Williams91c00922007-01-02 13:52:30 -07001756
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001757 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001758 (unsigned long long)sh->sector);
1759
1760 for (i = disks; i--; ) {
shli@kernel.org59fc6302014-12-15 12:57:03 +11001761 struct r5dev *dev;
Dan Williams91c00922007-01-02 13:52:30 -07001762 struct bio *chosen;
Dan Williams91c00922007-01-02 13:52:30 -07001763
shli@kernel.org59fc6302014-12-15 12:57:03 +11001764 sh = head_sh;
1765 if (test_and_clear_bit(R5_Wantdrain, &head_sh->dev[i].flags)) {
Dan Williams91c00922007-01-02 13:52:30 -07001766 struct bio *wbi;
1767
shli@kernel.org59fc6302014-12-15 12:57:03 +11001768again:
1769 dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001770 /*
1771 * clear R5_InJournal, so when rewriting a page in
1772 * journal, it is not skipped by r5l_log_stripe()
1773 */
1774 clear_bit(R5_InJournal, &dev->flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10001775 spin_lock_irq(&sh->stripe_lock);
Dan Williams91c00922007-01-02 13:52:30 -07001776 chosen = dev->towrite;
1777 dev->towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11001778 sh->overwrite_disks = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001779 BUG_ON(dev->written);
1780 wbi = dev->written = chosen;
Shaohua Lib17459c2012-07-19 16:01:31 +10001781 spin_unlock_irq(&sh->stripe_lock);
Shaohua Lid592a992014-05-21 17:57:44 +08001782 WARN_ON(dev->page != dev->orig_page);
Dan Williams91c00922007-01-02 13:52:30 -07001783
Kent Overstreet4f024f32013-10-11 15:44:27 -07001784 while (wbi && wbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04001785 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
Jens Axboe1eff9d32016-08-05 15:35:16 -06001786 if (wbi->bi_opf & REQ_FUA)
Tejun Heoe9c74692010-09-03 11:56:18 +02001787 set_bit(R5_WantFUA, &dev->flags);
Jens Axboe1eff9d32016-08-05 15:35:16 -06001788 if (wbi->bi_opf & REQ_SYNC)
Shaohua Libc0934f2012-05-22 13:55:05 +10001789 set_bit(R5_SyncIO, &dev->flags);
Mike Christie796a5cf2016-06-05 14:32:07 -05001790 if (bio_op(wbi) == REQ_OP_DISCARD)
Shaohua Li620125f2012-10-11 13:49:05 +11001791 set_bit(R5_Discard, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08001792 else {
1793 tx = async_copy_data(1, wbi, &dev->page,
Yufen Yu248728d2020-08-20 09:22:07 -04001794 dev->offset,
Song Liu1e6d6902016-11-17 15:24:39 -08001795 dev->sector, tx, sh,
1796 r5c_is_writeback(conf->log));
1797 if (dev->page != dev->orig_page &&
1798 !r5c_is_writeback(conf->log)) {
Shaohua Lid592a992014-05-21 17:57:44 +08001799 set_bit(R5_SkipCopy, &dev->flags);
1800 clear_bit(R5_UPTODATE, &dev->flags);
1801 clear_bit(R5_OVERWRITE, &dev->flags);
1802 }
1803 }
Yufen Yuc911c462020-07-18 05:29:07 -04001804 wbi = r5_next_bio(conf, wbi, dev->sector);
Dan Williams91c00922007-01-02 13:52:30 -07001805 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001806
1807 if (head_sh->batch_head) {
1808 sh = list_first_entry(&sh->batch_list,
1809 struct stripe_head,
1810 batch_list);
1811 if (sh == head_sh)
1812 continue;
1813 goto again;
1814 }
Dan Williams91c00922007-01-02 13:52:30 -07001815 }
1816 }
1817
1818 return tx;
1819}
1820
Dan Williamsac6b53b2009-07-14 13:40:19 -07001821static void ops_complete_reconstruct(void *stripe_head_ref)
Dan Williams91c00922007-01-02 13:52:30 -07001822{
1823 struct stripe_head *sh = stripe_head_ref;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001824 int disks = sh->disks;
1825 int pd_idx = sh->pd_idx;
1826 int qd_idx = sh->qd_idx;
1827 int i;
Shaohua Li9e4447682012-10-11 13:49:49 +11001828 bool fua = false, sync = false, discard = false;
Dan Williams91c00922007-01-02 13:52:30 -07001829
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001830 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001831 (unsigned long long)sh->sector);
1832
Shaohua Libc0934f2012-05-22 13:55:05 +10001833 for (i = disks; i--; ) {
Tejun Heoe9c74692010-09-03 11:56:18 +02001834 fua |= test_bit(R5_WantFUA, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001835 sync |= test_bit(R5_SyncIO, &sh->dev[i].flags);
Shaohua Li9e4447682012-10-11 13:49:49 +11001836 discard |= test_bit(R5_Discard, &sh->dev[i].flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001837 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001838
Dan Williams91c00922007-01-02 13:52:30 -07001839 for (i = disks; i--; ) {
1840 struct r5dev *dev = &sh->dev[i];
Dan Williamsac6b53b2009-07-14 13:40:19 -07001841
Tejun Heoe9c74692010-09-03 11:56:18 +02001842 if (dev->written || i == pd_idx || i == qd_idx) {
NeilBrown235b6002017-10-17 16:18:36 +11001843 if (!discard && !test_bit(R5_SkipCopy, &dev->flags)) {
Shaohua Li9e4447682012-10-11 13:49:49 +11001844 set_bit(R5_UPTODATE, &dev->flags);
NeilBrown235b6002017-10-17 16:18:36 +11001845 if (test_bit(STRIPE_EXPAND_READY, &sh->state))
1846 set_bit(R5_Expanded, &dev->flags);
1847 }
Tejun Heoe9c74692010-09-03 11:56:18 +02001848 if (fua)
1849 set_bit(R5_WantFUA, &dev->flags);
Shaohua Libc0934f2012-05-22 13:55:05 +10001850 if (sync)
1851 set_bit(R5_SyncIO, &dev->flags);
Tejun Heoe9c74692010-09-03 11:56:18 +02001852 }
Dan Williams91c00922007-01-02 13:52:30 -07001853 }
1854
Dan Williamsd8ee0722008-06-28 08:32:06 +10001855 if (sh->reconstruct_state == reconstruct_state_drain_run)
1856 sh->reconstruct_state = reconstruct_state_drain_result;
1857 else if (sh->reconstruct_state == reconstruct_state_prexor_drain_run)
1858 sh->reconstruct_state = reconstruct_state_prexor_drain_result;
1859 else {
1860 BUG_ON(sh->reconstruct_state != reconstruct_state_run);
1861 sh->reconstruct_state = reconstruct_state_result;
1862 }
Dan Williams91c00922007-01-02 13:52:30 -07001863
1864 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07001865 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07001866}
1867
1868static void
Dan Williamsac6b53b2009-07-14 13:40:19 -07001869ops_run_reconstruct5(struct stripe_head *sh, struct raid5_percpu *percpu,
1870 struct dma_async_tx_descriptor *tx)
Dan Williams91c00922007-01-02 13:52:30 -07001871{
Dan Williams91c00922007-01-02 13:52:30 -07001872 int disks = sh->disks;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001873 struct page **xor_srcs;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001874 unsigned int *off_srcs;
Dan Williamsa08abd82009-06-03 11:43:59 -07001875 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001876 int count, pd_idx = sh->pd_idx, i;
Dan Williams91c00922007-01-02 13:52:30 -07001877 struct page *xor_dest;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001878 unsigned int off_dest;
Dan Williamsd8ee0722008-06-28 08:32:06 +10001879 int prexor = 0;
Dan Williams91c00922007-01-02 13:52:30 -07001880 unsigned long flags;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001881 int j = 0;
1882 struct stripe_head *head_sh = sh;
1883 int last_stripe;
Dan Williams91c00922007-01-02 13:52:30 -07001884
Harvey Harrisone46b272b2008-04-28 02:15:50 -07001885 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07001886 (unsigned long long)sh->sector);
1887
Shaohua Li620125f2012-10-11 13:49:05 +11001888 for (i = 0; i < sh->disks; i++) {
1889 if (pd_idx == i)
1890 continue;
1891 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1892 break;
1893 }
1894 if (i >= sh->disks) {
1895 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001896 set_bit(R5_Discard, &sh->dev[pd_idx].flags);
1897 ops_complete_reconstruct(sh);
1898 return;
1899 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11001900again:
1901 count = 0;
1902 xor_srcs = to_addr_page(percpu, j);
Yufen Yu7aba13b2020-08-20 09:22:06 -04001903 off_srcs = to_addr_offs(sh, percpu);
Dan Williams91c00922007-01-02 13:52:30 -07001904 /* check if prexor is active which means only process blocks
1905 * that are part of a read-modify-write (written)
1906 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001907 if (head_sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10001908 prexor = 1;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001909 off_dest = off_srcs[count] = sh->dev[pd_idx].offset;
Dan Williams91c00922007-01-02 13:52:30 -07001910 xor_dest = xor_srcs[count++] = sh->dev[pd_idx].page;
1911 for (i = disks; i--; ) {
1912 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08001913 if (head_sh->dev[i].written ||
Yufen Yu7aba13b2020-08-20 09:22:06 -04001914 test_bit(R5_InJournal, &head_sh->dev[i].flags)) {
1915 off_srcs[count] = dev->offset;
Dan Williams91c00922007-01-02 13:52:30 -07001916 xor_srcs[count++] = dev->page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001917 }
Dan Williams91c00922007-01-02 13:52:30 -07001918 }
1919 } else {
1920 xor_dest = sh->dev[pd_idx].page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001921 off_dest = sh->dev[pd_idx].offset;
Dan Williams91c00922007-01-02 13:52:30 -07001922 for (i = disks; i--; ) {
1923 struct r5dev *dev = &sh->dev[i];
Yufen Yu7aba13b2020-08-20 09:22:06 -04001924 if (i != pd_idx) {
1925 off_srcs[count] = dev->offset;
Dan Williams91c00922007-01-02 13:52:30 -07001926 xor_srcs[count++] = dev->page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04001927 }
Dan Williams91c00922007-01-02 13:52:30 -07001928 }
1929 }
1930
Dan Williams91c00922007-01-02 13:52:30 -07001931 /* 1/ if we prexor'd then the dest is reused as a source
1932 * 2/ if we did not prexor then we are redoing the parity
1933 * set ASYNC_TX_XOR_DROP_DST and ASYNC_TX_XOR_ZERO_DST
1934 * for the synchronous xor case
1935 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11001936 last_stripe = !head_sh->batch_head ||
1937 list_first_entry(&sh->batch_list,
1938 struct stripe_head, batch_list) == head_sh;
1939 if (last_stripe) {
1940 flags = ASYNC_TX_ACK |
1941 (prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST);
Dan Williams91c00922007-01-02 13:52:30 -07001942
shli@kernel.org59fc6302014-12-15 12:57:03 +11001943 atomic_inc(&head_sh->count);
1944 init_async_submit(&submit, flags, tx, ops_complete_reconstruct, head_sh,
1945 to_addr_conv(sh, percpu, j));
1946 } else {
1947 flags = prexor ? ASYNC_TX_XOR_DROP_DST : ASYNC_TX_XOR_ZERO_DST;
1948 init_async_submit(&submit, flags, tx, NULL, NULL,
1949 to_addr_conv(sh, percpu, j));
1950 }
Dan Williams91c00922007-01-02 13:52:30 -07001951
Dan Williamsa08abd82009-06-03 11:43:59 -07001952 if (unlikely(count == 1))
Yufen Yu7aba13b2020-08-20 09:22:06 -04001953 tx = async_memcpy(xor_dest, xor_srcs[0], off_dest, off_srcs[0],
Yufen Yuc911c462020-07-18 05:29:07 -04001954 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
Dan Williamsa08abd82009-06-03 11:43:59 -07001955 else
Yufen Yuc911c462020-07-18 05:29:07 -04001956 tx = async_xor(xor_dest, xor_srcs, 0, count,
1957 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11001958 if (!last_stripe) {
1959 j++;
1960 sh = list_first_entry(&sh->batch_list, struct stripe_head,
1961 batch_list);
1962 goto again;
1963 }
Dan Williams91c00922007-01-02 13:52:30 -07001964}
1965
Dan Williamsac6b53b2009-07-14 13:40:19 -07001966static void
1967ops_run_reconstruct6(struct stripe_head *sh, struct raid5_percpu *percpu,
1968 struct dma_async_tx_descriptor *tx)
1969{
1970 struct async_submit_ctl submit;
shli@kernel.org59fc6302014-12-15 12:57:03 +11001971 struct page **blocks;
1972 int count, i, j = 0;
1973 struct stripe_head *head_sh = sh;
1974 int last_stripe;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001975 int synflags;
1976 unsigned long txflags;
Dan Williamsac6b53b2009-07-14 13:40:19 -07001977
1978 pr_debug("%s: stripe %llu\n", __func__, (unsigned long long)sh->sector);
1979
Shaohua Li620125f2012-10-11 13:49:05 +11001980 for (i = 0; i < sh->disks; i++) {
1981 if (sh->pd_idx == i || sh->qd_idx == i)
1982 continue;
1983 if (!test_bit(R5_Discard, &sh->dev[i].flags))
1984 break;
1985 }
1986 if (i >= sh->disks) {
1987 atomic_inc(&sh->count);
Shaohua Li620125f2012-10-11 13:49:05 +11001988 set_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
1989 set_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
1990 ops_complete_reconstruct(sh);
1991 return;
1992 }
1993
shli@kernel.org59fc6302014-12-15 12:57:03 +11001994again:
1995 blocks = to_addr_page(percpu, j);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11001996
1997 if (sh->reconstruct_state == reconstruct_state_prexor_drain_run) {
1998 synflags = SYNDROME_SRC_WRITTEN;
1999 txflags = ASYNC_TX_ACK | ASYNC_TX_PQ_XOR_DST;
2000 } else {
2001 synflags = SYNDROME_SRC_ALL;
2002 txflags = ASYNC_TX_ACK;
2003 }
2004
2005 count = set_syndrome_sources(blocks, sh, synflags);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002006 last_stripe = !head_sh->batch_head ||
2007 list_first_entry(&sh->batch_list,
2008 struct stripe_head, batch_list) == head_sh;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002009
shli@kernel.org59fc6302014-12-15 12:57:03 +11002010 if (last_stripe) {
2011 atomic_inc(&head_sh->count);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002012 init_async_submit(&submit, txflags, tx, ops_complete_reconstruct,
shli@kernel.org59fc6302014-12-15 12:57:03 +11002013 head_sh, to_addr_conv(sh, percpu, j));
2014 } else
2015 init_async_submit(&submit, 0, tx, NULL, NULL,
2016 to_addr_conv(sh, percpu, j));
Yufen Yuc911c462020-07-18 05:29:07 -04002017 tx = async_gen_syndrome(blocks, 0, count+2,
2018 RAID5_STRIPE_SIZE(sh->raid_conf), &submit);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002019 if (!last_stripe) {
2020 j++;
2021 sh = list_first_entry(&sh->batch_list, struct stripe_head,
2022 batch_list);
2023 goto again;
2024 }
Dan Williams91c00922007-01-02 13:52:30 -07002025}
2026
2027static void ops_complete_check(void *stripe_head_ref)
2028{
2029 struct stripe_head *sh = stripe_head_ref;
Dan Williams91c00922007-01-02 13:52:30 -07002030
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002031 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002032 (unsigned long long)sh->sector);
2033
Dan Williamsecc65c92008-06-28 08:31:57 +10002034 sh->check_state = check_state_check_result;
Dan Williams91c00922007-01-02 13:52:30 -07002035 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002036 raid5_release_stripe(sh);
Dan Williams91c00922007-01-02 13:52:30 -07002037}
2038
Dan Williamsac6b53b2009-07-14 13:40:19 -07002039static void ops_run_check_p(struct stripe_head *sh, struct raid5_percpu *percpu)
Dan Williams91c00922007-01-02 13:52:30 -07002040{
Dan Williams91c00922007-01-02 13:52:30 -07002041 int disks = sh->disks;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002042 int pd_idx = sh->pd_idx;
2043 int qd_idx = sh->qd_idx;
2044 struct page *xor_dest;
shli@kernel.org46d5b782014-12-15 12:57:02 +11002045 struct page **xor_srcs = to_addr_page(percpu, 0);
Dan Williams91c00922007-01-02 13:52:30 -07002046 struct dma_async_tx_descriptor *tx;
Dan Williamsa08abd82009-06-03 11:43:59 -07002047 struct async_submit_ctl submit;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002048 int count;
2049 int i;
Dan Williams91c00922007-01-02 13:52:30 -07002050
Harvey Harrisone46b272b2008-04-28 02:15:50 -07002051 pr_debug("%s: stripe %llu\n", __func__,
Dan Williams91c00922007-01-02 13:52:30 -07002052 (unsigned long long)sh->sector);
2053
shli@kernel.org59fc6302014-12-15 12:57:03 +11002054 BUG_ON(sh->batch_head);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002055 count = 0;
2056 xor_dest = sh->dev[pd_idx].page;
2057 xor_srcs[count++] = xor_dest;
Dan Williams91c00922007-01-02 13:52:30 -07002058 for (i = disks; i--; ) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002059 if (i == pd_idx || i == qd_idx)
2060 continue;
2061 xor_srcs[count++] = sh->dev[i].page;
Dan Williams91c00922007-01-02 13:52:30 -07002062 }
2063
Dan Williamsd6f38f32009-07-14 11:50:52 -07002064 init_async_submit(&submit, 0, NULL, NULL, NULL,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002065 to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04002066 tx = async_xor_val(xor_dest, xor_srcs, 0, count,
2067 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsa08abd82009-06-03 11:43:59 -07002068 &sh->ops.zero_sum_result, &submit);
Dan Williams91c00922007-01-02 13:52:30 -07002069
Dan Williams91c00922007-01-02 13:52:30 -07002070 atomic_inc(&sh->count);
Dan Williamsa08abd82009-06-03 11:43:59 -07002071 init_async_submit(&submit, ASYNC_TX_ACK, tx, ops_complete_check, sh, NULL);
2072 tx = async_trigger_callback(&submit);
Dan Williams91c00922007-01-02 13:52:30 -07002073}
2074
Dan Williamsac6b53b2009-07-14 13:40:19 -07002075static void ops_run_check_pq(struct stripe_head *sh, struct raid5_percpu *percpu, int checkp)
2076{
shli@kernel.org46d5b782014-12-15 12:57:02 +11002077 struct page **srcs = to_addr_page(percpu, 0);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002078 struct async_submit_ctl submit;
2079 int count;
2080
2081 pr_debug("%s: stripe %llu checkp: %d\n", __func__,
2082 (unsigned long long)sh->sector, checkp);
2083
shli@kernel.org59fc6302014-12-15 12:57:03 +11002084 BUG_ON(sh->batch_head);
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002085 count = set_syndrome_sources(srcs, sh, SYNDROME_SRC_ALL);
Dan Williamsac6b53b2009-07-14 13:40:19 -07002086 if (!checkp)
2087 srcs[count] = NULL;
2088
2089 atomic_inc(&sh->count);
2090 init_async_submit(&submit, ASYNC_TX_ACK, NULL, ops_complete_check,
shli@kernel.org46d5b782014-12-15 12:57:02 +11002091 sh, to_addr_conv(sh, percpu, 0));
Yufen Yuc911c462020-07-18 05:29:07 -04002092 async_syndrome_val(srcs, 0, count+2,
2093 RAID5_STRIPE_SIZE(sh->raid_conf),
Dan Williamsac6b53b2009-07-14 13:40:19 -07002094 &sh->ops.zero_sum_result, percpu->spare_page, &submit);
2095}
2096
NeilBrown51acbce2013-02-28 09:08:34 +11002097static void raid_run_ops(struct stripe_head *sh, unsigned long ops_request)
Dan Williams91c00922007-01-02 13:52:30 -07002098{
2099 int overlap_clear = 0, i, disks = sh->disks;
2100 struct dma_async_tx_descriptor *tx = NULL;
NeilBrownd1688a62011-10-11 16:49:52 +11002101 struct r5conf *conf = sh->raid_conf;
Dan Williamsac6b53b2009-07-14 13:40:19 -07002102 int level = conf->level;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002103 struct raid5_percpu *percpu;
2104 unsigned long cpu;
Dan Williams91c00922007-01-02 13:52:30 -07002105
Dan Williamsd6f38f32009-07-14 11:50:52 -07002106 cpu = get_cpu();
2107 percpu = per_cpu_ptr(conf->percpu, cpu);
Dan Williams83de75c2008-06-28 08:31:58 +10002108 if (test_bit(STRIPE_OP_BIOFILL, &ops_request)) {
Dan Williams91c00922007-01-02 13:52:30 -07002109 ops_run_biofill(sh);
2110 overlap_clear++;
2111 }
2112
Dan Williams7b3a8712008-06-28 08:32:09 +10002113 if (test_bit(STRIPE_OP_COMPUTE_BLK, &ops_request)) {
Dan Williamsac6b53b2009-07-14 13:40:19 -07002114 if (level < 6)
2115 tx = ops_run_compute5(sh, percpu);
2116 else {
2117 if (sh->ops.target2 < 0 || sh->ops.target < 0)
2118 tx = ops_run_compute6_1(sh, percpu);
2119 else
2120 tx = ops_run_compute6_2(sh, percpu);
2121 }
2122 /* terminate the chain if reconstruct is not set to be run */
2123 if (tx && !test_bit(STRIPE_OP_RECONSTRUCT, &ops_request))
Dan Williams7b3a8712008-06-28 08:32:09 +10002124 async_tx_ack(tx);
2125 }
Dan Williams91c00922007-01-02 13:52:30 -07002126
Markus Stockhausen584acdd2014-12-15 12:57:05 +11002127 if (test_bit(STRIPE_OP_PREXOR, &ops_request)) {
2128 if (level < 6)
2129 tx = ops_run_prexor5(sh, percpu, tx);
2130 else
2131 tx = ops_run_prexor6(sh, percpu, tx);
2132 }
Dan Williams91c00922007-01-02 13:52:30 -07002133
Artur Paszkiewiczae1713e2017-04-04 13:13:58 +02002134 if (test_bit(STRIPE_OP_PARTIAL_PARITY, &ops_request))
2135 tx = ops_run_partial_parity(sh, percpu, tx);
2136
Dan Williams600aa102008-06-28 08:32:05 +10002137 if (test_bit(STRIPE_OP_BIODRAIN, &ops_request)) {
Dan Williamsd8ee0722008-06-28 08:32:06 +10002138 tx = ops_run_biodrain(sh, tx);
Dan Williams91c00922007-01-02 13:52:30 -07002139 overlap_clear++;
2140 }
2141
Dan Williamsac6b53b2009-07-14 13:40:19 -07002142 if (test_bit(STRIPE_OP_RECONSTRUCT, &ops_request)) {
2143 if (level < 6)
2144 ops_run_reconstruct5(sh, percpu, tx);
2145 else
2146 ops_run_reconstruct6(sh, percpu, tx);
2147 }
Dan Williams91c00922007-01-02 13:52:30 -07002148
Dan Williamsac6b53b2009-07-14 13:40:19 -07002149 if (test_bit(STRIPE_OP_CHECK, &ops_request)) {
2150 if (sh->check_state == check_state_run)
2151 ops_run_check_p(sh, percpu);
2152 else if (sh->check_state == check_state_run_q)
2153 ops_run_check_pq(sh, percpu, 0);
2154 else if (sh->check_state == check_state_run_pq)
2155 ops_run_check_pq(sh, percpu, 1);
2156 else
2157 BUG();
2158 }
Dan Williams91c00922007-01-02 13:52:30 -07002159
shli@kernel.org59fc6302014-12-15 12:57:03 +11002160 if (overlap_clear && !sh->batch_head)
Dan Williams91c00922007-01-02 13:52:30 -07002161 for (i = disks; i--; ) {
2162 struct r5dev *dev = &sh->dev[i];
2163 if (test_and_clear_bit(R5_Overlap, &dev->flags))
2164 wake_up(&sh->raid_conf->wait_for_overlap);
2165 }
Dan Williamsd6f38f32009-07-14 11:50:52 -07002166 put_cpu();
Dan Williams91c00922007-01-02 13:52:30 -07002167}
2168
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002169static void free_stripe(struct kmem_cache *sc, struct stripe_head *sh)
2170{
2171 if (sh->ppl_page)
2172 __free_page(sh->ppl_page);
2173 kmem_cache_free(sc, sh);
2174}
2175
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002176static struct stripe_head *alloc_stripe(struct kmem_cache *sc, gfp_t gfp,
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002177 int disks, struct r5conf *conf)
NeilBrownf18c1a32015-05-08 18:19:04 +10002178{
2179 struct stripe_head *sh;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002180 int i;
NeilBrownf18c1a32015-05-08 18:19:04 +10002181
2182 sh = kmem_cache_zalloc(sc, gfp);
2183 if (sh) {
2184 spin_lock_init(&sh->stripe_lock);
2185 spin_lock_init(&sh->batch_lock);
2186 INIT_LIST_HEAD(&sh->batch_list);
2187 INIT_LIST_HEAD(&sh->lru);
Song Liua39f7af2016-11-17 15:24:40 -08002188 INIT_LIST_HEAD(&sh->r5c);
Song Liud7bd3982016-11-23 22:50:39 -08002189 INIT_LIST_HEAD(&sh->log_list);
NeilBrownf18c1a32015-05-08 18:19:04 +10002190 atomic_set(&sh->count, 1);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002191 sh->raid_conf = conf;
Song Liua39f7af2016-11-17 15:24:40 -08002192 sh->log_start = MaxSector;
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002193 for (i = 0; i < disks; i++) {
2194 struct r5dev *dev = &sh->dev[i];
2195
Ming Lei3a83f462016-11-22 08:57:21 -07002196 bio_init(&dev->req, &dev->vec, 1);
2197 bio_init(&dev->rreq, &dev->rvec, 1);
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002198 }
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002199
2200 if (raid5_has_ppl(conf)) {
2201 sh->ppl_page = alloc_page(gfp);
2202 if (!sh->ppl_page) {
2203 free_stripe(sc, sh);
2204 sh = NULL;
2205 }
2206 }
NeilBrownf18c1a32015-05-08 18:19:04 +10002207 }
2208 return sh;
2209}
NeilBrown486f0642015-02-25 12:10:35 +11002210static int grow_one_stripe(struct r5conf *conf, gfp_t gfp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
2212 struct stripe_head *sh;
NeilBrownf18c1a32015-05-08 18:19:04 +10002213
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002214 sh = alloc_stripe(conf->slab_cache, gfp, conf->pool_size, conf);
NeilBrown3f294f42005-11-08 21:39:25 -08002215 if (!sh)
2216 return 0;
Namhyung Kim6ce32842011-07-18 17:38:50 +10002217
NeilBrowna9683a72015-02-25 12:02:51 +11002218 if (grow_buffers(sh, gfp)) {
NeilBrowne4e11e32010-06-16 16:45:16 +10002219 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002220 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002221 return 0;
2222 }
NeilBrown486f0642015-02-25 12:10:35 +11002223 sh->hash_lock_index =
2224 conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS;
NeilBrown3f294f42005-11-08 21:39:25 -08002225 /* we just created an active stripe so... */
NeilBrown3f294f42005-11-08 21:39:25 -08002226 atomic_inc(&conf->active_stripes);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002227
Shaohua Li6d036f72015-08-13 14:31:57 -07002228 raid5_release_stripe(sh);
NeilBrown486f0642015-02-25 12:10:35 +11002229 conf->max_nr_stripes++;
NeilBrown3f294f42005-11-08 21:39:25 -08002230 return 1;
2231}
2232
NeilBrownd1688a62011-10-11 16:49:52 +11002233static int grow_stripes(struct r5conf *conf, int num)
NeilBrown3f294f42005-11-08 21:39:25 -08002234{
Christoph Lametere18b8902006-12-06 20:33:20 -08002235 struct kmem_cache *sc;
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002236 size_t namelen = sizeof(conf->cache_name[0]);
NeilBrown5e5e3e72009-10-16 16:35:30 +11002237 int devs = max(conf->raid_disks, conf->previous_raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002238
NeilBrownf4be6b42010-06-01 19:37:25 +10002239 if (conf->mddev->gendisk)
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002240 snprintf(conf->cache_name[0], namelen,
NeilBrownf4be6b42010-06-01 19:37:25 +10002241 "raid%d-%s", conf->level, mdname(conf->mddev));
2242 else
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002243 snprintf(conf->cache_name[0], namelen,
NeilBrownf4be6b42010-06-01 19:37:25 +10002244 "raid%d-%p", conf->level, conf->mddev);
Arnd Bergmann53b8d892018-02-20 14:09:11 +01002245 snprintf(conf->cache_name[1], namelen, "%.27s-alt", conf->cache_name[0]);
NeilBrownf4be6b42010-06-01 19:37:25 +10002246
NeilBrownad01c9e2006-03-27 01:18:07 -08002247 conf->active_name = 0;
2248 sc = kmem_cache_create(conf->cache_name[conf->active_name],
Linus Torvalds1da177e2005-04-16 15:20:36 -07002249 sizeof(struct stripe_head)+(devs-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002250 0, 0, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251 if (!sc)
2252 return 1;
2253 conf->slab_cache = sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002254 conf->pool_size = devs;
NeilBrown486f0642015-02-25 12:10:35 +11002255 while (num--)
2256 if (!grow_one_stripe(conf, GFP_KERNEL))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 return 1;
NeilBrown486f0642015-02-25 12:10:35 +11002258
Linus Torvalds1da177e2005-04-16 15:20:36 -07002259 return 0;
2260}
NeilBrown29269552006-03-27 01:18:10 -08002261
Dan Williamsd6f38f32009-07-14 11:50:52 -07002262/**
Coly Li7f8a30e2020-04-09 22:17:22 +08002263 * scribble_alloc - allocate percpu scribble buffer for required size
2264 * of the scribble region
Damien Le Moal2aada5b2020-07-16 13:54:42 +09002265 * @percpu: from for_each_present_cpu() of the caller
2266 * @num: total number of disks in the array
2267 * @cnt: scribble objs count for required size of the scribble region
Dan Williamsd6f38f32009-07-14 11:50:52 -07002268 *
Coly Li7f8a30e2020-04-09 22:17:22 +08002269 * The scribble buffer size must be enough to contain:
Dan Williamsd6f38f32009-07-14 11:50:52 -07002270 * 1/ a struct page pointer for each device in the array +2
2271 * 2/ room to convert each entry in (1) to its corresponding dma
2272 * (dma_map_page()) or page (page_address()) address.
2273 *
2274 * Note: the +2 is for the destination buffers of the ddf/raid6 case where we
2275 * calculate over all devices (not just the data blocks), using zeros in place
2276 * of the P and Q blocks.
2277 */
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002278static int scribble_alloc(struct raid5_percpu *percpu,
Coly Liba54d4d2020-04-09 22:17:21 +08002279 int num, int cnt)
Dan Williamsd6f38f32009-07-14 11:50:52 -07002280{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002281 size_t obj_size =
Yufen Yu7aba13b2020-08-20 09:22:06 -04002282 sizeof(struct page *) * (num + 2) +
2283 sizeof(addr_conv_t) * (num + 2) +
2284 sizeof(unsigned int) * (num + 2);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002285 void *scribble;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002286
Coly Liba54d4d2020-04-09 22:17:21 +08002287 /*
2288 * If here is in raid array suspend context, it is in memalloc noio
2289 * context as well, there is no potential recursive memory reclaim
2290 * I/Os with the GFP_KERNEL flag.
2291 */
2292 scribble = kvmalloc_array(cnt, obj_size, GFP_KERNEL);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002293 if (!scribble)
2294 return -ENOMEM;
2295
2296 kvfree(percpu->scribble);
2297
2298 percpu->scribble = scribble;
2299 percpu->scribble_obj_size = obj_size;
2300 return 0;
Dan Williamsd6f38f32009-07-14 11:50:52 -07002301}
2302
NeilBrown738a2732015-05-08 18:19:39 +10002303static int resize_chunks(struct r5conf *conf, int new_disks, int new_sectors)
2304{
2305 unsigned long cpu;
2306 int err = 0;
2307
Shaohua Li27a353c2016-02-24 17:38:28 -08002308 /*
2309 * Never shrink. And mddev_suspend() could deadlock if this is called
2310 * from raid5d. In that case, scribble_disks and scribble_sectors
2311 * should equal to new_disks and new_sectors
2312 */
2313 if (conf->scribble_disks >= new_disks &&
2314 conf->scribble_sectors >= new_sectors)
2315 return 0;
NeilBrown738a2732015-05-08 18:19:39 +10002316 mddev_suspend(conf->mddev);
2317 get_online_cpus();
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002318
NeilBrown738a2732015-05-08 18:19:39 +10002319 for_each_present_cpu(cpu) {
2320 struct raid5_percpu *percpu;
NeilBrown738a2732015-05-08 18:19:39 +10002321
2322 percpu = per_cpu_ptr(conf->percpu, cpu);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002323 err = scribble_alloc(percpu, new_disks,
Yufen Yuc911c462020-07-18 05:29:07 -04002324 new_sectors / RAID5_STRIPE_SECTORS(conf));
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002325 if (err)
NeilBrown738a2732015-05-08 18:19:39 +10002326 break;
NeilBrown738a2732015-05-08 18:19:39 +10002327 }
Kent Overstreetb330e6a2019-03-11 23:31:06 -07002328
NeilBrown738a2732015-05-08 18:19:39 +10002329 put_online_cpus();
2330 mddev_resume(conf->mddev);
Shaohua Li27a353c2016-02-24 17:38:28 -08002331 if (!err) {
2332 conf->scribble_disks = new_disks;
2333 conf->scribble_sectors = new_sectors;
2334 }
NeilBrown738a2732015-05-08 18:19:39 +10002335 return err;
2336}
2337
NeilBrownd1688a62011-10-11 16:49:52 +11002338static int resize_stripes(struct r5conf *conf, int newsize)
NeilBrownad01c9e2006-03-27 01:18:07 -08002339{
2340 /* Make all the stripes able to hold 'newsize' devices.
2341 * New slots in each stripe get 'page' set to a new page.
2342 *
2343 * This happens in stages:
2344 * 1/ create a new kmem_cache and allocate the required number of
2345 * stripe_heads.
Masanari Iida83f0d772012-10-30 00:18:08 +09002346 * 2/ gather all the old stripe_heads and transfer the pages across
NeilBrownad01c9e2006-03-27 01:18:07 -08002347 * to the new stripe_heads. This will have the side effect of
2348 * freezing the array as once all stripe_heads have been collected,
2349 * no IO will be possible. Old stripe heads are freed once their
2350 * pages have been transferred over, and the old kmem_cache is
2351 * freed when all stripes are done.
2352 * 3/ reallocate conf->disks to be suitable bigger. If this fails,
Zhilong Liu3560741e2017-03-15 16:14:53 +08002353 * we simple return a failure status - no need to clean anything up.
NeilBrownad01c9e2006-03-27 01:18:07 -08002354 * 4/ allocate new pages for the new slots in the new stripe_heads.
2355 * If this fails, we don't bother trying the shrink the
2356 * stripe_heads down again, we just leave them as they are.
2357 * As each stripe_head is processed the new one is released into
2358 * active service.
2359 *
2360 * Once step2 is started, we cannot afford to wait for a write,
2361 * so we use GFP_NOIO allocations.
2362 */
2363 struct stripe_head *osh, *nsh;
2364 LIST_HEAD(newstripes);
2365 struct disk_info *ndisks;
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02002366 int err = 0;
Christoph Lametere18b8902006-12-06 20:33:20 -08002367 struct kmem_cache *sc;
NeilBrownad01c9e2006-03-27 01:18:07 -08002368 int i;
Shaohua Li566c09c2013-11-14 15:16:17 +11002369 int hash, cnt;
NeilBrownad01c9e2006-03-27 01:18:07 -08002370
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02002371 md_allow_write(conf->mddev);
NeilBrown2a2275d2007-01-26 00:57:11 -08002372
NeilBrownad01c9e2006-03-27 01:18:07 -08002373 /* Step 1 */
2374 sc = kmem_cache_create(conf->cache_name[1-conf->active_name],
2375 sizeof(struct stripe_head)+(newsize-1)*sizeof(struct r5dev),
Paul Mundt20c2df82007-07-20 10:11:58 +09002376 0, 0, NULL);
NeilBrownad01c9e2006-03-27 01:18:07 -08002377 if (!sc)
2378 return -ENOMEM;
2379
NeilBrown2d5b5692015-07-06 12:49:23 +10002380 /* Need to ensure auto-resizing doesn't interfere */
2381 mutex_lock(&conf->cache_size_mutex);
2382
NeilBrownad01c9e2006-03-27 01:18:07 -08002383 for (i = conf->max_nr_stripes; i; i--) {
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002384 nsh = alloc_stripe(sc, GFP_KERNEL, newsize, conf);
NeilBrownad01c9e2006-03-27 01:18:07 -08002385 if (!nsh)
2386 break;
2387
NeilBrownad01c9e2006-03-27 01:18:07 -08002388 list_add(&nsh->lru, &newstripes);
2389 }
2390 if (i) {
2391 /* didn't get enough, give up */
2392 while (!list_empty(&newstripes)) {
2393 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2394 list_del(&nsh->lru);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002395 free_stripe(sc, nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002396 }
2397 kmem_cache_destroy(sc);
NeilBrown2d5b5692015-07-06 12:49:23 +10002398 mutex_unlock(&conf->cache_size_mutex);
NeilBrownad01c9e2006-03-27 01:18:07 -08002399 return -ENOMEM;
2400 }
2401 /* Step 2 - Must use GFP_NOIO now.
2402 * OK, we have enough stripes, start collecting inactive
2403 * stripes and copying them over
2404 */
Shaohua Li566c09c2013-11-14 15:16:17 +11002405 hash = 0;
2406 cnt = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002407 list_for_each_entry(nsh, &newstripes, lru) {
Shaohua Li566c09c2013-11-14 15:16:17 +11002408 lock_device_hash_lock(conf, hash);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08002409 wait_event_cmd(conf->wait_for_stripe,
Shaohua Li566c09c2013-11-14 15:16:17 +11002410 !list_empty(conf->inactive_list + hash),
2411 unlock_device_hash_lock(conf, hash),
2412 lock_device_hash_lock(conf, hash));
2413 osh = get_free_stripe(conf, hash);
2414 unlock_device_hash_lock(conf, hash);
NeilBrownf18c1a32015-05-08 18:19:04 +10002415
Shaohua Lid592a992014-05-21 17:57:44 +08002416 for(i=0; i<conf->pool_size; i++) {
NeilBrownad01c9e2006-03-27 01:18:07 -08002417 nsh->dev[i].page = osh->dev[i].page;
Shaohua Lid592a992014-05-21 17:57:44 +08002418 nsh->dev[i].orig_page = osh->dev[i].page;
Yufen Yu7aba13b2020-08-20 09:22:06 -04002419 nsh->dev[i].offset = osh->dev[i].offset;
Shaohua Lid592a992014-05-21 17:57:44 +08002420 }
Shaohua Li566c09c2013-11-14 15:16:17 +11002421 nsh->hash_lock_index = hash;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002422 free_stripe(conf->slab_cache, osh);
Shaohua Li566c09c2013-11-14 15:16:17 +11002423 cnt++;
2424 if (cnt >= conf->max_nr_stripes / NR_STRIPE_HASH_LOCKS +
2425 !!((conf->max_nr_stripes % NR_STRIPE_HASH_LOCKS) > hash)) {
2426 hash++;
2427 cnt = 0;
2428 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002429 }
2430 kmem_cache_destroy(conf->slab_cache);
2431
2432 /* Step 3.
2433 * At this point, we are holding all the stripes so the array
2434 * is completely stalled, so now is a good time to resize
Dan Williamsd6f38f32009-07-14 11:50:52 -07002435 * conf->disks and the scribble region
NeilBrownad01c9e2006-03-27 01:18:07 -08002436 */
Kees Cook6396bb22018-06-12 14:03:40 -07002437 ndisks = kcalloc(newsize, sizeof(struct disk_info), GFP_NOIO);
NeilBrownad01c9e2006-03-27 01:18:07 -08002438 if (ndisks) {
Song Liud7bd3982016-11-23 22:50:39 -08002439 for (i = 0; i < conf->pool_size; i++)
NeilBrownad01c9e2006-03-27 01:18:07 -08002440 ndisks[i] = conf->disks[i];
Song Liud7bd3982016-11-23 22:50:39 -08002441
2442 for (i = conf->pool_size; i < newsize; i++) {
2443 ndisks[i].extra_page = alloc_page(GFP_NOIO);
2444 if (!ndisks[i].extra_page)
2445 err = -ENOMEM;
2446 }
2447
2448 if (err) {
2449 for (i = conf->pool_size; i < newsize; i++)
2450 if (ndisks[i].extra_page)
2451 put_page(ndisks[i].extra_page);
2452 kfree(ndisks);
2453 } else {
2454 kfree(conf->disks);
2455 conf->disks = ndisks;
2456 }
NeilBrownad01c9e2006-03-27 01:18:07 -08002457 } else
2458 err = -ENOMEM;
2459
NeilBrown2d5b5692015-07-06 12:49:23 +10002460 mutex_unlock(&conf->cache_size_mutex);
Dennis Yang583da482017-03-29 15:46:13 +08002461
2462 conf->slab_cache = sc;
2463 conf->active_name = 1-conf->active_name;
2464
NeilBrownad01c9e2006-03-27 01:18:07 -08002465 /* Step 4, return new stripes to service */
2466 while(!list_empty(&newstripes)) {
2467 nsh = list_entry(newstripes.next, struct stripe_head, lru);
2468 list_del_init(&nsh->lru);
Dan Williamsd6f38f32009-07-14 11:50:52 -07002469
NeilBrownad01c9e2006-03-27 01:18:07 -08002470 for (i=conf->raid_disks; i < newsize; i++)
2471 if (nsh->dev[i].page == NULL) {
2472 struct page *p = alloc_page(GFP_NOIO);
2473 nsh->dev[i].page = p;
Shaohua Lid592a992014-05-21 17:57:44 +08002474 nsh->dev[i].orig_page = p;
Yufen Yu7aba13b2020-08-20 09:22:06 -04002475 nsh->dev[i].offset = 0;
NeilBrownad01c9e2006-03-27 01:18:07 -08002476 if (!p)
2477 err = -ENOMEM;
2478 }
Shaohua Li6d036f72015-08-13 14:31:57 -07002479 raid5_release_stripe(nsh);
NeilBrownad01c9e2006-03-27 01:18:07 -08002480 }
2481 /* critical section pass, GFP_NOIO no longer needed */
2482
NeilBrown6e9eac22015-05-08 18:19:34 +10002483 if (!err)
2484 conf->pool_size = newsize;
NeilBrownad01c9e2006-03-27 01:18:07 -08002485 return err;
2486}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487
NeilBrown486f0642015-02-25 12:10:35 +11002488static int drop_one_stripe(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002489{
2490 struct stripe_head *sh;
NeilBrown49895bc2015-08-03 17:09:57 +10002491 int hash = (conf->max_nr_stripes - 1) & STRIPE_HASH_LOCKS_MASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002492
Shaohua Li566c09c2013-11-14 15:16:17 +11002493 spin_lock_irq(conf->hash_locks + hash);
2494 sh = get_free_stripe(conf, hash);
2495 spin_unlock_irq(conf->hash_locks + hash);
NeilBrown3f294f42005-11-08 21:39:25 -08002496 if (!sh)
2497 return 0;
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02002498 BUG_ON(atomic_read(&sh->count));
NeilBrowne4e11e32010-06-16 16:45:16 +10002499 shrink_buffers(sh);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02002500 free_stripe(conf->slab_cache, sh);
NeilBrown3f294f42005-11-08 21:39:25 -08002501 atomic_dec(&conf->active_stripes);
NeilBrown486f0642015-02-25 12:10:35 +11002502 conf->max_nr_stripes--;
NeilBrown3f294f42005-11-08 21:39:25 -08002503 return 1;
2504}
2505
NeilBrownd1688a62011-10-11 16:49:52 +11002506static void shrink_stripes(struct r5conf *conf)
NeilBrown3f294f42005-11-08 21:39:25 -08002507{
NeilBrown486f0642015-02-25 12:10:35 +11002508 while (conf->max_nr_stripes &&
2509 drop_one_stripe(conf))
2510 ;
NeilBrown3f294f42005-11-08 21:39:25 -08002511
Julia Lawall644df1a2015-09-13 14:15:10 +02002512 kmem_cache_destroy(conf->slab_cache);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002513 conf->slab_cache = NULL;
2514}
2515
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002516static void raid5_end_read_request(struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517{
NeilBrown99c0fb52009-03-31 14:39:38 +11002518 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002519 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002520 int disks = sh->disks, i;
NeilBrownd6950432006-07-10 04:44:20 -07002521 char b[BDEVNAME_SIZE];
NeilBrowndd054fc2011-12-23 10:17:53 +11002522 struct md_rdev *rdev = NULL;
NeilBrown05616be2012-05-21 09:27:00 +10002523 sector_t s;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524
2525 for (i=0 ; i<disks; i++)
2526 if (bi == &sh->dev[i].req)
2527 break;
2528
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002529 pr_debug("end_read_request %llu/%d, count: %d, error %d.\n",
Dan Williams45b42332007-07-09 11:56:43 -07002530 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002531 bi->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002532 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002533 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002535 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002536 }
NeilBrown14a75d32011-12-23 10:17:52 +11002537 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowndd054fc2011-12-23 10:17:53 +11002538 /* If replacement finished while this request was outstanding,
2539 * 'replacement' might be NULL already.
2540 * In that case it moved down to 'rdev'.
2541 * rdev is not removed until all requests are finished.
2542 */
NeilBrown14a75d32011-12-23 10:17:52 +11002543 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002544 if (!rdev)
NeilBrown14a75d32011-12-23 10:17:52 +11002545 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002546
NeilBrown05616be2012-05-21 09:27:00 +10002547 if (use_new_offset(conf, sh))
2548 s = sh->sector + rdev->new_data_offset;
2549 else
2550 s = sh->sector + rdev->data_offset;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002551 if (!bi->bi_status) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 set_bit(R5_UPTODATE, &sh->dev[i].flags);
NeilBrown4e5314b2005-11-08 21:39:22 -08002553 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11002554 /* Note that this cannot happen on a
2555 * replacement device. We just fail those on
2556 * any error
2557 */
NeilBrowncc6167b2016-11-02 14:16:50 +11002558 pr_info_ratelimited(
2559 "md/raid:%s: read error corrected (%lu sectors at %llu on %s)\n",
Yufen Yuc911c462020-07-18 05:29:07 -04002560 mdname(conf->mddev), RAID5_STRIPE_SECTORS(conf),
NeilBrown05616be2012-05-21 09:27:00 +10002561 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002562 bdevname(rdev->bdev, b));
Yufen Yuc911c462020-07-18 05:29:07 -04002563 atomic_add(RAID5_STRIPE_SECTORS(conf), &rdev->corrected_errors);
NeilBrown4e5314b2005-11-08 21:39:22 -08002564 clear_bit(R5_ReadError, &sh->dev[i].flags);
2565 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng3f9e7c12012-07-31 10:04:21 +10002566 } else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2567 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2568
Song Liu86aa1392017-01-12 17:22:41 -08002569 if (test_bit(R5_InJournal, &sh->dev[i].flags))
2570 /*
2571 * end read for a page in journal, this
2572 * must be preparing for prexor in rmw
2573 */
2574 set_bit(R5_OrigPageUPTDODATE, &sh->dev[i].flags);
2575
NeilBrown14a75d32011-12-23 10:17:52 +11002576 if (atomic_read(&rdev->read_errors))
2577 atomic_set(&rdev->read_errors, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002578 } else {
NeilBrown14a75d32011-12-23 10:17:52 +11002579 const char *bdn = bdevname(rdev->bdev, b);
NeilBrownba22dcb2005-11-08 21:39:31 -08002580 int retry = 0;
majianpeng2e8ac3032012-07-03 15:57:02 +10002581 int set_bad = 0;
NeilBrownd6950432006-07-10 04:44:20 -07002582
Linus Torvalds1da177e2005-04-16 15:20:36 -07002583 clear_bit(R5_UPTODATE, &sh->dev[i].flags);
Nigel Croxonb76b4712019-09-06 09:21:33 -04002584 if (!(bi->bi_status == BLK_STS_PROTECTION))
2585 atomic_inc(&rdev->read_errors);
NeilBrown14a75d32011-12-23 10:17:52 +11002586 if (test_bit(R5_ReadRepl, &sh->dev[i].flags))
NeilBrowncc6167b2016-11-02 14:16:50 +11002587 pr_warn_ratelimited(
2588 "md/raid:%s: read error on replacement device (sector %llu on %s).\n",
NeilBrown14a75d32011-12-23 10:17:52 +11002589 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002590 (unsigned long long)s,
NeilBrown14a75d32011-12-23 10:17:52 +11002591 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002592 else if (conf->mddev->degraded >= conf->max_degraded) {
2593 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002594 pr_warn_ratelimited(
2595 "md/raid:%s: read error not correctable (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002596 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002597 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002598 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002599 } else if (test_bit(R5_ReWrite, &sh->dev[i].flags)) {
NeilBrown4e5314b2005-11-08 21:39:22 -08002600 /* Oh, no!!! */
majianpeng2e8ac3032012-07-03 15:57:02 +10002601 set_bad = 1;
NeilBrowncc6167b2016-11-02 14:16:50 +11002602 pr_warn_ratelimited(
2603 "md/raid:%s: read error NOT corrected!! (sector %llu on %s).\n",
Christian Dietrich8bda4702011-07-27 11:00:36 +10002604 mdname(conf->mddev),
NeilBrown05616be2012-05-21 09:27:00 +10002605 (unsigned long long)s,
Christian Dietrich8bda4702011-07-27 11:00:36 +10002606 bdn);
majianpeng2e8ac3032012-07-03 15:57:02 +10002607 } else if (atomic_read(&rdev->read_errors)
Nigel Croxon0009fad2019-08-21 09:27:08 -04002608 > conf->max_nr_stripes) {
2609 if (!test_bit(Faulty, &rdev->flags)) {
2610 pr_warn("md/raid:%s: %d read_errors > %d stripes\n",
2611 mdname(conf->mddev),
2612 atomic_read(&rdev->read_errors),
2613 conf->max_nr_stripes);
2614 pr_warn("md/raid:%s: Too many read errors, failing device %s.\n",
2615 mdname(conf->mddev), bdn);
2616 }
2617 } else
NeilBrownba22dcb2005-11-08 21:39:31 -08002618 retry = 1;
Bian Yuedfa1f62013-11-14 15:16:17 +11002619 if (set_bad && test_bit(In_sync, &rdev->flags)
2620 && !test_bit(R5_ReadNoMerge, &sh->dev[i].flags))
2621 retry = 1;
NeilBrownba22dcb2005-11-08 21:39:31 -08002622 if (retry)
Xiao Ni143f6e72019-07-08 10:14:32 +08002623 if (sh->qd_idx >= 0 && sh->pd_idx == i)
2624 set_bit(R5_ReadError, &sh->dev[i].flags);
2625 else if (test_bit(R5_ReadNoMerge, &sh->dev[i].flags)) {
majianpeng3f9e7c12012-07-31 10:04:21 +10002626 set_bit(R5_ReadError, &sh->dev[i].flags);
2627 clear_bit(R5_ReadNoMerge, &sh->dev[i].flags);
2628 } else
2629 set_bit(R5_ReadNoMerge, &sh->dev[i].flags);
NeilBrownba22dcb2005-11-08 21:39:31 -08002630 else {
NeilBrown4e5314b2005-11-08 21:39:22 -08002631 clear_bit(R5_ReadError, &sh->dev[i].flags);
2632 clear_bit(R5_ReWrite, &sh->dev[i].flags);
majianpeng2e8ac3032012-07-03 15:57:02 +10002633 if (!(set_bad
2634 && test_bit(In_sync, &rdev->flags)
2635 && rdev_set_badblocks(
Yufen Yuc911c462020-07-18 05:29:07 -04002636 rdev, sh->sector, RAID5_STRIPE_SECTORS(conf), 0)))
majianpeng2e8ac3032012-07-03 15:57:02 +10002637 md_error(conf->mddev, rdev);
NeilBrownba22dcb2005-11-08 21:39:31 -08002638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 }
NeilBrown14a75d32011-12-23 10:17:52 +11002640 rdev_dec_pending(rdev, conf->mddev);
Shaohua Lic9445552016-09-08 10:43:58 -07002641 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002642 clear_bit(R5_LOCKED, &sh->dev[i].flags);
2643 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002644 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002645}
2646
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002647static void raid5_end_write_request(struct bio *bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
NeilBrown99c0fb52009-03-31 14:39:38 +11002649 struct stripe_head *sh = bi->bi_private;
NeilBrownd1688a62011-10-11 16:49:52 +11002650 struct r5conf *conf = sh->raid_conf;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08002651 int disks = sh->disks, i;
Kees Cook3f649ab2020-06-03 13:09:38 -07002652 struct md_rdev *rdev;
NeilBrownb84db562011-07-28 11:39:23 +10002653 sector_t first_bad;
2654 int bad_sectors;
NeilBrown977df362011-12-23 10:17:53 +11002655 int replacement = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656
NeilBrown977df362011-12-23 10:17:53 +11002657 for (i = 0 ; i < disks; i++) {
2658 if (bi == &sh->dev[i].req) {
2659 rdev = conf->disks[i].rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002660 break;
NeilBrown977df362011-12-23 10:17:53 +11002661 }
2662 if (bi == &sh->dev[i].rreq) {
2663 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11002664 if (rdev)
2665 replacement = 1;
2666 else
2667 /* rdev was removed and 'replacement'
2668 * replaced it. rdev is not removed
2669 * until all requests are finished.
2670 */
2671 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11002672 break;
2673 }
2674 }
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02002675 pr_debug("end_write_request %llu/%d, count %d, error: %d.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -07002676 (unsigned long long)sh->sector, i, atomic_read(&sh->count),
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002677 bi->bi_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 if (i == disks) {
Shaohua Li5f9d1fd2016-08-22 21:14:01 -07002679 bio_reset(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 BUG();
NeilBrown6712ecf2007-09-27 12:47:43 +02002681 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 }
2683
NeilBrown977df362011-12-23 10:17:53 +11002684 if (replacement) {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002685 if (bi->bi_status)
NeilBrown977df362011-12-23 10:17:53 +11002686 md_error(conf->mddev, rdev);
2687 else if (is_badblock(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04002688 RAID5_STRIPE_SECTORS(conf),
NeilBrown977df362011-12-23 10:17:53 +11002689 &first_bad, &bad_sectors))
2690 set_bit(R5_MadeGoodRepl, &sh->dev[i].flags);
2691 } else {
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002692 if (bi->bi_status) {
NeilBrown9f97e4b2014-01-16 09:35:38 +11002693 set_bit(STRIPE_DEGRADED, &sh->state);
NeilBrown977df362011-12-23 10:17:53 +11002694 set_bit(WriteErrorSeen, &rdev->flags);
2695 set_bit(R5_WriteError, &sh->dev[i].flags);
NeilBrown3a6de292011-12-23 10:17:54 +11002696 if (!test_and_set_bit(WantReplacement, &rdev->flags))
2697 set_bit(MD_RECOVERY_NEEDED,
2698 &rdev->mddev->recovery);
NeilBrown977df362011-12-23 10:17:53 +11002699 } else if (is_badblock(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04002700 RAID5_STRIPE_SECTORS(conf),
NeilBrownc0b32972013-04-24 11:42:42 +10002701 &first_bad, &bad_sectors)) {
NeilBrown977df362011-12-23 10:17:53 +11002702 set_bit(R5_MadeGood, &sh->dev[i].flags);
NeilBrownc0b32972013-04-24 11:42:42 +10002703 if (test_bit(R5_ReadError, &sh->dev[i].flags))
2704 /* That was a successful write so make
2705 * sure it looks like we already did
2706 * a re-write.
2707 */
2708 set_bit(R5_ReWrite, &sh->dev[i].flags);
2709 }
NeilBrown977df362011-12-23 10:17:53 +11002710 }
2711 rdev_dec_pending(rdev, conf->mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02002713 if (sh->batch_head && bi->bi_status && !replacement)
shli@kernel.org72ac7332014-12-15 12:57:03 +11002714 set_bit(STRIPE_BATCH_ERR, &sh->batch_head->state);
2715
Shaohua Lic9445552016-09-08 10:43:58 -07002716 bio_reset(bi);
NeilBrown977df362011-12-23 10:17:53 +11002717 if (!test_and_clear_bit(R5_DOUBLE_LOCKED, &sh->dev[i].flags))
2718 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002719 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07002720 raid5_release_stripe(sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11002721
2722 if (sh->batch_head && sh != sh->batch_head)
Shaohua Li6d036f72015-08-13 14:31:57 -07002723 raid5_release_stripe(sh->batch_head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724}
2725
Shaohua Li849674e2016-01-20 13:52:20 -08002726static void raid5_error(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002727{
2728 char b[BDEVNAME_SIZE];
NeilBrownd1688a62011-10-11 16:49:52 +11002729 struct r5conf *conf = mddev->private;
NeilBrown908f4fb2011-12-23 10:17:50 +11002730 unsigned long flags;
NeilBrown0c55e022010-05-03 14:09:02 +10002731 pr_debug("raid456: error called\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002732
NeilBrown908f4fb2011-12-23 10:17:50 +11002733 spin_lock_irqsave(&conf->device_lock, flags);
Mariusz Tkaczykfb73b352018-09-04 15:08:30 +02002734
2735 if (test_bit(In_sync, &rdev->flags) &&
2736 mddev->degraded == conf->max_degraded) {
2737 /*
2738 * Don't allow to achieve failed state
2739 * Don't try to recover this device
2740 */
2741 conf->recovery_disabled = mddev->recovery_disabled;
2742 spin_unlock_irqrestore(&conf->device_lock, flags);
2743 return;
2744 }
2745
bingjingcaff69d82017-11-17 10:57:44 +08002746 set_bit(Faulty, &rdev->flags);
NeilBrown908f4fb2011-12-23 10:17:50 +11002747 clear_bit(In_sync, &rdev->flags);
Song Liu2e38a372017-01-24 10:45:30 -08002748 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11002749 spin_unlock_irqrestore(&conf->device_lock, flags);
2750 set_bit(MD_RECOVERY_INTR, &mddev->recovery);
2751
NeilBrownde393cd2011-07-28 11:31:48 +10002752 set_bit(Blocked, &rdev->flags);
Shaohua Li29530792016-12-08 15:48:19 -08002753 set_mask_bits(&mddev->sb_flags, 0,
2754 BIT(MD_SB_CHANGE_DEVS) | BIT(MD_SB_CHANGE_PENDING));
NeilBrowncc6167b2016-11-02 14:16:50 +11002755 pr_crit("md/raid:%s: Disk failure on %s, disabling device.\n"
2756 "md/raid:%s: Operation continuing on %d devices.\n",
2757 mdname(mddev),
2758 bdevname(rdev->bdev, b),
2759 mdname(mddev),
2760 conf->raid_disks - mddev->degraded);
Song Liu70d466f2017-05-11 15:28:28 -07002761 r5c_update_on_rdev_error(mddev, rdev);
NeilBrown16a53ec2006-06-26 00:27:38 -07002762}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763
2764/*
2765 * Input: a 'big' sector number,
2766 * Output: index of the data and parity disk, and the sector # in them.
2767 */
Shaohua Li6d036f72015-08-13 14:31:57 -07002768sector_t raid5_compute_sector(struct r5conf *conf, sector_t r_sector,
2769 int previous, int *dd_idx,
2770 struct stripe_head *sh)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002771{
NeilBrown6e3b96e2010-04-23 07:08:28 +10002772 sector_t stripe, stripe2;
NeilBrown35f2a592010-04-20 14:13:34 +10002773 sector_t chunk_number;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002774 unsigned int chunk_offset;
NeilBrown911d4ee2009-03-31 14:39:38 +11002775 int pd_idx, qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002776 int ddf_layout = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 sector_t new_sector;
NeilBrowne183eae2009-03-31 15:20:22 +11002778 int algorithm = previous ? conf->prev_algo
2779 : conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002780 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2781 : conf->chunk_sectors;
NeilBrown112bf892009-03-31 14:39:38 +11002782 int raid_disks = previous ? conf->previous_raid_disks
2783 : conf->raid_disks;
2784 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
2786 /* First compute the information on this sector */
2787
2788 /*
2789 * Compute the chunk number and the sector offset inside the chunk
2790 */
2791 chunk_offset = sector_div(r_sector, sectors_per_chunk);
2792 chunk_number = r_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793
2794 /*
2795 * Compute the stripe number
2796 */
NeilBrown35f2a592010-04-20 14:13:34 +10002797 stripe = chunk_number;
2798 *dd_idx = sector_div(stripe, data_disks);
NeilBrown6e3b96e2010-04-23 07:08:28 +10002799 stripe2 = stripe;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002800 /*
2801 * Select the parity disk based on the user selected algorithm.
2802 */
NeilBrown84789552011-07-27 11:00:36 +10002803 pd_idx = qd_idx = -1;
NeilBrown16a53ec2006-06-26 00:27:38 -07002804 switch(conf->level) {
2805 case 4:
NeilBrown911d4ee2009-03-31 14:39:38 +11002806 pd_idx = data_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002807 break;
2808 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002809 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002810 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002811 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002812 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 (*dd_idx)++;
2814 break;
2815 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002816 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002817 if (*dd_idx >= pd_idx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818 (*dd_idx)++;
2819 break;
2820 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002821 pd_idx = data_disks - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002822 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002823 break;
2824 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002825 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002826 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002828 case ALGORITHM_PARITY_0:
2829 pd_idx = 0;
2830 (*dd_idx)++;
2831 break;
2832 case ALGORITHM_PARITY_N:
2833 pd_idx = data_disks;
2834 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002836 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002837 }
2838 break;
2839 case 6:
2840
NeilBrowne183eae2009-03-31 15:20:22 +11002841 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07002842 case ALGORITHM_LEFT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002843 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002844 qd_idx = pd_idx + 1;
2845 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002846 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002847 qd_idx = 0;
2848 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002849 (*dd_idx) += 2; /* D D P Q D */
2850 break;
2851 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002852 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002853 qd_idx = pd_idx + 1;
2854 if (pd_idx == raid_disks-1) {
NeilBrown99c0fb52009-03-31 14:39:38 +11002855 (*dd_idx)++; /* Q D D D P */
NeilBrown911d4ee2009-03-31 14:39:38 +11002856 qd_idx = 0;
2857 } else if (*dd_idx >= pd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07002858 (*dd_idx) += 2; /* D D P Q D */
2859 break;
2860 case ALGORITHM_LEFT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002861 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002862 qd_idx = (pd_idx + 1) % raid_disks;
2863 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002864 break;
2865 case ALGORITHM_RIGHT_SYMMETRIC:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002866 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown911d4ee2009-03-31 14:39:38 +11002867 qd_idx = (pd_idx + 1) % raid_disks;
2868 *dd_idx = (pd_idx + 2 + *dd_idx) % raid_disks;
NeilBrown16a53ec2006-06-26 00:27:38 -07002869 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11002870
2871 case ALGORITHM_PARITY_0:
2872 pd_idx = 0;
2873 qd_idx = 1;
2874 (*dd_idx) += 2;
2875 break;
2876 case ALGORITHM_PARITY_N:
2877 pd_idx = data_disks;
2878 qd_idx = data_disks + 1;
2879 break;
2880
2881 case ALGORITHM_ROTATING_ZERO_RESTART:
2882 /* Exactly the same as RIGHT_ASYMMETRIC, but or
2883 * of blocks for computing Q is different.
2884 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002885 pd_idx = sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002886 qd_idx = pd_idx + 1;
2887 if (pd_idx == raid_disks-1) {
2888 (*dd_idx)++; /* Q D D D P */
2889 qd_idx = 0;
2890 } else if (*dd_idx >= pd_idx)
2891 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002892 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002893 break;
2894
2895 case ALGORITHM_ROTATING_N_RESTART:
2896 /* Same a left_asymmetric, by first stripe is
2897 * D D D P Q rather than
2898 * Q D D D P
2899 */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002900 stripe2 += 1;
2901 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002902 qd_idx = pd_idx + 1;
2903 if (pd_idx == raid_disks-1) {
2904 (*dd_idx)++; /* Q D D D P */
2905 qd_idx = 0;
2906 } else if (*dd_idx >= pd_idx)
2907 (*dd_idx) += 2; /* D D P Q D */
NeilBrown67cc2b82009-03-31 14:39:38 +11002908 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002909 break;
2910
2911 case ALGORITHM_ROTATING_N_CONTINUE:
2912 /* Same as left_symmetric but Q is before P */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002913 pd_idx = raid_disks - 1 - sector_div(stripe2, raid_disks);
NeilBrown99c0fb52009-03-31 14:39:38 +11002914 qd_idx = (pd_idx + raid_disks - 1) % raid_disks;
2915 *dd_idx = (pd_idx + 1 + *dd_idx) % raid_disks;
NeilBrown67cc2b82009-03-31 14:39:38 +11002916 ddf_layout = 1;
NeilBrown99c0fb52009-03-31 14:39:38 +11002917 break;
2918
2919 case ALGORITHM_LEFT_ASYMMETRIC_6:
2920 /* RAID5 left_asymmetric, with Q on last device */
NeilBrown6e3b96e2010-04-23 07:08:28 +10002921 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002922 if (*dd_idx >= pd_idx)
2923 (*dd_idx)++;
2924 qd_idx = raid_disks - 1;
2925 break;
2926
2927 case ALGORITHM_RIGHT_ASYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002928 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002929 if (*dd_idx >= pd_idx)
2930 (*dd_idx)++;
2931 qd_idx = raid_disks - 1;
2932 break;
2933
2934 case ALGORITHM_LEFT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002935 pd_idx = data_disks - sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002936 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2937 qd_idx = raid_disks - 1;
2938 break;
2939
2940 case ALGORITHM_RIGHT_SYMMETRIC_6:
NeilBrown6e3b96e2010-04-23 07:08:28 +10002941 pd_idx = sector_div(stripe2, raid_disks-1);
NeilBrown99c0fb52009-03-31 14:39:38 +11002942 *dd_idx = (pd_idx + 1 + *dd_idx) % (raid_disks-1);
2943 qd_idx = raid_disks - 1;
2944 break;
2945
2946 case ALGORITHM_PARITY_0_6:
2947 pd_idx = 0;
2948 (*dd_idx)++;
2949 qd_idx = raid_disks - 1;
2950 break;
2951
NeilBrown16a53ec2006-06-26 00:27:38 -07002952 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11002953 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07002954 }
2955 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 }
2957
NeilBrown911d4ee2009-03-31 14:39:38 +11002958 if (sh) {
2959 sh->pd_idx = pd_idx;
2960 sh->qd_idx = qd_idx;
NeilBrown67cc2b82009-03-31 14:39:38 +11002961 sh->ddf_layout = ddf_layout;
NeilBrown911d4ee2009-03-31 14:39:38 +11002962 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 /*
2964 * Finally, compute the new sector number
2965 */
2966 new_sector = (sector_t)stripe * sectors_per_chunk + chunk_offset;
2967 return new_sector;
2968}
2969
Shaohua Li6d036f72015-08-13 14:31:57 -07002970sector_t raid5_compute_blocknr(struct stripe_head *sh, int i, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971{
NeilBrownd1688a62011-10-11 16:49:52 +11002972 struct r5conf *conf = sh->raid_conf;
NeilBrownb875e532006-12-10 02:20:49 -08002973 int raid_disks = sh->disks;
2974 int data_disks = raid_disks - conf->max_degraded;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002975 sector_t new_sector = sh->sector, check;
Andre Noll09c9e5f2009-06-18 08:45:55 +10002976 int sectors_per_chunk = previous ? conf->prev_chunk_sectors
2977 : conf->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11002978 int algorithm = previous ? conf->prev_algo
2979 : conf->algorithm;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 sector_t stripe;
2981 int chunk_offset;
NeilBrown35f2a592010-04-20 14:13:34 +10002982 sector_t chunk_number;
2983 int dummy1, dd_idx = i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 sector_t r_sector;
NeilBrown911d4ee2009-03-31 14:39:38 +11002985 struct stripe_head sh2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986
2987 chunk_offset = sector_div(new_sector, sectors_per_chunk);
2988 stripe = new_sector;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002989
NeilBrown16a53ec2006-06-26 00:27:38 -07002990 if (i == sh->pd_idx)
2991 return 0;
2992 switch(conf->level) {
2993 case 4: break;
2994 case 5:
NeilBrowne183eae2009-03-31 15:20:22 +11002995 switch (algorithm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002996 case ALGORITHM_LEFT_ASYMMETRIC:
2997 case ALGORITHM_RIGHT_ASYMMETRIC:
2998 if (i > sh->pd_idx)
2999 i--;
3000 break;
3001 case ALGORITHM_LEFT_SYMMETRIC:
3002 case ALGORITHM_RIGHT_SYMMETRIC:
3003 if (i < sh->pd_idx)
3004 i += raid_disks;
3005 i -= (sh->pd_idx + 1);
3006 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11003007 case ALGORITHM_PARITY_0:
3008 i -= 1;
3009 break;
3010 case ALGORITHM_PARITY_N:
3011 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003012 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003013 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003014 }
3015 break;
3016 case 6:
NeilBrownd0dabf72009-03-31 14:39:38 +11003017 if (i == sh->qd_idx)
NeilBrown16a53ec2006-06-26 00:27:38 -07003018 return 0; /* It is the Q disk */
NeilBrowne183eae2009-03-31 15:20:22 +11003019 switch (algorithm) {
NeilBrown16a53ec2006-06-26 00:27:38 -07003020 case ALGORITHM_LEFT_ASYMMETRIC:
3021 case ALGORITHM_RIGHT_ASYMMETRIC:
NeilBrown99c0fb52009-03-31 14:39:38 +11003022 case ALGORITHM_ROTATING_ZERO_RESTART:
3023 case ALGORITHM_ROTATING_N_RESTART:
3024 if (sh->pd_idx == raid_disks-1)
3025 i--; /* Q D D D P */
NeilBrown16a53ec2006-06-26 00:27:38 -07003026 else if (i > sh->pd_idx)
3027 i -= 2; /* D D P Q D */
3028 break;
3029 case ALGORITHM_LEFT_SYMMETRIC:
3030 case ALGORITHM_RIGHT_SYMMETRIC:
3031 if (sh->pd_idx == raid_disks-1)
3032 i--; /* Q D D D P */
3033 else {
3034 /* D D P Q D */
3035 if (i < sh->pd_idx)
3036 i += raid_disks;
3037 i -= (sh->pd_idx + 2);
3038 }
3039 break;
NeilBrown99c0fb52009-03-31 14:39:38 +11003040 case ALGORITHM_PARITY_0:
3041 i -= 2;
3042 break;
3043 case ALGORITHM_PARITY_N:
3044 break;
3045 case ALGORITHM_ROTATING_N_CONTINUE:
NeilBrowne4424fe2009-10-16 16:27:34 +11003046 /* Like left_symmetric, but P is before Q */
NeilBrown99c0fb52009-03-31 14:39:38 +11003047 if (sh->pd_idx == 0)
3048 i--; /* P D D D Q */
NeilBrowne4424fe2009-10-16 16:27:34 +11003049 else {
3050 /* D D Q P D */
3051 if (i < sh->pd_idx)
3052 i += raid_disks;
3053 i -= (sh->pd_idx + 1);
3054 }
NeilBrown99c0fb52009-03-31 14:39:38 +11003055 break;
3056 case ALGORITHM_LEFT_ASYMMETRIC_6:
3057 case ALGORITHM_RIGHT_ASYMMETRIC_6:
3058 if (i > sh->pd_idx)
3059 i--;
3060 break;
3061 case ALGORITHM_LEFT_SYMMETRIC_6:
3062 case ALGORITHM_RIGHT_SYMMETRIC_6:
3063 if (i < sh->pd_idx)
3064 i += data_disks + 1;
3065 i -= (sh->pd_idx + 1);
3066 break;
3067 case ALGORITHM_PARITY_0_6:
3068 i -= 1;
3069 break;
NeilBrown16a53ec2006-06-26 00:27:38 -07003070 default:
NeilBrown99c0fb52009-03-31 14:39:38 +11003071 BUG();
NeilBrown16a53ec2006-06-26 00:27:38 -07003072 }
3073 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 }
3075
3076 chunk_number = stripe * data_disks + i;
NeilBrown35f2a592010-04-20 14:13:34 +10003077 r_sector = chunk_number * sectors_per_chunk + chunk_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078
NeilBrown112bf892009-03-31 14:39:38 +11003079 check = raid5_compute_sector(conf, r_sector,
NeilBrown784052e2009-03-31 15:19:07 +11003080 previous, &dummy1, &sh2);
NeilBrown911d4ee2009-03-31 14:39:38 +11003081 if (check != sh->sector || dummy1 != dd_idx || sh2.pd_idx != sh->pd_idx
3082 || sh2.qd_idx != sh->qd_idx) {
NeilBrowncc6167b2016-11-02 14:16:50 +11003083 pr_warn("md/raid:%s: compute_blocknr: map not correct\n",
3084 mdname(conf->mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 return 0;
3086 }
3087 return r_sector;
3088}
3089
Song Liu07e83362017-01-23 17:12:58 -08003090/*
3091 * There are cases where we want handle_stripe_dirtying() and
3092 * schedule_reconstruction() to delay towrite to some dev of a stripe.
3093 *
3094 * This function checks whether we want to delay the towrite. Specifically,
3095 * we delay the towrite when:
3096 *
3097 * 1. degraded stripe has a non-overwrite to the missing dev, AND this
3098 * stripe has data in journal (for other devices).
3099 *
3100 * In this case, when reading data for the non-overwrite dev, it is
3101 * necessary to handle complex rmw of write back cache (prexor with
3102 * orig_page, and xor with page). To keep read path simple, we would
3103 * like to flush data in journal to RAID disks first, so complex rmw
3104 * is handled in the write patch (handle_stripe_dirtying).
3105 *
Song Liu39b99582017-01-24 14:08:23 -08003106 * 2. when journal space is critical (R5C_LOG_CRITICAL=1)
3107 *
3108 * It is important to be able to flush all stripes in raid5-cache.
3109 * Therefore, we need reserve some space on the journal device for
3110 * these flushes. If flush operation includes pending writes to the
3111 * stripe, we need to reserve (conf->raid_disk + 1) pages per stripe
3112 * for the flush out. If we exclude these pending writes from flush
3113 * operation, we only need (conf->max_degraded + 1) pages per stripe.
3114 * Therefore, excluding pending writes in these cases enables more
3115 * efficient use of the journal device.
3116 *
3117 * Note: To make sure the stripe makes progress, we only delay
3118 * towrite for stripes with data already in journal (injournal > 0).
3119 * When LOG_CRITICAL, stripes with injournal == 0 will be sent to
3120 * no_space_stripes list.
3121 *
Song Liu70d466f2017-05-11 15:28:28 -07003122 * 3. during journal failure
3123 * In journal failure, we try to flush all cached data to raid disks
3124 * based on data in stripe cache. The array is read-only to upper
3125 * layers, so we would skip all pending writes.
3126 *
Song Liu07e83362017-01-23 17:12:58 -08003127 */
Song Liu39b99582017-01-24 14:08:23 -08003128static inline bool delay_towrite(struct r5conf *conf,
3129 struct r5dev *dev,
3130 struct stripe_head_state *s)
Song Liu07e83362017-01-23 17:12:58 -08003131{
Song Liu39b99582017-01-24 14:08:23 -08003132 /* case 1 above */
3133 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3134 !test_bit(R5_Insync, &dev->flags) && s->injournal)
3135 return true;
3136 /* case 2 above */
3137 if (test_bit(R5C_LOG_CRITICAL, &conf->cache_state) &&
3138 s->injournal > 0)
3139 return true;
Song Liu70d466f2017-05-11 15:28:28 -07003140 /* case 3 above */
3141 if (s->log_failed && s->injournal)
3142 return true;
Song Liu39b99582017-01-24 14:08:23 -08003143 return false;
Song Liu07e83362017-01-23 17:12:58 -08003144}
3145
Dan Williams600aa102008-06-28 08:32:05 +10003146static void
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003147schedule_reconstruction(struct stripe_head *sh, struct stripe_head_state *s,
Dan Williams600aa102008-06-28 08:32:05 +10003148 int rcw, int expand)
Dan Williamse33129d2007-01-02 13:52:30 -07003149{
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003150 int i, pd_idx = sh->pd_idx, qd_idx = sh->qd_idx, disks = sh->disks;
NeilBrownd1688a62011-10-11 16:49:52 +11003151 struct r5conf *conf = sh->raid_conf;
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003152 int level = conf->level;
NeilBrown16a53ec2006-06-26 00:27:38 -07003153
Dan Williamse33129d2007-01-02 13:52:30 -07003154 if (rcw) {
Song Liu1e6d6902016-11-17 15:24:39 -08003155 /*
3156 * In some cases, handle_stripe_dirtying initially decided to
3157 * run rmw and allocates extra page for prexor. However, rcw is
3158 * cheaper later on. We need to free the extra page now,
3159 * because we won't be able to do that in ops_complete_prexor().
3160 */
3161 r5c_release_extra_page(sh);
Dan Williamse33129d2007-01-02 13:52:30 -07003162
3163 for (i = disks; i--; ) {
3164 struct r5dev *dev = &sh->dev[i];
3165
Song Liu39b99582017-01-24 14:08:23 -08003166 if (dev->towrite && !delay_towrite(conf, dev, s)) {
Dan Williamse33129d2007-01-02 13:52:30 -07003167 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsd8ee0722008-06-28 08:32:06 +10003168 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003169 if (!expand)
3170 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003171 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003172 } else if (test_bit(R5_InJournal, &dev->flags)) {
3173 set_bit(R5_LOCKED, &dev->flags);
3174 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003175 }
3176 }
NeilBrownce7d3632013-03-04 12:37:14 +11003177 /* if we are not expanding this is a proper write request, and
3178 * there will be bios with new data to be drained into the
3179 * stripe cache
3180 */
3181 if (!expand) {
3182 if (!s->locked)
3183 /* False alarm, nothing to do */
3184 return;
3185 sh->reconstruct_state = reconstruct_state_drain_run;
3186 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3187 } else
3188 sh->reconstruct_state = reconstruct_state_run;
3189
3190 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
3191
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003192 if (s->locked + conf->max_degraded == disks)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003193 if (!test_and_set_bit(STRIPE_FULL_WRITE, &sh->state))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003194 atomic_inc(&conf->pending_full_writes);
Dan Williamse33129d2007-01-02 13:52:30 -07003195 } else {
3196 BUG_ON(!(test_bit(R5_UPTODATE, &sh->dev[pd_idx].flags) ||
3197 test_bit(R5_Wantcompute, &sh->dev[pd_idx].flags)));
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003198 BUG_ON(level == 6 &&
3199 (!(test_bit(R5_UPTODATE, &sh->dev[qd_idx].flags) ||
3200 test_bit(R5_Wantcompute, &sh->dev[qd_idx].flags))));
Dan Williamse33129d2007-01-02 13:52:30 -07003201
Dan Williamse33129d2007-01-02 13:52:30 -07003202 for (i = disks; i--; ) {
3203 struct r5dev *dev = &sh->dev[i];
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003204 if (i == pd_idx || i == qd_idx)
Dan Williamse33129d2007-01-02 13:52:30 -07003205 continue;
3206
Dan Williamse33129d2007-01-02 13:52:30 -07003207 if (dev->towrite &&
3208 (test_bit(R5_UPTODATE, &dev->flags) ||
Dan Williamsd8ee0722008-06-28 08:32:06 +10003209 test_bit(R5_Wantcompute, &dev->flags))) {
3210 set_bit(R5_Wantdrain, &dev->flags);
Dan Williamse33129d2007-01-02 13:52:30 -07003211 set_bit(R5_LOCKED, &dev->flags);
3212 clear_bit(R5_UPTODATE, &dev->flags);
Dan Williams600aa102008-06-28 08:32:05 +10003213 s->locked++;
Song Liu1e6d6902016-11-17 15:24:39 -08003214 } else if (test_bit(R5_InJournal, &dev->flags)) {
3215 set_bit(R5_LOCKED, &dev->flags);
3216 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003217 }
3218 }
NeilBrownce7d3632013-03-04 12:37:14 +11003219 if (!s->locked)
3220 /* False alarm - nothing to do */
3221 return;
3222 sh->reconstruct_state = reconstruct_state_prexor_drain_run;
3223 set_bit(STRIPE_OP_PREXOR, &s->ops_request);
3224 set_bit(STRIPE_OP_BIODRAIN, &s->ops_request);
3225 set_bit(STRIPE_OP_RECONSTRUCT, &s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003226 }
3227
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003228 /* keep the parity disk(s) locked while asynchronous operations
Dan Williamse33129d2007-01-02 13:52:30 -07003229 * are in flight
3230 */
3231 set_bit(R5_LOCKED, &sh->dev[pd_idx].flags);
3232 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
Dan Williams600aa102008-06-28 08:32:05 +10003233 s->locked++;
Dan Williamse33129d2007-01-02 13:52:30 -07003234
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07003235 if (level == 6) {
3236 int qd_idx = sh->qd_idx;
3237 struct r5dev *dev = &sh->dev[qd_idx];
3238
3239 set_bit(R5_LOCKED, &dev->flags);
3240 clear_bit(R5_UPTODATE, &dev->flags);
3241 s->locked++;
3242 }
3243
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02003244 if (raid5_has_ppl(sh->raid_conf) && sh->ppl_page &&
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003245 test_bit(STRIPE_OP_BIODRAIN, &s->ops_request) &&
3246 !test_bit(STRIPE_FULL_WRITE, &sh->state) &&
3247 test_bit(R5_Insync, &sh->dev[pd_idx].flags))
3248 set_bit(STRIPE_OP_PARTIAL_PARITY, &s->ops_request);
3249
Dan Williams600aa102008-06-28 08:32:05 +10003250 pr_debug("%s: stripe %llu locked: %d ops_request: %lx\n",
Harvey Harrisone46b272b2008-04-28 02:15:50 -07003251 __func__, (unsigned long long)sh->sector,
Dan Williams600aa102008-06-28 08:32:05 +10003252 s->locked, s->ops_request);
Dan Williamse33129d2007-01-02 13:52:30 -07003253}
NeilBrown16a53ec2006-06-26 00:27:38 -07003254
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255/*
3256 * Each stripe/dev can have one or more bion attached.
NeilBrown16a53ec2006-06-26 00:27:38 -07003257 * toread/towrite point to the first in a chain.
Linus Torvalds1da177e2005-04-16 15:20:36 -07003258 * The bi_next chain must be in order.
3259 */
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003260static int add_stripe_bio(struct stripe_head *sh, struct bio *bi, int dd_idx,
3261 int forwrite, int previous)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262{
3263 struct bio **bip;
NeilBrownd1688a62011-10-11 16:49:52 +11003264 struct r5conf *conf = sh->raid_conf;
NeilBrown72626682005-09-09 16:23:54 -07003265 int firstwrite=0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266
NeilBrowncbe47ec2011-07-26 11:20:35 +10003267 pr_debug("adding bi b#%llu to stripe s#%llu\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003268 (unsigned long long)bi->bi_iter.bi_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 (unsigned long long)sh->sector);
3270
Shaohua Lib17459c2012-07-19 16:01:31 +10003271 spin_lock_irq(&sh->stripe_lock);
Mariusz Dabrowski2cd259a2018-04-19 19:28:10 +02003272 sh->dev[dd_idx].write_hint = bi->bi_write_hint;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003273 /* Don't allow new IO added to stripes in batch list */
3274 if (sh->batch_head)
3275 goto overlap;
NeilBrown72626682005-09-09 16:23:54 -07003276 if (forwrite) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003277 bip = &sh->dev[dd_idx].towrite;
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003278 if (*bip == NULL)
NeilBrown72626682005-09-09 16:23:54 -07003279 firstwrite = 1;
3280 } else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003281 bip = &sh->dev[dd_idx].toread;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003282 while (*bip && (*bip)->bi_iter.bi_sector < bi->bi_iter.bi_sector) {
3283 if (bio_end_sector(*bip) > bi->bi_iter.bi_sector)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003284 goto overlap;
3285 bip = & (*bip)->bi_next;
3286 }
Kent Overstreet4f024f32013-10-11 15:44:27 -07003287 if (*bip && (*bip)->bi_iter.bi_sector < bio_end_sector(bi))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288 goto overlap;
3289
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01003290 if (forwrite && raid5_has_ppl(conf)) {
3291 /*
3292 * With PPL only writes to consecutive data chunks within a
3293 * stripe are allowed because for a single stripe_head we can
3294 * only have one PPL entry at a time, which describes one data
3295 * range. Not really an overlap, but wait_for_overlap can be
3296 * used to handle this.
3297 */
3298 sector_t sector;
3299 sector_t first = 0;
3300 sector_t last = 0;
3301 int count = 0;
3302 int i;
3303
3304 for (i = 0; i < sh->disks; i++) {
3305 if (i != sh->pd_idx &&
3306 (i == dd_idx || sh->dev[i].towrite)) {
3307 sector = sh->dev[i].sector;
3308 if (count == 0 || sector < first)
3309 first = sector;
3310 if (sector > last)
3311 last = sector;
3312 count++;
3313 }
3314 }
3315
3316 if (first + conf->chunk_sectors * (count - 1) != last)
3317 goto overlap;
3318 }
3319
shli@kernel.orgda41ba62014-12-15 12:57:03 +11003320 if (!forwrite || previous)
3321 clear_bit(STRIPE_BATCH_READY, &sh->state);
3322
Eric Sesterhenn78bafeb2006-04-02 13:31:42 +02003323 BUG_ON(*bip && bi->bi_next && (*bip) != bi->bi_next);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003324 if (*bip)
3325 bi->bi_next = *bip;
3326 *bip = bi;
NeilBrown016c76a2017-03-15 14:05:13 +11003327 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11003328 md_write_inc(conf->mddev, bi);
NeilBrown72626682005-09-09 16:23:54 -07003329
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 if (forwrite) {
3331 /* check if page is covered */
3332 sector_t sector = sh->dev[dd_idx].sector;
3333 for (bi=sh->dev[dd_idx].towrite;
Yufen Yuc911c462020-07-18 05:29:07 -04003334 sector < sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf) &&
Kent Overstreet4f024f32013-10-11 15:44:27 -07003335 bi && bi->bi_iter.bi_sector <= sector;
Yufen Yuc911c462020-07-18 05:29:07 -04003336 bi = r5_next_bio(conf, bi, sh->dev[dd_idx].sector)) {
Kent Overstreetf73a1c72012-09-25 15:05:12 -07003337 if (bio_end_sector(bi) >= sector)
3338 sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003339 }
Yufen Yuc911c462020-07-18 05:29:07 -04003340 if (sector >= sh->dev[dd_idx].sector + RAID5_STRIPE_SECTORS(conf))
shli@kernel.org7a87f432014-12-15 12:57:03 +11003341 if (!test_and_set_bit(R5_OVERWRITE, &sh->dev[dd_idx].flags))
3342 sh->overwrite_disks++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003344
3345 pr_debug("added bi b#%llu to stripe s#%llu, disk %d.\n",
Kent Overstreet4f024f32013-10-11 15:44:27 -07003346 (unsigned long long)(*bip)->bi_iter.bi_sector,
NeilBrowncbe47ec2011-07-26 11:20:35 +10003347 (unsigned long long)sh->sector, dd_idx);
3348
3349 if (conf->mddev->bitmap && firstwrite) {
NeilBrownd0852df52015-05-27 08:43:45 +10003350 /* Cannot hold spinlock over bitmap_startwrite,
3351 * but must ensure this isn't added to a batch until
3352 * we have added to the bitmap and set bm_seq.
3353 * So set STRIPE_BITMAP_PENDING to prevent
3354 * batching.
3355 * If multiple add_stripe_bio() calls race here they
3356 * much all set STRIPE_BITMAP_PENDING. So only the first one
3357 * to complete "bitmap_startwrite" gets to set
3358 * STRIPE_BIT_DELAY. This is important as once a stripe
3359 * is added to a batch, STRIPE_BIT_DELAY cannot be changed
3360 * any more.
3361 */
3362 set_bit(STRIPE_BITMAP_PENDING, &sh->state);
3363 spin_unlock_irq(&sh->stripe_lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003364 md_bitmap_startwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003365 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrownd0852df52015-05-27 08:43:45 +10003366 spin_lock_irq(&sh->stripe_lock);
3367 clear_bit(STRIPE_BITMAP_PENDING, &sh->state);
3368 if (!sh->batch_head) {
3369 sh->bm_seq = conf->seq_flush+1;
3370 set_bit(STRIPE_BIT_DELAY, &sh->state);
3371 }
NeilBrowncbe47ec2011-07-26 11:20:35 +10003372 }
NeilBrownd0852df52015-05-27 08:43:45 +10003373 spin_unlock_irq(&sh->stripe_lock);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003374
3375 if (stripe_can_batch(sh))
3376 stripe_add_to_batch_list(conf, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 return 1;
3378
3379 overlap:
3380 set_bit(R5_Overlap, &sh->dev[dd_idx].flags);
Shaohua Lib17459c2012-07-19 16:01:31 +10003381 spin_unlock_irq(&sh->stripe_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382 return 0;
3383}
3384
NeilBrownd1688a62011-10-11 16:49:52 +11003385static void end_reshape(struct r5conf *conf);
NeilBrown29269552006-03-27 01:18:10 -08003386
NeilBrownd1688a62011-10-11 16:49:52 +11003387static void stripe_set_idx(sector_t stripe, struct r5conf *conf, int previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003388 struct stripe_head *sh)
NeilBrownccfcc3c2006-03-27 01:18:09 -08003389{
NeilBrown784052e2009-03-31 15:19:07 +11003390 int sectors_per_chunk =
Andre Noll09c9e5f2009-06-18 08:45:55 +10003391 previous ? conf->prev_chunk_sectors : conf->chunk_sectors;
NeilBrown911d4ee2009-03-31 14:39:38 +11003392 int dd_idx;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003393 int chunk_offset = sector_div(stripe, sectors_per_chunk);
NeilBrown112bf892009-03-31 14:39:38 +11003394 int disks = previous ? conf->previous_raid_disks : conf->raid_disks;
Coywolf Qi Hunt2d2063c2006-10-03 01:15:50 -07003395
NeilBrown112bf892009-03-31 14:39:38 +11003396 raid5_compute_sector(conf,
3397 stripe * (disks - conf->max_degraded)
NeilBrownb875e532006-12-10 02:20:49 -08003398 *sectors_per_chunk + chunk_offset,
NeilBrown112bf892009-03-31 14:39:38 +11003399 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11003400 &dd_idx, sh);
NeilBrownccfcc3c2006-03-27 01:18:09 -08003401}
3402
Dan Williamsa4456852007-07-09 11:56:43 -07003403static void
NeilBrownd1688a62011-10-11 16:49:52 +11003404handle_failed_stripe(struct r5conf *conf, struct stripe_head *sh,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003405 struct stripe_head_state *s, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003406{
3407 int i;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003408 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07003409 for (i = disks; i--; ) {
3410 struct bio *bi;
3411 int bitmap_end = 0;
3412
3413 if (test_bit(R5_ReadError, &sh->dev[i].flags)) {
NeilBrown3cb03002011-10-11 16:45:26 +11003414 struct md_rdev *rdev;
Dan Williamsa4456852007-07-09 11:56:43 -07003415 rcu_read_lock();
3416 rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrownf5b67ae2016-06-02 16:19:53 +10003417 if (rdev && test_bit(In_sync, &rdev->flags) &&
3418 !test_bit(Faulty, &rdev->flags))
NeilBrown7f0da592011-07-28 11:39:22 +10003419 atomic_inc(&rdev->nr_pending);
3420 else
3421 rdev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07003422 rcu_read_unlock();
NeilBrown7f0da592011-07-28 11:39:22 +10003423 if (rdev) {
3424 if (!rdev_set_badblocks(
3425 rdev,
3426 sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003427 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown7f0da592011-07-28 11:39:22 +10003428 md_error(conf->mddev, rdev);
3429 rdev_dec_pending(rdev, conf->mddev);
3430 }
Dan Williamsa4456852007-07-09 11:56:43 -07003431 }
Shaohua Lib17459c2012-07-19 16:01:31 +10003432 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003433 /* fail all writes first */
3434 bi = sh->dev[i].towrite;
3435 sh->dev[i].towrite = NULL;
shli@kernel.org7a87f432014-12-15 12:57:03 +11003436 sh->overwrite_disks = 0;
Shaohua Lib17459c2012-07-19 16:01:31 +10003437 spin_unlock_irq(&sh->stripe_lock);
NeilBrown1ed850f2012-10-11 13:50:13 +11003438 if (bi)
Dan Williamsa4456852007-07-09 11:56:43 -07003439 bitmap_end = 1;
Dan Williamsa4456852007-07-09 11:56:43 -07003440
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003441 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003442
Dan Williamsa4456852007-07-09 11:56:43 -07003443 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3444 wake_up(&conf->wait_for_overlap);
3445
Kent Overstreet4f024f32013-10-11 15:44:27 -07003446 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003447 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3448 struct bio *nextbi = r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003449
NeilBrown49728052017-03-15 14:05:12 +11003450 md_write_end(conf->mddev);
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003451 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003452 bi = nextbi;
3453 }
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003454 if (bitmap_end)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003455 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003456 RAID5_STRIPE_SECTORS(conf), 0, 0);
Shaohua Li7eaf7e82012-07-19 16:01:31 +10003457 bitmap_end = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003458 /* and fail all 'written' */
3459 bi = sh->dev[i].written;
3460 sh->dev[i].written = NULL;
Shaohua Lid592a992014-05-21 17:57:44 +08003461 if (test_and_clear_bit(R5_SkipCopy, &sh->dev[i].flags)) {
3462 WARN_ON(test_bit(R5_UPTODATE, &sh->dev[i].flags));
3463 sh->dev[i].page = sh->dev[i].orig_page;
3464 }
3465
Dan Williamsa4456852007-07-09 11:56:43 -07003466 if (bi) bitmap_end = 1;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003467 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003468 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
3469 struct bio *bi2 = r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003470
NeilBrown49728052017-03-15 14:05:12 +11003471 md_write_end(conf->mddev);
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003472 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003473 bi = bi2;
3474 }
3475
Dan Williamsb5e98d62007-01-02 13:52:31 -07003476 /* fail any reads if this device is non-operational and
3477 * the data has not reached the cache yet.
3478 */
3479 if (!test_bit(R5_Wantfill, &sh->dev[i].flags) &&
Shaohua Li6e74a9c2015-10-08 21:54:08 -07003480 s->failed > conf->max_degraded &&
Dan Williamsb5e98d62007-01-02 13:52:31 -07003481 (!test_bit(R5_Insync, &sh->dev[i].flags) ||
3482 test_bit(R5_ReadError, &sh->dev[i].flags))) {
NeilBrown143c4d02012-10-11 13:50:12 +11003483 spin_lock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003484 bi = sh->dev[i].toread;
3485 sh->dev[i].toread = NULL;
NeilBrown143c4d02012-10-11 13:50:12 +11003486 spin_unlock_irq(&sh->stripe_lock);
Dan Williamsa4456852007-07-09 11:56:43 -07003487 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
3488 wake_up(&conf->wait_for_overlap);
Shaohua Liebda7802015-09-18 10:20:13 -07003489 if (bi)
3490 s->to_read--;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003491 while (bi && bi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003492 sh->dev[i].sector + RAID5_STRIPE_SECTORS(conf)) {
Dan Williamsa4456852007-07-09 11:56:43 -07003493 struct bio *nextbi =
Yufen Yuc911c462020-07-18 05:29:07 -04003494 r5_next_bio(conf, bi, sh->dev[i].sector);
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02003495
Guoqing Jiang6308d8e2017-07-21 16:33:44 +08003496 bio_io_error(bi);
Dan Williamsa4456852007-07-09 11:56:43 -07003497 bi = nextbi;
3498 }
3499 }
Dan Williamsa4456852007-07-09 11:56:43 -07003500 if (bitmap_end)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003501 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003502 RAID5_STRIPE_SECTORS(conf), 0, 0);
NeilBrown8cfa7b02011-07-27 11:00:36 +10003503 /* If we were in the middle of a write the parity block might
3504 * still be locked - so just clear all R5_LOCKED flags
3505 */
3506 clear_bit(R5_LOCKED, &sh->dev[i].flags);
Dan Williamsa4456852007-07-09 11:56:43 -07003507 }
Shaohua Liebda7802015-09-18 10:20:13 -07003508 s->to_write = 0;
3509 s->written = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07003510
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003511 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3512 if (atomic_dec_and_test(&conf->pending_full_writes))
3513 md_wakeup_thread(conf->mddev->thread);
Dan Williamsa4456852007-07-09 11:56:43 -07003514}
3515
NeilBrown7f0da592011-07-28 11:39:22 +10003516static void
NeilBrownd1688a62011-10-11 16:49:52 +11003517handle_failed_sync(struct r5conf *conf, struct stripe_head *sh,
NeilBrown7f0da592011-07-28 11:39:22 +10003518 struct stripe_head_state *s)
3519{
3520 int abort = 0;
3521 int i;
3522
shli@kernel.org59fc6302014-12-15 12:57:03 +11003523 BUG_ON(sh->batch_head);
NeilBrown7f0da592011-07-28 11:39:22 +10003524 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11003525 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
3526 wake_up(&conf->wait_for_overlap);
NeilBrown7f0da592011-07-28 11:39:22 +10003527 s->syncing = 0;
NeilBrown9a3e1102011-12-23 10:17:53 +11003528 s->replacing = 0;
NeilBrown7f0da592011-07-28 11:39:22 +10003529 /* There is nothing more to do for sync/check/repair.
NeilBrown18b98372012-04-01 23:48:38 +10003530 * Don't even need to abort as that is handled elsewhere
3531 * if needed, and not always wanted e.g. if there is a known
3532 * bad block here.
NeilBrown9a3e1102011-12-23 10:17:53 +11003533 * For recover/replace we need to record a bad block on all
NeilBrown7f0da592011-07-28 11:39:22 +10003534 * non-sync devices, or abort the recovery
3535 */
NeilBrown18b98372012-04-01 23:48:38 +10003536 if (test_bit(MD_RECOVERY_RECOVER, &conf->mddev->recovery)) {
3537 /* During recovery devices cannot be removed, so
3538 * locking and refcounting of rdevs is not needed
3539 */
NeilBrowne50d3992016-06-02 16:19:52 +10003540 rcu_read_lock();
NeilBrown18b98372012-04-01 23:48:38 +10003541 for (i = 0; i < conf->raid_disks; i++) {
NeilBrowne50d3992016-06-02 16:19:52 +10003542 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
NeilBrown18b98372012-04-01 23:48:38 +10003543 if (rdev
3544 && !test_bit(Faulty, &rdev->flags)
3545 && !test_bit(In_sync, &rdev->flags)
3546 && !rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003547 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown18b98372012-04-01 23:48:38 +10003548 abort = 1;
NeilBrowne50d3992016-06-02 16:19:52 +10003549 rdev = rcu_dereference(conf->disks[i].replacement);
NeilBrown18b98372012-04-01 23:48:38 +10003550 if (rdev
3551 && !test_bit(Faulty, &rdev->flags)
3552 && !test_bit(In_sync, &rdev->flags)
3553 && !rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003554 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrown18b98372012-04-01 23:48:38 +10003555 abort = 1;
3556 }
NeilBrowne50d3992016-06-02 16:19:52 +10003557 rcu_read_unlock();
NeilBrown18b98372012-04-01 23:48:38 +10003558 if (abort)
3559 conf->recovery_disabled =
3560 conf->mddev->recovery_disabled;
NeilBrown7f0da592011-07-28 11:39:22 +10003561 }
Yufen Yuc911c462020-07-18 05:29:07 -04003562 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), !abort);
NeilBrown7f0da592011-07-28 11:39:22 +10003563}
3564
NeilBrown9a3e1102011-12-23 10:17:53 +11003565static int want_replace(struct stripe_head *sh, int disk_idx)
3566{
3567 struct md_rdev *rdev;
3568 int rv = 0;
NeilBrown3f232d62016-06-02 16:19:52 +10003569
3570 rcu_read_lock();
3571 rdev = rcu_dereference(sh->raid_conf->disks[disk_idx].replacement);
NeilBrown9a3e1102011-12-23 10:17:53 +11003572 if (rdev
3573 && !test_bit(Faulty, &rdev->flags)
3574 && !test_bit(In_sync, &rdev->flags)
3575 && (rdev->recovery_offset <= sh->sector
3576 || rdev->mddev->recovery_cp <= sh->sector))
3577 rv = 1;
NeilBrown3f232d62016-06-02 16:19:52 +10003578 rcu_read_unlock();
NeilBrown9a3e1102011-12-23 10:17:53 +11003579 return rv;
3580}
3581
NeilBrown2c58f062015-02-02 11:32:23 +11003582static int need_this_block(struct stripe_head *sh, struct stripe_head_state *s,
3583 int disk_idx, int disks)
Dan Williamsf38e1212007-01-02 13:52:30 -07003584{
3585 struct r5dev *dev = &sh->dev[disk_idx];
NeilBrown93b3dbc2011-07-27 11:00:36 +10003586 struct r5dev *fdev[2] = { &sh->dev[s->failed_num[0]],
3587 &sh->dev[s->failed_num[1]] };
NeilBrownea664c82015-02-02 14:03:28 +11003588 int i;
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003589 bool force_rcw = (sh->raid_conf->rmw_level == PARITY_DISABLE_RMW);
Dan Williamsf38e1212007-01-02 13:52:30 -07003590
NeilBrowna79cfe12015-02-02 11:37:59 +11003591
3592 if (test_bit(R5_LOCKED, &dev->flags) ||
3593 test_bit(R5_UPTODATE, &dev->flags))
3594 /* No point reading this as we already have it or have
3595 * decided to get it.
3596 */
3597 return 0;
3598
3599 if (dev->toread ||
3600 (dev->towrite && !test_bit(R5_OVERWRITE, &dev->flags)))
3601 /* We need this block to directly satisfy a request */
3602 return 1;
3603
3604 if (s->syncing || s->expanding ||
3605 (s->replacing && want_replace(sh, disk_idx)))
3606 /* When syncing, or expanding we read everything.
3607 * When replacing, we need the replaced block.
3608 */
3609 return 1;
3610
3611 if ((s->failed >= 1 && fdev[0]->toread) ||
3612 (s->failed >= 2 && fdev[1]->toread))
3613 /* If we want to read from a failed device, then
3614 * we need to actually read every other device.
3615 */
3616 return 1;
3617
NeilBrowna9d56952015-02-02 11:49:10 +11003618 /* Sometimes neither read-modify-write nor reconstruct-write
3619 * cycles can work. In those cases we read every block we
3620 * can. Then the parity-update is certain to have enough to
3621 * work with.
3622 * This can only be a problem when we need to write something,
3623 * and some device has failed. If either of those tests
3624 * fail we need look no further.
3625 */
3626 if (!s->failed || !s->to_write)
3627 return 0;
3628
3629 if (test_bit(R5_Insync, &dev->flags) &&
3630 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
3631 /* Pre-reads at not permitted until after short delay
3632 * to gather multiple requests. However if this
Zhilong Liu3560741e2017-03-15 16:14:53 +08003633 * device is no Insync, the block could only be computed
NeilBrowna9d56952015-02-02 11:49:10 +11003634 * and there is no need to delay that.
3635 */
3636 return 0;
NeilBrownea664c82015-02-02 14:03:28 +11003637
NeilBrown36707bb2015-09-24 15:25:36 +10003638 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrownea664c82015-02-02 14:03:28 +11003639 if (fdev[i]->towrite &&
3640 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
3641 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3642 /* If we have a partial write to a failed
3643 * device, then we will need to reconstruct
3644 * the content of that device, so all other
3645 * devices must be read.
3646 */
3647 return 1;
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003648
3649 if (s->failed >= 2 &&
3650 (fdev[i]->towrite ||
3651 s->failed_num[i] == sh->pd_idx ||
3652 s->failed_num[i] == sh->qd_idx) &&
3653 !test_bit(R5_UPTODATE, &fdev[i]->flags))
3654 /* In max degraded raid6, If the failed disk is P, Q,
3655 * or we want to read the failed disk, we need to do
3656 * reconstruct-write.
3657 */
3658 force_rcw = true;
NeilBrownea664c82015-02-02 14:03:28 +11003659 }
3660
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003661 /* If we are forced to do a reconstruct-write, because parity
3662 * cannot be trusted and we are currently recovering it, there
3663 * is extra need to be careful.
NeilBrownea664c82015-02-02 14:03:28 +11003664 * If one of the devices that we would need to read, because
3665 * it is not being overwritten (and maybe not written at all)
3666 * is missing/faulty, then we need to read everything we can.
3667 */
ChangSyun Peng45a4d8f2020-07-31 17:50:31 +08003668 if (!force_rcw &&
NeilBrownea664c82015-02-02 14:03:28 +11003669 sh->sector < sh->raid_conf->mddev->recovery_cp)
3670 /* reconstruct-write isn't being forced */
3671 return 0;
NeilBrown36707bb2015-09-24 15:25:36 +10003672 for (i = 0; i < s->failed && i < 2; i++) {
NeilBrown10d82c52015-05-08 18:19:33 +10003673 if (s->failed_num[i] != sh->pd_idx &&
3674 s->failed_num[i] != sh->qd_idx &&
3675 !test_bit(R5_UPTODATE, &fdev[i]->flags) &&
NeilBrownea664c82015-02-02 14:03:28 +11003676 !test_bit(R5_OVERWRITE, &fdev[i]->flags))
3677 return 1;
3678 }
3679
NeilBrown2c58f062015-02-02 11:32:23 +11003680 return 0;
3681}
3682
Song Liuba026842017-01-12 17:22:42 -08003683/* fetch_block - checks the given member device to see if its data needs
3684 * to be read or computed to satisfy a request.
3685 *
3686 * Returns 1 when no more member devices need to be checked, otherwise returns
3687 * 0 to tell the loop in handle_stripe_fill to continue
3688 */
NeilBrown2c58f062015-02-02 11:32:23 +11003689static int fetch_block(struct stripe_head *sh, struct stripe_head_state *s,
3690 int disk_idx, int disks)
3691{
3692 struct r5dev *dev = &sh->dev[disk_idx];
3693
3694 /* is the data in this block needed, and can we get it? */
3695 if (need_this_block(sh, s, disk_idx, disks)) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003696 /* we would like to get this block, possibly by computing it,
3697 * otherwise read it if the backing disk is insync
3698 */
3699 BUG_ON(test_bit(R5_Wantcompute, &dev->flags));
3700 BUG_ON(test_bit(R5_Wantread, &dev->flags));
NeilBrownb0c783b2015-05-08 18:19:32 +10003701 BUG_ON(sh->batch_head);
NeilBrown7471fb72017-04-03 12:11:32 +10003702
3703 /*
3704 * In the raid6 case if the only non-uptodate disk is P
3705 * then we already trusted P to compute the other failed
3706 * drives. It is safe to compute rather than re-read P.
3707 * In other cases we only compute blocks from failed
3708 * devices, otherwise check/repair might fail to detect
3709 * a real inconsistency.
3710 */
3711
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003712 if ((s->uptodate == disks - 1) &&
NeilBrown7471fb72017-04-03 12:11:32 +10003713 ((sh->qd_idx >= 0 && sh->pd_idx == disk_idx) ||
NeilBrownf2b3b442011-07-26 11:35:19 +10003714 (s->failed && (disk_idx == s->failed_num[0] ||
NeilBrown7471fb72017-04-03 12:11:32 +10003715 disk_idx == s->failed_num[1])))) {
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003716 /* have disk failed, and we're requested to fetch it;
3717 * do compute it
3718 */
3719 pr_debug("Computing stripe %llu block %d\n",
3720 (unsigned long long)sh->sector, disk_idx);
3721 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3722 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3723 set_bit(R5_Wantcompute, &dev->flags);
3724 sh->ops.target = disk_idx;
3725 sh->ops.target2 = -1; /* no 2nd target */
3726 s->req_compute = 1;
NeilBrown93b3dbc2011-07-27 11:00:36 +10003727 /* Careful: from this point on 'uptodate' is in the eye
3728 * of raid_run_ops which services 'compute' operations
3729 * before writes. R5_Wantcompute flags a block that will
3730 * be R5_UPTODATE by the time it is needed for a
3731 * subsequent operation.
3732 */
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003733 s->uptodate++;
3734 return 1;
3735 } else if (s->uptodate == disks-2 && s->failed >= 2) {
3736 /* Computing 2-failure is *very* expensive; only
3737 * do it if failed >= 2
3738 */
3739 int other;
3740 for (other = disks; other--; ) {
3741 if (other == disk_idx)
3742 continue;
3743 if (!test_bit(R5_UPTODATE,
3744 &sh->dev[other].flags))
3745 break;
3746 }
3747 BUG_ON(other < 0);
3748 pr_debug("Computing stripe %llu blocks %d,%d\n",
3749 (unsigned long long)sh->sector,
3750 disk_idx, other);
3751 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
3752 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
3753 set_bit(R5_Wantcompute, &sh->dev[disk_idx].flags);
3754 set_bit(R5_Wantcompute, &sh->dev[other].flags);
3755 sh->ops.target = disk_idx;
3756 sh->ops.target2 = other;
3757 s->uptodate += 2;
3758 s->req_compute = 1;
3759 return 1;
3760 } else if (test_bit(R5_Insync, &dev->flags)) {
3761 set_bit(R5_LOCKED, &dev->flags);
3762 set_bit(R5_Wantread, &dev->flags);
3763 s->locked++;
3764 pr_debug("Reading block %d (sync=%d)\n",
3765 disk_idx, s->syncing);
3766 }
3767 }
3768
3769 return 0;
3770}
3771
Damien Le Moal2aada5b2020-07-16 13:54:42 +09003772/*
NeilBrown93b3dbc2011-07-27 11:00:36 +10003773 * handle_stripe_fill - read or compute data to satisfy pending requests.
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003774 */
NeilBrown93b3dbc2011-07-27 11:00:36 +10003775static void handle_stripe_fill(struct stripe_head *sh,
3776 struct stripe_head_state *s,
3777 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003778{
3779 int i;
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003780
3781 /* look for blocks to read/compute, skip this if a compute
3782 * is already in flight, or if the stripe contents are in the
3783 * midst of changing due to a write
3784 */
3785 if (!test_bit(STRIPE_COMPUTE_RUN, &sh->state) && !sh->check_state &&
Song Liu07e83362017-01-23 17:12:58 -08003786 !sh->reconstruct_state) {
3787
3788 /*
3789 * For degraded stripe with data in journal, do not handle
3790 * read requests yet, instead, flush the stripe to raid
3791 * disks first, this avoids handling complex rmw of write
3792 * back cache (prexor with orig_page, and then xor with
3793 * page) in the read path
3794 */
3795 if (s->injournal && s->failed) {
3796 if (test_bit(STRIPE_R5C_CACHING, &sh->state))
3797 r5c_make_stripe_write_out(sh);
3798 goto out;
3799 }
3800
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003801 for (i = disks; i--; )
NeilBrown93b3dbc2011-07-27 11:00:36 +10003802 if (fetch_block(sh, s, i, disks))
Yuri Tikhonov5599bec2009-08-29 19:13:12 -07003803 break;
Song Liu07e83362017-01-23 17:12:58 -08003804 }
3805out:
Dan Williamsa4456852007-07-09 11:56:43 -07003806 set_bit(STRIPE_HANDLE, &sh->state);
3807}
3808
NeilBrown787b76f2015-05-21 12:56:41 +10003809static void break_stripe_batch_list(struct stripe_head *head_sh,
3810 unsigned long handle_flags);
Dan Williams1fe797e2008-06-28 09:16:30 +10003811/* handle_stripe_clean_event
Dan Williamsa4456852007-07-09 11:56:43 -07003812 * any written block on an uptodate or failed drive can be returned.
3813 * Note that if we 'wrote' to a failed drive, it will be UPTODATE, but
3814 * never LOCKED, so we don't need to test 'failed' directly.
3815 */
NeilBrownd1688a62011-10-11 16:49:52 +11003816static void handle_stripe_clean_event(struct r5conf *conf,
NeilBrownbd83d0a2017-03-15 14:05:12 +11003817 struct stripe_head *sh, int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003818{
3819 int i;
3820 struct r5dev *dev;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003821 int discard_pending = 0;
shli@kernel.org59fc6302014-12-15 12:57:03 +11003822 struct stripe_head *head_sh = sh;
3823 bool do_endio = false;
Dan Williamsa4456852007-07-09 11:56:43 -07003824
3825 for (i = disks; i--; )
3826 if (sh->dev[i].written) {
3827 dev = &sh->dev[i];
3828 if (!test_bit(R5_LOCKED, &dev->flags) &&
Shaohua Li9e4447682012-10-11 13:49:49 +11003829 (test_bit(R5_UPTODATE, &dev->flags) ||
Shaohua Lid592a992014-05-21 17:57:44 +08003830 test_bit(R5_Discard, &dev->flags) ||
3831 test_bit(R5_SkipCopy, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003832 /* We can return any write requests */
3833 struct bio *wbi, *wbi2;
Dan Williams45b42332007-07-09 11:56:43 -07003834 pr_debug("Return write for disc %d\n", i);
NeilBrownca64cae2012-11-21 16:33:40 +11003835 if (test_and_clear_bit(R5_Discard, &dev->flags))
3836 clear_bit(R5_UPTODATE, &dev->flags);
Shaohua Lid592a992014-05-21 17:57:44 +08003837 if (test_and_clear_bit(R5_SkipCopy, &dev->flags)) {
3838 WARN_ON(test_bit(R5_UPTODATE, &dev->flags));
Shaohua Lid592a992014-05-21 17:57:44 +08003839 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003840 do_endio = true;
3841
3842returnbi:
3843 dev->page = dev->orig_page;
Dan Williamsa4456852007-07-09 11:56:43 -07003844 wbi = dev->written;
3845 dev->written = NULL;
Kent Overstreet4f024f32013-10-11 15:44:27 -07003846 while (wbi && wbi->bi_iter.bi_sector <
Yufen Yuc911c462020-07-18 05:29:07 -04003847 dev->sector + RAID5_STRIPE_SECTORS(conf)) {
3848 wbi2 = r5_next_bio(conf, wbi, dev->sector);
NeilBrown49728052017-03-15 14:05:12 +11003849 md_write_end(conf->mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11003850 bio_endio(wbi);
Dan Williamsa4456852007-07-09 11:56:43 -07003851 wbi = wbi2;
3852 }
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003853 md_bitmap_endwrite(conf->mddev->bitmap, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04003854 RAID5_STRIPE_SECTORS(conf),
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07003855 !test_bit(STRIPE_DEGRADED, &sh->state),
3856 0);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003857 if (head_sh->batch_head) {
3858 sh = list_first_entry(&sh->batch_list,
3859 struct stripe_head,
3860 batch_list);
3861 if (sh != head_sh) {
3862 dev = &sh->dev[i];
3863 goto returnbi;
3864 }
3865 }
3866 sh = head_sh;
3867 dev = &sh->dev[i];
NeilBrownf8dfcff2013-03-12 12:18:06 +11003868 } else if (test_bit(R5_Discard, &dev->flags))
3869 discard_pending = 1;
3870 }
Shaohua Lif6bed0e2015-08-13 14:31:59 -07003871
Artur Paszkiewiczff875732017-03-09 09:59:58 +01003872 log_stripe_write_finished(sh);
Shaohua Li0576b1c2015-08-13 14:32:00 -07003873
NeilBrownf8dfcff2013-03-12 12:18:06 +11003874 if (!discard_pending &&
3875 test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags)) {
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003876 int hash;
NeilBrownf8dfcff2013-03-12 12:18:06 +11003877 clear_bit(R5_Discard, &sh->dev[sh->pd_idx].flags);
3878 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
3879 if (sh->qd_idx >= 0) {
3880 clear_bit(R5_Discard, &sh->dev[sh->qd_idx].flags);
3881 clear_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags);
3882 }
3883 /* now that discard is done we can proceed with any sync */
3884 clear_bit(STRIPE_DISCARD, &sh->state);
Shaohua Lid47648f2013-10-19 14:51:42 +08003885 /*
3886 * SCSI discard will change some bio fields and the stripe has
3887 * no updated data, so remove it from hash list and the stripe
3888 * will be reinitialized
3889 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11003890unhash:
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003891 hash = sh->hash_lock_index;
3892 spin_lock_irq(conf->hash_locks + hash);
Shaohua Lid47648f2013-10-19 14:51:42 +08003893 remove_hash(sh);
Roman Gushchinb8a9d662015-10-31 10:53:50 +11003894 spin_unlock_irq(conf->hash_locks + hash);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003895 if (head_sh->batch_head) {
3896 sh = list_first_entry(&sh->batch_list,
3897 struct stripe_head, batch_list);
3898 if (sh != head_sh)
3899 goto unhash;
3900 }
shli@kernel.org59fc6302014-12-15 12:57:03 +11003901 sh = head_sh;
3902
NeilBrownf8dfcff2013-03-12 12:18:06 +11003903 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state))
3904 set_bit(STRIPE_HANDLE, &sh->state);
3905
3906 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07003907
3908 if (test_and_clear_bit(STRIPE_FULL_WRITE, &sh->state))
3909 if (atomic_dec_and_test(&conf->pending_full_writes))
3910 md_wakeup_thread(conf->mddev->thread);
shli@kernel.org59fc6302014-12-15 12:57:03 +11003911
NeilBrown787b76f2015-05-21 12:56:41 +10003912 if (head_sh->batch_head && do_endio)
3913 break_stripe_batch_list(head_sh, STRIPE_EXPAND_SYNC_FLAGS);
Dan Williamsa4456852007-07-09 11:56:43 -07003914}
3915
Song Liu86aa1392017-01-12 17:22:41 -08003916/*
3917 * For RMW in write back cache, we need extra page in prexor to store the
3918 * old data. This page is stored in dev->orig_page.
3919 *
3920 * This function checks whether we have data for prexor. The exact logic
3921 * is:
3922 * R5_UPTODATE && (!R5_InJournal || R5_OrigPageUPTDODATE)
3923 */
3924static inline bool uptodate_for_rmw(struct r5dev *dev)
3925{
3926 return (test_bit(R5_UPTODATE, &dev->flags)) &&
3927 (!test_bit(R5_InJournal, &dev->flags) ||
3928 test_bit(R5_OrigPageUPTDODATE, &dev->flags));
3929}
3930
Song Liud7bd3982016-11-23 22:50:39 -08003931static int handle_stripe_dirtying(struct r5conf *conf,
3932 struct stripe_head *sh,
3933 struct stripe_head_state *s,
3934 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07003935{
3936 int rmw = 0, rcw = 0, i;
Alexander Lyakasa7854482012-10-11 13:50:12 +11003937 sector_t recovery_cp = conf->mddev->recovery_cp;
3938
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003939 /* Check whether resync is now happening or should start.
Alexander Lyakasa7854482012-10-11 13:50:12 +11003940 * If yes, then the array is dirty (after unclean shutdown or
3941 * initial creation), so parity in some stripes might be inconsistent.
3942 * In this case, we need to always do reconstruct-write, to ensure
3943 * that in case of drive failure or read-error correction, we
3944 * generate correct data from the parity.
3945 */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003946 if (conf->rmw_level == PARITY_DISABLE_RMW ||
NeilBrown26ac1072015-02-18 11:35:14 +11003947 (recovery_cp < MaxSector && sh->sector >= recovery_cp &&
3948 s->failed == 0)) {
Alexander Lyakasa7854482012-10-11 13:50:12 +11003949 /* Calculate the real rcw later - for now make it
NeilBrownc8ac1802011-07-27 11:00:36 +10003950 * look like rcw is cheaper
3951 */
3952 rcw = 1; rmw = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003953 pr_debug("force RCW rmw_level=%u, recovery_cp=%llu sh->sector=%llu\n",
3954 conf->rmw_level, (unsigned long long)recovery_cp,
Alexander Lyakasa7854482012-10-11 13:50:12 +11003955 (unsigned long long)sh->sector);
NeilBrownc8ac1802011-07-27 11:00:36 +10003956 } else for (i = disks; i--; ) {
Dan Williamsa4456852007-07-09 11:56:43 -07003957 /* would I have to read this buffer for read_modify_write */
3958 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08003959 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu07e83362017-01-23 17:12:58 -08003960 i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08003961 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07003962 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08003963 !(uptodate_for_rmw(dev) ||
Dan Williamsf38e1212007-01-02 13:52:30 -07003964 test_bit(R5_Wantcompute, &dev->flags))) {
Dan Williamsa4456852007-07-09 11:56:43 -07003965 if (test_bit(R5_Insync, &dev->flags))
3966 rmw++;
3967 else
3968 rmw += 2*disks; /* cannot read it */
3969 }
3970 /* Would I have to read this buffer for reconstruct_write */
Markus Stockhausen584acdd2014-12-15 12:57:05 +11003971 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
3972 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07003973 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07003974 !(test_bit(R5_UPTODATE, &dev->flags) ||
Song Liu1e6d6902016-11-17 15:24:39 -08003975 test_bit(R5_Wantcompute, &dev->flags))) {
NeilBrown67f45542014-05-28 13:39:22 +10003976 if (test_bit(R5_Insync, &dev->flags))
3977 rcw++;
Dan Williamsa4456852007-07-09 11:56:43 -07003978 else
3979 rcw += 2*disks;
3980 }
3981 }
Song Liu1e6d6902016-11-17 15:24:39 -08003982
Song Liu39b99582017-01-24 14:08:23 -08003983 pr_debug("for sector %llu state 0x%lx, rmw=%d rcw=%d\n",
3984 (unsigned long long)sh->sector, sh->state, rmw, rcw);
Dan Williamsa4456852007-07-09 11:56:43 -07003985 set_bit(STRIPE_HANDLE, &sh->state);
Song Liu41257582016-05-23 17:25:06 -07003986 if ((rmw < rcw || (rmw == rcw && conf->rmw_level == PARITY_PREFER_RMW)) && rmw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07003987 /* prefer read-modify-write, but need to get some data */
Jonathan Brassowe3620a32013-03-07 16:22:01 -06003988 if (conf->mddev->queue)
3989 blk_add_trace_msg(conf->mddev->queue,
3990 "raid5 rmw %llu %d",
3991 (unsigned long long)sh->sector, rmw);
Dan Williamsa4456852007-07-09 11:56:43 -07003992 for (i = disks; i--; ) {
3993 struct r5dev *dev = &sh->dev[i];
Song Liu1e6d6902016-11-17 15:24:39 -08003994 if (test_bit(R5_InJournal, &dev->flags) &&
3995 dev->page == dev->orig_page &&
3996 !test_bit(R5_LOCKED, &sh->dev[sh->pd_idx].flags)) {
3997 /* alloc page for prexor */
Song Liud7bd3982016-11-23 22:50:39 -08003998 struct page *p = alloc_page(GFP_NOIO);
Song Liu1e6d6902016-11-17 15:24:39 -08003999
Song Liud7bd3982016-11-23 22:50:39 -08004000 if (p) {
4001 dev->orig_page = p;
4002 continue;
4003 }
4004
4005 /*
4006 * alloc_page() failed, try use
4007 * disk_info->extra_page
4008 */
4009 if (!test_and_set_bit(R5C_EXTRA_PAGE_IN_USE,
4010 &conf->cache_state)) {
4011 r5c_use_extra_page(sh);
4012 break;
4013 }
4014
4015 /* extra_page in use, add to delayed_list */
4016 set_bit(STRIPE_DELAYED, &sh->state);
4017 s->waiting_extra_page = 1;
4018 return -EAGAIN;
Song Liu1e6d6902016-11-17 15:24:39 -08004019 }
Song Liud7bd3982016-11-23 22:50:39 -08004020 }
Song Liu1e6d6902016-11-17 15:24:39 -08004021
Song Liud7bd3982016-11-23 22:50:39 -08004022 for (i = disks; i--; ) {
4023 struct r5dev *dev = &sh->dev[i];
Song Liu39b99582017-01-24 14:08:23 -08004024 if (((dev->towrite && !delay_towrite(conf, dev, s)) ||
Song Liu1e6d6902016-11-17 15:24:39 -08004025 i == sh->pd_idx || i == sh->qd_idx ||
4026 test_bit(R5_InJournal, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07004027 !test_bit(R5_LOCKED, &dev->flags) &&
Song Liu86aa1392017-01-12 17:22:41 -08004028 !(uptodate_for_rmw(dev) ||
Song Liu1e6d6902016-11-17 15:24:39 -08004029 test_bit(R5_Wantcompute, &dev->flags)) &&
Dan Williamsa4456852007-07-09 11:56:43 -07004030 test_bit(R5_Insync, &dev->flags)) {
NeilBrown67f45542014-05-28 13:39:22 +10004031 if (test_bit(STRIPE_PREREAD_ACTIVE,
4032 &sh->state)) {
4033 pr_debug("Read_old block %d for r-m-w\n",
4034 i);
Dan Williamsa4456852007-07-09 11:56:43 -07004035 set_bit(R5_LOCKED, &dev->flags);
4036 set_bit(R5_Wantread, &dev->flags);
4037 s->locked++;
Guoqing Jiange3914d52020-07-28 12:01:40 +02004038 } else
Dan Williamsa4456852007-07-09 11:56:43 -07004039 set_bit(STRIPE_DELAYED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004040 }
4041 }
NeilBrowna9add5d2012-10-31 11:59:09 +11004042 }
Song Liu41257582016-05-23 17:25:06 -07004043 if ((rcw < rmw || (rcw == rmw && conf->rmw_level != PARITY_PREFER_RMW)) && rcw > 0) {
Dan Williamsa4456852007-07-09 11:56:43 -07004044 /* want reconstruct write, but need to get some data */
NeilBrowna9add5d2012-10-31 11:59:09 +11004045 int qread =0;
NeilBrownc8ac1802011-07-27 11:00:36 +10004046 rcw = 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004047 for (i = disks; i--; ) {
4048 struct r5dev *dev = &sh->dev[i];
4049 if (!test_bit(R5_OVERWRITE, &dev->flags) &&
NeilBrownc8ac1802011-07-27 11:00:36 +10004050 i != sh->pd_idx && i != sh->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004051 !test_bit(R5_LOCKED, &dev->flags) &&
Dan Williamsf38e1212007-01-02 13:52:30 -07004052 !(test_bit(R5_UPTODATE, &dev->flags) ||
NeilBrownc8ac1802011-07-27 11:00:36 +10004053 test_bit(R5_Wantcompute, &dev->flags))) {
4054 rcw++;
NeilBrown67f45542014-05-28 13:39:22 +10004055 if (test_bit(R5_Insync, &dev->flags) &&
4056 test_bit(STRIPE_PREREAD_ACTIVE,
4057 &sh->state)) {
Dan Williams45b42332007-07-09 11:56:43 -07004058 pr_debug("Read_old block "
Dan Williamsa4456852007-07-09 11:56:43 -07004059 "%d for Reconstruct\n", i);
4060 set_bit(R5_LOCKED, &dev->flags);
4061 set_bit(R5_Wantread, &dev->flags);
4062 s->locked++;
NeilBrowna9add5d2012-10-31 11:59:09 +11004063 qread++;
Guoqing Jiange3914d52020-07-28 12:01:40 +02004064 } else
Dan Williamsa4456852007-07-09 11:56:43 -07004065 set_bit(STRIPE_DELAYED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004066 }
4067 }
Jonathan Brassowe3620a32013-03-07 16:22:01 -06004068 if (rcw && conf->mddev->queue)
NeilBrowna9add5d2012-10-31 11:59:09 +11004069 blk_add_trace_msg(conf->mddev->queue, "raid5 rcw %llu %d %d %d",
4070 (unsigned long long)sh->sector,
4071 rcw, qread, test_bit(STRIPE_DELAYED, &sh->state));
NeilBrownc8ac1802011-07-27 11:00:36 +10004072 }
NeilBrownb1b02fe2015-02-02 10:44:29 +11004073
4074 if (rcw > disks && rmw > disks &&
4075 !test_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4076 set_bit(STRIPE_DELAYED, &sh->state);
4077
Dan Williamsa4456852007-07-09 11:56:43 -07004078 /* now if nothing is locked, and if we have enough data,
4079 * we can start a write request
4080 */
Dan Williamsf38e1212007-01-02 13:52:30 -07004081 /* since handle_stripe can be called at any time we need to handle the
4082 * case where a compute block operation has been submitted and then a
Dan Williamsac6b53b2009-07-14 13:40:19 -07004083 * subsequent call wants to start a write request. raid_run_ops only
4084 * handles the case where compute block and reconstruct are requested
Dan Williamsf38e1212007-01-02 13:52:30 -07004085 * simultaneously. If this is not the case then new writes need to be
4086 * held off until the compute completes.
4087 */
Dan Williams976ea8d2008-06-28 08:32:03 +10004088 if ((s->req_compute || !test_bit(STRIPE_COMPUTE_RUN, &sh->state)) &&
4089 (s->locked == 0 && (rcw == 0 || rmw == 0) &&
Song Liu1e6d6902016-11-17 15:24:39 -08004090 !test_bit(STRIPE_BIT_DELAY, &sh->state)))
Yuri Tikhonovc0f7bdd2009-08-29 19:13:12 -07004091 schedule_reconstruction(sh, s, rcw == 0, 0);
Song Liud7bd3982016-11-23 22:50:39 -08004092 return 0;
Dan Williamsa4456852007-07-09 11:56:43 -07004093}
4094
NeilBrownd1688a62011-10-11 16:49:52 +11004095static void handle_parity_checks5(struct r5conf *conf, struct stripe_head *sh,
Dan Williamsa4456852007-07-09 11:56:43 -07004096 struct stripe_head_state *s, int disks)
4097{
Dan Williamsecc65c92008-06-28 08:31:57 +10004098 struct r5dev *dev = NULL;
Dan Williamse89f8962007-01-02 13:52:31 -07004099
shli@kernel.org59fc6302014-12-15 12:57:03 +11004100 BUG_ON(sh->batch_head);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004101 set_bit(STRIPE_HANDLE, &sh->state);
4102
Dan Williamsecc65c92008-06-28 08:31:57 +10004103 switch (sh->check_state) {
4104 case check_state_idle:
4105 /* start a new check operation if there are no failures */
Dan Williamsbd2ab672008-04-10 21:29:27 -07004106 if (s->failed == 0) {
Dan Williamsbd2ab672008-04-10 21:29:27 -07004107 BUG_ON(s->uptodate != disks);
Dan Williamsecc65c92008-06-28 08:31:57 +10004108 sh->check_state = check_state_run;
4109 set_bit(STRIPE_OP_CHECK, &s->ops_request);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004110 clear_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags);
Dan Williamsbd2ab672008-04-10 21:29:27 -07004111 s->uptodate--;
Dan Williamsecc65c92008-06-28 08:31:57 +10004112 break;
Dan Williamsbd2ab672008-04-10 21:29:27 -07004113 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004114 dev = &sh->dev[s->failed_num[0]];
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004115 fallthrough;
Dan Williamsecc65c92008-06-28 08:31:57 +10004116 case check_state_compute_result:
4117 sh->check_state = check_state_idle;
4118 if (!dev)
4119 dev = &sh->dev[sh->pd_idx];
4120
4121 /* check that a write has not made the stripe insync */
4122 if (test_bit(STRIPE_INSYNC, &sh->state))
4123 break;
4124
4125 /* either failed parity check, or recovery is happening */
Dan Williamsa4456852007-07-09 11:56:43 -07004126 BUG_ON(!test_bit(R5_UPTODATE, &dev->flags));
4127 BUG_ON(s->uptodate != disks);
4128
4129 set_bit(R5_LOCKED, &dev->flags);
Dan Williamsecc65c92008-06-28 08:31:57 +10004130 s->locked++;
Dan Williamsa4456852007-07-09 11:56:43 -07004131 set_bit(R5_Wantwrite, &dev->flags);
Dan Williams830ea012007-01-02 13:52:31 -07004132
Dan Williamsa4456852007-07-09 11:56:43 -07004133 clear_bit(STRIPE_DEGRADED, &sh->state);
Dan Williamsa4456852007-07-09 11:56:43 -07004134 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004135 break;
4136 case check_state_run:
4137 break; /* we will be called again upon completion */
4138 case check_state_check_result:
4139 sh->check_state = check_state_idle;
4140
4141 /* if a failure occurred during the check operation, leave
4142 * STRIPE_INSYNC not set and let the stripe be handled again
4143 */
4144 if (s->failed)
4145 break;
4146
4147 /* handle a successful check operation, if parity is correct
4148 * we are done. Otherwise update the mismatch count and repair
4149 * parity if !MD_RECOVERY_CHECK
4150 */
Dan Williamsad283ea2009-08-29 19:09:26 -07004151 if ((sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) == 0)
Dan Williamsecc65c92008-06-28 08:31:57 +10004152 /* parity is correct (on disc,
4153 * not in buffer any more)
4154 */
4155 set_bit(STRIPE_INSYNC, &sh->state);
4156 else {
Yufen Yuc911c462020-07-18 05:29:07 -04004157 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
Nixe1539032017-05-16 10:13:31 +01004158 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
Dan Williamsecc65c92008-06-28 08:31:57 +10004159 /* don't try to repair!! */
4160 set_bit(STRIPE_INSYNC, &sh->state);
Nixe1539032017-05-16 10:13:31 +01004161 pr_warn_ratelimited("%s: mismatch sector in range "
4162 "%llu-%llu\n", mdname(conf->mddev),
4163 (unsigned long long) sh->sector,
4164 (unsigned long long) sh->sector +
Yufen Yuc911c462020-07-18 05:29:07 -04004165 RAID5_STRIPE_SECTORS(conf));
Nixe1539032017-05-16 10:13:31 +01004166 } else {
Dan Williamsecc65c92008-06-28 08:31:57 +10004167 sh->check_state = check_state_compute_run;
Dan Williams976ea8d2008-06-28 08:32:03 +10004168 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
Dan Williamsecc65c92008-06-28 08:31:57 +10004169 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4170 set_bit(R5_Wantcompute,
4171 &sh->dev[sh->pd_idx].flags);
4172 sh->ops.target = sh->pd_idx;
Dan Williamsac6b53b2009-07-14 13:40:19 -07004173 sh->ops.target2 = -1;
Dan Williamsecc65c92008-06-28 08:31:57 +10004174 s->uptodate++;
4175 }
4176 }
4177 break;
4178 case check_state_compute_run:
4179 break;
4180 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004181 pr_err("%s: unknown check_state: %d sector: %llu\n",
Dan Williamsecc65c92008-06-28 08:31:57 +10004182 __func__, sh->check_state,
4183 (unsigned long long) sh->sector);
4184 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004185 }
4186}
4187
NeilBrownd1688a62011-10-11 16:49:52 +11004188static void handle_parity_checks6(struct r5conf *conf, struct stripe_head *sh,
Dan Williams36d1c642009-07-14 11:48:22 -07004189 struct stripe_head_state *s,
NeilBrownf2b3b442011-07-26 11:35:19 +10004190 int disks)
Dan Williamsa4456852007-07-09 11:56:43 -07004191{
Dan Williamsa4456852007-07-09 11:56:43 -07004192 int pd_idx = sh->pd_idx;
NeilBrown34e04e82009-03-31 15:10:16 +11004193 int qd_idx = sh->qd_idx;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004194 struct r5dev *dev;
Dan Williamsa4456852007-07-09 11:56:43 -07004195
shli@kernel.org59fc6302014-12-15 12:57:03 +11004196 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004197 set_bit(STRIPE_HANDLE, &sh->state);
4198
4199 BUG_ON(s->failed > 2);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004200
Dan Williamsa4456852007-07-09 11:56:43 -07004201 /* Want to check and possibly repair P and Q.
4202 * However there could be one 'failed' device, in which
4203 * case we can only check one of them, possibly using the
4204 * other to generate missing data
4205 */
4206
Dan Williamsd82dfee2009-07-14 13:40:57 -07004207 switch (sh->check_state) {
4208 case check_state_idle:
4209 /* start a new check operation if there are < 2 failures */
NeilBrownf2b3b442011-07-26 11:35:19 +10004210 if (s->failed == s->q_failed) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004211 /* The only possible failed device holds Q, so it
Dan Williamsa4456852007-07-09 11:56:43 -07004212 * makes sense to check P (If anything else were failed,
4213 * we would have used P to recreate it).
4214 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004215 sh->check_state = check_state_run;
Dan Williamsa4456852007-07-09 11:56:43 -07004216 }
NeilBrownf2b3b442011-07-26 11:35:19 +10004217 if (!s->q_failed && s->failed < 2) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004218 /* Q is not failed, and we didn't use it to generate
Dan Williamsa4456852007-07-09 11:56:43 -07004219 * anything, so it makes sense to check it
4220 */
Dan Williamsd82dfee2009-07-14 13:40:57 -07004221 if (sh->check_state == check_state_run)
4222 sh->check_state = check_state_run_pq;
4223 else
4224 sh->check_state = check_state_run_q;
Dan Williamsa4456852007-07-09 11:56:43 -07004225 }
Dan Williams36d1c642009-07-14 11:48:22 -07004226
Dan Williamsd82dfee2009-07-14 13:40:57 -07004227 /* discard potentially stale zero_sum_result */
4228 sh->ops.zero_sum_result = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07004229
Dan Williamsd82dfee2009-07-14 13:40:57 -07004230 if (sh->check_state == check_state_run) {
4231 /* async_xor_zero_sum destroys the contents of P */
4232 clear_bit(R5_UPTODATE, &sh->dev[pd_idx].flags);
4233 s->uptodate--;
Dan Williamsa4456852007-07-09 11:56:43 -07004234 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004235 if (sh->check_state >= check_state_run &&
4236 sh->check_state <= check_state_run_pq) {
4237 /* async_syndrome_zero_sum preserves P and Q, so
4238 * no need to mark them !uptodate here
4239 */
4240 set_bit(STRIPE_OP_CHECK, &s->ops_request);
4241 break;
4242 }
Dan Williams36d1c642009-07-14 11:48:22 -07004243
Dan Williamsd82dfee2009-07-14 13:40:57 -07004244 /* we have 2-disk failure */
4245 BUG_ON(s->failed != 2);
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -05004246 fallthrough;
Dan Williamsd82dfee2009-07-14 13:40:57 -07004247 case check_state_compute_result:
4248 sh->check_state = check_state_idle;
Dan Williams36d1c642009-07-14 11:48:22 -07004249
Dan Williamsd82dfee2009-07-14 13:40:57 -07004250 /* check that a write has not made the stripe insync */
4251 if (test_bit(STRIPE_INSYNC, &sh->state))
4252 break;
Dan Williamsa4456852007-07-09 11:56:43 -07004253
4254 /* now write out any block on a failed drive,
Dan Williamsd82dfee2009-07-14 13:40:57 -07004255 * or P or Q if they were recomputed
Dan Williamsa4456852007-07-09 11:56:43 -07004256 */
Nigel Croxonb2176a12019-04-16 09:50:09 -07004257 dev = NULL;
Dan Williamsa4456852007-07-09 11:56:43 -07004258 if (s->failed == 2) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004259 dev = &sh->dev[s->failed_num[1]];
Dan Williamsa4456852007-07-09 11:56:43 -07004260 s->locked++;
4261 set_bit(R5_LOCKED, &dev->flags);
4262 set_bit(R5_Wantwrite, &dev->flags);
4263 }
4264 if (s->failed >= 1) {
NeilBrownf2b3b442011-07-26 11:35:19 +10004265 dev = &sh->dev[s->failed_num[0]];
Dan Williamsa4456852007-07-09 11:56:43 -07004266 s->locked++;
4267 set_bit(R5_LOCKED, &dev->flags);
4268 set_bit(R5_Wantwrite, &dev->flags);
4269 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004270 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004271 dev = &sh->dev[pd_idx];
4272 s->locked++;
4273 set_bit(R5_LOCKED, &dev->flags);
4274 set_bit(R5_Wantwrite, &dev->flags);
4275 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004276 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
Dan Williamsa4456852007-07-09 11:56:43 -07004277 dev = &sh->dev[qd_idx];
4278 s->locked++;
4279 set_bit(R5_LOCKED, &dev->flags);
4280 set_bit(R5_Wantwrite, &dev->flags);
4281 }
Nigel Croxonb2176a12019-04-16 09:50:09 -07004282 if (WARN_ONCE(dev && !test_bit(R5_UPTODATE, &dev->flags),
4283 "%s: disk%td not up to date\n",
4284 mdname(conf->mddev),
4285 dev - (struct r5dev *) &sh->dev)) {
4286 clear_bit(R5_LOCKED, &dev->flags);
4287 clear_bit(R5_Wantwrite, &dev->flags);
4288 s->locked--;
4289 }
Dan Williamsa4456852007-07-09 11:56:43 -07004290 clear_bit(STRIPE_DEGRADED, &sh->state);
4291
4292 set_bit(STRIPE_INSYNC, &sh->state);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004293 break;
4294 case check_state_run:
4295 case check_state_run_q:
4296 case check_state_run_pq:
4297 break; /* we will be called again upon completion */
4298 case check_state_check_result:
4299 sh->check_state = check_state_idle;
4300
4301 /* handle a successful check operation, if parity is correct
4302 * we are done. Otherwise update the mismatch count and repair
4303 * parity if !MD_RECOVERY_CHECK
4304 */
4305 if (sh->ops.zero_sum_result == 0) {
Song Liua25d8c32019-04-16 09:34:21 -07004306 /* both parities are correct */
4307 if (!s->failed)
4308 set_bit(STRIPE_INSYNC, &sh->state);
4309 else {
4310 /* in contrast to the raid5 case we can validate
4311 * parity, but still have a failure to write
4312 * back
4313 */
4314 sh->check_state = check_state_compute_result;
4315 /* Returning at this point means that we may go
4316 * off and bring p and/or q uptodate again so
4317 * we make sure to check zero_sum_result again
4318 * to verify if p or q need writeback
4319 */
4320 }
Dan Williamsd82dfee2009-07-14 13:40:57 -07004321 } else {
Yufen Yuc911c462020-07-18 05:29:07 -04004322 atomic64_add(RAID5_STRIPE_SECTORS(conf), &conf->mddev->resync_mismatches);
Nixe1539032017-05-16 10:13:31 +01004323 if (test_bit(MD_RECOVERY_CHECK, &conf->mddev->recovery)) {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004324 /* don't try to repair!! */
4325 set_bit(STRIPE_INSYNC, &sh->state);
Nixe1539032017-05-16 10:13:31 +01004326 pr_warn_ratelimited("%s: mismatch sector in range "
4327 "%llu-%llu\n", mdname(conf->mddev),
4328 (unsigned long long) sh->sector,
4329 (unsigned long long) sh->sector +
Yufen Yuc911c462020-07-18 05:29:07 -04004330 RAID5_STRIPE_SECTORS(conf));
Nixe1539032017-05-16 10:13:31 +01004331 } else {
Dan Williamsd82dfee2009-07-14 13:40:57 -07004332 int *target = &sh->ops.target;
4333
4334 sh->ops.target = -1;
4335 sh->ops.target2 = -1;
4336 sh->check_state = check_state_compute_run;
4337 set_bit(STRIPE_COMPUTE_RUN, &sh->state);
4338 set_bit(STRIPE_OP_COMPUTE_BLK, &s->ops_request);
4339 if (sh->ops.zero_sum_result & SUM_CHECK_P_RESULT) {
4340 set_bit(R5_Wantcompute,
4341 &sh->dev[pd_idx].flags);
4342 *target = pd_idx;
4343 target = &sh->ops.target2;
4344 s->uptodate++;
4345 }
4346 if (sh->ops.zero_sum_result & SUM_CHECK_Q_RESULT) {
4347 set_bit(R5_Wantcompute,
4348 &sh->dev[qd_idx].flags);
4349 *target = qd_idx;
4350 s->uptodate++;
4351 }
4352 }
4353 }
4354 break;
4355 case check_state_compute_run:
4356 break;
4357 default:
NeilBrowncc6167b2016-11-02 14:16:50 +11004358 pr_warn("%s: unknown check_state: %d sector: %llu\n",
4359 __func__, sh->check_state,
4360 (unsigned long long) sh->sector);
Dan Williamsd82dfee2009-07-14 13:40:57 -07004361 BUG();
Dan Williamsa4456852007-07-09 11:56:43 -07004362 }
4363}
4364
NeilBrownd1688a62011-10-11 16:49:52 +11004365static void handle_stripe_expansion(struct r5conf *conf, struct stripe_head *sh)
Dan Williamsa4456852007-07-09 11:56:43 -07004366{
4367 int i;
4368
4369 /* We have read all the blocks in this stripe and now we need to
4370 * copy some of them into a target stripe for expand.
4371 */
Dan Williamsf0a50d32007-01-02 13:52:31 -07004372 struct dma_async_tx_descriptor *tx = NULL;
shli@kernel.org59fc6302014-12-15 12:57:03 +11004373 BUG_ON(sh->batch_head);
Dan Williamsa4456852007-07-09 11:56:43 -07004374 clear_bit(STRIPE_EXPAND_SOURCE, &sh->state);
4375 for (i = 0; i < sh->disks; i++)
NeilBrown34e04e82009-03-31 15:10:16 +11004376 if (i != sh->pd_idx && i != sh->qd_idx) {
NeilBrown911d4ee2009-03-31 14:39:38 +11004377 int dd_idx, j;
Dan Williamsa4456852007-07-09 11:56:43 -07004378 struct stripe_head *sh2;
Dan Williamsa08abd82009-06-03 11:43:59 -07004379 struct async_submit_ctl submit;
Dan Williamsa4456852007-07-09 11:56:43 -07004380
Shaohua Li6d036f72015-08-13 14:31:57 -07004381 sector_t bn = raid5_compute_blocknr(sh, i, 1);
NeilBrown911d4ee2009-03-31 14:39:38 +11004382 sector_t s = raid5_compute_sector(conf, bn, 0,
4383 &dd_idx, NULL);
Shaohua Li6d036f72015-08-13 14:31:57 -07004384 sh2 = raid5_get_active_stripe(conf, s, 0, 1, 1);
Dan Williamsa4456852007-07-09 11:56:43 -07004385 if (sh2 == NULL)
4386 /* so far only the early blocks of this stripe
4387 * have been requested. When later blocks
4388 * get requested, we will try again
4389 */
4390 continue;
4391 if (!test_bit(STRIPE_EXPANDING, &sh2->state) ||
4392 test_bit(R5_Expanded, &sh2->dev[dd_idx].flags)) {
4393 /* must have already done this block */
Shaohua Li6d036f72015-08-13 14:31:57 -07004394 raid5_release_stripe(sh2);
Dan Williamsa4456852007-07-09 11:56:43 -07004395 continue;
4396 }
Dan Williamsf0a50d32007-01-02 13:52:31 -07004397
4398 /* place all the copies on one channel */
Dan Williamsa08abd82009-06-03 11:43:59 -07004399 init_async_submit(&submit, 0, tx, NULL, NULL, NULL);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004400 tx = async_memcpy(sh2->dev[dd_idx].page,
Yufen Yu7aba13b2020-08-20 09:22:06 -04004401 sh->dev[i].page, sh2->dev[dd_idx].offset,
4402 sh->dev[i].offset, RAID5_STRIPE_SIZE(conf),
Dan Williamsa08abd82009-06-03 11:43:59 -07004403 &submit);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004404
Dan Williamsa4456852007-07-09 11:56:43 -07004405 set_bit(R5_Expanded, &sh2->dev[dd_idx].flags);
4406 set_bit(R5_UPTODATE, &sh2->dev[dd_idx].flags);
4407 for (j = 0; j < conf->raid_disks; j++)
4408 if (j != sh2->pd_idx &&
NeilBrown86c374b2011-07-27 11:00:36 +10004409 j != sh2->qd_idx &&
Dan Williamsa4456852007-07-09 11:56:43 -07004410 !test_bit(R5_Expanded, &sh2->dev[j].flags))
4411 break;
4412 if (j == conf->raid_disks) {
4413 set_bit(STRIPE_EXPAND_READY, &sh2->state);
4414 set_bit(STRIPE_HANDLE, &sh2->state);
4415 }
Shaohua Li6d036f72015-08-13 14:31:57 -07004416 raid5_release_stripe(sh2);
Dan Williamsf0a50d32007-01-02 13:52:31 -07004417
Dan Williamsa4456852007-07-09 11:56:43 -07004418 }
NeilBrowna2e08552007-09-11 15:23:36 -07004419 /* done submitting copies, wait for them to complete */
NeilBrown749586b2012-11-20 14:11:15 +11004420 async_tx_quiesce(&tx);
Dan Williamsa4456852007-07-09 11:56:43 -07004421}
Linus Torvalds1da177e2005-04-16 15:20:36 -07004422
4423/*
4424 * handle_stripe - do things to a stripe.
4425 *
NeilBrown9a3e1102011-12-23 10:17:53 +11004426 * We lock the stripe by setting STRIPE_ACTIVE and then examine the
4427 * state of various bits to see what needs to be done.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 * Possible results:
NeilBrown9a3e1102011-12-23 10:17:53 +11004429 * return some read requests which now have data
4430 * return some write requests which are safely on storage
Linus Torvalds1da177e2005-04-16 15:20:36 -07004431 * schedule a read on some buffers
4432 * schedule a write of some buffers
4433 * return confirmation of parity correctness
4434 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 */
Dan Williamsa4456852007-07-09 11:56:43 -07004436
NeilBrownacfe7262011-07-27 11:00:36 +10004437static void analyse_stripe(struct stripe_head *sh, struct stripe_head_state *s)
NeilBrown16a53ec2006-06-26 00:27:38 -07004438{
NeilBrownd1688a62011-10-11 16:49:52 +11004439 struct r5conf *conf = sh->raid_conf;
NeilBrownf4168852007-02-28 20:11:53 -08004440 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004441 struct r5dev *dev;
4442 int i;
NeilBrown9a3e1102011-12-23 10:17:53 +11004443 int do_recovery = 0;
NeilBrown16a53ec2006-06-26 00:27:38 -07004444
NeilBrownacfe7262011-07-27 11:00:36 +10004445 memset(s, 0, sizeof(*s));
NeilBrown16a53ec2006-06-26 00:27:38 -07004446
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004447 s->expanding = test_bit(STRIPE_EXPAND_SOURCE, &sh->state) && !sh->batch_head;
4448 s->expanded = test_bit(STRIPE_EXPAND_READY, &sh->state) && !sh->batch_head;
NeilBrownacfe7262011-07-27 11:00:36 +10004449 s->failed_num[0] = -1;
4450 s->failed_num[1] = -1;
Shaohua Li6e74a9c2015-10-08 21:54:08 -07004451 s->log_failed = r5l_log_disk_error(conf);
NeilBrownacfe7262011-07-27 11:00:36 +10004452
4453 /* Now to look around and see what can be done */
NeilBrown16a53ec2006-06-26 00:27:38 -07004454 rcu_read_lock();
4455 for (i=disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11004456 struct md_rdev *rdev;
NeilBrown31c176e2011-07-28 11:39:22 +10004457 sector_t first_bad;
4458 int bad_sectors;
4459 int is_bad = 0;
NeilBrownacfe7262011-07-27 11:00:36 +10004460
NeilBrown16a53ec2006-06-26 00:27:38 -07004461 dev = &sh->dev[i];
NeilBrown16a53ec2006-06-26 00:27:38 -07004462
Dan Williams45b42332007-07-09 11:56:43 -07004463 pr_debug("check %d: state 0x%lx read %p write %p written %p\n",
NeilBrown9a3e1102011-12-23 10:17:53 +11004464 i, dev->flags,
4465 dev->toread, dev->towrite, dev->written);
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07004466 /* maybe we can reply to a read
4467 *
4468 * new wantfill requests are only permitted while
4469 * ops_complete_biofill is guaranteed to be inactive
4470 */
4471 if (test_bit(R5_UPTODATE, &dev->flags) && dev->toread &&
4472 !test_bit(STRIPE_BIOFILL_RUN, &sh->state))
4473 set_bit(R5_Wantfill, &dev->flags);
NeilBrown16a53ec2006-06-26 00:27:38 -07004474
4475 /* now count some things */
NeilBrowncc940152011-07-26 11:35:35 +10004476 if (test_bit(R5_LOCKED, &dev->flags))
4477 s->locked++;
4478 if (test_bit(R5_UPTODATE, &dev->flags))
4479 s->uptodate++;
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004480 if (test_bit(R5_Wantcompute, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004481 s->compute++;
4482 BUG_ON(s->compute > 2);
Dan Williams2d6e4ec2009-09-16 12:11:54 -07004483 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004484
NeilBrownacfe7262011-07-27 11:00:36 +10004485 if (test_bit(R5_Wantfill, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004486 s->to_fill++;
NeilBrownacfe7262011-07-27 11:00:36 +10004487 else if (dev->toread)
NeilBrowncc940152011-07-26 11:35:35 +10004488 s->to_read++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004489 if (dev->towrite) {
NeilBrowncc940152011-07-26 11:35:35 +10004490 s->to_write++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004491 if (!test_bit(R5_OVERWRITE, &dev->flags))
NeilBrowncc940152011-07-26 11:35:35 +10004492 s->non_overwrite++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004493 }
Dan Williamsa4456852007-07-09 11:56:43 -07004494 if (dev->written)
NeilBrowncc940152011-07-26 11:35:35 +10004495 s->written++;
NeilBrown14a75d32011-12-23 10:17:52 +11004496 /* Prefer to use the replacement for reads, but only
4497 * if it is recovered enough and has no bad blocks.
4498 */
4499 rdev = rcu_dereference(conf->disks[i].replacement);
4500 if (rdev && !test_bit(Faulty, &rdev->flags) &&
Yufen Yuc911c462020-07-18 05:29:07 -04004501 rdev->recovery_offset >= sh->sector + RAID5_STRIPE_SECTORS(conf) &&
4502 !is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown14a75d32011-12-23 10:17:52 +11004503 &first_bad, &bad_sectors))
4504 set_bit(R5_ReadRepl, &dev->flags);
4505 else {
NeilBrowne6030cb2015-07-17 13:26:23 +10004506 if (rdev && !test_bit(Faulty, &rdev->flags))
NeilBrown9a3e1102011-12-23 10:17:53 +11004507 set_bit(R5_NeedReplace, &dev->flags);
NeilBrowne6030cb2015-07-17 13:26:23 +10004508 else
4509 clear_bit(R5_NeedReplace, &dev->flags);
NeilBrown14a75d32011-12-23 10:17:52 +11004510 rdev = rcu_dereference(conf->disks[i].rdev);
4511 clear_bit(R5_ReadRepl, &dev->flags);
4512 }
NeilBrown9283d8c2011-12-08 16:27:57 +11004513 if (rdev && test_bit(Faulty, &rdev->flags))
4514 rdev = NULL;
NeilBrown31c176e2011-07-28 11:39:22 +10004515 if (rdev) {
Yufen Yuc911c462020-07-18 05:29:07 -04004516 is_bad = is_badblock(rdev, sh->sector, RAID5_STRIPE_SECTORS(conf),
NeilBrown31c176e2011-07-28 11:39:22 +10004517 &first_bad, &bad_sectors);
4518 if (s->blocked_rdev == NULL
4519 && (test_bit(Blocked, &rdev->flags)
4520 || is_bad < 0)) {
4521 if (is_bad < 0)
4522 set_bit(BlockedBadBlocks,
4523 &rdev->flags);
4524 s->blocked_rdev = rdev;
4525 atomic_inc(&rdev->nr_pending);
4526 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07004527 }
NeilBrown415e72d2010-06-17 17:25:21 +10004528 clear_bit(R5_Insync, &dev->flags);
4529 if (!rdev)
4530 /* Not in-sync */;
NeilBrown31c176e2011-07-28 11:39:22 +10004531 else if (is_bad) {
4532 /* also not in-sync */
NeilBrown18b98372012-04-01 23:48:38 +10004533 if (!test_bit(WriteErrorSeen, &rdev->flags) &&
4534 test_bit(R5_UPTODATE, &dev->flags)) {
NeilBrown31c176e2011-07-28 11:39:22 +10004535 /* treat as in-sync, but with a read error
4536 * which we can now try to correct
4537 */
4538 set_bit(R5_Insync, &dev->flags);
4539 set_bit(R5_ReadError, &dev->flags);
4540 }
4541 } else if (test_bit(In_sync, &rdev->flags))
NeilBrown415e72d2010-06-17 17:25:21 +10004542 set_bit(R5_Insync, &dev->flags);
Yufen Yuc911c462020-07-18 05:29:07 -04004543 else if (sh->sector + RAID5_STRIPE_SECTORS(conf) <= rdev->recovery_offset)
NeilBrown415e72d2010-06-17 17:25:21 +10004544 /* in sync if before recovery_offset */
NeilBrown30d7a482011-12-23 09:57:00 +11004545 set_bit(R5_Insync, &dev->flags);
4546 else if (test_bit(R5_UPTODATE, &dev->flags) &&
4547 test_bit(R5_Expanded, &dev->flags))
4548 /* If we've reshaped into here, we assume it is Insync.
4549 * We will shortly update recovery_offset to make
4550 * it official.
4551 */
4552 set_bit(R5_Insync, &dev->flags);
4553
NeilBrown1cc03eb2014-01-06 13:19:42 +11004554 if (test_bit(R5_WriteError, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004555 /* This flag does not apply to '.replacement'
4556 * only to .rdev, so make sure to check that*/
4557 struct md_rdev *rdev2 = rcu_dereference(
4558 conf->disks[i].rdev);
4559 if (rdev2 == rdev)
4560 clear_bit(R5_Insync, &dev->flags);
4561 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004562 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004563 atomic_inc(&rdev2->nr_pending);
NeilBrownbc2607f2011-07-28 11:39:22 +10004564 } else
4565 clear_bit(R5_WriteError, &dev->flags);
4566 }
NeilBrown1cc03eb2014-01-06 13:19:42 +11004567 if (test_bit(R5_MadeGood, &dev->flags)) {
NeilBrown14a75d32011-12-23 10:17:52 +11004568 /* This flag does not apply to '.replacement'
4569 * only to .rdev, so make sure to check that*/
4570 struct md_rdev *rdev2 = rcu_dereference(
4571 conf->disks[i].rdev);
4572 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
NeilBrownb84db562011-07-28 11:39:23 +10004573 s->handle_bad_blocks = 1;
NeilBrown14a75d32011-12-23 10:17:52 +11004574 atomic_inc(&rdev2->nr_pending);
NeilBrownb84db562011-07-28 11:39:23 +10004575 } else
4576 clear_bit(R5_MadeGood, &dev->flags);
4577 }
NeilBrown977df362011-12-23 10:17:53 +11004578 if (test_bit(R5_MadeGoodRepl, &dev->flags)) {
4579 struct md_rdev *rdev2 = rcu_dereference(
4580 conf->disks[i].replacement);
4581 if (rdev2 && !test_bit(Faulty, &rdev2->flags)) {
4582 s->handle_bad_blocks = 1;
4583 atomic_inc(&rdev2->nr_pending);
4584 } else
4585 clear_bit(R5_MadeGoodRepl, &dev->flags);
4586 }
NeilBrown415e72d2010-06-17 17:25:21 +10004587 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrown16a53ec2006-06-26 00:27:38 -07004588 /* The ReadError flag will just be confusing now */
4589 clear_bit(R5_ReadError, &dev->flags);
4590 clear_bit(R5_ReWrite, &dev->flags);
4591 }
NeilBrown415e72d2010-06-17 17:25:21 +10004592 if (test_bit(R5_ReadError, &dev->flags))
4593 clear_bit(R5_Insync, &dev->flags);
4594 if (!test_bit(R5_Insync, &dev->flags)) {
NeilBrowncc940152011-07-26 11:35:35 +10004595 if (s->failed < 2)
4596 s->failed_num[s->failed] = i;
4597 s->failed++;
NeilBrown9a3e1102011-12-23 10:17:53 +11004598 if (rdev && !test_bit(Faulty, &rdev->flags))
4599 do_recovery = 1;
BingJing Changd63e2fc2018-08-01 17:08:36 +08004600 else if (!rdev) {
4601 rdev = rcu_dereference(
4602 conf->disks[i].replacement);
4603 if (rdev && !test_bit(Faulty, &rdev->flags))
4604 do_recovery = 1;
4605 }
NeilBrown415e72d2010-06-17 17:25:21 +10004606 }
Song Liu2ded3702016-11-17 15:24:38 -08004607
4608 if (test_bit(R5_InJournal, &dev->flags))
4609 s->injournal++;
Song Liu1e6d6902016-11-17 15:24:39 -08004610 if (test_bit(R5_InJournal, &dev->flags) && dev->written)
4611 s->just_cached++;
NeilBrown16a53ec2006-06-26 00:27:38 -07004612 }
NeilBrown9a3e1102011-12-23 10:17:53 +11004613 if (test_bit(STRIPE_SYNCING, &sh->state)) {
4614 /* If there is a failed device being replaced,
4615 * we must be recovering.
4616 * else if we are after recovery_cp, we must be syncing
majianpengc6d2e082012-04-02 01:16:59 +10004617 * else if MD_RECOVERY_REQUESTED is set, we also are syncing.
NeilBrown9a3e1102011-12-23 10:17:53 +11004618 * else we can only be replacing
4619 * sync and recovery both need to read all devices, and so
4620 * use the same flag.
4621 */
4622 if (do_recovery ||
majianpengc6d2e082012-04-02 01:16:59 +10004623 sh->sector >= conf->mddev->recovery_cp ||
4624 test_bit(MD_RECOVERY_REQUESTED, &(conf->mddev->recovery)))
NeilBrown9a3e1102011-12-23 10:17:53 +11004625 s->syncing = 1;
4626 else
4627 s->replacing = 1;
4628 }
NeilBrown16a53ec2006-06-26 00:27:38 -07004629 rcu_read_unlock();
NeilBrowncc940152011-07-26 11:35:35 +10004630}
NeilBrownf4168852007-02-28 20:11:53 -08004631
Guoqing Jiangcb9902d2020-06-16 11:25:51 +02004632/*
4633 * Return '1' if this is a member of batch, or '0' if it is a lone stripe or
4634 * a head which can now be handled.
4635 */
shli@kernel.org59fc6302014-12-15 12:57:03 +11004636static int clear_batch_ready(struct stripe_head *sh)
4637{
4638 struct stripe_head *tmp;
4639 if (!test_and_clear_bit(STRIPE_BATCH_READY, &sh->state))
NeilBrownb15a9db2015-05-22 15:20:04 +10004640 return (sh->batch_head && sh->batch_head != sh);
shli@kernel.org59fc6302014-12-15 12:57:03 +11004641 spin_lock(&sh->stripe_lock);
4642 if (!sh->batch_head) {
4643 spin_unlock(&sh->stripe_lock);
4644 return 0;
4645 }
4646
4647 /*
4648 * this stripe could be added to a batch list before we check
4649 * BATCH_READY, skips it
4650 */
4651 if (sh->batch_head != sh) {
4652 spin_unlock(&sh->stripe_lock);
4653 return 1;
4654 }
4655 spin_lock(&sh->batch_lock);
4656 list_for_each_entry(tmp, &sh->batch_list, batch_list)
4657 clear_bit(STRIPE_BATCH_READY, &tmp->state);
4658 spin_unlock(&sh->batch_lock);
4659 spin_unlock(&sh->stripe_lock);
4660
4661 /*
4662 * BATCH_READY is cleared, no new stripes can be added.
4663 * batch_list can be accessed without lock
4664 */
4665 return 0;
4666}
4667
NeilBrown3960ce72015-05-21 12:20:36 +10004668static void break_stripe_batch_list(struct stripe_head *head_sh,
4669 unsigned long handle_flags)
shli@kernel.org72ac7332014-12-15 12:57:03 +11004670{
NeilBrown4e3d62f2015-05-21 11:50:16 +10004671 struct stripe_head *sh, *next;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004672 int i;
NeilBrownfb642b92015-05-21 12:00:47 +10004673 int do_wakeup = 0;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004674
NeilBrownbb270512015-05-08 18:19:40 +10004675 list_for_each_entry_safe(sh, next, &head_sh->batch_list, batch_list) {
4676
shli@kernel.org72ac7332014-12-15 12:57:03 +11004677 list_del_init(&sh->batch_list);
4678
Shaohua Lifb3229d2016-03-09 10:08:38 -08004679 WARN_ONCE(sh->state & ((1 << STRIPE_ACTIVE) |
NeilBrown1b956f72015-05-21 12:40:26 +10004680 (1 << STRIPE_SYNCING) |
4681 (1 << STRIPE_REPLACED) |
NeilBrown1b956f72015-05-21 12:40:26 +10004682 (1 << STRIPE_DELAYED) |
4683 (1 << STRIPE_BIT_DELAY) |
4684 (1 << STRIPE_FULL_WRITE) |
4685 (1 << STRIPE_BIOFILL_RUN) |
4686 (1 << STRIPE_COMPUTE_RUN) |
NeilBrown1b956f72015-05-21 12:40:26 +10004687 (1 << STRIPE_DISCARD) |
4688 (1 << STRIPE_BATCH_READY) |
4689 (1 << STRIPE_BATCH_ERR) |
Shaohua Lifb3229d2016-03-09 10:08:38 -08004690 (1 << STRIPE_BITMAP_PENDING)),
4691 "stripe state: %lx\n", sh->state);
4692 WARN_ONCE(head_sh->state & ((1 << STRIPE_DISCARD) |
4693 (1 << STRIPE_REPLACED)),
4694 "head stripe state: %lx\n", head_sh->state);
NeilBrown1b956f72015-05-21 12:40:26 +10004695
4696 set_mask_bits(&sh->state, ~(STRIPE_EXPAND_SYNC_FLAGS |
NeilBrown550da242016-03-09 12:58:25 +11004697 (1 << STRIPE_PREREAD_ACTIVE) |
Dennis Yang184a09e2017-09-06 11:02:35 +08004698 (1 << STRIPE_DEGRADED) |
4699 (1 << STRIPE_ON_UNPLUG_LIST)),
NeilBrown1b956f72015-05-21 12:40:26 +10004700 head_sh->state & (1 << STRIPE_INSYNC));
4701
shli@kernel.org72ac7332014-12-15 12:57:03 +11004702 sh->check_state = head_sh->check_state;
4703 sh->reconstruct_state = head_sh->reconstruct_state;
Amy Chiang448ec632018-05-16 18:59:35 +08004704 spin_lock_irq(&sh->stripe_lock);
4705 sh->batch_head = NULL;
4706 spin_unlock_irq(&sh->stripe_lock);
NeilBrownfb642b92015-05-21 12:00:47 +10004707 for (i = 0; i < sh->disks; i++) {
4708 if (test_and_clear_bit(R5_Overlap, &sh->dev[i].flags))
4709 do_wakeup = 1;
shli@kernel.org72ac7332014-12-15 12:57:03 +11004710 sh->dev[i].flags = head_sh->dev[i].flags &
4711 (~((1 << R5_WriteError) | (1 << R5_Overlap)));
NeilBrownfb642b92015-05-21 12:00:47 +10004712 }
NeilBrown3960ce72015-05-21 12:20:36 +10004713 if (handle_flags == 0 ||
4714 sh->state & handle_flags)
4715 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07004716 raid5_release_stripe(sh);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004717 }
NeilBrownfb642b92015-05-21 12:00:47 +10004718 spin_lock_irq(&head_sh->stripe_lock);
4719 head_sh->batch_head = NULL;
4720 spin_unlock_irq(&head_sh->stripe_lock);
4721 for (i = 0; i < head_sh->disks; i++)
4722 if (test_and_clear_bit(R5_Overlap, &head_sh->dev[i].flags))
4723 do_wakeup = 1;
NeilBrown3960ce72015-05-21 12:20:36 +10004724 if (head_sh->state & handle_flags)
4725 set_bit(STRIPE_HANDLE, &head_sh->state);
NeilBrownfb642b92015-05-21 12:00:47 +10004726
4727 if (do_wakeup)
4728 wake_up(&head_sh->raid_conf->wait_for_overlap);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004729}
4730
NeilBrowncc940152011-07-26 11:35:35 +10004731static void handle_stripe(struct stripe_head *sh)
4732{
4733 struct stripe_head_state s;
NeilBrownd1688a62011-10-11 16:49:52 +11004734 struct r5conf *conf = sh->raid_conf;
NeilBrown3687c062011-07-27 11:00:36 +10004735 int i;
NeilBrown84789552011-07-27 11:00:36 +10004736 int prexor;
4737 int disks = sh->disks;
NeilBrown474af965fe2011-07-27 11:00:36 +10004738 struct r5dev *pdev, *qdev;
NeilBrowncc940152011-07-26 11:35:35 +10004739
4740 clear_bit(STRIPE_HANDLE, &sh->state);
Guoqing Jianga377a472020-06-16 11:25:50 +02004741
4742 /*
4743 * handle_stripe should not continue handle the batched stripe, only
4744 * the head of batch list or lone stripe can continue. Otherwise we
4745 * could see break_stripe_batch_list warns about the STRIPE_ACTIVE
4746 * is set for the batched stripe.
4747 */
4748 if (clear_batch_ready(sh))
4749 return;
4750
Dan Williams257a4b42011-11-08 16:22:06 +11004751 if (test_and_set_bit_lock(STRIPE_ACTIVE, &sh->state)) {
NeilBrowncc940152011-07-26 11:35:35 +10004752 /* already being handled, ensure it gets handled
4753 * again when current action finishes */
4754 set_bit(STRIPE_HANDLE, &sh->state);
4755 return;
4756 }
4757
NeilBrown4e3d62f2015-05-21 11:50:16 +10004758 if (test_and_clear_bit(STRIPE_BATCH_ERR, &sh->state))
NeilBrown3960ce72015-05-21 12:20:36 +10004759 break_stripe_batch_list(sh, 0);
shli@kernel.org72ac7332014-12-15 12:57:03 +11004760
shli@kernel.orgdabc4ec2014-12-15 12:57:04 +11004761 if (test_bit(STRIPE_SYNC_REQUESTED, &sh->state) && !sh->batch_head) {
NeilBrownf8dfcff2013-03-12 12:18:06 +11004762 spin_lock(&sh->stripe_lock);
Song Liu5ddf0442017-05-11 17:03:44 -07004763 /*
4764 * Cannot process 'sync' concurrently with 'discard'.
4765 * Flush data in r5cache before 'sync'.
4766 */
4767 if (!test_bit(STRIPE_R5C_PARTIAL_STRIPE, &sh->state) &&
4768 !test_bit(STRIPE_R5C_FULL_STRIPE, &sh->state) &&
4769 !test_bit(STRIPE_DISCARD, &sh->state) &&
NeilBrownf8dfcff2013-03-12 12:18:06 +11004770 test_and_clear_bit(STRIPE_SYNC_REQUESTED, &sh->state)) {
4771 set_bit(STRIPE_SYNCING, &sh->state);
4772 clear_bit(STRIPE_INSYNC, &sh->state);
NeilBrownf94c0b62013-07-22 12:57:21 +10004773 clear_bit(STRIPE_REPLACED, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004774 }
4775 spin_unlock(&sh->stripe_lock);
NeilBrowncc940152011-07-26 11:35:35 +10004776 }
4777 clear_bit(STRIPE_DELAYED, &sh->state);
4778
4779 pr_debug("handling stripe %llu, state=%#lx cnt=%d, "
4780 "pd_idx=%d, qd_idx=%d\n, check:%d, reconstruct:%d\n",
4781 (unsigned long long)sh->sector, sh->state,
4782 atomic_read(&sh->count), sh->pd_idx, sh->qd_idx,
4783 sh->check_state, sh->reconstruct_state);
NeilBrowncc940152011-07-26 11:35:35 +10004784
NeilBrownacfe7262011-07-27 11:00:36 +10004785 analyse_stripe(sh, &s);
NeilBrownc5a31002011-07-27 11:00:36 +10004786
Shaohua Lib70abcb2015-08-13 14:31:58 -07004787 if (test_bit(STRIPE_LOG_TRAPPED, &sh->state))
4788 goto finish;
4789
NeilBrown16d997b2017-03-15 14:05:12 +11004790 if (s.handle_bad_blocks ||
4791 test_bit(MD_SB_CHANGE_PENDING, &conf->mddev->sb_flags)) {
NeilBrownbc2607f2011-07-28 11:39:22 +10004792 set_bit(STRIPE_HANDLE, &sh->state);
4793 goto finish;
4794 }
4795
NeilBrown474af965fe2011-07-27 11:00:36 +10004796 if (unlikely(s.blocked_rdev)) {
4797 if (s.syncing || s.expanding || s.expanded ||
NeilBrown9a3e1102011-12-23 10:17:53 +11004798 s.replacing || s.to_write || s.written) {
NeilBrown474af965fe2011-07-27 11:00:36 +10004799 set_bit(STRIPE_HANDLE, &sh->state);
4800 goto finish;
4801 }
4802 /* There is nothing for the blocked_rdev to block */
4803 rdev_dec_pending(s.blocked_rdev, conf->mddev);
4804 s.blocked_rdev = NULL;
4805 }
4806
4807 if (s.to_fill && !test_bit(STRIPE_BIOFILL_RUN, &sh->state)) {
4808 set_bit(STRIPE_OP_BIOFILL, &s.ops_request);
4809 set_bit(STRIPE_BIOFILL_RUN, &sh->state);
4810 }
4811
4812 pr_debug("locked=%d uptodate=%d to_read=%d"
4813 " to_write=%d failed=%d failed_num=%d,%d\n",
4814 s.locked, s.uptodate, s.to_read, s.to_write, s.failed,
4815 s.failed_num[0], s.failed_num[1]);
Song Liu70d466f2017-05-11 15:28:28 -07004816 /*
4817 * check if the array has lost more than max_degraded devices and,
NeilBrown474af965fe2011-07-27 11:00:36 +10004818 * if so, some requests might need to be failed.
Song Liu70d466f2017-05-11 15:28:28 -07004819 *
4820 * When journal device failed (log_failed), we will only process
4821 * the stripe if there is data need write to raid disks
NeilBrown474af965fe2011-07-27 11:00:36 +10004822 */
Song Liu70d466f2017-05-11 15:28:28 -07004823 if (s.failed > conf->max_degraded ||
4824 (s.log_failed && s.injournal == 0)) {
NeilBrown9a3f5302011-11-08 16:22:01 +11004825 sh->check_state = 0;
4826 sh->reconstruct_state = 0;
NeilBrown626f2092015-05-22 14:03:10 +10004827 break_stripe_batch_list(sh, 0);
NeilBrown9a3f5302011-11-08 16:22:01 +11004828 if (s.to_read+s.to_write+s.written)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004829 handle_failed_stripe(conf, sh, &s, disks);
NeilBrown9a3e1102011-12-23 10:17:53 +11004830 if (s.syncing + s.replacing)
NeilBrown9a3f5302011-11-08 16:22:01 +11004831 handle_failed_sync(conf, sh, &s);
4832 }
NeilBrown474af965fe2011-07-27 11:00:36 +10004833
NeilBrown84789552011-07-27 11:00:36 +10004834 /* Now we check to see if any write operations have recently
4835 * completed
4836 */
4837 prexor = 0;
4838 if (sh->reconstruct_state == reconstruct_state_prexor_drain_result)
4839 prexor = 1;
4840 if (sh->reconstruct_state == reconstruct_state_drain_result ||
4841 sh->reconstruct_state == reconstruct_state_prexor_drain_result) {
4842 sh->reconstruct_state = reconstruct_state_idle;
4843
4844 /* All the 'written' buffers and the parity block are ready to
4845 * be written back to disk
4846 */
Shaohua Li9e4447682012-10-11 13:49:49 +11004847 BUG_ON(!test_bit(R5_UPTODATE, &sh->dev[sh->pd_idx].flags) &&
4848 !test_bit(R5_Discard, &sh->dev[sh->pd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004849 BUG_ON(sh->qd_idx >= 0 &&
Shaohua Li9e4447682012-10-11 13:49:49 +11004850 !test_bit(R5_UPTODATE, &sh->dev[sh->qd_idx].flags) &&
4851 !test_bit(R5_Discard, &sh->dev[sh->qd_idx].flags));
NeilBrown84789552011-07-27 11:00:36 +10004852 for (i = disks; i--; ) {
4853 struct r5dev *dev = &sh->dev[i];
4854 if (test_bit(R5_LOCKED, &dev->flags) &&
4855 (i == sh->pd_idx || i == sh->qd_idx ||
Song Liu1e6d6902016-11-17 15:24:39 -08004856 dev->written || test_bit(R5_InJournal,
4857 &dev->flags))) {
NeilBrown84789552011-07-27 11:00:36 +10004858 pr_debug("Writing block %d\n", i);
4859 set_bit(R5_Wantwrite, &dev->flags);
4860 if (prexor)
4861 continue;
NeilBrown9c4bdf62014-08-13 09:57:07 +10004862 if (s.failed > 1)
4863 continue;
NeilBrown84789552011-07-27 11:00:36 +10004864 if (!test_bit(R5_Insync, &dev->flags) ||
4865 ((i == sh->pd_idx || i == sh->qd_idx) &&
4866 s.failed == 0))
4867 set_bit(STRIPE_INSYNC, &sh->state);
4868 }
4869 }
4870 if (test_and_clear_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
4871 s.dec_preread_active = 1;
4872 }
4873
NeilBrownef5b7c62012-11-22 09:13:36 +11004874 /*
4875 * might be able to return some write requests if the parity blocks
4876 * are safe, or on a failed drive
4877 */
4878 pdev = &sh->dev[sh->pd_idx];
4879 s.p_failed = (s.failed >= 1 && s.failed_num[0] == sh->pd_idx)
4880 || (s.failed >= 2 && s.failed_num[1] == sh->pd_idx);
4881 qdev = &sh->dev[sh->qd_idx];
4882 s.q_failed = (s.failed >= 1 && s.failed_num[0] == sh->qd_idx)
4883 || (s.failed >= 2 && s.failed_num[1] == sh->qd_idx)
4884 || conf->level < 6;
4885
4886 if (s.written &&
4887 (s.p_failed || ((test_bit(R5_Insync, &pdev->flags)
4888 && !test_bit(R5_LOCKED, &pdev->flags)
4889 && (test_bit(R5_UPTODATE, &pdev->flags) ||
4890 test_bit(R5_Discard, &pdev->flags))))) &&
4891 (s.q_failed || ((test_bit(R5_Insync, &qdev->flags)
4892 && !test_bit(R5_LOCKED, &qdev->flags)
4893 && (test_bit(R5_UPTODATE, &qdev->flags) ||
4894 test_bit(R5_Discard, &qdev->flags))))))
NeilBrownbd83d0a2017-03-15 14:05:12 +11004895 handle_stripe_clean_event(conf, sh, disks);
NeilBrownef5b7c62012-11-22 09:13:36 +11004896
Song Liu1e6d6902016-11-17 15:24:39 -08004897 if (s.just_cached)
NeilBrownbd83d0a2017-03-15 14:05:12 +11004898 r5c_handle_cached_data_endio(conf, sh, disks);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01004899 log_stripe_write_finished(sh);
Song Liu1e6d6902016-11-17 15:24:39 -08004900
NeilBrownef5b7c62012-11-22 09:13:36 +11004901 /* Now we might consider reading some blocks, either to check/generate
4902 * parity, or to satisfy requests
4903 * or to load a block that is being partially written.
4904 */
4905 if (s.to_read || s.non_overwrite
ChangSyun Penga1c6ae32020-07-31 17:50:17 +08004906 || (s.to_write && s.failed)
NeilBrownef5b7c62012-11-22 09:13:36 +11004907 || (s.syncing && (s.uptodate + s.compute < disks))
4908 || s.replacing
4909 || s.expanding)
4910 handle_stripe_fill(sh, &s, disks);
4911
Song Liu2ded3702016-11-17 15:24:38 -08004912 /*
4913 * When the stripe finishes full journal write cycle (write to journal
4914 * and raid disk), this is the clean up procedure so it is ready for
4915 * next operation.
4916 */
4917 r5c_finish_stripe_write_out(conf, sh, &s);
4918
4919 /*
4920 * Now to consider new write requests, cache write back and what else,
4921 * if anything should be read. We do not handle new writes when:
NeilBrown84789552011-07-27 11:00:36 +10004922 * 1/ A 'write' operation (copy+xor) is already in flight.
4923 * 2/ A 'check' operation is in flight, as it may clobber the parity
4924 * block.
Song Liu2ded3702016-11-17 15:24:38 -08004925 * 3/ A r5c cache log write is in flight.
NeilBrown84789552011-07-27 11:00:36 +10004926 */
Song Liu2ded3702016-11-17 15:24:38 -08004927
4928 if (!sh->reconstruct_state && !sh->check_state && !sh->log_io) {
4929 if (!r5c_is_writeback(conf->log)) {
4930 if (s.to_write)
4931 handle_stripe_dirtying(conf, sh, &s, disks);
4932 } else { /* write back cache */
4933 int ret = 0;
4934
4935 /* First, try handle writes in caching phase */
4936 if (s.to_write)
4937 ret = r5c_try_caching_write(conf, sh, &s,
4938 disks);
4939 /*
4940 * If caching phase failed: ret == -EAGAIN
4941 * OR
4942 * stripe under reclaim: !caching && injournal
4943 *
4944 * fall back to handle_stripe_dirtying()
4945 */
4946 if (ret == -EAGAIN ||
4947 /* stripe under reclaim: !caching && injournal */
4948 (!test_bit(STRIPE_R5C_CACHING, &sh->state) &&
Song Liud7bd3982016-11-23 22:50:39 -08004949 s.injournal > 0)) {
4950 ret = handle_stripe_dirtying(conf, sh, &s,
4951 disks);
4952 if (ret == -EAGAIN)
4953 goto finish;
4954 }
Song Liu2ded3702016-11-17 15:24:38 -08004955 }
4956 }
NeilBrown84789552011-07-27 11:00:36 +10004957
4958 /* maybe we need to check and possibly fix the parity for this stripe
4959 * Any reads will already have been scheduled, so we just see if enough
4960 * data is available. The parity check is held off while parity
4961 * dependent operations are in flight.
4962 */
4963 if (sh->check_state ||
4964 (s.syncing && s.locked == 0 &&
4965 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
4966 !test_bit(STRIPE_INSYNC, &sh->state))) {
4967 if (conf->level == 6)
4968 handle_parity_checks6(conf, sh, &s, disks);
4969 else
4970 handle_parity_checks5(conf, sh, &s, disks);
4971 }
NeilBrownc5a31002011-07-27 11:00:36 +10004972
NeilBrownf94c0b62013-07-22 12:57:21 +10004973 if ((s.replacing || s.syncing) && s.locked == 0
4974 && !test_bit(STRIPE_COMPUTE_RUN, &sh->state)
4975 && !test_bit(STRIPE_REPLACED, &sh->state)) {
NeilBrown9a3e1102011-12-23 10:17:53 +11004976 /* Write out to replacement devices where possible */
4977 for (i = 0; i < conf->raid_disks; i++)
NeilBrownf94c0b62013-07-22 12:57:21 +10004978 if (test_bit(R5_NeedReplace, &sh->dev[i].flags)) {
4979 WARN_ON(!test_bit(R5_UPTODATE, &sh->dev[i].flags));
NeilBrown9a3e1102011-12-23 10:17:53 +11004980 set_bit(R5_WantReplace, &sh->dev[i].flags);
4981 set_bit(R5_LOCKED, &sh->dev[i].flags);
4982 s.locked++;
4983 }
NeilBrownf94c0b62013-07-22 12:57:21 +10004984 if (s.replacing)
4985 set_bit(STRIPE_INSYNC, &sh->state);
4986 set_bit(STRIPE_REPLACED, &sh->state);
NeilBrown9a3e1102011-12-23 10:17:53 +11004987 }
4988 if ((s.syncing || s.replacing) && s.locked == 0 &&
NeilBrownf94c0b62013-07-22 12:57:21 +10004989 !test_bit(STRIPE_COMPUTE_RUN, &sh->state) &&
NeilBrown9a3e1102011-12-23 10:17:53 +11004990 test_bit(STRIPE_INSYNC, &sh->state)) {
Yufen Yuc911c462020-07-18 05:29:07 -04004991 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
NeilBrownc5a31002011-07-27 11:00:36 +10004992 clear_bit(STRIPE_SYNCING, &sh->state);
NeilBrownf8dfcff2013-03-12 12:18:06 +11004993 if (test_and_clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags))
4994 wake_up(&conf->wait_for_overlap);
NeilBrownc5a31002011-07-27 11:00:36 +10004995 }
4996
4997 /* If the failed drives are just a ReadError, then we might need
4998 * to progress the repair/check process
4999 */
5000 if (s.failed <= conf->max_degraded && !conf->mddev->ro)
5001 for (i = 0; i < s.failed; i++) {
5002 struct r5dev *dev = &sh->dev[s.failed_num[i]];
5003 if (test_bit(R5_ReadError, &dev->flags)
5004 && !test_bit(R5_LOCKED, &dev->flags)
5005 && test_bit(R5_UPTODATE, &dev->flags)
5006 ) {
5007 if (!test_bit(R5_ReWrite, &dev->flags)) {
5008 set_bit(R5_Wantwrite, &dev->flags);
5009 set_bit(R5_ReWrite, &dev->flags);
Guoqing Jiang3a31cf32020-07-28 12:01:43 +02005010 } else
NeilBrownc5a31002011-07-27 11:00:36 +10005011 /* let's read it back */
5012 set_bit(R5_Wantread, &dev->flags);
Guoqing Jiang3a31cf32020-07-28 12:01:43 +02005013 set_bit(R5_LOCKED, &dev->flags);
5014 s.locked++;
NeilBrownc5a31002011-07-27 11:00:36 +10005015 }
5016 }
5017
NeilBrown3687c062011-07-27 11:00:36 +10005018 /* Finish reconstruct operations initiated by the expansion process */
5019 if (sh->reconstruct_state == reconstruct_state_result) {
5020 struct stripe_head *sh_src
Shaohua Li6d036f72015-08-13 14:31:57 -07005021 = raid5_get_active_stripe(conf, sh->sector, 1, 1, 1);
NeilBrown3687c062011-07-27 11:00:36 +10005022 if (sh_src && test_bit(STRIPE_EXPAND_SOURCE, &sh_src->state)) {
5023 /* sh cannot be written until sh_src has been read.
5024 * so arrange for sh to be delayed a little
5025 */
5026 set_bit(STRIPE_DELAYED, &sh->state);
5027 set_bit(STRIPE_HANDLE, &sh->state);
5028 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE,
5029 &sh_src->state))
5030 atomic_inc(&conf->preread_active_stripes);
Shaohua Li6d036f72015-08-13 14:31:57 -07005031 raid5_release_stripe(sh_src);
NeilBrown3687c062011-07-27 11:00:36 +10005032 goto finish;
5033 }
5034 if (sh_src)
Shaohua Li6d036f72015-08-13 14:31:57 -07005035 raid5_release_stripe(sh_src);
NeilBrown16a53ec2006-06-26 00:27:38 -07005036
NeilBrown3687c062011-07-27 11:00:36 +10005037 sh->reconstruct_state = reconstruct_state_idle;
5038 clear_bit(STRIPE_EXPANDING, &sh->state);
5039 for (i = conf->raid_disks; i--; ) {
5040 set_bit(R5_Wantwrite, &sh->dev[i].flags);
5041 set_bit(R5_LOCKED, &sh->dev[i].flags);
5042 s.locked++;
5043 }
5044 }
5045
5046 if (s.expanded && test_bit(STRIPE_EXPANDING, &sh->state) &&
5047 !sh->reconstruct_state) {
5048 /* Need to write out all blocks after computing parity */
5049 sh->disks = conf->raid_disks;
5050 stripe_set_idx(sh->sector, conf, 0, sh);
5051 schedule_reconstruction(sh, &s, 1, 1);
5052 } else if (s.expanded && !sh->reconstruct_state && s.locked == 0) {
5053 clear_bit(STRIPE_EXPAND_READY, &sh->state);
5054 atomic_dec(&conf->reshape_stripes);
5055 wake_up(&conf->wait_for_overlap);
Yufen Yuc911c462020-07-18 05:29:07 -04005056 md_done_sync(conf->mddev, RAID5_STRIPE_SECTORS(conf), 1);
NeilBrown3687c062011-07-27 11:00:36 +10005057 }
5058
5059 if (s.expanding && s.locked == 0 &&
5060 !test_bit(STRIPE_COMPUTE_RUN, &sh->state))
5061 handle_stripe_expansion(conf, sh);
5062
5063finish:
Dan Williams6bfe0b42008-04-30 00:52:32 -07005064 /* wait for this device to become unblocked */
NeilBrown5f066c62012-07-03 12:13:29 +10005065 if (unlikely(s.blocked_rdev)) {
5066 if (conf->mddev->external)
5067 md_wait_for_blocked_rdev(s.blocked_rdev,
5068 conf->mddev);
5069 else
5070 /* Internal metadata will immediately
5071 * be written by raid5d, so we don't
5072 * need to wait here.
5073 */
5074 rdev_dec_pending(s.blocked_rdev,
5075 conf->mddev);
5076 }
Dan Williams6bfe0b42008-04-30 00:52:32 -07005077
NeilBrownbc2607f2011-07-28 11:39:22 +10005078 if (s.handle_bad_blocks)
5079 for (i = disks; i--; ) {
NeilBrown3cb03002011-10-11 16:45:26 +11005080 struct md_rdev *rdev;
NeilBrownbc2607f2011-07-28 11:39:22 +10005081 struct r5dev *dev = &sh->dev[i];
5082 if (test_and_clear_bit(R5_WriteError, &dev->flags)) {
5083 /* We own a safe reference to the rdev */
5084 rdev = conf->disks[i].rdev;
5085 if (!rdev_set_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005086 RAID5_STRIPE_SECTORS(conf), 0))
NeilBrownbc2607f2011-07-28 11:39:22 +10005087 md_error(conf->mddev, rdev);
5088 rdev_dec_pending(rdev, conf->mddev);
5089 }
NeilBrownb84db562011-07-28 11:39:23 +10005090 if (test_and_clear_bit(R5_MadeGood, &dev->flags)) {
5091 rdev = conf->disks[i].rdev;
5092 rdev_clear_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005093 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrownb84db562011-07-28 11:39:23 +10005094 rdev_dec_pending(rdev, conf->mddev);
5095 }
NeilBrown977df362011-12-23 10:17:53 +11005096 if (test_and_clear_bit(R5_MadeGoodRepl, &dev->flags)) {
5097 rdev = conf->disks[i].replacement;
NeilBrowndd054fc2011-12-23 10:17:53 +11005098 if (!rdev)
5099 /* rdev have been moved down */
5100 rdev = conf->disks[i].rdev;
NeilBrown977df362011-12-23 10:17:53 +11005101 rdev_clear_badblocks(rdev, sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005102 RAID5_STRIPE_SECTORS(conf), 0);
NeilBrown977df362011-12-23 10:17:53 +11005103 rdev_dec_pending(rdev, conf->mddev);
5104 }
NeilBrownbc2607f2011-07-28 11:39:22 +10005105 }
5106
Yuri Tikhonov6c0069c2009-08-29 19:13:13 -07005107 if (s.ops_request)
5108 raid_run_ops(sh, s.ops_request);
5109
Dan Williamsf0e43bc2008-06-28 08:31:55 +10005110 ops_run_io(sh, &s);
5111
NeilBrownc5709ef2011-07-26 11:35:20 +10005112 if (s.dec_preread_active) {
NeilBrown729a1862009-12-14 12:49:50 +11005113 /* We delay this until after ops_run_io so that if make_request
Tejun Heoe9c74692010-09-03 11:56:18 +02005114 * is waiting on a flush, it won't continue until the writes
NeilBrown729a1862009-12-14 12:49:50 +11005115 * have actually been submitted.
5116 */
5117 atomic_dec(&conf->preread_active_stripes);
5118 if (atomic_read(&conf->preread_active_stripes) <
5119 IO_THRESHOLD)
5120 md_wakeup_thread(conf->mddev->thread);
5121 }
5122
Dan Williams257a4b42011-11-08 16:22:06 +11005123 clear_bit_unlock(STRIPE_ACTIVE, &sh->state);
NeilBrown16a53ec2006-06-26 00:27:38 -07005124}
5125
NeilBrownd1688a62011-10-11 16:49:52 +11005126static void raid5_activate_delayed(struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005127{
5128 if (atomic_read(&conf->preread_active_stripes) < IO_THRESHOLD) {
5129 while (!list_empty(&conf->delayed_list)) {
5130 struct list_head *l = conf->delayed_list.next;
5131 struct stripe_head *sh;
5132 sh = list_entry(l, struct stripe_head, lru);
5133 list_del_init(l);
5134 clear_bit(STRIPE_DELAYED, &sh->state);
5135 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5136 atomic_inc(&conf->preread_active_stripes);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005137 list_add_tail(&sh->lru, &conf->hold_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08005138 raid5_wakeup_stripe_thread(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005139 }
NeilBrown482c0832011-04-18 18:25:42 +10005140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141}
5142
Shaohua Li566c09c2013-11-14 15:16:17 +11005143static void activate_bit_delay(struct r5conf *conf,
5144 struct list_head *temp_inactive_list)
NeilBrown72626682005-09-09 16:23:54 -07005145{
5146 /* device_lock is held */
5147 struct list_head head;
5148 list_add(&head, &conf->bitmap_list);
5149 list_del_init(&conf->bitmap_list);
5150 while (!list_empty(&head)) {
5151 struct stripe_head *sh = list_entry(head.next, struct stripe_head, lru);
Shaohua Li566c09c2013-11-14 15:16:17 +11005152 int hash;
NeilBrown72626682005-09-09 16:23:54 -07005153 list_del_init(&sh->lru);
5154 atomic_inc(&sh->count);
Shaohua Li566c09c2013-11-14 15:16:17 +11005155 hash = sh->hash_lock_index;
5156 __release_stripe(conf, sh, &temp_inactive_list[hash]);
NeilBrown72626682005-09-09 16:23:54 -07005157 }
5158}
5159
NeilBrownfd01b882011-10-11 16:47:53 +11005160static int in_chunk_boundary(struct mddev *mddev, struct bio *bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005161{
NeilBrown3cb5edf2015-07-15 17:24:17 +10005162 struct r5conf *conf = mddev->private;
Christoph Hellwig10433d02017-08-23 19:10:28 +02005163 sector_t sector = bio->bi_iter.bi_sector;
NeilBrown3cb5edf2015-07-15 17:24:17 +10005164 unsigned int chunk_sectors;
Kent Overstreetaa8b57a2013-02-05 15:19:29 -08005165 unsigned int bio_sectors = bio_sectors(bio);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005166
Christoph Hellwig10433d02017-08-23 19:10:28 +02005167 WARN_ON_ONCE(bio->bi_partno);
5168
NeilBrown3cb5edf2015-07-15 17:24:17 +10005169 chunk_sectors = min(conf->chunk_sectors, conf->prev_chunk_sectors);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005170 return chunk_sectors >=
5171 ((sector & (chunk_sectors - 1)) + bio_sectors);
5172}
5173
5174/*
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005175 * add bio to the retry LIFO ( in O(1) ... we are in interrupt )
5176 * later sampled by raid5d.
5177 */
NeilBrownd1688a62011-10-11 16:49:52 +11005178static void add_bio_to_retry(struct bio *bi,struct r5conf *conf)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005179{
5180 unsigned long flags;
5181
5182 spin_lock_irqsave(&conf->device_lock, flags);
5183
5184 bi->bi_next = conf->retry_read_aligned_list;
5185 conf->retry_read_aligned_list = bi;
5186
5187 spin_unlock_irqrestore(&conf->device_lock, flags);
5188 md_wakeup_thread(conf->mddev->thread);
5189}
5190
NeilBrown0472a422017-03-15 14:05:13 +11005191static struct bio *remove_bio_from_retry(struct r5conf *conf,
5192 unsigned int *offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005193{
5194 struct bio *bi;
5195
5196 bi = conf->retry_read_aligned;
5197 if (bi) {
NeilBrown0472a422017-03-15 14:05:13 +11005198 *offset = conf->retry_read_offset;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005199 conf->retry_read_aligned = NULL;
5200 return bi;
5201 }
5202 bi = conf->retry_read_aligned_list;
5203 if(bi) {
Neil Brown387bb172007-02-08 14:20:29 -08005204 conf->retry_read_aligned_list = bi->bi_next;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005205 bi->bi_next = NULL;
NeilBrown0472a422017-03-15 14:05:13 +11005206 *offset = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005207 }
5208
5209 return bi;
5210}
5211
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005212/*
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005213 * The "raid5_align_endio" should check if the read succeeded and if it
5214 * did, call bio_endio on the original bio (having bio_put the new bio
5215 * first).
5216 * If the read failed..
5217 */
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005218static void raid5_align_endio(struct bio *bi)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005219{
5220 struct bio* raid_bi = bi->bi_private;
NeilBrownfd01b882011-10-11 16:47:53 +11005221 struct mddev *mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11005222 struct r5conf *conf;
NeilBrown3cb03002011-10-11 16:45:26 +11005223 struct md_rdev *rdev;
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005224 blk_status_t error = bi->bi_status;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005225
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005226 bio_put(bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005227
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005228 rdev = (void*)raid_bi->bi_next;
5229 raid_bi->bi_next = NULL;
NeilBrown2b7f2222010-03-25 16:06:03 +11005230 mddev = rdev->mddev;
5231 conf = mddev->private;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005232
5233 rdev_dec_pending(rdev, conf->mddev);
5234
Sasha Levin9b81c842015-08-10 19:05:18 -04005235 if (!error) {
Christoph Hellwig4246a0b2015-07-20 15:29:37 +02005236 bio_endio(raid_bi);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005237 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10005238 wake_up(&conf->wait_for_quiescent);
NeilBrown6712ecf2007-09-27 12:47:43 +02005239 return;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005240 }
5241
Dan Williams45b42332007-07-09 11:56:43 -07005242 pr_debug("raid5_align_endio : io error...handing IO for a retry\n");
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005243
5244 add_bio_to_retry(raid_bi, conf);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005245}
5246
Ming Lin7ef6b122015-05-06 22:51:24 -07005247static int raid5_read_one_chunk(struct mddev *mddev, struct bio *raid_bio)
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005248{
NeilBrownd1688a62011-10-11 16:49:52 +11005249 struct r5conf *conf = mddev->private;
NeilBrown8553fe7ec2009-12-14 12:49:47 +11005250 int dd_idx;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005251 struct bio* align_bi;
NeilBrown3cb03002011-10-11 16:45:26 +11005252 struct md_rdev *rdev;
NeilBrown671488c2011-12-23 10:17:52 +11005253 sector_t end_sector;
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005254
5255 if (!in_chunk_boundary(mddev, raid_bio)) {
Ming Lin7ef6b122015-05-06 22:51:24 -07005256 pr_debug("%s: non aligned\n", __func__);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005257 return 0;
5258 }
5259 /*
Ming Leid7a10302017-02-14 23:29:03 +08005260 * use bio_clone_fast to make a copy of the bio
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005261 */
Kent Overstreetafeee512018-05-20 18:25:52 -04005262 align_bi = bio_clone_fast(raid_bio, GFP_NOIO, &mddev->bio_set);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005263 if (!align_bi)
5264 return 0;
5265 /*
5266 * set bi_end_io to a new function, and set bi_private to the
5267 * original bio.
5268 */
5269 align_bi->bi_end_io = raid5_align_endio;
5270 align_bi->bi_private = raid_bio;
5271 /*
5272 * compute position
5273 */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005274 align_bi->bi_iter.bi_sector =
5275 raid5_compute_sector(conf, raid_bio->bi_iter.bi_sector,
5276 0, &dd_idx, NULL);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005277
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005278 end_sector = bio_end_sector(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005279 rcu_read_lock();
NeilBrown671488c2011-12-23 10:17:52 +11005280 rdev = rcu_dereference(conf->disks[dd_idx].replacement);
5281 if (!rdev || test_bit(Faulty, &rdev->flags) ||
5282 rdev->recovery_offset < end_sector) {
5283 rdev = rcu_dereference(conf->disks[dd_idx].rdev);
5284 if (rdev &&
5285 (test_bit(Faulty, &rdev->flags) ||
5286 !(test_bit(In_sync, &rdev->flags) ||
5287 rdev->recovery_offset >= end_sector)))
5288 rdev = NULL;
5289 }
Song Liu03b047f2017-01-11 13:39:14 -08005290
5291 if (r5c_big_stripe_cached(conf, align_bi->bi_iter.bi_sector)) {
5292 rcu_read_unlock();
5293 bio_put(align_bi);
5294 return 0;
5295 }
5296
NeilBrown671488c2011-12-23 10:17:52 +11005297 if (rdev) {
NeilBrown31c176e2011-07-28 11:39:22 +10005298 sector_t first_bad;
5299 int bad_sectors;
5300
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005301 atomic_inc(&rdev->nr_pending);
5302 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005303 raid_bio->bi_next = (void*)rdev;
Christoph Hellwig74d46992017-08-23 19:10:32 +02005304 bio_set_dev(align_bi, rdev->bdev);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005305
Kent Overstreet7140aaf2013-09-25 13:37:01 -07005306 if (is_badblock(rdev, align_bi->bi_iter.bi_sector,
Kent Overstreet4f024f32013-10-11 15:44:27 -07005307 bio_sectors(align_bi),
NeilBrown31c176e2011-07-28 11:39:22 +10005308 &first_bad, &bad_sectors)) {
Neil Brown387bb172007-02-08 14:20:29 -08005309 bio_put(align_bi);
5310 rdev_dec_pending(rdev, mddev);
5311 return 0;
5312 }
5313
majianpeng6c0544e2012-06-12 08:31:10 +08005314 /* No reshape active, so we can trust rdev->data_offset */
Kent Overstreet4f024f32013-10-11 15:44:27 -07005315 align_bi->bi_iter.bi_sector += rdev->data_offset;
majianpeng6c0544e2012-06-12 08:31:10 +08005316
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005317 spin_lock_irq(&conf->device_lock);
Yuanhan Liub1b46482015-05-08 18:19:06 +10005318 wait_event_lock_irq(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005319 conf->quiesce == 0,
Lukas Czernereed8c022012-11-30 11:42:40 +01005320 conf->device_lock);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005321 atomic_inc(&conf->active_aligned_reads);
5322 spin_unlock_irq(&conf->device_lock);
5323
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005324 if (mddev->gendisk)
Christoph Hellwig74d46992017-08-23 19:10:32 +02005325 trace_block_bio_remap(align_bi->bi_disk->queue,
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005326 align_bi, disk_devt(mddev->gendisk),
Kent Overstreet4f024f32013-10-11 15:44:27 -07005327 raid_bio->bi_iter.bi_sector);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005328 submit_bio_noacct(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005329 return 1;
5330 } else {
5331 rcu_read_unlock();
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08005332 bio_put(align_bi);
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005333 return 0;
5334 }
5335}
5336
Ming Lin7ef6b122015-05-06 22:51:24 -07005337static struct bio *chunk_aligned_read(struct mddev *mddev, struct bio *raid_bio)
5338{
5339 struct bio *split;
NeilBrowndd7a8f52017-04-05 14:05:51 +10005340 sector_t sector = raid_bio->bi_iter.bi_sector;
5341 unsigned chunk_sects = mddev->chunk_sectors;
5342 unsigned sectors = chunk_sects - (sector & (chunk_sects-1));
Ming Lin7ef6b122015-05-06 22:51:24 -07005343
NeilBrowndd7a8f52017-04-05 14:05:51 +10005344 if (sectors < bio_sectors(raid_bio)) {
5345 struct r5conf *conf = mddev->private;
Kent Overstreetafeee512018-05-20 18:25:52 -04005346 split = bio_split(raid_bio, sectors, GFP_NOIO, &conf->bio_split);
NeilBrowndd7a8f52017-04-05 14:05:51 +10005347 bio_chain(split, raid_bio);
Christoph Hellwiged00aab2020-07-01 10:59:44 +02005348 submit_bio_noacct(raid_bio);
NeilBrowndd7a8f52017-04-05 14:05:51 +10005349 raid_bio = split;
5350 }
Ming Lin7ef6b122015-05-06 22:51:24 -07005351
NeilBrowndd7a8f52017-04-05 14:05:51 +10005352 if (!raid5_read_one_chunk(mddev, raid_bio))
5353 return raid_bio;
Ming Lin7ef6b122015-05-06 22:51:24 -07005354
5355 return NULL;
5356}
5357
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005358/* __get_priority_stripe - get the next stripe to process
5359 *
5360 * Full stripe writes are allowed to pass preread active stripes up until
5361 * the bypass_threshold is exceeded. In general the bypass_count
5362 * increments when the handle_list is handled before the hold_list; however, it
5363 * will not be incremented when STRIPE_IO_STARTED is sampled set signifying a
5364 * stripe with in flight i/o. The bypass_count will be reset when the
5365 * head of the hold_list has changed, i.e. the head was promoted to the
5366 * handle_list.
5367 */
Shaohua Li851c30c2013-08-28 14:30:16 +08005368static struct stripe_head *__get_priority_stripe(struct r5conf *conf, int group)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005369{
Shaohua Li535ae4e2017-02-15 19:37:32 -08005370 struct stripe_head *sh, *tmp;
Shaohua Li851c30c2013-08-28 14:30:16 +08005371 struct list_head *handle_list = NULL;
Shaohua Li535ae4e2017-02-15 19:37:32 -08005372 struct r5worker_group *wg;
Song Liu70d466f2017-05-11 15:28:28 -07005373 bool second_try = !r5c_is_writeback(conf->log) &&
5374 !r5l_log_disk_error(conf);
5375 bool try_loprio = test_bit(R5C_LOG_TIGHT, &conf->cache_state) ||
5376 r5l_log_disk_error(conf);
Shaohua Li851c30c2013-08-28 14:30:16 +08005377
Shaohua Li535ae4e2017-02-15 19:37:32 -08005378again:
5379 wg = NULL;
5380 sh = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005381 if (conf->worker_cnt_per_group == 0) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005382 handle_list = try_loprio ? &conf->loprio_list :
5383 &conf->handle_list;
Shaohua Li851c30c2013-08-28 14:30:16 +08005384 } else if (group != ANY_GROUP) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005385 handle_list = try_loprio ? &conf->worker_groups[group].loprio_list :
5386 &conf->worker_groups[group].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005387 wg = &conf->worker_groups[group];
Shaohua Li851c30c2013-08-28 14:30:16 +08005388 } else {
5389 int i;
5390 for (i = 0; i < conf->group_cnt; i++) {
Shaohua Li535ae4e2017-02-15 19:37:32 -08005391 handle_list = try_loprio ? &conf->worker_groups[i].loprio_list :
5392 &conf->worker_groups[i].handle_list;
Shaohua Libfc90cb2013-08-29 15:40:32 +08005393 wg = &conf->worker_groups[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08005394 if (!list_empty(handle_list))
5395 break;
5396 }
5397 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005398
5399 pr_debug("%s: handle: %s hold: %s full_writes: %d bypass_count: %d\n",
5400 __func__,
Shaohua Li851c30c2013-08-28 14:30:16 +08005401 list_empty(handle_list) ? "empty" : "busy",
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005402 list_empty(&conf->hold_list) ? "empty" : "busy",
5403 atomic_read(&conf->pending_full_writes), conf->bypass_count);
5404
Shaohua Li851c30c2013-08-28 14:30:16 +08005405 if (!list_empty(handle_list)) {
5406 sh = list_entry(handle_list->next, typeof(*sh), lru);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005407
5408 if (list_empty(&conf->hold_list))
5409 conf->bypass_count = 0;
5410 else if (!test_bit(STRIPE_IO_STARTED, &sh->state)) {
5411 if (conf->hold_list.next == conf->last_hold)
5412 conf->bypass_count++;
5413 else {
5414 conf->last_hold = conf->hold_list.next;
5415 conf->bypass_count -= conf->bypass_threshold;
5416 if (conf->bypass_count < 0)
5417 conf->bypass_count = 0;
5418 }
5419 }
5420 } else if (!list_empty(&conf->hold_list) &&
5421 ((conf->bypass_threshold &&
5422 conf->bypass_count > conf->bypass_threshold) ||
5423 atomic_read(&conf->pending_full_writes) == 0)) {
Shaohua Li851c30c2013-08-28 14:30:16 +08005424
5425 list_for_each_entry(tmp, &conf->hold_list, lru) {
5426 if (conf->worker_cnt_per_group == 0 ||
5427 group == ANY_GROUP ||
5428 !cpu_online(tmp->cpu) ||
5429 cpu_to_group(tmp->cpu) == group) {
5430 sh = tmp;
5431 break;
5432 }
5433 }
5434
5435 if (sh) {
5436 conf->bypass_count -= conf->bypass_threshold;
5437 if (conf->bypass_count < 0)
5438 conf->bypass_count = 0;
5439 }
Shaohua Libfc90cb2013-08-29 15:40:32 +08005440 wg = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08005441 }
5442
Shaohua Li535ae4e2017-02-15 19:37:32 -08005443 if (!sh) {
5444 if (second_try)
5445 return NULL;
5446 second_try = true;
5447 try_loprio = !try_loprio;
5448 goto again;
5449 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005450
Shaohua Libfc90cb2013-08-29 15:40:32 +08005451 if (wg) {
5452 wg->stripes_cnt--;
5453 sh->group = NULL;
5454 }
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005455 list_del_init(&sh->lru);
Shaohua Lic7a6d352014-04-15 09:12:54 +08005456 BUG_ON(atomic_inc_return(&sh->count) != 1);
Dan Williams8b3e6cd2008-04-28 02:15:53 -07005457 return sh;
5458}
Raz Ben-Jehuda(caro)f6796232006-12-10 02:20:46 -08005459
Shaohua Li8811b592012-08-02 08:33:00 +10005460struct raid5_plug_cb {
5461 struct blk_plug_cb cb;
5462 struct list_head list;
Shaohua Li566c09c2013-11-14 15:16:17 +11005463 struct list_head temp_inactive_list[NR_STRIPE_HASH_LOCKS];
Shaohua Li8811b592012-08-02 08:33:00 +10005464};
5465
5466static void raid5_unplug(struct blk_plug_cb *blk_cb, bool from_schedule)
5467{
5468 struct raid5_plug_cb *cb = container_of(
5469 blk_cb, struct raid5_plug_cb, cb);
5470 struct stripe_head *sh;
5471 struct mddev *mddev = cb->cb.data;
5472 struct r5conf *conf = mddev->private;
NeilBrowna9add5d2012-10-31 11:59:09 +11005473 int cnt = 0;
Shaohua Li566c09c2013-11-14 15:16:17 +11005474 int hash;
Shaohua Li8811b592012-08-02 08:33:00 +10005475
5476 if (cb->list.next && !list_empty(&cb->list)) {
5477 spin_lock_irq(&conf->device_lock);
5478 while (!list_empty(&cb->list)) {
5479 sh = list_first_entry(&cb->list, struct stripe_head, lru);
5480 list_del_init(&sh->lru);
5481 /*
5482 * avoid race release_stripe_plug() sees
5483 * STRIPE_ON_UNPLUG_LIST clear but the stripe
5484 * is still in our list
5485 */
Peter Zijlstra4e857c52014-03-17 18:06:10 +01005486 smp_mb__before_atomic();
Shaohua Li8811b592012-08-02 08:33:00 +10005487 clear_bit(STRIPE_ON_UNPLUG_LIST, &sh->state);
Shaohua Li773ca822013-08-27 17:50:39 +08005488 /*
5489 * STRIPE_ON_RELEASE_LIST could be set here. In that
5490 * case, the count is always > 1 here
5491 */
Shaohua Li566c09c2013-11-14 15:16:17 +11005492 hash = sh->hash_lock_index;
5493 __release_stripe(conf, sh, &cb->temp_inactive_list[hash]);
NeilBrowna9add5d2012-10-31 11:59:09 +11005494 cnt++;
Shaohua Li8811b592012-08-02 08:33:00 +10005495 }
5496 spin_unlock_irq(&conf->device_lock);
5497 }
Shaohua Li566c09c2013-11-14 15:16:17 +11005498 release_inactive_stripe_list(conf, cb->temp_inactive_list,
5499 NR_STRIPE_HASH_LOCKS);
Jonathan Brassowe3620a32013-03-07 16:22:01 -06005500 if (mddev->queue)
5501 trace_block_unplug(mddev->queue, cnt, !from_schedule);
Shaohua Li8811b592012-08-02 08:33:00 +10005502 kfree(cb);
5503}
5504
5505static void release_stripe_plug(struct mddev *mddev,
5506 struct stripe_head *sh)
5507{
5508 struct blk_plug_cb *blk_cb = blk_check_plugged(
5509 raid5_unplug, mddev,
5510 sizeof(struct raid5_plug_cb));
5511 struct raid5_plug_cb *cb;
5512
5513 if (!blk_cb) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005514 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005515 return;
5516 }
5517
5518 cb = container_of(blk_cb, struct raid5_plug_cb, cb);
5519
Shaohua Li566c09c2013-11-14 15:16:17 +11005520 if (cb->list.next == NULL) {
5521 int i;
Shaohua Li8811b592012-08-02 08:33:00 +10005522 INIT_LIST_HEAD(&cb->list);
Shaohua Li566c09c2013-11-14 15:16:17 +11005523 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
5524 INIT_LIST_HEAD(cb->temp_inactive_list + i);
5525 }
Shaohua Li8811b592012-08-02 08:33:00 +10005526
5527 if (!test_and_set_bit(STRIPE_ON_UNPLUG_LIST, &sh->state))
5528 list_add_tail(&sh->lru, &cb->list);
5529 else
Shaohua Li6d036f72015-08-13 14:31:57 -07005530 raid5_release_stripe(sh);
Shaohua Li8811b592012-08-02 08:33:00 +10005531}
5532
Shaohua Li620125f2012-10-11 13:49:05 +11005533static void make_discard_request(struct mddev *mddev, struct bio *bi)
5534{
5535 struct r5conf *conf = mddev->private;
5536 sector_t logical_sector, last_sector;
5537 struct stripe_head *sh;
Shaohua Li620125f2012-10-11 13:49:05 +11005538 int stripe_sectors;
5539
5540 if (mddev->reshape_position != MaxSector)
5541 /* Skip discard while reshape is happening */
5542 return;
5543
Yufen Yuc911c462020-07-18 05:29:07 -04005544 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
Guoqing Jiangb0f01ec2019-09-03 11:41:03 +02005545 last_sector = bio_end_sector(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005546
5547 bi->bi_next = NULL;
Shaohua Li620125f2012-10-11 13:49:05 +11005548
5549 stripe_sectors = conf->chunk_sectors *
5550 (conf->raid_disks - conf->max_degraded);
5551 logical_sector = DIV_ROUND_UP_SECTOR_T(logical_sector,
5552 stripe_sectors);
5553 sector_div(last_sector, stripe_sectors);
5554
5555 logical_sector *= conf->chunk_sectors;
5556 last_sector *= conf->chunk_sectors;
5557
5558 for (; logical_sector < last_sector;
Yufen Yuc911c462020-07-18 05:29:07 -04005559 logical_sector += RAID5_STRIPE_SECTORS(conf)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005560 DEFINE_WAIT(w);
5561 int d;
5562 again:
Shaohua Li6d036f72015-08-13 14:31:57 -07005563 sh = raid5_get_active_stripe(conf, logical_sector, 0, 0, 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005564 prepare_to_wait(&conf->wait_for_overlap, &w,
5565 TASK_UNINTERRUPTIBLE);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005566 set_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
5567 if (test_bit(STRIPE_SYNCING, &sh->state)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005568 raid5_release_stripe(sh);
NeilBrownf8dfcff2013-03-12 12:18:06 +11005569 schedule();
5570 goto again;
5571 }
5572 clear_bit(R5_Overlap, &sh->dev[sh->pd_idx].flags);
Shaohua Li620125f2012-10-11 13:49:05 +11005573 spin_lock_irq(&sh->stripe_lock);
5574 for (d = 0; d < conf->raid_disks; d++) {
5575 if (d == sh->pd_idx || d == sh->qd_idx)
5576 continue;
5577 if (sh->dev[d].towrite || sh->dev[d].toread) {
5578 set_bit(R5_Overlap, &sh->dev[d].flags);
5579 spin_unlock_irq(&sh->stripe_lock);
Shaohua Li6d036f72015-08-13 14:31:57 -07005580 raid5_release_stripe(sh);
Shaohua Li620125f2012-10-11 13:49:05 +11005581 schedule();
5582 goto again;
5583 }
5584 }
NeilBrownf8dfcff2013-03-12 12:18:06 +11005585 set_bit(STRIPE_DISCARD, &sh->state);
Shaohua Li620125f2012-10-11 13:49:05 +11005586 finish_wait(&conf->wait_for_overlap, &w);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005587 sh->overwrite_disks = 0;
Shaohua Li620125f2012-10-11 13:49:05 +11005588 for (d = 0; d < conf->raid_disks; d++) {
5589 if (d == sh->pd_idx || d == sh->qd_idx)
5590 continue;
5591 sh->dev[d].towrite = bi;
5592 set_bit(R5_OVERWRITE, &sh->dev[d].flags);
NeilBrown016c76a2017-03-15 14:05:13 +11005593 bio_inc_remaining(bi);
NeilBrown49728052017-03-15 14:05:12 +11005594 md_write_inc(mddev, bi);
shli@kernel.org7a87f432014-12-15 12:57:03 +11005595 sh->overwrite_disks++;
Shaohua Li620125f2012-10-11 13:49:05 +11005596 }
5597 spin_unlock_irq(&sh->stripe_lock);
5598 if (conf->mddev->bitmap) {
5599 for (d = 0;
5600 d < conf->raid_disks - conf->max_degraded;
5601 d++)
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005602 md_bitmap_startwrite(mddev->bitmap,
5603 sh->sector,
Yufen Yuc911c462020-07-18 05:29:07 -04005604 RAID5_STRIPE_SECTORS(conf),
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07005605 0);
Shaohua Li620125f2012-10-11 13:49:05 +11005606 sh->bm_seq = conf->seq_flush + 1;
5607 set_bit(STRIPE_BIT_DELAY, &sh->state);
5608 }
5609
5610 set_bit(STRIPE_HANDLE, &sh->state);
5611 clear_bit(STRIPE_DELAYED, &sh->state);
5612 if (!test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5613 atomic_inc(&conf->preread_active_stripes);
5614 release_stripe_plug(mddev, sh);
5615 }
5616
NeilBrown016c76a2017-03-15 14:05:13 +11005617 bio_endio(bi);
Shaohua Li620125f2012-10-11 13:49:05 +11005618}
5619
NeilBrowncc27b0c2017-06-05 16:49:39 +10005620static bool raid5_make_request(struct mddev *mddev, struct bio * bi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005621{
NeilBrownd1688a62011-10-11 16:49:52 +11005622 struct r5conf *conf = mddev->private;
NeilBrown911d4ee2009-03-31 14:39:38 +11005623 int dd_idx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005624 sector_t new_sector;
5625 sector_t logical_sector, last_sector;
5626 struct stripe_head *sh;
Jens Axboea3623572005-11-01 09:26:16 +01005627 const int rw = bio_data_dir(bi);
Shaohua Li27c0f682014-04-09 11:25:47 +08005628 DEFINE_WAIT(w);
5629 bool do_prepare;
Song Liu3bddb7f2016-11-18 16:46:50 -08005630 bool do_flush = false;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005631
Jens Axboe1eff9d32016-08-05 15:35:16 -06005632 if (unlikely(bi->bi_opf & REQ_PREFLUSH)) {
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01005633 int ret = log_handle_flush_request(conf, bi);
Shaohua Li828cbe92015-09-02 13:49:49 -07005634
5635 if (ret == 0)
NeilBrowncc27b0c2017-06-05 16:49:39 +10005636 return true;
Shaohua Li828cbe92015-09-02 13:49:49 -07005637 if (ret == -ENODEV) {
David Jeffery775d7832019-09-16 13:15:14 -04005638 if (md_flush_request(mddev, bi))
5639 return true;
Shaohua Li828cbe92015-09-02 13:49:49 -07005640 }
5641 /* ret == -EAGAIN, fallback */
Song Liu3bddb7f2016-11-18 16:46:50 -08005642 /*
5643 * if r5l_handle_flush_request() didn't clear REQ_PREFLUSH,
5644 * we need to flush journal device
5645 */
5646 do_flush = bi->bi_opf & REQ_PREFLUSH;
NeilBrowne5dcdd82005-09-09 16:23:41 -07005647 }
5648
NeilBrowncc27b0c2017-06-05 16:49:39 +10005649 if (!md_write_start(mddev, bi))
5650 return false;
Eric Mei9ffc8f72015-03-18 23:39:11 -06005651 /*
5652 * If array is degraded, better not do chunk aligned read because
5653 * later we might have to read it again in order to reconstruct
5654 * data on failed drives.
5655 */
5656 if (rw == READ && mddev->degraded == 0 &&
Ming Lin7ef6b122015-05-06 22:51:24 -07005657 mddev->reshape_position == MaxSector) {
5658 bi = chunk_aligned_read(mddev, bi);
5659 if (!bi)
NeilBrowncc27b0c2017-06-05 16:49:39 +10005660 return true;
Ming Lin7ef6b122015-05-06 22:51:24 -07005661 }
Raz Ben-Jehuda(caro)52488612006-12-10 02:20:48 -08005662
Mike Christie796a5cf2016-06-05 14:32:07 -05005663 if (unlikely(bio_op(bi) == REQ_OP_DISCARD)) {
Shaohua Li620125f2012-10-11 13:49:05 +11005664 make_discard_request(mddev, bi);
NeilBrowncc27b0c2017-06-05 16:49:39 +10005665 md_write_end(mddev);
5666 return true;
Shaohua Li620125f2012-10-11 13:49:05 +11005667 }
5668
Yufen Yuc911c462020-07-18 05:29:07 -04005669 logical_sector = bi->bi_iter.bi_sector & ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07005670 last_sector = bio_end_sector(bi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005671 bi->bi_next = NULL;
NeilBrown06d91a52005-06-21 17:17:12 -07005672
Shaohua Li27c0f682014-04-09 11:25:47 +08005673 prepare_to_wait(&conf->wait_for_overlap, &w, TASK_UNINTERRUPTIBLE);
Yufen Yuc911c462020-07-18 05:29:07 -04005674 for (; logical_sector < last_sector; logical_sector += RAID5_STRIPE_SECTORS(conf)) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005675 int previous;
NeilBrownc46501b2013-08-27 15:52:13 +10005676 int seq;
NeilBrownb578d552006-03-27 01:18:12 -08005677
Shaohua Li27c0f682014-04-09 11:25:47 +08005678 do_prepare = false;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005679 retry:
NeilBrownc46501b2013-08-27 15:52:13 +10005680 seq = read_seqcount_begin(&conf->gen_lock);
NeilBrownb5663ba2009-03-31 14:39:38 +11005681 previous = 0;
Shaohua Li27c0f682014-04-09 11:25:47 +08005682 if (do_prepare)
5683 prepare_to_wait(&conf->wait_for_overlap, &w,
5684 TASK_UNINTERRUPTIBLE);
NeilBrownb0f9ec02009-03-31 15:27:18 +11005685 if (unlikely(conf->reshape_progress != MaxSector)) {
NeilBrownfef9c612009-03-31 15:16:46 +11005686 /* spinlock is needed as reshape_progress may be
NeilBrowndf8e7f762006-03-27 01:18:15 -08005687 * 64bit on a 32bit platform, and so it might be
5688 * possible to see a half-updated value
Jesper Juhlaeb878b2011-04-10 18:06:17 +02005689 * Of course reshape_progress could change after
NeilBrowndf8e7f762006-03-27 01:18:15 -08005690 * the lock is dropped, so once we get a reference
5691 * to the stripe that we think it is, we will have
5692 * to check again.
5693 */
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005694 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005695 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005696 ? logical_sector < conf->reshape_progress
5697 : logical_sector >= conf->reshape_progress) {
NeilBrownb5663ba2009-03-31 14:39:38 +11005698 previous = 1;
5699 } else {
NeilBrown2c810cd2012-05-21 09:27:00 +10005700 if (mddev->reshape_backwards
NeilBrownfef9c612009-03-31 15:16:46 +11005701 ? logical_sector < conf->reshape_safe
5702 : logical_sector >= conf->reshape_safe) {
NeilBrownb578d552006-03-27 01:18:12 -08005703 spin_unlock_irq(&conf->device_lock);
5704 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005705 do_prepare = true;
NeilBrownb578d552006-03-27 01:18:12 -08005706 goto retry;
5707 }
5708 }
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005709 spin_unlock_irq(&conf->device_lock);
5710 }
NeilBrown16a53ec2006-06-26 00:27:38 -07005711
NeilBrown112bf892009-03-31 14:39:38 +11005712 new_sector = raid5_compute_sector(conf, logical_sector,
5713 previous,
NeilBrown911d4ee2009-03-31 14:39:38 +11005714 &dd_idx, NULL);
Shaohua Li849674e2016-01-20 13:52:20 -08005715 pr_debug("raid456: raid5_make_request, sector %llu logical %llu\n",
NeilBrownc46501b2013-08-27 15:52:13 +10005716 (unsigned long long)new_sector,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005717 (unsigned long long)logical_sector);
5718
Shaohua Li6d036f72015-08-13 14:31:57 -07005719 sh = raid5_get_active_stripe(conf, new_sector, previous,
Jens Axboe1eff9d32016-08-05 15:35:16 -06005720 (bi->bi_opf & REQ_RAHEAD), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005721 if (sh) {
NeilBrownb0f9ec02009-03-31 15:27:18 +11005722 if (unlikely(previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005723 /* expansion might have moved on while waiting for a
NeilBrowndf8e7f762006-03-27 01:18:15 -08005724 * stripe, so we must do the range check again.
5725 * Expansion could still move past after this
5726 * test, but as we are holding a reference to
5727 * 'sh', we know that if that happens,
5728 * STRIPE_EXPANDING will get set and the expansion
5729 * won't proceed until we finish with the stripe.
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005730 */
5731 int must_retry = 0;
5732 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005733 if (mddev->reshape_backwards
NeilBrownb0f9ec02009-03-31 15:27:18 +11005734 ? logical_sector >= conf->reshape_progress
5735 : logical_sector < conf->reshape_progress)
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005736 /* mismatch, need to try again */
5737 must_retry = 1;
5738 spin_unlock_irq(&conf->device_lock);
5739 if (must_retry) {
Shaohua Li6d036f72015-08-13 14:31:57 -07005740 raid5_release_stripe(sh);
Dan Williams7a3ab902009-06-16 16:00:33 -07005741 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005742 do_prepare = true;
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005743 goto retry;
5744 }
5745 }
NeilBrownc46501b2013-08-27 15:52:13 +10005746 if (read_seqcount_retry(&conf->gen_lock, seq)) {
5747 /* Might have got the wrong stripe_head
5748 * by accident
5749 */
Shaohua Li6d036f72015-08-13 14:31:57 -07005750 raid5_release_stripe(sh);
NeilBrownc46501b2013-08-27 15:52:13 +10005751 goto retry;
5752 }
NeilBrowne62e58a2009-07-01 13:15:35 +10005753
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005754 if (test_bit(STRIPE_EXPANDING, &sh->state) ||
shli@kernel.orgda41ba62014-12-15 12:57:03 +11005755 !add_stripe_bio(sh, bi, dd_idx, rw, previous)) {
NeilBrown7ecaa1e2006-03-27 01:18:08 -08005756 /* Stripe is busy expanding or
5757 * add failed due to overlap. Flush everything
Linus Torvalds1da177e2005-04-16 15:20:36 -07005758 * and wait a while
5759 */
NeilBrown482c0832011-04-18 18:25:42 +10005760 md_wakeup_thread(mddev->thread);
Shaohua Li6d036f72015-08-13 14:31:57 -07005761 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005762 schedule();
Shaohua Li27c0f682014-04-09 11:25:47 +08005763 do_prepare = true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764 goto retry;
5765 }
Song Liu3bddb7f2016-11-18 16:46:50 -08005766 if (do_flush) {
5767 set_bit(STRIPE_R5C_PREFLUSH, &sh->state);
5768 /* we only need flush for one stripe */
5769 do_flush = false;
5770 }
5771
Guoqing Jiang1684e972020-06-16 11:25:52 +02005772 set_bit(STRIPE_HANDLE, &sh->state);
NeilBrown6ed30032008-02-06 01:40:00 -08005773 clear_bit(STRIPE_DELAYED, &sh->state);
shli@kernel.org59fc6302014-12-15 12:57:03 +11005774 if ((!sh->batch_head || sh == sh->batch_head) &&
Jens Axboe1eff9d32016-08-05 15:35:16 -06005775 (bi->bi_opf & REQ_SYNC) &&
NeilBrown729a1862009-12-14 12:49:50 +11005776 !test_and_set_bit(STRIPE_PREREAD_ACTIVE, &sh->state))
5777 atomic_inc(&conf->preread_active_stripes);
Shaohua Li8811b592012-08-02 08:33:00 +10005778 release_stripe_plug(mddev, sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005779 } else {
5780 /* cannot get stripe for read-ahead, just give-up */
Christoph Hellwig4e4cbee2017-06-03 09:38:06 +02005781 bi->bi_status = BLK_STS_IOERR;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 break;
5783 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 }
Shaohua Li27c0f682014-04-09 11:25:47 +08005785 finish_wait(&conf->wait_for_overlap, &w);
NeilBrown7c13edc2011-04-18 18:25:43 +10005786
NeilBrown49728052017-03-15 14:05:12 +11005787 if (rw == WRITE)
5788 md_write_end(mddev);
NeilBrown016c76a2017-03-15 14:05:13 +11005789 bio_endio(bi);
NeilBrowncc27b0c2017-06-05 16:49:39 +10005790 return true;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791}
5792
NeilBrownfd01b882011-10-11 16:47:53 +11005793static sector_t raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks);
Dan Williamsb522adc2009-03-31 15:00:31 +11005794
NeilBrownfd01b882011-10-11 16:47:53 +11005795static sector_t reshape_request(struct mddev *mddev, sector_t sector_nr, int *skipped)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005796{
NeilBrown52c03292006-06-26 00:27:43 -07005797 /* reshaping is quite different to recovery/resync so it is
5798 * handled quite separately ... here.
5799 *
5800 * On each call to sync_request, we gather one chunk worth of
5801 * destination stripes and flag them as expanding.
5802 * Then we find all the source stripes and request reads.
5803 * As the reads complete, handle_stripe will copy the data
5804 * into the destination stripe and release that stripe.
5805 */
NeilBrownd1688a62011-10-11 16:49:52 +11005806 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005807 struct stripe_head *sh;
NeilBrowndb0505d2017-10-17 16:18:36 +11005808 struct md_rdev *rdev;
NeilBrownccfcc3c2006-03-27 01:18:09 -08005809 sector_t first_sector, last_sector;
NeilBrownf4168852007-02-28 20:11:53 -08005810 int raid_disks = conf->previous_raid_disks;
5811 int data_disks = raid_disks - conf->max_degraded;
5812 int new_data_disks = conf->raid_disks - conf->max_degraded;
NeilBrown52c03292006-06-26 00:27:43 -07005813 int i;
5814 int dd_idx;
NeilBrownc8f517c2009-03-31 15:28:40 +11005815 sector_t writepos, readpos, safepos;
NeilBrownec32a2b2009-03-31 15:17:38 +11005816 sector_t stripe_addr;
NeilBrown7a661382009-03-31 15:21:40 +11005817 int reshape_sectors;
NeilBrownab69ae12009-03-31 15:26:47 +11005818 struct list_head stripes;
NeilBrown92140482015-07-06 12:28:45 +10005819 sector_t retn;
NeilBrown52c03292006-06-26 00:27:43 -07005820
NeilBrownfef9c612009-03-31 15:16:46 +11005821 if (sector_nr == 0) {
5822 /* If restarting in the middle, skip the initial sectors */
NeilBrown2c810cd2012-05-21 09:27:00 +10005823 if (mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005824 conf->reshape_progress < raid5_size(mddev, 0, 0)) {
5825 sector_nr = raid5_size(mddev, 0, 0)
5826 - conf->reshape_progress;
NeilBrown6cbd8142015-07-24 13:30:32 +10005827 } else if (mddev->reshape_backwards &&
5828 conf->reshape_progress == MaxSector) {
5829 /* shouldn't happen, but just in case, finish up.*/
5830 sector_nr = MaxSector;
NeilBrown2c810cd2012-05-21 09:27:00 +10005831 } else if (!mddev->reshape_backwards &&
NeilBrownfef9c612009-03-31 15:16:46 +11005832 conf->reshape_progress > 0)
5833 sector_nr = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005834 sector_div(sector_nr, new_data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005835 if (sector_nr) {
NeilBrown8dee7212009-11-06 14:59:29 +11005836 mddev->curr_resync_completed = sector_nr;
Junxiao Bie1a86db2020-07-14 16:10:26 -07005837 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrownfef9c612009-03-31 15:16:46 +11005838 *skipped = 1;
NeilBrown92140482015-07-06 12:28:45 +10005839 retn = sector_nr;
5840 goto finish;
NeilBrownfef9c612009-03-31 15:16:46 +11005841 }
NeilBrown52c03292006-06-26 00:27:43 -07005842 }
5843
NeilBrown7a661382009-03-31 15:21:40 +11005844 /* We need to process a full chunk at a time.
5845 * If old and new chunk sizes differ, we need to process the
5846 * largest of these
5847 */
NeilBrown3cb5edf2015-07-15 17:24:17 +10005848
5849 reshape_sectors = max(conf->chunk_sectors, conf->prev_chunk_sectors);
NeilBrown7a661382009-03-31 15:21:40 +11005850
NeilBrownb5254dd2012-05-21 09:27:01 +10005851 /* We update the metadata at least every 10 seconds, or when
5852 * the data about to be copied would over-write the source of
5853 * the data at the front of the range. i.e. one new_stripe
5854 * along from reshape_progress new_maps to after where
5855 * reshape_safe old_maps to
NeilBrown52c03292006-06-26 00:27:43 -07005856 */
NeilBrownfef9c612009-03-31 15:16:46 +11005857 writepos = conf->reshape_progress;
NeilBrownf4168852007-02-28 20:11:53 -08005858 sector_div(writepos, new_data_disks);
NeilBrownc8f517c2009-03-31 15:28:40 +11005859 readpos = conf->reshape_progress;
5860 sector_div(readpos, data_disks);
NeilBrownfef9c612009-03-31 15:16:46 +11005861 safepos = conf->reshape_safe;
NeilBrownf4168852007-02-28 20:11:53 -08005862 sector_div(safepos, data_disks);
NeilBrown2c810cd2012-05-21 09:27:00 +10005863 if (mddev->reshape_backwards) {
NeilBrownc74c0d72015-07-15 17:54:15 +10005864 BUG_ON(writepos < reshape_sectors);
5865 writepos -= reshape_sectors;
NeilBrownc8f517c2009-03-31 15:28:40 +11005866 readpos += reshape_sectors;
NeilBrown7a661382009-03-31 15:21:40 +11005867 safepos += reshape_sectors;
NeilBrownfef9c612009-03-31 15:16:46 +11005868 } else {
NeilBrown7a661382009-03-31 15:21:40 +11005869 writepos += reshape_sectors;
NeilBrownc74c0d72015-07-15 17:54:15 +10005870 /* readpos and safepos are worst-case calculations.
5871 * A negative number is overly pessimistic, and causes
5872 * obvious problems for unsigned storage. So clip to 0.
5873 */
NeilBrowned37d832009-05-27 21:39:05 +10005874 readpos -= min_t(sector_t, reshape_sectors, readpos);
5875 safepos -= min_t(sector_t, reshape_sectors, safepos);
NeilBrownfef9c612009-03-31 15:16:46 +11005876 }
NeilBrown52c03292006-06-26 00:27:43 -07005877
NeilBrownb5254dd2012-05-21 09:27:01 +10005878 /* Having calculated the 'writepos' possibly use it
5879 * to set 'stripe_addr' which is where we will write to.
5880 */
5881 if (mddev->reshape_backwards) {
5882 BUG_ON(conf->reshape_progress == 0);
5883 stripe_addr = writepos;
5884 BUG_ON((mddev->dev_sectors &
5885 ~((sector_t)reshape_sectors - 1))
5886 - reshape_sectors - stripe_addr
5887 != sector_nr);
5888 } else {
5889 BUG_ON(writepos != sector_nr + reshape_sectors);
5890 stripe_addr = sector_nr;
5891 }
5892
NeilBrownc8f517c2009-03-31 15:28:40 +11005893 /* 'writepos' is the most advanced device address we might write.
5894 * 'readpos' is the least advanced device address we might read.
5895 * 'safepos' is the least address recorded in the metadata as having
5896 * been reshaped.
NeilBrownb5254dd2012-05-21 09:27:01 +10005897 * If there is a min_offset_diff, these are adjusted either by
5898 * increasing the safepos/readpos if diff is negative, or
5899 * increasing writepos if diff is positive.
5900 * If 'readpos' is then behind 'writepos', there is no way that we can
NeilBrownc8f517c2009-03-31 15:28:40 +11005901 * ensure safety in the face of a crash - that must be done by userspace
5902 * making a backup of the data. So in that case there is no particular
5903 * rush to update metadata.
5904 * Otherwise if 'safepos' is behind 'writepos', then we really need to
5905 * update the metadata to advance 'safepos' to match 'readpos' so that
5906 * we can be safe in the event of a crash.
5907 * So we insist on updating metadata if safepos is behind writepos and
5908 * readpos is beyond writepos.
5909 * In any case, update the metadata every 10 seconds.
5910 * Maybe that number should be configurable, but I'm not sure it is
5911 * worth it.... maybe it could be a multiple of safemode_delay???
5912 */
NeilBrownb5254dd2012-05-21 09:27:01 +10005913 if (conf->min_offset_diff < 0) {
5914 safepos += -conf->min_offset_diff;
5915 readpos += -conf->min_offset_diff;
5916 } else
5917 writepos += conf->min_offset_diff;
5918
NeilBrown2c810cd2012-05-21 09:27:00 +10005919 if ((mddev->reshape_backwards
NeilBrownc8f517c2009-03-31 15:28:40 +11005920 ? (safepos > writepos && readpos < writepos)
5921 : (safepos < writepos && readpos > writepos)) ||
5922 time_after(jiffies, conf->reshape_checkpoint + 10*HZ)) {
NeilBrown52c03292006-06-26 00:27:43 -07005923 /* Cannot proceed until we've updated the superblock... */
5924 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11005925 atomic_read(&conf->reshape_stripes)==0
5926 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5927 if (atomic_read(&conf->reshape_stripes) != 0)
5928 return 0;
NeilBrownfef9c612009-03-31 15:16:46 +11005929 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11005930 mddev->curr_resync_completed = sector_nr;
NeilBrowndb0505d2017-10-17 16:18:36 +11005931 if (!mddev->reshape_backwards)
5932 /* Can update recovery_offset */
5933 rdev_for_each(rdev, mddev)
5934 if (rdev->raid_disk >= 0 &&
5935 !test_bit(Journal, &rdev->flags) &&
5936 !test_bit(In_sync, &rdev->flags) &&
5937 rdev->recovery_offset < sector_nr)
5938 rdev->recovery_offset = sector_nr;
5939
NeilBrownc8f517c2009-03-31 15:28:40 +11005940 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08005941 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown52c03292006-06-26 00:27:43 -07005942 md_wakeup_thread(mddev->thread);
Shaohua Li29530792016-12-08 15:48:19 -08005943 wait_event(mddev->sb_wait, mddev->sb_flags == 0 ||
NeilBrownc91abf52013-11-19 12:02:01 +11005944 test_bit(MD_RECOVERY_INTR, &mddev->recovery));
5945 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
5946 return 0;
NeilBrown52c03292006-06-26 00:27:43 -07005947 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11005948 conf->reshape_safe = mddev->reshape_position;
NeilBrown52c03292006-06-26 00:27:43 -07005949 spin_unlock_irq(&conf->device_lock);
5950 wake_up(&conf->wait_for_overlap);
Junxiao Bie1a86db2020-07-14 16:10:26 -07005951 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrown52c03292006-06-26 00:27:43 -07005952 }
5953
NeilBrownab69ae12009-03-31 15:26:47 +11005954 INIT_LIST_HEAD(&stripes);
Yufen Yuc911c462020-07-18 05:29:07 -04005955 for (i = 0; i < reshape_sectors; i += RAID5_STRIPE_SECTORS(conf)) {
NeilBrown52c03292006-06-26 00:27:43 -07005956 int j;
NeilBrowna9f326e2009-09-23 18:06:41 +10005957 int skipped_disk = 0;
Shaohua Li6d036f72015-08-13 14:31:57 -07005958 sh = raid5_get_active_stripe(conf, stripe_addr+i, 0, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07005959 set_bit(STRIPE_EXPANDING, &sh->state);
5960 atomic_inc(&conf->reshape_stripes);
5961 /* If any of this stripe is beyond the end of the old
5962 * array, then we need to zero those blocks
5963 */
5964 for (j=sh->disks; j--;) {
5965 sector_t s;
5966 if (j == sh->pd_idx)
5967 continue;
NeilBrownf4168852007-02-28 20:11:53 -08005968 if (conf->level == 6 &&
NeilBrownd0dabf72009-03-31 14:39:38 +11005969 j == sh->qd_idx)
NeilBrownf4168852007-02-28 20:11:53 -08005970 continue;
Shaohua Li6d036f72015-08-13 14:31:57 -07005971 s = raid5_compute_blocknr(sh, j, 0);
Dan Williamsb522adc2009-03-31 15:00:31 +11005972 if (s < raid5_size(mddev, 0, 0)) {
NeilBrowna9f326e2009-09-23 18:06:41 +10005973 skipped_disk = 1;
NeilBrown52c03292006-06-26 00:27:43 -07005974 continue;
5975 }
Yufen Yuc911c462020-07-18 05:29:07 -04005976 memset(page_address(sh->dev[j].page), 0, RAID5_STRIPE_SIZE(conf));
NeilBrown52c03292006-06-26 00:27:43 -07005977 set_bit(R5_Expanded, &sh->dev[j].flags);
5978 set_bit(R5_UPTODATE, &sh->dev[j].flags);
5979 }
NeilBrowna9f326e2009-09-23 18:06:41 +10005980 if (!skipped_disk) {
NeilBrown52c03292006-06-26 00:27:43 -07005981 set_bit(STRIPE_EXPAND_READY, &sh->state);
5982 set_bit(STRIPE_HANDLE, &sh->state);
5983 }
NeilBrownab69ae12009-03-31 15:26:47 +11005984 list_add(&sh->lru, &stripes);
NeilBrown52c03292006-06-26 00:27:43 -07005985 }
5986 spin_lock_irq(&conf->device_lock);
NeilBrown2c810cd2012-05-21 09:27:00 +10005987 if (mddev->reshape_backwards)
NeilBrown7a661382009-03-31 15:21:40 +11005988 conf->reshape_progress -= reshape_sectors * new_data_disks;
NeilBrownfef9c612009-03-31 15:16:46 +11005989 else
NeilBrown7a661382009-03-31 15:21:40 +11005990 conf->reshape_progress += reshape_sectors * new_data_disks;
NeilBrown52c03292006-06-26 00:27:43 -07005991 spin_unlock_irq(&conf->device_lock);
5992 /* Ok, those stripe are ready. We can start scheduling
5993 * reads on the source stripes.
5994 * The source stripes are determined by mapping the first and last
5995 * block on the destination stripes.
5996 */
NeilBrown52c03292006-06-26 00:27:43 -07005997 first_sector =
NeilBrownec32a2b2009-03-31 15:17:38 +11005998 raid5_compute_sector(conf, stripe_addr*(new_data_disks),
NeilBrown911d4ee2009-03-31 14:39:38 +11005999 1, &dd_idx, NULL);
NeilBrown52c03292006-06-26 00:27:43 -07006000 last_sector =
NeilBrown0e6e0272009-06-09 16:32:22 +10006001 raid5_compute_sector(conf, ((stripe_addr+reshape_sectors)
Andre Noll09c9e5f2009-06-18 08:45:55 +10006002 * new_data_disks - 1),
NeilBrown911d4ee2009-03-31 14:39:38 +11006003 1, &dd_idx, NULL);
Andre Noll58c0fed2009-03-31 14:33:13 +11006004 if (last_sector >= mddev->dev_sectors)
6005 last_sector = mddev->dev_sectors - 1;
NeilBrown52c03292006-06-26 00:27:43 -07006006 while (first_sector <= last_sector) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006007 sh = raid5_get_active_stripe(conf, first_sector, 1, 0, 1);
NeilBrown52c03292006-06-26 00:27:43 -07006008 set_bit(STRIPE_EXPAND_SOURCE, &sh->state);
6009 set_bit(STRIPE_HANDLE, &sh->state);
Shaohua Li6d036f72015-08-13 14:31:57 -07006010 raid5_release_stripe(sh);
Yufen Yuc911c462020-07-18 05:29:07 -04006011 first_sector += RAID5_STRIPE_SECTORS(conf);
NeilBrown52c03292006-06-26 00:27:43 -07006012 }
NeilBrownab69ae12009-03-31 15:26:47 +11006013 /* Now that the sources are clearly marked, we can release
6014 * the destination stripes
6015 */
6016 while (!list_empty(&stripes)) {
6017 sh = list_entry(stripes.next, struct stripe_head, lru);
6018 list_del_init(&sh->lru);
Shaohua Li6d036f72015-08-13 14:31:57 -07006019 raid5_release_stripe(sh);
NeilBrownab69ae12009-03-31 15:26:47 +11006020 }
NeilBrownc6207272008-02-06 01:39:52 -08006021 /* If this takes us to the resync_max point where we have to pause,
6022 * then we need to write out the superblock.
6023 */
NeilBrown7a661382009-03-31 15:21:40 +11006024 sector_nr += reshape_sectors;
NeilBrown92140482015-07-06 12:28:45 +10006025 retn = reshape_sectors;
6026finish:
NeilBrownc5e19d92015-07-17 12:06:02 +10006027 if (mddev->curr_resync_completed > mddev->resync_max ||
6028 (sector_nr - mddev->curr_resync_completed) * 2
NeilBrownc03f6a12009-04-17 11:06:30 +10006029 >= mddev->resync_max - mddev->curr_resync_completed) {
NeilBrownc6207272008-02-06 01:39:52 -08006030 /* Cannot proceed until we've updated the superblock... */
6031 wait_event(conf->wait_for_overlap,
NeilBrownc91abf52013-11-19 12:02:01 +11006032 atomic_read(&conf->reshape_stripes) == 0
6033 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6034 if (atomic_read(&conf->reshape_stripes) != 0)
6035 goto ret;
NeilBrownfef9c612009-03-31 15:16:46 +11006036 mddev->reshape_position = conf->reshape_progress;
NeilBrown75d3da42011-01-14 09:14:34 +11006037 mddev->curr_resync_completed = sector_nr;
NeilBrowndb0505d2017-10-17 16:18:36 +11006038 if (!mddev->reshape_backwards)
6039 /* Can update recovery_offset */
6040 rdev_for_each(rdev, mddev)
6041 if (rdev->raid_disk >= 0 &&
6042 !test_bit(Journal, &rdev->flags) &&
6043 !test_bit(In_sync, &rdev->flags) &&
6044 rdev->recovery_offset < sector_nr)
6045 rdev->recovery_offset = sector_nr;
NeilBrownc8f517c2009-03-31 15:28:40 +11006046 conf->reshape_checkpoint = jiffies;
Shaohua Li29530792016-12-08 15:48:19 -08006047 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownc6207272008-02-06 01:39:52 -08006048 md_wakeup_thread(mddev->thread);
6049 wait_event(mddev->sb_wait,
Shaohua Li29530792016-12-08 15:48:19 -08006050 !test_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags)
NeilBrownc91abf52013-11-19 12:02:01 +11006051 || test_bit(MD_RECOVERY_INTR, &mddev->recovery));
6052 if (test_bit(MD_RECOVERY_INTR, &mddev->recovery))
6053 goto ret;
NeilBrownc6207272008-02-06 01:39:52 -08006054 spin_lock_irq(&conf->device_lock);
NeilBrownfef9c612009-03-31 15:16:46 +11006055 conf->reshape_safe = mddev->reshape_position;
NeilBrownc6207272008-02-06 01:39:52 -08006056 spin_unlock_irq(&conf->device_lock);
6057 wake_up(&conf->wait_for_overlap);
Junxiao Bie1a86db2020-07-14 16:10:26 -07006058 sysfs_notify_dirent_safe(mddev->sysfs_completed);
NeilBrownc6207272008-02-06 01:39:52 -08006059 }
NeilBrownc91abf52013-11-19 12:02:01 +11006060ret:
NeilBrown92140482015-07-06 12:28:45 +10006061 return retn;
NeilBrown52c03292006-06-26 00:27:43 -07006062}
6063
Shaohua Li849674e2016-01-20 13:52:20 -08006064static inline sector_t raid5_sync_request(struct mddev *mddev, sector_t sector_nr,
6065 int *skipped)
NeilBrown52c03292006-06-26 00:27:43 -07006066{
NeilBrownd1688a62011-10-11 16:49:52 +11006067 struct r5conf *conf = mddev->private;
NeilBrown52c03292006-06-26 00:27:43 -07006068 struct stripe_head *sh;
Andre Noll58c0fed2009-03-31 14:33:13 +11006069 sector_t max_sector = mddev->dev_sectors;
NeilBrown57dab0b2010-10-19 10:03:39 +11006070 sector_t sync_blocks;
NeilBrown16a53ec2006-06-26 00:27:38 -07006071 int still_degraded = 0;
6072 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073
NeilBrown72626682005-09-09 16:23:54 -07006074 if (sector_nr >= max_sector) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 /* just being told to finish up .. nothing much to do */
NeilBrowncea9c222009-03-31 15:15:05 +11006076
NeilBrown29269552006-03-27 01:18:10 -08006077 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery)) {
6078 end_reshape(conf);
6079 return 0;
6080 }
NeilBrown72626682005-09-09 16:23:54 -07006081
6082 if (mddev->curr_resync < max_sector) /* aborted */
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006083 md_bitmap_end_sync(mddev->bitmap, mddev->curr_resync,
6084 &sync_blocks, 1);
NeilBrown16a53ec2006-06-26 00:27:38 -07006085 else /* completed sync */
NeilBrown72626682005-09-09 16:23:54 -07006086 conf->fullsync = 0;
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006087 md_bitmap_close_sync(mddev->bitmap);
NeilBrown72626682005-09-09 16:23:54 -07006088
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 return 0;
6090 }
NeilBrownccfcc3c2006-03-27 01:18:09 -08006091
NeilBrown64bd6602009-08-03 10:59:58 +10006092 /* Allow raid5_quiesce to complete */
6093 wait_event(conf->wait_for_overlap, conf->quiesce != 2);
6094
NeilBrown52c03292006-06-26 00:27:43 -07006095 if (test_bit(MD_RECOVERY_RESHAPE, &mddev->recovery))
6096 return reshape_request(mddev, sector_nr, skipped);
NeilBrownf6705572006-03-27 01:18:11 -08006097
NeilBrownc6207272008-02-06 01:39:52 -08006098 /* No need to check resync_max as we never do more than one
6099 * stripe, and as resync_max will always be on a chunk boundary,
6100 * if the check in md_do_sync didn't fire, there is no chance
6101 * of overstepping resync_max here
6102 */
6103
NeilBrown16a53ec2006-06-26 00:27:38 -07006104 /* if there is too many failed drives and we are trying
Linus Torvalds1da177e2005-04-16 15:20:36 -07006105 * to resync, then assert that we are finished, because there is
6106 * nothing we can do.
6107 */
NeilBrown3285edf2006-06-26 00:27:55 -07006108 if (mddev->degraded >= conf->max_degraded &&
NeilBrown16a53ec2006-06-26 00:27:38 -07006109 test_bit(MD_RECOVERY_SYNC, &mddev->recovery)) {
Andre Noll58c0fed2009-03-31 14:33:13 +11006110 sector_t rv = mddev->dev_sectors - sector_nr;
NeilBrown57afd892005-06-21 17:17:13 -07006111 *skipped = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006112 return rv;
6113 }
majianpeng6f608042013-04-24 11:42:41 +10006114 if (!test_bit(MD_RECOVERY_REQUESTED, &mddev->recovery) &&
6115 !conf->fullsync &&
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006116 !md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, 1) &&
Yufen Yuc911c462020-07-18 05:29:07 -04006117 sync_blocks >= RAID5_STRIPE_SECTORS(conf)) {
NeilBrown72626682005-09-09 16:23:54 -07006118 /* we can skip this block, and probably more */
Yufen Yu83c3e5e2020-07-22 23:29:05 -04006119 do_div(sync_blocks, RAID5_STRIPE_SECTORS(conf));
NeilBrown72626682005-09-09 16:23:54 -07006120 *skipped = 1;
Yufen Yuc911c462020-07-18 05:29:07 -04006121 /* keep things rounded to whole stripes */
6122 return sync_blocks * RAID5_STRIPE_SECTORS(conf);
NeilBrown72626682005-09-09 16:23:54 -07006123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006124
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006125 md_bitmap_cond_end_sync(mddev->bitmap, sector_nr, false);
NeilBrownb47490c2008-02-06 01:39:50 -08006126
Shaohua Li6d036f72015-08-13 14:31:57 -07006127 sh = raid5_get_active_stripe(conf, sector_nr, 0, 1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006128 if (sh == NULL) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006129 sh = raid5_get_active_stripe(conf, sector_nr, 0, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006130 /* make sure we don't swamp the stripe cache if someone else
NeilBrown16a53ec2006-06-26 00:27:38 -07006131 * is trying to get access
Linus Torvalds1da177e2005-04-16 15:20:36 -07006132 */
Nishanth Aravamudan66c006a2005-11-07 01:01:17 -08006133 schedule_timeout_uninterruptible(1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006134 }
NeilBrown16a53ec2006-06-26 00:27:38 -07006135 /* Need to check if array will still be degraded after recovery/resync
Eric Mei16d9cfa2015-01-06 09:35:02 -08006136 * Note in case of > 1 drive failures it's possible we're rebuilding
6137 * one drive while leaving another faulty drive in array.
NeilBrown16a53ec2006-06-26 00:27:38 -07006138 */
Eric Mei16d9cfa2015-01-06 09:35:02 -08006139 rcu_read_lock();
6140 for (i = 0; i < conf->raid_disks; i++) {
Mark Rutland6aa7de02017-10-23 14:07:29 -07006141 struct md_rdev *rdev = READ_ONCE(conf->disks[i].rdev);
Eric Mei16d9cfa2015-01-06 09:35:02 -08006142
6143 if (rdev == NULL || test_bit(Faulty, &rdev->flags))
NeilBrown16a53ec2006-06-26 00:27:38 -07006144 still_degraded = 1;
Eric Mei16d9cfa2015-01-06 09:35:02 -08006145 }
6146 rcu_read_unlock();
NeilBrown16a53ec2006-06-26 00:27:38 -07006147
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006148 md_bitmap_start_sync(mddev->bitmap, sector_nr, &sync_blocks, still_degraded);
NeilBrown16a53ec2006-06-26 00:27:38 -07006149
NeilBrown83206d62011-07-26 11:19:49 +10006150 set_bit(STRIPE_SYNC_REQUESTED, &sh->state);
Eivind Sarto053f5b62014-06-09 17:06:19 -07006151 set_bit(STRIPE_HANDLE, &sh->state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006152
Shaohua Li6d036f72015-08-13 14:31:57 -07006153 raid5_release_stripe(sh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
Yufen Yuc911c462020-07-18 05:29:07 -04006155 return RAID5_STRIPE_SECTORS(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006156}
6157
NeilBrown0472a422017-03-15 14:05:13 +11006158static int retry_aligned_read(struct r5conf *conf, struct bio *raid_bio,
6159 unsigned int offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006160{
6161 /* We may not be able to submit a whole bio at once as there
6162 * may not be enough stripe_heads available.
6163 * We cannot pre-allocate enough stripe_heads as we may need
6164 * more than exist in the cache (if we allow ever large chunks).
6165 * So we do one stripe head at a time and record in
6166 * ->bi_hw_segments how many have been done.
6167 *
6168 * We *know* that this entire raid_bio is in one chunk, so
6169 * it will be only one 'dd_idx' and only need one call to raid5_compute_sector.
6170 */
6171 struct stripe_head *sh;
NeilBrown911d4ee2009-03-31 14:39:38 +11006172 int dd_idx;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006173 sector_t sector, logical_sector, last_sector;
6174 int scnt = 0;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006175 int handled = 0;
6176
Kent Overstreet4f024f32013-10-11 15:44:27 -07006177 logical_sector = raid_bio->bi_iter.bi_sector &
Yufen Yuc911c462020-07-18 05:29:07 -04006178 ~((sector_t)RAID5_STRIPE_SECTORS(conf)-1);
NeilBrown112bf892009-03-31 14:39:38 +11006179 sector = raid5_compute_sector(conf, logical_sector,
NeilBrown911d4ee2009-03-31 14:39:38 +11006180 0, &dd_idx, NULL);
Kent Overstreetf73a1c72012-09-25 15:05:12 -07006181 last_sector = bio_end_sector(raid_bio);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006182
6183 for (; logical_sector < last_sector;
Yufen Yuc911c462020-07-18 05:29:07 -04006184 logical_sector += RAID5_STRIPE_SECTORS(conf),
6185 sector += RAID5_STRIPE_SECTORS(conf),
Neil Brown387bb172007-02-08 14:20:29 -08006186 scnt++) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006187
NeilBrown0472a422017-03-15 14:05:13 +11006188 if (scnt < offset)
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006189 /* already done this stripe */
6190 continue;
6191
Shaohua Li6d036f72015-08-13 14:31:57 -07006192 sh = raid5_get_active_stripe(conf, sector, 0, 1, 1);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006193
6194 if (!sh) {
6195 /* failed to get a stripe - must wait */
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006196 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006197 conf->retry_read_offset = scnt;
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006198 return handled;
6199 }
6200
shli@kernel.orgda41ba62014-12-15 12:57:03 +11006201 if (!add_stripe_bio(sh, raid_bio, dd_idx, 0, 0)) {
Shaohua Li6d036f72015-08-13 14:31:57 -07006202 raid5_release_stripe(sh);
Neil Brown387bb172007-02-08 14:20:29 -08006203 conf->retry_read_aligned = raid_bio;
NeilBrown0472a422017-03-15 14:05:13 +11006204 conf->retry_read_offset = scnt;
Neil Brown387bb172007-02-08 14:20:29 -08006205 return handled;
6206 }
6207
majianpeng3f9e7c12012-07-31 10:04:21 +10006208 set_bit(R5_ReadNoMerge, &sh->dev[dd_idx].flags);
Dan Williams36d1c642009-07-14 11:48:22 -07006209 handle_stripe(sh);
Shaohua Li6d036f72015-08-13 14:31:57 -07006210 raid5_release_stripe(sh);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006211 handled++;
6212 }
NeilBrown016c76a2017-03-15 14:05:13 +11006213
6214 bio_endio(raid_bio);
6215
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006216 if (atomic_dec_and_test(&conf->active_aligned_reads))
Yuanhan Liub1b46482015-05-08 18:19:06 +10006217 wake_up(&conf->wait_for_quiescent);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006218 return handled;
6219}
6220
Shaohua Libfc90cb2013-08-29 15:40:32 +08006221static int handle_active_stripes(struct r5conf *conf, int group,
Shaohua Li566c09c2013-11-14 15:16:17 +11006222 struct r5worker *worker,
6223 struct list_head *temp_inactive_list)
Christoph Hellwigefcd4872019-04-04 18:56:16 +02006224 __releases(&conf->device_lock)
6225 __acquires(&conf->device_lock)
Shaohua Li46a06402012-08-02 08:33:15 +10006226{
6227 struct stripe_head *batch[MAX_STRIPE_BATCH], *sh;
Shaohua Li566c09c2013-11-14 15:16:17 +11006228 int i, batch_size = 0, hash;
6229 bool release_inactive = false;
Shaohua Li46a06402012-08-02 08:33:15 +10006230
6231 while (batch_size < MAX_STRIPE_BATCH &&
Shaohua Li851c30c2013-08-28 14:30:16 +08006232 (sh = __get_priority_stripe(conf, group)) != NULL)
Shaohua Li46a06402012-08-02 08:33:15 +10006233 batch[batch_size++] = sh;
6234
Shaohua Li566c09c2013-11-14 15:16:17 +11006235 if (batch_size == 0) {
6236 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
6237 if (!list_empty(temp_inactive_list + i))
6238 break;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006239 if (i == NR_STRIPE_HASH_LOCKS) {
6240 spin_unlock_irq(&conf->device_lock);
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01006241 log_flush_stripe_to_raid(conf);
Shaohua Lia8c34f92015-09-02 13:49:46 -07006242 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006243 return batch_size;
Shaohua Lia8c34f92015-09-02 13:49:46 -07006244 }
Shaohua Li566c09c2013-11-14 15:16:17 +11006245 release_inactive = true;
6246 }
Shaohua Li46a06402012-08-02 08:33:15 +10006247 spin_unlock_irq(&conf->device_lock);
6248
Shaohua Li566c09c2013-11-14 15:16:17 +11006249 release_inactive_stripe_list(conf, temp_inactive_list,
6250 NR_STRIPE_HASH_LOCKS);
6251
Shaohua Lia8c34f92015-09-02 13:49:46 -07006252 r5l_flush_stripe_to_raid(conf->log);
Shaohua Li566c09c2013-11-14 15:16:17 +11006253 if (release_inactive) {
6254 spin_lock_irq(&conf->device_lock);
6255 return 0;
6256 }
6257
Shaohua Li46a06402012-08-02 08:33:15 +10006258 for (i = 0; i < batch_size; i++)
6259 handle_stripe(batch[i]);
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006260 log_write_stripe_run(conf);
Shaohua Li46a06402012-08-02 08:33:15 +10006261
6262 cond_resched();
6263
6264 spin_lock_irq(&conf->device_lock);
Shaohua Li566c09c2013-11-14 15:16:17 +11006265 for (i = 0; i < batch_size; i++) {
6266 hash = batch[i]->hash_lock_index;
6267 __release_stripe(conf, batch[i], &temp_inactive_list[hash]);
6268 }
Shaohua Li46a06402012-08-02 08:33:15 +10006269 return batch_size;
6270}
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006271
Shaohua Li851c30c2013-08-28 14:30:16 +08006272static void raid5_do_work(struct work_struct *work)
6273{
6274 struct r5worker *worker = container_of(work, struct r5worker, work);
6275 struct r5worker_group *group = worker->group;
6276 struct r5conf *conf = group->conf;
NeilBrown16d997b2017-03-15 14:05:12 +11006277 struct mddev *mddev = conf->mddev;
Shaohua Li851c30c2013-08-28 14:30:16 +08006278 int group_id = group - conf->worker_groups;
6279 int handled;
6280 struct blk_plug plug;
6281
6282 pr_debug("+++ raid5worker active\n");
6283
6284 blk_start_plug(&plug);
6285 handled = 0;
6286 spin_lock_irq(&conf->device_lock);
6287 while (1) {
6288 int batch_size, released;
6289
Shaohua Li566c09c2013-11-14 15:16:17 +11006290 released = release_stripe_list(conf, worker->temp_inactive_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006291
Shaohua Li566c09c2013-11-14 15:16:17 +11006292 batch_size = handle_active_stripes(conf, group_id, worker,
6293 worker->temp_inactive_list);
Shaohua Libfc90cb2013-08-29 15:40:32 +08006294 worker->working = false;
Shaohua Li851c30c2013-08-28 14:30:16 +08006295 if (!batch_size && !released)
6296 break;
6297 handled += batch_size;
NeilBrown16d997b2017-03-15 14:05:12 +11006298 wait_event_lock_irq(mddev->sb_wait,
6299 !test_bit(MD_SB_CHANGE_PENDING, &mddev->sb_flags),
6300 conf->device_lock);
Shaohua Li851c30c2013-08-28 14:30:16 +08006301 }
6302 pr_debug("%d stripes handled\n", handled);
6303
6304 spin_unlock_irq(&conf->device_lock);
Ofer Heifetz7e96d552017-07-24 09:17:40 +03006305
Song Liu9c72a18e42017-08-24 09:53:59 -07006306 flush_deferred_bios(conf);
6307
6308 r5l_flush_stripe_to_raid(conf->log);
6309
Ofer Heifetz7e96d552017-07-24 09:17:40 +03006310 async_tx_issue_pending_all();
Shaohua Li851c30c2013-08-28 14:30:16 +08006311 blk_finish_plug(&plug);
6312
6313 pr_debug("--- raid5worker inactive\n");
6314}
6315
Linus Torvalds1da177e2005-04-16 15:20:36 -07006316/*
6317 * This is our raid5 kernel thread.
6318 *
6319 * We scan the hash table for stripes which can be handled now.
6320 * During the scan, completed stripes are saved for us by the interrupt
6321 * handler, so that they will not have to wait for our next wakeup.
6322 */
Shaohua Li4ed87312012-10-11 13:34:00 +11006323static void raid5d(struct md_thread *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006324{
Shaohua Li4ed87312012-10-11 13:34:00 +11006325 struct mddev *mddev = thread->mddev;
NeilBrownd1688a62011-10-11 16:49:52 +11006326 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006327 int handled;
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006328 struct blk_plug plug;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006329
Dan Williams45b42332007-07-09 11:56:43 -07006330 pr_debug("+++ raid5d active\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006331
6332 md_check_recovery(mddev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006333
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006334 blk_start_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006335 handled = 0;
6336 spin_lock_irq(&conf->device_lock);
6337 while (1) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006338 struct bio *bio;
Shaohua Li773ca822013-08-27 17:50:39 +08006339 int batch_size, released;
NeilBrown0472a422017-03-15 14:05:13 +11006340 unsigned int offset;
Shaohua Li773ca822013-08-27 17:50:39 +08006341
Shaohua Li566c09c2013-11-14 15:16:17 +11006342 released = release_stripe_list(conf, conf->temp_inactive_list);
NeilBrownedbe83a2015-02-26 12:47:56 +11006343 if (released)
6344 clear_bit(R5_DID_ALLOC, &conf->cache_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006345
NeilBrown0021b7b2012-07-31 09:08:14 +02006346 if (
NeilBrown7c13edc2011-04-18 18:25:43 +10006347 !list_empty(&conf->bitmap_list)) {
6348 /* Now is a good time to flush some bitmap updates */
6349 conf->seq_flush++;
NeilBrown700e4322005-11-28 13:44:10 -08006350 spin_unlock_irq(&conf->device_lock);
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07006351 md_bitmap_unplug(mddev->bitmap);
NeilBrown700e4322005-11-28 13:44:10 -08006352 spin_lock_irq(&conf->device_lock);
NeilBrown7c13edc2011-04-18 18:25:43 +10006353 conf->seq_write = conf->seq_flush;
Shaohua Li566c09c2013-11-14 15:16:17 +11006354 activate_bit_delay(conf, conf->temp_inactive_list);
NeilBrown72626682005-09-09 16:23:54 -07006355 }
NeilBrown0021b7b2012-07-31 09:08:14 +02006356 raid5_activate_delayed(conf);
NeilBrown72626682005-09-09 16:23:54 -07006357
NeilBrown0472a422017-03-15 14:05:13 +11006358 while ((bio = remove_bio_from_retry(conf, &offset))) {
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006359 int ok;
6360 spin_unlock_irq(&conf->device_lock);
NeilBrown0472a422017-03-15 14:05:13 +11006361 ok = retry_aligned_read(conf, bio, offset);
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08006362 spin_lock_irq(&conf->device_lock);
6363 if (!ok)
6364 break;
6365 handled++;
6366 }
6367
Shaohua Li566c09c2013-11-14 15:16:17 +11006368 batch_size = handle_active_stripes(conf, ANY_GROUP, NULL,
6369 conf->temp_inactive_list);
Shaohua Li773ca822013-08-27 17:50:39 +08006370 if (!batch_size && !released)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006371 break;
Shaohua Li46a06402012-08-02 08:33:15 +10006372 handled += batch_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006373
Shaohua Li29530792016-12-08 15:48:19 -08006374 if (mddev->sb_flags & ~(1 << MD_SB_CHANGE_PENDING)) {
Shaohua Li46a06402012-08-02 08:33:15 +10006375 spin_unlock_irq(&conf->device_lock);
NeilBrownde393cd2011-07-28 11:31:48 +10006376 md_check_recovery(mddev);
Shaohua Li46a06402012-08-02 08:33:15 +10006377 spin_lock_irq(&conf->device_lock);
6378 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006379 }
Dan Williams45b42332007-07-09 11:56:43 -07006380 pr_debug("%d stripes handled\n", handled);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006381
6382 spin_unlock_irq(&conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10006383 if (test_and_clear_bit(R5_ALLOC_MORE, &conf->cache_state) &&
6384 mutex_trylock(&conf->cache_size_mutex)) {
NeilBrownedbe83a2015-02-26 12:47:56 +11006385 grow_one_stripe(conf, __GFP_NOWARN);
6386 /* Set flag even if allocation failed. This helps
6387 * slow down allocation requests when mem is short
6388 */
6389 set_bit(R5_DID_ALLOC, &conf->cache_state);
NeilBrown2d5b5692015-07-06 12:49:23 +10006390 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006392
Shaohua Li765d7042017-01-04 09:33:23 -08006393 flush_deferred_bios(conf);
6394
Shaohua Li0576b1c2015-08-13 14:32:00 -07006395 r5l_flush_stripe_to_raid(conf->log);
6396
Dan Williamsc9f21aa2008-07-23 12:05:51 -07006397 async_tx_issue_pending_all();
NeilBrowne1dfa0a2011-04-18 18:25:41 +10006398 blk_finish_plug(&plug);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006399
Dan Williams45b42332007-07-09 11:56:43 -07006400 pr_debug("--- raid5d inactive\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07006401}
6402
NeilBrown3f294f42005-11-08 21:39:25 -08006403static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006404raid5_show_stripe_cache_size(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006405{
NeilBrown7b1485b2014-12-15 12:56:59 +11006406 struct r5conf *conf;
6407 int ret = 0;
6408 spin_lock(&mddev->lock);
6409 conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006410 if (conf)
NeilBrownedbe83a2015-02-26 12:47:56 +11006411 ret = sprintf(page, "%d\n", conf->min_nr_stripes);
NeilBrown7b1485b2014-12-15 12:56:59 +11006412 spin_unlock(&mddev->lock);
6413 return ret;
NeilBrown3f294f42005-11-08 21:39:25 -08006414}
6415
NeilBrownc41d4ac2010-06-01 19:37:24 +10006416int
NeilBrownfd01b882011-10-11 16:47:53 +11006417raid5_set_cache_size(struct mddev *mddev, int size)
NeilBrownc41d4ac2010-06-01 19:37:24 +10006418{
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006419 int result = 0;
NeilBrownd1688a62011-10-11 16:49:52 +11006420 struct r5conf *conf = mddev->private;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006421
6422 if (size <= 16 || size > 32768)
6423 return -EINVAL;
NeilBrown486f0642015-02-25 12:10:35 +11006424
NeilBrownedbe83a2015-02-26 12:47:56 +11006425 conf->min_nr_stripes = size;
NeilBrown2d5b5692015-07-06 12:49:23 +10006426 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006427 while (size < conf->max_nr_stripes &&
6428 drop_one_stripe(conf))
6429 ;
NeilBrown2d5b5692015-07-06 12:49:23 +10006430 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006431
Artur Paszkiewicz2214c262017-05-08 11:56:55 +02006432 md_allow_write(mddev);
NeilBrown486f0642015-02-25 12:10:35 +11006433
NeilBrown2d5b5692015-07-06 12:49:23 +10006434 mutex_lock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006435 while (size > conf->max_nr_stripes)
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006436 if (!grow_one_stripe(conf, GFP_KERNEL)) {
6437 conf->min_nr_stripes = conf->max_nr_stripes;
6438 result = -ENOMEM;
NeilBrown486f0642015-02-25 12:10:35 +11006439 break;
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006440 }
NeilBrown2d5b5692015-07-06 12:49:23 +10006441 mutex_unlock(&conf->cache_size_mutex);
NeilBrown486f0642015-02-25 12:10:35 +11006442
Alexei Naberezhnov483cbbe2018-03-27 16:54:16 -07006443 return result;
NeilBrownc41d4ac2010-06-01 19:37:24 +10006444}
6445EXPORT_SYMBOL(raid5_set_cache_size);
6446
NeilBrown3f294f42005-11-08 21:39:25 -08006447static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006448raid5_store_stripe_cache_size(struct mddev *mddev, const char *page, size_t len)
NeilBrown3f294f42005-11-08 21:39:25 -08006449{
NeilBrown67918752014-12-15 12:57:01 +11006450 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006451 unsigned long new;
Dan Williamsb5470dc2008-06-27 21:44:04 -07006452 int err;
6453
NeilBrown3f294f42005-11-08 21:39:25 -08006454 if (len >= PAGE_SIZE)
6455 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006456 if (kstrtoul(page, 10, &new))
NeilBrown3f294f42005-11-08 21:39:25 -08006457 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006458 err = mddev_lock(mddev);
Dan Williamsb5470dc2008-06-27 21:44:04 -07006459 if (err)
6460 return err;
NeilBrown67918752014-12-15 12:57:01 +11006461 conf = mddev->private;
6462 if (!conf)
6463 err = -ENODEV;
6464 else
6465 err = raid5_set_cache_size(mddev, new);
6466 mddev_unlock(mddev);
6467
6468 return err ?: len;
NeilBrown3f294f42005-11-08 21:39:25 -08006469}
NeilBrown007583c2005-11-08 21:39:30 -08006470
NeilBrown96de1e62005-11-08 21:39:39 -08006471static struct md_sysfs_entry
6472raid5_stripecache_size = __ATTR(stripe_cache_size, S_IRUGO | S_IWUSR,
6473 raid5_show_stripe_cache_size,
6474 raid5_store_stripe_cache_size);
NeilBrown3f294f42005-11-08 21:39:25 -08006475
6476static ssize_t
Markus Stockhausend06f1912014-12-15 12:57:05 +11006477raid5_show_rmw_level(struct mddev *mddev, char *page)
6478{
6479 struct r5conf *conf = mddev->private;
6480 if (conf)
6481 return sprintf(page, "%d\n", conf->rmw_level);
6482 else
6483 return 0;
6484}
6485
6486static ssize_t
6487raid5_store_rmw_level(struct mddev *mddev, const char *page, size_t len)
6488{
6489 struct r5conf *conf = mddev->private;
6490 unsigned long new;
6491
6492 if (!conf)
6493 return -ENODEV;
6494
6495 if (len >= PAGE_SIZE)
6496 return -EINVAL;
6497
6498 if (kstrtoul(page, 10, &new))
6499 return -EINVAL;
6500
6501 if (new != PARITY_DISABLE_RMW && !raid6_call.xor_syndrome)
6502 return -EINVAL;
6503
6504 if (new != PARITY_DISABLE_RMW &&
6505 new != PARITY_ENABLE_RMW &&
6506 new != PARITY_PREFER_RMW)
6507 return -EINVAL;
6508
6509 conf->rmw_level = new;
6510 return len;
6511}
6512
6513static struct md_sysfs_entry
6514raid5_rmw_level = __ATTR(rmw_level, S_IRUGO | S_IWUSR,
6515 raid5_show_rmw_level,
6516 raid5_store_rmw_level);
6517
Yufen Yu3b5408b2020-07-18 05:29:09 -04006518static ssize_t
6519raid5_show_stripe_size(struct mddev *mddev, char *page)
6520{
6521 struct r5conf *conf;
6522 int ret = 0;
6523
6524 spin_lock(&mddev->lock);
6525 conf = mddev->private;
6526 if (conf)
6527 ret = sprintf(page, "%lu\n", RAID5_STRIPE_SIZE(conf));
6528 spin_unlock(&mddev->lock);
6529 return ret;
6530}
6531
6532#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
6533static ssize_t
6534raid5_store_stripe_size(struct mddev *mddev, const char *page, size_t len)
6535{
6536 struct r5conf *conf;
6537 unsigned long new;
6538 int err;
6539
6540 if (len >= PAGE_SIZE)
6541 return -EINVAL;
6542 if (kstrtoul(page, 10, &new))
6543 return -EINVAL;
6544
6545 /*
6546 * The value should not be bigger than PAGE_SIZE. It requires to
Yufen Yu6af10a32020-08-20 09:22:05 -04006547 * be multiple of DEFAULT_STRIPE_SIZE and the value should be power
6548 * of two.
Yufen Yu3b5408b2020-07-18 05:29:09 -04006549 */
Yufen Yu6af10a32020-08-20 09:22:05 -04006550 if (new % DEFAULT_STRIPE_SIZE != 0 ||
6551 new > PAGE_SIZE || new == 0 ||
6552 new != roundup_pow_of_two(new))
Yufen Yu3b5408b2020-07-18 05:29:09 -04006553 return -EINVAL;
6554
6555 err = mddev_lock(mddev);
6556 if (err)
6557 return err;
6558
6559 conf = mddev->private;
6560 if (!conf) {
6561 err = -ENODEV;
6562 goto out_unlock;
6563 }
6564
6565 if (new == conf->stripe_size)
6566 goto out_unlock;
6567
6568 pr_debug("md/raid: change stripe_size from %lu to %lu\n",
6569 conf->stripe_size, new);
6570
6571 mddev_suspend(mddev);
6572 conf->stripe_size = new;
6573 conf->stripe_shift = ilog2(new) - 9;
6574 conf->stripe_sectors = new >> 9;
6575 mddev_resume(mddev);
6576
6577out_unlock:
6578 mddev_unlock(mddev);
6579 return err ?: len;
6580}
6581
6582static struct md_sysfs_entry
6583raid5_stripe_size = __ATTR(stripe_size, 0644,
6584 raid5_show_stripe_size,
6585 raid5_store_stripe_size);
6586#else
6587static struct md_sysfs_entry
6588raid5_stripe_size = __ATTR(stripe_size, 0444,
6589 raid5_show_stripe_size,
6590 NULL);
6591#endif
Markus Stockhausend06f1912014-12-15 12:57:05 +11006592
6593static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006594raid5_show_preread_threshold(struct mddev *mddev, char *page)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006595{
NeilBrown7b1485b2014-12-15 12:56:59 +11006596 struct r5conf *conf;
6597 int ret = 0;
6598 spin_lock(&mddev->lock);
6599 conf = mddev->private;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006600 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006601 ret = sprintf(page, "%d\n", conf->bypass_threshold);
6602 spin_unlock(&mddev->lock);
6603 return ret;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006604}
6605
6606static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006607raid5_store_preread_threshold(struct mddev *mddev, const char *page, size_t len)
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006608{
NeilBrown67918752014-12-15 12:57:01 +11006609 struct r5conf *conf;
Dan Williams4ef197d82008-04-28 02:15:54 -07006610 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006611 int err;
6612
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006613 if (len >= PAGE_SIZE)
6614 return -EINVAL;
Jingoo Hanb29bebd2013-06-01 16:15:16 +09006615 if (kstrtoul(page, 10, &new))
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006616 return -EINVAL;
NeilBrown67918752014-12-15 12:57:01 +11006617
6618 err = mddev_lock(mddev);
6619 if (err)
6620 return err;
6621 conf = mddev->private;
6622 if (!conf)
6623 err = -ENODEV;
NeilBrownedbe83a2015-02-26 12:47:56 +11006624 else if (new > conf->min_nr_stripes)
NeilBrown67918752014-12-15 12:57:01 +11006625 err = -EINVAL;
6626 else
6627 conf->bypass_threshold = new;
6628 mddev_unlock(mddev);
6629 return err ?: len;
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006630}
6631
6632static struct md_sysfs_entry
6633raid5_preread_bypass_threshold = __ATTR(preread_bypass_threshold,
6634 S_IRUGO | S_IWUSR,
6635 raid5_show_preread_threshold,
6636 raid5_store_preread_threshold);
6637
6638static ssize_t
Shaohua Lid592a992014-05-21 17:57:44 +08006639raid5_show_skip_copy(struct mddev *mddev, char *page)
6640{
NeilBrown7b1485b2014-12-15 12:56:59 +11006641 struct r5conf *conf;
6642 int ret = 0;
6643 spin_lock(&mddev->lock);
6644 conf = mddev->private;
Shaohua Lid592a992014-05-21 17:57:44 +08006645 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006646 ret = sprintf(page, "%d\n", conf->skip_copy);
6647 spin_unlock(&mddev->lock);
6648 return ret;
Shaohua Lid592a992014-05-21 17:57:44 +08006649}
6650
6651static ssize_t
6652raid5_store_skip_copy(struct mddev *mddev, const char *page, size_t len)
6653{
NeilBrown67918752014-12-15 12:57:01 +11006654 struct r5conf *conf;
Shaohua Lid592a992014-05-21 17:57:44 +08006655 unsigned long new;
NeilBrown67918752014-12-15 12:57:01 +11006656 int err;
6657
Shaohua Lid592a992014-05-21 17:57:44 +08006658 if (len >= PAGE_SIZE)
6659 return -EINVAL;
Shaohua Lid592a992014-05-21 17:57:44 +08006660 if (kstrtoul(page, 10, &new))
6661 return -EINVAL;
6662 new = !!new;
Shaohua Lid592a992014-05-21 17:57:44 +08006663
NeilBrown67918752014-12-15 12:57:01 +11006664 err = mddev_lock(mddev);
6665 if (err)
6666 return err;
6667 conf = mddev->private;
6668 if (!conf)
6669 err = -ENODEV;
6670 else if (new != conf->skip_copy) {
Christoph Hellwig1cb039f2020-09-24 08:51:38 +02006671 struct request_queue *q = mddev->queue;
6672
NeilBrown67918752014-12-15 12:57:01 +11006673 mddev_suspend(mddev);
6674 conf->skip_copy = new;
6675 if (new)
Christoph Hellwig1cb039f2020-09-24 08:51:38 +02006676 blk_queue_flag_set(QUEUE_FLAG_STABLE_WRITES, q);
NeilBrown67918752014-12-15 12:57:01 +11006677 else
Christoph Hellwig1cb039f2020-09-24 08:51:38 +02006678 blk_queue_flag_clear(QUEUE_FLAG_STABLE_WRITES, q);
NeilBrown67918752014-12-15 12:57:01 +11006679 mddev_resume(mddev);
6680 }
6681 mddev_unlock(mddev);
6682 return err ?: len;
Shaohua Lid592a992014-05-21 17:57:44 +08006683}
6684
6685static struct md_sysfs_entry
6686raid5_skip_copy = __ATTR(skip_copy, S_IRUGO | S_IWUSR,
6687 raid5_show_skip_copy,
6688 raid5_store_skip_copy);
6689
Shaohua Lid592a992014-05-21 17:57:44 +08006690static ssize_t
NeilBrownfd01b882011-10-11 16:47:53 +11006691stripe_cache_active_show(struct mddev *mddev, char *page)
NeilBrown3f294f42005-11-08 21:39:25 -08006692{
NeilBrownd1688a62011-10-11 16:49:52 +11006693 struct r5conf *conf = mddev->private;
NeilBrown96de1e62005-11-08 21:39:39 -08006694 if (conf)
6695 return sprintf(page, "%d\n", atomic_read(&conf->active_stripes));
6696 else
6697 return 0;
NeilBrown3f294f42005-11-08 21:39:25 -08006698}
6699
NeilBrown96de1e62005-11-08 21:39:39 -08006700static struct md_sysfs_entry
6701raid5_stripecache_active = __ATTR_RO(stripe_cache_active);
NeilBrown3f294f42005-11-08 21:39:25 -08006702
Shaohua Lib7214202013-08-27 17:50:42 +08006703static ssize_t
6704raid5_show_group_thread_cnt(struct mddev *mddev, char *page)
6705{
NeilBrown7b1485b2014-12-15 12:56:59 +11006706 struct r5conf *conf;
6707 int ret = 0;
6708 spin_lock(&mddev->lock);
6709 conf = mddev->private;
Shaohua Lib7214202013-08-27 17:50:42 +08006710 if (conf)
NeilBrown7b1485b2014-12-15 12:56:59 +11006711 ret = sprintf(page, "%d\n", conf->worker_cnt_per_group);
6712 spin_unlock(&mddev->lock);
6713 return ret;
Shaohua Lib7214202013-08-27 17:50:42 +08006714}
6715
majianpeng60aaf932013-11-14 15:16:20 +11006716static int alloc_thread_groups(struct r5conf *conf, int cnt,
6717 int *group_cnt,
majianpeng60aaf932013-11-14 15:16:20 +11006718 struct r5worker_group **worker_groups);
Shaohua Lib7214202013-08-27 17:50:42 +08006719static ssize_t
6720raid5_store_group_thread_cnt(struct mddev *mddev, const char *page, size_t len)
6721{
NeilBrown67918752014-12-15 12:57:01 +11006722 struct r5conf *conf;
Shaohua Li7d5d7b52017-09-21 11:03:52 -07006723 unsigned int new;
Shaohua Lib7214202013-08-27 17:50:42 +08006724 int err;
majianpeng60aaf932013-11-14 15:16:20 +11006725 struct r5worker_group *new_groups, *old_groups;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006726 int group_cnt;
Shaohua Lib7214202013-08-27 17:50:42 +08006727
6728 if (len >= PAGE_SIZE)
6729 return -EINVAL;
Shaohua Li7d5d7b52017-09-21 11:03:52 -07006730 if (kstrtouint(page, 10, &new))
6731 return -EINVAL;
6732 /* 8192 should be big enough */
6733 if (new > 8192)
Shaohua Lib7214202013-08-27 17:50:42 +08006734 return -EINVAL;
6735
NeilBrown67918752014-12-15 12:57:01 +11006736 err = mddev_lock(mddev);
Shaohua Lib7214202013-08-27 17:50:42 +08006737 if (err)
6738 return err;
NeilBrown67918752014-12-15 12:57:01 +11006739 conf = mddev->private;
6740 if (!conf)
6741 err = -ENODEV;
6742 else if (new != conf->worker_cnt_per_group) {
6743 mddev_suspend(mddev);
6744
6745 old_groups = conf->worker_groups;
6746 if (old_groups)
6747 flush_workqueue(raid5_wq);
6748
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006749 err = alloc_thread_groups(conf, new, &group_cnt, &new_groups);
NeilBrown67918752014-12-15 12:57:01 +11006750 if (!err) {
6751 spin_lock_irq(&conf->device_lock);
6752 conf->group_cnt = group_cnt;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006753 conf->worker_cnt_per_group = new;
NeilBrown67918752014-12-15 12:57:01 +11006754 conf->worker_groups = new_groups;
6755 spin_unlock_irq(&conf->device_lock);
6756
6757 if (old_groups)
6758 kfree(old_groups[0].workers);
6759 kfree(old_groups);
6760 }
6761 mddev_resume(mddev);
6762 }
6763 mddev_unlock(mddev);
6764
6765 return err ?: len;
Shaohua Lib7214202013-08-27 17:50:42 +08006766}
6767
6768static struct md_sysfs_entry
6769raid5_group_thread_cnt = __ATTR(group_thread_cnt, S_IRUGO | S_IWUSR,
6770 raid5_show_group_thread_cnt,
6771 raid5_store_group_thread_cnt);
6772
NeilBrown007583c2005-11-08 21:39:30 -08006773static struct attribute *raid5_attrs[] = {
NeilBrown3f294f42005-11-08 21:39:25 -08006774 &raid5_stripecache_size.attr,
6775 &raid5_stripecache_active.attr,
Dan Williams8b3e6cd2008-04-28 02:15:53 -07006776 &raid5_preread_bypass_threshold.attr,
Shaohua Lib7214202013-08-27 17:50:42 +08006777 &raid5_group_thread_cnt.attr,
Shaohua Lid592a992014-05-21 17:57:44 +08006778 &raid5_skip_copy.attr,
Markus Stockhausend06f1912014-12-15 12:57:05 +11006779 &raid5_rmw_level.attr,
Yufen Yu3b5408b2020-07-18 05:29:09 -04006780 &raid5_stripe_size.attr,
Song Liu2c7da142016-11-17 15:24:41 -08006781 &r5c_journal_mode.attr,
Mariusz Dabrowskia596d082019-02-18 15:04:09 +01006782 &ppl_write_hint.attr,
NeilBrown3f294f42005-11-08 21:39:25 -08006783 NULL,
6784};
NeilBrown007583c2005-11-08 21:39:30 -08006785static struct attribute_group raid5_attrs_group = {
6786 .name = NULL,
6787 .attrs = raid5_attrs,
NeilBrown3f294f42005-11-08 21:39:25 -08006788};
6789
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006790static int alloc_thread_groups(struct r5conf *conf, int cnt, int *group_cnt,
majianpeng60aaf932013-11-14 15:16:20 +11006791 struct r5worker_group **worker_groups)
Shaohua Li851c30c2013-08-28 14:30:16 +08006792{
Shaohua Li566c09c2013-11-14 15:16:17 +11006793 int i, j, k;
Shaohua Li851c30c2013-08-28 14:30:16 +08006794 ssize_t size;
6795 struct r5worker *workers;
6796
Shaohua Li851c30c2013-08-28 14:30:16 +08006797 if (cnt == 0) {
majianpeng60aaf932013-11-14 15:16:20 +11006798 *group_cnt = 0;
6799 *worker_groups = NULL;
Shaohua Li851c30c2013-08-28 14:30:16 +08006800 return 0;
6801 }
majianpeng60aaf932013-11-14 15:16:20 +11006802 *group_cnt = num_possible_nodes();
Shaohua Li851c30c2013-08-28 14:30:16 +08006803 size = sizeof(struct r5worker) * cnt;
Kees Cook6396bb22018-06-12 14:03:40 -07006804 workers = kcalloc(size, *group_cnt, GFP_NOIO);
6805 *worker_groups = kcalloc(*group_cnt, sizeof(struct r5worker_group),
6806 GFP_NOIO);
majianpeng60aaf932013-11-14 15:16:20 +11006807 if (!*worker_groups || !workers) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006808 kfree(workers);
majianpeng60aaf932013-11-14 15:16:20 +11006809 kfree(*worker_groups);
Shaohua Li851c30c2013-08-28 14:30:16 +08006810 return -ENOMEM;
6811 }
6812
majianpeng60aaf932013-11-14 15:16:20 +11006813 for (i = 0; i < *group_cnt; i++) {
Shaohua Li851c30c2013-08-28 14:30:16 +08006814 struct r5worker_group *group;
6815
NeilBrown0c775d52013-11-25 11:12:43 +11006816 group = &(*worker_groups)[i];
Shaohua Li851c30c2013-08-28 14:30:16 +08006817 INIT_LIST_HEAD(&group->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08006818 INIT_LIST_HEAD(&group->loprio_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08006819 group->conf = conf;
6820 group->workers = workers + i * cnt;
6821
6822 for (j = 0; j < cnt; j++) {
Shaohua Li566c09c2013-11-14 15:16:17 +11006823 struct r5worker *worker = group->workers + j;
6824 worker->group = group;
6825 INIT_WORK(&worker->work, raid5_do_work);
6826
6827 for (k = 0; k < NR_STRIPE_HASH_LOCKS; k++)
6828 INIT_LIST_HEAD(worker->temp_inactive_list + k);
Shaohua Li851c30c2013-08-28 14:30:16 +08006829 }
6830 }
6831
6832 return 0;
6833}
6834
6835static void free_thread_groups(struct r5conf *conf)
6836{
6837 if (conf->worker_groups)
6838 kfree(conf->worker_groups[0].workers);
6839 kfree(conf->worker_groups);
6840 conf->worker_groups = NULL;
6841}
6842
Dan Williams80c3a6c2009-03-17 18:10:40 -07006843static sector_t
NeilBrownfd01b882011-10-11 16:47:53 +11006844raid5_size(struct mddev *mddev, sector_t sectors, int raid_disks)
Dan Williams80c3a6c2009-03-17 18:10:40 -07006845{
NeilBrownd1688a62011-10-11 16:49:52 +11006846 struct r5conf *conf = mddev->private;
Dan Williams80c3a6c2009-03-17 18:10:40 -07006847
6848 if (!sectors)
6849 sectors = mddev->dev_sectors;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006850 if (!raid_disks)
NeilBrown7ec05472009-03-31 15:10:36 +11006851 /* size is defined by the smallest of previous and new size */
NeilBrown5e5e3e72009-10-16 16:35:30 +11006852 raid_disks = min(conf->raid_disks, conf->previous_raid_disks);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006853
NeilBrown3cb5edf2015-07-15 17:24:17 +10006854 sectors &= ~((sector_t)conf->chunk_sectors - 1);
6855 sectors &= ~((sector_t)conf->prev_chunk_sectors - 1);
Dan Williams80c3a6c2009-03-17 18:10:40 -07006856 return sectors * (raid_disks - conf->max_degraded);
6857}
6858
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306859static void free_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6860{
6861 safe_put_page(percpu->spare_page);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306862 percpu->spare_page = NULL;
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006863 kvfree(percpu->scribble);
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306864 percpu->scribble = NULL;
6865}
6866
6867static int alloc_scratch_buffer(struct r5conf *conf, struct raid5_percpu *percpu)
6868{
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006869 if (conf->level == 6 && !percpu->spare_page) {
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306870 percpu->spare_page = alloc_page(GFP_KERNEL);
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006871 if (!percpu->spare_page)
6872 return -ENOMEM;
6873 }
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306874
Kent Overstreetb330e6a2019-03-11 23:31:06 -07006875 if (scribble_alloc(percpu,
6876 max(conf->raid_disks,
6877 conf->previous_raid_disks),
6878 max(conf->chunk_sectors,
6879 conf->prev_chunk_sectors)
Yufen Yuc911c462020-07-18 05:29:07 -04006880 / RAID5_STRIPE_SECTORS(conf))) {
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306881 free_scratch_buffer(conf, percpu);
6882 return -ENOMEM;
6883 }
6884
6885 return 0;
6886}
6887
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006888static int raid456_cpu_dead(unsigned int cpu, struct hlist_node *node)
6889{
6890 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
6891
6892 free_scratch_buffer(conf, per_cpu_ptr(conf->percpu, cpu));
6893 return 0;
6894}
6895
NeilBrownd1688a62011-10-11 16:49:52 +11006896static void raid5_free_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006897{
Dan Williams36d1c642009-07-14 11:48:22 -07006898 if (!conf->percpu)
6899 return;
6900
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006901 cpuhp_state_remove_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Dan Williams36d1c642009-07-14 11:48:22 -07006902 free_percpu(conf->percpu);
6903}
6904
NeilBrownd1688a62011-10-11 16:49:52 +11006905static void free_conf(struct r5conf *conf)
Dan Williams95fc17a2009-07-31 12:39:15 +10006906{
Song Liud7bd3982016-11-23 22:50:39 -08006907 int i;
6908
Artur Paszkiewiczff875732017-03-09 09:59:58 +01006909 log_exit(conf);
6910
Aliaksei Karaliou565e0452017-12-23 21:20:31 +03006911 unregister_shrinker(&conf->shrinker);
Shaohua Li851c30c2013-08-28 14:30:16 +08006912 free_thread_groups(conf);
Dan Williams95fc17a2009-07-31 12:39:15 +10006913 shrink_stripes(conf);
Dan Williams36d1c642009-07-14 11:48:22 -07006914 raid5_free_percpu(conf);
Song Liud7bd3982016-11-23 22:50:39 -08006915 for (i = 0; i < conf->pool_size; i++)
6916 if (conf->disks[i].extra_page)
6917 put_page(conf->disks[i].extra_page);
Dan Williams95fc17a2009-07-31 12:39:15 +10006918 kfree(conf->disks);
Kent Overstreetafeee512018-05-20 18:25:52 -04006919 bioset_exit(&conf->bio_split);
Dan Williams95fc17a2009-07-31 12:39:15 +10006920 kfree(conf->stripe_hashtbl);
Shaohua Liaaf9f122017-03-03 22:06:12 -08006921 kfree(conf->pending_data);
Dan Williams95fc17a2009-07-31 12:39:15 +10006922 kfree(conf);
6923}
6924
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006925static int raid456_cpu_up_prepare(unsigned int cpu, struct hlist_node *node)
Dan Williams36d1c642009-07-14 11:48:22 -07006926{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006927 struct r5conf *conf = hlist_entry_safe(node, struct r5conf, node);
Dan Williams36d1c642009-07-14 11:48:22 -07006928 struct raid5_percpu *percpu = per_cpu_ptr(conf->percpu, cpu);
6929
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006930 if (alloc_scratch_buffer(conf, percpu)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11006931 pr_warn("%s: failed memory allocation for cpu%u\n",
6932 __func__, cpu);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006933 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006934 }
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006935 return 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006936}
Dan Williams36d1c642009-07-14 11:48:22 -07006937
NeilBrownd1688a62011-10-11 16:49:52 +11006938static int raid5_alloc_percpu(struct r5conf *conf)
Dan Williams36d1c642009-07-14 11:48:22 -07006939{
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306940 int err = 0;
Dan Williams36d1c642009-07-14 11:48:22 -07006941
Oleg Nesterov789b5e02014-02-06 03:42:45 +05306942 conf->percpu = alloc_percpu(struct raid5_percpu);
6943 if (!conf->percpu)
Dan Williams36d1c642009-07-14 11:48:22 -07006944 return -ENOMEM;
Dan Williams36d1c642009-07-14 11:48:22 -07006945
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02006946 err = cpuhp_state_add_instance(CPUHP_MD_RAID5_PREPARE, &conf->node);
Shaohua Li27a353c2016-02-24 17:38:28 -08006947 if (!err) {
6948 conf->scribble_disks = max(conf->raid_disks,
6949 conf->previous_raid_disks);
6950 conf->scribble_sectors = max(conf->chunk_sectors,
6951 conf->prev_chunk_sectors);
6952 }
Dan Williams36d1c642009-07-14 11:48:22 -07006953 return err;
6954}
6955
NeilBrownedbe83a2015-02-26 12:47:56 +11006956static unsigned long raid5_cache_scan(struct shrinker *shrink,
6957 struct shrink_control *sc)
6958{
6959 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
NeilBrown2d5b5692015-07-06 12:49:23 +10006960 unsigned long ret = SHRINK_STOP;
6961
6962 if (mutex_trylock(&conf->cache_size_mutex)) {
6963 ret= 0;
NeilBrown49895bc2015-08-03 17:09:57 +10006964 while (ret < sc->nr_to_scan &&
6965 conf->max_nr_stripes > conf->min_nr_stripes) {
NeilBrown2d5b5692015-07-06 12:49:23 +10006966 if (drop_one_stripe(conf) == 0) {
6967 ret = SHRINK_STOP;
6968 break;
6969 }
6970 ret++;
6971 }
6972 mutex_unlock(&conf->cache_size_mutex);
NeilBrownedbe83a2015-02-26 12:47:56 +11006973 }
6974 return ret;
6975}
6976
6977static unsigned long raid5_cache_count(struct shrinker *shrink,
6978 struct shrink_control *sc)
6979{
6980 struct r5conf *conf = container_of(shrink, struct r5conf, shrinker);
6981
6982 if (conf->max_nr_stripes < conf->min_nr_stripes)
6983 /* unlikely, but not impossible */
6984 return 0;
6985 return conf->max_nr_stripes - conf->min_nr_stripes;
6986}
6987
NeilBrownd1688a62011-10-11 16:49:52 +11006988static struct r5conf *setup_conf(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006989{
NeilBrownd1688a62011-10-11 16:49:52 +11006990 struct r5conf *conf;
NeilBrown5e5e3e72009-10-16 16:35:30 +11006991 int raid_disk, memory, max_disks;
NeilBrown3cb03002011-10-11 16:45:26 +11006992 struct md_rdev *rdev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006993 struct disk_info *disk;
NeilBrown02326052012-07-03 15:56:52 +10006994 char pers_name[6];
Shaohua Li566c09c2013-11-14 15:16:17 +11006995 int i;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01006996 int group_cnt;
majianpeng60aaf932013-11-14 15:16:20 +11006997 struct r5worker_group *new_group;
Kent Overstreetafeee512018-05-20 18:25:52 -04006998 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006999
NeilBrown91adb562009-03-31 14:39:39 +11007000 if (mddev->new_level != 5
7001 && mddev->new_level != 4
7002 && mddev->new_level != 6) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007003 pr_warn("md/raid:%s: raid level not set to 4/5/6 (%d)\n",
7004 mdname(mddev), mddev->new_level);
NeilBrown91adb562009-03-31 14:39:39 +11007005 return ERR_PTR(-EIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007006 }
NeilBrown91adb562009-03-31 14:39:39 +11007007 if ((mddev->new_level == 5
7008 && !algorithm_valid_raid5(mddev->new_layout)) ||
7009 (mddev->new_level == 6
7010 && !algorithm_valid_raid6(mddev->new_layout))) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007011 pr_warn("md/raid:%s: layout %d not supported\n",
7012 mdname(mddev), mddev->new_layout);
NeilBrown91adb562009-03-31 14:39:39 +11007013 return ERR_PTR(-EIO);
7014 }
7015 if (mddev->new_level == 6 && mddev->raid_disks < 4) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007016 pr_warn("md/raid:%s: not enough configured devices (%d, minimum 4)\n",
7017 mdname(mddev), mddev->raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11007018 return ERR_PTR(-EINVAL);
NeilBrown99c0fb52009-03-31 14:39:38 +11007019 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007020
Andre Noll664e7c42009-06-18 08:45:27 +10007021 if (!mddev->new_chunk_sectors ||
7022 (mddev->new_chunk_sectors << 9) % PAGE_SIZE ||
7023 !is_power_of_2(mddev->new_chunk_sectors)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007024 pr_warn("md/raid:%s: invalid chunk size %d\n",
7025 mdname(mddev), mddev->new_chunk_sectors << 9);
NeilBrown91adb562009-03-31 14:39:39 +11007026 return ERR_PTR(-EINVAL);
NeilBrown4bbf3772008-10-13 11:55:12 +11007027 }
7028
NeilBrownd1688a62011-10-11 16:49:52 +11007029 conf = kzalloc(sizeof(struct r5conf), GFP_KERNEL);
NeilBrown91adb562009-03-31 14:39:39 +11007030 if (conf == NULL)
7031 goto abort;
Yufen Yuc911c462020-07-18 05:29:07 -04007032
Yufen Yue2368582020-07-18 05:29:08 -04007033#if PAGE_SIZE != DEFAULT_STRIPE_SIZE
7034 conf->stripe_size = DEFAULT_STRIPE_SIZE;
7035 conf->stripe_shift = ilog2(DEFAULT_STRIPE_SIZE) - 9;
7036 conf->stripe_sectors = DEFAULT_STRIPE_SIZE >> 9;
7037#endif
Shaohua Liaaf9f122017-03-03 22:06:12 -08007038 INIT_LIST_HEAD(&conf->free_list);
7039 INIT_LIST_HEAD(&conf->pending_list);
Kees Cook6396bb22018-06-12 14:03:40 -07007040 conf->pending_data = kcalloc(PENDING_IO_MAX,
7041 sizeof(struct r5pending_data),
7042 GFP_KERNEL);
Shaohua Liaaf9f122017-03-03 22:06:12 -08007043 if (!conf->pending_data)
7044 goto abort;
7045 for (i = 0; i < PENDING_IO_MAX; i++)
7046 list_add(&conf->pending_data[i].sibling, &conf->free_list);
Shaohua Li851c30c2013-08-28 14:30:16 +08007047 /* Don't enable multi-threading by default*/
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01007048 if (!alloc_thread_groups(conf, 0, &group_cnt, &new_group)) {
majianpeng60aaf932013-11-14 15:16:20 +11007049 conf->group_cnt = group_cnt;
Guoqing Jiangd2c9ad42019-12-20 15:46:29 +01007050 conf->worker_cnt_per_group = 0;
majianpeng60aaf932013-11-14 15:16:20 +11007051 conf->worker_groups = new_group;
7052 } else
Shaohua Li851c30c2013-08-28 14:30:16 +08007053 goto abort;
Dan Williamsf5efd452009-10-16 15:55:38 +11007054 spin_lock_init(&conf->device_lock);
Ahmed S. Darwish0a87b252020-07-20 17:55:25 +02007055 seqcount_spinlock_init(&conf->gen_lock, &conf->device_lock);
NeilBrown2d5b5692015-07-06 12:49:23 +10007056 mutex_init(&conf->cache_size_mutex);
Yuanhan Liub1b46482015-05-08 18:19:06 +10007057 init_waitqueue_head(&conf->wait_for_quiescent);
Shaohua Li6ab2a4b2016-02-25 16:24:42 -08007058 init_waitqueue_head(&conf->wait_for_stripe);
Dan Williamsf5efd452009-10-16 15:55:38 +11007059 init_waitqueue_head(&conf->wait_for_overlap);
7060 INIT_LIST_HEAD(&conf->handle_list);
Shaohua Li535ae4e2017-02-15 19:37:32 -08007061 INIT_LIST_HEAD(&conf->loprio_list);
Dan Williamsf5efd452009-10-16 15:55:38 +11007062 INIT_LIST_HEAD(&conf->hold_list);
7063 INIT_LIST_HEAD(&conf->delayed_list);
7064 INIT_LIST_HEAD(&conf->bitmap_list);
Shaohua Li773ca822013-08-27 17:50:39 +08007065 init_llist_head(&conf->released_stripes);
Dan Williamsf5efd452009-10-16 15:55:38 +11007066 atomic_set(&conf->active_stripes, 0);
7067 atomic_set(&conf->preread_active_stripes, 0);
7068 atomic_set(&conf->active_aligned_reads, 0);
Shaohua Li765d7042017-01-04 09:33:23 -08007069 spin_lock_init(&conf->pending_bios_lock);
7070 conf->batch_bio_dispatch = true;
7071 rdev_for_each(rdev, mddev) {
7072 if (test_bit(Journal, &rdev->flags))
7073 continue;
7074 if (blk_queue_nonrot(bdev_get_queue(rdev->bdev))) {
7075 conf->batch_bio_dispatch = false;
7076 break;
7077 }
7078 }
7079
Dan Williamsf5efd452009-10-16 15:55:38 +11007080 conf->bypass_threshold = BYPASS_THRESHOLD;
NeilBrownd890fa22011-10-26 11:54:39 +11007081 conf->recovery_disabled = mddev->recovery_disabled - 1;
NeilBrown91adb562009-03-31 14:39:39 +11007082
7083 conf->raid_disks = mddev->raid_disks;
7084 if (mddev->reshape_position == MaxSector)
7085 conf->previous_raid_disks = mddev->raid_disks;
7086 else
7087 conf->previous_raid_disks = mddev->raid_disks - mddev->delta_disks;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007088 max_disks = max(conf->raid_disks, conf->previous_raid_disks);
NeilBrown91adb562009-03-31 14:39:39 +11007089
Kees Cook6396bb22018-06-12 14:03:40 -07007090 conf->disks = kcalloc(max_disks, sizeof(struct disk_info),
NeilBrown91adb562009-03-31 14:39:39 +11007091 GFP_KERNEL);
Song Liud7bd3982016-11-23 22:50:39 -08007092
NeilBrown91adb562009-03-31 14:39:39 +11007093 if (!conf->disks)
7094 goto abort;
7095
Song Liud7bd3982016-11-23 22:50:39 -08007096 for (i = 0; i < max_disks; i++) {
7097 conf->disks[i].extra_page = alloc_page(GFP_KERNEL);
7098 if (!conf->disks[i].extra_page)
7099 goto abort;
7100 }
7101
Kent Overstreetafeee512018-05-20 18:25:52 -04007102 ret = bioset_init(&conf->bio_split, BIO_POOL_SIZE, 0, 0);
7103 if (ret)
NeilBrowndd7a8f52017-04-05 14:05:51 +10007104 goto abort;
NeilBrown91adb562009-03-31 14:39:39 +11007105 conf->mddev = mddev;
7106
7107 if ((conf->stripe_hashtbl = kzalloc(PAGE_SIZE, GFP_KERNEL)) == NULL)
7108 goto abort;
7109
Shaohua Li566c09c2013-11-14 15:16:17 +11007110 /* We init hash_locks[0] separately to that it can be used
7111 * as the reference lock in the spin_lock_nest_lock() call
7112 * in lock_all_device_hash_locks_irq in order to convince
7113 * lockdep that we know what we are doing.
7114 */
7115 spin_lock_init(conf->hash_locks);
7116 for (i = 1; i < NR_STRIPE_HASH_LOCKS; i++)
7117 spin_lock_init(conf->hash_locks + i);
7118
7119 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7120 INIT_LIST_HEAD(conf->inactive_list + i);
7121
7122 for (i = 0; i < NR_STRIPE_HASH_LOCKS; i++)
7123 INIT_LIST_HEAD(conf->temp_inactive_list + i);
7124
Song Liu1e6d6902016-11-17 15:24:39 -08007125 atomic_set(&conf->r5c_cached_full_stripes, 0);
7126 INIT_LIST_HEAD(&conf->r5c_full_stripe_list);
7127 atomic_set(&conf->r5c_cached_partial_stripes, 0);
7128 INIT_LIST_HEAD(&conf->r5c_partial_stripe_list);
Shaohua Lie33fbb92017-02-10 16:18:09 -08007129 atomic_set(&conf->r5c_flushing_full_stripes, 0);
7130 atomic_set(&conf->r5c_flushing_partial_stripes, 0);
Song Liu1e6d6902016-11-17 15:24:39 -08007131
Dan Williams36d1c642009-07-14 11:48:22 -07007132 conf->level = mddev->new_level;
shli@kernel.org46d5b782014-12-15 12:57:02 +11007133 conf->chunk_sectors = mddev->new_chunk_sectors;
Dan Williams36d1c642009-07-14 11:48:22 -07007134 if (raid5_alloc_percpu(conf) != 0)
7135 goto abort;
7136
NeilBrown0c55e022010-05-03 14:09:02 +10007137 pr_debug("raid456: run(%s) called.\n", mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007138
NeilBrowndafb20f2012-03-19 12:46:39 +11007139 rdev_for_each(rdev, mddev) {
NeilBrown91adb562009-03-31 14:39:39 +11007140 raid_disk = rdev->raid_disk;
NeilBrown5e5e3e72009-10-16 16:35:30 +11007141 if (raid_disk >= max_disks
Shaohua Lif2076e72015-10-08 21:54:12 -07007142 || raid_disk < 0 || test_bit(Journal, &rdev->flags))
NeilBrown91adb562009-03-31 14:39:39 +11007143 continue;
7144 disk = conf->disks + raid_disk;
7145
NeilBrown17045f52011-12-23 10:17:53 +11007146 if (test_bit(Replacement, &rdev->flags)) {
7147 if (disk->replacement)
7148 goto abort;
7149 disk->replacement = rdev;
7150 } else {
7151 if (disk->rdev)
7152 goto abort;
7153 disk->rdev = rdev;
7154 }
NeilBrown91adb562009-03-31 14:39:39 +11007155
7156 if (test_bit(In_sync, &rdev->flags)) {
7157 char b[BDEVNAME_SIZE];
NeilBrowncc6167b2016-11-02 14:16:50 +11007158 pr_info("md/raid:%s: device %s operational as raid disk %d\n",
7159 mdname(mddev), bdevname(rdev->bdev, b), raid_disk);
Jonathan Brassowd6b212f2011-06-08 18:00:28 -05007160 } else if (rdev->saved_raid_disk != raid_disk)
NeilBrown91adb562009-03-31 14:39:39 +11007161 /* Cannot rely on bitmap to complete recovery */
7162 conf->fullsync = 1;
7163 }
7164
NeilBrown91adb562009-03-31 14:39:39 +11007165 conf->level = mddev->new_level;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007166 if (conf->level == 6) {
NeilBrown91adb562009-03-31 14:39:39 +11007167 conf->max_degraded = 2;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007168 if (raid6_call.xor_syndrome)
7169 conf->rmw_level = PARITY_ENABLE_RMW;
7170 else
7171 conf->rmw_level = PARITY_DISABLE_RMW;
7172 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007173 conf->max_degraded = 1;
Markus Stockhausen584acdd2014-12-15 12:57:05 +11007174 conf->rmw_level = PARITY_ENABLE_RMW;
7175 }
NeilBrown91adb562009-03-31 14:39:39 +11007176 conf->algorithm = mddev->new_layout;
NeilBrownfef9c612009-03-31 15:16:46 +11007177 conf->reshape_progress = mddev->reshape_position;
NeilBrowne183eae2009-03-31 15:20:22 +11007178 if (conf->reshape_progress != MaxSector) {
Andre Noll09c9e5f2009-06-18 08:45:55 +10007179 conf->prev_chunk_sectors = mddev->chunk_sectors;
NeilBrowne183eae2009-03-31 15:20:22 +11007180 conf->prev_algo = mddev->layout;
NeilBrown5cac6bc2015-07-17 12:17:50 +10007181 } else {
7182 conf->prev_chunk_sectors = conf->chunk_sectors;
7183 conf->prev_algo = conf->algorithm;
NeilBrowne183eae2009-03-31 15:20:22 +11007184 }
NeilBrown91adb562009-03-31 14:39:39 +11007185
NeilBrownedbe83a2015-02-26 12:47:56 +11007186 conf->min_nr_stripes = NR_STRIPES;
Shaohua Liad5b0f72016-08-30 10:29:33 -07007187 if (mddev->reshape_position != MaxSector) {
7188 int stripes = max_t(int,
Yufen Yuc911c462020-07-18 05:29:07 -04007189 ((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4,
7190 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4);
Shaohua Liad5b0f72016-08-30 10:29:33 -07007191 conf->min_nr_stripes = max(NR_STRIPES, stripes);
7192 if (conf->min_nr_stripes != NR_STRIPES)
NeilBrowncc6167b2016-11-02 14:16:50 +11007193 pr_info("md/raid:%s: force stripe size %d for reshape\n",
Shaohua Liad5b0f72016-08-30 10:29:33 -07007194 mdname(mddev), conf->min_nr_stripes);
7195 }
NeilBrownedbe83a2015-02-26 12:47:56 +11007196 memory = conf->min_nr_stripes * (sizeof(struct stripe_head) +
NeilBrown5e5e3e72009-10-16 16:35:30 +11007197 max_disks * ((sizeof(struct bio) + PAGE_SIZE))) / 1024;
Shaohua Li4bda5562013-11-14 15:16:17 +11007198 atomic_set(&conf->empty_inactive_list_nr, NR_STRIPE_HASH_LOCKS);
NeilBrownedbe83a2015-02-26 12:47:56 +11007199 if (grow_stripes(conf, conf->min_nr_stripes)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007200 pr_warn("md/raid:%s: couldn't allocate %dkB for buffers\n",
7201 mdname(mddev), memory);
NeilBrown91adb562009-03-31 14:39:39 +11007202 goto abort;
7203 } else
NeilBrowncc6167b2016-11-02 14:16:50 +11007204 pr_debug("md/raid:%s: allocated %dkB\n", mdname(mddev), memory);
NeilBrownedbe83a2015-02-26 12:47:56 +11007205 /*
7206 * Losing a stripe head costs more than the time to refill it,
7207 * it reduces the queue depth and so can hurt throughput.
7208 * So set it rather large, scaled by number of devices.
7209 */
7210 conf->shrinker.seeks = DEFAULT_SEEKS * conf->raid_disks * 4;
7211 conf->shrinker.scan_objects = raid5_cache_scan;
7212 conf->shrinker.count_objects = raid5_cache_count;
7213 conf->shrinker.batch = 128;
7214 conf->shrinker.flags = 0;
Chao Yu6a0f53f2016-09-20 10:33:57 +08007215 if (register_shrinker(&conf->shrinker)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007216 pr_warn("md/raid:%s: couldn't register shrinker.\n",
7217 mdname(mddev));
Chao Yu6a0f53f2016-09-20 10:33:57 +08007218 goto abort;
7219 }
NeilBrown91adb562009-03-31 14:39:39 +11007220
NeilBrown02326052012-07-03 15:56:52 +10007221 sprintf(pers_name, "raid%d", mddev->new_level);
7222 conf->thread = md_register_thread(raid5d, mddev, pers_name);
NeilBrown91adb562009-03-31 14:39:39 +11007223 if (!conf->thread) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007224 pr_warn("md/raid:%s: couldn't allocate thread.\n",
7225 mdname(mddev));
NeilBrown91adb562009-03-31 14:39:39 +11007226 goto abort;
7227 }
7228
7229 return conf;
7230
7231 abort:
7232 if (conf) {
Dan Williams95fc17a2009-07-31 12:39:15 +10007233 free_conf(conf);
NeilBrown91adb562009-03-31 14:39:39 +11007234 return ERR_PTR(-EIO);
7235 } else
7236 return ERR_PTR(-ENOMEM);
7237}
7238
NeilBrownc148ffd2009-11-13 17:47:00 +11007239static int only_parity(int raid_disk, int algo, int raid_disks, int max_degraded)
7240{
7241 switch (algo) {
7242 case ALGORITHM_PARITY_0:
7243 if (raid_disk < max_degraded)
7244 return 1;
7245 break;
7246 case ALGORITHM_PARITY_N:
7247 if (raid_disk >= raid_disks - max_degraded)
7248 return 1;
7249 break;
7250 case ALGORITHM_PARITY_0_6:
NeilBrownf72ffdd2014-09-30 14:23:59 +10007251 if (raid_disk == 0 ||
NeilBrownc148ffd2009-11-13 17:47:00 +11007252 raid_disk == raid_disks - 1)
7253 return 1;
7254 break;
7255 case ALGORITHM_LEFT_ASYMMETRIC_6:
7256 case ALGORITHM_RIGHT_ASYMMETRIC_6:
7257 case ALGORITHM_LEFT_SYMMETRIC_6:
7258 case ALGORITHM_RIGHT_SYMMETRIC_6:
7259 if (raid_disk == raid_disks - 1)
7260 return 1;
7261 }
7262 return 0;
7263}
7264
Christoph Hellwig16ef5102020-09-24 08:51:33 +02007265static void raid5_set_io_opt(struct r5conf *conf)
7266{
7267 blk_queue_io_opt(conf->mddev->queue, (conf->chunk_sectors << 9) *
7268 (conf->raid_disks - conf->max_degraded));
7269}
7270
Shaohua Li849674e2016-01-20 13:52:20 -08007271static int raid5_run(struct mddev *mddev)
NeilBrown91adb562009-03-31 14:39:39 +11007272{
NeilBrownd1688a62011-10-11 16:49:52 +11007273 struct r5conf *conf;
NeilBrown9f7c2222010-07-26 12:04:13 +10007274 int working_disks = 0;
NeilBrownc148ffd2009-11-13 17:47:00 +11007275 int dirty_parity_disks = 0;
NeilBrown3cb03002011-10-11 16:45:26 +11007276 struct md_rdev *rdev;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007277 struct md_rdev *journal_dev = NULL;
NeilBrownc148ffd2009-11-13 17:47:00 +11007278 sector_t reshape_offset = 0;
NeilBrown17045f52011-12-23 10:17:53 +11007279 int i;
NeilBrownb5254dd2012-05-21 09:27:01 +10007280 long long min_offset_diff = 0;
7281 int first = 1;
NeilBrown91adb562009-03-31 14:39:39 +11007282
NeilBrowna415c0f2017-06-05 16:05:13 +10007283 if (mddev_init_writes_pending(mddev) < 0)
7284 return -ENOMEM;
7285
Andre Noll8c6ac8682009-06-18 08:48:06 +10007286 if (mddev->recovery_cp != MaxSector)
NeilBrowncc6167b2016-11-02 14:16:50 +11007287 pr_notice("md/raid:%s: not clean -- starting background reconstruction\n",
7288 mdname(mddev));
NeilBrownb5254dd2012-05-21 09:27:01 +10007289
7290 rdev_for_each(rdev, mddev) {
7291 long long diff;
Shaohua Li713cf5a2015-08-13 14:32:03 -07007292
Shaohua Lif2076e72015-10-08 21:54:12 -07007293 if (test_bit(Journal, &rdev->flags)) {
Shaohua Li713cf5a2015-08-13 14:32:03 -07007294 journal_dev = rdev;
Shaohua Lif2076e72015-10-08 21:54:12 -07007295 continue;
7296 }
NeilBrownb5254dd2012-05-21 09:27:01 +10007297 if (rdev->raid_disk < 0)
7298 continue;
7299 diff = (rdev->new_data_offset - rdev->data_offset);
7300 if (first) {
7301 min_offset_diff = diff;
7302 first = 0;
7303 } else if (mddev->reshape_backwards &&
7304 diff < min_offset_diff)
7305 min_offset_diff = diff;
7306 else if (!mddev->reshape_backwards &&
7307 diff > min_offset_diff)
7308 min_offset_diff = diff;
7309 }
7310
NeilBrown230b55f2017-10-17 14:24:09 +11007311 if ((test_bit(MD_HAS_JOURNAL, &mddev->flags) || journal_dev) &&
7312 (mddev->bitmap_info.offset || mddev->bitmap_info.file)) {
7313 pr_notice("md/raid:%s: array cannot have both journal and bitmap\n",
7314 mdname(mddev));
7315 return -EINVAL;
7316 }
7317
NeilBrownf6705572006-03-27 01:18:11 -08007318 if (mddev->reshape_position != MaxSector) {
7319 /* Check that we can continue the reshape.
NeilBrownb5254dd2012-05-21 09:27:01 +10007320 * Difficulties arise if the stripe we would write to
7321 * next is at or after the stripe we would read from next.
7322 * For a reshape that changes the number of devices, this
7323 * is only possible for a very short time, and mdadm makes
7324 * sure that time appears to have past before assembling
7325 * the array. So we fail if that time hasn't passed.
7326 * For a reshape that keeps the number of devices the same
7327 * mdadm must be monitoring the reshape can keeping the
7328 * critical areas read-only and backed up. It will start
7329 * the array in read-only mode, so we check for that.
NeilBrownf6705572006-03-27 01:18:11 -08007330 */
7331 sector_t here_new, here_old;
7332 int old_disks;
Andre Noll18b00332009-03-31 15:00:56 +11007333 int max_degraded = (mddev->level == 6 ? 2 : 1);
NeilBrown05256d92015-07-15 17:36:21 +10007334 int chunk_sectors;
7335 int new_data_disks;
NeilBrownf6705572006-03-27 01:18:11 -08007336
Shaohua Li713cf5a2015-08-13 14:32:03 -07007337 if (journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007338 pr_warn("md/raid:%s: don't support reshape with journal - aborting.\n",
7339 mdname(mddev));
Shaohua Li713cf5a2015-08-13 14:32:03 -07007340 return -EINVAL;
7341 }
7342
NeilBrown88ce4932009-03-31 15:24:23 +11007343 if (mddev->new_level != mddev->level) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007344 pr_warn("md/raid:%s: unsupported reshape required - aborting.\n",
7345 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007346 return -EINVAL;
7347 }
NeilBrownf6705572006-03-27 01:18:11 -08007348 old_disks = mddev->raid_disks - mddev->delta_disks;
7349 /* reshape_position must be on a new-stripe boundary, and one
NeilBrownf4168852007-02-28 20:11:53 -08007350 * further up in new geometry must map after here in old
7351 * geometry.
NeilBrown05256d92015-07-15 17:36:21 +10007352 * If the chunk sizes are different, then as we perform reshape
7353 * in units of the largest of the two, reshape_position needs
7354 * be a multiple of the largest chunk size times new data disks.
NeilBrownf6705572006-03-27 01:18:11 -08007355 */
7356 here_new = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007357 chunk_sectors = max(mddev->chunk_sectors, mddev->new_chunk_sectors);
7358 new_data_disks = mddev->raid_disks - max_degraded;
7359 if (sector_div(here_new, chunk_sectors * new_data_disks)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007360 pr_warn("md/raid:%s: reshape_position not on a stripe boundary\n",
7361 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007362 return -EINVAL;
7363 }
NeilBrown05256d92015-07-15 17:36:21 +10007364 reshape_offset = here_new * chunk_sectors;
NeilBrownf6705572006-03-27 01:18:11 -08007365 /* here_new is the stripe we will write to */
7366 here_old = mddev->reshape_position;
NeilBrown05256d92015-07-15 17:36:21 +10007367 sector_div(here_old, chunk_sectors * (old_disks-max_degraded));
NeilBrownf4168852007-02-28 20:11:53 -08007368 /* here_old is the first stripe that we might need to read
7369 * from */
NeilBrown67ac6012009-08-13 10:06:24 +10007370 if (mddev->delta_disks == 0) {
7371 /* We cannot be sure it is safe to start an in-place
NeilBrownb5254dd2012-05-21 09:27:01 +10007372 * reshape. It is only safe if user-space is monitoring
NeilBrown67ac6012009-08-13 10:06:24 +10007373 * and taking constant backups.
7374 * mdadm always starts a situation like this in
7375 * readonly mode so it can take control before
7376 * allowing any writes. So just check for that.
7377 */
NeilBrownb5254dd2012-05-21 09:27:01 +10007378 if (abs(min_offset_diff) >= mddev->chunk_sectors &&
7379 abs(min_offset_diff) >= mddev->new_chunk_sectors)
7380 /* not really in-place - so OK */;
7381 else if (mddev->ro == 0) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007382 pr_warn("md/raid:%s: in-place reshape must be started in read-only mode - aborting\n",
7383 mdname(mddev));
NeilBrown67ac6012009-08-13 10:06:24 +10007384 return -EINVAL;
7385 }
NeilBrown2c810cd2012-05-21 09:27:00 +10007386 } else if (mddev->reshape_backwards
NeilBrown05256d92015-07-15 17:36:21 +10007387 ? (here_new * chunk_sectors + min_offset_diff <=
7388 here_old * chunk_sectors)
7389 : (here_new * chunk_sectors >=
7390 here_old * chunk_sectors + (-min_offset_diff))) {
NeilBrownf6705572006-03-27 01:18:11 -08007391 /* Reading from the same stripe as writing to - bad */
NeilBrowncc6167b2016-11-02 14:16:50 +11007392 pr_warn("md/raid:%s: reshape_position too early for auto-recovery - aborting.\n",
7393 mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007394 return -EINVAL;
7395 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007396 pr_debug("md/raid:%s: reshape will continue\n", mdname(mddev));
NeilBrownf6705572006-03-27 01:18:11 -08007397 /* OK, we should be able to continue; */
NeilBrownf6705572006-03-27 01:18:11 -08007398 } else {
NeilBrown91adb562009-03-31 14:39:39 +11007399 BUG_ON(mddev->level != mddev->new_level);
7400 BUG_ON(mddev->layout != mddev->new_layout);
Andre Noll664e7c42009-06-18 08:45:27 +10007401 BUG_ON(mddev->chunk_sectors != mddev->new_chunk_sectors);
NeilBrown91adb562009-03-31 14:39:39 +11007402 BUG_ON(mddev->delta_disks != 0);
NeilBrownf6705572006-03-27 01:18:11 -08007403 }
7404
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007405 if (test_bit(MD_HAS_JOURNAL, &mddev->flags) &&
7406 test_bit(MD_HAS_PPL, &mddev->flags)) {
7407 pr_warn("md/raid:%s: using journal device and PPL not allowed - disabling PPL\n",
7408 mdname(mddev));
7409 clear_bit(MD_HAS_PPL, &mddev->flags);
Pawel Baldysiakddc08822017-08-16 17:13:45 +02007410 clear_bit(MD_HAS_MULTIPLE_PPLS, &mddev->flags);
Artur Paszkiewicz3418d032017-03-09 09:59:59 +01007411 }
7412
NeilBrown245f46c2009-03-31 14:39:39 +11007413 if (mddev->private == NULL)
7414 conf = setup_conf(mddev);
7415 else
7416 conf = mddev->private;
7417
NeilBrown91adb562009-03-31 14:39:39 +11007418 if (IS_ERR(conf))
7419 return PTR_ERR(conf);
NeilBrown9ffae0c2006-01-06 00:20:32 -08007420
Song Liu486b0f72016-08-19 15:34:01 -07007421 if (test_bit(MD_HAS_JOURNAL, &mddev->flags)) {
7422 if (!journal_dev) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007423 pr_warn("md/raid:%s: journal disk is missing, force array readonly\n",
7424 mdname(mddev));
Song Liu486b0f72016-08-19 15:34:01 -07007425 mddev->ro = 1;
7426 set_disk_ro(mddev->gendisk, 1);
7427 } else if (mddev->recovery_cp == MaxSector)
7428 set_bit(MD_JOURNAL_CLEAN, &mddev->flags);
Shaohua Li7dde2ad2015-10-08 21:54:10 -07007429 }
7430
NeilBrownb5254dd2012-05-21 09:27:01 +10007431 conf->min_offset_diff = min_offset_diff;
NeilBrown91adb562009-03-31 14:39:39 +11007432 mddev->thread = conf->thread;
7433 conf->thread = NULL;
7434 mddev->private = conf;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007435
NeilBrown17045f52011-12-23 10:17:53 +11007436 for (i = 0; i < conf->raid_disks && conf->previous_raid_disks;
7437 i++) {
7438 rdev = conf->disks[i].rdev;
7439 if (!rdev && conf->disks[i].replacement) {
7440 /* The replacement is all we have yet */
7441 rdev = conf->disks[i].replacement;
7442 conf->disks[i].replacement = NULL;
7443 clear_bit(Replacement, &rdev->flags);
7444 conf->disks[i].rdev = rdev;
7445 }
7446 if (!rdev)
NeilBrownc148ffd2009-11-13 17:47:00 +11007447 continue;
NeilBrown17045f52011-12-23 10:17:53 +11007448 if (conf->disks[i].replacement &&
7449 conf->reshape_progress != MaxSector) {
7450 /* replacements and reshape simply do not mix. */
NeilBrowncc6167b2016-11-02 14:16:50 +11007451 pr_warn("md: cannot handle concurrent replacement and reshape.\n");
NeilBrown17045f52011-12-23 10:17:53 +11007452 goto abort;
7453 }
NeilBrown2f115882010-06-17 17:41:03 +10007454 if (test_bit(In_sync, &rdev->flags)) {
NeilBrown91adb562009-03-31 14:39:39 +11007455 working_disks++;
NeilBrown2f115882010-06-17 17:41:03 +10007456 continue;
7457 }
NeilBrownc148ffd2009-11-13 17:47:00 +11007458 /* This disc is not fully in-sync. However if it
7459 * just stored parity (beyond the recovery_offset),
7460 * when we don't need to be concerned about the
7461 * array being dirty.
7462 * When reshape goes 'backwards', we never have
7463 * partially completed devices, so we only need
7464 * to worry about reshape going forwards.
7465 */
7466 /* Hack because v0.91 doesn't store recovery_offset properly. */
7467 if (mddev->major_version == 0 &&
7468 mddev->minor_version > 90)
7469 rdev->recovery_offset = reshape_offset;
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007470
NeilBrownc148ffd2009-11-13 17:47:00 +11007471 if (rdev->recovery_offset < reshape_offset) {
7472 /* We need to check old and new layout */
7473 if (!only_parity(rdev->raid_disk,
7474 conf->algorithm,
7475 conf->raid_disks,
7476 conf->max_degraded))
7477 continue;
7478 }
7479 if (!only_parity(rdev->raid_disk,
7480 conf->prev_algo,
7481 conf->previous_raid_disks,
7482 conf->max_degraded))
7483 continue;
7484 dirty_parity_disks++;
7485 }
NeilBrown91adb562009-03-31 14:39:39 +11007486
NeilBrown17045f52011-12-23 10:17:53 +11007487 /*
7488 * 0 for a fully functional array, 1 or 2 for a degraded array.
7489 */
Song Liu2e38a372017-01-24 10:45:30 -08007490 mddev->degraded = raid5_calc_degraded(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007491
NeilBrown674806d2010-06-16 17:17:53 +10007492 if (has_failed(conf)) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007493 pr_crit("md/raid:%s: not enough operational devices (%d/%d failed)\n",
NeilBrown02c2de82006-10-03 01:15:47 -07007494 mdname(mddev), mddev->degraded, conf->raid_disks);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007495 goto abort;
7496 }
7497
NeilBrown91adb562009-03-31 14:39:39 +11007498 /* device size must be a multiple of chunk size */
Andre Noll9d8f0362009-06-18 08:45:01 +10007499 mddev->dev_sectors &= ~(mddev->chunk_sectors - 1);
NeilBrown91adb562009-03-31 14:39:39 +11007500 mddev->resync_max_sectors = mddev->dev_sectors;
7501
NeilBrownc148ffd2009-11-13 17:47:00 +11007502 if (mddev->degraded > dirty_parity_disks &&
Linus Torvalds1da177e2005-04-16 15:20:36 -07007503 mddev->recovery_cp != MaxSector) {
Artur Paszkiewicz4536bf9b2017-03-09 10:00:01 +01007504 if (test_bit(MD_HAS_PPL, &mddev->flags))
7505 pr_crit("md/raid:%s: starting dirty degraded array with PPL.\n",
7506 mdname(mddev));
7507 else if (mddev->ok_start_degraded)
NeilBrowncc6167b2016-11-02 14:16:50 +11007508 pr_crit("md/raid:%s: starting dirty degraded array - data corruption possible.\n",
7509 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007510 else {
NeilBrowncc6167b2016-11-02 14:16:50 +11007511 pr_crit("md/raid:%s: cannot start dirty degraded array.\n",
7512 mdname(mddev));
NeilBrown6ff8d8ec2006-01-06 00:20:15 -08007513 goto abort;
7514 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007515 }
7516
NeilBrowncc6167b2016-11-02 14:16:50 +11007517 pr_info("md/raid:%s: raid level %d active with %d out of %d devices, algorithm %d\n",
7518 mdname(mddev), conf->level,
7519 mddev->raid_disks-mddev->degraded, mddev->raid_disks,
7520 mddev->new_layout);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007521
7522 print_raid5_conf(conf);
7523
NeilBrownfef9c612009-03-31 15:16:46 +11007524 if (conf->reshape_progress != MaxSector) {
NeilBrownfef9c612009-03-31 15:16:46 +11007525 conf->reshape_safe = conf->reshape_progress;
NeilBrownf6705572006-03-27 01:18:11 -08007526 atomic_set(&conf->reshape_stripes, 0);
7527 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
7528 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
7529 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
7530 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
7531 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10007532 "reshape");
Aditya Pakkie406f122019-03-04 16:48:54 -06007533 if (!mddev->sync_thread)
7534 goto abort;
NeilBrownf6705572006-03-27 01:18:11 -08007535 }
7536
Linus Torvalds1da177e2005-04-16 15:20:36 -07007537 /* Ok, everything is just fine now */
NeilBrowna64c8762010-04-14 17:15:37 +10007538 if (mddev->to_remove == &raid5_attrs_group)
7539 mddev->to_remove = NULL;
NeilBrown00bcb4a2010-06-01 19:37:23 +10007540 else if (mddev->kobj.sd &&
7541 sysfs_create_group(&mddev->kobj, &raid5_attrs_group))
NeilBrowncc6167b2016-11-02 14:16:50 +11007542 pr_warn("raid5: failed to create sysfs attributes for %s\n",
7543 mdname(mddev));
NeilBrown4a5add42010-06-01 19:37:28 +10007544 md_set_array_sectors(mddev, raid5_size(mddev, 0, 0));
7545
7546 if (mddev->queue) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007547 int chunk_size;
NeilBrown4a5add42010-06-01 19:37:28 +10007548 /* read-ahead size must cover two whole stripes, which
7549 * is 2 * (datadisks) * chunksize where 'n' is the
7550 * number of raid devices
7551 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07007552 int data_disks = conf->previous_raid_disks - conf->max_degraded;
7553 int stripe = data_disks *
7554 ((mddev->chunk_sectors << 9) / PAGE_SIZE);
NeilBrown4a5add42010-06-01 19:37:28 +10007555
NeilBrown9f7c2222010-07-26 12:04:13 +10007556 chunk_size = mddev->chunk_sectors << 9;
7557 blk_queue_io_min(mddev->queue, chunk_size);
Christoph Hellwig16ef5102020-09-24 08:51:33 +02007558 raid5_set_io_opt(conf);
Kent Overstreetc78afc62013-07-11 22:39:53 -07007559 mddev->queue->limits.raid_partial_stripes_expensive = 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007560 /*
7561 * We can only discard a whole stripe. It doesn't make sense to
7562 * discard data disk but write parity disk
7563 */
7564 stripe = stripe * PAGE_SIZE;
NeilBrown4ac68752012-11-19 13:11:26 +11007565 /* Round up to power of 2, as discard handling
7566 * currently assumes that */
7567 while ((stripe-1) & stripe)
7568 stripe = (stripe | (stripe-1)) + 1;
Shaohua Li620125f2012-10-11 13:49:05 +11007569 mddev->queue->limits.discard_alignment = stripe;
7570 mddev->queue->limits.discard_granularity = stripe;
Konstantin Khlebnikove8d7c332016-11-27 19:32:32 +03007571
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007572 blk_queue_max_write_same_sectors(mddev->queue, 0);
Christoph Hellwig3deff1a2017-04-05 19:21:03 +02007573 blk_queue_max_write_zeroes_sectors(mddev->queue, 0);
H. Peter Anvin5026d7a2013-06-12 07:37:43 -07007574
NeilBrown05616be2012-05-21 09:27:00 +10007575 rdev_for_each(rdev, mddev) {
NeilBrown9f7c2222010-07-26 12:04:13 +10007576 disk_stack_limits(mddev->gendisk, rdev->bdev,
7577 rdev->data_offset << 9);
NeilBrown05616be2012-05-21 09:27:00 +10007578 disk_stack_limits(mddev->gendisk, rdev->bdev,
7579 rdev->new_data_offset << 9);
7580 }
Shaohua Li620125f2012-10-11 13:49:05 +11007581
Christoph Hellwig48920ff2017-04-05 19:21:23 +02007582 /*
7583 * zeroing is required, otherwise data
7584 * could be lost. Consider a scenario: discard a stripe
7585 * (the stripe could be inconsistent if
7586 * discard_zeroes_data is 0); write one disk of the
7587 * stripe (the stripe could be inconsistent again
7588 * depending on which disks are used to calculate
7589 * parity); the disk is broken; The stripe data of this
7590 * disk is lost.
7591 *
7592 * We only allow DISCARD if the sysadmin has confirmed that
7593 * only safe devices are in use by setting a module parameter.
7594 * A better idea might be to turn DISCARD into WRITE_ZEROES
7595 * requests, as that is required to be safe.
7596 */
7597 if (devices_handle_discard_safely &&
Jes Sorensene7597e62016-02-16 16:44:24 -05007598 mddev->queue->limits.max_discard_sectors >= (stripe >> 9) &&
7599 mddev->queue->limits.discard_granularity >= stripe)
Bart Van Assche8b904b52018-03-07 17:10:10 -08007600 blk_queue_flag_set(QUEUE_FLAG_DISCARD,
Shaohua Li620125f2012-10-11 13:49:05 +11007601 mddev->queue);
7602 else
Bart Van Assche8b904b52018-03-07 17:10:10 -08007603 blk_queue_flag_clear(QUEUE_FLAG_DISCARD,
Shaohua Li620125f2012-10-11 13:49:05 +11007604 mddev->queue);
Shaohua Li1dffddd2016-09-08 10:49:06 -07007605
7606 blk_queue_max_hw_sectors(mddev->queue, UINT_MAX);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007607 }
7608
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007609 if (log_init(conf, journal_dev, raid5_has_ppl(conf)))
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007610 goto abort;
Shaohua Li5c7e81c2015-08-13 14:32:04 -07007611
Linus Torvalds1da177e2005-04-16 15:20:36 -07007612 return 0;
7613abort:
NeilBrown01f96c02011-09-21 15:30:20 +10007614 md_unregister_thread(&mddev->thread);
NeilBrowne4f869d2011-10-07 14:22:49 +11007615 print_raid5_conf(conf);
7616 free_conf(conf);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007617 mddev->private = NULL;
NeilBrowncc6167b2016-11-02 14:16:50 +11007618 pr_warn("md/raid:%s: failed to run raid set.\n", mdname(mddev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007619 return -EIO;
7620}
7621
NeilBrownafa0f552014-12-15 12:56:58 +11007622static void raid5_free(struct mddev *mddev, void *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007623{
NeilBrownafa0f552014-12-15 12:56:58 +11007624 struct r5conf *conf = priv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007625
Dan Williams95fc17a2009-07-31 12:39:15 +10007626 free_conf(conf);
NeilBrowna64c8762010-04-14 17:15:37 +10007627 mddev->to_remove = &raid5_attrs_group;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007628}
7629
Shaohua Li849674e2016-01-20 13:52:20 -08007630static void raid5_status(struct seq_file *seq, struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007631{
NeilBrownd1688a62011-10-11 16:49:52 +11007632 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007633 int i;
7634
Andre Noll9d8f0362009-06-18 08:45:01 +10007635 seq_printf(seq, " level %d, %dk chunk, algorithm %d", mddev->level,
NeilBrown3cb5edf2015-07-15 17:24:17 +10007636 conf->chunk_sectors / 2, mddev->layout);
NeilBrown02c2de82006-10-03 01:15:47 -07007637 seq_printf (seq, " [%d/%d] [", conf->raid_disks, conf->raid_disks - mddev->degraded);
NeilBrown5fd13352016-06-02 16:19:52 +10007638 rcu_read_lock();
7639 for (i = 0; i < conf->raid_disks; i++) {
7640 struct md_rdev *rdev = rcu_dereference(conf->disks[i].rdev);
7641 seq_printf (seq, "%s", rdev && test_bit(In_sync, &rdev->flags) ? "U" : "_");
7642 }
7643 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07007644 seq_printf (seq, "]");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007645}
7646
NeilBrownd1688a62011-10-11 16:49:52 +11007647static void print_raid5_conf (struct r5conf *conf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007648{
7649 int i;
7650 struct disk_info *tmp;
7651
NeilBrowncc6167b2016-11-02 14:16:50 +11007652 pr_debug("RAID conf printout:\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007653 if (!conf) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007654 pr_debug("(conf==NULL)\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07007655 return;
7656 }
NeilBrowncc6167b2016-11-02 14:16:50 +11007657 pr_debug(" --- level:%d rd:%d wd:%d\n", conf->level,
NeilBrown0c55e022010-05-03 14:09:02 +10007658 conf->raid_disks,
7659 conf->raid_disks - conf->mddev->degraded);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007660
7661 for (i = 0; i < conf->raid_disks; i++) {
7662 char b[BDEVNAME_SIZE];
7663 tmp = conf->disks + i;
7664 if (tmp->rdev)
NeilBrowncc6167b2016-11-02 14:16:50 +11007665 pr_debug(" disk %d, o:%d, dev:%s\n",
NeilBrown0c55e022010-05-03 14:09:02 +10007666 i, !test_bit(Faulty, &tmp->rdev->flags),
7667 bdevname(tmp->rdev->bdev, b));
Linus Torvalds1da177e2005-04-16 15:20:36 -07007668 }
7669}
7670
NeilBrownfd01b882011-10-11 16:47:53 +11007671static int raid5_spare_active(struct mddev *mddev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007672{
7673 int i;
NeilBrownd1688a62011-10-11 16:49:52 +11007674 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007675 struct disk_info *tmp;
NeilBrown6b965622010-08-18 11:56:59 +10007676 int count = 0;
7677 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007678
7679 for (i = 0; i < conf->raid_disks; i++) {
7680 tmp = conf->disks + i;
NeilBrowndd054fc2011-12-23 10:17:53 +11007681 if (tmp->replacement
7682 && tmp->replacement->recovery_offset == MaxSector
7683 && !test_bit(Faulty, &tmp->replacement->flags)
7684 && !test_and_set_bit(In_sync, &tmp->replacement->flags)) {
7685 /* Replacement has just become active. */
7686 if (!tmp->rdev
7687 || !test_and_clear_bit(In_sync, &tmp->rdev->flags))
7688 count++;
7689 if (tmp->rdev) {
7690 /* Replaced device not technically faulty,
7691 * but we need to be sure it gets removed
7692 * and never re-added.
7693 */
7694 set_bit(Faulty, &tmp->rdev->flags);
7695 sysfs_notify_dirent_safe(
7696 tmp->rdev->sysfs_state);
7697 }
7698 sysfs_notify_dirent_safe(tmp->replacement->sysfs_state);
7699 } else if (tmp->rdev
NeilBrown70fffd02010-06-16 17:01:25 +10007700 && tmp->rdev->recovery_offset == MaxSector
NeilBrownb2d444d2005-11-08 21:39:31 -08007701 && !test_bit(Faulty, &tmp->rdev->flags)
NeilBrownc04be0a2006-10-03 01:15:53 -07007702 && !test_and_set_bit(In_sync, &tmp->rdev->flags)) {
NeilBrown6b965622010-08-18 11:56:59 +10007703 count++;
Jonathan Brassow43c73ca2011-01-14 09:14:33 +11007704 sysfs_notify_dirent_safe(tmp->rdev->sysfs_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007705 }
7706 }
NeilBrown6b965622010-08-18 11:56:59 +10007707 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08007708 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007709 spin_unlock_irqrestore(&conf->device_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007710 print_raid5_conf(conf);
NeilBrown6b965622010-08-18 11:56:59 +10007711 return count;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007712}
7713
NeilBrownb8321b62011-12-23 10:17:51 +11007714static int raid5_remove_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007715{
NeilBrownd1688a62011-10-11 16:49:52 +11007716 struct r5conf *conf = mddev->private;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007717 int err = 0;
NeilBrownb8321b62011-12-23 10:17:51 +11007718 int number = rdev->raid_disk;
NeilBrown657e3e42011-12-23 10:17:52 +11007719 struct md_rdev **rdevp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007720 struct disk_info *p = conf->disks + number;
7721
7722 print_raid5_conf(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007723 if (test_bit(Journal, &rdev->flags) && conf->log) {
Shaohua Lic2bb6242015-10-08 21:54:07 -07007724 /*
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007725 * we can't wait pending write here, as this is called in
7726 * raid5d, wait will deadlock.
NeilBrown84dd97a2017-03-15 14:05:14 +11007727 * neilb: there is no locking about new writes here,
7728 * so this cannot be safe.
Shaohua Lic2bb6242015-10-08 21:54:07 -07007729 */
Song Liu70d466f2017-05-11 15:28:28 -07007730 if (atomic_read(&conf->active_stripes) ||
7731 atomic_read(&conf->r5c_cached_full_stripes) ||
7732 atomic_read(&conf->r5c_cached_partial_stripes)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007733 return -EBUSY;
NeilBrown84dd97a2017-03-15 14:05:14 +11007734 }
Artur Paszkiewiczff875732017-03-09 09:59:58 +01007735 log_exit(conf);
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007736 return 0;
Shaohua Lic2bb6242015-10-08 21:54:07 -07007737 }
NeilBrown657e3e42011-12-23 10:17:52 +11007738 if (rdev == p->rdev)
7739 rdevp = &p->rdev;
7740 else if (rdev == p->replacement)
7741 rdevp = &p->replacement;
7742 else
7743 return 0;
NeilBrownec32a2b2009-03-31 15:17:38 +11007744
NeilBrown657e3e42011-12-23 10:17:52 +11007745 if (number >= conf->raid_disks &&
7746 conf->reshape_progress == MaxSector)
7747 clear_bit(In_sync, &rdev->flags);
7748
7749 if (test_bit(In_sync, &rdev->flags) ||
7750 atomic_read(&rdev->nr_pending)) {
7751 err = -EBUSY;
7752 goto abort;
7753 }
7754 /* Only remove non-faulty devices if recovery
7755 * isn't possible.
7756 */
7757 if (!test_bit(Faulty, &rdev->flags) &&
7758 mddev->recovery_disabled != conf->recovery_disabled &&
7759 !has_failed(conf) &&
NeilBrowndd054fc2011-12-23 10:17:53 +11007760 (!p->replacement || p->replacement == rdev) &&
NeilBrown657e3e42011-12-23 10:17:52 +11007761 number < conf->raid_disks) {
7762 err = -EBUSY;
7763 goto abort;
7764 }
7765 *rdevp = NULL;
NeilBrownd787be42016-06-02 16:19:53 +10007766 if (!test_bit(RemoveSynchronized, &rdev->flags)) {
7767 synchronize_rcu();
7768 if (atomic_read(&rdev->nr_pending)) {
7769 /* lost the race, try later */
7770 err = -EBUSY;
7771 *rdevp = rdev;
7772 }
7773 }
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007774 if (!err) {
7775 err = log_modify(conf, rdev, false);
7776 if (err)
7777 goto abort;
7778 }
NeilBrownd787be42016-06-02 16:19:53 +10007779 if (p->replacement) {
NeilBrowndd054fc2011-12-23 10:17:53 +11007780 /* We must have just cleared 'rdev' */
7781 p->rdev = p->replacement;
7782 clear_bit(Replacement, &p->replacement->flags);
7783 smp_mb(); /* Make sure other CPUs may see both as identical
7784 * but will never see neither - if they are careful
7785 */
7786 p->replacement = NULL;
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007787
7788 if (!err)
7789 err = log_modify(conf, p->rdev, true);
Guoqing Jiange5bc9c32017-04-24 15:58:04 +08007790 }
7791
7792 clear_bit(WantReplacement, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007793abort:
7794
7795 print_raid5_conf(conf);
7796 return err;
7797}
7798
NeilBrownfd01b882011-10-11 16:47:53 +11007799static int raid5_add_disk(struct mddev *mddev, struct md_rdev *rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007800{
NeilBrownd1688a62011-10-11 16:49:52 +11007801 struct r5conf *conf = mddev->private;
Xiao Nid9771f52019-06-14 15:41:05 -07007802 int ret, err = -EEXIST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007803 int disk;
7804 struct disk_info *p;
Neil Brown6c2fce22008-06-28 08:31:31 +10007805 int first = 0;
7806 int last = conf->raid_disks - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007807
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007808 if (test_bit(Journal, &rdev->flags)) {
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007809 if (conf->log)
7810 return -EBUSY;
7811
7812 rdev->raid_disk = 0;
7813 /*
7814 * The array is in readonly mode if journal is missing, so no
7815 * write requests running. We should be safe
7816 */
Xiao Nid9771f52019-06-14 15:41:05 -07007817 ret = log_init(conf, rdev, false);
7818 if (ret)
7819 return ret;
7820
7821 ret = r5l_start(conf->log);
7822 if (ret)
7823 return ret;
7824
Shaohua Lif6b6ec52015-12-21 10:51:02 +11007825 return 0;
7826 }
NeilBrown7f0da592011-07-28 11:39:22 +10007827 if (mddev->recovery_disabled == conf->recovery_disabled)
7828 return -EBUSY;
7829
NeilBrowndc10c642012-03-19 12:46:37 +11007830 if (rdev->saved_raid_disk < 0 && has_failed(conf))
Linus Torvalds1da177e2005-04-16 15:20:36 -07007831 /* no point adding a device */
Neil Brown199050e2008-06-28 08:31:33 +10007832 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007833
Neil Brown6c2fce22008-06-28 08:31:31 +10007834 if (rdev->raid_disk >= 0)
7835 first = last = rdev->raid_disk;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007836
7837 /*
NeilBrown16a53ec2006-06-26 00:27:38 -07007838 * find the disk ... but prefer rdev->saved_raid_disk
7839 * if possible.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007840 */
NeilBrown16a53ec2006-06-26 00:27:38 -07007841 if (rdev->saved_raid_disk >= 0 &&
Neil Brown6c2fce22008-06-28 08:31:31 +10007842 rdev->saved_raid_disk >= first &&
NeilBrown16a53ec2006-06-26 00:27:38 -07007843 conf->disks[rdev->saved_raid_disk].rdev == NULL)
NeilBrown5cfb22a2012-07-03 11:46:53 +10007844 first = rdev->saved_raid_disk;
7845
7846 for (disk = first; disk <= last; disk++) {
NeilBrown7bfec5f2011-12-23 10:17:53 +11007847 p = conf->disks + disk;
7848 if (p->rdev == NULL) {
NeilBrownb2d444d2005-11-08 21:39:31 -08007849 clear_bit(In_sync, &rdev->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007850 rdev->raid_disk = disk;
NeilBrown72626682005-09-09 16:23:54 -07007851 if (rdev->saved_raid_disk != disk)
7852 conf->fullsync = 1;
Suzanne Woodd6065f72005-11-08 21:39:27 -08007853 rcu_assign_pointer(p->rdev, rdev);
Artur Paszkiewicz6358c232017-03-09 10:00:02 +01007854
7855 err = log_modify(conf, rdev, true);
7856
NeilBrown5cfb22a2012-07-03 11:46:53 +10007857 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007858 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007859 }
7860 for (disk = first; disk <= last; disk++) {
7861 p = conf->disks + disk;
NeilBrown7bfec5f2011-12-23 10:17:53 +11007862 if (test_bit(WantReplacement, &p->rdev->flags) &&
7863 p->replacement == NULL) {
7864 clear_bit(In_sync, &rdev->flags);
7865 set_bit(Replacement, &rdev->flags);
7866 rdev->raid_disk = disk;
7867 err = 0;
7868 conf->fullsync = 1;
7869 rcu_assign_pointer(p->replacement, rdev);
7870 break;
7871 }
7872 }
NeilBrown5cfb22a2012-07-03 11:46:53 +10007873out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07007874 print_raid5_conf(conf);
Neil Brown199050e2008-06-28 08:31:33 +10007875 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007876}
7877
NeilBrownfd01b882011-10-11 16:47:53 +11007878static int raid5_resize(struct mddev *mddev, sector_t sectors)
Linus Torvalds1da177e2005-04-16 15:20:36 -07007879{
7880 /* no resync is happening, and there is enough space
7881 * on all devices, so we can resize.
7882 * We need to make sure resync covers any new space.
7883 * If the array is shrinking we should possibly wait until
7884 * any io in the removed space completes, but it hardly seems
7885 * worth it.
7886 */
NeilBrowna4a61252012-05-22 13:55:27 +10007887 sector_t newsize;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007888 struct r5conf *conf = mddev->private;
7889
Shaohua Lie254de62018-08-29 11:05:42 -07007890 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07007891 return -EINVAL;
NeilBrown3cb5edf2015-07-15 17:24:17 +10007892 sectors &= ~((sector_t)conf->chunk_sectors - 1);
NeilBrowna4a61252012-05-22 13:55:27 +10007893 newsize = raid5_size(mddev, sectors, mddev->raid_disks);
7894 if (mddev->external_size &&
7895 mddev->array_sectors > newsize)
Dan Williamsb522adc2009-03-31 15:00:31 +11007896 return -EINVAL;
NeilBrowna4a61252012-05-22 13:55:27 +10007897 if (mddev->bitmap) {
Andy Shevchenkoe64e40182018-08-01 15:20:50 -07007898 int ret = md_bitmap_resize(mddev->bitmap, sectors, 0, 0);
NeilBrowna4a61252012-05-22 13:55:27 +10007899 if (ret)
7900 return ret;
7901 }
7902 md_set_array_sectors(mddev, newsize);
NeilBrownb0986362011-05-11 15:52:21 +10007903 if (sectors > mddev->dev_sectors &&
7904 mddev->recovery_cp > mddev->dev_sectors) {
Andre Noll58c0fed2009-03-31 14:33:13 +11007905 mddev->recovery_cp = mddev->dev_sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007906 set_bit(MD_RECOVERY_NEEDED, &mddev->recovery);
7907 }
Andre Noll58c0fed2009-03-31 14:33:13 +11007908 mddev->dev_sectors = sectors;
NeilBrown4b5c7ae2005-07-27 11:43:28 -07007909 mddev->resync_max_sectors = sectors;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007910 return 0;
7911}
7912
NeilBrownfd01b882011-10-11 16:47:53 +11007913static int check_stripe_cache(struct mddev *mddev)
NeilBrown01ee22b2009-06-18 08:47:20 +10007914{
7915 /* Can only proceed if there are plenty of stripe_heads.
7916 * We need a minimum of one full stripe,, and for sensible progress
7917 * it is best to have about 4 times that.
7918 * If we require 4 times, then the default 256 4K stripe_heads will
7919 * allow for chunk sizes up to 256K, which is probably OK.
7920 * If the chunk size is greater, user-space should request more
7921 * stripe_heads first.
7922 */
NeilBrownd1688a62011-10-11 16:49:52 +11007923 struct r5conf *conf = mddev->private;
Yufen Yuc911c462020-07-18 05:29:07 -04007924 if (((mddev->chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007925 > conf->min_nr_stripes ||
Yufen Yuc911c462020-07-18 05:29:07 -04007926 ((mddev->new_chunk_sectors << 9) / RAID5_STRIPE_SIZE(conf)) * 4
NeilBrownedbe83a2015-02-26 12:47:56 +11007927 > conf->min_nr_stripes) {
NeilBrowncc6167b2016-11-02 14:16:50 +11007928 pr_warn("md/raid:%s: reshape: not enough stripes. Needed %lu\n",
7929 mdname(mddev),
7930 ((max(mddev->chunk_sectors, mddev->new_chunk_sectors) << 9)
Yufen Yuc911c462020-07-18 05:29:07 -04007931 / RAID5_STRIPE_SIZE(conf))*4);
NeilBrown01ee22b2009-06-18 08:47:20 +10007932 return 0;
7933 }
7934 return 1;
7935}
7936
NeilBrownfd01b882011-10-11 16:47:53 +11007937static int check_reshape(struct mddev *mddev)
NeilBrown29269552006-03-27 01:18:10 -08007938{
NeilBrownd1688a62011-10-11 16:49:52 +11007939 struct r5conf *conf = mddev->private;
NeilBrown29269552006-03-27 01:18:10 -08007940
Shaohua Lie254de62018-08-29 11:05:42 -07007941 if (raid5_has_log(conf) || raid5_has_ppl(conf))
Shaohua Li713cf5a2015-08-13 14:32:03 -07007942 return -EINVAL;
NeilBrown88ce4932009-03-31 15:24:23 +11007943 if (mddev->delta_disks == 0 &&
7944 mddev->new_layout == mddev->layout &&
Andre Noll664e7c42009-06-18 08:45:27 +10007945 mddev->new_chunk_sectors == mddev->chunk_sectors)
NeilBrown50ac1682009-06-18 08:47:55 +10007946 return 0; /* nothing to do */
NeilBrown674806d2010-06-16 17:17:53 +10007947 if (has_failed(conf))
NeilBrownec32a2b2009-03-31 15:17:38 +11007948 return -EINVAL;
NeilBrownfdcfbbb2013-07-04 16:38:16 +10007949 if (mddev->delta_disks < 0 && mddev->reshape_position == MaxSector) {
NeilBrownec32a2b2009-03-31 15:17:38 +11007950 /* We might be able to shrink, but the devices must
7951 * be made bigger first.
7952 * For raid6, 4 is the minimum size.
7953 * Otherwise 2 is the minimum
7954 */
7955 int min = 2;
7956 if (mddev->level == 6)
7957 min = 4;
7958 if (mddev->raid_disks + mddev->delta_disks < min)
7959 return -EINVAL;
7960 }
NeilBrown29269552006-03-27 01:18:10 -08007961
NeilBrown01ee22b2009-06-18 08:47:20 +10007962 if (!check_stripe_cache(mddev))
NeilBrown29269552006-03-27 01:18:10 -08007963 return -ENOSPC;
NeilBrown29269552006-03-27 01:18:10 -08007964
NeilBrown738a2732015-05-08 18:19:39 +10007965 if (mddev->new_chunk_sectors > mddev->chunk_sectors ||
7966 mddev->delta_disks > 0)
7967 if (resize_chunks(conf,
7968 conf->previous_raid_disks
7969 + max(0, mddev->delta_disks),
7970 max(mddev->new_chunk_sectors,
7971 mddev->chunk_sectors)
7972 ) < 0)
7973 return -ENOMEM;
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02007974
7975 if (conf->previous_raid_disks + mddev->delta_disks <= conf->pool_size)
7976 return 0; /* never bother to shrink */
NeilBrowne56108d62012-10-11 14:24:13 +11007977 return resize_stripes(conf, (conf->previous_raid_disks
7978 + mddev->delta_disks));
NeilBrown63c70c42006-03-27 01:18:13 -08007979}
7980
NeilBrownfd01b882011-10-11 16:47:53 +11007981static int raid5_start_reshape(struct mddev *mddev)
NeilBrown63c70c42006-03-27 01:18:13 -08007982{
NeilBrownd1688a62011-10-11 16:49:52 +11007983 struct r5conf *conf = mddev->private;
NeilBrown3cb03002011-10-11 16:45:26 +11007984 struct md_rdev *rdev;
NeilBrown63c70c42006-03-27 01:18:13 -08007985 int spares = 0;
NeilBrownc04be0a2006-10-03 01:15:53 -07007986 unsigned long flags;
NeilBrown63c70c42006-03-27 01:18:13 -08007987
NeilBrownf4168852007-02-28 20:11:53 -08007988 if (test_bit(MD_RECOVERY_RUNNING, &mddev->recovery))
NeilBrown63c70c42006-03-27 01:18:13 -08007989 return -EBUSY;
7990
NeilBrown01ee22b2009-06-18 08:47:20 +10007991 if (!check_stripe_cache(mddev))
7992 return -ENOSPC;
7993
NeilBrown30b67642012-05-22 13:55:28 +10007994 if (has_failed(conf))
7995 return -EINVAL;
7996
NeilBrownc6563a82012-05-21 09:27:00 +10007997 rdev_for_each(rdev, mddev) {
NeilBrown469518a2011-01-31 11:57:43 +11007998 if (!test_bit(In_sync, &rdev->flags)
7999 && !test_bit(Faulty, &rdev->flags))
NeilBrown29269552006-03-27 01:18:10 -08008000 spares++;
NeilBrownc6563a82012-05-21 09:27:00 +10008001 }
NeilBrown63c70c42006-03-27 01:18:13 -08008002
NeilBrownf4168852007-02-28 20:11:53 -08008003 if (spares - mddev->degraded < mddev->delta_disks - conf->max_degraded)
NeilBrown29269552006-03-27 01:18:10 -08008004 /* Not enough devices even to make a degraded array
8005 * of that size
8006 */
8007 return -EINVAL;
8008
NeilBrownec32a2b2009-03-31 15:17:38 +11008009 /* Refuse to reduce size of the array. Any reductions in
8010 * array size must be through explicit setting of array_size
8011 * attribute.
8012 */
8013 if (raid5_size(mddev, 0, conf->raid_disks + mddev->delta_disks)
8014 < mddev->array_sectors) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008015 pr_warn("md/raid:%s: array size must be reduced before number of disks\n",
8016 mdname(mddev));
NeilBrownec32a2b2009-03-31 15:17:38 +11008017 return -EINVAL;
8018 }
8019
NeilBrownf6705572006-03-27 01:18:11 -08008020 atomic_set(&conf->reshape_stripes, 0);
NeilBrown29269552006-03-27 01:18:10 -08008021 spin_lock_irq(&conf->device_lock);
NeilBrownc46501b2013-08-27 15:52:13 +10008022 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008023 conf->previous_raid_disks = conf->raid_disks;
NeilBrown63c70c42006-03-27 01:18:13 -08008024 conf->raid_disks += mddev->delta_disks;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008025 conf->prev_chunk_sectors = conf->chunk_sectors;
8026 conf->chunk_sectors = mddev->new_chunk_sectors;
NeilBrown88ce4932009-03-31 15:24:23 +11008027 conf->prev_algo = conf->algorithm;
8028 conf->algorithm = mddev->new_layout;
NeilBrown05616be2012-05-21 09:27:00 +10008029 conf->generation++;
8030 /* Code that selects data_offset needs to see the generation update
8031 * if reshape_progress has been set - so a memory barrier needed.
8032 */
8033 smp_mb();
NeilBrown2c810cd2012-05-21 09:27:00 +10008034 if (mddev->reshape_backwards)
NeilBrownfef9c612009-03-31 15:16:46 +11008035 conf->reshape_progress = raid5_size(mddev, 0, 0);
8036 else
8037 conf->reshape_progress = 0;
8038 conf->reshape_safe = conf->reshape_progress;
NeilBrownc46501b2013-08-27 15:52:13 +10008039 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008040 spin_unlock_irq(&conf->device_lock);
8041
NeilBrown4d77e3b2013-08-27 15:57:47 +10008042 /* Now make sure any requests that proceeded on the assumption
8043 * the reshape wasn't running - like Discard or Read - have
8044 * completed.
8045 */
8046 mddev_suspend(mddev);
8047 mddev_resume(mddev);
8048
NeilBrown29269552006-03-27 01:18:10 -08008049 /* Add some new drives, as many as will fit.
8050 * We know there are enough to make the newly sized array work.
NeilBrown3424bf62010-06-17 17:48:26 +10008051 * Don't add devices if we are reducing the number of
8052 * devices in the array. This is because it is not possible
8053 * to correctly record the "partially reconstructed" state of
8054 * such devices during the reshape and confusion could result.
NeilBrown29269552006-03-27 01:18:10 -08008055 */
NeilBrown87a8dec2011-01-31 11:57:43 +11008056 if (mddev->delta_disks >= 0) {
NeilBrowndafb20f2012-03-19 12:46:39 +11008057 rdev_for_each(rdev, mddev)
NeilBrown87a8dec2011-01-31 11:57:43 +11008058 if (rdev->raid_disk < 0 &&
8059 !test_bit(Faulty, &rdev->flags)) {
8060 if (raid5_add_disk(mddev, rdev) == 0) {
NeilBrown87a8dec2011-01-31 11:57:43 +11008061 if (rdev->raid_disk
NeilBrown9d4c7d82012-03-13 11:21:21 +11008062 >= conf->previous_raid_disks)
NeilBrown87a8dec2011-01-31 11:57:43 +11008063 set_bit(In_sync, &rdev->flags);
NeilBrown9d4c7d82012-03-13 11:21:21 +11008064 else
NeilBrown87a8dec2011-01-31 11:57:43 +11008065 rdev->recovery_offset = 0;
Namhyung Kim36fad852011-07-27 11:00:36 +10008066
Damien Le Moal2aada5b2020-07-16 13:54:42 +09008067 /* Failure here is OK */
8068 sysfs_link_rdev(mddev, rdev);
NeilBrown50da0842011-01-31 11:57:43 +11008069 }
NeilBrown87a8dec2011-01-31 11:57:43 +11008070 } else if (rdev->raid_disk >= conf->previous_raid_disks
8071 && !test_bit(Faulty, &rdev->flags)) {
8072 /* This is a spare that was manually added */
8073 set_bit(In_sync, &rdev->flags);
NeilBrown87a8dec2011-01-31 11:57:43 +11008074 }
NeilBrown29269552006-03-27 01:18:10 -08008075
NeilBrown87a8dec2011-01-31 11:57:43 +11008076 /* When a reshape changes the number of devices,
8077 * ->degraded is measured against the larger of the
8078 * pre and post number of devices.
8079 */
NeilBrownec32a2b2009-03-31 15:17:38 +11008080 spin_lock_irqsave(&conf->device_lock, flags);
Song Liu2e38a372017-01-24 10:45:30 -08008081 mddev->degraded = raid5_calc_degraded(conf);
NeilBrownec32a2b2009-03-31 15:17:38 +11008082 spin_unlock_irqrestore(&conf->device_lock, flags);
8083 }
NeilBrown63c70c42006-03-27 01:18:13 -08008084 mddev->raid_disks = conf->raid_disks;
NeilBrowne5164022009-08-03 10:59:57 +10008085 mddev->reshape_position = conf->reshape_progress;
Shaohua Li29530792016-12-08 15:48:19 -08008086 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrownf6705572006-03-27 01:18:11 -08008087
NeilBrown29269552006-03-27 01:18:10 -08008088 clear_bit(MD_RECOVERY_SYNC, &mddev->recovery);
8089 clear_bit(MD_RECOVERY_CHECK, &mddev->recovery);
NeilBrownea358cd2015-06-12 20:05:04 +10008090 clear_bit(MD_RECOVERY_DONE, &mddev->recovery);
NeilBrown29269552006-03-27 01:18:10 -08008091 set_bit(MD_RECOVERY_RESHAPE, &mddev->recovery);
8092 set_bit(MD_RECOVERY_RUNNING, &mddev->recovery);
8093 mddev->sync_thread = md_register_thread(md_do_sync, mddev,
NeilBrown0da3c612009-09-23 18:09:45 +10008094 "reshape");
NeilBrown29269552006-03-27 01:18:10 -08008095 if (!mddev->sync_thread) {
8096 mddev->recovery = 0;
8097 spin_lock_irq(&conf->device_lock);
NeilBrownba8805b2013-11-14 15:16:15 +11008098 write_seqcount_begin(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008099 mddev->raid_disks = conf->raid_disks = conf->previous_raid_disks;
NeilBrownba8805b2013-11-14 15:16:15 +11008100 mddev->new_chunk_sectors =
8101 conf->chunk_sectors = conf->prev_chunk_sectors;
8102 mddev->new_layout = conf->algorithm = conf->prev_algo;
NeilBrown05616be2012-05-21 09:27:00 +10008103 rdev_for_each(rdev, mddev)
8104 rdev->new_data_offset = rdev->data_offset;
8105 smp_wmb();
NeilBrownba8805b2013-11-14 15:16:15 +11008106 conf->generation --;
NeilBrownfef9c612009-03-31 15:16:46 +11008107 conf->reshape_progress = MaxSector;
NeilBrown1e3fa9b2012-03-13 11:21:18 +11008108 mddev->reshape_position = MaxSector;
NeilBrownba8805b2013-11-14 15:16:15 +11008109 write_seqcount_end(&conf->gen_lock);
NeilBrown29269552006-03-27 01:18:10 -08008110 spin_unlock_irq(&conf->device_lock);
8111 return -EAGAIN;
8112 }
NeilBrownc8f517c2009-03-31 15:28:40 +11008113 conf->reshape_checkpoint = jiffies;
NeilBrown29269552006-03-27 01:18:10 -08008114 md_wakeup_thread(mddev->sync_thread);
8115 md_new_event(mddev);
8116 return 0;
8117}
NeilBrown29269552006-03-27 01:18:10 -08008118
NeilBrownec32a2b2009-03-31 15:17:38 +11008119/* This is called from the reshape thread and should make any
8120 * changes needed in 'conf'
8121 */
NeilBrownd1688a62011-10-11 16:49:52 +11008122static void end_reshape(struct r5conf *conf)
NeilBrown29269552006-03-27 01:18:10 -08008123{
NeilBrown29269552006-03-27 01:18:10 -08008124
NeilBrownf6705572006-03-27 01:18:11 -08008125 if (!test_bit(MD_RECOVERY_INTR, &conf->mddev->recovery)) {
NeilBrowndb0505d2017-10-17 16:18:36 +11008126 struct md_rdev *rdev;
Dan Williams80c3a6c2009-03-17 18:10:40 -07008127
NeilBrownf6705572006-03-27 01:18:11 -08008128 spin_lock_irq(&conf->device_lock);
NeilBrowncea9c222009-03-31 15:15:05 +11008129 conf->previous_raid_disks = conf->raid_disks;
Xiao Nib5d27712017-07-05 17:34:04 +08008130 md_finish_reshape(conf->mddev);
NeilBrown05616be2012-05-21 09:27:00 +10008131 smp_wmb();
NeilBrownfef9c612009-03-31 15:16:46 +11008132 conf->reshape_progress = MaxSector;
NeilBrown6cbd8142015-07-24 13:30:32 +10008133 conf->mddev->reshape_position = MaxSector;
NeilBrowndb0505d2017-10-17 16:18:36 +11008134 rdev_for_each(rdev, conf->mddev)
8135 if (rdev->raid_disk >= 0 &&
8136 !test_bit(Journal, &rdev->flags) &&
8137 !test_bit(In_sync, &rdev->flags))
8138 rdev->recovery_offset = MaxSector;
NeilBrownf6705572006-03-27 01:18:11 -08008139 spin_unlock_irq(&conf->device_lock);
NeilBrownb0f9ec02009-03-31 15:27:18 +11008140 wake_up(&conf->wait_for_overlap);
NeilBrown16a53ec2006-06-26 00:27:38 -07008141
Christoph Hellwigc2e4cd52020-09-24 08:51:34 +02008142 if (conf->mddev->queue)
Christoph Hellwig16ef5102020-09-24 08:51:33 +02008143 raid5_set_io_opt(conf);
NeilBrown29269552006-03-27 01:18:10 -08008144 }
NeilBrown29269552006-03-27 01:18:10 -08008145}
8146
NeilBrownec32a2b2009-03-31 15:17:38 +11008147/* This is called from the raid5d thread with mddev_lock held.
8148 * It makes config changes to the device.
8149 */
NeilBrownfd01b882011-10-11 16:47:53 +11008150static void raid5_finish_reshape(struct mddev *mddev)
NeilBrowncea9c222009-03-31 15:15:05 +11008151{
NeilBrownd1688a62011-10-11 16:49:52 +11008152 struct r5conf *conf = mddev->private;
NeilBrowncea9c222009-03-31 15:15:05 +11008153
8154 if (!test_bit(MD_RECOVERY_INTR, &mddev->recovery)) {
8155
BingJing Chang88763912018-02-22 13:34:46 +08008156 if (mddev->delta_disks <= 0) {
NeilBrownec32a2b2009-03-31 15:17:38 +11008157 int d;
NeilBrown908f4fb2011-12-23 10:17:50 +11008158 spin_lock_irq(&conf->device_lock);
Song Liu2e38a372017-01-24 10:45:30 -08008159 mddev->degraded = raid5_calc_degraded(conf);
NeilBrown908f4fb2011-12-23 10:17:50 +11008160 spin_unlock_irq(&conf->device_lock);
NeilBrownec32a2b2009-03-31 15:17:38 +11008161 for (d = conf->raid_disks ;
8162 d < conf->raid_disks - mddev->delta_disks;
NeilBrown1a67dde2009-08-13 10:41:49 +10008163 d++) {
NeilBrown3cb03002011-10-11 16:45:26 +11008164 struct md_rdev *rdev = conf->disks[d].rdev;
NeilBrownda7613b2012-05-22 13:55:33 +10008165 if (rdev)
8166 clear_bit(In_sync, &rdev->flags);
8167 rdev = conf->disks[d].replacement;
8168 if (rdev)
8169 clear_bit(In_sync, &rdev->flags);
NeilBrown1a67dde2009-08-13 10:41:49 +10008170 }
NeilBrowncea9c222009-03-31 15:15:05 +11008171 }
NeilBrown88ce4932009-03-31 15:24:23 +11008172 mddev->layout = conf->algorithm;
Andre Noll09c9e5f2009-06-18 08:45:55 +10008173 mddev->chunk_sectors = conf->chunk_sectors;
NeilBrownec32a2b2009-03-31 15:17:38 +11008174 mddev->reshape_position = MaxSector;
8175 mddev->delta_disks = 0;
NeilBrown2c810cd2012-05-21 09:27:00 +10008176 mddev->reshape_backwards = 0;
NeilBrowncea9c222009-03-31 15:15:05 +11008177 }
8178}
8179
NeilBrownb03e0cc2017-10-19 12:49:15 +11008180static void raid5_quiesce(struct mddev *mddev, int quiesce)
NeilBrown72626682005-09-09 16:23:54 -07008181{
NeilBrownd1688a62011-10-11 16:49:52 +11008182 struct r5conf *conf = mddev->private;
NeilBrown72626682005-09-09 16:23:54 -07008183
NeilBrownb03e0cc2017-10-19 12:49:15 +11008184 if (quiesce) {
8185 /* stop all writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008186 lock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008187 /* '2' tells resync/reshape to pause so that all
8188 * active stripes can drain
8189 */
Song Liua39f7af2016-11-17 15:24:40 -08008190 r5c_flush_cache(conf, INT_MAX);
NeilBrown64bd6602009-08-03 10:59:58 +10008191 conf->quiesce = 2;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008192 wait_event_cmd(conf->wait_for_quiescent,
Raz Ben-Jehuda(caro)46031f92006-12-10 02:20:47 -08008193 atomic_read(&conf->active_stripes) == 0 &&
8194 atomic_read(&conf->active_aligned_reads) == 0,
Shaohua Li566c09c2013-11-14 15:16:17 +11008195 unlock_all_device_hash_locks_irq(conf),
8196 lock_all_device_hash_locks_irq(conf));
NeilBrown64bd6602009-08-03 10:59:58 +10008197 conf->quiesce = 1;
Shaohua Li566c09c2013-11-14 15:16:17 +11008198 unlock_all_device_hash_locks_irq(conf);
NeilBrown64bd6602009-08-03 10:59:58 +10008199 /* allow reshape to continue */
8200 wake_up(&conf->wait_for_overlap);
NeilBrownb03e0cc2017-10-19 12:49:15 +11008201 } else {
8202 /* re-enable writes */
Shaohua Li566c09c2013-11-14 15:16:17 +11008203 lock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008204 conf->quiesce = 0;
Yuanhan Liub1b46482015-05-08 18:19:06 +10008205 wake_up(&conf->wait_for_quiescent);
NeilBrowne464eaf2006-03-27 01:18:14 -08008206 wake_up(&conf->wait_for_overlap);
Shaohua Li566c09c2013-11-14 15:16:17 +11008207 unlock_all_device_hash_locks_irq(conf);
NeilBrown72626682005-09-09 16:23:54 -07008208 }
Tomasz Majchrzak1532d9e2017-12-27 10:31:40 +01008209 log_quiesce(conf, quiesce);
NeilBrown72626682005-09-09 16:23:54 -07008210}
NeilBrownb15c2e52006-01-06 00:20:16 -08008211
NeilBrownfd01b882011-10-11 16:47:53 +11008212static void *raid45_takeover_raid0(struct mddev *mddev, int level)
Trela Maciej54071b32010-03-08 16:02:42 +11008213{
NeilBrowne373ab12011-10-11 16:48:59 +11008214 struct r0conf *raid0_conf = mddev->private;
Randy Dunlapd76c8422011-04-21 09:07:26 -07008215 sector_t sectors;
Trela Maciej54071b32010-03-08 16:02:42 +11008216
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008217 /* for raid0 takeover only one zone is supported */
NeilBrowne373ab12011-10-11 16:48:59 +11008218 if (raid0_conf->nr_strip_zones > 1) {
NeilBrowncc6167b2016-11-02 14:16:50 +11008219 pr_warn("md/raid:%s: cannot takeover raid0 with more than one zone.\n",
8220 mdname(mddev));
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008221 return ERR_PTR(-EINVAL);
8222 }
8223
NeilBrowne373ab12011-10-11 16:48:59 +11008224 sectors = raid0_conf->strip_zone[0].zone_end;
8225 sector_div(sectors, raid0_conf->strip_zone[0].nb_dev);
NeilBrown3b71bd92011-04-20 15:38:18 +10008226 mddev->dev_sectors = sectors;
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008227 mddev->new_level = level;
Trela Maciej54071b32010-03-08 16:02:42 +11008228 mddev->new_layout = ALGORITHM_PARITY_N;
8229 mddev->new_chunk_sectors = mddev->chunk_sectors;
8230 mddev->raid_disks += 1;
8231 mddev->delta_disks = 1;
8232 /* make sure it will be not marked as dirty */
8233 mddev->recovery_cp = MaxSector;
8234
8235 return setup_conf(mddev);
8236}
8237
NeilBrownfd01b882011-10-11 16:47:53 +11008238static void *raid5_takeover_raid1(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008239{
8240 int chunksect;
Shaohua Li6995f0b2016-12-08 15:48:17 -08008241 void *ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008242
8243 if (mddev->raid_disks != 2 ||
8244 mddev->degraded > 1)
8245 return ERR_PTR(-EINVAL);
8246
8247 /* Should check if there are write-behind devices? */
8248
8249 chunksect = 64*2; /* 64K by default */
8250
8251 /* The array must be an exact multiple of chunksize */
8252 while (chunksect && (mddev->array_sectors & (chunksect-1)))
8253 chunksect >>= 1;
8254
Yufen Yuc911c462020-07-18 05:29:07 -04008255 if ((chunksect<<9) < RAID5_STRIPE_SIZE((struct r5conf *)mddev->private))
NeilBrownd562b0c2009-03-31 14:39:39 +11008256 /* array size does not allow a suitable chunk size */
8257 return ERR_PTR(-EINVAL);
8258
8259 mddev->new_level = 5;
8260 mddev->new_layout = ALGORITHM_LEFT_SYMMETRIC;
Andre Noll664e7c42009-06-18 08:45:27 +10008261 mddev->new_chunk_sectors = chunksect;
NeilBrownd562b0c2009-03-31 14:39:39 +11008262
Shaohua Li6995f0b2016-12-08 15:48:17 -08008263 ret = setup_conf(mddev);
Jes Sorensen32cd7cb2017-01-06 19:31:35 -05008264 if (!IS_ERR(ret))
Shaohua Li394ed8e2017-01-04 16:10:19 -08008265 mddev_clear_unsupported_flags(mddev,
8266 UNSUPPORTED_MDDEV_FLAGS);
Shaohua Li6995f0b2016-12-08 15:48:17 -08008267 return ret;
NeilBrownd562b0c2009-03-31 14:39:39 +11008268}
8269
NeilBrownfd01b882011-10-11 16:47:53 +11008270static void *raid5_takeover_raid6(struct mddev *mddev)
NeilBrownfc9739c2009-03-31 14:57:20 +11008271{
8272 int new_layout;
8273
8274 switch (mddev->layout) {
8275 case ALGORITHM_LEFT_ASYMMETRIC_6:
8276 new_layout = ALGORITHM_LEFT_ASYMMETRIC;
8277 break;
8278 case ALGORITHM_RIGHT_ASYMMETRIC_6:
8279 new_layout = ALGORITHM_RIGHT_ASYMMETRIC;
8280 break;
8281 case ALGORITHM_LEFT_SYMMETRIC_6:
8282 new_layout = ALGORITHM_LEFT_SYMMETRIC;
8283 break;
8284 case ALGORITHM_RIGHT_SYMMETRIC_6:
8285 new_layout = ALGORITHM_RIGHT_SYMMETRIC;
8286 break;
8287 case ALGORITHM_PARITY_0_6:
8288 new_layout = ALGORITHM_PARITY_0;
8289 break;
8290 case ALGORITHM_PARITY_N:
8291 new_layout = ALGORITHM_PARITY_N;
8292 break;
8293 default:
8294 return ERR_PTR(-EINVAL);
8295 }
8296 mddev->new_level = 5;
8297 mddev->new_layout = new_layout;
8298 mddev->delta_disks = -1;
8299 mddev->raid_disks -= 1;
8300 return setup_conf(mddev);
8301}
8302
NeilBrownfd01b882011-10-11 16:47:53 +11008303static int raid5_check_reshape(struct mddev *mddev)
NeilBrownb3546032009-03-31 14:56:41 +11008304{
NeilBrown88ce4932009-03-31 15:24:23 +11008305 /* For a 2-drive array, the layout and chunk size can be changed
8306 * immediately as not restriping is needed.
8307 * For larger arrays we record the new value - after validation
8308 * to be used by a reshape pass.
NeilBrownb3546032009-03-31 14:56:41 +11008309 */
NeilBrownd1688a62011-10-11 16:49:52 +11008310 struct r5conf *conf = mddev->private;
NeilBrown597a7112009-06-18 08:47:42 +10008311 int new_chunk = mddev->new_chunk_sectors;
NeilBrownb3546032009-03-31 14:56:41 +11008312
NeilBrown597a7112009-06-18 08:47:42 +10008313 if (mddev->new_layout >= 0 && !algorithm_valid_raid5(mddev->new_layout))
NeilBrownb3546032009-03-31 14:56:41 +11008314 return -EINVAL;
8315 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008316 if (!is_power_of_2(new_chunk))
NeilBrownb3546032009-03-31 14:56:41 +11008317 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008318 if (new_chunk < (PAGE_SIZE>>9))
NeilBrownb3546032009-03-31 14:56:41 +11008319 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008320 if (mddev->array_sectors & (new_chunk-1))
NeilBrownb3546032009-03-31 14:56:41 +11008321 /* not factor of array size */
8322 return -EINVAL;
8323 }
8324
8325 /* They look valid */
8326
NeilBrown88ce4932009-03-31 15:24:23 +11008327 if (mddev->raid_disks == 2) {
NeilBrown597a7112009-06-18 08:47:42 +10008328 /* can make the change immediately */
8329 if (mddev->new_layout >= 0) {
8330 conf->algorithm = mddev->new_layout;
8331 mddev->layout = mddev->new_layout;
NeilBrown88ce4932009-03-31 15:24:23 +11008332 }
8333 if (new_chunk > 0) {
NeilBrown597a7112009-06-18 08:47:42 +10008334 conf->chunk_sectors = new_chunk ;
8335 mddev->chunk_sectors = new_chunk;
NeilBrown88ce4932009-03-31 15:24:23 +11008336 }
Shaohua Li29530792016-12-08 15:48:19 -08008337 set_bit(MD_SB_CHANGE_DEVS, &mddev->sb_flags);
NeilBrown88ce4932009-03-31 15:24:23 +11008338 md_wakeup_thread(mddev->thread);
NeilBrownb3546032009-03-31 14:56:41 +11008339 }
NeilBrown50ac1682009-06-18 08:47:55 +10008340 return check_reshape(mddev);
NeilBrown88ce4932009-03-31 15:24:23 +11008341}
8342
NeilBrownfd01b882011-10-11 16:47:53 +11008343static int raid6_check_reshape(struct mddev *mddev)
NeilBrown88ce4932009-03-31 15:24:23 +11008344{
NeilBrown597a7112009-06-18 08:47:42 +10008345 int new_chunk = mddev->new_chunk_sectors;
NeilBrown50ac1682009-06-18 08:47:55 +10008346
NeilBrown597a7112009-06-18 08:47:42 +10008347 if (mddev->new_layout >= 0 && !algorithm_valid_raid6(mddev->new_layout))
NeilBrown88ce4932009-03-31 15:24:23 +11008348 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008349 if (new_chunk > 0) {
Andre Noll0ba459d2009-06-18 08:46:10 +10008350 if (!is_power_of_2(new_chunk))
NeilBrown88ce4932009-03-31 15:24:23 +11008351 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008352 if (new_chunk < (PAGE_SIZE >> 9))
NeilBrown88ce4932009-03-31 15:24:23 +11008353 return -EINVAL;
NeilBrown597a7112009-06-18 08:47:42 +10008354 if (mddev->array_sectors & (new_chunk-1))
NeilBrown88ce4932009-03-31 15:24:23 +11008355 /* not factor of array size */
8356 return -EINVAL;
NeilBrownb3546032009-03-31 14:56:41 +11008357 }
NeilBrown88ce4932009-03-31 15:24:23 +11008358
8359 /* They look valid */
NeilBrown50ac1682009-06-18 08:47:55 +10008360 return check_reshape(mddev);
NeilBrownb3546032009-03-31 14:56:41 +11008361}
8362
NeilBrownfd01b882011-10-11 16:47:53 +11008363static void *raid5_takeover(struct mddev *mddev)
NeilBrownd562b0c2009-03-31 14:39:39 +11008364{
8365 /* raid5 can take over:
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008366 * raid0 - if there is only one strip zone - make it a raid4 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008367 * raid1 - if there are two drives. We need to know the chunk size
8368 * raid4 - trivial - just use a raid4 layout.
8369 * raid6 - Providing it is a *_6 layout
NeilBrownd562b0c2009-03-31 14:39:39 +11008370 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008371 if (mddev->level == 0)
8372 return raid45_takeover_raid0(mddev, 5);
NeilBrownd562b0c2009-03-31 14:39:39 +11008373 if (mddev->level == 1)
8374 return raid5_takeover_raid1(mddev);
NeilBrowne9d47582009-03-31 14:57:09 +11008375 if (mddev->level == 4) {
8376 mddev->new_layout = ALGORITHM_PARITY_N;
8377 mddev->new_level = 5;
8378 return setup_conf(mddev);
8379 }
NeilBrownfc9739c2009-03-31 14:57:20 +11008380 if (mddev->level == 6)
8381 return raid5_takeover_raid6(mddev);
NeilBrownd562b0c2009-03-31 14:39:39 +11008382
8383 return ERR_PTR(-EINVAL);
8384}
8385
NeilBrownfd01b882011-10-11 16:47:53 +11008386static void *raid4_takeover(struct mddev *mddev)
NeilBrowna78d38a2010-03-22 16:53:49 +11008387{
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008388 /* raid4 can take over:
8389 * raid0 - if there is only one strip zone
8390 * raid5 - if layout is right
NeilBrowna78d38a2010-03-22 16:53:49 +11008391 */
Dan Williamsf1b29bc2010-05-01 18:09:05 -07008392 if (mddev->level == 0)
8393 return raid45_takeover_raid0(mddev, 4);
NeilBrowna78d38a2010-03-22 16:53:49 +11008394 if (mddev->level == 5 &&
8395 mddev->layout == ALGORITHM_PARITY_N) {
8396 mddev->new_layout = 0;
8397 mddev->new_level = 4;
8398 return setup_conf(mddev);
8399 }
8400 return ERR_PTR(-EINVAL);
8401}
NeilBrownd562b0c2009-03-31 14:39:39 +11008402
NeilBrown84fc4b52011-10-11 16:49:58 +11008403static struct md_personality raid5_personality;
NeilBrown245f46c2009-03-31 14:39:39 +11008404
NeilBrownfd01b882011-10-11 16:47:53 +11008405static void *raid6_takeover(struct mddev *mddev)
NeilBrown245f46c2009-03-31 14:39:39 +11008406{
8407 /* Currently can only take over a raid5. We map the
8408 * personality to an equivalent raid6 personality
8409 * with the Q block at the end.
8410 */
8411 int new_layout;
8412
8413 if (mddev->pers != &raid5_personality)
8414 return ERR_PTR(-EINVAL);
8415 if (mddev->degraded > 1)
8416 return ERR_PTR(-EINVAL);
8417 if (mddev->raid_disks > 253)
8418 return ERR_PTR(-EINVAL);
8419 if (mddev->raid_disks < 3)
8420 return ERR_PTR(-EINVAL);
8421
8422 switch (mddev->layout) {
8423 case ALGORITHM_LEFT_ASYMMETRIC:
8424 new_layout = ALGORITHM_LEFT_ASYMMETRIC_6;
8425 break;
8426 case ALGORITHM_RIGHT_ASYMMETRIC:
8427 new_layout = ALGORITHM_RIGHT_ASYMMETRIC_6;
8428 break;
8429 case ALGORITHM_LEFT_SYMMETRIC:
8430 new_layout = ALGORITHM_LEFT_SYMMETRIC_6;
8431 break;
8432 case ALGORITHM_RIGHT_SYMMETRIC:
8433 new_layout = ALGORITHM_RIGHT_SYMMETRIC_6;
8434 break;
8435 case ALGORITHM_PARITY_0:
8436 new_layout = ALGORITHM_PARITY_0_6;
8437 break;
8438 case ALGORITHM_PARITY_N:
8439 new_layout = ALGORITHM_PARITY_N;
8440 break;
8441 default:
8442 return ERR_PTR(-EINVAL);
8443 }
8444 mddev->new_level = 6;
8445 mddev->new_layout = new_layout;
8446 mddev->delta_disks = 1;
8447 mddev->raid_disks += 1;
8448 return setup_conf(mddev);
8449}
8450
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008451static int raid5_change_consistency_policy(struct mddev *mddev, const char *buf)
8452{
8453 struct r5conf *conf;
8454 int err;
8455
8456 err = mddev_lock(mddev);
8457 if (err)
8458 return err;
8459 conf = mddev->private;
8460 if (!conf) {
8461 mddev_unlock(mddev);
8462 return -ENODEV;
8463 }
8464
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008465 if (strncmp(buf, "ppl", 3) == 0) {
Song Liu0bb0c102017-03-27 10:51:33 -07008466 /* ppl only works with RAID 5 */
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008467 if (!raid5_has_ppl(conf) && conf->level == 5) {
8468 err = log_init(conf, NULL, true);
8469 if (!err) {
8470 err = resize_stripes(conf, conf->pool_size);
8471 if (err)
8472 log_exit(conf);
8473 }
Song Liu0bb0c102017-03-27 10:51:33 -07008474 } else
8475 err = -EINVAL;
8476 } else if (strncmp(buf, "resync", 6) == 0) {
8477 if (raid5_has_ppl(conf)) {
8478 mddev_suspend(mddev);
8479 log_exit(conf);
Song Liu0bb0c102017-03-27 10:51:33 -07008480 mddev_resume(mddev);
Artur Paszkiewicz845b9e22017-04-04 13:13:57 +02008481 err = resize_stripes(conf, conf->pool_size);
Song Liu0bb0c102017-03-27 10:51:33 -07008482 } else if (test_bit(MD_HAS_JOURNAL, &conf->mddev->flags) &&
8483 r5l_log_disk_error(conf)) {
8484 bool journal_dev_exists = false;
8485 struct md_rdev *rdev;
8486
8487 rdev_for_each(rdev, mddev)
8488 if (test_bit(Journal, &rdev->flags)) {
8489 journal_dev_exists = true;
8490 break;
8491 }
8492
8493 if (!journal_dev_exists) {
8494 mddev_suspend(mddev);
8495 clear_bit(MD_HAS_JOURNAL, &mddev->flags);
8496 mddev_resume(mddev);
8497 } else /* need remove journal device first */
8498 err = -EBUSY;
8499 } else
8500 err = -EINVAL;
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008501 } else {
8502 err = -EINVAL;
8503 }
8504
8505 if (!err)
8506 md_update_sb(mddev, 1);
8507
8508 mddev_unlock(mddev);
8509
8510 return err;
8511}
8512
Song Liud5d885f2017-11-19 22:17:01 -08008513static int raid5_start(struct mddev *mddev)
8514{
8515 struct r5conf *conf = mddev->private;
8516
8517 return r5l_start(conf->log);
8518}
8519
NeilBrown84fc4b52011-10-11 16:49:58 +11008520static struct md_personality raid6_personality =
NeilBrown16a53ec2006-06-26 00:27:38 -07008521{
8522 .name = "raid6",
8523 .level = 6,
8524 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008525 .make_request = raid5_make_request,
8526 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008527 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008528 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008529 .status = raid5_status,
8530 .error_handler = raid5_error,
NeilBrown16a53ec2006-06-26 00:27:38 -07008531 .hot_add_disk = raid5_add_disk,
8532 .hot_remove_disk= raid5_remove_disk,
8533 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008534 .sync_request = raid5_sync_request,
NeilBrown16a53ec2006-06-26 00:27:38 -07008535 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008536 .size = raid5_size,
NeilBrown50ac1682009-06-18 08:47:55 +10008537 .check_reshape = raid6_check_reshape,
NeilBrownf4168852007-02-28 20:11:53 -08008538 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008539 .finish_reshape = raid5_finish_reshape,
NeilBrown16a53ec2006-06-26 00:27:38 -07008540 .quiesce = raid5_quiesce,
NeilBrown245f46c2009-03-31 14:39:39 +11008541 .takeover = raid6_takeover,
Song Liu0bb0c102017-03-27 10:51:33 -07008542 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown16a53ec2006-06-26 00:27:38 -07008543};
NeilBrown84fc4b52011-10-11 16:49:58 +11008544static struct md_personality raid5_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008545{
8546 .name = "raid5",
NeilBrown2604b702006-01-06 00:20:36 -08008547 .level = 5,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008548 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008549 .make_request = raid5_make_request,
8550 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008551 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008552 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008553 .status = raid5_status,
8554 .error_handler = raid5_error,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008555 .hot_add_disk = raid5_add_disk,
8556 .hot_remove_disk= raid5_remove_disk,
8557 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008558 .sync_request = raid5_sync_request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008559 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008560 .size = raid5_size,
NeilBrown63c70c42006-03-27 01:18:13 -08008561 .check_reshape = raid5_check_reshape,
8562 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008563 .finish_reshape = raid5_finish_reshape,
NeilBrown72626682005-09-09 16:23:54 -07008564 .quiesce = raid5_quiesce,
NeilBrownd562b0c2009-03-31 14:39:39 +11008565 .takeover = raid5_takeover,
Artur Paszkiewiczba903a32017-03-09 10:00:03 +01008566 .change_consistency_policy = raid5_change_consistency_policy,
Linus Torvalds1da177e2005-04-16 15:20:36 -07008567};
8568
NeilBrown84fc4b52011-10-11 16:49:58 +11008569static struct md_personality raid4_personality =
Linus Torvalds1da177e2005-04-16 15:20:36 -07008570{
NeilBrown2604b702006-01-06 00:20:36 -08008571 .name = "raid4",
8572 .level = 4,
8573 .owner = THIS_MODULE,
Shaohua Li849674e2016-01-20 13:52:20 -08008574 .make_request = raid5_make_request,
8575 .run = raid5_run,
Song Liud5d885f2017-11-19 22:17:01 -08008576 .start = raid5_start,
NeilBrownafa0f552014-12-15 12:56:58 +11008577 .free = raid5_free,
Shaohua Li849674e2016-01-20 13:52:20 -08008578 .status = raid5_status,
8579 .error_handler = raid5_error,
NeilBrown2604b702006-01-06 00:20:36 -08008580 .hot_add_disk = raid5_add_disk,
8581 .hot_remove_disk= raid5_remove_disk,
8582 .spare_active = raid5_spare_active,
Shaohua Li849674e2016-01-20 13:52:20 -08008583 .sync_request = raid5_sync_request,
NeilBrown2604b702006-01-06 00:20:36 -08008584 .resize = raid5_resize,
Dan Williams80c3a6c2009-03-17 18:10:40 -07008585 .size = raid5_size,
NeilBrown3d378902007-03-26 21:32:13 -08008586 .check_reshape = raid5_check_reshape,
8587 .start_reshape = raid5_start_reshape,
NeilBrowncea9c222009-03-31 15:15:05 +11008588 .finish_reshape = raid5_finish_reshape,
NeilBrown2604b702006-01-06 00:20:36 -08008589 .quiesce = raid5_quiesce,
NeilBrowna78d38a2010-03-22 16:53:49 +11008590 .takeover = raid4_takeover,
Song Liu0bb0c102017-03-27 10:51:33 -07008591 .change_consistency_policy = raid5_change_consistency_policy,
NeilBrown2604b702006-01-06 00:20:36 -08008592};
8593
8594static int __init raid5_init(void)
8595{
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008596 int ret;
8597
Shaohua Li851c30c2013-08-28 14:30:16 +08008598 raid5_wq = alloc_workqueue("raid5wq",
8599 WQ_UNBOUND|WQ_MEM_RECLAIM|WQ_CPU_INTENSIVE|WQ_SYSFS, 0);
8600 if (!raid5_wq)
8601 return -ENOMEM;
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008602
8603 ret = cpuhp_setup_state_multi(CPUHP_MD_RAID5_PREPARE,
8604 "md/raid5:prepare",
8605 raid456_cpu_up_prepare,
8606 raid456_cpu_dead);
8607 if (ret) {
8608 destroy_workqueue(raid5_wq);
8609 return ret;
8610 }
NeilBrown16a53ec2006-06-26 00:27:38 -07008611 register_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008612 register_md_personality(&raid5_personality);
8613 register_md_personality(&raid4_personality);
8614 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07008615}
8616
NeilBrown2604b702006-01-06 00:20:36 -08008617static void raid5_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07008618{
NeilBrown16a53ec2006-06-26 00:27:38 -07008619 unregister_md_personality(&raid6_personality);
NeilBrown2604b702006-01-06 00:20:36 -08008620 unregister_md_personality(&raid5_personality);
8621 unregister_md_personality(&raid4_personality);
Sebastian Andrzej Siewior29c6d1b2016-08-18 14:57:24 +02008622 cpuhp_remove_multi_state(CPUHP_MD_RAID5_PREPARE);
Shaohua Li851c30c2013-08-28 14:30:16 +08008623 destroy_workqueue(raid5_wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07008624}
8625
8626module_init(raid5_init);
8627module_exit(raid5_exit);
8628MODULE_LICENSE("GPL");
NeilBrown0efb9e62009-12-14 12:49:58 +11008629MODULE_DESCRIPTION("RAID4/5/6 (striping with parity) personality for MD");
Linus Torvalds1da177e2005-04-16 15:20:36 -07008630MODULE_ALIAS("md-personality-4"); /* RAID5 */
NeilBrownd9d166c2006-01-06 00:20:51 -08008631MODULE_ALIAS("md-raid5");
8632MODULE_ALIAS("md-raid4");
NeilBrown2604b702006-01-06 00:20:36 -08008633MODULE_ALIAS("md-level-5");
8634MODULE_ALIAS("md-level-4");
NeilBrown16a53ec2006-06-26 00:27:38 -07008635MODULE_ALIAS("md-personality-8"); /* RAID6 */
8636MODULE_ALIAS("md-raid6");
8637MODULE_ALIAS("md-level-6");
8638
8639/* This used to be two separate modules, they were: */
8640MODULE_ALIAS("raid5");
8641MODULE_ALIAS("raid6");